2002-10-03 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / System / System.Net / Dns.cs
1 // System.Net.Dns.cs
2 //
3 // Author: Mads Pultz (mpultz@diku.dk)
4 // Author: Lawrence Pit (loz@cable.a2000.nl)
5 //
6 // (C) Mads Pultz, 2001
7
8 using System;
9 using System.Net.Sockets;
10 using System.Text;
11 using System.Collections;
12 using System.Threading;
13 using System.Runtime.CompilerServices;
14 using System.Runtime.Remoting.Messaging;
15
16 namespace System.Net {
17         public sealed class Dns {
18
19                 private Dns () {}
20
21                 private delegate IPHostEntry GetHostByNameCallback (string hostName);
22                 private delegate IPHostEntry ResolveCallback (string hostName);
23                 
24                 public static IAsyncResult BeginGetHostByName (string hostName,
25                         AsyncCallback requestCallback, object stateObject)
26                 {
27                         if (hostName == null)
28                                 throw new ArgumentNullException();
29                         GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
30                         return c.BeginInvoke (hostName, requestCallback, stateObject);
31                 }
32
33                 public static IAsyncResult BeginResolve (string hostName,
34                         AsyncCallback requestCallback, object stateObject)
35                 {
36                         if (hostName == null)
37                                 throw new ArgumentNullException();
38                         ResolveCallback c = new ResolveCallback (Resolve);
39                         return c.BeginInvoke (hostName, requestCallback, stateObject);
40                 }
41                 
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);
49                 }
50
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);
58                 }
59                                 
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);
62
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);
65                 
66                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
67                 private extern static bool GetHostName_internal(out string h_name);
68                 
69                 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist) {
70                         IPHostEntry he = new IPHostEntry();
71                         IPAddress[] addrlist = new IPAddress[h_addrlist.Length];
72                         
73                         he.HostName=h_name;
74                         he.Aliases=h_aliases;
75                         for(int i=0; i<h_addrlist.Length; i++) {
76                                 addrlist[i]=IPAddress.Parse(h_addrlist[i]);
77                         }
78                         he.AddressList=addrlist;
79
80                         return(he);
81                 }
82
83                 public static IPHostEntry GetHostByAddress(IPAddress address) {
84                         if (address == null)
85                                 throw new ArgumentNullException();
86                         return GetHostByAddress(address.ToString());
87                 }
88                 
89                 public static IPHostEntry GetHostByAddress(string address) {
90                         if (address == null) {
91                                 throw new ArgumentNullException();
92                         }
93                         
94                         string h_name;
95                         string[] h_aliases, h_addrlist;
96                         
97                         bool ret = GetHostByAddr_internal(address, out h_name,
98                                                           out h_aliases,
99                                                           out h_addrlist);
100                         if (ret == false) {
101                                 throw new SocketException(11001);
102                         }
103                         
104                         return(hostent_to_IPHostEntry(h_name, h_aliases,
105                                                       h_addrlist));
106                 }
107
108                 public static IPHostEntry GetHostByName(string hostName) {
109                         if (hostName == null)
110                                 throw new ArgumentNullException();
111                         
112                         string h_name;
113                         string[] h_aliases, h_addrlist;
114                         
115                         bool ret = GetHostByName_internal(hostName, out h_name,
116                                                           out h_aliases,
117                                                           out h_addrlist);
118                         if (ret == false)
119                                 throw new SocketException(11001);
120
121                         return(hostent_to_IPHostEntry(h_name, h_aliases,
122                                                       h_addrlist));
123                 }
124                 
125                 /// <summary>
126                 /// This method returns the host name associated with the local host.
127                 /// </summary>
128                 public static string GetHostName() {
129                         string hostName;
130                         
131                         bool ret = GetHostName_internal(out hostName);
132                         
133                         if (ret == false)
134                                 throw new SocketException(11001);
135
136                         return hostName;
137                 }
138                 
139                 /// <summary>
140                 /// This method resolves a DNS-style host name or IP
141                 /// address.
142                 /// </summary>
143                 /// <param name=hostName>
144                 /// A string containing either a DNS-style host name (e.g.
145                 /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
146                 /// </param>
147                 public static IPHostEntry Resolve(string hostName) {
148                         if (hostName == null)
149                                 throw new ArgumentNullException();
150
151                         return GetHostByName (hostName);
152                 }
153         }
154 }
155