Exemplo n.º 1
0
    private static int GetIndex(NetworkInterface ni)
    {
        IPInterfaceProperties   ipprops   = ni.GetIPProperties();
        IPv4InterfaceProperties ipv4props = GetIPv4Properties(ipprops);

        if (ipv4props != null)
        {
            return(ipv4props.Index);
        }
        else if (Java_java_net_InetAddressImplFactory.isIPv6Supported())
        {
            IPv6InterfaceProperties ipv6props = GetIPv6Properties(ipprops);
            if (ipv6props != null)
            {
                return(ipv6props.Index);
            }
        }
        return(-1);
    }
Exemplo n.º 2
0
    public static int getMTU0(string name, int ind)
    {
#if FIRST_PASS
        return(0);
#else
        IPInterfaceProperties   ipprops = GetDotNetNetworkInterfaceByIndex(ind).GetIPProperties();
        IPv4InterfaceProperties v4props = GetIPv4Properties(ipprops);
        if (v4props != null)
        {
            return(v4props.Mtu);
        }
        if (Java_java_net_InetAddressImplFactory.isIPv6Supported())
        {
            IPv6InterfaceProperties v6props = GetIPv6Properties(ipprops);
            if (v6props != null)
            {
                return(v6props.Mtu);
            }
        }
        return(-1);
#endif
    }
Exemplo n.º 3
0
    private static NetworkInterfaceInfo GetInterfaces()
    {
        // Since many of the methods in java.net.NetworkInterface end up calling this method and the underlying stuff this is
        // based on isn't very quick either, we cache the array for a couple of seconds.
        if (cache != null && DateTime.UtcNow - cachedSince < new TimeSpan(0, 0, 5))
        {
            return(cache);
        }
        NetworkInterface[] ifaces = NetworkInterface.GetAllNetworkInterfaces();
        // on Mono (on Windows) we need to filter out the network interfaces that don't have any IP properties
        ifaces = Array.FindAll(ifaces, IsValid);
        Array.Sort(ifaces, Compare);
        java.net.NetworkInterface[] ret = new java.net.NetworkInterface[ifaces.Length];
        int eth  = 0;
        int tr   = 0;
        int fddi = 0;
        int lo   = 0;
        int ppp  = 0;
        int sl   = 0;
        int net  = 0;

        for (int i = 0; i < ifaces.Length; i++)
        {
            string name;
            switch (ifaces[i].NetworkInterfaceType)
            {
            case NetworkInterfaceType.Ethernet:
                name = "eth" + eth++;
                break;

            case NetworkInterfaceType.TokenRing:
                name = "tr" + tr++;
                break;

            case NetworkInterfaceType.Fddi:
                name = "fddi" + fddi++;
                break;

            case NetworkInterfaceType.Loopback:
                if (lo > 0)
                {
                    continue;
                }
                name = "lo";
                lo++;
                break;

            case NetworkInterfaceType.Ppp:
                name = "ppp" + ppp++;
                break;

            case NetworkInterfaceType.Slip:
                name = "sl" + sl++;
                break;

            default:
                name = "net" + net++;
                break;
            }
            java.net.NetworkInterface netif = new java.net.NetworkInterface();
            ret[i] = netif;
            netif._set1(name, ifaces[i].Description, GetIndex(ifaces[i]));
            UnicastIPAddressInformationCollection uipaic    = ifaces[i].GetIPProperties().UnicastAddresses;
            List <java.net.InetAddress>           addresses = new List <java.net.InetAddress>();
            List <java.net.InterfaceAddress>      bindings  = new List <java.net.InterfaceAddress>();
            for (int j = 0; j < uipaic.Count; j++)
            {
                IPAddress addr = uipaic[j].Address;
                if (addr.AddressFamily == AddressFamily.InterNetwork)
                {
                    java.net.Inet4Address     address = new java.net.Inet4Address(null, addr.GetAddressBytes());
                    java.net.InterfaceAddress binding = new java.net.InterfaceAddress();
                    short mask = 32;
                    java.net.Inet4Address broadcast = null;
                    IPAddress             v4mask;
                    try
                    {
                        v4mask = uipaic[j].IPv4Mask;
                    }
                    catch (NotImplementedException)
                    {
                        // Mono (as of 2.6.7) doesn't implement the IPv4Mask property
                        v4mask = null;
                    }
                    if (v4mask != null && !v4mask.Equals(IPAddress.Any))
                    {
                        broadcast = new java.net.Inet4Address(null, -1);
                        mask      = 0;
                        foreach (byte b in v4mask.GetAddressBytes())
                        {
                            mask += (short)java.lang.Integer.bitCount(b);
                        }
                    }
                    else if (address.isLoopbackAddress())
                    {
                        mask      = 8;
                        broadcast = new java.net.Inet4Address(null, 0xffffff);
                    }
                    binding._set(address, broadcast, mask);
                    addresses.Add(address);
                    bindings.Add(binding);
                }
                else if (Java_java_net_InetAddressImplFactory.isIPv6Supported())
                {
                    int scope = 0;
                    if (addr.IsIPv6LinkLocal || addr.IsIPv6SiteLocal)
                    {
                        scope = (int)addr.ScopeId;
                    }
                    java.net.Inet6Address ia6 = new java.net.Inet6Address();
                    ia6._holder().ipaddress   = addr.GetAddressBytes();
                    if (scope != 0)
                    {
                        ia6._holder().scope_id         = scope;
                        ia6._holder().scope_id_set     = true;
                        ia6._holder().scope_ifname     = netif;
                        ia6._holder().scope_ifname_set = true;
                    }
                    java.net.InterfaceAddress binding = new java.net.InterfaceAddress();
                    // TODO where do we get the IPv6 subnet prefix length?
                    short mask = 128;
                    binding._set(ia6, null, mask);
                    addresses.Add(ia6);
                    bindings.Add(binding);
                }
            }
            netif._set2(addresses.ToArray(), bindings.ToArray(), new java.net.NetworkInterface[0]);
        }
        NetworkInterfaceInfo nii = new NetworkInterfaceInfo();

        nii.dotnetInterfaces = ifaces;
        nii.javaInterfaces   = ret;
        cache       = nii;
        cachedSince = DateTime.UtcNow;
        return(nii);
    }