Example #1
0
        public void DNSPtrTest()
        {
            String NAME = "64.233.169.104";
              DNSPacket.TYPES TYPE = DNSPacket.TYPES.PTR;
              DNSPacket.CLASSES CLASS = DNSPacket.CLASSES.IN;
              Question qp = new Question(NAME, TYPE, CLASS);

              MemBlock ptrm = MemBlock.Reference(new byte[] {0x03, 0x31, 0x30, 0x34,
            0x03, 0x31, 0x36, 0x39, 0x03, 0x32, 0x33, 0x33, 0x02, 0x36, 0x34, 0x07,
            0x69, 0x6e, 0x2d, 0x61, 0x64, 0x64, 0x72, 0x04, 0x61, 0x72, 0x70, 0x61,
            0x00, 0x00, 0x0c, 0x00, 0x01});
              Question qm = new Question(ptrm, 0);

              Assert.AreEqual(qp.Packet, ptrm, "Packet");
              Assert.AreEqual(qm.QNAME, NAME, "NAME");
              Assert.AreEqual(qm.QTYPE, TYPE, "TYPE");
              Assert.AreEqual(qm.QCLASS, CLASS, "CLASS");
        }
Example #2
0
        public void DNSATest()
        {
            String NAME = "www.cnn.com";
              DNSPacket.TYPES TYPE = DNSPacket.TYPES.A;
              DNSPacket.CLASSES CLASS = DNSPacket.CLASSES.IN;
              Question qp = new Question(NAME, TYPE, CLASS);

              MemBlock namem = MemBlock.Reference(new byte[] {0x03, 0x77, 0x77, 0x77,
            0x03, 0x63, 0x6e, 0x6e, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00,
            0x01});
              Question qm = new Question(namem, 0);

              Assert.AreEqual(qp.Packet, namem, "Packet");
              Assert.AreEqual(qm.QNAME, NAME, "NAME");
              Assert.AreEqual(qm.QTYPE, TYPE, "TYPE");
              Assert.AreEqual(qm.QCLASS, CLASS, "CLASS");
        }
Example #3
0
        /**
        <summary>Parses a MemBlock as a DNSPacket.</summary>
        <param name="Packet">The payload containing hte DNS Packet in byte format.
        </param>
        */
        public DNSPacket(MemBlock Packet)
        {
            ID = (short) ((Packet[0] << 8) + Packet[1]);
              QUERY = (bool) (((Packet[2] & 0x80) >> 7) == 0);
              OPCODE = (byte) ((Packet[2] & 0x78) >> 3);

              if((Packet[2] & 0x4) == 0x4) {
            AA = true;
              }
              else {
            AA = false;
              }

              if((Packet[2] & 0x1) == 0x1) {
            RD = true;
              }
              else {
            RD = false;
              }

              if((Packet[3] & 0x80) == 0x80) {
            RA = true;
              }
              else {
            RA = false;
              }

              int qdcount = (Packet[4] << 8) + Packet[5];
              int ancount = (Packet[6] << 8) + Packet[7];
              int nscount = (Packet[8] << 8) + Packet[9];
              int arcount = (Packet[10] << 8) + Packet[11];
              int idx = 12;

              Questions = new Question[qdcount];
              for(int i = 0; i < qdcount; i++) {
            Questions[i] = new Question(Packet, idx);
            idx += Questions[i].Packet.Length;
              }

              Answers = new Response[ancount];
              for(int i = 0; i < ancount; i++) {
            Answers[i] = new Response(Packet, idx);
            idx += Answers[i].Packet.Length;
              }

              Authority = new Response[nscount];
              for(int i = 0; i < nscount; i++) {
            Authority[i] = new Response(Packet, idx);
            idx += Authority[i].Packet.Length;
              }

              Additional = new Response[arcount];
              for(int i = 0; i < arcount; i++) {
            Additional[i] = new Response(Packet, idx);
            idx += Additional[i].Packet.Length;
              }

              _icpacket = _packet = Packet;
        }
Example #4
0
        public void TestPtrRPacketWithoutCompression()
        {
            int id = 55885;
              short ID = (short) id;
              bool QUERY = false;
              byte OPCODE = 0;
              bool AA = false;

              String QNAME = "64.233.169.104";
              DNSPacket.TYPES QTYPE = DNSPacket.TYPES.PTR;
              DNSPacket.CLASSES QCLASS = DNSPacket.CLASSES.IN;
              Question qp = new Question(QNAME, QTYPE, QCLASS);

              String NAME = "64.233.169.104";
              DNSPacket.TYPES TYPE = DNSPacket.TYPES.PTR;
              DNSPacket.CLASSES CLASS = DNSPacket.CLASSES.IN;
              int TTL = 30;
              String RDATA = "yo-in-f104.google.com";
              Response rp = new Response(NAME, TYPE, CLASS, TTL, RDATA);

              DNSPacket dp = new DNSPacket(ID, QUERY, OPCODE, AA, false, false,
                                   new Question[] {qp}, new Response[] {rp},
                                   null, null);

              MemBlock ptrm = MemBlock.Reference(new byte[] {0xda, 0x4d, 0x80, 0x00,
            0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0x31, 0x30, 0x34,
            0x03, 0x31, 0x36, 0x39, 0x03, 0x32, 0x33, 0x33, 0x02, 0x36, 0x34, 0x07,
            0x69, 0x6e, 0x2d, 0x61, 0x64, 0x64, 0x72, 0x04, 0x61, 0x72, 0x70, 0x61,
            0x00, 0x00, 0x0c, 0x00, 0x01, 0x03, 0x31, 0x30, 0x34, 0x03, 0x31, 0x36,
            0x39, 0x03, 0x32, 0x33, 0x33, 0x02, 0x36, 0x34, 0x07, 0x69, 0x6e, 0x2d,
            0x61, 0x64, 0x64, 0x72, 0x04, 0x61, 0x72, 0x70, 0x61, 0x00, 0x00, 0x0c,
            0x00, 0x01, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x17, 0x0a, 0x79, 0x6f, 0x2d,
            0x69, 0x6e, 0x2d, 0x66, 0x31, 0x30, 0x34, 0x06, 0x67, 0x6f, 0x6f, 0x67,
            0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00});

            DNSPacket dm = new DNSPacket(ptrm);
            Assert.AreEqual(dm.ID, ID, "ID");
            Assert.AreEqual(dm.QUERY, QUERY, "QUERY");
            Assert.AreEqual(dm.OPCODE, OPCODE, "OPCODE");
            Assert.AreEqual(dm.AA, AA, "AA");
            Assert.AreEqual(dm.Questions.Length, 1, "Questions");
            Assert.AreEqual(dm.Answers.Length, 1, "Answers");
            Assert.AreEqual(dm.Packet, ptrm, "MemBlock");

            Response rm = dm.Answers[0];
            Assert.AreEqual(rm.NAME, NAME, "NAME");
            Assert.AreEqual(rm.TYPE, TYPE, "TYPE");
            Assert.AreEqual(rm.CLASS, CLASS, "CLASS");
            Assert.AreEqual(rm.TTL, TTL, "TTL");
            Assert.AreEqual(rm.RDATA, RDATA, "RDATA");

            Question qm = dm.Questions[0];
            Assert.AreEqual(qm.QNAME, NAME, "QNAME");
            Assert.AreEqual(qm.QTYPE, TYPE, "QTYPE");
            Assert.AreEqual(qm.QCLASS, CLASS, "QCLASS");

            Assert.AreEqual(dp.Packet, ptrm, "DNS Packet");
        }
Example #5
0
        /**
        <summary>Creates a DNS packet from the parameters provided.</summary>
        <param name="ID">A unique ID for the packet, responses should be the same
        as the query</param>
        <param name="QUERY">True if a query, false if a response</param>
        <param name="OPCODE">0 = Query, which is the only supported parsing method
        </param>
        <param name="AA">Authoritative Answer, true if there is a resolution for
        the lookup.</param>
        <param name="Questions">A list of Questions.</param>
        <param name="Answers">A list of Answers.</param>
        */
        public DNSPacket(short ID, bool QUERY, byte OPCODE, bool AA, bool RA,
                     bool RD, Question[] Questions, Response[] Answers,
                     Response[] Authority, Response[] Additional)
        {
            byte[] header = new byte[12];

              this.ID = ID;
              header[0] = (byte) ((ID >> 8) & 0xFF);
              header[1] = (byte) (ID & 0xFF);

              this.QUERY = QUERY;
              if(!QUERY) {
            header[2] |= 0x80;
              }

              this.OPCODE = OPCODE;
              header[2] |= (byte) (OPCODE << 3);

              this.AA = AA;
              if(AA) {
            header[2] |= 0x4;
              }
              this.RD = RD;
              if(RD) {
            header[2] |= 0x1;
              }
              this.RA = RA;
              if(RA) {
            header[3] |= 0x80;
              }

              if(Questions != null) {
            this.Questions = Questions;
            header[4] = (byte) ((Questions.Length >> 8) & 0xFF);
            header[5] = (byte) (Questions.Length  & 0xFF);
              }
              else {
            this.Questions = new Question[0];
            header[4] = 0;
            header[5] = 0;
              }

              if(Answers != null) {
            this.Answers = Answers;
            header[6] = (byte) ((Answers.Length >> 8) & 0xFF);
            header[7] = (byte) (Answers.Length  & 0xFF);
              }
              else {
            this.Answers = new Response[0];
            header[6] = 0;
            header[7] = 0;
              }

              if(Authority != null) {
            this.Authority = Authority;
            header[8] = (byte) ((Authority.Length >> 8) & 0xFF);
            header[9] = (byte) (Authority.Length  & 0xFF);
              }
              else {
            this.Authority = new Response[0];
            header[8] = 0;
            header[9] = 0;
              }

              if(Additional != null) {
            this.Additional = Additional;
            header[10] = (byte) ((Additional.Length >> 8) & 0xFF);
            header[11] = (byte) (Additional.Length  & 0xFF);
              }
              else {
            this.Additional = new Response[0];
            header[10] = 0;
            header[11] = 0;
              }

              _icpacket = MemBlock.Reference(header);

              for(int i = 0; i < this.Questions.Length; i++) {
            _icpacket = new CopyList(_icpacket, Questions[i].ICPacket);
              }
              for(int i = 0; i < this.Answers.Length; i++) {
            _icpacket = new CopyList(_icpacket, Answers[i].ICPacket);
              }
              for(int i = 0; i < this.Authority.Length; i++) {
            _icpacket = new CopyList(_icpacket, Authority[i].ICPacket);
              }
              for(int i = 0; i < this.Additional.Length; i++) {
            _icpacket = new CopyList(_icpacket, Additional[i].ICPacket);
              }
        }