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,\
65 #define GET_RESTART_TIME GNUNET_TIME_relative_multiply(\
66 GNUNET_TIME_UNIT_SECONDS,\
69 /******************************************************************************/
70 /************************ DATA STRUCTURES ****************************/
71 /******************************************************************************/
73 /** FWD declaration */
77 * Struct containing all info possibly needed to build a package when called
80 struct MeshDataDescriptor
82 /** ID of the tunnel this packet travels in */
83 struct MESH_TunnelID *origin;
88 /** Client that asked for the transmission, if any */
89 struct GNUNET_SERVER_Client *client;
91 /** Who was this message being sent to */
92 struct MeshPeerInfo *peer;
94 /** Ultimate destination of the packet */
95 GNUNET_PEER_Id destination;
97 /** Number of identical messages sent to different hops (multicast) */
100 /** Which handler was used to request the transmission */
101 unsigned int handler_n;
103 /** Size of the data */
110 * Struct containing all information regarding a given peer
120 * Last time we heard from this peer
122 struct GNUNET_TIME_Absolute last_contact;
125 * Number of attempts to reconnect so far
127 int n_reconnect_attempts;
130 * Paths to reach the peer, ordered by ascending hop count
132 struct MeshPeerPath *path_head;
135 * Paths to reach the peer, ordered by ascending hop count
137 struct MeshPeerPath *path_tail;
140 * Handle to stop the DHT search for a path to this peer
142 struct GNUNET_DHT_GetHandle *dhtget;
145 * Task to reissue the DHT get request a path for this peer
147 GNUNET_SCHEDULER_TaskIdentifier dhtget_task;
150 * Handles to stop queued transmissions for this peer
152 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
155 * Pointer to info stuctures used as cls for queued transmissions
157 void *infos[CORE_QUEUE_SIZE];
160 * Type of message being in each transmission
162 uint16_t types[CORE_QUEUE_SIZE];
165 * Array of tunnels this peer participates in
166 * (most probably a small amount, therefore not a hashmap)
167 * When the path to the peer changes, notify these tunnels to let them
168 * re-adjust their path trees.
170 struct MeshTunnel **tunnels;
173 * Number of tunnels above
175 unsigned int ntunnels;
180 * Data scheduled to transmit (to local client or remote peer)
187 struct MeshQueue *next;
188 struct MeshQueue *prev;
191 * Target of the data (NULL if target is client)
193 struct MeshPeerInfo *peer;
196 * Client to send the data to (NULL if target is peer)
198 struct MeshClient *client;
201 * Size of the message to transmit
206 * How old is the data?
208 struct GNUNET_TIME_Absolute timestamp;
213 struct GNUNET_MessageHeader *data;
217 * Globally unique tunnel identification (owner + number)
218 * DO NOT USE OVER THE NETWORK
223 * Node that owns the tunnel
228 * Tunnel number to differentiate all the tunnels owned by the node oid
229 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
231 MESH_TunnelNumber tid;
235 struct MeshClient; /* FWD declaration */
238 * Struct containing all information regarding a tunnel
239 * For an intermediate node the improtant info used will be:
240 * - id Tunnel unique identification
241 * - paths[0] To know where to send it next
242 * - metainfo: ready, speeds, accounting
249 struct MESH_TunnelID id;
252 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
254 MESH_TunnelNumber local_tid;
257 * Last time the tunnel was used
259 struct GNUNET_TIME_Absolute timestamp;
262 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
264 struct GNUNET_CONTAINER_MultiHashMap *peers;
267 * Number of peers that are connected and potentially ready to receive data
269 unsigned int peers_ready;
272 * Number of peers that have been added to the tunnel
274 unsigned int peers_total;
277 * Client owner of the tunnel, if any
279 struct MeshClient *client;
282 * Messages ready to transmit
284 struct MeshQueue *queue_head;
285 struct MeshQueue *queue_tail;
290 struct MeshTunnelTree *tree;
293 * Application type we are looking for in this tunnel
295 GNUNET_MESH_ApplicationType type;
298 * Used to search peers offering a service
300 struct GNUNET_DHT_GetHandle *dht_get_type;
303 * Task to reissue the DHT get request for a type
305 GNUNET_SCHEDULER_TaskIdentifier dht_get_type_task;
308 * Task to keep the used paths alive
310 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
315 * Info needed to work with tunnel paths and peers
322 struct MeshTunnel *t;
327 struct MeshPeerInfo *peer;
332 struct MeshPeerPath *path;
337 * Struct containing information about a client of the service
344 struct MeshClient *next;
345 struct MeshClient *prev;
348 * Tunnels that belong to this client, indexed by local id
350 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
353 * Handle to communicate with the client
355 struct GNUNET_SERVER_Client *handle;
358 * Applications that this client has claimed to provide
360 struct GNUNET_CONTAINER_MultiHashMap *apps;
363 * Messages that this client has declared interest in
365 struct GNUNET_CONTAINER_MultiHashMap *types;
369 * ID of the client, for debug messages
378 /******************************************************************************/
379 /************************ DEBUG FUNCTIONS ****************************/
380 /******************************************************************************/
384 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
385 * @param cls string to print
386 * @param tc task context
389 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
393 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
401 /******************************************************************************/
402 /*********************** GLOBAL VARIABLES ****************************/
403 /******************************************************************************/
408 static struct MeshClient *clients;
409 static struct MeshClient *clients_tail;
412 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
414 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
417 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
419 static struct GNUNET_CONTAINER_MultiHashMap *peers;
422 * Handle to communicate with core
424 static struct GNUNET_CORE_Handle *core_handle;
429 static struct GNUNET_DHT_Handle *dht_handle;
434 static struct GNUNET_SERVER_Handle *server_handle;
437 * Notification context, to send messages to local clients
439 static struct GNUNET_SERVER_NotificationContext *nc;
442 * Local peer own ID (memory efficient handle)
444 static GNUNET_PEER_Id myid;
447 * Local peer own ID (full value)
449 static struct GNUNET_PeerIdentity my_full_id;
454 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
459 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
462 * Tunnel ID for the next created tunnel (global tunnel number)
464 static MESH_TunnelNumber next_tid;
467 * Tunnel ID for the next incoming tunnel (local tunnel number)
469 static MESH_TunnelNumber next_local_tid;
472 * All application types provided by this peer
474 static struct GNUNET_CONTAINER_MultiHashMap *applications;
477 * All message types clients of this peer are interested in
479 static struct GNUNET_CONTAINER_MultiHashMap *types;
482 * Task to periodically announce provided applications
484 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
487 * Task to periodically announce itself in the network
489 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
492 unsigned int next_client_id;
496 /******************************************************************************/
497 /************************ ITERATORS ****************************/
498 /******************************************************************************/
500 /* FIXME move iterators here */
503 /******************************************************************************/
504 /************************ PERIODIC FUNCTIONS ****************************/
505 /******************************************************************************/
508 * Announce iterator over for each application provided by the peer
511 * @param key current key code
512 * @param value value in the hash map
513 * @return GNUNET_YES if we should continue to
518 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
520 /* FIXME are hashes in multihash map equal on all aquitectures? */
522 GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
523 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
524 (const char *) &my_full_id,
526 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
527 &mesh_debug, "DHT_put for app completed");
529 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
531 APP_ANNOUNCE_TIME, NULL, NULL);
538 * Periodically announce what applications are provided by local clients
541 * @param tc task context
544 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
546 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
548 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
552 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
554 announce_applications_task =
555 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
563 * Periodically announce self id in the DHT
566 * @param tc task context
569 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
571 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
573 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
577 * - Set data expiration in function of X
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
581 GNUNET_h2s_full (&my_full_id.hashPubKey));
582 GNUNET_DHT_put (dht_handle, /* DHT handle */
583 &my_full_id.hashPubKey, /* Key to use */
584 10U, /* Replication level */
585 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
586 GNUNET_BLOCK_TYPE_TEST, /* Block type */
587 sizeof(my_full_id), /* Size of the data */
588 (char *)&my_full_id, /* Data itself */
589 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
590 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
592 &mesh_debug, "DHT_put for id completed");
594 NULL, /* Continuation */
595 NULL); /* Continuation closure */
598 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
603 * Function to process paths received for a new peer addition. The recorded
604 * paths form the initial tunnel, which can be optimized later.
605 * Called on each result obtained for the DHT search.
608 * @param exp when will this value expire
609 * @param key key of the result
610 * @param type type of the result
611 * @param size number of bytes in data
612 * @param data pointer to the result data
615 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
616 const GNUNET_HashCode * key,
617 const struct GNUNET_PeerIdentity *get_path,
618 unsigned int get_path_length,
619 const struct GNUNET_PeerIdentity *put_path,
620 unsigned int put_path_length,
621 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
624 * Task to restart the DHT get search for a peer.
630 dht_get_restart (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
634 /******************************************************************************/
635 /****************** GENERAL HELPER FUNCTIONS ************************/
636 /******************************************************************************/
639 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
640 * and insert it in the appropiate structures if the peer is not known yet.
642 * @param peer Identity of the peer
644 * @return Existing or newly created peer info
646 static struct MeshPeerInfo *
647 peer_info_get (const struct GNUNET_PeerIdentity *peer)
649 struct MeshPeerInfo *peer_info;
651 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
652 if (NULL == peer_info)
655 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
656 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
657 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
658 peer_info->id = GNUNET_PEER_intern (peer);
667 * Destroy the peer_info and free any allocated resources linked to it
668 * @param t tunnel the path belongs to
669 * @param pi the peer_info to destroy
670 * @return GNUNET_OK on success
673 peer_info_destroy (struct MeshPeerInfo *pi)
675 GNUNET_HashCode hash;
676 struct GNUNET_PeerIdentity id;
678 GNUNET_PEER_resolve (pi->id, &id);
679 GNUNET_PEER_change_rc (pi->id, -1);
680 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
682 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
683 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
691 * Notify a tunnel that a connection has broken that affects at least
694 * @param t Tunnel affected.
695 * @param peer Peer that (at least) has been affected by the disconnection.
696 * @param p1 Peer that got disconnected from p2.
697 * @param p2 Peer that got disconnected from p1.
699 * @return Short ID of the peer disconnected (either p1 or p2).
700 * 0 if the tunnel remained unaffected.
702 static GNUNET_PEER_Id
703 tunnel_notify_connection_broken (struct MeshTunnel *t,
704 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
708 * Remove all paths that rely on a direct connection between p1 and p2
709 * from the peer itself and notify all tunnels about it.
711 * @param peer PeerInfo of affected peer.
712 * @param p1 GNUNET_PEER_Id of one peer.
713 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
716 * TODO: optimize (see below)
719 path_remove_from_peer (struct MeshPeerInfo *peer,
723 struct GNUNET_PeerIdentity id;
724 struct MeshPeerPath *p;
725 struct MeshPeerPath *aux;
726 struct MeshPeerInfo *peer_d;
728 unsigned int destroyed;
738 for (i = 0; i < (p->length - 1); i++)
740 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
741 (p->peers[i] == p2 && p->peers[i + 1] == p1))
753 for (i = 0; i < peer->ntunnels; i++)
755 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
757 * Problem: one or more peers have been deleted from the tunnel tree.
758 * We don't know who they are to try to add them again.
759 * We need to try to find a new path for each of the disconnected peers.
760 * Some of them might already have a path to reach them that does not
761 * involve p1 and p2. Adding all anew might render in a better tree than
762 * the trivial immediate fix.
764 * Trivial immiediate fix: try to reconnect to the disconnected node. All
765 * its children will be reachable trough him.
767 GNUNET_PEER_resolve(d, &id);
768 peer_d = peer_info_get(&id);
771 for (p = peer_d->path_head; NULL != p; p = p->next)
773 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
781 /* No callback, as peer will be already disconnected */
782 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
786 struct MeshPathInfo *path_info;
788 if (NULL != peer_d->dhtget)
790 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
792 path_info->peer = peer_d;
793 path_info->t = peer->tunnels[i];
794 peer_d->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
795 GNUNET_TIME_UNIT_SECONDS, /* timeout */
796 GNUNET_BLOCK_TYPE_TEST, /* type */
797 &id.hashPubKey, /*key to search */
798 4, /* replication level */
799 GNUNET_DHT_RO_RECORD_ROUTE,
804 peer_d->dhtget_task = GNUNET_SCHEDULER_add_delayed(GET_RESTART_TIME,
813 * Add the path to the peer and update the path used to reach it in case this
816 * @param peer_info Destination peer to add the path to.
817 * @param path New path to add. Last peer must be the peer in arg 1.
818 * Path will be either used of freed if already known.
820 * TODO: trim the part from origin to us? Add it as path to origin?
823 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
825 struct MeshPeerPath *aux;
829 if (NULL == peer_info || NULL == path)
835 l = path_get_length (path);
837 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
839 l2 = path_get_length (aux);
842 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
843 peer_info->path_tail, aux, path);
847 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
854 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
861 * Add the path to the origin peer and update the path used to reach it in case
862 * this is the shortest.
863 * The path is given in peer_info -> destination, therefore we turn the path
866 * @param peer_info Peer to add the path to, being the origin of the path.
867 * @param path New path to add after being inversed.
870 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
873 path_add_to_peer (peer_info, path);
878 * Build a PeerPath from the paths returned from the DHT, reversing the paths
879 * to obtain a local peer -> destination path and interning the peer ids.
881 * @return Newly allocated and created path
883 static struct MeshPeerPath *
884 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
885 unsigned int get_path_length,
886 const struct GNUNET_PeerIdentity *put_path,
887 unsigned int put_path_length)
889 struct MeshPeerPath *p;
895 GNUNET_PEER_change_rc(myid, 1);
897 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
898 for (i--; i >= 0; i--)
900 id = GNUNET_PEER_intern (&get_path[i]);
901 if (p->length > 0 && id == p->peers[p->length - 1])
903 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
904 GNUNET_PEER_change_rc(id, -1);
909 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
910 p->peers[p->length] = id;
914 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
915 for (i--; i >= 0; i--)
917 id = GNUNET_PEER_intern (&put_path[i]);
920 /* PUT path went through us, so discard the path up until now and start
921 * from here to get a much shorter (and loop-free) path.
926 if (p->length > 0 && id == p->peers[p->length - 1])
928 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
929 GNUNET_PEER_change_rc(id, -1);
934 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
935 p->peers[p->length] = id;
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
940 "MESH: (first of GET: %s)\n",
941 GNUNET_h2s_full(&get_path[0].hashPubKey));
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943 "MESH: (first of PUT: %s)\n",
944 GNUNET_h2s_full(&put_path[0].hashPubKey));
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
946 "MESH: In total: %d hops\n",
954 * Send keepalive packets for a peer
956 * @param cls Closure (tunnel for which to send the keepalive).
957 * @param tc Notification context.
959 * TODO: implement explicit multicast keepalive?
962 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
966 * Check if client has registered with the service and has not disconnected
968 * @param client the client to check
970 * @return non-NULL if client exists in the global DLL
972 static struct MeshClient *
973 client_get (struct GNUNET_SERVER_Client *client)
975 struct MeshClient *c;
980 if (c->handle == client)
989 * Checks if a given client has subscribed to certain message type
991 * @param message_type Type of message to check
992 * @param c Client to check
994 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
999 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1003 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1004 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1009 * Search for a tunnel among the tunnels for a client
1011 * @param c the client whose tunnels to search in
1012 * @param tid the local id of the tunnel
1014 * @return tunnel handler, NULL if doesn't exist
1016 static struct MeshTunnel *
1017 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1019 GNUNET_HashCode hash;
1021 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1022 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1027 * Search for a tunnel by global ID using PEER_ID
1029 * @param pi owner of the tunnel
1030 * @param tid global tunnel number
1032 * @return tunnel handler, NULL if doesn't exist
1034 static struct MeshTunnel *
1035 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1037 struct MESH_TunnelID id;
1038 GNUNET_HashCode hash;
1043 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1044 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1049 * Search for a tunnel by global ID using full PeerIdentities
1051 * @param oid owner of the tunnel
1052 * @param tid global tunnel number
1054 * @return tunnel handler, NULL if doesn't exist
1056 static struct MeshTunnel *
1057 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1059 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1064 * Callback used to notify a client owner of a tunnel that a peer has
1065 * disconnected, most likely because of a path change.
1067 * @param n Node in the tree representing the disconnected peer
1070 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1072 struct GNUNET_MESH_PeerControl msg;
1074 if (NULL == n->t->client || NULL == nc)
1077 msg.header.size = htons (sizeof (msg));
1078 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1079 msg.tunnel_id = htonl (n->t->local_tid);
1080 GNUNET_PEER_resolve (n->peer, &msg.peer);
1081 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1082 &msg.header, GNUNET_NO);
1087 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1088 * needed rescources.
1090 * @param t Tunnel we want to add a new peer to
1091 * @param peer PeerInfo of the peer being added
1095 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1097 struct MeshPeerPath *p;
1098 struct MeshPeerPath *best_p;
1099 unsigned int best_cost;
1102 if (NULL != tree_find_peer(t->tree->root, peer->id))
1104 /* Already have it, nothing to do. */
1109 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1110 if (NULL == (p = peer->path_head))
1117 best_cost = UINT_MAX;
1120 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1127 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1128 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1129 t->path_refresh_task =
1130 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1136 * Notify a tunnel that a connection has broken that affects at least
1137 * some of its peers.
1139 * @param t Tunnel affected.
1140 * @param peer Peer that (at least) has been affected by the disconnection.
1141 * @param p1 Peer that got disconnected from p2.
1142 * @param p2 Peer that got disconnected from p1.
1144 * @return Short ID of the peer disconnected (either p1 or p2).
1145 * 0 if the tunnel remained unaffected.
1147 static GNUNET_PEER_Id
1148 tunnel_notify_connection_broken (struct MeshTunnel *t,
1149 struct MeshPeerInfo *peer,
1153 return tree_notify_connection_broken (t->tree, p1, p2,
1154 ¬ify_peer_disconnected);
1159 * Destroy the tunnel and free any allocated resources linked to it
1161 * @param t the tunnel to destroy
1163 * @return GNUNET_OK on success
1166 tunnel_destroy (struct MeshTunnel *t)
1168 struct MeshClient *c;
1169 struct MeshQueue *q;
1170 struct MeshQueue *qn;
1171 GNUNET_HashCode hash;
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1183 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1184 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1189 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1190 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1194 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1198 if (NULL != q->data)
1199 GNUNET_free (q->data);
1203 /* TODO cancel core transmit ready in case it was active */
1205 tree_destroy(t->tree);
1212 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1213 * client when the client disconnects.
1215 * @param cls closure (client that is disconnecting)
1216 * @param key the hash of the local tunnel id (used to access the hashmap)
1217 * @param value the value stored at the key (tunnel to destroy)
1219 * @return GNUNET_OK on success
1222 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1224 struct MeshTunnel *t = value;
1227 if (NULL != t->dht_get_type)
1229 GNUNET_SCHEDULER_cancel (t->dht_get_type_task);
1230 GNUNET_DHT_get_stop (t->dht_get_type);
1232 r = tunnel_destroy (t);
1237 /******************************************************************************/
1238 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1239 /******************************************************************************/
1243 * Function called to notify a client about the socket
1244 * being ready to queue more data. "buf" will be
1245 * NULL and "size" zero if the socket was closed for
1246 * writing in the meantime.
1248 * @param cls closure
1249 * @param size number of bytes available in buf
1250 * @param buf where the callee should write the message
1251 * @return number of bytes written to buf
1254 send_core_create_path (void *cls, size_t size, void *buf)
1256 struct MeshPathInfo *info = cls;
1257 struct GNUNET_MESH_ManipulatePath *msg;
1258 struct GNUNET_PeerIdentity *peer_ptr;
1259 struct MeshPeerInfo *peer = info->peer;
1260 struct MeshTunnel *t = info->t;
1261 struct MeshPeerPath *p = info->path;
1266 sizeof (struct GNUNET_MESH_ManipulatePath) +
1267 p->length * sizeof (struct GNUNET_PeerIdentity);
1269 if (size < size_needed || NULL == buf)
1271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
1275 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1276 GNUNET_TIME_UNIT_FOREVER_REL,
1277 path_get_first_hop (t->tree, peer->id),
1279 &send_core_create_path,
1284 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1285 msg->header.size = htons (size_needed);
1286 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1287 msg->tid = ntohl (t->id.tid);
1289 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1290 for (i = 0; i < p->length; i++)
1292 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1299 "MESH: CREATE PATH (%u bytes long) sent!\n",
1307 * Function called to notify a client about the socket
1308 * being ready to queue more data. "buf" will be
1309 * NULL and "size" zero if the socket was closed for
1310 * writing in the meantime.
1312 * @param cls closure (MeshDataDescriptor with all info to build packet)
1313 * @param size number of bytes available in buf
1314 * @param buf where the callee should write the message
1315 * @return number of bytes written to buf
1318 send_core_data_to_origin (void *cls, size_t size, void *buf)
1320 struct MeshDataDescriptor *info = cls;
1321 struct GNUNET_MESH_ToOrigin *msg = buf;
1324 GNUNET_assert (NULL != info);
1325 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1326 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1328 if (total_size > size)
1330 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1331 "not enough buffer to send data to origin\n");
1334 msg->header.size = htons (total_size);
1335 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1336 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1337 msg->tid = htonl (info->origin->tid);
1338 if (0 != info->size)
1340 memcpy (&msg[1], &info[1], info->size);
1342 if (NULL != info->client)
1344 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1353 * Function called to notify a client about the socket
1354 * being ready to queue more data. "buf" will be
1355 * NULL and "size" zero if the socket was closed for
1356 * writing in the meantime.
1358 * @param cls closure (data itself)
1359 * @param size number of bytes available in buf
1360 * @param buf where the callee should write the message
1362 * @return number of bytes written to buf
1365 send_core_data_multicast (void *cls, size_t size, void *buf)
1367 struct MeshDataDescriptor *info = cls;
1368 struct GNUNET_MESH_Multicast *msg = buf;
1371 GNUNET_assert (NULL != info);
1372 GNUNET_assert (NULL != info->peer);
1373 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1374 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1376 if (total_size > size)
1379 struct GNUNET_PeerIdentity id;
1381 GNUNET_PEER_resolve(info->peer->id, &id);
1382 info->peer->infos[info->handler_n] = info;
1383 info->peer->types[info->handler_n] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1384 info->peer->core_transmit[info->handler_n] =
1385 GNUNET_CORE_notify_transmit_ready (core_handle,
1388 GNUNET_TIME_UNIT_FOREVER_REL,
1391 &send_core_data_multicast,
1395 info->peer->core_transmit[info->handler_n] = NULL;
1396 info->peer->infos[info->handler_n] = NULL;
1397 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1398 msg->header.size = htons (total_size);
1399 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1400 msg->tid = htonl (info->origin->tid);
1401 memcpy (&msg[1], info->data, info->size);
1402 if (0 == --(*info->copies))
1404 if (NULL != info->client)
1406 /* FIXME One unresponsive neighbor (who doesn't "call" tmt_rdy) can lock
1407 * the client from sending anything else to the service.
1408 * - Call receive_done after certain timeout.
1409 * - Here cancel the timeout.
1411 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1413 GNUNET_free (info->data);
1414 GNUNET_free (info->copies);
1422 * Function called to notify a client about the socket
1423 * being ready to queue more data. "buf" will be
1424 * NULL and "size" zero if the socket was closed for
1425 * writing in the meantime.
1427 * @param cls closure (MeshDataDescriptor)
1428 * @param size number of bytes available in buf
1429 * @param buf where the callee should write the message
1430 * @return number of bytes written to buf
1433 send_core_path_ack (void *cls, size_t size, void *buf)
1435 struct MeshDataDescriptor *info = cls;
1436 struct GNUNET_MESH_PathACK *msg = buf;
1438 GNUNET_assert (NULL != info);
1441 info->peer->core_transmit[info->handler_n] = NULL;
1443 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1448 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1449 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1450 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1451 msg->tid = htonl (info->origin->tid);
1452 msg->peer_id = my_full_id;
1454 /* TODO add signature */
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
1457 return sizeof (struct GNUNET_MESH_PathACK);
1462 * Function called to notify a client about the socket
1463 * being ready to queue more data. "buf" will be
1464 * NULL and "size" zero if the socket was closed for
1465 * writing in the meantime.
1467 * @param cls closure (data itself)
1468 * @param size number of bytes available in buf
1469 * @param buf where the callee should write the message
1470 * @return number of bytes written to buf
1473 send_core_data_raw (void *cls, size_t size, void *buf)
1475 struct GNUNET_MessageHeader *msg = cls;
1478 GNUNET_assert (NULL != msg);
1479 total_size = ntohs (msg->size);
1481 if (total_size > size)
1486 memcpy (buf, msg, total_size);
1494 * Send another peer a notification to destroy a tunnel
1495 * @param cls The tunnel to destroy
1496 * @param size Size in the buffer
1497 * @param buf Memory where to put the data to transmit
1498 * @return Size of data put in buffer
1501 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1503 struct MeshTunnel *t = cls;
1504 struct MeshClient *c;
1505 struct GNUNET_MESH_TunnelMessage *msg;
1509 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1510 /*FIXME*/ msg->header.size =
1511 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1512 msg->tunnel_id = htonl (t->id.tid);
1514 tunnel_destroy (c, t);
1515 return sizeof (struct GNUNET_MESH_TunnelMessage);
1521 * Send the message to all clients that have subscribed to its type
1523 * @param msg Pointer to the message itself
1524 * @return number of clients this message was sent to
1527 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1529 struct MeshClient *c;
1533 type = ntohs (msg->type);
1534 for (count = 0, c = clients; c != NULL; c = c->next)
1536 if (client_is_subscribed (type, c))
1539 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1549 * Notify the client that owns the tunnel that a peer has connected to it
1551 * @param t Tunnel whose owner to notify
1552 * @param id Short id of the peer that has connected
1555 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1557 struct GNUNET_MESH_PeerControl pc;
1559 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1560 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1561 pc.tunnel_id = htonl (t->local_tid);
1562 GNUNET_PEER_resolve (id, &pc.peer);
1563 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1564 &pc.header, GNUNET_NO);
1568 /******************************************************************************/
1569 /******************** MESH NETWORK HANDLERS **************************/
1570 /******************************************************************************/
1574 * Core handler for path creation
1575 * struct GNUNET_CORE_MessageHandler
1577 * @param cls closure
1578 * @param message message
1579 * @param peer peer identity this notification is about
1580 * @param atsi performance data
1581 * @return GNUNET_OK to keep the connection open,
1582 * GNUNET_SYSERR to close it (signal serious error)
1586 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1587 const struct GNUNET_MessageHeader *message,
1588 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1590 unsigned int own_pos;
1593 MESH_TunnelNumber tid;
1594 struct GNUNET_MESH_ManipulatePath *msg;
1595 struct GNUNET_PeerIdentity *pi;
1596 struct GNUNET_PeerIdentity id;
1597 GNUNET_HashCode hash;
1598 struct MeshPeerPath *path;
1599 struct MeshPeerInfo *dest_peer_info;
1600 struct MeshPeerInfo *orig_peer_info;
1601 struct MeshTunnel *t;
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604 "MESH: Received a path create msg\n");
1605 size = ntohs (message->size);
1606 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1608 GNUNET_break_op (0);
1612 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1613 if (size % sizeof (struct GNUNET_PeerIdentity))
1615 GNUNET_break_op (0);
1618 size /= sizeof (struct GNUNET_PeerIdentity);
1621 GNUNET_break_op (0);
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1625 "MESH: path as %u hops.\n",
1627 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1629 tid = ntohl (msg->tid);
1630 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1631 t = tunnel_get (pi, tid);
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1633 "MESH: path is for tunnel %s [%X].\n",
1638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1639 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1640 t->id.oid = GNUNET_PEER_intern (pi);
1642 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1643 t->local_tid = next_local_tid++;
1644 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
1646 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1648 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1649 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1656 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1657 if (NULL == dest_peer_info)
1659 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1660 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1661 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1663 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1665 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1666 if (NULL == orig_peer_info)
1668 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1669 orig_peer_info->id = GNUNET_PEER_intern (pi);
1670 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1671 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1674 path = path_new (size);
1676 for (i = 0; i < size; i++)
1678 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1679 if (path->peers[i] == myid)
1683 { /* cannot be self, must be 'not found' */
1684 /* create path: self not found in path through self */
1685 GNUNET_break_op (0);
1686 path_destroy (path);
1687 /* FIXME error. destroy tunnel? leave for timeout? */
1690 if (own_pos == size - 1)
1692 /* It is for us! Send ack. */
1693 struct GNUNET_MESH_TunnelNotification cmsg;
1694 struct MeshDataDescriptor *info;
1697 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1698 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1699 info->origin = &t->id;
1700 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1701 GNUNET_assert (NULL != info->peer);
1702 for (j = 0; info->peer->core_transmit[j]; j++)
1704 if (j == (CORE_QUEUE_SIZE - 1))
1710 info->handler_n = j;
1711 info->peer->core_transmit[j] =
1712 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1713 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1714 sizeof (struct GNUNET_MESH_PathACK),
1715 &send_core_path_ack, info);
1716 cmsg.header.size = htons(sizeof(cmsg));
1717 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1718 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
1719 cmsg.tunnel_id = htonl(t->local_tid);
1720 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
1724 /* It's for somebody else! Retransmit. */
1725 struct MeshPathInfo *path_info;
1727 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1729 path_info->path = path;
1730 path_info->peer = dest_peer_info;
1732 path_add_to_peer (dest_peer_info, path);
1733 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1734 GNUNET_CORE_notify_transmit_ready (
1738 GNUNET_TIME_UNIT_FOREVER_REL,
1740 sizeof (struct GNUNET_MESH_ManipulatePath) +
1741 path->length * sizeof(struct GNUNET_PeerIdentity),
1742 &send_core_create_path,
1750 * Core handler for mesh network traffic going from the origin to a peer
1752 * @param cls closure
1753 * @param peer peer identity this notification is about
1754 * @param message message
1755 * @param atsi performance data
1756 * @return GNUNET_OK to keep the connection open,
1757 * GNUNET_SYSERR to close it (signal serious error)
1760 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1761 const struct GNUNET_MessageHeader *message,
1762 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1764 struct GNUNET_MESH_Unicast *msg;
1765 struct MeshTunnel *t;
1766 struct MeshPeerInfo *pi;
1769 size = ntohs (message->size);
1771 sizeof (struct GNUNET_MESH_Unicast) +
1772 sizeof (struct GNUNET_MessageHeader))
1777 msg = (struct GNUNET_MESH_Unicast *) message;
1778 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1781 /* TODO notify back: we don't know this tunnel */
1782 GNUNET_break_op (0);
1785 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1786 &msg->destination.hashPubKey);
1789 /* TODO maybe feedback, log to statistics */
1790 GNUNET_break_op (0);
1795 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1798 msg = GNUNET_malloc (size);
1799 memcpy (msg, message, size);
1800 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1801 GNUNET_TIME_UNIT_FOREVER_REL,
1802 path_get_first_hop (t->tree, pi->id),
1804 &send_core_data_raw, msg);
1810 * Core handler for mesh network traffic going from the origin to all peers
1812 * @param cls closure
1813 * @param message message
1814 * @param peer peer identity this notification is about
1815 * @param atsi performance data
1816 * @return GNUNET_OK to keep the connection open,
1817 * GNUNET_SYSERR to close it (signal serious error)
1819 * TODO: Check who we got this from, to validate route.
1822 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1823 const struct GNUNET_MessageHeader *message,
1824 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1826 struct GNUNET_MESH_Multicast *msg;
1827 struct GNUNET_PeerIdentity *id;
1828 struct MeshDataDescriptor *info;
1829 struct MeshTunnelTreeNode *n;
1830 struct MeshTunnel *t;
1831 unsigned int *copies;
1836 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
1837 if (size < sizeof (struct GNUNET_MessageHeader))
1839 GNUNET_break_op (0);
1842 msg = (struct GNUNET_MESH_Multicast *) message;
1843 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1847 /* TODO notify that we dont know that tunnel */
1848 GNUNET_break_op (0);
1852 /* Transmit to locally interested clients */
1853 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1855 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1857 n = t->tree->me->children_head;
1860 copies = GNUNET_malloc (sizeof (unsigned int));
1861 for (*copies = 0; NULL != n; n = n->next)
1863 n = t->tree->me->children_head;
1864 data = GNUNET_malloc (size);
1865 memcpy (data, &msg[1], size);
1868 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1869 info->origin = &t->id;
1872 info->copies = copies;
1873 info->client = t->client->handle;
1874 info->destination = n->peer;
1875 id = path_get_first_hop(t->tree, n->peer);
1876 info->peer = peer_info_get(id);
1877 GNUNET_assert (NULL != info->peer);
1878 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
1880 if (i == (CORE_QUEUE_SIZE - 1))
1886 info->handler_n = i;
1887 info->peer->infos[i] = info;
1888 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1889 info->peer->core_transmit[i] =
1890 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1891 GNUNET_TIME_UNIT_FOREVER_REL, id,
1892 ntohs (msg->header.size),
1893 &send_core_data_multicast, info);
1901 * Core handler for mesh network traffic
1903 * @param cls closure
1904 * @param message message
1905 * @param peer peer identity this notification is about
1906 * @param atsi performance data
1908 * @return GNUNET_OK to keep the connection open,
1909 * GNUNET_SYSERR to close it (signal serious error)
1912 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1913 const struct GNUNET_MessageHeader *message,
1914 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1916 struct GNUNET_MESH_ToOrigin *msg;
1917 struct GNUNET_PeerIdentity id;
1918 struct MeshPeerInfo *peer_info;
1919 struct MeshTunnel *t;
1922 size = ntohs (message->size);
1923 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1924 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1926 GNUNET_break_op (0);
1929 msg = (struct GNUNET_MESH_ToOrigin *) message;
1930 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1934 /* TODO notify that we dont know this tunnel (whom)? */
1938 if (t->id.oid == myid)
1940 if (NULL == t->client)
1942 /* got data packet for ownerless tunnel */
1943 GNUNET_break_op (0);
1946 /* TODO signature verification */
1947 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1951 peer_info = peer_info_get (&msg->oid);
1952 if (NULL == peer_info)
1954 /* unknown origin of tunnel */
1958 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1959 msg = GNUNET_malloc (size);
1960 memcpy (msg, message, size);
1961 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1962 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1963 &send_core_data_raw, msg);
1970 * Core handler for path ACKs
1972 * @param cls closure
1973 * @param message message
1974 * @param peer peer identity this notification is about
1975 * @param atsi performance data
1977 * @return GNUNET_OK to keep the connection open,
1978 * GNUNET_SYSERR to close it (signal serious error)
1981 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1982 const struct GNUNET_MessageHeader *message,
1983 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1985 struct GNUNET_MESH_PathACK *msg;
1986 struct MeshTunnelTreeNode *n;
1987 struct MeshPeerInfo *peer_info;
1988 struct MeshTunnel *t;
1990 msg = (struct GNUNET_MESH_PathACK *) message;
1991 t = tunnel_get (&msg->oid, msg->tid);
1994 /* TODO notify that we don't know the tunnel */
1998 /* Message for us? */
1999 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2001 if (NULL == t->client)
2003 GNUNET_break_op (0);
2006 peer_info = peer_info_get (&msg->peer_id);
2007 if (NULL == peer_info)
2009 GNUNET_break_op (0);
2012 n = tree_find_peer(t->tree->root, peer_info->id);
2015 GNUNET_break_op (0);
2018 n->status = MESH_PEER_READY;
2019 send_client_peer_connected(t, peer_info->id);
2023 peer_info = peer_info_get (&msg->oid);
2024 if (NULL == peer_info)
2026 /* If we know the tunnel, we should DEFINITELY know the peer */
2030 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2031 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2032 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2033 GNUNET_TIME_UNIT_FOREVER_REL,
2034 path_get_first_hop (t->tree,
2036 sizeof (struct GNUNET_MESH_PathACK),
2037 &send_core_data_raw, msg);
2043 * Functions to handle messages from core
2045 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2046 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2047 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2048 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2049 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2050 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2051 sizeof (struct GNUNET_MESH_PathACK)},
2057 /******************************************************************************/
2058 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2059 /******************************************************************************/
2062 * deregister_app: iterator for removing each application registered by a client
2064 * @param cls closure
2065 * @param key the hash of the application id (used to access the hashmap)
2066 * @param value the value stored at the key (client)
2068 * @return GNUNET_OK on success
2071 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2073 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2079 * notify_client_connection_failure: notify a client that the connection to the
2080 * requested remote peer is not possible (for instance, no route found)
2081 * Function called when the socket is ready to queue more data. "buf" will be
2082 * NULL and "size" zero if the socket was closed for writing in the meantime.
2084 * @param cls closure
2085 * @param size number of bytes available in buf
2086 * @param buf where the callee should write the message
2087 * @return number of bytes written to buf
2090 notify_client_connection_failure (void *cls, size_t size, void *buf)
2093 struct MeshPeerInfo *peer_info;
2094 struct GNUNET_MESH_PeerControl *msg;
2095 struct GNUNET_PeerIdentity id;
2097 if (0 == size && NULL == buf)
2099 // TODO retry? cancel?
2103 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2104 peer_info = (struct MeshPeerInfo *) cls;
2105 msg = (struct GNUNET_MESH_PeerControl *) buf;
2106 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2107 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2108 // msg->tunnel_id = htonl(peer_info->t->tid);
2109 GNUNET_PEER_resolve (peer_info->id, &id);
2110 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2118 * Send keepalive packets for a peer
2120 * @param cls Closure (tunnel for which to send the keepalive).
2121 * @param tc Notification context.
2123 * TODO: implement explicit multicast keepalive?
2126 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2128 struct MeshTunnel *t = cls;
2129 struct GNUNET_MessageHeader *payload;
2130 struct GNUNET_MESH_Multicast *msg;
2133 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2134 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2140 "MESH: sending keepalive for tunnel %d\n",
2143 size = sizeof(struct GNUNET_MESH_Multicast) +
2144 sizeof(struct GNUNET_MessageHeader);
2145 msg = GNUNET_malloc (size);
2146 msg->header.size = htons (size);
2147 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2148 msg->oid = my_full_id;
2149 msg->tid = htonl(t->id.tid);
2150 payload = (struct GNUNET_MessageHeader *) &msg[1];
2151 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2152 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2153 handle_mesh_data_multicast (NULL, &my_full_id, &msg->header, NULL);
2156 t->path_refresh_task =
2157 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2163 * Function to process paths received for a new peer addition. The recorded
2164 * paths form the initial tunnel, which can be optimized later.
2165 * Called on each result obtained for the DHT search.
2167 * @param cls closure
2168 * @param exp when will this value expire
2169 * @param key key of the result
2170 * @param type type of the result
2171 * @param size number of bytes in data
2172 * @param data pointer to the result data
2174 * TODO: re-issue the request after certain time? cancel after X results?
2177 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2178 const GNUNET_HashCode * key,
2179 const struct GNUNET_PeerIdentity *get_path,
2180 unsigned int get_path_length,
2181 const struct GNUNET_PeerIdentity *put_path,
2182 unsigned int put_path_length,
2183 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2185 struct MeshPathInfo *path_info = cls;
2186 struct MeshPathInfo *path_info_aux;
2187 struct MeshPeerPath *p;
2188 struct MeshPeerPath *aux;
2189 struct GNUNET_PeerIdentity pi;
2192 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2193 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2194 "MESH: Got results from DHT for %s\n",
2195 GNUNET_h2s_full(&pi.hashPubKey));
2196 GNUNET_SCHEDULER_cancel(path_info->peer->dhtget_task);
2197 GNUNET_DHT_get_stop(path_info->peer->dhtget);
2198 path_info->peer->dhtget = NULL;
2199 if (NULL == get_path || NULL == put_path)
2201 if (NULL == path_info->peer->path_head)
2203 // Find ourselves some alternate initial path to the destination: retry
2204 path_info->peer->dhtget_task = GNUNET_SCHEDULER_add_now (&dht_get_restart,
2210 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2211 path_add_to_peer (path_info->peer, p);
2212 for (i = 0; i < path_info->peer->ntunnels; i++)
2214 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2215 aux = tree_get_path_to_peer(path_info->peer->tunnels[i]->tree,
2216 path_info->peer->id);
2217 if (aux->length > 1)
2219 struct GNUNET_PeerIdentity id;
2221 path_info_aux = GNUNET_malloc (sizeof (struct MeshPathInfo));
2222 path_info_aux->path = aux;
2223 path_info_aux->peer = path_info->peer;
2224 path_info_aux->t = path_info->t;
2225 GNUNET_PEER_resolve (p->peers[1], &id);
2226 GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
2229 GNUNET_TIME_UNIT_FOREVER_REL,
2232 sizeof (struct GNUNET_MESH_ManipulatePath)
2235 sizeof (struct GNUNET_PeerIdentity)),
2237 &send_core_create_path,
2239 path_info_aux); /* cls */
2243 send_client_peer_connected(path_info->t, myid);
2246 GNUNET_free (path_info);
2253 * Task to restart the DHT get search for a peer.
2259 dht_get_restart (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2261 struct MeshPathInfo *path = cls;
2262 struct MeshPeerInfo *peer = path->peer;
2263 struct GNUNET_PeerIdentity id;
2265 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2267 GNUNET_PEER_resolve(peer->id, &id);
2268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2269 "MESH: restarting DHT get for id %s [%u]\n",
2272 if (NULL != peer->dhtget)
2273 GNUNET_DHT_get_stop(peer->dhtget);
2274 peer->dhtget = GNUNET_DHT_get_start (dht_handle,
2275 GNUNET_TIME_UNIT_FOREVER_REL,
2276 GNUNET_BLOCK_TYPE_TEST,
2279 GNUNET_DHT_RO_RECORD_ROUTE,
2282 &dht_get_id_handler,
2284 peer->dhtget_task = GNUNET_SCHEDULER_add_delayed (GET_RESTART_TIME,
2291 * Function to process paths received for a new peer addition. The recorded
2292 * paths form the initial tunnel, which can be optimized later.
2293 * Called on each result obtained for the DHT search.
2295 * @param cls closure
2296 * @param exp when will this value expire
2297 * @param key key of the result
2298 * @param type type of the result
2299 * @param size number of bytes in data
2300 * @param data pointer to the result data
2303 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2304 const GNUNET_HashCode * key,
2305 const struct GNUNET_PeerIdentity *get_path,
2306 unsigned int get_path_length,
2307 const struct GNUNET_PeerIdentity *put_path,
2308 unsigned int put_path_length,
2309 enum GNUNET_BLOCK_Type type, size_t size,
2312 const struct GNUNET_PeerIdentity *pi = data;
2313 struct GNUNET_PeerIdentity id;
2314 struct MeshTunnel *t = cls;
2315 struct MeshPeerInfo *peer_info;
2316 struct MeshPathInfo *path_info;
2317 struct MeshPeerPath *p;
2320 if (size != sizeof (struct GNUNET_PeerIdentity))
2322 GNUNET_break_op (0);
2325 GNUNET_assert (NULL != t->client);
2326 GNUNET_DHT_get_stop (t->dht_get_type); /* FIXME move to task? */
2327 t->dht_get_type = NULL;
2328 peer_info = peer_info_get (pi);
2329 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2330 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2332 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2333 NULL == peer_info->dhtget)
2335 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2336 path_info->peer = peer_info;
2338 /* we don't have a route to the peer, let's try a direct lookup */
2339 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2341 GNUNET_TIME_UNIT_SECONDS,
2343 GNUNET_BLOCK_TYPE_TEST,
2346 /* key to look up */
2348 /* replication level */
2349 GNUNET_DHT_RO_RECORD_ROUTE,
2350 /* option to dht: record route */
2352 0, /* xquery bits */
2355 path_info); /* closure */
2356 peer_info->dhtget_task = GNUNET_SCHEDULER_add_delayed (GET_RESTART_TIME,
2362 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2363 path_add_to_peer (peer_info, p);
2364 tunnel_add_peer(t, peer_info);
2365 p = tree_get_path_to_peer(t->tree, peer_info->id);
2367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2368 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2369 t->local_tid, p->length);
2370 for (i = 0; i < p->length; i++)
2372 GNUNET_PEER_resolve (p->peers[0], &id);
2373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2374 GNUNET_h2s_full (&id.hashPubKey));
2380 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2382 path_info->peer = peer_info;
2383 path_info->path = p;
2384 GNUNET_PEER_resolve (p->peers[1], &id);
2385 GNUNET_CORE_notify_transmit_ready (core_handle,
2391 GNUNET_TIME_UNIT_FOREVER_REL,
2395 sizeof (struct GNUNET_MESH_ManipulatePath)
2398 sizeof (struct GNUNET_PeerIdentity)),
2400 &send_core_create_path,
2402 path_info); /* cls */
2406 send_client_peer_connected(t, myid);
2411 * Task to restart the DHT get search for a peer offering a type.
2417 dht_get_type_restart (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2419 struct MeshTunnel *t = cls;
2420 GNUNET_HashCode hash;
2422 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2425 "MESH: restarting DHT get for type %u\n",
2427 GNUNET_DHT_get_stop(t->dht_get_type);
2428 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2429 t->dht_get_type = GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2430 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2431 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0,
2432 &dht_get_type_handler, t);
2433 t->dht_get_type_task = GNUNET_SCHEDULER_add_delayed(GET_RESTART_TIME,
2434 &dht_get_type_restart,
2438 /******************************************************************************/
2439 /********************* MESH LOCAL HANDLES **************************/
2440 /******************************************************************************/
2444 * Handler for client disconnection
2446 * @param cls closure
2447 * @param client identification of the client; NULL
2448 * for the last call when the server is destroyed
2451 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2453 struct MeshClient *c;
2454 struct MeshClient *next;
2456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2462 if (c->handle != client && NULL != client)
2464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2469 if (NULL != c->tunnels)
2471 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2472 &tunnel_destroy_iterator,
2474 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2477 /* deregister clients applications */
2478 if (NULL != c->apps)
2480 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2481 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2483 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2484 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2486 GNUNET_SCHEDULER_cancel (announce_applications_task);
2487 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2489 if (NULL != c->types)
2490 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2491 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2503 * Handler for new clients
2505 * @param cls closure
2506 * @param client identification of the client
2507 * @param message the actual message, which includes messages the client wants
2510 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2511 const struct GNUNET_MessageHeader *message)
2513 struct GNUNET_MESH_ClientConnect *cc_msg;
2514 struct MeshClient *c;
2515 GNUNET_MESH_ApplicationType *a;
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2523 /* Check data sanity */
2524 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2525 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2526 ntypes = ntohs (cc_msg->types);
2527 napps = ntohs (cc_msg->applications);
2529 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2532 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2536 /* Create new client structure */
2537 c = GNUNET_malloc (sizeof (struct MeshClient));
2539 c->id = next_client_id++;
2542 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2545 GNUNET_MESH_ApplicationType at;
2548 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2549 for (i = 0; i < napps; i++)
2552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2553 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2554 /* store in clients hashmap */
2555 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2556 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2557 /* store in global hashmap, for announcements */
2558 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2559 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2561 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2562 announce_applications_task =
2563 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2571 t = (uint16_t *) & a[napps];
2572 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2573 for (i = 0; i < ntypes; i++)
2576 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2578 /* store in clients hashmap */
2579 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2580 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2581 /* store in global hashmap */
2582 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2583 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2587 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2589 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2590 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2591 GNUNET_SERVER_notification_context_add (nc, client);
2593 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2601 * Handler for requests of new tunnels
2603 * @param cls closure
2604 * @param client identification of the client
2605 * @param message the actual message
2608 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2609 const struct GNUNET_MessageHeader *message)
2611 struct GNUNET_MESH_TunnelMessage *t_msg;
2612 struct MeshTunnel *t;
2613 struct MeshClient *c;
2614 GNUNET_HashCode hash;
2616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2618 /* Sanity check for client registration */
2619 if (NULL == (c = client_get (client)))
2622 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2629 /* Message sanity check */
2630 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2633 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2637 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2638 /* Sanity check for tunnel numbering */
2639 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2642 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2645 /* Sanity check for duplicate tunnel IDs */
2646 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2649 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2653 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2655 while (NULL != tunnel_get_by_pi (myid, next_tid))
2656 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2657 t->id.tid = next_tid++;
2659 t->local_tid = ntohl (t_msg->tunnel_id);
2661 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2663 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2665 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2666 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2669 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2673 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2675 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2676 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2679 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2682 t->tree = tree_new (t, myid);
2683 t->tree->refresh = REFRESH_PATH_TIME;
2684 t->tree->root->status = MESH_PEER_READY;
2685 t->tree->me = t->tree->root;
2687 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2693 * Handler for requests of deleting tunnels
2695 * @param cls closure
2696 * @param client identification of the client
2697 * @param message the actual message
2700 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2701 const struct GNUNET_MessageHeader *message)
2703 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2704 struct MeshClient *c;
2705 struct MeshTunnel *t;
2706 MESH_TunnelNumber tid;
2707 GNUNET_HashCode hash;
2709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2711 /* Sanity check for client registration */
2712 if (NULL == (c = client_get (client)))
2715 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2718 /* Message sanity check */
2719 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2722 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2728 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2730 /* Retrieve tunnel */
2731 tid = ntohl (tunnel_msg->tunnel_id);
2733 /* Remove from local id hashmap */
2734 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2735 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2736 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2738 /* Remove from global id hashmap */
2739 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2740 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2742 // notify_tunnel_destroy(t); FIXME
2744 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2750 * Handler for connection requests to new peers
2752 * @param cls closure
2753 * @param client identification of the client
2754 * @param message the actual message (PeerControl)
2757 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2758 const struct GNUNET_MessageHeader *message)
2760 struct GNUNET_MESH_PeerControl *peer_msg;
2761 struct MeshPathInfo *path_info;
2762 struct MeshPeerInfo *peer_info;
2763 struct MeshClient *c;
2764 struct MeshTunnel *t;
2765 MESH_TunnelNumber tid;
2767 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
2768 /* Sanity check for client registration */
2769 if (NULL == (c = client_get (client)))
2772 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2776 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2777 /* Sanity check for message size */
2778 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2785 /* Tunnel exists? */
2786 tid = ntohl (peer_msg->tunnel_id);
2787 t = tunnel_get_by_local_id (c, tid);
2791 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2795 /* Does client own tunnel? */
2796 if (t->client->handle != client)
2799 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2802 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
2803 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
2804 peer_info = peer_info_get (&peer_msg->peer);
2806 /* Start DHT search if needed, otherwise just add peer to tunnel. */
2807 if (NULL == peer_info->dhtget && NULL == peer_info->path_head)
2809 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2810 path_info->peer = peer_info;
2812 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
2813 GNUNET_TIME_UNIT_SECONDS, /* timeout */
2814 GNUNET_BLOCK_TYPE_TEST, /* type */
2815 &peer_msg->peer.hashPubKey, /*key to search */
2816 4, /* replication level */
2817 GNUNET_DHT_RO_RECORD_ROUTE,
2819 0, /* xquery bits */
2820 &dht_get_id_handler,
2821 (void *) path_info);
2822 peer_info->dhtget_task = GNUNET_SCHEDULER_add_delayed (GET_RESTART_TIME,
2826 else if (NULL != peer_info->path_head)
2829 for (i = 0; i < CORE_QUEUE_SIZE; i++)
2831 if (NULL == peer_info->core_transmit[i])
2834 if (CORE_QUEUE_SIZE == i)
2837 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2840 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2841 path_info->peer = peer_info;
2843 tunnel_add_peer(t, peer_info);
2844 path_info->path = tree_get_path_to_peer(t->tree, peer_info->id);
2845 peer_info = peer_info_get(path_get_first_hop(t->tree, path_info->peer->id));
2846 peer_info->infos[i] = path_info;
2847 peer_info->types[i] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
2848 peer_info->core_transmit[i] =
2849 GNUNET_CORE_notify_transmit_ready (
2853 GNUNET_TIME_UNIT_FOREVER_REL,
2854 path_get_first_hop(t->tree, path_info->peer->id),
2855 sizeof (struct GNUNET_MESH_ManipulatePath)
2856 + path_info->path->length * sizeof(struct GNUNET_PeerIdentity),
2857 &send_core_create_path,
2860 /* Otherwise: there is no path yet, but there is a DHT_get active already. */
2861 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2867 * Handler for disconnection requests of peers in a tunnel
2869 * @param cls closure
2870 * @param client identification of the client
2871 * @param message the actual message (PeerControl)
2874 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2875 const struct GNUNET_MessageHeader *message)
2877 struct GNUNET_MESH_PeerControl *peer_msg;
2878 struct MeshClient *c;
2879 struct MeshTunnel *t;
2880 MESH_TunnelNumber tid;
2882 /* Sanity check for client registration */
2883 if (NULL == (c = client_get (client)))
2886 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2889 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2890 /* Sanity check for message size */
2891 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2894 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2898 /* Tunnel exists? */
2899 tid = ntohl (peer_msg->tunnel_id);
2900 t = tunnel_get_by_local_id (c, tid);
2904 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2908 /* Does client own tunnel? */
2909 if (t->client->handle != client)
2912 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2916 /* Ok, delete peer from tunnel */
2917 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2918 &peer_msg->peer.hashPubKey);
2920 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2926 * Handler for connection requests to new peers by type
2928 * @param cls closure
2929 * @param client identification of the client
2930 * @param message the actual message (ConnectPeerByType)
2933 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2934 const struct GNUNET_MessageHeader *message)
2936 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2937 struct MeshClient *c;
2938 struct MeshTunnel *t;
2939 GNUNET_HashCode hash;
2940 MESH_TunnelNumber tid;
2942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2943 /* Sanity check for client registration */
2944 if (NULL == (c = client_get (client)))
2947 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2951 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2952 /* Sanity check for message size */
2953 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2954 ntohs (connect_msg->header.size))
2957 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2961 /* Tunnel exists? */
2962 tid = ntohl (connect_msg->tunnel_id);
2963 t = tunnel_get_by_local_id (c, tid);
2967 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2971 /* Does client own tunnel? */
2972 if (t->client->handle != client)
2975 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2979 /* Do WE have the service? */
2980 t->type = ntohl (connect_msg->type);
2981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
2982 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2983 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2986 /* Yes! Fast forward, add ourselves to the tunnel and send the
2987 * good news to the client
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2990 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2991 peer_info_get (&my_full_id),
2992 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2995 send_client_peer_connected(t, myid);
2996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2997 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3000 /* Ok, lets find a peer offering the service */
3001 if (t->dht_get_type)
3003 GNUNET_SCHEDULER_cancel (t->dht_get_type_task);
3004 GNUNET_DHT_get_stop (t->dht_get_type);
3006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3007 GNUNET_h2s_full (&hash));
3009 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
3010 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
3011 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0,
3012 &dht_get_type_handler, t);
3013 t->dht_get_type_task = GNUNET_SCHEDULER_add_delayed(GET_RESTART_TIME,
3014 &dht_get_type_restart,
3017 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3023 * Handler for client traffic directed to one peer
3025 * @param cls closure
3026 * @param client identification of the client
3027 * @param message the actual message
3030 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3031 const struct GNUNET_MessageHeader *message)
3033 struct MeshClient *c;
3034 struct MeshTunnel *t;
3035 struct MeshPeerInfo *pi;
3036 struct GNUNET_MESH_Unicast *data_msg;
3037 MESH_TunnelNumber tid;
3040 /* Sanity check for client registration */
3041 if (NULL == (c = client_get (client)))
3044 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3047 data_msg = (struct GNUNET_MESH_Unicast *) message;
3048 /* Sanity check for message size */
3049 size = ntohs (message->size);
3050 if (sizeof (struct GNUNET_MESH_Unicast) +
3051 sizeof (struct GNUNET_MessageHeader) > size)
3054 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3058 /* Tunnel exists? */
3059 tid = ntohl (data_msg->tid);
3060 t = tunnel_get_by_local_id (c, tid);
3064 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3068 /* Is it a local tunnel? Then, does client own the tunnel? */
3069 if (t->client->handle != NULL && t->client->handle != client)
3072 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3076 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3077 &data_msg->destination.hashPubKey);
3078 /* Is the selected peer in the tunnel? */
3082 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3086 /* Ok, everything is correct, send the message
3087 * (pretend we got it from a mesh peer)
3090 char buf[ntohs (message->size)];
3091 struct GNUNET_MESH_Unicast *copy;
3093 /* Work around const limitation */
3094 copy = (struct GNUNET_MESH_Unicast *) buf;
3095 memcpy (buf, data_msg, size);
3096 copy->oid = my_full_id;
3097 copy->tid = htonl (t->id.tid);
3098 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
3100 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3105 * Handler for client traffic directed to all peers in a tunnel
3107 * @param cls closure
3108 * @param client identification of the client
3109 * @param message the actual message
3112 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3113 const struct GNUNET_MessageHeader *message)
3115 struct MeshClient *c;
3116 struct MeshTunnel *t;
3117 struct GNUNET_MESH_Multicast *data_msg;
3118 MESH_TunnelNumber tid;
3120 /* Sanity check for client registration */
3121 if (NULL == (c = client_get (client)))
3124 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3127 data_msg = (struct GNUNET_MESH_Multicast *) message;
3128 /* Sanity check for message size */
3129 if (sizeof (struct GNUNET_MESH_PeerControl) != 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 /* Does client own tunnel? */
3147 if (t->client->handle != client)
3150 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3155 char buf[ntohs(message->size)];
3156 struct GNUNET_MESH_Multicast *copy;
3158 copy = (struct GNUNET_MESH_Multicast *)buf;
3159 memcpy(buf, message, ntohs(message->size));
3160 copy->oid = my_full_id;
3161 copy->tid = htonl(t->id.tid);
3162 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3165 /* receive done gets called when last copy is sent */
3170 * Functions to handle messages from clients
3172 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3173 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3174 {&handle_local_tunnel_create, NULL,
3175 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3176 sizeof (struct GNUNET_MESH_TunnelMessage)},
3177 {&handle_local_tunnel_destroy, NULL,
3178 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3179 sizeof (struct GNUNET_MESH_TunnelMessage)},
3180 {&handle_local_connect_add, NULL,
3181 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3182 sizeof (struct GNUNET_MESH_PeerControl)},
3183 {&handle_local_connect_del, NULL,
3184 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3185 sizeof (struct GNUNET_MESH_PeerControl)},
3186 {&handle_local_connect_by_type, NULL,
3187 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3188 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3189 {&handle_local_unicast, NULL,
3190 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3191 {&handle_local_unicast, NULL,
3192 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3193 {&handle_local_multicast, NULL,
3194 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3200 * To be called on core init/fail.
3202 * @param cls service closure
3203 * @param server handle to the server for this service
3204 * @param identity the public identity of this peer
3205 * @param publicKey the public key of this peer
3208 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3209 const struct GNUNET_PeerIdentity *identity,
3210 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
3212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3213 core_handle = server;
3214 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3216 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3217 GNUNET_SCHEDULER_shutdown();
3223 * Method called whenever a given peer connects.
3225 * @param cls closure
3226 * @param peer peer identity this notification is about
3227 * @param atsi performance data for the connection
3230 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3231 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3233 struct MeshPeerInfo *peer_info;
3234 struct MeshPeerPath *path;
3236 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3237 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3238 // GNUNET_h2s(&my_full_id.hashPubKey));
3239 peer_info = peer_info_get (peer);
3240 if (myid == peer_info->id)
3242 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3247 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3248 // GNUNET_h2s(&peer->hashPubKey));
3250 path = path_new (2);
3251 path->peers[0] = myid;
3252 path->peers[1] = peer_info->id;
3253 GNUNET_PEER_change_rc(myid, 1);
3254 GNUNET_PEER_change_rc(peer_info->id, 1);
3255 path_add_to_peer (peer_info, path);
3260 * Method called whenever a peer disconnects.
3262 * @param cls closure
3263 * @param peer peer identity this notification is about
3266 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3268 struct MeshPeerInfo *pi;
3271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3272 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3278 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3280 if (pi->core_transmit[i])
3282 struct MeshDataDescriptor *dd;
3283 struct MeshPathInfo *path_info;
3284 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3285 /* TODO: notify that tranmission has failed */
3286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: data at %u\n", i);
3287 switch (pi->types[i])
3289 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3290 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3291 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
3294 if (0 == --(*dd->copies))
3296 GNUNET_free (dd->copies);
3297 GNUNET_free (dd->data);
3300 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
3302 path_info = pi->infos[i];
3303 path_destroy(path_info->path);
3306 GNUNET_free (pi->infos[i]);
3309 path_remove_from_peer (pi, pi->id, myid);
3312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3318 /******************************************************************************/
3319 /************************ MAIN FUNCTIONS ****************************/
3320 /******************************************************************************/
3323 * Task run during shutdown.
3329 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3332 /* TODO: destroy tunnels? */
3333 if (core_handle != NULL)
3335 GNUNET_CORE_disconnect (core_handle);
3338 if (dht_handle != NULL)
3340 GNUNET_DHT_disconnect (dht_handle);
3345 GNUNET_SERVER_notification_context_destroy (nc);
3348 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3350 GNUNET_SCHEDULER_cancel (announce_id_task);
3351 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3357 * Process mesh requests.
3359 * @param cls closure
3360 * @param server the initialized server
3361 * @param c configuration to use
3364 run (void *cls, struct GNUNET_SERVER_Handle *server,
3365 const struct GNUNET_CONFIGURATION_Handle *c)
3367 struct MeshPeerInfo *peer;
3368 struct MeshPeerPath *p;
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3372 server_handle = server;
3373 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3374 CORE_QUEUE_SIZE, /* queue size */
3375 NULL, /* Closure passed to MESH functions */
3376 &core_init, /* Call core_init once connected */
3377 &core_connect, /* Handle connects */
3378 &core_disconnect, /* remove peers on disconnects */
3379 NULL, /* Do we care about "status" updates? */
3380 NULL, /* Don't notify about all incoming messages */
3381 GNUNET_NO, /* For header only in notification */
3382 NULL, /* Don't notify about all outbound messages */
3383 GNUNET_NO, /* For header-only out notification */
3384 core_handlers); /* Register these handlers */
3385 if (core_handle == NULL)
3388 GNUNET_SCHEDULER_shutdown ();
3393 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3398 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3399 GNUNET_SCHEDULER_shutdown ();
3402 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3403 GNUNET_free (keyfile);
3404 if (my_private_key == NULL)
3406 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3407 _("Mesh service could not access hostkey. Exiting.\n"));
3408 GNUNET_SCHEDULER_shutdown ();
3411 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3412 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3413 &my_full_id.hashPubKey);
3414 myid = GNUNET_PEER_intern (&my_full_id);
3416 dht_handle = GNUNET_DHT_connect (c, 64);
3417 if (dht_handle == NULL)
3423 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3425 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3426 peers = GNUNET_CONTAINER_multihashmap_create (32);
3427 applications = GNUNET_CONTAINER_multihashmap_create (32);
3428 types = GNUNET_CONTAINER_multihashmap_create (32);
3430 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3431 nc = GNUNET_SERVER_notification_context_create (server_handle,
3433 GNUNET_SERVER_disconnect_notify (server_handle,
3434 &handle_local_client_disconnect,
3439 clients_tail = NULL;
3444 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3445 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3447 /* Create a peer_info for the local peer */
3448 peer = peer_info_get(&my_full_id);
3451 path_add_to_peer(peer, p);
3453 /* Scheduled the task to clean up when shutdown is called */
3454 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3461 * The main function for the mesh service.
3463 * @param argc number of arguments from the command line
3464 * @param argv command line arguments
3465 * @return 0 ok, 1 on error
3468 main (int argc, char *const *argv)
3473 // fprintf (stderr, "main ()\n");
3475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3478 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3482 // fprintf (stderr, "main () END\n");