2003-07-13 Andreas Nahr <ClassDevelopment@A-SoftTech.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                                                 // Undocumented MS behavior: when called with IF_ANY,
95                                                 // this should return the local host
96                                                 if (address.Equals ("0.0.0.0"))
97                                                         return GetHostByAddress ("127.0.0.1");
98
99                         string h_name;
100                         string[] h_aliases, h_addrlist;
101                         
102                         bool ret = GetHostByAddr_internal(address, out h_name,
103                                                           out h_aliases,
104                                                           out h_addrlist);
105                         if (ret == false) {
106                                 throw new SocketException(11001);
107                         }
108                         
109                         return(hostent_to_IPHostEntry(h_name, h_aliases,
110                                                       h_addrlist));
111                 }
112
113                 public static IPHostEntry GetHostByName(string hostName) {
114                         if (hostName == null)
115                                 throw new ArgumentNullException();
116
117                         string h_name;
118                         string[] h_aliases, h_addrlist;
119                         
120                         bool ret = GetHostByName_internal(hostName, out h_name,
121                                                           out h_aliases,
122                                                           out h_addrlist);
123                         if (ret == false)
124                                 throw new SocketException(11001);
125
126                         return(hostent_to_IPHostEntry(h_name, h_aliases,
127                                                       h_addrlist));
128                 }
129                 
130                 /// <summary>
131                 /// This method returns the host name associated with the local host.
132                 /// </summary>
133                 public static string GetHostName() {
134                         string hostName;
135                         
136                         bool ret = GetHostName_internal(out hostName);
137                         
138                         if (ret == false)
139                                 throw new SocketException(11001);
140
141                         return hostName;
142                 }
143                 
144                 /// <summary>
145                 /// This method resolves a DNS-style host name or IP
146                 /// address.
147                 /// </summary>
148                 /// <param name=hostName>
149                 /// A string containing either a DNS-style host name (e.g.
150                 /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
151                 /// </param>
152                 public static IPHostEntry Resolve(string hostName) {
153                         if (hostName == null)
154                                 throw new ArgumentNullException();
155
156                         return GetHostByName (hostName);
157                 }
158         }
159 }
160