示例#1
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));
        }
示例#2
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);
        }