Dns.cs 6.1 KB

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