public void AddNode(EndPoint nodeEP) { switch (nodeEP.AddressFamily) { case AddressFamily.InterNetwork: if (!NetUtilities.IsPrivateIPv4((nodeEP as IPEndPoint).Address)) { _ipv4InternetDhtNode.AddNode(nodeEP); } break; case AddressFamily.InterNetworkV6: if (NetUtilities.IsPublicIPv6((nodeEP as IPEndPoint).Address)) { _ipv6InternetDhtNode.AddNode(nodeEP); } break; case AddressFamily.Unspecified: _torInternetDhtNode?.AddNode(nodeEP); break; } }
public static async Task <List <Network> > GetNetworksAsync(DnsClient dnsClient) { List <Network> availableNetworks = new List <Network>(); foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) { if (nic.OperationalStatus != OperationalStatus.Up) { continue; } List <IPEndPoint> bindEPs = new List <IPEndPoint>(); { IPInterfaceProperties ipInterface = nic.GetIPProperties(); List <IPAddress> _ipv6TempAddresses = new List <IPAddress>(); List <IPAddress> _ipv6Addresses = new List <IPAddress>(); foreach (GatewayIPAddressInformation gateway in ipInterface.GatewayAddresses) { foreach (UnicastIPAddressInformation ip in ipInterface.UnicastAddresses) { if (ip.Address.AddressFamily != gateway.Address.AddressFamily) { continue; } switch (ip.Address.AddressFamily) { case AddressFamily.InterNetwork: if (gateway.Address.ConvertIpToNumber() == 0u) { bindEPs.Add(new IPEndPoint(ip.Address, 0)); } else { int subnetMaskWidth = ip.IPv4Mask.GetSubnetMaskWidth(); if (ip.Address.GetNetworkAddress(subnetMaskWidth).Equals(gateway.Address.GetNetworkAddress(subnetMaskWidth))) { bindEPs.Add(new IPEndPoint(ip.Address, 0)); } } break; case AddressFamily.InterNetworkV6: if (NetUtilities.IsPublicIPv6(ip.Address)) { if (ip.DuplicateAddressDetectionState == DuplicateAddressDetectionState.Preferred) { if (ip.SuffixOrigin == SuffixOrigin.Random) { _ipv6TempAddresses.Add(ip.Address); } else { _ipv6Addresses.Add(ip.Address); } } } break; } } } if (_ipv6TempAddresses.Count > 0) { foreach (IPAddress address in _ipv6TempAddresses) { bindEPs.Add(new IPEndPoint(address, 0)); } } else if (_ipv6Addresses.Count > 0) { foreach (IPAddress address in _ipv6Addresses) { bindEPs.Add(new IPEndPoint(address, 0)); } } } if (bindEPs.Count > 0) { List <Task <IPEndPoint> > checkNetworkTasks = new List <Task <IPEndPoint> >(); foreach (IPEndPoint bindEP in bindEPs) { checkNetworkTasks.Add(CheckNetworkAsync(dnsClient, bindEP)); } availableNetworks.Add(new Network(checkNetworkTasks)); } } List <Network> workingNetworks = new List <Network>(); foreach (Network network in availableNetworks) { await network.SelectWorkingNetworksAsync(); if ((network._ipv4BindEPs.Count > 0) || (network._ipv6BindEPs.Count > 0)) { workingNetworks.Add(network); } } return(workingNetworks); }