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
63 * @param cls string to print
64 * @param tc task context
67 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
71 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
79 /* TODO: move into configuration file */
80 #define CORE_QUEUE_SIZE 10
81 #define LOCAL_QUEUE_SIZE 100
82 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
83 GNUNET_TIME_UNIT_SECONDS,\
85 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
86 GNUNET_TIME_UNIT_SECONDS,\
89 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
90 GNUNET_TIME_UNIT_SECONDS,\
95 /******************************************************************************/
96 /************************ DATA STRUCTURES ****************************/
97 /******************************************************************************/
100 * Information regarding a path
108 struct MeshPath *next;
109 struct MeshPath *prev;
112 * Whether the path is serving traffic in a tunnel or is a backup
117 * List of all the peers that form the path from origin to target
119 GNUNET_PEER_Id *peers;
122 * Number of peers (hops) in the path
129 * All the states a peer participating in a tunnel can be in.
134 * Path to the peer not known yet
139 * Request sent, not yet answered.
144 * Peer connected and ready to accept data
149 * Peer connected previosly but not responding
151 MESH_PEER_RECONNECTING
155 /** FWD declaration */
159 * Struct containing all info possibly needed to build a package when called
162 struct MeshDataDescriptor
164 /** ID of the tunnel this packet travels in */
165 struct MESH_TunnelID *origin;
167 /** Ultimate destination of the packet */
168 GNUNET_PEER_Id destination;
170 /** Number of identical messages sent to different hops (multicast) */
173 /** Size of the data */
176 /** Client that asked for the transmission, if any */
177 struct GNUNET_SERVER_Client *client;
179 /** Who was is message being sent to */
180 struct MeshPeerInfo *peer;
182 /** Which handler was used to request the transmission */
183 unsigned int handler_n;
185 /* Data at the end */
190 * Struct containing all information regarding a given peer
200 * Is the peer reachable? Is the peer even connected?
202 enum MeshPeerState state;
205 * Last time we heard from this peer
207 struct GNUNET_TIME_Absolute last_contact;
210 * Number of attempts to reconnect so far
212 int n_reconnect_attempts;
215 * Paths to reach the peer
217 struct MeshPath *path;
218 struct MeshPath *path_tail;
221 * Handle to stop the DHT search for a path to this peer
223 struct GNUNET_DHT_GetHandle *dhtget;
226 * Handles to stop queued transmissions for this peer
228 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
231 * Pointer to info stuctures used as cls for queued transmissions
233 struct MeshDataDescriptor *infos[CORE_QUEUE_SIZE];
236 * Task to send keepalive packets over the current active path
238 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
243 * Data scheduled to transmit (to local client or remote peer)
250 struct MeshQueue *next;
251 struct MeshQueue *prev;
254 * Target of the data (NULL if target is client)
256 struct MeshPeerInfo *peer;
259 * Client to send the data to (NULL if target is peer)
261 struct MeshClient *client;
264 * Size of the message to transmit
269 * How old is the data?
271 struct GNUNET_TIME_Absolute timestamp;
276 struct GNUNET_MessageHeader *data;
280 * Globally unique tunnel identification (owner + number)
281 * DO NOT USE OVER THE NETWORK
286 * Node that owns the tunnel
291 * Tunnel number to differentiate all the tunnels owned by the node oid
292 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
294 MESH_TunnelNumber tid;
298 struct MeshClient; /* FWD declaration */
301 * Struct containing all information regarding a tunnel
302 * For an intermediate node the improtant info used will be:
303 * - id Tunnel unique identification
304 * - paths[0] To know where to send it next
305 * - metainfo: ready, speeds, accounting
312 struct MESH_TunnelID id;
315 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
317 MESH_TunnelNumber local_tid;
320 * Last time the tunnel was used
322 struct GNUNET_TIME_Absolute timestamp;
325 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
327 struct GNUNET_CONTAINER_MultiHashMap *peers;
330 * Number of peers that are connected and potentially ready to receive data
332 unsigned int peers_ready;
335 * Number of peers that have been added to the tunnel
337 unsigned int peers_total;
341 * Client owner of the tunnel, if any
343 struct MeshClient *client;
346 * Messages ready to transmit
348 struct MeshQueue *queue_head;
349 struct MeshQueue *queue_tail;
354 * Struct containing information about a client of the service
361 struct MeshClient *next;
362 struct MeshClient *prev;
365 * Tunnels that belong to this client, indexed by local id
367 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
370 * Handle to communicate with the client
372 struct GNUNET_SERVER_Client *handle;
375 * Applications that this client has claimed to provide
377 struct GNUNET_CONTAINER_MultiHashMap *apps;
380 * Messages that this client has declared interest in
382 struct GNUNET_CONTAINER_MultiHashMap *types;
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 struct GNUNET_CONTAINER_MultiHashMap *applications;
452 * All message types clients of this peer are interested in
454 static struct GNUNET_CONTAINER_MultiHashMap *types;
457 * Task to periodically announce provided applications
459 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
462 * Task to periodically announce itself in the network
464 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
467 /******************************************************************************/
468 /****************** GENERAL HELPER FUNCTIONS ************************/
469 /******************************************************************************/
472 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
473 * and inster it in the appropiate structures if the peer is not known yet.
474 * @param peer Identity of the peer
475 * @return Existing or newly created peer info
477 static struct MeshPeerInfo *
478 get_peer_info (const struct GNUNET_PeerIdentity *peer)
480 struct MeshPeerInfo *peer_info;
482 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
483 if (NULL == peer_info)
486 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
487 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
488 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
489 peer_info->id = GNUNET_PEER_intern (peer);
490 peer_info->state = MESH_PEER_SEARCHING;
497 * Find the first peer whom to send a packet to go down this path
498 * @param path The path to use
499 * @return short id of the next peer, myid in case of local delivery,
500 * or 0 in case of error
502 static GNUNET_PEER_Id
503 get_first_hop (struct MeshPath *path)
515 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
516 "tried to get the next hop from an invalid path\n");
520 for (i = 0; i < path->length; i++)
522 if (path->peers[i] == myid)
524 if (i < path->length - 1)
526 return path->peers[i + 1];
539 * Get the cost of the path.
540 * @param path The path to analyze
541 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
545 get_path_cost (struct MeshPath *path)
551 for (i = 0; i < path->length; i++)
553 if (path->peers[i] == myid)
555 return path->length - i;
563 * Add the path to the peer and update the path used to reach it in case this
565 * @param peer_info Destination peer to add the path to.
566 * @param path New path to add. Last peer must be the peer in arg 1.
569 add_path_to_peer (struct MeshPeerInfo *peer_info, struct MeshPath *path)
572 unsigned int new_cost;
573 unsigned int best_cost;
574 struct MeshPath *aux;
575 struct MeshPath *best;
577 if (NULL == peer_info || NULL == path)
580 new_cost = get_path_cost (path);
581 best_cost = UINT_MAX;
583 for (aux = peer_info->path; aux != NULL; aux = aux->next)
585 if ((i = get_path_cost (aux)) < best_cost)
591 if (best_cost < new_cost)
594 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path, peer_info->path_tail,
602 GNUNET_CONTAINER_DLL_insert (peer_info->path, peer_info->path_tail, path);
609 * Add the path to the peer and update the path used to reach it in case this
610 * is the shortest. The path is given in reverse, the destination peer is
611 * path[0]. The function modifies the path, inverting it to use the origin as
613 * @param peer_info Destination peer to add the path to.
614 * @param path New path to add. First peer must be the peer in arg 1.
617 add_path_to_origin (struct MeshPeerInfo *peer_info, struct MeshPath *path)
622 for (i = 0; i < path->length / 2; i++)
624 aux = path->peers[i];
625 path->peers[i] = path->peers[path->length - i - 1];
626 path->peers[path->length - i - 1] = aux;
628 add_path_to_peer (peer_info, path);
633 * Check if client has registered with the service and has not disconnected
634 * @param client the client to check
635 * @return non-NULL if client exists in the global DLL
637 static struct MeshClient *
638 retrieve_client (struct GNUNET_SERVER_Client *client)
640 struct MeshClient *c;
645 if (c->handle == client)
654 * Checks if a given client has subscribed to certain message type
655 * @param message_type Type of message to check
656 * @param c Client to check
657 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
659 static int /* FIXME inline? */
660 is_client_subscribed (uint16_t message_type, struct MeshClient *c)
664 GNUNET_CRYPTO_hash(&message_type, sizeof(uint16_t), &hc);
665 return GNUNET_CONTAINER_multihashmap_contains(c->types, &hc);
670 * Search for a tunnel among the tunnels for a client
671 * @param c the client whose tunnels to search in
672 * @param tid the local id of the tunnel
673 * @return tunnel handler, NULL if doesn't exist
675 static struct MeshTunnel *
676 retrieve_tunnel_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
678 GNUNET_HashCode hash;
680 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
681 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
685 * Search for a tunnel by global ID using PEER_ID
686 * @param pi owner of the tunnel
687 * @param tid global tunnel number
688 * @return tunnel handler, NULL if doesn't exist
690 static struct MeshTunnel *
691 retrieve_tunnel_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
693 struct MESH_TunnelID id;
694 GNUNET_HashCode hash;
699 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
700 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
706 * Search for a tunnel by global ID using full PeerIdentities
707 * @param oid owner of the tunnel
708 * @param tid global tunnel number
709 * @return tunnel handler, NULL if doesn't exist
711 static struct MeshTunnel *
712 retrieve_tunnel (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
714 return retrieve_tunnel_by_pi (GNUNET_PEER_search (oid), tid);
719 * Destroy the path and free any allocated resources linked to it
720 * @param p the path to destroy
721 * @return GNUNET_OK on success
724 destroy_path (struct MeshPath *p)
726 GNUNET_PEER_decrement_rcs (p->peers, p->length);
727 GNUNET_free (p->peers);
734 * Destroy the peer_info and free any allocated resources linked to it
735 * @param t tunnel the path belongs to
736 * @param pi the peer_info to destroy
737 * @return GNUNET_OK on success
740 destroy_peer_info (struct MeshPeerInfo *pi)
742 GNUNET_HashCode hash;
743 struct GNUNET_PeerIdentity id;
745 GNUNET_PEER_resolve (pi->id, &id);
746 GNUNET_PEER_change_rc (pi->id, -1);
747 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
749 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
750 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
758 * Destroy the tunnel and free any allocated resources linked to it
759 * @param t the tunnel to destroy
760 * @return GNUNET_OK on success
763 destroy_tunnel (struct MeshTunnel *t)
765 struct MeshClient *c;
767 struct MeshQueue *qn;
768 GNUNET_HashCode hash;
776 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
777 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
782 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
783 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
787 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
792 GNUNET_free (q->data);
796 /* TODO cancel core transmit ready in case it was active */
802 /******************************************************************************/
803 /************************ PERIODIC FUNCTIONS ****************************/
804 /******************************************************************************/
807 * Announce iterator over for each application provided by the peer
810 * @param key current key code
811 * @param value value in the hash map
812 * @return GNUNET_YES if we should continue to
817 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
819 /* FIXME are hashes in multihash map equal on all aquitectures? */
820 GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
821 GNUNET_BLOCK_TYPE_ANY, sizeof (struct GNUNET_PeerIdentity),
822 (const char *) &my_full_id,
823 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
827 &mesh_debug, "DHT_put for app completed");
836 * Periodically announce what applications are provided by local clients
839 * @param tc task context
842 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
844 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
846 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
850 GNUNET_CONTAINER_multihashmap_iterate(applications,
851 &announce_application,
853 announce_applications_task =
854 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
862 * Periodically announce self id in the DHT
865 * @param tc task context
868 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
870 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
872 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
876 * - Set data expiration in function of X
879 GNUNET_DHT_put (dht_handle, /* DHT handle */
880 &my_full_id.hashPubKey, /* Key to use */
881 10U, /* Replication level */
882 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
883 GNUNET_BLOCK_TYPE_ANY, /* Block type */
884 0, /* Size of the data */
885 NULL, /* Data itself */
886 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
887 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
889 &mesh_debug, "DHT_put for id completed");
891 NULL, /* Continuation */
892 NULL); /* Continuation closure */
895 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
898 /******************************************************************************/
899 /**************** MESH NETWORK HANDLER HELPERS ***********************/
900 /******************************************************************************/
903 * Function called to notify a client about the socket
904 * being ready to queue more data. "buf" will be
905 * NULL and "size" zero if the socket was closed for
906 * writing in the meantime.
909 * @param size number of bytes available in buf
910 * @param buf where the callee should write the message
911 * @return number of bytes written to buf
914 send_core_create_path_for_peer (void *cls, size_t size, void *buf)
916 struct MeshPeerInfo *peer_info = cls;
917 struct GNUNET_MESH_ManipulatePath *msg;
919 struct GNUNET_PeerIdentity *peer_ptr;
920 struct GNUNET_PeerIdentity id;
924 if (0 == size && NULL == buf)
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
927 GNUNET_PEER_resolve (get_first_hop (peer_info->path), &id);
928 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
929 GNUNET_TIME_UNIT_FOREVER_REL, &id,
931 GNUNET_MESH_ManipulatePath) +
932 (peer_info->path->length *
933 sizeof (struct GNUNET_PeerIdentity)),
934 &send_core_create_path_for_peer,
948 return 0; // TODO Notify ERROR Path not found
951 sizeof (struct GNUNET_MESH_ManipulatePath) +
952 p->length * sizeof (struct GNUNET_PeerIdentity);
953 if (size < size_needed)
955 // TODO retry? cancel?
959 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
960 msg->header.size = htons (size_needed);
961 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
963 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
964 for (i = 0; i < p->length; i++)
966 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
969 peer_info->state = MESH_PEER_WAITING;
977 * Function called to notify a client about the socket
978 * being ready to queue more data. "buf" will be
979 * NULL and "size" zero if the socket was closed for
980 * writing in the meantime.
982 * @param cls closure (MeshDataDescriptor with all info to build packet)
983 * @param size number of bytes available in buf
984 * @param buf where the callee should write the message
985 * @return number of bytes written to buf
988 send_core_data_to_origin (void *cls, size_t size, void *buf)
990 struct MeshDataDescriptor *info = cls;
991 struct GNUNET_MESH_ToOrigin *msg = buf;
994 GNUNET_assert (NULL != info);
995 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
996 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
998 if (total_size > size)
1000 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1001 "not enough buffer to send data to origin\n");
1004 msg->header.size = htons (total_size);
1005 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1006 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1007 msg->tid = htonl (info->origin->tid);
1008 if (0 != info->size)
1010 memcpy (&msg[1], &info[1], info->size);
1012 if (NULL != info->client)
1014 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1022 * Function called to notify a client about the socket
1023 * being ready to queue more data. "buf" will be
1024 * NULL and "size" zero if the socket was closed for
1025 * writing in the meantime.
1027 * @param cls closure (data itself)
1028 * @param size number of bytes available in buf
1029 * @param buf where the callee should write the message
1030 * @return number of bytes written to buf
1033 send_core_data_unicast (void *cls, size_t size, void *buf)
1035 struct MeshDataDescriptor *info = cls;
1036 struct GNUNET_MESH_Unicast *msg = buf;
1039 GNUNET_assert (NULL != info);
1040 total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
1041 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1043 if (total_size > size)
1045 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1046 "not enough buffer to send data to peer\n");
1049 msg->header.size = htons (total_size);
1050 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1051 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1052 GNUNET_PEER_resolve (info->destination, &msg->destination);
1053 msg->tid = htonl (info->origin->tid);
1054 if (0 != info->size)
1056 memcpy (&msg[1], &info[1], info->size);
1058 if (NULL != info->client)
1060 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1068 * Function called to notify a client about the socket
1069 * being ready to queue more data. "buf" will be
1070 * NULL and "size" zero if the socket was closed for
1071 * writing in the meantime.
1073 * @param cls closure (data itself)
1074 * @param size number of bytes available in buf
1075 * @param buf where the callee should write the message
1076 * @return number of bytes written to buf
1079 send_core_data_multicast (void *cls, size_t size, void *buf)
1081 struct MeshDataDescriptor *info = cls;
1082 struct GNUNET_MESH_Multicast *msg = buf;
1085 GNUNET_assert (NULL != info);
1086 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1087 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1091 info->peer->core_transmit[info->handler_n] = NULL;
1093 if (total_size > size)
1095 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1096 "not enough buffer to send data futher\n");
1099 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1100 msg->header.size = htons (total_size);
1101 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1102 msg->tid = htonl (info->origin->tid);
1103 memcpy (&msg[1], &info[1], total_size);
1104 if (0 == --info->copies)
1106 if (NULL != info->client)
1108 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1117 * Function called to notify a client about the socket
1118 * being ready to queue more data. "buf" will be
1119 * NULL and "size" zero if the socket was closed for
1120 * writing in the meantime.
1122 * @param cls closure (MeshDataDescriptor)
1123 * @param size number of bytes available in buf
1124 * @param buf where the callee should write the message
1125 * @return number of bytes written to buf
1128 send_core_path_ack (void *cls, size_t size, void *buf)
1130 struct MeshDataDescriptor *info = cls;
1131 struct GNUNET_MESH_PathACK *msg = buf;
1133 GNUNET_assert (NULL != info);
1136 info->peer->core_transmit[info->handler_n] = NULL;
1138 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1143 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1144 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1145 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1146 msg->tid = htonl (info->origin->tid);
1147 msg->peer_id = my_full_id;
1148 /* TODO add signature */
1150 return sizeof (struct GNUNET_MESH_PathACK);
1155 * Function called to notify a client about the socket
1156 * being ready to queue more data. "buf" will be
1157 * NULL and "size" zero if the socket was closed for
1158 * writing in the meantime.
1160 * @param cls closure (data itself)
1161 * @param size number of bytes available in buf
1162 * @param buf where the callee should write the message
1163 * @return number of bytes written to buf
1166 send_core_data_raw (void *cls, size_t size, void *buf)
1168 struct GNUNET_MessageHeader *msg = cls;
1171 GNUNET_assert (NULL != msg);
1172 total_size = ntohs (msg->size);
1174 if (total_size > size)
1179 memcpy (buf, msg, total_size);
1187 * Send another peer a notification to destroy a tunnel
1188 * @param cls The tunnel to destroy
1189 * @param size Size in the buffer
1190 * @param buf Memory where to put the data to transmit
1191 * @return Size of data put in buffer
1194 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1196 struct MeshTunnel *t = cls;
1197 struct MeshClient *c;
1198 struct GNUNET_MESH_TunnelMessage *msg;
1202 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1203 /*FIXME*/ msg->header.size =
1204 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1205 msg->tunnel_id = htonl (t->id.tid);
1207 destroy_tunnel (c, t);
1208 return sizeof (struct GNUNET_MESH_TunnelMessage);
1214 * Send the message to all clients that have subscribed to its type
1216 * @param msg Pointer to the message itself
1217 * @return number of clients this message was sent to
1220 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1222 struct MeshClient *c;
1226 type = ntohs (msg->type);
1227 for (count = 0, c = clients; c != NULL; c = c->next)
1229 if (is_client_subscribed (type, c))
1232 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1241 * Iterator over hash map peer entries collect all neighbors who to resend the
1244 * @param cls closure (**GNUNET_PEER_Id to store hops to send packet)
1245 * @param key current key code (peer id hash)
1246 * @param value value in the hash map (peer_info)
1247 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
1250 iterate_collect_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
1252 struct MeshPeerInfo *peer_info = value;
1253 GNUNET_PEER_Id **neighbors = cls;
1257 if (peer_info->id == myid)
1261 id = get_first_hop (peer_info->path);
1262 for (i = 0; *neighbors[i] != 0; i++)
1264 if (*neighbors[i] == id)
1267 *neighbors = GNUNET_realloc (*neighbors, (i + 2) * sizeof (GNUNET_PEER_Id));
1269 *neighbors[i + 1] = 0;
1275 /******************************************************************************/
1276 /******************** MESH NETWORK HANDLERS **************************/
1277 /******************************************************************************/
1281 * Core handler for path creation
1282 * struct GNUNET_CORE_MessageHandler
1284 * @param cls closure
1285 * @param message message
1286 * @param peer peer identity this notification is about
1287 * @param atsi performance data
1288 * @return GNUNET_OK to keep the connection open,
1289 * GNUNET_SYSERR to close it (signal serious error)
1293 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1294 const struct GNUNET_MessageHeader *message,
1295 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1297 unsigned int own_pos;
1300 MESH_TunnelNumber tid;
1301 struct GNUNET_MESH_ManipulatePath *msg;
1302 struct GNUNET_PeerIdentity *pi;
1303 struct GNUNET_PeerIdentity id;
1304 GNUNET_HashCode hash;
1305 struct MeshPath *path;
1306 struct MeshPeerInfo *dest_peer_info;
1307 struct MeshPeerInfo *orig_peer_info;
1308 struct MeshTunnel *t;
1310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1311 "MESH: Received a MESH path create msg\n");
1312 size = ntohs (message->size);
1313 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1316 "received create path message too short\n");
1320 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1321 if (size < 2 * sizeof (struct GNUNET_PeerIdentity))
1323 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1324 "create path message lacks enough peers\n");
1327 if (size % sizeof (struct GNUNET_PeerIdentity))
1329 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1330 "create path message of wrong size\n");
1333 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1334 size /= sizeof (struct GNUNET_PeerIdentity);
1336 tid = ntohl (msg->tid);
1337 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1338 t = retrieve_tunnel (pi, tid);
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1343 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1344 t->id.oid = GNUNET_PEER_intern (pi);
1346 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1348 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1350 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1351 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1359 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1360 if (NULL == dest_peer_info)
1362 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1363 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1364 dest_peer_info->state = MESH_PEER_WAITING;
1365 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1367 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1369 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1370 if (NULL == orig_peer_info)
1372 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1373 orig_peer_info->id = GNUNET_PEER_intern (pi);
1374 orig_peer_info->state = MESH_PEER_WAITING;
1375 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1376 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1380 path = GNUNET_malloc (sizeof (struct MeshPath));
1381 path->length = size;
1382 path->peers = GNUNET_malloc (size * sizeof (GNUNET_PEER_Id));
1384 for (i = 0; i < size; i++)
1386 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1387 if (path->peers[i] == myid)
1391 { /* cannot be self, must be 'not found' */
1392 /* create path: self not found in path through self */
1393 GNUNET_break_op (0);
1394 destroy_path (path);
1395 /* FIXME error. destroy tunnel? leave for timeout? */
1398 if (own_pos == size - 1)
1399 { /* it is for us! */
1400 struct MeshDataDescriptor *info;
1403 add_path_to_origin (orig_peer_info, path); /* inverts path! */
1404 GNUNET_PEER_resolve (get_first_hop (path), &id); /* path is inverted :) */
1405 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1406 info->origin = &t->id;
1407 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1408 GNUNET_assert (info->peer);
1409 for (j = 0; info->peer->core_transmit[j]; j++)
1417 info->handler_n = j;
1418 info->peer->core_transmit[j] =
1419 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1420 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1421 sizeof (struct GNUNET_MessageHeader),
1422 &send_core_path_ack, info);
1426 add_path_to_peer (dest_peer_info, path);
1427 GNUNET_PEER_resolve (get_first_hop (path), &id);
1428 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1429 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1430 sizeof (struct GNUNET_MessageHeader),
1431 &send_core_create_path_for_peer,
1439 * Core handler for mesh network traffic going from the origin to a peer
1441 * @param cls closure
1442 * @param peer peer identity this notification is about
1443 * @param message message
1444 * @param atsi performance data
1445 * @return GNUNET_OK to keep the connection open,
1446 * GNUNET_SYSERR to close it (signal serious error)
1449 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1450 const struct GNUNET_MessageHeader *message,
1451 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1453 struct GNUNET_MESH_Unicast *msg;
1454 struct GNUNET_PeerIdentity id;
1455 struct MeshTunnel *t;
1456 struct MeshPeerInfo *pi;
1459 size = ntohs (message->size);
1461 sizeof (struct GNUNET_MESH_Unicast) +
1462 sizeof (struct GNUNET_MessageHeader))
1467 msg = (struct GNUNET_MESH_Unicast *) message;
1468 t = retrieve_tunnel (&msg->oid, ntohl (msg->tid));
1471 /* TODO notify back: we don't know this tunnel */
1474 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1475 &msg->destination.hashPubKey);
1478 /* TODO maybe feedback, log to statistics */
1483 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1486 GNUNET_PEER_resolve (get_first_hop (pi->path), &id);
1487 msg = GNUNET_malloc (size);
1488 memcpy (msg, message, size);
1489 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1490 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1491 &send_core_data_raw, msg);
1497 * Core handler for mesh network traffic going from the origin to all peers
1499 * @param cls closure
1500 * @param message message
1501 * @param peer peer identity this notification is about
1502 * @param atsi performance data
1503 * @return GNUNET_OK to keep the connection open,
1504 * GNUNET_SYSERR to close it (signal serious error)
1507 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1508 const struct GNUNET_MessageHeader *message,
1509 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1511 struct GNUNET_MESH_Multicast *msg;
1512 struct GNUNET_PeerIdentity id;
1513 struct MeshTunnel *t;
1514 struct MeshDataDescriptor *info;
1515 GNUNET_PEER_Id *neighbors;
1521 size = ntohs (message->size);
1523 sizeof (struct GNUNET_MESH_Multicast) +
1524 sizeof (struct GNUNET_MessageHeader))
1526 GNUNET_break_op (0);
1529 msg = (struct GNUNET_MESH_Multicast *) message;
1530 t = retrieve_tunnel (&msg->oid, ntohl (msg->tid));
1534 /* TODO notify that we dont know that tunnel */
1538 /* Transmit to locally interested clients */
1539 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1541 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1544 /* Retransmit to other peers */
1545 neighbors = GNUNET_malloc (sizeof (GNUNET_PEER_Id));
1547 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &iterate_collect_neighbors,
1553 size -= sizeof (struct GNUNET_MESH_Multicast);
1554 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + size);
1555 info->origin = &t->id;
1557 for (i = 0; 0 != neighbors[i]; i++)
1559 GNUNET_PEER_resolve (neighbors[i], &id);
1561 info->destination = neighbors[i];
1562 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1563 GNUNET_assert (info->peer);
1564 for (j = 0; info->peer->core_transmit[j]; j++)
1572 info->handler_n = j;
1573 info->peer->infos[j] = info;
1574 info->peer->core_transmit[j] =
1575 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1576 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1577 ntohs (msg->header.size),
1578 &send_core_data_multicast, info);
1585 * Core handler for mesh network traffic
1587 * @param cls closure
1588 * @param message message
1589 * @param peer peer identity this notification is about
1590 * @param atsi performance data
1591 * @return GNUNET_OK to keep the connection open,
1592 * GNUNET_SYSERR to close it (signal serious error)
1595 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1596 const struct GNUNET_MessageHeader *message,
1597 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1599 struct GNUNET_MESH_ToOrigin *msg;
1600 struct GNUNET_PeerIdentity id;
1601 struct MeshTunnel *t;
1602 struct MeshPeerInfo *peer_info;
1605 size = ntohs (message->size);
1607 sizeof (struct GNUNET_MESH_ToOrigin) +
1608 sizeof (struct GNUNET_MessageHeader))
1610 GNUNET_break_op (0);
1613 msg = (struct GNUNET_MESH_ToOrigin *) message;
1614 t = retrieve_tunnel (&msg->oid, ntohl (msg->tid));
1618 /* TODO notify that we dont know this tunnel (whom)? */
1622 if (t->id.oid == myid)
1624 if (NULL == t->client)
1626 /* got data packet for ownerless tunnel */
1627 GNUNET_break_op (0);
1630 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1634 peer_info = get_peer_info (&msg->oid);
1635 if (NULL == peer_info)
1637 /* unknown origin of tunnel */
1641 GNUNET_PEER_resolve (get_first_hop (peer_info->path), &id);
1642 msg = GNUNET_malloc (size);
1643 memcpy (msg, message, size);
1644 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1645 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1646 &send_core_data_raw, msg);
1653 * Core handler for path ACKs
1655 * @param cls closure
1656 * @param message message
1657 * @param peer peer identity this notification is about
1658 * @param atsi performance data
1659 * @return GNUNET_OK to keep the connection open,
1660 * GNUNET_SYSERR to close it (signal serious error)
1663 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1664 const struct GNUNET_MessageHeader *message,
1665 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1667 struct GNUNET_MESH_PathACK *msg;
1668 struct GNUNET_PeerIdentity id;
1669 struct MeshTunnel *t;
1670 struct MeshPeerInfo *peer_info;
1672 msg = (struct GNUNET_MESH_PathACK *) message;
1673 t = retrieve_tunnel (&msg->oid, msg->tid);
1676 /* TODO notify that we don't know the tunnel */
1680 /* Message for us? */
1681 if (0 == memcmp (&msg->oid, &my_full_id, sizeof(struct GNUNET_PeerIdentity)))
1683 struct GNUNET_MESH_PeerControl pc;
1685 if (NULL == t->client)
1690 peer_info = get_peer_info (&msg->peer_id);
1691 if (NULL == peer_info)
1693 GNUNET_break_op (0);
1696 peer_info->state = MESH_PEER_READY;
1697 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1698 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1699 pc.tunnel_id = htonl (t->local_tid);
1700 GNUNET_PEER_resolve (peer_info->id, &pc.peer);
1701 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1702 &pc.header, GNUNET_NO);
1706 peer_info = get_peer_info (&msg->oid);
1707 if (NULL == peer_info)
1709 /* If we know the tunnel, we should DEFINITELY know the peer */
1713 GNUNET_PEER_resolve (get_first_hop (peer_info->path), &id);
1714 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1715 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1716 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1717 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1718 sizeof (struct GNUNET_MESH_PathACK),
1719 &send_core_data_raw, msg);
1725 * Functions to handle messages from core
1727 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1728 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1729 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1730 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1731 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1732 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1733 sizeof (struct GNUNET_MESH_PathACK)},
1739 /******************************************************************************/
1740 /**************** MESH LOCAL HANDLER HELPERS ***********************/
1741 /******************************************************************************/
1744 * delete_tunnel_entry: iterator for deleting each tunnel that belongs to a
1745 * client when the client disconnects.
1746 * @param cls closure (client that is disconnecting)
1747 * @param key the hash of the local tunnel id (used to access the hashmap)
1748 * @param value the value stored at the key (tunnel to destroy)
1749 * @return GNUNET_OK on success
1752 delete_tunnel_entry (void *cls, const GNUNET_HashCode * key, void *value)
1756 r = destroy_tunnel ((struct MeshTunnel *) value);
1762 * deregister_app: iterator for removing each application registered by a client
1763 * @param cls closure
1764 * @param key the hash of the application id (used to access the hashmap)
1765 * @param value the value stored at the key (client)
1766 * @return GNUNET_OK on success
1769 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
1771 GNUNET_CONTAINER_multihashmap_remove(applications, key, value);
1777 * notify_client_connection_failure: notify a client that the connection to the
1778 * requested remote peer is not possible (for instance, no route found)
1779 * Function called when the socket is ready to queue more data. "buf" will be
1780 * NULL and "size" zero if the socket was closed for writing in the meantime.
1782 * @param cls closure
1783 * @param size number of bytes available in buf
1784 * @param buf where the callee should write the message
1785 * @return number of bytes written to buf
1788 notify_client_connection_failure (void *cls, size_t size, void *buf)
1791 struct MeshPeerInfo *peer_info;
1792 struct GNUNET_MESH_PeerControl *msg;
1793 struct GNUNET_PeerIdentity id;
1795 if (0 == size && NULL == buf)
1797 // TODO retry? cancel?
1801 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
1802 peer_info = (struct MeshPeerInfo *) cls;
1803 msg = (struct GNUNET_MESH_PeerControl *) buf;
1804 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1805 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
1806 // msg->tunnel_id = htonl(peer_info->t->tid);
1807 GNUNET_PEER_resolve (peer_info->id, &id);
1808 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
1816 * Send keepalive packets for a peer
1822 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1824 struct MeshPeerInfo *peer_info = cls;
1825 struct GNUNET_PeerIdentity id;
1827 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1829 GNUNET_PEER_resolve (get_first_hop (peer_info->path), &id);
1830 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1831 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1832 sizeof (struct GNUNET_MESH_ManipulatePath)
1834 (peer_info->path->length *
1835 sizeof (struct GNUNET_PeerIdentity)),
1836 &send_core_create_path_for_peer,
1838 peer_info->path_refresh_task =
1839 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh,
1846 * Function to process paths received for a new peer addition. The recorded
1847 * paths form the initial tunnel, which can be optimized later.
1848 * Called on each result obtained for the DHT search.
1850 * @param cls closure
1851 * @param exp when will this value expire
1852 * @param key key of the result
1853 * @param get_path NULL-terminated array of pointers
1854 * to the peers on reverse GET path (or NULL if not recorded)
1855 * @param put_path NULL-terminated array of pointers
1856 * to the peers on the PUT path (or NULL if not recorded)
1857 * @param type type of the result
1858 * @param size number of bytes in data
1859 * @param data pointer to the result data
1862 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1863 const GNUNET_HashCode * key,
1864 const struct GNUNET_PeerIdentity *const *get_path,
1865 const struct GNUNET_PeerIdentity *const *put_path,
1866 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
1868 struct MeshPeerInfo *peer_info = cls;
1870 struct GNUNET_PeerIdentity pi;
1873 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path)
1875 // Find ourselves some alternate initial path to the destination: retry
1876 GNUNET_DHT_get_stop (peer_info->dhtget);
1877 GNUNET_PEER_resolve (peer_info->id, &pi);
1878 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1879 GNUNET_TIME_UNIT_FOREVER_REL,
1880 GNUNET_BLOCK_TYPE_ANY, /* type */
1881 &pi.hashPubKey, /*key to search */
1882 4, /* replication level */
1883 GNUNET_DHT_RO_RECORD_ROUTE,
1884 NULL, /* bloom filter */
1887 0, /* xquery bits */
1889 (void *) peer_info);
1892 p = GNUNET_malloc (sizeof (struct MeshPath));
1893 for (i = 0; get_path[i] != NULL; i++) ;
1894 for (i--; i >= 0; i--)
1897 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1898 p->peers[p->length] = GNUNET_PEER_intern (get_path[i]);
1901 for (i = 0; put_path[i] != NULL; i++) ;
1902 for (i--; i >= 0; i--)
1905 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1906 p->peers[p->length] = GNUNET_PEER_intern (put_path[i]);
1909 add_path_to_peer (peer_info, p);
1910 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1911 GNUNET_TIME_UNIT_FOREVER_REL, get_path[1],
1912 sizeof (struct GNUNET_MESH_ManipulatePath)
1915 sizeof (struct GNUNET_PeerIdentity)),
1916 &send_core_create_path_for_peer,
1918 if (0 == peer_info->path_refresh_task)
1920 peer_info->path_refresh_task =
1921 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh,
1929 * Function to process paths received for a new peer addition. The recorded
1930 * paths form the initial tunnel, which can be optimized later.
1931 * Called on each result obtained for the DHT search.
1933 * @param cls closure
1934 * @param exp when will this value expire
1935 * @param key key of the result
1936 * @param get_path NULL-terminated array of pointers
1937 * to the peers on reverse GET path (or NULL if not recorded)
1938 * @param put_path NULL-terminated array of pointers
1939 * to the peers on the PUT path (or NULL if not recorded)
1940 * @param type type of the result
1941 * @param size number of bytes in data
1942 * @param data pointer to the result data
1945 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1946 const GNUNET_HashCode * key,
1947 const struct GNUNET_PeerIdentity *const *get_path,
1948 const struct GNUNET_PeerIdentity *const *put_path,
1949 enum GNUNET_BLOCK_Type type, size_t size,
1952 const struct GNUNET_PeerIdentity *pi = data;
1953 struct MeshTunnel *t = cls;
1954 struct MeshPeerInfo *peer_info;
1959 if (size != sizeof (struct GNUNET_PeerIdentity))
1961 GNUNET_break_op (0);
1964 peer_info = get_peer_info (pi);
1965 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
1966 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1967 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path)
1969 /* we don't have a route to the peer, let's try a direct lookup */
1970 if (NULL == peer_info->dhtget)
1972 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_BLOCK_TYPE_ANY, &pi->hashPubKey, 10U, /* replication level */
1973 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
1976 0, /* xquery bits */
1977 dht_get_id_handler, peer_info);
1981 p = GNUNET_malloc (sizeof (struct MeshPath));
1982 for (i = 0; get_path[i] != NULL; i++) ;
1983 for (i--; i >= 0; i--)
1986 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1987 p->peers[p->length] = GNUNET_PEER_intern (get_path[i]);
1990 for (i = 0; put_path[i] != NULL; i++) ;
1991 for (i--; i >= 0; i--)
1994 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1995 p->peers[p->length] = GNUNET_PEER_intern (put_path[i]);
1998 add_path_to_peer (peer_info, p);
1999 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2000 GNUNET_TIME_UNIT_FOREVER_REL, get_path[1],
2001 sizeof (struct GNUNET_MESH_ManipulatePath)
2004 sizeof (struct GNUNET_PeerIdentity)),
2005 &send_core_create_path_for_peer,
2010 /******************************************************************************/
2011 /********************* MESH LOCAL HANDLES **************************/
2012 /******************************************************************************/
2016 * Handler for client disconnection
2018 * @param cls closure
2019 * @param client identification of the client; NULL
2020 * for the last call when the server is destroyed
2023 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2025 struct MeshClient *c;
2026 struct MeshClient *next;
2028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2032 if (c->handle != client)
2034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2039 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels, &delete_tunnel_entry, c);
2040 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2042 /* deregister clients applications */
2043 GNUNET_CONTAINER_multihashmap_iterate(c->apps, &deregister_app, NULL);
2044 GNUNET_CONTAINER_multihashmap_destroy(c->apps);
2045 if (0 == GNUNET_CONTAINER_multihashmap_size(applications))
2047 GNUNET_SCHEDULER_cancel (announce_applications_task);
2050 GNUNET_CONTAINER_multihashmap_destroy(c->types);
2051 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2063 * Handler for new clients
2065 * @param cls closure
2066 * @param client identification of the client
2067 * @param message the actual message, which includes messages the client wants
2070 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2071 const struct GNUNET_MessageHeader *message)
2073 struct GNUNET_MESH_ClientConnect *cc_msg;
2074 struct MeshClient *c;
2075 GNUNET_MESH_ApplicationType *a;
2082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2083 /* Check data sanity */
2084 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2085 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2086 ntypes = ntohs (cc_msg->types);
2087 napps = ntohs (cc_msg->applications);
2089 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2092 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2096 /* Create new client structure */
2097 c = GNUNET_malloc (sizeof (struct MeshClient));
2099 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2102 GNUNET_MESH_ApplicationType at;
2105 c->apps = GNUNET_CONTAINER_multihashmap_create(napps);
2106 for (i = 0; i < napps; i++)
2109 GNUNET_CRYPTO_hash(&at, sizeof(at), &hc);
2110 /* store in clients hashmap */
2111 GNUNET_CONTAINER_multihashmap_put(
2115 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2116 /* store in global hashmap, for announcements */
2117 GNUNET_CONTAINER_multihashmap_put(
2121 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2123 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2124 announce_applications_task = GNUNET_SCHEDULER_add_now (
2125 &announce_applications, NULL);
2133 t = (uint16_t *) &a[napps];
2134 c->types = GNUNET_CONTAINER_multihashmap_create(ntypes);
2135 for (i =0; i < ntypes; i++)
2138 GNUNET_CRYPTO_hash(&u16, sizeof(u16), &hc);
2139 /* store in clients hashmap */
2140 GNUNET_CONTAINER_multihashmap_put(
2144 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2145 /* store in global hashmap */
2146 GNUNET_CONTAINER_multihashmap_put(
2150 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2154 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2156 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2157 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2158 GNUNET_SERVER_notification_context_add (nc, client);
2160 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2166 * Handler for requests of new tunnels
2168 * @param cls closure
2169 * @param client identification of the client
2170 * @param message the actual message
2173 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2174 const struct GNUNET_MessageHeader *message)
2176 struct GNUNET_MESH_TunnelMessage *t_msg;
2177 struct MeshTunnel *t;
2178 struct MeshClient *c;
2179 GNUNET_HashCode hash;
2181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2183 /* Sanity check for client registration */
2184 if (NULL == (c = retrieve_client (client)))
2187 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2191 /* Message sanity check */
2192 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2195 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2199 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2200 /* Sanity check for tunnel numbering */
2201 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2204 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2207 /* Sanity check for duplicate tunnel IDs */
2208 if (NULL != retrieve_tunnel_by_local_id (c, ntohl (t_msg->tunnel_id)))
2211 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2215 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2216 while (NULL != retrieve_tunnel_by_pi (myid, next_tid))
2217 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2218 t->id.tid = next_tid++;
2220 t->local_tid = ntohl (t_msg->tunnel_id);
2222 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2224 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2226 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2227 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2230 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2234 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2236 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2237 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2240 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2244 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2250 * Handler for requests of deleting tunnels
2252 * @param cls closure
2253 * @param client identification of the client
2254 * @param message the actual message
2257 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2258 const struct GNUNET_MessageHeader *message)
2260 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2261 struct MeshClient *c;
2262 struct MeshTunnel *t;
2263 MESH_TunnelNumber tid;
2264 GNUNET_HashCode hash;
2266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2268 /* Sanity check for client registration */
2269 if (NULL == (c = retrieve_client (client)))
2272 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2275 /* Message sanity check */
2276 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2279 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2283 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2285 /* Retrieve tunnel */
2286 tid = ntohl (tunnel_msg->tunnel_id);
2288 /* Remove from local id hashmap */
2289 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2290 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2291 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2293 /* Remove from global id hashmap */
2294 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2295 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2297 // notify_tunnel_destroy(t);
2298 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2304 * Handler for connection requests to new peers
2306 * @param cls closure
2307 * @param client identification of the client
2308 * @param message the actual message (PeerControl)
2311 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2312 const struct GNUNET_MessageHeader *message)
2314 struct GNUNET_MESH_PeerControl *peer_msg;
2315 struct MeshClient *c;
2316 struct MeshTunnel *t;
2317 MESH_TunnelNumber tid;
2318 struct MeshPeerInfo *peer_info;
2321 /* Sanity check for client registration */
2322 if (NULL == (c = retrieve_client (client)))
2325 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2329 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2330 /* Sanity check for message size */
2331 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2334 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2338 /* Tunnel exists? */
2339 tid = ntohl (peer_msg->tunnel_id);
2340 t = retrieve_tunnel_by_local_id (c, tid);
2344 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2348 /* Does client own tunnel? */
2349 if (t->client->handle != client)
2352 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2357 peer_info = get_peer_info (&peer_msg->peer);
2359 /* Start DHT search if needed */
2360 if (MESH_PEER_READY != peer_info->state && NULL == peer_info->dhtget)
2362 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 */
2363 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2366 0, /* xquery bits */
2368 (void *) peer_info);
2371 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2377 * Handler for disconnection requests of peers in a tunnel
2379 * @param cls closure
2380 * @param client identification of the client
2381 * @param message the actual message (PeerControl)
2384 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2385 const struct GNUNET_MessageHeader *message)
2387 struct GNUNET_MESH_PeerControl *peer_msg;
2388 struct MeshClient *c;
2389 struct MeshTunnel *t;
2390 MESH_TunnelNumber tid;
2392 /* Sanity check for client registration */
2393 if (NULL == (c = retrieve_client (client)))
2396 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2399 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2400 /* Sanity check for message size */
2401 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2404 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2408 /* Tunnel exists? */
2409 tid = ntohl (peer_msg->tunnel_id);
2410 t = retrieve_tunnel_by_local_id (c, tid);
2414 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2418 /* Does client own tunnel? */
2419 if (t->client->handle != client)
2422 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2426 /* Ok, delete peer from tunnel */
2427 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2428 &peer_msg->peer.hashPubKey);
2430 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2436 * Handler for connection requests to new peers by type
2438 * @param cls closure
2439 * @param client identification of the client
2440 * @param message the actual message (ConnectPeerByType)
2443 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2444 const struct GNUNET_MessageHeader *message)
2446 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2447 struct MeshClient *c;
2448 struct MeshTunnel *t;
2449 GNUNET_HashCode hash;
2450 GNUNET_MESH_ApplicationType type;
2451 MESH_TunnelNumber tid;
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 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2494 if (GNUNET_CONTAINER_multihashmap_contains(applications, &hash) == GNUNET_YES)
2496 /* Yes! Fast forward, add ourselves to the tunnel and send the
2497 * good news to the client
2499 struct GNUNET_MESH_PeerControl pc;
2501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2502 pc.peer = my_full_id;
2503 GNUNET_CONTAINER_multihashmap_put (t->peers, &pc.peer.hashPubKey,
2504 get_peer_info (&pc.peer),
2505 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2506 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2507 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2508 pc.tunnel_id = htonl (t->local_tid);
2509 pc.peer = my_full_id;
2510 GNUNET_SERVER_notification_context_unicast (nc, /* context */
2512 &pc.header, /* msg */
2513 GNUNET_NO); /* can drop? */
2514 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2517 /* Ok, lets find a peer offering the service */
2518 if (c->dht_get_type)
2520 GNUNET_DHT_get_stop (c->dht_get_type);
2523 GNUNET_DHT_get_start (dht_handle,
2524 GNUNET_TIME_UNIT_FOREVER_REL,
2525 GNUNET_BLOCK_TYPE_ANY,
2528 GNUNET_DHT_RO_RECORD_ROUTE,
2533 &dht_get_type_handler,
2536 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2542 * Handler for client traffic directed to one peer
2544 * @param cls closure
2545 * @param client identification of the client
2546 * @param message the actual message
2549 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2550 const struct GNUNET_MessageHeader *message)
2552 struct MeshClient *c;
2553 struct MeshTunnel *t;
2554 struct MeshPeerInfo *pi;
2555 struct GNUNET_MESH_Unicast *data_msg;
2556 struct GNUNET_PeerIdentity next_hop;
2557 struct MeshDataDescriptor *info;
2558 MESH_TunnelNumber tid;
2561 /* Sanity check for client registration */
2562 if (NULL == (c = retrieve_client (client)))
2565 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2568 data_msg = (struct GNUNET_MESH_Unicast *) message;
2569 /* Sanity check for message size */
2570 if (sizeof (struct GNUNET_MESH_Unicast) +
2571 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
2574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2578 /* Tunnel exists? */
2579 tid = ntohl (data_msg->tid);
2580 t = retrieve_tunnel_by_local_id (c, tid);
2584 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2588 /* Is it a local tunnel? Then, does client own the tunnel? */
2589 if (t->client->handle != NULL && t->client->handle != client)
2592 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2596 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2597 &data_msg->destination.hashPubKey);
2598 /* Is the selected peer in the tunnel? */
2602 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2607 struct GNUNET_MESH_Unicast copy;
2609 /* Work around const limitation */
2610 memcpy (©, data_msg, sizeof (struct GNUNET_MESH_Unicast));
2611 copy.oid = my_full_id;
2612 copy.tid = htonl(t->id.tid);
2613 handle_mesh_data_unicast (NULL, &my_full_id, ©.header, NULL);
2616 GNUNET_PEER_resolve (get_first_hop (pi->path), &next_hop);
2617 data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
2618 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
2619 memcpy (&info[1], &data_msg[1], data_size);
2620 info->destination = pi->id;
2621 info->origin = &t->id;
2622 info->size = data_size;
2623 info->client = client;
2624 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2625 GNUNET_TIME_UNIT_FOREVER_REL, &next_hop,
2626 /* FIXME re-check types */
2628 sizeof (struct GNUNET_MESH_Unicast),
2629 &send_core_data_unicast, info);
2634 * Handler for client traffic directed to all peers in a tunnel
2636 * @param cls closure
2637 * @param client identification of the client
2638 * @param message the actual message
2641 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2642 const struct GNUNET_MessageHeader *message)
2644 struct MeshClient *c;
2645 struct MeshTunnel *t;
2646 struct GNUNET_MESH_Multicast *data_msg;
2647 MESH_TunnelNumber tid;
2649 /* Sanity check for client registration */
2650 if (NULL == (c = retrieve_client (client)))
2653 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2656 data_msg = (struct GNUNET_MESH_Multicast *) message;
2657 /* Sanity check for message size */
2658 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2661 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2665 /* Tunnel exists? */
2666 tid = ntohl (data_msg->tid);
2667 t = retrieve_tunnel_by_local_id (c, tid);
2671 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2675 /* Does client own tunnel? */
2676 if (t->client->handle != client)
2679 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2685 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2690 * Functions to handle messages from clients
2692 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
2693 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2694 {&handle_local_tunnel_create, NULL,
2695 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2696 sizeof (struct GNUNET_MESH_TunnelMessage)},
2697 {&handle_local_tunnel_destroy, NULL,
2698 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2699 sizeof (struct GNUNET_MESH_TunnelMessage)},
2700 {&handle_local_connect_add, NULL,
2701 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
2702 sizeof (struct GNUNET_MESH_PeerControl)},
2703 {&handle_local_connect_del, NULL,
2704 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
2705 sizeof (struct GNUNET_MESH_PeerControl)},
2706 {&handle_local_connect_by_type, NULL,
2707 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
2708 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
2709 {&handle_local_unicast, NULL,
2710 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2711 {&handle_local_unicast, NULL,
2712 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2713 {&handle_local_multicast, NULL,
2714 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2720 * To be called on core init/fail.
2722 * @param cls service closure
2723 * @param server handle to the server for this service
2724 * @param identity the public identity of this peer
2725 * @param publicKey the public key of this peer
2728 core_init (void *cls, struct GNUNET_CORE_Handle *server,
2729 const struct GNUNET_PeerIdentity *identity,
2730 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
2733 core_handle = server;
2734 my_full_id = *identity;
2735 myid = GNUNET_PEER_intern (identity);
2736 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
2741 * Method called whenever a given peer connects.
2743 * @param cls closure
2744 * @param peer peer identity this notification is about
2745 * @param atsi performance data for the connection
2748 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
2749 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2751 // GNUNET_PEER_Id pid;
2752 struct MeshPeerInfo *peer_info;
2753 struct MeshPath *path;
2755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
2756 peer_info = get_peer_info (peer);
2757 if (myid == peer_info->id)
2759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
2761 path = GNUNET_malloc (sizeof (struct MeshPath));
2763 path->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id) * 2);
2764 path->peers[0] = myid;
2765 path->peers[1] = peer_info->id;
2766 add_path_to_peer (peer_info, path);
2771 * Method called whenever a peer disconnects.
2773 * @param cls closure
2774 * @param peer peer identity this notification is about
2777 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
2779 struct MeshPeerInfo *pi;
2782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
2783 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2789 for (i = 0; i < CORE_QUEUE_SIZE; i++)
2791 if (pi->core_transmit[i])
2793 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
2794 /* TODO: notify that tranmission has failed */
2795 GNUNET_free (pi->infos[i]);
2800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
2806 /******************************************************************************/
2807 /************************ MAIN FUNCTIONS ****************************/
2808 /******************************************************************************/
2811 * Task run during shutdown.
2817 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
2820 if (core_handle != NULL)
2822 GNUNET_CORE_disconnect (core_handle);
2825 if (dht_handle != NULL)
2827 GNUNET_DHT_disconnect (dht_handle);
2832 GNUNET_SERVER_notification_context_destroy (nc);
2835 if (0 != announce_id_task)
2837 GNUNET_SCHEDULER_cancel (announce_id_task);
2838 announce_id_task = 0;
2840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
2844 * Process mesh requests.
2846 * @param cls closure
2847 * @param server the initialized server
2848 * @param c configuration to use
2851 run (void *cls, struct GNUNET_SERVER_Handle *server,
2852 const struct GNUNET_CONFIGURATION_Handle *c)
2854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
2855 GNUNET_SERVER_add_handlers (server, plugin_handlers);
2856 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
2857 server_handle = server;
2858 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
2859 CORE_QUEUE_SIZE, /* queue size */
2860 NULL, /* Closure passed to MESH functions */
2861 &core_init, /* Call core_init once connected */
2862 &core_connect, /* Handle connects */
2863 &core_disconnect, /* remove peers on disconnects */
2864 NULL, /* Do we care about "status" updates? */
2865 NULL, /* Don't notify about all incoming messages */
2866 GNUNET_NO, /* For header only in notification */
2867 NULL, /* Don't notify about all outbound messages */
2868 GNUNET_NO, /* For header-only out notification */
2869 core_handlers); /* Register these handlers */
2870 if (core_handle == NULL)
2874 dht_handle = GNUNET_DHT_connect (c, 64);
2875 if (dht_handle == NULL)
2877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
2878 Running without DHT has a severe\
2879 impact in MESH capabilities.\n\
2880 Plase check your configuretion and enable DHT.\n");
2886 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2887 peers = GNUNET_CONTAINER_multihashmap_create (32);
2888 applications = GNUNET_CONTAINER_multihashmap_create (32);
2889 types = GNUNET_CONTAINER_multihashmap_create (32);
2890 nc = GNUNET_SERVER_notification_context_create (server_handle,
2893 clients_tail = NULL;
2895 announce_applications_task = 0;
2897 /* Scheduled the task to clean up when shutdown is called */
2898 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
2901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
2905 * The main function for the mesh service.
2907 * @param argc number of arguments from the command line
2908 * @param argv command line arguments
2909 * @return 0 ok, 1 on error
2912 main (int argc, char *const *argv)
2916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
2919 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
2921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");