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.
391 static struct CadetPeerPath *
392 pop_direct_path (struct CadetPeer *peer)
394 struct CadetPeerPath *iter;
396 for (iter = peer->path_head; NULL != iter; iter = iter->next)
398 if (2 >= iter->length)
400 GNUNET_CONTAINER_DLL_remove (peer->path_head,
410 /******************************************************************************/
411 /***************************** CORE CALLBACKS *********************************/
412 /******************************************************************************/
416 * Method called whenever a given peer connects.
419 * @param peer peer identity this notification is about
422 core_connect (void *cls,
423 const struct GNUNET_PeerIdentity *peer)
425 struct CadetPeer *neighbor;
426 struct CadetPeerPath *path;
429 GCC_check_connections ();
430 GNUNET_snprintf (own_id,
433 GNUNET_i2s (&my_full_id));
434 neighbor = GCP_get (peer, GNUNET_YES);
435 if (myid == neighbor->id)
437 LOG (GNUNET_ERROR_TYPE_INFO,
438 "CONNECTED %s (self)\n",
444 LOG (GNUNET_ERROR_TYPE_INFO,
445 "CONNECTED %s <= %s\n",
449 path->peers[1] = neighbor->id;
450 GNUNET_PEER_change_rc (neighbor->id, 1);
452 path->peers[0] = myid;
453 GNUNET_PEER_change_rc (myid, 1);
454 GCP_add_path (neighbor, path, GNUNET_YES);
456 GNUNET_assert (NULL == neighbor->connections);
457 neighbor->connections = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
458 GNUNET_assert (NULL != neighbor->connections);
460 GNUNET_STATISTICS_update (stats,
465 if ( (NULL != GCP_get_tunnel (neighbor)) &&
466 (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer)) )
467 GCP_connect (neighbor);
468 GCC_check_connections ();
473 * Method called whenever a peer disconnects.
476 * @param peer peer identity this notification is about
479 core_disconnect (void *cls,
480 const struct GNUNET_PeerIdentity *peer)
483 struct CadetPeerPath *direct_path;
486 GCC_check_connections ();
487 strncpy (own_id, GNUNET_i2s (&my_full_id), 16);
489 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
492 GNUNET_break (GNUNET_YES == in_shutdown);
496 LOG (GNUNET_ERROR_TYPE_INFO,
497 "DISCONNECTED %s (self)\n",
500 LOG (GNUNET_ERROR_TYPE_INFO,
501 "DISCONNECTED %s <= %s\n",
502 own_id, GNUNET_i2s (peer));
503 direct_path = pop_direct_path (p);
504 GNUNET_CONTAINER_multihashmap_iterate (p->connections,
507 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
508 p->connections = NULL;
509 if (NULL != p->core_transmit)
511 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
512 p->core_transmit = NULL;
513 p->tmt_time.abs_value_us = 0;
515 GNUNET_STATISTICS_update (stats,
519 path_destroy (direct_path);
520 GCC_check_connections ();
525 * Functions to handle messages from core
527 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
528 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
529 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
530 sizeof (struct GNUNET_CADET_ConnectionACK)},
531 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
532 sizeof (struct GNUNET_CADET_ConnectionBroken)},
533 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
534 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
535 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
536 sizeof (struct GNUNET_CADET_ACK)},
537 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
538 sizeof (struct GNUNET_CADET_Poll)},
539 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
540 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
541 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_AX, 0},
547 * To be called on core init/fail.
549 * @param cls Closure (config)
550 * @param identity the public identity of this peer
553 core_init (void *cls,
554 const struct GNUNET_PeerIdentity *identity)
556 const struct GNUNET_CONFIGURATION_Handle *c = cls;
559 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
560 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
562 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
563 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
564 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
565 GNUNET_CORE_disconnect (core_handle);
566 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
567 NULL, /* Closure passed to CADET functions */
568 &core_init, /* Call core_init once connected */
569 &core_connect, /* Handle connects */
570 &core_disconnect, /* remove peers on disconnects */
571 NULL, /* Don't notify about all incoming messages */
572 GNUNET_NO, /* For header only in notification */
573 NULL, /* Don't notify about all outbound messages */
574 GNUNET_NO, /* For header-only out notification */
575 core_handlers); /* Register these handlers */
584 * Core callback to write a pre-constructed data packet to core buffer
586 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
587 * @param size Number of bytes available in buf.
588 * @param buf Where the to write the message.
590 * @return number of bytes written to buf
593 send_core_data_raw (void *cls, size_t size, void *buf)
595 struct GNUNET_MessageHeader *msg = cls;
598 GNUNET_assert (NULL != msg);
599 total_size = ntohs (msg->size);
601 if (total_size > size)
606 memcpy (buf, msg, total_size);
613 * Function to send a create connection message to a peer.
615 * @param c Connection to create.
616 * @param size number of bytes available in buf
617 * @param buf where the callee should write the message
618 * @return number of bytes written to buf
621 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
623 struct GNUNET_CADET_ConnectionCreate *msg;
624 struct GNUNET_PeerIdentity *peer_ptr;
625 const struct CadetPeerPath *p = GCC_get_path (c);
632 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
634 sizeof (struct GNUNET_CADET_ConnectionCreate) +
635 p->length * sizeof (struct GNUNET_PeerIdentity);
637 if (size < size_needed || NULL == buf)
642 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
643 msg->header.size = htons (size_needed);
644 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
645 msg->cid = *GCC_get_id (c);
647 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
648 for (i = 0; i < p->length; i++)
650 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
653 LOG (GNUNET_ERROR_TYPE_DEBUG,
654 "CONNECTION CREATE (%u bytes long) sent!\n",
661 * Creates a path ack message in buf and frees all unused resources.
663 * @param c Connection to send an ACK on.
664 * @param size number of bytes available in buf
665 * @param buf where the callee should write the message
667 * @return number of bytes written to buf
670 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
672 struct GNUNET_CADET_ConnectionACK *msg = buf;
674 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
675 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
680 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
681 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
682 msg->cid = *GCC_get_id (c);
684 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
685 return sizeof (struct GNUNET_CADET_ConnectionACK);
689 /******************************************************************************/
690 /******************************** STATIC ***********************************/
691 /******************************************************************************/
695 * Get priority for a queued message.
697 * @param q Queued message
699 * @return CORE priority to use.
701 static enum GNUNET_CORE_Priority
702 get_priority (struct CadetPeerQueue *q)
704 enum GNUNET_CORE_Priority low;
705 enum GNUNET_CORE_Priority high;
710 return GNUNET_CORE_PRIO_BACKGROUND;
713 /* Relayed traffic has lower priority, our own traffic has higher */
714 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
716 low = GNUNET_CORE_PRIO_BEST_EFFORT;
717 high = GNUNET_CORE_PRIO_URGENT;
721 low = GNUNET_CORE_PRIO_URGENT;
722 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
725 /* Bulky payload has lower priority, control traffic has higher. */
726 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type
727 || GNUNET_MESSAGE_TYPE_CADET_AX == q->type)
735 * Destroy the peer_info and free any allocated resources linked to it
737 * @param peer The peer_info to destroy.
738 * @return #GNUNET_OK on success
741 peer_destroy (struct CadetPeer *peer)
743 struct GNUNET_PeerIdentity id;
744 struct CadetPeerPath *p;
745 struct CadetPeerPath *nextp;
747 GNUNET_PEER_resolve (peer->id, &id);
748 GNUNET_PEER_change_rc (peer->id, -1);
750 LOG (GNUNET_ERROR_TYPE_INFO,
751 "destroying peer %s\n",
755 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
758 LOG (GNUNET_ERROR_TYPE_WARNING, " peer not in peermap!!\n");
760 GCP_stop_search (peer);
765 GNUNET_CONTAINER_DLL_remove (peer->path_head,
771 if (NULL != peer->tunnel)
772 GCT_destroy_empty (peer->tunnel);
773 if (NULL != peer->connections)
775 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (peer->connections));
776 GNUNET_CONTAINER_multihashmap_destroy (peer->connections);
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 if (NULL != peer->core_transmit)
794 GNUNET_break (0); /* GCP_queue_destroy should've cancelled it! */
795 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
796 peer->core_transmit = NULL;
799 GNUNET_free_non_null (peer->hello);
806 * Iterator over peer hash map entries to destroy the peer during in_shutdown.
809 * @param key current key code
810 * @param value value in the hash map
811 * @return #GNUNET_YES if we should continue to iterate,
815 shutdown_peer (void *cls,
816 const struct GNUNET_PeerIdentity *key,
819 struct CadetPeer *p = value;
820 struct CadetTunnel *t = p->tunnel;
821 LOG (GNUNET_ERROR_TYPE_DEBUG, " shutting down %s\n", GCP_2s (p));
832 * Check if peer is searching for a path (either active or delayed search).
834 * @param peer Peer to check
835 * @return #GNUNET_YES if there is a search active.
836 * #GNUNET_NO otherwise.
839 is_searching (const struct CadetPeer *peer)
841 return ( (NULL == peer->search_h) &&
842 (NULL == peer->search_delayed) ) ?
843 GNUNET_NO : GNUNET_YES;
848 * @brief Start a search for a peer.
850 * @param cls Closure (Peer to search for).
853 delayed_search (void *cls)
855 struct CadetPeer *peer = cls;
857 peer->search_delayed = NULL;
858 GCC_check_connections ();
859 GCP_start_search (peer);
860 GCC_check_connections ();
865 * Returns if peer is used (has a tunnel or is neighbor).
867 * @param peer Peer to check.
868 * @return #GNUNET_YES if peer is in use.
871 peer_is_used (struct CadetPeer *peer)
873 struct CadetPeerPath *p;
875 if (NULL != peer->tunnel)
878 for (p = peer->path_head; NULL != p; p = p->next)
888 * Iterator over all the peers to get the oldest timestamp.
890 * @param cls Closure (unsued).
891 * @param key ID of the peer.
892 * @param value Peer_Info of the peer.
895 peer_get_oldest (void *cls,
896 const struct GNUNET_PeerIdentity *key,
899 struct CadetPeer *p = value;
900 struct GNUNET_TIME_Absolute *abs = cls;
902 /* Don't count active peers */
903 if (GNUNET_YES == peer_is_used (p))
906 if (abs->abs_value_us < p->last_contact.abs_value_us)
907 abs->abs_value_us = p->last_contact.abs_value_us;
914 * Iterator over all the peers to remove the oldest entry.
916 * @param cls Closure (unsued).
917 * @param key ID of the peer.
918 * @param value Peer_Info of the peer.
921 peer_timeout (void *cls,
922 const struct GNUNET_PeerIdentity *key,
925 struct CadetPeer *p = value;
926 struct GNUNET_TIME_Absolute *abs = cls;
928 LOG (GNUNET_ERROR_TYPE_WARNING,
929 "peer %s timeout\n", GNUNET_i2s (key));
931 if (p->last_contact.abs_value_us == abs->abs_value_us &&
932 GNUNET_NO == peer_is_used (p))
942 * Delete oldest unused peer.
945 peer_delete_oldest (void)
947 struct GNUNET_TIME_Absolute abs;
949 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
951 GNUNET_CONTAINER_multipeermap_iterate (peers,
954 GNUNET_CONTAINER_multipeermap_iterate (peers,
961 * Choose the best (yet unused) path towards a peer,
962 * considering the tunnel properties.
964 * @param peer The destination peer.
965 * @return Best current known path towards the peer, if any.
967 static struct CadetPeerPath *
968 peer_get_best_path (const struct CadetPeer *peer)
970 struct CadetPeerPath *best_p;
971 struct CadetPeerPath *p;
972 unsigned int best_cost;
975 best_cost = UINT_MAX;
977 for (p = peer->path_head; NULL != p; p = p->next)
979 if (GNUNET_NO == path_is_valid (p))
980 continue; /* Don't use invalid paths. */
981 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
982 continue; /* If path is already in use, skip it. */
984 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
995 * Is this queue element sendable?
997 * - All management traffic is always sendable.
998 * - For payload traffic, check the connection flow control.
1000 * @param q Queue element to inspect.
1001 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
1004 queue_is_sendable (struct CadetPeerQueue *q)
1006 /* Is PID-independent? */
1009 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1010 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1011 case GNUNET_MESSAGE_TYPE_CADET_KX:
1012 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1013 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1014 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1015 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1018 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1019 case GNUNET_MESSAGE_TYPE_CADET_AX:
1026 return GCC_is_sendable (q->c, q->fwd);
1031 * Get first sendable message.
1033 * @param peer The destination peer.
1035 * @return First transmittable message, if any. Otherwise, NULL.
1037 static struct CadetPeerQueue *
1038 peer_get_first_message (const struct CadetPeer *peer)
1040 struct CadetPeerQueue *q;
1042 for (q = peer->queue_head; NULL != q; q = q->next)
1044 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
1045 if (queue_is_sendable (q))
1054 * Function to process paths received for a new peer addition. The recorded
1055 * paths form the initial tunnel, which can be optimized later.
1056 * Called on each result obtained for the DHT search.
1058 * @param cls Closure (peer towards a path has been found).
1059 * @param path Path created from the DHT query. Will be freed afterwards.
1062 search_handler (void *cls, const struct CadetPeerPath *path)
1064 struct CadetPeer *peer = cls;
1065 unsigned int connection_count;
1067 GCC_check_connections ();
1068 GCP_add_path_to_all (path, GNUNET_NO);
1070 /* Count connections */
1071 connection_count = GCT_count_connections (peer->tunnel);
1073 /* If we already have our minimum (or more) connections, it's enough */
1074 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1076 GCC_check_connections ();
1080 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1082 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1085 GCC_check_connections ();
1090 * Adjust core requested size to accomodate an ACK.
1092 * @param message_size Requested size.
1094 * @return Size enough to fit @c message_size and an ACK.
1097 get_core_size (size_t message_size)
1099 return message_size + sizeof (struct GNUNET_CADET_ACK);
1103 * Test if a message type is connection management traffic
1104 * or regular payload traffic.
1106 * @param type Message type.
1108 * @return #GNUNET_YES if connection management, #GNUNET_NO otherwise.
1111 is_connection_management (uint16_t type)
1113 return type == GNUNET_MESSAGE_TYPE_CADET_ACK ||
1114 type == GNUNET_MESSAGE_TYPE_CADET_POLL;
1119 * Fill a core buffer with the appropriate data for the queued message.
1121 * @param queue Queue element for the message.
1122 * @param buf Core buffer to fill.
1123 * @param size Size remaining in @c buf.
1124 * @param[out] pid In case its an encrypted payload, set payload.
1126 * @return Bytes written to @c buf.
1129 fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
1131 struct CadetConnection *c = queue->c;
1134 switch (queue->type)
1136 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1137 *pid = GCC_get_pid (queue->c, queue->fwd);
1138 LOG (GNUNET_ERROR_TYPE_DEBUG, " otr payload ID %u\n", *pid);
1139 msg_size = send_core_data_raw (queue->cls, size, buf);
1140 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
1142 case GNUNET_MESSAGE_TYPE_CADET_AX:
1143 *pid = GCC_get_pid (queue->c, queue->fwd);
1144 LOG (GNUNET_ERROR_TYPE_DEBUG, " ax payload ID %u\n", *pid);
1145 msg_size = send_core_data_raw (queue->cls, size, buf);
1146 ((struct GNUNET_CADET_AX *) buf)->pid = htonl (*pid);
1148 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1149 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1150 case GNUNET_MESSAGE_TYPE_CADET_KX:
1151 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1152 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1153 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1154 msg_size = send_core_data_raw (queue->cls, size, buf);
1156 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1157 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1158 if (GCC_is_origin (c, GNUNET_YES))
1159 msg_size = send_core_connection_create (c, size, buf);
1161 msg_size = send_core_data_raw (queue->cls, size, buf);
1163 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1164 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1165 if (GCC_is_origin (c, GNUNET_NO) ||
1166 GCC_is_origin (c, GNUNET_YES))
1168 msg_size = send_core_connection_ack (c, size, buf);
1172 msg_size = send_core_data_raw (queue->cls, size, buf);
1175 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1176 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1177 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1178 /* This should be encapsulted */
1184 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1188 GNUNET_assert (size >= msg_size);
1195 * Debug function should NEVER return true in production code, useful to
1196 * simulate losses for testcases.
1198 * @param q Queue handle with info about the message.
1200 * @return #GNUNET_YES or #GNUNET_NO with the decision to drop.
1203 should_I_drop (struct CadetPeerQueue *q)
1205 if (0 == drop_percent)
1208 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1216 * Core callback to write a queued packet to core buffer
1218 * @param cls Closure (peer info).
1219 * @param size Number of bytes available in buf.
1220 * @param buf Where the to write the message.
1222 * @return number of bytes written to buf
1225 queue_send (void *cls, size_t size, void *buf)
1227 struct CadetPeer *peer = cls;
1228 struct CadetConnection *c;
1229 struct CadetPeerQueue *queue;
1230 struct GNUNET_TIME_Relative core_wait_time;
1232 const struct GNUNET_PeerIdentity *dst_id;
1239 GCC_check_connections ();
1240 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1241 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1242 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
1243 GCP_2s (peer), size);
1245 /* Sanity checking */
1246 if (NULL == buf || 0 == size)
1248 LOG (GNUNET_ERROR_TYPE_DEBUG, " not allowed/\n");
1249 if (GNUNET_NO == in_shutdown)
1251 queue = peer_get_first_message (peer);
1254 peer->core_transmit = NULL;
1255 peer->tmt_time.abs_value_us = 0;
1256 GCC_check_connections ();
1259 dst_id = GNUNET_PEER_resolve2 (peer->id);
1260 peer->core_transmit =
1261 GNUNET_CORE_notify_transmit_ready (core_handle,
1262 GNUNET_NO, get_priority (queue),
1263 GNUNET_TIME_UNIT_FOREVER_REL,
1265 get_core_size (queue->size),
1268 peer->tmt_time = GNUNET_TIME_absolute_get ();
1272 peer->core_transmit = NULL;
1273 peer->tmt_time.abs_value_us = 0;
1275 GCC_check_connections ();
1284 peer->core_transmit = NULL;
1285 queue = peer_get_first_message (peer);
1288 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
1289 peer->tmt_time.abs_value_us = 0;
1292 core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1293 wait_s = GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_YES);
1294 if (core_wait_time.rel_value_us >= 1000000)
1296 LOG (GNUNET_ERROR_TYPE_WARNING,
1297 " %s: core wait time %s (> 1 second) for %u bytes\n",
1298 GCP_2s (peer), wait_s, queue->size);
1300 peer->tmt_time.abs_value_us = 0;
1302 /* Copy all possible messages to the core buffer */
1303 while (NULL != queue && rest >= queue->size)
1307 LOG (GNUNET_ERROR_TYPE_DEBUG, " on conn %s %s\n",
1308 GCC_2s (c), GC_f2s(queue->fwd));
1309 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok (%u/%u)\n",
1310 queue->size, total_size, size);
1312 msg_size = fill_buf (queue, (void *) dst, size, &pid);
1314 if (should_I_drop (queue))
1316 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on conn %s %s\n",
1317 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1318 queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
1323 LOG (GNUNET_ERROR_TYPE_INFO,
1324 ">>> %s (%s %4u) on conn %s (%p) %s [%5u], after %s\n",
1325 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1326 queue->payload_id, GCC_2s (c), c,
1327 GC_f2s (queue->fwd), msg_size, wait_s);
1329 total_size += msg_size;
1331 dst = &dst[msg_size];
1334 /* Free queue, but cls was freed by send_core_* in fill_buf. */
1335 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1338 queue = peer_get_first_message (peer);
1341 /* If more data in queue, send next */
1344 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data! (%u)\n", queue->size);
1345 if (NULL == peer->core_transmit)
1347 dst_id = GNUNET_PEER_resolve2 (peer->id);
1348 peer->core_transmit =
1349 GNUNET_CORE_notify_transmit_ready (core_handle,
1350 GNUNET_NO, get_priority (queue),
1351 GNUNET_TIME_UNIT_FOREVER_REL,
1353 get_core_size (queue->size),
1356 peer->tmt_time = GNUNET_TIME_absolute_get ();
1357 queue->start_waiting = GNUNET_TIME_absolute_get ();
1361 LOG (GNUNET_ERROR_TYPE_DEBUG, "* tmt rdy called somewhere else\n");
1363 // GCC_start_poll (); FIXME needed?
1367 // GCC_stop_poll(); FIXME needed?
1370 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", total_size);
1371 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1372 GCC_check_connections ();
1377 /******************************************************************************/
1378 /******************************** API ***********************************/
1379 /******************************************************************************/
1383 * Free a transmission that was already queued with all resources
1384 * associated to the request.
1386 * If connection was marked to be destroyed, and this was the last queued
1387 * message on it, the connection will be free'd as a result.
1389 * @param queue Queue handler to cancel.
1390 * @param clear_cls Is it necessary to free associated cls?
1391 * @param sent Was it really sent? (Could have been canceled)
1392 * @param pid PID, if relevant (was sent and was a payload message).
1394 * @return #GNUNET_YES if connection was destroyed as a result,
1395 * #GNUNET_NO otherwise.
1398 GCP_queue_destroy (struct CadetPeerQueue *queue,
1403 struct CadetPeer *peer;
1404 int connection_destroyed;
1406 GCC_check_connections ();
1408 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1409 if (GNUNET_YES == clear_cls)
1411 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1412 switch (queue->type)
1414 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1415 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1417 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1418 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1419 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1420 case GNUNET_MESSAGE_TYPE_CADET_KX:
1421 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1422 case GNUNET_MESSAGE_TYPE_CADET_AX:
1423 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1424 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1425 GNUNET_free_non_null (queue->cls);
1430 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1431 GC_m2s (queue->type));
1434 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1436 if (!is_connection_management (queue->type))
1441 if (NULL != queue->cont)
1443 struct GNUNET_TIME_Relative wait_time;
1445 wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1446 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1447 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1448 connection_destroyed = queue->cont (queue->cont_cls,
1449 queue->c, sent, queue->type, pid,
1450 queue->fwd, queue->size, wait_time);
1454 connection_destroyed = GNUNET_NO;
1457 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1459 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1460 peer->core_transmit = NULL;
1461 peer->tmt_time.abs_value_us = 0;
1464 GNUNET_free (queue);
1465 GCC_check_connections ();
1466 return connection_destroyed;
1471 * @brief Queue and pass message to core when possible.
1473 * @param peer Peer towards which to queue the message.
1474 * @param cls Closure (@c type dependant). It will be used by queue_send to
1475 * build the message to be sent if not already prebuilt.
1476 * @param type Type of the message.
1477 * @param payload_type Type of the message's payload
1478 * 0 if the message is a retransmission (unknown payload).
1479 * UINT16_MAX if the message does not have payload.
1480 * @param payload_id ID of the payload (MID, ACK #, etc)
1481 * @param size Size of the message.
1482 * @param c Connection this message belongs to (can be NULL).
1483 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1484 * @param cont Continuation to be called once CORE has taken the message.
1485 * @param cont_cls Closure for @c cont.
1487 * @return Handle to cancel the message before it is sent. Once cont is called
1488 * message has been sent and therefore the handle is no longer valid.
1490 struct CadetPeerQueue *
1491 GCP_queue_add (struct CadetPeer *peer,
1494 uint16_t payload_type,
1495 uint32_t payload_id,
1497 struct CadetConnection *c,
1502 struct CadetPeerQueue *q;
1506 GCC_check_connections ();
1507 LOG (GNUNET_ERROR_TYPE_DEBUG,
1508 "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n",
1509 GC_m2s (type), GC_m2s (payload_type), payload_id,
1510 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1512 if (NULL == peer->connections)
1514 /* We are not connected to this peer, ignore request. */
1515 LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
1516 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1522 if (is_connection_management (type))
1526 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1528 call_core = (NULL == c || GNUNET_MESSAGE_TYPE_CADET_KX == type) ?
1529 GNUNET_YES : GCC_is_sendable (c, fwd);
1530 q = GNUNET_new (struct CadetPeerQueue);
1533 q->payload_type = payload_type;
1534 q->payload_id = payload_id;
1540 q->cont_cls = cont_cls;
1543 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1548 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1549 call_core = GNUNET_YES;
1552 q->start_waiting = GNUNET_TIME_absolute_get ();
1553 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1555 LOG (GNUNET_ERROR_TYPE_DEBUG,
1556 "calling core tmt rdy towards %s for %u bytes\n",
1557 GCP_2s (peer), size);
1558 peer->core_transmit =
1559 GNUNET_CORE_notify_transmit_ready (core_handle,
1560 GNUNET_NO, get_priority (q),
1561 GNUNET_TIME_UNIT_FOREVER_REL,
1562 GNUNET_PEER_resolve2 (peer->id),
1563 get_core_size (size),
1565 peer->tmt_time = GNUNET_TIME_absolute_get ();
1567 else if (GNUNET_NO == call_core)
1569 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1575 struct GNUNET_TIME_Relative elapsed;
1576 elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1577 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
1579 GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
1582 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1583 GCC_check_connections ();
1589 * Cancel all queued messages to a peer that belong to a certain connection.
1591 * @param peer Peer towards whom to cancel.
1592 * @param c Connection whose queued messages to cancel. Might be destroyed by
1593 * the sent continuation call.
1596 GCP_queue_cancel (struct CadetPeer *peer,
1597 struct CadetConnection *c)
1599 struct CadetPeerQueue *q;
1600 struct CadetPeerQueue *next;
1601 struct CadetPeerQueue *prev;
1602 int connection_destroyed;
1604 GCC_check_connections ();
1605 connection_destroyed = GNUNET_NO;
1606 for (q = peer->queue_head; NULL != q; q = next)
1611 LOG (GNUNET_ERROR_TYPE_DEBUG,
1612 "GMP queue cancel %s\n",
1614 GNUNET_assert (GNUNET_NO == connection_destroyed);
1615 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1621 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1624 /* Get next from prev, q->next might be already freed:
1625 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1628 next = peer->queue_head;
1638 if ( (NULL == peer->queue_head) &&
1639 (NULL != peer->core_transmit) )
1641 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1642 peer->core_transmit = NULL;
1643 peer->tmt_time.abs_value_us = 0;
1645 GCC_check_connections ();
1650 * Get the first transmittable message for a connection.
1652 * @param peer Neighboring peer.
1653 * @param c Connection.
1655 * @return First transmittable message.
1657 static struct CadetPeerQueue *
1658 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1660 struct CadetPeerQueue *q;
1662 for (q = peer->queue_head; NULL != q; q = q->next)
1666 if (queue_is_sendable (q))
1668 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1671 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1679 * Get the first message for a connection and unqueue it.
1681 * Only tunnel (or higher) level messages are unqueued. Connection specific
1682 * messages are silently destroyed upon encounter.
1684 * @param peer Neighboring peer.
1685 * @param c Connection.
1686 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1689 * @return First message for this connection.
1691 struct GNUNET_MessageHeader *
1692 GCP_connection_pop (struct CadetPeer *peer,
1693 struct CadetConnection *c,
1696 struct CadetPeerQueue *q;
1697 struct CadetPeerQueue *next;
1698 struct GNUNET_MessageHeader *msg;
1701 GCC_check_connections ();
1702 GNUNET_assert (NULL != destroyed);
1703 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on conn %p\n", c);
1704 for (q = peer->queue_head; NULL != q; q = next)
1709 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), cont: %p\n",
1710 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1714 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1715 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1716 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1717 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1718 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1719 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1720 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1721 if (GNUNET_YES == dest)
1723 GNUNET_break (GNUNET_NO == *destroyed);
1724 *destroyed = GNUNET_YES;
1728 case GNUNET_MESSAGE_TYPE_CADET_KX:
1729 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1730 case GNUNET_MESSAGE_TYPE_CADET_AX:
1731 case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
1732 msg = (struct GNUNET_MessageHeader *) q->cls;
1733 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1734 if (GNUNET_YES == dest)
1736 GNUNET_break (GNUNET_NO == *destroyed);
1737 *destroyed = GNUNET_YES;
1743 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1746 GCC_check_connections ();
1752 * Unlock a possibly locked queue for a connection.
1754 * If there is a message that can be sent on this connection, call core for it.
1755 * Otherwise (if core transmit is already called or there is no sendable
1756 * message) do nothing.
1758 * @param peer Peer who keeps the queue.
1759 * @param c Connection whose messages to unlock.
1762 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1764 struct CadetPeerQueue *q;
1767 GCC_check_connections ();
1768 if (NULL != peer->core_transmit)
1770 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1771 return; /* Already unlocked */
1774 q = connection_get_first_message (peer, c);
1777 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1778 return; /* Nothing to transmit */
1782 peer->core_transmit =
1783 GNUNET_CORE_notify_transmit_ready (core_handle,
1784 GNUNET_NO, get_priority (q),
1785 GNUNET_TIME_UNIT_FOREVER_REL,
1786 GNUNET_PEER_resolve2 (peer->id),
1787 get_core_size (size),
1790 peer->tmt_time = GNUNET_TIME_absolute_get ();
1791 GCC_check_connections ();
1796 * Initialize the peer subsystem.
1798 * @param c Configuration.
1801 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1803 LOG (GNUNET_ERROR_TYPE_DEBUG,
1805 in_shutdown = GNUNET_NO;
1806 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1808 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1811 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1812 "CADET", "MAX_PEERS", "USING DEFAULT");
1817 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1824 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1825 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1826 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1827 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1828 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1830 ats_ch = GNUNET_ATS_connectivity_init (c);
1831 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1832 NULL, /* Closure passed to CADET functions */
1833 &core_init, /* Call core_init once connected */
1834 &core_connect, /* Handle connects */
1835 &core_disconnect, /* remove peers on disconnects */
1836 NULL, /* Don't notify about all incoming messages */
1837 GNUNET_NO, /* For header only in notification */
1838 NULL, /* Don't notify about all outbound messages */
1839 GNUNET_NO, /* For header-only out notification */
1840 core_handlers); /* Register these handlers */
1842 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1844 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1845 /* Notify callbacks */
1850 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1851 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1852 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1853 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1854 transport_handle = NULL;
1859 if (NULL == core_handle)
1862 GNUNET_SCHEDULER_shutdown ();
1870 * Shut down the peer subsystem.
1875 LOG (GNUNET_ERROR_TYPE_DEBUG,
1876 "Shutting down peer subsystem\n");
1877 in_shutdown = GNUNET_YES;
1878 GNUNET_CONTAINER_multipeermap_iterate (peers,
1881 if (NULL != core_handle)
1883 GNUNET_CORE_disconnect (core_handle);
1886 if (NULL != transport_handle)
1888 GNUNET_TRANSPORT_disconnect (transport_handle);
1889 transport_handle = NULL;
1893 GNUNET_ATS_connectivity_done (ats_ch);
1896 GNUNET_PEER_change_rc (myid, -1);
1897 GNUNET_CONTAINER_multipeermap_destroy (peers);
1903 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1904 * one and insert it in the appropriate structures if the peer is not known yet.
1906 * @param peer_id Full identity of the peer.
1907 * @param create #GNUNET_YES if a new peer should be created if unknown.
1908 * #GNUNET_NO otherwise.
1910 * @return Existing or newly created peer structure.
1911 * NULL if unknown and not requested @a create
1914 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
1916 struct CadetPeer *peer;
1918 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1921 peer = GNUNET_new (struct CadetPeer);
1922 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1924 peer_delete_oldest ();
1926 GNUNET_assert (GNUNET_OK ==
1927 GNUNET_CONTAINER_multipeermap_put (peers,
1930 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1931 peer->id = GNUNET_PEER_intern (peer_id);
1933 peer->last_contact = GNUNET_TIME_absolute_get ();
1940 * Retrieve the CadetPeer stucture associated with the
1941 * peer. Optionally create one and insert it in the appropriate
1942 * structures if the peer is not known yet.
1944 * @param peer Short identity of the peer.
1945 * @param create #GNUNET_YES if a new peer should be created if unknown.
1946 * #GNUNET_NO otherwise.
1948 * @return Existing or newly created peer structure.
1949 * NULL if unknown and not requested @a create
1952 GCP_get_short (const GNUNET_PEER_Id peer, int create)
1954 return GCP_get (GNUNET_PEER_resolve2 (peer), create);
1959 * Function called once #GNUNET_TRANSPORT_offer_hello() is done.
1960 * Marks the operation as finished.
1962 * @param cls Closure (our `struct CadetPeer`).
1965 hello_offer_done (void *cls)
1967 struct CadetPeer *peer = cls;
1969 peer->hello_offer = NULL;
1974 * Try to establish a new connection to this peer (in its tunnel).
1975 * If the peer doesn't have any path to it yet, try to get one.
1976 * If the peer already has some path, send a CREATE CONNECTION towards it.
1978 * @param peer Peer to connect to.
1981 GCP_connect (struct CadetPeer *peer)
1983 struct CadetTunnel *t;
1984 struct CadetPeerPath *path;
1985 struct CadetConnection *c;
1988 GCC_check_connections ();
1989 LOG (GNUNET_ERROR_TYPE_DEBUG,
1990 "peer_connect towards %s\n",
1992 /* If we have a current hello, try to connect using it. */
1993 GCP_try_connect (peer);
1997 rerun_search = GNUNET_NO;
1999 if (NULL != peer->path_head)
2001 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
2002 path = peer_get_best_path (peer);
2008 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
2011 c = GCT_use_path (t, path);
2014 /* This case can happen when the path includes a first hop that is
2015 * not yet known to be connected.
2017 * This happens quite often during testing when running cadet
2018 * under valgrind: core connect notifications come very late
2019 * and the DHT result has already come and created a valid
2020 * path. In this case, the peer->connections
2021 * hashmaps will be NULL and tunnel_use_path will not be able
2022 * to create a connection from that path.
2024 * Re-running the DHT GET should give core time to callback.
2026 * GCT_use_path -> GCC_new -> register_neighbors takes care of
2027 * updating statistics about this issue.
2029 rerun_search = GNUNET_YES;
2033 GCC_send_create (c);
2039 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
2043 if (GNUNET_YES == rerun_search)
2045 struct GNUNET_TIME_Relative delay;
2047 GCP_stop_search (peer);
2048 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
2049 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
2052 GCC_check_connections ();
2056 if (GNUNET_NO == is_searching (peer))
2057 GCP_start_search (peer);
2058 GCC_check_connections ();
2063 * Chech whether there is a direct (core level) connection to peer.
2065 * @param peer Peer to check.
2067 * @return #GNUNET_YES if there is a direct connection.
2070 GCP_is_neighbor (const struct CadetPeer *peer)
2072 struct CadetPeerPath *path;
2074 if (NULL == peer->connections)
2077 for (path = peer->path_head; NULL != path; path = path->next)
2079 if (3 > path->length)
2083 /* Is not a neighbor but connections is not NULL, probably disconnecting */
2090 * Create and initialize a new tunnel towards a peer, in case it has none.
2091 * In case the peer already has a tunnel, nothing is done.
2093 * Does not generate any traffic, just creates the local data structures.
2095 * @param peer Peer towards which to create the tunnel.
2098 GCP_add_tunnel (struct CadetPeer *peer)
2100 GCC_check_connections ();
2101 if (NULL != peer->tunnel)
2103 peer->tunnel = GCT_new (peer);
2104 GCC_check_connections ();
2109 * Add a connection to a neighboring peer.
2111 * Store that the peer is the first hop of the connection in one
2112 * direction and that on peer disconnect the connection must be
2113 * notified and destroyed, for it will no longer be valid.
2115 * @param peer Peer to add connection to.
2116 * @param c Connection to add.
2117 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
2120 GCP_add_connection (struct CadetPeer *peer,
2121 struct CadetConnection *c,
2124 LOG (GNUNET_ERROR_TYPE_DEBUG,
2125 "adding connection %s\n",
2127 LOG (GNUNET_ERROR_TYPE_DEBUG,
2130 GNUNET_assert (NULL != peer->connections);
2131 GNUNET_assert (GNUNET_OK ==
2132 GNUNET_CONTAINER_multihashmap_put (peer->connections,
2135 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2136 LOG (GNUNET_ERROR_TYPE_DEBUG,
2137 "Peer %s has now %u connections.\n",
2139 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2144 * Add the path to the peer and update the path used to reach it in case this
2147 * @param peer Destination peer to add the path to.
2148 * @param path New path to add. Last peer must be @c peer.
2149 * Path will be either used of freed if already known.
2150 * @param trusted Do we trust that this path is real?
2152 * @return path if path was taken, pointer to existing duplicate if exists
2155 struct CadetPeerPath *
2156 GCP_add_path (struct CadetPeer *peer,
2157 struct CadetPeerPath *path,
2160 struct CadetPeerPath *aux;
2164 GCC_check_connections ();
2165 LOG (GNUNET_ERROR_TYPE_DEBUG,
2166 "adding path [%u] to peer %s\n",
2167 path->length, GCP_2s (peer));
2169 if (NULL == peer || NULL == path
2170 || path->peers[path->length - 1] != peer->id)
2173 path_destroy (path);
2177 for (l = 1; l < path->length; l++)
2179 if (path->peers[l] == myid)
2181 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
2182 for (l2 = 0; l2 < path->length - l; l2++)
2184 path->peers[l2] = path->peers[l + l2];
2188 path->peers = GNUNET_realloc (path->peers,
2189 path->length * sizeof (GNUNET_PEER_Id));
2193 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
2195 if (2 >= path->length && GNUNET_NO == trusted)
2197 /* Only allow CORE to tell us about direct paths */
2198 path_destroy (path);
2202 l = path_get_length (path);
2205 path_destroy (path);
2209 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2210 for (aux = peer->path_head; aux != NULL; aux = aux->next)
2212 l2 = path_get_length (aux);
2215 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
2216 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
2217 peer->path_tail, aux, path);
2222 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2224 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
2225 path_destroy (path);
2230 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head,
2233 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
2236 if (NULL != peer->tunnel
2237 && CONNECTIONS_PER_TUNNEL > GCT_count_connections (peer->tunnel)
2238 && 2 < path->length) /* Direct paths are handled by core_connect */
2242 GCC_check_connections ();
2248 * Add the path to the origin peer and update the path used to reach it in case
2249 * this is the shortest.
2250 * The path is given in peer_info -> destination, therefore we turn the path
2251 * upside down first.
2253 * @param peer Peer to add the path to, being the origin of the path.
2254 * @param path New path to add after being inversed.
2255 * Path will be either used or freed.
2256 * @param trusted Do we trust that this path is real?
2258 * @return path if path was taken, pointer to existing duplicate if exists
2261 struct CadetPeerPath *
2262 GCP_add_path_to_origin (struct CadetPeer *peer,
2263 struct CadetPeerPath *path,
2269 return GCP_add_path (peer, path, trusted);
2274 * Adds a path to the info of all the peers in the path
2276 * @param p Path to process.
2277 * @param confirmed Whether we know if the path works or not.
2280 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
2284 /* TODO: invert and add */
2285 GCC_check_connections ();
2286 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2287 for (i++; i < p->length; i++)
2289 struct CadetPeer *peer;
2290 struct CadetPeerPath *copy;
2292 peer = GCP_get_short (p->peers[i], GNUNET_YES);
2293 copy = path_duplicate (p);
2294 copy->length = i + 1;
2295 GCP_add_path (peer, copy, 3 > p->length ? GNUNET_NO : confirmed);
2297 GCC_check_connections ();
2302 * Remove any path to the peer that has the exact same peers as the one given.
2304 * @param peer Peer to remove the path from.
2305 * @param path Path to remove. Is always destroyed .
2308 GCP_remove_path (struct CadetPeer *peer,
2309 struct CadetPeerPath *path)
2311 struct CadetPeerPath *iter;
2312 struct CadetPeerPath *next;
2314 GCC_check_connections ();
2315 GNUNET_assert (myid == path->peers[0]);
2316 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2318 LOG (GNUNET_ERROR_TYPE_INFO,
2319 "Removing path %p (%u) from %s\n",
2320 path, path->length, GCP_2s (peer));
2322 for (iter = peer->path_head; NULL != iter; iter = next)
2325 if (0 == path_cmp (path, iter))
2327 GNUNET_CONTAINER_DLL_remove (peer->path_head,
2331 path_destroy (iter);
2334 path_destroy (path);
2335 GCC_check_connections ();
2340 * Check that we are aware of a connection from a neighboring peer.
2342 * @param peer Peer to the connection is with
2343 * @param c Connection that should be in the map with this peer.
2346 GCP_check_connection (const struct CadetPeer *peer,
2347 const struct CadetConnection *c)
2349 GNUNET_assert (NULL != peer);
2350 GNUNET_assert (NULL != peer->connections);
2352 GNUNET_assert (GNUNET_YES ==
2353 GNUNET_CONTAINER_multihashmap_contains_value (peer->connections,
2360 * Remove a connection from a neighboring peer.
2362 * @param peer Peer to remove connection from.
2363 * @param c Connection to remove.
2366 GCP_remove_connection (struct CadetPeer *peer,
2367 const struct CadetConnection *c)
2369 LOG (GNUNET_ERROR_TYPE_DEBUG,
2370 "Removing connection %s\n",
2372 LOG (GNUNET_ERROR_TYPE_DEBUG,
2375 if ( (NULL == peer) ||
2376 (NULL == peer->connections) )
2378 GNUNET_assert (GNUNET_YES ==
2379 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2382 LOG (GNUNET_ERROR_TYPE_DEBUG,
2383 "Peer %s remains with %u connections.\n",
2385 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2390 * Start the DHT search for new paths towards the peer: we don't have
2391 * enough good connections.
2393 * @param peer Destination peer.
2396 GCP_start_search (struct CadetPeer *peer)
2398 const struct GNUNET_PeerIdentity *id;
2399 struct CadetTunnel *t = peer->tunnel;
2401 GCC_check_connections ();
2402 if (NULL != peer->search_h)
2408 if (NULL != peer->search_delayed)
2409 GCP_stop_search (peer);
2411 id = GNUNET_PEER_resolve2 (peer->id);
2412 peer->search_h = GCD_search (id, &search_handler, peer);
2416 /* Why would we search for a peer with no tunnel towards it? */
2421 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
2422 || 0 == GCT_count_any_connections (t))
2424 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2426 GCC_check_connections ();
2431 * Stop the DHT search for new paths towards the peer: we already have
2432 * enough good connections.
2434 * @param peer Destination peer.
2437 GCP_stop_search (struct CadetPeer *peer)
2439 GCC_check_connections ();
2440 if (NULL != peer->search_h)
2442 GCD_search_stop (peer->search_h);
2443 peer->search_h = NULL;
2445 if (NULL != peer->search_delayed)
2447 GNUNET_SCHEDULER_cancel (peer->search_delayed);
2448 peer->search_delayed = NULL;
2450 GCC_check_connections ();
2455 * Get the Full ID of a peer.
2457 * @param peer Peer to get from.
2459 * @return Full ID of peer.
2461 const struct GNUNET_PeerIdentity *
2462 GCP_get_id (const struct CadetPeer *peer)
2464 return GNUNET_PEER_resolve2 (peer->id);
2469 * Get the Short ID of a peer.
2471 * @param peer Peer to get from.
2473 * @return Short ID of peer.
2476 GCP_get_short_id (const struct CadetPeer *peer)
2485 * If tunnel is NULL and there was a search active, stop it, as it's useless.
2491 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2494 if (NULL == t && GNUNET_YES == is_searching (peer))
2496 GCP_stop_search (peer);
2502 * Get the tunnel towards a peer.
2504 * @param peer Peer to get from.
2506 * @return Tunnel towards peer.
2508 struct CadetTunnel *
2509 GCP_get_tunnel (const struct CadetPeer *peer)
2513 return peer->tunnel;
2518 * Set the hello message.
2520 * @param peer Peer whose message to set.
2521 * @param hello Hello message.
2524 GCP_set_hello (struct CadetPeer *peer,
2525 const struct GNUNET_HELLO_Message *hello)
2527 struct GNUNET_HELLO_Message *old;
2530 GCC_check_connections ();
2531 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2535 old = GCP_get_hello (peer);
2538 size = GNUNET_HELLO_size (hello);
2539 peer->hello = GNUNET_malloc (size);
2540 memcpy (peer->hello, hello, size);
2544 peer->hello = GNUNET_HELLO_merge (old, hello);
2547 GCC_check_connections ();
2552 * Get the hello message.
2554 * @param peer Peer whose message to get.
2556 * @return Hello message.
2558 struct GNUNET_HELLO_Message *
2559 GCP_get_hello (struct CadetPeer *peer)
2561 struct GNUNET_TIME_Absolute expiration;
2562 struct GNUNET_TIME_Relative remaining;
2564 if (NULL == peer->hello)
2567 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2568 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2569 if (0 == remaining.rel_value_us)
2571 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2572 GNUNET_STRINGS_absolute_time_to_string (expiration));
2573 GNUNET_free (peer->hello);
2581 * Try to connect to a peer on TRANSPORT level.
2583 * @param peer Peer to whom to connect.
2586 GCP_try_connect (struct CadetPeer *peer)
2588 struct GNUNET_HELLO_Message *hello;
2589 struct GNUNET_MessageHeader *mh;
2591 if (NULL == transport_handle)
2593 GCC_check_connections ();
2594 if (GNUNET_YES == GCP_is_neighbor (peer))
2596 hello = GCP_get_hello (peer);
2600 mh = GNUNET_HELLO_get_header (hello);
2601 if (NULL != peer->hello_offer)
2603 GNUNET_TRANSPORT_offer_hello_cancel (peer->hello_offer);
2604 peer->hello_offer = NULL;
2606 peer->hello_offer = GNUNET_TRANSPORT_offer_hello (transport_handle,
2610 if (NULL == peer->connectivity_suggestion)
2611 peer->connectivity_suggestion
2612 = GNUNET_ATS_connectivity_suggest (ats_ch,
2613 GNUNET_PEER_resolve2 (peer->id),
2615 GCC_check_connections ();
2620 * Notify a peer that a link between two other peers is broken. If any path
2621 * used that link, eliminate it.
2623 * @param peer Peer affected by the change.
2624 * @param peer1 Peer whose link is broken.
2625 * @param peer2 Peer whose link is broken.
2628 GCP_notify_broken_link (struct CadetPeer *peer,
2629 const struct GNUNET_PeerIdentity *peer1,
2630 const struct GNUNET_PeerIdentity *peer2)
2632 struct CadetPeerPath *iter;
2633 struct CadetPeerPath *next;
2638 GCC_check_connections ();
2639 p1 = GNUNET_PEER_search (peer1);
2640 p2 = GNUNET_PEER_search (peer2);
2642 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2643 if (0 == p1 || 0 == p2)
2645 /* We don't even know them */
2649 for (iter = peer->path_head; NULL != iter; iter = next)
2652 for (i = 0; i < iter->length - 1; i++)
2654 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2655 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2660 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2663 path_invalidate (iter);
2667 GCC_check_connections ();
2672 * Count the number of known paths toward the peer.
2674 * @param peer Peer to get path info.
2676 * @return Number of known paths.
2679 GCP_count_paths (const struct CadetPeer *peer)
2681 struct CadetPeerPath *iter;
2684 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2692 * Iterate over the paths to a peer.
2694 * @param peer Peer to get path info.
2695 * @param callback Function to call for every path.
2696 * @param cls Closure for @a callback.
2698 * @return Number of iterated paths.
2701 GCP_iterate_paths (struct CadetPeer *peer,
2702 GCP_path_iterator callback,
2705 struct CadetPeerPath *iter;
2708 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2711 if (GNUNET_YES != callback (cls, peer, iter))
2720 * Iterate all known peers.
2722 * @param iter Iterator.
2723 * @param cls Closure for @c iter.
2726 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2729 GCC_check_connections ();
2730 GNUNET_CONTAINER_multipeermap_iterate (peers,
2733 GCC_check_connections ();
2738 * Get the static string for a peer ID.
2742 * @return Static string for it's ID.
2745 GCP_2s (const struct CadetPeer *peer)
2749 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2753 /* end of gnunet-service-cadet_peer.c */