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