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 {
21 private delegate IPHostEntry GetHostByNameCallback (string hostName);
22 private delegate IPHostEntry ResolveCallback (string hostName);
24 public static IAsyncResult BeginGetHostByName (string hostName,
25 AsyncCallback requestCallback, object stateObject)
28 throw new ArgumentNullException();
29 GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
30 return c.BeginInvoke (hostName, requestCallback, stateObject);
33 public static IAsyncResult BeginResolve (string hostName,
34 AsyncCallback requestCallback, object stateObject)
37 throw new ArgumentNullException();
38 ResolveCallback c = new ResolveCallback (Resolve);
39 return c.BeginInvoke (hostName, requestCallback, stateObject);
42 public static IPHostEntry EndGetHostByName (IAsyncResult asyncResult) {
43 if (asyncResult == null)
44 throw new ArgumentNullException ("asyncResult");
45 AsyncResult async = (AsyncResult) asyncResult;
46 GetHostByNameCallback cb = (GetHostByNameCallback) async.AsyncDelegate;
47 asyncResult.AsyncWaitHandle.WaitOne ();
48 return cb.EndInvoke(asyncResult);
51 public static IPHostEntry EndResolve (IAsyncResult asyncResult) {
52 if (asyncResult == null)
53 throw new ArgumentNullException ("asyncResult");
54 AsyncResult async = (AsyncResult) asyncResult;
55 ResolveCallback cb = (ResolveCallback) async.AsyncDelegate;
56 asyncResult.AsyncWaitHandle.WaitOne ();
57 return cb.EndInvoke(asyncResult);
60 [MethodImplAttribute(MethodImplOptions.InternalCall)]
61 private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
63 [MethodImplAttribute(MethodImplOptions.InternalCall)]
64 private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
66 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist) {
67 IPHostEntry he = new IPHostEntry();
68 IPAddress[] addrlist = new IPAddress[h_addrlist.Length];
72 for(int i=0; i<h_addrlist.Length; i++) {
73 addrlist[i]=IPAddress.Parse(h_addrlist[i]);
75 he.AddressList=addrlist;
80 public static IPHostEntry GetHostByAddress(IPAddress address) {
82 throw new ArgumentNullException();
83 return GetHostByAddress(address.ToString());
86 public static IPHostEntry GetHostByAddress(string address) {
87 if (address == null) {
88 throw new ArgumentNullException();
92 string[] h_aliases, h_addrlist;
94 bool ret = GetHostByAddr_internal(address, out h_name,
98 throw new SocketException(11001);
101 return(hostent_to_IPHostEntry(h_name, h_aliases,
105 public static IPHostEntry GetHostByName(string hostName) {
106 if (hostName == null)
107 throw new ArgumentNullException();
110 string[] h_aliases, h_addrlist;
112 bool ret = GetHostByName_internal(hostName, out h_name,
116 throw new SocketException(11001);
118 return(hostent_to_IPHostEntry(h_name, h_aliases,
123 /// This method returns the host name associated with the local host.
126 public static string GetHostName() {
129 // This should really PInvoke into `gethostname', which is not the
130 // same thing as `127.0.0.1' which will be most likely localhost
132 IPHostEntry h = GetHostByAddress("127.0.0.1");
137 /// This method resolves a DNS-style host name or IP
140 /// <param name=hostName>
141 /// A string containing either a DNS-style host name (e.g.
142 /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
144 public static IPHostEntry Resolve(string hostName) {
145 if (hostName == null)
146 throw new ArgumentNullException();
148 return GetHostByName (hostName);