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