예제 #1
0
 public static bool IsMulticast(this IPAddress address)
 {
     address = address.EnsureIPv6();
     byte[] bytes = address.GetAddressBytes();
     return((address.IsIPv4() && (bytes[15 - 3] & 0xF0) == 0xE0) ||
            (bytes[15 - 15] == 0xFF));
 }
예제 #2
0
        public static bool IsRFC3927(this IPAddress address)
        {
            address = address.EnsureIPv6();
            var bytes = address.GetAddressBytes();

            return(address.IsIPv4() && (bytes[15 - 3] == 169 && bytes[15 - 2] == 254));
        }
예제 #3
0
 /// <summary>240.0.0.0/4 Reserved.</summary>
 public static bool IsRFC1112(this IPAddress address)
 {
     address = address.EnsureIPv6();
     byte[] bytes = address.GetAddressBytes();
     return(address.IsIPv4() && (
                (bytes[15 - 3] & 240) == 240));
 }
예제 #4
0
        public static bool IsValid(this IPAddress address)
        {
            address = address.EnsureIPv6();
            var ip = address.GetAddressBytes();

            // unspecified IPv6 address (::/128)
            byte[] ipNone = new byte[16];
            if ((Utils.ArrayEqual(ip, 0, ipNone, 0, 16) ? 0 : 1) == 0)
            {
                return(false);
            }

            // documentation IPv6 address
            if (address.IsRFC3849())
            {
                return(false);
            }

            if (address.IsIPv4())
            {
                //// INADDR_NONE
                if (Utils.ArrayEqual(ip, 12, new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, 0, 4))
                {
                    return(false);
                }

                //// 0
                if (Utils.ArrayEqual(ip, 12, new byte[] { 0x0, 0x0, 0x0, 0x0 }, 0, 4))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #5
0
 /// <summary>192.0.0.0/24 IETF Protocol Assignments.</summary>
 public static bool IsRFC6890(this IPAddress address)
 {
     address = address.EnsureIPv6();
     byte[] bytes = address.GetAddressBytes();
     return(address.IsIPv4() && (
                bytes[15 - 3] == 192 && bytes[15 - 2] == 0 && bytes[15 - 1] == 0));
 }
예제 #6
0
        public Node FindByIp(IPAddress ip)
        {
            ip = ip.EnsureIPv6();
            var endpoint = new IPEndPoint(ip, 0);

            return(_Nodes.Where(n => Match(endpoint, n.Key, ignorePort: true)).Select(s => s.Key).FirstOrDefault());
        }
예제 #7
0
 /// <summary>198.18.0.0/15 Benchmarking. Not globally reachable.</summary>
 public static bool IsRFC2544(this IPAddress address)
 {
     address = address.EnsureIPv6();
     byte[] bytes = address.GetAddressBytes();
     return(address.IsIPv4() && (
                bytes[15 - 3] == 198 && (bytes[15 - 2] & 254) == 18));
 }
예제 #8
0
        /// <summary>192.175.48.0/24 Direct Delegation AS112 Service. Globally reachable. Not globally unique.</summary>
        public static bool IsRFC7534(this IPAddress address)
        {
            address = address.EnsureIPv6();
            var bytes = address.GetAddressBytes();

            return(address.IsIPv4() && bytes[15 - 3] == 192 && bytes[15 - 2] == 175 && bytes[15 - 1] == 48);
        }
예제 #9
0
 /// <summary>100.64.0.0/10 Shared Address Space. Not globally reachable.</summary>
 public static bool IsRFC6598(this IPAddress address)
 {
     address = address.EnsureIPv6();
     byte[] bytes = address.GetAddressBytes();
     return(address.IsIPv4() && (
                bytes[15 - 3] == 100 && (bytes[15 - 2] & 0xc0) == 64));
 }
예제 #10
0
 public static bool IsRFC1918(this IPAddress address)
 {
     address = address.EnsureIPv6();
     byte[] bytes = address.GetAddressBytes();
     return(address.IsIPv4() && (
                bytes[15 - 3] == 10 ||
                (bytes[15 - 3] == 192 && bytes[15 - 2] == 168) ||
                (bytes[15 - 3] == 172 && (bytes[15 - 2] >= 16 && bytes[15 - 2] <= 31))));
 }
예제 #11
0
 /// <summary>192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24 Documentation. Not globally reachable.</summary>
 public static bool IsRFC5737(this IPAddress address)
 {
     address = address.EnsureIPv6();
     byte[] bytes = address.GetAddressBytes();
     return(address.IsIPv4() && (
                (bytes[15 - 3] == 192 && bytes[15 - 2] == 0 && bytes[15 - 1] == 2) ||
                (bytes[15 - 3] == 198 && bytes[15 - 2] == 51 && bytes[15 - 1] == 100) ||
                (bytes[15 - 3] == 203 && bytes[15 - 2] == 0 && bytes[15 - 1] == 113)));
 }
예제 #12
0
        public static bool IsLocal(this IPAddress address)
        {
            address = address.EnsureIPv6();
            var bytes = address.GetAddressBytes();

            // IPv4 loopback
            if (address.IsIPv4() && (bytes[15 - 3] == 127 || bytes[15 - 3] == 0))
            {
                return(true);
            }

            // IPv6 loopback (::1/128)
            if ((Utils.ArrayEqual(bytes, 0, pchLocal, 0, 16) ? 0 : 1) == 0)
            {
                return(true);
            }

            return(false);
        }
예제 #13
0
        public static byte[] GetGroup(this IPAddress address)
        {
            List <byte> vchRet     = new List <byte>();
            int         nClass     = 2;
            int         nStartByte = 0;
            int         nBits      = 16;

            address = address.EnsureIPv6();
            var bytes = address.GetAddressBytes();

            // all local addresses belong to the same group
            if (address.IsLocal())
            {
                nClass = 255;
                nBits  = 0;
            }

            // all unroutable addresses belong to the same group
            if (!address.IsRoutable(true))
            {
                nClass = 0;
                nBits  = 0;
            }
            // for IPv4 addresses, '1' + the 16 higher-order bits of the IP
            // includes mapped IPv4, SIIT translated IPv4, and the well-known prefix
            else if (address.IsIPv4() || address.IsRFC6145() || address.IsRFC6052())
            {
                nClass     = 1;
                nStartByte = 12;
            }
            // for 6to4 tunnelled addresses, use the encapsulated IPv4 address
            else if (address.IsRFC3964())
            {
                nClass     = 1;
                nStartByte = 2;
            }
            // for Teredo-tunnelled IPv6 addresses, use the encapsulated IPv4 address

            else if (address.IsRFC4380())
            {
                vchRet.Add(1);
                vchRet.Add((byte)(bytes[15 - 3] ^ 0xFF));
                vchRet.Add((byte)(bytes[15 - 2] ^ 0xFF));
                return(vchRet.ToArray());
            }
            else if (address.IsTor())
            {
                nClass     = 3;
                nStartByte = 6;
                nBits      = 4;
            }
            // for he.net, use /36 groups
            else if (bytes[15 - 15] == 0x20 && bytes[15 - 14] == 0x01 && bytes[15 - 13] == 0x04 && bytes[15 - 12] == 0x70)
            {
                nBits = 36;
            }
            // for the rest of the IPv6 network, use /32 groups
            else
            {
                nBits = 32;
            }

            vchRet.Add((byte)nClass);
            while (nBits >= 8)
            {
                vchRet.Add(bytes[15 - (15 - nStartByte)]);
                nStartByte++;
                nBits -= 8;
            }
            if (nBits > 0)
            {
                vchRet.Add((byte)(bytes[15 - (15 - nStartByte)] | ((1 << nBits) - 1)));
            }

            return(vchRet.ToArray());
        }
예제 #14
0
 public NetworkPeer FindByIp(IPAddress ip)
 {
     ip = ip.EnsureIPv6();
     return(this.networkPeers.Where(n => Match(ip, null, n.Key)).Select(s => s.Key).FirstOrDefault());
 }
 public INetworkPeer FindByIp(IPAddress ip)
 {
     ip = ip.EnsureIPv6();
     return(this.networkPeers.FirstOrDefault(n => Match(ip, null, n)));
 }
예제 #16
0
 public Node FindByIp(IPAddress ip)
 {
     ip = ip.EnsureIPv6();
     return(_nodes.Where(n => Match(ip, null, n.Key)).Select(s => s.Key).FirstOrDefault());
 }
예제 #17
0
 public List <INetworkPeer> FindByIp(IPAddress ip)
 {
     ip = ip.EnsureIPv6();
     return(this.networkPeers.Where(n => Match(ip, null, n)).ToList());
 }
예제 #18
0
 public List <INetworkPeer> FindByIp(IPAddress ip)
 {
     ip = ip.EnsureIPv6();
     return(this.networkPeers.Where(n => n.MatchRemoteIPAddress(ip)).ToList());
 }