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 * Request sent, not yet answered.
222 * Peer connected and ready to accept data
227 * Peer connected previosly but not responding
229 MESH_PEER_RECONNECTING,
234 * Struct containing all information regarding a given peer
241 struct PeerInfo *next;
242 struct PeerInfo *prev;
250 * Tunnel this peer belongs to
252 struct MESH_tunnel *t;
255 * Is the peer reachable? Is the peer even connected?
257 enum PeerState state;
260 * When to try to establish contact again?
262 struct GNUNET_TIME_Absolute next_reconnect_attempt;
265 * Who to send the data to --- FIXME what about multiple (alternate) paths?
267 GNUNET_PEER_Id first_hop;
270 * Max data rate to this peer
275 * Handle to stop the DHT search for a path to this peer
277 struct GNUNET_DHT_GetHandle *dhtget;
281 typedef uint32_t MESH_PathID;
285 * Information regarding a path
296 * Id of the path, in case it's needed
301 * Whether the path is serving traffic in a tunnel or is a backup
306 * List of all the peers that form the path from origin to target
308 GNUNET_PEER_Id *peers;
313 * Data scheduled to transmit (to local client or remote peer)
320 struct MESH_queue *next;
321 struct MESH_queue *prev;
324 * Size of the message to transmit
329 * How old is the data?
331 struct GNUNET_TIME_Absolute timestamp;
336 struct GNUNET_MessageHeader *data;
340 struct Client; /* FWD declaration */
342 * Struct containing all information regarding a tunnel
343 * For an intermediate node the improtant info used will be:
344 * - OID \ To identify
346 * - paths[0] | To know where to send it next
347 * - metainfo: ready, speeds, accounting
348 * For an end node more fields will be needed (client-handling)
356 struct MESH_tunnel *next;
357 struct MESH_tunnel *prev;
360 * Origin ID: Node that created the tunnel
365 * Tunnel number (unique for a given oid)
370 * Minimal speed for this tunnel in kb/s
375 * Maximal speed for this tunnel in kb/s
380 * Last time the tunnel was used
382 struct GNUNET_TIME_Absolute timestamp;
385 * Peers in the tunnel, for future optimizations
387 struct PeerInfo *peers_head;
388 struct PeerInfo *peers_tail;
391 * Number of peers that are connected and potentially ready to receive data
393 unsigned int peers_ready;
396 * Number of peers that have been added to the tunnel
398 unsigned int peers_total;
401 * Paths (used and backup)
403 struct Path *paths_head;
404 struct Path *paths_tail;
407 * If this tunnel was created by a local client, what's its handle?
409 struct Client *client;
412 * Messages ready to transmit
414 struct MESH_queue *out_head;
415 struct MESH_queue *out_tail;
418 * Messages received and not processed
420 struct MESH_queue *in_head;
421 struct MESH_queue *in_tail;
426 * Struct containing information about a client of the service
437 * Tunnels that belong to this client, for convenience on disconnect
439 struct MESH_tunnel *tunnels_head;
440 struct MESH_tunnel *tunnels_tail;
443 * Handle to communicate with the client
445 struct GNUNET_SERVER_Client *handle;
448 * Messages that this client has declared interest in
450 GNUNET_MESH_ApplicationType *messages_subscribed;
451 unsigned int subscription_counter;
455 /******************************************************************************/
456 /*********************** GLOBAL VARIABLES ****************************/
457 /******************************************************************************/
462 static struct Client *clients_head;
463 static struct Client *clients_tail;
466 * Tunnels not owned by this node
468 // static struct MESH_Tunnel *tunnels_head;
469 // static struct MESH_Tunnel *tunnels_tail;
472 * Handle to communicate with core
474 static struct GNUNET_CORE_Handle *core_handle;
479 static struct GNUNET_DHT_Handle *dht_handle;
482 * Local peer own ID (memory efficient handle)
484 static GNUNET_PEER_Id myid;
486 /******************************************************************************/
487 /******************** MESH NETWORK HANDLERS **************************/
488 /******************************************************************************/
491 * Core handler for path creation
492 * struct GNUNET_CORE_MessageHandler
495 * @param message message
496 * @param peer peer identity this notification is about
497 * @param atsi performance data
498 * @return GNUNET_OK to keep the connection open,
499 * GNUNET_SYSERR to close it (signal serious error)
503 handle_mesh_path_create (void *cls,
504 const struct GNUNET_PeerIdentity *peer,
505 const struct GNUNET_MessageHeader *message,
506 const struct GNUNET_TRANSPORT_ATS_Information
510 /* Find origin & self */
511 /* Search for origin in local tunnels */
512 /* Create tunnel / add path */
513 /* Retransmit to next link in chain, if any (core_notify + callback) */
518 * Core handler for mesh network traffic
521 * @param message message
522 * @param peer peer identity this notification is about
523 * @param atsi performance data
524 * @return GNUNET_OK to keep the connection open,
525 * GNUNET_SYSERR to close it (signal serious error)
528 handle_mesh_network_traffic (void *cls,
529 const struct GNUNET_PeerIdentity *peer,
530 const struct GNUNET_MessageHeader *message,
531 const struct GNUNET_TRANSPORT_ATS_Information
534 if(GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
535 /* Retransmit to next in path of tunnel identified by message */
537 } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
538 /* Retransmit to previous in path of tunnel identified by message */
544 * Functions to handle messages from core
546 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
547 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
548 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
549 {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
555 /******************************************************************************/
556 /********************* MESH LOCAL HANDLES **************************/
557 /******************************************************************************/
560 * Check if client has registered with the service and has not disconnected
561 * @param client the client to check
562 * @return non-NULL if client exists in the global DLL
565 client_retrieve (struct GNUNET_SERVER_Client *client) {
569 if(c->handle == client) return c;
570 if(c == clients_tail)
579 * Iterator called on each result obtained for a DHT
580 * operation that expects a reply
583 * @param exp when will this value expire
584 * @param key key of the result
585 * @param get_path NULL-terminated array of pointers
586 * to the peers on reverse GET path (or NULL if not recorded)
587 * @param put_path NULL-terminated array of pointers
588 * to the peers on the PUT path (or NULL if not recorded)
589 * @param type type of the result
590 * @param size number of bytes in data
591 * @param data pointer to the result data
593 void dht_get_response_handler(void *cls,
594 struct GNUNET_TIME_Absolute exp,
595 const GNUNET_HashCode * key,
596 const struct GNUNET_PeerIdentity * const *get_path,
597 const struct GNUNET_PeerIdentity * const *put_path,
598 enum GNUNET_BLOCK_Type type,
602 struct PeerInfo *peer_info;
603 struct MESH_tunnel *t;
606 peer_info = (struct PeerInfo *)cls;
608 p = GNUNET_malloc(sizeof(struct Path));
609 GNUNET_CONTAINER_DLL_insert(t->paths_head, t->paths_tail, p);
614 * Handler for client disconnection
617 * @param client identification of the client; NULL
618 * for the last call when the server is destroyed
621 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
623 struct Client *c, *next;
624 struct MESH_tunnel *t;
628 if (c->handle == client) {
629 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
630 while (NULL != (t = c->tunnels_head)) {
631 GNUNET_CONTAINER_DLL_remove (c->tunnels_head, c->tunnels_tail, t);
632 /* TODO free paths and other tunnel dynamic structures */
635 GNUNET_free (c->messages_subscribed);
642 if(c == clients_head) return; /* Tail already processed? */
648 * Handler for new clients
651 * @param client identification of the client
652 * @param message the actual message, which includes messages the client wants
655 handle_local_new_client (void *cls,
656 struct GNUNET_SERVER_Client *client,
657 const struct GNUNET_MessageHeader *message)
660 unsigned int payload_size;
662 /* Check data sanity */
663 payload_size = message->size - sizeof(struct GNUNET_MessageHeader);
664 if (0 != payload_size % sizeof(GNUNET_MESH_ApplicationType)) {
666 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
670 /* Create new client structure */
671 c = GNUNET_malloc(sizeof(struct Client));
673 c->tunnels_head = NULL;
674 c->tunnels_tail = NULL;
675 if(payload_size != 0) {
676 c->messages_subscribed = GNUNET_malloc(payload_size);
677 memcpy(c->messages_subscribed, &message[1], payload_size);
679 c->messages_subscribed = NULL;
681 c->subscription_counter = payload_size/sizeof(GNUNET_MESH_ApplicationType);
683 /* Insert new client in DLL */
684 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
686 GNUNET_SERVER_receive_done(client, GNUNET_OK);
690 * Handler for requests of new tunnels
693 * @param client identification of the client
694 * @param message the actual message
697 handle_local_tunnel_create (void *cls,
698 struct GNUNET_SERVER_Client *client,
699 const struct GNUNET_MessageHeader *message)
701 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
702 struct MESH_tunnel *t;
705 /* Sanity check for client registration */
706 if(NULL == (c = client_retrieve(client))) {
708 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
712 /* Message sanity check */
713 if(sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
715 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
719 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
720 /* Sanity check for tunnel numbering */
721 if(0 == (ntohl(tunnel_msg->tunnel_id) & 0x80000000)) {
723 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
726 /* Sanity check for duplicate tunnel IDs */
729 if(t->tid == ntohl(tunnel_msg->tunnel_id)) {
731 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
734 if(t == c->tunnels_tail) break;
737 /* FIXME: calloc? Is NULL != 0 on any platform? */
738 t = GNUNET_malloc(sizeof(struct MESH_tunnel));
739 t->tid = ntohl(tunnel_msg->tunnel_id);
743 t->peers_head = NULL;
744 t->peers_tail = NULL;
745 t->paths_head = NULL;
746 t->paths_tail = NULL;
753 GNUNET_CONTAINER_DLL_insert(c->tunnels_head, c->tunnels_tail, t);
755 GNUNET_SERVER_receive_done(client, GNUNET_OK);
760 * Handler for requests of deleting tunnels
763 * @param client identification of the client
764 * @param message the actual message
767 handle_local_tunnel_destroy (void *cls,
768 struct GNUNET_SERVER_Client *client,
769 const struct GNUNET_MessageHeader *message)
771 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
773 struct MESH_tunnel *t;
777 /* Sanity check for client registration */
778 if(NULL == (c = client_retrieve(client))) {
780 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
783 /* Message sanity check */
784 if(sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
786 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
790 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
793 tid = ntohl(tunnel_msg->tunnel_id);
794 if(NULL == (t = c->tunnels_head)) {
796 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
803 if(t == c->tunnels_tail) {
805 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
811 GNUNET_CONTAINER_DLL_remove(c->tunnels_head, c->tunnels_tail, t);
813 for(pi = t->peers_head; pi != NULL; pi = t->peers_head) {
814 GNUNET_PEER_change_rc(pi->id, -1);
815 GNUNET_CONTAINER_DLL_remove(t->peers_head, t->peers_tail, pi);
820 GNUNET_SERVER_receive_done(client, GNUNET_OK);
825 * Handler for connection requests to new peers
828 * @param client identification of the client
829 * @param message the actual message (PeerControl)
832 handle_local_connect_add (void *cls,
833 struct GNUNET_SERVER_Client *client,
834 const struct GNUNET_MessageHeader *message)
836 struct GNUNET_MESH_PeerControl *peer_msg;
838 struct MESH_tunnel *t;
840 struct PeerInfo *peer_info;
845 /* Sanity check for client registration */
846 if(NULL == (c = client_retrieve(client))) {
848 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
852 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
853 /* Sanity check for message size */
854 if(sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
856 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
861 tid = ntohl(peer_msg->tunnel_id);
862 if(NULL == (t = c->tunnels_head)) {
864 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
871 if(t == c->tunnels_tail) {
873 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
879 /* Does client own tunnel? */
880 if(t->client->handle != client) {
882 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
886 /* Ok, add peer to tunnel */
887 peer_info = (struct PeerInfo *) GNUNET_malloc(sizeof(struct PeerInfo));
888 peer_info->id = GNUNET_PEER_intern(&peer_msg->peer);
889 peer_info->state = MESH_PEER_WAITING;
892 GNUNET_CONTAINER_DLL_insert(t->peers_head, t->peers_tail, peer_info);
893 /* start dht search */
894 // FIXME key = hash (peerid + salt);
895 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
896 GNUNET_TIME_relative_get_forever(),
897 GNUNET_BLOCK_TYPE_ANY,
899 4, /* replication level */
900 GNUNET_DHT_RO_RECORD_ROUTE,
901 NULL, /* bloom filter */
905 dht_get_response_handler,
908 GNUNET_SERVER_receive_done(client, GNUNET_OK);
914 * Handler for disconnection requests of peers in a tunnel
917 * @param client identification of the client
918 * @param message the actual message (PeerControl)
921 handle_local_connect_del (void *cls,
922 struct GNUNET_SERVER_Client *client,
923 const struct GNUNET_MessageHeader *message)
925 struct GNUNET_MESH_PeerControl *peer_msg;
927 struct MESH_tunnel *t;
929 struct Path *aux_path;
931 GNUNET_PEER_Id peer_id;
932 struct PeerInfo *peer_info;
933 struct PeerInfo *aux_peer_info;
935 /* Sanity check for client registration */
936 if(NULL == (c = client_retrieve(client))) {
938 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
941 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
942 /* Sanity check for message size */
943 if(sizeof(struct GNUNET_MESH_PeerControl) != ntohs(peer_msg->header.size)) {
945 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
950 tid = ntohl(peer_msg->tunnel_id);
951 if(NULL == (t = c->tunnels_head)) {
953 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
960 if(t == c->tunnels_tail) {
962 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
968 /* Does client own tunnel? */
969 if(t->client->handle != client) {
971 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
975 /* Ok, delete peer from tunnel */
976 peer_id = GNUNET_PEER_intern(&peer_msg->peer);
981 if(p->peers[p->length-1] == peer_id) { /* one path per destination */
982 GNUNET_CONTAINER_DLL_remove(t->paths_head, t->paths_tail, p);
983 GNUNET_PEER_decrement_rcs(p->peers, p->length);
986 GNUNET_free(aux_path);
990 if(p == t->paths_head) {
995 /*Delete peer info */
996 peer_info = t->peers_head;
997 while(peer_info != NULL) {
998 if(peer_info->id == peer_id) {
999 GNUNET_CONTAINER_DLL_remove(t->peers_head,
1002 aux_peer_info = peer_info;
1003 peer_info = peer_info->next;
1004 GNUNET_free(aux_peer_info);
1006 peer_info = peer_info->next;
1008 if(peer_info == t->peers_head) {
1013 GNUNET_PEER_change_rc(peer_id, -1);
1015 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1021 * Handler for connection requests to new peers by type
1023 * @param cls closure
1024 * @param client identification of the client
1025 * @param message the actual message (ConnectPeerByType)
1028 handle_local_connect_by_type (void *cls,
1029 struct GNUNET_SERVER_Client *client,
1030 const struct GNUNET_MessageHeader *message)
1032 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
1034 GNUNET_MESH_ApplicationType application;
1036 struct MESH_tunnel *t;
1038 /* Sanity check for client registration */
1039 if(NULL == (c = client_retrieve(client))) {
1041 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1045 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *)message;
1046 /* Sanity check for message size */
1047 if(sizeof(struct GNUNET_MESH_PeerControl) != ntohs(connect_msg->header.size)) {
1049 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1053 /* Tunnel exists? */
1054 tid = ntohl(connect_msg->tunnel_id);
1055 if(NULL == (t = c->tunnels_head)) {
1057 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1064 if(t == c->tunnels_tail) {
1066 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1072 /* Does client own tunnel? */
1073 if(t->client->handle != client) {
1075 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1079 /* Ok, lets find a peer offering the service */
1080 application = ntohl(connect_msg->type);
1081 application++; // FIXME silence warnings
1083 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1089 * Handler for client traffic directed to one peer
1091 * @param cls closure
1092 * @param client identification of the client
1093 * @param message the actual message
1096 handle_local_network_traffic (void *cls,
1097 struct GNUNET_SERVER_Client *client,
1098 const struct GNUNET_MessageHeader *message)
1101 struct MESH_tunnel *t;
1102 struct GNUNET_MESH_Data *data_msg;
1105 /* Sanity check for client registration */
1106 if(NULL == (c = client_retrieve(client))) {
1108 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1111 data_msg = (struct GNUNET_MESH_Data *)message;
1112 /* Sanity check for message size */
1113 if(sizeof(struct GNUNET_MESH_PeerControl) != ntohs(data_msg->header.size)) {
1115 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1119 /* Tunnel exists? */
1120 tid = ntohl(data_msg->tunnel_id);
1121 if(NULL == (t = c->tunnels_head)) {
1123 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1130 if(t == c->tunnels_tail) {
1132 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1138 /* Does client own tunnel? */
1139 if(t->client->handle != client) {
1141 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1147 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1152 * Handler for client traffic directed to all peers in a tunnel
1154 * @param cls closure
1155 * @param client identification of the client
1156 * @param message the actual message
1159 handle_local_network_traffic_bcast (void *cls,
1160 struct GNUNET_SERVER_Client *client,
1161 const struct GNUNET_MessageHeader *message)
1164 struct MESH_tunnel *t;
1165 struct GNUNET_MESH_DataBroadcast *data_msg;
1168 /* Sanity check for client registration */
1169 if(NULL == (c = client_retrieve(client))) {
1171 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1174 data_msg = (struct GNUNET_MESH_DataBroadcast *)message;
1175 /* Sanity check for message size */
1176 if(sizeof(struct GNUNET_MESH_PeerControl) != ntohs(data_msg->header.size)) {
1178 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1182 /* Tunnel exists? */
1183 tid = ntohl(data_msg->tunnel_id);
1184 if(NULL == (t = c->tunnels_head)) {
1186 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1193 if(t == c->tunnels_tail) {
1195 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1201 /* Does client own tunnel? */
1202 if(t->client->handle != client) {
1204 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1210 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1215 * Functions to handle messages from clients
1217 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1218 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
1219 {&handle_local_tunnel_create, NULL,
1220 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE, 0},
1221 {&handle_local_tunnel_destroy, NULL,
1222 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY, 0},
1223 {&handle_local_connect_add, NULL,
1224 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD, 0},
1225 {&handle_local_connect_del, NULL,
1226 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL, 0},
1227 {&handle_local_connect_by_type, NULL,
1228 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE,
1229 sizeof(struct GNUNET_MESH_ConnectPeerByType)},
1230 {&handle_local_network_traffic, NULL,
1231 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
1232 {&handle_local_network_traffic_bcast, NULL,
1233 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0},
1239 * To be called on core init/fail.
1241 * @param cls service closure
1242 * @param server handle to the server for this service
1243 * @param identity the public identity of this peer
1244 * @param publicKey the public key of this peer
1247 core_init (void *cls,
1248 struct GNUNET_CORE_Handle *server,
1249 const struct GNUNET_PeerIdentity *identity,
1250 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1252 core_handle = server;
1253 myid = GNUNET_PEER_intern(identity);
1258 * Method called whenever a given peer connects.
1260 * @param cls closure
1261 * @param peer peer identity this notification is about
1262 * @param atsi performance data for the connection
1265 core_connect (void *cls,
1266 const struct GNUNET_PeerIdentity *peer,
1267 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1273 * Method called whenever a peer disconnects.
1275 * @param cls closure
1276 * @param peer peer identity this notification is about
1279 core_disconnect (void *cls,
1281 GNUNET_PeerIdentity *peer)
1286 /******************************************************************************/
1287 /************************ MAIN FUNCTIONS ****************************/
1288 /******************************************************************************/
1291 * Process mesh requests. FIXME NON FUNCTIONAL, SKELETON
1293 * @param cls closure
1294 * @param server the initialized server
1295 * @param c configuration to use
1299 struct GNUNET_SERVER_Handle *server,
1300 const struct GNUNET_CONFIGURATION_Handle *c)
1303 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1304 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1305 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1306 32, /* queue size */
1307 NULL, /* Closure passed to MESH functions */
1308 &core_init, /* Call core_init once connected */
1309 &core_connect, /* Handle connects */
1310 &core_disconnect, /* remove peers on disconnects */
1311 NULL, /* Do we care about "status" updates? */
1312 NULL, /* Don't notify about all incoming messages */
1313 GNUNET_NO, /* For header only in notification */
1314 NULL, /* Don't notify about all outbound messages */
1315 GNUNET_NO, /* For header-only out notification */
1316 core_handlers); /* Register these handlers */
1318 if (core_handle == NULL) {
1322 dht_handle = GNUNET_DHT_connect(c, 100); /* FIXME ht len correct size? */
1323 if (dht_handle == NULL) {
1329 * The main function for the mesh service.
1331 * @param argc number of arguments from the command line
1332 * @param argv command line arguments
1333 * @return 0 ok, 1 on error
1336 main (int argc, char *const *argv)
1341 GNUNET_SERVICE_run (argc,
1344 GNUNET_SERVICE_OPTION_NONE,
1345 &run, NULL)) ? 0 : 1;