2 // System.Net.NetworkInformation.IPGlobalProperties
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
6 // Atsushi Enomoto (atsushi@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections.Generic;
32 using System.Collections.Specialized;
33 using System.Globalization;
35 using System.Net.Sockets;
36 using System.Runtime.InteropServices;
39 namespace System.Net.NetworkInformation {
40 abstract class CommonUnixIPGlobalProperties : IPGlobalProperties
43 static extern int gethostname ([MarshalAs (UnmanagedType.LPArray, SizeParamIndex = 1)] byte [] name, int len);
46 static extern int getdomainname ([MarshalAs (UnmanagedType.LPArray, SizeParamIndex = 1)] byte [] name, int len);
48 public override string DhcpScopeName {
49 get { return String.Empty; }
52 public override string DomainName {
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);
62 public override string HostName {
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);
72 public override bool IsWinsProxy {
73 get { return false; } // no WINS
76 public override NetBiosNodeType NodeType {
77 get { return NetBiosNodeType.Unknown; } // no NetBios
81 class UnixIPGlobalProperties : CommonUnixIPGlobalProperties
83 public override TcpConnectionInformation [] GetActiveTcpConnections ()
85 throw new NotImplementedException ();
88 public override IPEndPoint [] GetActiveTcpListeners ()
90 throw new NotImplementedException ();
93 public override IPEndPoint [] GetActiveUdpListeners ()
95 throw new NotImplementedException ();
98 public override IcmpV4Statistics GetIcmpV4Statistics ()
100 throw new NotImplementedException ();
103 public override IcmpV6Statistics GetIcmpV6Statistics ()
105 throw new NotImplementedException ();
108 public override IPGlobalStatistics GetIPv4GlobalStatistics ()
110 throw new NotImplementedException ();
113 public override IPGlobalStatistics GetIPv6GlobalStatistics ()
115 throw new NotImplementedException ();
118 public override TcpStatistics GetTcpIPv4Statistics ()
120 throw new NotImplementedException ();
123 public override TcpStatistics GetTcpIPv6Statistics ()
125 throw new NotImplementedException ();
128 public override UdpStatistics GetUdpIPv4Statistics ()
130 throw new NotImplementedException ();
133 public override UdpStatistics GetUdpIPv6Statistics ()
135 throw new NotImplementedException ();
140 sealed class AndroidIPGlobalProperties : UnixIPGlobalProperties
142 public override string DomainName {
150 // It expects /proc/net/snmp (or /usr/compat/linux/proc/net/snmp),
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
156 public const string ProcDir = "/proc";
157 public const string CompatProcDir = "/usr/compat/linux/proc";
159 public readonly string StatisticsFile, StatisticsFileIPv6, TcpFile, Tcp6File, UdpFile, Udp6File;
161 public MibIPGlobalProperties (string procDir)
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");
171 StringDictionary GetProperties4 (string item)
173 string file = StatisticsFile;
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;
182 if (String.IsNullOrEmpty (s))
184 if (s.Length <= head.Length || String.CompareOrdinal (s, 0, head, 0, head.Length) != 0)
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));
192 values = s.Substring (head.Length).Split (' ');
195 } while (!sr.EndOfStream);
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];
208 StringDictionary GetProperties6 (string item)
210 if (!File.Exists (StatisticsFileIPv6))
211 throw new NetworkInformationException ();
213 string file = StatisticsFileIPv6;
216 using (StreamReader sr = new StreamReader (file, Encoding.ASCII)) {
217 StringDictionary dic = new StringDictionary ();
218 string s = String.Empty;
221 if (String.IsNullOrEmpty (s))
223 if (s.Length <= head.Length || String.CompareOrdinal (s, 0, head, 0, head.Length) != 0)
225 int idx = s.IndexOfAny (wsChars, head.Length);
227 throw CreateException (file, null);
228 dic [s.Substring (head.Length, idx - head.Length)] = s.Substring (idx + 1).Trim (wsChars);
229 } while (!sr.EndOfStream);
235 static readonly char [] wsChars = new char [] {' ', '\t'};
237 Exception CreateException (string file, string msg)
239 return new InvalidOperationException (String.Format ("Unsupported (unexpected) '{0}' file format. ", file) + msg);
241 IPEndPoint [] GetLocalAddresses (List<string []> list)
243 IPEndPoint [] ret = new IPEndPoint [list.Count];
244 for (int i = 0; i < ret.Length; i++)
245 ret [i] = ToEndpoint (list [i] [1]);
249 IPEndPoint ToEndpoint (string s)
251 int idx = s.IndexOf (':');
252 int port = int.Parse (s.Substring (idx + 1), NumberStyles.HexNumber);
254 return new IPEndPoint (long.Parse (s.Substring (0, idx), NumberStyles.HexNumber), port);
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);
263 void GetRows (string file, List<string []> list)
265 if (!File.Exists (file))
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);
272 throw CreateException (file, null);
278 public override TcpConnectionInformation [] GetActiveTcpConnections ()
280 List<string []> list = new List<string []> ();
281 GetRows (TcpFile, list);
282 GetRows (Tcp6File, list);
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);
295 public override IPEndPoint [] GetActiveTcpListeners ()
297 List<string []> list = new List<string []> ();
298 GetRows (TcpFile, list);
299 GetRows (Tcp6File, list);
300 return GetLocalAddresses (list);
303 public override IPEndPoint [] GetActiveUdpListeners ()
305 List<string []> list = new List<string []> ();
306 GetRows (UdpFile, list);
307 GetRows (Udp6File, list);
308 return GetLocalAddresses (list);
311 public override IcmpV4Statistics GetIcmpV4Statistics ()
313 return new MibIcmpV4Statistics (GetProperties4 ("Icmp"));
316 public override IcmpV6Statistics GetIcmpV6Statistics ()
318 return new MibIcmpV6Statistics (GetProperties6 ("Icmp6"));
321 public override IPGlobalStatistics GetIPv4GlobalStatistics ()
323 return new MibIPGlobalStatistics (GetProperties4 ("Ip"));
326 public override IPGlobalStatistics GetIPv6GlobalStatistics ()
328 return new MibIPGlobalStatistics (GetProperties6 ("Ip6"));
331 public override TcpStatistics GetTcpIPv4Statistics ()
333 return new MibTcpStatistics (GetProperties4 ("Tcp"));
336 public override TcpStatistics GetTcpIPv6Statistics ()
338 // There is no TCP info in /proc/net/snmp,
339 // so it is shared with IPv4 info.
340 return new MibTcpStatistics (GetProperties4 ("Tcp"));
343 public override UdpStatistics GetUdpIPv4Statistics ()
345 return new MibUdpStatistics (GetProperties4 ("Udp"));
348 public override UdpStatistics GetUdpIPv6Statistics ()
350 return new MibUdpStatistics (GetProperties6 ("Udp6"));
355 class Win32IPGlobalProperties : IPGlobalProperties
357 public const int AF_INET = 2;
358 public const int AF_INET6 = 23;
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)
364 tab4 = new List<Win32_MIB_TCPROW> ();
366 GetTcpTable (null, ref size4, true); // get size
367 byte [] bytes4 = new byte [size4];
368 GetTcpTable (bytes4, ref size4, true); // get list
370 int structSize4 = Marshal.SizeOf (typeof (Win32_MIB_TCPROW));
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);
381 tab6 = new List<Win32_MIB_TCP6ROW> ();
382 if (Environment.OSVersion.Version.Major >= 6) { // Vista
384 GetTcp6Table (null, ref size6, true); // get size
385 byte [] bytes6 = new byte [size6];
386 GetTcp6Table (bytes6, ref size6, true); // get list
388 int structSize6 = Marshal.SizeOf (typeof (Win32_MIB_TCP6ROW));
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);
401 bool IsListenerState (TcpState state)
404 case TcpState.SynSent:
405 case TcpState.Listen:
406 case TcpState.FinWait1:
407 case TcpState.FinWait2:
408 case TcpState.CloseWait:
414 public override TcpConnectionInformation [] GetActiveTcpConnections ()
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;
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;
429 public override IPEndPoint [] GetActiveTcpListeners ()
431 List<Win32_MIB_TCPROW> tab4 = null;
432 List<Win32_MIB_TCP6ROW> tab6 = null;
433 FillTcpTable (out tab4, out tab6);
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 ();
445 public unsafe override IPEndPoint [] GetActiveUdpListeners ()
447 List<IPEndPoint> list = new List<IPEndPoint> ();
449 byte [] bytes4 = null;
451 GetUdpTable (null, ref size4, true); // get size
452 bytes4 = new byte [size4];
453 GetUdpTable (bytes4, ref size4, true); // get list
455 int structSize4 = Marshal.SizeOf (typeof (Win32_MIB_UDPROW));
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);
466 if (Environment.OSVersion.Version.Major >= 6) { // Vista
467 byte [] bytes6 = null;
469 GetUdp6Table (null, ref size6, true); // get size
470 bytes6 = new byte [size6];
471 GetUdp6Table (bytes6, ref size6, true); // get list
473 int structSize6 = Marshal.SizeOf (typeof (Win32_MIB_UDP6ROW));
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);
485 return list.ToArray ();
488 public override IcmpV4Statistics GetIcmpV4Statistics ()
490 if (!Socket.SupportsIPv4)
491 throw new NetworkInformationException ();
492 Win32_MIBICMPINFO stats;
493 GetIcmpStatistics (out stats, AF_INET);
494 return new Win32IcmpV4Statistics (stats);
497 public override IcmpV6Statistics GetIcmpV6Statistics ()
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);
506 public override IPGlobalStatistics GetIPv4GlobalStatistics ()
508 if (!Socket.SupportsIPv4)
509 throw new NetworkInformationException ();
510 Win32_MIB_IPSTATS stats;
511 GetIpStatisticsEx (out stats, AF_INET);
512 return new Win32IPGlobalStatistics (stats);
515 public override IPGlobalStatistics GetIPv6GlobalStatistics ()
517 if (!Socket.OSSupportsIPv6)
518 throw new NetworkInformationException ();
519 Win32_MIB_IPSTATS stats;
520 GetIpStatisticsEx (out stats, AF_INET6);
521 return new Win32IPGlobalStatistics (stats);
524 public override TcpStatistics GetTcpIPv4Statistics ()
526 if (!Socket.SupportsIPv4)
527 throw new NetworkInformationException ();
528 Win32_MIB_TCPSTATS stats;
529 GetTcpStatisticsEx (out stats, AF_INET);
530 return new Win32TcpStatistics (stats);
533 public override TcpStatistics GetTcpIPv6Statistics ()
535 if (!Socket.OSSupportsIPv6)
536 throw new NetworkInformationException ();
537 Win32_MIB_TCPSTATS stats;
538 GetTcpStatisticsEx (out stats, AF_INET6);
539 return new Win32TcpStatistics (stats);
542 public override UdpStatistics GetUdpIPv4Statistics ()
544 if (!Socket.SupportsIPv4)
545 throw new NetworkInformationException ();
546 Win32_MIB_UDPSTATS stats;
547 GetUdpStatisticsEx (out stats, AF_INET);
548 return new Win32UdpStatistics (stats);
551 public override UdpStatistics GetUdpIPv6Statistics ()
553 if (!Socket.OSSupportsIPv6)
554 throw new NetworkInformationException ();
555 Win32_MIB_UDPSTATS stats;
556 GetUdpStatisticsEx (out stats, AF_INET6);
557 return new Win32UdpStatistics (stats);
560 public override string DhcpScopeName {
561 get { return Win32_FIXED_INFO.Instance.ScopeId; }
564 public override string DomainName {
565 get { return Win32_FIXED_INFO.Instance.DomainName; }
568 public override string HostName {
569 get { return Win32_FIXED_INFO.Instance.HostName; }
572 public override bool IsWinsProxy {
573 get { return Win32_FIXED_INFO.Instance.EnableProxy != 0; }
576 public override NetBiosNodeType NodeType {
577 get { return Win32_FIXED_INFO.Instance.NodeType; }
582 [DllImport ("iphlpapi.dll")]
583 static extern int GetTcpTable (byte [] pTcpTable, ref int pdwSize, bool bOrder);
585 [DllImport ("iphlpapi.dll")]
586 static extern int GetTcp6Table (byte [] TcpTable, ref int SizePointer, bool Order);
588 [DllImport ("iphlpapi.dll")]
589 static extern int GetUdpTable (byte [] pUdpTable, ref int pdwSize, bool bOrder);
591 [DllImport ("iphlpapi.dll")]
592 static extern int GetUdp6Table (byte [] Udp6Table, ref int SizePointer, bool Order);
594 [DllImport ("iphlpapi.dll")]
595 static extern int GetTcpStatisticsEx (out Win32_MIB_TCPSTATS pStats, int dwFamily);
597 [DllImport ("iphlpapi.dll")]
598 static extern int GetUdpStatisticsEx (out Win32_MIB_UDPSTATS pStats, int dwFamily);
600 [DllImport ("iphlpapi.dll")]
601 static extern int GetIcmpStatistics (out Win32_MIBICMPINFO pStats, int dwFamily);
603 [DllImport ("iphlpapi.dll")]
604 static extern int GetIcmpStatisticsEx (out Win32_MIB_ICMP_EX pStats, int dwFamily);
606 [DllImport ("iphlpapi.dll")]
607 static extern int GetIpStatisticsEx (out Win32_MIB_IPSTATS pStats, int dwFamily);
611 [StructLayout (LayoutKind.Explicit)]
612 struct Win32_IN6_ADDR
615 [MarshalAs ( UnmanagedType.ByValArray, SizeConst = 16)]
616 public byte [] Bytes;
619 [StructLayout (LayoutKind.Sequential)]
620 class Win32_MIB_TCPROW
622 public TcpState State;
623 public uint LocalAddr;
624 public int LocalPort;
625 public uint RemoteAddr;
626 public int RemotePort;
628 public IPEndPoint LocalEndPoint {
629 get { return new IPEndPoint (LocalAddr, LocalPort); }
632 public IPEndPoint RemoteEndPoint {
633 get { return new IPEndPoint (RemoteAddr, RemotePort); }
636 public TcpConnectionInformation TcpInfo {
637 get { return new SystemTcpConnectionInformation (LocalEndPoint, RemoteEndPoint, State); }
641 [StructLayout (LayoutKind.Sequential)]
642 class Win32_MIB_TCP6ROW
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;
652 public IPEndPoint LocalEndPoint {
653 get { return new IPEndPoint (new IPAddress (LocalAddr.Bytes, LocalScopeId), LocalPort); }
656 public IPEndPoint RemoteEndPoint {
657 get { return new IPEndPoint (new IPAddress (RemoteAddr.Bytes, RemoteScopeId), RemotePort); }
660 public TcpConnectionInformation TcpInfo {
661 get { return new SystemTcpConnectionInformation (LocalEndPoint, RemoteEndPoint, State); }
665 [StructLayout (LayoutKind.Sequential)]
666 class Win32_MIB_UDPROW
668 public uint LocalAddr;
669 public int LocalPort;
671 public IPEndPoint LocalEndPoint {
672 get { return new IPEndPoint (LocalAddr, LocalPort); }
676 [StructLayout (LayoutKind.Sequential)]
677 class Win32_MIB_UDP6ROW
679 public Win32_IN6_ADDR LocalAddr;
680 public uint LocalScopeId;
681 public int LocalPort;
683 public IPEndPoint LocalEndPoint {
684 get { return new IPEndPoint (new IPAddress (LocalAddr.Bytes, LocalScopeId), LocalPort); }