⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dnscache.cs

📁 A project written in C# sends email without smtp server. It queries dns server for mx records and se
💻 CS
字号:
// $Id: DnsCache.cs,v 1.6 2006/08/07 10:12:29 ethem Exp $
namespace Erle
{
	using System;
	using System.Text;
	using System.Threading;
	using System.Collections;
	using Erle.DnsMail;
	using System.Net;

	internal sealed class DnsCache
	{
		private DnsCache() { }
		private const double REMOVECACHEAFTER = 10.0D;
		private static Hashtable m_CacheTbl = new Hashtable();
		private static DateTime m_NextUpdate = NextUpdate();
		private static DateTime NextUpdate()
		{
			return DateTime.Now.AddMinutes(REMOVECACHEAFTER);
		}

		#region CacheEntry
		private struct CacheEntry
		{
			internal CacheEntry(MxRecord[] data)
			{
				// It will be deleted 2 minutes later.
				DeleteTime = unchecked(Environment.TickCount + 120000);
				RecordObj = data;
			}
			internal int DeleteTime;
			internal MxRecord[] RecordObj;
		}
		#endregion

		internal static MxRecord[] GetFromCache(String domain)
		{
			if (m_NextUpdate < DateTime.Now)
				DeleteAll();

			try
			{
				object val = m_CacheTbl[domain];
				if (val != null)
				{
					CacheEntry entry = (CacheEntry)val;
					if (entry.DeleteTime > Environment.TickCount)
						return entry.RecordObj;
				}
			}
			catch { }
			return null;
		}

		internal static void AddToCache(String domain, MxRecord[] data)
		{
			if ((domain == null) || (data == null))
				throw new ArgumentNullException("data || domain");

			Monitor.Enter(m_CacheTbl);
			try
			{
				m_CacheTbl[domain] = new CacheEntry(data);
			}
			catch { }
			finally { Monitor.Exit(m_CacheTbl); }
		}

		private static void DeleteAll()
		{
			m_NextUpdate = NextUpdate();
			Monitor.Enter(m_CacheTbl);
			try
			{
				m_CacheTbl.Clear();
			}
			catch { }
			finally
			{
				Monitor.Exit(m_CacheTbl);
			}
			OnDeletedAll(EventArgs.Empty);
		}

		private static void OnDeletedAll(EventArgs e)
		{
			try
			{
				if (DeletedAll != null)
					DeletedAll(typeof(DnsCache), e);
			}
			catch { }
		}

		private static bool IsIp4(string address)
		{
			if (address == null ||
				address == string.Empty ||
				address.IndexOf('.') == -1)
				return false;

			int labelCount = 0, octet;
			string[] ips = address.Split(new char[] { '.' });
			for (int i = 0; i < ips.Length; i++)
			{
				try
				{
					octet = int.Parse(ips[i]);
					if ((octet < 0) || (octet > 255))
						return false;
					else
						labelCount++;
				}
				catch
				{
					return false;
				}
			}
			return ((labelCount > 0) && (labelCount < 5));
		}

		internal static event EventHandler DeletedAll;
		internal static void InternalResolve(ref string mailHost, ref bool usedMx, out MxRecord[] mxss, out IPAddress[] ips)
		{
			if (mailHost == null || mailHost == String.Empty ||
				mailHost == "localhost" || mailHost == "127.0.0.1")
				mailHost = DNSAPI.LocalHost;

			if (mailHost[0] >= '1' && mailHost[0] <= '9' && IsIp4(mailHost))
			{
				ips = new IPAddress[] { IPAddress.Parse(mailHost) };
				mxss = null;
				usedMx = false;
				return;
			}

			if (usedMx)
			{
				mxss = DNSAPI.GetMxRecords(mailHost);
				if (mxss != null && mxss.Length > 0)
				{
					ips = null;
					return;
				}
			}

			try
			{
				ips = Dns.Resolve(mailHost).AddressList;
				if (ips != null && ips.Length > 0)
				{
					mxss = null;
					usedMx = false;
					return;
				}
			}
			catch { }
			throw new DnsException(String.Format("No" +
				((usedMx) ? " MaileXchange (MX) or" : String.Empty)
				+ " IP (A) record could be found for {0}.", mailHost));
		}

		static void Log(String log)
		{
			/*
			FileStream fs = new FileStream(@"C:\log.txt",FileMode.Append,FileAccess.Write);
			StreamWriter sw = new StreamWriter(fs); sw.AutoFlush = true;
			sw.Write(DateTime.Now.ToString() + ": " + log + "\r\n");
			sw.Close();	fs.Close(); fs = null; sw = null; log =null;
			*/
		}
		internal static void Debug()
		{

			if (m_CacheTbl == null)
				return;

			CacheEntry ce;
			String crlf = "\r\n";
			StringBuilder sb = new StringBuilder();
			sb.Append("<ul>" + crlf);
			for (IDictionaryEnumerator iex = m_CacheTbl.GetEnumerator(); iex.MoveNext(); )
			{
				ce = (CacheEntry)iex.Value;
				if (ce.RecordObj != null)
				{
					MxRecord[] ht = ce.RecordObj as MxRecord[];
					sb.Append("<li><b>" + iex.Key + "</b> (" + ce.DeleteTime + ")" + crlf + " <ul>" + crlf);
					for (int im = 0; im < ht.Length; im++)
					{
						sb.AppendFormat("\t<li>{0} (", ht[im].NameExchange);
						ArrayList ips = ht[im].IPAddresses;
						for (int i = 0; i < ips.Count; i++)
							sb.Append(ips[i].ToString() + ", ");
						sb.Append(")" + crlf);
					}
					sb.Append(" </ul>" + crlf + "</li>" + crlf);
				}
			}
			sb.Append("</ul>" + crlf);
			System.Web.HttpContext.Current.Response.Write(sb.ToString());

		}
	}
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -