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 sealed class Dns {
43 System.Net.Sockets.Socket.CheckProtocolSupport();
46 private delegate IPHostEntry GetHostByNameCallback (string hostName);
47 private delegate IPHostEntry ResolveCallback (string hostName);
49 public static IAsyncResult BeginGetHostByName (string hostName,
50 AsyncCallback requestCallback, object stateObject)
53 throw new ArgumentNullException();
55 GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
56 return c.BeginInvoke (hostName, requestCallback, stateObject);
59 public static IAsyncResult BeginResolve (string hostName,
60 AsyncCallback requestCallback, object stateObject)
63 throw new ArgumentNullException();
64 ResolveCallback c = new ResolveCallback (Resolve);
65 return c.BeginInvoke (hostName, requestCallback, stateObject);
68 public static IPHostEntry EndGetHostByName (IAsyncResult asyncResult)
\r
70 if (asyncResult == null)
71 throw new ArgumentNullException ("asyncResult");
72 AsyncResult async = (AsyncResult) asyncResult;
73 GetHostByNameCallback cb = (GetHostByNameCallback) async.AsyncDelegate;
74 return cb.EndInvoke(asyncResult);
77 public static IPHostEntry EndResolve (IAsyncResult asyncResult)
\r
79 if (asyncResult == null)
80 throw new ArgumentNullException ("asyncResult");
81 AsyncResult async = (AsyncResult) asyncResult;
82 ResolveCallback cb = (ResolveCallback) async.AsyncDelegate;
83 return cb.EndInvoke(asyncResult);
86 [MethodImplAttribute(MethodImplOptions.InternalCall)]
87 private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
89 [MethodImplAttribute(MethodImplOptions.InternalCall)]
90 private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
92 [MethodImplAttribute(MethodImplOptions.InternalCall)]
93 private extern static bool GetHostName_internal(out string h_name);
95 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist)
\r
97 IPHostEntry he = new IPHostEntry();
98 ArrayList addrlist = new ArrayList();
100 he.HostName = h_name;
101 he.Aliases = h_aliases;
102 for(int i=0; i<h_addrlist.Length; i++) {
103 IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
105 if( (Socket.SupportsIPv6 && newAddress.AddressFamily == AddressFamily.InterNetworkV6) ||
106 (Socket.SupportsIPv4 && newAddress.AddressFamily == AddressFamily.InterNetwork) )
107 addrlist.Add(newAddress);
110 if(addrlist.Count == 0)
111 throw new SocketException(11001);
113 he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
117 public static IPHostEntry GetHostByAddress(IPAddress address)
\r
120 throw new ArgumentNullException();
121 return GetHostByAddress (address.ToString());
124 public static IPHostEntry GetHostByAddress(string address)
\r
127 throw new ArgumentNullException();
129 // Undocumented MS behavior: when called with IF_ANY,
130 // this should return the local host
131 if (address.Equals ("0.0.0.0"))
132 return GetHostByAddress ("127.0.0.1");
134 /// Must check the IP format, might send an exception if
136 IPAddress.Parse(address);
139 string[] h_aliases, h_addrlist;
141 bool ret = GetHostByAddr_internal(address, out h_name,
145 throw new SocketException(11001);
147 return(hostent_to_IPHostEntry(h_name, h_aliases,
151 public static IPHostEntry GetHostByName(string hostName)
\r
153 if (hostName == null)
154 throw new ArgumentNullException();
157 string[] h_aliases, h_addrlist;
159 bool ret = GetHostByName_internal(hostName, out h_name,
163 throw new SocketException(11001);
165 return(hostent_to_IPHostEntry(h_name, h_aliases,
170 /// This method returns the host name associated with the local host.
172 public static string GetHostName()
\r
176 bool ret = GetHostName_internal(out hostName);
179 throw new SocketException(11001);
185 /// This method resolves a DNS-style host name or IP
188 /// <param name=hostName>
189 /// A string containing either a DNS-style host name (e.g.
190 /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
192 public static IPHostEntry Resolve(string hostName)
\r
194 if (hostName == null)
195 throw new ArgumentNullException();
197 IPHostEntry ret = null;
200 ret = GetHostByAddress(hostName);
\r
205 ret = GetHostByName(hostName);
\r