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
49 #include "mesh_protocol.h"
50 #include "gnunet_dht_service.h"
51 #include "mesh_tunnel_tree.h"
53 /* TODO: move into configuration file */
54 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
55 GNUNET_TIME_UNIT_SECONDS,\
57 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
58 GNUNET_TIME_UNIT_SECONDS,\
61 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
62 GNUNET_TIME_UNIT_SECONDS,\
66 /******************************************************************************/
67 /************************ DATA STRUCTURES ****************************/
68 /******************************************************************************/
70 /** FWD declaration */
74 * Struct containing all info possibly needed to build a package when called
77 struct MeshDataDescriptor
79 /** ID of the tunnel this packet travels in */
80 struct MESH_TunnelID *origin;
85 /** Client that asked for the transmission, if any */
86 struct GNUNET_SERVER_Client *client;
88 /** Who was this message being sent to */
89 struct MeshPeerInfo *peer;
91 /** Ultimate destination of the packet */
92 GNUNET_PEER_Id destination;
94 /** Number of identical messages sent to different hops (multicast) */
97 /** Which handler was used to request the transmission */
98 unsigned int handler_n;
100 /** Size of the data */
107 * Struct containing all information regarding a given peer
117 * Last time we heard from this peer
119 struct GNUNET_TIME_Absolute last_contact;
122 * Number of attempts to reconnect so far
124 int n_reconnect_attempts;
127 * Paths to reach the peer, ordered by ascending hop count
129 struct MeshPeerPath *path_head;
132 * Paths to reach the peer, ordered by ascending hop count
134 struct MeshPeerPath *path_tail;
137 * Handle to stop the DHT search for a path to this peer
139 struct GNUNET_DHT_GetHandle *dhtget;
142 * Handles to stop queued transmissions for this peer
144 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
147 * Pointer to info stuctures used as cls for queued transmissions
149 void *infos[CORE_QUEUE_SIZE];
152 * Type of message being in each transmission
154 uint16_t types[CORE_QUEUE_SIZE];
157 * Array of tunnels this peer participates in
158 * (most probably a small amount, therefore not a hashmap)
159 * When the path to the peer changes, notify these tunnels to let them
160 * re-adjust their path trees.
162 struct MeshTunnel **tunnels;
165 * Number of tunnels above
167 unsigned int ntunnels;
172 * Data scheduled to transmit (to local client or remote peer)
179 struct MeshQueue *next;
180 struct MeshQueue *prev;
183 * Target of the data (NULL if target is client)
185 struct MeshPeerInfo *peer;
188 * Client to send the data to (NULL if target is peer)
190 struct MeshClient *client;
193 * Size of the message to transmit
198 * How old is the data?
200 struct GNUNET_TIME_Absolute timestamp;
205 struct GNUNET_MessageHeader *data;
209 * Globally unique tunnel identification (owner + number)
210 * DO NOT USE OVER THE NETWORK
215 * Node that owns the tunnel
220 * Tunnel number to differentiate all the tunnels owned by the node oid
221 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
223 MESH_TunnelNumber tid;
227 struct MeshClient; /* FWD declaration */
230 * Struct containing all information regarding a tunnel
231 * For an intermediate node the improtant info used will be:
232 * - id Tunnel unique identification
233 * - paths[0] To know where to send it next
234 * - metainfo: ready, speeds, accounting
241 struct MESH_TunnelID id;
244 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
246 MESH_TunnelNumber local_tid;
249 * Last time the tunnel was used
251 struct GNUNET_TIME_Absolute timestamp;
254 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
256 struct GNUNET_CONTAINER_MultiHashMap *peers;
259 * Number of peers that are connected and potentially ready to receive data
261 unsigned int peers_ready;
264 * Number of peers that have been added to the tunnel
266 unsigned int peers_total;
269 * Client owner of the tunnel, if any
271 struct MeshClient *client;
274 * Messages ready to transmit
276 struct MeshQueue *queue_head;
277 struct MeshQueue *queue_tail;
282 struct MeshTunnelTree *tree;
285 * Task to keep the used paths alive
287 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
292 * Info needed to work with tunnel paths and peers
299 struct MeshTunnel *t;
304 struct MeshPeerInfo *peer;
309 struct MeshPeerPath *path;
314 * Struct containing information about a client of the service
321 struct MeshClient *next;
322 struct MeshClient *prev;
325 * Tunnels that belong to this client, indexed by local id
327 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
330 * Handle to communicate with the client
332 struct GNUNET_SERVER_Client *handle;
335 * Applications that this client has claimed to provide
337 struct GNUNET_CONTAINER_MultiHashMap *apps;
340 * Messages that this client has declared interest in
342 struct GNUNET_CONTAINER_MultiHashMap *types;
345 * Used to search peers offering a service
347 struct GNUNET_DHT_GetHandle *dht_get_type;
351 * ID of the client, for debug messages
360 /******************************************************************************/
361 /************************ DEBUG FUNCTIONS ****************************/
362 /******************************************************************************/
366 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
367 * @param cls string to print
368 * @param tc task context
371 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
375 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
384 /******************************************************************************/
385 /*********************** GLOBAL VARIABLES ****************************/
386 /******************************************************************************/
391 static struct MeshClient *clients;
392 static struct MeshClient *clients_tail;
395 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
397 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
400 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
402 static struct GNUNET_CONTAINER_MultiHashMap *peers;
405 * Handle to communicate with core
407 static struct GNUNET_CORE_Handle *core_handle;
412 static struct GNUNET_DHT_Handle *dht_handle;
417 static struct GNUNET_SERVER_Handle *server_handle;
420 * Notification context, to send messages to local clients
422 static struct GNUNET_SERVER_NotificationContext *nc;
425 * Local peer own ID (memory efficient handle)
427 static GNUNET_PEER_Id myid;
430 * Local peer own ID (full value)
432 static struct GNUNET_PeerIdentity my_full_id;
437 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
442 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
445 * Tunnel ID for the next created tunnel (global tunnel number)
447 static MESH_TunnelNumber next_tid;
450 * All application types provided by this peer
452 static struct GNUNET_CONTAINER_MultiHashMap *applications;
455 * All message types clients of this peer are interested in
457 static struct GNUNET_CONTAINER_MultiHashMap *types;
460 * Task to periodically announce provided applications
462 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
465 * Task to periodically announce itself in the network
467 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
470 unsigned int next_client_id;
474 /******************************************************************************/
475 /************************ ITERATORS ****************************/
476 /******************************************************************************/
478 /* FIXME move iterators here */
481 /******************************************************************************/
482 /************************ PERIODIC FUNCTIONS ****************************/
483 /******************************************************************************/
486 * Announce iterator over for each application provided by the peer
489 * @param key current key code
490 * @param value value in the hash map
491 * @return GNUNET_YES if we should continue to
496 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
498 /* FIXME are hashes in multihash map equal on all aquitectures? */
499 GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
500 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
501 (const char *) &my_full_id,
503 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
504 &mesh_debug, "DHT_put for app completed");
506 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
508 APP_ANNOUNCE_TIME, NULL, NULL);
515 * Periodically announce what applications are provided by local clients
518 * @param tc task context
521 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
523 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
525 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
529 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
531 announce_applications_task =
532 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
540 * Periodically announce self id in the DHT
543 * @param tc task context
546 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
548 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
550 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
554 * - Set data expiration in function of X
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
558 GNUNET_h2s_full (&my_full_id.hashPubKey));
559 GNUNET_DHT_put (dht_handle, /* DHT handle */
560 &my_full_id.hashPubKey, /* Key to use */
561 10U, /* Replication level */
562 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
563 GNUNET_BLOCK_TYPE_TEST, /* Block type */
564 sizeof(my_full_id), /* Size of the data */
565 (char *)&my_full_id, /* Data itself */
566 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
567 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
569 &mesh_debug, "DHT_put for id completed");
571 NULL, /* Continuation */
572 NULL); /* Continuation closure */
575 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
580 * Function to process paths received for a new peer addition. The recorded
581 * paths form the initial tunnel, which can be optimized later.
582 * Called on each result obtained for the DHT search.
585 * @param exp when will this value expire
586 * @param key key of the result
587 * @param type type of the result
588 * @param size number of bytes in data
589 * @param data pointer to the result data
592 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
593 const GNUNET_HashCode * key,
594 const struct GNUNET_PeerIdentity *get_path,
595 unsigned int get_path_length,
596 const struct GNUNET_PeerIdentity *put_path,
597 unsigned int put_path_length,
598 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
601 /******************************************************************************/
602 /****************** GENERAL HELPER FUNCTIONS ************************/
603 /******************************************************************************/
606 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
607 * and insert it in the appropiate structures if the peer is not known yet.
609 * @param peer Identity of the peer
611 * @return Existing or newly created peer info
613 static struct MeshPeerInfo *
614 peer_info_get (const struct GNUNET_PeerIdentity *peer)
616 struct MeshPeerInfo *peer_info;
618 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
619 if (NULL == peer_info)
622 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
623 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
624 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
625 peer_info->id = GNUNET_PEER_intern (peer);
634 * Destroy the peer_info and free any allocated resources linked to it
635 * @param t tunnel the path belongs to
636 * @param pi the peer_info to destroy
637 * @return GNUNET_OK on success
640 peer_info_destroy (struct MeshPeerInfo *pi)
642 GNUNET_HashCode hash;
643 struct GNUNET_PeerIdentity id;
645 GNUNET_PEER_resolve (pi->id, &id);
646 GNUNET_PEER_change_rc (pi->id, -1);
647 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
649 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
650 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
658 * Notify a tunnel that a connection has broken that affects at least
661 * @param t Tunnel affected.
662 * @param peer Peer that (at least) has been affected by the disconnection.
663 * @param p1 Peer that got disconnected from p2.
664 * @param p2 Peer that got disconnected from p1.
666 * @return Short ID of the peer disconnected (either p1 or p2).
667 * 0 if the tunnel remained unaffected.
669 static GNUNET_PEER_Id
670 tunnel_notify_connection_broken (struct MeshTunnel *t,
671 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
675 * Remove all paths that rely on a direct connection between p1 and p2
676 * from the peer itself and notify all tunnels about it.
678 * @param peer PeerInfo of affected peer.
679 * @param p1 GNUNET_PEER_Id of one peer.
680 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
683 * TODO: optimize (see below)
686 path_remove_from_peer (struct MeshPeerInfo *peer,
690 struct GNUNET_PeerIdentity id;
691 struct MeshPeerPath *p;
692 struct MeshPeerPath *aux;
693 struct MeshPeerInfo *peer_d;
695 unsigned int destroyed;
705 for (i = 0; i < (p->length - 1); i++)
707 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
708 (p->peers[i] == p2 && p->peers[i + 1] == p1))
720 for (i = 0; i < peer->ntunnels; i++)
722 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
724 * Problem: one or more peers have been deleted from the tunnel tree.
725 * We don't know who they are to try to add them again.
726 * We need to try to find a new path for each of the disconnected peers.
727 * Some of them might already have a path to reach them that does not
728 * involve p1 and p2. Adding all anew might render in a better tree than
729 * the trivial immediate fix.
731 * Trivial immiediate fix: try to reconnect to the disconnected node. All
732 * its children will be reachable trough him.
734 GNUNET_PEER_resolve(d, &id);
735 peer_d = peer_info_get(&id);
738 for (p = peer_d->path_head; NULL != p; p = p->next)
740 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
748 /* No callback, as peer will be already disconnected */
749 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
753 struct MeshPathInfo *path_info;
755 if (NULL != peer_d->dhtget)
757 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
759 path_info->peer = peer_d;
760 path_info->t = peer->tunnels[i];
761 peer_d->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
762 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
763 GNUNET_BLOCK_TYPE_TEST, /* type */
764 &id.hashPubKey, /*key to search */
765 4, /* replication level */
766 GNUNET_DHT_RO_RECORD_ROUTE,
777 * Add the path to the peer and update the path used to reach it in case this
780 * @param peer_info Destination peer to add the path to.
781 * @param path New path to add. Last peer must be the peer in arg 1.
782 * Path will be either used of freed if already known.
784 * TODO: trim the part from origin to us? Add it as path to origin?
787 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
789 struct MeshPeerPath *aux;
793 if (NULL == peer_info || NULL == path)
799 l = path_get_length (path);
801 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
803 l2 = path_get_length (aux);
806 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
807 peer_info->path_tail, aux, path);
811 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
818 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
825 * Add the path to the origin peer and update the path used to reach it in case
826 * this is the shortest.
827 * The path is given in peer_info -> destination, therefore we turn the path
830 * @param peer_info Peer to add the path to, being the origin of the path.
831 * @param path New path to add after being inversed.
834 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
837 path_add_to_peer (peer_info, path);
842 * Build a PeerPath from the paths returned from the DHT, reversing the paths
843 * to obtain a local peer -> destination path and interning the peer ids.
845 * @return Newly allocated and created path
847 static struct MeshPeerPath *
848 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
849 unsigned int get_path_length,
850 const struct GNUNET_PeerIdentity *put_path,
851 unsigned int put_path_length)
853 struct MeshPeerPath *p;
859 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
860 for (i--; i >= 0; i--)
862 id = GNUNET_PEER_intern (&get_path[i]);
863 if (p->length > 0 && id == p->peers[p->length - 1])
865 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
866 GNUNET_PEER_change_rc(id, -1);
871 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
872 p->peers[p->length] = id;
877 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
878 for (i--; i >= 0; i--)
880 id = GNUNET_PEER_intern (&put_path[i]);
881 if (p->length > 0 && id == p->peers[p->length - 1])
883 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
884 GNUNET_PEER_change_rc(id, -1);
889 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
890 p->peers[p->length] = id;
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "MESH: (first of GET: %s)\n",
897 GNUNET_h2s_full(&get_path[0].hashPubKey));
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899 "MESH: (first of PUT: %s)\n",
900 GNUNET_h2s_full(&put_path[0].hashPubKey));
901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
902 "MESH: In total: %d hops\n",
910 * Send keepalive packets for a peer
912 * @param cls Closure (tunnel for which to send the keepalive).
913 * @param tc Notification context.
915 * TODO: implement explicit multicast keepalive?
918 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
922 * Check if client has registered with the service and has not disconnected
924 * @param client the client to check
926 * @return non-NULL if client exists in the global DLL
928 static struct MeshClient *
929 client_get (struct GNUNET_SERVER_Client *client)
931 struct MeshClient *c;
936 if (c->handle == client)
945 * Checks if a given client has subscribed to certain message type
947 * @param message_type Type of message to check
948 * @param c Client to check
950 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
955 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
959 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
960 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
965 * Search for a tunnel among the tunnels for a client
967 * @param c the client whose tunnels to search in
968 * @param tid the local id of the tunnel
970 * @return tunnel handler, NULL if doesn't exist
972 static struct MeshTunnel *
973 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
975 GNUNET_HashCode hash;
977 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
978 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
983 * Search for a tunnel by global ID using PEER_ID
985 * @param pi owner of the tunnel
986 * @param tid global tunnel number
988 * @return tunnel handler, NULL if doesn't exist
990 static struct MeshTunnel *
991 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
993 struct MESH_TunnelID id;
994 GNUNET_HashCode hash;
999 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1000 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1005 * Search for a tunnel by global ID using full PeerIdentities
1007 * @param oid owner of the tunnel
1008 * @param tid global tunnel number
1010 * @return tunnel handler, NULL if doesn't exist
1012 static struct MeshTunnel *
1013 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1015 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1020 * Callback used to notify a client owner of a tunnel that a peer has
1021 * disconnected, most likely because of a path change.
1023 * @param n Node in the tree representing the disconnected peer
1026 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1028 struct GNUNET_MESH_PeerControl msg;
1030 if (NULL == n->t->client || NULL == nc)
1033 msg.header.size = htons (sizeof (msg));
1034 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1035 msg.tunnel_id = htonl (n->t->local_tid);
1036 GNUNET_PEER_resolve (n->peer, &msg.peer);
1037 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1038 &msg.header, GNUNET_NO);
1043 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1044 * needed rescources.
1046 * @param t Tunnel we want to add a new peer to
1047 * @param peer PeerInfo of the peer being added
1051 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1053 struct MeshPeerPath *p;
1054 struct MeshPeerPath *best_p;
1055 unsigned int best_cost;
1058 if (NULL != tree_find_peer(t->tree->root, peer->id))
1060 /* Already have it, nothing to do. */
1065 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1066 if (NULL == (p = peer->path_head))
1073 best_cost = UINT_MAX;
1076 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1083 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1084 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1085 t->path_refresh_task =
1086 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1092 * Notify a tunnel that a connection has broken that affects at least
1093 * some of its peers.
1095 * @param t Tunnel affected.
1096 * @param peer Peer that (at least) has been affected by the disconnection.
1097 * @param p1 Peer that got disconnected from p2.
1098 * @param p2 Peer that got disconnected from p1.
1100 * @return Short ID of the peer disconnected (either p1 or p2).
1101 * 0 if the tunnel remained unaffected.
1103 static GNUNET_PEER_Id
1104 tunnel_notify_connection_broken (struct MeshTunnel *t,
1105 struct MeshPeerInfo *peer,
1109 return tree_notify_connection_broken (t->tree, p1, p2,
1110 ¬ify_peer_disconnected);
1115 * Destroy the tunnel and free any allocated resources linked to it
1117 * @param t the tunnel to destroy
1119 * @return GNUNET_OK on success
1122 tunnel_destroy (struct MeshTunnel *t)
1124 struct MeshClient *c;
1125 struct MeshQueue *q;
1126 struct MeshQueue *qn;
1127 GNUNET_HashCode hash;
1130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1139 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1140 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1145 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1146 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1150 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1154 if (NULL != q->data)
1155 GNUNET_free (q->data);
1159 /* TODO cancel core transmit ready in case it was active */
1161 tree_destroy(t->tree);
1168 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1169 * client when the client disconnects.
1171 * @param cls closure (client that is disconnecting)
1172 * @param key the hash of the local tunnel id (used to access the hashmap)
1173 * @param value the value stored at the key (tunnel to destroy)
1175 * @return GNUNET_OK on success
1178 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1182 r = tunnel_destroy ((struct MeshTunnel *) value);
1187 /******************************************************************************/
1188 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1189 /******************************************************************************/
1193 * Function called to notify a client about the socket
1194 * being ready to queue more data. "buf" will be
1195 * NULL and "size" zero if the socket was closed for
1196 * writing in the meantime.
1198 * @param cls closure
1199 * @param size number of bytes available in buf
1200 * @param buf where the callee should write the message
1201 * @return number of bytes written to buf
1204 send_core_create_path (void *cls, size_t size, void *buf)
1206 struct MeshPathInfo *info = cls;
1207 struct GNUNET_MESH_ManipulatePath *msg;
1208 struct GNUNET_PeerIdentity *peer_ptr;
1209 struct MeshPeerInfo *peer = info->peer;
1210 struct MeshTunnel *t = info->t;
1211 struct MeshPeerPath *p = info->path;
1216 sizeof (struct GNUNET_MESH_ManipulatePath) +
1217 p->length * sizeof (struct GNUNET_PeerIdentity);
1219 if (size < size_needed || NULL == buf)
1221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
1222 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1223 GNUNET_TIME_UNIT_FOREVER_REL,
1224 path_get_first_hop (t->tree, peer->id),
1225 size_needed, &send_core_create_path,
1230 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1231 msg->header.size = htons (size_needed);
1232 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1233 msg->tid = ntohl (t->id.tid);
1235 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1236 for (i = 0; i < p->length; i++)
1238 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1250 * Function called to notify a client about the socket
1251 * being ready to queue more data. "buf" will be
1252 * NULL and "size" zero if the socket was closed for
1253 * writing in the meantime.
1255 * @param cls closure (MeshDataDescriptor with all info to build packet)
1256 * @param size number of bytes available in buf
1257 * @param buf where the callee should write the message
1258 * @return number of bytes written to buf
1261 send_core_data_to_origin (void *cls, size_t size, void *buf)
1263 struct MeshDataDescriptor *info = cls;
1264 struct GNUNET_MESH_ToOrigin *msg = buf;
1267 GNUNET_assert (NULL != info);
1268 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1269 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1271 if (total_size > size)
1273 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1274 "not enough buffer to send data to origin\n");
1277 msg->header.size = htons (total_size);
1278 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1279 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1280 msg->tid = htonl (info->origin->tid);
1281 if (0 != info->size)
1283 memcpy (&msg[1], &info[1], info->size);
1285 if (NULL != info->client)
1287 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1296 * Function called to notify a client about the socket
1297 * being ready to queue more data. "buf" will be
1298 * NULL and "size" zero if the socket was closed for
1299 * writing in the meantime.
1301 * @param cls closure (data itself)
1302 * @param size number of bytes available in buf
1303 * @param buf where the callee should write the message
1305 * @return number of bytes written to buf
1308 send_core_data_multicast (void *cls, size_t size, void *buf)
1310 struct MeshDataDescriptor *info = cls;
1311 struct GNUNET_MESH_Multicast *msg = buf;
1314 GNUNET_assert (NULL != info);
1315 GNUNET_assert (NULL != info->peer);
1316 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1317 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1319 if (total_size > size)
1322 struct GNUNET_PeerIdentity id;
1324 GNUNET_PEER_resolve(info->peer->id, &id);
1325 info->peer->infos[info->handler_n] = info;
1326 info->peer->types[info->handler_n] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1327 info->peer->core_transmit[info->handler_n] =
1328 GNUNET_CORE_notify_transmit_ready (core_handle,
1331 GNUNET_TIME_UNIT_FOREVER_REL,
1334 &send_core_data_multicast,
1338 info->peer->core_transmit[info->handler_n] = NULL;
1339 info->peer->infos[info->handler_n] = NULL;
1340 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1341 msg->header.size = htons (total_size);
1342 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1343 msg->tid = htonl (info->origin->tid);
1344 memcpy (&msg[1], info->data, info->size);
1345 if (0 == --(*info->copies))
1347 if (NULL != info->client)
1349 /* FIXME One unresponsive neighbor (who doesn't "call" tmt_rdy) can lock
1350 * the client from sending anything else to the service.
1351 * - Call receive_done after certain timeout.
1352 * - Here cancel the timeout.
1354 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1356 GNUNET_free (info->data);
1357 GNUNET_free (info->copies);
1365 * Function called to notify a client about the socket
1366 * being ready to queue more data. "buf" will be
1367 * NULL and "size" zero if the socket was closed for
1368 * writing in the meantime.
1370 * @param cls closure (MeshDataDescriptor)
1371 * @param size number of bytes available in buf
1372 * @param buf where the callee should write the message
1373 * @return number of bytes written to buf
1376 send_core_path_ack (void *cls, size_t size, void *buf)
1378 struct MeshDataDescriptor *info = cls;
1379 struct GNUNET_MESH_PathACK *msg = buf;
1381 GNUNET_assert (NULL != info);
1384 info->peer->core_transmit[info->handler_n] = NULL;
1386 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1391 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1392 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1393 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1394 msg->tid = htonl (info->origin->tid);
1395 msg->peer_id = my_full_id;
1397 /* TODO add signature */
1399 return sizeof (struct GNUNET_MESH_PathACK);
1404 * Function called to notify a client about the socket
1405 * being ready to queue more data. "buf" will be
1406 * NULL and "size" zero if the socket was closed for
1407 * writing in the meantime.
1409 * @param cls closure (data itself)
1410 * @param size number of bytes available in buf
1411 * @param buf where the callee should write the message
1412 * @return number of bytes written to buf
1415 send_core_data_raw (void *cls, size_t size, void *buf)
1417 struct GNUNET_MessageHeader *msg = cls;
1420 GNUNET_assert (NULL != msg);
1421 total_size = ntohs (msg->size);
1423 if (total_size > size)
1428 memcpy (buf, msg, total_size);
1436 * Send another peer a notification to destroy a tunnel
1437 * @param cls The tunnel to destroy
1438 * @param size Size in the buffer
1439 * @param buf Memory where to put the data to transmit
1440 * @return Size of data put in buffer
1443 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1445 struct MeshTunnel *t = cls;
1446 struct MeshClient *c;
1447 struct GNUNET_MESH_TunnelMessage *msg;
1451 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1452 /*FIXME*/ msg->header.size =
1453 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1454 msg->tunnel_id = htonl (t->id.tid);
1456 tunnel_destroy (c, t);
1457 return sizeof (struct GNUNET_MESH_TunnelMessage);
1463 * Send the message to all clients that have subscribed to its type
1465 * @param msg Pointer to the message itself
1466 * @return number of clients this message was sent to
1469 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1471 struct MeshClient *c;
1475 type = ntohs (msg->type);
1476 for (count = 0, c = clients; c != NULL; c = c->next)
1478 if (client_is_subscribed (type, c))
1481 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1491 * Notify the client that owns the tunnel that a peer has connected to it
1493 * @param t Tunnel whose owner to notify
1494 * @param id Short id of the peer that has connected
1497 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1499 struct GNUNET_MESH_PeerControl pc;
1501 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1502 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1503 pc.tunnel_id = htonl (t->local_tid);
1504 GNUNET_PEER_resolve (id, &pc.peer);
1505 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1506 &pc.header, GNUNET_NO);
1510 /******************************************************************************/
1511 /******************** MESH NETWORK HANDLERS **************************/
1512 /******************************************************************************/
1516 * Core handler for path creation
1517 * struct GNUNET_CORE_MessageHandler
1519 * @param cls closure
1520 * @param message message
1521 * @param peer peer identity this notification is about
1522 * @param atsi performance data
1523 * @return GNUNET_OK to keep the connection open,
1524 * GNUNET_SYSERR to close it (signal serious error)
1528 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1529 const struct GNUNET_MessageHeader *message,
1530 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1532 unsigned int own_pos;
1535 MESH_TunnelNumber tid;
1536 struct GNUNET_MESH_ManipulatePath *msg;
1537 struct GNUNET_PeerIdentity *pi;
1538 struct GNUNET_PeerIdentity id;
1539 GNUNET_HashCode hash;
1540 struct MeshPeerPath *path;
1541 struct MeshPeerInfo *dest_peer_info;
1542 struct MeshPeerInfo *orig_peer_info;
1543 struct MeshTunnel *t;
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546 "MESH: Received a MESH path create msg\n");
1547 size = ntohs (message->size);
1548 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1550 GNUNET_break_op (0);
1554 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1555 if (size % sizeof (struct GNUNET_PeerIdentity))
1557 GNUNET_break_op (0);
1560 size /= sizeof (struct GNUNET_PeerIdentity);
1563 GNUNET_break_op (0);
1566 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1568 tid = ntohl (msg->tid);
1569 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1570 t = tunnel_get (pi, tid);
1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1575 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1576 t->id.oid = GNUNET_PEER_intern (pi);
1578 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1580 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1582 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1583 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1591 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1592 if (NULL == dest_peer_info)
1594 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1595 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1596 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1598 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1600 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1601 if (NULL == orig_peer_info)
1603 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1604 orig_peer_info->id = GNUNET_PEER_intern (pi);
1605 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1606 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1609 path = path_new (size);
1611 for (i = 0; i < size; i++)
1613 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1614 if (path->peers[i] == myid)
1618 { /* cannot be self, must be 'not found' */
1619 /* create path: self not found in path through self */
1620 GNUNET_break_op (0);
1621 path_destroy (path);
1622 /* FIXME error. destroy tunnel? leave for timeout? */
1625 if (own_pos == size - 1)
1627 /* It is for us! Send ack. */
1628 struct MeshDataDescriptor *info;
1631 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1632 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1633 info->origin = &t->id;
1634 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1635 GNUNET_assert (info->peer);
1636 for (j = 0; info->peer->core_transmit[j]; j++)
1644 info->handler_n = j;
1645 info->peer->core_transmit[j] =
1646 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1647 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1648 sizeof (struct GNUNET_MessageHeader),
1649 &send_core_path_ack, info);
1653 /* It's for somebody else! Retransmit. */
1654 struct MeshPathInfo *path_info;
1656 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1658 path_info->path = path;
1659 path_info->peer = dest_peer_info;
1661 path_add_to_peer (dest_peer_info, path);
1662 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1663 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1664 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1665 sizeof (struct GNUNET_MessageHeader),
1666 &send_core_create_path, path_info);
1673 * Core handler for mesh network traffic going from the origin to a peer
1675 * @param cls closure
1676 * @param peer peer identity this notification is about
1677 * @param message message
1678 * @param atsi performance data
1679 * @return GNUNET_OK to keep the connection open,
1680 * GNUNET_SYSERR to close it (signal serious error)
1683 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1684 const struct GNUNET_MessageHeader *message,
1685 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1687 struct GNUNET_MESH_Unicast *msg;
1688 struct MeshTunnel *t;
1689 struct MeshPeerInfo *pi;
1692 size = ntohs (message->size);
1694 sizeof (struct GNUNET_MESH_Unicast) +
1695 sizeof (struct GNUNET_MessageHeader))
1700 msg = (struct GNUNET_MESH_Unicast *) message;
1701 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1704 /* TODO notify back: we don't know this tunnel */
1705 GNUNET_break_op (0);
1708 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1709 &msg->destination.hashPubKey);
1712 /* TODO maybe feedback, log to statistics */
1713 GNUNET_break_op (0);
1718 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1721 msg = GNUNET_malloc (size);
1722 memcpy (msg, message, size);
1723 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1724 GNUNET_TIME_UNIT_FOREVER_REL,
1725 path_get_first_hop (t->tree, pi->id),
1727 &send_core_data_raw, msg);
1733 * Core handler for mesh network traffic going from the origin to all peers
1735 * @param cls closure
1736 * @param message message
1737 * @param peer peer identity this notification is about
1738 * @param atsi performance data
1739 * @return GNUNET_OK to keep the connection open,
1740 * GNUNET_SYSERR to close it (signal serious error)
1742 * TODO: Check who we got this from, to validate route.
1745 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1746 const struct GNUNET_MessageHeader *message,
1747 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1749 struct GNUNET_MESH_Multicast *msg;
1750 struct GNUNET_PeerIdentity *id;
1751 struct MeshDataDescriptor *info;
1752 struct MeshTunnelTreeNode *n;
1753 struct MeshTunnel *t;
1754 unsigned int *copies;
1759 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
1760 if (size < sizeof (struct GNUNET_MessageHeader))
1762 GNUNET_break_op (0);
1765 msg = (struct GNUNET_MESH_Multicast *) message;
1766 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1770 /* TODO notify that we dont know that tunnel */
1771 GNUNET_break_op (0);
1775 /* Transmit to locally interested clients */
1776 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1778 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1780 n = t->tree->me->children_head;
1783 copies = GNUNET_malloc (sizeof (unsigned int));
1784 for (*copies = 0; NULL != n; n = n->next)
1786 n = t->tree->me->children_head;
1787 data = GNUNET_malloc (size);
1788 memcpy (data, &msg[1], size);
1791 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1792 info->origin = &t->id;
1795 info->copies = copies;
1796 info->client = t->client->handle;
1797 info->destination = n->peer;
1798 id = path_get_first_hop(t->tree, n->peer);
1799 info->peer = peer_info_get(id);
1800 GNUNET_assert (NULL != info->peer);
1801 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
1803 if (i == (CORE_QUEUE_SIZE - 1))
1809 info->handler_n = i;
1810 info->peer->infos[i] = info;
1811 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1812 info->peer->core_transmit[i] =
1813 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1814 GNUNET_TIME_UNIT_FOREVER_REL, id,
1815 ntohs (msg->header.size),
1816 &send_core_data_multicast, info);
1824 * Core handler for mesh network traffic
1826 * @param cls closure
1827 * @param message message
1828 * @param peer peer identity this notification is about
1829 * @param atsi performance data
1831 * @return GNUNET_OK to keep the connection open,
1832 * GNUNET_SYSERR to close it (signal serious error)
1835 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1836 const struct GNUNET_MessageHeader *message,
1837 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1839 struct GNUNET_MESH_ToOrigin *msg;
1840 struct GNUNET_PeerIdentity id;
1841 struct MeshPeerInfo *peer_info;
1842 struct MeshTunnel *t;
1845 size = ntohs (message->size);
1846 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1847 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1849 GNUNET_break_op (0);
1852 msg = (struct GNUNET_MESH_ToOrigin *) message;
1853 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1857 /* TODO notify that we dont know this tunnel (whom)? */
1861 if (t->id.oid == myid)
1863 if (NULL == t->client)
1865 /* got data packet for ownerless tunnel */
1866 GNUNET_break_op (0);
1869 /* TODO signature verification */
1870 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1874 peer_info = peer_info_get (&msg->oid);
1875 if (NULL == peer_info)
1877 /* unknown origin of tunnel */
1881 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1882 msg = GNUNET_malloc (size);
1883 memcpy (msg, message, size);
1884 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1885 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1886 &send_core_data_raw, msg);
1893 * Core handler for path ACKs
1895 * @param cls closure
1896 * @param message message
1897 * @param peer peer identity this notification is about
1898 * @param atsi performance data
1900 * @return GNUNET_OK to keep the connection open,
1901 * GNUNET_SYSERR to close it (signal serious error)
1904 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1905 const struct GNUNET_MessageHeader *message,
1906 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1908 struct GNUNET_MESH_PathACK *msg;
1909 struct MeshTunnelTreeNode *n;
1910 struct MeshPeerInfo *peer_info;
1911 struct MeshTunnel *t;
1913 msg = (struct GNUNET_MESH_PathACK *) message;
1914 t = tunnel_get (&msg->oid, msg->tid);
1917 /* TODO notify that we don't know the tunnel */
1921 /* Message for us? */
1922 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1924 if (NULL == t->client)
1926 GNUNET_break_op (0);
1929 peer_info = peer_info_get (&msg->peer_id);
1930 if (NULL == peer_info)
1932 GNUNET_break_op (0);
1935 n = tree_find_peer(t->tree->root, peer_info->id);
1938 GNUNET_break_op (0);
1941 n->status = MESH_PEER_READY;
1942 send_client_peer_connected(t, peer_info->id);
1946 peer_info = peer_info_get (&msg->oid);
1947 if (NULL == peer_info)
1949 /* If we know the tunnel, we should DEFINITELY know the peer */
1953 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1954 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1955 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1956 GNUNET_TIME_UNIT_FOREVER_REL,
1957 path_get_first_hop (t->tree,
1959 sizeof (struct GNUNET_MESH_PathACK),
1960 &send_core_data_raw, msg);
1966 * Functions to handle messages from core
1968 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1969 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1970 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1971 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1972 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1973 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1974 sizeof (struct GNUNET_MESH_PathACK)},
1980 /******************************************************************************/
1981 /**************** MESH LOCAL HANDLER HELPERS ***********************/
1982 /******************************************************************************/
1985 * deregister_app: iterator for removing each application registered by a client
1987 * @param cls closure
1988 * @param key the hash of the application id (used to access the hashmap)
1989 * @param value the value stored at the key (client)
1991 * @return GNUNET_OK on success
1994 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
1996 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2002 * notify_client_connection_failure: notify a client that the connection to the
2003 * requested remote peer is not possible (for instance, no route found)
2004 * Function called when the socket is ready to queue more data. "buf" will be
2005 * NULL and "size" zero if the socket was closed for writing in the meantime.
2007 * @param cls closure
2008 * @param size number of bytes available in buf
2009 * @param buf where the callee should write the message
2010 * @return number of bytes written to buf
2013 notify_client_connection_failure (void *cls, size_t size, void *buf)
2016 struct MeshPeerInfo *peer_info;
2017 struct GNUNET_MESH_PeerControl *msg;
2018 struct GNUNET_PeerIdentity id;
2020 if (0 == size && NULL == buf)
2022 // TODO retry? cancel?
2026 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2027 peer_info = (struct MeshPeerInfo *) cls;
2028 msg = (struct GNUNET_MESH_PeerControl *) buf;
2029 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2030 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2031 // msg->tunnel_id = htonl(peer_info->t->tid);
2032 GNUNET_PEER_resolve (peer_info->id, &id);
2033 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2041 * Send keepalive packets for a peer
2043 * @param cls Closure (tunnel for which to send the keepalive).
2044 * @param tc Notification context.
2046 * TODO: implement explicit multicast keepalive?
2049 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2051 struct MeshTunnel *t = cls;
2052 struct GNUNET_MessageHeader *payload;
2053 struct GNUNET_MESH_Multicast *msg;
2056 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2057 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2063 "MESH: sending keepalive for tunnel %d\n",
2066 size = sizeof(struct GNUNET_MESH_Multicast) +
2067 sizeof(struct GNUNET_MessageHeader);
2068 msg = GNUNET_malloc (size);
2069 msg->header.size = htons (size);
2070 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2071 msg->oid = my_full_id;
2072 msg->tid = htonl(t->id.tid);
2073 payload = (struct GNUNET_MessageHeader *) &msg[1];
2074 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2075 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2076 handle_mesh_data_multicast (NULL, &my_full_id, &msg->header, NULL);
2079 t->path_refresh_task =
2080 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2086 * Function to process paths received for a new peer addition. The recorded
2087 * paths form the initial tunnel, which can be optimized later.
2088 * Called on each result obtained for the DHT search.
2090 * @param cls closure
2091 * @param exp when will this value expire
2092 * @param key key of the result
2093 * @param type type of the result
2094 * @param size number of bytes in data
2095 * @param data pointer to the result data
2097 * TODO: re-issue the request after certain time? cancel after X results?
2100 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2101 const GNUNET_HashCode * key,
2102 const struct GNUNET_PeerIdentity *get_path,
2103 unsigned int get_path_length,
2104 const struct GNUNET_PeerIdentity *put_path,
2105 unsigned int put_path_length,
2106 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2108 struct MeshPathInfo *path_info = cls;
2109 struct MeshPathInfo *path_info_aux;
2110 struct MeshPeerPath *p;
2111 struct MeshPeerPath *aux;
2112 struct GNUNET_PeerIdentity pi;
2115 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2116 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2117 "MESH: Got results from DHT for %s\n",
2118 GNUNET_h2s_full(&pi.hashPubKey));
2119 if (NULL == get_path || NULL == put_path)
2121 if (NULL == path_info->peer->path_head)
2123 // Find ourselves some alternate initial path to the destination: retry
2124 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2125 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2126 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2127 GNUNET_BLOCK_TYPE_TEST, /* type */
2128 &pi.hashPubKey, /*key to search */
2129 4, /* replication level */
2130 GNUNET_DHT_RO_RECORD_ROUTE,
2132 0, /* xquery bits */
2133 &dht_get_id_handler,
2134 (void *) path_info);
2139 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2140 path_add_to_peer (path_info->peer, p);
2141 for (i = 0; i < path_info->peer->ntunnels; i++)
2143 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2144 aux = tree_get_path_to_peer(path_info->peer->tunnels[i]->tree,
2145 path_info->peer->id);
2146 if (aux->length > 1)
2148 struct GNUNET_PeerIdentity id;
2150 path_info_aux = GNUNET_malloc (sizeof (struct MeshPathInfo));
2151 path_info_aux->path = aux;
2152 path_info_aux->peer = path_info->peer;
2153 path_info_aux->t = path_info->t;
2154 GNUNET_PEER_resolve (p->peers[1], &id);
2155 GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
2158 GNUNET_TIME_UNIT_FOREVER_REL,
2161 sizeof (struct GNUNET_MESH_ManipulatePath)
2164 sizeof (struct GNUNET_PeerIdentity)),
2166 &send_core_create_path,
2168 path_info_aux); /* cls */
2172 send_client_peer_connected(path_info->t, myid);
2175 GNUNET_free (path_info);
2182 * Function to process paths received for a new peer addition. The recorded
2183 * paths form the initial tunnel, which can be optimized later.
2184 * Called on each result obtained for the DHT search.
2186 * @param cls closure
2187 * @param exp when will this value expire
2188 * @param key key of the result
2189 * @param type type of the result
2190 * @param size number of bytes in data
2191 * @param data pointer to the result data
2194 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2195 const GNUNET_HashCode * key,
2196 const struct GNUNET_PeerIdentity *get_path,
2197 unsigned int get_path_length,
2198 const struct GNUNET_PeerIdentity *put_path,
2199 unsigned int put_path_length,
2200 enum GNUNET_BLOCK_Type type, size_t size,
2203 const struct GNUNET_PeerIdentity *pi = data;
2204 struct GNUNET_PeerIdentity id;
2205 struct MeshTunnel *t = cls;
2206 struct MeshPeerInfo *peer_info;
2207 struct MeshPathInfo *path_info;
2208 struct MeshPeerPath *p;
2211 if (size != sizeof (struct GNUNET_PeerIdentity))
2213 GNUNET_break_op (0);
2216 GNUNET_assert (NULL != t->client);
2217 GNUNET_DHT_get_stop (t->client->dht_get_type);
2218 t->client->dht_get_type = NULL;
2219 peer_info = peer_info_get (pi);
2220 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2221 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2223 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2224 NULL == peer_info->dhtget)
2226 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2227 path_info->peer = peer_info;
2229 /* we don't have a route to the peer, let's try a direct lookup */
2230 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2232 GNUNET_TIME_UNIT_FOREVER_REL,
2234 GNUNET_BLOCK_TYPE_TEST,
2237 /* key to look up */
2239 /* replication level */
2240 GNUNET_DHT_RO_RECORD_ROUTE,
2241 /* option to dht: record route */
2243 0, /* xquery bits */
2246 path_info); /* closure */
2250 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2251 path_add_to_peer (peer_info, p);
2252 tunnel_add_peer(t, peer_info);
2253 p = tree_get_path_to_peer(t->tree, peer_info->id);
2255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2256 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2257 t->local_tid, p->length);
2258 for (i = 0; i < p->length; i++)
2260 GNUNET_PEER_resolve (p->peers[0], &id);
2261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2262 GNUNET_h2s_full (&id.hashPubKey));
2268 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2270 path_info->peer = peer_info;
2271 path_info->path = p;
2272 GNUNET_PEER_resolve (p->peers[1], &id);
2273 GNUNET_CORE_notify_transmit_ready (core_handle,
2279 GNUNET_TIME_UNIT_FOREVER_REL,
2283 sizeof (struct GNUNET_MESH_ManipulatePath)
2286 sizeof (struct GNUNET_PeerIdentity)),
2288 &send_core_create_path,
2290 path_info); /* cls */
2294 send_client_peer_connected(t, myid);
2297 /******************************************************************************/
2298 /********************* MESH LOCAL HANDLES **************************/
2299 /******************************************************************************/
2303 * Handler for client disconnection
2305 * @param cls closure
2306 * @param client identification of the client; NULL
2307 * for the last call when the server is destroyed
2310 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2312 struct MeshClient *c;
2313 struct MeshClient *next;
2315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2321 if (c->handle != client && NULL != client)
2323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2328 if (NULL != c->tunnels)
2330 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2331 &tunnel_destroy_iterator,
2333 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2336 /* deregister clients applications */
2337 if (NULL != c->apps)
2339 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2340 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2342 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2343 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2345 GNUNET_SCHEDULER_cancel (announce_applications_task);
2346 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2348 if (NULL != c->types)
2349 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2350 if (NULL != c->dht_get_type)
2351 GNUNET_DHT_get_stop (c->dht_get_type);
2352 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2364 * Handler for new clients
2366 * @param cls closure
2367 * @param client identification of the client
2368 * @param message the actual message, which includes messages the client wants
2371 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2372 const struct GNUNET_MessageHeader *message)
2374 struct GNUNET_MESH_ClientConnect *cc_msg;
2375 struct MeshClient *c;
2376 GNUNET_MESH_ApplicationType *a;
2383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2384 /* Check data sanity */
2385 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2386 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2387 ntypes = ntohs (cc_msg->types);
2388 napps = ntohs (cc_msg->applications);
2390 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2393 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2397 /* Create new client structure */
2398 c = GNUNET_malloc (sizeof (struct MeshClient));
2400 c->id = next_client_id++;
2403 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2406 GNUNET_MESH_ApplicationType at;
2409 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2410 for (i = 0; i < napps; i++)
2413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2414 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2415 /* store in clients hashmap */
2416 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2417 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2418 /* store in global hashmap, for announcements */
2419 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2420 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2422 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2423 announce_applications_task =
2424 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2432 t = (uint16_t *) & a[napps];
2433 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2434 for (i = 0; i < ntypes; i++)
2437 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2439 /* store in clients hashmap */
2440 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2441 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2442 /* store in global hashmap */
2443 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2444 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2448 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2450 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2451 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2452 GNUNET_SERVER_notification_context_add (nc, client);
2454 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2462 * Handler for requests of new tunnels
2464 * @param cls closure
2465 * @param client identification of the client
2466 * @param message the actual message
2469 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2470 const struct GNUNET_MessageHeader *message)
2472 struct GNUNET_MESH_TunnelMessage *t_msg;
2473 struct MeshTunnel *t;
2474 struct MeshClient *c;
2475 GNUNET_HashCode hash;
2477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2479 /* Sanity check for client registration */
2480 if (NULL == (c = client_get (client)))
2483 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2490 /* Message sanity check */
2491 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2494 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2498 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2499 /* Sanity check for tunnel numbering */
2500 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2503 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2506 /* Sanity check for duplicate tunnel IDs */
2507 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2510 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2514 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2516 while (NULL != tunnel_get_by_pi (myid, next_tid))
2517 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2518 t->id.tid = next_tid++;
2520 t->local_tid = ntohl (t_msg->tunnel_id);
2522 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2524 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2526 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2527 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2530 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2534 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2536 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2537 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2540 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2543 t->tree = tree_new (t, myid);
2544 t->tree->refresh = REFRESH_PATH_TIME;
2545 t->tree->root->status = MESH_PEER_READY;
2546 t->tree->me = t->tree->root;
2548 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2554 * Handler for requests of deleting tunnels
2556 * @param cls closure
2557 * @param client identification of the client
2558 * @param message the actual message
2561 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2562 const struct GNUNET_MessageHeader *message)
2564 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2565 struct MeshClient *c;
2566 struct MeshTunnel *t;
2567 MESH_TunnelNumber tid;
2568 GNUNET_HashCode hash;
2570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2572 /* Sanity check for client registration */
2573 if (NULL == (c = client_get (client)))
2576 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2579 /* Message sanity check */
2580 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2583 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2589 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2591 /* Retrieve tunnel */
2592 tid = ntohl (tunnel_msg->tunnel_id);
2594 /* Remove from local id hashmap */
2595 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2596 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2597 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2599 /* Remove from global id hashmap */
2600 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2601 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2603 // notify_tunnel_destroy(t); FIXME
2605 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2611 * Handler for connection requests to new peers
2613 * @param cls closure
2614 * @param client identification of the client
2615 * @param message the actual message (PeerControl)
2618 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2619 const struct GNUNET_MessageHeader *message)
2621 struct GNUNET_MESH_PeerControl *peer_msg;
2622 struct MeshPathInfo *path_info;
2623 struct MeshPeerInfo *peer_info;
2624 struct MeshClient *c;
2625 struct MeshTunnel *t;
2626 MESH_TunnelNumber tid;
2628 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
2629 /* Sanity check for client registration */
2630 if (NULL == (c = client_get (client)))
2633 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2637 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2638 /* Sanity check for message size */
2639 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2642 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2646 /* Tunnel exists? */
2647 tid = ntohl (peer_msg->tunnel_id);
2648 t = tunnel_get_by_local_id (c, tid);
2652 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2656 /* Does client own tunnel? */
2657 if (t->client->handle != client)
2660 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2663 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
2664 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
2665 peer_info = peer_info_get (&peer_msg->peer);
2667 /* Start DHT search if needed, otherwise just add peer to tunnel. */
2668 if (NULL == peer_info->dhtget && NULL == peer_info->path_head)
2670 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2671 path_info->peer = peer_info;
2673 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
2674 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2675 GNUNET_BLOCK_TYPE_TEST, /* type */
2676 &peer_msg->peer.hashPubKey, /*key to search */
2677 4, /* replication level */
2678 GNUNET_DHT_RO_RECORD_ROUTE,
2680 0, /* xquery bits */
2681 &dht_get_id_handler,
2682 (void *) path_info);
2684 else if (NULL != peer_info->path_head)
2687 for (i = 0; i < CORE_QUEUE_SIZE; i++)
2689 if (NULL == peer_info->core_transmit[i])
2692 if (CORE_QUEUE_SIZE == i)
2695 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2698 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2699 path_info->peer = peer_info;
2701 tunnel_add_peer(t, peer_info);
2702 path_info->path = tree_get_path_to_peer(t->tree, peer_info->id);
2703 peer_info = peer_info_get(path_get_first_hop(t->tree, path_info->peer->id));
2704 peer_info->infos[i] = path_info;
2705 peer_info->types[i] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
2706 peer_info->core_transmit[i] =
2707 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2708 GNUNET_TIME_UNIT_FOREVER_REL,
2709 path_get_first_hop(t->tree,
2710 path_info->peer->id),
2711 sizeof (struct GNUNET_MessageHeader),
2712 &send_core_create_path, path_info);
2714 /* Otherwise: there is no path yet, but there is a DHT_get active already. */
2715 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2721 * Handler for disconnection requests of peers in a tunnel
2723 * @param cls closure
2724 * @param client identification of the client
2725 * @param message the actual message (PeerControl)
2728 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2729 const struct GNUNET_MessageHeader *message)
2731 struct GNUNET_MESH_PeerControl *peer_msg;
2732 struct MeshClient *c;
2733 struct MeshTunnel *t;
2734 MESH_TunnelNumber tid;
2736 /* Sanity check for client registration */
2737 if (NULL == (c = client_get (client)))
2740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2743 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2744 /* Sanity check for message size */
2745 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2748 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2752 /* Tunnel exists? */
2753 tid = ntohl (peer_msg->tunnel_id);
2754 t = tunnel_get_by_local_id (c, tid);
2758 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2762 /* Does client own tunnel? */
2763 if (t->client->handle != client)
2766 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2770 /* Ok, delete peer from tunnel */
2771 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2772 &peer_msg->peer.hashPubKey);
2774 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2780 * Handler for connection requests to new peers by type
2782 * @param cls closure
2783 * @param client identification of the client
2784 * @param message the actual message (ConnectPeerByType)
2787 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2788 const struct GNUNET_MessageHeader *message)
2790 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2791 struct MeshClient *c;
2792 struct MeshTunnel *t;
2793 GNUNET_HashCode hash;
2794 GNUNET_MESH_ApplicationType type;
2795 MESH_TunnelNumber tid;
2797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2798 /* Sanity check for client registration */
2799 if (NULL == (c = client_get (client)))
2802 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2806 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2807 /* Sanity check for message size */
2808 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2809 ntohs (connect_msg->header.size))
2812 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2816 /* Tunnel exists? */
2817 tid = ntohl (connect_msg->tunnel_id);
2818 t = tunnel_get_by_local_id (c, tid);
2822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2826 /* Does client own tunnel? */
2827 if (t->client->handle != client)
2830 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2834 /* Do WE have the service? */
2835 type = ntohl (connect_msg->type);
2836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2837 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2838 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2841 /* Yes! Fast forward, add ourselves to the tunnel and send the
2842 * good news to the client
2844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2845 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2846 peer_info_get (&my_full_id),
2847 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2850 send_client_peer_connected(t, myid);
2851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2852 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2855 /* Ok, lets find a peer offering the service */
2856 if (c->dht_get_type)
2858 GNUNET_DHT_get_stop (c->dht_get_type);
2860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2861 GNUNET_h2s_full (&hash));
2863 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2864 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2865 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0,
2866 &dht_get_type_handler, t);
2868 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2874 * Handler for client traffic directed to one peer
2876 * @param cls closure
2877 * @param client identification of the client
2878 * @param message the actual message
2881 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2882 const struct GNUNET_MessageHeader *message)
2884 struct MeshClient *c;
2885 struct MeshTunnel *t;
2886 struct MeshPeerInfo *pi;
2887 struct GNUNET_MESH_Unicast *data_msg;
2888 MESH_TunnelNumber tid;
2891 /* Sanity check for client registration */
2892 if (NULL == (c = client_get (client)))
2895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2898 data_msg = (struct GNUNET_MESH_Unicast *) message;
2899 /* Sanity check for message size */
2900 size = ntohs (message->size);
2901 if (sizeof (struct GNUNET_MESH_Unicast) +
2902 sizeof (struct GNUNET_MessageHeader) > size)
2905 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2909 /* Tunnel exists? */
2910 tid = ntohl (data_msg->tid);
2911 t = tunnel_get_by_local_id (c, tid);
2915 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2919 /* Is it a local tunnel? Then, does client own the tunnel? */
2920 if (t->client->handle != NULL && t->client->handle != client)
2923 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2927 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2928 &data_msg->destination.hashPubKey);
2929 /* Is the selected peer in the tunnel? */
2933 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2937 /* Ok, everything is correct, send the message
2938 * (pretend we got it from a mesh peer)
2941 char buf[ntohs (message->size)];
2942 struct GNUNET_MESH_Unicast *copy;
2944 /* Work around const limitation */
2945 copy = (struct GNUNET_MESH_Unicast *) buf;
2946 memcpy (buf, data_msg, size);
2947 copy->oid = my_full_id;
2948 copy->tid = htonl (t->id.tid);
2949 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
2951 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2956 * Handler for client traffic directed to all peers in a tunnel
2958 * @param cls closure
2959 * @param client identification of the client
2960 * @param message the actual message
2963 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2964 const struct GNUNET_MessageHeader *message)
2966 struct MeshClient *c;
2967 struct MeshTunnel *t;
2968 struct GNUNET_MESH_Multicast *data_msg;
2969 MESH_TunnelNumber tid;
2971 /* Sanity check for client registration */
2972 if (NULL == (c = client_get (client)))
2975 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2978 data_msg = (struct GNUNET_MESH_Multicast *) message;
2979 /* Sanity check for message size */
2980 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2983 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2987 /* Tunnel exists? */
2988 tid = ntohl (data_msg->tid);
2989 t = tunnel_get_by_local_id (c, tid);
2993 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2997 /* Does client own tunnel? */
2998 if (t->client->handle != client)
3001 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3006 char buf[ntohs(message->size)];
3007 struct GNUNET_MESH_Multicast *copy;
3009 copy = (struct GNUNET_MESH_Multicast *)buf;
3010 memcpy(buf, message, ntohs(message->size));
3011 copy->oid = my_full_id;
3012 copy->tid = htonl(t->id.tid);
3013 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3016 /* receive done gets called when last copy is sent */
3021 * Functions to handle messages from clients
3023 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3024 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3025 {&handle_local_tunnel_create, NULL,
3026 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3027 sizeof (struct GNUNET_MESH_TunnelMessage)},
3028 {&handle_local_tunnel_destroy, NULL,
3029 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3030 sizeof (struct GNUNET_MESH_TunnelMessage)},
3031 {&handle_local_connect_add, NULL,
3032 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3033 sizeof (struct GNUNET_MESH_PeerControl)},
3034 {&handle_local_connect_del, NULL,
3035 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3036 sizeof (struct GNUNET_MESH_PeerControl)},
3037 {&handle_local_connect_by_type, NULL,
3038 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3039 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3040 {&handle_local_unicast, NULL,
3041 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3042 {&handle_local_unicast, NULL,
3043 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3044 {&handle_local_multicast, NULL,
3045 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3051 * To be called on core init/fail.
3053 * @param cls service closure
3054 * @param server handle to the server for this service
3055 * @param identity the public identity of this peer
3056 * @param publicKey the public key of this peer
3059 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3060 const struct GNUNET_PeerIdentity *identity,
3061 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
3063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3064 core_handle = server;
3065 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3067 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3068 GNUNET_SCHEDULER_shutdown();
3074 * Method called whenever a given peer connects.
3076 * @param cls closure
3077 * @param peer peer identity this notification is about
3078 * @param atsi performance data for the connection
3081 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3082 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3084 struct MeshPeerInfo *peer_info;
3085 struct MeshPeerPath *path;
3087 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3088 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3089 // GNUNET_h2s(&my_full_id.hashPubKey));
3090 peer_info = peer_info_get (peer);
3091 if (myid == peer_info->id)
3093 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3098 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3099 // GNUNET_h2s(&peer->hashPubKey));
3101 path = path_new (2);
3102 path->peers[0] = myid;
3103 path->peers[1] = peer_info->id;
3104 path_add_to_peer (peer_info, path);
3109 * Method called whenever a peer disconnects.
3111 * @param cls closure
3112 * @param peer peer identity this notification is about
3115 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3117 struct MeshPeerInfo *pi;
3120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3121 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3127 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3129 if (pi->core_transmit[i])
3131 struct MeshDataDescriptor *dd;
3132 struct MeshPathInfo *path_info;
3133 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3134 /* TODO: notify that tranmission has failed */
3135 switch (pi->types[i])
3137 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3138 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3139 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3141 if (0 == --(*dd->copies))
3143 GNUNET_free (dd->copies);
3144 GNUNET_free (dd->data);
3147 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3148 path_info = pi->infos[i];
3149 path_destroy(path_info->path);
3152 GNUNET_free (pi->infos[i]);
3155 path_remove_from_peer (pi, pi->id, myid);
3158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3164 /******************************************************************************/
3165 /************************ MAIN FUNCTIONS ****************************/
3166 /******************************************************************************/
3169 * Task run during shutdown.
3175 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3177 struct MeshClient *c;
3179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3180 if (core_handle != NULL)
3182 GNUNET_CORE_disconnect (core_handle);
3185 if (dht_handle != NULL)
3187 for (c = clients; NULL != c; c = c->next)
3188 if (NULL != c->dht_get_type)
3189 GNUNET_DHT_get_stop (c->dht_get_type);
3190 GNUNET_DHT_disconnect (dht_handle);
3195 GNUNET_SERVER_notification_context_destroy (nc);
3198 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3200 GNUNET_SCHEDULER_cancel (announce_id_task);
3201 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3207 * Process mesh requests.
3209 * @param cls closure
3210 * @param server the initialized server
3211 * @param c configuration to use
3214 run (void *cls, struct GNUNET_SERVER_Handle *server,
3215 const struct GNUNET_CONFIGURATION_Handle *c)
3217 struct MeshPeerInfo *peer;
3218 struct MeshPeerPath *p;
3221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3222 server_handle = server;
3223 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3224 CORE_QUEUE_SIZE, /* queue size */
3225 NULL, /* Closure passed to MESH functions */
3226 &core_init, /* Call core_init once connected */
3227 &core_connect, /* Handle connects */
3228 &core_disconnect, /* remove peers on disconnects */
3229 NULL, /* Do we care about "status" updates? */
3230 NULL, /* Don't notify about all incoming messages */
3231 GNUNET_NO, /* For header only in notification */
3232 NULL, /* Don't notify about all outbound messages */
3233 GNUNET_NO, /* For header-only out notification */
3234 core_handlers); /* Register these handlers */
3235 if (core_handle == NULL)
3238 GNUNET_SCHEDULER_shutdown ();
3243 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3248 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3249 GNUNET_SCHEDULER_shutdown ();
3252 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3253 GNUNET_free (keyfile);
3254 if (my_private_key == NULL)
3256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3257 _("Mesh service could not access hostkey. Exiting.\n"));
3258 GNUNET_SCHEDULER_shutdown ();
3261 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3262 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3263 &my_full_id.hashPubKey);
3264 myid = GNUNET_PEER_intern (&my_full_id);
3266 dht_handle = GNUNET_DHT_connect (c, 64);
3267 if (dht_handle == NULL)
3269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3270 Running without DHT has a severe\
3271 impact in MESH capabilities.\n\
3272 Plase check your configuretion and enable DHT.\n");
3278 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3279 peers = GNUNET_CONTAINER_multihashmap_create (32);
3280 applications = GNUNET_CONTAINER_multihashmap_create (32);
3281 types = GNUNET_CONTAINER_multihashmap_create (32);
3283 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3284 nc = GNUNET_SERVER_notification_context_create (server_handle,
3286 GNUNET_SERVER_disconnect_notify (server_handle,
3287 &handle_local_client_disconnect,
3292 clients_tail = NULL;
3297 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3298 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3300 /* Create a peer_info for the local peer */
3301 peer = peer_info_get(&my_full_id);
3304 path_add_to_peer(peer, p);
3306 /* Scheduled the task to clean up when shutdown is called */
3307 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3314 * The main function for the mesh service.
3316 * @param argc number of arguments from the command line
3317 * @param argv command line arguments
3318 * @return 0 ok, 1 on error
3321 main (int argc, char *const *argv)
3326 // fprintf (stderr, "main ()\n");
3328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3331 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3335 // fprintf (stderr, "main () END\n");