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 {
35 class Win32UnicastIPAddressInformation : UnicastIPAddressInformation
38 Win32_IP_ADAPTER_UNICAST_ADDRESS info;
40 public Win32UnicastIPAddressInformation (int ifIndex, Win32_IP_ADAPTER_UNICAST_ADDRESS info)
42 this.if_index = ifIndex;
46 public override IPAddress Address {
47 get { return info.Address.GetIPAddress (); }
50 public override bool IsDnsEligible {
51 get { return info.LengthFlags.IsDnsEligible; }
54 public override bool IsTransient {
55 get { return info.LengthFlags.IsTransient; }
58 // UnicastIPAddressInformation members
60 public override long AddressPreferredLifetime {
61 get { return info.PreferredLifetime; }
64 public override long AddressValidLifetime {
65 get { return info.ValidLifetime; }
68 public override long DhcpLeaseLifetime {
69 get { return info.LeaseLifetime; }
72 public override DuplicateAddressDetectionState DuplicateAddressDetectionState {
73 get { return info.DadState; }
76 public override IPAddress IPv4Mask {
78 Win32_IP_ADAPTER_INFO ai = Win32NetworkInterface2.GetAdapterInfoByIndex (if_index);
80 throw new Exception ("huh? " + if_index);
81 if (this.Address == null)
83 string expected = this.Address.ToString ();
85 Win32_IP_ADDR_STRING p = ai.IpAddressList;
87 if (p.IpAddress == expected)
88 return IPAddress.Parse (p.IpMask);
89 if (p.Next == IntPtr.Zero)
91 p = (Win32_IP_ADDR_STRING) Marshal.PtrToStructure (p.Next, typeof (Win32_IP_ADDR_STRING));
94 // Or whatever it should be...
100 public override PrefixOrigin PrefixOrigin {
101 get { return info.PrefixOrigin; }
104 public override SuffixOrigin SuffixOrigin {
105 get { return info.SuffixOrigin; }
109 class LinuxUnicastIPAddressInformation : UnicastIPAddressInformation
114 public LinuxUnicastIPAddressInformation (IPAddress address)
116 this.address = address;
119 public override IPAddress Address {
120 get { return address; }
123 public override bool IsDnsEligible {
125 byte[] addressBytes = address.GetAddressBytes ();
126 return !(addressBytes[0] == 169 && addressBytes[1] == 254);
130 [MonoTODO("Always returns false")]
131 public override bool IsTransient {
132 get { return false; }
135 // UnicastIPAddressInformation members
137 public override long AddressPreferredLifetime {
138 get { throw new NotImplementedException (); }
141 public override long AddressValidLifetime {
142 get { throw new NotImplementedException (); }
145 public override long DhcpLeaseLifetime {
146 get { throw new NotImplementedException (); }
149 public override DuplicateAddressDetectionState DuplicateAddressDetectionState {
150 get { throw new NotImplementedException (); }
153 public override IPAddress IPv4Mask {
155 // The IPv6 equivilant (for .net compatibility)
156 if (Address.AddressFamily != AddressFamily.InterNetwork)
157 return IPAddress.Any;
159 if (ipv4Mask == null)
160 ipv4Mask = NetworkInterface.GetNetMask (address);
166 public override PrefixOrigin PrefixOrigin {
167 get { throw new NotImplementedException (); }
170 public override SuffixOrigin SuffixOrigin {
171 get { throw new NotImplementedException (); }