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
27 * - MESH NETWORK MESSAGES
30 * - MESH NETWORK HANDLES
31 * - MESH LOCAL HANDLES
32 * - MAIN FUNCTIONS (main & run)
35 * - soft stateing (keep-alive (CHANGE?) / timeout / disconnect) -- not a message issue
36 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
37 * - partial disconnect reporting -- same as error reporting?
38 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
39 * - speed requirement specification (change?) in mesh API -- API call
43 #include "gnunet_common.h"
44 #include "gnunet_util_lib.h"
45 #include "gnunet_peer_lib.h"
46 #include "gnunet_core_service.h"
47 #include "gnunet_protocols.h"
50 #include "gnunet_dht_service.h"
52 /******************************************************************************/
53 /******************** MESH NETWORK MESSAGES **************************/
54 /******************************************************************************/
57 * Message for mesh path management
59 struct GNUNET_MESH_ManipulatePath
62 * Type: GNUNET_MESSAGE_TYPE_MESH_PATH_[CREATE|CHANGE|ADD|DEL]
64 * Size: sizeof(struct GNUNET_MESH_ManipulatePath) +
65 * path_length * sizeof (struct GNUNET_PeerIdentity)
67 struct GNUNET_MessageHeader header;
70 * Global id of the tunnel this path belongs to,
71 * unique in conjunction with the origin.
73 uint32_t tid GNUNET_PACKED;
76 * Information about speed requirements. If the tunnel cannot sustain the
77 * minimum bandwidth, packets are to be dropped.
79 uint32_t speed_min GNUNET_PACKED;
84 uint32_t reserved GNUNET_PACKED;
87 * path_length structs defining the *whole* path from the origin [0] to the
88 * final destination [path_length-1].
90 /* struct GNUNET_PeerIdentity peers[path_length]; */
94 * Message for mesh data traffic to all tunnel targets.
96 struct GNUNET_MESH_OriginMulticast
99 * Type: GNUNET_MESSAGE_TYPE_DATA_MULTICAST
101 struct GNUNET_MessageHeader header;
106 uint32_t tid GNUNET_PACKED;
111 struct GNUNET_PeerIdentity oid;
120 * Message for mesh data traffic to a particular destination from origin.
122 struct GNUNET_MESH_DataMessageFromOrigin
125 * Type: GNUNET_MESSAGE_TYPE_DATA_MESSAGE_FROM_ORIGIN
127 struct GNUNET_MessageHeader header;
132 uint32_t tid GNUNET_PACKED;
137 struct GNUNET_PeerIdentity oid;
142 struct GNUNET_PeerIdentity destination;
151 * Message for mesh data traffic from a tunnel participant to origin.
153 struct GNUNET_MESH_DataMessageToOrigin
156 * Type: GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN
158 struct GNUNET_MessageHeader header;
163 uint32_t tid GNUNET_PACKED;
168 struct GNUNET_PeerIdentity oid;
171 * Sender of the message.
173 struct GNUNET_PeerIdentity sender;
181 * Message for mesh flow control
183 struct GNUNET_MESH_SpeedNotify
186 * Type: GNUNET_MESSAGE_TYPE_DATA_SPEED_NOTIFY
188 struct GNUNET_MessageHeader header;
193 uint32_t tid GNUNET_PACKED;
198 struct GNUNET_PeerIdentity oid;
201 * Slowest link down the path (above minimum speed requirement).
207 /******************************************************************************/
208 /************************ DATA STRUCTURES ****************************/
209 /******************************************************************************/
212 * All the states a peer participating in a tunnel can be in.
217 * Path to the peer not known yet
222 * Request sent, not yet answered.
227 * Peer connected and ready to accept data
232 * Peer connected previosly but not responding
234 MESH_PEER_RECONNECTING
238 * Struct containing all information regarding a given peer
245 struct PeerInfo *next;
246 struct PeerInfo *prev;
254 * Tunnel this peer belongs to
256 struct MESH_tunnel *t;
259 * Is the peer reachable? Is the peer even connected?
261 enum PeerState state;
264 * When to try to establish contact again?
266 struct GNUNET_TIME_Absolute next_reconnect_attempt;
269 * Who to send the data to --- FIXME what about multiple (alternate) paths?
271 GNUNET_PEER_Id first_hop;
274 * Max data rate to this peer
279 * Handle to stop the DHT search for a path to this peer
281 struct GNUNET_DHT_GetHandle *dhtget;
285 typedef uint32_t MESH_PathID;
289 * Information regarding a path
300 * Id of the path, in case it's needed
305 * Whether the path is serving traffic in a tunnel or is a backup
310 * List of all the peers that form the path from origin to target
312 GNUNET_PEER_Id *peers;
317 * Data scheduled to transmit (to local client or remote peer)
324 struct MESH_queue *next;
325 struct MESH_queue *prev;
328 * Size of the message to transmit
333 * How old is the data?
335 struct GNUNET_TIME_Absolute timestamp;
340 struct GNUNET_MessageHeader *data;
344 struct Client; /* FWD declaration */
346 * Struct containing all information regarding a tunnel
347 * For an intermediate node the improtant info used will be:
348 * - OID \ To identify
350 * - paths[0] | To know where to send it next
351 * - metainfo: ready, speeds, accounting
352 * For an end node more fields will be needed (client-handling)
360 struct MESH_tunnel *next;
361 struct MESH_tunnel *prev;
364 * Origin ID: Node that created the tunnel
369 * Tunnel number (unique for a given oid)
374 * Minimal speed for this tunnel in kb/s
379 * Maximal speed for this tunnel in kb/s
384 * Last time the tunnel was used
386 struct GNUNET_TIME_Absolute timestamp;
389 * Peers in the tunnel, for future optimizations
391 struct PeerInfo *peers_head;
392 struct PeerInfo *peers_tail;
395 * Number of peers that are connected and potentially ready to receive data
397 unsigned int peers_ready;
400 * Number of peers that have been added to the tunnel
402 unsigned int peers_total;
405 * Paths (used and backup)
407 struct Path *paths_head;
408 struct Path *paths_tail;
411 * If this tunnel was created by a local client, what's its handle?
413 struct Client *client;
416 * Messages ready to transmit
418 struct MESH_queue *out_head;
419 struct MESH_queue *out_tail;
422 * Messages received and not processed
424 struct MESH_queue *in_head;
425 struct MESH_queue *in_tail;
430 * Struct containing information about a client of the service
441 * Tunnels that belong to this client, for convenience on disconnect
443 struct MESH_tunnel *tunnels_head;
444 struct MESH_tunnel *tunnels_tail;
447 * Handle to communicate with the client
449 struct GNUNET_SERVER_Client *handle;
452 * Messages that this client has declared interest in
454 GNUNET_MESH_ApplicationType *messages_subscribed;
455 unsigned int subscription_counter;
459 /******************************************************************************/
460 /*********************** GLOBAL VARIABLES ****************************/
461 /******************************************************************************/
466 static struct Client *clients_head;
467 static struct Client *clients_tail;
470 * Tunnels not owned by this node
472 // static struct MESH_Tunnel *tunnels_head;
473 // static struct MESH_Tunnel *tunnels_tail;
476 * Handle to communicate with core
478 static struct GNUNET_CORE_Handle *core_handle;
483 static struct GNUNET_DHT_Handle *dht_handle;
486 * Local peer own ID (memory efficient handle)
488 static GNUNET_PEER_Id myid;
490 /******************************************************************************/
491 /******************** MESH NETWORK HANDLERS **************************/
492 /******************************************************************************/
495 * Function called to notify a client about the socket
496 * being ready to queue more data. "buf" will be
497 * NULL and "size" zero if the socket was closed for
498 * writing in the meantime.
501 * @param size number of bytes available in buf
502 * @param buf where the callee should write the message
503 * @return number of bytes written to buf
505 size_t send_core_create_path_for_peer (void *cls, size_t size, void *buf) {
507 struct PeerInfo *peer_info;
508 struct GNUNET_MESH_ManipulatePath *msg;
510 struct GNUNET_PeerIdentity peer_id;
511 struct GNUNET_PeerIdentity *peer_ptr;
514 if(0 == size && NULL == buf) {
515 // TODO retry? cancel?
518 peer_info = (struct PeerInfo *)cls;
519 peer_info->dhtget = NULL;
520 p = peer_info->t->paths_head;
522 if(p->peers[p->length-1] == peer_info->id) {
525 if(p != peer_info->t->paths_tail) {
528 // TODO ERROR Path not found
532 size_needed = sizeof(struct GNUNET_MESH_ManipulatePath)
533 + p->length * sizeof(struct GNUNET_PeerIdentity);
534 if(size < size_needed) {
535 // TODO retry? cancel?
539 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
540 msg->header.size = htons(sizeof(struct GNUNET_MESH_ManipulatePath));
541 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
544 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
545 for(i = 0; i < p->length; i++) {
546 GNUNET_PEER_resolve(p->peers[i], &peer_id);
547 memcpy(&peer_ptr[i], &peer_id, sizeof(struct GNUNET_PeerIdentity));
550 peer_info->state = MESH_PEER_WAITING;
557 * Core handler for path creation
558 * struct GNUNET_CORE_MessageHandler
561 * @param message message
562 * @param peer peer identity this notification is about
563 * @param atsi performance data
564 * @return GNUNET_OK to keep the connection open,
565 * GNUNET_SYSERR to close it (signal serious error)
569 handle_mesh_path_create (void *cls,
570 const struct GNUNET_PeerIdentity *peer,
571 const struct GNUNET_MessageHeader *message,
572 const struct GNUNET_TRANSPORT_ATS_Information
576 /* Find origin & self */
577 /* Search for origin in local tunnels */
578 /* Create tunnel / add path */
579 /* Retransmit to next link in chain, if any (core_notify + callback) */
584 * Core handler for mesh network traffic
587 * @param message message
588 * @param peer peer identity this notification is about
589 * @param atsi performance data
590 * @return GNUNET_OK to keep the connection open,
591 * GNUNET_SYSERR to close it (signal serious error)
594 handle_mesh_network_traffic (void *cls,
595 const struct GNUNET_PeerIdentity *peer,
596 const struct GNUNET_MessageHeader *message,
597 const struct GNUNET_TRANSPORT_ATS_Information
600 if(GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
601 /* Retransmit to next in path of tunnel identified by message */
603 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
604 /* Retransmit to previous in path of tunnel identified by message */
610 * Functions to handle messages from core
612 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
613 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
614 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
615 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
621 /******************************************************************************/
622 /********************* MESH LOCAL HANDLES **************************/
623 /******************************************************************************/
626 * Check if client has registered with the service and has not disconnected
627 * @param client the client to check
628 * @return non-NULL if client exists in the global DLL
631 client_retrieve (struct GNUNET_SERVER_Client *client) {
635 if(c->handle == client) return c;
636 if(c == clients_tail)
645 * notify_client_connection_failure: notify a client that the connection to the
646 * requested remote peer is not possible (for instance, no route found)
647 * Function called when the socket is ready to queue more data."buf" will be
648 * NULL and "size" zero if the socket was closed for writing in the meantime.
651 * @param size number of bytes available in buf
652 * @param buf where the callee should write the message
653 * @return number of bytes written to buf
655 size_t notify_client_connection_failure (void *cls, size_t size, void *buf) {
657 struct PeerInfo *peer_info;
658 struct GNUNET_MESH_PeerControl *msg;
659 struct GNUNET_PeerIdentity id;
661 if(0 == size && NULL == buf) {
662 // TODO retry? cancel?
666 size_needed = sizeof(struct GNUNET_MESH_PeerControl);
667 peer_info = (struct PeerInfo *) cls;
668 msg = (struct GNUNET_MESH_PeerControl *) buf;
669 msg->header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
670 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
671 msg->tunnel_id = htonl(peer_info->t->tid);
672 GNUNET_PEER_resolve(peer_info->id, &id);
673 memcpy(&msg->peer, &id, sizeof(struct GNUNET_PeerIdentity));
680 * Iterator called on each result obtained for a DHT
681 * operation that expects a reply
684 * @param exp when will this value expire
685 * @param key key of the result
686 * @param get_path NULL-terminated array of pointers
687 * to the peers on reverse GET path (or NULL if not recorded)
688 * @param put_path NULL-terminated array of pointers
689 * to the peers on the PUT path (or NULL if not recorded)
690 * @param type type of the result
691 * @param size number of bytes in data
692 * @param data pointer to the result data
694 void dht_get_response_handler(void *cls,
695 struct GNUNET_TIME_Absolute exp,
696 const GNUNET_HashCode * key,
697 const struct GNUNET_PeerIdentity * const *get_path,
698 const struct GNUNET_PeerIdentity * const *put_path,
699 enum GNUNET_BLOCK_Type type,
703 struct PeerInfo *peer_info;
704 struct MESH_tunnel *t;
708 peer_info = (struct PeerInfo *)cls;
711 if(NULL == get_path || NULL == put_path) {
712 // TODO: find ourselves some alternate initial path to the destination
713 GNUNET_SERVER_notify_transmit_ready(
715 sizeof(struct GNUNET_MESH_PeerControl),
716 GNUNET_TIME_relative_get_forever(),
717 ¬ify_client_connection_failure,
722 p = GNUNET_malloc(sizeof(struct Path));
723 GNUNET_CONTAINER_DLL_insert(t->paths_head, t->paths_tail, p);
724 for(i = 0; get_path[i] != NULL; i++) {
725 p->peers = GNUNET_realloc(p->peers,
726 sizeof(GNUNET_PEER_Id) * (p->length + 1));
727 p->peers[p->length] = GNUNET_PEER_intern(get_path[i]);
730 for(i = 0; put_path[i] != NULL; i++) {
731 p->peers = GNUNET_realloc(p->peers,
732 sizeof(GNUNET_PEER_Id) * (p->length + 1));
733 p->peers[p->length] = GNUNET_PEER_intern(put_path[i]);
736 // p->id = 0; // FIXME generate ID or remove field
738 // peer_info->first_hop = p->peers[1]; // FIXME do this on path completion
739 GNUNET_CORE_notify_transmit_ready(core_handle,
742 GNUNET_TIME_relative_get_forever(),
744 sizeof(struct GNUNET_MESH_ManipulatePath)
746 * sizeof (struct GNUNET_PeerIdentity)),
747 &send_core_create_path_for_peer,
754 * Handler for client disconnection
757 * @param client identification of the client; NULL
758 * for the last call when the server is destroyed
761 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
763 struct Client *c, *next;
764 struct MESH_tunnel *t;
768 if (c->handle == client) {
769 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
770 while (NULL != (t = c->tunnels_head)) {
771 GNUNET_CONTAINER_DLL_remove (c->tunnels_head,
774 /* TODO free paths and other tunnel dynamic structures */
777 GNUNET_free (c->messages_subscribed);
784 if(c == clients_head) return; /* Tail already processed? */
790 * Handler for new clients
793 * @param client identification of the client
794 * @param message the actual message, which includes messages the client wants
797 handle_local_new_client (void *cls,
798 struct GNUNET_SERVER_Client *client,
799 const struct GNUNET_MessageHeader *message)
802 unsigned int payload_size;
804 /* Check data sanity */
805 payload_size = message->size - sizeof(struct GNUNET_MessageHeader);
806 if (0 != payload_size % sizeof(GNUNET_MESH_ApplicationType)) {
808 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
812 /* Create new client structure */
813 c = GNUNET_malloc(sizeof(struct Client));
815 c->tunnels_head = NULL;
816 c->tunnels_tail = NULL;
817 if(payload_size != 0) {
818 c->messages_subscribed = GNUNET_malloc(payload_size);
819 memcpy(c->messages_subscribed, &message[1], payload_size);
821 c->messages_subscribed = NULL;
823 c->subscription_counter = payload_size/sizeof(GNUNET_MESH_ApplicationType);
825 /* Insert new client in DLL */
826 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
828 GNUNET_SERVER_receive_done(client, GNUNET_OK);
832 * Handler for requests of new tunnels
835 * @param client identification of the client
836 * @param message the actual message
839 handle_local_tunnel_create (void *cls,
840 struct GNUNET_SERVER_Client *client,
841 const struct GNUNET_MessageHeader *message)
843 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
844 struct MESH_tunnel *t;
847 /* Sanity check for client registration */
848 if(NULL == (c = client_retrieve(client))) {
850 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
854 /* Message sanity check */
855 if(sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
857 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
861 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
862 /* Sanity check for tunnel numbering */
863 if(0 == (ntohl(tunnel_msg->tunnel_id) & 0x80000000)) {
865 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
868 /* Sanity check for duplicate tunnel IDs */
871 if(t->tid == ntohl(tunnel_msg->tunnel_id)) {
873 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
876 if(t == c->tunnels_tail) break;
879 /* FIXME: calloc? Is NULL != 0 on any platform? */
880 t = GNUNET_malloc(sizeof(struct MESH_tunnel));
881 t->tid = ntohl(tunnel_msg->tunnel_id);
885 t->peers_head = NULL;
886 t->peers_tail = NULL;
887 t->paths_head = NULL;
888 t->paths_tail = NULL;
895 GNUNET_CONTAINER_DLL_insert(c->tunnels_head, c->tunnels_tail, t);
897 GNUNET_SERVER_receive_done(client, GNUNET_OK);
902 * Handler for requests of deleting tunnels
905 * @param client identification of the client
906 * @param message the actual message
909 handle_local_tunnel_destroy (void *cls,
910 struct GNUNET_SERVER_Client *client,
911 const struct GNUNET_MessageHeader *message)
913 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
915 struct MESH_tunnel *t;
919 /* Sanity check for client registration */
920 if(NULL == (c = client_retrieve(client))) {
922 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
925 /* Message sanity check */
926 if(sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
928 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
932 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
935 tid = ntohl(tunnel_msg->tunnel_id);
936 if(NULL == (t = c->tunnels_head)) {
938 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
945 if(t == c->tunnels_tail) {
947 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
953 GNUNET_CONTAINER_DLL_remove(c->tunnels_head, c->tunnels_tail, t);
955 for(pi = t->peers_head; pi != NULL; pi = t->peers_head) {
956 GNUNET_PEER_change_rc(pi->id, -1);
957 GNUNET_CONTAINER_DLL_remove(t->peers_head, t->peers_tail, pi);
962 GNUNET_SERVER_receive_done(client, GNUNET_OK);
967 * Handler for connection requests to new peers
970 * @param client identification of the client
971 * @param message the actual message (PeerControl)
974 handle_local_connect_add (void *cls,
975 struct GNUNET_SERVER_Client *client,
976 const struct GNUNET_MessageHeader *message)
978 struct GNUNET_MESH_PeerControl *peer_msg;
980 struct MESH_tunnel *t;
982 struct PeerInfo *peer_info;
987 /* Sanity check for client registration */
988 if(NULL == (c = client_retrieve(client))) {
990 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
994 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
995 /* Sanity check for message size */
996 if(sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
998 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1002 /* Tunnel exists? */
1003 tid = ntohl(peer_msg->tunnel_id);
1004 if(NULL == (t = c->tunnels_head)) {
1006 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1013 if(t == c->tunnels_tail) {
1015 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1021 /* Does client own tunnel? */
1022 if(t->client->handle != client) {
1024 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1028 /* Ok, add peer to tunnel */
1029 peer_info = (struct PeerInfo *) GNUNET_malloc(sizeof(struct PeerInfo));
1030 peer_info->id = GNUNET_PEER_intern(&peer_msg->peer);
1031 peer_info->state = MESH_PEER_SEARCHING;
1034 GNUNET_CONTAINER_DLL_insert(t->peers_head, t->peers_tail, peer_info);
1035 /* start dht search */
1036 // FIXME key = hash (peerid + salt);
1037 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
1038 GNUNET_TIME_relative_get_forever(),
1039 GNUNET_BLOCK_TYPE_ANY,
1041 4, /* replication level */
1042 GNUNET_DHT_RO_RECORD_ROUTE,
1043 NULL, /* bloom filter */
1046 0, /* xquery bits */
1047 dht_get_response_handler,
1050 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1056 * Handler for disconnection requests of peers in a tunnel
1058 * @param cls closure
1059 * @param client identification of the client
1060 * @param message the actual message (PeerControl)
1063 handle_local_connect_del (void *cls,
1064 struct GNUNET_SERVER_Client *client,
1065 const struct GNUNET_MessageHeader *message)
1067 struct GNUNET_MESH_PeerControl *peer_msg;
1069 struct MESH_tunnel *t;
1071 struct Path *aux_path;
1073 GNUNET_PEER_Id peer_id;
1074 struct PeerInfo *peer_info;
1075 struct PeerInfo *aux_peer_info;
1077 /* Sanity check for client registration */
1078 if(NULL == (c = client_retrieve(client))) {
1080 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1083 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1084 /* Sanity check for message size */
1085 if(sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
1087 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1091 /* Tunnel exists? */
1092 tid = ntohl(peer_msg->tunnel_id);
1093 if(NULL == (t = c->tunnels_head)) {
1095 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1102 if(t == c->tunnels_tail) {
1104 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1110 /* Does client own tunnel? */
1111 if(t->client->handle != client) {
1113 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1117 /* Ok, delete peer from tunnel */
1118 peer_id = GNUNET_PEER_intern(&peer_msg->peer);
1123 if(p->peers[p->length-1] == peer_id) { /* one path per destination */
1124 GNUNET_CONTAINER_DLL_remove(t->paths_head, t->paths_tail, p);
1125 GNUNET_PEER_decrement_rcs(p->peers, p->length);
1128 GNUNET_free(aux_path);
1132 if(p == t->paths_head) {
1137 /*Delete peer info */
1138 peer_info = t->peers_head;
1139 while(peer_info != NULL) {
1140 if(peer_info->id == peer_id) {
1141 GNUNET_CONTAINER_DLL_remove(t->peers_head,
1144 aux_peer_info = peer_info;
1145 peer_info = peer_info->next;
1146 GNUNET_free(aux_peer_info);
1148 peer_info = peer_info->next;
1150 if(peer_info == t->peers_head) {
1155 GNUNET_PEER_change_rc(peer_id, -1);
1157 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1163 * Handler for connection requests to new peers by type
1165 * @param cls closure
1166 * @param client identification of the client
1167 * @param message the actual message (ConnectPeerByType)
1170 handle_local_connect_by_type (void *cls,
1171 struct GNUNET_SERVER_Client *client,
1172 const struct GNUNET_MessageHeader *message)
1174 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
1176 GNUNET_MESH_ApplicationType application;
1178 struct MESH_tunnel *t;
1180 /* Sanity check for client registration */
1181 if(NULL == (c = client_retrieve(client))) {
1183 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1187 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *)message;
1188 /* Sanity check for message size */
1189 if(sizeof(struct GNUNET_MESH_PeerControl) != ntohs(connect_msg->header.size)) {
1191 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1195 /* Tunnel exists? */
1196 tid = ntohl(connect_msg->tunnel_id);
1197 if(NULL == (t = c->tunnels_head)) {
1199 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1206 if(t == c->tunnels_tail) {
1208 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1214 /* Does client own tunnel? */
1215 if(t->client->handle != client) {
1217 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1221 /* Ok, lets find a peer offering the service */
1222 application = ntohl(connect_msg->type);
1223 application++; // FIXME silence warnings
1225 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1231 * Handler for client traffic directed to one peer
1233 * @param cls closure
1234 * @param client identification of the client
1235 * @param message the actual message
1238 handle_local_network_traffic (void *cls,
1239 struct GNUNET_SERVER_Client *client,
1240 const struct GNUNET_MessageHeader *message)
1243 struct MESH_tunnel *t;
1244 struct GNUNET_MESH_Data *data_msg;
1247 /* Sanity check for client registration */
1248 if(NULL == (c = client_retrieve(client))) {
1250 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1253 data_msg = (struct GNUNET_MESH_Data *)message;
1254 /* Sanity check for message size */
1255 if(sizeof(struct GNUNET_MESH_PeerControl) != ntohs(data_msg->header.size)) {
1257 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1261 /* Tunnel exists? */
1262 tid = ntohl(data_msg->tunnel_id);
1263 if(NULL == (t = c->tunnels_head)) {
1265 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1272 if(t == c->tunnels_tail) {
1274 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1280 /* Does client own tunnel? */
1281 if(t->client->handle != client) {
1283 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1289 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1294 * Handler for client traffic directed to all peers in a tunnel
1296 * @param cls closure
1297 * @param client identification of the client
1298 * @param message the actual message
1301 handle_local_network_traffic_bcast (void *cls,
1302 struct GNUNET_SERVER_Client *client,
1303 const struct GNUNET_MessageHeader *message)
1306 struct MESH_tunnel *t;
1307 struct GNUNET_MESH_DataBroadcast *data_msg;
1310 /* Sanity check for client registration */
1311 if(NULL == (c = client_retrieve(client))) {
1313 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1316 data_msg = (struct GNUNET_MESH_DataBroadcast *)message;
1317 /* Sanity check for message size */
1318 if(sizeof(struct GNUNET_MESH_PeerControl) != ntohs(data_msg->header.size)) {
1320 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1324 /* Tunnel exists? */
1325 tid = ntohl(data_msg->tunnel_id);
1326 if(NULL == (t = c->tunnels_head)) {
1328 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1335 if(t == c->tunnels_tail) {
1337 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1343 /* Does client own tunnel? */
1344 if(t->client->handle != client) {
1346 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1352 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1357 * Functions to handle messages from clients
1359 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1360 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
1361 {&handle_local_tunnel_create, NULL,
1362 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
1363 sizeof(struct GNUNET_MESH_TunnelMessage)},
1364 {&handle_local_tunnel_destroy, NULL,
1365 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
1366 sizeof(struct GNUNET_MESH_TunnelMessage)},
1367 {&handle_local_connect_add, NULL,
1368 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD,
1369 sizeof(struct GNUNET_MESH_PeerControl)},
1370 {&handle_local_connect_del, NULL,
1371 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL,
1372 sizeof(struct GNUNET_MESH_PeerControl)},
1373 {&handle_local_connect_by_type, NULL,
1374 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE,
1375 sizeof(struct GNUNET_MESH_ConnectPeerByType)},
1376 {&handle_local_network_traffic, NULL,
1377 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
1378 {&handle_local_network_traffic_bcast, NULL,
1379 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0},
1385 * To be called on core init/fail.
1387 * @param cls service closure
1388 * @param server handle to the server for this service
1389 * @param identity the public identity of this peer
1390 * @param publicKey the public key of this peer
1393 core_init (void *cls,
1394 struct GNUNET_CORE_Handle *server,
1395 const struct GNUNET_PeerIdentity *identity,
1396 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1398 core_handle = server;
1399 myid = GNUNET_PEER_intern(identity);
1404 * Method called whenever a given peer connects.
1406 * @param cls closure
1407 * @param peer peer identity this notification is about
1408 * @param atsi performance data for the connection
1411 core_connect (void *cls,
1412 const struct GNUNET_PeerIdentity *peer,
1413 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1419 * Method called whenever a peer disconnects.
1421 * @param cls closure
1422 * @param peer peer identity this notification is about
1425 core_disconnect (void *cls,
1427 GNUNET_PeerIdentity *peer)
1432 /******************************************************************************/
1433 /************************ MAIN FUNCTIONS ****************************/
1434 /******************************************************************************/
1437 * Process mesh requests.
1439 * @param cls closure
1440 * @param server the initialized server
1441 * @param c configuration to use
1445 struct GNUNET_SERVER_Handle *server,
1446 const struct GNUNET_CONFIGURATION_Handle *c)
1449 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1450 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1451 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1452 32, /* queue size */
1453 NULL, /* Closure passed to MESH functions */
1454 &core_init, /* Call core_init once connected */
1455 &core_connect, /* Handle connects */
1456 &core_disconnect, /* remove peers on disconnects */
1457 NULL, /* Do we care about "status" updates? */
1458 NULL, /* Don't notify about all incoming messages */
1459 GNUNET_NO, /* For header only in notification */
1460 NULL, /* Don't notify about all outbound messages */
1461 GNUNET_NO, /* For header-only out notification */
1462 core_handlers); /* Register these handlers */
1464 if (core_handle == NULL) {
1468 dht_handle = GNUNET_DHT_connect(c, 100); /* FIXME ht len correct size? */
1469 if (dht_handle == NULL) {
1475 * The main function for the mesh service.
1477 * @param argc number of arguments from the command line
1478 * @param argv command line arguments
1479 * @return 0 ok, 1 on error
1482 main (int argc, char *const *argv)
1487 GNUNET_SERVICE_run (argc,
1490 GNUNET_SERVICE_OPTION_NONE,
1491 &run, NULL)) ? 0 : 1;