Beispiel #1
0
        public static bool RouteExists(string destinationIP)
        {
            List <Ip4RouteEntry> routeTable = Ip4RouteTable.GetRouteTable();
            Ip4RouteEntry        routeEntry = routeTable.Find(i => i.DestinationIP.ToString().Equals(destinationIP));

            return(routeEntry != null);
        }
Beispiel #2
0
        public static bool InterfaceIndexExists(int interfaceIndex)
        {
            List <Ip4RouteEntry> routeTable = Ip4RouteTable.GetRouteTable();
            Ip4RouteEntry        routeEntry = routeTable.Find(i => i.InterfaceIndex.Equals(interfaceIndex));

            return(routeEntry != null);
        }
Beispiel #3
0
        public static List <Ip4RouteEntry> GetRouteEntry(string destinationIP, string mask)
        {
            List <Ip4RouteEntry> routeTable   = Ip4RouteTable.GetRouteTable();
            List <Ip4RouteEntry> routeMatches = routeTable.FindAll(i => i.DestinationIP.ToString().Equals(destinationIP) && i.SubnetMask.ToString().Equals(mask));

            return(routeMatches);
        }
        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);
        }
        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);
        }
Beispiel #6
0
        public static void DeleteRoute(string destinationIP, string mask)
        {
            List <Ip4RouteEntry> routeMatches = Ip4RouteTable.GetRouteEntry(destinationIP, mask);

            if (routeMatches == null)
            {
                return;
            }

            foreach (Ip4RouteEntry routeEntry in routeMatches)
            {
                DeleteRoute(routeEntry);
            }
        }