HostnameStringToMemBlock() публичный статический Метод

Given a Name as a string converts it into bytes given the type of query.
public static HostnameStringToMemBlock ( String name ) : MemBlock
name String The name to convert (and resolve).
Результат MemBlock
Пример #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));
        }
Пример #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);
        }
Пример #3
0
        public void TestHostname()
        {
            String   hostname  = "yo-in-f104.google.com";
            MemBlock hostnamem = MemBlock.Reference(new byte[] { 0x0a, 0x79, 0x6f,
                                                                 0x2d, 0x69, 0x6e, 0x2d, 0x66, 0x31, 0x30, 0x34, 0x06, 0x67, 0x6f, 0x6f,
                                                                 0x67, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00 });

            Assert.AreEqual(hostname, DnsPacket.HostnameMemBlockToString(hostnamem),
                            "HostnameMemBlockToString");
            Assert.AreEqual(hostnamem, DnsPacket.HostnameStringToMemBlock(hostname),
                            "HostnameStringToMemBlock");
            Assert.AreEqual(hostname, DnsPacket.HostnameMemBlockToString(
                                DnsPacket.HostnameStringToMemBlock(hostname)),
                            "Hostname String dual");
            Assert.AreEqual(hostnamem, DnsPacket.HostnameStringToMemBlock(
                                DnsPacket.HostnameMemBlockToString(hostnamem)),
                            "Hostname MemBlock dual");
        }