示例#1
0
        ushort ReceiveSize()
        {
            this.Receive(m_sizeBuffer, 2);
            DnsBufferReader reader = m_sizeBuffer.CreateReader();

            return(reader.ReadUShort());
        }
示例#2
0
        public ushort ReceiveLength()
        {
            DnsBuffer lengthBuffer = new DnsBuffer(2);

            lengthBuffer.Count = m_socket.Receive(lengthBuffer.Buffer, 2, SocketFlags.None);
            DnsBufferReader reader = lengthBuffer.CreateReader();

            return(reader.ReadUShort());
        }
示例#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)
 {
     this.DomainName      = reader.ReadDomainName();
     this.ResponsibleName = reader.ReadDomainName();
     this.SerialNumber    = reader.ReadInt();
     this.Refresh         = reader.ReadInt();
     this.Retry           = reader.ReadInt();
     this.Expire          = reader.ReadInt();
     this.Minimum         = reader.ReadInt();
 }
示例#4
0
        /// <summary>
        /// Deserialize the raw ResourceRecord embedded in this DnsRecord
        /// </summary>
        public DnsResourceRecord Deserialize()
        {
            if (this.RecordData.IsNullOrEmpty())
            {
                throw new InvalidOperationException("Empty record data found.");
            }

            DnsBufferReader bufferReader = new DnsBufferReader(this.RecordData, 0, this.RecordData.Length);

            return(DnsResourceRecord.Deserialize(ref bufferReader));
        }
示例#5
0
        T Roundtrip <T>(T record)
            where T : DnsResourceRecord
        {
            DnsBuffer buffer = new DnsBuffer(1024);

            record.Serialize(buffer);

            DnsBufferReader reader = buffer.CreateReader();

            return((T)DnsResourceRecord.Deserialize(ref reader));
        }
示例#6
0
        /// <summary>
        /// loads and verifies the dnsrecords from the bin associated bin files, ensuring that the types
        /// match up
        /// </summary>
        /// <typeparam name="T">Type of record that is expected</typeparam>
        /// <param name="path">path to the bin file to be loaded</param>
        /// <returns>bytes from the bin file</returns>
        protected byte[] LoadAndVerifyDnsRecordFromBin <T>(string path)
        {
            byte[] bytes = null;

            //----------------------------------------------------------------------------------------------------
            //---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);
                DnsResourceRecord rec = DnsResourceRecord.Deserialize(ref rdr);
                Assert.Equal(rec.GetType(), typeof(T));
            }
            return(bytes);
        }
示例#7
0
        /// <summary>
        /// loads and verifies the dnsrecords from the bin associated bin files, ensuring that the types
        /// match up
        /// </summary>
        /// <typeparam name="T">Type of record that is expected</typeparam>
        /// <param name="path">path to the bin file to be loaded</param>
        /// <returns>bytes from the bin file</returns>
        protected T LoadAndVerifyDnsRecordFromBin <T>(string path) where T : DnsResourceRecord
        {
            T record;

            //----------------------------------------------------------------------------------------------------
            //---read the stream from the bytes
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                byte[]          bytes = new BinaryReader(fs).ReadBytes((int)new FileInfo(path).Length);
                DnsBufferReader rdr   = new DnsBufferReader(bytes, 0, bytes.Length);
                record = DnsResourceRecord.Deserialize(ref rdr) as T;
                if (record == null)
                {
                    throw new TypeLoadException("unexpected type encountered in file");
                }
            }
            return(record);
        }
示例#8
0
        internal void Deserialize(ref DnsBufferReader buffer)
        {
            this.UniqueID = buffer.ReadUShort();

            byte b = buffer.ReadByte();

            this.IsRequest             = ((b & 0x80) == 0);
            this.OpCode                = (DnsStandard.OpCode)(byte)((b >> 3) & 0x0F);
            this.IsAuthoritativeAnswer = ((b & 0x04) != 0);
            this.IsTruncated           = ((b & 0x02) != 0);
            this.IsRecursionDesired    = ((b & 0x01) != 0);

            b = buffer.ReadByte();
            this.IsRecursionAvailable = ((b & 0x80) != 0);
            this.ResponseCode         = (DnsStandard.ResponseCode)(byte)(b & 0x0F);

            this.QuestionCount         = buffer.ReadShort();
            this.AnswerCount           = buffer.ReadShort();
            this.NameServerAnswerCount = buffer.ReadShort();
            this.AdditionalAnswerCount = buffer.ReadShort();
        }
        /// <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.", "")));
            }
        }
示例#10
0
 /// <summary>
 /// Deserializes this raw record
 /// </summary>
 /// <param name="reader"></param>
 protected override void DeserializeRecordData(ref DnsBufferReader reader)
 {
     this.RecordBytes = reader.ReadBytes(this.RecordDataLength);
 }
示例#11
0
 /// <summary>
 /// Creates an instance from the DNS message from a DNS reader.
 /// </summary>
 /// <param name="reader">The DNS reader</param>
 protected override void DeserializeRecordData(ref DnsBufferReader reader)
 {
     m_name = reader.ReadDomainName();
 }
示例#12
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.NameServer = reader.ReadDomainName();
 }
示例#13
0
 /// <summary>
 /// Deserialize the dns request using the given reader
 /// </summary>
 /// <param name="reader"></param>
 protected override void Deserialize(ref DnsBufferReader reader)
 {
     base.Deserialize(ref reader);            
     this.Validate();            
 }   
示例#14
0
 /// <summary>
 /// Construct a new Dns request object by parsing the data supplied by the given reader
 /// </summary>
 public DnsRequest(DnsBufferReader reader)
     : base(ref reader)
 {
 }
示例#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)
 {
     this.Preference = reader.ReadShort();
     this.Exchange   = reader.ReadDomainName();
 }
示例#16
0
文件: NSRecord.cs 项目: DM-TOR/nhin-d
 /// <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.NameServer = reader.ReadDomainName();
 }
示例#17
0
        internal void Deserialize(ref DnsBufferReader buffer)
        {
            this.UniqueID = buffer.ReadUShort();

            byte b = buffer.ReadByte();

            this.IsRequest = ((b & 0x80) == 0);
            this.OpCode = (DnsStandard.OpCode)(byte)((b >> 3) & 0x0F);
            this.IsAuthoritativeAnswer = ((b & 0x04) != 0);
            this.IsTruncated = ((b & 0x02) != 0);
            this.IsRecursionDesired = ((b & 0x01) != 0);

            b = buffer.ReadByte();
            this.IsRecursionAvailable = ((b & 0x80) != 0);
            this.ResponseCode = (DnsStandard.ResponseCode) (byte)(b & 0x0F);

            this.QuestionCount = buffer.ReadShort();
            this.AnswerCount = buffer.ReadShort();
            this.NameServerAnswerCount = buffer.ReadShort();
            this.AdditionalAnswerCount = buffer.ReadShort();
        }
示例#18
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);
        }
示例#19
0
 /// <summary>
 /// Initializes a DNS header filled from the specified <paramref name="reader"/>
 /// </summary>
 /// <param name="reader">The reader containing raw data for the header.</param>
 public DnsHeader(ref DnsBufferReader reader)
 {
     this.Deserialize(ref reader);
 }
示例#20
0
 /// <summary>
 /// Deserializes this raw record 
 /// </summary>
 /// <param name="reader"></param>
 protected override void DeserializeRecordData(ref DnsBufferReader reader)
 {
     this.RecordBytes = reader.ReadBytes(this.RecordDataLength);
 }
示例#21
0
文件: MXRecord.cs 项目: DM-TOR/nhin-d
 /// <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.Preference = reader.ReadShort();
     this.Exchange = reader.ReadDomainName();
 }
示例#22
0
 /// <summary>
 /// Construct a new Dns request object by parsing the data supplied by the given reader
 /// </summary>
 public DnsRequest(DnsBufferReader reader)
     : base(ref reader)
 {
 }
示例#23
0
 /// <summary>
 /// Deserialize the dns request using the given reader
 /// </summary>
 /// <param name="reader"></param>
 protected override void Deserialize(ref DnsBufferReader reader)
 {
     base.Deserialize(ref reader);
     this.Validate();
 }
示例#24
0
 /// <summary>
 /// Initializes a DNS header filled from the specified <paramref name="reader"/>
 /// </summary>
 /// <param name="reader">The reader containing raw data for the header.</param>
 public DnsHeader(ref DnsBufferReader reader)
 {
     this.Deserialize(ref reader);
 }
示例#25
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.DomainName = reader.ReadDomainName();
     this.ResponsibleName = reader.ReadDomainName();
     this.SerialNumber = reader.ReadInt();
     this.Refresh = reader.ReadInt();
     this.Retry = reader.ReadInt();
     this.Expire = reader.ReadInt();
     this.Minimum = reader.ReadInt();
 }
示例#26
0
 /// <summary>
 /// Creates an instance from the DNS message from a DNS reader.
 /// </summary>
 /// <param name="reader">The DNS reader</param>
 protected override void DeserializeRecordData(ref DnsBufferReader reader)
 {
     m_name = reader.ReadDomainName();
 }