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 origin = GCP_get (&pids[0],
473 LOG (GNUNET_ERROR_TYPE_DEBUG,
474 "Received CREATE message from %s for connection %s, building inverse path\n",
476 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
477 path = GCPP_get_path_from_route (path_length - 1,
479 GCT_add_inbound_connection (GCT_create_tunnel (origin),
484 /* We are merely a hop on the way, check if we can support the route */
485 next = GCP_get (&pids[off + 1],
487 if ( (NULL == next) ||
488 (GNUNET_NO == GCP_has_core_connection (next)) )
490 /* unworkable, send back BROKEN notification */
491 struct GNUNET_MQ_Envelope *env;
492 struct GNUNET_CADET_ConnectionBrokenMessage *bm;
494 LOG (GNUNET_ERROR_TYPE_DEBUG,
495 "Received CONNECTION_CREATE from %s for %s. Next hop %s:%u is down. Sending BROKEN\n",
497 GNUNET_sh2s (&msg->cid.connection_of_tunnel),
498 GNUNET_i2s (&pids[off + 1]),
500 env = GNUNET_MQ_msg (bm,
501 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
503 bm->peer1 = pids[off + 1];
504 bm->peer2 = my_full_id;
505 GCP_send_ooo (sender,
510 /* Workable route, create routing entry */
511 LOG (GNUNET_ERROR_TYPE_DEBUG,
512 "Received CONNECTION_CREATE from %s for %s. Next hop %s:%u is up. Creating route\n",
514 GNUNET_sh2s (&msg->cid.connection_of_tunnel),
515 GNUNET_i2s (&pids[off + 1]),
517 route = GNUNET_new (struct CadetRoute);
518 route->cid = msg->cid;
519 dir_init (&route->prev,
522 dir_init (&route->next,
525 GNUNET_assert (GNUNET_OK ==
526 GNUNET_CONTAINER_multishortmap_put (routes,
527 &route->cid.connection_of_tunnel,
529 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
534 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK
536 * @param cls Closure (CadetPeer for neighbor that sent the message).
537 * @param msg Message itself.
540 handle_connection_create_ack (void *cls,
541 const struct GNUNET_CADET_ConnectionCreateAckMessage *msg)
543 struct CadetPeer *peer = cls;
544 struct CadetConnection *cc;
546 /* First, check if ACK belongs to a connection that ends here. */
547 cc = GNUNET_CONTAINER_multishortmap_get (connections,
548 &msg->cid.connection_of_tunnel);
551 /* verify ACK came from the right direction */
552 struct CadetPeerPath *path = GCC_get_path (cc);
555 GCPP_get_peer_at_offset (path,
558 /* received ACK from unexpected direction, ignore! */
562 LOG (GNUNET_ERROR_TYPE_DEBUG,
563 "Received CONNECTION_CREATE_ACK for connection %s.\n",
564 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
565 GCC_handle_connection_create_ack (cc);
569 /* We're just an intermediary peer, route the message along its path */
577 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
579 * @param cls Closure (CadetPeer for neighbor that sent the message).
580 * @param msg Message itself.
581 * @deprecated duplicate logic with #handle_destroy(); dedup!
584 handle_connection_broken (void *cls,
585 const struct GNUNET_CADET_ConnectionBrokenMessage *msg)
587 struct CadetPeer *peer = cls;
588 struct CadetConnection *cc;
589 struct CadetRoute *route;
591 /* First, check if message belongs to a connection that ends here. */
592 cc = GNUNET_CONTAINER_multishortmap_get (connections,
593 &msg->cid.connection_of_tunnel);
596 /* verify message came from the right direction */
597 struct CadetPeerPath *path = GCC_get_path (cc);
600 GCPP_get_peer_at_offset (path,
603 /* received message from unexpected direction, ignore! */
607 LOG (GNUNET_ERROR_TYPE_DEBUG,
608 "Received CONNECTION_BROKEN for connection %s. Destroying it.\n",
609 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
612 /* FIXME: also destroy the path up to the specified link! */
616 /* We're just an intermediary peer, route the message along its path */
617 route = get_route (&msg->cid);
621 destroy_route (route);
622 /* FIXME: also destroy paths we MAY have up to the specified link! */
627 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
629 * @param cls Closure (CadetPeer for neighbor that sent the message).
630 * @param msg Message itself.
633 handle_connection_destroy (void *cls,
634 const struct GNUNET_CADET_ConnectionDestroyMessage *msg)
636 struct CadetPeer *peer = cls;
637 struct CadetConnection *cc;
638 struct CadetRoute *route;
640 /* First, check if message belongs to a connection that ends here. */
641 cc = GNUNET_CONTAINER_multishortmap_get (connections,
642 &msg->cid.connection_of_tunnel);
645 /* verify message came from the right direction */
646 struct CadetPeerPath *path = GCC_get_path (cc);
649 GCPP_get_peer_at_offset (path,
652 /* received message from unexpected direction, ignore! */
656 LOG (GNUNET_ERROR_TYPE_DEBUG,
657 "Received CONNECTION_DESTROY for connection %s. Destroying connection.\n",
658 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
664 /* We're just an intermediary peer, route the message along its path */
665 LOG (GNUNET_ERROR_TYPE_DEBUG,
666 "Received CONNECTION_DESTROY for connection %s. Destroying route.\n",
667 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
668 route = get_route (&msg->cid);
672 destroy_route (route);
677 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX
679 * @param cls Closure (CadetPeer for neighbor that sent the message).
680 * @param msg Message itself.
683 handle_tunnel_kx (void *cls,
684 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
686 struct CadetPeer *peer = cls;
687 struct CadetConnection *cc;
689 /* First, check if message belongs to a connection that ends here. */
690 cc = GNUNET_CONTAINER_multishortmap_get (connections,
691 &msg->cid.connection_of_tunnel);
694 /* verify message came from the right direction */
695 struct CadetPeerPath *path = GCC_get_path (cc);
698 GCPP_get_peer_at_offset (path,
701 /* received message from unexpected direction, ignore! */
710 /* We're just an intermediary peer, route the message along its path */
718 * Check if the encrypted message has the appropriate size.
720 * @param cls Closure (unused).
721 * @param msg Message to check.
723 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
726 check_tunnel_encrypted (void *cls,
727 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
734 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED.
736 * @param cls Closure (CadetPeer for neighbor that sent the message).
737 * @param msg Message itself.
740 handle_tunnel_encrypted (void *cls,
741 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
743 struct CadetPeer *peer = cls;
744 struct CadetConnection *cc;
746 /* First, check if message belongs to a connection that ends here. */
747 cc = GNUNET_CONTAINER_multishortmap_get (connections,
748 &msg->cid.connection_of_tunnel);
751 /* verify message came from the right direction */
752 struct CadetPeerPath *path = GCC_get_path (cc);
755 GCPP_get_peer_at_offset (path,
758 /* received message from unexpected direction, ignore! */
762 GCC_handle_encrypted (cc,
766 /* We're just an intermediary peer, route the message along its path */
774 * Function called after #GNUNET_CORE_connect has succeeded (or failed
775 * for good). Note that the private key of the peer is intentionally
776 * not exposed here; if you need it, your process should try to read
777 * the private key file directly (which should work if you are
778 * authorized...). Implementations of this function must not call
779 * #GNUNET_CORE_disconnect (other than by scheduling a new task to
783 * @param my_identity ID of this peer, NULL if we failed
786 core_init_cb (void *cls,
787 const struct GNUNET_PeerIdentity *my_identity)
789 if (NULL == my_identity)
797 sizeof (struct GNUNET_PeerIdentity)));
802 * Method called whenever a given peer connects.
805 * @param peer peer identity this notification is about
808 core_connect_cb (void *cls,
809 const struct GNUNET_PeerIdentity *peer,
810 struct GNUNET_MQ_Handle *mq)
812 struct CadetPeer *cp;
814 LOG (GNUNET_ERROR_TYPE_DEBUG,
815 "CORE connection to peer %s was established.\n",
826 * Method called whenever a peer disconnects.
829 * @param peer peer identity this notification is about
832 core_disconnect_cb (void *cls,
833 const struct GNUNET_PeerIdentity *peer,
836 struct CadetPeer *cp = peer_cls;
838 LOG (GNUNET_ERROR_TYPE_DEBUG,
839 "CORE connection to peer %s went down.\n",
847 * Initialize the CORE subsystem.
849 * @param c Configuration.
852 GCO_init (const struct GNUNET_CONFIGURATION_Handle *c)
854 struct GNUNET_MQ_MessageHandler handlers[] = {
855 GNUNET_MQ_hd_var_size (connection_create,
856 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
857 struct GNUNET_CADET_ConnectionCreateMessage,
859 GNUNET_MQ_hd_fixed_size (connection_create_ack,
860 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK,
861 struct GNUNET_CADET_ConnectionCreateAckMessage,
863 GNUNET_MQ_hd_fixed_size (connection_broken,
864 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
865 struct GNUNET_CADET_ConnectionBrokenMessage,
867 GNUNET_MQ_hd_fixed_size (connection_destroy,
868 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
869 struct GNUNET_CADET_ConnectionDestroyMessage,
871 GNUNET_MQ_hd_fixed_size (tunnel_kx,
872 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX,
873 struct GNUNET_CADET_TunnelKeyExchangeMessage,
875 GNUNET_MQ_hd_var_size (tunnel_encrypted,
876 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED,
877 struct GNUNET_CADET_TunnelEncryptedMessage,
879 GNUNET_MQ_handler_end ()
882 routes = GNUNET_CONTAINER_multishortmap_create (1024,
884 core = GNUNET_CORE_connect (c,
894 * Shut down the CORE subsystem.
901 GNUNET_CORE_disconnect (core);
904 GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (routes));
905 GNUNET_CONTAINER_multishortmap_destroy (routes);
908 /* end of gnunet-cadet-service_core.c */