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 (),
609 * Periodically announce what applications are provided by local clients
612 * @param tc task context
615 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
617 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
619 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
623 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
625 announce_applications_task =
626 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
634 * Periodically announce self id in the DHT
637 * @param tc task context
640 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
642 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
644 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
648 * - Set data expiration in function of X
651 GNUNET_DHT_put (dht_handle, /* DHT handle */
652 &my_full_id.hashPubKey, /* Key to use */
653 10U, /* Replication level */
654 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
655 GNUNET_BLOCK_TYPE_TEST, /* Block type */
656 0, /* Size of the data */
657 NULL, /* Data itself */
658 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
659 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
661 &mesh_debug, "DHT_put for id completed");
663 NULL, /* Continuation */
664 NULL); /* Continuation closure */
667 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
672 * Send keepalive packets for a peer
680 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
682 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,
706 /******************************************************************************/
707 /****************** GENERAL HELPER FUNCTIONS ************************/
708 /******************************************************************************/
711 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
712 * and insert it in the appropiate structures if the peer is not known yet.
714 * @param peer Identity of the peer
716 * @return Existing or newly created peer info
718 static struct MeshPeerInfo *
719 peer_info_get (const struct GNUNET_PeerIdentity *peer)
721 struct MeshPeerInfo *peer_info;
723 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
724 if (NULL == peer_info)
727 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
728 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
729 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
730 peer_info->id = GNUNET_PEER_intern (peer);
739 * Destroy the peer_info and free any allocated resources linked to it
740 * @param t tunnel the path belongs to
741 * @param pi the peer_info to destroy
742 * @return GNUNET_OK on success
745 peer_info_destroy (struct MeshPeerInfo *pi)
747 GNUNET_HashCode hash;
748 struct GNUNET_PeerIdentity id;
750 GNUNET_PEER_resolve (pi->id, &id);
751 GNUNET_PEER_change_rc (pi->id, -1);
752 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
754 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
755 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
763 * Destroy the path and free any allocated resources linked to it
765 * @param p the path to destroy
767 * @return GNUNET_OK on success
770 path_destroy (struct MeshPeerPath *p)
772 GNUNET_PEER_decrement_rcs (p->peers, p->length);
773 GNUNET_free (p->peers);
780 * Find the first peer whom to send a packet to go down this path
782 * @param t The tunnel to use
783 * @param peer The peerinfo of the peer we are trying to reach
785 * @return peerinfo of the peer who is the first hop in the tunnel
788 static struct MeshPeerInfo *
789 path_get_first_hop (struct MeshTunnel *t, struct MeshPeerInfo *peer)
791 struct GNUNET_PeerIdentity id;
793 GNUNET_PEER_resolve(peer->id, &id);
794 return GNUNET_CONTAINER_multihashmap_get(t->paths->first_hops,
800 * Get the length of a path
802 * @param path The path to measure, with the local peer at any point of it
804 * @return Number of hops to reach destination
805 * UINT_MAX in case the peer is not in the path
808 path_get_length (struct MeshPeerPath *path)
814 for (i = 0; i < path->length; i++)
816 if (path->peers[i] == myid)
818 return path->length - i;
826 * Get the cost of the path relative to the already built tunnel tree
828 * @param t The tunnel to which compare
829 * @param path The individual path to reach a peer
831 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
834 * TODO: remove dummy implementation, look into the tunnel tree
837 path_get_cost (struct MeshTunnel *t, struct MeshPeerPath *path)
839 return path_get_length(path);
844 * Add the path to the peer and update the path used to reach it in case this
847 * @param peer_info Destination peer to add the path to.
848 * @param path New path to add. Last peer must be the peer in arg 1.
850 * TODO: trim the part from origin to us? Add it as path to origin?
853 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
856 struct MeshPeerPath *aux;
858 if (NULL == peer_info || NULL == path)
864 l = path_get_length (path);
866 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
868 if (path_get_length (aux) > l)
870 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
871 peer_info->path_tail,
876 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head,
877 peer_info->path_tail,
884 * Notify a tunnel that a connection has broken that affects at least
887 * @param t Tunnel affected
888 * @param peer Peer that (at least) has been affected by the disconnection
889 * @param p1 Peer that got disconnected from p2
890 * @param p2 Peer that got disconnected from p1
893 tunnel_notify_connection_broken(struct MeshTunnel *t,
894 struct MeshPeerInfo *peer,
900 * Remove all paths that rely on a direct connection between p1 and p2
901 * from the peer itself and notify all tunnels about it.
903 * @param pi PeerInfo of affected peer
904 * @param p1 GNUNET_PEER_Id of one peer.
905 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
909 path_remove_from_peer (struct MeshPeerInfo *peer,
913 struct MeshPeerPath *p;
914 struct MeshPeerPath *aux;
915 unsigned int destroyed;
923 for (i = 0; i < (p->length - 1); i++)
925 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
926 (p->peers[i] == p2 && p->peers[i + 1] == p1))
938 for (i = 0; i < peer->ntunnels; i++)
940 tunnel_notify_connection_broken(peer->tunnels[i], peer, p1, p2);
946 * Add the path to the origin peer and update the path used to reach it in case
947 * this is the shortest.
948 * The path is given in peer_info -> destination, therefore we turn the path
951 * @param peer_info Peer to add the path to, being the origin of the path.
952 * @param path New path to add after being inversed.
955 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
960 for (i = 0; i < path->length / 2; i++)
962 aux = path->peers[i];
963 path->peers[i] = path->peers[path->length - i - 1];
964 path->peers[path->length - i - 1] = aux;
966 path_add_to_peer (peer_info, path);
971 * Build a PeerPath from the paths returned from the DHT, reversing the paths
972 * to obtain a local peer -> destination path and interning the peer ids.
974 * @param get_path NULL-terminated array of pointers
975 * to the peers on reverse GET path (or NULL if not recorded)
976 * @param put_path NULL-terminated array of pointers
977 * to the peers on the PUT path (or NULL if not recorded)
979 * @return Newly allocated and created path
981 static struct MeshPeerPath *
982 path_build_from_dht(const struct GNUNET_PeerIdentity *const *get_path,
983 const struct GNUNET_PeerIdentity *const *put_path)
985 struct MeshPeerPath *p;
988 p = GNUNET_malloc (sizeof (struct MeshPeerPath));
989 for (i = 0; get_path[i] != NULL; i++) ;
990 for (i--; i >= 0; i--)
993 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
994 p->peers[p->length] = GNUNET_PEER_intern (get_path[i]);
997 for (i = 0; put_path[i] != NULL; i++) ;
998 for (i--; i >= 0; i--)
1001 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1002 p->peers[p->length] = GNUNET_PEER_intern (put_path[i]);
1010 * Check if client has registered with the service and has not disconnected
1012 * @param client the client to check
1014 * @return non-NULL if client exists in the global DLL
1016 static struct MeshClient *
1017 client_get (struct GNUNET_SERVER_Client *client)
1019 struct MeshClient *c;
1024 if (c->handle == client)
1033 * Checks if a given client has subscribed to certain message type
1035 * @param message_type Type of message to check
1036 * @param c Client to check
1038 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1043 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1047 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1048 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1053 * Search for a tunnel among the tunnels for a client
1055 * @param c the client whose tunnels to search in
1056 * @param tid the local id of the tunnel
1058 * @return tunnel handler, NULL if doesn't exist
1060 static struct MeshTunnel *
1061 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1063 GNUNET_HashCode hash;
1065 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1066 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1071 * Search for a tunnel by global ID using PEER_ID
1073 * @param pi owner of the tunnel
1074 * @param tid global tunnel number
1076 * @return tunnel handler, NULL if doesn't exist
1078 static struct MeshTunnel *
1079 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1081 struct MESH_TunnelID id;
1082 GNUNET_HashCode hash;
1087 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1088 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1094 * Search for a tunnel by global ID using full PeerIdentities
1096 * @param oid owner of the tunnel
1097 * @param tid global tunnel number
1099 * @return tunnel handler, NULL if doesn't exist
1101 static struct MeshTunnel *
1102 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1104 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1109 * Recursively find the given peer in the tree.
1111 * @param t Tunnel where to add the new path.
1112 * @param p Path to look for.
1114 * @return Pointer to the node of the peer. NULL if not found.
1116 static struct MeshTunnelPathNode *
1117 tunnel_find_peer(struct MeshTunnelPathNode *root, struct MeshPeerInfo *peer)
1119 struct MeshTunnelPathNode *n;
1122 if (root->peer == peer)
1124 for (i = 0; i < root->nchildren; i++)
1126 n = tunnel_find_peer(&root->children[i], peer);
1135 * Delete the current path to the peer, including all now unused relays.
1137 * @param t Tunnel where to add the new path.
1138 * @param p Path to be integrated.
1140 * @return pointer to the pathless node, NULL on error
1142 * TODO: notify peers of deletion
1144 static struct MeshTunnelPathNode *
1145 tunnel_del_path(struct MeshTunnel *t, struct MeshPeerInfo *peer)
1147 struct MeshTunnelPathNode *parent;
1148 struct MeshTunnelPathNode *node;
1149 struct MeshTunnelPathNode *n;
1151 node = n = tunnel_find_peer(t->paths->me, peer);
1156 while (NULL != parent &&
1157 MESH_PEER_RELAY == parent->status &&
1158 1 == parent->nchildren)
1161 GNUNET_free(parent->children);
1162 parent = parent->parent;
1166 *n = parent->children[parent->nchildren - 1];
1167 parent->nchildren--;
1168 parent->children = GNUNET_realloc (parent->children, parent->nchildren);
1174 * Integrate a stand alone path into the tunnel tree.
1176 * @param t Tunnel where to add the new path.
1177 * @param p Path to be integrated.
1179 * @return GNUNET_OK in case of success.
1180 * GNUNET_SYSERR in case of error.
1183 * - go backwards on path looking for each peer in the present tree
1186 tunnel_add_path(struct MeshTunnel *t, struct MeshPeerPath *p)
1188 struct MeshTunnelPathNode *parent;
1189 struct MeshTunnelPathNode *oldnode;
1190 struct MeshTunnelPathNode *n;
1191 struct GNUNET_PeerIdentity id;
1192 struct GNUNET_PeerIdentity hop;
1198 if (n->peer->id != p->peers[0])
1201 return GNUNET_SYSERR;
1203 /* Ignore return value, if not found it's ok. */
1204 GNUNET_PEER_resolve(p->peers[p->length - 1], &id);
1205 oldnode = tunnel_del_path(t, peer_info_get(&id));
1206 /* Look for the first node that is not already present in the tree
1208 * Assuming that the tree is somewhat balanced, O(log n * log N).
1209 * - Length of the path is expected to be log N (size of whole network).
1210 * - Each level of the tree is expected to have log n children (size of tree).
1212 for (i = 1, me = -1; i < p->length; i++)
1215 if (p->peers[i] == myid)
1217 for (j = 0; j < n->nchildren; j++)
1219 if (n->children[j].peer->id == p->peers[i])
1221 n = &n->children[j];
1225 /* If we couldn't find a child equal to path[i], we have reached the end
1226 * of the common path. */
1232 /* New path deviates from tree before reaching us. What happened? */
1234 return GNUNET_SYSERR;
1236 /* Add the rest of the path as a branch from parent. */
1237 while (i < p->length)
1239 parent->nchildren++;
1240 parent->children = GNUNET_realloc(parent->children, parent->nchildren);
1241 n = &parent->children[parent->nchildren - 1];
1242 if (i == p->length - 1)
1244 if (NULL != oldnode)
1246 /* Assignation and free can be misleading, using explicit mempcy */
1247 memcpy (n, oldnode, sizeof(struct MeshTunnelPathNode));
1248 GNUNET_free (oldnode);
1250 n->status = MESH_PEER_WAITING;
1255 n->status = MESH_PEER_RELAY;
1256 GNUNET_PEER_resolve(p->peers[i], &id);
1257 n->peer = peer_info_get(&id);
1264 /* Add info about first hop into hashmap. */
1265 if (me < p->length - 1)
1267 GNUNET_PEER_resolve(p->peers[p->length - 1], &id);
1268 GNUNET_PEER_resolve(p->peers[me + 1], &hop);
1269 GNUNET_CONTAINER_multihashmap_put(
1270 t->paths->first_hops,
1272 peer_info_get(&hop),
1273 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1280 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1281 * needed rescources.
1283 * @param t Tunnel we want to add a new peer to
1284 * @param peer PeerInfo of the peer being added
1288 tunnel_add_peer(struct MeshTunnel *t, struct MeshPeerInfo *peer)
1290 struct MeshPeerPath *p;
1291 struct MeshPeerPath *best_p;
1292 unsigned int best_cost;
1295 GNUNET_array_append(peer->tunnels, peer->ntunnels, t);
1296 if (NULL == (p = peer->path_head))
1300 best_cost = UINT_MAX;
1303 if ((cost = path_get_cost(t, p)) < best_cost)
1310 tunnel_add_path(t, best_p);
1311 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1312 t->path_refresh_task = GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME,
1318 * Notify a tunnel that a connection has broken that affects at least
1319 * some of its peers.
1321 * @param t Tunnel affected
1322 * @param peer Peer that (at least) has been affected by the disconnection
1323 * @param p1 Peer that got disconnected from p2
1324 * @param p2 Peer that got disconnected from p1
1327 tunnel_notify_connection_broken(struct MeshTunnel *t,
1328 struct MeshPeerInfo *peer,
1336 * Destroy the tunnel and free any allocated resources linked to it
1338 * @param t the tunnel to destroy
1340 * @return GNUNET_OK on success
1343 tunnel_destroy (struct MeshTunnel *t)
1345 struct MeshClient *c;
1346 struct MeshQueue *q;
1347 struct MeshQueue *qn;
1348 GNUNET_HashCode hash;
1356 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1357 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1362 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1363 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1367 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1371 if (NULL != q->data)
1372 GNUNET_free (q->data);
1376 /* TODO cancel core transmit ready in case it was active */
1383 /******************************************************************************/
1384 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1385 /******************************************************************************/
1389 * Function called to notify a client about the socket
1390 * being ready to queue more data. "buf" will be
1391 * NULL and "size" zero if the socket was closed for
1392 * writing in the meantime.
1394 * @param cls closure
1395 * @param size number of bytes available in buf
1396 * @param buf where the callee should write the message
1397 * @return number of bytes written to buf
1400 send_core_create_path (void *cls, size_t size, void *buf)
1402 struct MeshPathInfo *info = cls;
1403 struct GNUNET_MESH_ManipulatePath *msg;
1404 struct GNUNET_PeerIdentity *peer_ptr;
1405 struct GNUNET_PeerIdentity id;
1406 struct MeshPeerInfo *peer = info->peer;
1407 struct MeshTunnel *t = info->t;
1408 struct MeshPeerPath *p = info->path;
1413 sizeof (struct GNUNET_MESH_ManipulatePath) +
1414 p->length * sizeof (struct GNUNET_PeerIdentity);
1416 if (size < size_needed || NULL == buf)
1418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
1419 GNUNET_PEER_resolve (path_get_first_hop (t, peer)->id, &id);
1420 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1421 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1422 size_needed, &send_core_create_path,
1427 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1428 msg->header.size = htons (size_needed);
1429 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1430 msg->tid = ntohl(t->id.tid);
1432 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1433 for (i = 0; i < p->length; i++)
1435 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1447 * Function called to notify a client about the socket
1448 * being ready to queue more data. "buf" will be
1449 * NULL and "size" zero if the socket was closed for
1450 * writing in the meantime.
1452 * @param cls closure (MeshDataDescriptor with all info to build packet)
1453 * @param size number of bytes available in buf
1454 * @param buf where the callee should write the message
1455 * @return number of bytes written to buf
1458 send_core_data_to_origin (void *cls, size_t size, void *buf)
1460 struct MeshDataDescriptor *info = cls;
1461 struct GNUNET_MESH_ToOrigin *msg = buf;
1464 GNUNET_assert (NULL != info);
1465 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1466 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1468 if (total_size > size)
1470 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1471 "not enough buffer to send data to origin\n");
1474 msg->header.size = htons (total_size);
1475 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1476 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1477 msg->tid = htonl (info->origin->tid);
1478 if (0 != info->size)
1480 memcpy (&msg[1], &info[1], info->size);
1482 if (NULL != info->client)
1484 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1492 * Function called to notify a client about the socket
1493 * being ready to queue more data. "buf" will be
1494 * NULL and "size" zero if the socket was closed for
1495 * writing in the meantime.
1497 * @param cls closure (data itself)
1498 * @param size number of bytes available in buf
1499 * @param buf where the callee should write the message
1500 * @return number of bytes written to buf
1503 send_core_data_unicast (void *cls, size_t size, void *buf)
1505 struct MeshDataDescriptor *info = cls;
1506 struct GNUNET_MESH_Unicast *msg = buf;
1509 GNUNET_assert (NULL != info);
1510 total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
1511 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1513 if (total_size > size)
1515 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1516 "not enough buffer to send data to peer\n");
1519 msg->header.size = htons (total_size);
1520 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1521 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1522 GNUNET_PEER_resolve (info->destination, &msg->destination);
1523 msg->tid = htonl (info->origin->tid);
1524 if (0 != info->size)
1526 memcpy (&msg[1], &info[1], info->size);
1528 if (NULL != info->client)
1530 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1538 * Function called to notify a client about the socket
1539 * being ready to queue more data. "buf" will be
1540 * NULL and "size" zero if the socket was closed for
1541 * writing in the meantime.
1543 * @param cls closure (data itself)
1544 * @param size number of bytes available in buf
1545 * @param buf where the callee should write the message
1546 * @return number of bytes written to buf
1549 send_core_data_multicast (void *cls, size_t size, void *buf)
1551 struct MeshDataDescriptor *info = cls;
1552 struct GNUNET_MESH_Multicast *msg = buf;
1555 GNUNET_assert (NULL != info);
1556 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1557 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1561 info->peer->core_transmit[info->handler_n] = NULL;
1563 if (total_size > size)
1565 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1566 "not enough buffer to send data futher\n");
1569 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1570 msg->header.size = htons (total_size);
1571 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1572 msg->tid = htonl (info->origin->tid);
1573 memcpy (&msg[1], &info[1], total_size);
1574 if (0 == --info->copies)
1576 if (NULL != info->client)
1578 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1587 * Function called to notify a client about the socket
1588 * being ready to queue more data. "buf" will be
1589 * NULL and "size" zero if the socket was closed for
1590 * writing in the meantime.
1592 * @param cls closure (MeshDataDescriptor)
1593 * @param size number of bytes available in buf
1594 * @param buf where the callee should write the message
1595 * @return number of bytes written to buf
1598 send_core_path_ack (void *cls, size_t size, void *buf)
1600 struct MeshDataDescriptor *info = cls;
1601 struct GNUNET_MESH_PathACK *msg = buf;
1603 GNUNET_assert (NULL != info);
1606 info->peer->core_transmit[info->handler_n] = NULL;
1608 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1613 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1614 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1615 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1616 msg->tid = htonl (info->origin->tid);
1617 msg->peer_id = my_full_id;
1619 /* TODO add signature */
1621 return sizeof (struct GNUNET_MESH_PathACK);
1626 * Function called to notify a client about the socket
1627 * being ready to queue more data. "buf" will be
1628 * NULL and "size" zero if the socket was closed for
1629 * writing in the meantime.
1631 * @param cls closure (data itself)
1632 * @param size number of bytes available in buf
1633 * @param buf where the callee should write the message
1634 * @return number of bytes written to buf
1637 send_core_data_raw (void *cls, size_t size, void *buf)
1639 struct GNUNET_MessageHeader *msg = cls;
1642 GNUNET_assert (NULL != msg);
1643 total_size = ntohs (msg->size);
1645 if (total_size > size)
1650 memcpy (buf, msg, total_size);
1658 * Send another peer a notification to destroy a tunnel
1659 * @param cls The tunnel to destroy
1660 * @param size Size in the buffer
1661 * @param buf Memory where to put the data to transmit
1662 * @return Size of data put in buffer
1665 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1667 struct MeshTunnel *t = cls;
1668 struct MeshClient *c;
1669 struct GNUNET_MESH_TunnelMessage *msg;
1673 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1674 /*FIXME*/ msg->header.size =
1675 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1676 msg->tunnel_id = htonl (t->id.tid);
1678 tunnel_destroy (c, t);
1679 return sizeof (struct GNUNET_MESH_TunnelMessage);
1685 * Send the message to all clients that have subscribed to its type
1687 * @param msg Pointer to the message itself
1688 * @return number of clients this message was sent to
1691 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1693 struct MeshClient *c;
1697 type = ntohs (msg->type);
1698 for (count = 0, c = clients; c != NULL; c = c->next)
1700 if (client_is_subscribed (type, c))
1703 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1712 * Iterator over hash map peer entries collect all neighbors who to resend the
1715 * @param cls closure (**GNUNET_PEER_Id to store hops to send packet)
1716 * @param key current key code (peer id hash)
1717 * @param value value in the hash map (peer_info)
1718 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
1721 iterate_collect_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
1723 struct MeshPeerInfo *peer_info = value;
1724 struct MeshPathInfo *neighbors = cls;
1727 if (peer_info->id == myid)
1731 peer_info = path_get_first_hop (neighbors->t, peer_info);
1732 for (i = 0; i < neighbors->path->length; i++)
1734 if (neighbors->path->peers[i] == peer_info->id)
1737 GNUNET_array_append(neighbors->path->peers,
1738 neighbors->path->length,
1745 /******************************************************************************/
1746 /******************** MESH NETWORK HANDLERS **************************/
1747 /******************************************************************************/
1751 * Core handler for path creation
1752 * struct GNUNET_CORE_MessageHandler
1754 * @param cls closure
1755 * @param message message
1756 * @param peer peer identity this notification is about
1757 * @param atsi performance data
1758 * @return GNUNET_OK to keep the connection open,
1759 * GNUNET_SYSERR to close it (signal serious error)
1763 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1764 const struct GNUNET_MessageHeader *message,
1765 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1767 unsigned int own_pos;
1770 MESH_TunnelNumber tid;
1771 struct GNUNET_MESH_ManipulatePath *msg;
1772 struct GNUNET_PeerIdentity *pi;
1773 struct GNUNET_PeerIdentity id;
1774 GNUNET_HashCode hash;
1775 struct MeshPeerPath *path;
1776 struct MeshPeerInfo *dest_peer_info;
1777 struct MeshPeerInfo *orig_peer_info;
1778 struct MeshTunnel *t;
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1781 "MESH: Received a MESH path create msg\n");
1782 size = ntohs (message->size);
1783 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1785 GNUNET_break_op (0);
1789 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1790 if (size % sizeof (struct GNUNET_PeerIdentity))
1792 GNUNET_break_op (0);
1795 size /= sizeof (struct GNUNET_PeerIdentity);
1798 GNUNET_break_op (0);
1801 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1803 tid = ntohl (msg->tid);
1804 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1805 t = tunnel_get (pi, tid);
1809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1810 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1811 t->id.oid = GNUNET_PEER_intern (pi);
1813 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1815 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1817 GNUNET_CONTAINER_multihashmap_put (
1819 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1827 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1828 if (NULL == dest_peer_info)
1830 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1831 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1832 GNUNET_CONTAINER_multihashmap_put (
1834 &pi[size - 1].hashPubKey,
1836 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1838 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1839 if (NULL == orig_peer_info)
1841 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1842 orig_peer_info->id = GNUNET_PEER_intern (pi);
1843 GNUNET_CONTAINER_multihashmap_put (
1847 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1850 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
1851 path->length = size;
1852 path->peers = GNUNET_malloc (size * sizeof (GNUNET_PEER_Id));
1854 for (i = 0; i < size; i++)
1856 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1857 if (path->peers[i] == myid)
1861 { /* cannot be self, must be 'not found' */
1862 /* create path: self not found in path through self */
1863 GNUNET_break_op (0);
1864 path_destroy (path);
1865 /* FIXME error. destroy tunnel? leave for timeout? */
1868 if (own_pos == size - 1)
1870 /* It is for us! Send ack. */
1871 struct MeshDataDescriptor *info;
1874 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1875 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1876 info->origin = &t->id;
1877 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1878 GNUNET_assert (info->peer);
1879 for (j = 0; info->peer->core_transmit[j]; j++)
1887 info->handler_n = j;
1888 info->peer->core_transmit[j] =
1889 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1890 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1891 sizeof (struct GNUNET_MessageHeader),
1892 &send_core_path_ack, info);
1896 /* It's for somebody else! Retransmit. */
1897 struct MeshPathInfo *path_info;
1899 path_info = GNUNET_malloc (sizeof(struct MeshPathInfo));
1901 path_info->path = path;
1902 path_info->peer = dest_peer_info;
1904 path_add_to_peer (dest_peer_info, path);
1905 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1906 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1907 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1908 sizeof (struct GNUNET_MessageHeader),
1909 &send_core_create_path,
1917 * Core handler for mesh network traffic going from the origin to a peer
1919 * @param cls closure
1920 * @param peer peer identity this notification is about
1921 * @param message message
1922 * @param atsi performance data
1923 * @return GNUNET_OK to keep the connection open,
1924 * GNUNET_SYSERR to close it (signal serious error)
1927 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1928 const struct GNUNET_MessageHeader *message,
1929 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1931 struct GNUNET_MESH_Unicast *msg;
1932 struct GNUNET_PeerIdentity id;
1933 struct MeshTunnel *t;
1934 struct MeshPeerInfo *pi;
1937 size = ntohs (message->size);
1939 sizeof (struct GNUNET_MESH_Unicast) +
1940 sizeof (struct GNUNET_MessageHeader))
1945 msg = (struct GNUNET_MESH_Unicast *) message;
1946 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1949 /* TODO notify back: we don't know this tunnel */
1952 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1953 &msg->destination.hashPubKey);
1956 /* TODO maybe feedback, log to statistics */
1961 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1964 GNUNET_PEER_resolve (path_get_first_hop (t, pi)->id, &id);
1965 msg = GNUNET_malloc (size);
1966 memcpy (msg, message, size);
1967 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1968 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1969 &send_core_data_raw, msg);
1975 * Core handler for mesh network traffic going from the origin to all peers
1977 * @param cls closure
1978 * @param message message
1979 * @param peer peer identity this notification is about
1980 * @param atsi performance data
1981 * @return GNUNET_OK to keep the connection open,
1982 * GNUNET_SYSERR to close it (signal serious error)
1985 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1986 const struct GNUNET_MessageHeader *message,
1987 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1989 struct GNUNET_MESH_Multicast *msg;
1990 struct GNUNET_PeerIdentity id;
1991 struct MeshDataDescriptor *info;
1992 struct MeshPathInfo neighbors;
1993 struct MeshTunnel *t;
1999 size = ntohs (message->size);
2001 sizeof (struct GNUNET_MESH_Multicast) +
2002 sizeof (struct GNUNET_MessageHeader))
2004 GNUNET_break_op (0);
2007 msg = (struct GNUNET_MESH_Multicast *) message;
2008 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2012 /* TODO notify that we dont know that tunnel */
2016 /* Transmit to locally interested clients */
2017 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2019 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
2022 /* Retransmit to other peers.
2023 * Using path here as just a collection of peers, not a path per se.
2026 neighbors.path = GNUNET_malloc (sizeof(struct MeshPeerPath));
2027 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &iterate_collect_neighbors,
2029 if (0 == neighbors.path->length)
2031 GNUNET_free(neighbors.path);
2034 size -= sizeof (struct GNUNET_MESH_Multicast);
2035 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + size);
2036 info->origin = &t->id;
2037 info->copies = neighbors.path->length;
2038 for (i = 0; i < info->copies; i++)
2040 GNUNET_PEER_resolve (neighbors.path->peers[i], &id);
2041 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2042 GNUNET_assert (NULL != info->peer);
2043 for (j = 0; 0 != info->peer->core_transmit[j]; j++)
2045 if (j == (CORE_QUEUE_SIZE - 1))
2051 info->handler_n = j;
2052 info->peer->infos[j] = info;
2053 info->peer->core_transmit[j] =
2054 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2055 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2056 ntohs (msg->header.size),
2057 &send_core_data_multicast, info);
2059 GNUNET_free(neighbors.path->peers);
2060 GNUNET_free(neighbors.path);
2066 * Core handler for mesh network traffic
2068 * @param cls closure
2069 * @param message message
2070 * @param peer peer identity this notification is about
2071 * @param atsi performance data
2072 * @return GNUNET_OK to keep the connection open,
2073 * GNUNET_SYSERR to close it (signal serious error)
2078 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2079 const struct GNUNET_MessageHeader *message,
2080 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2082 struct GNUNET_MESH_ToOrigin *msg;
2083 struct GNUNET_PeerIdentity id;
2084 struct MeshTunnel *t;
2085 struct MeshPeerInfo *peer_info;
2088 size = ntohs (message->size);
2090 sizeof (struct GNUNET_MESH_ToOrigin) +
2091 sizeof (struct GNUNET_MessageHeader))
2093 GNUNET_break_op (0);
2096 msg = (struct GNUNET_MESH_ToOrigin *) message;
2097 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2101 /* TODO notify that we dont know this tunnel (whom)? */
2105 if (t->id.oid == myid)
2107 if (NULL == t->client)
2109 /* got data packet for ownerless tunnel */
2110 GNUNET_break_op (0);
2113 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
2117 peer_info = peer_info_get (&msg->oid);
2118 if (NULL == peer_info)
2120 /* unknown origin of tunnel */
2124 GNUNET_PEER_resolve (path_get_first_hop (t, peer_info)->id, &id);
2125 msg = GNUNET_malloc (size);
2126 memcpy (msg, message, size);
2127 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2128 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
2129 &send_core_data_raw, msg);
2136 * Core handler for path ACKs
2138 * @param cls closure
2139 * @param message message
2140 * @param peer peer identity this notification is about
2141 * @param atsi performance data
2143 * @return GNUNET_OK to keep the connection open,
2144 * GNUNET_SYSERR to close it (signal serious error)
2146 * FIXME path change state
2149 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2150 const struct GNUNET_MessageHeader *message,
2151 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2153 struct GNUNET_MESH_PathACK *msg;
2154 struct GNUNET_PeerIdentity id;
2155 struct MeshTunnel *t;
2156 struct MeshPeerInfo *peer_info;
2158 msg = (struct GNUNET_MESH_PathACK *) message;
2159 t = tunnel_get (&msg->oid, msg->tid);
2162 /* TODO notify that we don't know the tunnel */
2166 /* Message for us? */
2167 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2169 struct GNUNET_MESH_PeerControl pc;
2171 if (NULL == t->client)
2176 peer_info = peer_info_get (&msg->peer_id);
2177 if (NULL == peer_info)
2179 GNUNET_break_op (0);
2182 /* FIXME change state of peer */
2183 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2184 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2185 pc.tunnel_id = htonl (t->local_tid);
2186 GNUNET_PEER_resolve (peer_info->id, &pc.peer);
2187 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
2188 &pc.header, GNUNET_NO);
2192 peer_info = peer_info_get (&msg->oid);
2193 if (NULL == peer_info)
2195 /* If we know the tunnel, we should DEFINITELY know the peer */
2199 GNUNET_PEER_resolve (path_get_first_hop (t, peer_info)->id, &id);
2200 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2201 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2202 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2203 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2204 sizeof (struct GNUNET_MESH_PathACK),
2205 &send_core_data_raw, msg);
2211 * Functions to handle messages from core
2213 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2214 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2215 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2216 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2217 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2218 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2219 sizeof (struct GNUNET_MESH_PathACK)},
2225 /******************************************************************************/
2226 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2227 /******************************************************************************/
2230 * delete_tunnel_entry: iterator for deleting each tunnel that belongs to a
2231 * client when the client disconnects.
2232 * @param cls closure (client that is disconnecting)
2233 * @param key the hash of the local tunnel id (used to access the hashmap)
2234 * @param value the value stored at the key (tunnel to destroy)
2235 * @return GNUNET_OK on success
2238 delete_tunnel_entry (void *cls, const GNUNET_HashCode * key, void *value)
2242 r = tunnel_destroy ((struct MeshTunnel *) value);
2248 * deregister_app: iterator for removing each application registered by a client
2249 * @param cls closure
2250 * @param key the hash of the application id (used to access the hashmap)
2251 * @param value the value stored at the key (client)
2252 * @return GNUNET_OK on success
2255 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2257 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2263 * notify_client_connection_failure: notify a client that the connection to the
2264 * requested remote peer is not possible (for instance, no route found)
2265 * Function called when the socket is ready to queue more data. "buf" will be
2266 * NULL and "size" zero if the socket was closed for writing in the meantime.
2268 * @param cls closure
2269 * @param size number of bytes available in buf
2270 * @param buf where the callee should write the message
2271 * @return number of bytes written to buf
2274 notify_client_connection_failure (void *cls, size_t size, void *buf)
2277 struct MeshPeerInfo *peer_info;
2278 struct GNUNET_MESH_PeerControl *msg;
2279 struct GNUNET_PeerIdentity id;
2281 if (0 == size && NULL == buf)
2283 // TODO retry? cancel?
2287 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2288 peer_info = (struct MeshPeerInfo *) cls;
2289 msg = (struct GNUNET_MESH_PeerControl *) buf;
2290 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2291 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2292 // msg->tunnel_id = htonl(peer_info->t->tid);
2293 GNUNET_PEER_resolve (peer_info->id, &id);
2294 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2302 * Function to process paths received for a new peer addition. The recorded
2303 * paths form the initial tunnel, which can be optimized later.
2304 * Called on each result obtained for the DHT search.
2306 * @param cls closure
2307 * @param exp when will this value expire
2308 * @param key key of the result
2309 * @param get_path NULL-terminated array of pointers
2310 * to the peers on reverse GET path (or NULL if not recorded)
2311 * @param put_path NULL-terminated array of pointers
2312 * to the peers on the PUT path (or NULL if not recorded)
2313 * @param type type of the result
2314 * @param size number of bytes in data
2315 * @param data pointer to the result data
2320 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2321 const GNUNET_HashCode * key,
2322 const struct GNUNET_PeerIdentity *const *get_path,
2323 const struct GNUNET_PeerIdentity *const *put_path,
2324 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2326 struct MeshPathInfo *path_info = cls;
2327 struct MeshPeerPath *p;
2328 struct GNUNET_PeerIdentity pi;
2331 if (NULL == get_path || NULL == put_path)
2333 if (NULL == path_info->peer->path_head)
2335 // Find ourselves some alternate initial path to the destination: retry
2336 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2337 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2338 path_info->peer->dhtget = GNUNET_DHT_get_start (
2339 dht_handle, /* handle */
2340 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2341 GNUNET_BLOCK_TYPE_TEST, /* type */
2342 &pi.hashPubKey, /*key to search */
2343 4, /* replication level */
2344 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2347 0, /* xquery bits */
2349 (void *) path_info);
2354 p = path_build_from_dht(get_path, put_path);
2355 path_add_to_peer (path_info->peer, p);
2356 for (i = 0; i < path_info->peer->ntunnels; i++)
2358 tunnel_add_peer(path_info->peer->tunnels[i], path_info->peer);
2360 GNUNET_free(path_info);
2367 * Function to process paths received for a new peer addition. The recorded
2368 * paths form the initial tunnel, which can be optimized later.
2369 * Called on each result obtained for the DHT search.
2371 * @param cls closure
2372 * @param exp when will this value expire
2373 * @param key key of the result
2374 * @param get_path NULL-terminated array of pointers
2375 * to the peers on reverse GET path (or NULL if not recorded)
2376 * @param put_path NULL-terminated array of pointers
2377 * to the peers on the PUT path (or NULL if not recorded)
2378 * @param type type of the result
2379 * @param size number of bytes in data
2380 * @param data pointer to the result data
2383 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2384 const GNUNET_HashCode * key,
2385 const struct GNUNET_PeerIdentity *const *get_path,
2386 const struct GNUNET_PeerIdentity *const *put_path,
2387 enum GNUNET_BLOCK_Type type, size_t size,
2390 const struct GNUNET_PeerIdentity *pi = data;
2391 struct GNUNET_PeerIdentity id;
2392 struct MeshTunnel *t = cls;
2393 struct MeshPeerInfo *peer_info;
2394 struct MeshPeerPath *p;
2397 if (size != sizeof (struct GNUNET_PeerIdentity))
2399 GNUNET_break_op (0);
2402 GNUNET_assert (NULL != t->client);
2403 GNUNET_DHT_get_stop (t->client->dht_get_type);
2404 t->client->dht_get_type = NULL;
2405 peer_info = peer_info_get (pi);
2406 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2407 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2409 if ((NULL == get_path || NULL == put_path) &&
2410 NULL == peer_info->path_head &&
2411 NULL == peer_info->dhtget)
2413 /* we don't have a route to the peer, let's try a direct lookup */
2414 peer_info->dhtget = GNUNET_DHT_get_start (
2415 dht_handle, /* handle */
2416 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2417 GNUNET_BLOCK_TYPE_TEST, /* block type */
2418 &pi->hashPubKey, /* key to look up */
2419 10U, /* replication level */
2420 GNUNET_DHT_RO_RECORD_ROUTE, /* option to dht: record route */
2421 NULL, /* bloom filter */
2424 0, /* xquery bits */
2425 dht_get_id_handler, /* callback */
2426 peer_info); /* closure */
2429 p = path_build_from_dht(get_path, put_path);
2430 path_add_to_peer (peer_info, p);
2432 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2433 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2436 for (i = 0; i < p->length; i++)
2438 GNUNET_PEER_resolve(p->peers[0], &id);
2439 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2442 GNUNET_h2s_full(&id.hashPubKey));
2446 GNUNET_PEER_resolve (p->peers[1], &id);
2447 GNUNET_CORE_notify_transmit_ready (
2448 core_handle, /* handle */
2451 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2453 sizeof (struct GNUNET_MESH_ManipulatePath) +
2454 (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
2455 &send_core_create_path, /* callback */
2456 peer_info); /* cls */
2459 /******************************************************************************/
2460 /********************* MESH LOCAL HANDLES **************************/
2461 /******************************************************************************/
2465 * Handler for client disconnection
2467 * @param cls closure
2468 * @param client identification of the client; NULL
2469 * for the last call when the server is destroyed
2472 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2474 struct MeshClient *c;
2475 struct MeshClient *next;
2477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2481 if (c->handle != client)
2483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2488 if (NULL != c->tunnels)
2490 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels, &delete_tunnel_entry,
2492 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2495 /* deregister clients applications */
2496 if (NULL != c->apps)
2498 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2499 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2501 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2502 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2504 GNUNET_SCHEDULER_cancel (announce_applications_task);
2505 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2507 if (NULL != c->types)
2508 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2509 if (NULL != c->dht_get_type)
2510 GNUNET_DHT_get_stop (c->dht_get_type);
2511 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2523 * Handler for new clients
2525 * @param cls closure
2526 * @param client identification of the client
2527 * @param message the actual message, which includes messages the client wants
2530 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2531 const struct GNUNET_MessageHeader *message)
2533 struct GNUNET_MESH_ClientConnect *cc_msg;
2534 struct MeshClient *c;
2535 GNUNET_MESH_ApplicationType *a;
2542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2543 /* Check data sanity */
2544 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2545 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2546 ntypes = ntohs (cc_msg->types);
2547 napps = ntohs (cc_msg->applications);
2549 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2552 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2556 /* Create new client structure */
2557 c = GNUNET_malloc (sizeof (struct MeshClient));
2559 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2562 GNUNET_MESH_ApplicationType at;
2565 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2566 for (i = 0; i < napps; i++)
2569 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2570 /* store in clients hashmap */
2571 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2572 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2573 /* store in global hashmap, for announcements */
2574 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2575 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2577 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2578 announce_applications_task =
2579 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2587 t = (uint16_t *) & a[napps];
2588 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2589 for (i = 0; i < ntypes; i++)
2592 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2594 /* store in clients hashmap */
2595 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2596 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2597 /* store in global hashmap */
2598 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2599 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2603 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2605 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2606 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2607 GNUNET_SERVER_notification_context_add (nc, client);
2609 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2615 * Handler for requests of new tunnels
2617 * @param cls closure
2618 * @param client identification of the client
2619 * @param message the actual message
2622 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2623 const struct GNUNET_MessageHeader *message)
2625 struct GNUNET_MESH_TunnelMessage *t_msg;
2626 struct MeshTunnel *t;
2627 struct MeshClient *c;
2628 GNUNET_HashCode hash;
2630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2632 /* Sanity check for client registration */
2633 if (NULL == (c = client_get (client)))
2636 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2640 /* Message sanity check */
2641 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2644 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2648 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2649 /* Sanity check for tunnel numbering */
2650 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2653 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2656 /* Sanity check for duplicate tunnel IDs */
2657 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2660 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2664 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2665 while (NULL != tunnel_get_by_pi (myid, next_tid))
2666 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2667 t->id.tid = next_tid++;
2669 t->local_tid = ntohl (t_msg->tunnel_id);
2671 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2673 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2675 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2676 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2679 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2683 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2685 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2686 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2689 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2693 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2699 * Handler for requests of deleting tunnels
2701 * @param cls closure
2702 * @param client identification of the client
2703 * @param message the actual message
2706 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2707 const struct GNUNET_MessageHeader *message)
2709 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2710 struct MeshClient *c;
2711 struct MeshTunnel *t;
2712 MESH_TunnelNumber tid;
2713 GNUNET_HashCode hash;
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2717 /* Sanity check for client registration */
2718 if (NULL == (c = client_get (client)))
2721 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2724 /* Message sanity check */
2725 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2728 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2732 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2734 /* Retrieve tunnel */
2735 tid = ntohl (tunnel_msg->tunnel_id);
2737 /* Remove from local id hashmap */
2738 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2739 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2740 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2742 /* Remove from global id hashmap */
2743 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2744 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2746 // notify_tunnel_destroy(t);
2747 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2753 * Handler for connection requests to new peers
2755 * @param cls closure
2756 * @param client identification of the client
2757 * @param message the actual message (PeerControl)
2762 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2763 const struct GNUNET_MessageHeader *message)
2765 struct GNUNET_MESH_PeerControl *peer_msg;
2766 struct MeshClient *c;
2767 struct MeshTunnel *t;
2768 MESH_TunnelNumber tid;
2769 struct MeshPeerInfo *peer_info;
2772 /* Sanity check for client registration */
2773 if (NULL == (c = client_get (client)))
2776 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2780 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2781 /* Sanity check for message size */
2782 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2789 /* Tunnel exists? */
2790 tid = ntohl (peer_msg->tunnel_id);
2791 t = tunnel_get_by_local_id (c, tid);
2795 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2799 /* Does client own tunnel? */
2800 if (t->client->handle != client)
2803 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2808 peer_info = peer_info_get (&peer_msg->peer);
2810 /* Start DHT search if needed FIXME: if not already connected */
2811 if (NULL == peer_info->dhtget)
2813 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 */
2814 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2817 0, /* xquery bits */
2819 (void *) peer_info);
2822 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2828 * Handler for disconnection requests of peers in a tunnel
2830 * @param cls closure
2831 * @param client identification of the client
2832 * @param message the actual message (PeerControl)
2835 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2836 const struct GNUNET_MessageHeader *message)
2838 struct GNUNET_MESH_PeerControl *peer_msg;
2839 struct MeshClient *c;
2840 struct MeshTunnel *t;
2841 MESH_TunnelNumber tid;
2843 /* Sanity check for client registration */
2844 if (NULL == (c = client_get (client)))
2847 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2850 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2851 /* Sanity check for message size */
2852 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2855 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2859 /* Tunnel exists? */
2860 tid = ntohl (peer_msg->tunnel_id);
2861 t = tunnel_get_by_local_id (c, tid);
2865 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2869 /* Does client own tunnel? */
2870 if (t->client->handle != client)
2873 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2877 /* Ok, delete peer from tunnel */
2878 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2879 &peer_msg->peer.hashPubKey);
2881 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2887 * Handler for connection requests to new peers by type
2889 * @param cls closure
2890 * @param client identification of the client
2891 * @param message the actual message (ConnectPeerByType)
2894 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2895 const struct GNUNET_MessageHeader *message)
2897 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2898 struct MeshClient *c;
2899 struct MeshTunnel *t;
2900 GNUNET_HashCode hash;
2901 GNUNET_MESH_ApplicationType type;
2902 MESH_TunnelNumber tid;
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2905 /* Sanity check for client registration */
2906 if (NULL == (c = client_get (client)))
2909 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2913 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2914 /* Sanity check for message size */
2915 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2916 ntohs (connect_msg->header.size))
2919 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2923 /* Tunnel exists? */
2924 tid = ntohl (connect_msg->tunnel_id);
2925 t = tunnel_get_by_local_id (c, tid);
2929 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2933 /* Does client own tunnel? */
2934 if (t->client->handle != client)
2937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2941 /* Do WE have the service? */
2942 type = ntohl (connect_msg->type);
2943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2944 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2945 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2948 /* Yes! Fast forward, add ourselves to the tunnel and send the
2949 * good news to the client
2951 struct GNUNET_MESH_PeerControl pc;
2953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2954 pc.peer = my_full_id;
2955 GNUNET_CONTAINER_multihashmap_put (t->peers, &pc.peer.hashPubKey,
2956 peer_info_get (&pc.peer),
2957 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2958 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2959 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2960 pc.tunnel_id = htonl (t->local_tid);
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2962 GNUNET_SERVER_notification_context_unicast (nc, /* context */
2964 &pc.header, /* msg */
2965 GNUNET_NO); /* can drop? */
2966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2967 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2970 /* Ok, lets find a peer offering the service */
2971 if (c->dht_get_type)
2973 GNUNET_DHT_get_stop (c->dht_get_type);
2975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2976 GNUNET_h2s_full (&hash));
2978 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2979 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2980 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
2981 &dht_get_type_handler, t);
2983 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2989 * Handler for client traffic directed to one peer
2991 * @param cls closure
2992 * @param client identification of the client
2993 * @param message the actual message
2996 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2997 const struct GNUNET_MessageHeader *message)
2999 struct MeshClient *c;
3000 struct MeshTunnel *t;
3001 struct MeshPeerInfo *pi;
3002 struct GNUNET_MESH_Unicast *data_msg;
3003 struct GNUNET_PeerIdentity next_hop;
3004 struct MeshDataDescriptor *info;
3005 MESH_TunnelNumber tid;
3008 /* Sanity check for client registration */
3009 if (NULL == (c = client_get (client)))
3012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3015 data_msg = (struct GNUNET_MESH_Unicast *) message;
3016 /* Sanity check for message size */
3017 if (sizeof (struct GNUNET_MESH_Unicast) +
3018 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
3021 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3025 /* Tunnel exists? */
3026 tid = ntohl (data_msg->tid);
3027 t = tunnel_get_by_local_id (c, tid);
3031 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3035 /* Is it a local tunnel? Then, does client own the tunnel? */
3036 if (t->client->handle != NULL && t->client->handle != client)
3039 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3043 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3044 &data_msg->destination.hashPubKey);
3045 /* Is the selected peer in the tunnel? */
3049 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3054 struct GNUNET_MESH_Unicast copy;
3056 /* Work around const limitation */
3057 memcpy (©, data_msg, sizeof (struct GNUNET_MESH_Unicast));
3058 copy.oid = my_full_id;
3059 copy.tid = htonl (t->id.tid);
3060 handle_mesh_data_unicast (NULL, &my_full_id, ©.header, NULL);
3063 GNUNET_PEER_resolve (path_get_first_hop (t, pi)->id, &next_hop);
3064 data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
3065 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
3066 memcpy (&info[1], &data_msg[1], data_size);
3067 info->destination = pi->id;
3068 info->origin = &t->id;
3069 info->size = data_size;
3070 info->client = client;
3071 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
3072 GNUNET_TIME_UNIT_FOREVER_REL, &next_hop,
3073 /* FIXME re-check types */
3075 sizeof (struct GNUNET_MESH_Unicast),
3076 &send_core_data_unicast, info);
3081 * Handler for client traffic directed to all peers in a tunnel
3083 * @param cls closure
3084 * @param client identification of the client
3085 * @param message the actual message
3088 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3089 const struct GNUNET_MessageHeader *message)
3091 struct MeshClient *c;
3092 struct MeshTunnel *t;
3093 struct GNUNET_MESH_Multicast *data_msg;
3094 MESH_TunnelNumber tid;
3096 /* Sanity check for client registration */
3097 if (NULL == (c = client_get (client)))
3100 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3103 data_msg = (struct GNUNET_MESH_Multicast *) message;
3104 /* Sanity check for message size */
3105 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
3108 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3112 /* Tunnel exists? */
3113 tid = ntohl (data_msg->tid);
3114 t = tunnel_get_by_local_id (c, tid);
3118 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3122 /* Does client own tunnel? */
3123 if (t->client->handle != client)
3126 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3132 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3137 * Functions to handle messages from clients
3139 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3140 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3141 {&handle_local_tunnel_create, NULL,
3142 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3143 sizeof (struct GNUNET_MESH_TunnelMessage)},
3144 {&handle_local_tunnel_destroy, NULL,
3145 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3146 sizeof (struct GNUNET_MESH_TunnelMessage)},
3147 {&handle_local_connect_add, NULL,
3148 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3149 sizeof (struct GNUNET_MESH_PeerControl)},
3150 {&handle_local_connect_del, NULL,
3151 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3152 sizeof (struct GNUNET_MESH_PeerControl)},
3153 {&handle_local_connect_by_type, NULL,
3154 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3155 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3156 {&handle_local_unicast, NULL,
3157 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3158 {&handle_local_unicast, NULL,
3159 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3160 {&handle_local_multicast, NULL,
3161 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3167 * To be called on core init/fail.
3169 * @param cls service closure
3170 * @param server handle to the server for this service
3171 * @param identity the public identity of this peer
3172 * @param publicKey the public key of this peer
3175 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3176 const struct GNUNET_PeerIdentity *identity,
3177 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
3179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3180 core_handle = server;
3181 my_full_id = *identity;
3182 myid = GNUNET_PEER_intern (identity);
3183 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3188 * Method called whenever a given peer connects.
3190 * @param cls closure
3191 * @param peer peer identity this notification is about
3192 * @param atsi performance data for the connection
3195 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3196 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3198 // GNUNET_PEER_Id pid;
3199 struct MeshPeerInfo *peer_info;
3200 struct MeshPeerPath *path;
3202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3203 peer_info = peer_info_get (peer);
3204 if (myid == peer_info->id)
3206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3208 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
3210 path->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id) * 2);
3211 path->peers[0] = myid;
3212 path->peers[1] = peer_info->id;
3213 path_add_to_peer (peer_info, path);
3218 * Method called whenever a peer disconnects.
3220 * @param cls closure
3221 * @param peer peer identity this notification is about
3224 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3226 struct MeshPeerInfo *pi;
3229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3230 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3236 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3238 if (pi->core_transmit[i])
3240 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3241 /* TODO: notify that tranmission has failed */
3242 GNUNET_free (pi->infos[i]);
3245 path_remove_from_peer (pi, pi->id, myid);
3248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3254 /******************************************************************************/
3255 /************************ MAIN FUNCTIONS ****************************/
3256 /******************************************************************************/
3259 * Task run during shutdown.
3265 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3267 struct MeshClient *c;
3269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3270 if (core_handle != NULL)
3272 GNUNET_CORE_disconnect (core_handle);
3275 if (dht_handle != NULL)
3277 for (c = clients; NULL != c; c = c->next)
3278 if (NULL != c->dht_get_type)
3279 GNUNET_DHT_get_stop (c->dht_get_type);
3280 GNUNET_DHT_disconnect (dht_handle);
3285 GNUNET_SERVER_notification_context_destroy (nc);
3288 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3290 GNUNET_SCHEDULER_cancel (announce_id_task);
3291 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3297 * Process mesh requests.
3299 * @param cls closure
3300 * @param server the initialized server
3301 * @param c configuration to use
3304 run (void *cls, struct GNUNET_SERVER_Handle *server,
3305 const struct GNUNET_CONFIGURATION_Handle *c)
3307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3308 GNUNET_SERVER_add_handlers (server, client_handlers);
3309 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
3310 server_handle = server;
3311 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3312 CORE_QUEUE_SIZE, /* queue size */
3313 NULL, /* Closure passed to MESH functions */
3314 &core_init, /* Call core_init once connected */
3315 &core_connect, /* Handle connects */
3316 &core_disconnect, /* remove peers on disconnects */
3317 NULL, /* Do we care about "status" updates? */
3318 NULL, /* Don't notify about all incoming messages */
3319 GNUNET_NO, /* For header only in notification */
3320 NULL, /* Don't notify about all outbound messages */
3321 GNUNET_NO, /* For header-only out notification */
3322 core_handlers); /* Register these handlers */
3323 if (core_handle == NULL)
3327 dht_handle = GNUNET_DHT_connect (c, 64);
3328 if (dht_handle == NULL)
3330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3331 Running without DHT has a severe\
3332 impact in MESH capabilities.\n\
3333 Plase check your configuretion and enable DHT.\n");
3339 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3340 peers = GNUNET_CONTAINER_multihashmap_create (32);
3341 applications = GNUNET_CONTAINER_multihashmap_create (32);
3342 types = GNUNET_CONTAINER_multihashmap_create (32);
3343 nc = GNUNET_SERVER_notification_context_create (server_handle,
3346 clients_tail = NULL;
3348 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3350 /* Scheduled the task to clean up when shutdown is called */
3351 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3358 * The main function for the mesh service.
3360 * @param argc number of arguments from the command line
3361 * @param argv command line arguments
3362 * @return 0 ok, 1 on error
3365 main (int argc, char *const *argv)
3369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3372 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");