示例#1
0
 public static ANameRData Parse(byte[] bytes, int offset, int size)
 {
     ANameRData aname = new ANameRData();
     uint addressBytes = BitConverter.ToUInt32(bytes, offset);
     aname.Address = new IPAddress(addressBytes);
     return aname;
 }
示例#2
0
        public static ANameRData Parse(byte[] bytes, int offset, int size)
        {
            ANameRData aname        = new ANameRData();
            uint       addressBytes = BitConverter.ToUInt32(bytes, offset);

            aname.Address = new IPAddress(addressBytes);
            return(aname);
        }
示例#3
0
        public int LoadFrom(byte[] bytes, int offset, ushort count)
        {
            int currentOffset = offset;

            for (int index = 0; index < count; index++)
            {
                // TODO: move this code into the Resource object

                Resource resource = new Resource();
                //// extract the domain, question type, question class and Ttl

                resource.Name = DnsProtocol.ReadString(bytes, ref currentOffset);

                resource.Type  = (ResourceType)(BitConverter.ToUInt16(bytes, currentOffset).SwapEndian());
                currentOffset += sizeof(ushort);

                resource.Class = (ResourceClass)(BitConverter.ToUInt16(bytes, currentOffset).SwapEndian());
                currentOffset += sizeof(ushort);

                resource.TTL   = BitConverter.ToUInt32(bytes, currentOffset).SwapEndian();
                currentOffset += sizeof(uint);

                resource.DataLength = BitConverter.ToUInt16(bytes, currentOffset).SwapEndian();
                currentOffset      += sizeof(ushort);

                if (resource.Class == ResourceClass.IN && resource.Type == ResourceType.A)
                {
                    resource.RData = ANameRData.Parse(bytes, currentOffset, resource.DataLength);
                }
                else if (resource.Type == ResourceType.CNAME)
                {
                    resource.RData = CNameRData.Parse(bytes, currentOffset, resource.DataLength);
                }

                // move past resource data record
                currentOffset = currentOffset + resource.DataLength;

                this.Add(resource);
            }

            int bytesRead = currentOffset - offset;

            return(bytesRead);
        }
        public void TransitiveQueryTest2()
        {
            DnsMessage message = new DnsMessage();
            message.QueryIdentifier = 0xFEED;
            message.QR = false;
            message.Opcode = (byte) OpCode.QUERY;
            message.AA = false;
            message.TC = false;
            message.RD = true;
            message.RA = false;
            message.Zero = false;
            message.AuthenticatingData = false;
            message.CheckingDisabled = false;
            message.RCode = 0x0000;
            message.QuestionCount = 1;
            message.AnswerCount = 2;
            message.NameServerCount = 0;
            message.AdditionalCount = 0;
            message.Questions = new QuestionList();
            message.Questions.Add(new Question {Name = "www.msn.com", Class = ResourceClass.IN, Type = ResourceType.A});
            message.Answers.Add(new Resource {Name = "8.8.8.8", Class = ResourceClass.IN, Type = ResourceType.NS, TTL = 468, DataLength = 0, RData = null});
            RData data = new ANameRData {Address = IPAddress.Parse("8.8.8.9")};
            message.Answers.Add(new Resource {Name = "8.8.8.9", Class = ResourceClass.IN, Type = ResourceType.NS, TTL = 468, RData = data, DataLength = (ushort) data.Length});

            DnsMessage outMessage;
            using (MemoryStream stream = new MemoryStream())
            {
                message.WriteToStream(stream);
                Assert.IsTrue(DnsMessage.TryParse(stream.GetBuffer(), out outMessage));
            }

            Assert.AreEqual(0xFEED, outMessage.QueryIdentifier);
            Assert.IsFalse(outMessage.QR);
            Assert.AreEqual((byte) OpCode.QUERY, outMessage.Opcode);
            Assert.IsFalse(outMessage.AA);
            Assert.IsFalse(outMessage.TC);
            Assert.IsTrue(outMessage.RD);
            Assert.IsFalse(outMessage.RA);
            Assert.IsFalse(outMessage.Zero);
            Assert.IsFalse(outMessage.AuthenticatingData);
            Assert.IsFalse(outMessage.CheckingDisabled);
            Assert.AreEqual(0x0000, outMessage.RCode);
            Assert.AreEqual(0x0001, outMessage.QuestionCount);
            Assert.AreEqual(0x0002, outMessage.AnswerCount);
            Assert.AreEqual(0x0000, outMessage.NameServerCount);
            Assert.AreEqual(0x0000, outMessage.AdditionalCount);

            // Question Checks
            Assert.AreEqual(outMessage.QuestionCount, outMessage.Questions.Count());

            // Q1
            Assert.AreEqual("www.msn.com", outMessage.Questions[0].Name);
            Assert.AreEqual(ResourceType.A, outMessage.Questions[0].Type);
            Assert.AreEqual(ResourceClass.IN, outMessage.Questions[0].Class);

            Assert.AreEqual(outMessage.AnswerCount, outMessage.Answers.Count());
            Assert.AreEqual(outMessage.AnswerCount, outMessage.Answers.Count());
            Assert.AreEqual("8.8.8.8", outMessage.Answers[0].Name);
            Assert.AreEqual("8.8.8.9", outMessage.Answers[1].Name);
        }