* All functions in this file should use the prefix GCO (Gnunet Cadet cOre (bottom))
*
* TODO:
+ * - properly implement GLOBAL message buffer, instead of per-route buffers
+ * - do NOT use buffering if the route options say no buffer!
* - Optimization: given BROKEN messages, destroy paths (?)
*/
#include "platform.h"
/**
* Number of messages we are willing to buffer per route.
+ * FIXME: have global buffer pool instead!
*/
#define ROUTE_BUFFER_SIZE 8
*/
struct GNUNET_TIME_Absolute last_use;
+ /**
+ * Position of this route in the #route_heap.
+ */
+ struct GNUNET_CONTAINER_HeapNode *hn;
+
+ /**
+ * Options for the route, control buffering.
+ */
+ enum GNUNET_CADET_ChannelOption options;
};
*/
static struct GNUNET_CONTAINER_MultiShortmap *routes;
+/**
+ * Heap of routes, MIN-sorted by last activity.
+ */
+static struct GNUNET_CONTAINER_Heap *route_heap;
+
+/**
+ * Maximum number of concurrent routes this peer will support.
+ */
+static unsigned long long max_routes;
+
+/**
+ * Task to timeout routes.
+ */
+static struct GNUNET_SCHEDULER_Task *timeout_task;
+
/**
* Get the route corresponding to a hash.
env);
return;
}
+ route->last_use = GNUNET_TIME_absolute_get ();
+ GNUNET_CONTAINER_heap_update_cost (route->hn,
+ route->last_use.abs_value_us);
dir = (prev == route->prev.hop) ? &route->next : &route->prev;
if (GNUNET_YES == dir->is_ready)
{
GNUNET_i2s (GCP_get_id (route->prev.hop)),
GNUNET_i2s2 (GCP_get_id (route->next.hop)),
GNUNET_sh2s (&route->cid.connection_of_tunnel));
+ GNUNET_assert (route ==
+ GNUNET_CONTAINER_heap_remove_node (route->hn));
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multishortmap_remove (routes,
+ &route->cid.connection_of_tunnel,
+ route));
destroy_direction (&route->prev);
destroy_direction (&route->next);
GNUNET_free (route);
}
+/**
+ * Function called to check if any routes have timed out, and if
+ * so, to clean them up. Finally, schedules itself again at the
+ * earliest time where there might be more work.
+ *
+ * @param cls NULL
+ */
+static void
+timeout_cb (void *cls)
+{
+ struct CadetRoute *r;
+ struct GNUNET_TIME_Relative linger;
+ struct GNUNET_TIME_Absolute exp;
+
+ timeout_task = NULL;
+ linger = GNUNET_TIME_relative_multiply (keepalive_period,
+ 3);
+ while (NULL != (r = GNUNET_CONTAINER_heap_peek (route_heap)))
+ {
+ exp = GNUNET_TIME_absolute_add (r->last_use,
+ linger);
+ if (0 != GNUNET_TIME_absolute_get_duration (exp).rel_value_us)
+ {
+ /* Route not yet timed out, wait until it does. */
+ timeout_task = GNUNET_SCHEDULER_add_at (exp,
+ &timeout_cb,
+ NULL);
+ return;
+ }
+ send_broken (&r->prev,
+ &r->cid,
+ NULL,
+ NULL);
+ send_broken (&r->next,
+ &r->cid,
+ NULL,
+ NULL);
+ destroy_route (r);
+ }
+ /* No more routes left, so no need for a #timeout_task */
+}
+
+
/**
* Function called when the message queue to the previous hop
* becomes available/unavailable. We expect this function to
}
+/**
+ * We could not create the desired route. Send a
+ * #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
+ * message to @a target.
+ *
+ * @param target who should receive the message
+ * @param cid identifier of the connection/route that failed
+ * @param failure_at neighbour with which we failed to route,
+ * or NULL.
+ */
+static void
+send_broken_without_mqm (struct CadetPeer *target,
+ const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
+ const struct GNUNET_PeerIdentity *failure_at)
+{
+ struct GNUNET_MQ_Envelope *env;
+ struct GNUNET_CADET_ConnectionBrokenMessage *bm;
+
+ env = GNUNET_MQ_msg (bm,
+ GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
+ bm->cid = *cid;
+ bm->peer1 = my_full_id;
+ if (NULL != failure_at)
+ bm->peer2 = *failure_at;
+ GCP_send_ooo (target,
+ env);
+}
+
+
/**
* Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
*
uint16_t size = ntohs (msg->header.size) - sizeof (*msg);
unsigned int path_length;
unsigned int off;
+ enum GNUNET_CADET_ChannelOption options;
+ options = (enum GNUNET_CADET_ChannelOption) ntohl (msg->options);
path_length = size / sizeof (struct GNUNET_PeerIdentity);
/* Initiator is at offset 0. */
for (off=1;off<path_length;off++)
GCT_add_inbound_connection (GCP_get_tunnel (origin,
GNUNET_YES),
&msg->cid,
+ (enum GNUNET_CADET_ChannelOption) ntohl (msg->options),
path))
{
/* Send back BROKEN: duplicate connection on the same path,
we will use the other one. */
- struct GNUNET_MQ_Envelope *env;
- struct GNUNET_CADET_ConnectionBrokenMessage *bm;
-
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Received CADET_CONNECTION_CREATE from %s for %s, but %s already has a connection. Sending BROKEN\n",
GCP_2s (sender),
GNUNET_sh2s (&msg->cid.connection_of_tunnel),
GCPP_2s (path));
- env = GNUNET_MQ_msg (bm,
- GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
- bm->cid = msg->cid;
- bm->peer1 = my_full_id;
- GCP_send_ooo (sender,
- env);
+ send_broken_without_mqm (sender,
+ &msg->cid,
+ NULL);
return;
}
return;
(GNUNET_NO == GCP_has_core_connection (next)) )
{
/* unworkable, send back BROKEN notification */
- struct GNUNET_MQ_Envelope *env;
- struct GNUNET_CADET_ConnectionBrokenMessage *bm;
-
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Received CADET_CONNECTION_CREATE from %s for %s. Next hop %s:%u is down. Sending BROKEN\n",
GCP_2s (sender),
GNUNET_sh2s (&msg->cid.connection_of_tunnel),
GNUNET_i2s (&pids[off + 1]),
off + 1);
- env = GNUNET_MQ_msg (bm,
- GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
- bm->cid = msg->cid;
- bm->peer1 = pids[off + 1];
- bm->peer2 = my_full_id;
- GCP_send_ooo (sender,
- env);
+ send_broken_without_mqm (sender,
+ &msg->cid,
+ &pids[off + 1]);
+ return;
+ }
+ if (max_routes <= GNUNET_CONTAINER_multishortmap_size (routes))
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Received CADET_CONNECTION_CREATE from %s for %s. We have reached our route limit. Sending BROKEN\n",
+ GCP_2s (sender),
+ GNUNET_sh2s (&msg->cid.connection_of_tunnel));
+ send_broken_without_mqm (sender,
+ &msg->cid,
+ &pids[off - 1]);
return;
}
GNUNET_i2s (&pids[off + 1]),
off + 1);
route = GNUNET_new (struct CadetRoute);
+ route->options = options;
route->cid = msg->cid;
+ route->last_use = GNUNET_TIME_absolute_get ();
dir_init (&route->prev,
route,
sender);
&route->cid.connection_of_tunnel,
route,
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+ route->hn = GNUNET_CONTAINER_heap_insert (route_heap,
+ route,
+ route->last_use.abs_value_us);
+ if (NULL == timeout_task)
+ timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (keepalive_period,
+ 3),
+ &timeout_cb,
+ NULL);
}
}
/* We're just an intermediary peer, route the message along its path */
- route = get_route (&msg->cid);
route_message (peer,
&msg->cid,
&msg->header);
- destroy_route (route);
+ route = get_route (&msg->cid);
+ if (NULL != route)
+ destroy_route (route);
/* FIXME: also destroy paths we MAY have up to the specified link! */
}
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Received CONNECTION_DESTROY for connection %s. Destroying route.\n",
GNUNET_sh2s (&msg->cid.connection_of_tunnel));
- route = get_route (&msg->cid);
route_message (peer,
&msg->cid,
&msg->header);
- destroy_route (route);
+ route = get_route (&msg->cid);
+ if (NULL != route)
+ destroy_route (route);
}
GNUNET_MQ_handler_end ()
};
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_number (c,
+ "CADET",
+ "MAX_ROUTES",
+ &max_routes))
+ max_routes = 10000;
routes = GNUNET_CONTAINER_multishortmap_create (1024,
GNUNET_NO);
+ route_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
core = GNUNET_CORE_connect (c,
NULL,
&core_init_cb,
}
GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (routes));
GNUNET_CONTAINER_multishortmap_destroy (routes);
+ routes = NULL;
+ GNUNET_CONTAINER_heap_destroy (route_heap);
+ route_heap = NULL;
+ if (NULL != timeout_task)
+ {
+ GNUNET_SCHEDULER_cancel (timeout_task);
+ timeout_task = NULL;
+ }
}
/* end of gnunet-cadet-service_core.c */