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 * Messages that this client has declared interest in
300 GNUNET_MESH_ApplicationType *messages_subscribed;
301 unsigned int subscription_counter;
305 /******************************************************************************/
306 /*********************** GLOBAL VARIABLES ****************************/
307 /******************************************************************************/
312 static struct MeshClient *clients_head;
313 static struct MeshClient *clients_tail;
316 * Tunnels not owned by this node
318 // static struct MESH_Tunnel *tunnels_head;
319 // static struct MESH_Tunnel *tunnels_tail;
322 * Handle to communicate with core
324 static struct GNUNET_CORE_Handle *core_handle;
329 static struct GNUNET_DHT_Handle *dht_handle;
332 * Local peer own ID (memory efficient handle)
334 static GNUNET_PEER_Id myid;
336 /******************************************************************************/
337 /****************** GENERAL HELPER FUNCTIONS ************************/
338 /******************************************************************************/
341 * Check if client has registered with the service and has not disconnected
342 * @param client the client to check
343 * @return non-NULL if client exists in the global DLL
345 static struct MeshClient *
346 retrieve_client (struct GNUNET_SERVER_Client *client)
348 struct MeshClient *c;
352 if (c->handle == client) return c;
359 * Destroy the path and free any allocated resources linked to it
360 * @param t tunnel the path belongs to
361 * @param p the path to destroy
362 * @return GNUNET_OK on success
365 destroy_path(struct MESH_tunnel *t, struct MeshPath *p)
367 GNUNET_PEER_decrement_rcs(p->peers, p->length);
368 GNUNET_free(p->peers);
369 GNUNET_CONTAINER_DLL_remove(t->paths_head, t->paths_tail, p);
375 * Destroy the peer_info and free any allocated resources linked to it
376 * @param t tunnel the path belongs to
377 * @param pi the peer_info to destroy
378 * @return GNUNET_OK on success
381 destroy_peer_info(struct MESH_tunnel *t, struct MeshPeerInfo *pi)
383 GNUNET_PEER_change_rc(pi->id, -1);
384 GNUNET_CONTAINER_DLL_remove(t->peers_head, t->peers_tail, pi);
390 * Destroy the tunnel and free any allocated resources linked to it
391 * @param c client the tunnel belongs to
392 * @param t the tunnel to destroy
393 * @return GNUNET_OK on success
396 destroy_tunnel(struct MeshClient *c, struct MESH_tunnel *t)
398 struct MeshPeerInfo *pi;
399 struct MeshPath *path;
401 for (pi = t->peers_head; pi != NULL; pi = t->peers_head) {
402 destroy_peer_info(t, pi);
405 for (path = t->paths_head; path != NULL; path = t->paths_head) {
406 destroy_path(t, path);
409 GNUNET_CONTAINER_DLL_remove(c->tunnels_head, c->tunnels_tail, t);
414 /******************************************************************************/
415 /******************** MESH NETWORK HANDLERS **************************/
416 /******************************************************************************/
419 * Function called to notify a client about the socket
420 * being ready to queue more data. "buf" will be
421 * NULL and "size" zero if the socket was closed for
422 * writing in the meantime.
425 * @param size number of bytes available in buf
426 * @param buf where the callee should write the message
427 * @return number of bytes written to buf
430 send_core_create_path_for_peer (void *cls, size_t size, void *buf)
433 struct MeshPeerInfo *peer_info;
434 struct GNUNET_MESH_ManipulatePath *msg;
436 struct GNUNET_PeerIdentity peer_id;
437 struct GNUNET_PeerIdentity *peer_ptr;
440 if (0 == size && NULL == buf) {
441 // TODO retry? cancel?
444 peer_info = (struct MeshPeerInfo *)cls;
445 peer_info->dhtget = NULL;
446 p = peer_info->t->paths_head;
448 if (p->peers[p->length-1] == peer_info->id) {
453 if (p == NULL) return 0; // TODO Notify ERROR Path not found
455 size_needed = sizeof(struct GNUNET_MESH_ManipulatePath)
456 + p->length * sizeof(struct GNUNET_PeerIdentity);
457 if (size < size_needed) {
458 // TODO retry? cancel?
462 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
463 msg->header.size = htons(sizeof(struct GNUNET_MESH_ManipulatePath));
464 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
467 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
468 for (i = 0; i < p->length; i++) {
469 GNUNET_PEER_resolve(p->peers[i], &peer_id);
470 memcpy(&peer_ptr[i], &peer_id, sizeof(struct GNUNET_PeerIdentity));
473 peer_info->state = MESH_PEER_WAITING;
480 * Core handler for path creation
481 * struct GNUNET_CORE_MessageHandler
484 * @param message message
485 * @param peer peer identity this notification is about
486 * @param atsi performance data
487 * @return GNUNET_OK to keep the connection open,
488 * GNUNET_SYSERR to close it (signal serious error)
492 handle_mesh_path_create (void *cls,
493 const struct GNUNET_PeerIdentity *peer,
494 const struct GNUNET_MessageHeader *message,
495 const struct GNUNET_TRANSPORT_ATS_Information
499 /* Find origin & self */
500 /* Search for origin in local tunnels */
501 /* Create tunnel / add path */
502 /* Retransmit to next link in chain, if any (core_notify + callback) */
508 * Core handler for mesh network traffic
511 * @param message message
512 * @param peer peer identity this notification is about
513 * @param atsi performance data
514 * @return GNUNET_OK to keep the connection open,
515 * GNUNET_SYSERR to close it (signal serious error)
518 handle_mesh_network_traffic (void *cls,
519 const struct GNUNET_PeerIdentity *peer,
520 const struct GNUNET_MessageHeader *message,
521 const struct GNUNET_TRANSPORT_ATS_Information
524 if (GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
525 /* Retransmit to next in path of tunnel identified by message */
527 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
528 /* Retransmit to previous in path of tunnel identified by message */
535 * Functions to handle messages from core
537 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
538 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
539 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
540 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
546 /******************************************************************************/
547 /********************* MESH LOCAL HANDLES **************************/
548 /******************************************************************************/
551 * notify_client_connection_failure: notify a client that the connection to the
552 * requested remote peer is not possible (for instance, no route found)
553 * Function called when the socket is ready to queue more data."buf" will be
554 * NULL and "size" zero if the socket was closed for writing in the meantime.
557 * @param size number of bytes available in buf
558 * @param buf where the callee should write the message
559 * @return number of bytes written to buf
562 notify_client_connection_failure (void *cls, size_t size, void *buf)
565 struct MeshPeerInfo *peer_info;
566 struct GNUNET_MESH_PeerControl *msg;
567 struct GNUNET_PeerIdentity id;
569 if (0 == size && NULL == buf) {
570 // TODO retry? cancel?
574 size_needed = sizeof(struct GNUNET_MESH_PeerControl);
575 peer_info = (struct MeshPeerInfo *) cls;
576 msg = (struct GNUNET_MESH_PeerControl *) buf;
577 msg->header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
578 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
579 msg->tunnel_id = htonl(peer_info->t->tid);
580 GNUNET_PEER_resolve(peer_info->id, &id);
581 memcpy(&msg->peer, &id, sizeof(struct GNUNET_PeerIdentity));
588 * Function to process paths received for a new peer addition. The recorded
589 * paths form the initial tunnel, which can be optimized later.
590 * Called on each result obtained for the DHT search.
593 * @param exp when will this value expire
594 * @param key key of the result
595 * @param get_path NULL-terminated array of pointers
596 * to the peers on reverse GET path (or NULL if not recorded)
597 * @param put_path NULL-terminated array of pointers
598 * to the peers on the PUT path (or NULL if not recorded)
599 * @param type type of the result
600 * @param size number of bytes in data
601 * @param data pointer to the result data
604 dht_get_response_handler(void *cls,
605 struct GNUNET_TIME_Absolute exp,
606 const GNUNET_HashCode * key,
607 const struct GNUNET_PeerIdentity * const *get_path,
608 const struct GNUNET_PeerIdentity * const *put_path,
609 enum GNUNET_BLOCK_Type type,
613 struct MeshPeerInfo *peer_info;
614 struct MESH_tunnel *t;
618 peer_info = (struct MeshPeerInfo *)cls;
621 if (NULL == get_path || NULL == put_path) {
622 // TODO: find ourselves some alternate initial path to the destination
623 GNUNET_SERVER_notify_transmit_ready(
625 sizeof(struct GNUNET_MESH_PeerControl),
626 GNUNET_TIME_relative_get_forever(),
627 ¬ify_client_connection_failure,
632 p = GNUNET_malloc(sizeof(struct MeshPath));
633 GNUNET_CONTAINER_DLL_insert(t->paths_head, t->paths_tail, p);
634 for (i = 0; get_path[i] != NULL; i++);
635 for (i--; i >= 0; i--) {
636 p->peers = GNUNET_realloc(p->peers,
637 sizeof(GNUNET_PEER_Id) * (p->length + 1));
638 p->peers[p->length] = GNUNET_PEER_intern(get_path[i]);
641 for (i = 0; put_path[i] != NULL; i++);
642 for (i--; i >= 0; i--) {
643 p->peers = GNUNET_realloc(p->peers,
644 sizeof(GNUNET_PEER_Id) * (p->length + 1));
645 p->peers[p->length] = GNUNET_PEER_intern(put_path[i]);
648 // p->id = 0; // FIXME generate ID or remove field
650 // peer_info->first_hop = p->peers[1]; // FIXME do this on path completion
651 GNUNET_CORE_notify_transmit_ready(core_handle,
654 GNUNET_TIME_relative_get_forever(),
656 sizeof(struct GNUNET_MESH_ManipulatePath)
658 * sizeof (struct GNUNET_PeerIdentity)),
659 &send_core_create_path_for_peer,
667 * Handler for client disconnection
670 * @param client identification of the client; NULL
671 * for the last call when the server is destroyed
674 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
676 struct MeshClient *c, *next;
677 struct MESH_tunnel *t;
681 if (c->handle == client) {
682 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
683 while (NULL != (t = c->tunnels_head)) {
684 destroy_tunnel(c, t);
686 GNUNET_free (c->messages_subscribed);
699 * Handler for new clients
702 * @param client identification of the client
703 * @param message the actual message, which includes messages the client wants
706 handle_local_new_client (void *cls,
707 struct GNUNET_SERVER_Client *client,
708 const struct GNUNET_MessageHeader *message)
710 struct MeshClient *c;
711 unsigned int payload_size;
713 /* Check data sanity */
714 payload_size = message->size - sizeof(struct GNUNET_MessageHeader);
715 if (0 != payload_size % sizeof(GNUNET_MESH_ApplicationType)) {
717 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
721 /* Create new client structure */
722 c = GNUNET_malloc(sizeof(struct MeshClient));
724 if (payload_size != 0) {
725 c->messages_subscribed = GNUNET_malloc(payload_size);
726 memcpy(c->messages_subscribed, &message[1], payload_size);
728 c->messages_subscribed = NULL;
730 c->subscription_counter = payload_size/sizeof(GNUNET_MESH_ApplicationType);
732 /* Insert new client in DLL */
733 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
735 GNUNET_SERVER_receive_done(client, GNUNET_OK);
741 * Handler for requests of new tunnels
744 * @param client identification of the client
745 * @param message the actual message
748 handle_local_tunnel_create (void *cls,
749 struct GNUNET_SERVER_Client *client,
750 const struct GNUNET_MessageHeader *message)
752 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
753 struct MESH_tunnel *t;
754 struct MeshClient *c;
756 /* Sanity check for client registration */
757 if (NULL == (c = retrieve_client(client))) {
759 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
763 /* Message sanity check */
764 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
766 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
770 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
771 /* Sanity check for tunnel numbering */
772 if (0 == (ntohl(tunnel_msg->tunnel_id) & 0x80000000)) {
774 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
777 /* Sanity check for duplicate tunnel IDs */
780 if (t->tid == ntohl(tunnel_msg->tunnel_id)) {
782 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
787 /* FIXME: calloc? Is NULL != 0 on any platform? */
788 t = GNUNET_malloc(sizeof(struct MESH_tunnel));
789 t->tid = ntohl(tunnel_msg->tunnel_id);
793 GNUNET_CONTAINER_DLL_insert(c->tunnels_head, c->tunnels_tail, t);
795 GNUNET_SERVER_receive_done(client, GNUNET_OK);
801 * Handler for requests of deleting tunnels
804 * @param client identification of the client
805 * @param message the actual message
808 handle_local_tunnel_destroy (void *cls,
809 struct GNUNET_SERVER_Client *client,
810 const struct GNUNET_MessageHeader *message)
812 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
813 struct MeshClient *c;
814 struct MESH_tunnel *t;
817 /* Sanity check for client registration */
818 if (NULL == (c = retrieve_client(client))) {
820 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
823 /* Message sanity check */
824 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
826 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
830 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
833 tid = ntohl(tunnel_msg->tunnel_id);
834 if (NULL == (t = c->tunnels_head)) {
836 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
843 if (t == c->tunnels_tail) {
845 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
851 destroy_tunnel(c, t);
853 GNUNET_SERVER_receive_done(client, GNUNET_OK);
859 * Handler for connection requests to new peers
862 * @param client identification of the client
863 * @param message the actual message (PeerControl)
866 handle_local_connect_add (void *cls,
867 struct GNUNET_SERVER_Client *client,
868 const struct GNUNET_MessageHeader *message)
870 struct GNUNET_MESH_PeerControl *peer_msg;
871 struct MeshClient *c;
872 struct MESH_tunnel *t;
874 struct MeshPeerInfo *peer_info;
878 /* Sanity check for client registration */
879 if (NULL == (c = retrieve_client(client))) {
881 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
885 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
886 /* Sanity check for message size */
887 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
889 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
894 tid = ntohl(peer_msg->tunnel_id);
904 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
908 /* Does client own tunnel? */
909 if (t->client->handle != client) {
911 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
915 /* Ok, add peer to tunnel */
916 peer_info = (struct MeshPeerInfo *) GNUNET_malloc(sizeof(struct MeshPeerInfo));
917 peer_info->id = GNUNET_PEER_intern(&peer_msg->peer);
918 peer_info->state = MESH_PEER_SEARCHING;
921 GNUNET_CONTAINER_DLL_insert(t->peers_head, t->peers_tail, peer_info);
922 /* Start DHT search */
923 GNUNET_CRYPTO_hash (&peer_msg->peer,
924 sizeof(struct GNUNET_PeerIdentity),
926 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
927 GNUNET_TIME_relative_get_forever(),
928 GNUNET_BLOCK_TYPE_ANY,
930 4, /* replication level */
931 GNUNET_DHT_RO_RECORD_ROUTE,
932 NULL, /* bloom filter */
936 dht_get_response_handler,
939 GNUNET_SERVER_receive_done(client, GNUNET_OK);
945 * Handler for disconnection requests of peers in a tunnel
948 * @param client identification of the client
949 * @param message the actual message (PeerControl)
952 handle_local_connect_del (void *cls,
953 struct GNUNET_SERVER_Client *client,
954 const struct GNUNET_MessageHeader *message)
956 struct GNUNET_MESH_PeerControl *peer_msg;
957 struct MeshClient *c;
958 struct MESH_tunnel *t;
960 struct MeshPath *aux_path;
962 GNUNET_PEER_Id peer_id;
963 struct MeshPeerInfo *peer_info;
964 struct MeshPeerInfo *aux_peer_info;
966 /* Sanity check for client registration */
967 if (NULL == (c = retrieve_client(client))) {
969 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
972 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
973 /* Sanity check for message size */
974 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
976 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
981 tid = ntohl(peer_msg->tunnel_id);
982 if (NULL == (t = c->tunnels_head)) {
984 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
991 if (t == c->tunnels_tail) {
993 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1000 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1004 /* Does client own tunnel? */
1005 if (t->client->handle != client) {
1007 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1011 /* Ok, delete peer from tunnel */
1012 peer_id = GNUNET_PEER_intern(&peer_msg->peer);
1017 if (p->peers[p->length-1] == peer_id) { /* one path per destination */
1018 GNUNET_CONTAINER_DLL_remove(t->paths_head, t->paths_tail, p);
1019 GNUNET_PEER_decrement_rcs(p->peers, p->length);
1022 GNUNET_free(aux_path);
1028 /*Delete peer info */
1029 peer_info = t->peers_head;
1030 while (peer_info != NULL) {
1031 if (peer_info->id == peer_id) {
1032 GNUNET_CONTAINER_DLL_remove(t->peers_head,
1035 aux_peer_info = peer_info;
1036 peer_info = peer_info->next;
1037 GNUNET_free(aux_peer_info);
1039 peer_info = peer_info->next;
1043 GNUNET_PEER_change_rc(peer_id, -1);
1045 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1051 * Handler for connection requests to new peers by type
1053 * @param cls closure
1054 * @param client identification of the client
1055 * @param message the actual message (ConnectPeerByType)
1058 handle_local_connect_by_type (void *cls,
1059 struct GNUNET_SERVER_Client *client,
1060 const struct GNUNET_MessageHeader *message)
1062 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
1064 GNUNET_MESH_ApplicationType application;
1065 struct MeshClient *c;
1066 struct MESH_tunnel *t;
1068 /* Sanity check for client registration */
1069 if (NULL == (c = retrieve_client(client))) {
1071 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1075 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *)message;
1076 /* Sanity check for message size */
1077 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1078 ntohs(connect_msg->header.size))
1081 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1085 /* Tunnel exists? */
1086 tid = ntohl(connect_msg->tunnel_id);
1087 t = c->tunnels_head;
1089 if (t->tid == tid) {
1096 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1100 /* Does client own tunnel? */
1101 if (t->client->handle != client) {
1103 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1107 /* Ok, lets find a peer offering the service */
1108 application = ntohl(connect_msg->type);
1109 application++; // FIXME silence warnings
1111 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1117 * Handler for client traffic directed to one peer
1119 * @param cls closure
1120 * @param client identification of the client
1121 * @param message the actual message
1124 handle_local_network_traffic (void *cls,
1125 struct GNUNET_SERVER_Client *client,
1126 const struct GNUNET_MessageHeader *message)
1128 struct MeshClient *c;
1129 struct MESH_tunnel *t;
1130 struct GNUNET_MESH_Data *data_msg;
1133 /* Sanity check for client registration */
1134 if (NULL == (c = retrieve_client(client))) {
1136 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1139 data_msg = (struct GNUNET_MESH_Data *)message;
1140 /* Sanity check for message size */
1141 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1142 ntohs(data_msg->header.size))
1145 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1149 /* Tunnel exists? */
1150 tid = ntohl(data_msg->tunnel_id);
1151 t = c->tunnels_head;
1153 if (t->tid == tid) {
1160 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1164 /* Does client own tunnel? */
1165 if (t->client->handle != client) {
1167 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1173 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1178 * Handler for client traffic directed to all peers in a tunnel
1180 * @param cls closure
1181 * @param client identification of the client
1182 * @param message the actual message
1185 handle_local_network_traffic_bcast (void *cls,
1186 struct GNUNET_SERVER_Client *client,
1187 const struct GNUNET_MessageHeader *message)
1189 struct MeshClient *c;
1190 struct MESH_tunnel *t;
1191 struct GNUNET_MESH_DataBroadcast *data_msg;
1194 /* Sanity check for client registration */
1195 if (NULL == (c = retrieve_client(client))) {
1197 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1200 data_msg = (struct GNUNET_MESH_DataBroadcast *)message;
1201 /* Sanity check for message size */
1202 if (sizeof(struct GNUNET_MESH_PeerControl) != ntohs(data_msg->header.size)) {
1204 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1208 /* Tunnel exists? */
1209 tid = ntohl(data_msg->tunnel_id);
1210 t = c->tunnels_head;
1212 if (t->tid == tid) {
1219 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1223 /* Does client own tunnel? */
1224 if (t->client->handle != client) {
1226 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1232 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1237 * Functions to handle messages from clients
1239 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1240 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
1241 {&handle_local_tunnel_create, NULL,
1242 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
1243 sizeof(struct GNUNET_MESH_TunnelMessage)},
1244 {&handle_local_tunnel_destroy, NULL,
1245 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
1246 sizeof(struct GNUNET_MESH_TunnelMessage)},
1247 {&handle_local_connect_add, NULL,
1248 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD,
1249 sizeof(struct GNUNET_MESH_PeerControl)},
1250 {&handle_local_connect_del, NULL,
1251 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL,
1252 sizeof(struct GNUNET_MESH_PeerControl)},
1253 {&handle_local_connect_by_type, NULL,
1254 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE,
1255 sizeof(struct GNUNET_MESH_ConnectPeerByType)},
1256 {&handle_local_network_traffic, NULL,
1257 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
1258 {&handle_local_network_traffic_bcast, NULL,
1259 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0},
1265 * To be called on core init/fail.
1267 * @param cls service closure
1268 * @param server handle to the server for this service
1269 * @param identity the public identity of this peer
1270 * @param publicKey the public key of this peer
1273 core_init (void *cls,
1274 struct GNUNET_CORE_Handle *server,
1275 const struct GNUNET_PeerIdentity *identity,
1276 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1280 core_handle = server;
1281 myid = GNUNET_PEER_intern(identity);
1286 * Method called whenever a given peer connects.
1288 * @param cls closure
1289 * @param peer peer identity this notification is about
1290 * @param atsi performance data for the connection
1293 core_connect (void *cls,
1294 const struct GNUNET_PeerIdentity *peer,
1295 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1299 "Peer connected\n");
1300 pid = GNUNET_PEER_intern(peer);
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1309 * Method called whenever a peer disconnects.
1311 * @param cls closure
1312 * @param peer peer identity this notification is about
1315 core_disconnect (void *cls,
1317 GNUNET_PeerIdentity *peer)
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1320 "Peer disconnected\n");
1324 /******************************************************************************/
1325 /************************ MAIN FUNCTIONS ****************************/
1326 /******************************************************************************/
1329 * Task run during shutdown.
1335 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338 "MESH shutting down\n");
1339 if (core_handle != NULL) {
1340 GNUNET_CORE_disconnect (core_handle);
1343 if (dht_handle != NULL) {
1344 GNUNET_DHT_disconnect (dht_handle);
1350 * Process mesh requests.
1352 * @param cls closure
1353 * @param server the initialized server
1354 * @param c configuration to use
1358 struct GNUNET_SERVER_Handle *server,
1359 const struct GNUNET_CONFIGURATION_Handle *c)
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1362 "MESH starting to run\n");
1363 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1364 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1365 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1366 32, /* queue size */
1367 NULL, /* Closure passed to MESH functions */
1368 &core_init, /* Call core_init once connected */
1369 &core_connect, /* Handle connects */
1370 &core_disconnect, /* remove peers on disconnects */
1371 NULL, /* Do we care about "status" updates? */
1372 NULL, /* Don't notify about all incoming messages */
1373 GNUNET_NO, /* For header only in notification */
1374 NULL, /* Don't notify about all outbound messages */
1375 GNUNET_NO, /* For header-only out notification */
1376 core_handlers); /* Register these handlers */
1377 if (core_handle == NULL) {
1380 dht_handle = GNUNET_DHT_connect(c, 100); /* FIXME ht len correct size? */
1381 if (dht_handle == NULL) {
1385 /* Scheduled the task to clean up when shutdown is called */
1386 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1387 &shutdown_task, NULL);
1392 * The main function for the mesh service.
1394 * @param argc number of arguments from the command line
1395 * @param argv command line arguments
1396 * @return 0 ok, 1 on error
1399 main (int argc, char *const *argv)
1404 GNUNET_SERVICE_run (argc,
1407 GNUNET_SERVICE_OPTION_NONE,
1408 &run, NULL)) ? 0 : 1;