4 // Mads Pultz (mpultz@diku.dk)
5 // Lawrence Pit (loz@cable.a2000.nl)
7 // Author: Mads Pultz (mpultz@diku.dk)
8 // Lawrence Pit (loz@cable.a2000.nl)
9 // Marek Safar (marek.safar@gmail.com)
10 // Gonzalo Paniagua Javier (gonzalo.mono@gmail.com)
12 // (C) Mads Pultz, 2001
13 // Copyright (c) 2011 Novell, Inc.
14 // Copyright (c) 2011 Xamarin, Inc.
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 using System.Net.Sockets;
40 using System.Collections;
41 using System.Threading;
42 using System.Runtime.CompilerServices;
43 using System.Runtime.Remoting.Messaging;
44 using System.Threading.Tasks;
50 namespace System.Net {
51 public static class Dns {
53 static bool use_mono_dns;
54 static SimpleResolver resolver;
59 System.Net.Sockets.Socket.CheckProtocolSupport();
62 if (Environment.GetEnvironmentVariable ("MONO_DNS") != null) {
63 resolver = new SimpleResolver ();
70 internal static bool UseMonoDns {
71 get { return use_mono_dns; }
75 private delegate IPHostEntry GetHostByNameCallback (string hostName);
76 private delegate IPHostEntry ResolveCallback (string hostName);
77 private delegate IPHostEntry GetHostEntryNameCallback (string hostName);
78 private delegate IPHostEntry GetHostEntryIPCallback (IPAddress hostAddress);
79 private delegate IPAddress [] GetHostAddressesCallback (string hostName);
82 static void OnCompleted (object sender, SimpleResolverEventArgs e)
84 DnsAsyncResult ares = (DnsAsyncResult) e.UserToken;
85 IPHostEntry entry = e.HostEntry;
86 if (entry == null || e.ResolverError != 0) {
87 ares.SetCompleted (false, new Exception ("Error: " + e.ResolverError));
90 ares.SetCompleted (false, entry);
93 static IAsyncResult BeginAsyncCallAddresses (string host, AsyncCallback callback, object state)
95 SimpleResolverEventArgs e = new SimpleResolverEventArgs ();
96 e.Completed += OnCompleted;
98 DnsAsyncResult ares = new DnsAsyncResult (callback, state);
100 if (resolver.GetHostAddressesAsync (e) == false)
101 ares.SetCompleted (true, e.HostEntry); // Completed synchronously
105 static IAsyncResult BeginAsyncCall (string host, AsyncCallback callback, object state)
107 SimpleResolverEventArgs e = new SimpleResolverEventArgs ();
108 e.Completed += OnCompleted;
110 DnsAsyncResult ares = new DnsAsyncResult (callback, state);
112 if (resolver.GetHostEntryAsync (e) == false)
113 ares.SetCompleted (true, e.HostEntry); // Completed synchronously
117 static IPHostEntry EndAsyncCall (DnsAsyncResult ares)
120 throw new ArgumentException ("Invalid asyncResult");
121 if (!ares.IsCompleted)
122 ares.AsyncWaitHandle.WaitOne ();
123 if (ares.Exception != null)
124 throw ares.Exception;
125 IPHostEntry entry = ares.HostEntry;
126 if (entry == null || entry.AddressList == null || entry.AddressList.Length == 0)
127 Error_11001 (entry.HostName);
132 [Obsolete ("Use BeginGetHostEntry instead")]
133 public static IAsyncResult BeginGetHostByName (string hostName, AsyncCallback requestCallback, object stateObject)
135 if (hostName == null)
136 throw new ArgumentNullException ("hostName");
140 return BeginAsyncCall (hostName, requestCallback, stateObject);
143 GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
144 return c.BeginInvoke (hostName, requestCallback, stateObject);
147 [Obsolete ("Use BeginGetHostEntry instead")]
148 public static IAsyncResult BeginResolve (string hostName, AsyncCallback requestCallback, object stateObject)
150 if (hostName == null)
151 throw new ArgumentNullException ("hostName");
155 return BeginAsyncCall (hostName, requestCallback, stateObject);
158 ResolveCallback c = new ResolveCallback (Resolve);
159 return c.BeginInvoke (hostName, requestCallback, stateObject);
162 public static IAsyncResult BeginGetHostAddresses (string hostNameOrAddress, AsyncCallback requestCallback, object state)
164 if (hostNameOrAddress == null)
165 throw new ArgumentNullException ("hostName");
166 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
167 throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
168 "and ::0 (IPv6) are unspecified addresses. You " +
169 "cannot use them as target address.",
170 "hostNameOrAddress");
174 return BeginAsyncCallAddresses (hostNameOrAddress, requestCallback, state);
177 GetHostAddressesCallback c = new GetHostAddressesCallback (GetHostAddresses);
178 return c.BeginInvoke (hostNameOrAddress, requestCallback, state);
181 public static IAsyncResult BeginGetHostEntry (string hostNameOrAddress, AsyncCallback requestCallback, object stateObject)
183 if (hostNameOrAddress == null)
184 throw new ArgumentNullException ("hostName");
185 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
186 throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
187 "and ::0 (IPv6) are unspecified addresses. You " +
188 "cannot use them as target address.",
189 "hostNameOrAddress");
193 return BeginAsyncCall (hostNameOrAddress, requestCallback, stateObject);
196 GetHostEntryNameCallback c = new GetHostEntryNameCallback (GetHostEntry);
197 return c.BeginInvoke (hostNameOrAddress, requestCallback, stateObject);
200 public static IAsyncResult BeginGetHostEntry (IPAddress address, AsyncCallback requestCallback, object stateObject)
203 throw new ArgumentNullException ("address");
207 return BeginAsyncCall (address.ToString (), requestCallback, stateObject);
210 GetHostEntryIPCallback c = new GetHostEntryIPCallback (GetHostEntry);
211 return c.BeginInvoke (address, requestCallback, stateObject);
214 [Obsolete ("Use EndGetHostEntry instead")]
215 public static IPHostEntry EndGetHostByName (IAsyncResult asyncResult)
217 if (asyncResult == null)
218 throw new ArgumentNullException ("asyncResult");
222 return EndAsyncCall (asyncResult as DnsAsyncResult);
225 AsyncResult async = (AsyncResult) asyncResult;
226 GetHostByNameCallback cb = (GetHostByNameCallback) async.AsyncDelegate;
227 return cb.EndInvoke(asyncResult);
230 [Obsolete ("Use EndGetHostEntry instead")]
231 public static IPHostEntry EndResolve (IAsyncResult asyncResult)
233 if (asyncResult == null)
234 throw new ArgumentNullException ("asyncResult");
238 return EndAsyncCall (asyncResult as DnsAsyncResult);
241 AsyncResult async = (AsyncResult) asyncResult;
242 ResolveCallback cb = (ResolveCallback) async.AsyncDelegate;
243 return cb.EndInvoke(asyncResult);
246 public static IPAddress [] EndGetHostAddresses (IAsyncResult asyncResult)
248 if (asyncResult == null)
249 throw new ArgumentNullException ("asyncResult");
253 IPHostEntry entry = EndAsyncCall (asyncResult as DnsAsyncResult);
256 return entry.AddressList;
260 AsyncResult async = (AsyncResult) asyncResult;
261 GetHostAddressesCallback cb = (GetHostAddressesCallback) async.AsyncDelegate;
262 return cb.EndInvoke(asyncResult);
265 public static IPHostEntry EndGetHostEntry (IAsyncResult asyncResult)
267 if (asyncResult == null)
268 throw new ArgumentNullException ("asyncResult");
272 return EndAsyncCall (asyncResult as DnsAsyncResult);
275 AsyncResult async = (AsyncResult) asyncResult;
276 if (async.AsyncDelegate is GetHostEntryIPCallback)
277 return ((GetHostEntryIPCallback) async.AsyncDelegate).EndInvoke (asyncResult);
278 GetHostEntryNameCallback cb = (GetHostEntryNameCallback) async.AsyncDelegate;
279 return cb.EndInvoke(asyncResult);
283 [MethodImplAttribute(MethodImplOptions.InternalCall)]
284 private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
286 [MethodImplAttribute(MethodImplOptions.InternalCall)]
287 private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
289 [MethodImplAttribute(MethodImplOptions.InternalCall)]
290 private extern static bool GetHostName_internal(out string h_name);
292 static void Error_11001 (string hostName)
294 throw new SocketException(11001, string.Format ("Could not resolve host '{0}'", hostName));
298 private static IPHostEntry hostent_to_IPHostEntry(string originalHostName, string h_name, string[] h_aliases, string[] h_addrlist)
300 IPHostEntry he = new IPHostEntry();
301 ArrayList addrlist = new ArrayList();
303 he.HostName = h_name;
304 he.Aliases = h_aliases;
305 for(int i=0; i<h_addrlist.Length; i++) {
307 IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
309 if( (Socket.SupportsIPv6 && newAddress.AddressFamily == AddressFamily.InterNetworkV6) ||
310 (Socket.SupportsIPv4 && newAddress.AddressFamily == AddressFamily.InterNetwork) )
311 addrlist.Add(newAddress);
312 } catch (ArgumentNullException) {
313 /* Ignore this, as the
314 * internal call might have
315 * left some blank entries at
316 * the end of the array
321 if(addrlist.Count == 0)
322 Error_11001 (originalHostName);
324 he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
328 [Obsolete ("Use GetHostEntry instead")]
329 public static IPHostEntry GetHostByAddress(IPAddress address)
332 throw new ArgumentNullException ("address");
334 return GetHostByAddressFromString (address.ToString (), false);
337 [Obsolete ("Use GetHostEntry instead")]
338 public static IPHostEntry GetHostByAddress(string address)
341 throw new ArgumentNullException ("address");
343 return GetHostByAddressFromString (address, true);
346 static IPHostEntry GetHostByAddressFromString (string address, bool parse)
348 // Undocumented MS behavior: when called with IF_ANY,
349 // this should return the local host
350 if (address.Equals ("0.0.0.0")) {
351 address = "127.0.0.1";
355 // Must check the IP format, might send an exception if invalid string.
357 IPAddress.Parse (address);
360 string[] h_aliases, h_addrlist;
361 bool ret = GetHostByAddr_internal(address, out h_name, out h_aliases, out h_addrlist);
363 Error_11001 (address);
364 return (hostent_to_IPHostEntry (address, h_name, h_aliases, h_addrlist));
368 public static IPHostEntry GetHostEntry (string hostNameOrAddress)
370 if (hostNameOrAddress == null)
371 throw new ArgumentNullException ("hostNameOrAddress");
372 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
373 throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
374 "and ::0 (IPv6) are unspecified addresses. You " +
375 "cannot use them as target address.",
376 "hostNameOrAddress");
379 if (hostNameOrAddress.Length > 0 && IPAddress.TryParse (hostNameOrAddress, out addr))
380 return GetHostEntry (addr);
382 return GetHostByName (hostNameOrAddress);
385 public static IPHostEntry GetHostEntry (IPAddress address)
388 throw new ArgumentNullException ("address");
390 return GetHostByAddressFromString (address.ToString (), false);
393 public static IPAddress [] GetHostAddresses (string hostNameOrAddress)
395 if (hostNameOrAddress == null)
396 throw new ArgumentNullException ("hostNameOrAddress");
398 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
399 throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
400 "and ::0 (IPv6) are unspecified addresses. You " +
401 "cannot use them as target address.",
402 "hostNameOrAddress");
405 if (hostNameOrAddress.Length > 0 && IPAddress.TryParse (hostNameOrAddress, out addr))
406 return new IPAddress[1] { addr };
408 return GetHostEntry (hostNameOrAddress).AddressList;
411 [Obsolete ("Use GetHostEntry instead")]
412 public static IPHostEntry GetHostByName (string hostName)
414 if (hostName == null)
415 throw new ArgumentNullException ("hostName");
417 string[] h_aliases, h_addrlist;
419 bool ret = GetHostByName_internal(hostName, out h_name, out h_aliases, out h_addrlist);
421 Error_11001 (hostName);
423 return(hostent_to_IPHostEntry(hostName, h_name, h_aliases, h_addrlist));
426 public static string GetHostName ()
430 bool ret = GetHostName_internal(out hostName);
433 Error_11001 (hostName);
438 [Obsolete ("Use GetHostEntry instead")]
439 public static IPHostEntry Resolve(string hostName)
441 if (hostName == null)
442 throw new ArgumentNullException ("hostName");
444 IPHostEntry ret = null;
447 ret = GetHostByAddress(hostName);
452 ret = GetHostByName(hostName);
457 public static Task<IPAddress[]> GetHostAddressesAsync (string hostNameOrAddress)
459 return Task<IPAddress[]>.Factory.FromAsync (BeginGetHostAddresses, EndGetHostAddresses, hostNameOrAddress, null);
462 public static Task<IPHostEntry> GetHostEntryAsync (IPAddress address)
464 return Task<IPHostEntry>.Factory.FromAsync (BeginGetHostEntry, EndGetHostEntry, address, null);
467 public static Task<IPHostEntry> GetHostEntryAsync (string hostNameOrAddress)
469 return Task<IPHostEntry>.Factory.FromAsync (BeginGetHostEntry, EndGetHostEntry, hostNameOrAddress, null);