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 * Closure given to the DHT GET
153 struct MeshPathInfo *dhtgetcls;
156 * Handles to stop queued transmissions for this peer
158 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
161 * Pointer to info stuctures used as cls for queued transmissions
163 void *infos[CORE_QUEUE_SIZE];
166 * Type of message being in each transmission
168 uint16_t types[CORE_QUEUE_SIZE];
171 * Array of tunnels this peer participates in
172 * (most probably a small amount, therefore not a hashmap)
173 * When the path to the peer changes, notify these tunnels to let them
174 * re-adjust their path trees.
176 struct MeshTunnel **tunnels;
179 * Number of tunnels this peers participates in
181 unsigned int ntunnels;
186 * Data scheduled to transmit (to local client or remote peer)
193 struct MeshQueue *next;
194 struct MeshQueue *prev;
197 * Target of the data (NULL if target is client)
199 struct MeshPeerInfo *peer;
202 * Client to send the data to (NULL if target is peer)
204 struct MeshClient *client;
207 * Size of the message to transmit
212 * How old is the data?
214 struct GNUNET_TIME_Absolute timestamp;
219 struct GNUNET_MessageHeader *data;
223 * Globally unique tunnel identification (owner + number)
224 * DO NOT USE OVER THE NETWORK
229 * Node that owns the tunnel
234 * Tunnel number to differentiate all the tunnels owned by the node oid
235 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
237 MESH_TunnelNumber tid;
241 struct MeshClient; /* FWD declaration */
244 * Struct containing all information regarding a tunnel
245 * For an intermediate node the improtant info used will be:
246 * - id Tunnel unique identification
247 * - paths[0] To know where to send it next
248 * - metainfo: ready, speeds, accounting
255 struct MESH_TunnelID id;
258 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
260 MESH_TunnelNumber local_tid;
263 * Last time the tunnel was used
265 struct GNUNET_TIME_Absolute timestamp;
268 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
269 * containing peers added by id or by type, not intermediate peers.
271 struct GNUNET_CONTAINER_MultiHashMap *peers;
274 * Number of peers that are connected and potentially ready to receive data
276 unsigned int peers_ready;
279 * Number of peers that have been added to the tunnel
281 unsigned int peers_total;
284 * Client owner of the tunnel, if any
286 struct MeshClient *client;
289 * Messages ready to transmit
291 struct MeshQueue *queue_head;
292 struct MeshQueue *queue_tail;
297 struct MeshTunnelTree *tree;
300 * Application type we are looking for in this tunnel
302 GNUNET_MESH_ApplicationType type;
305 * Used to search peers offering a service
307 struct GNUNET_DHT_GetHandle *dht_get_type;
310 * Task to keep the used paths alive
312 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
315 * Task to destroy the tunnel after timeout
317 * FIXME: merge the two? a tunnel will have either
318 * a path refresh OR a timeout, never both!
320 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
325 * Info needed to work with tunnel paths and peers
332 struct MeshTunnel *t;
335 * Neighbouring peer to whom we send the packet to
337 struct MeshPeerInfo *peer;
342 struct MeshPeerPath *path;
345 * Position in peer's transmit queue
352 * Struct containing information about a client of the service
359 struct MeshClient *next;
360 struct MeshClient *prev;
363 * Tunnels that belong to this client, indexed by local id
365 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
368 * Handle to communicate with the client
370 struct GNUNET_SERVER_Client *handle;
373 * Applications that this client has claimed to provide
375 struct GNUNET_CONTAINER_MultiHashMap *apps;
378 * Messages that this client has declared interest in
380 struct GNUNET_CONTAINER_MultiHashMap *types;
384 * ID of the client, for debug messages
393 /******************************************************************************/
394 /************************ DEBUG FUNCTIONS ****************************/
395 /******************************************************************************/
399 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
400 * @param cls string to print
401 * @param tc task context
404 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
408 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
416 /******************************************************************************/
417 /*********************** GLOBAL VARIABLES ****************************/
418 /******************************************************************************/
423 static struct MeshClient *clients;
424 static struct MeshClient *clients_tail;
427 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
429 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
432 * Tunnels incoming, indexed by MESH_TunnelNumber
433 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
435 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
438 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
440 static struct GNUNET_CONTAINER_MultiHashMap *peers;
443 * Handle to communicate with core
445 static struct GNUNET_CORE_Handle *core_handle;
450 static struct GNUNET_DHT_Handle *dht_handle;
455 static struct GNUNET_SERVER_Handle *server_handle;
458 * Notification context, to send messages to local clients
460 static struct GNUNET_SERVER_NotificationContext *nc;
463 * Local peer own ID (memory efficient handle)
465 static GNUNET_PEER_Id myid;
468 * Local peer own ID (full value)
470 static struct GNUNET_PeerIdentity my_full_id;
475 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
480 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
483 * Tunnel ID for the next created tunnel (global tunnel number)
485 static MESH_TunnelNumber next_tid;
488 * Tunnel ID for the next incoming tunnel (local tunnel number)
490 static MESH_TunnelNumber next_local_tid;
493 * All application types provided by this peer
495 static struct GNUNET_CONTAINER_MultiHashMap *applications;
498 * All message types clients of this peer are interested in
500 static struct GNUNET_CONTAINER_MultiHashMap *types;
503 * Task to periodically announce provided applications
505 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
508 * Task to periodically announce itself in the network
510 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
513 unsigned int next_client_id;
517 /******************************************************************************/
518 /************************ ITERATORS ****************************/
519 /******************************************************************************/
521 /* FIXME move iterators here */
524 /******************************************************************************/
525 /************************ PERIODIC FUNCTIONS ****************************/
526 /******************************************************************************/
529 * Announce iterator over for each application provided by the peer
532 * @param key current key code
533 * @param value value in the hash map
534 * @return GNUNET_YES if we should continue to
539 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
541 /* FIXME are hashes in multihash map equal on all aquitectures? */
542 GNUNET_DHT_put (dht_handle,
545 GNUNET_DHT_RO_RECORD_ROUTE |
546 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
547 GNUNET_BLOCK_TYPE_TEST,
548 sizeof (struct GNUNET_PeerIdentity),
549 (const char *) &my_full_id,
551 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
552 &mesh_debug, "DHT_put for app completed");
554 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
556 APP_ANNOUNCE_TIME, NULL, NULL);
563 * Periodically announce what applications are provided by local clients
566 * @param tc task context
569 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
571 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
573 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
577 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
579 announce_applications_task =
580 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
588 * Periodically announce self id in the DHT
591 * @param tc task context
594 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
596 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
598 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
602 * - Set data expiration in function of X
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
606 GNUNET_h2s_full (&my_full_id.hashPubKey));
607 GNUNET_DHT_put (dht_handle, /* DHT handle */
608 &my_full_id.hashPubKey, /* Key to use */
609 10U, /* Replication level */
610 GNUNET_DHT_RO_RECORD_ROUTE |
611 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
612 GNUNET_BLOCK_TYPE_TEST, /* Block type */
613 sizeof(my_full_id), /* Size of the data */
614 (char *)&my_full_id, /* Data itself */
615 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
616 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
618 &mesh_debug, "DHT_put for id completed");
620 NULL, /* Continuation */
621 NULL); /* Continuation closure */
624 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
629 * Function to process paths received for a new peer addition. The recorded
630 * paths form the initial tunnel, which can be optimized later.
631 * Called on each result obtained for the DHT search.
634 * @param exp when will this value expire
635 * @param key key of the result
636 * @param type type of the result
637 * @param size number of bytes in data
638 * @param data pointer to the result data
641 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
642 const GNUNET_HashCode * key,
643 const struct GNUNET_PeerIdentity *get_path,
644 unsigned int get_path_length,
645 const struct GNUNET_PeerIdentity *put_path,
646 unsigned int put_path_length,
647 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
650 /******************************************************************************/
651 /****************** GENERAL HELPER FUNCTIONS ************************/
652 /******************************************************************************/
655 * Check if client has registered with the service and has not disconnected
657 * @param client the client to check
659 * @return non-NULL if client exists in the global DLL
661 static struct MeshClient *
662 client_get (struct GNUNET_SERVER_Client *client)
664 struct MeshClient *c;
669 if (c->handle == client)
678 * Checks if a given client has subscribed to certain message type
680 * @param message_type Type of message to check
681 * @param c Client to check
683 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
688 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
692 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
693 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
697 client_allow_send(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
699 struct MeshDataDescriptor *info = cls;
701 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
703 info->timeout_task = GNUNET_SCHEDULER_NO_TASK;
704 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
709 * Search for a tunnel by global ID using full PeerIdentities
711 * @param oid owner of the tunnel
712 * @param tid global tunnel number
714 * @return tunnel handler, NULL if doesn't exist
716 static struct MeshTunnel *
717 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
721 * Send the message to all clients that have subscribed to its type
723 * @param msg Pointer to the message itself
724 * @return number of clients this message was sent to
727 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
728 const struct GNUNET_MessageHeader *payload)
730 struct GNUNET_PeerIdentity *oid;
731 struct MeshClient *c;
732 struct MeshTunnel *t;
733 MESH_TunnelNumber *tid;
736 char cbuf[htons(msg->size)];
738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
739 type = ntohs (payload->type);
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
742 memcpy (cbuf, msg, sizeof(cbuf));
743 switch (htons(msg->type))
745 struct GNUNET_MESH_Unicast *uc;
746 struct GNUNET_MESH_Multicast *mc;
747 struct GNUNET_MESH_ToOrigin *to;
749 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
750 uc = (struct GNUNET_MESH_Unicast *) cbuf;
754 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
755 mc = (struct GNUNET_MESH_Multicast *) cbuf;
759 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
760 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
768 t = tunnel_get (oid, ntohl(*tid));
774 *tid = htonl (t->local_tid);
775 for (count = 0, c = clients; c != NULL; c = c->next)
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
778 if (client_is_subscribed (type, c))
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
782 GNUNET_SERVER_notification_context_unicast (
785 (struct GNUNET_MessageHeader *) cbuf,
794 * Notify the client that owns the tunnel that a peer has connected to it
795 * (the requested path to it has been confirmed).
797 * @param t Tunnel whose owner to notify
798 * @param id Short id of the peer that has connected
801 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
803 struct GNUNET_MESH_PeerControl pc;
805 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
806 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
807 pc.tunnel_id = htonl (t->local_tid);
808 GNUNET_PEER_resolve (id, &pc.peer);
809 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
810 &pc.header, GNUNET_NO);
815 * Notify all clients (not depending on registration status) that the incoming
816 * tunnel is no longer valid.
818 * @param t Tunnel that was destroyed.
821 send_clients_tunnel_destroy (struct MeshTunnel *t)
823 struct GNUNET_MESH_TunnelMessage msg;
825 msg.header.size = htons (sizeof (msg));
826 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
827 msg.tunnel_id = htonl (t->local_tid);
828 GNUNET_SERVER_notification_context_broadcast(nc, &msg.header, GNUNET_NO);
833 * Function called to notify a client about the socket
834 * being ready to queue more data. "buf" will be
835 * NULL and "size" zero if the socket was closed for
836 * writing in the meantime.
839 * @param size number of bytes available in buf
840 * @param buf where the callee should write the message
841 * @return number of bytes written to buf
844 send_core_create_path (void *cls, size_t size, void *buf);
848 * Function called to notify a client about the socket
849 * being ready to queue more data. "buf" will be
850 * NULL and "size" zero if the socket was closed for
851 * writing in the meantime.
853 * @param cls closure (data itself)
854 * @param size number of bytes available in buf
855 * @param buf where the callee should write the message
857 * @return number of bytes written to buf
860 send_core_data_multicast (void *cls, size_t size, void *buf);
864 * Cancel a core transmission that was already requested and free all resources
865 * associated to the request.
867 * @param peer PeeInfo of the peer whose transmission is cancelled.
868 * @param i Position of the transmission to be cancelled.
871 peer_info_cancel_transmission(struct MeshPeerInfo *peer, unsigned int i)
873 if (NULL != peer->core_transmit[i])
875 struct MeshDataDescriptor *dd;
876 struct MeshPathInfo *path_info;
880 struct GNUNET_PeerIdentity id;
882 GNUNET_PEER_resolve (peer->id, &id);
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
884 "MESH: Cancelling data transmission at %s [%u]\n",
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888 "MESH: message type %u\n",
892 /* TODO: notify that tranmission has failed */
893 switch (peer->types[i])
895 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
896 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
897 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
900 if (0 == --(*dd->copies))
902 GNUNET_free (dd->copies);
903 GNUNET_free (dd->data);
906 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
908 path_info = peer->infos[i];
909 path_destroy(path_info->path);
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
915 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
916 peer->core_transmit[i] = NULL;
917 GNUNET_free (peer->infos[i]);
923 * Get a unused CORE slot to transmit a message to a peer. If all the slots
924 * are used, cancel one and return it's position.
926 * @param peer PeerInfo of the neighbor we want to transmit to.
928 * @return The index of an available slot to transmit to the neighbor.
931 peer_info_transmit_slot (struct MeshPeerInfo *peer)
935 for (i = 0; peer->core_transmit[i]; i++)
937 if (i == (CORE_QUEUE_SIZE - 1))
939 /* All positions are taken! Overwriting! */
941 peer_info_cancel_transmission(peer, 0);
950 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
951 * and insert it in the appropiate structures if the peer is not known yet.
953 * @param peer Full identity of the peer.
955 * @return Existing or newly created peer info.
957 static struct MeshPeerInfo *
958 peer_info_get (const struct GNUNET_PeerIdentity *peer)
960 struct MeshPeerInfo *peer_info;
962 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
963 if (NULL == peer_info)
966 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
967 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
968 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
969 peer_info->id = GNUNET_PEER_intern (peer);
977 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
978 * and insert it in the appropiate structures if the peer is not known yet.
980 * @param peer Short identity of the peer.
982 * @return Existing or newly created peer info.
984 static struct MeshPeerInfo *
985 peer_info_get_short (const GNUNET_PEER_Id peer)
987 struct GNUNET_PeerIdentity id;
989 GNUNET_PEER_resolve(peer, &id);
990 return peer_info_get(&id);
995 * Iterator to remove the tunnel from the list of tunnels a peer participates
998 * @param cls Closure (tunnel info)
999 * @param key GNUNET_PeerIdentity of the peer (unused)
1000 * @param value PeerInfo of the peer
1002 * @return always GNUNET_YES, to keep iterating
1005 peer_info_delete_tunnel (void* cls, const GNUNET_HashCode* key, void* value)
1007 struct MeshTunnel *t = cls;
1008 struct MeshPeerInfo *peer = value;
1011 for (i = 0; i < peer->ntunnels; i++)
1013 if (0 == memcmp(&peer->tunnels[i]->id,
1015 sizeof(struct MESH_TunnelID)))
1018 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1019 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1028 * Core callback to write a
1030 * @param cls Closure (MeshDataDescriptor with data in "data" member).
1031 * @param size Number of bytes available in buf.
1032 * @param buf Where the to write the message.
1034 * @return number of bytes written to buf
1037 send_core_data_raw (void *cls, size_t size, void *buf)
1039 struct MeshDataDescriptor *info = cls;
1040 struct GNUNET_MessageHeader *msg;
1043 GNUNET_assert (NULL != info);
1044 GNUNET_assert (NULL != info->data);
1045 msg = (struct GNUNET_MessageHeader *) info->data;
1046 total_size = ntohs (msg->size);
1048 if (total_size > size)
1050 struct GNUNET_PeerIdentity id;
1052 GNUNET_PEER_resolve (info->peer->id, &id);
1053 info->peer->core_transmit[info->handler_n] =
1054 GNUNET_CORE_notify_transmit_ready(core_handle,
1057 GNUNET_TIME_UNIT_FOREVER_REL,
1060 &send_core_data_raw,
1064 info->peer->core_transmit[info->handler_n] = NULL;
1065 memcpy (buf, msg, total_size);
1066 GNUNET_free (info->data);
1073 * Sends an already built message to a peer, properly registrating
1074 * all used resources.
1076 * @param message Message to send.
1077 * @param peer Short ID of the neighbor whom to send the message.
1080 send_message (const struct GNUNET_MessageHeader *message,
1081 const struct GNUNET_PeerIdentity *peer)
1083 struct MeshDataDescriptor *info;
1084 struct MeshPeerInfo *neighbor;
1088 size = ntohs (message->size);
1089 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1090 info->data = GNUNET_malloc (size);
1091 memcpy (info->data, message, size);
1092 neighbor = peer_info_get (peer);
1093 i = peer_info_transmit_slot (neighbor);
1094 info->handler_n = i;
1095 info->peer = neighbor;
1096 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1097 neighbor->infos[i] = info;
1098 neighbor->core_transmit[i] =
1099 GNUNET_CORE_notify_transmit_ready(core_handle,
1102 GNUNET_TIME_UNIT_FOREVER_REL,
1105 &send_core_data_raw,
1112 * Sends a CREATE PATH message for a path to a peer, properly registrating
1113 * all used resources.
1115 * @param peer PeerInfo of the final peer for whom this path is being created.
1116 * @param p Path itself.
1117 * @param t Tunnel for which the path is created.
1120 send_create_path (struct MeshPeerInfo *peer,
1121 struct MeshPeerPath *p,
1122 struct MeshTunnel *t)
1124 struct GNUNET_PeerIdentity id;
1125 struct MeshPathInfo *path_info;
1126 struct MeshPeerInfo *neighbor;
1131 p = tree_get_path_to_peer(t->tree, peer->id);
1138 for (i = 0; i < p->length; i++)
1140 if (p->peers[i] == myid)
1143 if (i >= p->length - 1)
1149 GNUNET_PEER_resolve(p->peers[i + 1], &id);
1151 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1152 path_info->path = p;
1154 neighbor = peer_info_get(&id);
1155 path_info->peer = neighbor;
1156 path_info->pos = peer_info_transmit_slot(neighbor);
1157 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1158 neighbor->infos[path_info->pos] = path_info;
1159 neighbor->core_transmit[path_info->pos] =
1160 GNUNET_CORE_notify_transmit_ready (
1161 core_handle, /* handle */
1164 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1166 sizeof (struct GNUNET_MESH_ManipulatePath)
1167 + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1168 &send_core_create_path, /* callback */
1169 path_info); /* cls */
1174 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1176 * @param t Tunnel whose path to destroy.
1177 * @param destination Short ID of the peer to whom the path to destroy.
1180 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1182 struct GNUNET_MESH_ManipulatePath *msg;
1183 struct GNUNET_PeerIdentity *pi;
1184 struct MeshPeerPath *p;
1188 p = tree_get_path_to_peer(t->tree, destination);
1194 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1195 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1196 msg = GNUNET_malloc (size);
1197 msg->header.size = htons (size);
1198 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1199 msg->tid = htonl (t->id.tid);
1200 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1201 for (i = 0; i < p->length; i++)
1203 GNUNET_PEER_resolve(p->peers[i], &pi[i]);
1205 send_message (&msg->header, path_get_first_hop(t->tree, destination));
1211 * Try to establish a new connection to this peer.
1212 * Use the best path for the given tunnel.
1213 * If the peer doesn't have any path to it yet, try to get one.
1214 * If the peer already has some path, send a CREATE PATH towards it.
1216 * @param peer PeerInfo of the peer.
1217 * @param t Tunnel for which to create the path, if possible.
1220 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1222 struct MeshPeerPath *p;
1223 struct MeshPathInfo *path_info;
1225 if (NULL != peer->path_head)
1227 p = tree_get_path_to_peer(t->tree, peer->id);
1230 send_create_path(peer, p, t);
1235 send_client_peer_connected(t, myid);
1238 else if (NULL == peer->dhtget)
1240 struct GNUNET_PeerIdentity id;
1242 GNUNET_PEER_resolve(peer->id, &id);
1243 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1244 path_info->peer = peer;
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 "MESH: Starting DHT GET for peer %s\n",
1249 peer->dhtgetcls = path_info;
1251 GNUNET_DHT_get_start(dht_handle, /* handle */
1252 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1253 GNUNET_BLOCK_TYPE_TEST, /* type */
1254 &id.hashPubKey, /* key to search */
1255 4, /* replication level */
1256 GNUNET_DHT_RO_RECORD_ROUTE |
1257 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1259 0, /* xquery bits */
1260 &dht_get_id_handler,
1263 /* Otherwise, there is no path but the DHT get is already started. */
1268 * Destroy the peer_info and free any allocated resources linked to it
1270 * @param pi The peer_info to destroy.
1272 * @return GNUNET_OK on success
1275 peer_info_destroy (struct MeshPeerInfo *pi)
1277 struct GNUNET_PeerIdentity id;
1278 GNUNET_HashCode hash;
1279 struct MeshPeerPath *p;
1280 struct MeshPeerPath *nextp;
1283 GNUNET_PEER_resolve (pi->id, &id);
1284 GNUNET_PEER_change_rc (pi->id, -1);
1285 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
1287 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
1288 if (NULL != pi->dhtget)
1290 GNUNET_DHT_get_stop(pi->dhtget);
1291 GNUNET_free (pi->dhtgetcls);
1293 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1295 peer_info_cancel_transmission(pi, i);
1301 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1311 * Notify a tunnel that a connection has broken that affects at least
1312 * some of its peers.
1314 * @param t Tunnel affected.
1315 * @param peer Peer that (at least) has been affected by the disconnection.
1316 * @param p1 Peer that got disconnected from p2.
1317 * @param p2 Peer that got disconnected from p1.
1319 * @return Short ID of the peer disconnected (either p1 or p2).
1320 * 0 if the tunnel remained unaffected.
1322 static GNUNET_PEER_Id
1323 tunnel_notify_connection_broken (struct MeshTunnel *t,
1324 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1329 * Remove all paths that rely on a direct connection between p1 and p2
1330 * from the peer itself and notify all tunnels about it.
1332 * @param peer PeerInfo of affected peer.
1333 * @param p1 GNUNET_PEER_Id of one peer.
1334 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1337 * TODO: optimize (see below)
1340 path_remove_from_peer (struct MeshPeerInfo *peer,
1344 struct MeshPeerPath *p;
1345 struct MeshPeerPath *aux;
1346 struct MeshPeerInfo *peer_d;
1348 unsigned int destroyed;
1354 p = peer->path_head;
1358 for (i = 0; i < (p->length - 1); i++)
1360 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1361 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1363 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1374 for (i = 0; i < peer->ntunnels; i++)
1376 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
1378 * Problem: one or more peers have been deleted from the tunnel tree.
1379 * We don't know who they are to try to add them again.
1380 * We need to try to find a new path for each of the disconnected peers.
1381 * Some of them might already have a path to reach them that does not
1382 * involve p1 and p2. Adding all anew might render in a better tree than
1383 * the trivial immediate fix.
1385 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1386 * its children will be reachable trough him.
1388 peer_d = peer_info_get_short(d);
1391 for (p = peer_d->path_head; NULL != p; p = p->next)
1393 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
1401 /* No callback, as peer will be already disconnected */
1402 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
1406 peer_info_connect (peer_d, peer->tunnels[i]);
1413 * Add the path to the peer and update the path used to reach it in case this
1416 * @param peer_info Destination peer to add the path to.
1417 * @param path New path to add. Last peer must be the peer in arg 1.
1418 * Path will be either used of freed if already known.
1420 * TODO: trim the part from origin to us? Add it as path to origin?
1423 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1425 struct MeshPeerPath *aux;
1429 if (NULL == peer_info || NULL == path)
1435 l = path_get_length (path);
1442 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1444 l2 = path_get_length (aux);
1447 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1448 peer_info->path_tail, aux, path);
1452 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1459 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1466 * Add the path to the origin peer and update the path used to reach it in case
1467 * this is the shortest.
1468 * The path is given in peer_info -> destination, therefore we turn the path
1469 * upside down first.
1471 * @param peer_info Peer to add the path to, being the origin of the path.
1472 * @param path New path to add after being inversed.
1475 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1478 path_add_to_peer (peer_info, path);
1483 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1484 * to obtain a local peer -> destination path and interning the peer ids.
1486 * @return Newly allocated and created path
1488 static struct MeshPeerPath *
1489 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1490 unsigned int get_path_length,
1491 const struct GNUNET_PeerIdentity *put_path,
1492 unsigned int put_path_length)
1494 struct MeshPeerPath *p;
1500 GNUNET_PEER_change_rc(myid, 1);
1501 i = get_path_length;
1502 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1503 for (i--; i >= 0; i--)
1505 id = GNUNET_PEER_intern (&get_path[i]);
1506 if (p->length > 0 && id == p->peers[p->length - 1])
1508 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1509 GNUNET_PEER_change_rc(id, -1);
1513 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1514 "MESH: Adding from GET: %s.\n",
1515 GNUNET_i2s(&get_path[i]));
1517 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1518 p->peers[p->length - 1] = id;
1521 i = put_path_length;
1522 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1523 for (i--; i >= 0; i--)
1525 id = GNUNET_PEER_intern (&put_path[i]);
1528 /* PUT path went through us, so discard the path up until now and start
1529 * from here to get a much shorter (and loop-free) path.
1534 if (p->length > 0 && id == p->peers[p->length - 1])
1536 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1537 GNUNET_PEER_change_rc(id, -1);
1541 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1542 "MESH: Adding from PUT: %s.\n",
1543 GNUNET_i2s(&put_path[i]));
1545 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1546 p->peers[p->length - 1] = id;
1550 if (get_path_length > 0)
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "MESH: (first of GET: %s)\n",
1553 GNUNET_h2s_full(&get_path[0].hashPubKey));
1554 if (put_path_length > 0)
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556 "MESH: (first of PUT: %s)\n",
1557 GNUNET_h2s_full(&put_path[0].hashPubKey));
1558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1559 "MESH: In total: %d hops\n",
1561 for (i = 0; i < p->length; i++)
1563 struct GNUNET_PeerIdentity peer_id;
1565 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 GNUNET_h2s_full(&peer_id.hashPubKey));
1577 * Send keepalive packets for a peer
1579 * @param cls Closure (tunnel for which to send the keepalive).
1580 * @param tc Notification context.
1582 * TODO: implement explicit multicast keepalive?
1585 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1589 * Search for a tunnel among the tunnels for a client
1591 * @param c the client whose tunnels to search in
1592 * @param tid the local id of the tunnel
1594 * @return tunnel handler, NULL if doesn't exist
1596 static struct MeshTunnel *
1597 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1599 GNUNET_HashCode hash;
1601 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1602 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1604 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1606 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1611 * Search for a tunnel by global ID using PEER_ID
1613 * @param pi owner of the tunnel
1614 * @param tid global tunnel number
1616 * @return tunnel handler, NULL if doesn't exist
1618 static struct MeshTunnel *
1619 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1621 struct MESH_TunnelID id;
1622 GNUNET_HashCode hash;
1627 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1628 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1633 * Search for a tunnel by global ID using full PeerIdentities
1635 * @param oid owner of the tunnel
1636 * @param tid global tunnel number
1638 * @return tunnel handler, NULL if doesn't exist
1640 static struct MeshTunnel *
1641 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1643 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1648 * Callback used to notify a client owner of a tunnel that a peer has
1649 * disconnected, most likely because of a path change.
1651 * @param n Node in the tree representing the disconnected peer
1654 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1656 struct MeshPeerInfo *peer;
1658 if (NULL != n->t->client && NULL != nc)
1660 struct GNUNET_MESH_PeerControl msg;
1661 msg.header.size = htons (sizeof (msg));
1662 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1663 msg.tunnel_id = htonl (n->t->local_tid);
1664 GNUNET_PEER_resolve (n->peer, &msg.peer);
1665 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1666 &msg.header, GNUNET_NO);
1668 peer = peer_info_get_short(n->peer);
1669 peer_info_connect(peer, n->t);
1674 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1675 * needed rescources.
1676 * If peer already exists, reevaluate shortest path and change if different.
1678 * @param t Tunnel we want to add a new peer to
1679 * @param peer PeerInfo of the peer being added
1683 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1685 struct GNUNET_PeerIdentity id;
1686 struct MeshPeerPath *best_p;
1687 struct MeshPeerPath *p;
1688 unsigned int best_cost;
1691 GNUNET_PEER_resolve(peer->id, &id);
1693 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1696 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1697 GNUNET_CONTAINER_multihashmap_put(
1701 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1704 if (NULL != (p = peer->path_head))
1707 best_cost = path_get_cost(t->tree, p);
1710 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1717 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1718 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1719 t->path_refresh_task =
1720 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1724 /* Start a DHT get if necessary */
1725 peer_info_connect(peer, t);
1730 * Add a path to a tunnel which we don't own, just to remember the next hop.
1731 * If destination node was already in the tunnel, the first hop information
1732 * will be replaced with the new path.
1734 * @param t Tunnel we want to add a new peer to
1735 * @param p Path to add
1736 * @param own_pos Position of local node in path.
1740 tunnel_add_path (struct MeshTunnel *t,
1741 struct MeshPeerPath *p,
1742 unsigned int own_pos)
1744 struct GNUNET_PeerIdentity id;
1746 GNUNET_assert (0 != own_pos);
1747 tree_add_path(t->tree, p, NULL);
1748 if (NULL == t->tree->me)
1749 t->tree->me = tree_find_peer(t->tree->root, p->peers[own_pos]);
1750 if (own_pos < p->length - 1)
1752 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1753 tree_update_first_hops(t->tree, t->tree->me, &id);
1759 * Notify a tunnel that a connection has broken that affects at least
1760 * some of its peers.
1762 * @param t Tunnel affected.
1763 * @param peer Peer that (at least) has been affected by the disconnection.
1764 * @param p1 Peer that got disconnected from p2.
1765 * @param p2 Peer that got disconnected from p1.
1767 * @return Short ID of the peer disconnected (either p1 or p2).
1768 * 0 if the tunnel remained unaffected.
1770 static GNUNET_PEER_Id
1771 tunnel_notify_connection_broken (struct MeshTunnel *t,
1772 struct MeshPeerInfo *peer,
1776 return tree_notify_connection_broken (t->tree, p1, p2,
1777 ¬ify_peer_disconnected);
1782 * Send a message in a tunnel in multicast, sending a copy to each child node
1783 * down the local one in the tunnel tree.
1785 * @param t Tunnel in which to send the data.
1786 * @param msg Message to be sent
1788 * @return Number of copies sent.
1791 tunnel_send_multicast (struct MeshTunnel *t,
1792 const struct GNUNET_MessageHeader *msg)
1794 struct GNUNET_PeerIdentity neighbor;
1795 struct MeshDataDescriptor *info;
1796 struct MeshTunnelTreeNode *n;
1797 unsigned int *copies;
1802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1803 "MESH: sending a multicast packet...\n");
1804 size = ntohs (msg->size);
1805 GNUNET_assert (NULL != t->tree->me);
1806 n = t->tree->me->children_head;
1809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1810 "MESH: no children in the tree, no one to send.\n");
1813 copies = GNUNET_malloc (sizeof (unsigned int));
1814 for (*copies = 0; NULL != n; n = n->next)
1816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (%u copies)\n", *copies);
1817 n = t->tree->me->children_head;
1818 data = GNUNET_malloc (size);
1819 memcpy (data, msg, size);
1822 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1825 info->copies = copies;
1826 if (NULL != t->client)
1828 info->client = t->client->handle;
1829 info->timeout_task = GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT,
1833 info->destination = n->peer;
1834 GNUNET_PEER_resolve (n->peer, &neighbor);
1835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1836 "MESH: sending to %s...\n",
1837 GNUNET_i2s (&neighbor));
1838 info->peer = peer_info_get(&neighbor);
1839 GNUNET_assert (NULL != info->peer);
1840 i = peer_info_transmit_slot(info->peer);
1841 info->handler_n = i;
1842 info->peer->infos[i] = info;
1843 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1844 info->peer->core_transmit[i] =
1845 GNUNET_CORE_notify_transmit_ready (core_handle,
1848 GNUNET_TIME_UNIT_FOREVER_REL,
1851 &send_core_data_multicast, info);
1859 * Send a message to all peers in this tunnel that the tunnel is no longer
1862 * @param t The tunnel whose peers to notify.
1865 tunnel_send_destroy (struct MeshTunnel *t)
1867 struct GNUNET_MESH_TunnelDestroy msg;
1869 msg.header.size = htons (sizeof (msg));
1870 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
1871 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1872 msg.tid = htonl (t->id.tid);
1873 tunnel_send_multicast (t, &msg.header);
1879 * Destroy the tunnel and free any allocated resources linked to it
1881 * @param t the tunnel to destroy
1883 * @return GNUNET_OK on success
1886 tunnel_destroy (struct MeshTunnel *t)
1888 struct MeshClient *c;
1889 struct MeshQueue *q;
1890 struct MeshQueue *qn;
1891 GNUNET_HashCode hash;
1900 struct GNUNET_PeerIdentity id;
1902 GNUNET_PEER_resolve(t->id.oid, &id);
1903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1904 "MESH: destroying tunnel %s [%x]\n",
1908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1912 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1913 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1918 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1920 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1924 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1926 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1927 GNUNET_break (GNUNET_YES ==
1928 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t));
1931 if (NULL != t->peers)
1933 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
1934 &peer_info_delete_tunnel, t);
1935 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1940 if (NULL != q->data)
1941 GNUNET_free (q->data);
1945 /* TODO cancel core transmit ready in case it was active */
1947 tree_destroy(t->tree);
1948 if (NULL != t->dht_get_type)
1949 GNUNET_DHT_get_stop(t->dht_get_type);
1950 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
1951 GNUNET_SCHEDULER_cancel(t->timeout_task);
1952 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
1953 GNUNET_SCHEDULER_cancel(t->path_refresh_task);
1960 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1961 * that are no longer needed, as well as nodes of no longer reachable peers.
1962 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1964 * @param t Tunnel from which to remove the path.
1965 * @param p Peer which should be removed.
1968 tunnel_delete_peer (struct MeshTunnel *t,
1969 GNUNET_PEER_Id peer)
1971 GNUNET_break (GNUNET_OK == tree_del_peer (t->tree, peer, NULL));
1972 if (NULL == t->tree->root)
1978 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1979 * client when the client disconnects.
1981 * @param cls closure (client that is disconnecting)
1982 * @param key the hash of the local tunnel id (used to access the hashmap)
1983 * @param value the value stored at the key (tunnel to destroy)
1985 * @return GNUNET_OK on success
1988 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1990 struct MeshTunnel *t = value;
1993 if (NULL != t->dht_get_type)
1995 GNUNET_DHT_get_stop (t->dht_get_type);
1997 r = tunnel_destroy (t);
2003 * Timeout function, destroys tunnel if called
2005 * @param cls Closure (tunnel to destroy).
2006 * @param tc TaskContext
2009 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2011 struct MeshTunnel *t = cls;
2013 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2015 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2020 * Resets the tunnel timeout. Starts it if no timeout was running.
2022 * @param t Tunnel whose timeout to reset.
2025 tunnel_reset_timeout (struct MeshTunnel *t)
2027 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2028 GNUNET_SCHEDULER_cancel (t->timeout_task);
2029 t->timeout_task = GNUNET_SCHEDULER_add_delayed (
2030 GNUNET_TIME_relative_multiply(REFRESH_PATH_TIME, 4),
2036 /******************************************************************************/
2037 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2038 /******************************************************************************/
2041 * Function called to notify a client about the socket
2042 * being ready to queue more data. "buf" will be
2043 * NULL and "size" zero if the socket was closed for
2044 * writing in the meantime.
2046 * @param cls closure
2047 * @param size number of bytes available in buf
2048 * @param buf where the callee should write the message
2049 * @return number of bytes written to buf
2052 send_core_create_path (void *cls, size_t size, void *buf)
2054 struct MeshPathInfo *info = cls;
2055 struct GNUNET_MESH_ManipulatePath *msg;
2056 struct GNUNET_PeerIdentity *peer_ptr;
2057 struct MeshPeerInfo *peer = info->peer;
2058 struct MeshTunnel *t = info->t;
2059 struct MeshPeerPath *p = info->path;
2063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2064 "MESH: CREATE PATH sending...\n");
2066 sizeof (struct GNUNET_MESH_ManipulatePath) +
2067 p->length * sizeof (struct GNUNET_PeerIdentity);
2069 if (size < size_needed || NULL == buf)
2071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
2075 info->peer->core_transmit[info->pos] =
2076 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2077 GNUNET_TIME_UNIT_FOREVER_REL,
2078 path_get_first_hop (t->tree, peer->id),
2080 &send_core_create_path,
2084 info->peer->core_transmit[info->pos] = NULL;
2087 struct GNUNET_PeerIdentity id;
2089 GNUNET_PEER_resolve (peer->id, &id);
2090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2091 "MESH: setting core_transmit %s [%u] to NULL\n",
2096 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2097 msg->header.size = htons (size_needed);
2098 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2099 msg->tid = ntohl (t->id.tid);
2101 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2102 for (i = 0; i < p->length; i++)
2104 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2111 "MESH: CREATE PATH (%u bytes long) sent!\n",
2118 * Function called to notify a client about the socket
2119 * being ready to queue more data. "buf" will be
2120 * NULL and "size" zero if the socket was closed for
2121 * writing in the meantime.
2123 * @param cls closure (data itself)
2124 * @param size number of bytes available in buf
2125 * @param buf where the callee should write the message
2127 * @return number of bytes written to buf
2130 send_core_data_multicast (void *cls, size_t size, void *buf)
2132 struct MeshDataDescriptor *info = cls;
2135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2136 GNUNET_assert (NULL != info);
2137 GNUNET_assert (NULL != info->peer);
2138 total_size = info->size;
2139 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2141 if (total_size > size)
2144 struct GNUNET_PeerIdentity id;
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2147 "MESH: Multicast: retransmitting... (%u/%u)\n",
2149 GNUNET_PEER_resolve(info->peer->id, &id);
2150 info->peer->core_transmit[info->handler_n] =
2151 GNUNET_CORE_notify_transmit_ready (core_handle,
2154 GNUNET_TIME_UNIT_FOREVER_REL,
2157 &send_core_data_multicast,
2161 info->peer->core_transmit[info->handler_n] = NULL;
2162 info->peer->infos[info->handler_n] = NULL;
2163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2164 memcpy (buf, info->data, total_size);
2167 struct GNUNET_MESH_Multicast *mc;
2168 struct GNUNET_MessageHeader *mh;
2171 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2173 mc = (struct GNUNET_MESH_Multicast *) mh;
2174 mh = (struct GNUNET_MessageHeader *) &mc[1];
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2176 "MESH: multicast, payload type %u\n",
2178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2179 "MESH: multicast, payload size %u\n",
2184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2190 if (0 == --(*info->copies))
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
2193 if (NULL != info->client)
2195 if (GNUNET_SCHEDULER_NO_TASK != info->timeout_task)
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 "MESH: cancelling client timeout (%u)...\n",
2199 info->timeout_task);
2200 GNUNET_SCHEDULER_cancel(info->timeout_task);
2201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
2202 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
2205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing memory...\n");
2206 GNUNET_free (info->data);
2207 GNUNET_free (info->copies);
2209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2217 * Function called to notify a client about the socket
2218 * being ready to queue more data. "buf" will be
2219 * NULL and "size" zero if the socket was closed for
2220 * writing in the meantime.
2222 * @param cls closure (MeshDataDescriptor)
2223 * @param size number of bytes available in buf
2224 * @param buf where the callee should write the message
2225 * @return number of bytes written to buf
2228 send_core_path_ack (void *cls, size_t size, void *buf)
2230 struct MeshDataDescriptor *info = cls;
2231 struct GNUNET_MESH_PathACK *msg = buf;
2233 GNUNET_assert (NULL != info);
2236 info->peer->core_transmit[info->handler_n] = NULL;
2238 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2243 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2244 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2245 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2246 msg->tid = htonl (info->origin->tid);
2247 msg->peer_id = my_full_id;
2249 /* TODO add signature */
2251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2252 return sizeof (struct GNUNET_MESH_PathACK);
2256 /******************************************************************************/
2257 /******************** MESH NETWORK HANDLERS **************************/
2258 /******************************************************************************/
2262 * Core handler for path creation
2264 * @param cls closure
2265 * @param message message
2266 * @param peer peer identity this notification is about
2267 * @param atsi performance data
2269 * @return GNUNET_OK to keep the connection open,
2270 * GNUNET_SYSERR to close it (signal serious error)
2273 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2274 const struct GNUNET_MessageHeader *message,
2275 const struct GNUNET_ATS_Information *atsi)
2277 unsigned int own_pos;
2280 MESH_TunnelNumber tid;
2281 struct GNUNET_MESH_ManipulatePath *msg;
2282 struct GNUNET_PeerIdentity *pi;
2283 GNUNET_HashCode hash;
2284 struct MeshPeerPath *path;
2285 struct MeshPeerInfo *dest_peer_info;
2286 struct MeshPeerInfo *orig_peer_info;
2287 struct MeshTunnel *t;
2289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2290 "MESH: Received a path create msg [%s]\n",
2291 GNUNET_i2s(&my_full_id));
2292 size = ntohs (message->size);
2293 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2295 GNUNET_break_op (0);
2299 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2300 if (size % sizeof (struct GNUNET_PeerIdentity))
2302 GNUNET_break_op (0);
2305 size /= sizeof (struct GNUNET_PeerIdentity);
2308 GNUNET_break_op (0);
2311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2312 "MESH: path has %u hops.\n",
2314 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2316 tid = ntohl (msg->tid);
2317 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2318 t = tunnel_get (pi, tid);
2319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2320 "MESH: path is for tunnel %s [%X].\n",
2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2326 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2327 t->id.oid = GNUNET_PEER_intern (pi);
2329 t->local_tid = next_local_tid++;
2330 /* FIXME test if taken */
2331 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2332 t->tree = tree_new(t, t->id.oid);
2334 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2336 GNUNET_CONTAINER_multihashmap_put (
2340 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2346 tunnel_reset_timeout (t);
2347 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2349 GNUNET_CONTAINER_multihashmap_put (
2353 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2361 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2362 if (NULL == dest_peer_info)
2364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2365 "MESH: Creating PeerInfo for destination.\n");
2366 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2367 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2368 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2370 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2372 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2373 if (NULL == orig_peer_info)
2375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2376 "MESH: Creating PeerInfo for origin.\n");
2377 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2378 orig_peer_info->id = GNUNET_PEER_intern (pi);
2379 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2380 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2383 path = path_new (size);
2385 for (i = 0; i < size; i++)
2387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2388 "MESH: ... adding %s\n",
2389 GNUNET_i2s(&pi[i]));
2390 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2391 if (path->peers[i] == myid)
2394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2395 "MESH: Own position: %u\n", own_pos);
2398 /* cannot be self, must be 'not found' */
2399 /* create path: self not found in path through self */
2400 GNUNET_break_op (0);
2401 path_destroy (path);
2402 /* FIXME error. destroy tunnel? leave for timeout? */
2405 tunnel_add_path (t, path, own_pos);
2406 if (own_pos == size - 1)
2408 /* It is for us! Send ack. */
2409 struct GNUNET_MESH_TunnelNotification cmsg;
2410 struct MeshDataDescriptor *info;
2413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2414 "MESH: It's for us!\n");
2415 path_add_to_origin (orig_peer_info, path);
2416 t->peers = GNUNET_CONTAINER_multihashmap_create(4);
2417 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2419 &my_full_id.hashPubKey,
2420 peer_info_get(&my_full_id),
2421 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2422 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2423 info->origin = &t->id;
2424 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2425 GNUNET_assert (NULL != info->peer);
2426 j = peer_info_transmit_slot(info->peer);
2427 info->handler_n = j;
2428 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2429 info->peer->infos[j] = info;
2430 info->peer->core_transmit[j] =
2431 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2432 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2433 sizeof (struct GNUNET_MESH_PathACK),
2434 &send_core_path_ack, info);
2435 cmsg.header.size = htons(sizeof(cmsg));
2436 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2437 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2438 cmsg.tunnel_id = htonl(t->local_tid);
2439 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2443 struct MeshPeerPath *path2;
2445 /* It's for somebody else! Retransmit. */
2446 path2 = path_duplicate(path);
2447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2448 "MESH: Retransmitting.\n");
2449 path_add_to_peer(dest_peer_info, path);
2450 path = path_duplicate(path2);
2451 path_add_to_origin(orig_peer_info, path2);
2452 send_create_path(dest_peer_info, path, t);
2459 * Core handler for path destruction
2461 * @param cls closure
2462 * @param message message
2463 * @param peer peer identity this notification is about
2464 * @param atsi performance data
2466 * @return GNUNET_OK to keep the connection open,
2467 * GNUNET_SYSERR to close it (signal serious error)
2470 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2471 const struct GNUNET_MessageHeader *message,
2472 const struct GNUNET_ATS_Information *atsi)
2474 struct GNUNET_MESH_ManipulatePath *msg;
2475 struct GNUNET_PeerIdentity *pi;
2476 struct MeshPeerPath *path;
2477 struct MeshTunnel *t;
2478 unsigned int own_pos;
2482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2483 "MESH: Received a PATH DESTROY msg from %s\n",
2485 size = ntohs (message->size);
2486 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2488 GNUNET_break_op (0);
2492 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2493 if (size % sizeof (struct GNUNET_PeerIdentity))
2495 GNUNET_break_op (0);
2498 size /= sizeof (struct GNUNET_PeerIdentity);
2501 GNUNET_break_op (0);
2504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2505 "MESH: path has %u hops.\n",
2508 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2509 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2511 "MESH: path is for tunnel %s [%X].\n",
2514 t = tunnel_get (pi, ntohl (msg->tid));
2517 /* TODO notify back: we don't know this tunnel */
2518 GNUNET_break_op (0);
2521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2522 path = path_new (size);
2524 for (i = 0; i < size; i++)
2526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2527 "MESH: ... adding %s\n",
2528 GNUNET_i2s(&pi[i]));
2529 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2530 if (path->peers[i] == myid)
2533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2534 "MESH: Own position: %u\n", own_pos);
2535 if (own_pos < path->length - 1)
2536 send_message (message, &pi[own_pos + 1]);
2537 tunnel_delete_peer (t, path->peers[path->length - 1]);
2543 * Core handler for tunnel destruction
2545 * @param cls closure
2546 * @param message message
2547 * @param peer peer identity this notification is about
2548 * @param atsi performance data
2550 * @return GNUNET_OK to keep the connection open,
2551 * GNUNET_SYSERR to close it (signal serious error)
2554 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2555 const struct GNUNET_MessageHeader *message,
2556 const struct GNUNET_ATS_Information *atsi)
2558 struct GNUNET_MESH_TunnelDestroy *msg;
2559 struct MeshTunnel *t;
2561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2562 "MESH: Got a TUNNEL DESTROY packet from %s\n",
2564 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2566 "MESH: for tunnel %s [%u]\n",
2567 GNUNET_i2s (&msg->oid),
2569 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2572 /* TODO notify back: we don't know this tunnel */
2573 GNUNET_break_op (0);
2576 if (t->id.oid == myid)
2578 GNUNET_break_op (0);
2581 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2583 /* Tunnel was incoming, notify clients */
2584 send_clients_tunnel_destroy (t);
2586 tunnel_send_destroy (t);
2593 * Core handler for mesh network traffic going from the origin to a peer
2595 * @param cls closure
2596 * @param peer peer identity this notification is about
2597 * @param message message
2598 * @param atsi performance data
2599 * @return GNUNET_OK to keep the connection open,
2600 * GNUNET_SYSERR to close it (signal serious error)
2603 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2604 const struct GNUNET_MessageHeader *message,
2605 const struct GNUNET_ATS_Information *atsi)
2607 struct GNUNET_MESH_Unicast *msg;
2608 struct MeshTunnel *t;
2612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2613 "MESH: got a unicast packet from %s\n",
2615 size = ntohs (message->size);
2617 sizeof (struct GNUNET_MESH_Unicast) +
2618 sizeof (struct GNUNET_MessageHeader))
2623 msg = (struct GNUNET_MESH_Unicast *) message;
2624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2625 "MESH: of type %u\n",
2626 ntohs (msg[1].header.type));
2627 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2630 /* TODO notify back: we don't know this tunnel */
2631 GNUNET_break_op (0);
2634 tunnel_reset_timeout (t);
2635 pid = GNUNET_PEER_search(&msg->destination);
2638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2639 "MESH: it's for us! sending to clients...\n");
2640 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2644 "MESH: not for us, retransmitting...\n");
2645 send_message (message, path_get_first_hop(t->tree, pid));
2651 * Core handler for mesh network traffic going from the origin to all peers
2653 * @param cls closure
2654 * @param message message
2655 * @param peer peer identity this notification is about
2656 * @param atsi performance data
2657 * @return GNUNET_OK to keep the connection open,
2658 * GNUNET_SYSERR to close it (signal serious error)
2660 * TODO: Check who we got this from, to validate route.
2663 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2664 const struct GNUNET_MessageHeader *message,
2665 const struct GNUNET_ATS_Information *atsi)
2667 struct GNUNET_MESH_Multicast *msg;
2668 struct MeshTunnel *t;
2671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2672 "MESH: got a multicast packet from %s\n",
2674 size = ntohs (message->size);
2675 if (sizeof (struct GNUNET_MESH_Multicast) +
2676 sizeof (struct GNUNET_MessageHeader) > size)
2678 GNUNET_break_op (0);
2681 msg = (struct GNUNET_MESH_Multicast *) message;
2682 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2686 /* TODO notify that we dont know that tunnel */
2687 GNUNET_break_op (0);
2690 tunnel_reset_timeout (t);
2692 /* Transmit to locally interested clients */
2693 if (NULL != t->peers &&
2694 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2696 send_subscribed_clients (message, &msg[1].header);
2698 tunnel_send_multicast(t, message);
2704 * Core handler for mesh network traffic toward the owner of a tunnel
2706 * @param cls closure
2707 * @param message message
2708 * @param peer peer identity this notification is about
2709 * @param atsi performance data
2711 * @return GNUNET_OK to keep the connection open,
2712 * GNUNET_SYSERR to close it (signal serious error)
2715 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2716 const struct GNUNET_MessageHeader *message,
2717 const struct GNUNET_ATS_Information *atsi)
2719 struct GNUNET_MESH_ToOrigin *msg;
2720 struct GNUNET_PeerIdentity id;
2721 struct MeshPeerInfo *peer_info;
2722 struct MeshTunnel *t;
2725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2726 "MESH: got a ToOrigin packet from %s\n",
2728 size = ntohs (message->size);
2729 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2730 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2732 GNUNET_break_op (0);
2735 msg = (struct GNUNET_MESH_ToOrigin *) message;
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2737 "MESH: of type %u\n",
2738 ntohs (msg[1].header.type));
2739 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2743 /* TODO notify that we dont know this tunnel (whom)? */
2744 GNUNET_break_op (0);
2748 if (t->id.oid == myid)
2751 struct GNUNET_MESH_ToOrigin *copy;
2753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2754 "MESH: it's for us! sending to clients...\n");
2755 if (NULL == t->client)
2757 /* got data packet for ownerless tunnel */
2758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2759 "MESH: no clients!\n");
2760 GNUNET_break_op (0);
2763 /* TODO signature verification */
2764 memcpy (cbuf, message, size);
2765 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2766 copy->tid = htonl (t->local_tid);
2767 GNUNET_SERVER_notification_context_unicast (nc,
2773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2774 "MESH: not for us, retransmitting...\n");
2776 peer_info = peer_info_get (&msg->oid);
2777 if (NULL == peer_info)
2779 /* unknown origin of tunnel */
2783 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
2784 send_message (message, &id);
2791 * Core handler for path ACKs
2793 * @param cls closure
2794 * @param message message
2795 * @param peer peer identity this notification is about
2796 * @param atsi performance data
2798 * @return GNUNET_OK to keep the connection open,
2799 * GNUNET_SYSERR to close it (signal serious error)
2802 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2803 const struct GNUNET_MessageHeader *message,
2804 const struct GNUNET_ATS_Information *atsi)
2806 struct GNUNET_MESH_PathACK *msg;
2807 struct GNUNET_PeerIdentity id;
2808 struct MeshTunnelTreeNode *n;
2809 struct MeshPeerInfo *peer_info;
2810 struct MeshTunnel *t;
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2813 "MESH: Received a path ACK msg [%s]\n",
2814 GNUNET_i2s(&my_full_id));
2815 msg = (struct GNUNET_MESH_PathACK *) message;
2816 t = tunnel_get (&msg->oid, msg->tid);
2819 /* TODO notify that we don't know the tunnel */
2823 /* Message for us? */
2824 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
2827 if (NULL == t->client)
2829 GNUNET_break_op (0);
2832 peer_info = peer_info_get (&msg->peer_id);
2833 if (NULL == peer_info)
2835 GNUNET_break_op (0);
2838 n = tree_find_peer(t->tree->root, peer_info->id);
2841 GNUNET_break_op (0);
2844 n->status = MESH_PEER_READY;
2845 send_client_peer_connected(t, peer_info->id);
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2850 "MESH: not for us, retransmitting...\n");
2851 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
2852 peer_info = peer_info_get (&msg->oid);
2853 if (NULL == peer_info)
2855 /* If we know the tunnel, we should DEFINITELY know the peer */
2859 send_message (message, &id);
2865 * Functions to handle messages from core
2867 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2868 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2869 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
2870 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
2871 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2872 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2873 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2874 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2875 sizeof (struct GNUNET_MESH_PathACK)},
2881 /******************************************************************************/
2882 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2883 /******************************************************************************/
2886 * deregister_app: iterator for removing each application registered by a client
2888 * @param cls closure
2889 * @param key the hash of the application id (used to access the hashmap)
2890 * @param value the value stored at the key (client)
2892 * @return GNUNET_OK on success
2895 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2897 GNUNET_break (GNUNET_YES ==
2898 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
2904 * notify_client_connection_failure: notify a client that the connection to the
2905 * requested remote peer is not possible (for instance, no route found)
2906 * Function called when the socket is ready to queue more data. "buf" will be
2907 * NULL and "size" zero if the socket was closed for writing in the meantime.
2909 * @param cls closure
2910 * @param size number of bytes available in buf
2911 * @param buf where the callee should write the message
2912 * @return number of bytes written to buf
2915 notify_client_connection_failure (void *cls, size_t size, void *buf)
2918 struct MeshPeerInfo *peer_info;
2919 struct GNUNET_MESH_PeerControl *msg;
2920 struct GNUNET_PeerIdentity id;
2922 if (0 == size && NULL == buf)
2924 // TODO retry? cancel?
2928 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2929 peer_info = (struct MeshPeerInfo *) cls;
2930 msg = (struct GNUNET_MESH_PeerControl *) buf;
2931 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2932 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2933 // msg->tunnel_id = htonl(peer_info->t->tid);
2934 GNUNET_PEER_resolve (peer_info->id, &id);
2935 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2943 * Send keepalive packets for a peer
2945 * @param cls Closure (tunnel for which to send the keepalive).
2946 * @param tc Notification context.
2948 * TODO: implement explicit multicast keepalive?
2951 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2953 struct MeshTunnel *t = cls;
2954 struct GNUNET_MessageHeader *payload;
2955 struct GNUNET_MESH_Multicast *msg;
2956 size_t size = sizeof(struct GNUNET_MESH_Multicast) +
2957 sizeof(struct GNUNET_MessageHeader);
2960 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2964 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2967 "MESH: sending keepalive for tunnel %d\n",
2970 msg = (struct GNUNET_MESH_Multicast *) cbuf;
2971 msg->header.size = htons (size);
2972 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2973 msg->oid = my_full_id;
2974 msg->tid = htonl(t->id.tid);
2975 payload = (struct GNUNET_MessageHeader *) &msg[1];
2976 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2977 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
2978 tunnel_send_multicast (t, &msg->header);
2980 t->path_refresh_task =
2981 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2987 * Function to process paths received for a new peer addition. The recorded
2988 * paths form the initial tunnel, which can be optimized later.
2989 * Called on each result obtained for the DHT search.
2991 * @param cls closure
2992 * @param exp when will this value expire
2993 * @param key key of the result
2994 * @param type type of the result
2995 * @param size number of bytes in data
2996 * @param data pointer to the result data
2998 * TODO: re-issue the request after certain time? cancel after X results?
3001 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3002 const GNUNET_HashCode * key,
3003 const struct GNUNET_PeerIdentity *get_path,
3004 unsigned int get_path_length,
3005 const struct GNUNET_PeerIdentity *put_path,
3006 unsigned int put_path_length,
3007 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
3009 struct MeshPathInfo *path_info = cls;
3010 struct MeshPeerPath *p;
3011 struct GNUNET_PeerIdentity pi;
3014 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3015 "MESH: Got results from DHT!\n");
3016 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3017 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3019 GNUNET_h2s_full(&pi.hashPubKey));
3020 // GNUNET_DHT_get_stop(path_info->peer->dhtget);
3021 // path_info->peer->dhtget = NULL;
3023 p = path_build_from_dht (get_path, get_path_length,
3024 put_path, put_path_length);
3025 path_add_to_peer (path_info->peer, p);
3026 for (i = 0; i < path_info->peer->ntunnels; i++)
3028 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3029 peer_info_connect(path_info->peer, path_info->t);
3031 GNUNET_free (path_info);
3038 * Function to process paths received for a new peer addition. The recorded
3039 * paths form the initial tunnel, which can be optimized later.
3040 * Called on each result obtained for the DHT search.
3042 * @param cls closure
3043 * @param exp when will this value expire
3044 * @param key key of the result
3045 * @param type type of the result
3046 * @param size number of bytes in data
3047 * @param data pointer to the result data
3050 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3051 const GNUNET_HashCode * key,
3052 const struct GNUNET_PeerIdentity *get_path,
3053 unsigned int get_path_length,
3054 const struct GNUNET_PeerIdentity *put_path,
3055 unsigned int put_path_length,
3056 enum GNUNET_BLOCK_Type type, size_t size,
3059 const struct GNUNET_PeerIdentity *pi = data;
3060 struct GNUNET_PeerIdentity id;
3061 struct MeshTunnel *t = cls;
3062 struct MeshPeerInfo *peer_info;
3063 struct MeshPeerPath *p;
3066 if (size != sizeof (struct GNUNET_PeerIdentity))
3068 GNUNET_break_op (0);
3071 GNUNET_assert (NULL != t->client);
3072 GNUNET_DHT_get_stop (t->dht_get_type);
3073 t->dht_get_type = NULL;
3074 peer_info = peer_info_get (pi);
3075 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
3076 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3078 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
3079 NULL == peer_info->dhtget)
3081 peer_info_connect (peer_info, t);
3085 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
3086 path_add_to_peer (peer_info, p);
3087 tunnel_add_peer(t, peer_info);
3088 p = tree_get_path_to_peer(t->tree, peer_info->id);
3090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3091 "MESH: new route for tunnel 0x%x found, has %u hops\n",
3092 t->local_tid, p->length);
3093 for (i = 0; i < p->length; i++)
3095 GNUNET_PEER_resolve (p->peers[0], &id);
3096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
3097 GNUNET_h2s_full (&id.hashPubKey));
3103 send_create_path(peer_info, p, t);
3107 send_client_peer_connected(t, myid);
3111 /******************************************************************************/
3112 /********************* MESH LOCAL HANDLES **************************/
3113 /******************************************************************************/
3117 * Handler for client disconnection
3119 * @param cls closure
3120 * @param client identification of the client; NULL
3121 * for the last call when the server is destroyed
3124 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3126 struct MeshClient *c;
3127 struct MeshClient *next;
3129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3135 if (c->handle != client && NULL != client)
3137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
3142 if (NULL != c->tunnels)
3144 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3145 &tunnel_destroy_iterator,
3147 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3150 /* deregister clients applications */
3151 if (NULL != c->apps)
3153 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3154 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3156 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3157 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3159 GNUNET_SCHEDULER_cancel (announce_applications_task);
3160 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3162 if (NULL != c->types)
3163 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3164 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3176 * Handler for new clients
3178 * @param cls closure
3179 * @param client identification of the client
3180 * @param message the actual message, which includes messages the client wants
3183 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3184 const struct GNUNET_MessageHeader *message)
3186 struct GNUNET_MESH_ClientConnect *cc_msg;
3187 struct MeshClient *c;
3188 GNUNET_MESH_ApplicationType *a;
3195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3196 /* Check data sanity */
3197 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3198 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3199 ntypes = ntohs (cc_msg->types);
3200 napps = ntohs (cc_msg->applications);
3202 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3205 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3209 /* Create new client structure */
3210 c = GNUNET_malloc (sizeof (struct MeshClient));
3212 c->id = next_client_id++;
3215 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3218 GNUNET_MESH_ApplicationType at;
3221 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3222 for (i = 0; i < napps; i++)
3225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3226 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3227 /* store in clients hashmap */
3228 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3229 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3230 /* store in global hashmap, for announcements */
3231 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3232 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3234 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3235 announce_applications_task =
3236 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3244 t = (uint16_t *) & a[napps];
3245 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3246 for (i = 0; i < ntypes; i++)
3249 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3251 /* store in clients hashmap */
3252 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3253 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3254 /* store in global hashmap */
3255 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3256 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3260 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3262 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3263 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3264 GNUNET_SERVER_notification_context_add (nc, client);
3266 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3274 * Handler for requests of new tunnels
3276 * @param cls closure
3277 * @param client identification of the client
3278 * @param message the actual message
3281 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3282 const struct GNUNET_MessageHeader *message)
3284 struct GNUNET_MESH_TunnelMessage *t_msg;
3285 struct MeshTunnel *t;
3286 struct MeshClient *c;
3287 GNUNET_HashCode hash;
3289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3291 /* Sanity check for client registration */
3292 if (NULL == (c = client_get (client)))
3295 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3302 /* Message sanity check */
3303 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3306 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3310 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3311 /* Sanity check for tunnel numbering */
3312 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3318 /* Sanity check for duplicate tunnel IDs */
3319 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3322 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3326 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
3328 while (NULL != tunnel_get_by_pi (myid, next_tid))
3329 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3330 t->id.tid = next_tid++;
3332 t->local_tid = ntohl (t_msg->tunnel_id);
3334 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3336 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3338 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3339 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3342 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3346 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3348 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3349 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3352 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3355 t->tree = tree_new (t, myid);
3356 t->tree->refresh = REFRESH_PATH_TIME;
3357 t->tree->root->status = MESH_PEER_READY;
3358 t->tree->me = t->tree->root;
3360 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3366 * Handler for requests of deleting tunnels
3368 * @param cls closure
3369 * @param client identification of the client
3370 * @param message the actual message
3373 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3374 const struct GNUNET_MessageHeader *message)
3376 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3377 struct MeshClient *c;
3378 struct MeshTunnel *t;
3379 MESH_TunnelNumber tid;
3380 GNUNET_HashCode hash;
3382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3383 "MESH: Got a DESTROY TUNNEL from client!\n");
3385 /* Sanity check for client registration */
3386 if (NULL == (c = client_get (client)))
3389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3392 /* Message sanity check */
3393 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3396 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3402 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3404 /* Retrieve tunnel */
3405 tid = ntohl (tunnel_msg->tunnel_id);
3407 /* Remove from local id hashmap */
3408 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3409 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3410 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
3413 tunnel_send_destroy (t);
3415 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3421 * Handler for connection requests to new peers
3423 * @param cls closure
3424 * @param client identification of the client
3425 * @param message the actual message (PeerControl)
3428 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3429 const struct GNUNET_MessageHeader *message)
3431 struct GNUNET_MESH_PeerControl *peer_msg;
3432 struct MeshPeerInfo *peer_info;
3433 struct MeshClient *c;
3434 struct MeshTunnel *t;
3435 MESH_TunnelNumber tid;
3437 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3438 /* Sanity check for client registration */
3439 if (NULL == (c = client_get (client)))
3442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3446 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3447 /* Sanity check for message size */
3448 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3455 /* Tunnel exists? */
3456 tid = ntohl (peer_msg->tunnel_id);
3457 t = tunnel_get_by_local_id (c, tid);
3461 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3465 /* Does client own tunnel? */
3466 if (t->client->handle != client)
3469 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3472 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3473 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
3474 peer_info = peer_info_get (&peer_msg->peer);
3476 tunnel_add_peer(t, peer_info);
3477 peer_info_connect(peer_info, t);
3479 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3485 * Handler for disconnection requests of peers in a tunnel
3487 * @param cls closure
3488 * @param client identification of the client
3489 * @param message the actual message (PeerControl)
3492 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3493 const struct GNUNET_MessageHeader *message)
3495 struct GNUNET_MESH_PeerControl *peer_msg;
3496 struct MeshPeerInfo *peer_info;
3497 struct MeshClient *c;
3498 struct MeshTunnel *t;
3499 MESH_TunnelNumber tid;
3501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got a PEER DEL request\n");
3502 /* Sanity check for client registration */
3503 if (NULL == (c = client_get (client)))
3506 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3509 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3510 /* Sanity check for message size */
3511 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3514 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3518 /* Tunnel exists? */
3519 tid = ntohl (peer_msg->tunnel_id);
3520 t = tunnel_get_by_local_id (c, tid);
3524 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: on tunnel %X\n", t->id.tid);
3529 /* Does client own tunnel? */
3530 if (t->client->handle != client)
3533 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3538 "MESH: for peer %s\n",
3539 GNUNET_i2s(&peer_msg->peer));
3540 /* Is the peer in the tunnel? */
3541 peer_info = GNUNET_CONTAINER_multihashmap_get(t->peers,
3542 &peer_msg->peer.hashPubKey);
3543 if (NULL == peer_info)
3546 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3550 /* Ok, delete peer from tunnel */
3551 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3552 &peer_msg->peer.hashPubKey);
3554 send_destroy_path (t, peer_info->id);
3555 tunnel_delete_peer(t, peer_info->id);
3556 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3562 * Handler for connection requests to new peers by type
3564 * @param cls closure
3565 * @param client identification of the client
3566 * @param message the actual message (ConnectPeerByType)
3569 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3570 const struct GNUNET_MessageHeader *message)
3572 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3573 struct MeshClient *c;
3574 struct MeshTunnel *t;
3575 GNUNET_HashCode hash;
3576 MESH_TunnelNumber tid;
3578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3579 /* Sanity check for client registration */
3580 if (NULL == (c = client_get (client)))
3583 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3587 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3588 /* Sanity check for message size */
3589 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3590 ntohs (connect_msg->header.size))
3593 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3597 /* Tunnel exists? */
3598 tid = ntohl (connect_msg->tunnel_id);
3599 t = tunnel_get_by_local_id (c, tid);
3603 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3607 /* Does client own tunnel? */
3608 if (t->client->handle != client)
3611 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3615 /* Do WE have the service? */
3616 t->type = ntohl (connect_msg->type);
3617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3618 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3619 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3622 /* Yes! Fast forward, add ourselves to the tunnel and send the
3623 * good news to the client
3625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3626 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3627 peer_info_get (&my_full_id),
3628 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3631 send_client_peer_connected(t, myid);
3632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3633 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3636 /* Ok, lets find a peer offering the service */
3637 if (NULL != t->dht_get_type)
3639 GNUNET_DHT_get_stop (t->dht_get_type);
3641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3642 GNUNET_h2s_full (&hash));
3644 GNUNET_DHT_get_start (dht_handle,
3645 GNUNET_TIME_UNIT_FOREVER_REL,
3646 GNUNET_BLOCK_TYPE_TEST,
3649 GNUNET_DHT_RO_RECORD_ROUTE |
3650 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3652 &dht_get_type_handler, t);
3654 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3660 * Handler for client traffic directed to one peer
3662 * @param cls closure
3663 * @param client identification of the client
3664 * @param message the actual message
3667 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3668 const struct GNUNET_MessageHeader *message)
3670 struct MeshClient *c;
3671 struct MeshTunnel *t;
3672 struct MeshPeerInfo *pi;
3673 struct GNUNET_MESH_Unicast *data_msg;
3674 MESH_TunnelNumber tid;
3677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3678 "MESH: Got a unicast request from a client!\n");
3680 /* Sanity check for client registration */
3681 if (NULL == (c = client_get (client)))
3684 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3687 data_msg = (struct GNUNET_MESH_Unicast *) message;
3688 /* Sanity check for message size */
3689 size = ntohs (message->size);
3690 if (sizeof (struct GNUNET_MESH_Unicast) +
3691 sizeof (struct GNUNET_MessageHeader) > size)
3694 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3698 /* Tunnel exists? */
3699 tid = ntohl (data_msg->tid);
3700 t = tunnel_get_by_local_id (c, tid);
3704 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3708 /* Is it a local tunnel? Then, does client own the tunnel? */
3709 if (NULL != t->client &&
3710 NULL != t->client->handle &&
3711 t->client->handle != client)
3714 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3718 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3719 &data_msg->destination.hashPubKey);
3720 /* Is the selected peer in the tunnel? */
3724 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3728 /* Ok, everything is correct, send the message
3729 * (pretend we got it from a mesh peer)
3732 char buf[ntohs (message->size)];
3733 struct GNUNET_MESH_Unicast *copy;
3735 /* Work around const limitation */
3736 copy = (struct GNUNET_MESH_Unicast *) buf;
3737 memcpy (buf, data_msg, size);
3738 copy->oid = my_full_id;
3739 copy->tid = htonl (t->id.tid);
3740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3741 "MESH: calling generic handler...\n");
3742 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
3744 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3750 * Handler for client traffic directed to the origin
3752 * @param cls closure
3753 * @param client identification of the client
3754 * @param message the actual message
3757 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3758 const struct GNUNET_MessageHeader *message)
3760 struct GNUNET_MESH_ToOrigin *data_msg;
3761 struct GNUNET_PeerIdentity id;
3762 struct MeshClient *c;
3763 struct MeshTunnel *t;
3764 MESH_TunnelNumber tid;
3767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3768 "MESH: Got a ToOrigin request from a client!\n");
3770 /* Sanity check for client registration */
3771 if (NULL == (c = client_get (client)))
3774 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3777 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3778 /* Sanity check for message size */
3779 size = ntohs (message->size);
3780 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3781 sizeof (struct GNUNET_MessageHeader) > size)
3784 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3788 /* Tunnel exists? */
3789 tid = ntohl (data_msg->tid);
3790 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3793 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3796 t = tunnel_get_by_local_id (c, tid);
3800 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3804 /* It shouldn't be a local tunnel. */
3805 if (NULL != t->client)
3808 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3811 GNUNET_PEER_resolve(t->id.oid, &id);
3813 /* Ok, everything is correct, send the message
3814 * (pretend we got it from a mesh peer)
3817 char buf[ntohs (message->size)];
3818 struct GNUNET_MESH_ToOrigin *copy;
3820 /* Work around const limitation */
3821 copy = (struct GNUNET_MESH_ToOrigin *) buf;
3822 memcpy (buf, data_msg, size);
3824 copy->tid = htonl (t->id.tid);
3825 copy->sender = my_full_id;
3826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3827 "MESH: calling generic handler...\n");
3828 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL);
3830 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3836 * Handler for client traffic directed to all peers in a tunnel
3838 * @param cls closure
3839 * @param client identification of the client
3840 * @param message the actual message
3843 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3844 const struct GNUNET_MessageHeader *message)
3846 struct MeshClient *c;
3847 struct MeshTunnel *t;
3848 struct GNUNET_MESH_Multicast *data_msg;
3849 MESH_TunnelNumber tid;
3851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3852 "MESH: Got a multicast request from a client!\n");
3854 /* Sanity check for client registration */
3855 if (NULL == (c = client_get (client)))
3858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3861 data_msg = (struct GNUNET_MESH_Multicast *) message;
3862 /* Sanity check for message size */
3863 if (sizeof (struct GNUNET_MESH_Multicast) +
3864 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
3867 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3871 /* Tunnel exists? */
3872 tid = ntohl (data_msg->tid);
3873 t = tunnel_get_by_local_id (c, tid);
3877 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3881 /* Does client own tunnel? */
3882 if (t->client->handle != client)
3885 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3890 char buf[ntohs(message->size)];
3891 struct GNUNET_MESH_Multicast *copy;
3893 copy = (struct GNUNET_MESH_Multicast *)buf;
3894 memcpy(buf, message, ntohs(message->size));
3895 copy->oid = my_full_id;
3896 copy->tid = htonl(t->id.tid);
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3898 "MESH: calling generic handler...\n");
3899 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3902 /* receive done gets called when last copy is sent to a neighbor */
3907 * Functions to handle messages from clients
3909 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3910 {&handle_local_new_client, NULL,
3911 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3912 {&handle_local_tunnel_create, NULL,
3913 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3914 sizeof (struct GNUNET_MESH_TunnelMessage)},
3915 {&handle_local_tunnel_destroy, NULL,
3916 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3917 sizeof (struct GNUNET_MESH_TunnelMessage)},
3918 {&handle_local_connect_add, NULL,
3919 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3920 sizeof (struct GNUNET_MESH_PeerControl)},
3921 {&handle_local_connect_del, NULL,
3922 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3923 sizeof (struct GNUNET_MESH_PeerControl)},
3924 {&handle_local_connect_by_type, NULL,
3925 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3926 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3927 {&handle_local_unicast, NULL,
3928 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3929 {&handle_local_to_origin, NULL,
3930 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3931 {&handle_local_multicast, NULL,
3932 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3938 * To be called on core init/fail.
3940 * @param cls service closure
3941 * @param server handle to the server for this service
3942 * @param identity the public identity of this peer
3945 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3946 const struct GNUNET_PeerIdentity *identity)
3948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3949 core_handle = server;
3950 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3952 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3953 GNUNET_SCHEDULER_shutdown();
3959 * Method called whenever a given peer connects.
3961 * @param cls closure
3962 * @param peer peer identity this notification is about
3963 * @param atsi performance data for the connection
3966 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3967 const struct GNUNET_ATS_Information *atsi)
3969 struct MeshPeerInfo *peer_info;
3970 struct MeshPeerPath *path;
3972 #if MESH_DEBUG_CONNECTION
3973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3975 GNUNET_h2s(&my_full_id.hashPubKey));
3977 peer_info = peer_info_get (peer);
3978 if (myid == peer_info->id)
3980 #if MESH_DEBUG_CONNECTION
3981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3985 #if MESH_DEBUG_CONNECTION
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3989 GNUNET_h2s(&peer->hashPubKey));
3992 path = path_new (2);
3993 path->peers[0] = myid;
3994 path->peers[1] = peer_info->id;
3995 GNUNET_PEER_change_rc(myid, 1);
3996 GNUNET_PEER_change_rc(peer_info->id, 1);
3997 path_add_to_peer (peer_info, path);
4002 * Method called whenever a peer disconnects.
4004 * @param cls closure
4005 * @param peer peer identity this notification is about
4008 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4010 struct MeshPeerInfo *pi;
4013 #if MESH_DEBUG_CONNECTION
4014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
4016 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4022 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4024 peer_info_cancel_transmission(pi, i);
4026 path_remove_from_peer (pi, pi->id, myid);
4027 #if MESH_DEBUG_CONNECTION
4030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4037 /******************************************************************************/
4038 /************************ MAIN FUNCTIONS ****************************/
4039 /******************************************************************************/
4042 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4044 * @param cls closure
4045 * @param key current key code
4046 * @param value value in the hash map
4047 * @return GNUNET_YES if we should continue to iterate,
4051 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4053 struct MeshTunnel *t = value;
4059 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4061 * @param cls closure
4062 * @param key current key code
4063 * @param value value in the hash map
4064 * @return GNUNET_YES if we should continue to iterate,
4068 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4070 struct MeshPeerInfo *p = value;
4071 peer_info_destroy(p);
4076 * Task run during shutdown.
4082 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4085 /* TODO: destroy tunnels? */
4086 if (core_handle != NULL)
4088 GNUNET_CORE_disconnect (core_handle);
4091 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
4092 GNUNET_CONTAINER_multihashmap_iterate(peers, &shutdown_peer, NULL);
4093 if (dht_handle != NULL)
4095 GNUNET_DHT_disconnect (dht_handle);
4100 GNUNET_SERVER_notification_context_destroy (nc);
4103 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4105 GNUNET_SCHEDULER_cancel (announce_id_task);
4106 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4112 * Process mesh requests.
4114 * @param cls closure
4115 * @param server the initialized server
4116 * @param c configuration to use
4119 run (void *cls, struct GNUNET_SERVER_Handle *server,
4120 const struct GNUNET_CONFIGURATION_Handle *c)
4122 struct MeshPeerInfo *peer;
4123 struct MeshPeerPath *p;
4126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4127 server_handle = server;
4128 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4129 CORE_QUEUE_SIZE, /* queue size */
4130 NULL, /* Closure passed to MESH functions */
4131 &core_init, /* Call core_init once connected */
4132 &core_connect, /* Handle connects */
4133 &core_disconnect, /* remove peers on disconnects */
4134 NULL, /* Don't notify about all incoming messages */
4135 GNUNET_NO, /* For header only in notification */
4136 NULL, /* Don't notify about all outbound messages */
4137 GNUNET_NO, /* For header-only out notification */
4138 core_handlers); /* Register these handlers */
4139 if (core_handle == NULL)
4142 GNUNET_SCHEDULER_shutdown ();
4147 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4152 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4153 GNUNET_SCHEDULER_shutdown ();
4156 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4157 GNUNET_free (keyfile);
4158 if (my_private_key == NULL)
4160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4161 _("Mesh service could not access hostkey. Exiting.\n"));
4162 GNUNET_SCHEDULER_shutdown ();
4165 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4166 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4167 &my_full_id.hashPubKey);
4168 myid = GNUNET_PEER_intern (&my_full_id);
4170 dht_handle = GNUNET_DHT_connect (c, 64);
4171 if (dht_handle == NULL)
4177 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4179 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4180 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4181 peers = GNUNET_CONTAINER_multihashmap_create (32);
4182 applications = GNUNET_CONTAINER_multihashmap_create (32);
4183 types = GNUNET_CONTAINER_multihashmap_create (32);
4185 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4186 nc = GNUNET_SERVER_notification_context_create (server_handle,
4188 GNUNET_SERVER_disconnect_notify (server_handle,
4189 &handle_local_client_disconnect,
4194 clients_tail = NULL;
4199 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4200 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4202 /* Create a peer_info for the local peer */
4203 peer = peer_info_get(&my_full_id);
4206 path_add_to_peer(peer, p);
4208 /* Scheduled the task to clean up when shutdown is called */
4209 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4216 * The main function for the mesh service.
4218 * @param argc number of arguments from the command line
4219 * @param argv command line arguments
4220 * @return 0 ok, 1 on error
4223 main (int argc, char *const *argv)
4227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4230 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");