示例#1
0
        public static bool IsInSameSubnet(this System.Net.IPAddress address2, System.Net.IPAddress address, System.Net.IPAddress subnetMask)
        {
            System.Net.IPAddress network1 = address.GetNetworkAddress(subnetMask);
            System.Net.IPAddress network2 = address2.GetNetworkAddress(subnetMask);

            return(network1.Equals(network2));
        }
示例#2
0
        private static bool PeekARP(string address)
        {
            System.Net.IPAddress adr = System.Net.IPAddress.Parse(address);
            byte[] macAddr           = new byte[6];
            uint   macAddrLen        = (uint)macAddr.Length;

            return(SendARP((int)adr.Address, 0, macAddr, ref macAddrLen) != 0);
        }
示例#3
0
        private static bool PeekARP(System.Net.IPAddress adr)
        {
            byte[] macAddr    = new byte[6];
            uint   macAddrLen = (uint)macAddr.Length;
            int    Ret        = SendARP((int)adr.Address, 0, macAddr, ref macAddrLen);

            System.Diagnostics.Trace.WriteLine(string.Format("Peeking {0} as {1}", adr.ToString(), Ret));
            return(Ret == 0);
        }
示例#4
0
        public static PrefixedIPv4Address NextAddress(PrefixedIPv4Address Orig)
        {
            System.Net.IPAddress adr = null;
            System.Diagnostics.Trace.WriteLine("Parsing address" + Orig.Address);
            if (!System.Net.IPAddress.TryParse(Orig.Address, out adr))
            {
                System.Diagnostics.Trace.WriteLine("Failed with parsing");
                return(Orig);
            }
            long from = (adr.Address >> 24) & 0xff;
            long i;

            /*
             * if (!PeekARP(adr))
             * {   // ARP not available - so just guess
             *  i = from + 1;
             *  if (i >= 254) i = 1;
             *  adr.Address = (adr.Address & 0xffffff) | (i << 24);
             *  return new PrefixedIPv4Address() { Address = adr.ToString(), PrefixLength = Orig.PrefixLength };
             * }*/
            // do ARP scanning, after current
            for (i = from + 1; i <= 254; i++)
            {
                adr.Address = (adr.Address & 0xffffff) | (i << 24);
                if (!PeekARP(adr))
                {
                    break;
                }
            }
            if (i >= 254)
            {
                // do ARP scanning, before current
                for (i = 1; i < from; i++)
                {
                    adr.Address = (adr.Address & 0xffffff) | (i << 24);
                    if (!PeekARP(adr))
                    {
                        break;
                    }
                }
                // nothing found - just guess
                if (i >= from)
                {
                    i = from + 1;
                    if (i >= 254)
                    {
                        i = 1;
                    }
                    adr.Address = (adr.Address & 0xffffff) | (i << 24);
                }
            }
            return(new PrefixedIPv4Address()
            {
                Address = adr.ToString(), PrefixLength = Orig.PrefixLength
            });
        }
示例#5
0
        public static System.Net.IPAddress GetNetworkAddress(this System.Net.IPAddress address, System.Net.IPAddress subnetMask)
        {
            byte[] ipAdressBytes   = address.GetAddressBytes();
            byte[] subnetMaskBytes = subnetMask.GetAddressBytes();

            if (ipAdressBytes.Length != subnetMaskBytes.Length)
            {
                throw new ArgumentException("Lengths of IP address and subnet mask do not match.");
            }

            byte[] broadcastAddress = new byte[ipAdressBytes.Length];
            for (int i = 0; i < broadcastAddress.Length; i++)
            {
                broadcastAddress[i] = (byte)(ipAdressBytes[i] & (subnetMaskBytes[i]));
            }
            return(new System.Net.IPAddress(broadcastAddress));
        }
示例#6
0
        public static bool IsIPv4LinkLocal(this System.Net.IPAddress address)
        {
            var addrBytes = address.GetAddressBytes();

            return(address.AddressFamily == AddressFamily.InterNetwork && addrBytes[0] == 169 && addrBytes[1] == 254);
        }