merge r67228-r67235, r67237, r67251 and r67256-67259 to trunk (they are
[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 #if !TARGET_JVM
169                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
170                 private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
171
172                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
173                 private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
174
175                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
176                 private extern static bool GetHostName_internal(out string h_name);
177 #endif  
178
179                 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist) 
180                 {
181                         IPHostEntry he = new IPHostEntry();
182                         ArrayList addrlist = new ArrayList();
183
184                         he.HostName = h_name;
185                         he.Aliases = h_aliases;
186                         for(int i=0; i<h_addrlist.Length; i++) {
187                                 try {
188                                         IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
189
190                                         if( (Socket.SupportsIPv6 && newAddress.AddressFamily == AddressFamily.InterNetworkV6) ||
191                                             (Socket.SupportsIPv4 && newAddress.AddressFamily == AddressFamily.InterNetwork) )
192                                                 addrlist.Add(newAddress);
193                                 } catch (ArgumentNullException) {
194                                         /* Ignore this, as the
195                                          * internal call might have
196                                          * left some blank entries at
197                                          * the end of the array
198                                          */
199                                 }
200                         }
201
202                         if(addrlist.Count == 0)
203                                 throw new SocketException(11001);
204
205                         he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
206                         return he;
207                 }
208
209 #if NET_2_0
210                 [Obsolete ("Use GetHostEntry instead")]
211 #endif
212                 public static IPHostEntry GetHostByAddress(IPAddress address)
213                 {
214                         if (address == null)
215                                 throw new ArgumentNullException ("address");
216
217                         return GetHostByAddressFromString (address.ToString (), false);
218                 }
219
220 #if NET_2_0
221                 [Obsolete ("Use GetHostEntry instead")]
222 #endif
223                 public static IPHostEntry GetHostByAddress(string address)
224                 {
225                         if (address == null)
226                                 throw new ArgumentNullException ("address");
227
228                         return GetHostByAddressFromString (address, true);
229                 }
230
231                 static IPHostEntry GetHostByAddressFromString (string address, bool parse)
232                 {
233                         // Undocumented MS behavior: when called with IF_ANY,
234                         // this should return the local host
235                         if (address.Equals ("0.0.0.0")) {
236                                 address = "127.0.0.1";
237                                 parse = false;
238                         }
239
240                         // Must check the IP format, might send an exception if invalid string.
241                         if (parse)
242                                 IPAddress.Parse (address);
243
244                         string h_name;
245                         string[] h_aliases, h_addrlist;
246 #if TARGET_JVM
247                         h_name = null;
248                         h_aliases = null;
249                         h_addrlist = null;
250                         java.net.InetAddress[] iaArr = java.net.InetAddress.getAllByName (address);
251                         if (iaArr != null && iaArr.Length > 0)
252                                 h_name = iaArr[0].getHostName ();
253                         if (iaArr != null && iaArr.Length > 0) {
254                                 h_addrlist = new String[iaArr.Length];
255                                 for (int i = 0; i < h_addrlist.Length; i++)
256                                         h_addrlist[i] = iaArr[i].getHostAddress ();
257                         }
258 #else
259                         bool ret = GetHostByAddr_internal(address, out h_name, out h_aliases, out h_addrlist);
260                         if (!ret)
261                                 throw new SocketException(11001);
262 #endif
263                         return (hostent_to_IPHostEntry (h_name, h_aliases, h_addrlist));
264                         
265                 }
266
267 #if NET_2_0
268                 public static IPHostEntry GetHostEntry (string hostNameOrAddress)
269                 {
270                         if (hostNameOrAddress == null)
271                                 throw new ArgumentNullException ("hostNameOrAddress");
272
273                         if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "")
274                                 hostNameOrAddress = "127.0.0.1";
275                         IPAddress addr;
276                         if (IPAddress.TryParse (hostNameOrAddress, out addr))
277                                 return GetHostEntry (addr);
278                         else
279                                 return GetHostByName (hostNameOrAddress);
280                 }
281
282                 public static IPHostEntry GetHostEntry (IPAddress address)
283                 {
284                         if (address == null)
285                                 throw new ArgumentNullException ("address");
286
287                         return GetHostByAddressFromString (address.ToString (), false);
288                 }
289
290                 public static IPAddress [] GetHostAddresses (string hostNameOrAddress)
291                 {
292                         if (hostNameOrAddress == null)
293                                 throw new ArgumentNullException ("hostNameOrAddress");
294
295                         if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "")
296                                 hostNameOrAddress = "127.0.0.1";
297
298                         IPAddress addr;
299                         if (IPAddress.TryParse (hostNameOrAddress, out addr))
300                         {
301                                 return new IPAddress[1] { addr };
302                         }
303                         else
304                         {
305                           
306                                 return GetHostEntry (hostNameOrAddress).AddressList;
307                         }
308                 }
309 #endif
310
311 #if NET_2_0
312                 [Obsolete ("Use GetHostEntry instead")]
313 #endif
314                 public static IPHostEntry GetHostByName (string hostName)
315                 {
316                         if (hostName == null)
317                                 throw new ArgumentNullException ();
318 #if TARGET_JVM
319                         if (hostName.Length == 0)
320                                 hostName = "localhost";
321
322                         java.net.InetAddress[] iaArr = java.net.InetAddress.getAllByName (hostName);
323                         IPHostEntry host = new IPHostEntry ();
324                         if (iaArr != null && iaArr.Length > 0) {
325
326                                 host.HostName = iaArr[0].getHostName ();
327
328                                 IPAddress[] ipArr = new IPAddress[iaArr.Length];
329                                 for (int i = 0; i < iaArr.Length; i++)
330                                         ipArr[i] = IPAddress.Parse (iaArr[i].getHostAddress ());
331
332                                 host.AddressList = ipArr;
333
334                         }
335                         return host;
336 #else
337                         string h_name;
338                         string[] h_aliases, h_addrlist;
339
340                         bool ret = GetHostByName_internal(hostName, out h_name,
341                                 out h_aliases,
342                                 out h_addrlist);
343                         if (ret == false)
344                                 throw new SocketException(11001);
345
346                         return(hostent_to_IPHostEntry(h_name, h_aliases,
347                                 h_addrlist));
348 #endif
349                 }
350
351                 public static string GetHostName ()
352                 {
353 #if TARGET_JVM
354                         return java.net.InetAddress.getLocalHost ().getHostName ();
355 #else
356                         string hostName;
357
358                         bool ret = GetHostName_internal(out hostName);
359
360                         if (ret == false)
361                                 throw new SocketException(11001);
362
363                         return hostName;
364 #endif
365                 }
366
367 #if NET_2_0
368                 [Obsolete ("Use GetHostEntry instead")]
369 #endif
370                 public static IPHostEntry Resolve(string hostName) 
371                 {
372                         if (hostName == null)
373                                 throw new ArgumentNullException ("hostName");
374
375                         IPHostEntry ret = null;
376
377                         try {
378                                 ret =  GetHostByAddress(hostName);
379                         }
380                         catch{}
381
382                         if(ret == null)
383                                 ret =  GetHostByName(hostName);
384
385                         return ret;
386                 }
387         }
388 }
389