2 This file is part of GNUnet.
3 Copyright (C) 2017 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file cadet/gnunet-service-cadet_core.c
23 * @brief cadet service; interaction with CORE service
24 * @author Bartlomiej Polot
25 * @author Christian Grothoff
27 * All functions in this file should use the prefix GCO (Gnunet Cadet cOre (bottom))
30 * - Optimization: given BROKEN messages, destroy paths (?)
33 #include "gnunet-service-cadet-new_core.h"
34 #include "gnunet-service-cadet-new_paths.h"
35 #include "gnunet-service-cadet-new_peer.h"
36 #include "gnunet-service-cadet-new_connection.h"
37 #include "gnunet-service-cadet-new_tunnels.h"
38 #include "gnunet_core_service.h"
39 #include "cadet_protocol.h"
42 #define LOG(level, ...) GNUNET_log_from(level,"cadet-cor",__VA_ARGS__)
46 * Number of messages we are willing to buffer per route.
48 #define ROUTE_BUFFER_SIZE 8
52 * Information we keep per direction for a route.
59 struct CadetPeer *hop;
62 * Route this direction is part of.
64 struct CadetRoute *my_route;
67 * Message queue manager for @e hop.
69 struct GCP_MessageQueueManager *mqm;
72 * Cyclic message buffer to @e hop.
74 struct GNUNET_MQ_Envelope *out_buffer[ROUTE_BUFFER_SIZE];
77 * Next write offset to use to append messages to @e out_buffer.
79 unsigned int out_wpos;
82 * Next read offset to use to retrieve messages from @e out_buffer.
84 unsigned int out_rpos;
87 * Is @e mqm currently ready for transmission?
95 * Description of a segment of a `struct CadetConnection` at the
96 * intermediate peers. Routes are basically entries in a peer's
97 * routing table for forwarding traffic. At both endpoints, the
98 * routes are terminated by a `struct CadetConnection`, which knows
99 * the complete `struct CadetPath` that is formed by the individual
106 * Information about the next hop on this route.
108 struct RouteDirection next;
111 * Information about the previous hop on this route.
113 struct RouteDirection prev;
116 * Unique identifier for the connection that uses this route.
118 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
121 * When was this route last in use?
123 struct GNUNET_TIME_Absolute last_use;
129 * Handle to the CORE service.
131 static struct GNUNET_CORE_Handle *core;
134 * Routes on which this peer is an intermediate.
136 static struct GNUNET_CONTAINER_MultiShortmap *routes;
140 * Get the route corresponding to a hash.
142 * @param cid hash generated from the connection identifier
144 static struct CadetRoute *
145 get_route (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
147 return GNUNET_CONTAINER_multishortmap_get (routes,
148 &cid->connection_of_tunnel);
153 * We message @a msg from @a prev. Find its route by @a cid and
154 * forward to the next hop. Drop and signal broken route if we do not
157 * @param prev previous hop (sender)
158 * @param cid connection identifier, tells us which route to use
159 * @param msg the message to forward
162 route_message (struct CadetPeer *prev,
163 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
164 const struct GNUNET_MessageHeader *msg)
166 struct CadetRoute *route;
167 struct RouteDirection *dir;
168 struct GNUNET_MQ_Envelope *env;
170 route = get_route (cid);
173 struct GNUNET_MQ_Envelope *env;
174 struct GNUNET_CADET_ConnectionBrokenMessage *bm;
176 env = GNUNET_MQ_msg (bm,
177 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
179 bm->peer1 = my_full_id;
184 dir = (prev == route->prev.hop) ? &route->next : &route->prev;
185 if (GNUNET_YES == dir->is_ready)
187 dir->is_ready = GNUNET_NO;
189 GNUNET_MQ_msg_copy (msg));
192 env = dir->out_buffer[dir->out_wpos];
195 /* Queue full, drop earliest message in queue */
196 GNUNET_assert (dir->out_rpos == dir->out_wpos);
197 GNUNET_MQ_discard (env);
199 if (ROUTE_BUFFER_SIZE == dir->out_rpos)
202 env = GNUNET_MQ_msg_copy (msg);
203 dir->out_buffer[dir->out_wpos] = env;
205 if (ROUTE_BUFFER_SIZE == dir->out_wpos)
211 * Check if the create_connection message has the appropriate size.
213 * @param cls Closure (unused).
214 * @param msg Message to check.
216 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
219 check_connection_create (void *cls,
220 const struct GNUNET_CADET_ConnectionCreateMessage *msg)
222 uint16_t size = ntohs (msg->header.size) - sizeof (*msg);
224 if (0 != (size % sizeof (struct GNUNET_PeerIdentity)))
234 * Free internal data of a route direction.
236 * @param dir direction to destroy (do NOT free memory of 'dir' itself)
239 destroy_direction (struct RouteDirection *dir)
241 for (unsigned int i=0;i<ROUTE_BUFFER_SIZE;i++)
242 if (NULL != dir->out_buffer[i])
244 GNUNET_MQ_discard (dir->out_buffer[i]);
245 dir->out_buffer[i] = NULL;
247 if (NULL != dir->mqm)
249 GCP_request_mq_cancel (dir->mqm,
257 * Destroy our state for @a route.
259 * @param route route to destroy
262 destroy_route (struct CadetRoute *route)
264 destroy_direction (&route->prev);
265 destroy_direction (&route->next);
271 * Send message that a route is broken between @a peer1 and @a peer2.
273 * @param target where to send the message
274 * @param cid connection identifier to use
275 * @param peer1 one of the peers where a link is broken
276 * @param peer2 another one of the peers where a link is broken
279 send_broken (struct RouteDirection *target,
280 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
281 const struct GNUNET_PeerIdentity *peer1,
282 const struct GNUNET_PeerIdentity *peer2)
284 struct GNUNET_MQ_Envelope *env;
285 struct GNUNET_CADET_ConnectionBrokenMessage *bm;
287 env = GNUNET_MQ_msg (bm,
288 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
294 GCP_request_mq_cancel (target->mqm,
301 * Function called when the message queue to the previous hop
302 * becomes available/unavailable. We expect this function to
303 * be called immediately when we register, and then again
304 * later if the connection ever goes down.
306 * @param cls the `struct RouteDirection`
307 * @param available #GNUNET_YES if sending is now possible,
308 * #GNUNET_NO if sending is no longer possible
309 * #GNUNET_SYSERR if sending is no longer possible
310 * and the last envelope was discarded
313 dir_ready_cb (void *cls,
316 struct RouteDirection *dir = cls;
317 struct CadetRoute *route = dir->my_route;
318 struct RouteDirection *odir;
320 if (GNUNET_YES == ready)
322 struct GNUNET_MQ_Envelope *env;
324 dir->is_ready = GNUNET_YES;
325 if (NULL != (env = dir->out_buffer[dir->out_rpos]))
327 dir->out_buffer[dir->out_rpos] = NULL;
329 if (ROUTE_BUFFER_SIZE == dir->out_rpos)
331 dir->is_ready = GNUNET_NO;
337 odir = (dir == &route->next) ? &route->prev : &route->next;
338 send_broken (&route->next,
340 GCP_get_id (odir->hop),
342 destroy_route (route);
347 * Initialize one of the directions of a route.
349 * @param route route the direction belongs to
350 * @param dir direction to initialize
351 * @param hop next hop on in the @a dir
354 dir_init (struct RouteDirection *dir,
355 struct CadetRoute *route,
356 struct CadetPeer *hop)
359 dir->my_route = route;
360 dir->mqm = GCP_request_mq (hop,
363 GNUNET_assert (GNUNET_YES == dir->is_ready);
368 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
370 * @param cls Closure (CadetPeer for neighbor that sent the message).
371 * @param msg Message itself.
374 handle_connection_create (void *cls,
375 const struct GNUNET_CADET_ConnectionCreateMessage *msg)
377 struct CadetPeer *sender = cls;
378 struct CadetPeer *next;
379 const struct GNUNET_PeerIdentity *pids = (const struct GNUNET_PeerIdentity *) &msg[1];
380 struct CadetRoute *route;
381 uint16_t size = ntohs (msg->header.size) - sizeof (*msg);
382 unsigned int path_length;
385 path_length = size / sizeof (struct GNUNET_PeerIdentity);
386 /* Initiator is at offset 0. */
387 for (off=1;off<path_length;off++)
388 if (0 == memcmp (&my_full_id,
390 sizeof (struct GNUNET_PeerIdentity)))
392 if (off == path_length)
394 /* We are not on the path, bogus request */
398 /* Check previous hop */
399 if (sender != GCP_get (&pids[off - 1],
402 /* sender is not on the path, not allowed */
407 get_route (&msg->cid))
409 /* Duplicate CREATE, pass it on, previous one might have been lost! */
410 route_message (sender,
415 if (off == path_length - 1)
417 /* We are the destination, create connection */
418 struct CadetConnection *cc;
419 struct CadetPeerPath *path;
420 struct CadetPeer *origin;
422 cc = GNUNET_CONTAINER_multishortmap_get (connections,
423 &msg->cid.connection_of_tunnel);
426 GCC_handle_duplicate_create (cc);
430 path = GCPP_get_path_from_route (path_length - 1,
432 origin = GCP_get (&pids[0],
434 GCT_add_inbound_connection (GCT_create_tunnel (origin),
439 /* We are merely a hop on the way, check if we can support the route */
440 next = GCP_get (&pids[off + 1],
442 if ( (NULL == next) ||
443 (GNUNET_NO == GCP_has_core_connection (next)) )
445 /* unworkable, send back BROKEN notification */
446 struct GNUNET_MQ_Envelope *env;
447 struct GNUNET_CADET_ConnectionBrokenMessage *bm;
449 env = GNUNET_MQ_msg (bm,
450 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
452 bm->peer1 = pids[off + 1];
453 bm->peer2 = my_full_id;
454 GCP_send_ooo (sender,
459 /* Workable route, create routing entry */
460 route = GNUNET_new (struct CadetRoute);
461 route->cid = msg->cid;
462 dir_init (&route->prev,
465 dir_init (&route->next,
468 GNUNET_assert (GNUNET_OK ==
469 GNUNET_CONTAINER_multishortmap_put (routes,
470 &route->cid.connection_of_tunnel,
472 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
477 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK
479 * @param cls Closure (CadetPeer for neighbor that sent the message).
480 * @param msg Message itself.
483 handle_connection_create_ack (void *cls,
484 const struct GNUNET_CADET_ConnectionCreateAckMessage *msg)
486 struct CadetPeer *peer = cls;
487 struct CadetConnection *cc;
489 /* First, check if ACK belongs to a connection that ends here. */
490 cc = GNUNET_CONTAINER_multishortmap_get (connections,
491 &msg->cid.connection_of_tunnel);
494 /* verify ACK came from the right direction */
495 struct CadetPeerPath *path = GCC_get_path (cc);
498 GCPP_get_peer_at_offset (path,
501 /* received ACK from unexpected direction, ignore! */
505 GCC_handle_connection_create_ack (cc);
509 /* We're just an intermediary peer, route the message along its path */
517 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
519 * @param cls Closure (CadetPeer for neighbor that sent the message).
520 * @param msg Message itself.
521 * @deprecated duplicate logic with #handle_destroy(); dedup!
524 handle_connection_broken (void *cls,
525 const struct GNUNET_CADET_ConnectionBrokenMessage *msg)
527 struct CadetPeer *peer = cls;
528 struct CadetConnection *cc;
529 struct CadetRoute *route;
531 /* First, check if message belongs to a connection that ends here. */
532 cc = GNUNET_CONTAINER_multishortmap_get (connections,
533 &msg->cid.connection_of_tunnel);
536 /* verify message came from the right direction */
537 struct CadetPeerPath *path = GCC_get_path (cc);
540 GCPP_get_peer_at_offset (path,
543 /* received message from unexpected direction, ignore! */
549 /* FIXME: also destroy the path up to the specified link! */
553 /* We're just an intermediary peer, route the message along its path */
554 route = get_route (&msg->cid);
558 destroy_route (route);
559 /* FIXME: also destroy paths we MAY have up to the specified link! */
564 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
566 * @param cls Closure (CadetPeer for neighbor that sent the message).
567 * @param msg Message itself.
570 handle_connection_destroy (void *cls,
571 const struct GNUNET_CADET_ConnectionDestroyMessage *msg)
573 struct CadetPeer *peer = cls;
574 struct CadetConnection *cc;
575 struct CadetRoute *route;
577 /* First, check if message belongs to a connection that ends here. */
578 cc = GNUNET_CONTAINER_multishortmap_get (connections,
579 &msg->cid.connection_of_tunnel);
582 /* verify message came from the right direction */
583 struct CadetPeerPath *path = GCC_get_path (cc);
586 GCPP_get_peer_at_offset (path,
589 /* received message from unexpected direction, ignore! */
597 /* We're just an intermediary peer, route the message along its path */
598 route = get_route (&msg->cid);
602 destroy_route (route);
607 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX
609 * @param cls Closure (CadetPeer for neighbor that sent the message).
610 * @param msg Message itself.
613 handle_tunnel_kx (void *cls,
614 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
616 struct CadetPeer *peer = cls;
617 struct CadetConnection *cc;
619 /* First, check if message belongs to a connection that ends here. */
620 cc = GNUNET_CONTAINER_multishortmap_get (connections,
621 &msg->cid.connection_of_tunnel);
624 /* verify message came from the right direction */
625 struct CadetPeerPath *path = GCC_get_path (cc);
628 GCPP_get_peer_at_offset (path,
631 /* received message from unexpected direction, ignore! */
640 /* We're just an intermediary peer, route the message along its path */
648 * Check if the encrypted message has the appropriate size.
650 * @param cls Closure (unused).
651 * @param msg Message to check.
653 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
656 check_tunnel_encrypted (void *cls,
657 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
664 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED.
666 * @param cls Closure (CadetPeer for neighbor that sent the message).
667 * @param msg Message itself.
670 handle_tunnel_encrypted (void *cls,
671 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
673 struct CadetPeer *peer = cls;
674 struct CadetConnection *cc;
676 /* First, check if message belongs to a connection that ends here. */
677 cc = GNUNET_CONTAINER_multishortmap_get (connections,
678 &msg->cid.connection_of_tunnel);
681 /* verify message came from the right direction */
682 struct CadetPeerPath *path = GCC_get_path (cc);
685 GCPP_get_peer_at_offset (path,
688 /* received message from unexpected direction, ignore! */
692 GCC_handle_encrypted (cc,
696 /* We're just an intermediary peer, route the message along its path */
704 * Function called after #GNUNET_CORE_connect has succeeded (or failed
705 * for good). Note that the private key of the peer is intentionally
706 * not exposed here; if you need it, your process should try to read
707 * the private key file directly (which should work if you are
708 * authorized...). Implementations of this function must not call
709 * #GNUNET_CORE_disconnect (other than by scheduling a new task to
713 * @param my_identity ID of this peer, NULL if we failed
716 core_init_cb (void *cls,
717 const struct GNUNET_PeerIdentity *my_identity)
719 if (NULL == my_identity)
727 sizeof (struct GNUNET_PeerIdentity)));
732 * Method called whenever a given peer connects.
735 * @param peer peer identity this notification is about
738 core_connect_cb (void *cls,
739 const struct GNUNET_PeerIdentity *peer,
740 struct GNUNET_MQ_Handle *mq)
742 struct CadetPeer *cp;
753 * Method called whenever a peer disconnects.
756 * @param peer peer identity this notification is about
759 core_disconnect_cb (void *cls,
760 const struct GNUNET_PeerIdentity *peer,
763 struct CadetPeer *cp = peer_cls;
771 * Initialize the CORE subsystem.
773 * @param c Configuration.
776 GCO_init (const struct GNUNET_CONFIGURATION_Handle *c)
778 struct GNUNET_MQ_MessageHandler handlers[] = {
779 GNUNET_MQ_hd_var_size (connection_create,
780 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
781 struct GNUNET_CADET_ConnectionCreateMessage,
783 GNUNET_MQ_hd_fixed_size (connection_create_ack,
784 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK,
785 struct GNUNET_CADET_ConnectionCreateAckMessage,
787 GNUNET_MQ_hd_fixed_size (connection_broken,
788 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
789 struct GNUNET_CADET_ConnectionBrokenMessage,
791 GNUNET_MQ_hd_fixed_size (connection_destroy,
792 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
793 struct GNUNET_CADET_ConnectionDestroyMessage,
795 GNUNET_MQ_hd_fixed_size (tunnel_kx,
796 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX,
797 struct GNUNET_CADET_TunnelKeyExchangeMessage,
799 GNUNET_MQ_hd_var_size (tunnel_encrypted,
800 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED,
801 struct GNUNET_CADET_TunnelEncryptedMessage,
803 GNUNET_MQ_handler_end ()
806 routes = GNUNET_CONTAINER_multishortmap_create (1024,
808 core = GNUNET_CORE_connect (c,
818 * Shut down the CORE subsystem.
825 GNUNET_CORE_disconnect (core);
828 GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (routes));
829 GNUNET_CONTAINER_multishortmap_destroy (routes);
832 /* end of gnunet-cadet-service_core.c */