/// <summary> /// Gets MX records from answer collection and ORDERS them by preference. /// NOTE: Duplicate preference records are appended to end. /// </summary> /// <returns></returns> internal MX_Record[] GetMxRecordsFromAnswers() { MX_Record[] retVal = null; try { SortedList mx = new SortedList(); ArrayList duplicateList = new ArrayList(); foreach(Dns_Answer answer in m_Answers){ if(answer.QTYPE == QTYPE.MX){ MX_Record mxRec = (MX_Record)answer.RecordObj; if(!mx.Contains(mxRec.Preference)){ mx.Add(mxRec.Preference,mxRec); } else{ duplicateList.Add(mxRec); } } } MX_Record[] mxBuff = new MX_Record[mx.Count + duplicateList.Count]; mx.Values.CopyTo(mxBuff,0); duplicateList.CopyTo(mxBuff,mx.Count); retVal = mxBuff; } catch{ } return retVal; }
/// <summary> /// Adds domain's MX records to cache. /// </summary> /// <param name="domain"></param> /// <param name="mx"></param> public static void AddMXToCache(string domain,MX_Record[] mx) { try { lock(m_ChacheTbl){ // Remove old cache entry, if any. if(m_ChacheTbl.Contains(domain + "[MX]")){ m_ChacheTbl.Remove(domain + "[MX]"); } m_ChacheTbl.Add(domain + "[MX]",new CacheEntry(mx,Environment.TickCount)); } } catch//(Exception x) { // Console.WriteLine(x.Message); } }
public DnsReplyCode GetMXRecords(string domain, out MX_Record[] mxRecords) { mxRecords = null; ArrayList answers = QueryServer(domain, QTYPE.MX, 1).Answers; if (answers != null) { SortedList mx = new SortedList(); ArrayList duplicateList = new ArrayList(); foreach (object answer in answers) { if (answer is MX_Record) { MX_Record mxRec = (MX_Record)answer; if (!mx.Contains(mxRec.Preference)) { mx.Add(mxRec.Preference, mxRec); } else { duplicateList.Add(mxRec); } } } mxRecords = new MX_Record[mx.Count + duplicateList.Count]; mx.Values.CopyTo(mxRecords, 0); duplicateList.CopyTo(mxRecords, mx.Count); } if (mxRecords == null || mxRecords.Length == 0) { return(DnsReplyCode.NoEntries); } return(DnsReplyCode.Ok); }
/// <summary> /// Gets MX records.(MX records are sorted by preference, lower array element is prefered) /// </summary> /// <returns></returns> public MX_Record[] GetMXRecords() { MX_Record[] mxRecords = (MX_Record[])FilterRecords(m_pAnswers, typeof(MX_Record)).ToArray(typeof(MX_Record)); SortedList mx = new SortedList(); ArrayList duplicateList = new ArrayList(); foreach (MX_Record mxRecord in mxRecords) { if (!mx.Contains(mxRecord.Preference)) { mx.Add(mxRecord.Preference, mxRecord); } else { duplicateList.Add(mxRecord); } } mxRecords = new MX_Record[mx.Count + duplicateList.Count]; mx.Values.CopyTo(mxRecords, 0); duplicateList.CopyTo(mxRecords, mx.Count); return(mxRecords); }
/// <summary> /// Gets MX records.(MX records are sorted by preference, lower array element is prefered) /// </summary> /// <param name="domain"></param> /// <param name="mxRecords"></param> /// <returns></returns> public DnsReplyCode GetMXRecords(string domain,out MX_Record[] mxRecords) { mxRecords = null; //--- Try to get MX records from cache if(m_UseDnsCache){ mxRecords = DnsCache.GetMXFromCache(domain); if(mxRecords != null){ // Console.WriteLine("domain:" + domain + " from cahce."); return DnsReplyCode.Ok; } } //------------------------------------// Dns_Header header = new Dns_Header(DnsEx.ID,OPCODE.IQUERY); Dns_Query query = new Dns_Query(domain,QTYPE.MX,1); byte[] bQuery = query.GetQuery(header); byte[] reply = GetQuery(bQuery,header.ID); if(reply != null){ Dns_Answers answers = new Dns_Answers(); if(answers.ParseAnswers(reply,header.ID)){ mxRecords = answers.GetMxRecordsFromAnswers(); if(mxRecords != null){ // Add to cache DnsCache.AddMXToCache(domain,mxRecords); return DnsReplyCode.Ok; } else{ return DnsReplyCode.NoEntries; } } } return DnsReplyCode.TempError; }
/// <summary> /// Gets MX records.(MX records are sorted by preference, lower array element is prefered) /// </summary> /// <param name="domain"></param> /// <param name="mxRecords"></param> /// <returns></returns> public DnsReplyCode GetMXRecords(string domain,out MX_Record[] mxRecords) { mxRecords = null; ArrayList answers = QueryServer(domain,QTYPE.MX,1); if(answers != null){ SortedList mx = new SortedList(); ArrayList duplicateList = new ArrayList(); foreach(object answer in answers){ if(answer is MX_Record){ MX_Record mxRec = (MX_Record)answer; if(!mx.Contains(mxRec.Preference)){ mx.Add(mxRec.Preference,mxRec); } else{ duplicateList.Add(mxRec); } } } mxRecords = new MX_Record[mx.Count + duplicateList.Count]; mx.Values.CopyTo(mxRecords,0); duplicateList.CopyTo(mxRecords,mx.Count); } if(mxRecords.Length == 0){ return DnsReplyCode.NoEntries; } return DnsReplyCode.Ok; }