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 insert 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_TEST, sizeof (struct GNUNET_PeerIdentity),
822 (const char *) &my_full_id,
823 // GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
824 // APP_ANNOUNCE_TIME), FIXME
825 // APP_ANNOUNCE_TIME,
826 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
828 &mesh_debug, "DHT_put for app completed");
837 * Periodically announce what applications are provided by local clients
840 * @param tc task context
843 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
845 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
847 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
851 GNUNET_CONTAINER_multihashmap_iterate (applications, &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_TEST, /* 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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
956 GNUNET_PEER_resolve (get_first_hop (peer_info->path), &id);
957 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
958 GNUNET_TIME_UNIT_FOREVER_REL, &id,
960 GNUNET_MESH_ManipulatePath) +
961 (peer_info->path->length *
962 sizeof (struct GNUNET_PeerIdentity)),
963 &send_core_create_path_for_peer,
968 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
969 msg->header.size = htons (size_needed);
970 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
971 msg->tid = 0; /* FIXME */
973 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
974 for (i = 0; i < p->length; i++)
976 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
979 peer_info->state = MESH_PEER_WAITING;
987 * Function called to notify a client about the socket
988 * being ready to queue more data. "buf" will be
989 * NULL and "size" zero if the socket was closed for
990 * writing in the meantime.
992 * @param cls closure (MeshDataDescriptor with all info to build packet)
993 * @param size number of bytes available in buf
994 * @param buf where the callee should write the message
995 * @return number of bytes written to buf
998 send_core_data_to_origin (void *cls, size_t size, void *buf)
1000 struct MeshDataDescriptor *info = cls;
1001 struct GNUNET_MESH_ToOrigin *msg = buf;
1004 GNUNET_assert (NULL != info);
1005 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1006 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1008 if (total_size > size)
1010 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1011 "not enough buffer to send data to origin\n");
1014 msg->header.size = htons (total_size);
1015 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1016 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1017 msg->tid = htonl (info->origin->tid);
1018 if (0 != info->size)
1020 memcpy (&msg[1], &info[1], info->size);
1022 if (NULL != info->client)
1024 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1032 * Function called to notify a client about the socket
1033 * being ready to queue more data. "buf" will be
1034 * NULL and "size" zero if the socket was closed for
1035 * writing in the meantime.
1037 * @param cls closure (data itself)
1038 * @param size number of bytes available in buf
1039 * @param buf where the callee should write the message
1040 * @return number of bytes written to buf
1043 send_core_data_unicast (void *cls, size_t size, void *buf)
1045 struct MeshDataDescriptor *info = cls;
1046 struct GNUNET_MESH_Unicast *msg = buf;
1049 GNUNET_assert (NULL != info);
1050 total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
1051 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1053 if (total_size > size)
1055 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1056 "not enough buffer to send data to peer\n");
1059 msg->header.size = htons (total_size);
1060 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1061 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1062 GNUNET_PEER_resolve (info->destination, &msg->destination);
1063 msg->tid = htonl (info->origin->tid);
1064 if (0 != info->size)
1066 memcpy (&msg[1], &info[1], info->size);
1068 if (NULL != info->client)
1070 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1078 * Function called to notify a client about the socket
1079 * being ready to queue more data. "buf" will be
1080 * NULL and "size" zero if the socket was closed for
1081 * writing in the meantime.
1083 * @param cls closure (data itself)
1084 * @param size number of bytes available in buf
1085 * @param buf where the callee should write the message
1086 * @return number of bytes written to buf
1089 send_core_data_multicast (void *cls, size_t size, void *buf)
1091 struct MeshDataDescriptor *info = cls;
1092 struct GNUNET_MESH_Multicast *msg = buf;
1095 GNUNET_assert (NULL != info);
1096 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1097 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1101 info->peer->core_transmit[info->handler_n] = NULL;
1103 if (total_size > size)
1105 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1106 "not enough buffer to send data futher\n");
1109 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1110 msg->header.size = htons (total_size);
1111 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1112 msg->tid = htonl (info->origin->tid);
1113 memcpy (&msg[1], &info[1], total_size);
1114 if (0 == --info->copies)
1116 if (NULL != info->client)
1118 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1127 * Function called to notify a client about the socket
1128 * being ready to queue more data. "buf" will be
1129 * NULL and "size" zero if the socket was closed for
1130 * writing in the meantime.
1132 * @param cls closure (MeshDataDescriptor)
1133 * @param size number of bytes available in buf
1134 * @param buf where the callee should write the message
1135 * @return number of bytes written to buf
1138 send_core_path_ack (void *cls, size_t size, void *buf)
1140 struct MeshDataDescriptor *info = cls;
1141 struct GNUNET_MESH_PathACK *msg = buf;
1143 GNUNET_assert (NULL != info);
1146 info->peer->core_transmit[info->handler_n] = NULL;
1148 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1153 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1154 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1155 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1156 msg->tid = htonl (info->origin->tid);
1157 msg->peer_id = my_full_id;
1159 /* TODO add signature */
1161 return sizeof (struct GNUNET_MESH_PathACK);
1166 * Function called to notify a client about the socket
1167 * being ready to queue more data. "buf" will be
1168 * NULL and "size" zero if the socket was closed for
1169 * writing in the meantime.
1171 * @param cls closure (data itself)
1172 * @param size number of bytes available in buf
1173 * @param buf where the callee should write the message
1174 * @return number of bytes written to buf
1177 send_core_data_raw (void *cls, size_t size, void *buf)
1179 struct GNUNET_MessageHeader *msg = cls;
1182 GNUNET_assert (NULL != msg);
1183 total_size = ntohs (msg->size);
1185 if (total_size > size)
1190 memcpy (buf, msg, total_size);
1198 * Send another peer a notification to destroy a tunnel
1199 * @param cls The tunnel to destroy
1200 * @param size Size in the buffer
1201 * @param buf Memory where to put the data to transmit
1202 * @return Size of data put in buffer
1205 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1207 struct MeshTunnel *t = cls;
1208 struct MeshClient *c;
1209 struct GNUNET_MESH_TunnelMessage *msg;
1213 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1214 /*FIXME*/ msg->header.size =
1215 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1216 msg->tunnel_id = htonl (t->id.tid);
1218 destroy_tunnel (c, t);
1219 return sizeof (struct GNUNET_MESH_TunnelMessage);
1225 * Send the message to all clients that have subscribed to its type
1227 * @param msg Pointer to the message itself
1228 * @return number of clients this message was sent to
1231 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1233 struct MeshClient *c;
1237 type = ntohs (msg->type);
1238 for (count = 0, c = clients; c != NULL; c = c->next)
1240 if (is_client_subscribed (type, c))
1243 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1252 * Iterator over hash map peer entries collect all neighbors who to resend the
1255 * @param cls closure (**GNUNET_PEER_Id to store hops to send packet)
1256 * @param key current key code (peer id hash)
1257 * @param value value in the hash map (peer_info)
1258 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
1261 iterate_collect_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
1263 struct MeshPeerInfo *peer_info = value;
1264 GNUNET_PEER_Id **neighbors = cls;
1268 if (peer_info->id == myid)
1272 id = get_first_hop (peer_info->path);
1273 for (i = 0; *neighbors[i] != 0; i++)
1275 if (*neighbors[i] == id)
1278 *neighbors = GNUNET_realloc (*neighbors, (i + 2) * sizeof (GNUNET_PEER_Id));
1280 *neighbors[i + 1] = 0;
1286 /******************************************************************************/
1287 /******************** MESH NETWORK HANDLERS **************************/
1288 /******************************************************************************/
1292 * Core handler for path creation
1293 * struct GNUNET_CORE_MessageHandler
1295 * @param cls closure
1296 * @param message message
1297 * @param peer peer identity this notification is about
1298 * @param atsi performance data
1299 * @return GNUNET_OK to keep the connection open,
1300 * GNUNET_SYSERR to close it (signal serious error)
1304 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1305 const struct GNUNET_MessageHeader *message,
1306 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1308 unsigned int own_pos;
1311 MESH_TunnelNumber tid;
1312 struct GNUNET_MESH_ManipulatePath *msg;
1313 struct GNUNET_PeerIdentity *pi;
1314 struct GNUNET_PeerIdentity id;
1315 GNUNET_HashCode hash;
1316 struct MeshPath *path;
1317 struct MeshPeerInfo *dest_peer_info;
1318 struct MeshPeerInfo *orig_peer_info;
1319 struct MeshTunnel *t;
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 "MESH: Received a MESH path create msg\n");
1323 size = ntohs (message->size);
1324 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1326 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1327 "received create path message too short\n");
1331 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1332 if (size < 2 * sizeof (struct GNUNET_PeerIdentity))
1334 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1335 "create path message lacks enough peers\n");
1338 if (size % sizeof (struct GNUNET_PeerIdentity))
1340 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1341 "create path message of wrong size\n");
1344 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1345 size /= sizeof (struct GNUNET_PeerIdentity);
1347 tid = ntohl (msg->tid);
1348 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1349 t = retrieve_tunnel (pi, tid);
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1354 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1355 t->id.oid = GNUNET_PEER_intern (pi);
1357 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1359 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1361 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1362 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1370 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1371 if (NULL == dest_peer_info)
1373 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1374 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1375 dest_peer_info->state = MESH_PEER_WAITING;
1376 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1378 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1380 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1381 if (NULL == orig_peer_info)
1383 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1384 orig_peer_info->id = GNUNET_PEER_intern (pi);
1385 orig_peer_info->state = MESH_PEER_WAITING;
1386 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1387 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1391 path = GNUNET_malloc (sizeof (struct MeshPath));
1392 path->length = size;
1393 path->peers = GNUNET_malloc (size * sizeof (GNUNET_PEER_Id));
1395 for (i = 0; i < size; i++)
1397 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1398 if (path->peers[i] == myid)
1402 { /* cannot be self, must be 'not found' */
1403 /* create path: self not found in path through self */
1404 GNUNET_break_op (0);
1405 destroy_path (path);
1406 /* FIXME error. destroy tunnel? leave for timeout? */
1409 if (own_pos == size - 1)
1410 { /* it is for us! */
1411 struct MeshDataDescriptor *info;
1414 add_path_to_origin (orig_peer_info, path); /* inverts path! */
1415 GNUNET_PEER_resolve (get_first_hop (path), &id); /* path is inverted :) */
1416 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1417 info->origin = &t->id;
1418 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1419 GNUNET_assert (info->peer);
1420 for (j = 0; info->peer->core_transmit[j]; j++)
1428 info->handler_n = j;
1429 info->peer->core_transmit[j] =
1430 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1431 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1432 sizeof (struct GNUNET_MessageHeader),
1433 &send_core_path_ack, info);
1437 add_path_to_peer (dest_peer_info, path);
1438 GNUNET_PEER_resolve (get_first_hop (path), &id);
1439 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1440 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1441 sizeof (struct GNUNET_MessageHeader),
1442 &send_core_create_path_for_peer,
1450 * Core handler for mesh network traffic going from the origin to a peer
1452 * @param cls closure
1453 * @param peer peer identity this notification is about
1454 * @param message message
1455 * @param atsi performance data
1456 * @return GNUNET_OK to keep the connection open,
1457 * GNUNET_SYSERR to close it (signal serious error)
1460 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1461 const struct GNUNET_MessageHeader *message,
1462 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1464 struct GNUNET_MESH_Unicast *msg;
1465 struct GNUNET_PeerIdentity id;
1466 struct MeshTunnel *t;
1467 struct MeshPeerInfo *pi;
1470 size = ntohs (message->size);
1472 sizeof (struct GNUNET_MESH_Unicast) +
1473 sizeof (struct GNUNET_MessageHeader))
1478 msg = (struct GNUNET_MESH_Unicast *) message;
1479 t = retrieve_tunnel (&msg->oid, ntohl (msg->tid));
1482 /* TODO notify back: we don't know this tunnel */
1485 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1486 &msg->destination.hashPubKey);
1489 /* TODO maybe feedback, log to statistics */
1494 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1497 GNUNET_PEER_resolve (get_first_hop (pi->path), &id);
1498 msg = GNUNET_malloc (size);
1499 memcpy (msg, message, size);
1500 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1501 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1502 &send_core_data_raw, msg);
1508 * Core handler for mesh network traffic going from the origin to all peers
1510 * @param cls closure
1511 * @param message message
1512 * @param peer peer identity this notification is about
1513 * @param atsi performance data
1514 * @return GNUNET_OK to keep the connection open,
1515 * GNUNET_SYSERR to close it (signal serious error)
1518 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1519 const struct GNUNET_MessageHeader *message,
1520 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1522 struct GNUNET_MESH_Multicast *msg;
1523 struct GNUNET_PeerIdentity id;
1524 struct MeshTunnel *t;
1525 struct MeshDataDescriptor *info;
1526 GNUNET_PEER_Id *neighbors;
1532 size = ntohs (message->size);
1534 sizeof (struct GNUNET_MESH_Multicast) +
1535 sizeof (struct GNUNET_MessageHeader))
1537 GNUNET_break_op (0);
1540 msg = (struct GNUNET_MESH_Multicast *) message;
1541 t = retrieve_tunnel (&msg->oid, ntohl (msg->tid));
1545 /* TODO notify that we dont know that tunnel */
1549 /* Transmit to locally interested clients */
1550 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1552 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1555 /* Retransmit to other peers */
1556 neighbors = GNUNET_malloc (sizeof (GNUNET_PEER_Id));
1558 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &iterate_collect_neighbors,
1564 size -= sizeof (struct GNUNET_MESH_Multicast);
1565 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + size);
1566 info->origin = &t->id;
1568 for (i = 0; 0 != neighbors[i]; i++)
1570 GNUNET_PEER_resolve (neighbors[i], &id);
1572 info->destination = neighbors[i];
1573 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1574 GNUNET_assert (info->peer);
1575 for (j = 0; info->peer->core_transmit[j]; j++)
1583 info->handler_n = j;
1584 info->peer->infos[j] = info;
1585 info->peer->core_transmit[j] =
1586 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1587 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1588 ntohs (msg->header.size),
1589 &send_core_data_multicast, info);
1596 * Core handler for mesh network traffic
1598 * @param cls closure
1599 * @param message message
1600 * @param peer peer identity this notification is about
1601 * @param atsi performance data
1602 * @return GNUNET_OK to keep the connection open,
1603 * GNUNET_SYSERR to close it (signal serious error)
1606 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1607 const struct GNUNET_MessageHeader *message,
1608 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1610 struct GNUNET_MESH_ToOrigin *msg;
1611 struct GNUNET_PeerIdentity id;
1612 struct MeshTunnel *t;
1613 struct MeshPeerInfo *peer_info;
1616 size = ntohs (message->size);
1618 sizeof (struct GNUNET_MESH_ToOrigin) +
1619 sizeof (struct GNUNET_MessageHeader))
1621 GNUNET_break_op (0);
1624 msg = (struct GNUNET_MESH_ToOrigin *) message;
1625 t = retrieve_tunnel (&msg->oid, ntohl (msg->tid));
1629 /* TODO notify that we dont know this tunnel (whom)? */
1633 if (t->id.oid == myid)
1635 if (NULL == t->client)
1637 /* got data packet for ownerless tunnel */
1638 GNUNET_break_op (0);
1641 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1645 peer_info = get_peer_info (&msg->oid);
1646 if (NULL == peer_info)
1648 /* unknown origin of tunnel */
1652 GNUNET_PEER_resolve (get_first_hop (peer_info->path), &id);
1653 msg = GNUNET_malloc (size);
1654 memcpy (msg, message, size);
1655 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1656 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1657 &send_core_data_raw, msg);
1664 * Core handler for path ACKs
1666 * @param cls closure
1667 * @param message message
1668 * @param peer peer identity this notification is about
1669 * @param atsi performance data
1670 * @return GNUNET_OK to keep the connection open,
1671 * GNUNET_SYSERR to close it (signal serious error)
1674 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1675 const struct GNUNET_MessageHeader *message,
1676 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1678 struct GNUNET_MESH_PathACK *msg;
1679 struct GNUNET_PeerIdentity id;
1680 struct MeshTunnel *t;
1681 struct MeshPeerInfo *peer_info;
1683 msg = (struct GNUNET_MESH_PathACK *) message;
1684 t = retrieve_tunnel (&msg->oid, msg->tid);
1687 /* TODO notify that we don't know the tunnel */
1691 /* Message for us? */
1692 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1694 struct GNUNET_MESH_PeerControl pc;
1696 if (NULL == t->client)
1701 peer_info = get_peer_info (&msg->peer_id);
1702 if (NULL == peer_info)
1704 GNUNET_break_op (0);
1707 peer_info->state = MESH_PEER_READY;
1708 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1709 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1710 pc.tunnel_id = htonl (t->local_tid);
1711 GNUNET_PEER_resolve (peer_info->id, &pc.peer);
1712 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1713 &pc.header, GNUNET_NO);
1717 peer_info = get_peer_info (&msg->oid);
1718 if (NULL == peer_info)
1720 /* If we know the tunnel, we should DEFINITELY know the peer */
1724 GNUNET_PEER_resolve (get_first_hop (peer_info->path), &id);
1725 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1726 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1727 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1728 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1729 sizeof (struct GNUNET_MESH_PathACK),
1730 &send_core_data_raw, msg);
1736 * Functions to handle messages from core
1738 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1739 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1740 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1741 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1742 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1743 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1744 sizeof (struct GNUNET_MESH_PathACK)},
1750 /******************************************************************************/
1751 /**************** MESH LOCAL HANDLER HELPERS ***********************/
1752 /******************************************************************************/
1755 * delete_tunnel_entry: iterator for deleting each tunnel that belongs to a
1756 * client when the client disconnects.
1757 * @param cls closure (client that is disconnecting)
1758 * @param key the hash of the local tunnel id (used to access the hashmap)
1759 * @param value the value stored at the key (tunnel to destroy)
1760 * @return GNUNET_OK on success
1763 delete_tunnel_entry (void *cls, const GNUNET_HashCode * key, void *value)
1767 r = destroy_tunnel ((struct MeshTunnel *) value);
1773 * deregister_app: iterator for removing each application registered by a client
1774 * @param cls closure
1775 * @param key the hash of the application id (used to access the hashmap)
1776 * @param value the value stored at the key (client)
1777 * @return GNUNET_OK on success
1780 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
1782 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
1788 * notify_client_connection_failure: notify a client that the connection to the
1789 * requested remote peer is not possible (for instance, no route found)
1790 * Function called when the socket is ready to queue more data. "buf" will be
1791 * NULL and "size" zero if the socket was closed for writing in the meantime.
1793 * @param cls closure
1794 * @param size number of bytes available in buf
1795 * @param buf where the callee should write the message
1796 * @return number of bytes written to buf
1799 notify_client_connection_failure (void *cls, size_t size, void *buf)
1802 struct MeshPeerInfo *peer_info;
1803 struct GNUNET_MESH_PeerControl *msg;
1804 struct GNUNET_PeerIdentity id;
1806 if (0 == size && NULL == buf)
1808 // TODO retry? cancel?
1812 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
1813 peer_info = (struct MeshPeerInfo *) cls;
1814 msg = (struct GNUNET_MESH_PeerControl *) buf;
1815 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1816 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
1817 // msg->tunnel_id = htonl(peer_info->t->tid);
1818 GNUNET_PEER_resolve (peer_info->id, &id);
1819 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
1827 * Send keepalive packets for a peer
1833 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1835 struct MeshPeerInfo *peer_info = cls;
1836 struct GNUNET_PeerIdentity id;
1838 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1840 GNUNET_PEER_resolve (get_first_hop (peer_info->path), &id);
1841 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1842 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1843 sizeof (struct GNUNET_MESH_ManipulatePath)
1845 (peer_info->path->length *
1846 sizeof (struct GNUNET_PeerIdentity)),
1847 &send_core_create_path_for_peer,
1849 peer_info->path_refresh_task =
1850 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh,
1857 * Function to process paths received for a new peer addition. The recorded
1858 * paths form the initial tunnel, which can be optimized later.
1859 * Called on each result obtained for the DHT search.
1861 * @param cls closure
1862 * @param exp when will this value expire
1863 * @param key key of the result
1864 * @param get_path NULL-terminated array of pointers
1865 * to the peers on reverse GET path (or NULL if not recorded)
1866 * @param put_path NULL-terminated array of pointers
1867 * to the peers on the PUT path (or NULL if not recorded)
1868 * @param type type of the result
1869 * @param size number of bytes in data
1870 * @param data pointer to the result data
1873 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1874 const GNUNET_HashCode * key,
1875 const struct GNUNET_PeerIdentity *const *get_path,
1876 const struct GNUNET_PeerIdentity *const *put_path,
1877 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
1879 struct MeshPeerInfo *peer_info = cls;
1881 struct GNUNET_PeerIdentity pi;
1884 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path)
1886 // Find ourselves some alternate initial path to the destination: retry
1887 GNUNET_DHT_get_stop (peer_info->dhtget);
1888 GNUNET_PEER_resolve (peer_info->id, &pi);
1889 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1890 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_BLOCK_TYPE_TEST, /* type */
1891 &pi.hashPubKey, /*key to search */
1892 4, /* replication level */
1893 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
1896 0, /* xquery bits */
1898 (void *) peer_info);
1901 p = GNUNET_malloc (sizeof (struct MeshPath));
1902 for (i = 0; get_path[i] != NULL; i++) ;
1903 for (i--; i >= 0; i--)
1906 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1907 p->peers[p->length] = GNUNET_PEER_intern (get_path[i]);
1910 for (i = 0; put_path[i] != NULL; i++) ;
1911 for (i--; i >= 0; i--)
1914 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1915 p->peers[p->length] = GNUNET_PEER_intern (put_path[i]);
1918 add_path_to_peer (peer_info, p);
1919 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1920 GNUNET_TIME_UNIT_FOREVER_REL, get_path[1],
1921 sizeof (struct GNUNET_MESH_ManipulatePath)
1924 sizeof (struct GNUNET_PeerIdentity)),
1925 &send_core_create_path_for_peer,
1927 if (0 == peer_info->path_refresh_task)
1929 peer_info->path_refresh_task =
1930 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh,
1938 * Function to process paths received for a new peer addition. The recorded
1939 * paths form the initial tunnel, which can be optimized later.
1940 * Called on each result obtained for the DHT search.
1942 * @param cls closure
1943 * @param exp when will this value expire
1944 * @param key key of the result
1945 * @param get_path NULL-terminated array of pointers
1946 * to the peers on reverse GET path (or NULL if not recorded)
1947 * @param put_path NULL-terminated array of pointers
1948 * to the peers on the PUT path (or NULL if not recorded)
1949 * @param type type of the result
1950 * @param size number of bytes in data
1951 * @param data pointer to the result data
1954 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1955 const GNUNET_HashCode * key,
1956 const struct GNUNET_PeerIdentity *const *get_path,
1957 const struct GNUNET_PeerIdentity *const *put_path,
1958 enum GNUNET_BLOCK_Type type, size_t size,
1961 const struct GNUNET_PeerIdentity *pi = data;
1962 struct GNUNET_PeerIdentity id;
1963 struct MeshTunnel *t = cls;
1964 struct MeshPeerInfo *peer_info;
1968 if (size != sizeof (struct GNUNET_PeerIdentity))
1970 GNUNET_break_op (0);
1973 GNUNET_assert (NULL != t->client);
1974 GNUNET_DHT_get_stop (t->client->dht_get_type);
1975 t->client->dht_get_type = NULL;
1976 peer_info = get_peer_info (pi);
1977 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
1978 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1980 if ((NULL == get_path || NULL == put_path) &&
1981 NULL == peer_info->path &&
1982 NULL == peer_info->dhtget)
1984 /* we don't have a route to the peer, let's try a direct lookup */
1985 peer_info->dhtget = GNUNET_DHT_get_start (
1986 dht_handle, /* handle */
1987 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1988 GNUNET_BLOCK_TYPE_TEST, /* block type */
1989 &pi->hashPubKey, /* key to look up */
1990 10U, /* replication level */
1991 GNUNET_DHT_RO_RECORD_ROUTE, /* option to dht: record route */
1992 NULL, /* bloom filter */
1995 0, /* xquery bits */
1996 dht_get_id_handler, /* callback */
1997 peer_info); /* closure */
2000 p = GNUNET_malloc (sizeof (struct MeshPath));
2001 for (i = 0; get_path[i] != NULL; i++) ;
2002 for (i--; i >= 0; i--)
2005 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
2006 p->peers[p->length] = GNUNET_PEER_intern (get_path[i]);
2009 for (i = 0; put_path[i] != NULL; i++) ;
2010 for (i--; i >= 0; i--)
2013 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
2014 p->peers[p->length] = GNUNET_PEER_intern (put_path[i]);
2017 add_path_to_peer (peer_info, p);
2019 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2020 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2023 for (i = 0; i < p->length; i++)
2025 GNUNET_PEER_resolve(p->peers[0], &id);
2026 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2029 GNUNET_h2s_full(&id.hashPubKey));
2033 GNUNET_PEER_resolve (p->peers[1], &id);
2034 GNUNET_CORE_notify_transmit_ready (
2035 core_handle, /* handle */
2038 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2040 sizeof (struct GNUNET_MESH_ManipulatePath) +
2041 (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
2042 &send_core_create_path_for_peer, /* callback */
2043 peer_info); /* cls */
2046 /******************************************************************************/
2047 /********************* MESH LOCAL HANDLES **************************/
2048 /******************************************************************************/
2052 * Handler for client disconnection
2054 * @param cls closure
2055 * @param client identification of the client; NULL
2056 * for the last call when the server is destroyed
2059 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2061 struct MeshClient *c;
2062 struct MeshClient *next;
2064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2068 if (c->handle != client)
2070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2075 if (NULL != c->tunnels)
2077 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels, &delete_tunnel_entry,
2079 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2082 /* deregister clients applications */
2083 if (NULL != c->apps)
2085 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2086 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2088 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2089 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2091 GNUNET_SCHEDULER_cancel (announce_applications_task);
2092 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2094 if (NULL != c->types)
2095 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2096 if (NULL != c->dht_get_type)
2097 GNUNET_DHT_get_stop (c->dht_get_type);
2098 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2110 * Handler for new clients
2112 * @param cls closure
2113 * @param client identification of the client
2114 * @param message the actual message, which includes messages the client wants
2117 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2118 const struct GNUNET_MessageHeader *message)
2120 struct GNUNET_MESH_ClientConnect *cc_msg;
2121 struct MeshClient *c;
2122 GNUNET_MESH_ApplicationType *a;
2129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2130 /* Check data sanity */
2131 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2132 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2133 ntypes = ntohs (cc_msg->types);
2134 napps = ntohs (cc_msg->applications);
2136 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2139 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2143 /* Create new client structure */
2144 c = GNUNET_malloc (sizeof (struct MeshClient));
2146 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2149 GNUNET_MESH_ApplicationType at;
2152 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2153 for (i = 0; i < napps; i++)
2156 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2157 /* store in clients hashmap */
2158 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2159 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2160 /* store in global hashmap, for announcements */
2161 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2162 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2164 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2165 announce_applications_task =
2166 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2174 t = (uint16_t *) & a[napps];
2175 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2176 for (i = 0; i < ntypes; i++)
2179 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2181 /* store in clients hashmap */
2182 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2183 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2184 /* store in global hashmap */
2185 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2186 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2190 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2192 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2193 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2194 GNUNET_SERVER_notification_context_add (nc, client);
2196 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2202 * Handler for requests of new tunnels
2204 * @param cls closure
2205 * @param client identification of the client
2206 * @param message the actual message
2209 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2210 const struct GNUNET_MessageHeader *message)
2212 struct GNUNET_MESH_TunnelMessage *t_msg;
2213 struct MeshTunnel *t;
2214 struct MeshClient *c;
2215 GNUNET_HashCode hash;
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2219 /* Sanity check for client registration */
2220 if (NULL == (c = retrieve_client (client)))
2223 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2227 /* Message sanity check */
2228 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2231 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2235 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2236 /* Sanity check for tunnel numbering */
2237 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2240 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2243 /* Sanity check for duplicate tunnel IDs */
2244 if (NULL != retrieve_tunnel_by_local_id (c, ntohl (t_msg->tunnel_id)))
2247 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2251 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2252 while (NULL != retrieve_tunnel_by_pi (myid, next_tid))
2253 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2254 t->id.tid = next_tid++;
2256 t->local_tid = ntohl (t_msg->tunnel_id);
2258 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2260 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2262 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2263 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2266 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2270 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2272 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2273 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2276 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2280 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2286 * Handler for requests of deleting tunnels
2288 * @param cls closure
2289 * @param client identification of the client
2290 * @param message the actual message
2293 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2294 const struct GNUNET_MessageHeader *message)
2296 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2297 struct MeshClient *c;
2298 struct MeshTunnel *t;
2299 MESH_TunnelNumber tid;
2300 GNUNET_HashCode hash;
2302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2304 /* Sanity check for client registration */
2305 if (NULL == (c = retrieve_client (client)))
2308 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2311 /* Message sanity check */
2312 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2319 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2321 /* Retrieve tunnel */
2322 tid = ntohl (tunnel_msg->tunnel_id);
2324 /* Remove from local id hashmap */
2325 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2326 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2327 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2329 /* Remove from global id hashmap */
2330 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2331 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2333 // notify_tunnel_destroy(t);
2334 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2340 * Handler for connection requests to new peers
2342 * @param cls closure
2343 * @param client identification of the client
2344 * @param message the actual message (PeerControl)
2347 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2348 const struct GNUNET_MessageHeader *message)
2350 struct GNUNET_MESH_PeerControl *peer_msg;
2351 struct MeshClient *c;
2352 struct MeshTunnel *t;
2353 MESH_TunnelNumber tid;
2354 struct MeshPeerInfo *peer_info;
2357 /* Sanity check for client registration */
2358 if (NULL == (c = retrieve_client (client)))
2361 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2365 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2366 /* Sanity check for message size */
2367 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2370 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2374 /* Tunnel exists? */
2375 tid = ntohl (peer_msg->tunnel_id);
2376 t = retrieve_tunnel_by_local_id (c, tid);
2380 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2384 /* Does client own tunnel? */
2385 if (t->client->handle != client)
2388 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2393 peer_info = get_peer_info (&peer_msg->peer);
2395 /* Start DHT search if needed */
2396 if (MESH_PEER_READY != peer_info->state && NULL == peer_info->dhtget)
2398 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_BLOCK_TYPE_TEST, &peer_msg->peer.hashPubKey, 4, /* replication level */
2399 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2402 0, /* xquery bits */
2404 (void *) peer_info);
2407 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2413 * Handler for disconnection requests of peers in a tunnel
2415 * @param cls closure
2416 * @param client identification of the client
2417 * @param message the actual message (PeerControl)
2420 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2421 const struct GNUNET_MessageHeader *message)
2423 struct GNUNET_MESH_PeerControl *peer_msg;
2424 struct MeshClient *c;
2425 struct MeshTunnel *t;
2426 MESH_TunnelNumber tid;
2428 /* Sanity check for client registration */
2429 if (NULL == (c = retrieve_client (client)))
2432 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2435 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2436 /* Sanity check for message size */
2437 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2440 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2444 /* Tunnel exists? */
2445 tid = ntohl (peer_msg->tunnel_id);
2446 t = retrieve_tunnel_by_local_id (c, tid);
2450 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2454 /* Does client own tunnel? */
2455 if (t->client->handle != client)
2458 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2462 /* Ok, delete peer from tunnel */
2463 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2464 &peer_msg->peer.hashPubKey);
2466 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2472 * Handler for connection requests to new peers by type
2474 * @param cls closure
2475 * @param client identification of the client
2476 * @param message the actual message (ConnectPeerByType)
2479 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2480 const struct GNUNET_MessageHeader *message)
2482 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2483 struct MeshClient *c;
2484 struct MeshTunnel *t;
2485 GNUNET_HashCode hash;
2486 GNUNET_MESH_ApplicationType type;
2487 MESH_TunnelNumber tid;
2489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2490 /* Sanity check for client registration */
2491 if (NULL == (c = retrieve_client (client)))
2494 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2498 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2499 /* Sanity check for message size */
2500 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2501 ntohs (connect_msg->header.size))
2504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2508 /* Tunnel exists? */
2509 tid = ntohl (connect_msg->tunnel_id);
2510 t = retrieve_tunnel_by_local_id (c, tid);
2514 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2518 /* Does client own tunnel? */
2519 if (t->client->handle != client)
2522 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2526 /* Do WE have the service? */
2527 type = ntohl (connect_msg->type);
2528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2529 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2530 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2533 /* Yes! Fast forward, add ourselves to the tunnel and send the
2534 * good news to the client
2536 struct GNUNET_MESH_PeerControl pc;
2538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2539 pc.peer = my_full_id;
2540 GNUNET_CONTAINER_multihashmap_put (t->peers, &pc.peer.hashPubKey,
2541 get_peer_info (&pc.peer),
2542 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2543 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2544 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2545 pc.tunnel_id = htonl (t->local_tid);
2546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2547 GNUNET_SERVER_notification_context_unicast (nc, /* context */
2549 &pc.header, /* msg */
2550 GNUNET_NO); /* can drop? */
2551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2552 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2555 /* Ok, lets find a peer offering the service */
2556 if (c->dht_get_type)
2558 GNUNET_DHT_get_stop (c->dht_get_type);
2560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2561 GNUNET_h2s_full (&hash));
2563 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2564 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2565 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
2566 &dht_get_type_handler, t);
2568 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2574 * Handler for client traffic directed to one peer
2576 * @param cls closure
2577 * @param client identification of the client
2578 * @param message the actual message
2581 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2582 const struct GNUNET_MessageHeader *message)
2584 struct MeshClient *c;
2585 struct MeshTunnel *t;
2586 struct MeshPeerInfo *pi;
2587 struct GNUNET_MESH_Unicast *data_msg;
2588 struct GNUNET_PeerIdentity next_hop;
2589 struct MeshDataDescriptor *info;
2590 MESH_TunnelNumber tid;
2593 /* Sanity check for client registration */
2594 if (NULL == (c = retrieve_client (client)))
2597 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2600 data_msg = (struct GNUNET_MESH_Unicast *) message;
2601 /* Sanity check for message size */
2602 if (sizeof (struct GNUNET_MESH_Unicast) +
2603 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
2606 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2610 /* Tunnel exists? */
2611 tid = ntohl (data_msg->tid);
2612 t = retrieve_tunnel_by_local_id (c, tid);
2616 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2620 /* Is it a local tunnel? Then, does client own the tunnel? */
2621 if (t->client->handle != NULL && t->client->handle != client)
2624 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2628 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2629 &data_msg->destination.hashPubKey);
2630 /* Is the selected peer in the tunnel? */
2634 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2639 struct GNUNET_MESH_Unicast copy;
2641 /* Work around const limitation */
2642 memcpy (©, data_msg, sizeof (struct GNUNET_MESH_Unicast));
2643 copy.oid = my_full_id;
2644 copy.tid = htonl (t->id.tid);
2645 handle_mesh_data_unicast (NULL, &my_full_id, ©.header, NULL);
2648 GNUNET_PEER_resolve (get_first_hop (pi->path), &next_hop);
2649 data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
2650 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
2651 memcpy (&info[1], &data_msg[1], data_size);
2652 info->destination = pi->id;
2653 info->origin = &t->id;
2654 info->size = data_size;
2655 info->client = client;
2656 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2657 GNUNET_TIME_UNIT_FOREVER_REL, &next_hop,
2658 /* FIXME re-check types */
2660 sizeof (struct GNUNET_MESH_Unicast),
2661 &send_core_data_unicast, info);
2666 * Handler for client traffic directed to all peers in a tunnel
2668 * @param cls closure
2669 * @param client identification of the client
2670 * @param message the actual message
2673 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2674 const struct GNUNET_MessageHeader *message)
2676 struct MeshClient *c;
2677 struct MeshTunnel *t;
2678 struct GNUNET_MESH_Multicast *data_msg;
2679 MESH_TunnelNumber tid;
2681 /* Sanity check for client registration */
2682 if (NULL == (c = retrieve_client (client)))
2685 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2688 data_msg = (struct GNUNET_MESH_Multicast *) message;
2689 /* Sanity check for message size */
2690 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2693 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2697 /* Tunnel exists? */
2698 tid = ntohl (data_msg->tid);
2699 t = retrieve_tunnel_by_local_id (c, tid);
2703 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2707 /* Does client own tunnel? */
2708 if (t->client->handle != client)
2711 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2717 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2722 * Functions to handle messages from clients
2724 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
2725 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2726 {&handle_local_tunnel_create, NULL,
2727 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2728 sizeof (struct GNUNET_MESH_TunnelMessage)},
2729 {&handle_local_tunnel_destroy, NULL,
2730 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2731 sizeof (struct GNUNET_MESH_TunnelMessage)},
2732 {&handle_local_connect_add, NULL,
2733 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
2734 sizeof (struct GNUNET_MESH_PeerControl)},
2735 {&handle_local_connect_del, NULL,
2736 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
2737 sizeof (struct GNUNET_MESH_PeerControl)},
2738 {&handle_local_connect_by_type, NULL,
2739 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
2740 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
2741 {&handle_local_unicast, NULL,
2742 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2743 {&handle_local_unicast, NULL,
2744 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2745 {&handle_local_multicast, NULL,
2746 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2752 * To be called on core init/fail.
2754 * @param cls service closure
2755 * @param server handle to the server for this service
2756 * @param identity the public identity of this peer
2757 * @param publicKey the public key of this peer
2760 core_init (void *cls, struct GNUNET_CORE_Handle *server,
2761 const struct GNUNET_PeerIdentity *identity,
2762 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
2765 core_handle = server;
2766 my_full_id = *identity;
2767 myid = GNUNET_PEER_intern (identity);
2768 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
2773 * Method called whenever a given peer connects.
2775 * @param cls closure
2776 * @param peer peer identity this notification is about
2777 * @param atsi performance data for the connection
2780 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
2781 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2783 // GNUNET_PEER_Id pid;
2784 struct MeshPeerInfo *peer_info;
2785 struct MeshPath *path;
2787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
2788 peer_info = get_peer_info (peer);
2789 if (myid == peer_info->id)
2791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
2793 path = GNUNET_malloc (sizeof (struct MeshPath));
2795 path->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id) * 2);
2796 path->peers[0] = myid;
2797 path->peers[1] = peer_info->id;
2798 add_path_to_peer (peer_info, path);
2803 * Method called whenever a peer disconnects.
2805 * @param cls closure
2806 * @param peer peer identity this notification is about
2809 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
2811 struct MeshPeerInfo *pi;
2814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
2815 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2821 for (i = 0; i < CORE_QUEUE_SIZE; i++)
2823 if (pi->core_transmit[i])
2825 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
2826 /* TODO: notify that tranmission has failed */
2827 GNUNET_free (pi->infos[i]);
2832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
2838 /******************************************************************************/
2839 /************************ MAIN FUNCTIONS ****************************/
2840 /******************************************************************************/
2843 * Task run during shutdown.
2849 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2851 struct MeshClient *c;
2853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
2854 if (core_handle != NULL)
2856 GNUNET_CORE_disconnect (core_handle);
2859 if (dht_handle != NULL)
2861 for (c = clients; NULL != c; c = c->next)
2862 if (NULL != c->dht_get_type)
2863 GNUNET_DHT_get_stop (c->dht_get_type);
2864 GNUNET_DHT_disconnect (dht_handle);
2869 GNUNET_SERVER_notification_context_destroy (nc);
2872 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
2874 GNUNET_SCHEDULER_cancel (announce_id_task);
2875 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
2877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
2881 * Process mesh requests.
2883 * @param cls closure
2884 * @param server the initialized server
2885 * @param c configuration to use
2888 run (void *cls, struct GNUNET_SERVER_Handle *server,
2889 const struct GNUNET_CONFIGURATION_Handle *c)
2891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
2892 GNUNET_SERVER_add_handlers (server, client_handlers);
2893 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
2894 server_handle = server;
2895 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
2896 CORE_QUEUE_SIZE, /* queue size */
2897 NULL, /* Closure passed to MESH functions */
2898 &core_init, /* Call core_init once connected */
2899 &core_connect, /* Handle connects */
2900 &core_disconnect, /* remove peers on disconnects */
2901 NULL, /* Do we care about "status" updates? */
2902 NULL, /* Don't notify about all incoming messages */
2903 GNUNET_NO, /* For header only in notification */
2904 NULL, /* Don't notify about all outbound messages */
2905 GNUNET_NO, /* For header-only out notification */
2906 core_handlers); /* Register these handlers */
2907 if (core_handle == NULL)
2911 dht_handle = GNUNET_DHT_connect (c, 64);
2912 if (dht_handle == NULL)
2914 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
2915 Running without DHT has a severe\
2916 impact in MESH capabilities.\n\
2917 Plase check your configuretion and enable DHT.\n");
2923 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2924 peers = GNUNET_CONTAINER_multihashmap_create (32);
2925 applications = GNUNET_CONTAINER_multihashmap_create (32);
2926 types = GNUNET_CONTAINER_multihashmap_create (32);
2927 nc = GNUNET_SERVER_notification_context_create (server_handle,
2930 clients_tail = NULL;
2932 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2934 /* Scheduled the task to clean up when shutdown is called */
2935 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
2942 * The main function for the mesh service.
2944 * @param argc number of arguments from the command line
2945 * @param argv command line arguments
2946 * @return 0 ok, 1 on error
2949 main (int argc, char *const *argv)
2953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
2956 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
2958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");