Dns.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist) {
  56. IPHostEntry he = new IPHostEntry();
  57. IPAddress[] addrlist = new IPAddress[h_addrlist.Length];
  58. he.HostName=h_name;
  59. he.Aliases=h_aliases;
  60. for(int i=0; i<h_addrlist.Length; i++) {
  61. addrlist[i]=IPAddress.Parse(h_addrlist[i]);
  62. }
  63. he.AddressList=addrlist;
  64. return(he);
  65. }
  66. public static IPHostEntry GetHostByAddress(IPAddress address) {
  67. if (address == null)
  68. throw new ArgumentNullException();
  69. return GetHostByAddress(address.ToString());
  70. }
  71. public static IPHostEntry GetHostByAddress(string address) {
  72. if (address == null) {
  73. throw new ArgumentNullException();
  74. }
  75. string h_name;
  76. string[] h_aliases, h_addrlist;
  77. bool ret = GetHostByAddr_internal(address, out h_name,
  78. out h_aliases,
  79. out h_addrlist);
  80. if (ret == false) {
  81. throw new SocketException(11001);
  82. }
  83. return(hostent_to_IPHostEntry(h_name, h_aliases,
  84. h_addrlist));
  85. }
  86. public static IPHostEntry GetHostByName(string hostName) {
  87. if (hostName == null)
  88. throw new ArgumentNullException();
  89. string h_name;
  90. string[] h_aliases, h_addrlist;
  91. bool ret = GetHostByName_internal(hostName, out h_name,
  92. out h_aliases,
  93. out h_addrlist);
  94. if (ret == false)
  95. throw new SocketException(11001);
  96. return(hostent_to_IPHostEntry(h_name, h_aliases,
  97. h_addrlist));
  98. }
  99. /// <summary>
  100. /// This method returns the host name associated with the local host.
  101. /// </summary>
  102. public static string GetHostName() {
  103. IPHostEntry h = GetHostByAddress("127.0.0.1");
  104. return h.HostName;
  105. }
  106. /// <summary>
  107. /// This method resolves a DNS-style host name or IP
  108. /// address.
  109. /// </summary>
  110. /// <param name=hostName>
  111. /// A string containing either a DNS-style host name (e.g.
  112. /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
  113. /// </param>
  114. public static IPHostEntry Resolve(string hostName) {
  115. if (hostName == null)
  116. throw new ArgumentNullException();
  117. return GetHostByName (hostName);
  118. }
  119. }
  120. }