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;
82 /** Ultimate destination of the packet */
83 GNUNET_PEER_Id destination;
85 /** Number of identical messages sent to different hops (multicast) */
88 /** Size of the data */
91 /** Client that asked for the transmission, if any */
92 struct GNUNET_SERVER_Client *client;
94 /** Who was is message being sent to */
95 struct MeshPeerInfo *peer;
97 /** Which handler was used to request the transmission */
98 unsigned int handler_n;
100 /* Data at the end */
105 * Struct containing all information regarding a given peer
115 * Last time we heard from this peer
117 struct GNUNET_TIME_Absolute last_contact;
120 * Number of attempts to reconnect so far
122 int n_reconnect_attempts;
125 * Paths to reach the peer, ordered by ascending hop count
127 struct MeshPeerPath *path_head;
130 * Paths to reach the peer, ordered by ascending hop count
132 struct MeshPeerPath *path_tail;
135 * Handle to stop the DHT search for a path to this peer
137 struct GNUNET_DHT_GetHandle *dhtget;
140 * Handles to stop queued transmissions for this peer
142 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
145 * Pointer to info stuctures used as cls for queued transmissions
147 struct MeshDataDescriptor *infos[CORE_QUEUE_SIZE];
150 * Array of tunnels this peer participates in
151 * (most probably a small amount, therefore not a hashmap)
152 * When the path to the peer changes, notify these tunnels to let them
153 * re-adjust their path trees.
155 struct MeshTunnel **tunnels;
158 * Number of tunnels above
160 unsigned int ntunnels;
165 * Data scheduled to transmit (to local client or remote peer)
172 struct MeshQueue *next;
173 struct MeshQueue *prev;
176 * Target of the data (NULL if target is client)
178 struct MeshPeerInfo *peer;
181 * Client to send the data to (NULL if target is peer)
183 struct MeshClient *client;
186 * Size of the message to transmit
191 * How old is the data?
193 struct GNUNET_TIME_Absolute timestamp;
198 struct GNUNET_MessageHeader *data;
202 * Globally unique tunnel identification (owner + number)
203 * DO NOT USE OVER THE NETWORK
208 * Node that owns the tunnel
213 * Tunnel number to differentiate all the tunnels owned by the node oid
214 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
216 MESH_TunnelNumber tid;
220 struct MeshClient; /* FWD declaration */
223 * Struct containing all information regarding a tunnel
224 * For an intermediate node the improtant info used will be:
225 * - id Tunnel unique identification
226 * - paths[0] To know where to send it next
227 * - metainfo: ready, speeds, accounting
234 struct MESH_TunnelID id;
237 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
239 MESH_TunnelNumber local_tid;
242 * Last time the tunnel was used
244 struct GNUNET_TIME_Absolute timestamp;
247 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
249 struct GNUNET_CONTAINER_MultiHashMap *peers;
252 * Number of peers that are connected and potentially ready to receive data
254 unsigned int peers_ready;
257 * Number of peers that have been added to the tunnel
259 unsigned int peers_total;
262 * Client owner of the tunnel, if any
264 struct MeshClient *client;
267 * Messages ready to transmit
269 struct MeshQueue *queue_head;
270 struct MeshQueue *queue_tail;
275 struct MeshTunnelTree *tree;
278 * Task to keep the used paths alive
280 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
285 * Info needed to work with tunnel paths and peers
292 struct MeshTunnel *t;
297 struct MeshPeerInfo *peer;
302 struct MeshPeerPath *path;
307 * Struct containing information about a client of the service
314 struct MeshClient *next;
315 struct MeshClient *prev;
318 * Tunnels that belong to this client, indexed by local id
320 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
323 * Handle to communicate with the client
325 struct GNUNET_SERVER_Client *handle;
328 * Applications that this client has claimed to provide
330 struct GNUNET_CONTAINER_MultiHashMap *apps;
333 * Messages that this client has declared interest in
335 struct GNUNET_CONTAINER_MultiHashMap *types;
338 * Used to search peers offering a service
340 struct GNUNET_DHT_GetHandle *dht_get_type;
344 * ID of the client, for debug messages
353 /******************************************************************************/
354 /************************ DEBUG FUNCTIONS ****************************/
355 /******************************************************************************/
359 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
360 * @param cls string to print
361 * @param tc task context
364 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
368 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
377 /******************************************************************************/
378 /*********************** GLOBAL VARIABLES ****************************/
379 /******************************************************************************/
384 static struct MeshClient *clients;
385 static struct MeshClient *clients_tail;
388 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
390 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
393 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
395 static struct GNUNET_CONTAINER_MultiHashMap *peers;
398 * Handle to communicate with core
400 static struct GNUNET_CORE_Handle *core_handle;
405 static struct GNUNET_DHT_Handle *dht_handle;
410 static struct GNUNET_SERVER_Handle *server_handle;
413 * Notification context, to send messages to local clients
415 static struct GNUNET_SERVER_NotificationContext *nc;
418 * Local peer own ID (memory efficient handle)
420 static GNUNET_PEER_Id myid;
423 * Local peer own ID (full value)
425 static struct GNUNET_PeerIdentity my_full_id;
430 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
435 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
438 * Tunnel ID for the next created tunnel (global tunnel number)
440 static MESH_TunnelNumber next_tid;
443 * All application types provided by this peer
445 static struct GNUNET_CONTAINER_MultiHashMap *applications;
448 * All message types clients of this peer are interested in
450 static struct GNUNET_CONTAINER_MultiHashMap *types;
453 * Task to periodically announce provided applications
455 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
458 * Task to periodically announce itself in the network
460 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
463 unsigned int next_client_id;
467 /******************************************************************************/
468 /************************ ITERATORS ****************************/
469 /******************************************************************************/
473 * Iterator over hash map peer entries collect all neighbors who to resend the
476 * @param cls closure (**GNUNET_PEER_Id to store hops to send packet)
477 * @param key current key code (peer id hash)
478 * @param value value in the hash map (peer_info)
479 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
482 iterate_collect_neighbors (void *cls, const GNUNET_HashCode * key, void *value)
484 struct MeshPeerInfo *peer_info = value;
485 struct MeshPathInfo *neighbors = cls;
486 struct GNUNET_PeerIdentity *id;
487 GNUNET_PEER_Id peer_id;
490 if (peer_info->id == myid)
494 id = path_get_first_hop (neighbors->t->tree, peer_info->id);
495 peer_id = GNUNET_PEER_search(id);
496 for (i = 0; i < neighbors->path->length; i++)
498 if (neighbors->path->peers[i] == peer_id)
501 GNUNET_array_append (neighbors->path->peers, neighbors->path->length,
509 * Iterator over hash map peer entries and frees all data in it.
510 * Used prior to destroying a hashmap. Makes you miss anonymous functions in C.
513 * @param key current key code (will no longer contain valid data!!)
514 * @param value value in the hash map (treated as void *)
515 * @return GNUNET_YES if we should continue to iterate, GNUNET_NO if not.
518 iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
525 /******************************************************************************/
526 /************************ PERIODIC FUNCTIONS ****************************/
527 /******************************************************************************/
530 * Announce iterator over for each application provided by the peer
533 * @param key current key code
534 * @param value value in the hash map
535 * @return GNUNET_YES if we should continue to
540 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
542 /* FIXME are hashes in multihash map equal on all aquitectures? */
543 GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
544 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
545 (const char *) &my_full_id,
547 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
548 &mesh_debug, "DHT_put for app completed");
550 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
552 APP_ANNOUNCE_TIME, NULL, NULL);
559 * Periodically announce what applications are provided by local clients
562 * @param tc task context
565 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
567 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
569 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
573 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
575 announce_applications_task =
576 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
584 * Periodically announce self id in the DHT
587 * @param tc task context
590 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
592 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
594 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
598 * - Set data expiration in function of X
601 GNUNET_DHT_put (dht_handle, /* DHT handle */
602 &my_full_id.hashPubKey, /* Key to use */
603 10U, /* Replication level */
604 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
605 GNUNET_BLOCK_TYPE_TEST, /* Block type */
606 0, /* Size of the data */
607 NULL, /* Data itself */
608 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
609 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
611 &mesh_debug, "DHT_put for id completed");
613 NULL, /* Continuation */
614 NULL); /* Continuation closure */
617 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
621 /******************************************************************************/
622 /****************** GENERAL HELPER FUNCTIONS ************************/
623 /******************************************************************************/
626 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
627 * and insert it in the appropiate structures if the peer is not known yet.
629 * @param peer Identity of the peer
631 * @return Existing or newly created peer info
633 static struct MeshPeerInfo *
634 peer_info_get (const struct GNUNET_PeerIdentity *peer)
636 struct MeshPeerInfo *peer_info;
638 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
639 if (NULL == peer_info)
642 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
643 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
644 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
645 peer_info->id = GNUNET_PEER_intern (peer);
654 * Destroy the peer_info and free any allocated resources linked to it
655 * @param t tunnel the path belongs to
656 * @param pi the peer_info to destroy
657 * @return GNUNET_OK on success
660 peer_info_destroy (struct MeshPeerInfo *pi)
662 GNUNET_HashCode hash;
663 struct GNUNET_PeerIdentity id;
665 GNUNET_PEER_resolve (pi->id, &id);
666 GNUNET_PEER_change_rc (pi->id, -1);
667 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
669 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
670 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
678 * Notify a tunnel that a connection has broken that affects at least
681 * @param t Tunnel affected
682 * @param peer Peer that (at least) has been affected by the disconnection
683 * @param p1 Peer that got disconnected from p2
684 * @param p2 Peer that got disconnected from p1
687 tunnel_notify_connection_broken (struct MeshTunnel *t,
688 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
693 * Remove all paths that rely on a direct connection between p1 and p2
694 * from the peer itself and notify all tunnels about it.
696 * @param pi PeerInfo of affected peer
697 * @param p1 GNUNET_PEER_Id of one peer.
698 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
702 path_remove_from_peer (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
705 struct MeshPeerPath *p;
706 struct MeshPeerPath *aux;
707 unsigned int destroyed;
715 for (i = 0; i < (p->length - 1); i++)
717 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
718 (p->peers[i] == p2 && p->peers[i + 1] == p1))
730 for (i = 0; i < peer->ntunnels; i++)
732 tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
738 * Add the path to the peer and update the path used to reach it in case this
741 * @param peer_info Destination peer to add the path to.
742 * @param path New path to add. Last peer must be the peer in arg 1.
743 * Path will be either used of freed if already known.
745 * TODO: trim the part from origin to us? Add it as path to origin?
748 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
750 struct MeshPeerPath *aux;
754 if (NULL == peer_info || NULL == path)
760 l = path_get_length (path);
762 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
764 l2 = path_get_length (aux);
767 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
768 peer_info->path_tail, aux, path);
772 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
779 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
786 * Add the path to the origin peer and update the path used to reach it in case
787 * this is the shortest.
788 * The path is given in peer_info -> destination, therefore we turn the path
791 * @param peer_info Peer to add the path to, being the origin of the path.
792 * @param path New path to add after being inversed.
795 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
798 path_add_to_peer (peer_info, path);
803 * Build a PeerPath from the paths returned from the DHT, reversing the paths
804 * to obtain a local peer -> destination path and interning the peer ids.
806 * @param get_path NULL-terminated array of pointers
807 * to the peers on reverse GET path (or NULL if not recorded)
808 * @param put_path NULL-terminated array of pointers
809 * to the peers on the PUT path (or NULL if not recorded)
811 * @return Newly allocated and created path
813 static struct MeshPeerPath *
814 path_build_from_dht (const struct GNUNET_PeerIdentity *const *get_path,
815 const struct GNUNET_PeerIdentity *const *put_path)
817 struct MeshPeerPath *p;
821 p = GNUNET_malloc (sizeof (struct MeshPeerPath));
822 for (i = 0; get_path[i] != NULL; i++) ;
823 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
824 for (i--; i >= 0; i--)
826 id = GNUNET_PEER_intern (get_path[i]);
827 if (p->length > 0 && id == p->peers[p->length - 1])
829 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
830 GNUNET_PEER_change_rc(id, -1);
835 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
836 p->peers[p->length] = id;
840 for (i = 0; put_path[i] != NULL; i++) ;
841 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
842 for (i--; i >= 0; i--)
844 id = GNUNET_PEER_intern (put_path[i]);
845 if (p->length > 0 && id == p->peers[p->length - 1])
847 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
848 GNUNET_PEER_change_rc(id, -1);
853 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
854 p->peers[p->length] = id;
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "MESH: (first of GET: %s)\n",
861 GNUNET_h2s_full(&get_path[0]->hashPubKey));
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863 "MESH: (first of PUT: %s)\n",
864 GNUNET_h2s_full(&put_path[0]->hashPubKey));
865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
866 "MESH: In total: %d hops\n",
874 * Send keepalive packets for a peer
882 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
884 struct MeshTunnel *t = cls;
886 // struct GNUNET_PeerIdentity id;
888 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
892 /* FIXME implement multicast keepalive. Just an empty multicast packet? */
893 // GNUNET_PEER_resolve (path_get_first_hop (path->t, path->peer)->id, &id);
894 // GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
895 // GNUNET_TIME_UNIT_FOREVER_REL, &id,
896 // sizeof (struct GNUNET_MESH_ManipulatePath)
898 // (path->path->length *
899 // sizeof (struct GNUNET_PeerIdentity)),
900 // &send_core_create_path,
902 t->path_refresh_task =
903 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
909 * Check if client has registered with the service and has not disconnected
911 * @param client the client to check
913 * @return non-NULL if client exists in the global DLL
915 static struct MeshClient *
916 client_get (struct GNUNET_SERVER_Client *client)
918 struct MeshClient *c;
923 if (c->handle == client)
932 * Checks if a given client has subscribed to certain message type
934 * @param message_type Type of message to check
935 * @param c Client to check
937 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
942 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
946 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
947 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
952 * Search for a tunnel among the tunnels for a client
954 * @param c the client whose tunnels to search in
955 * @param tid the local id of the tunnel
957 * @return tunnel handler, NULL if doesn't exist
959 static struct MeshTunnel *
960 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
962 GNUNET_HashCode hash;
964 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
965 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
970 * Search for a tunnel by global ID using PEER_ID
972 * @param pi owner of the tunnel
973 * @param tid global tunnel number
975 * @return tunnel handler, NULL if doesn't exist
977 static struct MeshTunnel *
978 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
980 struct MESH_TunnelID id;
981 GNUNET_HashCode hash;
986 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
987 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
992 * Search for a tunnel by global ID using full PeerIdentities
994 * @param oid owner of the tunnel
995 * @param tid global tunnel number
997 * @return tunnel handler, NULL if doesn't exist
999 static struct MeshTunnel *
1000 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1002 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1007 * Callback used to notify a client owner of a tunnel that a peer has
1008 * disconnected, most likely because of a path change.
1010 * @param n Node in the tree representing the disconnected peer
1013 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1015 struct GNUNET_MESH_PeerControl msg;
1017 if (NULL == n->t->client || NULL == nc)
1020 msg.header.size = htons (sizeof (msg));
1021 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1022 msg.tunnel_id = htonl (n->t->local_tid);
1023 GNUNET_PEER_resolve (n->peer, &msg.peer);
1024 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1025 &msg.header, GNUNET_NO);
1030 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1031 * needed rescources.
1033 * @param t Tunnel we want to add a new peer to
1034 * @param peer PeerInfo of the peer being added
1038 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1040 struct MeshPeerPath *p;
1041 struct MeshPeerPath *best_p;
1042 unsigned int best_cost;
1045 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1046 if (NULL == (p = peer->path_head))
1050 best_cost = UINT_MAX;
1053 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1060 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1061 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1062 t->path_refresh_task =
1063 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1068 * Notify a tunnel that a connection has broken that affects at least
1069 * some of its peers.
1071 * @param t Tunnel affected
1072 * @param peer Peer that (at least) has been affected by the disconnection
1073 * @param p1 Peer that got disconnected from p2
1074 * @param p2 Peer that got disconnected from p1
1079 tunnel_notify_connection_broken (struct MeshTunnel *t,
1080 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1087 * Recursively destory the path tree of a tunnel.
1088 * Note: it does not liberate memory for itself, parent must do it!
1090 * @param n The node to destroy, along with children.
1092 * @return GNUNET_OK on success
1095 tunnel_destroy_tree_node (struct MeshTunnelTreeNode *n)
1099 for (i = 0; i < n->nchildren; i++)
1101 tunnel_destroy_tree_node(&n->children[i]);
1103 if (NULL != n->children)
1104 GNUNET_free (n->children);
1109 * Destroy the tunnel and free any allocated resources linked to it
1111 * @param t the tunnel to destroy
1113 * @return GNUNET_OK on success
1116 tunnel_destroy (struct MeshTunnel *t)
1118 struct MeshClient *c;
1119 struct MeshQueue *q;
1120 struct MeshQueue *qn;
1121 GNUNET_HashCode hash;
1124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1133 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1134 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1139 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1140 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1144 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1148 if (NULL != q->data)
1149 GNUNET_free (q->data);
1153 /* TODO cancel core transmit ready in case it was active */
1156 GNUNET_CONTAINER_multihashmap_iterate(t->tree->first_hops, &iterate_free, t);
1157 GNUNET_CONTAINER_multihashmap_destroy(t->tree->first_hops);
1158 tunnel_destroy_tree_node(t->tree->root);
1159 GNUNET_free(t->tree->root);
1160 GNUNET_free (t->tree);
1167 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1168 * client when the client disconnects.
1170 * @param cls closure (client that is disconnecting)
1171 * @param key the hash of the local tunnel id (used to access the hashmap)
1172 * @param value the value stored at the key (tunnel to destroy)
1174 * @return GNUNET_OK on success
1177 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1181 r = tunnel_destroy ((struct MeshTunnel *) value);
1186 /******************************************************************************/
1187 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1188 /******************************************************************************/
1192 * Function called to notify a client about the socket
1193 * being ready to queue more data. "buf" will be
1194 * NULL and "size" zero if the socket was closed for
1195 * writing in the meantime.
1197 * @param cls closure
1198 * @param size number of bytes available in buf
1199 * @param buf where the callee should write the message
1200 * @return number of bytes written to buf
1203 send_core_create_path (void *cls, size_t size, void *buf)
1205 struct MeshPathInfo *info = cls;
1206 struct GNUNET_MESH_ManipulatePath *msg;
1207 struct GNUNET_PeerIdentity *peer_ptr;
1208 struct MeshPeerInfo *peer = info->peer;
1209 struct MeshTunnel *t = info->t;
1210 struct MeshPeerPath *p = info->path;
1215 sizeof (struct GNUNET_MESH_ManipulatePath) +
1216 p->length * sizeof (struct GNUNET_PeerIdentity);
1218 if (size < size_needed || NULL == buf)
1220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
1221 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1222 GNUNET_TIME_UNIT_FOREVER_REL,
1223 path_get_first_hop (t->tree, peer->id),
1224 size_needed, &send_core_create_path,
1229 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1230 msg->header.size = htons (size_needed);
1231 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1232 msg->tid = ntohl (t->id.tid);
1234 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1235 for (i = 0; i < p->length; i++)
1237 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1249 * Function called to notify a client about the socket
1250 * being ready to queue more data. "buf" will be
1251 * NULL and "size" zero if the socket was closed for
1252 * writing in the meantime.
1254 * @param cls closure (MeshDataDescriptor with all info to build packet)
1255 * @param size number of bytes available in buf
1256 * @param buf where the callee should write the message
1257 * @return number of bytes written to buf
1260 send_core_data_to_origin (void *cls, size_t size, void *buf)
1262 struct MeshDataDescriptor *info = cls;
1263 struct GNUNET_MESH_ToOrigin *msg = buf;
1266 GNUNET_assert (NULL != info);
1267 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1268 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1270 if (total_size > size)
1272 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1273 "not enough buffer to send data to origin\n");
1276 msg->header.size = htons (total_size);
1277 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1278 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1279 msg->tid = htonl (info->origin->tid);
1280 if (0 != info->size)
1282 memcpy (&msg[1], &info[1], info->size);
1284 if (NULL != info->client)
1286 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1294 * Function called to notify a client about the socket
1295 * being ready to queue more data. "buf" will be
1296 * NULL and "size" zero if the socket was closed for
1297 * writing in the meantime.
1299 * @param cls closure (data itself)
1300 * @param size number of bytes available in buf
1301 * @param buf where the callee should write the message
1302 * @return number of bytes written to buf
1305 send_core_data_unicast (void *cls, size_t size, void *buf)
1307 struct MeshDataDescriptor *info = cls;
1308 struct GNUNET_MESH_Unicast *msg = buf;
1311 GNUNET_assert (NULL != info);
1312 total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
1313 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1315 if (total_size > size)
1317 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1318 "not enough buffer to send data to peer\n");
1321 msg->header.size = htons (total_size);
1322 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1323 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1324 GNUNET_PEER_resolve (info->destination, &msg->destination);
1325 msg->tid = htonl (info->origin->tid);
1326 if (0 != info->size)
1328 memcpy (&msg[1], &info[1], info->size);
1330 if (NULL != info->client)
1332 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1340 * Function called to notify a client about the socket
1341 * being ready to queue more data. "buf" will be
1342 * NULL and "size" zero if the socket was closed for
1343 * writing in the meantime.
1345 * @param cls closure (data itself)
1346 * @param size number of bytes available in buf
1347 * @param buf where the callee should write the message
1348 * @return number of bytes written to buf
1351 send_core_data_multicast (void *cls, size_t size, void *buf)
1353 struct MeshDataDescriptor *info = cls;
1354 struct GNUNET_MESH_Multicast *msg = buf;
1357 GNUNET_assert (NULL != info);
1358 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1359 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1363 info->peer->core_transmit[info->handler_n] = NULL;
1365 if (total_size > size)
1367 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1368 "not enough buffer to send data futher\n");
1371 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1372 msg->header.size = htons (total_size);
1373 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1374 msg->tid = htonl (info->origin->tid);
1375 memcpy (&msg[1], &info[1], total_size);
1376 if (0 == --info->copies)
1378 if (NULL != info->client)
1380 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1389 * Function called to notify a client about the socket
1390 * being ready to queue more data. "buf" will be
1391 * NULL and "size" zero if the socket was closed for
1392 * writing in the meantime.
1394 * @param cls closure (MeshDataDescriptor)
1395 * @param size number of bytes available in buf
1396 * @param buf where the callee should write the message
1397 * @return number of bytes written to buf
1400 send_core_path_ack (void *cls, size_t size, void *buf)
1402 struct MeshDataDescriptor *info = cls;
1403 struct GNUNET_MESH_PathACK *msg = buf;
1405 GNUNET_assert (NULL != info);
1408 info->peer->core_transmit[info->handler_n] = NULL;
1410 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1415 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1416 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1417 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1418 msg->tid = htonl (info->origin->tid);
1419 msg->peer_id = my_full_id;
1421 /* TODO add signature */
1423 return sizeof (struct GNUNET_MESH_PathACK);
1428 * Function called to notify a client about the socket
1429 * being ready to queue more data. "buf" will be
1430 * NULL and "size" zero if the socket was closed for
1431 * writing in the meantime.
1433 * @param cls closure (data itself)
1434 * @param size number of bytes available in buf
1435 * @param buf where the callee should write the message
1436 * @return number of bytes written to buf
1439 send_core_data_raw (void *cls, size_t size, void *buf)
1441 struct GNUNET_MessageHeader *msg = cls;
1444 GNUNET_assert (NULL != msg);
1445 total_size = ntohs (msg->size);
1447 if (total_size > size)
1452 memcpy (buf, msg, total_size);
1460 * Send another peer a notification to destroy a tunnel
1461 * @param cls The tunnel to destroy
1462 * @param size Size in the buffer
1463 * @param buf Memory where to put the data to transmit
1464 * @return Size of data put in buffer
1467 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1469 struct MeshTunnel *t = cls;
1470 struct MeshClient *c;
1471 struct GNUNET_MESH_TunnelMessage *msg;
1475 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1476 /*FIXME*/ msg->header.size =
1477 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1478 msg->tunnel_id = htonl (t->id.tid);
1480 tunnel_destroy (c, t);
1481 return sizeof (struct GNUNET_MESH_TunnelMessage);
1487 * Send the message to all clients that have subscribed to its type
1489 * @param msg Pointer to the message itself
1490 * @return number of clients this message was sent to
1493 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1495 struct MeshClient *c;
1499 type = ntohs (msg->type);
1500 for (count = 0, c = clients; c != NULL; c = c->next)
1502 if (client_is_subscribed (type, c))
1505 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1515 * Notify the client that owns the tunnel that a peer has connected to it
1517 * @param t Tunnel whose owner to notify
1518 * @param id Short id of the peer that has connected
1521 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1523 struct GNUNET_MESH_PeerControl pc;
1525 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1526 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1527 pc.tunnel_id = htonl (t->local_tid);
1528 GNUNET_PEER_resolve (id, &pc.peer);
1529 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1530 &pc.header, GNUNET_NO);
1534 /******************************************************************************/
1535 /******************** MESH NETWORK HANDLERS **************************/
1536 /******************************************************************************/
1540 * Core handler for path creation
1541 * struct GNUNET_CORE_MessageHandler
1543 * @param cls closure
1544 * @param message message
1545 * @param peer peer identity this notification is about
1546 * @param atsi performance data
1547 * @return GNUNET_OK to keep the connection open,
1548 * GNUNET_SYSERR to close it (signal serious error)
1552 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1553 const struct GNUNET_MessageHeader *message,
1554 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1556 unsigned int own_pos;
1559 MESH_TunnelNumber tid;
1560 struct GNUNET_MESH_ManipulatePath *msg;
1561 struct GNUNET_PeerIdentity *pi;
1562 struct GNUNET_PeerIdentity id;
1563 GNUNET_HashCode hash;
1564 struct MeshPeerPath *path;
1565 struct MeshPeerInfo *dest_peer_info;
1566 struct MeshPeerInfo *orig_peer_info;
1567 struct MeshTunnel *t;
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1570 "MESH: Received a MESH path create msg\n");
1571 size = ntohs (message->size);
1572 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1574 GNUNET_break_op (0);
1578 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1579 if (size % sizeof (struct GNUNET_PeerIdentity))
1581 GNUNET_break_op (0);
1584 size /= sizeof (struct GNUNET_PeerIdentity);
1587 GNUNET_break_op (0);
1590 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1592 tid = ntohl (msg->tid);
1593 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1594 t = tunnel_get (pi, tid);
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1599 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1600 t->id.oid = GNUNET_PEER_intern (pi);
1602 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1604 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1606 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1607 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1615 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1616 if (NULL == dest_peer_info)
1618 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1619 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1620 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1622 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1624 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1625 if (NULL == orig_peer_info)
1627 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1628 orig_peer_info->id = GNUNET_PEER_intern (pi);
1629 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1630 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1633 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
1634 path->length = size;
1635 path->peers = GNUNET_malloc (size * sizeof (GNUNET_PEER_Id));
1637 for (i = 0; i < size; i++)
1639 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1640 if (path->peers[i] == myid)
1644 { /* cannot be self, must be 'not found' */
1645 /* create path: self not found in path through self */
1646 GNUNET_break_op (0);
1647 path_destroy (path);
1648 /* FIXME error. destroy tunnel? leave for timeout? */
1651 if (own_pos == size - 1)
1653 /* It is for us! Send ack. */
1654 struct MeshDataDescriptor *info;
1657 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1658 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1659 info->origin = &t->id;
1660 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1661 GNUNET_assert (info->peer);
1662 for (j = 0; info->peer->core_transmit[j]; j++)
1670 info->handler_n = j;
1671 info->peer->core_transmit[j] =
1672 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1673 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1674 sizeof (struct GNUNET_MessageHeader),
1675 &send_core_path_ack, info);
1679 /* It's for somebody else! Retransmit. */
1680 struct MeshPathInfo *path_info;
1682 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1684 path_info->path = path;
1685 path_info->peer = dest_peer_info;
1687 path_add_to_peer (dest_peer_info, path);
1688 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1689 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1690 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1691 sizeof (struct GNUNET_MessageHeader),
1692 &send_core_create_path, path_info);
1699 * Core handler for mesh network traffic going from the origin to a peer
1701 * @param cls closure
1702 * @param peer peer identity this notification is about
1703 * @param message message
1704 * @param atsi performance data
1705 * @return GNUNET_OK to keep the connection open,
1706 * GNUNET_SYSERR to close it (signal serious error)
1709 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1710 const struct GNUNET_MessageHeader *message,
1711 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1713 struct GNUNET_MESH_Unicast *msg;
1714 struct MeshTunnel *t;
1715 struct MeshPeerInfo *pi;
1718 size = ntohs (message->size);
1720 sizeof (struct GNUNET_MESH_Unicast) +
1721 sizeof (struct GNUNET_MessageHeader))
1726 msg = (struct GNUNET_MESH_Unicast *) message;
1727 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1730 /* TODO notify back: we don't know this tunnel */
1733 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1734 &msg->destination.hashPubKey);
1737 /* TODO maybe feedback, log to statistics */
1742 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1745 msg = GNUNET_malloc (size);
1746 memcpy (msg, message, size);
1747 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1748 GNUNET_TIME_UNIT_FOREVER_REL,
1749 path_get_first_hop (t->tree, pi->id),
1751 &send_core_data_raw, msg);
1757 * Core handler for mesh network traffic going from the origin to all peers
1759 * @param cls closure
1760 * @param message message
1761 * @param peer peer identity this notification is about
1762 * @param atsi performance data
1763 * @return GNUNET_OK to keep the connection open,
1764 * GNUNET_SYSERR to close it (signal serious error)
1767 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1768 const struct GNUNET_MessageHeader *message,
1769 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1771 struct GNUNET_MESH_Multicast *msg;
1772 struct GNUNET_PeerIdentity id;
1773 struct MeshDataDescriptor *info;
1774 struct MeshPathInfo neighbors;
1775 struct MeshTunnel *t;
1781 size = ntohs (message->size);
1783 sizeof (struct GNUNET_MESH_Multicast) +
1784 sizeof (struct GNUNET_MessageHeader))
1786 GNUNET_break_op (0);
1789 msg = (struct GNUNET_MESH_Multicast *) message;
1790 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1794 /* TODO notify that we dont know that tunnel */
1798 /* Transmit to locally interested clients */
1799 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1801 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1804 /* Retransmit to other peers.
1805 * Using path here as just a collection of peers, not a path per se.
1808 neighbors.path = GNUNET_malloc (sizeof (struct MeshPeerPath));
1809 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &iterate_collect_neighbors,
1811 if (0 == neighbors.path->length)
1813 GNUNET_free (neighbors.path);
1816 size -= sizeof (struct GNUNET_MESH_Multicast);
1817 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + size);
1818 info->origin = &t->id;
1819 info->copies = neighbors.path->length;
1820 for (i = 0; i < info->copies; i++)
1822 GNUNET_PEER_resolve (neighbors.path->peers[i], &id);
1823 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1824 GNUNET_assert (NULL != info->peer);
1825 for (j = 0; 0 != info->peer->core_transmit[j]; j++)
1827 if (j == (CORE_QUEUE_SIZE - 1))
1833 info->handler_n = j;
1834 info->peer->infos[j] = info;
1835 info->peer->core_transmit[j] =
1836 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1837 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1838 ntohs (msg->header.size),
1839 &send_core_data_multicast, info);
1841 GNUNET_free (neighbors.path->peers);
1842 GNUNET_free (neighbors.path);
1848 * Core handler for mesh network traffic
1850 * @param cls closure
1851 * @param message message
1852 * @param peer peer identity this notification is about
1853 * @param atsi performance data
1855 * @return GNUNET_OK to keep the connection open,
1856 * GNUNET_SYSERR to close it (signal serious error)
1859 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1860 const struct GNUNET_MessageHeader *message,
1861 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1863 struct GNUNET_MESH_ToOrigin *msg;
1864 struct GNUNET_PeerIdentity id;
1865 struct MeshPeerInfo *peer_info;
1866 struct MeshTunnel *t;
1869 size = ntohs (message->size);
1870 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1871 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1873 GNUNET_break_op (0);
1876 msg = (struct GNUNET_MESH_ToOrigin *) message;
1877 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1881 /* TODO notify that we dont know this tunnel (whom)? */
1885 if (t->id.oid == myid)
1887 if (NULL == t->client)
1889 /* got data packet for ownerless tunnel */
1890 GNUNET_break_op (0);
1893 /* TODO signature verification */
1894 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1898 peer_info = peer_info_get (&msg->oid);
1899 if (NULL == peer_info)
1901 /* unknown origin of tunnel */
1905 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1906 msg = GNUNET_malloc (size);
1907 memcpy (msg, message, size);
1908 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1909 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1910 &send_core_data_raw, msg);
1917 * Core handler for path ACKs
1919 * @param cls closure
1920 * @param message message
1921 * @param peer peer identity this notification is about
1922 * @param atsi performance data
1924 * @return GNUNET_OK to keep the connection open,
1925 * GNUNET_SYSERR to close it (signal serious error)
1927 * FIXME path change state
1930 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1931 const struct GNUNET_MessageHeader *message,
1932 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1934 struct GNUNET_MESH_PathACK *msg;
1935 struct MeshTunnel *t;
1936 struct MeshPeerInfo *peer_info;
1938 msg = (struct GNUNET_MESH_PathACK *) message;
1939 t = tunnel_get (&msg->oid, msg->tid);
1942 /* TODO notify that we don't know the tunnel */
1946 /* Message for us? */
1947 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1950 if (NULL == t->client)
1955 peer_info = peer_info_get (&msg->peer_id);
1956 if (NULL == peer_info)
1958 GNUNET_break_op (0);
1961 /* FIXME change state of peer */
1962 send_client_peer_connected(t, peer_info->id);
1966 peer_info = peer_info_get (&msg->oid);
1967 if (NULL == peer_info)
1969 /* If we know the tunnel, we should DEFINITELY know the peer */
1973 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1974 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1975 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1976 GNUNET_TIME_UNIT_FOREVER_REL,
1977 path_get_first_hop (t->tree,
1979 sizeof (struct GNUNET_MESH_PathACK),
1980 &send_core_data_raw, msg);
1986 * Functions to handle messages from core
1988 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1989 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1990 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1991 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1992 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1993 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1994 sizeof (struct GNUNET_MESH_PathACK)},
2000 /******************************************************************************/
2001 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2002 /******************************************************************************/
2005 * deregister_app: iterator for removing each application registered by a client
2007 * @param cls closure
2008 * @param key the hash of the application id (used to access the hashmap)
2009 * @param value the value stored at the key (client)
2011 * @return GNUNET_OK on success
2014 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2016 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2022 * notify_client_connection_failure: notify a client that the connection to the
2023 * requested remote peer is not possible (for instance, no route found)
2024 * Function called when the socket is ready to queue more data. "buf" will be
2025 * NULL and "size" zero if the socket was closed for writing in the meantime.
2027 * @param cls closure
2028 * @param size number of bytes available in buf
2029 * @param buf where the callee should write the message
2030 * @return number of bytes written to buf
2033 notify_client_connection_failure (void *cls, size_t size, void *buf)
2036 struct MeshPeerInfo *peer_info;
2037 struct GNUNET_MESH_PeerControl *msg;
2038 struct GNUNET_PeerIdentity id;
2040 if (0 == size && NULL == buf)
2042 // TODO retry? cancel?
2046 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2047 peer_info = (struct MeshPeerInfo *) cls;
2048 msg = (struct GNUNET_MESH_PeerControl *) buf;
2049 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2050 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2051 // msg->tunnel_id = htonl(peer_info->t->tid);
2052 GNUNET_PEER_resolve (peer_info->id, &id);
2053 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2061 * Function to process paths received for a new peer addition. The recorded
2062 * paths form the initial tunnel, which can be optimized later.
2063 * Called on each result obtained for the DHT search.
2065 * @param cls closure
2066 * @param exp when will this value expire
2067 * @param key key of the result
2068 * @param get_path NULL-terminated array of pointers
2069 * to the peers on reverse GET path (or NULL if not recorded)
2070 * @param put_path NULL-terminated array of pointers
2071 * to the peers on the PUT path (or NULL if not recorded)
2072 * @param type type of the result
2073 * @param size number of bytes in data
2074 * @param data pointer to the result data
2079 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2080 const GNUNET_HashCode * key,
2081 const struct GNUNET_PeerIdentity *const *get_path,
2082 const struct GNUNET_PeerIdentity *const *put_path,
2083 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2085 struct MeshPathInfo *path_info = cls;
2086 struct MeshPeerPath *p;
2087 struct GNUNET_PeerIdentity pi;
2090 if (NULL == get_path || NULL == put_path)
2092 if (NULL == path_info->peer->path_head)
2094 // Find ourselves some alternate initial path to the destination: retry
2095 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2096 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2097 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2098 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2099 GNUNET_BLOCK_TYPE_TEST, /* type */
2100 &pi.hashPubKey, /*key to search */
2101 4, /* replication level */
2102 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2105 0, /* xquery bits */
2107 (void *) path_info);
2112 p = path_build_from_dht (get_path, put_path);
2113 path_add_to_peer (path_info->peer, p);
2114 for (i = 0; i < path_info->peer->ntunnels; i++)
2116 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2118 GNUNET_free (path_info);
2125 * Function to process paths received for a new peer addition. The recorded
2126 * paths form the initial tunnel, which can be optimized later.
2127 * Called on each result obtained for the DHT search.
2129 * @param cls closure
2130 * @param exp when will this value expire
2131 * @param key key of the result
2132 * @param get_path NULL-terminated array of pointers
2133 * to the peers on reverse GET path (or NULL if not recorded)
2134 * @param put_path NULL-terminated array of pointers
2135 * to the peers on the PUT path (or NULL if not recorded)
2136 * @param type type of the result
2137 * @param size number of bytes in data
2138 * @param data pointer to the result data
2141 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2142 const GNUNET_HashCode * key,
2143 const struct GNUNET_PeerIdentity *const *get_path,
2144 const struct GNUNET_PeerIdentity *const *put_path,
2145 enum GNUNET_BLOCK_Type type, size_t size,
2148 const struct GNUNET_PeerIdentity *pi = data;
2149 struct GNUNET_PeerIdentity id;
2150 struct MeshTunnel *t = cls;
2151 struct MeshPeerInfo *peer_info;
2152 struct MeshPathInfo *path_info;
2153 struct MeshPeerPath *p;
2156 if (size != sizeof (struct GNUNET_PeerIdentity))
2158 GNUNET_break_op (0);
2161 GNUNET_assert (NULL != t->client);
2162 GNUNET_DHT_get_stop (t->client->dht_get_type);
2163 t->client->dht_get_type = NULL;
2164 peer_info = peer_info_get (pi);
2165 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2166 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2168 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2169 NULL == peer_info->dhtget)
2171 /* we don't have a route to the peer, let's try a direct lookup */
2172 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2174 GNUNET_TIME_UNIT_FOREVER_REL,
2176 GNUNET_BLOCK_TYPE_TEST,
2179 /* key to look up */
2181 /* replication level */
2182 GNUNET_DHT_RO_RECORD_ROUTE,
2183 /* option to dht: record route */
2184 NULL, /* bloom filter */
2187 0, /* xquery bits */
2190 peer_info); /* closure */
2193 p = path_build_from_dht (get_path, put_path);
2194 path_add_to_peer (peer_info, p);
2195 tunnel_add_peer(t, peer_info);
2196 p = tree_get_path_to_peer(t->tree, peer_info->id);
2198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2199 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2200 t->local_tid, p->length);
2201 for (i = 0; i < p->length; i++)
2203 GNUNET_PEER_resolve (p->peers[0], &id);
2204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2205 GNUNET_h2s_full (&id.hashPubKey));
2211 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2213 path_info->peer = peer_info;
2214 path_info->path = p;
2215 GNUNET_PEER_resolve (p->peers[1], &id);
2216 GNUNET_CORE_notify_transmit_ready (core_handle,
2222 GNUNET_TIME_UNIT_FOREVER_REL,
2226 sizeof (struct GNUNET_MESH_ManipulatePath)
2229 sizeof (struct GNUNET_PeerIdentity)),
2231 &send_core_create_path,
2233 path_info); /* cls */
2237 send_client_peer_connected(t, myid);
2240 /******************************************************************************/
2241 /********************* MESH LOCAL HANDLES **************************/
2242 /******************************************************************************/
2246 * Handler for client disconnection
2248 * @param cls closure
2249 * @param client identification of the client; NULL
2250 * for the last call when the server is destroyed
2253 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2255 struct MeshClient *c;
2256 struct MeshClient *next;
2258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2264 if (c->handle != client && NULL != client)
2266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2271 if (NULL != c->tunnels)
2273 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2274 &tunnel_destroy_iterator,
2276 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2279 /* deregister clients applications */
2280 if (NULL != c->apps)
2282 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2283 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2285 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2286 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2288 GNUNET_SCHEDULER_cancel (announce_applications_task);
2289 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2291 if (NULL != c->types)
2292 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2293 if (NULL != c->dht_get_type)
2294 GNUNET_DHT_get_stop (c->dht_get_type);
2295 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2307 * Handler for new clients
2309 * @param cls closure
2310 * @param client identification of the client
2311 * @param message the actual message, which includes messages the client wants
2314 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2315 const struct GNUNET_MessageHeader *message)
2317 struct GNUNET_MESH_ClientConnect *cc_msg;
2318 struct MeshClient *c;
2319 GNUNET_MESH_ApplicationType *a;
2326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2327 /* Check data sanity */
2328 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2329 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2330 ntypes = ntohs (cc_msg->types);
2331 napps = ntohs (cc_msg->applications);
2333 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2336 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2340 /* Create new client structure */
2341 c = GNUNET_malloc (sizeof (struct MeshClient));
2343 c->id = next_client_id++;
2346 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2349 GNUNET_MESH_ApplicationType at;
2352 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2353 for (i = 0; i < napps; i++)
2356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2357 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2358 /* store in clients hashmap */
2359 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2360 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2361 /* store in global hashmap, for announcements */
2362 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2363 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2365 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2366 announce_applications_task =
2367 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2375 t = (uint16_t *) & a[napps];
2376 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2377 for (i = 0; i < ntypes; i++)
2380 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2382 /* store in clients hashmap */
2383 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2384 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2385 /* store in global hashmap */
2386 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2387 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2391 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2393 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2394 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2395 GNUNET_SERVER_notification_context_add (nc, client);
2397 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2405 * Handler for requests of new tunnels
2407 * @param cls closure
2408 * @param client identification of the client
2409 * @param message the actual message
2412 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2413 const struct GNUNET_MessageHeader *message)
2415 struct GNUNET_MESH_TunnelMessage *t_msg;
2416 struct MeshTunnel *t;
2417 struct MeshClient *c;
2418 GNUNET_HashCode hash;
2420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2422 /* Sanity check for client registration */
2423 if (NULL == (c = client_get (client)))
2426 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2433 /* Message sanity check */
2434 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2437 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2441 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2442 /* Sanity check for tunnel numbering */
2443 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2446 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2449 /* Sanity check for duplicate tunnel IDs */
2450 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2453 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2457 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2459 while (NULL != tunnel_get_by_pi (myid, next_tid))
2460 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2461 t->id.tid = next_tid++;
2463 t->local_tid = ntohl (t_msg->tunnel_id);
2465 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2467 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2469 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2470 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2473 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2477 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2479 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2480 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2483 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2486 t->tree = GNUNET_malloc (sizeof(struct MeshTunnelTree));
2487 t->tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
2489 t->tree->refresh = REFRESH_PATH_TIME;
2490 t->tree->root = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
2491 t->tree->root->status = MESH_PEER_READY;
2492 t->tree->root->t = t;
2493 t->tree->root->peer = myid;
2494 t->tree->me = t->tree->root;
2496 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2502 * Handler for requests of deleting tunnels
2504 * @param cls closure
2505 * @param client identification of the client
2506 * @param message the actual message
2509 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2510 const struct GNUNET_MessageHeader *message)
2512 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2513 struct MeshClient *c;
2514 struct MeshTunnel *t;
2515 MESH_TunnelNumber tid;
2516 GNUNET_HashCode hash;
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2520 /* Sanity check for client registration */
2521 if (NULL == (c = client_get (client)))
2524 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2527 /* Message sanity check */
2528 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2537 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2539 /* Retrieve tunnel */
2540 tid = ntohl (tunnel_msg->tunnel_id);
2542 /* Remove from local id hashmap */
2543 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2544 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2545 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2547 /* Remove from global id hashmap */
2548 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2549 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2551 // notify_tunnel_destroy(t); FIXME
2553 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2559 * Handler for connection requests to new peers
2561 * @param cls closure
2562 * @param client identification of the client
2563 * @param message the actual message (PeerControl)
2568 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2569 const struct GNUNET_MessageHeader *message)
2571 struct GNUNET_MESH_PeerControl *peer_msg;
2572 struct MeshClient *c;
2573 struct MeshTunnel *t;
2574 MESH_TunnelNumber tid;
2575 struct MeshPeerInfo *peer_info;
2578 /* Sanity check for client registration */
2579 if (NULL == (c = client_get (client)))
2582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2586 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2587 /* Sanity check for message size */
2588 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2591 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2595 /* Tunnel exists? */
2596 tid = ntohl (peer_msg->tunnel_id);
2597 t = tunnel_get_by_local_id (c, tid);
2601 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2605 /* Does client own tunnel? */
2606 if (t->client->handle != client)
2609 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2614 peer_info = peer_info_get (&peer_msg->peer);
2616 /* Start DHT search if needed FIXME: if not already connected */
2617 if (NULL == peer_info->dhtget)
2619 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_BLOCK_TYPE_TEST, &peer_msg->peer.hashPubKey, 4, /* replication level */
2620 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2623 0, /* xquery bits */
2625 (void *) peer_info);
2628 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2634 * Handler for disconnection requests of peers in a tunnel
2636 * @param cls closure
2637 * @param client identification of the client
2638 * @param message the actual message (PeerControl)
2641 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2642 const struct GNUNET_MessageHeader *message)
2644 struct GNUNET_MESH_PeerControl *peer_msg;
2645 struct MeshClient *c;
2646 struct MeshTunnel *t;
2647 MESH_TunnelNumber tid;
2649 /* Sanity check for client registration */
2650 if (NULL == (c = client_get (client)))
2653 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2656 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2657 /* Sanity check for message size */
2658 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2661 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2665 /* Tunnel exists? */
2666 tid = ntohl (peer_msg->tunnel_id);
2667 t = tunnel_get_by_local_id (c, tid);
2671 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2675 /* Does client own tunnel? */
2676 if (t->client->handle != client)
2679 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2683 /* Ok, delete peer from tunnel */
2684 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2685 &peer_msg->peer.hashPubKey);
2687 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2693 * Handler for connection requests to new peers by type
2695 * @param cls closure
2696 * @param client identification of the client
2697 * @param message the actual message (ConnectPeerByType)
2700 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2701 const struct GNUNET_MessageHeader *message)
2703 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2704 struct MeshClient *c;
2705 struct MeshTunnel *t;
2706 GNUNET_HashCode hash;
2707 GNUNET_MESH_ApplicationType type;
2708 MESH_TunnelNumber tid;
2710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2711 /* Sanity check for client registration */
2712 if (NULL == (c = client_get (client)))
2715 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2719 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2720 /* Sanity check for message size */
2721 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2722 ntohs (connect_msg->header.size))
2725 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2729 /* Tunnel exists? */
2730 tid = ntohl (connect_msg->tunnel_id);
2731 t = tunnel_get_by_local_id (c, tid);
2735 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2739 /* Does client own tunnel? */
2740 if (t->client->handle != client)
2743 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2747 /* Do WE have the service? */
2748 type = ntohl (connect_msg->type);
2749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2750 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2751 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2754 /* Yes! Fast forward, add ourselves to the tunnel and send the
2755 * good news to the client
2757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2758 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2759 peer_info_get (&my_full_id),
2760 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2763 send_client_peer_connected(t, myid);
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2765 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2768 /* Ok, lets find a peer offering the service */
2769 if (c->dht_get_type)
2771 GNUNET_DHT_get_stop (c->dht_get_type);
2773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2774 GNUNET_h2s_full (&hash));
2776 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2777 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2778 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
2779 &dht_get_type_handler, t);
2781 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2787 * Handler for client traffic directed to one peer
2789 * @param cls closure
2790 * @param client identification of the client
2791 * @param message the actual message
2794 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2795 const struct GNUNET_MessageHeader *message)
2797 struct MeshClient *c;
2798 struct MeshTunnel *t;
2799 struct MeshPeerInfo *pi;
2800 struct GNUNET_MESH_Unicast *data_msg;
2801 struct MeshDataDescriptor *info;
2802 MESH_TunnelNumber tid;
2805 /* Sanity check for client registration */
2806 if (NULL == (c = client_get (client)))
2809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2812 data_msg = (struct GNUNET_MESH_Unicast *) message;
2813 /* Sanity check for message size */
2814 if (sizeof (struct GNUNET_MESH_Unicast) +
2815 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
2818 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2822 /* Tunnel exists? */
2823 tid = ntohl (data_msg->tid);
2824 t = tunnel_get_by_local_id (c, tid);
2828 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2832 /* Is it a local tunnel? Then, does client own the tunnel? */
2833 if (t->client->handle != NULL && t->client->handle != client)
2836 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2840 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2841 &data_msg->destination.hashPubKey);
2842 /* Is the selected peer in the tunnel? */
2846 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2851 struct GNUNET_MESH_Unicast copy;
2853 /* Work around const limitation */
2854 memcpy (©, data_msg, sizeof (struct GNUNET_MESH_Unicast));
2855 copy.oid = my_full_id;
2856 copy.tid = htonl (t->id.tid);
2857 handle_mesh_data_unicast (NULL, &my_full_id, ©.header, NULL);
2860 data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
2861 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
2862 memcpy (&info[1], &data_msg[1], data_size);
2863 info->destination = pi->id;
2864 info->origin = &t->id;
2865 info->size = data_size;
2866 info->client = client;
2867 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2868 GNUNET_TIME_UNIT_FOREVER_REL,
2869 path_get_first_hop (t->tree, pi->id),
2870 /* FIXME re-check types */
2872 sizeof (struct GNUNET_MESH_Unicast),
2873 &send_core_data_unicast, info);
2878 * Handler for client traffic directed to all peers in a tunnel
2880 * @param cls closure
2881 * @param client identification of the client
2882 * @param message the actual message
2885 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2886 const struct GNUNET_MessageHeader *message)
2888 struct MeshClient *c;
2889 struct MeshTunnel *t;
2890 struct GNUNET_MESH_Multicast *data_msg;
2891 MESH_TunnelNumber tid;
2893 /* Sanity check for client registration */
2894 if (NULL == (c = client_get (client)))
2897 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2900 data_msg = (struct GNUNET_MESH_Multicast *) message;
2901 /* Sanity check for message size */
2902 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2905 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2909 /* Tunnel exists? */
2910 tid = ntohl (data_msg->tid);
2911 t = tunnel_get_by_local_id (c, tid);
2915 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2919 /* Does client own tunnel? */
2920 if (t->client->handle != client)
2923 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2929 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2934 * Functions to handle messages from clients
2936 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
2937 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2938 {&handle_local_tunnel_create, NULL,
2939 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2940 sizeof (struct GNUNET_MESH_TunnelMessage)},
2941 {&handle_local_tunnel_destroy, NULL,
2942 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2943 sizeof (struct GNUNET_MESH_TunnelMessage)},
2944 {&handle_local_connect_add, NULL,
2945 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
2946 sizeof (struct GNUNET_MESH_PeerControl)},
2947 {&handle_local_connect_del, NULL,
2948 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
2949 sizeof (struct GNUNET_MESH_PeerControl)},
2950 {&handle_local_connect_by_type, NULL,
2951 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
2952 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
2953 {&handle_local_unicast, NULL,
2954 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2955 {&handle_local_unicast, NULL,
2956 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2957 {&handle_local_multicast, NULL,
2958 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2964 * To be called on core init/fail.
2966 * @param cls service closure
2967 * @param server handle to the server for this service
2968 * @param identity the public identity of this peer
2969 * @param publicKey the public key of this peer
2972 core_init (void *cls, struct GNUNET_CORE_Handle *server,
2973 const struct GNUNET_PeerIdentity *identity,
2974 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
2977 core_handle = server;
2978 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
2980 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
2981 GNUNET_SCHEDULER_shutdown();
2987 * Method called whenever a given peer connects.
2989 * @param cls closure
2990 * @param peer peer identity this notification is about
2991 * @param atsi performance data for the connection
2994 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
2995 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2997 // GNUNET_PEER_Id pid;
2998 struct MeshPeerInfo *peer_info;
2999 struct MeshPeerPath *path;
3001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3002 peer_info = peer_info_get (peer);
3003 if (myid == peer_info->id)
3005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3008 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
3010 path->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id) * 2);
3011 path->peers[0] = myid;
3012 path->peers[1] = peer_info->id;
3013 path_add_to_peer (peer_info, path);
3018 * Method called whenever a peer disconnects.
3020 * @param cls closure
3021 * @param peer peer identity this notification is about
3024 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3026 struct MeshPeerInfo *pi;
3029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3030 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3036 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3038 if (pi->core_transmit[i])
3040 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3041 /* TODO: notify that tranmission has failed */
3042 GNUNET_free (pi->infos[i]);
3045 path_remove_from_peer (pi, pi->id, myid);
3048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3054 /******************************************************************************/
3055 /************************ MAIN FUNCTIONS ****************************/
3056 /******************************************************************************/
3059 * Task run during shutdown.
3065 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3067 struct MeshClient *c;
3069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3070 if (core_handle != NULL)
3072 GNUNET_CORE_disconnect (core_handle);
3075 if (dht_handle != NULL)
3077 for (c = clients; NULL != c; c = c->next)
3078 if (NULL != c->dht_get_type)
3079 GNUNET_DHT_get_stop (c->dht_get_type);
3080 GNUNET_DHT_disconnect (dht_handle);
3085 GNUNET_SERVER_notification_context_destroy (nc);
3088 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3090 GNUNET_SCHEDULER_cancel (announce_id_task);
3091 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3097 * Process mesh requests.
3099 * @param cls closure
3100 * @param server the initialized server
3101 * @param c configuration to use
3104 run (void *cls, struct GNUNET_SERVER_Handle *server,
3105 const struct GNUNET_CONFIGURATION_Handle *c)
3107 struct MeshPeerInfo *peer;
3108 struct MeshPeerPath *p;
3111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3112 server_handle = server;
3113 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3114 CORE_QUEUE_SIZE, /* queue size */
3115 NULL, /* Closure passed to MESH functions */
3116 &core_init, /* Call core_init once connected */
3117 &core_connect, /* Handle connects */
3118 &core_disconnect, /* remove peers on disconnects */
3119 NULL, /* Do we care about "status" updates? */
3120 NULL, /* Don't notify about all incoming messages */
3121 GNUNET_NO, /* For header only in notification */
3122 NULL, /* Don't notify about all outbound messages */
3123 GNUNET_NO, /* For header-only out notification */
3124 core_handlers); /* Register these handlers */
3125 if (core_handle == NULL)
3128 GNUNET_SCHEDULER_shutdown ();
3133 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3138 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3139 GNUNET_SCHEDULER_shutdown ();
3142 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3143 GNUNET_free (keyfile);
3144 if (my_private_key == NULL)
3146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3147 _("Mesh service could not access hostkey. Exiting.\n"));
3148 GNUNET_SCHEDULER_shutdown ();
3151 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3152 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3153 &my_full_id.hashPubKey);
3154 myid = GNUNET_PEER_intern (&my_full_id);
3156 dht_handle = GNUNET_DHT_connect (c, 64);
3157 if (dht_handle == NULL)
3159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3160 Running without DHT has a severe\
3161 impact in MESH capabilities.\n\
3162 Plase check your configuretion and enable DHT.\n");
3168 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3169 peers = GNUNET_CONTAINER_multihashmap_create (32);
3170 applications = GNUNET_CONTAINER_multihashmap_create (32);
3171 types = GNUNET_CONTAINER_multihashmap_create (32);
3173 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3174 nc = GNUNET_SERVER_notification_context_create (server_handle,
3176 GNUNET_SERVER_disconnect_notify (server_handle,
3177 &handle_local_client_disconnect,
3182 clients_tail = NULL;
3187 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3188 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3190 /* Create a peer_info for the local peer */
3191 peer = peer_info_get(&my_full_id);
3192 p = GNUNET_malloc (sizeof (struct MeshPeerPath));
3193 p->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id));
3196 path_add_to_peer(peer, p);
3198 /* Scheduled the task to clean up when shutdown is called */
3199 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3206 * The main function for the mesh service.
3208 * @param argc number of arguments from the command line
3209 * @param argv command line arguments
3210 * @return 0 ok, 1 on error
3213 main (int argc, char *const *argv)
3218 fprintf (stderr, "main ()\n");
3220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3223 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3227 fprintf (stderr, "main () END\n");