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 LOG (GNUNET_ERROR_TYPE_DEBUG,
316 "Notifying %s about BROKEN route at %s-%s of connection %s\n",
317 GCP_2s (target->hop),
320 GNUNET_sh2s (&cid->connection_of_tunnel));
322 env = GNUNET_MQ_msg (bm,
323 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
329 GCP_request_mq_cancel (target->mqm,
336 * Function called when the message queue to the previous hop
337 * becomes available/unavailable. We expect this function to
338 * be called immediately when we register, and then again
339 * later if the connection ever goes down.
341 * @param cls the `struct RouteDirection`
342 * @param available #GNUNET_YES if sending is now possible,
343 * #GNUNET_NO if sending is no longer possible
344 * #GNUNET_SYSERR if sending is no longer possible
345 * and the last envelope was discarded
348 dir_ready_cb (void *cls,
351 struct RouteDirection *dir = cls;
352 struct CadetRoute *route = dir->my_route;
353 struct RouteDirection *odir;
355 if (GNUNET_YES == ready)
357 struct GNUNET_MQ_Envelope *env;
359 dir->is_ready = GNUNET_YES;
360 if (NULL != (env = dir->out_buffer[dir->out_rpos]))
362 dir->out_buffer[dir->out_rpos] = NULL;
364 if (ROUTE_BUFFER_SIZE == dir->out_rpos)
366 dir->is_ready = GNUNET_NO;
372 odir = (dir == &route->next) ? &route->prev : &route->next;
373 send_broken (&route->next,
375 GCP_get_id (odir->hop),
377 destroy_route (route);
382 * Initialize one of the directions of a route.
384 * @param route route the direction belongs to
385 * @param dir direction to initialize
386 * @param hop next hop on in the @a dir
389 dir_init (struct RouteDirection *dir,
390 struct CadetRoute *route,
391 struct CadetPeer *hop)
394 dir->my_route = route;
395 dir->mqm = GCP_request_mq (hop,
398 GNUNET_assert (GNUNET_YES == dir->is_ready);
403 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
405 * @param cls Closure (CadetPeer for neighbor that sent the message).
406 * @param msg Message itself.
409 handle_connection_create (void *cls,
410 const struct GNUNET_CADET_ConnectionCreateMessage *msg)
412 struct CadetPeer *sender = cls;
413 struct CadetPeer *next;
414 const struct GNUNET_PeerIdentity *pids = (const struct GNUNET_PeerIdentity *) &msg[1];
415 struct CadetRoute *route;
416 uint16_t size = ntohs (msg->header.size) - sizeof (*msg);
417 unsigned int path_length;
420 path_length = size / sizeof (struct GNUNET_PeerIdentity);
421 /* Initiator is at offset 0. */
422 for (off=1;off<path_length;off++)
423 if (0 == memcmp (&my_full_id,
425 sizeof (struct GNUNET_PeerIdentity)))
427 if (off == path_length)
429 /* We are not on the path, bogus request */
433 /* Check previous hop */
434 if (sender != GCP_get (&pids[off - 1],
437 /* sender is not on the path, not allowed */
442 get_route (&msg->cid))
444 /* Duplicate CREATE, pass it on, previous one might have been lost! */
445 LOG (GNUNET_ERROR_TYPE_DEBUG,
446 "Passing on duplicate CREATE message on connection %s\n",
447 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
448 route_message (sender,
453 if (off == path_length - 1)
455 /* We are the destination, create connection */
456 struct CadetConnection *cc;
457 struct CadetPeerPath *path;
458 struct CadetPeer *origin;
460 cc = GNUNET_CONTAINER_multishortmap_get (connections,
461 &msg->cid.connection_of_tunnel);
464 LOG (GNUNET_ERROR_TYPE_DEBUG,
465 "Received duplicate CREATE message on connection %s\n",
466 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
467 GCC_handle_duplicate_create (cc);
471 path = GCPP_get_path_from_route (path_length - 1,
473 origin = GCP_get (&pids[0],
475 LOG (GNUNET_ERROR_TYPE_DEBUG,
476 "Received CREATE message from %s via path %s for connection %s\n",
479 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
480 GCT_add_inbound_connection (GCT_create_tunnel (origin),
485 /* We are merely a hop on the way, check if we can support the route */
486 next = GCP_get (&pids[off + 1],
488 if ( (NULL == next) ||
489 (GNUNET_NO == GCP_has_core_connection (next)) )
491 /* unworkable, send back BROKEN notification */
492 struct GNUNET_MQ_Envelope *env;
493 struct GNUNET_CADET_ConnectionBrokenMessage *bm;
495 LOG (GNUNET_ERROR_TYPE_DEBUG,
496 "Received CONNECTION_CREATE from %s for %s. Next hop %s:%u is down. Sending BROKEN\n",
498 GNUNET_sh2s (&msg->cid.connection_of_tunnel),
499 GNUNET_i2s (&pids[off + 1]),
501 env = GNUNET_MQ_msg (bm,
502 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
504 bm->peer1 = pids[off + 1];
505 bm->peer2 = my_full_id;
506 GCP_send_ooo (sender,
511 /* Workable route, create routing entry */
512 LOG (GNUNET_ERROR_TYPE_DEBUG,
513 "Received CONNECTION_CREATE from %s for %s. Next hop %s:%u is up. Creating route\n",
515 GNUNET_sh2s (&msg->cid.connection_of_tunnel),
516 GNUNET_i2s (&pids[off + 1]),
518 route = GNUNET_new (struct CadetRoute);
519 route->cid = msg->cid;
520 dir_init (&route->prev,
523 dir_init (&route->next,
526 GNUNET_assert (GNUNET_OK ==
527 GNUNET_CONTAINER_multishortmap_put (routes,
528 &route->cid.connection_of_tunnel,
530 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
535 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK
537 * @param cls Closure (CadetPeer for neighbor that sent the message).
538 * @param msg Message itself.
541 handle_connection_create_ack (void *cls,
542 const struct GNUNET_CADET_ConnectionCreateAckMessage *msg)
544 struct CadetPeer *peer = cls;
545 struct CadetConnection *cc;
547 /* First, check if ACK belongs to a connection that ends here. */
548 cc = GNUNET_CONTAINER_multishortmap_get (connections,
549 &msg->cid.connection_of_tunnel);
552 /* verify ACK came from the right direction */
553 struct CadetPeerPath *path = GCC_get_path (cc);
556 GCPP_get_peer_at_offset (path,
559 /* received ACK from unexpected direction, ignore! */
563 LOG (GNUNET_ERROR_TYPE_DEBUG,
564 "Received CONNECTION_CREATE_ACK for connection %s.\n",
565 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
566 GCC_handle_connection_create_ack (cc);
570 /* We're just an intermediary peer, route the message along its path */
578 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
580 * @param cls Closure (CadetPeer for neighbor that sent the message).
581 * @param msg Message itself.
582 * @deprecated duplicate logic with #handle_destroy(); dedup!
585 handle_connection_broken (void *cls,
586 const struct GNUNET_CADET_ConnectionBrokenMessage *msg)
588 struct CadetPeer *peer = cls;
589 struct CadetConnection *cc;
590 struct CadetRoute *route;
592 /* First, check if message belongs to a connection that ends here. */
593 cc = GNUNET_CONTAINER_multishortmap_get (connections,
594 &msg->cid.connection_of_tunnel);
597 /* verify message came from the right direction */
598 struct CadetPeerPath *path = GCC_get_path (cc);
601 GCPP_get_peer_at_offset (path,
604 /* received message from unexpected direction, ignore! */
608 LOG (GNUNET_ERROR_TYPE_DEBUG,
609 "Received CONNECTION_BROKEN for connection %s. Destroying it.\n",
610 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
613 /* FIXME: also destroy the path up to the specified link! */
617 /* We're just an intermediary peer, route the message along its path */
618 route = get_route (&msg->cid);
622 destroy_route (route);
623 /* FIXME: also destroy paths we MAY have up to the specified link! */
628 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
630 * @param cls Closure (CadetPeer for neighbor that sent the message).
631 * @param msg Message itself.
634 handle_connection_destroy (void *cls,
635 const struct GNUNET_CADET_ConnectionDestroyMessage *msg)
637 struct CadetPeer *peer = cls;
638 struct CadetConnection *cc;
639 struct CadetRoute *route;
641 /* First, check if message belongs to a connection that ends here. */
642 cc = GNUNET_CONTAINER_multishortmap_get (connections,
643 &msg->cid.connection_of_tunnel);
646 /* verify message came from the right direction */
647 struct CadetPeerPath *path = GCC_get_path (cc);
650 GCPP_get_peer_at_offset (path,
653 /* received message from unexpected direction, ignore! */
657 LOG (GNUNET_ERROR_TYPE_DEBUG,
658 "Received CONNECTION_DESTROY for connection %s. Destroying connection.\n",
659 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
665 /* We're just an intermediary peer, route the message along its path */
666 LOG (GNUNET_ERROR_TYPE_DEBUG,
667 "Received CONNECTION_DESTROY for connection %s. Destroying route.\n",
668 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
669 route = get_route (&msg->cid);
673 destroy_route (route);
678 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX
680 * @param cls Closure (CadetPeer for neighbor that sent the message).
681 * @param msg Message itself.
684 handle_tunnel_kx (void *cls,
685 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
687 struct CadetPeer *peer = cls;
688 struct CadetConnection *cc;
690 /* First, check if message belongs to a connection that ends here. */
691 cc = GNUNET_CONTAINER_multishortmap_get (connections,
692 &msg->cid.connection_of_tunnel);
695 /* verify message came from the right direction */
696 struct CadetPeerPath *path = GCC_get_path (cc);
699 GCPP_get_peer_at_offset (path,
702 /* received message from unexpected direction, ignore! */
711 /* We're just an intermediary peer, route the message along its path */
719 * Check if the encrypted message has the appropriate size.
721 * @param cls Closure (unused).
722 * @param msg Message to check.
724 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
727 check_tunnel_encrypted (void *cls,
728 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
735 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED.
737 * @param cls Closure (CadetPeer for neighbor that sent the message).
738 * @param msg Message itself.
741 handle_tunnel_encrypted (void *cls,
742 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
744 struct CadetPeer *peer = cls;
745 struct CadetConnection *cc;
747 /* First, check if message belongs to a connection that ends here. */
748 cc = GNUNET_CONTAINER_multishortmap_get (connections,
749 &msg->cid.connection_of_tunnel);
752 /* verify message came from the right direction */
753 struct CadetPeerPath *path = GCC_get_path (cc);
756 GCPP_get_peer_at_offset (path,
759 /* received message from unexpected direction, ignore! */
763 GCC_handle_encrypted (cc,
767 /* We're just an intermediary peer, route the message along its path */
775 * Function called after #GNUNET_CORE_connect has succeeded (or failed
776 * for good). Note that the private key of the peer is intentionally
777 * not exposed here; if you need it, your process should try to read
778 * the private key file directly (which should work if you are
779 * authorized...). Implementations of this function must not call
780 * #GNUNET_CORE_disconnect (other than by scheduling a new task to
784 * @param my_identity ID of this peer, NULL if we failed
787 core_init_cb (void *cls,
788 const struct GNUNET_PeerIdentity *my_identity)
790 if (NULL == my_identity)
798 sizeof (struct GNUNET_PeerIdentity)));
803 * Method called whenever a given peer connects.
806 * @param peer peer identity this notification is about
809 core_connect_cb (void *cls,
810 const struct GNUNET_PeerIdentity *peer,
811 struct GNUNET_MQ_Handle *mq)
813 struct CadetPeer *cp;
815 LOG (GNUNET_ERROR_TYPE_DEBUG,
816 "CORE connection to peer %s was established.\n",
827 * Method called whenever a peer disconnects.
830 * @param peer peer identity this notification is about
833 core_disconnect_cb (void *cls,
834 const struct GNUNET_PeerIdentity *peer,
837 struct CadetPeer *cp = peer_cls;
839 LOG (GNUNET_ERROR_TYPE_DEBUG,
840 "CORE connection to peer %s went down.\n",
848 * Initialize the CORE subsystem.
850 * @param c Configuration.
853 GCO_init (const struct GNUNET_CONFIGURATION_Handle *c)
855 struct GNUNET_MQ_MessageHandler handlers[] = {
856 GNUNET_MQ_hd_var_size (connection_create,
857 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
858 struct GNUNET_CADET_ConnectionCreateMessage,
860 GNUNET_MQ_hd_fixed_size (connection_create_ack,
861 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK,
862 struct GNUNET_CADET_ConnectionCreateAckMessage,
864 GNUNET_MQ_hd_fixed_size (connection_broken,
865 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
866 struct GNUNET_CADET_ConnectionBrokenMessage,
868 GNUNET_MQ_hd_fixed_size (connection_destroy,
869 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
870 struct GNUNET_CADET_ConnectionDestroyMessage,
872 GNUNET_MQ_hd_fixed_size (tunnel_kx,
873 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX,
874 struct GNUNET_CADET_TunnelKeyExchangeMessage,
876 GNUNET_MQ_hd_var_size (tunnel_encrypted,
877 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED,
878 struct GNUNET_CADET_TunnelEncryptedMessage,
880 GNUNET_MQ_handler_end ()
883 routes = GNUNET_CONTAINER_multishortmap_create (1024,
885 core = GNUNET_CORE_connect (c,
895 * Shut down the CORE subsystem.
902 GNUNET_CORE_disconnect (core);
905 GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (routes));
906 GNUNET_CONTAINER_multishortmap_destroy (routes);
909 /* end of gnunet-cadet-service_core.c */