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 get_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 get_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)
408 * Check if client has registered with the service and has not disconnected
409 * @param client the client to check
410 * @return non-NULL if client exists in the global DLL
412 static struct MeshClient *
413 retrieve_client (struct GNUNET_SERVER_Client *client)
415 struct MeshClient *c;
419 if (c->handle == client) return c;
427 * Search for a tunnel among the tunnels for a client
428 * @param client the client whose tunnels to search in
429 * @param tid the local id of the tunnel
430 * @return tunnel handler, NULL if doesn't exist
432 static struct MeshTunnel *
433 retrieve_tunnel_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
435 GNUNET_HashCode hash;
437 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
438 return GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
442 * Search for a tunnel by global ID using PEER_ID
443 * @param pi owner of the tunnel
444 * @param tid global tunnel number
445 * @return tunnel handler, NULL if doesn't exist
447 static struct MeshTunnel *
448 retrieve_tunnel_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
450 struct MESH_TunnelID id;
451 GNUNET_HashCode hash;
456 GNUNET_CRYPTO_hash(&id, sizeof(struct MESH_TunnelID), &hash);
457 return GNUNET_CONTAINER_multihashmap_get(tunnels, &hash);
463 * Search for a tunnel by global ID using full PeerIdentities
464 * @param oid owner of the tunnel
465 * @param tid global tunnel number
466 * @return tunnel handler, NULL if doesn't exist
468 static struct MeshTunnel *
469 retrieve_tunnel (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
473 pi = GNUNET_PEER_intern(oid);
474 GNUNET_PEER_change_rc(pi, -1);
475 return retrieve_tunnel_by_pi(pi, tid);
480 * Destroy the path and free any allocated resources linked to it
481 * @param t tunnel the path belongs to
482 * @param p the path to destroy
483 * @return GNUNET_OK on success
486 destroy_path(struct MeshTunnel *t, struct MeshPath *p)
488 GNUNET_PEER_decrement_rcs(p->peers, p->length);
489 GNUNET_free(p->peers);
496 * Destroy the peer_info and free any allocated resources linked to it
497 * @param t tunnel the path belongs to
498 * @param pi the peer_info to destroy
499 * @return GNUNET_OK on success
502 destroy_peer_info(struct MeshPeerInfo *pi)
504 GNUNET_HashCode hash;
505 struct GNUNET_PeerIdentity id;
507 GNUNET_PEER_resolve(pi->id, &id);
508 GNUNET_PEER_change_rc(pi->id, -1);
509 GNUNET_CRYPTO_hash(&id, sizeof(struct GNUNET_PeerIdentity), &hash);
511 GNUNET_CONTAINER_multihashmap_remove(peers, &hash, pi);
519 * Destroy the tunnel and free any allocated resources linked to it
520 * @param c client the tunnel belongs to
521 * @param t the tunnel to destroy
522 * @return GNUNET_OK on success
525 destroy_tunnel(struct MeshClient *c, struct MeshTunnel *t)
527 // struct MeshPath *path;
528 GNUNET_HashCode hash;
531 if (NULL == t) return GNUNET_OK;
533 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
534 if(GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t)) {
538 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
540 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t))
548 /******************************************************************************/
549 /**************** MESH NETWORK HANDLER HELPERS ***********************/
550 /******************************************************************************/
553 * Function called to notify a client about the socket
554 * being ready to queue more data. "buf" will be
555 * NULL and "size" zero if the socket was closed for
556 * writing in the meantime.
559 * @param size number of bytes available in buf
560 * @param buf where the callee should write the message
561 * @return number of bytes written to buf
564 send_core_create_path_for_peer (void *cls, size_t size, void *buf)
566 struct MeshPeerInfo *peer_info = cls;
567 struct GNUNET_MESH_ManipulatePath *msg;
569 struct GNUNET_PeerIdentity *peer_ptr;
570 struct GNUNET_PeerIdentity id;
574 if (0 == size && NULL == buf) {
575 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Retransmitting create path\n");
576 GNUNET_PEER_resolve(get_first_hop(peer_info->path), &id);
577 GNUNET_CORE_notify_transmit_ready(core_handle,
580 GNUNET_TIME_UNIT_FOREVER_REL,
582 sizeof(struct GNUNET_MESH_ManipulatePath)
583 + (peer_info->path->length
584 * sizeof (struct GNUNET_PeerIdentity)),
585 &send_core_create_path_for_peer,
596 if (p == NULL) return 0; // TODO Notify ERROR Path not found
598 size_needed = sizeof(struct GNUNET_MESH_ManipulatePath)
599 + p->length * sizeof(struct GNUNET_PeerIdentity);
600 if (size < size_needed) {
601 // TODO retry? cancel?
605 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
606 msg->header.size = htons(size_needed);
607 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
609 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
610 for (i = 0; i < p->length; i++) {
611 GNUNET_PEER_resolve(p->peers[i], peer_ptr++);
614 peer_info->state = MESH_PEER_WAITING; // TODO maybe already ready?
621 * Send another peer a notification to destroy a tunnel
622 * @param cls The tunnel to destroy
623 * @param size Size in the buffer
624 * @param buf Memory where to put the data to transmit
625 * @return Size of data put in buffer
628 send_p2p_tunnel_destroy(void *cls, size_t size, void *buf)
630 struct MeshTunnel *t = cls;
631 struct MeshClient *c;
632 struct GNUNET_MESH_TunnelMessage *msg;
636 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY); /*FIXME*/
637 msg->header.size = htons(sizeof(struct GNUNET_MESH_TunnelMessage));
638 msg->tunnel_id = htonl(t->id.tid);
640 destroy_tunnel(c, t);
641 return sizeof(struct GNUNET_MESH_TunnelMessage);
646 /******************************************************************************/
647 /******************** MESH NETWORK HANDLERS **************************/
648 /******************************************************************************/
652 * Core handler for path creation
653 * struct GNUNET_CORE_MessageHandler
656 * @param message message
657 * @param peer peer identity this notification is about
658 * @param atsi performance data
659 * @return GNUNET_OK to keep the connection open,
660 * GNUNET_SYSERR to close it (signal serious error)
664 handle_mesh_path_create (void *cls,
665 const struct GNUNET_PeerIdentity *peer,
666 const struct GNUNET_MessageHeader *message,
667 const struct GNUNET_TRANSPORT_ATS_Information
671 /* Find origin & self */
672 /* Search for origin in local tunnels */
673 /* Create tunnel / add path */
674 /* Retransmit to next link in chain, if any (core_notify + callback) */
680 * Core handler for mesh network traffic
683 * @param message message
684 * @param peer peer identity this notification is about
685 * @param atsi performance data
686 * @return GNUNET_OK to keep the connection open,
687 * GNUNET_SYSERR to close it (signal serious error)
690 handle_mesh_network_traffic (void *cls,
691 const struct GNUNET_PeerIdentity *peer,
692 const struct GNUNET_MessageHeader *message,
693 const struct GNUNET_TRANSPORT_ATS_Information
696 if (GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
697 /* Retransmit to next in path of tunnel identified by message */
699 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
700 /* Retransmit to previous in path of tunnel identified by message */
707 * Functions to handle messages from core
709 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
710 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
711 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
712 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
718 /******************************************************************************/
719 /**************** MESH LOCAL HANDLER HELPERS ***********************/
720 /******************************************************************************/
723 * delete_tunnel_entry: iterator for deleting each tunnel that belongs to a
724 * client when the client disconnects.
725 * @param cls closure (client that is disconnecting)
726 * @param key the hash of the local tunnel id (used to access the hashmap)
727 * @param value the value stored at the key (tunnel to destroy)
728 * @return GNUNET_OK on success
731 delete_tunnel_entry (void *cls, const GNUNET_HashCode * key, void *value) {
733 r = destroy_tunnel((struct MeshClient *) cls, (struct MeshTunnel *) value);
739 * notify_client_connection_failure: notify a client that the connection to the
740 * requested remote peer is not possible (for instance, no route found)
741 * Function called when the socket is ready to queue more data. "buf" will be
742 * NULL and "size" zero if the socket was closed for writing in the meantime.
745 * @param size number of bytes available in buf
746 * @param buf where the callee should write the message
747 * @return number of bytes written to buf
750 notify_client_connection_failure (void *cls, size_t size, void *buf)
753 struct MeshPeerInfo *peer_info;
754 struct GNUNET_MESH_PeerControl *msg;
755 struct GNUNET_PeerIdentity id;
757 if (0 == size && NULL == buf) {
758 // TODO retry? cancel?
762 size_needed = sizeof(struct GNUNET_MESH_PeerControl);
763 peer_info = (struct MeshPeerInfo *) cls;
764 msg = (struct GNUNET_MESH_PeerControl *) buf;
765 msg->header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
766 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
767 // msg->tunnel_id = htonl(peer_info->t->tid);
768 GNUNET_PEER_resolve(peer_info->id, &id);
769 memcpy(&msg->peer, &id, sizeof(struct GNUNET_PeerIdentity));
777 * Send keepalive packets for a peer
783 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
785 struct MeshPeerInfo *peer_info = cls;
786 struct GNUNET_PeerIdentity id;
788 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) return;
789 GNUNET_PEER_resolve(get_first_hop(peer_info->path), &id);
790 GNUNET_CORE_notify_transmit_ready(core_handle,
793 GNUNET_TIME_UNIT_FOREVER_REL,
795 sizeof(struct GNUNET_MESH_ManipulatePath)
796 + (peer_info->path->length
797 * sizeof (struct GNUNET_PeerIdentity)),
798 &send_core_create_path_for_peer,
806 * Function to process paths received for a new peer addition. The recorded
807 * paths form the initial tunnel, which can be optimized later.
808 * Called on each result obtained for the DHT search.
811 * @param exp when will this value expire
812 * @param key key of the result
813 * @param get_path NULL-terminated array of pointers
814 * to the peers on reverse GET path (or NULL if not recorded)
815 * @param put_path NULL-terminated array of pointers
816 * to the peers on the PUT path (or NULL if not recorded)
817 * @param type type of the result
818 * @param size number of bytes in data
819 * @param data pointer to the result data
822 dht_get_response_handler(void *cls,
823 struct GNUNET_TIME_Absolute exp,
824 const GNUNET_HashCode * key,
825 const struct GNUNET_PeerIdentity * const *get_path,
826 const struct GNUNET_PeerIdentity * const *put_path,
827 enum GNUNET_BLOCK_Type type,
831 struct MeshPeerInfo *peer_info = cls;
833 struct GNUNET_PeerIdentity pi;
836 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path) {
837 // Find ourselves some alternate initial path to the destination: retry
838 GNUNET_DHT_get_stop(peer_info->dhtget);
839 GNUNET_PEER_resolve(peer_info->id, &pi);
840 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
841 GNUNET_TIME_UNIT_FOREVER_REL,
842 GNUNET_BLOCK_TYPE_ANY,
844 4, /* replication level */
845 GNUNET_DHT_RO_RECORD_ROUTE,
846 NULL, /* bloom filter */
850 dht_get_response_handler,
854 p = GNUNET_malloc(sizeof(struct MeshPath));
855 for (i = 0; get_path[i] != NULL; i++);
856 for (i--; i >= 0; i--) {
857 p->peers = GNUNET_realloc(p->peers,
858 sizeof(GNUNET_PEER_Id) * (p->length + 1));
859 p->peers[p->length] = GNUNET_PEER_intern(get_path[i]);
862 for (i = 0; put_path[i] != NULL; i++);
863 for (i--; i >= 0; i--) {
864 p->peers = GNUNET_realloc(p->peers,
865 sizeof(GNUNET_PEER_Id) * (p->length + 1));
866 p->peers[p->length] = GNUNET_PEER_intern(put_path[i]);
869 add_path_to_peer(peer_info, p);
870 GNUNET_CORE_notify_transmit_ready(core_handle,
873 GNUNET_TIME_UNIT_FOREVER_REL,
875 sizeof(struct GNUNET_MESH_ManipulatePath)
877 * sizeof (struct GNUNET_PeerIdentity)),
878 &send_core_create_path_for_peer,
880 GNUNET_SCHEDULER_add_delayed(REFRESH_PATH_TIME, &path_refresh, peer_info);
885 /******************************************************************************/
886 /********************* MESH LOCAL HANDLES **************************/
887 /******************************************************************************/
891 * Handler for client disconnection
894 * @param client identification of the client; NULL
895 * for the last call when the server is destroyed
898 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
900 struct MeshClient *c;
901 struct MeshClient *next;
903 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
904 "client disconnected\n");
907 if (c->handle == client) {
908 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
909 " matching client found, cleaning\n");
910 GNUNET_CONTAINER_multihashmap_iterate(c->tunnels,
911 &delete_tunnel_entry,
913 GNUNET_CONTAINER_multihashmap_destroy(c->tunnels);
914 if(0 != c->app_counter) GNUNET_free (c->apps);
915 if(0 != c->type_counter) GNUNET_free (c->types);
916 GNUNET_CONTAINER_DLL_remove(clients, clients_tail, c);
921 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
926 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
933 * Handler for new clients
936 * @param client identification of the client
937 * @param message the actual message, which includes messages the client wants
940 handle_local_new_client (void *cls,
941 struct GNUNET_SERVER_Client *client,
942 const struct GNUNET_MessageHeader *message)
944 struct GNUNET_MESH_ClientConnect *cc_msg;
945 struct MeshClient *c;
950 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
951 /* Check data sanity */
952 size = ntohs(message->size) - sizeof(struct GNUNET_MESH_ClientConnect);
953 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
954 types = ntohs(cc_msg->types);
955 apps = ntohs(cc_msg->applications);
957 types * sizeof(uint16_t) + apps * sizeof(GNUNET_MESH_ApplicationType))
960 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
964 /* Create new client structure */
965 c = GNUNET_malloc(sizeof(struct MeshClient));
968 c->type_counter = types;
969 c->types = GNUNET_malloc(types * sizeof(uint16_t));
970 memcpy(c->types, &message[1], types * sizeof(uint16_t));
973 c->app_counter = apps;
974 c->apps = GNUNET_malloc(apps * sizeof(GNUNET_MESH_ApplicationType));
976 &message[1] + types * sizeof(uint16_t),
977 apps * sizeof(GNUNET_MESH_ApplicationType));
979 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
980 " client has %u+%u subscriptions\n",
984 GNUNET_CONTAINER_DLL_insert(clients, clients_tail, c);
985 c->tunnels = GNUNET_CONTAINER_multihashmap_create(32);
987 GNUNET_SERVER_receive_done(client, GNUNET_OK);
993 * Handler for requests of new tunnels
996 * @param client identification of the client
997 * @param message the actual message
1000 handle_local_tunnel_create (void *cls,
1001 struct GNUNET_SERVER_Client *client,
1002 const struct GNUNET_MessageHeader *message)
1004 struct GNUNET_MESH_TunnelMessage *t_msg;
1005 struct MeshTunnel *t;
1006 struct MeshClient *c;
1007 GNUNET_HashCode hash;
1009 /* Sanity check for client registration */
1010 if (NULL == (c = retrieve_client(client))) {
1012 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1016 /* Message sanity check */
1017 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
1019 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1023 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
1024 /* Sanity check for tunnel numbering */
1025 if (0 == (ntohl(t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)) {
1027 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1030 /* Sanity check for duplicate tunnel IDs */
1031 if(NULL != retrieve_tunnel_by_local_id(c, ntohl(t_msg->tunnel_id))) {
1033 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1037 t = GNUNET_malloc(sizeof(struct MeshTunnel));
1038 while (NULL != retrieve_tunnel_by_pi(myid, next_tid))
1039 next_tid = (next_tid + 1) % GNUNET_MESH_LOCAL_TUNNEL_ID_MARK;
1040 t->id.tid = next_tid++;
1042 t->local_tid = ntohl(t_msg->tunnel_id);
1044 t->peers = GNUNET_CONTAINER_multihashmap_create(32);
1046 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
1048 GNUNET_CONTAINER_multihashmap_put(c->tunnels, &hash, t,
1049 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1052 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1056 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
1058 GNUNET_CONTAINER_multihashmap_put(tunnels, &hash, t,
1059 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1062 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1066 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1072 * Handler for requests of deleting tunnels
1074 * @param cls closure
1075 * @param client identification of the client
1076 * @param message the actual message
1079 handle_local_tunnel_destroy (void *cls,
1080 struct GNUNET_SERVER_Client *client,
1081 const struct GNUNET_MessageHeader *message)
1083 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
1084 struct MeshClient *c;
1085 struct MeshTunnel *t;
1086 MESH_TunnelNumber tid;
1087 GNUNET_HashCode hash;
1090 /* Sanity check for client registration */
1091 if (NULL == (c = retrieve_client(client))) {
1093 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1096 /* Message sanity check */
1097 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
1099 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1103 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
1105 /* Retrieve tunnel */
1106 tid = ntohl(tunnel_msg->tunnel_id);
1108 /* Remove from local id hashmap */
1109 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
1110 t = GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
1111 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t);
1113 /* Remove from global id hashmap */
1114 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
1115 GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t);
1117 // notify_tunnel_destroy(t);
1118 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1124 * Handler for connection requests to new peers
1126 * @param cls closure
1127 * @param client identification of the client
1128 * @param message the actual message (PeerControl)
1131 handle_local_connect_add (void *cls,
1132 struct GNUNET_SERVER_Client *client,
1133 const struct GNUNET_MessageHeader *message)
1135 struct GNUNET_MESH_PeerControl *peer_msg;
1136 struct MeshClient *c;
1137 struct MeshTunnel *t;
1138 MESH_TunnelNumber tid;
1139 struct MeshPeerInfo *peer_info;
1142 /* Sanity check for client registration */
1143 if (NULL == (c = retrieve_client(client))) {
1145 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1149 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1150 /* Sanity check for message size */
1151 if (sizeof(struct GNUNET_MESH_PeerControl)
1152 != ntohs(peer_msg->header.size))
1155 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1159 /* Tunnel exists? */
1160 tid = ntohl(peer_msg->tunnel_id);
1161 t = retrieve_tunnel_by_local_id(c, tid);
1164 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1168 /* Does client own tunnel? */
1169 if (t->client->handle != client) {
1171 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1176 peer_info = get_peer_info(&peer_msg->peer);
1178 /* Start DHT search if needed */
1179 if(MESH_PEER_READY != peer_info->state && NULL == peer_info->dhtget) {
1180 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
1181 GNUNET_TIME_UNIT_FOREVER_REL,
1182 GNUNET_BLOCK_TYPE_ANY,
1183 &peer_msg->peer.hashPubKey,
1184 4, /* replication level */
1185 GNUNET_DHT_RO_RECORD_ROUTE,
1186 NULL, /* bloom filter */
1189 0, /* xquery bits */
1190 dht_get_response_handler,
1194 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1200 * Handler for disconnection requests of peers in a tunnel
1202 * @param cls closure
1203 * @param client identification of the client
1204 * @param message the actual message (PeerControl)
1207 handle_local_connect_del (void *cls,
1208 struct GNUNET_SERVER_Client *client,
1209 const struct GNUNET_MessageHeader *message)
1211 struct GNUNET_MESH_PeerControl *peer_msg;
1212 struct MeshClient *c;
1213 struct MeshTunnel *t;
1214 MESH_TunnelNumber tid;
1215 GNUNET_PEER_Id peer_id;
1217 /* Sanity check for client registration */
1218 if (NULL == (c = retrieve_client(client))) {
1220 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1223 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1224 /* Sanity check for message size */
1225 if (sizeof(struct GNUNET_MESH_PeerControl)
1226 != ntohs(peer_msg->header.size))
1229 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1233 /* Tunnel exists? */
1234 tid = ntohl(peer_msg->tunnel_id);
1235 t = retrieve_tunnel_by_local_id(c, tid);
1238 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1242 /* Does client own tunnel? */
1243 if (t->client->handle != client) {
1245 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1249 /* Ok, delete peer from tunnel */
1250 peer_id = GNUNET_PEER_intern(&peer_msg->peer);
1252 /* FIXME Delete paths */
1253 /* FIXME Delete peer info */
1255 GNUNET_PEER_change_rc(peer_id, -1);
1257 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1263 * Handler for connection requests to new peers by type
1265 * @param cls closure
1266 * @param client identification of the client
1267 * @param message the actual message (ConnectPeerByType)
1270 handle_local_connect_by_type (void *cls,
1271 struct GNUNET_SERVER_Client *client,
1272 const struct GNUNET_MessageHeader *message)
1274 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
1275 MESH_TunnelNumber tid;
1276 GNUNET_MESH_ApplicationType application;
1277 struct MeshClient *c;
1278 struct MeshTunnel *t;
1280 /* Sanity check for client registration */
1281 if (NULL == (c = retrieve_client(client))) {
1283 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1287 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *)message;
1288 /* Sanity check for message size */
1289 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1290 ntohs(connect_msg->header.size))
1293 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1297 /* Tunnel exists? */
1298 tid = ntohl(connect_msg->tunnel_id);
1299 t = retrieve_tunnel_by_local_id(c, tid);
1302 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1306 /* Does client own tunnel? */
1307 if (t->client->handle != client) {
1309 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1313 /* Ok, lets find a peer offering the service */
1314 application = ntohl(connect_msg->type);
1315 application++; // FIXME silence warnings
1317 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1323 * Handler for client traffic directed to one peer
1325 * @param cls closure
1326 * @param client identification of the client
1327 * @param message the actual message
1330 handle_local_network_traffic (void *cls,
1331 struct GNUNET_SERVER_Client *client,
1332 const struct GNUNET_MessageHeader *message)
1334 struct MeshClient *c;
1335 struct MeshTunnel *t;
1336 struct GNUNET_MESH_Data *data_msg;
1337 MESH_TunnelNumber tid;
1339 /* Sanity check for client registration */
1340 if (NULL == (c = retrieve_client(client))) {
1342 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1345 data_msg = (struct GNUNET_MESH_Data *)message;
1346 /* Sanity check for message size */
1347 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1348 ntohs(data_msg->header.size))
1351 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1355 /* Tunnel exists? */
1356 tid = ntohl(data_msg->tunnel_id);
1357 t = retrieve_tunnel_by_local_id(c, tid);
1360 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1364 /* Does client own tunnel? */
1365 if (t->client->handle != client) {
1367 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1373 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1378 * Handler for client traffic directed to all peers in a tunnel
1380 * @param cls closure
1381 * @param client identification of the client
1382 * @param message the actual message
1385 handle_local_network_traffic_bcast (void *cls,
1386 struct GNUNET_SERVER_Client *client,
1387 const struct GNUNET_MessageHeader *message)
1389 struct MeshClient *c;
1390 struct MeshTunnel *t;
1391 struct GNUNET_MESH_DataBroadcast *data_msg;
1392 MESH_TunnelNumber tid;
1394 /* Sanity check for client registration */
1395 if (NULL == (c = retrieve_client(client))) {
1397 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1400 data_msg = (struct GNUNET_MESH_DataBroadcast *)message;
1401 /* Sanity check for message size */
1402 if (sizeof(struct GNUNET_MESH_PeerControl)
1403 != ntohs(data_msg->header.size))
1406 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1410 /* Tunnel exists? */
1411 tid = ntohl(data_msg->tunnel_id);
1412 t = retrieve_tunnel_by_local_id(c, tid);
1415 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1419 /* Does client own tunnel? */
1420 if (t->client->handle != client) {
1422 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1428 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1433 * Functions to handle messages from clients
1435 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1436 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
1437 {&handle_local_tunnel_create, NULL,
1438 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
1439 sizeof(struct GNUNET_MESH_TunnelMessage)},
1440 {&handle_local_tunnel_destroy, NULL,
1441 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
1442 sizeof(struct GNUNET_MESH_TunnelMessage)},
1443 {&handle_local_connect_add, NULL,
1444 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD,
1445 sizeof(struct GNUNET_MESH_PeerControl)},
1446 {&handle_local_connect_del, NULL,
1447 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL,
1448 sizeof(struct GNUNET_MESH_PeerControl)},
1449 {&handle_local_connect_by_type, NULL,
1450 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE,
1451 sizeof(struct GNUNET_MESH_ConnectPeerByType)},
1452 {&handle_local_network_traffic, NULL,
1453 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
1454 {&handle_local_network_traffic_bcast, NULL,
1455 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0},
1461 * To be called on core init/fail.
1463 * @param cls service closure
1464 * @param server handle to the server for this service
1465 * @param identity the public identity of this peer
1466 * @param publicKey the public key of this peer
1469 core_init (void *cls,
1470 struct GNUNET_CORE_Handle *server,
1471 const struct GNUNET_PeerIdentity *identity,
1472 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1476 core_handle = server;
1477 myid = GNUNET_PEER_intern(identity);
1482 * Method called whenever a given peer connects.
1484 * @param cls closure
1485 * @param peer peer identity this notification is about
1486 * @param atsi performance data for the connection
1489 core_connect (void *cls,
1490 const struct GNUNET_PeerIdentity *peer,
1491 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1493 // GNUNET_PEER_Id pid;
1494 struct MeshPeerInfo *peer_info;
1495 struct MeshPath *path;
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1498 "Peer connected\n");
1499 peer_info = get_peer_info(peer);
1500 if (myid == peer_info->id) {
1501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 path = GNUNET_malloc(sizeof(struct MeshPath));
1506 path->peers = GNUNET_malloc(sizeof(GNUNET_PEER_Id) * 2);
1507 path->peers[0] = myid;
1508 path->peers[1] = peer_info->id;
1509 add_path_to_peer(peer_info, path);
1514 * Method called whenever a peer disconnects.
1516 * @param cls closure
1517 * @param peer peer identity this notification is about
1520 core_disconnect (void *cls,
1522 GNUNET_PeerIdentity *peer)
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1526 "Peer disconnected\n");
1527 pid = GNUNET_PEER_intern(peer);
1528 GNUNET_PEER_change_rc(pid, -1);
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1537 /******************************************************************************/
1538 /************************ MAIN FUNCTIONS ****************************/
1539 /******************************************************************************/
1542 * Task run during shutdown.
1548 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 if (core_handle != NULL) {
1553 GNUNET_CORE_disconnect (core_handle);
1556 if (dht_handle != NULL) {
1557 GNUNET_DHT_disconnect (dht_handle);
1560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1565 * Process mesh requests.
1567 * @param cls closure
1568 * @param server the initialized server
1569 * @param c configuration to use
1573 struct GNUNET_SERVER_Handle *server,
1574 const struct GNUNET_CONFIGURATION_Handle *c)
1576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1577 "starting to run\n");
1578 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1579 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1580 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1582 NULL, /* Closure passed to MESH functions */
1583 &core_init, /* Call core_init once connected */
1584 &core_connect, /* Handle connects */
1585 &core_disconnect, /* remove peers on disconnects */
1586 NULL, /* Do we care about "status" updates? */
1587 NULL, /* Don't notify about all incoming messages */
1588 GNUNET_NO, /* For header only in notification */
1589 NULL, /* Don't notify about all outbound messages */
1590 GNUNET_NO, /* For header-only out notification */
1591 core_handlers); /* Register these handlers */
1592 if (core_handle == NULL) {
1595 dht_handle = GNUNET_DHT_connect(c, 64);
1596 if (dht_handle == NULL) {
1601 tunnels = GNUNET_CONTAINER_multihashmap_create(32);
1602 peers = GNUNET_CONTAINER_multihashmap_create(32);
1604 clients_tail = NULL;
1606 /* Scheduled the task to clean up when shutdown is called */
1607 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1608 &shutdown_task, NULL);
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1615 * The main function for the mesh service.
1617 * @param argc number of arguments from the command line
1618 * @param argv command line arguments
1619 * @return 0 ok, 1 on error
1622 main (int argc, char *const *argv)
1627 GNUNET_SERVICE_run (argc,
1630 GNUNET_SERVICE_OPTION_NONE,
1631 &run, NULL)) ? 0 : 1;
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,