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 void *infos[CORE_QUEUE_SIZE];
152 * Type of message being in each transmission
154 uint16_t types[CORE_QUEUE_SIZE];
157 * Array of tunnels this peer participates in
158 * (most probably a small amount, therefore not a hashmap)
159 * When the path to the peer changes, notify these tunnels to let them
160 * re-adjust their path trees.
162 struct MeshTunnel **tunnels;
165 * Number of tunnels above
167 unsigned int ntunnels;
172 * Data scheduled to transmit (to local client or remote peer)
179 struct MeshQueue *next;
180 struct MeshQueue *prev;
183 * Target of the data (NULL if target is client)
185 struct MeshPeerInfo *peer;
188 * Client to send the data to (NULL if target is peer)
190 struct MeshClient *client;
193 * Size of the message to transmit
198 * How old is the data?
200 struct GNUNET_TIME_Absolute timestamp;
205 struct GNUNET_MessageHeader *data;
209 * Globally unique tunnel identification (owner + number)
210 * DO NOT USE OVER THE NETWORK
215 * Node that owns the tunnel
220 * Tunnel number to differentiate all the tunnels owned by the node oid
221 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
223 MESH_TunnelNumber tid;
227 struct MeshClient; /* FWD declaration */
230 * Struct containing all information regarding a tunnel
231 * For an intermediate node the improtant info used will be:
232 * - id Tunnel unique identification
233 * - paths[0] To know where to send it next
234 * - metainfo: ready, speeds, accounting
241 struct MESH_TunnelID id;
244 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
246 MESH_TunnelNumber local_tid;
249 * Last time the tunnel was used
251 struct GNUNET_TIME_Absolute timestamp;
254 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
256 struct GNUNET_CONTAINER_MultiHashMap *peers;
259 * Number of peers that are connected and potentially ready to receive data
261 unsigned int peers_ready;
264 * Number of peers that have been added to the tunnel
266 unsigned int peers_total;
269 * Client owner of the tunnel, if any
271 struct MeshClient *client;
274 * Messages ready to transmit
276 struct MeshQueue *queue_head;
277 struct MeshQueue *queue_tail;
282 struct MeshTunnelTree *tree;
285 * Task to keep the used paths alive
287 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
292 * Info needed to work with tunnel paths and peers
299 struct MeshTunnel *t;
304 struct MeshPeerInfo *peer;
309 struct MeshPeerPath *path;
314 * Struct containing information about a client of the service
321 struct MeshClient *next;
322 struct MeshClient *prev;
325 * Tunnels that belong to this client, indexed by local id
327 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
330 * Handle to communicate with the client
332 struct GNUNET_SERVER_Client *handle;
335 * Applications that this client has claimed to provide
337 struct GNUNET_CONTAINER_MultiHashMap *apps;
340 * Messages that this client has declared interest in
342 struct GNUNET_CONTAINER_MultiHashMap *types;
345 * Used to search peers offering a service
347 struct GNUNET_DHT_GetHandle *dht_get_type;
351 * ID of the client, for debug messages
360 /******************************************************************************/
361 /************************ DEBUG FUNCTIONS ****************************/
362 /******************************************************************************/
366 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
367 * @param cls string to print
368 * @param tc task context
371 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
375 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
383 /******************************************************************************/
384 /*********************** GLOBAL VARIABLES ****************************/
385 /******************************************************************************/
390 static struct MeshClient *clients;
391 static struct MeshClient *clients_tail;
394 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
396 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
399 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
401 static struct GNUNET_CONTAINER_MultiHashMap *peers;
404 * Handle to communicate with core
406 static struct GNUNET_CORE_Handle *core_handle;
411 static struct GNUNET_DHT_Handle *dht_handle;
416 static struct GNUNET_SERVER_Handle *server_handle;
419 * Notification context, to send messages to local clients
421 static struct GNUNET_SERVER_NotificationContext *nc;
424 * Local peer own ID (memory efficient handle)
426 static GNUNET_PEER_Id myid;
429 * Local peer own ID (full value)
431 static struct GNUNET_PeerIdentity my_full_id;
436 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
441 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
444 * Tunnel ID for the next created tunnel (global tunnel number)
446 static MESH_TunnelNumber next_tid;
449 * Tunnel ID for the next incoming tunnel (local tunnel number)
451 static MESH_TunnelNumber next_local_tid;
454 * All application types provided by this peer
456 static struct GNUNET_CONTAINER_MultiHashMap *applications;
459 * All message types clients of this peer are interested in
461 static struct GNUNET_CONTAINER_MultiHashMap *types;
464 * Task to periodically announce provided applications
466 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
469 * Task to periodically announce itself in the network
471 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
474 unsigned int next_client_id;
478 /******************************************************************************/
479 /************************ ITERATORS ****************************/
480 /******************************************************************************/
482 /* FIXME move iterators here */
485 /******************************************************************************/
486 /************************ PERIODIC FUNCTIONS ****************************/
487 /******************************************************************************/
490 * Announce iterator over for each application provided by the peer
493 * @param key current key code
494 * @param value value in the hash map
495 * @return GNUNET_YES if we should continue to
500 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
502 /* FIXME are hashes in multihash map equal on all aquitectures? */
504 GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
505 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
506 (const char *) &my_full_id,
508 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
509 &mesh_debug, "DHT_put for app completed");
511 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
513 APP_ANNOUNCE_TIME, NULL, NULL);
520 * Periodically announce what applications are provided by local clients
523 * @param tc task context
526 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
528 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
530 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
534 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
536 announce_applications_task =
537 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
545 * Periodically announce self id in the DHT
548 * @param tc task context
551 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
553 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
555 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
559 * - Set data expiration in function of X
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
563 GNUNET_h2s_full (&my_full_id.hashPubKey));
564 GNUNET_DHT_put (dht_handle, /* DHT handle */
565 &my_full_id.hashPubKey, /* Key to use */
566 10U, /* Replication level */
567 GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
568 GNUNET_BLOCK_TYPE_TEST, /* Block type */
569 sizeof(my_full_id), /* Size of the data */
570 (char *)&my_full_id, /* Data itself */
571 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
572 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
574 &mesh_debug, "DHT_put for id completed");
576 NULL, /* Continuation */
577 NULL); /* Continuation closure */
580 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
585 * Function to process paths received for a new peer addition. The recorded
586 * paths form the initial tunnel, which can be optimized later.
587 * Called on each result obtained for the DHT search.
590 * @param exp when will this value expire
591 * @param key key of the result
592 * @param type type of the result
593 * @param size number of bytes in data
594 * @param data pointer to the result data
597 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
598 const GNUNET_HashCode * key,
599 const struct GNUNET_PeerIdentity *get_path,
600 unsigned int get_path_length,
601 const struct GNUNET_PeerIdentity *put_path,
602 unsigned int put_path_length,
603 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
606 /******************************************************************************/
607 /****************** GENERAL HELPER FUNCTIONS ************************/
608 /******************************************************************************/
611 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
612 * and insert it in the appropiate structures if the peer is not known yet.
614 * @param peer Identity of the peer
616 * @return Existing or newly created peer info
618 static struct MeshPeerInfo *
619 peer_info_get (const struct GNUNET_PeerIdentity *peer)
621 struct MeshPeerInfo *peer_info;
623 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
624 if (NULL == peer_info)
627 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
628 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
629 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
630 peer_info->id = GNUNET_PEER_intern (peer);
639 * Destroy the peer_info and free any allocated resources linked to it
640 * @param t tunnel the path belongs to
641 * @param pi the peer_info to destroy
642 * @return GNUNET_OK on success
645 peer_info_destroy (struct MeshPeerInfo *pi)
647 GNUNET_HashCode hash;
648 struct GNUNET_PeerIdentity id;
650 GNUNET_PEER_resolve (pi->id, &id);
651 GNUNET_PEER_change_rc (pi->id, -1);
652 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
654 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
655 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
663 * Notify a tunnel that a connection has broken that affects at least
666 * @param t Tunnel affected.
667 * @param peer Peer that (at least) has been affected by the disconnection.
668 * @param p1 Peer that got disconnected from p2.
669 * @param p2 Peer that got disconnected from p1.
671 * @return Short ID of the peer disconnected (either p1 or p2).
672 * 0 if the tunnel remained unaffected.
674 static GNUNET_PEER_Id
675 tunnel_notify_connection_broken (struct MeshTunnel *t,
676 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
680 * Remove all paths that rely on a direct connection between p1 and p2
681 * from the peer itself and notify all tunnels about it.
683 * @param peer PeerInfo of affected peer.
684 * @param p1 GNUNET_PEER_Id of one peer.
685 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
688 * TODO: optimize (see below)
691 path_remove_from_peer (struct MeshPeerInfo *peer,
695 struct GNUNET_PeerIdentity id;
696 struct MeshPeerPath *p;
697 struct MeshPeerPath *aux;
698 struct MeshPeerInfo *peer_d;
700 unsigned int destroyed;
710 for (i = 0; i < (p->length - 1); i++)
712 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
713 (p->peers[i] == p2 && p->peers[i + 1] == p1))
725 for (i = 0; i < peer->ntunnels; i++)
727 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
729 * Problem: one or more peers have been deleted from the tunnel tree.
730 * We don't know who they are to try to add them again.
731 * We need to try to find a new path for each of the disconnected peers.
732 * Some of them might already have a path to reach them that does not
733 * involve p1 and p2. Adding all anew might render in a better tree than
734 * the trivial immediate fix.
736 * Trivial immiediate fix: try to reconnect to the disconnected node. All
737 * its children will be reachable trough him.
739 GNUNET_PEER_resolve(d, &id);
740 peer_d = peer_info_get(&id);
743 for (p = peer_d->path_head; NULL != p; p = p->next)
745 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
753 /* No callback, as peer will be already disconnected */
754 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
758 struct MeshPathInfo *path_info;
760 if (NULL != peer_d->dhtget)
762 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
764 path_info->peer = peer_d;
765 path_info->t = peer->tunnels[i];
766 peer_d->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
767 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
768 GNUNET_BLOCK_TYPE_TEST, /* type */
769 &id.hashPubKey, /*key to search */
770 4, /* replication level */
771 GNUNET_DHT_RO_RECORD_ROUTE,
782 * Add the path to the peer and update the path used to reach it in case this
785 * @param peer_info Destination peer to add the path to.
786 * @param path New path to add. Last peer must be the peer in arg 1.
787 * Path will be either used of freed if already known.
789 * TODO: trim the part from origin to us? Add it as path to origin?
792 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
794 struct MeshPeerPath *aux;
798 if (NULL == peer_info || NULL == path)
804 l = path_get_length (path);
806 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
808 l2 = path_get_length (aux);
811 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
812 peer_info->path_tail, aux, path);
816 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
823 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
830 * Add the path to the origin peer and update the path used to reach it in case
831 * this is the shortest.
832 * The path is given in peer_info -> destination, therefore we turn the path
835 * @param peer_info Peer to add the path to, being the origin of the path.
836 * @param path New path to add after being inversed.
839 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
842 path_add_to_peer (peer_info, path);
847 * Build a PeerPath from the paths returned from the DHT, reversing the paths
848 * to obtain a local peer -> destination path and interning the peer ids.
850 * @return Newly allocated and created path
852 static struct MeshPeerPath *
853 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
854 unsigned int get_path_length,
855 const struct GNUNET_PeerIdentity *put_path,
856 unsigned int put_path_length)
858 struct MeshPeerPath *p;
864 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
865 for (i--; i >= 0; i--)
867 id = GNUNET_PEER_intern (&get_path[i]);
868 if (p->length > 0 && id == p->peers[p->length - 1])
870 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
871 GNUNET_PEER_change_rc(id, -1);
876 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
877 p->peers[p->length] = id;
882 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
883 for (i--; i >= 0; i--)
885 id = GNUNET_PEER_intern (&put_path[i]);
886 if (p->length > 0 && id == p->peers[p->length - 1])
888 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
889 GNUNET_PEER_change_rc(id, -1);
894 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
895 p->peers[p->length] = id;
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "MESH: (first of GET: %s)\n",
902 GNUNET_h2s_full(&get_path[0].hashPubKey));
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "MESH: (first of PUT: %s)\n",
905 GNUNET_h2s_full(&put_path[0].hashPubKey));
906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907 "MESH: In total: %d hops\n",
915 * Send keepalive packets for a peer
917 * @param cls Closure (tunnel for which to send the keepalive).
918 * @param tc Notification context.
920 * TODO: implement explicit multicast keepalive?
923 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
927 * Check if client has registered with the service and has not disconnected
929 * @param client the client to check
931 * @return non-NULL if client exists in the global DLL
933 static struct MeshClient *
934 client_get (struct GNUNET_SERVER_Client *client)
936 struct MeshClient *c;
941 if (c->handle == client)
950 * Checks if a given client has subscribed to certain message type
952 * @param message_type Type of message to check
953 * @param c Client to check
955 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
960 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
964 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
965 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
970 * Search for a tunnel among the tunnels for a client
972 * @param c the client whose tunnels to search in
973 * @param tid the local id of the tunnel
975 * @return tunnel handler, NULL if doesn't exist
977 static struct MeshTunnel *
978 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
980 GNUNET_HashCode hash;
982 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
983 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
988 * Search for a tunnel by global ID using PEER_ID
990 * @param pi owner of the tunnel
991 * @param tid global tunnel number
993 * @return tunnel handler, NULL if doesn't exist
995 static struct MeshTunnel *
996 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
998 struct MESH_TunnelID id;
999 GNUNET_HashCode hash;
1004 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1005 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1010 * Search for a tunnel by global ID using full PeerIdentities
1012 * @param oid owner of the tunnel
1013 * @param tid global tunnel number
1015 * @return tunnel handler, NULL if doesn't exist
1017 static struct MeshTunnel *
1018 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1020 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1025 * Callback used to notify a client owner of a tunnel that a peer has
1026 * disconnected, most likely because of a path change.
1028 * @param n Node in the tree representing the disconnected peer
1031 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1033 struct GNUNET_MESH_PeerControl msg;
1035 if (NULL == n->t->client || NULL == nc)
1038 msg.header.size = htons (sizeof (msg));
1039 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1040 msg.tunnel_id = htonl (n->t->local_tid);
1041 GNUNET_PEER_resolve (n->peer, &msg.peer);
1042 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1043 &msg.header, GNUNET_NO);
1048 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1049 * needed rescources.
1051 * @param t Tunnel we want to add a new peer to
1052 * @param peer PeerInfo of the peer being added
1056 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1058 struct MeshPeerPath *p;
1059 struct MeshPeerPath *best_p;
1060 unsigned int best_cost;
1063 if (NULL != tree_find_peer(t->tree->root, peer->id))
1065 /* Already have it, nothing to do. */
1070 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1071 if (NULL == (p = peer->path_head))
1078 best_cost = UINT_MAX;
1081 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1088 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1089 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1090 t->path_refresh_task =
1091 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1097 * Notify a tunnel that a connection has broken that affects at least
1098 * some of its peers.
1100 * @param t Tunnel affected.
1101 * @param peer Peer that (at least) has been affected by the disconnection.
1102 * @param p1 Peer that got disconnected from p2.
1103 * @param p2 Peer that got disconnected from p1.
1105 * @return Short ID of the peer disconnected (either p1 or p2).
1106 * 0 if the tunnel remained unaffected.
1108 static GNUNET_PEER_Id
1109 tunnel_notify_connection_broken (struct MeshTunnel *t,
1110 struct MeshPeerInfo *peer,
1114 return tree_notify_connection_broken (t->tree, p1, p2,
1115 ¬ify_peer_disconnected);
1120 * Destroy the tunnel and free any allocated resources linked to it
1122 * @param t the tunnel to destroy
1124 * @return GNUNET_OK on success
1127 tunnel_destroy (struct MeshTunnel *t)
1129 struct MeshClient *c;
1130 struct MeshQueue *q;
1131 struct MeshQueue *qn;
1132 GNUNET_HashCode hash;
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1144 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1145 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1150 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1151 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1155 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1159 if (NULL != q->data)
1160 GNUNET_free (q->data);
1164 /* TODO cancel core transmit ready in case it was active */
1166 tree_destroy(t->tree);
1173 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1174 * client when the client disconnects.
1176 * @param cls closure (client that is disconnecting)
1177 * @param key the hash of the local tunnel id (used to access the hashmap)
1178 * @param value the value stored at the key (tunnel to destroy)
1180 * @return GNUNET_OK on success
1183 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1187 r = tunnel_destroy ((struct MeshTunnel *) value);
1192 /******************************************************************************/
1193 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1194 /******************************************************************************/
1198 * Function called to notify a client about the socket
1199 * being ready to queue more data. "buf" will be
1200 * NULL and "size" zero if the socket was closed for
1201 * writing in the meantime.
1203 * @param cls closure
1204 * @param size number of bytes available in buf
1205 * @param buf where the callee should write the message
1206 * @return number of bytes written to buf
1209 send_core_create_path (void *cls, size_t size, void *buf)
1211 struct MeshPathInfo *info = cls;
1212 struct GNUNET_MESH_ManipulatePath *msg;
1213 struct GNUNET_PeerIdentity *peer_ptr;
1214 struct MeshPeerInfo *peer = info->peer;
1215 struct MeshTunnel *t = info->t;
1216 struct MeshPeerPath *p = info->path;
1221 sizeof (struct GNUNET_MESH_ManipulatePath) +
1222 p->length * sizeof (struct GNUNET_PeerIdentity);
1224 if (size < size_needed || NULL == buf)
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
1228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
1230 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1231 GNUNET_TIME_UNIT_FOREVER_REL,
1232 path_get_first_hop (t->tree, peer->id),
1234 &send_core_create_path,
1239 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1240 msg->header.size = htons (size_needed);
1241 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1242 msg->tid = ntohl (t->id.tid);
1244 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1245 for (i = 0; i < p->length; i++)
1247 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 "MESH: CREATE PATH (%u bytes long) sent!\n",
1262 * Function called to notify a client about the socket
1263 * being ready to queue more data. "buf" will be
1264 * NULL and "size" zero if the socket was closed for
1265 * writing in the meantime.
1267 * @param cls closure (MeshDataDescriptor with all info to build packet)
1268 * @param size number of bytes available in buf
1269 * @param buf where the callee should write the message
1270 * @return number of bytes written to buf
1273 send_core_data_to_origin (void *cls, size_t size, void *buf)
1275 struct MeshDataDescriptor *info = cls;
1276 struct GNUNET_MESH_ToOrigin *msg = buf;
1279 GNUNET_assert (NULL != info);
1280 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1281 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1283 if (total_size > size)
1285 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1286 "not enough buffer to send data to origin\n");
1289 msg->header.size = htons (total_size);
1290 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1291 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1292 msg->tid = htonl (info->origin->tid);
1293 if (0 != info->size)
1295 memcpy (&msg[1], &info[1], info->size);
1297 if (NULL != info->client)
1299 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1308 * Function called to notify a client about the socket
1309 * being ready to queue more data. "buf" will be
1310 * NULL and "size" zero if the socket was closed for
1311 * writing in the meantime.
1313 * @param cls closure (data itself)
1314 * @param size number of bytes available in buf
1315 * @param buf where the callee should write the message
1317 * @return number of bytes written to buf
1320 send_core_data_multicast (void *cls, size_t size, void *buf)
1322 struct MeshDataDescriptor *info = cls;
1323 struct GNUNET_MESH_Multicast *msg = buf;
1326 GNUNET_assert (NULL != info);
1327 GNUNET_assert (NULL != info->peer);
1328 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1329 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1331 if (total_size > size)
1334 struct GNUNET_PeerIdentity id;
1336 GNUNET_PEER_resolve(info->peer->id, &id);
1337 info->peer->infos[info->handler_n] = info;
1338 info->peer->types[info->handler_n] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1339 info->peer->core_transmit[info->handler_n] =
1340 GNUNET_CORE_notify_transmit_ready (core_handle,
1343 GNUNET_TIME_UNIT_FOREVER_REL,
1346 &send_core_data_multicast,
1350 info->peer->core_transmit[info->handler_n] = NULL;
1351 info->peer->infos[info->handler_n] = NULL;
1352 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1353 msg->header.size = htons (total_size);
1354 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1355 msg->tid = htonl (info->origin->tid);
1356 memcpy (&msg[1], info->data, info->size);
1357 if (0 == --(*info->copies))
1359 if (NULL != info->client)
1361 /* FIXME One unresponsive neighbor (who doesn't "call" tmt_rdy) can lock
1362 * the client from sending anything else to the service.
1363 * - Call receive_done after certain timeout.
1364 * - Here cancel the timeout.
1366 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1368 GNUNET_free (info->data);
1369 GNUNET_free (info->copies);
1377 * Function called to notify a client about the socket
1378 * being ready to queue more data. "buf" will be
1379 * NULL and "size" zero if the socket was closed for
1380 * writing in the meantime.
1382 * @param cls closure (MeshDataDescriptor)
1383 * @param size number of bytes available in buf
1384 * @param buf where the callee should write the message
1385 * @return number of bytes written to buf
1388 send_core_path_ack (void *cls, size_t size, void *buf)
1390 struct MeshDataDescriptor *info = cls;
1391 struct GNUNET_MESH_PathACK *msg = buf;
1393 GNUNET_assert (NULL != info);
1396 info->peer->core_transmit[info->handler_n] = NULL;
1398 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1403 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1404 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1405 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1406 msg->tid = htonl (info->origin->tid);
1407 msg->peer_id = my_full_id;
1409 /* TODO add signature */
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
1412 return sizeof (struct GNUNET_MESH_PathACK);
1417 * Function called to notify a client about the socket
1418 * being ready to queue more data. "buf" will be
1419 * NULL and "size" zero if the socket was closed for
1420 * writing in the meantime.
1422 * @param cls closure (data itself)
1423 * @param size number of bytes available in buf
1424 * @param buf where the callee should write the message
1425 * @return number of bytes written to buf
1428 send_core_data_raw (void *cls, size_t size, void *buf)
1430 struct GNUNET_MessageHeader *msg = cls;
1433 GNUNET_assert (NULL != msg);
1434 total_size = ntohs (msg->size);
1436 if (total_size > size)
1441 memcpy (buf, msg, total_size);
1449 * Send another peer a notification to destroy a tunnel
1450 * @param cls The tunnel to destroy
1451 * @param size Size in the buffer
1452 * @param buf Memory where to put the data to transmit
1453 * @return Size of data put in buffer
1456 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1458 struct MeshTunnel *t = cls;
1459 struct MeshClient *c;
1460 struct GNUNET_MESH_TunnelMessage *msg;
1464 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1465 /*FIXME*/ msg->header.size =
1466 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1467 msg->tunnel_id = htonl (t->id.tid);
1469 tunnel_destroy (c, t);
1470 return sizeof (struct GNUNET_MESH_TunnelMessage);
1476 * Send the message to all clients that have subscribed to its type
1478 * @param msg Pointer to the message itself
1479 * @return number of clients this message was sent to
1482 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1484 struct MeshClient *c;
1488 type = ntohs (msg->type);
1489 for (count = 0, c = clients; c != NULL; c = c->next)
1491 if (client_is_subscribed (type, c))
1494 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1504 * Notify the client that owns the tunnel that a peer has connected to it
1506 * @param t Tunnel whose owner to notify
1507 * @param id Short id of the peer that has connected
1510 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1512 struct GNUNET_MESH_PeerControl pc;
1514 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1515 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1516 pc.tunnel_id = htonl (t->local_tid);
1517 GNUNET_PEER_resolve (id, &pc.peer);
1518 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1519 &pc.header, GNUNET_NO);
1523 /******************************************************************************/
1524 /******************** MESH NETWORK HANDLERS **************************/
1525 /******************************************************************************/
1529 * Core handler for path creation
1530 * struct GNUNET_CORE_MessageHandler
1532 * @param cls closure
1533 * @param message message
1534 * @param peer peer identity this notification is about
1535 * @param atsi performance data
1536 * @return GNUNET_OK to keep the connection open,
1537 * GNUNET_SYSERR to close it (signal serious error)
1541 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1542 const struct GNUNET_MessageHeader *message,
1543 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1545 unsigned int own_pos;
1548 MESH_TunnelNumber tid;
1549 struct GNUNET_MESH_ManipulatePath *msg;
1550 struct GNUNET_PeerIdentity *pi;
1551 struct GNUNET_PeerIdentity id;
1552 GNUNET_HashCode hash;
1553 struct MeshPeerPath *path;
1554 struct MeshPeerInfo *dest_peer_info;
1555 struct MeshPeerInfo *orig_peer_info;
1556 struct MeshTunnel *t;
1558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1559 "MESH: Received a path create msg\n");
1560 size = ntohs (message->size);
1561 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1563 GNUNET_break_op (0);
1567 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1568 if (size % sizeof (struct GNUNET_PeerIdentity))
1570 GNUNET_break_op (0);
1573 size /= sizeof (struct GNUNET_PeerIdentity);
1576 GNUNET_break_op (0);
1579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1580 "MESH: path as %u hops.\n",
1582 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1584 tid = ntohl (msg->tid);
1585 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1586 t = tunnel_get (pi, tid);
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1588 "MESH: path is for tunnel %s [%X].\n",
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1594 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1595 t->id.oid = GNUNET_PEER_intern (pi);
1597 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1598 t->local_tid = next_local_tid++;
1599 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
1601 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1603 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1604 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1611 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1612 if (NULL == dest_peer_info)
1614 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1615 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1616 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1618 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1620 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1621 if (NULL == orig_peer_info)
1623 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1624 orig_peer_info->id = GNUNET_PEER_intern (pi);
1625 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1626 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1629 path = path_new (size);
1631 for (i = 0; i < size; i++)
1633 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1634 if (path->peers[i] == myid)
1638 { /* cannot be self, must be 'not found' */
1639 /* create path: self not found in path through self */
1640 GNUNET_break_op (0);
1641 path_destroy (path);
1642 /* FIXME error. destroy tunnel? leave for timeout? */
1645 if (own_pos == size - 1)
1647 /* It is for us! Send ack. */
1648 struct GNUNET_MESH_TunnelNotification cmsg;
1649 struct MeshDataDescriptor *info;
1652 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1653 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1654 info->origin = &t->id;
1655 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1656 GNUNET_assert (NULL != info->peer);
1657 for (j = 0; info->peer->core_transmit[j]; j++)
1659 if (j == (CORE_QUEUE_SIZE - 1))
1665 info->handler_n = j;
1666 info->peer->core_transmit[j] =
1667 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1668 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1669 sizeof (struct GNUNET_MESH_PathACK),
1670 &send_core_path_ack, info);
1671 cmsg.header.size = htons(sizeof(cmsg));
1672 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1673 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
1674 cmsg.tunnel_id = htonl(t->local_tid);
1675 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
1679 /* It's for somebody else! Retransmit. */
1680 struct MeshPathInfo *path_info;
1682 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1684 path_info->path = path;
1685 path_info->peer = dest_peer_info;
1687 path_add_to_peer (dest_peer_info, path);
1688 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1689 GNUNET_CORE_notify_transmit_ready (
1693 GNUNET_TIME_UNIT_FOREVER_REL,
1695 sizeof (struct GNUNET_MESH_ManipulatePath) +
1696 path->length * sizeof(struct GNUNET_PeerIdentity),
1697 &send_core_create_path,
1705 * Core handler for mesh network traffic going from the origin to a peer
1707 * @param cls closure
1708 * @param peer peer identity this notification is about
1709 * @param message message
1710 * @param atsi performance data
1711 * @return GNUNET_OK to keep the connection open,
1712 * GNUNET_SYSERR to close it (signal serious error)
1715 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1716 const struct GNUNET_MessageHeader *message,
1717 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1719 struct GNUNET_MESH_Unicast *msg;
1720 struct MeshTunnel *t;
1721 struct MeshPeerInfo *pi;
1724 size = ntohs (message->size);
1726 sizeof (struct GNUNET_MESH_Unicast) +
1727 sizeof (struct GNUNET_MessageHeader))
1732 msg = (struct GNUNET_MESH_Unicast *) message;
1733 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1736 /* TODO notify back: we don't know this tunnel */
1737 GNUNET_break_op (0);
1740 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1741 &msg->destination.hashPubKey);
1744 /* TODO maybe feedback, log to statistics */
1745 GNUNET_break_op (0);
1750 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1753 msg = GNUNET_malloc (size);
1754 memcpy (msg, message, size);
1755 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1756 GNUNET_TIME_UNIT_FOREVER_REL,
1757 path_get_first_hop (t->tree, pi->id),
1759 &send_core_data_raw, msg);
1765 * Core handler for mesh network traffic going from the origin to all peers
1767 * @param cls closure
1768 * @param message message
1769 * @param peer peer identity this notification is about
1770 * @param atsi performance data
1771 * @return GNUNET_OK to keep the connection open,
1772 * GNUNET_SYSERR to close it (signal serious error)
1774 * TODO: Check who we got this from, to validate route.
1777 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1778 const struct GNUNET_MessageHeader *message,
1779 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1781 struct GNUNET_MESH_Multicast *msg;
1782 struct GNUNET_PeerIdentity *id;
1783 struct MeshDataDescriptor *info;
1784 struct MeshTunnelTreeNode *n;
1785 struct MeshTunnel *t;
1786 unsigned int *copies;
1791 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
1792 if (size < sizeof (struct GNUNET_MessageHeader))
1794 GNUNET_break_op (0);
1797 msg = (struct GNUNET_MESH_Multicast *) message;
1798 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1802 /* TODO notify that we dont know that tunnel */
1803 GNUNET_break_op (0);
1807 /* Transmit to locally interested clients */
1808 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1810 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1812 n = t->tree->me->children_head;
1815 copies = GNUNET_malloc (sizeof (unsigned int));
1816 for (*copies = 0; NULL != n; n = n->next)
1818 n = t->tree->me->children_head;
1819 data = GNUNET_malloc (size);
1820 memcpy (data, &msg[1], size);
1823 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1824 info->origin = &t->id;
1827 info->copies = copies;
1828 info->client = t->client->handle;
1829 info->destination = n->peer;
1830 id = path_get_first_hop(t->tree, n->peer);
1831 info->peer = peer_info_get(id);
1832 GNUNET_assert (NULL != info->peer);
1833 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
1835 if (i == (CORE_QUEUE_SIZE - 1))
1841 info->handler_n = i;
1842 info->peer->infos[i] = info;
1843 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1844 info->peer->core_transmit[i] =
1845 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1846 GNUNET_TIME_UNIT_FOREVER_REL, id,
1847 ntohs (msg->header.size),
1848 &send_core_data_multicast, info);
1856 * Core handler for mesh network traffic
1858 * @param cls closure
1859 * @param message message
1860 * @param peer peer identity this notification is about
1861 * @param atsi performance data
1863 * @return GNUNET_OK to keep the connection open,
1864 * GNUNET_SYSERR to close it (signal serious error)
1867 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1868 const struct GNUNET_MessageHeader *message,
1869 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1871 struct GNUNET_MESH_ToOrigin *msg;
1872 struct GNUNET_PeerIdentity id;
1873 struct MeshPeerInfo *peer_info;
1874 struct MeshTunnel *t;
1877 size = ntohs (message->size);
1878 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1879 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1881 GNUNET_break_op (0);
1884 msg = (struct GNUNET_MESH_ToOrigin *) message;
1885 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1889 /* TODO notify that we dont know this tunnel (whom)? */
1893 if (t->id.oid == myid)
1895 if (NULL == t->client)
1897 /* got data packet for ownerless tunnel */
1898 GNUNET_break_op (0);
1901 /* TODO signature verification */
1902 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1906 peer_info = peer_info_get (&msg->oid);
1907 if (NULL == peer_info)
1909 /* unknown origin of tunnel */
1913 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1914 msg = GNUNET_malloc (size);
1915 memcpy (msg, message, size);
1916 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1917 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1918 &send_core_data_raw, msg);
1925 * Core handler for path ACKs
1927 * @param cls closure
1928 * @param message message
1929 * @param peer peer identity this notification is about
1930 * @param atsi performance data
1932 * @return GNUNET_OK to keep the connection open,
1933 * GNUNET_SYSERR to close it (signal serious error)
1936 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1937 const struct GNUNET_MessageHeader *message,
1938 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1940 struct GNUNET_MESH_PathACK *msg;
1941 struct MeshTunnelTreeNode *n;
1942 struct MeshPeerInfo *peer_info;
1943 struct MeshTunnel *t;
1945 msg = (struct GNUNET_MESH_PathACK *) message;
1946 t = tunnel_get (&msg->oid, msg->tid);
1949 /* TODO notify that we don't know the tunnel */
1953 /* Message for us? */
1954 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1956 if (NULL == t->client)
1958 GNUNET_break_op (0);
1961 peer_info = peer_info_get (&msg->peer_id);
1962 if (NULL == peer_info)
1964 GNUNET_break_op (0);
1967 n = tree_find_peer(t->tree->root, peer_info->id);
1970 GNUNET_break_op (0);
1973 n->status = MESH_PEER_READY;
1974 send_client_peer_connected(t, peer_info->id);
1978 peer_info = peer_info_get (&msg->oid);
1979 if (NULL == peer_info)
1981 /* If we know the tunnel, we should DEFINITELY know the peer */
1985 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1986 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1987 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1988 GNUNET_TIME_UNIT_FOREVER_REL,
1989 path_get_first_hop (t->tree,
1991 sizeof (struct GNUNET_MESH_PathACK),
1992 &send_core_data_raw, msg);
1998 * Functions to handle messages from core
2000 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2001 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2002 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2003 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2004 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2005 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2006 sizeof (struct GNUNET_MESH_PathACK)},
2012 /******************************************************************************/
2013 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2014 /******************************************************************************/
2017 * deregister_app: iterator for removing each application registered by a client
2019 * @param cls closure
2020 * @param key the hash of the application id (used to access the hashmap)
2021 * @param value the value stored at the key (client)
2023 * @return GNUNET_OK on success
2026 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2028 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2034 * notify_client_connection_failure: notify a client that the connection to the
2035 * requested remote peer is not possible (for instance, no route found)
2036 * Function called when the socket is ready to queue more data. "buf" will be
2037 * NULL and "size" zero if the socket was closed for writing in the meantime.
2039 * @param cls closure
2040 * @param size number of bytes available in buf
2041 * @param buf where the callee should write the message
2042 * @return number of bytes written to buf
2045 notify_client_connection_failure (void *cls, size_t size, void *buf)
2048 struct MeshPeerInfo *peer_info;
2049 struct GNUNET_MESH_PeerControl *msg;
2050 struct GNUNET_PeerIdentity id;
2052 if (0 == size && NULL == buf)
2054 // TODO retry? cancel?
2058 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2059 peer_info = (struct MeshPeerInfo *) cls;
2060 msg = (struct GNUNET_MESH_PeerControl *) buf;
2061 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2062 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2063 // msg->tunnel_id = htonl(peer_info->t->tid);
2064 GNUNET_PEER_resolve (peer_info->id, &id);
2065 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2073 * Send keepalive packets for a peer
2075 * @param cls Closure (tunnel for which to send the keepalive).
2076 * @param tc Notification context.
2078 * TODO: implement explicit multicast keepalive?
2081 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2083 struct MeshTunnel *t = cls;
2084 struct GNUNET_MessageHeader *payload;
2085 struct GNUNET_MESH_Multicast *msg;
2088 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2089 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2095 "MESH: sending keepalive for tunnel %d\n",
2098 size = sizeof(struct GNUNET_MESH_Multicast) +
2099 sizeof(struct GNUNET_MessageHeader);
2100 msg = GNUNET_malloc (size);
2101 msg->header.size = htons (size);
2102 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2103 msg->oid = my_full_id;
2104 msg->tid = htonl(t->id.tid);
2105 payload = (struct GNUNET_MessageHeader *) &msg[1];
2106 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2107 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2108 handle_mesh_data_multicast (NULL, &my_full_id, &msg->header, NULL);
2111 t->path_refresh_task =
2112 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2118 * Function to process paths received for a new peer addition. The recorded
2119 * paths form the initial tunnel, which can be optimized later.
2120 * Called on each result obtained for the DHT search.
2122 * @param cls closure
2123 * @param exp when will this value expire
2124 * @param key key of the result
2125 * @param type type of the result
2126 * @param size number of bytes in data
2127 * @param data pointer to the result data
2129 * TODO: re-issue the request after certain time? cancel after X results?
2132 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2133 const GNUNET_HashCode * key,
2134 const struct GNUNET_PeerIdentity *get_path,
2135 unsigned int get_path_length,
2136 const struct GNUNET_PeerIdentity *put_path,
2137 unsigned int put_path_length,
2138 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2140 struct MeshPathInfo *path_info = cls;
2141 struct MeshPathInfo *path_info_aux;
2142 struct MeshPeerPath *p;
2143 struct MeshPeerPath *aux;
2144 struct GNUNET_PeerIdentity pi;
2147 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2148 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2149 "MESH: Got results from DHT for %s\n",
2150 GNUNET_h2s_full(&pi.hashPubKey));
2151 if (NULL == get_path || NULL == put_path)
2153 if (NULL == path_info->peer->path_head)
2155 // Find ourselves some alternate initial path to the destination: retry
2156 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2157 path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2158 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2159 GNUNET_BLOCK_TYPE_TEST, /* type */
2160 &pi.hashPubKey, /*key to search */
2161 4, /* replication level */
2162 GNUNET_DHT_RO_RECORD_ROUTE,
2164 0, /* xquery bits */
2165 &dht_get_id_handler,
2166 (void *) path_info);
2171 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2172 path_add_to_peer (path_info->peer, p);
2173 for (i = 0; i < path_info->peer->ntunnels; i++)
2175 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2176 aux = tree_get_path_to_peer(path_info->peer->tunnels[i]->tree,
2177 path_info->peer->id);
2178 if (aux->length > 1)
2180 struct GNUNET_PeerIdentity id;
2182 path_info_aux = GNUNET_malloc (sizeof (struct MeshPathInfo));
2183 path_info_aux->path = aux;
2184 path_info_aux->peer = path_info->peer;
2185 path_info_aux->t = path_info->t;
2186 GNUNET_PEER_resolve (p->peers[1], &id);
2187 GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
2190 GNUNET_TIME_UNIT_FOREVER_REL,
2193 sizeof (struct GNUNET_MESH_ManipulatePath)
2196 sizeof (struct GNUNET_PeerIdentity)),
2198 &send_core_create_path,
2200 path_info_aux); /* cls */
2204 send_client_peer_connected(path_info->t, myid);
2207 GNUNET_free (path_info);
2214 * Function to process paths received for a new peer addition. The recorded
2215 * paths form the initial tunnel, which can be optimized later.
2216 * Called on each result obtained for the DHT search.
2218 * @param cls closure
2219 * @param exp when will this value expire
2220 * @param key key of the result
2221 * @param type type of the result
2222 * @param size number of bytes in data
2223 * @param data pointer to the result data
2226 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2227 const GNUNET_HashCode * key,
2228 const struct GNUNET_PeerIdentity *get_path,
2229 unsigned int get_path_length,
2230 const struct GNUNET_PeerIdentity *put_path,
2231 unsigned int put_path_length,
2232 enum GNUNET_BLOCK_Type type, size_t size,
2235 const struct GNUNET_PeerIdentity *pi = data;
2236 struct GNUNET_PeerIdentity id;
2237 struct MeshTunnel *t = cls;
2238 struct MeshPeerInfo *peer_info;
2239 struct MeshPathInfo *path_info;
2240 struct MeshPeerPath *p;
2243 if (size != sizeof (struct GNUNET_PeerIdentity))
2245 GNUNET_break_op (0);
2248 GNUNET_assert (NULL != t->client);
2249 GNUNET_DHT_get_stop (t->client->dht_get_type);
2250 t->client->dht_get_type = NULL;
2251 peer_info = peer_info_get (pi);
2252 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2253 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2255 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2256 NULL == peer_info->dhtget)
2258 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2259 path_info->peer = peer_info;
2261 /* we don't have a route to the peer, let's try a direct lookup */
2262 peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2264 GNUNET_TIME_UNIT_FOREVER_REL,
2266 GNUNET_BLOCK_TYPE_TEST,
2269 /* key to look up */
2271 /* replication level */
2272 GNUNET_DHT_RO_RECORD_ROUTE,
2273 /* option to dht: record route */
2275 0, /* xquery bits */
2278 path_info); /* closure */
2282 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2283 path_add_to_peer (peer_info, p);
2284 tunnel_add_peer(t, peer_info);
2285 p = tree_get_path_to_peer(t->tree, peer_info->id);
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2288 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2289 t->local_tid, p->length);
2290 for (i = 0; i < p->length; i++)
2292 GNUNET_PEER_resolve (p->peers[0], &id);
2293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2294 GNUNET_h2s_full (&id.hashPubKey));
2300 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2302 path_info->peer = peer_info;
2303 path_info->path = p;
2304 GNUNET_PEER_resolve (p->peers[1], &id);
2305 GNUNET_CORE_notify_transmit_ready (core_handle,
2311 GNUNET_TIME_UNIT_FOREVER_REL,
2315 sizeof (struct GNUNET_MESH_ManipulatePath)
2318 sizeof (struct GNUNET_PeerIdentity)),
2320 &send_core_create_path,
2322 path_info); /* cls */
2326 send_client_peer_connected(t, myid);
2330 /******************************************************************************/
2331 /********************* MESH LOCAL HANDLES **************************/
2332 /******************************************************************************/
2336 * Handler for client disconnection
2338 * @param cls closure
2339 * @param client identification of the client; NULL
2340 * for the last call when the server is destroyed
2343 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2345 struct MeshClient *c;
2346 struct MeshClient *next;
2348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2354 if (c->handle != client && NULL != client)
2356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2361 if (NULL != c->tunnels)
2363 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2364 &tunnel_destroy_iterator,
2366 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2369 /* deregister clients applications */
2370 if (NULL != c->apps)
2372 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2373 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2375 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2376 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2378 GNUNET_SCHEDULER_cancel (announce_applications_task);
2379 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2381 if (NULL != c->types)
2382 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2383 if (NULL != c->dht_get_type)
2384 GNUNET_DHT_get_stop (c->dht_get_type);
2385 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2397 * Handler for new clients
2399 * @param cls closure
2400 * @param client identification of the client
2401 * @param message the actual message, which includes messages the client wants
2404 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2405 const struct GNUNET_MessageHeader *message)
2407 struct GNUNET_MESH_ClientConnect *cc_msg;
2408 struct MeshClient *c;
2409 GNUNET_MESH_ApplicationType *a;
2416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2417 /* Check data sanity */
2418 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2419 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2420 ntypes = ntohs (cc_msg->types);
2421 napps = ntohs (cc_msg->applications);
2423 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2426 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2430 /* Create new client structure */
2431 c = GNUNET_malloc (sizeof (struct MeshClient));
2433 c->id = next_client_id++;
2436 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2439 GNUNET_MESH_ApplicationType at;
2442 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2443 for (i = 0; i < napps; i++)
2446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2447 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2448 /* store in clients hashmap */
2449 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2450 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2451 /* store in global hashmap, for announcements */
2452 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2453 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2455 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2456 announce_applications_task =
2457 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2465 t = (uint16_t *) & a[napps];
2466 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2467 for (i = 0; i < ntypes; i++)
2470 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2472 /* store in clients hashmap */
2473 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2474 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2475 /* store in global hashmap */
2476 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2477 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2481 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2483 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2484 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2485 GNUNET_SERVER_notification_context_add (nc, client);
2487 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2495 * Handler for requests of new tunnels
2497 * @param cls closure
2498 * @param client identification of the client
2499 * @param message the actual message
2502 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2503 const struct GNUNET_MessageHeader *message)
2505 struct GNUNET_MESH_TunnelMessage *t_msg;
2506 struct MeshTunnel *t;
2507 struct MeshClient *c;
2508 GNUNET_HashCode hash;
2510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2512 /* Sanity check for client registration */
2513 if (NULL == (c = client_get (client)))
2516 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2523 /* Message sanity check */
2524 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2527 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2531 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2532 /* Sanity check for tunnel numbering */
2533 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2536 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2539 /* Sanity check for duplicate tunnel IDs */
2540 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2543 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2547 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2549 while (NULL != tunnel_get_by_pi (myid, next_tid))
2550 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2551 t->id.tid = next_tid++;
2553 t->local_tid = ntohl (t_msg->tunnel_id);
2555 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2557 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2559 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2560 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2563 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2567 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2569 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2570 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2573 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2576 t->tree = tree_new (t, myid);
2577 t->tree->refresh = REFRESH_PATH_TIME;
2578 t->tree->root->status = MESH_PEER_READY;
2579 t->tree->me = t->tree->root;
2581 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2587 * Handler for requests of deleting tunnels
2589 * @param cls closure
2590 * @param client identification of the client
2591 * @param message the actual message
2594 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2595 const struct GNUNET_MessageHeader *message)
2597 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2598 struct MeshClient *c;
2599 struct MeshTunnel *t;
2600 MESH_TunnelNumber tid;
2601 GNUNET_HashCode hash;
2603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2605 /* Sanity check for client registration */
2606 if (NULL == (c = client_get (client)))
2609 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2612 /* Message sanity check */
2613 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2616 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2622 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2624 /* Retrieve tunnel */
2625 tid = ntohl (tunnel_msg->tunnel_id);
2627 /* Remove from local id hashmap */
2628 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2629 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2630 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2632 /* Remove from global id hashmap */
2633 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2634 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2636 // notify_tunnel_destroy(t); FIXME
2638 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2644 * Handler for connection requests to new peers
2646 * @param cls closure
2647 * @param client identification of the client
2648 * @param message the actual message (PeerControl)
2651 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2652 const struct GNUNET_MessageHeader *message)
2654 struct GNUNET_MESH_PeerControl *peer_msg;
2655 struct MeshPathInfo *path_info;
2656 struct MeshPeerInfo *peer_info;
2657 struct MeshClient *c;
2658 struct MeshTunnel *t;
2659 MESH_TunnelNumber tid;
2661 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
2662 /* Sanity check for client registration */
2663 if (NULL == (c = client_get (client)))
2666 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2670 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2671 /* Sanity check for message size */
2672 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2675 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2679 /* Tunnel exists? */
2680 tid = ntohl (peer_msg->tunnel_id);
2681 t = tunnel_get_by_local_id (c, tid);
2685 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2689 /* Does client own tunnel? */
2690 if (t->client->handle != client)
2693 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2696 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
2697 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
2698 peer_info = peer_info_get (&peer_msg->peer);
2700 /* Start DHT search if needed, otherwise just add peer to tunnel. */
2701 if (NULL == peer_info->dhtget && NULL == peer_info->path_head)
2703 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2704 path_info->peer = peer_info;
2706 peer_info->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
2707 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2708 GNUNET_BLOCK_TYPE_TEST, /* type */
2709 &peer_msg->peer.hashPubKey, /*key to search */
2710 4, /* replication level */
2711 GNUNET_DHT_RO_RECORD_ROUTE,
2713 0, /* xquery bits */
2714 &dht_get_id_handler,
2715 (void *) path_info);
2717 else if (NULL != peer_info->path_head)
2720 for (i = 0; i < CORE_QUEUE_SIZE; i++)
2722 if (NULL == peer_info->core_transmit[i])
2725 if (CORE_QUEUE_SIZE == i)
2728 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2731 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2732 path_info->peer = peer_info;
2734 tunnel_add_peer(t, peer_info);
2735 path_info->path = tree_get_path_to_peer(t->tree, peer_info->id);
2736 peer_info = peer_info_get(path_get_first_hop(t->tree, path_info->peer->id));
2737 peer_info->infos[i] = path_info;
2738 peer_info->types[i] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
2739 peer_info->core_transmit[i] =
2740 GNUNET_CORE_notify_transmit_ready (
2744 GNUNET_TIME_UNIT_FOREVER_REL,
2745 path_get_first_hop(t->tree, path_info->peer->id),
2746 sizeof (struct GNUNET_MESH_ManipulatePath)
2747 + path_info->path->length * sizeof(struct GNUNET_PeerIdentity),
2748 &send_core_create_path,
2751 /* Otherwise: there is no path yet, but there is a DHT_get active already. */
2752 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2758 * Handler for disconnection requests of peers in a tunnel
2760 * @param cls closure
2761 * @param client identification of the client
2762 * @param message the actual message (PeerControl)
2765 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2766 const struct GNUNET_MessageHeader *message)
2768 struct GNUNET_MESH_PeerControl *peer_msg;
2769 struct MeshClient *c;
2770 struct MeshTunnel *t;
2771 MESH_TunnelNumber tid;
2773 /* Sanity check for client registration */
2774 if (NULL == (c = client_get (client)))
2777 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2780 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2781 /* Sanity check for message size */
2782 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2789 /* Tunnel exists? */
2790 tid = ntohl (peer_msg->tunnel_id);
2791 t = tunnel_get_by_local_id (c, tid);
2795 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2799 /* Does client own tunnel? */
2800 if (t->client->handle != client)
2803 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2807 /* Ok, delete peer from tunnel */
2808 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2809 &peer_msg->peer.hashPubKey);
2811 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2817 * Handler for connection requests to new peers by type
2819 * @param cls closure
2820 * @param client identification of the client
2821 * @param message the actual message (ConnectPeerByType)
2824 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2825 const struct GNUNET_MessageHeader *message)
2827 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2828 struct MeshClient *c;
2829 struct MeshTunnel *t;
2830 GNUNET_HashCode hash;
2831 GNUNET_MESH_ApplicationType type;
2832 MESH_TunnelNumber tid;
2834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2835 /* Sanity check for client registration */
2836 if (NULL == (c = client_get (client)))
2839 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2843 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2844 /* Sanity check for message size */
2845 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2846 ntohs (connect_msg->header.size))
2849 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2853 /* Tunnel exists? */
2854 tid = ntohl (connect_msg->tunnel_id);
2855 t = tunnel_get_by_local_id (c, tid);
2859 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2863 /* Does client own tunnel? */
2864 if (t->client->handle != client)
2867 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2871 /* Do WE have the service? */
2872 type = ntohl (connect_msg->type);
2873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2874 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2875 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2878 /* Yes! Fast forward, add ourselves to the tunnel and send the
2879 * good news to the client
2881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2882 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2883 peer_info_get (&my_full_id),
2884 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2887 send_client_peer_connected(t, myid);
2888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2889 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2892 /* Ok, lets find a peer offering the service */
2893 if (c->dht_get_type)
2895 GNUNET_DHT_get_stop (c->dht_get_type);
2897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2898 GNUNET_h2s_full (&hash));
2900 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2901 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2902 GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0,
2903 &dht_get_type_handler, t);
2905 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2911 * Handler for client traffic directed to one peer
2913 * @param cls closure
2914 * @param client identification of the client
2915 * @param message the actual message
2918 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2919 const struct GNUNET_MessageHeader *message)
2921 struct MeshClient *c;
2922 struct MeshTunnel *t;
2923 struct MeshPeerInfo *pi;
2924 struct GNUNET_MESH_Unicast *data_msg;
2925 MESH_TunnelNumber tid;
2928 /* Sanity check for client registration */
2929 if (NULL == (c = client_get (client)))
2932 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2935 data_msg = (struct GNUNET_MESH_Unicast *) message;
2936 /* Sanity check for message size */
2937 size = ntohs (message->size);
2938 if (sizeof (struct GNUNET_MESH_Unicast) +
2939 sizeof (struct GNUNET_MessageHeader) > size)
2942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2946 /* Tunnel exists? */
2947 tid = ntohl (data_msg->tid);
2948 t = tunnel_get_by_local_id (c, tid);
2952 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2956 /* Is it a local tunnel? Then, does client own the tunnel? */
2957 if (t->client->handle != NULL && t->client->handle != client)
2960 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2964 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2965 &data_msg->destination.hashPubKey);
2966 /* Is the selected peer in the tunnel? */
2970 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2974 /* Ok, everything is correct, send the message
2975 * (pretend we got it from a mesh peer)
2978 char buf[ntohs (message->size)];
2979 struct GNUNET_MESH_Unicast *copy;
2981 /* Work around const limitation */
2982 copy = (struct GNUNET_MESH_Unicast *) buf;
2983 memcpy (buf, data_msg, size);
2984 copy->oid = my_full_id;
2985 copy->tid = htonl (t->id.tid);
2986 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
2988 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2993 * Handler for client traffic directed to all peers in a tunnel
2995 * @param cls closure
2996 * @param client identification of the client
2997 * @param message the actual message
3000 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3001 const struct GNUNET_MessageHeader *message)
3003 struct MeshClient *c;
3004 struct MeshTunnel *t;
3005 struct GNUNET_MESH_Multicast *data_msg;
3006 MESH_TunnelNumber tid;
3008 /* Sanity check for client registration */
3009 if (NULL == (c = client_get (client)))
3012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3015 data_msg = (struct GNUNET_MESH_Multicast *) message;
3016 /* Sanity check for message size */
3017 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
3020 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3024 /* Tunnel exists? */
3025 tid = ntohl (data_msg->tid);
3026 t = tunnel_get_by_local_id (c, tid);
3030 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3034 /* Does client own tunnel? */
3035 if (t->client->handle != client)
3038 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3043 char buf[ntohs(message->size)];
3044 struct GNUNET_MESH_Multicast *copy;
3046 copy = (struct GNUNET_MESH_Multicast *)buf;
3047 memcpy(buf, message, ntohs(message->size));
3048 copy->oid = my_full_id;
3049 copy->tid = htonl(t->id.tid);
3050 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3053 /* receive done gets called when last copy is sent */
3058 * Functions to handle messages from clients
3060 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3061 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3062 {&handle_local_tunnel_create, NULL,
3063 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3064 sizeof (struct GNUNET_MESH_TunnelMessage)},
3065 {&handle_local_tunnel_destroy, NULL,
3066 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3067 sizeof (struct GNUNET_MESH_TunnelMessage)},
3068 {&handle_local_connect_add, NULL,
3069 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3070 sizeof (struct GNUNET_MESH_PeerControl)},
3071 {&handle_local_connect_del, NULL,
3072 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3073 sizeof (struct GNUNET_MESH_PeerControl)},
3074 {&handle_local_connect_by_type, NULL,
3075 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3076 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3077 {&handle_local_unicast, NULL,
3078 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3079 {&handle_local_unicast, NULL,
3080 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3081 {&handle_local_multicast, NULL,
3082 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3088 * To be called on core init/fail.
3090 * @param cls service closure
3091 * @param server handle to the server for this service
3092 * @param identity the public identity of this peer
3093 * @param publicKey the public key of this peer
3096 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3097 const struct GNUNET_PeerIdentity *identity,
3098 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
3100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3101 core_handle = server;
3102 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3104 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3105 GNUNET_SCHEDULER_shutdown();
3111 * Method called whenever a given peer connects.
3113 * @param cls closure
3114 * @param peer peer identity this notification is about
3115 * @param atsi performance data for the connection
3118 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3119 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3121 struct MeshPeerInfo *peer_info;
3122 struct MeshPeerPath *path;
3124 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3125 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3126 // GNUNET_h2s(&my_full_id.hashPubKey));
3127 peer_info = peer_info_get (peer);
3128 if (myid == peer_info->id)
3130 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3135 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3136 // GNUNET_h2s(&peer->hashPubKey));
3138 path = path_new (2);
3139 path->peers[0] = myid;
3140 path->peers[1] = peer_info->id;
3141 GNUNET_PEER_change_rc(myid, 1);
3142 GNUNET_PEER_change_rc(peer_info->id, 1);
3143 path_add_to_peer (peer_info, path);
3148 * Method called whenever a peer disconnects.
3150 * @param cls closure
3151 * @param peer peer identity this notification is about
3154 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3156 struct MeshPeerInfo *pi;
3159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3160 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3166 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3168 if (pi->core_transmit[i])
3170 struct MeshDataDescriptor *dd;
3171 struct MeshPathInfo *path_info;
3172 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3173 /* TODO: notify that tranmission has failed */
3174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: data at %u\n", i);
3175 switch (pi->types[i])
3177 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3178 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3179 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
3182 if (0 == --(*dd->copies))
3184 GNUNET_free (dd->copies);
3185 GNUNET_free (dd->data);
3188 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
3190 path_info = pi->infos[i];
3191 path_destroy(path_info->path);
3194 GNUNET_free (pi->infos[i]);
3197 path_remove_from_peer (pi, pi->id, myid);
3200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3206 /******************************************************************************/
3207 /************************ MAIN FUNCTIONS ****************************/
3208 /******************************************************************************/
3211 * Task run during shutdown.
3217 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3219 struct MeshClient *c;
3221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3222 if (core_handle != NULL)
3224 GNUNET_CORE_disconnect (core_handle);
3227 if (dht_handle != NULL)
3229 for (c = clients; NULL != c; c = c->next)
3230 if (NULL != c->dht_get_type)
3231 GNUNET_DHT_get_stop (c->dht_get_type);
3232 GNUNET_DHT_disconnect (dht_handle);
3237 GNUNET_SERVER_notification_context_destroy (nc);
3240 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3242 GNUNET_SCHEDULER_cancel (announce_id_task);
3243 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3249 * Process mesh requests.
3251 * @param cls closure
3252 * @param server the initialized server
3253 * @param c configuration to use
3256 run (void *cls, struct GNUNET_SERVER_Handle *server,
3257 const struct GNUNET_CONFIGURATION_Handle *c)
3259 struct MeshPeerInfo *peer;
3260 struct MeshPeerPath *p;
3263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3264 server_handle = server;
3265 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3266 CORE_QUEUE_SIZE, /* queue size */
3267 NULL, /* Closure passed to MESH functions */
3268 &core_init, /* Call core_init once connected */
3269 &core_connect, /* Handle connects */
3270 &core_disconnect, /* remove peers on disconnects */
3271 NULL, /* Do we care about "status" updates? */
3272 NULL, /* Don't notify about all incoming messages */
3273 GNUNET_NO, /* For header only in notification */
3274 NULL, /* Don't notify about all outbound messages */
3275 GNUNET_NO, /* For header-only out notification */
3276 core_handlers); /* Register these handlers */
3277 if (core_handle == NULL)
3280 GNUNET_SCHEDULER_shutdown ();
3285 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3290 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3291 GNUNET_SCHEDULER_shutdown ();
3294 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3295 GNUNET_free (keyfile);
3296 if (my_private_key == NULL)
3298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3299 _("Mesh service could not access hostkey. Exiting.\n"));
3300 GNUNET_SCHEDULER_shutdown ();
3303 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3304 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3305 &my_full_id.hashPubKey);
3306 myid = GNUNET_PEER_intern (&my_full_id);
3308 dht_handle = GNUNET_DHT_connect (c, 64);
3309 if (dht_handle == NULL)
3315 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3317 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3318 peers = GNUNET_CONTAINER_multihashmap_create (32);
3319 applications = GNUNET_CONTAINER_multihashmap_create (32);
3320 types = GNUNET_CONTAINER_multihashmap_create (32);
3322 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3323 nc = GNUNET_SERVER_notification_context_create (server_handle,
3325 GNUNET_SERVER_disconnect_notify (server_handle,
3326 &handle_local_client_disconnect,
3331 clients_tail = NULL;
3336 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3337 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3339 /* Create a peer_info for the local peer */
3340 peer = peer_info_get(&my_full_id);
3343 path_add_to_peer(peer, p);
3345 /* Scheduled the task to clean up when shutdown is called */
3346 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3353 * The main function for the mesh service.
3355 * @param argc number of arguments from the command line
3356 * @param argv command line arguments
3357 * @return 0 ok, 1 on error
3360 main (int argc, char *const *argv)
3365 // fprintf (stderr, "main ()\n");
3367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3370 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3374 // fprintf (stderr, "main () END\n");