Exemplo n.º 1
0
        DnsResourceRecord ParseDnsResourceRecord(byte[] buffer, int beginIndex, ref int parseCount)
        {
            DnsResourceRecord resourceRecord = new DnsResourceRecord();
            int  index    = beginIndex;
            byte nameFlag = buffer[index];

            if ((nameFlag & 0xc0) == 0xc0) // check if the domain name is a 16-bit pointer;
            {
                resourceRecord.domainName = new Byte[sizeof(Int16)];
                Array.Copy(buffer, index, resourceRecord.domainName, 0, sizeof(Int16));
                index += sizeof(Int16);
            }
            else // reach here means it is a domain name string
            {
                // get the length of the query name
                int nameLength = 0;
                int sectionLen;
                index = beginIndex;
                while ((sectionLen = buffer[index]) != 0)
                {
                    nameLength += sectionLen + 1;
                    index      += sectionLen + 1;
                }
                nameLength += 1; // add the count of the last '0'
                index++;
                resourceRecord.domainName = new byte[nameLength];
                Array.Copy(buffer, index, resourceRecord.domainName, 0, nameLength);
            }
            resourceRecord.resType = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, index));
            index += sizeof(Int16);
            resourceRecord.resClass = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, index));
            index += sizeof(Int16);
            resourceRecord.ttl = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, index));
            index += sizeof(Int32);
            resourceRecord.resLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, index));
            index += sizeof(Int16);
            resourceRecord.resContent = new Byte[resourceRecord.resLength];
            Array.Copy(buffer, index, resourceRecord.resContent, 0, resourceRecord.resLength);
            index     += resourceRecord.resLength;
            parseCount = index - beginIndex;
            return(resourceRecord);
        }