2 This file is part of GNUnet.
3 Copyright (C) 2013, 2015 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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 while (NULL != peer->queue_head)
790 GCP_queue_destroy (peer->queue_head, GNUNET_YES, GNUNET_NO, 0);
792 GNUNET_free_non_null (peer->hello);
799 * Iterator over peer hash map entries to destroy the peer during in_shutdown.
802 * @param key current key code
803 * @param value value in the hash map
804 * @return #GNUNET_YES if we should continue to iterate,
808 shutdown_peer (void *cls,
809 const struct GNUNET_PeerIdentity *key,
812 struct CadetPeer *p = value;
813 struct CadetTunnel *t = p->tunnel;
814 LOG (GNUNET_ERROR_TYPE_DEBUG, " shutting down %s\n", GCP_2s (p));
825 * Check if peer is searching for a path (either active or delayed search).
827 * @param peer Peer to check
828 * @return #GNUNET_YES if there is a search active.
829 * #GNUNET_NO otherwise.
832 is_searching (const struct CadetPeer *peer)
834 return ( (NULL == peer->search_h) &&
835 (NULL == peer->search_delayed) ) ?
836 GNUNET_NO : GNUNET_YES;
841 * @brief Start a search for a peer.
843 * @param cls Closure (Peer to search for).
846 delayed_search (void *cls)
848 struct CadetPeer *peer = cls;
849 const struct GNUNET_SCHEDULER_TaskContext *tc;
851 peer->search_delayed = NULL;
852 tc = GNUNET_SCHEDULER_get_task_context ();
853 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
855 GCC_check_connections ();
856 GCP_start_search (peer);
857 GCC_check_connections ();
862 * Returns if peer is used (has a tunnel or is neighbor).
864 * @param peer Peer to check.
865 * @return #GNUNET_YES if peer is in use.
868 peer_is_used (struct CadetPeer *peer)
870 struct CadetPeerPath *p;
872 if (NULL != peer->tunnel)
875 for (p = peer->path_head; NULL != p; p = p->next)
885 * Iterator over all the peers to get the oldest timestamp.
887 * @param cls Closure (unsued).
888 * @param key ID of the peer.
889 * @param value Peer_Info of the peer.
892 peer_get_oldest (void *cls,
893 const struct GNUNET_PeerIdentity *key,
896 struct CadetPeer *p = value;
897 struct GNUNET_TIME_Absolute *abs = cls;
899 /* Don't count active peers */
900 if (GNUNET_YES == peer_is_used (p))
903 if (abs->abs_value_us < p->last_contact.abs_value_us)
904 abs->abs_value_us = p->last_contact.abs_value_us;
911 * Iterator over all the peers to remove the oldest entry.
913 * @param cls Closure (unsued).
914 * @param key ID of the peer.
915 * @param value Peer_Info of the peer.
918 peer_timeout (void *cls,
919 const struct GNUNET_PeerIdentity *key,
922 struct CadetPeer *p = value;
923 struct GNUNET_TIME_Absolute *abs = cls;
925 LOG (GNUNET_ERROR_TYPE_WARNING,
926 "peer %s timeout\n", GNUNET_i2s (key));
928 if (p->last_contact.abs_value_us == abs->abs_value_us &&
929 GNUNET_NO == peer_is_used (p))
939 * Delete oldest unused peer.
942 peer_delete_oldest (void)
944 struct GNUNET_TIME_Absolute abs;
946 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
948 GNUNET_CONTAINER_multipeermap_iterate (peers,
951 GNUNET_CONTAINER_multipeermap_iterate (peers,
958 * Choose the best (yet unused) path towards a peer,
959 * considering the tunnel properties.
961 * @param peer The destination peer.
962 * @return Best current known path towards the peer, if any.
964 static struct CadetPeerPath *
965 peer_get_best_path (const struct CadetPeer *peer)
967 struct CadetPeerPath *best_p;
968 struct CadetPeerPath *p;
969 unsigned int best_cost;
972 best_cost = UINT_MAX;
974 for (p = peer->path_head; NULL != p; p = p->next)
976 if (GNUNET_NO == path_is_valid (p))
977 continue; /* Don't use invalid paths. */
978 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
979 continue; /* If path is already in use, skip it. */
981 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
992 * Is this queue element sendable?
994 * - All management traffic is always sendable.
995 * - For payload traffic, check the connection flow control.
997 * @param q Queue element to inspect.
998 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
1001 queue_is_sendable (struct CadetPeerQueue *q)
1003 /* Is PID-independent? */
1006 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1007 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1008 case GNUNET_MESSAGE_TYPE_CADET_KX:
1009 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1010 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1011 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1012 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1015 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1016 case GNUNET_MESSAGE_TYPE_CADET_AX:
1023 return GCC_is_sendable (q->c, q->fwd);
1028 * Get first sendable message.
1030 * @param peer The destination peer.
1032 * @return First transmittable message, if any. Otherwise, NULL.
1034 static struct CadetPeerQueue *
1035 peer_get_first_message (const struct CadetPeer *peer)
1037 struct CadetPeerQueue *q;
1039 for (q = peer->queue_head; NULL != q; q = q->next)
1041 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
1042 if (queue_is_sendable (q))
1051 * Function to process paths received for a new peer addition. The recorded
1052 * paths form the initial tunnel, which can be optimized later.
1053 * Called on each result obtained for the DHT search.
1055 * @param cls Closure (peer towards a path has been found).
1056 * @param path Path created from the DHT query. Will be freed afterwards.
1059 search_handler (void *cls, const struct CadetPeerPath *path)
1061 struct CadetPeer *peer = cls;
1062 unsigned int connection_count;
1064 GCC_check_connections ();
1065 GCP_add_path_to_all (path, GNUNET_NO);
1067 /* Count connections */
1068 connection_count = GCT_count_connections (peer->tunnel);
1070 /* If we already have our minimum (or more) connections, it's enough */
1071 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1073 GCC_check_connections ();
1077 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1079 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1082 GCC_check_connections ();
1087 * Adjust core requested size to accomodate an ACK.
1089 * @param message_size Requested size.
1091 * @return Size enough to fit @c message_size and an ACK.
1094 get_core_size (size_t message_size)
1096 return message_size + sizeof (struct GNUNET_CADET_ACK);
1100 * Test if a message type is connection management traffic
1101 * or regular payload traffic.
1103 * @param type Message type.
1105 * @return #GNUNET_YES if connection management, #GNUNET_NO otherwise.
1108 is_connection_management (uint16_t type)
1110 return type == GNUNET_MESSAGE_TYPE_CADET_ACK ||
1111 type == GNUNET_MESSAGE_TYPE_CADET_POLL;
1116 * Fill a core buffer with the appropriate data for the queued message.
1118 * @param queue Queue element for the message.
1119 * @param buf Core buffer to fill.
1120 * @param size Size remaining in @c buf.
1121 * @param[out] pid In case its an encrypted payload, set payload.
1123 * @return Bytes written to @c buf.
1126 fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
1128 struct CadetConnection *c = queue->c;
1131 switch (queue->type)
1133 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1134 *pid = GCC_get_pid (queue->c, queue->fwd);
1135 LOG (GNUNET_ERROR_TYPE_DEBUG, " otr payload ID %u\n", *pid);
1136 msg_size = send_core_data_raw (queue->cls, size, buf);
1137 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
1139 case GNUNET_MESSAGE_TYPE_CADET_AX:
1140 *pid = GCC_get_pid (queue->c, queue->fwd);
1141 LOG (GNUNET_ERROR_TYPE_DEBUG, " ax payload ID %u\n", *pid);
1142 msg_size = send_core_data_raw (queue->cls, size, buf);
1143 ((struct GNUNET_CADET_AX *) buf)->pid = htonl (*pid);
1145 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1146 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1147 case GNUNET_MESSAGE_TYPE_CADET_KX:
1148 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1149 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1150 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1151 msg_size = send_core_data_raw (queue->cls, size, buf);
1153 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1154 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1155 if (GCC_is_origin (c, GNUNET_YES))
1156 msg_size = send_core_connection_create (c, size, buf);
1158 msg_size = send_core_data_raw (queue->cls, size, buf);
1160 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1161 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1162 if (GCC_is_origin (c, GNUNET_NO) ||
1163 GCC_is_origin (c, GNUNET_YES))
1165 msg_size = send_core_connection_ack (c, size, buf);
1169 msg_size = send_core_data_raw (queue->cls, size, buf);
1172 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1173 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1174 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1175 /* This should be encapsulted */
1181 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1185 GNUNET_assert (size >= msg_size);
1192 * Debug function should NEVER return true in production code, useful to
1193 * simulate losses for testcases.
1195 * @param q Queue handle with info about the message.
1197 * @return #GNUNET_YES or #GNUNET_NO with the decision to drop.
1200 should_I_drop (struct CadetPeerQueue *q)
1202 if (0 == drop_percent)
1205 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1213 * Core callback to write a queued packet to core buffer
1215 * @param cls Closure (peer info).
1216 * @param size Number of bytes available in buf.
1217 * @param buf Where the to write the message.
1219 * @return number of bytes written to buf
1222 queue_send (void *cls, size_t size, void *buf)
1224 struct CadetPeer *peer = cls;
1225 struct CadetConnection *c;
1226 struct CadetPeerQueue *queue;
1227 struct GNUNET_TIME_Relative core_wait_time;
1229 const struct GNUNET_PeerIdentity *dst_id;
1236 GCC_check_connections ();
1237 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1238 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1239 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
1240 GCP_2s (peer), size);
1242 /* Sanity checking */
1243 if (NULL == buf || 0 == size)
1245 LOG (GNUNET_ERROR_TYPE_DEBUG, " not allowed/\n");
1246 if (GNUNET_NO == in_shutdown)
1248 queue = peer_get_first_message (peer);
1251 peer->core_transmit = NULL;
1252 peer->tmt_time.abs_value_us = 0;
1253 GCC_check_connections ();
1256 dst_id = GNUNET_PEER_resolve2 (peer->id);
1257 peer->core_transmit =
1258 GNUNET_CORE_notify_transmit_ready (core_handle,
1259 GNUNET_NO, get_priority (queue),
1260 GNUNET_TIME_UNIT_FOREVER_REL,
1262 get_core_size (queue->size),
1265 peer->tmt_time = GNUNET_TIME_absolute_get ();
1269 peer->core_transmit = NULL;
1270 peer->tmt_time.abs_value_us = 0;
1272 GCC_check_connections ();
1281 peer->core_transmit = NULL;
1282 queue = peer_get_first_message (peer);
1285 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
1286 peer->tmt_time.abs_value_us = 0;
1289 core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1290 wait_s = GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_YES);
1291 if (core_wait_time.rel_value_us >= 1000000)
1293 LOG (GNUNET_ERROR_TYPE_WARNING,
1294 " %s: core wait time %s (> 1 second) for %u bytes\n",
1295 GCP_2s (peer), wait_s, queue->size);
1297 peer->tmt_time.abs_value_us = 0;
1299 /* Copy all possible messages to the core buffer */
1300 while (NULL != queue && rest >= queue->size)
1304 LOG (GNUNET_ERROR_TYPE_DEBUG, " on conn %s %s\n",
1305 GCC_2s (c), GC_f2s(queue->fwd));
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok (%u/%u)\n",
1307 queue->size, total_size, size);
1309 msg_size = fill_buf (queue, (void *) dst, size, &pid);
1311 if (should_I_drop (queue))
1313 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on conn %s %s\n",
1314 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1315 queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
1320 LOG (GNUNET_ERROR_TYPE_INFO,
1321 ">>> %s (%s %4u) on conn %s (%p) %s [%5u], after %s\n",
1322 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1323 queue->payload_id, GCC_2s (c), c,
1324 GC_f2s (queue->fwd), msg_size, wait_s);
1326 total_size += msg_size;
1328 dst = &dst[msg_size];
1331 /* Free queue, but cls was freed by send_core_* in fill_buf. */
1332 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1335 queue = peer_get_first_message (peer);
1338 /* If more data in queue, send next */
1341 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data! (%u)\n", queue->size);
1342 if (NULL == peer->core_transmit)
1344 dst_id = GNUNET_PEER_resolve2 (peer->id);
1345 peer->core_transmit =
1346 GNUNET_CORE_notify_transmit_ready (core_handle,
1347 GNUNET_NO, get_priority (queue),
1348 GNUNET_TIME_UNIT_FOREVER_REL,
1350 get_core_size (queue->size),
1353 peer->tmt_time = GNUNET_TIME_absolute_get ();
1354 queue->start_waiting = GNUNET_TIME_absolute_get ();
1358 LOG (GNUNET_ERROR_TYPE_DEBUG, "* tmt rdy called somewhere else\n");
1360 // GCC_start_poll (); FIXME needed?
1364 // GCC_stop_poll(); FIXME needed?
1367 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", total_size);
1368 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1369 GCC_check_connections ();
1374 /******************************************************************************/
1375 /******************************** API ***********************************/
1376 /******************************************************************************/
1380 * Free a transmission that was already queued with all resources
1381 * associated to the request.
1383 * If connection was marked to be destroyed, and this was the last queued
1384 * message on it, the connection will be free'd as a result.
1386 * @param queue Queue handler to cancel.
1387 * @param clear_cls Is it necessary to free associated cls?
1388 * @param sent Was it really sent? (Could have been canceled)
1389 * @param pid PID, if relevant (was sent and was a payload message).
1391 * @return #GNUNET_YES if connection was destroyed as a result,
1392 * #GNUNET_NO otherwise.
1395 GCP_queue_destroy (struct CadetPeerQueue *queue,
1400 struct CadetPeer *peer;
1401 int connection_destroyed;
1403 GCC_check_connections ();
1405 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1406 if (GNUNET_YES == clear_cls)
1408 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1409 switch (queue->type)
1411 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1412 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1414 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1415 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1416 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1417 case GNUNET_MESSAGE_TYPE_CADET_KX:
1418 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1419 case GNUNET_MESSAGE_TYPE_CADET_AX:
1420 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1421 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1422 GNUNET_free_non_null (queue->cls);
1427 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1428 GC_m2s (queue->type));
1431 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1433 if (!is_connection_management (queue->type))
1438 if (NULL != queue->cont)
1440 struct GNUNET_TIME_Relative wait_time;
1442 wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1443 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1444 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1445 connection_destroyed = queue->cont (queue->cont_cls,
1446 queue->c, sent, queue->type, pid,
1447 queue->fwd, queue->size, wait_time);
1451 connection_destroyed = GNUNET_NO;
1454 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1456 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1457 peer->core_transmit = NULL;
1458 peer->tmt_time.abs_value_us = 0;
1461 GNUNET_free (queue);
1462 GCC_check_connections ();
1463 return connection_destroyed;
1468 * @brief Queue and pass message to core when possible.
1470 * @param peer Peer towards which to queue the message.
1471 * @param cls Closure (@c type dependant). It will be used by queue_send to
1472 * build the message to be sent if not already prebuilt.
1473 * @param type Type of the message.
1474 * @param payload_type Type of the message's payload
1475 * 0 if the message is a retransmission (unknown payload).
1476 * UINT16_MAX if the message does not have payload.
1477 * @param payload_id ID of the payload (MID, ACK #, etc)
1478 * @param size Size of the message.
1479 * @param c Connection this message belongs to (can be NULL).
1480 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1481 * @param cont Continuation to be called once CORE has taken the message.
1482 * @param cont_cls Closure for @c cont.
1484 * @return Handle to cancel the message before it is sent. Once cont is called
1485 * message has been sent and therefore the handle is no longer valid.
1487 struct CadetPeerQueue *
1488 GCP_queue_add (struct CadetPeer *peer,
1491 uint16_t payload_type,
1492 uint32_t payload_id,
1494 struct CadetConnection *c,
1499 struct CadetPeerQueue *q;
1503 GCC_check_connections ();
1504 LOG (GNUNET_ERROR_TYPE_DEBUG,
1505 "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n",
1506 GC_m2s (type), GC_m2s (payload_type), payload_id,
1507 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1509 if (NULL == peer->connections)
1511 /* We are not connected to this peer, ignore request. */
1512 LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
1513 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1519 if (is_connection_management (type))
1523 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1525 call_core = (NULL == c || GNUNET_MESSAGE_TYPE_CADET_KX == type) ?
1526 GNUNET_YES : GCC_is_sendable (c, fwd);
1527 q = GNUNET_new (struct CadetPeerQueue);
1530 q->payload_type = payload_type;
1531 q->payload_id = payload_id;
1537 q->cont_cls = cont_cls;
1540 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1545 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1546 call_core = GNUNET_YES;
1549 q->start_waiting = GNUNET_TIME_absolute_get ();
1550 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1552 LOG (GNUNET_ERROR_TYPE_DEBUG,
1553 "calling core tmt rdy towards %s for %u bytes\n",
1554 GCP_2s (peer), size);
1555 peer->core_transmit =
1556 GNUNET_CORE_notify_transmit_ready (core_handle,
1557 GNUNET_NO, get_priority (q),
1558 GNUNET_TIME_UNIT_FOREVER_REL,
1559 GNUNET_PEER_resolve2 (peer->id),
1560 get_core_size (size),
1562 peer->tmt_time = GNUNET_TIME_absolute_get ();
1564 else if (GNUNET_NO == call_core)
1566 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1572 struct GNUNET_TIME_Relative elapsed;
1573 elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1574 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
1576 GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
1579 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1580 GCC_check_connections ();
1586 * Cancel all queued messages to a peer that belong to a certain connection.
1588 * @param peer Peer towards whom to cancel.
1589 * @param c Connection whose queued messages to cancel. Might be destroyed by
1590 * the sent continuation call.
1593 GCP_queue_cancel (struct CadetPeer *peer,
1594 struct CadetConnection *c)
1596 struct CadetPeerQueue *q;
1597 struct CadetPeerQueue *next;
1598 struct CadetPeerQueue *prev;
1599 int connection_destroyed;
1601 GCC_check_connections ();
1602 connection_destroyed = GNUNET_NO;
1603 for (q = peer->queue_head; NULL != q; q = next)
1608 LOG (GNUNET_ERROR_TYPE_DEBUG,
1609 "GMP queue cancel %s\n",
1611 GNUNET_assert (GNUNET_NO == connection_destroyed);
1612 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1618 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1621 /* Get next from prev, q->next might be already freed:
1622 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1625 next = peer->queue_head;
1635 if ( (NULL == peer->queue_head) &&
1636 (NULL != peer->core_transmit) )
1638 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1639 peer->core_transmit = NULL;
1640 peer->tmt_time.abs_value_us = 0;
1642 GCC_check_connections ();
1647 * Get the first transmittable message for a connection.
1649 * @param peer Neighboring peer.
1650 * @param c Connection.
1652 * @return First transmittable message.
1654 static struct CadetPeerQueue *
1655 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1657 struct CadetPeerQueue *q;
1659 for (q = peer->queue_head; NULL != q; q = q->next)
1663 if (queue_is_sendable (q))
1665 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1668 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1676 * Get the first message for a connection and unqueue it.
1678 * Only tunnel (or higher) level messages are unqueued. Connection specific
1679 * messages are silently destroyed upon encounter.
1681 * @param peer Neighboring peer.
1682 * @param c Connection.
1683 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1686 * @return First message for this connection.
1688 struct GNUNET_MessageHeader *
1689 GCP_connection_pop (struct CadetPeer *peer,
1690 struct CadetConnection *c,
1693 struct CadetPeerQueue *q;
1694 struct CadetPeerQueue *next;
1695 struct GNUNET_MessageHeader *msg;
1698 GCC_check_connections ();
1699 GNUNET_assert (NULL != destroyed);
1700 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on conn %p\n", c);
1701 for (q = peer->queue_head; NULL != q; q = next)
1706 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), cont: %p\n",
1707 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1711 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1712 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1713 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1714 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1715 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1716 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1717 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1718 if (GNUNET_YES == dest)
1720 GNUNET_break (GNUNET_NO == *destroyed);
1721 *destroyed = GNUNET_YES;
1725 case GNUNET_MESSAGE_TYPE_CADET_KX:
1726 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1727 case GNUNET_MESSAGE_TYPE_CADET_AX:
1728 case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
1729 msg = (struct GNUNET_MessageHeader *) q->cls;
1730 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1731 if (GNUNET_YES == dest)
1733 GNUNET_break (GNUNET_NO == *destroyed);
1734 *destroyed = GNUNET_YES;
1740 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1743 GCC_check_connections ();
1749 * Unlock a possibly locked queue for a connection.
1751 * If there is a message that can be sent on this connection, call core for it.
1752 * Otherwise (if core transmit is already called or there is no sendable
1753 * message) do nothing.
1755 * @param peer Peer who keeps the queue.
1756 * @param c Connection whose messages to unlock.
1759 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1761 struct CadetPeerQueue *q;
1764 GCC_check_connections ();
1765 if (NULL != peer->core_transmit)
1767 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1768 return; /* Already unlocked */
1771 q = connection_get_first_message (peer, c);
1774 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1775 return; /* Nothing to transmit */
1779 peer->core_transmit =
1780 GNUNET_CORE_notify_transmit_ready (core_handle,
1781 GNUNET_NO, get_priority (q),
1782 GNUNET_TIME_UNIT_FOREVER_REL,
1783 GNUNET_PEER_resolve2 (peer->id),
1784 get_core_size (size),
1787 peer->tmt_time = GNUNET_TIME_absolute_get ();
1788 GCC_check_connections ();
1793 * Initialize the peer subsystem.
1795 * @param c Configuration.
1798 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1800 LOG (GNUNET_ERROR_TYPE_DEBUG,
1802 in_shutdown = GNUNET_NO;
1803 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1805 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1808 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1809 "CADET", "MAX_PEERS", "USING DEFAULT");
1814 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1821 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1822 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1823 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1824 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1825 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1827 ats_ch = GNUNET_ATS_connectivity_init (c);
1828 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1829 NULL, /* Closure passed to CADET functions */
1830 &core_init, /* Call core_init once connected */
1831 &core_connect, /* Handle connects */
1832 &core_disconnect, /* remove peers on disconnects */
1833 NULL, /* Don't notify about all incoming messages */
1834 GNUNET_NO, /* For header only in notification */
1835 NULL, /* Don't notify about all outbound messages */
1836 GNUNET_NO, /* For header-only out notification */
1837 core_handlers); /* Register these handlers */
1839 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1841 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1842 /* Notify callbacks */
1847 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1848 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1849 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1850 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1851 transport_handle = NULL;
1856 if (NULL == core_handle)
1859 GNUNET_SCHEDULER_shutdown ();
1867 * Shut down the peer subsystem.
1872 LOG (GNUNET_ERROR_TYPE_DEBUG,
1873 "Shutting down peer subsystem\n");
1874 in_shutdown = GNUNET_YES;
1875 GNUNET_CONTAINER_multipeermap_iterate (peers,
1878 if (NULL != core_handle)
1880 GNUNET_CORE_disconnect (core_handle);
1883 if (NULL != transport_handle)
1885 GNUNET_TRANSPORT_disconnect (transport_handle);
1886 transport_handle = NULL;
1890 GNUNET_ATS_connectivity_done (ats_ch);
1893 GNUNET_PEER_change_rc (myid, -1);
1894 GNUNET_CONTAINER_multipeermap_destroy (peers);
1900 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1901 * one and insert it in the appropriate structures if the peer is not known yet.
1903 * @param peer_id Full identity of the peer.
1904 * @param create #GNUNET_YES if a new peer should be created if unknown.
1905 * #GNUNET_NO otherwise.
1907 * @return Existing or newly created peer structure.
1908 * NULL if unknown and not requested @a create
1911 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
1913 struct CadetPeer *peer;
1915 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1918 peer = GNUNET_new (struct CadetPeer);
1919 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1921 peer_delete_oldest ();
1923 GNUNET_assert (GNUNET_OK ==
1924 GNUNET_CONTAINER_multipeermap_put (peers,
1927 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1928 peer->id = GNUNET_PEER_intern (peer_id);
1930 peer->last_contact = GNUNET_TIME_absolute_get ();
1937 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1938 * one and insert it in the appropriate structures if the peer is not known yet.
1940 * @param peer Short identity of the peer.
1941 * @param create #GNUNET_YES if a new peer should be created if unknown.
1942 * #GNUNET_NO otherwise.
1944 * @return Existing or newly created peer structure.
1945 * NULL if unknown and not requested @a create
1948 GCP_get_short (const GNUNET_PEER_Id peer, int create)
1950 return GCP_get (GNUNET_PEER_resolve2 (peer), create);
1955 * Function called once #GNUNET_TRANSPORT_offer_hello() is done.
1956 * Marks the operation as finished.
1958 * @param cls Closure (our `struct CadetPeer`).
1961 hello_offer_done (void *cls)
1963 struct CadetPeer *peer = cls;
1965 peer->hello_offer = NULL;
1970 * Try to establish a new connection to this peer (in its tunnel).
1971 * If the peer doesn't have any path to it yet, try to get one.
1972 * If the peer already has some path, send a CREATE CONNECTION towards it.
1974 * @param peer Peer to connect to.
1977 GCP_connect (struct CadetPeer *peer)
1979 struct CadetTunnel *t;
1980 struct CadetPeerPath *path;
1981 struct CadetConnection *c;
1984 GCC_check_connections ();
1985 LOG (GNUNET_ERROR_TYPE_DEBUG,
1986 "peer_connect towards %s\n",
1988 /* If we have a current hello, try to connect using it. */
1989 GCP_try_connect (peer);
1993 rerun_search = GNUNET_NO;
1995 if (NULL != peer->path_head)
1997 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1998 path = peer_get_best_path (peer);
2004 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
2007 c = GCT_use_path (t, path);
2010 /* This case can happen when the path includes a first hop that is
2011 * not yet known to be connected.
2013 * This happens quite often during testing when running cadet
2014 * under valgrind: core connect notifications come very late
2015 * and the DHT result has already come and created a valid
2016 * path. In this case, the peer->connections
2017 * hashmaps will be NULL and tunnel_use_path will not be able
2018 * to create a connection from that path.
2020 * Re-running the DHT GET should give core time to callback.
2022 * GCT_use_path -> GCC_new -> register_neighbors takes care of
2023 * updating statistics about this issue.
2025 rerun_search = GNUNET_YES;
2029 GCC_send_create (c);
2035 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
2039 if (GNUNET_YES == rerun_search)
2041 struct GNUNET_TIME_Relative delay;
2043 GCP_stop_search (peer);
2044 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
2045 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
2048 GCC_check_connections ();
2052 if (GNUNET_NO == is_searching (peer))
2053 GCP_start_search (peer);
2054 GCC_check_connections ();
2059 * Chech whether there is a direct (core level) connection to peer.
2061 * @param peer Peer to check.
2063 * @return #GNUNET_YES if there is a direct connection.
2066 GCP_is_neighbor (const struct CadetPeer *peer)
2068 struct CadetPeerPath *path;
2070 if (NULL == peer->connections)
2073 for (path = peer->path_head; NULL != path; path = path->next)
2075 if (3 > path->length)
2079 /* Is not a neighbor but connections is not NULL, probably disconnecting */
2086 * Create and initialize a new tunnel towards a peer, in case it has none.
2087 * In case the peer already has a tunnel, nothing is done.
2089 * Does not generate any traffic, just creates the local data structures.
2091 * @param peer Peer towards which to create the tunnel.
2094 GCP_add_tunnel (struct CadetPeer *peer)
2096 GCC_check_connections ();
2097 if (NULL != peer->tunnel)
2099 peer->tunnel = GCT_new (peer);
2100 GCC_check_connections ();
2105 * Add a connection to a neighboring peer.
2107 * Store that the peer is the first hop of the connection in one
2108 * direction and that on peer disconnect the connection must be
2109 * notified and destroyed, for it will no longer be valid.
2111 * @param peer Peer to add connection to.
2112 * @param c Connection to add.
2113 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
2116 GCP_add_connection (struct CadetPeer *peer,
2117 struct CadetConnection *c,
2120 LOG (GNUNET_ERROR_TYPE_DEBUG,
2121 "adding connection %s\n",
2123 LOG (GNUNET_ERROR_TYPE_DEBUG,
2126 GNUNET_assert (NULL != peer->connections);
2127 GNUNET_assert (GNUNET_OK ==
2128 GNUNET_CONTAINER_multihashmap_put (peer->connections,
2131 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2132 LOG (GNUNET_ERROR_TYPE_DEBUG,
2133 "Peer %s has now %u connections.\n",
2135 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2140 * Add the path to the peer and update the path used to reach it in case this
2143 * @param peer Destination peer to add the path to.
2144 * @param path New path to add. Last peer must be @c peer.
2145 * Path will be either used of freed if already known.
2146 * @param trusted Do we trust that this path is real?
2148 * @return path if path was taken, pointer to existing duplicate if exists
2151 struct CadetPeerPath *
2152 GCP_add_path (struct CadetPeer *peer,
2153 struct CadetPeerPath *path,
2156 struct CadetPeerPath *aux;
2160 GCC_check_connections ();
2161 LOG (GNUNET_ERROR_TYPE_DEBUG,
2162 "adding path [%u] to peer %s\n",
2163 path->length, GCP_2s (peer));
2165 if (NULL == peer || NULL == path
2166 || path->peers[path->length - 1] != peer->id)
2169 path_destroy (path);
2173 for (l = 1; l < path->length; l++)
2175 if (path->peers[l] == myid)
2177 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
2178 for (l2 = 0; l2 < path->length - l; l2++)
2180 path->peers[l2] = path->peers[l + l2];
2184 path->peers = GNUNET_realloc (path->peers,
2185 path->length * sizeof (GNUNET_PEER_Id));
2189 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
2191 if (2 >= path->length && GNUNET_NO == trusted)
2193 /* Only allow CORE to tell us about direct paths */
2194 path_destroy (path);
2198 l = path_get_length (path);
2201 path_destroy (path);
2205 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2206 for (aux = peer->path_head; aux != NULL; aux = aux->next)
2208 l2 = path_get_length (aux);
2211 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
2212 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
2213 peer->path_tail, aux, path);
2218 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2220 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
2221 path_destroy (path);
2226 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
2228 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
2231 if (NULL != peer->tunnel
2232 && CONNECTIONS_PER_TUNNEL > GCT_count_connections (peer->tunnel)
2233 && 2 < path->length) /* Direct paths are handled by core_connect */
2237 GCC_check_connections ();
2243 * Add the path to the origin peer and update the path used to reach it in case
2244 * this is the shortest.
2245 * The path is given in peer_info -> destination, therefore we turn the path
2246 * upside down first.
2248 * @param peer Peer to add the path to, being the origin of the path.
2249 * @param path New path to add after being inversed.
2250 * Path will be either used or freed.
2251 * @param trusted Do we trust that this path is real?
2253 * @return path if path was taken, pointer to existing duplicate if exists
2256 struct CadetPeerPath *
2257 GCP_add_path_to_origin (struct CadetPeer *peer,
2258 struct CadetPeerPath *path,
2264 return GCP_add_path (peer, path, trusted);
2269 * Adds a path to the info of all the peers in the path
2271 * @param p Path to process.
2272 * @param confirmed Whether we know if the path works or not.
2275 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
2279 /* TODO: invert and add */
2280 GCC_check_connections ();
2281 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2282 for (i++; i < p->length; i++)
2284 struct CadetPeer *peer;
2285 struct CadetPeerPath *copy;
2287 peer = GCP_get_short (p->peers[i], GNUNET_YES);
2288 copy = path_duplicate (p);
2289 copy->length = i + 1;
2290 GCP_add_path (peer, copy, 3 > p->length ? GNUNET_NO : confirmed);
2292 GCC_check_connections ();
2297 * Remove any path to the peer that has the exact same peers as the one given.
2299 * @param peer Peer to remove the path from.
2300 * @param path Path to remove. Is always destroyed .
2303 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
2305 struct CadetPeerPath *iter;
2306 struct CadetPeerPath *next;
2308 GCC_check_connections ();
2309 GNUNET_assert (myid == path->peers[0]);
2310 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2312 LOG (GNUNET_ERROR_TYPE_INFO, "Removing path %p (%u) from %s\n",
2313 path, path->length, GCP_2s (peer));
2315 for (iter = peer->path_head; NULL != iter; iter = next)
2318 if (0 == path_cmp (path, iter))
2320 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
2322 path_destroy (iter);
2325 path_destroy (path);
2326 GCC_check_connections ();
2331 * Check that we are aware of a connection from a neighboring peer.
2333 * @param peer Peer to the connection is with
2334 * @param c Connection that should be in the map with this peer.
2337 GCP_check_connection (const struct CadetPeer *peer,
2338 const struct CadetConnection *c)
2340 GNUNET_assert (NULL != peer);
2341 GNUNET_assert (NULL != peer->connections);
2343 GNUNET_assert (GNUNET_YES ==
2344 GNUNET_CONTAINER_multihashmap_contains_value (peer->connections,
2351 * Remove a connection from a neighboring peer.
2353 * @param peer Peer to remove connection from.
2354 * @param c Connection to remove.
2357 GCP_remove_connection (struct CadetPeer *peer,
2358 const struct CadetConnection *c)
2360 LOG (GNUNET_ERROR_TYPE_DEBUG,
2361 "Removing connection %s\n",
2363 LOG (GNUNET_ERROR_TYPE_DEBUG,
2366 if ( (NULL == peer) ||
2367 (NULL == peer->connections) )
2369 GNUNET_assert (GNUNET_YES ==
2370 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2373 LOG (GNUNET_ERROR_TYPE_DEBUG,
2374 "Peer %s remains with %u connections.\n",
2376 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2381 * Start the DHT search for new paths towards the peer: we don't have
2382 * enough good connections.
2384 * @param peer Destination peer.
2387 GCP_start_search (struct CadetPeer *peer)
2389 const struct GNUNET_PeerIdentity *id;
2390 struct CadetTunnel *t = peer->tunnel;
2392 GCC_check_connections ();
2393 if (NULL != peer->search_h)
2399 if (NULL != peer->search_delayed)
2400 GCP_stop_search (peer);
2402 id = GNUNET_PEER_resolve2 (peer->id);
2403 peer->search_h = GCD_search (id, &search_handler, peer);
2407 /* Why would we search for a peer with no tunnel towards it? */
2412 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
2413 || 0 == GCT_count_any_connections (t))
2415 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2417 GCC_check_connections ();
2422 * Stop the DHT search for new paths towards the peer: we already have
2423 * enough good connections.
2425 * @param peer Destination peer.
2428 GCP_stop_search (struct CadetPeer *peer)
2430 GCC_check_connections ();
2431 if (NULL != peer->search_h)
2433 GCD_search_stop (peer->search_h);
2434 peer->search_h = NULL;
2436 if (NULL != peer->search_delayed)
2438 GNUNET_SCHEDULER_cancel (peer->search_delayed);
2439 peer->search_delayed = NULL;
2441 GCC_check_connections ();
2446 * Get the Full ID of a peer.
2448 * @param peer Peer to get from.
2450 * @return Full ID of peer.
2452 const struct GNUNET_PeerIdentity *
2453 GCP_get_id (const struct CadetPeer *peer)
2455 return GNUNET_PEER_resolve2 (peer->id);
2460 * Get the Short ID of a peer.
2462 * @param peer Peer to get from.
2464 * @return Short ID of peer.
2467 GCP_get_short_id (const struct CadetPeer *peer)
2476 * If tunnel is NULL and there was a search active, stop it, as it's useless.
2482 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2485 if (NULL == t && GNUNET_YES == is_searching (peer))
2487 GCP_stop_search (peer);
2493 * Get the tunnel towards a peer.
2495 * @param peer Peer to get from.
2497 * @return Tunnel towards peer.
2499 struct CadetTunnel *
2500 GCP_get_tunnel (const struct CadetPeer *peer)
2504 return peer->tunnel;
2509 * Set the hello message.
2511 * @param peer Peer whose message to set.
2512 * @param hello Hello message.
2515 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2517 struct GNUNET_HELLO_Message *old;
2520 GCC_check_connections ();
2521 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2525 old = GCP_get_hello (peer);
2528 size = GNUNET_HELLO_size (hello);
2529 peer->hello = GNUNET_malloc (size);
2530 memcpy (peer->hello, hello, size);
2534 peer->hello = GNUNET_HELLO_merge (old, hello);
2537 GCC_check_connections ();
2542 * Get the hello message.
2544 * @param peer Peer whose message to get.
2546 * @return Hello message.
2548 struct GNUNET_HELLO_Message *
2549 GCP_get_hello (struct CadetPeer *peer)
2551 struct GNUNET_TIME_Absolute expiration;
2552 struct GNUNET_TIME_Relative remaining;
2554 if (NULL == peer->hello)
2557 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2558 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2559 if (0 == remaining.rel_value_us)
2561 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2562 GNUNET_STRINGS_absolute_time_to_string (expiration));
2563 GNUNET_free (peer->hello);
2571 * Try to connect to a peer on TRANSPORT level.
2573 * @param peer Peer to whom to connect.
2576 GCP_try_connect (struct CadetPeer *peer)
2578 struct GNUNET_HELLO_Message *hello;
2579 struct GNUNET_MessageHeader *mh;
2581 if (NULL == transport_handle)
2583 GCC_check_connections ();
2584 if (GNUNET_YES == GCP_is_neighbor (peer))
2586 hello = GCP_get_hello (peer);
2590 mh = GNUNET_HELLO_get_header (hello);
2591 if (NULL != peer->hello_offer)
2593 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
2594 peer->hello_offer = NULL;
2596 peer->hello_offer = GNUNET_TRANSPORT_offer_hello (transport_handle,
2600 if (NULL == peer->connectivity_suggestion)
2601 peer->connectivity_suggestion
2602 = GNUNET_ATS_connectivity_suggest (ats_ch,
2603 GNUNET_PEER_resolve2 (peer->id),
2605 GCC_check_connections ();
2610 * Notify a peer that a link between two other peers is broken. If any path
2611 * used that link, eliminate it.
2613 * @param peer Peer affected by the change.
2614 * @param peer1 Peer whose link is broken.
2615 * @param peer2 Peer whose link is broken.
2618 GCP_notify_broken_link (struct CadetPeer *peer,
2619 const struct GNUNET_PeerIdentity *peer1,
2620 const struct GNUNET_PeerIdentity *peer2)
2622 struct CadetPeerPath *iter;
2623 struct CadetPeerPath *next;
2628 GCC_check_connections ();
2629 p1 = GNUNET_PEER_search (peer1);
2630 p2 = GNUNET_PEER_search (peer2);
2632 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2633 if (0 == p1 || 0 == p2)
2635 /* We don't even know them */
2639 for (iter = peer->path_head; NULL != iter; iter = next)
2642 for (i = 0; i < iter->length - 1; i++)
2644 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2645 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2650 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2653 path_invalidate (iter);
2657 GCC_check_connections ();
2662 * Count the number of known paths toward the peer.
2664 * @param peer Peer to get path info.
2666 * @return Number of known paths.
2669 GCP_count_paths (const struct CadetPeer *peer)
2671 struct CadetPeerPath *iter;
2674 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2682 * Iterate over the paths to a peer.
2684 * @param peer Peer to get path info.
2685 * @param callback Function to call for every path.
2686 * @param cls Closure for @a callback.
2688 * @return Number of iterated paths.
2691 GCP_iterate_paths (struct CadetPeer *peer,
2692 GCP_path_iterator callback,
2695 struct CadetPeerPath *iter;
2698 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2701 if (GNUNET_YES != callback (cls, peer, iter))
2710 * Iterate all known peers.
2712 * @param iter Iterator.
2713 * @param cls Closure for @c iter.
2716 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2719 GCC_check_connections ();
2720 GNUNET_CONTAINER_multipeermap_iterate (peers,
2723 GCC_check_connections ();
2728 * Get the static string for a peer ID.
2732 * @return Static string for it's ID.
2735 GCP_2s (const struct CadetPeer *peer)
2739 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2743 /* end of gnunet-service-cadet_peer.c */