3 // Author: Mads Pultz (mpultz@diku.dk)
5 // (C) Mads Pultz, 2001
8 using System.Net.Sockets;
10 using System.Collections;
11 using System.Threading;
12 using System.Runtime.CompilerServices;
14 namespace System.Net {
15 public sealed class Dns {
22 private sealed class DnsAsyncResult: IAsyncResult {
24 private WaitHandle waitHandle;
25 private bool completedSync, completed;
26 private Worker worker;
28 public DnsAsyncResult(object state) {
30 waitHandle = new ManualResetEvent(false);
31 completedSync = completed = false;
33 public object AsyncState {
36 public WaitHandle AsyncWaitHandle {
37 set { waitHandle = value; }
38 get { return waitHandle; }
40 public bool CompletedSynchronously {
41 get { return completedSync; }
43 public bool IsCompleted {
44 set { completed = value; }
45 get { return completed; }
47 public Worker Worker {
48 set { worker = value; }
49 get { return worker; }
54 /// Helper class for asynchronous calls to DNS server
56 private sealed class Worker {
57 private AsyncCallback reqCallback;
58 private DnsAsyncResult reqRes;
60 private IPHostEntry result;
62 public Worker(string req, AsyncCallback reqCallback, DnsAsyncResult reqRes) {
64 this.reqCallback = reqCallback;
69 ((ManualResetEvent)reqRes.AsyncWaitHandle).Set();
70 reqRes.IsCompleted = true;
72 public void GetHostByName() {
74 result = Dns.GetHostByName(req);
78 public void Resolve() {
80 result = Dns.Resolve(req);
84 public IPHostEntry Result {
85 get { return result; }
89 public static IAsyncResult BeginGetHostByName(string hostName,
90 AsyncCallback requestCallback, object stateObject)
92 DnsAsyncResult requestResult = new DnsAsyncResult(stateObject);
93 Worker worker = new Worker(hostName, requestCallback, requestResult);
94 Thread child = new Thread(new ThreadStart(worker.GetHostByName));
99 public static IAsyncResult BeginResolve(string hostName,
100 AsyncCallback requestCallback, object stateObject)
102 DnsAsyncResult requestResult = new DnsAsyncResult(stateObject);
103 Worker worker = new Worker(hostName, requestCallback, requestResult);
104 Thread child = new Thread(new ThreadStart(worker.Resolve));
106 return requestResult;
109 public static IPHostEntry EndGetHostByName(IAsyncResult asyncResult) {
110 return ((DnsAsyncResult)asyncResult).Worker.Result;
113 public static IPHostEntry EndResolve(IAsyncResult asyncResult) {
114 return ((DnsAsyncResult)asyncResult).Worker.Result;
118 [MethodImplAttribute(MethodImplOptions.InternalCall)]
119 private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
120 [MethodImplAttribute(MethodImplOptions.InternalCall)]
121 private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
123 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist) {
124 IPHostEntry he = new IPHostEntry();
125 IPAddress[] addrlist = new IPAddress[h_addrlist.Length];
128 he.Aliases=h_aliases;
129 for(int i=0; i<h_addrlist.Length; i++) {
130 addrlist[i]=IPAddress.Parse(h_addrlist[i]);
132 he.AddressList=addrlist;
137 public static IPHostEntry GetHostByAddress(IPAddress address) {
139 throw new ArgumentNullException();
140 return GetHostByAddress(address.ToString());
143 public static IPHostEntry GetHostByAddress(string address) {
144 if (address == null) {
145 throw new ArgumentNullException();
149 string[] h_aliases, h_addrlist;
151 bool ret = GetHostByAddr_internal(address, out h_name,
155 throw new SocketException(11001);
158 return(hostent_to_IPHostEntry(h_name, h_aliases,
162 public static IPHostEntry GetHostByName(string hostName) {
163 if (hostName == null) {
164 throw new ArgumentNullException();
168 string[] h_aliases, h_addrlist;
170 bool ret = GetHostByName_internal(hostName, out h_name,
174 throw new SocketException(11001);
177 return(hostent_to_IPHostEntry(h_name, h_aliases,
182 /// This method returns the host name associated with the local host.
184 public static string GetHostName() {
185 IPHostEntry h = GetHostByAddress("127.0.0.1");
190 /// This method resolves a DNS-style host name or IP
193 /// <param name=hostName>
194 /// A string containing either a DNS-style host name (e.g.
195 /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
197 public static IPHostEntry Resolve(string hostName) {
198 if (hostName == null)
199 throw new ArgumentNullException();
201 return GetHostByName (hostName);