Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet-new_channel.c
index 9d9edc28dae8b5db3303288c9845b7533e3d9180..8769601c201a4ddb6c8c4f4c9b3a0da44993fe31 100644 (file)
@@ -76,7 +76,7 @@
 
 
 /**
- * All the states a connection can be in.
+ * All the states a channel can be in.
  */
 enum CadetChannelState
 {
@@ -86,7 +86,13 @@ enum CadetChannelState
   CADET_CHANNEL_NEW,
 
   /**
-   * Connection create message sent, waiting for ACK.
+   * Channel is to a port that is not open, we're waiting for the
+   * port to be opened.
+   */
+  CADET_CHANNEL_LOOSE,
+
+  /**
+   * CHANNEL_OPEN message sent, waiting for CHANNEL_OPEN_ACK.
    */
   CADET_CHANNEL_OPEN_SENT,
 
@@ -644,6 +650,7 @@ GCCH_channel_local_new (struct CadetClient *owner,
     if (NULL == c)
     {
       /* port closed, wait for it to possibly open */
+      ch->state = CADET_CHANNEL_LOOSE;
       (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
                                                 port,
                                                 ch,
@@ -654,11 +661,8 @@ GCCH_channel_local_new (struct CadetClient *owner,
     }
     else
     {
-      ch->dest = GNUNET_new (struct CadetChannelClient);
-      ch->dest->c = c;
-      ch->dest->client_ready = GNUNET_YES;
       GCCH_bind (ch,
-                 ch->dest->c);
+                 c);
     }
   }
   else
@@ -740,10 +744,12 @@ GCCH_channel_incoming_new (struct CadetTunnel *t,
   if (NULL == c)
   {
     /* port closed, wait for it to possibly open */
+    ch->state = CADET_CHANNEL_LOOSE;
     (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
                                               port,
                                               ch,
                                               GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+    GNUNET_assert (NULL == ch->retry_control_task);
     ch->retry_control_task
       = GNUNET_SCHEDULER_add_delayed (TIMEOUT_CLOSED_PORT,
                                       &timeout_closed_cb,
@@ -829,10 +835,10 @@ send_open_ack (void *cls)
   struct CadetChannel *ch = cls;
   struct GNUNET_CADET_ChannelManageMessage msg;
 
+  ch->retry_control_task = NULL;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Sending CHANNEL_OPEN_ACK on %s\n",
        GCCH_2s (ch));
-  ch->retry_control_task = NULL;
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK);
   msg.header.size = htons (sizeof (msg));
   msg.reserved = htonl (0);
@@ -954,6 +960,7 @@ GCCH_bind (struct CadetChannel *ch,
   if (ch->out_of_order)
     options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
   cccd = GNUNET_new (struct CadetChannelClient);
+  GNUNET_assert (NULL == ch->dest);
   ch->dest = cccd;
   cccd->c = c;
   cccd->client_ready = GNUNET_YES;
@@ -977,6 +984,7 @@ GCCH_bind (struct CadetChannel *ch,
   else
   {
     /* notify other peer that we accepted the connection */
+    ch->state = CADET_CHANNEL_READY;
     ch->retry_control_task
       = GNUNET_SCHEDULER_add_now (&send_open_ack,
                                   ch);
@@ -990,6 +998,29 @@ GCCH_bind (struct CadetChannel *ch,
 }
 
 
+/**
+ * One of our clients has disconnected, tell the other one that we
+ * are finished. Done asynchronously to avoid concurrent modification
+ * issues if this is the same client.
+ *
+ * @param cls the `struct CadetChannel` where one of the ends is now dead
+ */
+static void
+signal_remote_destroy_cb (void *cls)
+{
+  struct CadetChannel *ch = cls;
+  struct CadetChannelClient *ccc;
+
+  /* Find which end is left... */
+  ch->retry_control_task = NULL;
+  ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
+  GSC_handle_remote_channel_destroy (ccc->c,
+                                     ccc->ccn,
+                                     ch);
+  channel_destroy (ch);
+}
+
+
 /**
  * Destroy locally created channel.  Called by the local client, so no
  * need to tell the client.
@@ -1034,22 +1065,44 @@ GCCH_channel_local_destroy (struct CadetChannel *ch,
     channel_destroy (ch);
     return;
   }
-  if ( (NULL != ch->head_sent) ||
-       (NULL != ch->owner) ||
-       (NULL != ch->dest) )
+  if ( (NULL != ch->head_sent) &&
+       ( (NULL != ch->owner) ||
+         (NULL != ch->dest) ) )
   {
     /* Wait for other end to destroy us as well,
        and otherwise allow send queue to be transmitted first */
     ch->destroy = GNUNET_YES;
     return;
   }
-  /* If the we ever sent the CHANNEL_CREATE, we need to send a destroy message. */
-  if (CADET_CHANNEL_NEW == ch->state)
-    GSC_drop_loose_channel (&ch->port,
-                            ch);
-  else
-    GCT_send_channel_destroy (ch->t,
-                              ch->ctn);
+  if ( (GNUNET_YES == ch->is_loopback) &&
+       ( (NULL != ch->owner) ||
+         (NULL != ch->dest) ) )
+  {
+    if (NULL != ch->retry_control_task)
+      GNUNET_SCHEDULER_cancel (ch->retry_control_task);
+    ch->retry_control_task
+      = GNUNET_SCHEDULER_add_now (&signal_remote_destroy_cb,
+                                  ch);
+    return;
+  }
+  if (GNUNET_NO == ch->is_loopback)
+  {
+    /* If the we ever sent the CHANNEL_CREATE, we need to send a destroy message. */
+    switch (ch->state)
+    {
+    case CADET_CHANNEL_NEW:
+      /* We gave up on a channel that we created as a client to a remote
+         target, but that never went anywhere. Nothing to do here. */
+      break;
+    case CADET_CHANNEL_LOOSE:
+      GSC_drop_loose_channel (&ch->port,
+                              ch);
+      break;
+    default:
+      GCT_send_channel_destroy (ch->t,
+                                ch->ctn);
+    }
+  }
   /* Nothing left to do, just finish destruction */
   channel_destroy (ch);
 }
@@ -1072,6 +1125,10 @@ GCCH_handle_channel_open_ack (struct CadetChannel *ch,
     /* this should be impossible */
     GNUNET_break (0);
     break;
+  case CADET_CHANNEL_LOOSE:
+    /* This makes no sense. */
+    GNUNET_break_op (0);
+    break;
   case CADET_CHANNEL_OPEN_SENT:
     if (NULL == ch->owner)
     {
@@ -1561,7 +1618,8 @@ GCCH_handle_remote_destroy (struct CadetChannel *ch,
     return;
   }
   ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
-  if (NULL != ccc->head_recv)
+  if ( (NULL != ccc) &&
+       (NULL != ccc->head_recv) )
   {
     LOG (GNUNET_ERROR_TYPE_WARNING,
          "Lost end of transmission due to remote shutdown on %s\n",
@@ -1569,9 +1627,10 @@ GCCH_handle_remote_destroy (struct CadetChannel *ch,
     /* FIXME: change API to notify client about truncated transmission! */
   }
   ch->destroy = GNUNET_YES;
-  GSC_handle_remote_channel_destroy (ccc->c,
-                                     ccc->ccn,
-                                     ch);
+  if (NULL != ccc)
+    GSC_handle_remote_channel_destroy (ccc->c,
+                                       ccc->ccn,
+                                       ch);
   channel_destroy (ch);
 }
 
@@ -1725,7 +1784,7 @@ GCCH_handle_local_data (struct CadetChannel *ch,
     struct CadetChannelClient *receiver;
     struct GNUNET_MQ_Envelope *env;
     struct GNUNET_CADET_LocalData *ld;
-    int to_owner;
+    int ack_to_owner;
 
     env = GNUNET_MQ_msg_extra (ld,
                                buf_len,
@@ -1735,14 +1794,14 @@ GCCH_handle_local_data (struct CadetChannel *ch,
           ch->owner->ccn.channel_of_client) )
     {
       receiver = ch->dest;
-      to_owner = GNUNET_NO;
+      ack_to_owner = GNUNET_YES;
     }
     else if ( (NULL != ch->dest) &&
               (sender_ccn.channel_of_client ==
                ch->dest->ccn.channel_of_client) )
     {
       receiver = ch->owner;
-      to_owner = GNUNET_YES;
+      ack_to_owner = GNUNET_NO;
     }
     else
     {
@@ -1755,10 +1814,11 @@ GCCH_handle_local_data (struct CadetChannel *ch,
                    buf_len);
     if (GNUNET_YES == receiver->client_ready)
     {
+      ch->pending_messages--;
       GSC_send_to_client (receiver->c,
                           env);
       send_ack_to_client (ch,
-                          to_owner);
+                          ack_to_owner);
     }
     else
     {