Beispiel #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 DNS_rr_MX))
            {
                throw new ArgumentException("Argument obj is not MX_Record !");
            }

            DNS_rr_MX mx = (DNS_rr_MX)obj;

            if (Preference > mx.Preference)
            {
                return(1);
            }
            else if (Preference < mx.Preference)
            {
                return(-1);
            }
            else
            {
                return(0);
            }
        }
Beispiel #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 <DNS_rr_base> 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                     /
             |          /                                               /
             +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
             */

            List <DNS_rr_base> answers = new List <DNS_rr_base>();

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

                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++];

                if ((QTYPE)type == QTYPE.A)
                {
                    answers.Add(DNS_rr_A.Parse(reply, ref offset, rdLength, ttl));
                }
                else if ((QTYPE)type == QTYPE.NS)
                {
                    answers.Add(DNS_rr_NS.Parse(reply, ref offset, rdLength, ttl));
                }
                else if ((QTYPE)type == QTYPE.CNAME)
                {
                    answers.Add(DNS_rr_CNAME.Parse(reply, ref offset, rdLength, ttl));
                }
                else if ((QTYPE)type == QTYPE.SOA)
                {
                    answers.Add(DNS_rr_SOA.Parse(reply, ref offset, rdLength, ttl));
                }
                else if ((QTYPE)type == QTYPE.PTR)
                {
                    answers.Add(DNS_rr_PTR.Parse(reply, ref offset, rdLength, ttl));
                }
                else if ((QTYPE)type == QTYPE.HINFO)
                {
                    answers.Add(DNS_rr_HINFO.Parse(reply, ref offset, rdLength, ttl));
                }
                else if ((QTYPE)type == QTYPE.MX)
                {
                    answers.Add(DNS_rr_MX.Parse(reply, ref offset, rdLength, ttl));
                }
                else if ((QTYPE)type == QTYPE.TXT)
                {
                    answers.Add(DNS_rr_TXT.Parse(reply, ref offset, rdLength, ttl));
                }
                else if ((QTYPE)type == QTYPE.AAAA)
                {
                    answers.Add(DNS_rr_AAAA.Parse(reply, ref offset, rdLength, ttl));
                }
                else if ((QTYPE)type == QTYPE.SRV)
                {
                    answers.Add(DNS_rr_SRV.Parse(reply, ref offset, rdLength, ttl));
                }
                else if ((QTYPE)type == QTYPE.NAPTR)
                {
                    answers.Add(DNS_rr_NAPTR.Parse(reply, ref offset, rdLength, ttl));
                }
                else
                {
                    // Unknown record, skip it.
                    offset += rdLength;
                }
            }

            return(answers);
        }