2 This file is part of GNUnet.
3 (C) 2001 - 2011 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.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - relay corking down to core
48 #include "mesh_protocol.h"
49 #include "gnunet_dht_service.h"
50 #include "mesh_tunnel_tree.h"
52 /* TODO: move into configuration file */
53 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
54 GNUNET_TIME_UNIT_SECONDS,\
56 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
57 GNUNET_TIME_UNIT_SECONDS,\
60 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
61 GNUNET_TIME_UNIT_SECONDS,\
64 #define GET_RESTART_TIME GNUNET_TIME_relative_multiply(\
65 GNUNET_TIME_UNIT_SECONDS,\
68 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
69 GNUNET_TIME_UNIT_SECONDS,\
72 /******************************************************************************/
73 /************************ DATA STRUCTURES ****************************/
74 /******************************************************************************/
76 /** FWD declaration */
80 * Struct containing all info possibly needed to build a package when called
83 struct MeshDataDescriptor
85 /** ID of the tunnel this packet travels in */
86 struct MESH_TunnelID *origin;
91 /** Client that asked for the transmission, if any */
92 struct GNUNET_SERVER_Client *client;
94 /** Who was this message being sent to */
95 struct MeshPeerInfo *peer;
97 /** Ultimate destination of the packet */
98 GNUNET_PEER_Id destination;
100 /** Number of identical messages sent to different hops (multicast) */
101 unsigned int *copies;
103 /** Which handler was used to request the transmission */
104 unsigned int handler_n;
106 /** Size of the data */
109 /** Used to allow a client send more traffic to the service after a
110 * previous packet was tried to be sent to a neighbor and couldn't */
111 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
116 * Struct containing all information regarding a given peer
126 * Last time we heard from this peer
128 struct GNUNET_TIME_Absolute last_contact;
131 * Number of attempts to reconnect so far
133 int n_reconnect_attempts;
136 * Paths to reach the peer, ordered by ascending hop count
138 struct MeshPeerPath *path_head;
141 * Paths to reach the peer, ordered by ascending hop count
143 struct MeshPeerPath *path_tail;
146 * Handle to stop the DHT search for a path to this peer
148 struct GNUNET_DHT_GetHandle *dhtget;
151 * Handles to stop queued transmissions for this peer
153 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
156 * Pointer to info stuctures used as cls for queued transmissions
158 void *infos[CORE_QUEUE_SIZE];
161 * Type of message being in each transmission
163 uint16_t types[CORE_QUEUE_SIZE];
166 * Array of tunnels this peer participates in
167 * (most probably a small amount, therefore not a hashmap)
168 * When the path to the peer changes, notify these tunnels to let them
169 * re-adjust their path trees.
171 struct MeshTunnel **tunnels;
174 * Number of tunnels this peers participates in
176 unsigned int ntunnels;
181 * Data scheduled to transmit (to local client or remote peer)
188 struct MeshQueue *next;
189 struct MeshQueue *prev;
192 * Target of the data (NULL if target is client)
194 struct MeshPeerInfo *peer;
197 * Client to send the data to (NULL if target is peer)
199 struct MeshClient *client;
202 * Size of the message to transmit
207 * How old is the data?
209 struct GNUNET_TIME_Absolute timestamp;
214 struct GNUNET_MessageHeader *data;
218 * Globally unique tunnel identification (owner + number)
219 * DO NOT USE OVER THE NETWORK
224 * Node that owns the tunnel
229 * Tunnel number to differentiate all the tunnels owned by the node oid
230 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
232 MESH_TunnelNumber tid;
236 struct MeshClient; /* FWD declaration */
239 * Struct containing all information regarding a tunnel
240 * For an intermediate node the improtant info used will be:
241 * - id Tunnel unique identification
242 * - paths[0] To know where to send it next
243 * - metainfo: ready, speeds, accounting
250 struct MESH_TunnelID id;
253 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
255 MESH_TunnelNumber local_tid;
258 * Last time the tunnel was used
260 struct GNUNET_TIME_Absolute timestamp;
263 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
264 * containing peers added by id or by type, not intermediate peers.
266 struct GNUNET_CONTAINER_MultiHashMap *peers;
269 * Number of peers that are connected and potentially ready to receive data
271 unsigned int peers_ready;
274 * Number of peers that have been added to the tunnel
276 unsigned int peers_total;
279 * Client owner of the tunnel, if any
281 struct MeshClient *client;
284 * Messages ready to transmit
286 struct MeshQueue *queue_head;
287 struct MeshQueue *queue_tail;
292 struct MeshTunnelTree *tree;
295 * Application type we are looking for in this tunnel
297 GNUNET_MESH_ApplicationType type;
300 * Used to search peers offering a service
302 struct GNUNET_DHT_GetHandle *dht_get_type;
305 * Task to keep the used paths alive
307 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
310 * Task to destroy the tunnel after timeout
312 * FIXME: merge the two? a tunnel will have either
313 * a path refresh OR a timeout, never both!
315 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
320 * Info needed to work with tunnel paths and peers
327 struct MeshTunnel *t;
330 * Neighbouring peer to whom we send the packet to
332 struct MeshPeerInfo *peer;
337 struct MeshPeerPath *path;
340 * Position in peer's transmit queue
347 * Struct containing information about a client of the service
354 struct MeshClient *next;
355 struct MeshClient *prev;
358 * Tunnels that belong to this client, indexed by local id
360 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
363 * Handle to communicate with the client
365 struct GNUNET_SERVER_Client *handle;
368 * Applications that this client has claimed to provide
370 struct GNUNET_CONTAINER_MultiHashMap *apps;
373 * Messages that this client has declared interest in
375 struct GNUNET_CONTAINER_MultiHashMap *types;
379 * ID of the client, for debug messages
388 /******************************************************************************/
389 /************************ DEBUG FUNCTIONS ****************************/
390 /******************************************************************************/
394 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
395 * @param cls string to print
396 * @param tc task context
399 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
403 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
411 /******************************************************************************/
412 /*********************** GLOBAL VARIABLES ****************************/
413 /******************************************************************************/
418 static struct MeshClient *clients;
419 static struct MeshClient *clients_tail;
422 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
424 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
427 * Tunnels incoming, indexed by MESH_TunnelNumber
428 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
430 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
433 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
435 static struct GNUNET_CONTAINER_MultiHashMap *peers;
438 * Handle to communicate with core
440 static struct GNUNET_CORE_Handle *core_handle;
445 static struct GNUNET_DHT_Handle *dht_handle;
450 static struct GNUNET_SERVER_Handle *server_handle;
453 * Notification context, to send messages to local clients
455 static struct GNUNET_SERVER_NotificationContext *nc;
458 * Local peer own ID (memory efficient handle)
460 static GNUNET_PEER_Id myid;
463 * Local peer own ID (full value)
465 static struct GNUNET_PeerIdentity my_full_id;
470 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
475 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
478 * Tunnel ID for the next created tunnel (global tunnel number)
480 static MESH_TunnelNumber next_tid;
483 * Tunnel ID for the next incoming tunnel (local tunnel number)
485 static MESH_TunnelNumber next_local_tid;
488 * All application types provided by this peer
490 static struct GNUNET_CONTAINER_MultiHashMap *applications;
493 * All message types clients of this peer are interested in
495 static struct GNUNET_CONTAINER_MultiHashMap *types;
498 * Task to periodically announce provided applications
500 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
503 * Task to periodically announce itself in the network
505 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
508 unsigned int next_client_id;
512 /******************************************************************************/
513 /************************ ITERATORS ****************************/
514 /******************************************************************************/
516 /* FIXME move iterators here */
519 /******************************************************************************/
520 /************************ PERIODIC FUNCTIONS ****************************/
521 /******************************************************************************/
524 * Announce iterator over for each application provided by the peer
527 * @param key current key code
528 * @param value value in the hash map
529 * @return GNUNET_YES if we should continue to
534 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
536 /* FIXME are hashes in multihash map equal on all aquitectures? */
537 GNUNET_DHT_put (dht_handle,
540 GNUNET_DHT_RO_RECORD_ROUTE |
541 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
542 GNUNET_BLOCK_TYPE_TEST,
543 sizeof (struct GNUNET_PeerIdentity),
544 (const char *) &my_full_id,
546 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
547 &mesh_debug, "DHT_put for app completed");
549 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
551 APP_ANNOUNCE_TIME, NULL, NULL);
558 * Periodically announce what applications are provided by local clients
561 * @param tc task context
564 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
566 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
568 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
572 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
574 announce_applications_task =
575 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
583 * Periodically announce self id in the DHT
586 * @param tc task context
589 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
591 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
593 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
597 * - Set data expiration in function of X
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
601 GNUNET_h2s_full (&my_full_id.hashPubKey));
602 GNUNET_DHT_put (dht_handle, /* DHT handle */
603 &my_full_id.hashPubKey, /* Key to use */
604 10U, /* Replication level */
605 GNUNET_DHT_RO_RECORD_ROUTE |
606 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
607 GNUNET_BLOCK_TYPE_TEST, /* Block type */
608 sizeof(my_full_id), /* Size of the data */
609 (char *)&my_full_id, /* Data itself */
610 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
611 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
613 &mesh_debug, "DHT_put for id completed");
615 NULL, /* Continuation */
616 NULL); /* Continuation closure */
619 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
624 * Function to process paths received for a new peer addition. The recorded
625 * paths form the initial tunnel, which can be optimized later.
626 * Called on each result obtained for the DHT search.
629 * @param exp when will this value expire
630 * @param key key of the result
631 * @param type type of the result
632 * @param size number of bytes in data
633 * @param data pointer to the result data
636 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
637 const GNUNET_HashCode * key,
638 const struct GNUNET_PeerIdentity *get_path,
639 unsigned int get_path_length,
640 const struct GNUNET_PeerIdentity *put_path,
641 unsigned int put_path_length,
642 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
645 /******************************************************************************/
646 /****************** GENERAL HELPER FUNCTIONS ************************/
647 /******************************************************************************/
650 * Check if client has registered with the service and has not disconnected
652 * @param client the client to check
654 * @return non-NULL if client exists in the global DLL
656 static struct MeshClient *
657 client_get (struct GNUNET_SERVER_Client *client)
659 struct MeshClient *c;
664 if (c->handle == client)
673 * Checks if a given client has subscribed to certain message type
675 * @param message_type Type of message to check
676 * @param c Client to check
678 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
683 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
687 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
688 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
692 client_allow_send(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
694 struct MeshDataDescriptor *info = cls;
696 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
698 info->timeout_task = GNUNET_SCHEDULER_NO_TASK;
699 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
704 * Search for a tunnel by global ID using full PeerIdentities
706 * @param oid owner of the tunnel
707 * @param tid global tunnel number
709 * @return tunnel handler, NULL if doesn't exist
711 static struct MeshTunnel *
712 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
716 * Send the message to all clients that have subscribed to its type
718 * @param msg Pointer to the message itself
719 * @return number of clients this message was sent to
722 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
723 const struct GNUNET_MessageHeader *payload)
725 struct GNUNET_PeerIdentity *oid;
726 struct MeshClient *c;
727 struct MeshTunnel *t;
728 MESH_TunnelNumber *tid;
731 char cbuf[htons(msg->size)];
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
734 type = ntohs (payload->type);
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
737 memcpy (cbuf, msg, sizeof(cbuf));
738 switch (htons(msg->type))
740 struct GNUNET_MESH_Unicast *uc;
741 struct GNUNET_MESH_Multicast *mc;
742 struct GNUNET_MESH_ToOrigin *to;
744 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
745 uc = (struct GNUNET_MESH_Unicast *) cbuf;
749 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
750 mc = (struct GNUNET_MESH_Multicast *) cbuf;
754 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
755 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
763 t = tunnel_get (oid, ntohl(*tid));
769 *tid = htonl (t->local_tid);
770 for (count = 0, c = clients; c != NULL; c = c->next)
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
773 if (client_is_subscribed (type, c))
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
777 GNUNET_SERVER_notification_context_unicast (
780 (struct GNUNET_MessageHeader *) cbuf,
789 * Notify the client that owns the tunnel that a peer has connected to it
791 * @param t Tunnel whose owner to notify
792 * @param id Short id of the peer that has connected
795 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
797 struct GNUNET_MESH_PeerControl pc;
799 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
800 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
801 pc.tunnel_id = htonl (t->local_tid);
802 GNUNET_PEER_resolve (id, &pc.peer);
803 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
804 &pc.header, GNUNET_NO);
809 * Function called to notify a client about the socket
810 * being ready to queue more data. "buf" will be
811 * NULL and "size" zero if the socket was closed for
812 * writing in the meantime.
815 * @param size number of bytes available in buf
816 * @param buf where the callee should write the message
817 * @return number of bytes written to buf
820 send_core_create_path (void *cls, size_t size, void *buf);
824 * Function called to notify a client about the socket
825 * being ready to queue more data. "buf" will be
826 * NULL and "size" zero if the socket was closed for
827 * writing in the meantime.
829 * @param cls closure (data itself)
830 * @param size number of bytes available in buf
831 * @param buf where the callee should write the message
833 * @return number of bytes written to buf
836 send_core_data_multicast (void *cls, size_t size, void *buf);
840 * Cancel a core transmission that was already requested and free all resources
841 * associated to the request.
843 * @param peer PeeInfo of the peer whose transmission is cancelled.
844 * @param i Position of the transmission to be cancelled.
847 peer_info_cancel_transmission(struct MeshPeerInfo *peer, unsigned int i)
849 if (NULL != peer->core_transmit[i])
851 struct MeshDataDescriptor *dd;
852 struct MeshPathInfo *path_info;
856 struct GNUNET_PeerIdentity id;
858 GNUNET_PEER_resolve (peer->id, &id);
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "MESH: Cancelling data transmission at %s [%u]\n",
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "MESH: message type %u\n",
868 /* TODO: notify that tranmission has failed */
869 switch (peer->types[i])
871 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
872 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
873 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
876 if (0 == --(*dd->copies))
878 GNUNET_free (dd->copies);
879 GNUNET_free (dd->data);
882 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
884 path_info = peer->infos[i];
885 path_destroy(path_info->path);
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
891 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
892 peer->core_transmit[i] = NULL;
893 GNUNET_free (peer->infos[i]);
899 * Get a unused CORE slot to transmit a message to a peer. If all the slots
900 * are used, cancel one and return it's position.
902 * @param peer PeerInfo of the neighbor we want to transmit to.
904 * @return The index of an available slot to transmit to the neighbor.
907 peer_info_transmit_slot (struct MeshPeerInfo *peer)
911 for (i = 0; peer->core_transmit[i]; i++)
913 if (i == (CORE_QUEUE_SIZE - 1))
915 /* All positions are taken! Overwriting! */
917 peer_info_cancel_transmission(peer, 0);
926 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
927 * and insert it in the appropiate structures if the peer is not known yet.
929 * @param peer Full identity of the peer.
931 * @return Existing or newly created peer info.
933 static struct MeshPeerInfo *
934 peer_info_get (const struct GNUNET_PeerIdentity *peer)
936 struct MeshPeerInfo *peer_info;
938 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
939 if (NULL == peer_info)
942 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
943 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
944 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
945 peer_info->id = GNUNET_PEER_intern (peer);
953 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
954 * and insert it in the appropiate structures if the peer is not known yet.
956 * @param peer Short identity of the peer.
958 * @return Existing or newly created peer info.
960 static struct MeshPeerInfo *
961 peer_info_get_short (const GNUNET_PEER_Id peer)
963 struct GNUNET_PeerIdentity id;
965 GNUNET_PEER_resolve(peer, &id);
966 return peer_info_get(&id);
971 * Iterator to remove the tunnel from the list of tunnels a peer participates
974 * @param cls Closure (tunnel info)
975 * @param key GNUNET_PeerIdentity of the peer (unused)
976 * @param value PeerInfo of the peer
978 * @return always GNUNET_YES, to keep iterating
981 peer_info_delete_tunnel (void* cls, const GNUNET_HashCode* key, void* value)
983 struct MeshTunnel *t = cls;
984 struct MeshPeerInfo *peer = value;
987 for (i = 0; i < peer->ntunnels; i++)
989 if (0 == memcmp(&peer->tunnels[i]->id,
991 sizeof(struct MESH_TunnelID)))
994 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
995 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1004 * Sends a CREATE PATH message for a path to a peer, properly registrating
1005 * all used resources.
1007 * @param peer PeerInfo of the final peer for whom this path is being created.
1008 * @param p Path itself.
1009 * @param t Tunnel for which the path is created.
1012 send_create_path (struct MeshPeerInfo *peer,
1013 struct MeshPeerPath *p,
1014 struct MeshTunnel *t)
1016 struct GNUNET_PeerIdentity id;
1017 struct MeshPathInfo *path_info;
1018 struct MeshPeerInfo *neighbor;
1023 p = tree_get_path_to_peer(t->tree, peer->id);
1030 for (i = 0; i < p->length; i++)
1032 if (p->peers[i] == myid)
1035 if (i >= p->length - 1)
1041 GNUNET_PEER_resolve(p->peers[i + 1], &id);
1043 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1044 path_info->path = p;
1046 neighbor = peer_info_get(&id);
1047 path_info->peer = neighbor;
1048 path_info->pos = peer_info_transmit_slot(neighbor);
1049 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1050 neighbor->infos[path_info->pos] = path_info;
1051 neighbor->core_transmit[path_info->pos] =
1052 GNUNET_CORE_notify_transmit_ready (
1053 core_handle, /* handle */
1056 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1058 sizeof (struct GNUNET_MESH_ManipulatePath)
1059 + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1060 &send_core_create_path, /* callback */
1061 path_info); /* cls */
1066 * Try to establish a new connection to this peer.
1067 * Use the best path for the given tunnel.
1068 * If the peer doesn't have any path to it yet, try to get one.
1069 * If the peer already has some path, send a CREATE PATH towards it.
1071 * @param peer PeerInfo of the peer.
1072 * @param t Tunnel for which to create the path, if possible.
1075 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1077 struct MeshPeerPath *p;
1078 struct MeshPathInfo *path_info;
1080 if (NULL != peer->path_head)
1082 p = tree_get_path_to_peer(t->tree, peer->id);
1085 send_create_path(peer, p, t);
1090 send_client_peer_connected(t, myid);
1093 else if (NULL == peer->dhtget)
1095 struct GNUNET_PeerIdentity id;
1097 GNUNET_PEER_resolve(peer->id, &id);
1098 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1099 path_info->peer = peer;
1102 GNUNET_DHT_get_start(dht_handle, /* handle */
1103 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1104 GNUNET_BLOCK_TYPE_TEST, /* type */
1105 &id.hashPubKey, /* key to search */
1106 4, /* replication level */
1107 GNUNET_DHT_RO_RECORD_ROUTE |
1108 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1110 0, /* xquery bits */
1111 &dht_get_id_handler,
1114 /* Otherwise, there is no path but the DHT get is already started. */
1120 * Destroy the peer_info and free any allocated resources linked to it
1121 * @param t tunnel the path belongs to
1122 * @param pi the peer_info to destroy
1123 * @return GNUNET_OK on success
1126 peer_info_destroy (struct MeshPeerInfo *pi)
1128 GNUNET_HashCode hash;
1129 struct GNUNET_PeerIdentity id;
1131 GNUNET_PEER_resolve (pi->id, &id);
1132 GNUNET_PEER_change_rc (pi->id, -1);
1133 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
1135 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
1136 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
1144 * Notify a tunnel that a connection has broken that affects at least
1145 * some of its peers.
1147 * @param t Tunnel affected.
1148 * @param peer Peer that (at least) has been affected by the disconnection.
1149 * @param p1 Peer that got disconnected from p2.
1150 * @param p2 Peer that got disconnected from p1.
1152 * @return Short ID of the peer disconnected (either p1 or p2).
1153 * 0 if the tunnel remained unaffected.
1155 static GNUNET_PEER_Id
1156 tunnel_notify_connection_broken (struct MeshTunnel *t,
1157 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1161 * Remove all paths that rely on a direct connection between p1 and p2
1162 * from the peer itself and notify all tunnels about it.
1164 * @param peer PeerInfo of affected peer.
1165 * @param p1 GNUNET_PEER_Id of one peer.
1166 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1169 * TODO: optimize (see below)
1172 path_remove_from_peer (struct MeshPeerInfo *peer,
1176 struct GNUNET_PeerIdentity id;
1177 struct MeshPeerPath *p;
1178 struct MeshPeerPath *aux;
1179 struct MeshPeerInfo *peer_d;
1181 unsigned int destroyed;
1187 p = peer->path_head;
1191 for (i = 0; i < (p->length - 1); i++)
1193 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1194 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1206 for (i = 0; i < peer->ntunnels; i++)
1208 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
1210 * Problem: one or more peers have been deleted from the tunnel tree.
1211 * We don't know who they are to try to add them again.
1212 * We need to try to find a new path for each of the disconnected peers.
1213 * Some of them might already have a path to reach them that does not
1214 * involve p1 and p2. Adding all anew might render in a better tree than
1215 * the trivial immediate fix.
1217 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1218 * its children will be reachable trough him.
1220 peer_d = peer_info_get_short(d);
1223 for (p = peer_d->path_head; NULL != p; p = p->next)
1225 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
1233 /* No callback, as peer will be already disconnected */
1234 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
1238 struct MeshPathInfo *path_info;
1240 if (NULL != peer_d->dhtget)
1242 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1243 path_info->path = p;
1244 path_info->peer = peer_d;
1245 path_info->t = peer->tunnels[i];
1247 GNUNET_DHT_get_start(dht_handle, /* handle */
1248 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1249 GNUNET_BLOCK_TYPE_TEST, /* type */
1250 &id.hashPubKey, /*key to search */
1251 4, /* replication level */
1252 GNUNET_DHT_RO_RECORD_ROUTE |
1253 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1255 0, /* xquery bits */
1256 &dht_get_id_handler,
1257 (void *) path_info);
1264 * Add the path to the peer and update the path used to reach it in case this
1267 * @param peer_info Destination peer to add the path to.
1268 * @param path New path to add. Last peer must be the peer in arg 1.
1269 * Path will be either used of freed if already known.
1271 * TODO: trim the part from origin to us? Add it as path to origin?
1274 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1276 struct MeshPeerPath *aux;
1280 if (NULL == peer_info || NULL == path)
1286 l = path_get_length (path);
1293 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1295 l2 = path_get_length (aux);
1298 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1299 peer_info->path_tail, aux, path);
1303 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1310 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1317 * Add the path to the origin peer and update the path used to reach it in case
1318 * this is the shortest.
1319 * The path is given in peer_info -> destination, therefore we turn the path
1320 * upside down first.
1322 * @param peer_info Peer to add the path to, being the origin of the path.
1323 * @param path New path to add after being inversed.
1326 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1329 path_add_to_peer (peer_info, path);
1334 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1335 * to obtain a local peer -> destination path and interning the peer ids.
1337 * @return Newly allocated and created path
1339 static struct MeshPeerPath *
1340 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1341 unsigned int get_path_length,
1342 const struct GNUNET_PeerIdentity *put_path,
1343 unsigned int put_path_length)
1345 struct MeshPeerPath *p;
1351 GNUNET_PEER_change_rc(myid, 1);
1352 i = get_path_length;
1353 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1354 for (i--; i >= 0; i--)
1356 id = GNUNET_PEER_intern (&get_path[i]);
1357 if (p->length > 0 && id == p->peers[p->length - 1])
1359 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1360 GNUNET_PEER_change_rc(id, -1);
1364 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1365 "MESH: Adding from GET: %s.\n",
1366 GNUNET_i2s(&get_path[i]));
1368 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1369 p->peers[p->length - 1] = id;
1372 i = put_path_length;
1373 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1374 for (i--; i >= 0; i--)
1376 id = GNUNET_PEER_intern (&put_path[i]);
1379 /* PUT path went through us, so discard the path up until now and start
1380 * from here to get a much shorter (and loop-free) path.
1385 if (p->length > 0 && id == p->peers[p->length - 1])
1387 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1388 GNUNET_PEER_change_rc(id, -1);
1392 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1393 "MESH: Adding from PUT: %s.\n",
1394 GNUNET_i2s(&put_path[i]));
1396 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1397 p->peers[p->length - 1] = id;
1401 if (get_path_length > 0)
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 "MESH: (first of GET: %s)\n",
1404 GNUNET_h2s_full(&get_path[0].hashPubKey));
1405 if (put_path_length > 0)
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1407 "MESH: (first of PUT: %s)\n",
1408 GNUNET_h2s_full(&put_path[0].hashPubKey));
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1410 "MESH: In total: %d hops\n",
1412 for (i = 0; i < p->length; i++)
1414 struct GNUNET_PeerIdentity peer_id;
1416 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1420 GNUNET_h2s_full(&peer_id.hashPubKey));
1428 * Send keepalive packets for a peer
1430 * @param cls Closure (tunnel for which to send the keepalive).
1431 * @param tc Notification context.
1433 * TODO: implement explicit multicast keepalive?
1436 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1440 * Search for a tunnel among the tunnels for a client
1442 * @param c the client whose tunnels to search in
1443 * @param tid the local id of the tunnel
1445 * @return tunnel handler, NULL if doesn't exist
1447 static struct MeshTunnel *
1448 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1450 GNUNET_HashCode hash;
1452 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1453 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1455 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1457 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1462 * Search for a tunnel by global ID using PEER_ID
1464 * @param pi owner of the tunnel
1465 * @param tid global tunnel number
1467 * @return tunnel handler, NULL if doesn't exist
1469 static struct MeshTunnel *
1470 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1472 struct MESH_TunnelID id;
1473 GNUNET_HashCode hash;
1478 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1479 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1484 * Search for a tunnel by global ID using full PeerIdentities
1486 * @param oid owner of the tunnel
1487 * @param tid global tunnel number
1489 * @return tunnel handler, NULL if doesn't exist
1491 static struct MeshTunnel *
1492 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1494 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1499 * Callback used to notify a client owner of a tunnel that a peer has
1500 * disconnected, most likely because of a path change.
1502 * @param n Node in the tree representing the disconnected peer
1505 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1507 struct MeshPeerInfo *peer;
1509 if (NULL != n->t->client && NULL != nc)
1511 struct GNUNET_MESH_PeerControl msg;
1512 msg.header.size = htons (sizeof (msg));
1513 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1514 msg.tunnel_id = htonl (n->t->local_tid);
1515 GNUNET_PEER_resolve (n->peer, &msg.peer);
1516 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1517 &msg.header, GNUNET_NO);
1519 peer = peer_info_get_short(n->peer);
1520 peer_info_connect(peer, n->t);
1525 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1526 * needed rescources.
1527 * If peer already exists, reevaluate shortest path and change if different.
1529 * @param t Tunnel we want to add a new peer to
1530 * @param peer PeerInfo of the peer being added
1534 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1536 struct GNUNET_PeerIdentity id;
1537 struct MeshPeerPath *best_p;
1538 struct MeshPeerPath *p;
1539 unsigned int best_cost;
1542 GNUNET_PEER_resolve(peer->id, &id);
1544 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1547 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1548 GNUNET_CONTAINER_multihashmap_put(
1552 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1555 if (NULL != (p = peer->path_head))
1558 best_cost = path_get_cost(t->tree, p);
1561 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1568 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1569 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1570 t->path_refresh_task =
1571 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1575 /* Start a DHT get if necessary */
1576 peer_info_connect(peer, t);
1581 * Add a path to a tunnel which we don't own, just to remember the next hop.
1582 * If destination node was already in the tunnel, the first hop information
1583 * will be replaced with the new path.
1585 * @param t Tunnel we want to add a new peer to
1586 * @param p Path to add
1587 * @param own_pos Position of local node in path.
1591 tunnel_add_path (struct MeshTunnel *t,
1592 struct MeshPeerPath *p,
1593 unsigned int own_pos)
1595 struct GNUNET_PeerIdentity id;
1597 GNUNET_assert (0 != own_pos);
1598 tree_add_path(t->tree, p, NULL);
1599 if (NULL == t->tree->me)
1600 t->tree->me = tree_find_peer(t->tree->root, p->peers[own_pos]);
1601 if (own_pos < p->length - 1)
1603 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1604 tree_update_first_hops(t->tree, t->tree->me, &id);
1610 * Notify a tunnel that a connection has broken that affects at least
1611 * some of its peers.
1613 * @param t Tunnel affected.
1614 * @param peer Peer that (at least) has been affected by the disconnection.
1615 * @param p1 Peer that got disconnected from p2.
1616 * @param p2 Peer that got disconnected from p1.
1618 * @return Short ID of the peer disconnected (either p1 or p2).
1619 * 0 if the tunnel remained unaffected.
1621 static GNUNET_PEER_Id
1622 tunnel_notify_connection_broken (struct MeshTunnel *t,
1623 struct MeshPeerInfo *peer,
1627 return tree_notify_connection_broken (t->tree, p1, p2,
1628 ¬ify_peer_disconnected);
1633 * Send a message in a tunnel in multicast, sending a copy to each child node
1634 * down the local one in the tunnel tree.
1636 * @param t Tunnel in which to send the data.
1637 * @param msg Message to be sent
1639 * @return Number of copies sent.
1642 tunnel_send_multicast (struct MeshTunnel *t,
1643 const struct GNUNET_MessageHeader *msg)
1645 struct GNUNET_PeerIdentity neighbor;
1646 struct MeshDataDescriptor *info;
1647 struct MeshTunnelTreeNode *n;
1648 unsigned int *copies;
1653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1654 "MESH: sending a multicast packet...\n");
1655 size = ntohs (msg->size);
1656 GNUNET_assert (NULL != t->tree->me);
1657 n = t->tree->me->children_head;
1660 copies = GNUNET_malloc (sizeof (unsigned int));
1661 for (*copies = 0; NULL != n; n = n->next)
1663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (%u copies)\n", *copies);
1664 n = t->tree->me->children_head;
1665 data = GNUNET_malloc (size);
1666 memcpy (data, msg, size);
1669 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1670 info->origin = &t->id;
1673 info->copies = copies;
1674 if (NULL != t->client)
1676 info->client = t->client->handle;
1677 info->timeout_task = GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT,
1681 info->destination = n->peer;
1682 GNUNET_PEER_resolve (n->peer, &neighbor);
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1684 "MESH: sending to %s...\n",
1685 GNUNET_i2s (&neighbor));
1686 info->peer = peer_info_get(&neighbor);
1687 GNUNET_assert (NULL != info->peer);
1688 i = peer_info_transmit_slot(info->peer);
1689 info->handler_n = i;
1690 info->peer->infos[i] = info;
1691 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1692 info->peer->core_transmit[i] =
1693 GNUNET_CORE_notify_transmit_ready (core_handle,
1696 GNUNET_TIME_UNIT_FOREVER_REL,
1699 &send_core_data_multicast, info);
1707 * Destroy the tunnel and free any allocated resources linked to it
1709 * @param t the tunnel to destroy
1711 * @return GNUNET_OK on success
1714 tunnel_destroy (struct MeshTunnel *t)
1716 struct MeshClient *c;
1717 struct MeshQueue *q;
1718 struct MeshQueue *qn;
1719 GNUNET_HashCode hash;
1728 struct GNUNET_PeerIdentity id;
1730 GNUNET_PEER_resolve(t->id.oid, &id);
1731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1732 "MESH: destroying tunnel %s [%x]\n",
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1740 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1741 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1746 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1748 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1752 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1754 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1755 GNUNET_break (GNUNET_YES ==
1756 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t));
1759 if (NULL != t->peers)
1761 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
1762 &peer_info_delete_tunnel, t);
1763 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1768 if (NULL != q->data)
1769 GNUNET_free (q->data);
1773 /* TODO cancel core transmit ready in case it was active */
1775 tree_destroy(t->tree);
1776 if (NULL != t->dht_get_type)
1777 GNUNET_DHT_get_stop(t->dht_get_type);
1778 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
1779 GNUNET_SCHEDULER_cancel(t->timeout_task);
1780 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
1781 GNUNET_SCHEDULER_cancel(t->path_refresh_task);
1788 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1789 * client when the client disconnects.
1791 * @param cls closure (client that is disconnecting)
1792 * @param key the hash of the local tunnel id (used to access the hashmap)
1793 * @param value the value stored at the key (tunnel to destroy)
1795 * @return GNUNET_OK on success
1798 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1800 struct MeshTunnel *t = value;
1803 if (NULL != t->dht_get_type)
1805 GNUNET_DHT_get_stop (t->dht_get_type);
1807 r = tunnel_destroy (t);
1813 * Timeout function, destroys tunnel if called
1815 * @param cls Closure (tunnel to destroy).
1816 * @param tc TaskContext
1819 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1821 struct MeshTunnel *t = cls;
1823 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
1825 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1830 * Resets the tunnel timeout. Starts it if no timeout was running.
1832 * @param t Tunnel whose timeout to reset.
1835 tunnel_reset_timeout (struct MeshTunnel *t)
1837 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
1838 GNUNET_SCHEDULER_cancel (t->timeout_task);
1839 t->timeout_task = GNUNET_SCHEDULER_add_delayed (
1840 GNUNET_TIME_relative_multiply(REFRESH_PATH_TIME, 4),
1846 /******************************************************************************/
1847 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1848 /******************************************************************************/
1851 * Function called to notify a client about the socket
1852 * being ready to queue more data. "buf" will be
1853 * NULL and "size" zero if the socket was closed for
1854 * writing in the meantime.
1856 * @param cls closure
1857 * @param size number of bytes available in buf
1858 * @param buf where the callee should write the message
1859 * @return number of bytes written to buf
1862 send_core_create_path (void *cls, size_t size, void *buf)
1864 struct MeshPathInfo *info = cls;
1865 struct GNUNET_MESH_ManipulatePath *msg;
1866 struct GNUNET_PeerIdentity *peer_ptr;
1867 struct MeshPeerInfo *peer = info->peer;
1868 struct MeshTunnel *t = info->t;
1869 struct MeshPeerPath *p = info->path;
1873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1874 "MESH: CREATE PATH sending...\n");
1876 sizeof (struct GNUNET_MESH_ManipulatePath) +
1877 p->length * sizeof (struct GNUNET_PeerIdentity);
1879 if (size < size_needed || NULL == buf)
1881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
1882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
1883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
1885 info->peer->core_transmit[info->pos] =
1886 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1887 GNUNET_TIME_UNIT_FOREVER_REL,
1888 path_get_first_hop (t->tree, peer->id),
1890 &send_core_create_path,
1894 info->peer->core_transmit[info->pos] = NULL;
1897 struct GNUNET_PeerIdentity id;
1899 GNUNET_PEER_resolve (peer->id, &id);
1900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1901 "MESH: setting core_transmit %s [%u] to NULL\n",
1906 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1907 msg->header.size = htons (size_needed);
1908 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1909 msg->tid = ntohl (t->id.tid);
1911 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1912 for (i = 0; i < p->length; i++)
1914 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1921 "MESH: CREATE PATH (%u bytes long) sent!\n",
1929 * Function called to notify a client about the socket
1930 * being ready to queue more data. "buf" will be
1931 * NULL and "size" zero if the socket was closed for
1932 * writing in the meantime.
1934 * @param cls closure (MeshDataDescriptor with all info to build packet)
1935 * @param size number of bytes available in buf
1936 * @param buf where the callee should write the message
1937 * @return number of bytes written to buf
1940 send_core_data_to_origin (void *cls, size_t size, void *buf)
1942 struct MeshDataDescriptor *info = cls;
1943 struct GNUNET_MESH_ToOrigin *msg = buf;
1946 GNUNET_assert (NULL != info);
1947 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1948 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1950 if (total_size > size)
1952 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1953 "not enough buffer to send data to origin\n");
1956 msg->header.size = htons (total_size);
1957 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1958 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1959 msg->tid = htonl (info->origin->tid);
1960 if (0 != info->size)
1962 memcpy (&msg[1], &info[1], info->size);
1964 if (NULL != info->client)
1966 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1975 * Function called to notify a client about the socket
1976 * being ready to queue more data. "buf" will be
1977 * NULL and "size" zero if the socket was closed for
1978 * writing in the meantime.
1980 * @param cls closure (data itself)
1981 * @param size number of bytes available in buf
1982 * @param buf where the callee should write the message
1984 * @return number of bytes written to buf
1987 send_core_data_multicast (void *cls, size_t size, void *buf)
1989 struct MeshDataDescriptor *info = cls;
1992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
1993 GNUNET_assert (NULL != info);
1994 GNUNET_assert (NULL != info->peer);
1995 total_size = info->size;
1996 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1998 if (total_size > size)
2001 struct GNUNET_PeerIdentity id;
2003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2004 "MESH: Multicast: retransmitting... (%u/%u)\n",
2006 GNUNET_PEER_resolve(info->peer->id, &id);
2007 info->peer->core_transmit[info->handler_n] =
2008 GNUNET_CORE_notify_transmit_ready (core_handle,
2011 GNUNET_TIME_UNIT_FOREVER_REL,
2014 &send_core_data_multicast,
2018 info->peer->core_transmit[info->handler_n] = NULL;
2019 info->peer->infos[info->handler_n] = NULL;
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2021 memcpy (buf, info->data, total_size);
2024 struct GNUNET_MESH_Multicast *mc;
2025 struct GNUNET_MessageHeader *mh;
2028 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2030 mc = (struct GNUNET_MESH_Multicast *) mh;
2031 mh = (struct GNUNET_MessageHeader *) &mc[1];
2032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2033 "MESH: multicast, payload type %u\n",
2035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2036 "MESH: multicast, payload size %u\n",
2041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2047 if (0 == --(*info->copies))
2049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
2050 if (NULL != info->client)
2052 if (GNUNET_SCHEDULER_NO_TASK != info->timeout_task)
2054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2055 "MESH: cancelling client timeout (%u)...\n",
2056 info->timeout_task);
2057 GNUNET_SCHEDULER_cancel(info->timeout_task);
2058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
2059 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
2062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing memory...\n");
2063 GNUNET_free (info->data);
2064 GNUNET_free (info->copies);
2066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2074 * Function called to notify a client about the socket
2075 * being ready to queue more data. "buf" will be
2076 * NULL and "size" zero if the socket was closed for
2077 * writing in the meantime.
2079 * @param cls closure (MeshDataDescriptor)
2080 * @param size number of bytes available in buf
2081 * @param buf where the callee should write the message
2082 * @return number of bytes written to buf
2085 send_core_path_ack (void *cls, size_t size, void *buf)
2087 struct MeshDataDescriptor *info = cls;
2088 struct GNUNET_MESH_PathACK *msg = buf;
2090 GNUNET_assert (NULL != info);
2093 info->peer->core_transmit[info->handler_n] = NULL;
2095 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2100 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2101 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2102 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2103 msg->tid = htonl (info->origin->tid);
2104 msg->peer_id = my_full_id;
2106 /* TODO add signature */
2108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2109 return sizeof (struct GNUNET_MESH_PathACK);
2114 * Function called to notify a client about the socket
2115 * being ready to queue more data. "buf" will be
2116 * NULL and "size" zero if the socket was closed for
2117 * writing in the meantime.
2119 * @param cls closure (data itself)
2120 * @param size number of bytes available in buf
2121 * @param buf where the callee should write the message
2122 * @return number of bytes written to buf
2125 send_core_data_raw (void *cls, size_t size, void *buf)
2127 struct GNUNET_MessageHeader *msg = cls;
2130 GNUNET_assert (NULL != msg);
2131 total_size = ntohs (msg->size);
2133 if (total_size > size)
2138 memcpy (buf, msg, total_size);
2146 * Send another peer a notification to destroy a tunnel
2147 * @param cls The tunnel to destroy
2148 * @param size Size in the buffer
2149 * @param buf Memory where to put the data to transmit
2150 * @return Size of data put in buffer
2153 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
2155 struct MeshTunnel *t = cls;
2156 struct MeshClient *c;
2157 struct GNUNET_MESH_TunnelMessage *msg;
2161 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2162 /*FIXME*/ msg->header.size =
2163 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
2164 msg->tunnel_id = htonl (t->id.tid);
2166 tunnel_destroy (c, t);
2167 return sizeof (struct GNUNET_MESH_TunnelMessage);
2172 /******************************************************************************/
2173 /******************** MESH NETWORK HANDLERS **************************/
2174 /******************************************************************************/
2178 * Core handler for path creation
2179 * struct GNUNET_CORE_MessageHandler
2181 * @param cls closure
2182 * @param message message
2183 * @param peer peer identity this notification is about
2184 * @param atsi performance data
2186 * @return GNUNET_OK to keep the connection open,
2187 * GNUNET_SYSERR to close it (signal serious error)
2190 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2191 const struct GNUNET_MessageHeader *message,
2192 const struct GNUNET_ATS_Information *atsi)
2194 unsigned int own_pos;
2197 MESH_TunnelNumber tid;
2198 struct GNUNET_MESH_ManipulatePath *msg;
2199 struct GNUNET_PeerIdentity *pi;
2200 GNUNET_HashCode hash;
2201 struct MeshPeerPath *path;
2202 struct MeshPeerInfo *dest_peer_info;
2203 struct MeshPeerInfo *orig_peer_info;
2204 struct MeshTunnel *t;
2206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2207 "MESH: Received a path create msg [%s]\n",
2208 GNUNET_i2s(&my_full_id));
2209 size = ntohs (message->size);
2210 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2212 GNUNET_break_op (0);
2216 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2217 if (size % sizeof (struct GNUNET_PeerIdentity))
2219 GNUNET_break_op (0);
2222 size /= sizeof (struct GNUNET_PeerIdentity);
2225 GNUNET_break_op (0);
2228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2229 "MESH: path has %u hops.\n",
2231 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2233 tid = ntohl (msg->tid);
2234 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2235 t = tunnel_get (pi, tid);
2236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2237 "MESH: path is for tunnel %s [%X].\n",
2242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2243 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2244 t->id.oid = GNUNET_PEER_intern (pi);
2246 t->local_tid = next_local_tid++;
2247 /* FIXME test if taken */
2248 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2249 t->tree = tree_new(t, t->id.oid);
2251 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2253 GNUNET_CONTAINER_multihashmap_put (
2257 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2263 tunnel_reset_timeout (t);
2264 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2266 GNUNET_CONTAINER_multihashmap_put (
2270 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2278 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2279 if (NULL == dest_peer_info)
2281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2282 "MESH: Creating PeerInfo for destination.\n");
2283 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2284 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2285 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2287 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2289 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2290 if (NULL == orig_peer_info)
2292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2293 "MESH: Creating PeerInfo for origin.\n");
2294 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2295 orig_peer_info->id = GNUNET_PEER_intern (pi);
2296 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2297 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2300 path = path_new (size);
2302 for (i = 0; i < size; i++)
2304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2305 "MESH: ... adding %s\n",
2306 GNUNET_i2s(&pi[i]));
2307 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2308 if (path->peers[i] == myid)
2311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2312 "MESH: Own position: %u\n", own_pos);
2315 /* cannot be self, must be 'not found' */
2316 /* create path: self not found in path through self */
2317 GNUNET_break_op (0);
2318 path_destroy (path);
2319 /* FIXME error. destroy tunnel? leave for timeout? */
2322 tunnel_add_path (t, path, own_pos);
2323 t->tree->me = tree_find_peer(t->tree->root, myid);
2324 if (own_pos == size - 1)
2326 /* It is for us! Send ack. */
2327 struct GNUNET_MESH_TunnelNotification cmsg;
2328 struct MeshDataDescriptor *info;
2331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2332 "MESH: It's for us!\n");
2333 path_add_to_origin (orig_peer_info, path);
2334 t->peers = GNUNET_CONTAINER_multihashmap_create(4);
2335 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2337 &my_full_id.hashPubKey,
2338 peer_info_get(&my_full_id),
2339 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2340 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2341 info->origin = &t->id;
2342 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2343 GNUNET_assert (NULL != info->peer);
2344 j = peer_info_transmit_slot(info->peer);
2345 info->handler_n = j;
2346 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2347 info->peer->infos[j] = info;
2348 info->peer->core_transmit[j] =
2349 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2350 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2351 sizeof (struct GNUNET_MESH_PathACK),
2352 &send_core_path_ack, info);
2353 cmsg.header.size = htons(sizeof(cmsg));
2354 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2355 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2356 cmsg.tunnel_id = htonl(t->local_tid);
2357 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2361 struct MeshPeerPath *path2;
2363 /* It's for somebody else! Retransmit. */
2364 path2 = path_duplicate(path);
2365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2366 "MESH: Retransmitting.\n");
2367 path_add_to_peer(dest_peer_info, path);
2368 path = path_duplicate(path2);
2369 path_add_to_origin(orig_peer_info, path2);
2370 send_create_path(dest_peer_info, path, t);
2377 * Core handler for mesh network traffic going from the origin to a peer
2379 * @param cls closure
2380 * @param peer peer identity this notification is about
2381 * @param message message
2382 * @param atsi performance data
2383 * @return GNUNET_OK to keep the connection open,
2384 * GNUNET_SYSERR to close it (signal serious error)
2387 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2388 const struct GNUNET_MessageHeader *message,
2389 const struct GNUNET_ATS_Information *atsi)
2391 struct GNUNET_MESH_Unicast *msg;
2392 struct MeshTunnel *t;
2396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2397 "MESH: got a unicast packet from %s\n",
2399 size = ntohs (message->size);
2401 sizeof (struct GNUNET_MESH_Unicast) +
2402 sizeof (struct GNUNET_MessageHeader))
2407 msg = (struct GNUNET_MESH_Unicast *) message;
2408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2409 "MESH: of type %u\n",
2410 ntohs (msg[1].header.type));
2411 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2414 /* TODO notify back: we don't know this tunnel */
2415 GNUNET_break_op (0);
2418 tunnel_reset_timeout (t);
2419 pid = GNUNET_PEER_search(&msg->destination);
2422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2423 "MESH: it's for us! sending to clients...\n");
2424 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2428 "MESH: not for us, retransmitting...\n");
2429 msg = GNUNET_malloc (size);
2430 memcpy (msg, message, size);
2431 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2432 GNUNET_TIME_UNIT_FOREVER_REL,
2433 path_get_first_hop (t->tree, pid),
2435 &send_core_data_raw, msg);
2441 * Core handler for mesh network traffic going from the origin to all peers
2443 * @param cls closure
2444 * @param message message
2445 * @param peer peer identity this notification is about
2446 * @param atsi performance data
2447 * @return GNUNET_OK to keep the connection open,
2448 * GNUNET_SYSERR to close it (signal serious error)
2450 * TODO: Check who we got this from, to validate route.
2453 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2454 const struct GNUNET_MessageHeader *message,
2455 const struct GNUNET_ATS_Information *atsi)
2457 struct GNUNET_MESH_Multicast *msg;
2458 struct MeshTunnel *t;
2461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2462 "MESH: got a multicast packet from %s\n",
2464 size = ntohs (message->size);
2465 if (sizeof (struct GNUNET_MESH_Multicast) +
2466 sizeof (struct GNUNET_MessageHeader) > size)
2468 GNUNET_break_op (0);
2471 msg = (struct GNUNET_MESH_Multicast *) message;
2472 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2476 /* TODO notify that we dont know that tunnel */
2477 GNUNET_break_op (0);
2480 tunnel_reset_timeout (t);
2482 /* Transmit to locally interested clients */
2483 if (NULL != t->peers &&
2484 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2486 send_subscribed_clients (message, &msg[1].header);
2488 tunnel_send_multicast(t, message);
2494 * Core handler for mesh network traffic toward the owner of a tunnel
2496 * @param cls closure
2497 * @param message message
2498 * @param peer peer identity this notification is about
2499 * @param atsi performance data
2501 * @return GNUNET_OK to keep the connection open,
2502 * GNUNET_SYSERR to close it (signal serious error)
2505 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2506 const struct GNUNET_MessageHeader *message,
2507 const struct GNUNET_ATS_Information *atsi)
2509 struct GNUNET_MESH_ToOrigin *msg;
2510 struct GNUNET_PeerIdentity id;
2511 struct MeshPeerInfo *peer_info;
2512 struct MeshTunnel *t;
2515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2516 "MESH: got a ToOrigin packet from %s\n",
2518 size = ntohs (message->size);
2519 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2520 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2522 GNUNET_break_op (0);
2525 msg = (struct GNUNET_MESH_ToOrigin *) message;
2526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2527 "MESH: of type %u\n",
2528 ntohs (msg[1].header.type));
2529 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2533 /* TODO notify that we dont know this tunnel (whom)? */
2534 GNUNET_break_op (0);
2538 if (t->id.oid == myid)
2541 struct GNUNET_MESH_ToOrigin *copy;
2543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2544 "MESH: it's for us! sending to clients...\n");
2545 if (NULL == t->client)
2547 /* got data packet for ownerless tunnel */
2548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2549 "MESH: no clients!\n");
2550 GNUNET_break_op (0);
2553 /* TODO signature verification */
2554 memcpy (cbuf, message, size);
2555 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2556 copy->tid = htonl (t->local_tid);
2557 GNUNET_SERVER_notification_context_unicast (nc,
2563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2564 "MESH: not for us, retransmitting...\n");
2566 peer_info = peer_info_get (&msg->oid);
2567 if (NULL == peer_info)
2569 /* unknown origin of tunnel */
2573 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
2574 msg = GNUNET_malloc (size);
2575 memcpy (msg, message, size);
2576 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2577 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
2578 &send_core_data_raw, msg);
2585 * Core handler for path ACKs
2587 * @param cls closure
2588 * @param message message
2589 * @param peer peer identity this notification is about
2590 * @param atsi performance data
2592 * @return GNUNET_OK to keep the connection open,
2593 * GNUNET_SYSERR to close it (signal serious error)
2596 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2597 const struct GNUNET_MessageHeader *message,
2598 const struct GNUNET_ATS_Information *atsi)
2600 struct GNUNET_MESH_PathACK *msg;
2601 struct GNUNET_PeerIdentity id;
2602 struct MeshTunnelTreeNode *n;
2603 struct MeshPeerInfo *peer_info;
2604 struct MeshTunnel *t;
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2607 "MESH: Received a path ACK msg [%s]\n",
2608 GNUNET_i2s(&my_full_id));
2609 msg = (struct GNUNET_MESH_PathACK *) message;
2610 t = tunnel_get (&msg->oid, msg->tid);
2613 /* TODO notify that we don't know the tunnel */
2617 /* Message for us? */
2618 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
2621 if (NULL == t->client)
2623 GNUNET_break_op (0);
2626 peer_info = peer_info_get (&msg->peer_id);
2627 if (NULL == peer_info)
2629 GNUNET_break_op (0);
2632 n = tree_find_peer(t->tree->root, peer_info->id);
2635 GNUNET_break_op (0);
2638 n->status = MESH_PEER_READY;
2639 send_client_peer_connected(t, peer_info->id);
2643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2644 "MESH: not for us, retransmitting...\n");
2645 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
2646 peer_info = peer_info_get (&msg->oid);
2647 if (NULL == peer_info)
2649 /* If we know the tunnel, we should DEFINITELY know the peer */
2653 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2654 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2655 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2656 GNUNET_TIME_UNIT_FOREVER_REL,
2658 sizeof (struct GNUNET_MESH_PathACK),
2659 &send_core_data_raw, msg);
2665 * Functions to handle messages from core
2667 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2668 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2669 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2670 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2671 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2672 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2673 sizeof (struct GNUNET_MESH_PathACK)},
2679 /******************************************************************************/
2680 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2681 /******************************************************************************/
2684 * deregister_app: iterator for removing each application registered by a client
2686 * @param cls closure
2687 * @param key the hash of the application id (used to access the hashmap)
2688 * @param value the value stored at the key (client)
2690 * @return GNUNET_OK on success
2693 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2695 GNUNET_break (GNUNET_YES ==
2696 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
2702 * notify_client_connection_failure: notify a client that the connection to the
2703 * requested remote peer is not possible (for instance, no route found)
2704 * Function called when the socket is ready to queue more data. "buf" will be
2705 * NULL and "size" zero if the socket was closed for writing in the meantime.
2707 * @param cls closure
2708 * @param size number of bytes available in buf
2709 * @param buf where the callee should write the message
2710 * @return number of bytes written to buf
2713 notify_client_connection_failure (void *cls, size_t size, void *buf)
2716 struct MeshPeerInfo *peer_info;
2717 struct GNUNET_MESH_PeerControl *msg;
2718 struct GNUNET_PeerIdentity id;
2720 if (0 == size && NULL == buf)
2722 // TODO retry? cancel?
2726 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2727 peer_info = (struct MeshPeerInfo *) cls;
2728 msg = (struct GNUNET_MESH_PeerControl *) buf;
2729 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2730 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2731 // msg->tunnel_id = htonl(peer_info->t->tid);
2732 GNUNET_PEER_resolve (peer_info->id, &id);
2733 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2741 * Send keepalive packets for a peer
2743 * @param cls Closure (tunnel for which to send the keepalive).
2744 * @param tc Notification context.
2746 * TODO: implement explicit multicast keepalive?
2749 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2751 struct MeshTunnel *t = cls;
2752 struct GNUNET_MessageHeader *payload;
2753 struct GNUNET_MESH_Multicast *msg;
2754 size_t size = sizeof(struct GNUNET_MESH_Multicast) +
2755 sizeof(struct GNUNET_MessageHeader);
2758 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2762 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2765 "MESH: sending keepalive for tunnel %d\n",
2768 msg = (struct GNUNET_MESH_Multicast *) cbuf;
2769 msg->header.size = htons (size);
2770 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2771 msg->oid = my_full_id;
2772 msg->tid = htonl(t->id.tid);
2773 payload = (struct GNUNET_MessageHeader *) &msg[1];
2774 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2775 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
2776 tunnel_send_multicast (t, &msg->header);
2778 t->path_refresh_task =
2779 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2785 * Function to process paths received for a new peer addition. The recorded
2786 * paths form the initial tunnel, which can be optimized later.
2787 * Called on each result obtained for the DHT search.
2789 * @param cls closure
2790 * @param exp when will this value expire
2791 * @param key key of the result
2792 * @param type type of the result
2793 * @param size number of bytes in data
2794 * @param data pointer to the result data
2796 * TODO: re-issue the request after certain time? cancel after X results?
2799 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2800 const GNUNET_HashCode * key,
2801 const struct GNUNET_PeerIdentity *get_path,
2802 unsigned int get_path_length,
2803 const struct GNUNET_PeerIdentity *put_path,
2804 unsigned int put_path_length,
2805 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2807 struct MeshPathInfo *path_info = cls;
2808 struct MeshPeerPath *p;
2809 struct GNUNET_PeerIdentity pi;
2812 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2813 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2814 "MESH: Got results from DHT for %s\n",
2815 GNUNET_h2s_full(&pi.hashPubKey));
2816 GNUNET_DHT_get_stop(path_info->peer->dhtget);
2817 path_info->peer->dhtget = NULL;
2819 p = path_build_from_dht (get_path, get_path_length,
2820 put_path, put_path_length);
2821 path_add_to_peer (path_info->peer, p);
2822 for (i = 0; i < path_info->peer->ntunnels; i++)
2824 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2825 peer_info_connect(path_info->peer, path_info->t);
2827 GNUNET_free (path_info);
2834 * Function to process paths received for a new peer addition. The recorded
2835 * paths form the initial tunnel, which can be optimized later.
2836 * Called on each result obtained for the DHT search.
2838 * @param cls closure
2839 * @param exp when will this value expire
2840 * @param key key of the result
2841 * @param type type of the result
2842 * @param size number of bytes in data
2843 * @param data pointer to the result data
2846 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2847 const GNUNET_HashCode * key,
2848 const struct GNUNET_PeerIdentity *get_path,
2849 unsigned int get_path_length,
2850 const struct GNUNET_PeerIdentity *put_path,
2851 unsigned int put_path_length,
2852 enum GNUNET_BLOCK_Type type, size_t size,
2855 const struct GNUNET_PeerIdentity *pi = data;
2856 struct GNUNET_PeerIdentity id;
2857 struct MeshTunnel *t = cls;
2858 struct MeshPeerInfo *peer_info;
2859 struct MeshPathInfo *path_info;
2860 struct MeshPeerPath *p;
2863 if (size != sizeof (struct GNUNET_PeerIdentity))
2865 GNUNET_break_op (0);
2868 GNUNET_assert (NULL != t->client);
2869 GNUNET_DHT_get_stop (t->dht_get_type);
2870 t->dht_get_type = NULL;
2871 peer_info = peer_info_get (pi);
2872 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2873 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2875 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2876 NULL == peer_info->dhtget)
2878 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2879 path_info->peer = peer_info;
2881 /* we don't have a route to the peer, let's try a direct lookup */
2883 GNUNET_DHT_get_start (dht_handle, /* handle */
2884 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2885 GNUNET_BLOCK_TYPE_TEST, /* block type */
2886 &pi->hashPubKey, /* key to look up */
2887 10U, /* replication level */
2888 GNUNET_DHT_RO_RECORD_ROUTE |
2889 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2890 /* option to dht: record route */
2892 0, /* xquery bits */
2893 dht_get_id_handler, /* callback */
2894 path_info); /* closure */
2898 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2899 path_add_to_peer (peer_info, p);
2900 tunnel_add_peer(t, peer_info);
2901 p = tree_get_path_to_peer(t->tree, peer_info->id);
2903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2904 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2905 t->local_tid, p->length);
2906 for (i = 0; i < p->length; i++)
2908 GNUNET_PEER_resolve (p->peers[0], &id);
2909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2910 GNUNET_h2s_full (&id.hashPubKey));
2916 send_create_path(peer_info, p, t);
2920 send_client_peer_connected(t, myid);
2924 /******************************************************************************/
2925 /********************* MESH LOCAL HANDLES **************************/
2926 /******************************************************************************/
2930 * Handler for client disconnection
2932 * @param cls closure
2933 * @param client identification of the client; NULL
2934 * for the last call when the server is destroyed
2937 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2939 struct MeshClient *c;
2940 struct MeshClient *next;
2942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2948 if (c->handle != client && NULL != client)
2950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2955 if (NULL != c->tunnels)
2957 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2958 &tunnel_destroy_iterator,
2960 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2963 /* deregister clients applications */
2964 if (NULL != c->apps)
2966 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2967 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2969 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2970 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2972 GNUNET_SCHEDULER_cancel (announce_applications_task);
2973 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2975 if (NULL != c->types)
2976 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2977 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2989 * Handler for new clients
2991 * @param cls closure
2992 * @param client identification of the client
2993 * @param message the actual message, which includes messages the client wants
2996 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2997 const struct GNUNET_MessageHeader *message)
2999 struct GNUNET_MESH_ClientConnect *cc_msg;
3000 struct MeshClient *c;
3001 GNUNET_MESH_ApplicationType *a;
3008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3009 /* Check data sanity */
3010 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3011 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3012 ntypes = ntohs (cc_msg->types);
3013 napps = ntohs (cc_msg->applications);
3015 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3018 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3022 /* Create new client structure */
3023 c = GNUNET_malloc (sizeof (struct MeshClient));
3025 c->id = next_client_id++;
3028 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3031 GNUNET_MESH_ApplicationType at;
3034 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3035 for (i = 0; i < napps; i++)
3038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3039 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3040 /* store in clients hashmap */
3041 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3042 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3043 /* store in global hashmap, for announcements */
3044 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3045 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3047 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3048 announce_applications_task =
3049 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3057 t = (uint16_t *) & a[napps];
3058 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3059 for (i = 0; i < ntypes; i++)
3062 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3064 /* store in clients hashmap */
3065 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3066 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3067 /* store in global hashmap */
3068 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3069 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3073 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3075 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3076 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3077 GNUNET_SERVER_notification_context_add (nc, client);
3079 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3087 * Handler for requests of new tunnels
3089 * @param cls closure
3090 * @param client identification of the client
3091 * @param message the actual message
3094 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3095 const struct GNUNET_MessageHeader *message)
3097 struct GNUNET_MESH_TunnelMessage *t_msg;
3098 struct MeshTunnel *t;
3099 struct MeshClient *c;
3100 GNUNET_HashCode hash;
3102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3104 /* Sanity check for client registration */
3105 if (NULL == (c = client_get (client)))
3108 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3115 /* Message sanity check */
3116 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3119 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3123 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3124 /* Sanity check for tunnel numbering */
3125 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3128 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3131 /* Sanity check for duplicate tunnel IDs */
3132 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3135 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3139 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
3141 while (NULL != tunnel_get_by_pi (myid, next_tid))
3142 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3143 t->id.tid = next_tid++;
3145 t->local_tid = ntohl (t_msg->tunnel_id);
3147 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3149 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3151 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3152 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3155 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3159 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3161 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3162 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3165 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3168 t->tree = tree_new (t, myid);
3169 t->tree->refresh = REFRESH_PATH_TIME;
3170 t->tree->root->status = MESH_PEER_READY;
3171 t->tree->me = t->tree->root;
3173 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3179 * Handler for requests of deleting tunnels
3181 * @param cls closure
3182 * @param client identification of the client
3183 * @param message the actual message
3186 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3187 const struct GNUNET_MessageHeader *message)
3189 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3190 struct MeshClient *c;
3191 struct MeshTunnel *t;
3192 MESH_TunnelNumber tid;
3193 GNUNET_HashCode hash;
3195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
3197 /* Sanity check for client registration */
3198 if (NULL == (c = client_get (client)))
3201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3204 /* Message sanity check */
3205 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3208 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3214 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3216 /* Retrieve tunnel */
3217 tid = ntohl (tunnel_msg->tunnel_id);
3219 /* Remove from local id hashmap */
3220 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3221 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3222 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
3224 // notify_tunnel_destroy(t);
3226 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3232 * Handler for connection requests to new peers
3234 * @param cls closure
3235 * @param client identification of the client
3236 * @param message the actual message (PeerControl)
3239 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3240 const struct GNUNET_MessageHeader *message)
3242 struct GNUNET_MESH_PeerControl *peer_msg;
3243 struct MeshPeerInfo *peer_info;
3244 struct MeshClient *c;
3245 struct MeshTunnel *t;
3246 MESH_TunnelNumber tid;
3248 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3249 /* Sanity check for client registration */
3250 if (NULL == (c = client_get (client)))
3253 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3257 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3258 /* Sanity check for message size */
3259 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3262 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3266 /* Tunnel exists? */
3267 tid = ntohl (peer_msg->tunnel_id);
3268 t = tunnel_get_by_local_id (c, tid);
3272 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3276 /* Does client own tunnel? */
3277 if (t->client->handle != client)
3280 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3283 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3284 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
3285 peer_info = peer_info_get (&peer_msg->peer);
3287 tunnel_add_peer(t, peer_info);
3288 peer_info_connect(peer_info, t);
3290 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3296 * Handler for disconnection requests of peers in a tunnel
3298 * @param cls closure
3299 * @param client identification of the client
3300 * @param message the actual message (PeerControl)
3303 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3304 const struct GNUNET_MessageHeader *message)
3306 struct GNUNET_MESH_PeerControl *peer_msg;
3307 struct MeshClient *c;
3308 struct MeshTunnel *t;
3309 MESH_TunnelNumber tid;
3311 /* Sanity check for client registration */
3312 if (NULL == (c = client_get (client)))
3315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3318 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3319 /* Sanity check for message size */
3320 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3323 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3327 /* Tunnel exists? */
3328 tid = ntohl (peer_msg->tunnel_id);
3329 t = tunnel_get_by_local_id (c, tid);
3333 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3337 /* Does client own tunnel? */
3338 if (t->client->handle != client)
3341 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3345 /* Ok, delete peer from tunnel */
3346 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3347 &peer_msg->peer.hashPubKey);
3349 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3355 * Handler for connection requests to new peers by type
3357 * @param cls closure
3358 * @param client identification of the client
3359 * @param message the actual message (ConnectPeerByType)
3362 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3363 const struct GNUNET_MessageHeader *message)
3365 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3366 struct MeshClient *c;
3367 struct MeshTunnel *t;
3368 GNUNET_HashCode hash;
3369 MESH_TunnelNumber tid;
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3372 /* Sanity check for client registration */
3373 if (NULL == (c = client_get (client)))
3376 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3380 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3381 /* Sanity check for message size */
3382 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3383 ntohs (connect_msg->header.size))
3386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3390 /* Tunnel exists? */
3391 tid = ntohl (connect_msg->tunnel_id);
3392 t = tunnel_get_by_local_id (c, tid);
3396 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3400 /* Does client own tunnel? */
3401 if (t->client->handle != client)
3404 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3408 /* Do WE have the service? */
3409 t->type = ntohl (connect_msg->type);
3410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3411 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3412 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3415 /* Yes! Fast forward, add ourselves to the tunnel and send the
3416 * good news to the client
3418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3419 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3420 peer_info_get (&my_full_id),
3421 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3424 send_client_peer_connected(t, myid);
3425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3426 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3429 /* Ok, lets find a peer offering the service */
3430 if (NULL != t->dht_get_type)
3432 GNUNET_DHT_get_stop (t->dht_get_type);
3434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3435 GNUNET_h2s_full (&hash));
3437 GNUNET_DHT_get_start (dht_handle,
3438 GNUNET_TIME_UNIT_FOREVER_REL,
3439 GNUNET_BLOCK_TYPE_TEST,
3442 GNUNET_DHT_RO_RECORD_ROUTE |
3443 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3445 &dht_get_type_handler, t);
3447 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3453 * Handler for client traffic directed to one peer
3455 * @param cls closure
3456 * @param client identification of the client
3457 * @param message the actual message
3460 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3461 const struct GNUNET_MessageHeader *message)
3463 struct MeshClient *c;
3464 struct MeshTunnel *t;
3465 struct MeshPeerInfo *pi;
3466 struct GNUNET_MESH_Unicast *data_msg;
3467 MESH_TunnelNumber tid;
3470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3471 "MESH: Got a unicast request from a client!\n");
3473 /* Sanity check for client registration */
3474 if (NULL == (c = client_get (client)))
3477 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3480 data_msg = (struct GNUNET_MESH_Unicast *) message;
3481 /* Sanity check for message size */
3482 size = ntohs (message->size);
3483 if (sizeof (struct GNUNET_MESH_Unicast) +
3484 sizeof (struct GNUNET_MessageHeader) > size)
3487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3491 /* Tunnel exists? */
3492 tid = ntohl (data_msg->tid);
3493 t = tunnel_get_by_local_id (c, tid);
3497 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3501 /* Is it a local tunnel? Then, does client own the tunnel? */
3502 if (NULL != t->client &&
3503 NULL != t->client->handle &&
3504 t->client->handle != client)
3507 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3511 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3512 &data_msg->destination.hashPubKey);
3513 /* Is the selected peer in the tunnel? */
3517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3521 /* Ok, everything is correct, send the message
3522 * (pretend we got it from a mesh peer)
3525 char buf[ntohs (message->size)];
3526 struct GNUNET_MESH_Unicast *copy;
3528 /* Work around const limitation */
3529 copy = (struct GNUNET_MESH_Unicast *) buf;
3530 memcpy (buf, data_msg, size);
3531 copy->oid = my_full_id;
3532 copy->tid = htonl (t->id.tid);
3533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3534 "MESH: calling generic handler...\n");
3535 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
3537 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3543 * Handler for client traffic directed to the origin
3545 * @param cls closure
3546 * @param client identification of the client
3547 * @param message the actual message
3550 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3551 const struct GNUNET_MessageHeader *message)
3553 struct GNUNET_MESH_ToOrigin *data_msg;
3554 struct GNUNET_PeerIdentity id;
3555 struct MeshClient *c;
3556 struct MeshTunnel *t;
3557 MESH_TunnelNumber tid;
3560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3561 "MESH: Got a ToOrigin request from a client!\n");
3563 /* Sanity check for client registration */
3564 if (NULL == (c = client_get (client)))
3567 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3570 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3571 /* Sanity check for message size */
3572 size = ntohs (message->size);
3573 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3574 sizeof (struct GNUNET_MessageHeader) > size)
3577 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3581 /* Tunnel exists? */
3582 tid = ntohl (data_msg->tid);
3583 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3586 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3589 t = tunnel_get_by_local_id (c, tid);
3593 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3597 /* It shouldn't be a local tunnel. */
3598 if (NULL != t->client)
3601 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3604 GNUNET_PEER_resolve(t->id.oid, &id);
3606 /* Ok, everything is correct, send the message
3607 * (pretend we got it from a mesh peer)
3610 char buf[ntohs (message->size)];
3611 struct GNUNET_MESH_ToOrigin *copy;
3613 /* Work around const limitation */
3614 copy = (struct GNUNET_MESH_ToOrigin *) buf;
3615 memcpy (buf, data_msg, size);
3617 copy->tid = htonl (t->id.tid);
3618 copy->sender = my_full_id;
3619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3620 "MESH: calling generic handler...\n");
3621 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL);
3623 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3629 * Handler for client traffic directed to all peers in a tunnel
3631 * @param cls closure
3632 * @param client identification of the client
3633 * @param message the actual message
3636 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3637 const struct GNUNET_MessageHeader *message)
3639 struct MeshClient *c;
3640 struct MeshTunnel *t;
3641 struct GNUNET_MESH_Multicast *data_msg;
3642 MESH_TunnelNumber tid;
3644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3645 "MESH: Got a multicast request from a client!\n");
3647 /* Sanity check for client registration */
3648 if (NULL == (c = client_get (client)))
3651 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3654 data_msg = (struct GNUNET_MESH_Multicast *) message;
3655 /* Sanity check for message size */
3656 if (sizeof (struct GNUNET_MESH_Multicast) +
3657 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
3660 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3664 /* Tunnel exists? */
3665 tid = ntohl (data_msg->tid);
3666 t = tunnel_get_by_local_id (c, tid);
3670 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3674 /* Does client own tunnel? */
3675 if (t->client->handle != client)
3678 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3683 char buf[ntohs(message->size)];
3684 struct GNUNET_MESH_Multicast *copy;
3686 copy = (struct GNUNET_MESH_Multicast *)buf;
3687 memcpy(buf, message, ntohs(message->size));
3688 copy->oid = my_full_id;
3689 copy->tid = htonl(t->id.tid);
3690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3691 "MESH: calling generic handler...\n");
3692 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3695 /* receive done gets called when last copy is sent to a neighbor */
3700 * Functions to handle messages from clients
3702 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3703 {&handle_local_new_client, NULL,
3704 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3705 {&handle_local_tunnel_create, NULL,
3706 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3707 sizeof (struct GNUNET_MESH_TunnelMessage)},
3708 {&handle_local_tunnel_destroy, NULL,
3709 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3710 sizeof (struct GNUNET_MESH_TunnelMessage)},
3711 {&handle_local_connect_add, NULL,
3712 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3713 sizeof (struct GNUNET_MESH_PeerControl)},
3714 {&handle_local_connect_del, NULL,
3715 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3716 sizeof (struct GNUNET_MESH_PeerControl)},
3717 {&handle_local_connect_by_type, NULL,
3718 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3719 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3720 {&handle_local_unicast, NULL,
3721 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3722 {&handle_local_to_origin, NULL,
3723 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3724 {&handle_local_multicast, NULL,
3725 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3731 * To be called on core init/fail.
3733 * @param cls service closure
3734 * @param server handle to the server for this service
3735 * @param identity the public identity of this peer
3738 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3739 const struct GNUNET_PeerIdentity *identity)
3741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3742 core_handle = server;
3743 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3745 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3746 GNUNET_SCHEDULER_shutdown();
3752 * Method called whenever a given peer connects.
3754 * @param cls closure
3755 * @param peer peer identity this notification is about
3756 * @param atsi performance data for the connection
3759 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3760 const struct GNUNET_ATS_Information *atsi)
3762 struct MeshPeerInfo *peer_info;
3763 struct MeshPeerPath *path;
3765 #if MESH_DEBUG_CONNECTION
3766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3768 GNUNET_h2s(&my_full_id.hashPubKey));
3770 peer_info = peer_info_get (peer);
3771 if (myid == peer_info->id)
3773 #if MESH_DEBUG_CONNECTION
3774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3778 #if MESH_DEBUG_CONNECTION
3781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3782 GNUNET_h2s(&peer->hashPubKey));
3785 path = path_new (2);
3786 path->peers[0] = myid;
3787 path->peers[1] = peer_info->id;
3788 GNUNET_PEER_change_rc(myid, 1);
3789 GNUNET_PEER_change_rc(peer_info->id, 1);
3790 path_add_to_peer (peer_info, path);
3795 * Method called whenever a peer disconnects.
3797 * @param cls closure
3798 * @param peer peer identity this notification is about
3801 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3803 struct MeshPeerInfo *pi;
3806 #if MESH_DEBUG_CONNECTION
3807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3809 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3815 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3817 peer_info_cancel_transmission(pi, i);
3819 path_remove_from_peer (pi, pi->id, myid);
3820 #if MESH_DEBUG_CONNECTION
3823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3830 /******************************************************************************/
3831 /************************ MAIN FUNCTIONS ****************************/
3832 /******************************************************************************/
3835 * Iterator over hash map entries.
3837 * @param cls closure
3838 * @param key current key code
3839 * @param value value in the hash map
3840 * @return GNUNET_YES if we should continue to
3845 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
3847 struct MeshTunnel *t = value;
3853 * Task run during shutdown.
3859 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3862 /* TODO: destroy tunnels? */
3863 if (core_handle != NULL)
3865 GNUNET_CORE_disconnect (core_handle);
3868 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
3869 if (dht_handle != NULL)
3871 GNUNET_DHT_disconnect (dht_handle);
3876 GNUNET_SERVER_notification_context_destroy (nc);
3879 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3881 GNUNET_SCHEDULER_cancel (announce_id_task);
3882 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3888 * Process mesh requests.
3890 * @param cls closure
3891 * @param server the initialized server
3892 * @param c configuration to use
3895 run (void *cls, struct GNUNET_SERVER_Handle *server,
3896 const struct GNUNET_CONFIGURATION_Handle *c)
3898 struct MeshPeerInfo *peer;
3899 struct MeshPeerPath *p;
3902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3903 server_handle = server;
3904 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3905 CORE_QUEUE_SIZE, /* queue size */
3906 NULL, /* Closure passed to MESH functions */
3907 &core_init, /* Call core_init once connected */
3908 &core_connect, /* Handle connects */
3909 &core_disconnect, /* remove peers on disconnects */
3910 NULL, /* Don't notify about all incoming messages */
3911 GNUNET_NO, /* For header only in notification */
3912 NULL, /* Don't notify about all outbound messages */
3913 GNUNET_NO, /* For header-only out notification */
3914 core_handlers); /* Register these handlers */
3915 if (core_handle == NULL)
3918 GNUNET_SCHEDULER_shutdown ();
3923 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3926 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3928 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3929 GNUNET_SCHEDULER_shutdown ();
3932 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3933 GNUNET_free (keyfile);
3934 if (my_private_key == NULL)
3936 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3937 _("Mesh service could not access hostkey. Exiting.\n"));
3938 GNUNET_SCHEDULER_shutdown ();
3941 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3942 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3943 &my_full_id.hashPubKey);
3944 myid = GNUNET_PEER_intern (&my_full_id);
3946 dht_handle = GNUNET_DHT_connect (c, 64);
3947 if (dht_handle == NULL)
3953 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3955 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3956 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3957 peers = GNUNET_CONTAINER_multihashmap_create (32);
3958 applications = GNUNET_CONTAINER_multihashmap_create (32);
3959 types = GNUNET_CONTAINER_multihashmap_create (32);
3961 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3962 nc = GNUNET_SERVER_notification_context_create (server_handle,
3964 GNUNET_SERVER_disconnect_notify (server_handle,
3965 &handle_local_client_disconnect,
3970 clients_tail = NULL;
3975 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3976 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3978 /* Create a peer_info for the local peer */
3979 peer = peer_info_get(&my_full_id);
3982 path_add_to_peer(peer, p);
3984 /* Scheduled the task to clean up when shutdown is called */
3985 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3992 * The main function for the mesh service.
3994 * @param argc number of arguments from the command line
3995 * @param argv command line arguments
3996 * @return 0 ok, 1 on error
3999 main (int argc, char *const *argv)
4004 // fprintf (stderr, "main ()\n");
4006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4009 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
4013 // fprintf (stderr, "main () END\n");