-options
[oweals/gnunet.git] / src / dns / gnunet-dns-monitor.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
19 */
20
21 /**
22  * @file src/dns/gnunet-dns-monitor.c
23  * @brief Tool to monitor DNS queries
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dns_service-new.h"
30 #include "gnunet_dnsparser_lib.h"
31
32 /**
33  * Handle to transport service.
34  */
35 static struct GNUNET_DNS_Handle *handle;
36
37 /**
38  * Option -i.
39  */
40 static int inbound_only;
41
42 /**
43  * Option -o.
44  */
45 static int outbound_only;
46
47 /**
48  * Global return value (0 success).
49  */
50 static int ret;
51
52 /**
53  * Selected level of verbosity.
54  */
55 static int verbosity;
56
57
58 /**
59  * Convert numeric DNS record type to a string.
60  *
61  * @param type type to convert
62  * @return type as string, only valid until the next call to this function
63  */
64 static const char *
65 get_type (uint16_t type)
66 {
67   static char buf[6];
68   switch (type)
69   {
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   }
79   GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) type);
80   return buf;
81 }
82
83
84 /**
85  * Convert numeric DNS record class to a string.
86  *
87  * @param class class to convert
88  * @return class as string, only valid until the next call to this function
89  */
90 static const char *
91 get_class (uint16_t class)
92 {
93   static char buf[6];
94   switch (class)
95   {
96   case GNUNET_DNSPARSER_CLASS_INTERNET: return "IN";
97   case GNUNET_DNSPARSER_CLASS_CHAOS: return "CHAOS";
98   case GNUNET_DNSPARSER_CLASS_HESIOD: return "HESIOD";
99   }
100   GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) class);
101   return buf;
102 }
103
104
105 /**
106  * Output the given DNS query to stdout.
107  *
108  * @param query query to display.
109  */
110 static void
111 display_query (const struct GNUNET_DNSPARSER_Query *query)
112 {
113   fprintf (stdout,
114            "\t\t%s %s: %s\n",
115            get_class (query->class),
116            get_type (query->type),
117            query->name);
118 }
119
120
121 /**
122  * Output the given DNS query to stdout.
123  *
124  * @param query query to display.
125  */
126 static void
127 display_record (const struct GNUNET_DNSPARSER_Record *record)
128 {
129   const char *format;
130   char buf[INET6_ADDRSTRLEN];
131   char *tmp;
132   
133   tmp = NULL;
134   switch (record->type)
135   {
136   case GNUNET_DNSPARSER_TYPE_A:
137     if (record->data_len != sizeof (struct in_addr))
138       format = "<invalid>";
139     else
140       format = inet_ntop (AF_INET, record->data.raw, buf, sizeof (buf));
141     break;
142   case GNUNET_DNSPARSER_TYPE_AAAA:
143     if (record->data_len != sizeof (struct in6_addr))
144       format = "<invalid>";
145     else
146       format = inet_ntop (AF_INET6, record->data.raw, buf, sizeof (buf));
147     break;
148   case GNUNET_DNSPARSER_TYPE_NS:
149   case GNUNET_DNSPARSER_TYPE_CNAME:
150   case GNUNET_DNSPARSER_TYPE_PTR:
151     format = record->data.hostname;
152     break;
153   case GNUNET_DNSPARSER_TYPE_SOA:
154     if (record->data.soa == NULL)
155       format = "<invalid>";
156     else
157     {
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);          
167       format = tmp;
168     }
169     break;
170   case GNUNET_DNSPARSER_TYPE_MX:
171     if (record->data.mx == NULL)
172       format = "<invalid>";
173     else
174     {
175       GNUNET_asprintf (&tmp,
176                        "%u: %s",
177                        record->data.mx->preference,
178                        record->data.mx->mxhost);
179       format = tmp;
180     }
181     break;
182   case GNUNET_DNSPARSER_TYPE_TXT:
183     GNUNET_asprintf (&tmp,
184                      "%.*s",
185                      (unsigned int) record->data_len,
186                      record->data.raw);
187     format = tmp;
188     break;
189   default:
190     format = "<payload>";
191     break;
192   }
193   fprintf (stdout,
194            "\t\t%s %s: %s = %s (%u bytes, %u s)\n",
195            get_class (record->class),
196            get_type (record->type),
197            record->name,
198            format,
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);
202 }
203
204
205 /**
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.
210  *
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.
215  *
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.
222  *
223  * @param cls closure
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
227  */
228 static void 
229 display_request (void *cls,
230                  struct GNUNET_DNS_RequestHandle *rh,
231                  size_t request_length,
232                  const char *request)
233 {
234   static const char *return_codes[] =
235     {
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>"
240     };
241   static const char *op_codes[] =
242     {
243       "Query", "Inverse query", "Status", "<invalid>",
244       "<invalid>", "<invalid>", "<invalid>", "<invalid>",
245       "<invalid>", "<invalid>", "<invalid>", "<invalid>",
246       "<invalid>", "<invalid>", "<invalid>", "<invalid>"
247     };
248   struct GNUNET_DNSPARSER_Packet *p;
249   unsigned int i;
250
251   p = GNUNET_DNSPARSER_parse (request, request_length);
252   if (NULL == p)
253   {
254     fprintf (stderr, "Received malformed DNS packet!\n");
255     // FIXME: drop instead?
256     GNUNET_DNS_request_forward (rh);
257     return;
258   }
259   fprintf (stdout,
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",
262            p->id,
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)
272     fprintf (stdout,
273              "\tQueries:\n");
274   for (i=0;i<p->num_queries;i++)
275     display_query (&p->queries[i]);
276   
277   if (p->num_answers > 0)
278     fprintf (stdout,
279              "\tAnswers:\n");
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);
285 }
286
287
288 /**
289  * Shutdown.
290  */
291 static void
292 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
293 {
294   if (NULL != handle)
295   {
296     GNUNET_DNS_disconnect (handle);
297     handle = NULL;
298   }
299 }
300
301
302 /**
303  * Main function that will be run by the scheduler.
304  *
305  * @param cls closure
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
309  */
310 static void
311 run (void *cls, char *const *args, const char *cfgfile,
312      const struct GNUNET_CONFIGURATION_Handle *cfg)
313 {
314   enum GNUNET_DNS_Flags flags;
315
316   flags = GNUNET_DNS_FLAG_REQUEST_MONITOR | GNUNET_DNS_FLAG_RESPONSE_MONITOR;
317   if (inbound_only | outbound_only)
318     flags = 0;
319   if (inbound_only)
320     flags |= GNUNET_DNS_FLAG_REQUEST_MONITOR;
321   if (outbound_only)
322     flags |= GNUNET_DNS_FLAG_RESPONSE_MONITOR;
323   handle =
324     GNUNET_DNS_connect (cfg, 
325                         flags,
326                         &display_request,
327                         NULL);
328   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
329                                 &do_disconnect, NULL);
330 }
331
332
333 int
334 main (int argc, char *const *argv)
335 {
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
345   };
346   return (GNUNET_OK ==
347           GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-monitor",
348                               gettext_noop
349                               ("Monitor DNS queries."), options,
350                               &run, NULL)) ? ret : 1;
351 }
352
353
354 /* end of gnunet-dns-monitor.c */