2 This file is part of GNUnet.
3 Copyright (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 #include "gnunet_util_lib.h"
25 #include "gnunet_transport_service.h"
26 #include "gnunet_core_service.h"
27 #include "gnunet_statistics_service.h"
29 #include "cadet_protocol.h"
31 #include "gnunet-service-cadet_peer.h"
32 #include "gnunet-service-cadet_dht.h"
33 #include "gnunet-service-cadet_connection.h"
34 #include "gnunet-service-cadet_tunnel.h"
35 #include "cadet_path.h"
37 #define LOG(level, ...) GNUNET_log_from (level,"cadet-p2p",__VA_ARGS__)
38 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-p2p",__VA_ARGS__)
41 /******************************************************************************/
42 /******************************** STRUCTS **********************************/
43 /******************************************************************************/
46 * Struct containing info about a queued transmission to this peer
53 struct CadetPeerQueue *next;
58 struct CadetPeerQueue *prev;
61 * Peer this transmission is directed to.
63 struct CadetPeer *peer;
66 * Connection this message belongs to.
68 struct CadetConnection *c;
76 * Pointer to info stucture used as cls.
88 uint16_t payload_type;
101 * Set when this message starts waiting for CORE.
103 struct GNUNET_TIME_Absolute start_waiting;
106 * Function to call on sending.
111 * Closure for callback.
117 * Struct containing all information regarding a given peer
127 * Last time we heard from this peer
129 struct GNUNET_TIME_Absolute last_contact;
132 * Paths to reach the peer, ordered by ascending hop count
134 struct CadetPeerPath *path_head;
137 * Paths to reach the peer, ordered by ascending hop count
139 struct CadetPeerPath *path_tail;
142 * Handle to stop the DHT search for paths to this peer
144 struct GCD_search_handle *search_h;
147 * Handle to stop the DHT search for paths to this peer
149 struct GNUNET_SCHEDULER_Task *search_delayed;
152 * Tunnel to this peer, if any.
154 struct CadetTunnel *tunnel;
157 * Connections that go through this peer, indexed by tid;
159 struct GNUNET_CONTAINER_MultiHashMap *connections;
162 * Handle for queued transmissions
164 struct GNUNET_CORE_TransmitHandle *core_transmit;
169 struct GNUNET_TIME_Absolute tmt_time;
172 * Transmission queue to core DLL head
174 struct CadetPeerQueue *queue_head;
177 * Transmission queue to core DLL tail
179 struct CadetPeerQueue *queue_tail;
182 * How many messages are in the queue to this peer.
184 unsigned int queue_n;
189 struct GNUNET_HELLO_Message* hello;
193 /******************************************************************************/
194 /******************************* GLOBALS ***********************************/
195 /******************************************************************************/
198 * Global handle to the statistics service.
200 extern struct GNUNET_STATISTICS_Handle *stats;
203 * Local peer own ID (full value).
205 extern struct GNUNET_PeerIdentity my_full_id;
208 * Local peer own ID (short)
210 extern GNUNET_PEER_Id myid;
213 * Peers known, indexed by PeerIdentity (CadetPeer).
215 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
218 * How many peers do we want to remember?
220 static unsigned long long max_peers;
223 * Percentage of messages that will be dropped (for test purposes only).
225 static unsigned long long drop_percent;
228 * Handle to communicate with core.
230 static struct GNUNET_CORE_Handle *core_handle;
233 * Handle to try to start new connections.
235 static struct GNUNET_TRANSPORT_Handle *transport_handle;
238 /******************************************************************************/
239 /***************************** DEBUG *********************************/
240 /******************************************************************************/
243 * Log all kinds of info about the queueing status of a peer.
245 * @param p Peer whose queue to show.
246 * @param level Error level to use for logging.
249 queue_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
251 struct CadetPeerQueue *q;
254 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
256 __FILE__, __FUNCTION__, __LINE__);
260 LOG2 (level, "QQQ Message queue towards %s\n", GCP_2s (p));
261 LOG2 (level, "QQQ queue length: %u\n", p->queue_n);
262 LOG2 (level, "QQQ core tmt rdy: %p\n", p->core_transmit);
264 for (q = p->queue_head; NULL != q; q = q->next)
266 LOG2 (level, "QQQ - %s %s on %s\n",
267 GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
268 LOG2 (level, "QQQ payload %s, %u\n",
269 GC_m2s (q->payload_type), q->payload_id);
270 LOG2 (level, "QQQ size: %u bytes\n", q->size);
273 LOG2 (level, "QQQ End queue towards %s\n", GCP_2s (p));
278 * Log all kinds of info about a peer.
283 GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
285 struct CadetPeerPath *path;
289 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
291 __FILE__, __FUNCTION__, __LINE__);
297 LOG2 (level, "PPP DEBUG PEER NULL\n");
301 LOG2 (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
302 LOG2 (level, "PPP last contact %s\n",
303 GNUNET_STRINGS_absolute_time_to_string (p->last_contact));
304 for (path = p->path_head; NULL != path; path = path->next)
309 LOG2 (level, "PPP path: %s\n", s);
313 LOG2 (level, "PPP core transmit handle %p\n", p->core_transmit);
314 LOG2 (level, "PPP DHT GET handle %p\n", p->search_h);
315 if (NULL != p->connections)
316 conns = GNUNET_CONTAINER_multihashmap_size (p->connections);
319 LOG2 (level, "PPP # connections over link to peer: %u\n", conns);
320 queue_debug (p, level);
321 LOG2 (level, "PPP DEBUG END\n");
325 /******************************************************************************/
326 /***************************** CORE HELPERS *********************************/
327 /******************************************************************************/
331 * Iterator to notify all connections of a broken link. Mark connections
332 * to destroy after all traffic has been sent.
334 * @param cls Closure (peer disconnected).
335 * @param key Current key code (peer id).
336 * @param value Value in the hash map (connection).
338 * @return #GNUNET_YES to continue to iterate.
341 notify_broken (void *cls,
342 const struct GNUNET_HashCode *key,
345 struct CadetPeer *peer = cls;
346 struct CadetConnection *c = value;
348 LOG (GNUNET_ERROR_TYPE_DEBUG, " notifying %s due to %s\n",
349 GCC_2s (c), GCP_2s (peer));
350 GCC_notify_broken (c, peer);
357 * Remove the direct path to the peer.
359 * @param peer Peer to remove the direct path from.
362 static struct CadetPeerPath *
363 pop_direct_path (struct CadetPeer *peer)
365 struct CadetPeerPath *iter;
367 for (iter = peer->path_head; NULL != iter; iter = iter->next)
369 if (2 >= iter->length)
371 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
379 /******************************************************************************/
380 /***************************** CORE CALLBACKS *********************************/
381 /******************************************************************************/
384 * Method called whenever a given peer connects.
387 * @param peer peer identity this notification is about
390 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
392 struct CadetPeer *mp;
393 struct CadetPeerPath *path;
396 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
400 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s (self)\n", own_id);
405 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s <= %s\n",
406 own_id, GNUNET_i2s (peer));
408 path->peers[1] = mp->id;
409 GNUNET_PEER_change_rc (mp->id, 1);
410 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
412 path->peers[0] = myid;
413 GNUNET_PEER_change_rc (myid, 1);
414 GCP_add_path (mp, path, GNUNET_YES);
416 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
418 if (NULL != GCP_get_tunnel (mp) &&
419 0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer))
429 * Method called whenever a peer disconnects.
432 * @param peer peer identity this notification is about
435 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
438 struct CadetPeerPath *direct_path;
441 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
442 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
449 LOG (GNUNET_ERROR_TYPE_INFO, "DISCONNECTED %s (self)\n", own_id);
451 LOG (GNUNET_ERROR_TYPE_INFO, "DISCONNECTED %s <= %s\n",
452 own_id, GNUNET_i2s (peer));
453 direct_path = pop_direct_path (p);
454 GNUNET_CONTAINER_multihashmap_iterate (p->connections, ¬ify_broken, p);
455 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
456 p->connections = NULL;
457 if (NULL != p->core_transmit)
459 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
460 p->core_transmit = NULL;
461 p->tmt_time.abs_value_us = 0;
463 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
465 path_destroy (direct_path);
471 * Functions to handle messages from core
473 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
474 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
475 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
476 sizeof (struct GNUNET_CADET_ConnectionACK)},
477 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
478 sizeof (struct GNUNET_CADET_ConnectionBroken)},
479 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
480 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
481 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
482 sizeof (struct GNUNET_CADET_ACK)},
483 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
484 sizeof (struct GNUNET_CADET_Poll)},
485 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
486 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0}
491 * To be called on core init/fail.
493 * @param cls Closure (config)
494 * @param identity the public identity of this peer
497 core_init (void *cls,
498 const struct GNUNET_PeerIdentity *identity)
500 const struct GNUNET_CONFIGURATION_Handle *c = cls;
503 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
504 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
506 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
507 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
508 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
509 GNUNET_CORE_disconnect (core_handle);
510 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
511 NULL, /* Closure passed to CADET functions */
512 &core_init, /* Call core_init once connected */
513 &core_connect, /* Handle connects */
514 &core_disconnect, /* remove peers on disconnects */
515 NULL, /* Don't notify about all incoming messages */
516 GNUNET_NO, /* For header only in notification */
517 NULL, /* Don't notify about all outbound messages */
518 GNUNET_NO, /* For header-only out notification */
519 core_handlers); /* Register these handlers */
529 * Core callback to write a pre-constructed data packet to core buffer
531 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
532 * @param size Number of bytes available in buf.
533 * @param buf Where the to write the message.
535 * @return number of bytes written to buf
538 send_core_data_raw (void *cls, size_t size, void *buf)
540 struct GNUNET_MessageHeader *msg = cls;
543 GNUNET_assert (NULL != msg);
544 total_size = ntohs (msg->size);
546 if (total_size > size)
551 memcpy (buf, msg, total_size);
558 * Function to send a create connection message to a peer.
560 * @param c Connection to create.
561 * @param size number of bytes available in buf
562 * @param buf where the callee should write the message
563 * @return number of bytes written to buf
566 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
568 struct GNUNET_CADET_ConnectionCreate *msg;
569 struct GNUNET_PeerIdentity *peer_ptr;
570 const struct CadetPeerPath *p = GCC_get_path (c);
577 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
579 sizeof (struct GNUNET_CADET_ConnectionCreate) +
580 p->length * sizeof (struct GNUNET_PeerIdentity);
582 if (size < size_needed || NULL == buf)
587 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
588 msg->header.size = htons (size_needed);
589 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
590 msg->cid = *GCC_get_id (c);
592 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
593 for (i = 0; i < p->length; i++)
595 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
598 LOG (GNUNET_ERROR_TYPE_DEBUG,
599 "CONNECTION CREATE (%u bytes long) sent!\n",
606 * Creates a path ack message in buf and frees all unused resources.
608 * @param c Connection to send an ACK on.
609 * @param size number of bytes available in buf
610 * @param buf where the callee should write the message
612 * @return number of bytes written to buf
615 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
617 struct GNUNET_CADET_ConnectionACK *msg = buf;
619 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
620 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
625 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
626 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
627 msg->cid = *GCC_get_id (c);
629 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
630 return sizeof (struct GNUNET_CADET_ConnectionACK);
634 /******************************************************************************/
635 /******************************** STATIC ***********************************/
636 /******************************************************************************/
640 * Get priority for a queued message.
642 * @param q Queued message
644 * @return CORE priority to use.
646 static enum GNUNET_CORE_Priority
647 get_priority (struct CadetPeerQueue *q)
649 enum GNUNET_CORE_Priority low;
650 enum GNUNET_CORE_Priority high;
655 return GNUNET_CORE_PRIO_BACKGROUND;
658 /* Relayed traffic has lower priority, our own traffic has higher */
659 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
661 low = GNUNET_CORE_PRIO_BEST_EFFORT;
662 high = GNUNET_CORE_PRIO_URGENT;
666 low = GNUNET_CORE_PRIO_URGENT;
667 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
670 /* Bulky payload has lower priority, control traffic has higher. */
671 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type)
679 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
682 * @param key current key code
683 * @param value value in the hash map
684 * @return #GNUNET_YES if we should continue to iterate,
688 shutdown_tunnel (void *cls,
689 const struct GNUNET_PeerIdentity *key,
692 struct CadetPeer *p = value;
693 struct CadetTunnel *t = p->tunnel;
703 * Check if peer is searching for a path (either active or delayed search).
705 * @param peer Peer to check
707 * @return GNUNET_YES if there is a search active.
708 * GNUNET_NO otherwise.
711 is_searching (const struct CadetPeer *peer)
713 return (NULL == peer->search_h && NULL == peer->search_delayed) ?
714 GNUNET_NO : GNUNET_YES;
719 * @brief Start a search for a peer.
721 * @param cls Closure (Peer to search for).
722 * @param tc Task context.
725 delayed_search (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
727 struct CadetPeer *peer = cls;
729 peer->search_delayed = NULL;
731 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
734 GCP_start_search (peer);
739 * Destroy the peer_info and free any allocated resources linked to it
741 * @param peer The peer_info to destroy.
743 * @return GNUNET_OK on success
746 peer_destroy (struct CadetPeer *peer)
748 struct GNUNET_PeerIdentity id;
749 struct CadetPeerPath *p;
750 struct CadetPeerPath *nextp;
752 GNUNET_PEER_resolve (peer->id, &id);
753 GNUNET_PEER_change_rc (peer->id, -1);
755 LOG (GNUNET_ERROR_TYPE_WARNING, "destroying peer %s\n", GNUNET_i2s (&id));
758 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
761 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
763 GCP_stop_search (peer);
768 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
772 GCT_destroy_empty (peer->tunnel);
779 * Returns if peer is used (has a tunnel or is neighbor).
781 * @param peer Peer to check.
783 * @return #GNUNET_YES if peer is in use.
786 peer_is_used (struct CadetPeer *peer)
788 struct CadetPeerPath *p;
790 if (NULL != peer->tunnel)
793 for (p = peer->path_head; NULL != p; p = p->next)
803 * Iterator over all the peers to get the oldest timestamp.
805 * @param cls Closure (unsued).
806 * @param key ID of the peer.
807 * @param value Peer_Info of the peer.
810 peer_get_oldest (void *cls,
811 const struct GNUNET_PeerIdentity *key,
814 struct CadetPeer *p = value;
815 struct GNUNET_TIME_Absolute *abs = cls;
817 /* Don't count active peers */
818 if (GNUNET_YES == peer_is_used (p))
821 if (abs->abs_value_us < p->last_contact.abs_value_us)
822 abs->abs_value_us = p->last_contact.abs_value_us;
829 * Iterator over all the peers to remove the oldest entry.
831 * @param cls Closure (unsued).
832 * @param key ID of the peer.
833 * @param value Peer_Info of the peer.
836 peer_timeout (void *cls,
837 const struct GNUNET_PeerIdentity *key,
840 struct CadetPeer *p = value;
841 struct GNUNET_TIME_Absolute *abs = cls;
843 LOG (GNUNET_ERROR_TYPE_WARNING,
844 "peer %s timeout\n", GNUNET_i2s (key));
846 if (p->last_contact.abs_value_us == abs->abs_value_us &&
847 GNUNET_NO == peer_is_used (p))
857 * Delete oldest unused peer.
860 peer_delete_oldest (void)
862 struct GNUNET_TIME_Absolute abs;
864 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
866 GNUNET_CONTAINER_multipeermap_iterate (peers,
869 GNUNET_CONTAINER_multipeermap_iterate (peers,
876 * Choose the best (yet unused) path towards a peer,
877 * considering the tunnel properties.
879 * @param peer The destination peer.
881 * @return Best current known path towards the peer, if any.
883 static struct CadetPeerPath *
884 peer_get_best_path (const struct CadetPeer *peer)
886 struct CadetPeerPath *best_p;
887 struct CadetPeerPath *p;
888 unsigned int best_cost;
891 best_cost = UINT_MAX;
893 for (p = peer->path_head; NULL != p; p = p->next)
895 if (GNUNET_NO == path_is_valid (p))
896 continue; /* Don't use invalid paths. */
897 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
898 continue; /* If path is already in use, skip it. */
900 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
911 * Is this queue element sendable?
913 * - All management traffic is always sendable.
914 * - For payload traffic, check the connection flow control.
916 * @param q Queue element to inspect.
918 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
921 queue_is_sendable (struct CadetPeerQueue *q)
923 /* Is PID-independent? */
926 case GNUNET_MESSAGE_TYPE_CADET_ACK:
927 case GNUNET_MESSAGE_TYPE_CADET_POLL:
928 case GNUNET_MESSAGE_TYPE_CADET_KX:
929 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
930 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
931 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
932 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
935 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
942 return GCC_is_sendable (q->c, q->fwd);
947 * Get first sendable message.
949 * @param peer The destination peer.
951 * @return First transmittable message, if any. Otherwise, NULL.
953 static struct CadetPeerQueue *
954 peer_get_first_message (const struct CadetPeer *peer)
956 struct CadetPeerQueue *q;
958 for (q = peer->queue_head; NULL != q; q = q->next)
960 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking q:%p on c:%s\n", q, GCC_2s (q->c));
961 if (queue_is_sendable (q))
970 * Function to process paths received for a new peer addition. The recorded
971 * paths form the initial tunnel, which can be optimized later.
972 * Called on each result obtained for the DHT search.
978 search_handler (void *cls, const struct CadetPeerPath *path)
980 struct CadetPeer *peer = cls;
981 unsigned int connection_count;
983 GCP_add_path_to_all (path, GNUNET_NO);
985 /* Count connections */
986 connection_count = GCT_count_connections (peer->tunnel);
988 /* If we already have our minimum (or more) connections, it's enough */
989 if (CONNECTIONS_PER_TUNNEL <= connection_count)
992 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
994 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
1002 * Adjust core requested size to accomodate an ACK.
1004 * @param message_size Requested size.
1006 * @return Size enough to fit @c message_size and an ACK.
1009 get_core_size (size_t message_size)
1011 return message_size + sizeof (struct GNUNET_CADET_ACK);
1016 * Fill a core buffer with the appropriate data for the queued message.
1018 * @param queue Queue element for the message.
1019 * @param buf Core buffer to fill.
1020 * @param size Size remaining in @c buf.
1021 * @param[out] pid In case its an encrypted payload, set payload.
1023 * @return Bytes written to @c buf.
1026 fill_buf (struct CadetPeerQueue *queue, void *buf, size_t size, uint32_t *pid)
1028 struct CadetConnection *c = queue->c;
1031 switch (queue->type)
1033 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1034 *pid = GCC_get_pid (queue->c, queue->fwd);
1035 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload ID %u\n", *pid);
1036 msg_size = send_core_data_raw (queue->cls, size, buf);
1037 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (*pid);
1039 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1040 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1041 case GNUNET_MESSAGE_TYPE_CADET_KX:
1042 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1043 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1044 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1045 msg_size = send_core_data_raw (queue->cls, size, buf);
1047 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1048 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1049 if (GCC_is_origin (c, GNUNET_YES))
1050 msg_size = send_core_connection_create (c, size, buf);
1052 msg_size = send_core_data_raw (queue->cls, size, buf);
1054 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1055 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1056 if (GCC_is_origin (c, GNUNET_NO) ||
1057 GCC_is_origin (c, GNUNET_YES))
1059 msg_size = send_core_connection_ack (c, size, buf);
1063 msg_size = send_core_data_raw (queue->cls, size, buf);
1066 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1067 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1068 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1069 /* This should be encapsulted */
1075 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1079 GNUNET_assert (size >= msg_size);
1086 * Core callback to write a queued packet to core buffer
1088 * @param cls Closure (peer info).
1089 * @param size Number of bytes available in buf.
1090 * @param buf Where the to write the message.
1092 * @return number of bytes written to buf
1095 queue_send (void *cls, size_t size, void *buf)
1097 struct CadetPeer *peer = cls;
1098 struct CadetConnection *c;
1099 struct CadetPeerQueue *queue;
1100 struct GNUNET_TIME_Relative core_wait_time;
1101 const struct GNUNET_PeerIdentity *dst_id;
1108 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1109 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1110 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
1111 GCP_2s (peer), size);
1113 /* Sanity checking */
1114 if (NULL == buf || 0 == size)
1116 LOG (GNUNET_ERROR_TYPE_DEBUG, "Buffer size 0.\n");
1117 peer->tmt_time.abs_value_us = 0;
1118 peer->core_transmit = NULL;
1127 peer->core_transmit = NULL;
1128 queue = peer_get_first_message (peer);
1131 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
1132 peer->tmt_time.abs_value_us = 0;
1135 core_wait_time = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1136 LOG (GNUNET_ERROR_TYPE_DEBUG, " core wait time %s\n",
1137 GNUNET_STRINGS_relative_time_to_string (core_wait_time, GNUNET_NO));
1138 peer->tmt_time.abs_value_us = 0;
1140 /* Copy all possible messages to the core buffer */
1141 while (NULL != queue && rest >= queue->size)
1145 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s %s\n",
1146 GCC_2s (c), GC_f2s(queue->fwd));
1147 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok (%u/%u)\n",
1148 queue->size, total_size, size);
1150 msg_size = fill_buf (queue, (void *) dst, size, &pid);
1152 if (0 < drop_percent &&
1153 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1155 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on connection %s %s\n",
1156 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1157 queue->payload_id, GCC_2s (c), GC_f2s (queue->fwd));
1162 LOG (GNUNET_ERROR_TYPE_INFO,
1163 "snd %s (%s %9u) on connection %s (%p) %s (size %u)\n",
1164 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1165 queue->payload_id, GCC_2s (c), c, GC_f2s (queue->fwd), msg_size);
1167 total_size += msg_size;
1169 dst = &dst[msg_size];
1172 /* Free queue, but cls was freed by send_core_* in fill_buf. */
1173 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1176 queue = peer_get_first_message (peer);
1179 /* If more data in queue, send next */
1182 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data! (%u)\n", queue->size);
1183 if (NULL == peer->core_transmit)
1185 dst_id = GNUNET_PEER_resolve2 (peer->id);
1186 peer->core_transmit =
1187 GNUNET_CORE_notify_transmit_ready (core_handle,
1188 GNUNET_NO, get_priority (queue),
1189 GNUNET_TIME_UNIT_FOREVER_REL,
1191 get_core_size (queue->size),
1194 peer->tmt_time = GNUNET_TIME_absolute_get ();
1195 queue->start_waiting = GNUNET_TIME_absolute_get ();
1199 LOG (GNUNET_ERROR_TYPE_DEBUG, "* tmt rdy called somewhere else\n");
1201 // GCC_start_poll (); FIXME needed?
1205 // GCC_stop_poll(); FIXME needed?
1208 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", total_size);
1209 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1215 /******************************************************************************/
1216 /******************************** API ***********************************/
1217 /******************************************************************************/
1221 * Free a transmission that was already queued with all resources
1222 * associated to the request.
1224 * If connection was marked to be destroyed, and this was the last queued
1225 * message on it, the connection will be free'd as a result.
1227 * @param queue Queue handler to cancel.
1228 * @param clear_cls Is it necessary to free associated cls?
1229 * @param sent Was it really sent? (Could have been canceled)
1230 * @param pid PID, if relevant (was sent and was a payload message).
1232 * @return #GNUNET_YES if connection was destroyed as a result,
1233 * #GNUNET_NO otherwise.
1236 GCP_queue_destroy (struct CadetPeerQueue *queue, int clear_cls,
1237 int sent, uint32_t pid)
1239 struct CadetPeer *peer;
1240 int connection_destroyed;
1243 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1244 if (GNUNET_YES == clear_cls)
1246 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1247 switch (queue->type)
1249 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1250 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1252 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1253 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1254 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1255 case GNUNET_MESSAGE_TYPE_CADET_KX:
1256 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1257 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1258 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1259 GNUNET_free_non_null (queue->cls);
1264 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1265 GC_m2s (queue->type));
1268 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1270 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1271 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1276 if (NULL != queue->callback)
1278 struct GNUNET_TIME_Relative wait_time;
1280 wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1281 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback, time elapsed %s\n",
1282 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1283 connection_destroyed = queue->callback (queue->callback_cls,
1284 queue->c, sent, queue->type, pid,
1285 queue->fwd, queue->size,
1290 connection_destroyed = GNUNET_NO;
1293 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1295 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1296 peer->core_transmit = NULL;
1297 peer->tmt_time.abs_value_us = 0;
1300 GNUNET_free (queue);
1301 return connection_destroyed;
1306 * @brief Queue and pass message to core when possible.
1308 * @param peer Peer towards which to queue the message.
1309 * @param cls Closure (@c type dependant). It will be used by queue_send to
1310 * build the message to be sent if not already prebuilt.
1311 * @param type Type of the message, 0 for a raw message.
1312 * @param size Size of the message.
1313 * @param c Connection this message belongs to (can be NULL).
1314 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1315 * @param cont Continuation to be called once CORE has taken the message.
1316 * @param cont_cls Closure for @c cont.
1318 * @return Handle to cancel the message before it is sent. Once cont is called
1319 * message has been sent and therefore the handle is no longer valid.
1321 struct CadetPeerQueue *
1322 GCP_queue_add (struct CadetPeer *peer, void *cls, uint16_t type,
1323 uint16_t payload_type, uint32_t payload_id, size_t size,
1324 struct CadetConnection *c, int fwd,
1325 GCP_sent cont, void *cont_cls)
1327 struct CadetPeerQueue *q;
1332 if (NULL == c && GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN != type)
1333 error_level = GNUNET_ERROR_TYPE_ERROR;
1335 error_level = GNUNET_ERROR_TYPE_INFO;
1337 "que %s (%s %9u) on connection %s (%p) %s towards %s (size %u)\n",
1338 GC_m2s (type), GC_m2s (payload_type), payload_id,
1339 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1341 if (error_level == GNUNET_ERROR_TYPE_ERROR)
1343 if (NULL == peer->connections)
1345 /* We are not connected to this peer, ignore request. */
1346 LOG (GNUNET_ERROR_TYPE_WARNING, "%s not a neighbor\n", GCP_2s (peer));
1347 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1354 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1355 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1360 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1362 call_core = (NULL == c || type == GNUNET_MESSAGE_TYPE_CADET_KX) ?
1363 GNUNET_YES : GCC_is_sendable (c, fwd);
1364 q = GNUNET_new (struct CadetPeerQueue);
1367 q->payload_type = payload_type;
1368 q->payload_id = payload_id;
1374 q->callback_cls = cont_cls;
1377 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1382 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1383 call_core = GNUNET_YES;
1386 q->start_waiting = GNUNET_TIME_absolute_get ();
1387 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1389 LOG (GNUNET_ERROR_TYPE_DEBUG,
1390 "calling core tmt rdy towards %s for %u bytes\n",
1391 GCP_2s (peer), size);
1392 peer->core_transmit =
1393 GNUNET_CORE_notify_transmit_ready (core_handle,
1394 GNUNET_NO, get_priority (q),
1395 GNUNET_TIME_UNIT_FOREVER_REL,
1396 GNUNET_PEER_resolve2 (peer->id),
1397 get_core_size (size),
1399 peer->tmt_time = GNUNET_TIME_absolute_get ();
1401 else if (GNUNET_NO == call_core)
1403 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1409 struct GNUNET_TIME_Relative elapsed;
1410 elapsed = GNUNET_TIME_absolute_get_duration (peer->tmt_time);
1411 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called %s\n",
1413 GNUNET_STRINGS_relative_time_to_string (elapsed, GNUNET_NO));
1416 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1422 * Cancel all queued messages to a peer that belong to a certain connection.
1424 * @param peer Peer towards whom to cancel.
1425 * @param c Connection whose queued messages to cancel. Might be destroyed by
1426 * the sent continuation call.
1429 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c)
1431 struct CadetPeerQueue *q;
1432 struct CadetPeerQueue *next;
1433 struct CadetPeerQueue *prev;
1434 int connection_destroyed;
1436 connection_destroyed = GNUNET_NO;
1437 for (q = peer->queue_head; NULL != q; q = next)
1442 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP queue cancel %s\n", GC_m2s (q->type));
1443 GNUNET_break (GNUNET_NO == connection_destroyed);
1444 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1450 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1453 /* Get next from prev, q->next might be already freed:
1454 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1457 next = peer->queue_head;
1467 if (NULL == peer->queue_head && NULL != peer->core_transmit)
1469 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1470 peer->core_transmit = NULL;
1471 peer->tmt_time.abs_value_us = 0;
1477 * Get the first transmittable message for a connection.
1479 * @param peer Neighboring peer.
1480 * @param c Connection.
1482 * @return First transmittable message.
1484 static struct CadetPeerQueue *
1485 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1487 struct CadetPeerQueue *q;
1489 for (q = peer->queue_head; NULL != q; q = q->next)
1493 if (queue_is_sendable (q))
1495 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1498 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1506 * Get the first message for a connection and unqueue it.
1508 * Only tunnel (or higher) level messages are unqueued. Connection specific
1509 * messages are silently destroyed upon encounter.
1511 * @param peer Neighboring peer.
1512 * @param c Connection.
1513 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1516 * @return First message for this connection.
1518 struct GNUNET_MessageHeader *
1519 GCP_connection_pop (struct CadetPeer *peer,
1520 struct CadetConnection *c,
1523 struct CadetPeerQueue *q;
1524 struct CadetPeerQueue *next;
1525 struct GNUNET_MessageHeader *msg;
1528 GNUNET_assert (NULL != destroyed);
1529 LOG (GNUNET_ERROR_TYPE_DEBUG, "connection_pop on connection %p\n", c);
1530 for (q = peer->queue_head; NULL != q; q = next)
1535 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), callback: %p\n",
1536 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1540 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1541 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1542 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1543 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1544 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1545 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1546 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1547 if (GNUNET_YES == dest)
1549 GNUNET_break (GNUNET_NO == *destroyed);
1550 *destroyed = GNUNET_YES;
1554 case GNUNET_MESSAGE_TYPE_CADET_KX:
1555 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1556 msg = (struct GNUNET_MessageHeader *) q->cls;
1557 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1558 if (GNUNET_YES == dest)
1560 GNUNET_break (GNUNET_NO == *destroyed);
1561 *destroyed = GNUNET_YES;
1567 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1575 * Unlock a possibly locked queue for a connection.
1577 * If there is a message that can be sent on this connection, call core for it.
1578 * Otherwise (if core transmit is already called or there is no sendable
1579 * message) do nothing.
1581 * @param peer Peer who keeps the queue.
1582 * @param c Connection whose messages to unlock.
1585 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1587 struct CadetPeerQueue *q;
1590 if (NULL != peer->core_transmit)
1592 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1593 return; /* Already unlocked */
1596 q = connection_get_first_message (peer, c);
1599 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1600 return; /* Nothing to transmit */
1604 peer->core_transmit =
1605 GNUNET_CORE_notify_transmit_ready (core_handle,
1606 GNUNET_NO, get_priority (q),
1607 GNUNET_TIME_UNIT_FOREVER_REL,
1608 GNUNET_PEER_resolve2 (peer->id),
1609 get_core_size (size),
1612 peer->tmt_time = GNUNET_TIME_absolute_get ();
1617 * Initialize the peer subsystem.
1619 * @param c Configuration.
1622 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1624 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1625 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1627 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1630 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1631 "CADET", "MAX_PEERS", "USING DEFAULT");
1636 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1643 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1644 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1645 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1646 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1647 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1650 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1651 NULL, /* Closure passed to CADET functions */
1652 &core_init, /* Call core_init once connected */
1653 &core_connect, /* Handle connects */
1654 &core_disconnect, /* remove peers on disconnects */
1655 NULL, /* Don't notify about all incoming messages */
1656 GNUNET_NO, /* For header only in notification */
1657 NULL, /* Don't notify about all outbound messages */
1658 GNUNET_NO, /* For header-only out notification */
1659 core_handlers); /* Register these handlers */
1661 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1663 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1664 /* Notify callbacks */
1669 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1670 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1671 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1672 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1673 transport_handle = NULL;
1678 if (NULL == core_handle)
1681 GNUNET_SCHEDULER_shutdown ();
1689 * Shut down the peer subsystem.
1694 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1696 if (core_handle != NULL)
1698 GNUNET_CORE_disconnect (core_handle);
1701 if (transport_handle != NULL)
1703 GNUNET_TRANSPORT_disconnect (transport_handle);
1704 transport_handle = NULL;
1706 GNUNET_PEER_change_rc (myid, -1);
1711 * Retrieve the CadetPeer stucture associated with the peer, create one
1712 * and insert it in the appropriate structures if the peer is not known yet.
1714 * @param peer_id Full identity of the peer.
1716 * @return Existing or newly created peer structure.
1719 GCP_get (const struct GNUNET_PeerIdentity *peer_id)
1721 struct CadetPeer *peer;
1723 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1726 peer = GNUNET_new (struct CadetPeer);
1727 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1729 peer_delete_oldest ();
1731 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1732 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1733 peer->id = GNUNET_PEER_intern (peer_id);
1735 peer->last_contact = GNUNET_TIME_absolute_get ();
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 Short identity of the peer.
1747 * @return Existing or newly created peer structure.
1750 GCP_get_short (const GNUNET_PEER_Id peer)
1752 return GCP_get (GNUNET_PEER_resolve2 (peer));
1757 * Try to connect to a peer on transport level.
1759 * @param cls Closure (peer).
1760 * @param tc TaskContext.
1763 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1765 struct CadetPeer *peer = cls;
1767 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1770 GNUNET_TRANSPORT_try_connect (transport_handle,
1771 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1776 * Try to establish a new connection to this peer (in its tunnel).
1777 * If the peer doesn't have any path to it yet, try to get one.
1778 * If the peer already has some path, send a CREATE CONNECTION towards it.
1780 * @param peer Peer to connect to.
1783 GCP_connect (struct CadetPeer *peer)
1785 struct CadetTunnel *t;
1786 struct CadetPeerPath *p;
1787 struct CadetConnection *c;
1790 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1792 /* If we have a current hello, try to connect using it. */
1793 GCP_try_connect (peer);
1797 rerun_search = GNUNET_NO;
1799 if (NULL != peer->path_head)
1801 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1802 p = peer_get_best_path (peer);
1808 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1811 c = GCT_use_path (t, p);
1814 /* This case can happen when the path includes a first hop that is
1815 * not yet known to be connected.
1817 * This happens quite often during testing when running cadet
1818 * under valgrind: core connect notifications come very late and the
1819 * DHT result has already come and created a valid path.
1820 * In this case, the peer->connections hashmap will be NULL and
1821 * tunnel_use_path will not be able to create a connection from that
1824 * Re-running the DHT GET should give core time to callback.
1826 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1827 * updating statistics about this issue.
1829 rerun_search = GNUNET_YES;
1833 GCC_send_create (c);
1839 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1843 if (GNUNET_YES == rerun_search)
1845 struct GNUNET_TIME_Relative delay;
1847 GCP_stop_search (peer);
1848 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1849 peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay, &delayed_search,
1854 if (GNUNET_NO == is_searching (peer))
1855 GCP_start_search (peer);
1860 * Chech whether there is a direct (core level) connection to peer.
1862 * @param peer Peer to check.
1864 * @return #GNUNET_YES if there is a direct connection.
1867 GCP_is_neighbor (const struct CadetPeer *peer)
1869 struct CadetPeerPath *path;
1871 if (NULL == peer->connections)
1874 for (path = peer->path_head; NULL != path; path = path->next)
1876 if (3 > path->length)
1880 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1886 * Create and initialize a new tunnel towards a peer, in case it has none.
1887 * In case the peer already has a tunnel, nothing is done.
1889 * Does not generate any traffic, just creates the local data structures.
1891 * @param peer Peer towards which to create the tunnel.
1894 GCP_add_tunnel (struct CadetPeer *peer)
1896 if (NULL != peer->tunnel)
1898 peer->tunnel = GCT_new (peer);
1903 * Add a connection to a neighboring peer.
1905 * Store that the peer is the first hop of the connection in one
1906 * direction and that on peer disconnect the connection must be
1907 * notified and destroyed, for it will no longer be valid.
1909 * @param peer Peer to add connection to.
1910 * @param c Connection to add.
1912 * @return GNUNET_OK on success.
1915 GCP_add_connection (struct CadetPeer *peer,
1916 struct CadetConnection *c)
1919 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding connection %s\n", GCC_2s (c));
1920 LOG (GNUNET_ERROR_TYPE_DEBUG, "to peer %s\n", GCP_2s (peer));
1922 if (NULL == peer->connections)
1925 LOG (GNUNET_ERROR_TYPE_DEBUG,
1926 "Peer %s is not a neighbor!\n",
1928 return GNUNET_SYSERR;
1930 LOG (GNUNET_ERROR_TYPE_DEBUG,
1931 "peer %s ok, has %u connections.\n",
1932 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1933 result = GNUNET_CONTAINER_multihashmap_put (peer->connections,
1936 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1937 LOG (GNUNET_ERROR_TYPE_DEBUG,
1938 " now has %u connections.\n",
1939 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1940 LOG (GNUNET_ERROR_TYPE_DEBUG, "result %u\n", result);
1947 * Add the path to the peer and update the path used to reach it in case this
1950 * @param peer Destination peer to add the path to.
1951 * @param path New path to add. Last peer must be the peer in arg 1.
1952 * Path will be either used of freed if already known.
1953 * @param trusted Do we trust that this path is real?
1955 * @return path if path was taken, pointer to existing duplicate if exists
1958 struct CadetPeerPath *
1959 GCP_add_path (struct CadetPeer *peer, struct CadetPeerPath *path,
1962 struct CadetPeerPath *aux;
1966 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1967 path->length, GCP_2s (peer));
1969 if (NULL == peer || NULL == path
1970 || path->peers[path->length - 1] != peer->id)
1973 path_destroy (path);
1977 for (l = 1; l < path->length; l++)
1979 if (path->peers[l] == myid)
1981 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1982 for (l2 = 0; l2 < path->length - l; l2++)
1984 path->peers[l2] = path->peers[l + l2];
1988 path->peers = GNUNET_realloc (path->peers,
1989 path->length * sizeof (GNUNET_PEER_Id));
1993 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1995 if (2 >= path->length && GNUNET_NO == trusted)
1997 /* Only allow CORE to tell us about direct paths */
1998 path_destroy (path);
2002 l = path_get_length (path);
2005 path_destroy (path);
2009 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2010 for (aux = peer->path_head; aux != NULL; aux = aux->next)
2012 l2 = path_get_length (aux);
2015 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
2016 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
2017 peer->path_tail, aux, path);
2022 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2024 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
2025 path_destroy (path);
2030 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
2032 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
2035 if (NULL != peer->tunnel
2036 && CONNECTIONS_PER_TUNNEL < GCT_count_connections (peer->tunnel))
2045 * Add the path to the origin peer and update the path used to reach it in case
2046 * this is the shortest.
2047 * The path is given in peer_info -> destination, therefore we turn the path
2048 * upside down first.
2050 * @param peer Peer to add the path to, being the origin of the path.
2051 * @param path New path to add after being inversed.
2052 * Path will be either used or freed.
2053 * @param trusted Do we trust that this path is real?
2055 * @return path if path was taken, pointer to existing duplicate if exists
2058 struct CadetPeerPath *
2059 GCP_add_path_to_origin (struct CadetPeer *peer,
2060 struct CadetPeerPath *path,
2066 return GCP_add_path (peer, path, trusted);
2071 * Adds a path to the info of all the peers in the path
2073 * @param p Path to process.
2074 * @param confirmed Whether we know if the path works or not.
2077 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
2081 /* TODO: invert and add */
2082 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2083 for (i++; i < p->length; i++)
2085 struct CadetPeer *aux;
2086 struct CadetPeerPath *copy;
2088 aux = GCP_get_short (p->peers[i]);
2089 copy = path_duplicate (p);
2090 copy->length = i + 1;
2091 GCP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
2097 * Remove any path to the peer that has the extact same peers as the one given.
2099 * @param peer Peer to remove the path from.
2100 * @param path Path to remove. Is always destroyed .
2103 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
2105 struct CadetPeerPath *iter;
2106 struct CadetPeerPath *next;
2108 GNUNET_assert (myid == path->peers[0]);
2109 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2111 LOG (GNUNET_ERROR_TYPE_INFO, "Removing path %p (%u) from %s\n",
2112 path, path->length, GCP_2s (peer));
2114 for (iter = peer->path_head; NULL != iter; iter = next)
2117 if (0 == path_cmp (path, iter))
2119 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
2121 path_destroy (iter);
2124 path_destroy (path);
2129 * Remove a connection from a neighboring peer.
2131 * @param peer Peer to remove connection from.
2132 * @param c Connection to remove.
2134 * @return GNUNET_OK on success.
2137 GCP_remove_connection (struct CadetPeer *peer,
2138 const struct CadetConnection *c)
2140 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing connection %s\n", GCC_2s (c));
2141 LOG (GNUNET_ERROR_TYPE_DEBUG, "from peer %s\n", GCP_2s (peer));
2143 if (NULL == peer || NULL == peer->connections)
2145 LOG (GNUNET_ERROR_TYPE_DEBUG,
2146 "Peer %s is not a neighbor!\n",
2148 return GNUNET_SYSERR;
2150 LOG (GNUNET_ERROR_TYPE_DEBUG,
2151 "peer %s ok, has %u connections.\n",
2152 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
2154 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2160 * Start the DHT search for new paths towards the peer: we don't have
2161 * enough good connections.
2163 * @param peer Destination peer.
2166 GCP_start_search (struct CadetPeer *peer)
2168 const struct GNUNET_PeerIdentity *id;
2169 struct CadetTunnel *t = peer->tunnel;
2171 if (NULL != peer->search_h)
2177 if (NULL != peer->search_delayed)
2178 GCP_stop_search (peer);
2180 id = GNUNET_PEER_resolve2 (peer->id);
2181 peer->search_h = GCD_search (id, &search_handler, peer);
2185 /* Why would we search for a peer with no tunnel towards it? */
2190 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
2191 || 0 == GCT_count_any_connections (t))
2193 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
2199 * Stop the DHT search for new paths towards the peer: we already have
2200 * enough good connections.
2202 * @param peer Destination peer.
2205 GCP_stop_search (struct CadetPeer *peer)
2207 if (NULL != peer->search_h)
2209 GCD_search_stop (peer->search_h);
2210 peer->search_h = NULL;
2212 if (NULL != peer->search_delayed)
2214 GNUNET_SCHEDULER_cancel (peer->search_delayed);
2215 peer->search_delayed = NULL;
2221 * Get the Full ID of a peer.
2223 * @param peer Peer to get from.
2225 * @return Full ID of peer.
2227 const struct GNUNET_PeerIdentity *
2228 GCP_get_id (const struct CadetPeer *peer)
2230 return GNUNET_PEER_resolve2 (peer->id);
2235 * Get the Short ID of a peer.
2237 * @param peer Peer to get from.
2239 * @return Short ID of peer.
2242 GCP_get_short_id (const struct CadetPeer *peer)
2251 * If tunnel is NULL and there was a search active, stop it, as it's useless.
2257 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2260 if (NULL == t && GNUNET_YES == is_searching (peer))
2262 GCP_stop_search (peer);
2268 * Get the tunnel towards a peer.
2270 * @param peer Peer to get from.
2272 * @return Tunnel towards peer.
2274 struct CadetTunnel *
2275 GCP_get_tunnel (const struct CadetPeer *peer)
2277 return peer->tunnel;
2282 * Set the hello message.
2284 * @param peer Peer whose message to set.
2285 * @param hello Hello message.
2288 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2290 struct GNUNET_HELLO_Message *old;
2293 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2297 old = GCP_get_hello (peer);
2300 size = GNUNET_HELLO_size (hello);
2301 LOG (GNUNET_ERROR_TYPE_DEBUG, " new (%u bytes)\n", size);
2302 peer->hello = GNUNET_malloc (size);
2303 memcpy (peer->hello, hello, size);
2307 peer->hello = GNUNET_HELLO_merge (old, hello);
2308 LOG (GNUNET_ERROR_TYPE_DEBUG, " merge into %p (%u bytes)\n",
2309 peer->hello, GNUNET_HELLO_size (hello));
2316 * Get the hello message.
2318 * @param peer Peer whose message to get.
2320 * @return Hello message.
2322 struct GNUNET_HELLO_Message *
2323 GCP_get_hello (struct CadetPeer *peer)
2325 struct GNUNET_TIME_Absolute expiration;
2326 struct GNUNET_TIME_Relative remaining;
2328 if (NULL == peer->hello)
2331 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2332 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2333 if (0 == remaining.rel_value_us)
2335 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2336 GNUNET_STRINGS_absolute_time_to_string (expiration));
2337 GNUNET_free (peer->hello);
2345 * Try to connect to a peer on TRANSPORT level.
2347 * @param peer Peer to whom to connect.
2350 GCP_try_connect (struct CadetPeer *peer)
2352 struct GNUNET_HELLO_Message *hello;
2353 struct GNUNET_MessageHeader *mh;
2355 if (NULL == transport_handle)
2358 hello = GCP_get_hello (peer);
2362 mh = GNUNET_HELLO_get_header (hello);
2363 GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
2368 * Notify a peer that a link between two other peers is broken. If any path
2369 * used that link, eliminate it.
2371 * @param peer Peer affected by the change.
2372 * @param peer1 Peer whose link is broken.
2373 * @param peer2 Peer whose link is broken.
2376 GCP_notify_broken_link (struct CadetPeer *peer,
2377 struct GNUNET_PeerIdentity *peer1,
2378 struct GNUNET_PeerIdentity *peer2)
2380 struct CadetPeerPath *iter;
2381 struct CadetPeerPath *next;
2386 p1 = GNUNET_PEER_search (peer1);
2387 p2 = GNUNET_PEER_search (peer2);
2389 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2390 if (0 == p1 || 0 == p2)
2392 /* We don't even know them */
2396 for (iter = peer->path_head; NULL != iter; iter = next)
2399 for (i = 0; i < iter->length - 1; i++)
2401 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2402 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2407 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2410 path_invalidate (iter);
2418 * Count the number of known paths toward the peer.
2420 * @param peer Peer to get path info.
2422 * @return Number of known paths.
2425 GCP_count_paths (const struct CadetPeer *peer)
2427 struct CadetPeerPath *iter;
2430 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2438 * Iterate all known peers.
2440 * @param iter Iterator.
2441 * @param cls Closure for @c iter.
2444 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2446 GNUNET_CONTAINER_multipeermap_iterate (peers, iter, cls);
2451 * Get the static string for a peer ID.
2455 * @return Static string for it's ID.
2458 GCP_2s (const struct CadetPeer *peer)
2462 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));