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,\
93 /******************************************************************************/
94 /************************ ENUMERATIONS ****************************/
95 /******************************************************************************/
98 * All the states a peer participating in a tunnel can be in.
103 * Peer only retransmits traffic, is not a final destination
108 * Path to the peer not known yet
113 * Request sent, not yet answered.
118 * Peer connected and ready to accept data
123 * Peer connected previosly but not responding
125 MESH_PEER_RECONNECTING
129 /******************************************************************************/
130 /************************ DATA STRUCTURES ****************************/
131 /******************************************************************************/
134 * Information regarding a possible path to reach a single peer
142 struct MeshPeerPath *next;
143 struct MeshPeerPath *prev;
146 * List of all the peers that form the path from origin to target.
148 GNUNET_PEER_Id *peers;
151 * Number of peers (hops) in the path
159 * Node of path tree for a tunnel
161 struct MeshTunnelPathNode
164 * Tunnel this node belongs to (and therefore tree)
166 struct MeshTunnel *t;
169 * Peer this node describes
171 struct MeshPeerInfo *peer;
174 * Parent node in the tree
176 struct MeshTunnelPathNode *parent;
181 struct MeshTunnelPathNode *children;
186 unsigned int nchildren;
189 * Status of the peer in the tunnel
191 enum MeshPeerState status;
196 * Tree to reach all peers in the tunnel
198 struct MeshTunnelPath
201 * Tunnel this path belongs to
203 struct MeshTunnel *t;
206 * Root node of peer tree
208 struct MeshTunnelPathNode *root;
211 * Node that represents our position in the tree (for non local tunnels)
213 struct MeshTunnelPathNode *me;
216 * Cache of all peers and the first hop to them.
217 * Indexed by Peer_Identity, contains a pointer to the PeerInfo of 1st hop.
219 struct GNUNET_CONTAINER_MultiHashMap *first_hops;
224 /** FWD declaration */
228 * Struct containing all info possibly needed to build a package when called
231 struct MeshDataDescriptor
233 /** ID of the tunnel this packet travels in */
234 struct MESH_TunnelID *origin;
236 /** Ultimate destination of the packet */
237 GNUNET_PEER_Id destination;
239 /** Number of identical messages sent to different hops (multicast) */
242 /** Size of the data */
245 /** Client that asked for the transmission, if any */
246 struct GNUNET_SERVER_Client *client;
248 /** Who was is message being sent to */
249 struct MeshPeerInfo *peer;
251 /** Which handler was used to request the transmission */
252 unsigned int handler_n;
254 /* Data at the end */
259 * Struct containing all information regarding a given peer
269 * Last time we heard from this peer
271 struct GNUNET_TIME_Absolute last_contact;
274 * Number of attempts to reconnect so far
276 int n_reconnect_attempts;
279 * Paths to reach the peer, ordered by ascending hop count
281 struct MeshPeerPath *path_head;
284 * Paths to reach the peer, ordered by ascending hop count
286 struct MeshPeerPath *path_tail;
289 * Handle to stop the DHT search for a path to this peer
291 struct GNUNET_DHT_GetHandle *dhtget;
294 * Handles to stop queued transmissions for this peer
296 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
299 * Pointer to info stuctures used as cls for queued transmissions
301 struct MeshDataDescriptor *infos[CORE_QUEUE_SIZE];
304 * Array of tunnels this peer participates in
305 * (most probably a small amount, therefore not a hashmap)
306 * When the path to the peer changes, notify these tunnels to let them
307 * re-adjust their path trees.
309 struct MeshTunnel **tunnels;
312 * Number of tunnels above
314 unsigned int ntunnels;
319 * Data scheduled to transmit (to local client or remote peer)
326 struct MeshQueue *next;
327 struct MeshQueue *prev;
330 * Target of the data (NULL if target is client)
332 struct MeshPeerInfo *peer;
335 * Client to send the data to (NULL if target is peer)
337 struct MeshClient *client;
340 * Size of the message to transmit
345 * How old is the data?
347 struct GNUNET_TIME_Absolute timestamp;
352 struct GNUNET_MessageHeader *data;
356 * Globally unique tunnel identification (owner + number)
357 * DO NOT USE OVER THE NETWORK
362 * Node that owns the tunnel
367 * Tunnel number to differentiate all the tunnels owned by the node oid
368 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
370 MESH_TunnelNumber tid;
374 struct MeshClient; /* FWD declaration */
377 * Struct containing all information regarding a tunnel
378 * For an intermediate node the improtant info used will be:
379 * - id Tunnel unique identification
380 * - paths[0] To know where to send it next
381 * - metainfo: ready, speeds, accounting
388 struct MESH_TunnelID id;
391 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
393 MESH_TunnelNumber local_tid;
396 * Last time the tunnel was used
398 struct GNUNET_TIME_Absolute timestamp;
401 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
403 struct GNUNET_CONTAINER_MultiHashMap *peers;
406 * Number of peers that are connected and potentially ready to receive data
408 unsigned int peers_ready;
411 * Number of peers that have been added to the tunnel
413 unsigned int peers_total;
416 * Client owner of the tunnel, if any
418 struct MeshClient *client;
421 * Messages ready to transmit
423 struct MeshQueue *queue_head;
424 struct MeshQueue *queue_tail;
429 struct MeshTunnelPath *paths;
432 * Task to keep the used paths alive
434 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
439 * Info needed to work with tunnel paths and peers
446 struct MeshTunnel *t;
451 struct MeshPeerInfo *peer;
456 struct MeshPeerPath *path;
461 * Struct containing information about a client of the service
468 struct MeshClient *next;
469 struct MeshClient *prev;
472 * Tunnels that belong to this client, indexed by local id
474 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
477 * Handle to communicate with the client
479 struct GNUNET_SERVER_Client *handle;
482 * Applications that this client has claimed to provide
484 struct GNUNET_CONTAINER_MultiHashMap *apps;
487 * Messages that this client has declared interest in
489 struct GNUNET_CONTAINER_MultiHashMap *types;
492 * Used to search peers offering a service
494 struct GNUNET_DHT_GetHandle *dht_get_type;
498 /******************************************************************************/
499 /*********************** GLOBAL VARIABLES ****************************/
500 /******************************************************************************/
505 static struct MeshClient *clients;
506 static struct MeshClient *clients_tail;
509 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
511 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
514 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
516 static struct GNUNET_CONTAINER_MultiHashMap *peers;
519 * Handle to communicate with core
521 static struct GNUNET_CORE_Handle *core_handle;
526 static struct GNUNET_DHT_Handle *dht_handle;
531 static struct GNUNET_SERVER_Handle *server_handle;
534 * Notification context, to send messages to local clients
536 static struct GNUNET_SERVER_NotificationContext *nc;
539 * Local peer own ID (memory efficient handle)
541 static GNUNET_PEER_Id myid;
544 * Local peer own ID (full value)
546 static struct GNUNET_PeerIdentity my_full_id;
549 * Tunnel ID for the next created tunnel (global tunnel number)
551 static MESH_TunnelNumber next_tid;
554 * All application types provided by this peer
556 static struct GNUNET_CONTAINER_MultiHashMap *applications;
559 * All message types clients of this peer are interested in
561 static struct GNUNET_CONTAINER_MultiHashMap *types;
564 * Task to periodically announce provided applications
566 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
569 * Task to periodically announce itself in the network
571 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
574 /******************************************************************************/
575 /************************ PERIODIC FUNCTIONS ****************************/
576 /******************************************************************************/
579 * Announce iterator over for each application provided by the peer
582 * @param key current key code
583 * @param value value in the hash map
584 * @return GNUNET_YES if we should continue to
589 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
591 /* FIXME are hashes in multihash map equal on all aquitectures? */
592 GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
593 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
594 (const char *) &my_full_id,
596 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
597 &mesh_debug, "DHT_put for app completed");
599 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
601 APP_ANNOUNCE_TIME, NULL, NULL);
608 * Periodically announce what applications are provided by local clients
611 * @param tc task context
614 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
616 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
618 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
622 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
624 announce_applications_task =
625 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
633 * Periodically announce self id in the DHT
636 * @param tc task context
639 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
641 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
643 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
647 * - Set data expiration in function of X
650 GNUNET_DHT_put (dht_handle, /* DHT handle */
651 &my_full_id.hashPubKey, /* Key to use */
652 10U, /* Replication level */
653 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
654 GNUNET_BLOCK_TYPE_TEST, /* Block type */
655 0, /* Size of the data */
656 NULL, /* Data itself */
657 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
658 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
660 &mesh_debug, "DHT_put for id completed");
662 NULL, /* Continuation */
663 NULL); /* Continuation closure */
666 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
671 * Send keepalive packets for a peer
679 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
681 struct MeshTunnel *t = cls;
683 // struct GNUNET_PeerIdentity id;
685 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
689 /* FIXME implement multicast keepalive. Just an empty multicast packet? */
690 // GNUNET_PEER_resolve (path_get_first_hop (path->t, path->peer)->id, &id);
691 // GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
692 // GNUNET_TIME_UNIT_FOREVER_REL, &id,
693 // sizeof (struct GNUNET_MESH_ManipulatePath)
695 // (path->path->length *
696 // sizeof (struct GNUNET_PeerIdentity)),
697 // &send_core_create_path,
699 t->path_refresh_task =
700 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
705 /******************************************************************************/
706 /****************** GENERAL HELPER FUNCTIONS ************************/
707 /******************************************************************************/
710 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
711 * and insert it in the appropiate structures if the peer is not known yet.
713 * @param peer Identity of the peer
715 * @return Existing or newly created peer info
717 static struct MeshPeerInfo *
718 peer_info_get (const struct GNUNET_PeerIdentity *peer)
720 struct MeshPeerInfo *peer_info;
722 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
723 if (NULL == peer_info)
726 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
727 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
728 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
729 peer_info->id = GNUNET_PEER_intern (peer);
738 * Destroy the peer_info and free any allocated resources linked to it
739 * @param t tunnel the path belongs to
740 * @param pi the peer_info to destroy
741 * @return GNUNET_OK on success
744 peer_info_destroy (struct MeshPeerInfo *pi)
746 GNUNET_HashCode hash;
747 struct GNUNET_PeerIdentity id;
749 GNUNET_PEER_resolve (pi->id, &id);
750 GNUNET_PEER_change_rc (pi->id, -1);
751 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
753 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
754 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
762 * Destroy the path and free any allocated resources linked to it
764 * @param p the path to destroy
766 * @return GNUNET_OK on success
769 path_destroy (struct MeshPeerPath *p)
771 GNUNET_PEER_decrement_rcs (p->peers, p->length);
772 GNUNET_free (p->peers);
779 * Find the first peer whom to send a packet to go down this path
781 * @param t The tunnel to use
782 * @param peer The peerinfo of the peer we are trying to reach
784 * @return peerinfo of the peer who is the first hop in the tunnel
787 static struct MeshPeerInfo *
788 path_get_first_hop (struct MeshTunnel *t, struct MeshPeerInfo *peer)
790 struct GNUNET_PeerIdentity id;
792 GNUNET_PEER_resolve (peer->id, &id);
793 return GNUNET_CONTAINER_multihashmap_get (t->paths->first_hops,
799 * Get the length of a path
801 * @param path The path to measure, with the local peer at any point of it
803 * @return Number of hops to reach destination
804 * UINT_MAX in case the peer is not in the path
807 path_get_length (struct MeshPeerPath *path)
813 for (i = 0; i < path->length; i++)
815 if (path->peers[i] == myid)
817 return path->length - i;
825 * Get the cost of the path relative to the already built tunnel tree
827 * @param t The tunnel to which compare
828 * @param path The individual path to reach a peer
830 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
833 * TODO: remove dummy implementation, look into the tunnel tree
836 path_get_cost (struct MeshTunnel *t, struct MeshPeerPath *path)
838 return path_get_length (path);
843 * Add the path to the peer and update the path used to reach it in case this
846 * @param peer_info Destination peer to add the path to.
847 * @param path New path to add. Last peer must be the peer in arg 1.
849 * TODO: trim the part from origin to us? Add it as path to origin?
852 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
855 struct MeshPeerPath *aux;
857 if (NULL == peer_info || NULL == path)
863 l = path_get_length (path);
865 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
867 if (path_get_length (aux) > l)
869 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
870 peer_info->path_tail, aux, path);
873 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
880 * Notify a tunnel that a connection has broken that affects at least
883 * @param t Tunnel affected
884 * @param peer Peer that (at least) has been affected by the disconnection
885 * @param p1 Peer that got disconnected from p2
886 * @param p2 Peer that got disconnected from p1
889 tunnel_notify_connection_broken (struct MeshTunnel *t,
890 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
895 * Remove all paths that rely on a direct connection between p1 and p2
896 * from the peer itself and notify all tunnels about it.
898 * @param pi PeerInfo of affected peer
899 * @param p1 GNUNET_PEER_Id of one peer.
900 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
904 path_remove_from_peer (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
907 struct MeshPeerPath *p;
908 struct MeshPeerPath *aux;
909 unsigned int destroyed;
917 for (i = 0; i < (p->length - 1); i++)
919 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
920 (p->peers[i] == p2 && p->peers[i + 1] == p1))
932 for (i = 0; i < peer->ntunnels; i++)
934 tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
940 * Add the path to the origin peer and update the path used to reach it in case
941 * this is the shortest.
942 * The path is given in peer_info -> destination, therefore we turn the path
945 * @param peer_info Peer to add the path to, being the origin of the path.
946 * @param path New path to add after being inversed.
949 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
954 for (i = 0; i < path->length / 2; i++)
956 aux = path->peers[i];
957 path->peers[i] = path->peers[path->length - i - 1];
958 path->peers[path->length - i - 1] = aux;
960 path_add_to_peer (peer_info, path);
965 * Build a PeerPath from the paths returned from the DHT, reversing the paths
966 * to obtain a local peer -> destination path and interning the peer ids.
968 * @param get_path NULL-terminated array of pointers
969 * to the peers on reverse GET path (or NULL if not recorded)
970 * @param put_path NULL-terminated array of pointers
971 * to the peers on the PUT path (or NULL if not recorded)
973 * @return Newly allocated and created path
975 static struct MeshPeerPath *
976 path_build_from_dht (const struct GNUNET_PeerIdentity *const *get_path,
977 const struct GNUNET_PeerIdentity *const *put_path)
979 struct MeshPeerPath *p;
982 p = GNUNET_malloc (sizeof (struct MeshPeerPath));
983 for (i = 0; get_path[i] != NULL; i++) ;
984 for (i--; i >= 0; i--)
987 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
988 p->peers[p->length] = GNUNET_PEER_intern (get_path[i]);
991 for (i = 0; put_path[i] != NULL; i++) ;
992 for (i--; i >= 0; i--)
995 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
996 p->peers[p->length] = GNUNET_PEER_intern (put_path[i]);
1004 * Check if client has registered with the service and has not disconnected
1006 * @param client the client to check
1008 * @return non-NULL if client exists in the global DLL
1010 static struct MeshClient *
1011 client_get (struct GNUNET_SERVER_Client *client)
1013 struct MeshClient *c;
1018 if (c->handle == client)
1027 * Checks if a given client has subscribed to certain message type
1029 * @param message_type Type of message to check
1030 * @param c Client to check
1032 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1037 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1041 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1042 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1047 * Search for a tunnel among the tunnels for a client
1049 * @param c the client whose tunnels to search in
1050 * @param tid the local id of the tunnel
1052 * @return tunnel handler, NULL if doesn't exist
1054 static struct MeshTunnel *
1055 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1057 GNUNET_HashCode hash;
1059 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1060 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1065 * Search for a tunnel by global ID using PEER_ID
1067 * @param pi owner of the tunnel
1068 * @param tid global tunnel number
1070 * @return tunnel handler, NULL if doesn't exist
1072 static struct MeshTunnel *
1073 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1075 struct MESH_TunnelID id;
1076 GNUNET_HashCode hash;
1081 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1082 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1088 * Search for a tunnel by global ID using full PeerIdentities
1090 * @param oid owner of the tunnel
1091 * @param tid global tunnel number
1093 * @return tunnel handler, NULL if doesn't exist
1095 static struct MeshTunnel *
1096 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1098 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1103 * Recursively find the given peer in the tree.
1105 * @param t Tunnel where to add the new path.
1106 * @param p Path to look for.
1108 * @return Pointer to the node of the peer. NULL if not found.
1110 static struct MeshTunnelPathNode *
1111 tunnel_find_peer (struct MeshTunnelPathNode *root, struct MeshPeerInfo *peer)
1113 struct MeshTunnelPathNode *n;
1116 if (root->peer == peer)
1118 for (i = 0; i < root->nchildren; i++)
1120 n = tunnel_find_peer (&root->children[i], peer);
1129 * Recusively mark peer and children as disconnected, notify client
1131 * @param parent Node to be clean, potentially with children
1134 tunnel_mark_peers_disconnected (struct MeshTunnelPathNode *parent)
1136 struct GNUNET_MESH_PeerControl msg;
1139 parent->status = MESH_PEER_RECONNECTING;
1140 for (i = 0; i < parent->nchildren; i++)
1142 tunnel_mark_peers_disconnected (&parent->children[i]);
1144 if (NULL == parent->t->client)
1146 msg.header.size = htons (sizeof (msg));
1147 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1148 msg.tunnel_id = htonl (parent->t->local_tid);
1149 GNUNET_PEER_resolve (parent->peer->id, &msg.peer);
1150 GNUNET_SERVER_notification_context_unicast (nc, parent->t->client->handle,
1151 &msg.header, GNUNET_NO);
1155 * Delete the current path to the peer, including all now unused relays.
1157 * @param t Tunnel where to add the new path.
1158 * @param p Path to be integrated.
1160 * @return pointer to the pathless node, NULL on error
1162 * TODO: notify peers of deletion
1164 static struct MeshTunnelPathNode *
1165 tunnel_del_path (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1167 struct MeshTunnelPathNode *parent;
1168 struct MeshTunnelPathNode *node;
1169 struct MeshTunnelPathNode *n;
1171 node = n = tunnel_find_peer (t->paths->me, peer);
1176 while (NULL != parent && MESH_PEER_RELAY == parent->status &&
1177 1 == parent->nchildren)
1180 GNUNET_free (parent->children);
1181 parent = parent->parent;
1185 *n = parent->children[parent->nchildren - 1];
1186 parent->nchildren--;
1187 parent->children = GNUNET_realloc (parent->children, parent->nchildren);
1189 tunnel_mark_peers_disconnected (node);
1196 * Integrate a stand alone path into the tunnel tree.
1198 * @param t Tunnel where to add the new path.
1199 * @param p Path to be integrated.
1201 * @return GNUNET_OK in case of success.
1202 * GNUNET_SYSERR in case of error.
1205 * - go backwards on path looking for each peer in the present tree
1208 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1210 struct MeshTunnelPathNode *parent;
1211 struct MeshTunnelPathNode *oldnode;
1212 struct MeshTunnelPathNode *n;
1213 struct GNUNET_PeerIdentity id;
1214 struct GNUNET_PeerIdentity hop;
1220 if (n->peer->id != p->peers[0])
1223 return GNUNET_SYSERR;
1225 /* Ignore return value, if not found it's ok. */
1226 GNUNET_PEER_resolve (p->peers[p->length - 1], &id);
1227 oldnode = tunnel_del_path (t, peer_info_get (&id));
1228 /* Look for the first node that is not already present in the tree
1230 * Assuming that the tree is somewhat balanced, O(log n * log N).
1231 * - Length of the path is expected to be log N (size of whole network).
1232 * - Each level of the tree is expected to have log n children (size of tree).
1234 for (i = 1, me = -1; i < p->length; i++)
1237 if (p->peers[i] == myid)
1239 for (j = 0; j < n->nchildren; j++)
1241 if (n->children[j].peer->id == p->peers[i])
1243 n = &n->children[j];
1247 /* If we couldn't find a child equal to path[i], we have reached the end
1248 * of the common path. */
1254 /* New path deviates from tree before reaching us. What happened? */
1256 return GNUNET_SYSERR;
1258 /* Add the rest of the path as a branch from parent. */
1259 while (i < p->length)
1261 parent->nchildren++;
1262 parent->children = GNUNET_realloc (parent->children, parent->nchildren);
1263 n = &parent->children[parent->nchildren - 1];
1264 if (i == p->length - 1)
1266 if (NULL != oldnode)
1268 /* Assignation and free can be misleading, using explicit mempcy */
1269 memcpy (n, oldnode, sizeof (struct MeshTunnelPathNode));
1270 GNUNET_free (oldnode);
1276 n->status = MESH_PEER_RELAY;
1277 GNUNET_PEER_resolve (p->peers[i], &id);
1278 n->peer = peer_info_get (&id);
1285 /* Add info about first hop into hashmap. */
1286 if (me < p->length - 1)
1288 GNUNET_PEER_resolve (p->peers[p->length - 1], &id);
1289 GNUNET_PEER_resolve (p->peers[me + 1], &hop);
1290 GNUNET_CONTAINER_multihashmap_put (t->paths->first_hops, &id.hashPubKey,
1291 peer_info_get (&hop),
1292 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1299 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1300 * needed rescources.
1302 * @param t Tunnel we want to add a new peer to
1303 * @param peer PeerInfo of the peer being added
1307 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1309 struct MeshPeerPath *p;
1310 struct MeshPeerPath *best_p;
1311 unsigned int best_cost;
1314 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1315 if (NULL == (p = peer->path_head))
1319 best_cost = UINT_MAX;
1322 if ((cost = path_get_cost (t, p)) < best_cost)
1329 tunnel_add_path (t, best_p);
1330 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1331 t->path_refresh_task =
1332 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
1337 * Notify a tunnel that a connection has broken that affects at least
1338 * some of its peers.
1340 * @param t Tunnel affected
1341 * @param peer Peer that (at least) has been affected by the disconnection
1342 * @param p1 Peer that got disconnected from p2
1343 * @param p2 Peer that got disconnected from p1
1346 tunnel_notify_connection_broken (struct MeshTunnel *t,
1347 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1354 * Destroy the tunnel and free any allocated resources linked to it
1356 * @param t the tunnel to destroy
1358 * @return GNUNET_OK on success
1361 tunnel_destroy (struct MeshTunnel *t)
1363 struct MeshClient *c;
1364 struct MeshQueue *q;
1365 struct MeshQueue *qn;
1366 GNUNET_HashCode hash;
1374 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1375 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1380 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1381 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1385 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1389 if (NULL != q->data)
1390 GNUNET_free (q->data);
1394 /* TODO cancel core transmit ready in case it was active */
1401 /******************************************************************************/
1402 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1403 /******************************************************************************/
1407 * Function called to notify a client about the socket
1408 * being ready to queue more data. "buf" will be
1409 * NULL and "size" zero if the socket was closed for
1410 * writing in the meantime.
1412 * @param cls closure
1413 * @param size number of bytes available in buf
1414 * @param buf where the callee should write the message
1415 * @return number of bytes written to buf
1418 send_core_create_path (void *cls, size_t size, void *buf)
1420 struct MeshPathInfo *info = cls;
1421 struct GNUNET_MESH_ManipulatePath *msg;
1422 struct GNUNET_PeerIdentity *peer_ptr;
1423 struct GNUNET_PeerIdentity id;
1424 struct MeshPeerInfo *peer = info->peer;
1425 struct MeshTunnel *t = info->t;
1426 struct MeshPeerPath *p = info->path;
1431 sizeof (struct GNUNET_MESH_ManipulatePath) +
1432 p->length * sizeof (struct GNUNET_PeerIdentity);
1434 if (size < size_needed || NULL == buf)
1436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
1437 GNUNET_PEER_resolve (path_get_first_hop (t, peer)->id, &id);
1438 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1439 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1440 size_needed, &send_core_create_path,
1445 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1446 msg->header.size = htons (size_needed);
1447 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1448 msg->tid = ntohl (t->id.tid);
1450 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1451 for (i = 0; i < p->length; i++)
1453 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1465 * Function called to notify a client about the socket
1466 * being ready to queue more data. "buf" will be
1467 * NULL and "size" zero if the socket was closed for
1468 * writing in the meantime.
1470 * @param cls closure (MeshDataDescriptor with all info to build packet)
1471 * @param size number of bytes available in buf
1472 * @param buf where the callee should write the message
1473 * @return number of bytes written to buf
1476 send_core_data_to_origin (void *cls, size_t size, void *buf)
1478 struct MeshDataDescriptor *info = cls;
1479 struct GNUNET_MESH_ToOrigin *msg = buf;
1482 GNUNET_assert (NULL != info);
1483 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1484 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1486 if (total_size > size)
1488 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1489 "not enough buffer to send data to origin\n");
1492 msg->header.size = htons (total_size);
1493 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1494 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1495 msg->tid = htonl (info->origin->tid);
1496 if (0 != info->size)
1498 memcpy (&msg[1], &info[1], info->size);
1500 if (NULL != info->client)
1502 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1510 * Function called to notify a client about the socket
1511 * being ready to queue more data. "buf" will be
1512 * NULL and "size" zero if the socket was closed for
1513 * writing in the meantime.
1515 * @param cls closure (data itself)
1516 * @param size number of bytes available in buf
1517 * @param buf where the callee should write the message
1518 * @return number of bytes written to buf
1521 send_core_data_unicast (void *cls, size_t size, void *buf)
1523 struct MeshDataDescriptor *info = cls;
1524 struct GNUNET_MESH_Unicast *msg = buf;
1527 GNUNET_assert (NULL != info);
1528 total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
1529 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1531 if (total_size > size)
1533 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1534 "not enough buffer to send data to peer\n");
1537 msg->header.size = htons (total_size);
1538 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1539 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1540 GNUNET_PEER_resolve (info->destination, &msg->destination);
1541 msg->tid = htonl (info->origin->tid);
1542 if (0 != info->size)
1544 memcpy (&msg[1], &info[1], info->size);
1546 if (NULL != info->client)
1548 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1556 * Function called to notify a client about the socket
1557 * being ready to queue more data. "buf" will be
1558 * NULL and "size" zero if the socket was closed for
1559 * writing in the meantime.
1561 * @param cls closure (data itself)
1562 * @param size number of bytes available in buf
1563 * @param buf where the callee should write the message
1564 * @return number of bytes written to buf
1567 send_core_data_multicast (void *cls, size_t size, void *buf)
1569 struct MeshDataDescriptor *info = cls;
1570 struct GNUNET_MESH_Multicast *msg = buf;
1573 GNUNET_assert (NULL != info);
1574 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1575 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1579 info->peer->core_transmit[info->handler_n] = NULL;
1581 if (total_size > size)
1583 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1584 "not enough buffer to send data futher\n");
1587 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1588 msg->header.size = htons (total_size);
1589 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1590 msg->tid = htonl (info->origin->tid);
1591 memcpy (&msg[1], &info[1], total_size);
1592 if (0 == --info->copies)
1594 if (NULL != info->client)
1596 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1605 * Function called to notify a client about the socket
1606 * being ready to queue more data. "buf" will be
1607 * NULL and "size" zero if the socket was closed for
1608 * writing in the meantime.
1610 * @param cls closure (MeshDataDescriptor)
1611 * @param size number of bytes available in buf
1612 * @param buf where the callee should write the message
1613 * @return number of bytes written to buf
1616 send_core_path_ack (void *cls, size_t size, void *buf)
1618 struct MeshDataDescriptor *info = cls;
1619 struct GNUNET_MESH_PathACK *msg = buf;
1621 GNUNET_assert (NULL != info);
1624 info->peer->core_transmit[info->handler_n] = NULL;
1626 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1631 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1632 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1633 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1634 msg->tid = htonl (info->origin->tid);
1635 msg->peer_id = my_full_id;
1637 /* TODO add signature */
1639 return sizeof (struct GNUNET_MESH_PathACK);
1644 * Function called to notify a client about the socket
1645 * being ready to queue more data. "buf" will be
1646 * NULL and "size" zero if the socket was closed for
1647 * writing in the meantime.
1649 * @param cls closure (data itself)
1650 * @param size number of bytes available in buf
1651 * @param buf where the callee should write the message
1652 * @return number of bytes written to buf
1655 send_core_data_raw (void *cls, size_t size, void *buf)
1657 struct GNUNET_MessageHeader *msg = cls;
1660 GNUNET_assert (NULL != msg);
1661 total_size = ntohs (msg->size);
1663 if (total_size > size)
1668 memcpy (buf, msg, total_size);
1676 * Send another peer a notification to destroy a tunnel
1677 * @param cls The tunnel to destroy
1678 * @param size Size in the buffer
1679 * @param buf Memory where to put the data to transmit
1680 * @return Size of data put in buffer
1683 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1685 struct MeshTunnel *t = cls;
1686 struct MeshClient *c;
1687 struct GNUNET_MESH_TunnelMessage *msg;
1691 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1692 /*FIXME*/ msg->header.size =
1693 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1694 msg->tunnel_id = htonl (t->id.tid);
1696 tunnel_destroy (c, t);
1697 return sizeof (struct GNUNET_MESH_TunnelMessage);
1703 * Send the message to all clients that have subscribed to its type
1705 * @param msg Pointer to the message itself
1706 * @return number of clients this message was sent to
1709 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1711 struct MeshClient *c;
1715 type = ntohs (msg->type);
1716 for (count = 0, c = clients; c != NULL; c = c->next)
1718 if (client_is_subscribed (type, c))
1721 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1730 * Iterator over hash map peer entries collect all neighbors who to resend the
1733 * @param cls closure (**GNUNET_PEER_Id to store hops to send packet)
1734 * @param key current key code (peer id hash)
1735 * @param value value in the hash map (peer_info)
1736 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
1739 iterate_collect_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
1741 struct MeshPeerInfo *peer_info = value;
1742 struct MeshPathInfo *neighbors = cls;
1745 if (peer_info->id == myid)
1749 peer_info = path_get_first_hop (neighbors->t, peer_info);
1750 for (i = 0; i < neighbors->path->length; i++)
1752 if (neighbors->path->peers[i] == peer_info->id)
1755 GNUNET_array_append (neighbors->path->peers, neighbors->path->length,
1762 /******************************************************************************/
1763 /******************** MESH NETWORK HANDLERS **************************/
1764 /******************************************************************************/
1768 * Core handler for path creation
1769 * struct GNUNET_CORE_MessageHandler
1771 * @param cls closure
1772 * @param message message
1773 * @param peer peer identity this notification is about
1774 * @param atsi performance data
1775 * @return GNUNET_OK to keep the connection open,
1776 * GNUNET_SYSERR to close it (signal serious error)
1780 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1781 const struct GNUNET_MessageHeader *message,
1782 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1784 unsigned int own_pos;
1787 MESH_TunnelNumber tid;
1788 struct GNUNET_MESH_ManipulatePath *msg;
1789 struct GNUNET_PeerIdentity *pi;
1790 struct GNUNET_PeerIdentity id;
1791 GNUNET_HashCode hash;
1792 struct MeshPeerPath *path;
1793 struct MeshPeerInfo *dest_peer_info;
1794 struct MeshPeerInfo *orig_peer_info;
1795 struct MeshTunnel *t;
1797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1798 "MESH: Received a MESH path create msg\n");
1799 size = ntohs (message->size);
1800 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1802 GNUNET_break_op (0);
1806 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1807 if (size % sizeof (struct GNUNET_PeerIdentity))
1809 GNUNET_break_op (0);
1812 size /= sizeof (struct GNUNET_PeerIdentity);
1815 GNUNET_break_op (0);
1818 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1820 tid = ntohl (msg->tid);
1821 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1822 t = tunnel_get (pi, tid);
1826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1827 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1828 t->id.oid = GNUNET_PEER_intern (pi);
1830 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1832 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1834 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1835 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1843 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1844 if (NULL == dest_peer_info)
1846 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1847 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1848 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1850 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1852 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1853 if (NULL == orig_peer_info)
1855 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1856 orig_peer_info->id = GNUNET_PEER_intern (pi);
1857 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1858 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1861 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
1862 path->length = size;
1863 path->peers = GNUNET_malloc (size * sizeof (GNUNET_PEER_Id));
1865 for (i = 0; i < size; i++)
1867 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1868 if (path->peers[i] == myid)
1872 { /* cannot be self, must be 'not found' */
1873 /* create path: self not found in path through self */
1874 GNUNET_break_op (0);
1875 path_destroy (path);
1876 /* FIXME error. destroy tunnel? leave for timeout? */
1879 if (own_pos == size - 1)
1881 /* It is for us! Send ack. */
1882 struct MeshDataDescriptor *info;
1885 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1886 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1887 info->origin = &t->id;
1888 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1889 GNUNET_assert (info->peer);
1890 for (j = 0; info->peer->core_transmit[j]; j++)
1898 info->handler_n = j;
1899 info->peer->core_transmit[j] =
1900 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1901 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1902 sizeof (struct GNUNET_MessageHeader),
1903 &send_core_path_ack, info);
1907 /* It's for somebody else! Retransmit. */
1908 struct MeshPathInfo *path_info;
1910 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1912 path_info->path = path;
1913 path_info->peer = dest_peer_info;
1915 path_add_to_peer (dest_peer_info, path);
1916 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1917 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1918 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1919 sizeof (struct GNUNET_MessageHeader),
1920 &send_core_create_path, path_info);
1927 * Core handler for mesh network traffic going from the origin to a peer
1929 * @param cls closure
1930 * @param peer peer identity this notification is about
1931 * @param message message
1932 * @param atsi performance data
1933 * @return GNUNET_OK to keep the connection open,
1934 * GNUNET_SYSERR to close it (signal serious error)
1937 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1938 const struct GNUNET_MessageHeader *message,
1939 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1941 struct GNUNET_MESH_Unicast *msg;
1942 struct GNUNET_PeerIdentity id;
1943 struct MeshTunnel *t;
1944 struct MeshPeerInfo *pi;
1947 size = ntohs (message->size);
1949 sizeof (struct GNUNET_MESH_Unicast) +
1950 sizeof (struct GNUNET_MessageHeader))
1955 msg = (struct GNUNET_MESH_Unicast *) message;
1956 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1959 /* TODO notify back: we don't know this tunnel */
1962 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1963 &msg->destination.hashPubKey);
1966 /* TODO maybe feedback, log to statistics */
1971 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1974 GNUNET_PEER_resolve (path_get_first_hop (t, pi)->id, &id);
1975 msg = GNUNET_malloc (size);
1976 memcpy (msg, message, size);
1977 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1978 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1979 &send_core_data_raw, msg);
1985 * Core handler for mesh network traffic going from the origin to all peers
1987 * @param cls closure
1988 * @param message message
1989 * @param peer peer identity this notification is about
1990 * @param atsi performance data
1991 * @return GNUNET_OK to keep the connection open,
1992 * GNUNET_SYSERR to close it (signal serious error)
1995 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1996 const struct GNUNET_MessageHeader *message,
1997 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1999 struct GNUNET_MESH_Multicast *msg;
2000 struct GNUNET_PeerIdentity id;
2001 struct MeshDataDescriptor *info;
2002 struct MeshPathInfo neighbors;
2003 struct MeshTunnel *t;
2009 size = ntohs (message->size);
2011 sizeof (struct GNUNET_MESH_Multicast) +
2012 sizeof (struct GNUNET_MessageHeader))
2014 GNUNET_break_op (0);
2017 msg = (struct GNUNET_MESH_Multicast *) message;
2018 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2022 /* TODO notify that we dont know that tunnel */
2026 /* Transmit to locally interested clients */
2027 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2029 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
2032 /* Retransmit to other peers.
2033 * Using path here as just a collection of peers, not a path per se.
2036 neighbors.path = GNUNET_malloc (sizeof (struct MeshPeerPath));
2037 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &iterate_collect_neighbors,
2039 if (0 == neighbors.path->length)
2041 GNUNET_free (neighbors.path);
2044 size -= sizeof (struct GNUNET_MESH_Multicast);
2045 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + size);
2046 info->origin = &t->id;
2047 info->copies = neighbors.path->length;
2048 for (i = 0; i < info->copies; i++)
2050 GNUNET_PEER_resolve (neighbors.path->peers[i], &id);
2051 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2052 GNUNET_assert (NULL != info->peer);
2053 for (j = 0; 0 != info->peer->core_transmit[j]; j++)
2055 if (j == (CORE_QUEUE_SIZE - 1))
2061 info->handler_n = j;
2062 info->peer->infos[j] = info;
2063 info->peer->core_transmit[j] =
2064 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2065 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2066 ntohs (msg->header.size),
2067 &send_core_data_multicast, info);
2069 GNUNET_free (neighbors.path->peers);
2070 GNUNET_free (neighbors.path);
2076 * Core handler for mesh network traffic
2078 * @param cls closure
2079 * @param message message
2080 * @param peer peer identity this notification is about
2081 * @param atsi performance data
2083 * @return GNUNET_OK to keep the connection open,
2084 * GNUNET_SYSERR to close it (signal serious error)
2087 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2088 const struct GNUNET_MessageHeader *message,
2089 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2091 struct GNUNET_MESH_ToOrigin *msg;
2092 struct GNUNET_PeerIdentity id;
2093 struct MeshPeerInfo *peer_info;
2094 struct MeshTunnel *t;
2097 size = ntohs (message->size);
2098 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2099 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2101 GNUNET_break_op (0);
2104 msg = (struct GNUNET_MESH_ToOrigin *) message;
2105 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2109 /* TODO notify that we dont know this tunnel (whom)? */
2113 if (t->id.oid == myid)
2115 if (NULL == t->client)
2117 /* got data packet for ownerless tunnel */
2118 GNUNET_break_op (0);
2121 /* TODO signature verification */
2122 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
2126 peer_info = peer_info_get (&msg->oid);
2127 if (NULL == peer_info)
2129 /* unknown origin of tunnel */
2133 GNUNET_PEER_resolve (t->paths->me->parent->peer->id, &id);
2134 msg = GNUNET_malloc (size);
2135 memcpy (msg, message, size);
2136 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2137 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
2138 &send_core_data_raw, msg);
2145 * Core handler for path ACKs
2147 * @param cls closure
2148 * @param message message
2149 * @param peer peer identity this notification is about
2150 * @param atsi performance data
2152 * @return GNUNET_OK to keep the connection open,
2153 * GNUNET_SYSERR to close it (signal serious error)
2155 * FIXME path change state
2158 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2159 const struct GNUNET_MessageHeader *message,
2160 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2162 struct GNUNET_MESH_PathACK *msg;
2163 struct GNUNET_PeerIdentity id;
2164 struct MeshTunnel *t;
2165 struct MeshPeerInfo *peer_info;
2167 msg = (struct GNUNET_MESH_PathACK *) message;
2168 t = tunnel_get (&msg->oid, msg->tid);
2171 /* TODO notify that we don't know the tunnel */
2175 /* Message for us? */
2176 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2178 struct GNUNET_MESH_PeerControl pc;
2180 if (NULL == t->client)
2185 peer_info = peer_info_get (&msg->peer_id);
2186 if (NULL == peer_info)
2188 GNUNET_break_op (0);
2191 /* FIXME change state of peer */
2192 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2193 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2194 pc.tunnel_id = htonl (t->local_tid);
2195 GNUNET_PEER_resolve (peer_info->id, &pc.peer);
2196 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
2197 &pc.header, GNUNET_NO);
2201 peer_info = peer_info_get (&msg->oid);
2202 if (NULL == peer_info)
2204 /* If we know the tunnel, we should DEFINITELY know the peer */
2208 GNUNET_PEER_resolve (path_get_first_hop (t, peer_info)->id, &id);
2209 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2210 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2211 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2212 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2213 sizeof (struct GNUNET_MESH_PathACK),
2214 &send_core_data_raw, msg);
2220 * Functions to handle messages from core
2222 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2223 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2224 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2225 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2226 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2227 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2228 sizeof (struct GNUNET_MESH_PathACK)},
2234 /******************************************************************************/
2235 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2236 /******************************************************************************/
2239 * delete_tunnel_entry: iterator for deleting each tunnel that belongs to a
2240 * client when the client disconnects.
2241 * @param cls closure (client that is disconnecting)
2242 * @param key the hash of the local tunnel id (used to access the hashmap)
2243 * @param value the value stored at the key (tunnel to destroy)
2244 * @return GNUNET_OK on success
2247 delete_tunnel_entry (void *cls, const GNUNET_HashCode * key, void *value)
2251 r = tunnel_destroy ((struct MeshTunnel *) value);
2257 * deregister_app: iterator for removing each application registered by a client
2258 * @param cls closure
2259 * @param key the hash of the application id (used to access the hashmap)
2260 * @param value the value stored at the key (client)
2261 * @return GNUNET_OK on success
2264 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2266 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2272 * notify_client_connection_failure: notify a client that the connection to the
2273 * requested remote peer is not possible (for instance, no route found)
2274 * Function called when the socket is ready to queue more data. "buf" will be
2275 * NULL and "size" zero if the socket was closed for writing in the meantime.
2277 * @param cls closure
2278 * @param size number of bytes available in buf
2279 * @param buf where the callee should write the message
2280 * @return number of bytes written to buf
2283 notify_client_connection_failure (void *cls, size_t size, void *buf)
2286 struct MeshPeerInfo *peer_info;
2287 struct GNUNET_MESH_PeerControl *msg;
2288 struct GNUNET_PeerIdentity id;
2290 if (0 == size && NULL == buf)
2292 // TODO retry? cancel?
2296 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2297 peer_info = (struct MeshPeerInfo *) cls;
2298 msg = (struct GNUNET_MESH_PeerControl *) buf;
2299 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2300 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2301 // msg->tunnel_id = htonl(peer_info->t->tid);
2302 GNUNET_PEER_resolve (peer_info->id, &id);
2303 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2311 * Function to process paths received for a new peer addition. The recorded
2312 * paths form the initial tunnel, which can be optimized later.
2313 * Called on each result obtained for the DHT search.
2315 * @param cls closure
2316 * @param exp when will this value expire
2317 * @param key key of the result
2318 * @param get_path NULL-terminated array of pointers
2319 * to the peers on reverse GET path (or NULL if not recorded)
2320 * @param put_path NULL-terminated array of pointers
2321 * to the peers on the PUT path (or NULL if not recorded)
2322 * @param type type of the result
2323 * @param size number of bytes in data
2324 * @param data pointer to the result data
2329 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2330 const GNUNET_HashCode * key,
2331 const struct GNUNET_PeerIdentity *const *get_path,
2332 const struct GNUNET_PeerIdentity *const *put_path,
2333 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2335 struct MeshPathInfo *path_info = cls;
2336 struct MeshPeerPath *p;
2337 struct GNUNET_PeerIdentity pi;
2340 if (NULL == get_path || NULL == put_path)
2342 if (NULL == path_info->peer->path_head)
2344 // Find ourselves some alternate initial path to the destination: retry
2345 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2346 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2347 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2348 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2349 GNUNET_BLOCK_TYPE_TEST, /* type */
2350 &pi.hashPubKey, /*key to search */
2351 4, /* replication level */
2352 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2355 0, /* xquery bits */
2357 (void *) path_info);
2362 p = path_build_from_dht (get_path, put_path);
2363 path_add_to_peer (path_info->peer, p);
2364 for (i = 0; i < path_info->peer->ntunnels; i++)
2366 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2368 GNUNET_free (path_info);
2375 * Function to process paths received for a new peer addition. The recorded
2376 * paths form the initial tunnel, which can be optimized later.
2377 * Called on each result obtained for the DHT search.
2379 * @param cls closure
2380 * @param exp when will this value expire
2381 * @param key key of the result
2382 * @param get_path NULL-terminated array of pointers
2383 * to the peers on reverse GET path (or NULL if not recorded)
2384 * @param put_path NULL-terminated array of pointers
2385 * to the peers on the PUT path (or NULL if not recorded)
2386 * @param type type of the result
2387 * @param size number of bytes in data
2388 * @param data pointer to the result data
2391 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2392 const GNUNET_HashCode * key,
2393 const struct GNUNET_PeerIdentity *const *get_path,
2394 const struct GNUNET_PeerIdentity *const *put_path,
2395 enum GNUNET_BLOCK_Type type, size_t size,
2398 const struct GNUNET_PeerIdentity *pi = data;
2399 struct GNUNET_PeerIdentity id;
2400 struct MeshTunnel *t = cls;
2401 struct MeshPeerInfo *peer_info;
2402 struct MeshPeerPath *p;
2405 if (size != sizeof (struct GNUNET_PeerIdentity))
2407 GNUNET_break_op (0);
2410 GNUNET_assert (NULL != t->client);
2411 GNUNET_DHT_get_stop (t->client->dht_get_type);
2412 t->client->dht_get_type = NULL;
2413 peer_info = peer_info_get (pi);
2414 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2415 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2417 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2418 NULL == peer_info->dhtget)
2420 /* we don't have a route to the peer, let's try a direct lookup */
2421 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2423 GNUNET_TIME_UNIT_FOREVER_REL,
2425 GNUNET_BLOCK_TYPE_TEST,
2428 /* key to look up */
2430 /* replication level */
2431 GNUNET_DHT_RO_RECORD_ROUTE,
2432 /* option to dht: record route */
2433 NULL, /* bloom filter */
2436 0, /* xquery bits */
2439 peer_info); /* closure */
2442 p = path_build_from_dht (get_path, put_path);
2443 path_add_to_peer (peer_info, p);
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2447 t->local_tid, p->length);
2448 for (i = 0; i < p->length; i++)
2450 GNUNET_PEER_resolve (p->peers[0], &id);
2451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2452 GNUNET_h2s_full (&id.hashPubKey));
2456 GNUNET_PEER_resolve (p->peers[1], &id);
2457 GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
2460 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2462 sizeof (struct GNUNET_MESH_ManipulatePath) + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
2463 &send_core_create_path, /* callback */
2464 peer_info); /* cls */
2467 /******************************************************************************/
2468 /********************* MESH LOCAL HANDLES **************************/
2469 /******************************************************************************/
2473 * Handler for client disconnection
2475 * @param cls closure
2476 * @param client identification of the client; NULL
2477 * for the last call when the server is destroyed
2480 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2482 struct MeshClient *c;
2483 struct MeshClient *next;
2485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2489 if (c->handle != client)
2491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2496 if (NULL != c->tunnels)
2498 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels, &delete_tunnel_entry,
2500 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2503 /* deregister clients applications */
2504 if (NULL != c->apps)
2506 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2507 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2509 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2510 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2512 GNUNET_SCHEDULER_cancel (announce_applications_task);
2513 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2515 if (NULL != c->types)
2516 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2517 if (NULL != c->dht_get_type)
2518 GNUNET_DHT_get_stop (c->dht_get_type);
2519 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2531 * Handler for new clients
2533 * @param cls closure
2534 * @param client identification of the client
2535 * @param message the actual message, which includes messages the client wants
2538 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2539 const struct GNUNET_MessageHeader *message)
2541 struct GNUNET_MESH_ClientConnect *cc_msg;
2542 struct MeshClient *c;
2543 GNUNET_MESH_ApplicationType *a;
2550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2551 /* Check data sanity */
2552 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2553 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2554 ntypes = ntohs (cc_msg->types);
2555 napps = ntohs (cc_msg->applications);
2557 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2560 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2564 /* Create new client structure */
2565 c = GNUNET_malloc (sizeof (struct MeshClient));
2567 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2570 GNUNET_MESH_ApplicationType at;
2573 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2574 for (i = 0; i < napps; i++)
2577 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2578 /* store in clients hashmap */
2579 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2580 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2581 /* store in global hashmap, for announcements */
2582 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2583 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2585 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2586 announce_applications_task =
2587 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2595 t = (uint16_t *) & a[napps];
2596 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2597 for (i = 0; i < ntypes; i++)
2600 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2602 /* store in clients hashmap */
2603 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2604 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2605 /* store in global hashmap */
2606 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2607 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2611 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2613 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2614 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2615 GNUNET_SERVER_notification_context_add (nc, client);
2617 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2623 * Handler for requests of new tunnels
2625 * @param cls closure
2626 * @param client identification of the client
2627 * @param message the actual message
2630 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2631 const struct GNUNET_MessageHeader *message)
2633 struct GNUNET_MESH_TunnelMessage *t_msg;
2634 struct MeshTunnel *t;
2635 struct MeshClient *c;
2636 GNUNET_HashCode hash;
2638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2640 /* Sanity check for client registration */
2641 if (NULL == (c = client_get (client)))
2644 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2648 /* Message sanity check */
2649 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2652 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2656 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2657 /* Sanity check for tunnel numbering */
2658 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2661 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2664 /* Sanity check for duplicate tunnel IDs */
2665 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2672 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2673 while (NULL != tunnel_get_by_pi (myid, next_tid))
2674 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2675 t->id.tid = next_tid++;
2677 t->local_tid = ntohl (t_msg->tunnel_id);
2679 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2681 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2683 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2687 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2691 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2693 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2694 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2697 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2701 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2707 * Handler for requests of deleting tunnels
2709 * @param cls closure
2710 * @param client identification of the client
2711 * @param message the actual message
2714 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2715 const struct GNUNET_MessageHeader *message)
2717 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2718 struct MeshClient *c;
2719 struct MeshTunnel *t;
2720 MESH_TunnelNumber tid;
2721 GNUNET_HashCode hash;
2723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2725 /* Sanity check for client registration */
2726 if (NULL == (c = client_get (client)))
2729 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2732 /* Message sanity check */
2733 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2736 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2740 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2742 /* Retrieve tunnel */
2743 tid = ntohl (tunnel_msg->tunnel_id);
2745 /* Remove from local id hashmap */
2746 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2747 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2748 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2750 /* Remove from global id hashmap */
2751 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2752 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2754 // notify_tunnel_destroy(t);
2755 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2761 * Handler for connection requests to new peers
2763 * @param cls closure
2764 * @param client identification of the client
2765 * @param message the actual message (PeerControl)
2770 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2771 const struct GNUNET_MessageHeader *message)
2773 struct GNUNET_MESH_PeerControl *peer_msg;
2774 struct MeshClient *c;
2775 struct MeshTunnel *t;
2776 MESH_TunnelNumber tid;
2777 struct MeshPeerInfo *peer_info;
2780 /* Sanity check for client registration */
2781 if (NULL == (c = client_get (client)))
2784 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2788 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2789 /* Sanity check for message size */
2790 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2793 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2797 /* Tunnel exists? */
2798 tid = ntohl (peer_msg->tunnel_id);
2799 t = tunnel_get_by_local_id (c, tid);
2803 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2807 /* Does client own tunnel? */
2808 if (t->client->handle != client)
2811 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2816 peer_info = peer_info_get (&peer_msg->peer);
2818 /* Start DHT search if needed FIXME: if not already connected */
2819 if (NULL == peer_info->dhtget)
2821 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 */
2822 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2825 0, /* xquery bits */
2827 (void *) peer_info);
2830 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2836 * Handler for disconnection requests of peers in a tunnel
2838 * @param cls closure
2839 * @param client identification of the client
2840 * @param message the actual message (PeerControl)
2843 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2844 const struct GNUNET_MessageHeader *message)
2846 struct GNUNET_MESH_PeerControl *peer_msg;
2847 struct MeshClient *c;
2848 struct MeshTunnel *t;
2849 MESH_TunnelNumber tid;
2851 /* Sanity check for client registration */
2852 if (NULL == (c = client_get (client)))
2855 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2858 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2859 /* Sanity check for message size */
2860 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2863 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2867 /* Tunnel exists? */
2868 tid = ntohl (peer_msg->tunnel_id);
2869 t = tunnel_get_by_local_id (c, tid);
2873 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2877 /* Does client own tunnel? */
2878 if (t->client->handle != client)
2881 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2885 /* Ok, delete peer from tunnel */
2886 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2887 &peer_msg->peer.hashPubKey);
2889 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2895 * Handler for connection requests to new peers by type
2897 * @param cls closure
2898 * @param client identification of the client
2899 * @param message the actual message (ConnectPeerByType)
2902 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2903 const struct GNUNET_MessageHeader *message)
2905 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2906 struct MeshClient *c;
2907 struct MeshTunnel *t;
2908 GNUNET_HashCode hash;
2909 GNUNET_MESH_ApplicationType type;
2910 MESH_TunnelNumber tid;
2912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2913 /* Sanity check for client registration */
2914 if (NULL == (c = client_get (client)))
2917 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2921 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2922 /* Sanity check for message size */
2923 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2924 ntohs (connect_msg->header.size))
2927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2931 /* Tunnel exists? */
2932 tid = ntohl (connect_msg->tunnel_id);
2933 t = tunnel_get_by_local_id (c, tid);
2937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2941 /* Does client own tunnel? */
2942 if (t->client->handle != client)
2945 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2949 /* Do WE have the service? */
2950 type = ntohl (connect_msg->type);
2951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2952 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2953 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2956 /* Yes! Fast forward, add ourselves to the tunnel and send the
2957 * good news to the client
2959 struct GNUNET_MESH_PeerControl pc;
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2962 pc.peer = my_full_id;
2963 GNUNET_CONTAINER_multihashmap_put (t->peers, &pc.peer.hashPubKey,
2964 peer_info_get (&pc.peer),
2965 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2966 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2967 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2968 pc.tunnel_id = htonl (t->local_tid);
2969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2970 GNUNET_SERVER_notification_context_unicast (nc, /* context */
2972 &pc.header, /* msg */
2973 GNUNET_NO); /* can drop? */
2974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2975 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2978 /* Ok, lets find a peer offering the service */
2979 if (c->dht_get_type)
2981 GNUNET_DHT_get_stop (c->dht_get_type);
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2984 GNUNET_h2s_full (&hash));
2986 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2987 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2988 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
2989 &dht_get_type_handler, t);
2991 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2997 * Handler for client traffic directed to one peer
2999 * @param cls closure
3000 * @param client identification of the client
3001 * @param message the actual message
3004 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3005 const struct GNUNET_MessageHeader *message)
3007 struct MeshClient *c;
3008 struct MeshTunnel *t;
3009 struct MeshPeerInfo *pi;
3010 struct GNUNET_MESH_Unicast *data_msg;
3011 struct GNUNET_PeerIdentity next_hop;
3012 struct MeshDataDescriptor *info;
3013 MESH_TunnelNumber tid;
3016 /* Sanity check for client registration */
3017 if (NULL == (c = client_get (client)))
3020 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3023 data_msg = (struct GNUNET_MESH_Unicast *) message;
3024 /* Sanity check for message size */
3025 if (sizeof (struct GNUNET_MESH_Unicast) +
3026 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
3029 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3033 /* Tunnel exists? */
3034 tid = ntohl (data_msg->tid);
3035 t = tunnel_get_by_local_id (c, tid);
3039 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3043 /* Is it a local tunnel? Then, does client own the tunnel? */
3044 if (t->client->handle != NULL && t->client->handle != client)
3047 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3051 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3052 &data_msg->destination.hashPubKey);
3053 /* Is the selected peer in the tunnel? */
3057 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3062 struct GNUNET_MESH_Unicast copy;
3064 /* Work around const limitation */
3065 memcpy (©, data_msg, sizeof (struct GNUNET_MESH_Unicast));
3066 copy.oid = my_full_id;
3067 copy.tid = htonl (t->id.tid);
3068 handle_mesh_data_unicast (NULL, &my_full_id, ©.header, NULL);
3071 GNUNET_PEER_resolve (path_get_first_hop (t, pi)->id, &next_hop);
3072 data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
3073 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
3074 memcpy (&info[1], &data_msg[1], data_size);
3075 info->destination = pi->id;
3076 info->origin = &t->id;
3077 info->size = data_size;
3078 info->client = client;
3079 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
3080 GNUNET_TIME_UNIT_FOREVER_REL, &next_hop,
3081 /* FIXME re-check types */
3083 sizeof (struct GNUNET_MESH_Unicast),
3084 &send_core_data_unicast, info);
3089 * Handler for client traffic directed to all peers in a tunnel
3091 * @param cls closure
3092 * @param client identification of the client
3093 * @param message the actual message
3096 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3097 const struct GNUNET_MessageHeader *message)
3099 struct MeshClient *c;
3100 struct MeshTunnel *t;
3101 struct GNUNET_MESH_Multicast *data_msg;
3102 MESH_TunnelNumber tid;
3104 /* Sanity check for client registration */
3105 if (NULL == (c = client_get (client)))
3108 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3111 data_msg = (struct GNUNET_MESH_Multicast *) message;
3112 /* Sanity check for message size */
3113 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
3116 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3120 /* Tunnel exists? */
3121 tid = ntohl (data_msg->tid);
3122 t = tunnel_get_by_local_id (c, tid);
3126 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3130 /* Does client own tunnel? */
3131 if (t->client->handle != client)
3134 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3140 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3145 * Functions to handle messages from clients
3147 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3148 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3149 {&handle_local_tunnel_create, NULL,
3150 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3151 sizeof (struct GNUNET_MESH_TunnelMessage)},
3152 {&handle_local_tunnel_destroy, NULL,
3153 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3154 sizeof (struct GNUNET_MESH_TunnelMessage)},
3155 {&handle_local_connect_add, NULL,
3156 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3157 sizeof (struct GNUNET_MESH_PeerControl)},
3158 {&handle_local_connect_del, NULL,
3159 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3160 sizeof (struct GNUNET_MESH_PeerControl)},
3161 {&handle_local_connect_by_type, NULL,
3162 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3163 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3164 {&handle_local_unicast, NULL,
3165 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3166 {&handle_local_unicast, NULL,
3167 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3168 {&handle_local_multicast, NULL,
3169 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3175 * To be called on core init/fail.
3177 * @param cls service closure
3178 * @param server handle to the server for this service
3179 * @param identity the public identity of this peer
3180 * @param publicKey the public key of this peer
3183 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3184 const struct GNUNET_PeerIdentity *identity,
3185 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
3187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3188 core_handle = server;
3189 my_full_id = *identity;
3190 myid = GNUNET_PEER_intern (identity);
3191 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3196 * Method called whenever a given peer connects.
3198 * @param cls closure
3199 * @param peer peer identity this notification is about
3200 * @param atsi performance data for the connection
3203 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3204 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3206 // GNUNET_PEER_Id pid;
3207 struct MeshPeerInfo *peer_info;
3208 struct MeshPeerPath *path;
3210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3211 peer_info = peer_info_get (peer);
3212 if (myid == peer_info->id)
3214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3216 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
3218 path->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id) * 2);
3219 path->peers[0] = myid;
3220 path->peers[1] = peer_info->id;
3221 path_add_to_peer (peer_info, path);
3226 * Method called whenever a peer disconnects.
3228 * @param cls closure
3229 * @param peer peer identity this notification is about
3232 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3234 struct MeshPeerInfo *pi;
3237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3238 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3244 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3246 if (pi->core_transmit[i])
3248 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3249 /* TODO: notify that tranmission has failed */
3250 GNUNET_free (pi->infos[i]);
3253 path_remove_from_peer (pi, pi->id, myid);
3256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3262 /******************************************************************************/
3263 /************************ MAIN FUNCTIONS ****************************/
3264 /******************************************************************************/
3267 * Task run during shutdown.
3273 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3275 struct MeshClient *c;
3277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3278 if (core_handle != NULL)
3280 GNUNET_CORE_disconnect (core_handle);
3283 if (dht_handle != NULL)
3285 for (c = clients; NULL != c; c = c->next)
3286 if (NULL != c->dht_get_type)
3287 GNUNET_DHT_get_stop (c->dht_get_type);
3288 GNUNET_DHT_disconnect (dht_handle);
3293 GNUNET_SERVER_notification_context_destroy (nc);
3296 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3298 GNUNET_SCHEDULER_cancel (announce_id_task);
3299 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3305 * Process mesh requests.
3307 * @param cls closure
3308 * @param server the initialized server
3309 * @param c configuration to use
3312 run (void *cls, struct GNUNET_SERVER_Handle *server,
3313 const struct GNUNET_CONFIGURATION_Handle *c)
3315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3316 GNUNET_SERVER_add_handlers (server, client_handlers);
3317 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
3318 server_handle = server;
3319 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3320 CORE_QUEUE_SIZE, /* queue size */
3321 NULL, /* Closure passed to MESH functions */
3322 &core_init, /* Call core_init once connected */
3323 &core_connect, /* Handle connects */
3324 &core_disconnect, /* remove peers on disconnects */
3325 NULL, /* Do we care about "status" updates? */
3326 NULL, /* Don't notify about all incoming messages */
3327 GNUNET_NO, /* For header only in notification */
3328 NULL, /* Don't notify about all outbound messages */
3329 GNUNET_NO, /* For header-only out notification */
3330 core_handlers); /* Register these handlers */
3331 if (core_handle == NULL)
3335 dht_handle = GNUNET_DHT_connect (c, 64);
3336 if (dht_handle == NULL)
3338 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3339 Running without DHT has a severe\
3340 impact in MESH capabilities.\n\
3341 Plase check your configuretion and enable DHT.\n");
3347 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3348 peers = GNUNET_CONTAINER_multihashmap_create (32);
3349 applications = GNUNET_CONTAINER_multihashmap_create (32);
3350 types = GNUNET_CONTAINER_multihashmap_create (32);
3351 nc = GNUNET_SERVER_notification_context_create (server_handle,
3354 clients_tail = NULL;
3356 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3358 /* Scheduled the task to clean up when shutdown is called */
3359 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3366 * The main function for the mesh service.
3368 * @param argc number of arguments from the command line
3369 * @param argv command line arguments
3370 * @return 0 ok, 1 on error
3373 main (int argc, char *const *argv)
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3380 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");