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;
76 * Whether the path is serving traffic in a tunnel or is a backup
81 * List of all the peers that form the path from origin to target
83 GNUNET_PEER_Id *peers;
86 * Number of peers (hops) in the path
93 * All the states a peer participating in a tunnel can be in.
98 * Path to the peer not known yet
103 * Request sent, not yet answered.
108 * Peer connected and ready to accept data
113 * Peer connected previosly but not responding
115 MESH_PEER_RECONNECTING
120 * Struct containing all information regarding a given peer
130 * Is the peer reachable? Is the peer even connected?
132 enum MeshPeerState state;
135 * Last time we heard from this peer
137 struct GNUNET_TIME_Absolute last_contact;
140 * Number of attempts to reconnect so far
142 int n_reconnect_attempts;
145 * Paths to reach the peer
147 struct MeshPath *path;
150 * Handle to stop the DHT search for a path to this peer
152 struct GNUNET_DHT_GetHandle *dhtget;
157 * Data scheduled to transmit (to local client or remote peer)
164 struct MeshQueue *next;
165 struct MeshQueue *prev;
168 * Target of the data (NULL if target is client)
170 struct MeshPeerInfo *peer;
173 * Client to send the data to (NULL if target is peer)
175 struct MeshClient *client;
178 * Size of the message to transmit
183 * How old is the data?
185 struct GNUNET_TIME_Absolute timestamp;
190 struct GNUNET_MessageHeader *data;
194 * Globally unique tunnel identification (owner + number)
195 * DO NOT USE OVER THE NETWORK
197 struct MESH_TunnelID {
199 * Node that owns the tunnel
204 * Tunnel number to differentiate all the tunnels owned by the node oid
205 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_MARK )
207 MESH_TunnelNumber tid;
211 struct MeshClient; /* FWD declaration */
213 * Struct containing all information regarding a tunnel
214 * For an intermediate node the improtant info used will be:
215 * - id Tunnel unique identification
216 * - paths[0] To know where to send it next
217 * - metainfo: ready, speeds, accounting
224 struct MESH_TunnelID id;
227 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK or 0 )
229 MESH_TunnelNumber local_tid;
232 * Last time the tunnel was used
234 struct GNUNET_TIME_Absolute timestamp;
237 * Peers in the tunnelindexed by PeerIdentity (MeshPeerInfo)
239 struct GNUNET_CONTAINER_MultiHashMap* peers;
242 * Number of peers that are connected and potentially ready to receive data
244 unsigned int peers_ready;
247 * Number of peers that have been added to the tunnel
249 unsigned int peers_total;
253 * Client owner of the tunnel, if any
255 struct MeshClient *client;
258 * Messages ready to transmit
260 struct MeshQueue *queue_head;
261 struct MeshQueue *queue_tail;
266 * Struct containing information about a client of the service
273 struct MeshClient *next;
274 struct MeshClient *prev;
277 * Tunnels that belong to this client, indexed by local id
279 struct GNUNET_CONTAINER_MultiHashMap* tunnels;
282 * Handle to communicate with the client
284 struct GNUNET_SERVER_Client *handle;
287 * Applications that this client has claimed to provide
289 GNUNET_MESH_ApplicationType *apps;
290 unsigned int app_counter;
293 * Messages that this client has declared interest in
296 unsigned int type_counter;
300 /******************************************************************************/
301 /*********************** GLOBAL VARIABLES ****************************/
302 /******************************************************************************/
307 static struct MeshClient *clients;
308 static struct MeshClient *clients_t;
311 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
313 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
316 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
318 struct GNUNET_CONTAINER_MultiHashMap *peers;
321 * Handle to communicate with core
323 static struct GNUNET_CORE_Handle *core_handle;
328 static struct GNUNET_DHT_Handle *dht_handle;
331 * Local peer own ID (memory efficient handle)
333 static GNUNET_PEER_Id myid;
336 * Tunnel ID for the next created tunnel (global tunnel number)
338 static MESH_TunnelNumber next_tid;
340 /******************************************************************************/
341 /****************** GENERAL HELPER FUNCTIONS ************************/
342 /******************************************************************************/
345 * Check if client has registered with the service and has not disconnected
346 * @param client the client to check
347 * @return non-NULL if client exists in the global DLL
349 static struct MeshClient *
350 retrieve_client (struct GNUNET_SERVER_Client *client)
352 struct MeshClient *c;
356 if (c->handle == client) return c;
364 * Search for a tunnel among the tunnels for a client
365 * @param client the client whose tunnels to search in
366 * @param tid the local id of the tunnel
367 * @return tunnel handler, NULL if doesn't exist
369 static struct MeshTunnel *
370 retrieve_tunnel_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
372 GNUNET_HashCode hash;
374 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
375 return GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
379 * Search for a tunnel by global ID using PEER_ID
380 * @param pi owner of the tunnel
381 * @param tid global tunnel number
382 * @return tunnel handler, NULL if doesn't exist
384 static struct MeshTunnel *
385 retrieve_tunnel_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
387 struct MESH_TunnelID id;
388 GNUNET_HashCode hash;
393 GNUNET_CRYPTO_hash(&id, sizeof(struct MESH_TunnelID), &hash);
394 return GNUNET_CONTAINER_multihashmap_get(tunnels, &hash);
400 * Search for a tunnel by global ID using full PeerIdentities
401 * @param oid owner of the tunnel
402 * @param tid global tunnel number
403 * @return tunnel handler, NULL if doesn't exist
405 static struct MeshTunnel *
406 retrieve_tunnel (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
410 pi = GNUNET_PEER_intern(oid);
411 GNUNET_PEER_change_rc(pi, -1);
412 return retrieve_tunnel_by_pi(pi, tid);
417 * Destroy the path and free any allocated resources linked to it
418 * @param t tunnel the path belongs to
419 * @param p the path to destroy
420 * @return GNUNET_OK on success
423 destroy_path(struct MeshTunnel *t, struct MeshPath *p)
425 GNUNET_PEER_decrement_rcs(p->peers, p->length);
426 GNUNET_free(p->peers);
433 * Destroy the peer_info and free any allocated resources linked to it
434 * @param t tunnel the path belongs to
435 * @param pi the peer_info to destroy
436 * @return GNUNET_OK on success
439 // destroy_peer_info(struct MeshTunnel *t, struct MeshPeerInfo *pi)
441 // GNUNET_PEER_change_rc(pi->id, -1);
442 // /* FIXME delete from list */
450 * Destroy the tunnel and free any allocated resources linked to it
451 * @param c client the tunnel belongs to
452 * @param t the tunnel to destroy
453 * @return GNUNET_OK on success
456 destroy_tunnel(struct MeshClient *c, struct MeshTunnel *t)
458 // struct MeshPath *path;
459 GNUNET_HashCode hash;
462 if (NULL == t) return GNUNET_OK;
465 // for (path = t->paths_head; path != NULL; path = t->paths_head) {
466 // if(GNUNET_OK != destroy_path(t, path)) r = GNUNET_SYSERR;
469 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
470 if(GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t)) {
474 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
476 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t))
484 /******************************************************************************/
485 /**************** MESH NETWORK HANDLER HELPERS ***********************/
486 /******************************************************************************/
489 * Function called to notify a client about the socket
490 * being ready to queue more data. "buf" will be
491 * NULL and "size" zero if the socket was closed for
492 * writing in the meantime.
495 * @param size number of bytes available in buf
496 * @param buf where the callee should write the message
497 * @return number of bytes written to buf
500 send_core_create_path_for_peer (void *cls, size_t size, void *buf)
502 struct MeshPeerInfo *peer_info = cls;
503 struct GNUNET_MESH_ManipulatePath *msg;
505 struct GNUNET_PeerIdentity *peer_ptr;
506 struct GNUNET_PeerIdentity id;
510 if (0 == size && NULL == buf) {
511 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Retransmitting create path\n");
512 GNUNET_PEER_resolve(peer_info->path->peers[1], &id);
513 GNUNET_CORE_notify_transmit_ready(core_handle,
516 GNUNET_TIME_UNIT_SECONDS,
518 sizeof(struct GNUNET_MESH_ManipulatePath)
519 + (peer_info->path->length
520 * sizeof (struct GNUNET_PeerIdentity)),
521 &send_core_create_path_for_peer,
532 if (p == NULL) return 0; // TODO Notify ERROR Path not found
534 size_needed = sizeof(struct GNUNET_MESH_ManipulatePath)
535 + p->length * sizeof(struct GNUNET_PeerIdentity);
536 if (size < size_needed) {
537 // TODO retry? cancel?
541 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
542 msg->header.size = htons(size_needed);
543 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
545 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
546 for (i = 0; i < p->length; i++) {
547 GNUNET_PEER_resolve(p->peers[i], peer_ptr++);
550 peer_info->state = MESH_PEER_WAITING; // TODO maybe already ready?
557 * Send another peer a notification to destroy a tunnel
558 * @param cls The tunnel to destroy
559 * @param size Size in the buffer
560 * @param buf Memory where to put the data to transmit
561 * @return Size of data put in buffer
564 send_p2p_tunnel_destroy(void *cls, size_t size, void *buf)
566 struct MeshTunnel *t = cls;
567 struct MeshClient *c;
568 struct GNUNET_MESH_TunnelMessage *msg;
572 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY); /*FIXME*/
573 msg->header.size = htons(sizeof(struct GNUNET_MESH_TunnelMessage));
574 msg->tunnel_id = htonl(t->id.tid);
576 destroy_tunnel(c, t);
577 return sizeof(struct GNUNET_MESH_TunnelMessage);
582 /******************************************************************************/
583 /******************** MESH NETWORK HANDLERS **************************/
584 /******************************************************************************/
588 * Core handler for path creation
589 * struct GNUNET_CORE_MessageHandler
592 * @param message message
593 * @param peer peer identity this notification is about
594 * @param atsi performance data
595 * @return GNUNET_OK to keep the connection open,
596 * GNUNET_SYSERR to close it (signal serious error)
600 handle_mesh_path_create (void *cls,
601 const struct GNUNET_PeerIdentity *peer,
602 const struct GNUNET_MessageHeader *message,
603 const struct GNUNET_TRANSPORT_ATS_Information
607 /* Find origin & self */
608 /* Search for origin in local tunnels */
609 /* Create tunnel / add path */
610 /* Retransmit to next link in chain, if any (core_notify + callback) */
616 * Core handler for mesh network traffic
619 * @param message message
620 * @param peer peer identity this notification is about
621 * @param atsi performance data
622 * @return GNUNET_OK to keep the connection open,
623 * GNUNET_SYSERR to close it (signal serious error)
626 handle_mesh_network_traffic (void *cls,
627 const struct GNUNET_PeerIdentity *peer,
628 const struct GNUNET_MessageHeader *message,
629 const struct GNUNET_TRANSPORT_ATS_Information
632 if (GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
633 /* Retransmit to next in path of tunnel identified by message */
635 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
636 /* Retransmit to previous in path of tunnel identified by message */
643 * Functions to handle messages from core
645 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
646 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
647 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
648 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
654 /******************************************************************************/
655 /**************** MESH LOCAL HANDLER HELPERS ***********************/
656 /******************************************************************************/
659 * delete_tunnel_entry: iterator for deleting each tunnel that belongs to a
660 * client when the client disconnects.
661 * @param cls closure (client that is disconnecting)
662 * @param key the hash of the local tunnel id (used to access the hashmap)
663 * @param value the value stored at the key (tunnel to destroy)
664 * @return GNUNET_OK on success
667 delete_tunnel_entry (void *cls, const GNUNET_HashCode * key, void *value) {
669 r = destroy_tunnel((struct MeshClient *) cls, (struct MeshTunnel *) value);
675 * notify_client_connection_failure: notify a client that the connection to the
676 * requested remote peer is not possible (for instance, no route found)
677 * Function called when the socket is ready to queue more data. "buf" will be
678 * NULL and "size" zero if the socket was closed for writing in the meantime.
681 * @param size number of bytes available in buf
682 * @param buf where the callee should write the message
683 * @return number of bytes written to buf
686 notify_client_connection_failure (void *cls, size_t size, void *buf)
689 struct MeshPeerInfo *peer_info;
690 struct GNUNET_MESH_PeerControl *msg;
691 struct GNUNET_PeerIdentity id;
693 if (0 == size && NULL == buf) {
694 // TODO retry? cancel?
698 size_needed = sizeof(struct GNUNET_MESH_PeerControl);
699 peer_info = (struct MeshPeerInfo *) cls;
700 msg = (struct GNUNET_MESH_PeerControl *) buf;
701 msg->header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
702 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
703 // msg->tunnel_id = htonl(peer_info->t->tid);
704 GNUNET_PEER_resolve(peer_info->id, &id);
705 memcpy(&msg->peer, &id, sizeof(struct GNUNET_PeerIdentity));
712 * Function to process paths received for a new peer addition. The recorded
713 * paths form the initial tunnel, which can be optimized later.
714 * Called on each result obtained for the DHT search.
717 * @param exp when will this value expire
718 * @param key key of the result
719 * @param get_path NULL-terminated array of pointers
720 * to the peers on reverse GET path (or NULL if not recorded)
721 * @param put_path NULL-terminated array of pointers
722 * to the peers on the PUT path (or NULL if not recorded)
723 * @param type type of the result
724 * @param size number of bytes in data
725 * @param data pointer to the result data
728 dht_get_response_handler(void *cls,
729 struct GNUNET_TIME_Absolute exp,
730 const GNUNET_HashCode * key,
731 const struct GNUNET_PeerIdentity * const *get_path,
732 const struct GNUNET_PeerIdentity * const *put_path,
733 enum GNUNET_BLOCK_Type type,
737 struct MeshPeerInfo *peer_info = cls;
739 struct MeshPath *aux;
740 struct GNUNET_PeerIdentity pi;
743 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path) {
744 // Find ourselves some alternate initial path to the destination: retry
745 GNUNET_DHT_get_stop(peer_info->dhtget);
746 GNUNET_PEER_resolve(peer_info->id, &pi);
747 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
748 GNUNET_TIME_UNIT_FOREVER_REL,
749 GNUNET_BLOCK_TYPE_ANY,
751 4, /* replication level */
752 GNUNET_DHT_RO_RECORD_ROUTE,
753 NULL, /* bloom filter */
757 dht_get_response_handler,
761 p = GNUNET_malloc(sizeof(struct MeshPath));
762 for (i = 0; get_path[i] != NULL; i++);
763 for (i--; i >= 0; i--) {
764 p->peers = GNUNET_realloc(p->peers,
765 sizeof(GNUNET_PEER_Id) * (p->length + 1));
766 p->peers[p->length] = GNUNET_PEER_intern(get_path[i]);
769 for (i = 0; put_path[i] != NULL; i++);
770 for (i--; i >= 0; i--) {
771 p->peers = GNUNET_realloc(p->peers,
772 sizeof(GNUNET_PEER_Id) * (p->length + 1));
773 p->peers[p->length] = GNUNET_PEER_intern(put_path[i]);
776 if (NULL == peer_info->path) {
781 aux = peer_info->path;
782 while (NULL != aux->next) aux = aux->next;
785 GNUNET_CORE_notify_transmit_ready(core_handle,
788 GNUNET_TIME_UNIT_FOREVER_REL,
790 sizeof(struct GNUNET_MESH_ManipulatePath)
792 * sizeof (struct GNUNET_PeerIdentity)),
793 &send_core_create_path_for_peer,
799 /******************************************************************************/
800 /********************* MESH LOCAL HANDLES **************************/
801 /******************************************************************************/
805 * Handler for client disconnection
808 * @param client identification of the client; NULL
809 * for the last call when the server is destroyed
812 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
814 struct MeshClient *c;
815 struct MeshClient *next;
817 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
818 "client disconnected\n");
821 if (c->handle == client) {
822 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
823 " matching client found, cleaning\n");
824 GNUNET_CONTAINER_multihashmap_iterate(c->tunnels,
825 &delete_tunnel_entry,
827 GNUNET_CONTAINER_multihashmap_destroy(c->tunnels);
828 if(0 != c->app_counter) GNUNET_free (c->apps);
829 if(0 != c->type_counter) GNUNET_free (c->types);
830 GNUNET_CONTAINER_DLL_remove(clients, clients_t, c);
835 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
840 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
847 * Handler for new clients
850 * @param client identification of the client
851 * @param message the actual message, which includes messages the client wants
854 handle_local_new_client (void *cls,
855 struct GNUNET_SERVER_Client *client,
856 const struct GNUNET_MessageHeader *message)
858 struct GNUNET_MESH_ClientConnect *cc_msg;
859 struct MeshClient *c;
864 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
865 /* Check data sanity */
866 size = ntohs(message->size) - sizeof(struct GNUNET_MESH_ClientConnect);
867 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
868 types = ntohs(cc_msg->types);
869 apps = ntohs(cc_msg->applications);
871 types * sizeof(uint16_t) + apps * sizeof(GNUNET_MESH_ApplicationType))
874 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
878 /* Create new client structure */
879 c = GNUNET_malloc(sizeof(struct MeshClient));
882 c->type_counter = types;
883 c->types = GNUNET_malloc(types * sizeof(uint16_t));
884 memcpy(c->types, &message[1], types * sizeof(uint16_t));
887 c->app_counter = apps;
888 c->apps = GNUNET_malloc(apps * sizeof(GNUNET_MESH_ApplicationType));
890 &message[1] + types * sizeof(uint16_t),
891 apps * sizeof(GNUNET_MESH_ApplicationType));
893 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
894 " client has %u+%u subscriptions\n",
898 GNUNET_CONTAINER_DLL_insert(clients, clients_t, c);
899 c->tunnels = GNUNET_CONTAINER_multihashmap_create(32);
901 GNUNET_SERVER_receive_done(client, GNUNET_OK);
907 * Handler for requests of new tunnels
910 * @param client identification of the client
911 * @param message the actual message
914 handle_local_tunnel_create (void *cls,
915 struct GNUNET_SERVER_Client *client,
916 const struct GNUNET_MessageHeader *message)
918 struct GNUNET_MESH_TunnelMessage *t_msg;
919 struct MeshTunnel *t;
920 struct MeshClient *c;
921 GNUNET_HashCode hash;
923 /* Sanity check for client registration */
924 if (NULL == (c = retrieve_client(client))) {
926 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
930 /* Message sanity check */
931 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
933 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
937 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
938 /* Sanity check for tunnel numbering */
939 if (0 == (ntohl(t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)) {
941 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
944 /* Sanity check for duplicate tunnel IDs */
945 if(NULL != retrieve_tunnel_by_local_id(c, ntohl(t_msg->tunnel_id))) {
947 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
951 t = GNUNET_malloc(sizeof(struct MeshTunnel));
952 while (NULL != retrieve_tunnel_by_pi(myid, next_tid))
953 next_tid = (next_tid + 1) % GNUNET_MESH_LOCAL_TUNNEL_ID_MARK;
954 t->id.tid = next_tid++;
956 t->local_tid = ntohl(t_msg->tunnel_id);
958 t->peers = GNUNET_CONTAINER_multihashmap_create(32);
960 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
962 GNUNET_CONTAINER_multihashmap_put(c->tunnels, &hash, t,
963 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
966 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
970 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
972 GNUNET_CONTAINER_multihashmap_put(tunnels, &hash, t,
973 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
976 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
980 GNUNET_SERVER_receive_done(client, GNUNET_OK);
986 * Handler for requests of deleting tunnels
989 * @param client identification of the client
990 * @param message the actual message
993 handle_local_tunnel_destroy (void *cls,
994 struct GNUNET_SERVER_Client *client,
995 const struct GNUNET_MessageHeader *message)
997 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
998 struct MeshClient *c;
999 struct MeshTunnel *t;
1000 MESH_TunnelNumber tid;
1001 GNUNET_HashCode hash;
1004 /* Sanity check for client registration */
1005 if (NULL == (c = retrieve_client(client))) {
1007 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1010 /* Message sanity check */
1011 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
1013 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1017 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
1019 /* Retrieve tunnel */
1020 tid = ntohl(tunnel_msg->tunnel_id);
1022 /* Remove from local id hashmap */
1023 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
1024 t = GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
1025 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t);
1027 /* Remove from global id hashmap */
1028 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
1029 GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t);
1031 // notify_tunnel_destroy(t);
1032 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1038 * Handler for connection requests to new peers
1040 * @param cls closure
1041 * @param client identification of the client
1042 * @param message the actual message (PeerControl)
1045 handle_local_connect_add (void *cls,
1046 struct GNUNET_SERVER_Client *client,
1047 const struct GNUNET_MessageHeader *message)
1049 struct GNUNET_MESH_PeerControl *peer_msg;
1050 struct MeshClient *c;
1051 struct MeshTunnel *t;
1052 MESH_TunnelNumber tid;
1053 struct MeshPeerInfo *peer_info;
1056 /* Sanity check for client registration */
1057 if (NULL == (c = retrieve_client(client))) {
1059 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1063 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1064 /* Sanity check for message size */
1065 if (sizeof(struct GNUNET_MESH_PeerControl)
1066 != ntohs(peer_msg->header.size))
1069 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1073 /* Tunnel exists? */
1074 tid = ntohl(peer_msg->tunnel_id);
1075 t = retrieve_tunnel_by_local_id(c, tid);
1078 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1082 /* Does client own tunnel? */
1083 if (t->client->handle != client) {
1085 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1089 /* Ok, add peer to tunnel */
1090 peer_info = GNUNET_CONTAINER_multihashmap_get(peers,
1091 &peer_msg->peer.hashPubKey);
1092 if (NULL == peer_info) {
1093 peer_info = (struct MeshPeerInfo *)
1094 GNUNET_malloc(sizeof(struct MeshPeerInfo));
1095 GNUNET_CONTAINER_multihashmap_put(peers,
1096 &peer_msg->peer.hashPubKey,
1098 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1099 peer_info->id = GNUNET_PEER_intern(&peer_msg->peer);
1100 peer_info->state = MESH_PEER_SEARCHING;
1105 /* Start DHT search if needed */
1106 if(MESH_PEER_READY != peer_info->state && NULL == peer_info->dhtget) {
1107 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
1108 GNUNET_TIME_UNIT_FOREVER_REL,
1109 GNUNET_BLOCK_TYPE_ANY,
1110 &peer_msg->peer.hashPubKey,
1111 4, /* replication level */
1112 GNUNET_DHT_RO_RECORD_ROUTE,
1113 NULL, /* bloom filter */
1116 0, /* xquery bits */
1117 dht_get_response_handler,
1121 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1127 * Handler for disconnection requests of peers in a tunnel
1129 * @param cls closure
1130 * @param client identification of the client
1131 * @param message the actual message (PeerControl)
1134 handle_local_connect_del (void *cls,
1135 struct GNUNET_SERVER_Client *client,
1136 const struct GNUNET_MessageHeader *message)
1138 struct GNUNET_MESH_PeerControl *peer_msg;
1139 struct MeshClient *c;
1140 struct MeshTunnel *t;
1141 MESH_TunnelNumber tid;
1142 GNUNET_PEER_Id peer_id;
1144 /* Sanity check for client registration */
1145 if (NULL == (c = retrieve_client(client))) {
1147 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1150 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1151 /* Sanity check for message size */
1152 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
1154 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1158 /* Tunnel exists? */
1159 tid = ntohl(peer_msg->tunnel_id);
1160 t = retrieve_tunnel_by_local_id(c, tid);
1163 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1167 /* Does client own tunnel? */
1168 if (t->client->handle != client) {
1170 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1174 /* Ok, delete peer from tunnel */
1175 peer_id = GNUNET_PEER_intern(&peer_msg->peer);
1177 /* FIXME Delete paths */
1178 /* FIXME Delete peer info */
1180 GNUNET_PEER_change_rc(peer_id, -1);
1182 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1188 * Handler for connection requests to new peers by type
1190 * @param cls closure
1191 * @param client identification of the client
1192 * @param message the actual message (ConnectPeerByType)
1195 handle_local_connect_by_type (void *cls,
1196 struct GNUNET_SERVER_Client *client,
1197 const struct GNUNET_MessageHeader *message)
1199 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
1200 MESH_TunnelNumber tid;
1201 GNUNET_MESH_ApplicationType application;
1202 struct MeshClient *c;
1203 struct MeshTunnel *t;
1205 /* Sanity check for client registration */
1206 if (NULL == (c = retrieve_client(client))) {
1208 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1212 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *)message;
1213 /* Sanity check for message size */
1214 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1215 ntohs(connect_msg->header.size))
1218 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1222 /* Tunnel exists? */
1223 tid = ntohl(connect_msg->tunnel_id);
1224 t = retrieve_tunnel_by_local_id(c, tid);
1227 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1231 /* Does client own tunnel? */
1232 if (t->client->handle != client) {
1234 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1238 /* Ok, lets find a peer offering the service */
1239 application = ntohl(connect_msg->type);
1240 application++; // FIXME silence warnings
1242 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1248 * Handler for client traffic directed to one peer
1250 * @param cls closure
1251 * @param client identification of the client
1252 * @param message the actual message
1255 handle_local_network_traffic (void *cls,
1256 struct GNUNET_SERVER_Client *client,
1257 const struct GNUNET_MessageHeader *message)
1259 struct MeshClient *c;
1260 struct MeshTunnel *t;
1261 struct GNUNET_MESH_Data *data_msg;
1262 MESH_TunnelNumber tid;
1264 /* Sanity check for client registration */
1265 if (NULL == (c = retrieve_client(client))) {
1267 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1270 data_msg = (struct GNUNET_MESH_Data *)message;
1271 /* Sanity check for message size */
1272 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1273 ntohs(data_msg->header.size))
1276 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1280 /* Tunnel exists? */
1281 tid = ntohl(data_msg->tunnel_id);
1282 t = retrieve_tunnel_by_local_id(c, tid);
1285 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1289 /* Does client own tunnel? */
1290 if (t->client->handle != client) {
1292 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1298 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1303 * Handler for client traffic directed to all peers in a tunnel
1305 * @param cls closure
1306 * @param client identification of the client
1307 * @param message the actual message
1310 handle_local_network_traffic_bcast (void *cls,
1311 struct GNUNET_SERVER_Client *client,
1312 const struct GNUNET_MessageHeader *message)
1314 struct MeshClient *c;
1315 struct MeshTunnel *t;
1316 struct GNUNET_MESH_DataBroadcast *data_msg;
1317 MESH_TunnelNumber tid;
1319 /* Sanity check for client registration */
1320 if (NULL == (c = retrieve_client(client))) {
1322 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1325 data_msg = (struct GNUNET_MESH_DataBroadcast *)message;
1326 /* Sanity check for message size */
1327 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(data_msg->header.size)) {
1329 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1333 /* Tunnel exists? */
1334 tid = ntohl(data_msg->tunnel_id);
1335 t = retrieve_tunnel_by_local_id(c, tid);
1338 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1342 /* Does client own tunnel? */
1343 if (t->client->handle != client) {
1345 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1351 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1356 * Functions to handle messages from clients
1358 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1359 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
1360 {&handle_local_tunnel_create, NULL,
1361 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
1362 sizeof(struct GNUNET_MESH_TunnelMessage)},
1363 {&handle_local_tunnel_destroy, NULL,
1364 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
1365 sizeof(struct GNUNET_MESH_TunnelMessage)},
1366 {&handle_local_connect_add, NULL,
1367 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD,
1368 sizeof(struct GNUNET_MESH_PeerControl)},
1369 {&handle_local_connect_del, NULL,
1370 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL,
1371 sizeof(struct GNUNET_MESH_PeerControl)},
1372 {&handle_local_connect_by_type, NULL,
1373 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE,
1374 sizeof(struct GNUNET_MESH_ConnectPeerByType)},
1375 {&handle_local_network_traffic, NULL,
1376 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
1377 {&handle_local_network_traffic_bcast, NULL,
1378 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0},
1384 * To be called on core init/fail.
1386 * @param cls service closure
1387 * @param server handle to the server for this service
1388 * @param identity the public identity of this peer
1389 * @param publicKey the public key of this peer
1392 core_init (void *cls,
1393 struct GNUNET_CORE_Handle *server,
1394 const struct GNUNET_PeerIdentity *identity,
1395 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1399 core_handle = server;
1400 myid = GNUNET_PEER_intern(identity);
1405 * Method called whenever a given peer connects.
1407 * @param cls closure
1408 * @param peer peer identity this notification is about
1409 * @param atsi performance data for the connection
1412 core_connect (void *cls,
1413 const struct GNUNET_PeerIdentity *peer,
1414 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1418 "Peer connected\n");
1419 pid = GNUNET_PEER_intern(peer);
1421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1428 * Method called whenever a peer disconnects.
1430 * @param cls closure
1431 * @param peer peer identity this notification is about
1434 core_disconnect (void *cls,
1436 GNUNET_PeerIdentity *peer)
1439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1440 "Peer disconnected\n");
1441 pid = GNUNET_PEER_intern(peer);
1442 GNUNET_PEER_change_rc(pid, -1);
1444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1451 /******************************************************************************/
1452 /********************** PERIODIC FUNCTIONS **************************/
1453 /******************************************************************************/
1456 * Iterator over peers to send keepalive packets when needed.
1459 * @param key current key code
1460 * @param value value in the hash map
1461 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
1464 path_refresh_peer (void *cls, const GNUNET_HashCode * key, void *value)
1466 struct MeshPeerInfo *pi = cls;
1467 struct GNUNET_TIME_Absolute threshold;
1468 struct GNUNET_PeerIdentity id;
1470 threshold = GNUNET_TIME_absolute_subtract(GNUNET_TIME_absolute_get(),
1473 if (pi->last_contact.abs_value < threshold.abs_value) {
1474 GNUNET_PEER_resolve(pi->path->peers[1], &id);
1475 GNUNET_CORE_notify_transmit_ready(core_handle,
1478 GNUNET_TIME_UNIT_FOREVER_REL,
1480 sizeof(struct GNUNET_MESH_ManipulatePath)
1482 * sizeof (struct GNUNET_PeerIdentity)),
1483 &send_core_create_path_for_peer,
1491 * Send keepalive packets for all routes
1497 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1499 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) return;
1500 GNUNET_CONTAINER_multihashmap_iterate(peers, path_refresh_peer, NULL);
1501 GNUNET_SCHEDULER_add_delayed(REFRESH_PATH_TIME,
1508 /******************************************************************************/
1509 /************************ MAIN FUNCTIONS ****************************/
1510 /******************************************************************************/
1513 * Task run during shutdown.
1519 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1523 if (core_handle != NULL) {
1524 GNUNET_CORE_disconnect (core_handle);
1527 if (dht_handle != NULL) {
1528 GNUNET_DHT_disconnect (dht_handle);
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1536 * Process mesh requests.
1538 * @param cls closure
1539 * @param server the initialized server
1540 * @param c configuration to use
1544 struct GNUNET_SERVER_Handle *server,
1545 const struct GNUNET_CONFIGURATION_Handle *c)
1547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1548 "starting to run\n");
1549 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1550 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1551 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1553 NULL, /* Closure passed to MESH functions */
1554 &core_init, /* Call core_init once connected */
1555 &core_connect, /* Handle connects */
1556 &core_disconnect, /* remove peers on disconnects */
1557 NULL, /* Do we care about "status" updates? */
1558 NULL, /* Don't notify about all incoming messages */
1559 GNUNET_NO, /* For header only in notification */
1560 NULL, /* Don't notify about all outbound messages */
1561 GNUNET_NO, /* For header-only out notification */
1562 core_handlers); /* Register these handlers */
1563 if (core_handle == NULL) {
1566 dht_handle = GNUNET_DHT_connect(c, 64);
1567 if (dht_handle == NULL) {
1572 tunnels = GNUNET_CONTAINER_multihashmap_create(32);
1573 peers = GNUNET_CONTAINER_multihashmap_create(32);
1577 /* Path keepalive */
1578 GNUNET_SCHEDULER_add_delayed(REFRESH_PATH_TIME,
1582 /* Scheduled the task to clean up when shutdown is called */
1583 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1584 &shutdown_task, NULL);
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 * The main function for the mesh service.
1593 * @param argc number of arguments from the command line
1594 * @param argv command line arguments
1595 * @return 0 ok, 1 on error
1598 main (int argc, char *const *argv)
1603 GNUNET_SERVICE_run (argc,
1606 GNUNET_SERVICE_OPTION_NONE,
1607 &run, NULL)) ? 0 : 1;
1608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,