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 private delegate IPHostEntry GetHostByNameCallback (string hostName);
52 private delegate IPHostEntry ResolveCallback (string hostName);
54 private delegate IPHostEntry GetHostEntryNameCallback (string hostName);
55 private delegate IPHostEntry GetHostEntryIPCallback (IPAddress hostAddress);
56 private delegate IPAddress [] GetHostAddressesCallback (string hostName);
60 [Obsolete ("Use BeginGetHostEntry instead")]
62 public static IAsyncResult BeginGetHostByName (string hostName,
63 AsyncCallback requestCallback, object stateObject)
66 throw new ArgumentNullException ("hostName");
68 GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
69 return c.BeginInvoke (hostName, requestCallback, stateObject);
73 [Obsolete ("Use BeginGetHostEntry instead")]
75 public static IAsyncResult BeginResolve (string hostName,
76 AsyncCallback requestCallback, object stateObject)
79 throw new ArgumentNullException ("hostName");
81 ResolveCallback c = new ResolveCallback (Resolve);
82 return c.BeginInvoke (hostName, requestCallback, stateObject);
86 public static IAsyncResult BeginGetHostAddresses (string hostName,
87 AsyncCallback requestCallback, object stateObject)
90 throw new ArgumentNullException ("hostName");
92 GetHostAddressesCallback c = new GetHostAddressesCallback (GetHostAddresses);
93 return c.BeginInvoke (hostName, requestCallback, stateObject);
96 public static IAsyncResult BeginGetHostEntry (string hostNameOrAddress,
97 AsyncCallback requestCallback, object stateObject)
99 if (hostNameOrAddress == null)
100 throw new ArgumentNullException ("hostNameOrAddress");
102 GetHostEntryNameCallback c = new GetHostEntryNameCallback (GetHostEntry);
103 return c.BeginInvoke (hostNameOrAddress, requestCallback, stateObject);
106 public static IAsyncResult BeginGetHostEntry (IPAddress hostAddress,
107 AsyncCallback requestCallback, object stateObject)
109 if (hostAddress == null)
110 throw new ArgumentNullException ("hostAddress");
112 GetHostEntryIPCallback c = new GetHostEntryIPCallback (GetHostEntry);
113 return c.BeginInvoke (hostAddress, requestCallback, stateObject);
118 [Obsolete ("Use EndGetHostEntry instead")]
120 public static IPHostEntry EndGetHostByName (IAsyncResult asyncResult)
122 if (asyncResult == null)
123 throw new ArgumentNullException ("asyncResult");
125 AsyncResult async = (AsyncResult) asyncResult;
126 GetHostByNameCallback cb = (GetHostByNameCallback) async.AsyncDelegate;
127 return cb.EndInvoke(asyncResult);
131 [Obsolete ("Use EndGetHostEntry instead")]
133 public static IPHostEntry EndResolve (IAsyncResult asyncResult)
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);
144 public static IPAddress [] EndGetHostAddresses (IAsyncResult asyncResult)
146 if (asyncResult == null)
147 throw new ArgumentNullException ("asyncResult");
149 AsyncResult async = (AsyncResult) asyncResult;
150 GetHostAddressesCallback cb = (GetHostAddressesCallback) async.AsyncDelegate;
151 return cb.EndInvoke(asyncResult);
154 public static IPHostEntry EndGetHostEntry (IAsyncResult asyncResult)
156 if (asyncResult == null)
157 throw new ArgumentNullException ("asyncResult");
158 AsyncResult async = (AsyncResult) asyncResult;
160 if (async.AsyncDelegate is GetHostEntryIPCallback)
161 return ((GetHostEntryIPCallback) async.AsyncDelegate).EndInvoke (asyncResult);
163 GetHostEntryNameCallback cb = (GetHostEntryNameCallback) async.AsyncDelegate;
164 return cb.EndInvoke(asyncResult);
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);
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);
175 [MethodImplAttribute(MethodImplOptions.InternalCall)]
176 private extern static bool GetHostName_internal(out string h_name);
179 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist)
181 IPHostEntry he = new IPHostEntry();
182 ArrayList addrlist = new ArrayList();
184 he.HostName = h_name;
185 he.Aliases = h_aliases;
186 for(int i=0; i<h_addrlist.Length; i++) {
188 IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
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
202 if(addrlist.Count == 0)
203 throw new SocketException(11001);
205 he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
210 [Obsolete ("Use GetHostEntry instead")]
212 public static IPHostEntry GetHostByAddress(IPAddress address)
215 throw new ArgumentNullException ("address");
217 return GetHostByAddressFromString (address.ToString (), false);
221 [Obsolete ("Use GetHostEntry instead")]
223 public static IPHostEntry GetHostByAddress(string address)
226 throw new ArgumentNullException ("address");
228 return GetHostByAddressFromString (address, true);
231 static IPHostEntry GetHostByAddressFromString (string address, bool parse)
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";
240 // Must check the IP format, might send an exception if invalid string.
242 IPAddress.Parse (address);
245 string[] h_aliases, h_addrlist;
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 ();
259 bool ret = GetHostByAddr_internal(address, out h_name, out h_aliases, out h_addrlist);
261 throw new SocketException(11001);
263 return (hostent_to_IPHostEntry (h_name, h_aliases, h_addrlist));
268 public static IPHostEntry GetHostEntry (string hostNameOrAddress)
270 if (hostNameOrAddress == null)
271 throw new ArgumentNullException ("hostNameOrAddress");
273 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "")
274 hostNameOrAddress = "127.0.0.1";
276 if (IPAddress.TryParse (hostNameOrAddress, out addr))
277 return GetHostEntry (addr);
279 return GetHostByName (hostNameOrAddress);
282 public static IPHostEntry GetHostEntry (IPAddress address)
285 throw new ArgumentNullException ("address");
287 return GetHostByAddressFromString (address.ToString (), false);
290 public static IPAddress [] GetHostAddresses (string hostNameOrAddress)
292 if (hostNameOrAddress == null)
293 throw new ArgumentNullException ("hostNameOrAddress");
295 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "")
296 hostNameOrAddress = "127.0.0.1";
299 if (IPAddress.TryParse (hostNameOrAddress, out addr))
301 return new IPAddress[1] { addr };
306 return GetHostEntry (hostNameOrAddress).AddressList;
312 [Obsolete ("Use GetHostEntry instead")]
314 public static IPHostEntry GetHostByName (string hostName)
316 if (hostName == null)
317 throw new ArgumentNullException ();
319 if (hostName.Length == 0)
320 hostName = "localhost";
322 java.net.InetAddress[] iaArr = java.net.InetAddress.getAllByName (hostName);
323 IPHostEntry host = new IPHostEntry ();
324 if (iaArr != null && iaArr.Length > 0) {
326 host.HostName = iaArr[0].getHostName ();
328 IPAddress[] ipArr = new IPAddress[iaArr.Length];
329 for (int i = 0; i < iaArr.Length; i++)
330 ipArr[i] = IPAddress.Parse (iaArr[i].getHostAddress ());
332 host.AddressList = ipArr;
338 string[] h_aliases, h_addrlist;
340 bool ret = GetHostByName_internal(hostName, out h_name,
344 throw new SocketException(11001);
346 return(hostent_to_IPHostEntry(h_name, h_aliases,
351 public static string GetHostName ()
354 return java.net.InetAddress.getLocalHost ().getHostName ();
358 bool ret = GetHostName_internal(out hostName);
361 throw new SocketException(11001);
368 [Obsolete ("Use GetHostEntry instead")]
370 public static IPHostEntry Resolve(string hostName)
372 if (hostName == null)
373 throw new ArgumentNullException ("hostName");
375 IPHostEntry ret = null;
378 ret = GetHostByAddress(hostName);
383 ret = GetHostByName(hostName);