dns_parse_packet returns a dns_reply_t structure which has this definition:
typedef struct
{
…
dns_header_t *header;
…
dns_resource_record_t **answer;
…
} dns_reply_t;
The header contains an ancount field that tells you how many entries there are in the answer array. So, to expand on my example above:
let packet: [UInt8] = [
0x47, 0x49, 0x81, 0x80, 0x00, 0x01, 0x00, 0x04,
0x00, 0x00, 0x00, 0x01, 0x03, 0x77, 0x77, 0x77,
0x05, 0x61, 0x70, 0x70, 0x6C, 0x65, 0x03, 0x63,
0x6F, 0x6D, 0x00, 0x00, 0x01, 0x00, 0x01, 0xC0,
0x0C, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x06,
0x30, 0x00, 0x1B, 0x03, 0x77, 0x77, 0x77, 0x05,
0x61, 0x70, 0x70, 0x6C, 0x65, 0x03, 0x63, 0x6F,
0x6D, 0x07, 0x65, 0x64, 0x67, 0x65, 0x6B, 0x65,
0x79, 0x03, 0x6E, 0x65, 0x74, 0x00, 0xC0, 0x2B,
0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x38, 0x58,
0x00, 0x32, 0x03, 0x77, 0x77, 0x77, 0x05, 0x61,
0x70, 0x70, 0x6C, 0x65, 0x03, 0x63, 0x6F, 0x6D,
0x07, 0x65, 0x64, 0x67, 0x65, 0x6B, 0x65, 0x79,
0x03, 0x6E, 0x65, 0x74, 0x0B, 0x67, 0x6C, 0x6F,
0x62, 0x61, 0x6C, 0x72, 0x65, 0x64, 0x69, 0x72,
0x06, 0x61, 0x6B, 0x61, 0x64, 0x6E, 0x73, 0x03,
0x6E, 0x65, 0x74, 0x00, 0xC0, 0x52, 0x00, 0x05,
0x00, 0x01, 0x00, 0x00, 0x0D, 0xBA, 0x00, 0x1B,
0x05, 0x65, 0x36, 0x38, 0x35, 0x38, 0x04, 0x64,
0x73, 0x63, 0x78, 0x0A, 0x61, 0x6B, 0x61, 0x6D,
0x61, 0x69, 0x65, 0x64, 0x67, 0x65, 0x03, 0x6E,
0x65, 0x74, 0x00, 0xC0, 0x90, 0x00, 0x01, 0x00,
0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x04, 0x02,
0x14, 0x5E, 0xB1, 0x00, 0x00, 0x29, 0x04, 0xD0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]
… packet parsing code as above …
let answers = UnsafeBufferPointer(start: reply.pointee.answer, count: Int(reply.pointee.header.pointee.ancount))
for a in answers {
print(a!.pointee)
}
which prints:
dns_resource_record_t(name: …, dnstype: 5, dnsclass: 1, ttl: 1584, data: …)
dns_resource_record_t(name: …, dnstype: 5, dnsclass: 1, ttl: 14424, data: …)
dns_resource_record_t(name: …, dnstype: 5, dnsclass: 1, ttl: 3514, data: …)
dns_resource_record_t(name: …, dnstype: 1, dnsclass: 1, ttl: 5, data: …)
Share and Enjoy
Quinn “The Eskimo!” @ DTS @ Apple