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 HANDLER HELPERS
30 * - MESH NETWORK HANDLES
31 * - MESH LOCAL HANDLER HELPERS
32 * - MESH LOCAL HANDLES
33 * - PERIODIC FUNCTIONS
34 * - MAIN FUNCTIONS (main & run)
37 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
38 * - partial disconnect reporting -- same as error reporting?
39 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
40 * - speed requirement specification (change?) in mesh API -- API call
41 * - add ping message (connection confirmation, others?)
45 #include "gnunet_common.h"
46 #include "gnunet_util_lib.h"
47 #include "gnunet_peer_lib.h"
48 #include "gnunet_core_service.h"
49 #include "gnunet_protocols.h"
52 #include "mesh_protocol.h"
53 #include "gnunet_dht_service.h"
55 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
56 GNUNET_TIME_UNIT_SECONDS,\
60 /******************************************************************************/
61 /************************ DATA STRUCTURES ****************************/
62 /******************************************************************************/
65 * Information regarding a path
73 struct MeshPath *next;
74 struct MeshPath *prev;
77 * Whether the path is serving traffic in a tunnel or is a backup
82 * List of all the peers that form the path from origin to target
84 GNUNET_PEER_Id *peers;
87 * Number of peers (hops) in the path
94 * All the states a peer participating in a tunnel can be in.
99 * Path to the peer not known yet
104 * Request sent, not yet answered.
109 * Peer connected and ready to accept data
114 * Peer connected previosly but not responding
116 MESH_PEER_RECONNECTING
121 * Struct containing all information regarding a given peer
131 * Is the peer reachable? Is the peer even connected?
133 enum MeshPeerState state;
136 * Last time we heard from this peer
138 struct GNUNET_TIME_Absolute last_contact;
141 * Number of attempts to reconnect so far
143 int n_reconnect_attempts;
146 * Paths to reach the peer
148 struct MeshPath *path;
149 struct MeshPath *path_tail;
152 * Handle to stop the DHT search for a path to this peer
154 struct GNUNET_DHT_GetHandle *dhtget;
159 * Data scheduled to transmit (to local client or remote peer)
166 struct MeshQueue *next;
167 struct MeshQueue *prev;
170 * Target of the data (NULL if target is client)
172 struct MeshPeerInfo *peer;
175 * Client to send the data to (NULL if target is peer)
177 struct MeshClient *client;
180 * Size of the message to transmit
185 * How old is the data?
187 struct GNUNET_TIME_Absolute timestamp;
192 struct GNUNET_MessageHeader *data;
196 * Globally unique tunnel identification (owner + number)
197 * DO NOT USE OVER THE NETWORK
199 struct MESH_TunnelID {
201 * Node that owns the tunnel
206 * Tunnel number to differentiate all the tunnels owned by the node oid
207 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_MARK )
209 MESH_TunnelNumber tid;
213 struct MeshClient; /* FWD declaration */
215 * Struct containing all information regarding a tunnel
216 * For an intermediate node the improtant info used will be:
217 * - id Tunnel unique identification
218 * - paths[0] To know where to send it next
219 * - metainfo: ready, speeds, accounting
226 struct MESH_TunnelID id;
229 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK or 0 )
231 MESH_TunnelNumber local_tid;
234 * Last time the tunnel was used
236 struct GNUNET_TIME_Absolute timestamp;
239 * Peers in the tunnelindexed by PeerIdentity (MeshPeerInfo)
241 struct GNUNET_CONTAINER_MultiHashMap* peers;
244 * Number of peers that are connected and potentially ready to receive data
246 unsigned int peers_ready;
249 * Number of peers that have been added to the tunnel
251 unsigned int peers_total;
255 * Client owner of the tunnel, if any
257 struct MeshClient *client;
260 * Messages ready to transmit
262 struct MeshQueue *queue_head;
263 struct MeshQueue *queue_tail;
268 * Struct containing information about a client of the service
275 struct MeshClient *next;
276 struct MeshClient *prev;
279 * Tunnels that belong to this client, indexed by local id
281 struct GNUNET_CONTAINER_MultiHashMap* tunnels;
284 * Handle to communicate with the client
286 struct GNUNET_SERVER_Client *handle;
289 * Applications that this client has claimed to provide
291 GNUNET_MESH_ApplicationType *apps;
292 unsigned int app_counter;
295 * Messages that this client has declared interest in
298 unsigned int type_counter;
302 /******************************************************************************/
303 /*********************** GLOBAL VARIABLES ****************************/
304 /******************************************************************************/
309 static struct MeshClient *clients;
310 static struct MeshClient *clients_tail;
313 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
315 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
318 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
320 struct GNUNET_CONTAINER_MultiHashMap *peers;
323 * Handle to communicate with core
325 static struct GNUNET_CORE_Handle *core_handle;
330 static struct GNUNET_DHT_Handle *dht_handle;
333 * Local peer own ID (memory efficient handle)
335 static GNUNET_PEER_Id myid;
338 * Tunnel ID for the next created tunnel (global tunnel number)
340 static MESH_TunnelNumber next_tid;
342 /******************************************************************************/
343 /****************** GENERAL HELPER FUNCTIONS ************************/
344 /******************************************************************************/
347 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
348 * and inster it in the appropiate structures if the peer is not known yet.
349 * @param peer Identity of the peer
350 * @return Existing or newly created peer info
352 static struct MeshPeerInfo *
353 get_peer_info (const struct GNUNET_PeerIdentity *peer)
355 struct MeshPeerInfo * peer_info;
357 peer_info = GNUNET_CONTAINER_multihashmap_get(peers,
359 if (NULL == peer_info) {
360 peer_info = (struct MeshPeerInfo *)
361 GNUNET_malloc(sizeof(struct MeshPeerInfo));
362 GNUNET_CONTAINER_multihashmap_put(peers,
365 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
366 peer_info->id = GNUNET_PEER_intern(peer);
367 peer_info->state = MESH_PEER_SEARCHING;
374 * Find the first peer whom to send a packet to go down this path
375 * @param path The path to use
376 * @return short id of the next peer, myid in case of local delivery,
377 * or 0 in case of error
379 static GNUNET_PEER_Id
380 get_first_hop (struct MeshPath *path)
384 while (NULL != path) {
385 if (path->in_use) break;
389 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
390 "tried to get the next hop from an invalid path\n");
394 for (i = 0; i < path->length; i++) {
395 if (path->peers[i] == myid) {
396 if (i < path->length - 1) {
397 return path->peers[i+1];
408 * Get the cost of the path.
409 * @param path The path to analyze
410 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
414 get_path_cost(struct MeshPath *path)
418 if (NULL == path) return UINT_MAX;
419 for (i = 0; i < path->length; i++) {
420 if (path->peers[i] == myid) {
421 return path->length - i;
429 * Add the path to the peer and update the path used to reach it in case this
431 * @param peer_info Destination peer to add the path to.
432 * @param path New path to add. Last peer must be the peer in arg 1.
435 add_path_to_peer(struct MeshPeerInfo *peer_info, struct MeshPath *path)
438 unsigned int new_cost;
439 unsigned int best_cost;
440 struct MeshPath *aux;
441 struct MeshPath *best;
443 if (NULL == peer_info || NULL == path) return;
445 new_cost = get_path_cost(path);
446 best_cost = UINT_MAX;
448 for (aux = peer_info->path; aux != NULL; aux = aux->next) {
449 if ((i = get_path_cost(aux)) < best_cost) {
454 if (best_cost < new_cost) {
456 GNUNET_CONTAINER_DLL_insert_tail(peer_info->path,
457 peer_info->path_tail,
460 if (NULL != best) best->in_use = 0;
462 GNUNET_CONTAINER_DLL_insert(peer_info->path,
463 peer_info->path_tail,
471 * Add the path to the peer and update the path used to reach it in case this
472 * is the shortest. The path is given in reverse, the destination peer is
473 * path[0]. The function modifies the path, inverting it to use the origin as
475 * @param peer_info Destination peer to add the path to.
476 * @param path New path to add. First peer must be the peer in arg 1.
479 add_path_to_origin(struct MeshPeerInfo *peer_info, struct MeshPath *path)
484 for (i = 0; i < path->length/2; i++) {
485 aux = path->peers[i];
486 path->peers[i] = path->peers[path->length - i - 1];
487 path->peers[path->length - i - 1] = aux;
489 add_path_to_peer(peer_info, path);
494 * Check if client has registered with the service and has not disconnected
495 * @param client the client to check
496 * @return non-NULL if client exists in the global DLL
498 static struct MeshClient *
499 retrieve_client (struct GNUNET_SERVER_Client *client)
501 struct MeshClient *c;
505 if (c->handle == client) return c;
513 * Search for a tunnel among the tunnels for a client
514 * @param client the client whose tunnels to search in
515 * @param tid the local id of the tunnel
516 * @return tunnel handler, NULL if doesn't exist
518 static struct MeshTunnel *
519 retrieve_tunnel_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
521 GNUNET_HashCode hash;
523 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
524 return GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
528 * Search for a tunnel by global ID using PEER_ID
529 * @param pi owner of the tunnel
530 * @param tid global tunnel number
531 * @return tunnel handler, NULL if doesn't exist
533 static struct MeshTunnel *
534 retrieve_tunnel_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
536 struct MESH_TunnelID id;
537 GNUNET_HashCode hash;
542 GNUNET_CRYPTO_hash(&id, sizeof(struct MESH_TunnelID), &hash);
543 return GNUNET_CONTAINER_multihashmap_get(tunnels, &hash);
549 * Search for a tunnel by global ID using full PeerIdentities
550 * @param oid owner of the tunnel
551 * @param tid global tunnel number
552 * @return tunnel handler, NULL if doesn't exist
554 static struct MeshTunnel *
555 retrieve_tunnel (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
557 return retrieve_tunnel_by_pi(GNUNET_PEER_search(oid), tid);
562 * Destroy the path and free any allocated resources linked to it
563 * @param t tunnel the path belongs to
564 * @param p the path to destroy
565 * @return GNUNET_OK on success
568 destroy_path(struct MeshPath *p)
570 GNUNET_PEER_decrement_rcs(p->peers, p->length);
571 GNUNET_free(p->peers);
578 * Destroy the peer_info and free any allocated resources linked to it
579 * @param t tunnel the path belongs to
580 * @param pi the peer_info to destroy
581 * @return GNUNET_OK on success
584 destroy_peer_info(struct MeshPeerInfo *pi)
586 GNUNET_HashCode hash;
587 struct GNUNET_PeerIdentity id;
589 GNUNET_PEER_resolve(pi->id, &id);
590 GNUNET_PEER_change_rc(pi->id, -1);
591 GNUNET_CRYPTO_hash(&id, sizeof(struct GNUNET_PeerIdentity), &hash);
593 GNUNET_CONTAINER_multihashmap_remove(peers, &hash, pi);
601 * Destroy the tunnel and free any allocated resources linked to it
602 * @param c client the tunnel belongs to
603 * @param t the tunnel to destroy
604 * @return GNUNET_OK on success
607 destroy_tunnel(struct MeshTunnel *t)
609 struct MeshClient *c;
610 GNUNET_HashCode hash;
613 if (NULL == t) return GNUNET_OK;
617 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
618 if(GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t)) {
622 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
624 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t))
632 /******************************************************************************/
633 /**************** MESH NETWORK HANDLER HELPERS ***********************/
634 /******************************************************************************/
637 * Function called to notify a client about the socket
638 * being ready to queue more data. "buf" will be
639 * NULL and "size" zero if the socket was closed for
640 * writing in the meantime.
643 * @param size number of bytes available in buf
644 * @param buf where the callee should write the message
645 * @return number of bytes written to buf
648 send_core_create_path_for_peer (void *cls, size_t size, void *buf)
650 struct MeshPeerInfo *peer_info = cls;
651 struct GNUNET_MESH_ManipulatePath *msg;
653 struct GNUNET_PeerIdentity *peer_ptr;
654 struct GNUNET_PeerIdentity id;
658 if (0 == size && NULL == buf) {
659 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Retransmitting create path\n");
660 GNUNET_PEER_resolve(get_first_hop(peer_info->path), &id);
661 GNUNET_CORE_notify_transmit_ready(core_handle,
664 GNUNET_TIME_UNIT_FOREVER_REL,
666 sizeof(struct GNUNET_MESH_ManipulatePath)
667 + (peer_info->path->length
668 * sizeof (struct GNUNET_PeerIdentity)),
669 &send_core_create_path_for_peer,
680 if (p == NULL) return 0; // TODO Notify ERROR Path not found
682 size_needed = sizeof(struct GNUNET_MESH_ManipulatePath)
683 + p->length * sizeof(struct GNUNET_PeerIdentity);
684 if (size < size_needed) {
685 // TODO retry? cancel?
689 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
690 msg->header.size = htons(size_needed);
691 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
693 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
694 for (i = 0; i < p->length; i++) {
695 GNUNET_PEER_resolve(p->peers[i], peer_ptr++);
698 peer_info->state = MESH_PEER_WAITING;
705 * There are several options to send a "data to origin" or similar packet.
706 * The core callback function needs to know at least: ID of tunnel and the
707 * data itself, so one parameter (cls) is not enough.
708 * 1. Build the message inside the original funtction, call core_ntfy_trnsmt_rdy
709 * passing the created message as cls
710 * - # memcpy: 2 (function X: to message struct, callback: from cls to buf)
711 * - Very messy, original function becomes huge and ugly
712 * (see "handle_mesh_path_create" for example)
713 * 2. Create a helper function to build the packet, then call
714 * core_ntfy_trnsmt_rdy with message as cls.
715 * - # memcpy: 2 (in helper function data->msg and in callback cls->buf)
716 * 3- Define new container, pass container with pointers
717 * - # memcpy = 1 (in callback, cls->buf)
718 * - Noise: extra containers defined per type of message
720 struct info_for_data_to_origin
722 struct MESH_TunnelID *origin;
728 * Function called to notify a client about the socket
729 * being ready to queue more data. "buf" will be
730 * NULL and "size" zero if the socket was closed for
731 * writing in the meantime.
733 * @param cls closure (data itself)
734 * @param size number of bytes available in buf
735 * @param buf where the callee should write the message
736 * @return number of bytes written to buf
739 send_core_data_to_origin (void *cls, size_t size, void *buf)
741 struct info_for_data_to_origin *info = cls;
742 struct GNUNET_MESH_DataMessageToOrigin *msg = buf;
745 GNUNET_assert(NULL != info);
746 total_size = sizeof(struct GNUNET_MESH_DataMessageToOrigin) + info->size;
747 GNUNET_assert(total_size < (uint16_t) -1); /* FIXME */
749 if (total_size > size) {
750 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
751 "not enough buffer to send data to origin\n");
754 msg->header.size = htons(total_size);
755 msg->header.type = htons(GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
756 GNUNET_PEER_resolve(info->origin->oid, &msg->oid);
757 msg->tid = htonl(info->origin->tid);
758 if (0 != info->size && NULL != info->data) {
759 memcpy(&msg[1], info->data, info->size);
768 * Function called to notify a client about the socket
769 * being ready to queue more data. "buf" will be
770 * NULL and "size" zero if the socket was closed for
771 * writing in the meantime.
773 * @param cls closure (data itself)
774 * @param size number of bytes available in buf
775 * @param buf where the callee should write the message
776 * @return number of bytes written to buf
779 send_core_data_to_peer (void *cls, size_t size, void *buf)
781 struct info_for_data_to_origin *info = cls;
782 struct GNUNET_MESH_DataMessageToOrigin *msg = buf;
785 GNUNET_assert(NULL != info);
786 total_size = sizeof(struct GNUNET_MESH_DataMessageToOrigin) + info->size;
787 /* FIXME better constant? short >= 16 bits, not == 16 bits... */
788 GNUNET_assert(total_size < USHRT_MAX);
790 if (total_size > size) {
791 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
792 "not enough buffer to send data to origin\n");
795 msg->header.size = htons(total_size);
796 msg->header.type = htons(GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
797 GNUNET_PEER_resolve(info->origin->oid, &msg->oid);
798 msg->tid = htonl(info->origin->tid);
799 if (0 != info->size && NULL != info->data) {
800 memcpy(&msg[1], info->data, info->size);
808 * Send another peer a notification to destroy a tunnel
809 * @param cls The tunnel to destroy
810 * @param size Size in the buffer
811 * @param buf Memory where to put the data to transmit
812 * @return Size of data put in buffer
815 send_p2p_tunnel_destroy(void *cls, size_t size, void *buf)
817 struct MeshTunnel *t = cls;
818 struct MeshClient *c;
819 struct GNUNET_MESH_TunnelMessage *msg;
823 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY); /*FIXME*/
824 msg->header.size = htons(sizeof(struct GNUNET_MESH_TunnelMessage));
825 msg->tunnel_id = htonl(t->id.tid);
827 destroy_tunnel(c, t);
828 return sizeof(struct GNUNET_MESH_TunnelMessage);
833 /******************************************************************************/
834 /******************** MESH NETWORK HANDLERS **************************/
835 /******************************************************************************/
839 * Core handler for path creation
840 * struct GNUNET_CORE_MessageHandler
843 * @param message message
844 * @param peer peer identity this notification is about
845 * @param atsi performance data
846 * @return GNUNET_OK to keep the connection open,
847 * GNUNET_SYSERR to close it (signal serious error)
851 handle_mesh_path_create (void *cls,
852 const struct GNUNET_PeerIdentity *peer,
853 const struct GNUNET_MessageHeader *message,
854 const struct GNUNET_TRANSPORT_ATS_Information
857 unsigned int own_pos;
860 MESH_TunnelNumber tid;
861 struct GNUNET_MESH_ManipulatePath *msg;
862 struct GNUNET_PeerIdentity *pi;
863 struct GNUNET_PeerIdentity id;
864 GNUNET_HashCode hash;
865 struct MeshPath *path;
866 struct MeshPeerInfo *dest_peer_info;
867 struct MeshPeerInfo *orig_peer_info;
868 struct MeshTunnel *t;
871 size = ntohs(message->size);
872 if (size < sizeof(struct GNUNET_MESH_ManipulatePath)) {
873 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
874 "received create path message too short\n");
878 size -= sizeof(struct GNUNET_MESH_ManipulatePath);
879 if (size < 2 * sizeof(struct GNUNET_PeerIdentity)) {
880 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
881 "create path message lacks enough peers\n");
884 if (size % sizeof(struct GNUNET_PeerIdentity)) {
885 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
886 "create path message of wrong size\n");
889 msg = (struct GNUNET_MESH_ManipulatePath *) message;
890 size /= sizeof(struct GNUNET_PeerIdentity);
892 tid = ntohl(msg->tid);
893 pi = (struct GNUNET_PeerIdentity *) &msg[1];
894 t = retrieve_tunnel(pi, tid);
897 t = GNUNET_malloc(sizeof(struct MeshTunnel));
898 t->id.oid = GNUNET_PEER_intern(pi);
902 t->peers = GNUNET_CONTAINER_multihashmap_create(32);
904 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
906 GNUNET_CONTAINER_multihashmap_put(tunnels,
909 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
911 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
912 "create path: could not store tunnel in hashmap\n");
917 dest_peer_info = GNUNET_CONTAINER_multihashmap_get(peers,
918 &pi[size - 1].hashPubKey);
919 if (NULL == dest_peer_info) {
920 dest_peer_info = GNUNET_malloc(sizeof(struct MeshPeerInfo));
921 dest_peer_info->id = GNUNET_PEER_intern(&pi[size - 1]);
922 dest_peer_info->state = MESH_PEER_WAITING;
923 GNUNET_CONTAINER_multihashmap_put(peers,
924 &pi[size - 1].hashPubKey,
926 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
928 orig_peer_info = GNUNET_CONTAINER_multihashmap_get(peers, &pi->hashPubKey);
929 if (NULL == orig_peer_info) {
930 orig_peer_info = GNUNET_malloc(sizeof(struct MeshPeerInfo));
931 orig_peer_info->id = GNUNET_PEER_intern(pi);
932 orig_peer_info->state = MESH_PEER_WAITING;
933 GNUNET_CONTAINER_multihashmap_put(peers,
936 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
940 path = GNUNET_malloc(sizeof(struct MeshPath));
942 path->peers = GNUNET_malloc(size * sizeof(GNUNET_PEER_Id));
944 for (i = 0; i < size; i++) {
945 path->peers[i] = GNUNET_PEER_intern(&pi[i]);
946 if (path->peers[i] == myid) own_pos = i;
948 if (own_pos == 0) { /* cannot be self, must be 'not found' */
949 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
950 "create path: self not found in path through self\n");
952 /* FIXME error. destroy tunnel? leave for timeout? */
955 if (own_pos == size - 1) { /* it is for us! */
956 add_path_to_origin(orig_peer_info, path); /* inverts path! */
957 GNUNET_PEER_resolve(get_first_hop(path), &id); /* path is inverted :) */
959 * is it allowed/desired to declare variables this way?
960 * (style, best bractices, etc)
961 * This variable is short lived and completely irrelevant for the rest
964 struct info_for_data_to_origin *info =
965 GNUNET_malloc(sizeof(struct info_for_data_to_origin));
966 info->origin = &t->id;
969 GNUNET_CORE_notify_transmit_ready(core_handle,
972 GNUNET_TIME_UNIT_FOREVER_REL,
974 sizeof(struct GNUNET_MessageHeader),
975 &send_core_data_to_origin,
978 add_path_to_peer(dest_peer_info, path);
979 GNUNET_PEER_resolve(get_first_hop(path), &id);
980 GNUNET_CORE_notify_transmit_ready(core_handle,
983 GNUNET_TIME_UNIT_FOREVER_REL,
985 sizeof(struct GNUNET_MessageHeader),
986 &send_core_create_path_for_peer,
994 * Core handler for mesh network traffic going from the origin to a peer
997 * @param message message
998 * @param peer peer identity this notification is about
999 * @param atsi performance data
1000 * @return GNUNET_OK to keep the connection open,
1001 * GNUNET_SYSERR to close it (signal serious error)
1004 handle_mesh_data_unicast (void *cls,
1005 const struct GNUNET_PeerIdentity *peer,
1006 const struct GNUNET_MessageHeader *message,
1007 const struct GNUNET_TRANSPORT_ATS_Information
1010 struct GNUNET_MESH_DataMessageFromOrigin *msg;
1011 struct GNUNET_PeerIdentity id;
1012 struct MeshTunnel *t;
1013 struct MeshPeerInfo *pi;
1016 size = ntohs(message->size);
1017 if (size < sizeof(struct GNUNET_MESH_DataMessageFromOrigin)) {
1018 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1019 "got data from origin packet: too short\n");
1020 return GNUNET_OK; // FIXME maybe SYSERR? peer misbehaving?
1022 msg = (struct GNUNET_MESH_DataMessageFromOrigin *) message;
1023 t = retrieve_tunnel(&msg->oid, ntohl(msg->tid));
1024 pi = GNUNET_CONTAINER_multihashmap_get(t->peers,
1025 &msg->destination.hashPubKey);
1027 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1028 "got invalid data from origin packet: wrong destination\n");
1029 /* TODO are we so nice to try to deliver it anyway? maybe we missed
1030 * a Create_path packed that added the peer but we have it in the global
1031 * peer pool anyway...
1033 return GNUNET_OK; // FIXME maybe SYSERR? peer misbehaving?
1035 GNUNET_PEER_resolve(get_first_hop(pi->path), &id);
1036 GNUNET_CORE_notify_transmit_ready(core_handle,
1039 GNUNET_TIME_UNIT_FOREVER_REL,
1042 &send_core_data_to_origin,
1049 * Core handler for mesh network traffic going from the origin to all peers
1051 * @param cls closure
1052 * @param message message
1053 * @param peer peer identity this notification is about
1054 * @param atsi performance data
1055 * @return GNUNET_OK to keep the connection open,
1056 * GNUNET_SYSERR to close it (signal serious error)
1059 handle_mesh_data_multicast (void *cls,
1060 const struct GNUNET_PeerIdentity *peer,
1061 const struct GNUNET_MessageHeader *message,
1062 const struct GNUNET_TRANSPORT_ATS_Information
1065 // struct GNUNET_MESH_DataMessageMulticast *msg = message;
1071 * Core handler for mesh network traffic
1073 * @param cls closure
1074 * @param message message
1075 * @param peer peer identity this notification is about
1076 * @param atsi performance data
1077 * @return GNUNET_OK to keep the connection open,
1078 * GNUNET_SYSERR to close it (signal serious error)
1081 handle_mesh_data_to_orig (void *cls,
1082 const struct GNUNET_PeerIdentity *peer,
1083 const struct GNUNET_MessageHeader *message,
1084 const struct GNUNET_TRANSPORT_ATS_Information
1087 // struct GNUNET_MESH_DataMessageToOrigin *msg = message;
1093 * Functions to handle messages from core
1095 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1096 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1097 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_DATA_MESSAGE_FROM_ORIGIN, 0},
1098 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_DATA_MULTICAST, 0},
1099 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN, 0},
1105 /******************************************************************************/
1106 /**************** MESH LOCAL HANDLER HELPERS ***********************/
1107 /******************************************************************************/
1110 * delete_tunnel_entry: iterator for deleting each tunnel that belongs to a
1111 * client when the client disconnects.
1112 * @param cls closure (client that is disconnecting)
1113 * @param key the hash of the local tunnel id (used to access the hashmap)
1114 * @param value the value stored at the key (tunnel to destroy)
1115 * @return GNUNET_OK on success
1118 delete_tunnel_entry (void *cls, const GNUNET_HashCode * key, void *value) {
1120 r = destroy_tunnel((struct MeshTunnel *) value);
1126 * notify_client_connection_failure: notify a client that the connection to the
1127 * requested remote peer is not possible (for instance, no route found)
1128 * Function called when the socket is ready to queue more data. "buf" will be
1129 * NULL and "size" zero if the socket was closed for writing in the meantime.
1131 * @param cls closure
1132 * @param size number of bytes available in buf
1133 * @param buf where the callee should write the message
1134 * @return number of bytes written to buf
1137 notify_client_connection_failure (void *cls, size_t size, void *buf)
1140 struct MeshPeerInfo *peer_info;
1141 struct GNUNET_MESH_PeerControl *msg;
1142 struct GNUNET_PeerIdentity id;
1144 if (0 == size && NULL == buf) {
1145 // TODO retry? cancel?
1149 size_needed = sizeof(struct GNUNET_MESH_PeerControl);
1150 peer_info = (struct MeshPeerInfo *) cls;
1151 msg = (struct GNUNET_MESH_PeerControl *) buf;
1152 msg->header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
1153 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
1154 // msg->tunnel_id = htonl(peer_info->t->tid);
1155 GNUNET_PEER_resolve(peer_info->id, &id);
1156 memcpy(&msg->peer, &id, sizeof(struct GNUNET_PeerIdentity));
1164 * Send keepalive packets for a peer
1170 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1172 struct MeshPeerInfo *peer_info = cls;
1173 struct GNUNET_PeerIdentity id;
1175 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) return;
1176 GNUNET_PEER_resolve(get_first_hop(peer_info->path), &id);
1177 GNUNET_CORE_notify_transmit_ready(core_handle,
1180 GNUNET_TIME_UNIT_FOREVER_REL,
1182 sizeof(struct GNUNET_MESH_ManipulatePath)
1183 + (peer_info->path->length
1184 * sizeof (struct GNUNET_PeerIdentity)),
1185 &send_core_create_path_for_peer,
1193 * Function to process paths received for a new peer addition. The recorded
1194 * paths form the initial tunnel, which can be optimized later.
1195 * Called on each result obtained for the DHT search.
1197 * @param cls closure
1198 * @param exp when will this value expire
1199 * @param key key of the result
1200 * @param get_path NULL-terminated array of pointers
1201 * to the peers on reverse GET path (or NULL if not recorded)
1202 * @param put_path NULL-terminated array of pointers
1203 * to the peers on the PUT path (or NULL if not recorded)
1204 * @param type type of the result
1205 * @param size number of bytes in data
1206 * @param data pointer to the result data
1209 dht_get_response_handler(void *cls,
1210 struct GNUNET_TIME_Absolute exp,
1211 const GNUNET_HashCode * key,
1212 const struct GNUNET_PeerIdentity * const *get_path,
1213 const struct GNUNET_PeerIdentity * const *put_path,
1214 enum GNUNET_BLOCK_Type type,
1218 struct MeshPeerInfo *peer_info = cls;
1220 struct GNUNET_PeerIdentity pi;
1223 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path) {
1224 // Find ourselves some alternate initial path to the destination: retry
1225 GNUNET_DHT_get_stop(peer_info->dhtget);
1226 GNUNET_PEER_resolve(peer_info->id, &pi);
1227 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
1228 GNUNET_TIME_UNIT_FOREVER_REL,
1229 GNUNET_BLOCK_TYPE_ANY,
1231 4, /* replication level */
1232 GNUNET_DHT_RO_RECORD_ROUTE,
1233 NULL, /* bloom filter */
1236 0, /* xquery bits */
1237 dht_get_response_handler,
1241 p = GNUNET_malloc(sizeof(struct MeshPath));
1242 for (i = 0; get_path[i] != NULL; i++);
1243 for (i--; i >= 0; i--) {
1244 p->peers = GNUNET_realloc(p->peers,
1245 sizeof(GNUNET_PEER_Id) * (p->length + 1));
1246 p->peers[p->length] = GNUNET_PEER_intern(get_path[i]);
1249 for (i = 0; put_path[i] != NULL; i++);
1250 for (i--; i >= 0; i--) {
1251 p->peers = GNUNET_realloc(p->peers,
1252 sizeof(GNUNET_PEER_Id) * (p->length + 1));
1253 p->peers[p->length] = GNUNET_PEER_intern(put_path[i]);
1256 add_path_to_peer(peer_info, p);
1257 GNUNET_CORE_notify_transmit_ready(core_handle,
1260 GNUNET_TIME_UNIT_FOREVER_REL,
1262 sizeof(struct GNUNET_MESH_ManipulatePath)
1264 * sizeof (struct GNUNET_PeerIdentity)),
1265 &send_core_create_path_for_peer,
1267 GNUNET_SCHEDULER_add_delayed(REFRESH_PATH_TIME, &path_refresh, peer_info);
1272 /******************************************************************************/
1273 /********************* MESH LOCAL HANDLES **************************/
1274 /******************************************************************************/
1278 * Handler for client disconnection
1280 * @param cls closure
1281 * @param client identification of the client; NULL
1282 * for the last call when the server is destroyed
1285 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
1287 struct MeshClient *c;
1288 struct MeshClient *next;
1290 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1291 "client disconnected\n");
1294 if (c->handle == client) {
1295 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1296 " matching client found, cleaning\n");
1297 GNUNET_CONTAINER_multihashmap_iterate(c->tunnels,
1298 &delete_tunnel_entry,
1300 GNUNET_CONTAINER_multihashmap_destroy(c->tunnels);
1301 if(0 != c->app_counter) GNUNET_free (c->apps);
1302 if(0 != c->type_counter) GNUNET_free (c->types);
1303 GNUNET_CONTAINER_DLL_remove(clients, clients_tail, c);
1308 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1309 " ... searching\n");
1313 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1320 * Handler for new clients
1322 * @param cls closure
1323 * @param client identification of the client
1324 * @param message the actual message, which includes messages the client wants
1327 handle_local_new_client (void *cls,
1328 struct GNUNET_SERVER_Client *client,
1329 const struct GNUNET_MessageHeader *message)
1331 struct GNUNET_MESH_ClientConnect *cc_msg;
1332 struct MeshClient *c;
1337 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
1338 /* Check data sanity */
1339 size = ntohs(message->size) - sizeof(struct GNUNET_MESH_ClientConnect);
1340 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
1341 types = ntohs(cc_msg->types);
1342 apps = ntohs(cc_msg->applications);
1344 types * sizeof(uint16_t) + apps * sizeof(GNUNET_MESH_ApplicationType))
1347 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1351 /* Create new client structure */
1352 c = GNUNET_malloc(sizeof(struct MeshClient));
1355 c->type_counter = types;
1356 c->types = GNUNET_malloc(types * sizeof(uint16_t));
1357 memcpy(c->types, &message[1], types * sizeof(uint16_t));
1360 c->app_counter = apps;
1361 c->apps = GNUNET_malloc(apps * sizeof(GNUNET_MESH_ApplicationType));
1363 &message[1] + types * sizeof(uint16_t),
1364 apps * sizeof(GNUNET_MESH_ApplicationType));
1366 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1367 " client has %u+%u subscriptions\n",
1371 GNUNET_CONTAINER_DLL_insert(clients, clients_tail, c);
1372 c->tunnels = GNUNET_CONTAINER_multihashmap_create(32);
1374 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1380 * Handler for requests of new tunnels
1382 * @param cls closure
1383 * @param client identification of the client
1384 * @param message the actual message
1387 handle_local_tunnel_create (void *cls,
1388 struct GNUNET_SERVER_Client *client,
1389 const struct GNUNET_MessageHeader *message)
1391 struct GNUNET_MESH_TunnelMessage *t_msg;
1392 struct MeshTunnel *t;
1393 struct MeshClient *c;
1394 GNUNET_HashCode hash;
1396 /* Sanity check for client registration */
1397 if (NULL == (c = retrieve_client(client))) {
1399 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1403 /* Message sanity check */
1404 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
1406 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1410 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
1411 /* Sanity check for tunnel numbering */
1412 if (0 == (ntohl(t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)) {
1414 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1417 /* Sanity check for duplicate tunnel IDs */
1418 if(NULL != retrieve_tunnel_by_local_id(c, ntohl(t_msg->tunnel_id))) {
1420 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1424 t = GNUNET_malloc(sizeof(struct MeshTunnel));
1425 while (NULL != retrieve_tunnel_by_pi(myid, next_tid))
1426 next_tid = (next_tid + 1) % GNUNET_MESH_LOCAL_TUNNEL_ID_MARK;
1427 t->id.tid = next_tid++;
1429 t->local_tid = ntohl(t_msg->tunnel_id);
1431 t->peers = GNUNET_CONTAINER_multihashmap_create(32);
1433 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
1435 GNUNET_CONTAINER_multihashmap_put(c->tunnels, &hash, t,
1436 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1439 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1443 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
1445 GNUNET_CONTAINER_multihashmap_put(tunnels, &hash, t,
1446 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1449 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1453 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1459 * Handler for requests of deleting tunnels
1461 * @param cls closure
1462 * @param client identification of the client
1463 * @param message the actual message
1466 handle_local_tunnel_destroy (void *cls,
1467 struct GNUNET_SERVER_Client *client,
1468 const struct GNUNET_MessageHeader *message)
1470 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
1471 struct MeshClient *c;
1472 struct MeshTunnel *t;
1473 MESH_TunnelNumber tid;
1474 GNUNET_HashCode hash;
1477 /* Sanity check for client registration */
1478 if (NULL == (c = retrieve_client(client))) {
1480 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1483 /* Message sanity check */
1484 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
1486 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1490 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
1492 /* Retrieve tunnel */
1493 tid = ntohl(tunnel_msg->tunnel_id);
1495 /* Remove from local id hashmap */
1496 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
1497 t = GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
1498 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t);
1500 /* Remove from global id hashmap */
1501 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
1502 GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t);
1504 // notify_tunnel_destroy(t);
1505 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1511 * Handler for connection requests to new peers
1513 * @param cls closure
1514 * @param client identification of the client
1515 * @param message the actual message (PeerControl)
1518 handle_local_connect_add (void *cls,
1519 struct GNUNET_SERVER_Client *client,
1520 const struct GNUNET_MessageHeader *message)
1522 struct GNUNET_MESH_PeerControl *peer_msg;
1523 struct MeshClient *c;
1524 struct MeshTunnel *t;
1525 MESH_TunnelNumber tid;
1526 struct MeshPeerInfo *peer_info;
1529 /* Sanity check for client registration */
1530 if (NULL == (c = retrieve_client(client))) {
1532 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1536 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1537 /* Sanity check for message size */
1538 if (sizeof(struct GNUNET_MESH_PeerControl)
1539 != ntohs(peer_msg->header.size))
1542 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1546 /* Tunnel exists? */
1547 tid = ntohl(peer_msg->tunnel_id);
1548 t = retrieve_tunnel_by_local_id(c, tid);
1551 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1555 /* Does client own tunnel? */
1556 if (t->client->handle != client) {
1558 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1563 peer_info = get_peer_info(&peer_msg->peer);
1565 /* Start DHT search if needed */
1566 if(MESH_PEER_READY != peer_info->state && NULL == peer_info->dhtget) {
1567 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
1568 GNUNET_TIME_UNIT_FOREVER_REL,
1569 GNUNET_BLOCK_TYPE_ANY,
1570 &peer_msg->peer.hashPubKey,
1571 4, /* replication level */
1572 GNUNET_DHT_RO_RECORD_ROUTE,
1573 NULL, /* bloom filter */
1576 0, /* xquery bits */
1577 dht_get_response_handler,
1581 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1587 * Handler for disconnection requests of peers in a tunnel
1589 * @param cls closure
1590 * @param client identification of the client
1591 * @param message the actual message (PeerControl)
1594 handle_local_connect_del (void *cls,
1595 struct GNUNET_SERVER_Client *client,
1596 const struct GNUNET_MessageHeader *message)
1598 struct GNUNET_MESH_PeerControl *peer_msg;
1599 struct MeshClient *c;
1600 struct MeshTunnel *t;
1601 MESH_TunnelNumber tid;
1603 /* Sanity check for client registration */
1604 if (NULL == (c = retrieve_client(client))) {
1606 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1609 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
1610 /* Sanity check for message size */
1611 if (sizeof(struct GNUNET_MESH_PeerControl)
1612 != ntohs(peer_msg->header.size))
1615 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1619 /* Tunnel exists? */
1620 tid = ntohl(peer_msg->tunnel_id);
1621 t = retrieve_tunnel_by_local_id(c, tid);
1624 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1628 /* Does client own tunnel? */
1629 if (t->client->handle != client) {
1631 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1635 /* Ok, delete peer from tunnel */
1636 GNUNET_CONTAINER_multihashmap_remove_all(t->peers,
1637 &peer_msg->peer.hashPubKey);
1639 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1645 * Handler for connection requests to new peers by type
1647 * @param cls closure
1648 * @param client identification of the client
1649 * @param message the actual message (ConnectPeerByType)
1652 handle_local_connect_by_type (void *cls,
1653 struct GNUNET_SERVER_Client *client,
1654 const struct GNUNET_MessageHeader *message)
1656 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
1657 MESH_TunnelNumber tid;
1658 GNUNET_MESH_ApplicationType application;
1659 struct MeshClient *c;
1660 struct MeshTunnel *t;
1662 /* Sanity check for client registration */
1663 if (NULL == (c = retrieve_client(client))) {
1665 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1669 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *)message;
1670 /* Sanity check for message size */
1671 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1672 ntohs(connect_msg->header.size))
1675 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1679 /* Tunnel exists? */
1680 tid = ntohl(connect_msg->tunnel_id);
1681 t = retrieve_tunnel_by_local_id(c, tid);
1684 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1688 /* Does client own tunnel? */
1689 if (t->client->handle != client) {
1691 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1695 /* Ok, lets find a peer offering the service */
1696 application = ntohl(connect_msg->type);
1697 application++; // FIXME silence warnings
1699 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1705 * Handler for client traffic directed to one peer
1707 * @param cls closure
1708 * @param client identification of the client
1709 * @param message the actual message
1712 handle_local_network_traffic (void *cls,
1713 struct GNUNET_SERVER_Client *client,
1714 const struct GNUNET_MessageHeader *message)
1716 struct MeshClient *c;
1717 struct MeshTunnel *t;
1718 struct MeshPeerInfo *pi;
1719 struct GNUNET_MESH_Data *data_msg;
1720 struct GNUNET_PeerIdentity next_hop;
1721 MESH_TunnelNumber tid;
1723 /* Sanity check for client registration */
1724 if (NULL == (c = retrieve_client(client))) {
1726 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1729 data_msg = (struct GNUNET_MESH_Data *)message;
1730 /* Sanity check for message size */
1731 if (sizeof(struct GNUNET_MESH_PeerControl) !=
1732 ntohs(data_msg->header.size))
1735 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1739 /* Tunnel exists? */
1740 tid = ntohl(data_msg->tunnel_id);
1741 t = retrieve_tunnel_by_local_id(c, tid);
1744 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1748 /* Is it a local tunnel? Then, does client own the tunnel? */
1749 if (t->client->handle != NULL && t->client->handle != client) {
1751 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1755 pi = GNUNET_CONTAINER_multihashmap_get(t->peers,
1756 &data_msg->peer_id.hashPubKey);
1757 /* Is the selected peer in the tunnel? */
1760 * Are we SO nice that we automatically try to add him to the tunnel?
1763 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1766 GNUNET_PEER_resolve(get_first_hop(pi->path), &next_hop);
1767 GNUNET_CORE_notify_transmit_ready(core_handle,
1770 GNUNET_TIME_UNIT_FOREVER_REL,
1772 /* FIXME re-check types */
1773 message->size - sizeof(struct GNUNET_MESH_Data)
1774 + sizeof(struct GNUNET_MESH_DataMessageFromOrigin),
1775 &send_core_data_to_origin, /* FIXME re-check */
1778 GNUNET_SERVER_receive_done(client, GNUNET_OK); /* FIXME not yet */
1783 * Handler for client traffic directed to all peers in a tunnel
1785 * @param cls closure
1786 * @param client identification of the client
1787 * @param message the actual message
1790 handle_local_network_traffic_bcast (void *cls,
1791 struct GNUNET_SERVER_Client *client,
1792 const struct GNUNET_MessageHeader *message)
1794 struct MeshClient *c;
1795 struct MeshTunnel *t;
1796 struct GNUNET_MESH_DataBroadcast *data_msg;
1797 MESH_TunnelNumber tid;
1799 /* Sanity check for client registration */
1800 if (NULL == (c = retrieve_client(client))) {
1802 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1805 data_msg = (struct GNUNET_MESH_DataBroadcast *)message;
1806 /* Sanity check for message size */
1807 if (sizeof(struct GNUNET_MESH_PeerControl)
1808 != ntohs(data_msg->header.size))
1811 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1815 /* Tunnel exists? */
1816 tid = ntohl(data_msg->tunnel_id);
1817 t = retrieve_tunnel_by_local_id(c, tid);
1820 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1824 /* Does client own tunnel? */
1825 if (t->client->handle != client) {
1827 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
1833 GNUNET_SERVER_receive_done(client, GNUNET_OK);
1838 * Functions to handle messages from clients
1840 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
1841 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
1842 {&handle_local_tunnel_create, NULL,
1843 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
1844 sizeof(struct GNUNET_MESH_TunnelMessage)},
1845 {&handle_local_tunnel_destroy, NULL,
1846 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
1847 sizeof(struct GNUNET_MESH_TunnelMessage)},
1848 {&handle_local_connect_add, NULL,
1849 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD,
1850 sizeof(struct GNUNET_MESH_PeerControl)},
1851 {&handle_local_connect_del, NULL,
1852 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL,
1853 sizeof(struct GNUNET_MESH_PeerControl)},
1854 {&handle_local_connect_by_type, NULL,
1855 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE,
1856 sizeof(struct GNUNET_MESH_ConnectPeerByType)},
1857 {&handle_local_network_traffic, NULL,
1858 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
1859 {&handle_local_network_traffic_bcast, NULL,
1860 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0},
1866 * To be called on core init/fail.
1868 * @param cls service closure
1869 * @param server handle to the server for this service
1870 * @param identity the public identity of this peer
1871 * @param publicKey the public key of this peer
1874 core_init (void *cls,
1875 struct GNUNET_CORE_Handle *server,
1876 const struct GNUNET_PeerIdentity *identity,
1877 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
1879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1881 core_handle = server;
1882 myid = GNUNET_PEER_intern(identity);
1887 * Method called whenever a given peer connects.
1889 * @param cls closure
1890 * @param peer peer identity this notification is about
1891 * @param atsi performance data for the connection
1894 core_connect (void *cls,
1895 const struct GNUNET_PeerIdentity *peer,
1896 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1898 // GNUNET_PEER_Id pid;
1899 struct MeshPeerInfo *peer_info;
1900 struct MeshPath *path;
1902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1903 "Peer connected\n");
1904 peer_info = get_peer_info(peer);
1905 if (myid == peer_info->id) {
1906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1909 path = GNUNET_malloc(sizeof(struct MeshPath));
1911 path->peers = GNUNET_malloc(sizeof(GNUNET_PEER_Id) * 2);
1912 path->peers[0] = myid;
1913 path->peers[1] = peer_info->id;
1914 add_path_to_peer(peer_info, path);
1919 * Method called whenever a peer disconnects.
1921 * @param cls closure
1922 * @param peer peer identity this notification is about
1925 core_disconnect (void *cls,
1927 GNUNET_PeerIdentity *peer)
1930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1931 "Peer disconnected\n");
1932 pid = GNUNET_PEER_search(peer);
1934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1941 /******************************************************************************/
1942 /************************ MAIN FUNCTIONS ****************************/
1943 /******************************************************************************/
1946 * Task run during shutdown.
1952 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1956 if (core_handle != NULL) {
1957 GNUNET_CORE_disconnect (core_handle);
1960 if (dht_handle != NULL) {
1961 GNUNET_DHT_disconnect (dht_handle);
1964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1969 * Process mesh requests.
1971 * @param cls closure
1972 * @param server the initialized server
1973 * @param c configuration to use
1977 struct GNUNET_SERVER_Handle *server,
1978 const struct GNUNET_CONFIGURATION_Handle *c)
1980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1981 "starting to run\n");
1982 GNUNET_SERVER_add_handlers (server, plugin_handlers);
1983 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1984 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1986 NULL, /* Closure passed to MESH functions */
1987 &core_init, /* Call core_init once connected */
1988 &core_connect, /* Handle connects */
1989 &core_disconnect, /* remove peers on disconnects */
1990 NULL, /* Do we care about "status" updates? */
1991 NULL, /* Don't notify about all incoming messages */
1992 GNUNET_NO, /* For header only in notification */
1993 NULL, /* Don't notify about all outbound messages */
1994 GNUNET_NO, /* For header-only out notification */
1995 core_handlers); /* Register these handlers */
1996 if (core_handle == NULL) {
1999 dht_handle = GNUNET_DHT_connect(c, 64);
2000 if (dht_handle == NULL) {
2005 tunnels = GNUNET_CONTAINER_multihashmap_create(32);
2006 peers = GNUNET_CONTAINER_multihashmap_create(32);
2008 clients_tail = NULL;
2010 /* Scheduled the task to clean up when shutdown is called */
2011 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2012 &shutdown_task, NULL);
2014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2019 * The main function for the mesh service.
2021 * @param argc number of arguments from the command line
2022 * @param argv command line arguments
2023 * @return 0 ok, 1 on error
2026 main (int argc, char *const *argv)
2031 GNUNET_SERVICE_run (argc,
2034 GNUNET_SERVICE_OPTION_NONE,
2035 &run, NULL)) ? 0 : 1;
2036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,