2 This file is part of GNUnet.
3 (C) 2013 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.
23 #include "gnunet_util_lib.h"
25 #include "gnunet_transport_service.h"
26 #include "gnunet_core_service.h"
27 #include "gnunet_statistics_service.h"
29 #include "cadet_protocol.h"
31 #include "gnunet-service-cadet_peer.h"
32 #include "gnunet-service-cadet_dht.h"
33 #include "gnunet-service-cadet_connection.h"
34 #include "gnunet-service-cadet_tunnel.h"
35 #include "cadet_path.h"
37 #define LOG(level, ...) GNUNET_log_from (level,"cadet-p2p",__VA_ARGS__)
39 /******************************************************************************/
40 /******************************** STRUCTS **********************************/
41 /******************************************************************************/
44 * Struct containing info about a queued transmission to this peer
51 struct CadetPeerQueue *next;
56 struct CadetPeerQueue *prev;
59 * Peer this transmission is directed to.
61 struct CadetPeer *peer;
64 * Connection this message belongs to.
66 struct CadetConnection *c;
74 * Pointer to info stucture used as cls.
86 uint16_t payload_type;
99 * Set when this message starts waiting for CORE.
101 struct GNUNET_TIME_Absolute start_waiting;
104 * Function to call on sending.
109 * Closure for callback.
115 * Struct containing all information regarding a given peer
125 * Last time we heard from this peer
127 struct GNUNET_TIME_Absolute last_contact;
130 * Paths to reach the peer, ordered by ascending hop count
132 struct CadetPeerPath *path_head;
135 * Paths to reach the peer, ordered by ascending hop count
137 struct CadetPeerPath *path_tail;
140 * Handle to stop the DHT search for paths to this peer
142 struct GCD_search_handle *search_h;
145 * Tunnel to this peer, if any.
147 struct CadetTunnel *tunnel;
150 * Connections that go through this peer, indexed by tid;
152 struct GNUNET_CONTAINER_MultiHashMap *connections;
155 * Handle for queued transmissions
157 struct GNUNET_CORE_TransmitHandle *core_transmit;
160 * Transmission queue to core DLL head
162 struct CadetPeerQueue *queue_head;
165 * Transmission queue to core DLL tail
167 struct CadetPeerQueue *queue_tail;
170 * How many messages are in the queue to this peer.
172 unsigned int queue_n;
177 struct GNUNET_HELLO_Message* hello;
181 /******************************************************************************/
182 /******************************* GLOBALS ***********************************/
183 /******************************************************************************/
186 * Global handle to the statistics service.
188 extern struct GNUNET_STATISTICS_Handle *stats;
191 * Local peer own ID (full value).
193 extern struct GNUNET_PeerIdentity my_full_id;
196 * Local peer own ID (short)
198 extern GNUNET_PEER_Id myid;
201 * Peers known, indexed by PeerIdentity (CadetPeer).
203 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
206 * How many peers do we want to remember?
208 static unsigned long long max_peers;
211 * Percentage of messages that will be dropped (for test purposes only).
213 static unsigned long long drop_percent;
216 * Handle to communicate with core.
218 static struct GNUNET_CORE_Handle *core_handle;
221 * Handle to try to start new connections.
223 static struct GNUNET_TRANSPORT_Handle *transport_handle;
226 /******************************************************************************/
227 /***************************** DEBUG *********************************/
228 /******************************************************************************/
231 queue_debug (struct CadetPeer *peer)
233 struct CadetPeerQueue *q;
235 LOG (GNUNET_ERROR_TYPE_DEBUG, "QQQ Messages queued towards %s\n", GCP_2s (peer));
236 LOG (GNUNET_ERROR_TYPE_DEBUG, "QQQ core tmt rdy: %p\n", peer->core_transmit);
238 for (q = peer->queue_head; NULL != q; q = q->next)
240 LOG (GNUNET_ERROR_TYPE_DEBUG, "QQQ - %s %s on %s\n",
241 GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
244 LOG (GNUNET_ERROR_TYPE_DEBUG, "QQQ End queued towards %s\n", GCP_2s (peer));
248 /******************************************************************************/
249 /***************************** CORE HELPERS *********************************/
250 /******************************************************************************/
254 * Iterator to notify all connections of a broken link. Mark connections
255 * to destroy after all traffic has been sent.
257 * @param cls Closure (peer disconnected).
258 * @param key Current key code (peer id).
259 * @param value Value in the hash map (connection).
261 * @return #GNUNET_YES to continue to iterate.
264 notify_broken (void *cls,
265 const struct GNUNET_HashCode *key,
268 struct CadetPeer *peer = cls;
269 struct CadetConnection *c = value;
271 LOG (GNUNET_ERROR_TYPE_DEBUG, " notifying %s due to %s\n",
272 GCC_2s (c), GCP_2s (peer));
273 GCC_notify_broken (c, peer);
280 * Remove the direct path to the peer.
282 * @param peer Peer to remove the direct path from.
285 static struct CadetPeerPath *
286 pop_direct_path (struct CadetPeer *peer)
288 struct CadetPeerPath *iter;
290 for (iter = peer->path_head; NULL != iter; iter = iter->next)
292 if (2 <= iter->length)
294 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
302 /******************************************************************************/
303 /***************************** CORE CALLBACKS *********************************/
304 /******************************************************************************/
307 * Method called whenever a given peer connects.
310 * @param peer peer identity this notification is about
313 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
315 struct CadetPeer *mp;
316 struct CadetPeerPath *path;
319 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
323 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s (self)\n", own_id);
328 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s <= %s\n",
329 own_id, GNUNET_i2s (peer));
331 path->peers[1] = mp->id;
332 GNUNET_PEER_change_rc (mp->id, 1);
333 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
335 path->peers[0] = myid;
336 GNUNET_PEER_change_rc (myid, 1);
337 GCP_add_path (mp, path, GNUNET_YES);
339 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
341 if (NULL != GCP_get_tunnel (mp) &&
342 0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer))
352 * Method called whenever a peer disconnects.
355 * @param peer peer identity this notification is about
358 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
361 struct CadetPeerPath *direct_path;
364 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
365 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
372 LOG (GNUNET_ERROR_TYPE_INFO, "DISCONNECTED %s (self)\n", own_id);
374 LOG (GNUNET_ERROR_TYPE_DEBUG, "DISCONNECTED %s <= %s\n",
375 own_id, GNUNET_i2s (peer));
376 direct_path = pop_direct_path (p);
377 GNUNET_CONTAINER_multihashmap_iterate (p->connections, ¬ify_broken, p);
378 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
379 p->connections = NULL;
380 if (NULL != p->core_transmit)
382 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
383 p->core_transmit = NULL;
385 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
387 path_destroy (direct_path);
393 * Functions to handle messages from core
395 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
396 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
397 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
398 sizeof (struct GNUNET_CADET_ConnectionACK)},
399 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
400 sizeof (struct GNUNET_CADET_ConnectionBroken)},
401 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
402 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
403 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
404 sizeof (struct GNUNET_CADET_ACK)},
405 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
406 sizeof (struct GNUNET_CADET_Poll)},
407 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
408 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
414 * To be called on core init/fail.
416 * @param cls Closure (config)
417 * @param identity the public identity of this peer
420 core_init (void *cls,
421 const struct GNUNET_PeerIdentity *identity)
423 const struct GNUNET_CONFIGURATION_Handle *c = cls;
426 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
427 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
429 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
430 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
431 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
432 GNUNET_CORE_disconnect (core_handle);
433 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
434 NULL, /* Closure passed to CADET functions */
435 &core_init, /* Call core_init once connected */
436 &core_connect, /* Handle connects */
437 &core_disconnect, /* remove peers on disconnects */
438 NULL, /* Don't notify about all incoming messages */
439 GNUNET_NO, /* For header only in notification */
440 NULL, /* Don't notify about all outbound messages */
441 GNUNET_NO, /* For header-only out notification */
442 core_handlers); /* Register these handlers */
452 * Core callback to write a pre-constructed data packet to core buffer
454 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
455 * @param size Number of bytes available in buf.
456 * @param buf Where the to write the message.
458 * @return number of bytes written to buf
461 send_core_data_raw (void *cls, size_t size, void *buf)
463 struct GNUNET_MessageHeader *msg = cls;
466 GNUNET_assert (NULL != msg);
467 total_size = ntohs (msg->size);
469 if (total_size > size)
474 memcpy (buf, msg, total_size);
481 * Function to send a create connection message to a peer.
483 * @param c Connection to create.
484 * @param size number of bytes available in buf
485 * @param buf where the callee should write the message
486 * @return number of bytes written to buf
489 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
491 struct GNUNET_CADET_ConnectionCreate *msg;
492 struct GNUNET_PeerIdentity *peer_ptr;
493 const struct CadetPeerPath *p = GCC_get_path (c);
500 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
502 sizeof (struct GNUNET_CADET_ConnectionCreate) +
503 p->length * sizeof (struct GNUNET_PeerIdentity);
505 if (size < size_needed || NULL == buf)
510 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
511 msg->header.size = htons (size_needed);
512 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
513 msg->cid = *GCC_get_id (c);
515 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
516 for (i = 0; i < p->length; i++)
518 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
521 LOG (GNUNET_ERROR_TYPE_DEBUG,
522 "CONNECTION CREATE (%u bytes long) sent!\n",
529 * Creates a path ack message in buf and frees all unused resources.
531 * @param c Connection to send an ACK on.
532 * @param size number of bytes available in buf
533 * @param buf where the callee should write the message
535 * @return number of bytes written to buf
538 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
540 struct GNUNET_CADET_ConnectionACK *msg = buf;
542 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
543 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
548 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
549 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
550 msg->cid = *GCC_get_id (c);
552 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
553 return sizeof (struct GNUNET_CADET_ConnectionACK);
557 /******************************************************************************/
558 /******************************** STATIC ***********************************/
559 /******************************************************************************/
563 * Get priority for a queued message.
565 * @param q Queued message
567 * @return CORE priority to use.
569 static enum GNUNET_CORE_Priority
570 get_priority (struct CadetPeerQueue *q)
572 enum GNUNET_CORE_Priority low;
573 enum GNUNET_CORE_Priority high;
578 return GNUNET_CORE_PRIO_BACKGROUND;
581 /* Relayed traffic has lower priority, our own traffic has higher */
582 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
584 low = GNUNET_CORE_PRIO_BEST_EFFORT;
585 high = GNUNET_CORE_PRIO_URGENT;
589 low = GNUNET_CORE_PRIO_URGENT;
590 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
593 /* Bulky payload has lower priority, control traffic has higher. */
594 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type)
602 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
605 * @param key current key code
606 * @param value value in the hash map
607 * @return #GNUNET_YES if we should continue to iterate,
611 shutdown_tunnel (void *cls,
612 const struct GNUNET_PeerIdentity *key,
615 struct CadetPeer *p = value;
616 struct CadetTunnel *t = p->tunnel;
625 * Destroy the peer_info and free any allocated resources linked to it
627 * @param peer The peer_info to destroy.
629 * @return GNUNET_OK on success
632 peer_destroy (struct CadetPeer *peer)
634 struct GNUNET_PeerIdentity id;
635 struct CadetPeerPath *p;
636 struct CadetPeerPath *nextp;
638 GNUNET_PEER_resolve (peer->id, &id);
639 GNUNET_PEER_change_rc (peer->id, -1);
641 LOG (GNUNET_ERROR_TYPE_WARNING, "destroying peer %s\n", GNUNET_i2s (&id));
644 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
647 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
649 if (NULL != peer->search_h)
651 GCD_search_stop (peer->search_h);
657 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
661 GCT_destroy_empty (peer->tunnel);
668 * Returns if peer is used (has a tunnel or is neighbor).
670 * @param peer Peer to check.
672 * @return #GNUNET_YES if peer is in use.
675 peer_is_used (struct CadetPeer *peer)
677 struct CadetPeerPath *p;
679 if (NULL != peer->tunnel)
682 for (p = peer->path_head; NULL != p; p = p->next)
692 * Iterator over all the peers to get the oldest timestamp.
694 * @param cls Closure (unsued).
695 * @param key ID of the peer.
696 * @param value Peer_Info of the peer.
699 peer_get_oldest (void *cls,
700 const struct GNUNET_PeerIdentity *key,
703 struct CadetPeer *p = value;
704 struct GNUNET_TIME_Absolute *abs = cls;
706 /* Don't count active peers */
707 if (GNUNET_YES == peer_is_used (p))
710 if (abs->abs_value_us < p->last_contact.abs_value_us)
711 abs->abs_value_us = p->last_contact.abs_value_us;
718 * Iterator over all the peers to remove the oldest entry.
720 * @param cls Closure (unsued).
721 * @param key ID of the peer.
722 * @param value Peer_Info of the peer.
725 peer_timeout (void *cls,
726 const struct GNUNET_PeerIdentity *key,
729 struct CadetPeer *p = value;
730 struct GNUNET_TIME_Absolute *abs = cls;
732 LOG (GNUNET_ERROR_TYPE_WARNING,
733 "peer %s timeout\n", GNUNET_i2s (key));
735 if (p->last_contact.abs_value_us == abs->abs_value_us &&
736 GNUNET_NO == peer_is_used (p))
746 * Delete oldest unused peer.
749 peer_delete_oldest (void)
751 struct GNUNET_TIME_Absolute abs;
753 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
755 GNUNET_CONTAINER_multipeermap_iterate (peers,
758 GNUNET_CONTAINER_multipeermap_iterate (peers,
765 * Choose the best (yet unused) path towards a peer,
766 * considering the tunnel properties.
768 * @param peer The destination peer.
770 * @return Best current known path towards the peer, if any.
772 static struct CadetPeerPath *
773 peer_get_best_path (const struct CadetPeer *peer)
775 struct CadetPeerPath *best_p;
776 struct CadetPeerPath *p;
777 unsigned int best_cost;
780 best_cost = UINT_MAX;
782 for (p = peer->path_head; NULL != p; p = p->next)
784 if (GNUNET_NO == path_is_valid (p))
785 continue; /* Don't use invalid paths. */
786 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
787 continue; /* If path is already in use, skip it. */
789 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
800 * Is this queue element sendable?
802 * - All management traffic is always sendable.
803 * - For payload traffic, check the connection flow control.
805 * @param q Queue element to inspect.
807 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
810 queue_is_sendable (struct CadetPeerQueue *q)
812 /* Is PID-independent? */
815 case GNUNET_MESSAGE_TYPE_CADET_ACK:
816 case GNUNET_MESSAGE_TYPE_CADET_POLL:
817 case GNUNET_MESSAGE_TYPE_CADET_KX:
818 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
819 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
820 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
821 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
824 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
831 return GCC_is_sendable (q->c, q->fwd);
836 * Get first sendable message.
838 * @param peer The destination peer.
840 * @return First transmittable message, if any. Otherwise, NULL.
842 static struct CadetPeerQueue *
843 peer_get_first_message (const struct CadetPeer *peer)
845 struct CadetPeerQueue *q;
847 for (q = peer->queue_head; NULL != q; q = q->next)
849 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking %p towards %s\n", q, GCC_2s (q->c));
850 if (queue_is_sendable (q))
859 * Function to process paths received for a new peer addition. The recorded
860 * paths form the initial tunnel, which can be optimized later.
861 * Called on each result obtained for the DHT search.
867 search_handler (void *cls, const struct CadetPeerPath *path)
869 struct CadetPeer *peer = cls;
870 unsigned int connection_count;
872 GCP_add_path_to_all (path, GNUNET_NO);
874 /* Count connections */
875 connection_count = GCT_count_connections (peer->tunnel);
877 /* If we already have 3 (or more (?!)) connections, it's enough */
878 if (3 <= connection_count)
881 if (CADET_TUNNEL3_SEARCHING == GCT_get_cstate (peer->tunnel))
883 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
892 * Core callback to write a queued packet to core buffer
894 * @param cls Closure (peer info).
895 * @param size Number of bytes available in buf.
896 * @param buf Where the to write the message.
898 * @return number of bytes written to buf
901 queue_send (void *cls, size_t size, void *buf)
903 struct CadetPeer *peer = cls;
904 struct CadetConnection *c;
905 struct CadetPeerQueue *queue;
906 const struct GNUNET_PeerIdentity *dst_id;
911 peer->core_transmit = NULL;
912 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
913 GCP_2s (peer), size);
915 if (NULL == buf || 0 == size)
917 LOG (GNUNET_ERROR_TYPE_DEBUG, "Buffer size 0.\n");
922 queue = peer_get_first_message (peer);
925 GNUNET_assert (0); /* Core tmt_rdy should've been canceled */
930 dst_id = GNUNET_PEER_resolve2 (peer->id);
931 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s %s\n",
932 GCC_2s (c), GC_f2s(queue->fwd));
933 /* Check if buffer size is enough for the message */
934 if (queue->size > size)
936 LOG (GNUNET_ERROR_TYPE_WARNING, "not enough room (%u vs %u), reissue\n",
938 peer->core_transmit =
939 GNUNET_CORE_notify_transmit_ready (core_handle,
940 GNUNET_NO, get_priority (queue),
941 GNUNET_TIME_UNIT_FOREVER_REL,
948 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok\n", queue->size);
953 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
954 pid = GCC_get_pid (queue->c, queue->fwd);
955 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload ID %u\n", pid);
956 data_size = send_core_data_raw (queue->cls, size, buf);
957 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (pid);
959 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
960 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
961 case GNUNET_MESSAGE_TYPE_CADET_KX:
962 case GNUNET_MESSAGE_TYPE_CADET_ACK:
963 case GNUNET_MESSAGE_TYPE_CADET_POLL:
964 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
965 data_size = send_core_data_raw (queue->cls, size, buf);
967 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
968 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
969 if (GCC_is_origin (c, GNUNET_YES))
970 data_size = send_core_connection_create (queue->c, size, buf);
972 data_size = send_core_data_raw (queue->cls, size, buf);
974 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
975 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
976 if (GCC_is_origin (c, GNUNET_NO) ||
977 GCC_is_origin (c, GNUNET_YES))
978 data_size = send_core_connection_ack (queue->c, size, buf);
980 data_size = send_core_data_raw (queue->cls, size, buf);
982 case GNUNET_MESSAGE_TYPE_CADET_DATA:
983 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
984 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
985 /* This should be encapsulted */
991 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
995 if (0 < drop_percent &&
996 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
998 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s on connection %s\n",
999 GC_m2s (queue->type), GCC_2s (c));
1004 LOG (GNUNET_ERROR_TYPE_INFO,
1005 "snd %s (%s %u) on connection %s (%p) %s (size %u)\n",
1006 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1007 queue->payload_type, GCC_2s (c), c, GC_f2s (queue->fwd), data_size);
1010 /* Free queue, but cls was freed by send_core_* */
1011 GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1013 /* If more data in queue, send next */
1014 queue = peer_get_first_message (peer);
1017 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data!\n");
1018 if (NULL == peer->core_transmit)
1020 peer->core_transmit =
1021 GNUNET_CORE_notify_transmit_ready (core_handle,
1022 GNUNET_NO, get_priority (queue),
1023 GNUNET_TIME_UNIT_FOREVER_REL,
1028 queue->start_waiting = GNUNET_TIME_absolute_get ();
1032 LOG (GNUNET_ERROR_TYPE_DEBUG,
1033 "* tmt rdy called somewhere else\n");
1035 // GCC_start_poll (); FIXME needed?
1039 // GCC_stop_poll(); FIXME needed?
1042 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", data_size);
1048 /******************************************************************************/
1049 /******************************** API ***********************************/
1050 /******************************************************************************/
1054 * Free a transmission that was already queued with all resources
1055 * associated to the request.
1057 * @param queue Queue handler to cancel.
1058 * @param clear_cls Is it necessary to free associated cls?
1059 * @param sent Was it really sent? (Could have been canceled)
1060 * @param pid PID, if relevant (was sent and was a payload message).
1063 GCP_queue_destroy (struct CadetPeerQueue *queue, int clear_cls,
1064 int sent, uint32_t pid)
1066 struct CadetPeer *peer;
1070 if (GNUNET_YES == clear_cls)
1072 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy type %s\n",
1073 GC_m2s (queue->type));
1074 switch (queue->type)
1076 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1077 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1079 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1080 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1081 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1082 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
1083 case GNUNET_MESSAGE_TYPE_CADET_KX:
1084 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1085 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1086 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1087 GNUNET_free_non_null (queue->cls);
1092 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1093 GC_m2s (queue->type));
1096 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1098 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1099 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1104 if (NULL != queue->callback)
1106 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback\n");
1107 queue->callback (queue->callback_cls,
1108 queue->c, sent, queue->type, pid,
1109 queue->fwd, queue->size,
1110 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
1113 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1115 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1116 peer->core_transmit = NULL;
1119 GNUNET_free (queue);
1124 * @brief Queue and pass message to core when possible.
1126 * @param peer Peer towards which to queue the message.
1127 * @param cls Closure (@c type dependant). It will be used by queue_send to
1128 * build the message to be sent if not already prebuilt.
1129 * @param type Type of the message, 0 for a raw message.
1130 * @param size Size of the message.
1131 * @param c Connection this message belongs to (can be NULL).
1132 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1133 * @param cont Continuation to be called once CORE has taken the message.
1134 * @param cont_cls Closure for @c cont.
1136 * @return Handle to cancel the message before it is sent. Once cont is called
1137 * message has been sent and therefore the handle is no longer valid.
1139 struct CadetPeerQueue *
1140 GCP_queue_add (struct CadetPeer *peer, void *cls, uint16_t type,
1141 uint16_t payload_type, uint32_t payload_id, size_t size,
1142 struct CadetConnection *c, int fwd,
1143 GCP_sent cont, void *cont_cls)
1145 struct CadetPeerQueue *queue;
1149 LOG (GNUNET_ERROR_TYPE_INFO,
1150 "que %s (%s %u) on connection %s (%p) %s towards %s (size %u)\n",
1151 GC_m2s (type), GC_m2s (payload_type), payload_id,
1152 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1154 if (NULL == peer->connections)
1156 /* We are not connected to this peer, ignore request. */
1157 LOG (GNUNET_ERROR_TYPE_WARNING, "%s not a neighbor\n", GCP_2s (peer));
1158 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1165 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1166 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1171 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1173 call_core = NULL == c ? GNUNET_YES : GCC_is_sendable (c, fwd);
1174 queue = GNUNET_new (struct CadetPeerQueue);
1177 queue->payload_type = payload_type;
1178 queue->payload_id = payload_id;
1183 queue->callback = cont;
1184 queue->callback_cls = cont_cls;
1187 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1192 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1193 call_core = GNUNET_YES;
1196 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1198 LOG (GNUNET_ERROR_TYPE_DEBUG,
1199 "calling core tmt rdy towards %s for %u bytes\n",
1200 GCP_2s (peer), size);
1201 peer->core_transmit =
1202 GNUNET_CORE_notify_transmit_ready (core_handle,
1203 GNUNET_NO, get_priority (queue),
1204 GNUNET_TIME_UNIT_FOREVER_REL,
1205 GNUNET_PEER_resolve2 (peer->id),
1209 queue->start_waiting = GNUNET_TIME_absolute_get ();
1211 else if (GNUNET_NO == call_core)
1213 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1219 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called\n",
1229 * Cancel all queued messages to a peer that belong to a certain connection.
1231 * @param peer Peer towards whom to cancel.
1232 * @param c Connection whose queued messages to cancel. Might be destroyed by
1233 * the sent continuation call.
1236 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c)
1238 struct CadetPeerQueue *q;
1239 struct CadetPeerQueue *next;
1240 struct CadetPeerQueue *prev;
1242 for (q = peer->queue_head; NULL != q; q = next)
1247 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP queue cancel %s\n", GC_m2s (q->type));
1248 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY != q->type)
1254 GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1257 /* Get next from prev, q->next might be already freed:
1258 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1261 next = peer->queue_head;
1270 if (NULL == peer->queue_head)
1272 if (NULL != peer->core_transmit)
1274 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1275 peer->core_transmit = NULL;
1282 * Get the first transmittable message for a connection.
1284 * @param peer Neighboring peer.
1285 * @param c Connection.
1287 * @return First transmittable message.
1289 static struct CadetPeerQueue *
1290 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1292 struct CadetPeerQueue *q;
1294 for (q = peer->queue_head; NULL != q; q = q->next)
1298 if (queue_is_sendable (q))
1300 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1303 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1311 * Get the first message for a connection and unqueue it.
1313 * @param peer Neighboring peer.
1314 * @param c Connection.
1316 * @return First message for this connection.
1318 struct GNUNET_MessageHeader *
1319 GCP_connection_pop (struct CadetPeer *peer, struct CadetConnection *c)
1321 struct CadetPeerQueue *q;
1322 struct CadetPeerQueue *next;
1323 struct GNUNET_MessageHeader *msg;
1325 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection pop on %s\n", GCC_2s (c));
1326 for (q = peer->queue_head; NULL != q; q = next)
1333 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1334 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1335 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1336 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1337 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1338 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1339 GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1342 case GNUNET_MESSAGE_TYPE_CADET_KX:
1343 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1344 msg = (struct GNUNET_MessageHeader *) q->cls;
1345 GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1358 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1360 struct CadetPeerQueue *q;
1363 if (NULL != peer->core_transmit)
1365 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1366 return; /* Already unlocked */
1369 q = connection_get_first_message (peer, c);
1372 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1373 return; /* Nothing to transmit */
1377 peer->core_transmit =
1378 GNUNET_CORE_notify_transmit_ready (core_handle,
1379 GNUNET_NO, get_priority (q),
1380 GNUNET_TIME_UNIT_FOREVER_REL,
1381 GNUNET_PEER_resolve2 (peer->id),
1389 * Initialize the peer subsystem.
1391 * @param c Configuration.
1394 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1396 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1397 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1399 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1402 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1403 "CADET", "MAX_PEERS", "USING DEFAULT");
1408 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1415 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1416 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1417 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1418 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1419 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1422 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1423 NULL, /* Closure passed to CADET functions */
1424 &core_init, /* Call core_init once connected */
1425 &core_connect, /* Handle connects */
1426 &core_disconnect, /* remove peers on disconnects */
1427 NULL, /* Don't notify about all incoming messages */
1428 GNUNET_NO, /* For header only in notification */
1429 NULL, /* Don't notify about all outbound messages */
1430 GNUNET_NO, /* For header-only out notification */
1431 core_handlers); /* Register these handlers */
1433 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1435 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1436 /* Notify callbacks */
1441 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1442 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1443 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1444 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1445 transport_handle = NULL;
1450 if (NULL == core_handle)
1453 GNUNET_SCHEDULER_shutdown ();
1460 * Shut down the peer subsystem.
1465 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1467 if (core_handle != NULL)
1469 GNUNET_CORE_disconnect (core_handle);
1472 if (transport_handle != NULL)
1474 GNUNET_TRANSPORT_disconnect (transport_handle);
1475 transport_handle = NULL;
1477 GNUNET_PEER_change_rc (myid, -1);
1481 * Retrieve the CadetPeer stucture associated with the peer, create one
1482 * and insert it in the appropriate structures if the peer is not known yet.
1484 * @param peer_id Full identity of the peer.
1486 * @return Existing or newly created peer structure.
1489 GCP_get (const struct GNUNET_PeerIdentity *peer_id)
1491 struct CadetPeer *peer;
1493 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1496 peer = GNUNET_new (struct CadetPeer);
1497 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1499 peer_delete_oldest ();
1501 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1502 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1503 peer->id = GNUNET_PEER_intern (peer_id);
1505 peer->last_contact = GNUNET_TIME_absolute_get();
1512 * Retrieve the CadetPeer stucture associated with the peer, create one
1513 * and insert it in the appropriate structures if the peer is not known yet.
1515 * @param peer Short identity of the peer.
1517 * @return Existing or newly created peer structure.
1520 GCP_get_short (const GNUNET_PEER_Id peer)
1522 return GCP_get (GNUNET_PEER_resolve2 (peer));
1527 * Try to connect to a peer on transport level.
1529 * @param cls Closure (peer).
1530 * @param tc TaskContext.
1533 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1535 struct CadetPeer *peer = cls;
1537 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1540 GNUNET_TRANSPORT_try_connect (transport_handle,
1541 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1546 * Try to establish a new connection to this peer (in its tunnel).
1547 * If the peer doesn't have any path to it yet, try to get one.
1548 * If the peer already has some path, send a CREATE CONNECTION towards it.
1550 * @param peer Peer to connect to.
1553 GCP_connect (struct CadetPeer *peer)
1555 struct CadetTunnel *t;
1556 struct CadetPeerPath *p;
1557 struct CadetConnection *c;
1560 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1562 /* If we have a current hello, try to connect using it. */
1563 GCP_try_connect (peer);
1567 rerun_search = GNUNET_NO;
1569 if (NULL != peer->path_head)
1571 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1572 p = peer_get_best_path (peer);
1578 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1581 c = GCT_use_path (t, p);
1584 /* This case can happen when the path includes a first hop that is
1585 * not yet known to be connected.
1587 * This happens quite often during testing when running cadet
1588 * under valgrind: core connect notifications come very late and the
1589 * DHT result has already come and created a valid path.
1590 * In this case, the peer->connections hashmap will be NULL and
1591 * tunnel_use_path will not be able to create a connection from that
1594 * Re-running the DHT GET should give core time to callback.
1596 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1597 * updating statistics about this issue.
1599 rerun_search = GNUNET_YES;
1603 GCC_send_create (c);
1609 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1613 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1615 GCD_search_stop (peer->search_h);
1616 peer->search_h = NULL;
1617 LOG (GNUNET_ERROR_TYPE_DEBUG,
1618 " Stopping DHT GET for peer %s\n",
1622 if (NULL == peer->search_h)
1624 const struct GNUNET_PeerIdentity *id;
1626 id = GNUNET_PEER_resolve2 (peer->id);
1627 LOG (GNUNET_ERROR_TYPE_DEBUG,
1628 " Starting DHT GET for peer %s\n", GCP_2s (peer));
1629 peer->search_h = GCD_search (id, &search_handler, peer);
1630 if (CADET_TUNNEL3_NEW == GCT_get_cstate (t))
1631 GCT_change_cstate (t, CADET_TUNNEL3_SEARCHING);
1637 * Chech whether there is a direct (core level) connection to peer.
1639 * @param peer Peer to check.
1641 * @return #GNUNET_YES if there is a direct connection.
1644 GCP_is_neighbor (const struct CadetPeer *peer)
1646 struct CadetPeerPath *path;
1648 if (NULL == peer->connections)
1651 for (path = peer->path_head; NULL != path; path = path->next)
1653 if (3 > path->length)
1657 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1663 * Create and initialize a new tunnel towards a peer, in case it has none.
1664 * In case the peer already has a tunnel, nothing is done.
1666 * Does not generate any traffic, just creates the local data structures.
1668 * @param peer Peer towards which to create the tunnel.
1671 GCP_add_tunnel (struct CadetPeer *peer)
1673 if (NULL != peer->tunnel)
1675 peer->tunnel = GCT_new (peer);
1680 * Add a connection to a neighboring peer.
1682 * Store that the peer is the first hop of the connection in one
1683 * direction and that on peer disconnect the connection must be
1684 * notified and destroyed, for it will no longer be valid.
1686 * @param peer Peer to add connection to.
1687 * @param c Connection to add.
1689 * @return GNUNET_OK on success.
1692 GCP_add_connection (struct CadetPeer *peer,
1693 struct CadetConnection *c)
1696 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding connection %s\n", GCC_2s (c));
1697 LOG (GNUNET_ERROR_TYPE_DEBUG, "to peer %s\n", GCP_2s (peer));
1699 if (NULL == peer->connections)
1702 LOG (GNUNET_ERROR_TYPE_DEBUG,
1703 "Peer %s is not a neighbor!\n",
1705 return GNUNET_SYSERR;
1707 LOG (GNUNET_ERROR_TYPE_DEBUG,
1708 "peer %s ok, has %u connections.\n",
1709 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1710 result = GNUNET_CONTAINER_multihashmap_put (peer->connections,
1713 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1714 LOG (GNUNET_ERROR_TYPE_DEBUG,
1715 " now has %u connections.\n",
1716 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1717 LOG (GNUNET_ERROR_TYPE_DEBUG, "result %u\n", result);
1724 * Add the path to the peer and update the path used to reach it in case this
1727 * @param peer Destination peer to add the path to.
1728 * @param path New path to add. Last peer must be the peer in arg 1.
1729 * Path will be either used of freed if already known.
1730 * @param trusted Do we trust that this path is real?
1732 * @return path if path was taken, pointer to existing duplicate if exists
1735 struct CadetPeerPath *
1736 GCP_add_path (struct CadetPeer *peer, struct CadetPeerPath *path,
1739 struct CadetPeerPath *aux;
1743 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1744 path->length, GCP_2s (peer));
1746 if ((NULL == peer) || (NULL == path))
1749 path_destroy (path);
1752 if (path->peers[path->length - 1] != peer->id)
1755 path_destroy (path);
1759 for (l = 1; l < path->length; l++)
1761 if (path->peers[l] == myid)
1763 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1764 for (l2 = 0; l2 < path->length - l; l2++)
1766 path->peers[l2] = path->peers[l + l2];
1770 path->peers = GNUNET_realloc (path->peers,
1771 path->length * sizeof (GNUNET_PEER_Id));
1775 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1777 if (2 >= path->length && GNUNET_NO == trusted)
1779 /* Only allow CORE to tell us about direct paths */
1780 path_destroy (path);
1784 l = path_get_length (path);
1787 path_destroy (path);
1791 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1792 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1794 l2 = path_get_length (aux);
1797 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1798 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1799 peer->path_tail, aux, path);
1800 if (NULL != peer->tunnel && 3 < GCT_count_connections (peer->tunnel))
1808 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1810 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1811 path_destroy (path);
1816 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
1818 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1819 if (NULL != peer->tunnel && 3 < GCT_count_connections (peer->tunnel))
1828 * Add the path to the origin peer and update the path used to reach it in case
1829 * this is the shortest.
1830 * The path is given in peer_info -> destination, therefore we turn the path
1831 * upside down first.
1833 * @param peer Peer to add the path to, being the origin of the path.
1834 * @param path New path to add after being inversed.
1835 * Path will be either used or freed.
1836 * @param trusted Do we trust that this path is real?
1838 * @return path if path was taken, pointer to existing duplicate if exists
1841 struct CadetPeerPath *
1842 GCP_add_path_to_origin (struct CadetPeer *peer,
1843 struct CadetPeerPath *path,
1849 return GCP_add_path (peer, path, trusted);
1854 * Adds a path to the info of all the peers in the path
1856 * @param p Path to process.
1857 * @param confirmed Whether we know if the path works or not.
1860 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
1864 /* TODO: invert and add */
1865 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1866 for (i++; i < p->length; i++)
1868 struct CadetPeer *aux;
1869 struct CadetPeerPath *copy;
1871 aux = GCP_get_short (p->peers[i]);
1872 copy = path_duplicate (p);
1873 copy->length = i + 1;
1874 GCP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1880 * Remove any path to the peer that has the extact same peers as the one given.
1882 * @param peer Peer to remove the path from.
1883 * @param path Path to remove. Is always destroyed .
1886 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
1888 struct CadetPeerPath *iter;
1889 struct CadetPeerPath *next;
1891 GNUNET_assert (myid == path->peers[0]);
1892 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1894 for (iter = peer->path_head; NULL != iter; iter = next)
1897 if (0 == memcmp (path->peers, iter->peers,
1898 sizeof (GNUNET_PEER_Id) * path->length))
1900 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
1902 path_destroy (iter);
1905 path_destroy (path);
1910 * Remove a connection from a neighboring peer.
1912 * @param peer Peer to remove connection from.
1913 * @param c Connection to remove.
1915 * @return GNUNET_OK on success.
1918 GCP_remove_connection (struct CadetPeer *peer,
1919 const struct CadetConnection *c)
1921 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing connection %s\n", GCC_2s (c));
1922 LOG (GNUNET_ERROR_TYPE_DEBUG, "from peer %s\n", GCP_2s (peer));
1924 if (NULL == peer || NULL == peer->connections)
1926 LOG (GNUNET_ERROR_TYPE_DEBUG,
1927 "Peer %s is not a neighbor!\n",
1929 return GNUNET_SYSERR;
1931 LOG (GNUNET_ERROR_TYPE_DEBUG,
1932 "peer %s ok, has %u connections.\n",
1933 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1935 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1941 * Start the DHT search for new paths towards the peer: we don't have
1942 * enough good connections.
1944 * @param peer Destination peer.
1947 GCP_start_search (struct CadetPeer *peer)
1949 if (NULL != peer->search_h)
1955 peer->search_h = GCD_search (GCP_get_id (peer), &search_handler, peer);
1960 * Stop the DHT search for new paths towards the peer: we already have
1961 * enough good connections.
1963 * @param peer Destination peer.
1966 GCP_stop_search (struct CadetPeer *peer)
1968 if (NULL == peer->search_h)
1973 GCD_search_stop (peer->search_h);
1974 peer->search_h = NULL;
1979 * Get the Full ID of a peer.
1981 * @param peer Peer to get from.
1983 * @return Full ID of peer.
1985 const struct GNUNET_PeerIdentity *
1986 GCP_get_id (const struct CadetPeer *peer)
1988 return GNUNET_PEER_resolve2 (peer->id);
1993 * Get the Short ID of a peer.
1995 * @param peer Peer to get from.
1997 * @return Short ID of peer.
2000 GCP_get_short_id (const struct CadetPeer *peer)
2013 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2016 if (NULL == t && NULL != peer->search_h)
2018 GCP_stop_search (peer);
2024 * Get the tunnel towards a peer.
2026 * @param peer Peer to get from.
2028 * @return Tunnel towards peer.
2030 struct CadetTunnel *
2031 GCP_get_tunnel (const struct CadetPeer *peer)
2033 return peer->tunnel;
2038 * Set the hello message.
2040 * @param peer Peer whose message to set.
2041 * @param hello Hello message.
2044 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2046 struct GNUNET_HELLO_Message *old;
2049 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2053 old = GCP_get_hello (peer);
2056 size = GNUNET_HELLO_size (hello);
2057 LOG (GNUNET_ERROR_TYPE_DEBUG, " new (%u bytes)\n", size);
2058 peer->hello = GNUNET_malloc (size);
2059 memcpy (peer->hello, hello, size);
2063 peer->hello = GNUNET_HELLO_merge (old, hello);
2064 LOG (GNUNET_ERROR_TYPE_DEBUG, " merge into %p (%u bytes)\n",
2065 peer->hello, GNUNET_HELLO_size (hello));
2072 * Get the hello message.
2074 * @param peer Peer whose message to get.
2076 * @return Hello message.
2078 struct GNUNET_HELLO_Message *
2079 GCP_get_hello (struct CadetPeer *peer)
2081 struct GNUNET_TIME_Absolute expiration;
2082 struct GNUNET_TIME_Relative remaining;
2084 if (NULL == peer->hello)
2087 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2088 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2089 if (0 == remaining.rel_value_us)
2091 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2092 GNUNET_STRINGS_absolute_time_to_string (expiration));
2093 GNUNET_free (peer->hello);
2101 * Try to connect to a peer on TRANSPORT level.
2103 * @param peer Peer to whom to connect.
2106 GCP_try_connect (struct CadetPeer *peer)
2108 struct GNUNET_HELLO_Message *hello;
2109 struct GNUNET_MessageHeader *mh;
2111 if (NULL == transport_handle)
2114 hello = GCP_get_hello (peer);
2118 mh = GNUNET_HELLO_get_header (hello);
2119 GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
2124 * Notify a peer that a link between two other peers is broken. If any path
2125 * used that link, eliminate it.
2127 * @param peer Peer affected by the change.
2128 * @param peer1 Peer whose link is broken.
2129 * @param peer2 Peer whose link is broken.
2132 GCP_notify_broken_link (struct CadetPeer *peer,
2133 struct GNUNET_PeerIdentity *peer1,
2134 struct GNUNET_PeerIdentity *peer2)
2136 struct CadetPeerPath *iter;
2137 struct CadetPeerPath *next;
2142 p1 = GNUNET_PEER_search (peer1);
2143 p2 = GNUNET_PEER_search (peer2);
2145 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2146 if (0 == p1 || 0 == p2)
2148 /* We don't even know them */
2152 for (iter = peer->path_head; NULL != iter; iter = next)
2155 for (i = 0; i < iter->length - 1; i++)
2157 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2158 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2163 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2166 path_invalidate (iter);
2174 * Count the number of known paths toward the peer.
2176 * @param peer Peer to get path info.
2178 * @return Number of known paths.
2181 GCP_count_paths (const struct CadetPeer *peer)
2183 struct CadetPeerPath *iter;
2186 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2194 * Iterate all known peers.
2196 * @param iter Iterator.
2197 * @param cls Closure for @c iter.
2200 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2202 GNUNET_CONTAINER_multipeermap_iterate (peers, iter, cls);
2207 * Get the static string for a peer ID.
2211 * @return Static string for it's ID.
2214 GCP_2s (const struct CadetPeer *peer)
2218 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));