Debian Codegraph @

src/test/print_dnstap.c

raw
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 if (dnstap_has_identity(*d)) {
67 printf("identity: %s\n", printable_string(dnstap_identity(*d), dnstap_identity_length(*d)));
68 }
69 if (dnstap_has_version(*d)) {
70 printf("version: %s\n", printable_string(dnstap_version(*d), dnstap_version_length(*d)));
71 }
72 if (dnstap_has_extra(*d)) {
73 printf("extra: %s\n", printable_string(dnstap_extra(*d), dnstap_extra_length(*d)));
74 }
75
76 if (dnstap_type(*d) == DNSTAP_TYPE_MESSAGE && dnstap_has_message(*d)) {
77 printf("message:\n type: %s\n", DNSTAP_MESSAGE_TYPE_STRING[dnstap_message_type(*d)]);
78
79 if (dnstap_message_has_query_time_sec(*d) && dnstap_message_has_query_time_nsec(*d)) {
80 printf(" query_time: %" PRIu64 ".%" PRIu32 "\n", dnstap_message_query_time_sec(*d), dnstap_message_query_time_nsec(*d));
81 }
82 if (dnstap_message_has_response_time_sec(*d) && dnstap_message_has_response_time_nsec(*d)) {
83 printf(" response_time: %" PRIu64 ".%" PRIu32 "\n", dnstap_message_response_time_sec(*d), dnstap_message_response_time_nsec(*d));
84 }
85 if (dnstap_message_has_socket_family(*d)) {
86 printf(" socket_family: %s\n", DNSTAP_SOCKET_FAMILY_STRING[dnstap_message_socket_family(*d)]);
87 }
88 if (dnstap_message_has_socket_protocol(*d)) {
89 printf(" socket_protocol: %s\n", DNSTAP_SOCKET_PROTOCOL_STRING[dnstap_message_socket_protocol(*d)]);
90 }
91 if (dnstap_message_has_query_address(*d)) {
92 printf(" query_address: %s\n", printable_ip_address(dnstap_message_query_address(*d), dnstap_message_query_address_length(*d)));
93 }
94 if (dnstap_message_has_query_port(*d)) {
95 printf(" query_port: %u\n", dnstap_message_query_port(*d));
96 }
97 if (dnstap_message_has_response_address(*d)) {
98 printf(" response_address: %s\n", printable_ip_address(dnstap_message_response_address(*d), dnstap_message_response_address_length(*d)));
99 }
100 if (dnstap_message_has_response_port(*d)) {
101 printf(" response_port: %u\n", dnstap_message_response_port(*d));
102 }
103 if (dnstap_message_has_query_zone(*d)) {
104 printf(" query_zone: %s\n", printable_string(dnstap_message_query_zone(*d), dnstap_message_query_zone_length(*d)));
105 }
106 if (dnstap_message_has_query_message(*d)) {
107 printf(" query_message_length: %zu\n", dnstap_message_query_message_length(*d));
108 printf(" query_message: %s\n", printable_string(dnstap_message_query_message(*d), dnstap_message_query_message_length(*d)));
109 }
110 if (dnstap_message_has_response_message(*d)) {
111 printf(" response_message_length: %zu\n", dnstap_message_response_message_length(*d));
112 printf(" response_message: %s\n", printable_string(dnstap_message_response_message(*d), dnstap_message_response_message_length(*d)));
113 }
114
115 if (dnstap_message_has_policy(*d)) {
116 printf(" policy:\n");
117
118 if (dnstap_message_policy_has_type(*d)) {
119 printf(" type: %s\n", dnstap_message_policy_type(*d));
120 }
121 if (dnstap_message_policy_has_rule(*d)) {
122 printf(" rule: %s\n", printable_string(dnstap_message_policy_rule(*d), dnstap_message_policy_rule_length(*d)));
123 }
124 if (dnstap_message_policy_has_action(*d)) {
125 printf(" action: %s\n", DNSTAP_POLICY_ACTION_STRING[dnstap_message_policy_action(*d)]);
126 }
127 if (dnstap_message_policy_has_match(*d)) {
128 printf(" match: %s\n", DNSTAP_POLICY_MATCH_STRING[dnstap_message_policy_match(*d)]);
129 }
130 if (dnstap_message_policy_has_value(*d)) {
131 printf(" value: %s\n", printable_string(dnstap_message_policy_value(*d), dnstap_message_policy_value_length(*d)));
132 }
133 }
134 }
135
136 printf("----\n");
137 }