glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / dns / gnunet-dns-monitor.c
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
16 /**
17  * @file src/dns/gnunet-dns-monitor.c
18  * @brief Tool to monitor DNS queries
19  * @author Christian Grothoff
20  */
21
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_dns_service.h"
25 #include "gnunet_dnsparser_lib.h"
26
27 /**
28  * Handle to transport service.
29  */
30 static struct GNUNET_DNS_Handle *handle;
31
32 /**
33  * Option -i.
34  */
35 static int inbound_only;
36
37 /**
38  * Option -o.
39  */
40 static int outbound_only;
41
42 /**
43  * Global return value (0 success).
44  */
45 static int ret;
46
47 /**
48  * Selected level of verbosity.
49  */
50 static unsigned int verbosity;
51
52
53 /**
54  * Convert numeric DNS record type to a string.
55  *
56  * @param type type to convert
57  * @return type as string, only valid until the next call to this function
58  */
59 static const char *
60 get_type (uint16_t type)
61 {
62   static char buf[6];
63   switch (type)
64   {
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";
74   }
75   GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) type);
76   return buf;
77 }
78
79
80 /**
81  * Convert numeric DNS record class to a string.
82  *
83  * @param class class to convert
84  * @return class as string, only valid until the next call to this function
85  */
86 static const char *
87 get_class (uint16_t class)
88 {
89   static char buf[6];
90   switch (class)
91   {
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";
95   }
96   GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) class);
97   return buf;
98 }
99
100
101 /**
102  * Output the given DNS query to stdout.
103  *
104  * @param query query to display.
105  */
106 static void
107 display_query (const struct GNUNET_DNSPARSER_Query *query)
108 {
109   fprintf (stdout,
110            "\t\t%s %s: %s\n",
111            get_class (query->dns_traffic_class),
112            get_type (query->type),
113            query->name);
114 }
115
116
117 /**
118  * Output the given DNS record to stdout.
119  *
120  * @param record record to display.
121  */
122 static void
123 display_record (const struct GNUNET_DNSPARSER_Record *record)
124 {
125   const char *format;
126   char buf[INET6_ADDRSTRLEN];
127   char *tmp;
128
129   tmp = NULL;
130   switch (record->type)
131   {
132   case GNUNET_DNSPARSER_TYPE_A:
133     if (record->data.raw.data_len != sizeof (struct in_addr))
134       format = "<invalid>";
135     else
136       format = inet_ntop (AF_INET, record->data.raw.data, buf, sizeof (buf));
137     break;
138   case GNUNET_DNSPARSER_TYPE_AAAA:
139     if (record->data.raw.data_len != sizeof (struct in6_addr))
140       format = "<invalid>";
141     else
142       format = inet_ntop (AF_INET6, record->data.raw.data, buf, sizeof (buf));
143     break;
144   case GNUNET_DNSPARSER_TYPE_NS:
145   case GNUNET_DNSPARSER_TYPE_CNAME:
146   case GNUNET_DNSPARSER_TYPE_PTR:
147     format = record->data.hostname;
148     break;
149   case GNUNET_DNSPARSER_TYPE_SOA:
150     if (NULL == record->data.soa)
151       format = "<invalid>";
152     else
153     {
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);
163       format = tmp;
164     }
165     break;
166   case GNUNET_DNSPARSER_TYPE_MX:
167     if (record->data.mx == NULL)
168       format = "<invalid>";
169     else
170     {
171       GNUNET_asprintf (&tmp,
172                        "%u: %s",
173                        record->data.mx->preference,
174                        record->data.mx->mxhost);
175       format = tmp;
176     }
177     break;
178   case GNUNET_DNSPARSER_TYPE_SRV:
179     if (NULL == record->data.srv)
180       format = "<invalid>";
181     else
182     {
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);
189       format = tmp;
190     }
191     break;
192   case GNUNET_DNSPARSER_TYPE_TXT:
193     GNUNET_asprintf (&tmp,
194                      "%.*s",
195                      (unsigned int) record->data.raw.data_len,
196                      record->data.raw.data);
197     format = tmp;
198     break;
199   default:
200     format = "<payload>";
201     break;
202   }
203   fprintf (stdout,
204            "\t\t%s %s: %s = %s (%u s)\n",
205            get_class (record->dns_traffic_class),
206            get_type (record->type),
207            record->name,
208            format,
209            (unsigned int) (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL));
210   GNUNET_free_non_null (tmp);
211 }
212
213
214 /**
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.
219  *
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.
224  *
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.
231  *
232  * @param cls closure
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
236  */
237 static void
238 display_request (void *cls,
239                  struct GNUNET_DNS_RequestHandle *rh,
240                  size_t request_length,
241                  const char *request)
242 {
243   static const char *return_codes[] =
244     {
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>"
249     };
250   static const char *op_codes[] =
251     {
252       "Query", "Inverse query", "Status", "<invalid>",
253       "<invalid>", "<invalid>", "<invalid>", "<invalid>",
254       "<invalid>", "<invalid>", "<invalid>", "<invalid>",
255       "<invalid>", "<invalid>", "<invalid>", "<invalid>"
256     };
257   struct GNUNET_DNSPARSER_Packet *p;
258   unsigned int i;
259
260   p = GNUNET_DNSPARSER_parse (request, request_length);
261   if (NULL == p)
262   {
263     fprintf (stderr, "Received malformed DNS packet!\n");
264     // FIXME: drop instead?
265     GNUNET_DNS_request_forward (rh);
266     return;
267   }
268   fprintf (stdout,
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",
271            p->id,
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)
281     fprintf (stdout,
282              "\tQueries:\n");
283   for (i=0;i<p->num_queries;i++)
284     display_query (&p->queries[i]);
285
286   if (p->num_answers > 0)
287     fprintf (stdout,
288              "\tAnswers:\n");
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);
294 }
295
296
297 /**
298  * Shutdown.
299  */
300 static void
301 do_disconnect (void *cls)
302 {
303   if (NULL != handle)
304   {
305     GNUNET_DNS_disconnect (handle);
306     handle = NULL;
307   }
308 }
309
310
311 /**
312  * Main function that will be run by the scheduler.
313  *
314  * @param cls closure
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
318  */
319 static void
320 run (void *cls, char *const *args, const char *cfgfile,
321      const struct GNUNET_CONFIGURATION_Handle *cfg)
322 {
323   enum GNUNET_DNS_Flags flags;
324
325   flags = GNUNET_DNS_FLAG_REQUEST_MONITOR | GNUNET_DNS_FLAG_RESPONSE_MONITOR;
326   if (inbound_only | outbound_only)
327     flags = 0;
328   if (inbound_only)
329     flags |= GNUNET_DNS_FLAG_REQUEST_MONITOR;
330   if (outbound_only)
331     flags |= GNUNET_DNS_FLAG_RESPONSE_MONITOR;
332   handle =
333     GNUNET_DNS_connect (cfg,
334                         flags,
335                         &display_request,
336                         NULL);
337   GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL);
338 }
339
340
341 int
342 main (int argc, char *const *argv)
343 {
344   struct GNUNET_GETOPT_CommandLineOption options[] = {
345
346     GNUNET_GETOPT_option_flag ('i',
347                                   "inbound-only",
348                                   gettext_noop ("only monitor DNS queries"),
349                                   &inbound_only),
350
351     GNUNET_GETOPT_option_flag ('o',
352                                   "outbound-only",
353                                   gettext_noop ("only monitor DNS queries"),
354                                   &outbound_only),
355
356     GNUNET_GETOPT_option_verbose (&verbosity),
357     GNUNET_GETOPT_OPTION_END
358   };
359
360   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
361     return 2;
362   ret = (GNUNET_OK ==
363          GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-monitor",
364                              gettext_noop
365                              ("Monitor DNS queries."), options,
366                              &run, NULL)) ? ret : 1;
367   GNUNET_free ((void*) argv);
368   return ret;
369 }
370
371
372 /* end of gnunet-dns-monitor.c */