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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file src/dns/gnunet-dns-monitor.c
23 * @brief Tool to monitor DNS queries
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dns_service.h"
30 #include "gnunet_dnsparser_lib.h"
33 * Handle to transport service.
35 static struct GNUNET_DNS_Handle *handle;
40 static int inbound_only;
45 static int outbound_only;
48 * Global return value (0 success).
53 * Selected level of verbosity.
59 * Convert numeric DNS record type to a string.
61 * @param type type to convert
62 * @return type as string, only valid until the next call to this function
65 get_type (uint16_t type)
70 case GNUNET_DNSPARSER_TYPE_A: return "A";
71 case GNUNET_DNSPARSER_TYPE_NS: return "NS";
72 case GNUNET_DNSPARSER_TYPE_CNAME: return "CNAME";
73 case GNUNET_DNSPARSER_TYPE_SOA: return "SOA";
74 case GNUNET_DNSPARSER_TYPE_PTR: return "PTR";
75 case GNUNET_DNSPARSER_TYPE_MX: return "MX";
76 case GNUNET_DNSPARSER_TYPE_TXT: return "TXT";
77 case GNUNET_DNSPARSER_TYPE_AAAA: return "AAAA";
78 case GNUNET_DNSPARSER_TYPE_SRV: return "SRV";
80 GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) type);
86 * Convert numeric DNS record class to a string.
88 * @param class class to convert
89 * @return class as string, only valid until the next call to this function
92 get_class (uint16_t class)
97 case GNUNET_TUN_DNS_CLASS_INTERNET: return "IN";
98 case GNUNET_TUN_DNS_CLASS_CHAOS: return "CHAOS";
99 case GNUNET_TUN_DNS_CLASS_HESIOD: return "HESIOD";
101 GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) class);
107 * Output the given DNS query to stdout.
109 * @param query query to display.
112 display_query (const struct GNUNET_DNSPARSER_Query *query)
116 get_class (query->dns_traffic_class),
117 get_type (query->type),
123 * Output the given DNS record to stdout.
125 * @param record record to display.
128 display_record (const struct GNUNET_DNSPARSER_Record *record)
131 char buf[INET6_ADDRSTRLEN];
135 switch (record->type)
137 case GNUNET_DNSPARSER_TYPE_A:
138 if (record->data.raw.data_len != sizeof (struct in_addr))
139 format = "<invalid>";
141 format = inet_ntop (AF_INET, record->data.raw.data, buf, sizeof (buf));
143 case GNUNET_DNSPARSER_TYPE_AAAA:
144 if (record->data.raw.data_len != sizeof (struct in6_addr))
145 format = "<invalid>";
147 format = inet_ntop (AF_INET6, record->data.raw.data, buf, sizeof (buf));
149 case GNUNET_DNSPARSER_TYPE_NS:
150 case GNUNET_DNSPARSER_TYPE_CNAME:
151 case GNUNET_DNSPARSER_TYPE_PTR:
152 format = record->data.hostname;
154 case GNUNET_DNSPARSER_TYPE_SOA:
155 if (NULL == record->data.soa)
156 format = "<invalid>";
159 GNUNET_asprintf (&tmp,
160 "origin: %s, mail: %s, serial = %u, refresh = %u s, retry = %u s, expire = %u s, minimum = %u s",
161 record->data.soa->mname,
162 record->data.soa->rname,
163 (unsigned int) record->data.soa->serial,
164 (unsigned int) record->data.soa->refresh,
165 (unsigned int) record->data.soa->retry,
166 (unsigned int) record->data.soa->expire,
167 (unsigned int) record->data.soa->minimum_ttl);
171 case GNUNET_DNSPARSER_TYPE_MX:
172 if (record->data.mx == NULL)
173 format = "<invalid>";
176 GNUNET_asprintf (&tmp,
178 record->data.mx->preference,
179 record->data.mx->mxhost);
183 case GNUNET_DNSPARSER_TYPE_SRV:
184 if (NULL == record->data.srv)
185 format = "<invalid>";
188 GNUNET_asprintf (&tmp,
189 "priority %u, weight = %s, port = %u, target = %s",
190 (unsigned int) record->data.srv->priority,
191 (unsigned int) record->data.srv->weight,
192 (unsigned int) record->data.srv->port,
193 record->data.srv->target);
197 case GNUNET_DNSPARSER_TYPE_TXT:
198 GNUNET_asprintf (&tmp,
200 (unsigned int) record->data.raw.data_len,
201 record->data.raw.data);
205 format = "<payload>";
209 "\t\t%s %s: %s = %s (%u s)\n",
210 get_class (record->dns_traffic_class),
211 get_type (record->type),
214 (unsigned int) (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL));
215 GNUNET_free_non_null (tmp);
220 * Signature of a function that is called whenever the DNS service
221 * encounters a DNS request and needs to do something with it. The
222 * function has then the chance to generate or modify the response by
223 * calling one of the three "GNUNET_DNS_request_*" continuations.
225 * When a request is intercepted, this function is called first to
226 * give the client a chance to do the complete address resolution;
227 * "rdata" will be NULL for this first call for a DNS request, unless
228 * some other client has already filled in a response.
230 * If multiple clients exist, all of them are called before the global
231 * DNS. The global DNS is only called if all of the clients'
232 * functions call GNUNET_DNS_request_forward. Functions that call
233 * GNUNET_DNS_request_forward will be called again before a final
234 * response is returned to the application. If any of the clients'
235 * functions call GNUNET_DNS_request_drop, the response is dropped.
238 * @param rh request handle to user for reply
239 * @param request_length number of bytes in request
240 * @param request udp payload of the DNS request
243 display_request (void *cls,
244 struct GNUNET_DNS_RequestHandle *rh,
245 size_t request_length,
248 static const char *return_codes[] =
250 "No error", "Format error", "Server failure", "Name error",
251 "Not implemented", "Refused", "YXDomain", "YXRRset",
252 "NXRRset", "NOT AUTH", "NOT ZONE", "<invalid>",
253 "<invalid>", "<invalid>", "<invalid>", "<invalid>"
255 static const char *op_codes[] =
257 "Query", "Inverse query", "Status", "<invalid>",
258 "<invalid>", "<invalid>", "<invalid>", "<invalid>",
259 "<invalid>", "<invalid>", "<invalid>", "<invalid>",
260 "<invalid>", "<invalid>", "<invalid>", "<invalid>"
262 struct GNUNET_DNSPARSER_Packet *p;
265 p = GNUNET_DNSPARSER_parse (request, request_length);
268 fprintf (stderr, "Received malformed DNS packet!\n");
269 // FIXME: drop instead?
270 GNUNET_DNS_request_forward (rh);
274 "%s with ID: %5u Flags: %s%s%s%s%s%s, Return Code: %s, Opcode: %s\n",
275 p->flags.query_or_response ? "Response" : "Query",
277 p->flags.recursion_desired ? "RD " : "",
278 p->flags.message_truncated ? "MT " : "",
279 p->flags.authoritative_answer ? "AA " : "",
280 p->flags.checking_disabled ? "CD " : "",
281 p->flags.authenticated_data ? "AD " : "",
282 p->flags.recursion_available ? "RA " : "",
283 return_codes[p->flags.return_code & 15],
284 op_codes[p->flags.opcode & 15]);
285 if (p->num_queries > 0)
288 for (i=0;i<p->num_queries;i++)
289 display_query (&p->queries[i]);
291 if (p->num_answers > 0)
294 for (i=0;i<p->num_answers;i++)
295 display_record (&p->answers[i]);
296 fprintf (stdout, "\n");
297 GNUNET_DNSPARSER_free_packet (p);
298 GNUNET_DNS_request_forward (rh);
306 do_disconnect (void *cls)
310 GNUNET_DNS_disconnect (handle);
317 * Main function that will be run by the scheduler.
320 * @param args remaining command-line arguments
321 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
322 * @param cfg configuration
325 run (void *cls, char *const *args, const char *cfgfile,
326 const struct GNUNET_CONFIGURATION_Handle *cfg)
328 enum GNUNET_DNS_Flags flags;
330 flags = GNUNET_DNS_FLAG_REQUEST_MONITOR | GNUNET_DNS_FLAG_RESPONSE_MONITOR;
331 if (inbound_only | outbound_only)
334 flags |= GNUNET_DNS_FLAG_REQUEST_MONITOR;
336 flags |= GNUNET_DNS_FLAG_RESPONSE_MONITOR;
338 GNUNET_DNS_connect (cfg,
342 GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL);
347 main (int argc, char *const *argv)
349 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
350 {'i', "inbound-only", NULL,
351 gettext_noop ("only monitor DNS queries"),
352 0, &GNUNET_GETOPT_set_one, &inbound_only},
353 {'o', "outbound-only", NULL,
354 gettext_noop ("only monitor DNS replies"),
355 0, &GNUNET_GETOPT_set_one, &outbound_only},
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 */