public void TestSerializeCompleteMXRecord() { Tuple <MemoryStream, DNSOutputStream> out_info = DNSOutput(); var record = new DNSRecord( new Domain("example.com"), AddressClass.INTERNET, 42, new MXResource(1000, new Domain("mail.example.com"))); record.Serialize(out_info.Item2); var record_bytes = out_info.Item1.ToArray(); var expected = new byte[] { 7, // Length of example 101, 120, 97, 109, 112, 108, 101, 3, // Length of com 99, 111, 109, 0, // MX record has code 15 0, 15, // INTERNET has class 1 0, 1, // Big-endian representation of 42 0, 0, 0, 42, // Record is 6 bytes long 0, 9, // Big-endian 1,000 3, 232, 4, // Length of mail 109, 97, 105, 108, // Pointer to byte 0, "example.com" 192, 0 }; Assert.That(record_bytes, Is.EqualTo(expected)); }
public void TestSerializeCompleteNSRecord() { Tuple <MemoryStream, DNSOutputStream> out_info = DNSOutput(); var record = new DNSRecord( new Domain("example.com"), AddressClass.INTERNET, 42, new NSResource(new Domain("dns.example.com"))); record.Serialize(out_info.Item2); var record_bytes = out_info.Item1.ToArray(); var expected = new byte[] { 7, // Length of example 101, 120, 97, 109, 112, 108, 101, 3, // Length of com 99, 111, 109, 0, // NS record has code 2 0, 2, // INTERNET has class 1 0, 1, // Big-endian representation of 42 0, 0, 0, 42, // Record is 6 bytes long 0, 6, // The record itself - dns + a pointer to example.com 3, // Length of dns 100, 110, 115, // Pointer to example.com at byte 0 192, 0 }; Assert.That(record_bytes, Is.EqualTo(expected)); }
public void TestSerializeCompleteCNAMERecord() { Tuple <MemoryStream, DNSOutputStream> out_info = DNSOutput(); var record = new DNSRecord( new Domain("example.com"), AddressClass.INTERNET, 42, new CNAMEResource(new Domain("www.example.com"))); record.Serialize(out_info.Item2); var record_bytes = out_info.Item1.ToArray(); var expected = new byte[] { 7, // Length of example 101, 120, 97, 109, 112, 108, 101, 3, // Length of com 99, 111, 109, 0, // CNAME record has code 5 0, 5, // INTERNET has class 1 0, 1, // Big-endian representation of 42 0, 0, 0, 42, // Record is 6 bytes long 0, 6, 3, // Length of www 119, 119, 119, // Reference to byte 0, 'example.com' 192, 0, }; Assert.That(record_bytes, Is.EqualTo(expected)); }
public void TestSerializeCompleteAResource() { Tuple <MemoryStream, DNSOutputStream> out_info = DNSOutput(); var record = new DNSRecord( new Domain("example.com"), AddressClass.INTERNET, 42, new AResource(IPv4Address.Parse("192.168.0.1"))); record.Serialize(out_info.Item2); var record_bytes = out_info.Item1.ToArray(); var expected = new byte[] { 7, // Length of example 101, 120, 97, 109, 112, 108, 101, 3, // Length of com 99, 111, 109, 0, // A record has code 1 0, 1, // INTERNET has class 1 0, 1, // Big-endian representation of 42 0, 0, 0, 42, // Record is 4 bytes long 0, 4, // The record itself 192, 168, 0, 1 }; Assert.That(record_bytes, Is.EqualTo(expected)); }
public static DNSPacket Unserialize(DNSInputStream stream) { var id = stream.ReadUInt16(); var is_query_bit = new Field(1); var query_type_flag = new Field(4); var is_authority_bit = new Field(1); var is_truncated_bit = new Field(1); var recursive_request_bit = new Field(1); new FieldGroup() .Add(is_query_bit) .Add(query_type_flag) .Add(is_authority_bit) .Add(is_truncated_bit) .Add(recursive_request_bit) .Unpack(stream.ReadByte()); var is_query = is_query_bit.Value == 0; var query_type = (QueryType)query_type_flag.Value; var is_authority = is_authority_bit.Value == 1; var is_truncated = is_truncated_bit.Value == 1; var recursive_request = recursive_request_bit.Value == 1; var recursion_availble_bit = new Field(1); var zeroes = new Field(3); var response_type_flag = new Field(4); new FieldGroup() .Add(recursion_availble_bit) .Add(zeroes) .Add(response_type_flag) .Unpack(stream.ReadByte()); var recursive_response = recursion_availble_bit.Value == 1; var response_type = (ResponseType)response_type_flag.Value; var question_count = stream.ReadUInt16(); var answer_count = stream.ReadUInt16(); var authority_count = stream.ReadUInt16(); var additional_count = stream.ReadUInt16(); var questions = new DNSQuestion[question_count]; for (int i = 0; i < question_count; i++) { questions[i] = DNSQuestion.Unserialize(stream); if (questions[i] == null) { throw new InvalidDataException("null Question " + i); } } var answers = new DNSRecord[answer_count]; for (int i = 0; i < answer_count; i++) { answers[i] = DNSRecord.Unserialize(stream); if (answers[i] == null) { throw new InvalidDataException("null Answer " + i); } } var authority = new DNSRecord[authority_count]; for (int i = 0; i < authority_count; i++) { authority[i] = DNSRecord.Unserialize(stream); if (authority[i] == null) { throw new InvalidDataException("null Authority " + i); } } var additional = new DNSRecord[additional_count]; for (int i = 0; i < additional_count; i++) { additional[i] = DNSRecord.Unserialize(stream); if (additional[i] == null) { throw new InvalidDataException("null Additional " + i); } } return(new DNSPacket(id, is_query, query_type.Normalize(), is_authority, is_truncated, recursive_request, recursive_response, response_type.Normalize(), questions, answers, authority, additional)); }
public void TestSerializeCompletePTRRecord() { Tuple <MemoryStream, DNSOutputStream> out_info = DNSOutput(); var record = new DNSRecord( new Domain("1.0.168.192.in-addr.arpa"), AddressClass.INTERNET, 42, new PTRResource(new Domain("www.example.com"))); record.Serialize(out_info.Item2); var record_bytes = out_info.Item1.ToArray(); var expected = new byte[] { 1, // Length of 1 49, 1, // Length of 0 48, 3, // Length of 168 49, 54, 56, 3, // Length of 192 49, 57, 50, 7, // Length of in-addr 105, 110, 45, 97, 100, 100, 114, 4, // Length of arpa 97, 114, 112, 97, 0, // PTR record has code 12 0, 12, // INTERNET has class 1 0, 1, // Big-endian representation of 42 0, 0, 0, 42, // Record is 17 bytes long 0, 17, // The record itself - www.example.com 3, // Length of www 119, 119, 119, 7, // Length of example 101, 120, 97, 109, 112, 108, 101, 3, // Length of com 99, 111, 109, 0 }; Assert.That(record_bytes, Is.EqualTo(expected)); }
public void TestSerializeCompleteSOAResource() { UInt32 one_hour = 60 * 60; UInt32 one_day = one_hour * 24; UInt32 one_week = one_day * 7; Tuple <MemoryStream, DNSOutputStream> out_info = DNSOutput(); var record = new DNSRecord( new Domain("example.com"), AddressClass.INTERNET, 42, new SOAResource( new Domain("dns.example.com"), new Domain("hostmaster.example.com"), 42, one_week, one_hour, one_week, one_day)); record.Serialize(out_info.Item2); var record_bytes = out_info.Item1.ToArray(); var expected = new byte[] { 7, // Length of example 101, 120, 97, 109, 112, 108, 101, 3, // Length of com 99, 111, 109, 0, // SOA record has code 6 0, 6, // INTERNET has class 1 0, 1, // Big-endian representation of 42 0, 0, 0, 42, // Record is 35 bytes long 0, 39, 3, // Length of dns 100, 110, 115, // Reference to byte 0, 'example.com' 192, 0, 10, // Length of hostmaster 104, 111, 115, 116, 109, 97, 115, 116, 101, 114, // Reference to byte 0, 'example.com' 192, 0, // Big-endian 42 0, 0, 0, 42, // Big-endian 604,800 0, 9, 58, 128, // Big-endian 3,600 0, 0, 14, 16, // Big-endian 604,800 0, 9, 58, 128, // Big-endian 86,400 0, 1, 81, 128 }; Assert.That(record_bytes, Is.EqualTo(expected)); }
public void TestSerializeCompleteAAAAResource() { Tuple <MemoryStream, DNSOutputStream> out_info = DNSOutput(); var record = new DNSRecord( new Domain("example.com"), AddressClass.INTERNET, 42, new AAAAResource(IPv6Address.Parse("2001:0DB8:AC10:FE01:0000:0000:0000:0000"))); record.Serialize(out_info.Item2); var record_bytes = out_info.Item1.ToArray(); var expected = new byte[] { 7, // Length of example 101, 120, 97, 109, 112, 108, 101, 3, // Length of com 99, 111, 109, 0, // AAAA record has code 28 0, 28, // INTERNET has class 1 0, 1, // Big-endian representation of 42 0, 0, 0, 42, // Record is 4 bytes long 0, 16, // The record itself 0x20, 0x01, 0x0D, 0xB8, 0xAC, 0x10, 0xFE, 0x01, 0, 0, 0, 0, 0, 0, 0, 0 }; Assert.That(record_bytes, Is.EqualTo(expected)); }
public void TestUnserializeDNSPacket() { var stream = DNSInput(new byte[] { // Big-endian 42 0, 42, // This is the query/resonse bit, the query type, the authority // bit, the truncation bit, and the recursion desired bit 4, // This is the recursion available bit, the zero segment, // and the return code 128, // 1 question 0, 1, // 1 answer 0, 1, // 0 authorities 0, 0, // 0 additional 0, 0, // The question - A record for example.com 7, // Length of example 101, 120, 97, 109, 112, 108, 101, 3, // Length of com 99, 111, 109, 0, // A record has code 1 0, 1, // INTERNET has class 1 0, 1, // The answer - the A record for example.com // Pointer to byte 96 - "example.com" 192, 12, // A record has code 1 0, 1, // INTERNET has class 1 0, 1, // Big-endian representation of 42 0, 0, 0, 42, // Record is 4 bytes long 0, 4, // The record itself 192, 168, 0, 1 }); var packet = DNSPacket.Unserialize(stream); var question = new DNSQuestion( new Domain("example.com"), ResourceRecordType.HOST_ADDRESS, AddressClass.INTERNET); var answer = new DNSRecord( new Domain("example.com"), AddressClass.INTERNET, 42, new AResource(IPv4Address.Parse("192.168.0.1"))); var expected = new DNSPacket( 42, true, QueryType.STANDARD_QUERY, true, false, false, true, ResponseType.NO_ERROR, new DNSQuestion[] { question }, new DNSRecord[] { answer }, new DNSRecord[0], new DNSRecord[0]); Assert.That(packet, Is.EqualTo(expected)); }