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 HANDLES
30 * - MESH LOCAL HANDLES
31 * - MAIN FUNCTIONS (main & run)
34 * - soft stateing (keep-alive (CHANGE?) / timeout / disconnect) -- not a message issue
35 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
36 * - partial disconnect reporting -- same as error reporting?
37 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
38 * - speed requirement specification (change?) in mesh API -- API call
42 #include "gnunet_common.h"
43 #include "gnunet_util_lib.h"
44 #include "gnunet_peer_lib.h"
45 #include "gnunet_core_service.h"
46 #include "gnunet_protocols.h"
49 #include "mesh_protocol.h"
50 #include "gnunet_dht_service.h"
53 /******************************************************************************/
54 /************************ DATA STRUCTURES ****************************/
55 /******************************************************************************/
58 * All the states a peer participating in a tunnel can be in.
63 * Path to the peer not known yet
68 * Request sent, not yet answered.
73 * Peer connected and ready to accept data
78 * Peer connected previosly but not responding
80 MESH_PEER_RECONNECTING
84 * Struct containing all information regarding a given peer
94 * Is the peer reachable? Is the peer even connected?
96 enum MeshPeerState state;
99 * When to try to establish contact again?
101 struct GNUNET_TIME_Absolute next_reconnect_attempt;
104 * Number of attempts to reconnect so far
106 int n_reconnect_attempts;
109 * First hop whom to send data to reach this peer in the current active path
111 GNUNET_PEER_Id first_hop;
114 * Handle to stop the DHT search for a path to this peer
116 struct GNUNET_DHT_GetHandle *dhtget;
120 typedef uint32_t MESH_PathID;
124 * Information regarding a path
131 struct MeshPath *next;
132 struct MeshPath *prev;
135 * Id of the path, in case it's needed
140 * Whether the path is serving traffic in a tunnel or is a backup
145 * List of all the peers that form the path from origin to target
147 GNUNET_PEER_Id *peers;
150 * Number of peers (hops) in the path
156 * Data scheduled to transmit (to local client or remote peer)
163 struct MeshQueue *next;
164 struct MeshQueue *prev;
167 * Target of the data (NULL if target is client)
169 struct MeshPeerInfo *peer;
172 * Client to send the data to (NULL if target is peer)
174 struct MeshClient *client;
177 * Size of the message to transmit
182 * How old is the data?
184 struct GNUNET_TIME_Absolute timestamp;
189 struct GNUNET_MessageHeader *data;
193 * Globally unique tunnel identification (owner + number)
194 * DO NOT USE OVER THE NETWORK
196 struct MESH_TunnelID {
198 * Node that owns the tunnel
203 * Tunnel number to differentiate all the tunnels owned by the node oid
204 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_MARK )
206 MESH_TunnelNumber tid;
210 struct MeshClient; /* FWD declaration */
212 * Struct containing all information regarding a tunnel
213 * For an intermediate node the improtant info used will be:
214 * - id Tunnel unique identification
215 * - paths[0] To know where to send it next
216 * - metainfo: ready, speeds, accounting
223 struct MESH_TunnelID id;
226 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK or 0 )
228 MESH_TunnelNumber local_tid;
231 * Last time the tunnel was used
233 struct GNUNET_TIME_Absolute timestamp;
236 * Peers in the tunnel, for future optimizations
238 struct GNUNET_CONTAINER_MultiHashMap* peers;
241 * Number of peers that are connected and potentially ready to receive data
243 unsigned int peers_ready;
246 * Number of peers that have been added to the tunnel
248 unsigned int peers_total;
251 * Paths (used and backup)
253 struct MeshPath *paths_head;
254 struct MeshPath *paths_tail;
257 * Client owner of the tunnel, if any
259 struct MeshClient *client;
262 * Messages ready to transmit
264 struct MeshQueue *queue_head;
265 struct MeshQueue *queue_tail;
270 * Struct containing information about a client of the service
277 struct MeshClient *next;
280 * Tunnels that belong to this client, indexed by local id
282 struct GNUNET_CONTAINER_MultiHashMap* tunnels;
285 * Handle to communicate with the client
287 struct GNUNET_SERVER_Client *handle;
290 * Applications that this client has claimed to provide
292 GNUNET_MESH_ApplicationType *apps;
293 unsigned int app_counter;
296 * Messages that this client has declared interest in
299 unsigned int type_counter;
303 /******************************************************************************/
304 /*********************** GLOBAL VARIABLES ****************************/
305 /******************************************************************************/
310 static struct MeshClient *clients;
313 * Tunnels known, indexed by MESH_TunnelID
315 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
318 * Handle to communicate with core
320 static struct GNUNET_CORE_Handle *core_handle;
325 static struct GNUNET_DHT_Handle *dht_handle;
328 * Local peer own ID (memory efficient handle)
330 static GNUNET_PEER_Id myid;
333 * Tunnel ID for the next created tunnel
335 static MESH_TunnelNumber next_tid;
337 /******************************************************************************/
338 /****************** GENERAL HELPER FUNCTIONS ************************/
339 /******************************************************************************/
342 * Check if client has registered with the service and has not disconnected
343 * @param client the client to check
344 * @return non-NULL if client exists in the global DLL
346 static struct MeshClient *
347 retrieve_client (struct GNUNET_SERVER_Client *client)
349 struct MeshClient *c;
353 if (c->handle == client) return c;
360 * Search for a tunnel among the tunnels for a client
361 * @param client the client whose tunnels to search in
362 * @param tid the local id of the tunnel
363 * @return tunnel handler, NULL if doesn't exist
365 static struct MeshTunnel *
366 retrieve_tunnel_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
368 GNUNET_HashCode hash;
370 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
371 return GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
375 * Search for a tunnel by global ID
377 static struct MeshTunnel *
378 retrieve_tunnel (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
380 struct MESH_TunnelID id;
381 GNUNET_HashCode hash;
383 id.oid = GNUNET_PEER_intern(oid);
384 GNUNET_PEER_change_rc(id.oid, -1);
387 GNUNET_CRYPTO_hash(&id, sizeof(struct MESH_TunnelID), &hash);
388 return GNUNET_CONTAINER_multihashmap_get(tunnels, &hash);
392 * Destroy the path and free any allocated resources linked to it
393 * @param t tunnel the path belongs to
394 * @param p the path to destroy
395 * @return GNUNET_OK on success
398 destroy_path(struct MeshTunnel *t, struct MeshPath *p)
400 GNUNET_PEER_decrement_rcs(p->peers, p->length);
401 GNUNET_free(p->peers);
402 GNUNET_CONTAINER_DLL_remove(t->paths_head, t->paths_tail, p);
408 * Destroy the peer_info and free any allocated resources linked to it
409 * @param t tunnel the path belongs to
410 * @param pi the peer_info to destroy
411 * @return GNUNET_OK on success
414 destroy_peer_info(struct MeshTunnel *t, struct MeshPeerInfo *pi)
416 GNUNET_PEER_change_rc(pi->id, -1);
417 /* FIXME delete from list */
423 * Destroy the tunnel and free any allocated resources linked to it
424 * @param c client the tunnel belongs to
425 * @param t the tunnel to destroy
426 * @return GNUNET_OK on success
429 destroy_tunnel(struct MeshClient *c, struct MeshTunnel *t)
431 struct MeshPath *path;
432 GNUNET_HashCode hash;
435 if (NULL == t) return GNUNET_OK;
437 for (path = t->paths_head; path != NULL; path = t->paths_head) {
438 if(GNUNET_OK != destroy_path(t, path)) r = GNUNET_SYSERR;
441 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
442 if(GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t)) {
446 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
448 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t))
456 /******************************************************************************/
457 /******************** MESH NETWORK HANDLERS **************************/
458 /******************************************************************************/
462 * Function called to notify a client about the socket
463 * being ready to queue more data. "buf" will be
464 * NULL and "size" zero if the socket was closed for
465 * writing in the meantime.
468 * @param size number of bytes available in buf
469 * @param buf where the callee should write the message
470 * @return number of bytes written to buf
473 send_core_create_path_for_peer (void *cls, size_t size, void *buf)
476 struct MeshPeerInfo *peer_info;
477 struct GNUNET_MESH_ManipulatePath *msg;
479 struct GNUNET_PeerIdentity peer_id;
480 struct GNUNET_PeerIdentity *peer_ptr;
483 if (0 == size && NULL == buf) {
484 // TODO retry? cancel?
487 peer_info = (struct MeshPeerInfo *)cls;
488 peer_info->dhtget = NULL;
489 // p = peer_info->t->paths_head;
491 if (p->peers[p->length-1] == peer_info->id) {
496 if (p == NULL) return 0; // TODO Notify ERROR Path not found
498 size_needed = sizeof(struct GNUNET_MESH_ManipulatePath)
499 + p->length * sizeof(struct GNUNET_PeerIdentity);
500 if (size < size_needed) {
501 // TODO retry? cancel?
505 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
506 msg->header.size = htons(sizeof(struct GNUNET_MESH_ManipulatePath));
507 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
510 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
511 for (i = 0; i < p->length; i++) {
512 GNUNET_PEER_resolve(p->peers[i], &peer_id);
513 memcpy(&peer_ptr[i], &peer_id, sizeof(struct GNUNET_PeerIdentity));
516 peer_info->state = MESH_PEER_WAITING;
523 * Send another peer a notification to destroy a tunnel
524 * @param cls The tunnel to destroy
525 * @param size Size in the buffer
526 * @param buf Memory where to put the data to transmit
527 * @return Size of data put in buffer
530 send_p2p_tunnel_destroy(void *cls, size_t size, void *buf)
532 struct MeshTunnel *t = cls;
533 struct MeshClient *c;
534 struct GNUNET_MESH_TunnelMessage *msg;
538 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY); /*FIXME*/
539 msg->header.size = htons(sizeof(struct GNUNET_MESH_TunnelMessage));
540 msg->tunnel_id = htonl(t->id.tid);
542 destroy_tunnel(c, t);
543 return sizeof(struct GNUNET_MESH_TunnelMessage);
548 * Core handler for path creation
549 * struct GNUNET_CORE_MessageHandler
552 * @param message message
553 * @param peer peer identity this notification is about
554 * @param atsi performance data
555 * @return GNUNET_OK to keep the connection open,
556 * GNUNET_SYSERR to close it (signal serious error)
560 handle_mesh_path_create (void *cls,
561 const struct GNUNET_PeerIdentity *peer,
562 const struct GNUNET_MessageHeader *message,
563 const struct GNUNET_TRANSPORT_ATS_Information
567 /* Find origin & self */
568 /* Search for origin in local tunnels */
569 /* Create tunnel / add path */
570 /* Retransmit to next link in chain, if any (core_notify + callback) */
576 * Core handler for mesh network traffic
579 * @param message message
580 * @param peer peer identity this notification is about
581 * @param atsi performance data
582 * @return GNUNET_OK to keep the connection open,
583 * GNUNET_SYSERR to close it (signal serious error)
586 handle_mesh_network_traffic (void *cls,
587 const struct GNUNET_PeerIdentity *peer,
588 const struct GNUNET_MessageHeader *message,
589 const struct GNUNET_TRANSPORT_ATS_Information
592 if (GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
593 /* Retransmit to next in path of tunnel identified by message */
595 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
596 /* Retransmit to previous in path of tunnel identified by message */
603 * Functions to handle messages from core
605 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
606 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
607 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
608 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
614 /******************************************************************************/
615 /********************* MESH LOCAL HANDLES **************************/
616 /******************************************************************************/
619 delete_tunnel_entry (void *cls, const GNUNET_HashCode * key, void *value) {
621 r = destroy_tunnel((struct MeshClient *) cls, (struct MeshTunnel *) value);
626 * notify_client_connection_failure: notify a client that the connection to the
627 * requested remote peer is not possible (for instance, no route found)
628 * Function called when the socket is ready to queue more data."buf" will be
629 * NULL and "size" zero if the socket was closed for writing in the meantime.
632 * @param size number of bytes available in buf
633 * @param buf where the callee should write the message
634 * @return number of bytes written to buf
637 notify_client_connection_failure (void *cls, size_t size, void *buf)
640 struct MeshPeerInfo *peer_info;
641 struct GNUNET_MESH_PeerControl *msg;
642 struct GNUNET_PeerIdentity id;
644 if (0 == size && NULL == buf) {
645 // TODO retry? cancel?
649 size_needed = sizeof(struct GNUNET_MESH_PeerControl);
650 peer_info = (struct MeshPeerInfo *) cls;
651 msg = (struct GNUNET_MESH_PeerControl *) buf;
652 msg->header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
653 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
654 // msg->tunnel_id = htonl(peer_info->t->tid);
655 GNUNET_PEER_resolve(peer_info->id, &id);
656 memcpy(&msg->peer, &id, sizeof(struct GNUNET_PeerIdentity));
664 * Function to process paths received for a new peer addition. The recorded
665 * paths form the initial tunnel, which can be optimized later.
666 * Called on each result obtained for the DHT search.
669 * @param exp when will this value expire
670 * @param key key of the result
671 * @param get_path NULL-terminated array of pointers
672 * to the peers on reverse GET path (or NULL if not recorded)
673 * @param put_path NULL-terminated array of pointers
674 * to the peers on the PUT path (or NULL if not recorded)
675 * @param type type of the result
676 * @param size number of bytes in data
677 * @param data pointer to the result data
680 dht_get_response_handler(void *cls,
681 struct GNUNET_TIME_Absolute exp,
682 const GNUNET_HashCode * key,
683 const struct GNUNET_PeerIdentity * const *get_path,
684 const struct GNUNET_PeerIdentity * const *put_path,
685 enum GNUNET_BLOCK_Type type,
689 struct MeshPeerInfo *peer_info;
690 struct MeshTunnel *t;
694 peer_info = (struct MeshPeerInfo *)cls;
697 if (NULL == get_path || NULL == put_path) {
698 // TODO: find ourselves some alternate initial path to the destination
699 GNUNET_SERVER_notify_transmit_ready(
701 sizeof(struct GNUNET_MESH_PeerControl),
702 GNUNET_TIME_UNIT_FOREVER_REL,
703 ¬ify_client_connection_failure,
708 p = GNUNET_malloc(sizeof(struct MeshPath));
709 GNUNET_CONTAINER_DLL_insert(t->paths_head, t->paths_tail, p);
710 for (i = 0; get_path[i] != NULL; i++);
711 for (i--; i >= 0; i--) {
712 p->peers = GNUNET_realloc(p->peers,
713 sizeof(GNUNET_PEER_Id) * (p->length + 1));
714 p->peers[p->length] = GNUNET_PEER_intern(get_path[i]);
717 for (i = 0; put_path[i] != NULL; i++);
718 for (i--; i >= 0; i--) {
719 p->peers = GNUNET_realloc(p->peers,
720 sizeof(GNUNET_PEER_Id) * (p->length + 1));
721 p->peers[p->length] = GNUNET_PEER_intern(put_path[i]);
724 // p->id = 0; // FIXME generate ID or remove field
726 // peer_info->first_hop = p->peers[1]; // FIXME do this on path completion
727 GNUNET_CORE_notify_transmit_ready(core_handle,
730 GNUNET_TIME_UNIT_FOREVER_REL,
732 sizeof(struct GNUNET_MESH_ManipulatePath)
734 * sizeof (struct GNUNET_PeerIdentity)),
735 &send_core_create_path_for_peer,
742 * Handler for client disconnection
745 * @param client identification of the client; NULL
746 * for the last call when the server is destroyed
749 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
751 struct MeshClient *c;
752 struct MeshClient *next;
754 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
755 "MESH: client disconnected\n");
758 if (c->handle == client) {
759 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
760 "MESH: matching client found, cleaning\n");
761 GNUNET_CONTAINER_multihashmap_iterate(c->tunnels,
762 &delete_tunnel_entry,
764 GNUNET_CONTAINER_multihashmap_destroy(c->tunnels);
765 if(0 != c->app_counter) GNUNET_free (c->apps);
766 if(0 != c->type_counter) GNUNET_free (c->types);
779 * Handler for new clients
782 * @param client identification of the client
783 * @param message the actual message, which includes messages the client wants
786 handle_local_new_client (void *cls,
787 struct GNUNET_SERVER_Client *client,
788 const struct GNUNET_MessageHeader *message)
790 struct GNUNET_MESH_ClientConnect *cc_msg;
791 struct MeshClient *c;
792 unsigned int payload_size;
796 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
797 "MESH: new client connected\n");
798 /* Check data sanity */
799 payload_size = ntohs(message->size)
800 - sizeof(struct GNUNET_MESH_ClientConnect);
801 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
802 types = ntohs(cc_msg->types);
803 apps = ntohs(cc_msg->applications);
805 types * sizeof(uint16_t) + apps * sizeof(GNUNET_MESH_ApplicationType))
808 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
812 /* Create new client structure */
813 c = GNUNET_malloc(sizeof(struct MeshClient));
816 c->type_counter = types;
817 c->types = GNUNET_malloc(types * sizeof(uint16_t));
818 memcpy(c->types, &message[1], types * sizeof(uint16_t));
821 c->app_counter = apps;
822 c->apps = GNUNET_malloc(apps * sizeof(GNUNET_MESH_ApplicationType));
824 &message[1] + types * sizeof(uint16_t),
825 apps * sizeof(GNUNET_MESH_ApplicationType));
827 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
828 "MESH: client has %u+%u subscriptions\n",
832 /* Insert new client in DLL */
833 /* FIXME insert client */
834 c->tunnels = GNUNET_CONTAINER_multihashmap_create(100);
836 GNUNET_SERVER_receive_done(client, GNUNET_OK);
842 * Handler for requests of new tunnels
845 * @param client identification of the client
846 * @param message the actual message
849 handle_local_tunnel_create (void *cls,
850 struct GNUNET_SERVER_Client *client,
851 const struct GNUNET_MessageHeader *message)
853 struct GNUNET_MESH_TunnelMessage *t_msg;
854 struct MeshTunnel *t;
855 struct MeshClient *c;
856 GNUNET_HashCode hash;
858 /* Sanity check for client registration */
859 if (NULL == (c = retrieve_client(client))) {
861 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
865 /* Message sanity check */
866 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
868 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
872 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
873 /* Sanity check for tunnel numbering */
874 if (0 == (ntohl(t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)) {
876 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
879 /* Sanity check for duplicate tunnel IDs */
880 if(NULL != retrieve_tunnel_by_local_id(c, ntohl(t_msg->tunnel_id))) {
882 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
885 t = GNUNET_malloc(sizeof(struct MeshTunnel ));
886 // FIXME:what if all 2^32 ID are taken?
887 while (NULL != retrieve_tunnel_by_local_id(c, next_tid)) next_tid++;
888 t->id.tid = next_tid++;
890 t->local_tid = ntohl(t_msg->tunnel_id);
893 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
895 GNUNET_CONTAINER_multihashmap_put(c->tunnels, &hash, t,
896 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
899 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
905 GNUNET_SERVER_receive_done(client, GNUNET_OK);
911 * Handler for requests of deleting tunnels
914 * @param client identification of the client
915 * @param message the actual message
918 handle_local_tunnel_destroy (void *cls,
919 struct GNUNET_SERVER_Client *client,
920 const struct GNUNET_MessageHeader *message)
922 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
923 struct MeshClient *c;
924 struct MeshTunnel *t;
925 MESH_TunnelNumber tid;
926 GNUNET_HashCode hash;
929 /* Sanity check for client registration */
930 if (NULL == (c = retrieve_client(client))) {
932 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
935 /* Message sanity check */
936 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
938 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
942 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
944 /* Retrieve tunnel */
945 tid = ntohl(tunnel_msg->tunnel_id);
946 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
947 t = GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
948 GNUNET_CONTAINER_multihashmap_remove_all(c->tunnels, &hash);
949 GNUNET_CORE_notify_transmit_ready(core_handle,
952 GNUNET_TIME_UNIT_FOREVER_REL,
954 sizeof(struct GNUNET_MESH_TunnelMessage),
955 &send_p2p_tunnel_destroy,
958 GNUNET_SERVER_receive_done(client, GNUNET_OK);
964 * Handler for connection requests to new peers
967 * @param client identification of the client
968 * @param message the actual message (PeerControl)
971 handle_local_connect_add (void *cls,
972 struct GNUNET_SERVER_Client *client,
973 const struct GNUNET_MessageHeader *message)
975 struct GNUNET_MESH_PeerControl *peer_msg;
976 struct MeshClient *c;
977 struct MeshTunnel *t;
978 MESH_TunnelNumber tid;
979 struct MeshPeerInfo *peer_info;
983 /* Sanity check for client registration */
984 if (NULL == (c = retrieve_client(client))) {
986 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
990 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
991 /* Sanity check for message size */
992 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
994 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
999 tid = ntohl(peer_msg->tunnel_id);
1000 t = retrieve_tunnel_by_local_id(c, tid);
1003 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1007 /* Does client own tunnel? */
1008 if (t->client->handle != client) {
1010 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1014 /* Ok, add peer to tunnel */
1015 peer_info = (struct MeshPeerInfo *) GNUNET_malloc(sizeof(struct MeshPeerInfo));
1016 peer_info->id = GNUNET_PEER_intern(&peer_msg->peer);
1017 peer_info->state = MESH_PEER_SEARCHING;
1020 /* Start DHT search */
1021 GNUNET_CRYPTO_hash (&peer_msg->peer,
1022 sizeof(struct GNUNET_PeerIdentity),
1024 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
1025 GNUNET_TIME_UNIT_FOREVER_REL,
1026 GNUNET_BLOCK_TYPE_ANY,
1028 4, /* replication level */
1029 GNUNET_DHT_RO_RECORD_ROUTE,
1030 NULL, /* bloom filter */
1033 0, /* xquery bits */
1034 dht_get_response_handler,
1037 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1043 * Handler for disconnection requests of peers in a tunnel
1045 * @param cls closure
1046 * @param client identification of the client
1047 * @param message the actual message (PeerControl)
1050 handle_local_connect_del (void *cls,
1051 struct GNUNET_SERVER_Client *client,
1052 const struct GNUNET_MessageHeader *message)
1054 struct GNUNET_MESH_PeerControl *peer_msg;
1055 struct MeshClient *c;
1056 struct MeshTunnel *t;
1058 struct MeshPath *aux_path;
1059 MESH_TunnelNumber tid;
1060 GNUNET_PEER_Id peer_id;
1062 /* Sanity check for client registration */
1063 if (NULL == (c = retrieve_client(client))) {
1065 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1068 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1069 /* Sanity check for message size */
1070 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
1072 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1076 /* Tunnel exists? */
1077 tid = ntohl(peer_msg->tunnel_id);
1078 t = retrieve_tunnel_by_local_id(c, tid);
1081 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1085 /* Does client own tunnel? */
1086 if (t->client->handle != client) {
1088 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1092 /* Ok, delete peer from tunnel */
1093 peer_id = GNUNET_PEER_intern(&peer_msg->peer);
1098 if (p->peers[p->length-1] == peer_id) { /* one path per destination */
1099 GNUNET_CONTAINER_DLL_remove(t->paths_head, t->paths_tail, p);
1100 GNUNET_PEER_decrement_rcs(p->peers, p->length);
1103 GNUNET_free(aux_path);
1109 /* FIXME Delete peer info */
1111 GNUNET_PEER_change_rc(peer_id, -1);
1113 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1119 * Handler for connection requests to new peers by type
1121 * @param cls closure
1122 * @param client identification of the client
1123 * @param message the actual message (ConnectPeerByType)
1126 handle_local_connect_by_type (void *cls,
1127 struct GNUNET_SERVER_Client *client,
1128 const struct GNUNET_MessageHeader *message)
1130 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
1131 MESH_TunnelNumber tid;
1132 GNUNET_MESH_ApplicationType application;
1133 struct MeshClient *c;
1134 struct MeshTunnel *t;
1136 /* Sanity check for client registration */
1137 if (NULL == (c = retrieve_client(client))) {
1139 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1143 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *)message;
1144 /* Sanity check for message size */
1145 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1146 ntohs(connect_msg->header.size))
1149 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1153 /* Tunnel exists? */
1154 tid = ntohl(connect_msg->tunnel_id);
1155 t = retrieve_tunnel_by_local_id(c, tid);
1158 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1162 /* Does client own tunnel? */
1163 if (t->client->handle != client) {
1165 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1169 /* Ok, lets find a peer offering the service */
1170 application = ntohl(connect_msg->type);
1171 application++; // FIXME silence warnings
1173 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1179 * Handler for client traffic directed to one peer
1181 * @param cls closure
1182 * @param client identification of the client
1183 * @param message the actual message
1186 handle_local_network_traffic (void *cls,
1187 struct GNUNET_SERVER_Client *client,
1188 const struct GNUNET_MessageHeader *message)
1190 struct MeshClient *c;
1191 struct MeshTunnel *t;
1192 struct GNUNET_MESH_Data *data_msg;
1193 MESH_TunnelNumber tid;
1195 /* Sanity check for client registration */
1196 if (NULL == (c = retrieve_client(client))) {
1198 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1201 data_msg = (struct GNUNET_MESH_Data *)message;
1202 /* Sanity check for message size */
1203 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1204 ntohs(data_msg->header.size))
1207 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1211 /* Tunnel exists? */
1212 tid = ntohl(data_msg->tunnel_id);
1213 t = retrieve_tunnel_by_local_id(c, tid);
1216 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1220 /* Does client own tunnel? */
1221 if (t->client->handle != client) {
1223 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1229 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1234 * Handler for client traffic directed to all peers in a tunnel
1236 * @param cls closure
1237 * @param client identification of the client
1238 * @param message the actual message
1241 handle_local_network_traffic_bcast (void *cls,
1242 struct GNUNET_SERVER_Client *client,
1243 const struct GNUNET_MessageHeader *message)
1245 struct MeshClient *c;
1246 struct MeshTunnel *t;
1247 struct GNUNET_MESH_DataBroadcast *data_msg;
1248 MESH_TunnelNumber tid;
1250 /* Sanity check for client registration */
1251 if (NULL == (c = retrieve_client(client))) {
1253 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1256 data_msg = (struct GNUNET_MESH_DataBroadcast *)message;
1257 /* Sanity check for message size */
1258 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(data_msg->header.size)) {
1260 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1264 /* Tunnel exists? */
1265 tid = ntohl(data_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);
1282 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1287 * Functions to handle messages from clients
1289 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1290 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
1291 {&handle_local_tunnel_create, NULL,
1292 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
1293 sizeof(struct GNUNET_MESH_TunnelMessage)},
1294 {&handle_local_tunnel_destroy, NULL,
1295 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
1296 sizeof(struct GNUNET_MESH_TunnelMessage)},
1297 {&handle_local_connect_add, NULL,
1298 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD,
1299 sizeof(struct GNUNET_MESH_PeerControl)},
1300 {&handle_local_connect_del, NULL,
1301 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL,
1302 sizeof(struct GNUNET_MESH_PeerControl)},
1303 {&handle_local_connect_by_type, NULL,
1304 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE,
1305 sizeof(struct GNUNET_MESH_ConnectPeerByType)},
1306 {&handle_local_network_traffic, NULL,
1307 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
1308 {&handle_local_network_traffic_bcast, NULL,
1309 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0},
1315 * To be called on core init/fail.
1317 * @param cls service closure
1318 * @param server handle to the server for this service
1319 * @param identity the public identity of this peer
1320 * @param publicKey the public key of this peer
1323 core_init (void *cls,
1324 struct GNUNET_CORE_Handle *server,
1325 const struct GNUNET_PeerIdentity *identity,
1326 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330 core_handle = server;
1331 myid = GNUNET_PEER_intern(identity);
1336 * Method called whenever a given peer connects.
1338 * @param cls closure
1339 * @param peer peer identity this notification is about
1340 * @param atsi performance data for the connection
1343 core_connect (void *cls,
1344 const struct GNUNET_PeerIdentity *peer,
1345 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1349 "Peer connected\n");
1350 pid = GNUNET_PEER_intern(peer);
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1359 * Method called whenever a peer disconnects.
1361 * @param cls closure
1362 * @param peer peer identity this notification is about
1365 core_disconnect (void *cls,
1367 GNUNET_PeerIdentity *peer)
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1370 "Peer disconnected\n");
1374 /******************************************************************************/
1375 /************************ MAIN FUNCTIONS ****************************/
1376 /******************************************************************************/
1379 * Task run during shutdown.
1385 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "MESH shutting down\n");
1389 if (core_handle != NULL) {
1390 GNUNET_CORE_disconnect (core_handle);
1393 if (dht_handle != NULL) {
1394 GNUNET_DHT_disconnect (dht_handle);
1400 * Process mesh requests.
1402 * @param cls closure
1403 * @param server the initialized server
1404 * @param c configuration to use
1408 struct GNUNET_SERVER_Handle *server,
1409 const struct GNUNET_CONFIGURATION_Handle *c)
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412 "MESH starting to run\n");
1413 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1414 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1415 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1416 32, /* queue size */
1417 NULL, /* Closure passed to MESH functions */
1418 &core_init, /* Call core_init once connected */
1419 &core_connect, /* Handle connects */
1420 &core_disconnect, /* remove peers on disconnects */
1421 NULL, /* Do we care about "status" updates? */
1422 NULL, /* Don't notify about all incoming messages */
1423 GNUNET_NO, /* For header only in notification */
1424 NULL, /* Don't notify about all outbound messages */
1425 GNUNET_NO, /* For header-only out notification */
1426 core_handlers); /* Register these handlers */
1427 if (core_handle == NULL) {
1430 dht_handle = GNUNET_DHT_connect(c, 100); /* FIXME ht len correct size? */
1431 if (dht_handle == NULL) {
1436 /* Scheduled the task to clean up when shutdown is called */
1437 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1438 &shutdown_task, NULL);
1443 * The main function for the mesh service.
1445 * @param argc number of arguments from the command line
1446 * @param argv command line arguments
1447 * @return 0 ok, 1 on error
1450 main (int argc, char *const *argv)
1455 GNUNET_SERVICE_run (argc,
1458 GNUNET_SERVICE_OPTION_NONE,
1459 &run, NULL)) ? 0 : 1;