Exemplo n.º 1
0
        private byte[] GetQuery(byte[] query, int queryID)
        {
            byte[] retVal = null;

            try
            {
                int helper = 0;
                int count  = 0;
                while (count < 10)
                {
                    if (count > m_DnsServers.Length)
                    {
                        helper = 0;
                    }

                    byte[]      reply   = QueryServer(query, m_DnsServers[helper]);
                    Dns_Answers answers = new Dns_Answers();

                    // If reply is ok, return it
                    if (reply != null && answers.ParseAnswers(reply, queryID))
                    {
                        return(reply);
                    }

                    count++;
                    helper++;
                }
            }
            catch {
            }

            return(retVal);
        }
Exemplo n.º 2
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)
        {
            //--- Try to get MX records from cache
            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);
        }