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 MESH_tunnel *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 MESH_tunnel *next;
207 struct MESH_tunnel *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 MESH_tunnel *tunnels_head;
290 struct MESH_tunnel *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;
311 /******************************************************************************/
312 /*********************** GLOBAL VARIABLES ****************************/
313 /******************************************************************************/
318 static struct MeshClient *clients_head;
319 static struct MeshClient *clients_tail;
322 * Tunnels not owned by this node
324 // static struct MESH_Tunnel *tunnels_head;
325 // static struct MESH_Tunnel *tunnels_tail;
328 * Handle to communicate with core
330 static struct GNUNET_CORE_Handle *core_handle;
335 static struct GNUNET_DHT_Handle *dht_handle;
338 * Local peer own ID (memory efficient handle)
340 static GNUNET_PEER_Id myid;
342 /******************************************************************************/
343 /****************** GENERAL HELPER FUNCTIONS ************************/
344 /******************************************************************************/
347 * Check if client has registered with the service and has not disconnected
348 * @param client the client to check
349 * @return non-NULL if client exists in the global DLL
351 static struct MeshClient *
352 retrieve_client (struct GNUNET_SERVER_Client *client)
354 struct MeshClient *c;
358 if (c->handle == client) return c;
365 * Destroy the path and free any allocated resources linked to it
366 * @param t tunnel the path belongs to
367 * @param p the path to destroy
368 * @return GNUNET_OK on success
371 destroy_path(struct MESH_tunnel *t, struct MeshPath *p)
373 GNUNET_PEER_decrement_rcs(p->peers, p->length);
374 GNUNET_free(p->peers);
375 GNUNET_CONTAINER_DLL_remove(t->paths_head, t->paths_tail, p);
381 * Destroy the peer_info and free any allocated resources linked to it
382 * @param t tunnel the path belongs to
383 * @param pi the peer_info to destroy
384 * @return GNUNET_OK on success
387 destroy_peer_info(struct MESH_tunnel *t, struct MeshPeerInfo *pi)
389 GNUNET_PEER_change_rc(pi->id, -1);
390 GNUNET_CONTAINER_DLL_remove(t->peers_head, t->peers_tail, pi);
396 * Destroy the tunnel and free any allocated resources linked to it
397 * @param c client the tunnel belongs to
398 * @param t the tunnel to destroy
399 * @return GNUNET_OK on success
402 destroy_tunnel(struct MeshClient *c, struct MESH_tunnel *t)
404 struct MeshPeerInfo *pi;
405 struct MeshPath *path;
407 for (pi = t->peers_head; pi != NULL; pi = t->peers_head) {
408 destroy_peer_info(t, pi);
411 for (path = t->paths_head; path != NULL; path = t->paths_head) {
412 destroy_path(t, path);
415 GNUNET_CONTAINER_DLL_remove(c->tunnels_head, c->tunnels_tail, t);
420 /******************************************************************************/
421 /******************** MESH NETWORK HANDLERS **************************/
422 /******************************************************************************/
425 * Function called to notify a client about the socket
426 * being ready to queue more data. "buf" will be
427 * NULL and "size" zero if the socket was closed for
428 * writing in the meantime.
431 * @param size number of bytes available in buf
432 * @param buf where the callee should write the message
433 * @return number of bytes written to buf
436 send_core_create_path_for_peer (void *cls, size_t size, void *buf)
439 struct MeshPeerInfo *peer_info;
440 struct GNUNET_MESH_ManipulatePath *msg;
442 struct GNUNET_PeerIdentity peer_id;
443 struct GNUNET_PeerIdentity *peer_ptr;
446 if (0 == size && NULL == buf) {
447 // TODO retry? cancel?
450 peer_info = (struct MeshPeerInfo *)cls;
451 peer_info->dhtget = NULL;
452 p = peer_info->t->paths_head;
454 if (p->peers[p->length-1] == peer_info->id) {
459 if (p == NULL) return 0; // TODO Notify ERROR Path not found
461 size_needed = sizeof(struct GNUNET_MESH_ManipulatePath)
462 + p->length * sizeof(struct GNUNET_PeerIdentity);
463 if (size < size_needed) {
464 // TODO retry? cancel?
468 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
469 msg->header.size = htons(sizeof(struct GNUNET_MESH_ManipulatePath));
470 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
473 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
474 for (i = 0; i < p->length; i++) {
475 GNUNET_PEER_resolve(p->peers[i], &peer_id);
476 memcpy(&peer_ptr[i], &peer_id, sizeof(struct GNUNET_PeerIdentity));
479 peer_info->state = MESH_PEER_WAITING;
486 * Core handler for path creation
487 * struct GNUNET_CORE_MessageHandler
490 * @param message message
491 * @param peer peer identity this notification is about
492 * @param atsi performance data
493 * @return GNUNET_OK to keep the connection open,
494 * GNUNET_SYSERR to close it (signal serious error)
498 handle_mesh_path_create (void *cls,
499 const struct GNUNET_PeerIdentity *peer,
500 const struct GNUNET_MessageHeader *message,
501 const struct GNUNET_TRANSPORT_ATS_Information
505 /* Find origin & self */
506 /* Search for origin in local tunnels */
507 /* Create tunnel / add path */
508 /* Retransmit to next link in chain, if any (core_notify + callback) */
514 * Core handler for mesh network traffic
517 * @param message message
518 * @param peer peer identity this notification is about
519 * @param atsi performance data
520 * @return GNUNET_OK to keep the connection open,
521 * GNUNET_SYSERR to close it (signal serious error)
524 handle_mesh_network_traffic (void *cls,
525 const struct GNUNET_PeerIdentity *peer,
526 const struct GNUNET_MessageHeader *message,
527 const struct GNUNET_TRANSPORT_ATS_Information
530 if (GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
531 /* Retransmit to next in path of tunnel identified by message */
533 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
534 /* Retransmit to previous in path of tunnel identified by message */
541 * Functions to handle messages from core
543 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
544 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
545 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
546 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
552 /******************************************************************************/
553 /********************* MESH LOCAL HANDLES **************************/
554 /******************************************************************************/
557 * notify_client_connection_failure: notify a client that the connection to the
558 * requested remote peer is not possible (for instance, no route found)
559 * Function called when the socket is ready to queue more data."buf" will be
560 * NULL and "size" zero if the socket was closed for writing in the meantime.
563 * @param size number of bytes available in buf
564 * @param buf where the callee should write the message
565 * @return number of bytes written to buf
568 notify_client_connection_failure (void *cls, size_t size, void *buf)
571 struct MeshPeerInfo *peer_info;
572 struct GNUNET_MESH_PeerControl *msg;
573 struct GNUNET_PeerIdentity id;
575 if (0 == size && NULL == buf) {
576 // TODO retry? cancel?
580 size_needed = sizeof(struct GNUNET_MESH_PeerControl);
581 peer_info = (struct MeshPeerInfo *) cls;
582 msg = (struct GNUNET_MESH_PeerControl *) buf;
583 msg->header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
584 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
585 msg->tunnel_id = htonl(peer_info->t->tid);
586 GNUNET_PEER_resolve(peer_info->id, &id);
587 memcpy(&msg->peer, &id, sizeof(struct GNUNET_PeerIdentity));
594 * Function to process paths received for a new peer addition. The recorded
595 * paths form the initial tunnel, which can be optimized later.
596 * Called on each result obtained for the DHT search.
599 * @param exp when will this value expire
600 * @param key key of the result
601 * @param get_path NULL-terminated array of pointers
602 * to the peers on reverse GET path (or NULL if not recorded)
603 * @param put_path NULL-terminated array of pointers
604 * to the peers on the PUT path (or NULL if not recorded)
605 * @param type type of the result
606 * @param size number of bytes in data
607 * @param data pointer to the result data
610 dht_get_response_handler(void *cls,
611 struct GNUNET_TIME_Absolute exp,
612 const GNUNET_HashCode * key,
613 const struct GNUNET_PeerIdentity * const *get_path,
614 const struct GNUNET_PeerIdentity * const *put_path,
615 enum GNUNET_BLOCK_Type type,
619 struct MeshPeerInfo *peer_info;
620 struct MESH_tunnel *t;
624 peer_info = (struct MeshPeerInfo *)cls;
627 if (NULL == get_path || NULL == put_path) {
628 // TODO: find ourselves some alternate initial path to the destination
629 GNUNET_SERVER_notify_transmit_ready(
631 sizeof(struct GNUNET_MESH_PeerControl),
632 GNUNET_TIME_relative_get_forever(),
633 ¬ify_client_connection_failure,
638 p = GNUNET_malloc(sizeof(struct MeshPath));
639 GNUNET_CONTAINER_DLL_insert(t->paths_head, t->paths_tail, p);
640 for (i = 0; get_path[i] != NULL; i++);
641 for (i--; i >= 0; i--) {
642 p->peers = GNUNET_realloc(p->peers,
643 sizeof(GNUNET_PEER_Id) * (p->length + 1));
644 p->peers[p->length] = GNUNET_PEER_intern(get_path[i]);
647 for (i = 0; put_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(put_path[i]);
654 // p->id = 0; // FIXME generate ID or remove field
656 // peer_info->first_hop = p->peers[1]; // FIXME do this on path completion
657 GNUNET_CORE_notify_transmit_ready(core_handle,
660 GNUNET_TIME_relative_get_forever(),
662 sizeof(struct GNUNET_MESH_ManipulatePath)
664 * sizeof (struct GNUNET_PeerIdentity)),
665 &send_core_create_path_for_peer,
672 * Handler for client disconnection
675 * @param client identification of the client; NULL
676 * for the last call when the server is destroyed
679 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
681 struct MeshClient *c, *next;
682 struct MESH_tunnel *t;
684 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
685 "MESH: client disconnected\n");
688 if (c->handle == client) {
689 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
690 "MESH: matching client found, cleaning\n");
691 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
692 while (NULL != (t = c->tunnels_head)) {
693 destroy_tunnel(c, t);
695 if(0 != c->app_counter) GNUNET_free (c->apps);
696 if(0 != c->type_counter) GNUNET_free (c->types);
709 * Handler for new clients
712 * @param client identification of the client
713 * @param message the actual message, which includes messages the client wants
716 handle_local_new_client (void *cls,
717 struct GNUNET_SERVER_Client *client,
718 const struct GNUNET_MessageHeader *message)
720 struct GNUNET_MESH_ClientConnect *cc_msg;
721 struct MeshClient *c;
722 unsigned int payload_size;
726 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
727 "MESH: new client connected\n");
728 /* Check data sanity */
729 payload_size = ntohs(message->size)
730 - sizeof(struct GNUNET_MESH_ClientConnect);
731 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
732 types = ntohs(cc_msg->types);
733 apps = ntohs(cc_msg->applications);
735 types * sizeof(uint16_t) + apps * sizeof(GNUNET_MESH_ApplicationType))
738 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
742 /* Create new client structure */
743 c = GNUNET_malloc(sizeof(struct MeshClient));
746 c->type_counter = types;
747 c->types = GNUNET_malloc(types * sizeof(uint16_t));
748 memcpy(c->types, &message[1], types * sizeof(uint16_t));
751 c->app_counter = apps;
752 c->apps = GNUNET_malloc(apps * sizeof(GNUNET_MESH_ApplicationType));
754 &message[1] + types * sizeof(uint16_t),
755 apps * sizeof(GNUNET_MESH_ApplicationType));
757 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
758 "MESH: client has %u+%u subscriptions\n",
762 /* Insert new client in DLL */
763 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
765 GNUNET_SERVER_receive_done(client, GNUNET_OK);
771 * Handler for requests of new tunnels
774 * @param client identification of the client
775 * @param message the actual message
778 handle_local_tunnel_create (void *cls,
779 struct GNUNET_SERVER_Client *client,
780 const struct GNUNET_MessageHeader *message)
782 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
783 struct MESH_tunnel *t;
784 struct MeshClient *c;
786 /* Sanity check for client registration */
787 if (NULL == (c = retrieve_client(client))) {
789 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
793 /* Message sanity check */
794 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
796 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
800 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
801 /* Sanity check for tunnel numbering */
802 if (0 == (ntohl(tunnel_msg->tunnel_id) & 0x80000000)) {
804 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
807 /* Sanity check for duplicate tunnel IDs */
810 if (t->tid == ntohl(tunnel_msg->tunnel_id)) {
812 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
817 /* FIXME: calloc? Is NULL != 0 on any platform? */
818 t = GNUNET_malloc(sizeof(struct MESH_tunnel));
819 t->tid = ntohl(tunnel_msg->tunnel_id);
823 GNUNET_CONTAINER_DLL_insert(c->tunnels_head, c->tunnels_tail, t);
825 GNUNET_SERVER_receive_done(client, GNUNET_OK);
831 * Handler for requests of deleting tunnels
834 * @param client identification of the client
835 * @param message the actual message
838 handle_local_tunnel_destroy (void *cls,
839 struct GNUNET_SERVER_Client *client,
840 const struct GNUNET_MessageHeader *message)
842 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
843 struct MeshClient *c;
844 struct MESH_tunnel *t;
847 /* Sanity check for client registration */
848 if (NULL == (c = retrieve_client(client))) {
850 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
853 /* Message sanity check */
854 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
856 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
860 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
863 tid = ntohl(tunnel_msg->tunnel_id);
864 if (NULL == (t = c->tunnels_head)) {
866 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
873 if (t == c->tunnels_tail) {
875 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
881 destroy_tunnel(c, t);
883 GNUNET_SERVER_receive_done(client, GNUNET_OK);
889 * Handler for connection requests to new peers
892 * @param client identification of the client
893 * @param message the actual message (PeerControl)
896 handle_local_connect_add (void *cls,
897 struct GNUNET_SERVER_Client *client,
898 const struct GNUNET_MessageHeader *message)
900 struct GNUNET_MESH_PeerControl *peer_msg;
901 struct MeshClient *c;
902 struct MESH_tunnel *t;
904 struct MeshPeerInfo *peer_info;
908 /* Sanity check for client registration */
909 if (NULL == (c = retrieve_client(client))) {
911 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
915 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
916 /* Sanity check for message size */
917 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
919 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
924 tid = ntohl(peer_msg->tunnel_id);
934 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
938 /* Does client own tunnel? */
939 if (t->client->handle != client) {
941 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
945 /* Ok, add peer to tunnel */
946 peer_info = (struct MeshPeerInfo *) GNUNET_malloc(sizeof(struct MeshPeerInfo));
947 peer_info->id = GNUNET_PEER_intern(&peer_msg->peer);
948 peer_info->state = MESH_PEER_SEARCHING;
951 GNUNET_CONTAINER_DLL_insert(t->peers_head, t->peers_tail, peer_info);
952 /* Start DHT search */
953 GNUNET_CRYPTO_hash (&peer_msg->peer,
954 sizeof(struct GNUNET_PeerIdentity),
956 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
957 GNUNET_TIME_relative_get_forever(),
958 GNUNET_BLOCK_TYPE_ANY,
960 4, /* replication level */
961 GNUNET_DHT_RO_RECORD_ROUTE,
962 NULL, /* bloom filter */
966 dht_get_response_handler,
969 GNUNET_SERVER_receive_done(client, GNUNET_OK);
975 * Handler for disconnection requests of peers in a tunnel
978 * @param client identification of the client
979 * @param message the actual message (PeerControl)
982 handle_local_connect_del (void *cls,
983 struct GNUNET_SERVER_Client *client,
984 const struct GNUNET_MessageHeader *message)
986 struct GNUNET_MESH_PeerControl *peer_msg;
987 struct MeshClient *c;
988 struct MESH_tunnel *t;
990 struct MeshPath *aux_path;
992 GNUNET_PEER_Id peer_id;
993 struct MeshPeerInfo *peer_info;
994 struct MeshPeerInfo *aux_peer_info;
996 /* Sanity check for client registration */
997 if (NULL == (c = retrieve_client(client))) {
999 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1002 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1003 /* Sanity check for message size */
1004 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
1006 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1010 /* Tunnel exists? */
1011 tid = ntohl(peer_msg->tunnel_id);
1012 if (NULL == (t = c->tunnels_head)) {
1014 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1018 if (t->tid == tid) {
1021 if (t == c->tunnels_tail) {
1023 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1030 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1034 /* Does client own tunnel? */
1035 if (t->client->handle != client) {
1037 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1041 /* Ok, delete peer from tunnel */
1042 peer_id = GNUNET_PEER_intern(&peer_msg->peer);
1047 if (p->peers[p->length-1] == peer_id) { /* one path per destination */
1048 GNUNET_CONTAINER_DLL_remove(t->paths_head, t->paths_tail, p);
1049 GNUNET_PEER_decrement_rcs(p->peers, p->length);
1052 GNUNET_free(aux_path);
1058 /*Delete peer info */
1059 peer_info = t->peers_head;
1060 while (peer_info != NULL) {
1061 if (peer_info->id == peer_id) {
1062 GNUNET_CONTAINER_DLL_remove(t->peers_head,
1065 aux_peer_info = peer_info;
1066 peer_info = peer_info->next;
1067 GNUNET_free(aux_peer_info);
1069 peer_info = peer_info->next;
1073 GNUNET_PEER_change_rc(peer_id, -1);
1075 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1081 * Handler for connection requests to new peers by type
1083 * @param cls closure
1084 * @param client identification of the client
1085 * @param message the actual message (ConnectPeerByType)
1088 handle_local_connect_by_type (void *cls,
1089 struct GNUNET_SERVER_Client *client,
1090 const struct GNUNET_MessageHeader *message)
1092 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
1094 GNUNET_MESH_ApplicationType application;
1095 struct MeshClient *c;
1096 struct MESH_tunnel *t;
1098 /* Sanity check for client registration */
1099 if (NULL == (c = retrieve_client(client))) {
1101 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1105 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *)message;
1106 /* Sanity check for message size */
1107 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1108 ntohs(connect_msg->header.size))
1111 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1115 /* Tunnel exists? */
1116 tid = ntohl(connect_msg->tunnel_id);
1117 t = c->tunnels_head;
1119 if (t->tid == tid) {
1126 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1130 /* Does client own tunnel? */
1131 if (t->client->handle != client) {
1133 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1137 /* Ok, lets find a peer offering the service */
1138 application = ntohl(connect_msg->type);
1139 application++; // FIXME silence warnings
1141 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1147 * Handler for client traffic directed to one peer
1149 * @param cls closure
1150 * @param client identification of the client
1151 * @param message the actual message
1154 handle_local_network_traffic (void *cls,
1155 struct GNUNET_SERVER_Client *client,
1156 const struct GNUNET_MessageHeader *message)
1158 struct MeshClient *c;
1159 struct MESH_tunnel *t;
1160 struct GNUNET_MESH_Data *data_msg;
1163 /* Sanity check for client registration */
1164 if (NULL == (c = retrieve_client(client))) {
1166 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1169 data_msg = (struct GNUNET_MESH_Data *)message;
1170 /* Sanity check for message size */
1171 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1172 ntohs(data_msg->header.size))
1175 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1179 /* Tunnel exists? */
1180 tid = ntohl(data_msg->tunnel_id);
1181 t = c->tunnels_head;
1183 if (t->tid == tid) {
1190 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1194 /* Does client own tunnel? */
1195 if (t->client->handle != client) {
1197 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1203 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1208 * Handler for client traffic directed to all peers in a tunnel
1210 * @param cls closure
1211 * @param client identification of the client
1212 * @param message the actual message
1215 handle_local_network_traffic_bcast (void *cls,
1216 struct GNUNET_SERVER_Client *client,
1217 const struct GNUNET_MessageHeader *message)
1219 struct MeshClient *c;
1220 struct MESH_tunnel *t;
1221 struct GNUNET_MESH_DataBroadcast *data_msg;
1224 /* Sanity check for client registration */
1225 if (NULL == (c = retrieve_client(client))) {
1227 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1230 data_msg = (struct GNUNET_MESH_DataBroadcast *)message;
1231 /* Sanity check for message size */
1232 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(data_msg->header.size)) {
1234 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1238 /* Tunnel exists? */
1239 tid = ntohl(data_msg->tunnel_id);
1240 t = c->tunnels_head;
1242 if (t->tid == tid) {
1249 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1253 /* Does client own tunnel? */
1254 if (t->client->handle != client) {
1256 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1262 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1267 * Functions to handle messages from clients
1269 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1270 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
1271 {&handle_local_tunnel_create, NULL,
1272 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
1273 sizeof(struct GNUNET_MESH_TunnelMessage)},
1274 {&handle_local_tunnel_destroy, NULL,
1275 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
1276 sizeof(struct GNUNET_MESH_TunnelMessage)},
1277 {&handle_local_connect_add, NULL,
1278 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD,
1279 sizeof(struct GNUNET_MESH_PeerControl)},
1280 {&handle_local_connect_del, NULL,
1281 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL,
1282 sizeof(struct GNUNET_MESH_PeerControl)},
1283 {&handle_local_connect_by_type, NULL,
1284 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE,
1285 sizeof(struct GNUNET_MESH_ConnectPeerByType)},
1286 {&handle_local_network_traffic, NULL,
1287 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
1288 {&handle_local_network_traffic_bcast, NULL,
1289 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0},
1295 * To be called on core init/fail.
1297 * @param cls service closure
1298 * @param server handle to the server for this service
1299 * @param identity the public identity of this peer
1300 * @param publicKey the public key of this peer
1303 core_init (void *cls,
1304 struct GNUNET_CORE_Handle *server,
1305 const struct GNUNET_PeerIdentity *identity,
1306 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1310 core_handle = server;
1311 myid = GNUNET_PEER_intern(identity);
1316 * Method called whenever a given peer connects.
1318 * @param cls closure
1319 * @param peer peer identity this notification is about
1320 * @param atsi performance data for the connection
1323 core_connect (void *cls,
1324 const struct GNUNET_PeerIdentity *peer,
1325 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1329 "Peer connected\n");
1330 pid = GNUNET_PEER_intern(peer);
1332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1339 * Method called whenever a peer disconnects.
1341 * @param cls closure
1342 * @param peer peer identity this notification is about
1345 core_disconnect (void *cls,
1347 GNUNET_PeerIdentity *peer)
1349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1350 "Peer disconnected\n");
1354 /******************************************************************************/
1355 /************************ MAIN FUNCTIONS ****************************/
1356 /******************************************************************************/
1359 * Task run during shutdown.
1365 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1368 "MESH shutting down\n");
1369 if (core_handle != NULL) {
1370 GNUNET_CORE_disconnect (core_handle);
1373 if (dht_handle != NULL) {
1374 GNUNET_DHT_disconnect (dht_handle);
1380 * Process mesh requests.
1382 * @param cls closure
1383 * @param server the initialized server
1384 * @param c configuration to use
1388 struct GNUNET_SERVER_Handle *server,
1389 const struct GNUNET_CONFIGURATION_Handle *c)
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 "MESH starting to run\n");
1393 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1394 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1395 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1396 32, /* queue size */
1397 NULL, /* Closure passed to MESH functions */
1398 &core_init, /* Call core_init once connected */
1399 &core_connect, /* Handle connects */
1400 &core_disconnect, /* remove peers on disconnects */
1401 NULL, /* Do we care about "status" updates? */
1402 NULL, /* Don't notify about all incoming messages */
1403 GNUNET_NO, /* For header only in notification */
1404 NULL, /* Don't notify about all outbound messages */
1405 GNUNET_NO, /* For header-only out notification */
1406 core_handlers); /* Register these handlers */
1407 if (core_handle == NULL) {
1410 dht_handle = GNUNET_DHT_connect(c, 100); /* FIXME ht len correct size? */
1411 if (dht_handle == NULL) {
1415 /* Scheduled the task to clean up when shutdown is called */
1416 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1417 &shutdown_task, NULL);
1422 * The main function for the mesh service.
1424 * @param argc number of arguments from the command line
1425 * @param argv command line arguments
1426 * @return 0 ok, 1 on error
1429 main (int argc, char *const *argv)
1434 GNUNET_SERVICE_run (argc,
1437 GNUNET_SERVICE_OPTION_NONE,
1438 &run, NULL)) ? 0 : 1;