2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU 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.
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.
17 * @file src/dns/gnunet-dns-monitor.c
18 * @brief Tool to monitor DNS queries
19 * @author Christian Grothoff
23 #include "gnunet_util_lib.h"
24 #include "gnunet_dns_service.h"
25 #include "gnunet_dnsparser_lib.h"
28 * Handle to transport service.
30 static struct GNUNET_DNS_Handle *handle;
35 static int inbound_only;
40 static int outbound_only;
43 * Global return value (0 success).
48 * Selected level of verbosity.
50 static unsigned int verbosity;
54 * Convert numeric DNS record type to a string.
56 * @param type type to convert
57 * @return type as string, only valid until the next call to this function
60 get_type (uint16_t type)
65 case GNUNET_DNSPARSER_TYPE_A: return "A";
66 case GNUNET_DNSPARSER_TYPE_NS: return "NS";
67 case GNUNET_DNSPARSER_TYPE_CNAME: return "CNAME";
68 case GNUNET_DNSPARSER_TYPE_SOA: return "SOA";
69 case GNUNET_DNSPARSER_TYPE_PTR: return "PTR";
70 case GNUNET_DNSPARSER_TYPE_MX: return "MX";
71 case GNUNET_DNSPARSER_TYPE_TXT: return "TXT";
72 case GNUNET_DNSPARSER_TYPE_AAAA: return "AAAA";
73 case GNUNET_DNSPARSER_TYPE_SRV: return "SRV";
75 GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) type);
81 * Convert numeric DNS record class to a string.
83 * @param class class to convert
84 * @return class as string, only valid until the next call to this function
87 get_class (uint16_t class)
92 case GNUNET_TUN_DNS_CLASS_INTERNET: return "IN";
93 case GNUNET_TUN_DNS_CLASS_CHAOS: return "CHAOS";
94 case GNUNET_TUN_DNS_CLASS_HESIOD: return "HESIOD";
96 GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) class);
102 * Output the given DNS query to stdout.
104 * @param query query to display.
107 display_query (const struct GNUNET_DNSPARSER_Query *query)
111 get_class (query->dns_traffic_class),
112 get_type (query->type),
118 * Output the given DNS record to stdout.
120 * @param record record to display.
123 display_record (const struct GNUNET_DNSPARSER_Record *record)
126 char buf[INET6_ADDRSTRLEN];
130 switch (record->type)
132 case GNUNET_DNSPARSER_TYPE_A:
133 if (record->data.raw.data_len != sizeof (struct in_addr))
134 format = "<invalid>";
136 format = inet_ntop (AF_INET, record->data.raw.data, buf, sizeof (buf));
138 case GNUNET_DNSPARSER_TYPE_AAAA:
139 if (record->data.raw.data_len != sizeof (struct in6_addr))
140 format = "<invalid>";
142 format = inet_ntop (AF_INET6, record->data.raw.data, buf, sizeof (buf));
144 case GNUNET_DNSPARSER_TYPE_NS:
145 case GNUNET_DNSPARSER_TYPE_CNAME:
146 case GNUNET_DNSPARSER_TYPE_PTR:
147 format = record->data.hostname;
149 case GNUNET_DNSPARSER_TYPE_SOA:
150 if (NULL == record->data.soa)
151 format = "<invalid>";
154 GNUNET_asprintf (&tmp,
155 "origin: %s, mail: %s, serial = %u, refresh = %u s, retry = %u s, expire = %u s, minimum = %u s",
156 record->data.soa->mname,
157 record->data.soa->rname,
158 (unsigned int) record->data.soa->serial,
159 (unsigned int) record->data.soa->refresh,
160 (unsigned int) record->data.soa->retry,
161 (unsigned int) record->data.soa->expire,
162 (unsigned int) record->data.soa->minimum_ttl);
166 case GNUNET_DNSPARSER_TYPE_MX:
167 if (record->data.mx == NULL)
168 format = "<invalid>";
171 GNUNET_asprintf (&tmp,
173 record->data.mx->preference,
174 record->data.mx->mxhost);
178 case GNUNET_DNSPARSER_TYPE_SRV:
179 if (NULL == record->data.srv)
180 format = "<invalid>";
183 GNUNET_asprintf (&tmp,
184 "priority %u, weight = %s, port = %u, target = %s",
185 (unsigned int) record->data.srv->priority,
186 (unsigned int) record->data.srv->weight,
187 (unsigned int) record->data.srv->port,
188 record->data.srv->target);
192 case GNUNET_DNSPARSER_TYPE_TXT:
193 GNUNET_asprintf (&tmp,
195 (unsigned int) record->data.raw.data_len,
196 record->data.raw.data);
200 format = "<payload>";
204 "\t\t%s %s: %s = %s (%u s)\n",
205 get_class (record->dns_traffic_class),
206 get_type (record->type),
209 (unsigned int) (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL));
210 GNUNET_free_non_null (tmp);
215 * Signature of a function that is called whenever the DNS service
216 * encounters a DNS request and needs to do something with it. The
217 * function has then the chance to generate or modify the response by
218 * calling one of the three "GNUNET_DNS_request_*" continuations.
220 * When a request is intercepted, this function is called first to
221 * give the client a chance to do the complete address resolution;
222 * "rdata" will be NULL for this first call for a DNS request, unless
223 * some other client has already filled in a response.
225 * If multiple clients exist, all of them are called before the global
226 * DNS. The global DNS is only called if all of the clients'
227 * functions call GNUNET_DNS_request_forward. Functions that call
228 * GNUNET_DNS_request_forward will be called again before a final
229 * response is returned to the application. If any of the clients'
230 * functions call GNUNET_DNS_request_drop, the response is dropped.
233 * @param rh request handle to user for reply
234 * @param request_length number of bytes in request
235 * @param request udp payload of the DNS request
238 display_request (void *cls,
239 struct GNUNET_DNS_RequestHandle *rh,
240 size_t request_length,
243 static const char *return_codes[] =
245 "No error", "Format error", "Server failure", "Name error",
246 "Not implemented", "Refused", "YXDomain", "YXRRset",
247 "NXRRset", "NOT AUTH", "NOT ZONE", "<invalid>",
248 "<invalid>", "<invalid>", "<invalid>", "<invalid>"
250 static const char *op_codes[] =
252 "Query", "Inverse query", "Status", "<invalid>",
253 "<invalid>", "<invalid>", "<invalid>", "<invalid>",
254 "<invalid>", "<invalid>", "<invalid>", "<invalid>",
255 "<invalid>", "<invalid>", "<invalid>", "<invalid>"
257 struct GNUNET_DNSPARSER_Packet *p;
260 p = GNUNET_DNSPARSER_parse (request, request_length);
263 fprintf (stderr, "Received malformed DNS packet!\n");
264 // FIXME: drop instead?
265 GNUNET_DNS_request_forward (rh);
269 "%s with ID: %5u Flags: %s%s%s%s%s%s, Return Code: %s, Opcode: %s\n",
270 p->flags.query_or_response ? "Response" : "Query",
272 p->flags.recursion_desired ? "RD " : "",
273 p->flags.message_truncated ? "MT " : "",
274 p->flags.authoritative_answer ? "AA " : "",
275 p->flags.checking_disabled ? "CD " : "",
276 p->flags.authenticated_data ? "AD " : "",
277 p->flags.recursion_available ? "RA " : "",
278 return_codes[p->flags.return_code & 15],
279 op_codes[p->flags.opcode & 15]);
280 if (p->num_queries > 0)
283 for (i=0;i<p->num_queries;i++)
284 display_query (&p->queries[i]);
286 if (p->num_answers > 0)
289 for (i=0;i<p->num_answers;i++)
290 display_record (&p->answers[i]);
291 fprintf (stdout, "\n");
292 GNUNET_DNSPARSER_free_packet (p);
293 GNUNET_DNS_request_forward (rh);
301 do_disconnect (void *cls)
305 GNUNET_DNS_disconnect (handle);
312 * Main function that will be run by the scheduler.
315 * @param args remaining command-line arguments
316 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
317 * @param cfg configuration
320 run (void *cls, char *const *args, const char *cfgfile,
321 const struct GNUNET_CONFIGURATION_Handle *cfg)
323 enum GNUNET_DNS_Flags flags;
325 flags = GNUNET_DNS_FLAG_REQUEST_MONITOR | GNUNET_DNS_FLAG_RESPONSE_MONITOR;
326 if (inbound_only | outbound_only)
329 flags |= GNUNET_DNS_FLAG_REQUEST_MONITOR;
331 flags |= GNUNET_DNS_FLAG_RESPONSE_MONITOR;
333 GNUNET_DNS_connect (cfg,
337 GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL);
342 main (int argc, char *const *argv)
344 struct GNUNET_GETOPT_CommandLineOption options[] = {
346 GNUNET_GETOPT_option_flag ('i',
348 gettext_noop ("only monitor DNS queries"),
351 GNUNET_GETOPT_option_flag ('o',
353 gettext_noop ("only monitor DNS queries"),
356 GNUNET_GETOPT_option_verbose (&verbosity),
357 GNUNET_GETOPT_OPTION_END
360 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
363 GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-monitor",
365 ("Monitor DNS queries."), options,
366 &run, NULL)) ? ret : 1;
367 GNUNET_free ((void*) argv);
372 /* end of gnunet-dns-monitor.c */