2 This file is part of GNUnet.
3 Copyright (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 * Handle to stop the DHT search for paths to this peer
149 struct GNUNET_SCHEDULER_Task *search_delayed;
152 * Tunnel to this peer, if any.
154 struct CadetTunnel *tunnel;
157 * Connections that go through this peer, indexed by tid;
159 struct GNUNET_CONTAINER_MultiHashMap *connections;
162 * Handle for queued transmissions
164 struct GNUNET_CORE_TransmitHandle *core_transmit;
169 struct GNUNET_TIME_Absolute tmt_time;
172 * Transmission queue to core DLL head
174 struct CadetPeerQueue *queue_head;
177 * Transmission queue to core DLL tail
179 struct CadetPeerQueue *queue_tail;
182 * How many messages are in the queue to this peer.
184 unsigned int queue_n;
189 struct GNUNET_HELLO_Message* hello;
193 /******************************************************************************/
194 /******************************* GLOBALS ***********************************/
195 /******************************************************************************/
198 * Global handle to the statistics service.
200 extern struct GNUNET_STATISTICS_Handle *stats;
203 * Local peer own ID (full value).
205 extern struct GNUNET_PeerIdentity my_full_id;
208 * Local peer own ID (short)
210 extern GNUNET_PEER_Id myid;
213 * Peers known, indexed by PeerIdentity (CadetPeer).
215 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
218 * How many peers do we want to remember?
220 static unsigned long long max_peers;
223 * Percentage of messages that will be dropped (for test purposes only).
225 static unsigned long long drop_percent;
228 * Handle to communicate with core.
230 static struct GNUNET_CORE_Handle *core_handle;
233 * Handle to try to start new connections.
235 static struct GNUNET_TRANSPORT_Handle *transport_handle;
238 /******************************************************************************/
239 /***************************** DEBUG *********************************/
240 /******************************************************************************/
243 * Log all kinds of info about the queueing status of a peer.
245 * @param p Peer whose queue to show.
246 * @param level Error level to use for logging.
249 queue_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
251 struct CadetPeerQueue *q;
254 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
256 __FILE__, __FUNCTION__, __LINE__);
260 LOG2 (level, "QQQ Message queue towards %s\n", GCP_2s (p));
261 LOG2 (level, "QQQ queue length: %u\n", p->queue_n);
262 LOG2 (level, "QQQ core tmt rdy: %p\n", p->core_transmit);
264 for (q = p->queue_head; NULL != q; q = q->next)
266 LOG2 (level, "QQQ - %s %s on %s\n",
267 GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
268 LOG2 (level, "QQQ payload %s, %u\n",
269 GC_m2s (q->payload_type), q->payload_id);
270 LOG2 (level, "QQQ size: %u bytes\n", q->size);
273 LOG2 (level, "QQQ End queue towards %s\n", GCP_2s (p));
278 * Log all kinds of info about a peer.
283 GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
285 struct CadetPeerPath *path;
289 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
291 __FILE__, __FUNCTION__, __LINE__);
297 LOG2 (level, "PPP DEBUG PEER NULL\n");
301 LOG2 (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
302 LOG2 (level, "PPP last contact %s\n",
303 GNUNET_STRINGS_absolute_time_to_string (p->last_contact));
304 for (path = p->path_head; NULL != path; path = path->next)
309 LOG2 (level, "PPP path: %s\n", s);
313 LOG2 (level, "PPP core transmit handle %p\n", p->core_transmit);
314 LOG2 (level, "PPP DHT GET handle %p\n", p->search_h);
315 if (NULL != p->connections)
316 conns = GNUNET_CONTAINER_multihashmap_size (p->connections);
319 LOG2 (level, "PPP # connections over link to peer: %u\n", conns);
320 queue_debug (p, level);
321 LOG2 (level, "PPP DEBUG END\n");
325 /******************************************************************************/
326 /***************************** CORE HELPERS *********************************/
327 /******************************************************************************/
331 * Iterator to notify all connections of a broken link. Mark connections
332 * to destroy after all traffic has been sent.
334 * @param cls Closure (peer disconnected).
335 * @param key Current key code (peer id).
336 * @param value Value in the hash map (connection).
338 * @return #GNUNET_YES to continue to iterate.
341 notify_broken (void *cls,
342 const struct GNUNET_HashCode *key,
345 struct CadetPeer *peer = cls;
346 struct CadetConnection *c = value;
348 LOG (GNUNET_ERROR_TYPE_DEBUG, " notifying %s due to %s\n",
349 GCC_2s (c), GCP_2s (peer));
350 GCC_notify_broken (c, peer);
357 * Remove the direct path to the peer.
359 * @param peer Peer to remove the direct path from.
362 static struct CadetPeerPath *
363 pop_direct_path (struct CadetPeer *peer)
365 struct CadetPeerPath *iter;
367 for (iter = peer->path_head; NULL != iter; iter = iter->next)
369 if (2 >= iter->length)
371 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
379 /******************************************************************************/
380 /***************************** CORE CALLBACKS *********************************/
381 /******************************************************************************/
384 * Method called whenever a given peer connects.
387 * @param peer peer identity this notification is about
390 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
392 struct CadetPeer *mp;
393 struct CadetPeerPath *path;
396 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
400 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s (self)\n", own_id);
405 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s <= %s\n",
406 own_id, GNUNET_i2s (peer));
408 path->peers[1] = mp->id;
409 GNUNET_PEER_change_rc (mp->id, 1);
410 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
412 path->peers[0] = myid;
413 GNUNET_PEER_change_rc (myid, 1);
414 GCP_add_path (mp, path, GNUNET_YES);
416 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
418 if (NULL != GCP_get_tunnel (mp) &&
419 0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer))
429 * Method called whenever a peer disconnects.
432 * @param peer peer identity this notification is about
435 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
438 struct CadetPeerPath *direct_path;
441 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
442 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
449 LOG (GNUNET_ERROR_TYPE_INFO, "DISCONNECTED %s (self)\n", own_id);
451 LOG (GNUNET_ERROR_TYPE_INFO, "DISCONNECTED %s <= %s\n",
452 own_id, GNUNET_i2s (peer));
453 direct_path = pop_direct_path (p);
454 GNUNET_CONTAINER_multihashmap_iterate (p->connections, ¬ify_broken, p);
455 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
456 p->connections = NULL;
457 if (NULL != p->core_transmit)
459 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
460 p->core_transmit = NULL;
461 p->tmt_time.abs_value_us = 0;
463 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
465 path_destroy (direct_path);
471 * Functions to handle messages from core
473 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
474 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
475 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
476 sizeof (struct GNUNET_CADET_ConnectionACK)},
477 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
478 sizeof (struct GNUNET_CADET_ConnectionBroken)},
479 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
480 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
481 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
482 sizeof (struct GNUNET_CADET_ACK)},
483 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
484 sizeof (struct GNUNET_CADET_Poll)},
485 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
486 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0}
491 * To be called on core init/fail.
493 * @param cls Closure (config)
494 * @param identity the public identity of this peer
497 core_init (void *cls,
498 const struct GNUNET_PeerIdentity *identity)
500 const struct GNUNET_CONFIGURATION_Handle *c = cls;
503 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
504 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
506 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
507 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
508 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
509 GNUNET_CORE_disconnect (core_handle);
510 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
511 NULL, /* Closure passed to CADET functions */
512 &core_init, /* Call core_init once connected */
513 &core_connect, /* Handle connects */
514 &core_disconnect, /* remove peers on disconnects */
515 NULL, /* Don't notify about all incoming messages */
516 GNUNET_NO, /* For header only in notification */
517 NULL, /* Don't notify about all outbound messages */
518 GNUNET_NO, /* For header-only out notification */
519 core_handlers); /* Register these handlers */
529 * Core callback to write a pre-constructed data packet to core buffer
531 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
532 * @param size Number of bytes available in buf.
533 * @param buf Where the to write the message.
535 * @return number of bytes written to buf
538 send_core_data_raw (void *cls, size_t size, void *buf)
540 struct GNUNET_MessageHeader *msg = cls;
543 GNUNET_assert (NULL != msg);
544 total_size = ntohs (msg->size);
546 if (total_size > size)
551 memcpy (buf, msg, total_size);
558 * Function to send a create connection message to a peer.
560 * @param c Connection to create.
561 * @param size number of bytes available in buf
562 * @param buf where the callee should write the message
563 * @return number of bytes written to buf
566 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
568 struct GNUNET_CADET_ConnectionCreate *msg;
569 struct GNUNET_PeerIdentity *peer_ptr;
570 const struct CadetPeerPath *p = GCC_get_path (c);
577 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
579 sizeof (struct GNUNET_CADET_ConnectionCreate) +
580 p->length * sizeof (struct GNUNET_PeerIdentity);
582 if (size < size_needed || NULL == buf)
587 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
588 msg->header.size = htons (size_needed);
589 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
590 msg->cid = *GCC_get_id (c);
592 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
593 for (i = 0; i < p->length; i++)
595 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
598 LOG (GNUNET_ERROR_TYPE_DEBUG,
599 "CONNECTION CREATE (%u bytes long) sent!\n",
606 * Creates a path ack message in buf and frees all unused resources.
608 * @param c Connection to send an ACK on.
609 * @param size number of bytes available in buf
610 * @param buf where the callee should write the message
612 * @return number of bytes written to buf
615 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
617 struct GNUNET_CADET_ConnectionACK *msg = buf;
619 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
620 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
625 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
626 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
627 msg->cid = *GCC_get_id (c);
629 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
630 return sizeof (struct GNUNET_CADET_ConnectionACK);
634 /******************************************************************************/
635 /******************************** STATIC ***********************************/
636 /******************************************************************************/
640 * Get priority for a queued message.
642 * @param q Queued message
644 * @return CORE priority to use.
646 static enum GNUNET_CORE_Priority
647 get_priority (struct CadetPeerQueue *q)
649 enum GNUNET_CORE_Priority low;
650 enum GNUNET_CORE_Priority high;
655 return GNUNET_CORE_PRIO_BACKGROUND;
658 /* Relayed traffic has lower priority, our own traffic has higher */
659 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
661 low = GNUNET_CORE_PRIO_BEST_EFFORT;
662 high = GNUNET_CORE_PRIO_URGENT;
666 low = GNUNET_CORE_PRIO_URGENT;
667 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
670 /* Bulky payload has lower priority, control traffic has higher. */
671 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type)
679 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
682 * @param key current key code
683 * @param value value in the hash map
684 * @return #GNUNET_YES if we should continue to iterate,
688 shutdown_tunnel (void *cls,
689 const struct GNUNET_PeerIdentity *key,
692 struct CadetPeer *p = value;
693 struct CadetTunnel *t = p->tunnel;
703 * Check if peer is searching for a path (either active or delayed search).
705 * @param peer Peer to check
707 * @return GNUNET_YES if there is a search active.
708 * GNUNET_NO otherwise.
711 is_searching (const struct CadetPeer *peer)
713 return (NULL == peer->search_h && NULL == peer->search_delayed) ?
714 GNUNET_NO : GNUNET_YES;
719 * @brief Start a search for a peer.
721 * @param cls Closure (Peer to search for).
722 * @param tc Task context.
725 delayed_search (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
727 struct CadetPeer *peer = cls;
729 peer->search_delayed = NULL;
731 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
734 GCP_start_search (peer);
739 * Destroy the peer_info and free any allocated resources linked to it
741 * @param peer The peer_info to destroy.
743 * @return GNUNET_OK on success
746 peer_destroy (struct CadetPeer *peer)
748 struct GNUNET_PeerIdentity id;
749 struct CadetPeerPath *p;
750 struct CadetPeerPath *nextp;
752 GNUNET_PEER_resolve (peer->id, &id);
753 GNUNET_PEER_change_rc (peer->id, -1);
755 LOG (GNUNET_ERROR_TYPE_WARNING, "destroying peer %s\n", GNUNET_i2s (&id));
758 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
761 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
763 GCP_stop_search (peer);
768 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
772 GCT_destroy_empty (peer->tunnel);
779 * Returns if peer is used (has a tunnel or is neighbor).
781 * @param peer Peer to check.
783 * @return #GNUNET_YES if peer is in use.
786 peer_is_used (struct CadetPeer *peer)
788 struct CadetPeerPath *p;
790 if (NULL != peer->tunnel)
793 for (p = peer->path_head; NULL != p; p = p->next)
803 * Iterator over all the peers to get the oldest timestamp.
805 * @param cls Closure (unsued).
806 * @param key ID of the peer.
807 * @param value Peer_Info of the peer.
810 peer_get_oldest (void *cls,
811 const struct GNUNET_PeerIdentity *key,
814 struct CadetPeer *p = value;
815 struct GNUNET_TIME_Absolute *abs = cls;
817 /* Don't count active peers */
818 if (GNUNET_YES == peer_is_used (p))
821 if (abs->abs_value_us < p->last_contact.abs_value_us)
822 abs->abs_value_us = p->last_contact.abs_value_us;
829 * Iterator over all the peers to remove the oldest entry.
831 * @param cls Closure (unsued).
832 * @param key ID of the peer.
833 * @param value Peer_Info of the peer.
836 peer_timeout (void *cls,
837 const struct GNUNET_PeerIdentity *key,
840 struct CadetPeer *p = value;
841 struct GNUNET_TIME_Absolute *abs = cls;
843 LOG (GNUNET_ERROR_TYPE_WARNING,
844 "peer %s timeout\n", GNUNET_i2s (key));
846 if (p->last_contact.abs_value_us == abs->abs_value_us &&
847 GNUNET_NO == peer_is_used (p))
857 * Delete oldest unused peer.
860 peer_delete_oldest (void)
862 struct GNUNET_TIME_Absolute abs;
864 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
866 GNUNET_CONTAINER_multipeermap_iterate (peers,
869 GNUNET_CONTAINER_multipeermap_iterate (peers,
876 * Choose the best (yet unused) path towards a peer,
877 * considering the tunnel properties.
879 * @param peer The destination peer.
881 * @return Best current known path towards the peer, if any.
883 static struct CadetPeerPath *
884 peer_get_best_path (const struct CadetPeer *peer)
886 struct CadetPeerPath *best_p;
887 struct CadetPeerPath *p;
888 unsigned int best_cost;
891 best_cost = UINT_MAX;
893 for (p = peer->path_head; NULL != p; p = p->next)
895 if (GNUNET_NO == path_is_valid (p))
896 continue; /* Don't use invalid paths. */
897 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
898 continue; /* If path is already in use, skip it. */
900 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
911 * Is this queue element sendable?
913 * - All management traffic is always sendable.
914 * - For payload traffic, check the connection flow control.
916 * @param q Queue element to inspect.
918 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
921 queue_is_sendable (struct CadetPeerQueue *q)
923 /* Is PID-independent? */
926 case GNUNET_MESSAGE_TYPE_CADET_ACK:
927 case GNUNET_MESSAGE_TYPE_CADET_POLL:
928 case GNUNET_MESSAGE_TYPE_CADET_KX:
929 case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
930 case GNUNET_MESSAGE_TYPE_CADET_AX:
931 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
932 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
933 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
934 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
937 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
944 return GCC_is_sendable (q->c, q->fwd);
949 * Get first sendable message.
951 * @param peer The destination peer.
953 * @return First transmittable message, if any. Otherwise, NULL.
955 static struct CadetPeerQueue *
956 peer_get_first_message (const struct CadetPeer *peer)
958 struct CadetPeerQueue *q;
960 for (q = peer->queue_head; NULL != q; q = q->next)
962 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
963 if (queue_is_sendable (q))
972 * Function to process paths received for a new peer addition. The recorded
973 * paths form the initial tunnel, which can be optimized later.
974 * Called on each result obtained for the DHT search.
980 search_handler (void *cls, const struct CadetPeerPath *path)
982 struct CadetPeer *peer = cls;
983 unsigned int connection_count;
985 GCP_add_path_to_all (path, GNUNET_NO);
987 /* Count connections */
988 connection_count = GCT_count_connections (peer->tunnel);
990 /* If we already have our minimum (or more) connections, it's enough */
991 if (CONNECTIONS_PER_TUNNEL <= connection_count)
994 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
996 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1004 * Adjust core requested size to accomodate an ACK.
1006 * @param message_size Requested size.
1008 * @return Size enough to fit @c message_size and an ACK.
1011 get_core_size (size_t message_size)
1013 return message_size + sizeof (struct GNUNET_CADET_ACK);
1018 * Fill a core buffer with the appropriate data for the queued message.
1020 * @param queue Queue element for the message.
1021 * @param buf Core buffer to fill.
1022 * @param size Size remaining in @c buf.
1023 * @param[out] pid In case its an encrypted payload, set payload.
1025 * @return Bytes written to @c buf.
1028 fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
1030 struct CadetConnection *c = queue->c;
1033 switch (queue->type)
1035 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1036 *pid = GCC_get_pid (queue->c, queue->fwd);
1037 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload ID %u\n", *pid);
1038 msg_size = send_core_data_raw (queue->cls, size, buf);
1039 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
1041 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1042 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1043 case GNUNET_MESSAGE_TYPE_CADET_KX:
1044 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1045 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1046 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1047 msg_size = send_core_data_raw (queue->cls, size, buf);
1049 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1051 if (GCC_is_origin (c, GNUNET_YES))
1052 msg_size = send_core_connection_create (c, size, buf);
1054 msg_size = send_core_data_raw (queue->cls, size, buf);
1056 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1057 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1058 if (GCC_is_origin (c, GNUNET_NO) ||
1059 GCC_is_origin (c, GNUNET_YES))
1061 msg_size = send_core_connection_ack (c, size, buf);
1065 msg_size = send_core_data_raw (queue->cls, size, buf);
1068 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1069 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1070 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1071 /* This should be encapsulted */
1077 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1081 GNUNET_assert (size >= msg_size);
1088 * Core callback to write a queued packet to core buffer
1090 * @param cls Closure (peer info).
1091 * @param size Number of bytes available in buf.
1092 * @param buf Where the to write the message.
1094 * @return number of bytes written to buf
1097 queue_send (void *cls, size_t size, void *buf)
1099 struct CadetPeer *peer = cls;
1100 struct CadetConnection *c;
1101 struct CadetPeerQueue *queue;
1102 struct GNUNET_TIME_Relative core_wait_time;
1103 const struct GNUNET_PeerIdentity *dst_id;
1110 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1111 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1112 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
1113 GCP_2s (peer), size);
1115 /* Sanity checking */
1116 if (NULL == buf || 0 == size)
1118 LOG (GNUNET_ERROR_TYPE_DEBUG, "Buffer size 0.\n");
1119 peer->tmt_time.abs_value_us = 0;
1120 peer->core_transmit = NULL;
1129 peer->core_transmit = NULL;
1130 queue = peer_get_first_message (peer);
1133 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
1134 peer->tmt_time.abs_value_us = 0;
1137 core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1138 LOG (GNUNET_ERROR_TYPE_DEBUG, " core wait time %s\n",
1139 GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_NO));
1140 peer->tmt_time.abs_value_us = 0;
1142 /* Copy all possible messages to the core buffer */
1143 while (NULL != queue && rest >= queue->size)
1147 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s %s\n",
1148 GCC_2s (c), GC_f2s(queue->fwd));
1149 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok (%u/%u)\n",
1150 queue->size, total_size, size);
1152 msg_size = fill_buf (queue, (void *) dst, size, &pid);
1154 if (0 < drop_percent &&
1155 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1157 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on connection %s %s\n",
1158 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1159 queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
1164 LOG (GNUNET_ERROR_TYPE_INFO,
1165 "snd %s (%s %9u) on connection %s (%p) %s (size %u)\n",
1166 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1167 queue->payload_id, GCC_2s (c), c, GC_f2s (queue->fwd), msg_size);
1169 total_size += msg_size;
1171 dst = &dst[msg_size];
1174 /* Free queue, but cls was freed by send_core_* in fill_buf. */
1175 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1178 queue = peer_get_first_message (peer);
1181 /* If more data in queue, send next */
1184 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data! (%u)\n", queue->size);
1185 if (NULL == peer->core_transmit)
1187 dst_id = GNUNET_PEER_resolve2 (peer->id);
1188 peer->core_transmit =
1189 GNUNET_CORE_notify_transmit_ready (core_handle,
1190 GNUNET_NO, get_priority (queue),
1191 GNUNET_TIME_UNIT_FOREVER_REL,
1193 get_core_size (queue->size),
1196 peer->tmt_time = GNUNET_TIME_absolute_get ();
1197 queue->start_waiting = GNUNET_TIME_absolute_get ();
1201 LOG (GNUNET_ERROR_TYPE_DEBUG, "* tmt rdy called somewhere else\n");
1203 // GCC_start_poll (); FIXME needed?
1207 // GCC_stop_poll(); FIXME needed?
1210 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", total_size);
1211 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1217 /******************************************************************************/
1218 /******************************** API ***********************************/
1219 /******************************************************************************/
1223 * Free a transmission that was already queued with all resources
1224 * associated to the request.
1226 * If connection was marked to be destroyed, and this was the last queued
1227 * message on it, the connection will be free'd as a result.
1229 * @param queue Queue handler to cancel.
1230 * @param clear_cls Is it necessary to free associated cls?
1231 * @param sent Was it really sent? (Could have been canceled)
1232 * @param pid PID, if relevant (was sent and was a payload message).
1234 * @return #GNUNET_YES if connection was destroyed as a result,
1235 * #GNUNET_NO otherwise.
1238 GCP_queue_destroy (struct CadetPeerQueue *queue, int clear_cls,
1239 int sent, uint32_t pid)
1241 struct CadetPeer *peer;
1242 int connection_destroyed;
1245 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1246 if (GNUNET_YES == clear_cls)
1248 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1249 switch (queue->type)
1251 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1252 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1254 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1255 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1256 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1257 case GNUNET_MESSAGE_TYPE_CADET_KX:
1258 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1259 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1260 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1261 GNUNET_free_non_null (queue->cls);
1266 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1267 GC_m2s (queue->type));
1270 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1272 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1273 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1278 if (NULL != queue->callback)
1280 struct GNUNET_TIME_Relative wait_time;
1282 wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1283 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1284 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1285 connection_destroyed = queue->callback (queue->callback_cls,
1286 queue->c, sent, queue->type, pid,
1287 queue->fwd, queue->size,
1292 connection_destroyed = GNUNET_NO;
1295 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1297 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1298 peer->core_transmit = NULL;
1299 peer->tmt_time.abs_value_us = 0;
1302 GNUNET_free (queue);
1303 return connection_destroyed;
1308 * @brief Queue and pass message to core when possible.
1310 * @param peer Peer towards which to queue the message.
1311 * @param cls Closure (@c type dependant). It will be used by queue_send to
1312 * build the message to be sent if not already prebuilt.
1313 * @param type Type of the message, 0 for a raw message.
1314 * @param size Size of the message.
1315 * @param c Connection this message belongs to (can be NULL).
1316 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1317 * @param cont Continuation to be called once CORE has taken the message.
1318 * @param cont_cls Closure for @c cont.
1320 * @return Handle to cancel the message before it is sent. Once cont is called
1321 * message has been sent and therefore the handle is no longer valid.
1323 struct CadetPeerQueue *
1324 GCP_queue_add (struct CadetPeer *peer, void *cls, uint16_t type,
1325 uint16_t payload_type, uint32_t payload_id, size_t size,
1326 struct CadetConnection *c, int fwd,
1327 GCP_sent cont, void *cont_cls)
1329 struct CadetPeerQueue *q;
1334 if (NULL == c && GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN != type)
1335 error_level = GNUNET_ERROR_TYPE_ERROR;
1337 error_level = GNUNET_ERROR_TYPE_INFO;
1339 "que %s (%s %9u) on connection %s (%p) %s towards %s (size %u)\n",
1340 GC_m2s (type), GC_m2s (payload_type), payload_id,
1341 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1343 if (error_level == GNUNET_ERROR_TYPE_ERROR)
1345 if (NULL == peer->connections)
1347 /* We are not connected to this peer, ignore request. */
1348 LOG (GNUNET_ERROR_TYPE_WARNING, "%s not a neighbor\n", GCP_2s (peer));
1349 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1356 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1357 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1362 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1364 call_core = (NULL == c || type == GNUNET_MESSAGE_TYPE_CADET_KX) ?
1365 GNUNET_YES : GCC_is_sendable (c, fwd);
1366 q = GNUNET_new (struct CadetPeerQueue);
1369 q->payload_type = payload_type;
1370 q->payload_id = payload_id;
1376 q->callback_cls = cont_cls;
1379 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1384 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1385 call_core = GNUNET_YES;
1388 q->start_waiting = GNUNET_TIME_absolute_get ();
1389 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1391 LOG (GNUNET_ERROR_TYPE_DEBUG,
1392 "calling core tmt rdy towards %s for %u bytes\n",
1393 GCP_2s (peer), size);
1394 peer->core_transmit =
1395 GNUNET_CORE_notify_transmit_ready (core_handle,
1396 GNUNET_NO, get_priority (q),
1397 GNUNET_TIME_UNIT_FOREVER_REL,
1398 GNUNET_PEER_resolve2 (peer->id),
1399 get_core_size (size),
1401 peer->tmt_time = GNUNET_TIME_absolute_get ();
1403 else if (GNUNET_NO == call_core)
1405 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1411 struct GNUNET_TIME_Relative elapsed;
1412 elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1413 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
1415 GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
1418 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1424 * Cancel all queued messages to a peer that belong to a certain connection.
1426 * @param peer Peer towards whom to cancel.
1427 * @param c Connection whose queued messages to cancel. Might be destroyed by
1428 * the sent continuation call.
1431 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c)
1433 struct CadetPeerQueue *q;
1434 struct CadetPeerQueue *next;
1435 struct CadetPeerQueue *prev;
1436 int connection_destroyed;
1438 connection_destroyed = GNUNET_NO;
1439 for (q = peer->queue_head; NULL != q; q = next)
1444 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP queue cancel %s\n", GC_m2s (q->type));
1445 GNUNET_break (GNUNET_NO == connection_destroyed);
1446 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1452 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1455 /* Get next from prev, q->next might be already freed:
1456 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1459 next = peer->queue_head;
1469 if (NULL == peer->queue_head && NULL != peer->core_transmit)
1471 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1472 peer->core_transmit = NULL;
1473 peer->tmt_time.abs_value_us = 0;
1479 * Get the first transmittable message for a connection.
1481 * @param peer Neighboring peer.
1482 * @param c Connection.
1484 * @return First transmittable message.
1486 static struct CadetPeerQueue *
1487 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1489 struct CadetPeerQueue *q;
1491 for (q = peer->queue_head; NULL != q; q = q->next)
1495 if (queue_is_sendable (q))
1497 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1500 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1508 * Get the first message for a connection and unqueue it.
1510 * Only tunnel (or higher) level messages are unqueued. Connection specific
1511 * messages are silently destroyed upon encounter.
1513 * @param peer Neighboring peer.
1514 * @param c Connection.
1515 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1518 * @return First message for this connection.
1520 struct GNUNET_MessageHeader *
1521 GCP_connection_pop (struct CadetPeer *peer,
1522 struct CadetConnection *c,
1525 struct CadetPeerQueue *q;
1526 struct CadetPeerQueue *next;
1527 struct GNUNET_MessageHeader *msg;
1530 GNUNET_assert (NULL != destroyed);
1531 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on connection %p\n", c);
1532 for (q = peer->queue_head; NULL != q; q = next)
1537 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), callback: %p\n",
1538 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1542 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1543 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1544 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1545 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1546 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1547 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1548 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1549 if (GNUNET_YES == dest)
1551 GNUNET_break (GNUNET_NO == *destroyed);
1552 *destroyed = GNUNET_YES;
1556 case GNUNET_MESSAGE_TYPE_CADET_KX:
1557 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1558 msg = (struct GNUNET_MessageHeader *) q->cls;
1559 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1560 if (GNUNET_YES == dest)
1562 GNUNET_break (GNUNET_NO == *destroyed);
1563 *destroyed = GNUNET_YES;
1569 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1577 * Unlock a possibly locked queue for a connection.
1579 * If there is a message that can be sent on this connection, call core for it.
1580 * Otherwise (if core transmit is already called or there is no sendable
1581 * message) do nothing.
1583 * @param peer Peer who keeps the queue.
1584 * @param c Connection whose messages to unlock.
1587 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1589 struct CadetPeerQueue *q;
1592 if (NULL != peer->core_transmit)
1594 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1595 return; /* Already unlocked */
1598 q = connection_get_first_message (peer, c);
1601 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1602 return; /* Nothing to transmit */
1606 peer->core_transmit =
1607 GNUNET_CORE_notify_transmit_ready (core_handle,
1608 GNUNET_NO, get_priority (q),
1609 GNUNET_TIME_UNIT_FOREVER_REL,
1610 GNUNET_PEER_resolve2 (peer->id),
1611 get_core_size (size),
1614 peer->tmt_time = GNUNET_TIME_absolute_get ();
1619 * Initialize the peer subsystem.
1621 * @param c Configuration.
1624 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1626 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1627 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1629 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1632 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1633 "CADET", "MAX_PEERS", "USING DEFAULT");
1638 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1645 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1646 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1647 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1648 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1649 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1652 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1653 NULL, /* Closure passed to CADET functions */
1654 &core_init, /* Call core_init once connected */
1655 &core_connect, /* Handle connects */
1656 &core_disconnect, /* remove peers on disconnects */
1657 NULL, /* Don't notify about all incoming messages */
1658 GNUNET_NO, /* For header only in notification */
1659 NULL, /* Don't notify about all outbound messages */
1660 GNUNET_NO, /* For header-only out notification */
1661 core_handlers); /* Register these handlers */
1663 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1665 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1666 /* Notify callbacks */
1671 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1672 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1673 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1674 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1675 transport_handle = NULL;
1680 if (NULL == core_handle)
1683 GNUNET_SCHEDULER_shutdown ();
1691 * Shut down the peer subsystem.
1696 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1698 if (core_handle != NULL)
1700 GNUNET_CORE_disconnect (core_handle);
1703 if (transport_handle != NULL)
1705 GNUNET_TRANSPORT_disconnect (transport_handle);
1706 transport_handle = NULL;
1708 GNUNET_PEER_change_rc (myid, -1);
1713 * Retrieve the CadetPeer stucture associated with the peer, create one
1714 * and insert it in the appropriate structures if the peer is not known yet.
1716 * @param peer_id Full identity of the peer.
1718 * @return Existing or newly created peer structure.
1721 GCP_get (const struct GNUNET_PeerIdentity *peer_id)
1723 struct CadetPeer *peer;
1725 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1728 peer = GNUNET_new (struct CadetPeer);
1729 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1731 peer_delete_oldest ();
1733 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1734 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1735 peer->id = GNUNET_PEER_intern (peer_id);
1737 peer->last_contact = GNUNET_TIME_absolute_get ();
1744 * Retrieve the CadetPeer stucture associated with the peer, create one
1745 * and insert it in the appropriate structures if the peer is not known yet.
1747 * @param peer Short identity of the peer.
1749 * @return Existing or newly created peer structure.
1752 GCP_get_short (const GNUNET_PEER_Id peer)
1754 return GCP_get (GNUNET_PEER_resolve2 (peer));
1759 * Try to connect to a peer on transport level.
1761 * @param cls Closure (peer).
1762 * @param tc TaskContext.
1765 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1767 struct CadetPeer *peer = cls;
1769 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1772 GNUNET_TRANSPORT_try_connect (transport_handle,
1773 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1778 * Try to establish a new connection to this peer (in its tunnel).
1779 * If the peer doesn't have any path to it yet, try to get one.
1780 * If the peer already has some path, send a CREATE CONNECTION towards it.
1782 * @param peer Peer to connect to.
1785 GCP_connect (struct CadetPeer *peer)
1787 struct CadetTunnel *t;
1788 struct CadetPeerPath *p;
1789 struct CadetConnection *c;
1792 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1794 /* If we have a current hello, try to connect using it. */
1795 GCP_try_connect (peer);
1799 rerun_search = GNUNET_NO;
1801 if (NULL != peer->path_head)
1803 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1804 p = peer_get_best_path (peer);
1810 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1813 c = GCT_use_path (t, p);
1816 /* This case can happen when the path includes a first hop that is
1817 * not yet known to be connected.
1819 * This happens quite often during testing when running cadet
1820 * under valgrind: core connect notifications come very late and the
1821 * DHT result has already come and created a valid path.
1822 * In this case, the peer->connections hashmap will be NULL and
1823 * tunnel_use_path will not be able to create a connection from that
1826 * Re-running the DHT GET should give core time to callback.
1828 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1829 * updating statistics about this issue.
1831 rerun_search = GNUNET_YES;
1835 GCC_send_create (c);
1841 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1845 if (GNUNET_YES == rerun_search)
1847 struct GNUNET_TIME_Relative delay;
1849 GCP_stop_search (peer);
1850 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1851 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay, &delayed_search,
1856 if (GNUNET_NO == is_searching (peer))
1857 GCP_start_search (peer);
1862 * Chech whether there is a direct (core level) connection to peer.
1864 * @param peer Peer to check.
1866 * @return #GNUNET_YES if there is a direct connection.
1869 GCP_is_neighbor (const struct CadetPeer *peer)
1871 struct CadetPeerPath *path;
1873 if (NULL == peer->connections)
1876 for (path = peer->path_head; NULL != path; path = path->next)
1878 if (3 > path->length)
1882 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1888 * Create and initialize a new tunnel towards a peer, in case it has none.
1889 * In case the peer already has a tunnel, nothing is done.
1891 * Does not generate any traffic, just creates the local data structures.
1893 * @param peer Peer towards which to create the tunnel.
1896 GCP_add_tunnel (struct CadetPeer *peer)
1898 if (NULL != peer->tunnel)
1900 peer->tunnel = GCT_new (peer);
1905 * Add a connection to a neighboring peer.
1907 * Store that the peer is the first hop of the connection in one
1908 * direction and that on peer disconnect the connection must be
1909 * notified and destroyed, for it will no longer be valid.
1911 * @param peer Peer to add connection to.
1912 * @param c Connection to add.
1914 * @return GNUNET_OK on success.
1917 GCP_add_connection (struct CadetPeer *peer,
1918 struct CadetConnection *c)
1921 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding connection %s\n", GCC_2s (c));
1922 LOG (GNUNET_ERROR_TYPE_DEBUG, "to peer %s\n", GCP_2s (peer));
1924 if (NULL == peer->connections)
1927 LOG (GNUNET_ERROR_TYPE_DEBUG,
1928 "Peer %s is not a neighbor!\n",
1930 return GNUNET_SYSERR;
1932 LOG (GNUNET_ERROR_TYPE_DEBUG,
1933 "peer %s ok, has %u connections.\n",
1934 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1935 result = GNUNET_CONTAINER_multihashmap_put (peer->connections,
1938 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1939 LOG (GNUNET_ERROR_TYPE_DEBUG,
1940 " now has %u connections.\n",
1941 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1942 LOG (GNUNET_ERROR_TYPE_DEBUG, "result %u\n", result);
1949 * Add the path to the peer and update the path used to reach it in case this
1952 * @param peer Destination peer to add the path to.
1953 * @param path New path to add. Last peer must be the peer in arg 1.
1954 * Path will be either used of freed if already known.
1955 * @param trusted Do we trust that this path is real?
1957 * @return path if path was taken, pointer to existing duplicate if exists
1960 struct CadetPeerPath *
1961 GCP_add_path (struct CadetPeer *peer, struct CadetPeerPath *path,
1964 struct CadetPeerPath *aux;
1968 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1969 path->length, GCP_2s (peer));
1971 if (NULL == peer || NULL == path
1972 || path->peers[path->length - 1] != peer->id)
1975 path_destroy (path);
1979 for (l = 1; l < path->length; l++)
1981 if (path->peers[l] == myid)
1983 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1984 for (l2 = 0; l2 < path->length - l; l2++)
1986 path->peers[l2] = path->peers[l + l2];
1990 path->peers = GNUNET_realloc (path->peers,
1991 path->length * sizeof (GNUNET_PEER_Id));
1995 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1997 if (2 >= path->length && GNUNET_NO == trusted)
1999 /* Only allow CORE to tell us about direct paths */
2000 path_destroy (path);
2004 l = path_get_length (path);
2007 path_destroy (path);
2011 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2012 for (aux = peer->path_head; aux != NULL; aux = aux->next)
2014 l2 = path_get_length (aux);
2017 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
2018 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
2019 peer->path_tail, aux, path);
2024 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2026 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
2027 path_destroy (path);
2032 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
2034 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
2037 if (NULL != peer->tunnel
2038 && CONNECTIONS_PER_TUNNEL < GCT_count_connections (peer->tunnel))
2047 * Add the path to the origin peer and update the path used to reach it in case
2048 * this is the shortest.
2049 * The path is given in peer_info -> destination, therefore we turn the path
2050 * upside down first.
2052 * @param peer Peer to add the path to, being the origin of the path.
2053 * @param path New path to add after being inversed.
2054 * Path will be either used or freed.
2055 * @param trusted Do we trust that this path is real?
2057 * @return path if path was taken, pointer to existing duplicate if exists
2060 struct CadetPeerPath *
2061 GCP_add_path_to_origin (struct CadetPeer *peer,
2062 struct CadetPeerPath *path,
2068 return GCP_add_path (peer, path, trusted);
2073 * Adds a path to the info of all the peers in the path
2075 * @param p Path to process.
2076 * @param confirmed Whether we know if the path works or not.
2079 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
2083 /* TODO: invert and add */
2084 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2085 for (i++; i < p->length; i++)
2087 struct CadetPeer *aux;
2088 struct CadetPeerPath *copy;
2090 aux = GCP_get_short (p->peers[i]);
2091 copy = path_duplicate (p);
2092 copy->length = i + 1;
2093 GCP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2099 * Remove any path to the peer that has the extact same peers as the one given.
2101 * @param peer Peer to remove the path from.
2102 * @param path Path to remove. Is always destroyed .
2105 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
2107 struct CadetPeerPath *iter;
2108 struct CadetPeerPath *next;
2110 GNUNET_assert (myid == path->peers[0]);
2111 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2113 LOG (GNUNET_ERROR_TYPE_INFO, "Removing path %p (%u) from %s\n",
2114 path, path->length, GCP_2s (peer));
2116 for (iter = peer->path_head; NULL != iter; iter = next)
2119 if (0 == path_cmp (path, iter))
2121 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
2123 path_destroy (iter);
2126 path_destroy (path);
2131 * Remove a connection from a neighboring peer.
2133 * @param peer Peer to remove connection from.
2134 * @param c Connection to remove.
2136 * @return GNUNET_OK on success.
2139 GCP_remove_connection (struct CadetPeer *peer,
2140 const struct CadetConnection *c)
2142 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing connection %s\n", GCC_2s (c));
2143 LOG (GNUNET_ERROR_TYPE_DEBUG, "from peer %s\n", GCP_2s (peer));
2145 if (NULL == peer || NULL == peer->connections)
2147 LOG (GNUNET_ERROR_TYPE_DEBUG,
2148 "Peer %s is not a neighbor!\n",
2150 return GNUNET_SYSERR;
2152 LOG (GNUNET_ERROR_TYPE_DEBUG,
2153 "peer %s ok, has %u connections.\n",
2154 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
2156 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2162 * Start the DHT search for new paths towards the peer: we don't have
2163 * enough good connections.
2165 * @param peer Destination peer.
2168 GCP_start_search (struct CadetPeer *peer)
2170 const struct GNUNET_PeerIdentity *id;
2171 struct CadetTunnel *t = peer->tunnel;
2173 if (NULL != peer->search_h)
2179 if (NULL != peer->search_delayed)
2180 GCP_stop_search (peer);
2182 id = GNUNET_PEER_resolve2 (peer->id);
2183 peer->search_h = GCD_search (id, &search_handler, peer);
2187 /* Why would we search for a peer with no tunnel towards it? */
2192 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
2193 || 0 == GCT_count_any_connections (t))
2195 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2201 * Stop the DHT search for new paths towards the peer: we already have
2202 * enough good connections.
2204 * @param peer Destination peer.
2207 GCP_stop_search (struct CadetPeer *peer)
2209 if (NULL != peer->search_h)
2211 GCD_search_stop (peer->search_h);
2212 peer->search_h = NULL;
2214 if (NULL != peer->search_delayed)
2216 GNUNET_SCHEDULER_cancel (peer->search_delayed);
2217 peer->search_delayed = NULL;
2223 * Get the Full ID of a peer.
2225 * @param peer Peer to get from.
2227 * @return Full ID of peer.
2229 const struct GNUNET_PeerIdentity *
2230 GCP_get_id (const struct CadetPeer *peer)
2232 return GNUNET_PEER_resolve2 (peer->id);
2237 * Get the Short ID of a peer.
2239 * @param peer Peer to get from.
2241 * @return Short ID of peer.
2244 GCP_get_short_id (const struct CadetPeer *peer)
2253 * If tunnel is NULL and there was a search active, stop it, as it's useless.
2259 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2262 if (NULL == t && GNUNET_YES == is_searching (peer))
2264 GCP_stop_search (peer);
2270 * Get the tunnel towards a peer.
2272 * @param peer Peer to get from.
2274 * @return Tunnel towards peer.
2276 struct CadetTunnel *
2277 GCP_get_tunnel (const struct CadetPeer *peer)
2279 return peer->tunnel;
2284 * Set the hello message.
2286 * @param peer Peer whose message to set.
2287 * @param hello Hello message.
2290 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2292 struct GNUNET_HELLO_Message *old;
2295 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2299 old = GCP_get_hello (peer);
2302 size = GNUNET_HELLO_size (hello);
2303 LOG (GNUNET_ERROR_TYPE_DEBUG, " new (%u bytes)\n", size);
2304 peer->hello = GNUNET_malloc (size);
2305 memcpy (peer->hello, hello, size);
2309 peer->hello = GNUNET_HELLO_merge (old, hello);
2310 LOG (GNUNET_ERROR_TYPE_DEBUG, " merge into %p (%u bytes)\n",
2311 peer->hello, GNUNET_HELLO_size (hello));
2318 * Get the hello message.
2320 * @param peer Peer whose message to get.
2322 * @return Hello message.
2324 struct GNUNET_HELLO_Message *
2325 GCP_get_hello (struct CadetPeer *peer)
2327 struct GNUNET_TIME_Absolute expiration;
2328 struct GNUNET_TIME_Relative remaining;
2330 if (NULL == peer->hello)
2333 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2334 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2335 if (0 == remaining.rel_value_us)
2337 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2338 GNUNET_STRINGS_absolute_time_to_string (expiration));
2339 GNUNET_free (peer->hello);
2347 * Try to connect to a peer on TRANSPORT level.
2349 * @param peer Peer to whom to connect.
2352 GCP_try_connect (struct CadetPeer *peer)
2354 struct GNUNET_HELLO_Message *hello;
2355 struct GNUNET_MessageHeader *mh;
2357 if (NULL == transport_handle)
2360 hello = GCP_get_hello (peer);
2364 mh = GNUNET_HELLO_get_header (hello);
2365 GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
2370 * Notify a peer that a link between two other peers is broken. If any path
2371 * used that link, eliminate it.
2373 * @param peer Peer affected by the change.
2374 * @param peer1 Peer whose link is broken.
2375 * @param peer2 Peer whose link is broken.
2378 GCP_notify_broken_link (struct CadetPeer *peer,
2379 struct GNUNET_PeerIdentity *peer1,
2380 struct GNUNET_PeerIdentity *peer2)
2382 struct CadetPeerPath *iter;
2383 struct CadetPeerPath *next;
2388 p1 = GNUNET_PEER_search (peer1);
2389 p2 = GNUNET_PEER_search (peer2);
2391 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2392 if (0 == p1 || 0 == p2)
2394 /* We don't even know them */
2398 for (iter = peer->path_head; NULL != iter; iter = next)
2401 for (i = 0; i < iter->length - 1; i++)
2403 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2404 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2409 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2412 path_invalidate (iter);
2420 * Count the number of known paths toward the peer.
2422 * @param peer Peer to get path info.
2424 * @return Number of known paths.
2427 GCP_count_paths (const struct CadetPeer *peer)
2429 struct CadetPeerPath *iter;
2432 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2440 * Iterate all known peers.
2442 * @param iter Iterator.
2443 * @param cls Closure for @c iter.
2446 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2448 GNUNET_CONTAINER_multipeermap_iterate (peers, iter, cls);
2453 * Get the static string for a peer ID.
2457 * @return Static string for it's ID.
2460 GCP_2s (const struct CadetPeer *peer)
2464 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));