2 This file is part of GNUnet.
3 Copyright (C) 2013, 2015 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file cadet/gnunet-service-cadet_peer.c
22 * @brief GNUnet CADET service connection handling
23 * @author Bartlomiej Polot
26 #include "gnunet_util_lib.h"
27 #include "gnunet_signatures.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_core_service.h"
30 #include "gnunet_statistics_service.h"
31 #include "cadet_protocol.h"
32 #include "gnunet-service-cadet_peer.h"
33 #include "gnunet-service-cadet_dht.h"
34 #include "gnunet-service-cadet_connection.h"
35 #include "gnunet-service-cadet_tunnel.h"
36 #include "cadet_path.h"
38 #define LOG(level, ...) GNUNET_log_from (level,"cadet-p2p",__VA_ARGS__)
39 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-p2p",__VA_ARGS__)
42 /******************************************************************************/
43 /******************************** STRUCTS **********************************/
44 /******************************************************************************/
47 * Struct containing info about a queued transmission to this peer
54 struct CadetPeerQueue *next;
59 struct CadetPeerQueue *prev;
62 * Peer this transmission is directed to.
64 struct CadetPeer *peer;
67 * Connection this message belongs to.
69 struct CadetConnection *c;
77 * Pointer to info stucture used as cls.
89 uint16_t payload_type;
102 * Set when this message starts waiting for CORE.
104 struct GNUNET_TIME_Absolute start_waiting;
107 * Function to call on sending.
112 * Closure for callback.
119 * Struct containing all information regarding a given peer
129 * Last time we heard from this peer
131 struct GNUNET_TIME_Absolute last_contact;
134 * Paths to reach the peer, ordered by ascending hop count
136 struct CadetPeerPath *path_head;
139 * Paths to reach the peer, ordered by ascending hop count
141 struct CadetPeerPath *path_tail;
144 * Handle to stop the DHT search for paths to this peer
146 struct GCD_search_handle *search_h;
149 * Handle to stop the DHT search for paths to this peer
151 struct GNUNET_SCHEDULER_Task *search_delayed;
154 * Tunnel to this peer, if any.
156 struct CadetTunnel *tunnel;
159 * Connections that go through this peer; indexed by tid.
161 struct GNUNET_CONTAINER_MultiHashMap *connections;
164 * Handle for queued transmissions
166 struct GNUNET_CORE_TransmitHandle *core_transmit;
171 struct GNUNET_TIME_Absolute tmt_time;
174 * Transmission queue to core DLL head
176 struct CadetPeerQueue *queue_head;
179 * Transmission queue to core DLL tail
181 struct CadetPeerQueue *queue_tail;
184 * How many messages are in the queue to this peer.
186 unsigned int queue_n;
191 struct GNUNET_HELLO_Message* hello;
195 /******************************************************************************/
196 /******************************* GLOBALS ***********************************/
197 /******************************************************************************/
200 * Global handle to the statistics service.
202 extern struct GNUNET_STATISTICS_Handle *stats;
205 * Local peer own ID (full value).
207 extern struct GNUNET_PeerIdentity my_full_id;
210 * Local peer own ID (short)
212 extern GNUNET_PEER_Id myid;
215 * Peers known, indexed by PeerIdentity, values of type `struct CadetPeer`.
217 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
220 * How many peers do we want to remember?
222 static unsigned long long max_peers;
225 * Percentage of messages that will be dropped (for test purposes only).
227 static unsigned long long drop_percent;
230 * Handle to communicate with core.
232 static struct GNUNET_CORE_Handle *core_handle;
235 * Handle to try to start new connections.
237 static struct GNUNET_TRANSPORT_Handle *transport_handle;
242 static int in_shutdown;
245 /******************************************************************************/
246 /***************************** DEBUG *********************************/
247 /******************************************************************************/
250 * Log all kinds of info about the queueing status of a peer.
252 * @param p Peer whose queue to show.
253 * @param level Error level to use for logging.
256 queue_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
258 struct GNUNET_TIME_Relative core_wait_time;
259 struct CadetPeerQueue *q;
262 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
264 __FILE__, __FUNCTION__, __LINE__);
268 LOG2 (level, "QQQ Message queue towards %s\n", GCP_2s (p));
269 LOG2 (level, "QQQ queue length: %u\n", p->queue_n);
270 LOG2 (level, "QQQ core tmt rdy: %p\n", p->core_transmit);
271 if (NULL != p->core_transmit)
273 core_wait_time = GNUNET_TIME_absolute_get_duration (p->tmt_time);
274 LOG2 (level, "QQQ core called %s ago\n",
275 GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_NO));
277 for (q = p->queue_head; NULL != q; q = q->next)
279 LOG2 (level, "QQQ - %s %s on %s\n",
280 GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
281 LOG2 (level, "QQQ payload %s, %u\n",
282 GC_m2s (q->payload_type), q->payload_id);
283 LOG2 (level, "QQQ size: %u bytes\n", q->size);
286 LOG2 (level, "QQQ End queue towards %s\n", GCP_2s (p));
291 * Log all kinds of info about a peer.
296 GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
298 struct CadetPeerPath *path;
302 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
304 __FILE__, __FUNCTION__, __LINE__);
310 LOG2 (level, "PPP DEBUG PEER NULL\n");
314 LOG2 (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
315 LOG2 (level, "PPP last contact %s\n",
316 GNUNET_STRINGS_absolute_time_to_string (p->last_contact));
317 for (path = p->path_head; NULL != path; path = path->next)
322 LOG2 (level, "PPP path: %s\n", s);
326 LOG2 (level, "PPP core transmit handle %p\n", p->core_transmit);
327 LOG2 (level, "PPP DHT GET handle %p\n", p->search_h);
329 if (NULL != p->connections)
330 conns += GNUNET_CONTAINER_multihashmap_size (p->connections);
331 LOG2 (level, "PPP # connections over link to peer: %u\n", conns);
332 queue_debug (p, level);
333 LOG2 (level, "PPP DEBUG END\n");
337 /******************************************************************************/
338 /***************************** CORE HELPERS *********************************/
339 /******************************************************************************/
343 * Iterator to notify all connections of a broken link. Mark connections
344 * to destroy after all traffic has been sent.
346 * @param cls Closure (disconnected peer).
347 * @param key Current key code (peer id).
348 * @param value Value in the hash map (connection).
350 * @return #GNUNET_YES to continue to iterate.
353 notify_broken (void *cls,
354 const struct GNUNET_HashCode *key,
357 struct CadetPeer *peer = cls;
358 struct CadetConnection *c = value;
360 LOG (GNUNET_ERROR_TYPE_DEBUG,
361 "Notifying %s due to %s\n",
364 GCC_neighbor_disconnected (c, peer);
370 * Remove the direct path to the peer.
372 * @param peer Peer to remove the direct path from.
375 static struct CadetPeerPath *
376 pop_direct_path (struct CadetPeer *peer)
378 struct CadetPeerPath *iter;
380 for (iter = peer->path_head; NULL != iter; iter = iter->next)
382 if (2 >= iter->length)
384 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
392 /******************************************************************************/
393 /***************************** CORE CALLBACKS *********************************/
394 /******************************************************************************/
398 * Method called whenever a given peer connects.
401 * @param peer peer identity this notification is about
404 core_connect (void *cls,
405 const struct GNUNET_PeerIdentity *peer)
407 struct CadetPeer *mp;
408 struct CadetPeerPath *path;
411 GCC_check_connections ();
412 GNUNET_snprintf (own_id,
415 GNUNET_i2s (&my_full_id));
416 mp = GCP_get (peer, GNUNET_YES);
419 LOG (GNUNET_ERROR_TYPE_INFO,
420 "CONNECTED %s (self)\n",
426 LOG (GNUNET_ERROR_TYPE_INFO,
427 "CONNECTED %s <= %s\n",
431 path->peers[1] = mp->id;
432 GNUNET_PEER_change_rc (mp->id, 1);
434 path->peers[0] = myid;
435 GNUNET_PEER_change_rc (myid, 1);
436 GCP_add_path (mp, path, GNUNET_YES);
437 GNUNET_STATISTICS_update (stats,
441 GNUNET_assert (NULL == mp->connections);
442 mp->connections = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
444 if ( (NULL != GCP_get_tunnel (mp)) &&
445 (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer)) )
447 GCC_check_connections ();
452 * Method called whenever a peer disconnects.
455 * @param peer peer identity this notification is about
458 core_disconnect (void *cls,
459 const struct GNUNET_PeerIdentity *peer)
462 struct CadetPeerPath *direct_path;
465 GCC_check_connections ();
466 strncpy (own_id, GNUNET_i2s (&my_full_id), 16);
468 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
471 GNUNET_break (GNUNET_YES == in_shutdown);
475 LOG (GNUNET_ERROR_TYPE_INFO,
476 "DISCONNECTED %s (self)\n",
479 LOG (GNUNET_ERROR_TYPE_INFO,
480 "DISCONNECTED %s <= %s\n",
481 own_id, GNUNET_i2s (peer));
482 direct_path = pop_direct_path (p);
483 GNUNET_CONTAINER_multihashmap_iterate (p->connections,
486 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
487 p->connections = NULL;
488 if (NULL != p->core_transmit)
490 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
491 p->core_transmit = NULL;
492 p->tmt_time.abs_value_us = 0;
494 GNUNET_STATISTICS_update (stats,
498 path_destroy (direct_path);
499 GCC_check_connections ();
504 * Functions to handle messages from core
506 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
507 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
508 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
509 sizeof (struct GNUNET_CADET_ConnectionACK)},
510 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
511 sizeof (struct GNUNET_CADET_ConnectionBroken)},
512 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
513 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
514 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
515 sizeof (struct GNUNET_CADET_ACK)},
516 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
517 sizeof (struct GNUNET_CADET_Poll)},
518 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
519 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
520 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_AX, 0},
526 * To be called on core init/fail.
528 * @param cls Closure (config)
529 * @param identity the public identity of this peer
532 core_init (void *cls,
533 const struct GNUNET_PeerIdentity *identity)
535 const struct GNUNET_CONFIGURATION_Handle *c = cls;
538 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
539 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
541 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
542 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
543 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
544 GNUNET_CORE_disconnect (core_handle);
545 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
546 NULL, /* Closure passed to CADET functions */
547 &core_init, /* Call core_init once connected */
548 &core_connect, /* Handle connects */
549 &core_disconnect, /* remove peers on disconnects */
550 NULL, /* Don't notify about all incoming messages */
551 GNUNET_NO, /* For header only in notification */
552 NULL, /* Don't notify about all outbound messages */
553 GNUNET_NO, /* For header-only out notification */
554 core_handlers); /* Register these handlers */
563 * Core callback to write a pre-constructed data packet to core buffer
565 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
566 * @param size Number of bytes available in buf.
567 * @param buf Where the to write the message.
569 * @return number of bytes written to buf
572 send_core_data_raw (void *cls, size_t size, void *buf)
574 struct GNUNET_MessageHeader *msg = cls;
577 GNUNET_assert (NULL != msg);
578 total_size = ntohs (msg->size);
580 if (total_size > size)
585 memcpy (buf, msg, total_size);
592 * Function to send a create connection message to a peer.
594 * @param c Connection to create.
595 * @param size number of bytes available in buf
596 * @param buf where the callee should write the message
597 * @return number of bytes written to buf
600 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
602 struct GNUNET_CADET_ConnectionCreate *msg;
603 struct GNUNET_PeerIdentity *peer_ptr;
604 const struct CadetPeerPath *p = GCC_get_path (c);
611 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
613 sizeof (struct GNUNET_CADET_ConnectionCreate) +
614 p->length * sizeof (struct GNUNET_PeerIdentity);
616 if (size < size_needed || NULL == buf)
621 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
622 msg->header.size = htons (size_needed);
623 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
624 msg->cid = *GCC_get_id (c);
626 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
627 for (i = 0; i < p->length; i++)
629 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
632 LOG (GNUNET_ERROR_TYPE_DEBUG,
633 "CONNECTION CREATE (%u bytes long) sent!\n",
640 * Creates a path ack message in buf and frees all unused resources.
642 * @param c Connection to send an ACK on.
643 * @param size number of bytes available in buf
644 * @param buf where the callee should write the message
646 * @return number of bytes written to buf
649 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
651 struct GNUNET_CADET_ConnectionACK *msg = buf;
653 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
654 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
659 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
660 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
661 msg->cid = *GCC_get_id (c);
663 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
664 return sizeof (struct GNUNET_CADET_ConnectionACK);
668 /******************************************************************************/
669 /******************************** STATIC ***********************************/
670 /******************************************************************************/
674 * Get priority for a queued message.
676 * @param q Queued message
678 * @return CORE priority to use.
680 static enum GNUNET_CORE_Priority
681 get_priority (struct CadetPeerQueue *q)
683 enum GNUNET_CORE_Priority low;
684 enum GNUNET_CORE_Priority high;
689 return GNUNET_CORE_PRIO_BACKGROUND;
692 /* Relayed traffic has lower priority, our own traffic has higher */
693 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
695 low = GNUNET_CORE_PRIO_BEST_EFFORT;
696 high = GNUNET_CORE_PRIO_URGENT;
700 low = GNUNET_CORE_PRIO_URGENT;
701 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
704 /* Bulky payload has lower priority, control traffic has higher. */
705 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type
706 || GNUNET_MESSAGE_TYPE_CADET_AX == q->type)
714 * Destroy the peer_info and free any allocated resources linked to it
716 * @param peer The peer_info to destroy.
717 * @return #GNUNET_OK on success
720 peer_destroy (struct CadetPeer *peer)
722 struct GNUNET_PeerIdentity id;
723 struct CadetPeerPath *p;
724 struct CadetPeerPath *nextp;
726 GNUNET_PEER_resolve (peer->id, &id);
727 GNUNET_PEER_change_rc (peer->id, -1);
729 LOG (GNUNET_ERROR_TYPE_INFO,
730 "destroying peer %s\n",
733 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
736 LOG (GNUNET_ERROR_TYPE_WARNING, " peer not in peermap!!\n");
738 GCP_stop_search (peer);
743 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
747 if (NULL != peer->tunnel)
748 GCT_destroy_empty (peer->tunnel);
749 if (NULL != peer->connections)
751 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (peer->connections));
752 GNUNET_CONTAINER_multihashmap_destroy (peer->connections);
754 GNUNET_free_non_null (peer->hello);
761 * Iterator over peer hash map entries to destroy the peer during in_shutdown.
764 * @param key current key code
765 * @param value value in the hash map
766 * @return #GNUNET_YES if we should continue to iterate,
770 shutdown_peer (void *cls,
771 const struct GNUNET_PeerIdentity *key,
774 struct CadetPeer *p = value;
775 struct CadetTunnel *t = p->tunnel;
787 * Check if peer is searching for a path (either active or delayed search).
789 * @param peer Peer to check
790 * @return #GNUNET_YES if there is a search active.
791 * #GNUNET_NO otherwise.
794 is_searching (const struct CadetPeer *peer)
796 return ( (NULL == peer->search_h) &&
797 (NULL == peer->search_delayed) ) ?
798 GNUNET_NO : GNUNET_YES;
803 * @brief Start a search for a peer.
805 * @param cls Closure (Peer to search for).
806 * @param tc Task context.
809 delayed_search (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
811 struct CadetPeer *peer = cls;
813 peer->search_delayed = NULL;
814 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
816 GCC_check_connections ();
817 GCP_start_search (peer);
818 GCC_check_connections ();
823 * Returns if peer is used (has a tunnel or is neighbor).
825 * @param peer Peer to check.
826 * @return #GNUNET_YES if peer is in use.
829 peer_is_used (struct CadetPeer *peer)
831 struct CadetPeerPath *p;
833 if (NULL != peer->tunnel)
836 for (p = peer->path_head; NULL != p; p = p->next)
846 * Iterator over all the peers to get the oldest timestamp.
848 * @param cls Closure (unsued).
849 * @param key ID of the peer.
850 * @param value Peer_Info of the peer.
853 peer_get_oldest (void *cls,
854 const struct GNUNET_PeerIdentity *key,
857 struct CadetPeer *p = value;
858 struct GNUNET_TIME_Absolute *abs = cls;
860 /* Don't count active peers */
861 if (GNUNET_YES == peer_is_used (p))
864 if (abs->abs_value_us < p->last_contact.abs_value_us)
865 abs->abs_value_us = p->last_contact.abs_value_us;
872 * Iterator over all the peers to remove the oldest entry.
874 * @param cls Closure (unsued).
875 * @param key ID of the peer.
876 * @param value Peer_Info of the peer.
879 peer_timeout (void *cls,
880 const struct GNUNET_PeerIdentity *key,
883 struct CadetPeer *p = value;
884 struct GNUNET_TIME_Absolute *abs = cls;
886 LOG (GNUNET_ERROR_TYPE_WARNING,
887 "peer %s timeout\n", GNUNET_i2s (key));
889 if (p->last_contact.abs_value_us == abs->abs_value_us &&
890 GNUNET_NO == peer_is_used (p))
900 * Delete oldest unused peer.
903 peer_delete_oldest (void)
905 struct GNUNET_TIME_Absolute abs;
907 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
909 GNUNET_CONTAINER_multipeermap_iterate (peers,
912 GNUNET_CONTAINER_multipeermap_iterate (peers,
919 * Choose the best (yet unused) path towards a peer,
920 * considering the tunnel properties.
922 * @param peer The destination peer.
923 * @return Best current known path towards the peer, if any.
925 static struct CadetPeerPath *
926 peer_get_best_path (const struct CadetPeer *peer)
928 struct CadetPeerPath *best_p;
929 struct CadetPeerPath *p;
930 unsigned int best_cost;
933 best_cost = UINT_MAX;
935 for (p = peer->path_head; NULL != p; p = p->next)
937 if (GNUNET_NO == path_is_valid (p))
938 continue; /* Don't use invalid paths. */
939 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
940 continue; /* If path is already in use, skip it. */
942 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
953 * Is this queue element sendable?
955 * - All management traffic is always sendable.
956 * - For payload traffic, check the connection flow control.
958 * @param q Queue element to inspect.
959 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
962 queue_is_sendable (struct CadetPeerQueue *q)
964 /* Is PID-independent? */
967 case GNUNET_MESSAGE_TYPE_CADET_ACK:
968 case GNUNET_MESSAGE_TYPE_CADET_POLL:
969 case GNUNET_MESSAGE_TYPE_CADET_KX:
970 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
971 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
972 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
973 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
976 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
977 case GNUNET_MESSAGE_TYPE_CADET_AX:
984 return GCC_is_sendable (q->c, q->fwd);
989 * Get first sendable message.
991 * @param peer The destination peer.
993 * @return First transmittable message, if any. Otherwise, NULL.
995 static struct CadetPeerQueue *
996 peer_get_first_message (const struct CadetPeer *peer)
998 struct CadetPeerQueue *q;
1000 for (q = peer->queue_head; NULL != q; q = q->next)
1002 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
1003 if (queue_is_sendable (q))
1012 * Function to process paths received for a new peer addition. The recorded
1013 * paths form the initial tunnel, which can be optimized later.
1014 * Called on each result obtained for the DHT search.
1016 * @param cls closure
1020 search_handler (void *cls, const struct CadetPeerPath *path)
1022 struct CadetPeer *peer = cls;
1023 unsigned int connection_count;
1025 GCC_check_connections ();
1026 GCP_add_path_to_all (path, GNUNET_NO);
1028 /* Count connections */
1029 connection_count = GCT_count_connections (peer->tunnel);
1031 /* If we already have our minimum (or more) connections, it's enough */
1032 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1034 GCC_check_connections ();
1038 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1040 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1043 GCC_check_connections ();
1048 * Adjust core requested size to accomodate an ACK.
1050 * @param message_size Requested size.
1052 * @return Size enough to fit @c message_size and an ACK.
1055 get_core_size (size_t message_size)
1057 return message_size + sizeof (struct GNUNET_CADET_ACK);
1062 * Fill a core buffer with the appropriate data for the queued message.
1064 * @param queue Queue element for the message.
1065 * @param buf Core buffer to fill.
1066 * @param size Size remaining in @c buf.
1067 * @param[out] pid In case its an encrypted payload, set payload.
1069 * @return Bytes written to @c buf.
1072 fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
1074 struct CadetConnection *c = queue->c;
1077 switch (queue->type)
1079 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1080 *pid = GCC_get_pid (queue->c, queue->fwd);
1081 LOG (GNUNET_ERROR_TYPE_DEBUG, " otr payload ID %u\n", *pid);
1082 msg_size = send_core_data_raw (queue->cls, size, buf);
1083 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
1085 case GNUNET_MESSAGE_TYPE_CADET_AX:
1086 *pid = GCC_get_pid (queue->c, queue->fwd);
1087 LOG (GNUNET_ERROR_TYPE_DEBUG, " ax payload ID %u\n", *pid);
1088 msg_size = send_core_data_raw (queue->cls, size, buf);
1089 ((struct GNUNET_CADET_AX *) buf)->pid = htonl (*pid);
1091 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1092 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1093 case GNUNET_MESSAGE_TYPE_CADET_KX:
1094 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1095 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1096 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1097 msg_size = send_core_data_raw (queue->cls, size, buf);
1099 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1100 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1101 if (GCC_is_origin (c, GNUNET_YES))
1102 msg_size = send_core_connection_create (c, size, buf);
1104 msg_size = send_core_data_raw (queue->cls, size, buf);
1106 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1107 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1108 if (GCC_is_origin (c, GNUNET_NO) ||
1109 GCC_is_origin (c, GNUNET_YES))
1111 msg_size = send_core_connection_ack (c, size, buf);
1115 msg_size = send_core_data_raw (queue->cls, size, buf);
1118 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1119 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1120 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1121 /* This should be encapsulted */
1127 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1131 GNUNET_assert (size >= msg_size);
1138 * Core callback to write a queued packet to core buffer
1140 * @param cls Closure (peer info).
1141 * @param size Number of bytes available in buf.
1142 * @param buf Where the to write the message.
1144 * @return number of bytes written to buf
1147 queue_send (void *cls, size_t size, void *buf)
1149 struct CadetPeer *peer = cls;
1150 struct CadetConnection *c;
1151 struct CadetPeerQueue *queue;
1152 struct GNUNET_TIME_Relative core_wait_time;
1154 const struct GNUNET_PeerIdentity *dst_id;
1161 GCC_check_connections ();
1162 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1163 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1164 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
1165 GCP_2s (peer), size);
1167 /* Sanity checking */
1168 if (NULL == buf || 0 == size)
1170 LOG (GNUNET_ERROR_TYPE_DEBUG, " not allowed/\n");
1171 if (GNUNET_NO == in_shutdown)
1173 queue = peer_get_first_message (peer);
1176 peer->core_transmit = NULL;
1177 peer->tmt_time.abs_value_us = 0;
1178 GCC_check_connections ();
1181 dst_id = GNUNET_PEER_resolve2 (peer->id);
1182 peer->core_transmit =
1183 GNUNET_CORE_notify_transmit_ready (core_handle,
1184 GNUNET_NO, get_priority (queue),
1185 GNUNET_TIME_UNIT_FOREVER_REL,
1187 get_core_size (queue->size),
1190 peer->tmt_time = GNUNET_TIME_absolute_get ();
1194 peer->core_transmit = NULL;
1195 peer->tmt_time.abs_value_us = 0;
1197 GCC_check_connections ();
1206 peer->core_transmit = NULL;
1207 queue = peer_get_first_message (peer);
1210 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
1211 peer->tmt_time.abs_value_us = 0;
1214 core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1215 wait_s = GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_YES);
1216 if (core_wait_time.rel_value_us >= 1000000)
1218 LOG (GNUNET_ERROR_TYPE_ERROR,
1219 " %s: core wait time %s (> 1 second) for %u bytes\n",
1220 GCP_2s (peer), wait_s, queue->size);
1222 peer->tmt_time.abs_value_us = 0;
1224 /* Copy all possible messages to the core buffer */
1225 while (NULL != queue && rest >= queue->size)
1229 LOG (GNUNET_ERROR_TYPE_DEBUG, " on conn %s %s\n",
1230 GCC_2s (c), GC_f2s(queue->fwd));
1231 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok (%u/%u)\n",
1232 queue->size, total_size, size);
1234 msg_size = fill_buf (queue, (void *) dst, size, &pid);
1236 if (0 < drop_percent &&
1237 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1239 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on conn %s %s\n",
1240 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1241 queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
1246 LOG (GNUNET_ERROR_TYPE_INFO,
1247 ">>> %s (%s %4u) on conn %s (%p) %s (%u bytes), after %s\n",
1248 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1249 queue->payload_id, GCC_2s (c), c,
1250 GC_f2s (queue->fwd), msg_size, wait_s);
1252 total_size += msg_size;
1254 dst = &dst[msg_size];
1257 /* Free queue, but cls was freed by send_core_* in fill_buf. */
1258 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1261 queue = peer_get_first_message (peer);
1264 /* If more data in queue, send next */
1267 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data! (%u)\n", queue->size);
1268 if (NULL == peer->core_transmit)
1270 dst_id = GNUNET_PEER_resolve2 (peer->id);
1271 peer->core_transmit =
1272 GNUNET_CORE_notify_transmit_ready (core_handle,
1273 GNUNET_NO, get_priority (queue),
1274 GNUNET_TIME_UNIT_FOREVER_REL,
1276 get_core_size (queue->size),
1279 peer->tmt_time = GNUNET_TIME_absolute_get ();
1280 queue->start_waiting = GNUNET_TIME_absolute_get ();
1284 LOG (GNUNET_ERROR_TYPE_DEBUG, "* tmt rdy called somewhere else\n");
1286 // GCC_start_poll (); FIXME needed?
1290 // GCC_stop_poll(); FIXME needed?
1293 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", total_size);
1294 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1295 GCC_check_connections ();
1300 /******************************************************************************/
1301 /******************************** API ***********************************/
1302 /******************************************************************************/
1306 * Free a transmission that was already queued with all resources
1307 * associated to the request.
1309 * If connection was marked to be destroyed, and this was the last queued
1310 * message on it, the connection will be free'd as a result.
1312 * @param queue Queue handler to cancel.
1313 * @param clear_cls Is it necessary to free associated cls?
1314 * @param sent Was it really sent? (Could have been canceled)
1315 * @param pid PID, if relevant (was sent and was a payload message).
1317 * @return #GNUNET_YES if connection was destroyed as a result,
1318 * #GNUNET_NO otherwise.
1321 GCP_queue_destroy (struct CadetPeerQueue *queue,
1326 struct CadetPeer *peer;
1327 int connection_destroyed;
1329 GCC_check_connections ();
1331 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1332 if (GNUNET_YES == clear_cls)
1334 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1335 switch (queue->type)
1337 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1338 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1340 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1341 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1342 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1343 case GNUNET_MESSAGE_TYPE_CADET_KX:
1344 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1345 case GNUNET_MESSAGE_TYPE_CADET_AX:
1346 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1347 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1348 GNUNET_free_non_null (queue->cls);
1353 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1354 GC_m2s (queue->type));
1357 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1359 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1360 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1365 if (NULL != queue->cont)
1367 struct GNUNET_TIME_Relative wait_time;
1369 wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1370 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1371 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1372 connection_destroyed = queue->cont (queue->cont_cls,
1373 queue->c, sent, queue->type, pid,
1374 queue->fwd, queue->size, wait_time);
1378 connection_destroyed = GNUNET_NO;
1381 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1383 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1384 peer->core_transmit = NULL;
1385 peer->tmt_time.abs_value_us = 0;
1388 GNUNET_free (queue);
1389 GCC_check_connections ();
1390 return connection_destroyed;
1395 * @brief Queue and pass message to core when possible.
1397 * @param peer Peer towards which to queue the message.
1398 * @param cls Closure (@c type dependant). It will be used by queue_send to
1399 * build the message to be sent if not already prebuilt.
1400 * @param type Type of the message, 0 for a raw message.
1401 * @param size Size of the message.
1402 * @param c Connection this message belongs to (can be NULL).
1403 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1404 * @param cont Continuation to be called once CORE has taken the message.
1405 * @param cont_cls Closure for @c cont.
1407 * @return Handle to cancel the message before it is sent. Once cont is called
1408 * message has been sent and therefore the handle is no longer valid.
1410 struct CadetPeerQueue *
1411 GCP_queue_add (struct CadetPeer *peer,
1414 uint16_t payload_type,
1415 uint32_t payload_id,
1417 struct CadetConnection *c,
1422 struct CadetPeerQueue *q;
1426 GCC_check_connections ();
1427 LOG (GNUNET_ERROR_TYPE_DEBUG,
1428 "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n",
1429 GC_m2s (type), GC_m2s (payload_type), payload_id,
1430 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1432 if (NULL == peer->connections)
1434 /* We are not connected to this peer, ignore request. */
1435 LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
1436 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1443 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1444 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1449 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1451 call_core = (NULL == c || type == GNUNET_MESSAGE_TYPE_CADET_KX) ?
1452 GNUNET_YES : GCC_is_sendable (c, fwd);
1453 q = GNUNET_new (struct CadetPeerQueue);
1456 q->payload_type = payload_type;
1457 q->payload_id = payload_id;
1463 q->cont_cls = cont_cls;
1466 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1471 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1472 call_core = GNUNET_YES;
1475 q->start_waiting = GNUNET_TIME_absolute_get ();
1476 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1478 LOG (GNUNET_ERROR_TYPE_DEBUG,
1479 "calling core tmt rdy towards %s for %u bytes\n",
1480 GCP_2s (peer), size);
1481 peer->core_transmit =
1482 GNUNET_CORE_notify_transmit_ready (core_handle,
1483 GNUNET_NO, get_priority (q),
1484 GNUNET_TIME_UNIT_FOREVER_REL,
1485 GNUNET_PEER_resolve2 (peer->id),
1486 get_core_size (size),
1488 peer->tmt_time = GNUNET_TIME_absolute_get ();
1490 else if (GNUNET_NO == call_core)
1492 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1498 struct GNUNET_TIME_Relative elapsed;
1499 elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1500 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
1502 GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
1505 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1506 GCC_check_connections ();
1512 * Cancel all queued messages to a peer that belong to a certain connection.
1514 * @param peer Peer towards whom to cancel.
1515 * @param c Connection whose queued messages to cancel. Might be destroyed by
1516 * the sent continuation call.
1519 GCP_queue_cancel (struct CadetPeer *peer,
1520 struct CadetConnection *c)
1522 struct CadetPeerQueue *q;
1523 struct CadetPeerQueue *next;
1524 struct CadetPeerQueue *prev;
1525 int connection_destroyed;
1527 GCC_check_connections ();
1528 connection_destroyed = GNUNET_NO;
1529 for (q = peer->queue_head; NULL != q; q = next)
1534 LOG (GNUNET_ERROR_TYPE_DEBUG,
1535 "GMP queue cancel %s\n",
1537 GNUNET_break (GNUNET_NO == connection_destroyed);
1538 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1544 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1547 /* Get next from prev, q->next might be already freed:
1548 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1551 next = peer->queue_head;
1561 if ( (NULL == peer->queue_head) &&
1562 (NULL != peer->core_transmit) )
1564 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1565 peer->core_transmit = NULL;
1566 peer->tmt_time.abs_value_us = 0;
1568 GCC_check_connections ();
1573 * Get the first transmittable message for a connection.
1575 * @param peer Neighboring peer.
1576 * @param c Connection.
1578 * @return First transmittable message.
1580 static struct CadetPeerQueue *
1581 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1583 struct CadetPeerQueue *q;
1585 for (q = peer->queue_head; NULL != q; q = q->next)
1589 if (queue_is_sendable (q))
1591 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1594 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1602 * Get the first message for a connection and unqueue it.
1604 * Only tunnel (or higher) level messages are unqueued. Connection specific
1605 * messages are silently destroyed upon encounter.
1607 * @param peer Neighboring peer.
1608 * @param c Connection.
1609 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1612 * @return First message for this connection.
1614 struct GNUNET_MessageHeader *
1615 GCP_connection_pop (struct CadetPeer *peer,
1616 struct CadetConnection *c,
1619 struct CadetPeerQueue *q;
1620 struct CadetPeerQueue *next;
1621 struct GNUNET_MessageHeader *msg;
1624 GCC_check_connections ();
1625 GNUNET_assert (NULL != destroyed);
1626 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on conn %p\n", c);
1627 for (q = peer->queue_head; NULL != q; q = next)
1632 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), cont: %p\n",
1633 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1637 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1638 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1639 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1640 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1641 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1642 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1643 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1644 if (GNUNET_YES == dest)
1646 GNUNET_break (GNUNET_NO == *destroyed);
1647 *destroyed = GNUNET_YES;
1651 case GNUNET_MESSAGE_TYPE_CADET_KX:
1652 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1653 case GNUNET_MESSAGE_TYPE_CADET_AX:
1654 case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
1655 msg = (struct GNUNET_MessageHeader *) q->cls;
1656 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1657 if (GNUNET_YES == dest)
1659 GNUNET_break (GNUNET_NO == *destroyed);
1660 *destroyed = GNUNET_YES;
1666 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1669 GCC_check_connections ();
1675 * Unlock a possibly locked queue for a connection.
1677 * If there is a message that can be sent on this connection, call core for it.
1678 * Otherwise (if core transmit is already called or there is no sendable
1679 * message) do nothing.
1681 * @param peer Peer who keeps the queue.
1682 * @param c Connection whose messages to unlock.
1685 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1687 struct CadetPeerQueue *q;
1690 GCC_check_connections ();
1691 if (NULL != peer->core_transmit)
1693 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1694 return; /* Already unlocked */
1697 q = connection_get_first_message (peer, c);
1700 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1701 return; /* Nothing to transmit */
1705 peer->core_transmit =
1706 GNUNET_CORE_notify_transmit_ready (core_handle,
1707 GNUNET_NO, get_priority (q),
1708 GNUNET_TIME_UNIT_FOREVER_REL,
1709 GNUNET_PEER_resolve2 (peer->id),
1710 get_core_size (size),
1713 peer->tmt_time = GNUNET_TIME_absolute_get ();
1714 GCC_check_connections ();
1719 * Initialize the peer subsystem.
1721 * @param c Configuration.
1724 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1726 LOG (GNUNET_ERROR_TYPE_DEBUG,
1728 in_shutdown = GNUNET_NO;
1729 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1731 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1734 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1735 "CADET", "MAX_PEERS", "USING DEFAULT");
1740 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1747 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1748 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1749 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1750 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1751 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1754 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1755 NULL, /* Closure passed to CADET functions */
1756 &core_init, /* Call core_init once connected */
1757 &core_connect, /* Handle connects */
1758 &core_disconnect, /* remove peers on disconnects */
1759 NULL, /* Don't notify about all incoming messages */
1760 GNUNET_NO, /* For header only in notification */
1761 NULL, /* Don't notify about all outbound messages */
1762 GNUNET_NO, /* For header-only out notification */
1763 core_handlers); /* Register these handlers */
1765 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1767 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1768 /* Notify callbacks */
1773 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1774 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1775 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1776 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1777 transport_handle = NULL;
1782 if (NULL == core_handle)
1785 GNUNET_SCHEDULER_shutdown ();
1793 * Shut down the peer subsystem.
1798 in_shutdown = GNUNET_YES;
1799 GNUNET_CONTAINER_multipeermap_iterate (peers,
1802 if (NULL != core_handle)
1804 GNUNET_CORE_disconnect (core_handle);
1807 if (NULL != transport_handle)
1809 GNUNET_TRANSPORT_disconnect (transport_handle);
1810 transport_handle = NULL;
1812 GNUNET_PEER_change_rc (myid, -1);
1813 GNUNET_CONTAINER_multipeermap_destroy (peers);
1819 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1820 * one and insert it in the appropriate structures if the peer is not known yet.
1822 * @param peer_id Full identity of the peer.
1823 * @param create #GNUNET_YES if a new peer should be created if unknown.
1824 * #GNUNET_NO otherwise.
1826 * @return Existing or newly created peer structure.
1827 * NULL if unknown and not requested @a create
1830 GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create)
1832 struct CadetPeer *peer;
1834 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1837 peer = GNUNET_new (struct CadetPeer);
1838 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1840 peer_delete_oldest ();
1842 GNUNET_assert (GNUNET_OK ==
1843 GNUNET_CONTAINER_multipeermap_put (peers,
1846 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1847 peer->id = GNUNET_PEER_intern (peer_id);
1849 peer->last_contact = GNUNET_TIME_absolute_get ();
1856 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
1857 * one and insert it in the appropriate structures if the peer is not known yet.
1859 * @param peer Short identity of the peer.
1860 * @param create #GNUNET_YES if a new peer should be created if unknown.
1861 * #GNUNET_NO otherwise.
1863 * @return Existing or newly created peer structure.
1864 * NULL if unknown and not requested @a create
1867 GCP_get_short (const GNUNET_PEER_Id peer, int create)
1869 return GCP_get (GNUNET_PEER_resolve2 (peer), create);
1874 * Try to connect to a peer on transport level.
1876 * @param cls Closure (peer).
1877 * @param tc TaskContext.
1880 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1882 struct CadetPeer *peer = cls;
1884 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1887 GNUNET_TRANSPORT_try_connect (transport_handle,
1888 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1893 * Try to establish a new connection to this peer (in its tunnel).
1894 * If the peer doesn't have any path to it yet, try to get one.
1895 * If the peer already has some path, send a CREATE CONNECTION towards it.
1897 * @param peer Peer to connect to.
1900 GCP_connect (struct CadetPeer *peer)
1902 struct CadetTunnel *t;
1903 struct CadetPeerPath *p;
1904 struct CadetConnection *c;
1907 GCC_check_connections ();
1908 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1910 /* If we have a current hello, try to connect using it. */
1911 GCP_try_connect (peer);
1915 rerun_search = GNUNET_NO;
1917 if (NULL != peer->path_head)
1919 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1920 p = peer_get_best_path (peer);
1926 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1929 c = GCT_use_path (t, p);
1932 /* This case can happen when the path includes a first hop that is
1933 * not yet known to be connected.
1935 * This happens quite often during testing when running cadet
1936 * under valgrind: core connect notifications come very late
1937 * and the DHT result has already come and created a valid
1938 * path. In this case, the peer->connections_{pred,succ}
1939 * hashmaps will be NULL and tunnel_use_path will not be able
1940 * to create a connection from that path.
1942 * Re-running the DHT GET should give core time to callback.
1944 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1945 * updating statistics about this issue.
1947 rerun_search = GNUNET_YES;
1951 GCC_send_create (c);
1957 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1961 if (GNUNET_YES == rerun_search)
1963 struct GNUNET_TIME_Relative delay;
1965 GCP_stop_search (peer);
1966 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1967 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay,
1970 GCC_check_connections ();
1974 if (GNUNET_NO == is_searching (peer))
1975 GCP_start_search (peer);
1976 GCC_check_connections ();
1981 * Chech whether there is a direct (core level) connection to peer.
1983 * @param peer Peer to check.
1985 * @return #GNUNET_YES if there is a direct connection.
1988 GCP_is_neighbor (const struct CadetPeer *peer)
1990 struct CadetPeerPath *path;
1992 if (NULL == peer->connections)
1995 for (path = peer->path_head; NULL != path; path = path->next)
1997 if (3 > path->length)
2001 /* Is not a neighbor but connections is not NULL, probably disconnecting */
2007 * Create and initialize a new tunnel towards a peer, in case it has none.
2008 * In case the peer already has a tunnel, nothing is done.
2010 * Does not generate any traffic, just creates the local data structures.
2012 * @param peer Peer towards which to create the tunnel.
2015 GCP_add_tunnel (struct CadetPeer *peer)
2017 GCC_check_connections ();
2018 if (NULL != peer->tunnel)
2020 peer->tunnel = GCT_new (peer);
2021 GCC_check_connections ();
2026 * Add a connection to a neighboring peer.
2028 * Store that the peer is the first hop of the connection in one
2029 * direction and that on peer disconnect the connection must be
2030 * notified and destroyed, for it will no longer be valid.
2032 * @param peer Peer to add connection to.
2033 * @param c Connection to add.
2034 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
2037 GCP_add_connection (struct CadetPeer *peer,
2038 struct CadetConnection *c,
2041 LOG (GNUNET_ERROR_TYPE_DEBUG,
2042 "adding connection %s\n",
2044 LOG (GNUNET_ERROR_TYPE_DEBUG,
2047 GNUNET_assert (NULL != peer->connections);
2048 GNUNET_assert (GNUNET_OK ==
2049 GNUNET_CONTAINER_multihashmap_put (peer->connections,
2052 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2053 LOG (GNUNET_ERROR_TYPE_DEBUG,
2054 "Peer %s has now %u connections.\n",
2056 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2061 * Add the path to the peer and update the path used to reach it in case this
2064 * @param peer Destination peer to add the path to.
2065 * @param path New path to add. Last peer must be the peer in arg 1.
2066 * Path will be either used of freed if already known.
2067 * @param trusted Do we trust that this path is real?
2069 * @return path if path was taken, pointer to existing duplicate if exists
2072 struct CadetPeerPath *
2073 GCP_add_path (struct CadetPeer *peer,
2074 struct CadetPeerPath *path,
2077 struct CadetPeerPath *aux;
2081 GCC_check_connections ();
2082 LOG (GNUNET_ERROR_TYPE_DEBUG,
2083 "adding path [%u] to peer %s\n",
2084 path->length, GCP_2s (peer));
2086 if (NULL == peer || NULL == path
2087 || path->peers[path->length - 1] != peer->id)
2090 path_destroy (path);
2094 for (l = 1; l < path->length; l++)
2096 if (path->peers[l] == myid)
2098 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
2099 for (l2 = 0; l2 < path->length - l; l2++)
2101 path->peers[l2] = path->peers[l + l2];
2105 path->peers = GNUNET_realloc (path->peers,
2106 path->length * sizeof (GNUNET_PEER_Id));
2110 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
2112 if (2 >= path->length && GNUNET_NO == trusted)
2114 /* Only allow CORE to tell us about direct paths */
2115 path_destroy (path);
2119 l = path_get_length (path);
2122 path_destroy (path);
2126 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2127 for (aux = peer->path_head; aux != NULL; aux = aux->next)
2129 l2 = path_get_length (aux);
2132 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
2133 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
2134 peer->path_tail, aux, path);
2139 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2141 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
2142 path_destroy (path);
2147 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
2149 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
2152 if (NULL != peer->tunnel
2153 && CONNECTIONS_PER_TUNNEL < GCT_count_connections (peer->tunnel))
2157 GCC_check_connections ();
2163 * Add the path to the origin peer and update the path used to reach it in case
2164 * this is the shortest.
2165 * The path is given in peer_info -> destination, therefore we turn the path
2166 * upside down first.
2168 * @param peer Peer to add the path to, being the origin of the path.
2169 * @param path New path to add after being inversed.
2170 * Path will be either used or freed.
2171 * @param trusted Do we trust that this path is real?
2173 * @return path if path was taken, pointer to existing duplicate if exists
2176 struct CadetPeerPath *
2177 GCP_add_path_to_origin (struct CadetPeer *peer,
2178 struct CadetPeerPath *path,
2184 return GCP_add_path (peer, path, trusted);
2189 * Adds a path to the info of all the peers in the path
2191 * @param p Path to process.
2192 * @param confirmed Whether we know if the path works or not.
2195 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
2199 /* TODO: invert and add */
2200 GCC_check_connections ();
2201 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2202 for (i++; i < p->length; i++)
2204 struct CadetPeer *aux;
2205 struct CadetPeerPath *copy;
2207 aux = GCP_get_short (p->peers[i], GNUNET_YES);
2208 copy = path_duplicate (p);
2209 copy->length = i + 1;
2210 GCP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2212 GCC_check_connections ();
2217 * Remove any path to the peer that has the extact same peers as the one given.
2219 * @param peer Peer to remove the path from.
2220 * @param path Path to remove. Is always destroyed .
2223 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
2225 struct CadetPeerPath *iter;
2226 struct CadetPeerPath *next;
2228 GCC_check_connections ();
2229 GNUNET_assert (myid == path->peers[0]);
2230 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2232 LOG (GNUNET_ERROR_TYPE_INFO, "Removing path %p (%u) from %s\n",
2233 path, path->length, GCP_2s (peer));
2235 for (iter = peer->path_head; NULL != iter; iter = next)
2238 if (0 == path_cmp (path, iter))
2240 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
2242 path_destroy (iter);
2245 path_destroy (path);
2246 GCC_check_connections ();
2251 * Check that we are aware of a connection from a neighboring peer.
2253 * @param peer Peer to the connection is with
2254 * @param c Connection that should be in the map with this peer.
2257 GCP_check_connection (const struct CadetPeer *peer,
2258 const struct CadetConnection *c)
2260 GNUNET_assert (NULL != peer);
2261 GNUNET_assert (NULL != peer->connections);
2263 GNUNET_assert (GNUNET_YES ==
2264 GNUNET_CONTAINER_multihashmap_contains_value (peer->connections,
2271 * Remove a connection from a neighboring peer.
2273 * @param peer Peer to remove connection from.
2274 * @param c Connection to remove.
2277 GCP_remove_connection (struct CadetPeer *peer,
2278 const struct CadetConnection *c)
2280 LOG (GNUNET_ERROR_TYPE_DEBUG,
2281 "Removing connection %s\n",
2283 LOG (GNUNET_ERROR_TYPE_DEBUG,
2286 if ( (NULL == peer) ||
2287 (NULL == peer->connections) )
2289 GNUNET_assert (GNUNET_YES ==
2290 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2293 LOG (GNUNET_ERROR_TYPE_DEBUG,
2294 "Peer %s remains with %u connections.\n",
2296 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2301 * Start the DHT search for new paths towards the peer: we don't have
2302 * enough good connections.
2304 * @param peer Destination peer.
2307 GCP_start_search (struct CadetPeer *peer)
2309 const struct GNUNET_PeerIdentity *id;
2310 struct CadetTunnel *t = peer->tunnel;
2312 GCC_check_connections ();
2313 if (NULL != peer->search_h)
2319 if (NULL != peer->search_delayed)
2320 GCP_stop_search (peer);
2322 id = GNUNET_PEER_resolve2 (peer->id);
2323 peer->search_h = GCD_search (id, &search_handler, peer);
2327 /* Why would we search for a peer with no tunnel towards it? */
2332 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
2333 || 0 == GCT_count_any_connections (t))
2335 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2337 GCC_check_connections ();
2342 * Stop the DHT search for new paths towards the peer: we already have
2343 * enough good connections.
2345 * @param peer Destination peer.
2348 GCP_stop_search (struct CadetPeer *peer)
2350 GCC_check_connections ();
2351 if (NULL != peer->search_h)
2353 GCD_search_stop (peer->search_h);
2354 peer->search_h = NULL;
2356 if (NULL != peer->search_delayed)
2358 GNUNET_SCHEDULER_cancel (peer->search_delayed);
2359 peer->search_delayed = NULL;
2361 GCC_check_connections ();
2366 * Get the Full ID of a peer.
2368 * @param peer Peer to get from.
2370 * @return Full ID of peer.
2372 const struct GNUNET_PeerIdentity *
2373 GCP_get_id (const struct CadetPeer *peer)
2375 return GNUNET_PEER_resolve2 (peer->id);
2380 * Get the Short ID of a peer.
2382 * @param peer Peer to get from.
2384 * @return Short ID of peer.
2387 GCP_get_short_id (const struct CadetPeer *peer)
2396 * If tunnel is NULL and there was a search active, stop it, as it's useless.
2402 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2405 if (NULL == t && GNUNET_YES == is_searching (peer))
2407 GCP_stop_search (peer);
2413 * Get the tunnel towards a peer.
2415 * @param peer Peer to get from.
2417 * @return Tunnel towards peer.
2419 struct CadetTunnel *
2420 GCP_get_tunnel (const struct CadetPeer *peer)
2424 return peer->tunnel;
2429 * Set the hello message.
2431 * @param peer Peer whose message to set.
2432 * @param hello Hello message.
2435 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2437 struct GNUNET_HELLO_Message *old;
2440 GCC_check_connections ();
2441 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2445 old = GCP_get_hello (peer);
2448 size = GNUNET_HELLO_size (hello);
2449 peer->hello = GNUNET_malloc (size);
2450 memcpy (peer->hello, hello, size);
2454 peer->hello = GNUNET_HELLO_merge (old, hello);
2457 GCC_check_connections ();
2462 * Get the hello message.
2464 * @param peer Peer whose message to get.
2466 * @return Hello message.
2468 struct GNUNET_HELLO_Message *
2469 GCP_get_hello (struct CadetPeer *peer)
2471 struct GNUNET_TIME_Absolute expiration;
2472 struct GNUNET_TIME_Relative remaining;
2474 if (NULL == peer->hello)
2477 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2478 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2479 if (0 == remaining.rel_value_us)
2481 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2482 GNUNET_STRINGS_absolute_time_to_string (expiration));
2483 GNUNET_free (peer->hello);
2491 * Try to connect to a peer on TRANSPORT level.
2493 * @param peer Peer to whom to connect.
2496 GCP_try_connect (struct CadetPeer *peer)
2498 struct GNUNET_HELLO_Message *hello;
2499 struct GNUNET_MessageHeader *mh;
2501 if (NULL == transport_handle)
2503 GCC_check_connections ();
2504 hello = GCP_get_hello (peer);
2508 mh = GNUNET_HELLO_get_header (hello);
2509 GNUNET_TRANSPORT_offer_hello (transport_handle,
2513 GCC_check_connections ();
2518 * Notify a peer that a link between two other peers is broken. If any path
2519 * used that link, eliminate it.
2521 * @param peer Peer affected by the change.
2522 * @param peer1 Peer whose link is broken.
2523 * @param peer2 Peer whose link is broken.
2526 GCP_notify_broken_link (struct CadetPeer *peer,
2527 struct GNUNET_PeerIdentity *peer1,
2528 struct GNUNET_PeerIdentity *peer2)
2530 struct CadetPeerPath *iter;
2531 struct CadetPeerPath *next;
2536 GCC_check_connections ();
2537 p1 = GNUNET_PEER_search (peer1);
2538 p2 = GNUNET_PEER_search (peer2);
2540 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2541 if (0 == p1 || 0 == p2)
2543 /* We don't even know them */
2547 for (iter = peer->path_head; NULL != iter; iter = next)
2550 for (i = 0; i < iter->length - 1; i++)
2552 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2553 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2558 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2561 path_invalidate (iter);
2565 GCC_check_connections ();
2570 * Count the number of known paths toward the peer.
2572 * @param peer Peer to get path info.
2574 * @return Number of known paths.
2577 GCP_count_paths (const struct CadetPeer *peer)
2579 struct CadetPeerPath *iter;
2582 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2590 * Iterate over the paths to a peer.
2592 * @param peer Peer to get path info.
2593 * @param callback Function to call for every path.
2594 * @param cls Closure for @a callback.
2596 * @return Number of iterated paths.
2599 GCP_iterate_paths (struct CadetPeer *peer,
2600 GCP_path_iterator callback,
2603 struct CadetPeerPath *iter;
2606 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2609 if (GNUNET_YES != callback (cls, peer, iter))
2618 * Iterate all known peers.
2620 * @param iter Iterator.
2621 * @param cls Closure for @c iter.
2624 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2627 GCC_check_connections ();
2628 GNUNET_CONTAINER_multipeermap_iterate (peers,
2631 GCC_check_connections ();
2636 * Get the static string for a peer ID.
2640 * @return Static string for it's ID.
2643 GCP_2s (const struct CadetPeer *peer)
2647 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2651 /* end of gnunet-service-cadet_peer.c */