-makefile for new test_stream_local (commented)
[oweals/gnunet.git] / src / transport / plugin_transport_unix.c
index 9f717f649f0fa126dcd17f9eeaa1483024a0cf9c..b7239ee9a24eccab9021bb5b47d47272ea3ab500 100644 (file)
@@ -64,8 +64,6 @@
  */
 #define UNIX_NAT_DEFAULT_PORT 22086
 
-#define MAX_RETRIES 5
-
 GNUNET_NETWORK_STRUCT_BEGIN
 
 /**
@@ -100,8 +98,6 @@ struct UNIXMessageWrapper
   struct UNIXMessage * msg;
   size_t msgsize;
 
-  int retry_counter;
-
   struct GNUNET_TIME_Relative timeout;
   unsigned int priority;
 
@@ -227,6 +223,8 @@ struct Plugin
    */
   struct GNUNET_NETWORK_FDSet *ws;
 
+  int with_ws;
+
   /**
    * socket that we transmit all data with
    */
@@ -319,7 +317,7 @@ unix_transport_server_stop (void *cls)
   GNUNET_break (GNUNET_OK ==
                 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
   plugin->unix_sock.desc = NULL;
-
+  plugin->with_ws = GNUNET_NO;
   return GNUNET_OK;
 }
 
@@ -346,7 +344,6 @@ find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
  * send_handle squared away!
  *
  * @param cls closure
- * @param incoming_retry_context the retry context to use
  * @param send_handle which handle to send message on
  * @param target who should receive this message (ignored by UNIX)
  * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
@@ -437,11 +434,9 @@ unix_real_send (void *cls,
 
     if (size < msgbuf_size)
     {
-#if DEBUG_UNIX
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Trying to increase socket buffer size from %i to %i for message size %i\n",
                   size, ((msgbuf_size / 1000) + 2) * 1000, msgbuf_size);
-#endif
       size = ((msgbuf_size / 1000) + 2) * 1000;
       if (GNUNET_NETWORK_socket_setsockopt
           ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
@@ -556,6 +551,18 @@ unix_plugin_get_session (void *cls,
   return s;
 }
 
+/*
+ * @param cls the plugin handle
+ * @param tc the scheduling context (for rescheduling this function again)
+ *
+ * We have been notified that our writeset has something to read.  We don't
+ * know which socket needs to be read, so we have to check each one
+ * Then reschedule this function to be called again once more is available.
+ *
+ */
+static void
+unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
 /**
  * Function that can be used by the transport service to transmit
  * a message using the plugin.   Note that in the case of a
@@ -621,7 +628,6 @@ unix_plugin_send (void *cls,
   wrapper->timeout = to;
   wrapper->cont = cont;
   wrapper->cont_cls = cont_cls;
-  wrapper->retry_counter = 0;
   wrapper->session = session;
 
   GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, wrapper);
@@ -631,6 +637,20 @@ unix_plugin_send (void *cls,
               (char *) session->addr);
 #endif
 
+  if (plugin->with_ws == GNUNET_NO)
+  {
+    if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
+      GNUNET_SCHEDULER_cancel(plugin->select_task);
+
+    plugin->select_task =
+        GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+                                     GNUNET_SCHEDULER_NO_TASK,
+                                     GNUNET_TIME_UNIT_FOREVER_REL,
+                                     plugin->rs,
+                                     plugin->ws,
+                                     &unix_plugin_select, plugin);
+    plugin->with_ws = GNUNET_YES;
+  }
   return ssize;
 }
 
@@ -690,6 +710,9 @@ unix_plugin_select_read (struct Plugin * plugin)
       GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
                                       (struct sockaddr *) &un, &addrlen);
 
+  if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS)))
+    return;
+
   if (ret == GNUNET_SYSERR)
   {
     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
@@ -760,17 +783,6 @@ unix_plugin_select_write (struct Plugin * plugin)
     return;
   }
 
-  /* max retries */
-  if (msgw->retry_counter > MAX_RETRIES)
-  {
-    msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR);
-    GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
-    GNUNET_break (0);
-    GNUNET_free (msgw->msg);
-    GNUNET_free (msgw);
-    return;
-  }
-
   /* failed and no retry */
   if (sent == -1)
   {
@@ -782,11 +794,7 @@ unix_plugin_select_write (struct Plugin * plugin)
 
   /* failed and retry */
   if (sent == 0)
-  {
-    msgw->retry_counter++;
     return;
-  }
-
 }
 
 /*
@@ -807,7 +815,7 @@ unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
     return;
 
-
+  plugin->with_ws = GNUNET_NO;
   if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
   {
     GNUNET_assert (GNUNET_NETWORK_fdset_isset
@@ -823,11 +831,17 @@ unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     unix_plugin_select_read (plugin);
   }
 
+  if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
+    GNUNET_SCHEDULER_cancel (plugin->select_task);
   plugin->select_task =
       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
                                    GNUNET_SCHEDULER_NO_TASK,
-                                   GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
-                                   plugin->ws, &unix_plugin_select, plugin);
+                                   GNUNET_TIME_UNIT_FOREVER_REL,
+                                   plugin->rs,
+                                   (plugin->msg_head != NULL) ? plugin->ws : NULL,
+                                   &unix_plugin_select, plugin);
+  if (plugin->msg_head != NULL)
+    plugin->with_ws = GNUNET_YES;
 }
 
 /**
@@ -893,8 +907,12 @@ unix_transport_server_start (void *cls)
   plugin->select_task =
       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
                                    GNUNET_SCHEDULER_NO_TASK,
-                                   GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
-                                   plugin->ws, &unix_plugin_select, plugin);
+                                   GNUNET_TIME_UNIT_FOREVER_REL,
+                                   plugin->rs,
+                                   NULL,
+                                   &unix_plugin_select, plugin);
+  plugin->with_ws = GNUNET_NO;
+
   return 1;
 }
 
@@ -960,6 +978,42 @@ 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 addr string address
+ * @param addrlen length of the address
+ * @param buf location to store the buffer
+ *        If the function returns GNUNET_SYSERR, its contents are undefined.
+ * @param added length of created address
+ * @return GNUNET_OK on success, GNUNET_SYSERR on failure
+ */
+int
+unix_string_to_address (void *cls, const char *addr, uint16_t addrlen,
+    void **buf, size_t *added)
+{
+  if ((NULL == addr) || (addrlen == 0))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+
+  if ((strlen (addr) + 1) != addrlen)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+
+  (*buf) = strdup (addr);
+  (*added) = strlen (addr) + 1;
+  return GNUNET_OK;
+}
+
+
+
+
 /**
  * Function called for a quick conversion of the binary address to
  * a numeric address.  Note that the caller must not free the
@@ -1009,6 +1063,18 @@ libgnunet_plugin_transport_unix_init (void *cls)
   struct Plugin *plugin;
   int sockets_created;
 
+  if (NULL == env->receive)
+  {
+    /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
+       initialze the plugin or the API */
+    api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
+    api->cls = NULL;
+    api->address_pretty_printer = &unix_plugin_address_pretty_printer;
+    api->address_to_string = &unix_address_to_string;
+    api->string_to_address = NULL; // FIXME!
+    return api;
+  }
+
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-unix", "PORT",
                                              &port))
@@ -1028,6 +1094,7 @@ libgnunet_plugin_transport_unix_init (void *cls)
   api->address_pretty_printer = &unix_plugin_address_pretty_printer;
   api->address_to_string = &unix_address_to_string;
   api->check_address = &unix_check_address;
+  api->string_to_address = &unix_string_to_address;
   sockets_created = unix_transport_server_start (plugin);
   if (sockets_created == 0)
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UNIX sockets\n"));
@@ -1044,6 +1111,11 @@ libgnunet_plugin_transport_unix_done (void *cls)
   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
   struct Plugin *plugin = api->cls;
 
+  if (NULL == plugin)
+  {
+    GNUNET_free (api);
+    return NULL;
+  }
   unix_transport_server_stop (plugin);
 
   GNUNET_CONTAINER_multihashmap_iterate (plugin->session_map, &get_session_delete_it, plugin);