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 LOG (GNUNET_ERROR_TYPE_DEBUG,
177 "Failed to route message of type %u from %s on connection %s: no route\n",
180 GNUNET_sh2s (&cid->connection_of_tunnel));
181 env = GNUNET_MQ_msg (bm,
182 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
184 bm->peer1 = my_full_id;
189 dir = (prev == route->prev.hop) ? &route->next : &route->prev;
190 if (GNUNET_YES == dir->is_ready)
192 LOG (GNUNET_ERROR_TYPE_DEBUG,
193 "Routing message of type %u from %s to %s on connection %s\n",
196 GNUNET_i2s (GCP_get_id (dir->hop)),
197 GNUNET_sh2s (&cid->connection_of_tunnel));
198 dir->is_ready = GNUNET_NO;
200 GNUNET_MQ_msg_copy (msg));
203 env = dir->out_buffer[dir->out_wpos];
206 /* Queue full, drop earliest message in queue */
207 LOG (GNUNET_ERROR_TYPE_DEBUG,
208 "Queue full due to new message of type %u from %s to %s on connection %s, dropping old message\n",
211 GNUNET_i2s (GCP_get_id (dir->hop)),
212 GNUNET_sh2s (&cid->connection_of_tunnel));
213 GNUNET_assert (dir->out_rpos == dir->out_wpos);
214 GNUNET_MQ_discard (env);
216 if (ROUTE_BUFFER_SIZE == dir->out_rpos)
219 LOG (GNUNET_ERROR_TYPE_DEBUG,
220 "Queueing new message of type %u from %s to %s on connection %s\n",
223 GNUNET_i2s (GCP_get_id (dir->hop)),
224 GNUNET_sh2s (&cid->connection_of_tunnel));
225 env = GNUNET_MQ_msg_copy (msg);
226 dir->out_buffer[dir->out_wpos] = env;
228 if (ROUTE_BUFFER_SIZE == dir->out_wpos)
234 * Check if the create_connection message has the appropriate size.
236 * @param cls Closure (unused).
237 * @param msg Message to check.
239 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
242 check_connection_create (void *cls,
243 const struct GNUNET_CADET_ConnectionCreateMessage *msg)
245 uint16_t size = ntohs (msg->header.size) - sizeof (*msg);
247 if (0 != (size % sizeof (struct GNUNET_PeerIdentity)))
257 * Free internal data of a route direction.
259 * @param dir direction to destroy (do NOT free memory of 'dir' itself)
262 destroy_direction (struct RouteDirection *dir)
264 for (unsigned int i=0;i<ROUTE_BUFFER_SIZE;i++)
265 if (NULL != dir->out_buffer[i])
267 GNUNET_MQ_discard (dir->out_buffer[i]);
268 dir->out_buffer[i] = NULL;
270 if (NULL != dir->mqm)
272 GCP_request_mq_cancel (dir->mqm,
280 * Destroy our state for @a route.
282 * @param route route to destroy
285 destroy_route (struct CadetRoute *route)
287 LOG (GNUNET_ERROR_TYPE_DEBUG,
288 "Destroying route from %s to %s of connection %s\n",
289 GNUNET_i2s (GCP_get_id (route->prev.hop)),
290 GNUNET_i2s2 (GCP_get_id (route->next.hop)),
291 GNUNET_sh2s (&route->cid.connection_of_tunnel));
292 destroy_direction (&route->prev);
293 destroy_direction (&route->next);
299 * Send message that a route is broken between @a peer1 and @a peer2.
301 * @param target where to send the message
302 * @param cid connection identifier to use
303 * @param peer1 one of the peers where a link is broken
304 * @param peer2 another one of the peers where a link is broken
307 send_broken (struct RouteDirection *target,
308 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
309 const struct GNUNET_PeerIdentity *peer1,
310 const struct GNUNET_PeerIdentity *peer2)
312 struct GNUNET_MQ_Envelope *env;
313 struct GNUNET_CADET_ConnectionBrokenMessage *bm;
315 if (NULL == target->mqm)
316 return; /* Can't send notification, connection is down! */
317 LOG (GNUNET_ERROR_TYPE_DEBUG,
318 "Notifying %s about BROKEN route at %s-%s of connection %s\n",
319 GCP_2s (target->hop),
322 GNUNET_sh2s (&cid->connection_of_tunnel));
324 env = GNUNET_MQ_msg (bm,
325 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
332 GCP_request_mq_cancel (target->mqm,
339 * Function called when the message queue to the previous hop
340 * becomes available/unavailable. We expect this function to
341 * be called immediately when we register, and then again
342 * later if the connection ever goes down.
344 * @param cls the `struct RouteDirection`
345 * @param available #GNUNET_YES if sending is now possible,
346 * #GNUNET_NO if sending is no longer possible
347 * #GNUNET_SYSERR if sending is no longer possible
348 * and the last envelope was discarded
351 dir_ready_cb (void *cls,
354 struct RouteDirection *dir = cls;
355 struct CadetRoute *route = dir->my_route;
356 struct RouteDirection *odir;
358 if (GNUNET_YES == ready)
360 struct GNUNET_MQ_Envelope *env;
362 dir->is_ready = GNUNET_YES;
363 if (NULL != (env = dir->out_buffer[dir->out_rpos]))
365 dir->out_buffer[dir->out_rpos] = NULL;
367 if (ROUTE_BUFFER_SIZE == dir->out_rpos)
369 dir->is_ready = GNUNET_NO;
375 odir = (dir == &route->next) ? &route->prev : &route->next;
376 send_broken (&route->next,
378 GCP_get_id (odir->hop),
380 destroy_route (route);
385 * Initialize one of the directions of a route.
387 * @param route route the direction belongs to
388 * @param dir direction to initialize
389 * @param hop next hop on in the @a dir
392 dir_init (struct RouteDirection *dir,
393 struct CadetRoute *route,
394 struct CadetPeer *hop)
397 dir->my_route = route;
398 dir->mqm = GCP_request_mq (hop,
401 GNUNET_assert (GNUNET_YES == dir->is_ready);
406 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
408 * @param cls Closure (CadetPeer for neighbor that sent the message).
409 * @param msg Message itself.
412 handle_connection_create (void *cls,
413 const struct GNUNET_CADET_ConnectionCreateMessage *msg)
415 struct CadetPeer *sender = cls;
416 struct CadetPeer *next;
417 const struct GNUNET_PeerIdentity *pids = (const struct GNUNET_PeerIdentity *) &msg[1];
418 struct CadetRoute *route;
419 uint16_t size = ntohs (msg->header.size) - sizeof (*msg);
420 unsigned int path_length;
423 path_length = size / sizeof (struct GNUNET_PeerIdentity);
424 /* Initiator is at offset 0. */
425 for (off=1;off<path_length;off++)
426 if (0 == memcmp (&my_full_id,
428 sizeof (struct GNUNET_PeerIdentity)))
430 if (off == path_length)
432 /* We are not on the path, bogus request */
436 /* Check previous hop */
437 if (sender != GCP_get (&pids[off - 1],
440 /* sender is not on the path, not allowed */
445 get_route (&msg->cid))
447 /* Duplicate CREATE, pass it on, previous one might have been lost! */
448 LOG (GNUNET_ERROR_TYPE_DEBUG,
449 "Passing on duplicate CREATE message on connection %s\n",
450 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
451 route_message (sender,
456 if (off == path_length - 1)
458 /* We are the destination, create connection */
459 struct CadetConnection *cc;
460 struct CadetPeerPath *path;
461 struct CadetPeer *origin;
463 cc = GNUNET_CONTAINER_multishortmap_get (connections,
464 &msg->cid.connection_of_tunnel);
467 LOG (GNUNET_ERROR_TYPE_DEBUG,
468 "Received duplicate CREATE message on connection %s\n",
469 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
470 GCC_handle_duplicate_create (cc);
474 origin = GCP_get (&pids[0],
476 LOG (GNUNET_ERROR_TYPE_DEBUG,
477 "Received CREATE message from %s for connection %s, building inverse path\n",
479 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
480 path = GCPP_get_path_from_route (path_length - 1,
482 GCT_add_inbound_connection (GCP_get_tunnel (origin,
488 /* We are merely a hop on the way, check if we can support the route */
489 next = GCP_get (&pids[off + 1],
491 if ( (NULL == next) ||
492 (GNUNET_NO == GCP_has_core_connection (next)) )
494 /* unworkable, send back BROKEN notification */
495 struct GNUNET_MQ_Envelope *env;
496 struct GNUNET_CADET_ConnectionBrokenMessage *bm;
498 LOG (GNUNET_ERROR_TYPE_DEBUG,
499 "Received CONNECTION_CREATE from %s for %s. Next hop %s:%u is down. Sending BROKEN\n",
501 GNUNET_sh2s (&msg->cid.connection_of_tunnel),
502 GNUNET_i2s (&pids[off + 1]),
504 env = GNUNET_MQ_msg (bm,
505 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
507 bm->peer1 = pids[off + 1];
508 bm->peer2 = my_full_id;
509 GCP_send_ooo (sender,
514 /* Workable route, create routing entry */
515 LOG (GNUNET_ERROR_TYPE_DEBUG,
516 "Received CONNECTION_CREATE from %s for %s. Next hop %s:%u is up. Creating route\n",
518 GNUNET_sh2s (&msg->cid.connection_of_tunnel),
519 GNUNET_i2s (&pids[off + 1]),
521 route = GNUNET_new (struct CadetRoute);
522 route->cid = msg->cid;
523 dir_init (&route->prev,
526 dir_init (&route->next,
529 GNUNET_assert (GNUNET_OK ==
530 GNUNET_CONTAINER_multishortmap_put (routes,
531 &route->cid.connection_of_tunnel,
533 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
538 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK
540 * @param cls Closure (CadetPeer for neighbor that sent the message).
541 * @param msg Message itself.
544 handle_connection_create_ack (void *cls,
545 const struct GNUNET_CADET_ConnectionCreateAckMessage *msg)
547 struct CadetPeer *peer = cls;
548 struct CadetConnection *cc;
550 /* First, check if ACK belongs to a connection that ends here. */
551 cc = GNUNET_CONTAINER_multishortmap_get (connections,
552 &msg->cid.connection_of_tunnel);
555 /* verify ACK came from the right direction */
556 struct CadetPeerPath *path = GCC_get_path (cc);
559 GCPP_get_peer_at_offset (path,
562 /* received ACK from unexpected direction, ignore! */
566 LOG (GNUNET_ERROR_TYPE_DEBUG,
567 "Received CONNECTION_CREATE_ACK for connection %s.\n",
568 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
569 GCC_handle_connection_create_ack (cc);
573 /* We're just an intermediary peer, route the message along its path */
581 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
583 * @param cls Closure (CadetPeer for neighbor that sent the message).
584 * @param msg Message itself.
585 * @deprecated duplicate logic with #handle_destroy(); dedup!
588 handle_connection_broken (void *cls,
589 const struct GNUNET_CADET_ConnectionBrokenMessage *msg)
591 struct CadetPeer *peer = cls;
592 struct CadetConnection *cc;
593 struct CadetRoute *route;
595 /* First, check if message belongs to a connection that ends here. */
596 cc = GNUNET_CONTAINER_multishortmap_get (connections,
597 &msg->cid.connection_of_tunnel);
600 /* verify message came from the right direction */
601 struct CadetPeerPath *path = GCC_get_path (cc);
604 GCPP_get_peer_at_offset (path,
607 /* received message from unexpected direction, ignore! */
611 LOG (GNUNET_ERROR_TYPE_DEBUG,
612 "Received CONNECTION_BROKEN for connection %s. Destroying it.\n",
613 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
616 /* FIXME: also destroy the path up to the specified link! */
620 /* We're just an intermediary peer, route the message along its path */
621 route = get_route (&msg->cid);
625 destroy_route (route);
626 /* FIXME: also destroy paths we MAY have up to the specified link! */
631 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
633 * @param cls Closure (CadetPeer for neighbor that sent the message).
634 * @param msg Message itself.
637 handle_connection_destroy (void *cls,
638 const struct GNUNET_CADET_ConnectionDestroyMessage *msg)
640 struct CadetPeer *peer = cls;
641 struct CadetConnection *cc;
642 struct CadetRoute *route;
644 /* First, check if message belongs to a connection that ends here. */
645 cc = GNUNET_CONTAINER_multishortmap_get (connections,
646 &msg->cid.connection_of_tunnel);
649 /* verify message came from the right direction */
650 struct CadetPeerPath *path = GCC_get_path (cc);
653 GCPP_get_peer_at_offset (path,
656 /* received message from unexpected direction, ignore! */
660 LOG (GNUNET_ERROR_TYPE_DEBUG,
661 "Received CONNECTION_DESTROY for connection %s. Destroying connection.\n",
662 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
668 /* We're just an intermediary peer, route the message along its path */
669 LOG (GNUNET_ERROR_TYPE_DEBUG,
670 "Received CONNECTION_DESTROY for connection %s. Destroying route.\n",
671 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
672 route = get_route (&msg->cid);
676 destroy_route (route);
681 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX
683 * @param cls Closure (CadetPeer for neighbor that sent the message).
684 * @param msg Message itself.
687 handle_tunnel_kx (void *cls,
688 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
690 struct CadetPeer *peer = cls;
691 struct CadetConnection *cc;
693 /* First, check if message belongs to a connection that ends here. */
694 cc = GNUNET_CONTAINER_multishortmap_get (connections,
695 &msg->cid.connection_of_tunnel);
698 /* verify message came from the right direction */
699 struct CadetPeerPath *path = GCC_get_path (cc);
702 GCPP_get_peer_at_offset (path,
705 /* received message from unexpected direction, ignore! */
714 /* We're just an intermediary peer, route the message along its path */
722 * Check if the encrypted message has the appropriate size.
724 * @param cls Closure (unused).
725 * @param msg Message to check.
727 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
730 check_tunnel_encrypted (void *cls,
731 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
738 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED.
740 * @param cls Closure (CadetPeer for neighbor that sent the message).
741 * @param msg Message itself.
744 handle_tunnel_encrypted (void *cls,
745 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
747 struct CadetPeer *peer = cls;
748 struct CadetConnection *cc;
750 /* First, check if message belongs to a connection that ends here. */
751 cc = GNUNET_CONTAINER_multishortmap_get (connections,
752 &msg->cid.connection_of_tunnel);
755 /* verify message came from the right direction */
756 struct CadetPeerPath *path = GCC_get_path (cc);
759 GCPP_get_peer_at_offset (path,
762 /* received message from unexpected direction, ignore! */
766 GCC_handle_encrypted (cc,
770 /* We're just an intermediary peer, route the message along its path */
778 * Function called after #GNUNET_CORE_connect has succeeded (or failed
779 * for good). Note that the private key of the peer is intentionally
780 * not exposed here; if you need it, your process should try to read
781 * the private key file directly (which should work if you are
782 * authorized...). Implementations of this function must not call
783 * #GNUNET_CORE_disconnect (other than by scheduling a new task to
787 * @param my_identity ID of this peer, NULL if we failed
790 core_init_cb (void *cls,
791 const struct GNUNET_PeerIdentity *my_identity)
793 if (NULL == my_identity)
801 sizeof (struct GNUNET_PeerIdentity)));
806 * Method called whenever a given peer connects.
809 * @param peer peer identity this notification is about
812 core_connect_cb (void *cls,
813 const struct GNUNET_PeerIdentity *peer,
814 struct GNUNET_MQ_Handle *mq)
816 struct CadetPeer *cp;
818 LOG (GNUNET_ERROR_TYPE_DEBUG,
819 "CORE connection to peer %s was established.\n",
830 * Method called whenever a peer disconnects.
833 * @param peer peer identity this notification is about
836 core_disconnect_cb (void *cls,
837 const struct GNUNET_PeerIdentity *peer,
840 struct CadetPeer *cp = peer_cls;
842 LOG (GNUNET_ERROR_TYPE_DEBUG,
843 "CORE connection to peer %s went down.\n",
851 * Initialize the CORE subsystem.
853 * @param c Configuration.
856 GCO_init (const struct GNUNET_CONFIGURATION_Handle *c)
858 struct GNUNET_MQ_MessageHandler handlers[] = {
859 GNUNET_MQ_hd_var_size (connection_create,
860 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
861 struct GNUNET_CADET_ConnectionCreateMessage,
863 GNUNET_MQ_hd_fixed_size (connection_create_ack,
864 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK,
865 struct GNUNET_CADET_ConnectionCreateAckMessage,
867 GNUNET_MQ_hd_fixed_size (connection_broken,
868 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
869 struct GNUNET_CADET_ConnectionBrokenMessage,
871 GNUNET_MQ_hd_fixed_size (connection_destroy,
872 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
873 struct GNUNET_CADET_ConnectionDestroyMessage,
875 GNUNET_MQ_hd_fixed_size (tunnel_kx,
876 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX,
877 struct GNUNET_CADET_TunnelKeyExchangeMessage,
879 GNUNET_MQ_hd_var_size (tunnel_encrypted,
880 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED,
881 struct GNUNET_CADET_TunnelEncryptedMessage,
883 GNUNET_MQ_handler_end ()
886 routes = GNUNET_CONTAINER_multishortmap_create (1024,
888 core = GNUNET_CORE_connect (c,
898 * Shut down the CORE subsystem.
905 GNUNET_CORE_disconnect (core);
908 GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (routes));
909 GNUNET_CONTAINER_multishortmap_destroy (routes);
912 /* end of gnunet-cadet-service_core.c */