| 1 |
#include <dnswire/dnstap.h> |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <inttypes.h> |
| 5 |
#include <arpa/inet.h> |
| 6 |
#include <ctype.h> |
| 7 |
#include <sys/types.h> |
| 8 |
#include <sys/socket.h> |
| 9 |
#include <netinet/in.h> |
| 10 |
|
| 11 |
static const char* printable_string(const uint8_t* data, size_t len) |
| 12 |
{ |
| 13 |
static char buf[512], hex; |
| 14 |
size_t r = 0, w = 0; |
| 15 |
|
| 16 |
while (r < len && w < sizeof(buf) - 1) { |
| 17 |
if (isprint(data[r])) { |
| 18 |
buf[w++] = data[r++]; |
| 19 |
} else { |
| 20 |
if (w + 4 >= sizeof(buf) - 1) { |
| 21 |
break; |
| 22 |
} |
| 23 |
|
| 24 |
buf[w++] = '\\'; |
| 25 |
buf[w++] = 'x'; |
| 26 |
hex = (data[r] & 0xf0) >> 4; |
| 27 |
if (hex > 9) { |
| 28 |
buf[w++] = 'a' + (hex - 10); |
| 29 |
} else { |
| 30 |
buf[w++] = '0' + hex; |
| 31 |
} |
| 32 |
hex = data[r++] & 0xf; |
| 33 |
if (hex > 9) { |
| 34 |
buf[w++] = 'a' + (hex - 10); |
| 35 |
} else { |
| 36 |
buf[w++] = '0' + hex; |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
if (w >= sizeof(buf)) { |
| 41 |
buf[sizeof(buf) - 1] = 0; |
| 42 |
} else { |
| 43 |
buf[w] = 0; |
| 44 |
} |
| 45 |
|
| 46 |
return buf; |
| 47 |
} |
| 48 |
|
| 49 |
static const char* printable_ip_address(const uint8_t* data, size_t len) |
| 50 |
{ |
| 51 |
static char buf[INET6_ADDRSTRLEN]; |
| 52 |
|
| 53 |
buf[0] = 0; |
| 54 |
if (len == 4) { |
| 55 |
inet_ntop(AF_INET, data, buf, sizeof(buf)); |
| 56 |
} else if (len == 16) { |
| 57 |
inet_ntop(AF_INET6, data, buf, sizeof(buf)); |
| 58 |
} |
| 59 |
|
| 60 |
return buf; |
| 61 |
} |
| 62 |
|
| 63 |
static void print_dnstap(const struct dnstap* d) |
| 64 |
{ |
| 65 |
printf("---- dnstap\n"); |
| 66 |
|
| 67 |
/* |
| 68 |
* Now we can print the available information in the message. |
| 69 |
*/ |
| 70 |
|
| 71 |
if (dnstap_has_identity(*d)) { |
| 72 |
printf("identity: %s\n", printable_string(dnstap_identity(*d), dnstap_identity_length(*d))); |
| 73 |
} |
| 74 |
if (dnstap_has_version(*d)) { |
| 75 |
printf("version: %s\n", printable_string(dnstap_version(*d), dnstap_version_length(*d))); |
| 76 |
} |
| 77 |
if (dnstap_has_extra(*d)) { |
| 78 |
printf("extra: %s\n", printable_string(dnstap_extra(*d), dnstap_extra_length(*d))); |
| 79 |
} |
| 80 |
|
| 81 |
if (dnstap_type(*d) == DNSTAP_TYPE_MESSAGE && dnstap_has_message(*d)) { |
| 82 |
printf("message:\n type: %s\n", DNSTAP_MESSAGE_TYPE_STRING[dnstap_message_type(*d)]); |
| 83 |
|
| 84 |
if (dnstap_message_has_query_time_sec(*d) && dnstap_message_has_query_time_nsec(*d)) { |
| 85 |
printf(" query_time: %" PRIu64 ".%" PRIu32 "\n", dnstap_message_query_time_sec(*d), dnstap_message_query_time_nsec(*d)); |
| 86 |
} |
| 87 |
if (dnstap_message_has_response_time_sec(*d) && dnstap_message_has_response_time_nsec(*d)) { |
| 88 |
printf(" response_time: %" PRIu64 ".%" PRIu32 "\n", dnstap_message_response_time_sec(*d), dnstap_message_response_time_nsec(*d)); |
| 89 |
} |
| 90 |
if (dnstap_message_has_socket_family(*d)) { |
| 91 |
printf(" socket_family: %s\n", DNSTAP_SOCKET_FAMILY_STRING[dnstap_message_socket_family(*d)]); |
| 92 |
} |
| 93 |
if (dnstap_message_has_socket_protocol(*d)) { |
| 94 |
printf(" socket_protocol: %s\n", DNSTAP_SOCKET_PROTOCOL_STRING[dnstap_message_socket_protocol(*d)]); |
| 95 |
} |
| 96 |
if (dnstap_message_has_query_address(*d)) { |
| 97 |
printf(" query_address: %s\n", printable_ip_address(dnstap_message_query_address(*d), dnstap_message_query_address_length(*d))); |
| 98 |
} |
| 99 |
if (dnstap_message_has_query_port(*d)) { |
| 100 |
printf(" query_port: %u\n", dnstap_message_query_port(*d)); |
| 101 |
} |
| 102 |
if (dnstap_message_has_response_address(*d)) { |
| 103 |
printf(" response_address: %s\n", printable_ip_address(dnstap_message_response_address(*d), dnstap_message_response_address_length(*d))); |
| 104 |
} |
| 105 |
if (dnstap_message_has_response_port(*d)) { |
| 106 |
printf(" response_port: %u\n", dnstap_message_response_port(*d)); |
| 107 |
} |
| 108 |
if (dnstap_message_has_query_zone(*d)) { |
| 109 |
printf(" query_zone: %s\n", printable_string(dnstap_message_query_zone(*d), dnstap_message_query_zone_length(*d))); |
| 110 |
} |
| 111 |
if (dnstap_message_has_query_message(*d)) { |
| 112 |
printf(" query_message_length: %lu\n", dnstap_message_query_message_length(*d)); |
| 113 |
printf(" query_message: %s\n", printable_string(dnstap_message_query_message(*d), dnstap_message_query_message_length(*d))); |
| 114 |
} |
| 115 |
if (dnstap_message_has_response_message(*d)) { |
| 116 |
printf(" response_message_length: %lu\n", dnstap_message_response_message_length(*d)); |
| 117 |
printf(" response_message: %s\n", printable_string(dnstap_message_response_message(*d), dnstap_message_response_message_length(*d))); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
printf("----\n"); |
| 122 |
} |