LumiSoft.Net.Dns.Client.Dns_Client.GetQName C# (CSharp) Method

GetQName() private method

private GetQName ( byte reply, int &offset, string &name ) : bool
reply byte
offset int
name string
return bool
        private bool GetQName(byte[] reply,ref int offset,ref string name)
        {
            try{
                // Do while not terminator
                while(reply[offset] != 0){

                    // Check if it's pointer(In pointer first two bits always 1)
                    bool isPointer = ((reply[offset] & 0xC0) == 0xC0);

                    // If pointer
                    if(isPointer){
                        // Pointer location number is 2 bytes long
                        // 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7  # byte 2 # 0 | 1 | 2 | | 3 | 4 | 5 | 6 | 7
                        // empty | < ---- pointer location number --------------------------------->
                        int pStart = ((reply[offset] & 0x3F) << 8) | (reply[++offset]);
                        offset++;
                        return GetQName(reply,ref pStart,ref name);
                    }
                    else{
                        // label length (length = 8Bit and first 2 bits always 0)
                        // 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
                        // empty | lablel length in bytes
                        int labelLength = (reply[offset] & 0x3F);
                        offset++;

                        // Copy label into name
                        name += Encoding.ASCII.GetString(reply,offset,labelLength);
                        offset += labelLength;
                    }

                    // If the next char isn't terminator,
                    // label continues - add dot between two labels
                    if (reply[offset] != 0){
                        name += ".";
                    }
                }

                // Move offset by terminator length
                offset++;

                return true;
            }
            catch{
                return false;
            }
        }

Usage Example

        /// <summary>
        /// Parses resource record from reply data.
        /// </summary>
        /// <param name="reply">DNS server reply data.</param>
        /// <param name="offset">Current offset in reply data.</param>
        /// <param name="rdLength">Resource record data length.</param>
        /// <param name="ttl">Time to live in seconds.</param>
        public static DNS_rr_CNAME Parse(byte[] reply, ref int offset, int rdLength, int ttl)
        {
            string name = "";

            if (Dns_Client.GetQName(reply, ref offset, ref name))
            {
                return(new DNS_rr_CNAME(name, ttl));
            }
            else
            {
                throw new ArgumentException("Invalid CNAME resource record data !");
            }
        }
All Usage Examples Of LumiSoft.Net.Dns.Client.Dns_Client::GetQName