Merge pull request #3008 from lateralusX/jlorenss/win-x64-shutdown-crash
[mono.git] / mcs / class / System / System.Net.NetworkInformation / NetworkInterface.cs
1 //
2 // System.Net.NetworkInformation.NetworkInterface
3 //
4 // Authors:
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)
11 //
12 // Copyright (c) 2006-2008 Novell, Inc. (http://www.novell.com)
13 //
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:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
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.
32 //
33 using System;
34 using System.Collections.Generic;
35 using System.Collections;
36 using System.Net;
37 using System.Net.Sockets;
38 using System.Runtime.InteropServices;
39 using System.Text;
40 using System.IO;
41 using System.Globalization;
42
43 namespace System.Net.NetworkInformation {
44         static class SystemNetworkInterface {
45
46                 static readonly NetworkInterfaceFactory nif = NetworkInterfaceFactory.Create ();
47
48                 public static NetworkInterface [] GetNetworkInterfaces ()
49                 {
50                         try {
51                                 return nif.GetAllNetworkInterfaces ();
52                         } catch {
53                                 return new NetworkInterface [0];
54                         }
55                 }
56
57                 public static bool InternalGetIsNetworkAvailable ()
58                 {
59                         // TODO:
60                         return true;
61                 }
62
63                 public static int InternalLoopbackInterfaceIndex {
64                         get {
65                                 return nif.GetLoopbackInterfaceIndex ();
66                         }
67                 }
68
69                 public static int InternalIPv6LoopbackInterfaceIndex {
70                         get {
71                                 throw new NotImplementedException ();
72                         }
73                 }
74
75                 public static IPAddress GetNetMask (IPAddress address)
76                 {
77                         return nif.GetNetMask (address);
78                 }
79         }
80
81         abstract class NetworkInterfaceFactory
82         {
83                 internal abstract class UnixNetworkInterfaceAPI : NetworkInterfaceFactory
84                 {
85                         [DllImport("libc")]
86                         public static extern int if_nametoindex(string ifname);
87
88                         [DllImport ("libc")]
89                         protected static extern int getifaddrs (out IntPtr ifap);
90
91                         [DllImport ("libc")]
92                         protected static extern void freeifaddrs (IntPtr ifap);
93                 }
94
95                 class MacOsNetworkInterfaceAPI : UnixNetworkInterfaceAPI
96                 {
97                         const int AF_INET  = 2;
98                         const int AF_INET6 = 30;
99                         const int AF_LINK  = 18;
100
101                         public override NetworkInterface [] GetAllNetworkInterfaces ()
102                         {
103                                 var interfaces = new Dictionary <string, MacOsNetworkInterface> ();
104                                 IntPtr ifap;
105                                 if (getifaddrs (out ifap) != 0)
106                                         throw new SystemException ("getifaddrs() failed");
107
108                                 try {
109                                         IntPtr next = ifap;
110                                         while (next != IntPtr.Zero) {
111                                                 MacOsStructs.ifaddrs addr = (MacOsStructs.ifaddrs) Marshal.PtrToStructure (next, typeof (MacOsStructs.ifaddrs));
112                                                 IPAddress address = IPAddress.None;
113                                                 string    name = addr.ifa_name;
114                                                 int       index = -1;
115                                                 byte[]    macAddress = null;
116                                                 NetworkInterfaceType type = NetworkInterfaceType.Unknown;
117
118                                                 if (addr.ifa_addr != IntPtr.Zero) {
119                                                         // optain IPAddress
120                                                         MacOsStructs.sockaddr sockaddr = (MacOsStructs.sockaddr) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr));
121
122                                                         if (sockaddr.sa_family == AF_INET6) {
123                                                                 MacOsStructs.sockaddr_in6 sockaddr6 = (MacOsStructs.sockaddr_in6) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr_in6));
124                                                                 address = new IPAddress (sockaddr6.sin6_addr.u6_addr8, sockaddr6.sin6_scope_id);
125                                                         } else if (sockaddr.sa_family == AF_INET) {
126                                                                 MacOsStructs.sockaddr_in sockaddrin = (MacOsStructs.sockaddr_in) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr_in));
127                                                                 address = new IPAddress (sockaddrin.sin_addr);
128                                                         } else if (sockaddr.sa_family == AF_LINK) {
129                                                                 MacOsStructs.sockaddr_dl sockaddrdl = new MacOsStructs.sockaddr_dl ();
130                                                                 sockaddrdl.Read (addr.ifa_addr);
131
132                                                                 macAddress = new byte [(int) sockaddrdl.sdl_alen];
133                                                                 // copy mac address from sdl_data field starting at last index pos of interface name into array macaddress, starting
134                                                                 // at index 0
135                                                                 Array.Copy (sockaddrdl.sdl_data, sockaddrdl.sdl_nlen, macAddress, 0, Math.Min (macAddress.Length, sockaddrdl.sdl_data.Length - sockaddrdl.sdl_nlen));
136
137                                                                 index = sockaddrdl.sdl_index;
138
139                                                                 int hwtype = (int) sockaddrdl.sdl_type;
140                                                                 if (Enum.IsDefined (typeof (MacOsArpHardware), hwtype)) {
141                                                                         switch ((MacOsArpHardware) hwtype) {
142                                                                                 case MacOsArpHardware.ETHER:
143                                                                                         type = NetworkInterfaceType.Ethernet;
144                                                                                         break;
145
146                                                                                 case MacOsArpHardware.ATM:
147                                                                                         type = NetworkInterfaceType.Atm;
148                                                                                         break;
149                                                                                 
150                                                                                 case MacOsArpHardware.SLIP:
151                                                                                         type = NetworkInterfaceType.Slip;
152                                                                                         break;
153                                                                                 
154                                                                                 case MacOsArpHardware.PPP:
155                                                                                         type = NetworkInterfaceType.Ppp;
156                                                                                         break;
157                                                                                 
158                                                                                 case MacOsArpHardware.LOOPBACK:
159                                                                                         type = NetworkInterfaceType.Loopback;
160                                                                                         macAddress = null;
161                                                                                         break;
162
163                                                                                 case MacOsArpHardware.FDDI:
164                                                                                         type = NetworkInterfaceType.Fddi;
165                                                                                         break;
166                                                                         }
167                                                                 }
168                                                         }
169                                                 }
170
171                                                 MacOsNetworkInterface iface = null;
172
173                                                 // create interface if not already present
174                                                 if (!interfaces.TryGetValue (name, out iface)) {
175                                                         iface = new MacOsNetworkInterface (name, addr.ifa_flags);
176                                                         interfaces.Add (name, iface);
177                                                 }
178
179                                                 // if a new address has been found, add it
180                                                 if (!address.Equals (IPAddress.None))
181                                                         iface.AddAddress (address);
182
183                                                 // set link layer info, if iface has macaddress or is loopback device
184                                                 if (macAddress != null || type == NetworkInterfaceType.Loopback)
185                                                         iface.SetLinkLayerInfo (index, macAddress, type);
186
187                                                 next = addr.ifa_next;
188                                         }
189                                 } finally {
190                                         freeifaddrs (ifap);
191                                 }
192
193                                 NetworkInterface [] result = new NetworkInterface [interfaces.Count];
194                                 int x = 0;
195                                 foreach (NetworkInterface thisInterface in interfaces.Values) {
196                                         result [x] = thisInterface;
197                                         x++;
198                                 }
199                                 return result;
200                         }
201
202                         public override int GetLoopbackInterfaceIndex ()
203                         {
204                                 return if_nametoindex ("lo0");
205                         }
206
207                         public override IPAddress GetNetMask (IPAddress address)
208                         {
209                                 IntPtr ifap;
210                                 if (getifaddrs (out ifap) != 0)
211                                         throw new SystemException ("getifaddrs() failed");
212
213                                 try {
214                                         IntPtr next = ifap;
215                                         while (next != IntPtr.Zero) {
216                                                 MacOsStructs.ifaddrs addr = (MacOsStructs.ifaddrs) Marshal.PtrToStructure (next, typeof (MacOsStructs.ifaddrs));
217
218                                                 if (addr.ifa_addr != IntPtr.Zero) {
219                                                         // optain IPAddress
220                                                         MacOsStructs.sockaddr sockaddr = (MacOsStructs.sockaddr) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr));
221
222                                                         if (sockaddr.sa_family == AF_INET) {
223                                                                 MacOsStructs.sockaddr_in sockaddrin = (MacOsStructs.sockaddr_in) Marshal.PtrToStructure (addr.ifa_addr, typeof (MacOsStructs.sockaddr_in));
224                                                                 var saddress = new IPAddress (sockaddrin.sin_addr);
225                                                                 if (address.Equals (saddress))
226                                                                         return new IPAddress(((sockaddr_in)Marshal.PtrToStructure(addr.ifa_netmask, typeof(sockaddr_in))).sin_addr);
227                                                         }
228                                                 }
229                                                 next = addr.ifa_next;
230                                         }
231                                 } finally {
232                                         freeifaddrs (ifap);
233                                 }
234
235                                 return null;
236                         }
237                 }
238
239                 class LinuxNetworkInterfaceAPI : UnixNetworkInterfaceAPI
240                 {
241                         const int AF_INET = 2;
242                         const int AF_INET6 = 10;
243                         const int AF_PACKET = 17;
244
245                         static void FreeInterfaceAddresses (IntPtr ifap)
246                         {
247 #if MONODROID
248                                 AndroidPlatform.FreeInterfaceAddresses (ifap);
249 #else
250                                 freeifaddrs (ifap);
251 #endif
252                         }
253
254                         static int GetInterfaceAddresses (out IntPtr ifap)
255                         {
256 #if MONODROID
257                                 return AndroidPlatform.GetInterfaceAddresses (out ifap);
258 #else
259                                 return getifaddrs (out ifap);
260 #endif
261                         }
262
263                         public override NetworkInterface [] GetAllNetworkInterfaces ()
264                         {
265
266                                 var interfaces = new Dictionary <string, LinuxNetworkInterface> ();
267                                 IntPtr ifap;
268                                 if (GetInterfaceAddresses (out ifap) != 0)
269                                         throw new SystemException ("getifaddrs() failed");
270
271                                 try {
272                                         IntPtr next = ifap;
273                                         while (next != IntPtr.Zero) {
274                                                 ifaddrs   addr = (ifaddrs) Marshal.PtrToStructure (next, typeof (ifaddrs));
275                                                 IPAddress address = IPAddress.None;
276                                                 string    name = addr.ifa_name;
277                                                 int       index = -1;
278                                                 byte[]    macAddress = null;
279                                                 NetworkInterfaceType type = NetworkInterfaceType.Unknown;
280                                                 int       nullNameCount = 0;
281
282                                                 if (addr.ifa_addr != IntPtr.Zero) {
283                                                         sockaddr_in sockaddr = (sockaddr_in) Marshal.PtrToStructure (addr.ifa_addr, typeof (sockaddr_in));
284
285                                                         if (sockaddr.sin_family == AF_INET6) {
286                                                                 sockaddr_in6 sockaddr6 = (sockaddr_in6) Marshal.PtrToStructure (addr.ifa_addr, typeof (sockaddr_in6));
287                                                                 address = new IPAddress (sockaddr6.sin6_addr.u6_addr8, sockaddr6.sin6_scope_id);
288                                                         } else if (sockaddr.sin_family == AF_INET) {
289                                                                 address = new IPAddress (sockaddr.sin_addr);
290                                                         } else if (sockaddr.sin_family == AF_PACKET) {
291                                                                 sockaddr_ll sockaddrll = (sockaddr_ll) Marshal.PtrToStructure (addr.ifa_addr, typeof (sockaddr_ll));
292                                                                 if (((int)sockaddrll.sll_halen) > sockaddrll.sll_addr.Length){
293                                                                         Console.Error.WriteLine ("Got a bad hardware address length for an AF_PACKET {0} {1}",
294                                                                                                  sockaddrll.sll_halen, sockaddrll.sll_addr.Length);
295                                                                         next = addr.ifa_next;
296                                                                         continue;
297                                                                 }
298                                                                 
299                                                                 macAddress = new byte [(int) sockaddrll.sll_halen];
300                                                                 Array.Copy (sockaddrll.sll_addr, 0, macAddress, 0, macAddress.Length);
301                                                                 index = sockaddrll.sll_ifindex;
302
303                                                                 int hwtype = (int)sockaddrll.sll_hatype;
304                                                                 if (Enum.IsDefined (typeof (LinuxArpHardware), hwtype)) {
305                                                                         switch ((LinuxArpHardware)hwtype) {
306                                                                                 case LinuxArpHardware.EETHER:
307                                                                                         goto case LinuxArpHardware.ETHER;
308                                                                                         
309                                                                                 case LinuxArpHardware.ETHER:
310                                                                                         type = NetworkInterfaceType.Ethernet;
311                                                                                         break;
312
313                                                                                 case LinuxArpHardware.PRONET:
314                                                                                         type = NetworkInterfaceType.TokenRing;
315                                                                                         break;
316
317                                                                                 case LinuxArpHardware.ATM:
318                                                                                         type = NetworkInterfaceType.Atm;
319                                                                                         break;
320                                                                                 
321                                                                                 case LinuxArpHardware.SLIP:
322                                                                                 case LinuxArpHardware.CSLIP:
323                                                                                 case LinuxArpHardware.SLIP6:
324                                                                                 case LinuxArpHardware.CSLIP6:
325                                                                                         type = NetworkInterfaceType.Slip;
326                                                                                         break;
327                                                                                 
328                                                                                 case LinuxArpHardware.PPP:
329                                                                                         type = NetworkInterfaceType.Ppp;
330                                                                                         break;
331                                                                                 
332                                                                                 case LinuxArpHardware.LOOPBACK:
333                                                                                         type = NetworkInterfaceType.Loopback;
334                                                                                         macAddress = null;
335                                                                                         break;
336
337                                                                                 case LinuxArpHardware.FDDI:
338                                                                                         type = NetworkInterfaceType.Fddi;
339                                                                                         break;
340
341                                                                                 case LinuxArpHardware.SIT:
342                                                                                 case LinuxArpHardware.IPDDP:
343                                                                                 case LinuxArpHardware.IPGRE:
344                                                                                 case LinuxArpHardware.IP6GRE:
345                                                                                 case LinuxArpHardware.TUNNEL6:
346                                                                                 case LinuxArpHardware.TUNNEL:
347                                                                                         type = NetworkInterfaceType.Tunnel;
348                                                                                         break;
349                                                                         }
350                                                                 }
351                                                         }
352                                                 }
353
354                                                 LinuxNetworkInterface iface = null;
355
356                                                 if (String.IsNullOrEmpty (name))
357                                                         name = "\0" + (++nullNameCount).ToString ();
358                                                 
359                                                 if (!interfaces.TryGetValue (name, out iface)) {
360                                                         iface = new LinuxNetworkInterface (name);
361                                                         interfaces.Add (name, iface);
362                                                 }
363
364                                                 if (!address.Equals (IPAddress.None))
365                                                         iface.AddAddress (address);
366
367                                                 if (macAddress != null || type == NetworkInterfaceType.Loopback) {
368                                                         if (type == NetworkInterfaceType.Ethernet) {
369                                                                 if (Directory.Exists(iface.IfacePath + "wireless")) {
370                                                                         type = NetworkInterfaceType.Wireless80211;
371                                                                 }
372                                                         }
373                                                         iface.SetLinkLayerInfo (index, macAddress, type);
374                                                 }
375
376                                                 next = addr.ifa_next;
377                                         }
378                                 } finally {
379                                         FreeInterfaceAddresses (ifap);
380                                 }
381
382                                 NetworkInterface [] result = new NetworkInterface [interfaces.Count];
383                                 int x = 0;
384                                 foreach (NetworkInterface thisInterface in interfaces.Values) {
385                                         result [x] = thisInterface;
386                                         x++;
387                                 }
388                                 return result;
389                         }
390
391                         public override int GetLoopbackInterfaceIndex ()
392                         {
393                                 return if_nametoindex ("lo");
394                         }
395
396                         public override IPAddress GetNetMask (IPAddress address)
397                         {
398                                 foreach (ifaddrs networkInteface in GetNetworkInterfaces()) {
399                                         if (networkInteface.ifa_addr == IntPtr.Zero)
400                                                 continue;
401
402                                         var sockaddr = (sockaddr_in)Marshal.PtrToStructure(networkInteface.ifa_addr, typeof(sockaddr_in));
403
404                                         if (sockaddr.sin_family != AF_INET)
405                                                 continue;
406
407                                         if (!address.Equals(new IPAddress(sockaddr.sin_addr)))
408                                                 continue;
409
410                                         var netmask = (sockaddr_in)Marshal.PtrToStructure(networkInteface.ifa_netmask, typeof(sockaddr_in));
411                                         return new IPAddress(netmask.sin_addr);
412                                 }
413
414                                 return null;
415                         }
416
417                         private static IEnumerable<ifaddrs> GetNetworkInterfaces()
418                         {
419                                 IntPtr ifap = IntPtr.Zero;
420
421                                 try {
422                                         if (GetInterfaceAddresses(out ifap) != 0)
423                                                 yield break;
424
425                                         var next = ifap;
426                                         while (next != IntPtr.Zero) {
427                                                 var addr = (ifaddrs)Marshal.PtrToStructure(next, typeof(ifaddrs));
428                                                 yield return addr;
429                                                 next = addr.ifa_next;
430                                         }
431                                 } finally {
432                                         if (ifap != IntPtr.Zero)
433                                                 FreeInterfaceAddresses(ifap);
434                                 }
435                         }
436                 }
437
438 #if !MOBILE
439                 class Win32NetworkInterfaceAPI : NetworkInterfaceFactory
440                 {
441                         [DllImport ("iphlpapi.dll", SetLastError = true)]
442                         static extern int GetAdaptersAddresses (uint family, uint flags, IntPtr reserved, byte [] info, ref int size);
443
444                         unsafe static Win32_IP_ADAPTER_ADDRESSES [] GetAdaptersAddresses ()
445                         {
446                                 byte [] bytes = null;
447                                 int len = 0;
448                                 GetAdaptersAddresses (0, 0, IntPtr.Zero, bytes, ref len);
449                                 bytes = new byte [len];
450                                 int ret = GetAdaptersAddresses (0, 0, IntPtr.Zero, bytes, ref len);
451                                 if (ret != 0)
452                                         throw new NetworkInformationException (ret);
453
454                                 List<Win32_IP_ADAPTER_ADDRESSES> l = new List<Win32_IP_ADAPTER_ADDRESSES> ();
455                                 fixed (byte* ptr = bytes) {
456                                         Win32_IP_ADAPTER_ADDRESSES info;
457                                         for (IntPtr p = (IntPtr) ptr; p != IntPtr.Zero; p = info.Next) {
458                                                 info = new Win32_IP_ADAPTER_ADDRESSES ();
459                                                 Marshal.PtrToStructure (p, info);
460                                                 l.Add (info);
461                                         }
462                                 }
463                                 return l.ToArray ();
464                         }
465
466                         public override NetworkInterface [] GetAllNetworkInterfaces ()
467                         {
468         //                      Win32_IP_ADAPTER_INFO [] ai = GetAdaptersInfo ();
469                                 Win32_IP_ADAPTER_ADDRESSES [] aa = GetAdaptersAddresses ();
470                                 NetworkInterface [] ret = new NetworkInterface [aa.Length];
471                                 for (int i = 0; i < ret.Length; i++)
472                                         ret [i] = new Win32NetworkInterface2 (aa [i]);
473                                 return ret;
474                         }
475
476                         public override int GetLoopbackInterfaceIndex ()
477                         {
478                                 throw new NotImplementedException ();
479                         }
480
481                         public override IPAddress GetNetMask (IPAddress address)
482                         {
483                                 throw new NotImplementedException ();
484                         }
485                 }
486 #endif
487
488                 public abstract NetworkInterface [] GetAllNetworkInterfaces ();
489                 public abstract int GetLoopbackInterfaceIndex ();
490                 public abstract IPAddress GetNetMask (IPAddress address);
491
492                 public static NetworkInterfaceFactory Create ()
493                 {
494 #if MONOTOUCH || XAMMAC
495                         return new MacOsNetworkInterfaceAPI ();
496 #else
497                         Version windowsVer51 = new Version (5, 1);
498                         bool runningOnUnix = (Environment.OSVersion.Platform == PlatformID.Unix);
499
500                         if (runningOnUnix) {
501                                 if (Platform.IsMacOS || Platform.IsFreeBSD)
502                                         return new MacOsNetworkInterfaceAPI ();
503                                         
504                                 return new LinuxNetworkInterfaceAPI ();
505                         }
506
507 #if !MOBILE
508                         if (Environment.OSVersion.Version >= windowsVer51)
509                                 return new Win32NetworkInterfaceAPI ();
510 #endif
511
512                         throw new NotImplementedException ();
513 #endif
514                 }
515         }
516
517         abstract class UnixNetworkInterface : NetworkInterface
518         {
519
520                 protected IPv4InterfaceStatistics ipv4stats;
521                 protected IPInterfaceProperties ipproperties;
522                 
523                 string               name;
524                 //int                  index;
525                 protected List <IPAddress> addresses;
526                 byte[]               macAddress;
527                 NetworkInterfaceType type;
528                 
529                 internal UnixNetworkInterface (string name)
530                 {
531                         this.name = name;
532                         addresses = new List<IPAddress> ();
533                 }
534
535                 internal void AddAddress (IPAddress address)
536                 {
537                         addresses.Add (address);
538                 }
539
540                 internal void SetLinkLayerInfo (int index, byte[] macAddress, NetworkInterfaceType type)
541                 {
542                         //this.index = index;
543                         this.macAddress = macAddress;
544                         this.type = type;
545                 }
546
547                 public override PhysicalAddress GetPhysicalAddress ()
548                 {
549                         if (macAddress != null)
550                                 return new PhysicalAddress (macAddress);
551                         else
552                                 return PhysicalAddress.None;
553                 }
554
555                 public override bool Supports (NetworkInterfaceComponent networkInterfaceComponent)
556                 {
557                         bool wantIPv4 = networkInterfaceComponent == NetworkInterfaceComponent.IPv4;
558                         bool wantIPv6 = wantIPv4 ? false : networkInterfaceComponent == NetworkInterfaceComponent.IPv6;
559                                 
560                         foreach (IPAddress address in addresses) {
561                                 if (wantIPv4 && address.AddressFamily == AddressFamily.InterNetwork)
562                                         return true;
563                                 else if (wantIPv6 && address.AddressFamily == AddressFamily.InterNetworkV6)
564                                         return true;
565                         }
566                         
567                         return false;
568                 }
569
570                 public override string Description {
571                         get { return name; }
572                 }
573
574                 public override string Id {
575                         get { return name; }
576                 }
577
578                 public override bool IsReceiveOnly {
579                         get { return false; }
580                 }
581
582                 public override string Name {
583                         get { return name; }
584                 }
585                 
586                 public override NetworkInterfaceType NetworkInterfaceType {
587                         get { return type; }
588                 }
589                 
590                 [MonoTODO ("Parse dmesg?")]
591                 public override long Speed {
592                         get {
593                                 // Bits/s
594                                 return 1000000;
595                         }
596                 }
597
598                 internal int NameIndex {
599                         get {
600                                 return NetworkInterfaceFactory.UnixNetworkInterfaceAPI.if_nametoindex (Name);
601                         }
602                 }
603         }
604
605         //
606         // This class needs support from the libsupport.so library to fetch the
607         // data using arch-specific ioctls.
608         //
609         // For this to work, we have to create this on the factory above.
610         //
611         sealed class LinuxNetworkInterface : UnixNetworkInterface
612         {
613                 //NetworkInterfaceType type;
614                 string               iface_path;
615                 string               iface_operstate_path;
616                 string               iface_flags_path;          
617
618                 internal string IfacePath {
619                         get { return iface_path; }
620                 }
621                 
622                 internal LinuxNetworkInterface (string name)
623                         : base (name)
624                 {
625                         iface_path = "/sys/class/net/" + name + "/";
626                         iface_operstate_path = iface_path + "operstate";
627                         iface_flags_path = iface_path + "flags";
628                 }
629
630                 public override IPInterfaceProperties GetIPProperties ()
631                 {
632                         if (ipproperties == null)
633                                 ipproperties = new LinuxIPInterfaceProperties (this, addresses);
634                         return ipproperties;
635                 }
636
637                 public override IPv4InterfaceStatistics GetIPv4Statistics ()
638                 {
639                         if (ipv4stats == null)
640                                 ipv4stats = new LinuxIPv4InterfaceStatistics (this);
641                         return ipv4stats;
642                 }
643
644                 public override OperationalStatus OperationalStatus {
645                         get {
646                                 if (!Directory.Exists (iface_path))
647                                         return OperationalStatus.Unknown;
648                                 
649                                 try {
650                                         string s = ReadLine (iface_operstate_path);
651
652                                         switch (s){
653                                                 case "unknown":
654                                                         return OperationalStatus.Unknown;
655                                                 
656                                                 case "notpresent":
657                                                         return OperationalStatus.NotPresent;
658
659                                                 case "down":
660                                                         return OperationalStatus.Down;
661
662                                                 case "lowerlayerdown":
663                                                         return OperationalStatus.LowerLayerDown;
664
665                                                 case "testing":
666                                                         return OperationalStatus.Testing;
667
668                                                 case "dormant":
669                                                         return OperationalStatus.Dormant;
670
671                                                 case "up":
672                                                         return OperationalStatus.Up;
673                                         }
674                                 } catch {
675                                 }
676                                 return OperationalStatus.Unknown;
677                         }
678                 }
679
680                 public override bool SupportsMulticast {
681                         get {
682                                 if (!Directory.Exists (iface_path))
683                                         return false;
684                                 
685                                 try {
686                                         string s = ReadLine (iface_flags_path);
687                                         if (s.Length > 2 && s [0] == '0' && s [1] == 'x')
688                                                 s = s.Substring (2);
689                                         
690                                         ulong f = UInt64.Parse (s, NumberStyles.HexNumber);
691
692                                         // Hardcoded, only useful for Linux.
693                                         return ((f & 0x1000) == 0x1000);
694                                 } catch {
695                                         return false;
696                                 }
697                         }
698                 }
699
700                 internal static string ReadLine (string path)
701                 {
702                         using (FileStream fs = File.OpenRead (path)){
703                                 using (StreamReader sr = new StreamReader (fs)){
704                                         return sr.ReadLine ();
705                                 }
706                         }
707                 }               
708         }
709
710         sealed class MacOsNetworkInterface : UnixNetworkInterface
711         {
712                 private uint _ifa_flags;
713
714                 internal MacOsNetworkInterface (string name, uint ifa_flags)
715                         : base (name)
716                 {
717                         _ifa_flags = ifa_flags;
718                 }
719
720                 public override IPInterfaceProperties GetIPProperties ()
721                 {
722                         if (ipproperties == null)
723                                 ipproperties = new MacOsIPInterfaceProperties (this, addresses);
724                         return ipproperties;
725                 }
726
727                 public override IPv4InterfaceStatistics GetIPv4Statistics ()
728                 {
729                         if (ipv4stats == null)
730                                 ipv4stats = new MacOsIPv4InterfaceStatistics (this);
731                         return ipv4stats;
732                 }
733
734                 public override OperationalStatus OperationalStatus {
735                         get {
736                                 if(((MacOsInterfaceFlags)_ifa_flags & MacOsInterfaceFlags.IFF_UP) == MacOsInterfaceFlags.IFF_UP){
737                                         return OperationalStatus.Up;
738                                 }
739                                 return OperationalStatus.Unknown;
740                         }
741                 }
742
743                 public override bool SupportsMulticast {
744                         get {
745                                 return ((MacOsInterfaceFlags)_ifa_flags & MacOsInterfaceFlags.IFF_MULTICAST) == MacOsInterfaceFlags.IFF_MULTICAST;
746                         }
747                 }
748         }
749
750 #if !MOBILE
751         class Win32NetworkInterface2 : NetworkInterface
752         {
753                 [DllImport ("iphlpapi.dll", SetLastError = true)]
754                 static extern int GetAdaptersInfo (byte [] info, ref int size);
755
756                 [DllImport ("iphlpapi.dll", SetLastError = true)]
757                 static extern int GetIfEntry (ref Win32_MIB_IFROW row);
758
759                 public static Win32_IP_ADAPTER_INFO GetAdapterInfoByIndex (int index)
760                 {
761                         foreach (Win32_IP_ADAPTER_INFO info in GetAdaptersInfo ())
762                                 if (info.Index == index)
763                                         return info;
764                         return null;
765                 }
766
767                 unsafe static Win32_IP_ADAPTER_INFO [] GetAdaptersInfo ()
768                 {
769                         byte [] bytes = null;
770                         int len = 0;
771                         GetAdaptersInfo (bytes, ref len);
772                         bytes = new byte [len];
773                         int ret = GetAdaptersInfo (bytes, ref len);
774
775                         if (ret != 0)
776                                 throw new NetworkInformationException (ret);
777
778                         List<Win32_IP_ADAPTER_INFO> l = new List<Win32_IP_ADAPTER_INFO> ();
779                         fixed (byte* ptr = bytes) {
780                                 Win32_IP_ADAPTER_INFO info;
781                                 for (IntPtr p = (IntPtr) ptr; p != IntPtr.Zero; p = info.Next) {
782                                         info = new Win32_IP_ADAPTER_INFO ();
783                                         Marshal.PtrToStructure (p, info);
784                                         l.Add (info);
785                                 }
786                         }
787                         return l.ToArray ();
788                 }
789
790                 Win32_IP_ADAPTER_ADDRESSES addr;
791                 Win32_MIB_IFROW mib4, mib6;
792                 Win32IPv4InterfaceStatistics ip4stats;
793                 IPInterfaceProperties ip_if_props;
794
795                 internal Win32NetworkInterface2 (Win32_IP_ADAPTER_ADDRESSES addr)
796                 {
797                         this.addr = addr;
798                         mib4 = default (Win32_MIB_IFROW);
799                         mib4.Index = addr.Alignment.IfIndex;
800                         if (GetIfEntry (ref mib4) != 0)
801                                 mib4.Index = -1; // unavailable;
802                         mib6 = default (Win32_MIB_IFROW);
803                         mib6.Index = addr.Ipv6IfIndex;
804                         if (GetIfEntry (ref mib6) != 0)
805                                 mib6.Index = -1; // unavailable;
806                         ip4stats = new Win32IPv4InterfaceStatistics (mib4);
807                         ip_if_props = new Win32IPInterfaceProperties2 (addr, mib4, mib6);
808                 }
809
810                 public override IPInterfaceProperties GetIPProperties ()
811                 {
812                         return ip_if_props;
813                 }
814
815                 public override IPv4InterfaceStatistics GetIPv4Statistics ()
816                 {
817                         return ip4stats;
818                 }
819
820                 public override PhysicalAddress GetPhysicalAddress ()
821                 {
822                         byte [] bytes = new byte [addr.PhysicalAddressLength];
823                         Array.Copy (addr.PhysicalAddress, 0, bytes, 0, bytes.Length);
824                         return new PhysicalAddress (bytes);
825                 }
826
827                 public override bool Supports (NetworkInterfaceComponent networkInterfaceComponent)
828                 {
829                         switch (networkInterfaceComponent) {
830                         case NetworkInterfaceComponent.IPv4:
831                                 return mib4.Index >= 0;
832                         case NetworkInterfaceComponent.IPv6:
833                                 return mib6.Index >= 0;
834                         }
835                         return false;
836                 }
837
838                 public override string Description {
839                         get { return addr.Description; }
840                 }
841                 public override string Id {
842                         get { return addr.AdapterName; }
843                 }
844                 public override bool IsReceiveOnly {
845                         get { return addr.IsReceiveOnly; }
846                 }
847                 public override string Name {
848                         get { return addr.FriendlyName; }
849                 }
850                 public override NetworkInterfaceType NetworkInterfaceType {
851                         get { return addr.IfType; }
852                 }
853                 public override OperationalStatus OperationalStatus {
854                         get { return addr.OperStatus; }
855                 }
856                 public override long Speed {
857                         get { return mib6.Index >= 0 ? mib6.Speed : mib4.Speed; }
858                 }
859                 public override bool SupportsMulticast {
860                         get { return !addr.NoMulticast; }
861                 }
862         }
863 #endif
864 }
865