Пример #1
0
        public void MDnsATest()
        {
            String Name = "david-laptop.local";

            DnsPacket.Types Type       = DnsPacket.Types.A;
            bool            CacheFlush = true;

            DnsPacket.Classes Class = DnsPacket.Classes.IN;
            int      Ttl            = 120;
            String   RData          = "10.227.56.136";
            Response rp             = new Response(Name, Type, Class, CacheFlush, Ttl, RData);

            MemBlock am = MemBlock.Reference(new byte[] { 0x0c, 0x64, 0x61, 0x76,
                                                          0x69, 0x64, 0x2d, 0x6c, 0x61, 0x70, 0x74, 0x6f, 0x70, 0x05, 0x6c, 0x6f,
                                                          0x63, 0x61, 0x6c, 0x00, 0x00, 0x01, 0x80, 0x01, 0x00, 0x00, 0x00, 0x78,
                                                          0x00, 0x04, 0x0a, 0xe3, 0x38, 0x88 });

            Response rm = new Response(am, 0);

            Assert.AreEqual(rp.Packet, am, "Packet");
            Assert.AreEqual(rm.Name, Name, "Name");
            Assert.AreEqual(rm.Type, Type, "Type");
            Assert.AreEqual(rm.Class, Class, "Class");
            Assert.AreEqual(rm.CacheFlush, CacheFlush, "CacheFlush");
            Assert.AreEqual(rm.Ttl, Ttl, "Ttl");
            Assert.AreEqual(rm.RData, RData, "RData");
        }
Пример #2
0
        public void DnsPtrTest()
        {
            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);

            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, 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 });
            Response rm = new Response(ptrm, 0);

            Assert.AreEqual(rp.Packet, ptrm, "Packet");
            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");
        }
Пример #3
0
        /**
         * <summary>Constructor when creating a Dns Query</summary>
         * <param name="QName">the name of resource you are looking up, IP Address
         * when QType = Ptr otherwise hostname</param>
         * <param name="QType"> the type of look up to perform</param>
         * <param name="QClass">should always be IN</param>
         */
        public Question(String QName, DnsPacket.Types QType, DnsPacket.Classes QClass)
        {
            this.QName  = QName;
            this.QType  = QType;
            this.QClass = QClass;

            if (QType == DnsPacket.Types.A || QType == DnsPacket.Types.AAAA)
            {
                QNameBlob = DnsPacket.HostnameStringToMemBlock(QName);
            }
            else if (QType == DnsPacket.Types.Ptr)
            {
                QNameBlob = DnsPacket.PtrStringToMemBlock(QName);
            }
            else
            {
                throw new Exception("Invalid QType: " + QType + "!");
            }

            // 2 for QType + 2 for QClass
            byte[] data = new byte[4];
            int    idx  = 0;

            data[idx++] = (byte)((((int)QType) >> 8) & 0xFF);
            data[idx++] = (byte)(((int)QType) & 0xFF);
            data[idx++] = (byte)((((int)QClass) >> 8) & 0xFF);
            data[idx++] = (byte)(((int)QClass) & 0xFF);
            _icpacket   = new CopyList(QNameBlob, MemBlock.Reference(data));
        }
Пример #4
0
        /**
         * <summary>Creates a response from the parameter fields with RData being
         * a memory chunk.  This is for MDns which supports caching</summary>
         * <param name="Name">The name resolved.</param>
         * <param name="Type">The query type.</param>
         * <param name="Class">The network type.</param>
         * <param name="CacheFlush">Flush the dns cache in the client.</param>
         * <param name="Ttl">How long to hold the result in the local dns cache.</param>
         * <param name="RData">RData in String format.</param>
         */
        public Response(string name, DnsPacket.Types type, DnsPacket.Classes class_type,
                        bool cache_flush, int ttl, String rdata)
        {
            Name       = name;
            Class      = class_type;
            Ttl        = ttl;
            Type       = type;
            CacheFlush = cache_flush;
            RData      = rdata;

            if (Type == DnsPacket.Types.A || Type == DnsPacket.Types.AAAA)
            {
                NameBlob  = DnsPacket.HostnameStringToMemBlock(Name);
                RDataBlob = DnsPacket.IPStringToMemBlock(RData);
            }
            else if (Type == DnsPacket.Types.Ptr)
            {
                if (DnsPacket.StringIsIP(Name))
                {
                    NameBlob = DnsPacket.PtrStringToMemBlock(Name);
                }
                else
                {
                    NameBlob = DnsPacket.HostnameStringToMemBlock(Name);
                }
                RDataBlob = DnsPacket.HostnameStringToMemBlock(RData);
            }
            else
            {
                throw new Exception("Invalid Query Type: " + Type + "!");
            }

            RdLength = (short)RDataBlob.Length;
            // 2 for Type + 2 for Class + 4 for Ttl + 2 for RdLength
            byte[] data = new byte[10];
            int    idx  = 0;

            data[idx++] = (byte)((((int)Type) >> 8) & 0xFF);
            data[idx++] = (byte)(((int)Type) & 0xFF);

            byte cf = 0x80;

            if (!CacheFlush)
            {
                cf = 0x00;
            }

            data[idx++] = (byte)(((((int)Class) >> 8) & 0x7F) | cf);
            data[idx++] = (byte)(((int)Class) & 0xFF);
            data[idx++] = (byte)((Ttl >> 24) & 0xFF);
            data[idx++] = (byte)((Ttl >> 16) & 0xFF);
            data[idx++] = (byte)((Ttl >> 8) & 0xFF);
            data[idx++] = (byte)(Ttl & 0xFF);
            data[idx++] = (byte)((RdLength >> 8) & 0xFF);
            data[idx]   = (byte)(RdLength & 0xFF);

            _icpacket = new CopyList(NameBlob, MemBlock.Reference(data), RDataBlob);
        }
Пример #5
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");
        }
Пример #6
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");
        }
Пример #7
0
        /**
         * <summary>Creates a response given the entire packet.</summary>
         * <remarks>The entire packet must be given, because some name servers take
         * advantage of pointers to reduce their size.</remarks>
         * <param name="Data">The entire Dns packet.</param>
         * <param name="Start">The starting position of the Response.</param>
         */
        public Response(MemBlock Data, int Start)
        {
            int idx = 0;

            NameBlob = DnsPacket.RetrieveBlob(Data, Start, out idx);

            int type = (Data[idx++] << 8) + Data[idx++];

            Type = (DnsPacket.Types)type;

            CacheFlush = ((Data[idx] & 0x80) == 0x80) ? true : false;
            int rclass = ((Data[idx++] << 8) & 0x7F) + Data[idx++];

            Class = (DnsPacket.Classes)rclass;

            Ttl  = (Data[idx++] << 24);
            Ttl |= (Data[idx++] << 16);
            Ttl |= (Data[idx++] << 8);
            Ttl |= (Data[idx++]);

            RdLength  = (short)((Data[idx++] << 8) + Data[idx++]);
            RDataBlob = Data.Slice(idx, RdLength);

            if (Type == DnsPacket.Types.Ptr)
            {
                try {
                    Name = DnsPacket.PtrMemBlockToString(NameBlob);
                }
                catch {
                    Name = DnsPacket.HostnameMemBlockToString(NameBlob);
                }
                int End = 0;
                RDataBlob = DnsPacket.RetrieveBlob(Data, idx, out End);
                RData     = DnsPacket.HostnameMemBlockToString(RDataBlob);
            }
            else if (Type == DnsPacket.Types.A)
            {
                Name  = DnsPacket.HostnameMemBlockToString(NameBlob);
                RData = DnsPacket.IPMemBlockToString(RDataBlob);
            }
            _icpacket = _packet = Data.Slice(Start, idx + RdLength - Start);
        }
Пример #8
0
        public void DnsATest()
        {
            String Name = "www.cnn.com";

            DnsPacket.Types   Type  = DnsPacket.Types.A;
            DnsPacket.Classes Class = DnsPacket.Classes.IN;
            int      Ttl            = 2;
            String   RData          = "64.236.91.24";
            Response rp             = new Response(Name, Type, Class, Ttl, RData);

            MemBlock am = MemBlock.Reference(new byte[] { 0x03, 0x77, 0x77, 0x77,
                                                          0x03, 0x63, 0x6e, 0x6e, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00,
                                                          0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x04, 0x40, 0xec, 0x5b, 0x18 });
            Response rm = new Response(am, 0);

            Assert.AreEqual(rp.Packet, am, "Packet");
            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");
        }
Пример #9
0
    /**
    <summary>Constructor when creating a Dns Query</summary>
    <param name="QName">the name of resource you are looking up, IP Address 
    when QType = Ptr otherwise hostname</param>
    <param name="QType"> the type of look up to perform</param>
    <param name="QClass">should always be IN</param>
    */
    public Question(String QName, DnsPacket.Types QType, DnsPacket.Classes QClass) {
      this.QName = QName;
      this.QType = QType;
      this.QClass = QClass;

      if(QType == DnsPacket.Types.A || QType == DnsPacket.Types.AAAA) {
        QNameBlob = DnsPacket.HostnameStringToMemBlock(QName);
      }
      else if(QType == DnsPacket.Types.Ptr) {
        QNameBlob = DnsPacket.PtrStringToMemBlock(QName);
      }
      else {
        throw new Exception("Invalid QType: " + QType + "!");
      }

        // 2 for QType + 2 for QClass
      byte[] data = new byte[4];
      int idx = 0;
      data[idx++] = (byte) ((((int) QType) >> 8) & 0xFF);
      data[idx++] = (byte) (((int) QType) & 0xFF);
      data[idx++] = (byte) ((((int) QClass) >> 8) & 0xFF);
      data[idx++] = (byte) (((int) QClass) & 0xFF);
      _icpacket = new CopyList(QNameBlob, MemBlock.Reference(data));
    }
Пример #10
0
        /**
         * <summary>Constructor when parsing a Dns Query</summary>
         * <param name="Data"> must pass in the entire packet from where the question
         * begins, after parsing, can check Data.Length to find where next
         * container begins.</param>
         */
        public Question(MemBlock Data, int Start)
        {
            int idx = 0;

            QNameBlob = DnsPacket.RetrieveBlob(Data, Start, out idx);
            int qtype = (Data[idx++] << 8) + Data[idx++];

            QType = (DnsPacket.Types)qtype;

            int qclass = (Data[idx++] << 8) + Data[idx];

            QClass = (DnsPacket.Classes)qclass;

            if (QType == DnsPacket.Types.A || QType == DnsPacket.Types.AAAA)
            {
                QName = DnsPacket.HostnameMemBlockToString(QNameBlob);
            }
            else if (QType == DnsPacket.Types.Ptr)
            {
                QName = DnsPacket.PtrMemBlockToString(QNameBlob);
            }

            _icpacket = _packet = Data.Slice(Start, idx + 1 - Start);
        }
Пример #11
0
    /**
    <summary>Constructor when parsing a Dns Query</summary>
    <param name="Data"> must pass in the entire packet from where the question
    begins, after parsing, can check Data.Length to find where next
    container begins.</param>
    */
    public Question(MemBlock Data, int Start) {
      int idx = 0;
      QNameBlob = DnsPacket.RetrieveBlob(Data, Start, out idx);
      int qtype = (Data[idx++] << 8) + Data[idx++];
      QType = (DnsPacket.Types) qtype;

      int qclass = (Data[idx++] << 8) + Data[idx];
      QClass = (DnsPacket.Classes) qclass;

      if(QType == DnsPacket.Types.A || QType == DnsPacket.Types.AAAA) {
        QName = DnsPacket.HostnameMemBlockToString(QNameBlob);
      }
      else if(QType == DnsPacket.Types.Ptr) {
        QName = DnsPacket.PtrMemBlockToString(QNameBlob);
      }

      _icpacket = _packet = Data.Slice(Start, idx + 1 - Start);
    }
Пример #12
0
 /**
  * <summary>Creates a response from the parameter fields with RData being
  * a memory chunk.  This is for regular dns which has no notion of caching.
  * </summary>
  * <param name="Name">The name resolved.</param>
  * <param name="Type">The query type.</param>
  * <param name="Class">The network type.</param>
  * <param name="CacheFlush">Flush the dns cache in the client.</param>
  * <param name="Ttl">How long to hold the result in the local dns cache.</param>
  * <param name="RData">RData in String format.</param>
  */
 public Response(String Name, DnsPacket.Types Type, DnsPacket.Classes Class,
                 int Ttl, String RData) : this(Name, Type, Class, false, Ttl, RData)
 {
 }
Пример #13
0
    /**
    <summary>Creates a response from the parameter fields with RData being
    a memory chunk.  This is for MDns which supports caching</summary>
    <param name="Name">The name resolved.</param>
    <param name="Type">The query type.</param>
    <param name="Class">The network type.</param>
    <param name="CacheFlush">Flush the dns cache in the client.</param>
    <param name="Ttl">How long to hold the result in the local dns cache.</param>
    <param name="RData">RData in String format.</param>
    */
    public Response(string name, DnsPacket.Types type, DnsPacket.Classes class_type,
                    bool cache_flush, int ttl, String rdata) {
      Name = name;
      Class = class_type;
      Ttl = ttl;
      Type = type;
      CacheFlush = cache_flush;
      RData = rdata;

      if(Type == DnsPacket.Types.A) {
        NameBlob = DnsPacket.HostnameStringToMemBlock(Name);
        RDataBlob = DnsPacket.IPStringToMemBlock(RData);
      }
      else if(Type == DnsPacket.Types.Ptr) {
        if(DnsPacket.StringIsIP(Name)) {
          NameBlob = DnsPacket.PtrStringToMemBlock(Name);
        }
        else {
          NameBlob = DnsPacket.HostnameStringToMemBlock(Name);
        }
        RDataBlob = DnsPacket.HostnameStringToMemBlock(RData);
      }
      else {
        throw new Exception("Invalid Query Type: " + Type + "!");
      }

      RdLength = (short) RDataBlob.Length;
      // 2 for Type + 2 for Class + 4 for Ttl + 2 for RdLength
      byte[] data = new byte[10];
      int idx = 0;
      data[idx++] = (byte) ((((int) Type) >> 8) & 0xFF);
      data[idx++] = (byte) (((int) Type) & 0xFF);

      byte cf = 0x80;
      if(!CacheFlush) {
        cf = 0x00;
      }

      data[idx++] = (byte) (((((int) Class) >> 8) & 0x7F) | cf);
      data[idx++] = (byte) (((int) Class) & 0xFF);
      data[idx++] = (byte) ((Ttl >> 24) & 0xFF);
      data[idx++] = (byte) ((Ttl >> 16) & 0xFF);
      data[idx++] = (byte) ((Ttl >> 8) & 0xFF);
      data[idx++] = (byte) (Ttl & 0xFF);
      data[idx++] = (byte) ((RdLength >> 8) & 0xFF);
      data[idx] = (byte) (RdLength & 0xFF);

      _icpacket = new CopyList(NameBlob, MemBlock.Reference(data), RDataBlob);
    }
Пример #14
0
    /**
    <summary>Creates a response given the entire packet.</summary>
    <remarks>The entire packet must be given, because some name servers take
    advantage of pointers to reduce their size.</remarks>
    <param name="Data">The entire Dns packet.</param>
    <param name="Start">The starting position of the Response.</param>
    */
    public Response(MemBlock Data, int Start) {
      int idx = 0;
      NameBlob = DnsPacket.RetrieveBlob(Data, Start, out idx);

      int type = (Data[idx++] << 8) + Data[idx++];
      Type = (DnsPacket.Types) type;

      CacheFlush = ((Data[idx] & 0x80) == 0x80) ? true : false;
      int rclass = ((Data[idx++] << 8) & 0x7F) + Data[idx++];
      Class = (DnsPacket.Classes) rclass;

      Ttl = (Data[idx++] << 24);
      Ttl |= (Data[idx++] << 16);
      Ttl |= (Data[idx++] << 8);
      Ttl |= (Data[idx++]);

      RdLength = (short) ((Data[idx++] << 8) + Data[idx++]);
      RDataBlob = Data.Slice(idx, RdLength);

      if(Type == DnsPacket.Types.Ptr) {
        try {
          Name = DnsPacket.PtrMemBlockToString(NameBlob);
        }
        catch {
          Name = DnsPacket.HostnameMemBlockToString(NameBlob);
        }
        int End = 0;
        RDataBlob = DnsPacket.RetrieveBlob(Data, idx, out End);
        RData = DnsPacket.HostnameMemBlockToString(RDataBlob);
      }
      else if(Type == DnsPacket.Types.A) {
        Name = DnsPacket.HostnameMemBlockToString(NameBlob);
        RData = DnsPacket.IPMemBlockToString(RDataBlob);
      }
      _icpacket = _packet = Data.Slice(Start, idx + RdLength - Start);
    }
Пример #15
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");
        }