2 // System.Net.NetworkInformation.IPInterfaceProperties
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
6 // Atsushi Enomoto (atsushi@ximian.com)
8 // Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
30 using System.Globalization;
32 using System.Net.Sockets;
33 using System.Runtime.CompilerServices;
34 using System.Text.RegularExpressions;
35 using System.Runtime.InteropServices;
37 namespace System.Net.NetworkInformation {
38 abstract class UnixIPInterfaceProperties : IPInterfaceProperties
40 protected IPv4InterfaceProperties ipv4iface_properties;
41 protected UnixNetworkInterface iface;
42 List <IPAddress> addresses;
43 IPAddressCollection dns_servers;
47 public UnixIPInterfaceProperties (UnixNetworkInterface iface, List <IPAddress> addresses)
50 this.addresses = addresses;
53 public override IPv6InterfaceProperties GetIPv6Properties ()
55 throw new NotImplementedException ();
58 [DllImport ("__Internal")]
59 static extern int _monodroid_get_dns_servers (out IntPtr dns_servers_array);
61 void GetDNSServersFromOS ()
64 int len = _monodroid_get_dns_servers (out dsa);
68 var servers = new IntPtr [len];
69 Marshal.Copy (dsa, servers, 0, len);
71 dns_servers = new IPAddressCollection ();
72 foreach (IntPtr s in servers) {
73 string server_ip = Marshal.PtrToStringAnsi (s);
74 Marshal.FreeHGlobal (s);
77 if (!IPAddress.TryParse (server_ip, out addr))
79 dns_servers.InternalAdd (addr);
81 Marshal.FreeHGlobal (dsa);
84 static Regex ns = new Regex (@"\s*nameserver\s+(?<address>.*)");
85 static Regex search = new Regex (@"\s*search\s+(?<domain>.*)");
86 void ParseResolvConf ()
89 DateTime wt = File.GetLastWriteTime ("/etc/resolv.conf");
95 dns_servers = new IPAddressCollection ();
96 using (StreamReader reader = new StreamReader ("/etc/resolv.conf")) {
99 while ((line = reader.ReadLine ()) != null) {
101 if (line.Length == 0 || line [0] == '#')
103 Match match = ns.Match (line);
106 str = match.Groups ["address"].Value;
108 dns_servers.InternalAdd (IPAddress.Parse (str));
112 match = search.Match (line);
114 str = match.Groups ["domain"].Value;
115 string [] parts = str.Split (',');
116 dns_suffix = parts [0].Trim ();
125 public override IPAddressInformationCollection AnycastAddresses {
127 var c = new IPAddressInformationCollection ();
128 foreach (IPAddress address in addresses) {
129 c.InternalAdd (new SystemIPAddressInformation (address, false, false));
135 [MonoTODO ("Always returns an empty collection.")]
136 public override IPAddressCollection DhcpServerAddresses {
138 // There are lots of different DHCP clients
139 // that all store their configuration differently.
140 // I'm not sure what to do here.
141 IPAddressCollection coll = new IPAddressCollection ();
146 public override IPAddressCollection DnsAddresses {
149 GetDNSServersFromOS ();
157 public override string DnsSuffix {
168 [MonoTODO ("Always returns true")]
169 public override bool IsDnsEnabled {
175 [MonoTODO ("Always returns false")]
176 public override bool IsDynamicDnsEnabled {
182 public override MulticastIPAddressInformationCollection MulticastAddresses {
184 var multicastAddresses = new MulticastIPAddressInformationCollection ();
185 foreach (IPAddress address in addresses) {
186 byte[] addressBytes = address.GetAddressBytes ();
187 if (addressBytes[0] >= 224 && addressBytes[0] <= 239) {
188 multicastAddresses.InternalAdd (new SystemMulticastIPAddressInformation (new SystemIPAddressInformation (address, true, false)));
191 return multicastAddresses;
195 public override UnicastIPAddressInformationCollection UnicastAddresses {
197 var unicastAddresses = new UnicastIPAddressInformationCollection ();
198 foreach (IPAddress address in addresses) {
199 switch (address.AddressFamily) {
200 case AddressFamily.InterNetwork:
201 byte top = address.GetAddressBytes () [0];
202 if (top >= 224 && top <= 239)
204 unicastAddresses.InternalAdd (new LinuxUnicastIPAddressInformation (address));
207 case AddressFamily.InterNetworkV6:
208 if (address.IsIPv6Multicast)
210 unicastAddresses.InternalAdd (new LinuxUnicastIPAddressInformation (address));
214 return unicastAddresses;
218 [MonoTODO ("Always returns an empty collection.")]
219 public override IPAddressCollection WinsServersAddresses {
221 // I do SUPPOSE we could scrape /etc/samba/smb.conf, but.. yeesh.
222 return new IPAddressCollection ();
227 class LinuxIPInterfaceProperties : UnixIPInterfaceProperties
229 public LinuxIPInterfaceProperties (LinuxNetworkInterface iface, List <IPAddress> addresses)
230 : base (iface, addresses)
234 public override IPv4InterfaceProperties GetIPv4Properties ()
236 if (ipv4iface_properties == null)
237 ipv4iface_properties = new LinuxIPv4InterfaceProperties (iface as LinuxNetworkInterface);
239 return ipv4iface_properties;
242 IPAddressCollection ParseRouteInfo (string iface)
244 var col = new IPAddressCollection ();
246 using (StreamReader reader = new StreamReader ("/proc/net/route")) {
248 reader.ReadLine (); // Ignore first line
249 while ((line = reader.ReadLine ()) != null) {
251 if (line.Length == 0)
254 string [] parts = line.Split ('\t');
255 if (parts.Length < 3)
257 string gw_address = parts [2].Trim ();
258 byte [] ipbytes = new byte [4];
259 if (gw_address.Length == 8 && iface.Equals (parts [0], StringComparison.OrdinalIgnoreCase)) {
260 for (int i = 0; i < 4; i++) {
261 if (!Byte.TryParse (gw_address.Substring (i * 2, 2), NumberStyles.HexNumber, null, out ipbytes [3 - i]))
264 IPAddress ip = new IPAddress (ipbytes);
265 if (!ip.Equals (IPAddress.Any) && !col.Contains (ip))
266 col.InternalAdd (ip);
276 public override GatewayIPAddressInformationCollection GatewayAddresses {
278 return SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection (ParseRouteInfo (this.iface.Name.ToString()));
283 class MacOsIPInterfaceProperties : UnixIPInterfaceProperties
285 public MacOsIPInterfaceProperties (MacOsNetworkInterface iface, List <IPAddress> addresses)
286 : base (iface, addresses)
290 public override IPv4InterfaceProperties GetIPv4Properties ()
292 if (ipv4iface_properties == null)
293 ipv4iface_properties = new MacOsIPv4InterfaceProperties (iface as MacOsNetworkInterface);
295 return ipv4iface_properties;
298 [MethodImplAttribute(MethodImplOptions.InternalCall)]
299 private extern static bool ParseRouteInfo_internal(string iface, out string[] gw_addr_list);
301 public override GatewayIPAddressInformationCollection GatewayAddresses {
303 var gateways = new IPAddressCollection ();
304 string[] gw_addrlist;
305 if (!ParseRouteInfo_internal (this.iface.Name.ToString(), out gw_addrlist))
306 return new GatewayIPAddressInformationCollection ();
308 for(int i=0; i<gw_addrlist.Length; i++) {
310 IPAddress ip = IPAddress.Parse(gw_addrlist[i]);
311 if (!ip.Equals (IPAddress.Any) && !gateways.Contains (ip))
312 gateways.InternalAdd (ip);
313 } catch (ArgumentNullException) {
314 /* Ignore this, as the
315 * internal call might have
316 * left some blank entries at
317 * the end of the array
322 return SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection (gateways);
328 class Win32IPInterfaceProperties2 : IPInterfaceProperties
330 readonly Win32_IP_ADAPTER_ADDRESSES addr;
331 readonly Win32_MIB_IFROW mib4, mib6;
333 public Win32IPInterfaceProperties2 (Win32_IP_ADAPTER_ADDRESSES addr, Win32_MIB_IFROW mib4, Win32_MIB_IFROW mib6)
340 public override IPv4InterfaceProperties GetIPv4Properties ()
342 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
343 return v4info != null ? new Win32IPv4InterfaceProperties (v4info, mib4) : null;
346 public override IPv6InterfaceProperties GetIPv6Properties ()
348 Win32_IP_ADAPTER_INFO v6info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib6.Index);
349 return v6info != null ? new Win32IPv6InterfaceProperties (mib6) : null;
352 public override IPAddressInformationCollection AnycastAddresses {
353 get { return Win32FromAnycast (addr.FirstAnycastAddress); }
356 static IPAddressInformationCollection Win32FromAnycast (IntPtr ptr)
358 var c = new IPAddressInformationCollection ();
359 Win32_IP_ADAPTER_ANYCAST_ADDRESS a;
360 for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
361 a = (Win32_IP_ADAPTER_ANYCAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_ANYCAST_ADDRESS));
362 c.InternalAdd (new SystemIPAddressInformation (
363 a.Address.GetIPAddress (),
364 a.LengthFlags.IsDnsEligible,
365 a.LengthFlags.IsTransient));
370 public override IPAddressCollection DhcpServerAddresses {
372 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
373 // FIXME: should ipv6 DhcpServer be considered?
374 return v4info != null ? new Win32IPAddressCollection (v4info.DhcpServer) : Win32IPAddressCollection.Empty;
378 public override IPAddressCollection DnsAddresses {
379 get { return Win32IPAddressCollection.FromDnsServer (addr.FirstDnsServerAddress); }
382 public override string DnsSuffix {
383 get { return addr.DnsSuffix; }
386 public override GatewayIPAddressInformationCollection GatewayAddresses {
388 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
389 // FIXME: should ipv6 DhcpServer be considered?
391 var col = new GatewayIPAddressInformationCollection ();
392 if (v4info != null) {
393 var a = v4info.GatewayList;
394 if (!String.IsNullOrEmpty (a.IpAddress)) {
395 col.InternalAdd(new SystemGatewayIPAddressInformation(IPAddress.Parse (a.IpAddress)));
396 AddSubsequently (a.Next, col);
404 static void AddSubsequently (IntPtr head, GatewayIPAddressInformationCollection col)
406 Win32_IP_ADDR_STRING a;
407 for (IntPtr p = head; p != IntPtr.Zero; p = a.Next) {
408 a = (Win32_IP_ADDR_STRING) Marshal.PtrToStructure (p, typeof (Win32_IP_ADDR_STRING));
409 col.InternalAdd (new SystemGatewayIPAddressInformation (IPAddress.Parse (a.IpAddress)));
413 public override bool IsDnsEnabled {
414 get { return Win32_FIXED_INFO.Instance.EnableDns != 0; }
417 public override bool IsDynamicDnsEnabled {
418 get { return addr.DdnsEnabled; }
421 public override MulticastIPAddressInformationCollection MulticastAddresses {
422 get { return Win32FromMulticast (addr.FirstMulticastAddress); }
425 static MulticastIPAddressInformationCollection Win32FromMulticast (IntPtr ptr)
427 var c = new MulticastIPAddressInformationCollection ();
428 Win32_IP_ADAPTER_MULTICAST_ADDRESS a;
429 for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
430 a = (Win32_IP_ADAPTER_MULTICAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_MULTICAST_ADDRESS));
431 c.InternalAdd (new SystemMulticastIPAddressInformation (new SystemIPAddressInformation (
432 a.Address.GetIPAddress (),
433 a.LengthFlags.IsDnsEligible,
434 a.LengthFlags.IsTransient)));
439 public override UnicastIPAddressInformationCollection UnicastAddresses {
441 Win32_IP_ADAPTER_INFO ai = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
442 // FIXME: should ipv6 DhcpServer be considered?
443 return ai != null ? Win32FromUnicast (addr.FirstUnicastAddress) : new UnicastIPAddressInformationCollection ();
447 static UnicastIPAddressInformationCollection Win32FromUnicast (IntPtr ptr)
449 UnicastIPAddressInformationCollection c = new UnicastIPAddressInformationCollection ();
450 Win32_IP_ADAPTER_UNICAST_ADDRESS a;
451 for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
452 a = (Win32_IP_ADAPTER_UNICAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_UNICAST_ADDRESS));
453 c.InternalAdd (new Win32UnicastIPAddressInformation (a));
458 public override IPAddressCollection WinsServersAddresses {
460 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
461 // FIXME: should ipv6 DhcpServer be considered?
462 return v4info != null ? new Win32IPAddressCollection (v4info.PrimaryWinsServer, v4info.SecondaryWinsServer) : Win32IPAddressCollection.Empty;