Dns.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // System.Net.Dns.cs
  2. //
  3. // Author: Mads Pultz ([email protected])
  4. //
  5. // (C) Mads Pultz, 2001
  6. using System;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Collections;
  10. using System.Threading;
  11. using System.Runtime.CompilerServices;
  12. namespace System.Net {
  13. public sealed class Dns {
  14. private Dns () {}
  15. /// <summary>
  16. /// Helper class
  17. /// </summary>
  18. private sealed class DnsAsyncResult: IAsyncResult {
  19. private object state;
  20. private WaitHandle waitHandle;
  21. private bool completedSync, completed;
  22. private Worker worker;
  23. public DnsAsyncResult(object state) {
  24. this.state = state;
  25. waitHandle = new ManualResetEvent(false);
  26. completedSync = completed = false;
  27. }
  28. public object AsyncState {
  29. get { return state; }
  30. }
  31. public WaitHandle AsyncWaitHandle {
  32. set { waitHandle = value; }
  33. get { return waitHandle; }
  34. }
  35. public bool CompletedSynchronously {
  36. get { return completedSync; }
  37. }
  38. public bool IsCompleted {
  39. set { completed = value; }
  40. get { return completed; }
  41. }
  42. public Worker Worker {
  43. set { worker = value; }
  44. get { return worker; }
  45. }
  46. }
  47. /// <summary>
  48. /// Helper class for asynchronous calls to DNS server
  49. /// </summary>
  50. private sealed class Worker {
  51. private AsyncCallback reqCallback;
  52. private DnsAsyncResult reqRes;
  53. private string req;
  54. private IPHostEntry result;
  55. public Worker(string req, AsyncCallback reqCallback, DnsAsyncResult reqRes) {
  56. this.req = req;
  57. this.reqCallback = reqCallback;
  58. this.reqRes = reqRes;
  59. }
  60. private void End() {
  61. reqCallback(reqRes);
  62. ((ManualResetEvent)reqRes.AsyncWaitHandle).Set();
  63. reqRes.IsCompleted = true;
  64. }
  65. public void GetHostByName() {
  66. lock(reqRes) {
  67. result = Dns.GetHostByName(req);
  68. End();
  69. }
  70. }
  71. public void Resolve() {
  72. lock(reqRes) {
  73. result = Dns.Resolve(req);
  74. End();
  75. }
  76. }
  77. public IPHostEntry Result {
  78. get { return result; }
  79. }
  80. }
  81. public static IAsyncResult BeginGetHostByName(string hostName,
  82. AsyncCallback requestCallback, object stateObject)
  83. {
  84. DnsAsyncResult requestResult = new DnsAsyncResult(stateObject);
  85. Worker worker = new Worker(hostName, requestCallback, requestResult);
  86. Thread child = new Thread(new ThreadStart(worker.GetHostByName));
  87. child.Start();
  88. return requestResult;
  89. }
  90. public static IAsyncResult BeginResolve(string hostName,
  91. AsyncCallback requestCallback, object stateObject)
  92. {
  93. DnsAsyncResult requestResult = new DnsAsyncResult(stateObject);
  94. Worker worker = new Worker(hostName, requestCallback, requestResult);
  95. Thread child = new Thread(new ThreadStart(worker.Resolve));
  96. child.Start();
  97. return requestResult;
  98. }
  99. public static IPHostEntry EndGetHostByName(IAsyncResult asyncResult) {
  100. return ((DnsAsyncResult)asyncResult).Worker.Result;
  101. }
  102. public static IPHostEntry EndResolve(IAsyncResult asyncResult) {
  103. return ((DnsAsyncResult)asyncResult).Worker.Result;
  104. }
  105. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  106. private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
  107. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  108. private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
  109. private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist) {
  110. IPHostEntry he = new IPHostEntry();
  111. IPAddress[] addrlist = new IPAddress[h_addrlist.Length];
  112. he.HostName=h_name;
  113. he.Aliases=h_aliases;
  114. for(int i=0; i<h_addrlist.Length; i++) {
  115. addrlist[i]=IPAddress.Parse(h_addrlist[i]);
  116. }
  117. he.AddressList=addrlist;
  118. return(he);
  119. }
  120. public static IPHostEntry GetHostByAddress(IPAddress address) {
  121. if (address == null)
  122. throw new ArgumentNullException();
  123. return GetHostByAddress(address.ToString());
  124. }
  125. public static IPHostEntry GetHostByAddress(string address) {
  126. if (address == null) {
  127. throw new ArgumentNullException();
  128. }
  129. string h_name;
  130. string[] h_aliases, h_addrlist;
  131. bool ret = GetHostByAddr_internal(address, out h_name,
  132. out h_aliases,
  133. out h_addrlist);
  134. if (ret == false) {
  135. throw new SocketException();
  136. }
  137. return(hostent_to_IPHostEntry(h_name, h_aliases,
  138. h_addrlist));
  139. }
  140. public static IPHostEntry GetHostByName(string hostName) {
  141. if (hostName == null) {
  142. throw new ArgumentNullException();
  143. }
  144. string h_name;
  145. string[] h_aliases, h_addrlist;
  146. bool ret = GetHostByName_internal(hostName, out h_name,
  147. out h_aliases,
  148. out h_addrlist);
  149. if (ret == false) {
  150. throw new SocketException();
  151. }
  152. return(hostent_to_IPHostEntry(h_name, h_aliases,
  153. h_addrlist));
  154. }
  155. /// <summary>
  156. /// This method returns the host name associated with the local host.
  157. /// </summary>
  158. public static string GetHostName() {
  159. IPHostEntry h = GetHostByAddress("127.0.0.1");
  160. return h.HostName;
  161. }
  162. /// <summary>
  163. /// This method resolves a DNS-style host name or IP
  164. /// address.
  165. /// </summary>
  166. /// <param name=hostName>
  167. /// A string containing either a DNS-style host name (e.g.
  168. /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
  169. /// </param>
  170. public static IPHostEntry Resolve(string hostName) {
  171. if (hostName == null)
  172. throw new ArgumentNullException();
  173. return GetHostByName (hostName);
  174. }
  175. }
  176. }