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;
33 namespace System.Net.NetworkInformation {
34 public abstract class UnicastIPAddressInformation : IPAddressInformation {
35 protected UnicastIPAddressInformation ()
39 public abstract long AddressPreferredLifetime { get; }
40 public abstract long AddressValidLifetime { get; }
41 public abstract long DhcpLeaseLifetime { get; }
42 public abstract DuplicateAddressDetectionState DuplicateAddressDetectionState { get; }
43 public abstract IPAddress IPv4Mask { get; }
44 public abstract PrefixOrigin PrefixOrigin { get; }
45 public abstract SuffixOrigin SuffixOrigin { get; }
48 class Win32UnicastIPAddressInformation : UnicastIPAddressInformation
51 Win32_IP_ADAPTER_UNICAST_ADDRESS info;
53 public Win32UnicastIPAddressInformation (int ifIndex, Win32_IP_ADAPTER_UNICAST_ADDRESS info)
55 this.if_index = ifIndex;
59 public override IPAddress Address {
60 get { return info.Address.GetIPAddress (); }
63 public override bool IsDnsEligible {
64 get { return info.LengthFlags.IsDnsEligible; }
67 public override bool IsTransient {
68 get { return info.LengthFlags.IsTransient; }
71 // UnicastIPAddressInformation members
73 public override long AddressPreferredLifetime {
74 get { return info.PreferredLifetime; }
77 public override long AddressValidLifetime {
78 get { return info.ValidLifetime; }
81 public override long DhcpLeaseLifetime {
82 get { return info.LeaseLifetime; }
85 public override DuplicateAddressDetectionState DuplicateAddressDetectionState {
86 get { return info.DadState; }
89 public override IPAddress IPv4Mask {
91 Win32_IP_ADAPTER_INFO ai = Win32NetworkInterface2.GetAdapterInfoByIndex (if_index);
93 throw new Exception ("huh? " + if_index);
94 if (this.Address == null)
96 string expected = this.Address.ToString ();
98 Win32_IP_ADDR_STRING p = ai.IpAddressList;
100 if (p.IpAddress == expected)
101 return IPAddress.Parse (p.IpMask);
102 if (p.Next == IntPtr.Zero)
104 p = (Win32_IP_ADDR_STRING) Marshal.PtrToStructure (p.Next, typeof (Win32_IP_ADDR_STRING));
107 // Or whatever it should be...
113 public override PrefixOrigin PrefixOrigin {
114 get { return info.PrefixOrigin; }
117 public override SuffixOrigin SuffixOrigin {
118 get { return info.SuffixOrigin; }
122 class LinuxUnicastIPAddressInformation : UnicastIPAddressInformation
126 public LinuxUnicastIPAddressInformation (IPAddress address)
128 this.address = address;
131 public override IPAddress Address {
132 get { return address; }
135 public override bool IsDnsEligible {
137 byte[] addressBytes = address.GetAddressBytes ();
138 return !(addressBytes[0] == 169 && addressBytes[1] == 254);
142 [MonoTODO("Always returns false")]
143 public override bool IsTransient {
144 get { return false; }
147 // UnicastIPAddressInformation members
149 public override long AddressPreferredLifetime {
150 get { throw new NotImplementedException (); }
153 public override long AddressValidLifetime {
154 get { throw new NotImplementedException (); }
157 public override long DhcpLeaseLifetime {
158 get { throw new NotImplementedException (); }
161 public override DuplicateAddressDetectionState DuplicateAddressDetectionState {
162 get { throw new NotImplementedException (); }
165 public override IPAddress IPv4Mask {
166 get { throw new NotImplementedException (); }
169 public override PrefixOrigin PrefixOrigin {
170 get { throw new NotImplementedException (); }
173 public override SuffixOrigin SuffixOrigin {
174 get { throw new NotImplementedException (); }