allow empty/NULL context message
[oweals/gnunet.git] / src / transport / transport_api.c
index 55caf407a5d88a453a1f993d69f8d8b8b5379d70..59f249686f19d8fd621e0d0df9170c38a79de4b4 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2009-2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009-2013 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
@@ -202,49 +202,6 @@ struct GNUNET_TRANSPORT_GetHelloHandle
 };
 
 
-/**
- * Entry in linked list for a try-connect request.
- */
-struct GNUNET_TRANSPORT_TryConnectHandle
-{
-  /**
-   * For the DLL.
-   */
-  struct GNUNET_TRANSPORT_TryConnectHandle *prev;
-
-  /**
-   * For the DLL.
-   */
-  struct GNUNET_TRANSPORT_TryConnectHandle *next;
-
-  /**
-   * Peer we should try to connect to.
-   */
-  struct GNUNET_PeerIdentity pid;
-
-  /**
-   * Transport service handle this request is part of.
-   */
-  struct GNUNET_TRANSPORT_Handle *th;
-
-  /**
-   * Message transmission request to communicate to service.
-   */
-  struct GNUNET_TRANSPORT_TransmitHandle *tth;
-
-  /**
-   * Function to call upon completion (of request transmission).
-   */
-  GNUNET_TRANSPORT_TryConnectCallback cb;
-
-  /**
-   * Closure for @e cb.
-   */
-  void *cb_cls;
-
-};
-
-
 /**
  * Entry in linked list for all offer-HELLO requests.
  */
@@ -355,16 +312,6 @@ struct GNUNET_TRANSPORT_Handle
    */
   struct GNUNET_TRANSPORT_GetHelloHandle *hwl_tail;
 
-  /**
-   * Linked list of pending try connect requests head
-   */
-  struct GNUNET_TRANSPORT_TryConnectHandle *tc_head;
-
-  /**
-   * Linked list of pending try connect requests tail
-   */
-  struct GNUNET_TRANSPORT_TryConnectHandle *tc_tail;
-
   /**
    * Linked list of pending offer HELLO requests head
    */
@@ -454,11 +401,9 @@ disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h);
  * A neighbour has not gotten a SEND_OK in a  while. Print a warning.
  *
  * @param cls the `struct Neighbour`
- * @param tc scheduler context
  */
 static void
-do_warn_unready (void *cls,
-                 const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_warn_unready (void *cls)
 {
   struct Neighbour *n = cls;
   struct GNUNET_TIME_Relative delay;
@@ -763,7 +708,8 @@ demultiplexer (void *cls,
     bytes_msg = ntohl (okm->bytes_msg);
     bytes_physical = ntohl (okm->bytes_physical);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Receiving SEND_OK message, transmission %s.\n",
+         "Receiving SEND_OK message, transmission to %s %s.\n",
+         GNUNET_i2s (&okm->peer),
          ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
 
     n = neighbour_find (h,
@@ -777,10 +723,10 @@ demultiplexer (void *cls,
       disconnect_and_schedule_reconnect (h);
       break;
     }
-    if (bytes_physical >= bytes_msg)
+    if (bytes_physical > bytes_msg)
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG,
-           "Overhead for %u byte message: %u\n",
+           "Overhead for %u byte message was %u\n",
            bytes_msg,
            bytes_physical - bytes_msg);
       n->traffic_overhead += bytes_physical - bytes_msg;
@@ -799,9 +745,11 @@ demultiplexer (void *cls,
       n->th->timeout_task = NULL;
       /* we've been waiting for this (congestion, not quota,
        * caused delayed transmission) */
-      n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap, n, 0);
-      schedule_transmission (h);
+      n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap,
+                                            n,
+                                            0);
     }
+    schedule_transmission (h);
     break;
   case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
     if (size <
@@ -822,8 +770,10 @@ demultiplexer (void *cls,
       break;
     }
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Received message of type %u from `%s'.\n",
-         ntohs (imm->type), GNUNET_i2s (&im->peer));
+         "Received message of type %u with %u bytes from `%s'.\n",
+         (unsigned int) ntohs (imm->type),
+         (unsigned int) ntohs (imm->size),
+         GNUNET_i2s (&im->peer));
     n = neighbour_find (h, &im->peer);
     if (NULL == n)
     {
@@ -878,11 +828,9 @@ demultiplexer (void *cls,
  * network congestion.  Notify the initiator and clean up.
  *
  * @param cls the `struct GNUNET_TRANSPORT_TransmitHandle`
- * @param tc scheduler context
  */
 static void
-timeout_request_due_to_congestion (void *cls,
-                                   const struct GNUNET_SCHEDULER_TaskContext *tc)
+timeout_request_due_to_congestion (void *cls)
 {
   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
   struct Neighbour *n = th->neighbour;
@@ -1063,11 +1011,9 @@ transport_notify_ready (void *cls,
  * list or the peer message queues  to the service.
  *
  * @param cls transport service to schedule a transmission for
- * @param tc scheduler context
  */
 static void
-schedule_transmission_task (void *cls,
-                            const struct GNUNET_SCHEDULER_TaskContext *tc)
+schedule_transmission_task (void *cls)
 {
   struct GNUNET_TRANSPORT_Handle *h = cls;
   size_t size;
@@ -1148,7 +1094,11 @@ schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
     n->traffic_overhead = 0;
   }
   else
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "No work to be done, not scheduling transmission.\n");
     return;                     /* no work to be done */
+  }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Scheduling next transmission to service in %s\n",
        GNUNET_STRINGS_relative_time_to_string (delay,
@@ -1241,20 +1191,13 @@ send_start (void *cls,
  * Try again to connect to transport service.
  *
  * @param cls the handle to the transport service
- * @param tc scheduler context
  */
 static void
-reconnect (void *cls,
-           const struct GNUNET_SCHEDULER_TaskContext *tc)
+reconnect (void *cls)
 {
   struct GNUNET_TRANSPORT_Handle *h = cls;
 
   h->reconnect_task = NULL;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-  {
-    /* shutdown, just give up */
-    return;
-  }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Connecting to transport service.\n");
   GNUNET_assert (NULL == h->client);
@@ -1343,111 +1286,6 @@ cancel_control_transmit (struct GNUNET_TRANSPORT_Handle *th,
 }
 
 
-/**
- * Send #GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT message to the
- * service.
- *
- * @param cls the `struct GNUNET_TRANSPORT_TryConnectHandle`
- * @param size number of bytes available in @a buf
- * @param buf where to copy the message
- * @return number of bytes copied to @a buf
- */
-static size_t
-send_try_connect (void *cls,
-                  size_t size,
-                  void *buf)
-{
-  struct GNUNET_TRANSPORT_TryConnectHandle *tch = cls;
-  struct TransportRequestConnectMessage msg;
-
-  tch->tth = NULL;
-  if (NULL == buf)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Discarding  `%s' request to `%s' due to error in transport service connection.\n",
-         "REQUEST_CONNECT",
-         GNUNET_i2s (&tch->pid));
-    if (NULL != tch->cb)
-      tch->cb (tch->cb_cls,
-               GNUNET_SYSERR);
-    GNUNET_TRANSPORT_try_connect_cancel (tch);
-    return 0;
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Transmitting `%s' request with respect to `%s'.\n",
-       "REQUEST_CONNECT",
-       GNUNET_i2s (&tch->pid));
-  GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
-  msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
-  msg.reserved = htonl (0);
-  msg.peer = tch->pid;
-  memcpy (buf, &msg, sizeof (msg));
-  if (NULL != tch->cb)
-    tch->cb (tch->cb_cls, GNUNET_OK);
-  GNUNET_TRANSPORT_try_connect_cancel (tch);
-  return sizeof (struct TransportRequestConnectMessage);
-}
-
-
-/**
- * Ask the transport service to establish a connection to
- * the given peer.
- *
- * @param handle connection to transport service
- * @param target who we should try to connect to
- * @param cb callback to be called when request was transmitted to transport
- *         service
- * @param cb_cls closure for the callback
- * @return a `struct GNUNET_TRANSPORT_TryConnectHandle` handle or
- *         NULL on failure (cb will not be called)
- */
-struct GNUNET_TRANSPORT_TryConnectHandle *
-GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
-                              const struct GNUNET_PeerIdentity *target,
-                              GNUNET_TRANSPORT_TryConnectCallback cb,
-                              void *cb_cls)
-{
-  struct GNUNET_TRANSPORT_TryConnectHandle *tch;
-
-  if (NULL == handle->client)
-    return NULL;
-  tch = GNUNET_new (struct GNUNET_TRANSPORT_TryConnectHandle);
-  tch->th = handle;
-  tch->pid = *target;
-  tch->cb = cb;
-  tch->cb_cls = cb_cls;
-  tch->tth = schedule_control_transmit (handle,
-                                        sizeof (struct TransportRequestConnectMessage),
-                                        &send_try_connect, tch);
-  GNUNET_CONTAINER_DLL_insert (handle->tc_head,
-                               handle->tc_tail,
-                               tch);
-  return tch;
-}
-
-
-/**
- * Cancel the request to transport to try a connect
- * Callback will not be called
- *
- * @param tch the handle to cancel
- */
-void
-GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch)
-{
-  struct GNUNET_TRANSPORT_Handle *th;
-
-  th = tch->th;
-  if (NULL != tch->tth)
-    cancel_control_transmit (th, tch->tth);
-  GNUNET_CONTAINER_DLL_remove (th->tc_head,
-                               th->tc_tail,
-                               tch);
-  GNUNET_free (tch);
-}
-
-
 /**
  * Send HELLO message to the service.
  *
@@ -1464,19 +1302,14 @@ send_hello (void *cls,
   struct GNUNET_TRANSPORT_OfferHelloHandle *ohh = cls;
   struct GNUNET_MessageHeader *msg = ohh->msg;
   uint16_t ssize;
-  struct GNUNET_SCHEDULER_TaskContext tc;
 
-  tc.read_ready = NULL;
-  tc.write_ready = NULL;
-  tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
   if (NULL == buf)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Timeout while trying to transmit `%s' request.\n",
          "HELLO");
     if (NULL != ohh->cont)
-      ohh->cont (ohh->cls,
-                 &tc);
+      ohh->cont (ohh->cls);
     GNUNET_free (msg);
     GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head,
                                  ohh->th->oh_tail,
@@ -1493,10 +1326,8 @@ send_hello (void *cls,
           msg,
           ssize);
   GNUNET_free (msg);
-  tc.reason = GNUNET_SCHEDULER_REASON_READ_READY;
   if (NULL != ohh->cont)
-    ohh->cont (ohh->cls,
-               &tc);
+    ohh->cont (ohh->cls);
   GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head,
                                ohh->th->oh_tail,
                                ohh);
@@ -1678,11 +1509,9 @@ GNUNET_TRANSPORT_check_peer_connected (struct GNUNET_TRANSPORT_Handle *handle,
  * Task to call the HelloUpdateCallback of the GetHelloHandle
  *
  * @param cls the `struct GNUNET_TRANSPORT_GetHelloHandle`
- * @param tc the scheduler task context
  */
 static void
-call_hello_update_cb_async (void *cls,
-                            const struct GNUNET_SCHEDULER_TaskContext *tc)
+call_hello_update_cb_async (void *cls)
 {
   struct GNUNET_TRANSPORT_GetHelloHandle *ghh = cls;
 
@@ -1739,7 +1568,9 @@ GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
 
   if (NULL != ghh->notify_task)
     GNUNET_SCHEDULER_cancel (ghh->notify_task);
-  GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
+  GNUNET_CONTAINER_DLL_remove (handle->hwl_head,
+                               handle->hwl_tail,
+                               ghh);
   GNUNET_free (ghh);
 }
 
@@ -1863,8 +1694,6 @@ GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
   }
   GNUNET_free_non_null (handle->my_hello);
   handle->my_hello = NULL;
-  GNUNET_assert (NULL == handle->tc_head);
-  GNUNET_assert (NULL == handle->tc_tail);
   GNUNET_assert (NULL == handle->hwl_head);
   GNUNET_assert (NULL == handle->hwl_tail);
   GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
@@ -1906,7 +1735,7 @@ GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
   n = neighbour_find (handle, target);
   if (NULL == n)
   {
-    /* use GNUNET_TRANSPORT_try_connect first, only use this function
+    /* only use this function
      * once a connection has been established */
     GNUNET_assert (0);
     return NULL;