3 // Author: Mads Pultz (mpultz@diku.dk)
4 // Author: Lawrence Pit (loz@cable.a2000.nl)
6 // (C) Mads Pultz, 2001
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Net.Sockets;
32 using System.Collections;
33 using System.Threading;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.Remoting.Messaging;
37 namespace System.Net {
39 public static class Dns {
41 public sealed class Dns {
48 System.Net.Sockets.Socket.CheckProtocolSupport();
51 #if !MOONLIGHT // global remove of async methods
53 private delegate IPHostEntry GetHostByNameCallback (string hostName);
54 private delegate IPHostEntry ResolveCallback (string hostName);
56 private delegate IPHostEntry GetHostEntryNameCallback (string hostName);
57 private delegate IPHostEntry GetHostEntryIPCallback (IPAddress hostAddress);
58 private delegate IPAddress [] GetHostAddressesCallback (string hostName);
62 [Obsolete ("Use BeginGetHostEntry instead")]
64 public static IAsyncResult BeginGetHostByName (string hostName,
65 AsyncCallback requestCallback, object stateObject)
68 throw new ArgumentNullException ("hostName");
70 GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
71 return c.BeginInvoke (hostName, requestCallback, stateObject);
75 [Obsolete ("Use BeginGetHostEntry instead")]
77 public static IAsyncResult BeginResolve (string hostName,
78 AsyncCallback requestCallback, object stateObject)
81 throw new ArgumentNullException ("hostName");
83 ResolveCallback c = new ResolveCallback (Resolve);
84 return c.BeginInvoke (hostName, requestCallback, stateObject);
88 public static IAsyncResult BeginGetHostAddresses (string hostNameOrAddress,
89 AsyncCallback requestCallback, object stateObject)
91 if (hostNameOrAddress == null)
92 throw new ArgumentNullException ("hostName");
93 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
94 throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
95 "and ::0 (IPv6) are unspecified addresses. You " +
96 "cannot use them as target address.",
99 GetHostAddressesCallback c = new GetHostAddressesCallback (GetHostAddresses);
100 return c.BeginInvoke (hostNameOrAddress, requestCallback, stateObject);
103 public static IAsyncResult BeginGetHostEntry (string hostNameOrAddress,
104 AsyncCallback requestCallback, object stateObject)
106 if (hostNameOrAddress == null)
107 throw new ArgumentNullException ("hostName");
108 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
109 throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
110 "and ::0 (IPv6) are unspecified addresses. You " +
111 "cannot use them as target address.",
112 "hostNameOrAddress");
114 GetHostEntryNameCallback c = new GetHostEntryNameCallback (GetHostEntry);
115 return c.BeginInvoke (hostNameOrAddress, requestCallback, stateObject);
118 public static IAsyncResult BeginGetHostEntry (IPAddress address,
119 AsyncCallback requestCallback, object stateObject)
122 throw new ArgumentNullException ("address");
124 GetHostEntryIPCallback c = new GetHostEntryIPCallback (GetHostEntry);
125 return c.BeginInvoke (address, requestCallback, stateObject);
130 [Obsolete ("Use EndGetHostEntry instead")]
132 public static IPHostEntry EndGetHostByName (IAsyncResult asyncResult)
134 if (asyncResult == null)
135 throw new ArgumentNullException ("asyncResult");
137 AsyncResult async = (AsyncResult) asyncResult;
138 GetHostByNameCallback cb = (GetHostByNameCallback) async.AsyncDelegate;
139 return cb.EndInvoke(asyncResult);
143 [Obsolete ("Use EndGetHostEntry instead")]
145 public static IPHostEntry EndResolve (IAsyncResult asyncResult)
147 if (asyncResult == null)
148 throw new ArgumentNullException ("asyncResult");
149 AsyncResult async = (AsyncResult) asyncResult;
150 ResolveCallback cb = (ResolveCallback) async.AsyncDelegate;
151 return cb.EndInvoke(asyncResult);
156 public static IPAddress [] EndGetHostAddresses (IAsyncResult asyncResult)
158 if (asyncResult == null)
159 throw new ArgumentNullException ("asyncResult");
161 AsyncResult async = (AsyncResult) asyncResult;
162 GetHostAddressesCallback cb = (GetHostAddressesCallback) async.AsyncDelegate;
163 return cb.EndInvoke(asyncResult);
166 public static IPHostEntry EndGetHostEntry (IAsyncResult asyncResult)
168 if (asyncResult == null)
169 throw new ArgumentNullException ("asyncResult");
170 AsyncResult async = (AsyncResult) asyncResult;
172 if (async.AsyncDelegate is GetHostEntryIPCallback)
173 return ((GetHostEntryIPCallback) async.AsyncDelegate).EndInvoke (asyncResult);
175 GetHostEntryNameCallback cb = (GetHostEntryNameCallback) async.AsyncDelegate;
176 return cb.EndInvoke(asyncResult);
180 #endif // !MOONLIGHT: global remove of async methods
183 [MethodImplAttribute(MethodImplOptions.InternalCall)]
184 private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
186 [MethodImplAttribute(MethodImplOptions.InternalCall)]
187 private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
189 [MethodImplAttribute(MethodImplOptions.InternalCall)]
190 private extern static bool GetHostName_internal(out string h_name);
193 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist)
195 IPHostEntry he = new IPHostEntry();
196 ArrayList addrlist = new ArrayList();
198 he.HostName = h_name;
199 he.Aliases = h_aliases;
200 for(int i=0; i<h_addrlist.Length; i++) {
202 IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
204 if( (Socket.SupportsIPv6 && newAddress.AddressFamily == AddressFamily.InterNetworkV6) ||
205 (Socket.SupportsIPv4 && newAddress.AddressFamily == AddressFamily.InterNetwork) )
206 addrlist.Add(newAddress);
207 } catch (ArgumentNullException) {
208 /* Ignore this, as the
209 * internal call might have
210 * left some blank entries at
211 * the end of the array
216 if(addrlist.Count == 0)
217 throw new SocketException(11001);
219 he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
224 [Obsolete ("Use GetHostEntry instead")]
226 public static IPHostEntry GetHostByAddress(IPAddress address)
229 throw new ArgumentNullException ("address");
231 return GetHostByAddressFromString (address.ToString (), false);
235 [Obsolete ("Use GetHostEntry instead")]
237 public static IPHostEntry GetHostByAddress(string address)
240 throw new ArgumentNullException ("address");
242 return GetHostByAddressFromString (address, true);
245 static IPHostEntry GetHostByAddressFromString (string address, bool parse)
247 // Undocumented MS behavior: when called with IF_ANY,
248 // this should return the local host
249 if (address.Equals ("0.0.0.0")) {
250 address = "127.0.0.1";
254 // Must check the IP format, might send an exception if invalid string.
256 IPAddress.Parse (address);
259 string[] h_aliases, h_addrlist;
265 java.net.InetAddress[] iaArr =
266 java.net.InetAddress.getAllByName(address);
267 if (iaArr != null && iaArr.Length > 0)
268 h_name = iaArr[0].getHostName();
269 if (iaArr != null && iaArr.Length > 0)
271 h_addrlist = new String[iaArr.Length];
272 for (int i = 0; i < h_addrlist.Length; i++)
273 h_addrlist[i] = iaArr[i].getHostAddress();
275 } catch (java.net.UnknownHostException jUHE) {
276 throw new SocketException((int)SocketError.HostNotFound, jUHE.Message);
279 bool ret = GetHostByAddr_internal(address, out h_name, out h_aliases, out h_addrlist);
281 throw new SocketException(11001);
283 return (hostent_to_IPHostEntry (h_name, h_aliases, h_addrlist));
292 static IPHostEntry GetHostEntry (string hostNameOrAddress)
294 if (hostNameOrAddress == null)
295 throw new ArgumentNullException ("hostNameOrAddress");
296 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
297 throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
298 "and ::0 (IPv6) are unspecified addresses. You " +
299 "cannot use them as target address.",
300 "hostNameOrAddress");
303 if (hostNameOrAddress.Length > 0 && IPAddress.TryParse (hostNameOrAddress, out addr))
304 return GetHostEntry (addr);
306 return GetHostByName (hostNameOrAddress);
314 static IPHostEntry GetHostEntry (IPAddress address)
317 throw new ArgumentNullException ("address");
319 return GetHostByAddressFromString (address.ToString (), false);
327 static IPAddress [] GetHostAddresses (string hostNameOrAddress)
329 if (hostNameOrAddress == null)
330 throw new ArgumentNullException ("hostNameOrAddress");
332 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
333 throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
334 "and ::0 (IPv6) are unspecified addresses. You " +
335 "cannot use them as target address.",
336 "hostNameOrAddress");
339 if (hostNameOrAddress.Length > 0 && IPAddress.TryParse (hostNameOrAddress, out addr))
340 return new IPAddress[1] { addr };
342 return GetHostEntry (hostNameOrAddress).AddressList;
346 [Obsolete ("Use GetHostEntry instead")]
348 public static IPHostEntry GetHostByName (string hostName)
350 if (hostName == null)
351 throw new ArgumentNullException ("hostName");
353 if (hostName.Length == 0)
354 hostName = "localhost";
356 java.net.InetAddress[] iaArr = java.net.InetAddress.getAllByName(hostName);
357 IPHostEntry host = new IPHostEntry();
358 if (iaArr != null && iaArr.Length > 0)
360 host.HostName = iaArr[0].getHostName();
361 IPAddress[] ipArr = new IPAddress[iaArr.Length];
362 for (int i = 0; i < iaArr.Length; i++)
363 ipArr[i] = IPAddress.Parse(iaArr[i].getHostAddress());
365 host.AddressList = ipArr;
368 } catch (java.net.UnknownHostException jUHE) {
369 throw new SocketException((int)SocketError.HostNotFound, jUHE.Message);
373 string[] h_aliases, h_addrlist;
375 bool ret = GetHostByName_internal(hostName, out h_name, out h_aliases, out h_addrlist);
377 throw new SocketException(11001);
379 return(hostent_to_IPHostEntry(h_name, h_aliases, h_addrlist));
383 public static string GetHostName ()
386 return java.net.InetAddress.getLocalHost ().getHostName ();
390 bool ret = GetHostName_internal(out hostName);
393 throw new SocketException(11001);
400 [Obsolete ("Use GetHostEntry instead")]
402 public static IPHostEntry Resolve(string hostName)
404 if (hostName == null)
405 throw new ArgumentNullException ("hostName");
407 IPHostEntry ret = null;
410 ret = GetHostByAddress(hostName);
415 ret = GetHostByName(hostName);