Exemple #1
0
            public override NetworkInterface [] GetAllNetworkInterfaces()
            {
                var    interfaces = new Dictionary <string, LinuxNetworkInterface> ();
                IntPtr ifap;

                if (GetInterfaceAddresses(out ifap) != 0)
                {
                    throw new SystemException("getifaddrs() failed");
                }

                try {
                    IntPtr next = ifap;
                    while (next != IntPtr.Zero)
                    {
                        ifaddrs              addr          = (ifaddrs)Marshal.PtrToStructure(next, typeof(ifaddrs));
                        IPAddress            address       = IPAddress.None;
                        string               name          = addr.ifa_name;
                        int                  index         = -1;
                        byte[]               macAddress    = null;
                        NetworkInterfaceType type          = NetworkInterfaceType.Unknown;
                        int                  nullNameCount = 0;

                        if (addr.ifa_addr != IntPtr.Zero)
                        {
                            sockaddr_in sockaddr = (sockaddr_in)Marshal.PtrToStructure(addr.ifa_addr, typeof(sockaddr_in));

                            if (sockaddr.sin_family == AF_INET6)
                            {
                                sockaddr_in6 sockaddr6 = (sockaddr_in6)Marshal.PtrToStructure(addr.ifa_addr, typeof(sockaddr_in6));
                                address = new IPAddress(sockaddr6.sin6_addr.u6_addr8, sockaddr6.sin6_scope_id);
                            }
                            else if (sockaddr.sin_family == AF_INET)
                            {
                                address = new IPAddress(sockaddr.sin_addr);
                            }
                            else if (sockaddr.sin_family == AF_PACKET)
                            {
                                sockaddr_ll sockaddrll = (sockaddr_ll)Marshal.PtrToStructure(addr.ifa_addr, typeof(sockaddr_ll));
                                if (((int)sockaddrll.sll_halen) > sockaddrll.sll_addr.Length)
                                {
                                    Console.Error.WriteLine("Got a bad hardware address length for an AF_PACKET {0} {1}",
                                                            sockaddrll.sll_halen, sockaddrll.sll_addr.Length);
                                    next = addr.ifa_next;
                                    continue;
                                }

                                macAddress = new byte [(int)sockaddrll.sll_halen];
                                Array.Copy(sockaddrll.sll_addr, 0, macAddress, 0, macAddress.Length);
                                index = sockaddrll.sll_ifindex;

                                int hwtype = (int)sockaddrll.sll_hatype;
                                if (Enum.IsDefined(typeof(LinuxArpHardware), hwtype))
                                {
                                    switch ((LinuxArpHardware)hwtype)
                                    {
                                    case LinuxArpHardware.EETHER:
                                        goto case LinuxArpHardware.ETHER;

                                    case LinuxArpHardware.ETHER:
                                        type = NetworkInterfaceType.Ethernet;
                                        break;

                                    case LinuxArpHardware.PRONET:
                                        type = NetworkInterfaceType.TokenRing;
                                        break;

                                    case LinuxArpHardware.ATM:
                                        type = NetworkInterfaceType.Atm;
                                        break;

                                    case LinuxArpHardware.SLIP:
                                    case LinuxArpHardware.CSLIP:
                                    case LinuxArpHardware.SLIP6:
                                    case LinuxArpHardware.CSLIP6:
                                        type = NetworkInterfaceType.Slip;
                                        break;

                                    case LinuxArpHardware.PPP:
                                        type = NetworkInterfaceType.Ppp;
                                        break;

                                    case LinuxArpHardware.LOOPBACK:
                                        type       = NetworkInterfaceType.Loopback;
                                        macAddress = null;
                                        break;

                                    case LinuxArpHardware.FDDI:
                                        type = NetworkInterfaceType.Fddi;
                                        break;

                                    case LinuxArpHardware.SIT:
                                    case LinuxArpHardware.IPDDP:
                                    case LinuxArpHardware.IPGRE:
                                    case LinuxArpHardware.IP6GRE:
                                    case LinuxArpHardware.TUNNEL6:
                                    case LinuxArpHardware.TUNNEL:
                                        type = NetworkInterfaceType.Tunnel;
                                        break;
                                    }
                                }
                            }
                        }

                        LinuxNetworkInterface iface = null;

                        if (String.IsNullOrEmpty(name))
                        {
                            name = "\0" + (++nullNameCount).ToString();
                        }

                        if (!interfaces.TryGetValue(name, out iface))
                        {
                            iface = new LinuxNetworkInterface(name);
                            interfaces.Add(name, iface);
                        }

                        if (!address.Equals(IPAddress.None))
                        {
                            iface.AddAddress(address);
                        }

                        if (macAddress != null || type == NetworkInterfaceType.Loopback)
                        {
                            if (type == NetworkInterfaceType.Ethernet)
                            {
                                if (Directory.Exists(iface.IfacePath + "wireless"))
                                {
                                    type = NetworkInterfaceType.Wireless80211;
                                }
                            }
                            iface.SetLinkLayerInfo(index, macAddress, type);
                        }

                        next = addr.ifa_next;
                    }
                } finally {
                    FreeInterfaceAddresses(ifap);
                }

                NetworkInterface [] result = new NetworkInterface [interfaces.Count];
                int x = 0;

                foreach (NetworkInterface thisInterface in interfaces.Values)
                {
                    result [x] = thisInterface;
                    x++;
                }
                return(result);
            }
        public static NetworkInterface[] ImplGetAllNetworkInterfaces()
        {
            Dictionary <string, MacOsNetworkInterface> dictionary = new Dictionary <string, MacOsNetworkInterface>();
            IntPtr intPtr;

            if (MacOsNetworkInterface.getifaddrs(out intPtr) != 0)
            {
                throw new SystemException("getifaddrs() failed");
            }
            try
            {
                IntPtr intPtr2 = intPtr;
                while (intPtr2 != IntPtr.Zero)
                {
                    ifaddrs              ifaddrs              = (ifaddrs)Marshal.PtrToStructure(intPtr2, typeof(ifaddrs));
                    IPAddress            ipaddress            = IPAddress.None;
                    string               ifa_name             = ifaddrs.ifa_name;
                    int                  index                = -1;
                    byte[]               array                = null;
                    NetworkInterfaceType networkInterfaceType = NetworkInterfaceType.Unknown;
                    if (ifaddrs.ifa_addr != IntPtr.Zero)
                    {
                        sockaddr sockaddr = (sockaddr)Marshal.PtrToStructure(ifaddrs.ifa_addr, typeof(sockaddr));
                        if (sockaddr.sa_family == 30)
                        {
                            sockaddr_in6 sockaddr_in = (sockaddr_in6)Marshal.PtrToStructure(ifaddrs.ifa_addr, typeof(sockaddr_in6));
                            ipaddress = new IPAddress(sockaddr_in.sin6_addr.u6_addr8, (long)((ulong)sockaddr_in.sin6_scope_id));
                        }
                        else if (sockaddr.sa_family == 2)
                        {
                            ipaddress = new IPAddress((long)((ulong)((sockaddr_in)Marshal.PtrToStructure(ifaddrs.ifa_addr, typeof(sockaddr_in))).sin_addr));
                        }
                        else if (sockaddr.sa_family == 18)
                        {
                            sockaddr_dl sockaddr_dl = (sockaddr_dl)Marshal.PtrToStructure(ifaddrs.ifa_addr, typeof(sockaddr_dl));
                            array = new byte[(int)sockaddr_dl.sdl_alen];
                            Array.Copy(sockaddr_dl.sdl_data, (int)sockaddr_dl.sdl_nlen, array, 0, Math.Min(array.Length, sockaddr_dl.sdl_data.Length - (int)sockaddr_dl.sdl_nlen));
                            index = (int)sockaddr_dl.sdl_index;
                            int sdl_type = (int)sockaddr_dl.sdl_type;
                            if (Enum.IsDefined(typeof(MacOsArpHardware), sdl_type))
                            {
                                MacOsArpHardware macOsArpHardware = (MacOsArpHardware)sdl_type;
                                switch (macOsArpHardware)
                                {
                                case MacOsArpHardware.PPP:
                                    networkInterfaceType = NetworkInterfaceType.Ppp;
                                    break;

                                case MacOsArpHardware.LOOPBACK:
                                    networkInterfaceType = NetworkInterfaceType.Loopback;
                                    array = null;
                                    break;

                                default:
                                    if (macOsArpHardware != MacOsArpHardware.ETHER)
                                    {
                                        if (macOsArpHardware != MacOsArpHardware.FDDI)
                                        {
                                            if (macOsArpHardware == MacOsArpHardware.ATM)
                                            {
                                                networkInterfaceType = NetworkInterfaceType.Atm;
                                            }
                                        }
                                        else
                                        {
                                            networkInterfaceType = NetworkInterfaceType.Fddi;
                                        }
                                    }
                                    else
                                    {
                                        networkInterfaceType = NetworkInterfaceType.Ethernet;
                                    }
                                    break;

                                case MacOsArpHardware.SLIP:
                                    networkInterfaceType = NetworkInterfaceType.Slip;
                                    break;
                                }
                            }
                        }
                    }
                    MacOsNetworkInterface macOsNetworkInterface = null;
                    if (!dictionary.TryGetValue(ifa_name, out macOsNetworkInterface))
                    {
                        macOsNetworkInterface = new MacOsNetworkInterface(ifa_name);
                        dictionary.Add(ifa_name, macOsNetworkInterface);
                    }
                    if (!ipaddress.Equals(IPAddress.None))
                    {
                        macOsNetworkInterface.AddAddress(ipaddress);
                    }
                    if (array != null || networkInterfaceType == NetworkInterfaceType.Loopback)
                    {
                        macOsNetworkInterface.SetLinkLayerInfo(index, array, networkInterfaceType);
                    }
                    intPtr2 = ifaddrs.ifa_next;
                }
            }
            finally
            {
                MacOsNetworkInterface.freeifaddrs(intPtr);
            }
            NetworkInterface[] array2 = new NetworkInterface[dictionary.Count];
            int num = 0;

            foreach (NetworkInterface networkInterface in dictionary.Values)
            {
                array2[num] = networkInterface;
                num++;
            }
            return(array2);
        }
        public static NetworkInterface[] ImplGetAllNetworkInterfaces()
        {
            Dictionary <string, LinuxNetworkInterface> dictionary = new Dictionary <string, LinuxNetworkInterface>();

            if (GetInterfaceAddresses(out IntPtr ifap) != 0)
            {
                throw new SystemException("getifaddrs() failed");
            }
            try
            {
                IntPtr intPtr = ifap;
                int    num    = 0;
                while (intPtr != IntPtr.Zero)
                {
                    ifaddrs              ifaddrs              = (ifaddrs)Marshal.PtrToStructure(intPtr, typeof(ifaddrs));
                    IPAddress            iPAddress            = IPAddress.None;
                    string               text                 = ifaddrs.ifa_name;
                    int                  index                = -1;
                    byte[]               array                = null;
                    NetworkInterfaceType networkInterfaceType = NetworkInterfaceType.Unknown;
                    if (ifaddrs.ifa_addr != IntPtr.Zero)
                    {
                        sockaddr_in sockaddr_in = (sockaddr_in)Marshal.PtrToStructure(ifaddrs.ifa_addr, typeof(sockaddr_in));
                        if (sockaddr_in.sin_family == 10)
                        {
                            sockaddr_in6 sockaddr_in2 = (sockaddr_in6)Marshal.PtrToStructure(ifaddrs.ifa_addr, typeof(sockaddr_in6));
                            iPAddress = new IPAddress(sockaddr_in2.sin6_addr.u6_addr8, sockaddr_in2.sin6_scope_id);
                        }
                        else if (sockaddr_in.sin_family == 2)
                        {
                            iPAddress = new IPAddress(sockaddr_in.sin_addr);
                        }
                        else if (sockaddr_in.sin_family == 17)
                        {
                            sockaddr_ll sockaddr_ll = (sockaddr_ll)Marshal.PtrToStructure(ifaddrs.ifa_addr, typeof(sockaddr_ll));
                            if (sockaddr_ll.sll_halen > sockaddr_ll.sll_addr.Length)
                            {
                                Console.Error.WriteLine("Got a bad hardware address length for an AF_PACKET {0} {1}", sockaddr_ll.sll_halen, sockaddr_ll.sll_addr.Length);
                                intPtr = ifaddrs.ifa_next;
                                continue;
                            }
                            array = new byte[sockaddr_ll.sll_halen];
                            Array.Copy(sockaddr_ll.sll_addr, 0, array, 0, array.Length);
                            index = sockaddr_ll.sll_ifindex;
                            int sll_hatype = sockaddr_ll.sll_hatype;
                            if (Enum.IsDefined(typeof(LinuxArpHardware), sll_hatype))
                            {
                                switch (sll_hatype)
                                {
                                case 1:
                                case 2:
                                    networkInterfaceType = NetworkInterfaceType.Ethernet;
                                    break;

                                case 4:
                                    networkInterfaceType = NetworkInterfaceType.TokenRing;
                                    break;

                                case 19:
                                    networkInterfaceType = NetworkInterfaceType.Atm;
                                    break;

                                case 256:
                                    networkInterfaceType = NetworkInterfaceType.Slip;
                                    break;

                                case 512:
                                    networkInterfaceType = NetworkInterfaceType.Ppp;
                                    break;

                                case 772:
                                    networkInterfaceType = NetworkInterfaceType.Loopback;
                                    array = null;
                                    break;

                                case 774:
                                    networkInterfaceType = NetworkInterfaceType.Fddi;
                                    break;

                                case 768:
                                case 769:
                                    networkInterfaceType = NetworkInterfaceType.Tunnel;
                                    break;
                                }
                            }
                        }
                    }
                    LinuxNetworkInterface value = null;
                    if (string.IsNullOrEmpty(text))
                    {
                        int num2 = ++num;
                        text = "\0" + num2.ToString();
                    }
                    if (!dictionary.TryGetValue(text, out value))
                    {
                        value = new LinuxNetworkInterface(text);
                        dictionary.Add(text, value);
                    }
                    if (!iPAddress.Equals(IPAddress.None))
                    {
                        value.AddAddress(iPAddress);
                    }
                    if (array != null || networkInterfaceType == NetworkInterfaceType.Loopback)
                    {
                        if (networkInterfaceType == NetworkInterfaceType.Ethernet && Directory.Exists(value.IfacePath + "wireless"))
                        {
                            networkInterfaceType = NetworkInterfaceType.Wireless80211;
                        }
                        value.SetLinkLayerInfo(index, array, networkInterfaceType);
                    }
                    intPtr = ifaddrs.ifa_next;
                }
            }
            finally
            {
                FreeInterfaceAddresses(ifap);
            }
            NetworkInterface[] array2 = new NetworkInterface[dictionary.Count];
            int num3 = 0;

            foreach (LinuxNetworkInterface value2 in dictionary.Values)
            {
                NetworkInterface networkInterface = array2[num3] = value2;
                num3++;
            }
            return(array2);
        }
Exemple #4
0
        public static NetworkInterface[] ImplGetAllNetworkInterfaces()
        {
            Dictionary <string, LinuxNetworkInterface> dictionary = new Dictionary <string, LinuxNetworkInterface>();
            IntPtr intPtr;

            if (LinuxNetworkInterface.GetInterfaceAddresses(out intPtr) != 0)
            {
                throw new SystemException("getifaddrs() failed");
            }
            try
            {
                IntPtr intPtr2 = intPtr;
                int    num     = 0;
                while (intPtr2 != IntPtr.Zero)
                {
                    ifaddrs              ifaddrs              = (ifaddrs)Marshal.PtrToStructure(intPtr2, typeof(ifaddrs));
                    IPAddress            ipaddress            = IPAddress.None;
                    string               text                 = ifaddrs.ifa_name;
                    int                  index                = -1;
                    byte[]               array                = null;
                    NetworkInterfaceType networkInterfaceType = NetworkInterfaceType.Unknown;
                    if (ifaddrs.ifa_addr != IntPtr.Zero)
                    {
                        sockaddr_in sockaddr_in = (sockaddr_in)Marshal.PtrToStructure(ifaddrs.ifa_addr, typeof(sockaddr_in));
                        if (sockaddr_in.sin_family == 10)
                        {
                            sockaddr_in6 sockaddr_in2 = (sockaddr_in6)Marshal.PtrToStructure(ifaddrs.ifa_addr, typeof(sockaddr_in6));
                            ipaddress = new IPAddress(sockaddr_in2.sin6_addr.u6_addr8, (long)((ulong)sockaddr_in2.sin6_scope_id));
                        }
                        else if (sockaddr_in.sin_family == 2)
                        {
                            ipaddress = new IPAddress((long)((ulong)sockaddr_in.sin_addr));
                        }
                        else if (sockaddr_in.sin_family == 17)
                        {
                            sockaddr_ll sockaddr_ll = (sockaddr_ll)Marshal.PtrToStructure(ifaddrs.ifa_addr, typeof(sockaddr_ll));
                            if ((int)sockaddr_ll.sll_halen > sockaddr_ll.sll_addr.Length)
                            {
                                Console.Error.WriteLine("Got a bad hardware address length for an AF_PACKET {0} {1}", sockaddr_ll.sll_halen, sockaddr_ll.sll_addr.Length);
                                intPtr2 = ifaddrs.ifa_next;
                                continue;
                            }
                            array = new byte[(int)sockaddr_ll.sll_halen];
                            Array.Copy(sockaddr_ll.sll_addr, 0, array, 0, array.Length);
                            index = sockaddr_ll.sll_ifindex;
                            int sll_hatype = (int)sockaddr_ll.sll_hatype;
                            if (Enum.IsDefined(typeof(LinuxArpHardware), sll_hatype))
                            {
                                LinuxArpHardware linuxArpHardware = (LinuxArpHardware)sll_hatype;
                                switch (linuxArpHardware)
                                {
                                case LinuxArpHardware.TUNNEL:
                                    break;

                                case LinuxArpHardware.TUNNEL6:
                                    break;

                                default:
                                    switch (linuxArpHardware)
                                    {
                                    case LinuxArpHardware.ETHER:
                                        break;

                                    case LinuxArpHardware.EETHER:
                                        break;

                                    default:
                                        if (linuxArpHardware == LinuxArpHardware.ATM)
                                        {
                                            networkInterfaceType = NetworkInterfaceType.Atm;
                                            goto IL_27D;
                                        }
                                        if (linuxArpHardware == LinuxArpHardware.SLIP)
                                        {
                                            networkInterfaceType = NetworkInterfaceType.Slip;
                                            goto IL_27D;
                                        }
                                        if (linuxArpHardware != LinuxArpHardware.PPP)
                                        {
                                            goto IL_27D;
                                        }
                                        networkInterfaceType = NetworkInterfaceType.Ppp;
                                        goto IL_27D;

                                    case LinuxArpHardware.PRONET:
                                        networkInterfaceType = NetworkInterfaceType.TokenRing;
                                        goto IL_27D;
                                    }
                                    networkInterfaceType = NetworkInterfaceType.Ethernet;
                                    goto IL_27D;

                                case LinuxArpHardware.LOOPBACK:
                                    networkInterfaceType = NetworkInterfaceType.Loopback;
                                    array = null;
                                    goto IL_27D;

                                case LinuxArpHardware.FDDI:
                                    networkInterfaceType = NetworkInterfaceType.Fddi;
                                    goto IL_27D;
                                }
                                networkInterfaceType = NetworkInterfaceType.Tunnel;
                            }
                        }
                    }
IL_27D:
                    LinuxNetworkInterface linuxNetworkInterface = null;
                    if (string.IsNullOrEmpty(text))
                    {
                        string str = "\0";
                        int    num2;
                        num  = (num2 = num + 1);
                        text = str + num2.ToString();
                    }
                    if (!dictionary.TryGetValue(text, out linuxNetworkInterface))
                    {
                        linuxNetworkInterface = new LinuxNetworkInterface(text);
                        dictionary.Add(text, linuxNetworkInterface);
                    }
                    if (!ipaddress.Equals(IPAddress.None))
                    {
                        linuxNetworkInterface.AddAddress(ipaddress);
                    }
                    if (array != null || networkInterfaceType == NetworkInterfaceType.Loopback)
                    {
                        if (networkInterfaceType == NetworkInterfaceType.Ethernet && Directory.Exists(linuxNetworkInterface.IfacePath + "wireless"))
                        {
                            networkInterfaceType = NetworkInterfaceType.Wireless80211;
                        }
                        linuxNetworkInterface.SetLinkLayerInfo(index, array, networkInterfaceType);
                    }
                    intPtr2 = ifaddrs.ifa_next;
                }
            }
            finally
            {
                LinuxNetworkInterface.FreeInterfaceAddresses(intPtr);
            }
            NetworkInterface[] array2 = new NetworkInterface[dictionary.Count];
            int num3 = 0;

            foreach (NetworkInterface networkInterface in dictionary.Values)
            {
                array2[num3] = networkInterface;
                num3++;
            }
            return(array2);
        }