Dns.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. string h_name;
  78. string[] h_aliases, h_addrlist;
  79. bool ret = GetHostByAddr_internal(address, out h_name,
  80. out h_aliases,
  81. out h_addrlist);
  82. if (ret == false) {
  83. throw new SocketException(11001);
  84. }
  85. return(hostent_to_IPHostEntry(h_name, h_aliases,
  86. h_addrlist));
  87. }
  88. public static IPHostEntry GetHostByName(string hostName) {
  89. if (hostName == null)
  90. throw new ArgumentNullException();
  91. string h_name;
  92. string[] h_aliases, h_addrlist;
  93. bool ret = GetHostByName_internal(hostName, out h_name,
  94. out h_aliases,
  95. out h_addrlist);
  96. if (ret == false)
  97. throw new SocketException(11001);
  98. return(hostent_to_IPHostEntry(h_name, h_aliases,
  99. h_addrlist));
  100. }
  101. /// <summary>
  102. /// This method returns the host name associated with the local host.
  103. /// </summary>
  104. public static string GetHostName() {
  105. string hostName;
  106. bool ret = GetHostName_internal(out hostName);
  107. if (ret == false)
  108. throw new SocketException(11001);
  109. return hostName;
  110. }
  111. /// <summary>
  112. /// This method resolves a DNS-style host name or IP
  113. /// address.
  114. /// </summary>
  115. /// <param name=hostName>
  116. /// A string containing either a DNS-style host name (e.g.
  117. /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
  118. /// </param>
  119. public static IPHostEntry Resolve(string hostName) {
  120. if (hostName == null)
  121. throw new ArgumentNullException();
  122. return GetHostByName (hostName);
  123. }
  124. }
  125. }