Dns.cs 5.2 KB

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