コード例 #1
0
ファイル: DnsQuestion.cs プロジェクト: gonzalop/Mono.Dns
 internal int Init(DnsPacket packet, int offset)
 {
     name   = packet.ReadName(ref offset);
     type   = (DnsQType)packet.ReadUInt16(ref offset);
     _class = (DnsQClass)packet.ReadUInt16(ref offset);
     return(offset);
 }
コード例 #2
0
ファイル: DnsQuestion.cs プロジェクト: westybsa/mono
		internal int Init (DnsPacket packet, int offset)
		{
			name = packet.ReadName (ref offset);
			type = (DnsQType) packet.ReadUInt16 (ref offset);
			_class = (DnsQClass) packet.ReadUInt16 (ref offset);
			return offset;
		}
コード例 #3
0
        public DnsQuery(string name, DnsQType qtype, DnsQClass qclass)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            int length = DnsUtil.GetEncodedLength(name);

            if (length == -1)
            {
                throw new ArgumentException("Invalid DNS name", "name");
            }

            length  += 12 + 2 + 2;            // Header + qtype + qclass
            packet   = new byte [length];
            header   = new DnsHeader(packet, 0);
            position = 12;
            WriteDnsName(name);
            WriteUInt16((ushort)qtype);
            WriteUInt16((ushort)qclass);
            Header.QuestionCount    = 1;
            Header.IsQuery          = true;
            Header.RecursionDesired = true;
        }
コード例 #4
0
        private byte[] data;                // Resource record data

        /// <summary>
        /// This constructor initializes an empty DNS resource
        /// record.
        /// </summary>
        public DnsRR()
        {
            this.rrtype = DnsRRType.UNKNOWN;
            this.rname  = string.Empty;
            this.qclass = DnsQClass.IN;
            this.ttl    = 0;
            this.data   = new byte[0];
        }
コード例 #5
0
 /// <summary>
 /// Shallow copies the data members from the source message passed
 /// to this instance.
 /// </summary>
 /// <param name="source">The source message.</param>
 protected void CopyFrom(DnsMessage source)
 {
     this.qid         = source.qid;
     this.flags       = source.flags;
     this.qname       = source.qname;
     this.qtype       = source.qtype;
     this.qclass      = source.qclass;
     this.answers     = source.answers;
     this.authorities = source.authorities;
     this.additional  = source.additional;
 }
コード例 #6
0
        private Dictionary <string, int> namePtrs;              // Set of existing name compression pointers

        /// <summary>
        /// This constructor initializes the message fields to default values.
        /// </summary>
        public DnsMessage()
        {
            this.qid         = 0;
            this.flags       = 0;
            this.qname       = null;
            this.qtype       = DnsQType.NULL;
            this.qclass      = DnsQClass.IN;
            this.answers     = null;
            this.authorities = null;
            this.additional  = null;
            this.namePtrs    = null;
        }
コード例 #7
0
        /// <summary>
        /// This method parses the resource record from the packet beginning
        /// at the offset passed.
        /// </summary>
        /// <param name="message">The message containing the record being parsed.</param>
        /// <param name="packet">The packet buffer.</param>
        /// <param name="offset">
        /// The offset in the packet where the record is located.
        /// This parameter will return set to the offset of the first
        /// byte after the parsed record.
        /// </param>
        /// <returns>True on success.</returns>
        public bool Read(DnsMessage message, byte[] packet, ref int offset)
        {
            if (!message.ReadName(packet, ref offset, out rname))
            {
                return(false);
            }

            rrtype = (DnsRRType)Helper.ReadInt16(packet, ref offset);
            qclass = (DnsQClass)Helper.ReadInt16(packet, ref offset);
            ttl    = Helper.ReadInt32(packet, ref offset);

            return(ReadData(message, packet, ref offset));
        }
コード例 #8
0
ファイル: DnsQuery.cs プロジェクト: ermau/Mono.Dns
		public DnsQuery (string name, DnsQType qtype, DnsQClass qclass)
		{
			if (String.IsNullOrEmpty (name))
				throw new ArgumentNullException ("name");

			int length = DnsUtil.GetEncodedLength (name);
			if (length == -1)
				throw new ArgumentException ("Invalid DNS name", "name");

			length += 12 + 2 + 2; // Header + qtype + qclass
			packet = new byte [length];
			header = new DnsHeader (packet, 0);
			position = 12;
			WriteDnsName (name);
			WriteUInt16 ((ushort) qtype);
			WriteUInt16 ((ushort) qclass);
			Header.QuestionCount = 1;
			Header.IsQuery = true;
			Header.RecursionDesired = true;
		}
コード例 #9
0
		static DnsQuery GetQuery (string host, DnsQType q, DnsQClass c)
		{
			return new DnsQuery (host, q, c);
		}
コード例 #10
0
        /// <summary>
        /// This method parses the raw DNS message packet passed and initializes
        /// the message properties.
        /// </summary>
        /// <param name="packet">The raw DNS packet received.</param>
        /// <param name="cbPacket">Number of bytes in that packet.</param>
        /// <returns>True on success.</returns>
        public bool ParsePacket(byte[] packet, int cbPacket)
        {
            uint qdCount;
            uint anCount;
            uint nsCount;
            uint arCount;
            int  pos;

            // Read the message header

            pos      = 0;
            this.qid = (ushort)Helper.ReadInt16(packet, ref pos); if (pos > cbPacket)
            {
                return(false);
            }
            this.flags = (DnsFlag)Helper.ReadInt16(packet, ref pos); if (pos > cbPacket)
            {
                return(false);
            }
            qdCount = (ushort)Helper.ReadInt16(packet, ref pos); if (pos > cbPacket)
            {
                return(false);
            }
            anCount = (ushort)Helper.ReadInt16(packet, ref pos); if (pos > cbPacket)
            {
                return(false);
            }
            nsCount = (ushort)Helper.ReadInt16(packet, ref pos); if (pos > cbPacket)
            {
                return(false);
            }
            arCount = (ushort)Helper.ReadInt16(packet, ref pos); if (pos > cbPacket)
            {
                return(false);
            }

            Assertion.Test(pos == 12);

            // Check for unsupported packets

            if ((this.flags & DnsFlag.TC) != 0)     // truncation bit is set
            {
                return(false);
            }

            if (this.Opcode != DnsOpcode.QUERY)     // only accept standard queries
            {
                return(false);
            }

            if (qdCount != 1)                       // only supporting one question
            {
                return(false);
            }

            // Parse the question

            if (!ReadName(packet, ref pos, out this.qname))
            {
                return(false);
            }

            if (pos > cbPacket)
            {
                return(false);
            }

            this.qtype = (DnsQType)Helper.ReadInt16(packet, ref pos); if (pos > cbPacket)
            {
                return(false);
            }
            this.qclass = (DnsQClass)Helper.ReadInt16(packet, ref pos); if (pos > cbPacket)
            {
                return(false);
            }

            // We're only going to accept Internet queries

            if (this.qclass != DnsQClass.IN)
            {
                return(false);
            }

            // Parse the answer records

            for (int i = 0; i < anCount; i++)
            {
                var rr = DnsRR.Parse(this, packet, ref pos);

                if (rr == null || pos > cbPacket)
                {
                    return(false);
                }

                this.Answers.Add(rr);
            }

            // Parse the authority records

            for (int i = 0; i < nsCount; i++)
            {
                var rr = DnsRR.Parse(this, packet, ref pos);

                if (rr == null || pos > cbPacket)
                {
                    return(false);
                }

                this.Authorities.Add(rr);
            }

            // Parse the additional records

            for (int i = 0; i < arCount; i++)
            {
                var rr = DnsRR.Parse(this, packet, ref pos);

                if (rr == null || pos > cbPacket)
                {
                    return(false);
                }

                this.Additional.Add(rr);
            }

            // $todo(jeff.lill):
            //
            // Delete this once we've figured out why the DNS server
            // sometimes get requests with NULL QNAME properties.

            if (this.QName == null)
            {
                var packetBytes     = Helper.Extract(packet, cbPacket);
                var hexDump         = Helper.HexDump(packetBytes, 16, HexDumpOption.ShowAll);
                var diagnostics     = GetTraceDetails(IPAddress.Any);
                var extendedLogInfo = new StringSysLogEntryExtension(diagnostics + "\r\n\r\nPacket:\r\n\r\n" + hexDump);

                SysLog.LogWarning(extendedLogInfo, "DNS Packet with QNAME=NULL");
            }

            return(true);
        }
コード例 #11
0
 static DnsQuery GetQuery(string host, DnsQType q, DnsQClass c)
 {
     return(new DnsQuery(host, q, c));
 }