Changed link from GUID to URL
[mono.git] / mcs / class / System / System.Net.NetworkInformation / UnicastIPAddressInformation.cs
1 //
2 // System.Net.NetworkInformation.UnicastIPAddressInformation
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //      Atsushi Enomoto (atsushi@ximian.com)
7 //      Eric Butler (eric@extremeboredom.net)
8 //
9 // Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30 using System;
31 using System.Runtime.InteropServices;
32 using System.Net.Sockets;
33
34 namespace System.Net.NetworkInformation {
35         class Win32UnicastIPAddressInformation : UnicastIPAddressInformation 
36         {
37                 int if_index;
38                 Win32_IP_ADAPTER_UNICAST_ADDRESS info;
39
40                 public Win32UnicastIPAddressInformation (int ifIndex, Win32_IP_ADAPTER_UNICAST_ADDRESS info)
41                 {
42                         this.if_index = ifIndex;
43                         this.info = info;
44                 }
45
46                 public override IPAddress Address {
47                         get { return info.Address.GetIPAddress (); }
48                 }
49
50                 public override bool IsDnsEligible {
51                         get { return info.LengthFlags.IsDnsEligible; }
52                 }
53
54                 public override bool IsTransient {
55                         get { return info.LengthFlags.IsTransient; }
56                 }
57
58                 // UnicastIPAddressInformation members
59
60                 public override long AddressPreferredLifetime {
61                         get { return info.PreferredLifetime; }
62                 }
63
64                 public override long AddressValidLifetime {
65                         get { return info.ValidLifetime; }
66                 }
67
68                 public override long DhcpLeaseLifetime {
69                         get { return info.LeaseLifetime; }
70                 }
71
72                 public override DuplicateAddressDetectionState DuplicateAddressDetectionState {
73                         get { return info.DadState; }
74                 }
75
76                 public override IPAddress IPv4Mask {
77                         get {
78                                 Win32_IP_ADAPTER_INFO ai = Win32NetworkInterface2.GetAdapterInfoByIndex (if_index);
79                                 if (ai == null)
80                                         throw new Exception ("huh? " + if_index);
81                                 if (this.Address == null)
82                                         return null;
83                                 string expected = this.Address.ToString ();
84                                 unsafe {
85                                         Win32_IP_ADDR_STRING p = ai.IpAddressList;
86                                         while (true) {
87                                                 if (p.IpAddress == expected)
88                                                         return IPAddress.Parse (p.IpMask);
89                                                 if (p.Next == IntPtr.Zero)
90                                                         break;
91                                                 p = (Win32_IP_ADDR_STRING) Marshal.PtrToStructure (p.Next, typeof (Win32_IP_ADDR_STRING));
92                                         }
93
94                                         // Or whatever it should be...
95                                         return null;
96                                 }
97                         }
98                 }
99
100                 public override PrefixOrigin PrefixOrigin {
101                         get { return info.PrefixOrigin; }
102                 }
103
104                 public override SuffixOrigin SuffixOrigin {
105                         get { return info.SuffixOrigin; }
106                 }
107         }
108
109         class LinuxUnicastIPAddressInformation : UnicastIPAddressInformation
110         {
111                 IPAddress address;
112                 IPAddress ipv4Mask;
113
114                 public LinuxUnicastIPAddressInformation (IPAddress address)
115                 {
116                         this.address = address;
117                 }
118
119                 public override IPAddress Address {
120                         get { return address; }
121                 }
122
123                 public override bool IsDnsEligible {
124                         get {
125                                 byte[] addressBytes = address.GetAddressBytes ();
126                                 return !(addressBytes[0] == 169 && addressBytes[1] == 254);
127                         }
128                 }
129
130                 [MonoTODO("Always returns false")]
131                 public override bool IsTransient {
132                         get { return false; }
133                 }
134
135                 // UnicastIPAddressInformation members
136
137                 public override long AddressPreferredLifetime {
138                         get { throw new NotImplementedException (); }
139                 }
140
141                 public override long AddressValidLifetime {
142                         get { throw new NotImplementedException (); }
143                 }
144
145                 public override long DhcpLeaseLifetime {
146                         get { throw new NotImplementedException (); }
147                 }
148
149                 public override DuplicateAddressDetectionState DuplicateAddressDetectionState {
150                         get { throw new NotImplementedException (); }
151                 }
152
153                 public override IPAddress IPv4Mask {
154                         get {
155                                 // The IPv6 equivilant (for .net compatibility)
156                                 if (Address.AddressFamily != AddressFamily.InterNetwork)
157                                         return IPAddress.Any;
158
159                                 if (ipv4Mask == null)
160                                         ipv4Mask = NetworkInterface.GetNetMask (address);
161
162                                 return ipv4Mask;
163                         }
164                 }
165
166                 public override PrefixOrigin PrefixOrigin {
167                         get { throw new NotImplementedException (); }
168                 }
169
170                 public override SuffixOrigin SuffixOrigin {
171                         get { throw new NotImplementedException (); }
172                 }
173         }
174 }
175