small API change: do no longer pass rarely needed GNUNET_SCHEDULER_TaskContext to...
[oweals/gnunet.git] / src / gns / gnunet-dns2gns.c
index 70c791de4f96b37d19fc7777850d3a6d59b854bf..119bda91a75be2305c72e508ce4c5a8b8470bc0e 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2012-2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2012-2013 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.
 */
 /**
  * @file gnunet-dns2gns.c
@@ -65,7 +65,7 @@ struct Request
    * converted to the DNS response.
    */
   struct GNUNET_DNSPARSER_Packet *packet;
-  
+
   /**
    * Our GNS request handle.
    */
@@ -80,11 +80,11 @@ struct Request
    * Task run on timeout or shutdown to clean up without
    * response.
    */
-  GNUNET_SCHEDULER_TaskIdentifier timeout_task;
+  struct GNUNET_SCHEDULER_Task * timeout_task;
 
   /**
    * Number of bytes in 'addr'.
-   */ 
+   */
   size_t addr_len;
 
 };
@@ -113,12 +113,12 @@ static struct GNUNET_NETWORK_Handle *listen_socket6;
 /**
  * Task for IPv4 socket.
  */
-static GNUNET_SCHEDULER_TaskIdentifier t4;
+static struct GNUNET_SCHEDULER_Task * t4;
 
 /**
  * Task for IPv6 socket.
  */
-static GNUNET_SCHEDULER_TaskIdentifier t6;
+static struct GNUNET_SCHEDULER_Task * t6;
 
 /**
  * DNS suffix, suffix of this gateway in DNS; defaults to '.zkey.eu'
@@ -138,12 +138,12 @@ static char *dns_ip;
 /**
  * UDP Port we listen on for inbound DNS requests.
  */
-static unsigned int listen_port = 53;
+static unsigned int listen_port = 2853;
 
 /**
  * Which GNS zone do we translate incoming DNS requests to?
  */
-static struct GNUNET_CRYPTO_EccPublicSignKey my_zone;
+static struct GNUNET_CRYPTO_EcdsaPublicKey my_zone;
 
 /**
  * '-z' option with the main zone to use.
@@ -166,20 +166,17 @@ static struct GNUNET_IDENTITY_Handle *identity;
 static struct GNUNET_IDENTITY_Operation *id_op;
 
 
-
 /**
  * Task run on shutdown.  Cleans up everything.
  *
  * @param cls unused
- * @param tc scheduler context
  */
 static void
-do_shutdown (void *cls,
-            const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_shutdown (void *cls)
 {
-  if (GNUNET_SCHEDULER_NO_TASK != t4)
+  if (NULL != t4)
     GNUNET_SCHEDULER_cancel (t4);
-  if (GNUNET_SCHEDULER_NO_TASK != t6)
+  if (NULL != t6)
     GNUNET_SCHEDULER_cancel (t6);
   if (NULL != listen_socket4)
   {
@@ -201,10 +198,16 @@ do_shutdown (void *cls,
     GNUNET_IDENTITY_disconnect (identity);
     identity = NULL;
   }
-  GNUNET_GNS_disconnect (gns);
-  gns = NULL;
-  GNUNET_DNSSTUB_stop (dns_stub);
-  dns_stub = NULL;
+  if (NULL != gns)
+  {
+    GNUNET_GNS_disconnect (gns);
+    gns = NULL;
+  }
+  if (NULL != dns_stub)
+  {
+    GNUNET_DNSSTUB_stop (dns_stub);
+    dns_stub = NULL;
+  }
 }
 
 
@@ -218,7 +221,7 @@ send_response (struct Request *request)
 {
   char *buf;
   size_t size;
-  
+
   if (GNUNET_SYSERR ==
       GNUNET_DNSPARSER_pack (request->packet,
                             UINT16_MAX /* is this not too much? */,
@@ -247,12 +250,10 @@ send_response (struct Request *request)
 /**
  * Task run on timeout.  Cleans up request.
  *
- * @param cls 'struct Request' of the request to clean up
- * @param tc scheduler context
+ * @param cls `struct Request *` of the request to clean up
  */
 static void
-do_timeout (void *cls,
-           const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_timeout (void *cls)
 {
   struct Request *request = cls;
 
@@ -297,7 +298,7 @@ dns_result_processor (void *cls,
 static void
 result_processor (void *cls,
                  uint32_t rd_count,
-                 const struct GNUNET_NAMESTORE_RecordData *rd)
+                 const struct GNUNET_GNSRECORD_Data *rd)
 {
   struct Request *request = cls;
   struct GNUNET_DNSPARSER_Packet *packet;
@@ -317,7 +318,7 @@ result_processor (void *cls,
   //packet->flags.opcode = GNUNET_TUN_DNS_OPCODE_STATUS; // ???
   for (i=0;i<rd_count;i++)
     {
-      // FIXME: do we need to hanlde #GNUNET_NAMESTORE_RF_SHADOW_RECORD
+      // FIXME: do we need to hanlde #GNUNET_GNSRECORD_RF_SHADOW_RECORD
       // here? Or should we do this in libgnunetgns?
       rec.expiration_time.abs_value_us = rd[i].expiration_time;
       switch (rd[i].record_type)
@@ -325,7 +326,7 @@ result_processor (void *cls,
        case GNUNET_DNSPARSER_TYPE_A:
          GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
          rec.name = GNUNET_strdup (packet->queries[0].name);
-         rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
+         rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
          rec.type = GNUNET_DNSPARSER_TYPE_A;
          rec.data.raw.data = GNUNET_new (struct in_addr);
          memcpy (rec.data.raw.data,
@@ -339,8 +340,8 @@ result_processor (void *cls,
        case GNUNET_DNSPARSER_TYPE_AAAA:
          GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
          rec.name = GNUNET_strdup (packet->queries[0].name);
-         rec.data.raw.data = GNUNET_malloc (sizeof (struct in6_addr));
-         rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
+         rec.data.raw.data = GNUNET_new (struct in6_addr);
+         rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
          rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
          memcpy (rec.data.raw.data,
                  rd[i].data,
@@ -352,8 +353,8 @@ result_processor (void *cls,
          break;
        case GNUNET_DNSPARSER_TYPE_CNAME:
          rec.name = GNUNET_strdup (packet->queries[0].name);
-         rec.data.hostname = strdup (rd[i].data);
-         rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
+         rec.data.hostname = GNUNET_strdup (rd[i].data);
+         rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
          rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
          memcpy (rec.data.hostname,
                  rd[i].data,
@@ -378,7 +379,7 @@ result_processor (void *cls,
  * @param addr address to use for sending the reply
  * @param addr_len number of bytes in @a addr
  * @param udp_msg DNS request payload
- * @param udp_msg_size number of bytes in @a udp_msg 
+ * @param udp_msg_size number of bytes in @a udp_msg
  */
 static void
 handle_request (struct GNUNET_NETWORK_Handle *lsock,
@@ -409,7 +410,7 @@ handle_request (struct GNUNET_NETWORK_Handle *lsock,
              (int) packet->num_answers,
              (int) packet->num_authority_records,
              (int) packet->num_additional_records);
-  if ( (0 != packet->flags.query_or_response) || 
+  if ( (0 != packet->flags.query_or_response) ||
        (0 != packet->num_answers) ||
        (0 != packet->num_authority_records))
     {
@@ -440,7 +441,7 @@ handle_request (struct GNUNET_NETWORK_Handle *lsock,
   name_len = strlen (name);
   use_gns = GNUNET_NO;
 
-  
+
   if ( (name_len > strlen (fcfs_suffix)) &&
        (0 == strcasecmp (fcfs_suffix,
                         &name[name_len - strlen (fcfs_suffix)])) )
@@ -449,9 +450,10 @@ handle_request (struct GNUNET_NETWORK_Handle *lsock,
     strcpy (&name[name_len - strlen (fcfs_suffix)],
            ".gnu");
     use_gns = GNUNET_YES;
-  } else if ( (name_len > strlen (dns_suffix)) &&
-       (0 == strcasecmp (dns_suffix,
-                        &name[name_len - strlen (dns_suffix)])) )
+  }
+  else if ( (name_len > strlen (dns_suffix)) &&
+            (0 == strcasecmp (dns_suffix,
+                              &name[name_len - strlen (dns_suffix)])) )
   {
     /* replace ".zkey.eu" with ".zkey" */
     strcpy (&name[name_len - strlen (dns_suffix)],
@@ -482,7 +484,7 @@ handle_request (struct GNUNET_NETWORK_Handle *lsock,
   else
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Using DNS resolver IP `%s' to resolve `%s'\n", 
+               "Using DNS resolver IP `%s' to resolve `%s'\n",
                dns_ip,
                name);
     GNUNET_DNSPARSER_free_packet (request->packet);
@@ -501,21 +503,21 @@ handle_request (struct GNUNET_NETWORK_Handle *lsock,
  * Task to read IPv4 DNS packets.
  *
  * @param cls the 'listen_socket4'
- * @param tc scheduler context
- */ 
+ */
 static void
-read_dns4 (void *cls,
-          const struct GNUNET_SCHEDULER_TaskContext *tc)
+read_dns4 (void *cls)
 {
   struct sockaddr_in v4;
   socklen_t addrlen;
   ssize_t size;
+  const struct GNUNET_SCHEDULER_TaskContext *tc;
 
   GNUNET_assert (listen_socket4 == cls);
   t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                      listen_socket4,
                                      &read_dns4,
                                      listen_socket4);
+  tc = GNUNET_SCHEDULER_get_task_context ();
   if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
     return; /* shutdown? */
   size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
@@ -526,9 +528,9 @@ read_dns4 (void *cls,
     }
   {
     char buf[size];
-    
+
     addrlen = sizeof (v4);
-    GNUNET_break (size == 
+    GNUNET_break (size ==
                  GNUNET_NETWORK_socket_recvfrom (listen_socket4,
                                                  buf,
                                                  size,
@@ -544,21 +546,21 @@ read_dns4 (void *cls,
  * Task to read IPv6 DNS packets.
  *
  * @param cls the 'listen_socket6'
- * @param tc scheduler context
- */ 
+ */
 static void
-read_dns6 (void *cls,
-          const struct GNUNET_SCHEDULER_TaskContext *tc)
+read_dns6 (void *cls)
 {
   struct sockaddr_in6 v6;
   socklen_t addrlen;
   ssize_t size;
+  const struct GNUNET_SCHEDULER_TaskContext *tc;
 
   GNUNET_assert (listen_socket6 == cls);
   t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                      listen_socket6,
                                      &read_dns6,
                                      listen_socket6);
+  tc = GNUNET_SCHEDULER_get_task_context ();
   if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
     return; /* shutdown? */
   size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
@@ -569,9 +571,9 @@ read_dns6 (void *cls,
     }
   {
     char buf[size];
-    
+
     addrlen = sizeof (v6);
-    GNUNET_break (size == 
+    GNUNET_break (size ==
                  GNUNET_NETWORK_socket_recvfrom (listen_socket6,
                                                  buf,
                                                  size,
@@ -602,7 +604,7 @@ run_dnsd ()
     return;
   }
   listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
-                                                SOCK_DGRAM, 
+                                                SOCK_DGRAM,
                                                 IPPROTO_UDP);
   if (NULL != listen_socket4)
     {
@@ -617,8 +619,7 @@ run_dnsd ()
       if (GNUNET_OK !=
          GNUNET_NETWORK_socket_bind (listen_socket4,
                                      (struct sockaddr *) &v4,
-                                     sizeof (v4),
-                                      0))
+                                     sizeof (v4)))
        {
          GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
          GNUNET_NETWORK_socket_close (listen_socket4);
@@ -626,7 +627,7 @@ run_dnsd ()
        }
     }
   listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
-                                               SOCK_DGRAM, 
+                                               SOCK_DGRAM,
                                                IPPROTO_UDP);
   if (NULL != listen_socket6)
     {
@@ -641,8 +642,7 @@ run_dnsd ()
       if (GNUNET_OK !=
          GNUNET_NETWORK_socket_bind (listen_socket6,
                                      (struct sockaddr *) &v6,
-                                     sizeof (v6),
-                                      0))
+                                     sizeof (v6)))
        {
          GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
          GNUNET_NETWORK_socket_close (listen_socket6);
@@ -672,7 +672,7 @@ run_dnsd ()
 }
 
 
-/** 
+/**
  * Method called to inform about the egos of this peer.
  *
  * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
@@ -680,7 +680,7 @@ run_dnsd ()
  * @a ego does indicate an error (i.e. name is taken or no default
  * value is known).  If @a ego is non-NULL and if '*ctx'
  * is set in those callbacks, the value WILL be passed to a subsequent
- * call to the identity callback of #GNUNET_IDENTITY_connect (if 
+ * call to the identity callback of #GNUNET_IDENTITY_connect (if
  * that one was not NULL).
  *
  * @param cls closure, NULL
@@ -744,11 +744,11 @@ run (void *cls, char *const *args, const char *cfgfile,
     }
   if ( (NULL == gns_zone_str) ||
        (GNUNET_OK !=
-       GNUNET_CRYPTO_ecc_public_sign_key_from_string (gns_zone_str,
+       GNUNET_CRYPTO_ecdsa_public_key_from_string (gns_zone_str,
                                                  strlen (gns_zone_str),
                                                  &my_zone)) )
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("No valid GNS zone specified!\n"));
     GNUNET_SCHEDULER_shutdown ();
     return;
@@ -765,7 +765,7 @@ run (void *cls, char *const *args, const char *cfgfile,
  * @return 0 ok, 1 on error
  */
 int
-main (int argc, 
+main (int argc,
       char *const *argv)
 {
   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
@@ -779,7 +779,7 @@ main (int argc,
       gettext_noop ("Authoritative DNS suffix to use (optional); default: zkey.eu"), 1,
       &GNUNET_GETOPT_set_string, &dns_suffix},
     {'p', "port", "UDPPORT",
-      gettext_noop ("UDP port to listen on for inbound DNS requests; default: 53"), 1,
+      gettext_noop ("UDP port to listen on for inbound DNS requests; default: 2853"), 1,
       &GNUNET_GETOPT_set_uint, &listen_port},
     {'z', "zone", "PUBLICKEY",
       gettext_noop ("Public key of the GNS zone to use (overrides default)"), 1,
@@ -795,7 +795,7 @@ main (int argc,
   ret =
       (GNUNET_OK ==
        GNUNET_PROGRAM_run (argc, argv, "gnunet-dns2gns",
-                           _("GNUnet DNS-to-GNS proxy (a DNS server)"), 
+                           _("GNUnet DNS-to-GNS proxy (a DNS server)"),
                           options,
                            &run, NULL)) ? 0 : 1;
   GNUNET_free ((void*) argv);