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 {
38 public static class Dns {
41 System.Net.Sockets.Socket.CheckProtocolSupport();
44 #if !MOONLIGHT // global remove of async methods
46 private delegate IPHostEntry GetHostByNameCallback (string hostName);
47 private delegate IPHostEntry ResolveCallback (string hostName);
48 private delegate IPHostEntry GetHostEntryNameCallback (string hostName);
49 private delegate IPHostEntry GetHostEntryIPCallback (IPAddress hostAddress);
50 private delegate IPAddress [] GetHostAddressesCallback (string hostName);
52 [Obsolete ("Use BeginGetHostEntry instead")]
53 public static IAsyncResult BeginGetHostByName (string hostName,
54 AsyncCallback requestCallback, object stateObject)
57 throw new ArgumentNullException ("hostName");
59 GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
60 return c.BeginInvoke (hostName, requestCallback, stateObject);
63 [Obsolete ("Use BeginGetHostEntry instead")]
64 public static IAsyncResult BeginResolve (string hostName,
65 AsyncCallback requestCallback, object stateObject)
68 throw new ArgumentNullException ("hostName");
70 ResolveCallback c = new ResolveCallback (Resolve);
71 return c.BeginInvoke (hostName, requestCallback, stateObject);
74 public static IAsyncResult BeginGetHostAddresses (string hostNameOrAddress,
75 AsyncCallback requestCallback, object stateObject)
77 if (hostNameOrAddress == null)
78 throw new ArgumentNullException ("hostName");
79 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
80 throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
81 "and ::0 (IPv6) are unspecified addresses. You " +
82 "cannot use them as target address.",
85 GetHostAddressesCallback c = new GetHostAddressesCallback (GetHostAddresses);
86 return c.BeginInvoke (hostNameOrAddress, requestCallback, stateObject);
89 public static IAsyncResult BeginGetHostEntry (string hostNameOrAddress,
90 AsyncCallback requestCallback, object stateObject)
92 if (hostNameOrAddress == null)
93 throw new ArgumentNullException ("hostName");
94 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
95 throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
96 "and ::0 (IPv6) are unspecified addresses. You " +
97 "cannot use them as target address.",
100 GetHostEntryNameCallback c = new GetHostEntryNameCallback (GetHostEntry);
101 return c.BeginInvoke (hostNameOrAddress, requestCallback, stateObject);
104 public static IAsyncResult BeginGetHostEntry (IPAddress address,
105 AsyncCallback requestCallback, object stateObject)
108 throw new ArgumentNullException ("address");
110 GetHostEntryIPCallback c = new GetHostEntryIPCallback (GetHostEntry);
111 return c.BeginInvoke (address, requestCallback, stateObject);
114 [Obsolete ("Use EndGetHostEntry instead")]
115 public static IPHostEntry EndGetHostByName (IAsyncResult asyncResult)
117 if (asyncResult == null)
118 throw new ArgumentNullException ("asyncResult");
120 AsyncResult async = (AsyncResult) asyncResult;
121 GetHostByNameCallback cb = (GetHostByNameCallback) async.AsyncDelegate;
122 return cb.EndInvoke(asyncResult);
125 [Obsolete ("Use EndGetHostEntry instead")]
126 public static IPHostEntry EndResolve (IAsyncResult asyncResult)
128 if (asyncResult == null)
129 throw new ArgumentNullException ("asyncResult");
130 AsyncResult async = (AsyncResult) asyncResult;
131 ResolveCallback cb = (ResolveCallback) async.AsyncDelegate;
132 return cb.EndInvoke(asyncResult);
135 public static IPAddress [] EndGetHostAddresses (IAsyncResult asyncResult)
137 if (asyncResult == null)
138 throw new ArgumentNullException ("asyncResult");
140 AsyncResult async = (AsyncResult) asyncResult;
141 GetHostAddressesCallback cb = (GetHostAddressesCallback) async.AsyncDelegate;
142 return cb.EndInvoke(asyncResult);
145 public static IPHostEntry EndGetHostEntry (IAsyncResult asyncResult)
147 if (asyncResult == null)
148 throw new ArgumentNullException ("asyncResult");
149 AsyncResult async = (AsyncResult) asyncResult;
150 if (async.AsyncDelegate is GetHostEntryIPCallback)
151 return ((GetHostEntryIPCallback) async.AsyncDelegate).EndInvoke (asyncResult);
152 GetHostEntryNameCallback cb = (GetHostEntryNameCallback) async.AsyncDelegate;
153 return cb.EndInvoke(asyncResult);
156 #endif // !MOONLIGHT: global remove of async methods
159 [MethodImplAttribute(MethodImplOptions.InternalCall)]
160 private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
162 [MethodImplAttribute(MethodImplOptions.InternalCall)]
163 private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
165 [MethodImplAttribute(MethodImplOptions.InternalCall)]
166 private extern static bool GetHostName_internal(out string h_name);
169 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist)
171 IPHostEntry he = new IPHostEntry();
172 ArrayList addrlist = new ArrayList();
174 he.HostName = h_name;
175 he.Aliases = h_aliases;
176 for(int i=0; i<h_addrlist.Length; i++) {
178 IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
180 if( (Socket.SupportsIPv6 && newAddress.AddressFamily == AddressFamily.InterNetworkV6) ||
181 (Socket.SupportsIPv4 && newAddress.AddressFamily == AddressFamily.InterNetwork) )
182 addrlist.Add(newAddress);
183 } catch (ArgumentNullException) {
184 /* Ignore this, as the
185 * internal call might have
186 * left some blank entries at
187 * the end of the array
192 if(addrlist.Count == 0)
193 throw new SocketException(11001);
195 he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
199 [Obsolete ("Use GetHostEntry instead")]
200 public static IPHostEntry GetHostByAddress(IPAddress address)
203 throw new ArgumentNullException ("address");
205 return GetHostByAddressFromString (address.ToString (), false);
208 [Obsolete ("Use GetHostEntry instead")]
209 public static IPHostEntry GetHostByAddress(string address)
212 throw new ArgumentNullException ("address");
214 return GetHostByAddressFromString (address, true);
217 static IPHostEntry GetHostByAddressFromString (string address, bool parse)
219 // Undocumented MS behavior: when called with IF_ANY,
220 // this should return the local host
221 if (address.Equals ("0.0.0.0")) {
222 address = "127.0.0.1";
226 // Must check the IP format, might send an exception if invalid string.
228 IPAddress.Parse (address);
231 string[] h_aliases, h_addrlist;
237 java.net.InetAddress[] iaArr =
238 java.net.InetAddress.getAllByName(address);
239 if (iaArr != null && iaArr.Length > 0)
240 h_name = iaArr[0].getHostName();
241 if (iaArr != null && iaArr.Length > 0)
243 h_addrlist = new String[iaArr.Length];
244 for (int i = 0; i < h_addrlist.Length; i++)
245 h_addrlist[i] = iaArr[i].getHostAddress();
247 } catch (java.net.UnknownHostException jUHE) {
248 throw new SocketException((int)SocketError.HostNotFound, jUHE.Message);
251 bool ret = GetHostByAddr_internal(address, out h_name, out h_aliases, out h_addrlist);
253 throw new SocketException(11001);
255 return (hostent_to_IPHostEntry (h_name, h_aliases, h_addrlist));
259 public static IPHostEntry GetHostEntry (string hostNameOrAddress)
261 if (hostNameOrAddress == null)
262 throw new ArgumentNullException ("hostNameOrAddress");
263 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
264 throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
265 "and ::0 (IPv6) are unspecified addresses. You " +
266 "cannot use them as target address.",
267 "hostNameOrAddress");
270 if (hostNameOrAddress.Length > 0 && IPAddress.TryParse (hostNameOrAddress, out addr))
271 return GetHostEntry (addr);
273 return GetHostByName (hostNameOrAddress);
276 public static IPHostEntry GetHostEntry (IPAddress address)
279 throw new ArgumentNullException ("address");
281 return GetHostByAddressFromString (address.ToString (), false);
284 public static IPAddress [] GetHostAddresses (string hostNameOrAddress)
286 if (hostNameOrAddress == null)
287 throw new ArgumentNullException ("hostNameOrAddress");
289 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
290 throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
291 "and ::0 (IPv6) are unspecified addresses. You " +
292 "cannot use them as target address.",
293 "hostNameOrAddress");
296 if (hostNameOrAddress.Length > 0 && IPAddress.TryParse (hostNameOrAddress, out addr))
297 return new IPAddress[1] { addr };
299 return GetHostEntry (hostNameOrAddress).AddressList;
302 [Obsolete ("Use GetHostEntry instead")]
303 public static IPHostEntry GetHostByName (string hostName)
305 if (hostName == null)
306 throw new ArgumentNullException ("hostName");
308 if (hostName.Length == 0)
309 hostName = "localhost";
311 java.net.InetAddress[] iaArr = java.net.InetAddress.getAllByName(hostName);
312 IPHostEntry host = new IPHostEntry();
313 if (iaArr != null && iaArr.Length > 0)
315 host.HostName = iaArr[0].getHostName();
316 IPAddress[] ipArr = new IPAddress[iaArr.Length];
317 for (int i = 0; i < iaArr.Length; i++)
318 ipArr[i] = IPAddress.Parse(iaArr[i].getHostAddress());
320 host.AddressList = ipArr;
323 } catch (java.net.UnknownHostException jUHE) {
324 throw new SocketException((int)SocketError.HostNotFound, jUHE.Message);
328 string[] h_aliases, h_addrlist;
330 bool ret = GetHostByName_internal(hostName, out h_name, out h_aliases, out h_addrlist);
332 throw new SocketException(11001);
334 return(hostent_to_IPHostEntry(h_name, h_aliases, h_addrlist));
338 public static string GetHostName ()
341 return java.net.InetAddress.getLocalHost ().getHostName ();
345 bool ret = GetHostName_internal(out hostName);
348 throw new SocketException(11001);
354 [Obsolete ("Use GetHostEntry instead")]
355 public static IPHostEntry Resolve(string hostName)
357 if (hostName == null)
358 throw new ArgumentNullException ("hostName");
360 IPHostEntry ret = null;
363 ret = GetHostByAddress(hostName);
368 ret = GetHostByName(hostName);