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 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1014 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1015 * needed rescources.
1017 * @param t Tunnel we want to add a new peer to
1018 * @param peer PeerInfo of the peer being added
1022 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1024 struct MeshPeerPath *p;
1025 struct MeshPeerPath *best_p;
1026 unsigned int best_cost;
1029 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1030 if (NULL == (p = peer->path_head))
1034 best_cost = UINT_MAX;
1037 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1044 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1045 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1046 t->path_refresh_task =
1047 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1052 * Notify a tunnel that a connection has broken that affects at least
1053 * some of its peers.
1055 * @param t Tunnel affected
1056 * @param peer Peer that (at least) has been affected by the disconnection
1057 * @param p1 Peer that got disconnected from p2
1058 * @param p2 Peer that got disconnected from p1
1063 tunnel_notify_connection_broken (struct MeshTunnel *t,
1064 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1071 * Recursively destory the path tree of a tunnel.
1072 * Note: it does not liberate memory for itself, parent must do it!
1074 * @param n The node to destroy, along with children.
1076 * @return GNUNET_OK on success
1079 tunnel_destroy_tree_node (struct MeshTunnelTreeNode *n)
1083 for (i = 0; i < n->nchildren; i++)
1085 tunnel_destroy_tree_node(&n->children[i]);
1087 if (NULL != n->children)
1088 GNUNET_free (n->children);
1093 * Destroy the tunnel and free any allocated resources linked to it
1095 * @param t the tunnel to destroy
1097 * @return GNUNET_OK on success
1100 tunnel_destroy (struct MeshTunnel *t)
1102 struct MeshClient *c;
1103 struct MeshQueue *q;
1104 struct MeshQueue *qn;
1105 GNUNET_HashCode hash;
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1117 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1118 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1123 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1124 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1128 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1132 if (NULL != q->data)
1133 GNUNET_free (q->data);
1137 /* TODO cancel core transmit ready in case it was active */
1140 GNUNET_CONTAINER_multihashmap_iterate(t->tree->first_hops, &iterate_free, t);
1141 GNUNET_CONTAINER_multihashmap_destroy(t->tree->first_hops);
1142 tunnel_destroy_tree_node(t->tree->root);
1143 GNUNET_free(t->tree->root);
1144 GNUNET_free (t->tree);
1151 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1152 * client when the client disconnects.
1154 * @param cls closure (client that is disconnecting)
1155 * @param key the hash of the local tunnel id (used to access the hashmap)
1156 * @param value the value stored at the key (tunnel to destroy)
1158 * @return GNUNET_OK on success
1161 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1165 r = tunnel_destroy ((struct MeshTunnel *) value);
1170 /******************************************************************************/
1171 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1172 /******************************************************************************/
1176 * Function called to notify a client about the socket
1177 * being ready to queue more data. "buf" will be
1178 * NULL and "size" zero if the socket was closed for
1179 * writing in the meantime.
1181 * @param cls closure
1182 * @param size number of bytes available in buf
1183 * @param buf where the callee should write the message
1184 * @return number of bytes written to buf
1187 send_core_create_path (void *cls, size_t size, void *buf)
1189 struct MeshPathInfo *info = cls;
1190 struct GNUNET_MESH_ManipulatePath *msg;
1191 struct GNUNET_PeerIdentity *peer_ptr;
1192 struct MeshPeerInfo *peer = info->peer;
1193 struct MeshTunnel *t = info->t;
1194 struct MeshPeerPath *p = info->path;
1199 sizeof (struct GNUNET_MESH_ManipulatePath) +
1200 p->length * sizeof (struct GNUNET_PeerIdentity);
1202 if (size < size_needed || NULL == buf)
1204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
1205 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1206 GNUNET_TIME_UNIT_FOREVER_REL,
1207 path_get_first_hop (t->tree, peer->id),
1208 size_needed, &send_core_create_path,
1213 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1214 msg->header.size = htons (size_needed);
1215 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1216 msg->tid = ntohl (t->id.tid);
1218 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1219 for (i = 0; i < p->length; i++)
1221 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1233 * Function called to notify a client about the socket
1234 * being ready to queue more data. "buf" will be
1235 * NULL and "size" zero if the socket was closed for
1236 * writing in the meantime.
1238 * @param cls closure (MeshDataDescriptor with all info to build packet)
1239 * @param size number of bytes available in buf
1240 * @param buf where the callee should write the message
1241 * @return number of bytes written to buf
1244 send_core_data_to_origin (void *cls, size_t size, void *buf)
1246 struct MeshDataDescriptor *info = cls;
1247 struct GNUNET_MESH_ToOrigin *msg = buf;
1250 GNUNET_assert (NULL != info);
1251 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1252 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1254 if (total_size > size)
1256 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1257 "not enough buffer to send data to origin\n");
1260 msg->header.size = htons (total_size);
1261 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1262 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1263 msg->tid = htonl (info->origin->tid);
1264 if (0 != info->size)
1266 memcpy (&msg[1], &info[1], info->size);
1268 if (NULL != info->client)
1270 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1278 * Function called to notify a client about the socket
1279 * being ready to queue more data. "buf" will be
1280 * NULL and "size" zero if the socket was closed for
1281 * writing in the meantime.
1283 * @param cls closure (data itself)
1284 * @param size number of bytes available in buf
1285 * @param buf where the callee should write the message
1286 * @return number of bytes written to buf
1289 send_core_data_unicast (void *cls, size_t size, void *buf)
1291 struct MeshDataDescriptor *info = cls;
1292 struct GNUNET_MESH_Unicast *msg = buf;
1295 GNUNET_assert (NULL != info);
1296 total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
1297 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1299 if (total_size > size)
1301 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1302 "not enough buffer to send data to peer\n");
1305 msg->header.size = htons (total_size);
1306 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1307 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1308 GNUNET_PEER_resolve (info->destination, &msg->destination);
1309 msg->tid = htonl (info->origin->tid);
1310 if (0 != info->size)
1312 memcpy (&msg[1], &info[1], info->size);
1314 if (NULL != info->client)
1316 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1324 * Function called to notify a client about the socket
1325 * being ready to queue more data. "buf" will be
1326 * NULL and "size" zero if the socket was closed for
1327 * writing in the meantime.
1329 * @param cls closure (data itself)
1330 * @param size number of bytes available in buf
1331 * @param buf where the callee should write the message
1332 * @return number of bytes written to buf
1335 send_core_data_multicast (void *cls, size_t size, void *buf)
1337 struct MeshDataDescriptor *info = cls;
1338 struct GNUNET_MESH_Multicast *msg = buf;
1341 GNUNET_assert (NULL != info);
1342 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1343 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1347 info->peer->core_transmit[info->handler_n] = NULL;
1349 if (total_size > size)
1351 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1352 "not enough buffer to send data futher\n");
1355 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1356 msg->header.size = htons (total_size);
1357 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1358 msg->tid = htonl (info->origin->tid);
1359 memcpy (&msg[1], &info[1], total_size);
1360 if (0 == --info->copies)
1362 if (NULL != info->client)
1364 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1373 * Function called to notify a client about the socket
1374 * being ready to queue more data. "buf" will be
1375 * NULL and "size" zero if the socket was closed for
1376 * writing in the meantime.
1378 * @param cls closure (MeshDataDescriptor)
1379 * @param size number of bytes available in buf
1380 * @param buf where the callee should write the message
1381 * @return number of bytes written to buf
1384 send_core_path_ack (void *cls, size_t size, void *buf)
1386 struct MeshDataDescriptor *info = cls;
1387 struct GNUNET_MESH_PathACK *msg = buf;
1389 GNUNET_assert (NULL != info);
1392 info->peer->core_transmit[info->handler_n] = NULL;
1394 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1399 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1400 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1401 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1402 msg->tid = htonl (info->origin->tid);
1403 msg->peer_id = my_full_id;
1405 /* TODO add signature */
1407 return sizeof (struct GNUNET_MESH_PathACK);
1412 * Function called to notify a client about the socket
1413 * being ready to queue more data. "buf" will be
1414 * NULL and "size" zero if the socket was closed for
1415 * writing in the meantime.
1417 * @param cls closure (data itself)
1418 * @param size number of bytes available in buf
1419 * @param buf where the callee should write the message
1420 * @return number of bytes written to buf
1423 send_core_data_raw (void *cls, size_t size, void *buf)
1425 struct GNUNET_MessageHeader *msg = cls;
1428 GNUNET_assert (NULL != msg);
1429 total_size = ntohs (msg->size);
1431 if (total_size > size)
1436 memcpy (buf, msg, total_size);
1444 * Send another peer a notification to destroy a tunnel
1445 * @param cls The tunnel to destroy
1446 * @param size Size in the buffer
1447 * @param buf Memory where to put the data to transmit
1448 * @return Size of data put in buffer
1451 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1453 struct MeshTunnel *t = cls;
1454 struct MeshClient *c;
1455 struct GNUNET_MESH_TunnelMessage *msg;
1459 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1460 /*FIXME*/ msg->header.size =
1461 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1462 msg->tunnel_id = htonl (t->id.tid);
1464 tunnel_destroy (c, t);
1465 return sizeof (struct GNUNET_MESH_TunnelMessage);
1471 * Send the message to all clients that have subscribed to its type
1473 * @param msg Pointer to the message itself
1474 * @return number of clients this message was sent to
1477 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1479 struct MeshClient *c;
1483 type = ntohs (msg->type);
1484 for (count = 0, c = clients; c != NULL; c = c->next)
1486 if (client_is_subscribed (type, c))
1489 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1499 * Notify the client that owns the tunnel that a peer has connected to it
1501 * @param t Tunnel whose owner to notify
1502 * @param id Short id of the peer that has connected
1505 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1507 struct GNUNET_MESH_PeerControl pc;
1509 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1510 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1511 pc.tunnel_id = htonl (t->local_tid);
1512 GNUNET_PEER_resolve (id, &pc.peer);
1513 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1514 &pc.header, GNUNET_NO);
1518 /******************************************************************************/
1519 /******************** MESH NETWORK HANDLERS **************************/
1520 /******************************************************************************/
1524 * Core handler for path creation
1525 * struct GNUNET_CORE_MessageHandler
1527 * @param cls closure
1528 * @param message message
1529 * @param peer peer identity this notification is about
1530 * @param atsi performance data
1531 * @return GNUNET_OK to keep the connection open,
1532 * GNUNET_SYSERR to close it (signal serious error)
1536 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1537 const struct GNUNET_MessageHeader *message,
1538 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1540 unsigned int own_pos;
1543 MESH_TunnelNumber tid;
1544 struct GNUNET_MESH_ManipulatePath *msg;
1545 struct GNUNET_PeerIdentity *pi;
1546 struct GNUNET_PeerIdentity id;
1547 GNUNET_HashCode hash;
1548 struct MeshPeerPath *path;
1549 struct MeshPeerInfo *dest_peer_info;
1550 struct MeshPeerInfo *orig_peer_info;
1551 struct MeshTunnel *t;
1553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1554 "MESH: Received a MESH path create msg\n");
1555 size = ntohs (message->size);
1556 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1558 GNUNET_break_op (0);
1562 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1563 if (size % sizeof (struct GNUNET_PeerIdentity))
1565 GNUNET_break_op (0);
1568 size /= sizeof (struct GNUNET_PeerIdentity);
1571 GNUNET_break_op (0);
1574 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1576 tid = ntohl (msg->tid);
1577 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1578 t = tunnel_get (pi, tid);
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1583 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1584 t->id.oid = GNUNET_PEER_intern (pi);
1586 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1588 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1590 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1591 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1599 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1600 if (NULL == dest_peer_info)
1602 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1603 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1604 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1606 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1608 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1609 if (NULL == orig_peer_info)
1611 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1612 orig_peer_info->id = GNUNET_PEER_intern (pi);
1613 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1614 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1617 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
1618 path->length = size;
1619 path->peers = GNUNET_malloc (size * sizeof (GNUNET_PEER_Id));
1621 for (i = 0; i < size; i++)
1623 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1624 if (path->peers[i] == myid)
1628 { /* cannot be self, must be 'not found' */
1629 /* create path: self not found in path through self */
1630 GNUNET_break_op (0);
1631 path_destroy (path);
1632 /* FIXME error. destroy tunnel? leave for timeout? */
1635 if (own_pos == size - 1)
1637 /* It is for us! Send ack. */
1638 struct MeshDataDescriptor *info;
1641 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1642 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1643 info->origin = &t->id;
1644 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1645 GNUNET_assert (info->peer);
1646 for (j = 0; info->peer->core_transmit[j]; j++)
1654 info->handler_n = j;
1655 info->peer->core_transmit[j] =
1656 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1657 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1658 sizeof (struct GNUNET_MessageHeader),
1659 &send_core_path_ack, info);
1663 /* It's for somebody else! Retransmit. */
1664 struct MeshPathInfo *path_info;
1666 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1668 path_info->path = path;
1669 path_info->peer = dest_peer_info;
1671 path_add_to_peer (dest_peer_info, path);
1672 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1673 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1674 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1675 sizeof (struct GNUNET_MessageHeader),
1676 &send_core_create_path, path_info);
1683 * Core handler for mesh network traffic going from the origin to a peer
1685 * @param cls closure
1686 * @param peer peer identity this notification is about
1687 * @param message message
1688 * @param atsi performance data
1689 * @return GNUNET_OK to keep the connection open,
1690 * GNUNET_SYSERR to close it (signal serious error)
1693 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1694 const struct GNUNET_MessageHeader *message,
1695 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1697 struct GNUNET_MESH_Unicast *msg;
1698 struct MeshTunnel *t;
1699 struct MeshPeerInfo *pi;
1702 size = ntohs (message->size);
1704 sizeof (struct GNUNET_MESH_Unicast) +
1705 sizeof (struct GNUNET_MessageHeader))
1710 msg = (struct GNUNET_MESH_Unicast *) message;
1711 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1714 /* TODO notify back: we don't know this tunnel */
1717 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1718 &msg->destination.hashPubKey);
1721 /* TODO maybe feedback, log to statistics */
1726 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1729 msg = GNUNET_malloc (size);
1730 memcpy (msg, message, size);
1731 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1732 GNUNET_TIME_UNIT_FOREVER_REL,
1733 path_get_first_hop (t->tree, pi->id),
1735 &send_core_data_raw, msg);
1741 * Core handler for mesh network traffic going from the origin to all peers
1743 * @param cls closure
1744 * @param message message
1745 * @param peer peer identity this notification is about
1746 * @param atsi performance data
1747 * @return GNUNET_OK to keep the connection open,
1748 * GNUNET_SYSERR to close it (signal serious error)
1751 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1752 const struct GNUNET_MessageHeader *message,
1753 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1755 struct GNUNET_MESH_Multicast *msg;
1756 struct GNUNET_PeerIdentity id;
1757 struct MeshDataDescriptor *info;
1758 struct MeshPathInfo neighbors;
1759 struct MeshTunnel *t;
1765 size = ntohs (message->size);
1767 sizeof (struct GNUNET_MESH_Multicast) +
1768 sizeof (struct GNUNET_MessageHeader))
1770 GNUNET_break_op (0);
1773 msg = (struct GNUNET_MESH_Multicast *) message;
1774 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1778 /* TODO notify that we dont know that tunnel */
1782 /* Transmit to locally interested clients */
1783 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1785 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1788 /* Retransmit to other peers.
1789 * Using path here as just a collection of peers, not a path per se.
1792 neighbors.path = GNUNET_malloc (sizeof (struct MeshPeerPath));
1793 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &iterate_collect_neighbors,
1795 if (0 == neighbors.path->length)
1797 GNUNET_free (neighbors.path);
1800 size -= sizeof (struct GNUNET_MESH_Multicast);
1801 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + size);
1802 info->origin = &t->id;
1803 info->copies = neighbors.path->length;
1804 for (i = 0; i < info->copies; i++)
1806 GNUNET_PEER_resolve (neighbors.path->peers[i], &id);
1807 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1808 GNUNET_assert (NULL != info->peer);
1809 for (j = 0; 0 != info->peer->core_transmit[j]; j++)
1811 if (j == (CORE_QUEUE_SIZE - 1))
1817 info->handler_n = j;
1818 info->peer->infos[j] = info;
1819 info->peer->core_transmit[j] =
1820 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1821 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1822 ntohs (msg->header.size),
1823 &send_core_data_multicast, info);
1825 GNUNET_free (neighbors.path->peers);
1826 GNUNET_free (neighbors.path);
1832 * Core handler for mesh network traffic
1834 * @param cls closure
1835 * @param message message
1836 * @param peer peer identity this notification is about
1837 * @param atsi performance data
1839 * @return GNUNET_OK to keep the connection open,
1840 * GNUNET_SYSERR to close it (signal serious error)
1843 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1844 const struct GNUNET_MessageHeader *message,
1845 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1847 struct GNUNET_MESH_ToOrigin *msg;
1848 struct GNUNET_PeerIdentity id;
1849 struct MeshPeerInfo *peer_info;
1850 struct MeshTunnel *t;
1853 size = ntohs (message->size);
1854 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1855 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1857 GNUNET_break_op (0);
1860 msg = (struct GNUNET_MESH_ToOrigin *) message;
1861 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1865 /* TODO notify that we dont know this tunnel (whom)? */
1869 if (t->id.oid == myid)
1871 if (NULL == t->client)
1873 /* got data packet for ownerless tunnel */
1874 GNUNET_break_op (0);
1877 /* TODO signature verification */
1878 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1882 peer_info = peer_info_get (&msg->oid);
1883 if (NULL == peer_info)
1885 /* unknown origin of tunnel */
1889 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1890 msg = GNUNET_malloc (size);
1891 memcpy (msg, message, size);
1892 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1893 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1894 &send_core_data_raw, msg);
1901 * Core handler for path ACKs
1903 * @param cls closure
1904 * @param message message
1905 * @param peer peer identity this notification is about
1906 * @param atsi performance data
1908 * @return GNUNET_OK to keep the connection open,
1909 * GNUNET_SYSERR to close it (signal serious error)
1911 * FIXME path change state
1914 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1915 const struct GNUNET_MessageHeader *message,
1916 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1918 struct GNUNET_MESH_PathACK *msg;
1919 struct MeshTunnel *t;
1920 struct MeshPeerInfo *peer_info;
1922 msg = (struct GNUNET_MESH_PathACK *) message;
1923 t = tunnel_get (&msg->oid, msg->tid);
1926 /* TODO notify that we don't know the tunnel */
1930 /* Message for us? */
1931 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1934 if (NULL == t->client)
1939 peer_info = peer_info_get (&msg->peer_id);
1940 if (NULL == peer_info)
1942 GNUNET_break_op (0);
1945 /* FIXME change state of peer */
1946 send_client_peer_connected(t, peer_info->id);
1950 peer_info = peer_info_get (&msg->oid);
1951 if (NULL == peer_info)
1953 /* If we know the tunnel, we should DEFINITELY know the peer */
1957 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1958 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1959 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1960 GNUNET_TIME_UNIT_FOREVER_REL,
1961 path_get_first_hop (t->tree,
1963 sizeof (struct GNUNET_MESH_PathACK),
1964 &send_core_data_raw, msg);
1970 * Functions to handle messages from core
1972 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1973 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1974 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1975 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1976 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1977 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1978 sizeof (struct GNUNET_MESH_PathACK)},
1984 /******************************************************************************/
1985 /**************** MESH LOCAL HANDLER HELPERS ***********************/
1986 /******************************************************************************/
1989 * deregister_app: iterator for removing each application registered by a client
1991 * @param cls closure
1992 * @param key the hash of the application id (used to access the hashmap)
1993 * @param value the value stored at the key (client)
1995 * @return GNUNET_OK on success
1998 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2000 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2006 * notify_client_connection_failure: notify a client that the connection to the
2007 * requested remote peer is not possible (for instance, no route found)
2008 * Function called when the socket is ready to queue more data. "buf" will be
2009 * NULL and "size" zero if the socket was closed for writing in the meantime.
2011 * @param cls closure
2012 * @param size number of bytes available in buf
2013 * @param buf where the callee should write the message
2014 * @return number of bytes written to buf
2017 notify_client_connection_failure (void *cls, size_t size, void *buf)
2020 struct MeshPeerInfo *peer_info;
2021 struct GNUNET_MESH_PeerControl *msg;
2022 struct GNUNET_PeerIdentity id;
2024 if (0 == size && NULL == buf)
2026 // TODO retry? cancel?
2030 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2031 peer_info = (struct MeshPeerInfo *) cls;
2032 msg = (struct GNUNET_MESH_PeerControl *) buf;
2033 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2034 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2035 // msg->tunnel_id = htonl(peer_info->t->tid);
2036 GNUNET_PEER_resolve (peer_info->id, &id);
2037 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2045 * Function to process paths received for a new peer addition. The recorded
2046 * paths form the initial tunnel, which can be optimized later.
2047 * Called on each result obtained for the DHT search.
2049 * @param cls closure
2050 * @param exp when will this value expire
2051 * @param key key of the result
2052 * @param get_path NULL-terminated array of pointers
2053 * to the peers on reverse GET path (or NULL if not recorded)
2054 * @param put_path NULL-terminated array of pointers
2055 * to the peers on the PUT path (or NULL if not recorded)
2056 * @param type type of the result
2057 * @param size number of bytes in data
2058 * @param data pointer to the result data
2063 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2064 const GNUNET_HashCode * key,
2065 const struct GNUNET_PeerIdentity *const *get_path,
2066 const struct GNUNET_PeerIdentity *const *put_path,
2067 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2069 struct MeshPathInfo *path_info = cls;
2070 struct MeshPeerPath *p;
2071 struct GNUNET_PeerIdentity pi;
2074 if (NULL == get_path || NULL == put_path)
2076 if (NULL == path_info->peer->path_head)
2078 // Find ourselves some alternate initial path to the destination: retry
2079 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2080 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2081 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2082 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2083 GNUNET_BLOCK_TYPE_TEST, /* type */
2084 &pi.hashPubKey, /*key to search */
2085 4, /* replication level */
2086 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2089 0, /* xquery bits */
2091 (void *) path_info);
2096 p = path_build_from_dht (get_path, put_path);
2097 path_add_to_peer (path_info->peer, p);
2098 for (i = 0; i < path_info->peer->ntunnels; i++)
2100 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2102 GNUNET_free (path_info);
2109 * Function to process paths received for a new peer addition. The recorded
2110 * paths form the initial tunnel, which can be optimized later.
2111 * Called on each result obtained for the DHT search.
2113 * @param cls closure
2114 * @param exp when will this value expire
2115 * @param key key of the result
2116 * @param get_path NULL-terminated array of pointers
2117 * to the peers on reverse GET path (or NULL if not recorded)
2118 * @param put_path NULL-terminated array of pointers
2119 * to the peers on the PUT path (or NULL if not recorded)
2120 * @param type type of the result
2121 * @param size number of bytes in data
2122 * @param data pointer to the result data
2125 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2126 const GNUNET_HashCode * key,
2127 const struct GNUNET_PeerIdentity *const *get_path,
2128 const struct GNUNET_PeerIdentity *const *put_path,
2129 enum GNUNET_BLOCK_Type type, size_t size,
2132 const struct GNUNET_PeerIdentity *pi = data;
2133 struct GNUNET_PeerIdentity id;
2134 struct MeshTunnel *t = cls;
2135 struct MeshPeerInfo *peer_info;
2136 struct MeshPathInfo *path_info;
2137 struct MeshPeerPath *p;
2140 if (size != sizeof (struct GNUNET_PeerIdentity))
2142 GNUNET_break_op (0);
2145 GNUNET_assert (NULL != t->client);
2146 GNUNET_DHT_get_stop (t->client->dht_get_type);
2147 t->client->dht_get_type = NULL;
2148 peer_info = peer_info_get (pi);
2149 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2150 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2152 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2153 NULL == peer_info->dhtget)
2155 /* we don't have a route to the peer, let's try a direct lookup */
2156 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2158 GNUNET_TIME_UNIT_FOREVER_REL,
2160 GNUNET_BLOCK_TYPE_TEST,
2163 /* key to look up */
2165 /* replication level */
2166 GNUNET_DHT_RO_RECORD_ROUTE,
2167 /* option to dht: record route */
2168 NULL, /* bloom filter */
2171 0, /* xquery bits */
2174 peer_info); /* closure */
2177 p = path_build_from_dht (get_path, put_path);
2178 path_add_to_peer (peer_info, p);
2179 tunnel_add_peer(t, peer_info);
2180 p = tree_get_path_to_peer(t->tree, peer_info->id);
2182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2183 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2184 t->local_tid, p->length);
2185 for (i = 0; i < p->length; i++)
2187 GNUNET_PEER_resolve (p->peers[0], &id);
2188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2189 GNUNET_h2s_full (&id.hashPubKey));
2195 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2197 path_info->peer = peer_info;
2198 path_info->path = p;
2199 GNUNET_PEER_resolve (p->peers[1], &id);
2200 GNUNET_CORE_notify_transmit_ready (core_handle,
2206 GNUNET_TIME_UNIT_FOREVER_REL,
2210 sizeof (struct GNUNET_MESH_ManipulatePath)
2213 sizeof (struct GNUNET_PeerIdentity)),
2215 &send_core_create_path,
2217 path_info); /* cls */
2221 send_client_peer_connected(t, myid);
2224 /******************************************************************************/
2225 /********************* MESH LOCAL HANDLES **************************/
2226 /******************************************************************************/
2230 * Handler for client disconnection
2232 * @param cls closure
2233 * @param client identification of the client; NULL
2234 * for the last call when the server is destroyed
2237 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2239 struct MeshClient *c;
2240 struct MeshClient *next;
2242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2248 if (c->handle != client && NULL != client)
2250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2255 if (NULL != c->tunnels)
2257 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2258 &tunnel_destroy_iterator,
2260 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2263 /* deregister clients applications */
2264 if (NULL != c->apps)
2266 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2267 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2269 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2270 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2272 GNUNET_SCHEDULER_cancel (announce_applications_task);
2273 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2275 if (NULL != c->types)
2276 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2277 if (NULL != c->dht_get_type)
2278 GNUNET_DHT_get_stop (c->dht_get_type);
2279 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2291 * Handler for new clients
2293 * @param cls closure
2294 * @param client identification of the client
2295 * @param message the actual message, which includes messages the client wants
2298 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2299 const struct GNUNET_MessageHeader *message)
2301 struct GNUNET_MESH_ClientConnect *cc_msg;
2302 struct MeshClient *c;
2303 GNUNET_MESH_ApplicationType *a;
2310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2311 /* Check data sanity */
2312 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2313 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2314 ntypes = ntohs (cc_msg->types);
2315 napps = ntohs (cc_msg->applications);
2317 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2320 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2324 /* Create new client structure */
2325 c = GNUNET_malloc (sizeof (struct MeshClient));
2327 c->id = next_client_id++;
2330 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2333 GNUNET_MESH_ApplicationType at;
2336 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2337 for (i = 0; i < napps; i++)
2340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2341 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2342 /* store in clients hashmap */
2343 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2344 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2345 /* store in global hashmap, for announcements */
2346 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2347 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2349 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2350 announce_applications_task =
2351 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2359 t = (uint16_t *) & a[napps];
2360 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2361 for (i = 0; i < ntypes; i++)
2364 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2366 /* store in clients hashmap */
2367 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2368 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2369 /* store in global hashmap */
2370 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2371 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2375 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2377 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2378 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2379 GNUNET_SERVER_notification_context_add (nc, client);
2381 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2389 * Handler for requests of new tunnels
2391 * @param cls closure
2392 * @param client identification of the client
2393 * @param message the actual message
2396 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2397 const struct GNUNET_MessageHeader *message)
2399 struct GNUNET_MESH_TunnelMessage *t_msg;
2400 struct MeshTunnel *t;
2401 struct MeshClient *c;
2402 GNUNET_HashCode hash;
2404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2406 /* Sanity check for client registration */
2407 if (NULL == (c = client_get (client)))
2410 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2417 /* Message sanity check */
2418 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2421 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2425 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2426 /* Sanity check for tunnel numbering */
2427 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2430 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2433 /* Sanity check for duplicate tunnel IDs */
2434 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2437 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2441 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2443 while (NULL != tunnel_get_by_pi (myid, next_tid))
2444 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2445 t->id.tid = next_tid++;
2447 t->local_tid = ntohl (t_msg->tunnel_id);
2449 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2451 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2453 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2454 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2461 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2463 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2464 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2467 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2470 t->tree = GNUNET_malloc (sizeof(struct MeshTunnelTree));
2471 t->tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32);
2473 t->tree->refresh = REFRESH_PATH_TIME;
2474 t->tree->root = GNUNET_malloc(sizeof(struct MeshTunnelTreeNode));
2475 t->tree->root->status = MESH_PEER_READY;
2476 t->tree->root->t = t;
2477 t->tree->root->peer = myid;
2478 t->tree->me = t->tree->root;
2480 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2486 * Handler for requests of deleting tunnels
2488 * @param cls closure
2489 * @param client identification of the client
2490 * @param message the actual message
2493 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2494 const struct GNUNET_MessageHeader *message)
2496 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2497 struct MeshClient *c;
2498 struct MeshTunnel *t;
2499 MESH_TunnelNumber tid;
2500 GNUNET_HashCode hash;
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2504 /* Sanity check for client registration */
2505 if (NULL == (c = client_get (client)))
2508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2511 /* Message sanity check */
2512 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2515 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2521 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2523 /* Retrieve tunnel */
2524 tid = ntohl (tunnel_msg->tunnel_id);
2526 /* Remove from local id hashmap */
2527 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2528 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2529 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2531 /* Remove from global id hashmap */
2532 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2533 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2535 // notify_tunnel_destroy(t); FIXME
2537 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2543 * Handler for connection requests to new peers
2545 * @param cls closure
2546 * @param client identification of the client
2547 * @param message the actual message (PeerControl)
2552 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2553 const struct GNUNET_MessageHeader *message)
2555 struct GNUNET_MESH_PeerControl *peer_msg;
2556 struct MeshClient *c;
2557 struct MeshTunnel *t;
2558 MESH_TunnelNumber tid;
2559 struct MeshPeerInfo *peer_info;
2562 /* Sanity check for client registration */
2563 if (NULL == (c = client_get (client)))
2566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2570 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2571 /* Sanity check for message size */
2572 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2575 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2579 /* Tunnel exists? */
2580 tid = ntohl (peer_msg->tunnel_id);
2581 t = tunnel_get_by_local_id (c, tid);
2585 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2589 /* Does client own tunnel? */
2590 if (t->client->handle != client)
2593 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2598 peer_info = peer_info_get (&peer_msg->peer);
2600 /* Start DHT search if needed FIXME: if not already connected */
2601 if (NULL == peer_info->dhtget)
2603 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 */
2604 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2607 0, /* xquery bits */
2609 (void *) peer_info);
2612 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2618 * Handler for disconnection requests of peers in a tunnel
2620 * @param cls closure
2621 * @param client identification of the client
2622 * @param message the actual message (PeerControl)
2625 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2626 const struct GNUNET_MessageHeader *message)
2628 struct GNUNET_MESH_PeerControl *peer_msg;
2629 struct MeshClient *c;
2630 struct MeshTunnel *t;
2631 MESH_TunnelNumber tid;
2633 /* Sanity check for client registration */
2634 if (NULL == (c = client_get (client)))
2637 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2640 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2641 /* Sanity check for message size */
2642 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2645 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2649 /* Tunnel exists? */
2650 tid = ntohl (peer_msg->tunnel_id);
2651 t = tunnel_get_by_local_id (c, tid);
2655 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2659 /* Does client own tunnel? */
2660 if (t->client->handle != client)
2663 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2667 /* Ok, delete peer from tunnel */
2668 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2669 &peer_msg->peer.hashPubKey);
2671 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2677 * Handler for connection requests to new peers by type
2679 * @param cls closure
2680 * @param client identification of the client
2681 * @param message the actual message (ConnectPeerByType)
2684 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2685 const struct GNUNET_MessageHeader *message)
2687 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2688 struct MeshClient *c;
2689 struct MeshTunnel *t;
2690 GNUNET_HashCode hash;
2691 GNUNET_MESH_ApplicationType type;
2692 MESH_TunnelNumber tid;
2694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2695 /* Sanity check for client registration */
2696 if (NULL == (c = client_get (client)))
2699 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2703 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2704 /* Sanity check for message size */
2705 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2706 ntohs (connect_msg->header.size))
2709 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2713 /* Tunnel exists? */
2714 tid = ntohl (connect_msg->tunnel_id);
2715 t = tunnel_get_by_local_id (c, tid);
2719 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2723 /* Does client own tunnel? */
2724 if (t->client->handle != client)
2727 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2731 /* Do WE have the service? */
2732 type = ntohl (connect_msg->type);
2733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2734 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2735 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2738 /* Yes! Fast forward, add ourselves to the tunnel and send the
2739 * good news to the client
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2742 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2743 peer_info_get (&my_full_id),
2744 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2747 send_client_peer_connected(t, myid);
2748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2749 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2752 /* Ok, lets find a peer offering the service */
2753 if (c->dht_get_type)
2755 GNUNET_DHT_get_stop (c->dht_get_type);
2757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2758 GNUNET_h2s_full (&hash));
2760 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2761 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2762 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
2763 &dht_get_type_handler, t);
2765 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2771 * Handler for client traffic directed to one peer
2773 * @param cls closure
2774 * @param client identification of the client
2775 * @param message the actual message
2778 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2779 const struct GNUNET_MessageHeader *message)
2781 struct MeshClient *c;
2782 struct MeshTunnel *t;
2783 struct MeshPeerInfo *pi;
2784 struct GNUNET_MESH_Unicast *data_msg;
2785 struct MeshDataDescriptor *info;
2786 MESH_TunnelNumber tid;
2789 /* Sanity check for client registration */
2790 if (NULL == (c = client_get (client)))
2793 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2796 data_msg = (struct GNUNET_MESH_Unicast *) message;
2797 /* Sanity check for message size */
2798 if (sizeof (struct GNUNET_MESH_Unicast) +
2799 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
2802 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2806 /* Tunnel exists? */
2807 tid = ntohl (data_msg->tid);
2808 t = tunnel_get_by_local_id (c, tid);
2812 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2816 /* Is it a local tunnel? Then, does client own the tunnel? */
2817 if (t->client->handle != NULL && t->client->handle != client)
2820 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2824 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2825 &data_msg->destination.hashPubKey);
2826 /* Is the selected peer in the tunnel? */
2830 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2835 struct GNUNET_MESH_Unicast copy;
2837 /* Work around const limitation */
2838 memcpy (©, data_msg, sizeof (struct GNUNET_MESH_Unicast));
2839 copy.oid = my_full_id;
2840 copy.tid = htonl (t->id.tid);
2841 handle_mesh_data_unicast (NULL, &my_full_id, ©.header, NULL);
2844 data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
2845 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
2846 memcpy (&info[1], &data_msg[1], data_size);
2847 info->destination = pi->id;
2848 info->origin = &t->id;
2849 info->size = data_size;
2850 info->client = client;
2851 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2852 GNUNET_TIME_UNIT_FOREVER_REL,
2853 path_get_first_hop (t->tree, pi->id),
2854 /* FIXME re-check types */
2856 sizeof (struct GNUNET_MESH_Unicast),
2857 &send_core_data_unicast, info);
2862 * Handler for client traffic directed to all peers in a tunnel
2864 * @param cls closure
2865 * @param client identification of the client
2866 * @param message the actual message
2869 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2870 const struct GNUNET_MessageHeader *message)
2872 struct MeshClient *c;
2873 struct MeshTunnel *t;
2874 struct GNUNET_MESH_Multicast *data_msg;
2875 MESH_TunnelNumber tid;
2877 /* Sanity check for client registration */
2878 if (NULL == (c = client_get (client)))
2881 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2884 data_msg = (struct GNUNET_MESH_Multicast *) message;
2885 /* Sanity check for message size */
2886 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2889 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2893 /* Tunnel exists? */
2894 tid = ntohl (data_msg->tid);
2895 t = tunnel_get_by_local_id (c, tid);
2899 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2903 /* Does client own tunnel? */
2904 if (t->client->handle != client)
2907 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2913 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2918 * Functions to handle messages from clients
2920 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
2921 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2922 {&handle_local_tunnel_create, NULL,
2923 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2924 sizeof (struct GNUNET_MESH_TunnelMessage)},
2925 {&handle_local_tunnel_destroy, NULL,
2926 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2927 sizeof (struct GNUNET_MESH_TunnelMessage)},
2928 {&handle_local_connect_add, NULL,
2929 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
2930 sizeof (struct GNUNET_MESH_PeerControl)},
2931 {&handle_local_connect_del, NULL,
2932 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
2933 sizeof (struct GNUNET_MESH_PeerControl)},
2934 {&handle_local_connect_by_type, NULL,
2935 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
2936 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
2937 {&handle_local_unicast, NULL,
2938 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2939 {&handle_local_unicast, NULL,
2940 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2941 {&handle_local_multicast, NULL,
2942 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2948 * To be called on core init/fail.
2950 * @param cls service closure
2951 * @param server handle to the server for this service
2952 * @param identity the public identity of this peer
2953 * @param publicKey the public key of this peer
2956 core_init (void *cls, struct GNUNET_CORE_Handle *server,
2957 const struct GNUNET_PeerIdentity *identity,
2958 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
2961 core_handle = server;
2962 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
2964 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
2965 GNUNET_SCHEDULER_shutdown();
2971 * Method called whenever a given peer connects.
2973 * @param cls closure
2974 * @param peer peer identity this notification is about
2975 * @param atsi performance data for the connection
2978 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
2979 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2981 // GNUNET_PEER_Id pid;
2982 struct MeshPeerInfo *peer_info;
2983 struct MeshPeerPath *path;
2985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
2986 peer_info = peer_info_get (peer);
2987 if (myid == peer_info->id)
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
2992 path = GNUNET_malloc (sizeof (struct MeshPeerPath));
2994 path->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id) * 2);
2995 path->peers[0] = myid;
2996 path->peers[1] = peer_info->id;
2997 path_add_to_peer (peer_info, path);
3002 * Method called whenever a peer disconnects.
3004 * @param cls closure
3005 * @param peer peer identity this notification is about
3008 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3010 struct MeshPeerInfo *pi;
3013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3014 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3020 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3022 if (pi->core_transmit[i])
3024 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3025 /* TODO: notify that tranmission has failed */
3026 GNUNET_free (pi->infos[i]);
3029 path_remove_from_peer (pi, pi->id, myid);
3032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3038 /******************************************************************************/
3039 /************************ MAIN FUNCTIONS ****************************/
3040 /******************************************************************************/
3043 * Task run during shutdown.
3049 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3051 struct MeshClient *c;
3053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3054 if (core_handle != NULL)
3056 GNUNET_CORE_disconnect (core_handle);
3059 if (dht_handle != NULL)
3061 for (c = clients; NULL != c; c = c->next)
3062 if (NULL != c->dht_get_type)
3063 GNUNET_DHT_get_stop (c->dht_get_type);
3064 GNUNET_DHT_disconnect (dht_handle);
3069 GNUNET_SERVER_notification_context_destroy (nc);
3072 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3074 GNUNET_SCHEDULER_cancel (announce_id_task);
3075 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3081 * Process mesh requests.
3083 * @param cls closure
3084 * @param server the initialized server
3085 * @param c configuration to use
3088 run (void *cls, struct GNUNET_SERVER_Handle *server,
3089 const struct GNUNET_CONFIGURATION_Handle *c)
3091 struct MeshPeerInfo *peer;
3092 struct MeshPeerPath *p;
3095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3096 server_handle = server;
3097 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3098 CORE_QUEUE_SIZE, /* queue size */
3099 NULL, /* Closure passed to MESH functions */
3100 &core_init, /* Call core_init once connected */
3101 &core_connect, /* Handle connects */
3102 &core_disconnect, /* remove peers on disconnects */
3103 NULL, /* Do we care about "status" updates? */
3104 NULL, /* Don't notify about all incoming messages */
3105 GNUNET_NO, /* For header only in notification */
3106 NULL, /* Don't notify about all outbound messages */
3107 GNUNET_NO, /* For header-only out notification */
3108 core_handlers); /* Register these handlers */
3109 if (core_handle == NULL)
3112 GNUNET_SCHEDULER_shutdown ();
3117 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3122 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3123 GNUNET_SCHEDULER_shutdown ();
3126 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3127 GNUNET_free (keyfile);
3128 if (my_private_key == NULL)
3130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3131 _("Mesh service could not access hostkey. Exiting.\n"));
3132 GNUNET_SCHEDULER_shutdown ();
3135 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3136 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3137 &my_full_id.hashPubKey);
3138 myid = GNUNET_PEER_intern (&my_full_id);
3140 dht_handle = GNUNET_DHT_connect (c, 64);
3141 if (dht_handle == NULL)
3143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3144 Running without DHT has a severe\
3145 impact in MESH capabilities.\n\
3146 Plase check your configuretion and enable DHT.\n");
3152 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3153 peers = GNUNET_CONTAINER_multihashmap_create (32);
3154 applications = GNUNET_CONTAINER_multihashmap_create (32);
3155 types = GNUNET_CONTAINER_multihashmap_create (32);
3157 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3158 nc = GNUNET_SERVER_notification_context_create (server_handle,
3160 GNUNET_SERVER_disconnect_notify (server_handle,
3161 &handle_local_client_disconnect,
3166 clients_tail = NULL;
3171 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3172 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3174 /* Create a peer_info for the local peer */
3175 peer = peer_info_get(&my_full_id);
3176 p = GNUNET_malloc (sizeof (struct MeshPeerPath));
3177 p->peers = GNUNET_malloc (sizeof (GNUNET_PEER_Id));
3180 path_add_to_peer(peer, p);
3182 /* Scheduled the task to clean up when shutdown is called */
3183 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3190 * The main function for the mesh service.
3192 * @param argc number of arguments from the command line
3193 * @param argv command line arguments
3194 * @return 0 ok, 1 on error
3197 main (int argc, char *const *argv)
3202 fprintf (stderr, "main ()\n");
3204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3207 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3211 fprintf (stderr, "main () END\n");