-typo
[oweals/gnunet.git] / src / gns / gnunet-dns2gns.c
index de3ede6e231d2064caac7723658539e1329514a4..ec64f0730cfdb14c68e6c831a1f32ed2e08a18df 100644 (file)
@@ -120,12 +120,12 @@ static GNUNET_SCHEDULER_TaskIdentifier t4;
 static GNUNET_SCHEDULER_TaskIdentifier t6;
 
 /**
- * DNS suffix
+ * DNS suffix, suffix of this gateway in DNS; defaults to '.zkey.eu'
  */
 static char *dns_suffix;
 
 /**
- * FCFS suffix
+ * FCFS suffix, suffix of FCFS-authority in DNS; defaults to 'fcfs.zkey.eu'.
  */
 static char *fcfs_suffix;
 
@@ -134,6 +134,12 @@ static char *fcfs_suffix;
  */
 static char *dns_ip;
 
+/**
+ * UDP Port we listen on for inbound DNS requests.
+ */
+static unsigned int listen_port = 53;
+
+
 /**
  * Task run on shutdown.  Cleans up everything.
  *
@@ -217,6 +223,8 @@ do_timeout (void *cls,
     GNUNET_DNSPARSER_free_packet (request->packet);
   if (NULL != request->lookup)
     GNUNET_GNS_cancel_lookup_request (request->lookup);
+  if (NULL != request->dns_lookup)
+    GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
   GNUNET_free (request);
 }
 
@@ -237,10 +245,12 @@ dns_result_processor (void *cls,
                   size_t r)
 {
   struct Request *request = cls;
+
   request->packet = GNUNET_DNSPARSER_parse ((char*)dns, r);
   send_response (request);
 }
 
+
 /**
  * Iterator called on obtained result for a GNS
  * lookup
@@ -343,9 +353,12 @@ handle_request (struct GNUNET_NETWORK_Handle *lsock,
   struct Request *request;
   struct GNUNET_DNSPARSER_Packet *packet;
   char *name;
+  char *dot;
+  char *nname;
   size_t name_len;
   enum GNUNET_GNS_RecordType type;
   int use_gns;
+  struct GNUNET_CRYPTO_ShortHashCode zone;
 
   packet = GNUNET_DNSPARSER_parse (udp_msg, udp_msg_size);
   if (NULL == packet)
@@ -356,8 +369,8 @@ handle_request (struct GNUNET_NETWORK_Handle *lsock,
       return;
     }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Received request for %s with flags %u, #answers %d, #auth %d, #additional %d\n",
-        packet->queries[0].name,
+             "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n",
+             packet->queries[0].name,
              (unsigned int) packet->flags.query_or_response,
              (int) packet->num_answers,
              (int) packet->num_authority_records,
@@ -396,51 +409,30 @@ handle_request (struct GNUNET_NETWORK_Handle *lsock,
        (0 == strcasecmp (dns_suffix,
                         &name[name_len - strlen (dns_suffix)])) )
     {
-      /*
-       * FIXME: Here we want to query fcfs. But what is fcfs in our
-       * context?? -> we need a PKEY or name
-       */
-      if ( (name_len >= strlen (fcfs_suffix)) &&
-          (0 == strcasecmp (fcfs_suffix,
-                            &name[name_len - strlen (fcfs_suffix)])) )
+      /* Test if '.zkey' was requested */
+      name[name_len - strlen (dns_suffix)] = '\0';
+      dot = strrchr (name, (int) '.');
+      if ( (NULL != dot) &&
+          (GNUNET_OK ==
+           GNUNET_CRYPTO_short_hash_from_string (dot + 1, &zone)) )
       {
-        name[name_len - strlen (fcfs_suffix)] = '\0';
-        if (0 == strcmp (name, ""))
-          strcpy (name, GNUNET_GNS_TLD);
-        else
-        {
-          if (sizeof (name) < (strlen (GNUNET_GNS_TLD)+strlen (name)))
-          {
-            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                        "Name %s.%s is too long!\n",
-                        name,
-                        GNUNET_GNS_TLD);
-            GNUNET_DNSPARSER_free_packet (request->packet);
-            return;
-          }
-          sprintf (name, "%s.%s", name, GNUNET_GNS_TLD);
-        }
+       /* valid '.zkey' name */
+       GNUNET_asprintf (&nname, 
+                        "%s.%s", 
+                        name, 
+                        GNUNET_GNS_TLD_ZKEY);
+       GNUNET_free (name);
+       name = nname;
       }
       else
-      {
-        name[name_len - strlen (dns_suffix)] = '\0';
-        if (0 == strcmp (name, ""))
-        {
-          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                      "No zkey given!\n");
-          GNUNET_DNSPARSER_free_packet (request->packet);
-          return;
-        }
-        if (sizeof (name) < (strlen (GNUNET_GNS_TLD_ZKEY)+strlen (name)))
-        {
-          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                      "Name %s.%s is too long!\n",
-                      name,
-                      GNUNET_GNS_TLD);
-          GNUNET_DNSPARSER_free_packet (request->packet);
-          return;
-        }
-        sprintf (name, "%s.%s", name, GNUNET_GNS_TLD_ZKEY);
+      {        
+       /* try '.gads' name */
+       GNUNET_asprintf (&nname, 
+                        "%s.%s", 
+                        name, 
+                        GNUNET_GNS_TLD);
+       GNUNET_free (name);
+       name = nname;
       }
       name_len = strlen (name);
     }
@@ -612,11 +604,12 @@ run (void *cls, char *const *args, const char *cfgfile,
 #if HAVE_SOCKADDR_IN_SIN_LEN
       v4.sin_len = sizeof (v4);
 #endif
-      v4.sin_port = htons (53);
+      v4.sin_port = htons (listen_port);
       if (GNUNET_OK !=
          GNUNET_NETWORK_socket_bind (listen_socket4,
                                      (struct sockaddr *) &v4,
-                                     sizeof (v4)))
+                                     sizeof (v4),
+                                      0))
        {
          GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
          GNUNET_NETWORK_socket_close (listen_socket4);
@@ -635,11 +628,12 @@ run (void *cls, char *const *args, const char *cfgfile,
 #if HAVE_SOCKADDR_IN_SIN_LEN
       v6.sin6_len = sizeof (v6);
 #endif
-      v6.sin6_port = htons (53);
+      v6.sin6_port = htons (listen_port);
       if (GNUNET_OK !=
          GNUNET_NETWORK_socket_bind (listen_socket6,
                                      (struct sockaddr *) &v6,
-                                     sizeof (v6)))
+                                     sizeof (v6),
+                                      0))
        {
          GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
          GNUNET_NETWORK_socket_close (listen_socket6);
@@ -683,15 +677,18 @@ main (int argc,
       char *const *argv)
 {
   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
-    {'d', "dns", NULL,
-      gettext_noop ("IP of recursive dns resolver to use (required)"), 1,
+    {'d', "dns", "IP",
+      gettext_noop ("IP of recursive DNS resolver to use (required)"), 1,
       &GNUNET_GETOPT_set_string, &dns_ip},
-    {'s', "suffix", NULL,
+    {'s', "suffix", "SUFFIX",
       gettext_noop ("Authoritative DNS suffix to use (optional); default: zkey.eu"), 1,
       &GNUNET_GETOPT_set_string, &dns_suffix},
-    {'f', "fcfs", NULL,
+    {'f', "fcfs", "NAME",
       gettext_noop ("Authoritative FCFS suffix to use (optional); default: fcfs.zkey.eu"), 1,
       &GNUNET_GETOPT_set_string, &fcfs_suffix},
+    {'p', "port", "UDPPORT",
+      gettext_noop ("UDP port to listen on for inbound DNS requests; default: 53"), 1,
+      &GNUNET_GETOPT_set_uint, &listen_port},
     GNUNET_GETOPT_OPTION_END
   };
   int ret;
@@ -706,7 +703,7 @@ main (int argc,
                            _("GNUnet DNS-to-GNS proxy (a DNS server)"), 
                           options,
                            &run, NULL)) ? 0 : 1;
-
+  GNUNET_free ((void*) argv);
   return ret;
 }