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 * ID of the client, for debug messages
505 /******************************************************************************/
506 /*********************** GLOBAL VARIABLES ****************************/
507 /******************************************************************************/
512 static struct MeshClient *clients;
513 static struct MeshClient *clients_tail;
516 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
518 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
521 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
523 static struct GNUNET_CONTAINER_MultiHashMap *peers;
526 * Handle to communicate with core
528 static struct GNUNET_CORE_Handle *core_handle;
533 static struct GNUNET_DHT_Handle *dht_handle;
538 static struct GNUNET_SERVER_Handle *server_handle;
541 * Notification context, to send messages to local clients
543 static struct GNUNET_SERVER_NotificationContext *nc;
546 * Local peer own ID (memory efficient handle)
548 static GNUNET_PEER_Id myid;
551 * Local peer own ID (full value)
553 static struct GNUNET_PeerIdentity my_full_id;
558 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
563 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
566 * Tunnel ID for the next created tunnel (global tunnel number)
568 static MESH_TunnelNumber next_tid;
571 * All application types provided by this peer
573 static struct GNUNET_CONTAINER_MultiHashMap *applications;
576 * All message types clients of this peer are interested in
578 static struct GNUNET_CONTAINER_MultiHashMap *types;
581 * Task to periodically announce provided applications
583 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
586 * Task to periodically announce itself in the network
588 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
591 unsigned int next_client_id;
595 /******************************************************************************/
596 /************************ PERIODIC FUNCTIONS ****************************/
597 /******************************************************************************/
600 * Announce iterator over for each application provided by the peer
603 * @param key current key code
604 * @param value value in the hash map
605 * @return GNUNET_YES if we should continue to
610 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
612 /* FIXME are hashes in multihash map equal on all aquitectures? */
613 GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
614 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
615 (const char *) &my_full_id,
617 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
618 &mesh_debug, "DHT_put for app completed");
620 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
622 APP_ANNOUNCE_TIME, NULL, NULL);
629 * Periodically announce what applications are provided by local clients
632 * @param tc task context
635 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
637 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
639 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
643 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
645 announce_applications_task =
646 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
654 * Periodically announce self id in the DHT
657 * @param tc task context
660 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
662 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
664 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
668 * - Set data expiration in function of X
671 GNUNET_DHT_put (dht_handle, /* DHT handle */
672 &my_full_id.hashPubKey, /* Key to use */
673 10U, /* Replication level */
674 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
675 GNUNET_BLOCK_TYPE_TEST, /* Block type */
676 0, /* Size of the data */
677 NULL, /* Data itself */
678 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
679 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
681 &mesh_debug, "DHT_put for id completed");
683 NULL, /* Continuation */
684 NULL); /* Continuation closure */
687 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
692 * Send keepalive packets for a peer
700 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
702 struct MeshTunnel *t = cls;
704 // struct GNUNET_PeerIdentity id;
706 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
710 /* FIXME implement multicast keepalive. Just an empty multicast packet? */
711 // GNUNET_PEER_resolve (path_get_first_hop (path->t, path->peer)->id, &id);
712 // GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
713 // GNUNET_TIME_UNIT_FOREVER_REL, &id,
714 // sizeof (struct GNUNET_MESH_ManipulatePath)
716 // (path->path->length *
717 // sizeof (struct GNUNET_PeerIdentity)),
718 // &send_core_create_path,
720 t->path_refresh_task =
721 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
726 /******************************************************************************/
727 /****************** GENERAL HELPER FUNCTIONS ************************/
728 /******************************************************************************/
731 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
732 * and insert it in the appropiate structures if the peer is not known yet.
734 * @param peer Identity of the peer
736 * @return Existing or newly created peer info
738 static struct MeshPeerInfo *
739 peer_info_get (const struct GNUNET_PeerIdentity *peer)
741 struct MeshPeerInfo *peer_info;
743 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
744 if (NULL == peer_info)
747 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
748 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
749 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
750 peer_info->id = GNUNET_PEER_intern (peer);
759 * Destroy the peer_info and free any allocated resources linked to it
760 * @param t tunnel the path belongs to
761 * @param pi the peer_info to destroy
762 * @return GNUNET_OK on success
765 peer_info_destroy (struct MeshPeerInfo *pi)
767 GNUNET_HashCode hash;
768 struct GNUNET_PeerIdentity id;
770 GNUNET_PEER_resolve (pi->id, &id);
771 GNUNET_PEER_change_rc (pi->id, -1);
772 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
774 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
775 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
783 * Destroy the path and free any allocated resources linked to it
785 * @param p the path to destroy
787 * @return GNUNET_OK on success
790 path_destroy (struct MeshPeerPath *p)
792 GNUNET_PEER_decrement_rcs (p->peers, p->length);
793 GNUNET_free (p->peers);
802 * @param p the path to invert
805 path_invert (struct MeshPeerPath *path)
810 for (i = 0; i < path->length / 2; i++)
812 aux = path->peers[i];
813 path->peers[i] = path->peers[path->length - i - 1];
814 path->peers[path->length - i - 1] = aux;
820 * Find the first peer whom to send a packet to go down this path
822 * @param t The tunnel to use
823 * @param peer The peerinfo of the peer we are trying to reach
825 * @return peerinfo of the peer who is the first hop in the tunnel
828 static struct MeshPeerInfo *
829 path_get_first_hop (struct MeshTunnel *t, struct MeshPeerInfo *peer)
831 struct GNUNET_PeerIdentity id;
833 GNUNET_PEER_resolve (peer->id, &id);
834 return GNUNET_CONTAINER_multihashmap_get (t->paths->first_hops,
840 * Get the length of a path
842 * @param path The path to measure, with the local peer at any point of it
844 * @return Number of hops to reach destination
845 * UINT_MAX in case the peer is not in the path
848 path_get_length (struct MeshPeerPath *path)
854 for (i = 0; i < path->length; i++)
856 if (path->peers[i] == myid)
858 return path->length - i;
866 * Get the cost of the path relative to the already built tunnel tree
868 * @param t The tunnel to which compare
869 * @param path The individual path to reach a peer
871 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
874 * TODO: remove dummy implementation, look into the tunnel tree
877 path_get_cost (struct MeshTunnel *t, struct MeshPeerPath *path)
879 return path_get_length (path);
884 * Add the path to the peer and update the path used to reach it in case this
887 * @param peer_info Destination peer to add the path to.
888 * @param path New path to add. Last peer must be the peer in arg 1.
890 * TODO: trim the part from origin to us? Add it as path to origin?
893 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
896 struct MeshPeerPath *aux;
898 if (NULL == peer_info || NULL == path)
904 l = path_get_length (path);
906 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
908 if (path_get_length (aux) > l)
910 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
911 peer_info->path_tail, aux, path);
914 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
921 * Notify a tunnel that a connection has broken that affects at least
924 * @param t Tunnel affected
925 * @param peer Peer that (at least) has been affected by the disconnection
926 * @param p1 Peer that got disconnected from p2
927 * @param p2 Peer that got disconnected from p1
930 tunnel_notify_connection_broken (struct MeshTunnel *t,
931 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
936 * Remove all paths that rely on a direct connection between p1 and p2
937 * from the peer itself and notify all tunnels about it.
939 * @param pi PeerInfo of affected peer
940 * @param p1 GNUNET_PEER_Id of one peer.
941 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
945 path_remove_from_peer (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
948 struct MeshPeerPath *p;
949 struct MeshPeerPath *aux;
950 unsigned int destroyed;
958 for (i = 0; i < (p->length - 1); i++)
960 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
961 (p->peers[i] == p2 && p->peers[i + 1] == p1))
973 for (i = 0; i < peer->ntunnels; i++)
975 tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
981 * Add the path to the origin peer and update the path used to reach it in case
982 * this is the shortest.
983 * The path is given in peer_info -> destination, therefore we turn the path
986 * @param peer_info Peer to add the path to, being the origin of the path.
987 * @param path New path to add after being inversed.
990 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
993 path_add_to_peer (peer_info, path);
998 * Build a PeerPath from the paths returned from the DHT, reversing the paths
999 * to obtain a local peer -> destination path and interning the peer ids.
1001 * @param get_path NULL-terminated array of pointers
1002 * to the peers on reverse GET path (or NULL if not recorded)
1003 * @param put_path NULL-terminated array of pointers
1004 * to the peers on the PUT path (or NULL if not recorded)
1006 * @return Newly allocated and created path
1008 static struct MeshPeerPath *
1009 path_build_from_dht (const struct GNUNET_PeerIdentity *const *get_path,
1010 const struct GNUNET_PeerIdentity *const *put_path)
1012 struct MeshPeerPath *p;
1015 p = GNUNET_malloc (sizeof (struct MeshPeerPath));
1016 for (i = 0; get_path[i] != NULL; i++) ;
1017 for (i--; i >= 0; i--)
1020 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1021 p->peers[p->length] = GNUNET_PEER_intern (get_path[i]);
1024 for (i = 0; put_path[i] != NULL; i++) ;
1025 for (i--; i >= 0; i--)
1028 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1029 p->peers[p->length] = GNUNET_PEER_intern (put_path[i]);
1037 * Check if client has registered with the service and has not disconnected
1039 * @param client the client to check
1041 * @return non-NULL if client exists in the global DLL
1043 static struct MeshClient *
1044 client_get (struct GNUNET_SERVER_Client *client)
1046 struct MeshClient *c;
1051 if (c->handle == client)
1060 * Checks if a given client has subscribed to certain message type
1062 * @param message_type Type of message to check
1063 * @param c Client to check
1065 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1070 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1074 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1075 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1080 * Search for a tunnel among the tunnels for a client
1082 * @param c the client whose tunnels to search in
1083 * @param tid the local id of the tunnel
1085 * @return tunnel handler, NULL if doesn't exist
1087 static struct MeshTunnel *
1088 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1090 GNUNET_HashCode hash;
1092 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1093 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1098 * Search for a tunnel by global ID using PEER_ID
1100 * @param pi owner of the tunnel
1101 * @param tid global tunnel number
1103 * @return tunnel handler, NULL if doesn't exist
1105 static struct MeshTunnel *
1106 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1108 struct MESH_TunnelID id;
1109 GNUNET_HashCode hash;
1114 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1115 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1121 * Search for a tunnel by global ID using full PeerIdentities
1123 * @param oid owner of the tunnel
1124 * @param tid global tunnel number
1126 * @return tunnel handler, NULL if doesn't exist
1128 static struct MeshTunnel *
1129 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1131 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1136 * Recursively find the given peer in the tree.
1138 * @param t Tunnel where to add the new path.
1139 * @param p Path to look for.
1141 * @return Pointer to the node of the peer. NULL if not found.
1143 static struct MeshTunnelPathNode *
1144 tunnel_find_peer (struct MeshTunnelPathNode *root, struct MeshPeerInfo *peer)
1146 struct MeshTunnelPathNode *n;
1149 if (root->peer == peer)
1151 for (i = 0; i < root->nchildren; i++)
1153 n = tunnel_find_peer (&root->children[i], peer);
1162 * Recusively mark peer and children as disconnected, notify client
1164 * @param parent Node to be clean, potentially with children
1167 tunnel_mark_peers_disconnected (struct MeshTunnelPathNode *parent)
1169 struct GNUNET_MESH_PeerControl msg;
1172 parent->status = MESH_PEER_RECONNECTING;
1173 for (i = 0; i < parent->nchildren; i++)
1175 tunnel_mark_peers_disconnected (&parent->children[i]);
1177 if (NULL == parent->t->client)
1179 msg.header.size = htons (sizeof (msg));
1180 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1181 msg.tunnel_id = htonl (parent->t->local_tid);
1182 GNUNET_PEER_resolve (parent->peer->id, &msg.peer);
1183 GNUNET_SERVER_notification_context_unicast (nc, parent->t->client->handle,
1184 &msg.header, GNUNET_NO);
1188 * Delete the current path to the peer, including all now unused relays.
1189 * The destination peer is NOT destroyed, it is returned in order to either set
1190 * a new path to it or destroy it explicitly, taking care of it's child nodes.
1192 * @param t Tunnel where to delete the path from.
1193 * @param peer Destination peer whose path we want to remove.
1195 * @return pointer to the pathless node, NULL on error
1197 * TODO: notify peers of deletion
1199 static struct MeshTunnelPathNode *
1200 tunnel_del_path (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1202 struct MeshTunnelPathNode *parent;
1203 struct MeshTunnelPathNode *node;
1204 struct MeshTunnelPathNode *n;
1206 node = n = tunnel_find_peer (t->paths->me, peer);
1211 while (NULL != parent && MESH_PEER_RELAY == parent->status &&
1212 1 == parent->nchildren)
1215 GNUNET_free (parent->children);
1216 parent = parent->parent;
1220 *n = parent->children[parent->nchildren - 1];
1221 parent->nchildren--;
1222 parent->children = GNUNET_realloc (parent->children, parent->nchildren);
1224 tunnel_mark_peers_disconnected (node);
1231 * Return a newly allocated individual path to reach a peer from the local peer,
1232 * according to the path tree of some tunnel.
1234 * @param t Tunnel from which to read the path tree
1235 * @param peer_info Destination peer to whom we want a path
1237 * @return A newly allocated individual path to reach the destination peer.
1238 * Path must be destroyed afterwards.
1240 static struct MeshPeerPath *
1241 tunnel_get_path_to_peer(struct MeshTunnel *t, struct MeshPeerInfo *peer_info)
1243 struct MeshTunnelPathNode *n;
1244 struct MeshPeerPath *p;
1246 n = tunnel_find_peer(t->paths->me, peer_info);
1247 p = GNUNET_malloc(sizeof(struct MeshPeerPath));
1249 /* Building the path (inverted!) */
1250 while (n->peer->id != myid)
1252 GNUNET_array_append(p->peers, p->length, n->peer->id);
1253 GNUNET_PEER_change_rc(n->peer->id, 1);
1255 GNUNET_assert(NULL != n);
1257 GNUNET_array_append(p->peers, p->length, myid);
1258 GNUNET_PEER_change_rc(myid, 1);
1267 * Integrate a stand alone path into the tunnel tree.
1269 * @param t Tunnel where to add the new path.
1270 * @param p Path to be integrated.
1272 * @return GNUNET_OK in case of success.
1273 * GNUNET_SYSERR in case of error.
1276 * - go backwards on path looking for each peer in the present tree
1279 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1281 struct MeshTunnelPathNode *parent;
1282 struct MeshTunnelPathNode *oldnode;
1283 struct MeshTunnelPathNode *n;
1284 struct GNUNET_PeerIdentity id;
1285 struct GNUNET_PeerIdentity hop;
1291 if (n->peer->id != p->peers[0])
1293 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1294 "local id's: %u %s\n",
1296 GNUNET_h2s_full(&my_full_id.hashPubKey));
1297 GNUNET_PEER_resolve(n->peer->id, &id);
1298 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1300 GNUNET_h2s_full(&id.hashPubKey));
1301 GNUNET_PEER_resolve (p->peers[0], &id);
1302 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1304 GNUNET_h2s_full(&id.hashPubKey));
1307 return GNUNET_SYSERR;
1309 /* Ignore return value, if not found it's ok. */
1310 GNUNET_PEER_resolve (p->peers[p->length - 1], &id);
1311 oldnode = tunnel_del_path (t, peer_info_get (&id));
1312 /* Look for the first node that is not already present in the tree
1314 * Assuming that the tree is somewhat balanced, O(log n * log N).
1315 * - Length of the path is expected to be log N (size of whole network).
1316 * - Each level of the tree is expected to have log n children (size of tree).
1318 for (i = 1, me = -1; i < p->length; i++)
1321 if (p->peers[i] == myid)
1323 for (j = 0; j < n->nchildren; j++)
1325 if (n->children[j].peer->id == p->peers[i])
1327 n = &n->children[j];
1331 /* If we couldn't find a child equal to path[i], we have reached the end
1332 * of the common path. */
1338 /* New path deviates from tree before reaching us. What happened? */
1340 return GNUNET_SYSERR;
1342 /* Add the rest of the path as a branch from parent. */
1343 while (i < p->length)
1345 parent->nchildren++;
1346 parent->children = GNUNET_realloc (parent->children,
1348 sizeof(struct MeshTunnelPathNode));
1349 n = &parent->children[parent->nchildren - 1];
1350 if (i == p->length - 1 && NULL != oldnode)
1352 /* Assignation and free can be misleading, using explicit mempcy */
1353 memcpy (n, oldnode, sizeof (struct MeshTunnelPathNode));
1354 GNUNET_free (oldnode);
1359 n->status = MESH_PEER_RELAY;
1360 GNUNET_PEER_resolve (p->peers[i], &id);
1361 n->peer = peer_info_get (&id);
1368 /* Add info about first hop into hashmap. */
1369 if (me < p->length - 1)
1371 GNUNET_PEER_resolve (p->peers[p->length - 1], &id);
1372 GNUNET_PEER_resolve (p->peers[me + 1], &hop);
1373 GNUNET_CONTAINER_multihashmap_put (t->paths->first_hops, &id.hashPubKey,
1374 peer_info_get (&hop),
1375 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1382 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1383 * needed rescources.
1385 * @param t Tunnel we want to add a new peer to
1386 * @param peer PeerInfo of the peer being added
1390 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1392 struct MeshPeerPath *p;
1393 struct MeshPeerPath *best_p;
1394 unsigned int best_cost;
1397 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1398 if (NULL == (p = peer->path_head))
1402 best_cost = UINT_MAX;
1405 if ((cost = path_get_cost (t, p)) < best_cost)
1412 tunnel_add_path (t, best_p);
1413 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1414 t->path_refresh_task =
1415 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
1420 * Notify a tunnel that a connection has broken that affects at least
1421 * some of its peers.
1423 * @param t Tunnel affected
1424 * @param peer Peer that (at least) has been affected by the disconnection
1425 * @param p1 Peer that got disconnected from p2
1426 * @param p2 Peer that got disconnected from p1
1431 tunnel_notify_connection_broken (struct MeshTunnel *t,
1432 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1439 * Recursively destory the path tree of a tunnel.
1440 * Note: it does not liberate memory for itself, parent must do it!
1442 * @param n The node to destroy, along with children.
1444 * @return GNUNET_OK on success
1447 tunnel_destroy_tree_node (struct MeshTunnelPathNode *n)
1451 for (i = 0; i < n->nchildren; i++)
1453 tunnel_destroy_tree_node(&n->children[i]);
1455 if (NULL != n->children)
1456 GNUNET_free (n->children);
1461 * Destroy the tunnel and free any allocated resources linked to it
1463 * @param t the tunnel to destroy
1465 * @return GNUNET_OK on success
1468 tunnel_destroy (struct MeshTunnel *t)
1470 struct MeshClient *c;
1471 struct MeshQueue *q;
1472 struct MeshQueue *qn;
1473 GNUNET_HashCode hash;
1476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1485 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1486 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1491 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1492 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1496 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1500 if (NULL != q->data)
1501 GNUNET_free (q->data);
1505 /* TODO cancel core transmit ready in case it was active */
1508 GNUNET_CONTAINER_multihashmap_destroy(t->paths->first_hops);
1509 tunnel_destroy_tree_node(t->paths->root);
1510 GNUNET_free(t->paths->root);
1511 GNUNET_free (t->paths);
1518 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1519 * client when the client disconnects.
1521 * @param cls closure (client that is disconnecting)
1522 * @param key the hash of the local tunnel id (used to access the hashmap)
1523 * @param value the value stored at the key (tunnel to destroy)
1525 * @return GNUNET_OK on success
1528 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1532 r = tunnel_destroy ((struct MeshTunnel *) value);
1537 /******************************************************************************/
1538 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1539 /******************************************************************************/
1543 * Function called to notify a client about the socket
1544 * being ready to queue more data. "buf" will be
1545 * NULL and "size" zero if the socket was closed for
1546 * writing in the meantime.
1548 * @param cls closure
1549 * @param size number of bytes available in buf
1550 * @param buf where the callee should write the message
1551 * @return number of bytes written to buf
1554 send_core_create_path (void *cls, size_t size, void *buf)
1556 struct MeshPathInfo *info = cls;
1557 struct GNUNET_MESH_ManipulatePath *msg;
1558 struct GNUNET_PeerIdentity *peer_ptr;
1559 struct GNUNET_PeerIdentity id;
1560 struct MeshPeerInfo *peer = info->peer;
1561 struct MeshTunnel *t = info->t;
1562 struct MeshPeerPath *p = info->path;
1567 sizeof (struct GNUNET_MESH_ManipulatePath) +
1568 p->length * sizeof (struct GNUNET_PeerIdentity);
1570 if (size < size_needed || NULL == buf)
1572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
1573 GNUNET_PEER_resolve (path_get_first_hop (t, peer)->id, &id);
1574 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1575 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1576 size_needed, &send_core_create_path,
1581 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1582 msg->header.size = htons (size_needed);
1583 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1584 msg->tid = ntohl (t->id.tid);
1586 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1587 for (i = 0; i < p->length; i++)
1589 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1601 * Function called to notify a client about the socket
1602 * being ready to queue more data. "buf" will be
1603 * NULL and "size" zero if the socket was closed for
1604 * writing in the meantime.
1606 * @param cls closure (MeshDataDescriptor with all info to build packet)
1607 * @param size number of bytes available in buf
1608 * @param buf where the callee should write the message
1609 * @return number of bytes written to buf
1612 send_core_data_to_origin (void *cls, size_t size, void *buf)
1614 struct MeshDataDescriptor *info = cls;
1615 struct GNUNET_MESH_ToOrigin *msg = buf;
1618 GNUNET_assert (NULL != info);
1619 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1620 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1622 if (total_size > size)
1624 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1625 "not enough buffer to send data to origin\n");
1628 msg->header.size = htons (total_size);
1629 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1630 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1631 msg->tid = htonl (info->origin->tid);
1632 if (0 != info->size)
1634 memcpy (&msg[1], &info[1], info->size);
1636 if (NULL != info->client)
1638 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1646 * Function called to notify a client about the socket
1647 * being ready to queue more data. "buf" will be
1648 * NULL and "size" zero if the socket was closed for
1649 * writing in the meantime.
1651 * @param cls closure (data itself)
1652 * @param size number of bytes available in buf
1653 * @param buf where the callee should write the message
1654 * @return number of bytes written to buf
1657 send_core_data_unicast (void *cls, size_t size, void *buf)
1659 struct MeshDataDescriptor *info = cls;
1660 struct GNUNET_MESH_Unicast *msg = buf;
1663 GNUNET_assert (NULL != info);
1664 total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
1665 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1667 if (total_size > size)
1669 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1670 "not enough buffer to send data to peer\n");
1673 msg->header.size = htons (total_size);
1674 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1675 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1676 GNUNET_PEER_resolve (info->destination, &msg->destination);
1677 msg->tid = htonl (info->origin->tid);
1678 if (0 != info->size)
1680 memcpy (&msg[1], &info[1], info->size);
1682 if (NULL != info->client)
1684 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1692 * Function called to notify a client about the socket
1693 * being ready to queue more data. "buf" will be
1694 * NULL and "size" zero if the socket was closed for
1695 * writing in the meantime.
1697 * @param cls closure (data itself)
1698 * @param size number of bytes available in buf
1699 * @param buf where the callee should write the message
1700 * @return number of bytes written to buf
1703 send_core_data_multicast (void *cls, size_t size, void *buf)
1705 struct MeshDataDescriptor *info = cls;
1706 struct GNUNET_MESH_Multicast *msg = buf;
1709 GNUNET_assert (NULL != info);
1710 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1711 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1715 info->peer->core_transmit[info->handler_n] = NULL;
1717 if (total_size > size)
1719 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1720 "not enough buffer to send data futher\n");
1723 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1724 msg->header.size = htons (total_size);
1725 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1726 msg->tid = htonl (info->origin->tid);
1727 memcpy (&msg[1], &info[1], total_size);
1728 if (0 == --info->copies)
1730 if (NULL != info->client)
1732 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1741 * Function called to notify a client about the socket
1742 * being ready to queue more data. "buf" will be
1743 * NULL and "size" zero if the socket was closed for
1744 * writing in the meantime.
1746 * @param cls closure (MeshDataDescriptor)
1747 * @param size number of bytes available in buf
1748 * @param buf where the callee should write the message
1749 * @return number of bytes written to buf
1752 send_core_path_ack (void *cls, size_t size, void *buf)
1754 struct MeshDataDescriptor *info = cls;
1755 struct GNUNET_MESH_PathACK *msg = buf;
1757 GNUNET_assert (NULL != info);
1760 info->peer->core_transmit[info->handler_n] = NULL;
1762 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1767 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1768 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1769 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1770 msg->tid = htonl (info->origin->tid);
1771 msg->peer_id = my_full_id;
1773 /* TODO add signature */
1775 return sizeof (struct GNUNET_MESH_PathACK);
1780 * Function called to notify a client about the socket
1781 * being ready to queue more data. "buf" will be
1782 * NULL and "size" zero if the socket was closed for
1783 * writing in the meantime.
1785 * @param cls closure (data itself)
1786 * @param size number of bytes available in buf
1787 * @param buf where the callee should write the message
1788 * @return number of bytes written to buf
1791 send_core_data_raw (void *cls, size_t size, void *buf)
1793 struct GNUNET_MessageHeader *msg = cls;
1796 GNUNET_assert (NULL != msg);
1797 total_size = ntohs (msg->size);
1799 if (total_size > size)
1804 memcpy (buf, msg, total_size);
1812 * Send another peer a notification to destroy a tunnel
1813 * @param cls The tunnel to destroy
1814 * @param size Size in the buffer
1815 * @param buf Memory where to put the data to transmit
1816 * @return Size of data put in buffer
1819 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1821 struct MeshTunnel *t = cls;
1822 struct MeshClient *c;
1823 struct GNUNET_MESH_TunnelMessage *msg;
1827 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1828 /*FIXME*/ msg->header.size =
1829 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1830 msg->tunnel_id = htonl (t->id.tid);
1832 tunnel_destroy (c, t);
1833 return sizeof (struct GNUNET_MESH_TunnelMessage);
1839 * Send the message to all clients that have subscribed to its type
1841 * @param msg Pointer to the message itself
1842 * @return number of clients this message was sent to
1845 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1847 struct MeshClient *c;
1851 type = ntohs (msg->type);
1852 for (count = 0, c = clients; c != NULL; c = c->next)
1854 if (client_is_subscribed (type, c))
1857 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1866 * Iterator over hash map peer entries collect all neighbors who to resend the
1869 * @param cls closure (**GNUNET_PEER_Id to store hops to send packet)
1870 * @param key current key code (peer id hash)
1871 * @param value value in the hash map (peer_info)
1872 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
1875 iterate_collect_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
1877 struct MeshPeerInfo *peer_info = value;
1878 struct MeshPathInfo *neighbors = cls;
1881 if (peer_info->id == myid)
1885 peer_info = path_get_first_hop (neighbors->t, peer_info);
1886 for (i = 0; i < neighbors->path->length; i++)
1888 if (neighbors->path->peers[i] == peer_info->id)
1891 GNUNET_array_append (neighbors->path->peers, neighbors->path->length,
1898 /******************************************************************************/
1899 /******************** MESH NETWORK HANDLERS **************************/
1900 /******************************************************************************/
1904 * Core handler for path creation
1905 * struct GNUNET_CORE_MessageHandler
1907 * @param cls closure
1908 * @param message message
1909 * @param peer peer identity this notification is about
1910 * @param atsi performance data
1911 * @return GNUNET_OK to keep the connection open,
1912 * GNUNET_SYSERR to close it (signal serious error)
1916 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1917 const struct GNUNET_MessageHeader *message,
1918 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1920 unsigned int own_pos;
1923 MESH_TunnelNumber tid;
1924 struct GNUNET_MESH_ManipulatePath *msg;
1925 struct GNUNET_PeerIdentity *pi;
1926 struct GNUNET_PeerIdentity id;
1927 GNUNET_HashCode hash;
1928 struct MeshPeerPath *path;
1929 struct MeshPeerInfo *dest_peer_info;
1930 struct MeshPeerInfo *orig_peer_info;
1931 struct MeshTunnel *t;
1933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1934 "MESH: Received a MESH path create msg\n");
1935 size = ntohs (message->size);
1936 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1938 GNUNET_break_op (0);
1942 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1943 if (size % sizeof (struct GNUNET_PeerIdentity))
1945 GNUNET_break_op (0);
1948 size /= sizeof (struct GNUNET_PeerIdentity);
1951 GNUNET_break_op (0);
1954 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1956 tid = ntohl (msg->tid);
1957 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1958 t = tunnel_get (pi, tid);
1962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1963 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1964 t->id.oid = GNUNET_PEER_intern (pi);
1966 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1968 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1970 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1971 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1979 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1980 if (NULL == dest_peer_info)
1982 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1983 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1984 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1986 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1988 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1989 if (NULL == orig_peer_info)
1991 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1992 orig_peer_info->id = GNUNET_PEER_intern (pi);
1993 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1994 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1997 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
1998 path->length = size;
1999 path->peers = GNUNET_malloc (size * sizeof (GNUNET_PEER_Id));
2001 for (i = 0; i < size; i++)
2003 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2004 if (path->peers[i] == myid)
2008 { /* cannot be self, must be 'not found' */
2009 /* create path: self not found in path through self */
2010 GNUNET_break_op (0);
2011 path_destroy (path);
2012 /* FIXME error. destroy tunnel? leave for timeout? */
2015 if (own_pos == size - 1)
2017 /* It is for us! Send ack. */
2018 struct MeshDataDescriptor *info;
2021 path_add_to_origin (orig_peer_info, path); /* inverts path! */
2022 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2023 info->origin = &t->id;
2024 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2025 GNUNET_assert (info->peer);
2026 for (j = 0; info->peer->core_transmit[j]; j++)
2034 info->handler_n = j;
2035 info->peer->core_transmit[j] =
2036 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2037 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2038 sizeof (struct GNUNET_MessageHeader),
2039 &send_core_path_ack, info);
2043 /* It's for somebody else! Retransmit. */
2044 struct MeshPathInfo *path_info;
2046 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2048 path_info->path = path;
2049 path_info->peer = dest_peer_info;
2051 path_add_to_peer (dest_peer_info, path);
2052 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
2053 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2054 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2055 sizeof (struct GNUNET_MessageHeader),
2056 &send_core_create_path, path_info);
2063 * Core handler for mesh network traffic going from the origin to a peer
2065 * @param cls closure
2066 * @param peer peer identity this notification is about
2067 * @param message message
2068 * @param atsi performance data
2069 * @return GNUNET_OK to keep the connection open,
2070 * GNUNET_SYSERR to close it (signal serious error)
2073 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2074 const struct GNUNET_MessageHeader *message,
2075 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2077 struct GNUNET_MESH_Unicast *msg;
2078 struct GNUNET_PeerIdentity id;
2079 struct MeshTunnel *t;
2080 struct MeshPeerInfo *pi;
2083 size = ntohs (message->size);
2085 sizeof (struct GNUNET_MESH_Unicast) +
2086 sizeof (struct GNUNET_MessageHeader))
2091 msg = (struct GNUNET_MESH_Unicast *) message;
2092 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2095 /* TODO notify back: we don't know this tunnel */
2098 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2099 &msg->destination.hashPubKey);
2102 /* TODO maybe feedback, log to statistics */
2107 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
2110 GNUNET_PEER_resolve (path_get_first_hop (t, pi)->id, &id);
2111 msg = GNUNET_malloc (size);
2112 memcpy (msg, message, size);
2113 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2114 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
2115 &send_core_data_raw, msg);
2121 * Core handler for mesh network traffic going from the origin to all peers
2123 * @param cls closure
2124 * @param message message
2125 * @param peer peer identity this notification is about
2126 * @param atsi performance data
2127 * @return GNUNET_OK to keep the connection open,
2128 * GNUNET_SYSERR to close it (signal serious error)
2131 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2132 const struct GNUNET_MessageHeader *message,
2133 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2135 struct GNUNET_MESH_Multicast *msg;
2136 struct GNUNET_PeerIdentity id;
2137 struct MeshDataDescriptor *info;
2138 struct MeshPathInfo neighbors;
2139 struct MeshTunnel *t;
2145 size = ntohs (message->size);
2147 sizeof (struct GNUNET_MESH_Multicast) +
2148 sizeof (struct GNUNET_MessageHeader))
2150 GNUNET_break_op (0);
2153 msg = (struct GNUNET_MESH_Multicast *) message;
2154 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2158 /* TODO notify that we dont know that tunnel */
2162 /* Transmit to locally interested clients */
2163 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2165 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
2168 /* Retransmit to other peers.
2169 * Using path here as just a collection of peers, not a path per se.
2172 neighbors.path = GNUNET_malloc (sizeof (struct MeshPeerPath));
2173 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &iterate_collect_neighbors,
2175 if (0 == neighbors.path->length)
2177 GNUNET_free (neighbors.path);
2180 size -= sizeof (struct GNUNET_MESH_Multicast);
2181 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + size);
2182 info->origin = &t->id;
2183 info->copies = neighbors.path->length;
2184 for (i = 0; i < info->copies; i++)
2186 GNUNET_PEER_resolve (neighbors.path->peers[i], &id);
2187 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2188 GNUNET_assert (NULL != info->peer);
2189 for (j = 0; 0 != info->peer->core_transmit[j]; j++)
2191 if (j == (CORE_QUEUE_SIZE - 1))
2197 info->handler_n = j;
2198 info->peer->infos[j] = info;
2199 info->peer->core_transmit[j] =
2200 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2201 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2202 ntohs (msg->header.size),
2203 &send_core_data_multicast, info);
2205 GNUNET_free (neighbors.path->peers);
2206 GNUNET_free (neighbors.path);
2212 * Core handler for mesh network traffic
2214 * @param cls closure
2215 * @param message message
2216 * @param peer peer identity this notification is about
2217 * @param atsi performance data
2219 * @return GNUNET_OK to keep the connection open,
2220 * GNUNET_SYSERR to close it (signal serious error)
2223 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2224 const struct GNUNET_MessageHeader *message,
2225 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2227 struct GNUNET_MESH_ToOrigin *msg;
2228 struct GNUNET_PeerIdentity id;
2229 struct MeshPeerInfo *peer_info;
2230 struct MeshTunnel *t;
2233 size = ntohs (message->size);
2234 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2235 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2237 GNUNET_break_op (0);
2240 msg = (struct GNUNET_MESH_ToOrigin *) message;
2241 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2245 /* TODO notify that we dont know this tunnel (whom)? */
2249 if (t->id.oid == myid)
2251 if (NULL == t->client)
2253 /* got data packet for ownerless tunnel */
2254 GNUNET_break_op (0);
2257 /* TODO signature verification */
2258 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
2262 peer_info = peer_info_get (&msg->oid);
2263 if (NULL == peer_info)
2265 /* unknown origin of tunnel */
2269 GNUNET_PEER_resolve (t->paths->me->parent->peer->id, &id);
2270 msg = GNUNET_malloc (size);
2271 memcpy (msg, message, size);
2272 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2273 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
2274 &send_core_data_raw, msg);
2281 * Core handler for path ACKs
2283 * @param cls closure
2284 * @param message message
2285 * @param peer peer identity this notification is about
2286 * @param atsi performance data
2288 * @return GNUNET_OK to keep the connection open,
2289 * GNUNET_SYSERR to close it (signal serious error)
2291 * FIXME path change state
2294 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2295 const struct GNUNET_MessageHeader *message,
2296 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2298 struct GNUNET_MESH_PathACK *msg;
2299 struct GNUNET_PeerIdentity id;
2300 struct MeshTunnel *t;
2301 struct MeshPeerInfo *peer_info;
2303 msg = (struct GNUNET_MESH_PathACK *) message;
2304 t = tunnel_get (&msg->oid, msg->tid);
2307 /* TODO notify that we don't know the tunnel */
2311 /* Message for us? */
2312 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2314 struct GNUNET_MESH_PeerControl pc;
2316 if (NULL == t->client)
2321 peer_info = peer_info_get (&msg->peer_id);
2322 if (NULL == peer_info)
2324 GNUNET_break_op (0);
2327 /* FIXME change state of peer */
2328 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2329 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2330 pc.tunnel_id = htonl (t->local_tid);
2331 GNUNET_PEER_resolve (peer_info->id, &pc.peer);
2332 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
2333 &pc.header, GNUNET_NO);
2337 peer_info = peer_info_get (&msg->oid);
2338 if (NULL == peer_info)
2340 /* If we know the tunnel, we should DEFINITELY know the peer */
2344 GNUNET_PEER_resolve (path_get_first_hop (t, peer_info)->id, &id);
2345 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2346 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2347 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2348 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2349 sizeof (struct GNUNET_MESH_PathACK),
2350 &send_core_data_raw, msg);
2356 * Functions to handle messages from core
2358 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2359 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2360 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2361 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2362 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2363 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2364 sizeof (struct GNUNET_MESH_PathACK)},
2370 /******************************************************************************/
2371 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2372 /******************************************************************************/
2375 * deregister_app: iterator for removing each application registered by a client
2377 * @param cls closure
2378 * @param key the hash of the application id (used to access the hashmap)
2379 * @param value the value stored at the key (client)
2381 * @return GNUNET_OK on success
2384 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2386 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2392 * notify_client_connection_failure: notify a client that the connection to the
2393 * requested remote peer is not possible (for instance, no route found)
2394 * Function called when the socket is ready to queue more data. "buf" will be
2395 * NULL and "size" zero if the socket was closed for writing in the meantime.
2397 * @param cls closure
2398 * @param size number of bytes available in buf
2399 * @param buf where the callee should write the message
2400 * @return number of bytes written to buf
2403 notify_client_connection_failure (void *cls, size_t size, void *buf)
2406 struct MeshPeerInfo *peer_info;
2407 struct GNUNET_MESH_PeerControl *msg;
2408 struct GNUNET_PeerIdentity id;
2410 if (0 == size && NULL == buf)
2412 // TODO retry? cancel?
2416 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2417 peer_info = (struct MeshPeerInfo *) cls;
2418 msg = (struct GNUNET_MESH_PeerControl *) buf;
2419 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2420 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2421 // msg->tunnel_id = htonl(peer_info->t->tid);
2422 GNUNET_PEER_resolve (peer_info->id, &id);
2423 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2431 * Function to process paths received for a new peer addition. The recorded
2432 * paths form the initial tunnel, which can be optimized later.
2433 * Called on each result obtained for the DHT search.
2435 * @param cls closure
2436 * @param exp when will this value expire
2437 * @param key key of the result
2438 * @param get_path NULL-terminated array of pointers
2439 * to the peers on reverse GET path (or NULL if not recorded)
2440 * @param put_path NULL-terminated array of pointers
2441 * to the peers on the PUT path (or NULL if not recorded)
2442 * @param type type of the result
2443 * @param size number of bytes in data
2444 * @param data pointer to the result data
2449 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2450 const GNUNET_HashCode * key,
2451 const struct GNUNET_PeerIdentity *const *get_path,
2452 const struct GNUNET_PeerIdentity *const *put_path,
2453 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2455 struct MeshPathInfo *path_info = cls;
2456 struct MeshPeerPath *p;
2457 struct GNUNET_PeerIdentity pi;
2460 if (NULL == get_path || NULL == put_path)
2462 if (NULL == path_info->peer->path_head)
2464 // Find ourselves some alternate initial path to the destination: retry
2465 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2466 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2467 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2468 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2469 GNUNET_BLOCK_TYPE_TEST, /* type */
2470 &pi.hashPubKey, /*key to search */
2471 4, /* replication level */
2472 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2475 0, /* xquery bits */
2477 (void *) path_info);
2482 p = path_build_from_dht (get_path, put_path);
2483 path_add_to_peer (path_info->peer, p);
2484 for (i = 0; i < path_info->peer->ntunnels; i++)
2486 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2488 GNUNET_free (path_info);
2495 * Function to process paths received for a new peer addition. The recorded
2496 * paths form the initial tunnel, which can be optimized later.
2497 * Called on each result obtained for the DHT search.
2499 * @param cls closure
2500 * @param exp when will this value expire
2501 * @param key key of the result
2502 * @param get_path NULL-terminated array of pointers
2503 * to the peers on reverse GET path (or NULL if not recorded)
2504 * @param put_path NULL-terminated array of pointers
2505 * to the peers on the PUT path (or NULL if not recorded)
2506 * @param type type of the result
2507 * @param size number of bytes in data
2508 * @param data pointer to the result data
2511 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2512 const GNUNET_HashCode * key,
2513 const struct GNUNET_PeerIdentity *const *get_path,
2514 const struct GNUNET_PeerIdentity *const *put_path,
2515 enum GNUNET_BLOCK_Type type, size_t size,
2518 const struct GNUNET_PeerIdentity *pi = data;
2519 struct GNUNET_PeerIdentity id;
2520 struct MeshTunnel *t = cls;
2521 struct MeshPeerInfo *peer_info;
2522 struct MeshPathInfo *path_info;
2523 struct MeshPeerPath *p;
2526 if (size != sizeof (struct GNUNET_PeerIdentity))
2528 GNUNET_break_op (0);
2531 GNUNET_assert (NULL != t->client);
2532 GNUNET_DHT_get_stop (t->client->dht_get_type);
2533 t->client->dht_get_type = NULL;
2534 peer_info = peer_info_get (pi);
2535 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2536 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2538 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2539 NULL == peer_info->dhtget)
2541 /* we don't have a route to the peer, let's try a direct lookup */
2542 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2544 GNUNET_TIME_UNIT_FOREVER_REL,
2546 GNUNET_BLOCK_TYPE_TEST,
2549 /* key to look up */
2551 /* replication level */
2552 GNUNET_DHT_RO_RECORD_ROUTE,
2553 /* option to dht: record route */
2554 NULL, /* bloom filter */
2557 0, /* xquery bits */
2560 peer_info); /* closure */
2563 p = path_build_from_dht (get_path, put_path);
2564 path_add_to_peer (peer_info, p);
2565 tunnel_add_peer(t, peer_info);
2566 p = tunnel_get_path_to_peer(t, peer_info);
2567 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2569 path_info->peer = peer_info;
2570 path_info->path = p;
2572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2573 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2574 t->local_tid, p->length);
2575 for (i = 0; i < p->length; i++)
2577 GNUNET_PEER_resolve (p->peers[0], &id);
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2579 GNUNET_h2s_full (&id.hashPubKey));
2583 GNUNET_PEER_resolve (p->peers[1], &id);
2584 GNUNET_CORE_notify_transmit_ready (core_handle,
2590 GNUNET_TIME_UNIT_FOREVER_REL,
2594 sizeof (struct GNUNET_MESH_ManipulatePath)
2597 sizeof (struct GNUNET_PeerIdentity)),
2599 &send_core_create_path,
2601 path_info); /* cls */
2604 /******************************************************************************/
2605 /********************* MESH LOCAL HANDLES **************************/
2606 /******************************************************************************/
2610 * Handler for client disconnection
2612 * @param cls closure
2613 * @param client identification of the client; NULL
2614 * for the last call when the server is destroyed
2617 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2619 struct MeshClient *c;
2620 struct MeshClient *next;
2622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2628 if (c->handle != client && NULL != client)
2630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2635 if (NULL != c->tunnels)
2637 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2638 &tunnel_destroy_iterator,
2640 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2643 /* deregister clients applications */
2644 if (NULL != c->apps)
2646 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2647 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2649 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2650 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2652 GNUNET_SCHEDULER_cancel (announce_applications_task);
2653 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2655 if (NULL != c->types)
2656 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2657 if (NULL != c->dht_get_type)
2658 GNUNET_DHT_get_stop (c->dht_get_type);
2659 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2671 * Handler for new clients
2673 * @param cls closure
2674 * @param client identification of the client
2675 * @param message the actual message, which includes messages the client wants
2678 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2679 const struct GNUNET_MessageHeader *message)
2681 struct GNUNET_MESH_ClientConnect *cc_msg;
2682 struct MeshClient *c;
2683 GNUNET_MESH_ApplicationType *a;
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2691 /* Check data sanity */
2692 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2693 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2694 ntypes = ntohs (cc_msg->types);
2695 napps = ntohs (cc_msg->applications);
2697 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2700 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2704 /* Create new client structure */
2705 c = GNUNET_malloc (sizeof (struct MeshClient));
2707 c->id = next_client_id++;
2710 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2713 GNUNET_MESH_ApplicationType at;
2716 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2717 for (i = 0; i < napps; i++)
2720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2721 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2722 /* store in clients hashmap */
2723 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2724 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2725 /* store in global hashmap, for announcements */
2726 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2727 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2729 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2730 announce_applications_task =
2731 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2739 t = (uint16_t *) & a[napps];
2740 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2741 for (i = 0; i < ntypes; i++)
2744 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2746 /* store in clients hashmap */
2747 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2748 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2749 /* store in global hashmap */
2750 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2751 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2755 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2757 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2758 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2759 GNUNET_SERVER_notification_context_add (nc, client);
2761 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2769 * Handler for requests of new tunnels
2771 * @param cls closure
2772 * @param client identification of the client
2773 * @param message the actual message
2776 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2777 const struct GNUNET_MessageHeader *message)
2779 struct GNUNET_MESH_TunnelMessage *t_msg;
2780 struct MeshTunnel *t;
2781 struct MeshClient *c;
2782 GNUNET_HashCode hash;
2784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2786 /* Sanity check for client registration */
2787 if (NULL == (c = client_get (client)))
2790 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2797 /* Message sanity check */
2798 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2801 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2805 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2806 /* Sanity check for tunnel numbering */
2807 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2810 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2813 /* Sanity check for duplicate tunnel IDs */
2814 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2817 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2821 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2823 while (NULL != tunnel_get_by_pi (myid, next_tid))
2824 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2825 t->id.tid = next_tid++;
2827 t->local_tid = ntohl (t_msg->tunnel_id);
2829 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2831 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2833 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2834 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2837 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2841 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2843 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2844 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2847 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2850 t->paths = GNUNET_malloc (sizeof(struct MeshTunnelPath));
2851 t->paths->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
2853 t->paths->root = GNUNET_malloc(sizeof(struct MeshTunnelPathNode));
2854 t->paths->root->status = MESH_PEER_READY;
2855 t->paths->root->t = t;
2856 t->paths->root->peer = peer_info_get(&my_full_id);
2857 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: adding root node id %u\n",
2858 t->paths->root->peer->id);
2859 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: own id is %s\n",
2860 GNUNET_h2s_full(&my_full_id.hashPubKey));
2861 struct GNUNET_PeerIdentity id;
2862 GNUNET_PEER_resolve(t->paths->root->peer->id, &id);
2863 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: id of peer is %s\n",
2864 GNUNET_h2s_full(&id.hashPubKey));
2865 t->paths->me = t->paths->root;
2867 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2873 * Handler for requests of deleting tunnels
2875 * @param cls closure
2876 * @param client identification of the client
2877 * @param message the actual message
2880 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2881 const struct GNUNET_MessageHeader *message)
2883 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2884 struct MeshClient *c;
2885 struct MeshTunnel *t;
2886 MESH_TunnelNumber tid;
2887 GNUNET_HashCode hash;
2889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2891 /* Sanity check for client registration */
2892 if (NULL == (c = client_get (client)))
2895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2898 /* Message sanity check */
2899 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2902 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2908 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2910 /* Retrieve tunnel */
2911 tid = ntohl (tunnel_msg->tunnel_id);
2913 /* Remove from local id hashmap */
2914 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2915 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2916 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2918 /* Remove from global id hashmap */
2919 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2920 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2922 // notify_tunnel_destroy(t); FIXME
2924 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2930 * Handler for connection requests to new peers
2932 * @param cls closure
2933 * @param client identification of the client
2934 * @param message the actual message (PeerControl)
2939 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2940 const struct GNUNET_MessageHeader *message)
2942 struct GNUNET_MESH_PeerControl *peer_msg;
2943 struct MeshClient *c;
2944 struct MeshTunnel *t;
2945 MESH_TunnelNumber tid;
2946 struct MeshPeerInfo *peer_info;
2949 /* Sanity check for client registration */
2950 if (NULL == (c = client_get (client)))
2953 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2957 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2958 /* Sanity check for message size */
2959 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2962 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2966 /* Tunnel exists? */
2967 tid = ntohl (peer_msg->tunnel_id);
2968 t = tunnel_get_by_local_id (c, tid);
2972 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2976 /* Does client own tunnel? */
2977 if (t->client->handle != client)
2980 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2985 peer_info = peer_info_get (&peer_msg->peer);
2987 /* Start DHT search if needed FIXME: if not already connected */
2988 if (NULL == peer_info->dhtget)
2990 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 */
2991 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2994 0, /* xquery bits */
2996 (void *) peer_info);
2999 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3005 * Handler for disconnection requests of peers in a tunnel
3007 * @param cls closure
3008 * @param client identification of the client
3009 * @param message the actual message (PeerControl)
3012 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3013 const struct GNUNET_MessageHeader *message)
3015 struct GNUNET_MESH_PeerControl *peer_msg;
3016 struct MeshClient *c;
3017 struct MeshTunnel *t;
3018 MESH_TunnelNumber tid;
3020 /* Sanity check for client registration */
3021 if (NULL == (c = client_get (client)))
3024 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3027 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3028 /* Sanity check for message size */
3029 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3032 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3036 /* Tunnel exists? */
3037 tid = ntohl (peer_msg->tunnel_id);
3038 t = tunnel_get_by_local_id (c, tid);
3042 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3046 /* Does client own tunnel? */
3047 if (t->client->handle != client)
3050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3054 /* Ok, delete peer from tunnel */
3055 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3056 &peer_msg->peer.hashPubKey);
3058 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3064 * Handler for connection requests to new peers by type
3066 * @param cls closure
3067 * @param client identification of the client
3068 * @param message the actual message (ConnectPeerByType)
3071 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3072 const struct GNUNET_MessageHeader *message)
3074 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3075 struct MeshClient *c;
3076 struct MeshTunnel *t;
3077 GNUNET_HashCode hash;
3078 GNUNET_MESH_ApplicationType type;
3079 MESH_TunnelNumber tid;
3081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3082 /* Sanity check for client registration */
3083 if (NULL == (c = client_get (client)))
3086 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3090 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3091 /* Sanity check for message size */
3092 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3093 ntohs (connect_msg->header.size))
3096 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3100 /* Tunnel exists? */
3101 tid = ntohl (connect_msg->tunnel_id);
3102 t = tunnel_get_by_local_id (c, tid);
3106 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3110 /* Does client own tunnel? */
3111 if (t->client->handle != client)
3114 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3118 /* Do WE have the service? */
3119 type = ntohl (connect_msg->type);
3120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
3121 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3122 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3125 /* Yes! Fast forward, add ourselves to the tunnel and send the
3126 * good news to the client
3128 struct GNUNET_MESH_PeerControl pc;
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3131 pc.peer = my_full_id;
3132 GNUNET_CONTAINER_multihashmap_put (t->peers, &pc.peer.hashPubKey,
3133 peer_info_get (&pc.peer),
3134 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3135 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3136 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
3137 pc.tunnel_id = htonl (t->local_tid);
3138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3139 GNUNET_SERVER_notification_context_unicast (nc, /* context */
3141 &pc.header, /* msg */
3142 GNUNET_NO); /* can drop? */
3143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3144 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3147 /* Ok, lets find a peer offering the service */
3148 if (c->dht_get_type)
3150 GNUNET_DHT_get_stop (c->dht_get_type);
3152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3153 GNUNET_h2s_full (&hash));
3155 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
3156 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
3157 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
3158 &dht_get_type_handler, t);
3160 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3166 * Handler for client traffic directed to one peer
3168 * @param cls closure
3169 * @param client identification of the client
3170 * @param message the actual message
3173 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3174 const struct GNUNET_MessageHeader *message)
3176 struct MeshClient *c;
3177 struct MeshTunnel *t;
3178 struct MeshPeerInfo *pi;
3179 struct GNUNET_MESH_Unicast *data_msg;
3180 struct GNUNET_PeerIdentity next_hop;
3181 struct MeshDataDescriptor *info;
3182 MESH_TunnelNumber tid;
3185 /* Sanity check for client registration */
3186 if (NULL == (c = client_get (client)))
3189 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3192 data_msg = (struct GNUNET_MESH_Unicast *) message;
3193 /* Sanity check for message size */
3194 if (sizeof (struct GNUNET_MESH_Unicast) +
3195 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
3198 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3202 /* Tunnel exists? */
3203 tid = ntohl (data_msg->tid);
3204 t = tunnel_get_by_local_id (c, tid);
3208 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3212 /* Is it a local tunnel? Then, does client own the tunnel? */
3213 if (t->client->handle != NULL && t->client->handle != client)
3216 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3220 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3221 &data_msg->destination.hashPubKey);
3222 /* Is the selected peer in the tunnel? */
3226 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3231 struct GNUNET_MESH_Unicast copy;
3233 /* Work around const limitation */
3234 memcpy (©, data_msg, sizeof (struct GNUNET_MESH_Unicast));
3235 copy.oid = my_full_id;
3236 copy.tid = htonl (t->id.tid);
3237 handle_mesh_data_unicast (NULL, &my_full_id, ©.header, NULL);
3240 GNUNET_PEER_resolve (path_get_first_hop (t, pi)->id, &next_hop);
3241 data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
3242 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
3243 memcpy (&info[1], &data_msg[1], data_size);
3244 info->destination = pi->id;
3245 info->origin = &t->id;
3246 info->size = data_size;
3247 info->client = client;
3248 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
3249 GNUNET_TIME_UNIT_FOREVER_REL, &next_hop,
3250 /* FIXME re-check types */
3252 sizeof (struct GNUNET_MESH_Unicast),
3253 &send_core_data_unicast, info);
3258 * Handler for client traffic directed to all peers in a tunnel
3260 * @param cls closure
3261 * @param client identification of the client
3262 * @param message the actual message
3265 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3266 const struct GNUNET_MessageHeader *message)
3268 struct MeshClient *c;
3269 struct MeshTunnel *t;
3270 struct GNUNET_MESH_Multicast *data_msg;
3271 MESH_TunnelNumber tid;
3273 /* Sanity check for client registration */
3274 if (NULL == (c = client_get (client)))
3277 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3280 data_msg = (struct GNUNET_MESH_Multicast *) message;
3281 /* Sanity check for message size */
3282 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
3285 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3289 /* Tunnel exists? */
3290 tid = ntohl (data_msg->tid);
3291 t = tunnel_get_by_local_id (c, tid);
3295 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3299 /* Does client own tunnel? */
3300 if (t->client->handle != client)
3303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3309 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3314 * Functions to handle messages from clients
3316 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3317 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3318 {&handle_local_tunnel_create, NULL,
3319 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3320 sizeof (struct GNUNET_MESH_TunnelMessage)},
3321 {&handle_local_tunnel_destroy, NULL,
3322 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3323 sizeof (struct GNUNET_MESH_TunnelMessage)},
3324 {&handle_local_connect_add, NULL,
3325 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3326 sizeof (struct GNUNET_MESH_PeerControl)},
3327 {&handle_local_connect_del, NULL,
3328 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3329 sizeof (struct GNUNET_MESH_PeerControl)},
3330 {&handle_local_connect_by_type, NULL,
3331 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3332 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3333 {&handle_local_unicast, NULL,
3334 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3335 {&handle_local_unicast, NULL,
3336 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3337 {&handle_local_multicast, NULL,
3338 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3344 * To be called on core init/fail.
3346 * @param cls service closure
3347 * @param server handle to the server for this service
3348 * @param identity the public identity of this peer
3349 * @param publicKey the public key of this peer
3352 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3353 const struct GNUNET_PeerIdentity *identity,
3354 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
3356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3357 core_handle = server;
3358 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3360 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3361 GNUNET_SCHEDULER_shutdown();
3367 * Method called whenever a given peer connects.
3369 * @param cls closure
3370 * @param peer peer identity this notification is about
3371 * @param atsi performance data for the connection
3374 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3375 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3377 // GNUNET_PEER_Id pid;
3378 struct MeshPeerInfo *peer_info;
3379 struct MeshPeerPath *path;
3381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3382 peer_info = peer_info_get (peer);
3383 if (myid == peer_info->id)
3385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3387 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
3389 path->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id) * 2);
3390 path->peers[0] = myid;
3391 path->peers[1] = peer_info->id;
3392 path_add_to_peer (peer_info, path);
3397 * Method called whenever a peer disconnects.
3399 * @param cls closure
3400 * @param peer peer identity this notification is about
3403 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3405 struct MeshPeerInfo *pi;
3408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3409 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3415 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3417 if (pi->core_transmit[i])
3419 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3420 /* TODO: notify that tranmission has failed */
3421 GNUNET_free (pi->infos[i]);
3424 path_remove_from_peer (pi, pi->id, myid);
3427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3433 /******************************************************************************/
3434 /************************ MAIN FUNCTIONS ****************************/
3435 /******************************************************************************/
3438 * Task run during shutdown.
3444 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3446 struct MeshClient *c;
3448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3449 if (core_handle != NULL)
3451 GNUNET_CORE_disconnect (core_handle);
3454 if (dht_handle != NULL)
3456 for (c = clients; NULL != c; c = c->next)
3457 if (NULL != c->dht_get_type)
3458 GNUNET_DHT_get_stop (c->dht_get_type);
3459 GNUNET_DHT_disconnect (dht_handle);
3464 GNUNET_SERVER_notification_context_destroy (nc);
3467 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3469 GNUNET_SCHEDULER_cancel (announce_id_task);
3470 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3476 * Process mesh requests.
3478 * @param cls closure
3479 * @param server the initialized server
3480 * @param c configuration to use
3483 run (void *cls, struct GNUNET_SERVER_Handle *server,
3484 const struct GNUNET_CONFIGURATION_Handle *c)
3488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3489 server_handle = server;
3490 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3491 CORE_QUEUE_SIZE, /* queue size */
3492 NULL, /* Closure passed to MESH functions */
3493 &core_init, /* Call core_init once connected */
3494 &core_connect, /* Handle connects */
3495 &core_disconnect, /* remove peers on disconnects */
3496 NULL, /* Do we care about "status" updates? */
3497 NULL, /* Don't notify about all incoming messages */
3498 GNUNET_NO, /* For header only in notification */
3499 NULL, /* Don't notify about all outbound messages */
3500 GNUNET_NO, /* For header-only out notification */
3501 core_handlers); /* Register these handlers */
3502 if (core_handle == NULL)
3505 GNUNET_SCHEDULER_shutdown ();
3510 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3515 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3516 GNUNET_SCHEDULER_shutdown ();
3519 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3520 GNUNET_free (keyfile);
3521 if (my_private_key == NULL)
3523 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3524 _("Mesh service could not access hostkey. Exiting.\n"));
3525 GNUNET_SCHEDULER_shutdown ();
3528 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3529 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3530 &my_full_id.hashPubKey);
3531 myid = GNUNET_PEER_intern (&my_full_id);
3533 dht_handle = GNUNET_DHT_connect (c, 64);
3534 if (dht_handle == NULL)
3536 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3537 Running without DHT has a severe\
3538 impact in MESH capabilities.\n\
3539 Plase check your configuretion and enable DHT.\n");
3545 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3546 peers = GNUNET_CONTAINER_multihashmap_create (32);
3547 applications = GNUNET_CONTAINER_multihashmap_create (32);
3548 types = GNUNET_CONTAINER_multihashmap_create (32);
3550 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3551 nc = GNUNET_SERVER_notification_context_create (server_handle,
3553 GNUNET_SERVER_disconnect_notify (server_handle,
3554 &handle_local_client_disconnect,
3559 clients_tail = NULL;
3564 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3565 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3567 /* Create a peer_info for the local peer */
3568 peer_info_get(&my_full_id);
3570 /* Scheduled the task to clean up when shutdown is called */
3571 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3578 * The main function for the mesh service.
3580 * @param argc number of arguments from the command line
3581 * @param argv command line arguments
3582 * @return 0 ok, 1 on error
3585 main (int argc, char *const *argv)
3590 fprintf (stderr, "main ()\n");
3592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3595 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3599 fprintf (stderr, "main () END\n");