PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System/System.Net.NetworkInformation/IPInterfaceProperties.cs

https://github.com/pruiz/mono
C# | 484 lines | 387 code | 56 blank | 41 comment | 48 complexity | 1488aeb2a0b1f9afd3dcdce5855898a1 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // System.Net.NetworkInformation.IPInterfaceProperties
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier (gonzalo@novell.com)
  6. // Atsushi Enomoto (atsushi@ximian.com)
  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. using System.Collections.Generic;
  30. using System.Globalization;
  31. using System.IO;
  32. using System.Net.Sockets;
  33. using System.Runtime.CompilerServices;
  34. using System.Text.RegularExpressions;
  35. using System.Runtime.InteropServices;
  36. namespace System.Net.NetworkInformation {
  37. abstract class UnixIPInterfaceProperties : IPInterfaceProperties
  38. {
  39. protected IPv4InterfaceProperties ipv4iface_properties;
  40. protected UnixNetworkInterface iface;
  41. List <IPAddress> addresses;
  42. IPAddressCollection dns_servers;
  43. public UnixIPInterfaceProperties (UnixNetworkInterface iface, List <IPAddress> addresses)
  44. {
  45. this.iface = iface;
  46. this.addresses = addresses;
  47. }
  48. public override IPv6InterfaceProperties GetIPv6Properties ()
  49. {
  50. throw new NotImplementedException ();
  51. }
  52. #if MONODROID
  53. [DllImport ("__Internal")]
  54. static extern int _monodroid_get_dns_servers (out IntPtr dns_servers_array);
  55. void GetDNSServersFromOS ()
  56. {
  57. IntPtr dsa;
  58. int len = _monodroid_get_dns_servers (out dsa);
  59. if (len <= 0)
  60. return;
  61. var servers = new IntPtr [len];
  62. Marshal.Copy (dsa, servers, 0, len);
  63. dns_servers = new IPAddressCollection ();
  64. foreach (IntPtr s in servers) {
  65. string server_ip = Marshal.PtrToStringAnsi (s);
  66. Marshal.FreeHGlobal (s);
  67. IPAddress addr;
  68. if (!IPAddress.TryParse (server_ip, out addr))
  69. continue;
  70. dns_servers.InternalAdd (addr);
  71. }
  72. Marshal.FreeHGlobal (dsa);
  73. }
  74. #else
  75. static Regex ns = new Regex (@"\s*nameserver\s+(?<address>.*)");
  76. static Regex search = new Regex (@"\s*search\s+(?<domain>.*)");
  77. string dns_suffix;
  78. DateTime last_parse;
  79. void ParseResolvConf ()
  80. {
  81. try {
  82. DateTime wt = File.GetLastWriteTime ("/etc/resolv.conf");
  83. if (wt <= last_parse)
  84. return;
  85. last_parse = wt;
  86. dns_suffix = "";
  87. dns_servers = new IPAddressCollection ();
  88. using (StreamReader reader = new StreamReader ("/etc/resolv.conf")) {
  89. string str;
  90. string line;
  91. while ((line = reader.ReadLine ()) != null) {
  92. line = line.Trim ();
  93. if (line.Length == 0 || line [0] == '#')
  94. continue;
  95. Match match = ns.Match (line);
  96. if (match.Success) {
  97. try {
  98. str = match.Groups ["address"].Value;
  99. str = str.Trim ();
  100. dns_servers.InternalAdd (IPAddress.Parse (str));
  101. } catch {
  102. }
  103. } else {
  104. match = search.Match (line);
  105. if (match.Success) {
  106. str = match.Groups ["domain"].Value;
  107. string [] parts = str.Split (',');
  108. dns_suffix = parts [0].Trim ();
  109. }
  110. }
  111. }
  112. }
  113. } catch {
  114. }
  115. }
  116. #endif
  117. public override IPAddressInformationCollection AnycastAddresses {
  118. get {
  119. var c = new IPAddressInformationCollection ();
  120. foreach (IPAddress address in addresses) {
  121. c.InternalAdd (new SystemIPAddressInformation (address, false, false));
  122. }
  123. return c;
  124. }
  125. }
  126. [MonoTODO ("Always returns an empty collection.")]
  127. public override IPAddressCollection DhcpServerAddresses {
  128. get {
  129. // There are lots of different DHCP clients
  130. // that all store their configuration differently.
  131. // I'm not sure what to do here.
  132. IPAddressCollection coll = new IPAddressCollection ();
  133. return coll;
  134. }
  135. }
  136. public override IPAddressCollection DnsAddresses {
  137. get {
  138. #if MONODROID
  139. GetDNSServersFromOS ();
  140. #else
  141. ParseResolvConf ();
  142. #endif
  143. return dns_servers;
  144. }
  145. }
  146. public override string DnsSuffix {
  147. get {
  148. #if MONODROID
  149. return String.Empty;
  150. #else
  151. ParseResolvConf ();
  152. return dns_suffix;
  153. #endif
  154. }
  155. }
  156. [MonoTODO ("Always returns true")]
  157. public override bool IsDnsEnabled {
  158. get {
  159. return true;
  160. }
  161. }
  162. [MonoTODO ("Always returns false")]
  163. public override bool IsDynamicDnsEnabled {
  164. get {
  165. return false;
  166. }
  167. }
  168. public override MulticastIPAddressInformationCollection MulticastAddresses {
  169. get {
  170. var multicastAddresses = new MulticastIPAddressInformationCollection ();
  171. foreach (IPAddress address in addresses) {
  172. byte[] addressBytes = address.GetAddressBytes ();
  173. if (addressBytes[0] >= 224 && addressBytes[0] <= 239) {
  174. multicastAddresses.InternalAdd (new SystemMulticastIPAddressInformation (new SystemIPAddressInformation (address, true, false)));
  175. }
  176. }
  177. return multicastAddresses;
  178. }
  179. }
  180. public override UnicastIPAddressInformationCollection UnicastAddresses {
  181. get {
  182. var unicastAddresses = new UnicastIPAddressInformationCollection ();
  183. foreach (IPAddress address in addresses) {
  184. switch (address.AddressFamily) {
  185. case AddressFamily.InterNetwork:
  186. byte top = address.GetAddressBytes () [0];
  187. if (top >= 224 && top <= 239)
  188. continue;
  189. unicastAddresses.InternalAdd (new LinuxUnicastIPAddressInformation (address));
  190. break;
  191. case AddressFamily.InterNetworkV6:
  192. if (address.IsIPv6Multicast)
  193. continue;
  194. unicastAddresses.InternalAdd (new LinuxUnicastIPAddressInformation (address));
  195. break;
  196. }
  197. }
  198. return unicastAddresses;
  199. }
  200. }
  201. [MonoTODO ("Always returns an empty collection.")]
  202. public override IPAddressCollection WinsServersAddresses {
  203. get {
  204. // I do SUPPOSE we could scrape /etc/samba/smb.conf, but.. yeesh.
  205. return new IPAddressCollection ();
  206. }
  207. }
  208. }
  209. class LinuxIPInterfaceProperties : UnixIPInterfaceProperties
  210. {
  211. public LinuxIPInterfaceProperties (LinuxNetworkInterface iface, List <IPAddress> addresses)
  212. : base (iface, addresses)
  213. {
  214. }
  215. public override IPv4InterfaceProperties GetIPv4Properties ()
  216. {
  217. if (ipv4iface_properties == null)
  218. ipv4iface_properties = new LinuxIPv4InterfaceProperties (iface as LinuxNetworkInterface);
  219. return ipv4iface_properties;
  220. }
  221. IPAddressCollection ParseRouteInfo (string iface)
  222. {
  223. var col = new IPAddressCollection ();
  224. try {
  225. using (StreamReader reader = new StreamReader ("/proc/net/route")) {
  226. string line;
  227. reader.ReadLine (); // Ignore first line
  228. while ((line = reader.ReadLine ()) != null) {
  229. line = line.Trim ();
  230. if (line.Length == 0)
  231. continue;
  232. string [] parts = line.Split ('\t');
  233. if (parts.Length < 3)
  234. continue;
  235. string gw_address = parts [2].Trim ();
  236. byte [] ipbytes = new byte [4];
  237. if (gw_address.Length == 8 && iface.Equals (parts [0], StringComparison.OrdinalIgnoreCase)) {
  238. for (int i = 0; i < 4; i++) {
  239. if (!Byte.TryParse (gw_address.Substring (i * 2, 2), NumberStyles.HexNumber, null, out ipbytes [3 - i]))
  240. continue;
  241. }
  242. IPAddress ip = new IPAddress (ipbytes);
  243. if (!ip.Equals (IPAddress.Any) && !col.Contains (ip))
  244. col.InternalAdd (ip);
  245. }
  246. }
  247. }
  248. } catch {
  249. }
  250. return col;
  251. }
  252. public override GatewayIPAddressInformationCollection GatewayAddresses {
  253. get {
  254. return SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection (ParseRouteInfo (this.iface.Name.ToString()));
  255. }
  256. }
  257. }
  258. class MacOsIPInterfaceProperties : UnixIPInterfaceProperties
  259. {
  260. public MacOsIPInterfaceProperties (MacOsNetworkInterface iface, List <IPAddress> addresses)
  261. : base (iface, addresses)
  262. {
  263. }
  264. public override IPv4InterfaceProperties GetIPv4Properties ()
  265. {
  266. if (ipv4iface_properties == null)
  267. ipv4iface_properties = new MacOsIPv4InterfaceProperties (iface as MacOsNetworkInterface);
  268. return ipv4iface_properties;
  269. }
  270. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  271. private extern static bool ParseRouteInfo_internal(string iface, out string[] gw_addr_list);
  272. public override GatewayIPAddressInformationCollection GatewayAddresses {
  273. get {
  274. var gateways = new IPAddressCollection ();
  275. string[] gw_addrlist;
  276. if (!ParseRouteInfo_internal (this.iface.Name.ToString(), out gw_addrlist))
  277. return new GatewayIPAddressInformationCollection ();
  278. for(int i=0; i<gw_addrlist.Length; i++) {
  279. try {
  280. IPAddress ip = IPAddress.Parse(gw_addrlist[i]);
  281. if (!ip.Equals (IPAddress.Any) && !gateways.Contains (ip))
  282. gateways.InternalAdd (ip);
  283. } catch (ArgumentNullException) {
  284. /* Ignore this, as the
  285. * internal call might have
  286. * left some blank entries at
  287. * the end of the array
  288. */
  289. }
  290. }
  291. return SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection (gateways);
  292. }
  293. }
  294. }
  295. #if WIN_PLATFORM
  296. class Win32IPInterfaceProperties2 : IPInterfaceProperties
  297. {
  298. readonly Win32_IP_ADAPTER_ADDRESSES addr;
  299. readonly Win32_MIB_IFROW mib4, mib6;
  300. public Win32IPInterfaceProperties2 (Win32_IP_ADAPTER_ADDRESSES addr, Win32_MIB_IFROW mib4, Win32_MIB_IFROW mib6)
  301. {
  302. this.addr = addr;
  303. this.mib4 = mib4;
  304. this.mib6 = mib6;
  305. }
  306. public override IPv4InterfaceProperties GetIPv4Properties ()
  307. {
  308. Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
  309. return new Win32IPv4InterfaceProperties (v4info, mib4);
  310. }
  311. public override IPv6InterfaceProperties GetIPv6Properties ()
  312. {
  313. Win32_IP_ADAPTER_INFO v6info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib6.Index);
  314. return new Win32IPv6InterfaceProperties (mib6);
  315. }
  316. public override IPAddressInformationCollection AnycastAddresses {
  317. get { return Win32FromAnycast (addr.FirstAnycastAddress); }
  318. }
  319. static IPAddressInformationCollection Win32FromAnycast (IntPtr ptr)
  320. {
  321. var c = new IPAddressInformationCollection ();
  322. Win32_IP_ADAPTER_ANYCAST_ADDRESS a;
  323. for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
  324. a = (Win32_IP_ADAPTER_ANYCAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_ANYCAST_ADDRESS));
  325. c.InternalAdd (new SystemIPAddressInformation (
  326. a.Address.GetIPAddress (),
  327. a.LengthFlags.IsDnsEligible,
  328. a.LengthFlags.IsTransient));
  329. }
  330. return c;
  331. }
  332. public override IPAddressCollection DhcpServerAddresses {
  333. get {
  334. Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
  335. // FIXME: should ipv6 DhcpServer be considered?
  336. try {
  337. return new Win32IPAddressCollection (v4info.DhcpServer);
  338. } catch (IndexOutOfRangeException) {
  339. return Win32IPAddressCollection.Empty;
  340. }
  341. }
  342. }
  343. public override IPAddressCollection DnsAddresses {
  344. get { return Win32IPAddressCollection.FromDnsServer (addr.FirstDnsServerAddress); }
  345. }
  346. public override string DnsSuffix {
  347. get { return addr.DnsSuffix; }
  348. }
  349. public override GatewayIPAddressInformationCollection GatewayAddresses {
  350. get {
  351. var col = new GatewayIPAddressInformationCollection ();
  352. try {
  353. Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
  354. // FIXME: should ipv6 DhcpServer be considered?
  355. var a = v4info.GatewayList;
  356. if (!String.IsNullOrEmpty (a.IpAddress)) {
  357. col.InternalAdd(new SystemGatewayIPAddressInformation(IPAddress.Parse (a.IpAddress)));
  358. AddSubsequently (a.Next, col);
  359. }
  360. } catch (IndexOutOfRangeException) {}
  361. return col;
  362. }
  363. }
  364. static void AddSubsequently (IntPtr head, GatewayIPAddressInformationCollection col)
  365. {
  366. Win32_IP_ADDR_STRING a;
  367. for (IntPtr p = head; p != IntPtr.Zero; p = a.Next) {
  368. a = (Win32_IP_ADDR_STRING) Marshal.PtrToStructure (p, typeof (Win32_IP_ADDR_STRING));
  369. col.InternalAdd (new SystemGatewayIPAddressInformation (IPAddress.Parse (a.IpAddress)));
  370. }
  371. }
  372. public override bool IsDnsEnabled {
  373. get { return Win32NetworkInterface.FixedInfo.EnableDns != 0; }
  374. }
  375. public override bool IsDynamicDnsEnabled {
  376. get { return addr.DdnsEnabled; }
  377. }
  378. public override MulticastIPAddressInformationCollection MulticastAddresses {
  379. get { return Win32FromMulticast (addr.FirstMulticastAddress); }
  380. }
  381. static MulticastIPAddressInformationCollection Win32FromMulticast (IntPtr ptr)
  382. {
  383. var c = new MulticastIPAddressInformationCollection ();
  384. Win32_IP_ADAPTER_MULTICAST_ADDRESS a;
  385. for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
  386. a = (Win32_IP_ADAPTER_MULTICAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_MULTICAST_ADDRESS));
  387. c.InternalAdd (new SystemMulticastIPAddressInformation (new SystemIPAddressInformation (
  388. a.Address.GetIPAddress (),
  389. a.LengthFlags.IsDnsEligible,
  390. a.LengthFlags.IsTransient)));
  391. }
  392. return c;
  393. }
  394. public override UnicastIPAddressInformationCollection UnicastAddresses {
  395. get {
  396. try {
  397. Win32_IP_ADAPTER_INFO ai = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
  398. // FIXME: should ipv6 DhcpServer be considered?
  399. return Win32FromUnicast (addr.FirstUnicastAddress);
  400. } catch (IndexOutOfRangeException) {
  401. return new UnicastIPAddressInformationCollection ();
  402. }
  403. }
  404. }
  405. static UnicastIPAddressInformationCollection Win32FromUnicast (IntPtr ptr)
  406. {
  407. UnicastIPAddressInformationCollection c = new UnicastIPAddressInformationCollection ();
  408. Win32_IP_ADAPTER_UNICAST_ADDRESS a;
  409. for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
  410. a = (Win32_IP_ADAPTER_UNICAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_UNICAST_ADDRESS));
  411. c.InternalAdd (new Win32UnicastIPAddressInformation (a));
  412. }
  413. return c;
  414. }
  415. public override IPAddressCollection WinsServersAddresses {
  416. get {
  417. try {
  418. Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
  419. // FIXME: should ipv6 DhcpServer be considered?
  420. return new Win32IPAddressCollection (v4info.PrimaryWinsServer, v4info.SecondaryWinsServer);
  421. } catch (IndexOutOfRangeException) {
  422. return Win32IPAddressCollection.Empty;
  423. }
  424. }
  425. }
  426. }
  427. #endif
  428. }