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 /******************************************************************************/
234 queue_debug (struct CadetPeer *peer)
236 struct CadetPeerQueue *q;
238 LOG (GNUNET_ERROR_TYPE_DEBUG, "QQQ Messages queued towards %s\n", GCP_2s (peer));
239 LOG (GNUNET_ERROR_TYPE_DEBUG, "QQQ core tmt rdy: %p\n", peer->core_transmit);
241 for (q = peer->queue_head; NULL != q; q = q->next)
243 LOG (GNUNET_ERROR_TYPE_DEBUG, "QQQ - %s %s on %s\n",
244 GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
247 LOG (GNUNET_ERROR_TYPE_DEBUG, "QQQ End queued towards %s\n", GCP_2s (peer));
251 /******************************************************************************/
252 /***************************** CORE HELPERS *********************************/
253 /******************************************************************************/
257 * Iterator to notify all connections of a broken link. Mark connections
258 * to destroy after all traffic has been sent.
260 * @param cls Closure (peer disconnected).
261 * @param key Current key code (peer id).
262 * @param value Value in the hash map (connection).
264 * @return #GNUNET_YES to continue to iterate.
267 notify_broken (void *cls,
268 const struct GNUNET_HashCode *key,
271 struct CadetPeer *peer = cls;
272 struct CadetConnection *c = value;
274 LOG (GNUNET_ERROR_TYPE_DEBUG, " notifying %s due to %s\n",
275 GCC_2s (c), GCP_2s (peer));
276 GCC_notify_broken (c, peer);
283 * Remove the direct path to the peer.
285 * @param peer Peer to remove the direct path from.
288 static struct CadetPeerPath *
289 pop_direct_path (struct CadetPeer *peer)
291 struct CadetPeerPath *iter;
293 for (iter = peer->path_head; NULL != iter; iter = iter->next)
295 if (2 <= iter->length)
297 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
305 /******************************************************************************/
306 /***************************** CORE CALLBACKS *********************************/
307 /******************************************************************************/
310 * Method called whenever a given peer connects.
313 * @param peer peer identity this notification is about
316 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
318 struct CadetPeer *mp;
319 struct CadetPeerPath *path;
322 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
326 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s (self)\n", own_id);
331 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s <= %s\n",
332 own_id, GNUNET_i2s (peer));
334 path->peers[1] = mp->id;
335 GNUNET_PEER_change_rc (mp->id, 1);
336 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
338 path->peers[0] = myid;
339 GNUNET_PEER_change_rc (myid, 1);
340 GCP_add_path (mp, path, GNUNET_YES);
342 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
344 if (NULL != GCP_get_tunnel (mp) &&
345 0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer))
355 * Method called whenever a peer disconnects.
358 * @param peer peer identity this notification is about
361 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
364 struct CadetPeerPath *direct_path;
367 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
368 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
375 LOG (GNUNET_ERROR_TYPE_INFO, "DISCONNECTED %s (self)\n", own_id);
377 LOG (GNUNET_ERROR_TYPE_DEBUG, "DISCONNECTED %s <= %s\n",
378 own_id, GNUNET_i2s (peer));
379 direct_path = pop_direct_path (p);
380 GNUNET_CONTAINER_multihashmap_iterate (p->connections, ¬ify_broken, p);
381 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
382 p->connections = NULL;
383 if (NULL != p->core_transmit)
385 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
386 p->core_transmit = NULL;
388 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
390 path_destroy (direct_path);
396 * Functions to handle messages from core
398 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
399 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
400 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
401 sizeof (struct GNUNET_CADET_ConnectionACK)},
402 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
403 sizeof (struct GNUNET_CADET_ConnectionBroken)},
404 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
405 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
406 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
407 sizeof (struct GNUNET_CADET_ACK)},
408 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
409 sizeof (struct GNUNET_CADET_Poll)},
410 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
411 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
417 * To be called on core init/fail.
419 * @param cls Closure (config)
420 * @param identity the public identity of this peer
423 core_init (void *cls,
424 const struct GNUNET_PeerIdentity *identity)
426 const struct GNUNET_CONFIGURATION_Handle *c = cls;
429 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
430 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
432 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
433 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
434 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
435 GNUNET_CORE_disconnect (core_handle);
436 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
437 NULL, /* Closure passed to CADET functions */
438 &core_init, /* Call core_init once connected */
439 &core_connect, /* Handle connects */
440 &core_disconnect, /* remove peers on disconnects */
441 NULL, /* Don't notify about all incoming messages */
442 GNUNET_NO, /* For header only in notification */
443 NULL, /* Don't notify about all outbound messages */
444 GNUNET_NO, /* For header-only out notification */
445 core_handlers); /* Register these handlers */
455 * Core callback to write a pre-constructed data packet to core buffer
457 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
458 * @param size Number of bytes available in buf.
459 * @param buf Where the to write the message.
461 * @return number of bytes written to buf
464 send_core_data_raw (void *cls, size_t size, void *buf)
466 struct GNUNET_MessageHeader *msg = cls;
469 GNUNET_assert (NULL != msg);
470 total_size = ntohs (msg->size);
472 if (total_size > size)
477 memcpy (buf, msg, total_size);
484 * Function to send a create connection message to a peer.
486 * @param c Connection to create.
487 * @param size number of bytes available in buf
488 * @param buf where the callee should write the message
489 * @return number of bytes written to buf
492 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
494 struct GNUNET_CADET_ConnectionCreate *msg;
495 struct GNUNET_PeerIdentity *peer_ptr;
496 const struct CadetPeerPath *p = GCC_get_path (c);
503 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
505 sizeof (struct GNUNET_CADET_ConnectionCreate) +
506 p->length * sizeof (struct GNUNET_PeerIdentity);
508 if (size < size_needed || NULL == buf)
513 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
514 msg->header.size = htons (size_needed);
515 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
516 msg->cid = *GCC_get_id (c);
518 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
519 for (i = 0; i < p->length; i++)
521 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
524 LOG (GNUNET_ERROR_TYPE_DEBUG,
525 "CONNECTION CREATE (%u bytes long) sent!\n",
532 * Creates a path ack message in buf and frees all unused resources.
534 * @param c Connection to send an ACK on.
535 * @param size number of bytes available in buf
536 * @param buf where the callee should write the message
538 * @return number of bytes written to buf
541 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
543 struct GNUNET_CADET_ConnectionACK *msg = buf;
545 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
546 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
551 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
552 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
553 msg->cid = *GCC_get_id (c);
555 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
556 return sizeof (struct GNUNET_CADET_ConnectionACK);
560 /******************************************************************************/
561 /******************************** STATIC ***********************************/
562 /******************************************************************************/
566 * Get priority for a queued message.
568 * @param q Queued message
570 * @return CORE priority to use.
572 static enum GNUNET_CORE_Priority
573 get_priority (struct CadetPeerQueue *q)
575 enum GNUNET_CORE_Priority low;
576 enum GNUNET_CORE_Priority high;
581 return GNUNET_CORE_PRIO_BACKGROUND;
584 /* Relayed traffic has lower priority, our own traffic has higher */
585 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
587 low = GNUNET_CORE_PRIO_BEST_EFFORT;
588 high = GNUNET_CORE_PRIO_URGENT;
592 low = GNUNET_CORE_PRIO_URGENT;
593 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
596 /* Bulky payload has lower priority, control traffic has higher. */
597 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type)
605 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
608 * @param key current key code
609 * @param value value in the hash map
610 * @return #GNUNET_YES if we should continue to iterate,
614 shutdown_tunnel (void *cls,
615 const struct GNUNET_PeerIdentity *key,
618 struct CadetPeer *p = value;
619 struct CadetTunnel *t = p->tunnel;
628 * Destroy the peer_info and free any allocated resources linked to it
630 * @param peer The peer_info to destroy.
632 * @return GNUNET_OK on success
635 peer_destroy (struct CadetPeer *peer)
637 struct GNUNET_PeerIdentity id;
638 struct CadetPeerPath *p;
639 struct CadetPeerPath *nextp;
641 GNUNET_PEER_resolve (peer->id, &id);
642 GNUNET_PEER_change_rc (peer->id, -1);
644 LOG (GNUNET_ERROR_TYPE_WARNING, "destroying peer %s\n", GNUNET_i2s (&id));
647 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
650 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
652 if (NULL != peer->search_h)
654 GCD_search_stop (peer->search_h);
660 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
664 GCT_destroy_empty (peer->tunnel);
671 * Returns if peer is used (has a tunnel or is neighbor).
673 * @param peer Peer to check.
675 * @return #GNUNET_YES if peer is in use.
678 peer_is_used (struct CadetPeer *peer)
680 struct CadetPeerPath *p;
682 if (NULL != peer->tunnel)
685 for (p = peer->path_head; NULL != p; p = p->next)
695 * Iterator over all the peers to get the oldest timestamp.
697 * @param cls Closure (unsued).
698 * @param key ID of the peer.
699 * @param value Peer_Info of the peer.
702 peer_get_oldest (void *cls,
703 const struct GNUNET_PeerIdentity *key,
706 struct CadetPeer *p = value;
707 struct GNUNET_TIME_Absolute *abs = cls;
709 /* Don't count active peers */
710 if (GNUNET_YES == peer_is_used (p))
713 if (abs->abs_value_us < p->last_contact.abs_value_us)
714 abs->abs_value_us = p->last_contact.abs_value_us;
721 * Iterator over all the peers to remove the oldest entry.
723 * @param cls Closure (unsued).
724 * @param key ID of the peer.
725 * @param value Peer_Info of the peer.
728 peer_timeout (void *cls,
729 const struct GNUNET_PeerIdentity *key,
732 struct CadetPeer *p = value;
733 struct GNUNET_TIME_Absolute *abs = cls;
735 LOG (GNUNET_ERROR_TYPE_WARNING,
736 "peer %s timeout\n", GNUNET_i2s (key));
738 if (p->last_contact.abs_value_us == abs->abs_value_us &&
739 GNUNET_NO == peer_is_used (p))
749 * Delete oldest unused peer.
752 peer_delete_oldest (void)
754 struct GNUNET_TIME_Absolute abs;
756 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
758 GNUNET_CONTAINER_multipeermap_iterate (peers,
761 GNUNET_CONTAINER_multipeermap_iterate (peers,
768 * Choose the best (yet unused) path towards a peer,
769 * considering the tunnel properties.
771 * @param peer The destination peer.
773 * @return Best current known path towards the peer, if any.
775 static struct CadetPeerPath *
776 peer_get_best_path (const struct CadetPeer *peer)
778 struct CadetPeerPath *best_p;
779 struct CadetPeerPath *p;
780 unsigned int best_cost;
783 best_cost = UINT_MAX;
785 for (p = peer->path_head; NULL != p; p = p->next)
787 if (GNUNET_NO == path_is_valid (p))
788 continue; /* Don't use invalid paths. */
789 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
790 continue; /* If path is already in use, skip it. */
792 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
803 * Is this queue element sendable?
805 * - All management traffic is always sendable.
806 * - For payload traffic, check the connection flow control.
808 * @param q Queue element to inspect.
810 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
813 queue_is_sendable (struct CadetPeerQueue *q)
815 /* Is PID-independent? */
818 case GNUNET_MESSAGE_TYPE_CADET_ACK:
819 case GNUNET_MESSAGE_TYPE_CADET_POLL:
820 case GNUNET_MESSAGE_TYPE_CADET_KX:
821 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
822 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
823 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
824 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
827 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
834 return GCC_is_sendable (q->c, q->fwd);
839 * Get first sendable message.
841 * @param peer The destination peer.
843 * @return First transmittable message, if any. Otherwise, NULL.
845 static struct CadetPeerQueue *
846 peer_get_first_message (const struct CadetPeer *peer)
848 struct CadetPeerQueue *q;
850 for (q = peer->queue_head; NULL != q; q = q->next)
852 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking %p towards %s\n", q, GCC_2s (q->c));
853 if (queue_is_sendable (q))
862 * Function to process paths received for a new peer addition. The recorded
863 * paths form the initial tunnel, which can be optimized later.
864 * Called on each result obtained for the DHT search.
870 search_handler (void *cls, const struct CadetPeerPath *path)
872 struct CadetPeer *peer = cls;
873 unsigned int connection_count;
875 GCP_add_path_to_all (path, GNUNET_NO);
877 /* Count connections */
878 connection_count = GCT_count_connections (peer->tunnel);
880 /* If we already have 3 (or more (?!)) connections, it's enough */
881 if (3 <= connection_count)
884 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
886 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
895 * Core callback to write a queued packet to core buffer
897 * @param cls Closure (peer info).
898 * @param size Number of bytes available in buf.
899 * @param buf Where the to write the message.
901 * @return number of bytes written to buf
904 queue_send (void *cls, size_t size, void *buf)
906 struct CadetPeer *peer = cls;
907 struct CadetConnection *c;
908 struct CadetPeerQueue *queue;
909 const struct GNUNET_PeerIdentity *dst_id;
914 peer->core_transmit = NULL;
915 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
916 GCP_2s (peer), size);
918 if (NULL == buf || 0 == size)
920 LOG (GNUNET_ERROR_TYPE_DEBUG, "Buffer size 0.\n");
925 queue = peer_get_first_message (peer);
928 GNUNET_assert (0); /* Core tmt_rdy should've been canceled */
933 dst_id = GNUNET_PEER_resolve2 (peer->id);
934 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s %s\n",
935 GCC_2s (c), GC_f2s(queue->fwd));
936 /* Check if buffer size is enough for the message */
937 if (queue->size > size)
939 LOG (GNUNET_ERROR_TYPE_WARNING, "not enough room (%u vs %u), reissue\n",
941 peer->core_transmit =
942 GNUNET_CORE_notify_transmit_ready (core_handle,
943 GNUNET_NO, get_priority (queue),
944 GNUNET_TIME_UNIT_FOREVER_REL,
951 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok\n", queue->size);
956 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
957 pid = GCC_get_pid (queue->c, queue->fwd);
958 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload ID %u\n", pid);
959 data_size = send_core_data_raw (queue->cls, size, buf);
960 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (pid);
962 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
963 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
964 case GNUNET_MESSAGE_TYPE_CADET_KX:
965 case GNUNET_MESSAGE_TYPE_CADET_ACK:
966 case GNUNET_MESSAGE_TYPE_CADET_POLL:
967 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
968 data_size = send_core_data_raw (queue->cls, size, buf);
970 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
971 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
972 if (GCC_is_origin (c, GNUNET_YES))
973 data_size = send_core_connection_create (c, size, buf);
975 data_size = send_core_data_raw (queue->cls, size, buf);
977 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
978 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
979 if (GCC_is_origin (c, GNUNET_NO) ||
980 GCC_is_origin (c, GNUNET_YES))
981 data_size = send_core_connection_ack (c, size, buf);
983 data_size = send_core_data_raw (queue->cls, size, buf);
985 case GNUNET_MESSAGE_TYPE_CADET_DATA:
986 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
987 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
988 /* This should be encapsulted */
994 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
998 if (0 < drop_percent &&
999 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1001 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s on connection %s\n",
1002 GC_m2s (queue->type), GCC_2s (c));
1007 LOG (GNUNET_ERROR_TYPE_INFO,
1008 "snd %s (%s %u) on connection %s (%p) %s (size %u)\n",
1009 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1010 queue->payload_type, GCC_2s (c), c, GC_f2s (queue->fwd), data_size);
1013 /* Free queue, but cls was freed by send_core_* */
1014 GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1016 /* If more data in queue, send next */
1017 queue = peer_get_first_message (peer);
1020 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data!\n");
1021 if (NULL == peer->core_transmit)
1023 peer->core_transmit =
1024 GNUNET_CORE_notify_transmit_ready (core_handle,
1025 GNUNET_NO, get_priority (queue),
1026 GNUNET_TIME_UNIT_FOREVER_REL,
1031 queue->start_waiting = GNUNET_TIME_absolute_get ();
1035 LOG (GNUNET_ERROR_TYPE_DEBUG,
1036 "* tmt rdy called somewhere else\n");
1038 // GCC_start_poll (); FIXME needed?
1042 // GCC_stop_poll(); FIXME needed?
1045 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", data_size);
1051 /******************************************************************************/
1052 /******************************** API ***********************************/
1053 /******************************************************************************/
1057 * Free a transmission that was already queued with all resources
1058 * associated to the request.
1060 * @param queue Queue handler to cancel.
1061 * @param clear_cls Is it necessary to free associated cls?
1062 * @param sent Was it really sent? (Could have been canceled)
1063 * @param pid PID, if relevant (was sent and was a payload message).
1066 GCP_queue_destroy (struct CadetPeerQueue *queue, int clear_cls,
1067 int sent, uint32_t pid)
1069 struct CadetPeer *peer;
1073 if (GNUNET_YES == clear_cls)
1075 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy type %s\n",
1076 GC_m2s (queue->type));
1077 switch (queue->type)
1079 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1080 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1082 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1083 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1084 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1085 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
1086 case GNUNET_MESSAGE_TYPE_CADET_KX:
1087 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1088 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1089 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1090 GNUNET_free_non_null (queue->cls);
1095 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1096 GC_m2s (queue->type));
1099 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1101 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1102 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1107 if (NULL != queue->callback)
1109 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback\n");
1110 queue->callback (queue->callback_cls,
1111 queue->c, sent, queue->type, pid,
1112 queue->fwd, queue->size,
1113 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
1116 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1118 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1119 peer->core_transmit = NULL;
1122 GNUNET_free (queue);
1127 * @brief Queue and pass message to core when possible.
1129 * @param peer Peer towards which to queue the message.
1130 * @param cls Closure (@c type dependant). It will be used by queue_send to
1131 * build the message to be sent if not already prebuilt.
1132 * @param type Type of the message, 0 for a raw message.
1133 * @param size Size of the message.
1134 * @param c Connection this message belongs to (can be NULL).
1135 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1136 * @param cont Continuation to be called once CORE has taken the message.
1137 * @param cont_cls Closure for @c cont.
1139 * @return Handle to cancel the message before it is sent. Once cont is called
1140 * message has been sent and therefore the handle is no longer valid.
1142 struct CadetPeerQueue *
1143 GCP_queue_add (struct CadetPeer *peer, void *cls, uint16_t type,
1144 uint16_t payload_type, uint32_t payload_id, size_t size,
1145 struct CadetConnection *c, int fwd,
1146 GCP_sent cont, void *cont_cls)
1148 struct CadetPeerQueue *queue;
1153 if (NULL == c && GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN != type)
1154 error_level = GNUNET_ERROR_TYPE_ERROR;
1156 error_level = GNUNET_ERROR_TYPE_INFO;
1158 "que %s (%s %u) on connection %s (%p) %s towards %s (size %u)\n",
1159 GC_m2s (type), GC_m2s (payload_type), payload_id,
1160 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1162 if (error_level == GNUNET_ERROR_TYPE_ERROR)
1164 if (NULL == peer->connections)
1166 /* We are not connected to this peer, ignore request. */
1167 LOG (GNUNET_ERROR_TYPE_WARNING, "%s not a neighbor\n", GCP_2s (peer));
1168 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1175 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1176 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1183 call_core = NULL == c ? GNUNET_YES : GCC_is_sendable (c, fwd);
1184 queue = GNUNET_new (struct CadetPeerQueue);
1187 queue->payload_type = payload_type;
1188 queue->payload_id = payload_id;
1193 queue->callback = cont;
1194 queue->callback_cls = cont_cls;
1197 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1202 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1203 call_core = GNUNET_YES;
1206 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1208 LOG (GNUNET_ERROR_TYPE_DEBUG,
1209 "calling core tmt rdy towards %s for %u bytes\n",
1210 GCP_2s (peer), size);
1211 peer->core_transmit =
1212 GNUNET_CORE_notify_transmit_ready (core_handle,
1213 GNUNET_NO, get_priority (queue),
1214 GNUNET_TIME_UNIT_FOREVER_REL,
1215 GNUNET_PEER_resolve2 (peer->id),
1219 queue->start_waiting = GNUNET_TIME_absolute_get ();
1221 else if (GNUNET_NO == call_core)
1223 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1229 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called\n",
1239 * Cancel all queued messages to a peer that belong to a certain connection.
1241 * @param peer Peer towards whom to cancel.
1242 * @param c Connection whose queued messages to cancel. Might be destroyed by
1243 * the sent continuation call.
1246 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c)
1248 struct CadetPeerQueue *q;
1249 struct CadetPeerQueue *next;
1250 struct CadetPeerQueue *prev;
1252 for (q = peer->queue_head; NULL != q; q = next)
1257 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP queue cancel %s\n", GC_m2s (q->type));
1258 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1264 GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1267 /* Get next from prev, q->next might be already freed:
1268 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1271 next = peer->queue_head;
1280 if (NULL == peer->queue_head)
1282 if (NULL != peer->core_transmit)
1284 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1285 peer->core_transmit = NULL;
1292 * Get the first transmittable message for a connection.
1294 * @param peer Neighboring peer.
1295 * @param c Connection.
1297 * @return First transmittable message.
1299 static struct CadetPeerQueue *
1300 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1302 struct CadetPeerQueue *q;
1304 for (q = peer->queue_head; NULL != q; q = q->next)
1308 if (queue_is_sendable (q))
1310 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1313 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1321 * Get the first message for a connection and unqueue it.
1323 * @param peer Neighboring peer.
1324 * @param c Connection.
1326 * @return First message for this connection.
1328 struct GNUNET_MessageHeader *
1329 GCP_connection_pop (struct CadetPeer *peer, struct CadetConnection *c)
1331 struct CadetPeerQueue *q;
1332 struct CadetPeerQueue *next;
1333 struct GNUNET_MessageHeader *msg;
1335 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection pop on %s\n", GCC_2s (c));
1336 for (q = peer->queue_head; NULL != q; q = next)
1343 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1344 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1345 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1346 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1347 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1348 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1349 GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1352 case GNUNET_MESSAGE_TYPE_CADET_KX:
1353 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1354 msg = (struct GNUNET_MessageHeader *) q->cls;
1355 GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1368 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1370 struct CadetPeerQueue *q;
1373 if (NULL != peer->core_transmit)
1375 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1376 return; /* Already unlocked */
1379 q = connection_get_first_message (peer, c);
1382 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1383 return; /* Nothing to transmit */
1387 peer->core_transmit =
1388 GNUNET_CORE_notify_transmit_ready (core_handle,
1389 GNUNET_NO, get_priority (q),
1390 GNUNET_TIME_UNIT_FOREVER_REL,
1391 GNUNET_PEER_resolve2 (peer->id),
1399 * Initialize the peer subsystem.
1401 * @param c Configuration.
1404 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1406 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1407 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1409 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1412 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1413 "CADET", "MAX_PEERS", "USING DEFAULT");
1418 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1425 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1426 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1427 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1428 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1429 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1432 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1433 NULL, /* Closure passed to CADET functions */
1434 &core_init, /* Call core_init once connected */
1435 &core_connect, /* Handle connects */
1436 &core_disconnect, /* remove peers on disconnects */
1437 NULL, /* Don't notify about all incoming messages */
1438 GNUNET_NO, /* For header only in notification */
1439 NULL, /* Don't notify about all outbound messages */
1440 GNUNET_NO, /* For header-only out notification */
1441 core_handlers); /* Register these handlers */
1443 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1445 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1446 /* Notify callbacks */
1451 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1452 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1453 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1454 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1455 transport_handle = NULL;
1460 if (NULL == core_handle)
1463 GNUNET_SCHEDULER_shutdown ();
1471 * Shut down the peer subsystem.
1476 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1478 if (core_handle != NULL)
1480 GNUNET_CORE_disconnect (core_handle);
1483 if (transport_handle != NULL)
1485 GNUNET_TRANSPORT_disconnect (transport_handle);
1486 transport_handle = NULL;
1488 GNUNET_PEER_change_rc (myid, -1);
1493 * Retrieve the CadetPeer stucture associated with the peer, create one
1494 * and insert it in the appropriate structures if the peer is not known yet.
1496 * @param peer_id Full identity of the peer.
1498 * @return Existing or newly created peer structure.
1501 GCP_get (const struct GNUNET_PeerIdentity *peer_id)
1503 struct CadetPeer *peer;
1505 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1508 peer = GNUNET_new (struct CadetPeer);
1509 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1511 peer_delete_oldest ();
1513 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1514 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1515 peer->id = GNUNET_PEER_intern (peer_id);
1517 peer->last_contact = GNUNET_TIME_absolute_get ();
1524 * Retrieve the CadetPeer stucture associated with the peer, create one
1525 * and insert it in the appropriate structures if the peer is not known yet.
1527 * @param peer Short identity of the peer.
1529 * @return Existing or newly created peer structure.
1532 GCP_get_short (const GNUNET_PEER_Id peer)
1534 return GCP_get (GNUNET_PEER_resolve2 (peer));
1539 * Try to connect to a peer on transport level.
1541 * @param cls Closure (peer).
1542 * @param tc TaskContext.
1545 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1547 struct CadetPeer *peer = cls;
1549 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1552 GNUNET_TRANSPORT_try_connect (transport_handle,
1553 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1558 * Try to establish a new connection to this peer (in its tunnel).
1559 * If the peer doesn't have any path to it yet, try to get one.
1560 * If the peer already has some path, send a CREATE CONNECTION towards it.
1562 * @param peer Peer to connect to.
1565 GCP_connect (struct CadetPeer *peer)
1567 struct CadetTunnel *t;
1568 struct CadetPeerPath *p;
1569 struct CadetConnection *c;
1572 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1574 /* If we have a current hello, try to connect using it. */
1575 GCP_try_connect (peer);
1579 rerun_search = GNUNET_NO;
1581 if (NULL != peer->path_head)
1583 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1584 p = peer_get_best_path (peer);
1590 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1593 c = GCT_use_path (t, p);
1596 /* This case can happen when the path includes a first hop that is
1597 * not yet known to be connected.
1599 * This happens quite often during testing when running cadet
1600 * under valgrind: core connect notifications come very late and the
1601 * DHT result has already come and created a valid path.
1602 * In this case, the peer->connections hashmap will be NULL and
1603 * tunnel_use_path will not be able to create a connection from that
1606 * Re-running the DHT GET should give core time to callback.
1608 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1609 * updating statistics about this issue.
1611 rerun_search = GNUNET_YES;
1615 GCC_send_create (c);
1621 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1625 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1627 GCD_search_stop (peer->search_h);
1628 peer->search_h = NULL;
1629 LOG (GNUNET_ERROR_TYPE_DEBUG,
1630 " Stopping DHT GET for peer %s\n",
1634 if (NULL == peer->search_h)
1636 const struct GNUNET_PeerIdentity *id;
1638 id = GNUNET_PEER_resolve2 (peer->id);
1639 LOG (GNUNET_ERROR_TYPE_DEBUG,
1640 " Starting DHT GET for peer %s\n", GCP_2s (peer));
1641 peer->search_h = GCD_search (id, &search_handler, peer);
1642 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
1643 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
1649 * Chech whether there is a direct (core level) connection to peer.
1651 * @param peer Peer to check.
1653 * @return #GNUNET_YES if there is a direct connection.
1656 GCP_is_neighbor (const struct CadetPeer *peer)
1658 struct CadetPeerPath *path;
1660 if (NULL == peer->connections)
1663 for (path = peer->path_head; NULL != path; path = path->next)
1665 if (3 > path->length)
1669 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1675 * Create and initialize a new tunnel towards a peer, in case it has none.
1676 * In case the peer already has a tunnel, nothing is done.
1678 * Does not generate any traffic, just creates the local data structures.
1680 * @param peer Peer towards which to create the tunnel.
1683 GCP_add_tunnel (struct CadetPeer *peer)
1685 if (NULL != peer->tunnel)
1687 peer->tunnel = GCT_new (peer);
1692 * Add a connection to a neighboring peer.
1694 * Store that the peer is the first hop of the connection in one
1695 * direction and that on peer disconnect the connection must be
1696 * notified and destroyed, for it will no longer be valid.
1698 * @param peer Peer to add connection to.
1699 * @param c Connection to add.
1701 * @return GNUNET_OK on success.
1704 GCP_add_connection (struct CadetPeer *peer,
1705 struct CadetConnection *c)
1708 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding connection %s\n", GCC_2s (c));
1709 LOG (GNUNET_ERROR_TYPE_DEBUG, "to peer %s\n", GCP_2s (peer));
1711 if (NULL == peer->connections)
1714 LOG (GNUNET_ERROR_TYPE_DEBUG,
1715 "Peer %s is not a neighbor!\n",
1717 return GNUNET_SYSERR;
1719 LOG (GNUNET_ERROR_TYPE_DEBUG,
1720 "peer %s ok, has %u connections.\n",
1721 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1722 result = GNUNET_CONTAINER_multihashmap_put (peer->connections,
1725 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1726 LOG (GNUNET_ERROR_TYPE_DEBUG,
1727 " now has %u connections.\n",
1728 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1729 LOG (GNUNET_ERROR_TYPE_DEBUG, "result %u\n", result);
1736 * Add the path to the peer and update the path used to reach it in case this
1739 * @param peer Destination peer to add the path to.
1740 * @param path New path to add. Last peer must be the peer in arg 1.
1741 * Path will be either used of freed if already known.
1742 * @param trusted Do we trust that this path is real?
1744 * @return path if path was taken, pointer to existing duplicate if exists
1747 struct CadetPeerPath *
1748 GCP_add_path (struct CadetPeer *peer, struct CadetPeerPath *path,
1751 struct CadetPeerPath *aux;
1755 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1756 path->length, GCP_2s (peer));
1758 if ((NULL == peer) || (NULL == path))
1761 path_destroy (path);
1764 if (path->peers[path->length - 1] != peer->id)
1767 path_destroy (path);
1771 for (l = 1; l < path->length; l++)
1773 if (path->peers[l] == myid)
1775 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1776 for (l2 = 0; l2 < path->length - l; l2++)
1778 path->peers[l2] = path->peers[l + l2];
1782 path->peers = GNUNET_realloc (path->peers,
1783 path->length * sizeof (GNUNET_PEER_Id));
1787 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1789 if (2 >= path->length && GNUNET_NO == trusted)
1791 /* Only allow CORE to tell us about direct paths */
1792 path_destroy (path);
1796 l = path_get_length (path);
1799 path_destroy (path);
1803 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1804 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1806 l2 = path_get_length (aux);
1809 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1810 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1811 peer->path_tail, aux, path);
1812 if (NULL != peer->tunnel && 3 < GCT_count_connections (peer->tunnel))
1820 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1822 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1823 path_destroy (path);
1828 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
1830 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1831 if (NULL != peer->tunnel && 3 < GCT_count_connections (peer->tunnel))
1840 * Add the path to the origin peer and update the path used to reach it in case
1841 * this is the shortest.
1842 * The path is given in peer_info -> destination, therefore we turn the path
1843 * upside down first.
1845 * @param peer Peer to add the path to, being the origin of the path.
1846 * @param path New path to add after being inversed.
1847 * Path will be either used or freed.
1848 * @param trusted Do we trust that this path is real?
1850 * @return path if path was taken, pointer to existing duplicate if exists
1853 struct CadetPeerPath *
1854 GCP_add_path_to_origin (struct CadetPeer *peer,
1855 struct CadetPeerPath *path,
1861 return GCP_add_path (peer, path, trusted);
1866 * Adds a path to the info of all the peers in the path
1868 * @param p Path to process.
1869 * @param confirmed Whether we know if the path works or not.
1872 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
1876 /* TODO: invert and add */
1877 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1878 for (i++; i < p->length; i++)
1880 struct CadetPeer *aux;
1881 struct CadetPeerPath *copy;
1883 aux = GCP_get_short (p->peers[i]);
1884 copy = path_duplicate (p);
1885 copy->length = i + 1;
1886 GCP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1892 * Remove any path to the peer that has the extact same peers as the one given.
1894 * @param peer Peer to remove the path from.
1895 * @param path Path to remove. Is always destroyed .
1898 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
1900 struct CadetPeerPath *iter;
1901 struct CadetPeerPath *next;
1903 GNUNET_assert (myid == path->peers[0]);
1904 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1906 for (iter = peer->path_head; NULL != iter; iter = next)
1909 if (0 == memcmp (path->peers, iter->peers,
1910 sizeof (GNUNET_PEER_Id) * path->length))
1912 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
1914 path_destroy (iter);
1917 path_destroy (path);
1922 * Remove a connection from a neighboring peer.
1924 * @param peer Peer to remove connection from.
1925 * @param c Connection to remove.
1927 * @return GNUNET_OK on success.
1930 GCP_remove_connection (struct CadetPeer *peer,
1931 const struct CadetConnection *c)
1933 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing connection %s\n", GCC_2s (c));
1934 LOG (GNUNET_ERROR_TYPE_DEBUG, "from peer %s\n", GCP_2s (peer));
1936 if (NULL == peer || NULL == peer->connections)
1938 LOG (GNUNET_ERROR_TYPE_DEBUG,
1939 "Peer %s is not a neighbor!\n",
1941 return GNUNET_SYSERR;
1943 LOG (GNUNET_ERROR_TYPE_DEBUG,
1944 "peer %s ok, has %u connections.\n",
1945 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1947 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1953 * Start the DHT search for new paths towards the peer: we don't have
1954 * enough good connections.
1956 * @param peer Destination peer.
1959 GCP_start_search (struct CadetPeer *peer)
1961 if (NULL != peer->search_h)
1967 peer->search_h = GCD_search (GCP_get_id (peer), &search_handler, peer);
1972 * Stop the DHT search for new paths towards the peer: we already have
1973 * enough good connections.
1975 * @param peer Destination peer.
1978 GCP_stop_search (struct CadetPeer *peer)
1980 if (NULL == peer->search_h)
1985 GCD_search_stop (peer->search_h);
1986 peer->search_h = NULL;
1991 * Get the Full ID of a peer.
1993 * @param peer Peer to get from.
1995 * @return Full ID of peer.
1997 const struct GNUNET_PeerIdentity *
1998 GCP_get_id (const struct CadetPeer *peer)
2000 return GNUNET_PEER_resolve2 (peer->id);
2005 * Get the Short ID of a peer.
2007 * @param peer Peer to get from.
2009 * @return Short ID of peer.
2012 GCP_get_short_id (const struct CadetPeer *peer)
2025 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2028 if (NULL == t && NULL != peer->search_h)
2030 GCP_stop_search (peer);
2036 * Get the tunnel towards a peer.
2038 * @param peer Peer to get from.
2040 * @return Tunnel towards peer.
2042 struct CadetTunnel *
2043 GCP_get_tunnel (const struct CadetPeer *peer)
2045 return peer->tunnel;
2050 * Set the hello message.
2052 * @param peer Peer whose message to set.
2053 * @param hello Hello message.
2056 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2058 struct GNUNET_HELLO_Message *old;
2061 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2065 old = GCP_get_hello (peer);
2068 size = GNUNET_HELLO_size (hello);
2069 LOG (GNUNET_ERROR_TYPE_DEBUG, " new (%u bytes)\n", size);
2070 peer->hello = GNUNET_malloc (size);
2071 memcpy (peer->hello, hello, size);
2075 peer->hello = GNUNET_HELLO_merge (old, hello);
2076 LOG (GNUNET_ERROR_TYPE_DEBUG, " merge into %p (%u bytes)\n",
2077 peer->hello, GNUNET_HELLO_size (hello));
2084 * Get the hello message.
2086 * @param peer Peer whose message to get.
2088 * @return Hello message.
2090 struct GNUNET_HELLO_Message *
2091 GCP_get_hello (struct CadetPeer *peer)
2093 struct GNUNET_TIME_Absolute expiration;
2094 struct GNUNET_TIME_Relative remaining;
2096 if (NULL == peer->hello)
2099 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2100 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2101 if (0 == remaining.rel_value_us)
2103 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2104 GNUNET_STRINGS_absolute_time_to_string (expiration));
2105 GNUNET_free (peer->hello);
2113 * Try to connect to a peer on TRANSPORT level.
2115 * @param peer Peer to whom to connect.
2118 GCP_try_connect (struct CadetPeer *peer)
2120 struct GNUNET_HELLO_Message *hello;
2121 struct GNUNET_MessageHeader *mh;
2123 if (NULL == transport_handle)
2126 hello = GCP_get_hello (peer);
2130 mh = GNUNET_HELLO_get_header (hello);
2131 GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
2136 * Notify a peer that a link between two other peers is broken. If any path
2137 * used that link, eliminate it.
2139 * @param peer Peer affected by the change.
2140 * @param peer1 Peer whose link is broken.
2141 * @param peer2 Peer whose link is broken.
2144 GCP_notify_broken_link (struct CadetPeer *peer,
2145 struct GNUNET_PeerIdentity *peer1,
2146 struct GNUNET_PeerIdentity *peer2)
2148 struct CadetPeerPath *iter;
2149 struct CadetPeerPath *next;
2154 p1 = GNUNET_PEER_search (peer1);
2155 p2 = GNUNET_PEER_search (peer2);
2157 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2158 if (0 == p1 || 0 == p2)
2160 /* We don't even know them */
2164 for (iter = peer->path_head; NULL != iter; iter = next)
2167 for (i = 0; i < iter->length - 1; i++)
2169 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2170 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2175 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2178 path_invalidate (iter);
2186 * Count the number of known paths toward the peer.
2188 * @param peer Peer to get path info.
2190 * @return Number of known paths.
2193 GCP_count_paths (const struct CadetPeer *peer)
2195 struct CadetPeerPath *iter;
2198 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2206 * Iterate all known peers.
2208 * @param iter Iterator.
2209 * @param cls Closure for @c iter.
2212 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2214 GNUNET_CONTAINER_multipeermap_iterate (peers, iter, cls);
2219 * Get the static string for a peer ID.
2223 * @return Static string for it's ID.
2226 GCP_2s (const struct CadetPeer *peer)
2230 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
2235 * Log all kinds of info about a peer.
2240 GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
2242 struct CadetPeerPath *path;
2243 struct CadetPeerQueue *q;
2248 LOG (level, "PPP DEBUG PEER NULL\n");
2252 LOG (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
2253 LOG (level, "PPP last contact %s\n",
2254 GNUNET_STRINGS_absolute_time_to_string (p->last_contact));
2255 for (path = p->path_head; NULL != path; path = path->next)
2260 LOG (level, "PPP path: %s\n", s);
2264 LOG (level, "PPP core transmit handle %p\n", p->core_transmit);
2265 LOG (level, "PPP DHT GET handle %p\n", p->search_h);
2266 if (NULL != p->connections)
2267 conns = GNUNET_CONTAINER_multihashmap_size (p->connections);
2270 LOG (level, "PPP # connections over link to peer: %u\n", conns);
2271 LOG (level, "PPP queue length: %u\n", p->queue_n);
2272 for (q = p->queue_head; NULL != q; q = q->next)
2274 LOG (level, "PPP - %s [payload %s, %u] on connection %s, %u bytes\n",
2275 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
2276 GCC_2s (q->c), q->size);
2278 LOG (level, "PPP DEBUG END\n");