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 * All the states a peer participating in a tunnel can be in.
65 * Path to the peer not known yet
70 * Request sent, not yet answered.
75 * Peer connected and ready to accept data
80 * Peer connected previosly but not responding
82 MESH_PEER_RECONNECTING
86 * Struct containing all information regarding a given peer
96 * Is the peer reachable? Is the peer even connected?
98 enum MeshPeerState state;
101 * When to try to establish contact again?
103 struct GNUNET_TIME_Absolute next_reconnect_attempt;
106 * Number of attempts to reconnect so far
108 int n_reconnect_attempts;
111 * First hop whom to send data to reach this peer in the current active path
113 GNUNET_PEER_Id first_hop;
116 * Handle to stop the DHT search for a path to this peer
118 struct GNUNET_DHT_GetHandle *dhtget;
123 * Information regarding a path
130 struct MeshPath *next;
131 struct MeshPath *prev;
134 * Whether the path is serving traffic in a tunnel or is a backup
139 * List of all the peers that form the path from origin to target
141 GNUNET_PEER_Id *peers;
144 * Number of peers (hops) in the path
150 * Data scheduled to transmit (to local client or remote peer)
157 struct MeshQueue *next;
158 struct MeshQueue *prev;
161 * Target of the data (NULL if target is client)
163 struct MeshPeerInfo *peer;
166 * Client to send the data to (NULL if target is peer)
168 struct MeshClient *client;
171 * Size of the message to transmit
176 * How old is the data?
178 struct GNUNET_TIME_Absolute timestamp;
183 struct GNUNET_MessageHeader *data;
187 * Globally unique tunnel identification (owner + number)
188 * DO NOT USE OVER THE NETWORK
190 struct MESH_TunnelID {
192 * Node that owns the tunnel
197 * Tunnel number to differentiate all the tunnels owned by the node oid
198 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_MARK )
200 MESH_TunnelNumber tid;
204 struct MeshClient; /* FWD declaration */
206 * Struct containing all information regarding a tunnel
207 * For an intermediate node the improtant info used will be:
208 * - id Tunnel unique identification
209 * - paths[0] To know where to send it next
210 * - metainfo: ready, speeds, accounting
217 struct MESH_TunnelID id;
220 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK or 0 )
222 MESH_TunnelNumber local_tid;
225 * Last time the tunnel was used
227 struct GNUNET_TIME_Absolute timestamp;
230 * Peers in the tunnel, for future optimizations
232 struct GNUNET_CONTAINER_MultiHashMap* peers;
235 * Number of peers that are connected and potentially ready to receive data
237 unsigned int peers_ready;
240 * Number of peers that have been added to the tunnel
242 unsigned int peers_total;
245 * Paths (used and backup)
247 struct MeshPath *paths_head;
248 struct MeshPath *paths_tail;
251 * Client owner of the tunnel, if any
253 struct MeshClient *client;
256 * Messages ready to transmit
258 struct MeshQueue *queue_head;
259 struct MeshQueue *queue_tail;
264 * Struct containing information about a client of the service
271 struct MeshClient *next;
274 * Tunnels that belong to this client, indexed by local id
276 struct GNUNET_CONTAINER_MultiHashMap* tunnels;
279 * Handle to communicate with the client
281 struct GNUNET_SERVER_Client *handle;
284 * Applications that this client has claimed to provide
286 GNUNET_MESH_ApplicationType *apps;
287 unsigned int app_counter;
290 * Messages that this client has declared interest in
293 unsigned int type_counter;
297 /******************************************************************************/
298 /*********************** GLOBAL VARIABLES ****************************/
299 /******************************************************************************/
304 static struct MeshClient *clients;
307 * Tunnels known, indexed by MESH_TunnelID
309 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
312 * Handle to communicate with core
314 static struct GNUNET_CORE_Handle *core_handle;
319 static struct GNUNET_DHT_Handle *dht_handle;
322 * Local peer own ID (memory efficient handle)
324 static GNUNET_PEER_Id myid;
327 * Tunnel ID for the next created tunnel (global tunnel number)
329 static MESH_TunnelNumber next_tid;
331 /******************************************************************************/
332 /****************** GENERAL HELPER FUNCTIONS ************************/
333 /******************************************************************************/
336 * Check if client has registered with the service and has not disconnected
337 * @param client the client to check
338 * @return non-NULL if client exists in the global DLL
340 static struct MeshClient *
341 retrieve_client (struct GNUNET_SERVER_Client *client)
343 struct MeshClient *c;
347 if (c->handle == client) return c;
355 * Search for a tunnel among the tunnels for a client
356 * @param client the client whose tunnels to search in
357 * @param tid the local id of the tunnel
358 * @return tunnel handler, NULL if doesn't exist
360 static struct MeshTunnel *
361 retrieve_tunnel_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
363 GNUNET_HashCode hash;
365 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
366 return GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
370 * Search for a tunnel by global ID using PEER_ID
371 * @param pi owner of the tunnel
372 * @param tid global tunnel number
373 * @return tunnel handler, NULL if doesn't exist
375 static struct MeshTunnel *
376 retrieve_tunnel_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
378 struct MESH_TunnelID id;
379 GNUNET_HashCode hash;
384 GNUNET_CRYPTO_hash(&id, sizeof(struct MESH_TunnelID), &hash);
385 return GNUNET_CONTAINER_multihashmap_get(tunnels, &hash);
391 * Search for a tunnel by global ID using full PeerIdentities
392 * @param oid owner of the tunnel
393 * @param tid global tunnel number
394 * @return tunnel handler, NULL if doesn't exist
396 static struct MeshTunnel *
397 retrieve_tunnel (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
401 pi = GNUNET_PEER_intern(oid);
402 GNUNET_PEER_change_rc(pi, -1);
403 return retrieve_tunnel_by_pi(pi, tid);
409 * Destroy the path and free any allocated resources linked to it
410 * @param t tunnel the path belongs to
411 * @param p the path to destroy
412 * @return GNUNET_OK on success
415 destroy_path(struct MeshTunnel *t, struct MeshPath *p)
417 GNUNET_PEER_decrement_rcs(p->peers, p->length);
418 GNUNET_free(p->peers);
419 GNUNET_CONTAINER_DLL_remove(t->paths_head, t->paths_tail, p);
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;
457 for (path = t->paths_head; path != NULL; path = t->paths_head) {
458 if(GNUNET_OK != destroy_path(t, path)) r = GNUNET_SYSERR;
461 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
462 if(GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t)) {
466 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
468 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t))
476 /******************************************************************************/
477 /**************** MESH NETWORK HANDLER HELPERS ***********************/
478 /******************************************************************************/
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)
496 struct MeshPeerInfo *peer_info;
497 struct GNUNET_MESH_ManipulatePath *msg;
499 struct GNUNET_PeerIdentity peer_id;
500 struct GNUNET_PeerIdentity *peer_ptr;
503 if (0 == size && NULL == buf) {
504 // TODO retry? cancel?
507 peer_info = (struct MeshPeerInfo *)cls;
508 peer_info->dhtget = NULL;
509 // p = peer_info->t->paths_head;
512 if (p->peers[p->length-1] == peer_info->id) {
517 if (p == NULL) return 0; // TODO Notify ERROR Path not found
519 size_needed = sizeof(struct GNUNET_MESH_ManipulatePath)
520 + p->length * sizeof(struct GNUNET_PeerIdentity);
521 if (size < size_needed) {
522 // TODO retry? cancel?
526 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
527 msg->header.size = htons(sizeof(struct GNUNET_MESH_ManipulatePath));
528 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
531 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
532 for (i = 0; i < p->length; i++) {
533 GNUNET_PEER_resolve(p->peers[i], &peer_id);
534 memcpy(&peer_ptr[i], &peer_id, sizeof(struct GNUNET_PeerIdentity));
537 peer_info->state = MESH_PEER_WAITING;
544 * Send another peer a notification to destroy a tunnel
545 * @param cls The tunnel to destroy
546 * @param size Size in the buffer
547 * @param buf Memory where to put the data to transmit
548 * @return Size of data put in buffer
551 send_p2p_tunnel_destroy(void *cls, size_t size, void *buf)
553 struct MeshTunnel *t = cls;
554 struct MeshClient *c;
555 struct GNUNET_MESH_TunnelMessage *msg;
559 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY); /*FIXME*/
560 msg->header.size = htons(sizeof(struct GNUNET_MESH_TunnelMessage));
561 msg->tunnel_id = htonl(t->id.tid);
563 destroy_tunnel(c, t);
564 return sizeof(struct GNUNET_MESH_TunnelMessage);
568 /******************************************************************************/
569 /******************** MESH NETWORK HANDLERS **************************/
570 /******************************************************************************/
574 * Core handler for path creation
575 * struct GNUNET_CORE_MessageHandler
578 * @param message message
579 * @param peer peer identity this notification is about
580 * @param atsi performance data
581 * @return GNUNET_OK to keep the connection open,
582 * GNUNET_SYSERR to close it (signal serious error)
586 handle_mesh_path_create (void *cls,
587 const struct GNUNET_PeerIdentity *peer,
588 const struct GNUNET_MessageHeader *message,
589 const struct GNUNET_TRANSPORT_ATS_Information
593 /* Find origin & self */
594 /* Search for origin in local tunnels */
595 /* Create tunnel / add path */
596 /* Retransmit to next link in chain, if any (core_notify + callback) */
602 * Core handler for mesh network traffic
605 * @param message message
606 * @param peer peer identity this notification is about
607 * @param atsi performance data
608 * @return GNUNET_OK to keep the connection open,
609 * GNUNET_SYSERR to close it (signal serious error)
612 handle_mesh_network_traffic (void *cls,
613 const struct GNUNET_PeerIdentity *peer,
614 const struct GNUNET_MessageHeader *message,
615 const struct GNUNET_TRANSPORT_ATS_Information
618 if (GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
619 /* Retransmit to next in path of tunnel identified by message */
621 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
622 /* Retransmit to previous in path of tunnel identified by message */
629 * Functions to handle messages from core
631 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
632 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
633 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
634 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
640 /******************************************************************************/
641 /**************** MESH LOCAL HANDLER HELPERS ***********************/
642 /******************************************************************************/
645 delete_tunnel_entry (void *cls, const GNUNET_HashCode * key, void *value) {
647 r = destroy_tunnel((struct MeshClient *) cls, (struct MeshTunnel *) value);
652 * notify_client_connection_failure: notify a client that the connection to the
653 * requested remote peer is not possible (for instance, no route found)
654 * Function called when the socket is ready to queue more data."buf" will be
655 * NULL and "size" zero if the socket was closed for writing in the meantime.
658 * @param size number of bytes available in buf
659 * @param buf where the callee should write the message
660 * @return number of bytes written to buf
663 notify_client_connection_failure (void *cls, size_t size, void *buf)
666 struct MeshPeerInfo *peer_info;
667 struct GNUNET_MESH_PeerControl *msg;
668 struct GNUNET_PeerIdentity id;
670 if (0 == size && NULL == buf) {
671 // TODO retry? cancel?
675 size_needed = sizeof(struct GNUNET_MESH_PeerControl);
676 peer_info = (struct MeshPeerInfo *) cls;
677 msg = (struct GNUNET_MESH_PeerControl *) buf;
678 msg->header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
679 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
680 // msg->tunnel_id = htonl(peer_info->t->tid);
681 GNUNET_PEER_resolve(peer_info->id, &id);
682 memcpy(&msg->peer, &id, sizeof(struct GNUNET_PeerIdentity));
690 * Function to process paths received for a new peer addition. The recorded
691 * paths form the initial tunnel, which can be optimized later.
692 * Called on each result obtained for the DHT search.
695 * @param exp when will this value expire
696 * @param key key of the result
697 * @param get_path NULL-terminated array of pointers
698 * to the peers on reverse GET path (or NULL if not recorded)
699 * @param put_path NULL-terminated array of pointers
700 * to the peers on the PUT path (or NULL if not recorded)
701 * @param type type of the result
702 * @param size number of bytes in data
703 * @param data pointer to the result data
706 dht_get_response_handler(void *cls,
707 struct GNUNET_TIME_Absolute exp,
708 const GNUNET_HashCode * key,
709 const struct GNUNET_PeerIdentity * const *get_path,
710 const struct GNUNET_PeerIdentity * const *put_path,
711 enum GNUNET_BLOCK_Type type,
715 struct MeshPeerInfo *peer_info;
716 struct MeshTunnel *t;
720 peer_info = (struct MeshPeerInfo *)cls;
724 if (NULL == get_path || NULL == put_path) {
725 // TODO: find ourselves some alternate initial path to the destination
726 GNUNET_SERVER_notify_transmit_ready(
728 sizeof(struct GNUNET_MESH_PeerControl),
729 GNUNET_TIME_UNIT_FOREVER_REL,
730 ¬ify_client_connection_failure,
735 p = GNUNET_malloc(sizeof(struct MeshPath));
736 GNUNET_CONTAINER_DLL_insert(t->paths_head, t->paths_tail, p);
737 for (i = 0; get_path[i] != NULL; i++);
738 for (i--; i >= 0; i--) {
739 p->peers = GNUNET_realloc(p->peers,
740 sizeof(GNUNET_PEER_Id) * (p->length + 1));
741 p->peers[p->length] = GNUNET_PEER_intern(get_path[i]);
744 for (i = 0; put_path[i] != NULL; i++);
745 for (i--; i >= 0; i--) {
746 p->peers = GNUNET_realloc(p->peers,
747 sizeof(GNUNET_PEER_Id) * (p->length + 1));
748 p->peers[p->length] = GNUNET_PEER_intern(put_path[i]);
751 // p->id = 0; // FIXME generate ID or remove field
753 // peer_info->first_hop = p->peers[1]; // FIXME do this on path completion
754 GNUNET_CORE_notify_transmit_ready(core_handle,
757 GNUNET_TIME_UNIT_FOREVER_REL,
759 sizeof(struct GNUNET_MESH_ManipulatePath)
761 * sizeof (struct GNUNET_PeerIdentity)),
762 &send_core_create_path_for_peer,
768 /******************************************************************************/
769 /********************* MESH LOCAL HANDLES **************************/
770 /******************************************************************************/
774 * Handler for client disconnection
777 * @param client identification of the client; NULL
778 * for the last call when the server is destroyed
781 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
783 struct MeshClient *c;
784 struct MeshClient *next;
786 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
787 "MESH: client disconnected\n");
790 if (c->handle == client) {
791 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
792 "MESH: matching client found, cleaning\n");
793 GNUNET_CONTAINER_multihashmap_iterate(c->tunnels,
794 &delete_tunnel_entry,
796 GNUNET_CONTAINER_multihashmap_destroy(c->tunnels);
797 if(0 != c->app_counter) GNUNET_free (c->apps);
798 if(0 != c->type_counter) GNUNET_free (c->types);
811 * Handler for new clients
814 * @param client identification of the client
815 * @param message the actual message, which includes messages the client wants
818 handle_local_new_client (void *cls,
819 struct GNUNET_SERVER_Client *client,
820 const struct GNUNET_MessageHeader *message)
822 struct GNUNET_MESH_ClientConnect *cc_msg;
823 struct MeshClient *c;
828 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
829 /* Check data sanity */
830 size = ntohs(message->size) - sizeof(struct GNUNET_MESH_ClientConnect);
831 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
832 types = ntohs(cc_msg->types);
833 apps = ntohs(cc_msg->applications);
835 types * sizeof(uint16_t) + apps * sizeof(GNUNET_MESH_ApplicationType))
838 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
842 /* Create new client structure */
843 c = GNUNET_malloc(sizeof(struct MeshClient));
846 c->type_counter = types;
847 c->types = GNUNET_malloc(types * sizeof(uint16_t));
848 memcpy(c->types, &message[1], types * sizeof(uint16_t));
851 c->app_counter = apps;
852 c->apps = GNUNET_malloc(apps * sizeof(GNUNET_MESH_ApplicationType));
854 &message[1] + types * sizeof(uint16_t),
855 apps * sizeof(GNUNET_MESH_ApplicationType));
857 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
858 "MESH: client has %u+%u subscriptions\n",
864 c->tunnels = GNUNET_CONTAINER_multihashmap_create(32);
866 GNUNET_SERVER_receive_done(client, GNUNET_OK);
872 * Handler for requests of new tunnels
875 * @param client identification of the client
876 * @param message the actual message
879 handle_local_tunnel_create (void *cls,
880 struct GNUNET_SERVER_Client *client,
881 const struct GNUNET_MessageHeader *message)
883 struct GNUNET_MESH_TunnelMessage *t_msg;
884 struct MeshTunnel *t;
885 struct MeshClient *c;
886 GNUNET_HashCode hash;
888 /* Sanity check for client registration */
889 if (NULL == (c = retrieve_client(client))) {
891 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
895 /* Message sanity check */
896 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
898 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
902 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
903 /* Sanity check for tunnel numbering */
904 if (0 == (ntohl(t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)) {
906 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
909 /* Sanity check for duplicate tunnel IDs */
910 if(NULL != retrieve_tunnel_by_local_id(c, ntohl(t_msg->tunnel_id))) {
912 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
916 t = GNUNET_malloc(sizeof(struct MeshTunnel));
917 // FIXME: what if all 2^32 ID are taken?
918 while (NULL != retrieve_tunnel_by_pi(myid, next_tid)) next_tid++;
919 t->id.tid = next_tid++;
921 t->local_tid = ntohl(t_msg->tunnel_id);
924 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
926 GNUNET_CONTAINER_multihashmap_put(c->tunnels, &hash, t,
927 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
930 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
934 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
936 GNUNET_CONTAINER_multihashmap_put(tunnels, &hash, t,
937 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
940 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
944 GNUNET_SERVER_receive_done(client, GNUNET_OK);
950 * Handler for requests of deleting tunnels
953 * @param client identification of the client
954 * @param message the actual message
957 handle_local_tunnel_destroy (void *cls,
958 struct GNUNET_SERVER_Client *client,
959 const struct GNUNET_MessageHeader *message)
961 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
962 struct MeshClient *c;
963 struct MeshTunnel *t;
964 MESH_TunnelNumber tid;
965 GNUNET_HashCode hash;
968 /* Sanity check for client registration */
969 if (NULL == (c = retrieve_client(client))) {
971 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
974 /* Message sanity check */
975 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
977 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
981 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
983 /* Retrieve tunnel */
984 tid = ntohl(tunnel_msg->tunnel_id);
986 /* Remove from local id hashmap */
987 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
988 t = GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
989 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t);
991 /* Remove from global id hashmap */
992 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
993 GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t);
995 GNUNET_CORE_notify_transmit_ready(core_handle,
998 GNUNET_TIME_UNIT_FOREVER_REL,
1000 sizeof(struct GNUNET_MESH_TunnelMessage),
1001 &send_p2p_tunnel_destroy,
1004 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1010 * Handler for connection requests to new peers
1012 * @param cls closure
1013 * @param client identification of the client
1014 * @param message the actual message (PeerControl)
1017 handle_local_connect_add (void *cls,
1018 struct GNUNET_SERVER_Client *client,
1019 const struct GNUNET_MessageHeader *message)
1021 struct GNUNET_MESH_PeerControl *peer_msg;
1022 struct MeshClient *c;
1023 struct MeshTunnel *t;
1024 MESH_TunnelNumber tid;
1025 struct MeshPeerInfo *peer_info;
1026 GNUNET_HashCode key;
1029 /* Sanity check for client registration */
1030 if (NULL == (c = retrieve_client(client))) {
1032 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1036 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1037 /* Sanity check for message size */
1038 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
1040 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1044 /* Tunnel exists? */
1045 tid = ntohl(peer_msg->tunnel_id);
1046 t = retrieve_tunnel_by_local_id(c, tid);
1049 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1053 /* Does client own tunnel? */
1054 if (t->client->handle != client) {
1056 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1060 /* Ok, add peer to tunnel */
1061 peer_info = (struct MeshPeerInfo *) GNUNET_malloc(sizeof(struct MeshPeerInfo));
1062 peer_info->id = GNUNET_PEER_intern(&peer_msg->peer);
1063 peer_info->state = MESH_PEER_SEARCHING;
1066 /* Start DHT search */
1067 GNUNET_CRYPTO_hash (&peer_msg->peer,
1068 sizeof(struct GNUNET_PeerIdentity),
1070 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
1071 GNUNET_TIME_UNIT_FOREVER_REL,
1072 GNUNET_BLOCK_TYPE_ANY,
1074 4, /* replication level */
1075 GNUNET_DHT_RO_RECORD_ROUTE,
1076 NULL, /* bloom filter */
1079 0, /* xquery bits */
1080 dht_get_response_handler,
1083 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1089 * Handler for disconnection requests of peers in a tunnel
1091 * @param cls closure
1092 * @param client identification of the client
1093 * @param message the actual message (PeerControl)
1096 handle_local_connect_del (void *cls,
1097 struct GNUNET_SERVER_Client *client,
1098 const struct GNUNET_MessageHeader *message)
1100 struct GNUNET_MESH_PeerControl *peer_msg;
1101 struct MeshClient *c;
1102 struct MeshTunnel *t;
1104 struct MeshPath *aux_path;
1105 MESH_TunnelNumber tid;
1106 GNUNET_PEER_Id peer_id;
1108 /* Sanity check for client registration */
1109 if (NULL == (c = retrieve_client(client))) {
1111 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1114 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1115 /* Sanity check for message size */
1116 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
1118 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1122 /* Tunnel exists? */
1123 tid = ntohl(peer_msg->tunnel_id);
1124 t = retrieve_tunnel_by_local_id(c, tid);
1127 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1131 /* Does client own tunnel? */
1132 if (t->client->handle != client) {
1134 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1138 /* Ok, delete peer from tunnel */
1139 peer_id = GNUNET_PEER_intern(&peer_msg->peer);
1144 if (p->peers[p->length-1] == peer_id) { /* one path per destination */
1145 GNUNET_CONTAINER_DLL_remove(t->paths_head, t->paths_tail, p);
1146 GNUNET_PEER_decrement_rcs(p->peers, p->length);
1149 GNUNET_free(aux_path);
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(64);
1484 /* Scheduled the task to clean up when shutdown is called */
1485 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1486 &shutdown_task, NULL);
1491 * The main function for the mesh service.
1493 * @param argc number of arguments from the command line
1494 * @param argv command line arguments
1495 * @return 0 ok, 1 on error
1498 main (int argc, char *const *argv)
1503 GNUNET_SERVICE_run (argc,
1506 GNUNET_SERVICE_OPTION_NONE,
1507 &run, NULL)) ? 0 : 1;