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 * Log all kinds of info about the queueing status of a peer.
234 queue_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
236 struct CadetPeerQueue *q;
238 LOG (level, "QQQ Message queue towards %s\n", GCP_2s (p));
239 LOG (level, "QQQ queue length: %u\n", p->queue_n);
240 LOG (level, "QQQ core tmt rdy: %p\n", p->core_transmit);
242 for (q = p->queue_head; NULL != q; q = q->next)
244 LOG (level, "QQQ - %s %s on %s\n",
245 GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
246 LOG (level, "QQQ payload %s, %u\n",
247 GC_m2s (q->payload_type), q->payload_id);
248 LOG (level, "QQQ size: %u bytes\n", q->size);
251 LOG (level, "QQQ End queue towards %s\n", GCP_2s (p));
256 * Log all kinds of info about a peer.
261 GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
263 struct CadetPeerPath *path;
268 LOG (level, "PPP DEBUG PEER NULL\n");
272 LOG (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
273 LOG (level, "PPP last contact %s\n",
274 GNUNET_STRINGS_absolute_time_to_string (p->last_contact));
275 for (path = p->path_head; NULL != path; path = path->next)
280 LOG (level, "PPP path: %s\n", s);
284 LOG (level, "PPP core transmit handle %p\n", p->core_transmit);
285 LOG (level, "PPP DHT GET handle %p\n", p->search_h);
286 if (NULL != p->connections)
287 conns = GNUNET_CONTAINER_multihashmap_size (p->connections);
290 LOG (level, "PPP # connections over link to peer: %u\n", conns);
291 queue_debug (p, level);
292 LOG (level, "PPP DEBUG END\n");
296 /******************************************************************************/
297 /***************************** CORE HELPERS *********************************/
298 /******************************************************************************/
302 * Iterator to notify all connections of a broken link. Mark connections
303 * to destroy after all traffic has been sent.
305 * @param cls Closure (peer disconnected).
306 * @param key Current key code (peer id).
307 * @param value Value in the hash map (connection).
309 * @return #GNUNET_YES to continue to iterate.
312 notify_broken (void *cls,
313 const struct GNUNET_HashCode *key,
316 struct CadetPeer *peer = cls;
317 struct CadetConnection *c = value;
319 LOG (GNUNET_ERROR_TYPE_DEBUG, " notifying %s due to %s\n",
320 GCC_2s (c), GCP_2s (peer));
321 GCC_notify_broken (c, peer);
328 * Remove the direct path to the peer.
330 * @param peer Peer to remove the direct path from.
333 static struct CadetPeerPath *
334 pop_direct_path (struct CadetPeer *peer)
336 struct CadetPeerPath *iter;
338 for (iter = peer->path_head; NULL != iter; iter = iter->next)
340 if (2 <= iter->length)
342 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
350 /******************************************************************************/
351 /***************************** CORE CALLBACKS *********************************/
352 /******************************************************************************/
355 * Method called whenever a given peer connects.
358 * @param peer peer identity this notification is about
361 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
363 struct CadetPeer *mp;
364 struct CadetPeerPath *path;
367 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
371 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s (self)\n", own_id);
376 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s <= %s\n",
377 own_id, GNUNET_i2s (peer));
379 path->peers[1] = mp->id;
380 GNUNET_PEER_change_rc (mp->id, 1);
381 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
383 path->peers[0] = myid;
384 GNUNET_PEER_change_rc (myid, 1);
385 GCP_add_path (mp, path, GNUNET_YES);
387 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
389 if (NULL != GCP_get_tunnel (mp) &&
390 0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer))
400 * Method called whenever a peer disconnects.
403 * @param peer peer identity this notification is about
406 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
409 struct CadetPeerPath *direct_path;
412 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
413 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
420 LOG (GNUNET_ERROR_TYPE_INFO, "DISCONNECTED %s (self)\n", own_id);
422 LOG (GNUNET_ERROR_TYPE_DEBUG, "DISCONNECTED %s <= %s\n",
423 own_id, GNUNET_i2s (peer));
424 direct_path = pop_direct_path (p);
425 GNUNET_CONTAINER_multihashmap_iterate (p->connections, ¬ify_broken, p);
426 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
427 p->connections = NULL;
428 if (NULL != p->core_transmit)
430 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
431 p->core_transmit = NULL;
433 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
435 path_destroy (direct_path);
441 * Functions to handle messages from core
443 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
444 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
445 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
446 sizeof (struct GNUNET_CADET_ConnectionACK)},
447 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
448 sizeof (struct GNUNET_CADET_ConnectionBroken)},
449 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
450 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
451 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
452 sizeof (struct GNUNET_CADET_ACK)},
453 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
454 sizeof (struct GNUNET_CADET_Poll)},
455 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
456 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
462 * To be called on core init/fail.
464 * @param cls Closure (config)
465 * @param identity the public identity of this peer
468 core_init (void *cls,
469 const struct GNUNET_PeerIdentity *identity)
471 const struct GNUNET_CONFIGURATION_Handle *c = cls;
474 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
475 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
477 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
478 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
479 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
480 GNUNET_CORE_disconnect (core_handle);
481 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
482 NULL, /* Closure passed to CADET functions */
483 &core_init, /* Call core_init once connected */
484 &core_connect, /* Handle connects */
485 &core_disconnect, /* remove peers on disconnects */
486 NULL, /* Don't notify about all incoming messages */
487 GNUNET_NO, /* For header only in notification */
488 NULL, /* Don't notify about all outbound messages */
489 GNUNET_NO, /* For header-only out notification */
490 core_handlers); /* Register these handlers */
500 * Core callback to write a pre-constructed data packet to core buffer
502 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
503 * @param size Number of bytes available in buf.
504 * @param buf Where the to write the message.
506 * @return number of bytes written to buf
509 send_core_data_raw (void *cls, size_t size, void *buf)
511 struct GNUNET_MessageHeader *msg = cls;
514 GNUNET_assert (NULL != msg);
515 total_size = ntohs (msg->size);
517 if (total_size > size)
522 memcpy (buf, msg, total_size);
529 * Function to send a create connection message to a peer.
531 * @param c Connection to create.
532 * @param size number of bytes available in buf
533 * @param buf where the callee should write the message
534 * @return number of bytes written to buf
537 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
539 struct GNUNET_CADET_ConnectionCreate *msg;
540 struct GNUNET_PeerIdentity *peer_ptr;
541 const struct CadetPeerPath *p = GCC_get_path (c);
548 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
550 sizeof (struct GNUNET_CADET_ConnectionCreate) +
551 p->length * sizeof (struct GNUNET_PeerIdentity);
553 if (size < size_needed || NULL == buf)
558 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
559 msg->header.size = htons (size_needed);
560 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
561 msg->cid = *GCC_get_id (c);
563 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
564 for (i = 0; i < p->length; i++)
566 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
569 LOG (GNUNET_ERROR_TYPE_DEBUG,
570 "CONNECTION CREATE (%u bytes long) sent!\n",
577 * Creates a path ack message in buf and frees all unused resources.
579 * @param c Connection to send an ACK on.
580 * @param size number of bytes available in buf
581 * @param buf where the callee should write the message
583 * @return number of bytes written to buf
586 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
588 struct GNUNET_CADET_ConnectionACK *msg = buf;
590 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
591 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
596 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
597 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
598 msg->cid = *GCC_get_id (c);
600 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
601 return sizeof (struct GNUNET_CADET_ConnectionACK);
605 /******************************************************************************/
606 /******************************** STATIC ***********************************/
607 /******************************************************************************/
611 * Get priority for a queued message.
613 * @param q Queued message
615 * @return CORE priority to use.
617 static enum GNUNET_CORE_Priority
618 get_priority (struct CadetPeerQueue *q)
620 enum GNUNET_CORE_Priority low;
621 enum GNUNET_CORE_Priority high;
626 return GNUNET_CORE_PRIO_BACKGROUND;
629 /* Relayed traffic has lower priority, our own traffic has higher */
630 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
632 low = GNUNET_CORE_PRIO_BEST_EFFORT;
633 high = GNUNET_CORE_PRIO_URGENT;
637 low = GNUNET_CORE_PRIO_URGENT;
638 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
641 /* Bulky payload has lower priority, control traffic has higher. */
642 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type)
650 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
653 * @param key current key code
654 * @param value value in the hash map
655 * @return #GNUNET_YES if we should continue to iterate,
659 shutdown_tunnel (void *cls,
660 const struct GNUNET_PeerIdentity *key,
663 struct CadetPeer *p = value;
664 struct CadetTunnel *t = p->tunnel;
673 * Destroy the peer_info and free any allocated resources linked to it
675 * @param peer The peer_info to destroy.
677 * @return GNUNET_OK on success
680 peer_destroy (struct CadetPeer *peer)
682 struct GNUNET_PeerIdentity id;
683 struct CadetPeerPath *p;
684 struct CadetPeerPath *nextp;
686 GNUNET_PEER_resolve (peer->id, &id);
687 GNUNET_PEER_change_rc (peer->id, -1);
689 LOG (GNUNET_ERROR_TYPE_WARNING, "destroying peer %s\n", GNUNET_i2s (&id));
692 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
695 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
697 if (NULL != peer->search_h)
699 GCD_search_stop (peer->search_h);
705 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
709 GCT_destroy_empty (peer->tunnel);
716 * Returns if peer is used (has a tunnel or is neighbor).
718 * @param peer Peer to check.
720 * @return #GNUNET_YES if peer is in use.
723 peer_is_used (struct CadetPeer *peer)
725 struct CadetPeerPath *p;
727 if (NULL != peer->tunnel)
730 for (p = peer->path_head; NULL != p; p = p->next)
740 * Iterator over all the peers to get the oldest timestamp.
742 * @param cls Closure (unsued).
743 * @param key ID of the peer.
744 * @param value Peer_Info of the peer.
747 peer_get_oldest (void *cls,
748 const struct GNUNET_PeerIdentity *key,
751 struct CadetPeer *p = value;
752 struct GNUNET_TIME_Absolute *abs = cls;
754 /* Don't count active peers */
755 if (GNUNET_YES == peer_is_used (p))
758 if (abs->abs_value_us < p->last_contact.abs_value_us)
759 abs->abs_value_us = p->last_contact.abs_value_us;
766 * Iterator over all the peers to remove the oldest entry.
768 * @param cls Closure (unsued).
769 * @param key ID of the peer.
770 * @param value Peer_Info of the peer.
773 peer_timeout (void *cls,
774 const struct GNUNET_PeerIdentity *key,
777 struct CadetPeer *p = value;
778 struct GNUNET_TIME_Absolute *abs = cls;
780 LOG (GNUNET_ERROR_TYPE_WARNING,
781 "peer %s timeout\n", GNUNET_i2s (key));
783 if (p->last_contact.abs_value_us == abs->abs_value_us &&
784 GNUNET_NO == peer_is_used (p))
794 * Delete oldest unused peer.
797 peer_delete_oldest (void)
799 struct GNUNET_TIME_Absolute abs;
801 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
803 GNUNET_CONTAINER_multipeermap_iterate (peers,
806 GNUNET_CONTAINER_multipeermap_iterate (peers,
813 * Choose the best (yet unused) path towards a peer,
814 * considering the tunnel properties.
816 * @param peer The destination peer.
818 * @return Best current known path towards the peer, if any.
820 static struct CadetPeerPath *
821 peer_get_best_path (const struct CadetPeer *peer)
823 struct CadetPeerPath *best_p;
824 struct CadetPeerPath *p;
825 unsigned int best_cost;
828 best_cost = UINT_MAX;
830 for (p = peer->path_head; NULL != p; p = p->next)
832 if (GNUNET_NO == path_is_valid (p))
833 continue; /* Don't use invalid paths. */
834 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
835 continue; /* If path is already in use, skip it. */
837 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
848 * Is this queue element sendable?
850 * - All management traffic is always sendable.
851 * - For payload traffic, check the connection flow control.
853 * @param q Queue element to inspect.
855 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
858 queue_is_sendable (struct CadetPeerQueue *q)
860 /* Is PID-independent? */
863 case GNUNET_MESSAGE_TYPE_CADET_ACK:
864 case GNUNET_MESSAGE_TYPE_CADET_POLL:
865 case GNUNET_MESSAGE_TYPE_CADET_KX:
866 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
867 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
868 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
869 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
872 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
879 return GCC_is_sendable (q->c, q->fwd);
884 * Get first sendable message.
886 * @param peer The destination peer.
888 * @return First transmittable message, if any. Otherwise, NULL.
890 static struct CadetPeerQueue *
891 peer_get_first_message (const struct CadetPeer *peer)
893 struct CadetPeerQueue *q;
895 for (q = peer->queue_head; NULL != q; q = q->next)
897 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking %p towards %s\n", q, GCC_2s (q->c));
898 if (queue_is_sendable (q))
907 * Function to process paths received for a new peer addition. The recorded
908 * paths form the initial tunnel, which can be optimized later.
909 * Called on each result obtained for the DHT search.
915 search_handler (void *cls, const struct CadetPeerPath *path)
917 struct CadetPeer *peer = cls;
918 unsigned int connection_count;
920 GCP_add_path_to_all (path, GNUNET_NO);
922 /* Count connections */
923 connection_count = GCT_count_connections (peer->tunnel);
925 /* If we already have 3 (or more (?!)) connections, it's enough */
926 if (3 <= connection_count)
929 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
931 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
940 * Core callback to write a queued packet to core buffer
942 * @param cls Closure (peer info).
943 * @param size Number of bytes available in buf.
944 * @param buf Where the to write the message.
946 * @return number of bytes written to buf
949 queue_send (void *cls, size_t size, void *buf)
951 struct CadetPeer *peer = cls;
952 struct CadetConnection *c;
953 struct CadetPeerQueue *queue;
954 const struct GNUNET_PeerIdentity *dst_id;
959 peer->core_transmit = NULL;
960 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
961 GCP_2s (peer), size);
963 if (NULL == buf || 0 == size)
965 LOG (GNUNET_ERROR_TYPE_DEBUG, "Buffer size 0.\n");
970 queue = peer_get_first_message (peer);
973 GNUNET_assert (0); /* Core tmt_rdy should've been canceled */
978 dst_id = GNUNET_PEER_resolve2 (peer->id);
979 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s %s\n",
980 GCC_2s (c), GC_f2s(queue->fwd));
981 /* Check if buffer size is enough for the message */
982 if (queue->size > size)
984 LOG (GNUNET_ERROR_TYPE_WARNING, "not enough room (%u vs %u), reissue\n",
986 peer->core_transmit =
987 GNUNET_CORE_notify_transmit_ready (core_handle,
988 GNUNET_NO, get_priority (queue),
989 GNUNET_TIME_UNIT_FOREVER_REL,
996 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok\n", queue->size);
1001 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1002 pid = GCC_get_pid (queue->c, queue->fwd);
1003 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload ID %u\n", pid);
1004 data_size = send_core_data_raw (queue->cls, size, buf);
1005 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (pid);
1007 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1008 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1009 case GNUNET_MESSAGE_TYPE_CADET_KX:
1010 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1011 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1012 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1013 data_size = send_core_data_raw (queue->cls, size, buf);
1015 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1016 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1017 if (GCC_is_origin (c, GNUNET_YES))
1018 data_size = send_core_connection_create (c, size, buf);
1020 data_size = send_core_data_raw (queue->cls, size, buf);
1022 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1023 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1024 if (GCC_is_origin (c, GNUNET_NO) ||
1025 GCC_is_origin (c, GNUNET_YES))
1026 data_size = send_core_connection_ack (c, size, buf);
1028 data_size = send_core_data_raw (queue->cls, size, buf);
1030 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1031 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1032 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1033 /* This should be encapsulted */
1039 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1043 if (0 < drop_percent &&
1044 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1046 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s on connection %s\n",
1047 GC_m2s (queue->type), GCC_2s (c));
1052 LOG (GNUNET_ERROR_TYPE_INFO,
1053 "snd %s (%s %u) on connection %s (%p) %s (size %u)\n",
1054 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1055 queue->payload_type, GCC_2s (c), c, GC_f2s (queue->fwd), data_size);
1058 /* Free queue, but cls was freed by send_core_* */
1059 GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1061 /* If more data in queue, send next */
1062 queue = peer_get_first_message (peer);
1065 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data!\n");
1066 if (NULL == peer->core_transmit)
1068 peer->core_transmit =
1069 GNUNET_CORE_notify_transmit_ready (core_handle,
1070 GNUNET_NO, get_priority (queue),
1071 GNUNET_TIME_UNIT_FOREVER_REL,
1076 queue->start_waiting = GNUNET_TIME_absolute_get ();
1080 LOG (GNUNET_ERROR_TYPE_DEBUG,
1081 "* tmt rdy called somewhere else\n");
1083 // GCC_start_poll (); FIXME needed?
1087 // GCC_stop_poll(); FIXME needed?
1090 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", data_size);
1091 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1096 /******************************************************************************/
1097 /******************************** API ***********************************/
1098 /******************************************************************************/
1102 * Free a transmission that was already queued with all resources
1103 * associated to the request.
1105 * @param queue Queue handler to cancel.
1106 * @param clear_cls Is it necessary to free associated cls?
1107 * @param sent Was it really sent? (Could have been canceled)
1108 * @param pid PID, if relevant (was sent and was a payload message).
1111 GCP_queue_destroy (struct CadetPeerQueue *queue, int clear_cls,
1112 int sent, uint32_t pid)
1114 struct CadetPeer *peer;
1118 if (GNUNET_YES == clear_cls)
1120 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy type %s\n",
1121 GC_m2s (queue->type));
1122 switch (queue->type)
1124 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1125 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1127 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1128 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1129 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1130 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
1131 case GNUNET_MESSAGE_TYPE_CADET_KX:
1132 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1133 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1134 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1135 GNUNET_free_non_null (queue->cls);
1140 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1141 GC_m2s (queue->type));
1144 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1146 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1147 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1152 if (NULL != queue->callback)
1154 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback\n");
1155 queue->callback (queue->callback_cls,
1156 queue->c, sent, queue->type, pid,
1157 queue->fwd, queue->size,
1158 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
1161 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1163 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1164 peer->core_transmit = NULL;
1167 GNUNET_free (queue);
1172 * @brief Queue and pass message to core when possible.
1174 * @param peer Peer towards which to queue the message.
1175 * @param cls Closure (@c type dependant). It will be used by queue_send to
1176 * build the message to be sent if not already prebuilt.
1177 * @param type Type of the message, 0 for a raw message.
1178 * @param size Size of the message.
1179 * @param c Connection this message belongs to (can be NULL).
1180 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1181 * @param cont Continuation to be called once CORE has taken the message.
1182 * @param cont_cls Closure for @c cont.
1184 * @return Handle to cancel the message before it is sent. Once cont is called
1185 * message has been sent and therefore the handle is no longer valid.
1187 struct CadetPeerQueue *
1188 GCP_queue_add (struct CadetPeer *peer, void *cls, uint16_t type,
1189 uint16_t payload_type, uint32_t payload_id, size_t size,
1190 struct CadetConnection *c, int fwd,
1191 GCP_sent cont, void *cont_cls)
1193 struct CadetPeerQueue *queue;
1198 if (NULL == c && GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN != type)
1199 error_level = GNUNET_ERROR_TYPE_ERROR;
1201 error_level = GNUNET_ERROR_TYPE_INFO;
1203 "que %s (%s %u) on connection %s (%p) %s towards %s (size %u)\n",
1204 GC_m2s (type), GC_m2s (payload_type), payload_id,
1205 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1207 if (error_level == GNUNET_ERROR_TYPE_ERROR)
1209 if (NULL == peer->connections)
1211 /* We are not connected to this peer, ignore request. */
1212 LOG (GNUNET_ERROR_TYPE_WARNING, "%s not a neighbor\n", GCP_2s (peer));
1213 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1220 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1221 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1226 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1228 call_core = NULL == c ? GNUNET_YES : GCC_is_sendable (c, fwd);
1229 queue = GNUNET_new (struct CadetPeerQueue);
1232 queue->payload_type = payload_type;
1233 queue->payload_id = payload_id;
1238 queue->callback = cont;
1239 queue->callback_cls = cont_cls;
1242 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1247 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1248 call_core = GNUNET_YES;
1251 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1253 LOG (GNUNET_ERROR_TYPE_DEBUG,
1254 "calling core tmt rdy towards %s for %u bytes\n",
1255 GCP_2s (peer), size);
1256 peer->core_transmit =
1257 GNUNET_CORE_notify_transmit_ready (core_handle,
1258 GNUNET_NO, get_priority (queue),
1259 GNUNET_TIME_UNIT_FOREVER_REL,
1260 GNUNET_PEER_resolve2 (peer->id),
1264 queue->start_waiting = GNUNET_TIME_absolute_get ();
1266 else if (GNUNET_NO == call_core)
1268 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1274 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called\n",
1278 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1284 * Cancel all queued messages to a peer that belong to a certain connection.
1286 * @param peer Peer towards whom to cancel.
1287 * @param c Connection whose queued messages to cancel. Might be destroyed by
1288 * the sent continuation call.
1291 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c)
1293 struct CadetPeerQueue *q;
1294 struct CadetPeerQueue *next;
1295 struct CadetPeerQueue *prev;
1297 for (q = peer->queue_head; NULL != q; q = next)
1302 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP queue cancel %s\n", GC_m2s (q->type));
1303 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1309 GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1312 /* Get next from prev, q->next might be already freed:
1313 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1316 next = peer->queue_head;
1325 if (NULL == peer->queue_head)
1327 if (NULL != peer->core_transmit)
1329 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1330 peer->core_transmit = NULL;
1337 * Get the first transmittable message for a connection.
1339 * @param peer Neighboring peer.
1340 * @param c Connection.
1342 * @return First transmittable message.
1344 static struct CadetPeerQueue *
1345 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1347 struct CadetPeerQueue *q;
1349 for (q = peer->queue_head; NULL != q; q = q->next)
1353 if (queue_is_sendable (q))
1355 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1358 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1366 * Get the first message for a connection and unqueue it.
1368 * @param peer Neighboring peer.
1369 * @param c Connection.
1371 * @return First message for this connection.
1373 struct GNUNET_MessageHeader *
1374 GCP_connection_pop (struct CadetPeer *peer, struct CadetConnection *c)
1376 struct CadetPeerQueue *q;
1377 struct CadetPeerQueue *next;
1378 struct GNUNET_MessageHeader *msg;
1380 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection pop on %s\n", GCC_2s (c));
1381 for (q = peer->queue_head; NULL != q; q = next)
1388 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1389 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1390 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1391 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1392 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1393 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1394 GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1397 case GNUNET_MESSAGE_TYPE_CADET_KX:
1398 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1399 msg = (struct GNUNET_MessageHeader *) q->cls;
1400 GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1413 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1415 struct CadetPeerQueue *q;
1418 if (NULL != peer->core_transmit)
1420 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1421 return; /* Already unlocked */
1424 q = connection_get_first_message (peer, c);
1427 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1428 return; /* Nothing to transmit */
1432 peer->core_transmit =
1433 GNUNET_CORE_notify_transmit_ready (core_handle,
1434 GNUNET_NO, get_priority (q),
1435 GNUNET_TIME_UNIT_FOREVER_REL,
1436 GNUNET_PEER_resolve2 (peer->id),
1444 * Initialize the peer subsystem.
1446 * @param c Configuration.
1449 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1451 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1452 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1454 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1457 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1458 "CADET", "MAX_PEERS", "USING DEFAULT");
1463 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1470 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1471 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1472 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1473 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1474 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1477 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1478 NULL, /* Closure passed to CADET functions */
1479 &core_init, /* Call core_init once connected */
1480 &core_connect, /* Handle connects */
1481 &core_disconnect, /* remove peers on disconnects */
1482 NULL, /* Don't notify about all incoming messages */
1483 GNUNET_NO, /* For header only in notification */
1484 NULL, /* Don't notify about all outbound messages */
1485 GNUNET_NO, /* For header-only out notification */
1486 core_handlers); /* Register these handlers */
1488 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1490 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1491 /* Notify callbacks */
1496 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1497 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1498 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1499 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1500 transport_handle = NULL;
1505 if (NULL == core_handle)
1508 GNUNET_SCHEDULER_shutdown ();
1516 * Shut down the peer subsystem.
1521 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1523 if (core_handle != NULL)
1525 GNUNET_CORE_disconnect (core_handle);
1528 if (transport_handle != NULL)
1530 GNUNET_TRANSPORT_disconnect (transport_handle);
1531 transport_handle = NULL;
1533 GNUNET_PEER_change_rc (myid, -1);
1538 * Retrieve the CadetPeer stucture associated with the peer, create one
1539 * and insert it in the appropriate structures if the peer is not known yet.
1541 * @param peer_id Full identity of the peer.
1543 * @return Existing or newly created peer structure.
1546 GCP_get (const struct GNUNET_PeerIdentity *peer_id)
1548 struct CadetPeer *peer;
1550 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1553 peer = GNUNET_new (struct CadetPeer);
1554 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1556 peer_delete_oldest ();
1558 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1559 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1560 peer->id = GNUNET_PEER_intern (peer_id);
1562 peer->last_contact = GNUNET_TIME_absolute_get ();
1569 * Retrieve the CadetPeer stucture associated with the peer, create one
1570 * and insert it in the appropriate structures if the peer is not known yet.
1572 * @param peer Short identity of the peer.
1574 * @return Existing or newly created peer structure.
1577 GCP_get_short (const GNUNET_PEER_Id peer)
1579 return GCP_get (GNUNET_PEER_resolve2 (peer));
1584 * Try to connect to a peer on transport level.
1586 * @param cls Closure (peer).
1587 * @param tc TaskContext.
1590 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1592 struct CadetPeer *peer = cls;
1594 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1597 GNUNET_TRANSPORT_try_connect (transport_handle,
1598 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1603 * Try to establish a new connection to this peer (in its tunnel).
1604 * If the peer doesn't have any path to it yet, try to get one.
1605 * If the peer already has some path, send a CREATE CONNECTION towards it.
1607 * @param peer Peer to connect to.
1610 GCP_connect (struct CadetPeer *peer)
1612 struct CadetTunnel *t;
1613 struct CadetPeerPath *p;
1614 struct CadetConnection *c;
1617 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1619 /* If we have a current hello, try to connect using it. */
1620 GCP_try_connect (peer);
1624 rerun_search = GNUNET_NO;
1626 if (NULL != peer->path_head)
1628 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1629 p = peer_get_best_path (peer);
1635 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1638 c = GCT_use_path (t, p);
1641 /* This case can happen when the path includes a first hop that is
1642 * not yet known to be connected.
1644 * This happens quite often during testing when running cadet
1645 * under valgrind: core connect notifications come very late and the
1646 * DHT result has already come and created a valid path.
1647 * In this case, the peer->connections hashmap will be NULL and
1648 * tunnel_use_path will not be able to create a connection from that
1651 * Re-running the DHT GET should give core time to callback.
1653 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1654 * updating statistics about this issue.
1656 rerun_search = GNUNET_YES;
1660 GCC_send_create (c);
1666 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1670 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1672 GCD_search_stop (peer->search_h);
1673 peer->search_h = NULL;
1674 LOG (GNUNET_ERROR_TYPE_DEBUG,
1675 " Stopping DHT GET for peer %s\n",
1679 if (NULL == peer->search_h)
1681 const struct GNUNET_PeerIdentity *id;
1683 id = GNUNET_PEER_resolve2 (peer->id);
1684 LOG (GNUNET_ERROR_TYPE_DEBUG,
1685 " Starting DHT GET for peer %s\n", GCP_2s (peer));
1686 peer->search_h = GCD_search (id, &search_handler, peer);
1687 if (CADET_TUNNEL_NEW == GCT_get_cstate (t))
1688 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
1694 * Chech whether there is a direct (core level) connection to peer.
1696 * @param peer Peer to check.
1698 * @return #GNUNET_YES if there is a direct connection.
1701 GCP_is_neighbor (const struct CadetPeer *peer)
1703 struct CadetPeerPath *path;
1705 if (NULL == peer->connections)
1708 for (path = peer->path_head; NULL != path; path = path->next)
1710 if (3 > path->length)
1714 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1720 * Create and initialize a new tunnel towards a peer, in case it has none.
1721 * In case the peer already has a tunnel, nothing is done.
1723 * Does not generate any traffic, just creates the local data structures.
1725 * @param peer Peer towards which to create the tunnel.
1728 GCP_add_tunnel (struct CadetPeer *peer)
1730 if (NULL != peer->tunnel)
1732 peer->tunnel = GCT_new (peer);
1737 * Add a connection to a neighboring peer.
1739 * Store that the peer is the first hop of the connection in one
1740 * direction and that on peer disconnect the connection must be
1741 * notified and destroyed, for it will no longer be valid.
1743 * @param peer Peer to add connection to.
1744 * @param c Connection to add.
1746 * @return GNUNET_OK on success.
1749 GCP_add_connection (struct CadetPeer *peer,
1750 struct CadetConnection *c)
1753 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding connection %s\n", GCC_2s (c));
1754 LOG (GNUNET_ERROR_TYPE_DEBUG, "to peer %s\n", GCP_2s (peer));
1756 if (NULL == peer->connections)
1759 LOG (GNUNET_ERROR_TYPE_DEBUG,
1760 "Peer %s is not a neighbor!\n",
1762 return GNUNET_SYSERR;
1764 LOG (GNUNET_ERROR_TYPE_DEBUG,
1765 "peer %s ok, has %u connections.\n",
1766 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1767 result = GNUNET_CONTAINER_multihashmap_put (peer->connections,
1770 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1771 LOG (GNUNET_ERROR_TYPE_DEBUG,
1772 " now has %u connections.\n",
1773 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1774 LOG (GNUNET_ERROR_TYPE_DEBUG, "result %u\n", result);
1781 * Add the path to the peer and update the path used to reach it in case this
1784 * @param peer Destination peer to add the path to.
1785 * @param path New path to add. Last peer must be the peer in arg 1.
1786 * Path will be either used of freed if already known.
1787 * @param trusted Do we trust that this path is real?
1789 * @return path if path was taken, pointer to existing duplicate if exists
1792 struct CadetPeerPath *
1793 GCP_add_path (struct CadetPeer *peer, struct CadetPeerPath *path,
1796 struct CadetPeerPath *aux;
1800 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1801 path->length, GCP_2s (peer));
1803 if ((NULL == peer) || (NULL == path))
1806 path_destroy (path);
1809 if (path->peers[path->length - 1] != peer->id)
1812 path_destroy (path);
1816 for (l = 1; l < path->length; l++)
1818 if (path->peers[l] == myid)
1820 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1821 for (l2 = 0; l2 < path->length - l; l2++)
1823 path->peers[l2] = path->peers[l + l2];
1827 path->peers = GNUNET_realloc (path->peers,
1828 path->length * sizeof (GNUNET_PEER_Id));
1832 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1834 if (2 >= path->length && GNUNET_NO == trusted)
1836 /* Only allow CORE to tell us about direct paths */
1837 path_destroy (path);
1841 l = path_get_length (path);
1844 path_destroy (path);
1848 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1849 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1851 l2 = path_get_length (aux);
1854 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1855 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1856 peer->path_tail, aux, path);
1857 if (NULL != peer->tunnel && 3 < GCT_count_connections (peer->tunnel))
1865 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1867 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1868 path_destroy (path);
1873 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
1875 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1876 if (NULL != peer->tunnel && 3 < GCT_count_connections (peer->tunnel))
1885 * Add the path to the origin peer and update the path used to reach it in case
1886 * this is the shortest.
1887 * The path is given in peer_info -> destination, therefore we turn the path
1888 * upside down first.
1890 * @param peer Peer to add the path to, being the origin of the path.
1891 * @param path New path to add after being inversed.
1892 * Path will be either used or freed.
1893 * @param trusted Do we trust that this path is real?
1895 * @return path if path was taken, pointer to existing duplicate if exists
1898 struct CadetPeerPath *
1899 GCP_add_path_to_origin (struct CadetPeer *peer,
1900 struct CadetPeerPath *path,
1906 return GCP_add_path (peer, path, trusted);
1911 * Adds a path to the info of all the peers in the path
1913 * @param p Path to process.
1914 * @param confirmed Whether we know if the path works or not.
1917 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
1921 /* TODO: invert and add */
1922 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1923 for (i++; i < p->length; i++)
1925 struct CadetPeer *aux;
1926 struct CadetPeerPath *copy;
1928 aux = GCP_get_short (p->peers[i]);
1929 copy = path_duplicate (p);
1930 copy->length = i + 1;
1931 GCP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1937 * Remove any path to the peer that has the extact same peers as the one given.
1939 * @param peer Peer to remove the path from.
1940 * @param path Path to remove. Is always destroyed .
1943 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
1945 struct CadetPeerPath *iter;
1946 struct CadetPeerPath *next;
1948 GNUNET_assert (myid == path->peers[0]);
1949 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1951 for (iter = peer->path_head; NULL != iter; iter = next)
1954 if (0 == memcmp (path->peers, iter->peers,
1955 sizeof (GNUNET_PEER_Id) * path->length))
1957 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
1959 path_destroy (iter);
1962 path_destroy (path);
1967 * Remove a connection from a neighboring peer.
1969 * @param peer Peer to remove connection from.
1970 * @param c Connection to remove.
1972 * @return GNUNET_OK on success.
1975 GCP_remove_connection (struct CadetPeer *peer,
1976 const struct CadetConnection *c)
1978 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing connection %s\n", GCC_2s (c));
1979 LOG (GNUNET_ERROR_TYPE_DEBUG, "from peer %s\n", GCP_2s (peer));
1981 if (NULL == peer || NULL == peer->connections)
1983 LOG (GNUNET_ERROR_TYPE_DEBUG,
1984 "Peer %s is not a neighbor!\n",
1986 return GNUNET_SYSERR;
1988 LOG (GNUNET_ERROR_TYPE_DEBUG,
1989 "peer %s ok, has %u connections.\n",
1990 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1992 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1998 * Start the DHT search for new paths towards the peer: we don't have
1999 * enough good connections.
2001 * @param peer Destination peer.
2004 GCP_start_search (struct CadetPeer *peer)
2006 if (NULL != peer->search_h)
2012 peer->search_h = GCD_search (GCP_get_id (peer), &search_handler, peer);
2017 * Stop the DHT search for new paths towards the peer: we already have
2018 * enough good connections.
2020 * @param peer Destination peer.
2023 GCP_stop_search (struct CadetPeer *peer)
2025 if (NULL == peer->search_h)
2030 GCD_search_stop (peer->search_h);
2031 peer->search_h = NULL;
2036 * Get the Full ID of a peer.
2038 * @param peer Peer to get from.
2040 * @return Full ID of peer.
2042 const struct GNUNET_PeerIdentity *
2043 GCP_get_id (const struct CadetPeer *peer)
2045 return GNUNET_PEER_resolve2 (peer->id);
2050 * Get the Short ID of a peer.
2052 * @param peer Peer to get from.
2054 * @return Short ID of peer.
2057 GCP_get_short_id (const struct CadetPeer *peer)
2070 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2073 if (NULL == t && NULL != peer->search_h)
2075 GCP_stop_search (peer);
2081 * Get the tunnel towards a peer.
2083 * @param peer Peer to get from.
2085 * @return Tunnel towards peer.
2087 struct CadetTunnel *
2088 GCP_get_tunnel (const struct CadetPeer *peer)
2090 return peer->tunnel;
2095 * Set the hello message.
2097 * @param peer Peer whose message to set.
2098 * @param hello Hello message.
2101 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2103 struct GNUNET_HELLO_Message *old;
2106 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2110 old = GCP_get_hello (peer);
2113 size = GNUNET_HELLO_size (hello);
2114 LOG (GNUNET_ERROR_TYPE_DEBUG, " new (%u bytes)\n", size);
2115 peer->hello = GNUNET_malloc (size);
2116 memcpy (peer->hello, hello, size);
2120 peer->hello = GNUNET_HELLO_merge (old, hello);
2121 LOG (GNUNET_ERROR_TYPE_DEBUG, " merge into %p (%u bytes)\n",
2122 peer->hello, GNUNET_HELLO_size (hello));
2129 * Get the hello message.
2131 * @param peer Peer whose message to get.
2133 * @return Hello message.
2135 struct GNUNET_HELLO_Message *
2136 GCP_get_hello (struct CadetPeer *peer)
2138 struct GNUNET_TIME_Absolute expiration;
2139 struct GNUNET_TIME_Relative remaining;
2141 if (NULL == peer->hello)
2144 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2145 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2146 if (0 == remaining.rel_value_us)
2148 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2149 GNUNET_STRINGS_absolute_time_to_string (expiration));
2150 GNUNET_free (peer->hello);
2158 * Try to connect to a peer on TRANSPORT level.
2160 * @param peer Peer to whom to connect.
2163 GCP_try_connect (struct CadetPeer *peer)
2165 struct GNUNET_HELLO_Message *hello;
2166 struct GNUNET_MessageHeader *mh;
2168 if (NULL == transport_handle)
2171 hello = GCP_get_hello (peer);
2175 mh = GNUNET_HELLO_get_header (hello);
2176 GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
2181 * Notify a peer that a link between two other peers is broken. If any path
2182 * used that link, eliminate it.
2184 * @param peer Peer affected by the change.
2185 * @param peer1 Peer whose link is broken.
2186 * @param peer2 Peer whose link is broken.
2189 GCP_notify_broken_link (struct CadetPeer *peer,
2190 struct GNUNET_PeerIdentity *peer1,
2191 struct GNUNET_PeerIdentity *peer2)
2193 struct CadetPeerPath *iter;
2194 struct CadetPeerPath *next;
2199 p1 = GNUNET_PEER_search (peer1);
2200 p2 = GNUNET_PEER_search (peer2);
2202 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2203 if (0 == p1 || 0 == p2)
2205 /* We don't even know them */
2209 for (iter = peer->path_head; NULL != iter; iter = next)
2212 for (i = 0; i < iter->length - 1; i++)
2214 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2215 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2220 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2223 path_invalidate (iter);
2231 * Count the number of known paths toward the peer.
2233 * @param peer Peer to get path info.
2235 * @return Number of known paths.
2238 GCP_count_paths (const struct CadetPeer *peer)
2240 struct CadetPeerPath *iter;
2243 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2251 * Iterate all known peers.
2253 * @param iter Iterator.
2254 * @param cls Closure for @c iter.
2257 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2259 GNUNET_CONTAINER_multipeermap_iterate (peers, iter, cls);
2264 * Get the static string for a peer ID.
2268 * @return Static string for it's ID.
2271 GCP_2s (const struct CadetPeer *peer)
2275 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));