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