2 This file is part of GNUnet.
3 (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 * Tunnel to this peer, if any.
149 struct CadetTunnel *tunnel;
152 * Connections that go through this peer, indexed by tid;
154 struct GNUNET_CONTAINER_MultiHashMap *connections;
157 * Handle for queued transmissions
159 struct GNUNET_CORE_TransmitHandle *core_transmit;
162 * Transmission queue to core DLL head
164 struct CadetPeerQueue *queue_head;
167 * Transmission queue to core DLL tail
169 struct CadetPeerQueue *queue_tail;
172 * How many messages are in the queue to this peer.
174 unsigned int queue_n;
179 struct GNUNET_HELLO_Message* hello;
183 /******************************************************************************/
184 /******************************* GLOBALS ***********************************/
185 /******************************************************************************/
188 * Global handle to the statistics service.
190 extern struct GNUNET_STATISTICS_Handle *stats;
193 * Local peer own ID (full value).
195 extern struct GNUNET_PeerIdentity my_full_id;
198 * Local peer own ID (short)
200 extern GNUNET_PEER_Id myid;
203 * Peers known, indexed by PeerIdentity (CadetPeer).
205 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
208 * How many peers do we want to remember?
210 static unsigned long long max_peers;
213 * Percentage of messages that will be dropped (for test purposes only).
215 static unsigned long long drop_percent;
218 * Handle to communicate with core.
220 static struct GNUNET_CORE_Handle *core_handle;
223 * Handle to try to start new connections.
225 static struct GNUNET_TRANSPORT_Handle *transport_handle;
228 /******************************************************************************/
229 /***************************** DEBUG *********************************/
230 /******************************************************************************/
233 * Log all kinds of info about the queueing status of a peer.
235 * @param p Peer whose queue to show.
236 * @param level Error level to use for logging.
239 queue_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
241 struct CadetPeerQueue *q;
244 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
246 __FILE__, __FUNCTION__, __LINE__);
250 LOG2 (level, "QQQ Message queue towards %s\n", GCP_2s (p));
251 LOG2 (level, "QQQ queue length: %u\n", p->queue_n);
252 LOG2 (level, "QQQ core tmt rdy: %p\n", p->core_transmit);
254 for (q = p->queue_head; NULL != q; q = q->next)
256 LOG2 (level, "QQQ - %s %s on %s\n",
257 GC_m2s (q->type), GC_f2s (q->fwd), GCC_2s (q->c));
258 LOG2 (level, "QQQ payload %s, %u\n",
259 GC_m2s (q->payload_type), q->payload_id);
260 LOG2 (level, "QQQ size: %u bytes\n", q->size);
263 LOG2 (level, "QQQ End queue towards %s\n", GCP_2s (p));
268 * Log all kinds of info about a peer.
273 GCP_debug (const struct CadetPeer *p, enum GNUNET_ErrorType level)
275 struct CadetPeerPath *path;
279 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
281 __FILE__, __FUNCTION__, __LINE__);
287 LOG2 (level, "PPP DEBUG PEER NULL\n");
291 LOG2 (level, "PPP DEBUG PEER %s\n", GCP_2s (p));
292 LOG2 (level, "PPP last contact %s\n",
293 GNUNET_STRINGS_absolute_time_to_string (p->last_contact));
294 for (path = p->path_head; NULL != path; path = path->next)
299 LOG2 (level, "PPP path: %s\n", s);
303 LOG2 (level, "PPP core transmit handle %p\n", p->core_transmit);
304 LOG2 (level, "PPP DHT GET handle %p\n", p->search_h);
305 if (NULL != p->connections)
306 conns = GNUNET_CONTAINER_multihashmap_size (p->connections);
309 LOG2 (level, "PPP # connections over link to peer: %u\n", conns);
310 queue_debug (p, level);
311 LOG2 (level, "PPP DEBUG END\n");
315 /******************************************************************************/
316 /***************************** CORE HELPERS *********************************/
317 /******************************************************************************/
321 * Iterator to notify all connections of a broken link. Mark connections
322 * to destroy after all traffic has been sent.
324 * @param cls Closure (peer disconnected).
325 * @param key Current key code (peer id).
326 * @param value Value in the hash map (connection).
328 * @return #GNUNET_YES to continue to iterate.
331 notify_broken (void *cls,
332 const struct GNUNET_HashCode *key,
335 struct CadetPeer *peer = cls;
336 struct CadetConnection *c = value;
338 LOG (GNUNET_ERROR_TYPE_DEBUG, " notifying %s due to %s\n",
339 GCC_2s (c), GCP_2s (peer));
340 GCC_notify_broken (c, peer);
347 * Remove the direct path to the peer.
349 * @param peer Peer to remove the direct path from.
352 static struct CadetPeerPath *
353 pop_direct_path (struct CadetPeer *peer)
355 struct CadetPeerPath *iter;
357 for (iter = peer->path_head; NULL != iter; iter = iter->next)
359 if (2 <= iter->length)
361 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
369 /******************************************************************************/
370 /***************************** CORE CALLBACKS *********************************/
371 /******************************************************************************/
374 * Method called whenever a given peer connects.
377 * @param peer peer identity this notification is about
380 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
382 struct CadetPeer *mp;
383 struct CadetPeerPath *path;
386 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
390 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s (self)\n", own_id);
395 LOG (GNUNET_ERROR_TYPE_INFO, "CONNECTED %s <= %s\n",
396 own_id, GNUNET_i2s (peer));
398 path->peers[1] = mp->id;
399 GNUNET_PEER_change_rc (mp->id, 1);
400 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
402 path->peers[0] = myid;
403 GNUNET_PEER_change_rc (myid, 1);
404 GCP_add_path (mp, path, GNUNET_YES);
406 mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
408 if (NULL != GCP_get_tunnel (mp) &&
409 0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, peer))
419 * Method called whenever a peer disconnects.
422 * @param peer peer identity this notification is about
425 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
428 struct CadetPeerPath *direct_path;
431 strncpy (own_id, GNUNET_i2s (&my_full_id), 15);
432 p = GNUNET_CONTAINER_multipeermap_get (peers, peer);
439 LOG (GNUNET_ERROR_TYPE_INFO, "DISCONNECTED %s (self)\n", own_id);
441 LOG (GNUNET_ERROR_TYPE_INFO, "DISCONNECTED %s <= %s\n",
442 own_id, GNUNET_i2s (peer));
443 direct_path = pop_direct_path (p);
444 GNUNET_CONTAINER_multihashmap_iterate (p->connections, ¬ify_broken, p);
445 GNUNET_CONTAINER_multihashmap_destroy (p->connections);
446 p->connections = NULL;
447 if (NULL != p->core_transmit)
449 GNUNET_CORE_notify_transmit_ready_cancel (p->core_transmit);
450 p->core_transmit = NULL;
452 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
454 path_destroy (direct_path);
460 * Functions to handle messages from core
462 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
463 {&GCC_handle_create, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0},
464 {&GCC_handle_confirm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK,
465 sizeof (struct GNUNET_CADET_ConnectionACK)},
466 {&GCC_handle_broken, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
467 sizeof (struct GNUNET_CADET_ConnectionBroken)},
468 {&GCC_handle_destroy, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
469 sizeof (struct GNUNET_CADET_ConnectionDestroy)},
470 {&GCC_handle_ack, GNUNET_MESSAGE_TYPE_CADET_ACK,
471 sizeof (struct GNUNET_CADET_ACK)},
472 {&GCC_handle_poll, GNUNET_MESSAGE_TYPE_CADET_POLL,
473 sizeof (struct GNUNET_CADET_Poll)},
474 {&GCC_handle_encrypted, GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 0},
475 {&GCC_handle_kx, GNUNET_MESSAGE_TYPE_CADET_KX, 0},
481 * To be called on core init/fail.
483 * @param cls Closure (config)
484 * @param identity the public identity of this peer
487 core_init (void *cls,
488 const struct GNUNET_PeerIdentity *identity)
490 const struct GNUNET_CONFIGURATION_Handle *c = cls;
493 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
494 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
496 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
497 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
498 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
499 GNUNET_CORE_disconnect (core_handle);
500 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
501 NULL, /* Closure passed to CADET functions */
502 &core_init, /* Call core_init once connected */
503 &core_connect, /* Handle connects */
504 &core_disconnect, /* remove peers on disconnects */
505 NULL, /* Don't notify about all incoming messages */
506 GNUNET_NO, /* For header only in notification */
507 NULL, /* Don't notify about all outbound messages */
508 GNUNET_NO, /* For header-only out notification */
509 core_handlers); /* Register these handlers */
519 * Core callback to write a pre-constructed data packet to core buffer
521 * @param cls Closure (CadetTransmissionDescriptor with data in "data" member).
522 * @param size Number of bytes available in buf.
523 * @param buf Where the to write the message.
525 * @return number of bytes written to buf
528 send_core_data_raw (void *cls, size_t size, void *buf)
530 struct GNUNET_MessageHeader *msg = cls;
533 GNUNET_assert (NULL != msg);
534 total_size = ntohs (msg->size);
536 if (total_size > size)
541 memcpy (buf, msg, total_size);
548 * Function to send a create connection message to a peer.
550 * @param c Connection to create.
551 * @param size number of bytes available in buf
552 * @param buf where the callee should write the message
553 * @return number of bytes written to buf
556 send_core_connection_create (struct CadetConnection *c, size_t size, void *buf)
558 struct GNUNET_CADET_ConnectionCreate *msg;
559 struct GNUNET_PeerIdentity *peer_ptr;
560 const struct CadetPeerPath *p = GCC_get_path (c);
567 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
569 sizeof (struct GNUNET_CADET_ConnectionCreate) +
570 p->length * sizeof (struct GNUNET_PeerIdentity);
572 if (size < size_needed || NULL == buf)
577 msg = (struct GNUNET_CADET_ConnectionCreate *) buf;
578 msg->header.size = htons (size_needed);
579 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
580 msg->cid = *GCC_get_id (c);
582 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
583 for (i = 0; i < p->length; i++)
585 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
588 LOG (GNUNET_ERROR_TYPE_DEBUG,
589 "CONNECTION CREATE (%u bytes long) sent!\n",
596 * Creates a path ack message in buf and frees all unused resources.
598 * @param c Connection to send an ACK on.
599 * @param size number of bytes available in buf
600 * @param buf where the callee should write the message
602 * @return number of bytes written to buf
605 send_core_connection_ack (struct CadetConnection *c, size_t size, void *buf)
607 struct GNUNET_CADET_ConnectionACK *msg = buf;
609 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
610 if (sizeof (struct GNUNET_CADET_ConnectionACK) > size)
615 msg->header.size = htons (sizeof (struct GNUNET_CADET_ConnectionACK));
616 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK);
617 msg->cid = *GCC_get_id (c);
619 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
620 return sizeof (struct GNUNET_CADET_ConnectionACK);
624 /******************************************************************************/
625 /******************************** STATIC ***********************************/
626 /******************************************************************************/
630 * Get priority for a queued message.
632 * @param q Queued message
634 * @return CORE priority to use.
636 static enum GNUNET_CORE_Priority
637 get_priority (struct CadetPeerQueue *q)
639 enum GNUNET_CORE_Priority low;
640 enum GNUNET_CORE_Priority high;
645 return GNUNET_CORE_PRIO_BACKGROUND;
648 /* Relayed traffic has lower priority, our own traffic has higher */
649 if (NULL == q->c || GNUNET_NO == GCC_is_origin (q->c, q->fwd))
651 low = GNUNET_CORE_PRIO_BEST_EFFORT;
652 high = GNUNET_CORE_PRIO_URGENT;
656 low = GNUNET_CORE_PRIO_URGENT;
657 high = GNUNET_CORE_PRIO_CRITICAL_CONTROL;
660 /* Bulky payload has lower priority, control traffic has higher. */
661 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type)
669 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
672 * @param key current key code
673 * @param value value in the hash map
674 * @return #GNUNET_YES if we should continue to iterate,
678 shutdown_tunnel (void *cls,
679 const struct GNUNET_PeerIdentity *key,
682 struct CadetPeer *p = value;
683 struct CadetTunnel *t = p->tunnel;
692 * Destroy the peer_info and free any allocated resources linked to it
694 * @param peer The peer_info to destroy.
696 * @return GNUNET_OK on success
699 peer_destroy (struct CadetPeer *peer)
701 struct GNUNET_PeerIdentity id;
702 struct CadetPeerPath *p;
703 struct CadetPeerPath *nextp;
705 GNUNET_PEER_resolve (peer->id, &id);
706 GNUNET_PEER_change_rc (peer->id, -1);
708 LOG (GNUNET_ERROR_TYPE_WARNING, "destroying peer %s\n", GNUNET_i2s (&id));
711 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
714 LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n");
716 if (NULL != peer->search_h)
718 GCD_search_stop (peer->search_h);
724 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
728 GCT_destroy_empty (peer->tunnel);
735 * Returns if peer is used (has a tunnel or is neighbor).
737 * @param peer Peer to check.
739 * @return #GNUNET_YES if peer is in use.
742 peer_is_used (struct CadetPeer *peer)
744 struct CadetPeerPath *p;
746 if (NULL != peer->tunnel)
749 for (p = peer->path_head; NULL != p; p = p->next)
759 * Iterator over all the peers to get the oldest timestamp.
761 * @param cls Closure (unsued).
762 * @param key ID of the peer.
763 * @param value Peer_Info of the peer.
766 peer_get_oldest (void *cls,
767 const struct GNUNET_PeerIdentity *key,
770 struct CadetPeer *p = value;
771 struct GNUNET_TIME_Absolute *abs = cls;
773 /* Don't count active peers */
774 if (GNUNET_YES == peer_is_used (p))
777 if (abs->abs_value_us < p->last_contact.abs_value_us)
778 abs->abs_value_us = p->last_contact.abs_value_us;
785 * Iterator over all the peers to remove the oldest entry.
787 * @param cls Closure (unsued).
788 * @param key ID of the peer.
789 * @param value Peer_Info of the peer.
792 peer_timeout (void *cls,
793 const struct GNUNET_PeerIdentity *key,
796 struct CadetPeer *p = value;
797 struct GNUNET_TIME_Absolute *abs = cls;
799 LOG (GNUNET_ERROR_TYPE_WARNING,
800 "peer %s timeout\n", GNUNET_i2s (key));
802 if (p->last_contact.abs_value_us == abs->abs_value_us &&
803 GNUNET_NO == peer_is_used (p))
813 * Delete oldest unused peer.
816 peer_delete_oldest (void)
818 struct GNUNET_TIME_Absolute abs;
820 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
822 GNUNET_CONTAINER_multipeermap_iterate (peers,
825 GNUNET_CONTAINER_multipeermap_iterate (peers,
832 * Choose the best (yet unused) path towards a peer,
833 * considering the tunnel properties.
835 * @param peer The destination peer.
837 * @return Best current known path towards the peer, if any.
839 static struct CadetPeerPath *
840 peer_get_best_path (const struct CadetPeer *peer)
842 struct CadetPeerPath *best_p;
843 struct CadetPeerPath *p;
844 unsigned int best_cost;
847 best_cost = UINT_MAX;
849 for (p = peer->path_head; NULL != p; p = p->next)
851 if (GNUNET_NO == path_is_valid (p))
852 continue; /* Don't use invalid paths. */
853 if (GNUNET_YES == GCT_is_path_used (peer->tunnel, p))
854 continue; /* If path is already in use, skip it. */
856 if ((cost = GCT_get_path_cost (peer->tunnel, p)) < best_cost)
867 * Is this queue element sendable?
869 * - All management traffic is always sendable.
870 * - For payload traffic, check the connection flow control.
872 * @param q Queue element to inspect.
874 * @return #GNUNET_YES if it is sendable, #GNUNET_NO otherwise.
877 queue_is_sendable (struct CadetPeerQueue *q)
879 /* Is PID-independent? */
882 case GNUNET_MESSAGE_TYPE_CADET_ACK:
883 case GNUNET_MESSAGE_TYPE_CADET_POLL:
884 case GNUNET_MESSAGE_TYPE_CADET_KX:
885 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
886 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
887 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
888 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
891 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
898 return GCC_is_sendable (q->c, q->fwd);
903 * Get first sendable message.
905 * @param peer The destination peer.
907 * @return First transmittable message, if any. Otherwise, NULL.
909 static struct CadetPeerQueue *
910 peer_get_first_message (const struct CadetPeer *peer)
912 struct CadetPeerQueue *q;
914 for (q = peer->queue_head; NULL != q; q = q->next)
916 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking %p towards %s\n", q, GCC_2s (q->c));
917 if (queue_is_sendable (q))
926 * Function to process paths received for a new peer addition. The recorded
927 * paths form the initial tunnel, which can be optimized later.
928 * Called on each result obtained for the DHT search.
934 search_handler (void *cls, const struct CadetPeerPath *path)
936 struct CadetPeer *peer = cls;
937 unsigned int connection_count;
939 GCP_add_path_to_all (path, GNUNET_NO);
941 /* Count connections */
942 connection_count = GCT_count_connections (peer->tunnel);
944 /* If we already have 3 (or more (?!)) connections, it's enough */
945 if (3 <= connection_count)
948 if (CADET_TUNNEL_SEARCHING == GCT_get_cstate (peer->tunnel))
950 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
959 * Core callback to write a queued packet to core buffer
961 * @param cls Closure (peer info).
962 * @param size Number of bytes available in buf.
963 * @param buf Where the to write the message.
965 * @return number of bytes written to buf
968 queue_send (void *cls, size_t size, void *buf)
970 struct CadetPeer *peer = cls;
971 struct CadetConnection *c;
972 struct CadetPeerQueue *queue;
973 const struct GNUNET_PeerIdentity *dst_id;
978 peer->core_transmit = NULL;
979 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue send towards %s (max %u)\n",
980 GCP_2s (peer), size);
982 if (NULL == buf || 0 == size)
984 LOG (GNUNET_ERROR_TYPE_DEBUG, "Buffer size 0.\n");
989 queue = peer_get_first_message (peer);
992 GNUNET_assert (0); /* Core tmt_rdy should've been canceled */
997 dst_id = GNUNET_PEER_resolve2 (peer->id);
998 LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s %s\n",
999 GCC_2s (c), GC_f2s(queue->fwd));
1000 /* Check if buffer size is enough for the message */
1001 if (queue->size > size)
1003 LOG (GNUNET_ERROR_TYPE_WARNING, "not enough room (%u vs %u), reissue\n",
1005 peer->core_transmit =
1006 GNUNET_CORE_notify_transmit_ready (core_handle,
1007 GNUNET_NO, get_priority (queue),
1008 GNUNET_TIME_UNIT_FOREVER_REL,
1015 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u ok\n", queue->size);
1018 switch (queue->type)
1020 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1021 pid = GCC_get_pid (queue->c, queue->fwd);
1022 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload ID %u\n", pid);
1023 data_size = send_core_data_raw (queue->cls, size, buf);
1024 ((struct GNUNET_CADET_Encrypted *) buf)->pid = htonl (pid);
1026 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1027 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1028 case GNUNET_MESSAGE_TYPE_CADET_KX:
1029 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1030 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1031 LOG (GNUNET_ERROR_TYPE_DEBUG, " raw %s\n", GC_m2s (queue->type));
1032 data_size = send_core_data_raw (queue->cls, size, buf);
1034 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1035 LOG (GNUNET_ERROR_TYPE_DEBUG, " path create\n");
1036 if (GCC_is_origin (c, GNUNET_YES))
1037 data_size = send_core_connection_create (c, size, buf);
1039 data_size = send_core_data_raw (queue->cls, size, buf);
1041 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1042 LOG (GNUNET_ERROR_TYPE_DEBUG, " path ack\n");
1043 if (GCC_is_origin (c, GNUNET_NO) ||
1044 GCC_is_origin (c, GNUNET_YES))
1045 data_size = send_core_connection_ack (c, size, buf);
1047 data_size = send_core_data_raw (queue->cls, size, buf);
1049 case GNUNET_MESSAGE_TYPE_CADET_DATA:
1050 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1051 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1052 /* This should be encapsulted */
1058 LOG (GNUNET_ERROR_TYPE_WARNING, " type unknown: %u\n", queue->type);
1062 if (0 < drop_percent &&
1063 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
1065 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on connection %s %s\n",
1066 GC_m2s (queue->type), GC_m2s (queue->payload_type), queue->payload_id,
1067 GCC_2s (c), GC_f2s (queue->fwd));
1072 LOG (GNUNET_ERROR_TYPE_INFO,
1073 "snd %s (%s %u) on connection %s (%p) %s (size %u)\n",
1074 GC_m2s (queue->type), GC_m2s (queue->payload_type),
1075 queue->payload_id, GCC_2s (c), c, GC_f2s (queue->fwd), data_size);
1078 /* Free queue, but cls was freed by send_core_*. */
1079 (void) GCP_queue_destroy (queue, GNUNET_NO, GNUNET_YES, pid);
1081 /* If more data in queue, send next */
1082 queue = peer_get_first_message (peer);
1085 LOG (GNUNET_ERROR_TYPE_DEBUG, " more data!\n");
1086 if (NULL == peer->core_transmit)
1088 peer->core_transmit =
1089 GNUNET_CORE_notify_transmit_ready (core_handle,
1090 GNUNET_NO, get_priority (queue),
1091 GNUNET_TIME_UNIT_FOREVER_REL,
1096 queue->start_waiting = GNUNET_TIME_absolute_get ();
1100 LOG (GNUNET_ERROR_TYPE_DEBUG,
1101 "* tmt rdy called somewhere else\n");
1103 // GCC_start_poll (); FIXME needed?
1107 // GCC_stop_poll(); FIXME needed?
1110 LOG (GNUNET_ERROR_TYPE_DEBUG, " return %d\n", data_size);
1111 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1116 /******************************************************************************/
1117 /******************************** API ***********************************/
1118 /******************************************************************************/
1122 * Free a transmission that was already queued with all resources
1123 * associated to the request.
1125 * If connection was marked to be destroyed, and this was the last queued
1126 * message on it, the connection will be free'd as a result.
1128 * @param queue Queue handler to cancel.
1129 * @param clear_cls Is it necessary to free associated cls?
1130 * @param sent Was it really sent? (Could have been canceled)
1131 * @param pid PID, if relevant (was sent and was a payload message).
1133 * @return #GNUNET_YES if connection was destroyed as a result,
1134 * #GNUNET_NO otherwise.
1137 GCP_queue_destroy (struct CadetPeerQueue *queue, int clear_cls,
1138 int sent, uint32_t pid)
1140 struct CadetPeer *peer;
1141 int connection_destroyed;
1144 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue destroy %s\n", GC_m2s (queue->type));
1145 if (GNUNET_YES == clear_cls)
1147 LOG (GNUNET_ERROR_TYPE_DEBUG, " free cls\n");
1148 switch (queue->type)
1150 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1151 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1153 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1154 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1155 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1156 case GNUNET_MESSAGE_TYPE_CADET_KX:
1157 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1158 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1159 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1160 GNUNET_free_non_null (queue->cls);
1165 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1166 GC_m2s (queue->type));
1169 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1171 if (queue->type != GNUNET_MESSAGE_TYPE_CADET_ACK &&
1172 queue->type != GNUNET_MESSAGE_TYPE_CADET_POLL)
1177 if (NULL != queue->callback)
1179 struct GNUNET_TIME_Relative core_wait_time;
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling callback\n");
1182 core_wait_time = GNUNET_TIME_absolute_get_duration (queue->start_waiting);
1183 connection_destroyed = queue->callback (queue->callback_cls,
1184 queue->c, sent, queue->type, pid,
1185 queue->fwd, queue->size,
1190 connection_destroyed = GNUNET_NO;
1193 if (NULL == peer_get_first_message (peer) && NULL != peer->core_transmit)
1195 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1196 peer->core_transmit = NULL;
1199 GNUNET_free (queue);
1200 return connection_destroyed;
1205 * @brief Queue and pass message to core when possible.
1207 * @param peer Peer towards which to queue the message.
1208 * @param cls Closure (@c type dependant). It will be used by queue_send to
1209 * build the message to be sent if not already prebuilt.
1210 * @param type Type of the message, 0 for a raw message.
1211 * @param size Size of the message.
1212 * @param c Connection this message belongs to (can be NULL).
1213 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1214 * @param cont Continuation to be called once CORE has taken the message.
1215 * @param cont_cls Closure for @c cont.
1217 * @return Handle to cancel the message before it is sent. Once cont is called
1218 * message has been sent and therefore the handle is no longer valid.
1220 struct CadetPeerQueue *
1221 GCP_queue_add (struct CadetPeer *peer, void *cls, uint16_t type,
1222 uint16_t payload_type, uint32_t payload_id, size_t size,
1223 struct CadetConnection *c, int fwd,
1224 GCP_sent cont, void *cont_cls)
1226 struct CadetPeerQueue *q;
1231 if (NULL == c && GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN != type)
1232 error_level = GNUNET_ERROR_TYPE_ERROR;
1234 error_level = GNUNET_ERROR_TYPE_INFO;
1236 "que %s (%s %u) on connection %s (%p) %s towards %s (size %u)\n",
1237 GC_m2s (type), GC_m2s (payload_type), payload_id,
1238 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1240 if (error_level == GNUNET_ERROR_TYPE_ERROR)
1242 if (NULL == peer->connections)
1244 /* We are not connected to this peer, ignore request. */
1245 LOG (GNUNET_ERROR_TYPE_WARNING, "%s not a neighbor\n", GCP_2s (peer));
1246 GNUNET_STATISTICS_update (stats, "# messages dropped due to wrong hop", 1,
1253 if (GNUNET_MESSAGE_TYPE_CADET_POLL == type ||
1254 GNUNET_MESSAGE_TYPE_CADET_ACK == type)
1259 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1261 call_core = (NULL == c || type == GNUNET_MESSAGE_TYPE_CADET_KX) ?
1262 GNUNET_YES : GCC_is_sendable (c, fwd);
1263 q = GNUNET_new (struct CadetPeerQueue);
1266 q->payload_type = payload_type;
1267 q->payload_id = payload_id;
1273 q->callback_cls = cont_cls;
1276 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, q);
1281 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, q);
1282 call_core = GNUNET_YES;
1285 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1287 LOG (GNUNET_ERROR_TYPE_DEBUG,
1288 "calling core tmt rdy towards %s for %u bytes\n",
1289 GCP_2s (peer), size);
1290 peer->core_transmit =
1291 GNUNET_CORE_notify_transmit_ready (core_handle,
1292 GNUNET_NO, get_priority (q),
1293 GNUNET_TIME_UNIT_FOREVER_REL,
1294 GNUNET_PEER_resolve2 (peer->id),
1295 size, &queue_send, peer);
1296 q->start_waiting = GNUNET_TIME_absolute_get ();
1298 else if (GNUNET_NO == call_core)
1300 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s not needed\n",
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, "core tmt rdy towards %s already called\n",
1310 queue_debug (peer, GNUNET_ERROR_TYPE_DEBUG);
1316 * Cancel all queued messages to a peer that belong to a certain connection.
1318 * @param peer Peer towards whom to cancel.
1319 * @param c Connection whose queued messages to cancel. Might be destroyed by
1320 * the sent continuation call.
1323 GCP_queue_cancel (struct CadetPeer *peer, struct CadetConnection *c)
1325 struct CadetPeerQueue *q;
1326 struct CadetPeerQueue *next;
1327 struct CadetPeerQueue *prev;
1328 int connection_destroyed;
1330 connection_destroyed = GNUNET_NO;
1331 for (q = peer->queue_head; NULL != q; q = next)
1336 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMP queue cancel %s\n", GC_m2s (q->type));
1337 GNUNET_break (GNUNET_NO == connection_destroyed);
1338 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY == q->type)
1344 connection_destroyed = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1347 /* Get next from prev, q->next might be already freed:
1348 * queue destroy -> callback -> GCC_destroy -> cancel_queues -> here
1351 next = peer->queue_head;
1361 if (NULL == peer->queue_head && NULL != peer->core_transmit)
1363 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1364 peer->core_transmit = NULL;
1370 * Get the first transmittable message for a connection.
1372 * @param peer Neighboring peer.
1373 * @param c Connection.
1375 * @return First transmittable message.
1377 static struct CadetPeerQueue *
1378 connection_get_first_message (struct CadetPeer *peer, struct CadetConnection *c)
1380 struct CadetPeerQueue *q;
1382 for (q = peer->queue_head; NULL != q; q = q->next)
1386 if (queue_is_sendable (q))
1388 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable!!\n");
1391 LOG (GNUNET_ERROR_TYPE_DEBUG, " not sendable\n");
1399 * Get the first message for a connection and unqueue it.
1401 * Only tunnel (or higher) level messages are unqueued. Connection specific
1402 * messages are silently destroyed upon encounter.
1404 * @param peer Neighboring peer.
1405 * @param c Connection.
1406 * @param destroyed[in/out] Was the connection destroyed (prev/as a result)?.
1408 * @return First message for this connection.
1410 struct GNUNET_MessageHeader *
1411 GCP_connection_pop (struct CadetPeer *peer,
1412 struct CadetConnection *c,
1415 struct CadetPeerQueue *q;
1416 struct CadetPeerQueue *next;
1417 struct GNUNET_MessageHeader *msg;
1420 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection pop on connection %p\n", c);
1421 for (q = peer->queue_head; NULL != q; q = next)
1423 GNUNET_break (NULL == destroyed || GNUNET_NO == *destroyed);
1427 LOG (GNUNET_ERROR_TYPE_DEBUG, " - queued: %s (%s %u), callback: %p\n",
1428 GC_m2s (q->type), GC_m2s (q->payload_type), q->payload_id,
1432 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1433 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
1434 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1435 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1436 case GNUNET_MESSAGE_TYPE_CADET_ACK:
1437 case GNUNET_MESSAGE_TYPE_CADET_POLL:
1438 dest = GCP_queue_destroy (q, GNUNET_YES, GNUNET_NO, 0);
1439 if (NULL != destroyed && GNUNET_YES == dest)
1440 *destroyed = GNUNET_YES;
1443 case GNUNET_MESSAGE_TYPE_CADET_KX:
1444 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
1445 msg = (struct GNUNET_MessageHeader *) q->cls;
1446 dest = GCP_queue_destroy (q, GNUNET_NO, GNUNET_NO, 0);
1447 if (NULL != destroyed && GNUNET_YES == dest)
1448 *destroyed = GNUNET_YES;
1453 LOG (GNUNET_ERROR_TYPE_DEBUG, "Unknown message %s\n", GC_m2s (q->type));
1461 * Unlock a possibly locked queue for a connection.
1463 * If there is a message that can be sent on this connection, call core for it.
1464 * Otherwise (if core transmit is already called or there is no sendable
1465 * message) do nothing.
1467 * @param peer Peer who keeps the queue.
1468 * @param c Connection whose messages to unlock.
1471 GCP_queue_unlock (struct CadetPeer *peer, struct CadetConnection *c)
1473 struct CadetPeerQueue *q;
1476 if (NULL != peer->core_transmit)
1478 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1479 return; /* Already unlocked */
1482 q = connection_get_first_message (peer, c);
1485 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1486 return; /* Nothing to transmit */
1490 peer->core_transmit =
1491 GNUNET_CORE_notify_transmit_ready (core_handle,
1492 GNUNET_NO, get_priority (q),
1493 GNUNET_TIME_UNIT_FOREVER_REL,
1494 GNUNET_PEER_resolve2 (peer->id),
1502 * Initialize the peer subsystem.
1504 * @param c Configuration.
1507 GCP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1509 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1510 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1512 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "MAX_PEERS",
1515 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1516 "CADET", "MAX_PEERS", "USING DEFAULT");
1521 GNUNET_CONFIGURATION_get_value_number (c, "CADET", "DROP_PERCENT",
1528 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1529 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1530 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1531 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1532 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1535 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1536 NULL, /* Closure passed to CADET functions */
1537 &core_init, /* Call core_init once connected */
1538 &core_connect, /* Handle connects */
1539 &core_disconnect, /* remove peers on disconnects */
1540 NULL, /* Don't notify about all incoming messages */
1541 GNUNET_NO, /* For header only in notification */
1542 NULL, /* Don't notify about all outbound messages */
1543 GNUNET_NO, /* For header-only out notification */
1544 core_handlers); /* Register these handlers */
1546 GNUNET_CONFIGURATION_get_value_yesno (c, "CADET", "DISABLE_TRY_CONNECT"))
1548 transport_handle = GNUNET_TRANSPORT_connect (c, &my_full_id, NULL, /* cls */
1549 /* Notify callbacks */
1554 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1555 LOG (GNUNET_ERROR_TYPE_WARNING, "* DISABLE TRYING CONNECT in config *\n");
1556 LOG (GNUNET_ERROR_TYPE_WARNING, "* Use this only for test purposes. *\n");
1557 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1558 transport_handle = NULL;
1563 if (NULL == core_handle)
1566 GNUNET_SCHEDULER_shutdown ();
1574 * Shut down the peer subsystem.
1579 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1581 if (core_handle != NULL)
1583 GNUNET_CORE_disconnect (core_handle);
1586 if (transport_handle != NULL)
1588 GNUNET_TRANSPORT_disconnect (transport_handle);
1589 transport_handle = NULL;
1591 GNUNET_PEER_change_rc (myid, -1);
1596 * Retrieve the CadetPeer stucture associated with the peer, create one
1597 * and insert it in the appropriate structures if the peer is not known yet.
1599 * @param peer_id Full identity of the peer.
1601 * @return Existing or newly created peer structure.
1604 GCP_get (const struct GNUNET_PeerIdentity *peer_id)
1606 struct CadetPeer *peer;
1608 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1611 peer = GNUNET_new (struct CadetPeer);
1612 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1614 peer_delete_oldest ();
1616 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1617 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1618 peer->id = GNUNET_PEER_intern (peer_id);
1620 peer->last_contact = GNUNET_TIME_absolute_get ();
1627 * Retrieve the CadetPeer stucture associated with the peer, create one
1628 * and insert it in the appropriate structures if the peer is not known yet.
1630 * @param peer Short identity of the peer.
1632 * @return Existing or newly created peer structure.
1635 GCP_get_short (const GNUNET_PEER_Id peer)
1637 return GCP_get (GNUNET_PEER_resolve2 (peer));
1642 * Try to connect to a peer on transport level.
1644 * @param cls Closure (peer).
1645 * @param tc TaskContext.
1648 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1650 struct CadetPeer *peer = cls;
1652 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1655 GNUNET_TRANSPORT_try_connect (transport_handle,
1656 GNUNET_PEER_resolve2 (peer->id), NULL, NULL);
1661 * Try to establish a new connection to this peer (in its tunnel).
1662 * If the peer doesn't have any path to it yet, try to get one.
1663 * If the peer already has some path, send a CREATE CONNECTION towards it.
1665 * @param peer Peer to connect to.
1668 GCP_connect (struct CadetPeer *peer)
1670 struct CadetTunnel *t;
1671 struct CadetPeerPath *p;
1672 struct CadetConnection *c;
1675 LOG (GNUNET_ERROR_TYPE_DEBUG, "peer_connect towards %s\n", GCP_2s (peer));
1677 /* If we have a current hello, try to connect using it. */
1678 GCP_try_connect (peer);
1682 rerun_search = GNUNET_NO;
1684 if (NULL != peer->path_head)
1686 LOG (GNUNET_ERROR_TYPE_DEBUG, " some path exists\n");
1687 p = peer_get_best_path (peer);
1693 LOG (GNUNET_ERROR_TYPE_DEBUG, " path to use: %s\n", s);
1696 c = GCT_use_path (t, p);
1699 /* This case can happen when the path includes a first hop that is
1700 * not yet known to be connected.
1702 * This happens quite often during testing when running cadet
1703 * under valgrind: core connect notifications come very late and the
1704 * DHT result has already come and created a valid path.
1705 * In this case, the peer->connections hashmap will be NULL and
1706 * tunnel_use_path will not be able to create a connection from that
1709 * Re-running the DHT GET should give core time to callback.
1711 * GCT_use_path -> GCC_new -> register_neighbors takes care of
1712 * updating statistics about this issue.
1714 rerun_search = GNUNET_YES;
1718 GCC_send_create (c);
1724 LOG (GNUNET_ERROR_TYPE_DEBUG, " but is NULL, all paths are in use\n");
1728 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1730 GCD_search_stop (peer->search_h);
1731 peer->search_h = NULL;
1732 LOG (GNUNET_ERROR_TYPE_DEBUG, " Stopping DHT GET for peer %s\n",
1736 if (NULL == peer->search_h)
1738 const struct GNUNET_PeerIdentity *id;
1740 id = GNUNET_PEER_resolve2 (peer->id);
1741 LOG (GNUNET_ERROR_TYPE_DEBUG,
1742 " Starting DHT GET for peer %s\n", GCP_2s (peer));
1743 peer->search_h = GCD_search (id, &search_handler, peer);
1744 if (CADET_TUNNEL_NEW == GCT_get_cstate (t)
1745 || 0 == GCT_count_any_connections (t))
1746 GCT_change_cstate (t, CADET_TUNNEL_SEARCHING);
1752 * Chech whether there is a direct (core level) connection to peer.
1754 * @param peer Peer to check.
1756 * @return #GNUNET_YES if there is a direct connection.
1759 GCP_is_neighbor (const struct CadetPeer *peer)
1761 struct CadetPeerPath *path;
1763 if (NULL == peer->connections)
1766 for (path = peer->path_head; NULL != path; path = path->next)
1768 if (3 > path->length)
1772 /* Is not a neighbor but connections is not NULL, probably disconnecting */
1778 * Create and initialize a new tunnel towards a peer, in case it has none.
1779 * In case the peer already has a tunnel, nothing is done.
1781 * Does not generate any traffic, just creates the local data structures.
1783 * @param peer Peer towards which to create the tunnel.
1786 GCP_add_tunnel (struct CadetPeer *peer)
1788 if (NULL != peer->tunnel)
1790 peer->tunnel = GCT_new (peer);
1795 * Add a connection to a neighboring peer.
1797 * Store that the peer is the first hop of the connection in one
1798 * direction and that on peer disconnect the connection must be
1799 * notified and destroyed, for it will no longer be valid.
1801 * @param peer Peer to add connection to.
1802 * @param c Connection to add.
1804 * @return GNUNET_OK on success.
1807 GCP_add_connection (struct CadetPeer *peer,
1808 struct CadetConnection *c)
1811 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding connection %s\n", GCC_2s (c));
1812 LOG (GNUNET_ERROR_TYPE_DEBUG, "to peer %s\n", GCP_2s (peer));
1814 if (NULL == peer->connections)
1817 LOG (GNUNET_ERROR_TYPE_DEBUG,
1818 "Peer %s is not a neighbor!\n",
1820 return GNUNET_SYSERR;
1822 LOG (GNUNET_ERROR_TYPE_DEBUG,
1823 "peer %s ok, has %u connections.\n",
1824 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
1825 result = GNUNET_CONTAINER_multihashmap_put (peer->connections,
1828 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1829 LOG (GNUNET_ERROR_TYPE_DEBUG,
1830 " now has %u connections.\n",
1831 GNUNET_CONTAINER_multihashmap_size (peer->connections));
1832 LOG (GNUNET_ERROR_TYPE_DEBUG, "result %u\n", result);
1839 * Add the path to the peer and update the path used to reach it in case this
1842 * @param peer Destination peer to add the path to.
1843 * @param path New path to add. Last peer must be the peer in arg 1.
1844 * Path will be either used of freed if already known.
1845 * @param trusted Do we trust that this path is real?
1847 * @return path if path was taken, pointer to existing duplicate if exists
1850 struct CadetPeerPath *
1851 GCP_add_path (struct CadetPeer *peer, struct CadetPeerPath *path,
1854 struct CadetPeerPath *aux;
1858 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1859 path->length, GCP_2s (peer));
1861 if (NULL == peer || NULL == path
1862 || path->peers[path->length - 1] != peer->id)
1865 path_destroy (path);
1869 for (l = 1; l < path->length; l++)
1871 if (path->peers[l] == myid)
1873 LOG (GNUNET_ERROR_TYPE_DEBUG, " shortening path by %u\n", l);
1874 for (l2 = 0; l2 < path->length - l; l2++)
1876 path->peers[l2] = path->peers[l + l2];
1880 path->peers = GNUNET_realloc (path->peers,
1881 path->length * sizeof (GNUNET_PEER_Id));
1885 LOG (GNUNET_ERROR_TYPE_DEBUG, " final length: %u\n", path->length);
1887 if (2 >= path->length && GNUNET_NO == trusted)
1889 /* Only allow CORE to tell us about direct paths */
1890 path_destroy (path);
1894 l = path_get_length (path);
1897 path_destroy (path);
1901 GNUNET_assert (peer->id == path->peers[path->length - 1]);
1902 for (aux = peer->path_head; aux != NULL; aux = aux->next)
1904 l2 = path_get_length (aux);
1907 LOG (GNUNET_ERROR_TYPE_DEBUG, " added\n");
1908 GNUNET_CONTAINER_DLL_insert_before (peer->path_head,
1909 peer->path_tail, aux, path);
1910 if (NULL != peer->tunnel && 3 < GCT_count_connections (peer->tunnel))
1918 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1920 LOG (GNUNET_ERROR_TYPE_DEBUG, " already known\n");
1921 path_destroy (path);
1926 GNUNET_CONTAINER_DLL_insert_tail (peer->path_head, peer->path_tail,
1928 LOG (GNUNET_ERROR_TYPE_DEBUG, " added last\n");
1929 if (NULL != peer->tunnel && 3 < GCT_count_connections (peer->tunnel))
1938 * Add the path to the origin peer and update the path used to reach it in case
1939 * this is the shortest.
1940 * The path is given in peer_info -> destination, therefore we turn the path
1941 * upside down first.
1943 * @param peer Peer to add the path to, being the origin of the path.
1944 * @param path New path to add after being inversed.
1945 * Path will be either used or freed.
1946 * @param trusted Do we trust that this path is real?
1948 * @return path if path was taken, pointer to existing duplicate if exists
1951 struct CadetPeerPath *
1952 GCP_add_path_to_origin (struct CadetPeer *peer,
1953 struct CadetPeerPath *path,
1959 return GCP_add_path (peer, path, trusted);
1964 * Adds a path to the info of all the peers in the path
1966 * @param p Path to process.
1967 * @param confirmed Whether we know if the path works or not.
1970 GCP_add_path_to_all (const struct CadetPeerPath *p, int confirmed)
1974 /* TODO: invert and add */
1975 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1976 for (i++; i < p->length; i++)
1978 struct CadetPeer *aux;
1979 struct CadetPeerPath *copy;
1981 aux = GCP_get_short (p->peers[i]);
1982 copy = path_duplicate (p);
1983 copy->length = i + 1;
1984 GCP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1990 * Remove any path to the peer that has the extact same peers as the one given.
1992 * @param peer Peer to remove the path from.
1993 * @param path Path to remove. Is always destroyed .
1996 GCP_remove_path (struct CadetPeer *peer, struct CadetPeerPath *path)
1998 struct CadetPeerPath *iter;
1999 struct CadetPeerPath *next;
2001 GNUNET_assert (myid == path->peers[0]);
2002 GNUNET_assert (peer->id == path->peers[path->length - 1]);
2004 for (iter = peer->path_head; NULL != iter; iter = next)
2007 if (0 == memcmp (path->peers, iter->peers,
2008 sizeof (GNUNET_PEER_Id) * path->length)
2009 && iter->length == path->length)
2011 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, iter);
2013 path_destroy (iter);
2016 path_destroy (path);
2021 * Remove a connection from a neighboring peer.
2023 * @param peer Peer to remove connection from.
2024 * @param c Connection to remove.
2026 * @return GNUNET_OK on success.
2029 GCP_remove_connection (struct CadetPeer *peer,
2030 const struct CadetConnection *c)
2032 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing connection %s\n", GCC_2s (c));
2033 LOG (GNUNET_ERROR_TYPE_DEBUG, "from peer %s\n", GCP_2s (peer));
2035 if (NULL == peer || NULL == peer->connections)
2037 LOG (GNUNET_ERROR_TYPE_DEBUG,
2038 "Peer %s is not a neighbor!\n",
2040 return GNUNET_SYSERR;
2042 LOG (GNUNET_ERROR_TYPE_DEBUG,
2043 "peer %s ok, has %u connections.\n",
2044 GCP_2s (peer), GNUNET_CONTAINER_multihashmap_size (peer->connections));
2046 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
2052 * Start the DHT search for new paths towards the peer: we don't have
2053 * enough good connections.
2055 * @param peer Destination peer.
2058 GCP_start_search (struct CadetPeer *peer)
2060 if (NULL != peer->search_h)
2066 peer->search_h = GCD_search (GCP_get_id (peer), &search_handler, peer);
2071 * Stop the DHT search for new paths towards the peer: we already have
2072 * enough good connections.
2074 * @param peer Destination peer.
2077 GCP_stop_search (struct CadetPeer *peer)
2079 if (NULL == peer->search_h)
2084 GCD_search_stop (peer->search_h);
2085 peer->search_h = NULL;
2090 * Get the Full ID of a peer.
2092 * @param peer Peer to get from.
2094 * @return Full ID of peer.
2096 const struct GNUNET_PeerIdentity *
2097 GCP_get_id (const struct CadetPeer *peer)
2099 return GNUNET_PEER_resolve2 (peer->id);
2104 * Get the Short ID of a peer.
2106 * @param peer Peer to get from.
2108 * @return Short ID of peer.
2111 GCP_get_short_id (const struct CadetPeer *peer)
2124 GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t)
2127 if (NULL == t && NULL != peer->search_h)
2129 GCP_stop_search (peer);
2135 * Get the tunnel towards a peer.
2137 * @param peer Peer to get from.
2139 * @return Tunnel towards peer.
2141 struct CadetTunnel *
2142 GCP_get_tunnel (const struct CadetPeer *peer)
2144 return peer->tunnel;
2149 * Set the hello message.
2151 * @param peer Peer whose message to set.
2152 * @param hello Hello message.
2155 GCP_set_hello (struct CadetPeer *peer, const struct GNUNET_HELLO_Message *hello)
2157 struct GNUNET_HELLO_Message *old;
2160 LOG (GNUNET_ERROR_TYPE_DEBUG, "set hello for %s\n", GCP_2s (peer));
2164 old = GCP_get_hello (peer);
2167 size = GNUNET_HELLO_size (hello);
2168 LOG (GNUNET_ERROR_TYPE_DEBUG, " new (%u bytes)\n", size);
2169 peer->hello = GNUNET_malloc (size);
2170 memcpy (peer->hello, hello, size);
2174 peer->hello = GNUNET_HELLO_merge (old, hello);
2175 LOG (GNUNET_ERROR_TYPE_DEBUG, " merge into %p (%u bytes)\n",
2176 peer->hello, GNUNET_HELLO_size (hello));
2183 * Get the hello message.
2185 * @param peer Peer whose message to get.
2187 * @return Hello message.
2189 struct GNUNET_HELLO_Message *
2190 GCP_get_hello (struct CadetPeer *peer)
2192 struct GNUNET_TIME_Absolute expiration;
2193 struct GNUNET_TIME_Relative remaining;
2195 if (NULL == peer->hello)
2198 expiration = GNUNET_HELLO_get_last_expiration (peer->hello);
2199 remaining = GNUNET_TIME_absolute_get_remaining (expiration);
2200 if (0 == remaining.rel_value_us)
2202 LOG (GNUNET_ERROR_TYPE_DEBUG, " get - hello expired on %s\n",
2203 GNUNET_STRINGS_absolute_time_to_string (expiration));
2204 GNUNET_free (peer->hello);
2212 * Try to connect to a peer on TRANSPORT level.
2214 * @param peer Peer to whom to connect.
2217 GCP_try_connect (struct CadetPeer *peer)
2219 struct GNUNET_HELLO_Message *hello;
2220 struct GNUNET_MessageHeader *mh;
2222 if (NULL == transport_handle)
2225 hello = GCP_get_hello (peer);
2229 mh = GNUNET_HELLO_get_header (hello);
2230 GNUNET_TRANSPORT_offer_hello (transport_handle, mh, try_connect, peer);
2235 * Notify a peer that a link between two other peers is broken. If any path
2236 * used that link, eliminate it.
2238 * @param peer Peer affected by the change.
2239 * @param peer1 Peer whose link is broken.
2240 * @param peer2 Peer whose link is broken.
2243 GCP_notify_broken_link (struct CadetPeer *peer,
2244 struct GNUNET_PeerIdentity *peer1,
2245 struct GNUNET_PeerIdentity *peer2)
2247 struct CadetPeerPath *iter;
2248 struct CadetPeerPath *next;
2253 p1 = GNUNET_PEER_search (peer1);
2254 p2 = GNUNET_PEER_search (peer2);
2256 LOG (GNUNET_ERROR_TYPE_DEBUG, "Link %u-%u broken\n", p1, p2);
2257 if (0 == p1 || 0 == p2)
2259 /* We don't even know them */
2263 for (iter = peer->path_head; NULL != iter; iter = next)
2266 for (i = 0; i < iter->length - 1; i++)
2268 if ((iter->peers[i] == p1 && iter->peers[i + 1] == p2)
2269 || (iter->peers[i] == p2 && iter->peers[i + 1] == p1))
2274 LOG (GNUNET_ERROR_TYPE_DEBUG, " - invalidating %s\n", s);
2277 path_invalidate (iter);
2285 * Count the number of known paths toward the peer.
2287 * @param peer Peer to get path info.
2289 * @return Number of known paths.
2292 GCP_count_paths (const struct CadetPeer *peer)
2294 struct CadetPeerPath *iter;
2297 for (iter = peer->path_head, i = 0; NULL != iter; iter = iter->next)
2305 * Iterate all known peers.
2307 * @param iter Iterator.
2308 * @param cls Closure for @c iter.
2311 GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls)
2313 GNUNET_CONTAINER_multipeermap_iterate (peers, iter, cls);
2318 * Get the static string for a peer ID.
2322 * @return Static string for it's ID.
2325 GCP_2s (const struct CadetPeer *peer)
2329 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));