PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/grendello/mono
C# | 226 lines | 175 code | 19 blank | 32 comment | 18 complexity | 467d12fcd2103efd6217244b836f71f1 MD5 | raw file
  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.Net.Sockets;
  31. using System.IO;
  32. using System.Text.RegularExpressions;
  33. using System.Runtime.InteropServices;
  34. namespace System.Net.NetworkInformation {
  35. abstract class UnixIPInterfaceProperties : IPInterfaceProperties
  36. {
  37. protected IPv4InterfaceProperties ipv4iface_properties;
  38. protected UnixNetworkInterface iface;
  39. List <IPAddress> addresses;
  40. IPAddressCollection dns_servers;
  41. public UnixIPInterfaceProperties (UnixNetworkInterface iface, List <IPAddress> addresses)
  42. {
  43. this.iface = iface;
  44. this.addresses = addresses;
  45. }
  46. public override IPv6InterfaceProperties GetIPv6Properties ()
  47. {
  48. throw new NotImplementedException ();
  49. }
  50. #if MONODROID
  51. [DllImport ("__Internal")]
  52. static extern int _monodroid_get_dns_servers (out IntPtr dns_servers_array);
  53. void GetDNSServersFromOS ()
  54. {
  55. IntPtr dsa;
  56. int len = _monodroid_get_dns_servers (out dsa);
  57. if (len <= 0)
  58. return;
  59. var servers = new IntPtr [len];
  60. Marshal.Copy (dsa, servers, 0, len);
  61. dns_servers = new IPAddressCollection ();
  62. foreach (IntPtr s in servers) {
  63. string server_ip = Marshal.PtrToStringAnsi (s);
  64. Marshal.FreeHGlobal (s);
  65. IPAddress addr;
  66. if (!IPAddress.TryParse (server_ip, out addr))
  67. continue;
  68. dns_servers.InternalAdd (addr);
  69. }
  70. Marshal.FreeHGlobal (dsa);
  71. }
  72. #else
  73. static Regex ns = new Regex (@"\s*nameserver\s+(?<address>.*)");
  74. static Regex search = new Regex (@"\s*search\s+(?<domain>.*)");
  75. string dns_suffix;
  76. DateTime last_parse;
  77. void ParseResolvConf ()
  78. {
  79. try {
  80. DateTime wt = File.GetLastWriteTime ("/etc/resolv.conf");
  81. if (wt <= last_parse)
  82. return;
  83. last_parse = wt;
  84. dns_suffix = "";
  85. dns_servers = new IPAddressCollection ();
  86. using (StreamReader reader = new StreamReader ("/etc/resolv.conf")) {
  87. string str;
  88. string line;
  89. while ((line = reader.ReadLine ()) != null) {
  90. line = line.Trim ();
  91. if (line.Length == 0 || line [0] == '#')
  92. continue;
  93. Match match = ns.Match (line);
  94. if (match.Success) {
  95. try {
  96. str = match.Groups ["address"].Value;
  97. str = str.Trim ();
  98. dns_servers.InternalAdd (IPAddress.Parse (str));
  99. } catch {
  100. }
  101. } else {
  102. match = search.Match (line);
  103. if (match.Success) {
  104. str = match.Groups ["domain"].Value;
  105. string [] parts = str.Split (',');
  106. dns_suffix = parts [0].Trim ();
  107. }
  108. }
  109. }
  110. }
  111. } catch {
  112. }
  113. }
  114. #endif
  115. public override IPAddressInformationCollection AnycastAddresses {
  116. get {
  117. var c = new IPAddressInformationCollection ();
  118. foreach (IPAddress address in addresses) {
  119. c.InternalAdd (new SystemIPAddressInformation (address, false, false));
  120. }
  121. return c;
  122. }
  123. }
  124. [MonoTODO ("Always returns an empty collection.")]
  125. public override IPAddressCollection DhcpServerAddresses {
  126. get {
  127. // There are lots of different DHCP clients
  128. // that all store their configuration differently.
  129. // I'm not sure what to do here.
  130. IPAddressCollection coll = new IPAddressCollection ();
  131. return coll;
  132. }
  133. }
  134. public override IPAddressCollection DnsAddresses {
  135. get {
  136. #if MONODROID
  137. GetDNSServersFromOS ();
  138. #else
  139. ParseResolvConf ();
  140. #endif
  141. return dns_servers;
  142. }
  143. }
  144. public override string DnsSuffix {
  145. get {
  146. #if MONODROID
  147. return String.Empty;
  148. #else
  149. ParseResolvConf ();
  150. return dns_suffix;
  151. #endif
  152. }
  153. }
  154. [MonoTODO ("Always returns true")]
  155. public override bool IsDnsEnabled {
  156. get {
  157. return true;
  158. }
  159. }
  160. [MonoTODO ("Always returns false")]
  161. public override bool IsDynamicDnsEnabled {
  162. get {
  163. return false;
  164. }
  165. }
  166. public override MulticastIPAddressInformationCollection MulticastAddresses {
  167. get {
  168. var multicastAddresses = new MulticastIPAddressInformationCollection ();
  169. foreach (IPAddress address in addresses) {
  170. byte[] addressBytes = address.GetAddressBytes ();
  171. if (addressBytes[0] >= 224 && addressBytes[0] <= 239) {
  172. multicastAddresses.InternalAdd (new SystemMulticastIPAddressInformation (new SystemIPAddressInformation (address, true, false)));
  173. }
  174. }
  175. return multicastAddresses;
  176. }
  177. }
  178. public override UnicastIPAddressInformationCollection UnicastAddresses {
  179. get {
  180. var unicastAddresses = new UnicastIPAddressInformationCollection ();
  181. foreach (IPAddress address in addresses) {
  182. switch (address.AddressFamily) {
  183. case AddressFamily.InterNetwork:
  184. byte top = address.GetAddressBytes () [0];
  185. if (top >= 224 && top <= 239)
  186. continue;
  187. unicastAddresses.InternalAdd (new LinuxUnicastIPAddressInformation (address));
  188. break;
  189. case AddressFamily.InterNetworkV6:
  190. if (address.IsIPv6Multicast)
  191. continue;
  192. unicastAddresses.InternalAdd (new LinuxUnicastIPAddressInformation (address));
  193. break;
  194. }
  195. }
  196. return unicastAddresses;
  197. }
  198. }
  199. [MonoTODO ("Always returns an empty collection.")]
  200. public override IPAddressCollection WinsServersAddresses {
  201. get {
  202. // I do SUPPOSE we could scrape /etc/samba/smb.conf, but.. yeesh.
  203. return new IPAddressCollection ();
  204. }
  205. }
  206. }
  207. }