-indentation
[oweals/gnunet.git] / src / transport / plugin_transport_unix.c
index 036096f6d8e325a051a2ea0f94a3c7e866d5e1ab..03c38f4e163a07676804c89f8187382734c53fa6 100644 (file)
@@ -64,9 +64,9 @@ GNUNET_NETWORK_STRUCT_BEGIN
 
 struct UnixAddress
 {
-       uint32_t options GNUNET_PACKED;
+  uint32_t options GNUNET_PACKED;
 
-       uint32_t addrlen GNUNET_PACKED;
+  uint32_t addrlen GNUNET_PACKED;
 };
 
 
@@ -340,6 +340,7 @@ reschedule_session_timeout (struct Session *s);
 static void
 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
+
 static struct sockaddr_un *
 unix_address_to_sockaddr (const char *unixpath,
                           socklen_t *sock_len)
@@ -372,11 +373,13 @@ unix_address_to_sockaddr (const char *unixpath,
  *
  * @param cls closure
  * @param addr binary address
- * @param addrlen length of the address
+ * @param addrlen length of the @a addr
  * @return string representing the same address
  */
 static const char *
-unix_address_to_string (void *cls, const void *addr, size_t addrlen)
+unix_address_to_string (void *cls,
+                        const void *addr,
+                        size_t addrlen)
 {
   static char rbuf[1024];
   struct UnixAddress *ua = (struct UnixAddress *) addr;
@@ -387,7 +390,7 @@ unix_address_to_string (void *cls, const void *addr, size_t addrlen)
   if ((NULL == addr) || (sizeof (struct UnixAddress) > addrlen))
   {
     GNUNET_break(0);
-    return NULL ;
+    return NULL;
   }
   addrstr = (char *) &ua[1];
   addr_str_len = ntohl (ua->addrlen);
@@ -395,39 +398,36 @@ unix_address_to_string (void *cls, const void *addr, size_t addrlen)
   if (addr_str_len != addrlen - sizeof(struct UnixAddress))
   {
     GNUNET_break(0);
-    return NULL ;
+    return NULL;
   }
 
   if ('\0' != addrstr[addr_str_len - 1])
   {
     GNUNET_break(0);
-    return NULL ;
+    return NULL;
   }
   if (strlen (addrstr) + 1 != addr_str_len)
   {
     GNUNET_break(0);
-    return NULL ;
+    return NULL;
   }
 
   off = 0;
   if ('\0' == addrstr[0])
     off++;
   memset (rbuf, 0, sizeof (rbuf));
-  GNUNET_snprintf (rbuf, sizeof (rbuf) - 1, "%s.%u.%s%.*s",
-      PLUGIN_NAME,
-      ntohl (ua->options),
-      (off == 1) ? "@" : "",
-            (int) (addr_str_len - off),
-            &addrstr[off]);
-/*
-  GNUNET_snprintf (rbuf, sizeof(rbuf), "%s.%u.%s", PLUGIN_NAME,
-      ntohl (ua->options), addrstr);*/
+  GNUNET_snprintf (rbuf,
+                   sizeof (rbuf) - 1,
+                   "%s.%u.%s%.*s",
+                   PLUGIN_NAME,
+                   ntohl (ua->options),
+                   (off == 1) ? "@" : "",
+                   (int) (addr_str_len - off),
+                   &addrstr[off]);
   return rbuf;
 }
 
 
-
-
 /**
  * Re-schedule the main 'select' callback (unix_plugin_select)
  * for this plugin.
@@ -547,7 +547,9 @@ unix_session_disconnect (void *cls,
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Disconnecting session for peer `%s' `%s'\n",
        GNUNET_i2s (&s->target),
-       unix_address_to_string (NULL, s->address->address, s->address->address_length) );
+       unix_address_to_string (NULL,
+                               s->address->address,
+                               s->address->address_length));
   plugin->env->session_end (plugin->env->cls, s->address, s);
   removed = GNUNET_NO;
   next = plugin->msg_head;
@@ -870,7 +872,8 @@ unix_plugin_get_session (void *cls,
                                              &get_session_it, &gsi);
   if (NULL != gsi.res)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p for address `%s'\n",
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Found existing session %p for address `%s'\n",
         gsi.res,
         unix_address_to_string (NULL, address->address, address->address_length));
     return gsi.res;
@@ -880,13 +883,17 @@ unix_plugin_get_session (void *cls,
   s = GNUNET_new (struct Session);
   s->target = address->peer;
   s->address = GNUNET_HELLO_address_copy (address);
+  s->plugin = plugin;
   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
   s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
                                                  &session_timeout,
                                                  s);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Creating a new session %p for address `%s'\n",
-       s,  unix_address_to_string (NULL, address->address, address->address_length));
+       s,
+       unix_address_to_string (NULL,
+                               address->address,
+                               address->address_length));
   (void) GNUNET_CONTAINER_multipeermap_put (plugin->session_map,
                                            &address->peer, s,
                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
@@ -960,8 +967,9 @@ unix_plugin_send (void *cls,
     LOG (GNUNET_ERROR_TYPE_ERROR,
         "Invalid session for peer `%s' `%s'\n",
         GNUNET_i2s (&session->target),
-        unix_address_to_string(NULL, session->address->address,
-            session->address->address_length));
+        unix_address_to_string(NULL,
+                                session->address->address,
+                                session->address->address_length));
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
@@ -969,8 +977,9 @@ unix_plugin_send (void *cls,
        "Sending %u bytes with session for peer `%s' `%s'\n",
        msgbuf_size,
        GNUNET_i2s (&session->target),
-       unix_address_to_string(NULL, session->address->address,
-                  session->address->address_length));
+       unix_address_to_string (NULL,
+                               session->address->address,
+                               session->address->address_length));
   ssize = sizeof (struct UNIXMessage) + msgbuf_size;
   message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
   message->header.size = htons (ssize);
@@ -1023,7 +1032,7 @@ unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
   GNUNET_assert (ua_len >= sizeof (struct UnixAddress));
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Received message from %s\n",
-       unix_address_to_string(NULL, ua, ua_len));
+       unix_address_to_string (NULL, ua, ua_len));
   GNUNET_STATISTICS_update (plugin->env->stats,
                            "# bytes received via UNIX",
                            ntohs (currhdr->size),
@@ -1314,7 +1323,17 @@ unix_transport_server_start (void *cls)
     return GNUNET_SYSERR;
   }
   if ('\0' != un->sun_path[0])
-    GNUNET_DISK_directory_create_for_file (un->sun_path);
+  {
+    if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (un->sun_path))
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot create path to `%s'\n"),
+          un->sun_path);
+      GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
+      plugin->unix_sock.desc = NULL;
+      GNUNET_free (un);
+      return GNUNET_SYSERR;
+    }
+  }
   if (GNUNET_OK !=
       GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, (const struct sockaddr *)  un, un_len))
   {
@@ -1408,20 +1427,18 @@ unix_plugin_address_pretty_printer (void *cls, const char *type,
                                     GNUNET_TRANSPORT_AddressStringCallback asc,
                                     void *asc_cls)
 {
-  if ((NULL != addr) && (addrlen > 0))
-  {
-    asc (asc_cls, unix_address_to_string (NULL, addr, addrlen));
-  }
-  else if (0 == addrlen)
-  {
-    asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
-  }
+  const char *ret;
+
+  if ( (NULL != addr) && (addrlen > 0))
+    ret = unix_address_to_string (NULL,
+                                  addr,
+                                  addrlen);
   else
-  {
-    GNUNET_break (0);
-    asc (asc_cls, "<invalid UNIX address>");
-  }
-  asc (asc_cls, NULL);
+    ret = NULL;
+  asc (asc_cls,
+       ret,
+       (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
+  asc (asc_cls, NULL, GNUNET_OK);
 }
 
 
@@ -1429,7 +1446,7 @@ unix_plugin_address_pretty_printer (void *cls, const char *type,
  * Function called to convert a string address to
  * a binary address.
  *
- * @param cls closure ('struct Plugin*')
+ * @param cls closure (`struct Plugin *`)
  * @param addr string address
  * @param addrlen length of the @a addr (strlen(addr) + '\0')
  * @param buf location to store the buffer
@@ -1569,7 +1586,7 @@ reschedule_session_timeout (struct Session *s)
  *
  * @param cls the plugin
  * @param key peer identity (unused)
- * @param value the 'struct Session' to disconnect
+ * @param value the `struct Session *` to disconnect
  * @return #GNUNET_YES (always, continue to iterate)
  */
 static int
@@ -1671,9 +1688,15 @@ libgnunet_plugin_transport_unix_init (void *cls)
   api->get_network = &unix_get_network;
   api->update_session_timeout = &unix_plugin_update_session_timeout;
   sockets_created = unix_transport_server_start (plugin);
-  if (0 == sockets_created)
+  if ((0 == sockets_created) || (GNUNET_SYSERR == sockets_created))
+  {
     LOG (GNUNET_ERROR_TYPE_WARNING,
         _("Failed to open UNIX listen socket\n"));
+    GNUNET_free (api);
+    GNUNET_free (plugin->unix_socket_path);
+    GNUNET_free (plugin);
+    return NULL;
+  }
   plugin->session_map = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
   plugin->address_update_task = GNUNET_SCHEDULER_add_now (&address_notification, plugin);
   return api;