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_TUNNEL_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 (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 (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;
1150 if (NULL == c && GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN != type)
1151 error_level = GNUNET_ERROR_TYPE_ERROR;
1153 error_level = GNUNET_ERROR_TYPE_INFO;
1155 "que %s (%s %u) on connection %s (%p) %s towards %s (size %u)\n",
1156 GC_m2s (type), GC_m2s (payload_type), payload_id,
1157 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1159 if (error_level == GNUNET_ERROR_TYPE_ERROR)
1161 if (NULL == peer->connections)
1163 /* We are not connected to this peer, ignore request. */
1164 LOG (GNUNET_ERROR_TYPE_WARNING, "%s not a neighbor\n", GCP_2s (peer));
1165 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1172 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1173 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1178 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1180 call_core = NULL == c ? GNUNET_YES : GCC_is_sendable (c, fwd);
1181 queue = GNUNET_new (struct CadetPeerQueue);
1184 queue->payload_type = payload_type;
1185 queue->payload_id = payload_id;
1190 queue->callback = cont;
1191 queue->callback_cls = cont_cls;
1194 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1199 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1200 call_core = GNUNET_YES;
1203 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1205 LOG (GNUNET_ERROR_TYPE_DEBUG,
1206 "calling core tmt rdy towards %s for %u bytes\n",
1207 GCP_2s (peer), size);
1208 peer->core_transmit =
1209 GNUNET_CORE_notify_transmit_ready (core_handle,
1210 GNUNET_NO, get_priority (queue),
1211 GNUNET_TIME_UNIT_FOREVER_REL,
1212 GNUNET_PEER_resolve2 (peer->id),
1216 queue->start_waiting = GNUNET_TIME_absolute_get ();
1218 else if (GNUNET_NO == call_core)
1220 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1226 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called\n",
1236 * Cancel all queued messages to a peer that belong to a certain connection.
1238 * @param peer Peer towards whom to cancel.
1239 * @param c Connection whose queued messages to cancel. Might be destroyed by
1240 * the sent continuation call.
1243 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c)
1245 struct CadetPeerQueue *q;
1246 struct CadetPeerQueue *next;
1247 struct CadetPeerQueue *prev;
1249 for (q = peer->queue_head; NULL != q; q = next)
1254 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP queue cancel %s\n", GC_m2s (q->type));
1255 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1261 GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1264 /* Get next from prev, q->next might be already freed:
1265 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1268 next = peer->queue_head;
1277 if (NULL == peer->queue_head)
1279 if (NULL != peer->core_transmit)
1281 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1282 peer->core_transmit = NULL;
1289 * Get the first transmittable message for a connection.
1291 * @param peer Neighboring peer.
1292 * @param c Connection.
1294 * @return First transmittable message.
1296 static struct CadetPeerQueue *
1297 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1299 struct CadetPeerQueue *q;
1301 for (q = peer->queue_head; NULL != q; q = q->next)
1305 if (queue_is_sendable (q))
1307 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1310 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1318 * Get the first message for a connection and unqueue it.
1320 * @param peer Neighboring peer.
1321 * @param c Connection.
1323 * @return First message for this connection.
1325 struct GNUNET_MessageHeader *
1326 GCP_connection_pop (struct CadetPeer *peer, struct CadetConnection *c)
1328 struct CadetPeerQueue *q;
1329 struct CadetPeerQueue *next;
1330 struct GNUNET_MessageHeader *msg;
1332 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection pop on %s\n", GCC_2s (c));
1333 for (q = peer->queue_head; NULL != q; q = next)
1340 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1341 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1342 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1343 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1344 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1345 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1346 GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1349 case GNUNET_MESSAGE_TYPE_CADET_KX:
1350 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1351 msg = (struct GNUNET_MessageHeader *) q->cls;
1352 GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1365 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1367 struct CadetPeerQueue *q;
1370 if (NULL != peer->core_transmit)
1372 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1373 return; /* Already unlocked */
1376 q = connection_get_first_message (peer, c);
1379 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1380 return; /* Nothing to transmit */
1384 peer->core_transmit =
1385 GNUNET_CORE_notify_transmit_ready (core_handle,
1386 GNUNET_NO, get_priority (q),
1387 GNUNET_TIME_UNIT_FOREVER_REL,
1388 GNUNET_PEER_resolve2 (peer->id),
1396 * Initialize the peer subsystem.
1398 * @param c Configuration.
1401 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1403 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1404 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1406 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1409 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1410 "CADET", "MAX_PEERS", "USING DEFAULT");
1415 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1422 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1423 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1424 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1425 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1426 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1429 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1430 NULL, /* Closure passed to CADET functions */
1431 &core_init, /* Call core_init once connected */
1432 &core_connect, /* Handle connects */
1433 &core_disconnect, /* remove peers on disconnects */
1434 NULL, /* Don't notify about all incoming messages */
1435 GNUNET_NO, /* For header only in notification */
1436 NULL, /* Don't notify about all outbound messages */
1437 GNUNET_NO, /* For header-only out notification */
1438 core_handlers); /* Register these handlers */
1440 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1442 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1443 /* Notify callbacks */
1448 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1449 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1450 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1451 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1452 transport_handle = NULL;
1457 if (NULL == core_handle)
1460 GNUNET_SCHEDULER_shutdown ();
1467 * Shut down the peer subsystem.
1472 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1474 if (core_handle != NULL)
1476 GNUNET_CORE_disconnect (core_handle);
1479 if (transport_handle != NULL)
1481 GNUNET_TRANSPORT_disconnect (transport_handle);
1482 transport_handle = NULL;
1484 GNUNET_PEER_change_rc (myid, -1);
1488 * Retrieve the CadetPeer stucture associated with the peer, create one
1489 * and insert it in the appropriate structures if the peer is not known yet.
1491 * @param peer_id Full identity of the peer.
1493 * @return Existing or newly created peer structure.
1496 GCP_get (const struct GNUNET_PeerIdentity *peer_id)
1498 struct CadetPeer *peer;
1500 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1503 peer = GNUNET_new (struct CadetPeer);
1504 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1506 peer_delete_oldest ();
1508 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1509 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1510 peer->id = GNUNET_PEER_intern (peer_id);
1512 peer->last_contact = GNUNET_TIME_absolute_get();
1519 * Retrieve the CadetPeer stucture associated with the peer, create one
1520 * and insert it in the appropriate structures if the peer is not known yet.
1522 * @param peer Short identity of the peer.
1524 * @return Existing or newly created peer structure.
1527 GCP_get_short (const GNUNET_PEER_Id peer)
1529 return GCP_get (GNUNET_PEER_resolve2 (peer));
1534 * Try to connect to a peer on transport level.
1536 * @param cls Closure (peer).
1537 * @param tc TaskContext.
1540 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1542 struct CadetPeer *peer = cls;
1544 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1547 GNUNET_TRANSPORT_try_connect (transport_handle,
1548 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1553 * Try to establish a new connection to this peer (in its tunnel).
1554 * If the peer doesn't have any path to it yet, try to get one.
1555 * If the peer already has some path, send a CREATE CONNECTION towards it.
1557 * @param peer Peer to connect to.
1560 GCP_connect (struct CadetPeer *peer)
1562 struct CadetTunnel *t;
1563 struct CadetPeerPath *p;
1564 struct CadetConnection *c;
1567 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1569 /* If we have a current hello, try to connect using it. */
1570 GCP_try_connect (peer);
1574 rerun_search = GNUNET_NO;
1576 if (NULL != peer->path_head)
1578 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1579 p = peer_get_best_path (peer);
1585 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1588 c = GCT_use_path (t, p);
1591 /* This case can happen when the path includes a first hop that is
1592 * not yet known to be connected.
1594 * This happens quite often during testing when running cadet
1595 * under valgrind: core connect notifications come very late and the
1596 * DHT result has already come and created a valid path.
1597 * In this case, the peer->connections hashmap will be NULL and
1598 * tunnel_use_path will not be able to create a connection from that
1601 * Re-running the DHT GET should give core time to callback.
1603 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1604 * updating statistics about this issue.
1606 rerun_search = GNUNET_YES;
1610 GCC_send_create (c);
1616 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1620 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1622 GCD_search_stop (peer->search_h);
1623 peer->search_h = NULL;
1624 LOG (GNUNET_ERROR_TYPE_DEBUG,
1625 " Stopping DHT GET for peer %s\n",
1629 if (NULL == peer->search_h)
1631 const struct GNUNET_PeerIdentity *id;
1633 id = GNUNET_PEER_resolve2 (peer->id);
1634 LOG (GNUNET_ERROR_TYPE_DEBUG,
1635 " Starting DHT GET for peer %s\n", GCP_2s (peer));
1636 peer->search_h = GCD_search (id, &search_handler, peer);
1637 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
1638 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
1644 * Chech whether there is a direct (core level) connection to peer.
1646 * @param peer Peer to check.
1648 * @return #GNUNET_YES if there is a direct connection.
1651 GCP_is_neighbor (const struct CadetPeer *peer)
1653 struct CadetPeerPath *path;
1655 if (NULL == peer->connections)
1658 for (path = peer->path_head; NULL != path; path = path->next)
1660 if (3 > path->length)
1664 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1670 * Create and initialize a new tunnel towards a peer, in case it has none.
1671 * In case the peer already has a tunnel, nothing is done.
1673 * Does not generate any traffic, just creates the local data structures.
1675 * @param peer Peer towards which to create the tunnel.
1678 GCP_add_tunnel (struct CadetPeer *peer)
1680 if (NULL != peer->tunnel)
1682 peer->tunnel = GCT_new (peer);
1687 * Add a connection to a neighboring peer.
1689 * Store that the peer is the first hop of the connection in one
1690 * direction and that on peer disconnect the connection must be
1691 * notified and destroyed, for it will no longer be valid.
1693 * @param peer Peer to add connection to.
1694 * @param c Connection to add.
1696 * @return GNUNET_OK on success.
1699 GCP_add_connection (struct CadetPeer *peer,
1700 struct CadetConnection *c)
1703 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding connection %s\n", GCC_2s (c));
1704 LOG (GNUNET_ERROR_TYPE_DEBUG, "to peer %s\n", GCP_2s (peer));
1706 if (NULL == peer->connections)
1709 LOG (GNUNET_ERROR_TYPE_DEBUG,
1710 "Peer %s is not a neighbor!\n",
1712 return GNUNET_SYSERR;
1714 LOG (GNUNET_ERROR_TYPE_DEBUG,
1715 "peer %s ok, has %u connections.\n",
1716 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1717 result = GNUNET_CONTAINER_multihashmap_put (peer->connections,
1720 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1721 LOG (GNUNET_ERROR_TYPE_DEBUG,
1722 " now has %u connections.\n",
1723 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1724 LOG (GNUNET_ERROR_TYPE_DEBUG, "result %u\n", result);
1731 * Add the path to the peer and update the path used to reach it in case this
1734 * @param peer Destination peer to add the path to.
1735 * @param path New path to add. Last peer must be the peer in arg 1.
1736 * Path will be either used of freed if already known.
1737 * @param trusted Do we trust that this path is real?
1739 * @return path if path was taken, pointer to existing duplicate if exists
1742 struct CadetPeerPath *
1743 GCP_add_path (struct CadetPeer *peer, struct CadetPeerPath *path,
1746 struct CadetPeerPath *aux;
1750 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1751 path->length, GCP_2s (peer));
1753 if ((NULL == peer) || (NULL == path))
1756 path_destroy (path);
1759 if (path->peers[path->length - 1] != peer->id)
1762 path_destroy (path);
1766 for (l = 1; l < path->length; l++)
1768 if (path->peers[l] == myid)
1770 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1771 for (l2 = 0; l2 < path->length - l; l2++)
1773 path->peers[l2] = path->peers[l + l2];
1777 path->peers = GNUNET_realloc (path->peers,
1778 path->length * sizeof (GNUNET_PEER_Id));
1782 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1784 if (2 >= path->length && GNUNET_NO == trusted)
1786 /* Only allow CORE to tell us about direct paths */
1787 path_destroy (path);
1791 l = path_get_length (path);
1794 path_destroy (path);
1798 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1799 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1801 l2 = path_get_length (aux);
1804 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1805 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1806 peer->path_tail, aux, path);
1807 if (NULL != peer->tunnel && 3 < GCT_count_connections (peer->tunnel))
1815 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1817 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1818 path_destroy (path);
1823 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
1825 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1826 if (NULL != peer->tunnel && 3 < GCT_count_connections (peer->tunnel))
1835 * Add the path to the origin peer and update the path used to reach it in case
1836 * this is the shortest.
1837 * The path is given in peer_info -> destination, therefore we turn the path
1838 * upside down first.
1840 * @param peer Peer to add the path to, being the origin of the path.
1841 * @param path New path to add after being inversed.
1842 * Path will be either used or freed.
1843 * @param trusted Do we trust that this path is real?
1845 * @return path if path was taken, pointer to existing duplicate if exists
1848 struct CadetPeerPath *
1849 GCP_add_path_to_origin (struct CadetPeer *peer,
1850 struct CadetPeerPath *path,
1856 return GCP_add_path (peer, path, trusted);
1861 * Adds a path to the info of all the peers in the path
1863 * @param p Path to process.
1864 * @param confirmed Whether we know if the path works or not.
1867 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
1871 /* TODO: invert and add */
1872 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1873 for (i++; i < p->length; i++)
1875 struct CadetPeer *aux;
1876 struct CadetPeerPath *copy;
1878 aux = GCP_get_short (p->peers[i]);
1879 copy = path_duplicate (p);
1880 copy->length = i + 1;
1881 GCP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1887 * Remove any path to the peer that has the extact same peers as the one given.
1889 * @param peer Peer to remove the path from.
1890 * @param path Path to remove. Is always destroyed .
1893 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
1895 struct CadetPeerPath *iter;
1896 struct CadetPeerPath *next;
1898 GNUNET_assert (myid == path->peers[0]);
1899 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1901 for (iter = peer->path_head; NULL != iter; iter = next)
1904 if (0 == memcmp (path->peers, iter->peers,
1905 sizeof (GNUNET_PEER_Id) * path->length))
1907 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
1909 path_destroy (iter);
1912 path_destroy (path);
1917 * Remove a connection from a neighboring peer.
1919 * @param peer Peer to remove connection from.
1920 * @param c Connection to remove.
1922 * @return GNUNET_OK on success.
1925 GCP_remove_connection (struct CadetPeer *peer,
1926 const struct CadetConnection *c)
1928 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing connection %s\n", GCC_2s (c));
1929 LOG (GNUNET_ERROR_TYPE_DEBUG, "from peer %s\n", GCP_2s (peer));
1931 if (NULL == peer || NULL == peer->connections)
1933 LOG (GNUNET_ERROR_TYPE_DEBUG,
1934 "Peer %s is not a neighbor!\n",
1936 return GNUNET_SYSERR;
1938 LOG (GNUNET_ERROR_TYPE_DEBUG,
1939 "peer %s ok, has %u connections.\n",
1940 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1942 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1948 * Start the DHT search for new paths towards the peer: we don't have
1949 * enough good connections.
1951 * @param peer Destination peer.
1954 GCP_start_search (struct CadetPeer *peer)
1956 if (NULL != peer->search_h)
1962 peer->search_h = GCD_search (GCP_get_id (peer), &search_handler, peer);
1967 * Stop the DHT search for new paths towards the peer: we already have
1968 * enough good connections.
1970 * @param peer Destination peer.
1973 GCP_stop_search (struct CadetPeer *peer)
1975 if (NULL == peer->search_h)
1980 GCD_search_stop (peer->search_h);
1981 peer->search_h = NULL;
1986 * Get the Full ID of a peer.
1988 * @param peer Peer to get from.
1990 * @return Full ID of peer.
1992 const struct GNUNET_PeerIdentity *
1993 GCP_get_id (const struct CadetPeer *peer)
1995 return GNUNET_PEER_resolve2 (peer->id);
2000 * Get the Short ID of a peer.
2002 * @param peer Peer to get from.
2004 * @return Short ID of peer.
2007 GCP_get_short_id (const struct CadetPeer *peer)
2020 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2023 if (NULL == t && NULL != peer->search_h)
2025 GCP_stop_search (peer);
2031 * Get the tunnel towards a peer.
2033 * @param peer Peer to get from.
2035 * @return Tunnel towards peer.
2037 struct CadetTunnel *
2038 GCP_get_tunnel (const struct CadetPeer *peer)
2040 return peer->tunnel;
2045 * Set the hello message.
2047 * @param peer Peer whose message to set.
2048 * @param hello Hello message.
2051 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2053 struct GNUNET_HELLO_Message *old;
2056 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2060 old = GCP_get_hello (peer);
2063 size = GNUNET_HELLO_size (hello);
2064 LOG (GNUNET_ERROR_TYPE_DEBUG, " new (%u bytes)\n", size);
2065 peer->hello = GNUNET_malloc (size);
2066 memcpy (peer->hello, hello, size);
2070 peer->hello = GNUNET_HELLO_merge (old, hello);
2071 LOG (GNUNET_ERROR_TYPE_DEBUG, " merge into %p (%u bytes)\n",
2072 peer->hello, GNUNET_HELLO_size (hello));
2079 * Get the hello message.
2081 * @param peer Peer whose message to get.
2083 * @return Hello message.
2085 struct GNUNET_HELLO_Message *
2086 GCP_get_hello (struct CadetPeer *peer)
2088 struct GNUNET_TIME_Absolute expiration;
2089 struct GNUNET_TIME_Relative remaining;
2091 if (NULL == peer->hello)
2094 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2095 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2096 if (0 == remaining.rel_value_us)
2098 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2099 GNUNET_STRINGS_absolute_time_to_string (expiration));
2100 GNUNET_free (peer->hello);
2108 * Try to connect to a peer on TRANSPORT level.
2110 * @param peer Peer to whom to connect.
2113 GCP_try_connect (struct CadetPeer *peer)
2115 struct GNUNET_HELLO_Message *hello;
2116 struct GNUNET_MessageHeader *mh;
2118 if (NULL == transport_handle)
2121 hello = GCP_get_hello (peer);
2125 mh = GNUNET_HELLO_get_header (hello);
2126 GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
2131 * Notify a peer that a link between two other peers is broken. If any path
2132 * used that link, eliminate it.
2134 * @param peer Peer affected by the change.
2135 * @param peer1 Peer whose link is broken.
2136 * @param peer2 Peer whose link is broken.
2139 GCP_notify_broken_link (struct CadetPeer *peer,
2140 struct GNUNET_PeerIdentity *peer1,
2141 struct GNUNET_PeerIdentity *peer2)
2143 struct CadetPeerPath *iter;
2144 struct CadetPeerPath *next;
2149 p1 = GNUNET_PEER_search (peer1);
2150 p2 = GNUNET_PEER_search (peer2);
2152 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2153 if (0 == p1 || 0 == p2)
2155 /* We don't even know them */
2159 for (iter = peer->path_head; NULL != iter; iter = next)
2162 for (i = 0; i < iter->length - 1; i++)
2164 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2165 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2170 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2173 path_invalidate (iter);
2181 * Count the number of known paths toward the peer.
2183 * @param peer Peer to get path info.
2185 * @return Number of known paths.
2188 GCP_count_paths (const struct CadetPeer *peer)
2190 struct CadetPeerPath *iter;
2193 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2201 * Iterate all known peers.
2203 * @param iter Iterator.
2204 * @param cls Closure for @c iter.
2207 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2209 GNUNET_CONTAINER_multipeermap_iterate (peers, iter, cls);
2214 * Get the static string for a peer ID.
2218 * @return Static string for it's ID.
2221 GCP_2s (const struct CadetPeer *peer)
2225 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2230 * Log all kinds of info about a peer.
2235 GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
2237 struct CadetPeerPath *path;
2238 struct CadetPeerQueue *q;
2243 LOG (level, "PPP DEBUG PEER NULL\n");
2247 LOG (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
2248 for (path = p->path_head; NULL != path; path = path->next)
2253 LOG (level, "PPP path: %s\n", s);
2257 LOG (level, "PPP core transmit handle %p\n", p->core_transmit);
2258 conns = GNUNET_CONTAINER_multihashmap_size (p->connections);
2259 LOG (level, "PPP # connections over link to peer: %u\n", conns);
2260 LOG (level, "PPP queue length: %u\n", p->queue_n);
2261 for (q = p->queue_head; NULL != q; q = q->next)
2263 LOG (level, "PPP - %s [payload %s, %u] on connection %s, %u bytes\n",
2264 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
2265 GCC_2s (q->c), q->size);
2267 LOG (level, "PPP DEBUG END\n");