Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / dns / gnunet-dns-monitor.c
index c8fb646e562e150e9edc6815873feb226c34e948..fb5c768ac2a8ab37a43a6b4b1d3a12599a8d57aa 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2011 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -26,7 +26,7 @@
 
 #include "platform.h"
 #include "gnunet_util_lib.h"
-#include "gnunet_dns_service-new.h"
+#include "gnunet_dns_service.h"
 #include "gnunet_dnsparser_lib.h"
 
 /**
 static struct GNUNET_DNS_Handle *handle;
 
 /**
- * Option -s.
+ * Option -i.
  */
-static int benchmark_send;
+static int inbound_only;
+
+/**
+ * Option -o.
+ */
+static int outbound_only;
 
 /**
  * Global return value (0 success).
@@ -47,7 +52,7 @@ static int ret;
 /**
  * Selected level of verbosity.
  */
-static int verbosity;
+static unsigned int verbosity;
 
 
 /**
@@ -70,8 +75,7 @@ get_type (uint16_t type)
   case GNUNET_DNSPARSER_TYPE_MX: return "MX";
   case GNUNET_DNSPARSER_TYPE_TXT: return "TXT";
   case GNUNET_DNSPARSER_TYPE_AAAA: return "AAAA";
-  case GNUNET_DNSPARSER_TYPE_IXFR: return "IXFR";
-  case GNUNET_DNSPARSER_TYPE_AXFR: return "AXFR";
+  case GNUNET_DNSPARSER_TYPE_SRV: return "SRV";
   }
   GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) type);
   return buf;
@@ -90,9 +94,9 @@ get_class (uint16_t class)
   static char buf[6];
   switch (class)
   {
-  case GNUNET_DNSPARSER_CLASS_INTERNET: return "IN";
-  case GNUNET_DNSPARSER_CLASS_CHAOS: return "CHAOS";
-  case GNUNET_DNSPARSER_CLASS_HESIOD: return "HESIOD";
+  case GNUNET_TUN_DNS_CLASS_INTERNET: return "IN";
+  case GNUNET_TUN_DNS_CLASS_CHAOS: return "CHAOS";
+  case GNUNET_TUN_DNS_CLASS_HESIOD: return "HESIOD";
   }
   GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) class);
   return buf;
@@ -109,16 +113,16 @@ display_query (const struct GNUNET_DNSPARSER_Query *query)
 {
   fprintf (stdout,
           "\t\t%s %s: %s\n",
-          get_class (query->class),
+          get_class (query->dns_traffic_class),
           get_type (query->type),
           query->name);
 }
 
 
 /**
- * Output the given DNS query to stdout.
+ * Output the given DNS record to stdout.
  *
- * @param query query to display.
+ * @param record record to display.
  */
 static void
 display_record (const struct GNUNET_DNSPARSER_Record *record)
@@ -126,36 +130,88 @@ display_record (const struct GNUNET_DNSPARSER_Record *record)
   const char *format;
   char buf[INET6_ADDRSTRLEN];
   char *tmp;
-  
+
   tmp = NULL;
   switch (record->type)
   {
   case GNUNET_DNSPARSER_TYPE_A:
-    if (record->data_len != sizeof (struct in_addr))
+    if (record->data.raw.data_len != sizeof (struct in_addr))
       format = "<invalid>";
     else
-      format = inet_ntop (AF_INET, record->data, buf, sizeof (buf));
+      format = inet_ntop (AF_INET, record->data.raw.data, buf, sizeof (buf));
     break;
   case GNUNET_DNSPARSER_TYPE_AAAA:
-    if (record->data_len != sizeof (struct in6_addr))
+    if (record->data.raw.data_len != sizeof (struct in6_addr))
       format = "<invalid>";
     else
-      format = inet_ntop (AF_INET6, record->data, buf, sizeof (buf));
+      format = inet_ntop (AF_INET6, record->data.raw.data, buf, sizeof (buf));
     break;
+  case GNUNET_DNSPARSER_TYPE_NS:
   case GNUNET_DNSPARSER_TYPE_CNAME:
-    tmp = GNUNET_strdup ("FIXME");
+  case GNUNET_DNSPARSER_TYPE_PTR:
+    format = record->data.hostname;
+    break;
+  case GNUNET_DNSPARSER_TYPE_SOA:
+    if (NULL == record->data.soa)
+      format = "<invalid>";
+    else
+    {
+      GNUNET_asprintf (&tmp,
+                      "origin: %s, mail: %s, serial = %u, refresh = %u s, retry = %u s, expire = %u s, minimum = %u s",
+                      record->data.soa->mname,
+                      record->data.soa->rname,
+                      (unsigned int) record->data.soa->serial,
+                      (unsigned int) record->data.soa->refresh,
+                      (unsigned int) record->data.soa->retry,
+                      (unsigned int) record->data.soa->expire,
+                      (unsigned int) record->data.soa->minimum_ttl);
+      format = tmp;
+    }
+    break;
+  case GNUNET_DNSPARSER_TYPE_MX:
+    if (record->data.mx == NULL)
+      format = "<invalid>";
+    else
+    {
+      GNUNET_asprintf (&tmp,
+                      "%u: %s",
+                      record->data.mx->preference,
+                      record->data.mx->mxhost);
+      format = tmp;
+    }
+    break;
+  case GNUNET_DNSPARSER_TYPE_SRV:
+    if (NULL == record->data.srv)
+      format = "<invalid>";
+    else
+    {
+      GNUNET_asprintf (&tmp,
+                      "priority %u, weight = %s, port = %u, target = %s",
+                      (unsigned int) record->data.srv->priority,
+                      (unsigned int) record->data.srv->weight,
+                      (unsigned int) record->data.srv->port,
+                      record->data.srv->target);
+      format = tmp;
+    }
+    break;
+  case GNUNET_DNSPARSER_TYPE_TXT:
+    GNUNET_asprintf (&tmp,
+                    "%.*s",
+                    (unsigned int) record->data.raw.data_len,
+                    record->data.raw.data);
+    format = tmp;
+    break;
   default:
     format = "<payload>";
     break;
   }
   fprintf (stdout,
-          "\t\t%s %s: %s = %s (%u bytes, %u s)\n",
-          get_class (record->class),
+          "\t\t%s %s: %s = %s (%u s)\n",
+          get_class (record->dns_traffic_class),
           get_type (record->type),
           record->name,
           format,
-          (unsigned int) record->data_len,
-          (unsigned int) (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value / 1000));
+          (unsigned int) (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL));
   GNUNET_free_non_null (tmp);
 }
 
@@ -183,7 +239,7 @@ display_record (const struct GNUNET_DNSPARSER_Record *record)
  * @param request_length number of bytes in request
  * @param request udp payload of the DNS request
  */
-static void 
+static void
 display_request (void *cls,
                 struct GNUNET_DNS_RequestHandle *rh,
                 size_t request_length,
@@ -215,7 +271,7 @@ display_request (void *cls,
     return;
   }
   fprintf (stdout,
-          "%s with ID: %5u Flags: %s%s%s%s%s%s Return Code: %s Opcode: %s\n",
+          "%s with ID: %5u Flags: %s%s%s%s%s%s, Return Code: %s, Opcode: %s\n",
           p->flags.query_or_response ? "Response" : "Query",
           p->id,
           p->flags.recursion_desired ? "RD " : "",
@@ -225,13 +281,13 @@ display_request (void *cls,
           p->flags.authenticated_data ? "AD " : "",
           p->flags.recursion_available ? "RA " : "",
           return_codes[p->flags.return_code & 15],
-          op_codes[p->flags.opcode & 15]);  
+          op_codes[p->flags.opcode & 15]);
   if (p->num_queries > 0)
     fprintf (stdout,
             "\tQueries:\n");
   for (i=0;i<p->num_queries;i++)
     display_query (&p->queries[i]);
-  
+
   if (p->num_answers > 0)
     fprintf (stdout,
             "\tAnswers:\n");
@@ -247,7 +303,7 @@ display_request (void *cls,
  * Shutdown.
  */
 static void
-do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_disconnect (void *cls)
 {
   if (NULL != handle)
   {
@@ -269,31 +325,52 @@ static void
 run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
+  enum GNUNET_DNS_Flags flags;
+
+  flags = GNUNET_DNS_FLAG_REQUEST_MONITOR | GNUNET_DNS_FLAG_RESPONSE_MONITOR;
+  if (inbound_only | outbound_only)
+    flags = 0;
+  if (inbound_only)
+    flags |= GNUNET_DNS_FLAG_REQUEST_MONITOR;
+  if (outbound_only)
+    flags |= GNUNET_DNS_FLAG_RESPONSE_MONITOR;
   handle =
-    GNUNET_DNS_connect (cfg, 
-                       GNUNET_DNS_FLAG_REQUEST_MONITOR | GNUNET_DNS_FLAG_RESPONSE_MONITOR,
+    GNUNET_DNS_connect (cfg,
+                       flags,
                        &display_request,
                        NULL);
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                               &do_disconnect, NULL);
+  GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL);
 }
 
 
 int
 main (int argc, char *const *argv)
 {
-  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
-    {'s', "testoption", NULL,
-     gettext_noop ("not useful"),
-     0, &GNUNET_GETOPT_set_one, &benchmark_send},
-    GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
+  struct GNUNET_GETOPT_CommandLineOption options[] = {
+
+    GNUNET_GETOPT_option_flag ('i',
+                                  "inbound-only",
+                                  gettext_noop ("only monitor DNS queries"),
+                                  &inbound_only),
+
+    GNUNET_GETOPT_option_flag ('o',
+                                  "outbound-only",
+                                  gettext_noop ("only monitor DNS queries"),
+                                  &outbound_only),
+
+    GNUNET_GETOPT_option_verbose (&verbosity),
     GNUNET_GETOPT_OPTION_END
   };
-  return (GNUNET_OK ==
-          GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-monitor",
-                              gettext_noop
-                              ("Monitor DNS queries."), options,
-                              &run, NULL)) ? ret : 1;
+
+  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
+    return 2;
+  ret = (GNUNET_OK ==
+        GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-monitor",
+                            gettext_noop
+                            ("Monitor DNS queries."), options,
+                            &run, NULL)) ? ret : 1;
+  GNUNET_free ((void*) argv);
+  return ret;
 }