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_DHT_put (dht_handle, /* DHT handle */
553 &my_full_id.hashPubKey, /* Key to use */
554 10U, /* Replication level */
555 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
556 GNUNET_BLOCK_TYPE_TEST, /* Block type */
557 0, /* Size of the data */
558 NULL, /* Data itself */
559 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
560 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
562 &mesh_debug, "DHT_put for id completed");
564 NULL, /* Continuation */
565 NULL); /* Continuation closure */
568 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
573 * Function to process paths received for a new peer addition. The recorded
574 * paths form the initial tunnel, which can be optimized later.
575 * Called on each result obtained for the DHT search.
578 * @param exp when will this value expire
579 * @param key key of the result
580 * @param get_path NULL-terminated array of pointers
581 * to the peers on reverse GET path (or NULL if not recorded)
582 * @param put_path NULL-terminated array of pointers
583 * to the peers on the PUT path (or NULL if not recorded)
584 * @param type type of the result
585 * @param size number of bytes in data
586 * @param data pointer to the result data
589 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
590 const GNUNET_HashCode * key,
591 const struct GNUNET_PeerIdentity *const *get_path,
592 const struct GNUNET_PeerIdentity *const *put_path,
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,
762 NULL, /* bloom filter */
774 * Add the path to the peer and update the path used to reach it in case this
777 * @param peer_info Destination peer to add the path to.
778 * @param path New path to add. Last peer must be the peer in arg 1.
779 * Path will be either used of freed if already known.
781 * TODO: trim the part from origin to us? Add it as path to origin?
784 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
786 struct MeshPeerPath *aux;
790 if (NULL == peer_info || NULL == path)
796 l = path_get_length (path);
798 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
800 l2 = path_get_length (aux);
803 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
804 peer_info->path_tail, aux, path);
808 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
815 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
822 * Add the path to the origin peer and update the path used to reach it in case
823 * this is the shortest.
824 * The path is given in peer_info -> destination, therefore we turn the path
827 * @param peer_info Peer to add the path to, being the origin of the path.
828 * @param path New path to add after being inversed.
831 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
834 path_add_to_peer (peer_info, path);
839 * Build a PeerPath from the paths returned from the DHT, reversing the paths
840 * to obtain a local peer -> destination path and interning the peer ids.
842 * @param get_path NULL-terminated array of pointers
843 * to the peers on reverse GET path (or NULL if not recorded)
844 * @param put_path NULL-terminated array of pointers
845 * to the peers on the PUT path (or NULL if not recorded)
847 * @return Newly allocated and created path
849 static struct MeshPeerPath *
850 path_build_from_dht (const struct GNUNET_PeerIdentity *const *get_path,
851 const struct GNUNET_PeerIdentity *const *put_path)
853 struct MeshPeerPath *p;
858 for (i = 0; get_path[i] != NULL; i++) ;
859 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
860 for (i--; i >= 0; i--)
862 id = GNUNET_PEER_intern (get_path[i]);
863 if (p->length > 0 && id == p->peers[p->length - 1])
865 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
866 GNUNET_PEER_change_rc(id, -1);
871 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
872 p->peers[p->length] = id;
876 for (i = 0; put_path[i] != NULL; i++) ;
877 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
878 for (i--; i >= 0; i--)
880 id = GNUNET_PEER_intern (put_path[i]);
881 if (p->length > 0 && id == p->peers[p->length - 1])
883 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
884 GNUNET_PEER_change_rc(id, -1);
889 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
890 p->peers[p->length] = id;
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "MESH: (first of GET: %s)\n",
897 GNUNET_h2s_full(&get_path[0]->hashPubKey));
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899 "MESH: (first of PUT: %s)\n",
900 GNUNET_h2s_full(&put_path[0]->hashPubKey));
901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
902 "MESH: In total: %d hops\n",
910 * Send keepalive packets for a peer
912 * @param cls Closure (tunnel for which to send the keepalive).
913 * @param tc Notification context.
916 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
918 struct MeshTunnel *t = cls;
920 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
925 * TODO: implement explicit multicast keepalive? */
926 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
927 GNUNET_TIME_UNIT_FOREVER_REL, NULL,
928 sizeof (struct GNUNET_MESH_ManipulatePath),
929 NULL, //&send_core_data_multicast,
931 t->path_refresh_task =
932 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
938 * Check if client has registered with the service and has not disconnected
940 * @param client the client to check
942 * @return non-NULL if client exists in the global DLL
944 static struct MeshClient *
945 client_get (struct GNUNET_SERVER_Client *client)
947 struct MeshClient *c;
952 if (c->handle == client)
961 * Checks if a given client has subscribed to certain message type
963 * @param message_type Type of message to check
964 * @param c Client to check
966 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
971 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
975 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
976 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
981 * Search for a tunnel among the tunnels for a client
983 * @param c the client whose tunnels to search in
984 * @param tid the local id of the tunnel
986 * @return tunnel handler, NULL if doesn't exist
988 static struct MeshTunnel *
989 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
991 GNUNET_HashCode hash;
993 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
994 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
999 * Search for a tunnel by global ID using PEER_ID
1001 * @param pi owner of the tunnel
1002 * @param tid global tunnel number
1004 * @return tunnel handler, NULL if doesn't exist
1006 static struct MeshTunnel *
1007 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1009 struct MESH_TunnelID id;
1010 GNUNET_HashCode hash;
1015 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1016 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1021 * Search for a tunnel by global ID using full PeerIdentities
1023 * @param oid owner of the tunnel
1024 * @param tid global tunnel number
1026 * @return tunnel handler, NULL if doesn't exist
1028 static struct MeshTunnel *
1029 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1031 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1036 * Callback used to notify a client owner of a tunnel that a peer has
1037 * disconnected, most likely because of a path change.
1039 * @param n Node in the tree representing the disconnected peer
1042 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1044 struct GNUNET_MESH_PeerControl msg;
1046 if (NULL == n->t->client || NULL == nc)
1049 msg.header.size = htons (sizeof (msg));
1050 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1051 msg.tunnel_id = htonl (n->t->local_tid);
1052 GNUNET_PEER_resolve (n->peer, &msg.peer);
1053 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1054 &msg.header, GNUNET_NO);
1059 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1060 * needed rescources.
1062 * @param t Tunnel we want to add a new peer to
1063 * @param peer PeerInfo of the peer being added
1067 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1069 // struct MeshTunnelTreeNode *n;
1070 struct MeshPeerPath *p;
1071 struct MeshPeerPath *best_p;
1072 unsigned int best_cost;
1075 if (NULL != tree_find_peer(t->tree->root, peer->id))
1077 /* Already have it, nothing to do. */
1082 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1083 if (NULL == (p = peer->path_head))
1090 best_cost = UINT_MAX;
1093 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1100 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1101 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1102 t->path_refresh_task =
1103 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1108 * Notify a tunnel that a connection has broken that affects at least
1109 * some of its peers.
1111 * @param t Tunnel affected.
1112 * @param peer Peer that (at least) has been affected by the disconnection.
1113 * @param p1 Peer that got disconnected from p2.
1114 * @param p2 Peer that got disconnected from p1.
1116 * @return Short ID of the peer disconnected (either p1 or p2).
1117 * 0 if the tunnel remained unaffected.
1119 static GNUNET_PEER_Id
1120 tunnel_notify_connection_broken (struct MeshTunnel *t,
1121 struct MeshPeerInfo *peer,
1125 return tree_notify_connection_broken (t->tree, p1, p2,
1126 ¬ify_peer_disconnected);
1131 * Destroy the tunnel and free any allocated resources linked to it
1133 * @param t the tunnel to destroy
1135 * @return GNUNET_OK on success
1138 tunnel_destroy (struct MeshTunnel *t)
1140 struct MeshClient *c;
1141 struct MeshQueue *q;
1142 struct MeshQueue *qn;
1143 GNUNET_HashCode hash;
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1155 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1156 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1161 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1162 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1166 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1170 if (NULL != q->data)
1171 GNUNET_free (q->data);
1175 /* TODO cancel core transmit ready in case it was active */
1177 tree_destroy(t->tree);
1184 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1185 * client when the client disconnects.
1187 * @param cls closure (client that is disconnecting)
1188 * @param key the hash of the local tunnel id (used to access the hashmap)
1189 * @param value the value stored at the key (tunnel to destroy)
1191 * @return GNUNET_OK on success
1194 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1198 r = tunnel_destroy ((struct MeshTunnel *) value);
1203 /******************************************************************************/
1204 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1205 /******************************************************************************/
1209 * Function called to notify a client about the socket
1210 * being ready to queue more data. "buf" will be
1211 * NULL and "size" zero if the socket was closed for
1212 * writing in the meantime.
1214 * @param cls closure
1215 * @param size number of bytes available in buf
1216 * @param buf where the callee should write the message
1217 * @return number of bytes written to buf
1220 send_core_create_path (void *cls, size_t size, void *buf)
1222 struct MeshPathInfo *info = cls;
1223 struct GNUNET_MESH_ManipulatePath *msg;
1224 struct GNUNET_PeerIdentity *peer_ptr;
1225 struct MeshPeerInfo *peer = info->peer;
1226 struct MeshTunnel *t = info->t;
1227 struct MeshPeerPath *p = info->path;
1232 sizeof (struct GNUNET_MESH_ManipulatePath) +
1233 p->length * sizeof (struct GNUNET_PeerIdentity);
1235 if (size < size_needed || NULL == buf)
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
1238 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1239 GNUNET_TIME_UNIT_FOREVER_REL,
1240 path_get_first_hop (t->tree, peer->id),
1241 size_needed, &send_core_create_path,
1246 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1247 msg->header.size = htons (size_needed);
1248 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1249 msg->tid = ntohl (t->id.tid);
1251 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1252 for (i = 0; i < p->length; i++)
1254 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1266 * Function called to notify a client about the socket
1267 * being ready to queue more data. "buf" will be
1268 * NULL and "size" zero if the socket was closed for
1269 * writing in the meantime.
1271 * @param cls closure (MeshDataDescriptor with all info to build packet)
1272 * @param size number of bytes available in buf
1273 * @param buf where the callee should write the message
1274 * @return number of bytes written to buf
1277 send_core_data_to_origin (void *cls, size_t size, void *buf)
1279 struct MeshDataDescriptor *info = cls;
1280 struct GNUNET_MESH_ToOrigin *msg = buf;
1283 GNUNET_assert (NULL != info);
1284 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1285 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1287 if (total_size > size)
1289 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1290 "not enough buffer to send data to origin\n");
1293 msg->header.size = htons (total_size);
1294 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1295 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1296 msg->tid = htonl (info->origin->tid);
1297 if (0 != info->size)
1299 memcpy (&msg[1], &info[1], info->size);
1301 if (NULL != info->client)
1303 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1312 * Function called to notify a client about the socket
1313 * being ready to queue more data. "buf" will be
1314 * NULL and "size" zero if the socket was closed for
1315 * writing in the meantime.
1317 * @param cls closure (data itself)
1318 * @param size number of bytes available in buf
1319 * @param buf where the callee should write the message
1321 * @return number of bytes written to buf
1326 send_core_data_multicast (void *cls, size_t size, void *buf)
1328 struct MeshDataDescriptor *info = cls;
1329 struct GNUNET_MESH_Multicast *msg = buf;
1332 GNUNET_assert (NULL != info);
1333 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1334 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1338 info->peer->core_transmit[info->handler_n] = NULL;
1339 info->peer->infos[info->handler_n] = NULL;
1341 if (total_size > size)
1348 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1349 msg->header.size = htons (total_size);
1350 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1351 msg->tid = htonl (info->origin->tid);
1352 memcpy (&msg[1], info->data, total_size);
1354 if (0 == --(*info->copies))
1356 if (NULL != info->client)
1358 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1360 GNUNET_free (info->data);
1361 GNUNET_free (info->copies);
1369 * Function called to notify a client about the socket
1370 * being ready to queue more data. "buf" will be
1371 * NULL and "size" zero if the socket was closed for
1372 * writing in the meantime.
1374 * @param cls closure (MeshDataDescriptor)
1375 * @param size number of bytes available in buf
1376 * @param buf where the callee should write the message
1377 * @return number of bytes written to buf
1380 send_core_path_ack (void *cls, size_t size, void *buf)
1382 struct MeshDataDescriptor *info = cls;
1383 struct GNUNET_MESH_PathACK *msg = buf;
1385 GNUNET_assert (NULL != info);
1388 info->peer->core_transmit[info->handler_n] = NULL;
1390 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1395 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1396 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1397 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1398 msg->tid = htonl (info->origin->tid);
1399 msg->peer_id = my_full_id;
1401 /* TODO add signature */
1403 return sizeof (struct GNUNET_MESH_PathACK);
1408 * Function called to notify a client about the socket
1409 * being ready to queue more data. "buf" will be
1410 * NULL and "size" zero if the socket was closed for
1411 * writing in the meantime.
1413 * @param cls closure (data itself)
1414 * @param size number of bytes available in buf
1415 * @param buf where the callee should write the message
1416 * @return number of bytes written to buf
1419 send_core_data_raw (void *cls, size_t size, void *buf)
1421 struct GNUNET_MessageHeader *msg = cls;
1424 GNUNET_assert (NULL != msg);
1425 total_size = ntohs (msg->size);
1427 if (total_size > size)
1432 memcpy (buf, msg, total_size);
1440 * Send another peer a notification to destroy a tunnel
1441 * @param cls The tunnel to destroy
1442 * @param size Size in the buffer
1443 * @param buf Memory where to put the data to transmit
1444 * @return Size of data put in buffer
1447 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1449 struct MeshTunnel *t = cls;
1450 struct MeshClient *c;
1451 struct GNUNET_MESH_TunnelMessage *msg;
1455 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1456 /*FIXME*/ msg->header.size =
1457 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1458 msg->tunnel_id = htonl (t->id.tid);
1460 tunnel_destroy (c, t);
1461 return sizeof (struct GNUNET_MESH_TunnelMessage);
1467 * Send the message to all clients that have subscribed to its type
1469 * @param msg Pointer to the message itself
1470 * @return number of clients this message was sent to
1473 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1475 struct MeshClient *c;
1479 type = ntohs (msg->type);
1480 for (count = 0, c = clients; c != NULL; c = c->next)
1482 if (client_is_subscribed (type, c))
1485 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1495 * Notify the client that owns the tunnel that a peer has connected to it
1497 * @param t Tunnel whose owner to notify
1498 * @param id Short id of the peer that has connected
1501 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1503 struct GNUNET_MESH_PeerControl pc;
1505 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1506 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1507 pc.tunnel_id = htonl (t->local_tid);
1508 GNUNET_PEER_resolve (id, &pc.peer);
1509 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1510 &pc.header, GNUNET_NO);
1514 /******************************************************************************/
1515 /******************** MESH NETWORK HANDLERS **************************/
1516 /******************************************************************************/
1520 * Core handler for path creation
1521 * struct GNUNET_CORE_MessageHandler
1523 * @param cls closure
1524 * @param message message
1525 * @param peer peer identity this notification is about
1526 * @param atsi performance data
1527 * @return GNUNET_OK to keep the connection open,
1528 * GNUNET_SYSERR to close it (signal serious error)
1532 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1533 const struct GNUNET_MessageHeader *message,
1534 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1536 unsigned int own_pos;
1539 MESH_TunnelNumber tid;
1540 struct GNUNET_MESH_ManipulatePath *msg;
1541 struct GNUNET_PeerIdentity *pi;
1542 struct GNUNET_PeerIdentity id;
1543 GNUNET_HashCode hash;
1544 struct MeshPeerPath *path;
1545 struct MeshPeerInfo *dest_peer_info;
1546 struct MeshPeerInfo *orig_peer_info;
1547 struct MeshTunnel *t;
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1550 "MESH: Received a MESH path create msg\n");
1551 size = ntohs (message->size);
1552 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1554 GNUNET_break_op (0);
1558 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1559 if (size % sizeof (struct GNUNET_PeerIdentity))
1561 GNUNET_break_op (0);
1564 size /= sizeof (struct GNUNET_PeerIdentity);
1567 GNUNET_break_op (0);
1570 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1572 tid = ntohl (msg->tid);
1573 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1574 t = tunnel_get (pi, tid);
1578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1579 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1580 t->id.oid = GNUNET_PEER_intern (pi);
1582 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1584 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1586 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1587 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1595 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1596 if (NULL == dest_peer_info)
1598 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1599 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1600 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1602 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1604 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1605 if (NULL == orig_peer_info)
1607 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1608 orig_peer_info->id = GNUNET_PEER_intern (pi);
1609 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1610 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1613 path = path_new (size);
1615 for (i = 0; i < size; i++)
1617 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1618 if (path->peers[i] == myid)
1622 { /* cannot be self, must be 'not found' */
1623 /* create path: self not found in path through self */
1624 GNUNET_break_op (0);
1625 path_destroy (path);
1626 /* FIXME error. destroy tunnel? leave for timeout? */
1629 if (own_pos == size - 1)
1631 /* It is for us! Send ack. */
1632 struct MeshDataDescriptor *info;
1635 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1636 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1637 info->origin = &t->id;
1638 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1639 GNUNET_assert (info->peer);
1640 for (j = 0; info->peer->core_transmit[j]; j++)
1648 info->handler_n = j;
1649 info->peer->core_transmit[j] =
1650 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1651 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1652 sizeof (struct GNUNET_MessageHeader),
1653 &send_core_path_ack, info);
1657 /* It's for somebody else! Retransmit. */
1658 struct MeshPathInfo *path_info;
1660 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1662 path_info->path = path;
1663 path_info->peer = dest_peer_info;
1665 path_add_to_peer (dest_peer_info, path);
1666 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1667 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1668 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1669 sizeof (struct GNUNET_MessageHeader),
1670 &send_core_create_path, path_info);
1677 * Core handler for mesh network traffic going from the origin to a peer
1679 * @param cls closure
1680 * @param peer peer identity this notification is about
1681 * @param message message
1682 * @param atsi performance data
1683 * @return GNUNET_OK to keep the connection open,
1684 * GNUNET_SYSERR to close it (signal serious error)
1687 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1688 const struct GNUNET_MessageHeader *message,
1689 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1691 struct GNUNET_MESH_Unicast *msg;
1692 struct MeshTunnel *t;
1693 struct MeshPeerInfo *pi;
1696 size = ntohs (message->size);
1698 sizeof (struct GNUNET_MESH_Unicast) +
1699 sizeof (struct GNUNET_MessageHeader))
1704 msg = (struct GNUNET_MESH_Unicast *) message;
1705 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1708 /* TODO notify back: we don't know this tunnel */
1711 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1712 &msg->destination.hashPubKey);
1715 /* TODO maybe feedback, log to statistics */
1720 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1723 msg = GNUNET_malloc (size);
1724 memcpy (msg, message, size);
1725 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1726 GNUNET_TIME_UNIT_FOREVER_REL,
1727 path_get_first_hop (t->tree, pi->id),
1729 &send_core_data_raw, msg);
1735 * Core handler for mesh network traffic going from the origin to all peers
1737 * @param cls closure
1738 * @param message message
1739 * @param peer peer identity this notification is about
1740 * @param atsi performance data
1741 * @return GNUNET_OK to keep the connection open,
1742 * GNUNET_SYSERR to close it (signal serious error)
1744 * TODO: Check who we got this from, to validate route.
1747 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1748 const struct GNUNET_MessageHeader *message,
1749 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1751 struct GNUNET_MESH_Multicast *msg;
1752 struct GNUNET_PeerIdentity *id;
1753 struct MeshDataDescriptor *info;
1754 struct MeshTunnelTreeNode *n;
1755 struct MeshTunnel *t;
1756 unsigned int *copies;
1761 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
1762 if (size < sizeof (struct GNUNET_MessageHeader))
1764 GNUNET_break_op (0);
1767 msg = (struct GNUNET_MESH_Multicast *) message;
1768 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1772 /* TODO notify that we dont know that tunnel */
1773 GNUNET_break_op (0);
1777 /* Transmit to locally interested clients */
1778 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1780 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1782 n = t->tree->me->children_head;
1785 copies = GNUNET_malloc (sizeof (unsigned int));
1786 for (*copies = 0; NULL != n; n = n->next)
1788 n = t->tree->me->children_head;
1789 data = GNUNET_malloc (size);
1790 memcpy (data, &msg[1], size);
1793 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1794 info->origin = &t->id;
1797 info->copies = copies;
1798 info->client = t->client->handle;
1799 info->destination = n->peer;
1800 id = path_get_first_hop(t->tree, n->peer);
1801 info->peer = peer_info_get(id);
1802 GNUNET_assert (NULL != info->peer);
1803 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
1805 if (i == (CORE_QUEUE_SIZE - 1))
1811 info->handler_n = i;
1812 info->peer->infos[i] = info;
1813 info->peer->core_transmit[i] =
1814 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1815 GNUNET_TIME_UNIT_FOREVER_REL, id,
1816 ntohs (msg->header.size),
1817 &send_core_data_multicast, info);
1825 * Core handler for mesh network traffic
1827 * @param cls closure
1828 * @param message message
1829 * @param peer peer identity this notification is about
1830 * @param atsi performance data
1832 * @return GNUNET_OK to keep the connection open,
1833 * GNUNET_SYSERR to close it (signal serious error)
1836 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1837 const struct GNUNET_MessageHeader *message,
1838 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1840 struct GNUNET_MESH_ToOrigin *msg;
1841 struct GNUNET_PeerIdentity id;
1842 struct MeshPeerInfo *peer_info;
1843 struct MeshTunnel *t;
1846 size = ntohs (message->size);
1847 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1848 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1850 GNUNET_break_op (0);
1853 msg = (struct GNUNET_MESH_ToOrigin *) message;
1854 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1858 /* TODO notify that we dont know this tunnel (whom)? */
1862 if (t->id.oid == myid)
1864 if (NULL == t->client)
1866 /* got data packet for ownerless tunnel */
1867 GNUNET_break_op (0);
1870 /* TODO signature verification */
1871 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1875 peer_info = peer_info_get (&msg->oid);
1876 if (NULL == peer_info)
1878 /* unknown origin of tunnel */
1882 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1883 msg = GNUNET_malloc (size);
1884 memcpy (msg, message, size);
1885 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1886 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1887 &send_core_data_raw, msg);
1894 * Core handler for path ACKs
1896 * @param cls closure
1897 * @param message message
1898 * @param peer peer identity this notification is about
1899 * @param atsi performance data
1901 * @return GNUNET_OK to keep the connection open,
1902 * GNUNET_SYSERR to close it (signal serious error)
1905 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1906 const struct GNUNET_MessageHeader *message,
1907 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1909 struct GNUNET_MESH_PathACK *msg;
1910 struct MeshTunnelTreeNode *n;
1911 struct MeshPeerInfo *peer_info;
1912 struct MeshTunnel *t;
1914 msg = (struct GNUNET_MESH_PathACK *) message;
1915 t = tunnel_get (&msg->oid, msg->tid);
1918 /* TODO notify that we don't know the tunnel */
1922 /* Message for us? */
1923 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1925 if (NULL == t->client)
1927 GNUNET_break_op (0);
1930 peer_info = peer_info_get (&msg->peer_id);
1931 if (NULL == peer_info)
1933 GNUNET_break_op (0);
1936 n = tree_find_peer(t->tree->root, peer_info->id);
1939 GNUNET_break_op (0);
1942 n->status = MESH_PEER_READY;
1943 send_client_peer_connected(t, peer_info->id);
1947 peer_info = peer_info_get (&msg->oid);
1948 if (NULL == peer_info)
1950 /* If we know the tunnel, we should DEFINITELY know the peer */
1954 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1955 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1956 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1957 GNUNET_TIME_UNIT_FOREVER_REL,
1958 path_get_first_hop (t->tree,
1960 sizeof (struct GNUNET_MESH_PathACK),
1961 &send_core_data_raw, msg);
1967 * Functions to handle messages from core
1969 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1970 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1971 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1972 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1973 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1974 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1975 sizeof (struct GNUNET_MESH_PathACK)},
1981 /******************************************************************************/
1982 /**************** MESH LOCAL HANDLER HELPERS ***********************/
1983 /******************************************************************************/
1986 * deregister_app: iterator for removing each application registered by a client
1988 * @param cls closure
1989 * @param key the hash of the application id (used to access the hashmap)
1990 * @param value the value stored at the key (client)
1992 * @return GNUNET_OK on success
1995 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
1997 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2003 * notify_client_connection_failure: notify a client that the connection to the
2004 * requested remote peer is not possible (for instance, no route found)
2005 * Function called when the socket is ready to queue more data. "buf" will be
2006 * NULL and "size" zero if the socket was closed for writing in the meantime.
2008 * @param cls closure
2009 * @param size number of bytes available in buf
2010 * @param buf where the callee should write the message
2011 * @return number of bytes written to buf
2014 notify_client_connection_failure (void *cls, size_t size, void *buf)
2017 struct MeshPeerInfo *peer_info;
2018 struct GNUNET_MESH_PeerControl *msg;
2019 struct GNUNET_PeerIdentity id;
2021 if (0 == size && NULL == buf)
2023 // TODO retry? cancel?
2027 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2028 peer_info = (struct MeshPeerInfo *) cls;
2029 msg = (struct GNUNET_MESH_PeerControl *) buf;
2030 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2031 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2032 // msg->tunnel_id = htonl(peer_info->t->tid);
2033 GNUNET_PEER_resolve (peer_info->id, &id);
2034 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2042 * Function to process paths received for a new peer addition. The recorded
2043 * paths form the initial tunnel, which can be optimized later.
2044 * Called on each result obtained for the DHT search.
2046 * @param cls closure
2047 * @param exp when will this value expire
2048 * @param key key of the result
2049 * @param get_path NULL-terminated array of pointers
2050 * to the peers on reverse GET path (or NULL if not recorded)
2051 * @param put_path NULL-terminated array of pointers
2052 * to the peers on the PUT path (or NULL if not recorded)
2053 * @param type type of the result
2054 * @param size number of bytes in data
2055 * @param data pointer to the result data
2057 * TODO: re-issue the request after certain time? cancel after X results?
2060 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2061 const GNUNET_HashCode * key,
2062 const struct GNUNET_PeerIdentity *const *get_path,
2063 const struct GNUNET_PeerIdentity *const *put_path,
2064 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2066 struct MeshPathInfo *path_info = cls;
2067 struct MeshPeerPath *p;
2068 struct GNUNET_PeerIdentity pi;
2071 if (NULL == get_path || NULL == put_path)
2073 if (NULL == path_info->peer->path_head)
2075 // Find ourselves some alternate initial path to the destination: retry
2076 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2077 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2078 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2079 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2080 GNUNET_BLOCK_TYPE_TEST, /* type */
2081 &pi.hashPubKey, /*key to search */
2082 4, /* replication level */
2083 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2086 0, /* xquery bits */
2088 (void *) path_info);
2093 p = path_build_from_dht (get_path, put_path);
2094 path_add_to_peer (path_info->peer, p);
2095 for (i = 0; i < path_info->peer->ntunnels; i++)
2097 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2099 GNUNET_free (path_info);
2106 * Function to process paths received for a new peer addition. The recorded
2107 * paths form the initial tunnel, which can be optimized later.
2108 * Called on each result obtained for the DHT search.
2110 * @param cls closure
2111 * @param exp when will this value expire
2112 * @param key key of the result
2113 * @param get_path NULL-terminated array of pointers
2114 * to the peers on reverse GET path (or NULL if not recorded)
2115 * @param put_path NULL-terminated array of pointers
2116 * to the peers on the PUT path (or NULL if not recorded)
2117 * @param type type of the result
2118 * @param size number of bytes in data
2119 * @param data pointer to the result data
2122 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2123 const GNUNET_HashCode * key,
2124 const struct GNUNET_PeerIdentity *const *get_path,
2125 const struct GNUNET_PeerIdentity *const *put_path,
2126 enum GNUNET_BLOCK_Type type, size_t size,
2129 const struct GNUNET_PeerIdentity *pi = data;
2130 struct GNUNET_PeerIdentity id;
2131 struct MeshTunnel *t = cls;
2132 struct MeshPeerInfo *peer_info;
2133 struct MeshPathInfo *path_info;
2134 struct MeshPeerPath *p;
2137 if (size != sizeof (struct GNUNET_PeerIdentity))
2139 GNUNET_break_op (0);
2142 GNUNET_assert (NULL != t->client);
2143 GNUNET_DHT_get_stop (t->client->dht_get_type);
2144 t->client->dht_get_type = NULL;
2145 peer_info = peer_info_get (pi);
2146 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2147 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2149 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2150 NULL == peer_info->dhtget)
2152 /* we don't have a route to the peer, let's try a direct lookup */
2153 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2155 GNUNET_TIME_UNIT_FOREVER_REL,
2157 GNUNET_BLOCK_TYPE_TEST,
2160 /* key to look up */
2162 /* replication level */
2163 GNUNET_DHT_RO_RECORD_ROUTE,
2164 /* option to dht: record route */
2165 NULL, /* bloom filter */
2168 0, /* xquery bits */
2171 peer_info); /* closure */
2174 p = path_build_from_dht (get_path, put_path);
2175 path_add_to_peer (peer_info, p);
2176 tunnel_add_peer(t, peer_info);
2177 p = tree_get_path_to_peer(t->tree, peer_info->id);
2179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2180 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2181 t->local_tid, p->length);
2182 for (i = 0; i < p->length; i++)
2184 GNUNET_PEER_resolve (p->peers[0], &id);
2185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2186 GNUNET_h2s_full (&id.hashPubKey));
2192 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2194 path_info->peer = peer_info;
2195 path_info->path = p;
2196 GNUNET_PEER_resolve (p->peers[1], &id);
2197 GNUNET_CORE_notify_transmit_ready (core_handle,
2203 GNUNET_TIME_UNIT_FOREVER_REL,
2207 sizeof (struct GNUNET_MESH_ManipulatePath)
2210 sizeof (struct GNUNET_PeerIdentity)),
2212 &send_core_create_path,
2214 path_info); /* cls */
2218 send_client_peer_connected(t, myid);
2221 /******************************************************************************/
2222 /********************* MESH LOCAL HANDLES **************************/
2223 /******************************************************************************/
2227 * Handler for client disconnection
2229 * @param cls closure
2230 * @param client identification of the client; NULL
2231 * for the last call when the server is destroyed
2234 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2236 struct MeshClient *c;
2237 struct MeshClient *next;
2239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2245 if (c->handle != client && NULL != client)
2247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2252 if (NULL != c->tunnels)
2254 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2255 &tunnel_destroy_iterator,
2257 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2260 /* deregister clients applications */
2261 if (NULL != c->apps)
2263 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2264 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2266 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2267 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2269 GNUNET_SCHEDULER_cancel (announce_applications_task);
2270 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2272 if (NULL != c->types)
2273 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2274 if (NULL != c->dht_get_type)
2275 GNUNET_DHT_get_stop (c->dht_get_type);
2276 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2288 * Handler for new clients
2290 * @param cls closure
2291 * @param client identification of the client
2292 * @param message the actual message, which includes messages the client wants
2295 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2296 const struct GNUNET_MessageHeader *message)
2298 struct GNUNET_MESH_ClientConnect *cc_msg;
2299 struct MeshClient *c;
2300 GNUNET_MESH_ApplicationType *a;
2307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2308 /* Check data sanity */
2309 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2310 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2311 ntypes = ntohs (cc_msg->types);
2312 napps = ntohs (cc_msg->applications);
2314 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2317 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2321 /* Create new client structure */
2322 c = GNUNET_malloc (sizeof (struct MeshClient));
2324 c->id = next_client_id++;
2327 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2330 GNUNET_MESH_ApplicationType at;
2333 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2334 for (i = 0; i < napps; i++)
2337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2338 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2339 /* store in clients hashmap */
2340 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2341 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2342 /* store in global hashmap, for announcements */
2343 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2344 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2346 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2347 announce_applications_task =
2348 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2356 t = (uint16_t *) & a[napps];
2357 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2358 for (i = 0; i < ntypes; i++)
2361 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2363 /* store in clients hashmap */
2364 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2365 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2366 /* store in global hashmap */
2367 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2368 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2372 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2374 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2375 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2376 GNUNET_SERVER_notification_context_add (nc, client);
2378 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2386 * Handler for requests of new tunnels
2388 * @param cls closure
2389 * @param client identification of the client
2390 * @param message the actual message
2393 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2394 const struct GNUNET_MessageHeader *message)
2396 struct GNUNET_MESH_TunnelMessage *t_msg;
2397 struct MeshTunnel *t;
2398 struct MeshClient *c;
2399 GNUNET_HashCode hash;
2401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2403 /* Sanity check for client registration */
2404 if (NULL == (c = client_get (client)))
2407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2414 /* Message sanity check */
2415 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2418 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2422 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2423 /* Sanity check for tunnel numbering */
2424 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2427 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2430 /* Sanity check for duplicate tunnel IDs */
2431 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2434 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2438 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2440 while (NULL != tunnel_get_by_pi (myid, next_tid))
2441 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2442 t->id.tid = next_tid++;
2444 t->local_tid = ntohl (t_msg->tunnel_id);
2446 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2448 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2450 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2451 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2454 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2458 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2460 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2461 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2464 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2467 t->tree = tree_new (t, myid);
2468 t->tree->refresh = REFRESH_PATH_TIME;
2469 t->tree->root->status = MESH_PEER_READY;
2470 t->tree->me = t->tree->root;
2472 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2478 * Handler for requests of deleting tunnels
2480 * @param cls closure
2481 * @param client identification of the client
2482 * @param message the actual message
2485 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2486 const struct GNUNET_MessageHeader *message)
2488 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2489 struct MeshClient *c;
2490 struct MeshTunnel *t;
2491 MESH_TunnelNumber tid;
2492 GNUNET_HashCode hash;
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2496 /* Sanity check for client registration */
2497 if (NULL == (c = client_get (client)))
2500 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2503 /* Message sanity check */
2504 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2507 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2513 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2515 /* Retrieve tunnel */
2516 tid = ntohl (tunnel_msg->tunnel_id);
2518 /* Remove from local id hashmap */
2519 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2520 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2521 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2523 /* Remove from global id hashmap */
2524 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2525 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2527 // notify_tunnel_destroy(t); FIXME
2529 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2535 * Handler for connection requests to new peers
2537 * @param cls closure
2538 * @param client identification of the client
2539 * @param message the actual message (PeerControl)
2542 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2543 const struct GNUNET_MessageHeader *message)
2545 struct GNUNET_MESH_PeerControl *peer_msg;
2546 struct MeshClient *c;
2547 struct MeshTunnel *t;
2548 MESH_TunnelNumber tid;
2549 struct MeshPeerInfo *peer_info;
2552 /* Sanity check for client registration */
2553 if (NULL == (c = client_get (client)))
2556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2560 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2561 /* Sanity check for message size */
2562 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2565 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2569 /* Tunnel exists? */
2570 tid = ntohl (peer_msg->tunnel_id);
2571 t = tunnel_get_by_local_id (c, tid);
2575 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2579 /* Does client own tunnel? */
2580 if (t->client->handle != client)
2583 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2587 peer_info = peer_info_get (&peer_msg->peer);
2589 /* Start DHT search if needed, otherwise just add peer to tunnel. */
2590 if (NULL == peer_info->dhtget && NULL == peer_info->path_head)
2592 struct MeshPathInfo *path_info;
2594 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2595 path_info->peer = peer_info;
2597 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
2598 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2599 GNUNET_BLOCK_TYPE_TEST, /* type */
2600 &peer_msg->peer.hashPubKey, /*key to search */
2601 4, /* replication level */
2602 GNUNET_DHT_RO_RECORD_ROUTE,
2603 NULL, /* bloom filter */
2606 0, /* xquery bits */
2608 (void *) path_info);
2610 if (NULL != peer_info->path_head)
2612 tunnel_add_peer(t, peer_info);
2614 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2620 * Handler for disconnection requests of peers in a tunnel
2622 * @param cls closure
2623 * @param client identification of the client
2624 * @param message the actual message (PeerControl)
2627 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2628 const struct GNUNET_MessageHeader *message)
2630 struct GNUNET_MESH_PeerControl *peer_msg;
2631 struct MeshClient *c;
2632 struct MeshTunnel *t;
2633 MESH_TunnelNumber tid;
2635 /* Sanity check for client registration */
2636 if (NULL == (c = client_get (client)))
2639 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2642 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2643 /* Sanity check for message size */
2644 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2651 /* Tunnel exists? */
2652 tid = ntohl (peer_msg->tunnel_id);
2653 t = tunnel_get_by_local_id (c, tid);
2657 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2661 /* Does client own tunnel? */
2662 if (t->client->handle != client)
2665 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2669 /* Ok, delete peer from tunnel */
2670 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2671 &peer_msg->peer.hashPubKey);
2673 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2679 * Handler for connection requests to new peers by type
2681 * @param cls closure
2682 * @param client identification of the client
2683 * @param message the actual message (ConnectPeerByType)
2686 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2687 const struct GNUNET_MessageHeader *message)
2689 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2690 struct MeshClient *c;
2691 struct MeshTunnel *t;
2692 GNUNET_HashCode hash;
2693 GNUNET_MESH_ApplicationType type;
2694 MESH_TunnelNumber tid;
2696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2697 /* Sanity check for client registration */
2698 if (NULL == (c = client_get (client)))
2701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2705 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2706 /* Sanity check for message size */
2707 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2708 ntohs (connect_msg->header.size))
2711 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2715 /* Tunnel exists? */
2716 tid = ntohl (connect_msg->tunnel_id);
2717 t = tunnel_get_by_local_id (c, tid);
2721 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2725 /* Does client own tunnel? */
2726 if (t->client->handle != client)
2729 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2733 /* Do WE have the service? */
2734 type = ntohl (connect_msg->type);
2735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2736 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2737 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2740 /* Yes! Fast forward, add ourselves to the tunnel and send the
2741 * good news to the client
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2744 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2745 peer_info_get (&my_full_id),
2746 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2749 send_client_peer_connected(t, myid);
2750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2751 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2754 /* Ok, lets find a peer offering the service */
2755 if (c->dht_get_type)
2757 GNUNET_DHT_get_stop (c->dht_get_type);
2759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2760 GNUNET_h2s_full (&hash));
2762 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2763 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2764 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
2765 &dht_get_type_handler, t);
2767 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2773 * Handler for client traffic directed to one peer
2775 * @param cls closure
2776 * @param client identification of the client
2777 * @param message the actual message
2780 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2781 const struct GNUNET_MessageHeader *message)
2783 struct MeshClient *c;
2784 struct MeshTunnel *t;
2785 struct MeshPeerInfo *pi;
2786 struct GNUNET_MESH_Unicast *data_msg;
2787 MESH_TunnelNumber tid;
2790 /* Sanity check for client registration */
2791 if (NULL == (c = client_get (client)))
2794 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2797 data_msg = (struct GNUNET_MESH_Unicast *) message;
2798 /* Sanity check for message size */
2799 size = ntohs (message->size);
2800 if (sizeof (struct GNUNET_MESH_Unicast) +
2801 sizeof (struct GNUNET_MessageHeader) > size)
2804 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2808 /* Tunnel exists? */
2809 tid = ntohl (data_msg->tid);
2810 t = tunnel_get_by_local_id (c, tid);
2814 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2818 /* Is it a local tunnel? Then, does client own the tunnel? */
2819 if (t->client->handle != NULL && t->client->handle != client)
2822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2826 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2827 &data_msg->destination.hashPubKey);
2828 /* Is the selected peer in the tunnel? */
2832 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2836 /* Ok, everything is correct, send the message
2837 * (pretend we got it from a mesh peer)
2840 char buf[ntohs (message->size)];
2841 struct GNUNET_MESH_Unicast *copy;
2843 /* Work around const limitation */
2844 copy = (struct GNUNET_MESH_Unicast *) buf;
2845 memcpy (buf, data_msg, size);
2846 copy->oid = my_full_id;
2847 copy->tid = htonl (t->id.tid);
2848 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
2849 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2855 * Handler for client traffic directed to all peers in a tunnel
2857 * @param cls closure
2858 * @param client identification of the client
2859 * @param message the actual message
2862 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2863 const struct GNUNET_MessageHeader *message)
2865 struct MeshClient *c;
2866 struct MeshTunnel *t;
2867 struct GNUNET_MESH_Multicast *data_msg;
2868 MESH_TunnelNumber tid;
2870 /* Sanity check for client registration */
2871 if (NULL == (c = client_get (client)))
2874 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2877 data_msg = (struct GNUNET_MESH_Multicast *) message;
2878 /* Sanity check for message size */
2879 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2882 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2886 /* Tunnel exists? */
2887 tid = ntohl (data_msg->tid);
2888 t = tunnel_get_by_local_id (c, tid);
2892 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2896 /* Does client own tunnel? */
2897 if (t->client->handle != client)
2900 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2906 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2911 * Functions to handle messages from clients
2913 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
2914 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2915 {&handle_local_tunnel_create, NULL,
2916 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2917 sizeof (struct GNUNET_MESH_TunnelMessage)},
2918 {&handle_local_tunnel_destroy, NULL,
2919 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2920 sizeof (struct GNUNET_MESH_TunnelMessage)},
2921 {&handle_local_connect_add, NULL,
2922 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
2923 sizeof (struct GNUNET_MESH_PeerControl)},
2924 {&handle_local_connect_del, NULL,
2925 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
2926 sizeof (struct GNUNET_MESH_PeerControl)},
2927 {&handle_local_connect_by_type, NULL,
2928 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
2929 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
2930 {&handle_local_unicast, NULL,
2931 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2932 {&handle_local_unicast, NULL,
2933 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2934 {&handle_local_multicast, NULL,
2935 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2941 * To be called on core init/fail.
2943 * @param cls service closure
2944 * @param server handle to the server for this service
2945 * @param identity the public identity of this peer
2946 * @param publicKey the public key of this peer
2949 core_init (void *cls, struct GNUNET_CORE_Handle *server,
2950 const struct GNUNET_PeerIdentity *identity,
2951 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
2954 core_handle = server;
2955 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
2957 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
2958 GNUNET_SCHEDULER_shutdown();
2964 * Method called whenever a given peer connects.
2966 * @param cls closure
2967 * @param peer peer identity this notification is about
2968 * @param atsi performance data for the connection
2971 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
2972 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2974 // GNUNET_PEER_Id pid;
2975 struct MeshPeerInfo *peer_info;
2976 struct MeshPeerPath *path;
2978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
2979 peer_info = peer_info_get (peer);
2980 if (myid == peer_info->id)
2982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
2985 path = path_new (2);
2986 path->peers[0] = myid;
2987 path->peers[1] = peer_info->id;
2988 path_add_to_peer (peer_info, path);
2993 * Method called whenever a peer disconnects.
2995 * @param cls closure
2996 * @param peer peer identity this notification is about
2999 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3001 struct MeshPeerInfo *pi;
3004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3005 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3011 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3013 if (pi->core_transmit[i])
3015 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3016 /* TODO: notify that tranmission has failed */
3017 GNUNET_free (pi->infos[i]);
3020 path_remove_from_peer (pi, pi->id, myid);
3023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3029 /******************************************************************************/
3030 /************************ MAIN FUNCTIONS ****************************/
3031 /******************************************************************************/
3034 * Task run during shutdown.
3040 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3042 struct MeshClient *c;
3044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3045 if (core_handle != NULL)
3047 GNUNET_CORE_disconnect (core_handle);
3050 if (dht_handle != NULL)
3052 for (c = clients; NULL != c; c = c->next)
3053 if (NULL != c->dht_get_type)
3054 GNUNET_DHT_get_stop (c->dht_get_type);
3055 GNUNET_DHT_disconnect (dht_handle);
3060 GNUNET_SERVER_notification_context_destroy (nc);
3063 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3065 GNUNET_SCHEDULER_cancel (announce_id_task);
3066 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3072 * Process mesh requests.
3074 * @param cls closure
3075 * @param server the initialized server
3076 * @param c configuration to use
3079 run (void *cls, struct GNUNET_SERVER_Handle *server,
3080 const struct GNUNET_CONFIGURATION_Handle *c)
3082 struct MeshPeerInfo *peer;
3083 struct MeshPeerPath *p;
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3087 server_handle = server;
3088 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3089 CORE_QUEUE_SIZE, /* queue size */
3090 NULL, /* Closure passed to MESH functions */
3091 &core_init, /* Call core_init once connected */
3092 &core_connect, /* Handle connects */
3093 &core_disconnect, /* remove peers on disconnects */
3094 NULL, /* Do we care about "status" updates? */
3095 NULL, /* Don't notify about all incoming messages */
3096 GNUNET_NO, /* For header only in notification */
3097 NULL, /* Don't notify about all outbound messages */
3098 GNUNET_NO, /* For header-only out notification */
3099 core_handlers); /* Register these handlers */
3100 if (core_handle == NULL)
3103 GNUNET_SCHEDULER_shutdown ();
3108 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3113 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3114 GNUNET_SCHEDULER_shutdown ();
3117 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3118 GNUNET_free (keyfile);
3119 if (my_private_key == NULL)
3121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3122 _("Mesh service could not access hostkey. Exiting.\n"));
3123 GNUNET_SCHEDULER_shutdown ();
3126 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3127 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3128 &my_full_id.hashPubKey);
3129 myid = GNUNET_PEER_intern (&my_full_id);
3131 dht_handle = GNUNET_DHT_connect (c, 64);
3132 if (dht_handle == NULL)
3134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3135 Running without DHT has a severe\
3136 impact in MESH capabilities.\n\
3137 Plase check your configuretion and enable DHT.\n");
3143 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3144 peers = GNUNET_CONTAINER_multihashmap_create (32);
3145 applications = GNUNET_CONTAINER_multihashmap_create (32);
3146 types = GNUNET_CONTAINER_multihashmap_create (32);
3148 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3149 nc = GNUNET_SERVER_notification_context_create (server_handle,
3151 GNUNET_SERVER_disconnect_notify (server_handle,
3152 &handle_local_client_disconnect,
3157 clients_tail = NULL;
3162 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3163 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3165 /* Create a peer_info for the local peer */
3166 peer = peer_info_get(&my_full_id);
3169 path_add_to_peer(peer, p);
3171 /* Scheduled the task to clean up when shutdown is called */
3172 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3179 * The main function for the mesh service.
3181 * @param argc number of arguments from the command line
3182 * @param argv command line arguments
3183 * @return 0 ok, 1 on error
3186 main (int argc, char *const *argv)
3191 fprintf (stderr, "main ()\n");
3193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3196 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3200 fprintf (stderr, "main () END\n");