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"
58 #define MESH_DEBUG GNUNET_YES
62 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
65 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
69 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
73 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s", s);
77 /* TODO: move into configuration file */
78 #define CORE_QUEUE_SIZE 10
79 #define LOCAL_QUEUE_SIZE 100
80 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
81 GNUNET_TIME_UNIT_SECONDS,\
83 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
84 GNUNET_TIME_UNIT_SECONDS,\
87 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
88 GNUNET_TIME_UNIT_SECONDS,\
93 /******************************************************************************/
94 /************************ DATA STRUCTURES ****************************/
95 /******************************************************************************/
98 * Information regarding a path
106 struct MeshPath *next;
107 struct MeshPath *prev;
110 * Whether the path is serving traffic in a tunnel or is a backup
115 * List of all the peers that form the path from origin to target
117 GNUNET_PEER_Id *peers;
120 * Number of peers (hops) in the path
127 * All the states a peer participating in a tunnel can be in.
132 * Path to the peer not known yet
137 * Request sent, not yet answered.
142 * Peer connected and ready to accept data
147 * Peer connected previosly but not responding
149 MESH_PEER_RECONNECTING
153 /** FWD declaration */
157 * Struct containing all info possibly needed to build a package when called
160 struct MeshDataDescriptor
162 /** ID of the tunnel this packet travels in */
163 struct MESH_TunnelID *origin;
165 /** Ultimate destination of the packet */
166 GNUNET_PEER_Id destination;
168 /** Number of identical messages sent to different hops (multicast) */
171 /** Size of the data */
174 /** Client that asked for the transmission, if any */
175 struct GNUNET_SERVER_Client *client;
177 /** Who was is message being sent to */
178 struct MeshPeerInfo *peer;
180 /** Which handler was used to request the transmission */
181 unsigned int handler_n;
183 /* Data at the end */
188 * Struct containing all information regarding a given peer
198 * Is the peer reachable? Is the peer even connected?
200 enum MeshPeerState state;
203 * Last time we heard from this peer
205 struct GNUNET_TIME_Absolute last_contact;
208 * Number of attempts to reconnect so far
210 int n_reconnect_attempts;
213 * Paths to reach the peer
215 struct MeshPath *path;
216 struct MeshPath *path_tail;
219 * Handle to stop the DHT search for a path to this peer
221 struct GNUNET_DHT_GetHandle *dhtget;
224 * Handles to stop queued transmissions for this peer
226 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
229 * Pointer to info stuctures used as cls for queued transmissions
231 struct MeshDataDescriptor *infos[CORE_QUEUE_SIZE];
234 * Task to send keepalive packets over the current active path
236 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
241 * Data scheduled to transmit (to local client or remote peer)
248 struct MeshQueue *next;
249 struct MeshQueue *prev;
252 * Target of the data (NULL if target is client)
254 struct MeshPeerInfo *peer;
257 * Client to send the data to (NULL if target is peer)
259 struct MeshClient *client;
262 * Size of the message to transmit
267 * How old is the data?
269 struct GNUNET_TIME_Absolute timestamp;
274 struct GNUNET_MessageHeader *data;
278 * Globally unique tunnel identification (owner + number)
279 * DO NOT USE OVER THE NETWORK
284 * Node that owns the tunnel
289 * Tunnel number to differentiate all the tunnels owned by the node oid
290 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
292 MESH_TunnelNumber tid;
296 struct MeshClient; /* FWD declaration */
299 * Struct containing all information regarding a tunnel
300 * For an intermediate node the improtant info used will be:
301 * - id Tunnel unique identification
302 * - paths[0] To know where to send it next
303 * - metainfo: ready, speeds, accounting
310 struct MESH_TunnelID id;
313 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
315 MESH_TunnelNumber local_tid;
318 * Last time the tunnel was used
320 struct GNUNET_TIME_Absolute timestamp;
323 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
325 struct GNUNET_CONTAINER_MultiHashMap *peers;
328 * Number of peers that are connected and potentially ready to receive data
330 unsigned int peers_ready;
333 * Number of peers that have been added to the tunnel
335 unsigned int peers_total;
339 * Client owner of the tunnel, if any
341 struct MeshClient *client;
344 * Messages ready to transmit
346 struct MeshQueue *queue_head;
347 struct MeshQueue *queue_tail;
352 * Struct containing information about a client of the service
359 struct MeshClient *next;
360 struct MeshClient *prev;
363 * Tunnels that belong to this client, indexed by local id
365 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
368 * Handle to communicate with the client
370 struct GNUNET_SERVER_Client *handle;
373 * Applications that this client has claimed to provide
375 GNUNET_MESH_ApplicationType *apps;
376 unsigned int app_counter;
379 * Messages that this client has declared interest in
382 unsigned int type_counter;
385 * Used for seachching peers offering a service
387 struct GNUNET_DHT_GetHandle *dht_get_type;
391 /******************************************************************************/
392 /*********************** GLOBAL VARIABLES ****************************/
393 /******************************************************************************/
398 static struct MeshClient *clients;
399 static struct MeshClient *clients_tail;
402 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
404 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
407 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
409 static struct GNUNET_CONTAINER_MultiHashMap *peers;
412 * Handle to communicate with core
414 static struct GNUNET_CORE_Handle *core_handle;
419 static struct GNUNET_DHT_Handle *dht_handle;
424 static struct GNUNET_SERVER_Handle *server_handle;
427 * Notification context, to send messages to local clients
429 static struct GNUNET_SERVER_NotificationContext *nc;
432 * Local peer own ID (memory efficient handle)
434 static GNUNET_PEER_Id myid;
437 * Local peer own ID (full value)
439 static struct GNUNET_PeerIdentity my_full_id;
442 * Tunnel ID for the next created tunnel (global tunnel number)
444 static MESH_TunnelNumber next_tid;
447 * All application types provided by this peer
449 static GNUNET_MESH_ApplicationType *applications;
452 * All application types provided by this peer (reference counter)
454 static unsigned int *applications_rc;
457 * Number of applications provided by this peer
459 static unsigned int n_apps;
462 * Task to periodically announce provided applications
464 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
467 * Task to periodically announce itself in the network
469 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
472 /******************************************************************************/
473 /****************** GENERAL HELPER FUNCTIONS ************************/
474 /******************************************************************************/
477 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
478 * and inster it in the appropiate structures if the peer is not known yet.
479 * @param peer Identity of the peer
480 * @return Existing or newly created peer info
482 static struct MeshPeerInfo *
483 get_peer_info (const struct GNUNET_PeerIdentity *peer)
485 struct MeshPeerInfo *peer_info;
487 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
488 if (NULL == peer_info)
491 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
492 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
493 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
494 peer_info->id = GNUNET_PEER_intern (peer);
495 peer_info->state = MESH_PEER_SEARCHING;
502 * Find the first peer whom to send a packet to go down this path
503 * @param path The path to use
504 * @return short id of the next peer, myid in case of local delivery,
505 * or 0 in case of error
507 static GNUNET_PEER_Id
508 get_first_hop (struct MeshPath *path)
520 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
521 "tried to get the next hop from an invalid path\n");
525 for (i = 0; i < path->length; i++)
527 if (path->peers[i] == myid)
529 if (i < path->length - 1)
531 return path->peers[i + 1];
544 * Get the cost of the path.
545 * @param path The path to analyze
546 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
550 get_path_cost (struct MeshPath *path)
556 for (i = 0; i < path->length; i++)
558 if (path->peers[i] == myid)
560 return path->length - i;
568 * Add the path to the peer and update the path used to reach it in case this
570 * @param peer_info Destination peer to add the path to.
571 * @param path New path to add. Last peer must be the peer in arg 1.
574 add_path_to_peer (struct MeshPeerInfo *peer_info, struct MeshPath *path)
577 unsigned int new_cost;
578 unsigned int best_cost;
579 struct MeshPath *aux;
580 struct MeshPath *best;
582 if (NULL == peer_info || NULL == path)
585 new_cost = get_path_cost (path);
586 best_cost = UINT_MAX;
588 for (aux = peer_info->path; aux != NULL; aux = aux->next)
590 if ((i = get_path_cost (aux)) < best_cost)
596 if (best_cost < new_cost)
599 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path, peer_info->path_tail,
607 GNUNET_CONTAINER_DLL_insert (peer_info->path, peer_info->path_tail, path);
614 * Add the path to the peer and update the path used to reach it in case this
615 * is the shortest. The path is given in reverse, the destination peer is
616 * path[0]. The function modifies the path, inverting it to use the origin as
618 * @param peer_info Destination peer to add the path to.
619 * @param path New path to add. First peer must be the peer in arg 1.
622 add_path_to_origin (struct MeshPeerInfo *peer_info, struct MeshPath *path)
627 for (i = 0; i < path->length / 2; i++)
629 aux = path->peers[i];
630 path->peers[i] = path->peers[path->length - i - 1];
631 path->peers[path->length - i - 1] = aux;
633 add_path_to_peer (peer_info, path);
638 * Check if client has registered with the service and has not disconnected
639 * @param client the client to check
640 * @return non-NULL if client exists in the global DLL
642 static struct MeshClient *
643 retrieve_client (struct GNUNET_SERVER_Client *client)
645 struct MeshClient *c;
650 if (c->handle == client)
659 * Checks if a given client has subscribed to certain message type
660 * @param message_type Type of message to check
661 * @param c Client to check
662 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
664 static int /* FIXME inline? */
665 is_client_subscribed (uint16_t message_type, struct MeshClient *c)
669 for (i = 0; i < c->type_counter; i++)
671 if (c->types[i] == message_type)
679 * Search for a tunnel among the tunnels for a client
680 * @param client the client whose tunnels to search in
681 * @param tid the local id of the tunnel
682 * @return tunnel handler, NULL if doesn't exist
684 static struct MeshTunnel *
685 retrieve_tunnel_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
687 GNUNET_HashCode hash;
689 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
690 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
694 * Search for a tunnel by global ID using PEER_ID
695 * @param pi owner of the tunnel
696 * @param tid global tunnel number
697 * @return tunnel handler, NULL if doesn't exist
699 static struct MeshTunnel *
700 retrieve_tunnel_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
702 struct MESH_TunnelID id;
703 GNUNET_HashCode hash;
708 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
709 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
715 * Search for a tunnel by global ID using full PeerIdentities
716 * @param oid owner of the tunnel
717 * @param tid global tunnel number
718 * @return tunnel handler, NULL if doesn't exist
720 static struct MeshTunnel *
721 retrieve_tunnel (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
723 return retrieve_tunnel_by_pi (GNUNET_PEER_search (oid), tid);
728 * Destroy the path and free any allocated resources linked to it
729 * @param t tunnel the path belongs to
730 * @param p the path to destroy
731 * @return GNUNET_OK on success
734 destroy_path (struct MeshPath *p)
736 GNUNET_PEER_decrement_rcs (p->peers, p->length);
737 GNUNET_free (p->peers);
744 * Destroy the peer_info and free any allocated resources linked to it
745 * @param t tunnel the path belongs to
746 * @param pi the peer_info to destroy
747 * @return GNUNET_OK on success
750 destroy_peer_info (struct MeshPeerInfo *pi)
752 GNUNET_HashCode hash;
753 struct GNUNET_PeerIdentity id;
755 GNUNET_PEER_resolve (pi->id, &id);
756 GNUNET_PEER_change_rc (pi->id, -1);
757 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
759 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
760 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
768 * Destroy the tunnel and free any allocated resources linked to it
769 * @param c client the tunnel belongs to
770 * @param t the tunnel to destroy
771 * @return GNUNET_OK on success
774 destroy_tunnel (struct MeshTunnel *t)
776 struct MeshClient *c;
778 struct MeshQueue *qn;
779 GNUNET_HashCode hash;
787 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
788 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
793 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
794 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
798 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
803 GNUNET_free (q->data);
807 /* TODO cancel core transmit ready in case it was active */
813 /******************************************************************************/
814 /************************ PERIODIC FUNCTIONS ****************************/
815 /******************************************************************************/
818 * Periodically announce what applications are provided by local clients
821 * @param tc task context
824 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
826 GNUNET_HashCode hash;
827 uint8_t buffer[12] = "MESH_APP";
831 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
833 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for %u apps\n",
838 p = (unsigned int *) &buffer[8];
839 for (i = 0; i < n_apps; i++)
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for app %u\n",
843 *p = htonl (applications[i]);
844 GNUNET_CRYPTO_hash (buffer, 12, &hash);
845 GNUNET_DHT_put (dht_handle, &hash, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
846 GNUNET_BLOCK_TYPE_ANY, sizeof (struct GNUNET_PeerIdentity),
847 (const char *) &my_full_id,
848 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
852 &mesh_debug, "DHT_put for apps completed\n");
857 announce_applications_task =
858 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
865 * Periodically announce self id in the DHT
868 * @param tc task context
871 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
873 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
875 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
879 * - Set data expiration in function of X
882 GNUNET_DHT_put (dht_handle, /* DHT handle */
883 &my_full_id.hashPubKey, /* Key to use */
884 10U, /* Replication level */
885 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
886 GNUNET_BLOCK_TYPE_ANY, /* Block type */
887 0, /* Size of the data */
888 NULL, /* Data itself */
889 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
890 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
892 &mesh_debug, "DHT_put for id completed\n");
894 NULL, /* Continuation */
895 NULL); /* Continuation closure */
898 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
901 /******************************************************************************/
902 /**************** MESH NETWORK HANDLER HELPERS ***********************/
903 /******************************************************************************/
906 * Function called to notify a client about the socket
907 * being ready to queue more data. "buf" will be
908 * NULL and "size" zero if the socket was closed for
909 * writing in the meantime.
912 * @param size number of bytes available in buf
913 * @param buf where the callee should write the message
914 * @return number of bytes written to buf
917 send_core_create_path_for_peer (void *cls, size_t size, void *buf)
919 struct MeshPeerInfo *peer_info = cls;
920 struct GNUNET_MESH_ManipulatePath *msg;
922 struct GNUNET_PeerIdentity *peer_ptr;
923 struct GNUNET_PeerIdentity id;
927 if (0 == size && NULL == buf)
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
930 GNUNET_PEER_resolve (get_first_hop (peer_info->path), &id);
931 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
932 GNUNET_TIME_UNIT_FOREVER_REL, &id,
934 GNUNET_MESH_ManipulatePath) +
935 (peer_info->path->length *
936 sizeof (struct GNUNET_PeerIdentity)),
937 &send_core_create_path_for_peer,
951 return 0; // TODO Notify ERROR Path not found
954 sizeof (struct GNUNET_MESH_ManipulatePath) +
955 p->length * sizeof (struct GNUNET_PeerIdentity);
956 if (size < size_needed)
958 // TODO retry? cancel?
962 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
963 msg->header.size = htons (size_needed);
964 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
966 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
967 for (i = 0; i < p->length; i++)
969 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
972 peer_info->state = MESH_PEER_WAITING;
980 * Function called to notify a client about the socket
981 * being ready to queue more data. "buf" will be
982 * NULL and "size" zero if the socket was closed for
983 * writing in the meantime.
985 * @param cls closure (MeshDataDescriptor with all info to build packet)
986 * @param size number of bytes available in buf
987 * @param buf where the callee should write the message
988 * @return number of bytes written to buf
991 send_core_data_to_origin (void *cls, size_t size, void *buf)
993 struct MeshDataDescriptor *info = cls;
994 struct GNUNET_MESH_ToOrigin *msg = buf;
997 GNUNET_assert (NULL != info);
998 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
999 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1001 if (total_size > size)
1003 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1004 "not enough buffer to send data to origin\n");
1007 msg->header.size = htons (total_size);
1008 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1009 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1010 msg->tid = htonl (info->origin->tid);
1011 if (0 != info->size)
1013 memcpy (&msg[1], &info[1], info->size);
1015 if (NULL != info->client)
1017 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1025 * Function called to notify a client about the socket
1026 * being ready to queue more data. "buf" will be
1027 * NULL and "size" zero if the socket was closed for
1028 * writing in the meantime.
1030 * @param cls closure (data itself)
1031 * @param size number of bytes available in buf
1032 * @param buf where the callee should write the message
1033 * @return number of bytes written to buf
1036 send_core_data_unicast (void *cls, size_t size, void *buf)
1038 struct MeshDataDescriptor *info = cls;
1039 struct GNUNET_MESH_Unicast *msg = buf;
1042 GNUNET_assert (NULL != info);
1043 total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
1044 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1046 if (total_size > size)
1048 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1049 "not enough buffer to send data to peer\n");
1052 msg->header.size = htons (total_size);
1053 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1054 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1055 GNUNET_PEER_resolve (info->destination, &msg->destination);
1056 msg->tid = htonl (info->origin->tid);
1057 if (0 != info->size)
1059 memcpy (&msg[1], &info[1], info->size);
1061 if (NULL != info->client)
1063 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1071 * Function called to notify a client about the socket
1072 * being ready to queue more data. "buf" will be
1073 * NULL and "size" zero if the socket was closed for
1074 * writing in the meantime.
1076 * @param cls closure (data itself)
1077 * @param size number of bytes available in buf
1078 * @param buf where the callee should write the message
1079 * @return number of bytes written to buf
1082 send_core_data_multicast (void *cls, size_t size, void *buf)
1084 struct MeshDataDescriptor *info = cls;
1085 struct GNUNET_MESH_Multicast *msg = buf;
1088 GNUNET_assert (NULL != info);
1089 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1090 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1094 info->peer->core_transmit[info->handler_n] = NULL;
1096 if (total_size > size)
1098 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1099 "not enough buffer to send data futher\n");
1102 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1103 msg->header.size = htons (total_size);
1104 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1105 msg->tid = htonl (info->origin->tid);
1106 memcpy (&msg[1], &info[1], total_size);
1107 if (0 == --info->copies)
1109 if (NULL != info->client)
1111 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1120 * Function called to notify a client about the socket
1121 * being ready to queue more data. "buf" will be
1122 * NULL and "size" zero if the socket was closed for
1123 * writing in the meantime.
1125 * @param cls closure (MeshDataDescriptor)
1126 * @param size number of bytes available in buf
1127 * @param buf where the callee should write the message
1128 * @return number of bytes written to buf
1131 send_core_path_ack (void *cls, size_t size, void *buf)
1133 struct MeshDataDescriptor *info = cls;
1134 struct GNUNET_MESH_PathACK *msg = buf;
1136 GNUNET_assert (NULL != info);
1139 info->peer->core_transmit[info->handler_n] = NULL;
1141 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1146 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1147 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1148 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1149 msg->tid = htonl (info->origin->tid);
1150 msg->peer_id = my_full_id;
1151 /* TODO add signature */
1153 return sizeof (struct GNUNET_MESH_PathACK);
1158 * Function called to notify a client about the socket
1159 * being ready to queue more data. "buf" will be
1160 * NULL and "size" zero if the socket was closed for
1161 * writing in the meantime.
1163 * @param cls closure (data itself)
1164 * @param size number of bytes available in buf
1165 * @param buf where the callee should write the message
1166 * @return number of bytes written to buf
1169 send_core_data_raw (void *cls, size_t size, void *buf)
1171 struct GNUNET_MessageHeader *msg = cls;
1174 GNUNET_assert (NULL != msg);
1175 total_size = ntohs (msg->size);
1177 if (total_size > size)
1182 memcpy (buf, msg, total_size);
1190 * Send another peer a notification to destroy a tunnel
1191 * @param cls The tunnel to destroy
1192 * @param size Size in the buffer
1193 * @param buf Memory where to put the data to transmit
1194 * @return Size of data put in buffer
1197 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1199 struct MeshTunnel *t = cls;
1200 struct MeshClient *c;
1201 struct GNUNET_MESH_TunnelMessage *msg;
1205 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1206 /*FIXME*/ msg->header.size =
1207 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1208 msg->tunnel_id = htonl (t->id.tid);
1210 destroy_tunnel (c, t);
1211 return sizeof (struct GNUNET_MESH_TunnelMessage);
1217 * Send the message to all clients that have subscribed to its type
1219 * @param msg Pointer to the message itself
1220 * @return number of clients this message was sent to
1223 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1225 struct MeshClient *c;
1229 type = ntohs (msg->type);
1230 for (count = 0, c = clients; c != NULL; c = c->next)
1232 if (is_client_subscribed (type, c))
1235 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1244 * Iterator over hash map peer entries collect all neighbors who to resend the
1247 * @param cls closure (**GNUNET_PEER_Id to store hops to send packet)
1248 * @param key current key code (peer id hash)
1249 * @param value value in the hash map (peer_info)
1250 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
1253 iterate_collect_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
1255 struct MeshPeerInfo *peer_info = value;
1256 GNUNET_PEER_Id **neighbors = cls;
1260 if (peer_info->id == myid)
1264 id = get_first_hop (peer_info->path);
1265 for (i = 0; *neighbors[i] != 0; i++)
1267 if (*neighbors[i] == id)
1270 *neighbors = GNUNET_realloc (*neighbors, (i + 2) * sizeof (GNUNET_PEER_Id));
1272 *neighbors[i + 1] = 0;
1278 /******************************************************************************/
1279 /******************** MESH NETWORK HANDLERS **************************/
1280 /******************************************************************************/
1284 * Core handler for path creation
1285 * struct GNUNET_CORE_MessageHandler
1287 * @param cls closure
1288 * @param message message
1289 * @param peer peer identity this notification is about
1290 * @param atsi performance data
1291 * @return GNUNET_OK to keep the connection open,
1292 * GNUNET_SYSERR to close it (signal serious error)
1296 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1297 const struct GNUNET_MessageHeader *message,
1298 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1300 unsigned int own_pos;
1303 MESH_TunnelNumber tid;
1304 struct GNUNET_MESH_ManipulatePath *msg;
1305 struct GNUNET_PeerIdentity *pi;
1306 struct GNUNET_PeerIdentity id;
1307 GNUNET_HashCode hash;
1308 struct MeshPath *path;
1309 struct MeshPeerInfo *dest_peer_info;
1310 struct MeshPeerInfo *orig_peer_info;
1311 struct MeshTunnel *t;
1313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1314 "MESH: Received a MESH path create msg\n");
1315 size = ntohs (message->size);
1316 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1318 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1319 "received create path message too short\n");
1323 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1324 if (size < 2 * sizeof (struct GNUNET_PeerIdentity))
1326 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1327 "create path message lacks enough peers\n");
1330 if (size % sizeof (struct GNUNET_PeerIdentity))
1332 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1333 "create path message of wrong size\n");
1336 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1337 size /= sizeof (struct GNUNET_PeerIdentity);
1339 tid = ntohl (msg->tid);
1340 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1341 t = retrieve_tunnel (pi, tid);
1345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1346 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1347 t->id.oid = GNUNET_PEER_intern (pi);
1349 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1351 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1353 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1354 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1362 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1363 if (NULL == dest_peer_info)
1365 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1366 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1367 dest_peer_info->state = MESH_PEER_WAITING;
1368 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1370 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1372 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1373 if (NULL == orig_peer_info)
1375 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1376 orig_peer_info->id = GNUNET_PEER_intern (pi);
1377 orig_peer_info->state = MESH_PEER_WAITING;
1378 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1379 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1383 path = GNUNET_malloc (sizeof (struct MeshPath));
1384 path->length = size;
1385 path->peers = GNUNET_malloc (size * sizeof (GNUNET_PEER_Id));
1387 for (i = 0; i < size; i++)
1389 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1390 if (path->peers[i] == myid)
1394 { /* cannot be self, must be 'not found' */
1395 /* create path: self not found in path through self */
1396 GNUNET_break_op (0);
1397 destroy_path (path);
1398 /* FIXME error. destroy tunnel? leave for timeout? */
1401 if (own_pos == size - 1)
1402 { /* it is for us! */
1403 struct MeshDataDescriptor *info;
1406 add_path_to_origin (orig_peer_info, path); /* inverts path! */
1407 GNUNET_PEER_resolve (get_first_hop (path), &id); /* path is inverted :) */
1408 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1409 info->origin = &t->id;
1410 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1411 GNUNET_assert (info->peer);
1412 for (j = 0; info->peer->core_transmit[j]; j++)
1420 info->handler_n = j;
1421 info->peer->core_transmit[j] =
1422 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1423 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1424 sizeof (struct GNUNET_MessageHeader),
1425 &send_core_path_ack, info);
1429 add_path_to_peer (dest_peer_info, path);
1430 GNUNET_PEER_resolve (get_first_hop (path), &id);
1431 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1432 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1433 sizeof (struct GNUNET_MessageHeader),
1434 &send_core_create_path_for_peer,
1442 * Core handler for mesh network traffic going from the origin to a peer
1444 * @param cls closure
1445 * @param peer peer identity this notification is about
1446 * @param message message
1447 * @param atsi performance data
1448 * @return GNUNET_OK to keep the connection open,
1449 * GNUNET_SYSERR to close it (signal serious error)
1452 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1453 const struct GNUNET_MessageHeader *message,
1454 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1456 struct GNUNET_MESH_Unicast *msg;
1457 struct GNUNET_PeerIdentity id;
1458 struct MeshTunnel *t;
1459 struct MeshPeerInfo *pi;
1462 size = ntohs (message->size);
1464 sizeof (struct GNUNET_MESH_Unicast) +
1465 sizeof (struct GNUNET_MessageHeader))
1470 msg = (struct GNUNET_MESH_Unicast *) message;
1471 t = retrieve_tunnel (&msg->oid, ntohl (msg->tid));
1474 /* TODO notify back: we don't know this tunnel */
1477 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1478 &msg->destination.hashPubKey);
1481 /* TODO maybe feedback, log to statistics */
1486 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1489 GNUNET_PEER_resolve (get_first_hop (pi->path), &id);
1490 msg = GNUNET_malloc (size);
1491 memcpy (msg, message, size);
1492 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1493 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1494 &send_core_data_raw, msg);
1500 * Core handler for mesh network traffic going from the origin to all peers
1502 * @param cls closure
1503 * @param message message
1504 * @param peer peer identity this notification is about
1505 * @param atsi performance data
1506 * @return GNUNET_OK to keep the connection open,
1507 * GNUNET_SYSERR to close it (signal serious error)
1510 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1511 const struct GNUNET_MessageHeader *message,
1512 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1514 struct GNUNET_MESH_Multicast *msg;
1515 struct GNUNET_PeerIdentity id;
1516 struct MeshTunnel *t;
1517 struct MeshDataDescriptor *info;
1518 GNUNET_PEER_Id *neighbors;
1524 size = ntohs (message->size);
1526 sizeof (struct GNUNET_MESH_Multicast) +
1527 sizeof (struct GNUNET_MessageHeader))
1529 GNUNET_break_op (0);
1532 msg = (struct GNUNET_MESH_Multicast *) message;
1533 t = retrieve_tunnel (&msg->oid, ntohl (msg->tid));
1537 /* TODO notify that we dont know that tunnel */
1541 /* Transmit to locally interested clients */
1542 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1544 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1547 /* Retransmit to other peers */
1548 neighbors = GNUNET_malloc (sizeof (GNUNET_PEER_Id));
1550 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &iterate_collect_neighbors,
1556 size -= sizeof (struct GNUNET_MESH_Multicast);
1557 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + size);
1558 info->origin = &t->id;
1560 for (i = 0; 0 != neighbors[i]; i++)
1562 GNUNET_PEER_resolve (neighbors[i], &id);
1564 info->destination = neighbors[i];
1565 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1566 GNUNET_assert (info->peer);
1567 for (j = 0; info->peer->core_transmit[j]; j++)
1575 info->handler_n = j;
1576 info->peer->infos[j] = info;
1577 info->peer->core_transmit[j] =
1578 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1579 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1580 ntohs (msg->header.size),
1581 &send_core_data_multicast, info);
1588 * Core handler for mesh network traffic
1590 * @param cls closure
1591 * @param message message
1592 * @param peer peer identity this notification is about
1593 * @param atsi performance data
1594 * @return GNUNET_OK to keep the connection open,
1595 * GNUNET_SYSERR to close it (signal serious error)
1598 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1599 const struct GNUNET_MessageHeader *message,
1600 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1602 struct GNUNET_MESH_ToOrigin *msg;
1603 struct GNUNET_PeerIdentity id;
1604 struct MeshTunnel *t;
1605 struct MeshPeerInfo *peer_info;
1608 size = ntohs (message->size);
1610 sizeof (struct GNUNET_MESH_ToOrigin) +
1611 sizeof (struct GNUNET_MessageHeader))
1613 GNUNET_break_op (0);
1616 msg = (struct GNUNET_MESH_ToOrigin *) message;
1617 t = retrieve_tunnel (&msg->oid, ntohl (msg->tid));
1621 /* TODO notify that we dont know this tunnel (whom)? */
1625 if (t->id.oid == myid)
1627 if (NULL == t->client)
1629 /* got data packet for ownerless tunnel */
1630 GNUNET_break_op (0);
1633 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1637 peer_info = get_peer_info (&msg->oid);
1638 if (NULL == peer_info)
1640 /* unknown origin of tunnel */
1644 GNUNET_PEER_resolve (get_first_hop (peer_info->path), &id);
1645 msg = GNUNET_malloc (size);
1646 memcpy (msg, message, size);
1647 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1648 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1649 &send_core_data_raw, msg);
1656 * Core handler for path ACKs
1658 * @param cls closure
1659 * @param message message
1660 * @param peer peer identity this notification is about
1661 * @param atsi performance data
1662 * @return GNUNET_OK to keep the connection open,
1663 * GNUNET_SYSERR to close it (signal serious error)
1666 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1667 const struct GNUNET_MessageHeader *message,
1668 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1670 struct GNUNET_MESH_PathACK *msg;
1671 struct GNUNET_PeerIdentity id;
1672 struct MeshTunnel *t;
1673 struct MeshPeerInfo *peer_info;
1675 msg = (struct GNUNET_MESH_PathACK *) message;
1676 t = retrieve_tunnel (&msg->oid, msg->tid);
1679 /* TODO notify that we don't know the tunnel */
1683 /* Message for us? */
1684 if (0 == memcmp (&msg->oid, &my_full_id, sizeof(struct GNUNET_PeerIdentity)))
1686 struct GNUNET_MESH_PeerControl pc;
1688 if (NULL == t->client)
1693 peer_info = get_peer_info (&msg->peer_id);
1694 if (NULL == peer_info)
1696 GNUNET_break_op (0);
1699 peer_info->state = MESH_PEER_READY;
1700 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1701 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1702 pc.tunnel_id = htonl (t->local_tid);
1703 GNUNET_PEER_resolve (peer_info->id, &pc.peer);
1704 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1705 &pc.header, GNUNET_NO);
1709 peer_info = get_peer_info (&msg->oid);
1710 if (NULL == peer_info)
1712 /* If we know the tunnel, we should DEFINITELY know the peer */
1716 GNUNET_PEER_resolve (get_first_hop (peer_info->path), &id);
1717 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1718 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1719 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1720 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1721 sizeof (struct GNUNET_MESH_PathACK),
1722 &send_core_data_raw, msg);
1728 * Functions to handle messages from core
1730 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1731 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1732 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1733 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1734 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1735 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1736 sizeof (struct GNUNET_MESH_PathACK)},
1742 /******************************************************************************/
1743 /**************** MESH LOCAL HANDLER HELPERS ***********************/
1744 /******************************************************************************/
1747 * delete_tunnel_entry: iterator for deleting each tunnel that belongs to a
1748 * client when the client disconnects.
1749 * @param cls closure (client that is disconnecting)
1750 * @param key the hash of the local tunnel id (used to access the hashmap)
1751 * @param value the value stored at the key (tunnel to destroy)
1752 * @return GNUNET_OK on success
1755 delete_tunnel_entry (void *cls, const GNUNET_HashCode * key, void *value)
1759 r = destroy_tunnel ((struct MeshTunnel *) value);
1765 * notify_client_connection_failure: notify a client that the connection to the
1766 * requested remote peer is not possible (for instance, no route found)
1767 * Function called when the socket is ready to queue more data. "buf" will be
1768 * NULL and "size" zero if the socket was closed for writing in the meantime.
1770 * @param cls closure
1771 * @param size number of bytes available in buf
1772 * @param buf where the callee should write the message
1773 * @return number of bytes written to buf
1776 notify_client_connection_failure (void *cls, size_t size, void *buf)
1779 struct MeshPeerInfo *peer_info;
1780 struct GNUNET_MESH_PeerControl *msg;
1781 struct GNUNET_PeerIdentity id;
1783 if (0 == size && NULL == buf)
1785 // TODO retry? cancel?
1789 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
1790 peer_info = (struct MeshPeerInfo *) cls;
1791 msg = (struct GNUNET_MESH_PeerControl *) buf;
1792 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1793 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
1794 // msg->tunnel_id = htonl(peer_info->t->tid);
1795 GNUNET_PEER_resolve (peer_info->id, &id);
1796 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
1804 * Send keepalive packets for a peer
1810 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1812 struct MeshPeerInfo *peer_info = cls;
1813 struct GNUNET_PeerIdentity id;
1815 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1817 GNUNET_PEER_resolve (get_first_hop (peer_info->path), &id);
1818 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1819 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1820 sizeof (struct GNUNET_MESH_ManipulatePath)
1822 (peer_info->path->length *
1823 sizeof (struct GNUNET_PeerIdentity)),
1824 &send_core_create_path_for_peer,
1826 peer_info->path_refresh_task =
1827 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh,
1834 * Function to process paths received for a new peer addition. The recorded
1835 * paths form the initial tunnel, which can be optimized later.
1836 * Called on each result obtained for the DHT search.
1838 * @param cls closure
1839 * @param exp when will this value expire
1840 * @param key key of the result
1841 * @param get_path NULL-terminated array of pointers
1842 * to the peers on reverse GET path (or NULL if not recorded)
1843 * @param put_path NULL-terminated array of pointers
1844 * to the peers on the PUT path (or NULL if not recorded)
1845 * @param type type of the result
1846 * @param size number of bytes in data
1847 * @param data pointer to the result data
1850 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1851 const GNUNET_HashCode * key,
1852 const struct GNUNET_PeerIdentity *const *get_path,
1853 const struct GNUNET_PeerIdentity *const *put_path,
1854 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
1856 struct MeshPeerInfo *peer_info = cls;
1858 struct GNUNET_PeerIdentity pi;
1861 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path)
1863 // Find ourselves some alternate initial path to the destination: retry
1864 GNUNET_DHT_get_stop (peer_info->dhtget);
1865 GNUNET_PEER_resolve (peer_info->id, &pi);
1866 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_BLOCK_TYPE_ANY, &pi.hashPubKey, 4, /* replication level */
1867 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
1870 0, /* xquery bits */
1872 (void *) peer_info);
1875 p = GNUNET_malloc (sizeof (struct MeshPath));
1876 for (i = 0; get_path[i] != NULL; i++) ;
1877 for (i--; i >= 0; i--)
1880 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1881 p->peers[p->length] = GNUNET_PEER_intern (get_path[i]);
1884 for (i = 0; put_path[i] != NULL; i++) ;
1885 for (i--; i >= 0; i--)
1888 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1889 p->peers[p->length] = GNUNET_PEER_intern (put_path[i]);
1892 add_path_to_peer (peer_info, p);
1893 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1894 GNUNET_TIME_UNIT_FOREVER_REL, get_path[1],
1895 sizeof (struct GNUNET_MESH_ManipulatePath)
1898 sizeof (struct GNUNET_PeerIdentity)),
1899 &send_core_create_path_for_peer,
1901 if (0 == peer_info->path_refresh_task)
1903 peer_info->path_refresh_task =
1904 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh,
1912 * Function to process paths received for a new peer addition. The recorded
1913 * paths form the initial tunnel, which can be optimized later.
1914 * Called on each result obtained for the DHT search.
1916 * @param cls closure
1917 * @param exp when will this value expire
1918 * @param key key of the result
1919 * @param get_path NULL-terminated array of pointers
1920 * to the peers on reverse GET path (or NULL if not recorded)
1921 * @param put_path NULL-terminated array of pointers
1922 * to the peers on the PUT path (or NULL if not recorded)
1923 * @param type type of the result
1924 * @param size number of bytes in data
1925 * @param data pointer to the result data
1928 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1929 const GNUNET_HashCode * key,
1930 const struct GNUNET_PeerIdentity *const *get_path,
1931 const struct GNUNET_PeerIdentity *const *put_path,
1932 enum GNUNET_BLOCK_Type type, size_t size,
1935 const struct GNUNET_PeerIdentity *pi = data;
1936 struct MeshTunnel *t = cls;
1937 struct MeshPeerInfo *peer_info;
1942 if (size != sizeof (struct GNUNET_PeerIdentity))
1944 GNUNET_break_op (0);
1947 peer_info = get_peer_info (pi);
1948 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
1949 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1950 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path)
1952 /* we don't have a route to the peer, let's try a direct lookup */
1953 if (NULL == peer_info->dhtget)
1955 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_BLOCK_TYPE_ANY, &pi->hashPubKey, 10U, /* replication level */
1956 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
1959 0, /* xquery bits */
1960 dht_get_id_handler, peer_info);
1964 p = GNUNET_malloc (sizeof (struct MeshPath));
1965 for (i = 0; get_path[i] != NULL; i++) ;
1966 for (i--; i >= 0; i--)
1969 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1970 p->peers[p->length] = GNUNET_PEER_intern (get_path[i]);
1973 for (i = 0; put_path[i] != NULL; i++) ;
1974 for (i--; i >= 0; i--)
1977 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1978 p->peers[p->length] = GNUNET_PEER_intern (put_path[i]);
1981 add_path_to_peer (peer_info, p);
1982 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1983 GNUNET_TIME_UNIT_FOREVER_REL, get_path[1],
1984 sizeof (struct GNUNET_MESH_ManipulatePath)
1987 sizeof (struct GNUNET_PeerIdentity)),
1988 &send_core_create_path_for_peer,
1993 /******************************************************************************/
1994 /********************* MESH LOCAL HANDLES **************************/
1995 /******************************************************************************/
1999 * Handler for client disconnection
2001 * @param cls closure
2002 * @param client identification of the client; NULL
2003 * for the last call when the server is destroyed
2006 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2008 struct MeshClient *c;
2009 struct MeshClient *next;
2013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2017 if (c->handle != client)
2019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2024 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels, &delete_tunnel_entry, c);
2025 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2026 if (0 != c->app_counter)
2028 /* deregister clients applications */
2029 for (i = 0; i < c->app_counter; i++)
2031 for (j = 0; j < n_apps; j++)
2033 if (c->apps[i] == applications[j] && 0 == --applications_rc[j])
2035 applications[j] = applications[n_apps - 1];
2036 GNUNET_array_grow (applications, n_apps, n_apps - 1);
2038 applications_rc[j] = applications_rc[n_apps - 1];
2039 GNUNET_array_grow (applications_rc, n_apps, n_apps - 1);
2045 GNUNET_free (c->apps);
2048 GNUNET_SCHEDULER_cancel (announce_applications_task);
2051 if (0 != c->type_counter)
2052 GNUNET_free (c->types);
2053 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2065 * Handler for new clients
2067 * @param cls closure
2068 * @param client identification of the client
2069 * @param message the actual message, which includes messages the client wants
2072 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2073 const struct GNUNET_MessageHeader *message)
2075 struct GNUNET_MESH_ClientConnect *cc_msg;
2076 struct MeshClient *c;
2077 GNUNET_MESH_ApplicationType *a;
2086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2087 /* Check data sanity */
2088 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2089 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2090 types = ntohs (cc_msg->types);
2091 apps = ntohs (cc_msg->applications);
2093 types * sizeof (uint16_t) + apps * sizeof (GNUNET_MESH_ApplicationType))
2096 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2100 /* Create new client structure */
2101 c = GNUNET_malloc (sizeof (struct MeshClient));
2103 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2106 c->app_counter = apps;
2107 c->apps = GNUNET_malloc (apps * sizeof(GNUNET_MESH_ApplicationType));
2108 for (i = 0; i < apps; i++)
2110 c->apps[i] = ntohl(a[i]);
2111 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: app %u\n", c->apps[i]);
2113 for (j = 0; i < n_apps; j++)
2115 if (c->apps[i] == applications[j])
2118 applications_rc[j]++;
2124 /* Register previously unknown application */
2125 GNUNET_array_append (applications, n_apps, c->apps[i]);
2127 GNUNET_array_append (applications_rc, n_apps, 1);
2128 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2130 announce_applications_task =
2131 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME,
2132 &announce_applications, NULL);
2134 /* TODO: if any client was looking for *type*, notify peer found */
2140 t = (uint16_t *) &a[apps];
2141 c->type_counter = types;
2142 c->types = GNUNET_malloc (types * sizeof (uint16_t));
2143 for (i =0; i < types; i++)
2145 c->types[i] = ntohs(t[i]);
2146 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: type %hu\n", c->types[i]);
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2150 "MESH: client has %u+%u subscriptions\n", c->type_counter,
2153 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2154 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2155 GNUNET_SERVER_notification_context_add (nc, client);
2157 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2163 * Handler for requests of new tunnels
2165 * @param cls closure
2166 * @param client identification of the client
2167 * @param message the actual message
2170 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2171 const struct GNUNET_MessageHeader *message)
2173 struct GNUNET_MESH_TunnelMessage *t_msg;
2174 struct MeshTunnel *t;
2175 struct MeshClient *c;
2176 GNUNET_HashCode hash;
2178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2180 /* Sanity check for client registration */
2181 if (NULL == (c = retrieve_client (client)))
2184 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2188 /* Message sanity check */
2189 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2192 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2196 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2197 /* Sanity check for tunnel numbering */
2198 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2204 /* Sanity check for duplicate tunnel IDs */
2205 if (NULL != retrieve_tunnel_by_local_id (c, ntohl (t_msg->tunnel_id)))
2208 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2212 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2213 while (NULL != retrieve_tunnel_by_pi (myid, next_tid))
2214 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2215 t->id.tid = next_tid++;
2217 t->local_tid = ntohl (t_msg->tunnel_id);
2219 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2221 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2223 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2224 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2227 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2231 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2233 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2234 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2237 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2241 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2247 * Handler for requests of deleting tunnels
2249 * @param cls closure
2250 * @param client identification of the client
2251 * @param message the actual message
2254 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2255 const struct GNUNET_MessageHeader *message)
2257 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2258 struct MeshClient *c;
2259 struct MeshTunnel *t;
2260 MESH_TunnelNumber tid;
2261 GNUNET_HashCode hash;
2263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2265 /* Sanity check for client registration */
2266 if (NULL == (c = retrieve_client (client)))
2269 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2272 /* Message sanity check */
2273 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2276 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2280 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2282 /* Retrieve tunnel */
2283 tid = ntohl (tunnel_msg->tunnel_id);
2285 /* Remove from local id hashmap */
2286 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2287 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2288 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2290 /* Remove from global id hashmap */
2291 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2292 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2294 // notify_tunnel_destroy(t);
2295 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2301 * Handler for connection requests to new peers
2303 * @param cls closure
2304 * @param client identification of the client
2305 * @param message the actual message (PeerControl)
2308 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2309 const struct GNUNET_MessageHeader *message)
2311 struct GNUNET_MESH_PeerControl *peer_msg;
2312 struct MeshClient *c;
2313 struct MeshTunnel *t;
2314 MESH_TunnelNumber tid;
2315 struct MeshPeerInfo *peer_info;
2318 /* Sanity check for client registration */
2319 if (NULL == (c = retrieve_client (client)))
2322 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2326 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2327 /* Sanity check for message size */
2328 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2331 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2335 /* Tunnel exists? */
2336 tid = ntohl (peer_msg->tunnel_id);
2337 t = retrieve_tunnel_by_local_id (c, tid);
2341 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2345 /* Does client own tunnel? */
2346 if (t->client->handle != client)
2349 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2354 peer_info = get_peer_info (&peer_msg->peer);
2356 /* Start DHT search if needed */
2357 if (MESH_PEER_READY != peer_info->state && NULL == peer_info->dhtget)
2359 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_BLOCK_TYPE_ANY, &peer_msg->peer.hashPubKey, 4, /* replication level */
2360 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2363 0, /* xquery bits */
2365 (void *) peer_info);
2368 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2374 * Handler for disconnection requests of peers in a tunnel
2376 * @param cls closure
2377 * @param client identification of the client
2378 * @param message the actual message (PeerControl)
2381 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2382 const struct GNUNET_MessageHeader *message)
2384 struct GNUNET_MESH_PeerControl *peer_msg;
2385 struct MeshClient *c;
2386 struct MeshTunnel *t;
2387 MESH_TunnelNumber tid;
2389 /* Sanity check for client registration */
2390 if (NULL == (c = retrieve_client (client)))
2393 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2396 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2397 /* Sanity check for message size */
2398 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2401 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2405 /* Tunnel exists? */
2406 tid = ntohl (peer_msg->tunnel_id);
2407 t = retrieve_tunnel_by_local_id (c, tid);
2411 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2415 /* Does client own tunnel? */
2416 if (t->client->handle != client)
2419 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2423 /* Ok, delete peer from tunnel */
2424 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2425 &peer_msg->peer.hashPubKey);
2427 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2433 * Handler for connection requests to new peers by type
2435 * @param cls closure
2436 * @param client identification of the client
2437 * @param message the actual message (ConnectPeerByType)
2440 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2441 const struct GNUNET_MessageHeader *message)
2443 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2444 struct MeshClient *c;
2445 struct MeshTunnel *t;
2446 GNUNET_HashCode hash;
2447 GNUNET_MESH_ApplicationType type;
2448 MESH_TunnelNumber tid;
2449 uint8_t buffer[12] = "MESH_APP";
2453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2454 /* Sanity check for client registration */
2455 if (NULL == (c = retrieve_client (client)))
2458 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2462 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2463 /* Sanity check for message size */
2464 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2465 ntohs (connect_msg->header.size))
2468 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2472 /* Tunnel exists? */
2473 tid = ntohl (connect_msg->tunnel_id);
2474 t = retrieve_tunnel_by_local_id (c, tid);
2478 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2482 /* Does client own tunnel? */
2483 if (t->client->handle != client)
2486 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2490 /* Do WE have the service? */
2491 type = ntohl (connect_msg->type);
2492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2493 for (i = 0; i < n_apps; i++)
2495 if (applications[i] == type)
2497 /* Yes! Fast forward, add ourselves to the tunnel and send the
2498 * good news to the client
2500 struct GNUNET_MESH_PeerControl pc;
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2503 pc.peer = my_full_id;
2504 GNUNET_CONTAINER_multihashmap_put (t->peers, &pc.peer.hashPubKey,
2505 get_peer_info (&pc.peer),
2506 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2507 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2508 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2509 pc.tunnel_id = htonl (t->local_tid);
2510 pc.peer = my_full_id;
2511 GNUNET_SERVER_notification_context_unicast (nc, /* context */
2513 &pc.header, /* msg */
2514 GNUNET_NO); /* can drop? */
2515 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2519 /* Ok, lets find a peer offering the service */
2520 p = (uint32_t *) & buffer[8];
2521 *p = connect_msg->type; /* Already in Network Byte Order! */
2522 GNUNET_CRYPTO_hash (buffer, 12, &hash);
2523 if (c->dht_get_type)
2525 GNUNET_DHT_get_stop (c->dht_get_type);
2528 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2529 GNUNET_BLOCK_TYPE_ANY, &hash, 10U,
2530 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
2531 &dht_get_type_handler, t);
2533 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2539 * Handler for client traffic directed to one peer
2541 * @param cls closure
2542 * @param client identification of the client
2543 * @param message the actual message
2546 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2547 const struct GNUNET_MessageHeader *message)
2549 struct MeshClient *c;
2550 struct MeshTunnel *t;
2551 struct MeshPeerInfo *pi;
2552 struct GNUNET_MESH_Unicast *data_msg;
2553 struct GNUNET_PeerIdentity next_hop;
2554 struct MeshDataDescriptor *info;
2555 MESH_TunnelNumber tid;
2558 /* Sanity check for client registration */
2559 if (NULL == (c = retrieve_client (client)))
2562 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2565 data_msg = (struct GNUNET_MESH_Unicast *) message;
2566 /* Sanity check for message size */
2567 if (sizeof (struct GNUNET_MESH_Unicast) +
2568 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
2571 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2575 /* Tunnel exists? */
2576 tid = ntohl (data_msg->tid);
2577 t = retrieve_tunnel_by_local_id (c, tid);
2581 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2585 /* Is it a local tunnel? Then, does client own the tunnel? */
2586 if (t->client->handle != NULL && t->client->handle != client)
2589 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2593 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2594 &data_msg->destination.hashPubKey);
2595 /* Is the selected peer in the tunnel? */
2599 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2604 struct GNUNET_MESH_Unicast copy;
2606 /* Work around const limitation */
2607 memcpy (©, data_msg, sizeof (struct GNUNET_MESH_Unicast));
2608 copy.oid = my_full_id;
2609 copy.tid = htonl(t->id.tid);
2610 handle_mesh_data_unicast (NULL, &my_full_id, ©.header, NULL);
2613 GNUNET_PEER_resolve (get_first_hop (pi->path), &next_hop);
2614 data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
2615 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
2616 memcpy (&info[1], &data_msg[1], data_size);
2617 info->destination = pi->id;
2618 info->origin = &t->id;
2619 info->size = data_size;
2620 info->client = client;
2621 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2622 GNUNET_TIME_UNIT_FOREVER_REL, &next_hop,
2623 /* FIXME re-check types */
2625 sizeof (struct GNUNET_MESH_Unicast),
2626 &send_core_data_unicast, info);
2631 * Handler for client traffic directed to all peers in a tunnel
2633 * @param cls closure
2634 * @param client identification of the client
2635 * @param message the actual message
2638 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2639 const struct GNUNET_MessageHeader *message)
2641 struct MeshClient *c;
2642 struct MeshTunnel *t;
2643 struct GNUNET_MESH_Multicast *data_msg;
2644 MESH_TunnelNumber tid;
2646 /* Sanity check for client registration */
2647 if (NULL == (c = retrieve_client (client)))
2650 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2653 data_msg = (struct GNUNET_MESH_Multicast *) message;
2654 /* Sanity check for message size */
2655 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2658 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2662 /* Tunnel exists? */
2663 tid = ntohl (data_msg->tid);
2664 t = retrieve_tunnel_by_local_id (c, tid);
2668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2672 /* Does client own tunnel? */
2673 if (t->client->handle != client)
2676 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2682 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2687 * Functions to handle messages from clients
2689 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
2690 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2691 {&handle_local_tunnel_create, NULL,
2692 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2693 sizeof (struct GNUNET_MESH_TunnelMessage)},
2694 {&handle_local_tunnel_destroy, NULL,
2695 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2696 sizeof (struct GNUNET_MESH_TunnelMessage)},
2697 {&handle_local_connect_add, NULL,
2698 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
2699 sizeof (struct GNUNET_MESH_PeerControl)},
2700 {&handle_local_connect_del, NULL,
2701 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
2702 sizeof (struct GNUNET_MESH_PeerControl)},
2703 {&handle_local_connect_by_type, NULL,
2704 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
2705 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
2706 {&handle_local_unicast, NULL,
2707 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2708 {&handle_local_unicast, NULL,
2709 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2710 {&handle_local_multicast, NULL,
2711 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2717 * To be called on core init/fail.
2719 * @param cls service closure
2720 * @param server handle to the server for this service
2721 * @param identity the public identity of this peer
2722 * @param publicKey the public key of this peer
2725 core_init (void *cls, struct GNUNET_CORE_Handle *server,
2726 const struct GNUNET_PeerIdentity *identity,
2727 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
2730 core_handle = server;
2731 my_full_id = *identity;
2732 myid = GNUNET_PEER_intern (identity);
2733 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
2738 * Method called whenever a given peer connects.
2740 * @param cls closure
2741 * @param peer peer identity this notification is about
2742 * @param atsi performance data for the connection
2745 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
2746 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2748 // GNUNET_PEER_Id pid;
2749 struct MeshPeerInfo *peer_info;
2750 struct MeshPath *path;
2752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
2753 peer_info = get_peer_info (peer);
2754 if (myid == peer_info->id)
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
2758 path = GNUNET_malloc (sizeof (struct MeshPath));
2760 path->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id) * 2);
2761 path->peers[0] = myid;
2762 path->peers[1] = peer_info->id;
2763 add_path_to_peer (peer_info, path);
2768 * Method called whenever a peer disconnects.
2770 * @param cls closure
2771 * @param peer peer identity this notification is about
2774 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
2776 struct MeshPeerInfo *pi;
2779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
2780 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2786 for (i = 0; i < CORE_QUEUE_SIZE; i++)
2788 if (pi->core_transmit[i])
2790 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
2791 /* TODO: notify that tranmission has failed */
2792 GNUNET_free (pi->infos[i]);
2797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
2803 /******************************************************************************/
2804 /************************ MAIN FUNCTIONS ****************************/
2805 /******************************************************************************/
2808 * Task run during shutdown.
2814 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
2817 if (core_handle != NULL)
2819 GNUNET_CORE_disconnect (core_handle);
2822 if (dht_handle != NULL)
2824 GNUNET_DHT_disconnect (dht_handle);
2829 GNUNET_SERVER_notification_context_destroy (nc);
2832 if (0 != announce_id_task)
2834 GNUNET_SCHEDULER_cancel (announce_id_task);
2835 announce_id_task = 0;
2837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
2841 * Process mesh requests.
2843 * @param cls closure
2844 * @param server the initialized server
2845 * @param c configuration to use
2848 run (void *cls, struct GNUNET_SERVER_Handle *server,
2849 const struct GNUNET_CONFIGURATION_Handle *c)
2851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
2852 GNUNET_SERVER_add_handlers (server, plugin_handlers);
2853 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
2854 server_handle = server;
2855 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
2856 CORE_QUEUE_SIZE, /* queue size */
2857 NULL, /* Closure passed to MESH functions */
2858 &core_init, /* Call core_init once connected */
2859 &core_connect, /* Handle connects */
2860 &core_disconnect, /* remove peers on disconnects */
2861 NULL, /* Do we care about "status" updates? */
2862 NULL, /* Don't notify about all incoming messages */
2863 GNUNET_NO, /* For header only in notification */
2864 NULL, /* Don't notify about all outbound messages */
2865 GNUNET_NO, /* For header-only out notification */
2866 core_handlers); /* Register these handlers */
2867 if (core_handle == NULL)
2871 dht_handle = GNUNET_DHT_connect (c, 64);
2872 if (dht_handle == NULL)
2874 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
2875 Running without DHT has a severe\
2876 impact in MESH capabilities.\n\
2877 Plase check your configuretion and enable DHT.\n");
2883 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2884 peers = GNUNET_CONTAINER_multihashmap_create (32);
2885 nc = GNUNET_SERVER_notification_context_create (server_handle,
2888 clients_tail = NULL;
2890 applications = NULL;
2891 applications_rc = NULL;
2893 announce_applications_task = 0;
2895 /* Scheduled the task to clean up when shutdown is called */
2896 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
2899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
2903 * The main function for the mesh service.
2905 * @param argc number of arguments from the command line
2906 * @param argv command line arguments
2907 * @return 0 ok, 1 on error
2910 main (int argc, char *const *argv)
2914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
2917 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
2919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");