NetworkInterface.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // System.Net.NetworkInformation.NetworkInterface
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Net;
  33. using System.Runtime.InteropServices;
  34. using System.Text;
  35. namespace System.Net.NetworkInformation {
  36. public abstract class NetworkInterface {
  37. protected NetworkInterface ()
  38. {
  39. }
  40. [MonoTODO ("unimplemented on Non-Windows")]
  41. public static NetworkInterface [] GetAllNetworkInterfaces ()
  42. {
  43. switch (Environment.OSVersion.Platform) {
  44. case PlatformID.Unix:
  45. throw new NotSupportedException ("This platform is not supported");
  46. default:
  47. if (Environment.OSVersion.Version >= new Version (5, 1))
  48. return Win32NetworkInterface2.GetAllNetworkInterfaces ();
  49. throw new NotImplementedException ();
  50. }
  51. }
  52. [MonoTODO]
  53. public static bool GetIsNetworkAvailable ()
  54. {
  55. return true;
  56. }
  57. [MonoTODO]
  58. public static int LoopbackInterfaceIndex {
  59. get { return 0; }
  60. }
  61. public abstract IPInterfaceProperties GetIPProperties ();
  62. public abstract IPv4InterfaceStatistics GetIPv4Statistics ();
  63. public abstract PhysicalAddress GetPhysicalAddress ();
  64. public abstract bool Supports (NetworkInterfaceComponent networkInterfaceComponent);
  65. public abstract string Description { get; }
  66. public abstract string Id { get; }
  67. public abstract bool IsReceiveOnly { get; }
  68. public abstract string Name { get; }
  69. public abstract NetworkInterfaceType NetworkInterfaceType { get; }
  70. public abstract OperationalStatus OperationalStatus { get; }
  71. public abstract long Speed { get; }
  72. public abstract bool SupportsMulticast { get; }
  73. }
  74. class Win32NetworkInterface2 : NetworkInterface
  75. {
  76. [DllImport ("iphlpapi.dll", SetLastError = true)]
  77. static extern int GetNetworkParams (byte [] bytes, ref int size);
  78. [DllImport ("iphlpapi.dll", SetLastError = true)]
  79. static extern int GetAdaptersInfo (byte [] info, ref int size);
  80. [DllImport ("iphlpapi.dll", SetLastError = true)]
  81. static extern int GetAdaptersAddresses (uint family, uint flags, IntPtr reserved, byte [] info, ref int size);
  82. [DllImport ("iphlpapi.dll", SetLastError = true)]
  83. static extern int GetIfEntry (ref Win32_MIB_IFROW row);
  84. static Win32_FIXED_INFO fixed_info;
  85. public static Win32_FIXED_INFO FixedInfo {
  86. get {
  87. if (fixed_info == null)
  88. fixed_info = GetFixedInfo ();
  89. return fixed_info;
  90. }
  91. }
  92. static Win32_FIXED_INFO GetFixedInfo ()
  93. {
  94. int len = 0;
  95. byte [] bytes = null;
  96. GetNetworkParams (null, ref len);
  97. bytes = new byte [len];
  98. GetNetworkParams (bytes, ref len);
  99. Win32_FIXED_INFO info = new Win32_FIXED_INFO ();
  100. unsafe {
  101. fixed (byte* ptr = bytes) {
  102. Marshal.PtrToStructure ((IntPtr) ptr, info);
  103. }
  104. }
  105. return info;
  106. }
  107. public static NetworkInterface [] GetAllNetworkInterfaces ()
  108. {
  109. Win32_IP_ADAPTER_INFO [] ai = GetAdaptersInfo ();
  110. Win32_IP_ADAPTER_ADDRESSES [] aa = GetAdaptersAddresses ();
  111. NetworkInterface [] ret = new NetworkInterface [aa.Length];
  112. for (int i = 0; i < ret.Length; i++)
  113. ret [i] = new Win32NetworkInterface2 (aa [i]);
  114. return ret;
  115. }
  116. public static Win32_IP_ADAPTER_INFO GetAdapterInfoByIndex (int index)
  117. {
  118. foreach (Win32_IP_ADAPTER_INFO info in GetAdaptersInfo ())
  119. if (info.Index == index)
  120. return info;
  121. return null;
  122. }
  123. unsafe static Win32_IP_ADAPTER_INFO [] GetAdaptersInfo ()
  124. {
  125. byte [] bytes = null;
  126. int len = 0;
  127. GetAdaptersInfo (bytes, ref len);
  128. bytes = new byte [len];
  129. int ret = GetAdaptersInfo (bytes, ref len);
  130. if (ret != 0)
  131. throw new NetworkInformationException (ret);
  132. List<Win32_IP_ADAPTER_INFO> l = new List<Win32_IP_ADAPTER_INFO> ();
  133. fixed (byte* ptr = bytes) {
  134. Win32_IP_ADAPTER_INFO info;
  135. for (IntPtr p = (IntPtr) ptr; p != IntPtr.Zero; p = info.Next) {
  136. info = new Win32_IP_ADAPTER_INFO ();
  137. Marshal.PtrToStructure (p, info);
  138. l.Add (info);
  139. }
  140. }
  141. return l.ToArray ();
  142. }
  143. unsafe static Win32_IP_ADAPTER_ADDRESSES [] GetAdaptersAddresses ()
  144. {
  145. byte [] bytes = null;
  146. int len = 0;
  147. GetAdaptersAddresses (0, 0, IntPtr.Zero, bytes, ref len);
  148. bytes = new byte [len];
  149. int ret = GetAdaptersAddresses (0, 0, IntPtr.Zero, bytes, ref len);
  150. if (ret != 0)
  151. throw new NetworkInformationException (ret);
  152. List<Win32_IP_ADAPTER_ADDRESSES> l = new List<Win32_IP_ADAPTER_ADDRESSES> ();
  153. fixed (byte* ptr = bytes) {
  154. Win32_IP_ADAPTER_ADDRESSES info;
  155. for (IntPtr p = (IntPtr) ptr; p != IntPtr.Zero; p = info.Next) {
  156. info = new Win32_IP_ADAPTER_ADDRESSES ();
  157. Marshal.PtrToStructure (p, info);
  158. l.Add (info);
  159. }
  160. }
  161. return l.ToArray ();
  162. }
  163. Win32_IP_ADAPTER_ADDRESSES addr;
  164. Win32_MIB_IFROW mib4, mib6;
  165. Win32IPv4InterfaceStatistics ip4stats;
  166. IPInterfaceProperties ip_if_props;
  167. Win32NetworkInterface2 (Win32_IP_ADAPTER_ADDRESSES addr)
  168. {
  169. this.addr = addr;
  170. mib4 = default (Win32_MIB_IFROW);
  171. mib4.Index = addr.Alignment.IfIndex;
  172. if (GetIfEntry (ref mib4) != 0)
  173. mib4.Index = -1; // unavailable;
  174. mib6 = default (Win32_MIB_IFROW);
  175. mib6.Index = addr.Ipv6IfIndex;
  176. if (GetIfEntry (ref mib6) != 0)
  177. mib6.Index = -1; // unavailable;
  178. ip4stats = new Win32IPv4InterfaceStatistics (mib4);
  179. ip_if_props = new Win32IPInterfaceProperties2 (addr, mib4, mib6);
  180. }
  181. public override IPInterfaceProperties GetIPProperties ()
  182. {
  183. return ip_if_props;
  184. }
  185. public override IPv4InterfaceStatistics GetIPv4Statistics ()
  186. {
  187. return ip4stats;
  188. }
  189. public override PhysicalAddress GetPhysicalAddress ()
  190. {
  191. byte [] bytes = new byte [addr.PhysicalAddressLength];
  192. Array.Copy (addr.PhysicalAddress, 0, bytes, 0, bytes.Length);
  193. return new PhysicalAddress (bytes);
  194. }
  195. public override bool Supports (NetworkInterfaceComponent networkInterfaceComponent)
  196. {
  197. switch (networkInterfaceComponent) {
  198. case NetworkInterfaceComponent.IPv4:
  199. return mib4.Index >= 0;
  200. case NetworkInterfaceComponent.IPv6:
  201. return mib6.Index >= 0;
  202. }
  203. return false;
  204. }
  205. public override string Description {
  206. get { return addr.Description; }
  207. }
  208. public override string Id {
  209. get { return addr.AdapterName; }
  210. }
  211. public override bool IsReceiveOnly {
  212. get { return addr.IsReceiveOnly; }
  213. }
  214. public override string Name {
  215. get { return addr.FriendlyName; }
  216. }
  217. public override NetworkInterfaceType NetworkInterfaceType {
  218. get { return addr.IfType; }
  219. }
  220. public override OperationalStatus OperationalStatus {
  221. get { return addr.OperStatus; }
  222. }
  223. public override long Speed {
  224. get { return mib6.Index >= 0 ? mib6.Speed : mib4.Speed; }
  225. }
  226. public override bool SupportsMulticast {
  227. get { return !addr.NoMulticast; }
  228. }
  229. }
  230. }
  231. #endif