DnsCache.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Net;
  9. using System.Net.NetworkInformation;
  10. using System.Net.Sockets;
  11. using System.Runtime;
  12. using System.Security.Permissions;
  13. using System.ServiceModel;
  14. using System.ServiceModel.Dispatcher;
  15. using System.Text;
  16. static class DnsCache
  17. {
  18. const int mruWatermark = 64;
  19. static MruCache<string, DnsCacheEntry> resolveCache = new MruCache<string, DnsCacheEntry>(mruWatermark);
  20. static readonly TimeSpan cacheTimeout = TimeSpan.FromSeconds(2);
  21. // Double-checked locking pattern requires volatile for read/write synchronization
  22. static volatile string machineName;
  23. static object ThisLock
  24. {
  25. get
  26. {
  27. return resolveCache;
  28. }
  29. }
  30. public static string MachineName
  31. {
  32. get
  33. {
  34. if (machineName == null)
  35. {
  36. lock (ThisLock)
  37. {
  38. if (machineName == null)
  39. {
  40. try
  41. {
  42. machineName = Dns.GetHostEntry(String.Empty).HostName;
  43. }
  44. catch (SocketException exception)
  45. {
  46. DiagnosticUtility.TraceHandledException(exception,
  47. TraceEventType.Information);
  48. // we fall back to the NetBios machine if Dns fails
  49. machineName = UnsafeNativeMethods.GetComputerName(ComputerNameFormat.PhysicalNetBIOS);
  50. }
  51. }
  52. }
  53. }
  54. return machineName;
  55. }
  56. }
  57. public static IPHostEntry Resolve(Uri uri)
  58. {
  59. string hostName = uri.DnsSafeHost;
  60. IPHostEntry hostEntry = null;
  61. DateTime now = DateTime.UtcNow;
  62. lock (ThisLock)
  63. {
  64. DnsCacheEntry cacheEntry;
  65. if (resolveCache.TryGetValue(hostName, out cacheEntry))
  66. {
  67. if (now.Subtract(cacheEntry.TimeStamp) > cacheTimeout)
  68. {
  69. resolveCache.Remove(hostName);
  70. }
  71. else
  72. {
  73. if (cacheEntry.HostEntry == null)
  74. {
  75. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  76. new EndpointNotFoundException(SR.GetString(SR.DnsResolveFailed, hostName)));
  77. }
  78. hostEntry = cacheEntry.HostEntry;
  79. }
  80. }
  81. }
  82. if (hostEntry == null)
  83. {
  84. SocketException dnsException = null;
  85. try
  86. {
  87. hostEntry = Dns.GetHostEntry(hostName);
  88. }
  89. catch (SocketException e)
  90. {
  91. dnsException = e;
  92. }
  93. lock (ThisLock)
  94. {
  95. // MruCache doesn't have a this[] operator, so we first remove (just in case it exists already)
  96. resolveCache.Remove(hostName);
  97. resolveCache.Add(hostName, new DnsCacheEntry(hostEntry, now));
  98. }
  99. if (dnsException != null)
  100. {
  101. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  102. new EndpointNotFoundException(SR.GetString(SR.DnsResolveFailed, hostName), dnsException));
  103. }
  104. }
  105. return hostEntry;
  106. }
  107. class DnsCacheEntry
  108. {
  109. IPHostEntry hostEntry;
  110. DateTime timeStamp;
  111. public DnsCacheEntry(IPHostEntry hostEntry, DateTime timeStamp)
  112. {
  113. this.hostEntry = hostEntry;
  114. this.timeStamp = timeStamp;
  115. }
  116. public IPHostEntry HostEntry
  117. {
  118. get
  119. {
  120. return hostEntry;
  121. }
  122. }
  123. public DateTime TimeStamp
  124. {
  125. get
  126. {
  127. return timeStamp;
  128. }
  129. }
  130. }
  131. }
  132. }