2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 "service: %s, protocol: %s, domain_name = %s, priority %u, weight = %s, port = %u, target = %s",
190 record->data.srv->service,
191 record->data.srv->proto,
192 record->data.srv->domain_name,
193 (unsigned int) record->data.srv->priority,
194 (unsigned int) record->data.srv->weight,
195 (unsigned int) record->data.srv->port,
196 record->data.srv->target);
200 case GNUNET_DNSPARSER_TYPE_TXT:
201 GNUNET_asprintf (&tmp,
203 (unsigned int) record->data.raw.data_len,
204 record->data.raw.data);
208 format = "<payload>";
212 "\t\t%s %s: %s = %s (%u s)\n",
213 get_class (record->dns_traffic_class),
214 get_type (record->type),
217 (unsigned int) (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL));
218 GNUNET_free_non_null (tmp);
223 * Signature of a function that is called whenever the DNS service
224 * encounters a DNS request and needs to do something with it. The
225 * function has then the chance to generate or modify the response by
226 * calling one of the three "GNUNET_DNS_request_*" continuations.
228 * When a request is intercepted, this function is called first to
229 * give the client a chance to do the complete address resolution;
230 * "rdata" will be NULL for this first call for a DNS request, unless
231 * some other client has already filled in a response.
233 * If multiple clients exist, all of them are called before the global
234 * DNS. The global DNS is only called if all of the clients'
235 * functions call GNUNET_DNS_request_forward. Functions that call
236 * GNUNET_DNS_request_forward will be called again before a final
237 * response is returned to the application. If any of the clients'
238 * functions call GNUNET_DNS_request_drop, the response is dropped.
241 * @param rh request handle to user for reply
242 * @param request_length number of bytes in request
243 * @param request udp payload of the DNS request
246 display_request (void *cls,
247 struct GNUNET_DNS_RequestHandle *rh,
248 size_t request_length,
251 static const char *return_codes[] =
253 "No error", "Format error", "Server failure", "Name error",
254 "Not implemented", "Refused", "YXDomain", "YXRRset",
255 "NXRRset", "NOT AUTH", "NOT ZONE", "<invalid>",
256 "<invalid>", "<invalid>", "<invalid>", "<invalid>"
258 static const char *op_codes[] =
260 "Query", "Inverse query", "Status", "<invalid>",
261 "<invalid>", "<invalid>", "<invalid>", "<invalid>",
262 "<invalid>", "<invalid>", "<invalid>", "<invalid>",
263 "<invalid>", "<invalid>", "<invalid>", "<invalid>"
265 struct GNUNET_DNSPARSER_Packet *p;
268 p = GNUNET_DNSPARSER_parse (request, request_length);
271 fprintf (stderr, "Received malformed DNS packet!\n");
272 // FIXME: drop instead?
273 GNUNET_DNS_request_forward (rh);
277 "%s with ID: %5u Flags: %s%s%s%s%s%s, Return Code: %s, Opcode: %s\n",
278 p->flags.query_or_response ? "Response" : "Query",
280 p->flags.recursion_desired ? "RD " : "",
281 p->flags.message_truncated ? "MT " : "",
282 p->flags.authoritative_answer ? "AA " : "",
283 p->flags.checking_disabled ? "CD " : "",
284 p->flags.authenticated_data ? "AD " : "",
285 p->flags.recursion_available ? "RA " : "",
286 return_codes[p->flags.return_code & 15],
287 op_codes[p->flags.opcode & 15]);
288 if (p->num_queries > 0)
291 for (i=0;i<p->num_queries;i++)
292 display_query (&p->queries[i]);
294 if (p->num_answers > 0)
297 for (i=0;i<p->num_answers;i++)
298 display_record (&p->answers[i]);
299 fprintf (stdout, "\n");
300 GNUNET_DNSPARSER_free_packet (p);
301 GNUNET_DNS_request_forward (rh);
309 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
313 GNUNET_DNS_disconnect (handle);
320 * Main function that will be run by the scheduler.
323 * @param args remaining command-line arguments
324 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
325 * @param cfg configuration
328 run (void *cls, char *const *args, const char *cfgfile,
329 const struct GNUNET_CONFIGURATION_Handle *cfg)
331 enum GNUNET_DNS_Flags flags;
333 flags = GNUNET_DNS_FLAG_REQUEST_MONITOR | GNUNET_DNS_FLAG_RESPONSE_MONITOR;
334 if (inbound_only | outbound_only)
337 flags |= GNUNET_DNS_FLAG_REQUEST_MONITOR;
339 flags |= GNUNET_DNS_FLAG_RESPONSE_MONITOR;
341 GNUNET_DNS_connect (cfg,
345 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
346 &do_disconnect, NULL);
351 main (int argc, char *const *argv)
353 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
354 {'i', "inbound-only", NULL,
355 gettext_noop ("only monitor DNS queries"),
356 0, &GNUNET_GETOPT_set_one, &inbound_only},
357 {'o', "outbound-only", NULL,
358 gettext_noop ("only monitor DNS replies"),
359 0, &GNUNET_GETOPT_set_one, &outbound_only},
360 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
361 GNUNET_GETOPT_OPTION_END
364 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
367 GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-monitor",
369 ("Monitor DNS queries."), options,
370 &run, NULL)) ? ret : 1;
371 GNUNET_free ((void*) argv);
376 /* end of gnunet-dns-monitor.c */