-
-/**
- * Send a notification that a connection is broken, when a connection
- * isn't even known to the local peer or soon to be destroyed.
- *
- * @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).
- */
-static void
-send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
- const struct GNUNET_PeerIdentity *id1,
- const struct GNUNET_PeerIdentity *id2,
- const struct GNUNET_PeerIdentity *peer_id)
-{
- struct GNUNET_CADET_ConnectionBroken *msg;
- struct CadetPeerQueue *q;
- struct CadetPeer *neighbor;
-
- GCC_check_connections ();
- LOG (GNUNET_ERROR_TYPE_INFO, "--> BROKEN on unknown connection %s\n",
- GNUNET_h2s (GC_h2hc (connection_id)));
-
- 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;
- if (NULL != 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);
- q = 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 */
- GNUNET_assert (NULL != q);
- GCC_check_connections ();
-}
-
-
-/**
- * Send keepalive packets for a connection.
- *
- * @param c Connection to keep alive..
- * @param fwd Is this a FWD keepalive? (owner -> dest).
- */
-static void
-send_connection_keepalive (struct CadetConnection *c, int fwd)
-{
- struct GNUNET_MessageHeader msg;
- struct CadetFlowControl *fc;
-
- GCC_check_connections ();
- LOG (GNUNET_ERROR_TYPE_INFO,
- "keepalive %s for connection %s\n",
- GC_f2s (fwd), GCC_2s (c));
-
- 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))
- {
- LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, traffic in queue\n");
- return;
- }
-
- GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
-
- GNUNET_assert (NULL != c->t);
- msg.size = htons (sizeof (msg));
- msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE);
-
- GNUNET_assert (NULL ==
- GCT_send_prebuilt_message (&msg, c->t, c,
- GNUNET_NO, NULL, NULL));
- GCC_check_connections ();
-}
-
-
-/**
- * Send CONNECTION_{CREATE/ACK} packets for a connection.
- *
- * @param c Connection for which to send the message.
- * @param fwd If #GNUNET_YES, send CREATE, otherwise send ACK.
- */
-static void
-connection_recreate (struct CadetConnection *c, int fwd)
-{
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "sending connection recreate\n");
- if (fwd)
- GCC_send_create (c);
- else
- send_connection_ack (c, GNUNET_NO);
-}
-
-
-/**
- * Generic connection timer management.
- * Depending on the role of the peer in the connection will send the
- * appropriate message (build or keepalive)
- *
- * @param c Conncetion to maintain.
- * @param fwd Is FWD?
- */
-static void
-connection_maintain (struct CadetConnection *c, int fwd)
-{
- if (GNUNET_NO != c->destroy)
- {
- LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, being destroyed\n");
- return;
- }
-
- if (NULL == c->t)
- {
- GNUNET_break (0);
- GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
- return;
- }
-
- if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (c->t))
- {
- /* If status is SEARCHING, why is there a connection? Should be WAITING */
- GNUNET_break (0);
- GCT_debug (c->t, GNUNET_ERROR_TYPE_ERROR);
- LOG (GNUNET_ERROR_TYPE_INFO, "not sending keepalive, tunnel SEARCHING\n");
- schedule_next_keepalive (c, fwd);
- return;
- }
- switch (c->state)
- {
- case CADET_CONNECTION_NEW:
- GNUNET_break (0);
- /* fall-through */
- case CADET_CONNECTION_SENT:
- connection_recreate (c, fwd);
- break;
- case CADET_CONNECTION_READY:
- send_connection_keepalive (c, fwd);
- break;
- default:
- break;
- }
-}
-
-
-/**
- * Keep the connection alive.
- *
- * @param c Connection to keep alive.
- * @param fwd Direction.
- */
-static void
-connection_keepalive (struct CadetConnection *c,
- int fwd)
-{
- GCC_check_connections ();
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s keepalive for %s\n",
- GC_f2s (fwd), GCC_2s (c));
-
- if (fwd)
- c->fwd_maintenance_task = NULL;
- else
- c->bck_maintenance_task = NULL;
- 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).
- */
-static void
-connection_fwd_keepalive (void *cls)
-{
- struct CadetConnection *c = cls;
-
- GCC_check_connections ();
- connection_keepalive (c,
- GNUNET_YES);
- GCC_check_connections ();
-}
-
-
-/**
- * Keep the connection alive in the BCK direction.
- *
- * @param cls Closure (connection to keepalive).
- */
-static void
-connection_bck_keepalive (void *cls)
-{
- struct CadetConnection *c = cls;
-
- GCC_check_connections ();
- connection_keepalive (c,
- GNUNET_NO);
- GCC_check_connections ();
-}
-
-
-/**
- * Schedule next keepalive task, taking in consideration
- * the connection state and number of retries.
- *
- * If the peer is not the origin, do nothing.
- *
- * @param c Connection for which to schedule the next keepalive.
- * @param fwd Direction for the next keepalive.
- */
-static void
-schedule_next_keepalive (struct CadetConnection *c, int fwd)
-{
- struct GNUNET_TIME_Relative delay;
- struct GNUNET_SCHEDULER_Task * *task_id;
- GNUNET_SCHEDULER_TaskCallback keepalive_task;
-
- GCC_check_connections ();
- if (GNUNET_NO == GCC_is_origin (c, fwd))
- return;
-
- /* Calculate delay to use, depending on the state of the connection */
- if (CADET_CONNECTION_READY == c->state)
- {
- delay = refresh_connection_time;
- }
- else
- {
- 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)
- c->create_retry *= 2;
- }
-
- /* Select direction-dependent parameters */
- if (GNUNET_YES == fwd)
- {
- task_id = &c->fwd_maintenance_task;
- keepalive_task = &connection_fwd_keepalive;
- }
- else
- {
- task_id = &c->bck_maintenance_task;
- keepalive_task = &connection_bck_keepalive;
- }
-
- /* Check that no one scheduled it before us */
- if (NULL != *task_id)
- {
- /* No need for a _break. It can happen for instance when sending a SYNACK
- * for a duplicate SYN: the first SYNACK scheduled the task. */
- GNUNET_SCHEDULER_cancel (*task_id);
- }
-
- /* Schedule the task */
- *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);
- GCC_check_connections ();
-}
-
-
-/**
- * Cancel all transmissions that belong to a certain connection.
- *
- * If the connection is scheduled for destruction and no more messages are left,
- * the connection will be destroyed by the continuation call.
- *
- * @param c Connection which to cancel. Might be destroyed during this call.
- * @param fwd Cancel fwd traffic?
- */
-static void
-connection_cancel_queues (struct CadetConnection *c,
- int fwd)
-{
- struct CadetFlowControl *fc;
- struct CadetPeer *peer;
-
- GCC_check_connections ();
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Cancel %s queues for connection %s\n",
- GC_f2s (fwd), GCC_2s (c));
- if (NULL == c)
- {
- GNUNET_break (0);
- return;
- }
-
- fc = fwd ? &c->fwd_fc : &c->bck_fc;
- if (NULL != fc->poll_task)
- {
- GNUNET_SCHEDULER_cancel (fc->poll_task);
- fc->poll_task = NULL;
- LOG (GNUNET_ERROR_TYPE_DEBUG, " cancelled POLL task for fc %p\n", fc);
- }
- if (NULL != fc->poll_msg)
- {
- 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).
- */
-static void
-connection_poll (void *cls);
-
-
-/**
- * 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)
- {
- 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,
- &connection_poll,
- fc);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
-}
-
-
-/**
- * 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
-connection_poll (void *cls)
-{
- struct CadetFlowControl *fc = cls;
- struct GNUNET_CADET_Poll msg;
- struct CadetConnection *c;
- int fwd;
-
- fc->poll_task = NULL;
- GCC_check_connections ();
- 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.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);
- 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 ();
- mark_destroyed (c);
-
- 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.
- *
- * Timeout function due to lack of keepalive/traffic from an endpoint.
- * Destroys connection if called.
- *
- * @param c Connection to destroy.
- * @param fwd Was the timeout from the origin? (FWD timeout)
- */
-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",
- GCC_2s (c),
- GC_f2s (fwd));
- GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
-
- if (GCC_is_origin (c, fwd)) /* Loopback? Something is wrong! */
- {
- GNUNET_break (0);
- return;
- }
-
- /* If dest, salvage queued traffic. */
- if (GCC_is_terminal (c, fwd))
- {
- const struct GNUNET_PeerIdentity *next_hop;
-
- next_hop = GCP_get_id (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);
- GCC_check_connections ();
-}
-
-
-/**
- * Timeout function due to lack of keepalive/traffic from the owner.
- * Destroys connection if called.
- *
- * @param cls Closure (connection to destroy).
- */
-static void
-connection_fwd_timeout (void *cls)
-{
- struct CadetConnection *c = cls;
-
- c->fwd_maintenance_task = NULL;
- GCC_check_connections ();
- connection_timeout (c, GNUNET_YES);
- GCC_check_connections ();
-}
-
-
-/**
- * Timeout function due to lack of keepalive/traffic from the destination.
- * Destroys connection if called.
- *
- * @param cls Closure (connection to destroy).
- */
-static void
-connection_bck_timeout (void *cls)
-{
- struct CadetConnection *c = cls;
-
- c->bck_maintenance_task = NULL;
- GCC_check_connections ();
- connection_timeout (c, GNUNET_NO);
- GCC_check_connections ();
-}
-
-
-/**
- * Resets the connection timeout task, some other message has done the
- * task's job.
- * - For the first peer on the direction this means to send
- * a keepalive or a path confirmation message (either create or ACK).
- * - For all other peers, this means to destroy the connection,
- * due to lack of activity.
- * Starts the timeout if no timeout was running (connection just created).
- *
- * @param c Connection whose timeout to reset.
- * @param fwd Is this forward?
- *
- * TODO use heap to improve efficiency of scheduler.
- */
-static void
-connection_reset_timeout (struct CadetConnection *c, int fwd)
-{
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GC_f2s (fwd));
- if (GCC_is_origin (c, fwd)) /* Startpoint */
- {
- schedule_next_keepalive (c, fwd);
- }
- else /* Relay, endpoint. */
- {
- struct GNUNET_TIME_Relative delay;
- struct GNUNET_SCHEDULER_Task * *ti;
- GNUNET_SCHEDULER_TaskCallback f;
-
- ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
-
- if (NULL != *ti)
- GNUNET_SCHEDULER_cancel (*ti);
- delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- " timing out in %s\n",
- GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_NO));
- f = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
- *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
- }
-}
-
-
-/**
- * Iterator to compare each connection's path with the path of a new 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).
- * @param c Connection in the tunnel to check.
- */
-static void
-check_path (void *cls, struct CadetConnection *c)
-{
- struct CadetConnection *new_conn = cls;
- struct CadetPeerPath *path = new_conn->path;
-
- LOG (GNUNET_ERROR_TYPE_DEBUG, " checking %s (%p), length %u\n",
- GCC_2s (c), c, c->path->length);
-
- if (c != new_conn
- && GNUNET_NO == c->destroy
- && CADET_CONNECTION_BROKEN != c->state
- && CADET_CONNECTION_DESTROYED != c->state
- && path_equivalent (path, c->path))
- {
- 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");
- }
-}
-
-
-/**
- * Finds out if this path is already being used by an existing connection.
- *
- * Checks the tunnel towards the destination to see if it contains
- * any connection with the same path.
- *
- * If the existing connection is ready, it is kept.
- * Otherwise if the sender has a smaller ID that ours, we accept it (and
- * the peer will eventually reject our attempt).
- *
- * @param path Path to check.
- * @return #GNUNET_YES if the tunnel has a connection with the same path,
- * #GNUNET_NO otherwise.
- */
-static int
-does_connection_exist (struct CadetConnection *conn)
-{
- struct CadetPeer *p;
- struct CadetTunnel *t;
- struct CadetConnection *c;
-
- p = GCP_get_short (conn->path->peers[0], GNUNET_NO);
- if (NULL == p)
- return GNUNET_NO;
- t = GCP_get_tunnel (p);
- if (NULL == t)
- return GNUNET_NO;
-
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking for duplicates\n");
-
- GCT_iterate_connections (t, &check_path, conn);
-
- if (GNUNET_YES == conn->destroy)
- {
- c = conn->path->c;
- conn->destroy = GNUNET_NO;
- conn->path->c = conn;
- LOG (GNUNET_ERROR_TYPE_DEBUG, " found duplicate of %s\n", GCC_2s (conn));
- LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate: %s\n", GCC_2s (c));
- GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
- if (CADET_CONNECTION_READY == c->state)
- {
- /* The other peer confirmed a live connection with this path,
- * 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");
- return GNUNET_NO;
- }
- else
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " %s has no duplicates\n", GCC_2s (conn));
- return GNUNET_NO;
- }
-}
-
-
-/**
- * @brief Check if the tunnel this connection belongs to has any other
- * connection with the same path, and destroy one if so.
- *
- * @param cls Closure (connection to check).
- */
-static void
-check_duplicates (void *cls)
-{
- struct CadetConnection *c = cls;
-
- c->check_duplicates_task = NULL;
- if (GNUNET_YES == does_connection_exist (c))
- {
- GCT_debug (c->t, GNUNET_ERROR_TYPE_DEBUG);
- send_broken (c, &my_full_id, &my_full_id, GCC_is_origin (c, GNUNET_YES));
- GCC_destroy (c);
- }
-}
-
-
-/**
- * Wait for enough time to let any dead connections time out and check for
- * any remaining duplicates.
- *
- * @param c Connection that is a potential duplicate.
- */
-static void
-schedule_check_duplicates (struct CadetConnection *c)
-{
- struct GNUNET_TIME_Relative delay;
-
- if (NULL != c->check_duplicates_task)
- return;
- delay = GNUNET_TIME_relative_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 c Connection.
- *
- * @return #GNUNET_OK if everything went fine
- * #GNUNET_SYSERR if the was an error and @c c is malformed.
- */
-static int
-register_neighbors (struct CadetConnection *c)
-{
- c->next_peer = get_next_hop (c);
- c->prev_peer = get_prev_hop (c);
- GNUNET_assert (c->next_peer != c->prev_peer);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "register neighbors for connection %s\n",
- GCC_2s (c));
- path_debug (c->path);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "own pos %u\n", c->own_pos);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "putting connection %s to next peer %p\n",
- GCC_2s (c),
- c->next_peer);
- LOG (GNUNET_ERROR_TYPE_DEBUG, "next peer %p %s\n",
- c->next_peer,
- GCP_2s (c->next_peer));
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "putting connection %s to prev peer %p\n",
- GCC_2s (c),
- c->prev_peer);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "prev peer %p %s\n",
- c->prev_peer,
- GCP_2s (c->prev_peer));
-
- if ( (GNUNET_NO == GCP_is_neighbor (c->next_peer)) ||
- (GNUNET_NO == GCP_is_neighbor (c->prev_peer)) )
- {
- if (GCC_is_origin (c, GNUNET_YES))
- GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
- GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
-
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- " register neighbors failed\n");
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- " prev: %s, neighbor?: %d\n",
- GCP_2s (c->prev_peer),
- GCP_is_neighbor (c->prev_peer));
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- " next: %s, neighbor?: %d\n",
- GCP_2s (c->next_peer),
- GCP_is_neighbor (c->next_peer));
- return GNUNET_SYSERR;
- }
- GCP_add_connection (c->next_peer, c, GNUNET_NO);
- GCP_add_connection (c->prev_peer, c, GNUNET_YES);
-
- return GNUNET_OK;
-}
-
-
-/**
- * Remove the connection from the list of both neighbors.
- *
- * @param c Connection.
- */
-static void
-unregister_neighbors (struct CadetConnection *c)
-{
-// struct CadetPeer *peer; FIXME dont use next_peer, prev_peer
- /* Either already unregistered or never got registered, it's ok either way. */
- if (NULL == c->path)
- return;
- if (NULL != c->next_peer)
- {
- GCP_remove_connection (c->next_peer, c);
- c->next_peer = NULL;
- }
- if (NULL != c->prev_peer)
- {
- GCP_remove_connection (c->prev_peer, c);
- c->prev_peer = NULL;
- }
-}
-
-
-/**
- * Invalidates all paths towards all peers that comprise the connection which
- * rely on the disconnected peer.
- *
- * ~O(n^3) (peers in connection * paths/peer * links/path)
- *
- * @param c Connection whose peers' paths to clean.
- * @param disconnected Peer that disconnected.
- */
-static void
-invalidate_paths (struct CadetConnection *c,
- struct CadetPeer *disconnected)
-{
- struct CadetPeer *peer;
- unsigned int i;
-
- for (i = 0; i < c->path->length; i++)
- {
- peer = GCP_get_short (c->path->peers[i], GNUNET_NO);
- if (NULL != peer)
- GCP_notify_broken_link (peer, &my_full_id, GCP_get_id (disconnected));
- }
-}
-
-
-/**
- * Bind the connection to the peer and the tunnel to that peer.
- *
- * If the peer has no tunnel, create one. Update tunnel and connection
- * data structres to reflect new status.
- *
- * @param c Connection.
- * @param peer Peer.
- */
-static void
-add_to_peer (struct CadetConnection *c,
- struct CadetPeer *peer)
-{
- GCP_add_tunnel (peer);
- c->t = GCP_get_tunnel (peer);
- GCT_add_connection (c->t, c);
-}
-
-
-/**
- * Log receipt of message on stderr (INFO level).
- *
- * @param message Message received.
- * @param peer Peer who sent the message.
- * @param hash Connection ID.
- */
-static void
-log_message (const struct GNUNET_MessageHeader *message,
- const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_CADET_Hash *hash)
-{
- uint16_t size;
- uint16_t type;
- char *arrow;
-
- size = ntohs (message->size);
- type = ntohs (message->type);
- switch (type)
- {
- case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
- case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
- case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
- case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
- arrow = "==";
- break;
- 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);
-}
-
-/******************************************************************************/
-/******************************** API ***********************************/
-/******************************************************************************/
-
-/**
- * Core 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)
- */
-int
-GCC_handle_create (void *cls,
- const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
-{
- struct GNUNET_CADET_ConnectionCreate *msg;
- struct GNUNET_PeerIdentity *id;
- struct GNUNET_CADET_Hash *cid;
- struct CadetPeerPath *path;
- struct CadetPeer *dest_peer;
- struct CadetPeer *orig_peer;
- struct CadetConnection *c;
- unsigned int own_pos;
- 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;
- }
-
- /* Calculate hops */
- size -= sizeof (struct GNUNET_CADET_ConnectionCreate);
- if (size % sizeof (struct GNUNET_PeerIdentity))
- {
- GNUNET_break_op (0);
- return GNUNET_OK;
- }
- if (0 != size % sizeof (struct GNUNET_PeerIdentity))
- {
- GNUNET_break_op (0);
- return GNUNET_OK;
- }
- size /= sizeof (struct GNUNET_PeerIdentity);
- if (1 > size)
- {
- GNUNET_break_op (0);
- return GNUNET_OK;
- }
- 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);
- id = (struct GNUNET_PeerIdentity *) &msg[1];
- LOG (GNUNET_ERROR_TYPE_DEBUG, " origin: %s\n", GNUNET_i2s (id));
-
- /* Create connection */
- c = connection_get (cid);
- if (NULL == c)
- {
- path = path_build_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1],
- size, myid, &own_pos);
- if (NULL == path)
- {
- /* 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;
- }
-
- 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;
- }
-
- LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
- LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n");
- c = GCC_new (cid, NULL, path, own_pos);
- if (NULL == c)
- {
- if (path->length - 1 == own_pos)
- {
- /* If we are destination, why did the creation fail? */
- GNUNET_break (0);
- path_destroy (path);
- GCC_check_connections ();
- return GNUNET_OK;
- }
- 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;
- }
- GCP_add_path_to_all (path, GNUNET_NO);
- connection_reset_timeout (c, GNUNET_YES);
- }
- else
- {
- path = path_duplicate (c->path);
- }
- if (CADET_CONNECTION_NEW == c->state)
- connection_change_state (c, CADET_CONNECTION_SENT);
-
- /* Remember peers */
- dest_peer = GCP_get (&id[size - 1], GNUNET_YES);
- orig_peer = GCP_get (&id[0], GNUNET_YES);
-
- /* Is it a connection to us? */
- if (c->own_pos == path->length - 1)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
- GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
-
- add_to_peer (c, orig_peer);
- if (GNUNET_YES == does_connection_exist (c))
- {
- /* Peer created a connection equal to one we think exists
- * and is fine.
- * Solution: Keep both and postpone disambiguation. In the meantime
- * the connection will time out or peer will inform us it is broken.
- *
- * Other options:
- * - Use explicit duplicate.
- * - Accept new conn and destroy the old. (interruption in higher level)
- * - Keep the one with higher ID / created by peer with higher ID. */
- schedule_check_duplicates (c);
- }
-
- if (CADET_TUNNEL_NEW == GCT_get_cstate (c->t))
- GCT_change_cstate (c->t, CADET_TUNNEL_WAITING);
-
- 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");
- 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));
- }
- path_destroy (path);
- GCC_check_connections ();
- return GNUNET_OK;
-}
-
-
-/**
- * Core handler for path 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)
- */
-int
-GCC_handle_confirm (void *cls,
- const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
-{
- struct GNUNET_CADET_ConnectionACK *msg;
- 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);
- c = connection_get (&msg->cid);
- if (NULL == c)
- {
- GNUNET_STATISTICS_update (stats, "# control on unknown connection",
- 1, GNUNET_NO);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- " don't know the connection!\n");
- send_broken_unknown (&msg->cid, &my_full_id, NULL, peer);
- GCC_check_connections ();
- return GNUNET_OK;
- }
-
- if (GNUNET_NO != c->destroy)
- {
- GNUNET_assert (CADET_CONNECTION_DESTROYED == c->state);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "connection %s being destroyed, ignoring confirm\n",
- GCC_2s (c));
- GCC_check_connections ();
- return GNUNET_OK;
- }
-
- 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, " SYNACK\n");
- fwd = GNUNET_NO;
- if (CADET_CONNECTION_SENT == oldstate)
- connection_change_state (c, CADET_CONNECTION_ACK);
- }
- else if (get_prev_hop (c) == pi)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " FINAL ACK\n");
- fwd = GNUNET_YES;
- connection_change_state (c, CADET_CONNECTION_READY);
- }
- else
- {
- GNUNET_break_op (0);
- return GNUNET_OK;
- }
-
- 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);
- }
-
- /* Message for us as creator? */
- if (GCC_is_origin (c, GNUNET_YES))
- {
- if (GNUNET_NO != fwd)
- {
- GNUNET_break_op (0);
- return GNUNET_OK;
- }
- 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)
- connection_reset_timeout (c, GNUNET_YES);
-
- /* Change connection state */
- connection_change_state (c, CADET_CONNECTION_READY);
- send_connection_ack (c, GNUNET_YES);
-
- /* Change tunnel state, trigger KX */
- if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
- GCT_change_cstate (c->t, CADET_TUNNEL_READY);
- GCC_check_connections ();
- return GNUNET_OK;
- }
-
- /* Message for us as destination? */
- if (GCC_is_terminal (c, GNUNET_YES))
- {
- if (GNUNET_YES != fwd)
- {
- GNUNET_break_op (0);
- return GNUNET_OK;
- }
- LOG (GNUNET_ERROR_TYPE_DEBUG, " Connection ACK for us!\n");
-
- /* If just created, cancel the short timeout and start a long one */
- if (CADET_CONNECTION_ACK == oldstate)
- connection_reset_timeout (c, GNUNET_NO);
-
- /* Change tunnel state */
- 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));
- GCC_check_connections ();
- return GNUNET_OK;
-}
-
-
-/**
- * Core 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)
- */
-int
-GCC_handle_broken (void* cls,
- const struct GNUNET_PeerIdentity* id,
- const struct GNUNET_MessageHeader* message)
-{
- struct GNUNET_CADET_ConnectionBroken *msg;
- 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));
- c = connection_get (&msg->cid);
- if (NULL == c)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, " duplicate CONNECTION_BROKEN\n");
- GCC_check_connections ();
- return GNUNET_OK;
- }
-
- t = c->t;
-
- fwd = is_fwd (c, id);
- mark_destroyed (c);
- if (GCC_is_terminal (c, fwd))
- {
- struct CadetPeer *endpoint;
-
- if (NULL == t)
- {
- /* A terminal connection should not have 't' set to NULL. */
- GNUNET_break (0);
- GCC_debug (c, GNUNET_ERROR_TYPE_ERROR);
- return GNUNET_OK;
- }
- 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);
-
- 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);
- }
- else
- {
- GNUNET_assert (NULL == GCC_send_prebuilt_message (message, 0, 0, c, fwd,
- GNUNET_YES, NULL, NULL));
- connection_cancel_queues (c, !fwd);
- }
- GCC_check_connections ();
- return GNUNET_OK;
-}
-
-
-/**
- * Core handler for tunnel destruction
- *
- * @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)
- */
-int
-GCC_handle_destroy (void *cls,
- const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
-{
- const struct GNUNET_CADET_ConnectionDestroy *msg;
- struct CadetConnection *c;
- int fwd;
-
- GCC_check_connections ();
- msg = (const struct GNUNET_CADET_ConnectionDestroy *) message;
- log_message (message, peer, &msg->cid);
- c = connection_get (&msg->cid);
- if (NULL == c)
- {
- /* Probably already got the message from another path,
- * destroyed the tunnel and retransmitted to children.
- * Safe to ignore.
- */
- GNUNET_STATISTICS_update (stats,
- "# control on unknown connection",
- 1, GNUNET_NO);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- " connection unknown: already destroyed?\n");
- GCC_check_connections ();
- return GNUNET_OK;
- }
- fwd = is_fwd (c, peer);
- if (GNUNET_SYSERR == fwd)
- {
- GNUNET_break_op (0); /* FIXME */
- return GNUNET_OK;
- }
- if (GNUNET_NO == GCC_is_terminal (c, fwd))
- {
- GNUNET_assert (NULL ==
- GCC_send_prebuilt_message (message, 0, 0, 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;
- }
- mark_destroyed (c);
- if (NULL != c->t)
- {
- GCT_remove_connection (c->t, c);
- c->t = NULL;
- }
- GCC_check_connections ();
- return GNUNET_OK;
-}
-
-
-/**
- * Check the message against internal state and test if it goes FWD or BCK.
- *
- * 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.
- */
-static int
-check_message (const struct GNUNET_MessageHeader *message,
- size_t minimum_size,
- const struct GNUNET_CADET_Hash* cid,
- struct CadetConnection *c,
- const struct GNUNET_PeerIdentity *neighbor,
- uint32_t 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)
- {
- GNUNET_STATISTICS_update (stats,
- "# unknown connection",
- 1, GNUNET_NO);
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "%s on unknown connection %s\n",
- GC_m2s (ntohs (message->type)),
- GNUNET_h2s (GC_h2hc (cid)));
- send_broken_unknown (cid,
- &my_full_id,
- NULL,
- neighbor);
- 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))
- {
- fwd = GNUNET_YES;
- }
- else
- {
- hop = get_next_hop (c);
- GNUNET_break (hop == c->next_peer);
- if (neighbor_id == GCP_get_short_id (hop))
- {
- fwd = GNUNET_NO;
- }
- else
- {
- /* Unexpected peer sending traffic on a connection. */
- GNUNET_break_op (0);
- return GNUNET_SYSERR;
- }
- }
-
- /* Check PID for payload messages */
- type = ntohs (message->type);
- if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type
- || GNUNET_MESSAGE_TYPE_CADET_AX == 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))
- {
- 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);
- return GNUNET_SYSERR;
- }
- if (GC_is_pid_bigger (pid, fc->last_pid_recv))
- {
- unsigned int delta;
-
- delta = pid - fc->last_pid_recv;
- 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))
- {
- LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n",
- pid, fc->last_pid_recv - 31);
- return GNUNET_SYSERR;
- }
- 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)
- {
- connection_change_state (c, CADET_CONNECTION_READY);
- if (NULL != c->t)
- {
- if (CADET_TUNNEL_WAITING == GCT_get_cstate (c->t))
- GCT_change_cstate (c->t, CADET_TUNNEL_READY);
- }
- }
- connection_reset_timeout (c, fwd);
-
- return fwd;
-}
-
-
-/**
- * Generic handler for cadet network encrypted traffic.
- *
- * @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)
- */
-static int
-handle_cadet_encrypted (const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_MessageHeader *message)
-{
- const struct GNUNET_CADET_Encrypted *otr_msg;
- const struct GNUNET_CADET_AX *ax_msg;
- const struct GNUNET_CADET_Hash* 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);
-
- minimum_size = sizeof (struct GNUNET_MessageHeader) + overhead;
- c = connection_get (cid);
- fwd = check_message (message,
- minimum_size,
- cid,
- c,
- peer,
- pid);
-
- /* If something went wrong, discard message. */
- if (GNUNET_SYSERR == fwd)
- {
- GCC_check_connections ();
- return GNUNET_OK;
- }
-
- /* Is this message for us? */
- if (GCC_is_terminal (c, fwd))
- {
- GNUNET_STATISTICS_update (stats, "# received encrypted", 1, GNUNET_NO);
-
- if (NULL == c->t)
- {
- GNUNET_break (GNUNET_NO != c->destroy);
- return GNUNET_OK;
- }
- GCT_handle_encrypted (c->t, message);
- GCC_send_ack (c, fwd, GNUNET_NO);
- GCC_check_connections ();
- return GNUNET_OK;
- }
-
- /* 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));
- GCC_check_connections ();
- return GNUNET_OK;
-}
-
-
-/**
- * Generic handler for cadet network encrypted traffic.
- *
- * @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)
- */
-static int
-handle_cadet_kx (const struct GNUNET_PeerIdentity *peer,
- const struct GNUNET_CADET_KX *msg)
-{
- const struct GNUNET_CADET_Hash* cid;
- struct CadetConnection *c;
- size_t expected_size;
- int fwd;
-
- GCC_check_connections ();
- cid = &msg->cid;
- 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);
-
- /* If something went wrong, discard message. */
- if (GNUNET_SYSERR == fwd)
- return GNUNET_OK;
-
- /* 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);
- if (NULL == c->t)
- {
- GNUNET_break (0);
- return GNUNET_OK;
- }
- GCT_handle_kx (c->t, &msg[1].header);
- GCC_check_connections ();
- return GNUNET_OK;
- }
-
- /* 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);
- }
- GCC_check_connections ();
-}
-
-
-/**
- * Initialize the connections subsystem
- *
- * @param c Configuration handle.
- */
-void
-GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
-{
- LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
- if (GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_MSGS_QUEUE",
- &max_msgs_queue))
- {
- GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
- "CADET", "MAX_MSGS_QUEUE", "MISSING");
- GNUNET_SCHEDULER_shutdown ();
- return;
- }
-
- if (GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_CONNECTIONS",
- &max_connections))
- {
- GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
- "CADET", "MAX_CONNECTIONS", "MISSING");
- GNUNET_SCHEDULER_shutdown ();
- return;
- }
-
- if (GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_time (c, "CADET", "REFRESH_CONNECTION_TIME",
- &refresh_connection_time))
- {
- GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
- "CADET", "REFRESH_CONNECTION_TIME", "MISSING");
- GNUNET_SCHEDULER_shutdown ();
- return;
- }
- create_connection_time = GNUNET_TIME_UNIT_SECONDS;
- connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
-}
-
-
-/**
- * Destroy each connection on shutdown.
- *
- * @param cls Closure (unused).
- * @param key Current key code (CID, unused).
- * @param value Value in the hash map (`struct CadetConnection`)
- *
- * @return #GNUNET_YES, because we should continue to iterate
- */
-static int
-shutdown_iterator (void *cls,
- const struct GNUNET_HashCode *key,
- void *value)
-{
- struct CadetConnection *c = value;
-
- c->state = CADET_CONNECTION_DESTROYED;
- GCC_destroy (c);
- return GNUNET_YES;
-}
-
-
-/**
- * Shut down the connections subsystem.
- */
-void
-GCC_shutdown (void)
-{
- 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);
- 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 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).
- */
-struct CadetConnection *
-GCC_new (const struct GNUNET_CADET_Hash *cid,
- struct CadetTunnel *t,
- struct CadetPeerPath *path,
- unsigned int own_pos)
-{
- struct CadetConnection *c;
- struct CadetPeerPath *cpath;
-
- GCC_check_connections ();
- cpath = path_duplicate (path);
- GNUNET_assert (NULL != cpath);
- 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));
- fc_init (&c->fwd_fc);
- fc_init (&c->bck_fc);
- c->fwd_fc.c = c;
- c->bck_fc.c = c;
-
- c->t = t;
- GNUNET_assert (own_pos <= cpath->length - 1);
- c->own_pos = own_pos;
- c->path = cpath;
- cpath->c = c;
- if (GNUNET_OK != register_neighbors (c))