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.
120 * Struct containing all information regarding a given peer
130 * Last time we heard from this peer
132 struct GNUNET_TIME_Absolute last_contact;
135 * Paths to reach the peer, ordered by ascending hop count
137 struct CadetPeerPath *path_head;
140 * Paths to reach the peer, ordered by ascending hop count
142 struct CadetPeerPath *path_tail;
145 * Handle to stop the DHT search for paths to this peer
147 struct GCD_search_handle *search_h;
150 * Handle to stop the DHT search for paths to this peer
152 struct GNUNET_SCHEDULER_Task *search_delayed;
155 * Tunnel to this peer, if any.
157 struct CadetTunnel *tunnel;
160 * Connections that go through this peer, indexed by tid;
162 struct GNUNET_CONTAINER_MultiHashMap *connections;
165 * Handle for queued transmissions
167 struct GNUNET_CORE_TransmitHandle *core_transmit;
172 struct GNUNET_TIME_Absolute tmt_time;
175 * Transmission queue to core DLL head
177 struct CadetPeerQueue *queue_head;
180 * Transmission queue to core DLL tail
182 struct CadetPeerQueue *queue_tail;
185 * How many messages are in the queue to this peer.
187 unsigned int queue_n;
192 struct GNUNET_HELLO_Message* hello;
196 /******************************************************************************/
197 /******************************* GLOBALS ***********************************/
198 /******************************************************************************/
201 * Global handle to the statistics service.
203 extern struct GNUNET_STATISTICS_Handle *stats;
206 * Local peer own ID (full value).
208 extern struct GNUNET_PeerIdentity my_full_id;
211 * Local peer own ID (short)
213 extern GNUNET_PEER_Id myid;
216 * Peers known, indexed by PeerIdentity, values of type `struct CadetPeer`.
218 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
221 * How many peers do we want to remember?
223 static unsigned long long max_peers;
226 * Percentage of messages that will be dropped (for test purposes only).
228 static unsigned long long drop_percent;
231 * Handle to communicate with core.
233 static struct GNUNET_CORE_Handle *core_handle;
236 * Handle to try to start new connections.
238 static struct GNUNET_TRANSPORT_Handle *transport_handle;
241 /******************************************************************************/
242 /***************************** DEBUG *********************************/
243 /******************************************************************************/
246 * Log all kinds of info about the queueing status of a peer.
248 * @param p Peer whose queue to show.
249 * @param level Error level to use for logging.
252 queue_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
254 struct CadetPeerQueue *q;
257 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
259 __FILE__, __FUNCTION__, __LINE__);
263 LOG2 (level, "QQQ Message queue towards %s\n", GCP_2s (p));
264 LOG2 (level, "QQQ queue length: %u\n", p->queue_n);
265 LOG2 (level, "QQQ core tmt rdy: %p\n", p->core_transmit);
267 for (q = p->queue_head; NULL != q; q = q->next)
269 LOG2 (level, "QQQ - %s %s on %s\n",
270 GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
271 LOG2 (level, "QQQ payload %s, %u\n",
272 GC_m2s (q->payload_type), q->payload_id);
273 LOG2 (level, "QQQ size: %u bytes\n", q->size);
276 LOG2 (level, "QQQ End queue towards %s\n", GCP_2s (p));
281 * Log all kinds of info about a peer.
286 GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
288 struct CadetPeerPath *path;
292 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
294 __FILE__, __FUNCTION__, __LINE__);
300 LOG2 (level, "PPP DEBUG PEER NULL\n");
304 LOG2 (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
305 LOG2 (level, "PPP last contact %s\n",
306 GNUNET_STRINGS_absolute_time_to_string (p->last_contact));
307 for (path = p->path_head; NULL != path; path = path->next)
312 LOG2 (level, "PPP path: %s\n", s);
316 LOG2 (level, "PPP core transmit handle %p\n", p->core_transmit);
317 LOG2 (level, "PPP DHT GET handle %p\n", p->search_h);
318 if (NULL != p->connections)
319 conns = GNUNET_CONTAINER_multihashmap_size (p->connections);
322 LOG2 (level, "PPP # connections over link to peer: %u\n", conns);
323 queue_debug (p, level);
324 LOG2 (level, "PPP DEBUG END\n");
328 /******************************************************************************/
329 /***************************** CORE HELPERS *********************************/
330 /******************************************************************************/
334 * Iterator to notify all connections of a broken link. Mark connections
335 * to destroy after all traffic has been sent.
337 * @param cls Closure (peer disconnected).
338 * @param key Current key code (peer id).
339 * @param value Value in the hash map (connection).
341 * @return #GNUNET_YES to continue to iterate.
344 notify_broken (void *cls,
345 const struct GNUNET_HashCode *key,
348 struct CadetPeer *peer = cls;
349 struct CadetConnection *c = value;
351 LOG (GNUNET_ERROR_TYPE_DEBUG,
352 "Notifying %s due to %s\n",
355 GCC_notify_broken (c,
362 * Remove the direct path to the peer.
364 * @param peer Peer to remove the direct path from.
367 static struct CadetPeerPath *
368 pop_direct_path (struct CadetPeer *peer)
370 struct CadetPeerPath *iter;
372 for (iter = peer->path_head; NULL != iter; iter = iter->next)
374 if (2 >= iter->length)
376 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
384 /******************************************************************************/
385 /***************************** CORE CALLBACKS *********************************/
386 /******************************************************************************/
389 * Method called whenever a given peer connects.
392 * @param peer peer identity this notification is about
395 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
397 struct CadetPeer *mp;
398 struct CadetPeerPath *path;
401 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
405 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s (self)\n", own_id);
410 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s <= %s\n",
411 own_id, GNUNET_i2s (peer));
413 path->peers[1] = mp->id;
414 GNUNET_PEER_change_rc (mp->id, 1);
415 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
417 path->peers[0] = myid;
418 GNUNET_PEER_change_rc (myid, 1);
419 GCP_add_path (mp, path, GNUNET_YES);
421 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
423 if (NULL != GCP_get_tunnel (mp) &&
424 0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer))
434 * Method called whenever a peer disconnects.
437 * @param peer peer identity this notification is about
440 core_disconnect (void *cls,
441 const struct GNUNET_PeerIdentity *peer)
444 struct CadetPeerPath *direct_path;
447 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
448 p = GNUNET_CONTAINER_multipeermap_get (peers,
456 LOG (GNUNET_ERROR_TYPE_INFO,
457 "DISCONNECTED %s (self)\n",
460 LOG (GNUNET_ERROR_TYPE_INFO,
461 "DISCONNECTED %s <= %s\n",
462 own_id, GNUNET_i2s (peer));
463 direct_path = pop_direct_path (p);
464 GNUNET_CONTAINER_multihashmap_iterate (p->connections,
467 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
468 p->connections = NULL;
469 if (NULL != p->core_transmit)
471 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
472 p->core_transmit = NULL;
473 p->tmt_time.abs_value_us = 0;
475 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
477 path_destroy (direct_path);
483 * Functions to handle messages from core
485 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
486 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
487 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
488 sizeof (struct GNUNET_CADET_ConnectionACK)},
489 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
490 sizeof (struct GNUNET_CADET_ConnectionBroken)},
491 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
492 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
493 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
494 sizeof (struct GNUNET_CADET_ACK)},
495 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
496 sizeof (struct GNUNET_CADET_Poll)},
497 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
498 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
499 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_AX, 0}
504 * To be called on core init/fail.
506 * @param cls Closure (config)
507 * @param identity the public identity of this peer
510 core_init (void *cls,
511 const struct GNUNET_PeerIdentity *identity)
513 const struct GNUNET_CONFIGURATION_Handle *c = cls;
516 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
517 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
519 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
520 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
521 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
522 GNUNET_CORE_disconnect (core_handle);
523 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
524 NULL, /* Closure passed to CADET functions */
525 &core_init, /* Call core_init once connected */
526 &core_connect, /* Handle connects */
527 &core_disconnect, /* remove peers on disconnects */
528 NULL, /* Don't notify about all incoming messages */
529 GNUNET_NO, /* For header only in notification */
530 NULL, /* Don't notify about all outbound messages */
531 GNUNET_NO, /* For header-only out notification */
532 core_handlers); /* Register these handlers */
542 * Core callback to write a pre-constructed data packet to core buffer
544 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
545 * @param size Number of bytes available in buf.
546 * @param buf Where the to write the message.
548 * @return number of bytes written to buf
551 send_core_data_raw (void *cls, size_t size, void *buf)
553 struct GNUNET_MessageHeader *msg = cls;
556 GNUNET_assert (NULL != msg);
557 total_size = ntohs (msg->size);
559 if (total_size > size)
564 memcpy (buf, msg, total_size);
571 * Function to send a create connection message to a peer.
573 * @param c Connection to create.
574 * @param size number of bytes available in buf
575 * @param buf where the callee should write the message
576 * @return number of bytes written to buf
579 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
581 struct GNUNET_CADET_ConnectionCreate *msg;
582 struct GNUNET_PeerIdentity *peer_ptr;
583 const struct CadetPeerPath *p = GCC_get_path (c);
590 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
592 sizeof (struct GNUNET_CADET_ConnectionCreate) +
593 p->length * sizeof (struct GNUNET_PeerIdentity);
595 if (size < size_needed || NULL == buf)
600 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
601 msg->header.size = htons (size_needed);
602 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
603 msg->cid = *GCC_get_id (c);
605 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
606 for (i = 0; i < p->length; i++)
608 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
611 LOG (GNUNET_ERROR_TYPE_DEBUG,
612 "CONNECTION CREATE (%u bytes long) sent!\n",
619 * Creates a path ack message in buf and frees all unused resources.
621 * @param c Connection to send an ACK on.
622 * @param size number of bytes available in buf
623 * @param buf where the callee should write the message
625 * @return number of bytes written to buf
628 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
630 struct GNUNET_CADET_ConnectionACK *msg = buf;
632 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
633 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
638 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
639 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
640 msg->cid = *GCC_get_id (c);
642 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
643 return sizeof (struct GNUNET_CADET_ConnectionACK);
647 /******************************************************************************/
648 /******************************** STATIC ***********************************/
649 /******************************************************************************/
653 * Get priority for a queued message.
655 * @param q Queued message
657 * @return CORE priority to use.
659 static enum GNUNET_CORE_Priority
660 get_priority (struct CadetPeerQueue *q)
662 enum GNUNET_CORE_Priority low;
663 enum GNUNET_CORE_Priority high;
668 return GNUNET_CORE_PRIO_BACKGROUND;
671 /* Relayed traffic has lower priority, our own traffic has higher */
672 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
674 low = GNUNET_CORE_PRIO_BEST_EFFORT;
675 high = GNUNET_CORE_PRIO_URGENT;
679 low = GNUNET_CORE_PRIO_URGENT;
680 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
683 /* Bulky payload has lower priority, control traffic has higher. */
684 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type
685 || GNUNET_MESSAGE_TYPE_CADET_AX == q->type)
693 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
696 * @param key current key code
697 * @param value value in the hash map
698 * @return #GNUNET_YES if we should continue to iterate,
702 shutdown_tunnel (void *cls,
703 const struct GNUNET_PeerIdentity *key,
706 struct CadetPeer *p = value;
707 struct CadetTunnel *t = p->tunnel;
717 * Check if peer is searching for a path (either active or delayed search).
719 * @param peer Peer to check
721 * @return GNUNET_YES if there is a search active.
722 * GNUNET_NO otherwise.
725 is_searching (const struct CadetPeer *peer)
727 return (NULL == peer->search_h && NULL == peer->search_delayed) ?
728 GNUNET_NO : GNUNET_YES;
733 * @brief Start a search for a peer.
735 * @param cls Closure (Peer to search for).
736 * @param tc Task context.
739 delayed_search (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
741 struct CadetPeer *peer = cls;
743 peer->search_delayed = NULL;
745 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
748 GCP_start_search (peer);
753 * Destroy the peer_info and free any allocated resources linked to it
755 * @param peer The peer_info to destroy.
757 * @return GNUNET_OK on success
760 peer_destroy (struct CadetPeer *peer)
762 struct GNUNET_PeerIdentity id;
763 struct CadetPeerPath *p;
764 struct CadetPeerPath *nextp;
766 GNUNET_PEER_resolve (peer->id, &id);
767 GNUNET_PEER_change_rc (peer->id, -1);
769 LOG (GNUNET_ERROR_TYPE_WARNING,
770 "destroying peer %s\n",
774 GNUNET_CONTAINER_multipeermap_remove (peers,
779 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
781 GCP_stop_search (peer);
786 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
790 GCT_destroy_empty (peer->tunnel);
797 * Returns if peer is used (has a tunnel or is neighbor).
799 * @param peer Peer to check.
801 * @return #GNUNET_YES if peer is in use.
804 peer_is_used (struct CadetPeer *peer)
806 struct CadetPeerPath *p;
808 if (NULL != peer->tunnel)
811 for (p = peer->path_head; NULL != p; p = p->next)
821 * Iterator over all the peers to get the oldest timestamp.
823 * @param cls Closure (unsued).
824 * @param key ID of the peer.
825 * @param value Peer_Info of the peer.
828 peer_get_oldest (void *cls,
829 const struct GNUNET_PeerIdentity *key,
832 struct CadetPeer *p = value;
833 struct GNUNET_TIME_Absolute *abs = cls;
835 /* Don't count active peers */
836 if (GNUNET_YES == peer_is_used (p))
839 if (abs->abs_value_us < p->last_contact.abs_value_us)
840 abs->abs_value_us = p->last_contact.abs_value_us;
847 * Iterator over all the peers to remove the oldest entry.
849 * @param cls Closure (unsued).
850 * @param key ID of the peer.
851 * @param value Peer_Info of the peer.
854 peer_timeout (void *cls,
855 const struct GNUNET_PeerIdentity *key,
858 struct CadetPeer *p = value;
859 struct GNUNET_TIME_Absolute *abs = cls;
861 LOG (GNUNET_ERROR_TYPE_WARNING,
862 "peer %s timeout\n", GNUNET_i2s (key));
864 if (p->last_contact.abs_value_us == abs->abs_value_us &&
865 GNUNET_NO == peer_is_used (p))
875 * Delete oldest unused peer.
878 peer_delete_oldest (void)
880 struct GNUNET_TIME_Absolute abs;
882 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
884 GNUNET_CONTAINER_multipeermap_iterate (peers,
887 GNUNET_CONTAINER_multipeermap_iterate (peers,
894 * Choose the best (yet unused) path towards a peer,
895 * considering the tunnel properties.
897 * @param peer The destination peer.
899 * @return Best current known path towards the peer, if any.
901 static struct CadetPeerPath *
902 peer_get_best_path (const struct CadetPeer *peer)
904 struct CadetPeerPath *best_p;
905 struct CadetPeerPath *p;
906 unsigned int best_cost;
909 best_cost = UINT_MAX;
911 for (p = peer->path_head; NULL != p; p = p->next)
913 if (GNUNET_NO == path_is_valid (p))
914 continue; /* Don't use invalid paths. */
915 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
916 continue; /* If path is already in use, skip it. */
918 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
929 * Is this queue element sendable?
931 * - All management traffic is always sendable.
932 * - For payload traffic, check the connection flow control.
934 * @param q Queue element to inspect.
936 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
939 queue_is_sendable (struct CadetPeerQueue *q)
941 /* Is PID-independent? */
944 case GNUNET_MESSAGE_TYPE_CADET_ACK:
945 case GNUNET_MESSAGE_TYPE_CADET_POLL:
946 case GNUNET_MESSAGE_TYPE_CADET_KX:
947 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
948 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
949 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
950 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
953 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
954 case GNUNET_MESSAGE_TYPE_CADET_AX:
961 return GCC_is_sendable (q->c, q->fwd);
966 * Get first sendable message.
968 * @param peer The destination peer.
970 * @return First transmittable message, if any. Otherwise, NULL.
972 static struct CadetPeerQueue *
973 peer_get_first_message (const struct CadetPeer *peer)
975 struct CadetPeerQueue *q;
977 for (q = peer->queue_head; NULL != q; q = q->next)
979 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
980 if (queue_is_sendable (q))
989 * Function to process paths received for a new peer addition. The recorded
990 * paths form the initial tunnel, which can be optimized later.
991 * Called on each result obtained for the DHT search.
997 search_handler (void *cls, const struct CadetPeerPath *path)
999 struct CadetPeer *peer = cls;
1000 unsigned int connection_count;
1002 GCP_add_path_to_all (path, GNUNET_NO);
1004 /* Count connections */
1005 connection_count = GCT_count_connections (peer->tunnel);
1007 /* If we already have our minimum (or more) connections, it's enough */
1008 if (CONNECTIONS_PER_TUNNEL <= connection_count)
1011 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
1013 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1021 * Adjust core requested size to accomodate an ACK.
1023 * @param message_size Requested size.
1025 * @return Size enough to fit @c message_size and an ACK.
1028 get_core_size (size_t message_size)
1030 return message_size + sizeof (struct GNUNET_CADET_ACK);
1035 * Fill a core buffer with the appropriate data for the queued message.
1037 * @param queue Queue element for the message.
1038 * @param buf Core buffer to fill.
1039 * @param size Size remaining in @c buf.
1040 * @param[out] pid In case its an encrypted payload, set payload.
1042 * @return Bytes written to @c buf.
1045 fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
1047 struct CadetConnection *c = queue->c;
1050 switch (queue->type)
1052 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1053 *pid = GCC_get_pid (queue->c, queue->fwd);
1054 LOG (GNUNET_ERROR_TYPE_DEBUG, " otr payload ID %u\n", *pid);
1055 msg_size = send_core_data_raw (queue->cls, size, buf);
1056 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
1058 case GNUNET_MESSAGE_TYPE_CADET_AX:
1059 *pid = GCC_get_pid (queue->c, queue->fwd);
1060 LOG (GNUNET_ERROR_TYPE_DEBUG, " ax payload ID %u\n", *pid);
1061 msg_size = send_core_data_raw (queue->cls, size, buf);
1062 ((struct GNUNET_CADET_AX *) buf)->pid = htonl (*pid);
1064 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1065 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1066 case GNUNET_MESSAGE_TYPE_CADET_KX:
1067 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1068 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1069 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1070 msg_size = send_core_data_raw (queue->cls, size, buf);
1072 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1073 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1074 if (GCC_is_origin (c, GNUNET_YES))
1075 msg_size = send_core_connection_create (c, size, buf);
1077 msg_size = send_core_data_raw (queue->cls, size, buf);
1079 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1080 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1081 if (GCC_is_origin (c, GNUNET_NO) ||
1082 GCC_is_origin (c, GNUNET_YES))
1084 msg_size = send_core_connection_ack (c, size, buf);
1088 msg_size = send_core_data_raw (queue->cls, size, buf);
1091 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1092 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1093 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1094 /* This should be encapsulted */
1100 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1104 GNUNET_assert (size >= msg_size);
1111 * Core callback to write a queued packet to core buffer
1113 * @param cls Closure (peer info).
1114 * @param size Number of bytes available in buf.
1115 * @param buf Where the to write the message.
1117 * @return number of bytes written to buf
1120 queue_send (void *cls, size_t size, void *buf)
1122 struct CadetPeer *peer = cls;
1123 struct CadetConnection *c;
1124 struct CadetPeerQueue *queue;
1125 struct GNUNET_TIME_Relative core_wait_time;
1126 const struct GNUNET_PeerIdentity *dst_id;
1133 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1134 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1135 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
1136 GCP_2s (peer), size);
1138 /* Sanity checking */
1139 if (NULL == buf || 0 == size)
1141 LOG (GNUNET_ERROR_TYPE_DEBUG, "Buffer size 0.\n");
1142 peer->tmt_time.abs_value_us = 0;
1143 peer->core_transmit = NULL;
1152 peer->core_transmit = NULL;
1153 queue = peer_get_first_message (peer);
1156 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
1157 peer->tmt_time.abs_value_us = 0;
1160 core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1161 LOG (GNUNET_ERROR_TYPE_DEBUG, " core wait time %s\n",
1162 GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_NO));
1163 peer->tmt_time.abs_value_us = 0;
1165 /* Copy all possible messages to the core buffer */
1166 while (NULL != queue && rest >= queue->size)
1170 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s %s\n",
1171 GCC_2s (c), GC_f2s(queue->fwd));
1172 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok (%u/%u)\n",
1173 queue->size, total_size, size);
1175 msg_size = fill_buf (queue, (void *) dst, size, &pid);
1177 if (0 < drop_percent &&
1178 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1180 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on connection %s %s\n",
1181 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1182 queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
1187 LOG (GNUNET_ERROR_TYPE_INFO,
1188 "snd %s (%s %4u) on connection %s (%p) %s (size %u)\n",
1189 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1190 queue->payload_id, GCC_2s (c), c, GC_f2s (queue->fwd), msg_size);
1192 total_size += msg_size;
1194 dst = &dst[msg_size];
1197 /* Free queue, but cls was freed by send_core_* in fill_buf. */
1198 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1201 queue = peer_get_first_message (peer);
1204 /* If more data in queue, send next */
1207 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data! (%u)\n", queue->size);
1208 if (NULL == peer->core_transmit)
1210 dst_id = GNUNET_PEER_resolve2 (peer->id);
1211 peer->core_transmit =
1212 GNUNET_CORE_notify_transmit_ready (core_handle,
1213 GNUNET_NO, get_priority (queue),
1214 GNUNET_TIME_UNIT_FOREVER_REL,
1216 get_core_size (queue->size),
1219 peer->tmt_time = GNUNET_TIME_absolute_get ();
1220 queue->start_waiting = GNUNET_TIME_absolute_get ();
1224 LOG (GNUNET_ERROR_TYPE_DEBUG, "* tmt rdy called somewhere else\n");
1226 // GCC_start_poll (); FIXME needed?
1230 // GCC_stop_poll(); FIXME needed?
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", total_size);
1234 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1240 /******************************************************************************/
1241 /******************************** API ***********************************/
1242 /******************************************************************************/
1246 * Free a transmission that was already queued with all resources
1247 * associated to the request.
1249 * If connection was marked to be destroyed, and this was the last queued
1250 * message on it, the connection will be free'd as a result.
1252 * @param queue Queue handler to cancel.
1253 * @param clear_cls Is it necessary to free associated cls?
1254 * @param sent Was it really sent? (Could have been canceled)
1255 * @param pid PID, if relevant (was sent and was a payload message).
1257 * @return #GNUNET_YES if connection was destroyed as a result,
1258 * #GNUNET_NO otherwise.
1261 GCP_queue_destroy (struct CadetPeerQueue *queue, int clear_cls,
1262 int sent, uint32_t pid)
1264 struct CadetPeer *peer;
1265 int connection_destroyed;
1268 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1269 if (GNUNET_YES == clear_cls)
1271 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1272 switch (queue->type)
1274 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1275 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1277 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1278 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1279 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1280 case GNUNET_MESSAGE_TYPE_CADET_KX:
1281 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1282 case GNUNET_MESSAGE_TYPE_CADET_AX:
1283 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1284 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1285 GNUNET_free_non_null (queue->cls);
1290 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1291 GC_m2s (queue->type));
1294 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1296 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1297 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1302 if (NULL != queue->cont)
1304 struct GNUNET_TIME_Relative wait_time;
1306 wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1307 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1308 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1309 connection_destroyed = queue->cont (queue->cont_cls,
1310 queue->c, sent, queue->type, pid,
1311 queue->fwd, queue->size, wait_time);
1315 connection_destroyed = GNUNET_NO;
1318 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1320 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1321 peer->core_transmit = NULL;
1322 peer->tmt_time.abs_value_us = 0;
1325 GNUNET_free (queue);
1326 return connection_destroyed;
1331 * @brief Queue and pass message to core when possible.
1333 * @param peer Peer towards which to queue the message.
1334 * @param cls Closure (@c type dependant). It will be used by queue_send to
1335 * build the message to be sent if not already prebuilt.
1336 * @param type Type of the message, 0 for a raw message.
1337 * @param size Size of the message.
1338 * @param c Connection this message belongs to (can be NULL).
1339 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1340 * @param cont Continuation to be called once CORE has taken the message.
1341 * @param cont_cls Closure for @c cont.
1343 * @return Handle to cancel the message before it is sent. Once cont is called
1344 * message has been sent and therefore the handle is no longer valid.
1346 struct CadetPeerQueue *
1347 GCP_queue_add (struct CadetPeer *peer, void *cls, uint16_t type,
1348 uint16_t payload_type, uint32_t payload_id, size_t size,
1349 struct CadetConnection *c, int fwd,
1350 GCP_sent cont, void *cont_cls)
1352 struct CadetPeerQueue *q;
1357 if (NULL == c && GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN != type)
1358 error_level = GNUNET_ERROR_TYPE_ERROR;
1360 error_level = GNUNET_ERROR_TYPE_INFO;
1362 "que %s (%s %4u) on connection %s (%p) %s towards %s (size %u)\n",
1363 GC_m2s (type), GC_m2s (payload_type), payload_id,
1364 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1366 if (error_level == GNUNET_ERROR_TYPE_ERROR)
1368 if (NULL == peer->connections)
1370 /* We are not connected to this peer, ignore request. */
1371 LOG (GNUNET_ERROR_TYPE_WARNING, "%s not a neighbor\n", GCP_2s (peer));
1372 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1379 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1380 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1385 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1387 call_core = (NULL == c || type == GNUNET_MESSAGE_TYPE_CADET_KX) ?
1388 GNUNET_YES : GCC_is_sendable (c, fwd);
1389 q = GNUNET_new (struct CadetPeerQueue);
1392 q->payload_type = payload_type;
1393 q->payload_id = payload_id;
1399 q->cont_cls = cont_cls;
1402 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1407 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1408 call_core = GNUNET_YES;
1411 q->start_waiting = GNUNET_TIME_absolute_get ();
1412 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1414 LOG (GNUNET_ERROR_TYPE_DEBUG,
1415 "calling core tmt rdy towards %s for %u bytes\n",
1416 GCP_2s (peer), size);
1417 peer->core_transmit =
1418 GNUNET_CORE_notify_transmit_ready (core_handle,
1419 GNUNET_NO, get_priority (q),
1420 GNUNET_TIME_UNIT_FOREVER_REL,
1421 GNUNET_PEER_resolve2 (peer->id),
1422 get_core_size (size),
1424 peer->tmt_time = GNUNET_TIME_absolute_get ();
1426 else if (GNUNET_NO == call_core)
1428 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1434 struct GNUNET_TIME_Relative elapsed;
1435 elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1436 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
1438 GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
1441 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1447 * Cancel all queued messages to a peer that belong to a certain connection.
1449 * @param peer Peer towards whom to cancel.
1450 * @param c Connection whose queued messages to cancel. Might be destroyed by
1451 * the sent continuation call.
1454 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c)
1456 struct CadetPeerQueue *q;
1457 struct CadetPeerQueue *next;
1458 struct CadetPeerQueue *prev;
1459 int connection_destroyed;
1461 connection_destroyed = GNUNET_NO;
1462 for (q = peer->queue_head; NULL != q; q = next)
1467 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP queue cancel %s\n", GC_m2s (q->type));
1468 GNUNET_break (GNUNET_NO == connection_destroyed);
1469 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1475 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1478 /* Get next from prev, q->next might be already freed:
1479 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1482 next = peer->queue_head;
1492 if (NULL == peer->queue_head && NULL != peer->core_transmit)
1494 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1495 peer->core_transmit = NULL;
1496 peer->tmt_time.abs_value_us = 0;
1502 * Get the first transmittable message for a connection.
1504 * @param peer Neighboring peer.
1505 * @param c Connection.
1507 * @return First transmittable message.
1509 static struct CadetPeerQueue *
1510 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1512 struct CadetPeerQueue *q;
1514 for (q = peer->queue_head; NULL != q; q = q->next)
1518 if (queue_is_sendable (q))
1520 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1523 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1531 * Get the first message for a connection and unqueue it.
1533 * Only tunnel (or higher) level messages are unqueued. Connection specific
1534 * messages are silently destroyed upon encounter.
1536 * @param peer Neighboring peer.
1537 * @param c Connection.
1538 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1541 * @return First message for this connection.
1543 struct GNUNET_MessageHeader *
1544 GCP_connection_pop (struct CadetPeer *peer,
1545 struct CadetConnection *c,
1548 struct CadetPeerQueue *q;
1549 struct CadetPeerQueue *next;
1550 struct GNUNET_MessageHeader *msg;
1553 GNUNET_assert (NULL != destroyed);
1554 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on connection %p\n", c);
1555 for (q = peer->queue_head; NULL != q; q = next)
1560 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), cont: %p\n",
1561 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1565 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1566 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1567 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1568 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1569 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1570 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1571 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1572 if (GNUNET_YES == dest)
1574 GNUNET_break (GNUNET_NO == *destroyed);
1575 *destroyed = GNUNET_YES;
1579 case GNUNET_MESSAGE_TYPE_CADET_KX:
1580 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1581 case GNUNET_MESSAGE_TYPE_CADET_AX:
1582 case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
1583 msg = (struct GNUNET_MessageHeader *) q->cls;
1584 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1585 if (GNUNET_YES == dest)
1587 GNUNET_break (GNUNET_NO == *destroyed);
1588 *destroyed = GNUNET_YES;
1594 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1602 * Unlock a possibly locked queue for a connection.
1604 * If there is a message that can be sent on this connection, call core for it.
1605 * Otherwise (if core transmit is already called or there is no sendable
1606 * message) do nothing.
1608 * @param peer Peer who keeps the queue.
1609 * @param c Connection whose messages to unlock.
1612 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1614 struct CadetPeerQueue *q;
1617 if (NULL != peer->core_transmit)
1619 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1620 return; /* Already unlocked */
1623 q = connection_get_first_message (peer, c);
1626 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1627 return; /* Nothing to transmit */
1631 peer->core_transmit =
1632 GNUNET_CORE_notify_transmit_ready (core_handle,
1633 GNUNET_NO, get_priority (q),
1634 GNUNET_TIME_UNIT_FOREVER_REL,
1635 GNUNET_PEER_resolve2 (peer->id),
1636 get_core_size (size),
1639 peer->tmt_time = GNUNET_TIME_absolute_get ();
1644 * Initialize the peer subsystem.
1646 * @param c Configuration.
1649 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1651 LOG (GNUNET_ERROR_TYPE_DEBUG,
1653 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1655 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1658 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1659 "CADET", "MAX_PEERS", "USING DEFAULT");
1664 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1671 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1672 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1673 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1674 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1675 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1678 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1679 NULL, /* Closure passed to CADET functions */
1680 &core_init, /* Call core_init once connected */
1681 &core_connect, /* Handle connects */
1682 &core_disconnect, /* remove peers on disconnects */
1683 NULL, /* Don't notify about all incoming messages */
1684 GNUNET_NO, /* For header only in notification */
1685 NULL, /* Don't notify about all outbound messages */
1686 GNUNET_NO, /* For header-only out notification */
1687 core_handlers); /* Register these handlers */
1689 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1691 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1692 /* Notify callbacks */
1697 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1698 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1699 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1700 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1701 transport_handle = NULL;
1706 if (NULL == core_handle)
1709 GNUNET_SCHEDULER_shutdown ();
1717 * Shut down the peer subsystem.
1722 GNUNET_CONTAINER_multipeermap_iterate (peers,
1725 if (NULL != core_handle)
1727 GNUNET_CORE_disconnect (core_handle);
1730 if (NULL != transport_handle)
1732 GNUNET_TRANSPORT_disconnect (transport_handle);
1733 transport_handle = NULL;
1735 GNUNET_PEER_change_rc (myid, -1);
1736 GNUNET_CONTAINER_multipeermap_destroy (peers);
1742 * Retrieve the CadetPeer stucture associated with the peer, create one
1743 * and insert it in the appropriate structures if the peer is not known yet.
1745 * @param peer_id Full identity of the peer.
1747 * @return Existing or newly created peer structure.
1750 GCP_get (const struct GNUNET_PeerIdentity *peer_id)
1752 struct CadetPeer *peer;
1754 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1757 peer = GNUNET_new (struct CadetPeer);
1758 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1760 peer_delete_oldest ();
1762 GNUNET_CONTAINER_multipeermap_put (peers,
1765 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1766 peer->id = GNUNET_PEER_intern (peer_id);
1768 peer->last_contact = GNUNET_TIME_absolute_get ();
1775 * Retrieve the CadetPeer stucture associated with the peer, create one
1776 * and insert it in the appropriate structures if the peer is not known yet.
1778 * @param peer Short identity of the peer.
1780 * @return Existing or newly created peer structure.
1783 GCP_get_short (const GNUNET_PEER_Id peer)
1785 return GCP_get (GNUNET_PEER_resolve2 (peer));
1790 * Try to connect to a peer on transport level.
1792 * @param cls Closure (peer).
1793 * @param tc TaskContext.
1796 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1798 struct CadetPeer *peer = cls;
1800 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1803 GNUNET_TRANSPORT_try_connect (transport_handle,
1804 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1809 * Try to establish a new connection to this peer (in its tunnel).
1810 * If the peer doesn't have any path to it yet, try to get one.
1811 * If the peer already has some path, send a CREATE CONNECTION towards it.
1813 * @param peer Peer to connect to.
1816 GCP_connect (struct CadetPeer *peer)
1818 struct CadetTunnel *t;
1819 struct CadetPeerPath *p;
1820 struct CadetConnection *c;
1823 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1825 /* If we have a current hello, try to connect using it. */
1826 GCP_try_connect (peer);
1830 rerun_search = GNUNET_NO;
1832 if (NULL != peer->path_head)
1834 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1835 p = peer_get_best_path (peer);
1841 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1844 c = GCT_use_path (t, p);
1847 /* This case can happen when the path includes a first hop that is
1848 * not yet known to be connected.
1850 * This happens quite often during testing when running cadet
1851 * under valgrind: core connect notifications come very late and the
1852 * DHT result has already come and created a valid path.
1853 * In this case, the peer->connections hashmap will be NULL and
1854 * tunnel_use_path will not be able to create a connection from that
1857 * Re-running the DHT GET should give core time to callback.
1859 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1860 * updating statistics about this issue.
1862 rerun_search = GNUNET_YES;
1866 GCC_send_create (c);
1872 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1876 if (GNUNET_YES == rerun_search)
1878 struct GNUNET_TIME_Relative delay;
1880 GCP_stop_search (peer);
1881 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1882 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay, &delayed_search,
1887 if (GNUNET_NO == is_searching (peer))
1888 GCP_start_search (peer);
1893 * Chech whether there is a direct (core level) connection to peer.
1895 * @param peer Peer to check.
1897 * @return #GNUNET_YES if there is a direct connection.
1900 GCP_is_neighbor (const struct CadetPeer *peer)
1902 struct CadetPeerPath *path;
1904 if (NULL == peer->connections)
1907 for (path = peer->path_head; NULL != path; path = path->next)
1909 if (3 > path->length)
1913 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1919 * Create and initialize a new tunnel towards a peer, in case it has none.
1920 * In case the peer already has a tunnel, nothing is done.
1922 * Does not generate any traffic, just creates the local data structures.
1924 * @param peer Peer towards which to create the tunnel.
1927 GCP_add_tunnel (struct CadetPeer *peer)
1929 if (NULL != peer->tunnel)
1931 peer->tunnel = GCT_new (peer);
1936 * Add a connection to a neighboring peer.
1938 * Store that the peer is the first hop of the connection in one
1939 * direction and that on peer disconnect the connection must be
1940 * notified and destroyed, for it will no longer be valid.
1942 * @param peer Peer to add connection to.
1943 * @param c Connection to add.
1946 GCP_add_connection (struct CadetPeer *peer,
1947 struct CadetConnection *c)
1949 LOG (GNUNET_ERROR_TYPE_DEBUG,
1950 "adding connection %s\n",
1952 LOG (GNUNET_ERROR_TYPE_DEBUG,
1955 GNUNET_assert (NULL != peer->connections);
1956 LOG (GNUNET_ERROR_TYPE_DEBUG,
1957 "peer %s has %u connections.\n",
1959 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1960 GNUNET_assert (GNUNET_OK ==
1961 GNUNET_CONTAINER_multihashmap_put (peer->connections,
1964 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1965 LOG (GNUNET_ERROR_TYPE_DEBUG,
1966 " now has %u connections.\n",
1967 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1972 * Add the path to the peer and update the path used to reach it in case this
1975 * @param peer Destination peer to add the path to.
1976 * @param path New path to add. Last peer must be the peer in arg 1.
1977 * Path will be either used of freed if already known.
1978 * @param trusted Do we trust that this path is real?
1980 * @return path if path was taken, pointer to existing duplicate if exists
1983 struct CadetPeerPath *
1984 GCP_add_path (struct CadetPeer *peer, struct CadetPeerPath *path,
1987 struct CadetPeerPath *aux;
1991 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1992 path->length, GCP_2s (peer));
1994 if (NULL == peer || NULL == path
1995 || path->peers[path->length - 1] != peer->id)
1998 path_destroy (path);
2002 for (l = 1; l < path->length; l++)
2004 if (path->peers[l] == myid)
2006 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
2007 for (l2 = 0; l2 < path->length - l; l2++)
2009 path->peers[l2] = path->peers[l + l2];
2013 path->peers = GNUNET_realloc (path->peers,
2014 path->length * sizeof (GNUNET_PEER_Id));
2018 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
2020 if (2 >= path->length && GNUNET_NO == trusted)
2022 /* Only allow CORE to tell us about direct paths */
2023 path_destroy (path);
2027 l = path_get_length (path);
2030 path_destroy (path);
2034 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2035 for (aux = peer->path_head; aux != NULL; aux = aux->next)
2037 l2 = path_get_length (aux);
2040 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
2041 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
2042 peer->path_tail, aux, path);
2047 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2049 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
2050 path_destroy (path);
2055 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
2057 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
2060 if (NULL != peer->tunnel
2061 && CONNECTIONS_PER_TUNNEL < GCT_count_connections (peer->tunnel))
2070 * Add the path to the origin peer and update the path used to reach it in case
2071 * this is the shortest.
2072 * The path is given in peer_info -> destination, therefore we turn the path
2073 * upside down first.
2075 * @param peer Peer to add the path to, being the origin of the path.
2076 * @param path New path to add after being inversed.
2077 * Path will be either used or freed.
2078 * @param trusted Do we trust that this path is real?
2080 * @return path if path was taken, pointer to existing duplicate if exists
2083 struct CadetPeerPath *
2084 GCP_add_path_to_origin (struct CadetPeer *peer,
2085 struct CadetPeerPath *path,
2091 return GCP_add_path (peer, path, trusted);
2096 * Adds a path to the info of all the peers in the path
2098 * @param p Path to process.
2099 * @param confirmed Whether we know if the path works or not.
2102 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
2106 /* TODO: invert and add */
2107 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2108 for (i++; i < p->length; i++)
2110 struct CadetPeer *aux;
2111 struct CadetPeerPath *copy;
2113 aux = GCP_get_short (p->peers[i]);
2114 copy = path_duplicate (p);
2115 copy->length = i + 1;
2116 GCP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2122 * Remove any path to the peer that has the extact same peers as the one given.
2124 * @param peer Peer to remove the path from.
2125 * @param path Path to remove. Is always destroyed .
2128 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
2130 struct CadetPeerPath *iter;
2131 struct CadetPeerPath *next;
2133 GNUNET_assert (myid == path->peers[0]);
2134 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2136 LOG (GNUNET_ERROR_TYPE_INFO,
2137 "Removing path %p (%u) from %s\n",
2142 for (iter = peer->path_head; NULL != iter; iter = next)
2145 if (0 == path_cmp (path, iter))
2147 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
2149 path_destroy (iter);
2152 path_destroy (path);
2157 * Remove a connection from a neighboring peer.
2159 * @param peer Peer to remove connection from.
2160 * @param c Connection to remove.
2163 GCP_remove_connection (struct CadetPeer *peer,
2164 const struct CadetConnection *c)
2166 LOG (GNUNET_ERROR_TYPE_DEBUG,
2167 "removing connection %s\n",
2169 LOG (GNUNET_ERROR_TYPE_DEBUG,
2173 GNUNET_assert ( (NULL != peer) &&
2174 (NULL != peer->connections) );
2175 GNUNET_assert (GNUNET_OK ==
2176 GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2179 LOG (GNUNET_ERROR_TYPE_DEBUG,
2180 "peer %s ok, has %u connections left.\n",
2182 GNUNET_CONTAINER_multihashmap_size (peer->connections));
2186 * Start the DHT search for new paths towards the peer: we don't have
2187 * enough good connections.
2189 * @param peer Destination peer.
2192 GCP_start_search (struct CadetPeer *peer)
2194 const struct GNUNET_PeerIdentity *id;
2195 struct CadetTunnel *t = peer->tunnel;
2197 if (NULL != peer->search_h)
2203 if (NULL != peer->search_delayed)
2204 GCP_stop_search (peer);
2206 id = GNUNET_PEER_resolve2 (peer->id);
2207 peer->search_h = GCD_search (id, &search_handler, peer);
2211 /* Why would we search for a peer with no tunnel towards it? */
2216 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
2217 || 0 == GCT_count_any_connections (t))
2219 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2225 * Stop the DHT search for new paths towards the peer: we already have
2226 * enough good connections.
2228 * @param peer Destination peer.
2231 GCP_stop_search (struct CadetPeer *peer)
2233 if (NULL != peer->search_h)
2235 GCD_search_stop (peer->search_h);
2236 peer->search_h = NULL;
2238 if (NULL != peer->search_delayed)
2240 GNUNET_SCHEDULER_cancel (peer->search_delayed);
2241 peer->search_delayed = NULL;
2247 * Get the Full ID of a peer.
2249 * @param peer Peer to get from.
2251 * @return Full ID of peer.
2253 const struct GNUNET_PeerIdentity *
2254 GCP_get_id (const struct CadetPeer *peer)
2256 return GNUNET_PEER_resolve2 (peer->id);
2261 * Get the Short ID of a peer.
2263 * @param peer Peer to get from.
2265 * @return Short ID of peer.
2268 GCP_get_short_id (const struct CadetPeer *peer)
2277 * If tunnel is NULL and there was a search active, stop it, as it's useless.
2283 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2286 if (NULL == t && GNUNET_YES == is_searching (peer))
2288 GCP_stop_search (peer);
2294 * Get the tunnel towards a peer.
2296 * @param peer Peer to get from.
2298 * @return Tunnel towards peer.
2300 struct CadetTunnel *
2301 GCP_get_tunnel (const struct CadetPeer *peer)
2303 return peer->tunnel;
2308 * Set the hello message.
2310 * @param peer Peer whose message to set.
2311 * @param hello Hello message.
2314 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2316 struct GNUNET_HELLO_Message *old;
2319 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2323 old = GCP_get_hello (peer);
2326 size = GNUNET_HELLO_size (hello);
2327 LOG (GNUNET_ERROR_TYPE_DEBUG, " new (%u bytes)\n", size);
2328 peer->hello = GNUNET_malloc (size);
2329 memcpy (peer->hello, hello, size);
2333 peer->hello = GNUNET_HELLO_merge (old, hello);
2334 LOG (GNUNET_ERROR_TYPE_DEBUG, " merge into %p (%u bytes)\n",
2335 peer->hello, GNUNET_HELLO_size (hello));
2342 * Get the hello message.
2344 * @param peer Peer whose message to get.
2346 * @return Hello message.
2348 struct GNUNET_HELLO_Message *
2349 GCP_get_hello (struct CadetPeer *peer)
2351 struct GNUNET_TIME_Absolute expiration;
2352 struct GNUNET_TIME_Relative remaining;
2354 if (NULL == peer->hello)
2357 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2358 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2359 if (0 == remaining.rel_value_us)
2361 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2362 GNUNET_STRINGS_absolute_time_to_string (expiration));
2363 GNUNET_free (peer->hello);
2371 * Try to connect to a peer on TRANSPORT level.
2373 * @param peer Peer to whom to connect.
2376 GCP_try_connect (struct CadetPeer *peer)
2378 struct GNUNET_HELLO_Message *hello;
2379 struct GNUNET_MessageHeader *mh;
2381 if (NULL == transport_handle)
2384 hello = GCP_get_hello (peer);
2388 mh = GNUNET_HELLO_get_header (hello);
2389 GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
2394 * Notify a peer that a link between two other peers is broken. If any path
2395 * used that link, eliminate it.
2397 * @param peer Peer affected by the change.
2398 * @param peer1 Peer whose link is broken.
2399 * @param peer2 Peer whose link is broken.
2402 GCP_notify_broken_link (struct CadetPeer *peer,
2403 struct GNUNET_PeerIdentity *peer1,
2404 struct GNUNET_PeerIdentity *peer2)
2406 struct CadetPeerPath *iter;
2407 struct CadetPeerPath *next;
2412 p1 = GNUNET_PEER_search (peer1);
2413 p2 = GNUNET_PEER_search (peer2);
2415 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2416 if (0 == p1 || 0 == p2)
2418 /* We don't even know them */
2422 for (iter = peer->path_head; NULL != iter; iter = next)
2425 for (i = 0; i < iter->length - 1; i++)
2427 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2428 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2433 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2436 path_invalidate (iter);
2444 * Count the number of known paths toward the peer.
2446 * @param peer Peer to get path info.
2448 * @return Number of known paths.
2451 GCP_count_paths (const struct CadetPeer *peer)
2453 struct CadetPeerPath *iter;
2456 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2464 * Iterate all known peers.
2466 * @param iter Iterator.
2467 * @param cls Closure for @c iter.
2470 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
2473 GNUNET_CONTAINER_multipeermap_iterate (peers,
2480 * Get the static string for a peer ID.
2484 * @return Static string for it's ID.
2487 GCP_2s (const struct CadetPeer *peer)
2491 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));