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
91 struct MeshPeerInfo *next;
92 struct MeshPeerInfo *prev;
100 * Tunnel this peer belongs to
102 struct MeshTunnel *t;
105 * Is the peer reachable? Is the peer even connected?
107 enum MeshPeerState state;
110 * When to try to establish contact again?
112 struct GNUNET_TIME_Absolute next_reconnect_attempt;
115 * Who to send the data to --- FIXME what about multiple (alternate) paths?
117 GNUNET_PEER_Id first_hop;
120 * Max data rate to this peer
125 * Handle to stop the DHT search for a path to this peer
127 struct GNUNET_DHT_GetHandle *dhtget;
131 typedef uint32_t MESH_PathID;
135 * Information regarding a path
142 struct MeshPath *next;
143 struct MeshPath *prev;
146 * Id of the path, in case it's needed
151 * Whether the path is serving traffic in a tunnel or is a backup
156 * List of all the peers that form the path from origin to target
158 GNUNET_PEER_Id *peers;
163 * Data scheduled to transmit (to local client or remote peer)
170 struct MeshQueue *next;
171 struct MeshQueue *prev;
174 * Size of the message to transmit
179 * How old is the data?
181 struct GNUNET_TIME_Absolute timestamp;
186 struct GNUNET_MessageHeader *data;
190 struct MeshClient; /* FWD declaration */
192 * Struct containing all information regarding a tunnel
193 * For an intermediate node the improtant info used will be:
194 * - OID \ To identify
196 * - paths[0] | To know where to send it next
197 * - metainfo: ready, speeds, accounting
198 * For an end node more fields will be needed (client-handling)
206 struct MeshTunnel *next;
207 struct MeshTunnel *prev;
210 * Origin ID: Node that created the tunnel
215 * Tunnel number (unique for a given oid)
220 * Minimal speed for this tunnel in kb/s
225 * Maximal speed for this tunnel in kb/s
230 * Last time the tunnel was used
232 struct GNUNET_TIME_Absolute timestamp;
235 * Peers in the tunnel, for future optimizations
237 struct MeshPeerInfo *peers_head;
238 struct MeshPeerInfo *peers_tail;
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 * If this tunnel was created by a local client, what's its handle?
259 struct MeshClient *client;
262 * Messages ready to transmit
264 struct MeshQueue *out_head;
265 struct MeshQueue *out_tail;
268 * Messages received and not processed
270 struct MeshQueue *in_head;
271 struct MeshQueue *in_tail;
276 * Struct containing information about a client of the service
283 struct MeshClient *next;
284 struct MeshClient *prev;
287 * Tunnels that belong to this client, for convenience on disconnect
289 struct MeshTunnel *tunnels_head;
290 struct MeshTunnel *tunnels_tail;
293 * Handle to communicate with the client
295 struct GNUNET_SERVER_Client *handle;
298 * Applications that this client has claimed to provide
300 GNUNET_MESH_ApplicationType *apps;
301 unsigned int app_counter;
304 * Messages that this client has declared interest in
307 unsigned int type_counter;
310 * Map tunnel IDs used by the client to owner and global tunnel ID
312 struct GNUNET_CONTAINER_MultiHashMap* tunnel_ids;
316 /******************************************************************************/
317 /*********************** GLOBAL VARIABLES ****************************/
318 /******************************************************************************/
323 static struct MeshClient *clients_head;
324 static struct MeshClient *clients_tail;
327 * Tunnels not owned by this node
329 // static struct MESH_Tunnel *tunnels_head;
330 // static struct MESH_Tunnel *tunnels_tail;
333 * Handle to communicate with core
335 static struct GNUNET_CORE_Handle *core_handle;
340 static struct GNUNET_DHT_Handle *dht_handle;
343 * Local peer own ID (memory efficient handle)
345 static GNUNET_PEER_Id myid;
347 /******************************************************************************/
348 /****************** GENERAL HELPER FUNCTIONS ************************/
349 /******************************************************************************/
352 * Check if client has registered with the service and has not disconnected
353 * @param client the client to check
354 * @return non-NULL if client exists in the global DLL
356 static struct MeshClient *
357 retrieve_client (struct GNUNET_SERVER_Client *client)
359 struct MeshClient *c;
363 if (c->handle == client) return c;
370 * Destroy the path and free any allocated resources linked to it
371 * @param t tunnel the path belongs to
372 * @param p the path to destroy
373 * @return GNUNET_OK on success
376 destroy_path(struct MeshTunnel *t, struct MeshPath *p)
378 GNUNET_PEER_decrement_rcs(p->peers, p->length);
379 GNUNET_free(p->peers);
380 GNUNET_CONTAINER_DLL_remove(t->paths_head, t->paths_tail, p);
386 * Destroy the peer_info and free any allocated resources linked to it
387 * @param t tunnel the path belongs to
388 * @param pi the peer_info to destroy
389 * @return GNUNET_OK on success
392 destroy_peer_info(struct MeshTunnel *t, struct MeshPeerInfo *pi)
394 GNUNET_PEER_change_rc(pi->id, -1);
395 GNUNET_CONTAINER_DLL_remove(t->peers_head, t->peers_tail, pi);
401 * Destroy the tunnel and free any allocated resources linked to it
402 * @param c client the tunnel belongs to
403 * @param t the tunnel to destroy
404 * @return GNUNET_OK on success
407 destroy_tunnel(struct MeshClient *c, struct MeshTunnel *t)
409 struct MeshPeerInfo *pi;
410 struct MeshPath *path;
412 if (NULL == t) return GNUNET_OK;
414 for (pi = t->peers_head; pi != NULL; pi = t->peers_head) {
415 destroy_peer_info(t, pi);
418 for (path = t->paths_head; path != NULL; path = t->paths_head) {
419 destroy_path(t, path);
422 GNUNET_CONTAINER_DLL_remove(c->tunnels_head, c->tunnels_tail, t);
427 /******************************************************************************/
428 /******************** MESH NETWORK HANDLERS **************************/
429 /******************************************************************************/
432 * Function called to notify a client about the socket
433 * being ready to queue more data. "buf" will be
434 * NULL and "size" zero if the socket was closed for
435 * writing in the meantime.
438 * @param size number of bytes available in buf
439 * @param buf where the callee should write the message
440 * @return number of bytes written to buf
443 send_core_create_path_for_peer (void *cls, size_t size, void *buf)
446 struct MeshPeerInfo *peer_info;
447 struct GNUNET_MESH_ManipulatePath *msg;
449 struct GNUNET_PeerIdentity peer_id;
450 struct GNUNET_PeerIdentity *peer_ptr;
453 if (0 == size && NULL == buf) {
454 // TODO retry? cancel?
457 peer_info = (struct MeshPeerInfo *)cls;
458 peer_info->dhtget = NULL;
459 p = peer_info->t->paths_head;
461 if (p->peers[p->length-1] == peer_info->id) {
466 if (p == NULL) return 0; // TODO Notify ERROR Path not found
468 size_needed = sizeof(struct GNUNET_MESH_ManipulatePath)
469 + p->length * sizeof(struct GNUNET_PeerIdentity);
470 if (size < size_needed) {
471 // TODO retry? cancel?
475 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
476 msg->header.size = htons(sizeof(struct GNUNET_MESH_ManipulatePath));
477 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
480 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
481 for (i = 0; i < p->length; i++) {
482 GNUNET_PEER_resolve(p->peers[i], &peer_id);
483 memcpy(&peer_ptr[i], &peer_id, sizeof(struct GNUNET_PeerIdentity));
486 peer_info->state = MESH_PEER_WAITING;
493 * Core handler for path creation
494 * struct GNUNET_CORE_MessageHandler
497 * @param message message
498 * @param peer peer identity this notification is about
499 * @param atsi performance data
500 * @return GNUNET_OK to keep the connection open,
501 * GNUNET_SYSERR to close it (signal serious error)
505 handle_mesh_path_create (void *cls,
506 const struct GNUNET_PeerIdentity *peer,
507 const struct GNUNET_MessageHeader *message,
508 const struct GNUNET_TRANSPORT_ATS_Information
512 /* Find origin & self */
513 /* Search for origin in local tunnels */
514 /* Create tunnel / add path */
515 /* Retransmit to next link in chain, if any (core_notify + callback) */
521 * Core handler for mesh network traffic
524 * @param message message
525 * @param peer peer identity this notification is about
526 * @param atsi performance data
527 * @return GNUNET_OK to keep the connection open,
528 * GNUNET_SYSERR to close it (signal serious error)
531 handle_mesh_network_traffic (void *cls,
532 const struct GNUNET_PeerIdentity *peer,
533 const struct GNUNET_MessageHeader *message,
534 const struct GNUNET_TRANSPORT_ATS_Information
537 if (GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
538 /* Retransmit to next in path of tunnel identified by message */
540 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
541 /* Retransmit to previous in path of tunnel identified by message */
548 * Functions to handle messages from core
550 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
551 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
552 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
553 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
559 /******************************************************************************/
560 /********************* MESH LOCAL HANDLES **************************/
561 /******************************************************************************/
564 * notify_client_connection_failure: notify a client that the connection to the
565 * requested remote peer is not possible (for instance, no route found)
566 * Function called when the socket is ready to queue more data."buf" will be
567 * NULL and "size" zero if the socket was closed for writing in the meantime.
570 * @param size number of bytes available in buf
571 * @param buf where the callee should write the message
572 * @return number of bytes written to buf
575 notify_client_connection_failure (void *cls, size_t size, void *buf)
578 struct MeshPeerInfo *peer_info;
579 struct GNUNET_MESH_PeerControl *msg;
580 struct GNUNET_PeerIdentity id;
582 if (0 == size && NULL == buf) {
583 // TODO retry? cancel?
587 size_needed = sizeof(struct GNUNET_MESH_PeerControl);
588 peer_info = (struct MeshPeerInfo *) cls;
589 msg = (struct GNUNET_MESH_PeerControl *) buf;
590 msg->header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
591 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
592 msg->tunnel_id = htonl(peer_info->t->tid);
593 GNUNET_PEER_resolve(peer_info->id, &id);
594 memcpy(&msg->peer, &id, sizeof(struct GNUNET_PeerIdentity));
601 * Function to process paths received for a new peer addition. The recorded
602 * paths form the initial tunnel, which can be optimized later.
603 * Called on each result obtained for the DHT search.
606 * @param exp when will this value expire
607 * @param key key of the result
608 * @param get_path NULL-terminated array of pointers
609 * to the peers on reverse GET path (or NULL if not recorded)
610 * @param put_path NULL-terminated array of pointers
611 * to the peers on the PUT path (or NULL if not recorded)
612 * @param type type of the result
613 * @param size number of bytes in data
614 * @param data pointer to the result data
617 dht_get_response_handler(void *cls,
618 struct GNUNET_TIME_Absolute exp,
619 const GNUNET_HashCode * key,
620 const struct GNUNET_PeerIdentity * const *get_path,
621 const struct GNUNET_PeerIdentity * const *put_path,
622 enum GNUNET_BLOCK_Type type,
626 struct MeshPeerInfo *peer_info;
627 struct MeshTunnel *t;
631 peer_info = (struct MeshPeerInfo *)cls;
634 if (NULL == get_path || NULL == put_path) {
635 // TODO: find ourselves some alternate initial path to the destination
636 GNUNET_SERVER_notify_transmit_ready(
638 sizeof(struct GNUNET_MESH_PeerControl),
639 GNUNET_TIME_relative_get_forever(),
640 ¬ify_client_connection_failure,
645 p = GNUNET_malloc(sizeof(struct MeshPath));
646 GNUNET_CONTAINER_DLL_insert(t->paths_head, t->paths_tail, p);
647 for (i = 0; get_path[i] != NULL; i++);
648 for (i--; i >= 0; i--) {
649 p->peers = GNUNET_realloc(p->peers,
650 sizeof(GNUNET_PEER_Id) * (p->length + 1));
651 p->peers[p->length] = GNUNET_PEER_intern(get_path[i]);
654 for (i = 0; put_path[i] != NULL; i++);
655 for (i--; i >= 0; i--) {
656 p->peers = GNUNET_realloc(p->peers,
657 sizeof(GNUNET_PEER_Id) * (p->length + 1));
658 p->peers[p->length] = GNUNET_PEER_intern(put_path[i]);
661 // p->id = 0; // FIXME generate ID or remove field
663 // peer_info->first_hop = p->peers[1]; // FIXME do this on path completion
664 GNUNET_CORE_notify_transmit_ready(core_handle,
667 GNUNET_TIME_relative_get_forever(),
669 sizeof(struct GNUNET_MESH_ManipulatePath)
671 * sizeof (struct GNUNET_PeerIdentity)),
672 &send_core_create_path_for_peer,
679 * Handler for client disconnection
682 * @param client identification of the client; NULL
683 * for the last call when the server is destroyed
686 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
688 struct MeshClient *c, *next;
689 struct MeshTunnel *t;
691 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
692 "MESH: client disconnected\n");
695 if (c->handle == client) {
696 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
697 "MESH: matching client found, cleaning\n");
698 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
699 while (NULL != (t = c->tunnels_head)) {
700 destroy_tunnel(c, t);
702 if(0 != c->app_counter) GNUNET_free (c->apps);
703 if(0 != c->type_counter) GNUNET_free (c->types);
704 GNUNET_CONTAINER_multihashmap_destroy(c->tunnel_ids);
717 * Handler for new clients
720 * @param client identification of the client
721 * @param message the actual message, which includes messages the client wants
724 handle_local_new_client (void *cls,
725 struct GNUNET_SERVER_Client *client,
726 const struct GNUNET_MessageHeader *message)
728 struct GNUNET_MESH_ClientConnect *cc_msg;
729 struct MeshClient *c;
730 unsigned int payload_size;
734 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
735 "MESH: new client connected\n");
736 /* Check data sanity */
737 payload_size = ntohs(message->size)
738 - sizeof(struct GNUNET_MESH_ClientConnect);
739 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
740 types = ntohs(cc_msg->types);
741 apps = ntohs(cc_msg->applications);
743 types * sizeof(uint16_t) + apps * sizeof(GNUNET_MESH_ApplicationType))
746 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
750 /* Create new client structure */
751 c = GNUNET_malloc(sizeof(struct MeshClient));
754 c->type_counter = types;
755 c->types = GNUNET_malloc(types * sizeof(uint16_t));
756 memcpy(c->types, &message[1], types * sizeof(uint16_t));
759 c->app_counter = apps;
760 c->apps = GNUNET_malloc(apps * sizeof(GNUNET_MESH_ApplicationType));
762 &message[1] + types * sizeof(uint16_t),
763 apps * sizeof(GNUNET_MESH_ApplicationType));
765 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
766 "MESH: client has %u+%u subscriptions\n",
770 /* Insert new client in DLL */
771 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
772 c->tunnel_ids = GNUNET_CONTAINER_multihashmap_create(100);
774 GNUNET_SERVER_receive_done(client, GNUNET_OK);
780 * Handler for requests of new tunnels
783 * @param client identification of the client
784 * @param message the actual message
787 handle_local_tunnel_create (void *cls,
788 struct GNUNET_SERVER_Client *client,
789 const struct GNUNET_MessageHeader *message)
791 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
792 struct MeshTunnel *t;
793 struct MeshClient *c;
794 GNUNET_HashCode hash;
796 /* Sanity check for client registration */
797 if (NULL == (c = retrieve_client(client))) {
799 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
803 /* Message sanity check */
804 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
806 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
810 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
811 /* Sanity check for tunnel numbering */
812 if (0 == (ntohl(tunnel_msg->tunnel_id) & 0x80000000)) {
814 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
817 /* Sanity check for duplicate tunnel IDs */
820 if (t->tid == ntohl(tunnel_msg->tunnel_id)) {
822 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
827 t = GNUNET_malloc(sizeof(struct MeshTunnel ));
828 t->tid = ntohl(tunnel_msg->tunnel_id);
832 GNUNET_CRYPTO_hash(&t->tid, sizeof(MESH_TunnelID), &hash);
834 GNUNET_CONTAINER_multihashmap_put(c->tunnel_ids, &hash, t,
835 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
838 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
842 GNUNET_CONTAINER_DLL_insert(c->tunnels_head, c->tunnels_tail, t);
844 GNUNET_SERVER_receive_done(client, GNUNET_OK);
850 * Handler for requests of deleting tunnels
853 * @param client identification of the client
854 * @param message the actual message
857 handle_local_tunnel_destroy (void *cls,
858 struct GNUNET_SERVER_Client *client,
859 const struct GNUNET_MessageHeader *message)
861 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
862 struct MeshClient *c;
863 struct MeshTunnel *t;
865 GNUNET_HashCode hash;
868 /* Sanity check for client registration */
869 if (NULL == (c = retrieve_client(client))) {
871 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
874 /* Message sanity check */
875 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
877 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
881 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
883 /* Retrieve tunnel */
884 tid = ntohl(tunnel_msg->tunnel_id);
885 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelID), &hash);
886 t = GNUNET_CONTAINER_multihashmap_get(c->tunnel_ids, &hash);
887 GNUNET_CONTAINER_multihashmap_remove_all(c->tunnel_ids, &hash);
888 destroy_tunnel(c, t);
890 GNUNET_SERVER_receive_done(client, GNUNET_OK);
896 * Handler for connection requests to new peers
899 * @param client identification of the client
900 * @param message the actual message (PeerControl)
903 handle_local_connect_add (void *cls,
904 struct GNUNET_SERVER_Client *client,
905 const struct GNUNET_MessageHeader *message)
907 struct GNUNET_MESH_PeerControl *peer_msg;
908 struct MeshClient *c;
909 struct MeshTunnel *t;
911 struct MeshPeerInfo *peer_info;
915 /* Sanity check for client registration */
916 if (NULL == (c = retrieve_client(client))) {
918 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
922 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
923 /* Sanity check for message size */
924 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
926 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
931 tid = ntohl(peer_msg->tunnel_id);
941 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
945 /* Does client own tunnel? */
946 if (t->client->handle != client) {
948 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
952 /* Ok, add peer to tunnel */
953 peer_info = (struct MeshPeerInfo *) GNUNET_malloc(sizeof(struct MeshPeerInfo));
954 peer_info->id = GNUNET_PEER_intern(&peer_msg->peer);
955 peer_info->state = MESH_PEER_SEARCHING;
958 GNUNET_CONTAINER_DLL_insert(t->peers_head, t->peers_tail, peer_info);
959 /* Start DHT search */
960 GNUNET_CRYPTO_hash (&peer_msg->peer,
961 sizeof(struct GNUNET_PeerIdentity),
963 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
964 GNUNET_TIME_relative_get_forever(),
965 GNUNET_BLOCK_TYPE_ANY,
967 4, /* replication level */
968 GNUNET_DHT_RO_RECORD_ROUTE,
969 NULL, /* bloom filter */
973 dht_get_response_handler,
976 GNUNET_SERVER_receive_done(client, GNUNET_OK);
982 * Handler for disconnection requests of peers in a tunnel
985 * @param client identification of the client
986 * @param message the actual message (PeerControl)
989 handle_local_connect_del (void *cls,
990 struct GNUNET_SERVER_Client *client,
991 const struct GNUNET_MessageHeader *message)
993 struct GNUNET_MESH_PeerControl *peer_msg;
994 struct MeshClient *c;
995 struct MeshTunnel *t;
997 struct MeshPath *aux_path;
999 GNUNET_PEER_Id peer_id;
1000 struct MeshPeerInfo *peer_info;
1001 struct MeshPeerInfo *aux_peer_info;
1003 /* Sanity check for client registration */
1004 if (NULL == (c = retrieve_client(client))) {
1006 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1009 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1010 /* Sanity check for message size */
1011 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
1013 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1017 /* Tunnel exists? */
1018 tid = ntohl(peer_msg->tunnel_id);
1019 if (NULL == (t = c->tunnels_head)) {
1021 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1025 if (t->tid == tid) {
1028 if (t == c->tunnels_tail) {
1030 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1037 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1041 /* Does client own tunnel? */
1042 if (t->client->handle != client) {
1044 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1048 /* Ok, delete peer from tunnel */
1049 peer_id = GNUNET_PEER_intern(&peer_msg->peer);
1054 if (p->peers[p->length-1] == peer_id) { /* one path per destination */
1055 GNUNET_CONTAINER_DLL_remove(t->paths_head, t->paths_tail, p);
1056 GNUNET_PEER_decrement_rcs(p->peers, p->length);
1059 GNUNET_free(aux_path);
1065 /*Delete peer info */
1066 peer_info = t->peers_head;
1067 while (peer_info != NULL) {
1068 if (peer_info->id == peer_id) {
1069 GNUNET_CONTAINER_DLL_remove(t->peers_head,
1072 aux_peer_info = peer_info;
1073 peer_info = peer_info->next;
1074 GNUNET_free(aux_peer_info);
1076 peer_info = peer_info->next;
1080 GNUNET_PEER_change_rc(peer_id, -1);
1082 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1088 * Handler for connection requests to new peers by type
1090 * @param cls closure
1091 * @param client identification of the client
1092 * @param message the actual message (ConnectPeerByType)
1095 handle_local_connect_by_type (void *cls,
1096 struct GNUNET_SERVER_Client *client,
1097 const struct GNUNET_MessageHeader *message)
1099 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
1101 GNUNET_MESH_ApplicationType application;
1102 struct MeshClient *c;
1103 struct MeshTunnel *t;
1105 /* Sanity check for client registration */
1106 if (NULL == (c = retrieve_client(client))) {
1108 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1112 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *)message;
1113 /* Sanity check for message size */
1114 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1115 ntohs(connect_msg->header.size))
1118 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1122 /* Tunnel exists? */
1123 tid = ntohl(connect_msg->tunnel_id);
1124 t = c->tunnels_head;
1126 if (t->tid == tid) {
1133 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1137 /* Does client own tunnel? */
1138 if (t->client->handle != client) {
1140 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1144 /* Ok, lets find a peer offering the service */
1145 application = ntohl(connect_msg->type);
1146 application++; // FIXME silence warnings
1148 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1154 * Handler for client traffic directed to one peer
1156 * @param cls closure
1157 * @param client identification of the client
1158 * @param message the actual message
1161 handle_local_network_traffic (void *cls,
1162 struct GNUNET_SERVER_Client *client,
1163 const struct GNUNET_MessageHeader *message)
1165 struct MeshClient *c;
1166 struct MeshTunnel *t;
1167 struct GNUNET_MESH_Data *data_msg;
1170 /* Sanity check for client registration */
1171 if (NULL == (c = retrieve_client(client))) {
1173 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1176 data_msg = (struct GNUNET_MESH_Data *)message;
1177 /* Sanity check for message size */
1178 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1179 ntohs(data_msg->header.size))
1182 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1186 /* Tunnel exists? */
1187 tid = ntohl(data_msg->tunnel_id);
1188 t = c->tunnels_head;
1190 if (t->tid == tid) {
1197 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1201 /* Does client own tunnel? */
1202 if (t->client->handle != client) {
1204 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1210 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1215 * Handler for client traffic directed to all peers in a tunnel
1217 * @param cls closure
1218 * @param client identification of the client
1219 * @param message the actual message
1222 handle_local_network_traffic_bcast (void *cls,
1223 struct GNUNET_SERVER_Client *client,
1224 const struct GNUNET_MessageHeader *message)
1226 struct MeshClient *c;
1227 struct MeshTunnel *t;
1228 struct GNUNET_MESH_DataBroadcast *data_msg;
1231 /* Sanity check for client registration */
1232 if (NULL == (c = retrieve_client(client))) {
1234 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1237 data_msg = (struct GNUNET_MESH_DataBroadcast *)message;
1238 /* Sanity check for message size */
1239 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(data_msg->header.size)) {
1241 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1245 /* Tunnel exists? */
1246 tid = ntohl(data_msg->tunnel_id);
1247 t = c->tunnels_head;
1249 if (t->tid == tid) {
1256 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1260 /* Does client own tunnel? */
1261 if (t->client->handle != client) {
1263 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1269 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1274 * Functions to handle messages from clients
1276 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1277 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
1278 {&handle_local_tunnel_create, NULL,
1279 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
1280 sizeof(struct GNUNET_MESH_TunnelMessage)},
1281 {&handle_local_tunnel_destroy, NULL,
1282 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
1283 sizeof(struct GNUNET_MESH_TunnelMessage)},
1284 {&handle_local_connect_add, NULL,
1285 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD,
1286 sizeof(struct GNUNET_MESH_PeerControl)},
1287 {&handle_local_connect_del, NULL,
1288 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL,
1289 sizeof(struct GNUNET_MESH_PeerControl)},
1290 {&handle_local_connect_by_type, NULL,
1291 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE,
1292 sizeof(struct GNUNET_MESH_ConnectPeerByType)},
1293 {&handle_local_network_traffic, NULL,
1294 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
1295 {&handle_local_network_traffic_bcast, NULL,
1296 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0},
1302 * To be called on core init/fail.
1304 * @param cls service closure
1305 * @param server handle to the server for this service
1306 * @param identity the public identity of this peer
1307 * @param publicKey the public key of this peer
1310 core_init (void *cls,
1311 struct GNUNET_CORE_Handle *server,
1312 const struct GNUNET_PeerIdentity *identity,
1313 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1317 core_handle = server;
1318 myid = GNUNET_PEER_intern(identity);
1323 * Method called whenever a given peer connects.
1325 * @param cls closure
1326 * @param peer peer identity this notification is about
1327 * @param atsi performance data for the connection
1330 core_connect (void *cls,
1331 const struct GNUNET_PeerIdentity *peer,
1332 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1336 "Peer connected\n");
1337 pid = GNUNET_PEER_intern(peer);
1339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1346 * Method called whenever a peer disconnects.
1348 * @param cls closure
1349 * @param peer peer identity this notification is about
1352 core_disconnect (void *cls,
1354 GNUNET_PeerIdentity *peer)
1356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1357 "Peer disconnected\n");
1361 /******************************************************************************/
1362 /************************ MAIN FUNCTIONS ****************************/
1363 /******************************************************************************/
1366 * Task run during shutdown.
1372 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1375 "MESH shutting down\n");
1376 if (core_handle != NULL) {
1377 GNUNET_CORE_disconnect (core_handle);
1380 if (dht_handle != NULL) {
1381 GNUNET_DHT_disconnect (dht_handle);
1387 * Process mesh requests.
1389 * @param cls closure
1390 * @param server the initialized server
1391 * @param c configuration to use
1395 struct GNUNET_SERVER_Handle *server,
1396 const struct GNUNET_CONFIGURATION_Handle *c)
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1399 "MESH starting to run\n");
1400 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1401 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1402 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1403 32, /* queue size */
1404 NULL, /* Closure passed to MESH functions */
1405 &core_init, /* Call core_init once connected */
1406 &core_connect, /* Handle connects */
1407 &core_disconnect, /* remove peers on disconnects */
1408 NULL, /* Do we care about "status" updates? */
1409 NULL, /* Don't notify about all incoming messages */
1410 GNUNET_NO, /* For header only in notification */
1411 NULL, /* Don't notify about all outbound messages */
1412 GNUNET_NO, /* For header-only out notification */
1413 core_handlers); /* Register these handlers */
1414 if (core_handle == NULL) {
1417 dht_handle = GNUNET_DHT_connect(c, 100); /* FIXME ht len correct size? */
1418 if (dht_handle == NULL) {
1422 /* Scheduled the task to clean up when shutdown is called */
1423 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1424 &shutdown_task, NULL);
1429 * The main function for the mesh service.
1431 * @param argc number of arguments from the command line
1432 * @param argv command line arguments
1433 * @return 0 ok, 1 on error
1436 main (int argc, char *const *argv)
1441 GNUNET_SERVICE_run (argc,
1444 GNUNET_SERVICE_OPTION_NONE,
1445 &run, NULL)) ? 0 : 1;