Example #1
0
 /// <summary>
 /// Reads data into this RR from the DNS wire format data in <paramref name="reader"/>
 /// </summary>
 /// <param name="reader">Reader in which wire format data for this RR is already buffered.</param>
 protected override void DeserializeRecordData(ref DnsBufferReader reader)
 {
     this.Priority = reader.ReadUShort();
     this.Weight   = reader.ReadUShort();
     this.Port     = reader.ReadUShort();
     this.Target   = reader.ReadDomainName();
 }
Example #2
0
            internal void Deserialize(ref DnsBufferReader reader)
            {
                this.Name = reader.ReadDomainName();
                this.Type = (DnsStandard.RecordType)reader.ReadShort();

                this.Class            = (DnsStandard.Class)reader.ReadShort();
                this.TTL              = reader.ReadInt();
                this.RecordDataLength = reader.ReadShort();
            }
Example #3
0
        /// <summary>
        /// Reads data into this RR from the DNS wire format data in <paramref name="reader"/>
        /// </summary>
        /// <param name="reader">Reader in which wire format data for this RR is already buffered.</param>
        protected override void DeserializeRecordData(ref DnsBufferReader reader)
        {
            ushort certType = reader.ReadUShort();

            if (certType > (ushort)CertificateType.IACPKIK)
            {
                throw new DnsProtocolException(DnsProtocolError.InvalidCertRecord);
            }
            m_certType  = (CertificateType)certType;
            m_keyTag    = reader.ReadUShort();
            m_algorithm = reader.ReadByte();
            m_certData  = reader.ReadBytes(this.RecordDataLength - 5); // 5 == # of bytes we've already read (certType, keytag etc)

            this.EnsureDnsCert();
        }
Example #4
0
        /// <summary>
        /// Deserialize the buffer into a DnsResourceRecord object
        /// </summary>
        /// <param name="reader">reader over a buffer containing raw Dns record bytes</param>
        /// <returns>DnsResourceRecord</returns>
        public static DnsResourceRecord Deserialize(ref DnsBufferReader reader)
        {
            //
            // We have to parse the header before we can figure out what kind of record this is
            //
            DnsResourceRecordHeader header = new DnsResourceRecordHeader();

            header.Deserialize(ref reader);

            DnsResourceRecord record = DnsResourceRecord.CreateRecordObject(header.Type);

            record.Deserialize(ref header, ref reader);

            return(record);
        }
        internal void Deserialize(int recordCount, ref DnsBufferReader reader)
        {
            if (recordCount < 0)
            {
                throw new DnsProtocolException(DnsProtocolError.InvalidRecordCount);
            }

            if (recordCount > 0)
            {
                this.EnsureCapacity(recordCount);
                for (int irecord = 0; irecord < recordCount; ++irecord)
                {
                    this.Add(DnsResourceRecord.Deserialize(ref reader));
                }
            }
        }
Example #6
0
        /// <summary>
        /// Reads data into this RR from the DNS wire format data in <paramref name="reader"/>
        /// </summary>
        /// <param name="reader">Reader in which wire format data for this RR is already buffered.</param>
        protected override void DeserializeRecordData(ref DnsBufferReader reader)
        {
            List <string> stringList = new List <string>();

            int maxIndex = reader.Index + this.RecordDataLength;

            while (reader.Index < maxIndex)
            {
                StringBuilder sb = reader.EnsureStringBuilder();
                int           cb = reader.ReadByte();
                while (cb-- > 0)
                {
                    sb.Append(reader.ReadChar());
                }
                stringList.Add(sb.ToString());
            }

            this.Strings = stringList;
        }
Example #7
0
        //--------------------------------------------
        //
        // TCP Transport
        //
        //--------------------------------------------
        void ExecuteTCP()
        {
            using (Socket tcpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                tcpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, this.TimeoutInMilliseconds);
                tcpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, this.TimeoutInMilliseconds);

                tcpSocket.Connect(m_dnsServer);

                try
                {
                    m_lengthBuffer.Clear();
                    m_lengthBuffer.AddUshort((ushort)m_requestBuffer.Count);

                    tcpSocket.Send(m_lengthBuffer.Buffer, m_lengthBuffer.Count, SocketFlags.None);
                    tcpSocket.Send(m_requestBuffer.Buffer, m_requestBuffer.Count, SocketFlags.None);
                    //
                    // First, receive the response message length
                    //
                    m_lengthBuffer.Clear();
                    m_lengthBuffer.Count = this.ReadTcp(tcpSocket, m_lengthBuffer.Buffer, m_lengthBuffer.Capacity);
                    //
                    // Read length of the response to come
                    //
                    DnsBufferReader reader       = m_lengthBuffer.CreateReader();
                    ushort          responseSize = reader.ReadUShort();
                    m_responseBuffer.ReserveCapacity(responseSize);
                    //
                    // Now receive the real response
                    //
                    m_responseBuffer.Count = this.ReadTcp(tcpSocket, m_responseBuffer.Buffer, responseSize);
                }
                finally
                {
                    tcpSocket.Shutdown(SocketShutdown.Both);
                }
            }
        }
Example #8
0
        /// <summary>
        /// Returns a list of dns response entries that has been loaded from the files that are part of the solution
        /// </summary>
        /// <remarks>
        /// Verifies that list has been properly loaded up. These are only A record responses.
        /// </remarks>
        protected void PopulateMockDnsARecordResponseEntries()
        {
            m_responses = new List<DnsResponse>();
            foreach (string s in MockDomainResponses)
            {
                byte[] buff;
                DnsResponse dr;

                string fileName = Path.GetFullPath(Path.Combine(m_filePath, s + ".bin"));
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    Dump("checking [{0}]", fileName);
                    buff = new BinaryReader(fs).ReadBytes((int)new FileInfo(fileName).Length);
                    DnsBufferReader reader = new DnsBufferReader(buff, 0, buff.Count());

                    // get a dr 
                    dr = new DnsResponse(reader);
                    m_responses.Add(dr);
                }

                // ensure that the qusetion QName matches the name of the mocked entry
                Assert.True(dr.Question.Domain.ToLower().Contains(s.ToLower().Replace("aname.","")));
            }
        }
Example #9
0
        /// <summary>
        /// Reads DNS wire format data to this response.
        /// </summary>
        /// <param name="reader">The reader that has already buffered response data.</param>
        protected override void Deserialize(ref DnsBufferReader reader)
        {
            base.Deserialize(ref reader);
            if (reader.IsDone)
            {
                //
                // No answers!
                //
                return;
            }

            if (this.Header.AnswerCount > 0)
            {
                this.AnswerRecords.Deserialize(this.Header.AnswerCount, ref reader);
            }
            if (this.Header.NameServerAnswerCount > 0)
            {
                this.NameServerRecords.Deserialize(this.Header.NameServerAnswerCount, ref reader);
            }
            if (this.Header.AdditionalAnswerCount > 0)
            {
                this.AdditionalRecords.Deserialize(this.Header.AdditionalAnswerCount, ref reader);
            }
        }
Example #10
0
 internal DnsQuestion(ref DnsBufferReader reader)
 {
     this.Deserialize(ref reader);
 }
Example #11
0
 /// <summary>
 /// Deserialize this message
 /// </summary>
 /// <param name="reader"></param>        
 protected virtual void Deserialize(ref DnsBufferReader reader)
 {
     m_header = new DnsHeader(ref reader);
     m_question = new DnsQuestion(ref reader);
 }
Example #12
0
        public void CreateDnsResourceRecords(string domain)
        {
            DnsBuffer buff = new DnsBuffer();
            byte[] bytes;
            AddressRecord arec = new AddressRecord(domain
                , "127.0.0.1") {TTL = 1000};
            arec.Serialize(buff);

            string path = Path.Combine(DNSRECORDSEPATH, string.Format("aname.{0}.bin", domain));
            Console.WriteLine("Creating {0}", path);

            using (FileStream s = new FileStream(path, FileMode.OpenOrCreate))
            {
                s.Write(buff.Buffer
                        , 0
                        , buff.Buffer.Length);
                s.Close();
            }

            
            //----------------------------------------------------------------------------------------------------
            //---read the stream from the bytes
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                Console.WriteLine("checking [{0}]", path);
                bytes = new BinaryReader(fs).ReadBytes((int)new FileInfo(path).Length);
                DnsBufferReader rdr = new DnsBufferReader(bytes, 0, bytes.Length);
                arec = (AddressRecord)DnsResourceRecord.Deserialize(ref rdr);
            }
            Console.WriteLine(arec.IPAddress);
            Console.WriteLine(arec.TTL);
            Console.WriteLine(arec.Name);
            //----------------------------------------------------------------------------------------------------------------
            SOARecord soa = new SOARecord(domain
                , domain + ".dom"
                , "somebody"
                , 1
                , 2
                , 3
                , 4
                , 5) {TTL = 2000};
            buff = new DnsBuffer();
            soa.Serialize(buff);

            path = Path.Combine(DNSRECORDSEPATH, string.Format("soa.{0}.bin", domain));
            Console.WriteLine("Creating {0}", path);

            using (FileStream s = new FileStream(path, FileMode.OpenOrCreate))
            {
                s.Write(buff.Buffer
                        , 0
                        , buff.Buffer.Length);
                s.Close();
            }

            //----------------------------------------------------------------------------------------------------
            //---read the stream from the bytes
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                Console.WriteLine("checking [{0}]", path);
                bytes = new BinaryReader(fs).ReadBytes((int)new FileInfo(path).Length);
                DnsBufferReader rdr = new DnsBufferReader(bytes, 0, bytes.Length);
                soa = (SOARecord)DnsResourceRecord.Deserialize(ref rdr);
            }
            Console.WriteLine(soa.ResponsibleName);
            Console.WriteLine(soa.SerialNumber);
            Console.WriteLine(soa.Retry);
            Console.WriteLine(soa.Refresh);
            Console.WriteLine(soa.Expire);
            Console.WriteLine(soa.Minimum);
            Console.WriteLine(soa.TTL);
            Console.WriteLine(soa.Name);
            //----------------------------------------------------------------------------------------------------------------
            MXRecord mx = new MXRecord(domain
                , string.Format("mx.{0}", domain)
                , 1) {TTL = 2000};

            buff = new DnsBuffer();
            mx.Serialize(buff);

            path = Path.Combine(DNSRECORDSEPATH, string.Format("mx.{0}.bin", domain));
            Console.WriteLine("Creating {0}", path);

            using (FileStream s = new FileStream(path, FileMode.OpenOrCreate))
            {
                s.Write(buff.Buffer
                        , 0
                        , buff.Buffer.Length);
                s.Close();
            }

            //----------------------------------------------------------------------------------------------------
            //---read the stream from the bytes
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                Console.WriteLine("checking [{0}]", path);
                bytes = new BinaryReader(fs).ReadBytes((int)new FileInfo(path).Length);
                DnsBufferReader rdr = new DnsBufferReader(bytes, 0, bytes.Length);
                mx = (MXRecord)DnsResourceRecord.Deserialize(ref rdr);
            }
            Console.WriteLine(mx.Exchange);
            Console.WriteLine(mx.Name);
            Console.WriteLine(mx.Preference);

            //----------------------------------------------------------------------------------------------------------------
            //---create the cert on the fly
            CertRecord cert = new CertRecord(new DnsX509Cert(CreateNamedKeyCertificate(new CertData(domain
                , domain
                , string.Format("CN={0}", domain)
                , "")))) {TTL = 2000};

            buff = new DnsBuffer();
            cert.Serialize(buff);

            path = Path.Combine(DNSRECORDSEPATH, string.Format("cert.{0}.bin", domain));
            Console.WriteLine("Creating {0}", path);

            using (FileStream s = new FileStream(path, FileMode.OpenOrCreate))
            {
                s.Write(buff.Buffer
                        , 0
                        , buff.Buffer.Length);
                s.Close();
            }

            //----------------------------------------------------------------------------------------------------
            //---read the stream from the bytes
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                Console.WriteLine("checking [{0}]", path);
                bytes = new BinaryReader(fs).ReadBytes((int)new FileInfo(path).Length);
                DnsBufferReader rdr = new DnsBufferReader(bytes, 0, bytes.Length);
                cert = (CertRecord)DnsResourceRecord.Deserialize(ref rdr);
            }
            Console.WriteLine(cert.Name);
            Console.WriteLine(cert.Cert.Certificate.NotBefore);
            Console.WriteLine(cert.Cert.Certificate.NotAfter);
        }
Example #13
0
 internal void Deserialize(ref DnsBufferReader reader)
 {
     this.Domain = reader.ReadDomainName();
     this.Type = (DnsStandard.RecordType) reader.ReadShort();
     this.Class = (DnsStandard.Class) reader.ReadShort();
 }
Example #14
0
            internal void Deserialize(ref DnsBufferReader reader)
            {
                this.Name = reader.ReadDomainName();
                this.Type = (DnsStandard.RecordType) reader.ReadShort();

                this.Class = (DnsStandard.Class) reader.ReadShort();
                this.TTL = reader.ReadInt();
                this.RecordDataLength = reader.ReadShort();
            }
Example #15
0
 /// <summary>
 /// Reads data into this RR from the DNS wire format data in <paramref name="reader"/>
 /// </summary>
 /// <param name="reader">Reader in which wire format data for this RR is already buffered.</param>
 protected override void DeserializeRecordData(ref DnsBufferReader reader)
 {
     ushort certType = reader.ReadUShort();
     if (certType > (ushort) CertificateType.IACPKIK)
     {
         throw new DnsProtocolException(DnsProtocolError.InvalidCertRecord);
     } 
     m_certType = (CertificateType) certType;
     m_keyTag = reader.ReadUShort();
     m_algorithm = reader.ReadByte();
     m_certData = reader.ReadBytes(this.RecordDataLength - 5); // 5 == # of bytes we've already read (certType, keytag etc)
     
     this.EnsureDnsCert();            
 }
Example #16
0
 /// <summary>
 /// Instantiate a new instance with the provided <paramref name="reader"/>
 /// </summary>
 /// <param name="reader">The reader that has been initialized with the response buffer.</param>
 public DnsResponse(DnsBufferReader reader)
     : base(ref reader)
 {
 }
Example #17
0
        /// <summary>
        /// Deserialize the buffer into a DnsResourceRecord object
        /// </summary>
        /// <param name="reader">reader over a buffer containing raw Dns record bytes</param>
        /// <returns>DnsResourceRecord</returns>                
        public static DnsResourceRecord Deserialize(ref DnsBufferReader reader)
        {
            //
            // We have to parse the header before we can figure out what kind of record this is
            //
            DnsResourceRecordHeader header = new DnsResourceRecordHeader();
            header.Deserialize(ref reader);

            DnsResourceRecord record = DnsResourceRecord.CreateRecordObject(header.Type);
            record.Deserialize(ref header, ref reader);

            return record;
        }
Example #18
0
 /// <summary>
 /// Reads data into this RR from the DNS wire format data in <paramref name="reader"/>
 /// </summary>
 /// <param name="reader">Reader in which wire format data for this RR is already buffered.</param>
 protected override void DeserializeRecordData(ref DnsBufferReader reader)
 {
     this.Address = reader.ReadUint();
 }
Example #19
0
 internal void Deserialize(ref DnsBufferReader reader)
 {
     this.Domain = reader.ReadDomainName();
     this.Type   = (DnsStandard.RecordType)reader.ReadShort();
     this.Class  = (DnsStandard.Class)reader.ReadShort();
 }
Example #20
0
 /// <summary>
 /// Instantiates a new message object by deserializing from the given reader
 /// </summary>
 /// <param name="reader"></param>
 protected DnsMessage(ref DnsBufferReader reader)
 {
     this.Deserialize(ref reader);
 }
Example #21
0
        /// <summary>
        /// Reads data into this RR from the DNS wire format data in <paramref name="reader"/>
        /// </summary>
        /// <param name="reader">Reader in which wire format data for this RR is already buffered.</param>
        protected override void DeserializeRecordData(ref DnsBufferReader reader)
        {
            List<string> stringList = new List<string>();

            int maxIndex = reader.Index + this.RecordDataLength;
            while (reader.Index < maxIndex)
            {
                StringBuilder sb = reader.EnsureStringBuilder();
                int cb = reader.ReadByte();
                while (cb-- > 0)
                {
                    sb.Append(reader.ReadChar());
                }
                stringList.Add(sb.ToString());
            }

            this.Strings = stringList;
        }
Example #22
0
 /// <summary>
 /// Reads DNS wire format data to this response.
 /// </summary>
 /// <param name="reader">The reader that has already buffered response data.</param>
 protected override void Deserialize(ref DnsBufferReader reader)
 {
     base.Deserialize(ref reader);
     if (reader.IsDone)
     {
         //
         // No answers!
         //
         return;
     }
     
     if (this.Header.AnswerCount > 0)
     {
         this.AnswerRecords.Deserialize(this.Header.AnswerCount, ref reader);
     }
     if (this.Header.NameServerAnswerCount > 0)
     {
         this.NameServerRecords.Deserialize(this.Header.NameServerAnswerCount, ref reader);
     }
     if (this.Header.AdditionalAnswerCount > 0)
     {
         this.AdditionalRecords.Deserialize(this.Header.AdditionalAnswerCount, ref reader);
     }
 }
Example #23
0
 /// <summary>
 /// Instantiates a new message object by deserializing from the given reader
 /// </summary>
 /// <param name="reader"></param>
 protected DnsMessage(ref DnsBufferReader reader)
 {
     this.Deserialize(ref reader);
 }
Example #24
0
 /// <summary>
 /// Deserialize this message
 /// </summary>
 /// <param name="reader"></param>
 protected virtual void Deserialize(ref DnsBufferReader reader)
 {
     m_header   = new DnsHeader(ref reader);
     m_question = new DnsQuestion(ref reader);
 }
Example #25
0
 internal void Deserialize(ref DnsResourceRecordHeader header, ref DnsBufferReader reader)
 {
     m_header = header;
     this.DeserializeRecordData(ref reader);
 }
Example #26
0
 /// <summary>
 /// Override to deserialize record specific information
 /// </summary>
 /// <param name="reader"></param>
 protected abstract void DeserializeRecordData(ref DnsBufferReader reader);
Example #27
0
 /// <summary>
 /// Override to deserialize record specific information
 /// </summary>
 /// <param name="reader"></param>
 protected abstract void DeserializeRecordData(ref DnsBufferReader reader);
Example #28
0
 /// <summary>
 /// Reads data into this RR from the DNS wire format data in <paramref name="reader"/>
 /// </summary>
 /// <param name="reader">Reader in which wire format data for this RR is already buffered.</param>
 protected override void DeserializeRecordData(ref DnsBufferReader reader)
 {
     this.Address = reader.ReadUint();
 }
Example #29
0
 /// <summary>
 /// Reads data into this RR from the DNS wire format data in <paramref name="reader"/>
 /// </summary>
 /// <param name="reader">Reader in which wire format data for this RR is already buffered.</param>
 protected override void DeserializeRecordData(ref DnsBufferReader reader)
 {
     this.Priority = reader.ReadUShort();
     this.Weight = reader.ReadUShort();
     this.Port = reader.ReadUShort();
     this.Target = reader.ReadDomainName();
 }
Example #30
0
 /// <summary>
 /// Instantiate a new instance with the provided <paramref name="reader"/>
 /// </summary>
 /// <param name="reader">The reader that has been initialized with the response buffer.</param>
 public DnsResponse(DnsBufferReader reader)
     : base(ref reader)
 {
 }
Example #31
0
 internal DnsQuestion(ref DnsBufferReader reader)
 {
     this.Deserialize(ref reader);
 }
Example #32
0
 internal void Deserialize(ref DnsResourceRecordHeader header, ref DnsBufferReader reader)
 {
     m_header = header;
     this.DeserializeRecordData(ref reader);
 }