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 * Destroy the tunnel and free any allocated resources linked to it
1089 * @param t the tunnel to destroy
1091 * @return GNUNET_OK on success
1094 tunnel_destroy (struct MeshTunnel *t)
1096 struct MeshClient *c;
1097 struct MeshQueue *q;
1098 struct MeshQueue *qn;
1099 GNUNET_HashCode hash;
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1111 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1112 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1117 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1118 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1122 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1126 if (NULL != q->data)
1127 GNUNET_free (q->data);
1131 /* TODO cancel core transmit ready in case it was active */
1134 GNUNET_CONTAINER_multihashmap_iterate(t->tree->first_hops, &iterate_free, t);
1135 GNUNET_CONTAINER_multihashmap_destroy(t->tree->first_hops);
1136 tree_destroy(t->tree);
1143 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1144 * client when the client disconnects.
1146 * @param cls closure (client that is disconnecting)
1147 * @param key the hash of the local tunnel id (used to access the hashmap)
1148 * @param value the value stored at the key (tunnel to destroy)
1150 * @return GNUNET_OK on success
1153 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1157 r = tunnel_destroy ((struct MeshTunnel *) value);
1162 /******************************************************************************/
1163 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1164 /******************************************************************************/
1168 * Function called to notify a client about the socket
1169 * being ready to queue more data. "buf" will be
1170 * NULL and "size" zero if the socket was closed for
1171 * writing in the meantime.
1173 * @param cls closure
1174 * @param size number of bytes available in buf
1175 * @param buf where the callee should write the message
1176 * @return number of bytes written to buf
1179 send_core_create_path (void *cls, size_t size, void *buf)
1181 struct MeshPathInfo *info = cls;
1182 struct GNUNET_MESH_ManipulatePath *msg;
1183 struct GNUNET_PeerIdentity *peer_ptr;
1184 struct MeshPeerInfo *peer = info->peer;
1185 struct MeshTunnel *t = info->t;
1186 struct MeshPeerPath *p = info->path;
1191 sizeof (struct GNUNET_MESH_ManipulatePath) +
1192 p->length * sizeof (struct GNUNET_PeerIdentity);
1194 if (size < size_needed || NULL == buf)
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
1197 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1198 GNUNET_TIME_UNIT_FOREVER_REL,
1199 path_get_first_hop (t->tree, peer->id),
1200 size_needed, &send_core_create_path,
1205 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1206 msg->header.size = htons (size_needed);
1207 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1208 msg->tid = ntohl (t->id.tid);
1210 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1211 for (i = 0; i < p->length; i++)
1213 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1225 * Function called to notify a client about the socket
1226 * being ready to queue more data. "buf" will be
1227 * NULL and "size" zero if the socket was closed for
1228 * writing in the meantime.
1230 * @param cls closure (MeshDataDescriptor with all info to build packet)
1231 * @param size number of bytes available in buf
1232 * @param buf where the callee should write the message
1233 * @return number of bytes written to buf
1236 send_core_data_to_origin (void *cls, size_t size, void *buf)
1238 struct MeshDataDescriptor *info = cls;
1239 struct GNUNET_MESH_ToOrigin *msg = buf;
1242 GNUNET_assert (NULL != info);
1243 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1244 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1246 if (total_size > size)
1248 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1249 "not enough buffer to send data to origin\n");
1252 msg->header.size = htons (total_size);
1253 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1254 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1255 msg->tid = htonl (info->origin->tid);
1256 if (0 != info->size)
1258 memcpy (&msg[1], &info[1], info->size);
1260 if (NULL != info->client)
1262 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1270 * Function called to notify a client about the socket
1271 * being ready to queue more data. "buf" will be
1272 * NULL and "size" zero if the socket was closed for
1273 * writing in the meantime.
1275 * @param cls closure (data itself)
1276 * @param size number of bytes available in buf
1277 * @param buf where the callee should write the message
1278 * @return number of bytes written to buf
1281 send_core_data_unicast (void *cls, size_t size, void *buf)
1283 struct MeshDataDescriptor *info = cls;
1284 struct GNUNET_MESH_Unicast *msg = buf;
1287 GNUNET_assert (NULL != info);
1288 total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
1289 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1291 if (total_size > size)
1293 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1294 "not enough buffer to send data to peer\n");
1297 msg->header.size = htons (total_size);
1298 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1299 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1300 GNUNET_PEER_resolve (info->destination, &msg->destination);
1301 msg->tid = htonl (info->origin->tid);
1302 if (0 != info->size)
1304 memcpy (&msg[1], &info[1], info->size);
1306 if (NULL != info->client)
1308 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1316 * Function called to notify a client about the socket
1317 * being ready to queue more data. "buf" will be
1318 * NULL and "size" zero if the socket was closed for
1319 * writing in the meantime.
1321 * @param cls closure (data itself)
1322 * @param size number of bytes available in buf
1323 * @param buf where the callee should write the message
1324 * @return number of bytes written to buf
1327 send_core_data_multicast (void *cls, size_t size, void *buf)
1329 struct MeshDataDescriptor *info = cls;
1330 struct GNUNET_MESH_Multicast *msg = buf;
1333 GNUNET_assert (NULL != info);
1334 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1335 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1339 info->peer->core_transmit[info->handler_n] = NULL;
1341 if (total_size > size)
1343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1344 "not enough buffer to send data futher\n");
1347 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1348 msg->header.size = htons (total_size);
1349 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1350 msg->tid = htonl (info->origin->tid);
1351 memcpy (&msg[1], &info[1], total_size);
1352 if (0 == --info->copies)
1354 if (NULL != info->client)
1356 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1365 * Function called to notify a client about the socket
1366 * being ready to queue more data. "buf" will be
1367 * NULL and "size" zero if the socket was closed for
1368 * writing in the meantime.
1370 * @param cls closure (MeshDataDescriptor)
1371 * @param size number of bytes available in buf
1372 * @param buf where the callee should write the message
1373 * @return number of bytes written to buf
1376 send_core_path_ack (void *cls, size_t size, void *buf)
1378 struct MeshDataDescriptor *info = cls;
1379 struct GNUNET_MESH_PathACK *msg = buf;
1381 GNUNET_assert (NULL != info);
1384 info->peer->core_transmit[info->handler_n] = NULL;
1386 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1391 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1392 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1393 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1394 msg->tid = htonl (info->origin->tid);
1395 msg->peer_id = my_full_id;
1397 /* TODO add signature */
1399 return sizeof (struct GNUNET_MESH_PathACK);
1404 * Function called to notify a client about the socket
1405 * being ready to queue more data. "buf" will be
1406 * NULL and "size" zero if the socket was closed for
1407 * writing in the meantime.
1409 * @param cls closure (data itself)
1410 * @param size number of bytes available in buf
1411 * @param buf where the callee should write the message
1412 * @return number of bytes written to buf
1415 send_core_data_raw (void *cls, size_t size, void *buf)
1417 struct GNUNET_MessageHeader *msg = cls;
1420 GNUNET_assert (NULL != msg);
1421 total_size = ntohs (msg->size);
1423 if (total_size > size)
1428 memcpy (buf, msg, total_size);
1436 * Send another peer a notification to destroy a tunnel
1437 * @param cls The tunnel to destroy
1438 * @param size Size in the buffer
1439 * @param buf Memory where to put the data to transmit
1440 * @return Size of data put in buffer
1443 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1445 struct MeshTunnel *t = cls;
1446 struct MeshClient *c;
1447 struct GNUNET_MESH_TunnelMessage *msg;
1451 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1452 /*FIXME*/ msg->header.size =
1453 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1454 msg->tunnel_id = htonl (t->id.tid);
1456 tunnel_destroy (c, t);
1457 return sizeof (struct GNUNET_MESH_TunnelMessage);
1463 * Send the message to all clients that have subscribed to its type
1465 * @param msg Pointer to the message itself
1466 * @return number of clients this message was sent to
1469 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1471 struct MeshClient *c;
1475 type = ntohs (msg->type);
1476 for (count = 0, c = clients; c != NULL; c = c->next)
1478 if (client_is_subscribed (type, c))
1481 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1491 * Notify the client that owns the tunnel that a peer has connected to it
1493 * @param t Tunnel whose owner to notify
1494 * @param id Short id of the peer that has connected
1497 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1499 struct GNUNET_MESH_PeerControl pc;
1501 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1502 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1503 pc.tunnel_id = htonl (t->local_tid);
1504 GNUNET_PEER_resolve (id, &pc.peer);
1505 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1506 &pc.header, GNUNET_NO);
1510 /******************************************************************************/
1511 /******************** MESH NETWORK HANDLERS **************************/
1512 /******************************************************************************/
1516 * Core handler for path creation
1517 * struct GNUNET_CORE_MessageHandler
1519 * @param cls closure
1520 * @param message message
1521 * @param peer peer identity this notification is about
1522 * @param atsi performance data
1523 * @return GNUNET_OK to keep the connection open,
1524 * GNUNET_SYSERR to close it (signal serious error)
1528 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1529 const struct GNUNET_MessageHeader *message,
1530 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1532 unsigned int own_pos;
1535 MESH_TunnelNumber tid;
1536 struct GNUNET_MESH_ManipulatePath *msg;
1537 struct GNUNET_PeerIdentity *pi;
1538 struct GNUNET_PeerIdentity id;
1539 GNUNET_HashCode hash;
1540 struct MeshPeerPath *path;
1541 struct MeshPeerInfo *dest_peer_info;
1542 struct MeshPeerInfo *orig_peer_info;
1543 struct MeshTunnel *t;
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546 "MESH: Received a MESH path create msg\n");
1547 size = ntohs (message->size);
1548 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1550 GNUNET_break_op (0);
1554 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1555 if (size % sizeof (struct GNUNET_PeerIdentity))
1557 GNUNET_break_op (0);
1560 size /= sizeof (struct GNUNET_PeerIdentity);
1563 GNUNET_break_op (0);
1566 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1568 tid = ntohl (msg->tid);
1569 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1570 t = tunnel_get (pi, tid);
1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1575 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1576 t->id.oid = GNUNET_PEER_intern (pi);
1578 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1580 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1582 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1583 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1591 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1592 if (NULL == dest_peer_info)
1594 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1595 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1596 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1598 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1600 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1601 if (NULL == orig_peer_info)
1603 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1604 orig_peer_info->id = GNUNET_PEER_intern (pi);
1605 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1606 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1609 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
1610 path->length = size;
1611 path->peers = GNUNET_malloc (size * sizeof (GNUNET_PEER_Id));
1613 for (i = 0; i < size; i++)
1615 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1616 if (path->peers[i] == myid)
1620 { /* cannot be self, must be 'not found' */
1621 /* create path: self not found in path through self */
1622 GNUNET_break_op (0);
1623 path_destroy (path);
1624 /* FIXME error. destroy tunnel? leave for timeout? */
1627 if (own_pos == size - 1)
1629 /* It is for us! Send ack. */
1630 struct MeshDataDescriptor *info;
1633 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1634 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1635 info->origin = &t->id;
1636 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1637 GNUNET_assert (info->peer);
1638 for (j = 0; info->peer->core_transmit[j]; j++)
1646 info->handler_n = j;
1647 info->peer->core_transmit[j] =
1648 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1649 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1650 sizeof (struct GNUNET_MessageHeader),
1651 &send_core_path_ack, info);
1655 /* It's for somebody else! Retransmit. */
1656 struct MeshPathInfo *path_info;
1658 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1660 path_info->path = path;
1661 path_info->peer = dest_peer_info;
1663 path_add_to_peer (dest_peer_info, path);
1664 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1665 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1666 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1667 sizeof (struct GNUNET_MessageHeader),
1668 &send_core_create_path, path_info);
1675 * Core handler for mesh network traffic going from the origin to a peer
1677 * @param cls closure
1678 * @param peer peer identity this notification is about
1679 * @param message message
1680 * @param atsi performance data
1681 * @return GNUNET_OK to keep the connection open,
1682 * GNUNET_SYSERR to close it (signal serious error)
1685 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1686 const struct GNUNET_MessageHeader *message,
1687 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1689 struct GNUNET_MESH_Unicast *msg;
1690 struct MeshTunnel *t;
1691 struct MeshPeerInfo *pi;
1694 size = ntohs (message->size);
1696 sizeof (struct GNUNET_MESH_Unicast) +
1697 sizeof (struct GNUNET_MessageHeader))
1702 msg = (struct GNUNET_MESH_Unicast *) message;
1703 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1706 /* TODO notify back: we don't know this tunnel */
1709 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1710 &msg->destination.hashPubKey);
1713 /* TODO maybe feedback, log to statistics */
1718 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1721 msg = GNUNET_malloc (size);
1722 memcpy (msg, message, size);
1723 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1724 GNUNET_TIME_UNIT_FOREVER_REL,
1725 path_get_first_hop (t->tree, pi->id),
1727 &send_core_data_raw, msg);
1733 * Core handler for mesh network traffic going from the origin to all peers
1735 * @param cls closure
1736 * @param message message
1737 * @param peer peer identity this notification is about
1738 * @param atsi performance data
1739 * @return GNUNET_OK to keep the connection open,
1740 * GNUNET_SYSERR to close it (signal serious error)
1743 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1744 const struct GNUNET_MessageHeader *message,
1745 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1747 struct GNUNET_MESH_Multicast *msg;
1748 struct GNUNET_PeerIdentity id;
1749 struct MeshDataDescriptor *info;
1750 struct MeshPathInfo neighbors;
1751 struct MeshTunnel *t;
1757 size = ntohs (message->size);
1759 sizeof (struct GNUNET_MESH_Multicast) +
1760 sizeof (struct GNUNET_MessageHeader))
1762 GNUNET_break_op (0);
1765 msg = (struct GNUNET_MESH_Multicast *) message;
1766 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1770 /* TODO notify that we dont know that tunnel */
1774 /* Transmit to locally interested clients */
1775 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1777 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1780 /* Retransmit to other peers.
1781 * Using path here as just a collection of peers, not a path per se.
1784 neighbors.path = GNUNET_malloc (sizeof (struct MeshPeerPath));
1785 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &iterate_collect_neighbors,
1787 if (0 == neighbors.path->length)
1789 GNUNET_free (neighbors.path);
1792 size -= sizeof (struct GNUNET_MESH_Multicast);
1793 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + size);
1794 info->origin = &t->id;
1795 info->copies = neighbors.path->length;
1796 for (i = 0; i < info->copies; i++)
1798 GNUNET_PEER_resolve (neighbors.path->peers[i], &id);
1799 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1800 GNUNET_assert (NULL != info->peer);
1801 for (j = 0; 0 != info->peer->core_transmit[j]; j++)
1803 if (j == (CORE_QUEUE_SIZE - 1))
1809 info->handler_n = j;
1810 info->peer->infos[j] = info;
1811 info->peer->core_transmit[j] =
1812 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1813 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1814 ntohs (msg->header.size),
1815 &send_core_data_multicast, info);
1817 GNUNET_free (neighbors.path->peers);
1818 GNUNET_free (neighbors.path);
1824 * Core handler for mesh network traffic
1826 * @param cls closure
1827 * @param message message
1828 * @param peer peer identity this notification is about
1829 * @param atsi performance data
1831 * @return GNUNET_OK to keep the connection open,
1832 * GNUNET_SYSERR to close it (signal serious error)
1835 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1836 const struct GNUNET_MessageHeader *message,
1837 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1839 struct GNUNET_MESH_ToOrigin *msg;
1840 struct GNUNET_PeerIdentity id;
1841 struct MeshPeerInfo *peer_info;
1842 struct MeshTunnel *t;
1845 size = ntohs (message->size);
1846 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1847 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1849 GNUNET_break_op (0);
1852 msg = (struct GNUNET_MESH_ToOrigin *) message;
1853 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1857 /* TODO notify that we dont know this tunnel (whom)? */
1861 if (t->id.oid == myid)
1863 if (NULL == t->client)
1865 /* got data packet for ownerless tunnel */
1866 GNUNET_break_op (0);
1869 /* TODO signature verification */
1870 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1874 peer_info = peer_info_get (&msg->oid);
1875 if (NULL == peer_info)
1877 /* unknown origin of tunnel */
1881 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1882 msg = GNUNET_malloc (size);
1883 memcpy (msg, message, size);
1884 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1885 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1886 &send_core_data_raw, msg);
1893 * Core handler for path ACKs
1895 * @param cls closure
1896 * @param message message
1897 * @param peer peer identity this notification is about
1898 * @param atsi performance data
1900 * @return GNUNET_OK to keep the connection open,
1901 * GNUNET_SYSERR to close it (signal serious error)
1903 * FIXME path change state
1906 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1907 const struct GNUNET_MessageHeader *message,
1908 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1910 struct GNUNET_MESH_PathACK *msg;
1911 struct MeshTunnel *t;
1912 struct MeshPeerInfo *peer_info;
1914 msg = (struct GNUNET_MESH_PathACK *) message;
1915 t = tunnel_get (&msg->oid, msg->tid);
1918 /* TODO notify that we don't know the tunnel */
1922 /* Message for us? */
1923 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1926 if (NULL == t->client)
1931 peer_info = peer_info_get (&msg->peer_id);
1932 if (NULL == peer_info)
1934 GNUNET_break_op (0);
1937 /* FIXME change state of peer */
1938 send_client_peer_connected(t, peer_info->id);
1942 peer_info = peer_info_get (&msg->oid);
1943 if (NULL == peer_info)
1945 /* If we know the tunnel, we should DEFINITELY know the peer */
1949 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1950 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1951 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1952 GNUNET_TIME_UNIT_FOREVER_REL,
1953 path_get_first_hop (t->tree,
1955 sizeof (struct GNUNET_MESH_PathACK),
1956 &send_core_data_raw, msg);
1962 * Functions to handle messages from core
1964 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1965 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1966 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1967 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1968 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1969 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1970 sizeof (struct GNUNET_MESH_PathACK)},
1976 /******************************************************************************/
1977 /**************** MESH LOCAL HANDLER HELPERS ***********************/
1978 /******************************************************************************/
1981 * deregister_app: iterator for removing each application registered by a client
1983 * @param cls closure
1984 * @param key the hash of the application id (used to access the hashmap)
1985 * @param value the value stored at the key (client)
1987 * @return GNUNET_OK on success
1990 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
1992 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
1998 * notify_client_connection_failure: notify a client that the connection to the
1999 * requested remote peer is not possible (for instance, no route found)
2000 * Function called when the socket is ready to queue more data. "buf" will be
2001 * NULL and "size" zero if the socket was closed for writing in the meantime.
2003 * @param cls closure
2004 * @param size number of bytes available in buf
2005 * @param buf where the callee should write the message
2006 * @return number of bytes written to buf
2009 notify_client_connection_failure (void *cls, size_t size, void *buf)
2012 struct MeshPeerInfo *peer_info;
2013 struct GNUNET_MESH_PeerControl *msg;
2014 struct GNUNET_PeerIdentity id;
2016 if (0 == size && NULL == buf)
2018 // TODO retry? cancel?
2022 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2023 peer_info = (struct MeshPeerInfo *) cls;
2024 msg = (struct GNUNET_MESH_PeerControl *) buf;
2025 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2026 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2027 // msg->tunnel_id = htonl(peer_info->t->tid);
2028 GNUNET_PEER_resolve (peer_info->id, &id);
2029 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2037 * Function to process paths received for a new peer addition. The recorded
2038 * paths form the initial tunnel, which can be optimized later.
2039 * Called on each result obtained for the DHT search.
2041 * @param cls closure
2042 * @param exp when will this value expire
2043 * @param key key of the result
2044 * @param get_path NULL-terminated array of pointers
2045 * to the peers on reverse GET path (or NULL if not recorded)
2046 * @param put_path NULL-terminated array of pointers
2047 * to the peers on the PUT path (or NULL if not recorded)
2048 * @param type type of the result
2049 * @param size number of bytes in data
2050 * @param data pointer to the result data
2055 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2056 const GNUNET_HashCode * key,
2057 const struct GNUNET_PeerIdentity *const *get_path,
2058 const struct GNUNET_PeerIdentity *const *put_path,
2059 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2061 struct MeshPathInfo *path_info = cls;
2062 struct MeshPeerPath *p;
2063 struct GNUNET_PeerIdentity pi;
2066 if (NULL == get_path || NULL == put_path)
2068 if (NULL == path_info->peer->path_head)
2070 // Find ourselves some alternate initial path to the destination: retry
2071 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2072 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2073 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2074 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2075 GNUNET_BLOCK_TYPE_TEST, /* type */
2076 &pi.hashPubKey, /*key to search */
2077 4, /* replication level */
2078 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2081 0, /* xquery bits */
2083 (void *) path_info);
2088 p = path_build_from_dht (get_path, put_path);
2089 path_add_to_peer (path_info->peer, p);
2090 for (i = 0; i < path_info->peer->ntunnels; i++)
2092 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2094 GNUNET_free (path_info);
2101 * Function to process paths received for a new peer addition. The recorded
2102 * paths form the initial tunnel, which can be optimized later.
2103 * Called on each result obtained for the DHT search.
2105 * @param cls closure
2106 * @param exp when will this value expire
2107 * @param key key of the result
2108 * @param get_path NULL-terminated array of pointers
2109 * to the peers on reverse GET path (or NULL if not recorded)
2110 * @param put_path NULL-terminated array of pointers
2111 * to the peers on the PUT path (or NULL if not recorded)
2112 * @param type type of the result
2113 * @param size number of bytes in data
2114 * @param data pointer to the result data
2117 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2118 const GNUNET_HashCode * key,
2119 const struct GNUNET_PeerIdentity *const *get_path,
2120 const struct GNUNET_PeerIdentity *const *put_path,
2121 enum GNUNET_BLOCK_Type type, size_t size,
2124 const struct GNUNET_PeerIdentity *pi = data;
2125 struct GNUNET_PeerIdentity id;
2126 struct MeshTunnel *t = cls;
2127 struct MeshPeerInfo *peer_info;
2128 struct MeshPathInfo *path_info;
2129 struct MeshPeerPath *p;
2132 if (size != sizeof (struct GNUNET_PeerIdentity))
2134 GNUNET_break_op (0);
2137 GNUNET_assert (NULL != t->client);
2138 GNUNET_DHT_get_stop (t->client->dht_get_type);
2139 t->client->dht_get_type = NULL;
2140 peer_info = peer_info_get (pi);
2141 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2142 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2144 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2145 NULL == peer_info->dhtget)
2147 /* we don't have a route to the peer, let's try a direct lookup */
2148 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2150 GNUNET_TIME_UNIT_FOREVER_REL,
2152 GNUNET_BLOCK_TYPE_TEST,
2155 /* key to look up */
2157 /* replication level */
2158 GNUNET_DHT_RO_RECORD_ROUTE,
2159 /* option to dht: record route */
2160 NULL, /* bloom filter */
2163 0, /* xquery bits */
2166 peer_info); /* closure */
2169 p = path_build_from_dht (get_path, put_path);
2170 path_add_to_peer (peer_info, p);
2171 tunnel_add_peer(t, peer_info);
2172 p = tree_get_path_to_peer(t->tree, peer_info->id);
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2175 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2176 t->local_tid, p->length);
2177 for (i = 0; i < p->length; i++)
2179 GNUNET_PEER_resolve (p->peers[0], &id);
2180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2181 GNUNET_h2s_full (&id.hashPubKey));
2187 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2189 path_info->peer = peer_info;
2190 path_info->path = p;
2191 GNUNET_PEER_resolve (p->peers[1], &id);
2192 GNUNET_CORE_notify_transmit_ready (core_handle,
2198 GNUNET_TIME_UNIT_FOREVER_REL,
2202 sizeof (struct GNUNET_MESH_ManipulatePath)
2205 sizeof (struct GNUNET_PeerIdentity)),
2207 &send_core_create_path,
2209 path_info); /* cls */
2213 send_client_peer_connected(t, myid);
2216 /******************************************************************************/
2217 /********************* MESH LOCAL HANDLES **************************/
2218 /******************************************************************************/
2222 * Handler for client disconnection
2224 * @param cls closure
2225 * @param client identification of the client; NULL
2226 * for the last call when the server is destroyed
2229 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2231 struct MeshClient *c;
2232 struct MeshClient *next;
2234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2240 if (c->handle != client && NULL != client)
2242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2247 if (NULL != c->tunnels)
2249 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2250 &tunnel_destroy_iterator,
2252 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2255 /* deregister clients applications */
2256 if (NULL != c->apps)
2258 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2259 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2261 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2262 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2264 GNUNET_SCHEDULER_cancel (announce_applications_task);
2265 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2267 if (NULL != c->types)
2268 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2269 if (NULL != c->dht_get_type)
2270 GNUNET_DHT_get_stop (c->dht_get_type);
2271 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2283 * Handler for new clients
2285 * @param cls closure
2286 * @param client identification of the client
2287 * @param message the actual message, which includes messages the client wants
2290 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2291 const struct GNUNET_MessageHeader *message)
2293 struct GNUNET_MESH_ClientConnect *cc_msg;
2294 struct MeshClient *c;
2295 GNUNET_MESH_ApplicationType *a;
2302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2303 /* Check data sanity */
2304 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2305 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2306 ntypes = ntohs (cc_msg->types);
2307 napps = ntohs (cc_msg->applications);
2309 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2312 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2316 /* Create new client structure */
2317 c = GNUNET_malloc (sizeof (struct MeshClient));
2319 c->id = next_client_id++;
2322 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2325 GNUNET_MESH_ApplicationType at;
2328 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2329 for (i = 0; i < napps; i++)
2332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2333 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2334 /* store in clients hashmap */
2335 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2336 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2337 /* store in global hashmap, for announcements */
2338 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2339 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2341 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2342 announce_applications_task =
2343 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2351 t = (uint16_t *) & a[napps];
2352 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2353 for (i = 0; i < ntypes; i++)
2356 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2358 /* store in clients hashmap */
2359 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2360 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2361 /* store in global hashmap */
2362 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2363 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2367 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2369 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2370 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2371 GNUNET_SERVER_notification_context_add (nc, client);
2373 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2381 * Handler for requests of new tunnels
2383 * @param cls closure
2384 * @param client identification of the client
2385 * @param message the actual message
2388 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2389 const struct GNUNET_MessageHeader *message)
2391 struct GNUNET_MESH_TunnelMessage *t_msg;
2392 struct MeshTunnel *t;
2393 struct MeshClient *c;
2394 GNUNET_HashCode hash;
2396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2398 /* Sanity check for client registration */
2399 if (NULL == (c = client_get (client)))
2402 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2409 /* Message sanity check */
2410 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2413 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2417 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2418 /* Sanity check for tunnel numbering */
2419 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2422 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2425 /* Sanity check for duplicate tunnel IDs */
2426 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2433 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2435 while (NULL != tunnel_get_by_pi (myid, next_tid))
2436 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2437 t->id.tid = next_tid++;
2439 t->local_tid = ntohl (t_msg->tunnel_id);
2441 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2443 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2445 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2446 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2449 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2453 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2455 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2456 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2462 t->tree = GNUNET_malloc (sizeof(struct MeshTunnelTree));
2463 t->tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
2465 t->tree->refresh = REFRESH_PATH_TIME;
2466 t->tree->root = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
2467 t->tree->root->status = MESH_PEER_READY;
2468 t->tree->root->t = t;
2469 t->tree->root->peer = myid;
2470 t->tree->me = t->tree->root;
2472 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2478 * Handler for requests of deleting tunnels
2480 * @param cls closure
2481 * @param client identification of the client
2482 * @param message the actual message
2485 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2486 const struct GNUNET_MessageHeader *message)
2488 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2489 struct MeshClient *c;
2490 struct MeshTunnel *t;
2491 MESH_TunnelNumber tid;
2492 GNUNET_HashCode hash;
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2496 /* Sanity check for client registration */
2497 if (NULL == (c = client_get (client)))
2500 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2503 /* Message sanity check */
2504 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2507 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2513 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2515 /* Retrieve tunnel */
2516 tid = ntohl (tunnel_msg->tunnel_id);
2518 /* Remove from local id hashmap */
2519 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2520 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2521 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2523 /* Remove from global id hashmap */
2524 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2525 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2527 // notify_tunnel_destroy(t); FIXME
2529 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2535 * Handler for connection requests to new peers
2537 * @param cls closure
2538 * @param client identification of the client
2539 * @param message the actual message (PeerControl)
2544 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2545 const struct GNUNET_MessageHeader *message)
2547 struct GNUNET_MESH_PeerControl *peer_msg;
2548 struct MeshClient *c;
2549 struct MeshTunnel *t;
2550 MESH_TunnelNumber tid;
2551 struct MeshPeerInfo *peer_info;
2554 /* Sanity check for client registration */
2555 if (NULL == (c = client_get (client)))
2558 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2562 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2563 /* Sanity check for message size */
2564 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2567 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2571 /* Tunnel exists? */
2572 tid = ntohl (peer_msg->tunnel_id);
2573 t = tunnel_get_by_local_id (c, tid);
2577 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2581 /* Does client own tunnel? */
2582 if (t->client->handle != client)
2585 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2590 peer_info = peer_info_get (&peer_msg->peer);
2592 /* Start DHT search if needed FIXME: if not already connected */
2593 if (NULL == peer_info->dhtget)
2595 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 */
2596 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2599 0, /* xquery bits */
2601 (void *) peer_info);
2604 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2610 * Handler for disconnection requests of peers in a tunnel
2612 * @param cls closure
2613 * @param client identification of the client
2614 * @param message the actual message (PeerControl)
2617 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2618 const struct GNUNET_MessageHeader *message)
2620 struct GNUNET_MESH_PeerControl *peer_msg;
2621 struct MeshClient *c;
2622 struct MeshTunnel *t;
2623 MESH_TunnelNumber tid;
2625 /* Sanity check for client registration */
2626 if (NULL == (c = client_get (client)))
2629 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2632 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2633 /* Sanity check for message size */
2634 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2637 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2641 /* Tunnel exists? */
2642 tid = ntohl (peer_msg->tunnel_id);
2643 t = tunnel_get_by_local_id (c, tid);
2647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2651 /* Does client own tunnel? */
2652 if (t->client->handle != client)
2655 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2659 /* Ok, delete peer from tunnel */
2660 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2661 &peer_msg->peer.hashPubKey);
2663 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2669 * Handler for connection requests to new peers by type
2671 * @param cls closure
2672 * @param client identification of the client
2673 * @param message the actual message (ConnectPeerByType)
2676 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2677 const struct GNUNET_MessageHeader *message)
2679 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2680 struct MeshClient *c;
2681 struct MeshTunnel *t;
2682 GNUNET_HashCode hash;
2683 GNUNET_MESH_ApplicationType type;
2684 MESH_TunnelNumber tid;
2686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2687 /* Sanity check for client registration */
2688 if (NULL == (c = client_get (client)))
2691 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2695 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2696 /* Sanity check for message size */
2697 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2698 ntohs (connect_msg->header.size))
2701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2705 /* Tunnel exists? */
2706 tid = ntohl (connect_msg->tunnel_id);
2707 t = tunnel_get_by_local_id (c, tid);
2711 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2715 /* Does client own tunnel? */
2716 if (t->client->handle != client)
2719 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2723 /* Do WE have the service? */
2724 type = ntohl (connect_msg->type);
2725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2726 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2727 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2730 /* Yes! Fast forward, add ourselves to the tunnel and send the
2731 * good news to the client
2733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2734 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2735 peer_info_get (&my_full_id),
2736 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2739 send_client_peer_connected(t, myid);
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2741 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2744 /* Ok, lets find a peer offering the service */
2745 if (c->dht_get_type)
2747 GNUNET_DHT_get_stop (c->dht_get_type);
2749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2750 GNUNET_h2s_full (&hash));
2752 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2753 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2754 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
2755 &dht_get_type_handler, t);
2757 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2763 * Handler for client traffic directed to one peer
2765 * @param cls closure
2766 * @param client identification of the client
2767 * @param message the actual message
2770 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2771 const struct GNUNET_MessageHeader *message)
2773 struct MeshClient *c;
2774 struct MeshTunnel *t;
2775 struct MeshPeerInfo *pi;
2776 struct GNUNET_MESH_Unicast *data_msg;
2777 struct MeshDataDescriptor *info;
2778 MESH_TunnelNumber tid;
2781 /* Sanity check for client registration */
2782 if (NULL == (c = client_get (client)))
2785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2788 data_msg = (struct GNUNET_MESH_Unicast *) message;
2789 /* Sanity check for message size */
2790 if (sizeof (struct GNUNET_MESH_Unicast) +
2791 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
2794 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2798 /* Tunnel exists? */
2799 tid = ntohl (data_msg->tid);
2800 t = tunnel_get_by_local_id (c, tid);
2804 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2808 /* Is it a local tunnel? Then, does client own the tunnel? */
2809 if (t->client->handle != NULL && t->client->handle != client)
2812 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2816 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2817 &data_msg->destination.hashPubKey);
2818 /* Is the selected peer in the tunnel? */
2822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2827 struct GNUNET_MESH_Unicast copy;
2829 /* Work around const limitation */
2830 memcpy (©, data_msg, sizeof (struct GNUNET_MESH_Unicast));
2831 copy.oid = my_full_id;
2832 copy.tid = htonl (t->id.tid);
2833 handle_mesh_data_unicast (NULL, &my_full_id, ©.header, NULL);
2836 data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
2837 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
2838 memcpy (&info[1], &data_msg[1], data_size);
2839 info->destination = pi->id;
2840 info->origin = &t->id;
2841 info->size = data_size;
2842 info->client = client;
2843 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2844 GNUNET_TIME_UNIT_FOREVER_REL,
2845 path_get_first_hop (t->tree, pi->id),
2846 /* FIXME re-check types */
2848 sizeof (struct GNUNET_MESH_Unicast),
2849 &send_core_data_unicast, info);
2854 * Handler for client traffic directed to all peers in a tunnel
2856 * @param cls closure
2857 * @param client identification of the client
2858 * @param message the actual message
2861 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2862 const struct GNUNET_MessageHeader *message)
2864 struct MeshClient *c;
2865 struct MeshTunnel *t;
2866 struct GNUNET_MESH_Multicast *data_msg;
2867 MESH_TunnelNumber tid;
2869 /* Sanity check for client registration */
2870 if (NULL == (c = client_get (client)))
2873 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2876 data_msg = (struct GNUNET_MESH_Multicast *) message;
2877 /* Sanity check for message size */
2878 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2881 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2885 /* Tunnel exists? */
2886 tid = ntohl (data_msg->tid);
2887 t = tunnel_get_by_local_id (c, tid);
2891 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2895 /* Does client own tunnel? */
2896 if (t->client->handle != client)
2899 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2905 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2910 * Functions to handle messages from clients
2912 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
2913 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2914 {&handle_local_tunnel_create, NULL,
2915 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2916 sizeof (struct GNUNET_MESH_TunnelMessage)},
2917 {&handle_local_tunnel_destroy, NULL,
2918 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2919 sizeof (struct GNUNET_MESH_TunnelMessage)},
2920 {&handle_local_connect_add, NULL,
2921 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
2922 sizeof (struct GNUNET_MESH_PeerControl)},
2923 {&handle_local_connect_del, NULL,
2924 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
2925 sizeof (struct GNUNET_MESH_PeerControl)},
2926 {&handle_local_connect_by_type, NULL,
2927 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
2928 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
2929 {&handle_local_unicast, NULL,
2930 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2931 {&handle_local_unicast, NULL,
2932 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2933 {&handle_local_multicast, NULL,
2934 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2940 * To be called on core init/fail.
2942 * @param cls service closure
2943 * @param server handle to the server for this service
2944 * @param identity the public identity of this peer
2945 * @param publicKey the public key of this peer
2948 core_init (void *cls, struct GNUNET_CORE_Handle *server,
2949 const struct GNUNET_PeerIdentity *identity,
2950 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
2953 core_handle = server;
2954 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
2956 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
2957 GNUNET_SCHEDULER_shutdown();
2963 * Method called whenever a given peer connects.
2965 * @param cls closure
2966 * @param peer peer identity this notification is about
2967 * @param atsi performance data for the connection
2970 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
2971 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2973 // GNUNET_PEER_Id pid;
2974 struct MeshPeerInfo *peer_info;
2975 struct MeshPeerPath *path;
2977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
2978 peer_info = peer_info_get (peer);
2979 if (myid == peer_info->id)
2981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
2984 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
2986 path->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id) * 2);
2987 path->peers[0] = myid;
2988 path->peers[1] = peer_info->id;
2989 path_add_to_peer (peer_info, path);
2994 * Method called whenever a peer disconnects.
2996 * @param cls closure
2997 * @param peer peer identity this notification is about
3000 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3002 struct MeshPeerInfo *pi;
3005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3006 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3012 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3014 if (pi->core_transmit[i])
3016 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3017 /* TODO: notify that tranmission has failed */
3018 GNUNET_free (pi->infos[i]);
3021 path_remove_from_peer (pi, pi->id, myid);
3024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3030 /******************************************************************************/
3031 /************************ MAIN FUNCTIONS ****************************/
3032 /******************************************************************************/
3035 * Task run during shutdown.
3041 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3043 struct MeshClient *c;
3045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3046 if (core_handle != NULL)
3048 GNUNET_CORE_disconnect (core_handle);
3051 if (dht_handle != NULL)
3053 for (c = clients; NULL != c; c = c->next)
3054 if (NULL != c->dht_get_type)
3055 GNUNET_DHT_get_stop (c->dht_get_type);
3056 GNUNET_DHT_disconnect (dht_handle);
3061 GNUNET_SERVER_notification_context_destroy (nc);
3064 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3066 GNUNET_SCHEDULER_cancel (announce_id_task);
3067 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3073 * Process mesh requests.
3075 * @param cls closure
3076 * @param server the initialized server
3077 * @param c configuration to use
3080 run (void *cls, struct GNUNET_SERVER_Handle *server,
3081 const struct GNUNET_CONFIGURATION_Handle *c)
3083 struct MeshPeerInfo *peer;
3084 struct MeshPeerPath *p;
3087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3088 server_handle = server;
3089 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3090 CORE_QUEUE_SIZE, /* queue size */
3091 NULL, /* Closure passed to MESH functions */
3092 &core_init, /* Call core_init once connected */
3093 &core_connect, /* Handle connects */
3094 &core_disconnect, /* remove peers on disconnects */
3095 NULL, /* Do we care about "status" updates? */
3096 NULL, /* Don't notify about all incoming messages */
3097 GNUNET_NO, /* For header only in notification */
3098 NULL, /* Don't notify about all outbound messages */
3099 GNUNET_NO, /* For header-only out notification */
3100 core_handlers); /* Register these handlers */
3101 if (core_handle == NULL)
3104 GNUNET_SCHEDULER_shutdown ();
3109 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3114 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3115 GNUNET_SCHEDULER_shutdown ();
3118 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3119 GNUNET_free (keyfile);
3120 if (my_private_key == NULL)
3122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3123 _("Mesh service could not access hostkey. Exiting.\n"));
3124 GNUNET_SCHEDULER_shutdown ();
3127 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3128 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3129 &my_full_id.hashPubKey);
3130 myid = GNUNET_PEER_intern (&my_full_id);
3132 dht_handle = GNUNET_DHT_connect (c, 64);
3133 if (dht_handle == NULL)
3135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3136 Running without DHT has a severe\
3137 impact in MESH capabilities.\n\
3138 Plase check your configuretion and enable DHT.\n");
3144 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3145 peers = GNUNET_CONTAINER_multihashmap_create (32);
3146 applications = GNUNET_CONTAINER_multihashmap_create (32);
3147 types = GNUNET_CONTAINER_multihashmap_create (32);
3149 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3150 nc = GNUNET_SERVER_notification_context_create (server_handle,
3152 GNUNET_SERVER_disconnect_notify (server_handle,
3153 &handle_local_client_disconnect,
3158 clients_tail = NULL;
3163 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3164 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3166 /* Create a peer_info for the local peer */
3167 peer = peer_info_get(&my_full_id);
3168 p = GNUNET_malloc (sizeof (struct MeshPeerPath));
3169 p->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id));
3172 path_add_to_peer(peer, p);
3174 /* Scheduled the task to clean up when shutdown is called */
3175 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3182 * The main function for the mesh service.
3184 * @param argc number of arguments from the command line
3185 * @param argv command line arguments
3186 * @return 0 ok, 1 on error
3189 main (int argc, char *const *argv)
3194 fprintf (stderr, "main ()\n");
3196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3199 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3203 fprintf (stderr, "main () END\n");