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 /******************************************************************************/
364 #if LATER /* FIXME DHT */
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);
384 /******************************************************************************/
385 /*********************** GLOBAL VARIABLES ****************************/
386 /******************************************************************************/
391 static struct MeshClient *clients;
392 static struct MeshClient *clients_tail;
395 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
397 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
400 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
402 static struct GNUNET_CONTAINER_MultiHashMap *peers;
405 * Handle to communicate with core
407 static struct GNUNET_CORE_Handle *core_handle;
412 //static struct GNUNET_DHT_Handle *dht_handle; FIXME DHT
417 static struct GNUNET_SERVER_Handle *server_handle;
420 * Notification context, to send messages to local clients
422 static struct GNUNET_SERVER_NotificationContext *nc;
425 * Local peer own ID (memory efficient handle)
427 static GNUNET_PEER_Id myid;
430 * Local peer own ID (full value)
432 static struct GNUNET_PeerIdentity my_full_id;
437 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
442 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
445 * Tunnel ID for the next created tunnel (global tunnel number)
447 static MESH_TunnelNumber next_tid;
450 * All application types provided by this peer
452 static struct GNUNET_CONTAINER_MultiHashMap *applications;
455 * All message types clients of this peer are interested in
457 static struct GNUNET_CONTAINER_MultiHashMap *types;
460 * Task to periodically announce provided applications
462 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
465 * Task to periodically announce itself in the network
467 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
470 unsigned int next_client_id;
474 /******************************************************************************/
475 /************************ ITERATORS ****************************/
476 /******************************************************************************/
478 /* FIXME move iterators here */
481 /******************************************************************************/
482 /************************ PERIODIC FUNCTIONS ****************************/
483 /******************************************************************************/
486 * Announce iterator over for each application provided by the peer
489 * @param key current key code
490 * @param value value in the hash map
491 * @return GNUNET_YES if we should continue to
496 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
498 /* FIXME are hashes in multihash map equal on all aquitectures? */
500 /* GNUNET_DHT_put (dht_handle, key, 10U, GNUNET_DHT_RO_RECORD_ROUTE,
501 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
502 (const char *) &my_full_id,
504 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
505 &mesh_debug, "DHT_put for app completed");
507 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
509 APP_ANNOUNCE_TIME, NULL, NULL);
516 * Periodically announce what applications are provided by local clients
519 * @param tc task context
522 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
524 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
526 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
530 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
532 announce_applications_task =
533 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
541 * Periodically announce self id in the DHT
544 * @param tc task context
547 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
549 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
551 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
555 * - Set data expiration in function of X
558 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
559 // GNUNET_h2s_full (&my_full_id.hashPubKey));
560 // GNUNET_DHT_put (dht_handle, /* DHT handle */ FIXME DHT
561 // &my_full_id.hashPubKey, /* Key to use */
562 // 10U, /* Replication level */
563 // GNUNET_DHT_RO_RECORD_ROUTE, /* DHT options */
564 // GNUNET_BLOCK_TYPE_TEST, /* Block type */
565 // sizeof(my_full_id), /* Size of the data */
566 // (char *)&my_full_id, /* Data itself */
567 // GNUNET_TIME_absolute_get_forever (), /* Data expiration */
568 // GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
570 // &mesh_debug, "DHT_put for id completed");
572 // NULL, /* Continuation */
573 // NULL); /* Continuation closure */
576 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
579 #if LATER /* FIXME DHT */
581 * Function to process paths received for a new peer addition. The recorded
582 * paths form the initial tunnel, which can be optimized later.
583 * Called on each result obtained for the DHT search.
586 * @param exp when will this value expire
587 * @param key key of the result
588 * @param type type of the result
589 * @param size number of bytes in data
590 * @param data pointer to the result data
593 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
594 const GNUNET_HashCode * key,
595 const struct GNUNET_PeerIdentity *get_path,
596 unsigned int get_path_length,
597 const struct GNUNET_PeerIdentity *put_path,
598 unsigned int put_path_length,
599 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
602 /******************************************************************************/
603 /****************** GENERAL HELPER FUNCTIONS ************************/
604 /******************************************************************************/
607 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
608 * and insert it in the appropiate structures if the peer is not known yet.
610 * @param peer Identity of the peer
612 * @return Existing or newly created peer info
614 static struct MeshPeerInfo *
615 peer_info_get (const struct GNUNET_PeerIdentity *peer)
617 struct MeshPeerInfo *peer_info;
619 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
620 if (NULL == peer_info)
623 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
624 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
625 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
626 peer_info->id = GNUNET_PEER_intern (peer);
635 * Destroy the peer_info and free any allocated resources linked to it
636 * @param t tunnel the path belongs to
637 * @param pi the peer_info to destroy
638 * @return GNUNET_OK on success
641 peer_info_destroy (struct MeshPeerInfo *pi)
643 GNUNET_HashCode hash;
644 struct GNUNET_PeerIdentity id;
646 GNUNET_PEER_resolve (pi->id, &id);
647 GNUNET_PEER_change_rc (pi->id, -1);
648 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
650 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
651 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
659 * Notify a tunnel that a connection has broken that affects at least
662 * @param t Tunnel affected.
663 * @param peer Peer that (at least) has been affected by the disconnection.
664 * @param p1 Peer that got disconnected from p2.
665 * @param p2 Peer that got disconnected from p1.
667 * @return Short ID of the peer disconnected (either p1 or p2).
668 * 0 if the tunnel remained unaffected.
670 static GNUNET_PEER_Id
671 tunnel_notify_connection_broken (struct MeshTunnel *t,
672 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
676 * Remove all paths that rely on a direct connection between p1 and p2
677 * from the peer itself and notify all tunnels about it.
679 * @param peer PeerInfo of affected peer.
680 * @param p1 GNUNET_PEER_Id of one peer.
681 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
684 * TODO: optimize (see below)
687 path_remove_from_peer (struct MeshPeerInfo *peer,
691 struct GNUNET_PeerIdentity id;
692 struct MeshPeerPath *p;
693 struct MeshPeerPath *aux;
694 struct MeshPeerInfo *peer_d;
696 unsigned int destroyed;
706 for (i = 0; i < (p->length - 1); i++)
708 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
709 (p->peers[i] == p2 && p->peers[i + 1] == p1))
721 for (i = 0; i < peer->ntunnels; i++)
723 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
725 * Problem: one or more peers have been deleted from the tunnel tree.
726 * We don't know who they are to try to add them again.
727 * We need to try to find a new path for each of the disconnected peers.
728 * Some of them might already have a path to reach them that does not
729 * involve p1 and p2. Adding all anew might render in a better tree than
730 * the trivial immediate fix.
732 * Trivial immiediate fix: try to reconnect to the disconnected node. All
733 * its children will be reachable trough him.
735 GNUNET_PEER_resolve(d, &id);
736 peer_d = peer_info_get(&id);
739 for (p = peer_d->path_head; NULL != p; p = p->next)
741 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
749 /* No callback, as peer will be already disconnected */
750 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
754 // struct MeshPathInfo *path_info;
756 if (NULL != peer_d->dhtget)
758 // path_info = GNUNET_malloc(sizeof(struct MeshPathInfo)); FIXME DHT
759 // path_info->path = p;
760 // path_info->peer = peer_d;
761 // path_info->t = peer->tunnels[i];
762 // peer_d->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
763 // GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
764 // GNUNET_BLOCK_TYPE_TEST, /* type */
765 // &id.hashPubKey, /*key to search */
766 // 4, /* replication level */
767 // GNUNET_DHT_RO_RECORD_ROUTE,
768 // NULL, /* xquery */
769 // 0, /* xquery bits */
770 // &dht_get_id_handler,
771 // (void *) path_info);
778 * Add the path to the peer and update the path used to reach it in case this
781 * @param peer_info Destination peer to add the path to.
782 * @param path New path to add. Last peer must be the peer in arg 1.
783 * Path will be either used of freed if already known.
785 * TODO: trim the part from origin to us? Add it as path to origin?
788 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
790 struct MeshPeerPath *aux;
794 if (NULL == peer_info || NULL == path)
800 l = path_get_length (path);
802 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
804 l2 = path_get_length (aux);
807 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
808 peer_info->path_tail, aux, path);
812 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
819 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
826 * Add the path to the origin peer and update the path used to reach it in case
827 * this is the shortest.
828 * The path is given in peer_info -> destination, therefore we turn the path
831 * @param peer_info Peer to add the path to, being the origin of the path.
832 * @param path New path to add after being inversed.
835 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
838 path_add_to_peer (peer_info, path);
842 #if LATER /* FIXME DHT */
844 * Build a PeerPath from the paths returned from the DHT, reversing the paths
845 * to obtain a local peer -> destination path and interning the peer ids.
847 * @return Newly allocated and created path
849 static struct MeshPeerPath *
850 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
851 unsigned int get_path_length,
852 const struct GNUNET_PeerIdentity *put_path,
853 unsigned int put_path_length)
855 struct MeshPeerPath *p;
861 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
862 for (i--; i >= 0; i--)
864 id = GNUNET_PEER_intern (&get_path[i]);
865 if (p->length > 0 && id == p->peers[p->length - 1])
867 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
868 GNUNET_PEER_change_rc(id, -1);
873 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
874 p->peers[p->length] = id;
879 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
880 for (i--; i >= 0; i--)
882 id = GNUNET_PEER_intern (&put_path[i]);
883 if (p->length > 0 && id == p->peers[p->length - 1])
885 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
886 GNUNET_PEER_change_rc(id, -1);
891 GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * (p->length + 1));
892 p->peers[p->length] = id;
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "MESH: (first of GET: %s)\n",
899 GNUNET_h2s_full(&get_path[0].hashPubKey));
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "MESH: (first of PUT: %s)\n",
902 GNUNET_h2s_full(&put_path[0].hashPubKey));
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "MESH: In total: %d hops\n",
913 * Send keepalive packets for a peer
915 * @param cls Closure (tunnel for which to send the keepalive).
916 * @param tc Notification context.
918 * TODO: implement explicit multicast keepalive?
921 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
925 * Check if client has registered with the service and has not disconnected
927 * @param client the client to check
929 * @return non-NULL if client exists in the global DLL
931 static struct MeshClient *
932 client_get (struct GNUNET_SERVER_Client *client)
934 struct MeshClient *c;
939 if (c->handle == client)
948 * Checks if a given client has subscribed to certain message type
950 * @param message_type Type of message to check
951 * @param c Client to check
953 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
958 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
962 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
963 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
968 * Search for a tunnel among the tunnels for a client
970 * @param c the client whose tunnels to search in
971 * @param tid the local id of the tunnel
973 * @return tunnel handler, NULL if doesn't exist
975 static struct MeshTunnel *
976 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
978 GNUNET_HashCode hash;
980 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
981 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
986 * Search for a tunnel by global ID using PEER_ID
988 * @param pi owner of the tunnel
989 * @param tid global tunnel number
991 * @return tunnel handler, NULL if doesn't exist
993 static struct MeshTunnel *
994 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
996 struct MESH_TunnelID id;
997 GNUNET_HashCode hash;
1002 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1003 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1008 * Search for a tunnel by global ID using full PeerIdentities
1010 * @param oid owner of the tunnel
1011 * @param tid global tunnel number
1013 * @return tunnel handler, NULL if doesn't exist
1015 static struct MeshTunnel *
1016 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1018 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1023 * Callback used to notify a client owner of a tunnel that a peer has
1024 * disconnected, most likely because of a path change.
1026 * @param n Node in the tree representing the disconnected peer
1029 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1031 struct GNUNET_MESH_PeerControl msg;
1033 if (NULL == n->t->client || NULL == nc)
1036 msg.header.size = htons (sizeof (msg));
1037 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1038 msg.tunnel_id = htonl (n->t->local_tid);
1039 GNUNET_PEER_resolve (n->peer, &msg.peer);
1040 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1041 &msg.header, GNUNET_NO);
1046 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1047 * needed rescources.
1049 * @param t Tunnel we want to add a new peer to
1050 * @param peer PeerInfo of the peer being added
1054 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1056 struct MeshPeerPath *p;
1057 struct MeshPeerPath *best_p;
1058 unsigned int best_cost;
1061 if (NULL != tree_find_peer(t->tree->root, peer->id))
1063 /* Already have it, nothing to do. */
1068 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1069 if (NULL == (p = peer->path_head))
1076 best_cost = UINT_MAX;
1079 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1086 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1087 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1088 t->path_refresh_task =
1089 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1095 * Notify a tunnel that a connection has broken that affects at least
1096 * some of its peers.
1098 * @param t Tunnel affected.
1099 * @param peer Peer that (at least) has been affected by the disconnection.
1100 * @param p1 Peer that got disconnected from p2.
1101 * @param p2 Peer that got disconnected from p1.
1103 * @return Short ID of the peer disconnected (either p1 or p2).
1104 * 0 if the tunnel remained unaffected.
1106 static GNUNET_PEER_Id
1107 tunnel_notify_connection_broken (struct MeshTunnel *t,
1108 struct MeshPeerInfo *peer,
1112 return tree_notify_connection_broken (t->tree, p1, p2,
1113 ¬ify_peer_disconnected);
1118 * Destroy the tunnel and free any allocated resources linked to it
1120 * @param t the tunnel to destroy
1122 * @return GNUNET_OK on success
1125 tunnel_destroy (struct MeshTunnel *t)
1127 struct MeshClient *c;
1128 struct MeshQueue *q;
1129 struct MeshQueue *qn;
1130 GNUNET_HashCode hash;
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DESTROYING TUNNEL at %p\n", t);
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1142 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1143 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1148 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1149 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1153 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1157 if (NULL != q->data)
1158 GNUNET_free (q->data);
1162 /* TODO cancel core transmit ready in case it was active */
1164 tree_destroy(t->tree);
1171 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1172 * client when the client disconnects.
1174 * @param cls closure (client that is disconnecting)
1175 * @param key the hash of the local tunnel id (used to access the hashmap)
1176 * @param value the value stored at the key (tunnel to destroy)
1178 * @return GNUNET_OK on success
1181 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1185 r = tunnel_destroy ((struct MeshTunnel *) value);
1190 /******************************************************************************/
1191 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1192 /******************************************************************************/
1196 * Function called to notify a client about the socket
1197 * being ready to queue more data. "buf" will be
1198 * NULL and "size" zero if the socket was closed for
1199 * writing in the meantime.
1201 * @param cls closure
1202 * @param size number of bytes available in buf
1203 * @param buf where the callee should write the message
1204 * @return number of bytes written to buf
1207 send_core_create_path (void *cls, size_t size, void *buf)
1209 struct MeshPathInfo *info = cls;
1210 struct GNUNET_MESH_ManipulatePath *msg;
1211 struct GNUNET_PeerIdentity *peer_ptr;
1212 struct MeshPeerInfo *peer = info->peer;
1213 struct MeshTunnel *t = info->t;
1214 struct MeshPeerPath *p = info->path;
1219 sizeof (struct GNUNET_MESH_ManipulatePath) +
1220 p->length * sizeof (struct GNUNET_PeerIdentity);
1222 if (size < size_needed || NULL == buf)
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
1225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
1228 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1229 GNUNET_TIME_UNIT_FOREVER_REL,
1230 path_get_first_hop (t->tree, peer->id),
1232 &send_core_create_path,
1237 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1238 msg->header.size = htons (size_needed);
1239 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1240 msg->tid = ntohl (t->id.tid);
1242 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1243 for (i = 0; i < p->length; i++)
1245 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1252 "MESH: %u bytes long create path sent!\n",
1260 * Function called to notify a client about the socket
1261 * being ready to queue more data. "buf" will be
1262 * NULL and "size" zero if the socket was closed for
1263 * writing in the meantime.
1265 * @param cls closure (MeshDataDescriptor with all info to build packet)
1266 * @param size number of bytes available in buf
1267 * @param buf where the callee should write the message
1268 * @return number of bytes written to buf
1271 send_core_data_to_origin (void *cls, size_t size, void *buf)
1273 struct MeshDataDescriptor *info = cls;
1274 struct GNUNET_MESH_ToOrigin *msg = buf;
1277 GNUNET_assert (NULL != info);
1278 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1279 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1281 if (total_size > size)
1283 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1284 "not enough buffer to send data to origin\n");
1287 msg->header.size = htons (total_size);
1288 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1289 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1290 msg->tid = htonl (info->origin->tid);
1291 if (0 != info->size)
1293 memcpy (&msg[1], &info[1], info->size);
1295 if (NULL != info->client)
1297 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1306 * Function called to notify a client about the socket
1307 * being ready to queue more data. "buf" will be
1308 * NULL and "size" zero if the socket was closed for
1309 * writing in the meantime.
1311 * @param cls closure (data itself)
1312 * @param size number of bytes available in buf
1313 * @param buf where the callee should write the message
1315 * @return number of bytes written to buf
1318 send_core_data_multicast (void *cls, size_t size, void *buf)
1320 struct MeshDataDescriptor *info = cls;
1321 struct GNUNET_MESH_Multicast *msg = buf;
1324 GNUNET_assert (NULL != info);
1325 GNUNET_assert (NULL != info->peer);
1326 total_size = info->size + sizeof (struct GNUNET_MESH_Multicast);
1327 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1329 if (total_size > size)
1332 struct GNUNET_PeerIdentity id;
1334 GNUNET_PEER_resolve(info->peer->id, &id);
1335 info->peer->infos[info->handler_n] = info;
1336 info->peer->types[info->handler_n] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1337 info->peer->core_transmit[info->handler_n] =
1338 GNUNET_CORE_notify_transmit_ready (core_handle,
1341 GNUNET_TIME_UNIT_FOREVER_REL,
1344 &send_core_data_multicast,
1348 info->peer->core_transmit[info->handler_n] = NULL;
1349 info->peer->infos[info->handler_n] = NULL;
1350 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1351 msg->header.size = htons (total_size);
1352 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1353 msg->tid = htonl (info->origin->tid);
1354 memcpy (&msg[1], info->data, info->size);
1355 if (0 == --(*info->copies))
1357 if (NULL != info->client)
1359 /* FIXME One unresponsive neighbor (who doesn't "call" tmt_rdy) can lock
1360 * the client from sending anything else to the service.
1361 * - Call receive_done after certain timeout.
1362 * - Here cancel the timeout.
1364 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1366 GNUNET_free (info->data);
1367 GNUNET_free (info->copies);
1375 * Function called to notify a client about the socket
1376 * being ready to queue more data. "buf" will be
1377 * NULL and "size" zero if the socket was closed for
1378 * writing in the meantime.
1380 * @param cls closure (MeshDataDescriptor)
1381 * @param size number of bytes available in buf
1382 * @param buf where the callee should write the message
1383 * @return number of bytes written to buf
1386 send_core_path_ack (void *cls, size_t size, void *buf)
1388 struct MeshDataDescriptor *info = cls;
1389 struct GNUNET_MESH_PathACK *msg = buf;
1391 GNUNET_assert (NULL != info);
1394 info->peer->core_transmit[info->handler_n] = NULL;
1396 if (sizeof (struct GNUNET_MESH_PathACK) > size)
1401 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
1402 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
1403 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1404 msg->tid = htonl (info->origin->tid);
1405 msg->peer_id = my_full_id;
1407 /* TODO add signature */
1409 return sizeof (struct GNUNET_MESH_PathACK);
1414 * Function called to notify a client about the socket
1415 * being ready to queue more data. "buf" will be
1416 * NULL and "size" zero if the socket was closed for
1417 * writing in the meantime.
1419 * @param cls closure (data itself)
1420 * @param size number of bytes available in buf
1421 * @param buf where the callee should write the message
1422 * @return number of bytes written to buf
1425 send_core_data_raw (void *cls, size_t size, void *buf)
1427 struct GNUNET_MessageHeader *msg = cls;
1430 GNUNET_assert (NULL != msg);
1431 total_size = ntohs (msg->size);
1433 if (total_size > size)
1438 memcpy (buf, msg, total_size);
1446 * Send another peer a notification to destroy a tunnel
1447 * @param cls The tunnel to destroy
1448 * @param size Size in the buffer
1449 * @param buf Memory where to put the data to transmit
1450 * @return Size of data put in buffer
1453 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
1455 struct MeshTunnel *t = cls;
1456 struct MeshClient *c;
1457 struct GNUNET_MESH_TunnelMessage *msg;
1461 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1462 /*FIXME*/ msg->header.size =
1463 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1464 msg->tunnel_id = htonl (t->id.tid);
1466 tunnel_destroy (c, t);
1467 return sizeof (struct GNUNET_MESH_TunnelMessage);
1473 * Send the message to all clients that have subscribed to its type
1475 * @param msg Pointer to the message itself
1476 * @return number of clients this message was sent to
1479 send_subscribed_clients (struct GNUNET_MessageHeader *msg)
1481 struct MeshClient *c;
1485 type = ntohs (msg->type);
1486 for (count = 0, c = clients; c != NULL; c = c->next)
1488 if (client_is_subscribed (type, c))
1491 GNUNET_SERVER_notification_context_unicast (nc, c->handle, msg,
1501 * Notify the client that owns the tunnel that a peer has connected to it
1503 * @param t Tunnel whose owner to notify
1504 * @param id Short id of the peer that has connected
1507 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1509 struct GNUNET_MESH_PeerControl pc;
1511 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1512 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1513 pc.tunnel_id = htonl (t->local_tid);
1514 GNUNET_PEER_resolve (id, &pc.peer);
1515 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1516 &pc.header, GNUNET_NO);
1520 /******************************************************************************/
1521 /******************** MESH NETWORK HANDLERS **************************/
1522 /******************************************************************************/
1526 * Core handler for path creation
1527 * struct GNUNET_CORE_MessageHandler
1529 * @param cls closure
1530 * @param message message
1531 * @param peer peer identity this notification is about
1532 * @param atsi performance data
1533 * @return GNUNET_OK to keep the connection open,
1534 * GNUNET_SYSERR to close it (signal serious error)
1538 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
1539 const struct GNUNET_MessageHeader *message,
1540 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1542 unsigned int own_pos;
1545 MESH_TunnelNumber tid;
1546 struct GNUNET_MESH_ManipulatePath *msg;
1547 struct GNUNET_PeerIdentity *pi;
1548 struct GNUNET_PeerIdentity id;
1549 GNUNET_HashCode hash;
1550 struct MeshPeerPath *path;
1551 struct MeshPeerInfo *dest_peer_info;
1552 struct MeshPeerInfo *orig_peer_info;
1553 struct MeshTunnel *t;
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556 "MESH: Received a path create msg\n");
1557 size = ntohs (message->size);
1558 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
1560 GNUNET_break_op (0);
1564 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
1565 if (size % sizeof (struct GNUNET_PeerIdentity))
1567 GNUNET_break_op (0);
1570 size /= sizeof (struct GNUNET_PeerIdentity);
1573 GNUNET_break_op (0);
1576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1577 "MESH: path as %u hops.\n",
1579 msg = (struct GNUNET_MESH_ManipulatePath *) message;
1581 tid = ntohl (msg->tid);
1582 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1583 t = tunnel_get (pi, tid);
1584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1585 "MESH: path as for tunnel %s [%X].\n",
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
1591 t = GNUNET_malloc (sizeof (struct MeshTunnel));
1592 t->id.oid = GNUNET_PEER_intern (pi);
1594 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
1596 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1598 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
1599 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1606 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
1607 if (NULL == dest_peer_info)
1609 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1610 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
1611 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
1613 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1615 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
1616 if (NULL == orig_peer_info)
1618 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
1619 orig_peer_info->id = GNUNET_PEER_intern (pi);
1620 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
1621 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1624 path = path_new (size);
1626 for (i = 0; i < size; i++)
1628 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
1629 if (path->peers[i] == myid)
1633 { /* cannot be self, must be 'not found' */
1634 /* create path: self not found in path through self */
1635 GNUNET_break_op (0);
1636 path_destroy (path);
1637 /* FIXME error. destroy tunnel? leave for timeout? */
1640 if (own_pos == size - 1)
1642 /* It is for us! Send ack. */
1643 struct MeshDataDescriptor *info;
1646 path_add_to_origin (orig_peer_info, path); /* inverts path! */
1647 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1648 info->origin = &t->id;
1649 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1650 GNUNET_assert (NULL != info->peer);
1651 for (j = 0; info->peer->core_transmit[j]; j++)
1653 if (j == (CORE_QUEUE_SIZE - 1))
1659 info->handler_n = j;
1660 info->peer->core_transmit[j] =
1661 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1662 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1663 sizeof (struct GNUNET_MESH_PathACK),
1664 &send_core_path_ack, info);
1668 /* It's for somebody else! Retransmit. */
1669 struct MeshPathInfo *path_info;
1671 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1673 path_info->path = path;
1674 path_info->peer = dest_peer_info;
1676 path_add_to_peer (dest_peer_info, path);
1677 GNUNET_PEER_resolve (path->peers[own_pos + 1], &id);
1678 GNUNET_CORE_notify_transmit_ready (
1682 GNUNET_TIME_UNIT_FOREVER_REL,
1684 sizeof (struct GNUNET_MESH_ManipulatePath) +
1685 path->length * sizeof(struct GNUNET_PeerIdentity),
1686 &send_core_create_path,
1694 * Core handler for mesh network traffic going from the origin to a peer
1696 * @param cls closure
1697 * @param peer peer identity this notification is about
1698 * @param message message
1699 * @param atsi performance data
1700 * @return GNUNET_OK to keep the connection open,
1701 * GNUNET_SYSERR to close it (signal serious error)
1704 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1705 const struct GNUNET_MessageHeader *message,
1706 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1708 struct GNUNET_MESH_Unicast *msg;
1709 struct MeshTunnel *t;
1710 struct MeshPeerInfo *pi;
1713 size = ntohs (message->size);
1715 sizeof (struct GNUNET_MESH_Unicast) +
1716 sizeof (struct GNUNET_MessageHeader))
1721 msg = (struct GNUNET_MESH_Unicast *) message;
1722 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1725 /* TODO notify back: we don't know this tunnel */
1726 GNUNET_break_op (0);
1729 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
1730 &msg->destination.hashPubKey);
1733 /* TODO maybe feedback, log to statistics */
1734 GNUNET_break_op (0);
1739 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1742 msg = GNUNET_malloc (size);
1743 memcpy (msg, message, size);
1744 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1745 GNUNET_TIME_UNIT_FOREVER_REL,
1746 path_get_first_hop (t->tree, pi->id),
1748 &send_core_data_raw, msg);
1754 * Core handler for mesh network traffic going from the origin to all peers
1756 * @param cls closure
1757 * @param message message
1758 * @param peer peer identity this notification is about
1759 * @param atsi performance data
1760 * @return GNUNET_OK to keep the connection open,
1761 * GNUNET_SYSERR to close it (signal serious error)
1763 * TODO: Check who we got this from, to validate route.
1766 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
1767 const struct GNUNET_MessageHeader *message,
1768 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1770 struct GNUNET_MESH_Multicast *msg;
1771 struct GNUNET_PeerIdentity *id;
1772 struct MeshDataDescriptor *info;
1773 struct MeshTunnelTreeNode *n;
1774 struct MeshTunnel *t;
1775 unsigned int *copies;
1780 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
1781 if (size < sizeof (struct GNUNET_MessageHeader))
1783 GNUNET_break_op (0);
1786 msg = (struct GNUNET_MESH_Multicast *) message;
1787 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1791 /* TODO notify that we dont know that tunnel */
1792 GNUNET_break_op (0);
1796 /* Transmit to locally interested clients */
1797 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
1799 send_subscribed_clients ((struct GNUNET_MessageHeader *) &msg[1]);
1801 n = t->tree->me->children_head;
1804 copies = GNUNET_malloc (sizeof (unsigned int));
1805 for (*copies = 0; NULL != n; n = n->next)
1807 n = t->tree->me->children_head;
1808 data = GNUNET_malloc (size);
1809 memcpy (data, &msg[1], size);
1812 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1813 info->origin = &t->id;
1816 info->copies = copies;
1817 info->client = t->client->handle;
1818 info->destination = n->peer;
1819 id = path_get_first_hop(t->tree, n->peer);
1820 info->peer = peer_info_get(id);
1821 GNUNET_assert (NULL != info->peer);
1822 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
1824 if (i == (CORE_QUEUE_SIZE - 1))
1830 info->handler_n = i;
1831 info->peer->infos[i] = info;
1832 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1833 info->peer->core_transmit[i] =
1834 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1835 GNUNET_TIME_UNIT_FOREVER_REL, id,
1836 ntohs (msg->header.size),
1837 &send_core_data_multicast, info);
1845 * Core handler for mesh network traffic
1847 * @param cls closure
1848 * @param message message
1849 * @param peer peer identity this notification is about
1850 * @param atsi performance data
1852 * @return GNUNET_OK to keep the connection open,
1853 * GNUNET_SYSERR to close it (signal serious error)
1856 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
1857 const struct GNUNET_MessageHeader *message,
1858 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1860 struct GNUNET_MESH_ToOrigin *msg;
1861 struct GNUNET_PeerIdentity id;
1862 struct MeshPeerInfo *peer_info;
1863 struct MeshTunnel *t;
1866 size = ntohs (message->size);
1867 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
1868 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
1870 GNUNET_break_op (0);
1873 msg = (struct GNUNET_MESH_ToOrigin *) message;
1874 t = tunnel_get (&msg->oid, ntohl (msg->tid));
1878 /* TODO notify that we dont know this tunnel (whom)? */
1882 if (t->id.oid == myid)
1884 if (NULL == t->client)
1886 /* got data packet for ownerless tunnel */
1887 GNUNET_break_op (0);
1890 /* TODO signature verification */
1891 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, message,
1895 peer_info = peer_info_get (&msg->oid);
1896 if (NULL == peer_info)
1898 /* unknown origin of tunnel */
1902 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
1903 msg = GNUNET_malloc (size);
1904 memcpy (msg, message, size);
1905 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1906 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
1907 &send_core_data_raw, msg);
1914 * Core handler for path ACKs
1916 * @param cls closure
1917 * @param message message
1918 * @param peer peer identity this notification is about
1919 * @param atsi performance data
1921 * @return GNUNET_OK to keep the connection open,
1922 * GNUNET_SYSERR to close it (signal serious error)
1925 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1926 const struct GNUNET_MessageHeader *message,
1927 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1929 struct GNUNET_MESH_PathACK *msg;
1930 struct MeshTunnelTreeNode *n;
1931 struct MeshPeerInfo *peer_info;
1932 struct MeshTunnel *t;
1934 msg = (struct GNUNET_MESH_PathACK *) message;
1935 t = tunnel_get (&msg->oid, msg->tid);
1938 /* TODO notify that we don't know the tunnel */
1942 /* Message for us? */
1943 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
1945 if (NULL == t->client)
1947 GNUNET_break_op (0);
1950 peer_info = peer_info_get (&msg->peer_id);
1951 if (NULL == peer_info)
1953 GNUNET_break_op (0);
1956 n = tree_find_peer(t->tree->root, peer_info->id);
1959 GNUNET_break_op (0);
1962 n->status = MESH_PEER_READY;
1963 send_client_peer_connected(t, peer_info->id);
1967 peer_info = peer_info_get (&msg->oid);
1968 if (NULL == peer_info)
1970 /* If we know the tunnel, we should DEFINITELY know the peer */
1974 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
1975 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
1976 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1977 GNUNET_TIME_UNIT_FOREVER_REL,
1978 path_get_first_hop (t->tree,
1980 sizeof (struct GNUNET_MESH_PathACK),
1981 &send_core_data_raw, msg);
1987 * Functions to handle messages from core
1989 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
1990 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
1991 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
1992 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
1993 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
1994 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1995 sizeof (struct GNUNET_MESH_PathACK)},
2001 /******************************************************************************/
2002 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2003 /******************************************************************************/
2006 * deregister_app: iterator for removing each application registered by a client
2008 * @param cls closure
2009 * @param key the hash of the application id (used to access the hashmap)
2010 * @param value the value stored at the key (client)
2012 * @return GNUNET_OK on success
2015 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2017 GNUNET_CONTAINER_multihashmap_remove (applications, key, value);
2023 * notify_client_connection_failure: notify a client that the connection to the
2024 * requested remote peer is not possible (for instance, no route found)
2025 * Function called when the socket is ready to queue more data. "buf" will be
2026 * NULL and "size" zero if the socket was closed for writing in the meantime.
2028 * @param cls closure
2029 * @param size number of bytes available in buf
2030 * @param buf where the callee should write the message
2031 * @return number of bytes written to buf
2034 notify_client_connection_failure (void *cls, size_t size, void *buf)
2037 struct MeshPeerInfo *peer_info;
2038 struct GNUNET_MESH_PeerControl *msg;
2039 struct GNUNET_PeerIdentity id;
2041 if (0 == size && NULL == buf)
2043 // TODO retry? cancel?
2047 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2048 peer_info = (struct MeshPeerInfo *) cls;
2049 msg = (struct GNUNET_MESH_PeerControl *) buf;
2050 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2051 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2052 // msg->tunnel_id = htonl(peer_info->t->tid);
2053 GNUNET_PEER_resolve (peer_info->id, &id);
2054 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2062 * Send keepalive packets for a peer
2064 * @param cls Closure (tunnel for which to send the keepalive).
2065 * @param tc Notification context.
2067 * TODO: implement explicit multicast keepalive?
2070 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2072 struct MeshTunnel *t = cls;
2073 struct GNUNET_MessageHeader *payload;
2074 struct GNUNET_MESH_Multicast *msg;
2077 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2078 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2084 "MESH: sending keepalive for tunnel %d\n",
2087 size = sizeof(struct GNUNET_MESH_Multicast) +
2088 sizeof(struct GNUNET_MessageHeader);
2089 msg = GNUNET_malloc (size);
2090 msg->header.size = htons (size);
2091 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2092 msg->oid = my_full_id;
2093 msg->tid = htonl(t->id.tid);
2094 payload = (struct GNUNET_MessageHeader *) &msg[1];
2095 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2096 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2097 handle_mesh_data_multicast (NULL, &my_full_id, &msg->header, NULL);
2100 t->path_refresh_task =
2101 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2105 #if LATER /* FIXME DHT */
2107 * Function to process paths received for a new peer addition. The recorded
2108 * paths form the initial tunnel, which can be optimized later.
2109 * Called on each result obtained for the DHT search.
2111 * @param cls closure
2112 * @param exp when will this value expire
2113 * @param key key of the result
2114 * @param type type of the result
2115 * @param size number of bytes in data
2116 * @param data pointer to the result data
2118 * TODO: re-issue the request after certain time? cancel after X results?
2121 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2122 const GNUNET_HashCode * key,
2123 const struct GNUNET_PeerIdentity *get_path,
2124 unsigned int get_path_length,
2125 const struct GNUNET_PeerIdentity *put_path,
2126 unsigned int put_path_length,
2127 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2129 struct MeshPathInfo *path_info = cls;
2130 struct MeshPathInfo *path_info_aux;
2131 struct MeshPeerPath *p;
2132 struct MeshPeerPath *aux;
2133 struct GNUNET_PeerIdentity pi;
2136 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2137 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2138 "MESH: Got results from DHT for %s\n",
2139 GNUNET_h2s_full(&pi.hashPubKey));
2140 if (NULL == get_path || NULL == put_path)
2142 if (NULL == path_info->peer->path_head)
2144 // Find ourselves some alternate initial path to the destination: retry
2145 GNUNET_DHT_get_stop (path_info->peer->dhtget);
2146 // path_info->peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */ FIXME DHT
2147 // GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2148 // GNUNET_BLOCK_TYPE_TEST, /* type */
2149 // &pi.hashPubKey, /*key to search */
2150 // 4, /* replication level */
2151 // GNUNET_DHT_RO_RECORD_ROUTE,
2152 // NULL, /* xquery */
2153 // 0, /* xquery bits */
2154 // &dht_get_id_handler,
2155 // (void *) path_info);
2160 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2161 path_add_to_peer (path_info->peer, p);
2162 for (i = 0; i < path_info->peer->ntunnels; i++)
2164 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2165 aux = tree_get_path_to_peer(path_info->peer->tunnels[i]->tree,
2166 path_info->peer->id);
2167 if (aux->length > 1)
2169 struct GNUNET_PeerIdentity id;
2171 path_info_aux = GNUNET_malloc (sizeof (struct MeshPathInfo));
2172 path_info_aux->path = aux;
2173 path_info_aux->peer = path_info->peer;
2174 path_info_aux->t = path_info->t;
2175 GNUNET_PEER_resolve (p->peers[1], &id);
2176 GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
2179 GNUNET_TIME_UNIT_FOREVER_REL,
2182 sizeof (struct GNUNET_MESH_ManipulatePath)
2185 sizeof (struct GNUNET_PeerIdentity)),
2187 &send_core_create_path,
2189 path_info_aux); /* cls */
2193 send_client_peer_connected(path_info->t, myid);
2196 GNUNET_free (path_info);
2203 * Function to process paths received for a new peer addition. The recorded
2204 * paths form the initial tunnel, which can be optimized later.
2205 * Called on each result obtained for the DHT search.
2207 * @param cls closure
2208 * @param exp when will this value expire
2209 * @param key key of the result
2210 * @param type type of the result
2211 * @param size number of bytes in data
2212 * @param data pointer to the result data
2215 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2216 const GNUNET_HashCode * key,
2217 const struct GNUNET_PeerIdentity *get_path,
2218 unsigned int get_path_length,
2219 const struct GNUNET_PeerIdentity *put_path,
2220 unsigned int put_path_length,
2221 enum GNUNET_BLOCK_Type type, size_t size,
2224 const struct GNUNET_PeerIdentity *pi = data;
2225 struct GNUNET_PeerIdentity id;
2226 struct MeshTunnel *t = cls;
2227 struct MeshPeerInfo *peer_info;
2228 struct MeshPathInfo *path_info;
2229 struct MeshPeerPath *p;
2232 if (size != sizeof (struct GNUNET_PeerIdentity))
2234 GNUNET_break_op (0);
2237 GNUNET_assert (NULL != t->client);
2238 GNUNET_DHT_get_stop (t->client->dht_get_type);
2239 t->client->dht_get_type = NULL;
2240 peer_info = peer_info_get (pi);
2241 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2242 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2244 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2245 NULL == peer_info->dhtget)
2247 // path_info = GNUNET_malloc (sizeof (struct MeshPathInfo)); FIXME DHT
2248 // path_info->peer = peer_info;
2249 // path_info->t = t;
2250 // /* we don't have a route to the peer, let's try a direct lookup */
2251 // peer_info->dhtget = GNUNET_DHT_get_start (dht_handle,
2253 // GNUNET_TIME_UNIT_FOREVER_REL,
2255 // GNUNET_BLOCK_TYPE_TEST,
2258 // /* key to look up */
2260 // /* replication level */
2261 // GNUNET_DHT_RO_RECORD_ROUTE,
2262 // /* option to dht: record route */
2263 // NULL, /* xquery */
2264 // 0, /* xquery bits */
2265 // dht_get_id_handler,
2267 // path_info); /* closure */
2271 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2272 path_add_to_peer (peer_info, p);
2273 tunnel_add_peer(t, peer_info);
2274 p = tree_get_path_to_peer(t->tree, peer_info->id);
2276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2277 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2278 t->local_tid, p->length);
2279 for (i = 0; i < p->length; i++)
2281 GNUNET_PEER_resolve (p->peers[0], &id);
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2283 GNUNET_h2s_full (&id.hashPubKey));
2289 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2291 path_info->peer = peer_info;
2292 path_info->path = p;
2293 GNUNET_PEER_resolve (p->peers[1], &id);
2294 GNUNET_CORE_notify_transmit_ready (core_handle,
2300 GNUNET_TIME_UNIT_FOREVER_REL,
2304 sizeof (struct GNUNET_MESH_ManipulatePath)
2307 sizeof (struct GNUNET_PeerIdentity)),
2309 &send_core_create_path,
2311 path_info); /* cls */
2315 send_client_peer_connected(t, myid);
2320 /******************************************************************************/
2321 /********************* MESH LOCAL HANDLES **************************/
2322 /******************************************************************************/
2326 * Handler for client disconnection
2328 * @param cls closure
2329 * @param client identification of the client; NULL
2330 * for the last call when the server is destroyed
2333 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2335 struct MeshClient *c;
2336 struct MeshClient *next;
2338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2344 if (c->handle != client && NULL != client)
2346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2351 if (NULL != c->tunnels)
2353 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2354 &tunnel_destroy_iterator,
2356 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2359 /* deregister clients applications */
2360 if (NULL != c->apps)
2362 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2363 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2365 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2366 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2368 GNUNET_SCHEDULER_cancel (announce_applications_task);
2369 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2371 if (NULL != c->types)
2372 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2373 if (NULL != c->dht_get_type)
2374 GNUNET_DHT_get_stop (c->dht_get_type);
2375 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2387 * Handler for new clients
2389 * @param cls closure
2390 * @param client identification of the client
2391 * @param message the actual message, which includes messages the client wants
2394 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2395 const struct GNUNET_MessageHeader *message)
2397 struct GNUNET_MESH_ClientConnect *cc_msg;
2398 struct MeshClient *c;
2399 GNUNET_MESH_ApplicationType *a;
2406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2407 /* Check data sanity */
2408 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2409 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2410 ntypes = ntohs (cc_msg->types);
2411 napps = ntohs (cc_msg->applications);
2413 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2416 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2420 /* Create new client structure */
2421 c = GNUNET_malloc (sizeof (struct MeshClient));
2423 c->id = next_client_id++;
2426 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2429 GNUNET_MESH_ApplicationType at;
2432 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2433 for (i = 0; i < napps; i++)
2436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2437 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2438 /* store in clients hashmap */
2439 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2440 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2441 /* store in global hashmap, for announcements */
2442 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2443 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2445 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2446 announce_applications_task =
2447 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2455 t = (uint16_t *) & a[napps];
2456 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2457 for (i = 0; i < ntypes; i++)
2460 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2462 /* store in clients hashmap */
2463 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2464 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2465 /* store in global hashmap */
2466 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2467 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2471 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2473 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2474 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2475 GNUNET_SERVER_notification_context_add (nc, client);
2477 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2485 * Handler for requests of new tunnels
2487 * @param cls closure
2488 * @param client identification of the client
2489 * @param message the actual message
2492 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2493 const struct GNUNET_MessageHeader *message)
2495 struct GNUNET_MESH_TunnelMessage *t_msg;
2496 struct MeshTunnel *t;
2497 struct MeshClient *c;
2498 GNUNET_HashCode hash;
2500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
2502 /* Sanity check for client registration */
2503 if (NULL == (c = client_get (client)))
2506 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2513 /* Message sanity check */
2514 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2521 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2522 /* Sanity check for tunnel numbering */
2523 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
2526 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2529 /* Sanity check for duplicate tunnel IDs */
2530 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
2533 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2537 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
2539 while (NULL != tunnel_get_by_pi (myid, next_tid))
2540 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
2541 t->id.tid = next_tid++;
2543 t->local_tid = ntohl (t_msg->tunnel_id);
2545 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
2547 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2549 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
2550 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2553 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2557 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2559 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2560 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2563 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2566 t->tree = tree_new (t, myid);
2567 t->tree->refresh = REFRESH_PATH_TIME;
2568 t->tree->root->status = MESH_PEER_READY;
2569 t->tree->me = t->tree->root;
2571 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2577 * Handler for requests of deleting tunnels
2579 * @param cls closure
2580 * @param client identification of the client
2581 * @param message the actual message
2584 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2585 const struct GNUNET_MessageHeader *message)
2587 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
2588 struct MeshClient *c;
2589 struct MeshTunnel *t;
2590 MESH_TunnelNumber tid;
2591 GNUNET_HashCode hash;
2593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
2595 /* Sanity check for client registration */
2596 if (NULL == (c = client_get (client)))
2599 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2602 /* Message sanity check */
2603 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
2606 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2612 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
2614 /* Retrieve tunnel */
2615 tid = ntohl (tunnel_msg->tunnel_id);
2617 /* Remove from local id hashmap */
2618 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2619 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
2620 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
2622 /* Remove from global id hashmap */
2623 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2624 GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t);
2626 // notify_tunnel_destroy(t); FIXME
2628 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2634 * Handler for connection requests to new peers
2636 * @param cls closure
2637 * @param client identification of the client
2638 * @param message the actual message (PeerControl)
2641 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
2642 const struct GNUNET_MessageHeader *message)
2644 struct GNUNET_MESH_PeerControl *peer_msg;
2645 struct MeshPathInfo *path_info;
2646 struct MeshPeerInfo *peer_info;
2647 struct MeshClient *c;
2648 struct MeshTunnel *t;
2649 MESH_TunnelNumber tid;
2651 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
2652 /* Sanity check for client registration */
2653 if (NULL == (c = client_get (client)))
2656 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2660 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2661 /* Sanity check for message size */
2662 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2665 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2669 /* Tunnel exists? */
2670 tid = ntohl (peer_msg->tunnel_id);
2671 t = tunnel_get_by_local_id (c, tid);
2675 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2679 /* Does client own tunnel? */
2680 if (t->client->handle != client)
2683 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2686 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
2687 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
2688 peer_info = peer_info_get (&peer_msg->peer);
2690 /* Start DHT search if needed, otherwise just add peer to tunnel. */
2691 if (NULL == peer_info->dhtget && NULL == peer_info->path_head)
2693 // path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2694 // path_info->peer = peer_info;
2695 // path_info->t = t;
2696 // peer_info->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */ FIXME DHT
2697 // GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2698 // GNUNET_BLOCK_TYPE_TEST, /* type */
2699 // &peer_msg->peer.hashPubKey, /*key to search */
2700 // 4, /* replication level */
2701 // GNUNET_DHT_RO_RECORD_ROUTE,
2702 // NULL, /* xquery */
2703 // 0, /* xquery bits */
2704 // &dht_get_id_handler,
2705 // (void *) path_info);
2707 else if (NULL != peer_info->path_head)
2710 for (i = 0; i < CORE_QUEUE_SIZE; i++)
2712 if (NULL == peer_info->core_transmit[i])
2715 if (CORE_QUEUE_SIZE == i)
2718 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2721 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
2722 path_info->peer = peer_info;
2724 tunnel_add_peer(t, peer_info);
2725 path_info->path = tree_get_path_to_peer(t->tree, peer_info->id);
2726 peer_info = peer_info_get(path_get_first_hop(t->tree, path_info->peer->id));
2727 peer_info->infos[i] = path_info;
2728 peer_info->types[i] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
2729 peer_info->core_transmit[i] =
2730 GNUNET_CORE_notify_transmit_ready (
2734 GNUNET_TIME_UNIT_FOREVER_REL,
2735 path_get_first_hop(t->tree, path_info->peer->id),
2736 sizeof (struct GNUNET_MESH_ManipulatePath)
2737 + path_info->path->length * sizeof(struct GNUNET_PeerIdentity),
2738 &send_core_create_path,
2741 /* Otherwise: there is no path yet, but there is a DHT_get active already. */
2742 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2748 * Handler for disconnection requests of peers in a tunnel
2750 * @param cls closure
2751 * @param client identification of the client
2752 * @param message the actual message (PeerControl)
2755 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
2756 const struct GNUNET_MessageHeader *message)
2758 struct GNUNET_MESH_PeerControl *peer_msg;
2759 struct MeshClient *c;
2760 struct MeshTunnel *t;
2761 MESH_TunnelNumber tid;
2763 /* Sanity check for client registration */
2764 if (NULL == (c = client_get (client)))
2767 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2770 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
2771 /* Sanity check for message size */
2772 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
2775 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2779 /* Tunnel exists? */
2780 tid = ntohl (peer_msg->tunnel_id);
2781 t = tunnel_get_by_local_id (c, tid);
2785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2789 /* Does client own tunnel? */
2790 if (t->client->handle != client)
2793 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2797 /* Ok, delete peer from tunnel */
2798 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
2799 &peer_msg->peer.hashPubKey);
2801 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2807 * Handler for connection requests to new peers by type
2809 * @param cls closure
2810 * @param client identification of the client
2811 * @param message the actual message (ConnectPeerByType)
2814 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
2815 const struct GNUNET_MessageHeader *message)
2817 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
2818 struct MeshClient *c;
2819 struct MeshTunnel *t;
2820 GNUNET_HashCode hash;
2821 GNUNET_MESH_ApplicationType type;
2822 MESH_TunnelNumber tid;
2824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
2825 /* Sanity check for client registration */
2826 if (NULL == (c = client_get (client)))
2829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2833 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
2834 /* Sanity check for message size */
2835 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
2836 ntohs (connect_msg->header.size))
2839 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2843 /* Tunnel exists? */
2844 tid = ntohl (connect_msg->tunnel_id);
2845 t = tunnel_get_by_local_id (c, tid);
2849 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2853 /* Does client own tunnel? */
2854 if (t->client->handle != client)
2857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2861 /* Do WE have the service? */
2862 type = ntohl (connect_msg->type);
2863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", type);
2864 GNUNET_CRYPTO_hash (&type, sizeof (GNUNET_MESH_ApplicationType), &hash);
2865 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
2868 /* Yes! Fast forward, add ourselves to the tunnel and send the
2869 * good news to the client
2871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
2872 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
2873 peer_info_get (&my_full_id),
2874 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
2877 send_client_peer_connected(t, myid);
2878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
2879 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2882 /* Ok, lets find a peer offering the service */
2883 if (c->dht_get_type)
2885 GNUNET_DHT_get_stop (c->dht_get_type);
2887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
2888 GNUNET_h2s_full (&hash));
2889 // c->dht_get_type = FIXME DHT
2890 // GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
2891 // GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
2892 // GNUNET_DHT_RO_RECORD_ROUTE, NULL, 0,
2893 // &dht_get_type_handler, t);
2895 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2901 * Handler for client traffic directed to one peer
2903 * @param cls closure
2904 * @param client identification of the client
2905 * @param message the actual message
2908 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
2909 const struct GNUNET_MessageHeader *message)
2911 struct MeshClient *c;
2912 struct MeshTunnel *t;
2913 struct MeshPeerInfo *pi;
2914 struct GNUNET_MESH_Unicast *data_msg;
2915 MESH_TunnelNumber tid;
2918 /* Sanity check for client registration */
2919 if (NULL == (c = client_get (client)))
2922 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2925 data_msg = (struct GNUNET_MESH_Unicast *) message;
2926 /* Sanity check for message size */
2927 size = ntohs (message->size);
2928 if (sizeof (struct GNUNET_MESH_Unicast) +
2929 sizeof (struct GNUNET_MessageHeader) > size)
2932 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2936 /* Tunnel exists? */
2937 tid = ntohl (data_msg->tid);
2938 t = tunnel_get_by_local_id (c, tid);
2942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2946 /* Is it a local tunnel? Then, does client own the tunnel? */
2947 if (t->client->handle != NULL && t->client->handle != client)
2950 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2954 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
2955 &data_msg->destination.hashPubKey);
2956 /* Is the selected peer in the tunnel? */
2960 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2964 /* Ok, everything is correct, send the message
2965 * (pretend we got it from a mesh peer)
2968 char buf[ntohs (message->size)];
2969 struct GNUNET_MESH_Unicast *copy;
2971 /* Work around const limitation */
2972 copy = (struct GNUNET_MESH_Unicast *) buf;
2973 memcpy (buf, data_msg, size);
2974 copy->oid = my_full_id;
2975 copy->tid = htonl (t->id.tid);
2976 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
2978 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2983 * Handler for client traffic directed to all peers in a tunnel
2985 * @param cls closure
2986 * @param client identification of the client
2987 * @param message the actual message
2990 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
2991 const struct GNUNET_MessageHeader *message)
2993 struct MeshClient *c;
2994 struct MeshTunnel *t;
2995 struct GNUNET_MESH_Multicast *data_msg;
2996 MESH_TunnelNumber tid;
2998 /* Sanity check for client registration */
2999 if (NULL == (c = client_get (client)))
3002 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3005 data_msg = (struct GNUNET_MESH_Multicast *) message;
3006 /* Sanity check for message size */
3007 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
3010 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3014 /* Tunnel exists? */
3015 tid = ntohl (data_msg->tid);
3016 t = tunnel_get_by_local_id (c, tid);
3020 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3024 /* Does client own tunnel? */
3025 if (t->client->handle != client)
3028 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3033 char buf[ntohs(message->size)];
3034 struct GNUNET_MESH_Multicast *copy;
3036 copy = (struct GNUNET_MESH_Multicast *)buf;
3037 memcpy(buf, message, ntohs(message->size));
3038 copy->oid = my_full_id;
3039 copy->tid = htonl(t->id.tid);
3040 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3043 /* receive done gets called when last copy is sent */
3048 * Functions to handle messages from clients
3050 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3051 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3052 {&handle_local_tunnel_create, NULL,
3053 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3054 sizeof (struct GNUNET_MESH_TunnelMessage)},
3055 {&handle_local_tunnel_destroy, NULL,
3056 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3057 sizeof (struct GNUNET_MESH_TunnelMessage)},
3058 {&handle_local_connect_add, NULL,
3059 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3060 sizeof (struct GNUNET_MESH_PeerControl)},
3061 {&handle_local_connect_del, NULL,
3062 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3063 sizeof (struct GNUNET_MESH_PeerControl)},
3064 {&handle_local_connect_by_type, NULL,
3065 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3066 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3067 {&handle_local_unicast, NULL,
3068 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3069 {&handle_local_unicast, NULL,
3070 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3071 {&handle_local_multicast, NULL,
3072 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3078 * To be called on core init/fail.
3080 * @param cls service closure
3081 * @param server handle to the server for this service
3082 * @param identity the public identity of this peer
3083 * @param publicKey the public key of this peer
3086 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3087 const struct GNUNET_PeerIdentity *identity,
3088 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
3090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3091 core_handle = server;
3092 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3094 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3095 GNUNET_SCHEDULER_shutdown();
3101 * Method called whenever a given peer connects.
3103 * @param cls closure
3104 * @param peer peer identity this notification is about
3105 * @param atsi performance data for the connection
3108 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3109 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
3111 struct MeshPeerInfo *peer_info;
3112 struct MeshPeerPath *path;
3114 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3115 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3116 // GNUNET_h2s(&my_full_id.hashPubKey));
3117 peer_info = peer_info_get (peer);
3118 if (myid == peer_info->id)
3120 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3125 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3126 // GNUNET_h2s(&peer->hashPubKey));
3128 path = path_new (2);
3129 path->peers[0] = myid;
3130 path->peers[1] = peer_info->id;
3131 path_add_to_peer (peer_info, path);
3136 * Method called whenever a peer disconnects.
3138 * @param cls closure
3139 * @param peer peer identity this notification is about
3142 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3144 struct MeshPeerInfo *pi;
3147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3148 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3154 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3156 if (pi->core_transmit[i])
3158 struct MeshDataDescriptor *dd;
3159 struct MeshPathInfo *path_info;
3160 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit[i]);
3161 /* TODO: notify that tranmission has failed */
3162 switch (pi->types[i])
3164 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3165 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3166 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3168 if (0 == --(*dd->copies))
3170 GNUNET_free (dd->copies);
3171 GNUNET_free (dd->data);
3174 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3175 path_info = pi->infos[i];
3176 path_destroy(path_info->path);
3179 GNUNET_free (pi->infos[i]);
3182 path_remove_from_peer (pi, pi->id, myid);
3185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3191 /******************************************************************************/
3192 /************************ MAIN FUNCTIONS ****************************/
3193 /******************************************************************************/
3196 * Task run during shutdown.
3202 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3204 // struct MeshClient *c;
3206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3207 if (core_handle != NULL)
3209 GNUNET_CORE_disconnect (core_handle);
3212 // if (dht_handle != NULL) FIXME DHT
3214 // for (c = clients; NULL != c; c = c->next)
3215 // if (NULL != c->dht_get_type)
3216 // GNUNET_DHT_get_stop (c->dht_get_type);
3217 // GNUNET_DHT_disconnect (dht_handle);
3218 // dht_handle = NULL;
3222 GNUNET_SERVER_notification_context_destroy (nc);
3225 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3227 GNUNET_SCHEDULER_cancel (announce_id_task);
3228 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3234 * Process mesh requests.
3236 * @param cls closure
3237 * @param server the initialized server
3238 * @param c configuration to use
3241 run (void *cls, struct GNUNET_SERVER_Handle *server,
3242 const struct GNUNET_CONFIGURATION_Handle *c)
3244 struct MeshPeerInfo *peer;
3245 struct MeshPeerPath *p;
3248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3249 server_handle = server;
3250 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3251 CORE_QUEUE_SIZE, /* queue size */
3252 NULL, /* Closure passed to MESH functions */
3253 &core_init, /* Call core_init once connected */
3254 &core_connect, /* Handle connects */
3255 &core_disconnect, /* remove peers on disconnects */
3256 NULL, /* Do we care about "status" updates? */
3257 NULL, /* Don't notify about all incoming messages */
3258 GNUNET_NO, /* For header only in notification */
3259 NULL, /* Don't notify about all outbound messages */
3260 GNUNET_NO, /* For header-only out notification */
3261 core_handlers); /* Register these handlers */
3262 if (core_handle == NULL)
3265 GNUNET_SCHEDULER_shutdown ();
3270 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3273 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3275 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3276 GNUNET_SCHEDULER_shutdown ();
3279 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3280 GNUNET_free (keyfile);
3281 if (my_private_key == NULL)
3283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3284 _("Mesh service could not access hostkey. Exiting.\n"));
3285 GNUNET_SCHEDULER_shutdown ();
3288 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3289 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3290 &my_full_id.hashPubKey);
3291 myid = GNUNET_PEER_intern (&my_full_id);
3293 // dht_handle = GNUNET_DHT_connect (c, 64); FIXME DHT
3294 // if (dht_handle == NULL)
3296 // GNUNET_break (0);
3301 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3302 peers = GNUNET_CONTAINER_multihashmap_create (32);
3303 applications = GNUNET_CONTAINER_multihashmap_create (32);
3304 types = GNUNET_CONTAINER_multihashmap_create (32);
3306 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3307 nc = GNUNET_SERVER_notification_context_create (server_handle,
3309 GNUNET_SERVER_disconnect_notify (server_handle,
3310 &handle_local_client_disconnect,
3315 clients_tail = NULL;
3320 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3321 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3323 /* Create a peer_info for the local peer */
3324 peer = peer_info_get(&my_full_id);
3327 path_add_to_peer(peer, p);
3329 /* Scheduled the task to clean up when shutdown is called */
3330 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3337 * The main function for the mesh service.
3339 * @param argc number of arguments from the command line
3340 * @param argv command line arguments
3341 * @return 0 ok, 1 on error
3344 main (int argc, char *const *argv)
3349 // fprintf (stderr, "main ()\n");
3351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3354 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3358 // fprintf (stderr, "main () END\n");