* @author Christian Grothoff
*
* TODO:
- * - Optimization: keepalive messages / timeout (timeout to be done @ peer level!)
- * - Optimization: keep performance metrics (?)
+ * - Optimization: keep per-connection performance metrics (?)
*/
#include "platform.h"
+#include "gnunet-service-cadet-new.h"
#include "gnunet-service-cadet-new_channel.h"
#include "gnunet-service-cadet-new_connection.h"
#include "gnunet-service-cadet-new_paths.h"
#include "gnunet-service-cadet-new_peer.h"
#include "gnunet-service-cadet-new_tunnels.h"
#include "gnunet_cadet_service.h"
+#include "gnunet_statistics_service.h"
#include "cadet_protocol.h"
*/
struct GNUNET_SCHEDULER_Task *task;
+ /**
+ * Queue entry for keepalive messages.
+ */
+ struct CadetTunnelQueueEntry *keepalive_qe;
+
/**
* Function to call once we are ready to transmit.
*/
/**
- * Destroy a connection.
+ * Destroy a connection, part of the internal implementation. Called
+ * only from #GCC_destroy_from_core() or #GCC_destroy_from_tunnel().
*
* @param cc connection to destroy
*/
-void
+static void
GCC_destroy (struct CadetConnection *cc)
{
- struct GNUNET_MQ_Envelope *env = NULL;
-
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Destroying %s\n",
GCC_2s (cc));
- if (CADET_CONNECTION_SENDING_CREATE != cc->state)
+ if (NULL != cc->mq_man)
{
- struct GNUNET_CADET_ConnectionDestroyMessage *destroy_msg;
-
- /* Need to notify next hop that we are down. */
- env = GNUNET_MQ_msg (destroy_msg,
- GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
- destroy_msg->cid = cc->cid;
+ GCP_request_mq_cancel (cc->mq_man,
+ NULL);
+ cc->mq_man = NULL;
}
- GCP_request_mq_cancel (cc->mq_man,
- env);
- cc->mq_man = NULL;
if (NULL != cc->task)
{
GNUNET_SCHEDULER_cancel (cc->task);
cc->task = NULL;
}
+ if (NULL != cc->keepalive_qe)
+ {
+ GCT_send_cancel (cc->keepalive_qe);
+ cc->keepalive_qe = NULL;
+ }
GCPP_del_connection (cc->path,
cc->off,
cc);
}
+
+/**
+ * Destroy a connection, called when the CORE layer is already done
+ * (i.e. has received a BROKEN message), but if we still have to
+ * communicate the destruction of the connection to the tunnel (if one
+ * exists).
+ *
+ * @param cc connection to destroy
+ */
+void
+GCC_destroy_without_core (struct CadetConnection *cc)
+{
+ if (NULL != cc->ct)
+ {
+ GCT_connection_lost (cc->ct);
+ cc->ct = NULL;
+ }
+ GCC_destroy (cc);
+}
+
+
+/**
+ * Destroy a connection, called if the tunnel association with the
+ * connection was already broken, but we still need to notify the CORE
+ * layer about the breakage.
+ *
+ * @param cc connection to destroy
+ */
+void
+GCC_destroy_without_tunnel (struct CadetConnection *cc)
+{
+ cc->ct = NULL;
+ if ( (CADET_CONNECTION_SENDING_CREATE != cc->state) &&
+ (NULL != cc->mq_man) )
+ {
+ struct GNUNET_MQ_Envelope *env;
+ struct GNUNET_CADET_ConnectionDestroyMessage *destroy_msg;
+
+ /* Need to notify next hop that we are down. */
+ env = GNUNET_MQ_msg (destroy_msg,
+ GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
+ destroy_msg->cid = cc->cid;
+ GCP_request_mq_cancel (cc->mq_man,
+ env);
+ cc->mq_man = NULL;
+ }
+ GCC_destroy (cc);
+}
+
+
/**
* Return the tunnel associated with this connection.
*
/**
- * A connection ACK was received for this connection, implying
+ * Send a #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE through the
+ * tunnel to prevent it from timing out.
+ *
+ * @param cls the `struct CadetConnection` to keep alive.
+ */
+static void
+send_keepalive (void *cls);
+
+
+/**
+ * Keepalive was transmitted. Remember this, and possibly
+ * schedule the next one.
+ *
+ * @param cls the `struct CadetConnection` to keep alive.
+ */
+static void
+keepalive_done (void *cls)
+{
+ struct CadetConnection *cc = cls;
+
+ cc->keepalive_qe = NULL;
+ if ( (GNUNET_YES == cc->mqm_ready) &&
+ (NULL == cc->task) )
+ cc->task = GNUNET_SCHEDULER_add_delayed (keepalive_period,
+ &send_keepalive,
+ cc);
+}
+
+
+/**
+ * Send a #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE through the
+ * tunnel to prevent it from timing out.
+ *
+ * @param cls the `struct CadetConnection` to keep alive.
+ */
+static void
+send_keepalive (void *cls)
+{
+ struct CadetConnection *cc = cls;
+ struct GNUNET_MessageHeader msg;
+
+ cc->task = NULL;
+ GNUNET_assert (NULL != cc->ct);
+ GNUNET_assert (GNUNET_YES == cc->mqm_ready);
+ GNUNET_assert (NULL == cc->keepalive_qe);
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ "Sending KEEPALIVE on behalf of %s via %s\n",
+ GCC_2s (cc),
+ GCT_2s (cc->ct->t));
+ GNUNET_STATISTICS_update (stats,
+ "# keepalives sent",
+ 1,
+ GNUNET_NO);
+ msg.size = htons (sizeof (msg));
+ msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE);
+
+ cc->keepalive_qe
+ = GCT_send (cc->ct->t,
+ &msg,
+ &keepalive_done,
+ cc);
+}
+
+
+/**
+ * A #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK was received for this connection, implying
* that the end-to-end connection is up. Process it.
*
* @param cc the connection that got the ACK.
GCC_handle_connection_create_ack (struct CadetConnection *cc)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Received CREATE_ACK for %s in state %d (%s)\n",
+ "Received CADET_CONNECTION_CREATE_ACK for %s in state %d (%s)\n",
GCC_2s (cc),
cc->state,
(GNUNET_YES == cc->mqm_ready) ? "MQM ready" : "MQM busy");
GNUNET_SCHEDULER_cancel (cc->task);
cc->task = NULL;
}
-#if FIXME_KEEPALIVE
- cc->task = GNUNET_SCHEDULER_add_delayed (cc->keepalive_period,
- &send_keepalive,
- cc);
-#endif
cc->state = CADET_CONNECTION_READY;
if (GNUNET_YES == cc->mqm_ready)
+ {
cc->ready_cb (cc->ready_cb_cls,
GNUNET_YES);
+ if ( (NULL == cc->keepalive_qe) &&
+ (GNUNET_YES == cc->mqm_ready) &&
+ (NULL == cc->task) )
+ cc->task = GNUNET_SCHEDULER_add_delayed (keepalive_period,
+ &send_keepalive,
+ cc);
+ }
}
{
if (CADET_CONNECTION_SENT == cc->state)
{
- /* We didn't get the CREATE_ACK, but instead got payload. That's fine,
+ /* We didn't get the CADET_CONNECTION_CREATE_ACK, but instead got payload. That's fine,
clearly something is working, so pretend we got an ACK. */
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Faking connection ACK for %s due to KX\n",
+ "Faking connection CADET_CONNECTION_CREATE_ACK for %s due to KX\n",
GCC_2s (cc));
GCC_handle_connection_create_ack (cc);
}
/**
- * Send a CREATE message to the first hop.
+ * Send a #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE message to the
+ * first hop.
*
* @param cls the `struct CadetConnection` to initiate
*/
pids[i + 1] = *GCP_get_id (GCPP_get_peer_at_offset (cc->path,
i));
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Sending CONNECTION_CREATE message for %s\n",
+ "Sending CADET_CONNECTION_CREATE message for %s\n",
GCC_2s (cc));
cc->env = env;
cc->mqm_ready = GNUNET_NO;
case CADET_CONNECTION_READY:
cc->ready_cb (cc->ready_cb_cls,
GNUNET_YES);
+ if ( (NULL == cc->keepalive_qe) &&
+ (GNUNET_YES == cc->mqm_ready) &&
+ (NULL == cc->task) )
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Scheduling keepalive for %s in %s\n",
+ GCC_2s (cc),
+ GNUNET_STRINGS_relative_time_to_string (keepalive_period,
+ GNUNET_YES));
+ cc->task = GNUNET_SCHEDULER_add_delayed (keepalive_period,
+ &send_keepalive,
+ cc);
+ }
break;
}
}
* @param ct which tunnel uses this connection
* @param ready_cb function to call when ready to transmit
* @param ready_cb_cls closure for @a cb
- * @return handle to the connection
+ * @return handle to the connection, NULL if we already have
+ * a connection that takes precedence on @a path
*/
struct CadetConnection *
GCC_create_inbound (struct CadetPeer *destination,
GCC_ReadyCallback ready_cb,
void *ready_cb_cls)
{
+ struct CadetConnection *cc;
+ unsigned int off;
+
+ off = GCPP_find_peer (path,
+ destination);
+ GNUNET_assert (UINT_MAX != off);
+ cc = GCPP_get_connection (path,
+ destination,
+ off);
+ if (NULL != cc)
+ {
+ int cmp;
+
+ cmp = memcmp (cid,
+ &cc->cid,
+ sizeof (*cid));
+ if (0 == cmp)
+ {
+ /* Two peers picked the SAME random connection identifier at the
+ same time for the same path? Must be malicious. Drop
+ connection (existing and inbound), even if it is the only
+ one. */
+ GNUNET_break_op (0);
+ GCT_connection_lost (cc->ct);
+ GCC_destroy_without_tunnel (cc);
+ return NULL;
+ }
+ if (0 < cmp)
+ {
+ /* drop existing */
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Got two connections on %s, dropping my existing %s\n",
+ GCPP_2s (path),
+ GCC_2s (cc));
+ GCT_connection_lost (cc->ct);
+ GCC_destroy_without_tunnel (cc);
+ }
+ else
+ {
+ /* keep existing */
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Got two connections on %s, keeping my existing %s\n",
+ GCPP_2s (path),
+ GCC_2s (cc));
+ return NULL;
+ }
+ }
+
return connection_create (destination,
path,
ct,
GNUNET_assert (GNUNET_YES == cc->mqm_ready);
GNUNET_assert (CADET_CONNECTION_READY == cc->state);
cc->mqm_ready = GNUNET_NO;
+ if (NULL != cc->task)
+ {
+ GNUNET_SCHEDULER_cancel (cc->task);
+ cc->task = NULL;
+ }
GCP_send (cc->mq_man,
env);
}