Ejemplo n.º 1
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.","")));
            }
        }
Ejemplo n.º 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();
            }
Ejemplo n.º 3
0
 /// <summary>
 /// Override to deserialize record specific information
 /// </summary>
 /// <param name="reader"></param>
 protected abstract void DeserializeRecordData(ref DnsBufferReader reader);
Ejemplo n.º 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;
        }
Ejemplo n.º 5
0
 internal void Deserialize(ref DnsResourceRecordHeader header, ref DnsBufferReader reader)
 {
     m_header = header;
     this.DeserializeRecordData(ref reader);
 }
Ejemplo n.º 6
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);
 }
Ejemplo n.º 7
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);
 }
Ejemplo n.º 8
0
 internal DnsQuestion(ref DnsBufferReader reader)
 {
     this.Deserialize(ref reader);
 }
Ejemplo n.º 9
0
 internal void Deserialize(ref DnsBufferReader reader)
 {
     this.Domain = reader.ReadDomainName();
     this.Type = (DnsStandard.RecordType) reader.ReadShort();
     this.Class = (DnsStandard.Class) reader.ReadShort();
 }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
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();
 }
Ejemplo n.º 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)
        {
            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;
        }
Ejemplo n.º 13
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();            
 }
Ejemplo n.º 14
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)
 {
 }
Ejemplo n.º 15
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);
     }
 }
Ejemplo n.º 16
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();
 }