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