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;
556 * Tunnel ID for the next created tunnel (global tunnel number)
558 static MESH_TunnelNumber next_tid;
561 * All application types provided by this peer
563 static struct GNUNET_CONTAINER_MultiHashMap *applications;
566 * All message types clients of this peer are interested in
568 static struct GNUNET_CONTAINER_MultiHashMap *types;
571 * Task to periodically announce provided applications
573 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
576 * Task to periodically announce itself in the network
578 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
581 unsigned int next_client_id;
585 /******************************************************************************/
586 /************************ PERIODIC FUNCTIONS ****************************/
587 /******************************************************************************/
590 * Announce iterator over for each application provided by the peer
593 * @param key current key code
594 * @param value value in the hash map
595 * @return GNUNET_YES if we should continue to
600 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
602 /* FIXME are hashes in multihash map equal on all aquitectures? */
603 GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
604 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
605 (const char *) &my_full_id,
607 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
608 &mesh_debug, "DHT_put for app completed");
610 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
612 APP_ANNOUNCE_TIME, NULL, NULL);
619 * Periodically announce what applications are provided by local clients
622 * @param tc task context
625 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
627 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
629 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
633 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
635 announce_applications_task =
636 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
644 * Periodically announce self id in the DHT
647 * @param tc task context
650 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
652 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
654 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
658 * - Set data expiration in function of X
661 GNUNET_DHT_put (dht_handle, /* DHT handle */
662 &my_full_id.hashPubKey, /* Key to use */
663 10U, /* Replication level */
664 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
665 GNUNET_BLOCK_TYPE_TEST, /* Block type */
666 0, /* Size of the data */
667 NULL, /* Data itself */
668 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
669 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
671 &mesh_debug, "DHT_put for id completed");
673 NULL, /* Continuation */
674 NULL); /* Continuation closure */
677 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
682 * Send keepalive packets for a peer
690 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
692 struct MeshTunnel *t = cls;
694 // struct GNUNET_PeerIdentity id;
696 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
700 /* FIXME implement multicast keepalive. Just an empty multicast packet? */
701 // GNUNET_PEER_resolve (path_get_first_hop (path->t, path->peer)->id, &id);
702 // GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
703 // GNUNET_TIME_UNIT_FOREVER_REL, &id,
704 // sizeof (struct GNUNET_MESH_ManipulatePath)
706 // (path->path->length *
707 // sizeof (struct GNUNET_PeerIdentity)),
708 // &send_core_create_path,
710 t->path_refresh_task =
711 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
716 /******************************************************************************/
717 /****************** GENERAL HELPER FUNCTIONS ************************/
718 /******************************************************************************/
721 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
722 * and insert it in the appropiate structures if the peer is not known yet.
724 * @param peer Identity of the peer
726 * @return Existing or newly created peer info
728 static struct MeshPeerInfo *
729 peer_info_get (const struct GNUNET_PeerIdentity *peer)
731 struct MeshPeerInfo *peer_info;
733 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
734 if (NULL == peer_info)
737 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
738 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
739 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
740 peer_info->id = GNUNET_PEER_intern (peer);
749 * Destroy the peer_info and free any allocated resources linked to it
750 * @param t tunnel the path belongs to
751 * @param pi the peer_info to destroy
752 * @return GNUNET_OK on success
755 peer_info_destroy (struct MeshPeerInfo *pi)
757 GNUNET_HashCode hash;
758 struct GNUNET_PeerIdentity id;
760 GNUNET_PEER_resolve (pi->id, &id);
761 GNUNET_PEER_change_rc (pi->id, -1);
762 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
764 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
765 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
773 * Destroy the path and free any allocated resources linked to it
775 * @param p the path to destroy
777 * @return GNUNET_OK on success
780 path_destroy (struct MeshPeerPath *p)
782 GNUNET_PEER_decrement_rcs (p->peers, p->length);
783 GNUNET_free (p->peers);
792 * @param p the path to invert
795 path_invert (struct MeshPeerPath *path)
800 for (i = 0; i < path->length / 2; i++)
802 aux = path->peers[i];
803 path->peers[i] = path->peers[path->length - i - 1];
804 path->peers[path->length - i - 1] = aux;
810 * Find the first peer whom to send a packet to go down this path
812 * @param t The tunnel to use
813 * @param peer The peerinfo of the peer we are trying to reach
815 * @return peerinfo of the peer who is the first hop in the tunnel
818 static struct MeshPeerInfo *
819 path_get_first_hop (struct MeshTunnel *t, struct MeshPeerInfo *peer)
821 struct GNUNET_PeerIdentity id;
823 GNUNET_PEER_resolve (peer->id, &id);
824 return GNUNET_CONTAINER_multihashmap_get (t->paths->first_hops,
830 * Get the length of a path
832 * @param path The path to measure, with the local peer at any point of it
834 * @return Number of hops to reach destination
835 * UINT_MAX in case the peer is not in the path
838 path_get_length (struct MeshPeerPath *path)
844 for (i = 0; i < path->length; i++)
846 if (path->peers[i] == myid)
848 return path->length - i;
856 * Get the cost of the path relative to the already built tunnel tree
858 * @param t The tunnel to which compare
859 * @param path The individual path to reach a peer
861 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
864 * TODO: remove dummy implementation, look into the tunnel tree
867 path_get_cost (struct MeshTunnel *t, struct MeshPeerPath *path)
869 return path_get_length (path);
874 * Add the path to the peer and update the path used to reach it in case this
877 * @param peer_info Destination peer to add the path to.
878 * @param path New path to add. Last peer must be the peer in arg 1.
880 * TODO: trim the part from origin to us? Add it as path to origin?
883 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
886 struct MeshPeerPath *aux;
888 if (NULL == peer_info || NULL == path)
894 l = path_get_length (path);
896 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
898 if (path_get_length (aux) > l)
900 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
901 peer_info->path_tail, aux, path);
904 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
911 * Notify a tunnel that a connection has broken that affects at least
914 * @param t Tunnel affected
915 * @param peer Peer that (at least) has been affected by the disconnection
916 * @param p1 Peer that got disconnected from p2
917 * @param p2 Peer that got disconnected from p1
920 tunnel_notify_connection_broken (struct MeshTunnel *t,
921 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
926 * Remove all paths that rely on a direct connection between p1 and p2
927 * from the peer itself and notify all tunnels about it.
929 * @param pi PeerInfo of affected peer
930 * @param p1 GNUNET_PEER_Id of one peer.
931 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
935 path_remove_from_peer (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
938 struct MeshPeerPath *p;
939 struct MeshPeerPath *aux;
940 unsigned int destroyed;
948 for (i = 0; i < (p->length - 1); i++)
950 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
951 (p->peers[i] == p2 && p->peers[i + 1] == p1))
963 for (i = 0; i < peer->ntunnels; i++)
965 tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
971 * Add the path to the origin peer and update the path used to reach it in case
972 * this is the shortest.
973 * The path is given in peer_info -> destination, therefore we turn the path
976 * @param peer_info Peer to add the path to, being the origin of the path.
977 * @param path New path to add after being inversed.
980 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
983 path_add_to_peer (peer_info, path);
988 * Build a PeerPath from the paths returned from the DHT, reversing the paths
989 * to obtain a local peer -> destination path and interning the peer ids.
991 * @param get_path NULL-terminated array of pointers
992 * to the peers on reverse GET path (or NULL if not recorded)
993 * @param put_path NULL-terminated array of pointers
994 * to the peers on the PUT path (or NULL if not recorded)
996 * @return Newly allocated and created path
998 static struct MeshPeerPath *
999 path_build_from_dht (const struct GNUNET_PeerIdentity *const *get_path,
1000 const struct GNUNET_PeerIdentity *const *put_path)
1002 struct MeshPeerPath *p;
1005 p = GNUNET_malloc (sizeof (struct MeshPeerPath));
1006 for (i = 0; get_path[i] != NULL; i++) ;
1007 for (i--; i >= 0; i--)
1010 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1011 p->peers[p->length] = GNUNET_PEER_intern (get_path[i]);
1014 for (i = 0; put_path[i] != NULL; i++) ;
1015 for (i--; i >= 0; i--)
1018 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
1019 p->peers[p->length] = GNUNET_PEER_intern (put_path[i]);
1027 * Check if client has registered with the service and has not disconnected
1029 * @param client the client to check
1031 * @return non-NULL if client exists in the global DLL
1033 static struct MeshClient *
1034 client_get (struct GNUNET_SERVER_Client *client)
1036 struct MeshClient *c;
1041 if (c->handle == client)
1050 * Checks if a given client has subscribed to certain message type
1052 * @param message_type Type of message to check
1053 * @param c Client to check
1055 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1060 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1064 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1065 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1070 * Search for a tunnel among the tunnels for a client
1072 * @param c the client whose tunnels to search in
1073 * @param tid the local id of the tunnel
1075 * @return tunnel handler, NULL if doesn't exist
1077 static struct MeshTunnel *
1078 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1080 GNUNET_HashCode hash;
1082 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1083 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1088 * Search for a tunnel by global ID using PEER_ID
1090 * @param pi owner of the tunnel
1091 * @param tid global tunnel number
1093 * @return tunnel handler, NULL if doesn't exist
1095 static struct MeshTunnel *
1096 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1098 struct MESH_TunnelID id;
1099 GNUNET_HashCode hash;
1104 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1105 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1111 * Search for a tunnel by global ID using full PeerIdentities
1113 * @param oid owner of the tunnel
1114 * @param tid global tunnel number
1116 * @return tunnel handler, NULL if doesn't exist
1118 static struct MeshTunnel *
1119 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1121 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1126 * Recursively find the given peer in the tree.
1128 * @param t Tunnel where to add the new path.
1129 * @param p Path to look for.
1131 * @return Pointer to the node of the peer. NULL if not found.
1133 static struct MeshTunnelPathNode *
1134 tunnel_find_peer (struct MeshTunnelPathNode *root, struct MeshPeerInfo *peer)
1136 struct MeshTunnelPathNode *n;
1139 if (root->peer == peer)
1141 for (i = 0; i < root->nchildren; i++)
1143 n = tunnel_find_peer (&root->children[i], peer);
1152 * Recusively mark peer and children as disconnected, notify client
1154 * @param parent Node to be clean, potentially with children
1157 tunnel_mark_peers_disconnected (struct MeshTunnelPathNode *parent)
1159 struct GNUNET_MESH_PeerControl msg;
1162 parent->status = MESH_PEER_RECONNECTING;
1163 for (i = 0; i < parent->nchildren; i++)
1165 tunnel_mark_peers_disconnected (&parent->children[i]);
1167 if (NULL == parent->t->client)
1169 msg.header.size = htons (sizeof (msg));
1170 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1171 msg.tunnel_id = htonl (parent->t->local_tid);
1172 GNUNET_PEER_resolve (parent->peer->id, &msg.peer);
1173 GNUNET_SERVER_notification_context_unicast (nc, parent->t->client->handle,
1174 &msg.header, GNUNET_NO);
1178 * Delete the current path to the peer, including all now unused relays.
1180 * @param t Tunnel where to add the new path.
1181 * @param p Path to be integrated.
1183 * @return pointer to the pathless node, NULL on error
1185 * TODO: notify peers of deletion
1187 static struct MeshTunnelPathNode *
1188 tunnel_del_path (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1190 struct MeshTunnelPathNode *parent;
1191 struct MeshTunnelPathNode *node;
1192 struct MeshTunnelPathNode *n;
1194 node = n = tunnel_find_peer (t->paths->me, peer);
1199 while (NULL != parent && MESH_PEER_RELAY == parent->status &&
1200 1 == parent->nchildren)
1203 GNUNET_free (parent->children);
1204 parent = parent->parent;
1208 *n = parent->children[parent->nchildren - 1];
1209 parent->nchildren--;
1210 parent->children = GNUNET_realloc (parent->children, parent->nchildren);
1212 tunnel_mark_peers_disconnected (node);
1219 * Return a newly allocated individual path to reach a peer from the local peer,
1220 * according to the path tree of some tunnel.
1222 * @param t Tunnel from which to read the path tree
1223 * @param peer_info Destination peer to whom we want a path
1225 * @return A newly allocated individual path to reach the destination peer.
1226 * Path must be destroyed afterwards.
1228 static struct MeshPeerPath *
1229 tunnel_get_path_to_peer(struct MeshTunnel *t, struct MeshPeerInfo *peer_info)
1231 struct MeshTunnelPathNode *n;
1232 struct MeshPeerPath *p;
1234 n = tunnel_find_peer(t->paths->me, peer_info);
1235 p = GNUNET_malloc(sizeof(struct MeshPeerPath));
1237 /* Building the path (inverted!) */
1238 while (n->peer->id != myid)
1240 GNUNET_array_append(p->peers, p->length, n->peer->id);
1241 GNUNET_PEER_change_rc(n->peer->id, 1);
1243 GNUNET_assert(NULL != n);
1245 GNUNET_array_append(p->peers, p->length, myid);
1246 GNUNET_PEER_change_rc(myid, 1);
1255 * Integrate a stand alone path into the tunnel tree.
1257 * @param t Tunnel where to add the new path.
1258 * @param p Path to be integrated.
1260 * @return GNUNET_OK in case of success.
1261 * GNUNET_SYSERR in case of error.
1264 * - go backwards on path looking for each peer in the present tree
1267 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1269 struct MeshTunnelPathNode *parent;
1270 struct MeshTunnelPathNode *oldnode;
1271 struct MeshTunnelPathNode *n;
1272 struct GNUNET_PeerIdentity id;
1273 struct GNUNET_PeerIdentity hop;
1279 if (n->peer->id != p->peers[0])
1282 return GNUNET_SYSERR;
1284 /* Ignore return value, if not found it's ok. */
1285 GNUNET_PEER_resolve (p->peers[p->length - 1], &id);
1286 oldnode = tunnel_del_path (t, peer_info_get (&id));
1287 /* Look for the first node that is not already present in the tree
1289 * Assuming that the tree is somewhat balanced, O(log n * log N).
1290 * - Length of the path is expected to be log N (size of whole network).
1291 * - Each level of the tree is expected to have log n children (size of tree).
1293 for (i = 1, me = -1; i < p->length; i++)
1296 if (p->peers[i] == myid)
1298 for (j = 0; j < n->nchildren; j++)
1300 if (n->children[j].peer->id == p->peers[i])
1302 n = &n->children[j];
1306 /* If we couldn't find a child equal to path[i], we have reached the end
1307 * of the common path. */
1313 /* New path deviates from tree before reaching us. What happened? */
1315 return GNUNET_SYSERR;
1317 /* Add the rest of the path as a branch from parent. */
1318 while (i < p->length)
1320 parent->nchildren++;
1321 parent->children = GNUNET_realloc (parent->children, parent->nchildren);
1322 n = &parent->children[parent->nchildren - 1];
1323 if (i == p->length - 1)
1325 if (NULL != oldnode)
1327 /* Assignation and free can be misleading, using explicit mempcy */
1328 memcpy (n, oldnode, sizeof (struct MeshTunnelPathNode));
1329 GNUNET_free (oldnode);
1335 n->status = MESH_PEER_RELAY;
1336 GNUNET_PEER_resolve (p->peers[i], &id);
1337 n->peer = peer_info_get (&id);
1344 /* Add info about first hop into hashmap. */
1345 if (me < p->length - 1)
1347 GNUNET_PEER_resolve (p->peers[p->length - 1], &id);
1348 GNUNET_PEER_resolve (p->peers[me + 1], &hop);
1349 GNUNET_CONTAINER_multihashmap_put (t->paths->first_hops, &id.hashPubKey,
1350 peer_info_get (&hop),
1351 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1358 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1359 * needed rescources.
1361 * @param t Tunnel we want to add a new peer to
1362 * @param peer PeerInfo of the peer being added
1366 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1368 struct MeshPeerPath *p;
1369 struct MeshPeerPath *best_p;
1370 unsigned int best_cost;
1373 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1374 if (NULL == (p = peer->path_head))
1378 best_cost = UINT_MAX;
1381 if ((cost = path_get_cost (t, p)) < best_cost)
1388 tunnel_add_path (t, best_p);
1389 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1390 t->path_refresh_task =
1391 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
1396 * Notify a tunnel that a connection has broken that affects at least
1397 * some of its peers.
1399 * @param t Tunnel affected
1400 * @param peer Peer that (at least) has been affected by the disconnection
1401 * @param p1 Peer that got disconnected from p2
1402 * @param p2 Peer that got disconnected from p1
1407 tunnel_notify_connection_broken (struct MeshTunnel *t,
1408 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1415 * Destroy the tunnel and free any allocated resources linked to it
1417 * @param t the tunnel to destroy
1419 * @return GNUNET_OK on success
1422 tunnel_destroy (struct MeshTunnel *t)
1424 struct MeshClient *c;
1425 struct MeshQueue *q;
1426 struct MeshQueue *qn;
1427 GNUNET_HashCode hash;
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1439 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1440 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1445 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1446 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1450 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1454 if (NULL != q->data)
1455 GNUNET_free (q->data);
1459 /* TODO cancel core transmit ready in case it was active */
1467 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1468 * client when the client disconnects.
1470 * @param cls closure (client that is disconnecting)
1471 * @param key the hash of the local tunnel id (used to access the hashmap)
1472 * @param value the value stored at the key (tunnel to destroy)
1474 * @return GNUNET_OK on success
1477 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: D %p\n", value);
1482 r = tunnel_destroy ((struct MeshTunnel *) value);
1487 /******************************************************************************/
1488 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1489 /******************************************************************************/
1493 * Function called to notify a client about the socket
1494 * being ready to queue more data. "buf" will be
1495 * NULL and "size" zero if the socket was closed for
1496 * writing in the meantime.
1498 * @param cls closure
1499 * @param size number of bytes available in buf
1500 * @param buf where the callee should write the message
1501 * @return number of bytes written to buf
1504 send_core_create_path (void *cls, size_t size, void *buf)
1506 struct MeshPathInfo *info = cls;
1507 struct GNUNET_MESH_ManipulatePath *msg;
1508 struct GNUNET_PeerIdentity *peer_ptr;
1509 struct GNUNET_PeerIdentity id;
1510 struct MeshPeerInfo *peer = info->peer;
1511 struct MeshTunnel *t = info->t;
1512 struct MeshPeerPath *p = info->path;
1517 sizeof (struct GNUNET_MESH_ManipulatePath) +
1518 p->length * sizeof (struct GNUNET_PeerIdentity);
1520 if (size < size_needed || NULL == buf)
1522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
1523 GNUNET_PEER_resolve (path_get_first_hop (t, peer)->id, &id);
1524 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1525 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1526 size_needed, &send_core_create_path,
1531 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1532 msg->header.size = htons (size_needed);
1533 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1534 msg->tid = ntohl (t->id.tid);
1536 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1537 for (i = 0; i < p->length; i++)
1539 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1551 * Function called to notify a client about the socket
1552 * being ready to queue more data. "buf" will be
1553 * NULL and "size" zero if the socket was closed for
1554 * writing in the meantime.
1556 * @param cls closure (MeshDataDescriptor with all info to build packet)
1557 * @param size number of bytes available in buf
1558 * @param buf where the callee should write the message
1559 * @return number of bytes written to buf
1562 send_core_data_to_origin (void *cls, size_t size, void *buf)
1564 struct MeshDataDescriptor *info = cls;
1565 struct GNUNET_MESH_ToOrigin *msg = buf;
1568 GNUNET_assert (NULL != info);
1569 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1570 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1572 if (total_size > size)
1574 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1575 "not enough buffer to send data to origin\n");
1578 msg->header.size = htons (total_size);
1579 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1580 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1581 msg->tid = htonl (info->origin->tid);
1582 if (0 != info->size)
1584 memcpy (&msg[1], &info[1], info->size);
1586 if (NULL != info->client)
1588 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1596 * Function called to notify a client about the socket
1597 * being ready to queue more data. "buf" will be
1598 * NULL and "size" zero if the socket was closed for
1599 * writing in the meantime.
1601 * @param cls closure (data itself)
1602 * @param size number of bytes available in buf
1603 * @param buf where the callee should write the message
1604 * @return number of bytes written to buf
1607 send_core_data_unicast (void *cls, size_t size, void *buf)
1609 struct MeshDataDescriptor *info = cls;
1610 struct GNUNET_MESH_Unicast *msg = buf;
1613 GNUNET_assert (NULL != info);
1614 total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
1615 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1617 if (total_size > size)
1619 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1620 "not enough buffer to send data to peer\n");
1623 msg->header.size = htons (total_size);
1624 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1625 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1626 GNUNET_PEER_resolve (info->destination, &msg->destination);
1627 msg->tid = htonl (info->origin->tid);
1628 if (0 != info->size)
1630 memcpy (&msg[1], &info[1], info->size);
1632 if (NULL != info->client)
1634 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1642 * Function called to notify a client about the socket
1643 * being ready to queue more data. "buf" will be
1644 * NULL and "size" zero if the socket was closed for
1645 * writing in the meantime.
1647 * @param cls closure (data itself)
1648 * @param size number of bytes available in buf
1649 * @param buf where the callee should write the message
1650 * @return number of bytes written to buf
1653 send_core_data_multicast (void *cls, size_t size, void *buf)
1655 struct MeshDataDescriptor *info = cls;
1656 struct GNUNET_MESH_Multicast *msg = buf;
1659 GNUNET_assert (NULL != info);
1660 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1661 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1665 info->peer->core_transmit[info->handler_n] = NULL;
1667 if (total_size > size)
1669 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1670 "not enough buffer to send data futher\n");
1673 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1674 msg->header.size = htons (total_size);
1675 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1676 msg->tid = htonl (info->origin->tid);
1677 memcpy (&msg[1], &info[1], total_size);
1678 if (0 == --info->copies)
1680 if (NULL != info->client)
1682 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1691 * Function called to notify a client about the socket
1692 * being ready to queue more data. "buf" will be
1693 * NULL and "size" zero if the socket was closed for
1694 * writing in the meantime.
1696 * @param cls closure (MeshDataDescriptor)
1697 * @param size number of bytes available in buf
1698 * @param buf where the callee should write the message
1699 * @return number of bytes written to buf
1702 send_core_path_ack (void *cls, size_t size, void *buf)
1704 struct MeshDataDescriptor *info = cls;
1705 struct GNUNET_MESH_PathACK *msg = buf;
1707 GNUNET_assert (NULL != info);
1710 info->peer->core_transmit[info->handler_n] = NULL;
1712 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1717 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1718 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1719 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1720 msg->tid = htonl (info->origin->tid);
1721 msg->peer_id = my_full_id;
1723 /* TODO add signature */
1725 return sizeof (struct GNUNET_MESH_PathACK);
1730 * Function called to notify a client about the socket
1731 * being ready to queue more data. "buf" will be
1732 * NULL and "size" zero if the socket was closed for
1733 * writing in the meantime.
1735 * @param cls closure (data itself)
1736 * @param size number of bytes available in buf
1737 * @param buf where the callee should write the message
1738 * @return number of bytes written to buf
1741 send_core_data_raw (void *cls, size_t size, void *buf)
1743 struct GNUNET_MessageHeader *msg = cls;
1746 GNUNET_assert (NULL != msg);
1747 total_size = ntohs (msg->size);
1749 if (total_size > size)
1754 memcpy (buf, msg, total_size);
1762 * Send another peer a notification to destroy a tunnel
1763 * @param cls The tunnel to destroy
1764 * @param size Size in the buffer
1765 * @param buf Memory where to put the data to transmit
1766 * @return Size of data put in buffer
1769 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1771 struct MeshTunnel *t = cls;
1772 struct MeshClient *c;
1773 struct GNUNET_MESH_TunnelMessage *msg;
1777 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1778 /*FIXME*/ msg->header.size =
1779 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1780 msg->tunnel_id = htonl (t->id.tid);
1782 tunnel_destroy (c, t);
1783 return sizeof (struct GNUNET_MESH_TunnelMessage);
1789 * Send the message to all clients that have subscribed to its type
1791 * @param msg Pointer to the message itself
1792 * @return number of clients this message was sent to
1795 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1797 struct MeshClient *c;
1801 type = ntohs (msg->type);
1802 for (count = 0, c = clients; c != NULL; c = c->next)
1804 if (client_is_subscribed (type, c))
1807 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1816 * Iterator over hash map peer entries collect all neighbors who to resend the
1819 * @param cls closure (**GNUNET_PEER_Id to store hops to send packet)
1820 * @param key current key code (peer id hash)
1821 * @param value value in the hash map (peer_info)
1822 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
1825 iterate_collect_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
1827 struct MeshPeerInfo *peer_info = value;
1828 struct MeshPathInfo *neighbors = cls;
1831 if (peer_info->id == myid)
1835 peer_info = path_get_first_hop (neighbors->t, peer_info);
1836 for (i = 0; i < neighbors->path->length; i++)
1838 if (neighbors->path->peers[i] == peer_info->id)
1841 GNUNET_array_append (neighbors->path->peers, neighbors->path->length,
1848 /******************************************************************************/
1849 /******************** MESH NETWORK HANDLERS **************************/
1850 /******************************************************************************/
1854 * Core handler for path creation
1855 * struct GNUNET_CORE_MessageHandler
1857 * @param cls closure
1858 * @param message message
1859 * @param peer peer identity this notification is about
1860 * @param atsi performance data
1861 * @return GNUNET_OK to keep the connection open,
1862 * GNUNET_SYSERR to close it (signal serious error)
1866 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1867 const struct GNUNET_MessageHeader *message,
1868 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1870 unsigned int own_pos;
1873 MESH_TunnelNumber tid;
1874 struct GNUNET_MESH_ManipulatePath *msg;
1875 struct GNUNET_PeerIdentity *pi;
1876 struct GNUNET_PeerIdentity id;
1877 GNUNET_HashCode hash;
1878 struct MeshPeerPath *path;
1879 struct MeshPeerInfo *dest_peer_info;
1880 struct MeshPeerInfo *orig_peer_info;
1881 struct MeshTunnel *t;
1883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1884 "MESH: Received a MESH path create msg\n");
1885 size = ntohs (message->size);
1886 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1888 GNUNET_break_op (0);
1892 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1893 if (size % sizeof (struct GNUNET_PeerIdentity))
1895 GNUNET_break_op (0);
1898 size /= sizeof (struct GNUNET_PeerIdentity);
1901 GNUNET_break_op (0);
1904 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1906 tid = ntohl (msg->tid);
1907 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1908 t = tunnel_get (pi, tid);
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1913 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1914 t->id.oid = GNUNET_PEER_intern (pi);
1916 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1918 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1920 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1921 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1929 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1930 if (NULL == dest_peer_info)
1932 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1933 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1934 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1936 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1938 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1939 if (NULL == orig_peer_info)
1941 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1942 orig_peer_info->id = GNUNET_PEER_intern (pi);
1943 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1944 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1947 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
1948 path->length = size;
1949 path->peers = GNUNET_malloc (size * sizeof (GNUNET_PEER_Id));
1951 for (i = 0; i < size; i++)
1953 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1954 if (path->peers[i] == myid)
1958 { /* cannot be self, must be 'not found' */
1959 /* create path: self not found in path through self */
1960 GNUNET_break_op (0);
1961 path_destroy (path);
1962 /* FIXME error. destroy tunnel? leave for timeout? */
1965 if (own_pos == size - 1)
1967 /* It is for us! Send ack. */
1968 struct MeshDataDescriptor *info;
1971 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1972 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1973 info->origin = &t->id;
1974 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1975 GNUNET_assert (info->peer);
1976 for (j = 0; info->peer->core_transmit[j]; j++)
1984 info->handler_n = j;
1985 info->peer->core_transmit[j] =
1986 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1987 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1988 sizeof (struct GNUNET_MessageHeader),
1989 &send_core_path_ack, info);
1993 /* It's for somebody else! Retransmit. */
1994 struct MeshPathInfo *path_info;
1996 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1998 path_info->path = path;
1999 path_info->peer = dest_peer_info;
2001 path_add_to_peer (dest_peer_info, path);
2002 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
2003 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2004 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2005 sizeof (struct GNUNET_MessageHeader),
2006 &send_core_create_path, path_info);
2013 * Core handler for mesh network traffic going from the origin to a peer
2015 * @param cls closure
2016 * @param peer peer identity this notification is about
2017 * @param message message
2018 * @param atsi performance data
2019 * @return GNUNET_OK to keep the connection open,
2020 * GNUNET_SYSERR to close it (signal serious error)
2023 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2024 const struct GNUNET_MessageHeader *message,
2025 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2027 struct GNUNET_MESH_Unicast *msg;
2028 struct GNUNET_PeerIdentity id;
2029 struct MeshTunnel *t;
2030 struct MeshPeerInfo *pi;
2033 size = ntohs (message->size);
2035 sizeof (struct GNUNET_MESH_Unicast) +
2036 sizeof (struct GNUNET_MessageHeader))
2041 msg = (struct GNUNET_MESH_Unicast *) message;
2042 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2045 /* TODO notify back: we don't know this tunnel */
2048 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2049 &msg->destination.hashPubKey);
2052 /* TODO maybe feedback, log to statistics */
2057 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
2060 GNUNET_PEER_resolve (path_get_first_hop (t, pi)->id, &id);
2061 msg = GNUNET_malloc (size);
2062 memcpy (msg, message, size);
2063 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2064 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
2065 &send_core_data_raw, msg);
2071 * Core handler for mesh network traffic going from the origin to all peers
2073 * @param cls closure
2074 * @param message message
2075 * @param peer peer identity this notification is about
2076 * @param atsi performance data
2077 * @return GNUNET_OK to keep the connection open,
2078 * GNUNET_SYSERR to close it (signal serious error)
2081 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2082 const struct GNUNET_MessageHeader *message,
2083 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2085 struct GNUNET_MESH_Multicast *msg;
2086 struct GNUNET_PeerIdentity id;
2087 struct MeshDataDescriptor *info;
2088 struct MeshPathInfo neighbors;
2089 struct MeshTunnel *t;
2095 size = ntohs (message->size);
2097 sizeof (struct GNUNET_MESH_Multicast) +
2098 sizeof (struct GNUNET_MessageHeader))
2100 GNUNET_break_op (0);
2103 msg = (struct GNUNET_MESH_Multicast *) message;
2104 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2108 /* TODO notify that we dont know that tunnel */
2112 /* Transmit to locally interested clients */
2113 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2115 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
2118 /* Retransmit to other peers.
2119 * Using path here as just a collection of peers, not a path per se.
2122 neighbors.path = GNUNET_malloc (sizeof (struct MeshPeerPath));
2123 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &iterate_collect_neighbors,
2125 if (0 == neighbors.path->length)
2127 GNUNET_free (neighbors.path);
2130 size -= sizeof (struct GNUNET_MESH_Multicast);
2131 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + size);
2132 info->origin = &t->id;
2133 info->copies = neighbors.path->length;
2134 for (i = 0; i < info->copies; i++)
2136 GNUNET_PEER_resolve (neighbors.path->peers[i], &id);
2137 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2138 GNUNET_assert (NULL != info->peer);
2139 for (j = 0; 0 != info->peer->core_transmit[j]; j++)
2141 if (j == (CORE_QUEUE_SIZE - 1))
2147 info->handler_n = j;
2148 info->peer->infos[j] = info;
2149 info->peer->core_transmit[j] =
2150 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2151 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2152 ntohs (msg->header.size),
2153 &send_core_data_multicast, info);
2155 GNUNET_free (neighbors.path->peers);
2156 GNUNET_free (neighbors.path);
2162 * Core handler for mesh network traffic
2164 * @param cls closure
2165 * @param message message
2166 * @param peer peer identity this notification is about
2167 * @param atsi performance data
2169 * @return GNUNET_OK to keep the connection open,
2170 * GNUNET_SYSERR to close it (signal serious error)
2173 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2174 const struct GNUNET_MessageHeader *message,
2175 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2177 struct GNUNET_MESH_ToOrigin *msg;
2178 struct GNUNET_PeerIdentity id;
2179 struct MeshPeerInfo *peer_info;
2180 struct MeshTunnel *t;
2183 size = ntohs (message->size);
2184 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2185 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2187 GNUNET_break_op (0);
2190 msg = (struct GNUNET_MESH_ToOrigin *) message;
2191 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2195 /* TODO notify that we dont know this tunnel (whom)? */
2199 if (t->id.oid == myid)
2201 if (NULL == t->client)
2203 /* got data packet for ownerless tunnel */
2204 GNUNET_break_op (0);
2207 /* TODO signature verification */
2208 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
2212 peer_info = peer_info_get (&msg->oid);
2213 if (NULL == peer_info)
2215 /* unknown origin of tunnel */
2219 GNUNET_PEER_resolve (t->paths->me->parent->peer->id, &id);
2220 msg = GNUNET_malloc (size);
2221 memcpy (msg, message, size);
2222 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2223 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
2224 &send_core_data_raw, msg);
2231 * Core handler for path ACKs
2233 * @param cls closure
2234 * @param message message
2235 * @param peer peer identity this notification is about
2236 * @param atsi performance data
2238 * @return GNUNET_OK to keep the connection open,
2239 * GNUNET_SYSERR to close it (signal serious error)
2241 * FIXME path change state
2244 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2245 const struct GNUNET_MessageHeader *message,
2246 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2248 struct GNUNET_MESH_PathACK *msg;
2249 struct GNUNET_PeerIdentity id;
2250 struct MeshTunnel *t;
2251 struct MeshPeerInfo *peer_info;
2253 msg = (struct GNUNET_MESH_PathACK *) message;
2254 t = tunnel_get (&msg->oid, msg->tid);
2257 /* TODO notify that we don't know the tunnel */
2261 /* Message for us? */
2262 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2264 struct GNUNET_MESH_PeerControl pc;
2266 if (NULL == t->client)
2271 peer_info = peer_info_get (&msg->peer_id);
2272 if (NULL == peer_info)
2274 GNUNET_break_op (0);
2277 /* FIXME change state of peer */
2278 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2279 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2280 pc.tunnel_id = htonl (t->local_tid);
2281 GNUNET_PEER_resolve (peer_info->id, &pc.peer);
2282 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
2283 &pc.header, GNUNET_NO);
2287 peer_info = peer_info_get (&msg->oid);
2288 if (NULL == peer_info)
2290 /* If we know the tunnel, we should DEFINITELY know the peer */
2294 GNUNET_PEER_resolve (path_get_first_hop (t, peer_info)->id, &id);
2295 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2296 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2297 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2298 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2299 sizeof (struct GNUNET_MESH_PathACK),
2300 &send_core_data_raw, msg);
2306 * Functions to handle messages from core
2308 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2309 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2310 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2311 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2312 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2313 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2314 sizeof (struct GNUNET_MESH_PathACK)},
2320 /******************************************************************************/
2321 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2322 /******************************************************************************/
2325 * deregister_app: iterator for removing each application registered by a client
2327 * @param cls closure
2328 * @param key the hash of the application id (used to access the hashmap)
2329 * @param value the value stored at the key (client)
2331 * @return GNUNET_OK on success
2334 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2336 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2342 * notify_client_connection_failure: notify a client that the connection to the
2343 * requested remote peer is not possible (for instance, no route found)
2344 * Function called when the socket is ready to queue more data. "buf" will be
2345 * NULL and "size" zero if the socket was closed for writing in the meantime.
2347 * @param cls closure
2348 * @param size number of bytes available in buf
2349 * @param buf where the callee should write the message
2350 * @return number of bytes written to buf
2353 notify_client_connection_failure (void *cls, size_t size, void *buf)
2356 struct MeshPeerInfo *peer_info;
2357 struct GNUNET_MESH_PeerControl *msg;
2358 struct GNUNET_PeerIdentity id;
2360 if (0 == size && NULL == buf)
2362 // TODO retry? cancel?
2366 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2367 peer_info = (struct MeshPeerInfo *) cls;
2368 msg = (struct GNUNET_MESH_PeerControl *) buf;
2369 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2370 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2371 // msg->tunnel_id = htonl(peer_info->t->tid);
2372 GNUNET_PEER_resolve (peer_info->id, &id);
2373 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2381 * Function to process paths received for a new peer addition. The recorded
2382 * paths form the initial tunnel, which can be optimized later.
2383 * Called on each result obtained for the DHT search.
2385 * @param cls closure
2386 * @param exp when will this value expire
2387 * @param key key of the result
2388 * @param get_path NULL-terminated array of pointers
2389 * to the peers on reverse GET path (or NULL if not recorded)
2390 * @param put_path NULL-terminated array of pointers
2391 * to the peers on the PUT path (or NULL if not recorded)
2392 * @param type type of the result
2393 * @param size number of bytes in data
2394 * @param data pointer to the result data
2399 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2400 const GNUNET_HashCode * key,
2401 const struct GNUNET_PeerIdentity *const *get_path,
2402 const struct GNUNET_PeerIdentity *const *put_path,
2403 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2405 struct MeshPathInfo *path_info = cls;
2406 struct MeshPeerPath *p;
2407 struct GNUNET_PeerIdentity pi;
2410 if (NULL == get_path || NULL == put_path)
2412 if (NULL == path_info->peer->path_head)
2414 // Find ourselves some alternate initial path to the destination: retry
2415 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2416 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2417 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2418 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2419 GNUNET_BLOCK_TYPE_TEST, /* type */
2420 &pi.hashPubKey, /*key to search */
2421 4, /* replication level */
2422 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2425 0, /* xquery bits */
2427 (void *) path_info);
2432 p = path_build_from_dht (get_path, put_path);
2433 path_add_to_peer (path_info->peer, p);
2434 for (i = 0; i < path_info->peer->ntunnels; i++)
2436 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2438 GNUNET_free (path_info);
2445 * Function to process paths received for a new peer addition. The recorded
2446 * paths form the initial tunnel, which can be optimized later.
2447 * Called on each result obtained for the DHT search.
2449 * @param cls closure
2450 * @param exp when will this value expire
2451 * @param key key of the result
2452 * @param get_path NULL-terminated array of pointers
2453 * to the peers on reverse GET path (or NULL if not recorded)
2454 * @param put_path NULL-terminated array of pointers
2455 * to the peers on the PUT path (or NULL if not recorded)
2456 * @param type type of the result
2457 * @param size number of bytes in data
2458 * @param data pointer to the result data
2461 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2462 const GNUNET_HashCode * key,
2463 const struct GNUNET_PeerIdentity *const *get_path,
2464 const struct GNUNET_PeerIdentity *const *put_path,
2465 enum GNUNET_BLOCK_Type type, size_t size,
2468 const struct GNUNET_PeerIdentity *pi = data;
2469 struct GNUNET_PeerIdentity id;
2470 struct MeshTunnel *t = cls;
2471 struct MeshPeerInfo *peer_info;
2472 struct MeshPathInfo *path_info;
2473 struct MeshPeerPath *p;
2476 if (size != sizeof (struct GNUNET_PeerIdentity))
2478 GNUNET_break_op (0);
2481 GNUNET_assert (NULL != t->client);
2482 GNUNET_DHT_get_stop (t->client->dht_get_type);
2483 t->client->dht_get_type = NULL;
2484 peer_info = peer_info_get (pi);
2485 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2486 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2488 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2489 NULL == peer_info->dhtget)
2491 /* we don't have a route to the peer, let's try a direct lookup */
2492 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2494 GNUNET_TIME_UNIT_FOREVER_REL,
2496 GNUNET_BLOCK_TYPE_TEST,
2499 /* key to look up */
2501 /* replication level */
2502 GNUNET_DHT_RO_RECORD_ROUTE,
2503 /* option to dht: record route */
2504 NULL, /* bloom filter */
2507 0, /* xquery bits */
2510 peer_info); /* closure */
2513 p = path_build_from_dht (get_path, put_path);
2514 path_add_to_peer (peer_info, p);
2515 tunnel_add_peer(t, peer_info);
2516 p = tunnel_get_path_to_peer(t, peer_info);
2517 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2519 path_info->peer = peer_info;
2520 path_info->path = p;
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2523 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2524 t->local_tid, p->length);
2525 for (i = 0; i < p->length; i++)
2527 GNUNET_PEER_resolve (p->peers[0], &id);
2528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2529 GNUNET_h2s_full (&id.hashPubKey));
2533 GNUNET_PEER_resolve (p->peers[1], &id);
2534 GNUNET_CORE_notify_transmit_ready (core_handle,
2540 GNUNET_TIME_UNIT_FOREVER_REL,
2544 sizeof (struct GNUNET_MESH_ManipulatePath)
2547 sizeof (struct GNUNET_PeerIdentity)),
2549 &send_core_create_path,
2551 path_info); /* cls */
2554 /******************************************************************************/
2555 /********************* MESH LOCAL HANDLES **************************/
2556 /******************************************************************************/
2560 * Handler for client disconnection
2562 * @param cls closure
2563 * @param client identification of the client; NULL
2564 * for the last call when the server is destroyed
2567 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2569 struct MeshClient *c;
2570 struct MeshClient *next;
2572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2578 if (c->handle != client && NULL != client)
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2585 if (NULL != c->tunnels)
2587 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2588 &tunnel_destroy_iterator,
2590 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2593 /* deregister clients applications */
2594 if (NULL != c->apps)
2596 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2597 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2599 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2600 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2602 GNUNET_SCHEDULER_cancel (announce_applications_task);
2603 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2605 if (NULL != c->types)
2606 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2607 if (NULL != c->dht_get_type)
2608 GNUNET_DHT_get_stop (c->dht_get_type);
2609 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2621 * Handler for new clients
2623 * @param cls closure
2624 * @param client identification of the client
2625 * @param message the actual message, which includes messages the client wants
2628 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2629 const struct GNUNET_MessageHeader *message)
2631 struct GNUNET_MESH_ClientConnect *cc_msg;
2632 struct MeshClient *c;
2633 GNUNET_MESH_ApplicationType *a;
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2641 /* Check data sanity */
2642 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2643 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2644 ntypes = ntohs (cc_msg->types);
2645 napps = ntohs (cc_msg->applications);
2647 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2650 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2654 /* Create new client structure */
2655 c = GNUNET_malloc (sizeof (struct MeshClient));
2657 c->id = next_client_id++;
2660 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2663 GNUNET_MESH_ApplicationType at;
2666 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2667 for (i = 0; i < napps; i++)
2670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2671 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2672 /* store in clients hashmap */
2673 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2674 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2675 /* store in global hashmap, for announcements */
2676 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2677 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2679 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2680 announce_applications_task =
2681 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2689 t = (uint16_t *) & a[napps];
2690 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2691 for (i = 0; i < ntypes; i++)
2694 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2696 /* store in clients hashmap */
2697 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2698 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2699 /* store in global hashmap */
2700 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2701 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2705 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2707 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2708 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2709 GNUNET_SERVER_notification_context_add (nc, client);
2711 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2719 * Handler for requests of new tunnels
2721 * @param cls closure
2722 * @param client identification of the client
2723 * @param message the actual message
2726 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2727 const struct GNUNET_MessageHeader *message)
2729 struct GNUNET_MESH_TunnelMessage *t_msg;
2730 struct MeshTunnel *t;
2731 struct MeshClient *c;
2732 GNUNET_HashCode hash;
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2736 /* Sanity check for client registration */
2737 if (NULL == (c = client_get (client)))
2740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2747 /* Message sanity check */
2748 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2751 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2755 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2756 /* Sanity check for tunnel numbering */
2757 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2760 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2763 /* Sanity check for duplicate tunnel IDs */
2764 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2767 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2771 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2773 while (NULL != tunnel_get_by_pi (myid, next_tid))
2774 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2775 t->id.tid = next_tid++;
2777 t->local_tid = ntohl (t_msg->tunnel_id);
2779 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2781 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2783 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2784 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2787 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2791 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2793 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2794 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2797 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2801 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2807 * Handler for requests of deleting tunnels
2809 * @param cls closure
2810 * @param client identification of the client
2811 * @param message the actual message
2814 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2815 const struct GNUNET_MessageHeader *message)
2817 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2818 struct MeshClient *c;
2819 struct MeshTunnel *t;
2820 MESH_TunnelNumber tid;
2821 GNUNET_HashCode hash;
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2825 /* Sanity check for client registration */
2826 if (NULL == (c = client_get (client)))
2829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2832 /* Message sanity check */
2833 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2836 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2842 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2844 /* Retrieve tunnel */
2845 tid = ntohl (tunnel_msg->tunnel_id);
2847 /* Remove from local id hashmap */
2848 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2849 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2850 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2852 /* Remove from global id hashmap */
2853 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2854 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2856 // notify_tunnel_destroy(t); FIXME
2858 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2864 * Handler for connection requests to new peers
2866 * @param cls closure
2867 * @param client identification of the client
2868 * @param message the actual message (PeerControl)
2873 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2874 const struct GNUNET_MessageHeader *message)
2876 struct GNUNET_MESH_PeerControl *peer_msg;
2877 struct MeshClient *c;
2878 struct MeshTunnel *t;
2879 MESH_TunnelNumber tid;
2880 struct MeshPeerInfo *peer_info;
2883 /* Sanity check for client registration */
2884 if (NULL == (c = client_get (client)))
2887 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2891 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2892 /* Sanity check for message size */
2893 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2896 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2900 /* Tunnel exists? */
2901 tid = ntohl (peer_msg->tunnel_id);
2902 t = tunnel_get_by_local_id (c, tid);
2906 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2910 /* Does client own tunnel? */
2911 if (t->client->handle != client)
2914 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2919 peer_info = peer_info_get (&peer_msg->peer);
2921 /* Start DHT search if needed FIXME: if not already connected */
2922 if (NULL == peer_info->dhtget)
2924 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 */
2925 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2928 0, /* xquery bits */
2930 (void *) peer_info);
2933 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2939 * Handler for disconnection requests of peers in a tunnel
2941 * @param cls closure
2942 * @param client identification of the client
2943 * @param message the actual message (PeerControl)
2946 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2947 const struct GNUNET_MessageHeader *message)
2949 struct GNUNET_MESH_PeerControl *peer_msg;
2950 struct MeshClient *c;
2951 struct MeshTunnel *t;
2952 MESH_TunnelNumber tid;
2954 /* Sanity check for client registration */
2955 if (NULL == (c = client_get (client)))
2958 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2961 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2962 /* Sanity check for message size */
2963 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2966 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2970 /* Tunnel exists? */
2971 tid = ntohl (peer_msg->tunnel_id);
2972 t = tunnel_get_by_local_id (c, tid);
2976 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2980 /* Does client own tunnel? */
2981 if (t->client->handle != client)
2984 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2988 /* Ok, delete peer from tunnel */
2989 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2990 &peer_msg->peer.hashPubKey);
2992 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2998 * Handler for connection requests to new peers by type
3000 * @param cls closure
3001 * @param client identification of the client
3002 * @param message the actual message (ConnectPeerByType)
3005 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3006 const struct GNUNET_MessageHeader *message)
3008 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3009 struct MeshClient *c;
3010 struct MeshTunnel *t;
3011 GNUNET_HashCode hash;
3012 GNUNET_MESH_ApplicationType type;
3013 MESH_TunnelNumber tid;
3015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3016 /* Sanity check for client registration */
3017 if (NULL == (c = client_get (client)))
3020 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3024 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3025 /* Sanity check for message size */
3026 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3027 ntohs (connect_msg->header.size))
3030 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3034 /* Tunnel exists? */
3035 tid = ntohl (connect_msg->tunnel_id);
3036 t = tunnel_get_by_local_id (c, tid);
3040 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3044 /* Does client own tunnel? */
3045 if (t->client->handle != client)
3048 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3052 /* Do WE have the service? */
3053 type = ntohl (connect_msg->type);
3054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
3055 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3056 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3059 /* Yes! Fast forward, add ourselves to the tunnel and send the
3060 * good news to the client
3062 struct GNUNET_MESH_PeerControl pc;
3064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3065 pc.peer = my_full_id;
3066 GNUNET_CONTAINER_multihashmap_put (t->peers, &pc.peer.hashPubKey,
3067 peer_info_get (&pc.peer),
3068 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3069 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3070 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
3071 pc.tunnel_id = htonl (t->local_tid);
3072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3073 GNUNET_SERVER_notification_context_unicast (nc, /* context */
3075 &pc.header, /* msg */
3076 GNUNET_NO); /* can drop? */
3077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3078 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3081 /* Ok, lets find a peer offering the service */
3082 if (c->dht_get_type)
3084 GNUNET_DHT_get_stop (c->dht_get_type);
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3087 GNUNET_h2s_full (&hash));
3089 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
3090 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
3091 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
3092 &dht_get_type_handler, t);
3094 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3100 * Handler for client traffic directed to one peer
3102 * @param cls closure
3103 * @param client identification of the client
3104 * @param message the actual message
3107 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3108 const struct GNUNET_MessageHeader *message)
3110 struct MeshClient *c;
3111 struct MeshTunnel *t;
3112 struct MeshPeerInfo *pi;
3113 struct GNUNET_MESH_Unicast *data_msg;
3114 struct GNUNET_PeerIdentity next_hop;
3115 struct MeshDataDescriptor *info;
3116 MESH_TunnelNumber tid;
3119 /* Sanity check for client registration */
3120 if (NULL == (c = client_get (client)))
3123 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3126 data_msg = (struct GNUNET_MESH_Unicast *) message;
3127 /* Sanity check for message size */
3128 if (sizeof (struct GNUNET_MESH_Unicast) +
3129 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
3132 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3136 /* Tunnel exists? */
3137 tid = ntohl (data_msg->tid);
3138 t = tunnel_get_by_local_id (c, tid);
3142 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3146 /* Is it a local tunnel? Then, does client own the tunnel? */
3147 if (t->client->handle != NULL && t->client->handle != client)
3150 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3154 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3155 &data_msg->destination.hashPubKey);
3156 /* Is the selected peer in the tunnel? */
3160 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3165 struct GNUNET_MESH_Unicast copy;
3167 /* Work around const limitation */
3168 memcpy (©, data_msg, sizeof (struct GNUNET_MESH_Unicast));
3169 copy.oid = my_full_id;
3170 copy.tid = htonl (t->id.tid);
3171 handle_mesh_data_unicast (NULL, &my_full_id, ©.header, NULL);
3174 GNUNET_PEER_resolve (path_get_first_hop (t, pi)->id, &next_hop);
3175 data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
3176 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
3177 memcpy (&info[1], &data_msg[1], data_size);
3178 info->destination = pi->id;
3179 info->origin = &t->id;
3180 info->size = data_size;
3181 info->client = client;
3182 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
3183 GNUNET_TIME_UNIT_FOREVER_REL, &next_hop,
3184 /* FIXME re-check types */
3186 sizeof (struct GNUNET_MESH_Unicast),
3187 &send_core_data_unicast, info);
3192 * Handler for client traffic directed to all peers in a tunnel
3194 * @param cls closure
3195 * @param client identification of the client
3196 * @param message the actual message
3199 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3200 const struct GNUNET_MessageHeader *message)
3202 struct MeshClient *c;
3203 struct MeshTunnel *t;
3204 struct GNUNET_MESH_Multicast *data_msg;
3205 MESH_TunnelNumber tid;
3207 /* Sanity check for client registration */
3208 if (NULL == (c = client_get (client)))
3211 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3214 data_msg = (struct GNUNET_MESH_Multicast *) message;
3215 /* Sanity check for message size */
3216 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
3219 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3223 /* Tunnel exists? */
3224 tid = ntohl (data_msg->tid);
3225 t = tunnel_get_by_local_id (c, tid);
3229 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3233 /* Does client own tunnel? */
3234 if (t->client->handle != client)
3237 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3243 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3248 * Functions to handle messages from clients
3250 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3251 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3252 {&handle_local_tunnel_create, NULL,
3253 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3254 sizeof (struct GNUNET_MESH_TunnelMessage)},
3255 {&handle_local_tunnel_destroy, NULL,
3256 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3257 sizeof (struct GNUNET_MESH_TunnelMessage)},
3258 {&handle_local_connect_add, NULL,
3259 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3260 sizeof (struct GNUNET_MESH_PeerControl)},
3261 {&handle_local_connect_del, NULL,
3262 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3263 sizeof (struct GNUNET_MESH_PeerControl)},
3264 {&handle_local_connect_by_type, NULL,
3265 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3266 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3267 {&handle_local_unicast, NULL,
3268 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3269 {&handle_local_unicast, NULL,
3270 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3271 {&handle_local_multicast, NULL,
3272 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3278 * To be called on core init/fail.
3280 * @param cls service closure
3281 * @param server handle to the server for this service
3282 * @param identity the public identity of this peer
3283 * @param publicKey the public key of this peer
3286 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3287 const struct GNUNET_PeerIdentity *identity,
3288 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
3290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3291 core_handle = server;
3292 my_full_id = *identity;
3293 myid = GNUNET_PEER_intern (identity);
3294 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3299 * Method called whenever a given peer connects.
3301 * @param cls closure
3302 * @param peer peer identity this notification is about
3303 * @param atsi performance data for the connection
3306 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3307 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3309 // GNUNET_PEER_Id pid;
3310 struct MeshPeerInfo *peer_info;
3311 struct MeshPeerPath *path;
3313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3314 peer_info = peer_info_get (peer);
3315 if (myid == peer_info->id)
3317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3319 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
3321 path->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id) * 2);
3322 path->peers[0] = myid;
3323 path->peers[1] = peer_info->id;
3324 path_add_to_peer (peer_info, path);
3329 * Method called whenever a peer disconnects.
3331 * @param cls closure
3332 * @param peer peer identity this notification is about
3335 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3337 struct MeshPeerInfo *pi;
3340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3341 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3347 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3349 if (pi->core_transmit[i])
3351 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3352 /* TODO: notify that tranmission has failed */
3353 GNUNET_free (pi->infos[i]);
3356 path_remove_from_peer (pi, pi->id, myid);
3359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3365 /******************************************************************************/
3366 /************************ MAIN FUNCTIONS ****************************/
3367 /******************************************************************************/
3370 * Task run during shutdown.
3376 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3378 struct MeshClient *c;
3380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3381 if (core_handle != NULL)
3383 GNUNET_CORE_disconnect (core_handle);
3386 if (dht_handle != NULL)
3388 for (c = clients; NULL != c; c = c->next)
3389 if (NULL != c->dht_get_type)
3390 GNUNET_DHT_get_stop (c->dht_get_type);
3391 GNUNET_DHT_disconnect (dht_handle);
3396 GNUNET_SERVER_notification_context_destroy (nc);
3399 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3401 GNUNET_SCHEDULER_cancel (announce_id_task);
3402 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3408 * Process mesh requests.
3410 * @param cls closure
3411 * @param server the initialized server
3412 * @param c configuration to use
3415 run (void *cls, struct GNUNET_SERVER_Handle *server,
3416 const struct GNUNET_CONFIGURATION_Handle *c)
3418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3419 GNUNET_SERVER_add_handlers (server, client_handlers);
3420 GNUNET_SERVER_disconnect_notify (server,
3421 &handle_local_client_disconnect,
3423 server_handle = server;
3424 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3425 CORE_QUEUE_SIZE, /* queue size */
3426 NULL, /* Closure passed to MESH functions */
3427 &core_init, /* Call core_init once connected */
3428 &core_connect, /* Handle connects */
3429 &core_disconnect, /* remove peers on disconnects */
3430 NULL, /* Do we care about "status" updates? */
3431 NULL, /* Don't notify about all incoming messages */
3432 GNUNET_NO, /* For header only in notification */
3433 NULL, /* Don't notify about all outbound messages */
3434 GNUNET_NO, /* For header-only out notification */
3435 core_handlers); /* Register these handlers */
3436 if (core_handle == NULL)
3440 dht_handle = GNUNET_DHT_connect (c, 64);
3441 if (dht_handle == NULL)
3443 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3444 Running without DHT has a severe\
3445 impact in MESH capabilities.\n\
3446 Plase check your configuretion and enable DHT.\n");
3452 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3453 peers = GNUNET_CONTAINER_multihashmap_create (32);
3454 applications = GNUNET_CONTAINER_multihashmap_create (32);
3455 types = GNUNET_CONTAINER_multihashmap_create (32);
3456 nc = GNUNET_SERVER_notification_context_create (server_handle,
3459 clients_tail = NULL;
3464 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3466 /* Scheduled the task to clean up when shutdown is called */
3467 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3474 * The main function for the mesh service.
3476 * @param argc number of arguments from the command line
3477 * @param argv command line arguments
3478 * @return 0 ok, 1 on error
3481 main (int argc, char *const *argv)
3485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3488 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");