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],
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 if (own_pos == size - 1)
2369 /* It is for us! Send ack. */
2370 struct GNUNET_MESH_TunnelNotification cmsg;
2371 struct MeshDataDescriptor *info;
2374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2375 "MESH: It's for us!\n");
2376 path_add_to_origin (orig_peer_info, path);
2377 t->peers = GNUNET_CONTAINER_multihashmap_create(4);
2378 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2380 &my_full_id.hashPubKey,
2381 peer_info_get(&my_full_id),
2382 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2383 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2384 info->origin = &t->id;
2385 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2386 GNUNET_assert (NULL != info->peer);
2387 j = peer_info_transmit_slot(info->peer);
2388 info->handler_n = j;
2389 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2390 info->peer->infos[j] = info;
2391 info->peer->core_transmit[j] =
2392 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2393 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2394 sizeof (struct GNUNET_MESH_PathACK),
2395 &send_core_path_ack, info);
2396 cmsg.header.size = htons(sizeof(cmsg));
2397 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2398 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2399 cmsg.tunnel_id = htonl(t->local_tid);
2400 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2404 struct MeshPeerPath *path2;
2406 /* It's for somebody else! Retransmit. */
2407 path2 = path_duplicate(path);
2408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2409 "MESH: Retransmitting.\n");
2410 path_add_to_peer(dest_peer_info, path);
2411 path = path_duplicate(path2);
2412 path_add_to_origin(orig_peer_info, path2);
2413 send_create_path(dest_peer_info, path, t);
2420 * Core handler for path destruction
2422 * @param cls closure
2423 * @param message message
2424 * @param peer peer identity this notification is about
2425 * @param atsi performance data
2427 * @return GNUNET_OK to keep the connection open,
2428 * GNUNET_SYSERR to close it (signal serious error)
2431 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2432 const struct GNUNET_MessageHeader *message,
2433 const struct GNUNET_ATS_Information *atsi)
2435 struct GNUNET_MESH_ManipulatePath *msg;
2436 struct GNUNET_PeerIdentity *pi;
2437 struct MeshPeerPath *path;
2438 struct MeshTunnel *t;
2439 unsigned int own_pos;
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2444 "MESH: Received a PATH DESTROY msg from %s\n",
2446 size = ntohs (message->size);
2447 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2449 GNUNET_break_op (0);
2453 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2454 if (size % sizeof (struct GNUNET_PeerIdentity))
2456 GNUNET_break_op (0);
2459 size /= sizeof (struct GNUNET_PeerIdentity);
2462 GNUNET_break_op (0);
2465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2466 "MESH: path has %u hops.\n",
2469 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2470 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2472 "MESH: path is for tunnel %s [%X].\n",
2475 t = tunnel_get (pi, ntohl (msg->tid));
2478 /* TODO notify back: we don't know this tunnel */
2479 GNUNET_break_op (0);
2482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2483 path = path_new (size);
2485 for (i = 0; i < size; i++)
2487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2488 "MESH: ... adding %s\n",
2489 GNUNET_i2s(&pi[i]));
2490 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2491 if (path->peers[i] == myid)
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 "MESH: Own position: %u\n", own_pos);
2496 if (own_pos < path->length - 1)
2497 send_message (message, &pi[own_pos + 1]);
2498 tunnel_delete_path (t, path);
2504 * Core handler for tunnel destruction
2506 * @param cls closure
2507 * @param message message
2508 * @param peer peer identity this notification is about
2509 * @param atsi performance data
2511 * @return GNUNET_OK to keep the connection open,
2512 * GNUNET_SYSERR to close it (signal serious error)
2515 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2516 const struct GNUNET_MessageHeader *message,
2517 const struct GNUNET_ATS_Information *atsi)
2519 struct GNUNET_MESH_TunnelDestroy *msg;
2520 struct MeshTunnel *t;
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2523 "MESH: Got a TUNNEL DESTROY packet from %s\n",
2525 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2526 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2529 /* TODO notify back: we don't know this tunnel */
2530 GNUNET_break_op (0);
2533 tunnel_send_destroy (t);
2540 * Core handler for mesh network traffic going from the origin to a peer
2542 * @param cls closure
2543 * @param peer peer identity this notification is about
2544 * @param message message
2545 * @param atsi performance data
2546 * @return GNUNET_OK to keep the connection open,
2547 * GNUNET_SYSERR to close it (signal serious error)
2550 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2551 const struct GNUNET_MessageHeader *message,
2552 const struct GNUNET_ATS_Information *atsi)
2554 struct GNUNET_MESH_Unicast *msg;
2555 struct MeshTunnel *t;
2559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2560 "MESH: got a unicast packet from %s\n",
2562 size = ntohs (message->size);
2564 sizeof (struct GNUNET_MESH_Unicast) +
2565 sizeof (struct GNUNET_MessageHeader))
2570 msg = (struct GNUNET_MESH_Unicast *) message;
2571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2572 "MESH: of type %u\n",
2573 ntohs (msg[1].header.type));
2574 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2577 /* TODO notify back: we don't know this tunnel */
2578 GNUNET_break_op (0);
2581 tunnel_reset_timeout (t);
2582 pid = GNUNET_PEER_search(&msg->destination);
2585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2586 "MESH: it's for us! sending to clients...\n");
2587 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2591 "MESH: not for us, retransmitting...\n");
2592 send_message (message, path_get_first_hop(t->tree, pid));
2598 * Core handler for mesh network traffic going from the origin to all peers
2600 * @param cls closure
2601 * @param message message
2602 * @param peer peer identity this notification is about
2603 * @param atsi performance data
2604 * @return GNUNET_OK to keep the connection open,
2605 * GNUNET_SYSERR to close it (signal serious error)
2607 * TODO: Check who we got this from, to validate route.
2610 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2611 const struct GNUNET_MessageHeader *message,
2612 const struct GNUNET_ATS_Information *atsi)
2614 struct GNUNET_MESH_Multicast *msg;
2615 struct MeshTunnel *t;
2618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2619 "MESH: got a multicast packet from %s\n",
2621 size = ntohs (message->size);
2622 if (sizeof (struct GNUNET_MESH_Multicast) +
2623 sizeof (struct GNUNET_MessageHeader) > size)
2625 GNUNET_break_op (0);
2628 msg = (struct GNUNET_MESH_Multicast *) message;
2629 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2633 /* TODO notify that we dont know that tunnel */
2634 GNUNET_break_op (0);
2637 tunnel_reset_timeout (t);
2639 /* Transmit to locally interested clients */
2640 if (NULL != t->peers &&
2641 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2643 send_subscribed_clients (message, &msg[1].header);
2645 tunnel_send_multicast(t, message);
2651 * Core handler for mesh network traffic toward the owner of a tunnel
2653 * @param cls closure
2654 * @param message message
2655 * @param peer peer identity this notification is about
2656 * @param atsi performance data
2658 * @return GNUNET_OK to keep the connection open,
2659 * GNUNET_SYSERR to close it (signal serious error)
2662 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2663 const struct GNUNET_MessageHeader *message,
2664 const struct GNUNET_ATS_Information *atsi)
2666 struct GNUNET_MESH_ToOrigin *msg;
2667 struct GNUNET_PeerIdentity id;
2668 struct MeshPeerInfo *peer_info;
2669 struct MeshTunnel *t;
2672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2673 "MESH: got a ToOrigin packet from %s\n",
2675 size = ntohs (message->size);
2676 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2677 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2679 GNUNET_break_op (0);
2682 msg = (struct GNUNET_MESH_ToOrigin *) message;
2683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2684 "MESH: of type %u\n",
2685 ntohs (msg[1].header.type));
2686 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2690 /* TODO notify that we dont know this tunnel (whom)? */
2691 GNUNET_break_op (0);
2695 if (t->id.oid == myid)
2698 struct GNUNET_MESH_ToOrigin *copy;
2700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2701 "MESH: it's for us! sending to clients...\n");
2702 if (NULL == t->client)
2704 /* got data packet for ownerless tunnel */
2705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2706 "MESH: no clients!\n");
2707 GNUNET_break_op (0);
2710 /* TODO signature verification */
2711 memcpy (cbuf, message, size);
2712 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2713 copy->tid = htonl (t->local_tid);
2714 GNUNET_SERVER_notification_context_unicast (nc,
2720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2721 "MESH: not for us, retransmitting...\n");
2723 peer_info = peer_info_get (&msg->oid);
2724 if (NULL == peer_info)
2726 /* unknown origin of tunnel */
2730 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
2731 send_message (message, &id);
2738 * Core handler for path ACKs
2740 * @param cls closure
2741 * @param message message
2742 * @param peer peer identity this notification is about
2743 * @param atsi performance data
2745 * @return GNUNET_OK to keep the connection open,
2746 * GNUNET_SYSERR to close it (signal serious error)
2749 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2750 const struct GNUNET_MessageHeader *message,
2751 const struct GNUNET_ATS_Information *atsi)
2753 struct GNUNET_MESH_PathACK *msg;
2754 struct GNUNET_PeerIdentity id;
2755 struct MeshTunnelTreeNode *n;
2756 struct MeshPeerInfo *peer_info;
2757 struct MeshTunnel *t;
2759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2760 "MESH: Received a path ACK msg [%s]\n",
2761 GNUNET_i2s(&my_full_id));
2762 msg = (struct GNUNET_MESH_PathACK *) message;
2763 t = tunnel_get (&msg->oid, msg->tid);
2766 /* TODO notify that we don't know the tunnel */
2770 /* Message for us? */
2771 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
2774 if (NULL == t->client)
2776 GNUNET_break_op (0);
2779 peer_info = peer_info_get (&msg->peer_id);
2780 if (NULL == peer_info)
2782 GNUNET_break_op (0);
2785 n = tree_find_peer(t->tree->root, peer_info->id);
2788 GNUNET_break_op (0);
2791 n->status = MESH_PEER_READY;
2792 send_client_peer_connected(t, peer_info->id);
2796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2797 "MESH: not for us, retransmitting...\n");
2798 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
2799 peer_info = peer_info_get (&msg->oid);
2800 if (NULL == peer_info)
2802 /* If we know the tunnel, we should DEFINITELY know the peer */
2806 send_message (message, &id);
2812 * Functions to handle messages from core
2814 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2815 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2816 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
2817 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
2818 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2819 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2820 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2821 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2822 sizeof (struct GNUNET_MESH_PathACK)},
2828 /******************************************************************************/
2829 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2830 /******************************************************************************/
2833 * deregister_app: iterator for removing each application registered by a client
2835 * @param cls closure
2836 * @param key the hash of the application id (used to access the hashmap)
2837 * @param value the value stored at the key (client)
2839 * @return GNUNET_OK on success
2842 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2844 GNUNET_break (GNUNET_YES ==
2845 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
2851 * notify_client_connection_failure: notify a client that the connection to the
2852 * requested remote peer is not possible (for instance, no route found)
2853 * Function called when the socket is ready to queue more data. "buf" will be
2854 * NULL and "size" zero if the socket was closed for writing in the meantime.
2856 * @param cls closure
2857 * @param size number of bytes available in buf
2858 * @param buf where the callee should write the message
2859 * @return number of bytes written to buf
2862 notify_client_connection_failure (void *cls, size_t size, void *buf)
2865 struct MeshPeerInfo *peer_info;
2866 struct GNUNET_MESH_PeerControl *msg;
2867 struct GNUNET_PeerIdentity id;
2869 if (0 == size && NULL == buf)
2871 // TODO retry? cancel?
2875 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2876 peer_info = (struct MeshPeerInfo *) cls;
2877 msg = (struct GNUNET_MESH_PeerControl *) buf;
2878 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2879 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2880 // msg->tunnel_id = htonl(peer_info->t->tid);
2881 GNUNET_PEER_resolve (peer_info->id, &id);
2882 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2890 * Send keepalive packets for a peer
2892 * @param cls Closure (tunnel for which to send the keepalive).
2893 * @param tc Notification context.
2895 * TODO: implement explicit multicast keepalive?
2898 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2900 struct MeshTunnel *t = cls;
2901 struct GNUNET_MessageHeader *payload;
2902 struct GNUNET_MESH_Multicast *msg;
2903 size_t size = sizeof(struct GNUNET_MESH_Multicast) +
2904 sizeof(struct GNUNET_MessageHeader);
2907 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2911 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2914 "MESH: sending keepalive for tunnel %d\n",
2917 msg = (struct GNUNET_MESH_Multicast *) cbuf;
2918 msg->header.size = htons (size);
2919 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2920 msg->oid = my_full_id;
2921 msg->tid = htonl(t->id.tid);
2922 payload = (struct GNUNET_MessageHeader *) &msg[1];
2923 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2924 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
2925 tunnel_send_multicast (t, &msg->header);
2927 t->path_refresh_task =
2928 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2934 * Function to process paths received for a new peer addition. The recorded
2935 * paths form the initial tunnel, which can be optimized later.
2936 * Called on each result obtained for the DHT search.
2938 * @param cls closure
2939 * @param exp when will this value expire
2940 * @param key key of the result
2941 * @param type type of the result
2942 * @param size number of bytes in data
2943 * @param data pointer to the result data
2945 * TODO: re-issue the request after certain time? cancel after X results?
2948 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2949 const GNUNET_HashCode * key,
2950 const struct GNUNET_PeerIdentity *get_path,
2951 unsigned int get_path_length,
2952 const struct GNUNET_PeerIdentity *put_path,
2953 unsigned int put_path_length,
2954 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2956 struct MeshPathInfo *path_info = cls;
2957 struct MeshPeerPath *p;
2958 struct GNUNET_PeerIdentity pi;
2961 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2962 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2963 "MESH: Got results from DHT for %s\n",
2964 GNUNET_h2s_full(&pi.hashPubKey));
2965 GNUNET_DHT_get_stop(path_info->peer->dhtget);
2966 path_info->peer->dhtget = NULL;
2968 p = path_build_from_dht (get_path, get_path_length,
2969 put_path, put_path_length);
2970 path_add_to_peer (path_info->peer, p);
2971 for (i = 0; i < path_info->peer->ntunnels; i++)
2973 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2974 peer_info_connect(path_info->peer, path_info->t);
2976 GNUNET_free (path_info);
2983 * Function to process paths received for a new peer addition. The recorded
2984 * paths form the initial tunnel, which can be optimized later.
2985 * Called on each result obtained for the DHT search.
2987 * @param cls closure
2988 * @param exp when will this value expire
2989 * @param key key of the result
2990 * @param type type of the result
2991 * @param size number of bytes in data
2992 * @param data pointer to the result data
2995 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2996 const GNUNET_HashCode * key,
2997 const struct GNUNET_PeerIdentity *get_path,
2998 unsigned int get_path_length,
2999 const struct GNUNET_PeerIdentity *put_path,
3000 unsigned int put_path_length,
3001 enum GNUNET_BLOCK_Type type, size_t size,
3004 const struct GNUNET_PeerIdentity *pi = data;
3005 struct GNUNET_PeerIdentity id;
3006 struct MeshTunnel *t = cls;
3007 struct MeshPeerInfo *peer_info;
3008 struct MeshPathInfo *path_info;
3009 struct MeshPeerPath *p;
3012 if (size != sizeof (struct GNUNET_PeerIdentity))
3014 GNUNET_break_op (0);
3017 GNUNET_assert (NULL != t->client);
3018 GNUNET_DHT_get_stop (t->dht_get_type);
3019 t->dht_get_type = NULL;
3020 peer_info = peer_info_get (pi);
3021 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
3022 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3024 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
3025 NULL == peer_info->dhtget)
3027 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3028 path_info->peer = peer_info;
3030 /* we don't have a route to the peer, let's try a direct lookup */
3032 GNUNET_DHT_get_start (dht_handle, /* handle */
3033 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
3034 GNUNET_BLOCK_TYPE_TEST, /* block type */
3035 &pi->hashPubKey, /* key to look up */
3036 10U, /* replication level */
3037 GNUNET_DHT_RO_RECORD_ROUTE |
3038 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3039 /* option to dht: record route */
3041 0, /* xquery bits */
3042 dht_get_id_handler, /* callback */
3043 path_info); /* closure */
3047 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
3048 path_add_to_peer (peer_info, p);
3049 tunnel_add_peer(t, peer_info);
3050 p = tree_get_path_to_peer(t->tree, peer_info->id);
3052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3053 "MESH: new route for tunnel 0x%x found, has %u hops\n",
3054 t->local_tid, p->length);
3055 for (i = 0; i < p->length; i++)
3057 GNUNET_PEER_resolve (p->peers[0], &id);
3058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
3059 GNUNET_h2s_full (&id.hashPubKey));
3065 send_create_path(peer_info, p, t);
3069 send_client_peer_connected(t, myid);
3073 /******************************************************************************/
3074 /********************* MESH LOCAL HANDLES **************************/
3075 /******************************************************************************/
3079 * Handler for client disconnection
3081 * @param cls closure
3082 * @param client identification of the client; NULL
3083 * for the last call when the server is destroyed
3086 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3088 struct MeshClient *c;
3089 struct MeshClient *next;
3091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3097 if (c->handle != client && NULL != client)
3099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
3104 if (NULL != c->tunnels)
3106 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3107 &tunnel_destroy_iterator,
3109 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3112 /* deregister clients applications */
3113 if (NULL != c->apps)
3115 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3116 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3118 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3119 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3121 GNUNET_SCHEDULER_cancel (announce_applications_task);
3122 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3124 if (NULL != c->types)
3125 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3126 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3138 * Handler for new clients
3140 * @param cls closure
3141 * @param client identification of the client
3142 * @param message the actual message, which includes messages the client wants
3145 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3146 const struct GNUNET_MessageHeader *message)
3148 struct GNUNET_MESH_ClientConnect *cc_msg;
3149 struct MeshClient *c;
3150 GNUNET_MESH_ApplicationType *a;
3157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3158 /* Check data sanity */
3159 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3160 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3161 ntypes = ntohs (cc_msg->types);
3162 napps = ntohs (cc_msg->applications);
3164 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3167 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3171 /* Create new client structure */
3172 c = GNUNET_malloc (sizeof (struct MeshClient));
3174 c->id = next_client_id++;
3177 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3180 GNUNET_MESH_ApplicationType at;
3183 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3184 for (i = 0; i < napps; i++)
3187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3188 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3189 /* store in clients hashmap */
3190 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3191 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3192 /* store in global hashmap, for announcements */
3193 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3194 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3196 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3197 announce_applications_task =
3198 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3206 t = (uint16_t *) & a[napps];
3207 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3208 for (i = 0; i < ntypes; i++)
3211 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3213 /* store in clients hashmap */
3214 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3215 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3216 /* store in global hashmap */
3217 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3218 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3222 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3224 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3225 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3226 GNUNET_SERVER_notification_context_add (nc, client);
3228 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3236 * Handler for requests of new tunnels
3238 * @param cls closure
3239 * @param client identification of the client
3240 * @param message the actual message
3243 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3244 const struct GNUNET_MessageHeader *message)
3246 struct GNUNET_MESH_TunnelMessage *t_msg;
3247 struct MeshTunnel *t;
3248 struct MeshClient *c;
3249 GNUNET_HashCode hash;
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3253 /* Sanity check for client registration */
3254 if (NULL == (c = client_get (client)))
3257 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3264 /* Message sanity check */
3265 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3268 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3272 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3273 /* Sanity check for tunnel numbering */
3274 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3277 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3280 /* Sanity check for duplicate tunnel IDs */
3281 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3284 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3288 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
3290 while (NULL != tunnel_get_by_pi (myid, next_tid))
3291 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3292 t->id.tid = next_tid++;
3294 t->local_tid = ntohl (t_msg->tunnel_id);
3296 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3298 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3300 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3301 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3308 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3310 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3311 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3314 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3317 t->tree = tree_new (t, myid);
3318 t->tree->refresh = REFRESH_PATH_TIME;
3319 t->tree->root->status = MESH_PEER_READY;
3320 t->tree->me = t->tree->root;
3322 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3328 * Handler for requests of deleting tunnels
3330 * @param cls closure
3331 * @param client identification of the client
3332 * @param message the actual message
3335 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3336 const struct GNUNET_MessageHeader *message)
3338 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3339 struct MeshClient *c;
3340 struct MeshTunnel *t;
3341 MESH_TunnelNumber tid;
3342 GNUNET_HashCode hash;
3344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
3346 /* Sanity check for client registration */
3347 if (NULL == (c = client_get (client)))
3350 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3353 /* Message sanity check */
3354 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3357 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3363 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3365 /* Retrieve tunnel */
3366 tid = ntohl (tunnel_msg->tunnel_id);
3368 /* Remove from local id hashmap */
3369 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3370 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3371 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
3374 tunnel_send_destroy (t);
3376 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3382 * Handler for connection requests to new peers
3384 * @param cls closure
3385 * @param client identification of the client
3386 * @param message the actual message (PeerControl)
3389 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3390 const struct GNUNET_MessageHeader *message)
3392 struct GNUNET_MESH_PeerControl *peer_msg;
3393 struct MeshPeerInfo *peer_info;
3394 struct MeshClient *c;
3395 struct MeshTunnel *t;
3396 MESH_TunnelNumber tid;
3398 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3399 /* Sanity check for client registration */
3400 if (NULL == (c = client_get (client)))
3403 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3407 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3408 /* Sanity check for message size */
3409 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3416 /* Tunnel exists? */
3417 tid = ntohl (peer_msg->tunnel_id);
3418 t = tunnel_get_by_local_id (c, tid);
3422 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3426 /* Does client own tunnel? */
3427 if (t->client->handle != client)
3430 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3433 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3434 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
3435 peer_info = peer_info_get (&peer_msg->peer);
3437 tunnel_add_peer(t, peer_info);
3438 peer_info_connect(peer_info, t);
3440 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3446 * Handler for disconnection requests of peers in a tunnel
3448 * @param cls closure
3449 * @param client identification of the client
3450 * @param message the actual message (PeerControl)
3453 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3454 const struct GNUNET_MessageHeader *message)
3456 struct GNUNET_MESH_PeerControl *peer_msg;
3457 struct MeshClient *c;
3458 struct MeshTunnel *t;
3459 MESH_TunnelNumber tid;
3461 /* Sanity check for client registration */
3462 if (NULL == (c = client_get (client)))
3465 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3468 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3469 /* Sanity check for message size */
3470 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3473 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3477 /* Tunnel exists? */
3478 tid = ntohl (peer_msg->tunnel_id);
3479 t = tunnel_get_by_local_id (c, tid);
3483 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3487 /* Does client own tunnel? */
3488 if (t->client->handle != client)
3491 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3495 /* Ok, delete peer from tunnel */
3496 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3497 &peer_msg->peer.hashPubKey);
3499 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3505 * Handler for connection requests to new peers by type
3507 * @param cls closure
3508 * @param client identification of the client
3509 * @param message the actual message (ConnectPeerByType)
3512 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3513 const struct GNUNET_MessageHeader *message)
3515 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3516 struct MeshClient *c;
3517 struct MeshTunnel *t;
3518 GNUNET_HashCode hash;
3519 MESH_TunnelNumber tid;
3521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3522 /* Sanity check for client registration */
3523 if (NULL == (c = client_get (client)))
3526 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3530 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3531 /* Sanity check for message size */
3532 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3533 ntohs (connect_msg->header.size))
3536 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3540 /* Tunnel exists? */
3541 tid = ntohl (connect_msg->tunnel_id);
3542 t = tunnel_get_by_local_id (c, tid);
3546 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3550 /* Does client own tunnel? */
3551 if (t->client->handle != client)
3554 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3558 /* Do WE have the service? */
3559 t->type = ntohl (connect_msg->type);
3560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3561 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3562 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3565 /* Yes! Fast forward, add ourselves to the tunnel and send the
3566 * good news to the client
3568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3569 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3570 peer_info_get (&my_full_id),
3571 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3574 send_client_peer_connected(t, myid);
3575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3576 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3579 /* Ok, lets find a peer offering the service */
3580 if (NULL != t->dht_get_type)
3582 GNUNET_DHT_get_stop (t->dht_get_type);
3584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3585 GNUNET_h2s_full (&hash));
3587 GNUNET_DHT_get_start (dht_handle,
3588 GNUNET_TIME_UNIT_FOREVER_REL,
3589 GNUNET_BLOCK_TYPE_TEST,
3592 GNUNET_DHT_RO_RECORD_ROUTE |
3593 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3595 &dht_get_type_handler, t);
3597 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3603 * Handler for client traffic directed to one peer
3605 * @param cls closure
3606 * @param client identification of the client
3607 * @param message the actual message
3610 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3611 const struct GNUNET_MessageHeader *message)
3613 struct MeshClient *c;
3614 struct MeshTunnel *t;
3615 struct MeshPeerInfo *pi;
3616 struct GNUNET_MESH_Unicast *data_msg;
3617 MESH_TunnelNumber tid;
3620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3621 "MESH: Got a unicast request from a client!\n");
3623 /* Sanity check for client registration */
3624 if (NULL == (c = client_get (client)))
3627 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3630 data_msg = (struct GNUNET_MESH_Unicast *) message;
3631 /* Sanity check for message size */
3632 size = ntohs (message->size);
3633 if (sizeof (struct GNUNET_MESH_Unicast) +
3634 sizeof (struct GNUNET_MessageHeader) > size)
3637 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3641 /* Tunnel exists? */
3642 tid = ntohl (data_msg->tid);
3643 t = tunnel_get_by_local_id (c, tid);
3647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3651 /* Is it a local tunnel? Then, does client own the tunnel? */
3652 if (NULL != t->client &&
3653 NULL != t->client->handle &&
3654 t->client->handle != client)
3657 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3661 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3662 &data_msg->destination.hashPubKey);
3663 /* Is the selected peer in the tunnel? */
3667 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3671 /* Ok, everything is correct, send the message
3672 * (pretend we got it from a mesh peer)
3675 char buf[ntohs (message->size)];
3676 struct GNUNET_MESH_Unicast *copy;
3678 /* Work around const limitation */
3679 copy = (struct GNUNET_MESH_Unicast *) buf;
3680 memcpy (buf, data_msg, size);
3681 copy->oid = my_full_id;
3682 copy->tid = htonl (t->id.tid);
3683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3684 "MESH: calling generic handler...\n");
3685 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
3687 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3693 * Handler for client traffic directed to the origin
3695 * @param cls closure
3696 * @param client identification of the client
3697 * @param message the actual message
3700 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3701 const struct GNUNET_MessageHeader *message)
3703 struct GNUNET_MESH_ToOrigin *data_msg;
3704 struct GNUNET_PeerIdentity id;
3705 struct MeshClient *c;
3706 struct MeshTunnel *t;
3707 MESH_TunnelNumber tid;
3710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3711 "MESH: Got a ToOrigin request from a client!\n");
3713 /* Sanity check for client registration */
3714 if (NULL == (c = client_get (client)))
3717 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3720 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3721 /* Sanity check for message size */
3722 size = ntohs (message->size);
3723 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3724 sizeof (struct GNUNET_MessageHeader) > size)
3727 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3731 /* Tunnel exists? */
3732 tid = ntohl (data_msg->tid);
3733 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3736 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3739 t = tunnel_get_by_local_id (c, tid);
3743 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3747 /* It shouldn't be a local tunnel. */
3748 if (NULL != t->client)
3751 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3754 GNUNET_PEER_resolve(t->id.oid, &id);
3756 /* Ok, everything is correct, send the message
3757 * (pretend we got it from a mesh peer)
3760 char buf[ntohs (message->size)];
3761 struct GNUNET_MESH_ToOrigin *copy;
3763 /* Work around const limitation */
3764 copy = (struct GNUNET_MESH_ToOrigin *) buf;
3765 memcpy (buf, data_msg, size);
3767 copy->tid = htonl (t->id.tid);
3768 copy->sender = my_full_id;
3769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3770 "MESH: calling generic handler...\n");
3771 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL);
3773 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3779 * Handler for client traffic directed to all peers in a tunnel
3781 * @param cls closure
3782 * @param client identification of the client
3783 * @param message the actual message
3786 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3787 const struct GNUNET_MessageHeader *message)
3789 struct MeshClient *c;
3790 struct MeshTunnel *t;
3791 struct GNUNET_MESH_Multicast *data_msg;
3792 MESH_TunnelNumber tid;
3794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3795 "MESH: Got a multicast request from a client!\n");
3797 /* Sanity check for client registration */
3798 if (NULL == (c = client_get (client)))
3801 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3804 data_msg = (struct GNUNET_MESH_Multicast *) message;
3805 /* Sanity check for message size */
3806 if (sizeof (struct GNUNET_MESH_Multicast) +
3807 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
3810 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3814 /* Tunnel exists? */
3815 tid = ntohl (data_msg->tid);
3816 t = tunnel_get_by_local_id (c, tid);
3820 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3824 /* Does client own tunnel? */
3825 if (t->client->handle != client)
3828 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3833 char buf[ntohs(message->size)];
3834 struct GNUNET_MESH_Multicast *copy;
3836 copy = (struct GNUNET_MESH_Multicast *)buf;
3837 memcpy(buf, message, ntohs(message->size));
3838 copy->oid = my_full_id;
3839 copy->tid = htonl(t->id.tid);
3840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3841 "MESH: calling generic handler...\n");
3842 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3845 /* receive done gets called when last copy is sent to a neighbor */
3850 * Functions to handle messages from clients
3852 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3853 {&handle_local_new_client, NULL,
3854 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3855 {&handle_local_tunnel_create, NULL,
3856 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3857 sizeof (struct GNUNET_MESH_TunnelMessage)},
3858 {&handle_local_tunnel_destroy, NULL,
3859 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3860 sizeof (struct GNUNET_MESH_TunnelMessage)},
3861 {&handle_local_connect_add, NULL,
3862 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3863 sizeof (struct GNUNET_MESH_PeerControl)},
3864 {&handle_local_connect_del, NULL,
3865 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3866 sizeof (struct GNUNET_MESH_PeerControl)},
3867 {&handle_local_connect_by_type, NULL,
3868 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3869 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3870 {&handle_local_unicast, NULL,
3871 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3872 {&handle_local_to_origin, NULL,
3873 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3874 {&handle_local_multicast, NULL,
3875 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3881 * To be called on core init/fail.
3883 * @param cls service closure
3884 * @param server handle to the server for this service
3885 * @param identity the public identity of this peer
3888 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3889 const struct GNUNET_PeerIdentity *identity)
3891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3892 core_handle = server;
3893 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3895 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3896 GNUNET_SCHEDULER_shutdown();
3902 * Method called whenever a given peer connects.
3904 * @param cls closure
3905 * @param peer peer identity this notification is about
3906 * @param atsi performance data for the connection
3909 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3910 const struct GNUNET_ATS_Information *atsi)
3912 struct MeshPeerInfo *peer_info;
3913 struct MeshPeerPath *path;
3915 #if MESH_DEBUG_CONNECTION
3916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3918 GNUNET_h2s(&my_full_id.hashPubKey));
3920 peer_info = peer_info_get (peer);
3921 if (myid == peer_info->id)
3923 #if MESH_DEBUG_CONNECTION
3924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3928 #if MESH_DEBUG_CONNECTION
3931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3932 GNUNET_h2s(&peer->hashPubKey));
3935 path = path_new (2);
3936 path->peers[0] = myid;
3937 path->peers[1] = peer_info->id;
3938 GNUNET_PEER_change_rc(myid, 1);
3939 GNUNET_PEER_change_rc(peer_info->id, 1);
3940 path_add_to_peer (peer_info, path);
3945 * Method called whenever a peer disconnects.
3947 * @param cls closure
3948 * @param peer peer identity this notification is about
3951 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3953 struct MeshPeerInfo *pi;
3956 #if MESH_DEBUG_CONNECTION
3957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3959 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3965 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3967 peer_info_cancel_transmission(pi, i);
3969 path_remove_from_peer (pi, pi->id, myid);
3970 #if MESH_DEBUG_CONNECTION
3973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3980 /******************************************************************************/
3981 /************************ MAIN FUNCTIONS ****************************/
3982 /******************************************************************************/
3985 * Iterator over hash map entries.
3987 * @param cls closure
3988 * @param key current key code
3989 * @param value value in the hash map
3990 * @return GNUNET_YES if we should continue to
3995 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
3997 struct MeshTunnel *t = value;
4003 * Task run during shutdown.
4009 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4012 /* TODO: destroy tunnels? */
4013 if (core_handle != NULL)
4015 GNUNET_CORE_disconnect (core_handle);
4018 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
4019 if (dht_handle != NULL)
4021 GNUNET_DHT_disconnect (dht_handle);
4026 GNUNET_SERVER_notification_context_destroy (nc);
4029 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4031 GNUNET_SCHEDULER_cancel (announce_id_task);
4032 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4038 * Process mesh requests.
4040 * @param cls closure
4041 * @param server the initialized server
4042 * @param c configuration to use
4045 run (void *cls, struct GNUNET_SERVER_Handle *server,
4046 const struct GNUNET_CONFIGURATION_Handle *c)
4048 struct MeshPeerInfo *peer;
4049 struct MeshPeerPath *p;
4052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4053 server_handle = server;
4054 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4055 CORE_QUEUE_SIZE, /* queue size */
4056 NULL, /* Closure passed to MESH functions */
4057 &core_init, /* Call core_init once connected */
4058 &core_connect, /* Handle connects */
4059 &core_disconnect, /* remove peers on disconnects */
4060 NULL, /* Don't notify about all incoming messages */
4061 GNUNET_NO, /* For header only in notification */
4062 NULL, /* Don't notify about all outbound messages */
4063 GNUNET_NO, /* For header-only out notification */
4064 core_handlers); /* Register these handlers */
4065 if (core_handle == NULL)
4068 GNUNET_SCHEDULER_shutdown ();
4073 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4076 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4078 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4079 GNUNET_SCHEDULER_shutdown ();
4082 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4083 GNUNET_free (keyfile);
4084 if (my_private_key == NULL)
4086 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4087 _("Mesh service could not access hostkey. Exiting.\n"));
4088 GNUNET_SCHEDULER_shutdown ();
4091 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4092 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4093 &my_full_id.hashPubKey);
4094 myid = GNUNET_PEER_intern (&my_full_id);
4096 dht_handle = GNUNET_DHT_connect (c, 64);
4097 if (dht_handle == NULL)
4103 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4105 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4106 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4107 peers = GNUNET_CONTAINER_multihashmap_create (32);
4108 applications = GNUNET_CONTAINER_multihashmap_create (32);
4109 types = GNUNET_CONTAINER_multihashmap_create (32);
4111 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4112 nc = GNUNET_SERVER_notification_context_create (server_handle,
4114 GNUNET_SERVER_disconnect_notify (server_handle,
4115 &handle_local_client_disconnect,
4120 clients_tail = NULL;
4125 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4126 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4128 /* Create a peer_info for the local peer */
4129 peer = peer_info_get(&my_full_id);
4132 path_add_to_peer(peer, p);
4134 /* Scheduled the task to clean up when shutdown is called */
4135 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4142 * The main function for the mesh service.
4144 * @param argc number of arguments from the command line
4145 * @param argv command line arguments
4146 * @return 0 ok, 1 on error
4149 main (int argc, char *const *argv)
4154 // fprintf (stderr, "main ()\n");
4156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4159 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
4163 // fprintf (stderr, "main () END\n");