-fix NPE
[oweals/gnunet.git] / src / transport / transport_api.c
index 9d221c69ed91aed2035f7ee4ee584fe3e4df72b6..50296855385e5862ecdd730b08ec8e3c0378b7ca 100644 (file)
@@ -743,6 +743,8 @@ demultiplexer (void *cls,
     if (size < sizeof (struct ConnectInfoMessage))
     {
       GNUNET_break (0);
+      h->reconnecting = GNUNET_YES;
+      disconnect_and_schedule_reconnect (h);
       break;
     }
     cim = (const struct ConnectInfoMessage *) msg;
@@ -750,6 +752,8 @@ demultiplexer (void *cls,
         sizeof (struct ConnectInfoMessage))
     {
       GNUNET_break (0);
+      h->reconnecting = GNUNET_YES;
+      disconnect_and_schedule_reconnect (h);
       break;
     }
     LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -759,6 +763,8 @@ demultiplexer (void *cls,
     if (NULL != n)
     {
       GNUNET_break (0);
+      h->reconnecting = GNUNET_YES;
+      disconnect_and_schedule_reconnect (h);
       break;
     }
     n = neighbour_add (h,
@@ -777,6 +783,8 @@ demultiplexer (void *cls,
     if (size != sizeof (struct DisconnectInfoMessage))
     {
       GNUNET_break (0);
+      h->reconnecting = GNUNET_YES;
+      disconnect_and_schedule_reconnect (h);
       break;
     }
     dim = (const struct DisconnectInfoMessage *) msg;
@@ -788,6 +796,8 @@ demultiplexer (void *cls,
     if (NULL == n)
     {
       GNUNET_break (0);
+      h->reconnecting = GNUNET_YES;
+      disconnect_and_schedule_reconnect (h);
       break;
     }
     neighbour_delete (h,
@@ -798,6 +808,8 @@ demultiplexer (void *cls,
     if (size != sizeof (struct SendOkMessage))
     {
       GNUNET_break (0);
+      h->reconnecting = GNUNET_YES;
+      disconnect_and_schedule_reconnect (h);
       break;
     }
     okm = (const struct SendOkMessage *) msg;
@@ -811,9 +823,11 @@ demultiplexer (void *cls,
                         &okm->peer);
     if (NULL == n)
     {
-      /* we should never get a 'SEND_OK' for a peer that we are not
+      /* We should never get a 'SEND_OK' for a peer that we are not
          connected to */
       GNUNET_break (0);
+      h->reconnecting = GNUNET_YES;
+      disconnect_and_schedule_reconnect (h);
       break;
     }
     if (bytes_physical >= bytes_msg)
@@ -826,8 +840,11 @@ demultiplexer (void *cls,
     }
     GNUNET_break (GNUNET_NO == n->is_ready);
     n->is_ready = GNUNET_YES;
-    GNUNET_SCHEDULER_cancel (n->unready_warn_task);
-    n->unready_warn_task = NULL;
+    if (NULL != n->unready_warn_task)
+    {
+      GNUNET_SCHEDULER_cancel (n->unready_warn_task);
+      n->unready_warn_task = NULL;
+    }
     if ((NULL != n->th) && (NULL == n->hn))
     {
       GNUNET_assert (NULL != n->th->timeout_task);
@@ -844,6 +861,8 @@ demultiplexer (void *cls,
         sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
     {
       GNUNET_break (0);
+      h->reconnecting = GNUNET_YES;
+      disconnect_and_schedule_reconnect (h);
       break;
     }
     im = (const struct InboundMessage *) msg;
@@ -851,6 +870,8 @@ demultiplexer (void *cls,
     if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
     {
       GNUNET_break (0);
+      h->reconnecting = GNUNET_YES;
+      disconnect_and_schedule_reconnect (h);
       break;
     }
     LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -860,6 +881,8 @@ demultiplexer (void *cls,
     if (NULL == n)
     {
       GNUNET_break (0);
+      h->reconnecting = GNUNET_YES;
+      disconnect_and_schedule_reconnect (h);
       break;
     }
     if (NULL != h->rec)
@@ -871,12 +894,19 @@ demultiplexer (void *cls,
     if (size != sizeof (struct QuotaSetMessage))
     {
       GNUNET_break (0);
+      h->reconnecting = GNUNET_YES;
+      disconnect_and_schedule_reconnect (h);
       break;
     }
     qm = (const struct QuotaSetMessage *) msg;
     n = neighbour_find (h, &qm->peer);
     if (NULL == n)
+    {
+      GNUNET_break (0);
+      h->reconnecting = GNUNET_YES;
+      disconnect_and_schedule_reconnect (h);
       break;
+    }
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Receiving SET_QUOTA message for `%s' with quota %u\n",
          GNUNET_i2s (&qm->peer),
@@ -973,7 +1003,7 @@ transport_notify_ready (void *cls,
                        size,
                        &cbuf[ret]);
     delay = GNUNET_TIME_absolute_get_duration (th->request_start);
-    if (delay.rel_value_us > 1000 * 1000)
+    if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
       LOG (GNUNET_ERROR_TYPE_WARNING,
            "Added %u bytes of control message at %u after %s delay\n",
            nret,
@@ -1018,53 +1048,58 @@ transport_notify_ready (void *cls,
     GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
     n->hn = NULL;
     n->th = NULL;
-    n->is_ready = GNUNET_NO; // FIXME! move into 'if' below!
     GNUNET_assert (size >= sizeof (struct OutboundMessage));
-    mret =
-        th->notify (th->notify_cls, size - sizeof (struct OutboundMessage),
-                    &cbuf[ret + sizeof (struct OutboundMessage)]);
+    mret = th->notify (th->notify_cls,
+                       size - sizeof (struct OutboundMessage),
+                       &cbuf[ret + sizeof (struct OutboundMessage)]);
     GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
-    if (0 != mret)
+    if (0 == mret)
     {
-      if (NULL != n->unready_warn_task)
-        n->unready_warn_task
-          = GNUNET_SCHEDULER_add_delayed (UNREADY_WARN_TIME,
-                                          &do_warn_unready,
-                                          n);
-      n->last_payload = GNUNET_TIME_absolute_get ();
-      GNUNET_assert (mret + sizeof (struct OutboundMessage) <
-                     GNUNET_SERVER_MAX_MESSAGE_SIZE);
-      obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
-      obm.header.size = htons (mret + sizeof (struct OutboundMessage));
-      obm.reserved = htonl (0);
-      obm.timeout =
-          GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
-                                     (th->timeout));
-      obm.peer = n->id;
-      memcpy (&cbuf[ret],
-              &obm,
-              sizeof (struct OutboundMessage));
-      ret += (mret + sizeof (struct OutboundMessage));
-      size -= (mret + sizeof (struct OutboundMessage));
-      GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker,
-                                        mret);
-      delay = GNUNET_TIME_absolute_get_duration (th->request_start);
-      if (delay.rel_value_us > 1000 * 1000)
-        LOG (GNUNET_ERROR_TYPE_WARNING,
-             "Added %u bytes of payload message at %u after %s delay\n",
-             mret,
-             ret,
-             GNUNET_STRINGS_relative_time_to_string (delay,
-                                                     GNUNET_YES));
-      else
-        LOG (GNUNET_ERROR_TYPE_DEBUG,
-             "Added %u bytes of payload message at %u after %s delay\n",
-             mret,
-             ret,
-             GNUNET_STRINGS_relative_time_to_string (delay,
-                                                     GNUNET_YES));
+      GNUNET_free (th);
+      continue;
     }
+    if (NULL != n->unready_warn_task)
+      n->unready_warn_task
+        = GNUNET_SCHEDULER_add_delayed (UNREADY_WARN_TIME,
+                                        &do_warn_unready,
+                                        n);
+    n->last_payload = GNUNET_TIME_absolute_get ();
+    n->is_ready = GNUNET_NO;
+    GNUNET_assert (mret + sizeof (struct OutboundMessage) <
+                   GNUNET_SERVER_MAX_MESSAGE_SIZE);
+    obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
+    obm.header.size = htons (mret + sizeof (struct OutboundMessage));
+    obm.reserved = htonl (0);
+    obm.timeout =
+      GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
+                                 (th->timeout));
+    obm.peer = n->id;
+    memcpy (&cbuf[ret],
+            &obm,
+            sizeof (struct OutboundMessage));
+    ret += (mret + sizeof (struct OutboundMessage));
+    size -= (mret + sizeof (struct OutboundMessage));
+    GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker,
+                                      mret);
+    delay = GNUNET_TIME_absolute_get_duration (th->request_start);
+    if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
+      LOG (GNUNET_ERROR_TYPE_WARNING,
+           "Added %u bytes of payload message for %s after %s delay at %u b/s\n",
+           mret,
+           GNUNET_i2s (&n->id),
+           GNUNET_STRINGS_relative_time_to_string (delay,
+                                                   GNUNET_YES),
+           (unsigned int) n->out_tracker.available_bytes_per_s__);
+    else
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+           "Added %u bytes of payload message for %s after %s delay at %u b/s\n",
+           mret,
+           GNUNET_i2s (&n->id),
+           GNUNET_STRINGS_relative_time_to_string (delay,
+                                                   GNUNET_YES),
+           (unsigned int) n->out_tracker.available_bytes_per_s__);
     GNUNET_free (th);
+    break;
   }
   /* if there are more pending messages, try to schedule those */
   schedule_transmission (h);
@@ -1297,7 +1332,7 @@ disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
 {
   struct GNUNET_TRANSPORT_TransmitHandle *th;
 
-  GNUNET_assert (h->reconnect_task == NULL);
+  GNUNET_assert (NULL == h->reconnect_task);
   if (NULL != h->cth)
   {
     GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
@@ -1707,16 +1742,16 @@ GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
  * @param cont continuation to call when HELLO has been sent,
  *     tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
  *     tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
- * @param cls closure for continuation
+ * @param cont_cls closure for @a cont
  * @return a `struct GNUNET_TRANSPORT_OfferHelloHandle` handle or NULL on failure,
- *      in case of failure cont will not be called
+ *      in case of failure @a cont will not be called
  *
  */
 struct GNUNET_TRANSPORT_OfferHelloHandle *
 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
                               const struct GNUNET_MessageHeader *hello,
                               GNUNET_SCHEDULER_TaskCallback cont,
-                              void *cls)
+                              void *cont_cls)
 {
   struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
   struct GNUNET_MessageHeader *msg;
@@ -1745,11 +1780,15 @@ GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
   ohh = GNUNET_new (struct GNUNET_TRANSPORT_OfferHelloHandle);
   ohh->th = handle;
   ohh->cont = cont;
-  ohh->cls = cls;
+  ohh->cls = cont_cls;
   ohh->msg = msg;
-  ohh->tth = schedule_control_transmit (handle, size,
-                                        &send_hello, ohh);
-  GNUNET_CONTAINER_DLL_insert (handle->oh_head, handle->oh_tail, ohh);
+  ohh->tth = schedule_control_transmit (handle,
+                                        size,
+                                        &send_hello,
+                                        ohh);
+  GNUNET_CONTAINER_DLL_insert (handle->oh_head,
+                               handle->oh_tail,
+                               ohh);
   return ohh;
 }
 
@@ -2050,7 +2089,7 @@ GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
   n->traffic_overhead = 0;
   if (delay.rel_value_us > timeout.rel_value_us)
     delay.rel_value_us = 0;        /* notify immediately (with failure) */
-  if (delay.rel_value_us > GNUNET_TIME_UNIT_SECONDS.rel_value_us)
+  if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
     LOG (GNUNET_ERROR_TYPE_WARNING,
          "At bandwidth %u byte/s next transmission to %s in %s\n",
          (unsigned int) n->out_tracker.available_bytes_per_s__,