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 disconnect\n",
362 GCC_2s (c), GCP_2s (peer));
363 GCC_neighbor_disconnected (c, peer);
369 * Remove the direct path to the peer.
371 * @param peer Peer to remove the direct path from.
374 static struct CadetPeerPath *
375 pop_direct_path (struct CadetPeer *peer)
377 struct CadetPeerPath *iter;
379 for (iter = peer->path_head; NULL != iter; iter = iter->next)
381 if (2 >= iter->length)
383 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
391 /******************************************************************************/
392 /***************************** CORE CALLBACKS *********************************/
393 /******************************************************************************/
397 * Method called whenever a given peer connects.
400 * @param peer peer identity this notification is about
403 core_connect (void *cls,
404 const struct GNUNET_PeerIdentity *peer)
406 struct CadetPeer *neighbor;
407 struct CadetPeerPath *path;
410 GCC_check_connections ();
411 GNUNET_snprintf (own_id,
414 GNUNET_i2s (&my_full_id));
415 neighbor = GCP_get (peer, GNUNET_YES);
416 if (myid == neighbor->id)
418 LOG (GNUNET_ERROR_TYPE_INFO,
419 "CONNECTED %s (self)\n",
425 LOG (GNUNET_ERROR_TYPE_INFO,
426 "CONNECTED %s <= %s\n",
430 path->peers[1] = neighbor->id;
431 GNUNET_PEER_change_rc (neighbor->id, 1);
433 path->peers[0] = myid;
434 GNUNET_PEER_change_rc (myid, 1);
435 GCP_add_path (neighbor, path, GNUNET_YES);
437 GNUNET_assert (NULL == neighbor->connections);
438 neighbor->connections = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
439 GNUNET_assert (NULL != neighbor->connections);
441 GNUNET_STATISTICS_update (stats,
446 if ( (NULL != GCP_get_tunnel (neighbor)) &&
447 (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer)) )
448 GCP_connect (neighbor);
449 GCC_check_connections ();
454 * Method called whenever a peer disconnects.
457 * @param peer peer identity this notification is about
460 core_disconnect (void *cls,
461 const struct GNUNET_PeerIdentity *peer)
464 struct CadetPeerPath *direct_path;
467 GCC_check_connections ();
468 strncpy (own_id, GNUNET_i2s (&my_full_id), 16);
470 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
473 GNUNET_break (GNUNET_YES == in_shutdown);
477 LOG (GNUNET_ERROR_TYPE_INFO,
478 "DISCONNECTED %s (self)\n",
481 LOG (GNUNET_ERROR_TYPE_INFO,
482 "DISCONNECTED %s <= %s\n",
483 own_id, GNUNET_i2s (peer));
484 direct_path = pop_direct_path (p);
485 GNUNET_CONTAINER_multihashmap_iterate (p->connections,
488 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
489 p->connections = NULL;
490 if (NULL != p->core_transmit)
492 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
493 p->core_transmit = NULL;
494 p->tmt_time.abs_value_us = 0;
496 GNUNET_STATISTICS_update (stats,
500 path_destroy (direct_path);
501 GCC_check_connections ();
506 * Functions to handle messages from core
508 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
509 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
510 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
511 sizeof (struct GNUNET_CADET_ConnectionACK)},
512 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
513 sizeof (struct GNUNET_CADET_ConnectionBroken)},
514 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
515 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
516 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
517 sizeof (struct GNUNET_CADET_ACK)},
518 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
519 sizeof (struct GNUNET_CADET_Poll)},
520 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
521 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
522 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_AX, 0},
528 * To be called on core init/fail.
530 * @param cls Closure (config)
531 * @param identity the public identity of this peer
534 core_init (void *cls,
535 const struct GNUNET_PeerIdentity *identity)
537 const struct GNUNET_CONFIGURATION_Handle *c = cls;
540 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
541 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
543 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
544 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
545 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
546 GNUNET_CORE_disconnect (core_handle);
547 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
548 NULL, /* Closure passed to CADET functions */
549 &core_init, /* Call core_init once connected */
550 &core_connect, /* Handle connects */
551 &core_disconnect, /* remove peers on disconnects */
552 NULL, /* Don't notify about all incoming messages */
553 GNUNET_NO, /* For header only in notification */
554 NULL, /* Don't notify about all outbound messages */
555 GNUNET_NO, /* For header-only out notification */
556 core_handlers); /* Register these handlers */
565 * Core callback to write a pre-constructed data packet to core buffer
567 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
568 * @param size Number of bytes available in buf.
569 * @param buf Where the to write the message.
571 * @return number of bytes written to buf
574 send_core_data_raw (void *cls, size_t size, void *buf)
576 struct GNUNET_MessageHeader *msg = cls;
579 GNUNET_assert (NULL != msg);
580 total_size = ntohs (msg->size);
582 if (total_size > size)
587 memcpy (buf, msg, total_size);
594 * Function to send a create connection message to a peer.
596 * @param c Connection to create.
597 * @param size number of bytes available in buf
598 * @param buf where the callee should write the message
599 * @return number of bytes written to buf
602 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
604 struct GNUNET_CADET_ConnectionCreate *msg;
605 struct GNUNET_PeerIdentity *peer_ptr;
606 const struct CadetPeerPath *p = GCC_get_path (c);
613 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
615 sizeof (struct GNUNET_CADET_ConnectionCreate) +
616 p->length * sizeof (struct GNUNET_PeerIdentity);
618 if (size < size_needed || NULL == buf)
623 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
624 msg->header.size = htons (size_needed);
625 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
626 msg->cid = *GCC_get_id (c);
628 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
629 for (i = 0; i < p->length; i++)
631 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
634 LOG (GNUNET_ERROR_TYPE_DEBUG,
635 "CONNECTION CREATE (%u bytes long) sent!\n",
642 * Creates a path ack message in buf and frees all unused resources.
644 * @param c Connection to send an ACK on.
645 * @param size number of bytes available in buf
646 * @param buf where the callee should write the message
648 * @return number of bytes written to buf
651 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
653 struct GNUNET_CADET_ConnectionACK *msg = buf;
655 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
656 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
661 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
662 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
663 msg->cid = *GCC_get_id (c);
665 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
666 return sizeof (struct GNUNET_CADET_ConnectionACK);
670 /******************************************************************************/
671 /******************************** STATIC ***********************************/
672 /******************************************************************************/
676 * Get priority for a queued message.
678 * @param q Queued message
680 * @return CORE priority to use.
682 static enum GNUNET_CORE_Priority
683 get_priority (struct CadetPeerQueue *q)
685 enum GNUNET_CORE_Priority low;
686 enum GNUNET_CORE_Priority high;
691 return GNUNET_CORE_PRIO_BACKGROUND;
694 /* Relayed traffic has lower priority, our own traffic has higher */
695 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
697 low = GNUNET_CORE_PRIO_BEST_EFFORT;
698 high = GNUNET_CORE_PRIO_URGENT;
702 low = GNUNET_CORE_PRIO_URGENT;
703 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
706 /* Bulky payload has lower priority, control traffic has higher. */
707 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type
708 || GNUNET_MESSAGE_TYPE_CADET_AX == q->type)
716 * Destroy the peer_info and free any allocated resources linked to it
718 * @param peer The peer_info to destroy.
719 * @return #GNUNET_OK on success
722 peer_destroy (struct CadetPeer *peer)
724 struct GNUNET_PeerIdentity id;
725 struct CadetPeerPath *p;
726 struct CadetPeerPath *nextp;
728 GNUNET_PEER_resolve (peer->id, &id);
729 GNUNET_PEER_change_rc (peer->id, -1);
731 LOG (GNUNET_ERROR_TYPE_INFO,
732 "destroying peer %s\n",
735 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
738 LOG (GNUNET_ERROR_TYPE_WARNING, " peer not in peermap!!\n");
740 GCP_stop_search (peer);
745 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
749 if (NULL != peer->tunnel)
750 GCT_destroy_empty (peer->tunnel);
751 if (NULL != peer->connections)
753 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (peer->connections));
754 GNUNET_CONTAINER_multihashmap_destroy (peer->connections);
756 GNUNET_free_non_null (peer->hello);
763 * Iterator over peer hash map entries to destroy the peer during in_shutdown.
766 * @param key current key code
767 * @param value value in the hash map
768 * @return #GNUNET_YES if we should continue to iterate,
772 shutdown_peer (void *cls,
773 const struct GNUNET_PeerIdentity *key,
776 struct CadetPeer *p = value;
777 struct CadetTunnel *t = p->tunnel;
778 LOG (GNUNET_ERROR_TYPE_DEBUG, " shutting down %s\n", GCP_2s (p));
789 * Check if peer is searching for a path (either active or delayed search).
791 * @param peer Peer to check
792 * @return #GNUNET_YES if there is a search active.
793 * #GNUNET_NO otherwise.
796 is_searching (const struct CadetPeer *peer)
798 return ( (NULL == peer->search_h) &&
799 (NULL == peer->search_delayed) ) ?
800 GNUNET_NO : GNUNET_YES;
805 * @brief Start a search for a peer.
807 * @param cls Closure (Peer to search for).
808 * @param tc Task context.
811 delayed_search (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
813 struct CadetPeer *peer = cls;
815 peer->search_delayed = NULL;
816 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
818 GCC_check_connections ();
819 GCP_start_search (peer);
820 GCC_check_connections ();
825 * Returns if peer is used (has a tunnel or is neighbor).
827 * @param peer Peer to check.
828 * @return #GNUNET_YES if peer is in use.
831 peer_is_used (struct CadetPeer *peer)
833 struct CadetPeerPath *p;
835 if (NULL != peer->tunnel)
838 for (p = peer->path_head; NULL != p; p = p->next)
848 * Iterator over all the peers to get the oldest timestamp.
850 * @param cls Closure (unsued).
851 * @param key ID of the peer.
852 * @param value Peer_Info of the peer.
855 peer_get_oldest (void *cls,
856 const struct GNUNET_PeerIdentity *key,
859 struct CadetPeer *p = value;
860 struct GNUNET_TIME_Absolute *abs = cls;
862 /* Don't count active peers */
863 if (GNUNET_YES == peer_is_used (p))
866 if (abs->abs_value_us < p->last_contact.abs_value_us)
867 abs->abs_value_us = p->last_contact.abs_value_us;
874 * Iterator over all the peers to remove the oldest entry.
876 * @param cls Closure (unsued).
877 * @param key ID of the peer.
878 * @param value Peer_Info of the peer.
881 peer_timeout (void *cls,
882 const struct GNUNET_PeerIdentity *key,
885 struct CadetPeer *p = value;
886 struct GNUNET_TIME_Absolute *abs = cls;
888 LOG (GNUNET_ERROR_TYPE_WARNING,
889 "peer %s timeout\n", GNUNET_i2s (key));
891 if (p->last_contact.abs_value_us == abs->abs_value_us &&
892 GNUNET_NO == peer_is_used (p))
902 * Delete oldest unused peer.
905 peer_delete_oldest (void)
907 struct GNUNET_TIME_Absolute abs;
909 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
911 GNUNET_CONTAINER_multipeermap_iterate (peers,
914 GNUNET_CONTAINER_multipeermap_iterate (peers,
921 * Choose the best (yet unused) path towards a peer,
922 * considering the tunnel properties.
924 * @param peer The destination peer.
925 * @return Best current known path towards the peer, if any.
927 static struct CadetPeerPath *
928 peer_get_best_path (const struct CadetPeer *peer)
930 struct CadetPeerPath *best_p;
931 struct CadetPeerPath *p;
932 unsigned int best_cost;
935 best_cost = UINT_MAX;
937 for (p = peer->path_head; NULL != p; p = p->next)
939 if (GNUNET_NO == path_is_valid (p))
940 continue; /* Don't use invalid paths. */
941 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
942 continue; /* If path is already in use, skip it. */
944 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
955 * Is this queue element sendable?
957 * - All management traffic is always sendable.
958 * - For payload traffic, check the connection flow control.
960 * @param q Queue element to inspect.
961 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
964 queue_is_sendable (struct CadetPeerQueue *q)
966 /* Is PID-independent? */
969 case GNUNET_MESSAGE_TYPE_CADET_ACK:
970 case GNUNET_MESSAGE_TYPE_CADET_POLL:
971 case GNUNET_MESSAGE_TYPE_CADET_KX:
972 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
973 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
974 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
975 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
978 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
979 case GNUNET_MESSAGE_TYPE_CADET_AX:
986 return GCC_is_sendable (q->c, q->fwd);
991 * Get first sendable message.
993 * @param peer The destination peer.
995 * @return First transmittable message, if any. Otherwise, NULL.
997 static struct CadetPeerQueue *
998 peer_get_first_message (const struct CadetPeer *peer)
1000 struct CadetPeerQueue *q;
1002 for (q = peer->queue_head; NULL != q; q = q->next)
1004 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
1005 if (queue_is_sendable (q))
1014 * Function to process paths received for a new peer addition. The recorded
1015 * paths form the initial tunnel, which can be optimized later.
1016 * Called on each result obtained for the DHT search.
1018 * @param cls Closure (peer towards a path has been found).
1019 * @param path Path created from the DHT query. Will be freed afterwards.
1022 search_handler (void *cls, const struct CadetPeerPath *path)
1024 struct CadetPeer *peer = cls;
1025 unsigned int connection_count;
1027 GCC_check_connections ();
1028 GCP_add_path_to_all (path, GNUNET_NO);
1030 /* Count connections */
1031 connection_count = GCT_count_connections (peer->tunnel);
1033 /* If we already have our minimum (or more) connections, it's enough */
1034 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1036 GCC_check_connections ();
1040 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1042 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1045 GCC_check_connections ();
1050 * Adjust core requested size to accomodate an ACK.
1052 * @param message_size Requested size.
1054 * @return Size enough to fit @c message_size and an ACK.
1057 get_core_size (size_t message_size)
1059 return message_size + sizeof (struct GNUNET_CADET_ACK);
1064 * Fill a core buffer with the appropriate data for the queued message.
1066 * @param queue Queue element for the message.
1067 * @param buf Core buffer to fill.
1068 * @param size Size remaining in @c buf.
1069 * @param[out] pid In case its an encrypted payload, set payload.
1071 * @return Bytes written to @c buf.
1074 fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
1076 struct CadetConnection *c = queue->c;
1079 switch (queue->type)
1081 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1082 *pid = GCC_get_pid (queue->c, queue->fwd);
1083 LOG (GNUNET_ERROR_TYPE_DEBUG, " otr payload ID %u\n", *pid);
1084 msg_size = send_core_data_raw (queue->cls, size, buf);
1085 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
1087 case GNUNET_MESSAGE_TYPE_CADET_AX:
1088 *pid = GCC_get_pid (queue->c, queue->fwd);
1089 LOG (GNUNET_ERROR_TYPE_DEBUG, " ax payload ID %u\n", *pid);
1090 msg_size = send_core_data_raw (queue->cls, size, buf);
1091 ((struct GNUNET_CADET_AX *) buf)->pid = htonl (*pid);
1093 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1094 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1095 case GNUNET_MESSAGE_TYPE_CADET_KX:
1096 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1097 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1098 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1099 msg_size = send_core_data_raw (queue->cls, size, buf);
1101 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1102 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1103 if (GCC_is_origin (c, GNUNET_YES))
1104 msg_size = send_core_connection_create (c, size, buf);
1106 msg_size = send_core_data_raw (queue->cls, size, buf);
1108 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1109 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1110 if (GCC_is_origin (c, GNUNET_NO) ||
1111 GCC_is_origin (c, GNUNET_YES))
1113 msg_size = send_core_connection_ack (c, size, buf);
1117 msg_size = send_core_data_raw (queue->cls, size, buf);
1120 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1121 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1122 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1123 /* This should be encapsulted */
1129 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1133 GNUNET_assert (size >= msg_size);
1140 * Core callback to write a queued packet to core buffer
1142 * @param cls Closure (peer info).
1143 * @param size Number of bytes available in buf.
1144 * @param buf Where the to write the message.
1146 * @return number of bytes written to buf
1149 queue_send (void *cls, size_t size, void *buf)
1151 struct CadetPeer *peer = cls;
1152 struct CadetConnection *c;
1153 struct CadetPeerQueue *queue;
1154 struct GNUNET_TIME_Relative core_wait_time;
1156 const struct GNUNET_PeerIdentity *dst_id;
1163 GCC_check_connections ();
1164 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1165 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1166 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
1167 GCP_2s (peer), size);
1169 /* Sanity checking */
1170 if (NULL == buf || 0 == size)
1172 LOG (GNUNET_ERROR_TYPE_DEBUG, " not allowed/\n");
1173 if (GNUNET_NO == in_shutdown)
1175 queue = peer_get_first_message (peer);
1178 peer->core_transmit = NULL;
1179 peer->tmt_time.abs_value_us = 0;
1180 GCC_check_connections ();
1183 dst_id = GNUNET_PEER_resolve2 (peer->id);
1184 peer->core_transmit =
1185 GNUNET_CORE_notify_transmit_ready (core_handle,
1186 GNUNET_NO, get_priority (queue),
1187 GNUNET_TIME_UNIT_FOREVER_REL,
1189 get_core_size (queue->size),
1192 peer->tmt_time = GNUNET_TIME_absolute_get ();
1196 peer->core_transmit = NULL;
1197 peer->tmt_time.abs_value_us = 0;
1199 GCC_check_connections ();
1208 peer->core_transmit = NULL;
1209 queue = peer_get_first_message (peer);
1212 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
1213 peer->tmt_time.abs_value_us = 0;
1216 core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1217 wait_s = GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_YES);
1218 if (core_wait_time.rel_value_us >= 1000000)
1220 LOG (GNUNET_ERROR_TYPE_ERROR,
1221 " %s: core wait time %s (> 1 second) for %u bytes\n",
1222 GCP_2s (peer), wait_s, queue->size);
1224 peer->tmt_time.abs_value_us = 0;
1226 /* Copy all possible messages to the core buffer */
1227 while (NULL != queue && rest >= queue->size)
1231 LOG (GNUNET_ERROR_TYPE_DEBUG, " on conn %s %s\n",
1232 GCC_2s (c), GC_f2s(queue->fwd));
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok (%u/%u)\n",
1234 queue->size, total_size, size);
1236 msg_size = fill_buf (queue, (void *) dst, size, &pid);
1238 if (0 < drop_percent &&
1239 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1241 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on conn %s %s\n",
1242 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1243 queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
1248 LOG (GNUNET_ERROR_TYPE_INFO,
1249 ">>> %s (%s %4u) on conn %s (%p) %s (%u bytes), after %s\n",
1250 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1251 queue->payload_id, GCC_2s (c), c,
1252 GC_f2s (queue->fwd), msg_size, wait_s);
1254 total_size += msg_size;
1256 dst = &dst[msg_size];
1259 /* Free queue, but cls was freed by send_core_* in fill_buf. */
1260 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1263 queue = peer_get_first_message (peer);
1266 /* If more data in queue, send next */
1269 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data! (%u)\n", queue->size);
1270 if (NULL == peer->core_transmit)
1272 dst_id = GNUNET_PEER_resolve2 (peer->id);
1273 peer->core_transmit =
1274 GNUNET_CORE_notify_transmit_ready (core_handle,
1275 GNUNET_NO, get_priority (queue),
1276 GNUNET_TIME_UNIT_FOREVER_REL,
1278 get_core_size (queue->size),
1281 peer->tmt_time = GNUNET_TIME_absolute_get ();
1282 queue->start_waiting = GNUNET_TIME_absolute_get ();
1286 LOG (GNUNET_ERROR_TYPE_DEBUG, "* tmt rdy called somewhere else\n");
1288 // GCC_start_poll (); FIXME needed?
1292 // GCC_stop_poll(); FIXME needed?
1295 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", total_size);
1296 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1297 GCC_check_connections ();
1302 /******************************************************************************/
1303 /******************************** API ***********************************/
1304 /******************************************************************************/
1308 * Free a transmission that was already queued with all resources
1309 * associated to the request.
1311 * If connection was marked to be destroyed, and this was the last queued
1312 * message on it, the connection will be free'd as a result.
1314 * @param queue Queue handler to cancel.
1315 * @param clear_cls Is it necessary to free associated cls?
1316 * @param sent Was it really sent? (Could have been canceled)
1317 * @param pid PID, if relevant (was sent and was a payload message).
1319 * @return #GNUNET_YES if connection was destroyed as a result,
1320 * #GNUNET_NO otherwise.
1323 GCP_queue_destroy (struct CadetPeerQueue *queue,
1328 struct CadetPeer *peer;
1329 int connection_destroyed;
1331 GCC_check_connections ();
1333 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1334 if (GNUNET_YES == clear_cls)
1336 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1337 switch (queue->type)
1339 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1340 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1342 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1343 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1344 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1345 case GNUNET_MESSAGE_TYPE_CADET_KX:
1346 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1347 case GNUNET_MESSAGE_TYPE_CADET_AX:
1348 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1349 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1350 GNUNET_free_non_null (queue->cls);
1355 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1356 GC_m2s (queue->type));
1359 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1361 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1362 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1367 if (NULL != queue->cont)
1369 struct GNUNET_TIME_Relative wait_time;
1371 wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1372 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1373 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1374 connection_destroyed = queue->cont (queue->cont_cls,
1375 queue->c, sent, queue->type, pid,
1376 queue->fwd, queue->size, wait_time);
1380 connection_destroyed = GNUNET_NO;
1383 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1385 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1386 peer->core_transmit = NULL;
1387 peer->tmt_time.abs_value_us = 0;
1390 GNUNET_free (queue);
1391 GCC_check_connections ();
1392 return connection_destroyed;
1397 * @brief Queue and pass message to core when possible.
1399 * @param peer Peer towards which to queue the message.
1400 * @param cls Closure (@c type dependant). It will be used by queue_send to
1401 * build the message to be sent if not already prebuilt.
1402 * @param type Type of the message, 0 for a raw message.
1403 * @param size Size of the message.
1404 * @param c Connection this message belongs to (can be NULL).
1405 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1406 * @param cont Continuation to be called once CORE has taken the message.
1407 * @param cont_cls Closure for @c cont.
1409 * @return Handle to cancel the message before it is sent. Once cont is called
1410 * message has been sent and therefore the handle is no longer valid.
1412 struct CadetPeerQueue *
1413 GCP_queue_add (struct CadetPeer *peer,
1416 uint16_t payload_type,
1417 uint32_t payload_id,
1419 struct CadetConnection *c,
1424 struct CadetPeerQueue *q;
1428 GCC_check_connections ();
1429 LOG (GNUNET_ERROR_TYPE_DEBUG,
1430 "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n",
1431 GC_m2s (type), GC_m2s (payload_type), payload_id,
1432 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1434 if (NULL == peer->connections)
1436 /* We are not connected to this peer, ignore request. */
1437 LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
1438 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1445 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1446 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1451 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1453 call_core = (NULL == c || type == GNUNET_MESSAGE_TYPE_CADET_KX) ?
1454 GNUNET_YES : GCC_is_sendable (c, fwd);
1455 q = GNUNET_new (struct CadetPeerQueue);
1458 q->payload_type = payload_type;
1459 q->payload_id = payload_id;
1465 q->cont_cls = cont_cls;
1468 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1473 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1474 call_core = GNUNET_YES;
1477 q->start_waiting = GNUNET_TIME_absolute_get ();
1478 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1480 LOG (GNUNET_ERROR_TYPE_DEBUG,
1481 "calling core tmt rdy towards %s for %u bytes\n",
1482 GCP_2s (peer), size);
1483 peer->core_transmit =
1484 GNUNET_CORE_notify_transmit_ready (core_handle,
1485 GNUNET_NO, get_priority (q),
1486 GNUNET_TIME_UNIT_FOREVER_REL,
1487 GNUNET_PEER_resolve2 (peer->id),
1488 get_core_size (size),
1490 peer->tmt_time = GNUNET_TIME_absolute_get ();
1492 else if (GNUNET_NO == call_core)
1494 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1500 struct GNUNET_TIME_Relative elapsed;
1501 elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1502 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
1504 GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
1507 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1508 GCC_check_connections ();
1514 * Cancel all queued messages to a peer that belong to a certain connection.
1516 * @param peer Peer towards whom to cancel.
1517 * @param c Connection whose queued messages to cancel. Might be destroyed by
1518 * the sent continuation call.
1521 GCP_queue_cancel (struct CadetPeer *peer,
1522 struct CadetConnection *c)
1524 struct CadetPeerQueue *q;
1525 struct CadetPeerQueue *next;
1526 struct CadetPeerQueue *prev;
1527 int connection_destroyed;
1529 GCC_check_connections ();
1530 connection_destroyed = GNUNET_NO;
1531 for (q = peer->queue_head; NULL != q; q = next)
1536 LOG (GNUNET_ERROR_TYPE_DEBUG,
1537 "GMP queue cancel %s\n",
1539 GNUNET_assert (GNUNET_NO == connection_destroyed);
1540 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1546 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1549 /* Get next from prev, q->next might be already freed:
1550 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1553 next = peer->queue_head;
1563 if ( (NULL == peer->queue_head) &&
1564 (NULL != peer->core_transmit) )
1566 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1567 peer->core_transmit = NULL;
1568 peer->tmt_time.abs_value_us = 0;
1570 GCC_check_connections ();
1575 * Get the first transmittable message for a connection.
1577 * @param peer Neighboring peer.
1578 * @param c Connection.
1580 * @return First transmittable message.
1582 static struct CadetPeerQueue *
1583 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1585 struct CadetPeerQueue *q;
1587 for (q = peer->queue_head; NULL != q; q = q->next)
1591 if (queue_is_sendable (q))
1593 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1596 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1604 * Get the first message for a connection and unqueue it.
1606 * Only tunnel (or higher) level messages are unqueued. Connection specific
1607 * messages are silently destroyed upon encounter.
1609 * @param peer Neighboring peer.
1610 * @param c Connection.
1611 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1614 * @return First message for this connection.
1616 struct GNUNET_MessageHeader *
1617 GCP_connection_pop (struct CadetPeer *peer,
1618 struct CadetConnection *c,
1621 struct CadetPeerQueue *q;
1622 struct CadetPeerQueue *next;
1623 struct GNUNET_MessageHeader *msg;
1626 GCC_check_connections ();
1627 GNUNET_assert (NULL != destroyed);
1628 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on conn %p\n", c);
1629 for (q = peer->queue_head; NULL != q; q = next)
1634 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), cont: %p\n",
1635 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1639 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1640 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1641 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1642 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1643 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1644 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1645 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1646 if (GNUNET_YES == dest)
1648 GNUNET_break (GNUNET_NO == *destroyed);
1649 *destroyed = GNUNET_YES;
1653 case GNUNET_MESSAGE_TYPE_CADET_KX:
1654 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1655 case GNUNET_MESSAGE_TYPE_CADET_AX:
1656 case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
1657 msg = (struct GNUNET_MessageHeader *) q->cls;
1658 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1659 if (GNUNET_YES == dest)
1661 GNUNET_break (GNUNET_NO == *destroyed);
1662 *destroyed = GNUNET_YES;
1668 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1671 GCC_check_connections ();
1677 * Unlock a possibly locked queue for a connection.
1679 * If there is a message that can be sent on this connection, call core for it.
1680 * Otherwise (if core transmit is already called or there is no sendable
1681 * message) do nothing.
1683 * @param peer Peer who keeps the queue.
1684 * @param c Connection whose messages to unlock.
1687 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1689 struct CadetPeerQueue *q;
1692 GCC_check_connections ();
1693 if (NULL != peer->core_transmit)
1695 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1696 return; /* Already unlocked */
1699 q = connection_get_first_message (peer, c);
1702 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1703 return; /* Nothing to transmit */
1707 peer->core_transmit =
1708 GNUNET_CORE_notify_transmit_ready (core_handle,
1709 GNUNET_NO, get_priority (q),
1710 GNUNET_TIME_UNIT_FOREVER_REL,
1711 GNUNET_PEER_resolve2 (peer->id),
1712 get_core_size (size),
1715 peer->tmt_time = GNUNET_TIME_absolute_get ();
1716 GCC_check_connections ();
1721 * Initialize the peer subsystem.
1723 * @param c Configuration.
1726 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1728 LOG (GNUNET_ERROR_TYPE_DEBUG,
1730 in_shutdown = GNUNET_NO;
1731 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1733 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1736 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1737 "CADET", "MAX_PEERS", "USING DEFAULT");
1742 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1749 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1750 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1751 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1752 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1753 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1756 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1757 NULL, /* Closure passed to CADET functions */
1758 &core_init, /* Call core_init once connected */
1759 &core_connect, /* Handle connects */
1760 &core_disconnect, /* remove peers on disconnects */
1761 NULL, /* Don't notify about all incoming messages */
1762 GNUNET_NO, /* For header only in notification */
1763 NULL, /* Don't notify about all outbound messages */
1764 GNUNET_NO, /* For header-only out notification */
1765 core_handlers); /* Register these handlers */
1767 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1769 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1770 /* Notify callbacks */
1775 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1776 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1777 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1778 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1779 transport_handle = NULL;
1784 if (NULL == core_handle)
1787 GNUNET_SCHEDULER_shutdown ();
1795 * Shut down the peer subsystem.
1800 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down peers\n");
1801 in_shutdown = GNUNET_YES;
1802 GNUNET_CONTAINER_multipeermap_iterate (peers,
1805 if (NULL != core_handle)
1807 GNUNET_CORE_disconnect (core_handle);
1810 if (NULL != transport_handle)
1812 GNUNET_TRANSPORT_disconnect (transport_handle);
1813 transport_handle = NULL;
1815 GNUNET_PEER_change_rc (myid, -1);
1816 GNUNET_CONTAINER_multipeermap_destroy (peers);
1822 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1823 * one and insert it in the appropriate structures if the peer is not known yet.
1825 * @param peer_id Full identity of the peer.
1826 * @param create #GNUNET_YES if a new peer should be created if unknown.
1827 * #GNUNET_NO otherwise.
1829 * @return Existing or newly created peer structure.
1830 * NULL if unknown and not requested @a create
1833 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
1835 struct CadetPeer *peer;
1837 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1840 peer = GNUNET_new (struct CadetPeer);
1841 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1843 peer_delete_oldest ();
1845 GNUNET_assert (GNUNET_OK ==
1846 GNUNET_CONTAINER_multipeermap_put (peers,
1849 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1850 peer->id = GNUNET_PEER_intern (peer_id);
1852 peer->last_contact = GNUNET_TIME_absolute_get ();
1859 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1860 * one and insert it in the appropriate structures if the peer is not known yet.
1862 * @param peer Short identity of the peer.
1863 * @param create #GNUNET_YES if a new peer should be created if unknown.
1864 * #GNUNET_NO otherwise.
1866 * @return Existing or newly created peer structure.
1867 * NULL if unknown and not requested @a create
1870 GCP_get_short (const GNUNET_PEER_Id peer, int create)
1872 return GCP_get (GNUNET_PEER_resolve2 (peer), create);
1877 * Try to connect to a peer on transport level.
1879 * @param cls Closure (peer).
1880 * @param tc TaskContext.
1883 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1885 struct CadetPeer *peer = cls;
1887 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1890 GNUNET_TRANSPORT_try_connect (transport_handle,
1891 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1896 * Try to establish a new connection to this peer (in its tunnel).
1897 * If the peer doesn't have any path to it yet, try to get one.
1898 * If the peer already has some path, send a CREATE CONNECTION towards it.
1900 * @param peer Peer to connect to.
1903 GCP_connect (struct CadetPeer *peer)
1905 struct CadetTunnel *t;
1906 struct CadetPeerPath *path;
1907 struct CadetConnection *c;
1910 GCC_check_connections ();
1911 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1913 /* If we have a current hello, try to connect using it. */
1914 GCP_try_connect (peer);
1918 rerun_search = GNUNET_NO;
1920 if (NULL != peer->path_head)
1922 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1923 path = peer_get_best_path (peer);
1929 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1932 c = GCT_use_path (t, path);
1935 /* This case can happen when the path includes a first hop that is
1936 * not yet known to be connected.
1938 * This happens quite often during testing when running cadet
1939 * under valgrind: core connect notifications come very late
1940 * and the DHT result has already come and created a valid
1941 * path. In this case, the peer->connections
1942 * hashmaps will be NULL and tunnel_use_path will not be able
1943 * to create a connection from that path.
1945 * Re-running the DHT GET should give core time to callback.
1947 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1948 * updating statistics about this issue.
1950 rerun_search = GNUNET_YES;
1954 GCC_send_create (c);
1960 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1964 if (GNUNET_YES == rerun_search)
1966 struct GNUNET_TIME_Relative delay;
1968 GCP_stop_search (peer);
1969 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1970 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
1973 GCC_check_connections ();
1977 if (GNUNET_NO == is_searching (peer))
1978 GCP_start_search (peer);
1979 GCC_check_connections ();
1984 * Chech whether there is a direct (core level) connection to peer.
1986 * @param peer Peer to check.
1988 * @return #GNUNET_YES if there is a direct connection.
1991 GCP_is_neighbor (const struct CadetPeer *peer)
1993 struct CadetPeerPath *path;
1995 if (NULL == peer->connections)
1998 for (path = peer->path_head; NULL != path; path = path->next)
2000 if (3 > path->length)
2004 /* Is not a neighbor but connections is not NULL, probably disconnecting */
2011 * Create and initialize a new tunnel towards a peer, in case it has none.
2012 * In case the peer already has a tunnel, nothing is done.
2014 * Does not generate any traffic, just creates the local data structures.
2016 * @param peer Peer towards which to create the tunnel.
2019 GCP_add_tunnel (struct CadetPeer *peer)
2021 GCC_check_connections ();
2022 if (NULL != peer->tunnel)
2024 peer->tunnel = GCT_new (peer);
2025 GCC_check_connections ();
2030 * Add a connection to a neighboring peer.
2032 * Store that the peer is the first hop of the connection in one
2033 * direction and that on peer disconnect the connection must be
2034 * notified and destroyed, for it will no longer be valid.
2036 * @param peer Peer to add connection to.
2037 * @param c Connection to add.
2038 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
2041 GCP_add_connection (struct CadetPeer *peer,
2042 struct CadetConnection *c,
2045 LOG (GNUNET_ERROR_TYPE_DEBUG,
2046 "adding connection %s\n",
2048 LOG (GNUNET_ERROR_TYPE_DEBUG,
2051 GNUNET_assert (NULL != peer->connections);
2052 GNUNET_assert (GNUNET_OK ==
2053 GNUNET_CONTAINER_multihashmap_put (peer->connections,
2056 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2057 LOG (GNUNET_ERROR_TYPE_DEBUG,
2058 "Peer %s has now %u connections.\n",
2060 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2065 * Add the path to the peer and update the path used to reach it in case this
2068 * @param peer Destination peer to add the path to.
2069 * @param path New path to add. Last peer must be @c peer.
2070 * Path will be either used of freed if already known.
2071 * @param trusted Do we trust that this path is real?
2073 * @return path if path was taken, pointer to existing duplicate if exists
2076 struct CadetPeerPath *
2077 GCP_add_path (struct CadetPeer *peer,
2078 struct CadetPeerPath *path,
2081 struct CadetPeerPath *aux;
2085 GCC_check_connections ();
2086 LOG (GNUNET_ERROR_TYPE_DEBUG,
2087 "adding path [%u] to peer %s\n",
2088 path->length, GCP_2s (peer));
2090 if (NULL == peer || NULL == path
2091 || path->peers[path->length - 1] != peer->id)
2094 path_destroy (path);
2098 for (l = 1; l < path->length; l++)
2100 if (path->peers[l] == myid)
2102 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
2103 for (l2 = 0; l2 < path->length - l; l2++)
2105 path->peers[l2] = path->peers[l + l2];
2109 path->peers = GNUNET_realloc (path->peers,
2110 path->length * sizeof (GNUNET_PEER_Id));
2114 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
2116 if (2 >= path->length && GNUNET_NO == trusted)
2118 /* Only allow CORE to tell us about direct paths */
2119 path_destroy (path);
2123 l = path_get_length (path);
2126 path_destroy (path);
2130 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2131 for (aux = peer->path_head; aux != NULL; aux = aux->next)
2133 l2 = path_get_length (aux);
2136 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
2137 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
2138 peer->path_tail, aux, path);
2143 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2145 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
2146 path_destroy (path);
2151 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
2153 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
2156 if (NULL != peer->tunnel
2157 && CONNECTIONS_PER_TUNNEL > GCT_count_connections (peer->tunnel)
2158 && 2 < path->length) /* Direct paths are handled by core_connect */
2162 GCC_check_connections ();
2168 * Add the path to the origin peer and update the path used to reach it in case
2169 * this is the shortest.
2170 * The path is given in peer_info -> destination, therefore we turn the path
2171 * upside down first.
2173 * @param peer Peer to add the path to, being the origin of the path.
2174 * @param path New path to add after being inversed.
2175 * Path will be either used or freed.
2176 * @param trusted Do we trust that this path is real?
2178 * @return path if path was taken, pointer to existing duplicate if exists
2181 struct CadetPeerPath *
2182 GCP_add_path_to_origin (struct CadetPeer *peer,
2183 struct CadetPeerPath *path,
2189 return GCP_add_path (peer, path, trusted);
2194 * Adds a path to the info of all the peers in the path
2196 * @param p Path to process.
2197 * @param confirmed Whether we know if the path works or not.
2200 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
2204 /* TODO: invert and add */
2205 GCC_check_connections ();
2206 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2207 for (i++; i < p->length; i++)
2209 struct CadetPeer *peer;
2210 struct CadetPeerPath *copy;
2212 peer = GCP_get_short (p->peers[i], GNUNET_YES);
2213 copy = path_duplicate (p);
2214 copy->length = i + 1;
2215 GCP_add_path (peer, copy, 3 > p->length ? GNUNET_NO : confirmed);
2217 GCC_check_connections ();
2222 * Remove any path to the peer that has the exact same peers as the one given.
2224 * @param peer Peer to remove the path from.
2225 * @param path Path to remove. Is always destroyed .
2228 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
2230 struct CadetPeerPath *iter;
2231 struct CadetPeerPath *next;
2233 GCC_check_connections ();
2234 GNUNET_assert (myid == path->peers[0]);
2235 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2237 LOG (GNUNET_ERROR_TYPE_INFO, "Removing path %p (%u) from %s\n",
2238 path, path->length, GCP_2s (peer));
2240 for (iter = peer->path_head; NULL != iter; iter = next)
2243 if (0 == path_cmp (path, iter))
2245 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
2247 path_destroy (iter);
2250 path_destroy (path);
2251 GCC_check_connections ();
2256 * Check that we are aware of a connection from a neighboring peer.
2258 * @param peer Peer to the connection is with
2259 * @param c Connection that should be in the map with this peer.
2262 GCP_check_connection (const struct CadetPeer *peer,
2263 const struct CadetConnection *c)
2265 GNUNET_assert (NULL != peer);
2266 GNUNET_assert (NULL != peer->connections);
2268 GNUNET_assert (GNUNET_YES ==
2269 GNUNET_CONTAINER_multihashmap_contains_value (peer->connections,
2276 * Remove a connection from a neighboring peer.
2278 * @param peer Peer to remove connection from.
2279 * @param c Connection to remove.
2282 GCP_remove_connection (struct CadetPeer *peer,
2283 const struct CadetConnection *c)
2285 LOG (GNUNET_ERROR_TYPE_DEBUG,
2286 "Removing connection %s\n",
2288 LOG (GNUNET_ERROR_TYPE_DEBUG,
2291 if ( (NULL == peer) ||
2292 (NULL == peer->connections) )
2294 GNUNET_assert (GNUNET_YES ==
2295 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2298 LOG (GNUNET_ERROR_TYPE_DEBUG,
2299 "Peer %s remains with %u connections.\n",
2301 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2306 * Start the DHT search for new paths towards the peer: we don't have
2307 * enough good connections.
2309 * @param peer Destination peer.
2312 GCP_start_search (struct CadetPeer *peer)
2314 const struct GNUNET_PeerIdentity *id;
2315 struct CadetTunnel *t = peer->tunnel;
2317 GCC_check_connections ();
2318 if (NULL != peer->search_h)
2324 if (NULL != peer->search_delayed)
2325 GCP_stop_search (peer);
2327 id = GNUNET_PEER_resolve2 (peer->id);
2328 peer->search_h = GCD_search (id, &search_handler, peer);
2332 /* Why would we search for a peer with no tunnel towards it? */
2337 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
2338 || 0 == GCT_count_any_connections (t))
2340 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2342 GCC_check_connections ();
2347 * Stop the DHT search for new paths towards the peer: we already have
2348 * enough good connections.
2350 * @param peer Destination peer.
2353 GCP_stop_search (struct CadetPeer *peer)
2355 GCC_check_connections ();
2356 if (NULL != peer->search_h)
2358 GCD_search_stop (peer->search_h);
2359 peer->search_h = NULL;
2361 if (NULL != peer->search_delayed)
2363 GNUNET_SCHEDULER_cancel (peer->search_delayed);
2364 peer->search_delayed = NULL;
2366 GCC_check_connections ();
2371 * Get the Full ID of a peer.
2373 * @param peer Peer to get from.
2375 * @return Full ID of peer.
2377 const struct GNUNET_PeerIdentity *
2378 GCP_get_id (const struct CadetPeer *peer)
2380 return GNUNET_PEER_resolve2 (peer->id);
2385 * Get the Short ID of a peer.
2387 * @param peer Peer to get from.
2389 * @return Short ID of peer.
2392 GCP_get_short_id (const struct CadetPeer *peer)
2401 * If tunnel is NULL and there was a search active, stop it, as it's useless.
2407 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2410 if (NULL == t && GNUNET_YES == is_searching (peer))
2412 GCP_stop_search (peer);
2418 * Get the tunnel towards a peer.
2420 * @param peer Peer to get from.
2422 * @return Tunnel towards peer.
2424 struct CadetTunnel *
2425 GCP_get_tunnel (const struct CadetPeer *peer)
2429 return peer->tunnel;
2434 * Set the hello message.
2436 * @param peer Peer whose message to set.
2437 * @param hello Hello message.
2440 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2442 struct GNUNET_HELLO_Message *old;
2445 GCC_check_connections ();
2446 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2450 old = GCP_get_hello (peer);
2453 size = GNUNET_HELLO_size (hello);
2454 peer->hello = GNUNET_malloc (size);
2455 memcpy (peer->hello, hello, size);
2459 peer->hello = GNUNET_HELLO_merge (old, hello);
2462 GCC_check_connections ();
2467 * Get the hello message.
2469 * @param peer Peer whose message to get.
2471 * @return Hello message.
2473 struct GNUNET_HELLO_Message *
2474 GCP_get_hello (struct CadetPeer *peer)
2476 struct GNUNET_TIME_Absolute expiration;
2477 struct GNUNET_TIME_Relative remaining;
2479 if (NULL == peer->hello)
2482 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2483 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2484 if (0 == remaining.rel_value_us)
2486 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2487 GNUNET_STRINGS_absolute_time_to_string (expiration));
2488 GNUNET_free (peer->hello);
2496 * Try to connect to a peer on TRANSPORT level.
2498 * @param peer Peer to whom to connect.
2501 GCP_try_connect (struct CadetPeer *peer)
2503 struct GNUNET_HELLO_Message *hello;
2504 struct GNUNET_MessageHeader *mh;
2506 if (NULL == transport_handle)
2508 GCC_check_connections ();
2509 if (GNUNET_YES == GCP_is_neighbor (peer))
2511 hello = GCP_get_hello (peer);
2515 mh = GNUNET_HELLO_get_header (hello);
2516 GNUNET_TRANSPORT_offer_hello (transport_handle,
2520 GCC_check_connections ();
2525 * Notify a peer that a link between two other peers is broken. If any path
2526 * used that link, eliminate it.
2528 * @param peer Peer affected by the change.
2529 * @param peer1 Peer whose link is broken.
2530 * @param peer2 Peer whose link is broken.
2533 GCP_notify_broken_link (struct CadetPeer *peer,
2534 const struct GNUNET_PeerIdentity *peer1,
2535 const struct GNUNET_PeerIdentity *peer2)
2537 struct CadetPeerPath *iter;
2538 struct CadetPeerPath *next;
2543 GCC_check_connections ();
2544 p1 = GNUNET_PEER_search (peer1);
2545 p2 = GNUNET_PEER_search (peer2);
2547 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2548 if (0 == p1 || 0 == p2)
2550 /* We don't even know them */
2554 for (iter = peer->path_head; NULL != iter; iter = next)
2557 for (i = 0; i < iter->length - 1; i++)
2559 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2560 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2565 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2568 path_invalidate (iter);
2572 GCC_check_connections ();
2577 * Count the number of known paths toward the peer.
2579 * @param peer Peer to get path info.
2581 * @return Number of known paths.
2584 GCP_count_paths (const struct CadetPeer *peer)
2586 struct CadetPeerPath *iter;
2589 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2597 * Iterate over the paths to a peer.
2599 * @param peer Peer to get path info.
2600 * @param callback Function to call for every path.
2601 * @param cls Closure for @a callback.
2603 * @return Number of iterated paths.
2606 GCP_iterate_paths (struct CadetPeer *peer,
2607 GCP_path_iterator callback,
2610 struct CadetPeerPath *iter;
2613 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2616 if (GNUNET_YES != callback (cls, peer, iter))
2625 * Iterate all known peers.
2627 * @param iter Iterator.
2628 * @param cls Closure for @c iter.
2631 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2634 GCC_check_connections ();
2635 GNUNET_CONTAINER_multipeermap_iterate (peers,
2638 GCC_check_connections ();
2643 * Get the static string for a peer ID.
2647 * @return Static string for it's ID.
2650 GCP_2s (const struct CadetPeer *peer)
2654 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2658 /* end of gnunet-service-cadet_peer.c */