PtrStringToMemBlock() public static method

Takes in an IP Address string and returns the dns ptr formatted memblock containing d.c.b.a.in-addr.arpa.
public static PtrStringToMemBlock ( String ptr ) : MemBlock
ptr String An IP Address in the format a.b.c.d.
return MemBlock
Exemplo n.º 1
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));
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public void TestPtr()
        {
            String   ptr  = "64.233.169.104";
            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 });

            Assert.AreEqual(ptr, DnsPacket.PtrMemBlockToString(ptrm),
                            "PtrMemBlockToString");
            Assert.AreEqual(ptrm, DnsPacket.PtrStringToMemBlock(ptr),
                            "PtrStringToMemBlock");
            Assert.AreEqual(ptr, DnsPacket.PtrMemBlockToString(
                                DnsPacket.PtrStringToMemBlock(ptr)),
                            "Ptr String dual");
            Assert.AreEqual(ptrm, DnsPacket.PtrStringToMemBlock(
                                DnsPacket.PtrMemBlockToString(ptrm)),
                            "Ptr MemBlock dual");
        }