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);
168 [MethodImplAttribute(MethodImplOptions.InternalCall)]
169 private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
171 [MethodImplAttribute(MethodImplOptions.InternalCall)]
172 private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
174 [MethodImplAttribute(MethodImplOptions.InternalCall)]
175 private extern static bool GetHostName_internal(out string h_name);
177 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist)
179 IPHostEntry he = new IPHostEntry();
180 ArrayList addrlist = new ArrayList();
182 he.HostName = h_name;
183 he.Aliases = h_aliases;
184 for(int i=0; i<h_addrlist.Length; i++) {
186 IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
188 if( (Socket.SupportsIPv6 && newAddress.AddressFamily == AddressFamily.InterNetworkV6) ||
189 (Socket.SupportsIPv4 && newAddress.AddressFamily == AddressFamily.InterNetwork) )
190 addrlist.Add(newAddress);
191 } catch (ArgumentNullException) {
192 /* Ignore this, as the
193 * internal call might have
194 * left some blank entries at
195 * the end of the array
200 if(addrlist.Count == 0)
201 throw new SocketException(11001);
203 he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
208 [Obsolete ("Use GetHostEntry instead")]
210 public static IPHostEntry GetHostByAddress(IPAddress address)
213 throw new ArgumentNullException ("address");
215 return GetHostByAddressFromString (address.ToString (), false);
219 [Obsolete ("Use GetHostEntry instead")]
221 public static IPHostEntry GetHostByAddress(string address)
224 throw new ArgumentNullException ("address");
226 return GetHostByAddressFromString (address, true);
229 static IPHostEntry GetHostByAddressFromString (string address, bool parse)
231 // Undocumented MS behavior: when called with IF_ANY,
232 // this should return the local host
233 if (address.Equals ("0.0.0.0")) {
234 address = "127.0.0.1";
238 // Must check the IP format, might send an exception if invalid string.
240 IPAddress.Parse(address);
243 string[] h_aliases, h_addrlist;
245 bool ret = GetHostByAddr_internal(address, out h_name, out h_aliases, out h_addrlist);
247 throw new SocketException(11001);
249 return (hostent_to_IPHostEntry (h_name, h_aliases, h_addrlist));
253 public static IPHostEntry GetHostEntry (string hostNameOrAddress)
255 if (hostNameOrAddress == null)
256 throw new ArgumentNullException ("hostNameOrAddress");
258 if (hostNameOrAddress == "0.0.0.0")
259 hostNameOrAddress = "127.0.0.1";
261 if (IPAddress.TryParse (hostNameOrAddress, out addr))
262 return GetHostEntry (addr);
264 return GetHostByName (hostNameOrAddress);
267 public static IPHostEntry GetHostEntry (IPAddress address)
270 throw new ArgumentNullException ("address");
272 return GetHostByAddressFromString (address.ToString (), false);
275 public static IPAddress [] GetHostAddresses (string hostNameOrAddress)
277 if (hostNameOrAddress == null)
278 throw new ArgumentNullException ("hostNameOrAddress");
280 return GetHostEntry (hostNameOrAddress).AddressList;
285 [Obsolete ("Use GetHostEntry instead")]
287 public static IPHostEntry GetHostByName(string hostName)
289 if (hostName == null)
290 throw new ArgumentNullException();
293 string[] h_aliases, h_addrlist;
295 bool ret = GetHostByName_internal(hostName, out h_name,
299 throw new SocketException(11001);
301 return(hostent_to_IPHostEntry(h_name, h_aliases,
305 public static string GetHostName()
309 bool ret = GetHostName_internal(out hostName);
312 throw new SocketException(11001);
318 [Obsolete ("Use GetHostEntry instead")]
320 public static IPHostEntry Resolve(string hostName)
322 if (hostName == null)
323 throw new ArgumentNullException ("hostName");
325 IPHostEntry ret = null;
328 ret = GetHostByAddress(hostName);
333 ret = GetHostByName(hostName);