diff options
Diffstat (limited to 'src/service/dns/gnunet-dns-monitor.c')
-rw-r--r-- | src/service/dns/gnunet-dns-monitor.c | 394 |
1 files changed, 394 insertions, 0 deletions
diff --git a/src/service/dns/gnunet-dns-monitor.c b/src/service/dns/gnunet-dns-monitor.c new file mode 100644 index 000000000..c1ef17255 --- /dev/null +++ b/src/service/dns/gnunet-dns-monitor.c | |||
@@ -0,0 +1,394 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file src/dns/gnunet-dns-monitor.c | ||
23 | * @brief Tool to monitor DNS queries | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_dns_service.h" | ||
30 | |||
31 | /** | ||
32 | * Handle to transport service. | ||
33 | */ | ||
34 | static struct GNUNET_DNS_Handle *handle; | ||
35 | |||
36 | /** | ||
37 | * Option -i. | ||
38 | */ | ||
39 | static int inbound_only; | ||
40 | |||
41 | /** | ||
42 | * Option -o. | ||
43 | */ | ||
44 | static int outbound_only; | ||
45 | |||
46 | /** | ||
47 | * Global return value (0 success). | ||
48 | */ | ||
49 | static int ret; | ||
50 | |||
51 | /** | ||
52 | * Selected level of verbosity. | ||
53 | */ | ||
54 | static unsigned int verbosity; | ||
55 | |||
56 | |||
57 | /** | ||
58 | * Convert numeric DNS record type to a string. | ||
59 | * | ||
60 | * @param type type to convert | ||
61 | * @return type as string, only valid until the next call to this function | ||
62 | */ | ||
63 | static const char * | ||
64 | get_type (uint16_t type) | ||
65 | { | ||
66 | static char buf[6]; | ||
67 | |||
68 | switch (type) | ||
69 | { | ||
70 | case GNUNET_DNSPARSER_TYPE_A: return "A"; | ||
71 | |||
72 | case GNUNET_DNSPARSER_TYPE_NS: return "NS"; | ||
73 | |||
74 | case GNUNET_DNSPARSER_TYPE_CNAME: return "CNAME"; | ||
75 | |||
76 | case GNUNET_DNSPARSER_TYPE_SOA: return "SOA"; | ||
77 | |||
78 | case GNUNET_DNSPARSER_TYPE_PTR: return "PTR"; | ||
79 | |||
80 | case GNUNET_DNSPARSER_TYPE_MX: return "MX"; | ||
81 | |||
82 | case GNUNET_DNSPARSER_TYPE_TXT: return "TXT"; | ||
83 | |||
84 | case GNUNET_DNSPARSER_TYPE_AAAA: return "AAAA"; | ||
85 | |||
86 | case GNUNET_DNSPARSER_TYPE_SRV: return "SRV"; | ||
87 | } | ||
88 | GNUNET_snprintf (buf, sizeof(buf), "%u", (unsigned int) type); | ||
89 | return buf; | ||
90 | } | ||
91 | |||
92 | |||
93 | /** | ||
94 | * Convert numeric DNS record class to a string. | ||
95 | * | ||
96 | * @param class class to convert | ||
97 | * @return class as string, only valid until the next call to this function | ||
98 | */ | ||
99 | static const char * | ||
100 | get_class (uint16_t class) | ||
101 | { | ||
102 | static char buf[6]; | ||
103 | |||
104 | switch (class) | ||
105 | { | ||
106 | case GNUNET_TUN_DNS_CLASS_INTERNET: return "IN"; | ||
107 | |||
108 | case GNUNET_TUN_DNS_CLASS_CHAOS: return "CHAOS"; | ||
109 | |||
110 | case GNUNET_TUN_DNS_CLASS_HESIOD: return "HESIOD"; | ||
111 | } | ||
112 | GNUNET_snprintf (buf, sizeof(buf), "%u", (unsigned int) class); | ||
113 | return buf; | ||
114 | } | ||
115 | |||
116 | |||
117 | /** | ||
118 | * Output the given DNS query to stdout. | ||
119 | * | ||
120 | * @param query query to display. | ||
121 | */ | ||
122 | static void | ||
123 | display_query (const struct GNUNET_DNSPARSER_Query *query) | ||
124 | { | ||
125 | fprintf (stdout, | ||
126 | "\t\t%s %s: %s\n", | ||
127 | get_class (query->dns_traffic_class), | ||
128 | get_type (query->type), | ||
129 | query->name); | ||
130 | } | ||
131 | |||
132 | |||
133 | /** | ||
134 | * Output the given DNS record to stdout. | ||
135 | * | ||
136 | * @param record record to display. | ||
137 | */ | ||
138 | static void | ||
139 | display_record (const struct GNUNET_DNSPARSER_Record *record) | ||
140 | { | ||
141 | const char *format; | ||
142 | char buf[INET6_ADDRSTRLEN]; | ||
143 | char *tmp; | ||
144 | |||
145 | tmp = NULL; | ||
146 | switch (record->type) | ||
147 | { | ||
148 | case GNUNET_DNSPARSER_TYPE_A: | ||
149 | if (record->data.raw.data_len != sizeof(struct in_addr)) | ||
150 | format = "<invalid>"; | ||
151 | else | ||
152 | format = inet_ntop (AF_INET, record->data.raw.data, buf, sizeof(buf)); | ||
153 | break; | ||
154 | |||
155 | case GNUNET_DNSPARSER_TYPE_AAAA: | ||
156 | if (record->data.raw.data_len != sizeof(struct in6_addr)) | ||
157 | format = "<invalid>"; | ||
158 | else | ||
159 | format = inet_ntop (AF_INET6, record->data.raw.data, buf, sizeof(buf)); | ||
160 | break; | ||
161 | |||
162 | case GNUNET_DNSPARSER_TYPE_NS: | ||
163 | case GNUNET_DNSPARSER_TYPE_CNAME: | ||
164 | case GNUNET_DNSPARSER_TYPE_PTR: | ||
165 | format = record->data.hostname; | ||
166 | break; | ||
167 | |||
168 | case GNUNET_DNSPARSER_TYPE_SOA: | ||
169 | if (NULL == record->data.soa) | ||
170 | format = "<invalid>"; | ||
171 | else | ||
172 | { | ||
173 | GNUNET_asprintf (&tmp, | ||
174 | "origin: %s, mail: %s, serial = %u, refresh = %u s, retry = %u s, expire = %u s, minimum = %u s", | ||
175 | record->data.soa->mname, | ||
176 | record->data.soa->rname, | ||
177 | (unsigned int) record->data.soa->serial, | ||
178 | (unsigned int) record->data.soa->refresh, | ||
179 | (unsigned int) record->data.soa->retry, | ||
180 | (unsigned int) record->data.soa->expire, | ||
181 | (unsigned int) record->data.soa->minimum_ttl); | ||
182 | format = tmp; | ||
183 | } | ||
184 | break; | ||
185 | |||
186 | case GNUNET_DNSPARSER_TYPE_MX: | ||
187 | if (record->data.mx == NULL) | ||
188 | format = "<invalid>"; | ||
189 | else | ||
190 | { | ||
191 | GNUNET_asprintf (&tmp, | ||
192 | "%u: %s", | ||
193 | record->data.mx->preference, | ||
194 | record->data.mx->mxhost); | ||
195 | format = tmp; | ||
196 | } | ||
197 | break; | ||
198 | |||
199 | case GNUNET_DNSPARSER_TYPE_SRV: | ||
200 | if (NULL == record->data.srv) | ||
201 | format = "<invalid>"; | ||
202 | else | ||
203 | { | ||
204 | GNUNET_asprintf (&tmp, | ||
205 | "priority %u, weight = %u, port = %u, target = %s", | ||
206 | (unsigned int) record->data.srv->priority, | ||
207 | (unsigned int) record->data.srv->weight, | ||
208 | (unsigned int) record->data.srv->port, | ||
209 | record->data.srv->target); | ||
210 | format = tmp; | ||
211 | } | ||
212 | break; | ||
213 | |||
214 | case GNUNET_DNSPARSER_TYPE_TXT: | ||
215 | GNUNET_asprintf (&tmp, | ||
216 | "%.*s", | ||
217 | (unsigned int) record->data.raw.data_len, | ||
218 | (char*) record->data.raw.data); | ||
219 | format = tmp; | ||
220 | break; | ||
221 | |||
222 | default: | ||
223 | format = "<payload>"; | ||
224 | break; | ||
225 | } | ||
226 | fprintf (stdout, | ||
227 | "\t\t%s %s: %s = %s (%u s)\n", | ||
228 | get_class (record->dns_traffic_class), | ||
229 | get_type (record->type), | ||
230 | record->name, | ||
231 | format, | ||
232 | (unsigned int) (GNUNET_TIME_absolute_get_remaining ( | ||
233 | record->expiration_time).rel_value_us / 1000LL | ||
234 | / 1000LL)); | ||
235 | GNUNET_free (tmp); | ||
236 | } | ||
237 | |||
238 | |||
239 | /** | ||
240 | * Signature of a function that is called whenever the DNS service | ||
241 | * encounters a DNS request and needs to do something with it. The | ||
242 | * function has then the chance to generate or modify the response by | ||
243 | * calling one of the three "GNUNET_DNS_request_*" continuations. | ||
244 | * | ||
245 | * When a request is intercepted, this function is called first to | ||
246 | * give the client a chance to do the complete address resolution; | ||
247 | * "rdata" will be NULL for this first call for a DNS request, unless | ||
248 | * some other client has already filled in a response. | ||
249 | * | ||
250 | * If multiple clients exist, all of them are called before the global | ||
251 | * DNS. The global DNS is only called if all of the clients' | ||
252 | * functions call GNUNET_DNS_request_forward. Functions that call | ||
253 | * GNUNET_DNS_request_forward will be called again before a final | ||
254 | * response is returned to the application. If any of the clients' | ||
255 | * functions call GNUNET_DNS_request_drop, the response is dropped. | ||
256 | * | ||
257 | * @param cls closure | ||
258 | * @param rh request handle to user for reply | ||
259 | * @param request_length number of bytes in request | ||
260 | * @param request udp payload of the DNS request | ||
261 | */ | ||
262 | static void | ||
263 | display_request (void *cls, | ||
264 | struct GNUNET_DNS_RequestHandle *rh, | ||
265 | size_t request_length, | ||
266 | const char *request) | ||
267 | { | ||
268 | static const char *return_codes[] = { | ||
269 | "No error", "Format error", "Server failure", "Name error", | ||
270 | "Not implemented", "Refused", "YXDomain", "YXRRset", | ||
271 | "NXRRset", "NOT AUTH", "NOT ZONE", "<invalid>", | ||
272 | "<invalid>", "<invalid>", "<invalid>", "<invalid>" | ||
273 | }; | ||
274 | static const char *op_codes[] = { | ||
275 | "Query", "Inverse query", "Status", "<invalid>", | ||
276 | "<invalid>", "<invalid>", "<invalid>", "<invalid>", | ||
277 | "<invalid>", "<invalid>", "<invalid>", "<invalid>", | ||
278 | "<invalid>", "<invalid>", "<invalid>", "<invalid>" | ||
279 | }; | ||
280 | struct GNUNET_DNSPARSER_Packet *p; | ||
281 | unsigned int i; | ||
282 | |||
283 | p = GNUNET_DNSPARSER_parse (request, request_length); | ||
284 | if (NULL == p) | ||
285 | { | ||
286 | fprintf (stderr, "Received malformed DNS packet!\n"); | ||
287 | // FIXME: drop instead? | ||
288 | GNUNET_DNS_request_forward (rh); | ||
289 | return; | ||
290 | } | ||
291 | fprintf (stdout, | ||
292 | "%s with ID: %5u Flags: %s%s%s%s%s%s, Return Code: %s, Opcode: %s\n", | ||
293 | p->flags.query_or_response ? "Response" : "Query", | ||
294 | p->id, | ||
295 | p->flags.recursion_desired ? "RD " : "", | ||
296 | p->flags.message_truncated ? "MT " : "", | ||
297 | p->flags.authoritative_answer ? "AA " : "", | ||
298 | p->flags.checking_disabled ? "CD " : "", | ||
299 | p->flags.authenticated_data ? "AD " : "", | ||
300 | p->flags.recursion_available ? "RA " : "", | ||
301 | return_codes[p->flags.return_code & 15], | ||
302 | op_codes[p->flags.opcode & 15]); | ||
303 | if (p->num_queries > 0) | ||
304 | fprintf (stdout, | ||
305 | "\tQueries:\n"); | ||
306 | for (i = 0; i < p->num_queries; i++) | ||
307 | display_query (&p->queries[i]); | ||
308 | |||
309 | if (p->num_answers > 0) | ||
310 | fprintf (stdout, | ||
311 | "\tAnswers:\n"); | ||
312 | for (i = 0; i < p->num_answers; i++) | ||
313 | display_record (&p->answers[i]); | ||
314 | fprintf (stdout, "\n"); | ||
315 | GNUNET_DNSPARSER_free_packet (p); | ||
316 | GNUNET_DNS_request_forward (rh); | ||
317 | } | ||
318 | |||
319 | |||
320 | /** | ||
321 | * Shutdown. | ||
322 | */ | ||
323 | static void | ||
324 | do_disconnect (void *cls) | ||
325 | { | ||
326 | if (NULL != handle) | ||
327 | { | ||
328 | GNUNET_DNS_disconnect (handle); | ||
329 | handle = NULL; | ||
330 | } | ||
331 | } | ||
332 | |||
333 | |||
334 | /** | ||
335 | * Main function that will be run by the scheduler. | ||
336 | * | ||
337 | * @param cls closure | ||
338 | * @param args remaining command-line arguments | ||
339 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
340 | * @param cfg configuration | ||
341 | */ | ||
342 | static void | ||
343 | run (void *cls, char *const *args, const char *cfgfile, | ||
344 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
345 | { | ||
346 | enum GNUNET_DNS_Flags flags; | ||
347 | |||
348 | flags = GNUNET_DNS_FLAG_REQUEST_MONITOR | GNUNET_DNS_FLAG_RESPONSE_MONITOR; | ||
349 | if (inbound_only | outbound_only) | ||
350 | flags = 0; | ||
351 | if (inbound_only) | ||
352 | flags |= GNUNET_DNS_FLAG_REQUEST_MONITOR; | ||
353 | if (outbound_only) | ||
354 | flags |= GNUNET_DNS_FLAG_RESPONSE_MONITOR; | ||
355 | handle = | ||
356 | GNUNET_DNS_connect (cfg, | ||
357 | flags, | ||
358 | &display_request, | ||
359 | NULL); | ||
360 | GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL); | ||
361 | } | ||
362 | |||
363 | |||
364 | int | ||
365 | main (int argc, char *const *argv) | ||
366 | { | ||
367 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
368 | GNUNET_GETOPT_option_flag ('i', | ||
369 | "inbound-only", | ||
370 | gettext_noop ("only monitor DNS queries"), | ||
371 | &inbound_only), | ||
372 | |||
373 | GNUNET_GETOPT_option_flag ('o', | ||
374 | "outbound-only", | ||
375 | gettext_noop ("only monitor DNS queries"), | ||
376 | &outbound_only), | ||
377 | |||
378 | GNUNET_GETOPT_option_verbose (&verbosity), | ||
379 | GNUNET_GETOPT_OPTION_END | ||
380 | }; | ||
381 | |||
382 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | ||
383 | return 2; | ||
384 | ret = (GNUNET_OK == | ||
385 | GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-monitor", | ||
386 | gettext_noop | ||
387 | ("Monitor DNS queries."), options, | ||
388 | &run, NULL)) ? ret : 1; | ||
389 | GNUNET_free_nz ((void *) argv); | ||
390 | return ret; | ||
391 | } | ||
392 | |||
393 | |||
394 | /* end of gnunet-dns-monitor.c */ | ||