コード例 #1
0
        /// <summary>
        /// Compares the current instance with another object of the same type.
        /// </summary>
        /// <param name="obj">An object to compare with this instance. </param>
        /// <returns>Returns 0 if two objects are equal, returns negative value if this object is less,
        /// returns positive value if this object is grater.</returns>
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (!(obj is DnsRecord_MX))
            {
                throw new ArgumentException("Argument obj is not MX_Record !");
            }

            DnsRecord_MX mx = (DnsRecord_MX)obj;

            if (this.Preference > mx.Preference)
            {
                return(1);
            }
            else if (this.Preference < mx.Preference)
            {
                return(-1);
            }
            else
            {
                return(0);
            }
        }
コード例 #2
0
        /// <summary>
        /// Parses specified count of answers from query.
        /// </summary>
        /// <param name="reply">Server returned query.</param>
        /// <param name="answerCount">Number of answers to parse.</param>
        /// <param name="offset">Position from where to start parsing answers.</param>
        /// <returns></returns>
        private List <DnsRecord> ParseAnswers(byte[] reply, int answerCount, ref int offset)
        {
            /* RFC 1035 4.1.3. Resource record format
             *
             *                                                                     1  1  1  1  1  1
             *           0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
             |                                               |
             |          /                                               /
             |          /                      NAME                     /
             |                                               |
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
             |                      TYPE                     |
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
             |                     CLASS                     |
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
             |                      TTL                      |
             |                                               |
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
             |                   RDLENGTH                    |
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
             |          /                     RDATA                     /
             |          /                                               /
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
             */

            var answers = new List <DnsRecord>();

            //---- Start parsing answers ------------------------------------------------------------------//
            for (int i = 0; i < answerCount; i++)
            {
                string name = "";
                if (!GetQName(reply, ref offset, ref name))
                {
                    break;
                }

                int type     = reply[offset++] << 8 | reply[offset++];
                int rdClass  = reply[offset++] << 8 | reply[offset++];
                int ttl      = reply[offset++] << 24 | reply[offset++] << 16 | reply[offset++] << 8 | reply[offset++];
                int rdLength = reply[offset++] << 8 | reply[offset++];

                var _type = (DnsRecordType)type;
                if (_type == DnsRecordType.A)
                {
                    answers.Add(DnsRecord_A.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if (_type == DnsRecordType.AAAA)
                {
                    answers.Add(DnsRecord_AAAA.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if (_type == DnsRecordType.NS)
                {
                    answers.Add(DnsRecord_NS.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if (_type == DnsRecordType.CNAME)
                {
                    answers.Add(DnsRecord_CNAME.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if (_type == DnsRecordType.MX)
                {
                    answers.Add(DnsRecord_MX.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if (_type == DnsRecordType.TXT)
                {
                    answers.Add(DnsRecord_TXT.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if (_type == DnsRecordType.SOA)
                {
                    answers.Add(DnsRecord_SOA.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if (_type == DnsRecordType.PTR)
                {
                    answers.Add(DnsRecord_PTR.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if (_type == DnsRecordType.HINFO)
                {
                    answers.Add(DnsRecord_HINFO.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if (_type == DnsRecordType.SRV)
                {
                    answers.Add(DnsRecord_SRV.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if (_type == DnsRecordType.NAPTR)
                {
                    answers.Add(DnsRecord_NAPTR.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else if (_type == DnsRecordType.SPF)
                {
                    answers.Add(DnsRecord_SPF.Parse(name, reply, ref offset, rdLength, ttl));
                }
                else
                {
                    // Unknown record, skip it.
                    offset += rdLength;
                }
            }

            return(answers);
        }