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 !NET_2_1 // 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 hostName,
89 AsyncCallback requestCallback, object stateObject)
92 throw new ArgumentNullException ("hostName");
94 GetHostAddressesCallback c = new GetHostAddressesCallback (GetHostAddresses);
95 return c.BeginInvoke (hostName, requestCallback, stateObject);
98 public static IAsyncResult BeginGetHostEntry (string hostNameOrAddress,
99 AsyncCallback requestCallback, object stateObject)
101 if (hostNameOrAddress == null)
102 throw new ArgumentNullException ("hostNameOrAddress");
104 GetHostEntryNameCallback c = new GetHostEntryNameCallback (GetHostEntry);
105 return c.BeginInvoke (hostNameOrAddress, requestCallback, stateObject);
108 public static IAsyncResult BeginGetHostEntry (IPAddress hostAddress,
109 AsyncCallback requestCallback, object stateObject)
111 if (hostAddress == null)
112 throw new ArgumentNullException ("hostAddress");
114 GetHostEntryIPCallback c = new GetHostEntryIPCallback (GetHostEntry);
115 return c.BeginInvoke (hostAddress, requestCallback, stateObject);
120 [Obsolete ("Use EndGetHostEntry instead")]
122 public static IPHostEntry EndGetHostByName (IAsyncResult asyncResult)
124 if (asyncResult == null)
125 throw new ArgumentNullException ("asyncResult");
127 AsyncResult async = (AsyncResult) asyncResult;
128 GetHostByNameCallback cb = (GetHostByNameCallback) async.AsyncDelegate;
129 return cb.EndInvoke(asyncResult);
133 [Obsolete ("Use EndGetHostEntry instead")]
135 public static IPHostEntry EndResolve (IAsyncResult asyncResult)
137 if (asyncResult == null)
138 throw new ArgumentNullException ("asyncResult");
139 AsyncResult async = (AsyncResult) asyncResult;
140 ResolveCallback cb = (ResolveCallback) async.AsyncDelegate;
141 return cb.EndInvoke(asyncResult);
146 public static IPAddress [] EndGetHostAddresses (IAsyncResult asyncResult)
148 if (asyncResult == null)
149 throw new ArgumentNullException ("asyncResult");
151 AsyncResult async = (AsyncResult) asyncResult;
152 GetHostAddressesCallback cb = (GetHostAddressesCallback) async.AsyncDelegate;
153 return cb.EndInvoke(asyncResult);
156 public static IPHostEntry EndGetHostEntry (IAsyncResult asyncResult)
158 if (asyncResult == null)
159 throw new ArgumentNullException ("asyncResult");
160 AsyncResult async = (AsyncResult) asyncResult;
162 if (async.AsyncDelegate is GetHostEntryIPCallback)
163 return ((GetHostEntryIPCallback) async.AsyncDelegate).EndInvoke (asyncResult);
165 GetHostEntryNameCallback cb = (GetHostEntryNameCallback) async.AsyncDelegate;
166 return cb.EndInvoke(asyncResult);
170 #endif // !NET_2_1: global remove of async methods
173 [MethodImplAttribute(MethodImplOptions.InternalCall)]
174 private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
176 [MethodImplAttribute(MethodImplOptions.InternalCall)]
177 private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
179 [MethodImplAttribute(MethodImplOptions.InternalCall)]
180 private extern static bool GetHostName_internal(out string h_name);
183 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist)
185 IPHostEntry he = new IPHostEntry();
186 ArrayList addrlist = new ArrayList();
188 he.HostName = h_name;
189 he.Aliases = h_aliases;
190 for(int i=0; i<h_addrlist.Length; i++) {
192 IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
194 if( (Socket.SupportsIPv6 && newAddress.AddressFamily == AddressFamily.InterNetworkV6) ||
195 (Socket.SupportsIPv4 && newAddress.AddressFamily == AddressFamily.InterNetwork) )
196 addrlist.Add(newAddress);
197 } catch (ArgumentNullException) {
198 /* Ignore this, as the
199 * internal call might have
200 * left some blank entries at
201 * the end of the array
206 if(addrlist.Count == 0)
207 throw new SocketException(11001);
209 he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
214 [Obsolete ("Use GetHostEntry instead")]
216 public static IPHostEntry GetHostByAddress(IPAddress address)
219 throw new ArgumentNullException ("address");
221 return GetHostByAddressFromString (address.ToString (), false);
225 [Obsolete ("Use GetHostEntry instead")]
227 public static IPHostEntry GetHostByAddress(string address)
230 throw new ArgumentNullException ("address");
232 return GetHostByAddressFromString (address, true);
235 static IPHostEntry GetHostByAddressFromString (string address, bool parse)
237 // Undocumented MS behavior: when called with IF_ANY,
238 // this should return the local host
239 if (address.Equals ("0.0.0.0")) {
240 address = "127.0.0.1";
244 // Must check the IP format, might send an exception if invalid string.
246 IPAddress.Parse (address);
249 string[] h_aliases, h_addrlist;
255 java.net.InetAddress[] iaArr =
256 java.net.InetAddress.getAllByName(address);
257 if (iaArr != null && iaArr.Length > 0)
258 h_name = iaArr[0].getHostName();
259 if (iaArr != null && iaArr.Length > 0)
261 h_addrlist = new String[iaArr.Length];
262 for (int i = 0; i < h_addrlist.Length; i++)
263 h_addrlist[i] = iaArr[i].getHostAddress();
265 } catch (java.net.UnknownHostException jUHE) {
266 throw new SocketException((int)SocketError.HostNotFound, jUHE.Message);
269 bool ret = GetHostByAddr_internal(address, out h_name, out h_aliases, out h_addrlist);
271 throw new SocketException(11001);
273 return (hostent_to_IPHostEntry (h_name, h_aliases, h_addrlist));
278 public static IPHostEntry GetHostEntry (string hostNameOrAddress)
280 if (hostNameOrAddress == null)
281 throw new ArgumentNullException ("hostNameOrAddress");
283 if (hostNameOrAddress == "0.0.0.0")
284 hostNameOrAddress = "";
286 if (hostNameOrAddress.Length > 0 && IPAddress.TryParse (hostNameOrAddress, out addr))
287 return GetHostEntry (addr);
289 return GetHostByName (hostNameOrAddress);
292 public static IPHostEntry GetHostEntry (IPAddress address)
295 throw new ArgumentNullException ("address");
297 return GetHostByAddressFromString (address.ToString (), false);
300 public static IPAddress [] GetHostAddresses (string hostNameOrAddress)
302 if (hostNameOrAddress == null)
303 throw new ArgumentNullException ("hostNameOrAddress");
305 if (hostNameOrAddress == "0.0.0.0")
306 hostNameOrAddress = "";
309 if (hostNameOrAddress.Length > 0 && IPAddress.TryParse (hostNameOrAddress, out addr))
310 return new IPAddress[1] { addr };
312 return GetHostEntry (hostNameOrAddress).AddressList;
317 [Obsolete ("Use GetHostEntry instead")]
319 public static IPHostEntry GetHostByName (string hostName)
321 if (hostName == null)
322 throw new ArgumentNullException ();
324 if (hostName.Length == 0)
325 hostName = "localhost";
327 java.net.InetAddress[] iaArr = java.net.InetAddress.getAllByName(hostName);
328 IPHostEntry host = new IPHostEntry();
329 if (iaArr != null && iaArr.Length > 0)
331 host.HostName = iaArr[0].getHostName();
332 IPAddress[] ipArr = new IPAddress[iaArr.Length];
333 for (int i = 0; i < iaArr.Length; i++)
334 ipArr[i] = IPAddress.Parse(iaArr[i].getHostAddress());
336 host.AddressList = ipArr;
339 } catch (java.net.UnknownHostException jUHE) {
340 throw new SocketException((int)SocketError.HostNotFound, jUHE.Message);
344 string[] h_aliases, h_addrlist;
346 bool ret = GetHostByName_internal(hostName, out h_name, out h_aliases, out h_addrlist);
348 throw new SocketException(11001);
350 return(hostent_to_IPHostEntry(h_name, h_aliases, h_addrlist));
354 public static string GetHostName ()
357 return java.net.InetAddress.getLocalHost ().getHostName ();
361 bool ret = GetHostName_internal(out hostName);
364 throw new SocketException(11001);
371 [Obsolete ("Use GetHostEntry instead")]
373 public static IPHostEntry Resolve(string hostName)
375 if (hostName == null)
376 throw new ArgumentNullException ("hostName");
378 IPHostEntry ret = null;
381 ret = GetHostByAddress(hostName);
386 ret = GetHostByName(hostName);