Implements a NetworkInterface on Linux.
Inheritance: UnixNetworkInterface
 public LinuxIPv4InterfaceProperties(LinuxNetworkInterface linuxNetworkInterface)
     : base(linuxNetworkInterface)
 {
     _linuxNetworkInterface = linuxNetworkInterface;
     _isForwardingEnabled = GetIsForwardingEnabled();
     _mtu = GetMtu();
 }
Beispiel #2
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
        }
        public unsafe static NetworkInterface[] GetLinuxNetworkInterfaces()
        {
            Dictionary <string, LinuxNetworkInterface> interfacesByName = new Dictionary <string, LinuxNetworkInterface>();
            const int MaxTries = 3;

            for (int attempt = 0; attempt < MaxTries; attempt++)
            {
                int result = Interop.Sys.EnumerateInterfaceAddresses(
                    (name, ipAddr, maskAddr) =>
                {
                    LinuxNetworkInterface lni = GetOrCreate(interfacesByName, name);
                    lni.ProcessIpv4Address(ipAddr, maskAddr);
                },
                    (name, ipAddr, scopeId) =>
                {
                    LinuxNetworkInterface lni = GetOrCreate(interfacesByName, name);
                    lni.ProcessIpv6Address(ipAddr, *scopeId);
                },
                    (name, llAddr) =>
                {
                    LinuxNetworkInterface lni = GetOrCreate(interfacesByName, name);
                    lni.ProcessLinkLayerAddress(llAddr);
                });
                if (result == 0)
                {
                    return(interfacesByName.Values.ToArray());
                }
                else
                {
                    interfacesByName.Clear();
                }
            }

            throw new NetworkInformationException(SR.net_PInvokeError);
        }
 public LinuxIPv4InterfaceProperties(LinuxNetworkInterface linuxNetworkInterface)
     : base(linuxNetworkInterface)
 {
     _linuxNetworkInterface = linuxNetworkInterface;
     _isForwardingEnabled   = GetIsForwardingEnabled();
     _mtu = GetMtu();
 }
 public LinuxIPInterfaceProperties(LinuxNetworkInterface lni)
     : base(lni)
 {
     _linuxNetworkInterface = lni;
     _gatewayAddresses = GetGatewayAddresses();
     _dhcpServerAddresses = GetDhcpServerAddresses();
     _winsServerAddresses = GetWinsServerAddresses();
     _ipv4Properties = new LinuxIPv4InterfaceProperties(lni);
     _ipv6Properties = new LinuxIPv6InterfaceProperties(lni);
 }
 public LinuxIPInterfaceProperties(LinuxNetworkInterface lni)
     : base(lni)
 {
     _linuxNetworkInterface = lni;
     _gatewayAddresses      = GetGatewayAddresses();
     _dhcpServerAddresses   = GetDhcpServerAddresses();
     _winsServerAddresses   = GetWinsServerAddresses();
     _ipv4Properties        = new LinuxIPv4InterfaceProperties(lni);
     _ipv6Properties        = new LinuxIPv6InterfaceProperties(lni);
 }
        /// <summary>
        /// Gets or creates a LinuxNetworkInterface, based on whether it already exists in the given Dictionary.
        /// If created, it is added to the Dictionary.
        /// </summary>
        /// <param name="interfaces">The Dictionary of existing interfaces.</param>
        /// <param name="name">The name of the interface.</param>
        /// <returns>The cached or new LinuxNetworkInterface with the given name.</returns>
        private static LinuxNetworkInterface GetOrCreate(Dictionary<string, LinuxNetworkInterface> interfaces, string name)
        {
            LinuxNetworkInterface lni;
            if (!interfaces.TryGetValue(name, out lni))
            {
                lni = new LinuxNetworkInterface(name);
                interfaces.Add(name, lni);
            }

            return lni;
        }
        private static int GetNumIPAddresses()
        {
            // PERF: Use EnumerateInterfaceAddresses directly.
            int count = 0;

            foreach (LinuxNetworkInterface lni in LinuxNetworkInterface.GetLinuxNetworkInterfaces())
            {
                count += lni.Addresses.Count;
            }
            return(count);
        }
        /// <summary>
        /// Gets or creates a LinuxNetworkInterface, based on whether it already exists in the given Dictionary.
        /// If created, it is added to the Dictionary.
        /// </summary>
        /// <param name="interfaces">The Dictionary of existing interfaces.</param>
        /// <param name="name">The name of the interface.</param>
        /// <returns>The cached or new LinuxNetworkInterface with the given name.</returns>
        private static LinuxNetworkInterface GetOrCreate(Dictionary <string, LinuxNetworkInterface> interfaces, string name)
        {
            LinuxNetworkInterface lni;

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

            return(lni);
        }
 public LinuxIPInterfaceProperties(LinuxNetworkInterface lni, LinuxNetworkInterface.LinuxNetworkInterfaceSystemProperties systemProperties)
     : base(lni, globalConfig: true)
 {
     _linuxNetworkInterface = lni;
     _gatewayAddresses      = GetGatewayAddresses(systemProperties);
     _dhcpServerAddresses   = GetDhcpServerAddresses();
     _winsServerAddresses   = GetWinsServerAddresses();
     _dnsSuffix             = systemProperties.DnsSuffix;
     _dnsAddresses          = systemProperties.DnsAddresses;
     _ipv4Properties        = new LinuxIPv4InterfaceProperties(lni);
     _ipv6Properties        = new LinuxIPv6InterfaceProperties(lni);
 }
        private UnicastIPAddressInformationCollection GetUnicastAddresses()
        {
            UnicastIPAddressInformationCollection collection = new UnicastIPAddressInformationCollection();

            foreach (UnicastIPAddressInformation info in
                     LinuxNetworkInterface.GetLinuxNetworkInterfaces().SelectMany(lni => lni.GetIPProperties().UnicastAddresses))
            {
                // PERF: Use Interop.Sys.EnumerateInterfaceAddresses directly here.
                collection.InternalAdd(info);
            }

            return(collection);
        }
Beispiel #12
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]);
        }
Beispiel #13
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]);
            }
        }
        public unsafe static NetworkInterface[] GetLinuxNetworkInterfaces()
        {
            Dictionary <string, LinuxNetworkInterface> interfacesByName = new Dictionary <string, LinuxNetworkInterface>();

            Interop.Sys.EnumerateInterfaceAddresses(
                (name, ipAddr, maskAddr) =>
            {
                LinuxNetworkInterface lni = GetOrCreate(interfacesByName, name);
                lni.ProcessIpv4Address(ipAddr, maskAddr);
            },
                (name, ipAddr, scopeId) =>
            {
                LinuxNetworkInterface lni = GetOrCreate(interfacesByName, name);
                lni.ProcessIpv6Address(ipAddr, *scopeId);
            },
                (name, llAddr) =>
            {
                LinuxNetworkInterface lni = GetOrCreate(interfacesByName, name);
                lni.ProcessLinkLayerAddress(llAddr);
            });

            return(interfacesByName.Values.ToArray());
        }
Beispiel #15
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);
            }
Beispiel #16
0
 static LinuxNetworkInterface()
 {
     LinuxNetworkInterface.InitializeInterfaceAddresses();
 }
		public LinuxIPInterfaceProperties (LinuxNetworkInterface iface, List <IPAddress> addresses)
			: base (iface, addresses)
		{
		}
Beispiel #18
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);
        }
Beispiel #19
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;
		}
 public LinuxIPInterfaceProperties(LinuxNetworkInterface iface, List <IPAddress> addresses)
     : base(iface, addresses)
 {
 }
        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);
        }
Beispiel #22
0
 public LinuxIPv4InterfaceProperties(LinuxNetworkInterface iface)
     : base(iface)
 {
 }
        public static unsafe NetworkInterface[] GetLinuxNetworkInterfaces()
        {
            var systemProperties = new LinuxNetworkInterfaceSystemProperties();

            int interfaceCount = 0;
            int addressCount   = 0;

            Interop.Sys.NetworkInterfaceInfo *nii = null;
            Interop.Sys.IpAddressInfo *       ai  = null;
            IntPtr globalMemory = (IntPtr)null;

            if (Interop.Sys.GetNetworkInterfaces(ref interfaceCount, ref nii, ref addressCount, ref ai) != 0)
            {
                string message = Interop.Sys.GetLastErrorInfo().GetErrorMessage();
                throw new NetworkInformationException(message);
            }

            globalMemory = (IntPtr)nii;
            try
            {
                NetworkInterface[] interfaces = new NetworkInterface[interfaceCount];
                Dictionary <int, LinuxNetworkInterface> interfacesByIndex = new Dictionary <int, LinuxNetworkInterface>(interfaceCount);

                for (int i = 0; i < interfaceCount; i++)
                {
                    var lni = new LinuxNetworkInterface(Marshal.PtrToStringAnsi((IntPtr)nii->Name), nii->InterfaceIndex, systemProperties);
                    lni._interfaceType     = (NetworkInterfaceType)nii->HardwareType;
                    lni._speed             = nii->Speed;
                    lni._operationalStatus = (OperationalStatus)nii->OperationalState;
                    lni._mtu = nii->Mtu;
                    lni._supportsMulticast = nii->SupportsMulticast != 0;

                    interfaces[i] = lni;
                    interfacesByIndex.Add(nii->InterfaceIndex, lni);
                    nii++;
                }

                while (addressCount != 0)
                {
                    var address = new IPAddress(new ReadOnlySpan <byte>(ai->AddressBytes, ai->NumAddressBytes));
                    if (address.IsIPv6LinkLocal)
                    {
                        address.ScopeId = ai->InterfaceIndex;
                    }

                    if (interfacesByIndex.TryGetValue(ai->InterfaceIndex, out LinuxNetworkInterface lni))
                    {
                        lni.AddAddress(address, ai->PrefixLength);
                    }

                    ai++;
                    addressCount--;
                }

                return(interfaces);
            }
            finally
            {
                Marshal.FreeHGlobal(globalMemory);
            }
        }
Beispiel #24
0
 public LinuxIPv6InterfaceProperties(LinuxNetworkInterface linuxNetworkInterface)
     : base(linuxNetworkInterface)
 {
     _linuxNetworkInterface = linuxNetworkInterface;
 }
Beispiel #25
0
 public LinuxIPv4InterfaceStatistics(LinuxNetworkInterface parent)
 {
     this.linux = parent;
 }
        public static unsafe NetworkInterface[] GetLinuxNetworkInterfaces()
        {
            Dictionary <string, LinuxNetworkInterface> interfacesByName = new Dictionary <string, LinuxNetworkInterface>();
            List <Exception> exceptions = null;
            const int        MaxTries   = 3;

            for (int attempt = 0; attempt < MaxTries; attempt++)
            {
                // Because these callbacks are executed in a reverse-PInvoke, we do not want any exceptions
                // to propogate out, because they will not be catchable. Instead, we track all the exceptions
                // that are thrown in these callbacks, and aggregate them at the end.
                int result = Interop.Sys.EnumerateInterfaceAddresses(
                    (name, ipAddr, maskAddr) =>
                {
                    try
                    {
                        LinuxNetworkInterface lni = GetOrCreate(interfacesByName, name);
                        lni.ProcessIpv4Address(ipAddr, maskAddr);
                    }
                    catch (Exception e)
                    {
                        if (exceptions == null)
                        {
                            exceptions = new List <Exception>();
                        }
                        exceptions.Add(e);
                    }
                },
                    (name, ipAddr, scopeId) =>
                {
                    try
                    {
                        LinuxNetworkInterface lni = GetOrCreate(interfacesByName, name);
                        lni.ProcessIpv6Address(ipAddr, *scopeId);
                    }
                    catch (Exception e)
                    {
                        if (exceptions == null)
                        {
                            exceptions = new List <Exception>();
                        }
                        exceptions.Add(e);
                    }
                },
                    (name, llAddr) =>
                {
                    try
                    {
                        LinuxNetworkInterface lni = GetOrCreate(interfacesByName, name);
                        lni.ProcessLinkLayerAddress(llAddr);
                    }
                    catch (Exception e)
                    {
                        if (exceptions == null)
                        {
                            exceptions = new List <Exception>();
                        }
                        exceptions.Add(e);
                    }
                });
                if (exceptions != null)
                {
                    throw new NetworkInformationException(SR.net_PInvokeError, new AggregateException(exceptions));
                }
                else if (result == 0)
                {
                    return(interfacesByName.Values.ToArray());
                }
                else
                {
                    interfacesByName.Clear();
                }
            }

            throw new NetworkInformationException(SR.net_PInvokeError);
        }
		public LinuxIPv4InterfaceProperties (LinuxNetworkInterface iface)
			: base (iface)
		{
		}
 public LinuxIPv6InterfaceProperties(LinuxNetworkInterface linuxNetworkInterface)
     : base(linuxNetworkInterface)
 {
     _linuxNetworkInterface = linuxNetworkInterface;
     _mtu = GetMtu();
 }