Exemple #1
0
        /// <summary>
        ///  Verifies that the given address is on a public network. (It is not a special address or one of the reserved private subnets.)
        /// </summary>
        /// <param name="addr">IP address to check</param>
        /// <returns>True, if the given IP is a public, routable address.</returns>
        public static bool IsPublicIP(IPAddress addr)
        {
            if (addr.AddressFamily != AddressFamily.InterNetwork)
            {
                throw new ArgumentException(String.Format("IPv4 address is required: {0}", addr.ToString()));
            }
            bool unroutable = PrivateClassA.IsIPInSubnet(addr) ||
                              PrivateClassB.IsIPInSubnet(addr) ||
                              PrivateClassC.IsIPInSubnet(addr) ||
                              Loopback.IsIPInSubnet(addr) ||
                              LinkLocal.IsIPInSubnet(addr) ||
                              SharedAddress.IsIPInSubnet(addr) ||
                              IETF.IsIPInSubnet(addr) ||
                              Dummy.IsIPInSubnet(addr) ||
                              PortControlAnycast.IsIPInSubnet(addr) ||
                              NatAnycastTraversal.IsIPInSubnet(addr) ||
                              Nat64Discovery.IsIPInSubnet(addr) ||
                              DNS64Discovery.IsIPInSubnet(addr) ||
                              TestNet1Documentation.IsIPInSubnet(addr) ||
                              AS112.IsIPInSubnet(addr) ||
                              AMT.IsIPInSubnet(addr) ||
                              Relay6to4.IsIPInSubnet(addr) ||
                              AS112DirectDelegation.IsIPInSubnet(addr) ||
                              Benchmarking.IsIPInSubnet(addr) ||
                              TestNet2Documentation.IsIPInSubnet(addr) ||
                              TestNet3Documentation.IsIPInSubnet(addr) ||
                              Reserved.IsIPInSubnet(addr) ||
                              Broadcast.IsIPInSubnet(addr);

            return(!unroutable);
        }
Exemple #2
0
        /// <summary>
        ///  Returns the first IPv4, unicast address on the local computer which has a gateway configured and is, therefore,
        ///  probably Internet-connectable.
        /// </summary>
        /// <returns>IP address object</returns>
        public static IPAddress GetPrimaryIPAddress()
        {
            // This is impossible to find, but we return the first IP which has a gateway configured
            List <IPAndMask> ips = new List <IPAndMask>();

            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface nic in nics)
            {
                if (nic.OperationalStatus == OperationalStatus.Up && nic.Supports(NetworkInterfaceComponent.IPv4))
                {
                    IPInterfaceProperties properties = nic.GetIPProperties();
                    if (properties.GatewayAddresses.Count == 0)
                    {
                        continue;
                    }
                    UnicastIPAddressInformationCollection unicast = properties.UnicastAddresses;
                    foreach (UnicastIPAddressInformation unicastIP in unicast)
                    {
                        if (unicastIP.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            IPv4Subnet subnet = IPv4Subnet.FromSubnet(unicastIP.Address, unicastIP.IPv4Mask);
                            foreach (GatewayIPAddressInformation gw_addr in properties.GatewayAddresses)
                            {
                                if (gw_addr.Address.AddressFamily == AddressFamily.InterNetwork)
                                {
                                    if (subnet.IsIPInSubnet(gw_addr.Address))
                                    {
                                        return(unicastIP.Address);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(null);
        }