merge -r 60814:60815
[mono.git] / mcs / class / System / System.Net / Dns.cs
1 // System.Net.Dns.cs
2 //
3 // Author: Mads Pultz (mpultz@diku.dk)
4 // Author: Lawrence Pit (loz@cable.a2000.nl)
5 //
6 // (C) Mads Pultz, 2001
7
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Net.Sockets;
31 using System.Text;
32 using System.Collections;
33 using System.Threading;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.Remoting.Messaging;
36
37 namespace System.Net {
38 #if NET_2_0
39         public static class Dns {
40 #else
41         public sealed class Dns {
42
43                 private Dns () {}
44 #endif
45
46                 static Dns ()
47                 {
48                         System.Net.Sockets.Socket.CheckProtocolSupport();
49                 }
50
51                 private delegate IPHostEntry GetHostByNameCallback (string hostName);
52                 private delegate IPHostEntry ResolveCallback (string hostName);
53 #if NET_2_0
54                 private delegate IPHostEntry GetHostEntryNameCallback (string hostName);
55                 private delegate IPHostEntry GetHostEntryIPCallback (IPAddress hostAddress);
56                 private delegate IPAddress [] GetHostAddressesCallback (string hostName);
57 #endif
58
59 #if NET_2_0
60                 [Obsolete ("Use BeginGetHostEntry instead")]
61 #endif
62                 public static IAsyncResult BeginGetHostByName (string hostName,
63                         AsyncCallback requestCallback, object stateObject)
64                 {
65                         if (hostName == null)
66                                 throw new ArgumentNullException ("hostName");
67
68                         GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
69                         return c.BeginInvoke (hostName, requestCallback, stateObject);
70                 }
71
72 #if NET_2_0
73                 [Obsolete ("Use BeginGetHostEntry instead")]
74 #endif
75                 public static IAsyncResult BeginResolve (string hostName,
76                         AsyncCallback requestCallback, object stateObject)
77                 {
78                         if (hostName == null)
79                                 throw new ArgumentNullException ("hostName");
80
81                         ResolveCallback c = new ResolveCallback (Resolve);
82                         return c.BeginInvoke (hostName, requestCallback, stateObject);
83                 }
84
85 #if NET_2_0
86                 public static IAsyncResult BeginGetHostAddresses (string hostName,
87                         AsyncCallback requestCallback, object stateObject)
88                 {
89                         if (hostName == null)
90                                 throw new ArgumentNullException ("hostName");
91
92                         GetHostAddressesCallback c = new GetHostAddressesCallback (GetHostAddresses);
93                         return c.BeginInvoke (hostName, requestCallback, stateObject);
94                 }
95
96                 public static IAsyncResult BeginGetHostEntry (string hostNameOrAddress,
97                         AsyncCallback requestCallback, object stateObject)
98                 {
99                         if (hostNameOrAddress == null)
100                                 throw new ArgumentNullException ("hostNameOrAddress");
101
102                         GetHostEntryNameCallback c = new GetHostEntryNameCallback (GetHostEntry);
103                         return c.BeginInvoke (hostNameOrAddress, requestCallback, stateObject);
104                 }
105
106                 public static IAsyncResult BeginGetHostEntry (IPAddress hostAddress,
107                         AsyncCallback requestCallback, object stateObject)
108                 {
109                         if (hostAddress == null)
110                                 throw new ArgumentNullException ("hostAddress");
111
112                         GetHostEntryIPCallback c = new GetHostEntryIPCallback (GetHostEntry);
113                         return c.BeginInvoke (hostAddress, requestCallback, stateObject);
114                 }
115 #endif
116
117 #if NET_2_0
118                 [Obsolete ("Use EndGetHostEntry instead")]
119 #endif
120                 public static IPHostEntry EndGetHostByName (IAsyncResult asyncResult) 
121                 {
122                         if (asyncResult == null)
123                                 throw new ArgumentNullException ("asyncResult");
124
125                         AsyncResult async = (AsyncResult) asyncResult;
126                         GetHostByNameCallback cb = (GetHostByNameCallback) async.AsyncDelegate;
127                         return cb.EndInvoke(asyncResult);
128                 }
129
130 #if NET_2_0
131                 [Obsolete ("Use EndGetHostEntry instead")]
132 #endif
133                 public static IPHostEntry EndResolve (IAsyncResult asyncResult) 
134                 {
135                         if (asyncResult == null)
136                                 throw new ArgumentNullException ("asyncResult");
137                         AsyncResult async = (AsyncResult) asyncResult;
138                         ResolveCallback cb = (ResolveCallback) async.AsyncDelegate;
139                         return cb.EndInvoke(asyncResult);
140                 }
141
142 #if NET_2_0
143
144                 public static IPAddress [] EndGetHostAddresses (IAsyncResult asyncResult) 
145                 {
146                         if (asyncResult == null)
147                                 throw new ArgumentNullException ("asyncResult");
148
149                         AsyncResult async = (AsyncResult) asyncResult;
150                         GetHostAddressesCallback cb = (GetHostAddressesCallback) async.AsyncDelegate;
151                         return cb.EndInvoke(asyncResult);
152                 }
153
154                 public static IPHostEntry EndGetHostEntry (IAsyncResult asyncResult) 
155                 {
156                         if (asyncResult == null)
157                                 throw new ArgumentNullException ("asyncResult");
158                         AsyncResult async = (AsyncResult) asyncResult;
159 #if NET_2_0
160                         if (async.AsyncDelegate is GetHostEntryIPCallback)
161                                 return ((GetHostEntryIPCallback) async.AsyncDelegate).EndInvoke (asyncResult);
162 #endif
163                         GetHostEntryNameCallback cb = (GetHostEntryNameCallback) async.AsyncDelegate;
164                         return cb.EndInvoke(asyncResult);
165                 }
166 #endif
167
168                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
169                 private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
170
171                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
172                 private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
173
174                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
175                 private extern static bool GetHostName_internal(out string h_name);
176                 
177                 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist) 
178                 {
179                         IPHostEntry he = new IPHostEntry();
180                         ArrayList addrlist = new ArrayList();
181
182                         he.HostName = h_name;
183                         he.Aliases = h_aliases;
184                         for(int i=0; i<h_addrlist.Length; i++) {
185                                 try {
186                                         IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
187
188                                         if( (Socket.SupportsIPv6 && newAddress.AddressFamily == AddressFamily.InterNetworkV6) ||
189                                             (Socket.SupportsIPv4 && newAddress.AddressFamily == AddressFamily.InterNetwork) )
190                                                 addrlist.Add(newAddress);
191                                 } catch (ArgumentNullException) {
192                                         /* Ignore this, as the
193                                          * internal call might have
194                                          * left some blank entries at
195                                          * the end of the array
196                                          */
197                                 }
198                         }
199
200                         if(addrlist.Count == 0)
201                                 throw new SocketException(11001);
202
203                         he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
204                         return he;
205                 }
206
207 #if NET_2_0
208                 [Obsolete ("Use GetHostEntry instead")]
209 #endif
210                 public static IPHostEntry GetHostByAddress(IPAddress address)
211                 {
212                         if (address == null)
213                                 throw new ArgumentNullException ("address");
214
215                         return GetHostByAddressFromString (address.ToString (), false);
216                 }
217
218 #if NET_2_0
219                 [Obsolete ("Use GetHostEntry instead")]
220 #endif
221                 public static IPHostEntry GetHostByAddress(string address)
222                 {
223                         if (address == null)
224                                 throw new ArgumentNullException ("address");
225
226                         return GetHostByAddressFromString (address, true);
227                 }
228
229                 static IPHostEntry GetHostByAddressFromString (string address, bool parse)
230                 {
231                         // Undocumented MS behavior: when called with IF_ANY,
232                         // this should return the local host
233                         if (address.Equals ("0.0.0.0")) {
234                                 address = "127.0.0.1";
235                                 parse = false;
236                         }
237
238                         // Must check the IP format, might send an exception if invalid string.
239                         if (parse)
240                                 IPAddress.Parse(address);
241
242                         string h_name;
243                         string[] h_aliases, h_addrlist;
244
245                         bool ret = GetHostByAddr_internal(address, out h_name, out h_aliases, out h_addrlist);
246                         if (!ret)
247                                 throw new SocketException(11001);
248
249                         return (hostent_to_IPHostEntry (h_name, h_aliases, h_addrlist));
250                 }
251
252 #if NET_2_0
253                 public static IPHostEntry GetHostEntry (string hostNameOrAddress)
254                 {
255                         if (hostNameOrAddress == null)
256                                 throw new ArgumentNullException ("hostNameOrAddress");
257
258                         if (hostNameOrAddress == "0.0.0.0")
259                                 hostNameOrAddress = "127.0.0.1";
260                         IPAddress addr;
261                         if (IPAddress.TryParse (hostNameOrAddress, out addr))
262                                 return GetHostEntry (addr);
263                         else
264                                 return GetHostByName (hostNameOrAddress);
265                 }
266
267                 public static IPHostEntry GetHostEntry (IPAddress address)
268                 {
269                         if (address == null)
270                                 throw new ArgumentNullException ("address");
271
272                         return GetHostByAddressFromString (address.ToString (), false);
273                 }
274
275                 public static IPAddress [] GetHostAddresses (string hostNameOrAddress)
276                 {
277                         if (hostNameOrAddress == null)
278                                 throw new ArgumentNullException ("hostNameOrAddress");
279
280                         return GetHostEntry (hostNameOrAddress).AddressList;
281                 }
282 #endif
283
284 #if NET_2_0
285                 [Obsolete ("Use GetHostEntry instead")]
286 #endif
287                 public static IPHostEntry GetHostByName(string hostName) 
288                 {
289                         if (hostName == null)
290                                 throw new ArgumentNullException();
291
292                         string h_name;
293                         string[] h_aliases, h_addrlist;
294
295                         bool ret = GetHostByName_internal(hostName, out h_name,
296                                 out h_aliases,
297                                 out h_addrlist);
298                         if (ret == false)
299                                 throw new SocketException(11001);
300
301                         return(hostent_to_IPHostEntry(h_name, h_aliases,
302                                 h_addrlist));
303                 }
304
305                 public static string GetHostName() 
306                 {
307                         string hostName;
308
309                         bool ret = GetHostName_internal(out hostName);
310
311                         if (ret == false)
312                                 throw new SocketException(11001);
313
314                         return hostName;
315                 }
316
317 #if NET_2_0
318                 [Obsolete ("Use GetHostEntry instead")]
319 #endif
320                 public static IPHostEntry Resolve(string hostName) 
321                 {
322                         if (hostName == null)
323                                 throw new ArgumentNullException ("hostName");
324
325                         IPHostEntry ret = null;
326
327                         try {
328                                 ret =  GetHostByAddress(hostName);
329                         }
330                         catch{}
331
332                         if(ret == null)
333                                 ret =  GetHostByName(hostName);
334
335                         return ret;
336                 }
337         }
338 }
339