示例#1
0
        /// <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 = new MX_Record[0];

            //--- 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);
        }
示例#2
0
        /// <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[] _result = 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) == false)
                        {
                            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);
                _result = mxBuff;
            }
            catch
            {
            }

            return(_result);
        }