fix uninit variable causing crash on 0 value
[oweals/gnunet.git] / src / gns / gnunet-bcd.c
index 05eaecff400c245ad91a71b6899b246ef3144c46..21471350d195833afa578384e0a1e1f6de1e1122 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 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.
 */
 
 /**
@@ -51,7 +51,7 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
 /**
  * Our primary task for the HTTPD.
  */
-static GNUNET_SCHEDULER_TaskIdentifier http_task;
+static struct GNUNET_SCHEDULER_Task * http_task;
 
 /**
  * Our main website.
@@ -90,10 +90,14 @@ struct Entry
  * Main request handler.
  */
 static int
-access_handler_callback (void *cls, struct MHD_Connection *connection,
-                         const char *url, const char *method,
-                         const char *version, const char *upload_data,
-                         size_t * upload_data_size, void **con_cls)
+access_handler_callback (void *cls,
+                        struct MHD_Connection *connection,
+                         const char *url,
+                        const char *method,
+                         const char *version,
+                        const char *upload_data,
+                         size_t * upload_data_size,
+                        void **con_cls)
 {
   static int dummy;
   static const struct Entry map[] = {
@@ -215,7 +219,7 @@ access_handler_callback (void *cls, struct MHD_Connection *connection,
       GNUNET_free (gpg1);
     }
     FPRINTF (f,
-             "\\def\\gns{gnunet://gns/%s/%s}\n",
+             "\\def\\gns{%s/%s}\n",
              gnskey,
              (NULL == gns_nick) ? "" : gns_nick);
     FCLOSE (f);
@@ -249,6 +253,10 @@ access_handler_callback (void *cls, struct MHD_Connection *connection,
     {
       GNUNET_break (0);
       GNUNET_break (0 == CLOSE (fd));
+      GNUNET_free (deffile);
+      GNUNET_free (p);
+      GNUNET_DISK_directory_remove (tmp);
+      GNUNET_free (tmp);
       return MHD_NO;
     }
     (void) MHD_add_response_header (response,
@@ -258,6 +266,7 @@ access_handler_callback (void *cls, struct MHD_Connection *connection,
                               MHD_HTTP_OK,
                               response);
     MHD_destroy_response (response);
+    GNUNET_free (deffile);
     GNUNET_free (p);
     GNUNET_DISK_directory_remove (tmp);
     GNUNET_free (tmp);
@@ -273,7 +282,7 @@ access_handler_callback (void *cls, struct MHD_Connection *connection,
  * Function that queries MHD's select sets and
  * starts the task waiting for them.
  */
-static GNUNET_SCHEDULER_TaskIdentifier
+static struct GNUNET_SCHEDULER_Task *
 prepare_daemon (struct MHD_Daemon *daemon_handle);
 
 
@@ -282,13 +291,11 @@ prepare_daemon (struct MHD_Daemon *daemon_handle);
  * and schedule the next run.
  */
 static void
-run_daemon (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+run_daemon (void *cls)
 {
   struct MHD_Daemon *daemon_handle = cls;
 
-  http_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
+  http_task = NULL;
   GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
   http_task = prepare_daemon (daemon_handle);
 }
@@ -298,10 +305,10 @@ run_daemon (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Function that queries MHD's select sets and
  * starts the task waiting for them.
  */
-static GNUNET_SCHEDULER_TaskIdentifier
+static struct GNUNET_SCHEDULER_Task *
 prepare_daemon (struct MHD_Daemon *daemon_handle)
 {
-  GNUNET_SCHEDULER_TaskIdentifier ret;
+  struct GNUNET_SCHEDULER_Task * ret;
   fd_set rs;
   fd_set ws;
   fd_set es;
@@ -321,7 +328,7 @@ prepare_daemon (struct MHD_Daemon *daemon_handle)
   GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
   haveto = MHD_get_timeout (daemon_handle, &timeout);
   if (haveto == MHD_YES)
-    tv.rel_value_us = (uint64_t) timeout;
+    tv.rel_value_us = (uint64_t) timeout * 1000LL;
   else
     tv = GNUNET_TIME_UNIT_FOREVER_REL;
   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
@@ -347,12 +354,12 @@ server_start ()
   if ((0 == port) || (port > UINT16_MAX))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Invalid port number %llu.  Exiting.\n"),
+                _("Invalid port number %u.  Exiting.\n"),
                 port);
     return GNUNET_SYSERR;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              _("Businesscard HTTP server starts on %llu\n"),
+              _("Businesscard HTTP server starts on %u\n"),
               port);
   daemon_handle = MHD_start_daemon (MHD_USE_DUAL_STACK | MHD_USE_DEBUG,
                                     (uint16_t) port,
@@ -360,7 +367,7 @@ server_start ()
                                     &access_handler_callback, NULL,
                                     MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 512,
                                     MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 2,
-                                    MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
+                                    MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 60,
                                     MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
                                     MHD_OPTION_END);
   if (NULL == daemon_handle)
@@ -379,15 +386,14 @@ server_start ()
  * Stop HTTP server.
  */
 static void
-server_stop (void *cls,
-             const struct GNUNET_SCHEDULER_TaskContext *tc)
+server_stop (void *cls)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "HTTP server shutdown\n");
-  if (GNUNET_SCHEDULER_NO_TASK != http_task)
+  if (NULL != http_task)
   {
     GNUNET_SCHEDULER_cancel (http_task);
-    http_task = GNUNET_SCHEDULER_NO_TASK;
+    http_task = NULL;
   }
   if (NULL != daemon_handle)
   {
@@ -493,9 +499,8 @@ run (void *cls,
   if (GNUNET_OK !=
       server_start ())
     return;
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                &server_stop,
-                                NULL);
+  GNUNET_SCHEDULER_add_shutdown (&server_stop,
+                                NULL);
 }