2 // System.Net.NetworkInformation.NetworkInterface
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
6 // Atsushi Enomoto (atsushi@ximian.com)
7 // Miguel de Icaza (miguel@novell.com)
8 // Eric Butler (eric@extremeboredom.net)
9 // Marek Habersack (mhabersack@novell.com)
10 // Marek Safar (marek.safar@gmail.com)
12 // Copyright (c) 2006-2008 Novell, Inc. (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections.Generic;
35 using System.Collections;
37 using System.Net.Sockets;
38 using System.Runtime.InteropServices;
41 using System.Globalization;
43 namespace System.Net.NetworkInformation {
44 static class SystemNetworkInterface {
46 static readonly NetworkInterfaceFactory nif = NetworkInterfaceFactory.Create ();
48 public static NetworkInterface [] GetNetworkInterfaces ()
51 return nif.GetAllNetworkInterfaces ();
53 return new NetworkInterface [0];
57 public static bool InternalGetIsNetworkAvailable ()
63 public static int InternalLoopbackInterfaceIndex {
65 return nif.GetLoopbackInterfaceIndex ();
69 public static int InternalIPv6LoopbackInterfaceIndex {
71 throw new NotImplementedException ();
75 public static IPAddress GetNetMask (IPAddress address)
77 return nif.GetNetMask (address);
81 abstract class NetworkInterfaceFactory
83 internal abstract class UnixNetworkInterfaceAPI : NetworkInterfaceFactory
86 public static int if_nametoindex(string ifname)
88 throw new PlatformNotSupportedException ();
91 protected static int getifaddrs (out IntPtr ifap)
93 throw new PlatformNotSupportedException ();
96 protected static void freeifaddrs (IntPtr ifap)
98 throw new PlatformNotSupportedException ();
102 public static extern int if_nametoindex(string ifname);
105 protected static extern int getifaddrs (out IntPtr ifap);
108 protected static extern void freeifaddrs (IntPtr ifap);
112 class MacOsNetworkInterfaceAPI : UnixNetworkInterfaceAPI
114 const int AF_INET = 2;
115 const int AF_INET6 = 30;
116 const int AF_LINK = 18;
118 public override NetworkInterface [] GetAllNetworkInterfaces ()
120 var interfaces = new Dictionary <string, MacOsNetworkInterface> ();
122 if (getifaddrs (out ifap) != 0)
123 throw new SystemException ("getifaddrs() failed");
127 while (next != IntPtr.Zero) {
128 MacOsStructs.ifaddrs addr = (MacOsStructs.ifaddrs) Marshal.PtrToStructure (next, typeof (MacOsStructs.ifaddrs));
129 IPAddress address = IPAddress.None;
130 string name = addr.ifa_name;
132 byte[] macAddress = null;
133 NetworkInterfaceType type = NetworkInterfaceType.Unknown;
135 if (addr.ifa_addr != IntPtr.Zero) {
137 MacOsStructs.sockaddr sockaddr = (MacOsStructs.sockaddr) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr));
139 if (sockaddr.sa_family == AF_INET6) {
140 MacOsStructs.sockaddr_in6 sockaddr6 = (MacOsStructs.sockaddr_in6) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr_in6));
141 address = new IPAddress (sockaddr6.sin6_addr.u6_addr8, sockaddr6.sin6_scope_id);
142 } else if (sockaddr.sa_family == AF_INET) {
143 MacOsStructs.sockaddr_in sockaddrin = (MacOsStructs.sockaddr_in) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr_in));
144 address = new IPAddress (sockaddrin.sin_addr);
145 } else if (sockaddr.sa_family == AF_LINK) {
146 MacOsStructs.sockaddr_dl sockaddrdl = new MacOsStructs.sockaddr_dl ();
147 sockaddrdl.Read (addr.ifa_addr);
149 macAddress = new byte [(int) sockaddrdl.sdl_alen];
150 // copy mac address from sdl_data field starting at last index pos of interface name into array macaddress, starting
152 Array.Copy (sockaddrdl.sdl_data, sockaddrdl.sdl_nlen, macAddress, 0, Math.Min (macAddress.Length, sockaddrdl.sdl_data.Length - sockaddrdl.sdl_nlen));
154 index = sockaddrdl.sdl_index;
156 int hwtype = (int) sockaddrdl.sdl_type;
157 if (Enum.IsDefined (typeof (MacOsArpHardware), hwtype)) {
158 switch ((MacOsArpHardware) hwtype) {
159 case MacOsArpHardware.ETHER:
160 type = NetworkInterfaceType.Ethernet;
163 case MacOsArpHardware.ATM:
164 type = NetworkInterfaceType.Atm;
167 case MacOsArpHardware.SLIP:
168 type = NetworkInterfaceType.Slip;
171 case MacOsArpHardware.PPP:
172 type = NetworkInterfaceType.Ppp;
175 case MacOsArpHardware.LOOPBACK:
176 type = NetworkInterfaceType.Loopback;
180 case MacOsArpHardware.FDDI:
181 type = NetworkInterfaceType.Fddi;
188 MacOsNetworkInterface iface = null;
190 // create interface if not already present
191 if (!interfaces.TryGetValue (name, out iface)) {
192 iface = new MacOsNetworkInterface (name, addr.ifa_flags);
193 interfaces.Add (name, iface);
196 // if a new address has been found, add it
197 if (!address.Equals (IPAddress.None))
198 iface.AddAddress (address);
200 // set link layer info, if iface has macaddress or is loopback device
201 if (macAddress != null || type == NetworkInterfaceType.Loopback)
202 iface.SetLinkLayerInfo (index, macAddress, type);
204 next = addr.ifa_next;
210 NetworkInterface [] result = new NetworkInterface [interfaces.Count];
212 foreach (NetworkInterface thisInterface in interfaces.Values) {
213 result [x] = thisInterface;
219 public override int GetLoopbackInterfaceIndex ()
221 return if_nametoindex ("lo0");
224 public override IPAddress GetNetMask (IPAddress address)
227 if (getifaddrs (out ifap) != 0)
228 throw new SystemException ("getifaddrs() failed");
232 while (next != IntPtr.Zero) {
233 MacOsStructs.ifaddrs addr = (MacOsStructs.ifaddrs) Marshal.PtrToStructure (next, typeof (MacOsStructs.ifaddrs));
235 if (addr.ifa_addr != IntPtr.Zero) {
237 MacOsStructs.sockaddr sockaddr = (MacOsStructs.sockaddr) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr));
239 if (sockaddr.sa_family == AF_INET) {
240 MacOsStructs.sockaddr_in sockaddrin = (MacOsStructs.sockaddr_in) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr_in));
241 var saddress = new IPAddress (sockaddrin.sin_addr);
242 if (address.Equals (saddress))
243 return new IPAddress(((sockaddr_in)Marshal.PtrToStructure(addr.ifa_netmask, typeof(sockaddr_in))).sin_addr);
246 next = addr.ifa_next;
256 class LinuxNetworkInterfaceAPI : UnixNetworkInterfaceAPI
258 const int AF_INET = 2;
259 const int AF_INET6 = 10;
260 const int AF_PACKET = 17;
262 static void FreeInterfaceAddresses (IntPtr ifap)
265 AndroidPlatform.FreeInterfaceAddresses (ifap);
271 static int GetInterfaceAddresses (out IntPtr ifap)
274 return AndroidPlatform.GetInterfaceAddresses (out ifap);
276 return getifaddrs (out ifap);
280 public override NetworkInterface [] GetAllNetworkInterfaces ()
283 var interfaces = new Dictionary <string, LinuxNetworkInterface> ();
285 if (GetInterfaceAddresses (out ifap) != 0)
286 throw new SystemException ("getifaddrs() failed");
290 while (next != IntPtr.Zero) {
291 ifaddrs addr = (ifaddrs) Marshal.PtrToStructure (next, typeof (ifaddrs));
292 IPAddress address = IPAddress.None;
293 string name = addr.ifa_name;
295 byte[] macAddress = null;
296 NetworkInterfaceType type = NetworkInterfaceType.Unknown;
297 int nullNameCount = 0;
299 if (addr.ifa_addr != IntPtr.Zero) {
300 sockaddr_in sockaddr = (sockaddr_in) Marshal.PtrToStructure (addr.ifa_addr, typeof (sockaddr_in));
302 if (sockaddr.sin_family == AF_INET6) {
303 sockaddr_in6 sockaddr6 = (sockaddr_in6) Marshal.PtrToStructure (addr.ifa_addr, typeof (sockaddr_in6));
304 address = new IPAddress (sockaddr6.sin6_addr.u6_addr8, sockaddr6.sin6_scope_id);
305 } else if (sockaddr.sin_family == AF_INET) {
306 address = new IPAddress (sockaddr.sin_addr);
307 } else if (sockaddr.sin_family == AF_PACKET) {
308 sockaddr_ll sockaddrll = (sockaddr_ll) Marshal.PtrToStructure (addr.ifa_addr, typeof (sockaddr_ll));
309 if (((int)sockaddrll.sll_halen) > sockaddrll.sll_addr.Length){
310 Console.Error.WriteLine ("Got a bad hardware address length for an AF_PACKET {0} {1}",
311 sockaddrll.sll_halen, sockaddrll.sll_addr.Length);
312 next = addr.ifa_next;
316 macAddress = new byte [(int) sockaddrll.sll_halen];
317 Array.Copy (sockaddrll.sll_addr, 0, macAddress, 0, macAddress.Length);
318 index = sockaddrll.sll_ifindex;
320 int hwtype = (int)sockaddrll.sll_hatype;
321 if (Enum.IsDefined (typeof (LinuxArpHardware), hwtype)) {
322 switch ((LinuxArpHardware)hwtype) {
323 case LinuxArpHardware.EETHER:
324 goto case LinuxArpHardware.ETHER;
326 case LinuxArpHardware.ETHER:
327 type = NetworkInterfaceType.Ethernet;
330 case LinuxArpHardware.PRONET:
331 type = NetworkInterfaceType.TokenRing;
334 case LinuxArpHardware.ATM:
335 type = NetworkInterfaceType.Atm;
338 case LinuxArpHardware.SLIP:
339 case LinuxArpHardware.CSLIP:
340 case LinuxArpHardware.SLIP6:
341 case LinuxArpHardware.CSLIP6:
342 type = NetworkInterfaceType.Slip;
345 case LinuxArpHardware.PPP:
346 type = NetworkInterfaceType.Ppp;
349 case LinuxArpHardware.LOOPBACK:
350 type = NetworkInterfaceType.Loopback;
354 case LinuxArpHardware.FDDI:
355 type = NetworkInterfaceType.Fddi;
358 case LinuxArpHardware.SIT:
359 case LinuxArpHardware.IPDDP:
360 case LinuxArpHardware.IPGRE:
361 case LinuxArpHardware.IP6GRE:
362 case LinuxArpHardware.TUNNEL6:
363 case LinuxArpHardware.TUNNEL:
364 type = NetworkInterfaceType.Tunnel;
371 LinuxNetworkInterface iface = null;
373 if (String.IsNullOrEmpty (name))
374 name = "\0" + (++nullNameCount).ToString ();
376 if (!interfaces.TryGetValue (name, out iface)) {
377 iface = new LinuxNetworkInterface (name);
378 interfaces.Add (name, iface);
381 if (!address.Equals (IPAddress.None))
382 iface.AddAddress (address);
384 if (macAddress != null || type == NetworkInterfaceType.Loopback) {
385 if (type == NetworkInterfaceType.Ethernet) {
386 if (Directory.Exists(iface.IfacePath + "wireless")) {
387 type = NetworkInterfaceType.Wireless80211;
390 iface.SetLinkLayerInfo (index, macAddress, type);
393 next = addr.ifa_next;
396 FreeInterfaceAddresses (ifap);
399 NetworkInterface [] result = new NetworkInterface [interfaces.Count];
401 foreach (NetworkInterface thisInterface in interfaces.Values) {
402 result [x] = thisInterface;
408 public override int GetLoopbackInterfaceIndex ()
410 return if_nametoindex ("lo");
413 public override IPAddress GetNetMask (IPAddress address)
415 foreach (ifaddrs networkInteface in GetNetworkInterfaces()) {
416 if (networkInteface.ifa_addr == IntPtr.Zero)
419 var sockaddr = (sockaddr_in)Marshal.PtrToStructure(networkInteface.ifa_addr, typeof(sockaddr_in));
421 if (sockaddr.sin_family != AF_INET)
424 if (!address.Equals(new IPAddress(sockaddr.sin_addr)))
427 var netmask = (sockaddr_in)Marshal.PtrToStructure(networkInteface.ifa_netmask, typeof(sockaddr_in));
428 return new IPAddress(netmask.sin_addr);
434 private static IEnumerable<ifaddrs> GetNetworkInterfaces()
436 IntPtr ifap = IntPtr.Zero;
439 if (GetInterfaceAddresses(out ifap) != 0)
443 while (next != IntPtr.Zero) {
444 var addr = (ifaddrs)Marshal.PtrToStructure(next, typeof(ifaddrs));
446 next = addr.ifa_next;
449 if (ifap != IntPtr.Zero)
450 FreeInterfaceAddresses(ifap);
456 class Win32NetworkInterfaceAPI : NetworkInterfaceFactory
458 private const string IPHLPAPI = "iphlpapi.dll";
460 [DllImport (IPHLPAPI, SetLastError = true)]
461 static extern int GetAdaptersAddresses (uint family, uint flags, IntPtr reserved, IntPtr info, ref int size);
463 [DllImport (IPHLPAPI)]
464 static extern uint GetBestInterfaceEx (byte[] ipAddress, out int index);
466 static Win32_IP_ADAPTER_ADDRESSES [] GetAdaptersAddresses ()
468 IntPtr ptr = IntPtr.Zero;
470 GetAdaptersAddresses (0, 0, IntPtr.Zero, ptr, ref len);
471 ptr = Marshal.AllocHGlobal(len);
472 int ret = GetAdaptersAddresses (0, 0, IntPtr.Zero, ptr, ref len);
474 throw new NetworkInformationException (ret);
476 List<Win32_IP_ADAPTER_ADDRESSES> l = new List<Win32_IP_ADAPTER_ADDRESSES> ();
477 Win32_IP_ADAPTER_ADDRESSES info;
478 for (IntPtr p = ptr; p != IntPtr.Zero; p = info.Next) {
479 info = Marshal.PtrToStructure<Win32_IP_ADAPTER_ADDRESSES> (p);
486 public override NetworkInterface [] GetAllNetworkInterfaces ()
488 // Win32_IP_ADAPTER_INFO [] ai = GetAdaptersInfo ();
489 Win32_IP_ADAPTER_ADDRESSES [] aa = GetAdaptersAddresses ();
490 NetworkInterface [] ret = new NetworkInterface [aa.Length];
491 for (int i = 0; i < ret.Length; i++)
492 ret [i] = new Win32NetworkInterface2 (aa [i]);
496 private static int GetBestInterfaceForAddress (IPAddress addr) {
498 SocketAddress address = new SocketAddress (addr);
499 int error = (int) GetBestInterfaceEx (address.m_Buffer, out index);
501 throw new NetworkInformationException (error);
507 public override int GetLoopbackInterfaceIndex ()
509 return GetBestInterfaceForAddress (IPAddress.Loopback);
512 public override IPAddress GetNetMask (IPAddress address)
514 throw new NotImplementedException ();
519 public abstract NetworkInterface [] GetAllNetworkInterfaces ();
520 public abstract int GetLoopbackInterfaceIndex ();
521 public abstract IPAddress GetNetMask (IPAddress address);
523 public static NetworkInterfaceFactory Create ()
525 #if MONOTOUCH || XAMMAC
526 return new MacOsNetworkInterfaceAPI ();
528 bool runningOnUnix = (Environment.OSVersion.Platform == PlatformID.Unix);
531 if (Platform.IsMacOS || Platform.IsFreeBSD)
532 return new MacOsNetworkInterfaceAPI ();
534 return new LinuxNetworkInterfaceAPI ();
538 Version windowsVer51 = new Version (5, 1);
539 if (Environment.OSVersion.Version >= windowsVer51)
540 return new Win32NetworkInterfaceAPI ();
543 throw new NotImplementedException ();
548 abstract class UnixNetworkInterface : NetworkInterface
551 protected IPv4InterfaceStatistics ipv4stats;
552 protected IPInterfaceProperties ipproperties;
556 protected List <IPAddress> addresses;
558 NetworkInterfaceType type;
560 internal UnixNetworkInterface (string name)
563 addresses = new List<IPAddress> ();
566 internal void AddAddress (IPAddress address)
568 addresses.Add (address);
571 internal void SetLinkLayerInfo (int index, byte[] macAddress, NetworkInterfaceType type)
573 //this.index = index;
574 this.macAddress = macAddress;
578 public override PhysicalAddress GetPhysicalAddress ()
580 if (macAddress != null)
581 return new PhysicalAddress (macAddress);
583 return PhysicalAddress.None;
586 public override bool Supports (NetworkInterfaceComponent networkInterfaceComponent)
588 bool wantIPv4 = networkInterfaceComponent == NetworkInterfaceComponent.IPv4;
589 bool wantIPv6 = wantIPv4 ? false : networkInterfaceComponent == NetworkInterfaceComponent.IPv6;
591 foreach (IPAddress address in addresses) {
592 if (wantIPv4 && address.AddressFamily == AddressFamily.InterNetwork)
594 else if (wantIPv6 && address.AddressFamily == AddressFamily.InterNetworkV6)
601 public override string Description {
605 public override string Id {
609 public override bool IsReceiveOnly {
610 get { return false; }
613 public override string Name {
617 public override NetworkInterfaceType NetworkInterfaceType {
621 [MonoTODO ("Parse dmesg?")]
622 public override long Speed {
629 internal int NameIndex {
631 return NetworkInterfaceFactory.UnixNetworkInterfaceAPI.if_nametoindex (Name);
637 // This class needs support from the libsupport.so library to fetch the
638 // data using arch-specific ioctls.
640 // For this to work, we have to create this on the factory above.
642 sealed class LinuxNetworkInterface : UnixNetworkInterface
644 //NetworkInterfaceType type;
646 string iface_operstate_path;
647 string iface_flags_path;
650 [DllImport ("__Internal")]
651 static extern int _monodroid_get_android_api_level ();
653 [DllImport ("__Internal")]
654 static extern bool _monodroid_get_network_interface_up_state (string ifname, ref bool is_up);
656 [DllImport ("__Internal")]
657 static extern bool _monodroid_get_network_interface_supports_multicast (string ifname, ref bool supports_multicast);
659 bool android_use_java_api;
662 internal string IfacePath {
663 get { return iface_path; }
666 internal LinuxNetworkInterface (string name)
669 iface_path = "/sys/class/net/" + name + "/";
670 iface_operstate_path = iface_path + "operstate";
671 iface_flags_path = iface_path + "flags";
673 android_use_java_api = _monodroid_get_android_api_level () >= 24;
677 public override IPInterfaceProperties GetIPProperties ()
679 if (ipproperties == null)
680 ipproperties = new LinuxIPInterfaceProperties (this, addresses);
684 public override IPv4InterfaceStatistics GetIPv4Statistics ()
686 if (ipv4stats == null)
687 ipv4stats = new LinuxIPv4InterfaceStatistics (this);
691 public override OperationalStatus OperationalStatus {
694 if (android_use_java_api) {
695 // Starting from API 24 (Android 7 "Nougat") Android restricts access to many
696 // files in the /sys filesystem (see https://code.google.com/p/android/issues/detail?id=205565
697 // for more information) and therefore we are forced to call into Java API in
698 // order to get the information. Alas, what we can obtain in this way is quite
699 // limited. In the case of OperationalStatus we can only determine whether the
700 // interface is up or down. There is a way to get more detailed information but
701 // it requires an instance of the Android Context class which is not available
704 if (_monodroid_get_network_interface_up_state (Name, ref is_up))
705 return is_up ? OperationalStatus.Up : OperationalStatus.Down;
707 return OperationalStatus.Unknown;
710 if (!Directory.Exists (iface_path))
711 return OperationalStatus.Unknown;
714 string s = ReadLine (iface_operstate_path);
718 return OperationalStatus.Unknown;
721 return OperationalStatus.NotPresent;
724 return OperationalStatus.Down;
726 case "lowerlayerdown":
727 return OperationalStatus.LowerLayerDown;
730 return OperationalStatus.Testing;
733 return OperationalStatus.Dormant;
736 return OperationalStatus.Up;
740 return OperationalStatus.Unknown;
744 public override bool SupportsMulticast {
747 if (android_use_java_api) {
748 // Starting from API 24 (Android 7 "Nougat") Android restricts access to many
749 // files in the /sys filesystem (see https://code.google.com/p/android/issues/detail?id=205565
750 // for more information) and therefore we are forced to call into Java API in
751 // order to get the information.
752 bool supports_multicast = false;
753 _monodroid_get_network_interface_supports_multicast (Name, ref supports_multicast);
754 return supports_multicast;
757 if (!Directory.Exists (iface_path))
761 string s = ReadLine (iface_flags_path);
762 if (s.Length > 2 && s [0] == '0' && s [1] == 'x')
765 ulong f = UInt64.Parse (s, NumberStyles.HexNumber);
767 // Hardcoded, only useful for Linux.
768 return ((f & 0x1000) == 0x1000);
775 internal static string ReadLine (string path)
777 using (FileStream fs = File.OpenRead (path)){
778 using (StreamReader sr = new StreamReader (fs)){
779 return sr.ReadLine ();
785 sealed class MacOsNetworkInterface : UnixNetworkInterface
787 private uint _ifa_flags;
789 internal MacOsNetworkInterface (string name, uint ifa_flags)
792 _ifa_flags = ifa_flags;
795 public override IPInterfaceProperties GetIPProperties ()
797 if (ipproperties == null)
798 ipproperties = new MacOsIPInterfaceProperties (this, addresses);
802 public override IPv4InterfaceStatistics GetIPv4Statistics ()
804 if (ipv4stats == null)
805 ipv4stats = new MacOsIPv4InterfaceStatistics (this);
809 public override OperationalStatus OperationalStatus {
811 if(((MacOsInterfaceFlags)_ifa_flags & MacOsInterfaceFlags.IFF_UP) == MacOsInterfaceFlags.IFF_UP){
812 return OperationalStatus.Up;
814 return OperationalStatus.Unknown;
818 public override bool SupportsMulticast {
820 return ((MacOsInterfaceFlags)_ifa_flags & MacOsInterfaceFlags.IFF_MULTICAST) == MacOsInterfaceFlags.IFF_MULTICAST;
826 class Win32NetworkInterface2 : NetworkInterface
828 [DllImport ("iphlpapi.dll", SetLastError = true)]
829 static extern int GetAdaptersInfo (IntPtr info, ref int size);
831 [DllImport ("iphlpapi.dll", SetLastError = true)]
832 static extern int GetIfEntry (ref Win32_MIB_IFROW row);
834 public static Win32_IP_ADAPTER_INFO GetAdapterInfoByIndex (int index)
836 foreach (Win32_IP_ADAPTER_INFO info in GetAdaptersInfo ())
837 if (info.Index == index)
839 throw new IndexOutOfRangeException ("No adapter found for index " + index);
842 static Win32_IP_ADAPTER_INFO [] GetAdaptersInfo ()
845 IntPtr ptr = IntPtr.Zero;
846 GetAdaptersInfo (ptr, ref len);
847 ptr = Marshal.AllocHGlobal(len);
848 int ret = GetAdaptersInfo (ptr, ref len);
851 throw new NetworkInformationException (ret);
853 List<Win32_IP_ADAPTER_INFO> l = new List<Win32_IP_ADAPTER_INFO> ();
854 Win32_IP_ADAPTER_INFO info;
855 for (IntPtr p = ptr; p != IntPtr.Zero; p = info.Next) {
856 info = Marshal.PtrToStructure<Win32_IP_ADAPTER_INFO> (p);
862 Win32_IP_ADAPTER_ADDRESSES addr;
863 Win32_MIB_IFROW mib4, mib6;
864 Win32IPv4InterfaceStatistics ip4stats;
865 IPInterfaceProperties ip_if_props;
867 internal Win32NetworkInterface2 (Win32_IP_ADAPTER_ADDRESSES addr)
870 mib4 = default (Win32_MIB_IFROW);
871 mib4.Index = addr.Alignment.IfIndex;
872 if (GetIfEntry (ref mib4) != 0)
873 mib4.Index = -1; // unavailable;
874 mib6 = default (Win32_MIB_IFROW);
875 mib6.Index = addr.Ipv6IfIndex;
876 if (GetIfEntry (ref mib6) != 0)
877 mib6.Index = -1; // unavailable;
878 ip4stats = new Win32IPv4InterfaceStatistics (mib4);
879 ip_if_props = new Win32IPInterfaceProperties2 (addr, mib4, mib6);
882 public override IPInterfaceProperties GetIPProperties ()
887 public override IPv4InterfaceStatistics GetIPv4Statistics ()
892 public override PhysicalAddress GetPhysicalAddress ()
894 byte [] bytes = new byte [addr.PhysicalAddressLength];
895 Array.Copy (addr.PhysicalAddress, 0, bytes, 0, bytes.Length);
896 return new PhysicalAddress (bytes);
899 public override bool Supports (NetworkInterfaceComponent networkInterfaceComponent)
901 switch (networkInterfaceComponent) {
902 case NetworkInterfaceComponent.IPv4:
903 return mib4.Index >= 0;
904 case NetworkInterfaceComponent.IPv6:
905 return mib6.Index >= 0;
910 public override string Description {
911 get { return addr.Description; }
913 public override string Id {
914 get { return addr.AdapterName; }
916 public override bool IsReceiveOnly {
917 get { return addr.IsReceiveOnly; }
919 public override string Name {
920 get { return addr.FriendlyName; }
922 public override NetworkInterfaceType NetworkInterfaceType {
923 get { return addr.IfType; }
925 public override OperationalStatus OperationalStatus {
926 get { return addr.OperStatus; }
928 public override long Speed {
929 get { return mib6.Index >= 0 ? mib6.Speed : mib4.Speed; }
931 public override bool SupportsMulticast {
932 get { return !addr.NoMulticast; }