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 struct MeshDataDescriptor *infos[CORE_QUEUE_SIZE];
152 * Array of tunnels this peer participates in
153 * (most probably a small amount, therefore not a hashmap)
154 * When the path to the peer changes, notify these tunnels to let them
155 * re-adjust their path trees.
157 struct MeshTunnel **tunnels;
160 * Number of tunnels above
162 unsigned int ntunnels;
167 * Data scheduled to transmit (to local client or remote peer)
174 struct MeshQueue *next;
175 struct MeshQueue *prev;
178 * Target of the data (NULL if target is client)
180 struct MeshPeerInfo *peer;
183 * Client to send the data to (NULL if target is peer)
185 struct MeshClient *client;
188 * Size of the message to transmit
193 * How old is the data?
195 struct GNUNET_TIME_Absolute timestamp;
200 struct GNUNET_MessageHeader *data;
204 * Globally unique tunnel identification (owner + number)
205 * DO NOT USE OVER THE NETWORK
210 * Node that owns the tunnel
215 * Tunnel number to differentiate all the tunnels owned by the node oid
216 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
218 MESH_TunnelNumber tid;
222 struct MeshClient; /* FWD declaration */
225 * Struct containing all information regarding a tunnel
226 * For an intermediate node the improtant info used will be:
227 * - id Tunnel unique identification
228 * - paths[0] To know where to send it next
229 * - metainfo: ready, speeds, accounting
236 struct MESH_TunnelID id;
239 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
241 MESH_TunnelNumber local_tid;
244 * Last time the tunnel was used
246 struct GNUNET_TIME_Absolute timestamp;
249 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
251 struct GNUNET_CONTAINER_MultiHashMap *peers;
254 * Number of peers that are connected and potentially ready to receive data
256 unsigned int peers_ready;
259 * Number of peers that have been added to the tunnel
261 unsigned int peers_total;
264 * Client owner of the tunnel, if any
266 struct MeshClient *client;
269 * Messages ready to transmit
271 struct MeshQueue *queue_head;
272 struct MeshQueue *queue_tail;
277 struct MeshTunnelTree *tree;
280 * Task to keep the used paths alive
282 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
287 * Info needed to work with tunnel paths and peers
294 struct MeshTunnel *t;
299 struct MeshPeerInfo *peer;
304 struct MeshPeerPath *path;
309 * Struct containing information about a client of the service
316 struct MeshClient *next;
317 struct MeshClient *prev;
320 * Tunnels that belong to this client, indexed by local id
322 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
325 * Handle to communicate with the client
327 struct GNUNET_SERVER_Client *handle;
330 * Applications that this client has claimed to provide
332 struct GNUNET_CONTAINER_MultiHashMap *apps;
335 * Messages that this client has declared interest in
337 struct GNUNET_CONTAINER_MultiHashMap *types;
340 * Used to search peers offering a service
342 struct GNUNET_DHT_GetHandle *dht_get_type;
346 * ID of the client, for debug messages
355 /******************************************************************************/
356 /************************ DEBUG FUNCTIONS ****************************/
357 /******************************************************************************/
361 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
362 * @param cls string to print
363 * @param tc task context
366 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
370 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
379 /******************************************************************************/
380 /*********************** GLOBAL VARIABLES ****************************/
381 /******************************************************************************/
386 static struct MeshClient *clients;
387 static struct MeshClient *clients_tail;
390 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
392 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
395 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
397 static struct GNUNET_CONTAINER_MultiHashMap *peers;
400 * Handle to communicate with core
402 static struct GNUNET_CORE_Handle *core_handle;
407 static struct GNUNET_DHT_Handle *dht_handle;
412 static struct GNUNET_SERVER_Handle *server_handle;
415 * Notification context, to send messages to local clients
417 static struct GNUNET_SERVER_NotificationContext *nc;
420 * Local peer own ID (memory efficient handle)
422 static GNUNET_PEER_Id myid;
425 * Local peer own ID (full value)
427 static struct GNUNET_PeerIdentity my_full_id;
432 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
437 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
440 * Tunnel ID for the next created tunnel (global tunnel number)
442 static MESH_TunnelNumber next_tid;
445 * All application types provided by this peer
447 static struct GNUNET_CONTAINER_MultiHashMap *applications;
450 * All message types clients of this peer are interested in
452 static struct GNUNET_CONTAINER_MultiHashMap *types;
455 * Task to periodically announce provided applications
457 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
460 * Task to periodically announce itself in the network
462 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
465 unsigned int next_client_id;
469 /******************************************************************************/
470 /************************ ITERATORS ****************************/
471 /******************************************************************************/
473 /* FIXME move iterators here */
476 /******************************************************************************/
477 /************************ PERIODIC FUNCTIONS ****************************/
478 /******************************************************************************/
481 * Announce iterator over for each application provided by the peer
484 * @param key current key code
485 * @param value value in the hash map
486 * @return GNUNET_YES if we should continue to
491 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
493 /* FIXME are hashes in multihash map equal on all aquitectures? */
494 GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
495 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
496 (const char *) &my_full_id,
498 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
499 &mesh_debug, "DHT_put for app completed");
501 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
503 APP_ANNOUNCE_TIME, NULL, NULL);
510 * Periodically announce what applications are provided by local clients
513 * @param tc task context
516 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
518 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
520 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
524 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
526 announce_applications_task =
527 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
535 * Periodically announce self id in the DHT
538 * @param tc task context
541 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
543 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
545 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
549 * - Set data expiration in function of X
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
553 GNUNET_h2s_full (&my_full_id.hashPubKey));
554 GNUNET_DHT_put (dht_handle, /* DHT handle */
555 &my_full_id.hashPubKey, /* Key to use */
556 10U, /* Replication level */
557 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
558 GNUNET_BLOCK_TYPE_TEST, /* Block type */
559 sizeof(my_full_id), /* Size of the data */
560 &my_full_id, /* Data itself */
561 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
562 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
564 &mesh_debug, "DHT_put for id completed");
566 NULL, /* Continuation */
567 NULL); /* Continuation closure */
570 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
575 * Function to process paths received for a new peer addition. The recorded
576 * paths form the initial tunnel, which can be optimized later.
577 * Called on each result obtained for the DHT search.
580 * @param exp when will this value expire
581 * @param key key of the result
582 * @param type type of the result
583 * @param size number of bytes in data
584 * @param data pointer to the result data
587 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
588 const GNUNET_HashCode * key,
589 const struct GNUNET_PeerIdentity *get_path,
590 unsigned int get_path_length,
591 const struct GNUNET_PeerIdentity *put_path,
592 unsigned int put_path_length,
593 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
596 /******************************************************************************/
597 /****************** GENERAL HELPER FUNCTIONS ************************/
598 /******************************************************************************/
601 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
602 * and insert it in the appropiate structures if the peer is not known yet.
604 * @param peer Identity of the peer
606 * @return Existing or newly created peer info
608 static struct MeshPeerInfo *
609 peer_info_get (const struct GNUNET_PeerIdentity *peer)
611 struct MeshPeerInfo *peer_info;
613 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
614 if (NULL == peer_info)
617 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
618 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
619 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
620 peer_info->id = GNUNET_PEER_intern (peer);
629 * Destroy the peer_info and free any allocated resources linked to it
630 * @param t tunnel the path belongs to
631 * @param pi the peer_info to destroy
632 * @return GNUNET_OK on success
635 peer_info_destroy (struct MeshPeerInfo *pi)
637 GNUNET_HashCode hash;
638 struct GNUNET_PeerIdentity id;
640 GNUNET_PEER_resolve (pi->id, &id);
641 GNUNET_PEER_change_rc (pi->id, -1);
642 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
644 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
645 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
653 * Notify a tunnel that a connection has broken that affects at least
656 * @param t Tunnel affected.
657 * @param peer Peer that (at least) has been affected by the disconnection.
658 * @param p1 Peer that got disconnected from p2.
659 * @param p2 Peer that got disconnected from p1.
661 * @return Short ID of the peer disconnected (either p1 or p2).
662 * 0 if the tunnel remained unaffected.
664 static GNUNET_PEER_Id
665 tunnel_notify_connection_broken (struct MeshTunnel *t,
666 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
670 * Remove all paths that rely on a direct connection between p1 and p2
671 * from the peer itself and notify all tunnels about it.
673 * @param peer PeerInfo of affected peer.
674 * @param p1 GNUNET_PEER_Id of one peer.
675 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
678 * TODO: optimize (see below)
681 path_remove_from_peer (struct MeshPeerInfo *peer,
685 struct GNUNET_PeerIdentity id;
686 struct MeshPeerPath *p;
687 struct MeshPeerPath *aux;
688 struct MeshPeerInfo *peer_d;
690 unsigned int destroyed;
700 for (i = 0; i < (p->length - 1); i++)
702 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
703 (p->peers[i] == p2 && p->peers[i + 1] == p1))
715 for (i = 0; i < peer->ntunnels; i++)
717 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
719 * Problem: one or more peers have been deleted from the tunnel tree.
720 * We don't know who they are to try to add them again.
721 * We need to try to find a new path for each of the disconnected peers.
722 * Some of them might already have a path to reach them that does not
723 * involve p1 and p2. Adding all anew might render in a better tree than
724 * the trivial immediate fix.
726 * Trivial immiediate fix: try to reconnect to the disconnected node. All
727 * its children will be reachable trough him.
729 GNUNET_PEER_resolve(d, &id);
730 peer_d = peer_info_get(&id);
733 for (p = peer_d->path_head; NULL != p; p = p->next)
735 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
743 /* No callback, as peer will be already disconnected */
744 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
748 struct MeshPathInfo *path_info;
750 if (NULL != peer_d->dhtget)
752 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
754 path_info->peer = peer_d;
755 path_info->t = peer->tunnels[i];
756 peer_d->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
757 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
758 GNUNET_BLOCK_TYPE_TEST, /* type */
759 &id.hashPubKey, /*key to search */
760 4, /* replication level */
761 GNUNET_DHT_RO_RECORD_ROUTE,
772 * Add the path to the peer and update the path used to reach it in case this
775 * @param peer_info Destination peer to add the path to.
776 * @param path New path to add. Last peer must be the peer in arg 1.
777 * Path will be either used of freed if already known.
779 * TODO: trim the part from origin to us? Add it as path to origin?
782 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
784 struct MeshPeerPath *aux;
788 if (NULL == peer_info || NULL == path)
794 l = path_get_length (path);
796 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
798 l2 = path_get_length (aux);
801 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
802 peer_info->path_tail, aux, path);
806 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
813 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
820 * Add the path to the origin peer and update the path used to reach it in case
821 * this is the shortest.
822 * The path is given in peer_info -> destination, therefore we turn the path
825 * @param peer_info Peer to add the path to, being the origin of the path.
826 * @param path New path to add after being inversed.
829 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
832 path_add_to_peer (peer_info, path);
837 * Build a PeerPath from the paths returned from the DHT, reversing the paths
838 * to obtain a local peer -> destination path and interning the peer ids.
840 * @return Newly allocated and created path
842 static struct MeshPeerPath *
843 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
844 unsigned int get_path_length,
845 const struct GNUNET_PeerIdentity *put_path,
846 unsigned int put_path_length)
848 struct MeshPeerPath *p;
854 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
855 for (i--; i >= 0; i--)
857 id = GNUNET_PEER_intern (&get_path[i]);
858 if (p->length > 0 && id == p->peers[p->length - 1])
860 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
861 GNUNET_PEER_change_rc(id, -1);
866 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
867 p->peers[p->length] = id;
872 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
873 for (i--; i >= 0; i--)
875 id = GNUNET_PEER_intern (&put_path[i]);
876 if (p->length > 0 && id == p->peers[p->length - 1])
878 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
879 GNUNET_PEER_change_rc(id, -1);
884 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
885 p->peers[p->length] = id;
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891 "MESH: (first of GET: %s)\n",
892 GNUNET_h2s_full(&get_path[0].hashPubKey));
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
894 "MESH: (first of PUT: %s)\n",
895 GNUNET_h2s_full(&put_path[0].hashPubKey));
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
897 "MESH: In total: %d hops\n",
905 * Send keepalive packets for a peer
907 * @param cls Closure (tunnel for which to send the keepalive).
908 * @param tc Notification context.
910 * TODO: implement explicit multicast keepalive?
913 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
917 * Check if client has registered with the service and has not disconnected
919 * @param client the client to check
921 * @return non-NULL if client exists in the global DLL
923 static struct MeshClient *
924 client_get (struct GNUNET_SERVER_Client *client)
926 struct MeshClient *c;
931 if (c->handle == client)
940 * Checks if a given client has subscribed to certain message type
942 * @param message_type Type of message to check
943 * @param c Client to check
945 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
950 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
954 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
955 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
960 * Search for a tunnel among the tunnels for a client
962 * @param c the client whose tunnels to search in
963 * @param tid the local id of the tunnel
965 * @return tunnel handler, NULL if doesn't exist
967 static struct MeshTunnel *
968 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
970 GNUNET_HashCode hash;
972 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
973 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
978 * Search for a tunnel by global ID using PEER_ID
980 * @param pi owner of the tunnel
981 * @param tid global tunnel number
983 * @return tunnel handler, NULL if doesn't exist
985 static struct MeshTunnel *
986 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
988 struct MESH_TunnelID id;
989 GNUNET_HashCode hash;
994 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
995 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1000 * Search for a tunnel by global ID using full PeerIdentities
1002 * @param oid owner of the tunnel
1003 * @param tid global tunnel number
1005 * @return tunnel handler, NULL if doesn't exist
1007 static struct MeshTunnel *
1008 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1010 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1015 * Callback used to notify a client owner of a tunnel that a peer has
1016 * disconnected, most likely because of a path change.
1018 * @param n Node in the tree representing the disconnected peer
1021 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1023 struct GNUNET_MESH_PeerControl msg;
1025 if (NULL == n->t->client || NULL == nc)
1028 msg.header.size = htons (sizeof (msg));
1029 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1030 msg.tunnel_id = htonl (n->t->local_tid);
1031 GNUNET_PEER_resolve (n->peer, &msg.peer);
1032 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1033 &msg.header, GNUNET_NO);
1038 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1039 * needed rescources.
1041 * @param t Tunnel we want to add a new peer to
1042 * @param peer PeerInfo of the peer being added
1046 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1048 // struct MeshTunnelTreeNode *n;
1049 struct MeshPeerPath *p;
1050 struct MeshPeerPath *best_p;
1051 unsigned int best_cost;
1054 if (NULL != tree_find_peer(t->tree->root, peer->id))
1056 /* Already have it, nothing to do. */
1061 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1062 if (NULL == (p = peer->path_head))
1069 best_cost = UINT_MAX;
1072 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1079 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1080 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1081 t->path_refresh_task =
1082 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1087 * Notify a tunnel that a connection has broken that affects at least
1088 * some of its peers.
1090 * @param t Tunnel affected.
1091 * @param peer Peer that (at least) has been affected by the disconnection.
1092 * @param p1 Peer that got disconnected from p2.
1093 * @param p2 Peer that got disconnected from p1.
1095 * @return Short ID of the peer disconnected (either p1 or p2).
1096 * 0 if the tunnel remained unaffected.
1098 static GNUNET_PEER_Id
1099 tunnel_notify_connection_broken (struct MeshTunnel *t,
1100 struct MeshPeerInfo *peer,
1104 return tree_notify_connection_broken (t->tree, p1, p2,
1105 ¬ify_peer_disconnected);
1110 * Destroy the tunnel and free any allocated resources linked to it
1112 * @param t the tunnel to destroy
1114 * @return GNUNET_OK on success
1117 tunnel_destroy (struct MeshTunnel *t)
1119 struct MeshClient *c;
1120 struct MeshQueue *q;
1121 struct MeshQueue *qn;
1122 GNUNET_HashCode hash;
1125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1134 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1135 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1140 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1141 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1145 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1149 if (NULL != q->data)
1150 GNUNET_free (q->data);
1154 /* TODO cancel core transmit ready in case it was active */
1156 tree_destroy(t->tree);
1163 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1164 * client when the client disconnects.
1166 * @param cls closure (client that is disconnecting)
1167 * @param key the hash of the local tunnel id (used to access the hashmap)
1168 * @param value the value stored at the key (tunnel to destroy)
1170 * @return GNUNET_OK on success
1173 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1177 r = tunnel_destroy ((struct MeshTunnel *) value);
1182 /******************************************************************************/
1183 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1184 /******************************************************************************/
1188 * Function called to notify a client about the socket
1189 * being ready to queue more data. "buf" will be
1190 * NULL and "size" zero if the socket was closed for
1191 * writing in the meantime.
1193 * @param cls closure
1194 * @param size number of bytes available in buf
1195 * @param buf where the callee should write the message
1196 * @return number of bytes written to buf
1199 send_core_create_path (void *cls, size_t size, void *buf)
1201 struct MeshPathInfo *info = cls;
1202 struct GNUNET_MESH_ManipulatePath *msg;
1203 struct GNUNET_PeerIdentity *peer_ptr;
1204 struct MeshPeerInfo *peer = info->peer;
1205 struct MeshTunnel *t = info->t;
1206 struct MeshPeerPath *p = info->path;
1211 sizeof (struct GNUNET_MESH_ManipulatePath) +
1212 p->length * sizeof (struct GNUNET_PeerIdentity);
1214 if (size < size_needed || NULL == buf)
1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
1217 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1218 GNUNET_TIME_UNIT_FOREVER_REL,
1219 path_get_first_hop (t->tree, peer->id),
1220 size_needed, &send_core_create_path,
1225 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1226 msg->header.size = htons (size_needed);
1227 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1228 msg->tid = ntohl (t->id.tid);
1230 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1231 for (i = 0; i < p->length; i++)
1233 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1245 * Function called to notify a client about the socket
1246 * being ready to queue more data. "buf" will be
1247 * NULL and "size" zero if the socket was closed for
1248 * writing in the meantime.
1250 * @param cls closure (MeshDataDescriptor with all info to build packet)
1251 * @param size number of bytes available in buf
1252 * @param buf where the callee should write the message
1253 * @return number of bytes written to buf
1256 send_core_data_to_origin (void *cls, size_t size, void *buf)
1258 struct MeshDataDescriptor *info = cls;
1259 struct GNUNET_MESH_ToOrigin *msg = buf;
1262 GNUNET_assert (NULL != info);
1263 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1264 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1266 if (total_size > size)
1268 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1269 "not enough buffer to send data to origin\n");
1272 msg->header.size = htons (total_size);
1273 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1274 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1275 msg->tid = htonl (info->origin->tid);
1276 if (0 != info->size)
1278 memcpy (&msg[1], &info[1], info->size);
1280 if (NULL != info->client)
1282 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1291 * Function called to notify a client about the socket
1292 * being ready to queue more data. "buf" will be
1293 * NULL and "size" zero if the socket was closed for
1294 * writing in the meantime.
1296 * @param cls closure (data itself)
1297 * @param size number of bytes available in buf
1298 * @param buf where the callee should write the message
1300 * @return number of bytes written to buf
1305 send_core_data_multicast (void *cls, size_t size, void *buf)
1307 struct MeshDataDescriptor *info = cls;
1308 struct GNUNET_MESH_Multicast *msg = buf;
1311 GNUNET_assert (NULL != info);
1312 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1313 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1317 info->peer->core_transmit[info->handler_n] = NULL;
1318 info->peer->infos[info->handler_n] = NULL;
1320 if (total_size > size)
1327 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1328 msg->header.size = htons (total_size);
1329 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1330 msg->tid = htonl (info->origin->tid);
1331 memcpy (&msg[1], info->data, total_size);
1333 if (0 == --(*info->copies))
1335 if (NULL != info->client)
1337 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1339 GNUNET_free (info->data);
1340 GNUNET_free (info->copies);
1348 * Function called to notify a client about the socket
1349 * being ready to queue more data. "buf" will be
1350 * NULL and "size" zero if the socket was closed for
1351 * writing in the meantime.
1353 * @param cls closure (MeshDataDescriptor)
1354 * @param size number of bytes available in buf
1355 * @param buf where the callee should write the message
1356 * @return number of bytes written to buf
1359 send_core_path_ack (void *cls, size_t size, void *buf)
1361 struct MeshDataDescriptor *info = cls;
1362 struct GNUNET_MESH_PathACK *msg = buf;
1364 GNUNET_assert (NULL != info);
1367 info->peer->core_transmit[info->handler_n] = NULL;
1369 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1374 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1375 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1376 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1377 msg->tid = htonl (info->origin->tid);
1378 msg->peer_id = my_full_id;
1380 /* TODO add signature */
1382 return sizeof (struct GNUNET_MESH_PathACK);
1387 * Function called to notify a client about the socket
1388 * being ready to queue more data. "buf" will be
1389 * NULL and "size" zero if the socket was closed for
1390 * writing in the meantime.
1392 * @param cls closure (data itself)
1393 * @param size number of bytes available in buf
1394 * @param buf where the callee should write the message
1395 * @return number of bytes written to buf
1398 send_core_data_raw (void *cls, size_t size, void *buf)
1400 struct GNUNET_MessageHeader *msg = cls;
1403 GNUNET_assert (NULL != msg);
1404 total_size = ntohs (msg->size);
1406 if (total_size > size)
1411 memcpy (buf, msg, total_size);
1419 * Send another peer a notification to destroy a tunnel
1420 * @param cls The tunnel to destroy
1421 * @param size Size in the buffer
1422 * @param buf Memory where to put the data to transmit
1423 * @return Size of data put in buffer
1426 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1428 struct MeshTunnel *t = cls;
1429 struct MeshClient *c;
1430 struct GNUNET_MESH_TunnelMessage *msg;
1434 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1435 /*FIXME*/ msg->header.size =
1436 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1437 msg->tunnel_id = htonl (t->id.tid);
1439 tunnel_destroy (c, t);
1440 return sizeof (struct GNUNET_MESH_TunnelMessage);
1446 * Send the message to all clients that have subscribed to its type
1448 * @param msg Pointer to the message itself
1449 * @return number of clients this message was sent to
1452 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1454 struct MeshClient *c;
1458 type = ntohs (msg->type);
1459 for (count = 0, c = clients; c != NULL; c = c->next)
1461 if (client_is_subscribed (type, c))
1464 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1474 * Notify the client that owns the tunnel that a peer has connected to it
1476 * @param t Tunnel whose owner to notify
1477 * @param id Short id of the peer that has connected
1480 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1482 struct GNUNET_MESH_PeerControl pc;
1484 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1485 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1486 pc.tunnel_id = htonl (t->local_tid);
1487 GNUNET_PEER_resolve (id, &pc.peer);
1488 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1489 &pc.header, GNUNET_NO);
1493 /******************************************************************************/
1494 /******************** MESH NETWORK HANDLERS **************************/
1495 /******************************************************************************/
1499 * Core handler for path creation
1500 * struct GNUNET_CORE_MessageHandler
1502 * @param cls closure
1503 * @param message message
1504 * @param peer peer identity this notification is about
1505 * @param atsi performance data
1506 * @return GNUNET_OK to keep the connection open,
1507 * GNUNET_SYSERR to close it (signal serious error)
1511 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1512 const struct GNUNET_MessageHeader *message,
1513 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1515 unsigned int own_pos;
1518 MESH_TunnelNumber tid;
1519 struct GNUNET_MESH_ManipulatePath *msg;
1520 struct GNUNET_PeerIdentity *pi;
1521 struct GNUNET_PeerIdentity id;
1522 GNUNET_HashCode hash;
1523 struct MeshPeerPath *path;
1524 struct MeshPeerInfo *dest_peer_info;
1525 struct MeshPeerInfo *orig_peer_info;
1526 struct MeshTunnel *t;
1528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1529 "MESH: Received a MESH path create msg\n");
1530 size = ntohs (message->size);
1531 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1533 GNUNET_break_op (0);
1537 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1538 if (size % sizeof (struct GNUNET_PeerIdentity))
1540 GNUNET_break_op (0);
1543 size /= sizeof (struct GNUNET_PeerIdentity);
1546 GNUNET_break_op (0);
1549 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1551 tid = ntohl (msg->tid);
1552 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1553 t = tunnel_get (pi, tid);
1557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1558 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1559 t->id.oid = GNUNET_PEER_intern (pi);
1561 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1563 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1565 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1566 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1574 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1575 if (NULL == dest_peer_info)
1577 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1578 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1579 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1581 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1583 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1584 if (NULL == orig_peer_info)
1586 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1587 orig_peer_info->id = GNUNET_PEER_intern (pi);
1588 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1589 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1592 path = path_new (size);
1594 for (i = 0; i < size; i++)
1596 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1597 if (path->peers[i] == myid)
1601 { /* cannot be self, must be 'not found' */
1602 /* create path: self not found in path through self */
1603 GNUNET_break_op (0);
1604 path_destroy (path);
1605 /* FIXME error. destroy tunnel? leave for timeout? */
1608 if (own_pos == size - 1)
1610 /* It is for us! Send ack. */
1611 struct MeshDataDescriptor *info;
1614 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1615 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1616 info->origin = &t->id;
1617 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1618 GNUNET_assert (info->peer);
1619 for (j = 0; info->peer->core_transmit[j]; j++)
1627 info->handler_n = j;
1628 info->peer->core_transmit[j] =
1629 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1630 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1631 sizeof (struct GNUNET_MessageHeader),
1632 &send_core_path_ack, info);
1636 /* It's for somebody else! Retransmit. */
1637 struct MeshPathInfo *path_info;
1639 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1641 path_info->path = path;
1642 path_info->peer = dest_peer_info;
1644 path_add_to_peer (dest_peer_info, path);
1645 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1646 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1647 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1648 sizeof (struct GNUNET_MessageHeader),
1649 &send_core_create_path, path_info);
1656 * Core handler for mesh network traffic going from the origin to a peer
1658 * @param cls closure
1659 * @param peer peer identity this notification is about
1660 * @param message message
1661 * @param atsi performance data
1662 * @return GNUNET_OK to keep the connection open,
1663 * GNUNET_SYSERR to close it (signal serious error)
1666 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1667 const struct GNUNET_MessageHeader *message,
1668 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1670 struct GNUNET_MESH_Unicast *msg;
1671 struct MeshTunnel *t;
1672 struct MeshPeerInfo *pi;
1675 size = ntohs (message->size);
1677 sizeof (struct GNUNET_MESH_Unicast) +
1678 sizeof (struct GNUNET_MessageHeader))
1683 msg = (struct GNUNET_MESH_Unicast *) message;
1684 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1687 /* TODO notify back: we don't know this tunnel */
1688 GNUNET_break_op (0);
1691 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1692 &msg->destination.hashPubKey);
1695 /* TODO maybe feedback, log to statistics */
1696 GNUNET_break_op (0);
1701 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1704 msg = GNUNET_malloc (size);
1705 memcpy (msg, message, size);
1706 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1707 GNUNET_TIME_UNIT_FOREVER_REL,
1708 path_get_first_hop (t->tree, pi->id),
1710 &send_core_data_raw, msg);
1716 * Core handler for mesh network traffic going from the origin to all peers
1718 * @param cls closure
1719 * @param message message
1720 * @param peer peer identity this notification is about
1721 * @param atsi performance data
1722 * @return GNUNET_OK to keep the connection open,
1723 * GNUNET_SYSERR to close it (signal serious error)
1725 * TODO: Check who we got this from, to validate route.
1728 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1729 const struct GNUNET_MessageHeader *message,
1730 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1732 struct GNUNET_MESH_Multicast *msg;
1733 struct GNUNET_PeerIdentity *id;
1734 struct MeshDataDescriptor *info;
1735 struct MeshTunnelTreeNode *n;
1736 struct MeshTunnel *t;
1737 unsigned int *copies;
1742 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
1743 if (size < sizeof (struct GNUNET_MessageHeader))
1745 GNUNET_break_op (0);
1748 msg = (struct GNUNET_MESH_Multicast *) message;
1749 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1753 /* TODO notify that we dont know that tunnel */
1754 GNUNET_break_op (0);
1758 /* Transmit to locally interested clients */
1759 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1761 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1763 n = t->tree->me->children_head;
1766 copies = GNUNET_malloc (sizeof (unsigned int));
1767 for (*copies = 0; NULL != n; n = n->next)
1769 n = t->tree->me->children_head;
1770 data = GNUNET_malloc (size);
1771 memcpy (data, &msg[1], size);
1774 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1775 info->origin = &t->id;
1778 info->copies = copies;
1779 info->client = t->client->handle;
1780 info->destination = n->peer;
1781 id = path_get_first_hop(t->tree, n->peer);
1782 info->peer = peer_info_get(id);
1783 GNUNET_assert (NULL != info->peer);
1784 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
1786 if (i == (CORE_QUEUE_SIZE - 1))
1792 info->handler_n = i;
1793 info->peer->infos[i] = info;
1794 info->peer->core_transmit[i] =
1795 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1796 GNUNET_TIME_UNIT_FOREVER_REL, id,
1797 ntohs (msg->header.size),
1798 &send_core_data_multicast, info);
1806 * Core handler for mesh network traffic
1808 * @param cls closure
1809 * @param message message
1810 * @param peer peer identity this notification is about
1811 * @param atsi performance data
1813 * @return GNUNET_OK to keep the connection open,
1814 * GNUNET_SYSERR to close it (signal serious error)
1817 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1818 const struct GNUNET_MessageHeader *message,
1819 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1821 struct GNUNET_MESH_ToOrigin *msg;
1822 struct GNUNET_PeerIdentity id;
1823 struct MeshPeerInfo *peer_info;
1824 struct MeshTunnel *t;
1827 size = ntohs (message->size);
1828 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1829 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1831 GNUNET_break_op (0);
1834 msg = (struct GNUNET_MESH_ToOrigin *) message;
1835 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1839 /* TODO notify that we dont know this tunnel (whom)? */
1843 if (t->id.oid == myid)
1845 if (NULL == t->client)
1847 /* got data packet for ownerless tunnel */
1848 GNUNET_break_op (0);
1851 /* TODO signature verification */
1852 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1856 peer_info = peer_info_get (&msg->oid);
1857 if (NULL == peer_info)
1859 /* unknown origin of tunnel */
1863 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1864 msg = GNUNET_malloc (size);
1865 memcpy (msg, message, size);
1866 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1867 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1868 &send_core_data_raw, msg);
1875 * Core handler for path ACKs
1877 * @param cls closure
1878 * @param message message
1879 * @param peer peer identity this notification is about
1880 * @param atsi performance data
1882 * @return GNUNET_OK to keep the connection open,
1883 * GNUNET_SYSERR to close it (signal serious error)
1886 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1887 const struct GNUNET_MessageHeader *message,
1888 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1890 struct GNUNET_MESH_PathACK *msg;
1891 struct MeshTunnelTreeNode *n;
1892 struct MeshPeerInfo *peer_info;
1893 struct MeshTunnel *t;
1895 msg = (struct GNUNET_MESH_PathACK *) message;
1896 t = tunnel_get (&msg->oid, msg->tid);
1899 /* TODO notify that we don't know the tunnel */
1903 /* Message for us? */
1904 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1906 if (NULL == t->client)
1908 GNUNET_break_op (0);
1911 peer_info = peer_info_get (&msg->peer_id);
1912 if (NULL == peer_info)
1914 GNUNET_break_op (0);
1917 n = tree_find_peer(t->tree->root, peer_info->id);
1920 GNUNET_break_op (0);
1923 n->status = MESH_PEER_READY;
1924 send_client_peer_connected(t, peer_info->id);
1928 peer_info = peer_info_get (&msg->oid);
1929 if (NULL == peer_info)
1931 /* If we know the tunnel, we should DEFINITELY know the peer */
1935 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1936 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1937 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1938 GNUNET_TIME_UNIT_FOREVER_REL,
1939 path_get_first_hop (t->tree,
1941 sizeof (struct GNUNET_MESH_PathACK),
1942 &send_core_data_raw, msg);
1948 * Functions to handle messages from core
1950 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1951 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1952 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1953 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1954 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1955 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1956 sizeof (struct GNUNET_MESH_PathACK)},
1962 /******************************************************************************/
1963 /**************** MESH LOCAL HANDLER HELPERS ***********************/
1964 /******************************************************************************/
1967 * deregister_app: iterator for removing each application registered by a client
1969 * @param cls closure
1970 * @param key the hash of the application id (used to access the hashmap)
1971 * @param value the value stored at the key (client)
1973 * @return GNUNET_OK on success
1976 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
1978 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
1984 * notify_client_connection_failure: notify a client that the connection to the
1985 * requested remote peer is not possible (for instance, no route found)
1986 * Function called when the socket is ready to queue more data. "buf" will be
1987 * NULL and "size" zero if the socket was closed for writing in the meantime.
1989 * @param cls closure
1990 * @param size number of bytes available in buf
1991 * @param buf where the callee should write the message
1992 * @return number of bytes written to buf
1995 notify_client_connection_failure (void *cls, size_t size, void *buf)
1998 struct MeshPeerInfo *peer_info;
1999 struct GNUNET_MESH_PeerControl *msg;
2000 struct GNUNET_PeerIdentity id;
2002 if (0 == size && NULL == buf)
2004 // TODO retry? cancel?
2008 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2009 peer_info = (struct MeshPeerInfo *) cls;
2010 msg = (struct GNUNET_MESH_PeerControl *) buf;
2011 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2012 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2013 // msg->tunnel_id = htonl(peer_info->t->tid);
2014 GNUNET_PEER_resolve (peer_info->id, &id);
2015 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2023 * Send keepalive packets for a peer
2025 * @param cls Closure (tunnel for which to send the keepalive).
2026 * @param tc Notification context.
2028 * TODO: implement explicit multicast keepalive?
2031 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2033 struct MeshTunnel *t = cls;
2034 struct GNUNET_MessageHeader *payload;
2035 struct GNUNET_MESH_Multicast *msg;
2038 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2039 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2045 "MESH: sending keepalive for tunnel %d\n",
2048 size = sizeof(struct GNUNET_MESH_Multicast) +
2049 sizeof(struct GNUNET_MessageHeader);
2050 msg = GNUNET_malloc (size);
2051 msg->header.size = htons (size);
2052 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2053 msg->oid = my_full_id;
2054 msg->tid = htonl(t->id.tid);
2055 payload = (struct GNUNET_MessageHeader *) &msg[1];
2056 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2057 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2058 handle_mesh_data_multicast (NULL, &my_full_id, &msg->header, NULL);
2061 t->path_refresh_task =
2062 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2068 * Function to process paths received for a new peer addition. The recorded
2069 * paths form the initial tunnel, which can be optimized later.
2070 * Called on each result obtained for the DHT search.
2072 * @param cls closure
2073 * @param exp when will this value expire
2074 * @param key key of the result
2075 * @param type type of the result
2076 * @param size number of bytes in data
2077 * @param data pointer to the result data
2079 * TODO: re-issue the request after certain time? cancel after X results?
2082 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2083 const GNUNET_HashCode * key,
2084 const struct GNUNET_PeerIdentity *get_path,
2085 unsigned int get_path_length,
2086 const struct GNUNET_PeerIdentity *put_path,
2087 unsigned int put_path_length,
2088 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2090 struct MeshPathInfo *path_info = cls;
2091 struct MeshPeerPath *p;
2092 struct GNUNET_PeerIdentity pi;
2095 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2096 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2097 "MESH: Got results from DHT for %s\n",
2098 GNUNET_h2s_full(&pi.hashPubKey));
2099 if (NULL == get_path || NULL == put_path)
2101 if (NULL == path_info->peer->path_head)
2103 // Find ourselves some alternate initial path to the destination: retry
2104 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2105 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2106 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2107 GNUNET_BLOCK_TYPE_TEST, /* type */
2108 &pi.hashPubKey, /*key to search */
2109 4, /* replication level */
2110 GNUNET_DHT_RO_RECORD_ROUTE,
2112 0, /* xquery bits */
2113 &dht_get_id_handler,
2114 (void *) path_info);
2119 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2120 path_add_to_peer (path_info->peer, p);
2121 for (i = 0; i < path_info->peer->ntunnels; i++)
2123 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2125 GNUNET_free (path_info);
2132 * Function to process paths received for a new peer addition. The recorded
2133 * paths form the initial tunnel, which can be optimized later.
2134 * Called on each result obtained for the DHT search.
2136 * @param cls closure
2137 * @param exp when will this value expire
2138 * @param key key of the result
2139 * @param type type of the result
2140 * @param size number of bytes in data
2141 * @param data pointer to the result data
2144 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2145 const GNUNET_HashCode * key,
2146 const struct GNUNET_PeerIdentity *get_path,
2147 unsigned int get_path_length,
2148 const struct GNUNET_PeerIdentity *put_path,
2149 unsigned int put_path_length,
2150 enum GNUNET_BLOCK_Type type, size_t size,
2153 const struct GNUNET_PeerIdentity *pi = data;
2154 struct GNUNET_PeerIdentity id;
2155 struct MeshTunnel *t = cls;
2156 struct MeshPeerInfo *peer_info;
2157 struct MeshPathInfo *path_info;
2158 struct MeshPeerPath *p;
2161 if (size != sizeof (struct GNUNET_PeerIdentity))
2163 GNUNET_break_op (0);
2166 GNUNET_assert (NULL != t->client);
2167 GNUNET_DHT_get_stop (t->client->dht_get_type);
2168 t->client->dht_get_type = NULL;
2169 peer_info = peer_info_get (pi);
2170 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2171 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2173 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2174 NULL == peer_info->dhtget)
2176 /* we don't have a route to the peer, let's try a direct lookup */
2177 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2179 GNUNET_TIME_UNIT_FOREVER_REL,
2181 GNUNET_BLOCK_TYPE_TEST,
2184 /* key to look up */
2186 /* replication level */
2187 GNUNET_DHT_RO_RECORD_ROUTE,
2188 /* option to dht: record route */
2190 0, /* xquery bits */
2193 peer_info); /* closure */
2196 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2197 path_add_to_peer (peer_info, p);
2198 tunnel_add_peer(t, peer_info);
2199 p = tree_get_path_to_peer(t->tree, peer_info->id);
2201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2202 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2203 t->local_tid, p->length);
2204 for (i = 0; i < p->length; i++)
2206 GNUNET_PEER_resolve (p->peers[0], &id);
2207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2208 GNUNET_h2s_full (&id.hashPubKey));
2214 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2216 path_info->peer = peer_info;
2217 path_info->path = p;
2218 GNUNET_PEER_resolve (p->peers[1], &id);
2219 GNUNET_CORE_notify_transmit_ready (core_handle,
2225 GNUNET_TIME_UNIT_FOREVER_REL,
2229 sizeof (struct GNUNET_MESH_ManipulatePath)
2232 sizeof (struct GNUNET_PeerIdentity)),
2234 &send_core_create_path,
2236 path_info); /* cls */
2240 send_client_peer_connected(t, myid);
2243 /******************************************************************************/
2244 /********************* MESH LOCAL HANDLES **************************/
2245 /******************************************************************************/
2249 * Handler for client disconnection
2251 * @param cls closure
2252 * @param client identification of the client; NULL
2253 * for the last call when the server is destroyed
2256 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2258 struct MeshClient *c;
2259 struct MeshClient *next;
2261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2267 if (c->handle != client && NULL != client)
2269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2274 if (NULL != c->tunnels)
2276 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2277 &tunnel_destroy_iterator,
2279 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2282 /* deregister clients applications */
2283 if (NULL != c->apps)
2285 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2286 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2288 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2289 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2291 GNUNET_SCHEDULER_cancel (announce_applications_task);
2292 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2294 if (NULL != c->types)
2295 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2296 if (NULL != c->dht_get_type)
2297 GNUNET_DHT_get_stop (c->dht_get_type);
2298 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2310 * Handler for new clients
2312 * @param cls closure
2313 * @param client identification of the client
2314 * @param message the actual message, which includes messages the client wants
2317 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2318 const struct GNUNET_MessageHeader *message)
2320 struct GNUNET_MESH_ClientConnect *cc_msg;
2321 struct MeshClient *c;
2322 GNUNET_MESH_ApplicationType *a;
2329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2330 /* Check data sanity */
2331 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2332 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2333 ntypes = ntohs (cc_msg->types);
2334 napps = ntohs (cc_msg->applications);
2336 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2339 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2343 /* Create new client structure */
2344 c = GNUNET_malloc (sizeof (struct MeshClient));
2346 c->id = next_client_id++;
2349 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2352 GNUNET_MESH_ApplicationType at;
2355 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2356 for (i = 0; i < napps; i++)
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2360 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2361 /* store in clients hashmap */
2362 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2363 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2364 /* store in global hashmap, for announcements */
2365 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2366 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2368 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2369 announce_applications_task =
2370 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2378 t = (uint16_t *) & a[napps];
2379 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2380 for (i = 0; i < ntypes; i++)
2383 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2385 /* store in clients hashmap */
2386 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2387 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2388 /* store in global hashmap */
2389 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2390 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2394 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2396 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2397 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2398 GNUNET_SERVER_notification_context_add (nc, client);
2400 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2408 * Handler for requests of new tunnels
2410 * @param cls closure
2411 * @param client identification of the client
2412 * @param message the actual message
2415 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2416 const struct GNUNET_MessageHeader *message)
2418 struct GNUNET_MESH_TunnelMessage *t_msg;
2419 struct MeshTunnel *t;
2420 struct MeshClient *c;
2421 GNUNET_HashCode hash;
2423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2425 /* Sanity check for client registration */
2426 if (NULL == (c = client_get (client)))
2429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2436 /* Message sanity check */
2437 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2440 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2444 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2445 /* Sanity check for tunnel numbering */
2446 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2449 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2452 /* Sanity check for duplicate tunnel IDs */
2453 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2460 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2462 while (NULL != tunnel_get_by_pi (myid, next_tid))
2463 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2464 t->id.tid = next_tid++;
2466 t->local_tid = ntohl (t_msg->tunnel_id);
2468 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2470 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2472 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2473 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2476 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2480 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2482 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2483 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2486 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2489 t->tree = tree_new (t, myid);
2490 t->tree->refresh = REFRESH_PATH_TIME;
2491 t->tree->root->status = MESH_PEER_READY;
2492 t->tree->me = t->tree->root;
2494 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2500 * Handler for requests of deleting tunnels
2502 * @param cls closure
2503 * @param client identification of the client
2504 * @param message the actual message
2507 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2508 const struct GNUNET_MessageHeader *message)
2510 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2511 struct MeshClient *c;
2512 struct MeshTunnel *t;
2513 MESH_TunnelNumber tid;
2514 GNUNET_HashCode hash;
2516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2518 /* Sanity check for client registration */
2519 if (NULL == (c = client_get (client)))
2522 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2525 /* Message sanity check */
2526 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2535 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2537 /* Retrieve tunnel */
2538 tid = ntohl (tunnel_msg->tunnel_id);
2540 /* Remove from local id hashmap */
2541 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2542 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2543 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2545 /* Remove from global id hashmap */
2546 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2547 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2549 // notify_tunnel_destroy(t); FIXME
2551 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2557 * Handler for connection requests to new peers
2559 * @param cls closure
2560 * @param client identification of the client
2561 * @param message the actual message (PeerControl)
2564 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2565 const struct GNUNET_MessageHeader *message)
2567 struct GNUNET_MESH_PeerControl *peer_msg;
2568 struct MeshClient *c;
2569 struct MeshTunnel *t;
2570 MESH_TunnelNumber tid;
2571 struct MeshPeerInfo *peer_info;
2573 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
2574 /* Sanity check for client registration */
2575 if (NULL == (c = client_get (client)))
2578 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2582 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2583 /* Sanity check for message size */
2584 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2587 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2591 /* Tunnel exists? */
2592 tid = ntohl (peer_msg->tunnel_id);
2593 t = tunnel_get_by_local_id (c, tid);
2597 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2601 /* Does client own tunnel? */
2602 if (t->client->handle != client)
2605 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2608 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
2609 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
2610 peer_info = peer_info_get (&peer_msg->peer);
2612 /* Start DHT search if needed, otherwise just add peer to tunnel. */
2613 if (NULL == peer_info->dhtget && NULL == peer_info->path_head)
2615 struct MeshPathInfo *path_info;
2617 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2618 path_info->peer = peer_info;
2620 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
2621 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2622 GNUNET_BLOCK_TYPE_TEST, /* type */
2623 &peer_msg->peer.hashPubKey, /*key to search */
2624 4, /* replication level */
2625 GNUNET_DHT_RO_RECORD_ROUTE,
2627 0, /* xquery bits */
2628 &dht_get_id_handler,
2629 (void *) path_info);
2631 if (NULL != peer_info->path_head)
2633 tunnel_add_peer(t, peer_info);
2635 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2641 * Handler for disconnection requests of peers in a tunnel
2643 * @param cls closure
2644 * @param client identification of the client
2645 * @param message the actual message (PeerControl)
2648 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2649 const struct GNUNET_MessageHeader *message)
2651 struct GNUNET_MESH_PeerControl *peer_msg;
2652 struct MeshClient *c;
2653 struct MeshTunnel *t;
2654 MESH_TunnelNumber tid;
2656 /* Sanity check for client registration */
2657 if (NULL == (c = client_get (client)))
2660 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2663 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2664 /* Sanity check for message size */
2665 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2672 /* Tunnel exists? */
2673 tid = ntohl (peer_msg->tunnel_id);
2674 t = tunnel_get_by_local_id (c, tid);
2678 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2682 /* Does client own tunnel? */
2683 if (t->client->handle != client)
2686 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2690 /* Ok, delete peer from tunnel */
2691 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2692 &peer_msg->peer.hashPubKey);
2694 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2700 * Handler for connection requests to new peers by type
2702 * @param cls closure
2703 * @param client identification of the client
2704 * @param message the actual message (ConnectPeerByType)
2707 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2708 const struct GNUNET_MessageHeader *message)
2710 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2711 struct MeshClient *c;
2712 struct MeshTunnel *t;
2713 GNUNET_HashCode hash;
2714 GNUNET_MESH_ApplicationType type;
2715 MESH_TunnelNumber tid;
2717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2718 /* Sanity check for client registration */
2719 if (NULL == (c = client_get (client)))
2722 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2726 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2727 /* Sanity check for message size */
2728 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2729 ntohs (connect_msg->header.size))
2732 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2736 /* Tunnel exists? */
2737 tid = ntohl (connect_msg->tunnel_id);
2738 t = tunnel_get_by_local_id (c, tid);
2742 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2746 /* Does client own tunnel? */
2747 if (t->client->handle != client)
2750 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2754 /* Do WE have the service? */
2755 type = ntohl (connect_msg->type);
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2757 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2758 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2761 /* Yes! Fast forward, add ourselves to the tunnel and send the
2762 * good news to the client
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2765 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2766 peer_info_get (&my_full_id),
2767 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2770 send_client_peer_connected(t, myid);
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2772 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2775 /* Ok, lets find a peer offering the service */
2776 if (c->dht_get_type)
2778 GNUNET_DHT_get_stop (c->dht_get_type);
2780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2781 GNUNET_h2s_full (&hash));
2783 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2784 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2785 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0,
2786 &dht_get_type_handler, t);
2788 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2794 * Handler for client traffic directed to one peer
2796 * @param cls closure
2797 * @param client identification of the client
2798 * @param message the actual message
2801 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2802 const struct GNUNET_MessageHeader *message)
2804 struct MeshClient *c;
2805 struct MeshTunnel *t;
2806 struct MeshPeerInfo *pi;
2807 struct GNUNET_MESH_Unicast *data_msg;
2808 MESH_TunnelNumber tid;
2811 /* Sanity check for client registration */
2812 if (NULL == (c = client_get (client)))
2815 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2818 data_msg = (struct GNUNET_MESH_Unicast *) message;
2819 /* Sanity check for message size */
2820 size = ntohs (message->size);
2821 if (sizeof (struct GNUNET_MESH_Unicast) +
2822 sizeof (struct GNUNET_MessageHeader) > size)
2825 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2829 /* Tunnel exists? */
2830 tid = ntohl (data_msg->tid);
2831 t = tunnel_get_by_local_id (c, tid);
2835 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2839 /* Is it a local tunnel? Then, does client own the tunnel? */
2840 if (t->client->handle != NULL && t->client->handle != client)
2843 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2847 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2848 &data_msg->destination.hashPubKey);
2849 /* Is the selected peer in the tunnel? */
2853 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2857 /* Ok, everything is correct, send the message
2858 * (pretend we got it from a mesh peer)
2861 char buf[ntohs (message->size)];
2862 struct GNUNET_MESH_Unicast *copy;
2864 /* Work around const limitation */
2865 copy = (struct GNUNET_MESH_Unicast *) buf;
2866 memcpy (buf, data_msg, size);
2867 copy->oid = my_full_id;
2868 copy->tid = htonl (t->id.tid);
2869 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
2871 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2876 * Handler for client traffic directed to all peers in a tunnel
2878 * @param cls closure
2879 * @param client identification of the client
2880 * @param message the actual message
2883 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2884 const struct GNUNET_MessageHeader *message)
2886 struct MeshClient *c;
2887 struct MeshTunnel *t;
2888 struct GNUNET_MESH_Multicast *data_msg;
2889 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_Multicast *) message;
2899 /* Sanity check for message size */
2900 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2903 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2907 /* Tunnel exists? */
2908 tid = ntohl (data_msg->tid);
2909 t = tunnel_get_by_local_id (c, tid);
2913 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2917 /* Does client own tunnel? */
2918 if (t->client->handle != client)
2921 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2926 char buf[ntohs(message->size)];
2927 struct GNUNET_MESH_Multicast *copy;
2929 copy = (struct GNUNET_MESH_Multicast *)buf;
2930 memcpy(buf, message, ntohs(message->size));
2931 copy->oid = my_full_id;
2932 copy->tid = htonl(t->id.tid);
2933 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
2936 /* receive done gets called when last copy is sent */
2941 * Functions to handle messages from clients
2943 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
2944 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2945 {&handle_local_tunnel_create, NULL,
2946 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2947 sizeof (struct GNUNET_MESH_TunnelMessage)},
2948 {&handle_local_tunnel_destroy, NULL,
2949 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2950 sizeof (struct GNUNET_MESH_TunnelMessage)},
2951 {&handle_local_connect_add, NULL,
2952 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
2953 sizeof (struct GNUNET_MESH_PeerControl)},
2954 {&handle_local_connect_del, NULL,
2955 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
2956 sizeof (struct GNUNET_MESH_PeerControl)},
2957 {&handle_local_connect_by_type, NULL,
2958 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
2959 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
2960 {&handle_local_unicast, NULL,
2961 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2962 {&handle_local_unicast, NULL,
2963 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2964 {&handle_local_multicast, NULL,
2965 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2971 * To be called on core init/fail.
2973 * @param cls service closure
2974 * @param server handle to the server for this service
2975 * @param identity the public identity of this peer
2976 * @param publicKey the public key of this peer
2979 core_init (void *cls, struct GNUNET_CORE_Handle *server,
2980 const struct GNUNET_PeerIdentity *identity,
2981 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
2984 core_handle = server;
2985 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
2987 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
2988 GNUNET_SCHEDULER_shutdown();
2994 * Method called whenever a given peer connects.
2996 * @param cls closure
2997 * @param peer peer identity this notification is about
2998 * @param atsi performance data for the connection
3001 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3002 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3004 struct MeshPeerInfo *peer_info;
3005 struct MeshPeerPath *path;
3007 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3008 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3009 // GNUNET_h2s(&my_full_id.hashPubKey));
3010 peer_info = peer_info_get (peer);
3011 if (myid == peer_info->id)
3013 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3018 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3019 // GNUNET_h2s(&peer->hashPubKey));
3021 path = path_new (2);
3022 path->peers[0] = myid;
3023 path->peers[1] = peer_info->id;
3024 path_add_to_peer (peer_info, path);
3029 * Method called whenever a peer disconnects.
3031 * @param cls closure
3032 * @param peer peer identity this notification is about
3035 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3037 struct MeshPeerInfo *pi;
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3041 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3047 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3049 if (pi->core_transmit[i])
3051 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3052 /* TODO: notify that tranmission has failed */
3053 GNUNET_free (pi->infos[i]);
3056 path_remove_from_peer (pi, pi->id, myid);
3059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3065 /******************************************************************************/
3066 /************************ MAIN FUNCTIONS ****************************/
3067 /******************************************************************************/
3070 * Task run during shutdown.
3076 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3078 struct MeshClient *c;
3080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3081 if (core_handle != NULL)
3083 GNUNET_CORE_disconnect (core_handle);
3086 if (dht_handle != NULL)
3088 for (c = clients; NULL != c; c = c->next)
3089 if (NULL != c->dht_get_type)
3090 GNUNET_DHT_get_stop (c->dht_get_type);
3091 GNUNET_DHT_disconnect (dht_handle);
3096 GNUNET_SERVER_notification_context_destroy (nc);
3099 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3101 GNUNET_SCHEDULER_cancel (announce_id_task);
3102 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3108 * Process mesh requests.
3110 * @param cls closure
3111 * @param server the initialized server
3112 * @param c configuration to use
3115 run (void *cls, struct GNUNET_SERVER_Handle *server,
3116 const struct GNUNET_CONFIGURATION_Handle *c)
3118 struct MeshPeerInfo *peer;
3119 struct MeshPeerPath *p;
3122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3123 server_handle = server;
3124 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3125 CORE_QUEUE_SIZE, /* queue size */
3126 NULL, /* Closure passed to MESH functions */
3127 &core_init, /* Call core_init once connected */
3128 &core_connect, /* Handle connects */
3129 &core_disconnect, /* remove peers on disconnects */
3130 NULL, /* Do we care about "status" updates? */
3131 NULL, /* Don't notify about all incoming messages */
3132 GNUNET_NO, /* For header only in notification */
3133 NULL, /* Don't notify about all outbound messages */
3134 GNUNET_NO, /* For header-only out notification */
3135 core_handlers); /* Register these handlers */
3136 if (core_handle == NULL)
3139 GNUNET_SCHEDULER_shutdown ();
3144 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3149 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3150 GNUNET_SCHEDULER_shutdown ();
3153 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3154 GNUNET_free (keyfile);
3155 if (my_private_key == NULL)
3157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3158 _("Mesh service could not access hostkey. Exiting.\n"));
3159 GNUNET_SCHEDULER_shutdown ();
3162 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3163 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3164 &my_full_id.hashPubKey);
3165 myid = GNUNET_PEER_intern (&my_full_id);
3167 dht_handle = GNUNET_DHT_connect (c, 64);
3168 if (dht_handle == NULL)
3170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3171 Running without DHT has a severe\
3172 impact in MESH capabilities.\n\
3173 Plase check your configuretion and enable DHT.\n");
3179 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3180 peers = GNUNET_CONTAINER_multihashmap_create (32);
3181 applications = GNUNET_CONTAINER_multihashmap_create (32);
3182 types = GNUNET_CONTAINER_multihashmap_create (32);
3184 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3185 nc = GNUNET_SERVER_notification_context_create (server_handle,
3187 GNUNET_SERVER_disconnect_notify (server_handle,
3188 &handle_local_client_disconnect,
3193 clients_tail = NULL;
3198 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3199 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3201 /* Create a peer_info for the local peer */
3202 peer = peer_info_get(&my_full_id);
3205 path_add_to_peer(peer, p);
3207 /* Scheduled the task to clean up when shutdown is called */
3208 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3215 * The main function for the mesh service.
3217 * @param argc number of arguments from the command line
3218 * @param argv command line arguments
3219 * @return 0 ok, 1 on error
3222 main (int argc, char *const *argv)
3227 // fprintf (stderr, "main ()\n");
3229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3232 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3236 // fprintf (stderr, "main () END\n");