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;
33 using System.Diagnostics.Contracts;
35 namespace System.Net.NetworkInformation {
37 class Win32UnicastIPAddressInformation : UnicastIPAddressInformation
39 Win32_IP_ADAPTER_UNICAST_ADDRESS info;
42 public Win32UnicastIPAddressInformation (Win32_IP_ADAPTER_UNICAST_ADDRESS info)
45 IPAddress ipAddress = info.Address.GetIPAddress ();
46 // IPv6 returns 0.0.0.0 for consistancy with XP
47 if (ipAddress.AddressFamily == AddressFamily.InterNetwork) {
48 ipv4Mask = PrefixLengthToSubnetMask (info.OnLinkPrefixLength, ipAddress.AddressFamily);
52 public override IPAddress Address {
53 get { return info.Address.GetIPAddress (); }
56 public override bool IsDnsEligible {
57 get { return info.LengthFlags.IsDnsEligible; }
60 public override bool IsTransient {
61 get { return info.LengthFlags.IsTransient; }
64 // UnicastIPAddressInformation members
66 public override long AddressPreferredLifetime {
67 get { return info.PreferredLifetime; }
70 public override long AddressValidLifetime {
71 get { return info.ValidLifetime; }
74 public override long DhcpLeaseLifetime {
75 get { return info.LeaseLifetime; }
78 public override DuplicateAddressDetectionState DuplicateAddressDetectionState {
79 get { return info.DadState; }
82 public override IPAddress IPv4Mask{
84 // The IPv6 equivilant was never available on XP, and we've kept this behavior for legacy reasons.
85 // For IPv6 use PrefixLength instead.
86 if (Address.AddressFamily != AddressFamily.InterNetwork) {
94 public override PrefixOrigin PrefixOrigin {
95 get { return info.PrefixOrigin; }
98 public override SuffixOrigin SuffixOrigin {
99 get { return info.SuffixOrigin; }
102 // Convert a CIDR prefix length to a subnet mask "255.255.255.0" format
103 private static IPAddress PrefixLengthToSubnetMask (byte prefixLength, AddressFamily family) {
104 Contract.Requires ((0 <= prefixLength) && (prefixLength <= 126));
105 Contract.Requires ((family == AddressFamily.InterNetwork) || (family == AddressFamily.InterNetworkV6));
108 if (family == AddressFamily.InterNetwork) {
109 addressBytes = new byte [4];
111 addressBytes = new byte [16];
114 Contract.Assert (prefixLength < (addressBytes.Length * 8));
116 // Enable bits one at a time from left/high to right/low
117 for (int bit = 0; bit < prefixLength; bit++) {
118 addressBytes [bit / 8] |= (byte) (0x80 >> (bit % 8));
121 return new IPAddress (addressBytes);
126 class LinuxUnicastIPAddressInformation : UnicastIPAddressInformation
131 public LinuxUnicastIPAddressInformation (IPAddress address)
133 this.address = address;
136 public override IPAddress Address {
137 get { return address; }
140 public override bool IsDnsEligible {
142 byte[] addressBytes = address.GetAddressBytes ();
143 return !(addressBytes[0] == 169 && addressBytes[1] == 254);
147 [MonoTODO("Always returns false")]
148 public override bool IsTransient {
149 get { return false; }
152 // UnicastIPAddressInformation members
154 public override long AddressPreferredLifetime {
155 get { throw new NotImplementedException (); }
158 public override long AddressValidLifetime {
159 get { throw new NotImplementedException (); }
162 public override long DhcpLeaseLifetime {
163 get { throw new NotImplementedException (); }
166 public override DuplicateAddressDetectionState DuplicateAddressDetectionState {
167 get { throw new NotImplementedException (); }
170 public override IPAddress IPv4Mask {
172 // The IPv6 equivilant (for .net compatibility)
173 if (Address.AddressFamily != AddressFamily.InterNetwork)
174 return IPAddress.Any;
176 if (ipv4Mask == null)
177 ipv4Mask = SystemNetworkInterface.GetNetMask (address);
183 public override PrefixOrigin PrefixOrigin {
184 get { throw new NotImplementedException (); }
187 public override SuffixOrigin SuffixOrigin {
188 get { throw new NotImplementedException (); }