2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Net.Sockets;
31 using System.Text;
32 using System.Collections;
33 using System.Threading;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.Remoting.Messaging;
36
37 namespace System.Net {
38         public sealed class Dns {
39
40                 private Dns () {}
41                 static Dns ()
42                 {
43                         System.Net.Sockets.Socket.CheckProtocolSupport();
44                 }
45
46                 private delegate IPHostEntry GetHostByNameCallback (string hostName);
47                 private delegate IPHostEntry ResolveCallback (string hostName);
48
49                 public static IAsyncResult BeginGetHostByName (string hostName,
50                         AsyncCallback requestCallback, object stateObject)
51                 {
52                         if (hostName == null)
53                                 throw new ArgumentNullException();
54
55                         GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
56                         return c.BeginInvoke (hostName, requestCallback, stateObject);
57                 }
58
59                 public static IAsyncResult BeginResolve (string hostName,
60                         AsyncCallback requestCallback, object stateObject)
61                 {
62                         if (hostName == null)
63                                 throw new ArgumentNullException();
64                         ResolveCallback c = new ResolveCallback (Resolve);
65                         return c.BeginInvoke (hostName, requestCallback, stateObject);
66                 }
67
68                 public static IPHostEntry EndGetHostByName (IAsyncResult asyncResult) \r
69                 {
70                         if (asyncResult == null)
71                                 throw new ArgumentNullException ("asyncResult");
72                         AsyncResult async = (AsyncResult) asyncResult;
73                         GetHostByNameCallback cb = (GetHostByNameCallback) async.AsyncDelegate;
74                         return cb.EndInvoke(asyncResult);
75                 }
76
77                 public static IPHostEntry EndResolve (IAsyncResult asyncResult) \r
78                 {
79                         if (asyncResult == null)
80                                 throw new ArgumentNullException ("asyncResult");
81                         AsyncResult async = (AsyncResult) asyncResult;
82                         ResolveCallback cb = (ResolveCallback) async.AsyncDelegate;
83                         return cb.EndInvoke(asyncResult);
84                 }
85
86                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
87                 private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
88
89                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
90                 private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
91
92                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
93                 private extern static bool GetHostName_internal(out string h_name);
94                 
95                 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist) \r
96                 {
97                         IPHostEntry he = new IPHostEntry();
98                         ArrayList addrlist = new ArrayList();
99
100                         he.HostName = h_name;
101                         he.Aliases = h_aliases;
102                         for(int i=0; i<h_addrlist.Length; i++) {
103                                 IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
104
105                                 if( (Socket.SupportsIPv6 && newAddress.AddressFamily == AddressFamily.InterNetworkV6) ||
106                                         (Socket.SupportsIPv4 && newAddress.AddressFamily == AddressFamily.InterNetwork) )
107                                         addrlist.Add(newAddress);
108                         }
109
110                         if(addrlist.Count == 0)
111                                 throw new SocketException(11001);
112
113                         he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
114                         return he;
115                 }
116
117                 public static IPHostEntry GetHostByAddress(IPAddress address) \r
118                 {
119                         if (address == null)
120                                 throw new ArgumentNullException();
121                         return GetHostByAddress (address.ToString());
122                 }
123 \r
124                 public static IPHostEntry GetHostByAddress(string address) \r
125                 {
126                         if (address == null)
127                                 throw new ArgumentNullException();
128
129                         // Undocumented MS behavior: when called with IF_ANY,
130                         // this should return the local host
131                         if (address.Equals ("0.0.0.0"))
132                                 return GetHostByAddress ("127.0.0.1");
133
134                         /// Must check the IP format, might send an exception if 
135                         /// invalid string.
136                         IPAddress.Parse(address);
137
138                         string h_name;
139                         string[] h_aliases, h_addrlist;
140
141                         bool ret = GetHostByAddr_internal(address, out h_name,
142                                 out h_aliases,
143                                 out h_addrlist);
144                         if (!ret)
145                                 throw new SocketException(11001);
146
147                         return(hostent_to_IPHostEntry(h_name, h_aliases,
148                                 h_addrlist));
149                 }
150
151                 public static IPHostEntry GetHostByName(string hostName) \r
152                 {
153                         if (hostName == null)
154                                 throw new ArgumentNullException();
155
156                         string h_name;
157                         string[] h_aliases, h_addrlist;
158
159                         bool ret = GetHostByName_internal(hostName, out h_name,
160                                 out h_aliases,
161                                 out h_addrlist);
162                         if (ret == false)
163                                 throw new SocketException(11001);
164
165                         return(hostent_to_IPHostEntry(h_name, h_aliases,
166                                 h_addrlist));
167                 }
168
169                 /// <summary>
170                 /// This method returns the host name associated with the local host.
171                 /// </summary>
172                 public static string GetHostName() \r
173                 {
174                         string hostName;
175
176                         bool ret = GetHostName_internal(out hostName);
177
178                         if (ret == false)
179                                 throw new SocketException(11001);
180
181                         return hostName;
182                 }
183
184                 /// <summary>
185                 /// This method resolves a DNS-style host name or IP
186                 /// address.
187                 /// </summary>
188                 /// <param name=hostName>
189                 /// A string containing either a DNS-style host name (e.g.
190                 /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
191                 /// </param>
192                 public static IPHostEntry Resolve(string hostName) \r
193                 {
194                         if (hostName == null)
195                                 throw new ArgumentNullException();
196
197                         IPHostEntry ret = null;
198
199                         try {\r
200                                 ret =  GetHostByAddress(hostName);\r
201                         }
202                         catch{}
203
204                         if(ret == null)\r
205                                 ret =  GetHostByName(hostName);\r
206
207                         return ret;
208                 }
209         }
210 }
211