update tests to use new MQ API
[oweals/gnunet.git] / src / dns / gnunet-service-dns.c
index ded21605a9d6cc3e4bd8604510b97e5fb09cc254..b580f433fc851872a83b400c26758eb0e09bc4ec 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2012 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2012 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.
 */
 
 /**
@@ -219,7 +219,7 @@ static struct GNUNET_HELPER_Handle *hijacker;
 /**
  * Command-line arguments we are giving to the hijacker process.
  */
-static char *helper_argv[7];
+static char *helper_argv[8];
 
 /**
  * Head of DLL of clients we consult.
@@ -273,17 +273,18 @@ cleanup_rr (struct RequestRecord *rr)
  * Task run during shutdown.
  *
  * @param cls unused
- * @param tc unused
  */
 static void
-cleanup_task (void *cls GNUNET_UNUSED,
-              const struct GNUNET_SCHEDULER_TaskContext *tc)
+cleanup_task (void *cls GNUNET_UNUSED)
 {
   unsigned int i;
 
-  GNUNET_HELPER_stop (hijacker, GNUNET_NO);
-  hijacker = NULL;
-  for (i=0;i<7;i++)
+  if (NULL != hijacker)
+  {
+    GNUNET_HELPER_stop (hijacker, GNUNET_NO);
+    hijacker = NULL;
+  }
+  for (i=0;i<8;i++)
     GNUNET_free_non_null (helper_argv[i]);
   for (i=0;i<=UINT16_MAX;i++)
     cleanup_rr (&requests[i]);
@@ -743,7 +744,7 @@ handle_client_init (void *cls GNUNET_UNUSED,
   struct ClientRecord *cr;
   const struct GNUNET_DNS_Register *reg = (const struct GNUNET_DNS_Register*) message;
 
-  cr = GNUNET_malloc (sizeof (struct ClientRecord));
+  cr = GNUNET_new (struct ClientRecord);
   cr->client = client;
   cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags);
   GNUNET_SERVER_client_keep (client);
@@ -1039,39 +1040,51 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   struct in6_addr dns_exit6;
   char *dns_exit;
   char *binary;
+  int nortsetup;
 
   cfg = cfg_;
-  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
-  if (GNUNET_YES !=
-      GNUNET_OS_check_helper_binary (binary, GNUNET_YES, NULL)) // TODO: once we have a windows-testcase, add test parameters here
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-               _("`%s' must be installed SUID, refusing to run\n"),
-               binary);
-    global_ret = 1;
-    GNUNET_free (binary);
-    return;
-  }
-  GNUNET_free (binary);
   stats = GNUNET_STATISTICS_create ("dns", cfg);
   nc = GNUNET_SERVER_notification_context_create (server, 1);
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
-                                cls);
+  GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
+                                cls);
   dns_exit = NULL;
   if ( ( (GNUNET_OK !=
-         GNUNET_CONFIGURATION_get_value_string (cfg, "dns",
+         GNUNET_CONFIGURATION_get_value_string (cfg,
+                                                 "dns",
                                                 "DNS_EXIT",
                                                 &dns_exit)) ||
         ( (1 != inet_pton (AF_INET, dns_exit, &dns_exit4)) &&
           (1 != inet_pton (AF_INET6, dns_exit, &dns_exit6)) ) ) )
   {
-    GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "dns", "DNS_EXIT",
+    GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
+                               "dns",
+                               "DNS_EXIT",
                               _("need a valid IPv4 or IPv6 address\n"));
     GNUNET_free_non_null (dns_exit);
     dns_exit = NULL;
   }
   dnsstub = GNUNET_DNSSTUB_start (dns_exit);
   GNUNET_free_non_null (dns_exit);
+  GNUNET_SERVER_add_handlers (server,
+                              handlers);
+  GNUNET_SERVER_disconnect_notify (server,
+                                   &client_disconnect,
+                                   NULL);
+  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
+  if (GNUNET_YES !=
+      GNUNET_OS_check_helper_binary (binary,
+                                     GNUNET_YES,
+                                     NULL)) // TODO: once we have a windows-testcase, add test parameters here
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+               _("`%s' must be installed SUID, will not run DNS interceptor\n"),
+               binary);
+    global_ret = 1;
+    GNUNET_free (binary);
+    return;
+  }
+  GNUNET_free (binary);
+
   helper_argv[0] = GNUNET_strdup ("gnunet-dns");
   if (GNUNET_SYSERR ==
       GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IFNAME", &ifc_name))
@@ -1123,14 +1136,20 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     return;
   }
   helper_argv[5] = ipv4mask;
-  helper_argv[6] = NULL;
+
+  nortsetup = GNUNET_CONFIGURATION_get_value_yesno (cfg, "dns",
+                                                     "SKIP_ROUTING_SETUP");
+  if (GNUNET_YES == nortsetup)
+    helper_argv[6] = GNUNET_strdup("1");
+  else
+    helper_argv[6] = GNUNET_strdup("0");
+
+  helper_argv[7] = NULL;
   hijacker = GNUNET_HELPER_start (GNUNET_NO,
                                  "gnunet-helper-dns",
                                  helper_argv,
                                  &process_helper_messages,
                                  NULL, NULL);
-  GNUNET_SERVER_add_handlers (server, handlers);
-  GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
 }
 
 
@@ -1159,7 +1178,7 @@ main (int argc, char *const *argv)
   }
   else if (sgid != rgid)
   {
-    if (-1 ==  setregid (sgid, sgid))
+    if (-1 == setregid (sgid, sgid))
       fprintf (stderr, "setregid failed: %s\n", strerror (errno));
   }
 #endif