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
29 * - MESH NETWORK HANDLER HELPERS
30 * - MESH NETWORK HANDLES
31 * - MESH LOCAL HANDLER HELPERS
32 * - MESH LOCAL HANDLES
33 * - PERIODIC FUNCTIONS
34 * - MAIN FUNCTIONS (main & run)
37 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
38 * - partial disconnect reporting -- same as error reporting?
39 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
40 * - speed requirement specification (change?) in mesh API -- API call
42 * - add connection confirmation message
43 * - handle trnsmt_rdy return values
47 #include "gnunet_common.h"
48 #include "gnunet_util_lib.h"
49 #include "gnunet_peer_lib.h"
50 #include "gnunet_core_service.h"
51 #include "gnunet_protocols.h"
54 #include "mesh_protocol.h"
55 #include "gnunet_dht_service.h"
58 #define CORE_QUEUE_SIZE 10
59 #define LOCAL_QUEUE_SIZE 100
60 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
61 GNUNET_TIME_UNIT_SECONDS,\
65 /******************************************************************************/
66 /************************ DATA STRUCTURES ****************************/
67 /******************************************************************************/
70 * Information regarding a path
78 struct MeshPath *next;
79 struct MeshPath *prev;
82 * Whether the path is serving traffic in a tunnel or is a backup
87 * List of all the peers that form the path from origin to target
89 GNUNET_PEER_Id *peers;
92 * Number of peers (hops) in the path
99 * All the states a peer participating in a tunnel can be in.
104 * Path to the peer not known yet
109 * Request sent, not yet answered.
114 * Peer connected and ready to accept data
119 * Peer connected previosly but not responding
121 MESH_PEER_RECONNECTING
125 /** FWD declaration */
129 * Struct containing all info possibly needed to build a package when called
132 struct MeshDataDescriptor
134 /** ID of the tunnel this packet travels in */
135 struct MESH_TunnelID *origin;
137 /** Ultimate destination of the packet */
138 GNUNET_PEER_Id destination;
140 /** Number of identical messages sent to different hops (multicast) */
143 /** Size of the data */
146 /** Client that asked for the transmission, if any */
147 struct GNUNET_SERVER_Client *client;
149 /** Who was is message being sent to */
150 struct MeshPeerInfo *peer;
152 /** Which handler was used to request the transmission */
153 unsigned int handler_n;
155 /* Data at the end */
160 * Struct containing all information regarding a given peer
170 * Is the peer reachable? Is the peer even connected?
172 enum MeshPeerState state;
175 * Last time we heard from this peer
177 struct GNUNET_TIME_Absolute last_contact;
180 * Number of attempts to reconnect so far
182 int n_reconnect_attempts;
185 * Paths to reach the peer
187 struct MeshPath *path;
188 struct MeshPath *path_tail;
191 * Handle to stop the DHT search for a path to this peer
193 struct GNUNET_DHT_GetHandle *dhtget;
196 * Handles to stop queued transmissions for this peer
198 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
201 * Pointer to info stuctures used as cls for queued transmissions
203 struct MeshDataDescriptor *infos[CORE_QUEUE_SIZE];
208 * Data scheduled to transmit (to local client or remote peer)
215 struct MeshQueue *next;
216 struct MeshQueue *prev;
219 * Target of the data (NULL if target is client)
221 struct MeshPeerInfo *peer;
224 * Client to send the data to (NULL if target is peer)
226 struct MeshClient *client;
229 * Size of the message to transmit
234 * How old is the data?
236 struct GNUNET_TIME_Absolute timestamp;
241 struct GNUNET_MessageHeader *data;
245 * Globally unique tunnel identification (owner + number)
246 * DO NOT USE OVER THE NETWORK
248 struct MESH_TunnelID {
250 * Node that owns the tunnel
255 * Tunnel number to differentiate all the tunnels owned by the node oid
256 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_MARK )
258 MESH_TunnelNumber tid;
262 struct MeshClient; /* FWD declaration */
264 * Struct containing all information regarding a tunnel
265 * For an intermediate node the improtant info used will be:
266 * - id Tunnel unique identification
267 * - paths[0] To know where to send it next
268 * - metainfo: ready, speeds, accounting
275 struct MESH_TunnelID id;
278 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK or 0 )
280 MESH_TunnelNumber local_tid;
283 * Last time the tunnel was used
285 struct GNUNET_TIME_Absolute timestamp;
288 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
290 struct GNUNET_CONTAINER_MultiHashMap* peers;
293 * Number of peers that are connected and potentially ready to receive data
295 unsigned int peers_ready;
298 * Number of peers that have been added to the tunnel
300 unsigned int peers_total;
304 * Client owner of the tunnel, if any
306 struct MeshClient *client;
309 * Messages ready to transmit
311 struct MeshQueue *queue_head;
312 struct MeshQueue *queue_tail;
317 * Struct containing information about a client of the service
324 struct MeshClient *next;
325 struct MeshClient *prev;
328 * Tunnels that belong to this client, indexed by local id
330 struct GNUNET_CONTAINER_MultiHashMap* tunnels;
333 * Handle to communicate with the client
335 struct GNUNET_SERVER_Client *handle;
338 * Applications that this client has claimed to provide
340 GNUNET_MESH_ApplicationType *apps;
341 unsigned int app_counter;
344 * Messages that this client has declared interest in
347 unsigned int type_counter;
351 /******************************************************************************/
352 /*********************** GLOBAL VARIABLES ****************************/
353 /******************************************************************************/
358 static struct MeshClient *clients;
359 static struct MeshClient *clients_tail;
362 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
364 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
367 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
369 static struct GNUNET_CONTAINER_MultiHashMap *peers;
372 * Handle to communicate with core
374 static struct GNUNET_CORE_Handle *core_handle;
379 static struct GNUNET_DHT_Handle *dht_handle;
384 static struct GNUNET_SERVER_Handle *server_handle;
387 * Notification context, to send messages to local clients
389 static struct GNUNET_SERVER_NotificationContext *nc;
392 * Local peer own ID (memory efficient handle)
394 static GNUNET_PEER_Id myid;
397 * Tunnel ID for the next created tunnel (global tunnel number)
399 static MESH_TunnelNumber next_tid;
401 /******************************************************************************/
402 /****************** GENERAL HELPER FUNCTIONS ************************/
403 /******************************************************************************/
406 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
407 * and inster it in the appropiate structures if the peer is not known yet.
408 * @param peer Identity of the peer
409 * @return Existing or newly created peer info
411 static struct MeshPeerInfo *
412 get_peer_info (const struct GNUNET_PeerIdentity *peer)
414 struct MeshPeerInfo * peer_info;
416 peer_info = GNUNET_CONTAINER_multihashmap_get(peers,
418 if (NULL == peer_info) {
419 peer_info = (struct MeshPeerInfo *)
420 GNUNET_malloc(sizeof(struct MeshPeerInfo));
421 GNUNET_CONTAINER_multihashmap_put(peers,
424 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
425 peer_info->id = GNUNET_PEER_intern(peer);
426 peer_info->state = MESH_PEER_SEARCHING;
433 * Find the first peer whom to send a packet to go down this path
434 * @param path The path to use
435 * @return short id of the next peer, myid in case of local delivery,
436 * or 0 in case of error
438 static GNUNET_PEER_Id
439 get_first_hop (struct MeshPath *path)
443 while (NULL != path) {
444 if (path->in_use) break;
448 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
449 "tried to get the next hop from an invalid path\n");
453 for (i = 0; i < path->length; i++) {
454 if (path->peers[i] == myid) {
455 if (i < path->length - 1) {
456 return path->peers[i+1];
467 * Get the cost of the path.
468 * @param path The path to analyze
469 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
473 get_path_cost(struct MeshPath *path)
477 if (NULL == path) return UINT_MAX;
478 for (i = 0; i < path->length; i++) {
479 if (path->peers[i] == myid) {
480 return path->length - i;
488 * Add the path to the peer and update the path used to reach it in case this
490 * @param peer_info Destination peer to add the path to.
491 * @param path New path to add. Last peer must be the peer in arg 1.
494 add_path_to_peer(struct MeshPeerInfo *peer_info, struct MeshPath *path)
497 unsigned int new_cost;
498 unsigned int best_cost;
499 struct MeshPath *aux;
500 struct MeshPath *best;
502 if (NULL == peer_info || NULL == path) return;
504 new_cost = get_path_cost(path);
505 best_cost = UINT_MAX;
507 for (aux = peer_info->path; aux != NULL; aux = aux->next) {
508 if ((i = get_path_cost(aux)) < best_cost) {
513 if (best_cost < new_cost) {
515 GNUNET_CONTAINER_DLL_insert_tail(peer_info->path,
516 peer_info->path_tail,
519 if (NULL != best) best->in_use = 0;
521 GNUNET_CONTAINER_DLL_insert(peer_info->path,
522 peer_info->path_tail,
530 * Add the path to the peer and update the path used to reach it in case this
531 * is the shortest. The path is given in reverse, the destination peer is
532 * path[0]. The function modifies the path, inverting it to use the origin as
534 * @param peer_info Destination peer to add the path to.
535 * @param path New path to add. First peer must be the peer in arg 1.
538 add_path_to_origin(struct MeshPeerInfo *peer_info, struct MeshPath *path)
543 for (i = 0; i < path->length/2; i++) {
544 aux = path->peers[i];
545 path->peers[i] = path->peers[path->length - i - 1];
546 path->peers[path->length - i - 1] = aux;
548 add_path_to_peer(peer_info, path);
553 * Check if client has registered with the service and has not disconnected
554 * @param client the client to check
555 * @return non-NULL if client exists in the global DLL
557 static struct MeshClient *
558 retrieve_client (struct GNUNET_SERVER_Client *client)
560 struct MeshClient *c;
564 if (c->handle == client) return c;
572 * Checks if a given client has subscribed to certain message type
573 * @param message_type Type of message to check
574 * @param c Client to check
575 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
577 static int /* FIXME inline? */
578 is_client_subscribed(uint16_t message_type, struct MeshClient *c)
582 for (i = 0; i < c->type_counter; i++) {
583 if (c->types[i] == message_type) return GNUNET_YES;
590 * Search for a tunnel among the tunnels for a client
591 * @param client the client whose tunnels to search in
592 * @param tid the local id of the tunnel
593 * @return tunnel handler, NULL if doesn't exist
595 static struct MeshTunnel *
596 retrieve_tunnel_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
598 GNUNET_HashCode hash;
600 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
601 return GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
605 * Search for a tunnel by global ID using PEER_ID
606 * @param pi owner of the tunnel
607 * @param tid global tunnel number
608 * @return tunnel handler, NULL if doesn't exist
610 static struct MeshTunnel *
611 retrieve_tunnel_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
613 struct MESH_TunnelID id;
614 GNUNET_HashCode hash;
619 GNUNET_CRYPTO_hash(&id, sizeof(struct MESH_TunnelID), &hash);
620 return GNUNET_CONTAINER_multihashmap_get(tunnels, &hash);
626 * Search for a tunnel by global ID using full PeerIdentities
627 * @param oid owner of the tunnel
628 * @param tid global tunnel number
629 * @return tunnel handler, NULL if doesn't exist
631 static struct MeshTunnel *
632 retrieve_tunnel (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
634 return retrieve_tunnel_by_pi(GNUNET_PEER_search(oid), tid);
639 * Destroy the path and free any allocated resources linked to it
640 * @param t tunnel the path belongs to
641 * @param p the path to destroy
642 * @return GNUNET_OK on success
645 destroy_path(struct MeshPath *p)
647 GNUNET_PEER_decrement_rcs(p->peers, p->length);
648 GNUNET_free(p->peers);
655 * Destroy the peer_info and free any allocated resources linked to it
656 * @param t tunnel the path belongs to
657 * @param pi the peer_info to destroy
658 * @return GNUNET_OK on success
661 destroy_peer_info(struct MeshPeerInfo *pi)
663 GNUNET_HashCode hash;
664 struct GNUNET_PeerIdentity id;
666 GNUNET_PEER_resolve(pi->id, &id);
667 GNUNET_PEER_change_rc(pi->id, -1);
668 GNUNET_CRYPTO_hash(&id, sizeof(struct GNUNET_PeerIdentity), &hash);
670 GNUNET_CONTAINER_multihashmap_remove(peers, &hash, pi);
678 * Destroy the tunnel and free any allocated resources linked to it
679 * @param c client the tunnel belongs to
680 * @param t the tunnel to destroy
681 * @return GNUNET_OK on success
684 destroy_tunnel(struct MeshTunnel *t)
686 struct MeshClient *c;
687 GNUNET_HashCode hash;
690 if (NULL == t) return GNUNET_OK;
694 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
695 if(GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t)) {
699 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
701 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t))
709 /******************************************************************************/
710 /**************** MESH NETWORK HANDLER HELPERS ***********************/
711 /******************************************************************************/
714 * Function called to notify a client about the socket
715 * being ready to queue more data. "buf" will be
716 * NULL and "size" zero if the socket was closed for
717 * writing in the meantime.
720 * @param size number of bytes available in buf
721 * @param buf where the callee should write the message
722 * @return number of bytes written to buf
725 send_core_create_path_for_peer (void *cls, size_t size, void *buf)
727 struct MeshPeerInfo *peer_info = cls;
728 struct GNUNET_MESH_ManipulatePath *msg;
730 struct GNUNET_PeerIdentity *peer_ptr;
731 struct GNUNET_PeerIdentity id;
735 if (0 == size && NULL == buf) {
736 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Retransmitting create path\n");
737 GNUNET_PEER_resolve(get_first_hop(peer_info->path), &id);
738 GNUNET_CORE_notify_transmit_ready(core_handle,
741 GNUNET_TIME_UNIT_FOREVER_REL,
743 sizeof(struct GNUNET_MESH_ManipulatePath)
744 + (peer_info->path->length
745 * sizeof (struct GNUNET_PeerIdentity)),
746 &send_core_create_path_for_peer,
757 if (p == NULL) return 0; // TODO Notify ERROR Path not found
759 size_needed = sizeof(struct GNUNET_MESH_ManipulatePath)
760 + p->length * sizeof(struct GNUNET_PeerIdentity);
761 if (size < size_needed) {
762 // TODO retry? cancel?
766 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
767 msg->header.size = htons(size_needed);
768 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
770 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
771 for (i = 0; i < p->length; i++) {
772 GNUNET_PEER_resolve(p->peers[i], peer_ptr++);
775 peer_info->state = MESH_PEER_WAITING;
783 * Function called to notify a client about the socket
784 * being ready to queue more data. "buf" will be
785 * NULL and "size" zero if the socket was closed for
786 * writing in the meantime.
788 * @param cls closure (MeshDataDescriptor with all info to build packet)
789 * @param size number of bytes available in buf
790 * @param buf where the callee should write the message
791 * @return number of bytes written to buf
794 send_core_data_to_origin (void *cls, size_t size, void *buf)
796 struct MeshDataDescriptor *info = cls;
797 struct GNUNET_MESH_DataMessageToOrigin *msg = buf;
800 GNUNET_assert(NULL != info);
801 total_size = sizeof(struct GNUNET_MESH_DataMessageToOrigin) + info->size;
802 GNUNET_assert(total_size < 65536); /* UNIT16_MAX */
804 if (total_size > size) {
805 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
806 "not enough buffer to send data to origin\n");
809 msg->header.size = htons(total_size);
810 msg->header.type = htons(GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
811 GNUNET_PEER_resolve(info->origin->oid, &msg->oid);
812 msg->tid = htonl(info->origin->tid);
813 if (0 != info->size) {
814 memcpy(&msg[1], &info[1], info->size);
816 if (NULL != info->client) {
817 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
825 * Function called to notify a client about the socket
826 * being ready to queue more data. "buf" will be
827 * NULL and "size" zero if the socket was closed for
828 * writing in the meantime.
830 * @param cls closure (data itself)
831 * @param size number of bytes available in buf
832 * @param buf where the callee should write the message
833 * @return number of bytes written to buf
836 send_core_data_to_peer (void *cls, size_t size, void *buf)
838 struct MeshDataDescriptor *info = cls;
839 struct GNUNET_MESH_DataMessageFromOrigin *msg = buf;
842 GNUNET_assert(NULL != info);
843 total_size = sizeof(struct GNUNET_MESH_DataMessageFromOrigin) + info->size;
844 GNUNET_assert(total_size < 65536); /* UNIT16_MAX */
846 if (total_size > size) {
847 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
848 "not enough buffer to send data to peer\n");
851 msg->header.size = htons(total_size);
852 msg->header.type = htons(GNUNET_MESSAGE_TYPE_DATA_MESSAGE_FROM_ORIGIN);
853 GNUNET_PEER_resolve(info->origin->oid, &msg->oid);
854 GNUNET_PEER_resolve(info->destination, &msg->destination);
855 msg->tid = htonl(info->origin->tid);
856 if (0 != info->size) {
857 memcpy(&msg[1], &info[1], info->size);
859 if (NULL != info->client) {
860 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
868 * Function called to notify a client about the socket
869 * being ready to queue more data. "buf" will be
870 * NULL and "size" zero if the socket was closed for
871 * writing in the meantime.
873 * @param cls closure (data itself)
874 * @param size number of bytes available in buf
875 * @param buf where the callee should write the message
876 * @return number of bytes written to buf
879 send_core_data_multicast (void *cls, size_t size, void *buf)
881 struct MeshDataDescriptor *info = cls;
882 struct GNUNET_MESH_DataMessageMulticast *msg = buf;
885 GNUNET_assert(NULL != info);
886 total_size = info->size + sizeof(struct GNUNET_MESH_DataMessageMulticast);
887 GNUNET_assert(total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
890 info->peer->core_transmit[info->handler_n] = NULL;
892 if (total_size > size) {
893 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
894 "not enough buffer to send data futher\n");
897 msg->header.type = htons(GNUNET_MESSAGE_TYPE_DATA_MULTICAST);
898 msg->header.size = htons(total_size);
899 GNUNET_PEER_resolve(info->origin->oid, &msg->oid);
900 msg->tid = htonl(info->origin->tid);
901 memcpy(&msg[1], &info[1], total_size);
902 if (0 == --info->copies) {
903 if (NULL != info->client) {
904 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
913 * Function called to notify a client about the socket
914 * being ready to queue more data. "buf" will be
915 * NULL and "size" zero if the socket was closed for
916 * writing in the meantime.
918 * @param cls closure (MeshDataDescriptor)
919 * @param size number of bytes available in buf
920 * @param buf where the callee should write the message
921 * @return number of bytes written to buf
924 send_core_path_ack (void *cls, size_t size, void *buf) {
925 struct MeshDataDescriptor *info = cls;
926 struct GNUNET_MESH_PathACK *msg = buf;
928 GNUNET_assert(NULL != info);
930 info->peer->core_transmit[info->handler_n] = NULL;
932 if (sizeof(struct GNUNET_MESH_PathACK) > size) {
936 msg->header.size = htons(sizeof(struct GNUNET_MESH_PathACK));
937 msg->header.type = htons(GNUNET_MESSAGE_TYPE_PATH_ACK);
938 GNUNET_PEER_resolve(info->origin->oid, &msg->oid);
939 msg->tid = htonl(info->origin->tid);
940 GNUNET_PEER_resolve(myid, &msg->peer_id);
941 /* TODO add signature */
943 return sizeof(struct GNUNET_MESH_PathACK);
948 * Function called to notify a client about the socket
949 * being ready to queue more data. "buf" will be
950 * NULL and "size" zero if the socket was closed for
951 * writing in the meantime.
953 * @param cls closure (data itself)
954 * @param size number of bytes available in buf
955 * @param buf where the callee should write the message
956 * @return number of bytes written to buf
959 send_core_data_raw (void *cls, size_t size, void *buf)
961 struct GNUNET_MessageHeader *msg = cls;
964 GNUNET_assert(NULL != msg);
965 total_size = ntohs(msg->size);
967 if (total_size > size) {
971 memcpy(buf, msg, total_size);
979 * Send another peer a notification to destroy a tunnel
980 * @param cls The tunnel to destroy
981 * @param size Size in the buffer
982 * @param buf Memory where to put the data to transmit
983 * @return Size of data put in buffer
986 send_p2p_tunnel_destroy(void *cls, size_t size, void *buf)
988 struct MeshTunnel *t = cls;
989 struct MeshClient *c;
990 struct GNUNET_MESH_TunnelMessage *msg;
994 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY); /*FIXME*/
995 msg->header.size = htons(sizeof(struct GNUNET_MESH_TunnelMessage));
996 msg->tunnel_id = htonl(t->id.tid);
998 destroy_tunnel(c, t);
999 return sizeof(struct GNUNET_MESH_TunnelMessage);
1005 * Send the message to all clients that have subscribed to its type
1007 * @param msg Pointer to the message itself
1008 * @return number of clients this message was sent to
1011 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1013 struct MeshClient *c;
1017 type = ntohs(msg->type);
1018 for (count = 0, c = clients; c != NULL; c = c->next) {
1019 if (is_client_subscribed(type, c)) {
1021 GNUNET_SERVER_notification_context_unicast(nc,
1032 * Iterator over hash map peer entries collect all neighbors who to resend the
1035 * @param cls closure (**GNUNET_PEER_Id to store hops to send packet)
1036 * @param key current key code (peer id hash)
1037 * @param value value in the hash map (peer_info)
1038 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
1040 static int iterate_collect_neighbors (void *cls,
1041 const GNUNET_HashCode * key,
1044 struct MeshPeerInfo *peer_info = value;
1045 GNUNET_PEER_Id **neighbors = cls;
1049 if (peer_info->id == myid) {
1052 id = get_first_hop(peer_info->path);
1053 for (i = 0; *neighbors[i] != 0; i++) {
1054 if (*neighbors[i] == id) return GNUNET_YES;
1056 *neighbors = GNUNET_realloc(*neighbors, (i + 2) * sizeof(GNUNET_PEER_Id));
1058 *neighbors[i + 1] = 0;
1064 /******************************************************************************/
1065 /******************** MESH NETWORK HANDLERS **************************/
1066 /******************************************************************************/
1070 * Core handler for path creation
1071 * struct GNUNET_CORE_MessageHandler
1073 * @param cls closure
1074 * @param message message
1075 * @param peer peer identity this notification is about
1076 * @param atsi performance data
1077 * @return GNUNET_OK to keep the connection open,
1078 * GNUNET_SYSERR to close it (signal serious error)
1082 handle_mesh_path_create (void *cls,
1083 const struct GNUNET_PeerIdentity *peer,
1084 const struct GNUNET_MessageHeader *message,
1085 const struct GNUNET_TRANSPORT_ATS_Information
1088 unsigned int own_pos;
1091 MESH_TunnelNumber tid;
1092 struct GNUNET_MESH_ManipulatePath *msg;
1093 struct GNUNET_PeerIdentity *pi;
1094 struct GNUNET_PeerIdentity id;
1095 GNUNET_HashCode hash;
1096 struct MeshPath *path;
1097 struct MeshPeerInfo *dest_peer_info;
1098 struct MeshPeerInfo *orig_peer_info;
1099 struct MeshTunnel *t;
1102 size = ntohs(message->size);
1103 if (size < sizeof(struct GNUNET_MESH_ManipulatePath)) {
1104 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1105 "received create path message too short\n");
1109 size -= sizeof(struct GNUNET_MESH_ManipulatePath);
1110 if (size < 2 * sizeof(struct GNUNET_PeerIdentity)) {
1111 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1112 "create path message lacks enough peers\n");
1115 if (size % sizeof(struct GNUNET_PeerIdentity)) {
1116 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1117 "create path message of wrong size\n");
1120 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1121 size /= sizeof(struct GNUNET_PeerIdentity);
1123 tid = ntohl(msg->tid);
1124 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1125 t = retrieve_tunnel(pi, tid);
1128 t = GNUNET_malloc(sizeof(struct MeshTunnel));
1129 t->id.oid = GNUNET_PEER_intern(pi);
1133 t->peers = GNUNET_CONTAINER_multihashmap_create(32);
1135 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
1137 GNUNET_CONTAINER_multihashmap_put(tunnels,
1140 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1142 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1143 "create path: could not store tunnel in hashmap\n");
1148 dest_peer_info = GNUNET_CONTAINER_multihashmap_get(peers,
1149 &pi[size - 1].hashPubKey);
1150 if (NULL == dest_peer_info) {
1151 dest_peer_info = GNUNET_malloc(sizeof(struct MeshPeerInfo));
1152 dest_peer_info->id = GNUNET_PEER_intern(&pi[size - 1]);
1153 dest_peer_info->state = MESH_PEER_WAITING;
1154 GNUNET_CONTAINER_multihashmap_put(peers,
1155 &pi[size - 1].hashPubKey,
1157 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1159 orig_peer_info = GNUNET_CONTAINER_multihashmap_get(peers, &pi->hashPubKey);
1160 if (NULL == orig_peer_info) {
1161 orig_peer_info = GNUNET_malloc(sizeof(struct MeshPeerInfo));
1162 orig_peer_info->id = GNUNET_PEER_intern(pi);
1163 orig_peer_info->state = MESH_PEER_WAITING;
1164 GNUNET_CONTAINER_multihashmap_put(peers,
1167 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1171 path = GNUNET_malloc(sizeof(struct MeshPath));
1172 path->length = size;
1173 path->peers = GNUNET_malloc(size * sizeof(GNUNET_PEER_Id));
1175 for (i = 0; i < size; i++) {
1176 path->peers[i] = GNUNET_PEER_intern(&pi[i]);
1177 if (path->peers[i] == myid) own_pos = i;
1179 if (own_pos == 0) { /* cannot be self, must be 'not found' */
1180 /* create path: self not found in path through self */
1183 /* FIXME error. destroy tunnel? leave for timeout? */
1186 if (own_pos == size - 1) { /* it is for us! */
1187 struct MeshDataDescriptor *info;
1190 add_path_to_origin(orig_peer_info, path); /* inverts path! */
1191 GNUNET_PEER_resolve(get_first_hop(path), &id); /* path is inverted :) */
1192 info = GNUNET_malloc(sizeof(struct MeshDataDescriptor));
1193 info->origin = &t->id;
1194 info->peer = GNUNET_CONTAINER_multihashmap_get(peers, &id.hashPubKey);
1195 GNUNET_assert(info->peer);
1196 for (j = 0; info->peer->core_transmit[j]; j++) {
1202 info->handler_n = j;
1203 info->peer->core_transmit[j] = GNUNET_CORE_notify_transmit_ready(
1207 GNUNET_TIME_UNIT_FOREVER_REL,
1209 sizeof(struct GNUNET_MessageHeader),
1210 &send_core_path_ack,
1213 add_path_to_peer(dest_peer_info, path);
1214 GNUNET_PEER_resolve(get_first_hop(path), &id);
1215 GNUNET_CORE_notify_transmit_ready(core_handle,
1218 GNUNET_TIME_UNIT_FOREVER_REL,
1220 sizeof(struct GNUNET_MessageHeader),
1221 &send_core_create_path_for_peer,
1229 * Core handler for mesh network traffic going from the origin to a peer
1231 * @param cls closure
1232 * @param message message
1233 * @param peer peer identity this notification is about
1234 * @param atsi performance data
1235 * @return GNUNET_OK to keep the connection open,
1236 * GNUNET_SYSERR to close it (signal serious error)
1239 handle_mesh_data_unicast (void *cls,
1240 const struct GNUNET_PeerIdentity *peer,
1241 const struct GNUNET_MessageHeader *message,
1242 const struct GNUNET_TRANSPORT_ATS_Information
1245 struct GNUNET_MESH_DataMessageFromOrigin *msg;
1246 struct GNUNET_PeerIdentity id;
1247 struct MeshTunnel *t;
1248 struct MeshPeerInfo *pi;
1251 size = ntohs(message->size);
1252 if (size < sizeof(struct GNUNET_MESH_DataMessageFromOrigin)
1253 + sizeof(struct GNUNET_MessageHeader))
1258 msg = (struct GNUNET_MESH_DataMessageFromOrigin *) message;
1259 t = retrieve_tunnel(&msg->oid, ntohl(msg->tid));
1261 /* TODO notify back: we don't know this tunnel */
1264 pi = GNUNET_CONTAINER_multihashmap_get(t->peers,
1265 &msg->destination.hashPubKey);
1267 /* TODO maybe feedback, log to statistics */
1270 if (pi->id == myid) {
1271 send_subscribed_clients((struct GNUNET_MessageHeader *)&msg[1]);
1274 GNUNET_PEER_resolve(get_first_hop(pi->path), &id);
1275 msg = GNUNET_malloc(size);
1276 memcpy(msg, message, size);
1277 GNUNET_CORE_notify_transmit_ready(core_handle,
1280 GNUNET_TIME_UNIT_FOREVER_REL,
1283 &send_core_data_raw,
1290 * Core handler for mesh network traffic going from the origin to all peers
1292 * @param cls closure
1293 * @param message message
1294 * @param peer peer identity this notification is about
1295 * @param atsi performance data
1296 * @return GNUNET_OK to keep the connection open,
1297 * GNUNET_SYSERR to close it (signal serious error)
1300 handle_mesh_data_multicast (void *cls,
1301 const struct GNUNET_PeerIdentity *peer,
1302 const struct GNUNET_MessageHeader *message,
1303 const struct GNUNET_TRANSPORT_ATS_Information
1306 struct GNUNET_MESH_DataMessageMulticast *msg;
1307 struct GNUNET_PeerIdentity id;
1308 struct MeshTunnel *t;
1309 struct MeshDataDescriptor *info;
1310 GNUNET_PEER_Id *neighbors;
1316 size = ntohs(message->size);
1317 if (size < sizeof(struct GNUNET_MESH_DataMessageMulticast)
1318 + sizeof(struct GNUNET_MessageHeader))
1320 GNUNET_break_op (0);
1323 msg = (struct GNUNET_MESH_DataMessageMulticast *) message;
1324 t = retrieve_tunnel(&msg->oid, ntohl(msg->tid));
1327 /* TODO notify that we dont know that tunnel */
1331 /* Transmit to locally interested clients */
1332 GNUNET_PEER_resolve(myid, &id);
1333 if (GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey)) {
1334 send_subscribed_clients((struct GNUNET_MessageHeader *)&msg[1]);
1337 /* Retransmit to other peers */
1338 neighbors = GNUNET_malloc(sizeof(GNUNET_PEER_Id));
1340 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
1341 &iterate_collect_neighbors,
1343 if (!neighbors[0]) {
1346 size -= sizeof(struct GNUNET_MESH_DataMessageMulticast);
1347 info = GNUNET_malloc(sizeof(struct MeshDataDescriptor) + size);
1348 info->origin = &t->id;
1350 for (i = 0; 0 != neighbors[i]; i++) {
1351 GNUNET_PEER_resolve(neighbors[i], &id);
1353 info->destination = neighbors[i];
1354 info->peer = GNUNET_CONTAINER_multihashmap_get(peers, &id.hashPubKey);
1355 GNUNET_assert(info->peer);
1356 for (j = 0; info->peer->core_transmit[j]; j++) {
1362 info->handler_n = j;
1363 info->peer->infos[j] = info;
1364 info->peer->core_transmit[j] = GNUNET_CORE_notify_transmit_ready(
1368 GNUNET_TIME_UNIT_FOREVER_REL,
1370 ntohs(msg->header.size),
1371 &send_core_data_multicast,
1379 * Core handler for mesh network traffic
1381 * @param cls closure
1382 * @param message message
1383 * @param peer peer identity this notification is about
1384 * @param atsi performance data
1385 * @return GNUNET_OK to keep the connection open,
1386 * GNUNET_SYSERR to close it (signal serious error)
1389 handle_mesh_data_to_orig (void *cls,
1390 const struct GNUNET_PeerIdentity *peer,
1391 const struct GNUNET_MessageHeader *message,
1392 const struct GNUNET_TRANSPORT_ATS_Information
1395 struct GNUNET_MESH_DataMessageToOrigin *msg;
1396 struct GNUNET_PeerIdentity id;
1397 struct MeshTunnel *t;
1398 struct MeshPeerInfo *peer_info;
1401 size = ntohs(message->size);
1402 if (size < sizeof(struct GNUNET_MESH_DataMessageToOrigin)
1403 + sizeof(struct GNUNET_MessageHeader))
1405 GNUNET_break_op (0);
1408 msg = (struct GNUNET_MESH_DataMessageToOrigin *) message;
1409 t = retrieve_tunnel(&msg->oid, ntohl(msg->tid));
1412 /* TODO notify that we dont know this tunnel (whom)? */
1416 if (t->id.oid == myid) {
1417 if (NULL == t->client) {
1418 /* got data packet for ownerless tunnel */
1419 GNUNET_break_op (0);
1422 GNUNET_SERVER_notification_context_unicast(nc,
1428 peer_info = get_peer_info(&msg->oid);
1429 if (NULL == peer_info) {
1430 /* unknown origin of tunnel */
1434 GNUNET_PEER_resolve(get_first_hop(peer_info->path), &id);
1435 msg = GNUNET_malloc(size);
1436 memcpy(msg, message, size);
1437 GNUNET_CORE_notify_transmit_ready(core_handle,
1440 GNUNET_TIME_UNIT_FOREVER_REL,
1443 &send_core_data_raw,
1451 * Core handler for path ACKs
1453 * @param cls closure
1454 * @param message message
1455 * @param peer peer identity this notification is about
1456 * @param atsi performance data
1457 * @return GNUNET_OK to keep the connection open,
1458 * GNUNET_SYSERR to close it (signal serious error)
1461 handle_mesh_path_ack (void *cls,
1462 const struct GNUNET_PeerIdentity *peer,
1463 const struct GNUNET_MessageHeader *message,
1464 const struct GNUNET_TRANSPORT_ATS_Information
1467 struct GNUNET_MESH_PathACK *msg;
1468 struct GNUNET_PeerIdentity id;
1469 struct MeshTunnel *t;
1470 struct MeshPeerInfo *peer_info;
1472 msg = (struct GNUNET_MESH_PathACK *) message;
1473 t = retrieve_tunnel(&msg->oid, msg->tid);
1475 /* TODO notify that we don't know the tunnel */
1479 /* Message for us? */
1480 if (GNUNET_PEER_search(&msg->oid) == myid) {
1481 struct GNUNET_MESH_PeerControl pc;
1482 if (NULL == t->client) {
1486 peer_info = get_peer_info(&msg->peer_id);
1487 if (NULL == peer_info) {
1491 peer_info->state = MESH_PEER_READY;
1492 pc.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED);
1493 pc.header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
1494 pc.tunnel_id = htonl(t->local_tid);
1495 GNUNET_PEER_resolve(peer_info->id, &pc.peer);
1496 GNUNET_SERVER_notification_context_unicast(nc,
1503 peer_info = get_peer_info(&msg->oid);
1504 if (NULL == peer_info) {
1505 /* If we know the tunnel, we should DEFINITELY know the peer */
1509 GNUNET_PEER_resolve(get_first_hop(peer_info->path), &id);
1510 msg = GNUNET_malloc(sizeof(struct GNUNET_MESH_PathACK));
1511 memcpy(msg, message, sizeof(struct GNUNET_MESH_PathACK));
1512 GNUNET_CORE_notify_transmit_ready(core_handle,
1515 GNUNET_TIME_UNIT_FOREVER_REL,
1517 sizeof(struct GNUNET_MESH_PathACK),
1518 &send_core_data_raw,
1525 * Functions to handle messages from core
1527 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1528 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1529 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_DATA_MESSAGE_FROM_ORIGIN, 0},
1530 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_DATA_MULTICAST, 0},
1531 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN, 0},
1532 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_PATH_ACK,
1533 sizeof(struct GNUNET_MESH_PathACK)},
1539 /******************************************************************************/
1540 /**************** MESH LOCAL HANDLER HELPERS ***********************/
1541 /******************************************************************************/
1544 * delete_tunnel_entry: iterator for deleting each tunnel that belongs to a
1545 * client when the client disconnects.
1546 * @param cls closure (client that is disconnecting)
1547 * @param key the hash of the local tunnel id (used to access the hashmap)
1548 * @param value the value stored at the key (tunnel to destroy)
1549 * @return GNUNET_OK on success
1552 delete_tunnel_entry (void *cls, const GNUNET_HashCode * key, void *value) {
1554 r = destroy_tunnel((struct MeshTunnel *) value);
1560 * notify_client_connection_failure: notify a client that the connection to the
1561 * requested remote peer is not possible (for instance, no route found)
1562 * Function called when the socket is ready to queue more data. "buf" will be
1563 * NULL and "size" zero if the socket was closed for writing in the meantime.
1565 * @param cls closure
1566 * @param size number of bytes available in buf
1567 * @param buf where the callee should write the message
1568 * @return number of bytes written to buf
1571 notify_client_connection_failure (void *cls, size_t size, void *buf)
1574 struct MeshPeerInfo *peer_info;
1575 struct GNUNET_MESH_PeerControl *msg;
1576 struct GNUNET_PeerIdentity id;
1578 if (0 == size && NULL == buf) {
1579 // TODO retry? cancel?
1583 size_needed = sizeof(struct GNUNET_MESH_PeerControl);
1584 peer_info = (struct MeshPeerInfo *) cls;
1585 msg = (struct GNUNET_MESH_PeerControl *) buf;
1586 msg->header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
1587 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
1588 // msg->tunnel_id = htonl(peer_info->t->tid);
1589 GNUNET_PEER_resolve(peer_info->id, &id);
1590 memcpy(&msg->peer, &id, sizeof(struct GNUNET_PeerIdentity));
1598 * Send keepalive packets for a peer
1604 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1606 struct MeshPeerInfo *peer_info = cls;
1607 struct GNUNET_PeerIdentity id;
1609 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) return;
1610 GNUNET_PEER_resolve(get_first_hop(peer_info->path), &id);
1611 GNUNET_CORE_notify_transmit_ready(core_handle,
1614 GNUNET_TIME_UNIT_FOREVER_REL,
1616 sizeof(struct GNUNET_MESH_ManipulatePath)
1617 + (peer_info->path->length
1618 * sizeof (struct GNUNET_PeerIdentity)),
1619 &send_core_create_path_for_peer,
1627 * Function to process paths received for a new peer addition. The recorded
1628 * paths form the initial tunnel, which can be optimized later.
1629 * Called on each result obtained for the DHT search.
1631 * @param cls closure
1632 * @param exp when will this value expire
1633 * @param key key of the result
1634 * @param get_path NULL-terminated array of pointers
1635 * to the peers on reverse GET path (or NULL if not recorded)
1636 * @param put_path NULL-terminated array of pointers
1637 * to the peers on the PUT path (or NULL if not recorded)
1638 * @param type type of the result
1639 * @param size number of bytes in data
1640 * @param data pointer to the result data
1643 dht_get_response_handler(void *cls,
1644 struct GNUNET_TIME_Absolute exp,
1645 const GNUNET_HashCode * key,
1646 const struct GNUNET_PeerIdentity * const *get_path,
1647 const struct GNUNET_PeerIdentity * const *put_path,
1648 enum GNUNET_BLOCK_Type type,
1652 struct MeshPeerInfo *peer_info = cls;
1654 struct GNUNET_PeerIdentity pi;
1657 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path) {
1658 // Find ourselves some alternate initial path to the destination: retry
1659 GNUNET_DHT_get_stop(peer_info->dhtget);
1660 GNUNET_PEER_resolve(peer_info->id, &pi);
1661 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
1662 GNUNET_TIME_UNIT_FOREVER_REL,
1663 GNUNET_BLOCK_TYPE_ANY,
1665 4, /* replication level */
1666 GNUNET_DHT_RO_RECORD_ROUTE,
1667 NULL, /* bloom filter */
1670 0, /* xquery bits */
1671 dht_get_response_handler,
1675 p = GNUNET_malloc(sizeof(struct MeshPath));
1676 for (i = 0; get_path[i] != NULL; i++);
1677 for (i--; i >= 0; i--) {
1678 p->peers = GNUNET_realloc(p->peers,
1679 sizeof(GNUNET_PEER_Id) * (p->length + 1));
1680 p->peers[p->length] = GNUNET_PEER_intern(get_path[i]);
1683 for (i = 0; put_path[i] != NULL; i++);
1684 for (i--; i >= 0; i--) {
1685 p->peers = GNUNET_realloc(p->peers,
1686 sizeof(GNUNET_PEER_Id) * (p->length + 1));
1687 p->peers[p->length] = GNUNET_PEER_intern(put_path[i]);
1690 add_path_to_peer(peer_info, p);
1691 GNUNET_CORE_notify_transmit_ready(core_handle,
1694 GNUNET_TIME_UNIT_FOREVER_REL,
1696 sizeof(struct GNUNET_MESH_ManipulatePath)
1698 * sizeof (struct GNUNET_PeerIdentity)),
1699 &send_core_create_path_for_peer,
1701 GNUNET_SCHEDULER_add_delayed(REFRESH_PATH_TIME, &path_refresh, peer_info);
1706 /******************************************************************************/
1707 /********************* MESH LOCAL HANDLES **************************/
1708 /******************************************************************************/
1712 * Handler for client disconnection
1714 * @param cls closure
1715 * @param client identification of the client; NULL
1716 * for the last call when the server is destroyed
1719 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
1721 struct MeshClient *c;
1722 struct MeshClient *next;
1724 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1725 "client disconnected\n");
1728 if (c->handle == client) {
1729 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1730 " matching client found, cleaning\n");
1731 GNUNET_CONTAINER_multihashmap_iterate(c->tunnels,
1732 &delete_tunnel_entry,
1734 GNUNET_CONTAINER_multihashmap_destroy(c->tunnels);
1735 if(0 != c->app_counter) GNUNET_free (c->apps);
1736 if(0 != c->type_counter) GNUNET_free (c->types);
1737 GNUNET_CONTAINER_DLL_remove(clients, clients_tail, c);
1742 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1743 " ... searching\n");
1748 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1755 * Handler for new clients
1757 * @param cls closure
1758 * @param client identification of the client
1759 * @param message the actual message, which includes messages the client wants
1762 handle_local_new_client (void *cls,
1763 struct GNUNET_SERVER_Client *client,
1764 const struct GNUNET_MessageHeader *message)
1766 struct GNUNET_MESH_ClientConnect *cc_msg;
1767 struct MeshClient *c;
1772 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
1773 /* Check data sanity */
1774 size = ntohs(message->size) - sizeof(struct GNUNET_MESH_ClientConnect);
1775 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
1776 types = ntohs(cc_msg->types);
1777 apps = ntohs(cc_msg->applications);
1779 types * sizeof(uint16_t) + apps * sizeof(GNUNET_MESH_ApplicationType))
1782 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1786 /* Create new client structure */
1787 c = GNUNET_malloc(sizeof(struct MeshClient));
1790 c->type_counter = types;
1791 c->types = GNUNET_malloc(types * sizeof(uint16_t));
1792 memcpy(c->types, &message[1], types * sizeof(uint16_t));
1795 c->app_counter = apps;
1796 c->apps = GNUNET_malloc(apps * sizeof(GNUNET_MESH_ApplicationType));
1798 &message[1] + types * sizeof(uint16_t),
1799 apps * sizeof(GNUNET_MESH_ApplicationType));
1801 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1802 " client has %u+%u subscriptions\n",
1806 GNUNET_CONTAINER_DLL_insert(clients, clients_tail, c);
1807 c->tunnels = GNUNET_CONTAINER_multihashmap_create(32);
1808 GNUNET_SERVER_notification_context_add(nc, client);
1810 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1816 * Handler for requests of new tunnels
1818 * @param cls closure
1819 * @param client identification of the client
1820 * @param message the actual message
1823 handle_local_tunnel_create (void *cls,
1824 struct GNUNET_SERVER_Client *client,
1825 const struct GNUNET_MessageHeader *message)
1827 struct GNUNET_MESH_TunnelMessage *t_msg;
1828 struct MeshTunnel *t;
1829 struct MeshClient *c;
1830 GNUNET_HashCode hash;
1832 /* Sanity check for client registration */
1833 if (NULL == (c = retrieve_client(client))) {
1835 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1839 /* Message sanity check */
1840 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
1842 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1846 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
1847 /* Sanity check for tunnel numbering */
1848 if (0 == (ntohl(t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)) {
1850 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1853 /* Sanity check for duplicate tunnel IDs */
1854 if(NULL != retrieve_tunnel_by_local_id(c, ntohl(t_msg->tunnel_id))) {
1856 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1860 t = GNUNET_malloc(sizeof(struct MeshTunnel));
1861 while (NULL != retrieve_tunnel_by_pi(myid, next_tid))
1862 next_tid = (next_tid + 1) % GNUNET_MESH_LOCAL_TUNNEL_ID_MARK;
1863 t->id.tid = next_tid++;
1865 t->local_tid = ntohl(t_msg->tunnel_id);
1867 t->peers = GNUNET_CONTAINER_multihashmap_create(32);
1869 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
1871 GNUNET_CONTAINER_multihashmap_put(c->tunnels, &hash, t,
1872 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1875 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1879 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
1881 GNUNET_CONTAINER_multihashmap_put(tunnels, &hash, t,
1882 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1885 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1889 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1895 * Handler for requests of deleting tunnels
1897 * @param cls closure
1898 * @param client identification of the client
1899 * @param message the actual message
1902 handle_local_tunnel_destroy (void *cls,
1903 struct GNUNET_SERVER_Client *client,
1904 const struct GNUNET_MessageHeader *message)
1906 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
1907 struct MeshClient *c;
1908 struct MeshTunnel *t;
1909 MESH_TunnelNumber tid;
1910 GNUNET_HashCode hash;
1913 /* Sanity check for client registration */
1914 if (NULL == (c = retrieve_client(client))) {
1916 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1919 /* Message sanity check */
1920 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
1922 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1926 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
1928 /* Retrieve tunnel */
1929 tid = ntohl(tunnel_msg->tunnel_id);
1931 /* Remove from local id hashmap */
1932 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
1933 t = GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
1934 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t);
1936 /* Remove from global id hashmap */
1937 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
1938 GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t);
1940 // notify_tunnel_destroy(t);
1941 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1947 * Handler for connection requests to new peers
1949 * @param cls closure
1950 * @param client identification of the client
1951 * @param message the actual message (PeerControl)
1954 handle_local_connect_add (void *cls,
1955 struct GNUNET_SERVER_Client *client,
1956 const struct GNUNET_MessageHeader *message)
1958 struct GNUNET_MESH_PeerControl *peer_msg;
1959 struct MeshClient *c;
1960 struct MeshTunnel *t;
1961 MESH_TunnelNumber tid;
1962 struct MeshPeerInfo *peer_info;
1965 /* Sanity check for client registration */
1966 if (NULL == (c = retrieve_client(client))) {
1968 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1972 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1973 /* Sanity check for message size */
1974 if (sizeof(struct GNUNET_MESH_PeerControl)
1975 != ntohs(peer_msg->header.size))
1978 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1982 /* Tunnel exists? */
1983 tid = ntohl(peer_msg->tunnel_id);
1984 t = retrieve_tunnel_by_local_id(c, tid);
1987 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1991 /* Does client own tunnel? */
1992 if (t->client->handle != client) {
1994 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1999 peer_info = get_peer_info(&peer_msg->peer);
2001 /* Start DHT search if needed */
2002 if(MESH_PEER_READY != peer_info->state && NULL == peer_info->dhtget) {
2003 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
2004 GNUNET_TIME_UNIT_FOREVER_REL,
2005 GNUNET_BLOCK_TYPE_ANY,
2006 &peer_msg->peer.hashPubKey,
2007 4, /* replication level */
2008 GNUNET_DHT_RO_RECORD_ROUTE,
2009 NULL, /* bloom filter */
2012 0, /* xquery bits */
2013 dht_get_response_handler,
2017 GNUNET_SERVER_receive_done(client, GNUNET_OK);
2023 * Handler for disconnection requests of peers in a tunnel
2025 * @param cls closure
2026 * @param client identification of the client
2027 * @param message the actual message (PeerControl)
2030 handle_local_connect_del (void *cls,
2031 struct GNUNET_SERVER_Client *client,
2032 const struct GNUNET_MessageHeader *message)
2034 struct GNUNET_MESH_PeerControl *peer_msg;
2035 struct MeshClient *c;
2036 struct MeshTunnel *t;
2037 MESH_TunnelNumber tid;
2039 /* Sanity check for client registration */
2040 if (NULL == (c = retrieve_client(client))) {
2042 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2045 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
2046 /* Sanity check for message size */
2047 if (sizeof(struct GNUNET_MESH_PeerControl)
2048 != ntohs(peer_msg->header.size))
2051 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2055 /* Tunnel exists? */
2056 tid = ntohl(peer_msg->tunnel_id);
2057 t = retrieve_tunnel_by_local_id(c, tid);
2060 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2064 /* Does client own tunnel? */
2065 if (t->client->handle != client) {
2067 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2071 /* Ok, delete peer from tunnel */
2072 GNUNET_CONTAINER_multihashmap_remove_all(t->peers,
2073 &peer_msg->peer.hashPubKey);
2075 GNUNET_SERVER_receive_done(client, GNUNET_OK);
2081 * Handler for connection requests to new peers by type
2083 * @param cls closure
2084 * @param client identification of the client
2085 * @param message the actual message (ConnectPeerByType)
2088 handle_local_connect_by_type (void *cls,
2089 struct GNUNET_SERVER_Client *client,
2090 const struct GNUNET_MessageHeader *message)
2092 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2093 MESH_TunnelNumber tid;
2094 GNUNET_MESH_ApplicationType application;
2095 struct MeshClient *c;
2096 struct MeshTunnel *t;
2098 /* Sanity check for client registration */
2099 if (NULL == (c = retrieve_client(client))) {
2101 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2105 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *)message;
2106 /* Sanity check for message size */
2107 if (sizeof(struct GNUNET_MESH_PeerControl) !=
2108 ntohs(connect_msg->header.size))
2111 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2115 /* Tunnel exists? */
2116 tid = ntohl(connect_msg->tunnel_id);
2117 t = retrieve_tunnel_by_local_id(c, tid);
2120 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2124 /* Does client own tunnel? */
2125 if (t->client->handle != client) {
2127 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2131 /* Ok, lets find a peer offering the service */
2132 application = ntohl(connect_msg->type);
2133 application++; // FIXME silence warnings
2135 GNUNET_SERVER_receive_done(client, GNUNET_OK);
2141 * Handler for client traffic directed to one peer
2143 * @param cls closure
2144 * @param client identification of the client
2145 * @param message the actual message
2148 handle_local_network_traffic (void *cls,
2149 struct GNUNET_SERVER_Client *client,
2150 const struct GNUNET_MessageHeader *message)
2152 struct MeshClient *c;
2153 struct MeshTunnel *t;
2154 struct MeshPeerInfo *pi;
2155 struct GNUNET_MESH_Data *data_msg;
2156 struct GNUNET_PeerIdentity next_hop;
2157 struct MeshDataDescriptor *info;
2158 MESH_TunnelNumber tid;
2161 /* Sanity check for client registration */
2162 if (NULL == (c = retrieve_client(client))) {
2164 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2167 data_msg = (struct GNUNET_MESH_Data *)message;
2168 /* Sanity check for message size */
2169 if (sizeof(struct GNUNET_MESH_Data) >
2170 ntohs(data_msg->header.size))
2173 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2177 /* Tunnel exists? */
2178 tid = ntohl(data_msg->tunnel_id);
2179 t = retrieve_tunnel_by_local_id(c, tid);
2182 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2186 /* Is it a local tunnel? Then, does client own the tunnel? */
2187 if (t->client->handle != NULL && t->client->handle != client) {
2189 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2193 pi = GNUNET_CONTAINER_multihashmap_get(t->peers,
2194 &data_msg->peer_id.hashPubKey);
2195 /* Is the selected peer in the tunnel? */
2198 * Are we SO nice that we automatically try to add him to the tunnel?
2201 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2204 GNUNET_PEER_resolve(get_first_hop(pi->path), &next_hop);
2205 data_size = ntohs(message->size) - sizeof(struct GNUNET_MESH_Data);
2206 info = GNUNET_malloc(sizeof(struct MeshDataDescriptor) + data_size);
2207 memcpy(&info[1], &data_msg[1], data_size);
2208 info->destination = pi->id;
2209 info->origin = &t->id;
2210 info->size = data_size;
2211 info->client = client;
2212 GNUNET_CORE_notify_transmit_ready(core_handle,
2215 GNUNET_TIME_UNIT_FOREVER_REL,
2217 /* FIXME re-check types */
2218 message->size - sizeof(struct GNUNET_MESH_Data)
2219 + sizeof(struct GNUNET_MESH_DataMessageFromOrigin),
2220 &send_core_data_to_peer,
2226 * Handler for client traffic directed to all peers in a tunnel
2228 * @param cls closure
2229 * @param client identification of the client
2230 * @param message the actual message
2233 handle_local_network_traffic_bcast (void *cls,
2234 struct GNUNET_SERVER_Client *client,
2235 const struct GNUNET_MessageHeader *message)
2237 struct MeshClient *c;
2238 struct MeshTunnel *t;
2239 struct GNUNET_MESH_DataBroadcast *data_msg;
2240 MESH_TunnelNumber tid;
2242 /* Sanity check for client registration */
2243 if (NULL == (c = retrieve_client(client))) {
2245 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2248 data_msg = (struct GNUNET_MESH_DataBroadcast *)message;
2249 /* Sanity check for message size */
2250 if (sizeof(struct GNUNET_MESH_PeerControl)
2251 != ntohs(data_msg->header.size))
2254 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2258 /* Tunnel exists? */
2259 tid = ntohl(data_msg->tunnel_id);
2260 t = retrieve_tunnel_by_local_id(c, tid);
2263 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2267 /* Does client own tunnel? */
2268 if (t->client->handle != client) {
2270 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2276 GNUNET_SERVER_receive_done(client, GNUNET_OK);
2281 * Functions to handle messages from clients
2283 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
2284 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2285 {&handle_local_tunnel_create, NULL,
2286 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2287 sizeof(struct GNUNET_MESH_TunnelMessage)},
2288 {&handle_local_tunnel_destroy, NULL,
2289 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2290 sizeof(struct GNUNET_MESH_TunnelMessage)},
2291 {&handle_local_connect_add, NULL,
2292 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD,
2293 sizeof(struct GNUNET_MESH_PeerControl)},
2294 {&handle_local_connect_del, NULL,
2295 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL,
2296 sizeof(struct GNUNET_MESH_PeerControl)},
2297 {&handle_local_connect_by_type, NULL,
2298 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE,
2299 sizeof(struct GNUNET_MESH_ConnectPeerByType)},
2300 {&handle_local_network_traffic, NULL,
2301 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
2302 {&handle_local_network_traffic_bcast, NULL,
2303 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0},
2309 * To be called on core init/fail.
2311 * @param cls service closure
2312 * @param server handle to the server for this service
2313 * @param identity the public identity of this peer
2314 * @param publicKey the public key of this peer
2317 core_init (void *cls,
2318 struct GNUNET_CORE_Handle *server,
2319 const struct GNUNET_PeerIdentity *identity,
2320 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2324 core_handle = server;
2325 myid = GNUNET_PEER_intern(identity);
2330 * Method called whenever a given peer connects.
2332 * @param cls closure
2333 * @param peer peer identity this notification is about
2334 * @param atsi performance data for the connection
2337 core_connect (void *cls,
2338 const struct GNUNET_PeerIdentity *peer,
2339 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2341 // GNUNET_PEER_Id pid;
2342 struct MeshPeerInfo *peer_info;
2343 struct MeshPath *path;
2345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2346 "Peer connected\n");
2347 peer_info = get_peer_info(peer);
2348 if (myid == peer_info->id) {
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2352 path = GNUNET_malloc(sizeof(struct MeshPath));
2354 path->peers = GNUNET_malloc(sizeof(GNUNET_PEER_Id) * 2);
2355 path->peers[0] = myid;
2356 path->peers[1] = peer_info->id;
2357 add_path_to_peer(peer_info, path);
2362 * Method called whenever a peer disconnects.
2364 * @param cls closure
2365 * @param peer peer identity this notification is about
2368 core_disconnect (void *cls,
2370 GNUNET_PeerIdentity *peer)
2372 struct MeshPeerInfo *pi;
2375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2376 "Peer disconnected\n");
2377 pi = GNUNET_CONTAINER_multihashmap_get(peers, &peer->hashPubKey);
2382 for (i = 0; i < CORE_QUEUE_SIZE; i++) {
2383 if (pi->core_transmit[i]) {
2384 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit[i]);
2385 /* TODO: notify that tranmission has failed */
2386 GNUNET_free(pi->infos[i]);
2389 if (myid == pi->id) {
2390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2397 /******************************************************************************/
2398 /************************ MAIN FUNCTIONS ****************************/
2399 /******************************************************************************/
2402 * Task run during shutdown.
2408 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2412 if (core_handle != NULL) {
2413 GNUNET_CORE_disconnect (core_handle);
2416 if (dht_handle != NULL) {
2417 GNUNET_DHT_disconnect (dht_handle);
2421 GNUNET_SERVER_notification_context_destroy(nc);
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2429 * Process mesh requests.
2431 * @param cls closure
2432 * @param server the initialized server
2433 * @param c configuration to use
2437 struct GNUNET_SERVER_Handle *server,
2438 const struct GNUNET_CONFIGURATION_Handle *c)
2440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2441 "starting to run\n");
2442 GNUNET_SERVER_add_handlers (server, plugin_handlers);
2443 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
2444 server_handle = server;
2445 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
2446 CORE_QUEUE_SIZE, /* queue size */
2447 NULL, /* Closure passed to MESH functions */
2448 &core_init, /* Call core_init once connected */
2449 &core_connect, /* Handle connects */
2450 &core_disconnect, /* remove peers on disconnects */
2451 NULL, /* Do we care about "status" updates? */
2452 NULL, /* Don't notify about all incoming messages */
2453 GNUNET_NO, /* For header only in notification */
2454 NULL, /* Don't notify about all outbound messages */
2455 GNUNET_NO, /* For header-only out notification */
2456 core_handlers); /* Register these handlers */
2457 if (core_handle == NULL) {
2460 dht_handle = GNUNET_DHT_connect(c, 64);
2461 if (dht_handle == NULL) {
2466 tunnels = GNUNET_CONTAINER_multihashmap_create(32);
2467 peers = GNUNET_CONTAINER_multihashmap_create(32);
2468 nc = GNUNET_SERVER_notification_context_create(server_handle,
2471 clients_tail = NULL;
2473 /* Scheduled the task to clean up when shutdown is called */
2474 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2475 &shutdown_task, NULL);
2477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2482 * The main function for the mesh service.
2484 * @param argc number of arguments from the command line
2485 * @param argv command line arguments
2486 * @return 0 ok, 1 on error
2489 main (int argc, char *const *argv)
2494 GNUNET_SERVICE_run (argc,
2497 GNUNET_SERVICE_OPTION_NONE,
2498 &run, NULL)) ? 0 : 1;
2499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,