Add a MonoTODO
[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                 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist) {
67                         IPHostEntry he = new IPHostEntry();
68                         IPAddress[] addrlist = new IPAddress[h_addrlist.Length];
69                         
70                         he.HostName=h_name;
71                         he.Aliases=h_aliases;
72                         for(int i=0; i<h_addrlist.Length; i++) {
73                                 addrlist[i]=IPAddress.Parse(h_addrlist[i]);
74                         }
75                         he.AddressList=addrlist;
76
77                         return(he);
78                 }
79
80                 public static IPHostEntry GetHostByAddress(IPAddress address) {
81                         if (address == null)
82                                 throw new ArgumentNullException();
83                         return GetHostByAddress(address.ToString());
84                 }
85                 
86                 public static IPHostEntry GetHostByAddress(string address) {
87                         if (address == null) {
88                                 throw new ArgumentNullException();
89                         }
90                         
91                         string h_name;
92                         string[] h_aliases, h_addrlist;
93                         
94                         bool ret = GetHostByAddr_internal(address, out h_name,
95                                                           out h_aliases,
96                                                           out h_addrlist);
97                         if (ret == false) {
98                                 throw new SocketException(11001);
99                         }
100                         
101                         return(hostent_to_IPHostEntry(h_name, h_aliases,
102                                                       h_addrlist));
103                 }
104
105                 public static IPHostEntry GetHostByName(string hostName) {
106                         if (hostName == null)
107                                 throw new ArgumentNullException();
108                         
109                         string h_name;
110                         string[] h_aliases, h_addrlist;
111                         
112                         bool ret = GetHostByName_internal(hostName, out h_name,
113                                                           out h_aliases,
114                                                           out h_addrlist);
115                         if (ret == false)
116                                 throw new SocketException(11001);
117
118                         return(hostent_to_IPHostEntry(h_name, h_aliases,
119                                                       h_addrlist));
120                 }
121                 
122                 /// <summary>
123                 /// This method returns the host name associated with the local host.
124                 /// </summary>
125                 [MonoTODO]
126                 public static string GetHostName() {
127
128                         //
129                         // This should really PInvoke into `gethostname', which is not the
130                         // same thing as `127.0.0.1' which will be most likely localhost
131                         //
132                         IPHostEntry h = GetHostByAddress("127.0.0.1");
133                         return h.HostName;
134                 }
135                 
136                 /// <summary>
137                 /// This method resolves a DNS-style host name or IP
138                 /// address.
139                 /// </summary>
140                 /// <param name=hostName>
141                 /// A string containing either a DNS-style host name (e.g.
142                 /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
143                 /// </param>
144                 public static IPHostEntry Resolve(string hostName) {
145                         if (hostName == null)
146                                 throw new ArgumentNullException();
147
148                         return GetHostByName (hostName);
149                 }
150         }
151 }
152