/*
This file is part of GNUnet.
- Copyright (C) 2001-2015 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2001-2015 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
/******************************** STRUCTS **********************************/
/******************************************************************************/
+/**
+ * Handle for messages queued but not yet sent.
+ */
+struct CadetConnectionQueue
+{
+
+ struct CadetConnectionQueue *next;
+ struct CadetConnectionQueue *prev;
+
+ /**
+ * Peer queue handle, to cancel if necessary.
+ */
+ struct CadetPeerQueue *peer_q;
+
+ /**
+ * Continuation to call once sent.
+ */
+ GCC_sent cont;
+
+ /**
+ * Closure for @e cont.
+ */
+ void *cont_cls;
+
+ /**
+ * Was this a forced message? (Do not account for it)
+ */
+ int forced;
+};
+
+
/**
* Struct to encapsulate all the Flow Control information to a peer to which
* we are directly connected (on a core level).
*/
struct CadetConnection *c;
+ struct CadetConnectionQueue *q_head;
+ struct CadetConnectionQueue *q_tail;
+
/**
* How many messages are in the queue on this connection.
*/
/**
* How many messages do we accept in the queue.
+ * If 0, the connection is broken in this direction (next hop disconnected).
*/
unsigned int queue_max;
+ /**
+ * ID of the next packet to send.
+ */
+ struct CadetEncryptedMessageIdentifier next_pid;
+
/**
* ID of the last packet sent towards the peer.
*/
- uint32_t last_pid_sent;
+ struct CadetEncryptedMessageIdentifier last_pid_sent;
/**
* ID of the last packet received from the peer.
*/
- uint32_t last_pid_recv;
+ struct CadetEncryptedMessageIdentifier last_pid_recv;
/**
* Bitmap of past 32 messages received:
uint32_t recv_bitmap;
/**
- * Last ACK sent to the peer (peer can't send more than this PID).
+ * Last ACK sent to the peer (peer is not allowed to send
+ * messages with PIDs higher than this value).
*/
- uint32_t last_ack_sent;
+ struct CadetEncryptedMessageIdentifier last_ack_sent;
/**
* Last ACK sent towards the origin (for traffic towards leaf node).
*/
- uint32_t last_ack_recv;
+ struct CadetEncryptedMessageIdentifier last_ack_recv;
/**
* Task to poll the peer in case of a lost ACK causes stall.
/**
* ID of the connection.
*/
- struct GNUNET_CADET_Hash id;
+ struct GNUNET_CADET_ConnectionTunnelIdentifier id;
/**
* Path being used for the tunnel. At the origin of the connection
};
-/**
- * Handle for messages queued but not yet sent.
- */
-struct CadetConnectionQueue
-{
- /**
- * Peer queue handle, to cancel if necessary.
- */
- struct CadetPeerQueue *q;
-
- /**
- * Continuation to call once sent.
- */
- GCC_sent cont;
-
- /**
- * Closure for @e cont.
- */
- void *cont_cls;
-
- /**
- * Was this a forced message? (Do not account for it)
- */
- int forced;
-};
-
/******************************************************************************/
/******************************* GLOBALS ***********************************/
/******************************************************************************/
/**
* Connections known, indexed by cid (CadetConnection).
*/
-static struct GNUNET_CONTAINER_MultiHashMap *connections;
+static struct GNUNET_CONTAINER_MultiShortmap *connections;
/**
* How many connections are we willing to maintain.
- * Local connections are always allowed, even if there are more connections than max.
+ * Local connections are always allowed,
+ * even if there are more connections than max.
*/
static unsigned long long max_connections;
fc_debug (struct CadetFlowControl *fc)
{
LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
- fc->last_pid_recv, fc->last_ack_sent);
+ ntohl (fc->last_pid_recv.pid),
+ ntohl (fc->last_ack_sent.pid));
LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
fc->last_pid_sent, fc->last_ack_recv);
LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
static void
fc_init (struct CadetFlowControl *fc)
{
- fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
- fc->last_pid_recv = (uint32_t) -1;
- fc->last_ack_sent = (uint32_t) 0;
- fc->last_ack_recv = (uint32_t) 0;
+ fc->next_pid.pid = 0;
+ fc->last_pid_sent.pid = htonl (UINT32_MAX);
+ fc->last_pid_recv.pid = htonl (UINT32_MAX);
+ fc->last_ack_sent.pid = (uint32_t) 0;
+ fc->last_ack_recv.pid = (uint32_t) 0;
fc->poll_task = NULL;
fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
fc->queue_n = 0;
* Find a connection.
*
* @param cid Connection ID.
+ *
+ * @return conntection with the given ID @cid or NULL if not found.
*/
static struct CadetConnection *
-connection_get (const struct GNUNET_CADET_Hash *cid)
+connection_get (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
{
- return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (cid));
+ return GNUNET_CONTAINER_multishortmap_get (connections,
+ &cid->connection_of_tunnel);
}
/**
- * Callback called when a queued ACK message is sent.
+ * Mark a connection as "destroyed", to send all pending traffic and freeing
+ * all associated resources, without accepting new status changes on it.
*
- * @param cls Closure (FC).
- * @param c Connection this message was on.
- * @param q Queue handler this call invalidates.
- * @param type Type of message sent.
- * @param fwd Was this a FWD going message?
- * @param size Size of the message.
+ * @param c Connection to mark as destroyed.
*/
static void
-ack_sent (void *cls,
- struct CadetConnection *c,
- struct CadetConnectionQueue *q,
- uint16_t type, int fwd, size_t size)
+mark_destroyed (struct CadetConnection *c)
{
- struct CadetFlowControl *fc = cls;
-
- fc->ack_msg = NULL;
+ c->destroy = GNUNET_YES;
+ connection_change_state (c, CADET_CONNECTION_DESTROYED);
}
+/**
+ * Function called if a connection has been stalled for a while,
+ * possibly due to a missed ACK. Poll the neighbor about its ACK status.
+ *
+ * @param cls Closure (poll ctx).
+ */
+static void
+send_poll (void *cls);
+
+
/**
* Send an ACK on the connection, informing the predecessor about
* the available buffer space. Should not be called in case the peer
* @param force Don't optimize out.
*/
static void
-send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
+send_ack (struct CadetConnection *c,
+ unsigned int buffer,
+ int fwd,
+ int force)
{
+ static struct CadetEncryptedMessageIdentifier zero;
struct CadetFlowControl *next_fc;
struct CadetFlowControl *prev_fc;
- struct GNUNET_CADET_ACK msg;
- uint32_t ack;
+ struct GNUNET_CADET_ConnectionEncryptedAckMessage msg;
+ struct CadetEncryptedMessageIdentifier ack_cemi;
int delta;
- /* If origin, there is no connection to send ACKs. Wrong function! */
GCC_check_connections ();
- if (GCC_is_origin (c, fwd))
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n",
- GCC_2s (c), GC_f2s (fwd));
- GNUNET_break (0);
- return;
- }
+ GNUNET_assert (GNUNET_NO == GCC_is_origin (c, fwd));
next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
- LOG (GNUNET_ERROR_TYPE_DEBUG, "connection send %s ack on %s\n",
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "send %s ack on %s\n",
GC_f2s (fwd), GCC_2s (c));
/* Check if we need to transmit the ACK. */
- delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
+ delta = ntohl (prev_fc->last_ack_sent.pid) - ntohl (prev_fc->last_pid_recv.pid);
if (3 < delta && buffer < delta && GNUNET_NO == force)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, delta > 3\n");
LOG (GNUNET_ERROR_TYPE_DEBUG,
" last pid recv: %u, last ack sent: %u\n",
- prev_fc->last_pid_recv, prev_fc->last_ack_sent);
+ ntohl (prev_fc->last_pid_recv.pid),
+ ntohl (prev_fc->last_ack_sent.pid));
GCC_check_connections ();
return;
}
/* Ok, ACK might be necessary, what PID to ACK? */
- ack = prev_fc->last_pid_recv + buffer;
- LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
+ ack_cemi.pid = htonl (ntohl (prev_fc->last_pid_recv.pid) + buffer);
LOG (GNUNET_ERROR_TYPE_DEBUG,
- " last pid %u, last ack %u, qmax %u, q %u\n",
- prev_fc->last_pid_recv, prev_fc->last_ack_sent,
+ " ACK %u, last PID %u, last ACK %u, qmax %u, q %u\n",
+ ntohl (ack_cemi.pid),
+ ntohl (prev_fc->last_pid_recv.pid),
+ ntohl (prev_fc->last_ack_sent.pid),
next_fc->queue_max, next_fc->queue_n);
- if (ack == prev_fc->last_ack_sent && GNUNET_NO == force)
+ if ( (ack_cemi.pid == prev_fc->last_ack_sent.pid) &&
+ (GNUNET_NO == force) )
{
LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
GCC_check_connections ();
/* Check if message is already in queue */
if (NULL != prev_fc->ack_msg)
{
- if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent))
+ if (GC_is_pid_bigger (ntohl (ack_cemi.pid),
+ ntohl (prev_fc->last_ack_sent.pid)))
{
LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
GCC_cancel (prev_fc->ack_msg);
return;
}
}
-
- prev_fc->last_ack_sent = ack;
+ GNUNET_break (GC_is_pid_bigger (ntohl (ack_cemi.pid),
+ ntohl (prev_fc->last_ack_sent.pid)));
+ prev_fc->last_ack_sent = ack_cemi;
/* Build ACK message and send on conn */
msg.header.size = htons (sizeof (msg));
- msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK);
- msg.ack = htonl (ack);
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK);
+ msg.cemi_max = ack_cemi;
msg.cid = c->id;
- prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, UINT16_MAX, ack,
- c, !fwd, GNUNET_YES,
- &ack_sent, prev_fc);
+ prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header,
+ UINT16_MAX,
+ zero,
+ c,
+ !fwd,
+ GNUNET_YES,
+ NULL, NULL);
GNUNET_assert (NULL != prev_fc->ack_msg);
GCC_check_connections ();
}
+/**
+ * Update performance information if we are a connection's endpoint.
+ *
+ * @param c Connection to update.
+ * @param wait How much time did we wait to send the last message.
+ * @param size Size of the last message.
+ */
+static void
+update_perf (struct CadetConnection *c,
+ struct GNUNET_TIME_Relative wait,
+ uint16_t size)
+{
+ struct CadetConnectionPerformance *p;
+ double usecsperbyte;
+
+ if (NULL == c->perf)
+ return; /* Only endpoints are interested in timing. */
+
+ p = c->perf;
+ usecsperbyte = ((double) wait.rel_value_us) / size;
+ if (p->size == AVG_MSGS)
+ {
+ /* Array is full. Substract oldest value, add new one and store. */
+ p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
+ p->usecsperbyte[p->idx] = usecsperbyte;
+ p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
+ }
+ else
+ {
+ /* Array not yet full. Add current value to avg and store. */
+ p->usecsperbyte[p->idx] = usecsperbyte;
+ p->avg *= p->size;
+ p->avg += p->usecsperbyte[p->idx];
+ p->size++;
+ p->avg /= p->size;
+ }
+ p->idx = (p->idx + 1) % AVG_MSGS;
+}
+
+
/**
* Callback called when a connection queued message is sent.
*
* Calculates the average time and connection packet tracking.
*
- * @param cls Closure (ConnectionQueue Handle).
+ * @param cls Closure (ConnectionQueue Handle), can be NULL.
* @param c Connection this message was on.
+ * @param fwd Was this a FWD going message?
* @param sent Was it really sent? (Could have been canceled)
* @param type Type of message sent.
- * @param pid Packet ID, or 0 if not applicable (create, destroy, etc).
- * @param fwd Was this a FWD going message?
+ * @param payload_type Type of payload, if applicable.
+ * @param pid Message ID, or 0 if not applicable (create, destroy, etc).
* @param size Size of the message.
* @param wait Time spent waiting for core (only the time for THIS message)
- * @return #GNUNET_YES if connection was destroyed, #GNUNET_NO otherwise.
*/
-static int
+static void
conn_message_sent (void *cls,
- struct CadetConnection *c, int sent,
- uint16_t type, uint32_t pid, int fwd, size_t size,
+ struct CadetConnection *c,
+ int fwd,
+ int sent,
+ uint16_t type,
+ uint16_t payload_type,
+ struct CadetEncryptedMessageIdentifier pid,
+ size_t size,
struct GNUNET_TIME_Relative wait)
{
- struct CadetConnectionPerformance *p;
- struct CadetFlowControl *fc;
struct CadetConnectionQueue *q = cls;
- double usecsperbyte;
+ struct CadetFlowControl *fc;
int forced;
GCC_check_connections ();
- LOG (GNUNET_ERROR_TYPE_DEBUG, "connection message_sent\n");
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ ">>> %s (%s %4u) on conn %s (%p) %s [%5u] in queue %s\n",
+ GC_m2s (type), GC_m2s (payload_type),
+ ntohl (pid.pid),
+ GCC_2s (c),
+ c,
+ GC_f2s (fwd), size,
+ GNUNET_STRINGS_relative_time_to_string (wait, GNUNET_YES));
+
+ /* If c is NULL, nothing to update. */
+ if (NULL == c)
+ {
+ if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
+ && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
+ {
+ LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
+ GC_m2s (type));
+ }
+ GCC_check_connections ();
+ return;
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
+ sent ? "" : "not ", GC_f2s (fwd),
+ GC_m2s (type), GC_m2s (payload_type),
+ ntohl (pid.pid));
GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
+ /* Update flow control info. */
fc = fwd ? &c->fwd_fc : &c->bck_fc;
- LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
- sent ? "" : "not ", GC_f2s (fwd), GC_m2s (type), pid);
+
if (NULL != q)
{
+ GNUNET_CONTAINER_DLL_remove (fc->q_head, fc->q_tail, q);
forced = q->forced;
if (NULL != q->cont)
{
}
GNUNET_free (q);
}
- else if (type == GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED
- || type == GNUNET_MESSAGE_TYPE_CADET_AX)
+ else /* CONN_CREATE or CONN_ACK */
{
- /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
+ GNUNET_assert (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED != type);
forced = GNUNET_YES;
}
- else
- {
- forced = GNUNET_NO;
- }
- if (NULL == c)
- {
- if (type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
- && type != GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY)
- {
- LOG (GNUNET_ERROR_TYPE_ERROR, "Message %s sent on NULL connection!\n",
- GC_m2s (type));
- }
- GCC_check_connections ();
- return GNUNET_NO;
- }
+
LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P- %p %u\n", c, c->pending_messages);
c->pending_messages--;
if ( (GNUNET_YES == c->destroy) &&
"! destroying connection!\n");
GCC_destroy (c);
GCC_check_connections ();
- return GNUNET_YES;
+ return;
}
+
/* Send ACK if needed, after accounting for sent ID in fc->queue_n */
switch (type)
{
case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
- case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
+ case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK:
c->maintenance_q = NULL;
/* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
schedule_next_keepalive (c, fwd);
break;
- case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
- case GNUNET_MESSAGE_TYPE_CADET_AX:
+ case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED:
if (GNUNET_YES == sent)
{
- GNUNET_assert (NULL != q);
fc->last_pid_sent = pid;
- if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
+ if (GC_is_pid_bigger (ntohl (fc->last_pid_sent.pid) + 1,
+ ntohl (fc->last_ack_recv.pid)) )
GCC_start_poll (c, fwd);
GCC_send_ack (c, fwd, GNUNET_NO);
connection_reset_timeout (c, fwd);
{
fc->queue_n--;
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "! accounting pid %u\n",
- fc->last_pid_sent);
+ "! accounting pid %u\n",
+ ntohl (fc->last_pid_sent.pid));
}
else
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"! forced, Q_N not accounting pid %u\n",
- fc->last_pid_sent);
+ ntohl (fc->last_pid_sent.pid));
}
break;
- case GNUNET_MESSAGE_TYPE_CADET_KX:
+ case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX:
if (GNUNET_YES == sent)
connection_reset_timeout (c, fwd);
break;
- case GNUNET_MESSAGE_TYPE_CADET_POLL:
+ case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL:
fc->poll_msg = NULL;
+ if (2 == c->destroy)
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
+ return;
+ }
+ if (0 == fc->queue_max)
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL cancelled: neighbor disconnected\n");
+ return;
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
+ GCC_2s (c));
+ GNUNET_assert (NULL == fc->poll_task);
+ fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
+ fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
+ &send_poll, fc);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
break;
- case GNUNET_MESSAGE_TYPE_CADET_ACK:
+ case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK:
fc->ack_msg = NULL;
break;
}
LOG (GNUNET_ERROR_TYPE_DEBUG, "! message sent!\n");
- if (NULL == c->perf)
- return GNUNET_NO; /* Only endpoints are interested in timing. */
-
- p = c->perf;
- usecsperbyte = ((double) wait.rel_value_us) / size;
- if (p->size == AVG_MSGS)
- {
- /* Array is full. Substract oldest value, add new one and store. */
- p->avg -= (p->usecsperbyte[p->idx] / AVG_MSGS);
- p->usecsperbyte[p->idx] = usecsperbyte;
- p->avg += (p->usecsperbyte[p->idx] / AVG_MSGS);
- }
- else
- {
- /* Array not yet full. Add current value to avg and store. */
- p->usecsperbyte[p->idx] = usecsperbyte;
- p->avg *= p->size;
- p->avg += p->usecsperbyte[p->idx];
- p->size++;
- p->avg /= p->size;
- }
- p->idx = (p->idx + 1) % AVG_MSGS;
+ update_perf (c, wait, size);
GCC_check_connections ();
- return GNUNET_NO;
}
*/
static int
check_connection (void *cls,
- const struct GNUNET_HashCode *key,
+ const struct GNUNET_ShortHashCode *key,
void *value)
{
struct CadetConnection *c = value;
return;
if (NULL == connections)
return;
- GNUNET_CONTAINER_multihashmap_iterate (connections,
- &check_connection,
- NULL);
+ GNUNET_CONTAINER_multishortmap_iterate (connections,
+ &check_connection,
+ NULL);
}
* @param ooo_pid PID of the out-of-order message.
*/
static uint32_t
-get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid)
+get_recv_bitmask (struct CadetEncryptedMessageIdentifier last_pid_recv,
+ struct CadetEncryptedMessageIdentifier ooo_pid)
{
- return 1 << (last_pid_recv - ooo_pid);
+ // FIXME: should assert that the delta is in range...
+ return 1 << (ntohl (last_pid_recv.pid) - ntohl (ooo_pid.pid));
}
* @param last_pid_recv Last in-order PID received.
*/
static int
-is_ooo_ok (uint32_t last_pid_recv, uint32_t ooo_pid, uint32_t ooo_bitmap)
+is_ooo_ok (struct CadetEncryptedMessageIdentifier last_pid_recv,
+ struct CadetEncryptedMessageIdentifier ooo_pid,
+ uint32_t ooo_bitmap)
{
uint32_t mask;
- if (GC_is_pid_bigger (last_pid_recv - 31, ooo_pid))
+ if (GC_is_pid_bigger (ntohl (last_pid_recv.pid) - 31,
+ ntohl (ooo_pid.pid)))
return GNUNET_NO;
- mask = get_recv_bitmask (last_pid_recv, ooo_pid);
+ mask = get_recv_bitmask (last_pid_recv,
+ ooo_pid);
if (0 != (ooo_bitmap & mask))
return GNUNET_NO;
* Is traffic coming from this sender 'FWD' traffic?
*
* @param c Connection to check.
- * @param sender Peer identity of neighbor.
+ * @param sender Short peer identity of neighbor.
*
* @return #GNUNET_YES in case the sender is the 'prev' hop and therefore
* the traffic is 'FWD'.
* #GNUNET_NO for BCK.
- * #GNUNET_SYSERR for errors.
+ * #GNUNET_SYSERR for errors (sender isn't a hop in the connection).
*/
static int
is_fwd (const struct CadetConnection *c,
- const struct GNUNET_PeerIdentity *sender)
+ const struct CadetPeer *sender)
{
GNUNET_PEER_Id id;
- id = GNUNET_PEER_search (sender);
+ id = GCP_get_short_id (sender);
if (GCP_get_short_id (get_prev_hop (c)) == id)
return GNUNET_YES;
if (GCP_get_short_id (get_next_hop (c)) == id)
return GNUNET_NO;
- GNUNET_break (0);
return GNUNET_SYSERR;
}
* Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
* or a first CONNECTION_ACK directed to us.
*
- * @param connection Connection to confirm.
+ * @param c Connection to confirm.
* @param fwd Should we send it FWD? (root->dest)
* (First (~SYNACK) goes BCK, second (~ACK) goes FWD)
*/
static void
-send_connection_ack (struct CadetConnection *connection, int fwd)
+send_connection_ack (struct CadetConnection *c, int fwd)
{
+ static struct CadetEncryptedMessageIdentifier zero;
+ struct GNUNET_CADET_ConnectionCreateAckMessage msg;
struct CadetTunnel *t;
- size_t size = sizeof (struct GNUNET_CADET_ConnectionACK);
+ const uint16_t size = sizeof (struct GNUNET_CADET_ConnectionCreateAckMessage);
+ const uint16_t type = GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK;
GCC_check_connections ();
- t = connection->t;
+ t = c->t;
LOG (GNUNET_ERROR_TYPE_INFO,
- "==> { C %s ACK} %19s on conn %s (%p) %s [%5u]\n",
- GC_f2s (!fwd), "", GCC_2s (connection), connection, GC_f2s (fwd), size);
- GCP_queue_add (get_hop (connection, fwd), NULL,
- GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, UINT16_MAX, 0,
- size, connection, fwd, &conn_message_sent, NULL);
- connection->pending_messages++;
+ "==> %s ({ C %s ACK} 0) on conn %s (%p) %s [%5u]\n",
+ GC_m2s (type), GC_f2s (!fwd), GCC_2s (c), c, GC_f2s (fwd), size);
+
+ msg.header.size = htons (size);
+ msg.header.type = htons (type);
+ msg.reserved = htonl (0);
+ msg.cid = c->id;
+
+ GNUNET_assert (NULL == c->maintenance_q);
+ c->maintenance_q = GCP_send (get_hop (c, fwd),
+ &msg.header,
+ GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK,
+ zero,
+ c,
+ fwd,
+ &conn_message_sent, NULL);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (conn`ACK)\n",
+ c, c->pending_messages);
+ c->pending_messages++;
+
if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
GCT_change_cstate (t, CADET_TUNNEL_WAITING);
- if (CADET_CONNECTION_READY != connection->state)
- connection_change_state (connection, CADET_CONNECTION_SENT);
+ if (CADET_CONNECTION_READY != c->state)
+ connection_change_state (c, CADET_CONNECTION_SENT);
GCC_check_connections ();
}
const struct GNUNET_PeerIdentity *id2,
int fwd)
{
- struct GNUNET_CADET_ConnectionBroken msg;
+ static struct CadetEncryptedMessageIdentifier zero;
+ struct GNUNET_CADET_ConnectionBrokenMessage msg;
GCC_check_connections ();
- msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
+ msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBrokenMessage));
msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
msg.cid = c->id;
+ msg.reserved = htonl (0);
msg.peer1 = *id1;
msg.peer2 = *id2;
- GNUNET_assert (NULL ==
- GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c, fwd,
- GNUNET_YES, NULL, NULL));
+ (void) GCC_send_prebuilt_message (&msg.header,
+ UINT16_MAX,
+ zero,
+ c,
+ fwd,
+ GNUNET_YES,
+ NULL, NULL);
GCC_check_connections ();
}
* @param connection_id Connection ID.
* @param id1 Peer that has disconnected, probably local peer.
* @param id2 Peer that has disconnected can be NULL if unknown.
- * @param peer Peer to notify (neighbor who sent the connection).
+ * @param neighbor Peer to notify (neighbor who sent the connection).
*/
static void
-send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
+send_broken_unknown (const struct GNUNET_CADET_ConnectionTunnelIdentifier *connection_id,
const struct GNUNET_PeerIdentity *id1,
const struct GNUNET_PeerIdentity *id2,
- const struct GNUNET_PeerIdentity *peer_id)
+ struct CadetPeer *neighbor)
{
- struct GNUNET_CADET_ConnectionBroken *msg;
- struct CadetPeer *neighbor;
+ static struct CadetEncryptedMessageIdentifier zero;
+ struct GNUNET_CADET_ConnectionBrokenMessage msg;
GCC_check_connections ();
LOG (GNUNET_ERROR_TYPE_INFO, "--> BROKEN on unknown connection %s\n",
- GNUNET_h2s (GC_h2hc (connection_id)));
+ GNUNET_sh2s (&connection_id->connection_of_tunnel));
- msg = GNUNET_new (struct GNUNET_CADET_ConnectionBroken);
- msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken));
- msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
- msg->cid = *connection_id;
- msg->peer1 = *id1;
+ msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBrokenMessage));
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
+ msg.cid = *connection_id;
+ msg.reserved = htonl (0);
+ msg.peer1 = *id1;
if (NULL != id2)
- msg->peer2 = *id2;
+ msg.peer2 = *id2;
else
- memset (&msg->peer2, 0, sizeof (msg->peer2));
- neighbor = GCP_get (peer_id, GNUNET_NO); /* We MUST know neighbor. */
- GNUNET_assert (NULL != neighbor);
- GCP_queue_add (neighbor, msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
- UINT16_MAX, 2, sizeof (struct GNUNET_CADET_ConnectionBroken),
- NULL, GNUNET_SYSERR, /* connection, fwd */
- NULL, NULL); /* continuation */
+ memset (&msg.peer2, 0, sizeof (msg.peer2));
+ GNUNET_assert (NULL != GCP_send (neighbor,
+ &msg.header,
+ UINT16_MAX,
+ zero,
+ NULL,
+ GNUNET_SYSERR, /* connection, fwd */
+ NULL, NULL)); /* continuation */
GCC_check_connections ();
}
{
struct GNUNET_MessageHeader msg;
struct CadetFlowControl *fc;
+ int tunnel_ready;
GCC_check_connections ();
LOG (GNUNET_ERROR_TYPE_INFO,
GNUNET_assert (NULL != c->t);
fc = fwd ? &c->fwd_fc : &c->bck_fc;
- if (0 < fc->queue_n || GNUNET_YES == GCT_has_queued_traffic (c->t))
+ tunnel_ready = GNUNET_YES == GCT_has_queued_traffic (c->t)
+ && CADET_TUNNEL_KEY_OK <= GCT_get_estate (c->t);
+ if (0 < fc->queue_n || tunnel_ready)
{
LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
return;
GNUNET_assert (NULL != c->t);
msg.size = htons (sizeof (msg));
- msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
+ msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE);
GNUNET_assert (NULL ==
GCT_send_prebuilt_message (&msg, c->t, c,
*
* @param c Connection to keep alive.
* @param fwd Direction.
- * @param shutdown Are we shutting down? (Don't send traffic)
- * Non-zero value for true, not necessarily GNUNET_YES.
*/
static void
-connection_keepalive (struct CadetConnection *c, int fwd, int shutdown)
+connection_keepalive (struct CadetConnection *c,
+ int fwd)
{
GCC_check_connections ();
LOG (GNUNET_ERROR_TYPE_DEBUG,
c->fwd_maintenance_task = NULL;
else
c->bck_maintenance_task = NULL;
-
- if (GNUNET_NO != shutdown)
- return;
-
connection_maintain (c, fwd);
GCC_check_connections ();
/* Next execution will be scheduled by message_sent or _maintain*/
* Keep the connection alive in the FWD direction.
*
* @param cls Closure (connection to keepalive).
- * @param tc TaskContext.
*/
static void
-connection_fwd_keepalive (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
+connection_fwd_keepalive (void *cls)
{
+ struct CadetConnection *c = cls;
+
GCC_check_connections ();
- connection_keepalive ((struct CadetConnection *) cls,
- GNUNET_YES,
- tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
+ connection_keepalive (c,
+ GNUNET_YES);
GCC_check_connections ();
}
* Keep the connection alive in the BCK direction.
*
* @param cls Closure (connection to keepalive).
- * @param tc TaskContext.
*/
static void
-connection_bck_keepalive (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
+connection_bck_keepalive (void *cls)
{
+ struct CadetConnection *c = cls;
+
GCC_check_connections ();
- connection_keepalive ((struct CadetConnection *) cls,
- GNUNET_NO,
- tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN);
+ connection_keepalive (c,
+ GNUNET_NO);
GCC_check_connections ();
}
{
if (1 > c->create_retry)
c->create_retry = 1;
- delay = GNUNET_TIME_relative_multiply (create_connection_time,
- c->create_retry);
- if (c->create_retry < 64)
+ delay = GNUNET_TIME_relative_saturating_multiply (create_connection_time,
+ c->create_retry);
+ if (c->create_retry < 64) // TODO make configurable
c->create_retry *= 2;
}
*task_id = GNUNET_SCHEDULER_add_delayed (delay,
keepalive_task,
c);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "next keepalive in %s\n",
- GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
- GCC_check_connections ();
-}
-
-
-/**
- * @brief Re-initiate traffic on this connection if necessary.
- *
- * Check if there is traffic queued towards this peer
- * and the core transmit handle is NULL (traffic was stalled).
- * If so, call core tmt rdy.
- *
- * @param c Connection on which initiate traffic.
- * @param fwd Is this about fwd traffic?
- */
-static void
-connection_unlock_queue (struct CadetConnection *c, int fwd)
-{
- struct CadetPeer *peer;
-
- GCC_check_connections ();
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "connection_unlock_queue %s on %s\n",
- GC_f2s (fwd), GCC_2s (c));
-
- if (GCC_is_terminal (c, fwd))
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal, can unlock!\n");
- return;
- }
-
- peer = get_hop (c, fwd);
- GCP_queue_unlock (peer, c);
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ "next keepalive for %s in in %s\n",
+ GCC_2s (c), GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
GCC_check_connections ();
}
int fwd)
{
struct CadetFlowControl *fc;
- struct CadetPeer *peer;
GCC_check_connections ();
LOG (GNUNET_ERROR_TYPE_DEBUG,
GCC_cancel (fc->poll_msg);
LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL msg for fc %p\n", fc);
}
- peer = get_hop (c, fwd);
- GCP_queue_cancel (peer, c);
- GCC_check_connections ();
-}
-
-
-/**
- * Function called if a connection has been stalled for a while,
- * possibly due to a missed ACK. Poll the neighbor about its ACK status.
- *
- * @param cls Closure (poll ctx).
- * @param tc TaskContext.
- */
-static void
-connection_poll (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc);
-
-
-/**
- * Callback called when a queued POLL message is sent.
- *
- * @param cls Closure (flow control context).
- * @param c Connection this message was on.
- * @param q Queue handler this call invalidates.
- * @param type Type of message sent.
- * @param fwd Was this a FWD going message?
- * @param size Size of the message.
- */
-static void
-poll_sent (void *cls,
- struct CadetConnection *c,
- struct CadetConnectionQueue *q,
- uint16_t type, int fwd, size_t size)
-{
- struct CadetFlowControl *fc = cls;
- GNUNET_assert (fc->poll_msg == q);
- fc->poll_msg = NULL;
- if (2 == c->destroy)
+ while (NULL != fc->q_head)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL canceled on shutdown\n");
- return;
+ GCC_cancel (fc->q_head);
}
- LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL sent for %s, scheduling new one!\n",
- GCC_2s (c));
- GNUNET_assert (NULL == fc->poll_task);
- fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
- fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
- &connection_poll,
- fc);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
+ GCC_check_connections ();
}
* possibly due to a missed ACK. Poll the neighbor about its ACK status.
*
* @param cls Closure (poll ctx).
- * @param tc TaskContext.
*/
static void
-connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+send_poll (void *cls)
{
+ static struct CadetEncryptedMessageIdentifier zero;
struct CadetFlowControl *fc = cls;
- struct GNUNET_CADET_Poll msg;
+ struct GNUNET_CADET_ConnectionHopByHopPollMessage msg;
struct CadetConnection *c;
int fwd;
fc->poll_task = NULL;
GCC_check_connections ();
- if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
- {
- return;
- }
-
c = fc->c;
fwd = fc == &c->fwd_fc;
LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
GCC_2s (c), GC_f2s (fwd));
- msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL);
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL);
msg.header.size = htons (sizeof (msg));
- msg.pid = htonl (fc->last_pid_sent);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent);
- fc->poll_msg =
- GCC_send_prebuilt_message (&msg.header, UINT16_MAX, fc->last_pid_sent, c,
- fc == &c->fwd_fc, GNUNET_YES, &poll_sent, fc);
+ msg.cid = c->id;
+ msg.cemi = fc->last_pid_sent;
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", ntohl (fc->last_pid_sent.pid));
+ fc->poll_msg
+ = GCC_send_prebuilt_message (&msg.header,
+ UINT16_MAX,
+ zero,
+ c,
+ fc == &c->fwd_fc,
+ GNUNET_YES,
+ NULL,
+ NULL);
GNUNET_assert (NULL != fc->poll_msg);
GCC_check_connections ();
}
/**
- * Resend all queued messages for a connection on other connections of the
- * same tunnel, if possible. The connection WILL BE DESTROYED by this function.
- *
- * @param c Connection whose messages to resend.
- * @param fwd Resend fwd messages?
- */
-static void
-resend_messages_and_destroy (struct CadetConnection *c, int fwd)
-{
- struct GNUNET_MessageHeader *out_msg;
- struct CadetTunnel *t = c->t;
- struct CadetPeer *neighbor;
- unsigned int pending;
- int destroyed;
-
- GCC_check_connections ();
- c->state = CADET_CONNECTION_DESTROYED;
- c->destroy = GNUNET_YES;
-
- destroyed = GNUNET_NO;
- neighbor = get_hop (c, fwd);
- pending = c->pending_messages;
-
- while (NULL != (out_msg = GCP_connection_pop (neighbor, c, &destroyed)))
- {
- if (NULL != t)
- GCT_resend_message (out_msg, t);
- GNUNET_free (out_msg);
- }
-
- /* All pending messages should have been popped,
- * and the connection destroyed by the continuation.
- */
- if (GNUNET_YES != destroyed)
- {
- if (0 != pending)
- {
- GNUNET_break (0);
- GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
- if (NULL != t) GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
- }
- GCC_destroy (c);
- }
- GCC_check_connections ();
-}
-
-
-/**
- * Generic connection timeout implementation.
+ * Generic connection timeout implementation.
*
* Timeout function due to lack of keepalive/traffic from an endpoint.
* Destroys connection if called.
static void
connection_timeout (struct CadetConnection *c, int fwd)
{
- struct CadetFlowControl *reverse_fc;
-
GCC_check_connections ();
- reverse_fc = fwd ? &c->bck_fc : &c->fwd_fc;
LOG (GNUNET_ERROR_TYPE_INFO,
"Connection %s %s timed out. Destroying.\n",
return;
}
- /* If dest, salvage queued traffic. */
- if (GCC_is_origin (c, !fwd))
+ /* If dest, send "broken" notification. */
+ if (GCC_is_terminal (c, fwd))
{
- const struct GNUNET_PeerIdentity *next_hop;
+ struct CadetPeer *next_hop;
- next_hop = GCP_get_id (fwd ? get_prev_hop (c) : get_next_hop (c));
+ next_hop = fwd ? get_prev_hop (c) : get_next_hop (c);
send_broken_unknown (&c->id, &my_full_id, NULL, next_hop);
- if (0 < reverse_fc->queue_n)
- resend_messages_and_destroy (c, !fwd);
- GCC_check_connections ();
- return;
}
GCC_destroy (c);
* Destroys connection if called.
*
* @param cls Closure (connection to destroy).
- * @param tc TaskContext.
*/
static void
-connection_fwd_timeout (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
+connection_fwd_timeout (void *cls)
{
struct CadetConnection *c = cls;
c->fwd_maintenance_task = NULL;
- if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
- return;
GCC_check_connections ();
connection_timeout (c, GNUNET_YES);
GCC_check_connections ();
* Destroys connection if called.
*
* @param cls Closure (connection to destroy).
- * @param tc TaskContext
*/
static void
-connection_bck_timeout (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
+connection_bck_timeout (void *cls)
{
struct CadetConnection *c = cls;
c->bck_maintenance_task = NULL;
- if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
- return;
GCC_check_connections ();
connection_timeout (c, GNUNET_NO);
GCC_check_connections ();
if (GCC_is_origin (c, fwd)) /* Startpoint */
{
schedule_next_keepalive (c, fwd);
+ if (NULL != c->maintenance_q)
+ {
+ GCP_send_cancel (c->maintenance_q);
+ c->maintenance_q = NULL; /* Is set to NULL by conn_message_sent anyway */
+ }
}
else /* Relay, endpoint. */
{
if (NULL != *ti)
GNUNET_SCHEDULER_cancel (*ti);
- delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
+ delay = GNUNET_TIME_relative_saturating_multiply (refresh_connection_time, 4);
LOG (GNUNET_ERROR_TYPE_DEBUG,
" timing out in %s\n",
GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
/**
* Iterator to compare each connection's path with the path of a new connection.
*
- * If the connection conincides, the c member of path is set to the connection
+ * If the connection coincides, the c member of path is set to the connection
* and the destroy flag of the connection is set.
*
* @param cls Closure (new path).
GCC_2s (c), c, c->path->length);
if (c != new_conn
- && c->destroy == GNUNET_NO
- && c->state != CADET_CONNECTION_BROKEN
- && c->state != CADET_CONNECTION_DESTROYED
+ && GNUNET_NO == c->destroy
+ && CADET_CONNECTION_BROKEN != c->state
+ && CADET_CONNECTION_DESTROYED != c->state
&& path_equivalent (path, c->path))
{
- new_conn->destroy = GNUNET_YES;
- new_conn->path->c = c;
+ new_conn->destroy = GNUNET_YES; /* Do not mark_destroyed, */
+ new_conn->path->c = c; /* this is only a flag for the Iterator. */
LOG (GNUNET_ERROR_TYPE_DEBUG, " MATCH!\n");
}
}
if (CADET_CONNECTION_READY == c->state)
{
/* The other peer confirmed a live connection with this path,
- * why is it trying to duplicate it. */
+ * why are they trying to duplicate it? */
GNUNET_STATISTICS_update (stats, "# duplicate connections", 1, GNUNET_NO);
return GNUNET_YES;
}
- LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate not valid, connection unique\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate not ready, connection unique\n");
return GNUNET_NO;
}
else
* connection with the same path, and destroy one if so.
*
* @param cls Closure (connection to check).
- * @param tc Task context.
*/
static void
-check_duplicates (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+check_duplicates (void *cls)
{
struct CadetConnection *c = cls;
c->check_duplicates_task = NULL;
- if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
- return;
-
if (GNUNET_YES == does_connection_exist (c))
{
GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
}
-
/**
* Wait for enough time to let any dead connections time out and check for
* any remaining duplicates.
if (NULL != c->check_duplicates_task)
return;
-
- delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 5);
+ delay = GNUNET_TIME_relative_saturating_multiply (refresh_connection_time, 5);
c->check_duplicates_task = GNUNET_SCHEDULER_add_delayed (delay,
&check_duplicates,
c);
}
-
/**
* Add the connection to the list of both neighbors.
*
* @param disconnected Peer that disconnected.
*/
static void
-invalidate_paths (struct CadetConnection *c, struct CadetPeer *disconnected)
+invalidate_paths (struct CadetConnection *c,
+ struct CadetPeer *disconnected)
{
struct CadetPeer *peer;
unsigned int i;
* Log receipt of message on stderr (INFO level).
*
* @param message Message received.
- * @param peer Peer who sent the message.
- * @param hash Connection ID.
+ * @param peer Peer who sent the message.
+ * @param conn_id Connection ID of the message.
*/
static void
log_message (const struct GNUNET_MessageHeader *message,
- const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_CADET_Hash *hash)
+ const struct CadetPeer *peer,
+ const struct GNUNET_CADET_ConnectionTunnelIdentifier *conn_id)
{
uint16_t size;
uint16_t type;
switch (type)
{
case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
- case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
+ case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK:
case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
arrow = "==";
default:
arrow = "--";
}
- LOG (GNUNET_ERROR_TYPE_INFO, "<%s %s on conn %s from %s, %6u bytes\n",
- arrow, GC_m2s (type), GNUNET_h2s (GC_h2hc (hash)),
- GNUNET_i2s (peer), (unsigned int) size);
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ "<%s %s on conn %s from %s, %6u bytes\n",
+ arrow,
+ GC_m2s (type),
+ GNUNET_sh2s (&conn_id->connection_of_tunnel),
+ GCP_2s(peer),
+ (unsigned int) size);
}
/******************************************************************************/
/******************************************************************************/
/**
- * Core handler for connection creation.
+ * Handler for connection creation.
*
- * @param cls Closure (unused).
- * @param peer Sender (neighbor).
- * @param message Message.
- * @return #GNUNET_OK to keep the connection open,
- * #GNUNET_SYSERR to close it (signal serious error)
+ * @param peer Message sender (neighbor).
+ * @param msg Message itself.
*/
-int
-GCC_handle_create (void *cls,
- const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
+void
+GCC_handle_create (struct CadetPeer *peer,
+ const struct GNUNET_CADET_ConnectionCreateMessage *msg)
{
- struct GNUNET_CADET_ConnectionCreate *msg;
+ static struct CadetEncryptedMessageIdentifier zero;
+ const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid;
struct GNUNET_PeerIdentity *id;
- struct GNUNET_CADET_Hash *cid;
struct CadetPeerPath *path;
struct CadetPeer *dest_peer;
struct CadetPeer *orig_peer;
uint16_t size;
GCC_check_connections ();
- /* Check size */
- size = ntohs (message->size);
- if (size < sizeof (struct GNUNET_CADET_ConnectionCreate))
- {
- GNUNET_break_op (0);
- return GNUNET_OK;
- }
+ size = ntohs (msg->header.size);
/* Calculate hops */
- size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
- if (size % sizeof (struct GNUNET_PeerIdentity))
- {
- GNUNET_break_op (0);
- return GNUNET_OK;
- }
+ size -= sizeof (struct GNUNET_CADET_ConnectionCreateMessage);
if (0 != size % sizeof (struct GNUNET_PeerIdentity))
{
GNUNET_break_op (0);
- return GNUNET_OK;
+ return;
}
size /= sizeof (struct GNUNET_PeerIdentity);
if (1 > size)
{
GNUNET_break_op (0);
- return GNUNET_OK;
+ return;
}
LOG (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
/* Get parameters */
- msg = (struct GNUNET_CADET_ConnectionCreate *) message;
cid = &msg->cid;
- log_message (message, peer, cid);
+ log_message (&msg->header, peer, cid);
id = (struct GNUNET_PeerIdentity *) &msg[1];
LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
/* Path was malformed, probably our own ID was not in it. */
GNUNET_STATISTICS_update (stats, "# malformed paths", 1, GNUNET_NO);
GNUNET_break_op (0);
- return GNUNET_OK;
+ return;
}
-
if (0 == own_pos)
{
/* We received this request from a neighbor, we cannot be origin */
GNUNET_STATISTICS_update (stats, "# fake paths", 1, GNUNET_NO);
GNUNET_break_op (0);
path_destroy (path);
- return GNUNET_OK;
+ return;
}
LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
GNUNET_break (0);
path_destroy (path);
GCC_check_connections ();
- return GNUNET_OK;
+ return;
}
send_broken_unknown (cid, &my_full_id,
GNUNET_PEER_resolve2 (path->peers[own_pos + 1]),
peer);
path_destroy (path);
GCC_check_connections ();
- return GNUNET_OK;
+ return;
}
GCP_add_path_to_all (path, GNUNET_NO);
connection_reset_timeout (c, GNUNET_YES);
if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
-
- send_connection_ack (c, GNUNET_NO);
+ if (NULL == c->maintenance_q)
+ send_connection_ack (c, GNUNET_NO);
if (CADET_CONNECTION_SENT == c->state)
connection_change_state (c, CADET_CONNECTION_ACK);
}
else
{
- /* It's for somebody else! Retransmit. */
- LOG (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
- GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c,
- GNUNET_YES, GNUNET_YES,
- NULL, NULL));
+ (void) GCC_send_prebuilt_message (&msg->header,
+ 0,
+ zero,
+ c,
+ GNUNET_YES, GNUNET_YES,
+ NULL, NULL);
}
path_destroy (path);
GCC_check_connections ();
- return GNUNET_OK;
}
/**
- * Core handler for path confirmations.
+ * Handler for connection confirmations.
*
- * @param cls closure
- * @param message message
- * @param peer peer identity this notification is about
- * @return #GNUNET_OK to keep the connection open,
- * #GNUNET_SYSERR to close it (signal serious error)
+ * @param peer Message sender (neighbor).
+ * @param msg Message itself.
*/
-int
-GCC_handle_confirm (void *cls,
- const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
+void
+GCC_handle_confirm (struct CadetPeer *peer,
+ const struct GNUNET_CADET_ConnectionCreateAckMessage *msg)
{
- struct GNUNET_CADET_ConnectionACK *msg;
+ static struct CadetEncryptedMessageIdentifier zero;
struct CadetConnection *c;
- struct CadetPeerPath *p;
- struct CadetPeer *pi;
enum CadetConnectionState oldstate;
int fwd;
GCC_check_connections ();
- msg = (struct GNUNET_CADET_ConnectionACK *) message;
- log_message (message, peer, &msg->cid);
+ log_message (&msg->header, peer, &msg->cid);
c = connection_get (&msg->cid);
if (NULL == c)
{
" don't know the connection!\n");
send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
GCC_check_connections ();
- return GNUNET_OK;
+ return;
}
-
if (GNUNET_NO != c->destroy)
{
- GNUNET_assert (CADET_CONNECTION_DESTROYED != c->state);
+ GNUNET_assert (CADET_CONNECTION_DESTROYED == c->state);
+ GNUNET_STATISTICS_update (stats, "# control on dying connection",
+ 1, GNUNET_NO);
LOG (GNUNET_ERROR_TYPE_DEBUG,
"connection %s being destroyed, ignoring confirm\n",
GCC_2s (c));
GCC_check_connections ();
- return GNUNET_OK;
+ return;
}
oldstate = c->state;
- LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GNUNET_i2s (peer));
- pi = GCP_get (peer, GNUNET_YES);
- if (get_next_hop (c) == pi)
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", GCP_2s (peer));
+ if (get_next_hop (c) == peer)
{
LOG (GNUNET_ERROR_TYPE_DEBUG, " SYNACK\n");
fwd = GNUNET_NO;
if (CADET_CONNECTION_SENT == oldstate)
connection_change_state (c, CADET_CONNECTION_ACK);
}
- else if (get_prev_hop (c) == pi)
+ else if (get_prev_hop (c) == peer)
{
LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
fwd = GNUNET_YES;
}
else
{
+ GNUNET_STATISTICS_update (stats, "# control on connection from wrong peer",
+ 1, GNUNET_NO);
GNUNET_break_op (0);
- return GNUNET_OK;
+ return;
}
connection_reset_timeout (c, fwd);
- /* Add path to peers? */
- p = c->path;
- if (NULL != p)
- {
- GCP_add_path_to_all (p, GNUNET_YES);
- }
- else
- {
- GNUNET_break (0);
- }
+ GNUNET_assert (NULL != c->path);
+ GCP_add_path_to_all (c->path, GNUNET_YES);
/* Message for us as creator? */
- if (GCC_is_origin (c, GNUNET_YES))
+ if (GNUNET_YES == GCC_is_origin (c, GNUNET_YES))
{
if (GNUNET_NO != fwd)
{
- GNUNET_break_op (0);
- return GNUNET_OK;
+ GNUNET_break (0);
+ return;
}
LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection (SYN)ACK for us!\n");
/* If just created, cancel the short timeout and start a long one */
if (CADET_CONNECTION_SENT == oldstate)
+ {
+ c->create_retry = 1;
connection_reset_timeout (c, GNUNET_YES);
+ }
- /* Change connection state */
+ /* Change connection state, send ACK */
connection_change_state (c, CADET_CONNECTION_READY);
send_connection_ack (c, GNUNET_YES);
if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
GCT_change_cstate (c->t, CADET_TUNNEL_READY);
GCC_check_connections ();
- return GNUNET_OK;
+ return;
}
/* Message for us as destination? */
{
if (GNUNET_YES != fwd)
{
- GNUNET_break_op (0);
- return GNUNET_OK;
+ GNUNET_break (0);
+ return;
}
LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
GCT_change_cstate (c->t, CADET_TUNNEL_READY);
GCC_check_connections ();
- return GNUNET_OK;
}
-
- LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
- GNUNET_assert (NULL ==
- GCC_send_prebuilt_message (message, 0, 0, c, fwd,
- GNUNET_YES, NULL, NULL));
+ else
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
+ (void) GCC_send_prebuilt_message (&msg->header, 0,
+ zero,
+ c,
+ fwd,
+ GNUNET_YES, NULL, NULL);
+ }
GCC_check_connections ();
- return GNUNET_OK;
}
/**
- * Core handler for notifications of broken connections.
+ * Handler for notifications of broken connections.
*
- * @param cls Closure (unused).
- * @param id Peer identity of sending neighbor.
- * @param message Message.
- * @return #GNUNET_OK to keep the connection open,
- * #GNUNET_SYSERR to close it (signal serious error)
+ * @param peer Message sender (neighbor).
+ * @param msg Message itself.
*/
-int
-GCC_handle_broken (void* cls,
- const struct GNUNET_PeerIdentity* id,
- const struct GNUNET_MessageHeader* message)
+void
+GCC_handle_broken (struct CadetPeer *peer,
+ const struct GNUNET_CADET_ConnectionBrokenMessage *msg)
{
- struct GNUNET_CADET_ConnectionBroken *msg;
+ static struct CadetEncryptedMessageIdentifier zero;
struct CadetConnection *c;
struct CadetTunnel *t;
- int pending;
int fwd;
GCC_check_connections ();
- msg = (struct GNUNET_CADET_ConnectionBroken *) message;
- log_message (message, id, &msg->cid);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
- GNUNET_i2s (&msg->peer1));
- LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
- GNUNET_i2s (&msg->peer2));
+ log_message (&msg->header, peer, &msg->cid);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", GNUNET_i2s (&msg->peer1));
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", GNUNET_i2s (&msg->peer2));
c = connection_get (&msg->cid);
if (NULL == c)
{
LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
+ GNUNET_STATISTICS_update (stats, "# duplicate CONNECTION_BROKEN",
+ 1, GNUNET_NO);
GCC_check_connections ();
- return GNUNET_OK;
+ return;
}
t = c->t;
- fwd = is_fwd (c, id);
- c->destroy = GNUNET_YES;
+ fwd = is_fwd (c, peer);
+ if (GNUNET_SYSERR == fwd)
+ {
+ GNUNET_break_op (0);
+ GCC_check_connections ();
+ return;
+ }
+ mark_destroyed (c);
if (GCC_is_terminal (c, fwd))
{
struct CadetPeer *endpoint;
/* A terminal connection should not have 't' set to NULL. */
GNUNET_break (0);
GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
- return GNUNET_OK;
+ return;
}
endpoint = GCP_get_short (c->path->peers[c->path->length - 1], GNUNET_YES);
if (2 < c->path->length)
path_invalidate (c->path);
GCP_notify_broken_link (endpoint, &msg->peer1, &msg->peer2);
- c->state = CADET_CONNECTION_BROKEN;
+ connection_change_state (c, CADET_CONNECTION_BROKEN);
GCT_remove_connection (t, c);
c->t = NULL;
- pending = c->pending_messages;
- if (0 < pending)
- resend_messages_and_destroy (c, !fwd);
- else
- GCC_destroy (c);
+ GCC_destroy (c);
}
else
{
- GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
- GNUNET_YES, NULL, NULL));
+ (void) GCC_send_prebuilt_message (&msg->header, 0,
+ zero, c, fwd,
+ GNUNET_YES, NULL, NULL);
connection_cancel_queues (c, !fwd);
}
GCC_check_connections ();
- return GNUNET_OK;
+ return;
}
/**
- * Core handler for tunnel destruction
+ * Handler for notifications of destroyed connections.
*
- * @param cls Closure (unused).
- * @param peer Peer identity of sending neighbor.
- * @param message Message.
- * @return #GNUNET_OK to keep the connection open,
- * #GNUNET_SYSERR to close it (signal serious error)
+ * @param peer Message sender (neighbor).
+ * @param msg Message itself.
*/
-int
-GCC_handle_destroy (void *cls,
- const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
+void
+GCC_handle_destroy (struct CadetPeer *peer,
+ const struct GNUNET_CADET_ConnectionDestroyMessage *msg)
{
- const struct GNUNET_CADET_ConnectionDestroy *msg;
+ static struct CadetEncryptedMessageIdentifier zero;
struct CadetConnection *c;
int fwd;
GCC_check_connections ();
- msg = (const struct GNUNET_CADET_ConnectionDestroy *) message;
- log_message (message, peer, &msg->cid);
+ log_message (&msg->header, peer, &msg->cid);
c = connection_get (&msg->cid);
if (NULL == c)
{
"# control on unknown connection",
1, GNUNET_NO);
LOG (GNUNET_ERROR_TYPE_DEBUG,
- " connection unknown: already destroyed?\n");
+ " connection unknown destroyed: previously destroyed?\n");
GCC_check_connections ();
- return GNUNET_OK;
+ return;
}
+
fwd = is_fwd (c, peer);
if (GNUNET_SYSERR == fwd)
{
- GNUNET_break_op (0); /* FIXME */
- return GNUNET_OK;
+ GNUNET_break_op (0);
+ GCC_check_connections ();
+ return;
}
+
if (GNUNET_NO == GCC_is_terminal (c, fwd))
{
- GNUNET_assert (NULL ==
- GCC_send_prebuilt_message (message, 0, 0, c, fwd,
- GNUNET_YES, NULL, NULL));
+ (void) GCC_send_prebuilt_message (&msg->header, 0,
+ zero, c, fwd,
+ GNUNET_YES, NULL, NULL);
}
else if (0 == c->pending_messages)
{
LOG (GNUNET_ERROR_TYPE_DEBUG, " directly destroying connection!\n");
GCC_destroy (c);
GCC_check_connections ();
- return GNUNET_OK;
+ return;
}
- c->destroy = GNUNET_YES;
- c->state = CADET_CONNECTION_DESTROYED;
+ mark_destroyed (c);
if (NULL != c->t)
{
GCT_remove_connection (c->t, c);
c->t = NULL;
}
GCC_check_connections ();
- return GNUNET_OK;
+ return;
+}
+
+
+/**
+ * Handler for cadet network traffic hop-by-hop acks.
+ *
+ * @param peer Message sender (neighbor).
+ * @param msg Message itself.
+ */
+void
+GCC_handle_ack (struct CadetPeer *peer,
+ const struct GNUNET_CADET_ConnectionEncryptedAckMessage *msg)
+{
+ struct CadetConnection *c;
+ struct CadetFlowControl *fc;
+ struct CadetEncryptedMessageIdentifier ack;
+ int fwd;
+
+ GCC_check_connections ();
+ log_message (&msg->header, peer, &msg->cid);
+ c = connection_get (&msg->cid);
+ if (NULL == c)
+ {
+ GNUNET_STATISTICS_update (stats,
+ "# ack on unknown connection",
+ 1,
+ GNUNET_NO);
+ send_broken_unknown (&msg->cid,
+ &my_full_id,
+ NULL,
+ peer);
+ GCC_check_connections ();
+ return;
+ }
+
+ /* Is this a forward or backward ACK? */
+ if (get_next_hop (c) == peer)
+ {
+ fc = &c->fwd_fc;
+ fwd = GNUNET_YES;
+ }
+ else if (get_prev_hop (c) == peer)
+ {
+ fc = &c->bck_fc;
+ fwd = GNUNET_NO;
+ }
+ else
+ {
+ GNUNET_break_op (0);
+ return;
+ }
+
+ ack = msg->cemi_max;
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
+ GC_f2s (fwd),
+ ntohl (ack.pid),
+ ntohl (fc->last_ack_recv.pid));
+ if (GC_is_pid_bigger (ntohl (ack.pid),
+ ntohl (fc->last_ack_recv.pid)))
+ fc->last_ack_recv = ack;
+
+ /* Cancel polling if the ACK is big enough. */
+ if ( (NULL != fc->poll_task) &
+ GC_is_pid_bigger (ntohl (fc->last_ack_recv.pid),
+ ntohl (fc->last_pid_sent.pid)))
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
+ GNUNET_SCHEDULER_cancel (fc->poll_task);
+ fc->poll_task = NULL;
+ fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
+ }
+
+ GCC_check_connections ();
+}
+
+
+/**
+ * Handler for cadet network traffic hop-by-hop data counter polls.
+ *
+ * @param peer Message sender (neighbor).
+ * @param msg Message itself.
+ */
+void
+GCC_handle_poll (struct CadetPeer *peer,
+ const struct GNUNET_CADET_ConnectionHopByHopPollMessage *msg)
+{
+ struct CadetConnection *c;
+ struct CadetFlowControl *fc;
+ struct CadetEncryptedMessageIdentifier pid;
+ int fwd;
+
+ GCC_check_connections ();
+ log_message (&msg->header, peer, &msg->cid);
+ c = connection_get (&msg->cid);
+ if (NULL == c)
+ {
+ GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
+ GNUNET_NO);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "POLL message on unknown connection %s!\n",
+ GNUNET_sh2s (&msg->cid.connection_of_tunnel));
+ send_broken_unknown (&msg->cid,
+ &my_full_id,
+ NULL,
+ peer);
+ GCC_check_connections ();
+ return;
+ }
+
+ /* Is this a forward or backward ACK?
+ * Note: a poll should never be needed in a loopback case,
+ * since there is no possiblility of packet loss there, so
+ * this way of discerining FWD/BCK should not be a problem.
+ */
+ if (get_next_hop (c) == peer)
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
+ fc = &c->fwd_fc;
+ }
+ else if (get_prev_hop (c) == peer)
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
+ fc = &c->bck_fc;
+ }
+ else
+ {
+ GNUNET_break_op (0);
+ return;
+ }
+
+ pid = msg->cemi;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ " PID %u, OLD %u\n",
+ ntohl (pid.pid),
+ ntohl (fc->last_pid_recv.pid));
+ fc->last_pid_recv = pid;
+ fwd = fc == &c->bck_fc;
+ GCC_send_ack (c, fwd, GNUNET_YES);
+ GCC_check_connections ();
}
* Updates the PID, state and timeout values for the connection.
*
* @param message Message to check. It must belong to an existing connection.
- * @param minimum_size The message cannot be smaller than this value.
* @param cid Connection ID (even if @a c is NULL, the ID is still needed).
* @param c Connection this message should belong. If NULL, check fails.
- * @param neighbor Neighbor that sent the message.
+ * @param sender Neighbor that sent the message.
+ *
+ * @return #GNUNET_YES if the message goes FWD.
+ * #GNUNET_NO if it goes BCK.
+ * #GNUNET_SYSERR if there is an error (unauthorized sender, ...).
*/
static int
check_message (const struct GNUNET_MessageHeader *message,
- size_t minimum_size,
- const struct GNUNET_CADET_Hash* cid,
+ const struct GNUNET_CADET_ConnectionTunnelIdentifier* cid,
struct CadetConnection *c,
- const struct GNUNET_PeerIdentity *neighbor,
- uint32_t pid)
+ struct CadetPeer *sender,
+ struct CadetEncryptedMessageIdentifier pid)
{
- GNUNET_PEER_Id neighbor_id;
struct CadetFlowControl *fc;
struct CadetPeer *hop;
int fwd;
uint16_t type;
- /* Check size */
- if (ntohs (message->size) < minimum_size)
- {
- GNUNET_break_op (0);
- LOG (GNUNET_ERROR_TYPE_WARNING, "Size %u < %u\n",
- ntohs (message->size), minimum_size);
- return GNUNET_SYSERR;
- }
-
/* Check connection */
if (NULL == c)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"%s on unknown connection %s\n",
GC_m2s (ntohs (message->type)),
- GNUNET_h2s (GC_h2hc (cid)));
+ GNUNET_sh2s (&cid->connection_of_tunnel));
+ GNUNET_break_op (0);
send_broken_unknown (cid,
&my_full_id,
NULL,
- neighbor);
+ sender);
return GNUNET_SYSERR;
}
/* Check if origin is as expected */
- neighbor_id = GNUNET_PEER_search (neighbor);
hop = get_prev_hop (c);
- if (neighbor_id == GCP_get_short_id (hop))
+ if (sender == hop)
{
fwd = GNUNET_YES;
}
{
hop = get_next_hop (c);
GNUNET_break (hop == c->next_peer);
- if (neighbor_id == GCP_get_short_id (hop))
+ if (sender == hop)
{
fwd = GNUNET_NO;
}
/* Check PID for payload messages */
type = ntohs (message->type);
- if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
- || GNUNET_MESSAGE_TYPE_CADET_AX == type)
+ if (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED == type)
{
fc = fwd ? &c->bck_fc : &c->fwd_fc;
- LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n",
- pid, fc->last_pid_recv + 1, fc->last_ack_sent);
- if (GC_is_pid_bigger (pid, fc->last_ack_sent))
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected in interval [%u,%u])\n",
+ ntohl (pid.pid),
+ ntohl (fc->last_pid_recv.pid) + 1,
+ ntohl (fc->last_ack_sent.pid));
+ if (GC_is_pid_bigger (ntohl (pid.pid),
+ ntohl (fc->last_ack_sent.pid)))
{
- GNUNET_break_op (0);
- GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
- LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n",
- pid, fc->last_pid_recv, fc->last_ack_sent);
+ GNUNET_STATISTICS_update (stats,
+ "# unsolicited message",
+ 1,
+ GNUNET_NO);
+ LOG (GNUNET_ERROR_TYPE_WARNING,
+ "Received PID %u, (prev %u), ACK %u\n",
+ pid, fc->last_pid_recv, fc->last_ack_sent);
return GNUNET_SYSERR;
}
- if (GC_is_pid_bigger (pid, fc->last_pid_recv))
+ if (GC_is_pid_bigger (ntohl (pid.pid),
+ ntohl (fc->last_pid_recv.pid)))
{
unsigned int delta;
- delta = pid - fc->last_pid_recv;
+ delta = ntohl (pid.pid) - ntohl (fc->last_pid_recv.pid);
fc->last_pid_recv = pid;
fc->recv_bitmap <<= delta;
fc->recv_bitmap |= 1;
}
else
{
- GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO);
- if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap))
+ GNUNET_STATISTICS_update (stats,
+ "# out of order PID",
+ 1,
+ GNUNET_NO);
+ if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv,
+ pid,
+ fc->recv_bitmap))
{
- LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
- pid, fc->last_pid_recv - 31);
+ LOG (GNUNET_ERROR_TYPE_WARNING,
+ "PID %u unexpected (%u+), dropping!\n",
+ ntohl (pid.pid),
+ ntohl (fc->last_pid_recv.pid) - 31);
return GNUNET_SYSERR;
}
- fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid);
+ fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv,
+ pid);
}
}
/* Count as connection confirmation. */
- if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state)
+ if ( (CADET_CONNECTION_SENT == c->state) ||
+ (CADET_CONNECTION_ACK == c->state) )
{
connection_change_state (c, CADET_CONNECTION_READY);
if (NULL != c->t)
/**
- * Generic handler for cadet network encrypted traffic.
+ * Handler for key exchange traffic (Axolotl KX).
*
- * @param peer Peer identity this notification is about.
- * @param msg Encrypted message.
- * @return #GNUNET_OK to keep the connection open,
- * #GNUNET_SYSERR to close it (signal serious error)
+ * @param peer Message sender (neighbor).
+ * @param msg Message itself.
*/
-static int
-handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
+void
+GCC_handle_kx (struct CadetPeer *peer,
+ const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
{
- const struct GNUNET_CADET_Encrypted *otr_msg;
- const struct GNUNET_CADET_AX *ax_msg;
- const struct GNUNET_CADET_Hash* cid;
+ static struct CadetEncryptedMessageIdentifier zero;
+ const struct GNUNET_CADET_ConnectionTunnelIdentifier* cid;
struct CadetConnection *c;
- size_t minimum_size;
- size_t overhead;
- uint32_t pid;
- uint32_t ttl;
int fwd;
GCC_check_connections ();
- if (GNUNET_MESSAGE_TYPE_CADET_AX == ntohs (message->type))
- {
- overhead = sizeof (struct GNUNET_CADET_AX);
- ax_msg = (const struct GNUNET_CADET_AX *) message;
- cid = &ax_msg->cid;
- pid = ntohl (ax_msg->pid);
- otr_msg = NULL;
- }
- else
- {
- overhead = sizeof (struct GNUNET_CADET_Encrypted);
- otr_msg = (const struct GNUNET_CADET_Encrypted *) message;
- cid = &otr_msg->cid;
- pid = ntohl (otr_msg->pid);
- }
-
- log_message (message, peer, cid);
+ cid = &msg->cid;
+ log_message (&msg->header, peer, cid);
- minimum_size = sizeof (struct GNUNET_MessageHeader) + overhead;
c = connection_get (cid);
- fwd = check_message (message,
- minimum_size,
+ fwd = check_message (&msg->header,
cid,
c,
peer,
- pid);
+ zero);
/* If something went wrong, discard message. */
if (GNUNET_SYSERR == fwd)
{
+ GNUNET_break_op (0);
GCC_check_connections ();
- return GNUNET_OK;
+ return;
}
/* Is this message for us? */
if (GCC_is_terminal (c, fwd))
{
- GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
-
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
+ GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
if (NULL == c->t)
{
- GNUNET_break (GNUNET_NO != c->destroy);
- return GNUNET_OK;
+ GNUNET_break (0);
+ return;
}
- GCT_handle_encrypted (c->t, message);
- GCC_send_ack (c, fwd, GNUNET_NO);
+ GCT_handle_kx (c->t, msg);
GCC_check_connections ();
- return GNUNET_OK;
+ return;
}
/* Message not for us: forward to next hop */
LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
- if (NULL != otr_msg) /* only otr has ttl */
- {
- ttl = ntohl (otr_msg->ttl);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
- if (ttl == 0)
- {
- GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
- LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
- GCC_send_ack (c, fwd, GNUNET_NO);
- GCC_check_connections ();
- return GNUNET_OK;
- }
- }
-
GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
- GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
- GNUNET_NO, NULL, NULL));
+ (void) GCC_send_prebuilt_message (&msg->header, 0,
+ zero, c, fwd,
+ GNUNET_NO, NULL, NULL);
GCC_check_connections ();
- return GNUNET_OK;
}
/**
- * Generic handler for cadet network encrypted traffic.
+ * Handler for encrypted cadet network traffic (channel mgmt, data).
*
- * @param peer Peer identity this notification is about.
- * @param msg Encrypted message.
- * @return #GNUNET_OK to keep the connection open,
- * #GNUNET_SYSERR to close it (signal serious error)
+ * @param peer Message sender (neighbor).
+ * @param msg Message itself.
*/
-static int
-handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_CADET_KX *msg)
+void
+GCC_handle_encrypted (struct CadetPeer *peer,
+ const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
{
- const struct GNUNET_CADET_Hash* cid;
+ static struct CadetEncryptedMessageIdentifier zero;
+ const struct GNUNET_CADET_ConnectionTunnelIdentifier* cid;
struct CadetConnection *c;
- size_t expected_size;
+ struct CadetEncryptedMessageIdentifier pid;
int fwd;
GCC_check_connections ();
cid = &msg->cid;
+ pid = msg->cemi;
log_message (&msg->header, peer, cid);
- expected_size = sizeof (struct GNUNET_CADET_KX)
- + sizeof (struct GNUNET_MessageHeader);
c = connection_get (cid);
fwd = check_message (&msg->header,
- expected_size,
cid,
c,
peer,
- 0);
+ pid);
/* If something went wrong, discard message. */
if (GNUNET_SYSERR == fwd)
- return GNUNET_OK;
+ {
+ GCC_check_connections ();
+ return;
+ }
/* Is this message for us? */
if (GCC_is_terminal (c, fwd))
{
- LOG (GNUNET_ERROR_TYPE_DEBUG, " message for us!\n");
- GNUNET_STATISTICS_update (stats, "# received KX", 1, GNUNET_NO);
+ GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
+
if (NULL == c->t)
{
- GNUNET_break (0);
- return GNUNET_OK;
+ GNUNET_break (GNUNET_NO != c->destroy);
+ return;
}
- GCT_handle_kx (c->t, &msg[1].header);
+ GCT_handle_encrypted (c->t, msg);
+ GCC_send_ack (c, fwd, GNUNET_NO);
GCC_check_connections ();
- return GNUNET_OK;
+ return;
}
/* Message not for us: forward to next hop */
LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
- GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd,
- GNUNET_NO, NULL, NULL));
- GCC_check_connections ();
- return GNUNET_OK;
-}
-
-
-/**
- * Core handler for key exchange traffic (ephemeral key, ping, pong).
- *
- * @param cls Closure (unused).
- * @param message Message received.
- * @param peer Peer who sent the message.
- * @return #GNUNET_OK to keep the connection open,
- * #GNUNET_SYSERR to close it (signal serious error)
- */
-int
-GCC_handle_kx (void *cls,
- const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
-{
- GCC_check_connections ();
- return handle_cadet_kx (peer, (struct GNUNET_CADET_KX *) message);
-}
-
-
-/**
- * Core handler for encrypted cadet network traffic (channel mgmt, data).
- *
- * @param cls Closure (unused).
- * @param message Message received.
- * @param peer Peer who sent the message.
- * @return #GNUNET_OK to keep the connection open,
- * #GNUNET_SYSERR to close it (signal serious error)
- */
-int
-GCC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
-{
- GCC_check_connections ();
- return handle_cadet_encrypted (peer, message);
-}
-
-
-/**
- * Core handler for cadet network traffic point-to-point acks.
- *
- * @param cls closure
- * @param message message
- * @param peer peer identity this notification is about
- * @return #GNUNET_OK to keep the connection open,
- * #GNUNET_SYSERR to close it (signal serious error)
- */
-int
-GCC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
-{
- struct GNUNET_CADET_ACK *msg;
- struct CadetConnection *c;
- struct CadetFlowControl *fc;
- GNUNET_PEER_Id id;
- uint32_t ack;
- int fwd;
-
- GCC_check_connections ();
- msg = (struct GNUNET_CADET_ACK *) message;
- log_message (message, peer, &msg->cid);
- c = connection_get (&msg->cid);
- if (NULL == c)
- {
- GNUNET_STATISTICS_update (stats,
- "# ack on unknown connection",
- 1,
- GNUNET_NO);
- send_broken_unknown (&msg->cid,
- &my_full_id,
- NULL,
- peer);
- GCC_check_connections ();
- return GNUNET_OK;
- }
-
- /* Is this a forward or backward ACK? */
- id = GNUNET_PEER_search (peer);
- if (GCP_get_short_id (get_next_hop (c)) == id)
- {
- fc = &c->fwd_fc;
- fwd = GNUNET_YES;
- }
- else if (GCP_get_short_id (get_prev_hop (c)) == id)
- {
- fc = &c->bck_fc;
- fwd = GNUNET_NO;
- }
- else
- {
- GNUNET_break_op (0);
- return GNUNET_OK;
- }
-
- ack = ntohl (msg->ack);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
- GC_f2s (fwd), ack, fc->last_ack_recv);
- if (GC_is_pid_bigger (ack, fc->last_ack_recv))
- fc->last_ack_recv = ack;
-
- /* Cancel polling if the ACK is big enough. */
- if (NULL != fc->poll_task &&
- GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
- GNUNET_SCHEDULER_cancel (fc->poll_task);
- fc->poll_task = NULL;
- fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
- }
-
- connection_unlock_queue (c, fwd);
- GCC_check_connections ();
- return GNUNET_OK;
-}
-
-
-/**
- * Core handler for cadet network traffic point-to-point ack polls.
- *
- * @param cls closure
- * @param message message
- * @param peer peer identity this notification is about
- * @return #GNUNET_OK to keep the connection open,
- * #GNUNET_SYSERR to close it (signal serious error)
- */
-int
-GCC_handle_poll (void *cls,
- const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
-{
- struct GNUNET_CADET_Poll *msg;
- struct CadetConnection *c;
- struct CadetFlowControl *fc;
- GNUNET_PEER_Id id;
- uint32_t pid;
- int fwd;
-
- GCC_check_connections ();
- msg = (struct GNUNET_CADET_Poll *) message;
- log_message (message, peer, &msg->cid);
- c = connection_get (&msg->cid);
- if (NULL == c)
- {
- GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
- GNUNET_NO);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "POLL message on unknown connection %s!\n",
- GNUNET_h2s (GC_h2hc (&msg->cid)));
- send_broken_unknown (&msg->cid,
- &my_full_id,
- NULL,
- peer);
- GCC_check_connections ();
- return GNUNET_OK;
- }
-
- /* Is this a forward or backward ACK?
- * Note: a poll should never be needed in a loopback case,
- * since there is no possiblility of packet loss there, so
- * this way of discerining FWD/BCK should not be a problem.
- */
- id = GNUNET_PEER_search (peer);
- if (GCP_get_short_id (get_next_hop (c)) == id)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " FWD FC\n");
- fc = &c->fwd_fc;
- }
- else if (GCP_get_short_id (get_prev_hop (c)) == id)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " BCK FC\n");
- fc = &c->bck_fc;
- }
- else
- {
- GNUNET_break_op (0);
- return GNUNET_OK;
- }
-
- pid = ntohl (msg->pid);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv);
- fc->last_pid_recv = pid;
- fwd = fc == &c->bck_fc;
- GCC_send_ack (c, fwd, GNUNET_YES);
- GCC_check_connections ();
-
- return GNUNET_OK;
-}
-
-
-/**
- * Send an ACK on the appropriate connection/channel, depending on
- * the direction and the position of the peer.
- *
- * @param c Which connection to send the hop-by-hop ACK.
- * @param fwd Is this a fwd ACK? (will go dest->root).
- * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
- */
-void
-GCC_send_ack (struct CadetConnection *c, int fwd, int force)
-{
- unsigned int buffer;
-
- GCC_check_connections ();
- LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
- GC_f2s (fwd), GCC_2s (c));
-
- if (NULL == c)
- {
- GNUNET_break (0);
- return;
- }
-
- if (GNUNET_NO != c->destroy)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
- GCC_check_connections ();
- return;
- }
-
- /* Get available buffer space */
- if (GCC_is_terminal (c, fwd))
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
- buffer = GCT_get_channels_buffer (c->t);
- }
- else
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
- buffer = GCC_get_buffer (c, fwd);
- }
- LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
- if (0 == buffer && GNUNET_NO == force)
- {
- GCC_check_connections ();
- return;
- }
-
- /* Send available buffer space */
- if (GCC_is_origin (c, fwd))
- {
- GNUNET_assert (NULL != c->t);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
- GCT_unchoke_channels (c->t);
- }
- else
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
- send_ack (c, buffer, fwd, force);
- }
+ (void) GCC_send_prebuilt_message (&msg->header, 0,
+ zero, c, fwd,
+ GNUNET_NO, NULL, NULL);
GCC_check_connections ();
}
GNUNET_SCHEDULER_shutdown ();
return;
}
- create_connection_time = GNUNET_TIME_UNIT_SECONDS;
- connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
+ create_connection_time = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
+ refresh_connection_time);
+ connections = GNUNET_CONTAINER_multishortmap_create (1024,
+ GNUNET_YES);
}
*/
static int
shutdown_iterator (void *cls,
- const struct GNUNET_HashCode *key,
+ const struct GNUNET_ShortHashCode *key,
void *value)
{
struct CadetConnection *c = value;
{
LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connections\n");
GCC_check_connections ();
- GNUNET_CONTAINER_multihashmap_iterate (connections,
- &shutdown_iterator,
- NULL);
- GNUNET_CONTAINER_multihashmap_destroy (connections);
+ GNUNET_CONTAINER_multishortmap_iterate (connections,
+ &shutdown_iterator,
+ NULL);
+ GNUNET_CONTAINER_multishortmap_destroy (connections);
connections = NULL;
}
* Create a connection.
*
* @param cid Connection ID (either created locally or imposed remotely).
- * @param t Tunnel this connection belongs to (or NULL);
+ * @param t Tunnel this connection belongs to (or NULL for transit connections);
* @param path Path this connection has to use (copy is made).
* @param own_pos Own position in the @c path path.
*
- * @return Newly created connection, NULL in case of error (own id not in path).
- */
+ * @return Newly created connection.
+ * NULL in case of error: own id not in path, wrong neighbors, ...
+*/
struct CadetConnection *
-GCC_new (const struct GNUNET_CADET_Hash *cid,
+GCC_new (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
struct CadetTunnel *t,
struct CadetPeerPath *path,
unsigned int own_pos)
c = GNUNET_new (struct CadetConnection);
c->id = *cid;
GNUNET_assert (GNUNET_OK ==
- GNUNET_CONTAINER_multihashmap_put (connections,
- GCC_get_h (c), c,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+ GNUNET_CONTAINER_multishortmap_put (connections,
+ &c->id.connection_of_tunnel,
+ c,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
fc_init (&c->fwd_fc);
fc_init (&c->bck_fc);
c->fwd_fc.c = c;
}
+/**
+ * Connection is no longer needed: destroy it.
+ *
+ * Cancels all pending traffic (including possible DESTROY messages), all
+ * maintenance tasks and removes the connection from neighbor peers and tunnel.
+ *
+ * @param c Connection to destroy.
+ */
void
GCC_destroy (struct CadetConnection *c)
{
{
connection_cancel_queues (c, GNUNET_YES);
connection_cancel_queues (c, GNUNET_NO);
+ if (NULL != c->maintenance_q)
+ {
+ GCP_send_cancel (c->maintenance_q);
+ c->maintenance_q = NULL;
+ }
}
unregister_neighbors (c);
path_destroy (c->path);
if (GNUNET_NO == c->was_removed)
{
GNUNET_break (GNUNET_YES ==
- GNUNET_CONTAINER_multihashmap_remove (connections,
- GCC_get_h (c),
- c));
+ GNUNET_CONTAINER_multishortmap_remove (connections,
+ &c->id.connection_of_tunnel,
+ c));
}
GNUNET_STATISTICS_update (stats,
"# connections",
*
* @return ID of the connection.
*/
-const struct GNUNET_CADET_Hash *
+const struct GNUNET_CADET_ConnectionTunnelIdentifier *
GCC_get_id (const struct CadetConnection *c)
{
return &c->id;
}
-/**
- * Get the connection ID.
- *
- * @param c Connection to get the ID from.
- *
- * @return ID of the connection.
- */
-const struct GNUNET_HashCode *
-GCC_get_h (const struct CadetConnection *c)
-{
- return GC_h2hc (&c->id);
-}
-
-
/**
* Get the connection path.
*
struct CadetFlowControl *fc;
fc = fwd ? &c->fwd_fc : &c->bck_fc;
- if (CADET_CONNECTION_READY != c->state
- || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent))
+ if ( (CADET_CONNECTION_READY != c->state) ||
+ GC_is_pid_bigger (ntohl (fc->last_pid_recv.pid),
+ ntohl (fc->last_ack_sent.pid)) )
{
return 0;
}
- return (fc->last_ack_sent - fc->last_pid_recv);
+ return (ntohl (fc->last_ack_sent.pid) - ntohl (fc->last_pid_recv.pid));
}
*
* @param c Connection.
* @param fwd Is query about FWD traffic?
- *
- * @return Last PID used + 1.
+ * @return Next PID to use.
*/
-unsigned int
+struct CadetEncryptedMessageIdentifier
GCC_get_pid (struct CadetConnection *c, int fwd)
{
struct CadetFlowControl *fc;
+ struct CadetEncryptedMessageIdentifier pid;
fc = fwd ? &c->fwd_fc : &c->bck_fc;
-
- return fc->last_pid_sent + 1;
+ pid = fc->next_pid;
+ fc->next_pid.pid = htonl (1 + ntohl (pid.pid));
+ return pid;
}
void
GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer)
{
- struct CadetPeer *hop;
+ struct CadetFlowControl *fc;
char peer_name[16];
int fwd;
invalidate_paths (c, peer);
- hop = get_prev_hop (c);
- if (NULL == hop)
+ fwd = is_fwd (c, peer);
+ if (GNUNET_SYSERR == fwd)
{
- /* Path was NULL, we should have deleted the connection. */
GNUNET_break (0);
return;
}
- fwd = (peer == hop);
if ( (GNUNET_YES == GCC_is_terminal (c, fwd)) ||
(GNUNET_NO != c->destroy) )
{
GCC_check_connections ();
return;
}
+ /* Mark FlowControl towards the peer as unavaliable. */
+ fc = fwd ? &c->bck_fc : &c->fwd_fc;
+ fc->queue_max = 0;
+
send_broken (c, &my_full_id, GCP_get_id (peer), fwd);
+
/* Connection will have at least one pending message
* (the one we just scheduled), so delay destruction
* and remove from map so we don't use accidentally. */
- c->destroy = GNUNET_YES;
- c->state = CADET_CONNECTION_DESTROYED;
+ mark_destroyed (c);
GNUNET_assert (GNUNET_NO == c->was_removed);
c->was_removed = GNUNET_YES;
GNUNET_break (GNUNET_YES ==
- GNUNET_CONTAINER_multihashmap_remove (connections,
- GCC_get_h (c),
- c));
+ GNUNET_CONTAINER_multishortmap_remove (connections,
+ &c->id.connection_of_tunnel,
+ c));
/* Cancel queue in the direction that just died. */
connection_cancel_queues (c, ! fwd);
GCC_stop_poll (c, ! fwd);
fc = fwd ? &c->fwd_fc : &c->bck_fc;
LOG (GNUNET_ERROR_TYPE_DEBUG,
" last ack recv: %u, last pid sent: %u\n",
- fc->last_ack_recv, fc->last_pid_sent);
- if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
+ ntohl (fc->last_ack_recv.pid),
+ ntohl (fc->last_pid_sent.pid));
+ if (GC_is_pid_bigger (ntohl (fc->last_ack_recv.pid),
+ ntohl (fc->last_pid_sent.pid)))
{
LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
return GNUNET_YES;
/**
- * Sends an already built message on a connection, properly registering
+ * Sends a completely built message on a connection, properly registering
* all used resources.
*
- * @param message Message to send. Function makes a copy of it.
- * If message is not hop-by-hop, decrements TTL of copy.
+ * @param message Message to send.
* @param payload_type Type of payload, in case the message is encrypted.
+ * 0 for restransmissions (when type is no longer known)
+ * UINT16_MAX when not applicable.
+ * @param payload_id ID of the payload (PID, ACK, ...).
* @param c Connection on which this message is transmitted.
* @param fwd Is this a fwd message?
* @param force Force the connection to accept the message (buffer overfill).
* @param cont_cls Closure for @c cont.
*
* @return Handle to cancel the message before it's sent.
- * NULL on error or if @c cont is NULL.
+ * NULL on error.
* Invalid on @c cont call.
*/
struct CadetConnectionQueue *
GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
- uint16_t payload_type, uint32_t payload_id,
+ uint16_t payload_type,
+ struct CadetEncryptedMessageIdentifier payload_id,
struct CadetConnection *c, int fwd, int force,
GCC_sent cont, void *cont_cls)
{
struct CadetFlowControl *fc;
struct CadetConnectionQueue *q;
- void *data;
- size_t size;
+ uint16_t size;
uint16_t type;
- int droppable;
- GCC_check_connections ();
size = ntohs (message->size);
- data = GNUNET_malloc (size);
- memcpy (data, message, size);
type = ntohs (message->type);
+
+ GCC_check_connections ();
+ fc = fwd ? &c->fwd_fc : &c->bck_fc;
+ if (0 == fc->queue_max)
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
+
LOG (GNUNET_ERROR_TYPE_INFO,
"--> %s (%s %4u) on conn %s (%p) %s [%5u]\n",
GC_m2s (type), GC_m2s (payload_type), payload_id, GCC_2s (c), c,
GC_f2s(fwd), size);
-
- fc = fwd ? &c->fwd_fc : &c->bck_fc;
- droppable = GNUNET_NO == force;
switch (type)
{
- struct GNUNET_CADET_AX *axmsg;
- struct GNUNET_CADET_Encrypted *emsg;
- struct GNUNET_CADET_KX *kmsg;
- struct GNUNET_CADET_ACK *amsg;
- struct GNUNET_CADET_Poll *pmsg;
- struct GNUNET_CADET_ConnectionDestroy *dmsg;
- struct GNUNET_CADET_ConnectionBroken *bmsg;
- uint32_t ttl;
-
- case GNUNET_MESSAGE_TYPE_CADET_AX:
- case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
- if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type)
- {
- emsg = (struct GNUNET_CADET_Encrypted *) data;
- ttl = ntohl (emsg->ttl);
- if (0 == ttl)
- {
- GNUNET_break_op (0);
- GNUNET_free (data);
- return NULL;
- }
- emsg->cid = c->id;
- emsg->ttl = htonl (ttl - 1);
- }
- else
- {
- axmsg = (struct GNUNET_CADET_AX *) data;
- axmsg->cid = c->id;
- }
- LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
- LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " ack recv %u\n", fc->last_ack_recv);
- if (GNUNET_YES == droppable)
+ case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED:
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u, PIDsnt: %u, ACKrcv: %u\n",
+ fc,
+ fc->queue_n,
+ ntohl (fc->last_pid_sent.pid),
+ ntohl (fc->last_ack_recv.pid));
+ if (GNUNET_NO == force)
{
fc->queue_n++;
}
- else
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " not droppable, Q_N stays the same\n");
- }
break;
- case GNUNET_MESSAGE_TYPE_CADET_KX:
- kmsg = (struct GNUNET_CADET_KX *) data;
- kmsg->cid = c->id;
+ case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX:
+ /* nothing to do here */
break;
- case GNUNET_MESSAGE_TYPE_CADET_ACK:
- amsg = (struct GNUNET_CADET_ACK *) data;
- amsg->cid = c->id;
- LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
- droppable = GNUNET_NO;
- break;
-
- case GNUNET_MESSAGE_TYPE_CADET_POLL:
- pmsg = (struct GNUNET_CADET_Poll *) data;
- pmsg->cid = c->id;
- LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL %u\n", ntohl (pmsg->pid));
- droppable = GNUNET_NO;
+ case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
+ case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK:
+ /* Should've only be used for restransmissions. */
+ GNUNET_break (0 == payload_type);
break;
+ case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK:
+ case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL:
case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
- dmsg = (struct GNUNET_CADET_ConnectionDestroy *) data;
- dmsg->cid = c->id;
- break;
-
case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
- bmsg = (struct GNUNET_CADET_ConnectionBroken *) data;
- bmsg->cid = c->id;
- break;
-
- case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
- case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
+ GNUNET_assert (GNUNET_YES == force);
break;
default:
GNUNET_break (0);
- GNUNET_free (data);
return NULL;
}
- if (fc->queue_n > fc->queue_max && droppable)
+ if (fc->queue_n > fc->queue_max && GNUNET_NO == force)
{
GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
1, GNUNET_NO);
GNUNET_break (0);
LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
fc->queue_n, fc->queue_max);
- if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
- || GNUNET_MESSAGE_TYPE_CADET_AX == type)
+ if (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED == type)
{
fc->queue_n--;
}
- GNUNET_free (data);
return NULL; /* Drop this message */
}
c->pending_messages++;
q = GNUNET_new (struct CadetConnectionQueue);
- q->forced = !droppable;
- q->q = GCP_queue_add (get_hop (c, fwd), data, type, payload_type, payload_id,
- size, c, fwd, &conn_message_sent, q);
- if (NULL == q->q)
+ q->cont = cont;
+ q->cont_cls = cont_cls;
+ q->forced = force;
+ GNUNET_CONTAINER_DLL_insert (fc->q_head, fc->q_tail, q);
+ q->peer_q = GCP_send (get_hop (c, fwd),
+ message,
+ payload_type,
+ payload_id,
+ c,
+ fwd,
+ &conn_message_sent, q);
+ if (NULL == q->peer_q)
{
LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
- GNUNET_free (data);
+ GNUNET_CONTAINER_DLL_remove (fc->q_head, fc->q_tail, q);
GNUNET_free (q);
GCC_check_connections ();
return NULL;
}
- q->cont = cont;
- q->cont_cls = cont_cls;
GCC_check_connections ();
- return (NULL == cont) ? NULL : q;
+ return q;
}
{
LOG (GNUNET_ERROR_TYPE_DEBUG, "! GCC cancel message\n");
- /* queue destroy calls message_sent, which calls q->cont and frees q */
- GCP_queue_destroy (q->q, GNUNET_YES, GNUNET_NO, 0);
+ /* send_cancel calls message_sent, which calls q->cont and frees q */
+ GCP_send_cancel (q->peer_q);
GCC_check_connections ();
}
* Sends a CREATE CONNECTION message for a path to a peer.
* Changes the connection and tunnel states if necessary.
*
- * @param connection Connection to create.
+ * @param c Connection to create.
*/
void
-GCC_send_create (struct CadetConnection *connection)
+GCC_send_create (struct CadetConnection *c)
{
+ static struct CadetEncryptedMessageIdentifier zero;
enum CadetTunnelCState state;
size_t size;
GCC_check_connections ();
- size = sizeof (struct GNUNET_CADET_ConnectionCreate);
- size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
+ size = sizeof (struct GNUNET_CADET_ConnectionCreateMessage);
+ size += c->path->length * sizeof (struct GNUNET_PeerIdentity);
+ {
+ /* Allocate message on the stack */
+ unsigned char cbuf[size];
+ struct GNUNET_CADET_ConnectionCreateMessage *msg;
+ struct GNUNET_PeerIdentity *peers;
+
+
+ msg = (struct GNUNET_CADET_ConnectionCreateMessage *) cbuf;
+ msg->header.size = htons (size);
+ msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
+ msg->reserved = htonl (0);
+ msg->cid = *GCC_get_id (c);
+ peers = (struct GNUNET_PeerIdentity *) &msg[1];
+ for (int i = 0; i < c->path->length; i++)
+ {
+ GNUNET_PEER_resolve (c->path->peers[i], peers++);
+ }
+ GNUNET_assert (NULL == c->maintenance_q);
+ c->maintenance_q = GCP_send (get_next_hop (c),
+ &msg->header,
+ GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
+ zero,
+ c, GNUNET_YES,
+ &conn_message_sent, NULL);
+ }
LOG (GNUNET_ERROR_TYPE_INFO, "==> %s %19s on conn %s (%p) FWD [%5u]\n",
GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE), "",
- GCC_2s (connection), connection, size);
+ GCC_2s (c), c, size);
LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (create)\n",
- connection, connection->pending_messages);
- connection->pending_messages++;
-
- connection->maintenance_q =
- GCP_queue_add (get_next_hop (connection), NULL,
- GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, UINT16_MAX, 0,
- size, connection, GNUNET_YES, &conn_message_sent, NULL);
+ c, c->pending_messages);
+ c->pending_messages++;
- state = GCT_get_cstate (connection->t);
+ state = GCT_get_cstate (c->t);
if (CADET_TUNNEL_SEARCHING == state || CADET_TUNNEL_NEW == state)
- GCT_change_cstate (connection->t, CADET_TUNNEL_WAITING);
- if (CADET_CONNECTION_NEW == connection->state)
- connection_change_state (connection, CADET_CONNECTION_SENT);
+ GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
+ if (CADET_CONNECTION_NEW == c->state)
+ connection_change_state (c, CADET_CONNECTION_SENT);
+ GCC_check_connections ();
+}
+
+
+/**
+ * Send an ACK on the appropriate connection/channel, depending on
+ * the direction and the position of the peer.
+ *
+ * @param c Which connection to send the hop-by-hop ACK.
+ * @param fwd Is this a fwd ACK? (will go dest->root).
+ * @param force Send the ACK even if suboptimal (e.g. requested by POLL).
+ */
+void
+GCC_send_ack (struct CadetConnection *c, int fwd, int force)
+{
+ unsigned int buffer;
+
+ GCC_check_connections ();
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "GCC send %s ACK on %s\n",
+ GC_f2s (fwd), GCC_2s (c));
+
+ if (NULL == c)
+ {
+ GNUNET_break (0);
+ return;
+ }
+
+ if (GNUNET_NO != c->destroy)
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " being destroyed, why bother...\n");
+ GCC_check_connections ();
+ return;
+ }
+
+ /* Get available buffer space */
+ if (GCC_is_terminal (c, fwd))
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from all channels\n");
+ buffer = GCT_get_channels_buffer (c->t);
+ }
+ else
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " getting from one connection\n");
+ buffer = GCC_get_buffer (c, fwd);
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer available: %u\n", buffer);
+ if (0 == buffer && GNUNET_NO == force)
+ {
+ GCC_check_connections ();
+ return;
+ }
+
+ /* Send available buffer space */
+ if (GNUNET_YES == GCC_is_origin (c, fwd))
+ {
+ GNUNET_assert (NULL != c->t);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channels...\n");
+ GCT_unchoke_channels (c->t);
+ }
+ else
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on connection\n");
+ send_ack (c, buffer, fwd, force);
+ }
GCC_check_connections ();
}
void
GCC_send_destroy (struct CadetConnection *c)
{
- struct GNUNET_CADET_ConnectionDestroy msg;
+ static struct CadetEncryptedMessageIdentifier zero;
+ struct GNUNET_CADET_ConnectionDestroyMessage msg;
if (GNUNET_YES == c->destroy)
return;
msg.header.size = htons (sizeof (msg));
msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
msg.cid = c->id;
+ msg.reserved = htonl (0);
LOG (GNUNET_ERROR_TYPE_DEBUG,
" sending connection destroy for connection %s\n",
GCC_2s (c));
if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
- GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, UINT16_MAX,
- 0, c, GNUNET_YES,
- GNUNET_YES, NULL, NULL));
+ (void) GCC_send_prebuilt_message (&msg.header,
+ UINT16_MAX,
+ zero,
+ c,
+ GNUNET_YES, GNUNET_YES, NULL, NULL);
if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
- GNUNET_assert (NULL == GCC_send_prebuilt_message (&msg.header, UINT16_MAX,
- 0, c, GNUNET_NO,
- GNUNET_YES, NULL, NULL));
- c->destroy = GNUNET_YES;
- c->state = CADET_CONNECTION_DESTROYED;
+ (void) GCC_send_prebuilt_message (&msg.header,
+ UINT16_MAX,
+ zero,
+ c,
+ GNUNET_NO, GNUNET_YES, NULL, NULL);
+ mark_destroyed (c);
GCC_check_connections ();
}
fc->poll_task, fc->poll_msg);
return;
}
+ if (0 == fc->queue_max)
+ {
+ /* Should not be needed, traffic should've been cancelled. */
+ GNUNET_break (0);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL not possible, peer disconnected\n");
+ return;
+ }
LOG (GNUNET_ERROR_TYPE_DEBUG, "POLL started on request\n");
- fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
- &connection_poll,
- fc);
+ fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time, &send_poll, fc);
}
static char buf[128];
SPRINTF (buf, "%s (->%s)",
- GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t));
+ GNUNET_sh2s (&GCC_get_id (c)->connection_of_tunnel),
+ GCT_2s (c->t));
return buf;
}
- return GNUNET_h2s (GC_h2hc (&c->id));
+ return GNUNET_sh2s (&c->id.connection_of_tunnel);
}
LOG2 (level, "CCC FWD flow control:\n");
LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
- c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv);
+ ntohl (c->fwd_fc.last_pid_sent.pid),
+ ntohl (c->fwd_fc.last_pid_recv.pid));
LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
- c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv);
+ ntohl (c->fwd_fc.last_ack_sent.pid),
+ ntohl (c->fwd_fc.last_ack_recv.pid));
LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
LOG2 (level, "CCC BCK flow control:\n");
LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
- c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv);
+ ntohl (c->bck_fc.last_pid_sent.pid),
+ ntohl (c->bck_fc.last_pid_recv.pid));
LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
- c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv);
+ ntohl (c->bck_fc.last_ack_sent.pid),
+ ntohl (c->bck_fc.last_ack_recv.pid));
LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);