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
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - add connection confirmation message
44 * - handle trnsmt_rdy return values
48 #include "gnunet_common.h"
49 #include "gnunet_util_lib.h"
50 #include "gnunet_peer_lib.h"
51 #include "gnunet_core_service.h"
52 #include "gnunet_protocols.h"
55 #include "mesh_protocol.h"
56 #include "gnunet_dht_service.h"
62 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
65 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
68 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason) {
71 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s", s);
75 #define CORE_QUEUE_SIZE 10
76 #define LOCAL_QUEUE_SIZE 100
77 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
78 GNUNET_TIME_UNIT_SECONDS,\
80 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
81 GNUNET_TIME_UNIT_SECONDS,\
84 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
85 GNUNET_TIME_UNIT_SECONDS,\
90 /******************************************************************************/
91 /************************ DATA STRUCTURES ****************************/
92 /******************************************************************************/
95 * Information regarding a path
103 struct MeshPath *next;
104 struct MeshPath *prev;
107 * Whether the path is serving traffic in a tunnel or is a backup
112 * List of all the peers that form the path from origin to target
114 GNUNET_PEER_Id *peers;
117 * Number of peers (hops) in the path
124 * All the states a peer participating in a tunnel can be in.
129 * Path to the peer not known yet
134 * Request sent, not yet answered.
139 * Peer connected and ready to accept data
144 * Peer connected previosly but not responding
146 MESH_PEER_RECONNECTING
150 /** FWD declaration */
154 * Struct containing all info possibly needed to build a package when called
157 struct MeshDataDescriptor
159 /** ID of the tunnel this packet travels in */
160 struct MESH_TunnelID *origin;
162 /** Ultimate destination of the packet */
163 GNUNET_PEER_Id destination;
165 /** Number of identical messages sent to different hops (multicast) */
168 /** Size of the data */
171 /** Client that asked for the transmission, if any */
172 struct GNUNET_SERVER_Client *client;
174 /** Who was is message being sent to */
175 struct MeshPeerInfo *peer;
177 /** Which handler was used to request the transmission */
178 unsigned int handler_n;
180 /* Data at the end */
185 * Struct containing all information regarding a given peer
195 * Is the peer reachable? Is the peer even connected?
197 enum MeshPeerState state;
200 * Last time we heard from this peer
202 struct GNUNET_TIME_Absolute last_contact;
205 * Number of attempts to reconnect so far
207 int n_reconnect_attempts;
210 * Paths to reach the peer
212 struct MeshPath *path;
213 struct MeshPath *path_tail;
216 * Handle to stop the DHT search for a path to this peer
218 struct GNUNET_DHT_GetHandle *dhtget;
221 * Handles to stop queued transmissions for this peer
223 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
226 * Pointer to info stuctures used as cls for queued transmissions
228 struct MeshDataDescriptor *infos[CORE_QUEUE_SIZE];
231 * Task to send keepalive packets over the current active path
233 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
238 * Data scheduled to transmit (to local client or remote peer)
245 struct MeshQueue *next;
246 struct MeshQueue *prev;
249 * Target of the data (NULL if target is client)
251 struct MeshPeerInfo *peer;
254 * Client to send the data to (NULL if target is peer)
256 struct MeshClient *client;
259 * Size of the message to transmit
264 * How old is the data?
266 struct GNUNET_TIME_Absolute timestamp;
271 struct GNUNET_MessageHeader *data;
275 * Globally unique tunnel identification (owner + number)
276 * DO NOT USE OVER THE NETWORK
278 struct MESH_TunnelID {
280 * Node that owns the tunnel
285 * Tunnel number to differentiate all the tunnels owned by the node oid
286 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_MARK )
288 MESH_TunnelNumber tid;
292 struct MeshClient; /* FWD declaration */
294 * Struct containing all information regarding a tunnel
295 * For an intermediate node the improtant info used will be:
296 * - id Tunnel unique identification
297 * - paths[0] To know where to send it next
298 * - metainfo: ready, speeds, accounting
305 struct MESH_TunnelID id;
308 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK or 0 )
310 MESH_TunnelNumber local_tid;
313 * Last time the tunnel was used
315 struct GNUNET_TIME_Absolute timestamp;
318 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
320 struct GNUNET_CONTAINER_MultiHashMap* peers;
323 * Number of peers that are connected and potentially ready to receive data
325 unsigned int peers_ready;
328 * Number of peers that have been added to the tunnel
330 unsigned int peers_total;
334 * Client owner of the tunnel, if any
336 struct MeshClient *client;
339 * Messages ready to transmit
341 struct MeshQueue *queue_head;
342 struct MeshQueue *queue_tail;
347 * Struct containing information about a client of the service
354 struct MeshClient *next;
355 struct MeshClient *prev;
358 * Tunnels that belong to this client, indexed by local id
360 struct GNUNET_CONTAINER_MultiHashMap* tunnels;
363 * Handle to communicate with the client
365 struct GNUNET_SERVER_Client *handle;
368 * Applications that this client has claimed to provide
370 GNUNET_MESH_ApplicationType *apps;
371 unsigned int app_counter;
374 * Messages that this client has declared interest in
377 unsigned int type_counter;
380 * Used for seachching peers offering a service
382 struct GNUNET_DHT_GetHandle *dht_get_type;
386 /******************************************************************************/
387 /*********************** GLOBAL VARIABLES ****************************/
388 /******************************************************************************/
393 static struct MeshClient *clients;
394 static struct MeshClient *clients_tail;
397 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
399 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
402 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
404 static struct GNUNET_CONTAINER_MultiHashMap *peers;
407 * Handle to communicate with core
409 static struct GNUNET_CORE_Handle *core_handle;
414 static struct GNUNET_DHT_Handle *dht_handle;
419 static struct GNUNET_SERVER_Handle *server_handle;
422 * Notification context, to send messages to local clients
424 static struct GNUNET_SERVER_NotificationContext *nc;
427 * Local peer own ID (memory efficient handle)
429 static GNUNET_PEER_Id myid;
432 * Tunnel ID for the next created tunnel (global tunnel number)
434 static MESH_TunnelNumber next_tid;
437 * All application types provided by this peer
439 static GNUNET_MESH_ApplicationType *applications;
442 * All application types provided by this peer (reference counter)
444 static unsigned int *applications_rc;
447 * Number of applications provided by this peer
449 static unsigned int n_applications;
452 * Task to periodically announce provided applications
454 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
457 * Task to periodically announce itself in the network
459 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
461 /******************************************************************************/
462 /****************** GENERAL HELPER FUNCTIONS ************************/
463 /******************************************************************************/
466 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
467 * and inster it in the appropiate structures if the peer is not known yet.
468 * @param peer Identity of the peer
469 * @return Existing or newly created peer info
471 static struct MeshPeerInfo *
472 get_peer_info (const struct GNUNET_PeerIdentity *peer)
474 struct MeshPeerInfo * peer_info;
476 peer_info = GNUNET_CONTAINER_multihashmap_get(peers,
478 if (NULL == peer_info) {
479 peer_info = (struct MeshPeerInfo *)
480 GNUNET_malloc(sizeof(struct MeshPeerInfo));
481 GNUNET_CONTAINER_multihashmap_put(peers,
484 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
485 peer_info->id = GNUNET_PEER_intern(peer);
486 peer_info->state = MESH_PEER_SEARCHING;
493 * Find the first peer whom to send a packet to go down this path
494 * @param path The path to use
495 * @return short id of the next peer, myid in case of local delivery,
496 * or 0 in case of error
498 static GNUNET_PEER_Id
499 get_first_hop (struct MeshPath *path)
503 while (NULL != path) {
504 if (path->in_use) break;
508 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
509 "tried to get the next hop from an invalid path\n");
513 for (i = 0; i < path->length; i++) {
514 if (path->peers[i] == myid) {
515 if (i < path->length - 1) {
516 return path->peers[i+1];
527 * Get the cost of the path.
528 * @param path The path to analyze
529 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
533 get_path_cost(struct MeshPath *path)
537 if (NULL == path) return UINT_MAX;
538 for (i = 0; i < path->length; i++) {
539 if (path->peers[i] == myid) {
540 return path->length - i;
548 * Add the path to the peer and update the path used to reach it in case this
550 * @param peer_info Destination peer to add the path to.
551 * @param path New path to add. Last peer must be the peer in arg 1.
554 add_path_to_peer(struct MeshPeerInfo *peer_info, struct MeshPath *path)
557 unsigned int new_cost;
558 unsigned int best_cost;
559 struct MeshPath *aux;
560 struct MeshPath *best;
562 if (NULL == peer_info || NULL == path) return;
564 new_cost = get_path_cost(path);
565 best_cost = UINT_MAX;
567 for (aux = peer_info->path; aux != NULL; aux = aux->next) {
568 if ((i = get_path_cost(aux)) < best_cost) {
573 if (best_cost < new_cost) {
575 GNUNET_CONTAINER_DLL_insert_tail(peer_info->path,
576 peer_info->path_tail,
579 if (NULL != best) best->in_use = 0;
581 GNUNET_CONTAINER_DLL_insert(peer_info->path,
582 peer_info->path_tail,
590 * Add the path to the peer and update the path used to reach it in case this
591 * is the shortest. The path is given in reverse, the destination peer is
592 * path[0]. The function modifies the path, inverting it to use the origin as
594 * @param peer_info Destination peer to add the path to.
595 * @param path New path to add. First peer must be the peer in arg 1.
598 add_path_to_origin(struct MeshPeerInfo *peer_info, struct MeshPath *path)
603 for (i = 0; i < path->length/2; i++) {
604 aux = path->peers[i];
605 path->peers[i] = path->peers[path->length - i - 1];
606 path->peers[path->length - i - 1] = aux;
608 add_path_to_peer(peer_info, path);
613 * Check if client has registered with the service and has not disconnected
614 * @param client the client to check
615 * @return non-NULL if client exists in the global DLL
617 static struct MeshClient *
618 retrieve_client (struct GNUNET_SERVER_Client *client)
620 struct MeshClient *c;
624 if (c->handle == client) return c;
632 * Checks if a given client has subscribed to certain message type
633 * @param message_type Type of message to check
634 * @param c Client to check
635 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
637 static int /* FIXME inline? */
638 is_client_subscribed(uint16_t message_type, struct MeshClient *c)
642 for (i = 0; i < c->type_counter; i++) {
643 if (c->types[i] == message_type) return GNUNET_YES;
650 * Search for a tunnel among the tunnels for a client
651 * @param client the client whose tunnels to search in
652 * @param tid the local id of the tunnel
653 * @return tunnel handler, NULL if doesn't exist
655 static struct MeshTunnel *
656 retrieve_tunnel_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
658 GNUNET_HashCode hash;
660 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
661 return GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
665 * Search for a tunnel by global ID using PEER_ID
666 * @param pi owner of the tunnel
667 * @param tid global tunnel number
668 * @return tunnel handler, NULL if doesn't exist
670 static struct MeshTunnel *
671 retrieve_tunnel_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
673 struct MESH_TunnelID id;
674 GNUNET_HashCode hash;
679 GNUNET_CRYPTO_hash(&id, sizeof(struct MESH_TunnelID), &hash);
680 return GNUNET_CONTAINER_multihashmap_get(tunnels, &hash);
686 * Search for a tunnel by global ID using full PeerIdentities
687 * @param oid owner of the tunnel
688 * @param tid global tunnel number
689 * @return tunnel handler, NULL if doesn't exist
691 static struct MeshTunnel *
692 retrieve_tunnel (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
694 return retrieve_tunnel_by_pi(GNUNET_PEER_search(oid), tid);
699 * Destroy the path and free any allocated resources linked to it
700 * @param t tunnel the path belongs to
701 * @param p the path to destroy
702 * @return GNUNET_OK on success
705 destroy_path(struct MeshPath *p)
707 GNUNET_PEER_decrement_rcs(p->peers, p->length);
708 GNUNET_free(p->peers);
715 * Destroy the peer_info and free any allocated resources linked to it
716 * @param t tunnel the path belongs to
717 * @param pi the peer_info to destroy
718 * @return GNUNET_OK on success
721 destroy_peer_info(struct MeshPeerInfo *pi)
723 GNUNET_HashCode hash;
724 struct GNUNET_PeerIdentity id;
726 GNUNET_PEER_resolve(pi->id, &id);
727 GNUNET_PEER_change_rc(pi->id, -1);
728 GNUNET_CRYPTO_hash(&id, sizeof(struct GNUNET_PeerIdentity), &hash);
730 GNUNET_CONTAINER_multihashmap_remove(peers, &hash, pi);
731 GNUNET_SCHEDULER_cancel(pi->path_refresh_task);
739 * Destroy the tunnel and free any allocated resources linked to it
740 * @param c client the tunnel belongs to
741 * @param t the tunnel to destroy
742 * @return GNUNET_OK on success
745 destroy_tunnel(struct MeshTunnel *t)
747 struct MeshClient *c;
748 GNUNET_HashCode hash;
751 if (NULL == t) return GNUNET_OK;
755 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
756 if(GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t)) {
760 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
762 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t))
770 /******************************************************************************/
771 /************************ PERIODIC FUNCTIONS ****************************/
772 /******************************************************************************/
775 * Periodically announce what applications are provided by local clients
778 * @param tc task context
781 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
783 struct GNUNET_PeerIdentity id;
784 GNUNET_HashCode hash;
785 uint8_t buffer[12] = "MESH_APP";
789 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) {
790 announce_applications_task = (GNUNET_SCHEDULER_TaskIdentifier) 0;
793 p = (unsigned int *) &buffer[8];
794 GNUNET_PEER_resolve(myid, &id);
795 for (i = 0; i < n_applications; i++) {
796 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
797 "Starting PUT for app %d\n",
799 *p = htonl(applications[i]);
800 GNUNET_CRYPTO_hash(buffer, 12, &hash);
801 GNUNET_DHT_put(dht_handle,
804 GNUNET_DHT_RO_RECORD_ROUTE,
805 GNUNET_BLOCK_TYPE_ANY,
806 sizeof(struct GNUNET_PeerIdentity),
808 GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(),
813 "DHT_put for app completed\n");
819 announce_applications_task = GNUNET_SCHEDULER_add_delayed(
821 &announce_applications, cls);
827 * Periodically announce self id in the DHT
830 * @param tc task context
833 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
835 struct GNUNET_PeerIdentity id;
836 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) {
837 announce_id_task = (GNUNET_SCHEDULER_TaskIdentifier) 0;
840 GNUNET_PEER_resolve(myid, &id);
842 * - Set data expiration in function of X
845 GNUNET_DHT_put(dht_handle, /* DHT handle */
846 &id.hashPubKey, /* Key to use */
847 10U, /* Replication level */
848 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
849 GNUNET_BLOCK_TYPE_ANY, /* Block type */
850 0, /* Size of the data */
851 NULL, /* Data itself */
852 GNUNET_TIME_absolute_get_forever(), /* Data expiration */
853 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
856 "DHT_put for id completed\n");
858 NULL, /* Continuation */
859 NULL); /* Continuation closure */
861 announce_id_task = GNUNET_SCHEDULER_add_delayed(ID_ANNOUNCE_TIME,
865 /******************************************************************************/
866 /**************** MESH NETWORK HANDLER HELPERS ***********************/
867 /******************************************************************************/
870 * Function called to notify a client about the socket
871 * being ready to queue more data. "buf" will be
872 * NULL and "size" zero if the socket was closed for
873 * writing in the meantime.
876 * @param size number of bytes available in buf
877 * @param buf where the callee should write the message
878 * @return number of bytes written to buf
881 send_core_create_path_for_peer (void *cls, size_t size, void *buf)
883 struct MeshPeerInfo *peer_info = cls;
884 struct GNUNET_MESH_ManipulatePath *msg;
886 struct GNUNET_PeerIdentity *peer_ptr;
887 struct GNUNET_PeerIdentity id;
891 if (0 == size && NULL == buf) {
892 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Retransmitting create path\n");
893 GNUNET_PEER_resolve(get_first_hop(peer_info->path), &id);
894 GNUNET_CORE_notify_transmit_ready(core_handle,
897 GNUNET_TIME_UNIT_FOREVER_REL,
899 sizeof(struct GNUNET_MESH_ManipulatePath)
900 + (peer_info->path->length
901 * sizeof (struct GNUNET_PeerIdentity)),
902 &send_core_create_path_for_peer,
913 if (p == NULL) return 0; // TODO Notify ERROR Path not found
915 size_needed = sizeof(struct GNUNET_MESH_ManipulatePath)
916 + p->length * sizeof(struct GNUNET_PeerIdentity);
917 if (size < size_needed) {
918 // TODO retry? cancel?
922 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
923 msg->header.size = htons(size_needed);
924 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
926 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
927 for (i = 0; i < p->length; i++) {
928 GNUNET_PEER_resolve(p->peers[i], peer_ptr++);
931 peer_info->state = MESH_PEER_WAITING;
939 * Function called to notify a client about the socket
940 * being ready to queue more data. "buf" will be
941 * NULL and "size" zero if the socket was closed for
942 * writing in the meantime.
944 * @param cls closure (MeshDataDescriptor with all info to build packet)
945 * @param size number of bytes available in buf
946 * @param buf where the callee should write the message
947 * @return number of bytes written to buf
950 send_core_data_to_origin (void *cls, size_t size, void *buf)
952 struct MeshDataDescriptor *info = cls;
953 struct GNUNET_MESH_ToOrigin *msg = buf;
956 GNUNET_assert(NULL != info);
957 total_size = sizeof(struct GNUNET_MESH_ToOrigin) + info->size;
958 GNUNET_assert(total_size < 65536); /* UNIT16_MAX */
960 if (total_size > size) {
961 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
962 "not enough buffer to send data to origin\n");
965 msg->header.size = htons(total_size);
966 msg->header.type = htons(GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
967 GNUNET_PEER_resolve(info->origin->oid, &msg->oid);
968 msg->tid = htonl(info->origin->tid);
969 if (0 != info->size) {
970 memcpy(&msg[1], &info[1], info->size);
972 if (NULL != info->client) {
973 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
981 * Function called to notify a client about the socket
982 * being ready to queue more data. "buf" will be
983 * NULL and "size" zero if the socket was closed for
984 * writing in the meantime.
986 * @param cls closure (data itself)
987 * @param size number of bytes available in buf
988 * @param buf where the callee should write the message
989 * @return number of bytes written to buf
992 send_core_data_unicast (void *cls, size_t size, void *buf)
994 struct MeshDataDescriptor *info = cls;
995 struct GNUNET_MESH_Unicast *msg = buf;
998 GNUNET_assert(NULL != info);
999 total_size = sizeof(struct GNUNET_MESH_Unicast) + info->size;
1000 GNUNET_assert(total_size < 65536); /* UNIT16_MAX */
1002 if (total_size > size) {
1003 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1004 "not enough buffer to send data to peer\n");
1007 msg->header.size = htons(total_size);
1008 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1009 GNUNET_PEER_resolve(info->origin->oid, &msg->oid);
1010 GNUNET_PEER_resolve(info->destination, &msg->destination);
1011 msg->tid = htonl(info->origin->tid);
1012 if (0 != info->size) {
1013 memcpy(&msg[1], &info[1], info->size);
1015 if (NULL != info->client) {
1016 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
1024 * Function called to notify a client about the socket
1025 * being ready to queue more data. "buf" will be
1026 * NULL and "size" zero if the socket was closed for
1027 * writing in the meantime.
1029 * @param cls closure (data itself)
1030 * @param size number of bytes available in buf
1031 * @param buf where the callee should write the message
1032 * @return number of bytes written to buf
1035 send_core_data_multicast (void *cls, size_t size, void *buf)
1037 struct MeshDataDescriptor *info = cls;
1038 struct GNUNET_MESH_Multicast *msg = buf;
1041 GNUNET_assert(NULL != info);
1042 total_size = info->size + sizeof(struct GNUNET_MESH_Multicast);
1043 GNUNET_assert(total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1046 info->peer->core_transmit[info->handler_n] = NULL;
1048 if (total_size > size) {
1049 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1050 "not enough buffer to send data futher\n");
1053 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1054 msg->header.size = htons(total_size);
1055 GNUNET_PEER_resolve(info->origin->oid, &msg->oid);
1056 msg->tid = htonl(info->origin->tid);
1057 memcpy(&msg[1], &info[1], total_size);
1058 if (0 == --info->copies) {
1059 if (NULL != info->client) {
1060 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
1069 * Function called to notify a client about the socket
1070 * being ready to queue more data. "buf" will be
1071 * NULL and "size" zero if the socket was closed for
1072 * writing in the meantime.
1074 * @param cls closure (MeshDataDescriptor)
1075 * @param size number of bytes available in buf
1076 * @param buf where the callee should write the message
1077 * @return number of bytes written to buf
1080 send_core_path_ack (void *cls, size_t size, void *buf) {
1081 struct MeshDataDescriptor *info = cls;
1082 struct GNUNET_MESH_PathACK *msg = buf;
1084 GNUNET_assert(NULL != info);
1086 info->peer->core_transmit[info->handler_n] = NULL;
1088 if (sizeof(struct GNUNET_MESH_PathACK) > size) {
1092 msg->header.size = htons(sizeof(struct GNUNET_MESH_PathACK));
1093 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1094 GNUNET_PEER_resolve(info->origin->oid, &msg->oid);
1095 msg->tid = htonl(info->origin->tid);
1096 GNUNET_PEER_resolve(myid, &msg->peer_id);
1097 /* TODO add signature */
1099 return sizeof(struct GNUNET_MESH_PathACK);
1104 * Function called to notify a client about the socket
1105 * being ready to queue more data. "buf" will be
1106 * NULL and "size" zero if the socket was closed for
1107 * writing in the meantime.
1109 * @param cls closure (data itself)
1110 * @param size number of bytes available in buf
1111 * @param buf where the callee should write the message
1112 * @return number of bytes written to buf
1115 send_core_data_raw (void *cls, size_t size, void *buf)
1117 struct GNUNET_MessageHeader *msg = cls;
1120 GNUNET_assert(NULL != msg);
1121 total_size = ntohs(msg->size);
1123 if (total_size > size) {
1127 memcpy(buf, msg, total_size);
1135 * Send another peer a notification to destroy a tunnel
1136 * @param cls The tunnel to destroy
1137 * @param size Size in the buffer
1138 * @param buf Memory where to put the data to transmit
1139 * @return Size of data put in buffer
1142 send_p2p_tunnel_destroy(void *cls, size_t size, void *buf)
1144 struct MeshTunnel *t = cls;
1145 struct MeshClient *c;
1146 struct GNUNET_MESH_TunnelMessage *msg;
1150 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY); /*FIXME*/
1151 msg->header.size = htons(sizeof(struct GNUNET_MESH_TunnelMessage));
1152 msg->tunnel_id = htonl(t->id.tid);
1154 destroy_tunnel(c, t);
1155 return sizeof(struct GNUNET_MESH_TunnelMessage);
1161 * Send the message to all clients that have subscribed to its type
1163 * @param msg Pointer to the message itself
1164 * @return number of clients this message was sent to
1167 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1169 struct MeshClient *c;
1173 type = ntohs(msg->type);
1174 for (count = 0, c = clients; c != NULL; c = c->next) {
1175 if (is_client_subscribed(type, c)) {
1177 GNUNET_SERVER_notification_context_unicast(nc,
1188 * Iterator over hash map peer entries collect all neighbors who to resend the
1191 * @param cls closure (**GNUNET_PEER_Id to store hops to send packet)
1192 * @param key current key code (peer id hash)
1193 * @param value value in the hash map (peer_info)
1194 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
1196 static int iterate_collect_neighbors (void *cls,
1197 const GNUNET_HashCode * key,
1200 struct MeshPeerInfo *peer_info = value;
1201 GNUNET_PEER_Id **neighbors = cls;
1205 if (peer_info->id == myid) {
1208 id = get_first_hop(peer_info->path);
1209 for (i = 0; *neighbors[i] != 0; i++) {
1210 if (*neighbors[i] == id) return GNUNET_YES;
1212 *neighbors = GNUNET_realloc(*neighbors, (i + 2) * sizeof(GNUNET_PEER_Id));
1214 *neighbors[i + 1] = 0;
1220 /******************************************************************************/
1221 /******************** MESH NETWORK HANDLERS **************************/
1222 /******************************************************************************/
1226 * Core handler for path creation
1227 * struct GNUNET_CORE_MessageHandler
1229 * @param cls closure
1230 * @param message message
1231 * @param peer peer identity this notification is about
1232 * @param atsi performance data
1233 * @return GNUNET_OK to keep the connection open,
1234 * GNUNET_SYSERR to close it (signal serious error)
1238 handle_mesh_path_create (void *cls,
1239 const struct GNUNET_PeerIdentity *peer,
1240 const struct GNUNET_MessageHeader *message,
1241 const struct GNUNET_TRANSPORT_ATS_Information
1244 unsigned int own_pos;
1247 MESH_TunnelNumber tid;
1248 struct GNUNET_MESH_ManipulatePath *msg;
1249 struct GNUNET_PeerIdentity *pi;
1250 struct GNUNET_PeerIdentity id;
1251 GNUNET_HashCode hash;
1252 struct MeshPath *path;
1253 struct MeshPeerInfo *dest_peer_info;
1254 struct MeshPeerInfo *orig_peer_info;
1255 struct MeshTunnel *t;
1257 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a MESH path create msg\n");
1258 size = ntohs(message->size);
1259 if (size < sizeof(struct GNUNET_MESH_ManipulatePath)) {
1260 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1261 "received create path message too short\n");
1265 size -= sizeof(struct GNUNET_MESH_ManipulatePath);
1266 if (size < 2 * sizeof(struct GNUNET_PeerIdentity)) {
1267 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1268 "create path message lacks enough peers\n");
1271 if (size % sizeof(struct GNUNET_PeerIdentity)) {
1272 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1273 "create path message of wrong size\n");
1276 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1277 size /= sizeof(struct GNUNET_PeerIdentity);
1279 tid = ntohl(msg->tid);
1280 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1281 t = retrieve_tunnel(pi, tid);
1284 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Creating tunnel\n");
1285 t = GNUNET_malloc(sizeof(struct MeshTunnel));
1286 t->id.oid = GNUNET_PEER_intern(pi);
1288 t->peers = GNUNET_CONTAINER_multihashmap_create(32);
1290 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
1292 GNUNET_CONTAINER_multihashmap_put(tunnels,
1295 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1302 dest_peer_info = GNUNET_CONTAINER_multihashmap_get(peers,
1303 &pi[size - 1].hashPubKey);
1304 if (NULL == dest_peer_info) {
1305 dest_peer_info = GNUNET_malloc(sizeof(struct MeshPeerInfo));
1306 dest_peer_info->id = GNUNET_PEER_intern(&pi[size - 1]);
1307 dest_peer_info->state = MESH_PEER_WAITING;
1308 GNUNET_CONTAINER_multihashmap_put(peers,
1309 &pi[size - 1].hashPubKey,
1311 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1313 orig_peer_info = GNUNET_CONTAINER_multihashmap_get(peers, &pi->hashPubKey);
1314 if (NULL == orig_peer_info) {
1315 orig_peer_info = GNUNET_malloc(sizeof(struct MeshPeerInfo));
1316 orig_peer_info->id = GNUNET_PEER_intern(pi);
1317 orig_peer_info->state = MESH_PEER_WAITING;
1318 GNUNET_CONTAINER_multihashmap_put(peers,
1321 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1325 path = GNUNET_malloc(sizeof(struct MeshPath));
1326 path->length = size;
1327 path->peers = GNUNET_malloc(size * sizeof(GNUNET_PEER_Id));
1329 for (i = 0; i < size; i++) {
1330 path->peers[i] = GNUNET_PEER_intern(&pi[i]);
1331 if (path->peers[i] == myid) own_pos = i;
1333 if (own_pos == 0) { /* cannot be self, must be 'not found' */
1334 /* create path: self not found in path through self */
1337 /* FIXME error. destroy tunnel? leave for timeout? */
1340 if (own_pos == size - 1) { /* it is for us! */
1341 struct MeshDataDescriptor *info;
1344 add_path_to_origin(orig_peer_info, path); /* inverts path! */
1345 GNUNET_PEER_resolve(get_first_hop(path), &id); /* path is inverted :) */
1346 info = GNUNET_malloc(sizeof(struct MeshDataDescriptor));
1347 info->origin = &t->id;
1348 info->peer = GNUNET_CONTAINER_multihashmap_get(peers, &id.hashPubKey);
1349 GNUNET_assert(info->peer);
1350 for (j = 0; info->peer->core_transmit[j]; j++) {
1356 info->handler_n = j;
1357 info->peer->core_transmit[j] = GNUNET_CORE_notify_transmit_ready(
1361 GNUNET_TIME_UNIT_FOREVER_REL,
1363 sizeof(struct GNUNET_MessageHeader),
1364 &send_core_path_ack,
1367 add_path_to_peer(dest_peer_info, path);
1368 GNUNET_PEER_resolve(get_first_hop(path), &id);
1369 GNUNET_CORE_notify_transmit_ready(core_handle,
1372 GNUNET_TIME_UNIT_FOREVER_REL,
1374 sizeof(struct GNUNET_MessageHeader),
1375 &send_core_create_path_for_peer,
1383 * Core handler for mesh network traffic going from the origin to a peer
1385 * @param cls closure
1386 * @param message message
1387 * @param peer peer identity this notification is about
1388 * @param atsi performance data
1389 * @return GNUNET_OK to keep the connection open,
1390 * GNUNET_SYSERR to close it (signal serious error)
1393 handle_mesh_data_unicast (void *cls,
1394 const struct GNUNET_PeerIdentity *peer,
1395 const struct GNUNET_MessageHeader *message,
1396 const struct GNUNET_TRANSPORT_ATS_Information
1399 struct GNUNET_MESH_Unicast *msg;
1400 struct GNUNET_PeerIdentity id;
1401 struct MeshTunnel *t;
1402 struct MeshPeerInfo *pi;
1405 size = ntohs(message->size);
1406 if (size < sizeof(struct GNUNET_MESH_Unicast)
1407 + sizeof(struct GNUNET_MessageHeader))
1412 msg = (struct GNUNET_MESH_Unicast *) message;
1413 t = retrieve_tunnel(&msg->oid, ntohl(msg->tid));
1415 /* TODO notify back: we don't know this tunnel */
1418 pi = GNUNET_CONTAINER_multihashmap_get(t->peers,
1419 &msg->destination.hashPubKey);
1421 /* TODO maybe feedback, log to statistics */
1424 if (pi->id == myid) {
1425 send_subscribed_clients((struct GNUNET_MessageHeader *)&msg[1]);
1428 GNUNET_PEER_resolve(get_first_hop(pi->path), &id);
1429 msg = GNUNET_malloc(size);
1430 memcpy(msg, message, size);
1431 GNUNET_CORE_notify_transmit_ready(core_handle,
1434 GNUNET_TIME_UNIT_FOREVER_REL,
1437 &send_core_data_raw,
1444 * Core handler for mesh network traffic going from the origin to all peers
1446 * @param cls closure
1447 * @param message message
1448 * @param peer peer identity this notification is about
1449 * @param atsi performance data
1450 * @return GNUNET_OK to keep the connection open,
1451 * GNUNET_SYSERR to close it (signal serious error)
1454 handle_mesh_data_multicast (void *cls,
1455 const struct GNUNET_PeerIdentity *peer,
1456 const struct GNUNET_MessageHeader *message,
1457 const struct GNUNET_TRANSPORT_ATS_Information
1460 struct GNUNET_MESH_Multicast *msg;
1461 struct GNUNET_PeerIdentity id;
1462 struct MeshTunnel *t;
1463 struct MeshDataDescriptor *info;
1464 GNUNET_PEER_Id *neighbors;
1470 size = ntohs(message->size);
1471 if (size < sizeof(struct GNUNET_MESH_Multicast)
1472 + sizeof(struct GNUNET_MessageHeader))
1474 GNUNET_break_op (0);
1477 msg = (struct GNUNET_MESH_Multicast *) message;
1478 t = retrieve_tunnel(&msg->oid, ntohl(msg->tid));
1481 /* TODO notify that we dont know that tunnel */
1485 /* Transmit to locally interested clients */
1486 GNUNET_PEER_resolve(myid, &id);
1487 if (GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey)) {
1488 send_subscribed_clients((struct GNUNET_MessageHeader *)&msg[1]);
1491 /* Retransmit to other peers */
1492 neighbors = GNUNET_malloc(sizeof(GNUNET_PEER_Id));
1494 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
1495 &iterate_collect_neighbors,
1497 if (!neighbors[0]) {
1500 size -= sizeof(struct GNUNET_MESH_Multicast);
1501 info = GNUNET_malloc(sizeof(struct MeshDataDescriptor) + size);
1502 info->origin = &t->id;
1504 for (i = 0; 0 != neighbors[i]; i++) {
1505 GNUNET_PEER_resolve(neighbors[i], &id);
1507 info->destination = neighbors[i];
1508 info->peer = GNUNET_CONTAINER_multihashmap_get(peers, &id.hashPubKey);
1509 GNUNET_assert(info->peer);
1510 for (j = 0; info->peer->core_transmit[j]; j++) {
1516 info->handler_n = j;
1517 info->peer->infos[j] = info;
1518 info->peer->core_transmit[j] = GNUNET_CORE_notify_transmit_ready(
1522 GNUNET_TIME_UNIT_FOREVER_REL,
1524 ntohs(msg->header.size),
1525 &send_core_data_multicast,
1533 * Core handler for mesh network traffic
1535 * @param cls closure
1536 * @param message message
1537 * @param peer peer identity this notification is about
1538 * @param atsi performance data
1539 * @return GNUNET_OK to keep the connection open,
1540 * GNUNET_SYSERR to close it (signal serious error)
1543 handle_mesh_data_to_orig (void *cls,
1544 const struct GNUNET_PeerIdentity *peer,
1545 const struct GNUNET_MessageHeader *message,
1546 const struct GNUNET_TRANSPORT_ATS_Information
1549 struct GNUNET_MESH_ToOrigin *msg;
1550 struct GNUNET_PeerIdentity id;
1551 struct MeshTunnel *t;
1552 struct MeshPeerInfo *peer_info;
1555 size = ntohs(message->size);
1556 if (size < sizeof(struct GNUNET_MESH_ToOrigin)
1557 + sizeof(struct GNUNET_MessageHeader))
1559 GNUNET_break_op (0);
1562 msg = (struct GNUNET_MESH_ToOrigin *) message;
1563 t = retrieve_tunnel(&msg->oid, ntohl(msg->tid));
1566 /* TODO notify that we dont know this tunnel (whom)? */
1570 if (t->id.oid == myid) {
1571 if (NULL == t->client) {
1572 /* got data packet for ownerless tunnel */
1573 GNUNET_break_op (0);
1576 GNUNET_SERVER_notification_context_unicast(nc,
1582 peer_info = get_peer_info(&msg->oid);
1583 if (NULL == peer_info) {
1584 /* unknown origin of tunnel */
1588 GNUNET_PEER_resolve(get_first_hop(peer_info->path), &id);
1589 msg = GNUNET_malloc(size);
1590 memcpy(msg, message, size);
1591 GNUNET_CORE_notify_transmit_ready(core_handle,
1594 GNUNET_TIME_UNIT_FOREVER_REL,
1597 &send_core_data_raw,
1605 * Core handler for path ACKs
1607 * @param cls closure
1608 * @param message message
1609 * @param peer peer identity this notification is about
1610 * @param atsi performance data
1611 * @return GNUNET_OK to keep the connection open,
1612 * GNUNET_SYSERR to close it (signal serious error)
1615 handle_mesh_path_ack (void *cls,
1616 const struct GNUNET_PeerIdentity *peer,
1617 const struct GNUNET_MessageHeader *message,
1618 const struct GNUNET_TRANSPORT_ATS_Information
1621 struct GNUNET_MESH_PathACK *msg;
1622 struct GNUNET_PeerIdentity id;
1623 struct MeshTunnel *t;
1624 struct MeshPeerInfo *peer_info;
1626 msg = (struct GNUNET_MESH_PathACK *) message;
1627 t = retrieve_tunnel(&msg->oid, msg->tid);
1629 /* TODO notify that we don't know the tunnel */
1633 /* Message for us? */
1634 if (GNUNET_PEER_search(&msg->oid) == myid) {
1635 struct GNUNET_MESH_PeerControl pc;
1636 if (NULL == t->client) {
1640 peer_info = get_peer_info(&msg->peer_id);
1641 if (NULL == peer_info) {
1645 peer_info->state = MESH_PEER_READY;
1646 pc.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED);
1647 pc.header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
1648 pc.tunnel_id = htonl(t->local_tid);
1649 GNUNET_PEER_resolve(peer_info->id, &pc.peer);
1650 GNUNET_SERVER_notification_context_unicast(nc,
1657 peer_info = get_peer_info(&msg->oid);
1658 if (NULL == peer_info) {
1659 /* If we know the tunnel, we should DEFINITELY know the peer */
1663 GNUNET_PEER_resolve(get_first_hop(peer_info->path), &id);
1664 msg = GNUNET_malloc(sizeof(struct GNUNET_MESH_PathACK));
1665 memcpy(msg, message, sizeof(struct GNUNET_MESH_PathACK));
1666 GNUNET_CORE_notify_transmit_ready(core_handle,
1669 GNUNET_TIME_UNIT_FOREVER_REL,
1671 sizeof(struct GNUNET_MESH_PathACK),
1672 &send_core_data_raw,
1679 * Functions to handle messages from core
1681 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1682 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1683 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1684 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1685 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1686 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1687 sizeof(struct GNUNET_MESH_PathACK)},
1693 /******************************************************************************/
1694 /**************** MESH LOCAL HANDLER HELPERS ***********************/
1695 /******************************************************************************/
1698 * delete_tunnel_entry: iterator for deleting each tunnel that belongs to a
1699 * client when the client disconnects.
1700 * @param cls closure (client that is disconnecting)
1701 * @param key the hash of the local tunnel id (used to access the hashmap)
1702 * @param value the value stored at the key (tunnel to destroy)
1703 * @return GNUNET_OK on success
1706 delete_tunnel_entry (void *cls, const GNUNET_HashCode * key, void *value) {
1708 r = destroy_tunnel((struct MeshTunnel *) value);
1714 * notify_client_connection_failure: notify a client that the connection to the
1715 * requested remote peer is not possible (for instance, no route found)
1716 * Function called when the socket is ready to queue more data. "buf" will be
1717 * NULL and "size" zero if the socket was closed for writing in the meantime.
1719 * @param cls closure
1720 * @param size number of bytes available in buf
1721 * @param buf where the callee should write the message
1722 * @return number of bytes written to buf
1725 notify_client_connection_failure (void *cls, size_t size, void *buf)
1728 struct MeshPeerInfo *peer_info;
1729 struct GNUNET_MESH_PeerControl *msg;
1730 struct GNUNET_PeerIdentity id;
1732 if (0 == size && NULL == buf) {
1733 // TODO retry? cancel?
1737 size_needed = sizeof(struct GNUNET_MESH_PeerControl);
1738 peer_info = (struct MeshPeerInfo *) cls;
1739 msg = (struct GNUNET_MESH_PeerControl *) buf;
1740 msg->header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
1741 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
1742 // msg->tunnel_id = htonl(peer_info->t->tid);
1743 GNUNET_PEER_resolve(peer_info->id, &id);
1744 memcpy(&msg->peer, &id, sizeof(struct GNUNET_PeerIdentity));
1752 * Send keepalive packets for a peer
1758 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1760 struct MeshPeerInfo *peer_info = cls;
1761 struct GNUNET_PeerIdentity id;
1763 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) return;
1764 GNUNET_PEER_resolve(get_first_hop(peer_info->path), &id);
1765 GNUNET_CORE_notify_transmit_ready(core_handle,
1768 GNUNET_TIME_UNIT_FOREVER_REL,
1770 sizeof(struct GNUNET_MESH_ManipulatePath)
1771 + (peer_info->path->length
1772 * sizeof (struct GNUNET_PeerIdentity)),
1773 &send_core_create_path_for_peer,
1775 peer_info->path_refresh_task = GNUNET_SCHEDULER_add_delayed(
1784 * Function to process paths received for a new peer addition. The recorded
1785 * paths form the initial tunnel, which can be optimized later.
1786 * Called on each result obtained for the DHT search.
1788 * @param cls closure
1789 * @param exp when will this value expire
1790 * @param key key of the result
1791 * @param get_path NULL-terminated array of pointers
1792 * to the peers on reverse GET path (or NULL if not recorded)
1793 * @param put_path NULL-terminated array of pointers
1794 * to the peers on the PUT path (or NULL if not recorded)
1795 * @param type type of the result
1796 * @param size number of bytes in data
1797 * @param data pointer to the result data
1800 dht_get_id_handler(void *cls,
1801 struct GNUNET_TIME_Absolute exp,
1802 const GNUNET_HashCode * key,
1803 const struct GNUNET_PeerIdentity * const *get_path,
1804 const struct GNUNET_PeerIdentity * const *put_path,
1805 enum GNUNET_BLOCK_Type type,
1809 struct MeshPeerInfo *peer_info = cls;
1811 struct GNUNET_PeerIdentity pi;
1814 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path) {
1815 // Find ourselves some alternate initial path to the destination: retry
1816 GNUNET_DHT_get_stop(peer_info->dhtget);
1817 GNUNET_PEER_resolve(peer_info->id, &pi);
1818 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
1819 GNUNET_TIME_UNIT_FOREVER_REL,
1820 GNUNET_BLOCK_TYPE_ANY,
1822 4, /* replication level */
1823 GNUNET_DHT_RO_RECORD_ROUTE,
1824 NULL, /* bloom filter */
1827 0, /* xquery bits */
1832 p = GNUNET_malloc(sizeof(struct MeshPath));
1833 for (i = 0; get_path[i] != NULL; i++);
1834 for (i--; i >= 0; i--) {
1835 p->peers = GNUNET_realloc(p->peers,
1836 sizeof(GNUNET_PEER_Id) * (p->length + 1));
1837 p->peers[p->length] = GNUNET_PEER_intern(get_path[i]);
1840 for (i = 0; put_path[i] != NULL; i++);
1841 for (i--; i >= 0; i--) {
1842 p->peers = GNUNET_realloc(p->peers,
1843 sizeof(GNUNET_PEER_Id) * (p->length + 1));
1844 p->peers[p->length] = GNUNET_PEER_intern(put_path[i]);
1847 add_path_to_peer(peer_info, p);
1848 GNUNET_CORE_notify_transmit_ready(core_handle,
1851 GNUNET_TIME_UNIT_FOREVER_REL,
1853 sizeof(struct GNUNET_MESH_ManipulatePath)
1855 * sizeof (struct GNUNET_PeerIdentity)),
1856 &send_core_create_path_for_peer,
1858 if (0 == peer_info->path_refresh_task) {
1859 peer_info->path_refresh_task = GNUNET_SCHEDULER_add_delayed(
1869 * Function to process paths received for a new peer addition. The recorded
1870 * paths form the initial tunnel, which can be optimized later.
1871 * Called on each result obtained for the DHT search.
1873 * @param cls closure
1874 * @param exp when will this value expire
1875 * @param key key of the result
1876 * @param get_path NULL-terminated array of pointers
1877 * to the peers on reverse GET path (or NULL if not recorded)
1878 * @param put_path NULL-terminated array of pointers
1879 * to the peers on the PUT path (or NULL if not recorded)
1880 * @param type type of the result
1881 * @param size number of bytes in data
1882 * @param data pointer to the result data
1885 dht_get_type_handler(void *cls,
1886 struct GNUNET_TIME_Absolute exp,
1887 const GNUNET_HashCode * key,
1888 const struct GNUNET_PeerIdentity * const *get_path,
1889 const struct GNUNET_PeerIdentity * const *put_path,
1890 enum GNUNET_BLOCK_Type type,
1894 const struct GNUNET_PeerIdentity *pi = data;
1895 struct MeshTunnel *t = cls;
1896 struct MeshPeerInfo *peer_info;
1901 if (size != sizeof(struct GNUNET_PeerIdentity)) {
1905 peer_info = get_peer_info(pi);
1906 GNUNET_CONTAINER_multihashmap_put(t->peers,
1909 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1910 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path) {
1911 /* we don't have a route to the peer, let's try a direct lookup */
1912 if (NULL == peer_info->dhtget) {
1913 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
1914 GNUNET_TIME_UNIT_FOREVER_REL,
1915 GNUNET_BLOCK_TYPE_ANY,
1917 10U, /* replication level */
1918 GNUNET_DHT_RO_RECORD_ROUTE,
1919 NULL, /* bloom filter */
1922 0, /* xquery bits */
1928 p = GNUNET_malloc(sizeof(struct MeshPath));
1929 for (i = 0; get_path[i] != NULL; i++);
1930 for (i--; i >= 0; i--) {
1931 p->peers = GNUNET_realloc(p->peers,
1932 sizeof(GNUNET_PEER_Id) * (p->length + 1));
1933 p->peers[p->length] = GNUNET_PEER_intern(get_path[i]);
1936 for (i = 0; put_path[i] != NULL; i++);
1937 for (i--; i >= 0; i--) {
1938 p->peers = GNUNET_realloc(p->peers,
1939 sizeof(GNUNET_PEER_Id) * (p->length + 1));
1940 p->peers[p->length] = GNUNET_PEER_intern(put_path[i]);
1943 add_path_to_peer(peer_info, p);
1944 GNUNET_CORE_notify_transmit_ready(core_handle,
1947 GNUNET_TIME_UNIT_FOREVER_REL,
1949 sizeof(struct GNUNET_MESH_ManipulatePath)
1951 * sizeof (struct GNUNET_PeerIdentity)),
1952 &send_core_create_path_for_peer,
1957 /******************************************************************************/
1958 /********************* MESH LOCAL HANDLES **************************/
1959 /******************************************************************************/
1963 * Handler for client disconnection
1965 * @param cls closure
1966 * @param client identification of the client; NULL
1967 * for the last call when the server is destroyed
1970 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
1972 struct MeshClient *c;
1973 struct MeshClient *next;
1977 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1978 "client disconnected\n");
1981 if (c->handle == client) {
1982 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1983 " matching client found, cleaning\n");
1984 GNUNET_CONTAINER_multihashmap_iterate(c->tunnels,
1985 &delete_tunnel_entry,
1987 GNUNET_CONTAINER_multihashmap_destroy(c->tunnels);
1988 if(0 != c->app_counter) { /* deregister clients applications */
1989 for (i = 0; i < c->app_counter; i++) {
1990 for (j = 0; j < n_applications; j++) {
1991 if (c->apps[i] == applications[j]) {
1992 if (0 == --applications_rc[j]) {
1994 applications[n_applications -1];
1995 applications_rc[j] =
1996 applications_rc[n_applications -1];
1998 applications = GNUNET_realloc(
2001 sizeof(GNUNET_MESH_ApplicationType));
2002 applications_rc = GNUNET_realloc(
2005 sizeof(unsigned int));
2011 GNUNET_free (c->apps);
2012 if (0 == n_applications) {
2013 GNUNET_SCHEDULER_cancel(announce_applications_task);
2016 if(0 != c->type_counter) GNUNET_free (c->types);
2017 GNUNET_CONTAINER_DLL_remove(clients, clients_tail, c);
2022 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2023 " ... searching\n");
2028 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2035 * Handler for new clients
2037 * @param cls closure
2038 * @param client identification of the client
2039 * @param message the actual message, which includes messages the client wants
2042 handle_local_new_client (void *cls,
2043 struct GNUNET_SERVER_Client *client,
2044 const struct GNUNET_MessageHeader *message)
2046 struct GNUNET_MESH_ClientConnect *cc_msg;
2047 struct MeshClient *c;
2055 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
2056 /* Check data sanity */
2057 size = ntohs(message->size) - sizeof(struct GNUNET_MESH_ClientConnect);
2058 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2059 types = ntohs(cc_msg->types);
2060 apps = ntohs(cc_msg->applications);
2062 types * sizeof(uint16_t) + apps * sizeof(GNUNET_MESH_ApplicationType))
2065 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2069 /* Create new client structure */
2070 c = GNUNET_malloc(sizeof(struct MeshClient));
2073 c->type_counter = types;
2074 c->types = GNUNET_malloc(types * sizeof(uint16_t));
2075 memcpy(c->types, &message[1], types * sizeof(uint16_t));
2078 c->app_counter = apps;
2079 c->apps = GNUNET_malloc(apps * sizeof(GNUNET_MESH_ApplicationType));
2081 &message[1] + types * sizeof(uint16_t),
2082 apps * sizeof(GNUNET_MESH_ApplicationType));
2084 for (i = 0; i < apps; i++) {
2086 for (j = 0; i < n_applications; j++) {
2087 if (c->apps[i] == applications[j]) {
2089 applications_rc[j]++;
2093 if (!known) { /* Register previously unknown application */
2095 applications = GNUNET_realloc(applications,
2097 sizeof(GNUNET_MESH_ApplicationType));
2098 applications_rc = GNUNET_realloc(applications_rc,
2100 sizeof(unsigned int));
2101 applications[n_applications - 1] = c->apps[i];
2102 applications_rc[n_applications - 1] = 1;
2103 if (0 == announce_applications_task) {
2104 announce_applications_task = GNUNET_SCHEDULER_add_delayed(
2106 &announce_applications,
2111 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2112 " client has %u+%u subscriptions\n",
2116 GNUNET_CONTAINER_DLL_insert(clients, clients_tail, c);
2117 c->tunnels = GNUNET_CONTAINER_multihashmap_create(32);
2118 GNUNET_SERVER_notification_context_add(nc, client);
2120 GNUNET_SERVER_receive_done(client, GNUNET_OK);
2126 * Handler for requests of new tunnels
2128 * @param cls closure
2129 * @param client identification of the client
2130 * @param message the actual message
2133 handle_local_tunnel_create (void *cls,
2134 struct GNUNET_SERVER_Client *client,
2135 const struct GNUNET_MessageHeader *message)
2137 struct GNUNET_MESH_TunnelMessage *t_msg;
2138 struct MeshTunnel *t;
2139 struct MeshClient *c;
2140 GNUNET_HashCode hash;
2142 /* Sanity check for client registration */
2143 if (NULL == (c = retrieve_client(client))) {
2145 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2149 /* Message sanity check */
2150 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
2152 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2156 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2157 /* Sanity check for tunnel numbering */
2158 if (0 == (ntohl(t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)) {
2160 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2163 /* Sanity check for duplicate tunnel IDs */
2164 if(NULL != retrieve_tunnel_by_local_id(c, ntohl(t_msg->tunnel_id))) {
2166 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2170 t = GNUNET_malloc(sizeof(struct MeshTunnel));
2171 while (NULL != retrieve_tunnel_by_pi(myid, next_tid))
2172 next_tid = (next_tid + 1) % GNUNET_MESH_LOCAL_TUNNEL_ID_MARK;
2173 t->id.tid = next_tid++;
2175 t->local_tid = ntohl(t_msg->tunnel_id);
2177 t->peers = GNUNET_CONTAINER_multihashmap_create(32);
2179 GNUNET_CRYPTO_hash(&t->local_tid, sizeof(MESH_TunnelNumber), &hash);
2181 GNUNET_CONTAINER_multihashmap_put(c->tunnels, &hash, t,
2182 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2185 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2189 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
2191 GNUNET_CONTAINER_multihashmap_put(tunnels, &hash, t,
2192 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2195 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2199 GNUNET_SERVER_receive_done(client, GNUNET_OK);
2205 * Handler for requests of deleting tunnels
2207 * @param cls closure
2208 * @param client identification of the client
2209 * @param message the actual message
2212 handle_local_tunnel_destroy (void *cls,
2213 struct GNUNET_SERVER_Client *client,
2214 const struct GNUNET_MessageHeader *message)
2216 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2217 struct MeshClient *c;
2218 struct MeshTunnel *t;
2219 MESH_TunnelNumber tid;
2220 GNUNET_HashCode hash;
2223 /* Sanity check for client registration */
2224 if (NULL == (c = retrieve_client(client))) {
2226 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2229 /* Message sanity check */
2230 if (sizeof(struct GNUNET_MESH_TunnelMessage) != ntohs(message->size)) {
2232 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2236 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2238 /* Retrieve tunnel */
2239 tid = ntohl(tunnel_msg->tunnel_id);
2241 /* Remove from local id hashmap */
2242 GNUNET_CRYPTO_hash(&tid, sizeof(MESH_TunnelNumber), &hash);
2243 t = GNUNET_CONTAINER_multihashmap_get(c->tunnels, &hash);
2244 GNUNET_CONTAINER_multihashmap_remove(c->tunnels, &hash, t);
2246 /* Remove from global id hashmap */
2247 GNUNET_CRYPTO_hash(&t->id, sizeof(struct MESH_TunnelID), &hash);
2248 GNUNET_CONTAINER_multihashmap_remove(tunnels, &hash, t);
2250 // notify_tunnel_destroy(t);
2251 GNUNET_SERVER_receive_done(client, GNUNET_OK);
2257 * Handler for connection requests to new peers
2259 * @param cls closure
2260 * @param client identification of the client
2261 * @param message the actual message (PeerControl)
2264 handle_local_connect_add (void *cls,
2265 struct GNUNET_SERVER_Client *client,
2266 const struct GNUNET_MessageHeader *message)
2268 struct GNUNET_MESH_PeerControl *peer_msg;
2269 struct MeshClient *c;
2270 struct MeshTunnel *t;
2271 MESH_TunnelNumber tid;
2272 struct MeshPeerInfo *peer_info;
2275 /* Sanity check for client registration */
2276 if (NULL == (c = retrieve_client(client))) {
2278 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2282 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
2283 /* Sanity check for message size */
2284 if (sizeof(struct GNUNET_MESH_PeerControl)
2285 != ntohs(peer_msg->header.size))
2288 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2292 /* Tunnel exists? */
2293 tid = ntohl(peer_msg->tunnel_id);
2294 t = retrieve_tunnel_by_local_id(c, tid);
2297 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2301 /* Does client own tunnel? */
2302 if (t->client->handle != client) {
2304 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2309 peer_info = get_peer_info(&peer_msg->peer);
2311 /* Start DHT search if needed */
2312 if(MESH_PEER_READY != peer_info->state && NULL == peer_info->dhtget) {
2313 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle,
2314 GNUNET_TIME_UNIT_FOREVER_REL,
2315 GNUNET_BLOCK_TYPE_ANY,
2316 &peer_msg->peer.hashPubKey,
2317 4, /* replication level */
2318 GNUNET_DHT_RO_RECORD_ROUTE,
2319 NULL, /* bloom filter */
2322 0, /* xquery bits */
2327 GNUNET_SERVER_receive_done(client, GNUNET_OK);
2333 * Handler for disconnection requests of peers in a tunnel
2335 * @param cls closure
2336 * @param client identification of the client
2337 * @param message the actual message (PeerControl)
2340 handle_local_connect_del (void *cls,
2341 struct GNUNET_SERVER_Client *client,
2342 const struct GNUNET_MessageHeader *message)
2344 struct GNUNET_MESH_PeerControl *peer_msg;
2345 struct MeshClient *c;
2346 struct MeshTunnel *t;
2347 MESH_TunnelNumber tid;
2349 /* Sanity check for client registration */
2350 if (NULL == (c = retrieve_client(client))) {
2352 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2355 peer_msg = (struct GNUNET_MESH_PeerControl *)message;
2356 /* Sanity check for message size */
2357 if (sizeof(struct GNUNET_MESH_PeerControl)
2358 != ntohs(peer_msg->header.size))
2361 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2365 /* Tunnel exists? */
2366 tid = ntohl(peer_msg->tunnel_id);
2367 t = retrieve_tunnel_by_local_id(c, tid);
2370 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2374 /* Does client own tunnel? */
2375 if (t->client->handle != client) {
2377 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2381 /* Ok, delete peer from tunnel */
2382 GNUNET_CONTAINER_multihashmap_remove_all(t->peers,
2383 &peer_msg->peer.hashPubKey);
2385 GNUNET_SERVER_receive_done(client, GNUNET_OK);
2391 * Handler for connection requests to new peers by type
2393 * @param cls closure
2394 * @param client identification of the client
2395 * @param message the actual message (ConnectPeerByType)
2398 handle_local_connect_by_type (void *cls,
2399 struct GNUNET_SERVER_Client *client,
2400 const struct GNUNET_MessageHeader *message)
2402 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2403 struct MeshClient *c;
2404 struct MeshTunnel *t;
2405 GNUNET_HashCode hash;
2406 GNUNET_MESH_ApplicationType type;
2407 MESH_TunnelNumber tid;
2408 uint8_t buffer[12] = "MESH_APP";
2412 /* Sanity check for client registration */
2413 if (NULL == (c = retrieve_client(client))) {
2415 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2419 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *)message;
2420 /* Sanity check for message size */
2421 if (sizeof(struct GNUNET_MESH_PeerControl) !=
2422 ntohs(connect_msg->header.size))
2425 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2429 /* Tunnel exists? */
2430 tid = ntohl(connect_msg->tunnel_id);
2431 t = retrieve_tunnel_by_local_id(c, tid);
2434 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2438 /* Does client own tunnel? */
2439 if (t->client->handle != client) {
2441 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2445 /* Do WE have the service? */
2446 type = ntohl(connect_msg->type);
2447 for (i = 0; i < n_applications; i++) {
2448 if (applications[i] == type) {
2449 /* Yes! Fast forward, add ourselves to the tunnel and send the
2450 * good news to the client
2452 struct GNUNET_MESH_PeerControl pc;
2453 GNUNET_PEER_resolve(myid, &pc.peer);
2454 GNUNET_CONTAINER_multihashmap_put(t->peers,
2455 &pc.peer.hashPubKey,
2456 get_peer_info(&pc.peer),
2457 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2458 pc.header.size = htons(sizeof(struct GNUNET_MESH_PeerControl));
2460 htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED);
2461 pc.tunnel_id = htonl(t->local_tid);
2462 GNUNET_SERVER_notification_context_unicast(nc,
2468 /* Ok, lets find a peer offering the service */
2469 p = (uint32_t *) &buffer[8];
2470 *p = connect_msg->type; /* Already in Network Byte Order! */
2471 GNUNET_CRYPTO_hash(buffer, 12, &hash);
2472 if (c->dht_get_type) {
2473 GNUNET_DHT_get_stop(c->dht_get_type);
2475 c->dht_get_type = GNUNET_DHT_get_start(dht_handle,
2476 GNUNET_TIME_UNIT_FOREVER_REL,
2477 GNUNET_BLOCK_TYPE_ANY,
2480 GNUNET_DHT_RO_RECORD_ROUTE,
2485 &dht_get_type_handler,
2488 GNUNET_SERVER_receive_done(client, GNUNET_OK);
2494 * Handler for client traffic directed to one peer
2496 * @param cls closure
2497 * @param client identification of the client
2498 * @param message the actual message
2501 handle_local_unicast (void *cls,
2502 struct GNUNET_SERVER_Client *client,
2503 const struct GNUNET_MessageHeader *message)
2505 struct MeshClient *c;
2506 struct MeshTunnel *t;
2507 struct MeshPeerInfo *pi;
2508 struct GNUNET_MESH_Unicast *data_msg;
2509 struct GNUNET_PeerIdentity next_hop;
2510 struct MeshDataDescriptor *info;
2511 MESH_TunnelNumber tid;
2514 /* Sanity check for client registration */
2515 if (NULL == (c = retrieve_client(client))) {
2517 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2520 data_msg = (struct GNUNET_MESH_Unicast *)message;
2521 /* Sanity check for message size */
2522 if (sizeof(struct GNUNET_MESH_Unicast) + sizeof(struct GNUNET_MessageHeader)
2523 > ntohs(data_msg->header.size))
2526 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2530 /* Tunnel exists? */
2531 tid = ntohl(data_msg->tid);
2532 t = retrieve_tunnel_by_local_id(c, tid);
2535 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2539 /* Is it a local tunnel? Then, does client own the tunnel? */
2540 if (t->client->handle != NULL && t->client->handle != client) {
2542 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2546 pi = GNUNET_CONTAINER_multihashmap_get(t->peers,
2547 &data_msg->destination.hashPubKey);
2548 /* Is the selected peer in the tunnel? */
2551 * Are we SO nice that we automatically try to add him to the tunnel?
2554 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2557 if (pi->id == myid) {
2558 struct GNUNET_MESH_Unicast copy;
2559 memcpy(©, data_msg, sizeof(struct GNUNET_MESH_Unicast));
2561 handle_mesh_data_unicast(NULL, NULL, ©.header, NULL);
2564 GNUNET_PEER_resolve(get_first_hop(pi->path), &next_hop);
2565 data_size = ntohs(message->size) - sizeof(struct GNUNET_MESH_Unicast);
2566 info = GNUNET_malloc(sizeof(struct MeshDataDescriptor) + data_size);
2567 memcpy(&info[1], &data_msg[1], data_size);
2568 info->destination = pi->id;
2569 info->origin = &t->id;
2570 info->size = data_size;
2571 info->client = client;
2572 GNUNET_CORE_notify_transmit_ready(core_handle,
2575 GNUNET_TIME_UNIT_FOREVER_REL,
2577 /* FIXME re-check types */
2578 data_size + sizeof(struct GNUNET_MESH_Unicast),
2579 &send_core_data_unicast,
2585 * Handler for client traffic directed to all peers in a tunnel
2587 * @param cls closure
2588 * @param client identification of the client
2589 * @param message the actual message
2592 handle_local_multicast (void *cls,
2593 struct GNUNET_SERVER_Client *client,
2594 const struct GNUNET_MessageHeader *message)
2596 struct MeshClient *c;
2597 struct MeshTunnel *t;
2598 struct GNUNET_MESH_Multicast *data_msg;
2599 MESH_TunnelNumber tid;
2601 /* Sanity check for client registration */
2602 if (NULL == (c = retrieve_client(client))) {
2604 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2607 data_msg = (struct GNUNET_MESH_Multicast *)message;
2608 /* Sanity check for message size */
2609 if (sizeof(struct GNUNET_MESH_PeerControl)
2610 != ntohs(data_msg->header.size))
2613 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2617 /* Tunnel exists? */
2618 tid = ntohl(data_msg->tid);
2619 t = retrieve_tunnel_by_local_id(c, tid);
2622 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2626 /* Does client own tunnel? */
2627 if (t->client->handle != client) {
2629 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
2635 GNUNET_SERVER_receive_done(client, GNUNET_OK);
2640 * Functions to handle messages from clients
2642 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
2643 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2644 {&handle_local_tunnel_create, NULL,
2645 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2646 sizeof(struct GNUNET_MESH_TunnelMessage)},
2647 {&handle_local_tunnel_destroy, NULL,
2648 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2649 sizeof(struct GNUNET_MESH_TunnelMessage)},
2650 {&handle_local_connect_add, NULL,
2651 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD,
2652 sizeof(struct GNUNET_MESH_PeerControl)},
2653 {&handle_local_connect_del, NULL,
2654 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL,
2655 sizeof(struct GNUNET_MESH_PeerControl)},
2656 {&handle_local_connect_by_type, NULL,
2657 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE,
2658 sizeof(struct GNUNET_MESH_ConnectPeerByType)},
2659 {&handle_local_unicast, NULL,
2660 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2661 {&handle_local_unicast, NULL,
2662 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2663 {&handle_local_multicast, NULL,
2664 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2670 * To be called on core init/fail.
2672 * @param cls service closure
2673 * @param server handle to the server for this service
2674 * @param identity the public identity of this peer
2675 * @param publicKey the public key of this peer
2678 core_init (void *cls,
2679 struct GNUNET_CORE_Handle *server,
2680 const struct GNUNET_PeerIdentity *identity,
2681 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2685 core_handle = server;
2686 myid = GNUNET_PEER_intern(identity);
2687 announce_id_task = GNUNET_SCHEDULER_add_now(&announce_id, cls);
2692 * Method called whenever a given peer connects.
2694 * @param cls closure
2695 * @param peer peer identity this notification is about
2696 * @param atsi performance data for the connection
2699 core_connect (void *cls,
2700 const struct GNUNET_PeerIdentity *peer,
2701 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2703 // GNUNET_PEER_Id pid;
2704 struct MeshPeerInfo *peer_info;
2705 struct MeshPath *path;
2707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2708 "Peer connected\n");
2709 peer_info = get_peer_info(peer);
2710 if (myid == peer_info->id) {
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2714 path = GNUNET_malloc(sizeof(struct MeshPath));
2716 path->peers = GNUNET_malloc(sizeof(GNUNET_PEER_Id) * 2);
2717 path->peers[0] = myid;
2718 path->peers[1] = peer_info->id;
2719 add_path_to_peer(peer_info, path);
2724 * Method called whenever a peer disconnects.
2726 * @param cls closure
2727 * @param peer peer identity this notification is about
2730 core_disconnect (void *cls,
2732 GNUNET_PeerIdentity *peer)
2734 struct MeshPeerInfo *pi;
2737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2738 "Peer disconnected\n");
2739 pi = GNUNET_CONTAINER_multihashmap_get(peers, &peer->hashPubKey);
2744 for (i = 0; i < CORE_QUEUE_SIZE; i++) {
2745 if (pi->core_transmit[i]) {
2746 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit[i]);
2747 /* TODO: notify that tranmission has failed */
2748 GNUNET_free(pi->infos[i]);
2751 if (myid == pi->id) {
2752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2759 /******************************************************************************/
2760 /************************ MAIN FUNCTIONS ****************************/
2761 /******************************************************************************/
2764 * Task run during shutdown.
2770 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2774 if (core_handle != NULL) {
2775 GNUNET_CORE_disconnect (core_handle);
2778 if (dht_handle != NULL) {
2779 GNUNET_DHT_disconnect (dht_handle);
2783 GNUNET_SERVER_notification_context_destroy(nc);
2786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2791 * Process mesh requests.
2793 * @param cls closure
2794 * @param server the initialized server
2795 * @param c configuration to use
2799 struct GNUNET_SERVER_Handle *server,
2800 const struct GNUNET_CONFIGURATION_Handle *c)
2802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2803 "starting to run\n");
2804 GNUNET_SERVER_add_handlers (server, plugin_handlers);
2805 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
2806 server_handle = server;
2807 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
2808 CORE_QUEUE_SIZE, /* queue size */
2809 NULL, /* Closure passed to MESH functions */
2810 &core_init, /* Call core_init once connected */
2811 &core_connect, /* Handle connects */
2812 &core_disconnect, /* remove peers on disconnects */
2813 NULL, /* Do we care about "status" updates? */
2814 NULL, /* Don't notify about all incoming messages */
2815 GNUNET_NO, /* For header only in notification */
2816 NULL, /* Don't notify about all outbound messages */
2817 GNUNET_NO, /* For header-only out notification */
2818 core_handlers); /* Register these handlers */
2819 if (core_handle == NULL) {
2822 dht_handle = GNUNET_DHT_connect(c, 64);
2823 if (dht_handle == NULL) {
2824 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2825 "Error connecting to DHT.\
2826 Running without DHT has a severe\
2827 impact in MESH capabilities.\n\
2828 Plase check your configuretion and enable DHT.\n");
2834 tunnels = GNUNET_CONTAINER_multihashmap_create(32);
2835 peers = GNUNET_CONTAINER_multihashmap_create(32);
2836 nc = GNUNET_SERVER_notification_context_create(server_handle,
2839 clients_tail = NULL;
2841 applications = NULL;
2842 applications_rc = NULL;
2844 announce_applications_task = 0;
2846 /* Scheduled the task to clean up when shutdown is called */
2847 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2848 &shutdown_task, NULL);
2850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2855 * The main function for the mesh service.
2857 * @param argc number of arguments from the command line
2858 * @param argv command line arguments
2859 * @return 0 ok, 1 on error
2862 main (int argc, char *const *argv)
2867 GNUNET_SERVICE_run (argc,
2870 GNUNET_SERVICE_OPTION_NONE,
2871 &run, NULL)) ? 0 : 1;
2872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,