Ejemplo n.º 1
0
            public override IPAddress GetNetMask(IPAddress address)
            {
                IntPtr ifap;

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

                try {
                    IntPtr next = ifap;
                    while (next != IntPtr.Zero)
                    {
                        MacOsStructs.ifaddrs addr = (MacOsStructs.ifaddrs)Marshal.PtrToStructure(next, typeof(MacOsStructs.ifaddrs));

                        if (addr.ifa_addr != IntPtr.Zero)
                        {
                            // optain IPAddress
                            MacOsStructs.sockaddr sockaddr = (MacOsStructs.sockaddr)Marshal.PtrToStructure(addr.ifa_addr, typeof(MacOsStructs.sockaddr));

                            if (sockaddr.sa_family == AF_INET)
                            {
                                MacOsStructs.sockaddr_in sockaddrin = (MacOsStructs.sockaddr_in)Marshal.PtrToStructure(addr.ifa_addr, typeof(MacOsStructs.sockaddr_in));
                                var saddress = new IPAddress(sockaddrin.sin_addr);
                                if (address.Equals(saddress))
                                {
                                    return(new IPAddress(((sockaddr_in)Marshal.PtrToStructure(addr.ifa_netmask, typeof(sockaddr_in))).sin_addr));
                                }
                            }
                        }
                        next = addr.ifa_next;
                    }
                } finally {
                    freeifaddrs(ifap);
                }

                return(null);
            }
Ejemplo n.º 2
0
            public override NetworkInterface [] GetAllNetworkInterfaces()
            {
                var    interfaces = new Dictionary <string, MacOsNetworkInterface> ();
                IntPtr ifap;

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

                try {
                    IntPtr next = ifap;
                    while (next != IntPtr.Zero)
                    {
                        MacOsStructs.ifaddrs addr    = (MacOsStructs.ifaddrs)Marshal.PtrToStructure(next, typeof(MacOsStructs.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)
                        {
                            // optain IPAddress
                            MacOsStructs.sockaddr sockaddr = (MacOsStructs.sockaddr)Marshal.PtrToStructure(addr.ifa_addr, typeof(MacOsStructs.sockaddr));

                            if (sockaddr.sa_family == AF_INET6)
                            {
                                MacOsStructs.sockaddr_in6 sockaddr6 = (MacOsStructs.sockaddr_in6)Marshal.PtrToStructure(addr.ifa_addr, typeof(MacOsStructs.sockaddr_in6));
                                address = new IPAddress(sockaddr6.sin6_addr.u6_addr8, sockaddr6.sin6_scope_id);
                            }
                            else if (sockaddr.sa_family == AF_INET)
                            {
                                MacOsStructs.sockaddr_in sockaddrin = (MacOsStructs.sockaddr_in)Marshal.PtrToStructure(addr.ifa_addr, typeof(MacOsStructs.sockaddr_in));
                                address = new IPAddress(sockaddrin.sin_addr);
                            }
                            else if (sockaddr.sa_family == AF_LINK)
                            {
                                MacOsStructs.sockaddr_dl sockaddrdl = new MacOsStructs.sockaddr_dl();
                                sockaddrdl.Read(addr.ifa_addr);

                                macAddress = new byte [(int)sockaddrdl.sdl_alen];
                                // copy mac address from sdl_data field starting at last index pos of interface name into array macaddress, starting
                                // at index 0
                                Array.Copy(sockaddrdl.sdl_data, sockaddrdl.sdl_nlen, macAddress, 0, Math.Min(macAddress.Length, sockaddrdl.sdl_data.Length - sockaddrdl.sdl_nlen));

                                index = sockaddrdl.sdl_index;

                                int hwtype = (int)sockaddrdl.sdl_type;
                                if (Enum.IsDefined(typeof(MacOsArpHardware), hwtype))
                                {
                                    switch ((MacOsArpHardware)hwtype)
                                    {
                                    case MacOsArpHardware.ETHER:
                                        type = NetworkInterfaceType.Ethernet;
                                        break;

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

                                    case MacOsArpHardware.SLIP:
                                        type = NetworkInterfaceType.Slip;
                                        break;

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

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

                                    case MacOsArpHardware.FDDI:
                                        type = NetworkInterfaceType.Fddi;
                                        break;
                                    }
                                }
                            }
                        }

                        MacOsNetworkInterface iface = null;

                        // create interface if not already present
                        if (!interfaces.TryGetValue(name, out iface))
                        {
                            iface = new MacOsNetworkInterface(name, addr.ifa_flags);
                            interfaces.Add(name, iface);
                        }

                        // if a new address has been found, add it
                        if (!address.Equals(IPAddress.None))
                        {
                            iface.AddAddress(address);
                        }

                        // set link layer info, if iface has macaddress or is loopback device
                        if (macAddress != null || type == NetworkInterfaceType.Loopback)
                        {
                            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);
            }
Ejemplo n.º 3
0
        public static NetworkInterface [] ImplGetAllNetworkInterfaces()
        {
            var    interfaces = new Dictionary <string, MacOsNetworkInterface> ();
            IntPtr ifap;

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

            try {
                IntPtr next = ifap;
                while (next != IntPtr.Zero)
                {
                    MacOsStructs.ifaddrs addr    = (MacOsStructs.ifaddrs)Marshal.PtrToStructure(next, typeof(MacOsStructs.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)
                    {
                        MacOsStructs.sockaddr sockaddr = (MacOsStructs.sockaddr)Marshal.PtrToStructure(addr.ifa_addr, typeof(MacOsStructs.sockaddr));

                        if (sockaddr.sa_family == AF_INET6)
                        {
                            MacOsStructs.sockaddr_in6 sockaddr6 = (MacOsStructs.sockaddr_in6)Marshal.PtrToStructure(addr.ifa_addr, typeof(MacOsStructs.sockaddr_in6));
                            address = new IPAddress(sockaddr6.sin6_addr.u6_addr8, sockaddr6.sin6_scope_id);
                        }
                        else if (sockaddr.sa_family == AF_INET)
                        {
                            MacOsStructs.sockaddr_in sockaddrin = (MacOsStructs.sockaddr_in)Marshal.PtrToStructure(addr.ifa_addr, typeof(MacOsStructs.sockaddr_in));
                            address = new IPAddress(sockaddrin.sin_addr);
                        }
                        else if (sockaddr.sa_family == AF_LINK)
                        {
                            MacOsStructs.sockaddr_dl sockaddrdl = (MacOsStructs.sockaddr_dl)Marshal.PtrToStructure(addr.ifa_addr, typeof(MacOsStructs.sockaddr_dl));

                            macAddress = new byte [(int)sockaddrdl.sdl_alen];
                            Array.Copy(sockaddrdl.sdl_data, sockaddrdl.sdl_alen, macAddress, 0, macAddress.Length);
                            index = sockaddrdl.sdl_index;

                            int hwtype = (int)sockaddrdl.sdl_type;
                            if (Enum.IsDefined(typeof(MacOsArpHardware), hwtype))
                            {
                                switch ((MacOsArpHardware)hwtype)
                                {
                                case MacOsArpHardware.ETHER:
                                    type = NetworkInterfaceType.Ethernet;
                                    break;

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

                                case MacOsArpHardware.SLIP:
                                    type = NetworkInterfaceType.Slip;
                                    break;

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

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

                                case MacOsArpHardware.FDDI:
                                    type = NetworkInterfaceType.Fddi;
                                    break;
                                }
                            }
                        }
                    }

                    MacOsNetworkInterface iface = null;

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

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

                    if (macAddress != null || type == NetworkInterfaceType.Loopback)
                    {
                        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);
        }
Ejemplo n.º 4
0
        public static NetworkInterface[] ImplGetAllNetworkInterfaces()
        {
            Dictionary <string, MacOsNetworkInterface> dictionary = new Dictionary <string, MacOsNetworkInterface>();

            if (getifaddrs(out IntPtr ifap) != 0)
            {
                throw new SystemException("getifaddrs() failed");
            }
            try
            {
                IntPtr intPtr = ifap;
                while (intPtr != IntPtr.Zero)
                {
                    System.Net.NetworkInformation.MacOsStructs.ifaddrs ifaddrs = (System.Net.NetworkInformation.MacOsStructs.ifaddrs)Marshal.PtrToStructure(intPtr, typeof(System.Net.NetworkInformation.MacOsStructs.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)
                    {
                        System.Net.NetworkInformation.MacOsStructs.sockaddr sockaddr = (System.Net.NetworkInformation.MacOsStructs.sockaddr)Marshal.PtrToStructure(ifaddrs.ifa_addr, typeof(System.Net.NetworkInformation.MacOsStructs.sockaddr));
                        if (sockaddr.sa_family == 30)
                        {
                            System.Net.NetworkInformation.MacOsStructs.sockaddr_in6 sockaddr_in = (System.Net.NetworkInformation.MacOsStructs.sockaddr_in6)Marshal.PtrToStructure(ifaddrs.ifa_addr, typeof(System.Net.NetworkInformation.MacOsStructs.sockaddr_in6));
                            iPAddress = new IPAddress(sockaddr_in.sin6_addr.u6_addr8, sockaddr_in.sin6_scope_id);
                        }
                        else if (sockaddr.sa_family == 2)
                        {
                            System.Net.NetworkInformation.MacOsStructs.sockaddr_in sockaddr_in2 = (System.Net.NetworkInformation.MacOsStructs.sockaddr_in)Marshal.PtrToStructure(ifaddrs.ifa_addr, typeof(System.Net.NetworkInformation.MacOsStructs.sockaddr_in));
                            iPAddress = new IPAddress(sockaddr_in2.sin_addr);
                        }
                        else if (sockaddr.sa_family == 18)
                        {
                            System.Net.NetworkInformation.MacOsStructs.sockaddr_dl sockaddr_dl = (System.Net.NetworkInformation.MacOsStructs.sockaddr_dl)Marshal.PtrToStructure(ifaddrs.ifa_addr, typeof(System.Net.NetworkInformation.MacOsStructs.sockaddr_dl));
                            array = new byte[sockaddr_dl.sdl_alen];
                            Array.Copy(sockaddr_dl.sdl_data, sockaddr_dl.sdl_nlen, array, 0, Math.Min(array.Length, sockaddr_dl.sdl_data.Length - sockaddr_dl.sdl_nlen));
                            index = sockaddr_dl.sdl_index;
                            int sdl_type = sockaddr_dl.sdl_type;
                            if (Enum.IsDefined(typeof(MacOsArpHardware), sdl_type))
                            {
                                switch (sdl_type)
                                {
                                case 6:
                                    networkInterfaceType = NetworkInterfaceType.Ethernet;
                                    break;

                                case 37:
                                    networkInterfaceType = NetworkInterfaceType.Atm;
                                    break;

                                case 28:
                                    networkInterfaceType = NetworkInterfaceType.Slip;
                                    break;

                                case 23:
                                    networkInterfaceType = NetworkInterfaceType.Ppp;
                                    break;

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

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

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