2 // System.Net.NetworkInformation.UnicastIPAddressInformation
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
6 // Atsushi Enomoto (atsushi@ximian.com)
7 // Eric Butler (eric@extremeboredom.net)
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.Runtime.InteropServices;
32 using System.Net.Sockets;
34 namespace System.Net.NetworkInformation {
36 class Win32UnicastIPAddressInformation : UnicastIPAddressInformation
39 Win32_IP_ADAPTER_UNICAST_ADDRESS info;
41 public Win32UnicastIPAddressInformation (int ifIndex, Win32_IP_ADAPTER_UNICAST_ADDRESS info)
43 this.if_index = ifIndex;
47 public override IPAddress Address {
48 get { return info.Address.GetIPAddress (); }
51 public override bool IsDnsEligible {
52 get { return info.LengthFlags.IsDnsEligible; }
55 public override bool IsTransient {
56 get { return info.LengthFlags.IsTransient; }
59 // UnicastIPAddressInformation members
61 public override long AddressPreferredLifetime {
62 get { return info.PreferredLifetime; }
65 public override long AddressValidLifetime {
66 get { return info.ValidLifetime; }
69 public override long DhcpLeaseLifetime {
70 get { return info.LeaseLifetime; }
73 public override DuplicateAddressDetectionState DuplicateAddressDetectionState {
74 get { return info.DadState; }
77 public override IPAddress IPv4Mask {
79 Win32_IP_ADAPTER_INFO ai = Win32NetworkInterface2.GetAdapterInfoByIndex (if_index);
81 throw new Exception ("huh? " + if_index);
82 if (this.Address == null)
84 string expected = this.Address.ToString ();
86 Win32_IP_ADDR_STRING p = ai.IpAddressList;
88 if (p.IpAddress == expected)
89 return IPAddress.Parse (p.IpMask);
90 if (p.Next == IntPtr.Zero)
92 p = (Win32_IP_ADDR_STRING) Marshal.PtrToStructure (p.Next, typeof (Win32_IP_ADDR_STRING));
95 // Or whatever it should be...
101 public override PrefixOrigin PrefixOrigin {
102 get { return info.PrefixOrigin; }
105 public override SuffixOrigin SuffixOrigin {
106 get { return info.SuffixOrigin; }
111 class LinuxUnicastIPAddressInformation : UnicastIPAddressInformation
116 public LinuxUnicastIPAddressInformation (IPAddress address)
118 this.address = address;
121 public override IPAddress Address {
122 get { return address; }
125 public override bool IsDnsEligible {
127 byte[] addressBytes = address.GetAddressBytes ();
128 return !(addressBytes[0] == 169 && addressBytes[1] == 254);
132 [MonoTODO("Always returns false")]
133 public override bool IsTransient {
134 get { return false; }
137 // UnicastIPAddressInformation members
139 public override long AddressPreferredLifetime {
140 get { throw new NotImplementedException (); }
143 public override long AddressValidLifetime {
144 get { throw new NotImplementedException (); }
147 public override long DhcpLeaseLifetime {
148 get { throw new NotImplementedException (); }
151 public override DuplicateAddressDetectionState DuplicateAddressDetectionState {
152 get { throw new NotImplementedException (); }
155 public override IPAddress IPv4Mask {
157 // The IPv6 equivilant (for .net compatibility)
158 if (Address.AddressFamily != AddressFamily.InterNetwork)
159 return IPAddress.Any;
161 if (ipv4Mask == null)
162 ipv4Mask = NetworkInterface.GetNetMask (address);
168 public override PrefixOrigin PrefixOrigin {
169 get { throw new NotImplementedException (); }
172 public override SuffixOrigin SuffixOrigin {
173 get { throw new NotImplementedException (); }