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__)
38 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-p2p",__VA_ARGS__)
41 /******************************************************************************/
42 /******************************** STRUCTS **********************************/
43 /******************************************************************************/
46 * Struct containing info about a queued transmission to this peer
53 struct CadetPeerQueue *next;
58 struct CadetPeerQueue *prev;
61 * Peer this transmission is directed to.
63 struct CadetPeer *peer;
66 * Connection this message belongs to.
68 struct CadetConnection *c;
76 * Pointer to info stucture used as cls.
88 uint16_t payload_type;
101 * Set when this message starts waiting for CORE.
103 struct GNUNET_TIME_Absolute start_waiting;
106 * Function to call on sending.
111 * Closure for callback.
117 * Struct containing all information regarding a given peer
127 * Last time we heard from this peer
129 struct GNUNET_TIME_Absolute last_contact;
132 * Paths to reach the peer, ordered by ascending hop count
134 struct CadetPeerPath *path_head;
137 * Paths to reach the peer, ordered by ascending hop count
139 struct CadetPeerPath *path_tail;
142 * Handle to stop the DHT search for paths to this peer
144 struct GCD_search_handle *search_h;
147 * Tunnel to this peer, if any.
149 struct CadetTunnel *tunnel;
152 * Connections that go through this peer, indexed by tid;
154 struct GNUNET_CONTAINER_MultiHashMap *connections;
157 * Handle for queued transmissions
159 struct GNUNET_CORE_TransmitHandle *core_transmit;
162 * Transmission queue to core DLL head
164 struct CadetPeerQueue *queue_head;
167 * Transmission queue to core DLL tail
169 struct CadetPeerQueue *queue_tail;
172 * How many messages are in the queue to this peer.
174 unsigned int queue_n;
179 struct GNUNET_HELLO_Message* hello;
183 /******************************************************************************/
184 /******************************* GLOBALS ***********************************/
185 /******************************************************************************/
188 * Global handle to the statistics service.
190 extern struct GNUNET_STATISTICS_Handle *stats;
193 * Local peer own ID (full value).
195 extern struct GNUNET_PeerIdentity my_full_id;
198 * Local peer own ID (short)
200 extern GNUNET_PEER_Id myid;
203 * Peers known, indexed by PeerIdentity (CadetPeer).
205 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
208 * How many peers do we want to remember?
210 static unsigned long long max_peers;
213 * Percentage of messages that will be dropped (for test purposes only).
215 static unsigned long long drop_percent;
218 * Handle to communicate with core.
220 static struct GNUNET_CORE_Handle *core_handle;
223 * Handle to try to start new connections.
225 static struct GNUNET_TRANSPORT_Handle *transport_handle;
228 /******************************************************************************/
229 /***************************** DEBUG *********************************/
230 /******************************************************************************/
233 * Log all kinds of info about the queueing status of a peer.
235 * @param p Peer whose queue to show.
236 * @param level Error level to use for logging.
239 queue_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
241 struct CadetPeerQueue *q;
244 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
246 __FILE__, __FUNCTION__, __LINE__);
250 LOG2 (level, "QQQ Message queue towards %s\n", GCP_2s (p));
251 LOG2 (level, "QQQ queue length: %u\n", p->queue_n);
252 LOG2 (level, "QQQ core tmt rdy: %p\n", p->core_transmit);
254 for (q = p->queue_head; NULL != q; q = q->next)
256 LOG2 (level, "QQQ - %s %s on %s\n",
257 GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
258 LOG2 (level, "QQQ payload %s, %u\n",
259 GC_m2s (q->payload_type), q->payload_id);
260 LOG2 (level, "QQQ size: %u bytes\n", q->size);
263 LOG2 (level, "QQQ End queue towards %s\n", GCP_2s (p));
268 * Log all kinds of info about a peer.
273 GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
275 struct CadetPeerPath *path;
279 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
281 __FILE__, __FUNCTION__, __LINE__);
287 LOG2 (level, "PPP DEBUG PEER NULL\n");
291 LOG2 (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
292 LOG2 (level, "PPP last contact %s\n",
293 GNUNET_STRINGS_absolute_time_to_string (p->last_contact));
294 for (path = p->path_head; NULL != path; path = path->next)
299 LOG2 (level, "PPP path: %s\n", s);
303 LOG2 (level, "PPP core transmit handle %p\n", p->core_transmit);
304 LOG2 (level, "PPP DHT GET handle %p\n", p->search_h);
305 if (NULL != p->connections)
306 conns = GNUNET_CONTAINER_multihashmap_size (p->connections);
309 LOG2 (level, "PPP # connections over link to peer: %u\n", conns);
310 queue_debug (p, level);
311 LOG2 (level, "PPP DEBUG END\n");
315 /******************************************************************************/
316 /***************************** CORE HELPERS *********************************/
317 /******************************************************************************/
321 * Iterator to notify all connections of a broken link. Mark connections
322 * to destroy after all traffic has been sent.
324 * @param cls Closure (peer disconnected).
325 * @param key Current key code (peer id).
326 * @param value Value in the hash map (connection).
328 * @return #GNUNET_YES to continue to iterate.
331 notify_broken (void *cls,
332 const struct GNUNET_HashCode *key,
335 struct CadetPeer *peer = cls;
336 struct CadetConnection *c = value;
338 LOG (GNUNET_ERROR_TYPE_DEBUG, " notifying %s due to %s\n",
339 GCC_2s (c), GCP_2s (peer));
340 GCC_notify_broken (c, peer);
347 * Remove the direct path to the peer.
349 * @param peer Peer to remove the direct path from.
352 static struct CadetPeerPath *
353 pop_direct_path (struct CadetPeer *peer)
355 struct CadetPeerPath *iter;
357 for (iter = peer->path_head; NULL != iter; iter = iter->next)
359 if (2 >= iter->length)
361 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
369 /******************************************************************************/
370 /***************************** CORE CALLBACKS *********************************/
371 /******************************************************************************/
374 * Method called whenever a given peer connects.
377 * @param peer peer identity this notification is about
380 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
382 struct CadetPeer *mp;
383 struct CadetPeerPath *path;
386 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
390 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s (self)\n", own_id);
395 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s <= %s\n",
396 own_id, GNUNET_i2s (peer));
398 path->peers[1] = mp->id;
399 GNUNET_PEER_change_rc (mp->id, 1);
400 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
402 path->peers[0] = myid;
403 GNUNET_PEER_change_rc (myid, 1);
404 GCP_add_path (mp, path, GNUNET_YES);
406 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
408 if (NULL != GCP_get_tunnel (mp) &&
409 0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer))
419 * Method called whenever a peer disconnects.
422 * @param peer peer identity this notification is about
425 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
428 struct CadetPeerPath *direct_path;
431 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
432 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
439 LOG (GNUNET_ERROR_TYPE_INFO, "DISCONNECTED %s (self)\n", own_id);
441 LOG (GNUNET_ERROR_TYPE_INFO, "DISCONNECTED %s <= %s\n",
442 own_id, GNUNET_i2s (peer));
443 direct_path = pop_direct_path (p);
444 GNUNET_CONTAINER_multihashmap_iterate (p->connections, ¬ify_broken, p);
445 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
446 p->connections = NULL;
447 if (NULL != p->core_transmit)
449 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
450 p->core_transmit = NULL;
452 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
454 path_destroy (direct_path);
460 * Functions to handle messages from core
462 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
463 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
464 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
465 sizeof (struct GNUNET_CADET_ConnectionACK)},
466 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
467 sizeof (struct GNUNET_CADET_ConnectionBroken)},
468 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
469 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
470 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
471 sizeof (struct GNUNET_CADET_ACK)},
472 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
473 sizeof (struct GNUNET_CADET_Poll)},
474 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
475 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
481 * To be called on core init/fail.
483 * @param cls Closure (config)
484 * @param identity the public identity of this peer
487 core_init (void *cls,
488 const struct GNUNET_PeerIdentity *identity)
490 const struct GNUNET_CONFIGURATION_Handle *c = cls;
493 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
494 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
496 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
497 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
498 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
499 GNUNET_CORE_disconnect (core_handle);
500 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
501 NULL, /* Closure passed to CADET functions */
502 &core_init, /* Call core_init once connected */
503 &core_connect, /* Handle connects */
504 &core_disconnect, /* remove peers on disconnects */
505 NULL, /* Don't notify about all incoming messages */
506 GNUNET_NO, /* For header only in notification */
507 NULL, /* Don't notify about all outbound messages */
508 GNUNET_NO, /* For header-only out notification */
509 core_handlers); /* Register these handlers */
519 * Core callback to write a pre-constructed data packet to core buffer
521 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
522 * @param size Number of bytes available in buf.
523 * @param buf Where the to write the message.
525 * @return number of bytes written to buf
528 send_core_data_raw (void *cls, size_t size, void *buf)
530 struct GNUNET_MessageHeader *msg = cls;
533 GNUNET_assert (NULL != msg);
534 total_size = ntohs (msg->size);
536 if (total_size > size)
541 memcpy (buf, msg, total_size);
548 * Function to send a create connection message to a peer.
550 * @param c Connection to create.
551 * @param size number of bytes available in buf
552 * @param buf where the callee should write the message
553 * @return number of bytes written to buf
556 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
558 struct GNUNET_CADET_ConnectionCreate *msg;
559 struct GNUNET_PeerIdentity *peer_ptr;
560 const struct CadetPeerPath *p = GCC_get_path (c);
567 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
569 sizeof (struct GNUNET_CADET_ConnectionCreate) +
570 p->length * sizeof (struct GNUNET_PeerIdentity);
572 if (size < size_needed || NULL == buf)
577 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
578 msg->header.size = htons (size_needed);
579 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
580 msg->cid = *GCC_get_id (c);
582 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
583 for (i = 0; i < p->length; i++)
585 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
588 LOG (GNUNET_ERROR_TYPE_DEBUG,
589 "CONNECTION CREATE (%u bytes long) sent!\n",
596 * Creates a path ack message in buf and frees all unused resources.
598 * @param c Connection to send an ACK on.
599 * @param size number of bytes available in buf
600 * @param buf where the callee should write the message
602 * @return number of bytes written to buf
605 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
607 struct GNUNET_CADET_ConnectionACK *msg = buf;
609 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
610 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
615 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
616 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
617 msg->cid = *GCC_get_id (c);
619 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
620 return sizeof (struct GNUNET_CADET_ConnectionACK);
624 /******************************************************************************/
625 /******************************** STATIC ***********************************/
626 /******************************************************************************/
630 * Get priority for a queued message.
632 * @param q Queued message
634 * @return CORE priority to use.
636 static enum GNUNET_CORE_Priority
637 get_priority (struct CadetPeerQueue *q)
639 enum GNUNET_CORE_Priority low;
640 enum GNUNET_CORE_Priority high;
645 return GNUNET_CORE_PRIO_BACKGROUND;
648 /* Relayed traffic has lower priority, our own traffic has higher */
649 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
651 low = GNUNET_CORE_PRIO_BEST_EFFORT;
652 high = GNUNET_CORE_PRIO_URGENT;
656 low = GNUNET_CORE_PRIO_URGENT;
657 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
660 /* Bulky payload has lower priority, control traffic has higher. */
661 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type)
669 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
672 * @param key current key code
673 * @param value value in the hash map
674 * @return #GNUNET_YES if we should continue to iterate,
678 shutdown_tunnel (void *cls,
679 const struct GNUNET_PeerIdentity *key,
682 struct CadetPeer *p = value;
683 struct CadetTunnel *t = p->tunnel;
692 * Destroy the peer_info and free any allocated resources linked to it
694 * @param peer The peer_info to destroy.
696 * @return GNUNET_OK on success
699 peer_destroy (struct CadetPeer *peer)
701 struct GNUNET_PeerIdentity id;
702 struct CadetPeerPath *p;
703 struct CadetPeerPath *nextp;
705 GNUNET_PEER_resolve (peer->id, &id);
706 GNUNET_PEER_change_rc (peer->id, -1);
708 LOG (GNUNET_ERROR_TYPE_WARNING, "destroying peer %s\n", GNUNET_i2s (&id));
711 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
714 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
716 if (NULL != peer->search_h)
718 GCD_search_stop (peer->search_h);
724 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
728 GCT_destroy_empty (peer->tunnel);
735 * Returns if peer is used (has a tunnel or is neighbor).
737 * @param peer Peer to check.
739 * @return #GNUNET_YES if peer is in use.
742 peer_is_used (struct CadetPeer *peer)
744 struct CadetPeerPath *p;
746 if (NULL != peer->tunnel)
749 for (p = peer->path_head; NULL != p; p = p->next)
759 * Iterator over all the peers to get the oldest timestamp.
761 * @param cls Closure (unsued).
762 * @param key ID of the peer.
763 * @param value Peer_Info of the peer.
766 peer_get_oldest (void *cls,
767 const struct GNUNET_PeerIdentity *key,
770 struct CadetPeer *p = value;
771 struct GNUNET_TIME_Absolute *abs = cls;
773 /* Don't count active peers */
774 if (GNUNET_YES == peer_is_used (p))
777 if (abs->abs_value_us < p->last_contact.abs_value_us)
778 abs->abs_value_us = p->last_contact.abs_value_us;
785 * Iterator over all the peers to remove the oldest entry.
787 * @param cls Closure (unsued).
788 * @param key ID of the peer.
789 * @param value Peer_Info of the peer.
792 peer_timeout (void *cls,
793 const struct GNUNET_PeerIdentity *key,
796 struct CadetPeer *p = value;
797 struct GNUNET_TIME_Absolute *abs = cls;
799 LOG (GNUNET_ERROR_TYPE_WARNING,
800 "peer %s timeout\n", GNUNET_i2s (key));
802 if (p->last_contact.abs_value_us == abs->abs_value_us &&
803 GNUNET_NO == peer_is_used (p))
813 * Delete oldest unused peer.
816 peer_delete_oldest (void)
818 struct GNUNET_TIME_Absolute abs;
820 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
822 GNUNET_CONTAINER_multipeermap_iterate (peers,
825 GNUNET_CONTAINER_multipeermap_iterate (peers,
832 * Choose the best (yet unused) path towards a peer,
833 * considering the tunnel properties.
835 * @param peer The destination peer.
837 * @return Best current known path towards the peer, if any.
839 static struct CadetPeerPath *
840 peer_get_best_path (const struct CadetPeer *peer)
842 struct CadetPeerPath *best_p;
843 struct CadetPeerPath *p;
844 unsigned int best_cost;
847 best_cost = UINT_MAX;
849 for (p = peer->path_head; NULL != p; p = p->next)
851 if (GNUNET_NO == path_is_valid (p))
852 continue; /* Don't use invalid paths. */
853 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
854 continue; /* If path is already in use, skip it. */
856 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
867 * Is this queue element sendable?
869 * - All management traffic is always sendable.
870 * - For payload traffic, check the connection flow control.
872 * @param q Queue element to inspect.
874 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
877 queue_is_sendable (struct CadetPeerQueue *q)
879 /* Is PID-independent? */
882 case GNUNET_MESSAGE_TYPE_CADET_ACK:
883 case GNUNET_MESSAGE_TYPE_CADET_POLL:
884 case GNUNET_MESSAGE_TYPE_CADET_KX:
885 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
886 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
887 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
888 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
891 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
898 return GCC_is_sendable (q->c, q->fwd);
903 * Get first sendable message.
905 * @param peer The destination peer.
907 * @return First transmittable message, if any. Otherwise, NULL.
909 static struct CadetPeerQueue *
910 peer_get_first_message (const struct CadetPeer *peer)
912 struct CadetPeerQueue *q;
914 for (q = peer->queue_head; NULL != q; q = q->next)
916 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
917 if (queue_is_sendable (q))
926 * Function to process paths received for a new peer addition. The recorded
927 * paths form the initial tunnel, which can be optimized later.
928 * Called on each result obtained for the DHT search.
934 search_handler (void *cls, const struct CadetPeerPath *path)
936 struct CadetPeer *peer = cls;
937 unsigned int connection_count;
939 GCP_add_path_to_all (path, GNUNET_NO);
941 /* Count connections */
942 connection_count = GCT_count_connections (peer->tunnel);
944 /* If we already have 3 (or more (?!)) connections, it's enough */
945 if (3 <= connection_count)
948 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
950 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
959 * Core callback to write a queued packet to core buffer
961 * @param cls Closure (peer info).
962 * @param size Number of bytes available in buf.
963 * @param buf Where the to write the message.
965 * @return number of bytes written to buf
968 queue_send (void *cls, size_t size, void *buf)
970 struct CadetPeer *peer = cls;
971 struct CadetConnection *c;
972 struct CadetPeerQueue *queue;
973 const struct GNUNET_PeerIdentity *dst_id;
978 peer->core_transmit = NULL;
979 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
980 GCP_2s (peer), size);
982 if (NULL == buf || 0 == size)
984 LOG (GNUNET_ERROR_TYPE_DEBUG, "Buffer size 0.\n");
989 queue = peer_get_first_message (peer);
992 GNUNET_assert (0); /* Core tmt_rdy should've been canceled */
997 dst_id = GNUNET_PEER_resolve2 (peer->id);
998 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s %s\n",
999 GCC_2s (c), GC_f2s(queue->fwd));
1000 /* Check if buffer size is enough for the message */
1001 if (queue->size > size)
1003 LOG (GNUNET_ERROR_TYPE_WARNING, "not enough room (%u vs %u), reissue\n",
1005 peer->core_transmit =
1006 GNUNET_CORE_notify_transmit_ready (core_handle,
1007 GNUNET_NO, get_priority (queue),
1008 GNUNET_TIME_UNIT_FOREVER_REL,
1015 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok\n", queue->size);
1018 switch (queue->type)
1020 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1021 pid = GCC_get_pid (queue->c, queue->fwd);
1022 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload ID %u\n", pid);
1023 data_size = send_core_data_raw (queue->cls, size, buf);
1024 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (pid);
1026 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1027 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1028 case GNUNET_MESSAGE_TYPE_CADET_KX:
1029 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1030 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1031 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1032 data_size = send_core_data_raw (queue->cls, size, buf);
1034 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1035 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1036 if (GCC_is_origin (c, GNUNET_YES))
1037 data_size = send_core_connection_create (c, size, buf);
1039 data_size = send_core_data_raw (queue->cls, size, buf);
1041 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1042 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1043 if (GCC_is_origin (c, GNUNET_NO) ||
1044 GCC_is_origin (c, GNUNET_YES))
1045 data_size = send_core_connection_ack (c, size, buf);
1047 data_size = send_core_data_raw (queue->cls, size, buf);
1049 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1050 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1051 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1052 /* This should be encapsulted */
1058 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1062 if (0 < drop_percent &&
1063 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1065 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on connection %s %s\n",
1066 GC_m2s (queue->type), GC_m2s (queue->payload_type), queue->payload_id,
1067 GCC_2s (c), GC_f2s (queue->fwd));
1072 LOG (GNUNET_ERROR_TYPE_INFO,
1073 "snd %s (%s %u) on connection %s (%p) %s (size %u)\n",
1074 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1075 queue->payload_id, GCC_2s (c), c, GC_f2s (queue->fwd), data_size);
1078 /* Free queue, but cls was freed by send_core_*. */
1079 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1081 /* If more data in queue, send next */
1082 queue = peer_get_first_message (peer);
1085 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data!\n");
1086 if (NULL == peer->core_transmit)
1088 peer->core_transmit =
1089 GNUNET_CORE_notify_transmit_ready (core_handle,
1090 GNUNET_NO, get_priority (queue),
1091 GNUNET_TIME_UNIT_FOREVER_REL,
1096 queue->start_waiting = GNUNET_TIME_absolute_get ();
1100 LOG (GNUNET_ERROR_TYPE_DEBUG,
1101 "* tmt rdy called somewhere else\n");
1103 // GCC_start_poll (); FIXME needed?
1107 // GCC_stop_poll(); FIXME needed?
1110 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", data_size);
1111 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1116 /******************************************************************************/
1117 /******************************** API ***********************************/
1118 /******************************************************************************/
1122 * Free a transmission that was already queued with all resources
1123 * associated to the request.
1125 * If connection was marked to be destroyed, and this was the last queued
1126 * message on it, the connection will be free'd as a result.
1128 * @param queue Queue handler to cancel.
1129 * @param clear_cls Is it necessary to free associated cls?
1130 * @param sent Was it really sent? (Could have been canceled)
1131 * @param pid PID, if relevant (was sent and was a payload message).
1133 * @return #GNUNET_YES if connection was destroyed as a result,
1134 * #GNUNET_NO otherwise.
1137 GCP_queue_destroy (struct CadetPeerQueue *queue, int clear_cls,
1138 int sent, uint32_t pid)
1140 struct CadetPeer *peer;
1141 int connection_destroyed;
1144 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1145 if (GNUNET_YES == clear_cls)
1147 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1148 switch (queue->type)
1150 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1151 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1153 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1154 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1155 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1156 case GNUNET_MESSAGE_TYPE_CADET_KX:
1157 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1158 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1159 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1160 GNUNET_free_non_null (queue->cls);
1165 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1166 GC_m2s (queue->type));
1169 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1171 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1172 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1177 if (NULL != queue->callback)
1179 struct GNUNET_TIME_Relative core_wait_time;
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback\n");
1182 core_wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1183 connection_destroyed = queue->callback (queue->callback_cls,
1184 queue->c, sent, queue->type, pid,
1185 queue->fwd, queue->size,
1190 connection_destroyed = GNUNET_NO;
1193 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1195 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1196 peer->core_transmit = NULL;
1199 GNUNET_free (queue);
1200 return connection_destroyed;
1205 * @brief Queue and pass message to core when possible.
1207 * @param peer Peer towards which to queue the message.
1208 * @param cls Closure (@c type dependant). It will be used by queue_send to
1209 * build the message to be sent if not already prebuilt.
1210 * @param type Type of the message, 0 for a raw message.
1211 * @param size Size of the message.
1212 * @param c Connection this message belongs to (can be NULL).
1213 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1214 * @param cont Continuation to be called once CORE has taken the message.
1215 * @param cont_cls Closure for @c cont.
1217 * @return Handle to cancel the message before it is sent. Once cont is called
1218 * message has been sent and therefore the handle is no longer valid.
1220 struct CadetPeerQueue *
1221 GCP_queue_add (struct CadetPeer *peer, void *cls, uint16_t type,
1222 uint16_t payload_type, uint32_t payload_id, size_t size,
1223 struct CadetConnection *c, int fwd,
1224 GCP_sent cont, void *cont_cls)
1226 struct CadetPeerQueue *q;
1231 if (NULL == c && GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN != type)
1232 error_level = GNUNET_ERROR_TYPE_ERROR;
1234 error_level = GNUNET_ERROR_TYPE_INFO;
1236 "que %s (%s %u) on connection %s (%p) %s towards %s (size %u)\n",
1237 GC_m2s (type), GC_m2s (payload_type), payload_id,
1238 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1240 if (error_level == GNUNET_ERROR_TYPE_ERROR)
1242 if (NULL == peer->connections)
1244 /* We are not connected to this peer, ignore request. */
1245 LOG (GNUNET_ERROR_TYPE_WARNING, "%s not a neighbor\n", GCP_2s (peer));
1246 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1253 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1254 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1259 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1261 call_core = (NULL == c || type == GNUNET_MESSAGE_TYPE_CADET_KX) ?
1262 GNUNET_YES : GCC_is_sendable (c, fwd);
1263 q = GNUNET_new (struct CadetPeerQueue);
1266 q->payload_type = payload_type;
1267 q->payload_id = payload_id;
1273 q->callback_cls = cont_cls;
1276 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1281 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1282 call_core = GNUNET_YES;
1285 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1287 LOG (GNUNET_ERROR_TYPE_DEBUG,
1288 "calling core tmt rdy towards %s for %u bytes\n",
1289 GCP_2s (peer), size);
1290 peer->core_transmit =
1291 GNUNET_CORE_notify_transmit_ready (core_handle,
1292 GNUNET_NO, get_priority (q),
1293 GNUNET_TIME_UNIT_FOREVER_REL,
1294 GNUNET_PEER_resolve2 (peer->id),
1295 size, &queue_send, peer);
1296 q->start_waiting = GNUNET_TIME_absolute_get ();
1298 else if (GNUNET_NO == call_core)
1300 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called\n",
1310 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1316 * Cancel all queued messages to a peer that belong to a certain connection.
1318 * @param peer Peer towards whom to cancel.
1319 * @param c Connection whose queued messages to cancel. Might be destroyed by
1320 * the sent continuation call.
1323 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c)
1325 struct CadetPeerQueue *q;
1326 struct CadetPeerQueue *next;
1327 struct CadetPeerQueue *prev;
1328 int connection_destroyed;
1330 connection_destroyed = GNUNET_NO;
1331 for (q = peer->queue_head; NULL != q; q = next)
1336 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP queue cancel %s\n", GC_m2s (q->type));
1337 GNUNET_break (GNUNET_NO == connection_destroyed);
1338 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1344 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1347 /* Get next from prev, q->next might be already freed:
1348 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1351 next = peer->queue_head;
1361 if (NULL == peer->queue_head && NULL != peer->core_transmit)
1363 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1364 peer->core_transmit = NULL;
1370 * Get the first transmittable message for a connection.
1372 * @param peer Neighboring peer.
1373 * @param c Connection.
1375 * @return First transmittable message.
1377 static struct CadetPeerQueue *
1378 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1380 struct CadetPeerQueue *q;
1382 for (q = peer->queue_head; NULL != q; q = q->next)
1386 if (queue_is_sendable (q))
1388 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1391 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1399 * Get the first message for a connection and unqueue it.
1401 * Only tunnel (or higher) level messages are unqueued. Connection specific
1402 * messages are silently destroyed upon encounter.
1404 * @param peer Neighboring peer.
1405 * @param c Connection.
1406 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1409 * @return First message for this connection.
1411 struct GNUNET_MessageHeader *
1412 GCP_connection_pop (struct CadetPeer *peer,
1413 struct CadetConnection *c,
1416 struct CadetPeerQueue *q;
1417 struct CadetPeerQueue *next;
1418 struct GNUNET_MessageHeader *msg;
1421 GNUNET_assert (NULL != destroyed);
1422 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on connection %p\n", c);
1423 for (q = peer->queue_head; NULL != q; q = next)
1428 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), callback: %p\n",
1429 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1433 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1434 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1435 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1436 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1437 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1438 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1439 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1440 if (GNUNET_YES == dest)
1442 GNUNET_break (GNUNET_NO == *destroyed);
1443 *destroyed = GNUNET_YES;
1447 case GNUNET_MESSAGE_TYPE_CADET_KX:
1448 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1449 msg = (struct GNUNET_MessageHeader *) q->cls;
1450 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1451 if (GNUNET_YES == dest)
1453 GNUNET_break (GNUNET_NO == *destroyed);
1454 *destroyed = GNUNET_YES;
1460 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1468 * Unlock a possibly locked queue for a connection.
1470 * If there is a message that can be sent on this connection, call core for it.
1471 * Otherwise (if core transmit is already called or there is no sendable
1472 * message) do nothing.
1474 * @param peer Peer who keeps the queue.
1475 * @param c Connection whose messages to unlock.
1478 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1480 struct CadetPeerQueue *q;
1483 if (NULL != peer->core_transmit)
1485 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1486 return; /* Already unlocked */
1489 q = connection_get_first_message (peer, c);
1492 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1493 return; /* Nothing to transmit */
1497 peer->core_transmit =
1498 GNUNET_CORE_notify_transmit_ready (core_handle,
1499 GNUNET_NO, get_priority (q),
1500 GNUNET_TIME_UNIT_FOREVER_REL,
1501 GNUNET_PEER_resolve2 (peer->id),
1509 * Initialize the peer subsystem.
1511 * @param c Configuration.
1514 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1516 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1517 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1519 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1522 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1523 "CADET", "MAX_PEERS", "USING DEFAULT");
1528 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1535 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1536 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1537 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1538 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1539 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1542 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1543 NULL, /* Closure passed to CADET functions */
1544 &core_init, /* Call core_init once connected */
1545 &core_connect, /* Handle connects */
1546 &core_disconnect, /* remove peers on disconnects */
1547 NULL, /* Don't notify about all incoming messages */
1548 GNUNET_NO, /* For header only in notification */
1549 NULL, /* Don't notify about all outbound messages */
1550 GNUNET_NO, /* For header-only out notification */
1551 core_handlers); /* Register these handlers */
1553 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1555 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1556 /* Notify callbacks */
1561 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1562 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1563 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1564 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1565 transport_handle = NULL;
1570 if (NULL == core_handle)
1573 GNUNET_SCHEDULER_shutdown ();
1581 * Shut down the peer subsystem.
1586 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1588 if (core_handle != NULL)
1590 GNUNET_CORE_disconnect (core_handle);
1593 if (transport_handle != NULL)
1595 GNUNET_TRANSPORT_disconnect (transport_handle);
1596 transport_handle = NULL;
1598 GNUNET_PEER_change_rc (myid, -1);
1603 * Retrieve the CadetPeer stucture associated with the peer, create one
1604 * and insert it in the appropriate structures if the peer is not known yet.
1606 * @param peer_id Full identity of the peer.
1608 * @return Existing or newly created peer structure.
1611 GCP_get (const struct GNUNET_PeerIdentity *peer_id)
1613 struct CadetPeer *peer;
1615 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1618 peer = GNUNET_new (struct CadetPeer);
1619 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1621 peer_delete_oldest ();
1623 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1624 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1625 peer->id = GNUNET_PEER_intern (peer_id);
1627 peer->last_contact = GNUNET_TIME_absolute_get ();
1634 * Retrieve the CadetPeer stucture associated with the peer, create one
1635 * and insert it in the appropriate structures if the peer is not known yet.
1637 * @param peer Short identity of the peer.
1639 * @return Existing or newly created peer structure.
1642 GCP_get_short (const GNUNET_PEER_Id peer)
1644 return GCP_get (GNUNET_PEER_resolve2 (peer));
1649 * Try to connect to a peer on transport level.
1651 * @param cls Closure (peer).
1652 * @param tc TaskContext.
1655 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1657 struct CadetPeer *peer = cls;
1659 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1662 GNUNET_TRANSPORT_try_connect (transport_handle,
1663 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1668 * Try to establish a new connection to this peer (in its tunnel).
1669 * If the peer doesn't have any path to it yet, try to get one.
1670 * If the peer already has some path, send a CREATE CONNECTION towards it.
1672 * @param peer Peer to connect to.
1675 GCP_connect (struct CadetPeer *peer)
1677 struct CadetTunnel *t;
1678 struct CadetPeerPath *p;
1679 struct CadetConnection *c;
1682 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1684 /* If we have a current hello, try to connect using it. */
1685 GCP_try_connect (peer);
1689 rerun_search = GNUNET_NO;
1691 if (NULL != peer->path_head)
1693 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1694 p = peer_get_best_path (peer);
1700 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1703 c = GCT_use_path (t, p);
1706 /* This case can happen when the path includes a first hop that is
1707 * not yet known to be connected.
1709 * This happens quite often during testing when running cadet
1710 * under valgrind: core connect notifications come very late and the
1711 * DHT result has already come and created a valid path.
1712 * In this case, the peer->connections hashmap will be NULL and
1713 * tunnel_use_path will not be able to create a connection from that
1716 * Re-running the DHT GET should give core time to callback.
1718 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1719 * updating statistics about this issue.
1721 rerun_search = GNUNET_YES;
1725 GCC_send_create (c);
1731 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1735 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1737 GCD_search_stop (peer->search_h);
1738 peer->search_h = NULL;
1739 LOG (GNUNET_ERROR_TYPE_DEBUG, " Stopping DHT GET for peer %s\n",
1743 if (NULL == peer->search_h)
1745 const struct GNUNET_PeerIdentity *id;
1747 id = GNUNET_PEER_resolve2 (peer->id);
1748 LOG (GNUNET_ERROR_TYPE_DEBUG,
1749 " Starting DHT GET for peer %s\n", GCP_2s (peer));
1750 peer->search_h = GCD_search (id, &search_handler, peer);
1751 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
1752 || 0 == GCT_count_any_connections (t))
1753 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
1759 * Chech whether there is a direct (core level) connection to peer.
1761 * @param peer Peer to check.
1763 * @return #GNUNET_YES if there is a direct connection.
1766 GCP_is_neighbor (const struct CadetPeer *peer)
1768 struct CadetPeerPath *path;
1770 if (NULL == peer->connections)
1773 for (path = peer->path_head; NULL != path; path = path->next)
1775 if (3 > path->length)
1779 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1785 * Create and initialize a new tunnel towards a peer, in case it has none.
1786 * In case the peer already has a tunnel, nothing is done.
1788 * Does not generate any traffic, just creates the local data structures.
1790 * @param peer Peer towards which to create the tunnel.
1793 GCP_add_tunnel (struct CadetPeer *peer)
1795 if (NULL != peer->tunnel)
1797 peer->tunnel = GCT_new (peer);
1802 * Add a connection to a neighboring peer.
1804 * Store that the peer is the first hop of the connection in one
1805 * direction and that on peer disconnect the connection must be
1806 * notified and destroyed, for it will no longer be valid.
1808 * @param peer Peer to add connection to.
1809 * @param c Connection to add.
1811 * @return GNUNET_OK on success.
1814 GCP_add_connection (struct CadetPeer *peer,
1815 struct CadetConnection *c)
1818 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding connection %s\n", GCC_2s (c));
1819 LOG (GNUNET_ERROR_TYPE_DEBUG, "to peer %s\n", GCP_2s (peer));
1821 if (NULL == peer->connections)
1824 LOG (GNUNET_ERROR_TYPE_DEBUG,
1825 "Peer %s is not a neighbor!\n",
1827 return GNUNET_SYSERR;
1829 LOG (GNUNET_ERROR_TYPE_DEBUG,
1830 "peer %s ok, has %u connections.\n",
1831 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1832 result = GNUNET_CONTAINER_multihashmap_put (peer->connections,
1835 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1836 LOG (GNUNET_ERROR_TYPE_DEBUG,
1837 " now has %u connections.\n",
1838 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1839 LOG (GNUNET_ERROR_TYPE_DEBUG, "result %u\n", result);
1846 * Add the path to the peer and update the path used to reach it in case this
1849 * @param peer Destination peer to add the path to.
1850 * @param path New path to add. Last peer must be the peer in arg 1.
1851 * Path will be either used of freed if already known.
1852 * @param trusted Do we trust that this path is real?
1854 * @return path if path was taken, pointer to existing duplicate if exists
1857 struct CadetPeerPath *
1858 GCP_add_path (struct CadetPeer *peer, struct CadetPeerPath *path,
1861 struct CadetPeerPath *aux;
1865 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1866 path->length, GCP_2s (peer));
1868 if (NULL == peer || NULL == path
1869 || path->peers[path->length - 1] != peer->id)
1872 path_destroy (path);
1876 for (l = 1; l < path->length; l++)
1878 if (path->peers[l] == myid)
1880 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1881 for (l2 = 0; l2 < path->length - l; l2++)
1883 path->peers[l2] = path->peers[l + l2];
1887 path->peers = GNUNET_realloc (path->peers,
1888 path->length * sizeof (GNUNET_PEER_Id));
1892 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1894 if (2 >= path->length && GNUNET_NO == trusted)
1896 /* Only allow CORE to tell us about direct paths */
1897 path_destroy (path);
1901 l = path_get_length (path);
1904 path_destroy (path);
1908 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1909 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1911 l2 = path_get_length (aux);
1914 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1915 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1916 peer->path_tail, aux, path);
1917 if (NULL != peer->tunnel && 3 < GCT_count_connections (peer->tunnel))
1925 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1927 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1928 path_destroy (path);
1933 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
1935 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1936 if (NULL != peer->tunnel && 3 < GCT_count_connections (peer->tunnel))
1945 * Add the path to the origin peer and update the path used to reach it in case
1946 * this is the shortest.
1947 * The path is given in peer_info -> destination, therefore we turn the path
1948 * upside down first.
1950 * @param peer Peer to add the path to, being the origin of the path.
1951 * @param path New path to add after being inversed.
1952 * Path will be either used or freed.
1953 * @param trusted Do we trust that this path is real?
1955 * @return path if path was taken, pointer to existing duplicate if exists
1958 struct CadetPeerPath *
1959 GCP_add_path_to_origin (struct CadetPeer *peer,
1960 struct CadetPeerPath *path,
1966 return GCP_add_path (peer, path, trusted);
1971 * Adds a path to the info of all the peers in the path
1973 * @param p Path to process.
1974 * @param confirmed Whether we know if the path works or not.
1977 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
1981 /* TODO: invert and add */
1982 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1983 for (i++; i < p->length; i++)
1985 struct CadetPeer *aux;
1986 struct CadetPeerPath *copy;
1988 aux = GCP_get_short (p->peers[i]);
1989 copy = path_duplicate (p);
1990 copy->length = i + 1;
1991 GCP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1997 * Remove any path to the peer that has the extact same peers as the one given.
1999 * @param peer Peer to remove the path from.
2000 * @param path Path to remove. Is always destroyed .
2003 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
2005 struct CadetPeerPath *iter;
2006 struct CadetPeerPath *next;
2008 GNUNET_assert (myid == path->peers[0]);
2009 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2011 LOG (GNUNET_ERROR_TYPE_INFO, "Removing path %p (%u) from %s\n",
2012 path, path->length, GCP_2s (peer));
2014 for (iter = peer->path_head; NULL != iter; iter = next)
2017 if (0 == memcmp (path->peers, iter->peers,
2018 sizeof (GNUNET_PEER_Id) * path->length)
2019 && iter->length == path->length)
2021 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
2023 path_destroy (iter);
2026 path_destroy (path);
2031 * Remove a connection from a neighboring peer.
2033 * @param peer Peer to remove connection from.
2034 * @param c Connection to remove.
2036 * @return GNUNET_OK on success.
2039 GCP_remove_connection (struct CadetPeer *peer,
2040 const struct CadetConnection *c)
2042 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing connection %s\n", GCC_2s (c));
2043 LOG (GNUNET_ERROR_TYPE_DEBUG, "from peer %s\n", GCP_2s (peer));
2045 if (NULL == peer || NULL == peer->connections)
2047 LOG (GNUNET_ERROR_TYPE_DEBUG,
2048 "Peer %s is not a neighbor!\n",
2050 return GNUNET_SYSERR;
2052 LOG (GNUNET_ERROR_TYPE_DEBUG,
2053 "peer %s ok, has %u connections.\n",
2054 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
2056 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2062 * Start the DHT search for new paths towards the peer: we don't have
2063 * enough good connections.
2065 * @param peer Destination peer.
2068 GCP_start_search (struct CadetPeer *peer)
2070 if (NULL != peer->search_h)
2076 peer->search_h = GCD_search (GCP_get_id (peer), &search_handler, peer);
2081 * Stop the DHT search for new paths towards the peer: we already have
2082 * enough good connections.
2084 * @param peer Destination peer.
2087 GCP_stop_search (struct CadetPeer *peer)
2089 if (NULL == peer->search_h)
2094 GCD_search_stop (peer->search_h);
2095 peer->search_h = NULL;
2100 * Get the Full ID of a peer.
2102 * @param peer Peer to get from.
2104 * @return Full ID of peer.
2106 const struct GNUNET_PeerIdentity *
2107 GCP_get_id (const struct CadetPeer *peer)
2109 return GNUNET_PEER_resolve2 (peer->id);
2114 * Get the Short ID of a peer.
2116 * @param peer Peer to get from.
2118 * @return Short ID of peer.
2121 GCP_get_short_id (const struct CadetPeer *peer)
2134 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2137 if (NULL == t && NULL != peer->search_h)
2139 GCP_stop_search (peer);
2145 * Get the tunnel towards a peer.
2147 * @param peer Peer to get from.
2149 * @return Tunnel towards peer.
2151 struct CadetTunnel *
2152 GCP_get_tunnel (const struct CadetPeer *peer)
2154 return peer->tunnel;
2159 * Set the hello message.
2161 * @param peer Peer whose message to set.
2162 * @param hello Hello message.
2165 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2167 struct GNUNET_HELLO_Message *old;
2170 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2174 old = GCP_get_hello (peer);
2177 size = GNUNET_HELLO_size (hello);
2178 LOG (GNUNET_ERROR_TYPE_DEBUG, " new (%u bytes)\n", size);
2179 peer->hello = GNUNET_malloc (size);
2180 memcpy (peer->hello, hello, size);
2184 peer->hello = GNUNET_HELLO_merge (old, hello);
2185 LOG (GNUNET_ERROR_TYPE_DEBUG, " merge into %p (%u bytes)\n",
2186 peer->hello, GNUNET_HELLO_size (hello));
2193 * Get the hello message.
2195 * @param peer Peer whose message to get.
2197 * @return Hello message.
2199 struct GNUNET_HELLO_Message *
2200 GCP_get_hello (struct CadetPeer *peer)
2202 struct GNUNET_TIME_Absolute expiration;
2203 struct GNUNET_TIME_Relative remaining;
2205 if (NULL == peer->hello)
2208 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2209 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2210 if (0 == remaining.rel_value_us)
2212 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2213 GNUNET_STRINGS_absolute_time_to_string (expiration));
2214 GNUNET_free (peer->hello);
2222 * Try to connect to a peer on TRANSPORT level.
2224 * @param peer Peer to whom to connect.
2227 GCP_try_connect (struct CadetPeer *peer)
2229 struct GNUNET_HELLO_Message *hello;
2230 struct GNUNET_MessageHeader *mh;
2232 if (NULL == transport_handle)
2235 hello = GCP_get_hello (peer);
2239 mh = GNUNET_HELLO_get_header (hello);
2240 GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
2245 * Notify a peer that a link between two other peers is broken. If any path
2246 * used that link, eliminate it.
2248 * @param peer Peer affected by the change.
2249 * @param peer1 Peer whose link is broken.
2250 * @param peer2 Peer whose link is broken.
2253 GCP_notify_broken_link (struct CadetPeer *peer,
2254 struct GNUNET_PeerIdentity *peer1,
2255 struct GNUNET_PeerIdentity *peer2)
2257 struct CadetPeerPath *iter;
2258 struct CadetPeerPath *next;
2263 p1 = GNUNET_PEER_search (peer1);
2264 p2 = GNUNET_PEER_search (peer2);
2266 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2267 if (0 == p1 || 0 == p2)
2269 /* We don't even know them */
2273 for (iter = peer->path_head; NULL != iter; iter = next)
2276 for (i = 0; i < iter->length - 1; i++)
2278 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2279 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2284 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2287 path_invalidate (iter);
2295 * Count the number of known paths toward the peer.
2297 * @param peer Peer to get path info.
2299 * @return Number of known paths.
2302 GCP_count_paths (const struct CadetPeer *peer)
2304 struct CadetPeerPath *iter;
2307 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2315 * Iterate all known peers.
2317 * @param iter Iterator.
2318 * @param cls Closure for @c iter.
2321 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2323 GNUNET_CONTAINER_multipeermap_iterate (peers, iter, cls);
2328 * Get the static string for a peer ID.
2332 * @return Static string for it's ID.
2335 GCP_2s (const struct CadetPeer *peer)
2339 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));