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 if (NULL != peer->core_transmit)
757 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
758 GNUNET_free_non_null (peer->hello);
765 * Iterator over peer hash map entries to destroy the peer during in_shutdown.
768 * @param key current key code
769 * @param value value in the hash map
770 * @return #GNUNET_YES if we should continue to iterate,
774 shutdown_peer (void *cls,
775 const struct GNUNET_PeerIdentity *key,
778 struct CadetPeer *p = value;
779 struct CadetTunnel *t = p->tunnel;
780 LOG (GNUNET_ERROR_TYPE_DEBUG, " shutting down %s\n", GCP_2s (p));
791 * Check if peer is searching for a path (either active or delayed search).
793 * @param peer Peer to check
794 * @return #GNUNET_YES if there is a search active.
795 * #GNUNET_NO otherwise.
798 is_searching (const struct CadetPeer *peer)
800 return ( (NULL == peer->search_h) &&
801 (NULL == peer->search_delayed) ) ?
802 GNUNET_NO : GNUNET_YES;
807 * @brief Start a search for a peer.
809 * @param cls Closure (Peer to search for).
810 * @param tc Task context.
813 delayed_search (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
815 struct CadetPeer *peer = cls;
817 peer->search_delayed = NULL;
818 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
820 GCC_check_connections ();
821 GCP_start_search (peer);
822 GCC_check_connections ();
827 * Returns if peer is used (has a tunnel or is neighbor).
829 * @param peer Peer to check.
830 * @return #GNUNET_YES if peer is in use.
833 peer_is_used (struct CadetPeer *peer)
835 struct CadetPeerPath *p;
837 if (NULL != peer->tunnel)
840 for (p = peer->path_head; NULL != p; p = p->next)
850 * Iterator over all the peers to get the oldest timestamp.
852 * @param cls Closure (unsued).
853 * @param key ID of the peer.
854 * @param value Peer_Info of the peer.
857 peer_get_oldest (void *cls,
858 const struct GNUNET_PeerIdentity *key,
861 struct CadetPeer *p = value;
862 struct GNUNET_TIME_Absolute *abs = cls;
864 /* Don't count active peers */
865 if (GNUNET_YES == peer_is_used (p))
868 if (abs->abs_value_us < p->last_contact.abs_value_us)
869 abs->abs_value_us = p->last_contact.abs_value_us;
876 * Iterator over all the peers to remove the oldest entry.
878 * @param cls Closure (unsued).
879 * @param key ID of the peer.
880 * @param value Peer_Info of the peer.
883 peer_timeout (void *cls,
884 const struct GNUNET_PeerIdentity *key,
887 struct CadetPeer *p = value;
888 struct GNUNET_TIME_Absolute *abs = cls;
890 LOG (GNUNET_ERROR_TYPE_WARNING,
891 "peer %s timeout\n", GNUNET_i2s (key));
893 if (p->last_contact.abs_value_us == abs->abs_value_us &&
894 GNUNET_NO == peer_is_used (p))
904 * Delete oldest unused peer.
907 peer_delete_oldest (void)
909 struct GNUNET_TIME_Absolute abs;
911 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
913 GNUNET_CONTAINER_multipeermap_iterate (peers,
916 GNUNET_CONTAINER_multipeermap_iterate (peers,
923 * Choose the best (yet unused) path towards a peer,
924 * considering the tunnel properties.
926 * @param peer The destination peer.
927 * @return Best current known path towards the peer, if any.
929 static struct CadetPeerPath *
930 peer_get_best_path (const struct CadetPeer *peer)
932 struct CadetPeerPath *best_p;
933 struct CadetPeerPath *p;
934 unsigned int best_cost;
937 best_cost = UINT_MAX;
939 for (p = peer->path_head; NULL != p; p = p->next)
941 if (GNUNET_NO == path_is_valid (p))
942 continue; /* Don't use invalid paths. */
943 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
944 continue; /* If path is already in use, skip it. */
946 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
957 * Is this queue element sendable?
959 * - All management traffic is always sendable.
960 * - For payload traffic, check the connection flow control.
962 * @param q Queue element to inspect.
963 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
966 queue_is_sendable (struct CadetPeerQueue *q)
968 /* Is PID-independent? */
971 case GNUNET_MESSAGE_TYPE_CADET_ACK:
972 case GNUNET_MESSAGE_TYPE_CADET_POLL:
973 case GNUNET_MESSAGE_TYPE_CADET_KX:
974 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
975 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
976 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
977 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
980 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
981 case GNUNET_MESSAGE_TYPE_CADET_AX:
988 return GCC_is_sendable (q->c, q->fwd);
993 * Get first sendable message.
995 * @param peer The destination peer.
997 * @return First transmittable message, if any. Otherwise, NULL.
999 static struct CadetPeerQueue *
1000 peer_get_first_message (const struct CadetPeer *peer)
1002 struct CadetPeerQueue *q;
1004 for (q = peer->queue_head; NULL != q; q = q->next)
1006 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
1007 if (queue_is_sendable (q))
1016 * Function to process paths received for a new peer addition. The recorded
1017 * paths form the initial tunnel, which can be optimized later.
1018 * Called on each result obtained for the DHT search.
1020 * @param cls Closure (peer towards a path has been found).
1021 * @param path Path created from the DHT query. Will be freed afterwards.
1024 search_handler (void *cls, const struct CadetPeerPath *path)
1026 struct CadetPeer *peer = cls;
1027 unsigned int connection_count;
1029 GCC_check_connections ();
1030 GCP_add_path_to_all (path, GNUNET_NO);
1032 /* Count connections */
1033 connection_count = GCT_count_connections (peer->tunnel);
1035 /* If we already have our minimum (or more) connections, it's enough */
1036 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1038 GCC_check_connections ();
1042 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1044 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1047 GCC_check_connections ();
1052 * Adjust core requested size to accomodate an ACK.
1054 * @param message_size Requested size.
1056 * @return Size enough to fit @c message_size and an ACK.
1059 get_core_size (size_t message_size)
1061 return message_size + sizeof (struct GNUNET_CADET_ACK);
1066 * Fill a core buffer with the appropriate data for the queued message.
1068 * @param queue Queue element for the message.
1069 * @param buf Core buffer to fill.
1070 * @param size Size remaining in @c buf.
1071 * @param[out] pid In case its an encrypted payload, set payload.
1073 * @return Bytes written to @c buf.
1076 fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
1078 struct CadetConnection *c = queue->c;
1081 switch (queue->type)
1083 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1084 *pid = GCC_get_pid (queue->c, queue->fwd);
1085 LOG (GNUNET_ERROR_TYPE_DEBUG, " otr payload ID %u\n", *pid);
1086 msg_size = send_core_data_raw (queue->cls, size, buf);
1087 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
1089 case GNUNET_MESSAGE_TYPE_CADET_AX:
1090 *pid = GCC_get_pid (queue->c, queue->fwd);
1091 LOG (GNUNET_ERROR_TYPE_DEBUG, " ax payload ID %u\n", *pid);
1092 msg_size = send_core_data_raw (queue->cls, size, buf);
1093 ((struct GNUNET_CADET_AX *) buf)->pid = htonl (*pid);
1095 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1096 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1097 case GNUNET_MESSAGE_TYPE_CADET_KX:
1098 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1099 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1100 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1101 msg_size = send_core_data_raw (queue->cls, size, buf);
1103 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1104 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1105 if (GCC_is_origin (c, GNUNET_YES))
1106 msg_size = send_core_connection_create (c, size, buf);
1108 msg_size = send_core_data_raw (queue->cls, size, buf);
1110 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1111 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1112 if (GCC_is_origin (c, GNUNET_NO) ||
1113 GCC_is_origin (c, GNUNET_YES))
1115 msg_size = send_core_connection_ack (c, size, buf);
1119 msg_size = send_core_data_raw (queue->cls, size, buf);
1122 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1123 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1124 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1125 /* This should be encapsulted */
1131 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1135 GNUNET_assert (size >= msg_size);
1142 * Core callback to write a queued packet to core buffer
1144 * @param cls Closure (peer info).
1145 * @param size Number of bytes available in buf.
1146 * @param buf Where the to write the message.
1148 * @return number of bytes written to buf
1151 queue_send (void *cls, size_t size, void *buf)
1153 struct CadetPeer *peer = cls;
1154 struct CadetConnection *c;
1155 struct CadetPeerQueue *queue;
1156 struct GNUNET_TIME_Relative core_wait_time;
1158 const struct GNUNET_PeerIdentity *dst_id;
1165 GCC_check_connections ();
1166 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1167 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1168 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
1169 GCP_2s (peer), size);
1171 /* Sanity checking */
1172 if (NULL == buf || 0 == size)
1174 LOG (GNUNET_ERROR_TYPE_DEBUG, " not allowed/\n");
1175 if (GNUNET_NO == in_shutdown)
1177 queue = peer_get_first_message (peer);
1180 peer->core_transmit = NULL;
1181 peer->tmt_time.abs_value_us = 0;
1182 GCC_check_connections ();
1185 dst_id = GNUNET_PEER_resolve2 (peer->id);
1186 peer->core_transmit =
1187 GNUNET_CORE_notify_transmit_ready (core_handle,
1188 GNUNET_NO, get_priority (queue),
1189 GNUNET_TIME_UNIT_FOREVER_REL,
1191 get_core_size (queue->size),
1194 peer->tmt_time = GNUNET_TIME_absolute_get ();
1198 peer->core_transmit = NULL;
1199 peer->tmt_time.abs_value_us = 0;
1201 GCC_check_connections ();
1210 peer->core_transmit = NULL;
1211 queue = peer_get_first_message (peer);
1214 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
1215 peer->tmt_time.abs_value_us = 0;
1218 core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1219 wait_s = GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_YES);
1220 if (core_wait_time.rel_value_us >= 1000000)
1222 LOG (GNUNET_ERROR_TYPE_WARNING,
1223 " %s: core wait time %s (> 1 second) for %u bytes\n",
1224 GCP_2s (peer), wait_s, queue->size);
1226 peer->tmt_time.abs_value_us = 0;
1228 /* Copy all possible messages to the core buffer */
1229 while (NULL != queue && rest >= queue->size)
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, " on conn %s %s\n",
1234 GCC_2s (c), GC_f2s(queue->fwd));
1235 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok (%u/%u)\n",
1236 queue->size, total_size, size);
1238 msg_size = fill_buf (queue, (void *) dst, size, &pid);
1240 if (0 < drop_percent &&
1241 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1243 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on conn %s %s\n",
1244 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1245 queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
1250 LOG (GNUNET_ERROR_TYPE_INFO,
1251 ">>> %s (%s %4u) on conn %s (%p) %s [%5u], after %s\n",
1252 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1253 queue->payload_id, GCC_2s (c), c,
1254 GC_f2s (queue->fwd), msg_size, wait_s);
1256 total_size += msg_size;
1258 dst = &dst[msg_size];
1261 /* Free queue, but cls was freed by send_core_* in fill_buf. */
1262 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1265 queue = peer_get_first_message (peer);
1268 /* If more data in queue, send next */
1271 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data! (%u)\n", queue->size);
1272 if (NULL == peer->core_transmit)
1274 dst_id = GNUNET_PEER_resolve2 (peer->id);
1275 peer->core_transmit =
1276 GNUNET_CORE_notify_transmit_ready (core_handle,
1277 GNUNET_NO, get_priority (queue),
1278 GNUNET_TIME_UNIT_FOREVER_REL,
1280 get_core_size (queue->size),
1283 peer->tmt_time = GNUNET_TIME_absolute_get ();
1284 queue->start_waiting = GNUNET_TIME_absolute_get ();
1288 LOG (GNUNET_ERROR_TYPE_DEBUG, "* tmt rdy called somewhere else\n");
1290 // GCC_start_poll (); FIXME needed?
1294 // GCC_stop_poll(); FIXME needed?
1297 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", total_size);
1298 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1299 GCC_check_connections ();
1304 /******************************************************************************/
1305 /******************************** API ***********************************/
1306 /******************************************************************************/
1310 * Free a transmission that was already queued with all resources
1311 * associated to the request.
1313 * If connection was marked to be destroyed, and this was the last queued
1314 * message on it, the connection will be free'd as a result.
1316 * @param queue Queue handler to cancel.
1317 * @param clear_cls Is it necessary to free associated cls?
1318 * @param sent Was it really sent? (Could have been canceled)
1319 * @param pid PID, if relevant (was sent and was a payload message).
1321 * @return #GNUNET_YES if connection was destroyed as a result,
1322 * #GNUNET_NO otherwise.
1325 GCP_queue_destroy (struct CadetPeerQueue *queue,
1330 struct CadetPeer *peer;
1331 int connection_destroyed;
1333 GCC_check_connections ();
1335 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1336 if (GNUNET_YES == clear_cls)
1338 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1339 switch (queue->type)
1341 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1342 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1344 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1345 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1346 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1347 case GNUNET_MESSAGE_TYPE_CADET_KX:
1348 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1349 case GNUNET_MESSAGE_TYPE_CADET_AX:
1350 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1351 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1352 GNUNET_free_non_null (queue->cls);
1357 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1358 GC_m2s (queue->type));
1361 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1363 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1364 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1369 if (NULL != queue->cont)
1371 struct GNUNET_TIME_Relative wait_time;
1373 wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1374 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1375 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1376 connection_destroyed = queue->cont (queue->cont_cls,
1377 queue->c, sent, queue->type, pid,
1378 queue->fwd, queue->size, wait_time);
1382 connection_destroyed = GNUNET_NO;
1385 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1387 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1388 peer->core_transmit = NULL;
1389 peer->tmt_time.abs_value_us = 0;
1392 GNUNET_free (queue);
1393 GCC_check_connections ();
1394 return connection_destroyed;
1399 * @brief Queue and pass message to core when possible.
1401 * @param peer Peer towards which to queue the message.
1402 * @param cls Closure (@c type dependant). It will be used by queue_send to
1403 * build the message to be sent if not already prebuilt.
1404 * @param type Type of the message.
1405 * @param payload_type Type of the message's payload
1406 * 0 if the message is a retransmission (unknown payload).
1407 * UINT16_MAX if the message does not have payload.
1408 * @param payload_id ID of the payload (MID, ACK #, etc)
1409 * @param size Size of the message.
1410 * @param c Connection this message belongs to (can be NULL).
1411 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1412 * @param cont Continuation to be called once CORE has taken the message.
1413 * @param cont_cls Closure for @c cont.
1415 * @return Handle to cancel the message before it is sent. Once cont is called
1416 * message has been sent and therefore the handle is no longer valid.
1418 struct CadetPeerQueue *
1419 GCP_queue_add (struct CadetPeer *peer,
1422 uint16_t payload_type,
1423 uint32_t payload_id,
1425 struct CadetConnection *c,
1430 struct CadetPeerQueue *q;
1434 GCC_check_connections ();
1435 LOG (GNUNET_ERROR_TYPE_DEBUG,
1436 "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n",
1437 GC_m2s (type), GC_m2s (payload_type), payload_id,
1438 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1440 if (NULL == peer->connections)
1442 /* We are not connected to this peer, ignore request. */
1443 LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
1444 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1451 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1452 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1457 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1459 call_core = (NULL == c || type == GNUNET_MESSAGE_TYPE_CADET_KX) ?
1460 GNUNET_YES : GCC_is_sendable (c, fwd);
1461 q = GNUNET_new (struct CadetPeerQueue);
1464 q->payload_type = payload_type;
1465 q->payload_id = payload_id;
1471 q->cont_cls = cont_cls;
1474 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1479 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1480 call_core = GNUNET_YES;
1483 q->start_waiting = GNUNET_TIME_absolute_get ();
1484 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1486 LOG (GNUNET_ERROR_TYPE_DEBUG,
1487 "calling core tmt rdy towards %s for %u bytes\n",
1488 GCP_2s (peer), size);
1489 peer->core_transmit =
1490 GNUNET_CORE_notify_transmit_ready (core_handle,
1491 GNUNET_NO, get_priority (q),
1492 GNUNET_TIME_UNIT_FOREVER_REL,
1493 GNUNET_PEER_resolve2 (peer->id),
1494 get_core_size (size),
1496 peer->tmt_time = GNUNET_TIME_absolute_get ();
1498 else if (GNUNET_NO == call_core)
1500 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1506 struct GNUNET_TIME_Relative elapsed;
1507 elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1508 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
1510 GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
1513 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1514 GCC_check_connections ();
1520 * Cancel all queued messages to a peer that belong to a certain connection.
1522 * @param peer Peer towards whom to cancel.
1523 * @param c Connection whose queued messages to cancel. Might be destroyed by
1524 * the sent continuation call.
1527 GCP_queue_cancel (struct CadetPeer *peer,
1528 struct CadetConnection *c)
1530 struct CadetPeerQueue *q;
1531 struct CadetPeerQueue *next;
1532 struct CadetPeerQueue *prev;
1533 int connection_destroyed;
1535 GCC_check_connections ();
1536 connection_destroyed = GNUNET_NO;
1537 for (q = peer->queue_head; NULL != q; q = next)
1542 LOG (GNUNET_ERROR_TYPE_DEBUG,
1543 "GMP queue cancel %s\n",
1545 GNUNET_assert (GNUNET_NO == connection_destroyed);
1546 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1552 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1555 /* Get next from prev, q->next might be already freed:
1556 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1559 next = peer->queue_head;
1569 if ( (NULL == peer->queue_head) &&
1570 (NULL != peer->core_transmit) )
1572 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1573 peer->core_transmit = NULL;
1574 peer->tmt_time.abs_value_us = 0;
1576 GCC_check_connections ();
1581 * Get the first transmittable message for a connection.
1583 * @param peer Neighboring peer.
1584 * @param c Connection.
1586 * @return First transmittable message.
1588 static struct CadetPeerQueue *
1589 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1591 struct CadetPeerQueue *q;
1593 for (q = peer->queue_head; NULL != q; q = q->next)
1597 if (queue_is_sendable (q))
1599 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1602 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1610 * Get the first message for a connection and unqueue it.
1612 * Only tunnel (or higher) level messages are unqueued. Connection specific
1613 * messages are silently destroyed upon encounter.
1615 * @param peer Neighboring peer.
1616 * @param c Connection.
1617 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1620 * @return First message for this connection.
1622 struct GNUNET_MessageHeader *
1623 GCP_connection_pop (struct CadetPeer *peer,
1624 struct CadetConnection *c,
1627 struct CadetPeerQueue *q;
1628 struct CadetPeerQueue *next;
1629 struct GNUNET_MessageHeader *msg;
1632 GCC_check_connections ();
1633 GNUNET_assert (NULL != destroyed);
1634 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on conn %p\n", c);
1635 for (q = peer->queue_head; NULL != q; q = next)
1640 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), cont: %p\n",
1641 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1645 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1646 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1647 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1648 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1649 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1650 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1651 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1652 if (GNUNET_YES == dest)
1654 GNUNET_break (GNUNET_NO == *destroyed);
1655 *destroyed = GNUNET_YES;
1659 case GNUNET_MESSAGE_TYPE_CADET_KX:
1660 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1661 case GNUNET_MESSAGE_TYPE_CADET_AX:
1662 case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
1663 msg = (struct GNUNET_MessageHeader *) q->cls;
1664 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1665 if (GNUNET_YES == dest)
1667 GNUNET_break (GNUNET_NO == *destroyed);
1668 *destroyed = GNUNET_YES;
1674 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1677 GCC_check_connections ();
1683 * Unlock a possibly locked queue for a connection.
1685 * If there is a message that can be sent on this connection, call core for it.
1686 * Otherwise (if core transmit is already called or there is no sendable
1687 * message) do nothing.
1689 * @param peer Peer who keeps the queue.
1690 * @param c Connection whose messages to unlock.
1693 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1695 struct CadetPeerQueue *q;
1698 GCC_check_connections ();
1699 if (NULL != peer->core_transmit)
1701 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1702 return; /* Already unlocked */
1705 q = connection_get_first_message (peer, c);
1708 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1709 return; /* Nothing to transmit */
1713 peer->core_transmit =
1714 GNUNET_CORE_notify_transmit_ready (core_handle,
1715 GNUNET_NO, get_priority (q),
1716 GNUNET_TIME_UNIT_FOREVER_REL,
1717 GNUNET_PEER_resolve2 (peer->id),
1718 get_core_size (size),
1721 peer->tmt_time = GNUNET_TIME_absolute_get ();
1722 GCC_check_connections ();
1727 * Initialize the peer subsystem.
1729 * @param c Configuration.
1732 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1734 LOG (GNUNET_ERROR_TYPE_DEBUG,
1736 in_shutdown = GNUNET_NO;
1737 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1739 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1742 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1743 "CADET", "MAX_PEERS", "USING DEFAULT");
1748 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1755 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1756 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1757 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1758 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1759 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1762 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1763 NULL, /* Closure passed to CADET functions */
1764 &core_init, /* Call core_init once connected */
1765 &core_connect, /* Handle connects */
1766 &core_disconnect, /* remove peers on disconnects */
1767 NULL, /* Don't notify about all incoming messages */
1768 GNUNET_NO, /* For header only in notification */
1769 NULL, /* Don't notify about all outbound messages */
1770 GNUNET_NO, /* For header-only out notification */
1771 core_handlers); /* Register these handlers */
1773 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1775 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1776 /* Notify callbacks */
1781 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1782 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1783 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1784 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1785 transport_handle = NULL;
1790 if (NULL == core_handle)
1793 GNUNET_SCHEDULER_shutdown ();
1801 * Shut down the peer subsystem.
1806 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down peers\n");
1807 in_shutdown = GNUNET_YES;
1808 GNUNET_CONTAINER_multipeermap_iterate (peers,
1811 if (NULL != core_handle)
1813 GNUNET_CORE_disconnect (core_handle);
1816 if (NULL != transport_handle)
1818 GNUNET_TRANSPORT_disconnect (transport_handle);
1819 transport_handle = NULL;
1821 GNUNET_PEER_change_rc (myid, -1);
1822 GNUNET_CONTAINER_multipeermap_destroy (peers);
1828 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1829 * one and insert it in the appropriate structures if the peer is not known yet.
1831 * @param peer_id Full identity of the peer.
1832 * @param create #GNUNET_YES if a new peer should be created if unknown.
1833 * #GNUNET_NO otherwise.
1835 * @return Existing or newly created peer structure.
1836 * NULL if unknown and not requested @a create
1839 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
1841 struct CadetPeer *peer;
1843 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1846 peer = GNUNET_new (struct CadetPeer);
1847 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1849 peer_delete_oldest ();
1851 GNUNET_assert (GNUNET_OK ==
1852 GNUNET_CONTAINER_multipeermap_put (peers,
1855 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1856 peer->id = GNUNET_PEER_intern (peer_id);
1858 peer->last_contact = GNUNET_TIME_absolute_get ();
1865 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1866 * one and insert it in the appropriate structures if the peer is not known yet.
1868 * @param peer Short identity of the peer.
1869 * @param create #GNUNET_YES if a new peer should be created if unknown.
1870 * #GNUNET_NO otherwise.
1872 * @return Existing or newly created peer structure.
1873 * NULL if unknown and not requested @a create
1876 GCP_get_short (const GNUNET_PEER_Id peer, int create)
1878 return GCP_get (GNUNET_PEER_resolve2 (peer), create);
1883 * Try to connect to a peer on transport level.
1885 * @param cls Closure (peer).
1886 * @param tc TaskContext.
1889 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1891 struct CadetPeer *peer = cls;
1893 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1896 GNUNET_TRANSPORT_try_connect (transport_handle,
1897 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1902 * Try to establish a new connection to this peer (in its tunnel).
1903 * If the peer doesn't have any path to it yet, try to get one.
1904 * If the peer already has some path, send a CREATE CONNECTION towards it.
1906 * @param peer Peer to connect to.
1909 GCP_connect (struct CadetPeer *peer)
1911 struct CadetTunnel *t;
1912 struct CadetPeerPath *path;
1913 struct CadetConnection *c;
1916 GCC_check_connections ();
1917 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1919 /* If we have a current hello, try to connect using it. */
1920 GCP_try_connect (peer);
1924 rerun_search = GNUNET_NO;
1926 if (NULL != peer->path_head)
1928 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1929 path = peer_get_best_path (peer);
1935 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1938 c = GCT_use_path (t, path);
1941 /* This case can happen when the path includes a first hop that is
1942 * not yet known to be connected.
1944 * This happens quite often during testing when running cadet
1945 * under valgrind: core connect notifications come very late
1946 * and the DHT result has already come and created a valid
1947 * path. In this case, the peer->connections
1948 * hashmaps will be NULL and tunnel_use_path will not be able
1949 * to create a connection from that path.
1951 * Re-running the DHT GET should give core time to callback.
1953 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1954 * updating statistics about this issue.
1956 rerun_search = GNUNET_YES;
1960 GCC_send_create (c);
1966 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1970 if (GNUNET_YES == rerun_search)
1972 struct GNUNET_TIME_Relative delay;
1974 GCP_stop_search (peer);
1975 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1976 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
1979 GCC_check_connections ();
1983 if (GNUNET_NO == is_searching (peer))
1984 GCP_start_search (peer);
1985 GCC_check_connections ();
1990 * Chech whether there is a direct (core level) connection to peer.
1992 * @param peer Peer to check.
1994 * @return #GNUNET_YES if there is a direct connection.
1997 GCP_is_neighbor (const struct CadetPeer *peer)
1999 struct CadetPeerPath *path;
2001 if (NULL == peer->connections)
2004 for (path = peer->path_head; NULL != path; path = path->next)
2006 if (3 > path->length)
2010 /* Is not a neighbor but connections is not NULL, probably disconnecting */
2017 * Create and initialize a new tunnel towards a peer, in case it has none.
2018 * In case the peer already has a tunnel, nothing is done.
2020 * Does not generate any traffic, just creates the local data structures.
2022 * @param peer Peer towards which to create the tunnel.
2025 GCP_add_tunnel (struct CadetPeer *peer)
2027 GCC_check_connections ();
2028 if (NULL != peer->tunnel)
2030 peer->tunnel = GCT_new (peer);
2031 GCC_check_connections ();
2036 * Add a connection to a neighboring peer.
2038 * Store that the peer is the first hop of the connection in one
2039 * direction and that on peer disconnect the connection must be
2040 * notified and destroyed, for it will no longer be valid.
2042 * @param peer Peer to add connection to.
2043 * @param c Connection to add.
2044 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
2047 GCP_add_connection (struct CadetPeer *peer,
2048 struct CadetConnection *c,
2051 LOG (GNUNET_ERROR_TYPE_DEBUG,
2052 "adding connection %s\n",
2054 LOG (GNUNET_ERROR_TYPE_DEBUG,
2057 GNUNET_assert (NULL != peer->connections);
2058 GNUNET_assert (GNUNET_OK ==
2059 GNUNET_CONTAINER_multihashmap_put (peer->connections,
2062 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2063 LOG (GNUNET_ERROR_TYPE_DEBUG,
2064 "Peer %s has now %u connections.\n",
2066 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2071 * Add the path to the peer and update the path used to reach it in case this
2074 * @param peer Destination peer to add the path to.
2075 * @param path New path to add. Last peer must be @c peer.
2076 * Path will be either used of freed if already known.
2077 * @param trusted Do we trust that this path is real?
2079 * @return path if path was taken, pointer to existing duplicate if exists
2082 struct CadetPeerPath *
2083 GCP_add_path (struct CadetPeer *peer,
2084 struct CadetPeerPath *path,
2087 struct CadetPeerPath *aux;
2091 GCC_check_connections ();
2092 LOG (GNUNET_ERROR_TYPE_DEBUG,
2093 "adding path [%u] to peer %s\n",
2094 path->length, GCP_2s (peer));
2096 if (NULL == peer || NULL == path
2097 || path->peers[path->length - 1] != peer->id)
2100 path_destroy (path);
2104 for (l = 1; l < path->length; l++)
2106 if (path->peers[l] == myid)
2108 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
2109 for (l2 = 0; l2 < path->length - l; l2++)
2111 path->peers[l2] = path->peers[l + l2];
2115 path->peers = GNUNET_realloc (path->peers,
2116 path->length * sizeof (GNUNET_PEER_Id));
2120 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
2122 if (2 >= path->length && GNUNET_NO == trusted)
2124 /* Only allow CORE to tell us about direct paths */
2125 path_destroy (path);
2129 l = path_get_length (path);
2132 path_destroy (path);
2136 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2137 for (aux = peer->path_head; aux != NULL; aux = aux->next)
2139 l2 = path_get_length (aux);
2142 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
2143 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
2144 peer->path_tail, aux, path);
2149 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2151 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
2152 path_destroy (path);
2157 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
2159 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
2162 if (NULL != peer->tunnel
2163 && CONNECTIONS_PER_TUNNEL > GCT_count_connections (peer->tunnel)
2164 && 2 < path->length) /* Direct paths are handled by core_connect */
2168 GCC_check_connections ();
2174 * Add the path to the origin peer and update the path used to reach it in case
2175 * this is the shortest.
2176 * The path is given in peer_info -> destination, therefore we turn the path
2177 * upside down first.
2179 * @param peer Peer to add the path to, being the origin of the path.
2180 * @param path New path to add after being inversed.
2181 * Path will be either used or freed.
2182 * @param trusted Do we trust that this path is real?
2184 * @return path if path was taken, pointer to existing duplicate if exists
2187 struct CadetPeerPath *
2188 GCP_add_path_to_origin (struct CadetPeer *peer,
2189 struct CadetPeerPath *path,
2195 return GCP_add_path (peer, path, trusted);
2200 * Adds a path to the info of all the peers in the path
2202 * @param p Path to process.
2203 * @param confirmed Whether we know if the path works or not.
2206 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
2210 /* TODO: invert and add */
2211 GCC_check_connections ();
2212 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2213 for (i++; i < p->length; i++)
2215 struct CadetPeer *peer;
2216 struct CadetPeerPath *copy;
2218 peer = GCP_get_short (p->peers[i], GNUNET_YES);
2219 copy = path_duplicate (p);
2220 copy->length = i + 1;
2221 GCP_add_path (peer, copy, 3 > p->length ? GNUNET_NO : confirmed);
2223 GCC_check_connections ();
2228 * Remove any path to the peer that has the exact same peers as the one given.
2230 * @param peer Peer to remove the path from.
2231 * @param path Path to remove. Is always destroyed .
2234 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
2236 struct CadetPeerPath *iter;
2237 struct CadetPeerPath *next;
2239 GCC_check_connections ();
2240 GNUNET_assert (myid == path->peers[0]);
2241 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2243 LOG (GNUNET_ERROR_TYPE_INFO, "Removing path %p (%u) from %s\n",
2244 path, path->length, GCP_2s (peer));
2246 for (iter = peer->path_head; NULL != iter; iter = next)
2249 if (0 == path_cmp (path, iter))
2251 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
2253 path_destroy (iter);
2256 path_destroy (path);
2257 GCC_check_connections ();
2262 * Check that we are aware of a connection from a neighboring peer.
2264 * @param peer Peer to the connection is with
2265 * @param c Connection that should be in the map with this peer.
2268 GCP_check_connection (const struct CadetPeer *peer,
2269 const struct CadetConnection *c)
2271 GNUNET_assert (NULL != peer);
2272 GNUNET_assert (NULL != peer->connections);
2274 GNUNET_assert (GNUNET_YES ==
2275 GNUNET_CONTAINER_multihashmap_contains_value (peer->connections,
2282 * Remove a connection from a neighboring peer.
2284 * @param peer Peer to remove connection from.
2285 * @param c Connection to remove.
2288 GCP_remove_connection (struct CadetPeer *peer,
2289 const struct CadetConnection *c)
2291 LOG (GNUNET_ERROR_TYPE_DEBUG,
2292 "Removing connection %s\n",
2294 LOG (GNUNET_ERROR_TYPE_DEBUG,
2297 if ( (NULL == peer) ||
2298 (NULL == peer->connections) )
2300 GNUNET_assert (GNUNET_YES ==
2301 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2304 LOG (GNUNET_ERROR_TYPE_DEBUG,
2305 "Peer %s remains with %u connections.\n",
2307 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2312 * Start the DHT search for new paths towards the peer: we don't have
2313 * enough good connections.
2315 * @param peer Destination peer.
2318 GCP_start_search (struct CadetPeer *peer)
2320 const struct GNUNET_PeerIdentity *id;
2321 struct CadetTunnel *t = peer->tunnel;
2323 GCC_check_connections ();
2324 if (NULL != peer->search_h)
2330 if (NULL != peer->search_delayed)
2331 GCP_stop_search (peer);
2333 id = GNUNET_PEER_resolve2 (peer->id);
2334 peer->search_h = GCD_search (id, &search_handler, peer);
2338 /* Why would we search for a peer with no tunnel towards it? */
2343 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
2344 || 0 == GCT_count_any_connections (t))
2346 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2348 GCC_check_connections ();
2353 * Stop the DHT search for new paths towards the peer: we already have
2354 * enough good connections.
2356 * @param peer Destination peer.
2359 GCP_stop_search (struct CadetPeer *peer)
2361 GCC_check_connections ();
2362 if (NULL != peer->search_h)
2364 GCD_search_stop (peer->search_h);
2365 peer->search_h = NULL;
2367 if (NULL != peer->search_delayed)
2369 GNUNET_SCHEDULER_cancel (peer->search_delayed);
2370 peer->search_delayed = NULL;
2372 GCC_check_connections ();
2377 * Get the Full ID of a peer.
2379 * @param peer Peer to get from.
2381 * @return Full ID of peer.
2383 const struct GNUNET_PeerIdentity *
2384 GCP_get_id (const struct CadetPeer *peer)
2386 return GNUNET_PEER_resolve2 (peer->id);
2391 * Get the Short ID of a peer.
2393 * @param peer Peer to get from.
2395 * @return Short ID of peer.
2398 GCP_get_short_id (const struct CadetPeer *peer)
2407 * If tunnel is NULL and there was a search active, stop it, as it's useless.
2413 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2416 if (NULL == t && GNUNET_YES == is_searching (peer))
2418 GCP_stop_search (peer);
2424 * Get the tunnel towards a peer.
2426 * @param peer Peer to get from.
2428 * @return Tunnel towards peer.
2430 struct CadetTunnel *
2431 GCP_get_tunnel (const struct CadetPeer *peer)
2435 return peer->tunnel;
2440 * Set the hello message.
2442 * @param peer Peer whose message to set.
2443 * @param hello Hello message.
2446 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2448 struct GNUNET_HELLO_Message *old;
2451 GCC_check_connections ();
2452 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2456 old = GCP_get_hello (peer);
2459 size = GNUNET_HELLO_size (hello);
2460 peer->hello = GNUNET_malloc (size);
2461 memcpy (peer->hello, hello, size);
2465 peer->hello = GNUNET_HELLO_merge (old, hello);
2468 GCC_check_connections ();
2473 * Get the hello message.
2475 * @param peer Peer whose message to get.
2477 * @return Hello message.
2479 struct GNUNET_HELLO_Message *
2480 GCP_get_hello (struct CadetPeer *peer)
2482 struct GNUNET_TIME_Absolute expiration;
2483 struct GNUNET_TIME_Relative remaining;
2485 if (NULL == peer->hello)
2488 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2489 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2490 if (0 == remaining.rel_value_us)
2492 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2493 GNUNET_STRINGS_absolute_time_to_string (expiration));
2494 GNUNET_free (peer->hello);
2502 * Try to connect to a peer on TRANSPORT level.
2504 * @param peer Peer to whom to connect.
2507 GCP_try_connect (struct CadetPeer *peer)
2509 struct GNUNET_HELLO_Message *hello;
2510 struct GNUNET_MessageHeader *mh;
2512 if (NULL == transport_handle)
2514 GCC_check_connections ();
2515 if (GNUNET_YES == GCP_is_neighbor (peer))
2517 hello = GCP_get_hello (peer);
2521 mh = GNUNET_HELLO_get_header (hello);
2522 GNUNET_TRANSPORT_offer_hello (transport_handle,
2526 GCC_check_connections ();
2531 * Notify a peer that a link between two other peers is broken. If any path
2532 * used that link, eliminate it.
2534 * @param peer Peer affected by the change.
2535 * @param peer1 Peer whose link is broken.
2536 * @param peer2 Peer whose link is broken.
2539 GCP_notify_broken_link (struct CadetPeer *peer,
2540 const struct GNUNET_PeerIdentity *peer1,
2541 const struct GNUNET_PeerIdentity *peer2)
2543 struct CadetPeerPath *iter;
2544 struct CadetPeerPath *next;
2549 GCC_check_connections ();
2550 p1 = GNUNET_PEER_search (peer1);
2551 p2 = GNUNET_PEER_search (peer2);
2553 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2554 if (0 == p1 || 0 == p2)
2556 /* We don't even know them */
2560 for (iter = peer->path_head; NULL != iter; iter = next)
2563 for (i = 0; i < iter->length - 1; i++)
2565 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2566 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2571 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2574 path_invalidate (iter);
2578 GCC_check_connections ();
2583 * Count the number of known paths toward the peer.
2585 * @param peer Peer to get path info.
2587 * @return Number of known paths.
2590 GCP_count_paths (const struct CadetPeer *peer)
2592 struct CadetPeerPath *iter;
2595 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2603 * Iterate over the paths to a peer.
2605 * @param peer Peer to get path info.
2606 * @param callback Function to call for every path.
2607 * @param cls Closure for @a callback.
2609 * @return Number of iterated paths.
2612 GCP_iterate_paths (struct CadetPeer *peer,
2613 GCP_path_iterator callback,
2616 struct CadetPeerPath *iter;
2619 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2622 if (GNUNET_YES != callback (cls, peer, iter))
2631 * Iterate all known peers.
2633 * @param iter Iterator.
2634 * @param cls Closure for @c iter.
2637 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2640 GCC_check_connections ();
2641 GNUNET_CONTAINER_multipeermap_iterate (peers,
2644 GCC_check_connections ();
2649 * Get the static string for a peer ID.
2653 * @return Static string for it's ID.
2656 GCP_2s (const struct CadetPeer *peer)
2660 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2664 /* end of gnunet-service-cadet_peer.c */