Ejemplo n.º 1
0
        public static void CreateRoute(string destination, string mask, int interfaceIndex, int metric)
        {
            NetworkAdaptor adaptor = NicInterface.GetNetworkAdaptor(interfaceIndex);
            var            route   = new NativeMethods.MIB_IPFORWARDROW
            {
                dwForwardDest    = BitConverter.ToUInt32(IPAddress.Parse(destination).GetAddressBytes(), 0),
                dwForwardMask    = BitConverter.ToUInt32(IPAddress.Parse(mask).GetAddressBytes(), 0),
                dwForwardNextHop = BitConverter.ToUInt32(IPAddress.Parse(adaptor.PrimaryGateway.ToString()).GetAddressBytes(), 0),
                dwForwardMetric1 = Convert.ToUInt32(metric),
                dwForwardType    = Convert.ToUInt32(3), //Default to 3
                dwForwardProto   = Convert.ToUInt32(3), //Default to 3
                dwForwardAge     = 0,
                dwForwardIfIndex = Convert.ToUInt32(interfaceIndex)
            };

            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.MIB_IPFORWARDROW)));

            try
            {
                Marshal.StructureToPtr(route, ptr, false);
                var status = NativeMethods.CreateIpForwardEntry(ptr);
            }
            finally
            {
                Marshal.FreeHGlobal(ptr);
            }
        }
Ejemplo n.º 2
0
        public static List <NetworkAdaptor> GetAllNetworkAdaptor()
        {
            List <NetworkAdaptor> naList = new List <NetworkAdaptor>();

            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                IPInterfaceProperties   properties    = adapter.GetIPProperties();
                IPv4InterfaceProperties ip4Properties = null;
                if (!HasIp4Support(adapter))
                {
                    continue;
                }
                else
                {
                    ip4Properties = properties.GetIPv4Properties();
                }

                NetworkAdaptor na = new NetworkAdaptor();
                na.Name             = adapter.Name;
                na.Description      = adapter.Description;
                na.MACAddress       = adapter.GetPhysicalAddress().ToString();
                na.InterfaceIndex   = ip4Properties != null ? ip4Properties.Index : 0;
                na.PrimaryIpAddress = properties.UnicastAddresses.Where(i => i.Address.AddressFamily == AddressFamily.InterNetwork)?.First()?.Address;
                na.SubnetMask       = properties.UnicastAddresses.Where(i => i.Address.AddressFamily == AddressFamily.InterNetwork)?.First()?.IPv4Mask;
                if (properties.GatewayAddresses.Count > 0)
                {
                    na.PrimaryGateway = null;
                    foreach (GatewayIPAddressInformation gatewayInfo in properties.GatewayAddresses)
                    {
                        if (gatewayInfo.Address != null && gatewayInfo.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            na.PrimaryGateway = gatewayInfo.Address;
                            break;
                        }
                    }
                }
                else
                {
                    //if the gateways on the Network adaptor properties is null, then get it from the routing table
                    List <Ip4RouteEntry> routeTable = Ip4RouteTable.GetRouteTable();
                    if (routeTable.Where(i => i.InterfaceIndex == na.InterfaceIndex).Count() > 0)
                    {
                        na.PrimaryGateway = routeTable.Where(i => i.InterfaceIndex == na.InterfaceIndex)?.First()?.GatewayIP;
                    }
                }
                if (na.PrimaryGateway == null && properties.DhcpServerAddresses.Count > 0)
                {
                    na.PrimaryGateway = properties.DhcpServerAddresses.First();
                }
                naList.Add(na);
            }
            return(naList);
        }
Ejemplo n.º 3
0
        public static NetworkAdaptor GetNetworkAdaptor(int interfaceIndex)
        {
            NetworkAdaptor na = null;

            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                IPInterfaceProperties properties = adapter.GetIPProperties();
                if (!HasIp4Support(adapter))
                {
                    continue;
                }
                IPv4InterfaceProperties ip4Properties = properties.GetIPv4Properties();
                if (properties.GetIPv4Properties().Index == interfaceIndex)
                {
                    na                  = new NetworkAdaptor();
                    na.Name             = adapter.Name;
                    na.Description      = adapter.Description;
                    na.MACAddress       = adapter.GetPhysicalAddress().ToString();
                    na.InterfaceIndex   = ip4Properties.Index;
                    na.PrimaryIpAddress = properties.UnicastAddresses.Where(i => i.Address.AddressFamily == AddressFamily.InterNetwork)?.First()?.Address;
                    na.SubnetMask       = properties.UnicastAddresses.Where(i => i.Address.AddressFamily == AddressFamily.InterNetwork)?.First()?.IPv4Mask;
                    if (properties.GatewayAddresses.Count > 0)
                    {
                        na.PrimaryGateway = null;
                        foreach (GatewayIPAddressInformation gatewayInfo in properties.GatewayAddresses)
                        {
                            if (gatewayInfo.Address != null && gatewayInfo.Address.AddressFamily == AddressFamily.InterNetwork)
                            {
                                na.PrimaryGateway = gatewayInfo.Address;
                                break;
                            }
                        }
                    }
                    else
                    {
                        //if the gateways on the Network adaptor properties is null, then get it from the routing table, especially the case for VPN routers
                        List <Ip4RouteEntry> routeTable = Ip4RouteTable.GetRouteTable();
                        if (routeTable.Where(i => i.InterfaceIndex == na.InterfaceIndex)?.Count() > 0)
                        {
                            na.PrimaryGateway = routeTable.Where(i => i.InterfaceIndex == na.InterfaceIndex)?.First()?.GatewayIP;
                        }
                    }
                    //not ideal and incorrect, but hopefully it doesn't execute this as the gateways are defined elsewhere
                    //the correct way is to locate the primary gateway in some other property other than the 3 methods here
                    if (na.PrimaryGateway == null && properties.DhcpServerAddresses.Count > 0)
                    {
                        na.PrimaryGateway = properties.DhcpServerAddresses.First();
                    }
                    break;
                }
            }
            return(na);
        }