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
41 * - add ping message (connection confirmation, others?)
45 #include "gnunet_common.h"
46 #include "gnunet_util_lib.h"
47 #include "gnunet_peer_lib.h"
48 #include "gnunet_core_service.h"
49 #include "gnunet_protocols.h"
52 #include "mesh_protocol.h"
53 #include "gnunet_dht_service.h"
55 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
56 GNUNET_TIME_UNIT_SECONDS,\
60 /******************************************************************************/
61 /************************ DATA STRUCTURES ****************************/
62 /******************************************************************************/
65 * Information regarding a path
73 struct MeshPath *next;
74 struct MeshPath *prev;
77 * Whether the path is serving traffic in a tunnel or is a backup
82 * List of all the peers that form the path from origin to target
84 GNUNET_PEER_Id *peers;
87 * Number of peers (hops) in the path
94 * All the states a peer participating in a tunnel can be in.
99 * Path to the peer not known yet
104 * Request sent, not yet answered.
109 * Peer connected and ready to accept data
114 * Peer connected previosly but not responding
116 MESH_PEER_RECONNECTING
121 * Struct containing all information regarding a given peer
131 * Is the peer reachable? Is the peer even connected?
133 enum MeshPeerState state;
136 * Last time we heard from this peer
138 struct GNUNET_TIME_Absolute last_contact;
141 * Number of attempts to reconnect so far
143 int n_reconnect_attempts;
146 * Paths to reach the peer
148 struct MeshPath *path;
149 struct MeshPath *path_tail;
152 * Handle to stop the DHT search for a path to this peer
154 struct GNUNET_DHT_GetHandle *dhtget;
159 * Data scheduled to transmit (to local client or remote peer)
166 struct MeshQueue *next;
167 struct MeshQueue *prev;
170 * Target of the data (NULL if target is client)
172 struct MeshPeerInfo *peer;
175 * Client to send the data to (NULL if target is peer)
177 struct MeshClient *client;
180 * Size of the message to transmit
185 * How old is the data?
187 struct GNUNET_TIME_Absolute timestamp;
192 struct GNUNET_MessageHeader *data;
196 * Globally unique tunnel identification (owner + number)
197 * DO NOT USE OVER THE NETWORK
199 struct MESH_TunnelID {
201 * Node that owns the tunnel
206 * Tunnel number to differentiate all the tunnels owned by the node oid
207 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_MARK )
209 MESH_TunnelNumber tid;
213 struct MeshClient; /* FWD declaration */
215 * Struct containing all information regarding a tunnel
216 * For an intermediate node the improtant info used will be:
217 * - id Tunnel unique identification
218 * - paths[0] To know where to send it next
219 * - metainfo: ready, speeds, accounting
226 struct MESH_TunnelID id;
229 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK or 0 )
231 MESH_TunnelNumber local_tid;
234 * Last time the tunnel was used
236 struct GNUNET_TIME_Absolute timestamp;
239 * Peers in the tunnelindexed by PeerIdentity (MeshPeerInfo)
241 struct GNUNET_CONTAINER_MultiHashMap* peers;
244 * Number of peers that are connected and potentially ready to receive data
246 unsigned int peers_ready;
249 * Number of peers that have been added to the tunnel
251 unsigned int peers_total;
255 * Client owner of the tunnel, if any
257 struct MeshClient *client;
260 * Messages ready to transmit
262 struct MeshQueue *queue_head;
263 struct MeshQueue *queue_tail;
268 * Struct containing information about a client of the service
275 struct MeshClient *next;
276 struct MeshClient *prev;
279 * Tunnels that belong to this client, indexed by local id
281 struct GNUNET_CONTAINER_MultiHashMap* tunnels;
284 * Handle to communicate with the client
286 struct GNUNET_SERVER_Client *handle;
289 * Applications that this client has claimed to provide
291 GNUNET_MESH_ApplicationType *apps;
292 unsigned int app_counter;
295 * Messages that this client has declared interest in
298 unsigned int type_counter;
302 /******************************************************************************/
303 /*********************** GLOBAL VARIABLES ****************************/
304 /******************************************************************************/
309 static struct MeshClient *clients;
310 static struct MeshClient *clients_tail;
313 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
315 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
318 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
320 struct GNUNET_CONTAINER_MultiHashMap *peers;
323 * Handle to communicate with core
325 static struct GNUNET_CORE_Handle *core_handle;
330 static struct GNUNET_DHT_Handle *dht_handle;
333 * Local peer own ID (memory efficient handle)
335 static GNUNET_PEER_Id myid;
338 * Tunnel ID for the next created tunnel (global tunnel number)
340 static MESH_TunnelNumber next_tid;
342 /******************************************************************************/
343 /****************** GENERAL HELPER FUNCTIONS ************************/
344 /******************************************************************************/
347 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
348 * and inster it in the appropiate structures if the peer is not known yet.
349 * @param peer Identity of the peer
350 * @return Existing or newly created peer info
352 static struct MeshPeerInfo *
353 retireve_peer_info (const struct GNUNET_PeerIdentity *peer)
355 struct MeshPeerInfo * peer_info;
357 peer_info = GNUNET_CONTAINER_multihashmap_get(peers,
359 if (NULL == peer_info) {
360 peer_info = (struct MeshPeerInfo *)
361 GNUNET_malloc(sizeof(struct MeshPeerInfo));
362 GNUNET_CONTAINER_multihashmap_put(peers,
365 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
366 peer_info->id = GNUNET_PEER_intern(peer);
367 peer_info->state = MESH_PEER_SEARCHING;
374 * find the first peer whom to send a packet to go down this path
375 * @param path The path to use
376 * @return short id of the next peer, myid in case of local delivery,
377 * or 0 in case of error
379 static GNUNET_PEER_Id
380 retrieve_first_hop (struct MeshPath *path)
384 if (NULL == path) return 0;
385 if (path->in_use == 0) return 0;
387 for (i = 0; i < path->length; i++) {
388 if (path->peers[i] == myid) {
389 if (i < path->length - 1) {
390 return path->peers[i+1];
400 add_path_to_peer(struct MeshPeerInfo *peer_info, struct MeshPath *path)
407 * Check if client has registered with the service and has not disconnected
408 * @param client the client to check
409 * @return non-NULL if client exists in the global DLL
411 static struct MeshClient *
412 retrieve_client (struct GNUNET_SERVER_Client *client)
414 struct MeshClient *c;
418 if (c->handle == client) return c;
426 * Search for a tunnel among the tunnels for a client
427 * @param client the client whose tunnels to search in
428 * @param tid the local id of the tunnel
429 * @return tunnel handler, NULL if doesn't exist
431 static struct MeshTunnel *
432 retrieve_tunnel_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
434 GNUNET_HashCode hash;
436 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
437 return GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
441 * Search for a tunnel by global ID using PEER_ID
442 * @param pi owner of the tunnel
443 * @param tid global tunnel number
444 * @return tunnel handler, NULL if doesn't exist
446 static struct MeshTunnel *
447 retrieve_tunnel_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
449 struct MESH_TunnelID id;
450 GNUNET_HashCode hash;
455 GNUNET_CRYPTO_hash(&id, sizeof(struct MESH_TunnelID), &hash);
456 return GNUNET_CONTAINER_multihashmap_get(tunnels, &hash);
462 * Search for a tunnel by global ID using full PeerIdentities
463 * @param oid owner of the tunnel
464 * @param tid global tunnel number
465 * @return tunnel handler, NULL if doesn't exist
467 static struct MeshTunnel *
468 retrieve_tunnel (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
472 pi = GNUNET_PEER_intern(oid);
473 GNUNET_PEER_change_rc(pi, -1);
474 return retrieve_tunnel_by_pi(pi, tid);
479 * Destroy the path and free any allocated resources linked to it
480 * @param t tunnel the path belongs to
481 * @param p the path to destroy
482 * @return GNUNET_OK on success
485 destroy_path(struct MeshTunnel *t, struct MeshPath *p)
487 GNUNET_PEER_decrement_rcs(p->peers, p->length);
488 GNUNET_free(p->peers);
495 * Destroy the peer_info and free any allocated resources linked to it
496 * @param t tunnel the path belongs to
497 * @param pi the peer_info to destroy
498 * @return GNUNET_OK on success
501 // destroy_peer_info(struct MeshTunnel *t, struct MeshPeerInfo *pi)
503 // GNUNET_PEER_change_rc(pi->id, -1);
504 // /* FIXME delete from list */
512 * Destroy the tunnel and free any allocated resources linked to it
513 * @param c client the tunnel belongs to
514 * @param t the tunnel to destroy
515 * @return GNUNET_OK on success
518 destroy_tunnel(struct MeshClient *c, struct MeshTunnel *t)
520 // struct MeshPath *path;
521 GNUNET_HashCode hash;
524 if (NULL == t) return GNUNET_OK;
527 // for (path = t->paths_head; path != NULL; path = t->paths_head) {
528 // if(GNUNET_OK != destroy_path(t, path)) r = GNUNET_SYSERR;
531 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
532 if(GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t)) {
536 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
538 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t))
546 /******************************************************************************/
547 /**************** MESH NETWORK HANDLER HELPERS ***********************/
548 /******************************************************************************/
551 * Function called to notify a client about the socket
552 * being ready to queue more data. "buf" will be
553 * NULL and "size" zero if the socket was closed for
554 * writing in the meantime.
557 * @param size number of bytes available in buf
558 * @param buf where the callee should write the message
559 * @return number of bytes written to buf
562 send_core_create_path_for_peer (void *cls, size_t size, void *buf)
564 struct MeshPeerInfo *peer_info = cls;
565 struct GNUNET_MESH_ManipulatePath *msg;
567 struct GNUNET_PeerIdentity *peer_ptr;
568 struct GNUNET_PeerIdentity id;
572 if (0 == size && NULL == buf) {
573 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Retransmitting create path\n");
574 GNUNET_PEER_resolve(retrieve_first_hop(peer_info->path), &id);
575 GNUNET_CORE_notify_transmit_ready(core_handle,
578 GNUNET_TIME_UNIT_FOREVER_REL,
580 sizeof(struct GNUNET_MESH_ManipulatePath)
581 + (peer_info->path->length
582 * sizeof (struct GNUNET_PeerIdentity)),
583 &send_core_create_path_for_peer,
594 if (p == NULL) return 0; // TODO Notify ERROR Path not found
596 size_needed = sizeof(struct GNUNET_MESH_ManipulatePath)
597 + p->length * sizeof(struct GNUNET_PeerIdentity);
598 if (size < size_needed) {
599 // TODO retry? cancel?
603 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
604 msg->header.size = htons(size_needed);
605 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
607 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
608 for (i = 0; i < p->length; i++) {
609 GNUNET_PEER_resolve(p->peers[i], peer_ptr++);
612 peer_info->state = MESH_PEER_WAITING; // TODO maybe already ready?
619 * Send another peer a notification to destroy a tunnel
620 * @param cls The tunnel to destroy
621 * @param size Size in the buffer
622 * @param buf Memory where to put the data to transmit
623 * @return Size of data put in buffer
626 send_p2p_tunnel_destroy(void *cls, size_t size, void *buf)
628 struct MeshTunnel *t = cls;
629 struct MeshClient *c;
630 struct GNUNET_MESH_TunnelMessage *msg;
634 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY); /*FIXME*/
635 msg->header.size = htons(sizeof(struct GNUNET_MESH_TunnelMessage));
636 msg->tunnel_id = htonl(t->id.tid);
638 destroy_tunnel(c, t);
639 return sizeof(struct GNUNET_MESH_TunnelMessage);
644 /******************************************************************************/
645 /******************** MESH NETWORK HANDLERS **************************/
646 /******************************************************************************/
650 * Core handler for path creation
651 * struct GNUNET_CORE_MessageHandler
654 * @param message message
655 * @param peer peer identity this notification is about
656 * @param atsi performance data
657 * @return GNUNET_OK to keep the connection open,
658 * GNUNET_SYSERR to close it (signal serious error)
662 handle_mesh_path_create (void *cls,
663 const struct GNUNET_PeerIdentity *peer,
664 const struct GNUNET_MessageHeader *message,
665 const struct GNUNET_TRANSPORT_ATS_Information
669 /* Find origin & self */
670 /* Search for origin in local tunnels */
671 /* Create tunnel / add path */
672 /* Retransmit to next link in chain, if any (core_notify + callback) */
678 * Core handler for mesh network traffic
681 * @param message message
682 * @param peer peer identity this notification is about
683 * @param atsi performance data
684 * @return GNUNET_OK to keep the connection open,
685 * GNUNET_SYSERR to close it (signal serious error)
688 handle_mesh_network_traffic (void *cls,
689 const struct GNUNET_PeerIdentity *peer,
690 const struct GNUNET_MessageHeader *message,
691 const struct GNUNET_TRANSPORT_ATS_Information
694 if (GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
695 /* Retransmit to next in path of tunnel identified by message */
697 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
698 /* Retransmit to previous in path of tunnel identified by message */
705 * Functions to handle messages from core
707 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
708 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
709 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
710 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
716 /******************************************************************************/
717 /**************** MESH LOCAL HANDLER HELPERS ***********************/
718 /******************************************************************************/
721 * delete_tunnel_entry: iterator for deleting each tunnel that belongs to a
722 * client when the client disconnects.
723 * @param cls closure (client that is disconnecting)
724 * @param key the hash of the local tunnel id (used to access the hashmap)
725 * @param value the value stored at the key (tunnel to destroy)
726 * @return GNUNET_OK on success
729 delete_tunnel_entry (void *cls, const GNUNET_HashCode * key, void *value) {
731 r = destroy_tunnel((struct MeshClient *) cls, (struct MeshTunnel *) value);
737 * notify_client_connection_failure: notify a client that the connection to the
738 * requested remote peer is not possible (for instance, no route found)
739 * Function called when the socket is ready to queue more data. "buf" will be
740 * NULL and "size" zero if the socket was closed for writing in the meantime.
743 * @param size number of bytes available in buf
744 * @param buf where the callee should write the message
745 * @return number of bytes written to buf
748 notify_client_connection_failure (void *cls, size_t size, void *buf)
751 struct MeshPeerInfo *peer_info;
752 struct GNUNET_MESH_PeerControl *msg;
753 struct GNUNET_PeerIdentity id;
755 if (0 == size && NULL == buf) {
756 // TODO retry? cancel?
760 size_needed = sizeof(struct GNUNET_MESH_PeerControl);
761 peer_info = (struct MeshPeerInfo *) cls;
762 msg = (struct GNUNET_MESH_PeerControl *) buf;
763 msg->header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
764 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
765 // msg->tunnel_id = htonl(peer_info->t->tid);
766 GNUNET_PEER_resolve(peer_info->id, &id);
767 memcpy(&msg->peer, &id, sizeof(struct GNUNET_PeerIdentity));
774 * Function to process paths received for a new peer addition. The recorded
775 * paths form the initial tunnel, which can be optimized later.
776 * Called on each result obtained for the DHT search.
779 * @param exp when will this value expire
780 * @param key key of the result
781 * @param get_path NULL-terminated array of pointers
782 * to the peers on reverse GET path (or NULL if not recorded)
783 * @param put_path NULL-terminated array of pointers
784 * to the peers on the PUT path (or NULL if not recorded)
785 * @param type type of the result
786 * @param size number of bytes in data
787 * @param data pointer to the result data
790 dht_get_response_handler(void *cls,
791 struct GNUNET_TIME_Absolute exp,
792 const GNUNET_HashCode * key,
793 const struct GNUNET_PeerIdentity * const *get_path,
794 const struct GNUNET_PeerIdentity * const *put_path,
795 enum GNUNET_BLOCK_Type type,
799 struct MeshPeerInfo *peer_info = cls;
801 struct GNUNET_PeerIdentity pi;
804 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path) {
805 // Find ourselves some alternate initial path to the destination: retry
806 GNUNET_DHT_get_stop(peer_info->dhtget);
807 GNUNET_PEER_resolve(peer_info->id, &pi);
808 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
809 GNUNET_TIME_UNIT_FOREVER_REL,
810 GNUNET_BLOCK_TYPE_ANY,
812 4, /* replication level */
813 GNUNET_DHT_RO_RECORD_ROUTE,
814 NULL, /* bloom filter */
818 dht_get_response_handler,
822 p = GNUNET_malloc(sizeof(struct MeshPath));
823 for (i = 0; get_path[i] != NULL; i++);
824 for (i--; i >= 0; i--) {
825 p->peers = GNUNET_realloc(p->peers,
826 sizeof(GNUNET_PEER_Id) * (p->length + 1));
827 p->peers[p->length] = GNUNET_PEER_intern(get_path[i]);
830 for (i = 0; put_path[i] != NULL; i++);
831 for (i--; i >= 0; i--) {
832 p->peers = GNUNET_realloc(p->peers,
833 sizeof(GNUNET_PEER_Id) * (p->length + 1));
834 p->peers[p->length] = GNUNET_PEER_intern(put_path[i]);
837 add_path_to_peer(peer_info, p);
838 GNUNET_CORE_notify_transmit_ready(core_handle,
841 GNUNET_TIME_UNIT_FOREVER_REL,
843 sizeof(struct GNUNET_MESH_ManipulatePath)
845 * sizeof (struct GNUNET_PeerIdentity)),
846 &send_core_create_path_for_peer,
852 /******************************************************************************/
853 /********************* MESH LOCAL HANDLES **************************/
854 /******************************************************************************/
858 * Handler for client disconnection
861 * @param client identification of the client; NULL
862 * for the last call when the server is destroyed
865 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
867 struct MeshClient *c;
868 struct MeshClient *next;
870 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
871 "client disconnected\n");
874 if (c->handle == client) {
875 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
876 " matching client found, cleaning\n");
877 GNUNET_CONTAINER_multihashmap_iterate(c->tunnels,
878 &delete_tunnel_entry,
880 GNUNET_CONTAINER_multihashmap_destroy(c->tunnels);
881 if(0 != c->app_counter) GNUNET_free (c->apps);
882 if(0 != c->type_counter) GNUNET_free (c->types);
883 GNUNET_CONTAINER_DLL_remove(clients, clients_tail, c);
888 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
893 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
900 * Handler for new clients
903 * @param client identification of the client
904 * @param message the actual message, which includes messages the client wants
907 handle_local_new_client (void *cls,
908 struct GNUNET_SERVER_Client *client,
909 const struct GNUNET_MessageHeader *message)
911 struct GNUNET_MESH_ClientConnect *cc_msg;
912 struct MeshClient *c;
917 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
918 /* Check data sanity */
919 size = ntohs(message->size) - sizeof(struct GNUNET_MESH_ClientConnect);
920 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
921 types = ntohs(cc_msg->types);
922 apps = ntohs(cc_msg->applications);
924 types * sizeof(uint16_t) + apps * sizeof(GNUNET_MESH_ApplicationType))
927 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
931 /* Create new client structure */
932 c = GNUNET_malloc(sizeof(struct MeshClient));
935 c->type_counter = types;
936 c->types = GNUNET_malloc(types * sizeof(uint16_t));
937 memcpy(c->types, &message[1], types * sizeof(uint16_t));
940 c->app_counter = apps;
941 c->apps = GNUNET_malloc(apps * sizeof(GNUNET_MESH_ApplicationType));
943 &message[1] + types * sizeof(uint16_t),
944 apps * sizeof(GNUNET_MESH_ApplicationType));
946 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
947 " client has %u+%u subscriptions\n",
951 GNUNET_CONTAINER_DLL_insert(clients, clients_tail, c);
952 c->tunnels = GNUNET_CONTAINER_multihashmap_create(32);
954 GNUNET_SERVER_receive_done(client, GNUNET_OK);
960 * Handler for requests of new tunnels
963 * @param client identification of the client
964 * @param message the actual message
967 handle_local_tunnel_create (void *cls,
968 struct GNUNET_SERVER_Client *client,
969 const struct GNUNET_MessageHeader *message)
971 struct GNUNET_MESH_TunnelMessage *t_msg;
972 struct MeshTunnel *t;
973 struct MeshClient *c;
974 GNUNET_HashCode hash;
976 /* Sanity check for client registration */
977 if (NULL == (c = retrieve_client(client))) {
979 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
983 /* Message sanity check */
984 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
986 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
990 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
991 /* Sanity check for tunnel numbering */
992 if (0 == (ntohl(t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)) {
994 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
997 /* Sanity check for duplicate tunnel IDs */
998 if(NULL != retrieve_tunnel_by_local_id(c, ntohl(t_msg->tunnel_id))) {
1000 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1004 t = GNUNET_malloc(sizeof(struct MeshTunnel));
1005 while (NULL != retrieve_tunnel_by_pi(myid, next_tid))
1006 next_tid = (next_tid + 1) % GNUNET_MESH_LOCAL_TUNNEL_ID_MARK;
1007 t->id.tid = next_tid++;
1009 t->local_tid = ntohl(t_msg->tunnel_id);
1011 t->peers = GNUNET_CONTAINER_multihashmap_create(32);
1013 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
1015 GNUNET_CONTAINER_multihashmap_put(c->tunnels, &hash, t,
1016 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1019 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1023 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
1025 GNUNET_CONTAINER_multihashmap_put(tunnels, &hash, t,
1026 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1029 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1033 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1039 * Handler for requests of deleting tunnels
1041 * @param cls closure
1042 * @param client identification of the client
1043 * @param message the actual message
1046 handle_local_tunnel_destroy (void *cls,
1047 struct GNUNET_SERVER_Client *client,
1048 const struct GNUNET_MessageHeader *message)
1050 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
1051 struct MeshClient *c;
1052 struct MeshTunnel *t;
1053 MESH_TunnelNumber tid;
1054 GNUNET_HashCode hash;
1057 /* Sanity check for client registration */
1058 if (NULL == (c = retrieve_client(client))) {
1060 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1063 /* Message sanity check */
1064 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
1066 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1070 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
1072 /* Retrieve tunnel */
1073 tid = ntohl(tunnel_msg->tunnel_id);
1075 /* Remove from local id hashmap */
1076 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
1077 t = GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
1078 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t);
1080 /* Remove from global id hashmap */
1081 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
1082 GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t);
1084 // notify_tunnel_destroy(t);
1085 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1091 * Handler for connection requests to new peers
1093 * @param cls closure
1094 * @param client identification of the client
1095 * @param message the actual message (PeerControl)
1098 handle_local_connect_add (void *cls,
1099 struct GNUNET_SERVER_Client *client,
1100 const struct GNUNET_MessageHeader *message)
1102 struct GNUNET_MESH_PeerControl *peer_msg;
1103 struct MeshClient *c;
1104 struct MeshTunnel *t;
1105 MESH_TunnelNumber tid;
1106 struct MeshPeerInfo *peer_info;
1109 /* Sanity check for client registration */
1110 if (NULL == (c = retrieve_client(client))) {
1112 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1116 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1117 /* Sanity check for message size */
1118 if (sizeof(struct GNUNET_MESH_PeerControl)
1119 != ntohs(peer_msg->header.size))
1122 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1126 /* Tunnel exists? */
1127 tid = ntohl(peer_msg->tunnel_id);
1128 t = retrieve_tunnel_by_local_id(c, tid);
1131 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1135 /* Does client own tunnel? */
1136 if (t->client->handle != client) {
1138 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1143 peer_info = retireve_peer_info(&peer_msg->peer);
1145 /* Start DHT search if needed */
1146 if(MESH_PEER_READY != peer_info->state && NULL == peer_info->dhtget) {
1147 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
1148 GNUNET_TIME_UNIT_FOREVER_REL,
1149 GNUNET_BLOCK_TYPE_ANY,
1150 &peer_msg->peer.hashPubKey,
1151 4, /* replication level */
1152 GNUNET_DHT_RO_RECORD_ROUTE,
1153 NULL, /* bloom filter */
1156 0, /* xquery bits */
1157 dht_get_response_handler,
1161 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1167 * Handler for disconnection requests of peers in a tunnel
1169 * @param cls closure
1170 * @param client identification of the client
1171 * @param message the actual message (PeerControl)
1174 handle_local_connect_del (void *cls,
1175 struct GNUNET_SERVER_Client *client,
1176 const struct GNUNET_MessageHeader *message)
1178 struct GNUNET_MESH_PeerControl *peer_msg;
1179 struct MeshClient *c;
1180 struct MeshTunnel *t;
1181 MESH_TunnelNumber tid;
1182 GNUNET_PEER_Id peer_id;
1184 /* Sanity check for client registration */
1185 if (NULL == (c = retrieve_client(client))) {
1187 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1190 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1191 /* Sanity check for message size */
1192 if (sizeof(struct GNUNET_MESH_PeerControl)
1193 != ntohs(peer_msg->header.size))
1196 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1200 /* Tunnel exists? */
1201 tid = ntohl(peer_msg->tunnel_id);
1202 t = retrieve_tunnel_by_local_id(c, tid);
1205 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1209 /* Does client own tunnel? */
1210 if (t->client->handle != client) {
1212 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1216 /* Ok, delete peer from tunnel */
1217 peer_id = GNUNET_PEER_intern(&peer_msg->peer);
1219 /* FIXME Delete paths */
1220 /* FIXME Delete peer info */
1222 GNUNET_PEER_change_rc(peer_id, -1);
1224 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1230 * Handler for connection requests to new peers by type
1232 * @param cls closure
1233 * @param client identification of the client
1234 * @param message the actual message (ConnectPeerByType)
1237 handle_local_connect_by_type (void *cls,
1238 struct GNUNET_SERVER_Client *client,
1239 const struct GNUNET_MessageHeader *message)
1241 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
1242 MESH_TunnelNumber tid;
1243 GNUNET_MESH_ApplicationType application;
1244 struct MeshClient *c;
1245 struct MeshTunnel *t;
1247 /* Sanity check for client registration */
1248 if (NULL == (c = retrieve_client(client))) {
1250 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1254 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *)message;
1255 /* Sanity check for message size */
1256 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1257 ntohs(connect_msg->header.size))
1260 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1264 /* Tunnel exists? */
1265 tid = ntohl(connect_msg->tunnel_id);
1266 t = retrieve_tunnel_by_local_id(c, tid);
1269 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1273 /* Does client own tunnel? */
1274 if (t->client->handle != client) {
1276 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1280 /* Ok, lets find a peer offering the service */
1281 application = ntohl(connect_msg->type);
1282 application++; // FIXME silence warnings
1284 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1290 * Handler for client traffic directed to one peer
1292 * @param cls closure
1293 * @param client identification of the client
1294 * @param message the actual message
1297 handle_local_network_traffic (void *cls,
1298 struct GNUNET_SERVER_Client *client,
1299 const struct GNUNET_MessageHeader *message)
1301 struct MeshClient *c;
1302 struct MeshTunnel *t;
1303 struct GNUNET_MESH_Data *data_msg;
1304 MESH_TunnelNumber tid;
1306 /* Sanity check for client registration */
1307 if (NULL == (c = retrieve_client(client))) {
1309 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1312 data_msg = (struct GNUNET_MESH_Data *)message;
1313 /* Sanity check for message size */
1314 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1315 ntohs(data_msg->header.size))
1318 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1322 /* Tunnel exists? */
1323 tid = ntohl(data_msg->tunnel_id);
1324 t = retrieve_tunnel_by_local_id(c, tid);
1327 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1331 /* Does client own tunnel? */
1332 if (t->client->handle != client) {
1334 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1340 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1345 * Handler for client traffic directed to all peers in a tunnel
1347 * @param cls closure
1348 * @param client identification of the client
1349 * @param message the actual message
1352 handle_local_network_traffic_bcast (void *cls,
1353 struct GNUNET_SERVER_Client *client,
1354 const struct GNUNET_MessageHeader *message)
1356 struct MeshClient *c;
1357 struct MeshTunnel *t;
1358 struct GNUNET_MESH_DataBroadcast *data_msg;
1359 MESH_TunnelNumber tid;
1361 /* Sanity check for client registration */
1362 if (NULL == (c = retrieve_client(client))) {
1364 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1367 data_msg = (struct GNUNET_MESH_DataBroadcast *)message;
1368 /* Sanity check for message size */
1369 if (sizeof(struct GNUNET_MESH_PeerControl)
1370 != ntohs(data_msg->header.size))
1373 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1377 /* Tunnel exists? */
1378 tid = ntohl(data_msg->tunnel_id);
1379 t = retrieve_tunnel_by_local_id(c, tid);
1382 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1386 /* Does client own tunnel? */
1387 if (t->client->handle != client) {
1389 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1395 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1400 * Functions to handle messages from clients
1402 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1403 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
1404 {&handle_local_tunnel_create, NULL,
1405 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
1406 sizeof(struct GNUNET_MESH_TunnelMessage)},
1407 {&handle_local_tunnel_destroy, NULL,
1408 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
1409 sizeof(struct GNUNET_MESH_TunnelMessage)},
1410 {&handle_local_connect_add, NULL,
1411 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD,
1412 sizeof(struct GNUNET_MESH_PeerControl)},
1413 {&handle_local_connect_del, NULL,
1414 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL,
1415 sizeof(struct GNUNET_MESH_PeerControl)},
1416 {&handle_local_connect_by_type, NULL,
1417 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE,
1418 sizeof(struct GNUNET_MESH_ConnectPeerByType)},
1419 {&handle_local_network_traffic, NULL,
1420 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
1421 {&handle_local_network_traffic_bcast, NULL,
1422 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0},
1428 * To be called on core init/fail.
1430 * @param cls service closure
1431 * @param server handle to the server for this service
1432 * @param identity the public identity of this peer
1433 * @param publicKey the public key of this peer
1436 core_init (void *cls,
1437 struct GNUNET_CORE_Handle *server,
1438 const struct GNUNET_PeerIdentity *identity,
1439 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1443 core_handle = server;
1444 myid = GNUNET_PEER_intern(identity);
1449 * Method called whenever a given peer connects.
1451 * @param cls closure
1452 * @param peer peer identity this notification is about
1453 * @param atsi performance data for the connection
1456 core_connect (void *cls,
1457 const struct GNUNET_PeerIdentity *peer,
1458 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1460 // GNUNET_PEER_Id pid;
1461 struct MeshPeerInfo *peer_info;
1462 struct MeshPath *path;
1464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1465 "Peer connected\n");
1466 peer_info = retireve_peer_info(peer);
1467 if (myid == peer_info->id) {
1468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1471 path = GNUNET_malloc(sizeof(struct MeshPath));
1473 path->peers = GNUNET_malloc(sizeof(GNUNET_PEER_Id) * 2);
1474 path->peers[0] = myid;
1475 path->peers[1] = peer_info->id;
1476 add_path_to_peer(peer_info, path);
1481 * Method called whenever a peer disconnects.
1483 * @param cls closure
1484 * @param peer peer identity this notification is about
1487 core_disconnect (void *cls,
1489 GNUNET_PeerIdentity *peer)
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1493 "Peer disconnected\n");
1494 pid = GNUNET_PEER_intern(peer);
1495 GNUNET_PEER_change_rc(pid, -1);
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 /******************************************************************************/
1505 /********************** PERIODIC FUNCTIONS **************************/
1506 /******************************************************************************/
1509 * Iterator over peers to send keepalive packets when needed.
1512 * @param key current key code
1513 * @param value value in the hash map
1514 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
1517 path_refresh_peer (void *cls, const GNUNET_HashCode * key, void *value)
1519 struct MeshPeerInfo *pi = cls;
1520 struct GNUNET_TIME_Absolute threshold;
1521 struct GNUNET_PeerIdentity id;
1523 threshold = GNUNET_TIME_absolute_subtract(GNUNET_TIME_absolute_get(),
1526 if (pi->last_contact.abs_value < threshold.abs_value) {
1527 GNUNET_PEER_resolve(pi->path->peers[1], &id);
1528 GNUNET_CORE_notify_transmit_ready(core_handle,
1531 GNUNET_TIME_UNIT_FOREVER_REL,
1533 sizeof(struct GNUNET_MESH_ManipulatePath)
1535 * sizeof (struct GNUNET_PeerIdentity)),
1536 &send_core_create_path_for_peer,
1544 * Send keepalive packets for all routes
1550 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1552 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) return;
1553 GNUNET_CONTAINER_multihashmap_iterate(peers, path_refresh_peer, NULL);
1554 GNUNET_SCHEDULER_add_delayed(REFRESH_PATH_TIME,
1561 /******************************************************************************/
1562 /************************ MAIN FUNCTIONS ****************************/
1563 /******************************************************************************/
1566 * Task run during shutdown.
1572 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1576 if (core_handle != NULL) {
1577 GNUNET_CORE_disconnect (core_handle);
1580 if (dht_handle != NULL) {
1581 GNUNET_DHT_disconnect (dht_handle);
1584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1589 * Process mesh requests.
1591 * @param cls closure
1592 * @param server the initialized server
1593 * @param c configuration to use
1597 struct GNUNET_SERVER_Handle *server,
1598 const struct GNUNET_CONFIGURATION_Handle *c)
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1601 "starting to run\n");
1602 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1603 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1604 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1606 NULL, /* Closure passed to MESH functions */
1607 &core_init, /* Call core_init once connected */
1608 &core_connect, /* Handle connects */
1609 &core_disconnect, /* remove peers on disconnects */
1610 NULL, /* Do we care about "status" updates? */
1611 NULL, /* Don't notify about all incoming messages */
1612 GNUNET_NO, /* For header only in notification */
1613 NULL, /* Don't notify about all outbound messages */
1614 GNUNET_NO, /* For header-only out notification */
1615 core_handlers); /* Register these handlers */
1616 if (core_handle == NULL) {
1619 dht_handle = GNUNET_DHT_connect(c, 64);
1620 if (dht_handle == NULL) {
1625 tunnels = GNUNET_CONTAINER_multihashmap_create(32);
1626 peers = GNUNET_CONTAINER_multihashmap_create(32);
1628 clients_tail = NULL;
1630 /* Path keepalive */
1631 GNUNET_SCHEDULER_add_delayed(REFRESH_PATH_TIME,
1635 /* Scheduled the task to clean up when shutdown is called */
1636 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1637 &shutdown_task, NULL);
1639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1644 * The main function for the mesh service.
1646 * @param argc number of arguments from the command line
1647 * @param argv command line arguments
1648 * @return 0 ok, 1 on error
1651 main (int argc, char *const *argv)
1656 GNUNET_SERVICE_run (argc,
1659 GNUNET_SERVICE_OPTION_NONE,
1660 &run, NULL)) ? 0 : 1;
1661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,