-makefile for new test_stream_local (commented)
[oweals/gnunet.git] / src / transport / plugin_transport_udp.c
index 6f190de20aa94d77359e17dd7e2fe828ed2cc3c8..5ea2ba8da521fa58370339d2383794498e952f7e 100644 (file)
@@ -363,6 +363,53 @@ udp_address_to_string (void *cls, const void *addr, size_t addrlen)
 }
 
 
+/**
+ * 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
+ * @param added location to store the number of bytes in the buffer.
+ *        If the function returns GNUNET_SYSERR, its contents are undefined.
+ * @return GNUNET_OK on success, GNUNET_SYSERR on failure
+ */
+int
+udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
+    void **buf, size_t *added)
+{
+  struct sockaddr_storage socket_address;
+  int ret = GNUNET_STRINGS_to_address_ip (addr, addrlen,
+    &socket_address);
+
+  if (ret != GNUNET_OK)
+    return GNUNET_SYSERR;
+
+  if (socket_address.ss_family == AF_INET)
+  {
+    struct IPv4UdpAddress *u4;
+    struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
+    u4 = GNUNET_malloc (sizeof (struct IPv4UdpAddress));
+    u4->ipv4_addr = in4->sin_addr.s_addr;
+    u4->u4_port = in4->sin_port;
+    *buf = u4;
+    *added = sizeof (struct IPv4UdpAddress);
+  }
+  else if (socket_address.ss_family == AF_INET6)
+  {
+    struct IPv6UdpAddress *u6;
+    struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
+    u6 = GNUNET_malloc (sizeof (struct IPv6UdpAddress));
+    u6->ipv6_addr = in6->sin6_addr;
+    u6->u6_port = in6->sin6_port;
+    *buf = u6;
+    *added = sizeof (struct IPv6UdpAddress);
+  }
+  return GNUNET_SYSERR;
+}
+
+
 /**
  * Append our port and forward the result.
  *
@@ -762,6 +809,8 @@ udp_plugin_get_session (void *cls,
 {
   struct Session * s = NULL;
   struct Plugin * plugin = cls;
+  struct IPv6UdpAddress * udp_a6;
+  struct IPv4UdpAddress * udp_a4;
 
   GNUNET_assert (plugin != NULL);
   GNUNET_assert (address != NULL);
@@ -775,14 +824,23 @@ udp_plugin_get_session (void *cls,
     return NULL;
   }
 
-  if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
-      (plugin->sockv4 == NULL))
-    return NULL;
-
-  if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
-      (plugin->sockv6 == NULL))
-    return NULL;
+  if (address->address_length == sizeof (struct IPv4UdpAddress))
+  {
+    if (plugin->sockv4 == NULL)
+      return NULL;
+    udp_a4 = (struct IPv4UdpAddress *) address->address;
+    if (udp_a4->u4_port == 0)
+      return NULL;
+  }
 
+  if (address->address_length == sizeof (struct IPv6UdpAddress))
+  {
+    if (plugin->sockv6 == NULL)
+      return NULL;
+    udp_a6 = (struct IPv6UdpAddress *) address->address;
+    if (udp_a6->u6_port == 0)
+      return NULL;
+  }
 
   /* check if session already exists */
   struct SessionCompareContext cctx;
@@ -1329,7 +1387,8 @@ ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
       &l_ctx);
   s = l_ctx.res;
 
-  GNUNET_assert (s != NULL);
+  if (NULL == s)
+    return;
 
   if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
     delay = s->flow_delay_for_other_peer.rel_value;
@@ -1702,7 +1761,6 @@ udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
         udpw = udpw->next;
       }
     }
-
   }
 
   if (udpw == NULL)
@@ -1715,19 +1773,20 @@ udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
 
   if (GNUNET_SYSERR == sent)
   {
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         "UDP could not transmit %u-byte message to `%s': `%s'\n",
+         (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen),
+         STRERROR (errno));
+    if (udpw->cont != NULL)
+      udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_SYSERR);
+  }
+  else
+  {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "UDP transmitted %u-byte message to %s (%d: %s)\n",
+         "UDP transmitted %u-byte message to `%s' (%d: %s)\n",
          (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
          (sent < 0) ? STRERROR (errno) : "ok");
-    if (udpw->cont != NULL)
-      udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_SYSERR);
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "UDP transmitted %u-byte message to %s (%d: %s)\n",
-       (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
-       (sent < 0) ? STRERROR (errno) : "ok");
-
   /* This was just a message fragment */
   if (udpw->frag_ctx != NULL)
   {
@@ -2042,6 +2101,18 @@ libgnunet_plugin_transport_udp_init (void *cls)
 
   int res;
 
+  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 = &udp_plugin_address_pretty_printer;
+    api->address_to_string = &udp_address_to_string;
+    api->string_to_address = &udp_string_to_address;
+    return api;
+  }
+
   /* Get port number */
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
@@ -2147,6 +2218,7 @@ libgnunet_plugin_transport_udp_init (void *cls)
   api->disconnect = &udp_disconnect;
   api->address_pretty_printer = &udp_plugin_address_pretty_printer;
   api->address_to_string = &udp_address_to_string;
+  api->string_to_address = &udp_string_to_address;
   api->check_address = &udp_plugin_check_address;
   api->get_session = &udp_plugin_get_session;
   api->send = &udp_plugin_send;
@@ -2171,11 +2243,13 @@ libgnunet_plugin_transport_udp_init (void *cls)
   return api;
 }
 
-int heap_cleanup_iterator (void *cls,
-                          struct GNUNET_CONTAINER_HeapNode *
-                          node, void *element,
-                          GNUNET_CONTAINER_HeapCostType
-                          cost)
+
+static int
+heap_cleanup_iterator (void *cls,
+                      struct GNUNET_CONTAINER_HeapNode *
+                      node, void *element,
+                      GNUNET_CONTAINER_HeapCostType
+                      cost)
 {
   struct DefragContext * d_ctx = element;
 
@@ -2192,13 +2266,20 @@ int heap_cleanup_iterator (void *cls,
  * returns the udp transport API.
  *
  * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
- * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
+ * @return NULL
  */
 void *
 libgnunet_plugin_transport_udp_done (void *cls)
 {
   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
   struct Plugin *plugin = api->cls;
+
+  if (NULL == plugin)
+  {
+    GNUNET_free (api);
+    return NULL;
+  }
+
   stop_broadcast (plugin);
 
   if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)