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,
508 /******************************************************************************/
509 /************************ PERIODIC FUNCTIONS ****************************/
510 /******************************************************************************/
513 * Announce iterator over for each application provided by the peer
516 * @param key current key code
517 * @param value value in the hash map
518 * @return GNUNET_YES if we should continue to
523 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
525 /* FIXME are hashes in multihash map equal on all aquitectures? */
526 GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
527 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
528 (const char *) &my_full_id,
530 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
531 &mesh_debug, "DHT_put for app completed");
533 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
535 APP_ANNOUNCE_TIME, NULL, NULL);
542 * Periodically announce what applications are provided by local clients
545 * @param tc task context
548 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
550 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
552 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
556 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
558 announce_applications_task =
559 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
567 * Periodically announce self id in the DHT
570 * @param tc task context
573 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
575 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
577 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
581 * - Set data expiration in function of X
584 GNUNET_DHT_put (dht_handle, /* DHT handle */
585 &my_full_id.hashPubKey, /* Key to use */
586 10U, /* Replication level */
587 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
588 GNUNET_BLOCK_TYPE_TEST, /* Block type */
589 0, /* Size of the data */
590 NULL, /* Data itself */
591 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
592 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
594 &mesh_debug, "DHT_put for id completed");
596 NULL, /* Continuation */
597 NULL); /* Continuation closure */
600 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
605 * Function to process paths received for a new peer addition. The recorded
606 * paths form the initial tunnel, which can be optimized later.
607 * Called on each result obtained for the DHT search.
610 * @param exp when will this value expire
611 * @param key key of the result
612 * @param get_path NULL-terminated array of pointers
613 * to the peers on reverse GET path (or NULL if not recorded)
614 * @param put_path NULL-terminated array of pointers
615 * to the peers on the PUT path (or NULL if not recorded)
616 * @param type type of the result
617 * @param size number of bytes in data
618 * @param data pointer to the result data
621 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
622 const GNUNET_HashCode * key,
623 const struct GNUNET_PeerIdentity *const *get_path,
624 const struct GNUNET_PeerIdentity *const *put_path,
625 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
628 /******************************************************************************/
629 /****************** GENERAL HELPER FUNCTIONS ************************/
630 /******************************************************************************/
633 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
634 * and insert it in the appropiate structures if the peer is not known yet.
636 * @param peer Identity of the peer
638 * @return Existing or newly created peer info
640 static struct MeshPeerInfo *
641 peer_info_get (const struct GNUNET_PeerIdentity *peer)
643 struct MeshPeerInfo *peer_info;
645 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
646 if (NULL == peer_info)
649 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
650 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
651 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
652 peer_info->id = GNUNET_PEER_intern (peer);
661 * Destroy the peer_info and free any allocated resources linked to it
662 * @param t tunnel the path belongs to
663 * @param pi the peer_info to destroy
664 * @return GNUNET_OK on success
667 peer_info_destroy (struct MeshPeerInfo *pi)
669 GNUNET_HashCode hash;
670 struct GNUNET_PeerIdentity id;
672 GNUNET_PEER_resolve (pi->id, &id);
673 GNUNET_PEER_change_rc (pi->id, -1);
674 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
676 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
677 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
685 * Notify a tunnel that a connection has broken that affects at least
688 * @param t Tunnel affected.
689 * @param peer Peer that (at least) has been affected by the disconnection.
690 * @param p1 Peer that got disconnected from p2.
691 * @param p2 Peer that got disconnected from p1.
693 * @return Short ID of the peer disconnected (either p1 or p2).
694 * 0 if the tunnel remained unaffected.
696 static GNUNET_PEER_Id
697 tunnel_notify_connection_broken (struct MeshTunnel *t,
698 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
702 * Remove all paths that rely on a direct connection between p1 and p2
703 * from the peer itself and notify all tunnels about it.
705 * @param peer PeerInfo of affected peer.
706 * @param p1 GNUNET_PEER_Id of one peer.
707 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
710 * TODO: optimize (see below)
713 path_remove_from_peer (struct MeshPeerInfo *peer,
717 struct GNUNET_PeerIdentity id;
718 struct MeshPeerPath *p;
719 struct MeshPeerPath *aux;
720 struct MeshPeerInfo *peer_d;
722 unsigned int destroyed;
732 for (i = 0; i < (p->length - 1); i++)
734 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
735 (p->peers[i] == p2 && p->peers[i + 1] == p1))
747 for (i = 0; i < peer->ntunnels; i++)
749 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
751 * Problem: one or more peers have been deleted from the tunnel tree.
752 * We don't know who they are to try to add them again.
753 * We need to try to find a new path for each of the disconnected peers.
754 * Some of them might already have a path to reach them that does not
755 * involve p1 and p2. Adding all anew might render in a better tree than
756 * the trivial immediate fix.
758 * Trivial immiediate fix: try to reconnect to the disconnected node. All
759 * its children will be reachable trough him.
761 GNUNET_PEER_resolve(d, &id);
762 peer_d = peer_info_get(&id);
765 for (p = peer_d->path_head; NULL != p; p = p->next)
767 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
775 /* No callback, as peer will be already disconnected */
776 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
780 struct MeshPathInfo *path_info;
782 if (NULL != peer_d->dhtget)
784 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
786 path_info->peer = peer_d;
787 path_info->t = peer->tunnels[i];
788 peer_d->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
789 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
790 GNUNET_BLOCK_TYPE_TEST, /* type */
791 &id.hashPubKey, /*key to search */
792 4, /* replication level */
793 GNUNET_DHT_RO_RECORD_ROUTE,
794 NULL, /* bloom filter */
806 * Add the path to the peer and update the path used to reach it in case this
809 * @param peer_info Destination peer to add the path to.
810 * @param path New path to add. Last peer must be the peer in arg 1.
811 * Path will be either used of freed if already known.
813 * TODO: trim the part from origin to us? Add it as path to origin?
816 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
818 struct MeshPeerPath *aux;
822 if (NULL == peer_info || NULL == path)
828 l = path_get_length (path);
830 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
832 l2 = path_get_length (aux);
835 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
836 peer_info->path_tail, aux, path);
840 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
847 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
854 * Add the path to the origin peer and update the path used to reach it in case
855 * this is the shortest.
856 * The path is given in peer_info -> destination, therefore we turn the path
859 * @param peer_info Peer to add the path to, being the origin of the path.
860 * @param path New path to add after being inversed.
863 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
866 path_add_to_peer (peer_info, path);
871 * Build a PeerPath from the paths returned from the DHT, reversing the paths
872 * to obtain a local peer -> destination path and interning the peer ids.
874 * @param get_path NULL-terminated array of pointers
875 * to the peers on reverse GET path (or NULL if not recorded)
876 * @param put_path NULL-terminated array of pointers
877 * to the peers on the PUT path (or NULL if not recorded)
879 * @return Newly allocated and created path
881 static struct MeshPeerPath *
882 path_build_from_dht (const struct GNUNET_PeerIdentity *const *get_path,
883 const struct GNUNET_PeerIdentity *const *put_path)
885 struct MeshPeerPath *p;
890 for (i = 0; get_path[i] != NULL; i++) ;
891 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
892 for (i--; i >= 0; i--)
894 id = GNUNET_PEER_intern (get_path[i]);
895 if (p->length > 0 && id == p->peers[p->length - 1])
897 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
898 GNUNET_PEER_change_rc(id, -1);
903 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
904 p->peers[p->length] = id;
908 for (i = 0; put_path[i] != NULL; i++) ;
909 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
910 for (i--; i >= 0; i--)
912 id = GNUNET_PEER_intern (put_path[i]);
913 if (p->length > 0 && id == p->peers[p->length - 1])
915 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
916 GNUNET_PEER_change_rc(id, -1);
921 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
922 p->peers[p->length] = id;
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
928 "MESH: (first of GET: %s)\n",
929 GNUNET_h2s_full(&get_path[0]->hashPubKey));
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
931 "MESH: (first of PUT: %s)\n",
932 GNUNET_h2s_full(&put_path[0]->hashPubKey));
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934 "MESH: In total: %d hops\n",
942 * Send keepalive packets for a peer
950 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
952 struct MeshTunnel *t = cls;
954 // struct GNUNET_PeerIdentity id;
956 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
960 /* FIXME implement multicast keepalive. Just an empty multicast packet? */
961 // GNUNET_PEER_resolve (path_get_first_hop (path->t, path->peer)->id, &id);
962 // GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
963 // GNUNET_TIME_UNIT_FOREVER_REL, &id,
964 // sizeof (struct GNUNET_MESH_ManipulatePath)
966 // (path->path->length *
967 // sizeof (struct GNUNET_PeerIdentity)),
968 // &send_core_create_path,
970 t->path_refresh_task =
971 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
977 * Check if client has registered with the service and has not disconnected
979 * @param client the client to check
981 * @return non-NULL if client exists in the global DLL
983 static struct MeshClient *
984 client_get (struct GNUNET_SERVER_Client *client)
986 struct MeshClient *c;
991 if (c->handle == client)
1000 * Checks if a given client has subscribed to certain message type
1002 * @param message_type Type of message to check
1003 * @param c Client to check
1005 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1010 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1014 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1015 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1020 * Search for a tunnel among the tunnels for a client
1022 * @param c the client whose tunnels to search in
1023 * @param tid the local id of the tunnel
1025 * @return tunnel handler, NULL if doesn't exist
1027 static struct MeshTunnel *
1028 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1030 GNUNET_HashCode hash;
1032 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1033 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1038 * Search for a tunnel by global ID using PEER_ID
1040 * @param pi owner of the tunnel
1041 * @param tid global tunnel number
1043 * @return tunnel handler, NULL if doesn't exist
1045 static struct MeshTunnel *
1046 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1048 struct MESH_TunnelID id;
1049 GNUNET_HashCode hash;
1054 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1055 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1060 * Search for a tunnel by global ID using full PeerIdentities
1062 * @param oid owner of the tunnel
1063 * @param tid global tunnel number
1065 * @return tunnel handler, NULL if doesn't exist
1067 static struct MeshTunnel *
1068 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1070 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1075 * Callback used to notify a client owner of a tunnel that a peer has
1076 * disconnected, most likely because of a path change.
1078 * @param n Node in the tree representing the disconnected peer
1081 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1083 struct GNUNET_MESH_PeerControl msg;
1085 if (NULL == n->t->client || NULL == nc)
1088 msg.header.size = htons (sizeof (msg));
1089 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1090 msg.tunnel_id = htonl (n->t->local_tid);
1091 GNUNET_PEER_resolve (n->peer, &msg.peer);
1092 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1093 &msg.header, GNUNET_NO);
1098 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1099 * needed rescources.
1101 * @param t Tunnel we want to add a new peer to
1102 * @param peer PeerInfo of the peer being added
1106 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1108 // struct MeshTunnelTreeNode *n;
1109 struct MeshPeerPath *p;
1110 struct MeshPeerPath *best_p;
1111 unsigned int best_cost;
1114 if (NULL != tree_find_peer(t->tree->root, peer->id))
1116 /* Already have it, nothing to do. */
1121 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1122 if (NULL == (p = peer->path_head))
1129 best_cost = UINT_MAX;
1132 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1139 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1140 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1141 t->path_refresh_task =
1142 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1147 * Notify a tunnel that a connection has broken that affects at least
1148 * some of its peers.
1150 * @param t Tunnel affected.
1151 * @param peer Peer that (at least) has been affected by the disconnection.
1152 * @param p1 Peer that got disconnected from p2.
1153 * @param p2 Peer that got disconnected from p1.
1155 * @return Short ID of the peer disconnected (either p1 or p2).
1156 * 0 if the tunnel remained unaffected.
1158 static GNUNET_PEER_Id
1159 tunnel_notify_connection_broken (struct MeshTunnel *t,
1160 struct MeshPeerInfo *peer,
1164 return tree_notify_connection_broken (t->tree, p1, p2,
1165 ¬ify_peer_disconnected);
1170 * Destroy the tunnel and free any allocated resources linked to it
1172 * @param t the tunnel to destroy
1174 * @return GNUNET_OK on success
1177 tunnel_destroy (struct MeshTunnel *t)
1179 struct MeshClient *c;
1180 struct MeshQueue *q;
1181 struct MeshQueue *qn;
1182 GNUNET_HashCode hash;
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1194 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1195 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1200 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1201 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1205 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1209 if (NULL != q->data)
1210 GNUNET_free (q->data);
1214 /* TODO cancel core transmit ready in case it was active */
1216 tree_destroy(t->tree);
1223 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1224 * client when the client disconnects.
1226 * @param cls closure (client that is disconnecting)
1227 * @param key the hash of the local tunnel id (used to access the hashmap)
1228 * @param value the value stored at the key (tunnel to destroy)
1230 * @return GNUNET_OK on success
1233 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1237 r = tunnel_destroy ((struct MeshTunnel *) value);
1242 /******************************************************************************/
1243 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1244 /******************************************************************************/
1248 * Function called to notify a client about the socket
1249 * being ready to queue more data. "buf" will be
1250 * NULL and "size" zero if the socket was closed for
1251 * writing in the meantime.
1253 * @param cls closure
1254 * @param size number of bytes available in buf
1255 * @param buf where the callee should write the message
1256 * @return number of bytes written to buf
1259 send_core_create_path (void *cls, size_t size, void *buf)
1261 struct MeshPathInfo *info = cls;
1262 struct GNUNET_MESH_ManipulatePath *msg;
1263 struct GNUNET_PeerIdentity *peer_ptr;
1264 struct MeshPeerInfo *peer = info->peer;
1265 struct MeshTunnel *t = info->t;
1266 struct MeshPeerPath *p = info->path;
1271 sizeof (struct GNUNET_MESH_ManipulatePath) +
1272 p->length * sizeof (struct GNUNET_PeerIdentity);
1274 if (size < size_needed || NULL == buf)
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
1277 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1278 GNUNET_TIME_UNIT_FOREVER_REL,
1279 path_get_first_hop (t->tree, peer->id),
1280 size_needed, &send_core_create_path,
1285 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1286 msg->header.size = htons (size_needed);
1287 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1288 msg->tid = ntohl (t->id.tid);
1290 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1291 for (i = 0; i < p->length; i++)
1293 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1305 * Function called to notify a client about the socket
1306 * being ready to queue more data. "buf" will be
1307 * NULL and "size" zero if the socket was closed for
1308 * writing in the meantime.
1310 * @param cls closure (MeshDataDescriptor with all info to build packet)
1311 * @param size number of bytes available in buf
1312 * @param buf where the callee should write the message
1313 * @return number of bytes written to buf
1316 send_core_data_to_origin (void *cls, size_t size, void *buf)
1318 struct MeshDataDescriptor *info = cls;
1319 struct GNUNET_MESH_ToOrigin *msg = buf;
1322 GNUNET_assert (NULL != info);
1323 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1324 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1326 if (total_size > size)
1328 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1329 "not enough buffer to send data to origin\n");
1332 msg->header.size = htons (total_size);
1333 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1334 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1335 msg->tid = htonl (info->origin->tid);
1336 if (0 != info->size)
1338 memcpy (&msg[1], &info[1], info->size);
1340 if (NULL != info->client)
1342 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1350 * Function called to notify a client about the socket
1351 * being ready to queue more data. "buf" will be
1352 * NULL and "size" zero if the socket was closed for
1353 * writing in the meantime.
1355 * @param cls closure (data itself)
1356 * @param size number of bytes available in buf
1357 * @param buf where the callee should write the message
1358 * @return number of bytes written to buf
1361 send_core_data_unicast (void *cls, size_t size, void *buf)
1363 struct MeshDataDescriptor *info = cls;
1364 struct GNUNET_MESH_Unicast *msg = buf;
1367 GNUNET_assert (NULL != info);
1368 total_size = sizeof (struct GNUNET_MESH_Unicast) + info->size;
1369 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1371 if (total_size > size)
1373 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1374 "not enough buffer to send data to peer\n");
1377 msg->header.size = htons (total_size);
1378 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1379 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1380 GNUNET_PEER_resolve (info->destination, &msg->destination);
1381 msg->tid = htonl (info->origin->tid);
1382 if (0 != info->size)
1384 memcpy (&msg[1], &info[1], info->size);
1386 if (NULL != info->client)
1388 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1396 * Function called to notify a client about the socket
1397 * being ready to queue more data. "buf" will be
1398 * NULL and "size" zero if the socket was closed for
1399 * writing in the meantime.
1401 * @param cls closure (data itself)
1402 * @param size number of bytes available in buf
1403 * @param buf where the callee should write the message
1404 * @return number of bytes written to buf
1407 send_core_data_multicast (void *cls, size_t size, void *buf)
1409 struct MeshDataDescriptor *info = cls;
1410 struct GNUNET_MESH_Multicast *msg = buf;
1413 GNUNET_assert (NULL != info);
1414 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1415 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1419 info->peer->core_transmit[info->handler_n] = NULL;
1421 if (total_size > size)
1423 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1424 "not enough buffer to send data futher\n");
1427 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1428 msg->header.size = htons (total_size);
1429 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1430 msg->tid = htonl (info->origin->tid);
1431 memcpy (&msg[1], &info[1], total_size);
1432 if (0 == --info->copies)
1434 if (NULL != info->client)
1436 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1445 * Function called to notify a client about the socket
1446 * being ready to queue more data. "buf" will be
1447 * NULL and "size" zero if the socket was closed for
1448 * writing in the meantime.
1450 * @param cls closure (MeshDataDescriptor)
1451 * @param size number of bytes available in buf
1452 * @param buf where the callee should write the message
1453 * @return number of bytes written to buf
1456 send_core_path_ack (void *cls, size_t size, void *buf)
1458 struct MeshDataDescriptor *info = cls;
1459 struct GNUNET_MESH_PathACK *msg = buf;
1461 GNUNET_assert (NULL != info);
1464 info->peer->core_transmit[info->handler_n] = NULL;
1466 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1471 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1472 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1473 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1474 msg->tid = htonl (info->origin->tid);
1475 msg->peer_id = my_full_id;
1477 /* TODO add signature */
1479 return sizeof (struct GNUNET_MESH_PathACK);
1484 * Function called to notify a client about the socket
1485 * being ready to queue more data. "buf" will be
1486 * NULL and "size" zero if the socket was closed for
1487 * writing in the meantime.
1489 * @param cls closure (data itself)
1490 * @param size number of bytes available in buf
1491 * @param buf where the callee should write the message
1492 * @return number of bytes written to buf
1495 send_core_data_raw (void *cls, size_t size, void *buf)
1497 struct GNUNET_MessageHeader *msg = cls;
1500 GNUNET_assert (NULL != msg);
1501 total_size = ntohs (msg->size);
1503 if (total_size > size)
1508 memcpy (buf, msg, total_size);
1516 * Send another peer a notification to destroy a tunnel
1517 * @param cls The tunnel to destroy
1518 * @param size Size in the buffer
1519 * @param buf Memory where to put the data to transmit
1520 * @return Size of data put in buffer
1523 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1525 struct MeshTunnel *t = cls;
1526 struct MeshClient *c;
1527 struct GNUNET_MESH_TunnelMessage *msg;
1531 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1532 /*FIXME*/ msg->header.size =
1533 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1534 msg->tunnel_id = htonl (t->id.tid);
1536 tunnel_destroy (c, t);
1537 return sizeof (struct GNUNET_MESH_TunnelMessage);
1543 * Send the message to all clients that have subscribed to its type
1545 * @param msg Pointer to the message itself
1546 * @return number of clients this message was sent to
1549 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1551 struct MeshClient *c;
1555 type = ntohs (msg->type);
1556 for (count = 0, c = clients; c != NULL; c = c->next)
1558 if (client_is_subscribed (type, c))
1561 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1571 * Notify the client that owns the tunnel that a peer has connected to it
1573 * @param t Tunnel whose owner to notify
1574 * @param id Short id of the peer that has connected
1577 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1579 struct GNUNET_MESH_PeerControl pc;
1581 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1582 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1583 pc.tunnel_id = htonl (t->local_tid);
1584 GNUNET_PEER_resolve (id, &pc.peer);
1585 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1586 &pc.header, GNUNET_NO);
1590 /******************************************************************************/
1591 /******************** MESH NETWORK HANDLERS **************************/
1592 /******************************************************************************/
1596 * Core handler for path creation
1597 * struct GNUNET_CORE_MessageHandler
1599 * @param cls closure
1600 * @param message message
1601 * @param peer peer identity this notification is about
1602 * @param atsi performance data
1603 * @return GNUNET_OK to keep the connection open,
1604 * GNUNET_SYSERR to close it (signal serious error)
1608 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1609 const struct GNUNET_MessageHeader *message,
1610 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1612 unsigned int own_pos;
1615 MESH_TunnelNumber tid;
1616 struct GNUNET_MESH_ManipulatePath *msg;
1617 struct GNUNET_PeerIdentity *pi;
1618 struct GNUNET_PeerIdentity id;
1619 GNUNET_HashCode hash;
1620 struct MeshPeerPath *path;
1621 struct MeshPeerInfo *dest_peer_info;
1622 struct MeshPeerInfo *orig_peer_info;
1623 struct MeshTunnel *t;
1625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1626 "MESH: Received a MESH path create msg\n");
1627 size = ntohs (message->size);
1628 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1630 GNUNET_break_op (0);
1634 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1635 if (size % sizeof (struct GNUNET_PeerIdentity))
1637 GNUNET_break_op (0);
1640 size /= sizeof (struct GNUNET_PeerIdentity);
1643 GNUNET_break_op (0);
1646 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1648 tid = ntohl (msg->tid);
1649 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1650 t = tunnel_get (pi, tid);
1654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1655 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1656 t->id.oid = GNUNET_PEER_intern (pi);
1658 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1660 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1662 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1663 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1671 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1672 if (NULL == dest_peer_info)
1674 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1675 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1676 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1678 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1680 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1681 if (NULL == orig_peer_info)
1683 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1684 orig_peer_info->id = GNUNET_PEER_intern (pi);
1685 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1686 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1689 path = path_new (size);
1691 for (i = 0; i < size; i++)
1693 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1694 if (path->peers[i] == myid)
1698 { /* cannot be self, must be 'not found' */
1699 /* create path: self not found in path through self */
1700 GNUNET_break_op (0);
1701 path_destroy (path);
1702 /* FIXME error. destroy tunnel? leave for timeout? */
1705 if (own_pos == size - 1)
1707 /* It is for us! Send ack. */
1708 struct MeshDataDescriptor *info;
1711 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1712 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1713 info->origin = &t->id;
1714 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1715 GNUNET_assert (info->peer);
1716 for (j = 0; info->peer->core_transmit[j]; j++)
1724 info->handler_n = j;
1725 info->peer->core_transmit[j] =
1726 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1727 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1728 sizeof (struct GNUNET_MessageHeader),
1729 &send_core_path_ack, info);
1733 /* It's for somebody else! Retransmit. */
1734 struct MeshPathInfo *path_info;
1736 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1738 path_info->path = path;
1739 path_info->peer = dest_peer_info;
1741 path_add_to_peer (dest_peer_info, path);
1742 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1743 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1744 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1745 sizeof (struct GNUNET_MessageHeader),
1746 &send_core_create_path, path_info);
1753 * Core handler for mesh network traffic going from the origin to a peer
1755 * @param cls closure
1756 * @param peer peer identity this notification is about
1757 * @param message message
1758 * @param atsi performance data
1759 * @return GNUNET_OK to keep the connection open,
1760 * GNUNET_SYSERR to close it (signal serious error)
1763 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1764 const struct GNUNET_MessageHeader *message,
1765 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1767 struct GNUNET_MESH_Unicast *msg;
1768 struct MeshTunnel *t;
1769 struct MeshPeerInfo *pi;
1772 size = ntohs (message->size);
1774 sizeof (struct GNUNET_MESH_Unicast) +
1775 sizeof (struct GNUNET_MessageHeader))
1780 msg = (struct GNUNET_MESH_Unicast *) message;
1781 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1784 /* TODO notify back: we don't know this tunnel */
1787 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1788 &msg->destination.hashPubKey);
1791 /* TODO maybe feedback, log to statistics */
1796 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1799 msg = GNUNET_malloc (size);
1800 memcpy (msg, message, size);
1801 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1802 GNUNET_TIME_UNIT_FOREVER_REL,
1803 path_get_first_hop (t->tree, pi->id),
1805 &send_core_data_raw, msg);
1811 * Core handler for mesh network traffic going from the origin to all peers
1813 * @param cls closure
1814 * @param message message
1815 * @param peer peer identity this notification is about
1816 * @param atsi performance data
1817 * @return GNUNET_OK to keep the connection open,
1818 * GNUNET_SYSERR to close it (signal serious error)
1821 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1822 const struct GNUNET_MessageHeader *message,
1823 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1825 struct GNUNET_MESH_Multicast *msg;
1826 struct GNUNET_PeerIdentity id;
1827 struct MeshDataDescriptor *info;
1828 struct MeshPathInfo neighbors;
1829 struct MeshTunnel *t;
1835 size = ntohs (message->size);
1837 sizeof (struct GNUNET_MESH_Multicast) +
1838 sizeof (struct GNUNET_MessageHeader))
1840 GNUNET_break_op (0);
1843 msg = (struct GNUNET_MESH_Multicast *) message;
1844 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1848 /* TODO notify that we dont know that tunnel */
1852 /* Transmit to locally interested clients */
1853 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1855 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1858 /* Retransmit to other peers.
1859 * Using path here as just a collection of peers, not a path per se.
1862 neighbors.path = path_new (0);
1863 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &iterate_collect_neighbors,
1865 if (0 == neighbors.path->length)
1867 GNUNET_free (neighbors.path);
1870 size -= sizeof (struct GNUNET_MESH_Multicast);
1871 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + size);
1872 info->origin = &t->id;
1873 info->copies = neighbors.path->length;
1874 for (i = 0; i < info->copies; i++)
1876 GNUNET_PEER_resolve (neighbors.path->peers[i], &id);
1877 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1878 GNUNET_assert (NULL != info->peer);
1879 for (j = 0; 0 != info->peer->core_transmit[j]; j++)
1881 if (j == (CORE_QUEUE_SIZE - 1))
1887 info->handler_n = j;
1888 info->peer->infos[j] = info;
1889 info->peer->core_transmit[j] =
1890 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1891 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1892 ntohs (msg->header.size),
1893 &send_core_data_multicast, info);
1895 GNUNET_free (neighbors.path->peers);
1896 GNUNET_free (neighbors.path);
1902 * Core handler for mesh network traffic
1904 * @param cls closure
1905 * @param message message
1906 * @param peer peer identity this notification is about
1907 * @param atsi performance data
1909 * @return GNUNET_OK to keep the connection open,
1910 * GNUNET_SYSERR to close it (signal serious error)
1913 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1914 const struct GNUNET_MessageHeader *message,
1915 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1917 struct GNUNET_MESH_ToOrigin *msg;
1918 struct GNUNET_PeerIdentity id;
1919 struct MeshPeerInfo *peer_info;
1920 struct MeshTunnel *t;
1923 size = ntohs (message->size);
1924 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1925 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1927 GNUNET_break_op (0);
1930 msg = (struct GNUNET_MESH_ToOrigin *) message;
1931 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1935 /* TODO notify that we dont know this tunnel (whom)? */
1939 if (t->id.oid == myid)
1941 if (NULL == t->client)
1943 /* got data packet for ownerless tunnel */
1944 GNUNET_break_op (0);
1947 /* TODO signature verification */
1948 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1952 peer_info = peer_info_get (&msg->oid);
1953 if (NULL == peer_info)
1955 /* unknown origin of tunnel */
1959 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1960 msg = GNUNET_malloc (size);
1961 memcpy (msg, message, size);
1962 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1963 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1964 &send_core_data_raw, msg);
1971 * Core handler for path ACKs
1973 * @param cls closure
1974 * @param message message
1975 * @param peer peer identity this notification is about
1976 * @param atsi performance data
1978 * @return GNUNET_OK to keep the connection open,
1979 * GNUNET_SYSERR to close it (signal serious error)
1981 * FIXME path change state
1984 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1985 const struct GNUNET_MessageHeader *message,
1986 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1988 struct GNUNET_MESH_PathACK *msg;
1989 struct MeshTunnel *t;
1990 struct MeshPeerInfo *peer_info;
1992 msg = (struct GNUNET_MESH_PathACK *) message;
1993 t = tunnel_get (&msg->oid, msg->tid);
1996 /* TODO notify that we don't know the tunnel */
2000 /* Message for us? */
2001 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2004 if (NULL == t->client)
2009 peer_info = peer_info_get (&msg->peer_id);
2010 if (NULL == peer_info)
2012 GNUNET_break_op (0);
2015 /* FIXME change state of peer */
2016 send_client_peer_connected(t, peer_info->id);
2020 peer_info = peer_info_get (&msg->oid);
2021 if (NULL == peer_info)
2023 /* If we know the tunnel, we should DEFINITELY know the peer */
2027 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2028 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2029 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2030 GNUNET_TIME_UNIT_FOREVER_REL,
2031 path_get_first_hop (t->tree,
2033 sizeof (struct GNUNET_MESH_PathACK),
2034 &send_core_data_raw, msg);
2040 * Functions to handle messages from core
2042 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2043 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2044 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2045 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2046 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2047 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2048 sizeof (struct GNUNET_MESH_PathACK)},
2054 /******************************************************************************/
2055 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2056 /******************************************************************************/
2059 * deregister_app: iterator for removing each application registered by a client
2061 * @param cls closure
2062 * @param key the hash of the application id (used to access the hashmap)
2063 * @param value the value stored at the key (client)
2065 * @return GNUNET_OK on success
2068 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2070 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2076 * notify_client_connection_failure: notify a client that the connection to the
2077 * requested remote peer is not possible (for instance, no route found)
2078 * Function called when the socket is ready to queue more data. "buf" will be
2079 * NULL and "size" zero if the socket was closed for writing in the meantime.
2081 * @param cls closure
2082 * @param size number of bytes available in buf
2083 * @param buf where the callee should write the message
2084 * @return number of bytes written to buf
2087 notify_client_connection_failure (void *cls, size_t size, void *buf)
2090 struct MeshPeerInfo *peer_info;
2091 struct GNUNET_MESH_PeerControl *msg;
2092 struct GNUNET_PeerIdentity id;
2094 if (0 == size && NULL == buf)
2096 // TODO retry? cancel?
2100 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2101 peer_info = (struct MeshPeerInfo *) cls;
2102 msg = (struct GNUNET_MESH_PeerControl *) buf;
2103 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2104 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2105 // msg->tunnel_id = htonl(peer_info->t->tid);
2106 GNUNET_PEER_resolve (peer_info->id, &id);
2107 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2115 * Function to process paths received for a new peer addition. The recorded
2116 * paths form the initial tunnel, which can be optimized later.
2117 * Called on each result obtained for the DHT search.
2119 * @param cls closure
2120 * @param exp when will this value expire
2121 * @param key key of the result
2122 * @param get_path NULL-terminated array of pointers
2123 * to the peers on reverse GET path (or NULL if not recorded)
2124 * @param put_path NULL-terminated array of pointers
2125 * to the peers on the PUT path (or NULL if not recorded)
2126 * @param type type of the result
2127 * @param size number of bytes in data
2128 * @param data pointer to the result data
2130 * TODO: re-issue the request after certain time? cancel after X results?
2133 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2134 const GNUNET_HashCode * key,
2135 const struct GNUNET_PeerIdentity *const *get_path,
2136 const struct GNUNET_PeerIdentity *const *put_path,
2137 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2139 struct MeshPathInfo *path_info = cls;
2140 struct MeshPeerPath *p;
2141 struct GNUNET_PeerIdentity pi;
2144 if (NULL == get_path || NULL == put_path)
2146 if (NULL == path_info->peer->path_head)
2148 // Find ourselves some alternate initial path to the destination: retry
2149 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2150 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2151 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2152 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2153 GNUNET_BLOCK_TYPE_TEST, /* type */
2154 &pi.hashPubKey, /*key to search */
2155 4, /* replication level */
2156 GNUNET_DHT_RO_RECORD_ROUTE, NULL, /* bloom filter */
2159 0, /* xquery bits */
2161 (void *) path_info);
2166 p = path_build_from_dht (get_path, put_path);
2167 path_add_to_peer (path_info->peer, p);
2168 for (i = 0; i < path_info->peer->ntunnels; i++)
2170 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2172 GNUNET_free (path_info);
2179 * Function to process paths received for a new peer addition. The recorded
2180 * paths form the initial tunnel, which can be optimized later.
2181 * Called on each result obtained for the DHT search.
2183 * @param cls closure
2184 * @param exp when will this value expire
2185 * @param key key of the result
2186 * @param get_path NULL-terminated array of pointers
2187 * to the peers on reverse GET path (or NULL if not recorded)
2188 * @param put_path NULL-terminated array of pointers
2189 * to the peers on the PUT path (or NULL if not recorded)
2190 * @param type type of the result
2191 * @param size number of bytes in data
2192 * @param data pointer to the result data
2195 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2196 const GNUNET_HashCode * key,
2197 const struct GNUNET_PeerIdentity *const *get_path,
2198 const struct GNUNET_PeerIdentity *const *put_path,
2199 enum GNUNET_BLOCK_Type type, size_t size,
2202 const struct GNUNET_PeerIdentity *pi = data;
2203 struct GNUNET_PeerIdentity id;
2204 struct MeshTunnel *t = cls;
2205 struct MeshPeerInfo *peer_info;
2206 struct MeshPathInfo *path_info;
2207 struct MeshPeerPath *p;
2210 if (size != sizeof (struct GNUNET_PeerIdentity))
2212 GNUNET_break_op (0);
2215 GNUNET_assert (NULL != t->client);
2216 GNUNET_DHT_get_stop (t->client->dht_get_type);
2217 t->client->dht_get_type = NULL;
2218 peer_info = peer_info_get (pi);
2219 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2220 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2222 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2223 NULL == peer_info->dhtget)
2225 /* we don't have a route to the peer, let's try a direct lookup */
2226 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2228 GNUNET_TIME_UNIT_FOREVER_REL,
2230 GNUNET_BLOCK_TYPE_TEST,
2233 /* key to look up */
2235 /* replication level */
2236 GNUNET_DHT_RO_RECORD_ROUTE,
2237 /* option to dht: record route */
2238 NULL, /* bloom filter */
2241 0, /* xquery bits */
2244 peer_info); /* closure */
2247 p = path_build_from_dht (get_path, put_path);
2248 path_add_to_peer (peer_info, p);
2249 tunnel_add_peer(t, peer_info);
2250 p = tree_get_path_to_peer(t->tree, peer_info->id);
2252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2253 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2254 t->local_tid, p->length);
2255 for (i = 0; i < p->length; i++)
2257 GNUNET_PEER_resolve (p->peers[0], &id);
2258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2259 GNUNET_h2s_full (&id.hashPubKey));
2265 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2267 path_info->peer = peer_info;
2268 path_info->path = p;
2269 GNUNET_PEER_resolve (p->peers[1], &id);
2270 GNUNET_CORE_notify_transmit_ready (core_handle,
2276 GNUNET_TIME_UNIT_FOREVER_REL,
2280 sizeof (struct GNUNET_MESH_ManipulatePath)
2283 sizeof (struct GNUNET_PeerIdentity)),
2285 &send_core_create_path,
2287 path_info); /* cls */
2291 send_client_peer_connected(t, myid);
2294 /******************************************************************************/
2295 /********************* MESH LOCAL HANDLES **************************/
2296 /******************************************************************************/
2300 * Handler for client disconnection
2302 * @param cls closure
2303 * @param client identification of the client; NULL
2304 * for the last call when the server is destroyed
2307 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2309 struct MeshClient *c;
2310 struct MeshClient *next;
2312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2318 if (c->handle != client && NULL != client)
2320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2325 if (NULL != c->tunnels)
2327 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2328 &tunnel_destroy_iterator,
2330 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2333 /* deregister clients applications */
2334 if (NULL != c->apps)
2336 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2337 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2339 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2340 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2342 GNUNET_SCHEDULER_cancel (announce_applications_task);
2343 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2345 if (NULL != c->types)
2346 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2347 if (NULL != c->dht_get_type)
2348 GNUNET_DHT_get_stop (c->dht_get_type);
2349 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2361 * Handler for new clients
2363 * @param cls closure
2364 * @param client identification of the client
2365 * @param message the actual message, which includes messages the client wants
2368 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2369 const struct GNUNET_MessageHeader *message)
2371 struct GNUNET_MESH_ClientConnect *cc_msg;
2372 struct MeshClient *c;
2373 GNUNET_MESH_ApplicationType *a;
2380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2381 /* Check data sanity */
2382 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2383 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2384 ntypes = ntohs (cc_msg->types);
2385 napps = ntohs (cc_msg->applications);
2387 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2390 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2394 /* Create new client structure */
2395 c = GNUNET_malloc (sizeof (struct MeshClient));
2397 c->id = next_client_id++;
2400 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2403 GNUNET_MESH_ApplicationType at;
2406 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2407 for (i = 0; i < napps; i++)
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2411 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2412 /* store in clients hashmap */
2413 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2414 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2415 /* store in global hashmap, for announcements */
2416 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2417 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2419 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2420 announce_applications_task =
2421 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2429 t = (uint16_t *) & a[napps];
2430 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2431 for (i = 0; i < ntypes; i++)
2434 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2436 /* store in clients hashmap */
2437 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2438 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2439 /* store in global hashmap */
2440 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2441 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2445 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2447 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2448 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2449 GNUNET_SERVER_notification_context_add (nc, client);
2451 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2459 * Handler for requests of new tunnels
2461 * @param cls closure
2462 * @param client identification of the client
2463 * @param message the actual message
2466 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2467 const struct GNUNET_MessageHeader *message)
2469 struct GNUNET_MESH_TunnelMessage *t_msg;
2470 struct MeshTunnel *t;
2471 struct MeshClient *c;
2472 GNUNET_HashCode hash;
2474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2476 /* Sanity check for client registration */
2477 if (NULL == (c = client_get (client)))
2480 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2487 /* Message sanity check */
2488 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2491 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2495 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2496 /* Sanity check for tunnel numbering */
2497 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2500 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2503 /* Sanity check for duplicate tunnel IDs */
2504 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2507 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2511 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2513 while (NULL != tunnel_get_by_pi (myid, next_tid))
2514 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2515 t->id.tid = next_tid++;
2517 t->local_tid = ntohl (t_msg->tunnel_id);
2519 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2521 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2523 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2524 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2527 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2531 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2533 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2534 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2537 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2540 t->tree = tree_new (t, myid);
2541 t->tree->refresh = REFRESH_PATH_TIME;
2542 t->tree->root->status = MESH_PEER_READY;
2543 t->tree->me = t->tree->root;
2545 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2551 * Handler for requests of deleting tunnels
2553 * @param cls closure
2554 * @param client identification of the client
2555 * @param message the actual message
2558 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2559 const struct GNUNET_MessageHeader *message)
2561 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2562 struct MeshClient *c;
2563 struct MeshTunnel *t;
2564 MESH_TunnelNumber tid;
2565 GNUNET_HashCode hash;
2567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2569 /* Sanity check for client registration */
2570 if (NULL == (c = client_get (client)))
2573 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2576 /* Message sanity check */
2577 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2580 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2586 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2588 /* Retrieve tunnel */
2589 tid = ntohl (tunnel_msg->tunnel_id);
2591 /* Remove from local id hashmap */
2592 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2593 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2594 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2596 /* Remove from global id hashmap */
2597 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2598 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2600 // notify_tunnel_destroy(t); FIXME
2602 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2608 * Handler for connection requests to new peers
2610 * @param cls closure
2611 * @param client identification of the client
2612 * @param message the actual message (PeerControl)
2615 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2616 const struct GNUNET_MessageHeader *message)
2618 struct GNUNET_MESH_PeerControl *peer_msg;
2619 struct MeshClient *c;
2620 struct MeshTunnel *t;
2621 MESH_TunnelNumber tid;
2622 struct MeshPeerInfo *peer_info;
2625 /* Sanity check for client registration */
2626 if (NULL == (c = client_get (client)))
2629 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2633 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2634 /* Sanity check for message size */
2635 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2638 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2642 /* Tunnel exists? */
2643 tid = ntohl (peer_msg->tunnel_id);
2644 t = tunnel_get_by_local_id (c, tid);
2648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2652 /* Does client own tunnel? */
2653 if (t->client->handle != client)
2656 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2660 peer_info = peer_info_get (&peer_msg->peer);
2662 /* Start DHT search if needed, otherwise just add peer to tunnel. */
2663 if (NULL == peer_info->dhtget && NULL == peer_info->path_head)
2665 struct MeshPathInfo *path_info;
2667 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2668 path_info->peer = peer_info;
2670 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
2671 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2672 GNUNET_BLOCK_TYPE_TEST, /* type */
2673 &peer_msg->peer.hashPubKey, /*key to search */
2674 4, /* replication level */
2675 GNUNET_DHT_RO_RECORD_ROUTE,
2676 NULL, /* bloom filter */
2679 0, /* xquery bits */
2681 (void *) path_info);
2683 if (NULL != peer_info->path_head)
2685 tunnel_add_peer(t, peer_info);
2687 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2693 * Handler for disconnection requests of peers in a tunnel
2695 * @param cls closure
2696 * @param client identification of the client
2697 * @param message the actual message (PeerControl)
2700 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2701 const struct GNUNET_MessageHeader *message)
2703 struct GNUNET_MESH_PeerControl *peer_msg;
2704 struct MeshClient *c;
2705 struct MeshTunnel *t;
2706 MESH_TunnelNumber tid;
2708 /* Sanity check for client registration */
2709 if (NULL == (c = client_get (client)))
2712 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2715 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2716 /* Sanity check for message size */
2717 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2720 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2724 /* Tunnel exists? */
2725 tid = ntohl (peer_msg->tunnel_id);
2726 t = tunnel_get_by_local_id (c, tid);
2730 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2734 /* Does client own tunnel? */
2735 if (t->client->handle != client)
2738 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2742 /* Ok, delete peer from tunnel */
2743 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2744 &peer_msg->peer.hashPubKey);
2746 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2752 * Handler for connection requests to new peers by type
2754 * @param cls closure
2755 * @param client identification of the client
2756 * @param message the actual message (ConnectPeerByType)
2759 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2760 const struct GNUNET_MessageHeader *message)
2762 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2763 struct MeshClient *c;
2764 struct MeshTunnel *t;
2765 GNUNET_HashCode hash;
2766 GNUNET_MESH_ApplicationType type;
2767 MESH_TunnelNumber tid;
2769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2770 /* Sanity check for client registration */
2771 if (NULL == (c = client_get (client)))
2774 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2778 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2779 /* Sanity check for message size */
2780 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2781 ntohs (connect_msg->header.size))
2784 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2788 /* Tunnel exists? */
2789 tid = ntohl (connect_msg->tunnel_id);
2790 t = tunnel_get_by_local_id (c, tid);
2794 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2798 /* Does client own tunnel? */
2799 if (t->client->handle != client)
2802 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2806 /* Do WE have the service? */
2807 type = ntohl (connect_msg->type);
2808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2809 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2810 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2813 /* Yes! Fast forward, add ourselves to the tunnel and send the
2814 * good news to the client
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2817 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2818 peer_info_get (&my_full_id),
2819 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2822 send_client_peer_connected(t, myid);
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2824 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2827 /* Ok, lets find a peer offering the service */
2828 if (c->dht_get_type)
2830 GNUNET_DHT_get_stop (c->dht_get_type);
2832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2833 GNUNET_h2s_full (&hash));
2835 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2836 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2837 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0, NULL, 0,
2838 &dht_get_type_handler, t);
2840 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2846 * Handler for client traffic directed to one peer
2848 * @param cls closure
2849 * @param client identification of the client
2850 * @param message the actual message
2853 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2854 const struct GNUNET_MessageHeader *message)
2856 struct MeshClient *c;
2857 struct MeshTunnel *t;
2858 struct MeshPeerInfo *pi;
2859 struct GNUNET_MESH_Unicast *data_msg;
2860 struct MeshDataDescriptor *info;
2861 MESH_TunnelNumber tid;
2864 /* Sanity check for client registration */
2865 if (NULL == (c = client_get (client)))
2868 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2871 data_msg = (struct GNUNET_MESH_Unicast *) message;
2872 /* Sanity check for message size */
2873 if (sizeof (struct GNUNET_MESH_Unicast) +
2874 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
2877 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2881 /* Tunnel exists? */
2882 tid = ntohl (data_msg->tid);
2883 t = tunnel_get_by_local_id (c, tid);
2887 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2891 /* Is it a local tunnel? Then, does client own the tunnel? */
2892 if (t->client->handle != NULL && t->client->handle != client)
2895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2899 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2900 &data_msg->destination.hashPubKey);
2901 /* Is the selected peer in the tunnel? */
2905 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2910 struct GNUNET_MESH_Unicast copy;
2912 /* Work around const limitation */
2913 memcpy (©, data_msg, sizeof (struct GNUNET_MESH_Unicast));
2914 copy.oid = my_full_id;
2915 copy.tid = htonl (t->id.tid);
2916 handle_mesh_data_unicast (NULL, &my_full_id, ©.header, NULL);
2919 data_size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Unicast);
2920 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor) + data_size);
2921 memcpy (&info[1], &data_msg[1], data_size);
2922 info->destination = pi->id;
2923 info->origin = &t->id;
2924 info->size = data_size;
2925 info->client = client;
2926 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2927 GNUNET_TIME_UNIT_FOREVER_REL,
2928 path_get_first_hop (t->tree, pi->id),
2929 /* FIXME re-check types */
2931 sizeof (struct GNUNET_MESH_Unicast),
2932 &send_core_data_unicast, info);
2937 * Handler for client traffic directed to all peers in a tunnel
2939 * @param cls closure
2940 * @param client identification of the client
2941 * @param message the actual message
2944 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2945 const struct GNUNET_MessageHeader *message)
2947 struct MeshClient *c;
2948 struct MeshTunnel *t;
2949 struct GNUNET_MESH_Multicast *data_msg;
2950 MESH_TunnelNumber tid;
2952 /* Sanity check for client registration */
2953 if (NULL == (c = client_get (client)))
2956 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2959 data_msg = (struct GNUNET_MESH_Multicast *) message;
2960 /* Sanity check for message size */
2961 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2964 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2968 /* Tunnel exists? */
2969 tid = ntohl (data_msg->tid);
2970 t = tunnel_get_by_local_id (c, tid);
2974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2978 /* Does client own tunnel? */
2979 if (t->client->handle != client)
2982 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2988 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2993 * Functions to handle messages from clients
2995 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
2996 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2997 {&handle_local_tunnel_create, NULL,
2998 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2999 sizeof (struct GNUNET_MESH_TunnelMessage)},
3000 {&handle_local_tunnel_destroy, NULL,
3001 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3002 sizeof (struct GNUNET_MESH_TunnelMessage)},
3003 {&handle_local_connect_add, NULL,
3004 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3005 sizeof (struct GNUNET_MESH_PeerControl)},
3006 {&handle_local_connect_del, NULL,
3007 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3008 sizeof (struct GNUNET_MESH_PeerControl)},
3009 {&handle_local_connect_by_type, NULL,
3010 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3011 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3012 {&handle_local_unicast, NULL,
3013 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3014 {&handle_local_unicast, NULL,
3015 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3016 {&handle_local_multicast, NULL,
3017 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3023 * To be called on core init/fail.
3025 * @param cls service closure
3026 * @param server handle to the server for this service
3027 * @param identity the public identity of this peer
3028 * @param publicKey the public key of this peer
3031 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3032 const struct GNUNET_PeerIdentity *identity,
3033 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
3035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3036 core_handle = server;
3037 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3039 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3040 GNUNET_SCHEDULER_shutdown();
3046 * Method called whenever a given peer connects.
3048 * @param cls closure
3049 * @param peer peer identity this notification is about
3050 * @param atsi performance data for the connection
3053 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3054 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3056 // GNUNET_PEER_Id pid;
3057 struct MeshPeerInfo *peer_info;
3058 struct MeshPeerPath *path;
3060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3061 peer_info = peer_info_get (peer);
3062 if (myid == peer_info->id)
3064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3067 path = path_new (2);
3068 path->peers[0] = myid;
3069 path->peers[1] = peer_info->id;
3070 path_add_to_peer (peer_info, path);
3075 * Method called whenever a peer disconnects.
3077 * @param cls closure
3078 * @param peer peer identity this notification is about
3081 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3083 struct MeshPeerInfo *pi;
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3087 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3093 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3095 if (pi->core_transmit[i])
3097 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3098 /* TODO: notify that tranmission has failed */
3099 GNUNET_free (pi->infos[i]);
3102 path_remove_from_peer (pi, pi->id, myid);
3105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3111 /******************************************************************************/
3112 /************************ MAIN FUNCTIONS ****************************/
3113 /******************************************************************************/
3116 * Task run during shutdown.
3122 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3124 struct MeshClient *c;
3126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3127 if (core_handle != NULL)
3129 GNUNET_CORE_disconnect (core_handle);
3132 if (dht_handle != NULL)
3134 for (c = clients; NULL != c; c = c->next)
3135 if (NULL != c->dht_get_type)
3136 GNUNET_DHT_get_stop (c->dht_get_type);
3137 GNUNET_DHT_disconnect (dht_handle);
3142 GNUNET_SERVER_notification_context_destroy (nc);
3145 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3147 GNUNET_SCHEDULER_cancel (announce_id_task);
3148 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3154 * Process mesh requests.
3156 * @param cls closure
3157 * @param server the initialized server
3158 * @param c configuration to use
3161 run (void *cls, struct GNUNET_SERVER_Handle *server,
3162 const struct GNUNET_CONFIGURATION_Handle *c)
3164 struct MeshPeerInfo *peer;
3165 struct MeshPeerPath *p;
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3169 server_handle = server;
3170 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3171 CORE_QUEUE_SIZE, /* queue size */
3172 NULL, /* Closure passed to MESH functions */
3173 &core_init, /* Call core_init once connected */
3174 &core_connect, /* Handle connects */
3175 &core_disconnect, /* remove peers on disconnects */
3176 NULL, /* Do we care about "status" updates? */
3177 NULL, /* Don't notify about all incoming messages */
3178 GNUNET_NO, /* For header only in notification */
3179 NULL, /* Don't notify about all outbound messages */
3180 GNUNET_NO, /* For header-only out notification */
3181 core_handlers); /* Register these handlers */
3182 if (core_handle == NULL)
3185 GNUNET_SCHEDULER_shutdown ();
3190 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3195 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3196 GNUNET_SCHEDULER_shutdown ();
3199 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3200 GNUNET_free (keyfile);
3201 if (my_private_key == NULL)
3203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3204 _("Mesh service could not access hostkey. Exiting.\n"));
3205 GNUNET_SCHEDULER_shutdown ();
3208 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3209 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3210 &my_full_id.hashPubKey);
3211 myid = GNUNET_PEER_intern (&my_full_id);
3213 dht_handle = GNUNET_DHT_connect (c, 64);
3214 if (dht_handle == NULL)
3216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3217 Running without DHT has a severe\
3218 impact in MESH capabilities.\n\
3219 Plase check your configuretion and enable DHT.\n");
3225 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3226 peers = GNUNET_CONTAINER_multihashmap_create (32);
3227 applications = GNUNET_CONTAINER_multihashmap_create (32);
3228 types = GNUNET_CONTAINER_multihashmap_create (32);
3230 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3231 nc = GNUNET_SERVER_notification_context_create (server_handle,
3233 GNUNET_SERVER_disconnect_notify (server_handle,
3234 &handle_local_client_disconnect,
3239 clients_tail = NULL;
3244 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3245 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3247 /* Create a peer_info for the local peer */
3248 peer = peer_info_get(&my_full_id);
3251 path_add_to_peer(peer, p);
3253 /* Scheduled the task to clean up when shutdown is called */
3254 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3261 * The main function for the mesh service.
3263 * @param argc number of arguments from the command line
3264 * @param argv command line arguments
3265 * @return 0 ok, 1 on error
3268 main (int argc, char *const *argv)
3273 fprintf (stderr, "main ()\n");
3275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3278 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3282 fprintf (stderr, "main () END\n");