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 * - relay corking down to core
48 #include "mesh_protocol.h"
49 #include "gnunet_dht_service.h"
50 #include "mesh_tunnel_tree.h"
52 /* TODO: move into configuration file */
53 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
54 GNUNET_TIME_UNIT_SECONDS,\
56 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
57 GNUNET_TIME_UNIT_SECONDS,\
60 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
61 GNUNET_TIME_UNIT_SECONDS,\
64 #define GET_RESTART_TIME GNUNET_TIME_relative_multiply(\
65 GNUNET_TIME_UNIT_SECONDS,\
68 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
69 GNUNET_TIME_UNIT_SECONDS,\
72 /******************************************************************************/
73 /************************ DATA STRUCTURES ****************************/
74 /******************************************************************************/
76 /** FWD declaration */
80 * Struct containing all info possibly needed to build a package when called
83 struct MeshDataDescriptor
85 /** ID of the tunnel this packet travels in */
86 struct MESH_TunnelID *origin;
91 /** Client that asked for the transmission, if any */
92 struct GNUNET_SERVER_Client *client;
94 /** Who was this message being sent to */
95 struct MeshPeerInfo *peer;
97 /** Ultimate destination of the packet */
98 GNUNET_PEER_Id destination;
100 /** Number of identical messages sent to different hops (multicast) */
101 unsigned int *copies;
103 /** Which handler was used to request the transmission */
104 unsigned int handler_n;
106 /** Size of the data */
109 /** Used to allow a client send more traffic to the service after a
110 * previous packet was tried to be sent to a neighbor and couldn't */
111 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
116 * Struct containing all information regarding a given peer
126 * Last time we heard from this peer
128 struct GNUNET_TIME_Absolute last_contact;
131 * Number of attempts to reconnect so far
133 int n_reconnect_attempts;
136 * Paths to reach the peer, ordered by ascending hop count
138 struct MeshPeerPath *path_head;
141 * Paths to reach the peer, ordered by ascending hop count
143 struct MeshPeerPath *path_tail;
146 * Handle to stop the DHT search for a path to this peer
148 struct GNUNET_DHT_GetHandle *dhtget;
151 * Handles to stop queued transmissions for this peer
153 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
156 * Pointer to info stuctures used as cls for queued transmissions
158 void *infos[CORE_QUEUE_SIZE];
161 * Type of message being in each transmission
163 uint16_t types[CORE_QUEUE_SIZE];
166 * Array of tunnels this peer participates in
167 * (most probably a small amount, therefore not a hashmap)
168 * When the path to the peer changes, notify these tunnels to let them
169 * re-adjust their path trees.
171 struct MeshTunnel **tunnels;
174 * Number of tunnels this peers participates in
176 unsigned int ntunnels;
181 * Data scheduled to transmit (to local client or remote peer)
188 struct MeshQueue *next;
189 struct MeshQueue *prev;
192 * Target of the data (NULL if target is client)
194 struct MeshPeerInfo *peer;
197 * Client to send the data to (NULL if target is peer)
199 struct MeshClient *client;
202 * Size of the message to transmit
207 * How old is the data?
209 struct GNUNET_TIME_Absolute timestamp;
214 struct GNUNET_MessageHeader *data;
218 * Globally unique tunnel identification (owner + number)
219 * DO NOT USE OVER THE NETWORK
224 * Node that owns the tunnel
229 * Tunnel number to differentiate all the tunnels owned by the node oid
230 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
232 MESH_TunnelNumber tid;
236 struct MeshClient; /* FWD declaration */
239 * Struct containing all information regarding a tunnel
240 * For an intermediate node the improtant info used will be:
241 * - id Tunnel unique identification
242 * - paths[0] To know where to send it next
243 * - metainfo: ready, speeds, accounting
250 struct MESH_TunnelID id;
253 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
255 MESH_TunnelNumber local_tid;
258 * Last time the tunnel was used
260 struct GNUNET_TIME_Absolute timestamp;
263 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
264 * containing peers added by id or by type, not intermediate peers.
266 struct GNUNET_CONTAINER_MultiHashMap *peers;
269 * Number of peers that are connected and potentially ready to receive data
271 unsigned int peers_ready;
274 * Number of peers that have been added to the tunnel
276 unsigned int peers_total;
279 * Client owner of the tunnel, if any
281 struct MeshClient *client;
284 * Messages ready to transmit
286 struct MeshQueue *queue_head;
287 struct MeshQueue *queue_tail;
292 struct MeshTunnelTree *tree;
295 * Application type we are looking for in this tunnel
297 GNUNET_MESH_ApplicationType type;
300 * Used to search peers offering a service
302 struct GNUNET_DHT_GetHandle *dht_get_type;
305 * Task to keep the used paths alive
307 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
310 * Task to destroy the tunnel after timeout
312 * FIXME: merge the two? a tunnel will have either
313 * a path refresh OR a timeout, never both!
315 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
320 * Info needed to work with tunnel paths and peers
327 struct MeshTunnel *t;
330 * Neighbouring peer to whom we send the packet to
332 struct MeshPeerInfo *peer;
337 struct MeshPeerPath *path;
340 * Position in peer's transmit queue
347 * Struct containing information about a client of the service
354 struct MeshClient *next;
355 struct MeshClient *prev;
358 * Tunnels that belong to this client, indexed by local id
360 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
363 * Handle to communicate with the client
365 struct GNUNET_SERVER_Client *handle;
368 * Applications that this client has claimed to provide
370 struct GNUNET_CONTAINER_MultiHashMap *apps;
373 * Messages that this client has declared interest in
375 struct GNUNET_CONTAINER_MultiHashMap *types;
379 * ID of the client, for debug messages
388 /******************************************************************************/
389 /************************ DEBUG FUNCTIONS ****************************/
390 /******************************************************************************/
394 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
395 * @param cls string to print
396 * @param tc task context
399 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
403 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
411 /******************************************************************************/
412 /*********************** GLOBAL VARIABLES ****************************/
413 /******************************************************************************/
418 static struct MeshClient *clients;
419 static struct MeshClient *clients_tail;
422 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
424 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
427 * Tunnels incoming, indexed by MESH_TunnelNumber
428 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
430 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
433 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
435 static struct GNUNET_CONTAINER_MultiHashMap *peers;
438 * Handle to communicate with core
440 static struct GNUNET_CORE_Handle *core_handle;
445 static struct GNUNET_DHT_Handle *dht_handle;
450 static struct GNUNET_SERVER_Handle *server_handle;
453 * Notification context, to send messages to local clients
455 static struct GNUNET_SERVER_NotificationContext *nc;
458 * Local peer own ID (memory efficient handle)
460 static GNUNET_PEER_Id myid;
463 * Local peer own ID (full value)
465 static struct GNUNET_PeerIdentity my_full_id;
470 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
475 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
478 * Tunnel ID for the next created tunnel (global tunnel number)
480 static MESH_TunnelNumber next_tid;
483 * Tunnel ID for the next incoming tunnel (local tunnel number)
485 static MESH_TunnelNumber next_local_tid;
488 * All application types provided by this peer
490 static struct GNUNET_CONTAINER_MultiHashMap *applications;
493 * All message types clients of this peer are interested in
495 static struct GNUNET_CONTAINER_MultiHashMap *types;
498 * Task to periodically announce provided applications
500 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
503 * Task to periodically announce itself in the network
505 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
508 unsigned int next_client_id;
512 /******************************************************************************/
513 /************************ ITERATORS ****************************/
514 /******************************************************************************/
516 /* FIXME move iterators here */
519 /******************************************************************************/
520 /************************ PERIODIC FUNCTIONS ****************************/
521 /******************************************************************************/
524 * Announce iterator over for each application provided by the peer
527 * @param key current key code
528 * @param value value in the hash map
529 * @return GNUNET_YES if we should continue to
534 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
536 /* FIXME are hashes in multihash map equal on all aquitectures? */
537 GNUNET_DHT_put (dht_handle,
540 GNUNET_DHT_RO_RECORD_ROUTE |
541 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
542 GNUNET_BLOCK_TYPE_TEST,
543 sizeof (struct GNUNET_PeerIdentity),
544 (const char *) &my_full_id,
546 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
547 &mesh_debug, "DHT_put for app completed");
549 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
551 APP_ANNOUNCE_TIME, NULL, NULL);
558 * Periodically announce what applications are provided by local clients
561 * @param tc task context
564 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
566 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
568 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
572 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
574 announce_applications_task =
575 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
583 * Periodically announce self id in the DHT
586 * @param tc task context
589 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
591 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
593 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
597 * - Set data expiration in function of X
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
601 GNUNET_h2s_full (&my_full_id.hashPubKey));
602 GNUNET_DHT_put (dht_handle, /* DHT handle */
603 &my_full_id.hashPubKey, /* Key to use */
604 10U, /* Replication level */
605 GNUNET_DHT_RO_RECORD_ROUTE |
606 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
607 GNUNET_BLOCK_TYPE_TEST, /* Block type */
608 sizeof(my_full_id), /* Size of the data */
609 (char *)&my_full_id, /* Data itself */
610 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
611 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
613 &mesh_debug, "DHT_put for id completed");
615 NULL, /* Continuation */
616 NULL); /* Continuation closure */
619 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
624 * Function to process paths received for a new peer addition. The recorded
625 * paths form the initial tunnel, which can be optimized later.
626 * Called on each result obtained for the DHT search.
629 * @param exp when will this value expire
630 * @param key key of the result
631 * @param type type of the result
632 * @param size number of bytes in data
633 * @param data pointer to the result data
636 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
637 const GNUNET_HashCode * key,
638 const struct GNUNET_PeerIdentity *get_path,
639 unsigned int get_path_length,
640 const struct GNUNET_PeerIdentity *put_path,
641 unsigned int put_path_length,
642 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
645 /******************************************************************************/
646 /****************** GENERAL HELPER FUNCTIONS ************************/
647 /******************************************************************************/
650 * Check if client has registered with the service and has not disconnected
652 * @param client the client to check
654 * @return non-NULL if client exists in the global DLL
656 static struct MeshClient *
657 client_get (struct GNUNET_SERVER_Client *client)
659 struct MeshClient *c;
664 if (c->handle == client)
673 * Checks if a given client has subscribed to certain message type
675 * @param message_type Type of message to check
676 * @param c Client to check
678 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
683 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
687 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
688 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
692 client_allow_send(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
694 struct MeshDataDescriptor *info = cls;
696 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
698 info->timeout_task = GNUNET_SCHEDULER_NO_TASK;
699 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
704 * Search for a tunnel by global ID using full PeerIdentities
706 * @param oid owner of the tunnel
707 * @param tid global tunnel number
709 * @return tunnel handler, NULL if doesn't exist
711 static struct MeshTunnel *
712 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
716 * Send the message to all clients that have subscribed to its type
718 * @param msg Pointer to the message itself
719 * @return number of clients this message was sent to
722 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
723 const struct GNUNET_MessageHeader *payload)
725 struct GNUNET_PeerIdentity *oid;
726 struct MeshClient *c;
727 struct MeshTunnel *t;
728 MESH_TunnelNumber *tid;
731 char cbuf[htons(msg->size)];
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
734 type = ntohs (payload->type);
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
737 memcpy (cbuf, msg, sizeof(cbuf));
738 switch (htons(msg->type))
740 struct GNUNET_MESH_Unicast *uc;
741 struct GNUNET_MESH_Multicast *mc;
742 struct GNUNET_MESH_ToOrigin *to;
744 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
745 uc = (struct GNUNET_MESH_Unicast *) cbuf;
749 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
750 mc = (struct GNUNET_MESH_Multicast *) cbuf;
754 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
755 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
763 t = tunnel_get (oid, ntohl(*tid));
769 *tid = htonl (t->local_tid);
770 for (count = 0, c = clients; c != NULL; c = c->next)
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
773 if (client_is_subscribed (type, c))
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
777 GNUNET_SERVER_notification_context_unicast (
780 (struct GNUNET_MessageHeader *) cbuf,
789 * Notify the client that owns the tunnel that a peer has connected to it
791 * @param t Tunnel whose owner to notify
792 * @param id Short id of the peer that has connected
795 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
797 struct GNUNET_MESH_PeerControl pc;
799 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
800 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
801 pc.tunnel_id = htonl (t->local_tid);
802 GNUNET_PEER_resolve (id, &pc.peer);
803 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
804 &pc.header, GNUNET_NO);
809 * Function called to notify a client about the socket
810 * being ready to queue more data. "buf" will be
811 * NULL and "size" zero if the socket was closed for
812 * writing in the meantime.
815 * @param size number of bytes available in buf
816 * @param buf where the callee should write the message
817 * @return number of bytes written to buf
820 send_core_create_path (void *cls, size_t size, void *buf);
824 * Function called to notify a client about the socket
825 * being ready to queue more data. "buf" will be
826 * NULL and "size" zero if the socket was closed for
827 * writing in the meantime.
829 * @param cls closure (data itself)
830 * @param size number of bytes available in buf
831 * @param buf where the callee should write the message
833 * @return number of bytes written to buf
836 send_core_data_multicast (void *cls, size_t size, void *buf);
840 * Cancel a core transmission that was already requested and free all resources
841 * associated to the request.
843 * @param peer PeeInfo of the peer whose transmission is cancelled.
844 * @param i Position of the transmission to be cancelled.
847 peer_info_cancel_transmission(struct MeshPeerInfo *peer, unsigned int i)
849 if (NULL != peer->core_transmit[i])
851 struct MeshDataDescriptor *dd;
852 struct MeshPathInfo *path_info;
856 struct GNUNET_PeerIdentity id;
858 GNUNET_PEER_resolve (peer->id, &id);
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "MESH: Cancelling data transmission at %s [%u]\n",
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "MESH: message type %u\n",
868 /* TODO: notify that tranmission has failed */
869 switch (peer->types[i])
871 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
872 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
873 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
876 if (0 == --(*dd->copies))
878 GNUNET_free (dd->copies);
879 GNUNET_free (dd->data);
882 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
884 path_info = peer->infos[i];
885 path_destroy(path_info->path);
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
891 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
892 peer->core_transmit[i] = NULL;
893 GNUNET_free (peer->infos[i]);
899 * Get a unused CORE slot to transmit a message to a peer. If all the slots
900 * are used, cancel one and return it's position.
902 * @param peer PeerInfo of the neighbor we want to transmit to.
904 * @return The index of an available slot to transmit to the neighbor.
907 peer_info_transmit_slot (struct MeshPeerInfo *peer)
911 for (i = 0; peer->core_transmit[i]; i++)
913 if (i == (CORE_QUEUE_SIZE - 1))
915 /* All positions are taken! Overwriting! */
917 peer_info_cancel_transmission(peer, 0);
926 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
927 * and insert it in the appropiate structures if the peer is not known yet.
929 * @param peer Full identity of the peer.
931 * @return Existing or newly created peer info.
933 static struct MeshPeerInfo *
934 peer_info_get (const struct GNUNET_PeerIdentity *peer)
936 struct MeshPeerInfo *peer_info;
938 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
939 if (NULL == peer_info)
942 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
943 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
944 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
945 peer_info->id = GNUNET_PEER_intern (peer);
953 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
954 * and insert it in the appropiate structures if the peer is not known yet.
956 * @param peer Short identity of the peer.
958 * @return Existing or newly created peer info.
960 static struct MeshPeerInfo *
961 peer_info_get_short (const GNUNET_PEER_Id peer)
963 struct GNUNET_PeerIdentity id;
965 GNUNET_PEER_resolve(peer, &id);
966 return peer_info_get(&id);
971 * Iterator to remove the tunnel from the list of tunnels a peer participates
974 * @param cls Closure (tunnel info)
975 * @param key GNUNET_PeerIdentity of the peer (unused)
976 * @param value PeerInfo of the peer
978 * @return always GNUNET_YES, to keep iterating
981 peer_info_delete_tunnel (void* cls, const GNUNET_HashCode* key, void* value)
983 struct MeshTunnel *t = cls;
984 struct MeshPeerInfo *peer = value;
987 for (i = 0; i < peer->ntunnels; i++)
989 if (0 == memcmp(&peer->tunnels[i]->id,
991 sizeof(struct MESH_TunnelID)))
994 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
995 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1004 * Sends a CREATE PATH message for a path to a peer, properly registrating
1005 * all used resources.
1007 * @param peer PeerInfo of the final peer for whom this path is being created.
1008 * @param p Path itself.
1009 * @param t Tunnel for which the path is created.
1012 send_create_path (struct MeshPeerInfo *peer,
1013 struct MeshPeerPath *p,
1014 struct MeshTunnel *t)
1016 struct GNUNET_PeerIdentity id;
1017 struct MeshPathInfo *path_info;
1018 struct MeshPeerInfo *neighbor;
1023 p = tree_get_path_to_peer(t->tree, peer->id);
1030 for (i = 0; i < p->length; i++)
1032 if (p->peers[i] == myid)
1035 if (i >= p->length - 1)
1041 GNUNET_PEER_resolve(p->peers[i + 1], &id);
1043 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1044 path_info->path = p;
1046 neighbor = peer_info_get(&id);
1047 path_info->peer = neighbor;
1048 path_info->pos = peer_info_transmit_slot(neighbor);
1049 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1050 neighbor->infos[path_info->pos] = path_info;
1051 neighbor->core_transmit[path_info->pos] =
1052 GNUNET_CORE_notify_transmit_ready (
1053 core_handle, /* handle */
1056 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1058 sizeof (struct GNUNET_MESH_ManipulatePath)
1059 + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1060 &send_core_create_path, /* callback */
1061 path_info); /* cls */
1065 * Core callback to write a
1067 * @param cls Closure (MeshDataDescriptor with data in "data" member).
1068 * @param size Number of bytes available in buf.
1069 * @param buf Where the to write the message.
1071 * @return number of bytes written to buf
1074 send_core_data_raw (void *cls, size_t size, void *buf)
1076 struct MeshDataDescriptor *info = cls;
1077 struct GNUNET_MessageHeader *msg;
1080 GNUNET_assert (NULL != info);
1081 GNUNET_assert (NULL != info->data);
1082 msg = (struct GNUNET_MessageHeader *) info->data;
1083 total_size = ntohs (msg->size);
1085 if (total_size > size)
1087 struct GNUNET_PeerIdentity id;
1089 GNUNET_PEER_resolve (info->peer->id, &id);
1090 info->peer->core_transmit[info->handler_n] =
1091 GNUNET_CORE_notify_transmit_ready(core_handle,
1094 GNUNET_TIME_UNIT_FOREVER_REL,
1097 &send_core_data_raw,
1101 info->peer->core_transmit[info->handler_n] = NULL;
1102 memcpy (buf, msg, total_size);
1103 GNUNET_free (info->data);
1110 * Sends an already built message to a peer, properly registrating
1111 * all used resources.
1113 * @param message Message to send.
1114 * @param peer Short ID of the neighbor whom to send the message.
1117 send_message (const struct GNUNET_MessageHeader *message,
1118 const struct GNUNET_PeerIdentity *peer)
1120 struct MeshDataDescriptor *info;
1121 struct MeshPeerInfo *neighbor;
1125 size = ntohs (message->size);
1126 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1127 info->data = GNUNET_malloc (size);
1128 memcpy (info->data, message, size);
1129 neighbor = peer_info_get (peer);
1130 i = peer_info_transmit_slot (neighbor);
1131 info->handler_n = i;
1132 info->peer = neighbor;
1133 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1134 neighbor->infos[i] = info;
1135 neighbor->core_transmit[i] =
1136 GNUNET_CORE_notify_transmit_ready(core_handle,
1139 GNUNET_TIME_UNIT_FOREVER_REL,
1142 &send_core_data_raw,
1149 * Try to establish a new connection to this peer.
1150 * Use the best path for the given tunnel.
1151 * If the peer doesn't have any path to it yet, try to get one.
1152 * If the peer already has some path, send a CREATE PATH towards it.
1154 * @param peer PeerInfo of the peer.
1155 * @param t Tunnel for which to create the path, if possible.
1158 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1160 struct MeshPeerPath *p;
1161 struct MeshPathInfo *path_info;
1163 if (NULL != peer->path_head)
1165 p = tree_get_path_to_peer(t->tree, peer->id);
1168 send_create_path(peer, p, t);
1173 send_client_peer_connected(t, myid);
1176 else if (NULL == peer->dhtget)
1178 struct GNUNET_PeerIdentity id;
1180 GNUNET_PEER_resolve(peer->id, &id);
1181 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1182 path_info->peer = peer;
1185 GNUNET_DHT_get_start(dht_handle, /* handle */
1186 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1187 GNUNET_BLOCK_TYPE_TEST, /* type */
1188 &id.hashPubKey, /* key to search */
1189 4, /* replication level */
1190 GNUNET_DHT_RO_RECORD_ROUTE |
1191 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1193 0, /* xquery bits */
1194 &dht_get_id_handler,
1197 /* Otherwise, there is no path but the DHT get is already started. */
1203 * Destroy the peer_info and free any allocated resources linked to it
1204 * @param t tunnel the path belongs to
1205 * @param pi the peer_info to destroy
1206 * @return GNUNET_OK on success
1209 peer_info_destroy (struct MeshPeerInfo *pi)
1211 GNUNET_HashCode hash;
1212 struct GNUNET_PeerIdentity id;
1214 GNUNET_PEER_resolve (pi->id, &id);
1215 GNUNET_PEER_change_rc (pi->id, -1);
1216 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
1218 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
1219 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
1227 * Notify a tunnel that a connection has broken that affects at least
1228 * some of its peers.
1230 * @param t Tunnel affected.
1231 * @param peer Peer that (at least) has been affected by the disconnection.
1232 * @param p1 Peer that got disconnected from p2.
1233 * @param p2 Peer that got disconnected from p1.
1235 * @return Short ID of the peer disconnected (either p1 or p2).
1236 * 0 if the tunnel remained unaffected.
1238 static GNUNET_PEER_Id
1239 tunnel_notify_connection_broken (struct MeshTunnel *t,
1240 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1244 * Remove all paths that rely on a direct connection between p1 and p2
1245 * from the peer itself and notify all tunnels about it.
1247 * @param peer PeerInfo of affected peer.
1248 * @param p1 GNUNET_PEER_Id of one peer.
1249 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1252 * TODO: optimize (see below)
1255 path_remove_from_peer (struct MeshPeerInfo *peer,
1259 struct GNUNET_PeerIdentity id;
1260 struct MeshPeerPath *p;
1261 struct MeshPeerPath *aux;
1262 struct MeshPeerInfo *peer_d;
1264 unsigned int destroyed;
1270 p = peer->path_head;
1274 for (i = 0; i < (p->length - 1); i++)
1276 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1277 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1289 for (i = 0; i < peer->ntunnels; i++)
1291 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
1293 * Problem: one or more peers have been deleted from the tunnel tree.
1294 * We don't know who they are to try to add them again.
1295 * We need to try to find a new path for each of the disconnected peers.
1296 * Some of them might already have a path to reach them that does not
1297 * involve p1 and p2. Adding all anew might render in a better tree than
1298 * the trivial immediate fix.
1300 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1301 * its children will be reachable trough him.
1303 peer_d = peer_info_get_short(d);
1306 for (p = peer_d->path_head; NULL != p; p = p->next)
1308 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
1316 /* No callback, as peer will be already disconnected */
1317 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
1321 struct MeshPathInfo *path_info;
1323 if (NULL != peer_d->dhtget)
1325 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1326 path_info->path = p;
1327 path_info->peer = peer_d;
1328 path_info->t = peer->tunnels[i];
1330 GNUNET_DHT_get_start(dht_handle, /* handle */
1331 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1332 GNUNET_BLOCK_TYPE_TEST, /* type */
1333 &id.hashPubKey, /*key to search */
1334 4, /* replication level */
1335 GNUNET_DHT_RO_RECORD_ROUTE |
1336 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1338 0, /* xquery bits */
1339 &dht_get_id_handler,
1340 (void *) path_info);
1347 * Add the path to the peer and update the path used to reach it in case this
1350 * @param peer_info Destination peer to add the path to.
1351 * @param path New path to add. Last peer must be the peer in arg 1.
1352 * Path will be either used of freed if already known.
1354 * TODO: trim the part from origin to us? Add it as path to origin?
1357 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1359 struct MeshPeerPath *aux;
1363 if (NULL == peer_info || NULL == path)
1369 l = path_get_length (path);
1376 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1378 l2 = path_get_length (aux);
1381 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1382 peer_info->path_tail, aux, path);
1386 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1393 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1400 * Add the path to the origin peer and update the path used to reach it in case
1401 * this is the shortest.
1402 * The path is given in peer_info -> destination, therefore we turn the path
1403 * upside down first.
1405 * @param peer_info Peer to add the path to, being the origin of the path.
1406 * @param path New path to add after being inversed.
1409 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1412 path_add_to_peer (peer_info, path);
1417 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1418 * to obtain a local peer -> destination path and interning the peer ids.
1420 * @return Newly allocated and created path
1422 static struct MeshPeerPath *
1423 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1424 unsigned int get_path_length,
1425 const struct GNUNET_PeerIdentity *put_path,
1426 unsigned int put_path_length)
1428 struct MeshPeerPath *p;
1434 GNUNET_PEER_change_rc(myid, 1);
1435 i = get_path_length;
1436 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1437 for (i--; i >= 0; i--)
1439 id = GNUNET_PEER_intern (&get_path[i]);
1440 if (p->length > 0 && id == p->peers[p->length - 1])
1442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1443 GNUNET_PEER_change_rc(id, -1);
1447 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1448 "MESH: Adding from GET: %s.\n",
1449 GNUNET_i2s(&get_path[i]));
1451 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1452 p->peers[p->length - 1] = id;
1455 i = put_path_length;
1456 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1457 for (i--; i >= 0; i--)
1459 id = GNUNET_PEER_intern (&put_path[i]);
1462 /* PUT path went through us, so discard the path up until now and start
1463 * from here to get a much shorter (and loop-free) path.
1468 if (p->length > 0 && id == p->peers[p->length - 1])
1470 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1471 GNUNET_PEER_change_rc(id, -1);
1475 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1476 "MESH: Adding from PUT: %s.\n",
1477 GNUNET_i2s(&put_path[i]));
1479 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1480 p->peers[p->length - 1] = id;
1484 if (get_path_length > 0)
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1486 "MESH: (first of GET: %s)\n",
1487 GNUNET_h2s_full(&get_path[0].hashPubKey));
1488 if (put_path_length > 0)
1489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1490 "MESH: (first of PUT: %s)\n",
1491 GNUNET_h2s_full(&put_path[0].hashPubKey));
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1493 "MESH: In total: %d hops\n",
1495 for (i = 0; i < p->length; i++)
1497 struct GNUNET_PeerIdentity peer_id;
1499 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1503 GNUNET_h2s_full(&peer_id.hashPubKey));
1511 * Send keepalive packets for a peer
1513 * @param cls Closure (tunnel for which to send the keepalive).
1514 * @param tc Notification context.
1516 * TODO: implement explicit multicast keepalive?
1519 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1523 * Search for a tunnel among the tunnels for a client
1525 * @param c the client whose tunnels to search in
1526 * @param tid the local id of the tunnel
1528 * @return tunnel handler, NULL if doesn't exist
1530 static struct MeshTunnel *
1531 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1533 GNUNET_HashCode hash;
1535 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1536 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1538 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1540 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1545 * Search for a tunnel by global ID using PEER_ID
1547 * @param pi owner of the tunnel
1548 * @param tid global tunnel number
1550 * @return tunnel handler, NULL if doesn't exist
1552 static struct MeshTunnel *
1553 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1555 struct MESH_TunnelID id;
1556 GNUNET_HashCode hash;
1561 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1562 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1567 * Search for a tunnel by global ID using full PeerIdentities
1569 * @param oid owner of the tunnel
1570 * @param tid global tunnel number
1572 * @return tunnel handler, NULL if doesn't exist
1574 static struct MeshTunnel *
1575 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1577 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1582 * Callback used to notify a client owner of a tunnel that a peer has
1583 * disconnected, most likely because of a path change.
1585 * @param n Node in the tree representing the disconnected peer
1588 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1590 struct MeshPeerInfo *peer;
1592 if (NULL != n->t->client && NULL != nc)
1594 struct GNUNET_MESH_PeerControl msg;
1595 msg.header.size = htons (sizeof (msg));
1596 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1597 msg.tunnel_id = htonl (n->t->local_tid);
1598 GNUNET_PEER_resolve (n->peer, &msg.peer);
1599 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1600 &msg.header, GNUNET_NO);
1602 peer = peer_info_get_short(n->peer);
1603 peer_info_connect(peer, n->t);
1608 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1609 * needed rescources.
1610 * If peer already exists, reevaluate shortest path and change if different.
1612 * @param t Tunnel we want to add a new peer to
1613 * @param peer PeerInfo of the peer being added
1617 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1619 struct GNUNET_PeerIdentity id;
1620 struct MeshPeerPath *best_p;
1621 struct MeshPeerPath *p;
1622 unsigned int best_cost;
1625 GNUNET_PEER_resolve(peer->id, &id);
1627 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1630 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1631 GNUNET_CONTAINER_multihashmap_put(
1635 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1638 if (NULL != (p = peer->path_head))
1641 best_cost = path_get_cost(t->tree, p);
1644 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1651 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1652 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1653 t->path_refresh_task =
1654 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1658 /* Start a DHT get if necessary */
1659 peer_info_connect(peer, t);
1664 * Add a path to a tunnel which we don't own, just to remember the next hop.
1665 * If destination node was already in the tunnel, the first hop information
1666 * will be replaced with the new path.
1668 * @param t Tunnel we want to add a new peer to
1669 * @param p Path to add
1670 * @param own_pos Position of local node in path.
1674 tunnel_add_path (struct MeshTunnel *t,
1675 struct MeshPeerPath *p,
1676 unsigned int own_pos)
1678 struct GNUNET_PeerIdentity id;
1680 GNUNET_assert (0 != own_pos);
1681 tree_add_path(t->tree, p, NULL);
1682 if (NULL == t->tree->me)
1683 t->tree->me = tree_find_peer(t->tree->root, p->peers[own_pos]);
1684 if (own_pos < p->length - 1)
1686 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1687 tree_update_first_hops(t->tree, t->tree->me, &id);
1693 * Notify a tunnel that a connection has broken that affects at least
1694 * some of its peers.
1696 * @param t Tunnel affected.
1697 * @param peer Peer that (at least) has been affected by the disconnection.
1698 * @param p1 Peer that got disconnected from p2.
1699 * @param p2 Peer that got disconnected from p1.
1701 * @return Short ID of the peer disconnected (either p1 or p2).
1702 * 0 if the tunnel remained unaffected.
1704 static GNUNET_PEER_Id
1705 tunnel_notify_connection_broken (struct MeshTunnel *t,
1706 struct MeshPeerInfo *peer,
1710 return tree_notify_connection_broken (t->tree, p1, p2,
1711 ¬ify_peer_disconnected);
1716 * Send a message in a tunnel in multicast, sending a copy to each child node
1717 * down the local one in the tunnel tree.
1719 * @param t Tunnel in which to send the data.
1720 * @param msg Message to be sent
1722 * @return Number of copies sent.
1725 tunnel_send_multicast (struct MeshTunnel *t,
1726 const struct GNUNET_MessageHeader *msg)
1728 struct GNUNET_PeerIdentity neighbor;
1729 struct MeshDataDescriptor *info;
1730 struct MeshTunnelTreeNode *n;
1731 unsigned int *copies;
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1737 "MESH: sending a multicast packet...\n");
1738 size = ntohs (msg->size);
1739 GNUNET_assert (NULL != t->tree->me);
1740 n = t->tree->me->children_head;
1743 copies = GNUNET_malloc (sizeof (unsigned int));
1744 for (*copies = 0; NULL != n; n = n->next)
1746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (%u copies)\n", *copies);
1747 n = t->tree->me->children_head;
1748 data = GNUNET_malloc (size);
1749 memcpy (data, msg, size);
1752 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1755 info->copies = copies;
1756 if (NULL != t->client)
1758 info->client = t->client->handle;
1759 info->timeout_task = GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT,
1763 info->destination = n->peer;
1764 GNUNET_PEER_resolve (n->peer, &neighbor);
1765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1766 "MESH: sending to %s...\n",
1767 GNUNET_i2s (&neighbor));
1768 info->peer = peer_info_get(&neighbor);
1769 GNUNET_assert (NULL != info->peer);
1770 i = peer_info_transmit_slot(info->peer);
1771 info->handler_n = i;
1772 info->peer->infos[i] = info;
1773 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1774 info->peer->core_transmit[i] =
1775 GNUNET_CORE_notify_transmit_ready (core_handle,
1778 GNUNET_TIME_UNIT_FOREVER_REL,
1781 &send_core_data_multicast, info);
1789 * Send a message to all peers in this tunnel that the tunnel is no longer
1792 * @param t The tunnel whose peers to notify.
1795 tunnel_send_destroy (struct MeshTunnel *t)
1797 struct GNUNET_MESH_TunnelDestroy msg;
1799 msg.header.size = htons (sizeof (msg));
1800 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
1801 msg.oid = my_full_id;
1802 msg.tid = htonl (t->id.tid);
1803 tunnel_send_multicast (t, &msg.header);
1809 * Destroy the tunnel and free any allocated resources linked to it
1811 * @param t the tunnel to destroy
1813 * @return GNUNET_OK on success
1816 tunnel_destroy (struct MeshTunnel *t)
1818 struct MeshClient *c;
1819 struct MeshQueue *q;
1820 struct MeshQueue *qn;
1821 GNUNET_HashCode hash;
1830 struct GNUNET_PeerIdentity id;
1832 GNUNET_PEER_resolve(t->id.oid, &id);
1833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1834 "MESH: destroying tunnel %s [%x]\n",
1838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1842 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1843 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1848 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1850 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1854 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1856 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1857 GNUNET_break (GNUNET_YES ==
1858 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t));
1861 if (NULL != t->peers)
1863 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
1864 &peer_info_delete_tunnel, t);
1865 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1870 if (NULL != q->data)
1871 GNUNET_free (q->data);
1875 /* TODO cancel core transmit ready in case it was active */
1877 tree_destroy(t->tree);
1878 if (NULL != t->dht_get_type)
1879 GNUNET_DHT_get_stop(t->dht_get_type);
1880 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
1881 GNUNET_SCHEDULER_cancel(t->timeout_task);
1882 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
1883 GNUNET_SCHEDULER_cancel(t->path_refresh_task);
1890 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1891 * that are no longer needed, as well as nodes of no longer reachable peers.
1892 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1894 * @param t Tunnel from which to remove the path.
1895 * @param p Path which should be removed.
1898 tunnel_delete_path (struct MeshTunnel *t,
1899 struct MeshPeerPath *p)
1901 GNUNET_break (GNUNET_OK ==
1902 tree_del_peer (t->tree,
1903 p->peers[p->length - 1],
1904 ¬ify_peer_disconnected));
1905 if (NULL == t->tree->root)
1911 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1912 * client when the client disconnects.
1914 * @param cls closure (client that is disconnecting)
1915 * @param key the hash of the local tunnel id (used to access the hashmap)
1916 * @param value the value stored at the key (tunnel to destroy)
1918 * @return GNUNET_OK on success
1921 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1923 struct MeshTunnel *t = value;
1926 if (NULL != t->dht_get_type)
1928 GNUNET_DHT_get_stop (t->dht_get_type);
1930 r = tunnel_destroy (t);
1936 * Timeout function, destroys tunnel if called
1938 * @param cls Closure (tunnel to destroy).
1939 * @param tc TaskContext
1942 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1944 struct MeshTunnel *t = cls;
1946 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
1948 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1953 * Resets the tunnel timeout. Starts it if no timeout was running.
1955 * @param t Tunnel whose timeout to reset.
1958 tunnel_reset_timeout (struct MeshTunnel *t)
1960 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
1961 GNUNET_SCHEDULER_cancel (t->timeout_task);
1962 t->timeout_task = GNUNET_SCHEDULER_add_delayed (
1963 GNUNET_TIME_relative_multiply(REFRESH_PATH_TIME, 4),
1969 /******************************************************************************/
1970 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1971 /******************************************************************************/
1974 * Function called to notify a client about the socket
1975 * being ready to queue more data. "buf" will be
1976 * NULL and "size" zero if the socket was closed for
1977 * writing in the meantime.
1979 * @param cls closure
1980 * @param size number of bytes available in buf
1981 * @param buf where the callee should write the message
1982 * @return number of bytes written to buf
1985 send_core_create_path (void *cls, size_t size, void *buf)
1987 struct MeshPathInfo *info = cls;
1988 struct GNUNET_MESH_ManipulatePath *msg;
1989 struct GNUNET_PeerIdentity *peer_ptr;
1990 struct MeshPeerInfo *peer = info->peer;
1991 struct MeshTunnel *t = info->t;
1992 struct MeshPeerPath *p = info->path;
1996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1997 "MESH: CREATE PATH sending...\n");
1999 sizeof (struct GNUNET_MESH_ManipulatePath) +
2000 p->length * sizeof (struct GNUNET_PeerIdentity);
2002 if (size < size_needed || NULL == buf)
2004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
2008 info->peer->core_transmit[info->pos] =
2009 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2010 GNUNET_TIME_UNIT_FOREVER_REL,
2011 path_get_first_hop (t->tree, peer->id),
2013 &send_core_create_path,
2017 info->peer->core_transmit[info->pos] = NULL;
2020 struct GNUNET_PeerIdentity id;
2022 GNUNET_PEER_resolve (peer->id, &id);
2023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2024 "MESH: setting core_transmit %s [%u] to NULL\n",
2029 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2030 msg->header.size = htons (size_needed);
2031 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2032 msg->tid = ntohl (t->id.tid);
2034 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2035 for (i = 0; i < p->length; i++)
2037 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2044 "MESH: CREATE PATH (%u bytes long) sent!\n",
2051 * Function called to notify a client about the socket
2052 * being ready to queue more data. "buf" will be
2053 * NULL and "size" zero if the socket was closed for
2054 * writing in the meantime.
2056 * @param cls closure (data itself)
2057 * @param size number of bytes available in buf
2058 * @param buf where the callee should write the message
2060 * @return number of bytes written to buf
2063 send_core_data_multicast (void *cls, size_t size, void *buf)
2065 struct MeshDataDescriptor *info = cls;
2068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2069 GNUNET_assert (NULL != info);
2070 GNUNET_assert (NULL != info->peer);
2071 total_size = info->size;
2072 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2074 if (total_size > size)
2077 struct GNUNET_PeerIdentity id;
2079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2080 "MESH: Multicast: retransmitting... (%u/%u)\n",
2082 GNUNET_PEER_resolve(info->peer->id, &id);
2083 info->peer->core_transmit[info->handler_n] =
2084 GNUNET_CORE_notify_transmit_ready (core_handle,
2087 GNUNET_TIME_UNIT_FOREVER_REL,
2090 &send_core_data_multicast,
2094 info->peer->core_transmit[info->handler_n] = NULL;
2095 info->peer->infos[info->handler_n] = NULL;
2096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2097 memcpy (buf, info->data, total_size);
2100 struct GNUNET_MESH_Multicast *mc;
2101 struct GNUNET_MessageHeader *mh;
2104 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2106 mc = (struct GNUNET_MESH_Multicast *) mh;
2107 mh = (struct GNUNET_MessageHeader *) &mc[1];
2108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2109 "MESH: multicast, payload type %u\n",
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2112 "MESH: multicast, payload size %u\n",
2117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2123 if (0 == --(*info->copies))
2125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
2126 if (NULL != info->client)
2128 if (GNUNET_SCHEDULER_NO_TASK != info->timeout_task)
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131 "MESH: cancelling client timeout (%u)...\n",
2132 info->timeout_task);
2133 GNUNET_SCHEDULER_cancel(info->timeout_task);
2134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
2135 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing memory...\n");
2139 GNUNET_free (info->data);
2140 GNUNET_free (info->copies);
2142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2150 * Function called to notify a client about the socket
2151 * being ready to queue more data. "buf" will be
2152 * NULL and "size" zero if the socket was closed for
2153 * writing in the meantime.
2155 * @param cls closure (MeshDataDescriptor)
2156 * @param size number of bytes available in buf
2157 * @param buf where the callee should write the message
2158 * @return number of bytes written to buf
2161 send_core_path_ack (void *cls, size_t size, void *buf)
2163 struct MeshDataDescriptor *info = cls;
2164 struct GNUNET_MESH_PathACK *msg = buf;
2166 GNUNET_assert (NULL != info);
2169 info->peer->core_transmit[info->handler_n] = NULL;
2171 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2176 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2177 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2178 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2179 msg->tid = htonl (info->origin->tid);
2180 msg->peer_id = my_full_id;
2182 /* TODO add signature */
2184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2185 return sizeof (struct GNUNET_MESH_PathACK);
2191 * Send another peer a notification to destroy a tunnel
2192 * @param cls The tunnel to destroy
2193 * @param size Size in the buffer
2194 * @param buf Memory where to put the data to transmit
2195 * @return Size of data put in buffer
2198 send_core_tunnel_destroy (void *cls, size_t size, void *buf)
2200 struct MeshTunnel *t = cls;
2201 struct MeshClient *c;
2202 struct GNUNET_MESH_TunnelMessage *msg;
2206 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2207 /*FIXME*/ msg->header.size =
2208 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
2209 msg->tunnel_id = htonl (t->id.tid);
2211 tunnel_destroy (c, t);
2212 return sizeof (struct GNUNET_MESH_TunnelMessage);
2217 /******************************************************************************/
2218 /******************** MESH NETWORK HANDLERS **************************/
2219 /******************************************************************************/
2223 * Core handler for path creation
2225 * @param cls closure
2226 * @param message message
2227 * @param peer peer identity this notification is about
2228 * @param atsi performance data
2230 * @return GNUNET_OK to keep the connection open,
2231 * GNUNET_SYSERR to close it (signal serious error)
2234 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2235 const struct GNUNET_MessageHeader *message,
2236 const struct GNUNET_ATS_Information *atsi)
2238 unsigned int own_pos;
2241 MESH_TunnelNumber tid;
2242 struct GNUNET_MESH_ManipulatePath *msg;
2243 struct GNUNET_PeerIdentity *pi;
2244 GNUNET_HashCode hash;
2245 struct MeshPeerPath *path;
2246 struct MeshPeerInfo *dest_peer_info;
2247 struct MeshPeerInfo *orig_peer_info;
2248 struct MeshTunnel *t;
2250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2251 "MESH: Received a path create msg [%s]\n",
2252 GNUNET_i2s(&my_full_id));
2253 size = ntohs (message->size);
2254 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2256 GNUNET_break_op (0);
2260 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2261 if (size % sizeof (struct GNUNET_PeerIdentity))
2263 GNUNET_break_op (0);
2266 size /= sizeof (struct GNUNET_PeerIdentity);
2269 GNUNET_break_op (0);
2272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2273 "MESH: path has %u hops.\n",
2275 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2277 tid = ntohl (msg->tid);
2278 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2279 t = tunnel_get (pi, tid);
2280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2281 "MESH: path is for tunnel %s [%X].\n",
2286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2287 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2288 t->id.oid = GNUNET_PEER_intern (pi);
2290 t->local_tid = next_local_tid++;
2291 /* FIXME test if taken */
2292 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2293 t->tree = tree_new(t, t->id.oid);
2295 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2297 GNUNET_CONTAINER_multihashmap_put (
2301 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2307 tunnel_reset_timeout (t);
2308 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2310 GNUNET_CONTAINER_multihashmap_put (
2314 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2322 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2323 if (NULL == dest_peer_info)
2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2326 "MESH: Creating PeerInfo for destination.\n");
2327 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2328 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2329 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2331 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2333 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2334 if (NULL == orig_peer_info)
2336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2337 "MESH: Creating PeerInfo for origin.\n");
2338 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2339 orig_peer_info->id = GNUNET_PEER_intern (pi);
2340 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2341 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2344 path = path_new (size);
2346 for (i = 0; i < size; i++)
2348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2349 "MESH: ... adding %s\n",
2350 GNUNET_i2s(&pi[i]));
2351 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2352 if (path->peers[i] == myid)
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2356 "MESH: Own position: %u\n", own_pos);
2359 /* cannot be self, must be 'not found' */
2360 /* create path: self not found in path through self */
2361 GNUNET_break_op (0);
2362 path_destroy (path);
2363 /* FIXME error. destroy tunnel? leave for timeout? */
2366 tunnel_add_path (t, path, own_pos);
2367 t->tree->me = tree_find_peer(t->tree->root, myid);
2368 if (own_pos == size - 1)
2370 /* It is for us! Send ack. */
2371 struct GNUNET_MESH_TunnelNotification cmsg;
2372 struct MeshDataDescriptor *info;
2375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2376 "MESH: It's for us!\n");
2377 path_add_to_origin (orig_peer_info, path);
2378 t->peers = GNUNET_CONTAINER_multihashmap_create(4);
2379 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2381 &my_full_id.hashPubKey,
2382 peer_info_get(&my_full_id),
2383 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2384 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2385 info->origin = &t->id;
2386 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2387 GNUNET_assert (NULL != info->peer);
2388 j = peer_info_transmit_slot(info->peer);
2389 info->handler_n = j;
2390 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2391 info->peer->infos[j] = info;
2392 info->peer->core_transmit[j] =
2393 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2394 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2395 sizeof (struct GNUNET_MESH_PathACK),
2396 &send_core_path_ack, info);
2397 cmsg.header.size = htons(sizeof(cmsg));
2398 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2399 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2400 cmsg.tunnel_id = htonl(t->local_tid);
2401 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2405 struct MeshPeerPath *path2;
2407 /* It's for somebody else! Retransmit. */
2408 path2 = path_duplicate(path);
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410 "MESH: Retransmitting.\n");
2411 path_add_to_peer(dest_peer_info, path);
2412 path = path_duplicate(path2);
2413 path_add_to_origin(orig_peer_info, path2);
2414 send_create_path(dest_peer_info, path, t);
2421 * Core handler for path destruction
2423 * @param cls closure
2424 * @param message message
2425 * @param peer peer identity this notification is about
2426 * @param atsi performance data
2428 * @return GNUNET_OK to keep the connection open,
2429 * GNUNET_SYSERR to close it (signal serious error)
2432 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2433 const struct GNUNET_MessageHeader *message,
2434 const struct GNUNET_ATS_Information *atsi)
2436 struct GNUNET_MESH_ManipulatePath *msg;
2437 struct GNUNET_PeerIdentity *pi;
2438 struct MeshPeerPath *path;
2439 struct MeshTunnel *t;
2440 unsigned int own_pos;
2444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2445 "MESH: Received a PATH DESTROY msg from %s\n",
2447 size = ntohs (message->size);
2448 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2450 GNUNET_break_op (0);
2454 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2455 if (size % sizeof (struct GNUNET_PeerIdentity))
2457 GNUNET_break_op (0);
2460 size /= sizeof (struct GNUNET_PeerIdentity);
2463 GNUNET_break_op (0);
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2467 "MESH: path has %u hops.\n",
2470 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2471 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2473 "MESH: path is for tunnel %s [%X].\n",
2476 t = tunnel_get (pi, ntohl (msg->tid));
2479 /* TODO notify back: we don't know this tunnel */
2480 GNUNET_break_op (0);
2483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2484 path = path_new (size);
2486 for (i = 0; i < size; i++)
2488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2489 "MESH: ... adding %s\n",
2490 GNUNET_i2s(&pi[i]));
2491 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2492 if (path->peers[i] == myid)
2495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2496 "MESH: Own position: %u\n", own_pos);
2497 if (own_pos < path->length - 1)
2498 send_message (message, &pi[own_pos + 1]);
2499 tunnel_delete_path (t, path);
2505 * Core handler for tunnel destruction
2507 * @param cls closure
2508 * @param message message
2509 * @param peer peer identity this notification is about
2510 * @param atsi performance data
2512 * @return GNUNET_OK to keep the connection open,
2513 * GNUNET_SYSERR to close it (signal serious error)
2516 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2517 const struct GNUNET_MessageHeader *message,
2518 const struct GNUNET_ATS_Information *atsi)
2520 struct GNUNET_MESH_TunnelDestroy *msg;
2521 struct MeshTunnel *t;
2523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2524 "MESH: Got a TUNNEL DESTROY packet from %s\n",
2526 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2527 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2530 /* TODO notify back: we don't know this tunnel */
2531 GNUNET_break_op (0);
2534 tunnel_send_destroy (t);
2541 * Core handler for mesh network traffic going from the origin to a peer
2543 * @param cls closure
2544 * @param peer peer identity this notification is about
2545 * @param message message
2546 * @param atsi performance data
2547 * @return GNUNET_OK to keep the connection open,
2548 * GNUNET_SYSERR to close it (signal serious error)
2551 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2552 const struct GNUNET_MessageHeader *message,
2553 const struct GNUNET_ATS_Information *atsi)
2555 struct GNUNET_MESH_Unicast *msg;
2556 struct MeshTunnel *t;
2560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2561 "MESH: got a unicast packet from %s\n",
2563 size = ntohs (message->size);
2565 sizeof (struct GNUNET_MESH_Unicast) +
2566 sizeof (struct GNUNET_MessageHeader))
2571 msg = (struct GNUNET_MESH_Unicast *) message;
2572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2573 "MESH: of type %u\n",
2574 ntohs (msg[1].header.type));
2575 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2578 /* TODO notify back: we don't know this tunnel */
2579 GNUNET_break_op (0);
2582 tunnel_reset_timeout (t);
2583 pid = GNUNET_PEER_search(&msg->destination);
2586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2587 "MESH: it's for us! sending to clients...\n");
2588 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2592 "MESH: not for us, retransmitting...\n");
2593 send_message (message, path_get_first_hop(t->tree, pid));
2599 * Core handler for mesh network traffic going from the origin to all peers
2601 * @param cls closure
2602 * @param message message
2603 * @param peer peer identity this notification is about
2604 * @param atsi performance data
2605 * @return GNUNET_OK to keep the connection open,
2606 * GNUNET_SYSERR to close it (signal serious error)
2608 * TODO: Check who we got this from, to validate route.
2611 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2612 const struct GNUNET_MessageHeader *message,
2613 const struct GNUNET_ATS_Information *atsi)
2615 struct GNUNET_MESH_Multicast *msg;
2616 struct MeshTunnel *t;
2619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2620 "MESH: got a multicast packet from %s\n",
2622 size = ntohs (message->size);
2623 if (sizeof (struct GNUNET_MESH_Multicast) +
2624 sizeof (struct GNUNET_MessageHeader) > size)
2626 GNUNET_break_op (0);
2629 msg = (struct GNUNET_MESH_Multicast *) message;
2630 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2634 /* TODO notify that we dont know that tunnel */
2635 GNUNET_break_op (0);
2638 tunnel_reset_timeout (t);
2640 /* Transmit to locally interested clients */
2641 if (NULL != t->peers &&
2642 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2644 send_subscribed_clients (message, &msg[1].header);
2646 tunnel_send_multicast(t, message);
2652 * Core handler for mesh network traffic toward the owner of a tunnel
2654 * @param cls closure
2655 * @param message message
2656 * @param peer peer identity this notification is about
2657 * @param atsi performance data
2659 * @return GNUNET_OK to keep the connection open,
2660 * GNUNET_SYSERR to close it (signal serious error)
2663 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2664 const struct GNUNET_MessageHeader *message,
2665 const struct GNUNET_ATS_Information *atsi)
2667 struct GNUNET_MESH_ToOrigin *msg;
2668 struct GNUNET_PeerIdentity id;
2669 struct MeshPeerInfo *peer_info;
2670 struct MeshTunnel *t;
2673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2674 "MESH: got a ToOrigin packet from %s\n",
2676 size = ntohs (message->size);
2677 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2678 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2680 GNUNET_break_op (0);
2683 msg = (struct GNUNET_MESH_ToOrigin *) message;
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2685 "MESH: of type %u\n",
2686 ntohs (msg[1].header.type));
2687 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2691 /* TODO notify that we dont know this tunnel (whom)? */
2692 GNUNET_break_op (0);
2696 if (t->id.oid == myid)
2699 struct GNUNET_MESH_ToOrigin *copy;
2701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2702 "MESH: it's for us! sending to clients...\n");
2703 if (NULL == t->client)
2705 /* got data packet for ownerless tunnel */
2706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2707 "MESH: no clients!\n");
2708 GNUNET_break_op (0);
2711 /* TODO signature verification */
2712 memcpy (cbuf, message, size);
2713 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2714 copy->tid = htonl (t->local_tid);
2715 GNUNET_SERVER_notification_context_unicast (nc,
2721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2722 "MESH: not for us, retransmitting...\n");
2724 peer_info = peer_info_get (&msg->oid);
2725 if (NULL == peer_info)
2727 /* unknown origin of tunnel */
2731 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
2732 send_message (message, &id);
2739 * Core handler for path ACKs
2741 * @param cls closure
2742 * @param message message
2743 * @param peer peer identity this notification is about
2744 * @param atsi performance data
2746 * @return GNUNET_OK to keep the connection open,
2747 * GNUNET_SYSERR to close it (signal serious error)
2750 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2751 const struct GNUNET_MessageHeader *message,
2752 const struct GNUNET_ATS_Information *atsi)
2754 struct GNUNET_MESH_PathACK *msg;
2755 struct GNUNET_PeerIdentity id;
2756 struct MeshTunnelTreeNode *n;
2757 struct MeshPeerInfo *peer_info;
2758 struct MeshTunnel *t;
2760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2761 "MESH: Received a path ACK msg [%s]\n",
2762 GNUNET_i2s(&my_full_id));
2763 msg = (struct GNUNET_MESH_PathACK *) message;
2764 t = tunnel_get (&msg->oid, msg->tid);
2767 /* TODO notify that we don't know the tunnel */
2771 /* Message for us? */
2772 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
2775 if (NULL == t->client)
2777 GNUNET_break_op (0);
2780 peer_info = peer_info_get (&msg->peer_id);
2781 if (NULL == peer_info)
2783 GNUNET_break_op (0);
2786 n = tree_find_peer(t->tree->root, peer_info->id);
2789 GNUNET_break_op (0);
2792 n->status = MESH_PEER_READY;
2793 send_client_peer_connected(t, peer_info->id);
2797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2798 "MESH: not for us, retransmitting...\n");
2799 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
2800 peer_info = peer_info_get (&msg->oid);
2801 if (NULL == peer_info)
2803 /* If we know the tunnel, we should DEFINITELY know the peer */
2807 send_message (message, &id);
2813 * Functions to handle messages from core
2815 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2816 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2817 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
2818 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
2819 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2820 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2821 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2822 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2823 sizeof (struct GNUNET_MESH_PathACK)},
2829 /******************************************************************************/
2830 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2831 /******************************************************************************/
2834 * deregister_app: iterator for removing each application registered by a client
2836 * @param cls closure
2837 * @param key the hash of the application id (used to access the hashmap)
2838 * @param value the value stored at the key (client)
2840 * @return GNUNET_OK on success
2843 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2845 GNUNET_break (GNUNET_YES ==
2846 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
2852 * notify_client_connection_failure: notify a client that the connection to the
2853 * requested remote peer is not possible (for instance, no route found)
2854 * Function called when the socket is ready to queue more data. "buf" will be
2855 * NULL and "size" zero if the socket was closed for writing in the meantime.
2857 * @param cls closure
2858 * @param size number of bytes available in buf
2859 * @param buf where the callee should write the message
2860 * @return number of bytes written to buf
2863 notify_client_connection_failure (void *cls, size_t size, void *buf)
2866 struct MeshPeerInfo *peer_info;
2867 struct GNUNET_MESH_PeerControl *msg;
2868 struct GNUNET_PeerIdentity id;
2870 if (0 == size && NULL == buf)
2872 // TODO retry? cancel?
2876 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2877 peer_info = (struct MeshPeerInfo *) cls;
2878 msg = (struct GNUNET_MESH_PeerControl *) buf;
2879 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2880 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2881 // msg->tunnel_id = htonl(peer_info->t->tid);
2882 GNUNET_PEER_resolve (peer_info->id, &id);
2883 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2891 * Send keepalive packets for a peer
2893 * @param cls Closure (tunnel for which to send the keepalive).
2894 * @param tc Notification context.
2896 * TODO: implement explicit multicast keepalive?
2899 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2901 struct MeshTunnel *t = cls;
2902 struct GNUNET_MessageHeader *payload;
2903 struct GNUNET_MESH_Multicast *msg;
2904 size_t size = sizeof(struct GNUNET_MESH_Multicast) +
2905 sizeof(struct GNUNET_MessageHeader);
2908 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2912 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2915 "MESH: sending keepalive for tunnel %d\n",
2918 msg = (struct GNUNET_MESH_Multicast *) cbuf;
2919 msg->header.size = htons (size);
2920 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2921 msg->oid = my_full_id;
2922 msg->tid = htonl(t->id.tid);
2923 payload = (struct GNUNET_MessageHeader *) &msg[1];
2924 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2925 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
2926 tunnel_send_multicast (t, &msg->header);
2928 t->path_refresh_task =
2929 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2935 * Function to process paths received for a new peer addition. The recorded
2936 * paths form the initial tunnel, which can be optimized later.
2937 * Called on each result obtained for the DHT search.
2939 * @param cls closure
2940 * @param exp when will this value expire
2941 * @param key key of the result
2942 * @param type type of the result
2943 * @param size number of bytes in data
2944 * @param data pointer to the result data
2946 * TODO: re-issue the request after certain time? cancel after X results?
2949 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2950 const GNUNET_HashCode * key,
2951 const struct GNUNET_PeerIdentity *get_path,
2952 unsigned int get_path_length,
2953 const struct GNUNET_PeerIdentity *put_path,
2954 unsigned int put_path_length,
2955 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2957 struct MeshPathInfo *path_info = cls;
2958 struct MeshPeerPath *p;
2959 struct GNUNET_PeerIdentity pi;
2962 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2963 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2964 "MESH: Got results from DHT for %s\n",
2965 GNUNET_h2s_full(&pi.hashPubKey));
2966 GNUNET_DHT_get_stop(path_info->peer->dhtget);
2967 path_info->peer->dhtget = NULL;
2969 p = path_build_from_dht (get_path, get_path_length,
2970 put_path, put_path_length);
2971 path_add_to_peer (path_info->peer, p);
2972 for (i = 0; i < path_info->peer->ntunnels; i++)
2974 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2975 peer_info_connect(path_info->peer, path_info->t);
2977 GNUNET_free (path_info);
2984 * Function to process paths received for a new peer addition. The recorded
2985 * paths form the initial tunnel, which can be optimized later.
2986 * Called on each result obtained for the DHT search.
2988 * @param cls closure
2989 * @param exp when will this value expire
2990 * @param key key of the result
2991 * @param type type of the result
2992 * @param size number of bytes in data
2993 * @param data pointer to the result data
2996 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2997 const GNUNET_HashCode * key,
2998 const struct GNUNET_PeerIdentity *get_path,
2999 unsigned int get_path_length,
3000 const struct GNUNET_PeerIdentity *put_path,
3001 unsigned int put_path_length,
3002 enum GNUNET_BLOCK_Type type, size_t size,
3005 const struct GNUNET_PeerIdentity *pi = data;
3006 struct GNUNET_PeerIdentity id;
3007 struct MeshTunnel *t = cls;
3008 struct MeshPeerInfo *peer_info;
3009 struct MeshPathInfo *path_info;
3010 struct MeshPeerPath *p;
3013 if (size != sizeof (struct GNUNET_PeerIdentity))
3015 GNUNET_break_op (0);
3018 GNUNET_assert (NULL != t->client);
3019 GNUNET_DHT_get_stop (t->dht_get_type);
3020 t->dht_get_type = NULL;
3021 peer_info = peer_info_get (pi);
3022 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
3023 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3025 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
3026 NULL == peer_info->dhtget)
3028 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3029 path_info->peer = peer_info;
3031 /* we don't have a route to the peer, let's try a direct lookup */
3033 GNUNET_DHT_get_start (dht_handle, /* handle */
3034 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
3035 GNUNET_BLOCK_TYPE_TEST, /* block type */
3036 &pi->hashPubKey, /* key to look up */
3037 10U, /* replication level */
3038 GNUNET_DHT_RO_RECORD_ROUTE |
3039 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3040 /* option to dht: record route */
3042 0, /* xquery bits */
3043 dht_get_id_handler, /* callback */
3044 path_info); /* closure */
3048 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
3049 path_add_to_peer (peer_info, p);
3050 tunnel_add_peer(t, peer_info);
3051 p = tree_get_path_to_peer(t->tree, peer_info->id);
3053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3054 "MESH: new route for tunnel 0x%x found, has %u hops\n",
3055 t->local_tid, p->length);
3056 for (i = 0; i < p->length; i++)
3058 GNUNET_PEER_resolve (p->peers[0], &id);
3059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
3060 GNUNET_h2s_full (&id.hashPubKey));
3066 send_create_path(peer_info, p, t);
3070 send_client_peer_connected(t, myid);
3074 /******************************************************************************/
3075 /********************* MESH LOCAL HANDLES **************************/
3076 /******************************************************************************/
3080 * Handler for client disconnection
3082 * @param cls closure
3083 * @param client identification of the client; NULL
3084 * for the last call when the server is destroyed
3087 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3089 struct MeshClient *c;
3090 struct MeshClient *next;
3092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3098 if (c->handle != client && NULL != client)
3100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
3105 if (NULL != c->tunnels)
3107 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3108 &tunnel_destroy_iterator,
3110 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3113 /* deregister clients applications */
3114 if (NULL != c->apps)
3116 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3117 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3119 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3120 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3122 GNUNET_SCHEDULER_cancel (announce_applications_task);
3123 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3125 if (NULL != c->types)
3126 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3127 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3139 * Handler for new clients
3141 * @param cls closure
3142 * @param client identification of the client
3143 * @param message the actual message, which includes messages the client wants
3146 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3147 const struct GNUNET_MessageHeader *message)
3149 struct GNUNET_MESH_ClientConnect *cc_msg;
3150 struct MeshClient *c;
3151 GNUNET_MESH_ApplicationType *a;
3158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3159 /* Check data sanity */
3160 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3161 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3162 ntypes = ntohs (cc_msg->types);
3163 napps = ntohs (cc_msg->applications);
3165 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3168 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3172 /* Create new client structure */
3173 c = GNUNET_malloc (sizeof (struct MeshClient));
3175 c->id = next_client_id++;
3178 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3181 GNUNET_MESH_ApplicationType at;
3184 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3185 for (i = 0; i < napps; i++)
3188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3189 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3190 /* store in clients hashmap */
3191 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3192 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3193 /* store in global hashmap, for announcements */
3194 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3195 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3197 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3198 announce_applications_task =
3199 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3207 t = (uint16_t *) & a[napps];
3208 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3209 for (i = 0; i < ntypes; i++)
3212 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3214 /* store in clients hashmap */
3215 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3216 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3217 /* store in global hashmap */
3218 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3219 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3223 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3225 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3226 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3227 GNUNET_SERVER_notification_context_add (nc, client);
3229 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3237 * Handler for requests of new tunnels
3239 * @param cls closure
3240 * @param client identification of the client
3241 * @param message the actual message
3244 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3245 const struct GNUNET_MessageHeader *message)
3247 struct GNUNET_MESH_TunnelMessage *t_msg;
3248 struct MeshTunnel *t;
3249 struct MeshClient *c;
3250 GNUNET_HashCode hash;
3252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3254 /* Sanity check for client registration */
3255 if (NULL == (c = client_get (client)))
3258 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3265 /* Message sanity check */
3266 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3269 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3273 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3274 /* Sanity check for tunnel numbering */
3275 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3278 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3281 /* Sanity check for duplicate tunnel IDs */
3282 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3285 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3289 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
3291 while (NULL != tunnel_get_by_pi (myid, next_tid))
3292 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3293 t->id.tid = next_tid++;
3295 t->local_tid = ntohl (t_msg->tunnel_id);
3297 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3299 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3301 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3302 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3305 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3309 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3311 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3312 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3318 t->tree = tree_new (t, myid);
3319 t->tree->refresh = REFRESH_PATH_TIME;
3320 t->tree->root->status = MESH_PEER_READY;
3321 t->tree->me = t->tree->root;
3323 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3329 * Handler for requests of deleting tunnels
3331 * @param cls closure
3332 * @param client identification of the client
3333 * @param message the actual message
3336 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3337 const struct GNUNET_MessageHeader *message)
3339 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3340 struct MeshClient *c;
3341 struct MeshTunnel *t;
3342 MESH_TunnelNumber tid;
3343 GNUNET_HashCode hash;
3345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
3347 /* Sanity check for client registration */
3348 if (NULL == (c = client_get (client)))
3351 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3354 /* Message sanity check */
3355 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3358 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3364 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3366 /* Retrieve tunnel */
3367 tid = ntohl (tunnel_msg->tunnel_id);
3369 /* Remove from local id hashmap */
3370 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3371 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3372 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
3375 tunnel_send_destroy (t);
3377 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3383 * Handler for connection requests to new peers
3385 * @param cls closure
3386 * @param client identification of the client
3387 * @param message the actual message (PeerControl)
3390 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3391 const struct GNUNET_MessageHeader *message)
3393 struct GNUNET_MESH_PeerControl *peer_msg;
3394 struct MeshPeerInfo *peer_info;
3395 struct MeshClient *c;
3396 struct MeshTunnel *t;
3397 MESH_TunnelNumber tid;
3399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3400 /* Sanity check for client registration */
3401 if (NULL == (c = client_get (client)))
3404 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3408 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3409 /* Sanity check for message size */
3410 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3413 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3417 /* Tunnel exists? */
3418 tid = ntohl (peer_msg->tunnel_id);
3419 t = tunnel_get_by_local_id (c, tid);
3423 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3427 /* Does client own tunnel? */
3428 if (t->client->handle != client)
3431 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3434 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3435 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
3436 peer_info = peer_info_get (&peer_msg->peer);
3438 tunnel_add_peer(t, peer_info);
3439 peer_info_connect(peer_info, t);
3441 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3447 * Handler for disconnection requests of peers in a tunnel
3449 * @param cls closure
3450 * @param client identification of the client
3451 * @param message the actual message (PeerControl)
3454 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3455 const struct GNUNET_MessageHeader *message)
3457 struct GNUNET_MESH_PeerControl *peer_msg;
3458 struct MeshClient *c;
3459 struct MeshTunnel *t;
3460 MESH_TunnelNumber tid;
3462 /* Sanity check for client registration */
3463 if (NULL == (c = client_get (client)))
3466 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3469 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3470 /* Sanity check for message size */
3471 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3474 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3478 /* Tunnel exists? */
3479 tid = ntohl (peer_msg->tunnel_id);
3480 t = tunnel_get_by_local_id (c, tid);
3484 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3488 /* Does client own tunnel? */
3489 if (t->client->handle != client)
3492 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3496 /* Ok, delete peer from tunnel */
3497 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3498 &peer_msg->peer.hashPubKey);
3500 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3506 * Handler for connection requests to new peers by type
3508 * @param cls closure
3509 * @param client identification of the client
3510 * @param message the actual message (ConnectPeerByType)
3513 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3514 const struct GNUNET_MessageHeader *message)
3516 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3517 struct MeshClient *c;
3518 struct MeshTunnel *t;
3519 GNUNET_HashCode hash;
3520 MESH_TunnelNumber tid;
3522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3523 /* Sanity check for client registration */
3524 if (NULL == (c = client_get (client)))
3527 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3531 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3532 /* Sanity check for message size */
3533 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3534 ntohs (connect_msg->header.size))
3537 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3541 /* Tunnel exists? */
3542 tid = ntohl (connect_msg->tunnel_id);
3543 t = tunnel_get_by_local_id (c, tid);
3547 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3551 /* Does client own tunnel? */
3552 if (t->client->handle != client)
3555 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3559 /* Do WE have the service? */
3560 t->type = ntohl (connect_msg->type);
3561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3562 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3563 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3566 /* Yes! Fast forward, add ourselves to the tunnel and send the
3567 * good news to the client
3569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3570 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3571 peer_info_get (&my_full_id),
3572 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3575 send_client_peer_connected(t, myid);
3576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3577 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3580 /* Ok, lets find a peer offering the service */
3581 if (NULL != t->dht_get_type)
3583 GNUNET_DHT_get_stop (t->dht_get_type);
3585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3586 GNUNET_h2s_full (&hash));
3588 GNUNET_DHT_get_start (dht_handle,
3589 GNUNET_TIME_UNIT_FOREVER_REL,
3590 GNUNET_BLOCK_TYPE_TEST,
3593 GNUNET_DHT_RO_RECORD_ROUTE |
3594 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3596 &dht_get_type_handler, t);
3598 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3604 * Handler for client traffic directed to one peer
3606 * @param cls closure
3607 * @param client identification of the client
3608 * @param message the actual message
3611 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3612 const struct GNUNET_MessageHeader *message)
3614 struct MeshClient *c;
3615 struct MeshTunnel *t;
3616 struct MeshPeerInfo *pi;
3617 struct GNUNET_MESH_Unicast *data_msg;
3618 MESH_TunnelNumber tid;
3621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3622 "MESH: Got a unicast request from a client!\n");
3624 /* Sanity check for client registration */
3625 if (NULL == (c = client_get (client)))
3628 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3631 data_msg = (struct GNUNET_MESH_Unicast *) message;
3632 /* Sanity check for message size */
3633 size = ntohs (message->size);
3634 if (sizeof (struct GNUNET_MESH_Unicast) +
3635 sizeof (struct GNUNET_MessageHeader) > size)
3638 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3642 /* Tunnel exists? */
3643 tid = ntohl (data_msg->tid);
3644 t = tunnel_get_by_local_id (c, tid);
3648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3652 /* Is it a local tunnel? Then, does client own the tunnel? */
3653 if (NULL != t->client &&
3654 NULL != t->client->handle &&
3655 t->client->handle != client)
3658 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3662 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3663 &data_msg->destination.hashPubKey);
3664 /* Is the selected peer in the tunnel? */
3668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3672 /* Ok, everything is correct, send the message
3673 * (pretend we got it from a mesh peer)
3676 char buf[ntohs (message->size)];
3677 struct GNUNET_MESH_Unicast *copy;
3679 /* Work around const limitation */
3680 copy = (struct GNUNET_MESH_Unicast *) buf;
3681 memcpy (buf, data_msg, size);
3682 copy->oid = my_full_id;
3683 copy->tid = htonl (t->id.tid);
3684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3685 "MESH: calling generic handler...\n");
3686 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
3688 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3694 * Handler for client traffic directed to the origin
3696 * @param cls closure
3697 * @param client identification of the client
3698 * @param message the actual message
3701 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3702 const struct GNUNET_MessageHeader *message)
3704 struct GNUNET_MESH_ToOrigin *data_msg;
3705 struct GNUNET_PeerIdentity id;
3706 struct MeshClient *c;
3707 struct MeshTunnel *t;
3708 MESH_TunnelNumber tid;
3711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3712 "MESH: Got a ToOrigin request from a client!\n");
3714 /* Sanity check for client registration */
3715 if (NULL == (c = client_get (client)))
3718 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3721 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3722 /* Sanity check for message size */
3723 size = ntohs (message->size);
3724 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3725 sizeof (struct GNUNET_MessageHeader) > size)
3728 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3732 /* Tunnel exists? */
3733 tid = ntohl (data_msg->tid);
3734 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3737 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3740 t = tunnel_get_by_local_id (c, tid);
3744 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3748 /* It shouldn't be a local tunnel. */
3749 if (NULL != t->client)
3752 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3755 GNUNET_PEER_resolve(t->id.oid, &id);
3757 /* Ok, everything is correct, send the message
3758 * (pretend we got it from a mesh peer)
3761 char buf[ntohs (message->size)];
3762 struct GNUNET_MESH_ToOrigin *copy;
3764 /* Work around const limitation */
3765 copy = (struct GNUNET_MESH_ToOrigin *) buf;
3766 memcpy (buf, data_msg, size);
3768 copy->tid = htonl (t->id.tid);
3769 copy->sender = my_full_id;
3770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3771 "MESH: calling generic handler...\n");
3772 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL);
3774 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3780 * Handler for client traffic directed to all peers in a tunnel
3782 * @param cls closure
3783 * @param client identification of the client
3784 * @param message the actual message
3787 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3788 const struct GNUNET_MessageHeader *message)
3790 struct MeshClient *c;
3791 struct MeshTunnel *t;
3792 struct GNUNET_MESH_Multicast *data_msg;
3793 MESH_TunnelNumber tid;
3795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3796 "MESH: Got a multicast request from a client!\n");
3798 /* Sanity check for client registration */
3799 if (NULL == (c = client_get (client)))
3802 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3805 data_msg = (struct GNUNET_MESH_Multicast *) message;
3806 /* Sanity check for message size */
3807 if (sizeof (struct GNUNET_MESH_Multicast) +
3808 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
3811 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3815 /* Tunnel exists? */
3816 tid = ntohl (data_msg->tid);
3817 t = tunnel_get_by_local_id (c, tid);
3821 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3825 /* Does client own tunnel? */
3826 if (t->client->handle != client)
3829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3834 char buf[ntohs(message->size)];
3835 struct GNUNET_MESH_Multicast *copy;
3837 copy = (struct GNUNET_MESH_Multicast *)buf;
3838 memcpy(buf, message, ntohs(message->size));
3839 copy->oid = my_full_id;
3840 copy->tid = htonl(t->id.tid);
3841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3842 "MESH: calling generic handler...\n");
3843 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3846 /* receive done gets called when last copy is sent to a neighbor */
3851 * Functions to handle messages from clients
3853 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3854 {&handle_local_new_client, NULL,
3855 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3856 {&handle_local_tunnel_create, NULL,
3857 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3858 sizeof (struct GNUNET_MESH_TunnelMessage)},
3859 {&handle_local_tunnel_destroy, NULL,
3860 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3861 sizeof (struct GNUNET_MESH_TunnelMessage)},
3862 {&handle_local_connect_add, NULL,
3863 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3864 sizeof (struct GNUNET_MESH_PeerControl)},
3865 {&handle_local_connect_del, NULL,
3866 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3867 sizeof (struct GNUNET_MESH_PeerControl)},
3868 {&handle_local_connect_by_type, NULL,
3869 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3870 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3871 {&handle_local_unicast, NULL,
3872 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3873 {&handle_local_to_origin, NULL,
3874 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3875 {&handle_local_multicast, NULL,
3876 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3882 * To be called on core init/fail.
3884 * @param cls service closure
3885 * @param server handle to the server for this service
3886 * @param identity the public identity of this peer
3889 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3890 const struct GNUNET_PeerIdentity *identity)
3892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3893 core_handle = server;
3894 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3896 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3897 GNUNET_SCHEDULER_shutdown();
3903 * Method called whenever a given peer connects.
3905 * @param cls closure
3906 * @param peer peer identity this notification is about
3907 * @param atsi performance data for the connection
3910 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3911 const struct GNUNET_ATS_Information *atsi)
3913 struct MeshPeerInfo *peer_info;
3914 struct MeshPeerPath *path;
3916 #if MESH_DEBUG_CONNECTION
3917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3919 GNUNET_h2s(&my_full_id.hashPubKey));
3921 peer_info = peer_info_get (peer);
3922 if (myid == peer_info->id)
3924 #if MESH_DEBUG_CONNECTION
3925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3929 #if MESH_DEBUG_CONNECTION
3932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3933 GNUNET_h2s(&peer->hashPubKey));
3936 path = path_new (2);
3937 path->peers[0] = myid;
3938 path->peers[1] = peer_info->id;
3939 GNUNET_PEER_change_rc(myid, 1);
3940 GNUNET_PEER_change_rc(peer_info->id, 1);
3941 path_add_to_peer (peer_info, path);
3946 * Method called whenever a peer disconnects.
3948 * @param cls closure
3949 * @param peer peer identity this notification is about
3952 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3954 struct MeshPeerInfo *pi;
3957 #if MESH_DEBUG_CONNECTION
3958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3960 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3966 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3968 peer_info_cancel_transmission(pi, i);
3970 path_remove_from_peer (pi, pi->id, myid);
3971 #if MESH_DEBUG_CONNECTION
3974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3981 /******************************************************************************/
3982 /************************ MAIN FUNCTIONS ****************************/
3983 /******************************************************************************/
3986 * Iterator over hash map entries.
3988 * @param cls closure
3989 * @param key current key code
3990 * @param value value in the hash map
3991 * @return GNUNET_YES if we should continue to
3996 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
3998 struct MeshTunnel *t = value;
4004 * Task run during shutdown.
4010 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4013 /* TODO: destroy tunnels? */
4014 if (core_handle != NULL)
4016 GNUNET_CORE_disconnect (core_handle);
4019 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
4020 if (dht_handle != NULL)
4022 GNUNET_DHT_disconnect (dht_handle);
4027 GNUNET_SERVER_notification_context_destroy (nc);
4030 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4032 GNUNET_SCHEDULER_cancel (announce_id_task);
4033 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4039 * Process mesh requests.
4041 * @param cls closure
4042 * @param server the initialized server
4043 * @param c configuration to use
4046 run (void *cls, struct GNUNET_SERVER_Handle *server,
4047 const struct GNUNET_CONFIGURATION_Handle *c)
4049 struct MeshPeerInfo *peer;
4050 struct MeshPeerPath *p;
4053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4054 server_handle = server;
4055 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4056 CORE_QUEUE_SIZE, /* queue size */
4057 NULL, /* Closure passed to MESH functions */
4058 &core_init, /* Call core_init once connected */
4059 &core_connect, /* Handle connects */
4060 &core_disconnect, /* remove peers on disconnects */
4061 NULL, /* Don't notify about all incoming messages */
4062 GNUNET_NO, /* For header only in notification */
4063 NULL, /* Don't notify about all outbound messages */
4064 GNUNET_NO, /* For header-only out notification */
4065 core_handlers); /* Register these handlers */
4066 if (core_handle == NULL)
4069 GNUNET_SCHEDULER_shutdown ();
4074 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4077 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4079 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4080 GNUNET_SCHEDULER_shutdown ();
4083 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4084 GNUNET_free (keyfile);
4085 if (my_private_key == NULL)
4087 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4088 _("Mesh service could not access hostkey. Exiting.\n"));
4089 GNUNET_SCHEDULER_shutdown ();
4092 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4093 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4094 &my_full_id.hashPubKey);
4095 myid = GNUNET_PEER_intern (&my_full_id);
4097 dht_handle = GNUNET_DHT_connect (c, 64);
4098 if (dht_handle == NULL)
4104 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4106 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4107 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4108 peers = GNUNET_CONTAINER_multihashmap_create (32);
4109 applications = GNUNET_CONTAINER_multihashmap_create (32);
4110 types = GNUNET_CONTAINER_multihashmap_create (32);
4112 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4113 nc = GNUNET_SERVER_notification_context_create (server_handle,
4115 GNUNET_SERVER_disconnect_notify (server_handle,
4116 &handle_local_client_disconnect,
4121 clients_tail = NULL;
4126 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4127 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4129 /* Create a peer_info for the local peer */
4130 peer = peer_info_get(&my_full_id);
4133 path_add_to_peer(peer, p);
4135 /* Scheduled the task to clean up when shutdown is called */
4136 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4143 * The main function for the mesh service.
4145 * @param argc number of arguments from the command line
4146 * @param argv command line arguments
4147 * @return 0 ok, 1 on error
4150 main (int argc, char *const *argv)
4155 // fprintf (stderr, "main ()\n");
4157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4160 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
4164 // fprintf (stderr, "main () END\n");