3 // Author: Mads Pultz (mpultz@diku.dk)
4 // Author: Lawrence Pit (loz@cable.a2000.nl)
6 // (C) Mads Pultz, 2001
9 using System.Net.Sockets;
11 using System.Collections;
12 using System.Threading;
13 using System.Runtime.CompilerServices;
14 using System.Runtime.Remoting.Messaging;
16 namespace System.Net {
17 public sealed class Dns {
22 System.Net.Sockets.Socket.CheckProtocolSupport();
25 private delegate IPHostEntry GetHostByNameCallback (string hostName);
26 private delegate IPHostEntry ResolveCallback (string hostName);
28 public static IAsyncResult BeginGetHostByName (string hostName,
29 AsyncCallback requestCallback, object stateObject)
32 throw new ArgumentNullException();
34 GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
35 return c.BeginInvoke (hostName, requestCallback, stateObject);
38 public static IAsyncResult BeginResolve (string hostName,
39 AsyncCallback requestCallback, object stateObject)
42 throw new ArgumentNullException();
43 ResolveCallback c = new ResolveCallback (Resolve);
44 return c.BeginInvoke (hostName, requestCallback, stateObject);
47 public static IPHostEntry EndGetHostByName (IAsyncResult asyncResult)
\r
49 if (asyncResult == null)
50 throw new ArgumentNullException ("asyncResult");
51 AsyncResult async = (AsyncResult) asyncResult;
52 GetHostByNameCallback cb = (GetHostByNameCallback) async.AsyncDelegate;
53 return cb.EndInvoke(asyncResult);
56 public static IPHostEntry EndResolve (IAsyncResult asyncResult)
\r
58 if (asyncResult == null)
59 throw new ArgumentNullException ("asyncResult");
60 AsyncResult async = (AsyncResult) asyncResult;
61 ResolveCallback cb = (ResolveCallback) async.AsyncDelegate;
62 return cb.EndInvoke(asyncResult);
65 [MethodImplAttribute(MethodImplOptions.InternalCall)]
66 private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
68 [MethodImplAttribute(MethodImplOptions.InternalCall)]
69 private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
71 [MethodImplAttribute(MethodImplOptions.InternalCall)]
72 private extern static bool GetHostName_internal(out string h_name);
74 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist)
\r
76 IPHostEntry he = new IPHostEntry();
77 ArrayList addrlist = new ArrayList();
80 he.Aliases = h_aliases;
81 for(int i=0; i<h_addrlist.Length; i++) {
82 IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
84 if( (Socket.SupportsIPv6 && newAddress.AddressFamily == AddressFamily.InterNetworkV6) ||
85 (Socket.SupportsIPv4 && newAddress.AddressFamily == AddressFamily.InterNetwork) )
86 addrlist.Add(newAddress);
89 if(addrlist.Count == 0)
90 throw new SocketException(11001);
92 he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
96 public static IPHostEntry GetHostByAddress(IPAddress address)
\r
99 throw new ArgumentNullException();
100 return GetHostByAddress (address.ToString());
103 public static IPHostEntry GetHostByAddress(string address)
\r
106 throw new ArgumentNullException();
108 // Undocumented MS behavior: when called with IF_ANY,
109 // this should return the local host
110 if (address.Equals ("0.0.0.0"))
111 return GetHostByAddress ("127.0.0.1");
113 /// Must check the IP format, might send an exception if
115 IPAddress.Parse(address);
118 string[] h_aliases, h_addrlist;
120 bool ret = GetHostByAddr_internal(address, out h_name,
124 throw new SocketException(11001);
126 return(hostent_to_IPHostEntry(h_name, h_aliases,
130 public static IPHostEntry GetHostByName(string hostName)
\r
132 if (hostName == null)
133 throw new ArgumentNullException();
136 string[] h_aliases, h_addrlist;
138 bool ret = GetHostByName_internal(hostName, out h_name,
142 throw new SocketException(11001);
144 return(hostent_to_IPHostEntry(h_name, h_aliases,
149 /// This method returns the host name associated with the local host.
151 public static string GetHostName()
\r
155 bool ret = GetHostName_internal(out hostName);
158 throw new SocketException(11001);
164 /// This method resolves a DNS-style host name or IP
167 /// <param name=hostName>
168 /// A string containing either a DNS-style host name (e.g.
169 /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
171 public static IPHostEntry Resolve(string hostName)
\r
173 if (hostName == null)
174 throw new ArgumentNullException();
176 IPHostEntry ret = null;
179 ret = GetHostByAddress(hostName);
\r
184 ret = GetHostByName(hostName);
\r