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_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
486 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
492 * To be called on core init/fail.
494 * @param cls Closure (config)
495 * @param identity the public identity of this peer
498 core_init (void *cls,
499 const struct GNUNET_PeerIdentity *identity)
501 const struct GNUNET_CONFIGURATION_Handle *c = cls;
504 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
505 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
507 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
508 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
509 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
510 GNUNET_CORE_disconnect (core_handle);
511 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
512 NULL, /* Closure passed to CADET functions */
513 &core_init, /* Call core_init once connected */
514 &core_connect, /* Handle connects */
515 &core_disconnect, /* remove peers on disconnects */
516 NULL, /* Don't notify about all incoming messages */
517 GNUNET_NO, /* For header only in notification */
518 NULL, /* Don't notify about all outbound messages */
519 GNUNET_NO, /* For header-only out notification */
520 core_handlers); /* Register these handlers */
530 * Core callback to write a pre-constructed data packet to core buffer
532 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
533 * @param size Number of bytes available in buf.
534 * @param buf Where the to write the message.
536 * @return number of bytes written to buf
539 send_core_data_raw (void *cls, size_t size, void *buf)
541 struct GNUNET_MessageHeader *msg = cls;
544 GNUNET_assert (NULL != msg);
545 total_size = ntohs (msg->size);
547 if (total_size > size)
552 memcpy (buf, msg, total_size);
559 * Function to send a create connection message to a peer.
561 * @param c Connection to create.
562 * @param size number of bytes available in buf
563 * @param buf where the callee should write the message
564 * @return number of bytes written to buf
567 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
569 struct GNUNET_CADET_ConnectionCreate *msg;
570 struct GNUNET_PeerIdentity *peer_ptr;
571 const struct CadetPeerPath *p = GCC_get_path (c);
578 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
580 sizeof (struct GNUNET_CADET_ConnectionCreate) +
581 p->length * sizeof (struct GNUNET_PeerIdentity);
583 if (size < size_needed || NULL == buf)
588 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
589 msg->header.size = htons (size_needed);
590 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
591 msg->cid = *GCC_get_id (c);
593 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
594 for (i = 0; i < p->length; i++)
596 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
599 LOG (GNUNET_ERROR_TYPE_DEBUG,
600 "CONNECTION CREATE (%u bytes long) sent!\n",
607 * Creates a path ack message in buf and frees all unused resources.
609 * @param c Connection to send an ACK on.
610 * @param size number of bytes available in buf
611 * @param buf where the callee should write the message
613 * @return number of bytes written to buf
616 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
618 struct GNUNET_CADET_ConnectionACK *msg = buf;
620 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
621 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
626 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
627 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
628 msg->cid = *GCC_get_id (c);
630 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
631 return sizeof (struct GNUNET_CADET_ConnectionACK);
635 /******************************************************************************/
636 /******************************** STATIC ***********************************/
637 /******************************************************************************/
641 * Get priority for a queued message.
643 * @param q Queued message
645 * @return CORE priority to use.
647 static enum GNUNET_CORE_Priority
648 get_priority (struct CadetPeerQueue *q)
650 enum GNUNET_CORE_Priority low;
651 enum GNUNET_CORE_Priority high;
656 return GNUNET_CORE_PRIO_BACKGROUND;
659 /* Relayed traffic has lower priority, our own traffic has higher */
660 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
662 low = GNUNET_CORE_PRIO_BEST_EFFORT;
663 high = GNUNET_CORE_PRIO_URGENT;
667 low = GNUNET_CORE_PRIO_URGENT;
668 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
671 /* Bulky payload has lower priority, control traffic has higher. */
672 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type)
680 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
683 * @param key current key code
684 * @param value value in the hash map
685 * @return #GNUNET_YES if we should continue to iterate,
689 shutdown_tunnel (void *cls,
690 const struct GNUNET_PeerIdentity *key,
693 struct CadetPeer *p = value;
694 struct CadetTunnel *t = p->tunnel;
704 * Check if peer is searching for a path (either active or delayed search).
706 * @param peer Peer to check
708 * @return GNUNET_YES if there is a search active.
709 * GNUNET_NO otherwise.
712 is_searching (const struct CadetPeer *peer)
714 return (NULL == peer->search_h && NULL == peer->search_delayed) ?
715 GNUNET_NO : GNUNET_YES;
720 * @brief Start a search for a peer.
722 * @param cls Closure (Peer to search for).
723 * @param tc Task context.
726 delayed_search (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
728 struct CadetPeer *peer = cls;
730 peer->search_delayed = NULL;
732 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
735 GCP_start_search (peer);
740 * Destroy the peer_info and free any allocated resources linked to it
742 * @param peer The peer_info to destroy.
744 * @return GNUNET_OK on success
747 peer_destroy (struct CadetPeer *peer)
749 struct GNUNET_PeerIdentity id;
750 struct CadetPeerPath *p;
751 struct CadetPeerPath *nextp;
753 GNUNET_PEER_resolve (peer->id, &id);
754 GNUNET_PEER_change_rc (peer->id, -1);
756 LOG (GNUNET_ERROR_TYPE_WARNING, "destroying peer %s\n", GNUNET_i2s (&id));
759 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
762 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
764 GCP_stop_search (peer);
769 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
773 GCT_destroy_empty (peer->tunnel);
780 * Returns if peer is used (has a tunnel or is neighbor).
782 * @param peer Peer to check.
784 * @return #GNUNET_YES if peer is in use.
787 peer_is_used (struct CadetPeer *peer)
789 struct CadetPeerPath *p;
791 if (NULL != peer->tunnel)
794 for (p = peer->path_head; NULL != p; p = p->next)
804 * Iterator over all the peers to get the oldest timestamp.
806 * @param cls Closure (unsued).
807 * @param key ID of the peer.
808 * @param value Peer_Info of the peer.
811 peer_get_oldest (void *cls,
812 const struct GNUNET_PeerIdentity *key,
815 struct CadetPeer *p = value;
816 struct GNUNET_TIME_Absolute *abs = cls;
818 /* Don't count active peers */
819 if (GNUNET_YES == peer_is_used (p))
822 if (abs->abs_value_us < p->last_contact.abs_value_us)
823 abs->abs_value_us = p->last_contact.abs_value_us;
830 * Iterator over all the peers to remove the oldest entry.
832 * @param cls Closure (unsued).
833 * @param key ID of the peer.
834 * @param value Peer_Info of the peer.
837 peer_timeout (void *cls,
838 const struct GNUNET_PeerIdentity *key,
841 struct CadetPeer *p = value;
842 struct GNUNET_TIME_Absolute *abs = cls;
844 LOG (GNUNET_ERROR_TYPE_WARNING,
845 "peer %s timeout\n", GNUNET_i2s (key));
847 if (p->last_contact.abs_value_us == abs->abs_value_us &&
848 GNUNET_NO == peer_is_used (p))
858 * Delete oldest unused peer.
861 peer_delete_oldest (void)
863 struct GNUNET_TIME_Absolute abs;
865 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
867 GNUNET_CONTAINER_multipeermap_iterate (peers,
870 GNUNET_CONTAINER_multipeermap_iterate (peers,
877 * Choose the best (yet unused) path towards a peer,
878 * considering the tunnel properties.
880 * @param peer The destination peer.
882 * @return Best current known path towards the peer, if any.
884 static struct CadetPeerPath *
885 peer_get_best_path (const struct CadetPeer *peer)
887 struct CadetPeerPath *best_p;
888 struct CadetPeerPath *p;
889 unsigned int best_cost;
892 best_cost = UINT_MAX;
894 for (p = peer->path_head; NULL != p; p = p->next)
896 if (GNUNET_NO == path_is_valid (p))
897 continue; /* Don't use invalid paths. */
898 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
899 continue; /* If path is already in use, skip it. */
901 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
912 * Is this queue element sendable?
914 * - All management traffic is always sendable.
915 * - For payload traffic, check the connection flow control.
917 * @param q Queue element to inspect.
919 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
922 queue_is_sendable (struct CadetPeerQueue *q)
924 /* Is PID-independent? */
927 case GNUNET_MESSAGE_TYPE_CADET_ACK:
928 case GNUNET_MESSAGE_TYPE_CADET_POLL:
929 case GNUNET_MESSAGE_TYPE_CADET_KX:
930 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
931 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
932 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
933 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
936 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
943 return GCC_is_sendable (q->c, q->fwd);
948 * Get first sendable message.
950 * @param peer The destination peer.
952 * @return First transmittable message, if any. Otherwise, NULL.
954 static struct CadetPeerQueue *
955 peer_get_first_message (const struct CadetPeer *peer)
957 struct CadetPeerQueue *q;
959 for (q = peer->queue_head; NULL != q; q = q->next)
961 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
962 if (queue_is_sendable (q))
971 * Function to process paths received for a new peer addition. The recorded
972 * paths form the initial tunnel, which can be optimized later.
973 * Called on each result obtained for the DHT search.
979 search_handler (void *cls, const struct CadetPeerPath *path)
981 struct CadetPeer *peer = cls;
982 unsigned int connection_count;
984 GCP_add_path_to_all (path, GNUNET_NO);
986 /* Count connections */
987 connection_count = GCT_count_connections (peer->tunnel);
989 /* If we already have our minimum (or more) connections, it's enough */
990 if (CONNECTIONS_PER_TUNNEL <= connection_count)
993 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
995 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1003 * Adjust core requested size to accomodate an ACK.
1005 * @param message_size Requested size.
1007 * @return Size enough to fit @c message_size and an ACK.
1010 get_core_size (size_t message_size)
1012 return message_size + sizeof (struct GNUNET_CADET_ACK);
1017 * Fill a core buffer with the appropriate data for the queued message.
1019 * @param queue Queue element for the message.
1020 * @param buf Core buffer to fill.
1021 * @param size Size remaining in @c buf.
1022 * @param[out] pid In case its an encrypted payload, set payload.
1024 * @return Bytes written to @c buf.
1027 fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
1029 struct CadetConnection *c = queue->c;
1032 switch (queue->type)
1034 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1035 *pid = GCC_get_pid (queue->c, queue->fwd);
1036 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload ID %u\n", *pid);
1037 msg_size = send_core_data_raw (queue->cls, size, buf);
1038 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
1040 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1041 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1042 case GNUNET_MESSAGE_TYPE_CADET_KX:
1043 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1044 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1045 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1046 msg_size = send_core_data_raw (queue->cls, size, buf);
1048 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1049 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1050 if (GCC_is_origin (c, GNUNET_YES))
1051 msg_size = send_core_connection_create (c, size, buf);
1053 msg_size = send_core_data_raw (queue->cls, size, buf);
1055 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1056 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1057 if (GCC_is_origin (c, GNUNET_NO) ||
1058 GCC_is_origin (c, GNUNET_YES))
1060 msg_size = send_core_connection_ack (c, size, buf);
1064 msg_size = send_core_data_raw (queue->cls, size, buf);
1067 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1068 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1069 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1070 /* This should be encapsulted */
1076 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1080 GNUNET_assert (size >= msg_size);
1087 * Core callback to write a queued packet to core buffer
1089 * @param cls Closure (peer info).
1090 * @param size Number of bytes available in buf.
1091 * @param buf Where the to write the message.
1093 * @return number of bytes written to buf
1096 queue_send (void *cls, size_t size, void *buf)
1098 struct CadetPeer *peer = cls;
1099 struct CadetConnection *c;
1100 struct CadetPeerQueue *queue;
1101 struct GNUNET_TIME_Relative core_wait_time;
1102 const struct GNUNET_PeerIdentity *dst_id;
1109 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1110 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1111 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
1112 GCP_2s (peer), size);
1114 /* Sanity checking */
1115 if (NULL == buf || 0 == size)
1117 LOG (GNUNET_ERROR_TYPE_DEBUG, "Buffer size 0.\n");
1118 peer->tmt_time.abs_value_us = 0;
1119 peer->core_transmit = NULL;
1128 peer->core_transmit = NULL;
1129 queue = peer_get_first_message (peer);
1132 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
1133 peer->tmt_time.abs_value_us = 0;
1136 core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1137 LOG (GNUNET_ERROR_TYPE_DEBUG, " core wait time %s\n",
1138 GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_NO));
1139 peer->tmt_time.abs_value_us = 0;
1141 /* Copy all possible messages to the core buffer */
1142 while (NULL != queue && rest >= queue->size)
1146 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s %s\n",
1147 GCC_2s (c), GC_f2s(queue->fwd));
1148 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok (%u/%u)\n",
1149 queue->size, total_size, size);
1151 msg_size = fill_buf (queue, (void *) dst, size, &pid);
1153 if (0 < drop_percent &&
1154 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1156 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on connection %s %s\n",
1157 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1158 queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
1163 LOG (GNUNET_ERROR_TYPE_INFO,
1164 "snd %s (%s %9u) on connection %s (%p) %s (size %u)\n",
1165 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1166 queue->payload_id, GCC_2s (c), c, GC_f2s (queue->fwd), msg_size);
1168 total_size += msg_size;
1170 dst = &dst[msg_size];
1173 /* Free queue, but cls was freed by send_core_* in fill_buf. */
1174 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1177 queue = peer_get_first_message (peer);
1180 /* If more data in queue, send next */
1183 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data! (%u)\n", queue->size);
1184 if (NULL == peer->core_transmit)
1186 dst_id = GNUNET_PEER_resolve2 (peer->id);
1187 peer->core_transmit =
1188 GNUNET_CORE_notify_transmit_ready (core_handle,
1189 GNUNET_NO, get_priority (queue),
1190 GNUNET_TIME_UNIT_FOREVER_REL,
1192 get_core_size (queue->size),
1195 peer->tmt_time = GNUNET_TIME_absolute_get ();
1196 queue->start_waiting = GNUNET_TIME_absolute_get ();
1200 LOG (GNUNET_ERROR_TYPE_DEBUG, "* tmt rdy called somewhere else\n");
1202 // GCC_start_poll (); FIXME needed?
1206 // GCC_stop_poll(); FIXME needed?
1209 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", total_size);
1210 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1216 /******************************************************************************/
1217 /******************************** API ***********************************/
1218 /******************************************************************************/
1222 * Free a transmission that was already queued with all resources
1223 * associated to the request.
1225 * If connection was marked to be destroyed, and this was the last queued
1226 * message on it, the connection will be free'd as a result.
1228 * @param queue Queue handler to cancel.
1229 * @param clear_cls Is it necessary to free associated cls?
1230 * @param sent Was it really sent? (Could have been canceled)
1231 * @param pid PID, if relevant (was sent and was a payload message).
1233 * @return #GNUNET_YES if connection was destroyed as a result,
1234 * #GNUNET_NO otherwise.
1237 GCP_queue_destroy (struct CadetPeerQueue *queue, int clear_cls,
1238 int sent, uint32_t pid)
1240 struct CadetPeer *peer;
1241 int connection_destroyed;
1244 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1245 if (GNUNET_YES == clear_cls)
1247 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1248 switch (queue->type)
1250 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1251 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1253 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1254 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1255 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1256 case GNUNET_MESSAGE_TYPE_CADET_KX:
1257 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1258 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1259 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1260 GNUNET_free_non_null (queue->cls);
1265 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1266 GC_m2s (queue->type));
1269 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1271 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1272 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1277 if (NULL != queue->callback)
1279 struct GNUNET_TIME_Relative wait_time;
1281 wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1282 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1283 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1284 connection_destroyed = queue->callback (queue->callback_cls,
1285 queue->c, sent, queue->type, pid,
1286 queue->fwd, queue->size,
1291 connection_destroyed = GNUNET_NO;
1294 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1296 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1297 peer->core_transmit = NULL;
1298 peer->tmt_time.abs_value_us = 0;
1301 GNUNET_free (queue);
1302 return connection_destroyed;
1307 * @brief Queue and pass message to core when possible.
1309 * @param peer Peer towards which to queue the message.
1310 * @param cls Closure (@c type dependant). It will be used by queue_send to
1311 * build the message to be sent if not already prebuilt.
1312 * @param type Type of the message, 0 for a raw message.
1313 * @param size Size of the message.
1314 * @param c Connection this message belongs to (can be NULL).
1315 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1316 * @param cont Continuation to be called once CORE has taken the message.
1317 * @param cont_cls Closure for @c cont.
1319 * @return Handle to cancel the message before it is sent. Once cont is called
1320 * message has been sent and therefore the handle is no longer valid.
1322 struct CadetPeerQueue *
1323 GCP_queue_add (struct CadetPeer *peer, void *cls, uint16_t type,
1324 uint16_t payload_type, uint32_t payload_id, size_t size,
1325 struct CadetConnection *c, int fwd,
1326 GCP_sent cont, void *cont_cls)
1328 struct CadetPeerQueue *q;
1333 if (NULL == c && GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN != type)
1334 error_level = GNUNET_ERROR_TYPE_ERROR;
1336 error_level = GNUNET_ERROR_TYPE_INFO;
1338 "que %s (%s %9u) on connection %s (%p) %s towards %s (size %u)\n",
1339 GC_m2s (type), GC_m2s (payload_type), payload_id,
1340 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1342 if (error_level == GNUNET_ERROR_TYPE_ERROR)
1344 if (NULL == peer->connections)
1346 /* We are not connected to this peer, ignore request. */
1347 LOG (GNUNET_ERROR_TYPE_WARNING, "%s not a neighbor\n", GCP_2s (peer));
1348 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1355 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1356 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1361 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1363 call_core = (NULL == c || type == GNUNET_MESSAGE_TYPE_CADET_KX) ?
1364 GNUNET_YES : GCC_is_sendable (c, fwd);
1365 q = GNUNET_new (struct CadetPeerQueue);
1368 q->payload_type = payload_type;
1369 q->payload_id = payload_id;
1375 q->callback_cls = cont_cls;
1378 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1383 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1384 call_core = GNUNET_YES;
1387 q->start_waiting = GNUNET_TIME_absolute_get ();
1388 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1390 LOG (GNUNET_ERROR_TYPE_DEBUG,
1391 "calling core tmt rdy towards %s for %u bytes\n",
1392 GCP_2s (peer), size);
1393 peer->core_transmit =
1394 GNUNET_CORE_notify_transmit_ready (core_handle,
1395 GNUNET_NO, get_priority (q),
1396 GNUNET_TIME_UNIT_FOREVER_REL,
1397 GNUNET_PEER_resolve2 (peer->id),
1398 get_core_size (size),
1400 peer->tmt_time = GNUNET_TIME_absolute_get ();
1402 else if (GNUNET_NO == call_core)
1404 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1410 struct GNUNET_TIME_Relative elapsed;
1411 elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1412 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
1414 GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
1417 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1423 * Cancel all queued messages to a peer that belong to a certain connection.
1425 * @param peer Peer towards whom to cancel.
1426 * @param c Connection whose queued messages to cancel. Might be destroyed by
1427 * the sent continuation call.
1430 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c)
1432 struct CadetPeerQueue *q;
1433 struct CadetPeerQueue *next;
1434 struct CadetPeerQueue *prev;
1435 int connection_destroyed;
1437 connection_destroyed = GNUNET_NO;
1438 for (q = peer->queue_head; NULL != q; q = next)
1443 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP queue cancel %s\n", GC_m2s (q->type));
1444 GNUNET_break (GNUNET_NO == connection_destroyed);
1445 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1451 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1454 /* Get next from prev, q->next might be already freed:
1455 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1458 next = peer->queue_head;
1468 if (NULL == peer->queue_head && NULL != peer->core_transmit)
1470 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1471 peer->core_transmit = NULL;
1472 peer->tmt_time.abs_value_us = 0;
1478 * Get the first transmittable message for a connection.
1480 * @param peer Neighboring peer.
1481 * @param c Connection.
1483 * @return First transmittable message.
1485 static struct CadetPeerQueue *
1486 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1488 struct CadetPeerQueue *q;
1490 for (q = peer->queue_head; NULL != q; q = q->next)
1494 if (queue_is_sendable (q))
1496 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1499 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1507 * Get the first message for a connection and unqueue it.
1509 * Only tunnel (or higher) level messages are unqueued. Connection specific
1510 * messages are silently destroyed upon encounter.
1512 * @param peer Neighboring peer.
1513 * @param c Connection.
1514 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1517 * @return First message for this connection.
1519 struct GNUNET_MessageHeader *
1520 GCP_connection_pop (struct CadetPeer *peer,
1521 struct CadetConnection *c,
1524 struct CadetPeerQueue *q;
1525 struct CadetPeerQueue *next;
1526 struct GNUNET_MessageHeader *msg;
1529 GNUNET_assert (NULL != destroyed);
1530 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on connection %p\n", c);
1531 for (q = peer->queue_head; NULL != q; q = next)
1536 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), callback: %p\n",
1537 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1541 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1542 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1543 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1544 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1545 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1546 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1547 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1548 if (GNUNET_YES == dest)
1550 GNUNET_break (GNUNET_NO == *destroyed);
1551 *destroyed = GNUNET_YES;
1555 case GNUNET_MESSAGE_TYPE_CADET_KX:
1556 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1557 msg = (struct GNUNET_MessageHeader *) q->cls;
1558 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1559 if (GNUNET_YES == dest)
1561 GNUNET_break (GNUNET_NO == *destroyed);
1562 *destroyed = GNUNET_YES;
1568 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1576 * Unlock a possibly locked queue for a connection.
1578 * If there is a message that can be sent on this connection, call core for it.
1579 * Otherwise (if core transmit is already called or there is no sendable
1580 * message) do nothing.
1582 * @param peer Peer who keeps the queue.
1583 * @param c Connection whose messages to unlock.
1586 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1588 struct CadetPeerQueue *q;
1591 if (NULL != peer->core_transmit)
1593 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1594 return; /* Already unlocked */
1597 q = connection_get_first_message (peer, c);
1600 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1601 return; /* Nothing to transmit */
1605 peer->core_transmit =
1606 GNUNET_CORE_notify_transmit_ready (core_handle,
1607 GNUNET_NO, get_priority (q),
1608 GNUNET_TIME_UNIT_FOREVER_REL,
1609 GNUNET_PEER_resolve2 (peer->id),
1610 get_core_size (size),
1613 peer->tmt_time = GNUNET_TIME_absolute_get ();
1618 * Initialize the peer subsystem.
1620 * @param c Configuration.
1623 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1625 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1626 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1628 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1631 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1632 "CADET", "MAX_PEERS", "USING DEFAULT");
1637 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1644 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1645 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1646 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1647 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1648 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1651 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1652 NULL, /* Closure passed to CADET functions */
1653 &core_init, /* Call core_init once connected */
1654 &core_connect, /* Handle connects */
1655 &core_disconnect, /* remove peers on disconnects */
1656 NULL, /* Don't notify about all incoming messages */
1657 GNUNET_NO, /* For header only in notification */
1658 NULL, /* Don't notify about all outbound messages */
1659 GNUNET_NO, /* For header-only out notification */
1660 core_handlers); /* Register these handlers */
1662 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1664 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1665 /* Notify callbacks */
1670 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1671 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1672 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1673 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1674 transport_handle = NULL;
1679 if (NULL == core_handle)
1682 GNUNET_SCHEDULER_shutdown ();
1690 * Shut down the peer subsystem.
1695 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1697 if (core_handle != NULL)
1699 GNUNET_CORE_disconnect (core_handle);
1702 if (transport_handle != NULL)
1704 GNUNET_TRANSPORT_disconnect (transport_handle);
1705 transport_handle = NULL;
1707 GNUNET_PEER_change_rc (myid, -1);
1712 * Retrieve the CadetPeer stucture associated with the peer, create one
1713 * and insert it in the appropriate structures if the peer is not known yet.
1715 * @param peer_id Full identity of the peer.
1717 * @return Existing or newly created peer structure.
1720 GCP_get (const struct GNUNET_PeerIdentity *peer_id)
1722 struct CadetPeer *peer;
1724 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1727 peer = GNUNET_new (struct CadetPeer);
1728 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1730 peer_delete_oldest ();
1732 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1733 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1734 peer->id = GNUNET_PEER_intern (peer_id);
1736 peer->last_contact = GNUNET_TIME_absolute_get ();
1743 * Retrieve the CadetPeer stucture associated with the peer, create one
1744 * and insert it in the appropriate structures if the peer is not known yet.
1746 * @param peer Short identity of the peer.
1748 * @return Existing or newly created peer structure.
1751 GCP_get_short (const GNUNET_PEER_Id peer)
1753 return GCP_get (GNUNET_PEER_resolve2 (peer));
1758 * Try to connect to a peer on transport level.
1760 * @param cls Closure (peer).
1761 * @param tc TaskContext.
1764 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1766 struct CadetPeer *peer = cls;
1768 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1771 GNUNET_TRANSPORT_try_connect (transport_handle,
1772 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1777 * Try to establish a new connection to this peer (in its tunnel).
1778 * If the peer doesn't have any path to it yet, try to get one.
1779 * If the peer already has some path, send a CREATE CONNECTION towards it.
1781 * @param peer Peer to connect to.
1784 GCP_connect (struct CadetPeer *peer)
1786 struct CadetTunnel *t;
1787 struct CadetPeerPath *p;
1788 struct CadetConnection *c;
1791 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1793 /* If we have a current hello, try to connect using it. */
1794 GCP_try_connect (peer);
1798 rerun_search = GNUNET_NO;
1800 if (NULL != peer->path_head)
1802 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1803 p = peer_get_best_path (peer);
1809 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1812 c = GCT_use_path (t, p);
1815 /* This case can happen when the path includes a first hop that is
1816 * not yet known to be connected.
1818 * This happens quite often during testing when running cadet
1819 * under valgrind: core connect notifications come very late and the
1820 * DHT result has already come and created a valid path.
1821 * In this case, the peer->connections hashmap will be NULL and
1822 * tunnel_use_path will not be able to create a connection from that
1825 * Re-running the DHT GET should give core time to callback.
1827 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1828 * updating statistics about this issue.
1830 rerun_search = GNUNET_YES;
1834 GCC_send_create (c);
1840 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1844 if (GNUNET_YES == rerun_search)
1846 struct GNUNET_TIME_Relative delay;
1848 GCP_stop_search (peer);
1849 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1850 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay, &delayed_search,
1855 if (GNUNET_NO == is_searching (peer))
1856 GCP_start_search (peer);
1861 * Chech whether there is a direct (core level) connection to peer.
1863 * @param peer Peer to check.
1865 * @return #GNUNET_YES if there is a direct connection.
1868 GCP_is_neighbor (const struct CadetPeer *peer)
1870 struct CadetPeerPath *path;
1872 if (NULL == peer->connections)
1875 for (path = peer->path_head; NULL != path; path = path->next)
1877 if (3 > path->length)
1881 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1887 * Create and initialize a new tunnel towards a peer, in case it has none.
1888 * In case the peer already has a tunnel, nothing is done.
1890 * Does not generate any traffic, just creates the local data structures.
1892 * @param peer Peer towards which to create the tunnel.
1895 GCP_add_tunnel (struct CadetPeer *peer)
1897 if (NULL != peer->tunnel)
1899 peer->tunnel = GCT_new (peer);
1904 * Add a connection to a neighboring peer.
1906 * Store that the peer is the first hop of the connection in one
1907 * direction and that on peer disconnect the connection must be
1908 * notified and destroyed, for it will no longer be valid.
1910 * @param peer Peer to add connection to.
1911 * @param c Connection to add.
1913 * @return GNUNET_OK on success.
1916 GCP_add_connection (struct CadetPeer *peer,
1917 struct CadetConnection *c)
1920 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding connection %s\n", GCC_2s (c));
1921 LOG (GNUNET_ERROR_TYPE_DEBUG, "to peer %s\n", GCP_2s (peer));
1923 if (NULL == peer->connections)
1926 LOG (GNUNET_ERROR_TYPE_DEBUG,
1927 "Peer %s is not a neighbor!\n",
1929 return GNUNET_SYSERR;
1931 LOG (GNUNET_ERROR_TYPE_DEBUG,
1932 "peer %s ok, has %u connections.\n",
1933 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1934 result = GNUNET_CONTAINER_multihashmap_put (peer->connections,
1937 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1938 LOG (GNUNET_ERROR_TYPE_DEBUG,
1939 " now has %u connections.\n",
1940 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1941 LOG (GNUNET_ERROR_TYPE_DEBUG, "result %u\n", result);
1948 * Add the path to the peer and update the path used to reach it in case this
1951 * @param peer Destination peer to add the path to.
1952 * @param path New path to add. Last peer must be the peer in arg 1.
1953 * Path will be either used of freed if already known.
1954 * @param trusted Do we trust that this path is real?
1956 * @return path if path was taken, pointer to existing duplicate if exists
1959 struct CadetPeerPath *
1960 GCP_add_path (struct CadetPeer *peer, struct CadetPeerPath *path,
1963 struct CadetPeerPath *aux;
1967 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1968 path->length, GCP_2s (peer));
1970 if (NULL == peer || NULL == path
1971 || path->peers[path->length - 1] != peer->id)
1974 path_destroy (path);
1978 for (l = 1; l < path->length; l++)
1980 if (path->peers[l] == myid)
1982 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1983 for (l2 = 0; l2 < path->length - l; l2++)
1985 path->peers[l2] = path->peers[l + l2];
1989 path->peers = GNUNET_realloc (path->peers,
1990 path->length * sizeof (GNUNET_PEER_Id));
1994 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1996 if (2 >= path->length && GNUNET_NO == trusted)
1998 /* Only allow CORE to tell us about direct paths */
1999 path_destroy (path);
2003 l = path_get_length (path);
2006 path_destroy (path);
2010 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2011 for (aux = peer->path_head; aux != NULL; aux = aux->next)
2013 l2 = path_get_length (aux);
2016 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
2017 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
2018 peer->path_tail, aux, path);
2023 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2025 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
2026 path_destroy (path);
2031 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
2033 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
2036 if (NULL != peer->tunnel
2037 && CONNECTIONS_PER_TUNNEL < GCT_count_connections (peer->tunnel))
2046 * Add the path to the origin peer and update the path used to reach it in case
2047 * this is the shortest.
2048 * The path is given in peer_info -> destination, therefore we turn the path
2049 * upside down first.
2051 * @param peer Peer to add the path to, being the origin of the path.
2052 * @param path New path to add after being inversed.
2053 * Path will be either used or freed.
2054 * @param trusted Do we trust that this path is real?
2056 * @return path if path was taken, pointer to existing duplicate if exists
2059 struct CadetPeerPath *
2060 GCP_add_path_to_origin (struct CadetPeer *peer,
2061 struct CadetPeerPath *path,
2067 return GCP_add_path (peer, path, trusted);
2072 * Adds a path to the info of all the peers in the path
2074 * @param p Path to process.
2075 * @param confirmed Whether we know if the path works or not.
2078 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
2082 /* TODO: invert and add */
2083 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2084 for (i++; i < p->length; i++)
2086 struct CadetPeer *aux;
2087 struct CadetPeerPath *copy;
2089 aux = GCP_get_short (p->peers[i]);
2090 copy = path_duplicate (p);
2091 copy->length = i + 1;
2092 GCP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2098 * Remove any path to the peer that has the extact same peers as the one given.
2100 * @param peer Peer to remove the path from.
2101 * @param path Path to remove. Is always destroyed .
2104 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
2106 struct CadetPeerPath *iter;
2107 struct CadetPeerPath *next;
2109 GNUNET_assert (myid == path->peers[0]);
2110 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2112 LOG (GNUNET_ERROR_TYPE_INFO, "Removing path %p (%u) from %s\n",
2113 path, path->length, GCP_2s (peer));
2115 for (iter = peer->path_head; NULL != iter; iter = next)
2118 if (0 == path_cmp (path, iter))
2120 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
2122 path_destroy (iter);
2125 path_destroy (path);
2130 * Remove a connection from a neighboring peer.
2132 * @param peer Peer to remove connection from.
2133 * @param c Connection to remove.
2135 * @return GNUNET_OK on success.
2138 GCP_remove_connection (struct CadetPeer *peer,
2139 const struct CadetConnection *c)
2141 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing connection %s\n", GCC_2s (c));
2142 LOG (GNUNET_ERROR_TYPE_DEBUG, "from peer %s\n", GCP_2s (peer));
2144 if (NULL == peer || NULL == peer->connections)
2146 LOG (GNUNET_ERROR_TYPE_DEBUG,
2147 "Peer %s is not a neighbor!\n",
2149 return GNUNET_SYSERR;
2151 LOG (GNUNET_ERROR_TYPE_DEBUG,
2152 "peer %s ok, has %u connections.\n",
2153 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
2155 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2161 * Start the DHT search for new paths towards the peer: we don't have
2162 * enough good connections.
2164 * @param peer Destination peer.
2167 GCP_start_search (struct CadetPeer *peer)
2169 const struct GNUNET_PeerIdentity *id;
2170 struct CadetTunnel *t = peer->tunnel;
2172 if (NULL != peer->search_h)
2178 if (NULL != peer->search_delayed)
2179 GCP_stop_search (peer);
2181 id = GNUNET_PEER_resolve2 (peer->id);
2182 peer->search_h = GCD_search (id, &search_handler, peer);
2186 /* Why would we search for a peer with no tunnel towards it? */
2191 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
2192 || 0 == GCT_count_any_connections (t))
2194 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2200 * Stop the DHT search for new paths towards the peer: we already have
2201 * enough good connections.
2203 * @param peer Destination peer.
2206 GCP_stop_search (struct CadetPeer *peer)
2208 if (NULL != peer->search_h)
2210 GCD_search_stop (peer->search_h);
2211 peer->search_h = NULL;
2213 if (NULL != peer->search_delayed)
2215 GNUNET_SCHEDULER_cancel (peer->search_delayed);
2216 peer->search_delayed = NULL;
2222 * Get the Full ID of a peer.
2224 * @param peer Peer to get from.
2226 * @return Full ID of peer.
2228 const struct GNUNET_PeerIdentity *
2229 GCP_get_id (const struct CadetPeer *peer)
2231 return GNUNET_PEER_resolve2 (peer->id);
2236 * Get the Short ID of a peer.
2238 * @param peer Peer to get from.
2240 * @return Short ID of peer.
2243 GCP_get_short_id (const struct CadetPeer *peer)
2252 * If tunnel is NULL and there was a search active, stop it, as it's useless.
2258 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2261 if (NULL == t && GNUNET_YES == is_searching (peer))
2263 GCP_stop_search (peer);
2269 * Get the tunnel towards a peer.
2271 * @param peer Peer to get from.
2273 * @return Tunnel towards peer.
2275 struct CadetTunnel *
2276 GCP_get_tunnel (const struct CadetPeer *peer)
2278 return peer->tunnel;
2283 * Set the hello message.
2285 * @param peer Peer whose message to set.
2286 * @param hello Hello message.
2289 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2291 struct GNUNET_HELLO_Message *old;
2294 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2298 old = GCP_get_hello (peer);
2301 size = GNUNET_HELLO_size (hello);
2302 LOG (GNUNET_ERROR_TYPE_DEBUG, " new (%u bytes)\n", size);
2303 peer->hello = GNUNET_malloc (size);
2304 memcpy (peer->hello, hello, size);
2308 peer->hello = GNUNET_HELLO_merge (old, hello);
2309 LOG (GNUNET_ERROR_TYPE_DEBUG, " merge into %p (%u bytes)\n",
2310 peer->hello, GNUNET_HELLO_size (hello));
2317 * Get the hello message.
2319 * @param peer Peer whose message to get.
2321 * @return Hello message.
2323 struct GNUNET_HELLO_Message *
2324 GCP_get_hello (struct CadetPeer *peer)
2326 struct GNUNET_TIME_Absolute expiration;
2327 struct GNUNET_TIME_Relative remaining;
2329 if (NULL == peer->hello)
2332 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2333 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2334 if (0 == remaining.rel_value_us)
2336 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2337 GNUNET_STRINGS_absolute_time_to_string (expiration));
2338 GNUNET_free (peer->hello);
2346 * Try to connect to a peer on TRANSPORT level.
2348 * @param peer Peer to whom to connect.
2351 GCP_try_connect (struct CadetPeer *peer)
2353 struct GNUNET_HELLO_Message *hello;
2354 struct GNUNET_MessageHeader *mh;
2356 if (NULL == transport_handle)
2359 hello = GCP_get_hello (peer);
2363 mh = GNUNET_HELLO_get_header (hello);
2364 GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
2369 * Notify a peer that a link between two other peers is broken. If any path
2370 * used that link, eliminate it.
2372 * @param peer Peer affected by the change.
2373 * @param peer1 Peer whose link is broken.
2374 * @param peer2 Peer whose link is broken.
2377 GCP_notify_broken_link (struct CadetPeer *peer,
2378 struct GNUNET_PeerIdentity *peer1,
2379 struct GNUNET_PeerIdentity *peer2)
2381 struct CadetPeerPath *iter;
2382 struct CadetPeerPath *next;
2387 p1 = GNUNET_PEER_search (peer1);
2388 p2 = GNUNET_PEER_search (peer2);
2390 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2391 if (0 == p1 || 0 == p2)
2393 /* We don't even know them */
2397 for (iter = peer->path_head; NULL != iter; iter = next)
2400 for (i = 0; i < iter->length - 1; i++)
2402 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2403 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2408 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2411 path_invalidate (iter);
2419 * Count the number of known paths toward the peer.
2421 * @param peer Peer to get path info.
2423 * @return Number of known paths.
2426 GCP_count_paths (const struct CadetPeer *peer)
2428 struct CadetPeerPath *iter;
2431 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2439 * Iterate all known peers.
2441 * @param iter Iterator.
2442 * @param cls Closure for @c iter.
2445 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2447 GNUNET_CONTAINER_multipeermap_iterate (peers, iter, cls);
2452 * Get the static string for a peer ID.
2456 * @return Static string for it's ID.
2459 GCP_2s (const struct CadetPeer *peer)
2463 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));