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 MeshTunnel2 *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 * Peers known, indexed by PeerIdentity (MeshPeer).
183 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
186 * How many peers do we want to remember?
188 static unsigned long long max_peers;
191 * Percentage of messages that will be dropped (for test purposes only).
193 static unsigned long long drop_percent;
196 * Handle to communicate with core.
198 static struct GNUNET_CORE_Handle *core_handle;
201 * Local peer own ID (full value).
203 const static struct GNUNET_PeerIdentity *my_full_id;
206 * Local peer own ID (short)
208 static GNUNET_PEER_Id my_short_id;
210 /******************************************************************************/
211 /***************************** CORE CALLBACKS *********************************/
212 /******************************************************************************/
216 * Iterator to notify all connections of a broken link. Mark connections
217 * to destroy after all traffic has been sent.
219 * @param cls Closure (peer disconnected).
220 * @param key Current key code (peer id).
221 * @param value Value in the hash map (connection).
223 * @return GNUNET_YES if we should continue to iterate,
227 notify_broken (void *cls,
228 const struct GNUNET_HashCode *key,
231 struct MeshPeer *peer = cls;
232 struct MeshConnection *c = value;
234 GMC_notify_broken (c, peer, my_full_id);
241 * Method called whenever a given peer connects.
244 * @param peer peer identity this notification is about
247 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
250 struct MeshPeerPath *path;
252 LOG ("Peer connected\n");
253 LOG (" %s\n", GNUNET_i2s (&my_full_id));
254 pi = peer_get (peer);
262 LOG (" %s\n", GNUNET_i2s (peer));
264 path->peers[1] = pi->id;
265 GNUNET_PEER_change_rc (pi->id, 1);
266 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
268 path->peers[0] = myid;
269 GNUNET_PEER_change_rc (myid, 1);
270 peer_add_path (pi, path, GNUNET_YES);
272 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
278 * Method called whenever a peer disconnects.
281 * @param peer peer identity this notification is about
284 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
288 LOG ("Peer disconnected\n");
289 pi = GNUNET_CONTAINER_multipeermap_get (peers, peer);
296 GNUNET_CONTAINER_multihashmap_iterate (pi->connections, ¬ify_broken, pi);
297 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
298 pi->connections = NULL;
299 if (NULL != pi->core_transmit)
301 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit);
302 pi->core_transmit = NULL;
308 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
315 * Functions to handle messages from core
317 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
318 {&GMC_handle_create, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
320 {&GMC_handle_confirm, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
321 sizeof (struct GNUNET_MESH_ConnectionACK)},
322 {&GMC_handle_broken, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN,
323 sizeof (struct GNUNET_MESH_ConnectionBroken)},
324 {&GMC_handle_destroy, GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY,
325 sizeof (struct GNUNET_MESH_ConnectionDestroy)},
326 {&GMC_handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
327 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
328 {&GMC_handle_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
329 sizeof (struct GNUNET_MESH_ConnectionKeepAlive)},
330 {&GMC_handle_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
331 sizeof (struct GNUNET_MESH_ACK)},
332 {&GMC_handle_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
333 sizeof (struct GNUNET_MESH_Poll)},
334 {&GMC_handle_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
335 {&GMC_handle_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
341 * To be called on core init/fail.
343 * @param cls Closure (config)
344 * @param identity the public identity of this peer
347 core_init (void *cls,
348 const struct GNUNET_PeerIdentity *identity)
350 const struct GNUNET_CONFIGURATION_Handle *c = cls;
353 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
354 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
356 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
357 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (identity));
358 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
359 GNUNET_CORE_disconnect (core_handle);
360 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
361 NULL, /* Closure passed to MESH functions */
362 &core_init, /* Call core_init once connected */
363 &core_connect, /* Handle connects */
364 &core_disconnect, /* remove peers on disconnects */
365 NULL, /* Don't notify about all incoming messages */
366 GNUNET_NO, /* For header only in notification */
367 NULL, /* Don't notify about all outbound messages */
368 GNUNET_NO, /* For header-only out notification */
369 core_handlers); /* Register these handlers */
378 * Core callback to write a pre-constructed data packet to core buffer
380 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
381 * @param size Number of bytes available in buf.
382 * @param buf Where the to write the message.
384 * @return number of bytes written to buf
387 send_core_data_raw (void *cls, size_t size, void *buf)
389 struct GNUNET_MessageHeader *msg = cls;
392 GNUNET_assert (NULL != msg);
393 total_size = ntohs (msg->size);
395 if (total_size > size)
400 memcpy (buf, msg, total_size);
407 * Function to send a create connection message to a peer.
409 * @param c Connection to create.
410 * @param size number of bytes available in buf
411 * @param buf where the callee should write the message
412 * @return number of bytes written to buf
415 send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
417 struct GNUNET_MESH_ConnectionCreate *msg;
418 struct GNUNET_PeerIdentity *peer_ptr;
419 struct MeshPeerPath *p = c->path;
423 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION CREATE...\n");
425 sizeof (struct GNUNET_MESH_ConnectionCreate) +
426 p->length * sizeof (struct GNUNET_PeerIdentity);
428 if (size < size_needed || NULL == buf)
433 msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
434 msg->header.size = htons (size_needed);
435 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
438 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
439 for (i = 0; i < p->length; i++)
441 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
444 LOG (GNUNET_ERROR_TYPE_DEBUG,
445 "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
451 * Creates a path ack message in buf and frees all unused resources.
453 * @param c Connection to send an ACK on.
454 * @param size number of bytes available in buf
455 * @param buf where the callee should write the message
457 * @return number of bytes written to buf
460 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
462 struct GNUNET_MESH_ConnectionACK *msg = buf;
463 struct MeshTunnel2 *t = c->t;
465 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
466 GNUNET_assert (NULL != t);
467 if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
472 msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
473 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
477 /* TODO add signature */
479 LOG (GNUNET_ERROR_TYPE_DEBUG, "CONNECTION ACK sent!\n");
480 return sizeof (struct GNUNET_MESH_ConnectionACK);
484 /******************************************************************************/
485 /******************************** STATIC ***********************************/
486 /******************************************************************************/
489 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
492 * @param key current key code
493 * @param value value in the hash map
494 * @return #GNUNET_YES if we should continue to iterate,
498 shutdown_tunnel (void *cls,
499 const struct GNUNET_PeerIdentity *key,
502 struct MeshPeer *p = value;
503 struct MeshTunnel2 *t = p->tunnel;
513 * Destroy the peer_info and free any allocated resources linked to it
515 * @param peer The peer_info to destroy.
517 * @return GNUNET_OK on success
520 peer_destroy (struct MeshPeer *peer)
522 struct GNUNET_PeerIdentity id;
523 struct MeshPeerPath *p;
524 struct MeshPeerPath *nextp;
526 GNUNET_PEER_resolve (peer->id, &id);
527 GNUNET_PEER_change_rc (peer->id, -1);
530 GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
533 LOG (GNUNET_ERROR_TYPE_WARNING,
534 "removing peer %s, not in peermap\n", GNUNET_i2s (&id));
536 if (NULL != peer->search_h)
538 GMD_search_stop (peer->search_h);
544 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
548 tunnel_destroy_empty (peer->tunnel);
555 * Returns if peer is used (has a tunnel, is neighbor).
557 * @peer Peer to check.
559 * @return GNUNET_YES if peer is in use.
562 peer_is_used (struct MeshPeer *peer)
564 struct MeshPeerPath *p;
566 if (NULL != peer->tunnel)
569 for (p = peer->path_head; NULL != p; p = p->next)
579 * Iterator over all the peers to get the oldest timestamp.
581 * @param cls Closure (unsued).
582 * @param key ID of the peer.
583 * @param value Peer_Info of the peer.
586 peer_get_oldest (void *cls,
587 const struct GNUNET_PeerIdentity *key,
590 struct MeshPeer *p = value;
591 struct GNUNET_TIME_Absolute *abs = cls;
593 /* Don't count active peers */
594 if (GNUNET_YES == peer_is_used (p))
597 if (abs->abs_value_us < p->last_contact.abs_value_us)
598 abs->abs_value_us = p->last_contact.abs_value_us;
605 * Iterator over all the peers to remove the oldest entry.
607 * @param cls Closure (unsued).
608 * @param key ID of the peer.
609 * @param value Peer_Info of the peer.
612 peer_timeout (void *cls,
613 const struct GNUNET_PeerIdentity *key,
616 struct MeshPeer *p = value;
617 struct GNUNET_TIME_Absolute *abs = cls;
619 if (p->last_contact.abs_value_us == abs->abs_value_us &&
620 GNUNET_NO == peer_is_used (p))
630 * Delete oldest unused peer.
633 peer_delete_oldest (void)
635 struct GNUNET_TIME_Absolute abs;
637 abs = GNUNET_TIME_UNIT_FOREVER_ABS;
639 GNUNET_CONTAINER_multipeermap_iterate (peers,
642 GNUNET_CONTAINER_multipeermap_iterate (peers,
649 * Retrieve the MeshPeer stucture associated with the peer, create one
650 * and insert it in the appropriate structures if the peer is not known yet.
652 * @param peer Full identity of the peer.
654 * @return Existing or newly created peer info.
656 static struct MeshPeer *
657 peer_get (const struct GNUNET_PeerIdentity *peer_id)
659 struct MeshPeer *peer;
661 peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
664 peer = GNUNET_new (struct MeshPeer);
665 if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
667 peer_delete_oldest ();
669 GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
670 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
671 peer->id = GNUNET_PEER_intern (peer_id);
673 peer->last_contact = GNUNET_TIME_absolute_get();
680 * Retrieve the MeshPeer stucture associated with the peer, create one
681 * and insert it in the appropriate structures if the peer is not known yet.
683 * @param peer Short identity of the peer.
685 * @return Existing or newly created peer info.
687 static struct MeshPeer *
688 peer_get_short (const GNUNET_PEER_Id peer)
690 return peer_get (GNUNET_PEER_resolve2 (peer));
695 * Get a cost of a path for a peer considering existing tunnel connections.
697 * @param peer Peer towards which the path is considered.
698 * @param path Candidate path.
700 * @return Cost of the path (path length + number of overlapping nodes)
703 peer_get_path_cost (const struct MeshPeer *peer,
704 const struct MeshPeerPath *path)
706 struct MeshConnection *c;
707 unsigned int overlap;
715 GNUNET_assert (NULL != peer->tunnel);
717 for (i = 0; i < path->length; i++)
719 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
721 for (j = 0; j < c->path->length; j++)
723 if (path->peers[i] == c->path->peers[j])
731 return (path->length + overlap) * (path->score * -1);
736 * Choose the best path towards a peer considering the tunnel properties.
738 * @param peer The destination peer.
740 * @return Best current known path towards the peer, if any.
742 static struct MeshPeerPath *
743 peer_get_best_path (const struct MeshPeer *peer)
745 struct MeshPeerPath *best_p;
746 struct MeshPeerPath *p;
747 struct MeshConnection *c;
748 unsigned int best_cost;
751 best_cost = UINT_MAX;
753 for (p = peer->path_head; NULL != p; p = p->next)
755 for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
759 continue; /* If path is in use in a connection, skip it. */
761 if ((cost = peer_get_path_cost (peer, p)) < best_cost)
772 * Function to process paths received for a new peer addition. The recorded
773 * paths form the initial tunnel, which can be optimized later.
774 * Called on each result obtained for the DHT search.
780 search_handler (void *cls, struct MeshPeerPath *path)
782 struct MeshPeer *peer = cls;
783 unsigned int connection_count;
785 path_add_to_peers (path, GNUNET_NO);
787 /* Count connections */
788 connection_count = GMC_count (peer->tunnel->connection_head);
790 /* If we already have 3 (or more (?!)) connections, it's enough */
791 if (3 <= connection_count)
794 if (peer->tunnel->state == MESH_TUNNEL3_SEARCHING)
796 LOG (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
804 * Core callback to write a queued packet to core buffer
806 * @param cls Closure (peer info).
807 * @param size Number of bytes available in buf.
808 * @param buf Where the to write the message.
810 * @return number of bytes written to buf
813 queue_send (void *cls, size_t size, void *buf)
815 struct MeshPeer *peer = cls;
816 struct MeshFlowControl *fc;
817 struct MeshConnection *c;
818 struct GNUNET_MessageHeader *msg;
819 struct MeshPeerQueue *queue;
820 struct MeshTunnel2 *t;
821 struct MeshChannel *ch;
822 const struct GNUNET_PeerIdentity *dst_id;
828 peer->core_transmit = NULL;
829 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Queue send (max %u)\n", size);
831 if (NULL == buf || 0 == size)
833 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
838 queue = peer_get_first_message (peer);
841 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
846 fc = fwd ? &c->fwd_fc : &c->bck_fc;
848 dst_id = GNUNET_PEER_resolve2 (peer->id);
849 LOG (GNUNET_ERROR_TYPE_DEBUG, "* towards %s\n", GNUNET_i2s (dst_id));
850 /* Check if buffer size is enough for the message */
851 if (queue->size > size)
853 LOG (GNUNET_ERROR_TYPE_DEBUG, "* not enough room, reissue\n");
854 peer->core_transmit =
855 GNUNET_CORE_notify_transmit_ready (core_handle,
858 GNUNET_TIME_UNIT_FOREVER_REL,
865 LOG (GNUNET_ERROR_TYPE_DEBUG, "* size %u ok\n", queue->size);
867 t = (NULL != c) ? c->t : NULL;
873 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL3_DESTROY:
874 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
875 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
876 case GNUNET_MESSAGE_TYPE_MESH_FWD:
877 case GNUNET_MESSAGE_TYPE_MESH_BCK:
878 case GNUNET_MESSAGE_TYPE_MESH_ACK:
879 case GNUNET_MESSAGE_TYPE_MESH_POLL:
880 LOG (GNUNET_ERROR_TYPE_DEBUG,
882 GNUNET_MESH_DEBUG_M2S (queue->type));
883 data_size = send_core_data_raw (queue->cls, size, buf);
884 msg = (struct GNUNET_MessageHeader *) buf;
885 type = ntohs (msg->type);
887 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
888 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
889 if (GMC_is_origin (c, GNUNET_YES))
890 data_size = send_core_connection_create (queue->c, size, buf);
892 data_size = send_core_data_raw (queue->cls, size, buf);
894 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
895 LOG (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
896 if (GMC_is_origin (c, GNUNET_NO) ||
897 GMC_is_origin (c, GNUNET_YES))
898 data_size = send_core_connection_ack (queue->c, size, buf);
900 data_size = send_core_data_raw (queue->cls, size, buf);
902 case GNUNET_MESSAGE_TYPE_MESH_DATA:
903 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
904 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
905 /* This should be encapsulted */
911 LOG (GNUNET_ERROR_TYPE_WARNING, "* type unknown: %u\n",
916 if (0 < drop_percent &&
917 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
919 LOG (GNUNET_ERROR_TYPE_WARNING,
920 "Dropping message of type %s\n",
921 GNUNET_MESH_DEBUG_M2S (queue->type));
925 if (NULL != queue->callback)
927 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Calling callback\n");
928 queue->callback (queue->callback_cls,
930 GNUNET_TIME_absolute_get_duration (queue->start_waiting));
933 /* Free queue, but cls was freed by send_core_* */
935 GMP_queue_destroy (queue, GNUNET_NO);
937 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
940 case GNUNET_MESSAGE_TYPE_MESH_FWD:
941 case GNUNET_MESSAGE_TYPE_MESH_BCK:
942 pid = ntohl ( ((struct GNUNET_MESH_Encrypted *) buf)->pid );
943 LOG (GNUNET_ERROR_TYPE_DEBUG, "* accounting pid %u\n", pid);
944 fc->last_pid_sent = pid;
945 send_ack (c, ch, fwd);
951 /* If more data in queue, send next */
952 queue = peer_get_first_message (peer);
955 LOG (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
956 if (NULL == peer->core_transmit) {
957 peer->core_transmit =
958 GNUNET_CORE_notify_transmit_ready(core_handle,
961 GNUNET_TIME_UNIT_FOREVER_REL,
966 queue->start_waiting = GNUNET_TIME_absolute_get ();
970 LOG (GNUNET_ERROR_TYPE_DEBUG,
971 "* tmt rdy called somewhere else\n");
973 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
975 LOG (GNUNET_ERROR_TYPE_DEBUG, "* starting poll timeout\n");
977 GNUNET_SCHEDULER_add_delayed (fc->poll_time, &connection_poll, fc);
982 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
984 GNUNET_SCHEDULER_cancel (fc->poll_task);
985 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
990 c->pending_messages--;
991 if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
993 LOG (GNUNET_ERROR_TYPE_DEBUG, "* destroying connection!\n");
1000 t->pending_messages--;
1001 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
1003 // LOG (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
1007 LOG (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
1014 * Get first sendable message.
1016 * @param peer The destination peer.
1018 * @return Best current known path towards the peer, if any.
1020 static struct MeshPeerQueue *
1021 peer_get_first_message (const struct MeshPeer *peer)
1023 struct MeshPeerQueue *q;
1025 for (q = peer->queue_head; NULL != q; q = q->next)
1027 if (queue_is_sendable (q))
1036 queue_is_sendable (struct MeshPeerQueue *q)
1038 struct MeshFlowControl *fc;
1040 /* Is PID-independent? */
1043 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1044 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1048 /* Is PID allowed? */
1049 fc = q->fwd ? &q->c->fwd_fc : &q->c->bck_fc;
1050 if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1057 /******************************************************************************/
1058 /******************************** API ***********************************/
1059 /******************************************************************************/
1063 * Free a transmission that was already queued with all resources
1064 * associated to the request.
1066 * @param queue Queue handler to cancel.
1067 * @param clear_cls Is it necessary to free associated cls?
1070 GMP_queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
1072 struct MeshPeer *peer;
1073 struct MeshFlowControl *fc;
1078 GNUNET_assert (NULL != queue->c);
1079 fc = fwd ? &queue->c->fwd_fc : &queue->c->bck_fc;
1081 if (GNUNET_YES == clear_cls)
1083 LOG (GNUNET_ERROR_TYPE_DEBUG, " queue destroy type %s\n",
1084 GNUNET_MESH_DEBUG_M2S (queue->type));
1085 switch (queue->type)
1087 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
1088 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL3_DESTROY:
1089 LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
1090 GNUNET_break (GNUNET_YES == queue->c->destroy);
1092 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1093 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1094 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1095 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1096 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
1097 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
1098 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
1099 LOG (GNUNET_ERROR_TYPE_DEBUG, " prebuilt message\n");;
1100 GNUNET_free_non_null (queue->cls);
1105 LOG (GNUNET_ERROR_TYPE_ERROR, " type %s unknown!\n",
1106 GNUNET_MESH_DEBUG_M2S (queue->type));
1110 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1112 if (queue->type != GNUNET_MESSAGE_TYPE_MESH_ACK &&
1113 queue->type != GNUNET_MESSAGE_TYPE_MESH_POLL)
1115 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N- %p %u\n", fc, fc->queue_n);
1119 if (NULL != queue->c)
1121 queue->c->pending_messages--;
1122 if (NULL != queue->c->t)
1124 queue->c->t->pending_messages--;
1128 GNUNET_free (queue);
1133 * @brief Queue and pass message to core when possible.
1135 * @param cls Closure (@c type dependant). It will be used by queue_send to
1136 * build the message to be sent if not already prebuilt.
1137 * @param type Type of the message, 0 for a raw message.
1138 * @param size Size of the message.
1139 * @param c Connection this message belongs to (cannot be NULL).
1140 * @param ch Channel this message belongs to, if applicable (otherwise NULL).
1141 * @param fwd Is this a message going root->dest? (FWD ACK are NOT FWD!)
1142 * @param callback Function to be called once CORE has taken the message.
1143 * @param callback_cls Closure for @c callback.
1146 GMP_queue_add (void *cls, uint16_t type, size_t size,
1147 struct MeshConnection *c,
1148 struct MeshChannel *ch,
1150 GMP_sent callback, void *callback_cls)
1152 struct MeshPeerQueue *queue;
1153 struct MeshFlowControl *fc;
1154 struct MeshPeer *peer;
1158 LOG (GNUNET_ERROR_TYPE_DEBUG,
1159 "queue add %s %s (%u) on c %p, ch %p\n",
1160 fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type), size, c, ch);
1161 GNUNET_assert (NULL != c);
1163 fc = fwd ? &c->fwd_fc : &c->bck_fc;
1164 peer = fwd ? connection_get_next_hop (c) : connection_get_prev_hop (c);
1172 if (NULL == peer->connections)
1174 /* We are not connected to this peer, ignore request. */
1175 GNUNET_break_op (0);
1181 if (GNUNET_MESSAGE_TYPE_MESH_POLL == type ||
1182 GNUNET_MESSAGE_TYPE_MESH_ACK == type)
1187 LOG (GNUNET_ERROR_TYPE_DEBUG, "priority %d\n", priority);
1188 LOG (GNUNET_ERROR_TYPE_DEBUG, "fc %p\n", fc);
1189 if (fc->queue_n >= fc->queue_max && 0 == priority)
1191 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
1194 LOG (GNUNET_ERROR_TYPE_DEBUG,
1195 "queue full: %u/%u\n",
1196 fc->queue_n, fc->queue_max);
1197 return; /* Drop this message */
1200 LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid %u\n", fc->last_pid_sent);
1201 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", fc->last_ack_recv);
1202 if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
1204 call_core = GNUNET_NO;
1205 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task &&
1206 GNUNET_MESSAGE_TYPE_MESH_POLL != type)
1208 LOG (GNUNET_ERROR_TYPE_DEBUG,
1209 "no buffer space (%u > %u): starting poll\n",
1210 fc->last_pid_sent + 1, fc->last_ack_recv);
1211 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1217 call_core = GNUNET_YES;
1218 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
1226 queue->callback = callback;
1227 queue->callback_cls = callback_cls;
1228 if (100 <= priority)
1230 struct MeshPeerQueue *copy;
1231 struct MeshPeerQueue *next;
1233 for (copy = peer->queue_head; NULL != copy; copy = next)
1236 if (copy->type == type && copy->c == c && copy->fwd == fwd)
1238 /* Example: also a FWD ACK for connection XYZ */
1239 GMP_queue_destroy (copy, GNUNET_YES);
1242 GNUNET_CONTAINER_DLL_insert (peer->queue_head, peer->queue_tail, queue);
1246 GNUNET_CONTAINER_DLL_insert_tail (peer->queue_head, peer->queue_tail, queue);
1247 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
1252 if (NULL == peer->core_transmit && GNUNET_YES == call_core)
1254 LOG (GNUNET_ERROR_TYPE_DEBUG,
1255 "calling core tmt rdy towards %s for %u bytes\n",
1256 peer2s (peer), size);
1257 peer->core_transmit =
1258 GNUNET_CORE_notify_transmit_ready (core_handle,
1261 GNUNET_TIME_UNIT_FOREVER_REL,
1262 GNUNET_PEER_resolve2 (peer->id),
1266 queue->start_waiting = GNUNET_TIME_absolute_get ();
1270 LOG (GNUNET_ERROR_TYPE_DEBUG,
1271 "core tmt rdy towards %s already called\n",
1275 c->pending_messages++;
1277 c->t->pending_messages++;
1282 * Cancel all queued messages to a peer that belong to a certain connection.
1284 * @param peer Peer towards whom to cancel.
1285 * @param c Connection whose queued messages to cancel.
1288 GMP_queue_cancel (struct MeshPeer *peer, struct MeshConnection *c)
1290 struct MeshPeerQueue *q;
1291 struct MeshPeerQueue *next;
1293 for (q = peer->queue_head; NULL != q; q = next)
1298 LOG (GNUNET_ERROR_TYPE_DEBUG,
1299 "connection_cancel_queue %s\n",
1300 GNUNET_MESH_DEBUG_M2S (q->type));
1301 GMP_queue_destroy (q, GNUNET_YES);
1304 if (NULL == peer->queue_head)
1306 if (NULL != peer->core_transmit)
1308 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
1309 peer->core_transmit = NULL;
1316 * Initialize the peer subsystem.
1318 * @param c Configuration.
1319 * @param id Peer identity
1322 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c,
1323 const struct GNUNET_PeerIdentity *id)
1326 my_short_id = GNUNET_PEER_intern (id);
1328 peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1330 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
1333 LOG_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1334 "MESH", "MAX_PEERS", "USING DEFAULT");
1339 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
1346 LOG (GNUNET_ERROR_TYPE_WARNING,
1347 "\n***************************************\n"
1348 "Mesh is running with drop mode enabled.\n"
1349 "This is NOT a good idea!\n"
1350 "Remove the DROP_PERCENT option from your configuration.\n"
1351 "***************************************\n");
1354 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1355 NULL, /* Closure passed to MESH functions */
1356 &core_init, /* Call core_init once connected */
1357 &core_connect, /* Handle connects */
1358 &core_disconnect, /* remove peers on disconnects */
1359 NULL, /* Don't notify about all incoming messages */
1360 GNUNET_NO, /* For header only in notification */
1361 NULL, /* Don't notify about all outbound messages */
1362 GNUNET_NO, /* For header-only out notification */
1363 core_handlers); /* Register these handlers */
1364 if (NULL == core_handle)
1367 GNUNET_SCHEDULER_shutdown ();
1373 * Shut down the peer subsystem.
1378 GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
1380 if (core_handle != NULL)
1382 GNUNET_CORE_disconnect (core_handle);
1385 GNUNET_PEER_change_rc (my_short_id, -1);
1390 * Try to establish a new connection to this peer in the given tunnel.
1391 * If the peer doesn't have any path to it yet, try to get one.
1392 * If the peer already has some path, send a CREATE CONNECTION towards it.
1394 * @param peer PeerInfo of the peer.
1397 GMP_connect (struct MeshPeer *peer)
1399 struct MeshTunnel2 *t;
1400 struct MeshPeerPath *p;
1401 struct MeshConnection *c;
1404 LOG (GNUNET_ERROR_TYPE_DEBUG,
1405 "peer_connect towards %s\n",
1409 rerun_search = GNUNET_NO;
1411 if (NULL != peer->path_head)
1413 LOG (GNUNET_ERROR_TYPE_DEBUG, "path exists\n");
1414 p = peer_get_best_path (peer);
1417 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u hops\n", p->length);
1418 c = tunnel_use_path (t, p);
1421 /* This case can happen when the path includes a first hop that is
1422 * not yet known to be connected.
1424 * This happens quite often during testing when running mesh
1425 * under valgrind: core connect notifications come very late and the
1426 * DHT result has already come and created a valid path.
1427 * In this case, the peer->connections hashmap will be NULL and
1428 * tunnel_use_path will not be able to create a connection from that
1431 * Re-running the DHT GET should give core time to callback.
1434 rerun_search = GNUNET_YES;
1438 send_connection_create (c);
1444 if (NULL != peer->search_h && GNUNET_YES == rerun_search)
1446 GMD_search_stop (peer->search_h);
1447 peer->search_h = NULL;
1448 LOG (GNUNET_ERROR_TYPE_DEBUG,
1449 " Stopping DHT GET for peer %s\n",
1453 if (NULL == peer->search_h)
1455 const struct GNUNET_PeerIdentity *id;
1457 id = GNUNET_PEER_resolve2 (peer->id);
1458 LOG (GNUNET_ERROR_TYPE_DEBUG,
1459 " Starting DHT GET for peer %s\n", peer2s (peer));
1460 peer->search_h = GMD_search (id, &search_handler, peer);
1461 if (MESH_TUNNEL3_NEW == t->state)
1462 GMT_change_state (t, MESH_TUNNEL3_SEARCHING);
1474 GMP_set_tunnel (struct MeshPeer *peer, struct MeshTunnel2 *t)
1481 * Chech whether there is a direct (core level) connection to peer.
1483 * @param peer Peer to check.
1485 * @return GNUNET_YES if there is a direct connection.
1488 GMP_is_neighbor (const struct MeshPeer *peer)
1490 struct MeshPeerPath *path;
1492 if (NULL == peer->connections)
1495 for (path = peer->path_head; NULL != path; path = path->next)
1497 if (3 > path->length)
1501 GNUNET_break (0); /* Is not a neighbor but connections is not NULL */
1507 * Add a connection to a neighboring peer.
1509 * Store that the peer is the first hop of the connection in one
1510 * direction and that on peer disconnect the connection must be
1511 * notified and destroyed, for it will no longer be valid.
1513 * @param peer Peer to add connection to.
1514 * @param c Connection to add.
1516 * @return GNUNET_OK on success.
1519 GMP_add_connection (struct MeshPeer *peer,
1520 const struct MeshConnection *c)
1522 if (NULL == peer->connections)
1525 return GNUNET_SYSERR;
1527 return GNUNET_CONTAINER_multihashmap_put (peer->connections,
1530 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1535 * Add the path to the peer and update the path used to reach it in case this
1538 * @param peer_info Destination peer to add the path to.
1539 * @param path New path to add. Last peer must be the peer in arg 1.
1540 * Path will be either used of freed if already known.
1541 * @param trusted Do we trust that this path is real?
1544 GMP_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
1547 struct MeshPeerPath *aux;
1551 if ((NULL == peer_info) || (NULL == path))
1554 path_destroy (path);
1557 if (path->peers[path->length - 1] != peer_info->id)
1560 path_destroy (path);
1563 if (2 >= path->length && GNUNET_NO == trusted)
1565 /* Only allow CORE to tell us about direct paths */
1566 path_destroy (path);
1569 for (l = 1; l < path->length; l++)
1571 if (path->peers[l] == myid)
1573 LOG (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1574 for (l2 = 0; l2 < path->length - l; l2++)
1576 path->peers[l2] = path->peers[l + l2];
1581 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1585 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1586 path->length, peer2s (peer_info));
1588 l = path_get_length (path);
1591 path_destroy (path);
1595 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1596 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1598 l2 = path_get_length (aux);
1601 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1602 peer_info->path_tail, aux, path);
1607 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1609 path_destroy (path);
1614 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1621 * Add the path to the origin peer and update the path used to reach it in case
1622 * this is the shortest.
1623 * The path is given in peer_info -> destination, therefore we turn the path
1624 * upside down first.
1626 * @param peer_info Peer to add the path to, being the origin of the path.
1627 * @param path New path to add after being inversed.
1628 * Path will be either used or freed.
1629 * @param trusted Do we trust that this path is real?
1632 GMP_add_path_to_origin (struct MeshPeer *peer,
1633 struct MeshPeerPath *path,
1639 GMP_add_path (peer, path, trusted);
1644 * Adds a path to the info of all the peers in the path
1646 * @param p Path to process.
1647 * @param confirmed Whether we know if the path works or not.
1650 GMP_add_path_to_all (struct MeshPeerPath *p, int confirmed)
1654 /* TODO: invert and add */
1655 for (i = 0; i < p->length && p->peers[i] != my_short_id; i++) /* skip'em */ ;
1656 for (i++; i < p->length; i++)
1658 struct MeshPeer *aux;
1659 struct MeshPeerPath *copy;
1661 aux = peer_get_short (p->peers[i]);
1662 copy = path_duplicate (p);
1663 copy->length = i + 1;
1664 GMP_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1670 * Remove a connection from a neighboring peer.
1672 * @param peer Peer to remove connection from.
1673 * @param c Connection to remove.
1675 * @return GNUNET_OK on success.
1678 GMP_remove_connection (struct MeshPeer *peer,
1679 const struct MeshConnection *c)
1681 if (NULL == peer || NULL == peer->connections)
1684 return GNUNET_SYSERR;
1686 return GNUNET_CONTAINER_multihashmap_remove (peer->connections,
1692 * Get the Full ID of a peer.
1694 * @param peer Peer to get from.
1696 * @return Full ID of peer.
1698 struct GNUNET_PeerIdentity *
1699 GMP_get_id (const struct MeshPeer *peer)
1701 return GNUNET_PEER_resolve2 (peer->id);
1706 * Get the Short ID of a peer.
1708 * @param peer Peer to get from.
1710 * @return Short ID of peer.
1713 GMP_get_short_id (const struct MeshPeer *peer)
1720 * Get the static string for a peer ID.
1724 * @return Static string for it's ID.
1727 GMP_2s (const struct MeshPeer *peer)
1731 return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));