public static RecordType DeserializeRecordType(byte[] bytes, int start)
        {
            int val = DnsQuestionBinarySerializer.Read2BytesAsInt(bytes, start);

            val--;
            return((RecordType)val);
        }
示例#2
0
 public DnsMessageBinarySerializer(ResourceRecordBinarySerializer rrSer, DnsQuestionBinarySerializer qSerializer)
 {
     this.rrSerializer = rrSer ?? throw new ArgumentNullException(nameof(rrSer));
     this.qSerializer  = qSerializer ?? throw new ArgumentNullException(nameof(qSerializer));
 }
        public ResourceRecord FromBytes(byte[] bytes, int start, out int totalBytesRead)
        {
            var name = this.dnsSerializer.ParseQuestionName(bytes, start, out var bytesRead);

            totalBytesRead = bytesRead;

            var recordType = DeserializeRecordType(bytes, start + totalBytesRead);

            totalBytesRead += 2;

            // count the record class bytes.
            totalBytesRead += 2;

            var timeToLive = Read4BytesAsInt(bytes, start + totalBytesRead);

            totalBytesRead += 4;

            var length = DnsQuestionBinarySerializer.Read2BytesAsInt(bytes, start + totalBytesRead);

            totalBytesRead += 2;

            ResourceRecord result = null;

            switch (recordType)
            {
            case RecordType.A:
                result = new ARecord()
                {
                    Name       = name,
                    TimeToLive = timeToLive,
                    Address    = new System.Net.IPAddress(new ReadOnlySpan <byte>(bytes, start + totalBytesRead, 4)),
                };
                break;

            case RecordType.CNAME:
                result = new CNameRecord()
                {
                    Name       = name,
                    TimeToLive = timeToLive,
                    CName      = this.dnsSerializer.ParseQuestionName(bytes, start + totalBytesRead, out var cnameBytesRead),
                };
                break;

            case RecordType.NS:
                result = new NSRecord()
                {
                    Name       = name,
                    TimeToLive = timeToLive,
                    DName      = this.dnsSerializer.ParseQuestionName(bytes, start + totalBytesRead, out var dnameBytesRead),
                };
                break;

            case RecordType.MX:
                result = this.DeserializeMxRr(name, timeToLive, bytes, start + totalBytesRead, out var rdataSize);
                break;

            case RecordType.PTR:
                result = this.DeserializePtrRr(name, timeToLive, bytes, start + totalBytesRead, out var ptrSize);
                break;

            case RecordType.TXT:
                result = this.DeserializeTxtRr(name, timeToLive, bytes, start + totalBytesRead, length, out var txtSize);
                break;

            case RecordType.SOA:
                result = this.DeserializeSoaRr(name, timeToLive, bytes, start + totalBytesRead, out var soaSize);
                break;

            default:
                Console.WriteLine($"Deserialization of recordType {recordType} is not yet implemented.");
                result = null;
                break;
            }

            totalBytesRead += length;
            return(result);
        }
 public ResourceRecordBinarySerializer(DnsQuestionBinarySerializer dnsSerializer)
 {
     this.dnsSerializer = dnsSerializer;
 }