/// <summary>
 /// Creates a new unicast network address information instance.
 /// </summary>
 /// <param name="iface">The network interface.</param>
 /// <param name="information">The address information.</param>
 public UnicastNetworkAddressInformation(NetworkInterface iface, UnicastIPAddressInformation information)
     : base(iface)
 {
     this.UnicastInformation = information;
     this.Stale = false;
     this.Selected = true;
 }
        /// <summary>
        /// UnicastIPAddressInformation.IPv4Mask is not implemented in Xamarin. This method sits in a partial class definition
        /// on each native platform and retrieves the netmask in whatever way it can be done for each platform. 
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        protected static IPAddress GetSubnetMask(UnicastIPAddressInformation ip)
        {
            var nativeInterfaceInfo = NetInfo.GetInterfaceInfo();
            var match = nativeInterfaceInfo.FirstOrDefault(ni => ni != null && ni.Address != null && ip != null && ip.Address != null && Equals(ni.Address, ip.Address));

            return match != null ? match.Netmask : null;
        }
Пример #3
0
        /// <summary>
        /// UnicastIPAddressInformation.IPv4Mask is not implemented in Xamarin. This method sits in a partial class definition
        /// on each native platform and retrieves the netmask in whatever way it can be done for each platform. 
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        protected static IPAddress GetSubnetMask(UnicastIPAddressInformation ip)
        {
            // use native calls to get comprehensive interface info
            var nativeInterfaceInfo = NetInfo.GetInterfaceInfo();

            // match or nothing
            var match = nativeInterfaceInfo.FirstOrDefault(ni => ni != null && ni.Address != null && ip != null && ip.Address != null && Equals(ni.Address, ip.Address));

            return match != null ? match.Netmask : null;
        }
Пример #4
0
        public static IPAddress GetBroadcastAddress(this UnicastIPAddressInformation unicast)
        {
            Guard.Argument(unicast).NotNull();

            var ip        = unicast.Address.GetAddressBytes();
            var mask      = unicast.IPv4Mask.GetAddressBytes();
            var broadcast = new byte[ip.Length];

            for (var a = 0; a < ip.Length; a++)
            {
                broadcast[a] = (byte)(ip[a] | (byte)~mask[a]);
            }

            return(new(broadcast));
        }
        /// <summary>
        /// Gets whether an IP address is connectable from an interface described
        /// by unicast address info by checking the subnet ID. Compatible with IPv4 and
        /// IPv6 addresses. 
        /// </summary>
        /// <param name="address">The address to check for connectivity.</param>
        /// <param name="fromAddress">The unicase ip address information describing the interface.</param>
        /// <returns>True if on the same subnet, false otherwise.</returns>
        public static bool ConnectableFrom(this IPAddress address, UnicastIPAddressInformation fromAddress)
        {
            // If its an IPv4 address and its subnet matches the network subnet
            if (address.AddressFamily == AddressFamily.InterNetwork &&
                fromAddress.Address.AddressFamily == AddressFamily.InterNetwork &&
                address.SameSubnetAs(fromAddress.Address, fromAddress.IPv4Mask))
                return true;

            // If its an IPv6 address and both IP addresses are link local and they are on the same subnet
            if (address.AddressFamily == AddressFamily.InterNetworkV6 &&
                fromAddress.Address.AddressFamily == AddressFamily.InterNetworkV6 &&
                address.SameSubnetAs(fromAddress.Address))
                return true;

            return false;
        }
Пример #6
0
        public static IPAddress CalculateNetwork(UnicastIPAddressInformation addr)
        {
            // The mask will be null in some scenarios, like a dhcp address 169.254.x.x
            if (addr.IPv4Mask == null)
                return null;

            var ip = addr.Address.GetAddressBytes();
            var mask = addr.IPv4Mask.GetAddressBytes();
            var result = new Byte[4];
            for (int i = 0; i < 4; ++i)
            {
                result[i] = (Byte)(ip[i] & mask[i]);
            }

            return new IPAddress(result);
        }
Пример #7
0
 /// <summary>
 /// UnicastIPAddressInformation.IPv4Mask is not implemented in Xamarin. This method sits in a partial class definition
 /// on each native platform and retrieves the netmask in whatever way it can be done for each platform. 
 /// </summary>
 /// <param name="ip"></param>
 /// <returns></returns>
 protected static IPAddress GetSubnetMask(UnicastIPAddressInformation ip)
 {
     return ip.IPv4Mask;
 }
Пример #8
0
    /// <summary>
    /// Returns a collection of all IP addresses in a given subnet except the network address and the broadcast address
    /// </summary>
    /// <param name="network">Subnet for which the IP addresses should be returned</param>
    /// <returns>Collection of IP addresses in the given <see cref="network"/></returns>
    public static ICollection<IPAddress> GetAllAddressesInSubnet(UnicastIPAddressInformation network)
    {
      var address = ToUInt32(network.Address);
      var netMask = ToUInt32(network.IPv4Mask);
      var networkAddress = address & netMask;
      var broadcastAddress = networkAddress ^ ~netMask;

      // we leave out the networkAdress and the broadcastAddress
      var result = new List<IPAddress>();
      for (var i = networkAddress + 1; i < broadcastAddress; i++)
        result.Add(ToIpAddress(i));

      return result;
    }
Пример #9
0
        private Profile MatchNetwork(UnicastIPAddressInformation ipAddr, GatewayIPAddressInformation gw, byte[] addrBytes, byte[] maskBytes)
        {
            var maxConfidence = 0;
            Profile result = null;

            foreach (var item in _profiles)
            {
                var confidence = 6;

                if (item.Subnet.Value == null)
                    confidence -= 1;
                else if (!item.Subnet.Value.Equals(ipAddr.IPv4Mask))
                    confidence = 0;

                if (item.IPAddress.Value == null)
                {
                    confidence -= 1;
                }
                else
                {
                    var compBytes = item.IPAddress.Value.GetAddressBytes();
                    for (var i = 0; i < compBytes.Length; i++)
                    {
                        if ((addrBytes[i] & maskBytes[i]) !=
                            (compBytes[i] & maskBytes[i]))
                        {
                            confidence = 0;
                            break;
                        }
                    }
                }

                if (item.Gateway.Value == null)
                    confidence -= 1;
                else if (!item.Gateway.Value.Equals(gw.Address))
                    confidence = 0;

                confidence = Math.Max(0, confidence);
                if (confidence > maxConfidence)
                {
                    maxConfidence = confidence;
                    result = item;
                }
            }

            return result;
        }
Пример #10
0
		internal override bool Equals(UnicastIPAddressInformation item)
		{
			return base.Equals(item) && item.PrefixLength.Equals(Network.Cidr);
		}
Пример #11
0
		internal Ipv6UnicastAddress(UnicastIPAddressInformation unicastIp)
		{
			Update(unicastIp);
			Network = NetworkCalculator.V6(Address, Mask);
		}
Пример #12
0
        private static IPAddress CalculateNetwork(UnicastIPAddressInformation addr)
        {
            if (addr.IPv4Mask == null)
                return null;

               Byte [] ip = addr.Address.GetAddressBytes();
            Byte[] mask = addr.IPv4Mask.GetAddressBytes();
            Byte[] result = new Byte[4];
            for (int i = 0; i < 4; ++i)
            {
                result[i] = (Byte)(ip[i] & mask[i]);
            }

            return new IPAddress(result);
        }
 private static bool NonTransientAddress(UnicastIPAddressInformation address)
 {
     return !address.IsTransient;
 }
Пример #14
0
 // An address is valid if it is a non-transient addresss (if global, must be DNS-eligible as well)
 static bool NonTransientAddress(UnicastIPAddressInformation address)
 {
     return (!address.IsTransient);
 }
Пример #15
0
		internal override bool Equals(UnicastIPAddressInformation item)
		{
			return base.Equals(item) && item.IPv4Mask.Equals(Mask);
		}
        private static bool IsIPAddressValid(UnicastIPAddressInformation addr)
        {
            Log.V(TAG, "    Checking IP Address {0}", addr.Address);
            if(addr.Address.AddressFamily == AddressFamily.InterNetwork) {
                var bytes = addr.Address.GetAddressBytes();
                var correct = bytes[0] != 169 && bytes[1] != 254;
                if(correct) {
                    Log.I(TAG, "Found acceptable IPv4 address {0}", addr.Address);
                } else {
                    Log.V(TAG, "    Rejecting link-local IPv4 address");
                }

                return correct;
            } else if(addr.Address.AddressFamily == AddressFamily.InterNetworkV6) {
                var correct = !addr.Address.IsIPv6LinkLocal;
                if(correct) {
                    Log.I(TAG, "Found acceptable IPv6 address {0}", addr.Address);
                } else {
                    Log.V(TAG, "    Rejecting link-local IPv6 address");
                }

                return correct;
            }

            Log.V(TAG, "   IP Address type is invalid");
            return false;
        }