예제 #1
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) {
						// 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;
		}
예제 #2
0
        public static NetworkInterface [] ImplGetAllNetworkInterfaces()
        {
            var    interfaces = new Dictionary <string, NetworkInterface> ();
            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, 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;
                                }
                            }
                        }
                    }

                    NetworkInterface iface = null;

                    if (!interfaces.TryGetValue(name, out iface))
                    {
                        iface = new NetworkInterface(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);
        }