fix
[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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file src/dns/gnunet-dns-monitor.c
21  * @brief Tool to monitor DNS queries
22  * @author Christian Grothoff
23  */
24
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_dns_service.h"
28 #include "gnunet_dnsparser_lib.h"
29
30 /**
31  * Handle to transport service.
32  */
33 static struct GNUNET_DNS_Handle *handle;
34
35 /**
36  * Option -i.
37  */
38 static int inbound_only;
39
40 /**
41  * Option -o.
42  */
43 static int outbound_only;
44
45 /**
46  * Global return value (0 success).
47  */
48 static int ret;
49
50 /**
51  * Selected level of verbosity.
52  */
53 static unsigned int verbosity;
54
55
56 /**
57  * Convert numeric DNS record type to a string.
58  *
59  * @param type type to convert
60  * @return type as string, only valid until the next call to this function
61  */
62 static const char *
63 get_type (uint16_t type)
64 {
65   static char buf[6];
66   switch (type)
67   {
68   case GNUNET_DNSPARSER_TYPE_A: return "A";
69   case GNUNET_DNSPARSER_TYPE_NS: return "NS";
70   case GNUNET_DNSPARSER_TYPE_CNAME: return "CNAME";
71   case GNUNET_DNSPARSER_TYPE_SOA: return "SOA";
72   case GNUNET_DNSPARSER_TYPE_PTR: return "PTR";
73   case GNUNET_DNSPARSER_TYPE_MX: return "MX";
74   case GNUNET_DNSPARSER_TYPE_TXT: return "TXT";
75   case GNUNET_DNSPARSER_TYPE_AAAA: return "AAAA";
76   case GNUNET_DNSPARSER_TYPE_SRV: return "SRV";
77   }
78   GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) type);
79   return buf;
80 }
81
82
83 /**
84  * Convert numeric DNS record class to a string.
85  *
86  * @param class class to convert
87  * @return class as string, only valid until the next call to this function
88  */
89 static const char *
90 get_class (uint16_t class)
91 {
92   static char buf[6];
93   switch (class)
94   {
95   case GNUNET_TUN_DNS_CLASS_INTERNET: return "IN";
96   case GNUNET_TUN_DNS_CLASS_CHAOS: return "CHAOS";
97   case GNUNET_TUN_DNS_CLASS_HESIOD: return "HESIOD";
98   }
99   GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) class);
100   return buf;
101 }
102
103
104 /**
105  * Output the given DNS query to stdout.
106  *
107  * @param query query to display.
108  */
109 static void
110 display_query (const struct GNUNET_DNSPARSER_Query *query)
111 {
112   fprintf (stdout,
113            "\t\t%s %s: %s\n",
114            get_class (query->dns_traffic_class),
115            get_type (query->type),
116            query->name);
117 }
118
119
120 /**
121  * Output the given DNS record to stdout.
122  *
123  * @param record record to display.
124  */
125 static void
126 display_record (const struct GNUNET_DNSPARSER_Record *record)
127 {
128   const char *format;
129   char buf[INET6_ADDRSTRLEN];
130   char *tmp;
131
132   tmp = NULL;
133   switch (record->type)
134   {
135   case GNUNET_DNSPARSER_TYPE_A:
136     if (record->data.raw.data_len != sizeof (struct in_addr))
137       format = "<invalid>";
138     else
139       format = inet_ntop (AF_INET, record->data.raw.data, buf, sizeof (buf));
140     break;
141   case GNUNET_DNSPARSER_TYPE_AAAA:
142     if (record->data.raw.data_len != sizeof (struct in6_addr))
143       format = "<invalid>";
144     else
145       format = inet_ntop (AF_INET6, record->data.raw.data, buf, sizeof (buf));
146     break;
147   case GNUNET_DNSPARSER_TYPE_NS:
148   case GNUNET_DNSPARSER_TYPE_CNAME:
149   case GNUNET_DNSPARSER_TYPE_PTR:
150     format = record->data.hostname;
151     break;
152   case GNUNET_DNSPARSER_TYPE_SOA:
153     if (NULL == record->data.soa)
154       format = "<invalid>";
155     else
156     {
157       GNUNET_asprintf (&tmp,
158                        "origin: %s, mail: %s, serial = %u, refresh = %u s, retry = %u s, expire = %u s, minimum = %u s",
159                        record->data.soa->mname,
160                        record->data.soa->rname,
161                        (unsigned int) record->data.soa->serial,
162                        (unsigned int) record->data.soa->refresh,
163                        (unsigned int) record->data.soa->retry,
164                        (unsigned int) record->data.soa->expire,
165                        (unsigned int) record->data.soa->minimum_ttl);
166       format = tmp;
167     }
168     break;
169   case GNUNET_DNSPARSER_TYPE_MX:
170     if (record->data.mx == NULL)
171       format = "<invalid>";
172     else
173     {
174       GNUNET_asprintf (&tmp,
175                        "%u: %s",
176                        record->data.mx->preference,
177                        record->data.mx->mxhost);
178       format = tmp;
179     }
180     break;
181   case GNUNET_DNSPARSER_TYPE_SRV:
182     if (NULL == record->data.srv)
183       format = "<invalid>";
184     else
185     {
186       GNUNET_asprintf (&tmp,
187                        "priority %u, weight = %s, port = %u, target = %s",
188                        (unsigned int) record->data.srv->priority,
189                        (unsigned int) record->data.srv->weight,
190                        (unsigned int) record->data.srv->port,
191                        record->data.srv->target);
192       format = tmp;
193     }
194     break;
195   case GNUNET_DNSPARSER_TYPE_TXT:
196     GNUNET_asprintf (&tmp,
197                      "%.*s",
198                      (unsigned int) record->data.raw.data_len,
199                      record->data.raw.data);
200     format = tmp;
201     break;
202   default:
203     format = "<payload>";
204     break;
205   }
206   fprintf (stdout,
207            "\t\t%s %s: %s = %s (%u s)\n",
208            get_class (record->dns_traffic_class),
209            get_type (record->type),
210            record->name,
211            format,
212            (unsigned int) (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL));
213   GNUNET_free_non_null (tmp);
214 }
215
216
217 /**
218  * Signature of a function that is called whenever the DNS service
219  * encounters a DNS request and needs to do something with it.  The
220  * function has then the chance to generate or modify the response by
221  * calling one of the three "GNUNET_DNS_request_*" continuations.
222  *
223  * When a request is intercepted, this function is called first to
224  * give the client a chance to do the complete address resolution;
225  * "rdata" will be NULL for this first call for a DNS request, unless
226  * some other client has already filled in a response.
227  *
228  * If multiple clients exist, all of them are called before the global
229  * DNS.  The global DNS is only called if all of the clients'
230  * functions call GNUNET_DNS_request_forward.  Functions that call
231  * GNUNET_DNS_request_forward will be called again before a final
232  * response is returned to the application.  If any of the clients'
233  * functions call GNUNET_DNS_request_drop, the response is dropped.
234  *
235  * @param cls closure
236  * @param rh request handle to user for reply
237  * @param request_length number of bytes in request
238  * @param request udp payload of the DNS request
239  */
240 static void
241 display_request (void *cls,
242                  struct GNUNET_DNS_RequestHandle *rh,
243                  size_t request_length,
244                  const char *request)
245 {
246   static const char *return_codes[] =
247     {
248       "No error", "Format error", "Server failure", "Name error",
249       "Not implemented", "Refused", "YXDomain", "YXRRset",
250       "NXRRset", "NOT AUTH", "NOT ZONE", "<invalid>",
251       "<invalid>", "<invalid>", "<invalid>", "<invalid>"
252     };
253   static const char *op_codes[] =
254     {
255       "Query", "Inverse query", "Status", "<invalid>",
256       "<invalid>", "<invalid>", "<invalid>", "<invalid>",
257       "<invalid>", "<invalid>", "<invalid>", "<invalid>",
258       "<invalid>", "<invalid>", "<invalid>", "<invalid>"
259     };
260   struct GNUNET_DNSPARSER_Packet *p;
261   unsigned int i;
262
263   p = GNUNET_DNSPARSER_parse (request, request_length);
264   if (NULL == p)
265   {
266     fprintf (stderr, "Received malformed DNS packet!\n");
267     // FIXME: drop instead?
268     GNUNET_DNS_request_forward (rh);
269     return;
270   }
271   fprintf (stdout,
272            "%s with ID: %5u Flags: %s%s%s%s%s%s, Return Code: %s, Opcode: %s\n",
273            p->flags.query_or_response ? "Response" : "Query",
274            p->id,
275            p->flags.recursion_desired ? "RD " : "",
276            p->flags.message_truncated ? "MT " : "",
277            p->flags.authoritative_answer ? "AA " : "",
278            p->flags.checking_disabled ? "CD " : "",
279            p->flags.authenticated_data ? "AD " : "",
280            p->flags.recursion_available ? "RA " : "",
281            return_codes[p->flags.return_code & 15],
282            op_codes[p->flags.opcode & 15]);
283   if (p->num_queries > 0)
284     fprintf (stdout,
285              "\tQueries:\n");
286   for (i=0;i<p->num_queries;i++)
287     display_query (&p->queries[i]);
288
289   if (p->num_answers > 0)
290     fprintf (stdout,
291              "\tAnswers:\n");
292   for (i=0;i<p->num_answers;i++)
293     display_record (&p->answers[i]);
294   fprintf (stdout, "\n");
295   GNUNET_DNSPARSER_free_packet (p);
296   GNUNET_DNS_request_forward (rh);
297 }
298
299
300 /**
301  * Shutdown.
302  */
303 static void
304 do_disconnect (void *cls)
305 {
306   if (NULL != handle)
307   {
308     GNUNET_DNS_disconnect (handle);
309     handle = NULL;
310   }
311 }
312
313
314 /**
315  * Main function that will be run by the scheduler.
316  *
317  * @param cls closure
318  * @param args remaining command-line arguments
319  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
320  * @param cfg configuration
321  */
322 static void
323 run (void *cls, char *const *args, const char *cfgfile,
324      const struct GNUNET_CONFIGURATION_Handle *cfg)
325 {
326   enum GNUNET_DNS_Flags flags;
327
328   flags = GNUNET_DNS_FLAG_REQUEST_MONITOR | GNUNET_DNS_FLAG_RESPONSE_MONITOR;
329   if (inbound_only | outbound_only)
330     flags = 0;
331   if (inbound_only)
332     flags |= GNUNET_DNS_FLAG_REQUEST_MONITOR;
333   if (outbound_only)
334     flags |= GNUNET_DNS_FLAG_RESPONSE_MONITOR;
335   handle =
336     GNUNET_DNS_connect (cfg,
337                         flags,
338                         &display_request,
339                         NULL);
340   GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL);
341 }
342
343
344 int
345 main (int argc, char *const *argv)
346 {
347   struct GNUNET_GETOPT_CommandLineOption options[] = {
348
349     GNUNET_GETOPT_option_flag ('i',
350                                   "inbound-only",
351                                   gettext_noop ("only monitor DNS queries"),
352                                   &inbound_only),
353
354     GNUNET_GETOPT_option_flag ('o',
355                                   "outbound-only",
356                                   gettext_noop ("only monitor DNS queries"),
357                                   &outbound_only),
358
359     GNUNET_GETOPT_option_verbose (&verbosity),
360     GNUNET_GETOPT_OPTION_END
361   };
362
363   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
364     return 2;
365   ret = (GNUNET_OK ==
366          GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-monitor",
367                              gettext_noop
368                              ("Monitor DNS queries."), options,
369                              &run, NULL)) ? ret : 1;
370   GNUNET_free ((void*) argv);
371   return ret;
372 }
373
374
375 /* end of gnunet-dns-monitor.c */