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,\
65 #define GET_RESTART_TIME GNUNET_TIME_relative_multiply(\
66 GNUNET_TIME_UNIT_SECONDS,\
69 /******************************************************************************/
70 /************************ DATA STRUCTURES ****************************/
71 /******************************************************************************/
73 /** FWD declaration */
77 * Struct containing all info possibly needed to build a package when called
80 struct MeshDataDescriptor
82 /** ID of the tunnel this packet travels in */
83 struct MESH_TunnelID *origin;
88 /** Client that asked for the transmission, if any */
89 struct GNUNET_SERVER_Client *client;
91 /** Who was this message being sent to */
92 struct MeshPeerInfo *peer;
94 /** Ultimate destination of the packet */
95 GNUNET_PEER_Id destination;
97 /** Number of identical messages sent to different hops (multicast) */
100 /** Which handler was used to request the transmission */
101 unsigned int handler_n;
103 /** Size of the data */
110 * Struct containing all information regarding a given peer
120 * Last time we heard from this peer
122 struct GNUNET_TIME_Absolute last_contact;
125 * Number of attempts to reconnect so far
127 int n_reconnect_attempts;
130 * Paths to reach the peer, ordered by ascending hop count
132 struct MeshPeerPath *path_head;
135 * Paths to reach the peer, ordered by ascending hop count
137 struct MeshPeerPath *path_tail;
140 * Handle to stop the DHT search for a path to this peer
142 struct GNUNET_DHT_GetHandle *dhtget;
145 * Handles to stop queued transmissions for this peer
147 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
150 * Pointer to info stuctures used as cls for queued transmissions
152 void *infos[CORE_QUEUE_SIZE];
155 * Type of message being in each transmission
157 uint16_t types[CORE_QUEUE_SIZE];
160 * Array of tunnels this peer participates in
161 * (most probably a small amount, therefore not a hashmap)
162 * When the path to the peer changes, notify these tunnels to let them
163 * re-adjust their path trees.
165 struct MeshTunnel **tunnels;
168 * Number of tunnels above
170 unsigned int ntunnels;
175 * Data scheduled to transmit (to local client or remote peer)
182 struct MeshQueue *next;
183 struct MeshQueue *prev;
186 * Target of the data (NULL if target is client)
188 struct MeshPeerInfo *peer;
191 * Client to send the data to (NULL if target is peer)
193 struct MeshClient *client;
196 * Size of the message to transmit
201 * How old is the data?
203 struct GNUNET_TIME_Absolute timestamp;
208 struct GNUNET_MessageHeader *data;
212 * Globally unique tunnel identification (owner + number)
213 * DO NOT USE OVER THE NETWORK
218 * Node that owns the tunnel
223 * Tunnel number to differentiate all the tunnels owned by the node oid
224 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
226 MESH_TunnelNumber tid;
230 struct MeshClient; /* FWD declaration */
233 * Struct containing all information regarding a tunnel
234 * For an intermediate node the improtant info used will be:
235 * - id Tunnel unique identification
236 * - paths[0] To know where to send it next
237 * - metainfo: ready, speeds, accounting
244 struct MESH_TunnelID id;
247 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
249 MESH_TunnelNumber local_tid;
252 * Last time the tunnel was used
254 struct GNUNET_TIME_Absolute timestamp;
257 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
259 struct GNUNET_CONTAINER_MultiHashMap *peers;
262 * Number of peers that are connected and potentially ready to receive data
264 unsigned int peers_ready;
267 * Number of peers that have been added to the tunnel
269 unsigned int peers_total;
272 * Client owner of the tunnel, if any
274 struct MeshClient *client;
277 * Messages ready to transmit
279 struct MeshQueue *queue_head;
280 struct MeshQueue *queue_tail;
285 struct MeshTunnelTree *tree;
288 * Application type we are looking for in this tunnel
290 GNUNET_MESH_ApplicationType type;
293 * Used to search peers offering a service
295 struct GNUNET_DHT_GetHandle *dht_get_type;
298 * Task to keep the used paths alive
300 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
305 * Info needed to work with tunnel paths and peers
312 struct MeshTunnel *t;
317 struct MeshPeerInfo *peer;
322 struct MeshPeerPath *path;
327 * Struct containing information about a client of the service
334 struct MeshClient *next;
335 struct MeshClient *prev;
338 * Tunnels that belong to this client, indexed by local id
340 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
343 * Handle to communicate with the client
345 struct GNUNET_SERVER_Client *handle;
348 * Applications that this client has claimed to provide
350 struct GNUNET_CONTAINER_MultiHashMap *apps;
353 * Messages that this client has declared interest in
355 struct GNUNET_CONTAINER_MultiHashMap *types;
359 * ID of the client, for debug messages
368 /******************************************************************************/
369 /************************ DEBUG FUNCTIONS ****************************/
370 /******************************************************************************/
374 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
375 * @param cls string to print
376 * @param tc task context
379 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
383 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
391 /******************************************************************************/
392 /*********************** GLOBAL VARIABLES ****************************/
393 /******************************************************************************/
398 static struct MeshClient *clients;
399 static struct MeshClient *clients_tail;
402 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
404 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
407 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
409 static struct GNUNET_CONTAINER_MultiHashMap *peers;
412 * Handle to communicate with core
414 static struct GNUNET_CORE_Handle *core_handle;
419 static struct GNUNET_DHT_Handle *dht_handle;
424 static struct GNUNET_SERVER_Handle *server_handle;
427 * Notification context, to send messages to local clients
429 static struct GNUNET_SERVER_NotificationContext *nc;
432 * Local peer own ID (memory efficient handle)
434 static GNUNET_PEER_Id myid;
437 * Local peer own ID (full value)
439 static struct GNUNET_PeerIdentity my_full_id;
444 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
449 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
452 * Tunnel ID for the next created tunnel (global tunnel number)
454 static MESH_TunnelNumber next_tid;
457 * Tunnel ID for the next incoming tunnel (local tunnel number)
459 static MESH_TunnelNumber next_local_tid;
462 * All application types provided by this peer
464 static struct GNUNET_CONTAINER_MultiHashMap *applications;
467 * All message types clients of this peer are interested in
469 static struct GNUNET_CONTAINER_MultiHashMap *types;
472 * Task to periodically announce provided applications
474 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
477 * Task to periodically announce itself in the network
479 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
482 unsigned int next_client_id;
486 /******************************************************************************/
487 /************************ ITERATORS ****************************/
488 /******************************************************************************/
490 /* FIXME move iterators here */
493 /******************************************************************************/
494 /************************ PERIODIC FUNCTIONS ****************************/
495 /******************************************************************************/
498 * Announce iterator over for each application provided by the peer
501 * @param key current key code
502 * @param value value in the hash map
503 * @return GNUNET_YES if we should continue to
508 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
510 /* FIXME are hashes in multihash map equal on all aquitectures? */
511 GNUNET_DHT_put (dht_handle,
514 GNUNET_DHT_RO_RECORD_ROUTE |
515 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
516 GNUNET_BLOCK_TYPE_TEST,
517 sizeof (struct GNUNET_PeerIdentity),
518 (const char *) &my_full_id,
520 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
521 &mesh_debug, "DHT_put for app completed");
523 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
525 APP_ANNOUNCE_TIME, NULL, NULL);
532 * Periodically announce what applications are provided by local clients
535 * @param tc task context
538 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
540 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
542 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
546 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
548 announce_applications_task =
549 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
557 * Periodically announce self id in the DHT
560 * @param tc task context
563 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
565 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
567 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
571 * - Set data expiration in function of X
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
575 GNUNET_h2s_full (&my_full_id.hashPubKey));
576 GNUNET_DHT_put (dht_handle, /* DHT handle */
577 &my_full_id.hashPubKey, /* Key to use */
578 10U, /* Replication level */
579 GNUNET_DHT_RO_RECORD_ROUTE |
580 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
581 GNUNET_BLOCK_TYPE_TEST, /* Block type */
582 sizeof(my_full_id), /* Size of the data */
583 (char *)&my_full_id, /* Data itself */
584 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
585 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
587 &mesh_debug, "DHT_put for id completed");
589 NULL, /* Continuation */
590 NULL); /* Continuation closure */
593 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
598 * Function to process paths received for a new peer addition. The recorded
599 * paths form the initial tunnel, which can be optimized later.
600 * Called on each result obtained for the DHT search.
603 * @param exp when will this value expire
604 * @param key key of the result
605 * @param type type of the result
606 * @param size number of bytes in data
607 * @param data pointer to the result data
610 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
611 const GNUNET_HashCode * key,
612 const struct GNUNET_PeerIdentity *get_path,
613 unsigned int get_path_length,
614 const struct GNUNET_PeerIdentity *put_path,
615 unsigned int put_path_length,
616 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
619 /******************************************************************************/
620 /****************** GENERAL HELPER FUNCTIONS ************************/
621 /******************************************************************************/
624 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
625 * and insert it in the appropiate structures if the peer is not known yet.
627 * @param peer Identity of the peer
629 * @return Existing or newly created peer info
631 static struct MeshPeerInfo *
632 peer_info_get (const struct GNUNET_PeerIdentity *peer)
634 struct MeshPeerInfo *peer_info;
636 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
637 if (NULL == peer_info)
640 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
641 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
642 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
643 peer_info->id = GNUNET_PEER_intern (peer);
652 * Destroy the peer_info and free any allocated resources linked to it
653 * @param t tunnel the path belongs to
654 * @param pi the peer_info to destroy
655 * @return GNUNET_OK on success
658 peer_info_destroy (struct MeshPeerInfo *pi)
660 GNUNET_HashCode hash;
661 struct GNUNET_PeerIdentity id;
663 GNUNET_PEER_resolve (pi->id, &id);
664 GNUNET_PEER_change_rc (pi->id, -1);
665 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
667 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
668 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
676 * Notify a tunnel that a connection has broken that affects at least
679 * @param t Tunnel affected.
680 * @param peer Peer that (at least) has been affected by the disconnection.
681 * @param p1 Peer that got disconnected from p2.
682 * @param p2 Peer that got disconnected from p1.
684 * @return Short ID of the peer disconnected (either p1 or p2).
685 * 0 if the tunnel remained unaffected.
687 static GNUNET_PEER_Id
688 tunnel_notify_connection_broken (struct MeshTunnel *t,
689 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
693 * Remove all paths that rely on a direct connection between p1 and p2
694 * from the peer itself and notify all tunnels about it.
696 * @param peer PeerInfo of affected peer.
697 * @param p1 GNUNET_PEER_Id of one peer.
698 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
701 * TODO: optimize (see below)
704 path_remove_from_peer (struct MeshPeerInfo *peer,
708 struct GNUNET_PeerIdentity id;
709 struct MeshPeerPath *p;
710 struct MeshPeerPath *aux;
711 struct MeshPeerInfo *peer_d;
713 unsigned int destroyed;
723 for (i = 0; i < (p->length - 1); i++)
725 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
726 (p->peers[i] == p2 && p->peers[i + 1] == p1))
738 for (i = 0; i < peer->ntunnels; i++)
740 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
742 * Problem: one or more peers have been deleted from the tunnel tree.
743 * We don't know who they are to try to add them again.
744 * We need to try to find a new path for each of the disconnected peers.
745 * Some of them might already have a path to reach them that does not
746 * involve p1 and p2. Adding all anew might render in a better tree than
747 * the trivial immediate fix.
749 * Trivial immiediate fix: try to reconnect to the disconnected node. All
750 * its children will be reachable trough him.
752 GNUNET_PEER_resolve(d, &id);
753 peer_d = peer_info_get(&id);
756 for (p = peer_d->path_head; NULL != p; p = p->next)
758 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
766 /* No callback, as peer will be already disconnected */
767 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
771 struct MeshPathInfo *path_info;
773 if (NULL != peer_d->dhtget)
775 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
777 path_info->peer = peer_d;
778 path_info->t = peer->tunnels[i];
780 GNUNET_DHT_get_start(dht_handle, /* handle */
781 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
782 GNUNET_BLOCK_TYPE_TEST, /* type */
783 &id.hashPubKey, /*key to search */
784 4, /* replication level */
785 GNUNET_DHT_RO_RECORD_ROUTE |
786 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
797 * Add the path to the peer and update the path used to reach it in case this
800 * @param peer_info Destination peer to add the path to.
801 * @param path New path to add. Last peer must be the peer in arg 1.
802 * Path will be either used of freed if already known.
804 * TODO: trim the part from origin to us? Add it as path to origin?
807 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
809 struct MeshPeerPath *aux;
813 if (NULL == peer_info || NULL == path)
819 l = path_get_length (path);
821 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
823 l2 = path_get_length (aux);
826 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
827 peer_info->path_tail, aux, path);
831 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
838 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
845 * Add the path to the origin peer and update the path used to reach it in case
846 * this is the shortest.
847 * The path is given in peer_info -> destination, therefore we turn the path
850 * @param peer_info Peer to add the path to, being the origin of the path.
851 * @param path New path to add after being inversed.
854 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
857 path_add_to_peer (peer_info, path);
862 * Build a PeerPath from the paths returned from the DHT, reversing the paths
863 * to obtain a local peer -> destination path and interning the peer ids.
865 * @return Newly allocated and created path
867 static struct MeshPeerPath *
868 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
869 unsigned int get_path_length,
870 const struct GNUNET_PeerIdentity *put_path,
871 unsigned int put_path_length)
873 struct MeshPeerPath *p;
879 GNUNET_PEER_change_rc(myid, 1);
881 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
882 for (i--; i >= 0; i--)
884 id = GNUNET_PEER_intern (&get_path[i]);
885 if (p->length > 0 && id == p->peers[p->length - 1])
887 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
888 GNUNET_PEER_change_rc(id, -1);
893 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
894 p->peers[p->length] = id;
898 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
899 for (i--; i >= 0; i--)
901 id = GNUNET_PEER_intern (&put_path[i]);
904 /* PUT path went through us, so discard the path up until now and start
905 * from here to get a much shorter (and loop-free) path.
910 if (p->length > 0 && id == p->peers[p->length - 1])
912 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
913 GNUNET_PEER_change_rc(id, -1);
918 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
919 p->peers[p->length] = id;
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
924 "MESH: (first of GET: %s)\n",
925 GNUNET_h2s_full(&get_path[0].hashPubKey));
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
927 "MESH: (first of PUT: %s)\n",
928 GNUNET_h2s_full(&put_path[0].hashPubKey));
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
930 "MESH: In total: %d hops\n",
938 * Send keepalive packets for a peer
940 * @param cls Closure (tunnel for which to send the keepalive).
941 * @param tc Notification context.
943 * TODO: implement explicit multicast keepalive?
946 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
950 * Check if client has registered with the service and has not disconnected
952 * @param client the client to check
954 * @return non-NULL if client exists in the global DLL
956 static struct MeshClient *
957 client_get (struct GNUNET_SERVER_Client *client)
959 struct MeshClient *c;
964 if (c->handle == client)
973 * Checks if a given client has subscribed to certain message type
975 * @param message_type Type of message to check
976 * @param c Client to check
978 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
983 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
987 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
988 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
993 * Search for a tunnel among the tunnels for a client
995 * @param c the client whose tunnels to search in
996 * @param tid the local id of the tunnel
998 * @return tunnel handler, NULL if doesn't exist
1000 static struct MeshTunnel *
1001 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1003 GNUNET_HashCode hash;
1005 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1006 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1011 * Search for a tunnel by global ID using PEER_ID
1013 * @param pi owner of the tunnel
1014 * @param tid global tunnel number
1016 * @return tunnel handler, NULL if doesn't exist
1018 static struct MeshTunnel *
1019 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1021 struct MESH_TunnelID id;
1022 GNUNET_HashCode hash;
1027 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1028 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1033 * Search for a tunnel by global ID using full PeerIdentities
1035 * @param oid owner of the tunnel
1036 * @param tid global tunnel number
1038 * @return tunnel handler, NULL if doesn't exist
1040 static struct MeshTunnel *
1041 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1043 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1048 * Callback used to notify a client owner of a tunnel that a peer has
1049 * disconnected, most likely because of a path change.
1051 * @param n Node in the tree representing the disconnected peer
1054 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1056 struct GNUNET_MESH_PeerControl msg;
1058 if (NULL == n->t->client || NULL == nc)
1061 msg.header.size = htons (sizeof (msg));
1062 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1063 msg.tunnel_id = htonl (n->t->local_tid);
1064 GNUNET_PEER_resolve (n->peer, &msg.peer);
1065 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1066 &msg.header, GNUNET_NO);
1071 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1072 * needed rescources.
1074 * @param t Tunnel we want to add a new peer to
1075 * @param peer PeerInfo of the peer being added
1079 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1081 struct MeshPeerPath *p;
1082 struct MeshPeerPath *best_p;
1083 unsigned int best_cost;
1086 if (NULL != tree_find_peer(t->tree->root, peer->id))
1088 /* Already have it, nothing to do. */
1093 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1094 if (NULL == (p = peer->path_head))
1101 best_cost = UINT_MAX;
1104 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1111 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1112 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1113 t->path_refresh_task =
1114 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1120 * Notify a tunnel that a connection has broken that affects at least
1121 * some of its peers.
1123 * @param t Tunnel affected.
1124 * @param peer Peer that (at least) has been affected by the disconnection.
1125 * @param p1 Peer that got disconnected from p2.
1126 * @param p2 Peer that got disconnected from p1.
1128 * @return Short ID of the peer disconnected (either p1 or p2).
1129 * 0 if the tunnel remained unaffected.
1131 static GNUNET_PEER_Id
1132 tunnel_notify_connection_broken (struct MeshTunnel *t,
1133 struct MeshPeerInfo *peer,
1137 return tree_notify_connection_broken (t->tree, p1, p2,
1138 ¬ify_peer_disconnected);
1143 * Destroy the tunnel and free any allocated resources linked to it
1145 * @param t the tunnel to destroy
1147 * @return GNUNET_OK on success
1150 tunnel_destroy (struct MeshTunnel *t)
1152 struct MeshClient *c;
1153 struct MeshQueue *q;
1154 struct MeshQueue *qn;
1155 GNUNET_HashCode hash;
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1167 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1168 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1173 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1174 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1178 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1182 if (NULL != q->data)
1183 GNUNET_free (q->data);
1187 /* TODO cancel core transmit ready in case it was active */
1189 tree_destroy(t->tree);
1196 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1197 * client when the client disconnects.
1199 * @param cls closure (client that is disconnecting)
1200 * @param key the hash of the local tunnel id (used to access the hashmap)
1201 * @param value the value stored at the key (tunnel to destroy)
1203 * @return GNUNET_OK on success
1206 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1208 struct MeshTunnel *t = value;
1211 if (NULL != t->dht_get_type)
1213 GNUNET_DHT_get_stop (t->dht_get_type);
1215 r = tunnel_destroy (t);
1220 /******************************************************************************/
1221 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1222 /******************************************************************************/
1226 * Function called to notify a client about the socket
1227 * being ready to queue more data. "buf" will be
1228 * NULL and "size" zero if the socket was closed for
1229 * writing in the meantime.
1231 * @param cls closure
1232 * @param size number of bytes available in buf
1233 * @param buf where the callee should write the message
1234 * @return number of bytes written to buf
1237 send_core_create_path (void *cls, size_t size, void *buf)
1239 struct MeshPathInfo *info = cls;
1240 struct GNUNET_MESH_ManipulatePath *msg;
1241 struct GNUNET_PeerIdentity *peer_ptr;
1242 struct MeshPeerInfo *peer = info->peer;
1243 struct MeshTunnel *t = info->t;
1244 struct MeshPeerPath *p = info->path;
1249 sizeof (struct GNUNET_MESH_ManipulatePath) +
1250 p->length * sizeof (struct GNUNET_PeerIdentity);
1252 if (size < size_needed || NULL == buf)
1254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
1258 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1259 GNUNET_TIME_UNIT_FOREVER_REL,
1260 path_get_first_hop (t->tree, peer->id),
1262 &send_core_create_path,
1267 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1268 msg->header.size = htons (size_needed);
1269 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1270 msg->tid = ntohl (t->id.tid);
1272 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1273 for (i = 0; i < p->length; i++)
1275 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1282 "MESH: CREATE PATH (%u bytes long) sent!\n",
1290 * Function called to notify a client about the socket
1291 * being ready to queue more data. "buf" will be
1292 * NULL and "size" zero if the socket was closed for
1293 * writing in the meantime.
1295 * @param cls closure (MeshDataDescriptor with all info to build packet)
1296 * @param size number of bytes available in buf
1297 * @param buf where the callee should write the message
1298 * @return number of bytes written to buf
1301 send_core_data_to_origin (void *cls, size_t size, void *buf)
1303 struct MeshDataDescriptor *info = cls;
1304 struct GNUNET_MESH_ToOrigin *msg = buf;
1307 GNUNET_assert (NULL != info);
1308 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1309 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1311 if (total_size > size)
1313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1314 "not enough buffer to send data to origin\n");
1317 msg->header.size = htons (total_size);
1318 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1319 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1320 msg->tid = htonl (info->origin->tid);
1321 if (0 != info->size)
1323 memcpy (&msg[1], &info[1], info->size);
1325 if (NULL != info->client)
1327 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1336 * Function called to notify a client about the socket
1337 * being ready to queue more data. "buf" will be
1338 * NULL and "size" zero if the socket was closed for
1339 * writing in the meantime.
1341 * @param cls closure (data itself)
1342 * @param size number of bytes available in buf
1343 * @param buf where the callee should write the message
1345 * @return number of bytes written to buf
1348 send_core_data_multicast (void *cls, size_t size, void *buf)
1350 struct MeshDataDescriptor *info = cls;
1351 struct GNUNET_MESH_Multicast *msg = buf;
1354 GNUNET_assert (NULL != info);
1355 GNUNET_assert (NULL != info->peer);
1356 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1357 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1359 if (total_size > size)
1362 struct GNUNET_PeerIdentity id;
1364 GNUNET_PEER_resolve(info->peer->id, &id);
1365 info->peer->infos[info->handler_n] = info;
1366 info->peer->types[info->handler_n] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1367 info->peer->core_transmit[info->handler_n] =
1368 GNUNET_CORE_notify_transmit_ready (core_handle,
1371 GNUNET_TIME_UNIT_FOREVER_REL,
1374 &send_core_data_multicast,
1378 info->peer->core_transmit[info->handler_n] = NULL;
1379 info->peer->infos[info->handler_n] = NULL;
1380 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1381 msg->header.size = htons (total_size);
1382 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1383 msg->tid = htonl (info->origin->tid);
1384 memcpy (&msg[1], info->data, info->size);
1385 if (0 == --(*info->copies))
1387 if (NULL != info->client)
1389 /* FIXME One unresponsive neighbor (who doesn't "call" tmt_rdy) can lock
1390 * the client from sending anything else to the service.
1391 * - Call receive_done after certain timeout.
1392 * - Here cancel the timeout.
1394 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1396 GNUNET_free (info->data);
1397 GNUNET_free (info->copies);
1405 * Function called to notify a client about the socket
1406 * being ready to queue more data. "buf" will be
1407 * NULL and "size" zero if the socket was closed for
1408 * writing in the meantime.
1410 * @param cls closure (MeshDataDescriptor)
1411 * @param size number of bytes available in buf
1412 * @param buf where the callee should write the message
1413 * @return number of bytes written to buf
1416 send_core_path_ack (void *cls, size_t size, void *buf)
1418 struct MeshDataDescriptor *info = cls;
1419 struct GNUNET_MESH_PathACK *msg = buf;
1421 GNUNET_assert (NULL != info);
1424 info->peer->core_transmit[info->handler_n] = NULL;
1426 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1431 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1432 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1433 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1434 msg->tid = htonl (info->origin->tid);
1435 msg->peer_id = my_full_id;
1437 /* TODO add signature */
1439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
1440 return sizeof (struct GNUNET_MESH_PathACK);
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 (data itself)
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_data_raw (void *cls, size_t size, void *buf)
1458 struct GNUNET_MessageHeader *msg = cls;
1461 GNUNET_assert (NULL != msg);
1462 total_size = ntohs (msg->size);
1464 if (total_size > size)
1469 memcpy (buf, msg, total_size);
1477 * Send another peer a notification to destroy a tunnel
1478 * @param cls The tunnel to destroy
1479 * @param size Size in the buffer
1480 * @param buf Memory where to put the data to transmit
1481 * @return Size of data put in buffer
1484 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1486 struct MeshTunnel *t = cls;
1487 struct MeshClient *c;
1488 struct GNUNET_MESH_TunnelMessage *msg;
1492 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1493 /*FIXME*/ msg->header.size =
1494 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1495 msg->tunnel_id = htonl (t->id.tid);
1497 tunnel_destroy (c, t);
1498 return sizeof (struct GNUNET_MESH_TunnelMessage);
1504 * Send the message to all clients that have subscribed to its type
1506 * @param msg Pointer to the message itself
1507 * @return number of clients this message was sent to
1510 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1512 struct MeshClient *c;
1516 type = ntohs (msg->type);
1517 for (count = 0, c = clients; c != NULL; c = c->next)
1519 if (client_is_subscribed (type, c))
1522 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1532 * Notify the client that owns the tunnel that a peer has connected to it
1534 * @param t Tunnel whose owner to notify
1535 * @param id Short id of the peer that has connected
1538 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1540 struct GNUNET_MESH_PeerControl pc;
1542 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1543 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1544 pc.tunnel_id = htonl (t->local_tid);
1545 GNUNET_PEER_resolve (id, &pc.peer);
1546 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1547 &pc.header, GNUNET_NO);
1551 /******************************************************************************/
1552 /******************** MESH NETWORK HANDLERS **************************/
1553 /******************************************************************************/
1557 * Core handler for path creation
1558 * struct GNUNET_CORE_MessageHandler
1560 * @param cls closure
1561 * @param message message
1562 * @param peer peer identity this notification is about
1563 * @param atsi performance data
1564 * @return GNUNET_OK to keep the connection open,
1565 * GNUNET_SYSERR to close it (signal serious error)
1569 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1570 const struct GNUNET_MessageHeader *message,
1571 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1573 unsigned int own_pos;
1576 MESH_TunnelNumber tid;
1577 struct GNUNET_MESH_ManipulatePath *msg;
1578 struct GNUNET_PeerIdentity *pi;
1579 struct GNUNET_PeerIdentity id;
1580 GNUNET_HashCode hash;
1581 struct MeshPeerPath *path;
1582 struct MeshPeerInfo *dest_peer_info;
1583 struct MeshPeerInfo *orig_peer_info;
1584 struct MeshTunnel *t;
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1587 "MESH: Received a path create msg\n");
1588 size = ntohs (message->size);
1589 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1591 GNUNET_break_op (0);
1595 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1596 if (size % sizeof (struct GNUNET_PeerIdentity))
1598 GNUNET_break_op (0);
1601 size /= sizeof (struct GNUNET_PeerIdentity);
1604 GNUNET_break_op (0);
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1608 "MESH: path as %u hops.\n",
1610 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1612 tid = ntohl (msg->tid);
1613 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1614 t = tunnel_get (pi, tid);
1615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1616 "MESH: path is for tunnel %s [%X].\n",
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1622 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1623 t->id.oid = GNUNET_PEER_intern (pi);
1625 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1626 t->local_tid = next_local_tid++;
1627 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
1629 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1631 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1632 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1639 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1640 if (NULL == dest_peer_info)
1642 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1643 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1644 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1646 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1648 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1649 if (NULL == orig_peer_info)
1651 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1652 orig_peer_info->id = GNUNET_PEER_intern (pi);
1653 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1654 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1657 path = path_new (size);
1659 for (i = 0; i < size; i++)
1661 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1662 if (path->peers[i] == myid)
1666 { /* cannot be self, must be 'not found' */
1667 /* create path: self not found in path through self */
1668 GNUNET_break_op (0);
1669 path_destroy (path);
1670 /* FIXME error. destroy tunnel? leave for timeout? */
1673 if (own_pos == size - 1)
1675 /* It is for us! Send ack. */
1676 struct GNUNET_MESH_TunnelNotification cmsg;
1677 struct MeshDataDescriptor *info;
1680 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1681 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1682 info->origin = &t->id;
1683 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1684 GNUNET_assert (NULL != info->peer);
1685 for (j = 0; info->peer->core_transmit[j]; j++)
1687 if (j == (CORE_QUEUE_SIZE - 1))
1693 info->handler_n = j;
1694 info->peer->core_transmit[j] =
1695 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1696 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1697 sizeof (struct GNUNET_MESH_PathACK),
1698 &send_core_path_ack, info);
1699 cmsg.header.size = htons(sizeof(cmsg));
1700 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1701 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
1702 cmsg.tunnel_id = htonl(t->local_tid);
1703 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
1707 /* It's for somebody else! Retransmit. */
1708 struct MeshPathInfo *path_info;
1710 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1712 path_info->path = path;
1713 path_info->peer = dest_peer_info;
1715 path_add_to_peer (dest_peer_info, path);
1716 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1717 GNUNET_CORE_notify_transmit_ready (
1721 GNUNET_TIME_UNIT_FOREVER_REL,
1723 sizeof (struct GNUNET_MESH_ManipulatePath) +
1724 path->length * sizeof(struct GNUNET_PeerIdentity),
1725 &send_core_create_path,
1733 * Core handler for mesh network traffic going from the origin to a peer
1735 * @param cls closure
1736 * @param peer peer identity this notification is about
1737 * @param message message
1738 * @param atsi performance data
1739 * @return GNUNET_OK to keep the connection open,
1740 * GNUNET_SYSERR to close it (signal serious error)
1743 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1744 const struct GNUNET_MessageHeader *message,
1745 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1747 struct GNUNET_MESH_Unicast *msg;
1748 struct MeshTunnel *t;
1749 struct MeshPeerInfo *pi;
1752 size = ntohs (message->size);
1754 sizeof (struct GNUNET_MESH_Unicast) +
1755 sizeof (struct GNUNET_MessageHeader))
1760 msg = (struct GNUNET_MESH_Unicast *) message;
1761 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1764 /* TODO notify back: we don't know this tunnel */
1765 GNUNET_break_op (0);
1768 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1769 &msg->destination.hashPubKey);
1772 /* TODO maybe feedback, log to statistics */
1773 GNUNET_break_op (0);
1778 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1781 msg = GNUNET_malloc (size);
1782 memcpy (msg, message, size);
1783 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1784 GNUNET_TIME_UNIT_FOREVER_REL,
1785 path_get_first_hop (t->tree, pi->id),
1787 &send_core_data_raw, msg);
1793 * Core handler for mesh network traffic going from the origin to all peers
1795 * @param cls closure
1796 * @param message message
1797 * @param peer peer identity this notification is about
1798 * @param atsi performance data
1799 * @return GNUNET_OK to keep the connection open,
1800 * GNUNET_SYSERR to close it (signal serious error)
1802 * TODO: Check who we got this from, to validate route.
1805 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1806 const struct GNUNET_MessageHeader *message,
1807 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1809 struct GNUNET_MESH_Multicast *msg;
1810 struct GNUNET_PeerIdentity *id;
1811 struct MeshDataDescriptor *info;
1812 struct MeshTunnelTreeNode *n;
1813 struct MeshTunnel *t;
1814 unsigned int *copies;
1819 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
1820 if (size < sizeof (struct GNUNET_MessageHeader))
1822 GNUNET_break_op (0);
1825 msg = (struct GNUNET_MESH_Multicast *) message;
1826 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1830 /* TODO notify that we dont know that tunnel */
1831 GNUNET_break_op (0);
1835 /* Transmit to locally interested clients */
1836 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1838 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1840 n = t->tree->me->children_head;
1843 copies = GNUNET_malloc (sizeof (unsigned int));
1844 for (*copies = 0; NULL != n; n = n->next)
1846 n = t->tree->me->children_head;
1847 data = GNUNET_malloc (size);
1848 memcpy (data, &msg[1], size);
1851 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1852 info->origin = &t->id;
1855 info->copies = copies;
1856 info->client = t->client->handle;
1857 info->destination = n->peer;
1858 id = path_get_first_hop(t->tree, n->peer);
1859 info->peer = peer_info_get(id);
1860 GNUNET_assert (NULL != info->peer);
1861 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
1863 if (i == (CORE_QUEUE_SIZE - 1))
1869 info->handler_n = i;
1870 info->peer->infos[i] = info;
1871 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1872 info->peer->core_transmit[i] =
1873 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1874 GNUNET_TIME_UNIT_FOREVER_REL, id,
1875 ntohs (msg->header.size),
1876 &send_core_data_multicast, info);
1884 * Core handler for mesh network traffic
1886 * @param cls closure
1887 * @param message message
1888 * @param peer peer identity this notification is about
1889 * @param atsi performance data
1891 * @return GNUNET_OK to keep the connection open,
1892 * GNUNET_SYSERR to close it (signal serious error)
1895 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1896 const struct GNUNET_MessageHeader *message,
1897 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1899 struct GNUNET_MESH_ToOrigin *msg;
1900 struct GNUNET_PeerIdentity id;
1901 struct MeshPeerInfo *peer_info;
1902 struct MeshTunnel *t;
1905 size = ntohs (message->size);
1906 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1907 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1909 GNUNET_break_op (0);
1912 msg = (struct GNUNET_MESH_ToOrigin *) message;
1913 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1917 /* TODO notify that we dont know this tunnel (whom)? */
1921 if (t->id.oid == myid)
1923 if (NULL == t->client)
1925 /* got data packet for ownerless tunnel */
1926 GNUNET_break_op (0);
1929 /* TODO signature verification */
1930 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1934 peer_info = peer_info_get (&msg->oid);
1935 if (NULL == peer_info)
1937 /* unknown origin of tunnel */
1941 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1942 msg = GNUNET_malloc (size);
1943 memcpy (msg, message, size);
1944 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1945 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1946 &send_core_data_raw, msg);
1953 * Core handler for path ACKs
1955 * @param cls closure
1956 * @param message message
1957 * @param peer peer identity this notification is about
1958 * @param atsi performance data
1960 * @return GNUNET_OK to keep the connection open,
1961 * GNUNET_SYSERR to close it (signal serious error)
1964 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1965 const struct GNUNET_MessageHeader *message,
1966 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1968 struct GNUNET_MESH_PathACK *msg;
1969 struct MeshTunnelTreeNode *n;
1970 struct MeshPeerInfo *peer_info;
1971 struct MeshTunnel *t;
1973 msg = (struct GNUNET_MESH_PathACK *) message;
1974 t = tunnel_get (&msg->oid, msg->tid);
1977 /* TODO notify that we don't know the tunnel */
1981 /* Message for us? */
1982 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1984 if (NULL == t->client)
1986 GNUNET_break_op (0);
1989 peer_info = peer_info_get (&msg->peer_id);
1990 if (NULL == peer_info)
1992 GNUNET_break_op (0);
1995 n = tree_find_peer(t->tree->root, peer_info->id);
1998 GNUNET_break_op (0);
2001 n->status = MESH_PEER_READY;
2002 send_client_peer_connected(t, peer_info->id);
2006 peer_info = peer_info_get (&msg->oid);
2007 if (NULL == peer_info)
2009 /* If we know the tunnel, we should DEFINITELY know the peer */
2013 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2014 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2015 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2016 GNUNET_TIME_UNIT_FOREVER_REL,
2017 path_get_first_hop (t->tree,
2019 sizeof (struct GNUNET_MESH_PathACK),
2020 &send_core_data_raw, msg);
2026 * Functions to handle messages from core
2028 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2029 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2030 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2031 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2032 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2033 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2034 sizeof (struct GNUNET_MESH_PathACK)},
2040 /******************************************************************************/
2041 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2042 /******************************************************************************/
2045 * deregister_app: iterator for removing each application registered by a client
2047 * @param cls closure
2048 * @param key the hash of the application id (used to access the hashmap)
2049 * @param value the value stored at the key (client)
2051 * @return GNUNET_OK on success
2054 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2056 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2062 * notify_client_connection_failure: notify a client that the connection to the
2063 * requested remote peer is not possible (for instance, no route found)
2064 * Function called when the socket is ready to queue more data. "buf" will be
2065 * NULL and "size" zero if the socket was closed for writing in the meantime.
2067 * @param cls closure
2068 * @param size number of bytes available in buf
2069 * @param buf where the callee should write the message
2070 * @return number of bytes written to buf
2073 notify_client_connection_failure (void *cls, size_t size, void *buf)
2076 struct MeshPeerInfo *peer_info;
2077 struct GNUNET_MESH_PeerControl *msg;
2078 struct GNUNET_PeerIdentity id;
2080 if (0 == size && NULL == buf)
2082 // TODO retry? cancel?
2086 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2087 peer_info = (struct MeshPeerInfo *) cls;
2088 msg = (struct GNUNET_MESH_PeerControl *) buf;
2089 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2090 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2091 // msg->tunnel_id = htonl(peer_info->t->tid);
2092 GNUNET_PEER_resolve (peer_info->id, &id);
2093 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2101 * Send keepalive packets for a peer
2103 * @param cls Closure (tunnel for which to send the keepalive).
2104 * @param tc Notification context.
2106 * TODO: implement explicit multicast keepalive?
2109 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2111 struct MeshTunnel *t = cls;
2112 struct GNUNET_MessageHeader *payload;
2113 struct GNUNET_MESH_Multicast *msg;
2116 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2117 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2123 "MESH: sending keepalive for tunnel %d\n",
2126 size = sizeof(struct GNUNET_MESH_Multicast) +
2127 sizeof(struct GNUNET_MessageHeader);
2128 msg = GNUNET_malloc (size);
2129 msg->header.size = htons (size);
2130 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2131 msg->oid = my_full_id;
2132 msg->tid = htonl(t->id.tid);
2133 payload = (struct GNUNET_MessageHeader *) &msg[1];
2134 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2135 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2136 handle_mesh_data_multicast (NULL, &my_full_id, &msg->header, NULL);
2139 t->path_refresh_task =
2140 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2146 * Function to process paths received for a new peer addition. The recorded
2147 * paths form the initial tunnel, which can be optimized later.
2148 * Called on each result obtained for the DHT search.
2150 * @param cls closure
2151 * @param exp when will this value expire
2152 * @param key key of the result
2153 * @param type type of the result
2154 * @param size number of bytes in data
2155 * @param data pointer to the result data
2157 * TODO: re-issue the request after certain time? cancel after X results?
2160 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2161 const GNUNET_HashCode * key,
2162 const struct GNUNET_PeerIdentity *get_path,
2163 unsigned int get_path_length,
2164 const struct GNUNET_PeerIdentity *put_path,
2165 unsigned int put_path_length,
2166 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2168 struct MeshPathInfo *path_info = cls;
2169 struct MeshPathInfo *path_info_aux;
2170 struct MeshPeerPath *p;
2171 struct MeshPeerPath *aux;
2172 struct GNUNET_PeerIdentity pi;
2175 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2176 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2177 "MESH: Got results from DHT for %s\n",
2178 GNUNET_h2s_full(&pi.hashPubKey));
2179 GNUNET_DHT_get_stop(path_info->peer->dhtget);
2180 path_info->peer->dhtget = NULL;
2181 if (NULL == get_path || NULL == put_path)
2183 if (NULL == path_info->peer->path_head)
2185 // Find ourselves some alternate initial path to the destination: retry
2186 GNUNET_PEER_resolve(path_info->peer->id, &pi);
2187 path_info->peer->dhtget =
2188 GNUNET_DHT_get_start(dht_handle, /* handle */
2189 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2190 GNUNET_BLOCK_TYPE_TEST, /* type */
2191 &pi.hashPubKey, /*key to search */
2192 4, /* replication level */
2193 GNUNET_DHT_RO_RECORD_ROUTE |
2194 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2196 0, /* xquery bits */
2197 &dht_get_id_handler,
2198 (void *) path_info);
2203 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2204 path_add_to_peer (path_info->peer, p);
2205 for (i = 0; i < path_info->peer->ntunnels; i++)
2207 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2208 aux = tree_get_path_to_peer(path_info->peer->tunnels[i]->tree,
2209 path_info->peer->id);
2210 if (aux->length > 1)
2212 struct GNUNET_PeerIdentity id;
2214 path_info_aux = GNUNET_malloc (sizeof (struct MeshPathInfo));
2215 path_info_aux->path = aux;
2216 path_info_aux->peer = path_info->peer;
2217 path_info_aux->t = path_info->t;
2218 GNUNET_PEER_resolve (p->peers[1], &id);
2219 GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
2222 GNUNET_TIME_UNIT_FOREVER_REL,
2225 sizeof (struct GNUNET_MESH_ManipulatePath)
2228 sizeof (struct GNUNET_PeerIdentity)),
2230 &send_core_create_path,
2232 path_info_aux); /* cls */
2236 send_client_peer_connected(path_info->t, myid);
2239 GNUNET_free (path_info);
2246 * Function to process paths received for a new peer addition. The recorded
2247 * paths form the initial tunnel, which can be optimized later.
2248 * Called on each result obtained for the DHT search.
2250 * @param cls closure
2251 * @param exp when will this value expire
2252 * @param key key of the result
2253 * @param type type of the result
2254 * @param size number of bytes in data
2255 * @param data pointer to the result data
2258 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2259 const GNUNET_HashCode * key,
2260 const struct GNUNET_PeerIdentity *get_path,
2261 unsigned int get_path_length,
2262 const struct GNUNET_PeerIdentity *put_path,
2263 unsigned int put_path_length,
2264 enum GNUNET_BLOCK_Type type, size_t size,
2267 const struct GNUNET_PeerIdentity *pi = data;
2268 struct GNUNET_PeerIdentity id;
2269 struct MeshTunnel *t = cls;
2270 struct MeshPeerInfo *peer_info;
2271 struct MeshPathInfo *path_info;
2272 struct MeshPeerPath *p;
2275 if (size != sizeof (struct GNUNET_PeerIdentity))
2277 GNUNET_break_op (0);
2280 GNUNET_assert (NULL != t->client);
2281 GNUNET_DHT_get_stop (t->dht_get_type);
2282 t->dht_get_type = NULL;
2283 peer_info = peer_info_get (pi);
2284 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2285 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2287 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2288 NULL == peer_info->dhtget)
2290 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2291 path_info->peer = peer_info;
2293 /* we don't have a route to the peer, let's try a direct lookup */
2295 GNUNET_DHT_get_start (dht_handle, /* handle */
2296 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2297 GNUNET_BLOCK_TYPE_TEST, /* block type */
2298 &pi->hashPubKey, /* key to look up */
2299 10U, /* replication level */
2300 GNUNET_DHT_RO_RECORD_ROUTE |
2301 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2302 /* option to dht: record route */
2304 0, /* xquery bits */
2305 dht_get_id_handler, /* callback */
2306 path_info); /* closure */
2310 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2311 path_add_to_peer (peer_info, p);
2312 tunnel_add_peer(t, peer_info);
2313 p = tree_get_path_to_peer(t->tree, peer_info->id);
2315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2316 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2317 t->local_tid, p->length);
2318 for (i = 0; i < p->length; i++)
2320 GNUNET_PEER_resolve (p->peers[0], &id);
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2322 GNUNET_h2s_full (&id.hashPubKey));
2328 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2330 path_info->peer = peer_info;
2331 path_info->path = p;
2332 GNUNET_PEER_resolve (p->peers[1], &id);
2333 GNUNET_CORE_notify_transmit_ready (core_handle,
2339 GNUNET_TIME_UNIT_FOREVER_REL,
2343 sizeof (struct GNUNET_MESH_ManipulatePath)
2346 sizeof (struct GNUNET_PeerIdentity)),
2348 &send_core_create_path,
2350 path_info); /* cls */
2354 send_client_peer_connected(t, myid);
2358 /******************************************************************************/
2359 /********************* MESH LOCAL HANDLES **************************/
2360 /******************************************************************************/
2364 * Handler for client disconnection
2366 * @param cls closure
2367 * @param client identification of the client; NULL
2368 * for the last call when the server is destroyed
2371 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2373 struct MeshClient *c;
2374 struct MeshClient *next;
2376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2382 if (c->handle != client && NULL != client)
2384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2389 if (NULL != c->tunnels)
2391 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2392 &tunnel_destroy_iterator,
2394 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2397 /* deregister clients applications */
2398 if (NULL != c->apps)
2400 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2401 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2403 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2404 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2406 GNUNET_SCHEDULER_cancel (announce_applications_task);
2407 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2409 if (NULL != c->types)
2410 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2411 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2423 * Handler for new clients
2425 * @param cls closure
2426 * @param client identification of the client
2427 * @param message the actual message, which includes messages the client wants
2430 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2431 const struct GNUNET_MessageHeader *message)
2433 struct GNUNET_MESH_ClientConnect *cc_msg;
2434 struct MeshClient *c;
2435 GNUNET_MESH_ApplicationType *a;
2442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2443 /* Check data sanity */
2444 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2445 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2446 ntypes = ntohs (cc_msg->types);
2447 napps = ntohs (cc_msg->applications);
2449 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2452 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2456 /* Create new client structure */
2457 c = GNUNET_malloc (sizeof (struct MeshClient));
2459 c->id = next_client_id++;
2462 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2465 GNUNET_MESH_ApplicationType at;
2468 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2469 for (i = 0; i < napps; i++)
2472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2473 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2474 /* store in clients hashmap */
2475 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2476 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2477 /* store in global hashmap, for announcements */
2478 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2479 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2481 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2482 announce_applications_task =
2483 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2491 t = (uint16_t *) & a[napps];
2492 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2493 for (i = 0; i < ntypes; i++)
2496 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2498 /* store in clients hashmap */
2499 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2500 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2501 /* store in global hashmap */
2502 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2503 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2507 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2509 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2510 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2511 GNUNET_SERVER_notification_context_add (nc, client);
2513 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2521 * Handler for requests of new tunnels
2523 * @param cls closure
2524 * @param client identification of the client
2525 * @param message the actual message
2528 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2529 const struct GNUNET_MessageHeader *message)
2531 struct GNUNET_MESH_TunnelMessage *t_msg;
2532 struct MeshTunnel *t;
2533 struct MeshClient *c;
2534 GNUNET_HashCode hash;
2536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2538 /* Sanity check for client registration */
2539 if (NULL == (c = client_get (client)))
2542 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2549 /* Message sanity check */
2550 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2553 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2557 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2558 /* Sanity check for tunnel numbering */
2559 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2562 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2565 /* Sanity check for duplicate tunnel IDs */
2566 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2573 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2575 while (NULL != tunnel_get_by_pi (myid, next_tid))
2576 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2577 t->id.tid = next_tid++;
2579 t->local_tid = ntohl (t_msg->tunnel_id);
2581 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2583 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2585 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2586 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2589 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2593 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2595 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2596 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2599 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2602 t->tree = tree_new (t, myid);
2603 t->tree->refresh = REFRESH_PATH_TIME;
2604 t->tree->root->status = MESH_PEER_READY;
2605 t->tree->me = t->tree->root;
2607 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2613 * Handler for requests of deleting tunnels
2615 * @param cls closure
2616 * @param client identification of the client
2617 * @param message the actual message
2620 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2621 const struct GNUNET_MessageHeader *message)
2623 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2624 struct MeshClient *c;
2625 struct MeshTunnel *t;
2626 MESH_TunnelNumber tid;
2627 GNUNET_HashCode hash;
2629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2631 /* Sanity check for client registration */
2632 if (NULL == (c = client_get (client)))
2635 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2638 /* Message sanity check */
2639 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2642 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2648 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2650 /* Retrieve tunnel */
2651 tid = ntohl (tunnel_msg->tunnel_id);
2653 /* Remove from local id hashmap */
2654 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2655 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2656 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2658 /* Remove from global id hashmap */
2659 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2660 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2662 // notify_tunnel_destroy(t); FIXME
2664 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2670 * Handler for connection requests to new peers
2672 * @param cls closure
2673 * @param client identification of the client
2674 * @param message the actual message (PeerControl)
2677 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2678 const struct GNUNET_MessageHeader *message)
2680 struct GNUNET_MESH_PeerControl *peer_msg;
2681 struct MeshPathInfo *path_info;
2682 struct MeshPeerInfo *peer_info;
2683 struct MeshClient *c;
2684 struct MeshTunnel *t;
2685 MESH_TunnelNumber tid;
2687 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
2688 /* Sanity check for client registration */
2689 if (NULL == (c = client_get (client)))
2692 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2696 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2697 /* Sanity check for message size */
2698 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2705 /* Tunnel exists? */
2706 tid = ntohl (peer_msg->tunnel_id);
2707 t = tunnel_get_by_local_id (c, tid);
2711 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2715 /* Does client own tunnel? */
2716 if (t->client->handle != client)
2719 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2722 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
2723 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
2724 peer_info = peer_info_get (&peer_msg->peer);
2726 /* Start DHT search if needed, otherwise just add peer to tunnel. */
2727 if (NULL == peer_info->dhtget && NULL == peer_info->path_head)
2729 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2730 path_info->peer = peer_info;
2733 GNUNET_DHT_get_start(dht_handle, /* handle */
2734 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2735 GNUNET_BLOCK_TYPE_TEST, /* type */
2736 &peer_msg->peer.hashPubKey, /*key to search */
2737 4, /* replication level */
2738 GNUNET_DHT_RO_RECORD_ROUTE |
2739 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2741 0, /* xquery bits */
2742 &dht_get_id_handler,
2743 (void *) path_info);
2745 else if (NULL != peer_info->path_head)
2748 for (i = 0; i < CORE_QUEUE_SIZE; i++)
2750 if (NULL == peer_info->core_transmit[i])
2753 if (CORE_QUEUE_SIZE == i)
2756 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2759 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2760 path_info->peer = peer_info;
2762 tunnel_add_peer(t, peer_info);
2763 path_info->path = tree_get_path_to_peer(t->tree, peer_info->id);
2764 peer_info = peer_info_get(path_get_first_hop(t->tree, path_info->peer->id));
2765 peer_info->infos[i] = path_info;
2766 peer_info->types[i] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
2767 peer_info->core_transmit[i] =
2768 GNUNET_CORE_notify_transmit_ready (
2772 GNUNET_TIME_UNIT_FOREVER_REL,
2773 path_get_first_hop(t->tree, path_info->peer->id),
2774 sizeof (struct GNUNET_MESH_ManipulatePath)
2775 + path_info->path->length * sizeof(struct GNUNET_PeerIdentity),
2776 &send_core_create_path,
2779 /* Otherwise: there is no path yet, but there is a DHT_get active already. */
2780 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2786 * Handler for disconnection requests of peers in a tunnel
2788 * @param cls closure
2789 * @param client identification of the client
2790 * @param message the actual message (PeerControl)
2793 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2794 const struct GNUNET_MessageHeader *message)
2796 struct GNUNET_MESH_PeerControl *peer_msg;
2797 struct MeshClient *c;
2798 struct MeshTunnel *t;
2799 MESH_TunnelNumber tid;
2801 /* Sanity check for client registration */
2802 if (NULL == (c = client_get (client)))
2805 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2808 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2809 /* Sanity check for message size */
2810 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2813 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2817 /* Tunnel exists? */
2818 tid = ntohl (peer_msg->tunnel_id);
2819 t = tunnel_get_by_local_id (c, tid);
2823 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2827 /* Does client own tunnel? */
2828 if (t->client->handle != client)
2831 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2835 /* Ok, delete peer from tunnel */
2836 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2837 &peer_msg->peer.hashPubKey);
2839 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2845 * Handler for connection requests to new peers by type
2847 * @param cls closure
2848 * @param client identification of the client
2849 * @param message the actual message (ConnectPeerByType)
2852 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2853 const struct GNUNET_MessageHeader *message)
2855 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2856 struct MeshClient *c;
2857 struct MeshTunnel *t;
2858 GNUNET_HashCode hash;
2859 MESH_TunnelNumber tid;
2861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2862 /* Sanity check for client registration */
2863 if (NULL == (c = client_get (client)))
2866 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2870 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2871 /* Sanity check for message size */
2872 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2873 ntohs (connect_msg->header.size))
2876 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2880 /* Tunnel exists? */
2881 tid = ntohl (connect_msg->tunnel_id);
2882 t = tunnel_get_by_local_id (c, tid);
2886 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2890 /* Does client own tunnel? */
2891 if (t->client->handle != client)
2894 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2898 /* Do WE have the service? */
2899 t->type = ntohl (connect_msg->type);
2900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
2901 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2902 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2905 /* Yes! Fast forward, add ourselves to the tunnel and send the
2906 * good news to the client
2908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2909 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2910 peer_info_get (&my_full_id),
2911 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2914 send_client_peer_connected(t, myid);
2915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2916 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2919 /* Ok, lets find a peer offering the service */
2920 if (NULL != t->dht_get_type)
2922 GNUNET_DHT_get_stop (t->dht_get_type);
2924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2925 GNUNET_h2s_full (&hash));
2927 GNUNET_DHT_get_start (dht_handle,
2928 GNUNET_TIME_UNIT_FOREVER_REL,
2929 GNUNET_BLOCK_TYPE_TEST,
2932 GNUNET_DHT_RO_RECORD_ROUTE |
2933 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2935 &dht_get_type_handler, t);
2937 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2943 * Handler for client traffic directed to one peer
2945 * @param cls closure
2946 * @param client identification of the client
2947 * @param message the actual message
2950 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2951 const struct GNUNET_MessageHeader *message)
2953 struct MeshClient *c;
2954 struct MeshTunnel *t;
2955 struct MeshPeerInfo *pi;
2956 struct GNUNET_MESH_Unicast *data_msg;
2957 MESH_TunnelNumber tid;
2960 /* Sanity check for client registration */
2961 if (NULL == (c = client_get (client)))
2964 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2967 data_msg = (struct GNUNET_MESH_Unicast *) message;
2968 /* Sanity check for message size */
2969 size = ntohs (message->size);
2970 if (sizeof (struct GNUNET_MESH_Unicast) +
2971 sizeof (struct GNUNET_MessageHeader) > size)
2974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2978 /* Tunnel exists? */
2979 tid = ntohl (data_msg->tid);
2980 t = tunnel_get_by_local_id (c, tid);
2984 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2988 /* Is it a local tunnel? Then, does client own the tunnel? */
2989 if (t->client->handle != NULL && t->client->handle != client)
2992 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2996 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2997 &data_msg->destination.hashPubKey);
2998 /* Is the selected peer in the tunnel? */
3002 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3006 /* Ok, everything is correct, send the message
3007 * (pretend we got it from a mesh peer)
3010 char buf[ntohs (message->size)];
3011 struct GNUNET_MESH_Unicast *copy;
3013 /* Work around const limitation */
3014 copy = (struct GNUNET_MESH_Unicast *) buf;
3015 memcpy (buf, data_msg, size);
3016 copy->oid = my_full_id;
3017 copy->tid = htonl (t->id.tid);
3018 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
3020 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3025 * Handler for client traffic directed to all peers in a tunnel
3027 * @param cls closure
3028 * @param client identification of the client
3029 * @param message the actual message
3032 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3033 const struct GNUNET_MessageHeader *message)
3035 struct MeshClient *c;
3036 struct MeshTunnel *t;
3037 struct GNUNET_MESH_Multicast *data_msg;
3038 MESH_TunnelNumber tid;
3040 /* Sanity check for client registration */
3041 if (NULL == (c = client_get (client)))
3044 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3047 data_msg = (struct GNUNET_MESH_Multicast *) message;
3048 /* Sanity check for message size */
3049 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
3052 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3056 /* Tunnel exists? */
3057 tid = ntohl (data_msg->tid);
3058 t = tunnel_get_by_local_id (c, tid);
3062 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3066 /* Does client own tunnel? */
3067 if (t->client->handle != client)
3070 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3075 char buf[ntohs(message->size)];
3076 struct GNUNET_MESH_Multicast *copy;
3078 copy = (struct GNUNET_MESH_Multicast *)buf;
3079 memcpy(buf, message, ntohs(message->size));
3080 copy->oid = my_full_id;
3081 copy->tid = htonl(t->id.tid);
3082 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3085 /* receive done gets called when last copy is sent */
3090 * Functions to handle messages from clients
3092 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3093 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3094 {&handle_local_tunnel_create, NULL,
3095 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3096 sizeof (struct GNUNET_MESH_TunnelMessage)},
3097 {&handle_local_tunnel_destroy, NULL,
3098 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3099 sizeof (struct GNUNET_MESH_TunnelMessage)},
3100 {&handle_local_connect_add, NULL,
3101 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3102 sizeof (struct GNUNET_MESH_PeerControl)},
3103 {&handle_local_connect_del, NULL,
3104 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3105 sizeof (struct GNUNET_MESH_PeerControl)},
3106 {&handle_local_connect_by_type, NULL,
3107 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3108 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3109 {&handle_local_unicast, NULL,
3110 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3111 {&handle_local_unicast, NULL,
3112 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3113 {&handle_local_multicast, NULL,
3114 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3120 * To be called on core init/fail.
3122 * @param cls service closure
3123 * @param server handle to the server for this service
3124 * @param identity the public identity of this peer
3125 * @param publicKey the public key of this peer
3128 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3129 const struct GNUNET_PeerIdentity *identity,
3130 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
3132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3133 core_handle = server;
3134 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3136 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3137 GNUNET_SCHEDULER_shutdown();
3143 * Method called whenever a given peer connects.
3145 * @param cls closure
3146 * @param peer peer identity this notification is about
3147 * @param atsi performance data for the connection
3150 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3151 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3153 struct MeshPeerInfo *peer_info;
3154 struct MeshPeerPath *path;
3156 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3157 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3158 // GNUNET_h2s(&my_full_id.hashPubKey));
3159 peer_info = peer_info_get (peer);
3160 if (myid == peer_info->id)
3162 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3167 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3168 // GNUNET_h2s(&peer->hashPubKey));
3170 path = path_new (2);
3171 path->peers[0] = myid;
3172 path->peers[1] = peer_info->id;
3173 GNUNET_PEER_change_rc(myid, 1);
3174 GNUNET_PEER_change_rc(peer_info->id, 1);
3175 path_add_to_peer (peer_info, path);
3180 * Method called whenever a peer disconnects.
3182 * @param cls closure
3183 * @param peer peer identity this notification is about
3186 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3188 struct MeshPeerInfo *pi;
3191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3192 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3198 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3200 if (pi->core_transmit[i])
3202 struct MeshDataDescriptor *dd;
3203 struct MeshPathInfo *path_info;
3204 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3205 /* TODO: notify that tranmission has failed */
3206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: data at %u\n", i);
3207 switch (pi->types[i])
3209 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3210 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3211 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
3214 if (0 == --(*dd->copies))
3216 GNUNET_free (dd->copies);
3217 GNUNET_free (dd->data);
3220 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
3222 path_info = pi->infos[i];
3223 path_destroy(path_info->path);
3226 GNUNET_free (pi->infos[i]);
3229 path_remove_from_peer (pi, pi->id, myid);
3232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3238 /******************************************************************************/
3239 /************************ MAIN FUNCTIONS ****************************/
3240 /******************************************************************************/
3243 * Task run during shutdown.
3249 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3252 /* TODO: destroy tunnels? */
3253 if (core_handle != NULL)
3255 GNUNET_CORE_disconnect (core_handle);
3258 if (dht_handle != NULL)
3260 GNUNET_DHT_disconnect (dht_handle);
3265 GNUNET_SERVER_notification_context_destroy (nc);
3268 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3270 GNUNET_SCHEDULER_cancel (announce_id_task);
3271 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3277 * Process mesh requests.
3279 * @param cls closure
3280 * @param server the initialized server
3281 * @param c configuration to use
3284 run (void *cls, struct GNUNET_SERVER_Handle *server,
3285 const struct GNUNET_CONFIGURATION_Handle *c)
3287 struct MeshPeerInfo *peer;
3288 struct MeshPeerPath *p;
3291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3292 server_handle = server;
3293 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3294 CORE_QUEUE_SIZE, /* queue size */
3295 NULL, /* Closure passed to MESH functions */
3296 &core_init, /* Call core_init once connected */
3297 &core_connect, /* Handle connects */
3298 &core_disconnect, /* remove peers on disconnects */
3299 NULL, /* Do we care about "status" updates? */
3300 NULL, /* Don't notify about all incoming messages */
3301 GNUNET_NO, /* For header only in notification */
3302 NULL, /* Don't notify about all outbound messages */
3303 GNUNET_NO, /* For header-only out notification */
3304 core_handlers); /* Register these handlers */
3305 if (core_handle == NULL)
3308 GNUNET_SCHEDULER_shutdown ();
3313 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3316 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3318 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3319 GNUNET_SCHEDULER_shutdown ();
3322 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3323 GNUNET_free (keyfile);
3324 if (my_private_key == NULL)
3326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3327 _("Mesh service could not access hostkey. Exiting.\n"));
3328 GNUNET_SCHEDULER_shutdown ();
3331 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3332 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3333 &my_full_id.hashPubKey);
3334 myid = GNUNET_PEER_intern (&my_full_id);
3336 dht_handle = GNUNET_DHT_connect (c, 64);
3337 if (dht_handle == NULL)
3343 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3345 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3346 peers = GNUNET_CONTAINER_multihashmap_create (32);
3347 applications = GNUNET_CONTAINER_multihashmap_create (32);
3348 types = GNUNET_CONTAINER_multihashmap_create (32);
3350 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3351 nc = GNUNET_SERVER_notification_context_create (server_handle,
3353 GNUNET_SERVER_disconnect_notify (server_handle,
3354 &handle_local_client_disconnect,
3359 clients_tail = NULL;
3364 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3365 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3367 /* Create a peer_info for the local peer */
3368 peer = peer_info_get(&my_full_id);
3371 path_add_to_peer(peer, p);
3373 /* Scheduled the task to clean up when shutdown is called */
3374 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3381 * The main function for the mesh service.
3383 * @param argc number of arguments from the command line
3384 * @param argv command line arguments
3385 * @return 0 ok, 1 on error
3388 main (int argc, char *const *argv)
3393 // fprintf (stderr, "main ()\n");
3395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3398 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3402 // fprintf (stderr, "main () END\n");