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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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;
240 /******************************************************************************/
241 /***************************** DEBUG *********************************/
242 /******************************************************************************/
245 * Log all kinds of info about the queueing status of a peer.
247 * @param p Peer whose queue to show.
248 * @param level Error level to use for logging.
251 queue_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
253 struct GNUNET_TIME_Relative core_wait_time;
254 struct CadetPeerQueue *q;
257 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
259 __FILE__, __FUNCTION__, __LINE__);
263 LOG2 (level, "QQQ Message queue towards %s\n", GCP_2s (p));
264 LOG2 (level, "QQQ queue length: %u\n", p->queue_n);
265 LOG2 (level, "QQQ core tmt rdy: %p\n", p->core_transmit);
266 if (NULL != p->core_transmit)
268 core_wait_time = GNUNET_TIME_absolute_get_duration (p->tmt_time);
269 LOG2 (level, "QQQ core called %s ago\n",
270 GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_NO));
272 for (q = p->queue_head; NULL != q; q = q->next)
274 LOG2 (level, "QQQ - %s %s on %s\n",
275 GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
276 LOG2 (level, "QQQ payload %s, %u\n",
277 GC_m2s (q->payload_type), q->payload_id);
278 LOG2 (level, "QQQ size: %u bytes\n", q->size);
281 LOG2 (level, "QQQ End queue towards %s\n", GCP_2s (p));
286 * Log all kinds of info about a peer.
291 GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
293 struct CadetPeerPath *path;
297 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
299 __FILE__, __FUNCTION__, __LINE__);
305 LOG2 (level, "PPP DEBUG PEER NULL\n");
309 LOG2 (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
310 LOG2 (level, "PPP last contact %s\n",
311 GNUNET_STRINGS_absolute_time_to_string (p->last_contact));
312 for (path = p->path_head; NULL != path; path = path->next)
317 LOG2 (level, "PPP path: %s\n", s);
321 LOG2 (level, "PPP core transmit handle %p\n", p->core_transmit);
322 LOG2 (level, "PPP DHT GET handle %p\n", p->search_h);
324 if (NULL != p->connections)
325 conns += GNUNET_CONTAINER_multihashmap_size (p->connections);
326 LOG2 (level, "PPP # connections over link to peer: %u\n", conns);
327 queue_debug (p, level);
328 LOG2 (level, "PPP DEBUG END\n");
332 /******************************************************************************/
333 /***************************** CORE HELPERS *********************************/
334 /******************************************************************************/
338 * Iterator to notify all connections of a broken link. Mark connections
339 * to destroy after all traffic has been sent.
341 * @param cls Closure (peer disconnected).
342 * @param key Current key code (peer id).
343 * @param value Value in the hash map (connection).
345 * @return #GNUNET_YES to continue to iterate.
348 notify_broken (void *cls,
349 const struct GNUNET_HashCode *key,
352 struct CadetPeer *peer = cls;
353 struct CadetConnection *c = value;
355 LOG (GNUNET_ERROR_TYPE_DEBUG,
356 "Notifying %s due to %s\n",
359 GCC_notify_broken (c,
366 * Remove the direct path to the peer.
368 * @param peer Peer to remove the direct path from.
371 static struct CadetPeerPath *
372 pop_direct_path (struct CadetPeer *peer)
374 struct CadetPeerPath *iter;
376 for (iter = peer->path_head; NULL != iter; iter = iter->next)
378 if (2 >= iter->length)
380 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
388 /******************************************************************************/
389 /***************************** CORE CALLBACKS *********************************/
390 /******************************************************************************/
394 * Method called whenever a given peer connects.
397 * @param peer peer identity this notification is about
400 core_connect (void *cls,
401 const struct GNUNET_PeerIdentity *peer)
403 struct CadetPeer *mp;
404 struct CadetPeerPath *path;
407 GNUNET_snprintf (own_id,
410 GNUNET_i2s (&my_full_id));
414 LOG (GNUNET_ERROR_TYPE_INFO,
415 "CONNECTED %s (self)\n",
421 LOG (GNUNET_ERROR_TYPE_INFO,
422 "CONNECTED %s <= %s\n",
426 path->peers[1] = mp->id;
427 GNUNET_PEER_change_rc (mp->id, 1);
429 path->peers[0] = myid;
430 GNUNET_PEER_change_rc (myid, 1);
431 GCP_add_path (mp, path, GNUNET_YES);
432 GNUNET_STATISTICS_update (stats,
436 GNUNET_assert (NULL == mp->connections);
437 mp->connections = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
439 if ( (NULL != GCP_get_tunnel (mp)) &&
440 (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer)) )
446 * Method called whenever a peer disconnects.
449 * @param peer peer identity this notification is about
452 core_disconnect (void *cls,
453 const struct GNUNET_PeerIdentity *peer)
456 struct CadetPeerPath *direct_path;
459 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
460 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
467 LOG (GNUNET_ERROR_TYPE_INFO,
468 "DISCONNECTED %s (self)\n",
471 LOG (GNUNET_ERROR_TYPE_INFO,
472 "DISCONNECTED %s <= %s\n",
473 own_id, GNUNET_i2s (peer));
474 direct_path = pop_direct_path (p);
475 GNUNET_CONTAINER_multihashmap_iterate (p->connections,
478 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
479 p->connections = NULL;
480 if (NULL != p->core_transmit)
482 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
483 p->core_transmit = NULL;
484 p->tmt_time.abs_value_us = 0;
486 GNUNET_STATISTICS_update (stats,
490 path_destroy (direct_path);
495 * Functions to handle messages from core
497 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
498 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
499 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
500 sizeof (struct GNUNET_CADET_ConnectionACK)},
501 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
502 sizeof (struct GNUNET_CADET_ConnectionBroken)},
503 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
504 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
505 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
506 sizeof (struct GNUNET_CADET_ACK)},
507 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
508 sizeof (struct GNUNET_CADET_Poll)},
509 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
510 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
511 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_AX, 0},
517 * To be called on core init/fail.
519 * @param cls Closure (config)
520 * @param identity the public identity of this peer
523 core_init (void *cls,
524 const struct GNUNET_PeerIdentity *identity)
526 const struct GNUNET_CONFIGURATION_Handle *c = cls;
529 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
530 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
532 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
533 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
534 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
535 GNUNET_CORE_disconnect (core_handle);
536 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
537 NULL, /* Closure passed to CADET functions */
538 &core_init, /* Call core_init once connected */
539 &core_connect, /* Handle connects */
540 &core_disconnect, /* remove peers on disconnects */
541 NULL, /* Don't notify about all incoming messages */
542 GNUNET_NO, /* For header only in notification */
543 NULL, /* Don't notify about all outbound messages */
544 GNUNET_NO, /* For header-only out notification */
545 core_handlers); /* Register these handlers */
554 * Core callback to write a pre-constructed data packet to core buffer
556 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
557 * @param size Number of bytes available in buf.
558 * @param buf Where the to write the message.
560 * @return number of bytes written to buf
563 send_core_data_raw (void *cls, size_t size, void *buf)
565 struct GNUNET_MessageHeader *msg = cls;
568 GNUNET_assert (NULL != msg);
569 total_size = ntohs (msg->size);
571 if (total_size > size)
576 memcpy (buf, msg, total_size);
583 * Function to send a create connection message to a peer.
585 * @param c Connection to create.
586 * @param size number of bytes available in buf
587 * @param buf where the callee should write the message
588 * @return number of bytes written to buf
591 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
593 struct GNUNET_CADET_ConnectionCreate *msg;
594 struct GNUNET_PeerIdentity *peer_ptr;
595 const struct CadetPeerPath *p = GCC_get_path (c);
602 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
604 sizeof (struct GNUNET_CADET_ConnectionCreate) +
605 p->length * sizeof (struct GNUNET_PeerIdentity);
607 if (size < size_needed || NULL == buf)
612 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
613 msg->header.size = htons (size_needed);
614 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
615 msg->cid = *GCC_get_id (c);
617 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
618 for (i = 0; i < p->length; i++)
620 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
623 LOG (GNUNET_ERROR_TYPE_DEBUG,
624 "CONNECTION CREATE (%u bytes long) sent!\n",
631 * Creates a path ack message in buf and frees all unused resources.
633 * @param c Connection to send an ACK on.
634 * @param size number of bytes available in buf
635 * @param buf where the callee should write the message
637 * @return number of bytes written to buf
640 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
642 struct GNUNET_CADET_ConnectionACK *msg = buf;
644 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
645 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
650 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
651 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
652 msg->cid = *GCC_get_id (c);
654 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
655 return sizeof (struct GNUNET_CADET_ConnectionACK);
659 /******************************************************************************/
660 /******************************** STATIC ***********************************/
661 /******************************************************************************/
665 * Get priority for a queued message.
667 * @param q Queued message
669 * @return CORE priority to use.
671 static enum GNUNET_CORE_Priority
672 get_priority (struct CadetPeerQueue *q)
674 enum GNUNET_CORE_Priority low;
675 enum GNUNET_CORE_Priority high;
680 return GNUNET_CORE_PRIO_BACKGROUND;
683 /* Relayed traffic has lower priority, our own traffic has higher */
684 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
686 low = GNUNET_CORE_PRIO_BEST_EFFORT;
687 high = GNUNET_CORE_PRIO_URGENT;
691 low = GNUNET_CORE_PRIO_URGENT;
692 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
695 /* Bulky payload has lower priority, control traffic has higher. */
696 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type
697 || GNUNET_MESSAGE_TYPE_CADET_AX == q->type)
705 * Destroy the peer_info and free any allocated resources linked to it
707 * @param peer The peer_info to destroy.
708 * @return #GNUNET_OK on success
711 peer_destroy (struct CadetPeer *peer)
713 struct GNUNET_PeerIdentity id;
714 struct CadetPeerPath *p;
715 struct CadetPeerPath *nextp;
717 GNUNET_PEER_resolve (peer->id, &id);
718 GNUNET_PEER_change_rc (peer->id, -1);
720 LOG (GNUNET_ERROR_TYPE_INFO,
721 "destroying peer %s\n",
724 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
727 LOG (GNUNET_ERROR_TYPE_WARNING, " peer not in peermap!!\n");
729 GCP_stop_search (peer);
734 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
738 if (NULL != peer->tunnel)
739 GCT_destroy_empty (peer->tunnel);
740 if (NULL != peer->connections)
742 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (peer->connections));
743 GNUNET_CONTAINER_multihashmap_destroy (peer->connections);
745 GNUNET_free_non_null (peer->hello);
752 * Iterator over peer hash map entries to destroy the peer during shutdown.
755 * @param key current key code
756 * @param value value in the hash map
757 * @return #GNUNET_YES if we should continue to iterate,
761 shutdown_peer (void *cls,
762 const struct GNUNET_PeerIdentity *key,
765 struct CadetPeer *p = value;
766 struct CadetTunnel *t = p->tunnel;
778 * Check if peer is searching for a path (either active or delayed search).
780 * @param peer Peer to check
781 * @return #GNUNET_YES if there is a search active.
782 * #GNUNET_NO otherwise.
785 is_searching (const struct CadetPeer *peer)
787 return (NULL == peer->search_h && NULL == peer->search_delayed) ?
788 GNUNET_NO : GNUNET_YES;
793 * @brief Start a search for a peer.
795 * @param cls Closure (Peer to search for).
796 * @param tc Task context.
799 delayed_search (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
801 struct CadetPeer *peer = cls;
803 peer->search_delayed = NULL;
805 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
808 GCP_start_search (peer);
813 * Returns if peer is used (has a tunnel or is neighbor).
815 * @param peer Peer to check.
816 * @return #GNUNET_YES if peer is in use.
819 peer_is_used (struct CadetPeer *peer)
821 struct CadetPeerPath *p;
823 if (NULL != peer->tunnel)
826 for (p = peer->path_head; NULL != p; p = p->next)
836 * Iterator over all the peers to get the oldest timestamp.
838 * @param cls Closure (unsued).
839 * @param key ID of the peer.
840 * @param value Peer_Info of the peer.
843 peer_get_oldest (void *cls,
844 const struct GNUNET_PeerIdentity *key,
847 struct CadetPeer *p = value;
848 struct GNUNET_TIME_Absolute *abs = cls;
850 /* Don't count active peers */
851 if (GNUNET_YES == peer_is_used (p))
854 if (abs->abs_value_us < p->last_contact.abs_value_us)
855 abs->abs_value_us = p->last_contact.abs_value_us;
862 * Iterator over all the peers to remove the oldest entry.
864 * @param cls Closure (unsued).
865 * @param key ID of the peer.
866 * @param value Peer_Info of the peer.
869 peer_timeout (void *cls,
870 const struct GNUNET_PeerIdentity *key,
873 struct CadetPeer *p = value;
874 struct GNUNET_TIME_Absolute *abs = cls;
876 LOG (GNUNET_ERROR_TYPE_WARNING,
877 "peer %s timeout\n", GNUNET_i2s (key));
879 if (p->last_contact.abs_value_us == abs->abs_value_us &&
880 GNUNET_NO == peer_is_used (p))
890 * Delete oldest unused peer.
893 peer_delete_oldest (void)
895 struct GNUNET_TIME_Absolute abs;
897 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
899 GNUNET_CONTAINER_multipeermap_iterate (peers,
902 GNUNET_CONTAINER_multipeermap_iterate (peers,
909 * Choose the best (yet unused) path towards a peer,
910 * considering the tunnel properties.
912 * @param peer The destination peer.
913 * @return Best current known path towards the peer, if any.
915 static struct CadetPeerPath *
916 peer_get_best_path (const struct CadetPeer *peer)
918 struct CadetPeerPath *best_p;
919 struct CadetPeerPath *p;
920 unsigned int best_cost;
923 best_cost = UINT_MAX;
925 for (p = peer->path_head; NULL != p; p = p->next)
927 if (GNUNET_NO == path_is_valid (p))
928 continue; /* Don't use invalid paths. */
929 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
930 continue; /* If path is already in use, skip it. */
932 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
943 * Is this queue element sendable?
945 * - All management traffic is always sendable.
946 * - For payload traffic, check the connection flow control.
948 * @param q Queue element to inspect.
949 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
952 queue_is_sendable (struct CadetPeerQueue *q)
954 /* Is PID-independent? */
957 case GNUNET_MESSAGE_TYPE_CADET_ACK:
958 case GNUNET_MESSAGE_TYPE_CADET_POLL:
959 case GNUNET_MESSAGE_TYPE_CADET_KX:
960 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
961 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
962 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
963 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
966 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
967 case GNUNET_MESSAGE_TYPE_CADET_AX:
974 return GCC_is_sendable (q->c, q->fwd);
979 * Get first sendable message.
981 * @param peer The destination peer.
983 * @return First transmittable message, if any. Otherwise, NULL.
985 static struct CadetPeerQueue *
986 peer_get_first_message (const struct CadetPeer *peer)
988 struct CadetPeerQueue *q;
990 for (q = peer->queue_head; NULL != q; q = q->next)
992 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
993 if (queue_is_sendable (q))
1002 * Function to process paths received for a new peer addition. The recorded
1003 * paths form the initial tunnel, which can be optimized later.
1004 * Called on each result obtained for the DHT search.
1006 * @param cls closure
1010 search_handler (void *cls, const struct CadetPeerPath *path)
1012 struct CadetPeer *peer = cls;
1013 unsigned int connection_count;
1015 GCP_add_path_to_all (path, GNUNET_NO);
1017 /* Count connections */
1018 connection_count = GCT_count_connections (peer->tunnel);
1020 /* If we already have our minimum (or more) connections, it's enough */
1021 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1024 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1026 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1033 * Adjust core requested size to accomodate an ACK.
1035 * @param message_size Requested size.
1037 * @return Size enough to fit @c message_size and an ACK.
1040 get_core_size (size_t message_size)
1042 return message_size + sizeof (struct GNUNET_CADET_ACK);
1047 * Fill a core buffer with the appropriate data for the queued message.
1049 * @param queue Queue element for the message.
1050 * @param buf Core buffer to fill.
1051 * @param size Size remaining in @c buf.
1052 * @param[out] pid In case its an encrypted payload, set payload.
1054 * @return Bytes written to @c buf.
1057 fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
1059 struct CadetConnection *c = queue->c;
1062 switch (queue->type)
1064 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1065 *pid = GCC_get_pid (queue->c, queue->fwd);
1066 LOG (GNUNET_ERROR_TYPE_DEBUG, " otr payload ID %u\n", *pid);
1067 msg_size = send_core_data_raw (queue->cls, size, buf);
1068 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
1070 case GNUNET_MESSAGE_TYPE_CADET_AX:
1071 *pid = GCC_get_pid (queue->c, queue->fwd);
1072 LOG (GNUNET_ERROR_TYPE_DEBUG, " ax payload ID %u\n", *pid);
1073 msg_size = send_core_data_raw (queue->cls, size, buf);
1074 ((struct GNUNET_CADET_AX *) buf)->pid = htonl (*pid);
1076 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1077 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1078 case GNUNET_MESSAGE_TYPE_CADET_KX:
1079 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1080 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1081 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1082 msg_size = send_core_data_raw (queue->cls, size, buf);
1084 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1085 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1086 if (GCC_is_origin (c, GNUNET_YES))
1087 msg_size = send_core_connection_create (c, size, buf);
1089 msg_size = send_core_data_raw (queue->cls, size, buf);
1091 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1092 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1093 if (GCC_is_origin (c, GNUNET_NO) ||
1094 GCC_is_origin (c, GNUNET_YES))
1096 msg_size = send_core_connection_ack (c, size, buf);
1100 msg_size = send_core_data_raw (queue->cls, size, buf);
1103 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1104 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1105 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1106 /* This should be encapsulted */
1112 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1116 GNUNET_assert (size >= msg_size);
1123 * Core callback to write a queued packet to core buffer
1125 * @param cls Closure (peer info).
1126 * @param size Number of bytes available in buf.
1127 * @param buf Where the to write the message.
1129 * @return number of bytes written to buf
1132 queue_send (void *cls, size_t size, void *buf)
1134 struct CadetPeer *peer = cls;
1135 struct CadetConnection *c;
1136 struct CadetPeerQueue *queue;
1137 struct GNUNET_TIME_Relative core_wait_time;
1138 const struct GNUNET_PeerIdentity *dst_id;
1145 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1146 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1147 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
1148 GCP_2s (peer), size);
1150 /* Sanity checking */
1151 if (NULL == buf || 0 == size)
1153 LOG (GNUNET_ERROR_TYPE_DEBUG, "Buffer size 0.\n");
1154 peer->tmt_time.abs_value_us = 0;
1155 peer->core_transmit = NULL;
1164 peer->core_transmit = NULL;
1165 queue = peer_get_first_message (peer);
1168 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
1169 peer->tmt_time.abs_value_us = 0;
1172 core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1173 LOG (GNUNET_ERROR_TYPE_DEBUG, " core wait time %s\n",
1174 GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_NO));
1175 peer->tmt_time.abs_value_us = 0;
1177 /* Copy all possible messages to the core buffer */
1178 while (NULL != queue && rest >= queue->size)
1182 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s %s\n",
1183 GCC_2s (c), GC_f2s(queue->fwd));
1184 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok (%u/%u)\n",
1185 queue->size, total_size, size);
1187 msg_size = fill_buf (queue, (void *) dst, size, &pid);
1189 if (0 < drop_percent &&
1190 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1192 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on connection %s %s\n",
1193 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1194 queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
1199 LOG (GNUNET_ERROR_TYPE_INFO,
1200 "snd %s (%s %4u) on connection %s (%p) %s (size %u)\n",
1201 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1202 queue->payload_id, GCC_2s (c), c, GC_f2s (queue->fwd), msg_size);
1204 total_size += msg_size;
1206 dst = &dst[msg_size];
1209 /* Free queue, but cls was freed by send_core_* in fill_buf. */
1210 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1213 queue = peer_get_first_message (peer);
1216 /* If more data in queue, send next */
1219 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data! (%u)\n", queue->size);
1220 if (NULL == peer->core_transmit)
1222 dst_id = GNUNET_PEER_resolve2 (peer->id);
1223 peer->core_transmit =
1224 GNUNET_CORE_notify_transmit_ready (core_handle,
1225 GNUNET_NO, get_priority (queue),
1226 GNUNET_TIME_UNIT_FOREVER_REL,
1228 get_core_size (queue->size),
1231 peer->tmt_time = GNUNET_TIME_absolute_get ();
1232 queue->start_waiting = GNUNET_TIME_absolute_get ();
1236 LOG (GNUNET_ERROR_TYPE_DEBUG, "* tmt rdy called somewhere else\n");
1238 // GCC_start_poll (); FIXME needed?
1242 // GCC_stop_poll(); FIXME needed?
1245 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", total_size);
1246 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1252 /******************************************************************************/
1253 /******************************** API ***********************************/
1254 /******************************************************************************/
1258 * Free a transmission that was already queued with all resources
1259 * associated to the request.
1261 * If connection was marked to be destroyed, and this was the last queued
1262 * message on it, the connection will be free'd as a result.
1264 * @param queue Queue handler to cancel.
1265 * @param clear_cls Is it necessary to free associated cls?
1266 * @param sent Was it really sent? (Could have been canceled)
1267 * @param pid PID, if relevant (was sent and was a payload message).
1269 * @return #GNUNET_YES if connection was destroyed as a result,
1270 * #GNUNET_NO otherwise.
1273 GCP_queue_destroy (struct CadetPeerQueue *queue, int clear_cls,
1274 int sent, uint32_t pid)
1276 struct CadetPeer *peer;
1277 int connection_destroyed;
1280 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1281 if (GNUNET_YES == clear_cls)
1283 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1284 switch (queue->type)
1286 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1287 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1289 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1290 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1291 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1292 case GNUNET_MESSAGE_TYPE_CADET_KX:
1293 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1294 case GNUNET_MESSAGE_TYPE_CADET_AX:
1295 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1296 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1297 GNUNET_free_non_null (queue->cls);
1302 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1303 GC_m2s (queue->type));
1306 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1308 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1309 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1314 if (NULL != queue->cont)
1316 struct GNUNET_TIME_Relative wait_time;
1318 wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1319 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1320 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1321 connection_destroyed = queue->cont (queue->cont_cls,
1322 queue->c, sent, queue->type, pid,
1323 queue->fwd, queue->size, wait_time);
1327 connection_destroyed = GNUNET_NO;
1330 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1332 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1333 peer->core_transmit = NULL;
1334 peer->tmt_time.abs_value_us = 0;
1337 GNUNET_free (queue);
1338 return connection_destroyed;
1343 * @brief Queue and pass message to core when possible.
1345 * @param peer Peer towards which to queue the message.
1346 * @param cls Closure (@c type dependant). It will be used by queue_send to
1347 * build the message to be sent if not already prebuilt.
1348 * @param type Type of the message, 0 for a raw message.
1349 * @param size Size of the message.
1350 * @param c Connection this message belongs to (can be NULL).
1351 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1352 * @param cont Continuation to be called once CORE has taken the message.
1353 * @param cont_cls Closure for @c cont.
1355 * @return Handle to cancel the message before it is sent. Once cont is called
1356 * message has been sent and therefore the handle is no longer valid.
1358 struct CadetPeerQueue *
1359 GCP_queue_add (struct CadetPeer *peer, void *cls, uint16_t type,
1360 uint16_t payload_type, uint32_t payload_id, size_t size,
1361 struct CadetConnection *c, int fwd,
1362 GCP_sent cont, void *cont_cls)
1364 struct CadetPeerQueue *q;
1369 if (NULL == c && GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN != type)
1370 error_level = GNUNET_ERROR_TYPE_ERROR;
1372 error_level = GNUNET_ERROR_TYPE_INFO;
1374 "que %s (%s %4u) on connection %s (%p) %s towards %s (size %u)\n",
1375 GC_m2s (type), GC_m2s (payload_type), payload_id,
1376 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1378 if (error_level == GNUNET_ERROR_TYPE_ERROR)
1380 if (NULL == peer->connections)
1382 /* We are not connected to this peer, ignore request. */
1383 LOG (GNUNET_ERROR_TYPE_INFO, "%s not a neighbor\n", GCP_2s (peer));
1384 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1391 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1392 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1397 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1399 call_core = (NULL == c || type == GNUNET_MESSAGE_TYPE_CADET_KX) ?
1400 GNUNET_YES : GCC_is_sendable (c, fwd);
1401 q = GNUNET_new (struct CadetPeerQueue);
1404 q->payload_type = payload_type;
1405 q->payload_id = payload_id;
1411 q->cont_cls = cont_cls;
1414 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1419 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1420 call_core = GNUNET_YES;
1423 q->start_waiting = GNUNET_TIME_absolute_get ();
1424 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1426 LOG (GNUNET_ERROR_TYPE_DEBUG,
1427 "calling core tmt rdy towards %s for %u bytes\n",
1428 GCP_2s (peer), size);
1429 peer->core_transmit =
1430 GNUNET_CORE_notify_transmit_ready (core_handle,
1431 GNUNET_NO, get_priority (q),
1432 GNUNET_TIME_UNIT_FOREVER_REL,
1433 GNUNET_PEER_resolve2 (peer->id),
1434 get_core_size (size),
1436 peer->tmt_time = GNUNET_TIME_absolute_get ();
1438 else if (GNUNET_NO == call_core)
1440 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1446 struct GNUNET_TIME_Relative elapsed;
1447 elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1448 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
1450 GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
1453 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1459 * Cancel all queued messages to a peer that belong to a certain connection.
1461 * @param peer Peer towards whom to cancel.
1462 * @param c Connection whose queued messages to cancel. Might be destroyed by
1463 * the sent continuation call.
1466 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c)
1468 struct CadetPeerQueue *q;
1469 struct CadetPeerQueue *next;
1470 struct CadetPeerQueue *prev;
1471 int connection_destroyed;
1473 connection_destroyed = GNUNET_NO;
1474 for (q = peer->queue_head; NULL != q; q = next)
1479 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP queue cancel %s\n", GC_m2s (q->type));
1480 GNUNET_break (GNUNET_NO == connection_destroyed);
1481 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1487 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1490 /* Get next from prev, q->next might be already freed:
1491 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1494 next = peer->queue_head;
1504 if (NULL == peer->queue_head && NULL != peer->core_transmit)
1506 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1507 peer->core_transmit = NULL;
1508 peer->tmt_time.abs_value_us = 0;
1514 * Get the first transmittable message for a connection.
1516 * @param peer Neighboring peer.
1517 * @param c Connection.
1519 * @return First transmittable message.
1521 static struct CadetPeerQueue *
1522 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1524 struct CadetPeerQueue *q;
1526 for (q = peer->queue_head; NULL != q; q = q->next)
1530 if (queue_is_sendable (q))
1532 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1535 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1543 * Get the first message for a connection and unqueue it.
1545 * Only tunnel (or higher) level messages are unqueued. Connection specific
1546 * messages are silently destroyed upon encounter.
1548 * @param peer Neighboring peer.
1549 * @param c Connection.
1550 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1553 * @return First message for this connection.
1555 struct GNUNET_MessageHeader *
1556 GCP_connection_pop (struct CadetPeer *peer,
1557 struct CadetConnection *c,
1560 struct CadetPeerQueue *q;
1561 struct CadetPeerQueue *next;
1562 struct GNUNET_MessageHeader *msg;
1565 GNUNET_assert (NULL != destroyed);
1566 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on connection %p\n", c);
1567 for (q = peer->queue_head; NULL != q; q = next)
1572 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), cont: %p\n",
1573 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1577 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1578 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1579 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1580 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1581 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1582 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1583 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1584 if (GNUNET_YES == dest)
1586 GNUNET_break (GNUNET_NO == *destroyed);
1587 *destroyed = GNUNET_YES;
1591 case GNUNET_MESSAGE_TYPE_CADET_KX:
1592 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1593 case GNUNET_MESSAGE_TYPE_CADET_AX:
1594 case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
1595 msg = (struct GNUNET_MessageHeader *) q->cls;
1596 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1597 if (GNUNET_YES == dest)
1599 GNUNET_break (GNUNET_NO == *destroyed);
1600 *destroyed = GNUNET_YES;
1606 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1614 * Unlock a possibly locked queue for a connection.
1616 * If there is a message that can be sent on this connection, call core for it.
1617 * Otherwise (if core transmit is already called or there is no sendable
1618 * message) do nothing.
1620 * @param peer Peer who keeps the queue.
1621 * @param c Connection whose messages to unlock.
1624 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1626 struct CadetPeerQueue *q;
1629 if (NULL != peer->core_transmit)
1631 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1632 return; /* Already unlocked */
1635 q = connection_get_first_message (peer, c);
1638 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1639 return; /* Nothing to transmit */
1643 peer->core_transmit =
1644 GNUNET_CORE_notify_transmit_ready (core_handle,
1645 GNUNET_NO, get_priority (q),
1646 GNUNET_TIME_UNIT_FOREVER_REL,
1647 GNUNET_PEER_resolve2 (peer->id),
1648 get_core_size (size),
1651 peer->tmt_time = GNUNET_TIME_absolute_get ();
1656 * Initialize the peer subsystem.
1658 * @param c Configuration.
1661 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1663 LOG (GNUNET_ERROR_TYPE_DEBUG,
1665 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1667 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1670 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1671 "CADET", "MAX_PEERS", "USING DEFAULT");
1676 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1683 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1684 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1685 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1686 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1687 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1690 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1691 NULL, /* Closure passed to CADET functions */
1692 &core_init, /* Call core_init once connected */
1693 &core_connect, /* Handle connects */
1694 &core_disconnect, /* remove peers on disconnects */
1695 NULL, /* Don't notify about all incoming messages */
1696 GNUNET_NO, /* For header only in notification */
1697 NULL, /* Don't notify about all outbound messages */
1698 GNUNET_NO, /* For header-only out notification */
1699 core_handlers); /* Register these handlers */
1701 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1703 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1704 /* Notify callbacks */
1709 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1710 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1711 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1712 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1713 transport_handle = NULL;
1718 if (NULL == core_handle)
1721 GNUNET_SCHEDULER_shutdown ();
1729 * Shut down the peer subsystem.
1734 GNUNET_CONTAINER_multipeermap_iterate (peers,
1737 if (NULL != core_handle)
1739 GNUNET_CORE_disconnect (core_handle);
1742 if (NULL != transport_handle)
1744 GNUNET_TRANSPORT_disconnect (transport_handle);
1745 transport_handle = NULL;
1747 GNUNET_PEER_change_rc (myid, -1);
1748 GNUNET_CONTAINER_multipeermap_destroy (peers);
1754 * Retrieve the CadetPeer stucture associated with the peer, create one
1755 * and insert it in the appropriate structures if the peer is not known yet.
1757 * @param peer_id Full identity of the peer.
1759 * @return Existing or newly created peer structure.
1762 GCP_get (const struct GNUNET_PeerIdentity *peer_id)
1764 struct CadetPeer *peer;
1766 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1769 peer = GNUNET_new (struct CadetPeer);
1770 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1772 peer_delete_oldest ();
1774 GNUNET_assert (GNUNET_OK ==
1775 GNUNET_CONTAINER_multipeermap_put (peers,
1778 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1779 peer->id = GNUNET_PEER_intern (peer_id);
1781 peer->last_contact = GNUNET_TIME_absolute_get ();
1788 * Retrieve the CadetPeer stucture associated with the peer, create one
1789 * and insert it in the appropriate structures if the peer is not known yet.
1791 * @param peer Short identity of the peer.
1793 * @return Existing or newly created peer structure.
1796 GCP_get_short (const GNUNET_PEER_Id peer)
1798 return GCP_get (GNUNET_PEER_resolve2 (peer));
1803 * Try to connect to a peer on transport level.
1805 * @param cls Closure (peer).
1806 * @param tc TaskContext.
1809 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1811 struct CadetPeer *peer = cls;
1813 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1816 GNUNET_TRANSPORT_try_connect (transport_handle,
1817 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1822 * Try to establish a new connection to this peer (in its tunnel).
1823 * If the peer doesn't have any path to it yet, try to get one.
1824 * If the peer already has some path, send a CREATE CONNECTION towards it.
1826 * @param peer Peer to connect to.
1829 GCP_connect (struct CadetPeer *peer)
1831 struct CadetTunnel *t;
1832 struct CadetPeerPath *p;
1833 struct CadetConnection *c;
1836 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1838 /* If we have a current hello, try to connect using it. */
1839 GCP_try_connect (peer);
1843 rerun_search = GNUNET_NO;
1845 if (NULL != peer->path_head)
1847 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1848 p = peer_get_best_path (peer);
1854 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1857 c = GCT_use_path (t, p);
1860 /* This case can happen when the path includes a first hop that is
1861 * not yet known to be connected.
1863 * This happens quite often during testing when running cadet
1864 * under valgrind: core connect notifications come very late
1865 * and the DHT result has already come and created a valid
1866 * path. In this case, the peer->connections_{pred,succ}
1867 * hashmaps will be NULL and tunnel_use_path will not be able
1868 * to create a connection from that path.
1870 * Re-running the DHT GET should give core time to callback.
1872 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1873 * updating statistics about this issue.
1875 rerun_search = GNUNET_YES;
1879 GCC_send_create (c);
1885 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1889 if (GNUNET_YES == rerun_search)
1891 struct GNUNET_TIME_Relative delay;
1893 GCP_stop_search (peer);
1894 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1895 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay, &delayed_search,
1900 if (GNUNET_NO == is_searching (peer))
1901 GCP_start_search (peer);
1906 * Chech whether there is a direct (core level) connection to peer.
1908 * @param peer Peer to check.
1910 * @return #GNUNET_YES if there is a direct connection.
1913 GCP_is_neighbor (const struct CadetPeer *peer)
1915 struct CadetPeerPath *path;
1917 if (NULL == peer->connections)
1920 for (path = peer->path_head; NULL != path; path = path->next)
1922 if (3 > path->length)
1926 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1932 * Create and initialize a new tunnel towards a peer, in case it has none.
1933 * In case the peer already has a tunnel, nothing is done.
1935 * Does not generate any traffic, just creates the local data structures.
1937 * @param peer Peer towards which to create the tunnel.
1940 GCP_add_tunnel (struct CadetPeer *peer)
1942 if (NULL != peer->tunnel)
1944 peer->tunnel = GCT_new (peer);
1949 * Add a connection to a neighboring peer.
1951 * Store that the peer is the first hop of the connection in one
1952 * direction and that on peer disconnect the connection must be
1953 * notified and destroyed, for it will no longer be valid.
1955 * @param peer Peer to add connection to.
1956 * @param c Connection to add.
1957 * @param pred #GNUNET_YES if we are predecessor, #GNUNET_NO if we are successor
1960 GCP_add_connection (struct CadetPeer *peer,
1961 struct CadetConnection *c,
1964 LOG (GNUNET_ERROR_TYPE_DEBUG,
1965 "adding connection %s\n",
1967 LOG (GNUNET_ERROR_TYPE_DEBUG,
1970 GNUNET_assert (NULL != peer->connections);
1971 GNUNET_assert (GNUNET_OK ==
1972 GNUNET_CONTAINER_multihashmap_put (peer->connections,
1975 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1976 LOG (GNUNET_ERROR_TYPE_DEBUG,
1977 "Peer %s has now %u connections.\n",
1979 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1984 * Add the path to the peer and update the path used to reach it in case this
1987 * @param peer Destination peer to add the path to.
1988 * @param path New path to add. Last peer must be the peer in arg 1.
1989 * Path will be either used of freed if already known.
1990 * @param trusted Do we trust that this path is real?
1992 * @return path if path was taken, pointer to existing duplicate if exists
1995 struct CadetPeerPath *
1996 GCP_add_path (struct CadetPeer *peer, struct CadetPeerPath *path,
1999 struct CadetPeerPath *aux;
2003 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2004 path->length, GCP_2s (peer));
2006 if (NULL == peer || NULL == path
2007 || path->peers[path->length - 1] != peer->id)
2010 path_destroy (path);
2014 for (l = 1; l < path->length; l++)
2016 if (path->peers[l] == myid)
2018 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
2019 for (l2 = 0; l2 < path->length - l; l2++)
2021 path->peers[l2] = path->peers[l + l2];
2025 path->peers = GNUNET_realloc (path->peers,
2026 path->length * sizeof (GNUNET_PEER_Id));
2030 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
2032 if (2 >= path->length && GNUNET_NO == trusted)
2034 /* Only allow CORE to tell us about direct paths */
2035 path_destroy (path);
2039 l = path_get_length (path);
2042 path_destroy (path);
2046 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2047 for (aux = peer->path_head; aux != NULL; aux = aux->next)
2049 l2 = path_get_length (aux);
2052 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
2053 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
2054 peer->path_tail, aux, path);
2059 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2061 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
2062 path_destroy (path);
2067 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
2069 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
2072 if (NULL != peer->tunnel
2073 && CONNECTIONS_PER_TUNNEL < GCT_count_connections (peer->tunnel))
2082 * Add the path to the origin peer and update the path used to reach it in case
2083 * this is the shortest.
2084 * The path is given in peer_info -> destination, therefore we turn the path
2085 * upside down first.
2087 * @param peer Peer to add the path to, being the origin of the path.
2088 * @param path New path to add after being inversed.
2089 * Path will be either used or freed.
2090 * @param trusted Do we trust that this path is real?
2092 * @return path if path was taken, pointer to existing duplicate if exists
2095 struct CadetPeerPath *
2096 GCP_add_path_to_origin (struct CadetPeer *peer,
2097 struct CadetPeerPath *path,
2103 return GCP_add_path (peer, path, trusted);
2108 * Adds a path to the info of all the peers in the path
2110 * @param p Path to process.
2111 * @param confirmed Whether we know if the path works or not.
2114 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
2118 /* TODO: invert and add */
2119 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2120 for (i++; i < p->length; i++)
2122 struct CadetPeer *aux;
2123 struct CadetPeerPath *copy;
2125 aux = GCP_get_short (p->peers[i]);
2126 copy = path_duplicate (p);
2127 copy->length = i + 1;
2128 GCP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2134 * Remove any path to the peer that has the extact same peers as the one given.
2136 * @param peer Peer to remove the path from.
2137 * @param path Path to remove. Is always destroyed .
2140 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
2142 struct CadetPeerPath *iter;
2143 struct CadetPeerPath *next;
2145 GNUNET_assert (myid == path->peers[0]);
2146 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2148 LOG (GNUNET_ERROR_TYPE_INFO,
2149 "Removing path %p (%u) from %s\n",
2154 for (iter = peer->path_head; NULL != iter; iter = next)
2157 if (0 == path_cmp (path, iter))
2159 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
2161 path_destroy (iter);
2164 path_destroy (path);
2169 * Remove a connection from a neighboring peer.
2171 * @param peer Peer to remove connection from.
2172 * @param c Connection to remove.
2175 GCP_remove_connection (struct CadetPeer *peer,
2176 const struct CadetConnection *c)
2178 LOG (GNUNET_ERROR_TYPE_DEBUG,
2179 "Removing connection %s\n",
2181 LOG (GNUNET_ERROR_TYPE_DEBUG,
2184 if ( (NULL == peer) ||
2185 (NULL == peer->connections) )
2187 GNUNET_assert (GNUNET_YES ==
2188 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2191 LOG (GNUNET_ERROR_TYPE_DEBUG,
2192 "Peer %s remains with %u connections.\n",
2194 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2199 * Start the DHT search for new paths towards the peer: we don't have
2200 * enough good connections.
2202 * @param peer Destination peer.
2205 GCP_start_search (struct CadetPeer *peer)
2207 const struct GNUNET_PeerIdentity *id;
2208 struct CadetTunnel *t = peer->tunnel;
2210 if (NULL != peer->search_h)
2216 if (NULL != peer->search_delayed)
2217 GCP_stop_search (peer);
2219 id = GNUNET_PEER_resolve2 (peer->id);
2220 peer->search_h = GCD_search (id, &search_handler, peer);
2224 /* Why would we search for a peer with no tunnel towards it? */
2229 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
2230 || 0 == GCT_count_any_connections (t))
2232 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2238 * Stop the DHT search for new paths towards the peer: we already have
2239 * enough good connections.
2241 * @param peer Destination peer.
2244 GCP_stop_search (struct CadetPeer *peer)
2246 if (NULL != peer->search_h)
2248 GCD_search_stop (peer->search_h);
2249 peer->search_h = NULL;
2251 if (NULL != peer->search_delayed)
2253 GNUNET_SCHEDULER_cancel (peer->search_delayed);
2254 peer->search_delayed = NULL;
2260 * Get the Full ID of a peer.
2262 * @param peer Peer to get from.
2264 * @return Full ID of peer.
2266 const struct GNUNET_PeerIdentity *
2267 GCP_get_id (const struct CadetPeer *peer)
2269 return GNUNET_PEER_resolve2 (peer->id);
2274 * Get the Short ID of a peer.
2276 * @param peer Peer to get from.
2278 * @return Short ID of peer.
2281 GCP_get_short_id (const struct CadetPeer *peer)
2290 * If tunnel is NULL and there was a search active, stop it, as it's useless.
2296 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2299 if (NULL == t && GNUNET_YES == is_searching (peer))
2301 GCP_stop_search (peer);
2307 * Get the tunnel towards a peer.
2309 * @param peer Peer to get from.
2311 * @return Tunnel towards peer.
2313 struct CadetTunnel *
2314 GCP_get_tunnel (const struct CadetPeer *peer)
2316 return peer->tunnel;
2321 * Set the hello message.
2323 * @param peer Peer whose message to set.
2324 * @param hello Hello message.
2327 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2329 struct GNUNET_HELLO_Message *old;
2332 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2336 old = GCP_get_hello (peer);
2339 size = GNUNET_HELLO_size (hello);
2340 LOG (GNUNET_ERROR_TYPE_DEBUG, " new (%u bytes)\n", size);
2341 peer->hello = GNUNET_malloc (size);
2342 memcpy (peer->hello, hello, size);
2346 peer->hello = GNUNET_HELLO_merge (old, hello);
2347 LOG (GNUNET_ERROR_TYPE_DEBUG, " merge into %p (%u bytes)\n",
2348 peer->hello, GNUNET_HELLO_size (hello));
2355 * Get the hello message.
2357 * @param peer Peer whose message to get.
2359 * @return Hello message.
2361 struct GNUNET_HELLO_Message *
2362 GCP_get_hello (struct CadetPeer *peer)
2364 struct GNUNET_TIME_Absolute expiration;
2365 struct GNUNET_TIME_Relative remaining;
2367 if (NULL == peer->hello)
2370 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2371 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2372 if (0 == remaining.rel_value_us)
2374 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2375 GNUNET_STRINGS_absolute_time_to_string (expiration));
2376 GNUNET_free (peer->hello);
2384 * Try to connect to a peer on TRANSPORT level.
2386 * @param peer Peer to whom to connect.
2389 GCP_try_connect (struct CadetPeer *peer)
2391 struct GNUNET_HELLO_Message *hello;
2392 struct GNUNET_MessageHeader *mh;
2394 if (NULL == transport_handle)
2397 hello = GCP_get_hello (peer);
2401 mh = GNUNET_HELLO_get_header (hello);
2402 GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
2407 * Notify a peer that a link between two other peers is broken. If any path
2408 * used that link, eliminate it.
2410 * @param peer Peer affected by the change.
2411 * @param peer1 Peer whose link is broken.
2412 * @param peer2 Peer whose link is broken.
2415 GCP_notify_broken_link (struct CadetPeer *peer,
2416 struct GNUNET_PeerIdentity *peer1,
2417 struct GNUNET_PeerIdentity *peer2)
2419 struct CadetPeerPath *iter;
2420 struct CadetPeerPath *next;
2425 p1 = GNUNET_PEER_search (peer1);
2426 p2 = GNUNET_PEER_search (peer2);
2428 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2429 if (0 == p1 || 0 == p2)
2431 /* We don't even know them */
2435 for (iter = peer->path_head; NULL != iter; iter = next)
2438 for (i = 0; i < iter->length - 1; i++)
2440 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2441 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2446 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2449 path_invalidate (iter);
2457 * Count the number of known paths toward the peer.
2459 * @param peer Peer to get path info.
2461 * @return Number of known paths.
2464 GCP_count_paths (const struct CadetPeer *peer)
2466 struct CadetPeerPath *iter;
2469 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2477 * Iterate all known peers.
2479 * @param iter Iterator.
2480 * @param cls Closure for @c iter.
2483 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2486 GNUNET_CONTAINER_multipeermap_iterate (peers,
2493 * Get the static string for a peer ID.
2497 * @return Static string for it's ID.
2500 GCP_2s (const struct CadetPeer *peer)
2504 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));