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_signatures.h"
27 #include "gnunet_transport_service.h"
28 #include "gnunet_core_service.h"
29 #include "gnunet_statistics_service.h"
31 #include "cadet_protocol.h"
33 #include "gnunet-service-cadet_peer.h"
34 #include "gnunet-service-cadet_dht.h"
35 #include "gnunet-service-cadet_connection.h"
36 #include "gnunet-service-cadet_tunnel.h"
37 #include "cadet_path.h"
39 #define LOG(level, ...) GNUNET_log_from (level,"cadet-p2p",__VA_ARGS__)
40 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-p2p",__VA_ARGS__)
43 /******************************************************************************/
44 /******************************** STRUCTS **********************************/
45 /******************************************************************************/
48 * Struct containing info about a queued transmission to this peer
55 struct CadetPeerQueue *next;
60 struct CadetPeerQueue *prev;
63 * Peer this transmission is directed to.
65 struct CadetPeer *peer;
68 * Connection this message belongs to.
70 struct CadetConnection *c;
78 * Pointer to info stucture used as cls.
90 uint16_t payload_type;
103 * Set when this message starts waiting for CORE.
105 struct GNUNET_TIME_Absolute start_waiting;
108 * Function to call on sending.
113 * Closure for callback.
119 * Struct containing all information regarding a given peer
129 * Axolotl permanent public key.
131 struct GNUNET_CRYPTO_EcdhePublicKey ax_key;
134 * Last time we heard from this peer
136 struct GNUNET_TIME_Absolute last_contact;
139 * Paths to reach the peer, ordered by ascending hop count
141 struct CadetPeerPath *path_head;
144 * Paths to reach the peer, ordered by ascending hop count
146 struct CadetPeerPath *path_tail;
149 * Handle to stop the DHT search for paths to this peer
151 struct GCD_search_handle *search_h;
154 * Handle to stop the DHT search for paths to this peer
156 struct GNUNET_SCHEDULER_Task *search_delayed;
159 * Tunnel to this peer, if any.
161 struct CadetTunnel *tunnel;
164 * Connections that go through this peer, indexed by tid;
166 struct GNUNET_CONTAINER_MultiHashMap *connections;
169 * Handle for queued transmissions
171 struct GNUNET_CORE_TransmitHandle *core_transmit;
176 struct GNUNET_TIME_Absolute tmt_time;
179 * Transmission queue to core DLL head
181 struct CadetPeerQueue *queue_head;
184 * Transmission queue to core DLL tail
186 struct CadetPeerQueue *queue_tail;
189 * How many messages are in the queue to this peer.
191 unsigned int queue_n;
196 struct GNUNET_HELLO_Message* hello;
200 /******************************************************************************/
201 /******************************* GLOBALS ***********************************/
202 /******************************************************************************/
205 * Global handle to the statistics service.
207 extern struct GNUNET_STATISTICS_Handle *stats;
210 * Local peer own ID (full value).
212 extern struct GNUNET_PeerIdentity my_full_id;
215 * Local peer own ID (short)
217 extern GNUNET_PEER_Id myid;
220 * Peers known, indexed by PeerIdentity (CadetPeer).
222 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
225 * How many peers do we want to remember?
227 static unsigned long long max_peers;
230 * Percentage of messages that will be dropped (for test purposes only).
232 static unsigned long long drop_percent;
235 * Handle to communicate with core.
237 static struct GNUNET_CORE_Handle *core_handle;
240 * Handle to try to start new connections.
242 static struct GNUNET_TRANSPORT_Handle *transport_handle;
245 /******************************************************************************/
246 /***************************** DEBUG *********************************/
247 /******************************************************************************/
250 * Log all kinds of info about the queueing status of a peer.
252 * @param p Peer whose queue to show.
253 * @param level Error level to use for logging.
256 queue_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
258 struct CadetPeerQueue *q;
261 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
263 __FILE__, __FUNCTION__, __LINE__);
267 LOG2 (level, "QQQ Message queue towards %s\n", GCP_2s (p));
268 LOG2 (level, "QQQ queue length: %u\n", p->queue_n);
269 LOG2 (level, "QQQ core tmt rdy: %p\n", p->core_transmit);
271 for (q = p->queue_head; NULL != q; q = q->next)
273 LOG2 (level, "QQQ - %s %s on %s\n",
274 GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
275 LOG2 (level, "QQQ payload %s, %u\n",
276 GC_m2s (q->payload_type), q->payload_id);
277 LOG2 (level, "QQQ size: %u bytes\n", q->size);
280 LOG2 (level, "QQQ End queue towards %s\n", GCP_2s (p));
285 * Log all kinds of info about a peer.
290 GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
292 struct CadetPeerPath *path;
296 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
298 __FILE__, __FUNCTION__, __LINE__);
304 LOG2 (level, "PPP DEBUG PEER NULL\n");
308 LOG2 (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
309 LOG2 (level, "PPP last contact %s\n",
310 GNUNET_STRINGS_absolute_time_to_string (p->last_contact));
311 for (path = p->path_head; NULL != path; path = path->next)
316 LOG2 (level, "PPP path: %s\n", s);
320 LOG2 (level, "PPP core transmit handle %p\n", p->core_transmit);
321 LOG2 (level, "PPP DHT GET handle %p\n", p->search_h);
322 if (NULL != p->connections)
323 conns = GNUNET_CONTAINER_multihashmap_size (p->connections);
326 LOG2 (level, "PPP # connections over link to peer: %u\n", conns);
327 queue_debug (p, level);
328 LOG2 (level, "PPP DEBUG END\n");
332 /******************************************************************************/
333 /***************************** CORE HELPERS *********************************/
334 /******************************************************************************/
338 * Iterator to notify all connections of a broken link. Mark connections
339 * to destroy after all traffic has been sent.
341 * @param cls Closure (peer disconnected).
342 * @param key Current key code (peer id).
343 * @param value Value in the hash map (connection).
345 * @return #GNUNET_YES to continue to iterate.
348 notify_broken (void *cls,
349 const struct GNUNET_HashCode *key,
352 struct CadetPeer *peer = cls;
353 struct CadetConnection *c = value;
355 LOG (GNUNET_ERROR_TYPE_DEBUG, " notifying %s due to %s\n",
356 GCC_2s (c), GCP_2s (peer));
357 GCC_notify_broken (c, peer);
364 * Remove the direct path to the peer.
366 * @param peer Peer to remove the direct path from.
369 static struct CadetPeerPath *
370 pop_direct_path (struct CadetPeer *peer)
372 struct CadetPeerPath *iter;
374 for (iter = peer->path_head; NULL != iter; iter = iter->next)
376 if (2 >= iter->length)
378 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
386 /******************************************************************************/
387 /***************************** CORE CALLBACKS *********************************/
388 /******************************************************************************/
391 * Method called whenever a given peer connects.
394 * @param peer peer identity this notification is about
397 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
399 struct CadetPeer *mp;
400 struct CadetPeerPath *path;
403 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
407 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s (self)\n", own_id);
412 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s <= %s\n",
413 own_id, GNUNET_i2s (peer));
415 path->peers[1] = mp->id;
416 GNUNET_PEER_change_rc (mp->id, 1);
417 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
419 path->peers[0] = myid;
420 GNUNET_PEER_change_rc (myid, 1);
421 GCP_add_path (mp, path, GNUNET_YES);
423 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
425 if (NULL != GCP_get_tunnel (mp) &&
426 0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer))
436 * Method called whenever a peer disconnects.
439 * @param peer peer identity this notification is about
442 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
445 struct CadetPeerPath *direct_path;
448 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
449 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
456 LOG (GNUNET_ERROR_TYPE_INFO, "DISCONNECTED %s (self)\n", own_id);
458 LOG (GNUNET_ERROR_TYPE_INFO, "DISCONNECTED %s <= %s\n",
459 own_id, GNUNET_i2s (peer));
460 direct_path = pop_direct_path (p);
461 GNUNET_CONTAINER_multihashmap_iterate (p->connections, ¬ify_broken, p);
462 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
463 p->connections = NULL;
464 if (NULL != p->core_transmit)
466 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
467 p->core_transmit = NULL;
468 p->tmt_time.abs_value_us = 0;
470 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
472 path_destroy (direct_path);
478 * Functions to handle messages from core
480 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
481 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
482 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
483 sizeof (struct GNUNET_CADET_ConnectionACK)},
484 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
485 sizeof (struct GNUNET_CADET_ConnectionBroken)},
486 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
487 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
488 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
489 sizeof (struct GNUNET_CADET_ACK)},
490 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
491 sizeof (struct GNUNET_CADET_Poll)},
492 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
493 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0}
498 * To be called on core init/fail.
500 * @param cls Closure (config)
501 * @param identity the public identity of this peer
504 core_init (void *cls,
505 const struct GNUNET_PeerIdentity *identity)
507 const struct GNUNET_CONFIGURATION_Handle *c = cls;
510 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
511 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
513 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
514 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
515 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
516 GNUNET_CORE_disconnect (core_handle);
517 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
518 NULL, /* Closure passed to CADET functions */
519 &core_init, /* Call core_init once connected */
520 &core_connect, /* Handle connects */
521 &core_disconnect, /* remove peers on disconnects */
522 NULL, /* Don't notify about all incoming messages */
523 GNUNET_NO, /* For header only in notification */
524 NULL, /* Don't notify about all outbound messages */
525 GNUNET_NO, /* For header-only out notification */
526 core_handlers); /* Register these handlers */
536 * Core callback to write a pre-constructed data packet to core buffer
538 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
539 * @param size Number of bytes available in buf.
540 * @param buf Where the to write the message.
542 * @return number of bytes written to buf
545 send_core_data_raw (void *cls, size_t size, void *buf)
547 struct GNUNET_MessageHeader *msg = cls;
550 GNUNET_assert (NULL != msg);
551 total_size = ntohs (msg->size);
553 if (total_size > size)
558 memcpy (buf, msg, total_size);
565 * Function to send a create connection message to a peer.
567 * @param c Connection to create.
568 * @param size number of bytes available in buf
569 * @param buf where the callee should write the message
570 * @return number of bytes written to buf
573 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
575 struct GNUNET_CADET_ConnectionCreate *msg;
576 struct GNUNET_PeerIdentity *peer_ptr;
577 const struct CadetPeerPath *p = GCC_get_path (c);
584 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
586 sizeof (struct GNUNET_CADET_ConnectionCreate) +
587 p->length * sizeof (struct GNUNET_PeerIdentity);
589 if (size < size_needed || NULL == buf)
594 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
595 msg->header.size = htons (size_needed);
596 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
597 msg->cid = *GCC_get_id (c);
599 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
600 for (i = 0; i < p->length; i++)
602 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
605 LOG (GNUNET_ERROR_TYPE_DEBUG,
606 "CONNECTION CREATE (%u bytes long) sent!\n",
613 * Creates a path ack message in buf and frees all unused resources.
615 * @param c Connection to send an ACK on.
616 * @param size number of bytes available in buf
617 * @param buf where the callee should write the message
619 * @return number of bytes written to buf
622 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
624 struct GNUNET_CADET_ConnectionACK *msg = buf;
626 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
627 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
632 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
633 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
634 msg->cid = *GCC_get_id (c);
636 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
637 return sizeof (struct GNUNET_CADET_ConnectionACK);
641 /******************************************************************************/
642 /******************************** STATIC ***********************************/
643 /******************************************************************************/
647 * Get priority for a queued message.
649 * @param q Queued message
651 * @return CORE priority to use.
653 static enum GNUNET_CORE_Priority
654 get_priority (struct CadetPeerQueue *q)
656 enum GNUNET_CORE_Priority low;
657 enum GNUNET_CORE_Priority high;
662 return GNUNET_CORE_PRIO_BACKGROUND;
665 /* Relayed traffic has lower priority, our own traffic has higher */
666 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
668 low = GNUNET_CORE_PRIO_BEST_EFFORT;
669 high = GNUNET_CORE_PRIO_URGENT;
673 low = GNUNET_CORE_PRIO_URGENT;
674 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
677 /* Bulky payload has lower priority, control traffic has higher. */
678 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type)
686 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
689 * @param key current key code
690 * @param value value in the hash map
691 * @return #GNUNET_YES if we should continue to iterate,
695 shutdown_tunnel (void *cls,
696 const struct GNUNET_PeerIdentity *key,
699 struct CadetPeer *p = value;
700 struct CadetTunnel *t = p->tunnel;
710 * Check if peer is searching for a path (either active or delayed search).
712 * @param peer Peer to check
714 * @return GNUNET_YES if there is a search active.
715 * GNUNET_NO otherwise.
718 is_searching (const struct CadetPeer *peer)
720 return (NULL == peer->search_h && NULL == peer->search_delayed) ?
721 GNUNET_NO : GNUNET_YES;
726 * @brief Start a search for a peer.
728 * @param cls Closure (Peer to search for).
729 * @param tc Task context.
732 delayed_search (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
734 struct CadetPeer *peer = cls;
736 peer->search_delayed = NULL;
738 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
741 GCP_start_search (peer);
746 * Destroy the peer_info and free any allocated resources linked to it
748 * @param peer The peer_info to destroy.
750 * @return GNUNET_OK on success
753 peer_destroy (struct CadetPeer *peer)
755 struct GNUNET_PeerIdentity id;
756 struct CadetPeerPath *p;
757 struct CadetPeerPath *nextp;
759 GNUNET_PEER_resolve (peer->id, &id);
760 GNUNET_PEER_change_rc (peer->id, -1);
762 LOG (GNUNET_ERROR_TYPE_WARNING, "destroying peer %s\n", GNUNET_i2s (&id));
765 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
768 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
770 GCP_stop_search (peer);
775 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
779 GCT_destroy_empty (peer->tunnel);
786 * Returns if peer is used (has a tunnel or is neighbor).
788 * @param peer Peer to check.
790 * @return #GNUNET_YES if peer is in use.
793 peer_is_used (struct CadetPeer *peer)
795 struct CadetPeerPath *p;
797 if (NULL != peer->tunnel)
800 for (p = peer->path_head; NULL != p; p = p->next)
810 * Iterator over all the peers to get the oldest timestamp.
812 * @param cls Closure (unsued).
813 * @param key ID of the peer.
814 * @param value Peer_Info of the peer.
817 peer_get_oldest (void *cls,
818 const struct GNUNET_PeerIdentity *key,
821 struct CadetPeer *p = value;
822 struct GNUNET_TIME_Absolute *abs = cls;
824 /* Don't count active peers */
825 if (GNUNET_YES == peer_is_used (p))
828 if (abs->abs_value_us < p->last_contact.abs_value_us)
829 abs->abs_value_us = p->last_contact.abs_value_us;
836 * Iterator over all the peers to remove the oldest entry.
838 * @param cls Closure (unsued).
839 * @param key ID of the peer.
840 * @param value Peer_Info of the peer.
843 peer_timeout (void *cls,
844 const struct GNUNET_PeerIdentity *key,
847 struct CadetPeer *p = value;
848 struct GNUNET_TIME_Absolute *abs = cls;
850 LOG (GNUNET_ERROR_TYPE_WARNING,
851 "peer %s timeout\n", GNUNET_i2s (key));
853 if (p->last_contact.abs_value_us == abs->abs_value_us &&
854 GNUNET_NO == peer_is_used (p))
864 * Delete oldest unused peer.
867 peer_delete_oldest (void)
869 struct GNUNET_TIME_Absolute abs;
871 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
873 GNUNET_CONTAINER_multipeermap_iterate (peers,
876 GNUNET_CONTAINER_multipeermap_iterate (peers,
883 * Choose the best (yet unused) path towards a peer,
884 * considering the tunnel properties.
886 * @param peer The destination peer.
888 * @return Best current known path towards the peer, if any.
890 static struct CadetPeerPath *
891 peer_get_best_path (const struct CadetPeer *peer)
893 struct CadetPeerPath *best_p;
894 struct CadetPeerPath *p;
895 unsigned int best_cost;
898 best_cost = UINT_MAX;
900 for (p = peer->path_head; NULL != p; p = p->next)
902 if (GNUNET_NO == path_is_valid (p))
903 continue; /* Don't use invalid paths. */
904 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
905 continue; /* If path is already in use, skip it. */
907 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
918 * Is this queue element sendable?
920 * - All management traffic is always sendable.
921 * - For payload traffic, check the connection flow control.
923 * @param q Queue element to inspect.
925 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
928 queue_is_sendable (struct CadetPeerQueue *q)
930 /* Is PID-independent? */
933 case GNUNET_MESSAGE_TYPE_CADET_ACK:
934 case GNUNET_MESSAGE_TYPE_CADET_POLL:
935 case GNUNET_MESSAGE_TYPE_CADET_KX:
936 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
937 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
938 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
939 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
942 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
943 case GNUNET_MESSAGE_TYPE_CADET_AX:
950 return GCC_is_sendable (q->c, q->fwd);
955 * Get first sendable message.
957 * @param peer The destination peer.
959 * @return First transmittable message, if any. Otherwise, NULL.
961 static struct CadetPeerQueue *
962 peer_get_first_message (const struct CadetPeer *peer)
964 struct CadetPeerQueue *q;
966 for (q = peer->queue_head; NULL != q; q = q->next)
968 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
969 if (queue_is_sendable (q))
978 * Function to process paths received for a new peer addition. The recorded
979 * paths form the initial tunnel, which can be optimized later.
980 * Called on each result obtained for the DHT search.
986 search_handler (void *cls, const struct CadetPeerPath *path)
988 struct CadetPeer *peer = cls;
989 unsigned int connection_count;
991 GCP_add_path_to_all (path, GNUNET_NO);
993 /* Count connections */
994 connection_count = GCT_count_connections (peer->tunnel);
996 /* If we already have our minimum (or more) connections, it's enough */
997 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1000 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1002 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1010 * Adjust core requested size to accomodate an ACK.
1012 * @param message_size Requested size.
1014 * @return Size enough to fit @c message_size and an ACK.
1017 get_core_size (size_t message_size)
1019 return message_size + sizeof (struct GNUNET_CADET_ACK);
1024 * Fill a core buffer with the appropriate data for the queued message.
1026 * @param queue Queue element for the message.
1027 * @param buf Core buffer to fill.
1028 * @param size Size remaining in @c buf.
1029 * @param[out] pid In case its an encrypted payload, set payload.
1031 * @return Bytes written to @c buf.
1034 fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
1036 struct CadetConnection *c = queue->c;
1039 switch (queue->type)
1041 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1042 *pid = GCC_get_pid (queue->c, queue->fwd);
1043 LOG (GNUNET_ERROR_TYPE_DEBUG, " otr payload ID %u\n", *pid);
1044 msg_size = send_core_data_raw (queue->cls, size, buf);
1045 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
1047 case GNUNET_MESSAGE_TYPE_CADET_AX:
1048 *pid = GCC_get_pid (queue->c, queue->fwd);
1049 LOG (GNUNET_ERROR_TYPE_DEBUG, " ax payload ID %u\n", *pid);
1050 msg_size = send_core_data_raw (queue->cls, size, buf);
1051 ((struct GNUNET_CADET_AX *) buf)->pid = htonl (*pid);
1053 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1054 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1055 case GNUNET_MESSAGE_TYPE_CADET_KX:
1056 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1057 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1058 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1059 msg_size = send_core_data_raw (queue->cls, size, buf);
1061 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1062 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1063 if (GCC_is_origin (c, GNUNET_YES))
1064 msg_size = send_core_connection_create (c, size, buf);
1066 msg_size = send_core_data_raw (queue->cls, size, buf);
1068 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1069 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1070 if (GCC_is_origin (c, GNUNET_NO) ||
1071 GCC_is_origin (c, GNUNET_YES))
1073 msg_size = send_core_connection_ack (c, size, buf);
1077 msg_size = send_core_data_raw (queue->cls, size, buf);
1080 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1081 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1082 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1083 /* This should be encapsulted */
1089 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1093 GNUNET_assert (size >= msg_size);
1100 * Core callback to write a queued packet to core buffer
1102 * @param cls Closure (peer info).
1103 * @param size Number of bytes available in buf.
1104 * @param buf Where the to write the message.
1106 * @return number of bytes written to buf
1109 queue_send (void *cls, size_t size, void *buf)
1111 struct CadetPeer *peer = cls;
1112 struct CadetConnection *c;
1113 struct CadetPeerQueue *queue;
1114 struct GNUNET_TIME_Relative core_wait_time;
1115 const struct GNUNET_PeerIdentity *dst_id;
1122 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1123 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1124 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
1125 GCP_2s (peer), size);
1127 /* Sanity checking */
1128 if (NULL == buf || 0 == size)
1130 LOG (GNUNET_ERROR_TYPE_DEBUG, "Buffer size 0.\n");
1131 peer->tmt_time.abs_value_us = 0;
1132 peer->core_transmit = NULL;
1141 peer->core_transmit = NULL;
1142 queue = peer_get_first_message (peer);
1145 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
1146 peer->tmt_time.abs_value_us = 0;
1149 core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1150 LOG (GNUNET_ERROR_TYPE_DEBUG, " core wait time %s\n",
1151 GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_NO));
1152 peer->tmt_time.abs_value_us = 0;
1154 /* Copy all possible messages to the core buffer */
1155 while (NULL != queue && rest >= queue->size)
1159 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s %s\n",
1160 GCC_2s (c), GC_f2s(queue->fwd));
1161 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok (%u/%u)\n",
1162 queue->size, total_size, size);
1164 msg_size = fill_buf (queue, (void *) dst, size, &pid);
1166 if (0 < drop_percent &&
1167 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1169 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on connection %s %s\n",
1170 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1171 queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
1176 LOG (GNUNET_ERROR_TYPE_INFO,
1177 "snd %s (%s %4u) on connection %s (%p) %s (size %u)\n",
1178 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1179 queue->payload_id, GCC_2s (c), c, GC_f2s (queue->fwd), msg_size);
1181 total_size += msg_size;
1183 dst = &dst[msg_size];
1186 /* Free queue, but cls was freed by send_core_* in fill_buf. */
1187 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1190 queue = peer_get_first_message (peer);
1193 /* If more data in queue, send next */
1196 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data! (%u)\n", queue->size);
1197 if (NULL == peer->core_transmit)
1199 dst_id = GNUNET_PEER_resolve2 (peer->id);
1200 peer->core_transmit =
1201 GNUNET_CORE_notify_transmit_ready (core_handle,
1202 GNUNET_NO, get_priority (queue),
1203 GNUNET_TIME_UNIT_FOREVER_REL,
1205 get_core_size (queue->size),
1208 peer->tmt_time = GNUNET_TIME_absolute_get ();
1209 queue->start_waiting = GNUNET_TIME_absolute_get ();
1213 LOG (GNUNET_ERROR_TYPE_DEBUG, "* tmt rdy called somewhere else\n");
1215 // GCC_start_poll (); FIXME needed?
1219 // GCC_stop_poll(); FIXME needed?
1222 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", total_size);
1223 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1229 /******************************************************************************/
1230 /******************************** API ***********************************/
1231 /******************************************************************************/
1235 * Free a transmission that was already queued with all resources
1236 * associated to the request.
1238 * If connection was marked to be destroyed, and this was the last queued
1239 * message on it, the connection will be free'd as a result.
1241 * @param queue Queue handler to cancel.
1242 * @param clear_cls Is it necessary to free associated cls?
1243 * @param sent Was it really sent? (Could have been canceled)
1244 * @param pid PID, if relevant (was sent and was a payload message).
1246 * @return #GNUNET_YES if connection was destroyed as a result,
1247 * #GNUNET_NO otherwise.
1250 GCP_queue_destroy (struct CadetPeerQueue *queue, int clear_cls,
1251 int sent, uint32_t pid)
1253 struct CadetPeer *peer;
1254 int connection_destroyed;
1257 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1258 if (GNUNET_YES == clear_cls)
1260 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1261 switch (queue->type)
1263 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1264 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1266 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1267 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1268 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1269 case GNUNET_MESSAGE_TYPE_CADET_KX:
1270 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1271 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1272 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1273 GNUNET_free_non_null (queue->cls);
1278 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1279 GC_m2s (queue->type));
1282 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1284 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1285 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1290 if (NULL != queue->callback)
1292 struct GNUNET_TIME_Relative wait_time;
1294 wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1295 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1296 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1297 connection_destroyed = queue->callback (queue->callback_cls,
1298 queue->c, sent, queue->type, pid,
1299 queue->fwd, queue->size,
1304 connection_destroyed = GNUNET_NO;
1307 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1309 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1310 peer->core_transmit = NULL;
1311 peer->tmt_time.abs_value_us = 0;
1314 GNUNET_free (queue);
1315 return connection_destroyed;
1320 * @brief Queue and pass message to core when possible.
1322 * @param peer Peer towards which to queue the message.
1323 * @param cls Closure (@c type dependant). It will be used by queue_send to
1324 * build the message to be sent if not already prebuilt.
1325 * @param type Type of the message, 0 for a raw message.
1326 * @param size Size of the message.
1327 * @param c Connection this message belongs to (can be NULL).
1328 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1329 * @param cont Continuation to be called once CORE has taken the message.
1330 * @param cont_cls Closure for @c cont.
1332 * @return Handle to cancel the message before it is sent. Once cont is called
1333 * message has been sent and therefore the handle is no longer valid.
1335 struct CadetPeerQueue *
1336 GCP_queue_add (struct CadetPeer *peer, void *cls, uint16_t type,
1337 uint16_t payload_type, uint32_t payload_id, size_t size,
1338 struct CadetConnection *c, int fwd,
1339 GCP_sent cont, void *cont_cls)
1341 struct CadetPeerQueue *q;
1346 if (NULL == c && GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN != type)
1347 error_level = GNUNET_ERROR_TYPE_ERROR;
1349 error_level = GNUNET_ERROR_TYPE_INFO;
1351 "que %s (%s %4u) on connection %s (%p) %s towards %s (size %u)\n",
1352 GC_m2s (type), GC_m2s (payload_type), payload_id,
1353 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1355 if (error_level == GNUNET_ERROR_TYPE_ERROR)
1357 if (NULL == peer->connections)
1359 /* We are not connected to this peer, ignore request. */
1360 LOG (GNUNET_ERROR_TYPE_WARNING, "%s not a neighbor\n", GCP_2s (peer));
1361 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1368 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1369 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1374 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1376 call_core = (NULL == c || type == GNUNET_MESSAGE_TYPE_CADET_KX) ?
1377 GNUNET_YES : GCC_is_sendable (c, fwd);
1378 q = GNUNET_new (struct CadetPeerQueue);
1381 q->payload_type = payload_type;
1382 q->payload_id = payload_id;
1388 q->callback_cls = cont_cls;
1391 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1396 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1397 call_core = GNUNET_YES;
1400 q->start_waiting = GNUNET_TIME_absolute_get ();
1401 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1403 LOG (GNUNET_ERROR_TYPE_DEBUG,
1404 "calling core tmt rdy towards %s for %u bytes\n",
1405 GCP_2s (peer), size);
1406 peer->core_transmit =
1407 GNUNET_CORE_notify_transmit_ready (core_handle,
1408 GNUNET_NO, get_priority (q),
1409 GNUNET_TIME_UNIT_FOREVER_REL,
1410 GNUNET_PEER_resolve2 (peer->id),
1411 get_core_size (size),
1413 peer->tmt_time = GNUNET_TIME_absolute_get ();
1415 else if (GNUNET_NO == call_core)
1417 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1423 struct GNUNET_TIME_Relative elapsed;
1424 elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1425 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
1427 GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
1430 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1436 * Cancel all queued messages to a peer that belong to a certain connection.
1438 * @param peer Peer towards whom to cancel.
1439 * @param c Connection whose queued messages to cancel. Might be destroyed by
1440 * the sent continuation call.
1443 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c)
1445 struct CadetPeerQueue *q;
1446 struct CadetPeerQueue *next;
1447 struct CadetPeerQueue *prev;
1448 int connection_destroyed;
1450 connection_destroyed = GNUNET_NO;
1451 for (q = peer->queue_head; NULL != q; q = next)
1456 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP queue cancel %s\n", GC_m2s (q->type));
1457 GNUNET_break (GNUNET_NO == connection_destroyed);
1458 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1464 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1467 /* Get next from prev, q->next might be already freed:
1468 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1471 next = peer->queue_head;
1481 if (NULL == peer->queue_head && NULL != peer->core_transmit)
1483 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1484 peer->core_transmit = NULL;
1485 peer->tmt_time.abs_value_us = 0;
1491 * Get the first transmittable message for a connection.
1493 * @param peer Neighboring peer.
1494 * @param c Connection.
1496 * @return First transmittable message.
1498 static struct CadetPeerQueue *
1499 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1501 struct CadetPeerQueue *q;
1503 for (q = peer->queue_head; NULL != q; q = q->next)
1507 if (queue_is_sendable (q))
1509 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1512 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1520 * Get the first message for a connection and unqueue it.
1522 * Only tunnel (or higher) level messages are unqueued. Connection specific
1523 * messages are silently destroyed upon encounter.
1525 * @param peer Neighboring peer.
1526 * @param c Connection.
1527 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1530 * @return First message for this connection.
1532 struct GNUNET_MessageHeader *
1533 GCP_connection_pop (struct CadetPeer *peer,
1534 struct CadetConnection *c,
1537 struct CadetPeerQueue *q;
1538 struct CadetPeerQueue *next;
1539 struct GNUNET_MessageHeader *msg;
1542 GNUNET_assert (NULL != destroyed);
1543 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on connection %p\n", c);
1544 for (q = peer->queue_head; NULL != q; q = next)
1549 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), callback: %p\n",
1550 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1554 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1555 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1556 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1557 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1558 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1559 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1560 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1561 if (GNUNET_YES == dest)
1563 GNUNET_break (GNUNET_NO == *destroyed);
1564 *destroyed = GNUNET_YES;
1568 case GNUNET_MESSAGE_TYPE_CADET_KX:
1569 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1570 msg = (struct GNUNET_MessageHeader *) q->cls;
1571 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1572 if (GNUNET_YES == dest)
1574 GNUNET_break (GNUNET_NO == *destroyed);
1575 *destroyed = GNUNET_YES;
1581 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1589 * Unlock a possibly locked queue for a connection.
1591 * If there is a message that can be sent on this connection, call core for it.
1592 * Otherwise (if core transmit is already called or there is no sendable
1593 * message) do nothing.
1595 * @param peer Peer who keeps the queue.
1596 * @param c Connection whose messages to unlock.
1599 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1601 struct CadetPeerQueue *q;
1604 if (NULL != peer->core_transmit)
1606 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1607 return; /* Already unlocked */
1610 q = connection_get_first_message (peer, c);
1613 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1614 return; /* Nothing to transmit */
1618 peer->core_transmit =
1619 GNUNET_CORE_notify_transmit_ready (core_handle,
1620 GNUNET_NO, get_priority (q),
1621 GNUNET_TIME_UNIT_FOREVER_REL,
1622 GNUNET_PEER_resolve2 (peer->id),
1623 get_core_size (size),
1626 peer->tmt_time = GNUNET_TIME_absolute_get ();
1631 * Initialize the peer subsystem.
1633 * @param c Configuration.
1636 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1638 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1639 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1641 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1644 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1645 "CADET", "MAX_PEERS", "USING DEFAULT");
1650 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1657 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1658 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1659 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1660 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1661 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1664 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1665 NULL, /* Closure passed to CADET functions */
1666 &core_init, /* Call core_init once connected */
1667 &core_connect, /* Handle connects */
1668 &core_disconnect, /* remove peers on disconnects */
1669 NULL, /* Don't notify about all incoming messages */
1670 GNUNET_NO, /* For header only in notification */
1671 NULL, /* Don't notify about all outbound messages */
1672 GNUNET_NO, /* For header-only out notification */
1673 core_handlers); /* Register these handlers */
1675 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1677 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1678 /* Notify callbacks */
1683 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1684 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1685 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1686 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1687 transport_handle = NULL;
1692 if (NULL == core_handle)
1695 GNUNET_SCHEDULER_shutdown ();
1703 * Shut down the peer subsystem.
1708 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1710 if (core_handle != NULL)
1712 GNUNET_CORE_disconnect (core_handle);
1715 if (transport_handle != NULL)
1717 GNUNET_TRANSPORT_disconnect (transport_handle);
1718 transport_handle = NULL;
1720 GNUNET_PEER_change_rc (myid, -1);
1725 * Retrieve the CadetPeer stucture associated with the peer, create one
1726 * and insert it in the appropriate structures if the peer is not known yet.
1728 * @param peer_id Full identity of the peer.
1730 * @return Existing or newly created peer structure.
1733 GCP_get (const struct GNUNET_PeerIdentity *peer_id)
1735 struct CadetPeer *peer;
1737 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1740 peer = GNUNET_new (struct CadetPeer);
1741 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1743 peer_delete_oldest ();
1745 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1746 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1747 peer->id = GNUNET_PEER_intern (peer_id);
1749 peer->last_contact = GNUNET_TIME_absolute_get ();
1756 * Retrieve the CadetPeer stucture associated with the peer, create one
1757 * and insert it in the appropriate structures if the peer is not known yet.
1759 * @param peer Short identity of the peer.
1761 * @return Existing or newly created peer structure.
1764 GCP_get_short (const GNUNET_PEER_Id peer)
1766 return GCP_get (GNUNET_PEER_resolve2 (peer));
1771 * Try to connect to a peer on transport level.
1773 * @param cls Closure (peer).
1774 * @param tc TaskContext.
1777 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1779 struct CadetPeer *peer = cls;
1781 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1784 GNUNET_TRANSPORT_try_connect (transport_handle,
1785 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1790 * Try to establish a new connection to this peer (in its tunnel).
1791 * If the peer doesn't have any path to it yet, try to get one.
1792 * If the peer already has some path, send a CREATE CONNECTION towards it.
1794 * @param peer Peer to connect to.
1797 GCP_connect (struct CadetPeer *peer)
1799 struct CadetTunnel *t;
1800 struct CadetPeerPath *p;
1801 struct CadetConnection *c;
1804 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1806 /* If we have a current hello, try to connect using it. */
1807 GCP_try_connect (peer);
1811 rerun_search = GNUNET_NO;
1813 if (NULL != peer->path_head)
1815 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1816 p = peer_get_best_path (peer);
1822 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1825 c = GCT_use_path (t, p);
1828 /* This case can happen when the path includes a first hop that is
1829 * not yet known to be connected.
1831 * This happens quite often during testing when running cadet
1832 * under valgrind: core connect notifications come very late and the
1833 * DHT result has already come and created a valid path.
1834 * In this case, the peer->connections hashmap will be NULL and
1835 * tunnel_use_path will not be able to create a connection from that
1838 * Re-running the DHT GET should give core time to callback.
1840 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1841 * updating statistics about this issue.
1843 rerun_search = GNUNET_YES;
1847 GCC_send_create (c);
1853 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1857 if (GNUNET_YES == rerun_search)
1859 struct GNUNET_TIME_Relative delay;
1861 GCP_stop_search (peer);
1862 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1863 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay, &delayed_search,
1868 if (GNUNET_NO == is_searching (peer))
1869 GCP_start_search (peer);
1874 * Chech whether there is a direct (core level) connection to peer.
1876 * @param peer Peer to check.
1878 * @return #GNUNET_YES if there is a direct connection.
1881 GCP_is_neighbor (const struct CadetPeer *peer)
1883 struct CadetPeerPath *path;
1885 if (NULL == peer->connections)
1888 for (path = peer->path_head; NULL != path; path = path->next)
1890 if (3 > path->length)
1894 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1900 * Create and initialize a new tunnel towards a peer, in case it has none.
1901 * In case the peer already has a tunnel, nothing is done.
1903 * Does not generate any traffic, just creates the local data structures.
1905 * @param peer Peer towards which to create the tunnel.
1908 GCP_add_tunnel (struct CadetPeer *peer)
1910 if (NULL != peer->tunnel)
1912 peer->tunnel = GCT_new (peer);
1917 * Add a connection to a neighboring peer.
1919 * Store that the peer is the first hop of the connection in one
1920 * direction and that on peer disconnect the connection must be
1921 * notified and destroyed, for it will no longer be valid.
1923 * @param peer Peer to add connection to.
1924 * @param c Connection to add.
1926 * @return GNUNET_OK on success.
1929 GCP_add_connection (struct CadetPeer *peer,
1930 struct CadetConnection *c)
1933 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding connection %s\n", GCC_2s (c));
1934 LOG (GNUNET_ERROR_TYPE_DEBUG, "to peer %s\n", GCP_2s (peer));
1936 if (NULL == peer->connections)
1939 LOG (GNUNET_ERROR_TYPE_DEBUG,
1940 "Peer %s is not a neighbor!\n",
1942 return GNUNET_SYSERR;
1944 LOG (GNUNET_ERROR_TYPE_DEBUG,
1945 "peer %s ok, has %u connections.\n",
1946 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1947 result = GNUNET_CONTAINER_multihashmap_put (peer->connections,
1950 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1951 LOG (GNUNET_ERROR_TYPE_DEBUG,
1952 " now has %u connections.\n",
1953 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1954 LOG (GNUNET_ERROR_TYPE_DEBUG, "result %u\n", result);
1961 * Add the path to the peer and update the path used to reach it in case this
1964 * @param peer Destination peer to add the path to.
1965 * @param path New path to add. Last peer must be the peer in arg 1.
1966 * Path will be either used of freed if already known.
1967 * @param trusted Do we trust that this path is real?
1969 * @return path if path was taken, pointer to existing duplicate if exists
1972 struct CadetPeerPath *
1973 GCP_add_path (struct CadetPeer *peer, struct CadetPeerPath *path,
1976 struct CadetPeerPath *aux;
1980 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1981 path->length, GCP_2s (peer));
1983 if (NULL == peer || NULL == path
1984 || path->peers[path->length - 1] != peer->id)
1987 path_destroy (path);
1991 for (l = 1; l < path->length; l++)
1993 if (path->peers[l] == myid)
1995 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1996 for (l2 = 0; l2 < path->length - l; l2++)
1998 path->peers[l2] = path->peers[l + l2];
2002 path->peers = GNUNET_realloc (path->peers,
2003 path->length * sizeof (GNUNET_PEER_Id));
2007 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
2009 if (2 >= path->length && GNUNET_NO == trusted)
2011 /* Only allow CORE to tell us about direct paths */
2012 path_destroy (path);
2016 l = path_get_length (path);
2019 path_destroy (path);
2023 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2024 for (aux = peer->path_head; aux != NULL; aux = aux->next)
2026 l2 = path_get_length (aux);
2029 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
2030 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
2031 peer->path_tail, aux, path);
2036 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2038 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
2039 path_destroy (path);
2044 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
2046 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
2049 if (NULL != peer->tunnel
2050 && CONNECTIONS_PER_TUNNEL < GCT_count_connections (peer->tunnel))
2059 * Add the path to the origin peer and update the path used to reach it in case
2060 * this is the shortest.
2061 * The path is given in peer_info -> destination, therefore we turn the path
2062 * upside down first.
2064 * @param peer Peer to add the path to, being the origin of the path.
2065 * @param path New path to add after being inversed.
2066 * Path will be either used or freed.
2067 * @param trusted Do we trust that this path is real?
2069 * @return path if path was taken, pointer to existing duplicate if exists
2072 struct CadetPeerPath *
2073 GCP_add_path_to_origin (struct CadetPeer *peer,
2074 struct CadetPeerPath *path,
2080 return GCP_add_path (peer, path, trusted);
2085 * Adds a path to the info of all the peers in the path
2087 * @param p Path to process.
2088 * @param confirmed Whether we know if the path works or not.
2091 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
2095 /* TODO: invert and add */
2096 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2097 for (i++; i < p->length; i++)
2099 struct CadetPeer *aux;
2100 struct CadetPeerPath *copy;
2102 aux = GCP_get_short (p->peers[i]);
2103 copy = path_duplicate (p);
2104 copy->length = i + 1;
2105 GCP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2111 * Remove any path to the peer that has the extact same peers as the one given.
2113 * @param peer Peer to remove the path from.
2114 * @param path Path to remove. Is always destroyed .
2117 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
2119 struct CadetPeerPath *iter;
2120 struct CadetPeerPath *next;
2122 GNUNET_assert (myid == path->peers[0]);
2123 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2125 LOG (GNUNET_ERROR_TYPE_INFO, "Removing path %p (%u) from %s\n",
2126 path, path->length, GCP_2s (peer));
2128 for (iter = peer->path_head; NULL != iter; iter = next)
2131 if (0 == path_cmp (path, iter))
2133 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
2135 path_destroy (iter);
2138 path_destroy (path);
2143 * Remove a connection from a neighboring peer.
2145 * @param peer Peer to remove connection from.
2146 * @param c Connection to remove.
2148 * @return GNUNET_OK on success.
2151 GCP_remove_connection (struct CadetPeer *peer,
2152 const struct CadetConnection *c)
2154 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing connection %s\n", GCC_2s (c));
2155 LOG (GNUNET_ERROR_TYPE_DEBUG, "from peer %s\n", GCP_2s (peer));
2157 if (NULL == peer || NULL == peer->connections)
2159 LOG (GNUNET_ERROR_TYPE_DEBUG,
2160 "Peer %s is not a neighbor!\n",
2162 return GNUNET_SYSERR;
2164 LOG (GNUNET_ERROR_TYPE_DEBUG,
2165 "peer %s ok, has %u connections.\n",
2166 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
2168 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2174 * Start the DHT search for new paths towards the peer: we don't have
2175 * enough good connections.
2177 * @param peer Destination peer.
2180 GCP_start_search (struct CadetPeer *peer)
2182 const struct GNUNET_PeerIdentity *id;
2183 struct CadetTunnel *t = peer->tunnel;
2185 if (NULL != peer->search_h)
2191 if (NULL != peer->search_delayed)
2192 GCP_stop_search (peer);
2194 id = GNUNET_PEER_resolve2 (peer->id);
2195 peer->search_h = GCD_search (id, &search_handler, peer);
2199 /* Why would we search for a peer with no tunnel towards it? */
2204 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
2205 || 0 == GCT_count_any_connections (t))
2207 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2213 * Stop the DHT search for new paths towards the peer: we already have
2214 * enough good connections.
2216 * @param peer Destination peer.
2219 GCP_stop_search (struct CadetPeer *peer)
2221 if (NULL != peer->search_h)
2223 GCD_search_stop (peer->search_h);
2224 peer->search_h = NULL;
2226 if (NULL != peer->search_delayed)
2228 GNUNET_SCHEDULER_cancel (peer->search_delayed);
2229 peer->search_delayed = NULL;
2235 * Get the Full ID of a peer.
2237 * @param peer Peer to get from.
2239 * @return Full ID of peer.
2241 const struct GNUNET_PeerIdentity *
2242 GCP_get_id (const struct CadetPeer *peer)
2244 return GNUNET_PEER_resolve2 (peer->id);
2249 * Get the Short ID of a peer.
2251 * @param peer Peer to get from.
2253 * @return Short ID of peer.
2256 GCP_get_short_id (const struct CadetPeer *peer)
2265 * If tunnel is NULL and there was a search active, stop it, as it's useless.
2271 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2274 if (NULL == t && GNUNET_YES == is_searching (peer))
2276 GCP_stop_search (peer);
2282 * Get the tunnel towards a peer.
2284 * @param peer Peer to get from.
2286 * @return Tunnel towards peer.
2288 struct CadetTunnel *
2289 GCP_get_tunnel (const struct CadetPeer *peer)
2291 return peer->tunnel;
2296 * Set the hello message.
2298 * @param peer Peer whose message to set.
2299 * @param hello Hello message.
2302 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2304 struct GNUNET_HELLO_Message *old;
2307 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2311 old = GCP_get_hello (peer);
2314 size = GNUNET_HELLO_size (hello);
2315 LOG (GNUNET_ERROR_TYPE_DEBUG, " new (%u bytes)\n", size);
2316 peer->hello = GNUNET_malloc (size);
2317 memcpy (peer->hello, hello, size);
2321 peer->hello = GNUNET_HELLO_merge (old, hello);
2322 LOG (GNUNET_ERROR_TYPE_DEBUG, " merge into %p (%u bytes)\n",
2323 peer->hello, GNUNET_HELLO_size (hello));
2330 * Get the hello message.
2332 * @param peer Peer whose message to get.
2334 * @return Hello message.
2336 struct GNUNET_HELLO_Message *
2337 GCP_get_hello (struct CadetPeer *peer)
2339 struct GNUNET_TIME_Absolute expiration;
2340 struct GNUNET_TIME_Relative remaining;
2342 if (NULL == peer->hello)
2345 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2346 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2347 if (0 == remaining.rel_value_us)
2349 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2350 GNUNET_STRINGS_absolute_time_to_string (expiration));
2351 GNUNET_free (peer->hello);
2359 * Try to connect to a peer on TRANSPORT level.
2361 * @param peer Peer to whom to connect.
2364 GCP_try_connect (struct CadetPeer *peer)
2366 struct GNUNET_HELLO_Message *hello;
2367 struct GNUNET_MessageHeader *mh;
2369 if (NULL == transport_handle)
2372 hello = GCP_get_hello (peer);
2376 mh = GNUNET_HELLO_get_header (hello);
2377 GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
2382 * Check if the given ECDH key is correct for the peer.
2384 * This function caches the results if the key has been previoulsy checked,
2385 * otherwise checks that the key is signed with the peer's ID (EdDSA key).
2387 * TODO: save the cached public key to permanent storage / peerinfo.
2389 * @param peer Peer whose key to check.
2390 * @param key ECDH key to check.
2391 * @param purpose Purpose of the signature (followed by the key).
2392 * @param sig Signature with the peer's EdDSA key (PeerID).
2395 GCP_check_key (struct CadetPeer *peer,
2396 const struct GNUNET_CRYPTO_EcdhePublicKey *key,
2397 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
2398 const struct GNUNET_CRYPTO_EddsaSignature *sig)
2400 struct GNUNET_CRYPTO_EddsaPublicKey *pub;
2403 /* Is it the same as the cached key? */
2404 if (0 == memcmp (&peer->ax_key, key, sizeof (*key)))
2407 /* New key, verify. */
2408 pub = (struct GNUNET_CRYPTO_EddsaPublicKey *) GCP_get_id (peer);
2409 verified = GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_CADET_AXKX,
2412 if (GNUNET_OK != verified)
2413 return GNUNET_SYSERR;
2415 /* Cache key for later. */
2416 peer->ax_key = *key;
2422 * Notify a peer that a link between two other peers is broken. If any path
2423 * used that link, eliminate it.
2425 * @param peer Peer affected by the change.
2426 * @param peer1 Peer whose link is broken.
2427 * @param peer2 Peer whose link is broken.
2430 GCP_notify_broken_link (struct CadetPeer *peer,
2431 struct GNUNET_PeerIdentity *peer1,
2432 struct GNUNET_PeerIdentity *peer2)
2434 struct CadetPeerPath *iter;
2435 struct CadetPeerPath *next;
2440 p1 = GNUNET_PEER_search (peer1);
2441 p2 = GNUNET_PEER_search (peer2);
2443 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2444 if (0 == p1 || 0 == p2)
2446 /* We don't even know them */
2450 for (iter = peer->path_head; NULL != iter; iter = next)
2453 for (i = 0; i < iter->length - 1; i++)
2455 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2456 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2461 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2464 path_invalidate (iter);
2472 * Count the number of known paths toward the peer.
2474 * @param peer Peer to get path info.
2476 * @return Number of known paths.
2479 GCP_count_paths (const struct CadetPeer *peer)
2481 struct CadetPeerPath *iter;
2484 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2492 * Iterate all known peers.
2494 * @param iter Iterator.
2495 * @param cls Closure for @c iter.
2498 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2500 GNUNET_CONTAINER_multipeermap_iterate (peers, iter, cls);
2505 * Get the static string for a peer ID.
2509 * @return Static string for it's ID.
2512 GCP_2s (const struct CadetPeer *peer)
2516 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));