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 * - MAIN FUNCTIONS (main & run)
36 * - soft stateing (keep-alive (CHANGE?) / timeout / disconnect) -- not a message issue
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
44 #include "gnunet_common.h"
45 #include "gnunet_util_lib.h"
46 #include "gnunet_peer_lib.h"
47 #include "gnunet_core_service.h"
48 #include "gnunet_protocols.h"
51 #include "mesh_protocol.h"
52 #include "gnunet_dht_service.h"
55 /******************************************************************************/
56 /************************ DATA STRUCTURES ****************************/
57 /******************************************************************************/
60 * Information regarding a path
68 struct MeshPath *next;
71 * Whether the path is serving traffic in a tunnel or is a backup
76 * List of all the peers that form the path from origin to target
78 GNUNET_PEER_Id *peers;
81 * Number of peers (hops) in the path
88 * All the states a peer participating in a tunnel can be in.
93 * Path to the peer not known yet
98 * Request sent, not yet answered.
103 * Peer connected and ready to accept data
108 * Peer connected previosly but not responding
110 MESH_PEER_RECONNECTING
115 * Struct containing all information regarding a given peer
125 * Is the peer reachable? Is the peer even connected?
127 enum MeshPeerState state;
130 * When to try to establish contact again?
132 struct GNUNET_TIME_Absolute next_reconnect_attempt;
135 * Number of attempts to reconnect so far
137 int n_reconnect_attempts;
140 * Paths to reach the peer
142 struct MeshPath *path;
145 * Handle to stop the DHT search for a path to this peer
147 struct GNUNET_DHT_GetHandle *dhtget;
152 * Data scheduled to transmit (to local client or remote peer)
159 struct MeshQueue *next;
160 struct MeshQueue *prev;
163 * Target of the data (NULL if target is client)
165 struct MeshPeerInfo *peer;
168 * Client to send the data to (NULL if target is peer)
170 struct MeshClient *client;
173 * Size of the message to transmit
178 * How old is the data?
180 struct GNUNET_TIME_Absolute timestamp;
185 struct GNUNET_MessageHeader *data;
189 * Globally unique tunnel identification (owner + number)
190 * DO NOT USE OVER THE NETWORK
192 struct MESH_TunnelID {
194 * Node that owns the tunnel
199 * Tunnel number to differentiate all the tunnels owned by the node oid
200 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_MARK )
202 MESH_TunnelNumber tid;
206 struct MeshClient; /* FWD declaration */
208 * Struct containing all information regarding a tunnel
209 * For an intermediate node the improtant info used will be:
210 * - id Tunnel unique identification
211 * - paths[0] To know where to send it next
212 * - metainfo: ready, speeds, accounting
219 struct MESH_TunnelID id;
222 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK or 0 )
224 MESH_TunnelNumber local_tid;
227 * Last time the tunnel was used
229 struct GNUNET_TIME_Absolute timestamp;
232 * Peers in the tunnelindexed by PeerIdentity (MeshPeerInfo)
234 struct GNUNET_CONTAINER_MultiHashMap* peers;
237 * Number of peers that are connected and potentially ready to receive data
239 unsigned int peers_ready;
242 * Number of peers that have been added to the tunnel
244 unsigned int peers_total;
248 * Client owner of the tunnel, if any
250 struct MeshClient *client;
253 * Messages ready to transmit
255 struct MeshQueue *queue_head;
256 struct MeshQueue *queue_tail;
261 * Struct containing information about a client of the service
268 struct MeshClient *next;
271 * Tunnels that belong to this client, indexed by local id
273 struct GNUNET_CONTAINER_MultiHashMap* tunnels;
276 * Handle to communicate with the client
278 struct GNUNET_SERVER_Client *handle;
281 * Applications that this client has claimed to provide
283 GNUNET_MESH_ApplicationType *apps;
284 unsigned int app_counter;
287 * Messages that this client has declared interest in
290 unsigned int type_counter;
294 /******************************************************************************/
295 /*********************** GLOBAL VARIABLES ****************************/
296 /******************************************************************************/
301 static struct MeshClient *clients;
304 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
306 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
309 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
311 struct GNUNET_CONTAINER_MultiHashMap *peers;
314 * Handle to communicate with core
316 static struct GNUNET_CORE_Handle *core_handle;
321 static struct GNUNET_DHT_Handle *dht_handle;
324 * Local peer own ID (memory efficient handle)
326 static GNUNET_PEER_Id myid;
329 * Tunnel ID for the next created tunnel (global tunnel number)
331 static MESH_TunnelNumber next_tid;
333 /******************************************************************************/
334 /****************** GENERAL HELPER FUNCTIONS ************************/
335 /******************************************************************************/
338 * Check if client has registered with the service and has not disconnected
339 * @param client the client to check
340 * @return non-NULL if client exists in the global DLL
342 static struct MeshClient *
343 retrieve_client (struct GNUNET_SERVER_Client *client)
345 struct MeshClient *c;
349 if (c->handle == client) return c;
357 * Search for a tunnel among the tunnels for a client
358 * @param client the client whose tunnels to search in
359 * @param tid the local id of the tunnel
360 * @return tunnel handler, NULL if doesn't exist
362 static struct MeshTunnel *
363 retrieve_tunnel_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
365 GNUNET_HashCode hash;
367 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
368 return GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
372 * Search for a tunnel by global ID using PEER_ID
373 * @param pi owner of the tunnel
374 * @param tid global tunnel number
375 * @return tunnel handler, NULL if doesn't exist
377 static struct MeshTunnel *
378 retrieve_tunnel_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
380 struct MESH_TunnelID id;
381 GNUNET_HashCode hash;
386 GNUNET_CRYPTO_hash(&id, sizeof(struct MESH_TunnelID), &hash);
387 return GNUNET_CONTAINER_multihashmap_get(tunnels, &hash);
393 * Search for a tunnel by global ID using full PeerIdentities
394 * @param oid owner of the tunnel
395 * @param tid global tunnel number
396 * @return tunnel handler, NULL if doesn't exist
398 static struct MeshTunnel *
399 retrieve_tunnel (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
403 pi = GNUNET_PEER_intern(oid);
404 GNUNET_PEER_change_rc(pi, -1);
405 return retrieve_tunnel_by_pi(pi, tid);
410 * Destroy the path and free any allocated resources linked to it
411 * @param t tunnel the path belongs to
412 * @param p the path to destroy
413 * @return GNUNET_OK on success
416 destroy_path(struct MeshTunnel *t, struct MeshPath *p)
418 GNUNET_PEER_decrement_rcs(p->peers, p->length);
419 GNUNET_free(p->peers);
426 * Destroy the peer_info and free any allocated resources linked to it
427 * @param t tunnel the path belongs to
428 * @param pi the peer_info to destroy
429 * @return GNUNET_OK on success
432 // destroy_peer_info(struct MeshTunnel *t, struct MeshPeerInfo *pi)
434 // GNUNET_PEER_change_rc(pi->id, -1);
435 // /* FIXME delete from list */
443 * Destroy the tunnel and free any allocated resources linked to it
444 * @param c client the tunnel belongs to
445 * @param t the tunnel to destroy
446 * @return GNUNET_OK on success
449 destroy_tunnel(struct MeshClient *c, struct MeshTunnel *t)
451 // struct MeshPath *path;
452 GNUNET_HashCode hash;
455 if (NULL == t) return GNUNET_OK;
458 // for (path = t->paths_head; path != NULL; path = t->paths_head) {
459 // if(GNUNET_OK != destroy_path(t, path)) r = GNUNET_SYSERR;
462 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
463 if(GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t)) {
467 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
469 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t))
477 /******************************************************************************/
478 /**************** MESH NETWORK HANDLER HELPERS ***********************/
479 /******************************************************************************/
482 * Function called to notify a client about the socket
483 * being ready to queue more data. "buf" will be
484 * NULL and "size" zero if the socket was closed for
485 * writing in the meantime.
488 * @param size number of bytes available in buf
489 * @param buf where the callee should write the message
490 * @return number of bytes written to buf
493 send_core_create_path_for_peer (void *cls, size_t size, void *buf)
495 struct MeshPeerInfo *peer_info = cls;
496 struct GNUNET_MESH_ManipulatePath *msg;
498 struct GNUNET_PeerIdentity *peer_ptr;
502 if (0 == size && NULL == buf) {
503 // TODO retry? cancel?
513 if (p == NULL) return 0; // TODO Notify ERROR Path not found
515 size_needed = sizeof(struct GNUNET_MESH_ManipulatePath)
516 + p->length * sizeof(struct GNUNET_PeerIdentity);
517 if (size < size_needed) {
518 // TODO retry? cancel?
522 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
523 msg->header.size = htons(size_needed);
524 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
526 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
527 for (i = 0; i < p->length; i++) {
528 GNUNET_PEER_resolve(p->peers[i], peer_ptr++);
531 peer_info->state = MESH_PEER_WAITING;
538 * Send another peer a notification to destroy a tunnel
539 * @param cls The tunnel to destroy
540 * @param size Size in the buffer
541 * @param buf Memory where to put the data to transmit
542 * @return Size of data put in buffer
545 send_p2p_tunnel_destroy(void *cls, size_t size, void *buf)
547 struct MeshTunnel *t = cls;
548 struct MeshClient *c;
549 struct GNUNET_MESH_TunnelMessage *msg;
553 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY); /*FIXME*/
554 msg->header.size = htons(sizeof(struct GNUNET_MESH_TunnelMessage));
555 msg->tunnel_id = htonl(t->id.tid);
557 destroy_tunnel(c, t);
558 return sizeof(struct GNUNET_MESH_TunnelMessage);
563 /******************************************************************************/
564 /******************** MESH NETWORK HANDLERS **************************/
565 /******************************************************************************/
569 * Core handler for path creation
570 * struct GNUNET_CORE_MessageHandler
573 * @param message message
574 * @param peer peer identity this notification is about
575 * @param atsi performance data
576 * @return GNUNET_OK to keep the connection open,
577 * GNUNET_SYSERR to close it (signal serious error)
581 handle_mesh_path_create (void *cls,
582 const struct GNUNET_PeerIdentity *peer,
583 const struct GNUNET_MessageHeader *message,
584 const struct GNUNET_TRANSPORT_ATS_Information
588 /* Find origin & self */
589 /* Search for origin in local tunnels */
590 /* Create tunnel / add path */
591 /* Retransmit to next link in chain, if any (core_notify + callback) */
597 * Core handler for mesh network traffic
600 * @param message message
601 * @param peer peer identity this notification is about
602 * @param atsi performance data
603 * @return GNUNET_OK to keep the connection open,
604 * GNUNET_SYSERR to close it (signal serious error)
607 handle_mesh_network_traffic (void *cls,
608 const struct GNUNET_PeerIdentity *peer,
609 const struct GNUNET_MessageHeader *message,
610 const struct GNUNET_TRANSPORT_ATS_Information
613 if (GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
614 /* Retransmit to next in path of tunnel identified by message */
616 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
617 /* Retransmit to previous in path of tunnel identified by message */
624 * Functions to handle messages from core
626 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
627 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
628 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
629 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
635 /******************************************************************************/
636 /**************** MESH LOCAL HANDLER HELPERS ***********************/
637 /******************************************************************************/
640 * delete_tunnel_entry: iterator for deleting each tunnel that belongs to a
641 * client when the client disconnects.
642 * @param cls closure (client that is disconnecting)
643 * @param key the hash of the local tunnel id (used to access the hashmap)
644 * @param value the value stored at the key (tunnel to destroy)
645 * @return GNUNET_OK on success
648 delete_tunnel_entry (void *cls, const GNUNET_HashCode * key, void *value) {
650 r = destroy_tunnel((struct MeshClient *) cls, (struct MeshTunnel *) value);
656 * notify_client_connection_failure: notify a client that the connection to the
657 * requested remote peer is not possible (for instance, no route found)
658 * Function called when the socket is ready to queue more data."buf" will be
659 * NULL and "size" zero if the socket was closed for writing in the meantime.
662 * @param size number of bytes available in buf
663 * @param buf where the callee should write the message
664 * @return number of bytes written to buf
667 notify_client_connection_failure (void *cls, size_t size, void *buf)
670 struct MeshPeerInfo *peer_info;
671 struct GNUNET_MESH_PeerControl *msg;
672 struct GNUNET_PeerIdentity id;
674 if (0 == size && NULL == buf) {
675 // TODO retry? cancel?
679 size_needed = sizeof(struct GNUNET_MESH_PeerControl);
680 peer_info = (struct MeshPeerInfo *) cls;
681 msg = (struct GNUNET_MESH_PeerControl *) buf;
682 msg->header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
683 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
684 // msg->tunnel_id = htonl(peer_info->t->tid);
685 GNUNET_PEER_resolve(peer_info->id, &id);
686 memcpy(&msg->peer, &id, sizeof(struct GNUNET_PeerIdentity));
693 * Function to process paths received for a new peer addition. The recorded
694 * paths form the initial tunnel, which can be optimized later.
695 * Called on each result obtained for the DHT search.
698 * @param exp when will this value expire
699 * @param key key of the result
700 * @param get_path NULL-terminated array of pointers
701 * to the peers on reverse GET path (or NULL if not recorded)
702 * @param put_path NULL-terminated array of pointers
703 * to the peers on the PUT path (or NULL if not recorded)
704 * @param type type of the result
705 * @param size number of bytes in data
706 * @param data pointer to the result data
709 dht_get_response_handler(void *cls,
710 struct GNUNET_TIME_Absolute exp,
711 const GNUNET_HashCode * key,
712 const struct GNUNET_PeerIdentity * const *get_path,
713 const struct GNUNET_PeerIdentity * const *put_path,
714 enum GNUNET_BLOCK_Type type,
718 struct MeshPeerInfo *peer_info = cls;
720 struct MeshPath *aux;
721 struct GNUNET_PeerIdentity pi;
724 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path) {
725 // Find ourselves some alternate initial path to the destination: retry
726 GNUNET_DHT_get_stop(peer_info->dhtget);
727 GNUNET_PEER_resolve(peer_info->id, &pi);
728 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
729 GNUNET_TIME_UNIT_FOREVER_REL,
730 GNUNET_BLOCK_TYPE_ANY,
732 4, /* replication level */
733 GNUNET_DHT_RO_RECORD_ROUTE,
734 NULL, /* bloom filter */
738 dht_get_response_handler,
742 p = GNUNET_malloc(sizeof(struct MeshPath));
743 for (i = 0; get_path[i] != NULL; i++);
744 for (i--; i >= 0; i--) {
745 p->peers = GNUNET_realloc(p->peers,
746 sizeof(GNUNET_PEER_Id) * (p->length + 1));
747 p->peers[p->length] = GNUNET_PEER_intern(get_path[i]);
750 for (i = 0; put_path[i] != NULL; i++);
751 for (i--; i >= 0; i--) {
752 p->peers = GNUNET_realloc(p->peers,
753 sizeof(GNUNET_PEER_Id) * (p->length + 1));
754 p->peers[p->length] = GNUNET_PEER_intern(put_path[i]);
757 if (NULL == peer_info->path) {
762 aux = peer_info->path;
763 while (NULL != aux->next) aux = aux->next;
766 GNUNET_CORE_notify_transmit_ready(core_handle,
769 GNUNET_TIME_UNIT_FOREVER_REL,
771 sizeof(struct GNUNET_MESH_ManipulatePath)
773 * sizeof (struct GNUNET_PeerIdentity)),
774 &send_core_create_path_for_peer,
780 /******************************************************************************/
781 /********************* MESH LOCAL HANDLES **************************/
782 /******************************************************************************/
786 * Handler for client disconnection
789 * @param client identification of the client; NULL
790 * for the last call when the server is destroyed
793 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
795 struct MeshClient *c;
796 struct MeshClient *next;
798 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
799 "MESH: client disconnected\n");
802 if (c->handle == client) {
803 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
804 "MESH: matching client found, cleaning\n");
805 GNUNET_CONTAINER_multihashmap_iterate(c->tunnels,
806 &delete_tunnel_entry,
808 GNUNET_CONTAINER_multihashmap_destroy(c->tunnels);
809 if(0 != c->app_counter) GNUNET_free (c->apps);
810 if(0 != c->type_counter) GNUNET_free (c->types);
823 * Handler for new clients
826 * @param client identification of the client
827 * @param message the actual message, which includes messages the client wants
830 handle_local_new_client (void *cls,
831 struct GNUNET_SERVER_Client *client,
832 const struct GNUNET_MessageHeader *message)
834 struct GNUNET_MESH_ClientConnect *cc_msg;
835 struct MeshClient *c;
840 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
841 /* Check data sanity */
842 size = ntohs(message->size) - sizeof(struct GNUNET_MESH_ClientConnect);
843 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
844 types = ntohs(cc_msg->types);
845 apps = ntohs(cc_msg->applications);
847 types * sizeof(uint16_t) + apps * sizeof(GNUNET_MESH_ApplicationType))
850 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
854 /* Create new client structure */
855 c = GNUNET_malloc(sizeof(struct MeshClient));
858 c->type_counter = types;
859 c->types = GNUNET_malloc(types * sizeof(uint16_t));
860 memcpy(c->types, &message[1], types * sizeof(uint16_t));
863 c->app_counter = apps;
864 c->apps = GNUNET_malloc(apps * sizeof(GNUNET_MESH_ApplicationType));
866 &message[1] + types * sizeof(uint16_t),
867 apps * sizeof(GNUNET_MESH_ApplicationType));
869 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
870 "MESH: client has %u+%u subscriptions\n",
876 c->tunnels = GNUNET_CONTAINER_multihashmap_create(32);
878 GNUNET_SERVER_receive_done(client, GNUNET_OK);
884 * Handler for requests of new tunnels
887 * @param client identification of the client
888 * @param message the actual message
891 handle_local_tunnel_create (void *cls,
892 struct GNUNET_SERVER_Client *client,
893 const struct GNUNET_MessageHeader *message)
895 struct GNUNET_MESH_TunnelMessage *t_msg;
896 struct MeshTunnel *t;
897 struct MeshClient *c;
898 GNUNET_HashCode hash;
900 /* Sanity check for client registration */
901 if (NULL == (c = retrieve_client(client))) {
903 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
907 /* Message sanity check */
908 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
910 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
914 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
915 /* Sanity check for tunnel numbering */
916 if (0 == (ntohl(t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)) {
918 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
921 /* Sanity check for duplicate tunnel IDs */
922 if(NULL != retrieve_tunnel_by_local_id(c, ntohl(t_msg->tunnel_id))) {
924 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
928 t = GNUNET_malloc(sizeof(struct MeshTunnel));
929 // FIXME: what if all 2^32 ID are taken?
930 while (NULL != retrieve_tunnel_by_pi(myid, next_tid)) next_tid++;
931 t->id.tid = next_tid++;
933 t->local_tid = ntohl(t_msg->tunnel_id);
935 t->peers = GNUNET_CONTAINER_multihashmap_create(32);
937 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
939 GNUNET_CONTAINER_multihashmap_put(c->tunnels, &hash, t,
940 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
943 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
947 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
949 GNUNET_CONTAINER_multihashmap_put(tunnels, &hash, t,
950 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
953 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
957 GNUNET_SERVER_receive_done(client, GNUNET_OK);
963 * Handler for requests of deleting tunnels
966 * @param client identification of the client
967 * @param message the actual message
970 handle_local_tunnel_destroy (void *cls,
971 struct GNUNET_SERVER_Client *client,
972 const struct GNUNET_MessageHeader *message)
974 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
975 struct MeshClient *c;
976 struct MeshTunnel *t;
977 MESH_TunnelNumber tid;
978 GNUNET_HashCode hash;
981 /* Sanity check for client registration */
982 if (NULL == (c = retrieve_client(client))) {
984 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
987 /* Message sanity check */
988 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
990 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
994 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
996 /* Retrieve tunnel */
997 tid = ntohl(tunnel_msg->tunnel_id);
999 /* Remove from local id hashmap */
1000 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
1001 t = GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
1002 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t);
1004 /* Remove from global id hashmap */
1005 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
1006 GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t);
1008 // notify_tunnel_destroy(t);
1009 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1015 * Handler for connection requests to new peers
1017 * @param cls closure
1018 * @param client identification of the client
1019 * @param message the actual message (PeerControl)
1022 handle_local_connect_add (void *cls,
1023 struct GNUNET_SERVER_Client *client,
1024 const struct GNUNET_MessageHeader *message)
1026 struct GNUNET_MESH_PeerControl *peer_msg;
1027 struct MeshClient *c;
1028 struct MeshTunnel *t;
1029 MESH_TunnelNumber tid;
1030 struct MeshPeerInfo *peer_info;
1033 /* Sanity check for client registration */
1034 if (NULL == (c = retrieve_client(client))) {
1036 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1040 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1041 /* Sanity check for message size */
1042 if (sizeof(struct GNUNET_MESH_PeerControl)
1043 != ntohs(peer_msg->header.size))
1046 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1050 /* Tunnel exists? */
1051 tid = ntohl(peer_msg->tunnel_id);
1052 t = retrieve_tunnel_by_local_id(c, tid);
1055 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1059 /* Does client own tunnel? */
1060 if (t->client->handle != client) {
1062 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1066 /* Ok, add peer to tunnel */
1067 peer_info = GNUNET_CONTAINER_multihashmap_get(peers,
1068 &peer_msg->peer.hashPubKey);
1069 if (NULL == peer_info) {
1070 peer_info = (struct MeshPeerInfo *)
1071 GNUNET_malloc(sizeof(struct MeshPeerInfo));
1072 GNUNET_CONTAINER_multihashmap_put(peers,
1073 &peer_msg->peer.hashPubKey,
1075 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1076 peer_info->id = GNUNET_PEER_intern(&peer_msg->peer);
1077 peer_info->state = MESH_PEER_SEARCHING;
1082 /* Start DHT search if needed */
1083 if(MESH_PEER_READY != peer_info->state && NULL == peer_info->dhtget) {
1084 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
1085 GNUNET_TIME_UNIT_FOREVER_REL,
1086 GNUNET_BLOCK_TYPE_ANY,
1087 &peer_msg->peer.hashPubKey,
1088 4, /* replication level */
1089 GNUNET_DHT_RO_RECORD_ROUTE,
1090 NULL, /* bloom filter */
1093 0, /* xquery bits */
1094 dht_get_response_handler,
1098 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1104 * Handler for disconnection requests of peers in a tunnel
1106 * @param cls closure
1107 * @param client identification of the client
1108 * @param message the actual message (PeerControl)
1111 handle_local_connect_del (void *cls,
1112 struct GNUNET_SERVER_Client *client,
1113 const struct GNUNET_MessageHeader *message)
1115 struct GNUNET_MESH_PeerControl *peer_msg;
1116 struct MeshClient *c;
1117 struct MeshTunnel *t;
1118 MESH_TunnelNumber tid;
1119 GNUNET_PEER_Id peer_id;
1121 /* Sanity check for client registration */
1122 if (NULL == (c = retrieve_client(client))) {
1124 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1127 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1128 /* Sanity check for message size */
1129 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
1131 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1135 /* Tunnel exists? */
1136 tid = ntohl(peer_msg->tunnel_id);
1137 t = retrieve_tunnel_by_local_id(c, tid);
1140 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1144 /* Does client own tunnel? */
1145 if (t->client->handle != client) {
1147 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1151 /* Ok, delete peer from tunnel */
1152 peer_id = GNUNET_PEER_intern(&peer_msg->peer);
1154 /* FIXME Delete paths */
1155 /* FIXME Delete peer info */
1157 GNUNET_PEER_change_rc(peer_id, -1);
1159 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1165 * Handler for connection requests to new peers by type
1167 * @param cls closure
1168 * @param client identification of the client
1169 * @param message the actual message (ConnectPeerByType)
1172 handle_local_connect_by_type (void *cls,
1173 struct GNUNET_SERVER_Client *client,
1174 const struct GNUNET_MessageHeader *message)
1176 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
1177 MESH_TunnelNumber tid;
1178 GNUNET_MESH_ApplicationType application;
1179 struct MeshClient *c;
1180 struct MeshTunnel *t;
1182 /* Sanity check for client registration */
1183 if (NULL == (c = retrieve_client(client))) {
1185 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1189 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *)message;
1190 /* Sanity check for message size */
1191 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1192 ntohs(connect_msg->header.size))
1195 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1199 /* Tunnel exists? */
1200 tid = ntohl(connect_msg->tunnel_id);
1201 t = retrieve_tunnel_by_local_id(c, tid);
1204 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1208 /* Does client own tunnel? */
1209 if (t->client->handle != client) {
1211 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1215 /* Ok, lets find a peer offering the service */
1216 application = ntohl(connect_msg->type);
1217 application++; // FIXME silence warnings
1219 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1225 * Handler for client traffic directed to one peer
1227 * @param cls closure
1228 * @param client identification of the client
1229 * @param message the actual message
1232 handle_local_network_traffic (void *cls,
1233 struct GNUNET_SERVER_Client *client,
1234 const struct GNUNET_MessageHeader *message)
1236 struct MeshClient *c;
1237 struct MeshTunnel *t;
1238 struct GNUNET_MESH_Data *data_msg;
1239 MESH_TunnelNumber tid;
1241 /* Sanity check for client registration */
1242 if (NULL == (c = retrieve_client(client))) {
1244 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1247 data_msg = (struct GNUNET_MESH_Data *)message;
1248 /* Sanity check for message size */
1249 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1250 ntohs(data_msg->header.size))
1253 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1257 /* Tunnel exists? */
1258 tid = ntohl(data_msg->tunnel_id);
1259 t = retrieve_tunnel_by_local_id(c, tid);
1262 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1266 /* Does client own tunnel? */
1267 if (t->client->handle != client) {
1269 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1275 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1280 * Handler for client traffic directed to all peers in a tunnel
1282 * @param cls closure
1283 * @param client identification of the client
1284 * @param message the actual message
1287 handle_local_network_traffic_bcast (void *cls,
1288 struct GNUNET_SERVER_Client *client,
1289 const struct GNUNET_MessageHeader *message)
1291 struct MeshClient *c;
1292 struct MeshTunnel *t;
1293 struct GNUNET_MESH_DataBroadcast *data_msg;
1294 MESH_TunnelNumber tid;
1296 /* Sanity check for client registration */
1297 if (NULL == (c = retrieve_client(client))) {
1299 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1302 data_msg = (struct GNUNET_MESH_DataBroadcast *)message;
1303 /* Sanity check for message size */
1304 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(data_msg->header.size)) {
1306 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1310 /* Tunnel exists? */
1311 tid = ntohl(data_msg->tunnel_id);
1312 t = retrieve_tunnel_by_local_id(c, tid);
1315 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1319 /* Does client own tunnel? */
1320 if (t->client->handle != client) {
1322 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1328 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1333 * Functions to handle messages from clients
1335 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1336 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
1337 {&handle_local_tunnel_create, NULL,
1338 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
1339 sizeof(struct GNUNET_MESH_TunnelMessage)},
1340 {&handle_local_tunnel_destroy, NULL,
1341 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
1342 sizeof(struct GNUNET_MESH_TunnelMessage)},
1343 {&handle_local_connect_add, NULL,
1344 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD,
1345 sizeof(struct GNUNET_MESH_PeerControl)},
1346 {&handle_local_connect_del, NULL,
1347 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL,
1348 sizeof(struct GNUNET_MESH_PeerControl)},
1349 {&handle_local_connect_by_type, NULL,
1350 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE,
1351 sizeof(struct GNUNET_MESH_ConnectPeerByType)},
1352 {&handle_local_network_traffic, NULL,
1353 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
1354 {&handle_local_network_traffic_bcast, NULL,
1355 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0},
1361 * To be called on core init/fail.
1363 * @param cls service closure
1364 * @param server handle to the server for this service
1365 * @param identity the public identity of this peer
1366 * @param publicKey the public key of this peer
1369 core_init (void *cls,
1370 struct GNUNET_CORE_Handle *server,
1371 const struct GNUNET_PeerIdentity *identity,
1372 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376 core_handle = server;
1377 myid = GNUNET_PEER_intern(identity);
1382 * Method called whenever a given peer connects.
1384 * @param cls closure
1385 * @param peer peer identity this notification is about
1386 * @param atsi performance data for the connection
1389 core_connect (void *cls,
1390 const struct GNUNET_PeerIdentity *peer,
1391 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "Peer connected\n");
1396 pid = GNUNET_PEER_intern(peer);
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 * Method called whenever a peer disconnects.
1407 * @param cls closure
1408 * @param peer peer identity this notification is about
1411 core_disconnect (void *cls,
1413 GNUNET_PeerIdentity *peer)
1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1416 "Peer disconnected\n");
1420 /******************************************************************************/
1421 /************************ MAIN FUNCTIONS ****************************/
1422 /******************************************************************************/
1425 * Task run during shutdown.
1431 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1434 "MESH shutting down\n");
1435 if (core_handle != NULL) {
1436 GNUNET_CORE_disconnect (core_handle);
1439 if (dht_handle != NULL) {
1440 GNUNET_DHT_disconnect (dht_handle);
1446 * Process mesh requests.
1448 * @param cls closure
1449 * @param server the initialized server
1450 * @param c configuration to use
1454 struct GNUNET_SERVER_Handle *server,
1455 const struct GNUNET_CONFIGURATION_Handle *c)
1457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1458 "MESH starting to run\n");
1459 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1460 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1461 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1462 32, /* queue size */
1463 NULL, /* Closure passed to MESH functions */
1464 &core_init, /* Call core_init once connected */
1465 &core_connect, /* Handle connects */
1466 &core_disconnect, /* remove peers on disconnects */
1467 NULL, /* Do we care about "status" updates? */
1468 NULL, /* Don't notify about all incoming messages */
1469 GNUNET_NO, /* For header only in notification */
1470 NULL, /* Don't notify about all outbound messages */
1471 GNUNET_NO, /* For header-only out notification */
1472 core_handlers); /* Register these handlers */
1473 if (core_handle == NULL) {
1476 dht_handle = GNUNET_DHT_connect(c, 64);
1477 if (dht_handle == NULL) {
1482 tunnels = GNUNET_CONTAINER_multihashmap_create(32);
1483 peers = GNUNET_CONTAINER_multihashmap_create(32);
1485 /* Scheduled the task to clean up when shutdown is called */
1486 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1487 &shutdown_task, NULL);
1492 * The main function for the mesh service.
1494 * @param argc number of arguments from the command line
1495 * @param argv command line arguments
1496 * @return 0 ok, 1 on error
1499 main (int argc, char *const *argv)
1504 GNUNET_SERVICE_run (argc,
1507 GNUNET_SERVICE_OPTION_NONE,
1508 &run, NULL)) ? 0 : 1;