상속: UnixNetworkInterface
예제 #1
0
        public static NetworkInterface [] GetAllNetworkInterfaces()
        {
#if MONOTOUCH
            return(MacOsNetworkInterface.ImplGetAllNetworkInterfaces());
#else
            if (runningOnUnix)
            {
                try {
                    if (Platform.IsMacOS)
                    {
                        return(MacOsNetworkInterface.ImplGetAllNetworkInterfaces());
                    }
                    else
                    {
                        return(LinuxNetworkInterface.ImplGetAllNetworkInterfaces());
                    }
                } catch (SystemException ex) {
                    throw ex;
                } catch {
                    return(new NetworkInterface [0]);
                }
            }
            else
            {
                if (Environment.OSVersion.Version >= windowsVer51)
                {
                    return(Win32NetworkInterface2.ImplGetAllNetworkInterfaces());
                }
                return(new NetworkInterface [0]);
            }
#endif
        }
예제 #2
0
        public static NetworkInterface[] GetAllNetworkInterfaces()
        {
            if (runningOnUnix)
            {
                bool   flag   = false;
                IntPtr intPtr = Marshal.AllocHGlobal(8192);
                if (uname(intPtr) == 0)
                {
                    string a = Marshal.PtrToStringAnsi(intPtr);
                    if (a == "Darwin")
                    {
                        flag = true;
                    }
                }
                Marshal.FreeHGlobal(intPtr);
                try
                {
                    if (flag)
                    {
                        return(MacOsNetworkInterface.ImplGetAllNetworkInterfaces());
                    }
                    return(LinuxNetworkInterface.ImplGetAllNetworkInterfaces());

                    IL_005f :;
                }
                catch (SystemException ex)
                {
                    throw ex;
                    IL_0067 :;
                }
                catch
                {
                    return(new NetworkInterface[0]);

                    IL_007a :;
                }
            }
            if (Environment.OSVersion.Version >= windowsVer51)
            {
                return(Win32NetworkInterface2.ImplGetAllNetworkInterfaces());
            }
            return(new NetworkInterface[0]);
        }
예제 #3
0
        public static NetworkInterface [] GetAllNetworkInterfaces()
        {
            if (runningOnUnix)
            {
                bool   darwin = false;
                IntPtr buf    = Marshal.AllocHGlobal(8192);
                if (uname(buf) == 0)
                {
                    string os = Marshal.PtrToStringAnsi(buf);
                    if (os == "Darwin")
                    {
                        darwin = true;
                    }
                }
                Marshal.FreeHGlobal(buf);

                try {
                    if (darwin)
                    {
                        return(MacOsNetworkInterface.ImplGetAllNetworkInterfaces());
                    }
                    else
                    {
                        return(LinuxNetworkInterface.ImplGetAllNetworkInterfaces());
                    }
                } catch (SystemException ex) {
                    throw ex;
                } catch {
                    return(new NetworkInterface [0]);
                }
            }
            else
            {
                if (Environment.OSVersion.Version >= windowsVer51)
                {
                    return(Win32NetworkInterface2.ImplGetAllNetworkInterfaces());
                }
                return(new NetworkInterface [0]);
            }
        }
예제 #4
0
		public MacOsIPv4InterfaceProperties (MacOsNetworkInterface iface)
			: base (iface)
		{
		}
예제 #5
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);
            }
예제 #6
0
		public MacOsIPInterfaceProperties (MacOsNetworkInterface iface, List <IPAddress> addresses)
			: base (iface, addresses)
		{
		}
예제 #7
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_nlen, 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;
		}
예제 #8
0
 public MacOsIPv4InterfaceProperties(MacOsNetworkInterface iface)
     : base(iface)
 {
 }
예제 #9
0
 public MacOsIPInterfaceProperties(MacOsNetworkInterface iface, List <IPAddress> addresses)
     : base(iface, addresses)
 {
 }
 public MacOsIPv4InterfaceStatistics(MacOsNetworkInterface parent)
 {
     this.macos = parent;
 }
예제 #11
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);
        }
예제 #12
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;
			}
        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);
        }
예제 #14
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);
        }