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_core_service.h"
26 #include "gnunet_statistics_service.h"
28 #include "mesh_protocol_enc.h"
30 #include "gnunet-service-mesh_peer.h"
31 #include "gnunet-service-mesh_dht.h"
32 #include "gnunet-service-mesh_connection.h"
33 #include "gnunet-service-mesh_local.h"
34 #include "gnunet-service-mesh_tunnel.h"
35 #include "mesh_path.h"
37 #define LOG(level, ...) GNUNET_log_from (level,"mesh-p2p",__VA_ARGS__)
39 /******************************************************************************/
40 /******************************** STRUCTS **********************************/
41 /******************************************************************************/
44 * Struct containing info about a queued transmission to this peer
51 struct MeshPeerQueue *next;
56 struct MeshPeerQueue *prev;
59 * Peer this transmission is directed to.
61 struct MeshPeer *peer;
64 * Connection this message belongs to.
66 struct MeshConnection *c;
74 * Channel this message belongs to, if known.
76 struct MeshChannel *ch;
79 * Pointer to info stucture used as cls.
94 * Set when this message starts waiting for CORE.
96 struct GNUNET_TIME_Absolute start_waiting;
99 * Function to call on sending.
104 * Closure for callback.
110 * Struct containing all information regarding a given peer
120 * Last time we heard from this peer
122 struct GNUNET_TIME_Absolute last_contact;
125 * Paths to reach the peer, ordered by ascending hop count
127 struct MeshPeerPath *path_head;
130 * Paths to reach the peer, ordered by ascending hop count
132 struct MeshPeerPath *path_tail;
135 * Handle to stop the DHT search for paths to this peer
137 struct GMD_search_handle *search_h;
140 * Tunnel to this peer, if any.
142 struct MeshTunnel3 *tunnel;
145 * Connections that go through this peer, indexed by tid;
147 struct GNUNET_CONTAINER_MultiHashMap *connections;
150 * Handle for queued transmissions
152 struct GNUNET_CORE_TransmitHandle *core_transmit;
155 * Transmission queue to core DLL head
157 struct MeshPeerQueue *queue_head;
160 * Transmission queue to core DLL tail
162 struct MeshPeerQueue *queue_tail;
165 * How many messages are in the queue to this peer.
167 unsigned int queue_n;
171 /******************************************************************************/
172 /******************************* GLOBALS ***********************************/
173 /******************************************************************************/
176 * Global handle to the statistics service.
178 extern struct GNUNET_STATISTICS_Handle *stats;
181 * Local peer own ID (full value).
183 extern struct GNUNET_PeerIdentity my_full_id;
186 * Local peer own ID (short)
188 extern GNUNET_PEER_Id myid;
191 * Peers known, indexed by PeerIdentity (MeshPeer).
193 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
196 * How many peers do we want to remember?
198 static unsigned long long max_peers;
201 * Percentage of messages that will be dropped (for test purposes only).
203 static unsigned long long drop_percent;
206 * Handle to communicate with core.
208 static struct GNUNET_CORE_Handle *core_handle;
211 /******************************************************************************/
212 /***************************** CORE CALLBACKS *********************************/
213 /******************************************************************************/
217 * Iterator to notify all connections of a broken link. Mark connections
218 * to destroy after all traffic has been sent.
220 * @param cls Closure (peer disconnected).
221 * @param key Current key code (peer id).
222 * @param value Value in the hash map (connection).
224 * @return GNUNET_YES if we should continue to iterate,
228 notify_broken (void *cls,
229 const struct GNUNET_HashCode *key,
232 struct MeshPeer *peer = cls;
233 struct MeshConnection *c = value;
235 GMC_notify_broken (c, peer, &my_full_id);
242 * Method called whenever a given peer connects.
245 * @param peer peer identity this notification is about
248 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
251 struct MeshPeerPath *path;
253 LOG ("Peer connected\n");
254 LOG (" %s\n", GNUNET_i2s (&my_full_id));
263 LOG (" %s\n", GNUNET_i2s (peer));
265 path->peers[1] = pi->id;
266 GNUNET_PEER_change_rc (pi->id, 1);
267 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
269 path->peers[0] = myid;
270 GNUNET_PEER_change_rc (myid, 1);
271 peer_add_path (pi, path, GNUNET_YES);
273 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
279 * Method called whenever a peer disconnects.
282 * @param peer peer identity this notification is about
285 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
289 LOG ("Peer disconnected\n");
290 pi = GNUNET_CONTAINER_multipeermap_get (peers, peer);
297 GNUNET_CONTAINER_multihashmap_iterate (pi->connections, ¬ify_broken, pi);
298 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
299 pi->connections = NULL;
300 if (NULL != pi->core_transmit)
302 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit);
303 pi->core_transmit = NULL;
309 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
316 * Functions to handle messages from core
318 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
319 {&GMC_handle_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
321 {&GMC_handle_confirm, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
322 sizeof (struct GNUNET_MESH_ConnectionACK)},
323 {&GMC_handle_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
324 sizeof (struct GNUNET_MESH_ConnectionBroken)},
325 {&GMC_handle_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
326 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
327 {&GMC_handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
328 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
329 {&GMC_handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
330 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
331 {&GMC_handle_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
332 sizeof (struct GNUNET_MESH_ACK)},
333 {&GMC_handle_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
334 sizeof (struct GNUNET_MESH_Poll)},
335 {&GMC_handle_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
336 {&GMC_handle_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
342 * To be called on core init/fail.
344 * @param cls Closure (config)
345 * @param identity the public identity of this peer
348 core_init (void *cls,
349 const struct GNUNET_PeerIdentity *identity)
351 const struct GNUNET_CONFIGURATION_Handle *c = cls;
354 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
355 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
357 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
358 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
359 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
360 GNUNET_CORE_disconnect (core_handle);
361 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
362 NULL, /* Closure passed to MESH functions */
363 &core_init, /* Call core_init once connected */
364 &core_connect, /* Handle connects */
365 &core_disconnect, /* remove peers on disconnects */
366 NULL, /* Don't notify about all incoming messages */
367 GNUNET_NO, /* For header only in notification */
368 NULL, /* Don't notify about all outbound messages */
369 GNUNET_NO, /* For header-only out notification */
370 core_handlers); /* Register these handlers */
379 * Core callback to write a pre-constructed data packet to core buffer
381 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
382 * @param size Number of bytes available in buf.
383 * @param buf Where the to write the message.
385 * @return number of bytes written to buf
388 send_core_data_raw (void *cls, size_t size, void *buf)
390 struct GNUNET_MessageHeader *msg = cls;
393 GNUNET_assert (NULL != msg);
394 total_size = ntohs (msg->size);
396 if (total_size > size)
401 memcpy (buf, msg, total_size);
408 * Function to send a create connection message to a peer.
410 * @param c Connection to create.
411 * @param size number of bytes available in buf
412 * @param buf where the callee should write the message
413 * @return number of bytes written to buf
416 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
418 struct GNUNET_MESH_ConnectionCreate *msg;
419 struct GNUNET_PeerIdentity *peer_ptr;
420 struct MeshPeerPath *p = c->path;
424 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
426 sizeof (struct GNUNET_MESH_ConnectionCreate) +
427 p->length * sizeof (struct GNUNET_PeerIdentity);
429 if (size < size_needed || NULL == buf)
434 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
435 msg->header.size = htons (size_needed);
436 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
439 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
440 for (i = 0; i < p->length; i++)
442 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
445 LOG (GNUNET_ERROR_TYPE_DEBUG,
446 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
452 * Creates a path ack message in buf and frees all unused resources.
454 * @param c Connection to send an ACK on.
455 * @param size number of bytes available in buf
456 * @param buf where the callee should write the message
458 * @return number of bytes written to buf
461 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
463 struct GNUNET_MESH_ConnectionACK *msg = buf;
464 struct MeshTunnel3 *t = c->t;
466 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
467 GNUNET_assert (NULL != t);
468 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
473 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
474 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
478 /* TODO add signature */
480 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
481 return sizeof (struct GNUNET_MESH_ConnectionACK);
485 /******************************************************************************/
486 /******************************** STATIC ***********************************/
487 /******************************************************************************/
490 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
493 * @param key current key code
494 * @param value value in the hash map
495 * @return #GNUNET_YES if we should continue to iterate,
499 shutdown_tunnel (void *cls,
500 const struct GNUNET_PeerIdentity *key,
503 struct MeshPeer *p = value;
504 struct MeshTunnel3 *t = p->tunnel;
514 * Destroy the peer_info and free any allocated resources linked to it
516 * @param peer The peer_info to destroy.
518 * @return GNUNET_OK on success
521 peer_destroy (struct MeshPeer *peer)
523 struct GNUNET_PeerIdentity id;
524 struct MeshPeerPath *p;
525 struct MeshPeerPath *nextp;
527 GNUNET_PEER_resolve (peer->id, &id);
528 GNUNET_PEER_change_rc (peer->id, -1);
531 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
534 LOG (GNUNET_ERROR_TYPE_WARNING,
535 "removing peer %s, not in peermap\n", GNUNET_i2s (&id));
537 if (NULL != peer->search_h)
539 GMD_search_stop (peer->search_h);
545 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
549 tunnel_destroy_empty (peer->tunnel);
556 * Returns if peer is used (has a tunnel, is neighbor).
558 * @peer Peer to check.
560 * @return GNUNET_YES if peer is in use.
563 peer_is_used (struct MeshPeer *peer)
565 struct MeshPeerPath *p;
567 if (NULL != peer->tunnel)
570 for (p = peer->path_head; NULL != p; p = p->next)
580 * Iterator over all the peers to get the oldest timestamp.
582 * @param cls Closure (unsued).
583 * @param key ID of the peer.
584 * @param value Peer_Info of the peer.
587 peer_get_oldest (void *cls,
588 const struct GNUNET_PeerIdentity *key,
591 struct MeshPeer *p = value;
592 struct GNUNET_TIME_Absolute *abs = cls;
594 /* Don't count active peers */
595 if (GNUNET_YES == peer_is_used (p))
598 if (abs->abs_value_us < p->last_contact.abs_value_us)
599 abs->abs_value_us = p->last_contact.abs_value_us;
606 * Iterator over all the peers to remove the oldest entry.
608 * @param cls Closure (unsued).
609 * @param key ID of the peer.
610 * @param value Peer_Info of the peer.
613 peer_timeout (void *cls,
614 const struct GNUNET_PeerIdentity *key,
617 struct MeshPeer *p = value;
618 struct GNUNET_TIME_Absolute *abs = cls;
620 if (p->last_contact.abs_value_us == abs->abs_value_us &&
621 GNUNET_NO == peer_is_used (p))
631 * Delete oldest unused peer.
634 peer_delete_oldest (void)
636 struct GNUNET_TIME_Absolute abs;
638 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
640 GNUNET_CONTAINER_multipeermap_iterate (peers,
643 GNUNET_CONTAINER_multipeermap_iterate (peers,
650 * Get a cost of a path for a peer considering existing tunnel connections.
652 * @param peer Peer towards which the path is considered.
653 * @param path Candidate path.
655 * @return Cost of the path (path length + number of overlapping nodes)
658 peer_get_path_cost (const struct MeshPeer *peer,
659 const struct MeshPeerPath *path)
661 struct MeshConnection *c;
662 unsigned int overlap;
670 GNUNET_assert (NULL != peer->tunnel);
672 for (i = 0; i < path->length; i++)
674 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
676 for (j = 0; j < c->path->length; j++)
678 if (path->peers[i] == c->path->peers[j])
686 return (path->length + overlap) * (path->score * -1);
691 * Choose the best path towards a peer considering the tunnel properties.
693 * @param peer The destination peer.
695 * @return Best current known path towards the peer, if any.
697 static struct MeshPeerPath *
698 peer_get_best_path (const struct MeshPeer *peer)
700 struct MeshPeerPath *best_p;
701 struct MeshPeerPath *p;
702 struct MeshConnection *c;
703 unsigned int best_cost;
706 best_cost = UINT_MAX;
708 for (p = peer->path_head; NULL != p; p = p->next)
710 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
714 continue; /* If path is in use in a connection, skip it. */
716 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
727 * Function to process paths received for a new peer addition. The recorded
728 * paths form the initial tunnel, which can be optimized later.
729 * Called on each result obtained for the DHT search.
735 search_handler (void *cls, const struct MeshPeerPath *path)
737 struct MeshPeer *peer = cls;
738 unsigned int connection_count;
740 path_add_to_peers (path, GNUNET_NO);
742 /* Count connections */
743 connection_count = GMC_count (peer->tunnel->connection_head);
745 /* If we already have 3 (or more (?!)) connections, it's enough */
746 if (3 <= connection_count)
749 if (peer->tunnel->state == MESH_TUNNEL3_SEARCHING)
751 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
759 * Core callback to write a queued packet to core buffer
761 * @param cls Closure (peer info).
762 * @param size Number of bytes available in buf.
763 * @param buf Where the to write the message.
765 * @return number of bytes written to buf
768 queue_send (void *cls, size_t size, void *buf)
770 struct MeshPeer *peer = cls;
771 struct MeshConnection *c;
772 struct GNUNET_MessageHeader *msg;
773 struct MeshPeerQueue *queue;
774 struct MeshTunnel3 *t;
775 struct MeshChannel *ch;
776 const struct GNUNET_PeerIdentity *dst_id;
782 peer->core_transmit = NULL;
783 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
785 if (NULL == buf || 0 == size)
787 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
792 queue = peer_get_first_message (peer);
795 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
801 dst_id = GNUNET_PEER_resolve2 (peer->id);
802 LOG (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
803 /* Check if buffer size is enough for the message */
804 if (queue->size > size)
806 LOG (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
807 peer->core_transmit =
808 GNUNET_CORE_notify_transmit_ready (core_handle,
811 GNUNET_TIME_UNIT_FOREVER_REL,
818 LOG (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
820 t = (NULL != c) ? c->t : NULL;
826 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
827 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
828 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
829 case GNUNET_MESSAGE_TYPE_MESH_FWD:
830 case GNUNET_MESSAGE_TYPE_MESH_BCK:
831 case GNUNET_MESSAGE_TYPE_MESH_ACK:
832 case GNUNET_MESSAGE_TYPE_MESH_POLL:
833 LOG (GNUNET_ERROR_TYPE_DEBUG,
835 GNUNET_MESH_DEBUG_M2S (queue->type));
836 data_size = send_core_data_raw (queue->cls, size, buf);
837 msg = (struct GNUNET_MessageHeader *) buf;
838 type = ntohs (msg->type);
840 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
841 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
842 if (GMC_is_origin (c, GNUNET_YES))
843 data_size = send_core_connection_create (queue->c, size, buf);
845 data_size = send_core_data_raw (queue->cls, size, buf);
847 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
848 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
849 if (GMC_is_origin (c, GNUNET_NO) ||
850 GMC_is_origin (c, GNUNET_YES))
851 data_size = send_core_connection_ack (queue->c, size, buf);
853 data_size = send_core_data_raw (queue->cls, size, buf);
855 case GNUNET_MESSAGE_TYPE_MESH_DATA:
856 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
857 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
858 /* This should be encapsulted */
864 LOG (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
869 if (0 < drop_percent &&
870 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
872 LOG (GNUNET_ERROR_TYPE_WARNING,
873 "Dropping message of type %s\n",
874 GNUNET_MESH_DEBUG_M2S (queue->type));
878 /* Free queue, but cls was freed by send_core_* */
880 GMP_queue_destroy (queue, GNUNET_NO);
882 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
885 case GNUNET_MESSAGE_TYPE_MESH_FWD:
886 case GNUNET_MESSAGE_TYPE_MESH_BCK:
887 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
888 LOG (GNUNET_ERROR_TYPE_DEBUG, "* accounting pid %u\n", pid);
889 fc->last_pid_sent = pid;
890 send_ack (c, ch, fwd);
896 /* If more data in queue, send next */
897 queue = peer_get_first_message (peer);
900 LOG (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
901 if (NULL == peer->core_transmit) {
902 peer->core_transmit =
903 GNUNET_CORE_notify_transmit_ready(core_handle,
906 GNUNET_TIME_UNIT_FOREVER_REL,
911 queue->start_waiting = GNUNET_TIME_absolute_get ();
915 LOG (GNUNET_ERROR_TYPE_DEBUG,
916 "* tmt rdy called somewhere else\n");
918 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
920 LOG (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n");
922 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
927 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
929 GNUNET_SCHEDULER_cancel (fc->poll_task);
930 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
936 t->pending_messages--;
937 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
939 LOG (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
943 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
949 queue_is_sendable (struct MeshPeerQueue *q)
951 /* Is PID-independent? */
954 case GNUNET_MESSAGE_TYPE_MESH_ACK:
955 case GNUNET_MESSAGE_TYPE_MESH_POLL:
959 if (GMC_is_sendable (q->c, q->fwd))
966 * Get first sendable message.
968 * @param peer The destination peer.
970 * @return Best current known path towards the peer, if any.
972 static struct MeshPeerQueue *
973 peer_get_first_message (const struct MeshPeer *peer)
975 struct MeshPeerQueue *q;
977 for (q = peer->queue_head; NULL != q; q = q->next)
979 if (queue_is_sendable (q))
989 /******************************************************************************/
990 /******************************** API ***********************************/
991 /******************************************************************************/
995 * Free a transmission that was already queued with all resources
996 * associated to the request.
998 * @param queue Queue handler to cancel.
999 * @param clear_cls Is it necessary to free associated cls?
1002 GMP_queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
1004 struct MeshPeer *peer;
1007 GNUNET_assert (NULL != queue->c);
1009 if (GNUNET_YES == clear_cls)
1011 LOG (GNUNET_ERROR_TYPE_DEBUG, "# queue destroy type %s\n",
1012 GNUNET_MESH_DEBUG_M2S (queue->type));
1013 switch (queue->type)
1015 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
1016 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
1017 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1019 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1020 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1021 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1022 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1023 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
1024 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
1025 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
1026 LOG (GNUNET_ERROR_TYPE_DEBUG, "# prebuilt message\n");;
1027 GNUNET_free_non_null (queue->cls);
1032 LOG (GNUNET_ERROR_TYPE_ERROR, "# type %s unknown!\n",
1033 GNUNET_MESH_DEBUG_M2S (queue->type));
1036 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1038 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
1039 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
1044 if (NULL != queue->callback)
1046 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Calling callback\n");
1047 queue->callback (queue->callback_cls,
1048 queue->c, queue->type,
1049 queue->fwd, queue->size,
1050 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
1053 GNUNET_free (queue);
1058 * @brief Queue and pass message to core when possible.
1060 * @param peer Peer towards which to queue the message.
1061 * @param cls Closure (@c type dependant). It will be used by queue_send to
1062 * build the message to be sent if not already prebuilt.
1063 * @param type Type of the message, 0 for a raw message.
1064 * @param size Size of the message.
1065 * @param c Connection this message belongs to (cannot be NULL).
1066 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1067 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1068 * @param cont Continuation to be called once CORE has taken the message.
1069 * @param cont_cls Closure for @c cont.
1072 GMP_queue_add (struct MeshPeer *peer, void *cls, uint16_t type, size_t size,
1073 struct MeshConnection *c, struct MeshChannel *ch, int fwd,
1074 GMP_sent cont, void *cont_cls)
1076 struct MeshPeerQueue *queue;
1080 LOG (GNUNET_ERROR_TYPE_DEBUG,
1081 "queue add %s %s (%u) on c %p, ch %p\n",
1082 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
1083 GNUNET_assert (NULL != c);
1085 if (NULL == peer->connections)
1087 /* We are not connected to this peer, ignore request. */
1088 GNUNET_break_op (0);
1094 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
1095 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
1100 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1102 call_core = GMC_is_sendable (c, fwd);
1103 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
1111 queue->callback = cont;
1112 queue->callback_cls = cont_cls;
1113 if (100 <= priority)
1115 struct MeshPeerQueue *copy;
1116 struct MeshPeerQueue *next;
1118 for (copy = peer->queue_head; NULL != copy; copy = next)
1121 if (copy->type == type && copy->c == c && copy->fwd == fwd)
1123 /* Example: also a FWD ACK for connection XYZ */
1124 GMP_queue_destroy (copy, GNUNET_YES);
1127 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1131 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1135 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1137 LOG (GNUNET_ERROR_TYPE_DEBUG,
1138 "calling core tmt rdy towards %s for %u bytes\n",
1139 peer2s (peer), size);
1140 peer->core_transmit =
1141 GNUNET_CORE_notify_transmit_ready (core_handle,
1144 GNUNET_TIME_UNIT_FOREVER_REL,
1145 GNUNET_PEER_resolve2 (peer->id),
1149 queue->start_waiting = GNUNET_TIME_absolute_get ();
1153 LOG (GNUNET_ERROR_TYPE_DEBUG,
1154 "core tmt rdy towards %s already called\n",
1162 * Cancel all queued messages to a peer that belong to a certain connection.
1164 * @param peer Peer towards whom to cancel.
1165 * @param c Connection whose queued messages to cancel.
1168 GMP_queue_cancel (struct MeshPeer *peer, struct MeshConnection *c)
1170 struct MeshPeerQueue *q;
1171 struct MeshPeerQueue *next;
1173 for (q = peer->queue_head; NULL != q; q = next)
1178 LOG (GNUNET_ERROR_TYPE_DEBUG,
1179 "connection_cancel_queue %s\n",
1180 GNUNET_MESH_DEBUG_M2S (q->type));
1181 GMP_queue_destroy (q, GNUNET_YES);
1184 if (NULL == peer->queue_head)
1186 if (NULL != peer->core_transmit)
1188 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1189 peer->core_transmit = NULL;
1196 * Get the first transmittable message for a connection.
1198 * @param peer Neighboring peer.
1199 * @param c Connection.
1201 * @return First transmittable message.
1203 static struct MeshPeerQueue *
1204 connection_get_first_message (struct MeshPeer *peer, struct MeshConnection *c)
1206 struct MeshPeerQueue *q;
1208 for (q = peer->queue_head; NULL != q; q = q->next)
1212 if (queue_is_sendable (q))
1220 GMP_queue_unlock (struct MeshPeer *peer, struct MeshConnection *c)
1222 struct MeshPeerQueue *q;
1225 if (NULL != peer->core_transmit)
1227 LOG (GNUNET_ERROR_TYPE_DEBUG, " already unlocked!\n");
1228 return; /* Already unlocked */
1231 q = connection_get_first_message (peer, c);
1234 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue empty!\n");
1235 return; /* Nothing to transmit */
1239 peer->core_transmit =
1240 GNUNET_CORE_notify_transmit_ready (core_handle,
1243 GNUNET_TIME_UNIT_FOREVER_REL,
1244 GNUNET_PEER_resolve2 (peer->id),
1252 * Initialize the peer subsystem.
1254 * @param c Configuration.
1255 * @param id Peer identity
1258 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c)
1260 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1262 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
1265 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1266 "MESH", "MAX_PEERS", "USING DEFAULT");
1271 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
1278 LOG (GNUNET_ERROR_TYPE_WARNING,
1279 "\n***************************************\n"
1280 "Mesh is running with drop mode enabled.\n"
1281 "This is NOT a good idea!\n"
1282 "Remove the DROP_PERCENT option from your configuration.\n"
1283 "***************************************\n");
1286 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1287 NULL, /* Closure passed to MESH functions */
1288 &core_init, /* Call core_init once connected */
1289 &core_connect, /* Handle connects */
1290 &core_disconnect, /* remove peers on disconnects */
1291 NULL, /* Don't notify about all incoming messages */
1292 GNUNET_NO, /* For header only in notification */
1293 NULL, /* Don't notify about all outbound messages */
1294 GNUNET_NO, /* For header-only out notification */
1295 core_handlers); /* Register these handlers */
1296 if (NULL == core_handle)
1299 GNUNET_SCHEDULER_shutdown ();
1305 * Shut down the peer subsystem.
1310 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1312 if (core_handle != NULL)
1314 GNUNET_CORE_disconnect (core_handle);
1317 GNUNET_PEER_change_rc (myid, -1);
1321 * Retrieve the MeshPeer stucture associated with the peer, create one
1322 * and insert it in the appropriate structures if the peer is not known yet.
1324 * @param peer Full identity of the peer.
1326 * @return Existing or newly created peer structure.
1329 GMP_get (const struct GNUNET_PeerIdentity *peer_id)
1331 struct MeshPeer *peer;
1333 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
1336 peer = GNUNET_new (struct MeshPeer);
1337 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
1339 peer_delete_oldest ();
1341 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
1342 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1343 peer->id = GNUNET_PEER_intern (peer_id);
1345 peer->last_contact = GNUNET_TIME_absolute_get();
1352 * Retrieve the MeshPeer stucture associated with the peer, create one
1353 * and insert it in the appropriate structures if the peer is not known yet.
1355 * @param peer Short identity of the peer.
1357 * @return Existing or newly created peer structure.
1360 GMP_get_short (const GNUNET_PEER_Id peer)
1362 return GMP_get (GNUNET_PEER_resolve2 (peer));
1367 * Try to establish a new connection to this peer (in its tunnel).
1368 * If the peer doesn't have any path to it yet, try to get one.
1369 * If the peer already has some path, send a CREATE CONNECTION towards it.
1371 * @param peer Peer to connect to.
1374 GMP_connect (struct MeshPeer *peer)
1376 struct MeshTunnel3 *t;
1377 struct MeshPeerPath *p;
1378 struct MeshConnection *c;
1381 LOG (GNUNET_ERROR_TYPE_DEBUG,
1382 "peer_connect towards %s\n",
1386 rerun_search = GNUNET_NO;
1388 if (NULL != peer->path_head)
1390 LOG (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
1391 p = peer_get_best_path (peer);
1394 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
1395 c = GMT_use_path (t, p);
1398 /* This case can happen when the path includes a first hop that is
1399 * not yet known to be connected.
1401 * This happens quite often during testing when running mesh
1402 * under valgrind: core connect notifications come very late and the
1403 * DHT result has already come and created a valid path.
1404 * In this case, the peer->connections hashmap will be NULL and
1405 * tunnel_use_path will not be able to create a connection from that
1408 * Re-running the DHT GET should give core time to callback.
1411 rerun_search = GNUNET_YES;
1415 GMC_send_create (c);
1421 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1423 GMD_search_stop (peer->search_h);
1424 peer->search_h = NULL;
1425 LOG (GNUNET_ERROR_TYPE_DEBUG,
1426 " Stopping DHT GET for peer %s\n",
1430 if (NULL == peer->search_h)
1432 const struct GNUNET_PeerIdentity *id;
1434 id = GNUNET_PEER_resolve2 (peer->id);
1435 LOG (GNUNET_ERROR_TYPE_DEBUG,
1436 " Starting DHT GET for peer %s\n", peer2s (peer));
1437 peer->search_h = GMD_search (id, &search_handler, peer);
1438 if (MESH_TUNNEL3_NEW == GMT_get_state (t))
1439 GMT_change_state (t, MESH_TUNNEL3_SEARCHING);
1451 GMP_set_tunnel (struct MeshPeer *peer, struct MeshTunnel3 *t)
1458 * Chech whether there is a direct (core level) connection to peer.
1460 * @param peer Peer to check.
1462 * @return GNUNET_YES if there is a direct connection.
1465 GMP_is_neighbor (const struct MeshPeer *peer)
1467 struct MeshPeerPath *path;
1469 if (NULL == peer->connections)
1472 for (path = peer->path_head; NULL != path; path = path->next)
1474 if (3 > path->length)
1478 GNUNET_break (0); /* Is not a neighbor but connections is not NULL */
1484 * Create and initialize a new tunnel towards a peer, in case it has none.
1486 * Does not generate any traffic, just creates the local data structures.
1488 * @param peer Peer towards which to create the tunnel.
1491 GMP_add_tunnel (struct MeshPeer *peer)
1493 if (NULL != peer->tunnel)
1495 peer->tunnel = GMT_new (peer);
1500 * Add a connection to a neighboring peer.
1502 * Store that the peer is the first hop of the connection in one
1503 * direction and that on peer disconnect the connection must be
1504 * notified and destroyed, for it will no longer be valid.
1506 * @param peer Peer to add connection to.
1507 * @param c Connection to add.
1509 * @return GNUNET_OK on success.
1512 GMP_add_connection (struct MeshPeer *peer,
1513 struct MeshConnection *c)
1515 if (NULL == peer->connections)
1518 return GNUNET_SYSERR;
1520 return GNUNET_CONTAINER_multihashmap_put (peer->connections,
1523 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1528 * Add the path to the peer and update the path used to reach it in case this
1531 * @param peer_info Destination peer to add the path to.
1532 * @param path New path to add. Last peer must be the peer in arg 1.
1533 * Path will be either used of freed if already known.
1534 * @param trusted Do we trust that this path is real?
1537 GMP_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
1540 struct MeshPeerPath *aux;
1544 if ((NULL == peer_info) || (NULL == path))
1547 path_destroy (path);
1550 if (path->peers[path->length - 1] != peer_info->id)
1553 path_destroy (path);
1556 if (2 >= path->length && GNUNET_NO == trusted)
1558 /* Only allow CORE to tell us about direct paths */
1559 path_destroy (path);
1562 for (l = 1; l < path->length; l++)
1564 if (path->peers[l] == myid)
1566 LOG (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1567 for (l2 = 0; l2 < path->length - l; l2++)
1569 path->peers[l2] = path->peers[l + l2];
1574 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1578 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1579 path->length, peer2s (peer_info));
1581 l = path_get_length (path);
1584 path_destroy (path);
1588 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1589 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1591 l2 = path_get_length (aux);
1594 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1595 peer_info->path_tail, aux, path);
1600 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1602 path_destroy (path);
1607 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1614 * Add the path to the origin peer and update the path used to reach it in case
1615 * this is the shortest.
1616 * The path is given in peer_info -> destination, therefore we turn the path
1617 * upside down first.
1619 * @param peer_info Peer to add the path to, being the origin of the path.
1620 * @param path New path to add after being inversed.
1621 * Path will be either used or freed.
1622 * @param trusted Do we trust that this path is real?
1625 GMP_add_path_to_origin (struct MeshPeer *peer,
1626 struct MeshPeerPath *path,
1632 GMP_add_path (peer, path, trusted);
1637 * Adds a path to the info of all the peers in the path
1639 * @param p Path to process.
1640 * @param confirmed Whether we know if the path works or not.
1643 GMP_add_path_to_all (struct MeshPeerPath *p, int confirmed)
1647 /* TODO: invert and add */
1648 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1649 for (i++; i < p->length; i++)
1651 struct MeshPeer *aux;
1652 struct MeshPeerPath *copy;
1654 aux = GMP_get_short (p->peers[i]);
1655 copy = path_duplicate (p);
1656 copy->length = i + 1;
1657 GMP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1663 * Remove a connection from a neighboring peer.
1665 * @param peer Peer to remove connection from.
1666 * @param c Connection to remove.
1668 * @return GNUNET_OK on success.
1671 GMP_remove_connection (struct MeshPeer *peer,
1672 const struct MeshConnection *c)
1674 if (NULL == peer || NULL == peer->connections)
1677 return GNUNET_SYSERR;
1679 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1685 * Get the Full ID of a peer.
1687 * @param peer Peer to get from.
1689 * @return Full ID of peer.
1691 const struct GNUNET_PeerIdentity *
1692 GMP_get_id (const struct MeshPeer *peer)
1694 return GNUNET_PEER_resolve2 (peer->id);
1699 * Get the Short ID of a peer.
1701 * @param peer Peer to get from.
1703 * @return Short ID of peer.
1706 GMP_get_short_id (const struct MeshPeer *peer)
1713 * Get the static string for a peer ID.
1717 * @return Static string for it's ID.
1720 GMP_2s (const struct MeshPeer *peer)
1724 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));