2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - add connection confirmation message
44 * - handle trnsmt_rdy return values
49 #include "mesh_protocol.h"
50 #include "gnunet_dht_service.h"
51 #include "mesh_tunnel_tree.h"
53 /* TODO: move into configuration file */
54 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
55 GNUNET_TIME_UNIT_SECONDS,\
57 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
58 GNUNET_TIME_UNIT_SECONDS,\
61 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
62 GNUNET_TIME_UNIT_SECONDS,\
66 /******************************************************************************/
67 /************************ DATA STRUCTURES ****************************/
68 /******************************************************************************/
70 /** FWD declaration */
74 * Struct containing all info possibly needed to build a package when called
77 struct MeshDataDescriptor
79 /** ID of the tunnel this packet travels in */
80 struct MESH_TunnelID *origin;
85 /** Client that asked for the transmission, if any */
86 struct GNUNET_SERVER_Client *client;
88 /** Who was this message being sent to */
89 struct MeshPeerInfo *peer;
91 /** Ultimate destination of the packet */
92 GNUNET_PEER_Id destination;
94 /** Number of identical messages sent to different hops (multicast) */
97 /** Which handler was used to request the transmission */
98 unsigned int handler_n;
100 /** Size of the data */
107 * Struct containing all information regarding a given peer
117 * Last time we heard from this peer
119 struct GNUNET_TIME_Absolute last_contact;
122 * Number of attempts to reconnect so far
124 int n_reconnect_attempts;
127 * Paths to reach the peer, ordered by ascending hop count
129 struct MeshPeerPath *path_head;
132 * Paths to reach the peer, ordered by ascending hop count
134 struct MeshPeerPath *path_tail;
137 * Handle to stop the DHT search for a path to this peer
139 struct GNUNET_DHT_GetHandle *dhtget;
142 * Handles to stop queued transmissions for this peer
144 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
147 * Pointer to info stuctures used as cls for queued transmissions
149 struct MeshDataDescriptor *infos[CORE_QUEUE_SIZE];
152 * Array of tunnels this peer participates in
153 * (most probably a small amount, therefore not a hashmap)
154 * When the path to the peer changes, notify these tunnels to let them
155 * re-adjust their path trees.
157 struct MeshTunnel **tunnels;
160 * Number of tunnels above
162 unsigned int ntunnels;
167 * Data scheduled to transmit (to local client or remote peer)
174 struct MeshQueue *next;
175 struct MeshQueue *prev;
178 * Target of the data (NULL if target is client)
180 struct MeshPeerInfo *peer;
183 * Client to send the data to (NULL if target is peer)
185 struct MeshClient *client;
188 * Size of the message to transmit
193 * How old is the data?
195 struct GNUNET_TIME_Absolute timestamp;
200 struct GNUNET_MessageHeader *data;
204 * Globally unique tunnel identification (owner + number)
205 * DO NOT USE OVER THE NETWORK
210 * Node that owns the tunnel
215 * Tunnel number to differentiate all the tunnels owned by the node oid
216 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
218 MESH_TunnelNumber tid;
222 struct MeshClient; /* FWD declaration */
225 * Struct containing all information regarding a tunnel
226 * For an intermediate node the improtant info used will be:
227 * - id Tunnel unique identification
228 * - paths[0] To know where to send it next
229 * - metainfo: ready, speeds, accounting
236 struct MESH_TunnelID id;
239 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
241 MESH_TunnelNumber local_tid;
244 * Last time the tunnel was used
246 struct GNUNET_TIME_Absolute timestamp;
249 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
251 struct GNUNET_CONTAINER_MultiHashMap *peers;
254 * Number of peers that are connected and potentially ready to receive data
256 unsigned int peers_ready;
259 * Number of peers that have been added to the tunnel
261 unsigned int peers_total;
264 * Client owner of the tunnel, if any
266 struct MeshClient *client;
269 * Messages ready to transmit
271 struct MeshQueue *queue_head;
272 struct MeshQueue *queue_tail;
277 struct MeshTunnelTree *tree;
280 * Task to keep the used paths alive
282 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
287 * Info needed to work with tunnel paths and peers
294 struct MeshTunnel *t;
299 struct MeshPeerInfo *peer;
304 struct MeshPeerPath *path;
309 * Struct containing information about a client of the service
316 struct MeshClient *next;
317 struct MeshClient *prev;
320 * Tunnels that belong to this client, indexed by local id
322 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
325 * Handle to communicate with the client
327 struct GNUNET_SERVER_Client *handle;
330 * Applications that this client has claimed to provide
332 struct GNUNET_CONTAINER_MultiHashMap *apps;
335 * Messages that this client has declared interest in
337 struct GNUNET_CONTAINER_MultiHashMap *types;
340 * Used to search peers offering a service
342 struct GNUNET_DHT_GetHandle *dht_get_type;
346 * ID of the client, for debug messages
355 /******************************************************************************/
356 /************************ DEBUG FUNCTIONS ****************************/
357 /******************************************************************************/
361 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
362 * @param cls string to print
363 * @param tc task context
366 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
370 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
379 /******************************************************************************/
380 /*********************** GLOBAL VARIABLES ****************************/
381 /******************************************************************************/
386 static struct MeshClient *clients;
387 static struct MeshClient *clients_tail;
390 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
392 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
395 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
397 static struct GNUNET_CONTAINER_MultiHashMap *peers;
400 * Handle to communicate with core
402 static struct GNUNET_CORE_Handle *core_handle;
407 static struct GNUNET_DHT_Handle *dht_handle;
412 static struct GNUNET_SERVER_Handle *server_handle;
415 * Notification context, to send messages to local clients
417 static struct GNUNET_SERVER_NotificationContext *nc;
420 * Local peer own ID (memory efficient handle)
422 static GNUNET_PEER_Id myid;
425 * Local peer own ID (full value)
427 static struct GNUNET_PeerIdentity my_full_id;
432 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
437 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
440 * Tunnel ID for the next created tunnel (global tunnel number)
442 static MESH_TunnelNumber next_tid;
445 * All application types provided by this peer
447 static struct GNUNET_CONTAINER_MultiHashMap *applications;
450 * All message types clients of this peer are interested in
452 static struct GNUNET_CONTAINER_MultiHashMap *types;
455 * Task to periodically announce provided applications
457 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
460 * Task to periodically announce itself in the network
462 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
465 unsigned int next_client_id;
469 /******************************************************************************/
470 /************************ ITERATORS ****************************/
471 /******************************************************************************/
473 /* FIXME move iterators here */
476 /******************************************************************************/
477 /************************ PERIODIC FUNCTIONS ****************************/
478 /******************************************************************************/
481 * Announce iterator over for each application provided by the peer
484 * @param key current key code
485 * @param value value in the hash map
486 * @return GNUNET_YES if we should continue to
491 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
493 /* FIXME are hashes in multihash map equal on all aquitectures? */
494 GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
495 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
496 (const char *) &my_full_id,
498 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
499 &mesh_debug, "DHT_put for app completed");
501 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
503 APP_ANNOUNCE_TIME, NULL, NULL);
510 * Periodically announce what applications are provided by local clients
513 * @param tc task context
516 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
518 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
520 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
524 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
526 announce_applications_task =
527 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
535 * Periodically announce self id in the DHT
538 * @param tc task context
541 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
543 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
545 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
549 * - Set data expiration in function of X
552 GNUNET_DHT_put (dht_handle, /* DHT handle */
553 &my_full_id.hashPubKey, /* Key to use */
554 10U, /* Replication level */
555 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
556 GNUNET_BLOCK_TYPE_TEST, /* Block type */
557 0, /* Size of the data */
558 NULL, /* Data itself */
559 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
560 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
562 &mesh_debug, "DHT_put for id completed");
564 NULL, /* Continuation */
565 NULL); /* Continuation closure */
568 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
573 * Function to process paths received for a new peer addition. The recorded
574 * paths form the initial tunnel, which can be optimized later.
575 * Called on each result obtained for the DHT search.
578 * @param exp when will this value expire
579 * @param key key of the result
580 * @param type type of the result
581 * @param size number of bytes in data
582 * @param data pointer to the result data
585 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
586 const GNUNET_HashCode * key,
587 const struct GNUNET_PeerIdentity *get_path,
588 unsigned int get_path_length,
589 const struct GNUNET_PeerIdentity *put_path,
590 unsigned int put_path_length,
591 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
594 /******************************************************************************/
595 /****************** GENERAL HELPER FUNCTIONS ************************/
596 /******************************************************************************/
599 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
600 * and insert it in the appropiate structures if the peer is not known yet.
602 * @param peer Identity of the peer
604 * @return Existing or newly created peer info
606 static struct MeshPeerInfo *
607 peer_info_get (const struct GNUNET_PeerIdentity *peer)
609 struct MeshPeerInfo *peer_info;
611 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
612 if (NULL == peer_info)
615 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
616 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
617 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
618 peer_info->id = GNUNET_PEER_intern (peer);
627 * Destroy the peer_info and free any allocated resources linked to it
628 * @param t tunnel the path belongs to
629 * @param pi the peer_info to destroy
630 * @return GNUNET_OK on success
633 peer_info_destroy (struct MeshPeerInfo *pi)
635 GNUNET_HashCode hash;
636 struct GNUNET_PeerIdentity id;
638 GNUNET_PEER_resolve (pi->id, &id);
639 GNUNET_PEER_change_rc (pi->id, -1);
640 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
642 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
643 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
651 * Notify a tunnel that a connection has broken that affects at least
654 * @param t Tunnel affected.
655 * @param peer Peer that (at least) has been affected by the disconnection.
656 * @param p1 Peer that got disconnected from p2.
657 * @param p2 Peer that got disconnected from p1.
659 * @return Short ID of the peer disconnected (either p1 or p2).
660 * 0 if the tunnel remained unaffected.
662 static GNUNET_PEER_Id
663 tunnel_notify_connection_broken (struct MeshTunnel *t,
664 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
668 * Remove all paths that rely on a direct connection between p1 and p2
669 * from the peer itself and notify all tunnels about it.
671 * @param peer PeerInfo of affected peer.
672 * @param p1 GNUNET_PEER_Id of one peer.
673 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
676 * TODO: optimize (see below)
679 path_remove_from_peer (struct MeshPeerInfo *peer,
683 struct GNUNET_PeerIdentity id;
684 struct MeshPeerPath *p;
685 struct MeshPeerPath *aux;
686 struct MeshPeerInfo *peer_d;
688 unsigned int destroyed;
698 for (i = 0; i < (p->length - 1); i++)
700 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
701 (p->peers[i] == p2 && p->peers[i + 1] == p1))
713 for (i = 0; i < peer->ntunnels; i++)
715 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
717 * Problem: one or more peers have been deleted from the tunnel tree.
718 * We don't know who they are to try to add them again.
719 * We need to try to find a new path for each of the disconnected peers.
720 * Some of them might already have a path to reach them that does not
721 * involve p1 and p2. Adding all anew might render in a better tree than
722 * the trivial immediate fix.
724 * Trivial immiediate fix: try to reconnect to the disconnected node. All
725 * its children will be reachable trough him.
727 GNUNET_PEER_resolve(d, &id);
728 peer_d = peer_info_get(&id);
731 for (p = peer_d->path_head; NULL != p; p = p->next)
733 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
741 /* No callback, as peer will be already disconnected */
742 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
746 struct MeshPathInfo *path_info;
748 if (NULL != peer_d->dhtget)
750 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
752 path_info->peer = peer_d;
753 path_info->t = peer->tunnels[i];
754 peer_d->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
755 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
756 GNUNET_BLOCK_TYPE_TEST, /* type */
757 &id.hashPubKey, /*key to search */
758 4, /* replication level */
759 GNUNET_DHT_RO_RECORD_ROUTE,
770 * Add the path to the peer and update the path used to reach it in case this
773 * @param peer_info Destination peer to add the path to.
774 * @param path New path to add. Last peer must be the peer in arg 1.
775 * Path will be either used of freed if already known.
777 * TODO: trim the part from origin to us? Add it as path to origin?
780 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
782 struct MeshPeerPath *aux;
786 if (NULL == peer_info || NULL == path)
792 l = path_get_length (path);
794 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
796 l2 = path_get_length (aux);
799 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
800 peer_info->path_tail, aux, path);
804 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
811 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
818 * Add the path to the origin peer and update the path used to reach it in case
819 * this is the shortest.
820 * The path is given in peer_info -> destination, therefore we turn the path
823 * @param peer_info Peer to add the path to, being the origin of the path.
824 * @param path New path to add after being inversed.
827 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
830 path_add_to_peer (peer_info, path);
835 * Build a PeerPath from the paths returned from the DHT, reversing the paths
836 * to obtain a local peer -> destination path and interning the peer ids.
838 * @return Newly allocated and created path
840 static struct MeshPeerPath *
841 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
842 unsigned int get_path_length,
843 const struct GNUNET_PeerIdentity *put_path,
844 unsigned int put_path_length)
846 struct MeshPeerPath *p;
852 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
853 for (i--; i >= 0; i--)
855 id = GNUNET_PEER_intern (&get_path[i]);
856 if (p->length > 0 && id == p->peers[p->length - 1])
858 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
859 GNUNET_PEER_change_rc(id, -1);
864 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
865 p->peers[p->length] = id;
870 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
871 for (i--; i >= 0; i--)
873 id = GNUNET_PEER_intern (&put_path[i]);
874 if (p->length > 0 && id == p->peers[p->length - 1])
876 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
877 GNUNET_PEER_change_rc(id, -1);
882 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
883 p->peers[p->length] = id;
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "MESH: (first of GET: %s)\n",
890 GNUNET_h2s_full(&get_path[0].hashPubKey));
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 "MESH: (first of PUT: %s)\n",
893 GNUNET_h2s_full(&put_path[0].hashPubKey));
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "MESH: In total: %d hops\n",
903 * Send keepalive packets for a peer
905 * @param cls Closure (tunnel for which to send the keepalive).
906 * @param tc Notification context.
908 * TODO: implement explicit multicast keepalive?
911 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
915 * Check if client has registered with the service and has not disconnected
917 * @param client the client to check
919 * @return non-NULL if client exists in the global DLL
921 static struct MeshClient *
922 client_get (struct GNUNET_SERVER_Client *client)
924 struct MeshClient *c;
929 if (c->handle == client)
938 * Checks if a given client has subscribed to certain message type
940 * @param message_type Type of message to check
941 * @param c Client to check
943 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
948 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
952 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
953 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
958 * Search for a tunnel among the tunnels for a client
960 * @param c the client whose tunnels to search in
961 * @param tid the local id of the tunnel
963 * @return tunnel handler, NULL if doesn't exist
965 static struct MeshTunnel *
966 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
968 GNUNET_HashCode hash;
970 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
971 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
976 * Search for a tunnel by global ID using PEER_ID
978 * @param pi owner of the tunnel
979 * @param tid global tunnel number
981 * @return tunnel handler, NULL if doesn't exist
983 static struct MeshTunnel *
984 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
986 struct MESH_TunnelID id;
987 GNUNET_HashCode hash;
992 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
993 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
998 * Search for a tunnel by global ID using full PeerIdentities
1000 * @param oid owner of the tunnel
1001 * @param tid global tunnel number
1003 * @return tunnel handler, NULL if doesn't exist
1005 static struct MeshTunnel *
1006 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1008 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1013 * Callback used to notify a client owner of a tunnel that a peer has
1014 * disconnected, most likely because of a path change.
1016 * @param n Node in the tree representing the disconnected peer
1019 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1021 struct GNUNET_MESH_PeerControl msg;
1023 if (NULL == n->t->client || NULL == nc)
1026 msg.header.size = htons (sizeof (msg));
1027 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1028 msg.tunnel_id = htonl (n->t->local_tid);
1029 GNUNET_PEER_resolve (n->peer, &msg.peer);
1030 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1031 &msg.header, GNUNET_NO);
1036 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1037 * needed rescources.
1039 * @param t Tunnel we want to add a new peer to
1040 * @param peer PeerInfo of the peer being added
1044 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1046 // struct MeshTunnelTreeNode *n;
1047 struct MeshPeerPath *p;
1048 struct MeshPeerPath *best_p;
1049 unsigned int best_cost;
1052 if (NULL != tree_find_peer(t->tree->root, peer->id))
1054 /* Already have it, nothing to do. */
1059 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1060 if (NULL == (p = peer->path_head))
1067 best_cost = UINT_MAX;
1070 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1077 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1078 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1079 t->path_refresh_task =
1080 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1085 * Notify a tunnel that a connection has broken that affects at least
1086 * some of its peers.
1088 * @param t Tunnel affected.
1089 * @param peer Peer that (at least) has been affected by the disconnection.
1090 * @param p1 Peer that got disconnected from p2.
1091 * @param p2 Peer that got disconnected from p1.
1093 * @return Short ID of the peer disconnected (either p1 or p2).
1094 * 0 if the tunnel remained unaffected.
1096 static GNUNET_PEER_Id
1097 tunnel_notify_connection_broken (struct MeshTunnel *t,
1098 struct MeshPeerInfo *peer,
1102 return tree_notify_connection_broken (t->tree, p1, p2,
1103 ¬ify_peer_disconnected);
1108 * Destroy the tunnel and free any allocated resources linked to it
1110 * @param t the tunnel to destroy
1112 * @return GNUNET_OK on success
1115 tunnel_destroy (struct MeshTunnel *t)
1117 struct MeshClient *c;
1118 struct MeshQueue *q;
1119 struct MeshQueue *qn;
1120 GNUNET_HashCode hash;
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1132 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1133 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1138 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1139 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1143 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1147 if (NULL != q->data)
1148 GNUNET_free (q->data);
1152 /* TODO cancel core transmit ready in case it was active */
1154 tree_destroy(t->tree);
1161 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1162 * client when the client disconnects.
1164 * @param cls closure (client that is disconnecting)
1165 * @param key the hash of the local tunnel id (used to access the hashmap)
1166 * @param value the value stored at the key (tunnel to destroy)
1168 * @return GNUNET_OK on success
1171 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1175 r = tunnel_destroy ((struct MeshTunnel *) value);
1180 /******************************************************************************/
1181 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1182 /******************************************************************************/
1186 * Function called to notify a client about the socket
1187 * being ready to queue more data. "buf" will be
1188 * NULL and "size" zero if the socket was closed for
1189 * writing in the meantime.
1191 * @param cls closure
1192 * @param size number of bytes available in buf
1193 * @param buf where the callee should write the message
1194 * @return number of bytes written to buf
1197 send_core_create_path (void *cls, size_t size, void *buf)
1199 struct MeshPathInfo *info = cls;
1200 struct GNUNET_MESH_ManipulatePath *msg;
1201 struct GNUNET_PeerIdentity *peer_ptr;
1202 struct MeshPeerInfo *peer = info->peer;
1203 struct MeshTunnel *t = info->t;
1204 struct MeshPeerPath *p = info->path;
1209 sizeof (struct GNUNET_MESH_ManipulatePath) +
1210 p->length * sizeof (struct GNUNET_PeerIdentity);
1212 if (size < size_needed || NULL == buf)
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting create path\n");
1215 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1216 GNUNET_TIME_UNIT_FOREVER_REL,
1217 path_get_first_hop (t->tree, peer->id),
1218 size_needed, &send_core_create_path,
1223 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1224 msg->header.size = htons (size_needed);
1225 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1226 msg->tid = ntohl (t->id.tid);
1228 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1229 for (i = 0; i < p->length; i++)
1231 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1243 * Function called to notify a client about the socket
1244 * being ready to queue more data. "buf" will be
1245 * NULL and "size" zero if the socket was closed for
1246 * writing in the meantime.
1248 * @param cls closure (MeshDataDescriptor with all info to build packet)
1249 * @param size number of bytes available in buf
1250 * @param buf where the callee should write the message
1251 * @return number of bytes written to buf
1254 send_core_data_to_origin (void *cls, size_t size, void *buf)
1256 struct MeshDataDescriptor *info = cls;
1257 struct GNUNET_MESH_ToOrigin *msg = buf;
1260 GNUNET_assert (NULL != info);
1261 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1262 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1264 if (total_size > size)
1266 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1267 "not enough buffer to send data to origin\n");
1270 msg->header.size = htons (total_size);
1271 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1272 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1273 msg->tid = htonl (info->origin->tid);
1274 if (0 != info->size)
1276 memcpy (&msg[1], &info[1], info->size);
1278 if (NULL != info->client)
1280 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1289 * Function called to notify a client about the socket
1290 * being ready to queue more data. "buf" will be
1291 * NULL and "size" zero if the socket was closed for
1292 * writing in the meantime.
1294 * @param cls closure (data itself)
1295 * @param size number of bytes available in buf
1296 * @param buf where the callee should write the message
1298 * @return number of bytes written to buf
1303 send_core_data_multicast (void *cls, size_t size, void *buf)
1305 struct MeshDataDescriptor *info = cls;
1306 struct GNUNET_MESH_Multicast *msg = buf;
1309 GNUNET_assert (NULL != info);
1310 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1311 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1315 info->peer->core_transmit[info->handler_n] = NULL;
1316 info->peer->infos[info->handler_n] = NULL;
1318 if (total_size > size)
1325 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1326 msg->header.size = htons (total_size);
1327 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1328 msg->tid = htonl (info->origin->tid);
1329 memcpy (&msg[1], info->data, total_size);
1331 if (0 == --(*info->copies))
1333 if (NULL != info->client)
1335 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1337 GNUNET_free (info->data);
1338 GNUNET_free (info->copies);
1346 * Function called to notify a client about the socket
1347 * being ready to queue more data. "buf" will be
1348 * NULL and "size" zero if the socket was closed for
1349 * writing in the meantime.
1351 * @param cls closure (MeshDataDescriptor)
1352 * @param size number of bytes available in buf
1353 * @param buf where the callee should write the message
1354 * @return number of bytes written to buf
1357 send_core_path_ack (void *cls, size_t size, void *buf)
1359 struct MeshDataDescriptor *info = cls;
1360 struct GNUNET_MESH_PathACK *msg = buf;
1362 GNUNET_assert (NULL != info);
1365 info->peer->core_transmit[info->handler_n] = NULL;
1367 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1372 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1373 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1374 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1375 msg->tid = htonl (info->origin->tid);
1376 msg->peer_id = my_full_id;
1378 /* TODO add signature */
1380 return sizeof (struct GNUNET_MESH_PathACK);
1385 * Function called to notify a client about the socket
1386 * being ready to queue more data. "buf" will be
1387 * NULL and "size" zero if the socket was closed for
1388 * writing in the meantime.
1390 * @param cls closure (data itself)
1391 * @param size number of bytes available in buf
1392 * @param buf where the callee should write the message
1393 * @return number of bytes written to buf
1396 send_core_data_raw (void *cls, size_t size, void *buf)
1398 struct GNUNET_MessageHeader *msg = cls;
1401 GNUNET_assert (NULL != msg);
1402 total_size = ntohs (msg->size);
1404 if (total_size > size)
1409 memcpy (buf, msg, total_size);
1417 * Send another peer a notification to destroy a tunnel
1418 * @param cls The tunnel to destroy
1419 * @param size Size in the buffer
1420 * @param buf Memory where to put the data to transmit
1421 * @return Size of data put in buffer
1424 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1426 struct MeshTunnel *t = cls;
1427 struct MeshClient *c;
1428 struct GNUNET_MESH_TunnelMessage *msg;
1432 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1433 /*FIXME*/ msg->header.size =
1434 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1435 msg->tunnel_id = htonl (t->id.tid);
1437 tunnel_destroy (c, t);
1438 return sizeof (struct GNUNET_MESH_TunnelMessage);
1444 * Send the message to all clients that have subscribed to its type
1446 * @param msg Pointer to the message itself
1447 * @return number of clients this message was sent to
1450 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1452 struct MeshClient *c;
1456 type = ntohs (msg->type);
1457 for (count = 0, c = clients; c != NULL; c = c->next)
1459 if (client_is_subscribed (type, c))
1462 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1472 * Notify the client that owns the tunnel that a peer has connected to it
1474 * @param t Tunnel whose owner to notify
1475 * @param id Short id of the peer that has connected
1478 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1480 struct GNUNET_MESH_PeerControl pc;
1482 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1483 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1484 pc.tunnel_id = htonl (t->local_tid);
1485 GNUNET_PEER_resolve (id, &pc.peer);
1486 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1487 &pc.header, GNUNET_NO);
1491 /******************************************************************************/
1492 /******************** MESH NETWORK HANDLERS **************************/
1493 /******************************************************************************/
1497 * Core handler for path creation
1498 * struct GNUNET_CORE_MessageHandler
1500 * @param cls closure
1501 * @param message message
1502 * @param peer peer identity this notification is about
1503 * @param atsi performance data
1504 * @return GNUNET_OK to keep the connection open,
1505 * GNUNET_SYSERR to close it (signal serious error)
1509 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1510 const struct GNUNET_MessageHeader *message,
1511 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1513 unsigned int own_pos;
1516 MESH_TunnelNumber tid;
1517 struct GNUNET_MESH_ManipulatePath *msg;
1518 struct GNUNET_PeerIdentity *pi;
1519 struct GNUNET_PeerIdentity id;
1520 GNUNET_HashCode hash;
1521 struct MeshPeerPath *path;
1522 struct MeshPeerInfo *dest_peer_info;
1523 struct MeshPeerInfo *orig_peer_info;
1524 struct MeshTunnel *t;
1526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1527 "MESH: Received a MESH path create msg\n");
1528 size = ntohs (message->size);
1529 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1531 GNUNET_break_op (0);
1535 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1536 if (size % sizeof (struct GNUNET_PeerIdentity))
1538 GNUNET_break_op (0);
1541 size /= sizeof (struct GNUNET_PeerIdentity);
1544 GNUNET_break_op (0);
1547 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1549 tid = ntohl (msg->tid);
1550 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1551 t = tunnel_get (pi, tid);
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1556 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1557 t->id.oid = GNUNET_PEER_intern (pi);
1559 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1561 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1563 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1564 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1572 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1573 if (NULL == dest_peer_info)
1575 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1576 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1577 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1579 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1581 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1582 if (NULL == orig_peer_info)
1584 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1585 orig_peer_info->id = GNUNET_PEER_intern (pi);
1586 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1587 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1590 path = path_new (size);
1592 for (i = 0; i < size; i++)
1594 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1595 if (path->peers[i] == myid)
1599 { /* cannot be self, must be 'not found' */
1600 /* create path: self not found in path through self */
1601 GNUNET_break_op (0);
1602 path_destroy (path);
1603 /* FIXME error. destroy tunnel? leave for timeout? */
1606 if (own_pos == size - 1)
1608 /* It is for us! Send ack. */
1609 struct MeshDataDescriptor *info;
1612 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1613 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1614 info->origin = &t->id;
1615 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1616 GNUNET_assert (info->peer);
1617 for (j = 0; info->peer->core_transmit[j]; j++)
1625 info->handler_n = j;
1626 info->peer->core_transmit[j] =
1627 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1628 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1629 sizeof (struct GNUNET_MessageHeader),
1630 &send_core_path_ack, info);
1634 /* It's for somebody else! Retransmit. */
1635 struct MeshPathInfo *path_info;
1637 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1639 path_info->path = path;
1640 path_info->peer = dest_peer_info;
1642 path_add_to_peer (dest_peer_info, path);
1643 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1644 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1645 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1646 sizeof (struct GNUNET_MessageHeader),
1647 &send_core_create_path, path_info);
1654 * Core handler for mesh network traffic going from the origin to a peer
1656 * @param cls closure
1657 * @param peer peer identity this notification is about
1658 * @param message message
1659 * @param atsi performance data
1660 * @return GNUNET_OK to keep the connection open,
1661 * GNUNET_SYSERR to close it (signal serious error)
1664 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1665 const struct GNUNET_MessageHeader *message,
1666 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1668 struct GNUNET_MESH_Unicast *msg;
1669 struct MeshTunnel *t;
1670 struct MeshPeerInfo *pi;
1673 size = ntohs (message->size);
1675 sizeof (struct GNUNET_MESH_Unicast) +
1676 sizeof (struct GNUNET_MessageHeader))
1681 msg = (struct GNUNET_MESH_Unicast *) message;
1682 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1685 /* TODO notify back: we don't know this tunnel */
1686 GNUNET_break_op (0);
1689 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1690 &msg->destination.hashPubKey);
1693 /* TODO maybe feedback, log to statistics */
1694 GNUNET_break_op (0);
1699 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1702 msg = GNUNET_malloc (size);
1703 memcpy (msg, message, size);
1704 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1705 GNUNET_TIME_UNIT_FOREVER_REL,
1706 path_get_first_hop (t->tree, pi->id),
1708 &send_core_data_raw, msg);
1714 * Core handler for mesh network traffic going from the origin to all peers
1716 * @param cls closure
1717 * @param message message
1718 * @param peer peer identity this notification is about
1719 * @param atsi performance data
1720 * @return GNUNET_OK to keep the connection open,
1721 * GNUNET_SYSERR to close it (signal serious error)
1723 * TODO: Check who we got this from, to validate route.
1726 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1727 const struct GNUNET_MessageHeader *message,
1728 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1730 struct GNUNET_MESH_Multicast *msg;
1731 struct GNUNET_PeerIdentity *id;
1732 struct MeshDataDescriptor *info;
1733 struct MeshTunnelTreeNode *n;
1734 struct MeshTunnel *t;
1735 unsigned int *copies;
1740 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
1741 if (size < sizeof (struct GNUNET_MessageHeader))
1743 GNUNET_break_op (0);
1746 msg = (struct GNUNET_MESH_Multicast *) message;
1747 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1751 /* TODO notify that we dont know that tunnel */
1752 GNUNET_break_op (0);
1756 /* Transmit to locally interested clients */
1757 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1759 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1761 n = t->tree->me->children_head;
1764 copies = GNUNET_malloc (sizeof (unsigned int));
1765 for (*copies = 0; NULL != n; n = n->next)
1767 n = t->tree->me->children_head;
1768 data = GNUNET_malloc (size);
1769 memcpy (data, &msg[1], size);
1772 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1773 info->origin = &t->id;
1776 info->copies = copies;
1777 info->client = t->client->handle;
1778 info->destination = n->peer;
1779 id = path_get_first_hop(t->tree, n->peer);
1780 info->peer = peer_info_get(id);
1781 GNUNET_assert (NULL != info->peer);
1782 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
1784 if (i == (CORE_QUEUE_SIZE - 1))
1790 info->handler_n = i;
1791 info->peer->infos[i] = info;
1792 info->peer->core_transmit[i] =
1793 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1794 GNUNET_TIME_UNIT_FOREVER_REL, id,
1795 ntohs (msg->header.size),
1796 &send_core_data_multicast, info);
1804 * Core handler for mesh network traffic
1806 * @param cls closure
1807 * @param message message
1808 * @param peer peer identity this notification is about
1809 * @param atsi performance data
1811 * @return GNUNET_OK to keep the connection open,
1812 * GNUNET_SYSERR to close it (signal serious error)
1815 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1816 const struct GNUNET_MessageHeader *message,
1817 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1819 struct GNUNET_MESH_ToOrigin *msg;
1820 struct GNUNET_PeerIdentity id;
1821 struct MeshPeerInfo *peer_info;
1822 struct MeshTunnel *t;
1825 size = ntohs (message->size);
1826 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1827 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1829 GNUNET_break_op (0);
1832 msg = (struct GNUNET_MESH_ToOrigin *) message;
1833 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1837 /* TODO notify that we dont know this tunnel (whom)? */
1841 if (t->id.oid == myid)
1843 if (NULL == t->client)
1845 /* got data packet for ownerless tunnel */
1846 GNUNET_break_op (0);
1849 /* TODO signature verification */
1850 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1854 peer_info = peer_info_get (&msg->oid);
1855 if (NULL == peer_info)
1857 /* unknown origin of tunnel */
1861 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1862 msg = GNUNET_malloc (size);
1863 memcpy (msg, message, size);
1864 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1865 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1866 &send_core_data_raw, msg);
1873 * Core handler for path ACKs
1875 * @param cls closure
1876 * @param message message
1877 * @param peer peer identity this notification is about
1878 * @param atsi performance data
1880 * @return GNUNET_OK to keep the connection open,
1881 * GNUNET_SYSERR to close it (signal serious error)
1884 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1885 const struct GNUNET_MessageHeader *message,
1886 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1888 struct GNUNET_MESH_PathACK *msg;
1889 struct MeshTunnelTreeNode *n;
1890 struct MeshPeerInfo *peer_info;
1891 struct MeshTunnel *t;
1893 msg = (struct GNUNET_MESH_PathACK *) message;
1894 t = tunnel_get (&msg->oid, msg->tid);
1897 /* TODO notify that we don't know the tunnel */
1901 /* Message for us? */
1902 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1904 if (NULL == t->client)
1906 GNUNET_break_op (0);
1909 peer_info = peer_info_get (&msg->peer_id);
1910 if (NULL == peer_info)
1912 GNUNET_break_op (0);
1915 n = tree_find_peer(t->tree->root, peer_info->id);
1918 GNUNET_break_op (0);
1921 n->status = MESH_PEER_READY;
1922 send_client_peer_connected(t, peer_info->id);
1926 peer_info = peer_info_get (&msg->oid);
1927 if (NULL == peer_info)
1929 /* If we know the tunnel, we should DEFINITELY know the peer */
1933 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1934 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1935 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1936 GNUNET_TIME_UNIT_FOREVER_REL,
1937 path_get_first_hop (t->tree,
1939 sizeof (struct GNUNET_MESH_PathACK),
1940 &send_core_data_raw, msg);
1946 * Functions to handle messages from core
1948 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1949 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1950 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1951 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1952 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1953 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1954 sizeof (struct GNUNET_MESH_PathACK)},
1960 /******************************************************************************/
1961 /**************** MESH LOCAL HANDLER HELPERS ***********************/
1962 /******************************************************************************/
1965 * deregister_app: iterator for removing each application registered by a client
1967 * @param cls closure
1968 * @param key the hash of the application id (used to access the hashmap)
1969 * @param value the value stored at the key (client)
1971 * @return GNUNET_OK on success
1974 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
1976 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
1982 * notify_client_connection_failure: notify a client that the connection to the
1983 * requested remote peer is not possible (for instance, no route found)
1984 * Function called when the socket is ready to queue more data. "buf" will be
1985 * NULL and "size" zero if the socket was closed for writing in the meantime.
1987 * @param cls closure
1988 * @param size number of bytes available in buf
1989 * @param buf where the callee should write the message
1990 * @return number of bytes written to buf
1993 notify_client_connection_failure (void *cls, size_t size, void *buf)
1996 struct MeshPeerInfo *peer_info;
1997 struct GNUNET_MESH_PeerControl *msg;
1998 struct GNUNET_PeerIdentity id;
2000 if (0 == size && NULL == buf)
2002 // TODO retry? cancel?
2006 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2007 peer_info = (struct MeshPeerInfo *) cls;
2008 msg = (struct GNUNET_MESH_PeerControl *) buf;
2009 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2010 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2011 // msg->tunnel_id = htonl(peer_info->t->tid);
2012 GNUNET_PEER_resolve (peer_info->id, &id);
2013 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2021 * Send keepalive packets for a peer
2023 * @param cls Closure (tunnel for which to send the keepalive).
2024 * @param tc Notification context.
2026 * TODO: implement explicit multicast keepalive?
2029 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2031 struct MeshTunnel *t = cls;
2032 struct GNUNET_MessageHeader *payload;
2033 struct GNUNET_MESH_Multicast *msg;
2036 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2037 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2043 "MESH: sending keepalive for tunnel %d\n",
2046 size = sizeof(struct GNUNET_MESH_Multicast) +
2047 sizeof(struct GNUNET_MessageHeader);
2048 msg = GNUNET_malloc (size);
2049 msg->header.size = htons (size);
2050 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2051 msg->oid = my_full_id;
2052 msg->tid = htonl(t->id.tid);
2053 payload = (struct GNUNET_MessageHeader *) &msg[1];
2054 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2055 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2056 handle_mesh_data_multicast (NULL, &my_full_id, &msg->header, NULL);
2059 t->path_refresh_task =
2060 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2066 * Function to process paths received for a new peer addition. The recorded
2067 * paths form the initial tunnel, which can be optimized later.
2068 * Called on each result obtained for the DHT search.
2070 * @param cls closure
2071 * @param exp when will this value expire
2072 * @param key key of the result
2073 * @param type type of the result
2074 * @param size number of bytes in data
2075 * @param data pointer to the result data
2077 * TODO: re-issue the request after certain time? cancel after X results?
2080 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2081 const GNUNET_HashCode * key,
2082 const struct GNUNET_PeerIdentity *get_path,
2083 unsigned int get_path_length,
2084 const struct GNUNET_PeerIdentity *put_path,
2085 unsigned int put_path_length,
2086 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2088 struct MeshPathInfo *path_info = cls;
2089 struct MeshPeerPath *p;
2090 struct GNUNET_PeerIdentity pi;
2093 if (NULL == get_path || NULL == put_path)
2095 if (NULL == path_info->peer->path_head)
2097 // Find ourselves some alternate initial path to the destination: retry
2098 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2099 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2100 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2101 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2102 GNUNET_BLOCK_TYPE_TEST, /* type */
2103 &pi.hashPubKey, /*key to search */
2104 4, /* replication level */
2105 GNUNET_DHT_RO_RECORD_ROUTE,
2107 0, /* xquery bits */
2108 &dht_get_id_handler,
2109 (void *) path_info);
2114 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2115 path_add_to_peer (path_info->peer, p);
2116 for (i = 0; i < path_info->peer->ntunnels; i++)
2118 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2120 GNUNET_free (path_info);
2127 * Function to process paths received for a new peer addition. The recorded
2128 * paths form the initial tunnel, which can be optimized later.
2129 * Called on each result obtained for the DHT search.
2131 * @param cls closure
2132 * @param exp when will this value expire
2133 * @param key key of the result
2134 * @param type type of the result
2135 * @param size number of bytes in data
2136 * @param data pointer to the result data
2139 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2140 const GNUNET_HashCode * key,
2141 const struct GNUNET_PeerIdentity *get_path,
2142 unsigned int get_path_length,
2143 const struct GNUNET_PeerIdentity *put_path,
2144 unsigned int put_path_length,
2145 enum GNUNET_BLOCK_Type type, size_t size,
2148 const struct GNUNET_PeerIdentity *pi = data;
2149 struct GNUNET_PeerIdentity id;
2150 struct MeshTunnel *t = cls;
2151 struct MeshPeerInfo *peer_info;
2152 struct MeshPathInfo *path_info;
2153 struct MeshPeerPath *p;
2156 if (size != sizeof (struct GNUNET_PeerIdentity))
2158 GNUNET_break_op (0);
2161 GNUNET_assert (NULL != t->client);
2162 GNUNET_DHT_get_stop (t->client->dht_get_type);
2163 t->client->dht_get_type = NULL;
2164 peer_info = peer_info_get (pi);
2165 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2166 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2168 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2169 NULL == peer_info->dhtget)
2171 /* we don't have a route to the peer, let's try a direct lookup */
2172 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2174 GNUNET_TIME_UNIT_FOREVER_REL,
2176 GNUNET_BLOCK_TYPE_TEST,
2179 /* key to look up */
2181 /* replication level */
2182 GNUNET_DHT_RO_RECORD_ROUTE,
2183 /* option to dht: record route */
2185 0, /* xquery bits */
2188 peer_info); /* closure */
2191 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2192 path_add_to_peer (peer_info, p);
2193 tunnel_add_peer(t, peer_info);
2194 p = tree_get_path_to_peer(t->tree, peer_info->id);
2196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2197 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2198 t->local_tid, p->length);
2199 for (i = 0; i < p->length; i++)
2201 GNUNET_PEER_resolve (p->peers[0], &id);
2202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2203 GNUNET_h2s_full (&id.hashPubKey));
2209 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2211 path_info->peer = peer_info;
2212 path_info->path = p;
2213 GNUNET_PEER_resolve (p->peers[1], &id);
2214 GNUNET_CORE_notify_transmit_ready (core_handle,
2220 GNUNET_TIME_UNIT_FOREVER_REL,
2224 sizeof (struct GNUNET_MESH_ManipulatePath)
2227 sizeof (struct GNUNET_PeerIdentity)),
2229 &send_core_create_path,
2231 path_info); /* cls */
2235 send_client_peer_connected(t, myid);
2238 /******************************************************************************/
2239 /********************* MESH LOCAL HANDLES **************************/
2240 /******************************************************************************/
2244 * Handler for client disconnection
2246 * @param cls closure
2247 * @param client identification of the client; NULL
2248 * for the last call when the server is destroyed
2251 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2253 struct MeshClient *c;
2254 struct MeshClient *next;
2256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2262 if (c->handle != client && NULL != client)
2264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2269 if (NULL != c->tunnels)
2271 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2272 &tunnel_destroy_iterator,
2274 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2277 /* deregister clients applications */
2278 if (NULL != c->apps)
2280 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2281 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2283 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2284 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2286 GNUNET_SCHEDULER_cancel (announce_applications_task);
2287 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2289 if (NULL != c->types)
2290 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2291 if (NULL != c->dht_get_type)
2292 GNUNET_DHT_get_stop (c->dht_get_type);
2293 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2305 * Handler for new clients
2307 * @param cls closure
2308 * @param client identification of the client
2309 * @param message the actual message, which includes messages the client wants
2312 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2313 const struct GNUNET_MessageHeader *message)
2315 struct GNUNET_MESH_ClientConnect *cc_msg;
2316 struct MeshClient *c;
2317 GNUNET_MESH_ApplicationType *a;
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2325 /* Check data sanity */
2326 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2327 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2328 ntypes = ntohs (cc_msg->types);
2329 napps = ntohs (cc_msg->applications);
2331 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2334 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2338 /* Create new client structure */
2339 c = GNUNET_malloc (sizeof (struct MeshClient));
2341 c->id = next_client_id++;
2344 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2347 GNUNET_MESH_ApplicationType at;
2350 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2351 for (i = 0; i < napps; i++)
2354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2355 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2356 /* store in clients hashmap */
2357 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2358 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2359 /* store in global hashmap, for announcements */
2360 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2361 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2363 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2364 announce_applications_task =
2365 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2373 t = (uint16_t *) & a[napps];
2374 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2375 for (i = 0; i < ntypes; i++)
2378 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2380 /* store in clients hashmap */
2381 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2382 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2383 /* store in global hashmap */
2384 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2385 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2389 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2391 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2392 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2393 GNUNET_SERVER_notification_context_add (nc, client);
2395 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2403 * Handler for requests of new tunnels
2405 * @param cls closure
2406 * @param client identification of the client
2407 * @param message the actual message
2410 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2411 const struct GNUNET_MessageHeader *message)
2413 struct GNUNET_MESH_TunnelMessage *t_msg;
2414 struct MeshTunnel *t;
2415 struct MeshClient *c;
2416 GNUNET_HashCode hash;
2418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2420 /* Sanity check for client registration */
2421 if (NULL == (c = client_get (client)))
2424 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2431 /* Message sanity check */
2432 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2435 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2439 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2440 /* Sanity check for tunnel numbering */
2441 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2444 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2447 /* Sanity check for duplicate tunnel IDs */
2448 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2455 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2457 while (NULL != tunnel_get_by_pi (myid, next_tid))
2458 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2459 t->id.tid = next_tid++;
2461 t->local_tid = ntohl (t_msg->tunnel_id);
2463 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2465 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2467 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2468 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2471 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2475 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2477 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2478 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2481 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2484 t->tree = tree_new (t, myid);
2485 t->tree->refresh = REFRESH_PATH_TIME;
2486 t->tree->root->status = MESH_PEER_READY;
2487 t->tree->me = t->tree->root;
2489 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2495 * Handler for requests of deleting tunnels
2497 * @param cls closure
2498 * @param client identification of the client
2499 * @param message the actual message
2502 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2503 const struct GNUNET_MessageHeader *message)
2505 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2506 struct MeshClient *c;
2507 struct MeshTunnel *t;
2508 MESH_TunnelNumber tid;
2509 GNUNET_HashCode hash;
2511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2513 /* Sanity check for client registration */
2514 if (NULL == (c = client_get (client)))
2517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2520 /* Message sanity check */
2521 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2524 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2530 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2532 /* Retrieve tunnel */
2533 tid = ntohl (tunnel_msg->tunnel_id);
2535 /* Remove from local id hashmap */
2536 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2537 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2538 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2540 /* Remove from global id hashmap */
2541 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2542 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2544 // notify_tunnel_destroy(t); FIXME
2546 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2552 * Handler for connection requests to new peers
2554 * @param cls closure
2555 * @param client identification of the client
2556 * @param message the actual message (PeerControl)
2559 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2560 const struct GNUNET_MessageHeader *message)
2562 struct GNUNET_MESH_PeerControl *peer_msg;
2563 struct MeshClient *c;
2564 struct MeshTunnel *t;
2565 MESH_TunnelNumber tid;
2566 struct MeshPeerInfo *peer_info;
2569 /* Sanity check for client registration */
2570 if (NULL == (c = client_get (client)))
2573 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2577 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2578 /* Sanity check for message size */
2579 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2586 /* Tunnel exists? */
2587 tid = ntohl (peer_msg->tunnel_id);
2588 t = tunnel_get_by_local_id (c, tid);
2592 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2596 /* Does client own tunnel? */
2597 if (t->client->handle != client)
2600 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2604 peer_info = peer_info_get (&peer_msg->peer);
2606 /* Start DHT search if needed, otherwise just add peer to tunnel. */
2607 if (NULL == peer_info->dhtget && NULL == peer_info->path_head)
2609 struct MeshPathInfo *path_info;
2611 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2612 path_info->peer = peer_info;
2614 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
2615 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2616 GNUNET_BLOCK_TYPE_TEST, /* type */
2617 &peer_msg->peer.hashPubKey, /*key to search */
2618 4, /* replication level */
2619 GNUNET_DHT_RO_RECORD_ROUTE,
2621 0, /* xquery bits */
2622 &dht_get_id_handler,
2623 (void *) path_info);
2625 if (NULL != peer_info->path_head)
2627 tunnel_add_peer(t, peer_info);
2629 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2635 * Handler for disconnection requests of peers in a tunnel
2637 * @param cls closure
2638 * @param client identification of the client
2639 * @param message the actual message (PeerControl)
2642 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2643 const struct GNUNET_MessageHeader *message)
2645 struct GNUNET_MESH_PeerControl *peer_msg;
2646 struct MeshClient *c;
2647 struct MeshTunnel *t;
2648 MESH_TunnelNumber tid;
2650 /* Sanity check for client registration */
2651 if (NULL == (c = client_get (client)))
2654 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2657 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2658 /* Sanity check for message size */
2659 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2662 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2666 /* Tunnel exists? */
2667 tid = ntohl (peer_msg->tunnel_id);
2668 t = tunnel_get_by_local_id (c, tid);
2672 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2676 /* Does client own tunnel? */
2677 if (t->client->handle != client)
2680 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2684 /* Ok, delete peer from tunnel */
2685 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2686 &peer_msg->peer.hashPubKey);
2688 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2694 * Handler for connection requests to new peers by type
2696 * @param cls closure
2697 * @param client identification of the client
2698 * @param message the actual message (ConnectPeerByType)
2701 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2702 const struct GNUNET_MessageHeader *message)
2704 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2705 struct MeshClient *c;
2706 struct MeshTunnel *t;
2707 GNUNET_HashCode hash;
2708 GNUNET_MESH_ApplicationType type;
2709 MESH_TunnelNumber tid;
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2712 /* Sanity check for client registration */
2713 if (NULL == (c = client_get (client)))
2716 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2720 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2721 /* Sanity check for message size */
2722 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2723 ntohs (connect_msg->header.size))
2726 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2730 /* Tunnel exists? */
2731 tid = ntohl (connect_msg->tunnel_id);
2732 t = tunnel_get_by_local_id (c, tid);
2736 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2740 /* Does client own tunnel? */
2741 if (t->client->handle != client)
2744 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2748 /* Do WE have the service? */
2749 type = ntohl (connect_msg->type);
2750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2751 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2752 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2755 /* Yes! Fast forward, add ourselves to the tunnel and send the
2756 * good news to the client
2758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2759 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2760 peer_info_get (&my_full_id),
2761 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2764 send_client_peer_connected(t, myid);
2765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2766 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2769 /* Ok, lets find a peer offering the service */
2770 if (c->dht_get_type)
2772 GNUNET_DHT_get_stop (c->dht_get_type);
2774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2775 GNUNET_h2s_full (&hash));
2777 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2778 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2779 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0,
2780 &dht_get_type_handler, t);
2782 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2788 * Handler for client traffic directed to one peer
2790 * @param cls closure
2791 * @param client identification of the client
2792 * @param message the actual message
2795 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2796 const struct GNUNET_MessageHeader *message)
2798 struct MeshClient *c;
2799 struct MeshTunnel *t;
2800 struct MeshPeerInfo *pi;
2801 struct GNUNET_MESH_Unicast *data_msg;
2802 MESH_TunnelNumber tid;
2805 /* Sanity check for client registration */
2806 if (NULL == (c = client_get (client)))
2809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2812 data_msg = (struct GNUNET_MESH_Unicast *) message;
2813 /* Sanity check for message size */
2814 size = ntohs (message->size);
2815 if (sizeof (struct GNUNET_MESH_Unicast) +
2816 sizeof (struct GNUNET_MessageHeader) > size)
2819 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2823 /* Tunnel exists? */
2824 tid = ntohl (data_msg->tid);
2825 t = tunnel_get_by_local_id (c, tid);
2829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2833 /* Is it a local tunnel? Then, does client own the tunnel? */
2834 if (t->client->handle != NULL && t->client->handle != client)
2837 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2841 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2842 &data_msg->destination.hashPubKey);
2843 /* Is the selected peer in the tunnel? */
2847 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2851 /* Ok, everything is correct, send the message
2852 * (pretend we got it from a mesh peer)
2855 char buf[ntohs (message->size)];
2856 struct GNUNET_MESH_Unicast *copy;
2858 /* Work around const limitation */
2859 copy = (struct GNUNET_MESH_Unicast *) buf;
2860 memcpy (buf, data_msg, size);
2861 copy->oid = my_full_id;
2862 copy->tid = htonl (t->id.tid);
2863 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
2865 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2870 * Handler for client traffic directed to all peers in a tunnel
2872 * @param cls closure
2873 * @param client identification of the client
2874 * @param message the actual message
2877 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2878 const struct GNUNET_MessageHeader *message)
2880 struct MeshClient *c;
2881 struct MeshTunnel *t;
2882 struct GNUNET_MESH_Multicast *data_msg;
2883 MESH_TunnelNumber tid;
2885 /* Sanity check for client registration */
2886 if (NULL == (c = client_get (client)))
2889 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2892 data_msg = (struct GNUNET_MESH_Multicast *) message;
2893 /* Sanity check for message size */
2894 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
2897 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2901 /* Tunnel exists? */
2902 tid = ntohl (data_msg->tid);
2903 t = tunnel_get_by_local_id (c, tid);
2907 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2911 /* Does client own tunnel? */
2912 if (t->client->handle != client)
2915 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2920 char buf[ntohs(message->size)];
2921 struct GNUNET_MESH_Multicast *copy;
2923 copy = (struct GNUNET_MESH_Multicast *)buf;
2924 memcpy(buf, message, ntohs(message->size));
2925 copy->oid = my_full_id;
2926 copy->tid = htonl(t->id.tid);
2927 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
2930 /* receive done gets called when last copy is sent */
2935 * Functions to handle messages from clients
2937 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
2938 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
2939 {&handle_local_tunnel_create, NULL,
2940 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
2941 sizeof (struct GNUNET_MESH_TunnelMessage)},
2942 {&handle_local_tunnel_destroy, NULL,
2943 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
2944 sizeof (struct GNUNET_MESH_TunnelMessage)},
2945 {&handle_local_connect_add, NULL,
2946 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
2947 sizeof (struct GNUNET_MESH_PeerControl)},
2948 {&handle_local_connect_del, NULL,
2949 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
2950 sizeof (struct GNUNET_MESH_PeerControl)},
2951 {&handle_local_connect_by_type, NULL,
2952 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
2953 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
2954 {&handle_local_unicast, NULL,
2955 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2956 {&handle_local_unicast, NULL,
2957 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2958 {&handle_local_multicast, NULL,
2959 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2965 * To be called on core init/fail.
2967 * @param cls service closure
2968 * @param server handle to the server for this service
2969 * @param identity the public identity of this peer
2970 * @param publicKey the public key of this peer
2973 core_init (void *cls, struct GNUNET_CORE_Handle *server,
2974 const struct GNUNET_PeerIdentity *identity,
2975 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
2978 core_handle = server;
2979 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
2981 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
2982 GNUNET_SCHEDULER_shutdown();
2988 * Method called whenever a given peer connects.
2990 * @param cls closure
2991 * @param peer peer identity this notification is about
2992 * @param atsi performance data for the connection
2995 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
2996 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2998 // GNUNET_PEER_Id pid;
2999 struct MeshPeerInfo *peer_info;
3000 struct MeshPeerPath *path;
3002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3003 peer_info = peer_info_get (peer);
3004 if (myid == peer_info->id)
3006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3009 path = path_new (2);
3010 path->peers[0] = myid;
3011 path->peers[1] = peer_info->id;
3012 path_add_to_peer (peer_info, path);
3017 * Method called whenever a peer disconnects.
3019 * @param cls closure
3020 * @param peer peer identity this notification is about
3023 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3025 struct MeshPeerInfo *pi;
3028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3029 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3035 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3037 if (pi->core_transmit[i])
3039 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3040 /* TODO: notify that tranmission has failed */
3041 GNUNET_free (pi->infos[i]);
3044 path_remove_from_peer (pi, pi->id, myid);
3047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3053 /******************************************************************************/
3054 /************************ MAIN FUNCTIONS ****************************/
3055 /******************************************************************************/
3058 * Task run during shutdown.
3064 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3066 struct MeshClient *c;
3068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3069 if (core_handle != NULL)
3071 GNUNET_CORE_disconnect (core_handle);
3074 if (dht_handle != NULL)
3076 for (c = clients; NULL != c; c = c->next)
3077 if (NULL != c->dht_get_type)
3078 GNUNET_DHT_get_stop (c->dht_get_type);
3079 GNUNET_DHT_disconnect (dht_handle);
3084 GNUNET_SERVER_notification_context_destroy (nc);
3087 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3089 GNUNET_SCHEDULER_cancel (announce_id_task);
3090 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3096 * Process mesh requests.
3098 * @param cls closure
3099 * @param server the initialized server
3100 * @param c configuration to use
3103 run (void *cls, struct GNUNET_SERVER_Handle *server,
3104 const struct GNUNET_CONFIGURATION_Handle *c)
3106 struct MeshPeerInfo *peer;
3107 struct MeshPeerPath *p;
3110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3111 server_handle = server;
3112 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3113 CORE_QUEUE_SIZE, /* queue size */
3114 NULL, /* Closure passed to MESH functions */
3115 &core_init, /* Call core_init once connected */
3116 &core_connect, /* Handle connects */
3117 &core_disconnect, /* remove peers on disconnects */
3118 NULL, /* Do we care about "status" updates? */
3119 NULL, /* Don't notify about all incoming messages */
3120 GNUNET_NO, /* For header only in notification */
3121 NULL, /* Don't notify about all outbound messages */
3122 GNUNET_NO, /* For header-only out notification */
3123 core_handlers); /* Register these handlers */
3124 if (core_handle == NULL)
3127 GNUNET_SCHEDULER_shutdown ();
3132 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3137 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3138 GNUNET_SCHEDULER_shutdown ();
3141 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3142 GNUNET_free (keyfile);
3143 if (my_private_key == NULL)
3145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3146 _("Mesh service could not access hostkey. Exiting.\n"));
3147 GNUNET_SCHEDULER_shutdown ();
3150 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3151 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3152 &my_full_id.hashPubKey);
3153 myid = GNUNET_PEER_intern (&my_full_id);
3155 dht_handle = GNUNET_DHT_connect (c, 64);
3156 if (dht_handle == NULL)
3158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error connecting to DHT.\
3159 Running without DHT has a severe\
3160 impact in MESH capabilities.\n\
3161 Plase check your configuretion and enable DHT.\n");
3167 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3168 peers = GNUNET_CONTAINER_multihashmap_create (32);
3169 applications = GNUNET_CONTAINER_multihashmap_create (32);
3170 types = GNUNET_CONTAINER_multihashmap_create (32);
3172 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3173 nc = GNUNET_SERVER_notification_context_create (server_handle,
3175 GNUNET_SERVER_disconnect_notify (server_handle,
3176 &handle_local_client_disconnect,
3181 clients_tail = NULL;
3186 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3187 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3189 /* Create a peer_info for the local peer */
3190 peer = peer_info_get(&my_full_id);
3193 path_add_to_peer(peer, p);
3195 /* Scheduled the task to clean up when shutdown is called */
3196 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3203 * The main function for the mesh service.
3205 * @param argc number of arguments from the command line
3206 * @param argv command line arguments
3207 * @return 0 ok, 1 on error
3210 main (int argc, char *const *argv)
3215 // fprintf (stderr, "main ()\n");
3217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3220 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3224 // fprintf (stderr, "main () END\n");