2 This file is part of GNUnet.
3 Copyright (C) 2013, 2015 Christian Grothoff (and other contributing authors)
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.
21 * @file cadet/gnunet-service-cadet_peer.c
22 * @brief GNUnet CADET service connection handling
23 * @author Bartlomiej Polot
26 #include "gnunet_util_lib.h"
27 #include "gnunet_signatures.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_core_service.h"
30 #include "gnunet_statistics_service.h"
31 #include "cadet_protocol.h"
32 #include "gnunet-service-cadet_peer.h"
33 #include "gnunet-service-cadet_dht.h"
34 #include "gnunet-service-cadet_connection.h"
35 #include "gnunet-service-cadet_tunnel.h"
36 #include "cadet_path.h"
38 #define LOG(level, ...) GNUNET_log_from (level,"cadet-p2p",__VA_ARGS__)
39 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-p2p",__VA_ARGS__)
42 /******************************************************************************/
43 /******************************** STRUCTS **********************************/
44 /******************************************************************************/
47 * Struct containing info about a queued transmission to this peer
54 struct CadetPeerQueue *next;
59 struct CadetPeerQueue *prev;
62 * Peer this transmission is directed to.
64 struct CadetPeer *peer;
67 * Connection this message belongs to.
69 struct CadetConnection *c;
77 * Pointer to info stucture used as cls.
89 uint16_t payload_type;
102 * Set when this message starts waiting for CORE.
104 struct GNUNET_TIME_Absolute start_waiting;
107 * Function to call on sending.
112 * Closure for callback.
119 * Struct containing all information regarding a given peer
129 * Last time we heard from this peer
131 struct GNUNET_TIME_Absolute last_contact;
134 * Paths to reach the peer, ordered by ascending hop count
136 struct CadetPeerPath *path_head;
139 * Paths to reach the peer, ordered by ascending hop count
141 struct CadetPeerPath *path_tail;
144 * Handle to stop the DHT search for paths to this peer
146 struct GCD_search_handle *search_h;
149 * Handle to stop the DHT search for paths to this peer
151 struct GNUNET_SCHEDULER_Task *search_delayed;
154 * Tunnel to this peer, if any.
156 struct CadetTunnel *tunnel;
159 * Connections that go through this peer; indexed by tid.
161 struct GNUNET_CONTAINER_MultiHashMap *connections;
164 * Handle for queued transmissions
166 struct GNUNET_CORE_TransmitHandle *core_transmit;
171 struct GNUNET_TIME_Absolute tmt_time;
174 * Transmission queue to core DLL head
176 struct CadetPeerQueue *queue_head;
179 * Transmission queue to core DLL tail
181 struct CadetPeerQueue *queue_tail;
184 * How many messages are in the queue to this peer.
186 unsigned int queue_n;
191 struct GNUNET_HELLO_Message* hello;
195 /******************************************************************************/
196 /******************************* GLOBALS ***********************************/
197 /******************************************************************************/
200 * Global handle to the statistics service.
202 extern struct GNUNET_STATISTICS_Handle *stats;
205 * Local peer own ID (full value).
207 extern struct GNUNET_PeerIdentity my_full_id;
210 * Local peer own ID (short)
212 extern GNUNET_PEER_Id myid;
215 * Peers known, indexed by PeerIdentity, values of type `struct CadetPeer`.
217 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
220 * How many peers do we want to remember?
222 static unsigned long long max_peers;
225 * Percentage of messages that will be dropped (for test purposes only).
227 static unsigned long long drop_percent;
230 * Handle to communicate with core.
232 static struct GNUNET_CORE_Handle *core_handle;
235 * Handle to try to start new connections.
237 static struct GNUNET_TRANSPORT_Handle *transport_handle;
242 static int in_shutdown;
245 /******************************************************************************/
246 /***************************** DEBUG *********************************/
247 /******************************************************************************/
250 * Log all kinds of info about the queueing status of a peer.
252 * @param p Peer whose queue to show.
253 * @param level Error level to use for logging.
256 queue_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
258 struct GNUNET_TIME_Relative core_wait_time;
259 struct CadetPeerQueue *q;
262 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
264 __FILE__, __FUNCTION__, __LINE__);
268 LOG2 (level, "QQQ Message queue towards %s\n", GCP_2s (p));
269 LOG2 (level, "QQQ queue length: %u\n", p->queue_n);
270 LOG2 (level, "QQQ core tmt rdy: %p\n", p->core_transmit);
271 if (NULL != p->core_transmit)
273 core_wait_time = GNUNET_TIME_absolute_get_duration (p->tmt_time);
274 LOG2 (level, "QQQ core called %s ago\n",
275 GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_NO));
277 for (q = p->queue_head; NULL != q; q = q->next)
279 LOG2 (level, "QQQ - %s %s on %s\n",
280 GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
281 LOG2 (level, "QQQ payload %s, %u\n",
282 GC_m2s (q->payload_type), q->payload_id);
283 LOG2 (level, "QQQ size: %u bytes\n", q->size);
286 LOG2 (level, "QQQ End queue towards %s\n", GCP_2s (p));
291 * Log all kinds of info about a peer.
296 GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
298 struct CadetPeerPath *path;
302 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
304 __FILE__, __FUNCTION__, __LINE__);
310 LOG2 (level, "PPP DEBUG PEER NULL\n");
314 LOG2 (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
315 LOG2 (level, "PPP last contact %s\n",
316 GNUNET_STRINGS_absolute_time_to_string (p->last_contact));
317 for (path = p->path_head; NULL != path; path = path->next)
322 LOG2 (level, "PPP path: %s\n", s);
326 LOG2 (level, "PPP core transmit handle %p\n", p->core_transmit);
327 LOG2 (level, "PPP DHT GET handle %p\n", p->search_h);
329 if (NULL != p->connections)
330 conns += GNUNET_CONTAINER_multihashmap_size (p->connections);
331 LOG2 (level, "PPP # connections over link to peer: %u\n", conns);
332 queue_debug (p, level);
333 LOG2 (level, "PPP DEBUG END\n");
337 /******************************************************************************/
338 /***************************** CORE HELPERS *********************************/
339 /******************************************************************************/
343 * Iterator to notify all connections of a broken link. Mark connections
344 * to destroy after all traffic has been sent.
346 * @param cls Closure (disconnected peer).
347 * @param key Current key code (peer id).
348 * @param value Value in the hash map (connection).
350 * @return #GNUNET_YES to continue to iterate.
353 notify_broken (void *cls,
354 const struct GNUNET_HashCode *key,
357 struct CadetPeer *peer = cls;
358 struct CadetConnection *c = value;
360 LOG (GNUNET_ERROR_TYPE_DEBUG,
361 "Notifying %s due to %s\n",
364 GCC_neighbor_disconnected (c, peer);
370 * Remove the direct path to the peer.
372 * @param peer Peer to remove the direct path from.
375 static struct CadetPeerPath *
376 pop_direct_path (struct CadetPeer *peer)
378 struct CadetPeerPath *iter;
380 for (iter = peer->path_head; NULL != iter; iter = iter->next)
382 if (2 >= iter->length)
384 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
392 /******************************************************************************/
393 /***************************** CORE CALLBACKS *********************************/
394 /******************************************************************************/
398 * Method called whenever a given peer connects.
401 * @param peer peer identity this notification is about
404 core_connect (void *cls,
405 const struct GNUNET_PeerIdentity *peer)
407 struct CadetPeer *mp;
408 struct CadetPeerPath *path;
411 GCC_check_connections ();
412 GNUNET_snprintf (own_id,
415 GNUNET_i2s (&my_full_id));
416 mp = GCP_get (peer, GNUNET_YES);
419 LOG (GNUNET_ERROR_TYPE_INFO,
420 "CONNECTED %s (self)\n",
426 LOG (GNUNET_ERROR_TYPE_INFO,
427 "CONNECTED %s <= %s\n",
431 path->peers[1] = mp->id;
432 GNUNET_PEER_change_rc (mp->id, 1);
434 path->peers[0] = myid;
435 GNUNET_PEER_change_rc (myid, 1);
436 GCP_add_path (mp, path, GNUNET_YES);
437 GNUNET_STATISTICS_update (stats,
441 GNUNET_assert (NULL == mp->connections);
442 mp->connections = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
444 if ( (NULL != GCP_get_tunnel (mp)) &&
445 (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer)) )
447 GCC_check_connections ();
452 * Method called whenever a peer disconnects.
455 * @param peer peer identity this notification is about
458 core_disconnect (void *cls,
459 const struct GNUNET_PeerIdentity *peer)
462 struct CadetPeerPath *direct_path;
465 GCC_check_connections ();
466 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
467 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
470 GNUNET_break (GNUNET_YES == in_shutdown);
474 LOG (GNUNET_ERROR_TYPE_INFO,
475 "DISCONNECTED %s (self)\n",
478 LOG (GNUNET_ERROR_TYPE_INFO,
479 "DISCONNECTED %s <= %s\n",
480 own_id, GNUNET_i2s (peer));
481 direct_path = pop_direct_path (p);
482 GNUNET_CONTAINER_multihashmap_iterate (p->connections,
485 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
486 p->connections = NULL;
487 if (NULL != p->core_transmit)
489 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
490 p->core_transmit = NULL;
491 p->tmt_time.abs_value_us = 0;
493 GNUNET_STATISTICS_update (stats,
497 path_destroy (direct_path);
498 GCC_check_connections ();
503 * Functions to handle messages from core
505 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
506 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
507 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
508 sizeof (struct GNUNET_CADET_ConnectionACK)},
509 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
510 sizeof (struct GNUNET_CADET_ConnectionBroken)},
511 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
512 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
513 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
514 sizeof (struct GNUNET_CADET_ACK)},
515 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
516 sizeof (struct GNUNET_CADET_Poll)},
517 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
518 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
519 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_AX, 0},
525 * To be called on core init/fail.
527 * @param cls Closure (config)
528 * @param identity the public identity of this peer
531 core_init (void *cls,
532 const struct GNUNET_PeerIdentity *identity)
534 const struct GNUNET_CONFIGURATION_Handle *c = cls;
537 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
538 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
540 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
541 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
542 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
543 GNUNET_CORE_disconnect (core_handle);
544 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
545 NULL, /* Closure passed to CADET functions */
546 &core_init, /* Call core_init once connected */
547 &core_connect, /* Handle connects */
548 &core_disconnect, /* remove peers on disconnects */
549 NULL, /* Don't notify about all incoming messages */
550 GNUNET_NO, /* For header only in notification */
551 NULL, /* Don't notify about all outbound messages */
552 GNUNET_NO, /* For header-only out notification */
553 core_handlers); /* Register these handlers */
562 * Core callback to write a pre-constructed data packet to core buffer
564 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
565 * @param size Number of bytes available in buf.
566 * @param buf Where the to write the message.
568 * @return number of bytes written to buf
571 send_core_data_raw (void *cls, size_t size, void *buf)
573 struct GNUNET_MessageHeader *msg = cls;
576 GNUNET_assert (NULL != msg);
577 total_size = ntohs (msg->size);
579 if (total_size > size)
584 memcpy (buf, msg, total_size);
591 * Function to send a create connection message to a peer.
593 * @param c Connection to create.
594 * @param size number of bytes available in buf
595 * @param buf where the callee should write the message
596 * @return number of bytes written to buf
599 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
601 struct GNUNET_CADET_ConnectionCreate *msg;
602 struct GNUNET_PeerIdentity *peer_ptr;
603 const struct CadetPeerPath *p = GCC_get_path (c);
610 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
612 sizeof (struct GNUNET_CADET_ConnectionCreate) +
613 p->length * sizeof (struct GNUNET_PeerIdentity);
615 if (size < size_needed || NULL == buf)
620 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
621 msg->header.size = htons (size_needed);
622 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
623 msg->cid = *GCC_get_id (c);
625 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
626 for (i = 0; i < p->length; i++)
628 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
631 LOG (GNUNET_ERROR_TYPE_DEBUG,
632 "CONNECTION CREATE (%u bytes long) sent!\n",
639 * Creates a path ack message in buf and frees all unused resources.
641 * @param c Connection to send an ACK on.
642 * @param size number of bytes available in buf
643 * @param buf where the callee should write the message
645 * @return number of bytes written to buf
648 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
650 struct GNUNET_CADET_ConnectionACK *msg = buf;
652 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
653 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
658 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
659 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
660 msg->cid = *GCC_get_id (c);
662 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
663 return sizeof (struct GNUNET_CADET_ConnectionACK);
667 /******************************************************************************/
668 /******************************** STATIC ***********************************/
669 /******************************************************************************/
673 * Get priority for a queued message.
675 * @param q Queued message
677 * @return CORE priority to use.
679 static enum GNUNET_CORE_Priority
680 get_priority (struct CadetPeerQueue *q)
682 enum GNUNET_CORE_Priority low;
683 enum GNUNET_CORE_Priority high;
688 return GNUNET_CORE_PRIO_BACKGROUND;
691 /* Relayed traffic has lower priority, our own traffic has higher */
692 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
694 low = GNUNET_CORE_PRIO_BEST_EFFORT;
695 high = GNUNET_CORE_PRIO_URGENT;
699 low = GNUNET_CORE_PRIO_URGENT;
700 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
703 /* Bulky payload has lower priority, control traffic has higher. */
704 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type
705 || GNUNET_MESSAGE_TYPE_CADET_AX == q->type)
713 * Destroy the peer_info and free any allocated resources linked to it
715 * @param peer The peer_info to destroy.
716 * @return #GNUNET_OK on success
719 peer_destroy (struct CadetPeer *peer)
721 struct GNUNET_PeerIdentity id;
722 struct CadetPeerPath *p;
723 struct CadetPeerPath *nextp;
725 GNUNET_PEER_resolve (peer->id, &id);
726 GNUNET_PEER_change_rc (peer->id, -1);
728 LOG (GNUNET_ERROR_TYPE_INFO,
729 "destroying peer %s\n",
732 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
735 LOG (GNUNET_ERROR_TYPE_WARNING, " peer not in peermap!!\n");
737 GCP_stop_search (peer);
742 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
746 if (NULL != peer->tunnel)
747 GCT_destroy_empty (peer->tunnel);
748 if (NULL != peer->connections)
750 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (peer->connections));
751 GNUNET_CONTAINER_multihashmap_destroy (peer->connections);
753 GNUNET_free_non_null (peer->hello);
760 * Iterator over peer hash map entries to destroy the peer during in_shutdown.
763 * @param key current key code
764 * @param value value in the hash map
765 * @return #GNUNET_YES if we should continue to iterate,
769 shutdown_peer (void *cls,
770 const struct GNUNET_PeerIdentity *key,
773 struct CadetPeer *p = value;
774 struct CadetTunnel *t = p->tunnel;
786 * Check if peer is searching for a path (either active or delayed search).
788 * @param peer Peer to check
789 * @return #GNUNET_YES if there is a search active.
790 * #GNUNET_NO otherwise.
793 is_searching (const struct CadetPeer *peer)
795 return ( (NULL == peer->search_h) &&
796 (NULL == peer->search_delayed) ) ?
797 GNUNET_NO : GNUNET_YES;
802 * @brief Start a search for a peer.
804 * @param cls Closure (Peer to search for).
805 * @param tc Task context.
808 delayed_search (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
810 struct CadetPeer *peer = cls;
812 peer->search_delayed = NULL;
813 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
815 GCC_check_connections ();
816 GCP_start_search (peer);
817 GCC_check_connections ();
822 * Returns if peer is used (has a tunnel or is neighbor).
824 * @param peer Peer to check.
825 * @return #GNUNET_YES if peer is in use.
828 peer_is_used (struct CadetPeer *peer)
830 struct CadetPeerPath *p;
832 if (NULL != peer->tunnel)
835 for (p = peer->path_head; NULL != p; p = p->next)
845 * Iterator over all the peers to get the oldest timestamp.
847 * @param cls Closure (unsued).
848 * @param key ID of the peer.
849 * @param value Peer_Info of the peer.
852 peer_get_oldest (void *cls,
853 const struct GNUNET_PeerIdentity *key,
856 struct CadetPeer *p = value;
857 struct GNUNET_TIME_Absolute *abs = cls;
859 /* Don't count active peers */
860 if (GNUNET_YES == peer_is_used (p))
863 if (abs->abs_value_us < p->last_contact.abs_value_us)
864 abs->abs_value_us = p->last_contact.abs_value_us;
871 * Iterator over all the peers to remove the oldest entry.
873 * @param cls Closure (unsued).
874 * @param key ID of the peer.
875 * @param value Peer_Info of the peer.
878 peer_timeout (void *cls,
879 const struct GNUNET_PeerIdentity *key,
882 struct CadetPeer *p = value;
883 struct GNUNET_TIME_Absolute *abs = cls;
885 LOG (GNUNET_ERROR_TYPE_WARNING,
886 "peer %s timeout\n", GNUNET_i2s (key));
888 if (p->last_contact.abs_value_us == abs->abs_value_us &&
889 GNUNET_NO == peer_is_used (p))
899 * Delete oldest unused peer.
902 peer_delete_oldest (void)
904 struct GNUNET_TIME_Absolute abs;
906 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
908 GNUNET_CONTAINER_multipeermap_iterate (peers,
911 GNUNET_CONTAINER_multipeermap_iterate (peers,
918 * Choose the best (yet unused) path towards a peer,
919 * considering the tunnel properties.
921 * @param peer The destination peer.
922 * @return Best current known path towards the peer, if any.
924 static struct CadetPeerPath *
925 peer_get_best_path (const struct CadetPeer *peer)
927 struct CadetPeerPath *best_p;
928 struct CadetPeerPath *p;
929 unsigned int best_cost;
932 best_cost = UINT_MAX;
934 for (p = peer->path_head; NULL != p; p = p->next)
936 if (GNUNET_NO == path_is_valid (p))
937 continue; /* Don't use invalid paths. */
938 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
939 continue; /* If path is already in use, skip it. */
941 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
952 * Is this queue element sendable?
954 * - All management traffic is always sendable.
955 * - For payload traffic, check the connection flow control.
957 * @param q Queue element to inspect.
958 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
961 queue_is_sendable (struct CadetPeerQueue *q)
963 /* Is PID-independent? */
966 case GNUNET_MESSAGE_TYPE_CADET_ACK:
967 case GNUNET_MESSAGE_TYPE_CADET_POLL:
968 case GNUNET_MESSAGE_TYPE_CADET_KX:
969 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
970 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
971 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
972 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
975 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
976 case GNUNET_MESSAGE_TYPE_CADET_AX:
983 return GCC_is_sendable (q->c, q->fwd);
988 * Get first sendable message.
990 * @param peer The destination peer.
992 * @return First transmittable message, if any. Otherwise, NULL.
994 static struct CadetPeerQueue *
995 peer_get_first_message (const struct CadetPeer *peer)
997 struct CadetPeerQueue *q;
999 for (q = peer->queue_head; NULL != q; q = q->next)
1001 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
1002 if (queue_is_sendable (q))
1011 * Function to process paths received for a new peer addition. The recorded
1012 * paths form the initial tunnel, which can be optimized later.
1013 * Called on each result obtained for the DHT search.
1015 * @param cls closure
1019 search_handler (void *cls, const struct CadetPeerPath *path)
1021 struct CadetPeer *peer = cls;
1022 unsigned int connection_count;
1024 GCC_check_connections ();
1025 GCP_add_path_to_all (path, GNUNET_NO);
1027 /* Count connections */
1028 connection_count = GCT_count_connections (peer->tunnel);
1030 /* If we already have our minimum (or more) connections, it's enough */
1031 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1033 GCC_check_connections ();
1037 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1039 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1042 GCC_check_connections ();
1047 * Adjust core requested size to accomodate an ACK.
1049 * @param message_size Requested size.
1051 * @return Size enough to fit @c message_size and an ACK.
1054 get_core_size (size_t message_size)
1056 return message_size + sizeof (struct GNUNET_CADET_ACK);
1061 * Fill a core buffer with the appropriate data for the queued message.
1063 * @param queue Queue element for the message.
1064 * @param buf Core buffer to fill.
1065 * @param size Size remaining in @c buf.
1066 * @param[out] pid In case its an encrypted payload, set payload.
1068 * @return Bytes written to @c buf.
1071 fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
1073 struct CadetConnection *c = queue->c;
1076 switch (queue->type)
1078 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1079 *pid = GCC_get_pid (queue->c, queue->fwd);
1080 LOG (GNUNET_ERROR_TYPE_DEBUG, " otr payload ID %u\n", *pid);
1081 msg_size = send_core_data_raw (queue->cls, size, buf);
1082 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
1084 case GNUNET_MESSAGE_TYPE_CADET_AX:
1085 *pid = GCC_get_pid (queue->c, queue->fwd);
1086 LOG (GNUNET_ERROR_TYPE_DEBUG, " ax payload ID %u\n", *pid);
1087 msg_size = send_core_data_raw (queue->cls, size, buf);
1088 ((struct GNUNET_CADET_AX *) buf)->pid = htonl (*pid);
1090 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1091 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1092 case GNUNET_MESSAGE_TYPE_CADET_KX:
1093 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1094 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1095 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1096 msg_size = send_core_data_raw (queue->cls, size, buf);
1098 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1099 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1100 if (GCC_is_origin (c, GNUNET_YES))
1101 msg_size = send_core_connection_create (c, size, buf);
1103 msg_size = send_core_data_raw (queue->cls, size, buf);
1105 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1106 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1107 if (GCC_is_origin (c, GNUNET_NO) ||
1108 GCC_is_origin (c, GNUNET_YES))
1110 msg_size = send_core_connection_ack (c, size, buf);
1114 msg_size = send_core_data_raw (queue->cls, size, buf);
1117 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1118 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1119 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1120 /* This should be encapsulted */
1126 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1130 GNUNET_assert (size >= msg_size);
1137 * Core callback to write a queued packet to core buffer
1139 * @param cls Closure (peer info).
1140 * @param size Number of bytes available in buf.
1141 * @param buf Where the to write the message.
1143 * @return number of bytes written to buf
1146 queue_send (void *cls, size_t size, void *buf)
1148 struct CadetPeer *peer = cls;
1149 struct CadetConnection *c;
1150 struct CadetPeerQueue *queue;
1151 struct GNUNET_TIME_Relative core_wait_time;
1152 const struct GNUNET_PeerIdentity *dst_id;
1159 GCC_check_connections ();
1160 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1161 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1162 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
1163 GCP_2s (peer), size);
1165 /* Sanity checking */
1166 if (NULL == buf || 0 == size)
1168 LOG (GNUNET_ERROR_TYPE_DEBUG, " not allowed/\n");
1169 if (GNUNET_NO == in_shutdown)
1171 queue = peer_get_first_message (peer);
1172 dst_id = GNUNET_PEER_resolve2 (peer->id);
1173 peer->core_transmit =
1174 GNUNET_CORE_notify_transmit_ready (core_handle,
1175 GNUNET_NO, get_priority (queue),
1176 GNUNET_TIME_UNIT_FOREVER_REL,
1178 get_core_size (queue->size),
1181 peer->tmt_time = GNUNET_TIME_absolute_get ();
1185 peer->core_transmit = NULL;
1186 peer->tmt_time.abs_value_us = 0;
1188 GCC_check_connections ();
1197 peer->core_transmit = NULL;
1198 queue = peer_get_first_message (peer);
1201 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
1202 peer->tmt_time.abs_value_us = 0;
1205 core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1206 if (core_wait_time.rel_value_us >= 1000000)
1208 LOG (GNUNET_ERROR_TYPE_ERROR,
1209 " %s: core wait time %s (> 1 second) for %u bytes\n",
1211 GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_YES),
1216 LOG (GNUNET_ERROR_TYPE_DEBUG, " core wait time %s\n",
1217 GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_YES));
1219 peer->tmt_time.abs_value_us = 0;
1221 /* Copy all possible messages to the core buffer */
1222 while (NULL != queue && rest >= queue->size)
1226 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s %s\n",
1227 GCC_2s (c), GC_f2s(queue->fwd));
1228 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok (%u/%u)\n",
1229 queue->size, total_size, size);
1231 msg_size = fill_buf (queue, (void *) dst, size, &pid);
1233 if (0 < drop_percent &&
1234 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1236 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on connection %s %s\n",
1237 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1238 queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
1243 LOG (GNUNET_ERROR_TYPE_INFO,
1244 "snd %s (%s %4u) on connection %s (%p) %s (size %u)\n",
1245 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1246 queue->payload_id, GCC_2s (c), c, GC_f2s (queue->fwd), msg_size);
1248 total_size += msg_size;
1250 dst = &dst[msg_size];
1253 /* Free queue, but cls was freed by send_core_* in fill_buf. */
1254 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1257 queue = peer_get_first_message (peer);
1260 /* If more data in queue, send next */
1263 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data! (%u)\n", queue->size);
1264 if (NULL == peer->core_transmit)
1266 dst_id = GNUNET_PEER_resolve2 (peer->id);
1267 peer->core_transmit =
1268 GNUNET_CORE_notify_transmit_ready (core_handle,
1269 GNUNET_NO, get_priority (queue),
1270 GNUNET_TIME_UNIT_FOREVER_REL,
1272 get_core_size (queue->size),
1275 peer->tmt_time = GNUNET_TIME_absolute_get ();
1276 queue->start_waiting = GNUNET_TIME_absolute_get ();
1280 LOG (GNUNET_ERROR_TYPE_DEBUG, "* tmt rdy called somewhere else\n");
1282 // GCC_start_poll (); FIXME needed?
1286 // GCC_stop_poll(); FIXME needed?
1289 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", total_size);
1290 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1291 GCC_check_connections ();
1296 /******************************************************************************/
1297 /******************************** API ***********************************/
1298 /******************************************************************************/
1302 * Free a transmission that was already queued with all resources
1303 * associated to the request.
1305 * If connection was marked to be destroyed, and this was the last queued
1306 * message on it, the connection will be free'd as a result.
1308 * @param queue Queue handler to cancel.
1309 * @param clear_cls Is it necessary to free associated cls?
1310 * @param sent Was it really sent? (Could have been canceled)
1311 * @param pid PID, if relevant (was sent and was a payload message).
1313 * @return #GNUNET_YES if connection was destroyed as a result,
1314 * #GNUNET_NO otherwise.
1317 GCP_queue_destroy (struct CadetPeerQueue *queue,
1322 struct CadetPeer *peer;
1323 int connection_destroyed;
1325 GCC_check_connections ();
1327 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1328 if (GNUNET_YES == clear_cls)
1330 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1331 switch (queue->type)
1333 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1334 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1336 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1337 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1338 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1339 case GNUNET_MESSAGE_TYPE_CADET_KX:
1340 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1341 case GNUNET_MESSAGE_TYPE_CADET_AX:
1342 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1343 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1344 GNUNET_free_non_null (queue->cls);
1349 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1350 GC_m2s (queue->type));
1353 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1355 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1356 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1361 if (NULL != queue->cont)
1363 struct GNUNET_TIME_Relative wait_time;
1365 wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1366 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1367 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1368 connection_destroyed = queue->cont (queue->cont_cls,
1369 queue->c, sent, queue->type, pid,
1370 queue->fwd, queue->size, wait_time);
1374 connection_destroyed = GNUNET_NO;
1377 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1379 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1380 peer->core_transmit = NULL;
1381 peer->tmt_time.abs_value_us = 0;
1384 GNUNET_free (queue);
1385 GCC_check_connections ();
1386 return connection_destroyed;
1391 * @brief Queue and pass message to core when possible.
1393 * @param peer Peer towards which to queue the message.
1394 * @param cls Closure (@c type dependant). It will be used by queue_send to
1395 * build the message to be sent if not already prebuilt.
1396 * @param type Type of the message, 0 for a raw message.
1397 * @param size Size of the message.
1398 * @param c Connection this message belongs to (can be NULL).
1399 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1400 * @param cont Continuation to be called once CORE has taken the message.
1401 * @param cont_cls Closure for @c cont.
1403 * @return Handle to cancel the message before it is sent. Once cont is called
1404 * message has been sent and therefore the handle is no longer valid.
1406 struct CadetPeerQueue *
1407 GCP_queue_add (struct CadetPeer *peer,
1410 uint16_t payload_type,
1411 uint32_t payload_id,
1413 struct CadetConnection *c,
1418 struct CadetPeerQueue *q;
1422 GCC_check_connections ();
1423 LOG (GNUNET_ERROR_TYPE_DEBUG,
1424 "que %s (%s %4u) on connection %s (%p) %s towards %s (size %u)\n",
1425 GC_m2s (type), GC_m2s (payload_type), payload_id,
1426 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1428 if (NULL == peer->connections)
1430 /* We are not connected to this peer, ignore request. */
1431 LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
1432 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1439 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1440 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1445 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1447 call_core = (NULL == c || type == GNUNET_MESSAGE_TYPE_CADET_KX) ?
1448 GNUNET_YES : GCC_is_sendable (c, fwd);
1449 q = GNUNET_new (struct CadetPeerQueue);
1452 q->payload_type = payload_type;
1453 q->payload_id = payload_id;
1459 q->cont_cls = cont_cls;
1462 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1467 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1468 call_core = GNUNET_YES;
1471 q->start_waiting = GNUNET_TIME_absolute_get ();
1472 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1474 LOG (GNUNET_ERROR_TYPE_DEBUG,
1475 "calling core tmt rdy towards %s for %u bytes\n",
1476 GCP_2s (peer), size);
1477 peer->core_transmit =
1478 GNUNET_CORE_notify_transmit_ready (core_handle,
1479 GNUNET_NO, get_priority (q),
1480 GNUNET_TIME_UNIT_FOREVER_REL,
1481 GNUNET_PEER_resolve2 (peer->id),
1482 get_core_size (size),
1484 peer->tmt_time = GNUNET_TIME_absolute_get ();
1486 else if (GNUNET_NO == call_core)
1488 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1494 struct GNUNET_TIME_Relative elapsed;
1495 elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1496 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
1498 GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
1501 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1502 GCC_check_connections ();
1508 * Cancel all queued messages to a peer that belong to a certain connection.
1510 * @param peer Peer towards whom to cancel.
1511 * @param c Connection whose queued messages to cancel. Might be destroyed by
1512 * the sent continuation call.
1515 GCP_queue_cancel (struct CadetPeer *peer,
1516 struct CadetConnection *c)
1518 struct CadetPeerQueue *q;
1519 struct CadetPeerQueue *next;
1520 struct CadetPeerQueue *prev;
1521 int connection_destroyed;
1523 GCC_check_connections ();
1524 connection_destroyed = GNUNET_NO;
1525 for (q = peer->queue_head; NULL != q; q = next)
1530 LOG (GNUNET_ERROR_TYPE_DEBUG,
1531 "GMP queue cancel %s\n",
1533 GNUNET_break (GNUNET_NO == connection_destroyed);
1534 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1540 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1543 /* Get next from prev, q->next might be already freed:
1544 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1547 next = peer->queue_head;
1557 if ( (NULL == peer->queue_head) &&
1558 (NULL != peer->core_transmit) )
1560 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1561 peer->core_transmit = NULL;
1562 peer->tmt_time.abs_value_us = 0;
1564 GCC_check_connections ();
1569 * Get the first transmittable message for a connection.
1571 * @param peer Neighboring peer.
1572 * @param c Connection.
1574 * @return First transmittable message.
1576 static struct CadetPeerQueue *
1577 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1579 struct CadetPeerQueue *q;
1581 for (q = peer->queue_head; NULL != q; q = q->next)
1585 if (queue_is_sendable (q))
1587 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1590 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1598 * Get the first message for a connection and unqueue it.
1600 * Only tunnel (or higher) level messages are unqueued. Connection specific
1601 * messages are silently destroyed upon encounter.
1603 * @param peer Neighboring peer.
1604 * @param c Connection.
1605 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1608 * @return First message for this connection.
1610 struct GNUNET_MessageHeader *
1611 GCP_connection_pop (struct CadetPeer *peer,
1612 struct CadetConnection *c,
1615 struct CadetPeerQueue *q;
1616 struct CadetPeerQueue *next;
1617 struct GNUNET_MessageHeader *msg;
1620 GCC_check_connections ();
1621 GNUNET_assert (NULL != destroyed);
1622 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on connection %p\n", c);
1623 for (q = peer->queue_head; NULL != q; q = next)
1628 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), cont: %p\n",
1629 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1633 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1634 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1635 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1636 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1637 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1638 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1639 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1640 if (GNUNET_YES == dest)
1642 GNUNET_break (GNUNET_NO == *destroyed);
1643 *destroyed = GNUNET_YES;
1647 case GNUNET_MESSAGE_TYPE_CADET_KX:
1648 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1649 case GNUNET_MESSAGE_TYPE_CADET_AX:
1650 case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
1651 msg = (struct GNUNET_MessageHeader *) q->cls;
1652 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1653 if (GNUNET_YES == dest)
1655 GNUNET_break (GNUNET_NO == *destroyed);
1656 *destroyed = GNUNET_YES;
1662 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1665 GCC_check_connections ();
1671 * Unlock a possibly locked queue for a connection.
1673 * If there is a message that can be sent on this connection, call core for it.
1674 * Otherwise (if core transmit is already called or there is no sendable
1675 * message) do nothing.
1677 * @param peer Peer who keeps the queue.
1678 * @param c Connection whose messages to unlock.
1681 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1683 struct CadetPeerQueue *q;
1686 GCC_check_connections ();
1687 if (NULL != peer->core_transmit)
1689 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1690 return; /* Already unlocked */
1693 q = connection_get_first_message (peer, c);
1696 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1697 return; /* Nothing to transmit */
1701 peer->core_transmit =
1702 GNUNET_CORE_notify_transmit_ready (core_handle,
1703 GNUNET_NO, get_priority (q),
1704 GNUNET_TIME_UNIT_FOREVER_REL,
1705 GNUNET_PEER_resolve2 (peer->id),
1706 get_core_size (size),
1709 peer->tmt_time = GNUNET_TIME_absolute_get ();
1710 GCC_check_connections ();
1715 * Initialize the peer subsystem.
1717 * @param c Configuration.
1720 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1722 LOG (GNUNET_ERROR_TYPE_DEBUG,
1724 in_shutdown = GNUNET_NO;
1725 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1727 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1730 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1731 "CADET", "MAX_PEERS", "USING DEFAULT");
1736 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1743 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1744 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1745 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1746 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1747 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1750 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1751 NULL, /* Closure passed to CADET functions */
1752 &core_init, /* Call core_init once connected */
1753 &core_connect, /* Handle connects */
1754 &core_disconnect, /* remove peers on disconnects */
1755 NULL, /* Don't notify about all incoming messages */
1756 GNUNET_NO, /* For header only in notification */
1757 NULL, /* Don't notify about all outbound messages */
1758 GNUNET_NO, /* For header-only out notification */
1759 core_handlers); /* Register these handlers */
1761 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1763 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1764 /* Notify callbacks */
1769 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1770 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1771 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1772 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1773 transport_handle = NULL;
1778 if (NULL == core_handle)
1781 GNUNET_SCHEDULER_shutdown ();
1789 * Shut down the peer subsystem.
1794 in_shutdown = GNUNET_YES;
1795 GNUNET_CONTAINER_multipeermap_iterate (peers,
1798 if (NULL != core_handle)
1800 GNUNET_CORE_disconnect (core_handle);
1803 if (NULL != transport_handle)
1805 GNUNET_TRANSPORT_disconnect (transport_handle);
1806 transport_handle = NULL;
1808 GNUNET_PEER_change_rc (myid, -1);
1809 GNUNET_CONTAINER_multipeermap_destroy (peers);
1815 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1816 * one and insert it in the appropriate structures if the peer is not known yet.
1818 * @param peer_id Full identity of the peer.
1819 * @param create #GNUNET_YES if a new peer should be created if unknown.
1820 * #GNUNET_NO otherwise.
1822 * @return Existing or newly created peer structure.
1823 * NULL if unknown and not requested @a create
1826 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
1828 struct CadetPeer *peer;
1830 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1833 peer = GNUNET_new (struct CadetPeer);
1834 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1836 peer_delete_oldest ();
1838 GNUNET_assert (GNUNET_OK ==
1839 GNUNET_CONTAINER_multipeermap_put (peers,
1842 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1843 peer->id = GNUNET_PEER_intern (peer_id);
1845 peer->last_contact = GNUNET_TIME_absolute_get ();
1852 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1853 * one and insert it in the appropriate structures if the peer is not known yet.
1855 * @param peer Short identity of the peer.
1856 * @param create #GNUNET_YES if a new peer should be created if unknown.
1857 * #GNUNET_NO otherwise.
1859 * @return Existing or newly created peer structure.
1860 * NULL if unknown and not requested @a create
1863 GCP_get_short (const GNUNET_PEER_Id peer, int create)
1865 return GCP_get (GNUNET_PEER_resolve2 (peer), create);
1870 * Try to connect to a peer on transport level.
1872 * @param cls Closure (peer).
1873 * @param tc TaskContext.
1876 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1878 struct CadetPeer *peer = cls;
1880 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1883 GNUNET_TRANSPORT_try_connect (transport_handle,
1884 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1889 * Try to establish a new connection to this peer (in its tunnel).
1890 * If the peer doesn't have any path to it yet, try to get one.
1891 * If the peer already has some path, send a CREATE CONNECTION towards it.
1893 * @param peer Peer to connect to.
1896 GCP_connect (struct CadetPeer *peer)
1898 struct CadetTunnel *t;
1899 struct CadetPeerPath *p;
1900 struct CadetConnection *c;
1903 GCC_check_connections ();
1904 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1906 /* If we have a current hello, try to connect using it. */
1907 GCP_try_connect (peer);
1911 rerun_search = GNUNET_NO;
1913 if (NULL != peer->path_head)
1915 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1916 p = peer_get_best_path (peer);
1922 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1925 c = GCT_use_path (t, p);
1928 /* This case can happen when the path includes a first hop that is
1929 * not yet known to be connected.
1931 * This happens quite often during testing when running cadet
1932 * under valgrind: core connect notifications come very late
1933 * and the DHT result has already come and created a valid
1934 * path. In this case, the peer->connections_{pred,succ}
1935 * hashmaps will be NULL and tunnel_use_path will not be able
1936 * to create a connection from that path.
1938 * Re-running the DHT GET should give core time to callback.
1940 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1941 * updating statistics about this issue.
1943 rerun_search = GNUNET_YES;
1947 GCC_send_create (c);
1953 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1957 if (GNUNET_YES == rerun_search)
1959 struct GNUNET_TIME_Relative delay;
1961 GCP_stop_search (peer);
1962 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1963 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
1966 GCC_check_connections ();
1970 if (GNUNET_NO == is_searching (peer))
1971 GCP_start_search (peer);
1972 GCC_check_connections ();
1977 * Chech whether there is a direct (core level) connection to peer.
1979 * @param peer Peer to check.
1981 * @return #GNUNET_YES if there is a direct connection.
1984 GCP_is_neighbor (const struct CadetPeer *peer)
1986 struct CadetPeerPath *path;
1988 if (NULL == peer->connections)
1991 for (path = peer->path_head; NULL != path; path = path->next)
1993 if (3 > path->length)
1997 /* Is not a neighbor but connections is not NULL, probably disconnecting */
2003 * Create and initialize a new tunnel towards a peer, in case it has none.
2004 * In case the peer already has a tunnel, nothing is done.
2006 * Does not generate any traffic, just creates the local data structures.
2008 * @param peer Peer towards which to create the tunnel.
2011 GCP_add_tunnel (struct CadetPeer *peer)
2013 GCC_check_connections ();
2014 if (NULL != peer->tunnel)
2016 peer->tunnel = GCT_new (peer);
2017 GCC_check_connections ();
2022 * Add a connection to a neighboring peer.
2024 * Store that the peer is the first hop of the connection in one
2025 * direction and that on peer disconnect the connection must be
2026 * notified and destroyed, for it will no longer be valid.
2028 * @param peer Peer to add connection to.
2029 * @param c Connection to add.
2030 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
2033 GCP_add_connection (struct CadetPeer *peer,
2034 struct CadetConnection *c,
2037 LOG (GNUNET_ERROR_TYPE_DEBUG,
2038 "adding connection %s\n",
2040 LOG (GNUNET_ERROR_TYPE_DEBUG,
2043 GNUNET_assert (NULL != peer->connections);
2044 GNUNET_assert (GNUNET_OK ==
2045 GNUNET_CONTAINER_multihashmap_put (peer->connections,
2048 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2049 LOG (GNUNET_ERROR_TYPE_DEBUG,
2050 "Peer %s has now %u connections.\n",
2052 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2057 * Add the path to the peer and update the path used to reach it in case this
2060 * @param peer Destination peer to add the path to.
2061 * @param path New path to add. Last peer must be the peer in arg 1.
2062 * Path will be either used of freed if already known.
2063 * @param trusted Do we trust that this path is real?
2065 * @return path if path was taken, pointer to existing duplicate if exists
2068 struct CadetPeerPath *
2069 GCP_add_path (struct CadetPeer *peer,
2070 struct CadetPeerPath *path,
2073 struct CadetPeerPath *aux;
2077 GCC_check_connections ();
2078 LOG (GNUNET_ERROR_TYPE_DEBUG,
2079 "adding path [%u] to peer %s\n",
2080 path->length, GCP_2s (peer));
2082 if (NULL == peer || NULL == path
2083 || path->peers[path->length - 1] != peer->id)
2086 path_destroy (path);
2090 for (l = 1; l < path->length; l++)
2092 if (path->peers[l] == myid)
2094 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
2095 for (l2 = 0; l2 < path->length - l; l2++)
2097 path->peers[l2] = path->peers[l + l2];
2101 path->peers = GNUNET_realloc (path->peers,
2102 path->length * sizeof (GNUNET_PEER_Id));
2106 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
2108 if (2 >= path->length && GNUNET_NO == trusted)
2110 /* Only allow CORE to tell us about direct paths */
2111 path_destroy (path);
2115 l = path_get_length (path);
2118 path_destroy (path);
2122 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2123 for (aux = peer->path_head; aux != NULL; aux = aux->next)
2125 l2 = path_get_length (aux);
2128 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
2129 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
2130 peer->path_tail, aux, path);
2135 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2137 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
2138 path_destroy (path);
2143 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
2145 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
2148 if (NULL != peer->tunnel
2149 && CONNECTIONS_PER_TUNNEL < GCT_count_connections (peer->tunnel))
2153 GCC_check_connections ();
2159 * Add the path to the origin peer and update the path used to reach it in case
2160 * this is the shortest.
2161 * The path is given in peer_info -> destination, therefore we turn the path
2162 * upside down first.
2164 * @param peer Peer to add the path to, being the origin of the path.
2165 * @param path New path to add after being inversed.
2166 * Path will be either used or freed.
2167 * @param trusted Do we trust that this path is real?
2169 * @return path if path was taken, pointer to existing duplicate if exists
2172 struct CadetPeerPath *
2173 GCP_add_path_to_origin (struct CadetPeer *peer,
2174 struct CadetPeerPath *path,
2180 return GCP_add_path (peer, path, trusted);
2185 * Adds a path to the info of all the peers in the path
2187 * @param p Path to process.
2188 * @param confirmed Whether we know if the path works or not.
2191 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
2195 /* TODO: invert and add */
2196 GCC_check_connections ();
2197 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2198 for (i++; i < p->length; i++)
2200 struct CadetPeer *aux;
2201 struct CadetPeerPath *copy;
2203 aux = GCP_get_short (p->peers[i], GNUNET_YES);
2204 copy = path_duplicate (p);
2205 copy->length = i + 1;
2206 GCP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2208 GCC_check_connections ();
2213 * Remove any path to the peer that has the extact same peers as the one given.
2215 * @param peer Peer to remove the path from.
2216 * @param path Path to remove. Is always destroyed .
2219 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
2221 struct CadetPeerPath *iter;
2222 struct CadetPeerPath *next;
2224 GCC_check_connections ();
2225 GNUNET_assert (myid == path->peers[0]);
2226 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2228 LOG (GNUNET_ERROR_TYPE_INFO,
2229 "Removing path %p (%u) from %s\n",
2234 for (iter = peer->path_head; NULL != iter; iter = next)
2237 if (0 == path_cmp (path, iter))
2239 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
2241 path_destroy (iter);
2244 path_destroy (path);
2245 GCC_check_connections ();
2250 * Check that we are aware of a connection from a neighboring peer.
2252 * @param peer Peer to the connection is with
2253 * @param c Connection that should be in the map with this peer.
2256 GCP_check_connection (const struct CadetPeer *peer,
2257 const struct CadetConnection *c)
2259 GNUNET_assert (NULL != peer);
2260 GNUNET_assert (NULL != peer->connections);
2262 GNUNET_assert (GNUNET_YES ==
2263 GNUNET_CONTAINER_multihashmap_contains_value (peer->connections,
2270 * Remove a connection from a neighboring peer.
2272 * @param peer Peer to remove connection from.
2273 * @param c Connection to remove.
2276 GCP_remove_connection (struct CadetPeer *peer,
2277 const struct CadetConnection *c)
2279 LOG (GNUNET_ERROR_TYPE_DEBUG,
2280 "Removing connection %s\n",
2282 LOG (GNUNET_ERROR_TYPE_DEBUG,
2285 if ( (NULL == peer) ||
2286 (NULL == peer->connections) )
2288 GNUNET_assert (GNUNET_YES ==
2289 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2292 LOG (GNUNET_ERROR_TYPE_DEBUG,
2293 "Peer %s remains with %u connections.\n",
2295 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2300 * Start the DHT search for new paths towards the peer: we don't have
2301 * enough good connections.
2303 * @param peer Destination peer.
2306 GCP_start_search (struct CadetPeer *peer)
2308 const struct GNUNET_PeerIdentity *id;
2309 struct CadetTunnel *t = peer->tunnel;
2311 GCC_check_connections ();
2312 if (NULL != peer->search_h)
2318 if (NULL != peer->search_delayed)
2319 GCP_stop_search (peer);
2321 id = GNUNET_PEER_resolve2 (peer->id);
2322 peer->search_h = GCD_search (id, &search_handler, peer);
2326 /* Why would we search for a peer with no tunnel towards it? */
2331 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
2332 || 0 == GCT_count_any_connections (t))
2334 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2336 GCC_check_connections ();
2341 * Stop the DHT search for new paths towards the peer: we already have
2342 * enough good connections.
2344 * @param peer Destination peer.
2347 GCP_stop_search (struct CadetPeer *peer)
2349 GCC_check_connections ();
2350 if (NULL != peer->search_h)
2352 GCD_search_stop (peer->search_h);
2353 peer->search_h = NULL;
2355 if (NULL != peer->search_delayed)
2357 GNUNET_SCHEDULER_cancel (peer->search_delayed);
2358 peer->search_delayed = NULL;
2360 GCC_check_connections ();
2365 * Get the Full ID of a peer.
2367 * @param peer Peer to get from.
2369 * @return Full ID of peer.
2371 const struct GNUNET_PeerIdentity *
2372 GCP_get_id (const struct CadetPeer *peer)
2374 return GNUNET_PEER_resolve2 (peer->id);
2379 * Get the Short ID of a peer.
2381 * @param peer Peer to get from.
2383 * @return Short ID of peer.
2386 GCP_get_short_id (const struct CadetPeer *peer)
2395 * If tunnel is NULL and there was a search active, stop it, as it's useless.
2401 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2404 if (NULL == t && GNUNET_YES == is_searching (peer))
2406 GCP_stop_search (peer);
2412 * Get the tunnel towards a peer.
2414 * @param peer Peer to get from.
2416 * @return Tunnel towards peer.
2418 struct CadetTunnel *
2419 GCP_get_tunnel (const struct CadetPeer *peer)
2423 return peer->tunnel;
2428 * Set the hello message.
2430 * @param peer Peer whose message to set.
2431 * @param hello Hello message.
2434 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2436 struct GNUNET_HELLO_Message *old;
2439 GCC_check_connections ();
2440 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2444 old = GCP_get_hello (peer);
2447 size = GNUNET_HELLO_size (hello);
2448 peer->hello = GNUNET_malloc (size);
2449 memcpy (peer->hello, hello, size);
2453 peer->hello = GNUNET_HELLO_merge (old, hello);
2456 GCC_check_connections ();
2461 * Get the hello message.
2463 * @param peer Peer whose message to get.
2465 * @return Hello message.
2467 struct GNUNET_HELLO_Message *
2468 GCP_get_hello (struct CadetPeer *peer)
2470 struct GNUNET_TIME_Absolute expiration;
2471 struct GNUNET_TIME_Relative remaining;
2473 if (NULL == peer->hello)
2476 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2477 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2478 if (0 == remaining.rel_value_us)
2480 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2481 GNUNET_STRINGS_absolute_time_to_string (expiration));
2482 GNUNET_free (peer->hello);
2490 * Try to connect to a peer on TRANSPORT level.
2492 * @param peer Peer to whom to connect.
2495 GCP_try_connect (struct CadetPeer *peer)
2497 struct GNUNET_HELLO_Message *hello;
2498 struct GNUNET_MessageHeader *mh;
2500 if (NULL == transport_handle)
2502 GCC_check_connections ();
2503 hello = GCP_get_hello (peer);
2507 mh = GNUNET_HELLO_get_header (hello);
2508 GNUNET_TRANSPORT_offer_hello (transport_handle,
2512 GCC_check_connections ();
2517 * Notify a peer that a link between two other peers is broken. If any path
2518 * used that link, eliminate it.
2520 * @param peer Peer affected by the change.
2521 * @param peer1 Peer whose link is broken.
2522 * @param peer2 Peer whose link is broken.
2525 GCP_notify_broken_link (struct CadetPeer *peer,
2526 struct GNUNET_PeerIdentity *peer1,
2527 struct GNUNET_PeerIdentity *peer2)
2529 struct CadetPeerPath *iter;
2530 struct CadetPeerPath *next;
2535 GCC_check_connections ();
2536 p1 = GNUNET_PEER_search (peer1);
2537 p2 = GNUNET_PEER_search (peer2);
2539 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2540 if (0 == p1 || 0 == p2)
2542 /* We don't even know them */
2546 for (iter = peer->path_head; NULL != iter; iter = next)
2549 for (i = 0; i < iter->length - 1; i++)
2551 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2552 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2557 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2560 path_invalidate (iter);
2564 GCC_check_connections ();
2569 * Count the number of known paths toward the peer.
2571 * @param peer Peer to get path info.
2573 * @return Number of known paths.
2576 GCP_count_paths (const struct CadetPeer *peer)
2578 struct CadetPeerPath *iter;
2581 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2589 * Iterate over the paths to a peer.
2591 * @param peer Peer to get path info.
2592 * @param callback Function to call for every path.
2593 * @param cls Closure for @a callback.
2595 * @return Number of iterated paths.
2598 GCP_iterate_paths (struct CadetPeer *peer,
2599 GCP_path_iterator callback,
2602 struct CadetPeerPath *iter;
2605 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2608 if (GNUNET_YES != callback (cls, peer, iter))
2617 * Iterate all known peers.
2619 * @param iter Iterator.
2620 * @param cls Closure for @c iter.
2623 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2626 GCC_check_connections ();
2627 GNUNET_CONTAINER_multipeermap_iterate (peers,
2630 GCC_check_connections ();
2635 * Get the static string for a peer ID.
2639 * @return Static string for it's ID.
2642 GCP_2s (const struct CadetPeer *peer)
2646 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2650 /* end of gnunet-service-cadet_peer.c */