Merge pull request #3142 from henricm/fix-for-win-mono_string_to_utf8
[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 #if !MOBILE
36         class Win32UnicastIPAddressInformation : UnicastIPAddressInformation 
37         {
38                 int if_index;
39                 Win32_IP_ADAPTER_UNICAST_ADDRESS info;
40
41                 public Win32UnicastIPAddressInformation (int ifIndex, Win32_IP_ADAPTER_UNICAST_ADDRESS info)
42                 {
43                         this.if_index = ifIndex;
44                         this.info = info;
45                 }
46
47                 public override IPAddress Address {
48                         get { return info.Address.GetIPAddress (); }
49                 }
50
51                 public override bool IsDnsEligible {
52                         get { return info.LengthFlags.IsDnsEligible; }
53                 }
54
55                 public override bool IsTransient {
56                         get { return info.LengthFlags.IsTransient; }
57                 }
58
59                 // UnicastIPAddressInformation members
60
61                 public override long AddressPreferredLifetime {
62                         get { return info.PreferredLifetime; }
63                 }
64
65                 public override long AddressValidLifetime {
66                         get { return info.ValidLifetime; }
67                 }
68
69                 public override long DhcpLeaseLifetime {
70                         get { return info.LeaseLifetime; }
71                 }
72
73                 public override DuplicateAddressDetectionState DuplicateAddressDetectionState {
74                         get { return info.DadState; }
75                 }
76
77                 public override IPAddress IPv4Mask {
78                         get {
79                                 Win32_IP_ADAPTER_INFO ai = Win32NetworkInterface2.GetAdapterInfoByIndex (if_index);
80                                 if (ai == null)
81                                         throw new Exception ("huh? " + if_index);
82                                 if (this.Address == null)
83                                         return null;
84                                 string expected = this.Address.ToString ();
85                                 unsafe {
86                                         Win32_IP_ADDR_STRING p = ai.IpAddressList;
87                                         while (true) {
88                                                 if (p.IpAddress == expected)
89                                                         return IPAddress.Parse (p.IpMask);
90                                                 if (p.Next == IntPtr.Zero)
91                                                         break;
92                                                 p = (Win32_IP_ADDR_STRING) Marshal.PtrToStructure (p.Next, typeof (Win32_IP_ADDR_STRING));
93                                         }
94
95                                         // Or whatever it should be...
96                                         return null;
97                                 }
98                         }
99                 }
100
101                 public override PrefixOrigin PrefixOrigin {
102                         get { return info.PrefixOrigin; }
103                 }
104
105                 public override SuffixOrigin SuffixOrigin {
106                         get { return info.SuffixOrigin; }
107                 }
108         }
109 #endif
110
111         class LinuxUnicastIPAddressInformation : UnicastIPAddressInformation
112         {
113                 IPAddress address;
114                 IPAddress ipv4Mask;
115
116                 public LinuxUnicastIPAddressInformation (IPAddress address)
117                 {
118                         this.address = address;
119                 }
120
121                 public override IPAddress Address {
122                         get { return address; }
123                 }
124
125                 public override bool IsDnsEligible {
126                         get {
127                                 byte[] addressBytes = address.GetAddressBytes ();
128                                 return !(addressBytes[0] == 169 && addressBytes[1] == 254);
129                         }
130                 }
131
132                 [MonoTODO("Always returns false")]
133                 public override bool IsTransient {
134                         get { return false; }
135                 }
136
137                 // UnicastIPAddressInformation members
138
139                 public override long AddressPreferredLifetime {
140                         get { throw new NotImplementedException (); }
141                 }
142
143                 public override long AddressValidLifetime {
144                         get { throw new NotImplementedException (); }
145                 }
146
147                 public override long DhcpLeaseLifetime {
148                         get { throw new NotImplementedException (); }
149                 }
150
151                 public override DuplicateAddressDetectionState DuplicateAddressDetectionState {
152                         get { throw new NotImplementedException (); }
153                 }
154
155                 public override IPAddress IPv4Mask {
156                         get {
157                                 // The IPv6 equivilant (for .net compatibility)
158                                 if (Address.AddressFamily != AddressFamily.InterNetwork)
159                                         return IPAddress.Any;
160
161                                 if (ipv4Mask == null)
162                                         ipv4Mask = SystemNetworkInterface.GetNetMask (address);
163
164                                 return ipv4Mask;
165                         }
166                 }
167
168                 public override PrefixOrigin PrefixOrigin {
169                         get { throw new NotImplementedException (); }
170                 }
171
172                 public override SuffixOrigin SuffixOrigin {
173                         get { throw new NotImplementedException (); }
174                 }
175         }
176 }
177