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_ats_service.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "cadet_protocol.h"
33 #include "gnunet-service-cadet_peer.h"
34 #include "gnunet-service-cadet_dht.h"
35 #include "gnunet-service-cadet_connection.h"
36 #include "gnunet-service-cadet_tunnel.h"
37 #include "cadet_path.h"
39 #define LOG(level, ...) GNUNET_log_from (level,"cadet-p2p",__VA_ARGS__)
40 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-p2p",__VA_ARGS__)
43 /******************************************************************************/
44 /******************************** STRUCTS **********************************/
45 /******************************************************************************/
48 * Struct containing info about a queued transmission to this peer
55 struct CadetPeerQueue *next;
60 struct CadetPeerQueue *prev;
63 * Peer this transmission is directed to.
65 struct CadetPeer *peer;
68 * Connection this message belongs to.
70 struct CadetConnection *c;
78 * Pointer to info stucture used as cls.
90 uint16_t payload_type;
103 * Set when this message starts waiting for CORE.
105 struct GNUNET_TIME_Absolute start_waiting;
108 * Function to call on sending.
113 * Closure for callback.
120 * Struct containing all information regarding a given peer
130 * Last time we heard from this peer
132 struct GNUNET_TIME_Absolute last_contact;
135 * Paths to reach the peer, ordered by ascending hop count
137 struct CadetPeerPath *path_head;
140 * Paths to reach the peer, ordered by ascending hop count
142 struct CadetPeerPath *path_tail;
145 * Handle to stop the DHT search for paths to this peer
147 struct GCD_search_handle *search_h;
150 * Handle to stop the DHT search for paths to this peer
152 struct GNUNET_SCHEDULER_Task *search_delayed;
155 * Tunnel to this peer, if any.
157 struct CadetTunnel *tunnel;
160 * Connections that go through this peer; indexed by tid.
162 struct GNUNET_CONTAINER_MultiHashMap *connections;
165 * Handle for queued transmissions
167 struct GNUNET_CORE_TransmitHandle *core_transmit;
172 struct GNUNET_TIME_Absolute tmt_time;
175 * Transmission queue to core DLL head
177 struct CadetPeerQueue *queue_head;
180 * Transmission queue to core DLL tail
182 struct CadetPeerQueue *queue_tail;
185 * How many messages are in the queue to this peer.
187 unsigned int queue_n;
192 struct GNUNET_HELLO_Message* hello;
195 * Handle to us offering the HELLO to the transport.
197 struct GNUNET_TRANSPORT_OfferHelloHandle *hello_offer;
200 * Handle to our ATS request asking ATS to suggest an address
201 * to TRANSPORT for this peer (to establish a direct link).
203 struct GNUNET_ATS_ConnectivitySuggestHandle *connectivity_suggestion;
208 /******************************************************************************/
209 /******************************* GLOBALS ***********************************/
210 /******************************************************************************/
213 * Global handle to the statistics service.
215 extern struct GNUNET_STATISTICS_Handle *stats;
218 * Local peer own ID (full value).
220 extern struct GNUNET_PeerIdentity my_full_id;
223 * Local peer own ID (short)
225 extern GNUNET_PEER_Id myid;
228 * Peers known, indexed by PeerIdentity, values of type `struct CadetPeer`.
230 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
233 * How many peers do we want to remember?
235 static unsigned long long max_peers;
238 * Percentage of messages that will be dropped (for test purposes only).
240 static unsigned long long drop_percent;
243 * Handle to communicate with CORE.
245 static struct GNUNET_CORE_Handle *core_handle;
248 * Handle to communicate with ATS.
250 static struct GNUNET_ATS_ConnectivityHandle *ats_ch;
253 * Handle to try to start new connections.
255 static struct GNUNET_TRANSPORT_Handle *transport_handle;
260 static int in_shutdown;
263 /******************************************************************************/
264 /***************************** DEBUG *********************************/
265 /******************************************************************************/
268 * Log all kinds of info about the queueing status of a peer.
270 * @param p Peer whose queue to show.
271 * @param level Error level to use for logging.
274 queue_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
276 struct GNUNET_TIME_Relative core_wait_time;
277 struct CadetPeerQueue *q;
280 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
282 __FILE__, __FUNCTION__, __LINE__);
286 LOG2 (level, "QQQ Message queue towards %s\n", GCP_2s (p));
287 LOG2 (level, "QQQ queue length: %u\n", p->queue_n);
288 LOG2 (level, "QQQ core tmt rdy: %p\n", p->core_transmit);
289 if (NULL != p->core_transmit)
291 core_wait_time = GNUNET_TIME_absolute_get_duration (p->tmt_time);
292 LOG2 (level, "QQQ core called %s ago\n",
293 GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_NO));
295 for (q = p->queue_head; NULL != q; q = q->next)
297 LOG2 (level, "QQQ - %s %s on %s\n",
298 GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
299 LOG2 (level, "QQQ payload %s, %u\n",
300 GC_m2s (q->payload_type), q->payload_id);
301 LOG2 (level, "QQQ size: %u bytes\n", q->size);
304 LOG2 (level, "QQQ End queue towards %s\n", GCP_2s (p));
309 * Log all kinds of info about a peer.
314 GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
316 struct CadetPeerPath *path;
320 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
322 __FILE__, __FUNCTION__, __LINE__);
328 LOG2 (level, "PPP DEBUG PEER NULL\n");
332 LOG2 (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
333 LOG2 (level, "PPP last contact %s\n",
334 GNUNET_STRINGS_absolute_time_to_string (p->last_contact));
335 for (path = p->path_head; NULL != path; path = path->next)
340 LOG2 (level, "PPP path: %s\n", s);
344 LOG2 (level, "PPP core transmit handle %p\n", p->core_transmit);
345 LOG2 (level, "PPP DHT GET handle %p\n", p->search_h);
347 if (NULL != p->connections)
348 conns += GNUNET_CONTAINER_multihashmap_size (p->connections);
349 LOG2 (level, "PPP # connections over link to peer: %u\n", conns);
350 queue_debug (p, level);
351 LOG2 (level, "PPP DEBUG END\n");
355 /******************************************************************************/
356 /***************************** CORE HELPERS *********************************/
357 /******************************************************************************/
361 * Iterator to notify all connections of a broken link. Mark connections
362 * to destroy after all traffic has been sent.
364 * @param cls Closure (disconnected peer).
365 * @param key Current key code (peer id).
366 * @param value Value in the hash map (connection).
368 * @return #GNUNET_YES to continue to iterate.
371 notify_broken (void *cls,
372 const struct GNUNET_HashCode *key,
375 struct CadetPeer *peer = cls;
376 struct CadetConnection *c = value;
378 LOG (GNUNET_ERROR_TYPE_DEBUG,
379 "Notifying %s due to %s disconnect\n",
380 GCC_2s (c), GCP_2s (peer));
381 GCC_neighbor_disconnected (c, peer);
387 * Remove the direct path to the peer.
389 * @param peer Peer to remove the direct path from.
392 static struct CadetPeerPath *
393 pop_direct_path (struct CadetPeer *peer)
395 struct CadetPeerPath *iter;
397 for (iter = peer->path_head; NULL != iter; iter = iter->next)
399 if (2 >= iter->length)
401 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
409 /******************************************************************************/
410 /***************************** CORE CALLBACKS *********************************/
411 /******************************************************************************/
415 * Method called whenever a given peer connects.
418 * @param peer peer identity this notification is about
421 core_connect (void *cls,
422 const struct GNUNET_PeerIdentity *peer)
424 struct CadetPeer *neighbor;
425 struct CadetPeerPath *path;
428 GCC_check_connections ();
429 GNUNET_snprintf (own_id,
432 GNUNET_i2s (&my_full_id));
433 neighbor = GCP_get (peer, GNUNET_YES);
434 if (myid == neighbor->id)
436 LOG (GNUNET_ERROR_TYPE_INFO,
437 "CONNECTED %s (self)\n",
443 LOG (GNUNET_ERROR_TYPE_INFO,
444 "CONNECTED %s <= %s\n",
448 path->peers[1] = neighbor->id;
449 GNUNET_PEER_change_rc (neighbor->id, 1);
451 path->peers[0] = myid;
452 GNUNET_PEER_change_rc (myid, 1);
453 GCP_add_path (neighbor, path, GNUNET_YES);
455 GNUNET_assert (NULL == neighbor->connections);
456 neighbor->connections = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
457 GNUNET_assert (NULL != neighbor->connections);
459 GNUNET_STATISTICS_update (stats,
464 if ( (NULL != GCP_get_tunnel (neighbor)) &&
465 (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer)) )
466 GCP_connect (neighbor);
467 GCC_check_connections ();
472 * Method called whenever a peer disconnects.
475 * @param peer peer identity this notification is about
478 core_disconnect (void *cls,
479 const struct GNUNET_PeerIdentity *peer)
482 struct CadetPeerPath *direct_path;
485 GCC_check_connections ();
486 strncpy (own_id, GNUNET_i2s (&my_full_id), 16);
488 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
491 GNUNET_break (GNUNET_YES == in_shutdown);
495 LOG (GNUNET_ERROR_TYPE_INFO,
496 "DISCONNECTED %s (self)\n",
499 LOG (GNUNET_ERROR_TYPE_INFO,
500 "DISCONNECTED %s <= %s\n",
501 own_id, GNUNET_i2s (peer));
502 direct_path = pop_direct_path (p);
503 GNUNET_CONTAINER_multihashmap_iterate (p->connections,
506 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
507 p->connections = NULL;
508 if (NULL != p->core_transmit)
510 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
511 p->core_transmit = NULL;
512 p->tmt_time.abs_value_us = 0;
514 GNUNET_STATISTICS_update (stats,
518 path_destroy (direct_path);
519 GCC_check_connections ();
524 * Functions to handle messages from core
526 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
527 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
528 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
529 sizeof (struct GNUNET_CADET_ConnectionACK)},
530 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
531 sizeof (struct GNUNET_CADET_ConnectionBroken)},
532 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
533 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
534 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
535 sizeof (struct GNUNET_CADET_ACK)},
536 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
537 sizeof (struct GNUNET_CADET_Poll)},
538 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
539 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
540 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_AX, 0},
546 * To be called on core init/fail.
548 * @param cls Closure (config)
549 * @param identity the public identity of this peer
552 core_init (void *cls,
553 const struct GNUNET_PeerIdentity *identity)
555 const struct GNUNET_CONFIGURATION_Handle *c = cls;
558 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
559 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
561 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
562 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
563 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
564 GNUNET_CORE_disconnect (core_handle);
565 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
566 NULL, /* Closure passed to CADET functions */
567 &core_init, /* Call core_init once connected */
568 &core_connect, /* Handle connects */
569 &core_disconnect, /* remove peers on disconnects */
570 NULL, /* Don't notify about all incoming messages */
571 GNUNET_NO, /* For header only in notification */
572 NULL, /* Don't notify about all outbound messages */
573 GNUNET_NO, /* For header-only out notification */
574 core_handlers); /* Register these handlers */
583 * Core callback to write a pre-constructed data packet to core buffer
585 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
586 * @param size Number of bytes available in buf.
587 * @param buf Where the to write the message.
589 * @return number of bytes written to buf
592 send_core_data_raw (void *cls, size_t size, void *buf)
594 struct GNUNET_MessageHeader *msg = cls;
597 GNUNET_assert (NULL != msg);
598 total_size = ntohs (msg->size);
600 if (total_size > size)
605 memcpy (buf, msg, total_size);
612 * Function to send a create connection message to a peer.
614 * @param c Connection to create.
615 * @param size number of bytes available in buf
616 * @param buf where the callee should write the message
617 * @return number of bytes written to buf
620 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
622 struct GNUNET_CADET_ConnectionCreate *msg;
623 struct GNUNET_PeerIdentity *peer_ptr;
624 const struct CadetPeerPath *p = GCC_get_path (c);
631 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
633 sizeof (struct GNUNET_CADET_ConnectionCreate) +
634 p->length * sizeof (struct GNUNET_PeerIdentity);
636 if (size < size_needed || NULL == buf)
641 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
642 msg->header.size = htons (size_needed);
643 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
644 msg->cid = *GCC_get_id (c);
646 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
647 for (i = 0; i < p->length; i++)
649 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
652 LOG (GNUNET_ERROR_TYPE_DEBUG,
653 "CONNECTION CREATE (%u bytes long) sent!\n",
660 * Creates a path ack message in buf and frees all unused resources.
662 * @param c Connection to send an ACK on.
663 * @param size number of bytes available in buf
664 * @param buf where the callee should write the message
666 * @return number of bytes written to buf
669 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
671 struct GNUNET_CADET_ConnectionACK *msg = buf;
673 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
674 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
679 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
680 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
681 msg->cid = *GCC_get_id (c);
683 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
684 return sizeof (struct GNUNET_CADET_ConnectionACK);
688 /******************************************************************************/
689 /******************************** STATIC ***********************************/
690 /******************************************************************************/
694 * Get priority for a queued message.
696 * @param q Queued message
698 * @return CORE priority to use.
700 static enum GNUNET_CORE_Priority
701 get_priority (struct CadetPeerQueue *q)
703 enum GNUNET_CORE_Priority low;
704 enum GNUNET_CORE_Priority high;
709 return GNUNET_CORE_PRIO_BACKGROUND;
712 /* Relayed traffic has lower priority, our own traffic has higher */
713 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
715 low = GNUNET_CORE_PRIO_BEST_EFFORT;
716 high = GNUNET_CORE_PRIO_URGENT;
720 low = GNUNET_CORE_PRIO_URGENT;
721 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
724 /* Bulky payload has lower priority, control traffic has higher. */
725 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type
726 || GNUNET_MESSAGE_TYPE_CADET_AX == q->type)
734 * Destroy the peer_info and free any allocated resources linked to it
736 * @param peer The peer_info to destroy.
737 * @return #GNUNET_OK on success
740 peer_destroy (struct CadetPeer *peer)
742 struct GNUNET_PeerIdentity id;
743 struct CadetPeerPath *p;
744 struct CadetPeerPath *nextp;
746 GNUNET_PEER_resolve (peer->id, &id);
747 GNUNET_PEER_change_rc (peer->id, -1);
749 LOG (GNUNET_ERROR_TYPE_INFO,
750 "destroying peer %s\n",
753 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
756 LOG (GNUNET_ERROR_TYPE_WARNING, " peer not in peermap!!\n");
758 GCP_stop_search (peer);
763 GNUNET_CONTAINER_DLL_remove (peer->path_head,
769 if (NULL != peer->tunnel)
770 GCT_destroy_empty (peer->tunnel);
771 if (NULL != peer->connections)
773 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (peer->connections));
774 GNUNET_CONTAINER_multihashmap_destroy (peer->connections);
776 if (NULL != peer->core_transmit)
777 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
778 if (NULL != peer->hello_offer)
780 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
781 peer->hello_offer = NULL;
783 if (NULL != peer->connectivity_suggestion)
785 GNUNET_ATS_connectivity_suggest_cancel (peer->connectivity_suggestion);
786 peer->connectivity_suggestion = NULL;
788 GNUNET_free_non_null (peer->hello);
795 * Iterator over peer hash map entries to destroy the peer during in_shutdown.
798 * @param key current key code
799 * @param value value in the hash map
800 * @return #GNUNET_YES if we should continue to iterate,
804 shutdown_peer (void *cls,
805 const struct GNUNET_PeerIdentity *key,
808 struct CadetPeer *p = value;
809 struct CadetTunnel *t = p->tunnel;
810 LOG (GNUNET_ERROR_TYPE_DEBUG, " shutting down %s\n", GCP_2s (p));
821 * Check if peer is searching for a path (either active or delayed search).
823 * @param peer Peer to check
824 * @return #GNUNET_YES if there is a search active.
825 * #GNUNET_NO otherwise.
828 is_searching (const struct CadetPeer *peer)
830 return ( (NULL == peer->search_h) &&
831 (NULL == peer->search_delayed) ) ?
832 GNUNET_NO : GNUNET_YES;
837 * @brief Start a search for a peer.
839 * @param cls Closure (Peer to search for).
840 * @param tc Task context.
843 delayed_search (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
845 struct CadetPeer *peer = cls;
847 peer->search_delayed = NULL;
848 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
850 GCC_check_connections ();
851 GCP_start_search (peer);
852 GCC_check_connections ();
857 * Returns if peer is used (has a tunnel or is neighbor).
859 * @param peer Peer to check.
860 * @return #GNUNET_YES if peer is in use.
863 peer_is_used (struct CadetPeer *peer)
865 struct CadetPeerPath *p;
867 if (NULL != peer->tunnel)
870 for (p = peer->path_head; NULL != p; p = p->next)
880 * Iterator over all the peers to get the oldest timestamp.
882 * @param cls Closure (unsued).
883 * @param key ID of the peer.
884 * @param value Peer_Info of the peer.
887 peer_get_oldest (void *cls,
888 const struct GNUNET_PeerIdentity *key,
891 struct CadetPeer *p = value;
892 struct GNUNET_TIME_Absolute *abs = cls;
894 /* Don't count active peers */
895 if (GNUNET_YES == peer_is_used (p))
898 if (abs->abs_value_us < p->last_contact.abs_value_us)
899 abs->abs_value_us = p->last_contact.abs_value_us;
906 * Iterator over all the peers to remove the oldest entry.
908 * @param cls Closure (unsued).
909 * @param key ID of the peer.
910 * @param value Peer_Info of the peer.
913 peer_timeout (void *cls,
914 const struct GNUNET_PeerIdentity *key,
917 struct CadetPeer *p = value;
918 struct GNUNET_TIME_Absolute *abs = cls;
920 LOG (GNUNET_ERROR_TYPE_WARNING,
921 "peer %s timeout\n", GNUNET_i2s (key));
923 if (p->last_contact.abs_value_us == abs->abs_value_us &&
924 GNUNET_NO == peer_is_used (p))
934 * Delete oldest unused peer.
937 peer_delete_oldest (void)
939 struct GNUNET_TIME_Absolute abs;
941 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
943 GNUNET_CONTAINER_multipeermap_iterate (peers,
946 GNUNET_CONTAINER_multipeermap_iterate (peers,
953 * Choose the best (yet unused) path towards a peer,
954 * considering the tunnel properties.
956 * @param peer The destination peer.
957 * @return Best current known path towards the peer, if any.
959 static struct CadetPeerPath *
960 peer_get_best_path (const struct CadetPeer *peer)
962 struct CadetPeerPath *best_p;
963 struct CadetPeerPath *p;
964 unsigned int best_cost;
967 best_cost = UINT_MAX;
969 for (p = peer->path_head; NULL != p; p = p->next)
971 if (GNUNET_NO == path_is_valid (p))
972 continue; /* Don't use invalid paths. */
973 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
974 continue; /* If path is already in use, skip it. */
976 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
987 * Is this queue element sendable?
989 * - All management traffic is always sendable.
990 * - For payload traffic, check the connection flow control.
992 * @param q Queue element to inspect.
993 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
996 queue_is_sendable (struct CadetPeerQueue *q)
998 /* Is PID-independent? */
1001 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1002 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1003 case GNUNET_MESSAGE_TYPE_CADET_KX:
1004 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1005 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1006 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1007 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1010 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1011 case GNUNET_MESSAGE_TYPE_CADET_AX:
1018 return GCC_is_sendable (q->c, q->fwd);
1023 * Get first sendable message.
1025 * @param peer The destination peer.
1027 * @return First transmittable message, if any. Otherwise, NULL.
1029 static struct CadetPeerQueue *
1030 peer_get_first_message (const struct CadetPeer *peer)
1032 struct CadetPeerQueue *q;
1034 for (q = peer->queue_head; NULL != q; q = q->next)
1036 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
1037 if (queue_is_sendable (q))
1046 * Function to process paths received for a new peer addition. The recorded
1047 * paths form the initial tunnel, which can be optimized later.
1048 * Called on each result obtained for the DHT search.
1050 * @param cls Closure (peer towards a path has been found).
1051 * @param path Path created from the DHT query. Will be freed afterwards.
1054 search_handler (void *cls, const struct CadetPeerPath *path)
1056 struct CadetPeer *peer = cls;
1057 unsigned int connection_count;
1059 GCC_check_connections ();
1060 GCP_add_path_to_all (path, GNUNET_NO);
1062 /* Count connections */
1063 connection_count = GCT_count_connections (peer->tunnel);
1065 /* If we already have our minimum (or more) connections, it's enough */
1066 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1068 GCC_check_connections ();
1072 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1074 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1077 GCC_check_connections ();
1082 * Adjust core requested size to accomodate an ACK.
1084 * @param message_size Requested size.
1086 * @return Size enough to fit @c message_size and an ACK.
1089 get_core_size (size_t message_size)
1091 return message_size + sizeof (struct GNUNET_CADET_ACK);
1096 * Fill a core buffer with the appropriate data for the queued message.
1098 * @param queue Queue element for the message.
1099 * @param buf Core buffer to fill.
1100 * @param size Size remaining in @c buf.
1101 * @param[out] pid In case its an encrypted payload, set payload.
1103 * @return Bytes written to @c buf.
1106 fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
1108 struct CadetConnection *c = queue->c;
1111 switch (queue->type)
1113 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1114 *pid = GCC_get_pid (queue->c, queue->fwd);
1115 LOG (GNUNET_ERROR_TYPE_DEBUG, " otr payload ID %u\n", *pid);
1116 msg_size = send_core_data_raw (queue->cls, size, buf);
1117 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
1119 case GNUNET_MESSAGE_TYPE_CADET_AX:
1120 *pid = GCC_get_pid (queue->c, queue->fwd);
1121 LOG (GNUNET_ERROR_TYPE_DEBUG, " ax payload ID %u\n", *pid);
1122 msg_size = send_core_data_raw (queue->cls, size, buf);
1123 ((struct GNUNET_CADET_AX *) buf)->pid = htonl (*pid);
1125 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1126 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1127 case GNUNET_MESSAGE_TYPE_CADET_KX:
1128 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1129 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1130 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1131 msg_size = send_core_data_raw (queue->cls, size, buf);
1133 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1134 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1135 if (GCC_is_origin (c, GNUNET_YES))
1136 msg_size = send_core_connection_create (c, size, buf);
1138 msg_size = send_core_data_raw (queue->cls, size, buf);
1140 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1141 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1142 if (GCC_is_origin (c, GNUNET_NO) ||
1143 GCC_is_origin (c, GNUNET_YES))
1145 msg_size = send_core_connection_ack (c, size, buf);
1149 msg_size = send_core_data_raw (queue->cls, size, buf);
1152 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1153 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1154 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1155 /* This should be encapsulted */
1161 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1165 GNUNET_assert (size >= msg_size);
1172 * Debug function should NEVER return true in production code, useful to
1173 * simulate losses for testcases.
1175 * @param q Queue handle with info about the message.
1177 * @return #GNUNET_YES or #GNUNET_NO with the decision to drop.
1180 should_I_drop (struct CadetPeerQueue *q)
1182 if (0 == drop_percent)
1185 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1193 * Core callback to write a queued packet to core buffer
1195 * @param cls Closure (peer info).
1196 * @param size Number of bytes available in buf.
1197 * @param buf Where the to write the message.
1199 * @return number of bytes written to buf
1202 queue_send (void *cls, size_t size, void *buf)
1204 struct CadetPeer *peer = cls;
1205 struct CadetConnection *c;
1206 struct CadetPeerQueue *queue;
1207 struct GNUNET_TIME_Relative core_wait_time;
1209 const struct GNUNET_PeerIdentity *dst_id;
1216 GCC_check_connections ();
1217 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1218 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1219 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
1220 GCP_2s (peer), size);
1222 /* Sanity checking */
1223 if (NULL == buf || 0 == size)
1225 LOG (GNUNET_ERROR_TYPE_DEBUG, " not allowed/\n");
1226 if (GNUNET_NO == in_shutdown)
1228 queue = peer_get_first_message (peer);
1231 peer->core_transmit = NULL;
1232 peer->tmt_time.abs_value_us = 0;
1233 GCC_check_connections ();
1236 dst_id = GNUNET_PEER_resolve2 (peer->id);
1237 peer->core_transmit =
1238 GNUNET_CORE_notify_transmit_ready (core_handle,
1239 GNUNET_NO, get_priority (queue),
1240 GNUNET_TIME_UNIT_FOREVER_REL,
1242 get_core_size (queue->size),
1245 peer->tmt_time = GNUNET_TIME_absolute_get ();
1249 peer->core_transmit = NULL;
1250 peer->tmt_time.abs_value_us = 0;
1252 GCC_check_connections ();
1261 peer->core_transmit = NULL;
1262 queue = peer_get_first_message (peer);
1265 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
1266 peer->tmt_time.abs_value_us = 0;
1269 core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1270 wait_s = GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_YES);
1271 if (core_wait_time.rel_value_us >= 1000000)
1273 LOG (GNUNET_ERROR_TYPE_WARNING,
1274 " %s: core wait time %s (> 1 second) for %u bytes\n",
1275 GCP_2s (peer), wait_s, queue->size);
1277 peer->tmt_time.abs_value_us = 0;
1279 /* Copy all possible messages to the core buffer */
1280 while (NULL != queue && rest >= queue->size)
1284 LOG (GNUNET_ERROR_TYPE_DEBUG, " on conn %s %s\n",
1285 GCC_2s (c), GC_f2s(queue->fwd));
1286 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok (%u/%u)\n",
1287 queue->size, total_size, size);
1289 msg_size = fill_buf (queue, (void *) dst, size, &pid);
1291 if (should_I_drop (queue))
1293 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on conn %s %s\n",
1294 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1295 queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
1300 LOG (GNUNET_ERROR_TYPE_INFO,
1301 ">>> %s (%s %4u) on conn %s (%p) %s [%5u], after %s\n",
1302 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1303 queue->payload_id, GCC_2s (c), c,
1304 GC_f2s (queue->fwd), msg_size, wait_s);
1306 total_size += msg_size;
1308 dst = &dst[msg_size];
1311 /* Free queue, but cls was freed by send_core_* in fill_buf. */
1312 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1315 queue = peer_get_first_message (peer);
1318 /* If more data in queue, send next */
1321 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data! (%u)\n", queue->size);
1322 if (NULL == peer->core_transmit)
1324 dst_id = GNUNET_PEER_resolve2 (peer->id);
1325 peer->core_transmit =
1326 GNUNET_CORE_notify_transmit_ready (core_handle,
1327 GNUNET_NO, get_priority (queue),
1328 GNUNET_TIME_UNIT_FOREVER_REL,
1330 get_core_size (queue->size),
1333 peer->tmt_time = GNUNET_TIME_absolute_get ();
1334 queue->start_waiting = GNUNET_TIME_absolute_get ();
1338 LOG (GNUNET_ERROR_TYPE_DEBUG, "* tmt rdy called somewhere else\n");
1340 // GCC_start_poll (); FIXME needed?
1344 // GCC_stop_poll(); FIXME needed?
1347 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", total_size);
1348 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1349 GCC_check_connections ();
1354 /******************************************************************************/
1355 /******************************** API ***********************************/
1356 /******************************************************************************/
1360 * Free a transmission that was already queued with all resources
1361 * associated to the request.
1363 * If connection was marked to be destroyed, and this was the last queued
1364 * message on it, the connection will be free'd as a result.
1366 * @param queue Queue handler to cancel.
1367 * @param clear_cls Is it necessary to free associated cls?
1368 * @param sent Was it really sent? (Could have been canceled)
1369 * @param pid PID, if relevant (was sent and was a payload message).
1371 * @return #GNUNET_YES if connection was destroyed as a result,
1372 * #GNUNET_NO otherwise.
1375 GCP_queue_destroy (struct CadetPeerQueue *queue,
1380 struct CadetPeer *peer;
1381 int connection_destroyed;
1383 GCC_check_connections ();
1385 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1386 if (GNUNET_YES == clear_cls)
1388 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1389 switch (queue->type)
1391 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1392 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1394 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1395 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1396 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1397 case GNUNET_MESSAGE_TYPE_CADET_KX:
1398 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1399 case GNUNET_MESSAGE_TYPE_CADET_AX:
1400 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1401 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1402 GNUNET_free_non_null (queue->cls);
1407 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1408 GC_m2s (queue->type));
1411 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1413 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1414 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1419 if (NULL != queue->cont)
1421 struct GNUNET_TIME_Relative wait_time;
1423 wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1424 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1425 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1426 connection_destroyed = queue->cont (queue->cont_cls,
1427 queue->c, sent, queue->type, pid,
1428 queue->fwd, queue->size, wait_time);
1432 connection_destroyed = GNUNET_NO;
1435 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1437 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1438 peer->core_transmit = NULL;
1439 peer->tmt_time.abs_value_us = 0;
1442 GNUNET_free (queue);
1443 GCC_check_connections ();
1444 return connection_destroyed;
1449 * @brief Queue and pass message to core when possible.
1451 * @param peer Peer towards which to queue the message.
1452 * @param cls Closure (@c type dependant). It will be used by queue_send to
1453 * build the message to be sent if not already prebuilt.
1454 * @param type Type of the message.
1455 * @param payload_type Type of the message's payload
1456 * 0 if the message is a retransmission (unknown payload).
1457 * UINT16_MAX if the message does not have payload.
1458 * @param payload_id ID of the payload (MID, ACK #, etc)
1459 * @param size Size of the message.
1460 * @param c Connection this message belongs to (can be NULL).
1461 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1462 * @param cont Continuation to be called once CORE has taken the message.
1463 * @param cont_cls Closure for @c cont.
1465 * @return Handle to cancel the message before it is sent. Once cont is called
1466 * message has been sent and therefore the handle is no longer valid.
1468 struct CadetPeerQueue *
1469 GCP_queue_add (struct CadetPeer *peer,
1472 uint16_t payload_type,
1473 uint32_t payload_id,
1475 struct CadetConnection *c,
1480 struct CadetPeerQueue *q;
1484 GCC_check_connections ();
1485 LOG (GNUNET_ERROR_TYPE_DEBUG,
1486 "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n",
1487 GC_m2s (type), GC_m2s (payload_type), payload_id,
1488 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1490 if (NULL == peer->connections)
1492 /* We are not connected to this peer, ignore request. */
1493 LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
1494 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1501 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1502 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1507 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1509 call_core = (NULL == c || type == GNUNET_MESSAGE_TYPE_CADET_KX) ?
1510 GNUNET_YES : GCC_is_sendable (c, fwd);
1511 q = GNUNET_new (struct CadetPeerQueue);
1514 q->payload_type = payload_type;
1515 q->payload_id = payload_id;
1521 q->cont_cls = cont_cls;
1524 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1529 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1530 call_core = GNUNET_YES;
1533 q->start_waiting = GNUNET_TIME_absolute_get ();
1534 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1536 LOG (GNUNET_ERROR_TYPE_DEBUG,
1537 "calling core tmt rdy towards %s for %u bytes\n",
1538 GCP_2s (peer), size);
1539 peer->core_transmit =
1540 GNUNET_CORE_notify_transmit_ready (core_handle,
1541 GNUNET_NO, get_priority (q),
1542 GNUNET_TIME_UNIT_FOREVER_REL,
1543 GNUNET_PEER_resolve2 (peer->id),
1544 get_core_size (size),
1546 peer->tmt_time = GNUNET_TIME_absolute_get ();
1548 else if (GNUNET_NO == call_core)
1550 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1556 struct GNUNET_TIME_Relative elapsed;
1557 elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1558 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
1560 GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
1563 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1564 GCC_check_connections ();
1570 * Cancel all queued messages to a peer that belong to a certain connection.
1572 * @param peer Peer towards whom to cancel.
1573 * @param c Connection whose queued messages to cancel. Might be destroyed by
1574 * the sent continuation call.
1577 GCP_queue_cancel (struct CadetPeer *peer,
1578 struct CadetConnection *c)
1580 struct CadetPeerQueue *q;
1581 struct CadetPeerQueue *next;
1582 struct CadetPeerQueue *prev;
1583 int connection_destroyed;
1585 GCC_check_connections ();
1586 connection_destroyed = GNUNET_NO;
1587 for (q = peer->queue_head; NULL != q; q = next)
1592 LOG (GNUNET_ERROR_TYPE_DEBUG,
1593 "GMP queue cancel %s\n",
1595 GNUNET_assert (GNUNET_NO == connection_destroyed);
1596 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1602 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1605 /* Get next from prev, q->next might be already freed:
1606 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1609 next = peer->queue_head;
1619 if ( (NULL == peer->queue_head) &&
1620 (NULL != peer->core_transmit) )
1622 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1623 peer->core_transmit = NULL;
1624 peer->tmt_time.abs_value_us = 0;
1626 GCC_check_connections ();
1631 * Get the first transmittable message for a connection.
1633 * @param peer Neighboring peer.
1634 * @param c Connection.
1636 * @return First transmittable message.
1638 static struct CadetPeerQueue *
1639 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1641 struct CadetPeerQueue *q;
1643 for (q = peer->queue_head; NULL != q; q = q->next)
1647 if (queue_is_sendable (q))
1649 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1652 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1660 * Get the first message for a connection and unqueue it.
1662 * Only tunnel (or higher) level messages are unqueued. Connection specific
1663 * messages are silently destroyed upon encounter.
1665 * @param peer Neighboring peer.
1666 * @param c Connection.
1667 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1670 * @return First message for this connection.
1672 struct GNUNET_MessageHeader *
1673 GCP_connection_pop (struct CadetPeer *peer,
1674 struct CadetConnection *c,
1677 struct CadetPeerQueue *q;
1678 struct CadetPeerQueue *next;
1679 struct GNUNET_MessageHeader *msg;
1682 GCC_check_connections ();
1683 GNUNET_assert (NULL != destroyed);
1684 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on conn %p\n", c);
1685 for (q = peer->queue_head; NULL != q; q = next)
1690 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), cont: %p\n",
1691 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1695 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1696 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1697 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1698 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1699 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1700 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1701 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1702 if (GNUNET_YES == dest)
1704 GNUNET_break (GNUNET_NO == *destroyed);
1705 *destroyed = GNUNET_YES;
1709 case GNUNET_MESSAGE_TYPE_CADET_KX:
1710 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1711 case GNUNET_MESSAGE_TYPE_CADET_AX:
1712 case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
1713 msg = (struct GNUNET_MessageHeader *) q->cls;
1714 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1715 if (GNUNET_YES == dest)
1717 GNUNET_break (GNUNET_NO == *destroyed);
1718 *destroyed = GNUNET_YES;
1724 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1727 GCC_check_connections ();
1733 * Unlock a possibly locked queue for a connection.
1735 * If there is a message that can be sent on this connection, call core for it.
1736 * Otherwise (if core transmit is already called or there is no sendable
1737 * message) do nothing.
1739 * @param peer Peer who keeps the queue.
1740 * @param c Connection whose messages to unlock.
1743 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1745 struct CadetPeerQueue *q;
1748 GCC_check_connections ();
1749 if (NULL != peer->core_transmit)
1751 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1752 return; /* Already unlocked */
1755 q = connection_get_first_message (peer, c);
1758 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1759 return; /* Nothing to transmit */
1763 peer->core_transmit =
1764 GNUNET_CORE_notify_transmit_ready (core_handle,
1765 GNUNET_NO, get_priority (q),
1766 GNUNET_TIME_UNIT_FOREVER_REL,
1767 GNUNET_PEER_resolve2 (peer->id),
1768 get_core_size (size),
1771 peer->tmt_time = GNUNET_TIME_absolute_get ();
1772 GCC_check_connections ();
1777 * Initialize the peer subsystem.
1779 * @param c Configuration.
1782 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1784 LOG (GNUNET_ERROR_TYPE_DEBUG,
1786 in_shutdown = GNUNET_NO;
1787 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1789 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1792 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1793 "CADET", "MAX_PEERS", "USING DEFAULT");
1798 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1805 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1806 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1807 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1808 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1809 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1811 ats_ch = GNUNET_ATS_connectivity_init (c);
1812 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1813 NULL, /* Closure passed to CADET functions */
1814 &core_init, /* Call core_init once connected */
1815 &core_connect, /* Handle connects */
1816 &core_disconnect, /* remove peers on disconnects */
1817 NULL, /* Don't notify about all incoming messages */
1818 GNUNET_NO, /* For header only in notification */
1819 NULL, /* Don't notify about all outbound messages */
1820 GNUNET_NO, /* For header-only out notification */
1821 core_handlers); /* Register these handlers */
1823 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1825 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1826 /* Notify callbacks */
1831 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1832 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1833 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1834 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1835 transport_handle = NULL;
1840 if (NULL == core_handle)
1843 GNUNET_SCHEDULER_shutdown ();
1851 * Shut down the peer subsystem.
1856 LOG (GNUNET_ERROR_TYPE_DEBUG,
1857 "Shutting down peer subsystem\n");
1858 in_shutdown = GNUNET_YES;
1859 GNUNET_CONTAINER_multipeermap_iterate (peers,
1862 if (NULL != core_handle)
1864 GNUNET_CORE_disconnect (core_handle);
1867 if (NULL != transport_handle)
1869 GNUNET_TRANSPORT_disconnect (transport_handle);
1870 transport_handle = NULL;
1874 GNUNET_ATS_connectivity_done (ats_ch);
1877 GNUNET_PEER_change_rc (myid, -1);
1878 GNUNET_CONTAINER_multipeermap_destroy (peers);
1884 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1885 * one and insert it in the appropriate structures if the peer is not known yet.
1887 * @param peer_id Full identity of the peer.
1888 * @param create #GNUNET_YES if a new peer should be created if unknown.
1889 * #GNUNET_NO otherwise.
1891 * @return Existing or newly created peer structure.
1892 * NULL if unknown and not requested @a create
1895 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
1897 struct CadetPeer *peer;
1899 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1902 peer = GNUNET_new (struct CadetPeer);
1903 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1905 peer_delete_oldest ();
1907 GNUNET_assert (GNUNET_OK ==
1908 GNUNET_CONTAINER_multipeermap_put (peers,
1911 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1912 peer->id = GNUNET_PEER_intern (peer_id);
1914 peer->last_contact = GNUNET_TIME_absolute_get ();
1921 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1922 * one and insert it in the appropriate structures if the peer is not known yet.
1924 * @param peer Short identity of the peer.
1925 * @param create #GNUNET_YES if a new peer should be created if unknown.
1926 * #GNUNET_NO otherwise.
1928 * @return Existing or newly created peer structure.
1929 * NULL if unknown and not requested @a create
1932 GCP_get_short (const GNUNET_PEER_Id peer, int create)
1934 return GCP_get (GNUNET_PEER_resolve2 (peer), create);
1939 * Function called once #GNUNET_TRANSPORT_offer_hello() is done.
1940 * Marks the operation as finished.
1942 * @param cls Closure (our `struct CadetPeer`).
1943 * @param tc TaskContext.
1946 hello_offer_done (void *cls,
1947 const struct GNUNET_SCHEDULER_TaskContext *tc)
1949 struct CadetPeer *peer = cls;
1951 peer->hello_offer = NULL;
1956 * Try to establish a new connection to this peer (in its tunnel).
1957 * If the peer doesn't have any path to it yet, try to get one.
1958 * If the peer already has some path, send a CREATE CONNECTION towards it.
1960 * @param peer Peer to connect to.
1963 GCP_connect (struct CadetPeer *peer)
1965 struct CadetTunnel *t;
1966 struct CadetPeerPath *path;
1967 struct CadetConnection *c;
1970 GCC_check_connections ();
1971 LOG (GNUNET_ERROR_TYPE_DEBUG,
1972 "peer_connect towards %s\n",
1974 /* If we have a current hello, try to connect using it. */
1975 GCP_try_connect (peer);
1979 rerun_search = GNUNET_NO;
1981 if (NULL != peer->path_head)
1983 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1984 path = peer_get_best_path (peer);
1990 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1993 c = GCT_use_path (t, path);
1996 /* This case can happen when the path includes a first hop that is
1997 * not yet known to be connected.
1999 * This happens quite often during testing when running cadet
2000 * under valgrind: core connect notifications come very late
2001 * and the DHT result has already come and created a valid
2002 * path. In this case, the peer->connections
2003 * hashmaps will be NULL and tunnel_use_path will not be able
2004 * to create a connection from that path.
2006 * Re-running the DHT GET should give core time to callback.
2008 * GCT_use_path -> GCC_new -> register_neighbors takes care of
2009 * updating statistics about this issue.
2011 rerun_search = GNUNET_YES;
2015 GCC_send_create (c);
2021 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
2025 if (GNUNET_YES == rerun_search)
2027 struct GNUNET_TIME_Relative delay;
2029 GCP_stop_search (peer);
2030 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
2031 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
2034 GCC_check_connections ();
2038 if (GNUNET_NO == is_searching (peer))
2039 GCP_start_search (peer);
2040 GCC_check_connections ();
2045 * Chech whether there is a direct (core level) connection to peer.
2047 * @param peer Peer to check.
2049 * @return #GNUNET_YES if there is a direct connection.
2052 GCP_is_neighbor (const struct CadetPeer *peer)
2054 struct CadetPeerPath *path;
2056 if (NULL == peer->connections)
2059 for (path = peer->path_head; NULL != path; path = path->next)
2061 if (3 > path->length)
2065 /* Is not a neighbor but connections is not NULL, probably disconnecting */
2072 * Create and initialize a new tunnel towards a peer, in case it has none.
2073 * In case the peer already has a tunnel, nothing is done.
2075 * Does not generate any traffic, just creates the local data structures.
2077 * @param peer Peer towards which to create the tunnel.
2080 GCP_add_tunnel (struct CadetPeer *peer)
2082 GCC_check_connections ();
2083 if (NULL != peer->tunnel)
2085 peer->tunnel = GCT_new (peer);
2086 GCC_check_connections ();
2091 * Add a connection to a neighboring peer.
2093 * Store that the peer is the first hop of the connection in one
2094 * direction and that on peer disconnect the connection must be
2095 * notified and destroyed, for it will no longer be valid.
2097 * @param peer Peer to add connection to.
2098 * @param c Connection to add.
2099 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
2102 GCP_add_connection (struct CadetPeer *peer,
2103 struct CadetConnection *c,
2106 LOG (GNUNET_ERROR_TYPE_DEBUG,
2107 "adding connection %s\n",
2109 LOG (GNUNET_ERROR_TYPE_DEBUG,
2112 GNUNET_assert (NULL != peer->connections);
2113 GNUNET_assert (GNUNET_OK ==
2114 GNUNET_CONTAINER_multihashmap_put (peer->connections,
2117 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2118 LOG (GNUNET_ERROR_TYPE_DEBUG,
2119 "Peer %s has now %u connections.\n",
2121 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2126 * Add the path to the peer and update the path used to reach it in case this
2129 * @param peer Destination peer to add the path to.
2130 * @param path New path to add. Last peer must be @c peer.
2131 * Path will be either used of freed if already known.
2132 * @param trusted Do we trust that this path is real?
2134 * @return path if path was taken, pointer to existing duplicate if exists
2137 struct CadetPeerPath *
2138 GCP_add_path (struct CadetPeer *peer,
2139 struct CadetPeerPath *path,
2142 struct CadetPeerPath *aux;
2146 GCC_check_connections ();
2147 LOG (GNUNET_ERROR_TYPE_DEBUG,
2148 "adding path [%u] to peer %s\n",
2149 path->length, GCP_2s (peer));
2151 if (NULL == peer || NULL == path
2152 || path->peers[path->length - 1] != peer->id)
2155 path_destroy (path);
2159 for (l = 1; l < path->length; l++)
2161 if (path->peers[l] == myid)
2163 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
2164 for (l2 = 0; l2 < path->length - l; l2++)
2166 path->peers[l2] = path->peers[l + l2];
2170 path->peers = GNUNET_realloc (path->peers,
2171 path->length * sizeof (GNUNET_PEER_Id));
2175 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
2177 if (2 >= path->length && GNUNET_NO == trusted)
2179 /* Only allow CORE to tell us about direct paths */
2180 path_destroy (path);
2184 l = path_get_length (path);
2187 path_destroy (path);
2191 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2192 for (aux = peer->path_head; aux != NULL; aux = aux->next)
2194 l2 = path_get_length (aux);
2197 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
2198 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
2199 peer->path_tail, aux, path);
2204 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2206 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
2207 path_destroy (path);
2212 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
2214 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
2217 if (NULL != peer->tunnel
2218 && CONNECTIONS_PER_TUNNEL > GCT_count_connections (peer->tunnel)
2219 && 2 < path->length) /* Direct paths are handled by core_connect */
2223 GCC_check_connections ();
2229 * Add the path to the origin peer and update the path used to reach it in case
2230 * this is the shortest.
2231 * The path is given in peer_info -> destination, therefore we turn the path
2232 * upside down first.
2234 * @param peer Peer to add the path to, being the origin of the path.
2235 * @param path New path to add after being inversed.
2236 * Path will be either used or freed.
2237 * @param trusted Do we trust that this path is real?
2239 * @return path if path was taken, pointer to existing duplicate if exists
2242 struct CadetPeerPath *
2243 GCP_add_path_to_origin (struct CadetPeer *peer,
2244 struct CadetPeerPath *path,
2250 return GCP_add_path (peer, path, trusted);
2255 * Adds a path to the info of all the peers in the path
2257 * @param p Path to process.
2258 * @param confirmed Whether we know if the path works or not.
2261 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
2265 /* TODO: invert and add */
2266 GCC_check_connections ();
2267 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2268 for (i++; i < p->length; i++)
2270 struct CadetPeer *peer;
2271 struct CadetPeerPath *copy;
2273 peer = GCP_get_short (p->peers[i], GNUNET_YES);
2274 copy = path_duplicate (p);
2275 copy->length = i + 1;
2276 GCP_add_path (peer, copy, 3 > p->length ? GNUNET_NO : confirmed);
2278 GCC_check_connections ();
2283 * Remove any path to the peer that has the exact same peers as the one given.
2285 * @param peer Peer to remove the path from.
2286 * @param path Path to remove. Is always destroyed .
2289 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
2291 struct CadetPeerPath *iter;
2292 struct CadetPeerPath *next;
2294 GCC_check_connections ();
2295 GNUNET_assert (myid == path->peers[0]);
2296 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2298 LOG (GNUNET_ERROR_TYPE_INFO, "Removing path %p (%u) from %s\n",
2299 path, path->length, GCP_2s (peer));
2301 for (iter = peer->path_head; NULL != iter; iter = next)
2304 if (0 == path_cmp (path, iter))
2306 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
2308 path_destroy (iter);
2311 path_destroy (path);
2312 GCC_check_connections ();
2317 * Check that we are aware of a connection from a neighboring peer.
2319 * @param peer Peer to the connection is with
2320 * @param c Connection that should be in the map with this peer.
2323 GCP_check_connection (const struct CadetPeer *peer,
2324 const struct CadetConnection *c)
2326 GNUNET_assert (NULL != peer);
2327 GNUNET_assert (NULL != peer->connections);
2329 GNUNET_assert (GNUNET_YES ==
2330 GNUNET_CONTAINER_multihashmap_contains_value (peer->connections,
2337 * Remove a connection from a neighboring peer.
2339 * @param peer Peer to remove connection from.
2340 * @param c Connection to remove.
2343 GCP_remove_connection (struct CadetPeer *peer,
2344 const struct CadetConnection *c)
2346 LOG (GNUNET_ERROR_TYPE_DEBUG,
2347 "Removing connection %s\n",
2349 LOG (GNUNET_ERROR_TYPE_DEBUG,
2352 if ( (NULL == peer) ||
2353 (NULL == peer->connections) )
2355 GNUNET_assert (GNUNET_YES ==
2356 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2359 LOG (GNUNET_ERROR_TYPE_DEBUG,
2360 "Peer %s remains with %u connections.\n",
2362 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2367 * Start the DHT search for new paths towards the peer: we don't have
2368 * enough good connections.
2370 * @param peer Destination peer.
2373 GCP_start_search (struct CadetPeer *peer)
2375 const struct GNUNET_PeerIdentity *id;
2376 struct CadetTunnel *t = peer->tunnel;
2378 GCC_check_connections ();
2379 if (NULL != peer->search_h)
2385 if (NULL != peer->search_delayed)
2386 GCP_stop_search (peer);
2388 id = GNUNET_PEER_resolve2 (peer->id);
2389 peer->search_h = GCD_search (id, &search_handler, peer);
2393 /* Why would we search for a peer with no tunnel towards it? */
2398 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
2399 || 0 == GCT_count_any_connections (t))
2401 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2403 GCC_check_connections ();
2408 * Stop the DHT search for new paths towards the peer: we already have
2409 * enough good connections.
2411 * @param peer Destination peer.
2414 GCP_stop_search (struct CadetPeer *peer)
2416 GCC_check_connections ();
2417 if (NULL != peer->search_h)
2419 GCD_search_stop (peer->search_h);
2420 peer->search_h = NULL;
2422 if (NULL != peer->search_delayed)
2424 GNUNET_SCHEDULER_cancel (peer->search_delayed);
2425 peer->search_delayed = NULL;
2427 GCC_check_connections ();
2432 * Get the Full ID of a peer.
2434 * @param peer Peer to get from.
2436 * @return Full ID of peer.
2438 const struct GNUNET_PeerIdentity *
2439 GCP_get_id (const struct CadetPeer *peer)
2441 return GNUNET_PEER_resolve2 (peer->id);
2446 * Get the Short ID of a peer.
2448 * @param peer Peer to get from.
2450 * @return Short ID of peer.
2453 GCP_get_short_id (const struct CadetPeer *peer)
2462 * If tunnel is NULL and there was a search active, stop it, as it's useless.
2468 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2471 if (NULL == t && GNUNET_YES == is_searching (peer))
2473 GCP_stop_search (peer);
2479 * Get the tunnel towards a peer.
2481 * @param peer Peer to get from.
2483 * @return Tunnel towards peer.
2485 struct CadetTunnel *
2486 GCP_get_tunnel (const struct CadetPeer *peer)
2490 return peer->tunnel;
2495 * Set the hello message.
2497 * @param peer Peer whose message to set.
2498 * @param hello Hello message.
2501 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2503 struct GNUNET_HELLO_Message *old;
2506 GCC_check_connections ();
2507 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2511 old = GCP_get_hello (peer);
2514 size = GNUNET_HELLO_size (hello);
2515 peer->hello = GNUNET_malloc (size);
2516 memcpy (peer->hello, hello, size);
2520 peer->hello = GNUNET_HELLO_merge (old, hello);
2523 GCC_check_connections ();
2528 * Get the hello message.
2530 * @param peer Peer whose message to get.
2532 * @return Hello message.
2534 struct GNUNET_HELLO_Message *
2535 GCP_get_hello (struct CadetPeer *peer)
2537 struct GNUNET_TIME_Absolute expiration;
2538 struct GNUNET_TIME_Relative remaining;
2540 if (NULL == peer->hello)
2543 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2544 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2545 if (0 == remaining.rel_value_us)
2547 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2548 GNUNET_STRINGS_absolute_time_to_string (expiration));
2549 GNUNET_free (peer->hello);
2557 * Try to connect to a peer on TRANSPORT level.
2559 * @param peer Peer to whom to connect.
2562 GCP_try_connect (struct CadetPeer *peer)
2564 struct GNUNET_HELLO_Message *hello;
2565 struct GNUNET_MessageHeader *mh;
2567 if (NULL == transport_handle)
2569 GCC_check_connections ();
2570 if (GNUNET_YES == GCP_is_neighbor (peer))
2572 hello = GCP_get_hello (peer);
2576 mh = GNUNET_HELLO_get_header (hello);
2577 if (NULL != peer->hello_offer)
2579 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
2580 peer->hello_offer = NULL;
2582 peer->hello_offer = GNUNET_TRANSPORT_offer_hello (transport_handle,
2586 if (NULL == peer->connectivity_suggestion)
2587 peer->connectivity_suggestion
2588 = GNUNET_ATS_connectivity_suggest (ats_ch,
2589 GNUNET_PEER_resolve2 (peer->id),
2591 GCC_check_connections ();
2596 * Notify a peer that a link between two other peers is broken. If any path
2597 * used that link, eliminate it.
2599 * @param peer Peer affected by the change.
2600 * @param peer1 Peer whose link is broken.
2601 * @param peer2 Peer whose link is broken.
2604 GCP_notify_broken_link (struct CadetPeer *peer,
2605 const struct GNUNET_PeerIdentity *peer1,
2606 const struct GNUNET_PeerIdentity *peer2)
2608 struct CadetPeerPath *iter;
2609 struct CadetPeerPath *next;
2614 GCC_check_connections ();
2615 p1 = GNUNET_PEER_search (peer1);
2616 p2 = GNUNET_PEER_search (peer2);
2618 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2619 if (0 == p1 || 0 == p2)
2621 /* We don't even know them */
2625 for (iter = peer->path_head; NULL != iter; iter = next)
2628 for (i = 0; i < iter->length - 1; i++)
2630 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2631 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2636 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2639 path_invalidate (iter);
2643 GCC_check_connections ();
2648 * Count the number of known paths toward the peer.
2650 * @param peer Peer to get path info.
2652 * @return Number of known paths.
2655 GCP_count_paths (const struct CadetPeer *peer)
2657 struct CadetPeerPath *iter;
2660 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2668 * Iterate over the paths to a peer.
2670 * @param peer Peer to get path info.
2671 * @param callback Function to call for every path.
2672 * @param cls Closure for @a callback.
2674 * @return Number of iterated paths.
2677 GCP_iterate_paths (struct CadetPeer *peer,
2678 GCP_path_iterator callback,
2681 struct CadetPeerPath *iter;
2684 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2687 if (GNUNET_YES != callback (cls, peer, iter))
2696 * Iterate all known peers.
2698 * @param iter Iterator.
2699 * @param cls Closure for @c iter.
2702 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2705 GCC_check_connections ();
2706 GNUNET_CONTAINER_multipeermap_iterate (peers,
2709 GCC_check_connections ();
2714 * Get the static string for a peer ID.
2718 * @return Static string for it's ID.
2721 GCP_2s (const struct CadetPeer *peer)
2725 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2729 /* end of gnunet-service-cadet_peer.c */