2003-07-14 Jerome Laban <jlaban@wanadoo.fr>
[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                 static Dns ()
21                 {
22                         System.Net.Sockets.Socket.CheckProtocolSupport();
23                 }
24
25                 private delegate IPHostEntry GetHostByNameCallback (string hostName);
26                 private delegate IPHostEntry ResolveCallback (string hostName);
27
28                 public static IAsyncResult BeginGetHostByName (string hostName,
29                         AsyncCallback requestCallback, object stateObject)
30                 {
31                         if (hostName == null)
32                                 throw new ArgumentNullException();
33
34                         GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
35                         return c.BeginInvoke (hostName, requestCallback, stateObject);
36                 }
37
38                 public static IAsyncResult BeginResolve (string hostName,
39                         AsyncCallback requestCallback, object stateObject)
40                 {
41                         if (hostName == null)
42                                 throw new ArgumentNullException();
43                         ResolveCallback c = new ResolveCallback (Resolve);
44                         return c.BeginInvoke (hostName, requestCallback, stateObject);
45                 }
46
47                 public static IPHostEntry EndGetHostByName (IAsyncResult asyncResult) \r
48                 {
49                         if (asyncResult == null)
50                                 throw new ArgumentNullException ("asyncResult");
51                         AsyncResult async = (AsyncResult) asyncResult;
52                         GetHostByNameCallback cb = (GetHostByNameCallback) async.AsyncDelegate;
53                         asyncResult.AsyncWaitHandle.WaitOne ();
54                         return cb.EndInvoke(asyncResult);
55                 }
56
57                 public static IPHostEntry EndResolve (IAsyncResult asyncResult) \r
58                 {
59                         if (asyncResult == null)
60                                 throw new ArgumentNullException ("asyncResult");
61                         AsyncResult async = (AsyncResult) asyncResult;
62                         ResolveCallback cb = (ResolveCallback) async.AsyncDelegate;
63                         asyncResult.AsyncWaitHandle.WaitOne ();
64                         return cb.EndInvoke(asyncResult);
65                 }
66
67                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
68                 private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
69
70                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
71                 private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
72
73                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
74                 private extern static bool GetHostName_internal(out string h_name);
75                 
76                 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist) \r
77                 {
78                         IPHostEntry he = new IPHostEntry();
79                         ArrayList addrlist = new ArrayList();
80
81                         he.HostName = h_name;
82                         he.Aliases = h_aliases;
83                         for(int i=0; i<h_addrlist.Length; i++) {
84                                 IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
85
86                                 if( (Socket.SupportsIPv6 && newAddress.AddressFamily == AddressFamily.InterNetworkV6) ||
87                                         (Socket.SupportsIPv4 && newAddress.AddressFamily == AddressFamily.InterNetwork) )
88                                         addrlist.Add(newAddress);
89                         }
90
91                         if(addrlist.Count == 0)
92                                 throw new SocketException(11001);
93
94                         he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
95                         return he;
96                 }
97
98                 public static IPHostEntry GetHostByAddress(IPAddress address) \r
99                 {
100                         if (address == null)
101                                 throw new ArgumentNullException();
102                         return GetHostByAddress (address.ToString());
103                 }
104 \r
105                 public static IPHostEntry GetHostByAddress(string address) \r
106                 {
107                         if (address == null)
108                                 throw new ArgumentNullException();
109
110                         // Undocumented MS behavior: when called with IF_ANY,
111                         // this should return the local host
112                         if (address.Equals ("0.0.0.0"))
113                                 return GetHostByAddress ("127.0.0.1");
114
115                         /// Must check the IP format, might send an exception if 
116                         /// invalid string.
117                         IPAddress.Parse(address);
118
119                         string h_name;
120                         string[] h_aliases, h_addrlist;
121
122                         bool ret = GetHostByAddr_internal(address, out h_name,
123                                 out h_aliases,
124                                 out h_addrlist);
125                         if (!ret)
126                                 throw new SocketException(11001);
127
128                         return(hostent_to_IPHostEntry(h_name, h_aliases,
129                                 h_addrlist));
130                 }
131
132                 public static IPHostEntry GetHostByName(string hostName) \r
133                 {
134                         if (hostName == null)
135                                 throw new ArgumentNullException();
136
137                         string h_name;
138                         string[] h_aliases, h_addrlist;
139
140                         bool ret = GetHostByName_internal(hostName, out h_name,
141                                 out h_aliases,
142                                 out h_addrlist);
143                         if (ret == false)
144                                 throw new SocketException(11001);
145
146                         return(hostent_to_IPHostEntry(h_name, h_aliases,
147                                 h_addrlist));
148                 }
149
150                 /// <summary>
151                 /// This method returns the host name associated with the local host.
152                 /// </summary>
153                 public static string GetHostName() \r
154                 {
155                         string hostName;
156
157                         bool ret = GetHostName_internal(out hostName);
158
159                         if (ret == false)
160                                 throw new SocketException(11001);
161
162                         return hostName;
163                 }
164
165                 /// <summary>
166                 /// This method resolves a DNS-style host name or IP
167                 /// address.
168                 /// </summary>
169                 /// <param name=hostName>
170                 /// A string containing either a DNS-style host name (e.g.
171                 /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
172                 /// </param>
173                 public static IPHostEntry Resolve(string hostName) \r
174                 {
175                         if (hostName == null)
176                                 throw new ArgumentNullException();
177
178                         IPHostEntry ret = null;
179
180                         try {\r
181                                 ret =  GetHostByAddress(hostName);\r
182                         }
183                         catch{}
184
185                         if(ret == null)\r
186                                 ret =  GetHostByName(hostName);\r
187
188                         return ret;
189                 }
190         }
191 }
192