Update plibc header
[oweals/gnunet.git] / src / util / client.c
index 73b912cc6fd55d29f9b2088e23fee12be7437a32..5b753ec74103db2159621860137235777f9a667c 100644 (file)
@@ -1,10 +1,10 @@
 /*
      This file is part of GNUnet.
-     (C) 2001, 2002, 2006, 2008, 2009, 2012 Christian Grothoff (and other contributing authors)
+     (C) 2001-2013 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
@@ -251,8 +251,8 @@ try_unixpath (const char *service_name,
   struct sockaddr_un s_un;
 
   unixpath = NULL;
-  if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && 
-      (0 < strlen (unixpath)))     
+  if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) &&
+      (0 < strlen (unixpath)))
   {
     /* We have a non-NULL unixpath, need to validate it */
     if (strlen (unixpath) >= sizeof (s_un.sun_path))
@@ -263,6 +263,8 @@ try_unixpath (const char *service_name,
       unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
       LOG (GNUNET_ERROR_TYPE_INFO,
           _("Using `%s' instead\n"), unixpath);
+      if (NULL == unixpath)
+       return NULL;
     }
     connection = GNUNET_CONNECTION_create_from_connect_to_unixpath (cfg, unixpath);
     if (NULL != connection)
@@ -280,7 +282,8 @@ try_unixpath (const char *service_name,
 
 
 /**
- * Try connecting to the server using UNIX domain sockets.
+ * Test whether the configuration has proper values for connection
+ * (UNIXPATH || (PORT && HOSTNAME)).
  *
  * @param service_name name of service to connect to
  * @param cfg configuration to use
@@ -296,8 +299,8 @@ test_service_configuration (const char *service_name,
 #if AF_UNIX
   char *unixpath = NULL;
 
-  if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && 
-      (0 < strlen (unixpath)))     
+  if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) &&
+      (0 < strlen (unixpath)))
     ret = GNUNET_OK;
   GNUNET_free_non_null (unixpath);
 #endif
@@ -305,7 +308,7 @@ test_service_configuration (const char *service_name,
   if ( (GNUNET_YES ==
        GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT")) &&
        (GNUNET_OK ==
-       GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port)) && 
+       GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "PORT", &port)) &&
        (port <= 65535) && (0 != port) &&
        (GNUNET_OK ==
        GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "HOSTNAME",
@@ -403,12 +406,12 @@ GNUNET_CLIENT_connect (const char *service_name,
   struct GNUNET_CLIENT_Connection *client;
   struct GNUNET_CONNECTION_Handle *connection;
 
-  if (GNUNET_OK != 
+  if (GNUNET_OK !=
       test_service_configuration (service_name,
                                  cfg))
     return NULL;
   connection = do_connect (service_name, cfg, 0);
-  client = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
+  client = GNUNET_new (struct GNUNET_CLIENT_Connection);
   client->first_message = GNUNET_YES;
   client->attempts = 1;
   client->connection = connection;
@@ -535,7 +538,7 @@ receive_helper (void *cls, const void *buf, size_t available,
   check_complete (client);
   /* check for timeout */
   remaining = GNUNET_TIME_absolute_get_remaining (client->receive_timeout);
-  if (0 == remaining.rel_value)
+  if (0 == remaining.rel_value_us)
   {
     /* signal timeout! */
     if (NULL != client->receiver_handler)
@@ -626,7 +629,7 @@ GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *client,
 /**
  * Handle for a test to check if a service is running.
  */
-struct GNUNET_CLIENT_TestHandle 
+struct GNUNET_CLIENT_TestHandle
 {
   /**
    * Function to call with the result of the test.
@@ -640,7 +643,7 @@ struct GNUNET_CLIENT_TestHandle
 
   /**
    * Client connection we are using for the test, if any.
-   */ 
+   */
   struct GNUNET_CLIENT_Connection *client;
 
   /**
@@ -704,7 +707,7 @@ report_result (void *cls,
               const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_CLIENT_TestHandle *th = cls;
-  
+
   th->task = GNUNET_SCHEDULER_NO_TASK;
   th->cb (th->cb_cls, th->result);
   GNUNET_CLIENT_service_test_cancel (th);
@@ -723,7 +726,7 @@ service_test_report (struct GNUNET_CLIENT_TestHandle *th,
 {
   th->result = result;
   th->task = GNUNET_SCHEDULER_add_now (&report_result,
-                                      th);                                    
+                                      th);                             
 }
 
 
@@ -772,18 +775,18 @@ write_test (void *cls, size_t size, void *buf)
   th->th = NULL;
   if (size < sizeof (struct GNUNET_MessageHeader))
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, 
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
         "Failed to transmit TEST request.\n");
     service_test_report (th, GNUNET_NO);
     return 0;                   /* client disconnected */
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, 
-       "Transmitting `%s' request.\n", 
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Transmitting `%s' request.\n",
        "TEST");
   msg = (struct GNUNET_MessageHeader *) buf;
   msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
-  GNUNET_CLIENT_receive (th->client, 
+  GNUNET_CLIENT_receive (th->client,
                         &confirm_handler, th,
                          GNUNET_TIME_absolute_get_remaining
                          (th->test_deadline));
@@ -819,7 +822,7 @@ GNUNET_CLIENT_service_test (const char *service,
   th->cb = cb;
   th->cb_cls = cb_cls;
   th->test_deadline = GNUNET_TIME_relative_to_absolute (timeout);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, 
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Testing if service `%s' is running.\n",
        service);
 #ifdef AF_UNIX
@@ -863,7 +866,7 @@ GNUNET_CLIENT_service_test (const char *service,
 #endif
        if (GNUNET_OK !=
            GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un,
-                                       slen))
+                                       slen, GNUNET_BIND_EXCLUSIVE))
         {
          /* failed to bind => service must be running */
          GNUNET_free (unixpath);
@@ -871,7 +874,7 @@ GNUNET_CLIENT_service_test (const char *service,
          service_test_report (th, GNUNET_YES);
          return th;
        }
-       (void) GNUNET_NETWORK_socket_close (sock);        
+       (void) GNUNET_NETWORK_socket_close (sock);
         /* let's try IP */
       }
     }
@@ -913,7 +916,7 @@ GNUNET_CLIENT_service_test (const char *service,
     {
       if (GNUNET_OK !=
           GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in,
-                                      sizeof (s_in)))
+                                      sizeof (s_in), GNUNET_BIND_EXCLUSIVE))
       {
         /* failed to bind => service must be running */
         GNUNET_free (hostname);
@@ -946,7 +949,7 @@ GNUNET_CLIENT_service_test (const char *service,
     {
       if (GNUNET_OK !=
           GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6,
-                                      sizeof (s_in6)))
+                                      sizeof (s_in6), GNUNET_BIND_EXCLUSIVE))
       {
         /* failed to bind => service must be running */
         GNUNET_free (hostname);
@@ -1023,7 +1026,7 @@ client_delayed_retry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_CLIENT_TransmitHandle *th = cls;
   struct GNUNET_TIME_Relative delay;
-  
+
   th->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
   {
@@ -1093,9 +1096,9 @@ client_notify (void *cls, size_t size, void *buf)
   if (NULL == buf)
   {
     delay = GNUNET_TIME_absolute_get_remaining (th->timeout);
-    delay.rel_value /= 2;
+    delay.rel_value_us /= 2;
     if ((GNUNET_YES != th->auto_retry) || (0 == --th->attempts_left) ||
-        (delay.rel_value < 1)||
+        (delay.rel_value_us < 1)||
        (0 != (GNUNET_SCHEDULER_get_reason() & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1113,7 +1116,7 @@ client_notify (void *cls, size_t size, void *buf)
     {
       GNUNET_CONNECTION_receive_cancel (client->connection);
       client->in_receive = GNUNET_NO;
-    }    
+    }
     GNUNET_CONNECTION_destroy (client->connection);
     client->connection = NULL;
     delay = GNUNET_TIME_relative_min (delay, client->back_off);