Dns.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // System.Net.Dns.cs
  2. //
  3. // Author: Mads Pultz ([email protected])
  4. // Author: Lawrence Pit ([email protected])
  5. //
  6. // (C) Mads Pultz, 2001
  7. using System;
  8. using System.Net.Sockets;
  9. using System.Text;
  10. using System.Collections;
  11. using System.Threading;
  12. using System.Runtime.CompilerServices;
  13. using System.Runtime.Remoting.Messaging;
  14. namespace System.Net {
  15. public sealed class Dns {
  16. private Dns () {}
  17. static Dns ()
  18. {
  19. System.Net.Sockets.Socket.CheckProtocolSupport();
  20. }
  21. private delegate IPHostEntry GetHostByNameCallback (string hostName);
  22. private delegate IPHostEntry ResolveCallback (string hostName);
  23. public static IAsyncResult BeginGetHostByName (string hostName,
  24. AsyncCallback requestCallback, object stateObject)
  25. {
  26. if (hostName == null)
  27. throw new ArgumentNullException();
  28. GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
  29. return c.BeginInvoke (hostName, requestCallback, stateObject);
  30. }
  31. public static IAsyncResult BeginResolve (string hostName,
  32. AsyncCallback requestCallback, object stateObject)
  33. {
  34. if (hostName == null)
  35. throw new ArgumentNullException();
  36. ResolveCallback c = new ResolveCallback (Resolve);
  37. return c.BeginInvoke (hostName, requestCallback, stateObject);
  38. }
  39. public static IPHostEntry EndGetHostByName (IAsyncResult asyncResult)
  40. {
  41. if (asyncResult == null)
  42. throw new ArgumentNullException ("asyncResult");
  43. AsyncResult async = (AsyncResult) asyncResult;
  44. GetHostByNameCallback cb = (GetHostByNameCallback) async.AsyncDelegate;
  45. asyncResult.AsyncWaitHandle.WaitOne ();
  46. return cb.EndInvoke(asyncResult);
  47. }
  48. public static IPHostEntry EndResolve (IAsyncResult asyncResult)
  49. {
  50. if (asyncResult == null)
  51. throw new ArgumentNullException ("asyncResult");
  52. AsyncResult async = (AsyncResult) asyncResult;
  53. ResolveCallback cb = (ResolveCallback) async.AsyncDelegate;
  54. asyncResult.AsyncWaitHandle.WaitOne ();
  55. return cb.EndInvoke(asyncResult);
  56. }
  57. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  58. private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
  59. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  60. private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
  61. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  62. private extern static bool GetHostName_internal(out string h_name);
  63. private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist)
  64. {
  65. IPHostEntry he = new IPHostEntry();
  66. ArrayList addrlist = new ArrayList();
  67. he.HostName = h_name;
  68. he.Aliases = h_aliases;
  69. for(int i=0; i<h_addrlist.Length; i++) {
  70. IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
  71. if( (Socket.SupportsIPv6 && newAddress.AddressFamily == AddressFamily.InterNetworkV6) ||
  72. (Socket.SupportsIPv4 && newAddress.AddressFamily == AddressFamily.InterNetwork) )
  73. addrlist.Add(newAddress);
  74. }
  75. if(addrlist.Count == 0)
  76. throw new SocketException(11001);
  77. he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
  78. return he;
  79. }
  80. public static IPHostEntry GetHostByAddress(IPAddress address)
  81. {
  82. if (address == null)
  83. throw new ArgumentNullException();
  84. return GetHostByAddress (address.ToString());
  85. }
  86. public static IPHostEntry GetHostByAddress(string address)
  87. {
  88. if (address == null)
  89. throw new ArgumentNullException();
  90. // Undocumented MS behavior: when called with IF_ANY,
  91. // this should return the local host
  92. if (address.Equals ("0.0.0.0"))
  93. return GetHostByAddress ("127.0.0.1");
  94. /// Must check the IP format, might send an exception if
  95. /// invalid string.
  96. IPAddress.Parse(address);
  97. string h_name;
  98. string[] h_aliases, h_addrlist;
  99. bool ret = GetHostByAddr_internal(address, out h_name,
  100. out h_aliases,
  101. out h_addrlist);
  102. if (!ret)
  103. throw new SocketException(11001);
  104. return(hostent_to_IPHostEntry(h_name, h_aliases,
  105. h_addrlist));
  106. }
  107. public static IPHostEntry GetHostByName(string hostName)
  108. {
  109. if (hostName == null)
  110. throw new ArgumentNullException();
  111. string h_name;
  112. string[] h_aliases, h_addrlist;
  113. bool ret = GetHostByName_internal(hostName, out h_name,
  114. out h_aliases,
  115. out h_addrlist);
  116. if (ret == false)
  117. throw new SocketException(11001);
  118. return(hostent_to_IPHostEntry(h_name, h_aliases,
  119. h_addrlist));
  120. }
  121. /// <summary>
  122. /// This method returns the host name associated with the local host.
  123. /// </summary>
  124. public static string GetHostName()
  125. {
  126. string hostName;
  127. bool ret = GetHostName_internal(out hostName);
  128. if (ret == false)
  129. throw new SocketException(11001);
  130. return hostName;
  131. }
  132. /// <summary>
  133. /// This method resolves a DNS-style host name or IP
  134. /// address.
  135. /// </summary>
  136. /// <param name=hostName>
  137. /// A string containing either a DNS-style host name (e.g.
  138. /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
  139. /// </param>
  140. public static IPHostEntry Resolve(string hostName)
  141. {
  142. if (hostName == null)
  143. throw new ArgumentNullException();
  144. IPHostEntry ret = null;
  145. try {
  146. ret = GetHostByAddress(hostName);
  147. }
  148. catch{}
  149. if(ret == null)
  150. ret = GetHostByName(hostName);
  151. return ret;
  152. }
  153. }
  154. }