Dns.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. private delegate IPHostEntry GetHostByNameCallback (string hostName);
  18. private delegate IPHostEntry ResolveCallback (string hostName);
  19. public static IAsyncResult BeginGetHostByName (string hostName,
  20. AsyncCallback requestCallback, object stateObject)
  21. {
  22. if (hostName == null)
  23. throw new ArgumentNullException();
  24. GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
  25. return c.BeginInvoke (hostName, requestCallback, stateObject);
  26. }
  27. public static IAsyncResult BeginResolve (string hostName,
  28. AsyncCallback requestCallback, object stateObject)
  29. {
  30. if (hostName == null)
  31. throw new ArgumentNullException();
  32. ResolveCallback c = new ResolveCallback (Resolve);
  33. return c.BeginInvoke (hostName, requestCallback, stateObject);
  34. }
  35. public static IPHostEntry EndGetHostByName (IAsyncResult asyncResult) {
  36. if (asyncResult == null)
  37. throw new ArgumentNullException ("asyncResult");
  38. AsyncResult async = (AsyncResult) asyncResult;
  39. GetHostByNameCallback cb = (GetHostByNameCallback) async.AsyncDelegate;
  40. asyncResult.AsyncWaitHandle.WaitOne ();
  41. return cb.EndInvoke(asyncResult);
  42. }
  43. public static IPHostEntry EndResolve (IAsyncResult asyncResult) {
  44. if (asyncResult == null)
  45. throw new ArgumentNullException ("asyncResult");
  46. AsyncResult async = (AsyncResult) asyncResult;
  47. ResolveCallback cb = (ResolveCallback) async.AsyncDelegate;
  48. asyncResult.AsyncWaitHandle.WaitOne ();
  49. return cb.EndInvoke(asyncResult);
  50. }
  51. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  52. private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
  53. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  54. private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
  55. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  56. private extern static bool GetHostName_internal(out string h_name);
  57. private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist) {
  58. IPHostEntry he = new IPHostEntry();
  59. IPAddress[] addrlist = new IPAddress[h_addrlist.Length];
  60. he.HostName=h_name;
  61. he.Aliases=h_aliases;
  62. for(int i=0; i<h_addrlist.Length; i++) {
  63. addrlist[i]=IPAddress.Parse(h_addrlist[i]);
  64. }
  65. he.AddressList=addrlist;
  66. return(he);
  67. }
  68. public static IPHostEntry GetHostByAddress(IPAddress address) {
  69. if (address == null)
  70. throw new ArgumentNullException();
  71. return GetHostByAddress(address.ToString());
  72. }
  73. public static IPHostEntry GetHostByAddress(string address) {
  74. if (address == null) {
  75. throw new ArgumentNullException();
  76. }
  77. // Undocumented MS behavior: when called with IF_ANY,
  78. // this should return the local host
  79. if (address.Equals ("0.0.0.0"))
  80. return GetHostByAddress ("127.0.0.1");
  81. string h_name;
  82. string[] h_aliases, h_addrlist;
  83. bool ret = GetHostByAddr_internal(address, out h_name,
  84. out h_aliases,
  85. out h_addrlist);
  86. if (ret == false) {
  87. throw new SocketException(11001);
  88. }
  89. return(hostent_to_IPHostEntry(h_name, h_aliases,
  90. h_addrlist));
  91. }
  92. public static IPHostEntry GetHostByName(string hostName) {
  93. if (hostName == null)
  94. throw new ArgumentNullException();
  95. string h_name;
  96. string[] h_aliases, h_addrlist;
  97. bool ret = GetHostByName_internal(hostName, out h_name,
  98. out h_aliases,
  99. out h_addrlist);
  100. if (ret == false)
  101. throw new SocketException(11001);
  102. return(hostent_to_IPHostEntry(h_name, h_aliases,
  103. h_addrlist));
  104. }
  105. /// <summary>
  106. /// This method returns the host name associated with the local host.
  107. /// </summary>
  108. public static string GetHostName() {
  109. string hostName;
  110. bool ret = GetHostName_internal(out hostName);
  111. if (ret == false)
  112. throw new SocketException(11001);
  113. return hostName;
  114. }
  115. /// <summary>
  116. /// This method resolves a DNS-style host name or IP
  117. /// address.
  118. /// </summary>
  119. /// <param name=hostName>
  120. /// A string containing either a DNS-style host name (e.g.
  121. /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
  122. /// </param>
  123. public static IPHostEntry Resolve(string hostName) {
  124. if (hostName == null)
  125. throw new ArgumentNullException();
  126. return GetHostByName (hostName);
  127. }
  128. }
  129. }