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-new.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";
79 GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) type);
85 * Convert numeric DNS record class to a string.
87 * @param class class to convert
88 * @return class as string, only valid until the next call to this function
91 get_class (uint16_t class)
96 case GNUNET_DNSPARSER_CLASS_INTERNET: return "IN";
97 case GNUNET_DNSPARSER_CLASS_CHAOS: return "CHAOS";
98 case GNUNET_DNSPARSER_CLASS_HESIOD: return "HESIOD";
100 GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) class);
106 * Output the given DNS query to stdout.
108 * @param query query to display.
111 display_query (const struct GNUNET_DNSPARSER_Query *query)
115 get_class (query->class),
116 get_type (query->type),
122 * Output the given DNS query to stdout.
124 * @param query query to display.
127 display_record (const struct GNUNET_DNSPARSER_Record *record)
130 char buf[INET6_ADDRSTRLEN];
134 switch (record->type)
136 case GNUNET_DNSPARSER_TYPE_A:
137 if (record->data_len != sizeof (struct in_addr))
138 format = "<invalid>";
140 format = inet_ntop (AF_INET, record->data.raw, buf, sizeof (buf));
142 case GNUNET_DNSPARSER_TYPE_AAAA:
143 if (record->data_len != sizeof (struct in6_addr))
144 format = "<invalid>";
146 format = inet_ntop (AF_INET6, record->data.raw, buf, sizeof (buf));
148 case GNUNET_DNSPARSER_TYPE_NS:
149 case GNUNET_DNSPARSER_TYPE_CNAME:
150 case GNUNET_DNSPARSER_TYPE_PTR:
151 format = record->data.hostname;
153 case GNUNET_DNSPARSER_TYPE_SOA:
154 if (record->data.soa == NULL)
155 format = "<invalid>";
158 GNUNET_asprintf (&tmp,
159 "origin: %s, mail: %s, serial = %u, refresh = %u s, retry = %u s, expire = %u s, minimum = %u s",
160 record->data.soa->mname,
161 record->data.soa->rname,
162 (unsigned int) record->data.soa->serial,
163 (unsigned int) record->data.soa->refresh,
164 (unsigned int) record->data.soa->retry,
165 (unsigned int) record->data.soa->expire,
166 (unsigned int) record->data.soa->minimum_ttl);
170 case GNUNET_DNSPARSER_TYPE_MX:
171 if (record->data.mx == NULL)
172 format = "<invalid>";
175 GNUNET_asprintf (&tmp,
177 record->data.mx->preference,
178 record->data.mx->mxhost);
182 case GNUNET_DNSPARSER_TYPE_TXT:
183 GNUNET_asprintf (&tmp,
185 (unsigned int) record->data_len,
190 format = "<payload>";
194 "\t\t%s %s: %s = %s (%u bytes, %u s)\n",
195 get_class (record->class),
196 get_type (record->type),
199 (unsigned int) record->data_len,
200 (unsigned int) (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value / 1000));
201 GNUNET_free_non_null (tmp);
206 * Signature of a function that is called whenever the DNS service
207 * encounters a DNS request and needs to do something with it. The
208 * function has then the chance to generate or modify the response by
209 * calling one of the three "GNUNET_DNS_request_*" continuations.
211 * When a request is intercepted, this function is called first to
212 * give the client a chance to do the complete address resolution;
213 * "rdata" will be NULL for this first call for a DNS request, unless
214 * some other client has already filled in a response.
216 * If multiple clients exist, all of them are called before the global
217 * DNS. The global DNS is only called if all of the clients'
218 * functions call GNUNET_DNS_request_forward. Functions that call
219 * GNUNET_DNS_request_forward will be called again before a final
220 * response is returned to the application. If any of the clients'
221 * functions call GNUNET_DNS_request_drop, the response is dropped.
224 * @param rh request handle to user for reply
225 * @param request_length number of bytes in request
226 * @param request udp payload of the DNS request
229 display_request (void *cls,
230 struct GNUNET_DNS_RequestHandle *rh,
231 size_t request_length,
234 static const char *return_codes[] =
236 "No error", "Format error", "Server failure", "Name error",
237 "Not implemented", "Refused", "YXDomain", "YXRRset",
238 "NXRRset", "NOT AUTH", "NOT ZONE", "<invalid>",
239 "<invalid>", "<invalid>", "<invalid>", "<invalid>"
241 static const char *op_codes[] =
243 "Query", "Inverse query", "Status", "<invalid>",
244 "<invalid>", "<invalid>", "<invalid>", "<invalid>",
245 "<invalid>", "<invalid>", "<invalid>", "<invalid>",
246 "<invalid>", "<invalid>", "<invalid>", "<invalid>"
248 struct GNUNET_DNSPARSER_Packet *p;
251 p = GNUNET_DNSPARSER_parse (request, request_length);
254 fprintf (stderr, "Received malformed DNS packet!\n");
255 // FIXME: drop instead?
256 GNUNET_DNS_request_forward (rh);
260 "%s with ID: %5u Flags: %s%s%s%s%s%s, Return Code: %s, Opcode: %s\n",
261 p->flags.query_or_response ? "Response" : "Query",
263 p->flags.recursion_desired ? "RD " : "",
264 p->flags.message_truncated ? "MT " : "",
265 p->flags.authoritative_answer ? "AA " : "",
266 p->flags.checking_disabled ? "CD " : "",
267 p->flags.authenticated_data ? "AD " : "",
268 p->flags.recursion_available ? "RA " : "",
269 return_codes[p->flags.return_code & 15],
270 op_codes[p->flags.opcode & 15]);
271 if (p->num_queries > 0)
274 for (i=0;i<p->num_queries;i++)
275 display_query (&p->queries[i]);
277 if (p->num_answers > 0)
280 for (i=0;i<p->num_answers;i++)
281 display_record (&p->answers[i]);
282 fprintf (stdout, "\n");
283 GNUNET_DNSPARSER_free_packet (p);
284 GNUNET_DNS_request_forward (rh);
292 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
296 GNUNET_DNS_disconnect (handle);
303 * Main function that will be run by the scheduler.
306 * @param args remaining command-line arguments
307 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
308 * @param cfg configuration
311 run (void *cls, char *const *args, const char *cfgfile,
312 const struct GNUNET_CONFIGURATION_Handle *cfg)
314 enum GNUNET_DNS_Flags flags;
316 flags = GNUNET_DNS_FLAG_REQUEST_MONITOR | GNUNET_DNS_FLAG_RESPONSE_MONITOR;
317 if (inbound_only | outbound_only)
320 flags |= GNUNET_DNS_FLAG_REQUEST_MONITOR;
322 flags |= GNUNET_DNS_FLAG_RESPONSE_MONITOR;
324 GNUNET_DNS_connect (cfg,
328 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
329 &do_disconnect, NULL);
334 main (int argc, char *const *argv)
336 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
337 {'i', "inbound-only", NULL,
338 gettext_noop ("only monitor DNS queries"),
339 0, &GNUNET_GETOPT_set_one, &inbound_only},
340 {'o', "outbound-only", NULL,
341 gettext_noop ("only monitor DNS replies"),
342 0, &GNUNET_GETOPT_set_one, &outbound_only},
343 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
344 GNUNET_GETOPT_OPTION_END
347 GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-monitor",
349 ("Monitor DNS queries."), options,
350 &run, NULL)) ? ret : 1;
354 /* end of gnunet-dns-monitor.c */