Пример #1
0
        public static NetworkInterface [] ImplGetAllNetworkInterfaces()
        {
            var    interfaces = new Dictionary <string, LinuxNetworkInterface> ();
            IntPtr ifap;

            if (getifaddrs(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;

                    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:
                                    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.TUNNEL6:
                                    goto case LinuxArpHardware.TUNNEL;

                                case LinuxArpHardware.TUNNEL:
                                    type = NetworkInterfaceType.Tunnel;
                                    break;
                                }
                            }
                        }
                    }

                    LinuxNetworkInterface iface = null;

                    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 {
                freeifaddrs(ifap);
            }

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

            foreach (NetworkInterface thisInterface in interfaces.Values)
            {
                result [x] = thisInterface;
                x++;
            }
            return(result);
        }