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);
1311 * Function called to notify a client about the socket
1312 * being ready to queue more data. "buf" will be
1313 * NULL and "size" zero if the socket was closed for
1314 * writing in the meantime.
1316 * @param cls closure (data itself)
1317 * @param size number of bytes available in buf
1318 * @param buf where the callee should write the message
1319 * @return number of bytes written to buf
1322 send_core_data_unicast (void *cls, size_t size, void *buf)
1324 struct MeshDataDescriptor *info = cls;
1325 struct GNUNET_MESH_Unicast *msg = buf;
1328 GNUNET_assert (NULL != info);
1329 total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
1330 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1332 if (total_size > size)
1334 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1335 "not enough buffer to send data to peer\n");
1338 msg->header.size = htons (total_size);
1339 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1340 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1341 GNUNET_PEER_resolve (info->destination, &msg->destination);
1342 msg->tid = htonl (info->origin->tid);
1343 if (0 != info->size)
1345 memcpy (&msg[1], &info[1], info->size);
1347 if (NULL != info->client)
1349 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1357 * Function called to notify a client about the socket
1358 * being ready to queue more data. "buf" will be
1359 * NULL and "size" zero if the socket was closed for
1360 * writing in the meantime.
1362 * @param cls closure (data itself)
1363 * @param size number of bytes available in buf
1364 * @param buf where the callee should write the message
1366 * @return number of bytes written to buf
1371 send_core_data_multicast (void *cls, size_t size, void *buf)
1373 struct MeshDataDescriptor *info = cls;
1374 struct GNUNET_MESH_Multicast *msg = buf;
1377 GNUNET_assert (NULL != info);
1378 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1379 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1383 info->peer->core_transmit[info->handler_n] = NULL;
1385 if (total_size > size)
1387 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1388 "not enough buffer to send data futher\n");
1391 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1392 msg->header.size = htons (total_size);
1393 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1394 msg->tid = htonl (info->origin->tid);
1395 memcpy (&msg[1], &info[1], total_size);
1396 if (0 == --info->copies)
1398 if (NULL != info->client)
1400 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1409 * Function called to notify a client about the socket
1410 * being ready to queue more data. "buf" will be
1411 * NULL and "size" zero if the socket was closed for
1412 * writing in the meantime.
1414 * @param cls closure (MeshDataDescriptor)
1415 * @param size number of bytes available in buf
1416 * @param buf where the callee should write the message
1417 * @return number of bytes written to buf
1420 send_core_path_ack (void *cls, size_t size, void *buf)
1422 struct MeshDataDescriptor *info = cls;
1423 struct GNUNET_MESH_PathACK *msg = buf;
1425 GNUNET_assert (NULL != info);
1428 info->peer->core_transmit[info->handler_n] = NULL;
1430 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1435 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1436 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1437 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1438 msg->tid = htonl (info->origin->tid);
1439 msg->peer_id = my_full_id;
1441 /* TODO add signature */
1443 return sizeof (struct GNUNET_MESH_PathACK);
1448 * Function called to notify a client about the socket
1449 * being ready to queue more data. "buf" will be
1450 * NULL and "size" zero if the socket was closed for
1451 * writing in the meantime.
1453 * @param cls closure (data itself)
1454 * @param size number of bytes available in buf
1455 * @param buf where the callee should write the message
1456 * @return number of bytes written to buf
1459 send_core_data_raw (void *cls, size_t size, void *buf)
1461 struct GNUNET_MessageHeader *msg = cls;
1464 GNUNET_assert (NULL != msg);
1465 total_size = ntohs (msg->size);
1467 if (total_size > size)
1472 memcpy (buf, msg, total_size);
1480 * Send another peer a notification to destroy a tunnel
1481 * @param cls The tunnel to destroy
1482 * @param size Size in the buffer
1483 * @param buf Memory where to put the data to transmit
1484 * @return Size of data put in buffer
1487 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1489 struct MeshTunnel *t = cls;
1490 struct MeshClient *c;
1491 struct GNUNET_MESH_TunnelMessage *msg;
1495 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1496 /*FIXME*/ msg->header.size =
1497 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1498 msg->tunnel_id = htonl (t->id.tid);
1500 tunnel_destroy (c, t);
1501 return sizeof (struct GNUNET_MESH_TunnelMessage);
1507 * Send the message to all clients that have subscribed to its type
1509 * @param msg Pointer to the message itself
1510 * @return number of clients this message was sent to
1513 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1515 struct MeshClient *c;
1519 type = ntohs (msg->type);
1520 for (count = 0, c = clients; c != NULL; c = c->next)
1522 if (client_is_subscribed (type, c))
1525 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1535 * Notify the client that owns the tunnel that a peer has connected to it
1537 * @param t Tunnel whose owner to notify
1538 * @param id Short id of the peer that has connected
1541 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1543 struct GNUNET_MESH_PeerControl pc;
1545 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1546 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1547 pc.tunnel_id = htonl (t->local_tid);
1548 GNUNET_PEER_resolve (id, &pc.peer);
1549 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1550 &pc.header, GNUNET_NO);
1554 /******************************************************************************/
1555 /******************** MESH NETWORK HANDLERS **************************/
1556 /******************************************************************************/
1560 * Core handler for path creation
1561 * struct GNUNET_CORE_MessageHandler
1563 * @param cls closure
1564 * @param message message
1565 * @param peer peer identity this notification is about
1566 * @param atsi performance data
1567 * @return GNUNET_OK to keep the connection open,
1568 * GNUNET_SYSERR to close it (signal serious error)
1572 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1573 const struct GNUNET_MessageHeader *message,
1574 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1576 unsigned int own_pos;
1579 MESH_TunnelNumber tid;
1580 struct GNUNET_MESH_ManipulatePath *msg;
1581 struct GNUNET_PeerIdentity *pi;
1582 struct GNUNET_PeerIdentity id;
1583 GNUNET_HashCode hash;
1584 struct MeshPeerPath *path;
1585 struct MeshPeerInfo *dest_peer_info;
1586 struct MeshPeerInfo *orig_peer_info;
1587 struct MeshTunnel *t;
1589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1590 "MESH: Received a MESH path create msg\n");
1591 size = ntohs (message->size);
1592 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1594 GNUNET_break_op (0);
1598 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1599 if (size % sizeof (struct GNUNET_PeerIdentity))
1601 GNUNET_break_op (0);
1604 size /= sizeof (struct GNUNET_PeerIdentity);
1607 GNUNET_break_op (0);
1610 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1612 tid = ntohl (msg->tid);
1613 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1614 t = tunnel_get (pi, tid);
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1619 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1620 t->id.oid = GNUNET_PEER_intern (pi);
1622 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1624 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1626 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1627 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1635 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1636 if (NULL == dest_peer_info)
1638 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1639 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1640 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1642 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1644 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1645 if (NULL == orig_peer_info)
1647 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1648 orig_peer_info->id = GNUNET_PEER_intern (pi);
1649 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1650 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1653 path = path_new (size);
1655 for (i = 0; i < size; i++)
1657 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1658 if (path->peers[i] == myid)
1662 { /* cannot be self, must be 'not found' */
1663 /* create path: self not found in path through self */
1664 GNUNET_break_op (0);
1665 path_destroy (path);
1666 /* FIXME error. destroy tunnel? leave for timeout? */
1669 if (own_pos == size - 1)
1671 /* It is for us! Send ack. */
1672 struct MeshDataDescriptor *info;
1675 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1676 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1677 info->origin = &t->id;
1678 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1679 GNUNET_assert (info->peer);
1680 for (j = 0; info->peer->core_transmit[j]; j++)
1688 info->handler_n = j;
1689 info->peer->core_transmit[j] =
1690 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1691 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1692 sizeof (struct GNUNET_MessageHeader),
1693 &send_core_path_ack, info);
1697 /* It's for somebody else! Retransmit. */
1698 struct MeshPathInfo *path_info;
1700 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1702 path_info->path = path;
1703 path_info->peer = dest_peer_info;
1705 path_add_to_peer (dest_peer_info, path);
1706 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1707 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1708 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1709 sizeof (struct GNUNET_MessageHeader),
1710 &send_core_create_path, path_info);
1717 * Core handler for mesh network traffic going from the origin to a peer
1719 * @param cls closure
1720 * @param peer peer identity this notification is about
1721 * @param message message
1722 * @param atsi performance data
1723 * @return GNUNET_OK to keep the connection open,
1724 * GNUNET_SYSERR to close it (signal serious error)
1727 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1728 const struct GNUNET_MessageHeader *message,
1729 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1731 struct GNUNET_MESH_Unicast *msg;
1732 struct MeshTunnel *t;
1733 struct MeshPeerInfo *pi;
1736 size = ntohs (message->size);
1738 sizeof (struct GNUNET_MESH_Unicast) +
1739 sizeof (struct GNUNET_MessageHeader))
1744 msg = (struct GNUNET_MESH_Unicast *) message;
1745 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1748 /* TODO notify back: we don't know this tunnel */
1751 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1752 &msg->destination.hashPubKey);
1755 /* TODO maybe feedback, log to statistics */
1760 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1763 msg = GNUNET_malloc (size);
1764 memcpy (msg, message, size);
1765 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1766 GNUNET_TIME_UNIT_FOREVER_REL,
1767 path_get_first_hop (t->tree, pi->id),
1769 &send_core_data_raw, msg);
1775 * Core handler for mesh network traffic going from the origin to all peers
1777 * @param cls closure
1778 * @param message message
1779 * @param peer peer identity this notification is about
1780 * @param atsi performance data
1781 * @return GNUNET_OK to keep the connection open,
1782 * GNUNET_SYSERR to close it (signal serious error)
1784 * TODO: Check who we got this from, to validate route.
1787 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1788 const struct GNUNET_MessageHeader *message,
1789 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1791 struct GNUNET_MESH_Multicast *msg;
1792 struct GNUNET_PeerIdentity id;
1793 struct MeshDataDescriptor *info;
1794 struct MeshTunnelTreeNode *n;
1795 struct MeshTunnel *t;
1796 unsigned int copies;
1800 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
1801 if (size < sizeof (struct GNUNET_MessageHeader))
1803 GNUNET_break_op (0);
1806 msg = (struct GNUNET_MESH_Multicast *) message;
1807 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1811 /* TODO notify that we dont know that tunnel */
1812 GNUNET_break_op (0);
1816 /* Transmit to locally interested clients */
1817 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1819 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1821 n = t->tree->me->children_head;
1824 for (copies = 0; NULL != n; n = n->next)
1826 n = t->tree->me->children_head;
1827 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1828 info->data = GNUNET_malloc (size);
1829 memcpy(info->data, &msg[1], size);
1830 info->origin = &t->id;
1831 info->copies = copies;
1833 info->client = t->client->handle;
1836 GNUNET_PEER_resolve (n->peer, &id);
1837 info->peer = peer_info_get(&id);
1838 GNUNET_assert (NULL != info->peer);
1839 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
1841 if (i == (CORE_QUEUE_SIZE - 1))
1847 info->handler_n = i;
1848 info->peer->infos[i] = info;
1849 info->peer->core_transmit[i] =
1850 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1851 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1852 ntohs (msg->header.size),
1853 &send_core_data_multicast, info);
1861 * Core handler for mesh network traffic
1863 * @param cls closure
1864 * @param message message
1865 * @param peer peer identity this notification is about
1866 * @param atsi performance data
1868 * @return GNUNET_OK to keep the connection open,
1869 * GNUNET_SYSERR to close it (signal serious error)
1872 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1873 const struct GNUNET_MessageHeader *message,
1874 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1876 struct GNUNET_MESH_ToOrigin *msg;
1877 struct GNUNET_PeerIdentity id;
1878 struct MeshPeerInfo *peer_info;
1879 struct MeshTunnel *t;
1882 size = ntohs (message->size);
1883 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1884 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1886 GNUNET_break_op (0);
1889 msg = (struct GNUNET_MESH_ToOrigin *) message;
1890 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1894 /* TODO notify that we dont know this tunnel (whom)? */
1898 if (t->id.oid == myid)
1900 if (NULL == t->client)
1902 /* got data packet for ownerless tunnel */
1903 GNUNET_break_op (0);
1906 /* TODO signature verification */
1907 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1911 peer_info = peer_info_get (&msg->oid);
1912 if (NULL == peer_info)
1914 /* unknown origin of tunnel */
1918 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1919 msg = GNUNET_malloc (size);
1920 memcpy (msg, message, size);
1921 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1922 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1923 &send_core_data_raw, msg);
1930 * Core handler for path ACKs
1932 * @param cls closure
1933 * @param message message
1934 * @param peer peer identity this notification is about
1935 * @param atsi performance data
1937 * @return GNUNET_OK to keep the connection open,
1938 * GNUNET_SYSERR to close it (signal serious error)
1941 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1942 const struct GNUNET_MessageHeader *message,
1943 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1945 struct GNUNET_MESH_PathACK *msg;
1946 struct MeshTunnelTreeNode *n;
1947 struct MeshPeerInfo *peer_info;
1948 struct MeshTunnel *t;
1950 msg = (struct GNUNET_MESH_PathACK *) message;
1951 t = tunnel_get (&msg->oid, msg->tid);
1954 /* TODO notify that we don't know the tunnel */
1958 /* Message for us? */
1959 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1961 if (NULL == t->client)
1963 GNUNET_break_op (0);
1966 peer_info = peer_info_get (&msg->peer_id);
1967 if (NULL == peer_info)
1969 GNUNET_break_op (0);
1972 n = tree_find_peer(t->tree->root, peer_info->id);
1975 GNUNET_break_op (0);
1978 n->status = MESH_PEER_READY;
1979 send_client_peer_connected(t, peer_info->id);
1983 peer_info = peer_info_get (&msg->oid);
1984 if (NULL == peer_info)
1986 /* If we know the tunnel, we should DEFINITELY know the peer */
1990 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1991 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1992 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1993 GNUNET_TIME_UNIT_FOREVER_REL,
1994 path_get_first_hop (t->tree,
1996 sizeof (struct GNUNET_MESH_PathACK),
1997 &send_core_data_raw, msg);
2003 * Functions to handle messages from core
2005 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2006 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2007 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2008 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2009 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2010 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2011 sizeof (struct GNUNET_MESH_PathACK)},
2017 /******************************************************************************/
2018 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2019 /******************************************************************************/
2022 * deregister_app: iterator for removing each application registered by a client
2024 * @param cls closure
2025 * @param key the hash of the application id (used to access the hashmap)
2026 * @param value the value stored at the key (client)
2028 * @return GNUNET_OK on success
2031 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2033 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2039 * notify_client_connection_failure: notify a client that the connection to the
2040 * requested remote peer is not possible (for instance, no route found)
2041 * Function called when the socket is ready to queue more data. "buf" will be
2042 * NULL and "size" zero if the socket was closed for writing in the meantime.
2044 * @param cls closure
2045 * @param size number of bytes available in buf
2046 * @param buf where the callee should write the message
2047 * @return number of bytes written to buf
2050 notify_client_connection_failure (void *cls, size_t size, void *buf)
2053 struct MeshPeerInfo *peer_info;
2054 struct GNUNET_MESH_PeerControl *msg;
2055 struct GNUNET_PeerIdentity id;
2057 if (0 == size && NULL == buf)
2059 // TODO retry? cancel?
2063 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2064 peer_info = (struct MeshPeerInfo *) cls;
2065 msg = (struct GNUNET_MESH_PeerControl *) buf;
2066 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2067 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2068 // msg->tunnel_id = htonl(peer_info->t->tid);
2069 GNUNET_PEER_resolve (peer_info->id, &id);
2070 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2078 * Function to process paths received for a new peer addition. The recorded
2079 * paths form the initial tunnel, which can be optimized later.
2080 * Called on each result obtained for the DHT search.
2082 * @param cls closure
2083 * @param exp when will this value expire
2084 * @param key key of the result
2085 * @param get_path NULL-terminated array of pointers
2086 * to the peers on reverse GET path (or NULL if not recorded)
2087 * @param put_path NULL-terminated array of pointers
2088 * to the peers on the PUT path (or NULL if not recorded)
2089 * @param type type of the result
2090 * @param size number of bytes in data
2091 * @param data pointer to the result data
2093 * TODO: re-issue the request after certain time? cancel after X results?
2096 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2097 const GNUNET_HashCode * key,
2098 const struct GNUNET_PeerIdentity *const *get_path,
2099 const struct GNUNET_PeerIdentity *const *put_path,
2100 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2102 struct MeshPathInfo *path_info = cls;
2103 struct MeshPeerPath *p;
2104 struct GNUNET_PeerIdentity pi;
2107 if (NULL == get_path || NULL == put_path)
2109 if (NULL == path_info->peer->path_head)
2111 // Find ourselves some alternate initial path to the destination: retry
2112 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2113 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2114 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2115 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2116 GNUNET_BLOCK_TYPE_TEST, /* type */
2117 &pi.hashPubKey, /*key to search */
2118 4, /* replication level */
2119 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2122 0, /* xquery bits */
2124 (void *) path_info);
2129 p = path_build_from_dht (get_path, put_path);
2130 path_add_to_peer (path_info->peer, p);
2131 for (i = 0; i < path_info->peer->ntunnels; i++)
2133 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2135 GNUNET_free (path_info);
2142 * Function to process paths received for a new peer addition. The recorded
2143 * paths form the initial tunnel, which can be optimized later.
2144 * Called on each result obtained for the DHT search.
2146 * @param cls closure
2147 * @param exp when will this value expire
2148 * @param key key of the result
2149 * @param get_path NULL-terminated array of pointers
2150 * to the peers on reverse GET path (or NULL if not recorded)
2151 * @param put_path NULL-terminated array of pointers
2152 * to the peers on the PUT path (or NULL if not recorded)
2153 * @param type type of the result
2154 * @param size number of bytes in data
2155 * @param data pointer to the result data
2158 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2159 const GNUNET_HashCode * key,
2160 const struct GNUNET_PeerIdentity *const *get_path,
2161 const struct GNUNET_PeerIdentity *const *put_path,
2162 enum GNUNET_BLOCK_Type type, size_t size,
2165 const struct GNUNET_PeerIdentity *pi = data;
2166 struct GNUNET_PeerIdentity id;
2167 struct MeshTunnel *t = cls;
2168 struct MeshPeerInfo *peer_info;
2169 struct MeshPathInfo *path_info;
2170 struct MeshPeerPath *p;
2173 if (size != sizeof (struct GNUNET_PeerIdentity))
2175 GNUNET_break_op (0);
2178 GNUNET_assert (NULL != t->client);
2179 GNUNET_DHT_get_stop (t->client->dht_get_type);
2180 t->client->dht_get_type = NULL;
2181 peer_info = peer_info_get (pi);
2182 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2183 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2185 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2186 NULL == peer_info->dhtget)
2188 /* we don't have a route to the peer, let's try a direct lookup */
2189 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2191 GNUNET_TIME_UNIT_FOREVER_REL,
2193 GNUNET_BLOCK_TYPE_TEST,
2196 /* key to look up */
2198 /* replication level */
2199 GNUNET_DHT_RO_RECORD_ROUTE,
2200 /* option to dht: record route */
2201 NULL, /* bloom filter */
2204 0, /* xquery bits */
2207 peer_info); /* closure */
2210 p = path_build_from_dht (get_path, put_path);
2211 path_add_to_peer (peer_info, p);
2212 tunnel_add_peer(t, peer_info);
2213 p = tree_get_path_to_peer(t->tree, peer_info->id);
2215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2216 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2217 t->local_tid, p->length);
2218 for (i = 0; i < p->length; i++)
2220 GNUNET_PEER_resolve (p->peers[0], &id);
2221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2222 GNUNET_h2s_full (&id.hashPubKey));
2228 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2230 path_info->peer = peer_info;
2231 path_info->path = p;
2232 GNUNET_PEER_resolve (p->peers[1], &id);
2233 GNUNET_CORE_notify_transmit_ready (core_handle,
2239 GNUNET_TIME_UNIT_FOREVER_REL,
2243 sizeof (struct GNUNET_MESH_ManipulatePath)
2246 sizeof (struct GNUNET_PeerIdentity)),
2248 &send_core_create_path,
2250 path_info); /* cls */
2254 send_client_peer_connected(t, myid);
2257 /******************************************************************************/
2258 /********************* MESH LOCAL HANDLES **************************/
2259 /******************************************************************************/
2263 * Handler for client disconnection
2265 * @param cls closure
2266 * @param client identification of the client; NULL
2267 * for the last call when the server is destroyed
2270 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2272 struct MeshClient *c;
2273 struct MeshClient *next;
2275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2281 if (c->handle != client && NULL != client)
2283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2288 if (NULL != c->tunnels)
2290 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2291 &tunnel_destroy_iterator,
2293 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2296 /* deregister clients applications */
2297 if (NULL != c->apps)
2299 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2300 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2302 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2303 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2305 GNUNET_SCHEDULER_cancel (announce_applications_task);
2306 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2308 if (NULL != c->types)
2309 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2310 if (NULL != c->dht_get_type)
2311 GNUNET_DHT_get_stop (c->dht_get_type);
2312 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2324 * Handler for new clients
2326 * @param cls closure
2327 * @param client identification of the client
2328 * @param message the actual message, which includes messages the client wants
2331 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2332 const struct GNUNET_MessageHeader *message)
2334 struct GNUNET_MESH_ClientConnect *cc_msg;
2335 struct MeshClient *c;
2336 GNUNET_MESH_ApplicationType *a;
2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2344 /* Check data sanity */
2345 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2346 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2347 ntypes = ntohs (cc_msg->types);
2348 napps = ntohs (cc_msg->applications);
2350 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2353 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2357 /* Create new client structure */
2358 c = GNUNET_malloc (sizeof (struct MeshClient));
2360 c->id = next_client_id++;
2363 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2366 GNUNET_MESH_ApplicationType at;
2369 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2370 for (i = 0; i < napps; i++)
2373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2374 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2375 /* store in clients hashmap */
2376 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2377 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2378 /* store in global hashmap, for announcements */
2379 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2380 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2382 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2383 announce_applications_task =
2384 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2392 t = (uint16_t *) & a[napps];
2393 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2394 for (i = 0; i < ntypes; i++)
2397 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2399 /* store in clients hashmap */
2400 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2401 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2402 /* store in global hashmap */
2403 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2404 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2408 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2410 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2411 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2412 GNUNET_SERVER_notification_context_add (nc, client);
2414 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2422 * Handler for requests of new tunnels
2424 * @param cls closure
2425 * @param client identification of the client
2426 * @param message the actual message
2429 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2430 const struct GNUNET_MessageHeader *message)
2432 struct GNUNET_MESH_TunnelMessage *t_msg;
2433 struct MeshTunnel *t;
2434 struct MeshClient *c;
2435 GNUNET_HashCode hash;
2437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2439 /* Sanity check for client registration */
2440 if (NULL == (c = client_get (client)))
2443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2450 /* Message sanity check */
2451 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2454 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2458 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2459 /* Sanity check for tunnel numbering */
2460 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2463 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2466 /* Sanity check for duplicate tunnel IDs */
2467 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2470 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2474 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2476 while (NULL != tunnel_get_by_pi (myid, next_tid))
2477 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2478 t->id.tid = next_tid++;
2480 t->local_tid = ntohl (t_msg->tunnel_id);
2482 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2484 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2486 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2487 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2490 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2494 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2496 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2497 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2500 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2503 t->tree = tree_new (t, myid);
2504 t->tree->refresh = REFRESH_PATH_TIME;
2505 t->tree->root->status = MESH_PEER_READY;
2506 t->tree->me = t->tree->root;
2508 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2514 * Handler for requests of deleting tunnels
2516 * @param cls closure
2517 * @param client identification of the client
2518 * @param message the actual message
2521 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2522 const struct GNUNET_MessageHeader *message)
2524 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2525 struct MeshClient *c;
2526 struct MeshTunnel *t;
2527 MESH_TunnelNumber tid;
2528 GNUNET_HashCode hash;
2530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2532 /* Sanity check for client registration */
2533 if (NULL == (c = client_get (client)))
2536 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2539 /* Message sanity check */
2540 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2543 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2549 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2551 /* Retrieve tunnel */
2552 tid = ntohl (tunnel_msg->tunnel_id);
2554 /* Remove from local id hashmap */
2555 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2556 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2557 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2559 /* Remove from global id hashmap */
2560 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2561 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2563 // notify_tunnel_destroy(t); FIXME
2565 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2571 * Handler for connection requests to new peers
2573 * @param cls closure
2574 * @param client identification of the client
2575 * @param message the actual message (PeerControl)
2578 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2579 const struct GNUNET_MessageHeader *message)
2581 struct GNUNET_MESH_PeerControl *peer_msg;
2582 struct MeshClient *c;
2583 struct MeshTunnel *t;
2584 MESH_TunnelNumber tid;
2585 struct MeshPeerInfo *peer_info;
2588 /* Sanity check for client registration */
2589 if (NULL == (c = client_get (client)))
2592 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2596 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2597 /* Sanity check for message size */
2598 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2601 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2605 /* Tunnel exists? */
2606 tid = ntohl (peer_msg->tunnel_id);
2607 t = tunnel_get_by_local_id (c, tid);
2611 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2615 /* Does client own tunnel? */
2616 if (t->client->handle != client)
2619 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2623 peer_info = peer_info_get (&peer_msg->peer);
2625 /* Start DHT search if needed, otherwise just add peer to tunnel. */
2626 if (NULL == peer_info->dhtget && NULL == peer_info->path_head)
2628 struct MeshPathInfo *path_info;
2630 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2631 path_info->peer = peer_info;
2633 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
2634 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2635 GNUNET_BLOCK_TYPE_TEST, /* type */
2636 &peer_msg->peer.hashPubKey, /*key to search */
2637 4, /* replication level */
2638 GNUNET_DHT_RO_RECORD_ROUTE,
2639 NULL, /* bloom filter */
2642 0, /* xquery bits */
2644 (void *) path_info);
2646 if (NULL != peer_info->path_head)
2648 tunnel_add_peer(t, peer_info);
2650 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2656 * Handler for disconnection requests of peers in a tunnel
2658 * @param cls closure
2659 * @param client identification of the client
2660 * @param message the actual message (PeerControl)
2663 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2664 const struct GNUNET_MessageHeader *message)
2666 struct GNUNET_MESH_PeerControl *peer_msg;
2667 struct MeshClient *c;
2668 struct MeshTunnel *t;
2669 MESH_TunnelNumber tid;
2671 /* Sanity check for client registration */
2672 if (NULL == (c = client_get (client)))
2675 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2678 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2679 /* Sanity check for message size */
2680 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2683 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2687 /* Tunnel exists? */
2688 tid = ntohl (peer_msg->tunnel_id);
2689 t = tunnel_get_by_local_id (c, tid);
2693 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2697 /* Does client own tunnel? */
2698 if (t->client->handle != client)
2701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2705 /* Ok, delete peer from tunnel */
2706 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2707 &peer_msg->peer.hashPubKey);
2709 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2715 * Handler for connection requests to new peers by type
2717 * @param cls closure
2718 * @param client identification of the client
2719 * @param message the actual message (ConnectPeerByType)
2722 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2723 const struct GNUNET_MessageHeader *message)
2725 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2726 struct MeshClient *c;
2727 struct MeshTunnel *t;
2728 GNUNET_HashCode hash;
2729 GNUNET_MESH_ApplicationType type;
2730 MESH_TunnelNumber tid;
2732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2733 /* Sanity check for client registration */
2734 if (NULL == (c = client_get (client)))
2737 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2741 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2742 /* Sanity check for message size */
2743 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2744 ntohs (connect_msg->header.size))
2747 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2751 /* Tunnel exists? */
2752 tid = ntohl (connect_msg->tunnel_id);
2753 t = tunnel_get_by_local_id (c, tid);
2757 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2761 /* Does client own tunnel? */
2762 if (t->client->handle != client)
2765 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2769 /* Do WE have the service? */
2770 type = ntohl (connect_msg->type);
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2772 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2773 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2776 /* Yes! Fast forward, add ourselves to the tunnel and send the
2777 * good news to the client
2779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2780 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2781 peer_info_get (&my_full_id),
2782 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2785 send_client_peer_connected(t, myid);
2786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2787 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2790 /* Ok, lets find a peer offering the service */
2791 if (c->dht_get_type)
2793 GNUNET_DHT_get_stop (c->dht_get_type);
2795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2796 GNUNET_h2s_full (&hash));
2798 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2799 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2800 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
2801 &dht_get_type_handler, t);
2803 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2809 * Handler for client traffic directed to one peer
2811 * @param cls closure
2812 * @param client identification of the client
2813 * @param message the actual message
2816 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2817 const struct GNUNET_MessageHeader *message)
2819 struct MeshClient *c;
2820 struct MeshTunnel *t;
2821 struct MeshPeerInfo *pi;
2822 struct GNUNET_MESH_Unicast *data_msg;
2823 struct MeshDataDescriptor *info;
2824 MESH_TunnelNumber tid;
2827 /* Sanity check for client registration */
2828 if (NULL == (c = client_get (client)))
2831 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2834 data_msg = (struct GNUNET_MESH_Unicast *) message;
2835 /* Sanity check for message size */
2836 if (sizeof (struct GNUNET_MESH_Unicast) +
2837 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
2840 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2844 /* Tunnel exists? */
2845 tid = ntohl (data_msg->tid);
2846 t = tunnel_get_by_local_id (c, tid);
2850 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2854 /* Is it a local tunnel? Then, does client own the tunnel? */
2855 if (t->client->handle != NULL && t->client->handle != client)
2858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2862 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2863 &data_msg->destination.hashPubKey);
2864 /* Is the selected peer in the tunnel? */
2868 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2873 struct GNUNET_MESH_Unicast copy;
2875 /* Work around const limitation */
2876 memcpy (©, data_msg, sizeof (struct GNUNET_MESH_Unicast));
2877 copy.oid = my_full_id;
2878 copy.tid = htonl (t->id.tid);
2879 handle_mesh_data_unicast (NULL, &my_full_id, ©.header, NULL);
2882 data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
2883 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
2884 memcpy (&info[1], &data_msg[1], data_size);
2885 info->destination = pi->id;
2886 info->origin = &t->id;
2887 info->size = data_size;
2888 info->client = client;
2889 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2890 GNUNET_TIME_UNIT_FOREVER_REL,
2891 path_get_first_hop (t->tree, pi->id),
2892 /* FIXME re-check types */
2894 sizeof (struct GNUNET_MESH_Unicast),
2895 &send_core_data_unicast, info);
2900 * Handler for client traffic directed to all peers in a tunnel
2902 * @param cls closure
2903 * @param client identification of the client
2904 * @param message the actual message
2907 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2908 const struct GNUNET_MessageHeader *message)
2910 struct MeshClient *c;
2911 struct MeshTunnel *t;
2912 struct GNUNET_MESH_Multicast *data_msg;
2913 MESH_TunnelNumber tid;
2915 /* Sanity check for client registration */
2916 if (NULL == (c = client_get (client)))
2919 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2922 data_msg = (struct GNUNET_MESH_Multicast *) message;
2923 /* Sanity check for message size */
2924 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2931 /* Tunnel exists? */
2932 tid = ntohl (data_msg->tid);
2933 t = tunnel_get_by_local_id (c, tid);
2937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2941 /* Does client own tunnel? */
2942 if (t->client->handle != client)
2945 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2951 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2956 * Functions to handle messages from clients
2958 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
2959 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2960 {&handle_local_tunnel_create, NULL,
2961 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2962 sizeof (struct GNUNET_MESH_TunnelMessage)},
2963 {&handle_local_tunnel_destroy, NULL,
2964 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2965 sizeof (struct GNUNET_MESH_TunnelMessage)},
2966 {&handle_local_connect_add, NULL,
2967 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
2968 sizeof (struct GNUNET_MESH_PeerControl)},
2969 {&handle_local_connect_del, NULL,
2970 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
2971 sizeof (struct GNUNET_MESH_PeerControl)},
2972 {&handle_local_connect_by_type, NULL,
2973 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
2974 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
2975 {&handle_local_unicast, NULL,
2976 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2977 {&handle_local_unicast, NULL,
2978 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2979 {&handle_local_multicast, NULL,
2980 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2986 * To be called on core init/fail.
2988 * @param cls service closure
2989 * @param server handle to the server for this service
2990 * @param identity the public identity of this peer
2991 * @param publicKey the public key of this peer
2994 core_init (void *cls, struct GNUNET_CORE_Handle *server,
2995 const struct GNUNET_PeerIdentity *identity,
2996 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
2999 core_handle = server;
3000 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3002 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3003 GNUNET_SCHEDULER_shutdown();
3009 * Method called whenever a given peer connects.
3011 * @param cls closure
3012 * @param peer peer identity this notification is about
3013 * @param atsi performance data for the connection
3016 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3017 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3019 // GNUNET_PEER_Id pid;
3020 struct MeshPeerInfo *peer_info;
3021 struct MeshPeerPath *path;
3023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3024 peer_info = peer_info_get (peer);
3025 if (myid == peer_info->id)
3027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3030 path = path_new (2);
3031 path->peers[0] = myid;
3032 path->peers[1] = peer_info->id;
3033 path_add_to_peer (peer_info, path);
3038 * Method called whenever a peer disconnects.
3040 * @param cls closure
3041 * @param peer peer identity this notification is about
3044 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3046 struct MeshPeerInfo *pi;
3049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3050 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3056 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3058 if (pi->core_transmit[i])
3060 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3061 /* TODO: notify that tranmission has failed */
3062 GNUNET_free (pi->infos[i]);
3065 path_remove_from_peer (pi, pi->id, myid);
3068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3074 /******************************************************************************/
3075 /************************ MAIN FUNCTIONS ****************************/
3076 /******************************************************************************/
3079 * Task run during shutdown.
3085 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3087 struct MeshClient *c;
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3090 if (core_handle != NULL)
3092 GNUNET_CORE_disconnect (core_handle);
3095 if (dht_handle != NULL)
3097 for (c = clients; NULL != c; c = c->next)
3098 if (NULL != c->dht_get_type)
3099 GNUNET_DHT_get_stop (c->dht_get_type);
3100 GNUNET_DHT_disconnect (dht_handle);
3105 GNUNET_SERVER_notification_context_destroy (nc);
3108 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3110 GNUNET_SCHEDULER_cancel (announce_id_task);
3111 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3117 * Process mesh requests.
3119 * @param cls closure
3120 * @param server the initialized server
3121 * @param c configuration to use
3124 run (void *cls, struct GNUNET_SERVER_Handle *server,
3125 const struct GNUNET_CONFIGURATION_Handle *c)
3127 struct MeshPeerInfo *peer;
3128 struct MeshPeerPath *p;
3131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3132 server_handle = server;
3133 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3134 CORE_QUEUE_SIZE, /* queue size */
3135 NULL, /* Closure passed to MESH functions */
3136 &core_init, /* Call core_init once connected */
3137 &core_connect, /* Handle connects */
3138 &core_disconnect, /* remove peers on disconnects */
3139 NULL, /* Do we care about "status" updates? */
3140 NULL, /* Don't notify about all incoming messages */
3141 GNUNET_NO, /* For header only in notification */
3142 NULL, /* Don't notify about all outbound messages */
3143 GNUNET_NO, /* For header-only out notification */
3144 core_handlers); /* Register these handlers */
3145 if (core_handle == NULL)
3148 GNUNET_SCHEDULER_shutdown ();
3153 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3158 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3159 GNUNET_SCHEDULER_shutdown ();
3162 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3163 GNUNET_free (keyfile);
3164 if (my_private_key == NULL)
3166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3167 _("Mesh service could not access hostkey. Exiting.\n"));
3168 GNUNET_SCHEDULER_shutdown ();
3171 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3172 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3173 &my_full_id.hashPubKey);
3174 myid = GNUNET_PEER_intern (&my_full_id);
3176 dht_handle = GNUNET_DHT_connect (c, 64);
3177 if (dht_handle == NULL)
3179 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3180 Running without DHT has a severe\
3181 impact in MESH capabilities.\n\
3182 Plase check your configuretion and enable DHT.\n");
3188 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3189 peers = GNUNET_CONTAINER_multihashmap_create (32);
3190 applications = GNUNET_CONTAINER_multihashmap_create (32);
3191 types = GNUNET_CONTAINER_multihashmap_create (32);
3193 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3194 nc = GNUNET_SERVER_notification_context_create (server_handle,
3196 GNUNET_SERVER_disconnect_notify (server_handle,
3197 &handle_local_client_disconnect,
3202 clients_tail = NULL;
3207 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3208 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3210 /* Create a peer_info for the local peer */
3211 peer = peer_info_get(&my_full_id);
3214 path_add_to_peer(peer, p);
3216 /* Scheduled the task to clean up when shutdown is called */
3217 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3224 * The main function for the mesh service.
3226 * @param argc number of arguments from the command line
3227 * @param argv command line arguments
3228 * @return 0 ok, 1 on error
3231 main (int argc, char *const *argv)
3236 fprintf (stderr, "main ()\n");
3238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3241 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3245 fprintf (stderr, "main () END\n");