Changed link from GUID to URL
[mono.git] / mcs / class / System / System.Net.NetworkInformation / IPGlobalProperties.cs
1 //
2 // System.Net.NetworkInformation.IPGlobalProperties
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //      Atsushi Enomoto (atsushi@ximian.com)
7 //      Marek Safar (marek.safar@gmail.com)
8 //
9 // Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections.Generic;
32 using System.Collections.Specialized;
33 using System.Globalization;
34 using System.IO;
35 using System.Net.Sockets;
36 using System.Runtime.InteropServices;
37 using System.Text;
38
39 namespace System.Net.NetworkInformation {
40         public abstract class IPGlobalProperties {
41                 protected IPGlobalProperties ()
42                 {
43                 }
44
45                 public static IPGlobalProperties GetIPGlobalProperties ()
46                 {
47 #if MONODROID
48                         return new AndroidIPGlobalProperties ();
49 #else
50                         switch (Environment.OSVersion.Platform) {
51                         case PlatformID.Unix:
52                                 MibIPGlobalProperties impl = null;
53                                 if (Directory.Exists (MibIPGlobalProperties.ProcDir)) {
54                                         impl = new MibIPGlobalProperties (MibIPGlobalProperties.ProcDir);
55                                         if (File.Exists (impl.StatisticsFile))
56                                                 return impl;
57                                 }
58                                 if (Directory.Exists (MibIPGlobalProperties.CompatProcDir)) {
59                                         impl = new MibIPGlobalProperties (MibIPGlobalProperties.CompatProcDir);
60                                         if (File.Exists (impl.StatisticsFile))
61                                                 return impl;
62                                 }
63                                 return new UnixIPGlobalProperties ();
64                         default:
65                                 return new Win32IPGlobalProperties ();
66                         }
67 #endif
68                 }
69
70                 internal static IPGlobalProperties InternalGetIPGlobalProperties()
71                 {
72                         return GetIPGlobalProperties ();
73                 }
74
75                 public abstract TcpConnectionInformation [] GetActiveTcpConnections ();
76                 public abstract IPEndPoint [] GetActiveTcpListeners ();
77                 public abstract IPEndPoint [] GetActiveUdpListeners ();
78                 public abstract IcmpV4Statistics GetIcmpV4Statistics ();
79                 public abstract IcmpV6Statistics GetIcmpV6Statistics ();
80                 public abstract IPGlobalStatistics GetIPv4GlobalStatistics ();
81                 public abstract IPGlobalStatistics GetIPv6GlobalStatistics ();
82                 public abstract TcpStatistics GetTcpIPv4Statistics ();
83                 public abstract TcpStatistics GetTcpIPv6Statistics ();
84                 public abstract UdpStatistics GetUdpIPv4Statistics ();
85                 public abstract UdpStatistics GetUdpIPv6Statistics ();
86
87                 public abstract string DhcpScopeName { get; }
88                 public abstract string DomainName { get; }
89                 public abstract string HostName { get; }
90                 public abstract bool IsWinsProxy { get; }
91                 public abstract NetBiosNodeType NodeType { get; }
92         }
93
94         abstract class CommonUnixIPGlobalProperties : IPGlobalProperties
95         {
96                 [DllImport ("libc")]
97                 static extern int gethostname ([MarshalAs (UnmanagedType.LPArray, SizeParamIndex = 1)] byte [] name, int len);
98
99                 [DllImport ("libc")]
100                 static extern int getdomainname ([MarshalAs (UnmanagedType.LPArray, SizeParamIndex = 1)] byte [] name, int len);
101
102                 public override string DhcpScopeName {
103                         get { return String.Empty; }
104                 }
105
106                 public override string DomainName {
107                         get {
108                                 byte [] bytes = new byte [256];
109                                 if (getdomainname (bytes, 256) != 0)
110                                         throw new NetworkInformationException ();
111                                 int len = Array.IndexOf<byte> (bytes, 0);
112                                 return Encoding.ASCII.GetString (bytes, 0, len < 0 ? 256 : len);
113                         }
114                 }
115
116                 public override string HostName {
117                         get {
118                                 byte [] bytes = new byte [256];
119                                 if (gethostname (bytes, 256) != 0)
120                                         throw new NetworkInformationException ();
121                                 int len = Array.IndexOf<byte> (bytes, 0);
122                                 return Encoding.ASCII.GetString (bytes, 0, len < 0 ? 256 : len);
123                         }
124                 }
125
126                 public override bool IsWinsProxy {
127                         get { return false; } // no WINS
128                 }
129
130                 public override NetBiosNodeType NodeType {
131                         get { return NetBiosNodeType.Unknown; } // no NetBios
132                 }
133         }
134
135         class UnixIPGlobalProperties : CommonUnixIPGlobalProperties
136         {
137                 public override TcpConnectionInformation [] GetActiveTcpConnections ()
138                 {
139                         throw new NotImplementedException ();
140                 }
141
142                 public override IPEndPoint [] GetActiveTcpListeners ()
143                 {
144                         throw new NotImplementedException ();
145                 }
146
147                 public override IPEndPoint [] GetActiveUdpListeners ()
148                 {
149                         throw new NotImplementedException ();
150                 }
151
152                 public override IcmpV4Statistics GetIcmpV4Statistics ()
153                 {
154                         throw new NotImplementedException ();
155                 }
156
157                 public override IcmpV6Statistics GetIcmpV6Statistics ()
158                 {
159                         throw new NotImplementedException ();
160                 }
161
162                 public override IPGlobalStatistics GetIPv4GlobalStatistics ()
163                 {
164                         throw new NotImplementedException ();
165                 }
166
167                 public override IPGlobalStatistics GetIPv6GlobalStatistics ()
168                 {
169                         throw new NotImplementedException ();
170                 }
171
172                 public override TcpStatistics GetTcpIPv4Statistics ()
173                 {
174                         throw new NotImplementedException ();
175                 }
176
177                 public override TcpStatistics GetTcpIPv6Statistics ()
178                 {
179                         throw new NotImplementedException ();
180                 }
181
182                 public override UdpStatistics GetUdpIPv4Statistics ()
183                 {
184                         throw new NotImplementedException ();
185                 }
186
187                 public override UdpStatistics GetUdpIPv6Statistics ()
188                 {
189                         throw new NotImplementedException ();
190                 }
191         }
192
193         sealed class AndroidIPGlobalProperties : UnixIPGlobalProperties
194         {
195                 public override string DomainName {
196                         get {
197                                 return String.Empty;
198                         }
199                 }
200         }
201
202         // It expects /proc/net/snmp (or /usr/compat/linux/proc/net/snmp),
203         // formatted like:
204         // http://www.linuxdevcenter.com/linux/2000/11/16/example5.html
205         // http://www.linuxdevcenter.com/linux/2000/11/16/example2.html
206         class MibIPGlobalProperties : UnixIPGlobalProperties
207         {
208                 public const string ProcDir = "/proc";
209                 public const string CompatProcDir = "/usr/compat/linux/proc";
210
211                 public readonly string StatisticsFile, StatisticsFileIPv6, TcpFile, Tcp6File, UdpFile, Udp6File;
212
213                 public MibIPGlobalProperties (string procDir)
214                 {
215                         StatisticsFile = Path.Combine (procDir, "net/snmp");
216                         StatisticsFileIPv6 = Path.Combine (procDir, "net/snmp6");
217                         TcpFile = Path.Combine (procDir,"net/tcp");
218                         Tcp6File = Path.Combine (procDir,"net/tcp6");
219                         UdpFile = Path.Combine (procDir,"net/udp");
220                         Udp6File = Path.Combine (procDir,"net/udp6");
221                 }
222
223                 StringDictionary GetProperties4 (string item)
224                 {
225                         string file = StatisticsFile;
226
227                         string head = item + ": ";
228                         using (StreamReader sr = new StreamReader (file, Encoding.ASCII)) {
229                                 string [] keys = null;
230                                 string [] values = null;
231                                 string s = String.Empty;
232                                 do {
233                                         s = sr.ReadLine ();
234                                         if (String.IsNullOrEmpty (s))
235                                                 continue;
236                                         if (s.Length <= head.Length || String.CompareOrdinal (s, 0, head, 0, head.Length) != 0)
237                                                 continue;
238                                         if (keys == null)
239                                                 keys = s.Substring (head.Length).Split (' ');
240                                         else if (values != null)
241                                                 // hmm, there may be better error type...
242                                                 throw CreateException (file, String.Format ("Found duplicate line for values for the same item '{0}'", item));
243                                         else {
244                                                 values = s.Substring (head.Length).Split (' ');
245                                                 break;
246                                         }
247                                 } while (!sr.EndOfStream);
248
249                                 if (values == null)
250                                         throw CreateException (file, String.Format ("No corresponding line was not found for '{0}'", item));
251                                 if (keys.Length != values.Length)
252                                         throw CreateException (file, String.Format ("The counts in the header line and the value line do not match for '{0}'", item));
253                                 StringDictionary dic = new StringDictionary ();
254                                 for (int i = 0; i < keys.Length; i++)
255                                         dic [keys [i]] = values [i];
256                                 return dic;
257                         }
258                 }
259
260                 StringDictionary GetProperties6 (string item)
261                 {
262                         if (!File.Exists (StatisticsFileIPv6))
263                                 throw new NetworkInformationException ();
264
265                         string file = StatisticsFileIPv6;
266
267                         string head = item;
268                         using (StreamReader sr = new StreamReader (file, Encoding.ASCII)) {
269                                 StringDictionary dic = new StringDictionary ();
270                                 string s = String.Empty;
271                                 do {
272                                         s = sr.ReadLine ();
273                                         if (String.IsNullOrEmpty (s))
274                                                 continue;
275                                         if (s.Length <= head.Length || String.CompareOrdinal (s, 0, head, 0, head.Length) != 0)
276                                                 continue;
277                                         int idx = s.IndexOfAny (wsChars, head.Length);
278                                         if (idx < 0)
279                                                 throw CreateException (file, null);
280                                         dic [s.Substring (head.Length, idx - head.Length)] = s.Substring (idx + 1).Trim (wsChars);
281                                 } while (!sr.EndOfStream);
282
283                                 return dic;
284                         }
285                 }
286
287                 static readonly char [] wsChars = new char [] {' ', '\t'};
288
289                 Exception CreateException (string file, string msg)
290                 {
291                         return new InvalidOperationException (String.Format ("Unsupported (unexpected) '{0}' file format. ", file) + msg);
292                 }
293                 IPEndPoint [] GetLocalAddresses (List<string []> list)
294                 {
295                         IPEndPoint [] ret = new IPEndPoint [list.Count];
296                         for (int i = 0; i < ret.Length; i++)
297                                 ret [i] = ToEndpoint (list [i] [1]);
298                         return ret;
299                 }
300
301                 IPEndPoint ToEndpoint (string s)
302                 {
303                         int idx = s.IndexOf (':');
304                         int port = int.Parse (s.Substring (idx + 1), NumberStyles.HexNumber);
305                         if (s.Length == 13)
306                                 return new IPEndPoint (long.Parse (s.Substring (0, idx), NumberStyles.HexNumber), port);
307                         else {
308                                 byte [] bytes = new byte [16];
309                                 for (int i = 0; (i << 1) < idx; i++)
310                                         bytes [i] = byte.Parse (s.Substring (i << 1, 2), NumberStyles.HexNumber);
311                                 return new IPEndPoint (new IPAddress (bytes), port);
312                         }
313                 }
314
315                 void GetRows (string file, List<string []> list)
316                 {
317                         if (!File.Exists (file))
318                                 return;
319                         using (StreamReader sr = new StreamReader (file, Encoding.ASCII)) {
320                                 sr.ReadLine (); // skip first line
321                                 while (!sr.EndOfStream) {
322                                         string [] item = sr.ReadLine ().Split (wsChars, StringSplitOptions.RemoveEmptyEntries);
323                                         if (item.Length < 4)
324                                                 throw CreateException (file, null);
325                                         list.Add (item);
326                                 }
327                         }
328                 }
329
330                 public override TcpConnectionInformation [] GetActiveTcpConnections ()
331                 {
332                         List<string []> list = new List<string []> ();
333                         GetRows (TcpFile, list);
334                         GetRows (Tcp6File, list);
335
336                         TcpConnectionInformation [] ret = new TcpConnectionInformation [list.Count];
337                         for (int i = 0; i < ret.Length; i++) {
338                                 // sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
339                                 IPEndPoint local = ToEndpoint (list [i] [1]);
340                                 IPEndPoint remote = ToEndpoint (list [i] [2]);
341                                 TcpState state = (TcpState) int.Parse (list [i] [3], NumberStyles.HexNumber);
342                                 ret [i] = new TcpConnectionInformationImpl (local, remote, state);
343                         }
344                         return ret;
345                 }
346
347                 public override IPEndPoint [] GetActiveTcpListeners ()
348                 {
349                         List<string []> list = new List<string []> ();
350                         GetRows (TcpFile, list);
351                         GetRows (Tcp6File, list);
352                         return GetLocalAddresses (list);
353                 }
354
355                 public override IPEndPoint [] GetActiveUdpListeners ()
356                 {
357                         List<string []> list = new List<string []> ();
358                         GetRows (UdpFile, list);
359                         GetRows (Udp6File, list);
360                         return GetLocalAddresses (list);
361                 }
362
363                 public override IcmpV4Statistics GetIcmpV4Statistics ()
364                 {
365                         return new MibIcmpV4Statistics (GetProperties4 ("Icmp"));
366                 }
367
368                 public override IcmpV6Statistics GetIcmpV6Statistics ()
369                 {
370                         return new MibIcmpV6Statistics (GetProperties6 ("Icmp6"));
371                 }
372
373                 public override IPGlobalStatistics GetIPv4GlobalStatistics ()
374                 {
375                         return new MibIPGlobalStatistics (GetProperties4 ("Ip"));
376                 }
377
378                 public override IPGlobalStatistics GetIPv6GlobalStatistics ()
379                 {
380                         return new MibIPGlobalStatistics (GetProperties6 ("Ip6"));
381                 }
382
383                 public override TcpStatistics GetTcpIPv4Statistics ()
384                 {
385                         return new MibTcpStatistics (GetProperties4 ("Tcp"));
386                 }
387
388                 public override TcpStatistics GetTcpIPv6Statistics ()
389                 {
390                         // There is no TCP info in /proc/net/snmp,
391                         // so it is shared with IPv4 info.
392                         return new MibTcpStatistics (GetProperties4 ("Tcp"));
393                 }
394
395                 public override UdpStatistics GetUdpIPv4Statistics ()
396                 {
397                         return new MibUdpStatistics (GetProperties4 ("Udp"));
398                 }
399
400                 public override UdpStatistics GetUdpIPv6Statistics ()
401                 {
402                         return new MibUdpStatistics (GetProperties6 ("Udp6"));
403                 }
404         }
405
406         class Win32IPGlobalProperties : IPGlobalProperties
407         {
408                 public const int AF_INET = 2;
409                 public const int AF_INET6 = 23;
410
411                 // FIXME: it might be getting wrong table. I'm getting
412                 // different results from .NET 2.0.
413                 unsafe void FillTcpTable (out List<Win32_MIB_TCPROW> tab4, out List<Win32_MIB_TCP6ROW> tab6)
414                 {
415                         tab4 = new List<Win32_MIB_TCPROW> ();
416                         int size4 = 0;
417                         GetTcpTable (null, ref size4, true); // get size
418                         byte [] bytes4 = new byte [size4];
419                         GetTcpTable (bytes4, ref size4, true); // get list
420
421                         int structSize4 = Marshal.SizeOf (typeof (Win32_MIB_TCPROW));
422
423                         fixed (byte* ptr = bytes4) {
424                                 int count = Marshal.ReadInt32 ((IntPtr) ptr);
425                                 for (int i = 0; i < count; i++) {
426                                         Win32_MIB_TCPROW row = new Win32_MIB_TCPROW ();
427                                         Marshal.PtrToStructure ((IntPtr) (ptr + i * structSize4 + 4), row);
428                                         tab4.Add (row);
429                                 }
430                         }
431
432                         tab6 = new List<Win32_MIB_TCP6ROW> ();
433                         if (Environment.OSVersion.Version.Major >= 6) { // Vista
434                                 int size6 = 0;
435                                 GetTcp6Table (null, ref size6, true); // get size
436                                 byte [] bytes6 = new byte [size6];
437                                 GetTcp6Table (bytes6, ref size6, true); // get list
438
439                                 int structSize6 = Marshal.SizeOf (typeof (Win32_MIB_TCP6ROW));
440
441                                 fixed (byte* ptr = bytes6) {
442                                         int count = Marshal.ReadInt32 ((IntPtr) ptr);
443                                         for (int i = 0; i < count; i++) {
444                                                 Win32_MIB_TCP6ROW row = new Win32_MIB_TCP6ROW ();
445                                                 Marshal.PtrToStructure ((IntPtr) (ptr + i * structSize6 + 4), row);
446                                                 tab6.Add (row);
447                                         }
448                                 }
449                         }
450                 }
451
452                 bool IsListenerState (TcpState state)
453                 {
454                         switch (state) {
455                         case TcpState.SynSent:
456                         case TcpState.Listen:
457                         case TcpState.FinWait1:
458                         case TcpState.FinWait2:
459                         case TcpState.CloseWait:
460                                 return true;
461                         }
462                         return false;
463                 }
464
465                 public override TcpConnectionInformation [] GetActiveTcpConnections ()
466                 {
467                         List<Win32_MIB_TCPROW> tab4 = null;
468                         List<Win32_MIB_TCP6ROW> tab6 = null;
469                         FillTcpTable (out tab4, out tab6);
470                         int size4 = tab4.Count;
471
472                         TcpConnectionInformation [] ret = new TcpConnectionInformation [size4 + tab6.Count];
473                         for (int i = 0; i < size4; i++)
474                                 ret [i] = tab4 [i].TcpInfo;
475                         for (int i = 0; i < tab6.Count; i++)
476                                 ret [size4 + i] = tab6 [i].TcpInfo;
477                         return ret;
478                 }
479
480                 public override IPEndPoint [] GetActiveTcpListeners ()
481                 {
482                         List<Win32_MIB_TCPROW> tab4 = null;
483                         List<Win32_MIB_TCP6ROW> tab6 = null;
484                         FillTcpTable (out tab4, out tab6);
485
486                         List<IPEndPoint> ret = new List<IPEndPoint> ();
487                         for (int i = 0, count = tab4.Count; i < count; i++)
488                                 if (IsListenerState (tab4 [i].State))
489                                         ret.Add (tab4 [i].LocalEndPoint);
490                         for (int i = 0, count = tab6.Count; i < count; i++)
491                                 if (IsListenerState (tab6 [i].State))
492                                         ret.Add (tab6 [i].LocalEndPoint);
493                         return ret.ToArray ();
494                 }
495
496                 public unsafe override IPEndPoint [] GetActiveUdpListeners ()
497                 {
498                         List<IPEndPoint> list = new List<IPEndPoint> ();
499
500                         byte [] bytes4 = null;
501                         int size4 = 0;
502                         GetUdpTable (null, ref size4, true); // get size
503                         bytes4 = new byte [size4];
504                         GetUdpTable (bytes4, ref size4, true); // get list
505
506                         int structSize4 = Marshal.SizeOf (typeof (Win32_MIB_UDPROW));
507
508                         fixed (byte* ptr = bytes4) {
509                                 int count = Marshal.ReadInt32 ((IntPtr) ptr);
510                                 for (int i = 0; i < count; i++) {
511                                         Win32_MIB_UDPROW row = new Win32_MIB_UDPROW ();
512                                         Marshal.PtrToStructure ((IntPtr) (ptr + i * structSize4 + 4), row);
513                                         list.Add (row.LocalEndPoint);
514                                 }
515                         }
516
517                         if (Environment.OSVersion.Version.Major >= 6) { // Vista
518                                 byte [] bytes6 = null;
519                                 int size6 = 0;
520                                 GetUdp6Table (null, ref size6, true); // get size
521                                 bytes6 = new byte [size6];
522                                 GetUdp6Table (bytes6, ref size6, true); // get list
523
524                                 int structSize6 = Marshal.SizeOf (typeof (Win32_MIB_UDP6ROW));
525
526                                 fixed (byte* ptr = bytes6) {
527                                         int count = Marshal.ReadInt32 ((IntPtr) ptr);
528                                         for (int i = 0; i < count; i++) {
529                                                 Win32_MIB_UDP6ROW row = new Win32_MIB_UDP6ROW ();
530                                                 Marshal.PtrToStructure ((IntPtr) (ptr + i * structSize6 + 4), row);
531                                                 list.Add (row.LocalEndPoint);
532                                         }
533                                 }
534                         }
535
536                         return list.ToArray ();
537                 }
538
539                 public override IcmpV4Statistics GetIcmpV4Statistics ()
540                 {
541                         if (!Socket.SupportsIPv4)
542                                 throw new NetworkInformationException ();
543                         Win32_MIBICMPINFO stats;
544                         GetIcmpStatistics (out stats, AF_INET);
545                         return new Win32IcmpV4Statistics (stats);
546                 }
547
548                 public override IcmpV6Statistics GetIcmpV6Statistics ()
549                 {
550                         if (!Socket.OSSupportsIPv6)
551                                 throw new NetworkInformationException ();
552                         Win32_MIB_ICMP_EX stats;
553                         GetIcmpStatisticsEx (out stats, AF_INET6);
554                         return new Win32IcmpV6Statistics (stats);
555                 }
556
557                 public override IPGlobalStatistics GetIPv4GlobalStatistics ()
558                 {
559                         if (!Socket.SupportsIPv4)
560                                 throw new NetworkInformationException ();
561                         Win32_MIB_IPSTATS stats;
562                         GetIpStatisticsEx (out stats, AF_INET);
563                         return new Win32IPGlobalStatistics (stats);
564                 }
565
566                 public override IPGlobalStatistics GetIPv6GlobalStatistics ()
567                 {
568                         if (!Socket.OSSupportsIPv6)
569                                 throw new NetworkInformationException ();
570                         Win32_MIB_IPSTATS stats;
571                         GetIpStatisticsEx (out stats, AF_INET6);
572                         return new Win32IPGlobalStatistics (stats);
573                 }
574
575                 public override TcpStatistics GetTcpIPv4Statistics ()
576                 {
577                         if (!Socket.SupportsIPv4)
578                                 throw new NetworkInformationException ();
579                         Win32_MIB_TCPSTATS stats;
580                         GetTcpStatisticsEx (out stats, AF_INET);
581                         return new Win32TcpStatistics (stats);
582                 }
583
584                 public override TcpStatistics GetTcpIPv6Statistics ()
585                 {
586                         if (!Socket.OSSupportsIPv6)
587                                 throw new NetworkInformationException ();
588                         Win32_MIB_TCPSTATS stats;
589                         GetTcpStatisticsEx (out stats, AF_INET6);
590                         return new Win32TcpStatistics (stats);
591                 }
592
593                 public override UdpStatistics GetUdpIPv4Statistics ()
594                 {
595                         if (!Socket.SupportsIPv4)
596                                 throw new NetworkInformationException ();
597                         Win32_MIB_UDPSTATS stats;
598                         GetUdpStatisticsEx (out stats, AF_INET);
599                         return new Win32UdpStatistics (stats);
600                 }
601
602                 public override UdpStatistics GetUdpIPv6Statistics ()
603                 {
604                         if (!Socket.OSSupportsIPv6)
605                                 throw new NetworkInformationException ();
606                         Win32_MIB_UDPSTATS stats;
607                         GetUdpStatisticsEx (out stats, AF_INET6);
608                         return new Win32UdpStatistics (stats);
609                 }
610
611                 public override string DhcpScopeName {
612                         get { return Win32_FIXED_INFO.Instance.ScopeId; }
613                 }
614
615                 public override string DomainName {
616                         get { return Win32_FIXED_INFO.Instance.DomainName; }
617                 }
618
619                 public override string HostName {
620                         get { return Win32_FIXED_INFO.Instance.HostName; }
621                 }
622
623                 public override bool IsWinsProxy {
624                         get { return Win32_FIXED_INFO.Instance.EnableProxy != 0; }
625                 }
626
627                 public override NetBiosNodeType NodeType {
628                         get { return Win32_FIXED_INFO.Instance.NodeType; }
629                 }
630
631                 // PInvokes
632
633                 [DllImport ("iphlpapi.dll")]
634                 static extern int GetTcpTable (byte [] pTcpTable, ref int pdwSize, bool bOrder);
635
636                 [DllImport ("iphlpapi.dll")]
637                 static extern int GetTcp6Table (byte [] TcpTable, ref int SizePointer, bool Order);
638
639                 [DllImport ("iphlpapi.dll")]
640                 static extern int GetUdpTable (byte [] pUdpTable, ref int pdwSize, bool bOrder);
641
642                 [DllImport ("iphlpapi.dll")]
643                 static extern int GetUdp6Table (byte [] Udp6Table, ref int SizePointer, bool Order);
644
645                 [DllImport ("iphlpapi.dll")]
646                 static extern int GetTcpStatisticsEx (out Win32_MIB_TCPSTATS pStats, int dwFamily);
647
648                 [DllImport ("iphlpapi.dll")]
649                 static extern int GetUdpStatisticsEx (out Win32_MIB_UDPSTATS pStats, int dwFamily);
650
651                 [DllImport ("iphlpapi.dll")]
652                 static extern int GetIcmpStatistics (out Win32_MIBICMPINFO pStats, int dwFamily);
653
654                 [DllImport ("iphlpapi.dll")]
655                 static extern int GetIcmpStatisticsEx (out Win32_MIB_ICMP_EX pStats, int dwFamily);
656
657                 [DllImport ("iphlpapi.dll")]
658                 static extern int GetIpStatisticsEx (out Win32_MIB_IPSTATS pStats, int dwFamily);
659
660                 // Win32 structures
661
662                 [StructLayout (LayoutKind.Explicit)]
663                 struct Win32_IN6_ADDR
664                 {
665                         [FieldOffset (0)]
666                         [MarshalAs ( UnmanagedType.ByValArray, SizeConst = 16)]
667                         public byte [] Bytes;
668                 }
669
670                 [StructLayout (LayoutKind.Sequential)]
671                 class Win32_MIB_TCPROW
672                 {
673                         public TcpState State;
674                         public uint LocalAddr;
675                         public int LocalPort;
676                         public uint RemoteAddr;
677                         public int RemotePort;
678
679                         public IPEndPoint LocalEndPoint {
680                                 get { return new IPEndPoint (LocalAddr, LocalPort); }
681                         }
682
683                         public IPEndPoint RemoteEndPoint {
684                                 get { return new IPEndPoint (RemoteAddr, RemotePort); }
685                         }
686
687                         public TcpConnectionInformation TcpInfo {
688                                 get { return new TcpConnectionInformationImpl (LocalEndPoint, RemoteEndPoint, State); }
689                         }
690                 }
691
692                 [StructLayout (LayoutKind.Sequential)]
693                 class Win32_MIB_TCP6ROW
694                 {
695                         public TcpState State;
696                         public Win32_IN6_ADDR LocalAddr;
697                         public uint LocalScopeId;
698                         public int LocalPort;
699                         public Win32_IN6_ADDR RemoteAddr;
700                         public uint RemoteScopeId;
701                         public int RemotePort;
702
703                         public IPEndPoint LocalEndPoint {
704                                 get { return new IPEndPoint (new IPAddress (LocalAddr.Bytes, LocalScopeId), LocalPort); }
705                         }
706
707                         public IPEndPoint RemoteEndPoint {
708                                 get { return new IPEndPoint (new IPAddress (RemoteAddr.Bytes, RemoteScopeId), RemotePort); }
709                         }
710
711                         public TcpConnectionInformation TcpInfo {
712                                 get { return new TcpConnectionInformationImpl (LocalEndPoint, RemoteEndPoint, State); }
713                         }
714                 }
715
716                 [StructLayout (LayoutKind.Sequential)]
717                 class Win32_MIB_UDPROW
718                 {
719                         public uint LocalAddr;
720                         public int LocalPort;
721
722                         public IPEndPoint LocalEndPoint {
723                                 get { return new IPEndPoint (LocalAddr, LocalPort); }
724                         }
725                 }
726
727                 [StructLayout (LayoutKind.Sequential)]
728                 class Win32_MIB_UDP6ROW
729                 {
730                         public Win32_IN6_ADDR LocalAddr;
731                         public uint LocalScopeId;
732                         public int LocalPort;
733
734                         public IPEndPoint LocalEndPoint {
735                                 get { return new IPEndPoint (new IPAddress (LocalAddr.Bytes, LocalScopeId), LocalPort); }
736                         }
737                 }
738         }
739 }