2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - relay corking down to core
48 #include "mesh_protocol.h"
49 #include "gnunet_dht_service.h"
50 #include "mesh_tunnel_tree.h"
52 /* TODO: move into configuration file */
53 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
54 GNUNET_TIME_UNIT_SECONDS,\
56 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
57 GNUNET_TIME_UNIT_SECONDS,\
60 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
61 GNUNET_TIME_UNIT_SECONDS,\
64 #define GET_RESTART_TIME GNUNET_TIME_relative_multiply(\
65 GNUNET_TIME_UNIT_SECONDS,\
68 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
69 GNUNET_TIME_UNIT_SECONDS,\
72 /******************************************************************************/
73 /************************ DATA STRUCTURES ****************************/
74 /******************************************************************************/
76 /** FWD declaration */
80 * Struct containing all info possibly needed to build a package when called
83 struct MeshDataDescriptor
85 /** ID of the tunnel this packet travels in */
86 struct MESH_TunnelID *origin;
91 /** Client that asked for the transmission, if any */
92 struct GNUNET_SERVER_Client *client;
94 /** Who was this message being sent to */
95 struct MeshPeerInfo *peer;
97 /** Ultimate destination of the packet */
98 GNUNET_PEER_Id destination;
100 /** Number of identical messages sent to different hops (multicast) */
101 unsigned int *copies;
103 /** Which handler was used to request the transmission */
104 unsigned int handler_n;
106 /** Size of the data */
109 /** Used to allow a client send more traffic to the service after a
110 * previous packet was tried to be sent to a neighbor and couldn't */
111 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
116 * Struct containing all information regarding a given peer
126 * Last time we heard from this peer
128 struct GNUNET_TIME_Absolute last_contact;
131 * Number of attempts to reconnect so far
133 int n_reconnect_attempts;
136 * Paths to reach the peer, ordered by ascending hop count
138 struct MeshPeerPath *path_head;
141 * Paths to reach the peer, ordered by ascending hop count
143 struct MeshPeerPath *path_tail;
146 * Handle to stop the DHT search for a path to this peer
148 struct GNUNET_DHT_GetHandle *dhtget;
151 * Handles to stop queued transmissions for this peer
153 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
156 * Pointer to info stuctures used as cls for queued transmissions
158 void *infos[CORE_QUEUE_SIZE];
161 * Type of message being in each transmission
163 uint16_t types[CORE_QUEUE_SIZE];
166 * Array of tunnels this peer participates in
167 * (most probably a small amount, therefore not a hashmap)
168 * When the path to the peer changes, notify these tunnels to let them
169 * re-adjust their path trees.
171 struct MeshTunnel **tunnels;
174 * Number of tunnels this peers participates in
176 unsigned int ntunnels;
181 * Data scheduled to transmit (to local client or remote peer)
188 struct MeshQueue *next;
189 struct MeshQueue *prev;
192 * Target of the data (NULL if target is client)
194 struct MeshPeerInfo *peer;
197 * Client to send the data to (NULL if target is peer)
199 struct MeshClient *client;
202 * Size of the message to transmit
207 * How old is the data?
209 struct GNUNET_TIME_Absolute timestamp;
214 struct GNUNET_MessageHeader *data;
218 * Globally unique tunnel identification (owner + number)
219 * DO NOT USE OVER THE NETWORK
224 * Node that owns the tunnel
229 * Tunnel number to differentiate all the tunnels owned by the node oid
230 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
232 MESH_TunnelNumber tid;
236 struct MeshClient; /* FWD declaration */
239 * Struct containing all information regarding a tunnel
240 * For an intermediate node the improtant info used will be:
241 * - id Tunnel unique identification
242 * - paths[0] To know where to send it next
243 * - metainfo: ready, speeds, accounting
250 struct MESH_TunnelID id;
253 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
255 MESH_TunnelNumber local_tid;
258 * Last time the tunnel was used
260 struct GNUNET_TIME_Absolute timestamp;
263 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
264 * containing peers added by id or by type, not intermediate peers.
266 struct GNUNET_CONTAINER_MultiHashMap *peers;
269 * Number of peers that are connected and potentially ready to receive data
271 unsigned int peers_ready;
274 * Number of peers that have been added to the tunnel
276 unsigned int peers_total;
279 * Client owner of the tunnel, if any
281 struct MeshClient *client;
284 * Messages ready to transmit
286 struct MeshQueue *queue_head;
287 struct MeshQueue *queue_tail;
292 struct MeshTunnelTree *tree;
295 * Application type we are looking for in this tunnel
297 GNUNET_MESH_ApplicationType type;
300 * Used to search peers offering a service
302 struct GNUNET_DHT_GetHandle *dht_get_type;
305 * Task to keep the used paths alive
307 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
310 * Task to destroy the tunnel after timeout
312 * FIXME: merge the two? a tunnel will have either
313 * a path refresh OR a timeout, never both!
315 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
320 * Info needed to work with tunnel paths and peers
327 struct MeshTunnel *t;
330 * Neighbouring peer to whom we send the packet to
332 struct MeshPeerInfo *peer;
337 struct MeshPeerPath *path;
340 * Position in peer's transmit queue
347 * Struct containing information about a client of the service
354 struct MeshClient *next;
355 struct MeshClient *prev;
358 * Tunnels that belong to this client, indexed by local id
360 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
363 * Handle to communicate with the client
365 struct GNUNET_SERVER_Client *handle;
368 * Applications that this client has claimed to provide
370 struct GNUNET_CONTAINER_MultiHashMap *apps;
373 * Messages that this client has declared interest in
375 struct GNUNET_CONTAINER_MultiHashMap *types;
379 * ID of the client, for debug messages
388 /******************************************************************************/
389 /************************ DEBUG FUNCTIONS ****************************/
390 /******************************************************************************/
394 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
395 * @param cls string to print
396 * @param tc task context
399 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
403 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
411 /******************************************************************************/
412 /*********************** GLOBAL VARIABLES ****************************/
413 /******************************************************************************/
418 static struct MeshClient *clients;
419 static struct MeshClient *clients_tail;
422 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
424 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
427 * Tunnels incoming, indexed by MESH_TunnelNumber
428 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
430 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
433 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
435 static struct GNUNET_CONTAINER_MultiHashMap *peers;
438 * Handle to communicate with core
440 static struct GNUNET_CORE_Handle *core_handle;
445 static struct GNUNET_DHT_Handle *dht_handle;
450 static struct GNUNET_SERVER_Handle *server_handle;
453 * Notification context, to send messages to local clients
455 static struct GNUNET_SERVER_NotificationContext *nc;
458 * Local peer own ID (memory efficient handle)
460 static GNUNET_PEER_Id myid;
463 * Local peer own ID (full value)
465 static struct GNUNET_PeerIdentity my_full_id;
470 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
475 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
478 * Tunnel ID for the next created tunnel (global tunnel number)
480 static MESH_TunnelNumber next_tid;
483 * Tunnel ID for the next incoming tunnel (local tunnel number)
485 static MESH_TunnelNumber next_local_tid;
488 * All application types provided by this peer
490 static struct GNUNET_CONTAINER_MultiHashMap *applications;
493 * All message types clients of this peer are interested in
495 static struct GNUNET_CONTAINER_MultiHashMap *types;
498 * Task to periodically announce provided applications
500 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
503 * Task to periodically announce itself in the network
505 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
508 unsigned int next_client_id;
512 /******************************************************************************/
513 /************************ ITERATORS ****************************/
514 /******************************************************************************/
516 /* FIXME move iterators here */
519 /******************************************************************************/
520 /************************ PERIODIC FUNCTIONS ****************************/
521 /******************************************************************************/
524 * Announce iterator over for each application provided by the peer
527 * @param key current key code
528 * @param value value in the hash map
529 * @return GNUNET_YES if we should continue to
534 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
536 /* FIXME are hashes in multihash map equal on all aquitectures? */
537 GNUNET_DHT_put (dht_handle,
540 GNUNET_DHT_RO_RECORD_ROUTE |
541 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
542 GNUNET_BLOCK_TYPE_TEST,
543 sizeof (struct GNUNET_PeerIdentity),
544 (const char *) &my_full_id,
546 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
547 &mesh_debug, "DHT_put for app completed");
549 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
551 APP_ANNOUNCE_TIME, NULL, NULL);
558 * Periodically announce what applications are provided by local clients
561 * @param tc task context
564 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
566 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
568 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
572 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
574 announce_applications_task =
575 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
583 * Periodically announce self id in the DHT
586 * @param tc task context
589 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
591 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
593 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
597 * - Set data expiration in function of X
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
601 GNUNET_h2s_full (&my_full_id.hashPubKey));
602 GNUNET_DHT_put (dht_handle, /* DHT handle */
603 &my_full_id.hashPubKey, /* Key to use */
604 10U, /* Replication level */
605 GNUNET_DHT_RO_RECORD_ROUTE |
606 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
607 GNUNET_BLOCK_TYPE_TEST, /* Block type */
608 sizeof(my_full_id), /* Size of the data */
609 (char *)&my_full_id, /* Data itself */
610 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
611 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
613 &mesh_debug, "DHT_put for id completed");
615 NULL, /* Continuation */
616 NULL); /* Continuation closure */
619 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
624 * Function to process paths received for a new peer addition. The recorded
625 * paths form the initial tunnel, which can be optimized later.
626 * Called on each result obtained for the DHT search.
629 * @param exp when will this value expire
630 * @param key key of the result
631 * @param type type of the result
632 * @param size number of bytes in data
633 * @param data pointer to the result data
636 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
637 const GNUNET_HashCode * key,
638 const struct GNUNET_PeerIdentity *get_path,
639 unsigned int get_path_length,
640 const struct GNUNET_PeerIdentity *put_path,
641 unsigned int put_path_length,
642 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
645 /******************************************************************************/
646 /****************** GENERAL HELPER FUNCTIONS ************************/
647 /******************************************************************************/
650 * Check if client has registered with the service and has not disconnected
652 * @param client the client to check
654 * @return non-NULL if client exists in the global DLL
656 static struct MeshClient *
657 client_get (struct GNUNET_SERVER_Client *client)
659 struct MeshClient *c;
664 if (c->handle == client)
673 * Checks if a given client has subscribed to certain message type
675 * @param message_type Type of message to check
676 * @param c Client to check
678 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
683 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
687 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
688 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
692 client_allow_send(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
694 struct MeshDataDescriptor *info = cls;
696 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
698 info->timeout_task = GNUNET_SCHEDULER_NO_TASK;
699 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
704 * Search for a tunnel by global ID using full PeerIdentities
706 * @param oid owner of the tunnel
707 * @param tid global tunnel number
709 * @return tunnel handler, NULL if doesn't exist
711 static struct MeshTunnel *
712 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
716 * Send the message to all clients that have subscribed to its type
718 * @param msg Pointer to the message itself
719 * @return number of clients this message was sent to
722 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
723 const struct GNUNET_MessageHeader *payload)
725 struct GNUNET_PeerIdentity *oid;
726 struct MeshClient *c;
727 struct MeshTunnel *t;
728 MESH_TunnelNumber *tid;
731 char cbuf[htons(msg->size)];
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
734 type = ntohs (payload->type);
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
737 memcpy (cbuf, msg, sizeof(cbuf));
738 switch (htons(msg->type))
740 struct GNUNET_MESH_Unicast *uc;
741 struct GNUNET_MESH_Multicast *mc;
742 struct GNUNET_MESH_ToOrigin *to;
744 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
745 uc = (struct GNUNET_MESH_Unicast *) cbuf;
749 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
750 mc = (struct GNUNET_MESH_Multicast *) cbuf;
754 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
755 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
763 t = tunnel_get (oid, ntohl(*tid));
769 *tid = htonl (t->local_tid);
770 for (count = 0, c = clients; c != NULL; c = c->next)
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
773 if (client_is_subscribed (type, c))
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
777 GNUNET_SERVER_notification_context_unicast (
780 (struct GNUNET_MessageHeader *) cbuf,
789 * Notify the client that owns the tunnel that a peer has connected to it
791 * @param t Tunnel whose owner to notify
792 * @param id Short id of the peer that has connected
795 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
797 struct GNUNET_MESH_PeerControl pc;
799 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
800 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
801 pc.tunnel_id = htonl (t->local_tid);
802 GNUNET_PEER_resolve (id, &pc.peer);
803 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
804 &pc.header, GNUNET_NO);
809 * Function called to notify a client about the socket
810 * being ready to queue more data. "buf" will be
811 * NULL and "size" zero if the socket was closed for
812 * writing in the meantime.
815 * @param size number of bytes available in buf
816 * @param buf where the callee should write the message
817 * @return number of bytes written to buf
820 send_core_create_path (void *cls, size_t size, void *buf);
824 * Function called to notify a client about the socket
825 * being ready to queue more data. "buf" will be
826 * NULL and "size" zero if the socket was closed for
827 * writing in the meantime.
829 * @param cls closure (data itself)
830 * @param size number of bytes available in buf
831 * @param buf where the callee should write the message
833 * @return number of bytes written to buf
836 send_core_data_multicast (void *cls, size_t size, void *buf);
840 * Cancel a core transmission that was already requested and free all resources
841 * associated to the request.
843 * @param peer PeeInfo of the peer whose transmission is cancelled.
844 * @param i Position of the transmission to be cancelled.
847 peer_info_cancel_transmission(struct MeshPeerInfo *peer, unsigned int i)
849 if (NULL != peer->core_transmit[i])
851 struct MeshDataDescriptor *dd;
852 struct MeshPathInfo *path_info;
856 struct GNUNET_PeerIdentity id;
858 GNUNET_PEER_resolve (peer->id, &id);
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "MESH: Cancelling data transmission at %s [%u]\n",
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "MESH: message type %u\n",
868 /* TODO: notify that tranmission has failed */
869 switch (peer->types[i])
871 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
872 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
873 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
876 if (0 == --(*dd->copies))
878 GNUNET_free (dd->copies);
879 GNUNET_free (dd->data);
882 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
884 path_info = peer->infos[i];
885 path_destroy(path_info->path);
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
891 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
892 peer->core_transmit[i] = NULL;
893 GNUNET_free (peer->infos[i]);
899 * Get a unused CORE slot to transmit a message to a peer. If all the slots
900 * are used, cancel one and return it's position.
902 * @param peer PeerInfo of the neighbor we want to transmit to.
904 * @return The index of an available slot to transmit to the neighbor.
907 peer_info_transmit_slot (struct MeshPeerInfo *peer)
911 for (i = 0; peer->core_transmit[i]; i++)
913 if (i == (CORE_QUEUE_SIZE - 1))
915 /* All positions are taken! Overwriting! */
917 peer_info_cancel_transmission(peer, 0);
926 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
927 * and insert it in the appropiate structures if the peer is not known yet.
929 * @param peer Full identity of the peer.
931 * @return Existing or newly created peer info.
933 static struct MeshPeerInfo *
934 peer_info_get (const struct GNUNET_PeerIdentity *peer)
936 struct MeshPeerInfo *peer_info;
938 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
939 if (NULL == peer_info)
942 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
943 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
944 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
945 peer_info->id = GNUNET_PEER_intern (peer);
953 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
954 * and insert it in the appropiate structures if the peer is not known yet.
956 * @param peer Short identity of the peer.
958 * @return Existing or newly created peer info.
960 static struct MeshPeerInfo *
961 peer_info_get_short (const GNUNET_PEER_Id peer)
963 struct GNUNET_PeerIdentity id;
965 GNUNET_PEER_resolve(peer, &id);
966 return peer_info_get(&id);
971 * Iterator to remove the tunnel from the list of tunnels a peer participates
974 * @param cls Closure (tunnel info)
975 * @param key GNUNET_PeerIdentity of the peer (unused)
976 * @param value PeerInfo of the peer
978 * @return always GNUNET_YES, to keep iterating
981 peer_info_delete_tunnel (void* cls, const GNUNET_HashCode* key, void* value)
983 struct MeshTunnel *t = cls;
984 struct MeshPeerInfo *peer = value;
987 for (i = 0; i < peer->ntunnels; i++)
989 if (0 == memcmp(&peer->tunnels[i]->id,
991 sizeof(struct MESH_TunnelID)))
994 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
995 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1004 * Core callback to write a
1006 * @param cls Closure (MeshDataDescriptor with data in "data" member).
1007 * @param size Number of bytes available in buf.
1008 * @param buf Where the to write the message.
1010 * @return number of bytes written to buf
1013 send_core_data_raw (void *cls, size_t size, void *buf)
1015 struct MeshDataDescriptor *info = cls;
1016 struct GNUNET_MessageHeader *msg;
1019 GNUNET_assert (NULL != info);
1020 GNUNET_assert (NULL != info->data);
1021 msg = (struct GNUNET_MessageHeader *) info->data;
1022 total_size = ntohs (msg->size);
1024 if (total_size > size)
1026 struct GNUNET_PeerIdentity id;
1028 GNUNET_PEER_resolve (info->peer->id, &id);
1029 info->peer->core_transmit[info->handler_n] =
1030 GNUNET_CORE_notify_transmit_ready(core_handle,
1033 GNUNET_TIME_UNIT_FOREVER_REL,
1036 &send_core_data_raw,
1040 info->peer->core_transmit[info->handler_n] = NULL;
1041 memcpy (buf, msg, total_size);
1042 GNUNET_free (info->data);
1049 * Sends an already built message to a peer, properly registrating
1050 * all used resources.
1052 * @param message Message to send.
1053 * @param peer Short ID of the neighbor whom to send the message.
1056 send_message (const struct GNUNET_MessageHeader *message,
1057 const struct GNUNET_PeerIdentity *peer)
1059 struct MeshDataDescriptor *info;
1060 struct MeshPeerInfo *neighbor;
1064 size = ntohs (message->size);
1065 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1066 info->data = GNUNET_malloc (size);
1067 memcpy (info->data, message, size);
1068 neighbor = peer_info_get (peer);
1069 i = peer_info_transmit_slot (neighbor);
1070 info->handler_n = i;
1071 info->peer = neighbor;
1072 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1073 neighbor->infos[i] = info;
1074 neighbor->core_transmit[i] =
1075 GNUNET_CORE_notify_transmit_ready(core_handle,
1078 GNUNET_TIME_UNIT_FOREVER_REL,
1081 &send_core_data_raw,
1088 * Sends a CREATE PATH message for a path to a peer, properly registrating
1089 * all used resources.
1091 * @param peer PeerInfo of the final peer for whom this path is being created.
1092 * @param p Path itself.
1093 * @param t Tunnel for which the path is created.
1096 send_create_path (struct MeshPeerInfo *peer,
1097 struct MeshPeerPath *p,
1098 struct MeshTunnel *t)
1100 struct GNUNET_PeerIdentity id;
1101 struct MeshPathInfo *path_info;
1102 struct MeshPeerInfo *neighbor;
1107 p = tree_get_path_to_peer(t->tree, peer->id);
1114 for (i = 0; i < p->length; i++)
1116 if (p->peers[i] == myid)
1119 if (i >= p->length - 1)
1125 GNUNET_PEER_resolve(p->peers[i + 1], &id);
1127 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1128 path_info->path = p;
1130 neighbor = peer_info_get(&id);
1131 path_info->peer = neighbor;
1132 path_info->pos = peer_info_transmit_slot(neighbor);
1133 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1134 neighbor->infos[path_info->pos] = path_info;
1135 neighbor->core_transmit[path_info->pos] =
1136 GNUNET_CORE_notify_transmit_ready (
1137 core_handle, /* handle */
1140 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1142 sizeof (struct GNUNET_MESH_ManipulatePath)
1143 + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1144 &send_core_create_path, /* callback */
1145 path_info); /* cls */
1150 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1152 * @param t Tunnel whose path to destroy.
1153 * @param destination Short ID of the peer to whom the path to destroy.
1156 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1158 struct GNUNET_MESH_ManipulatePath *msg;
1159 struct GNUNET_PeerIdentity *pi;
1160 struct MeshPeerPath *p;
1164 p = tree_get_path_to_peer(t->tree, destination);
1170 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1171 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1172 msg = GNUNET_malloc (size);
1173 msg->header.size = htons (size);
1174 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1175 msg->tid = htonl (t->id.tid);
1176 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1177 for (i = 0; i < p->length; i++)
1179 GNUNET_PEER_resolve(p->peers[i], &pi[i]);
1181 send_message (&msg->header, path_get_first_hop(t->tree, destination));
1187 * Try to establish a new connection to this peer.
1188 * Use the best path for the given tunnel.
1189 * If the peer doesn't have any path to it yet, try to get one.
1190 * If the peer already has some path, send a CREATE PATH towards it.
1192 * @param peer PeerInfo of the peer.
1193 * @param t Tunnel for which to create the path, if possible.
1196 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1198 struct MeshPeerPath *p;
1199 struct MeshPathInfo *path_info;
1201 if (NULL != peer->path_head)
1203 p = tree_get_path_to_peer(t->tree, peer->id);
1206 send_create_path(peer, p, t);
1211 send_client_peer_connected(t, myid);
1214 else if (NULL == peer->dhtget)
1216 struct GNUNET_PeerIdentity id;
1218 GNUNET_PEER_resolve(peer->id, &id);
1219 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1220 path_info->peer = peer;
1223 GNUNET_DHT_get_start(dht_handle, /* handle */
1224 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1225 GNUNET_BLOCK_TYPE_TEST, /* type */
1226 &id.hashPubKey, /* key to search */
1227 4, /* replication level */
1228 GNUNET_DHT_RO_RECORD_ROUTE |
1229 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1231 0, /* xquery bits */
1232 &dht_get_id_handler,
1235 /* Otherwise, there is no path but the DHT get is already started. */
1241 * Destroy the peer_info and free any allocated resources linked to it
1242 * @param t tunnel the path belongs to
1243 * @param pi the peer_info to destroy
1244 * @return GNUNET_OK on success
1247 peer_info_destroy (struct MeshPeerInfo *pi)
1249 GNUNET_HashCode hash;
1250 struct GNUNET_PeerIdentity id;
1252 GNUNET_PEER_resolve (pi->id, &id);
1253 GNUNET_PEER_change_rc (pi->id, -1);
1254 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
1256 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
1257 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
1265 * Notify a tunnel that a connection has broken that affects at least
1266 * some of its peers.
1268 * @param t Tunnel affected.
1269 * @param peer Peer that (at least) has been affected by the disconnection.
1270 * @param p1 Peer that got disconnected from p2.
1271 * @param p2 Peer that got disconnected from p1.
1273 * @return Short ID of the peer disconnected (either p1 or p2).
1274 * 0 if the tunnel remained unaffected.
1276 static GNUNET_PEER_Id
1277 tunnel_notify_connection_broken (struct MeshTunnel *t,
1278 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1282 * Remove all paths that rely on a direct connection between p1 and p2
1283 * from the peer itself and notify all tunnels about it.
1285 * @param peer PeerInfo of affected peer.
1286 * @param p1 GNUNET_PEER_Id of one peer.
1287 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1290 * TODO: optimize (see below)
1293 path_remove_from_peer (struct MeshPeerInfo *peer,
1297 struct GNUNET_PeerIdentity id;
1298 struct MeshPeerPath *p;
1299 struct MeshPeerPath *aux;
1300 struct MeshPeerInfo *peer_d;
1302 unsigned int destroyed;
1308 p = peer->path_head;
1312 for (i = 0; i < (p->length - 1); i++)
1314 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1315 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1327 for (i = 0; i < peer->ntunnels; i++)
1329 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
1331 * Problem: one or more peers have been deleted from the tunnel tree.
1332 * We don't know who they are to try to add them again.
1333 * We need to try to find a new path for each of the disconnected peers.
1334 * Some of them might already have a path to reach them that does not
1335 * involve p1 and p2. Adding all anew might render in a better tree than
1336 * the trivial immediate fix.
1338 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1339 * its children will be reachable trough him.
1341 peer_d = peer_info_get_short(d);
1344 for (p = peer_d->path_head; NULL != p; p = p->next)
1346 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
1354 /* No callback, as peer will be already disconnected */
1355 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
1359 struct MeshPathInfo *path_info;
1361 if (NULL != peer_d->dhtget)
1363 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1364 path_info->path = p;
1365 path_info->peer = peer_d;
1366 path_info->t = peer->tunnels[i];
1368 GNUNET_DHT_get_start(dht_handle, /* handle */
1369 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1370 GNUNET_BLOCK_TYPE_TEST, /* type */
1371 &id.hashPubKey, /*key to search */
1372 4, /* replication level */
1373 GNUNET_DHT_RO_RECORD_ROUTE |
1374 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1376 0, /* xquery bits */
1377 &dht_get_id_handler,
1378 (void *) path_info);
1385 * Add the path to the peer and update the path used to reach it in case this
1388 * @param peer_info Destination peer to add the path to.
1389 * @param path New path to add. Last peer must be the peer in arg 1.
1390 * Path will be either used of freed if already known.
1392 * TODO: trim the part from origin to us? Add it as path to origin?
1395 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1397 struct MeshPeerPath *aux;
1401 if (NULL == peer_info || NULL == path)
1407 l = path_get_length (path);
1414 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1416 l2 = path_get_length (aux);
1419 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1420 peer_info->path_tail, aux, path);
1424 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1431 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1438 * Add the path to the origin peer and update the path used to reach it in case
1439 * this is the shortest.
1440 * The path is given in peer_info -> destination, therefore we turn the path
1441 * upside down first.
1443 * @param peer_info Peer to add the path to, being the origin of the path.
1444 * @param path New path to add after being inversed.
1447 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1450 path_add_to_peer (peer_info, path);
1455 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1456 * to obtain a local peer -> destination path and interning the peer ids.
1458 * @return Newly allocated and created path
1460 static struct MeshPeerPath *
1461 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1462 unsigned int get_path_length,
1463 const struct GNUNET_PeerIdentity *put_path,
1464 unsigned int put_path_length)
1466 struct MeshPeerPath *p;
1472 GNUNET_PEER_change_rc(myid, 1);
1473 i = get_path_length;
1474 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1475 for (i--; i >= 0; i--)
1477 id = GNUNET_PEER_intern (&get_path[i]);
1478 if (p->length > 0 && id == p->peers[p->length - 1])
1480 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1481 GNUNET_PEER_change_rc(id, -1);
1485 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1486 "MESH: Adding from GET: %s.\n",
1487 GNUNET_i2s(&get_path[i]));
1489 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1490 p->peers[p->length - 1] = id;
1493 i = put_path_length;
1494 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1495 for (i--; i >= 0; i--)
1497 id = GNUNET_PEER_intern (&put_path[i]);
1500 /* PUT path went through us, so discard the path up until now and start
1501 * from here to get a much shorter (and loop-free) path.
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 PUT: %s.\n",
1515 GNUNET_i2s(&put_path[i]));
1517 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1518 p->peers[p->length - 1] = id;
1522 if (get_path_length > 0)
1523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1524 "MESH: (first of GET: %s)\n",
1525 GNUNET_h2s_full(&get_path[0].hashPubKey));
1526 if (put_path_length > 0)
1527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1528 "MESH: (first of PUT: %s)\n",
1529 GNUNET_h2s_full(&put_path[0].hashPubKey));
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1531 "MESH: In total: %d hops\n",
1533 for (i = 0; i < p->length; i++)
1535 struct GNUNET_PeerIdentity peer_id;
1537 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1541 GNUNET_h2s_full(&peer_id.hashPubKey));
1549 * Send keepalive packets for a peer
1551 * @param cls Closure (tunnel for which to send the keepalive).
1552 * @param tc Notification context.
1554 * TODO: implement explicit multicast keepalive?
1557 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1561 * Search for a tunnel among the tunnels for a client
1563 * @param c the client whose tunnels to search in
1564 * @param tid the local id of the tunnel
1566 * @return tunnel handler, NULL if doesn't exist
1568 static struct MeshTunnel *
1569 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1571 GNUNET_HashCode hash;
1573 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1574 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1576 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1578 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1583 * Search for a tunnel by global ID using PEER_ID
1585 * @param pi owner of the tunnel
1586 * @param tid global tunnel number
1588 * @return tunnel handler, NULL if doesn't exist
1590 static struct MeshTunnel *
1591 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1593 struct MESH_TunnelID id;
1594 GNUNET_HashCode hash;
1599 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1600 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1605 * Search for a tunnel by global ID using full PeerIdentities
1607 * @param oid owner of the tunnel
1608 * @param tid global tunnel number
1610 * @return tunnel handler, NULL if doesn't exist
1612 static struct MeshTunnel *
1613 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1615 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1620 * Callback used to notify a client owner of a tunnel that a peer has
1621 * disconnected, most likely because of a path change.
1623 * @param n Node in the tree representing the disconnected peer
1626 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1628 struct MeshPeerInfo *peer;
1630 if (NULL != n->t->client && NULL != nc)
1632 struct GNUNET_MESH_PeerControl msg;
1633 msg.header.size = htons (sizeof (msg));
1634 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1635 msg.tunnel_id = htonl (n->t->local_tid);
1636 GNUNET_PEER_resolve (n->peer, &msg.peer);
1637 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1638 &msg.header, GNUNET_NO);
1640 peer = peer_info_get_short(n->peer);
1641 peer_info_connect(peer, n->t);
1646 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1647 * needed rescources.
1648 * If peer already exists, reevaluate shortest path and change if different.
1650 * @param t Tunnel we want to add a new peer to
1651 * @param peer PeerInfo of the peer being added
1655 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1657 struct GNUNET_PeerIdentity id;
1658 struct MeshPeerPath *best_p;
1659 struct MeshPeerPath *p;
1660 unsigned int best_cost;
1663 GNUNET_PEER_resolve(peer->id, &id);
1665 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1668 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1669 GNUNET_CONTAINER_multihashmap_put(
1673 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1676 if (NULL != (p = peer->path_head))
1679 best_cost = path_get_cost(t->tree, p);
1682 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1689 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1690 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1691 t->path_refresh_task =
1692 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1696 /* Start a DHT get if necessary */
1697 peer_info_connect(peer, t);
1702 * Add a path to a tunnel which we don't own, just to remember the next hop.
1703 * If destination node was already in the tunnel, the first hop information
1704 * will be replaced with the new path.
1706 * @param t Tunnel we want to add a new peer to
1707 * @param p Path to add
1708 * @param own_pos Position of local node in path.
1712 tunnel_add_path (struct MeshTunnel *t,
1713 struct MeshPeerPath *p,
1714 unsigned int own_pos)
1716 struct GNUNET_PeerIdentity id;
1718 GNUNET_assert (0 != own_pos);
1719 tree_add_path(t->tree, p, NULL);
1720 if (NULL == t->tree->me)
1721 t->tree->me = tree_find_peer(t->tree->root, p->peers[own_pos]);
1722 if (own_pos < p->length - 1)
1724 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1725 tree_update_first_hops(t->tree, t->tree->me, &id);
1731 * Notify a tunnel that a connection has broken that affects at least
1732 * some of its peers.
1734 * @param t Tunnel affected.
1735 * @param peer Peer that (at least) has been affected by the disconnection.
1736 * @param p1 Peer that got disconnected from p2.
1737 * @param p2 Peer that got disconnected from p1.
1739 * @return Short ID of the peer disconnected (either p1 or p2).
1740 * 0 if the tunnel remained unaffected.
1742 static GNUNET_PEER_Id
1743 tunnel_notify_connection_broken (struct MeshTunnel *t,
1744 struct MeshPeerInfo *peer,
1748 return tree_notify_connection_broken (t->tree, p1, p2,
1749 ¬ify_peer_disconnected);
1754 * Send a message in a tunnel in multicast, sending a copy to each child node
1755 * down the local one in the tunnel tree.
1757 * @param t Tunnel in which to send the data.
1758 * @param msg Message to be sent
1760 * @return Number of copies sent.
1763 tunnel_send_multicast (struct MeshTunnel *t,
1764 const struct GNUNET_MessageHeader *msg)
1766 struct GNUNET_PeerIdentity neighbor;
1767 struct MeshDataDescriptor *info;
1768 struct MeshTunnelTreeNode *n;
1769 unsigned int *copies;
1774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1775 "MESH: sending a multicast packet...\n");
1776 size = ntohs (msg->size);
1777 GNUNET_assert (NULL != t->tree->me);
1778 n = t->tree->me->children_head;
1781 copies = GNUNET_malloc (sizeof (unsigned int));
1782 for (*copies = 0; NULL != n; n = n->next)
1784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (%u copies)\n", *copies);
1785 n = t->tree->me->children_head;
1786 data = GNUNET_malloc (size);
1787 memcpy (data, msg, size);
1790 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1793 info->copies = copies;
1794 if (NULL != t->client)
1796 info->client = t->client->handle;
1797 info->timeout_task = GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT,
1801 info->destination = n->peer;
1802 GNUNET_PEER_resolve (n->peer, &neighbor);
1803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1804 "MESH: sending to %s...\n",
1805 GNUNET_i2s (&neighbor));
1806 info->peer = peer_info_get(&neighbor);
1807 GNUNET_assert (NULL != info->peer);
1808 i = peer_info_transmit_slot(info->peer);
1809 info->handler_n = i;
1810 info->peer->infos[i] = info;
1811 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1812 info->peer->core_transmit[i] =
1813 GNUNET_CORE_notify_transmit_ready (core_handle,
1816 GNUNET_TIME_UNIT_FOREVER_REL,
1819 &send_core_data_multicast, info);
1827 * Send a message to all peers in this tunnel that the tunnel is no longer
1830 * @param t The tunnel whose peers to notify.
1833 tunnel_send_destroy (struct MeshTunnel *t)
1835 struct GNUNET_MESH_TunnelDestroy msg;
1837 msg.header.size = htons (sizeof (msg));
1838 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
1839 msg.oid = my_full_id;
1840 msg.tid = htonl (t->id.tid);
1841 tunnel_send_multicast (t, &msg.header);
1847 * Destroy the tunnel and free any allocated resources linked to it
1849 * @param t the tunnel to destroy
1851 * @return GNUNET_OK on success
1854 tunnel_destroy (struct MeshTunnel *t)
1856 struct MeshClient *c;
1857 struct MeshQueue *q;
1858 struct MeshQueue *qn;
1859 GNUNET_HashCode hash;
1868 struct GNUNET_PeerIdentity id;
1870 GNUNET_PEER_resolve(t->id.oid, &id);
1871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1872 "MESH: destroying tunnel %s [%x]\n",
1876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1880 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1881 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1886 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1888 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1892 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1894 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1895 GNUNET_break (GNUNET_YES ==
1896 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t));
1899 if (NULL != t->peers)
1901 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
1902 &peer_info_delete_tunnel, t);
1903 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1908 if (NULL != q->data)
1909 GNUNET_free (q->data);
1913 /* TODO cancel core transmit ready in case it was active */
1915 tree_destroy(t->tree);
1916 if (NULL != t->dht_get_type)
1917 GNUNET_DHT_get_stop(t->dht_get_type);
1918 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
1919 GNUNET_SCHEDULER_cancel(t->timeout_task);
1920 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
1921 GNUNET_SCHEDULER_cancel(t->path_refresh_task);
1928 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1929 * that are no longer needed, as well as nodes of no longer reachable peers.
1930 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1932 * @param t Tunnel from which to remove the path.
1933 * @param p Peer which should be removed.
1936 tunnel_delete_peer (struct MeshTunnel *t,
1937 GNUNET_PEER_Id peer)
1939 GNUNET_break (GNUNET_OK == tree_del_peer (t->tree, peer, NULL));
1940 if (NULL == t->tree->root)
1946 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1947 * client when the client disconnects.
1949 * @param cls closure (client that is disconnecting)
1950 * @param key the hash of the local tunnel id (used to access the hashmap)
1951 * @param value the value stored at the key (tunnel to destroy)
1953 * @return GNUNET_OK on success
1956 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1958 struct MeshTunnel *t = value;
1961 if (NULL != t->dht_get_type)
1963 GNUNET_DHT_get_stop (t->dht_get_type);
1965 r = tunnel_destroy (t);
1971 * Timeout function, destroys tunnel if called
1973 * @param cls Closure (tunnel to destroy).
1974 * @param tc TaskContext
1977 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1979 struct MeshTunnel *t = cls;
1981 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
1983 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1988 * Resets the tunnel timeout. Starts it if no timeout was running.
1990 * @param t Tunnel whose timeout to reset.
1993 tunnel_reset_timeout (struct MeshTunnel *t)
1995 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
1996 GNUNET_SCHEDULER_cancel (t->timeout_task);
1997 t->timeout_task = GNUNET_SCHEDULER_add_delayed (
1998 GNUNET_TIME_relative_multiply(REFRESH_PATH_TIME, 4),
2004 /******************************************************************************/
2005 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2006 /******************************************************************************/
2009 * Function called to notify a client about the socket
2010 * being ready to queue more data. "buf" will be
2011 * NULL and "size" zero if the socket was closed for
2012 * writing in the meantime.
2014 * @param cls closure
2015 * @param size number of bytes available in buf
2016 * @param buf where the callee should write the message
2017 * @return number of bytes written to buf
2020 send_core_create_path (void *cls, size_t size, void *buf)
2022 struct MeshPathInfo *info = cls;
2023 struct GNUNET_MESH_ManipulatePath *msg;
2024 struct GNUNET_PeerIdentity *peer_ptr;
2025 struct MeshPeerInfo *peer = info->peer;
2026 struct MeshTunnel *t = info->t;
2027 struct MeshPeerPath *p = info->path;
2031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2032 "MESH: CREATE PATH sending...\n");
2034 sizeof (struct GNUNET_MESH_ManipulatePath) +
2035 p->length * sizeof (struct GNUNET_PeerIdentity);
2037 if (size < size_needed || NULL == buf)
2039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
2043 info->peer->core_transmit[info->pos] =
2044 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2045 GNUNET_TIME_UNIT_FOREVER_REL,
2046 path_get_first_hop (t->tree, peer->id),
2048 &send_core_create_path,
2052 info->peer->core_transmit[info->pos] = NULL;
2055 struct GNUNET_PeerIdentity id;
2057 GNUNET_PEER_resolve (peer->id, &id);
2058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2059 "MESH: setting core_transmit %s [%u] to NULL\n",
2064 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2065 msg->header.size = htons (size_needed);
2066 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2067 msg->tid = ntohl (t->id.tid);
2069 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2070 for (i = 0; i < p->length; i++)
2072 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2079 "MESH: CREATE PATH (%u bytes long) sent!\n",
2086 * Function called to notify a client about the socket
2087 * being ready to queue more data. "buf" will be
2088 * NULL and "size" zero if the socket was closed for
2089 * writing in the meantime.
2091 * @param cls closure (data itself)
2092 * @param size number of bytes available in buf
2093 * @param buf where the callee should write the message
2095 * @return number of bytes written to buf
2098 send_core_data_multicast (void *cls, size_t size, void *buf)
2100 struct MeshDataDescriptor *info = cls;
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2104 GNUNET_assert (NULL != info);
2105 GNUNET_assert (NULL != info->peer);
2106 total_size = info->size;
2107 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2109 if (total_size > size)
2112 struct GNUNET_PeerIdentity id;
2114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2115 "MESH: Multicast: retransmitting... (%u/%u)\n",
2117 GNUNET_PEER_resolve(info->peer->id, &id);
2118 info->peer->core_transmit[info->handler_n] =
2119 GNUNET_CORE_notify_transmit_ready (core_handle,
2122 GNUNET_TIME_UNIT_FOREVER_REL,
2125 &send_core_data_multicast,
2129 info->peer->core_transmit[info->handler_n] = NULL;
2130 info->peer->infos[info->handler_n] = NULL;
2131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2132 memcpy (buf, info->data, total_size);
2135 struct GNUNET_MESH_Multicast *mc;
2136 struct GNUNET_MessageHeader *mh;
2139 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2141 mc = (struct GNUNET_MESH_Multicast *) mh;
2142 mh = (struct GNUNET_MessageHeader *) &mc[1];
2143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2144 "MESH: multicast, payload type %u\n",
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2147 "MESH: multicast, payload size %u\n",
2152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2158 if (0 == --(*info->copies))
2160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
2161 if (NULL != info->client)
2163 if (GNUNET_SCHEDULER_NO_TASK != info->timeout_task)
2165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2166 "MESH: cancelling client timeout (%u)...\n",
2167 info->timeout_task);
2168 GNUNET_SCHEDULER_cancel(info->timeout_task);
2169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
2170 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
2173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing memory...\n");
2174 GNUNET_free (info->data);
2175 GNUNET_free (info->copies);
2177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2185 * Function called to notify a client about the socket
2186 * being ready to queue more data. "buf" will be
2187 * NULL and "size" zero if the socket was closed for
2188 * writing in the meantime.
2190 * @param cls closure (MeshDataDescriptor)
2191 * @param size number of bytes available in buf
2192 * @param buf where the callee should write the message
2193 * @return number of bytes written to buf
2196 send_core_path_ack (void *cls, size_t size, void *buf)
2198 struct MeshDataDescriptor *info = cls;
2199 struct GNUNET_MESH_PathACK *msg = buf;
2201 GNUNET_assert (NULL != info);
2204 info->peer->core_transmit[info->handler_n] = NULL;
2206 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2211 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2212 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2213 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2214 msg->tid = htonl (info->origin->tid);
2215 msg->peer_id = my_full_id;
2217 /* TODO add signature */
2219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2220 return sizeof (struct GNUNET_MESH_PathACK);
2226 * Send another peer a notification to destroy a tunnel
2227 * @param cls The tunnel to destroy
2228 * @param size Size in the buffer
2229 * @param buf Memory where to put the data to transmit
2230 * @return Size of data put in buffer
2233 send_core_tunnel_destroy (void *cls, size_t size, void *buf)
2235 struct MeshTunnel *t = cls;
2236 struct MeshClient *c;
2237 struct GNUNET_MESH_TunnelMessage *msg;
2241 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2242 /*FIXME*/ msg->header.size =
2243 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
2244 msg->tunnel_id = htonl (t->id.tid);
2246 tunnel_destroy (c, t);
2247 return sizeof (struct GNUNET_MESH_TunnelMessage);
2252 /******************************************************************************/
2253 /******************** MESH NETWORK HANDLERS **************************/
2254 /******************************************************************************/
2258 * Core handler for path creation
2260 * @param cls closure
2261 * @param message message
2262 * @param peer peer identity this notification is about
2263 * @param atsi performance data
2265 * @return GNUNET_OK to keep the connection open,
2266 * GNUNET_SYSERR to close it (signal serious error)
2269 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2270 const struct GNUNET_MessageHeader *message,
2271 const struct GNUNET_ATS_Information *atsi)
2273 unsigned int own_pos;
2276 MESH_TunnelNumber tid;
2277 struct GNUNET_MESH_ManipulatePath *msg;
2278 struct GNUNET_PeerIdentity *pi;
2279 GNUNET_HashCode hash;
2280 struct MeshPeerPath *path;
2281 struct MeshPeerInfo *dest_peer_info;
2282 struct MeshPeerInfo *orig_peer_info;
2283 struct MeshTunnel *t;
2285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2286 "MESH: Received a path create msg [%s]\n",
2287 GNUNET_i2s(&my_full_id));
2288 size = ntohs (message->size);
2289 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2291 GNUNET_break_op (0);
2295 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2296 if (size % sizeof (struct GNUNET_PeerIdentity))
2298 GNUNET_break_op (0);
2301 size /= sizeof (struct GNUNET_PeerIdentity);
2304 GNUNET_break_op (0);
2307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2308 "MESH: path has %u hops.\n",
2310 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2312 tid = ntohl (msg->tid);
2313 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2314 t = tunnel_get (pi, tid);
2315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2316 "MESH: path is for tunnel %s [%X].\n",
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2322 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2323 t->id.oid = GNUNET_PEER_intern (pi);
2325 t->local_tid = next_local_tid++;
2326 /* FIXME test if taken */
2327 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2328 t->tree = tree_new(t, t->id.oid);
2330 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2332 GNUNET_CONTAINER_multihashmap_put (
2336 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2342 tunnel_reset_timeout (t);
2343 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2345 GNUNET_CONTAINER_multihashmap_put (
2349 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2357 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2358 if (NULL == dest_peer_info)
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2361 "MESH: Creating PeerInfo for destination.\n");
2362 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2363 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2364 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2366 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2368 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2369 if (NULL == orig_peer_info)
2371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2372 "MESH: Creating PeerInfo for origin.\n");
2373 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2374 orig_peer_info->id = GNUNET_PEER_intern (pi);
2375 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2376 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2379 path = path_new (size);
2381 for (i = 0; i < size; i++)
2383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2384 "MESH: ... adding %s\n",
2385 GNUNET_i2s(&pi[i]));
2386 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2387 if (path->peers[i] == myid)
2390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2391 "MESH: Own position: %u\n", own_pos);
2394 /* cannot be self, must be 'not found' */
2395 /* create path: self not found in path through self */
2396 GNUNET_break_op (0);
2397 path_destroy (path);
2398 /* FIXME error. destroy tunnel? leave for timeout? */
2401 tunnel_add_path (t, path, own_pos);
2402 if (own_pos == size - 1)
2404 /* It is for us! Send ack. */
2405 struct GNUNET_MESH_TunnelNotification cmsg;
2406 struct MeshDataDescriptor *info;
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410 "MESH: It's for us!\n");
2411 path_add_to_origin (orig_peer_info, path);
2412 t->peers = GNUNET_CONTAINER_multihashmap_create(4);
2413 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2415 &my_full_id.hashPubKey,
2416 peer_info_get(&my_full_id),
2417 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2418 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2419 info->origin = &t->id;
2420 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2421 GNUNET_assert (NULL != info->peer);
2422 j = peer_info_transmit_slot(info->peer);
2423 info->handler_n = j;
2424 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2425 info->peer->infos[j] = info;
2426 info->peer->core_transmit[j] =
2427 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2428 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2429 sizeof (struct GNUNET_MESH_PathACK),
2430 &send_core_path_ack, info);
2431 cmsg.header.size = htons(sizeof(cmsg));
2432 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2433 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2434 cmsg.tunnel_id = htonl(t->local_tid);
2435 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2439 struct MeshPeerPath *path2;
2441 /* It's for somebody else! Retransmit. */
2442 path2 = path_duplicate(path);
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2444 "MESH: Retransmitting.\n");
2445 path_add_to_peer(dest_peer_info, path);
2446 path = path_duplicate(path2);
2447 path_add_to_origin(orig_peer_info, path2);
2448 send_create_path(dest_peer_info, path, t);
2455 * Core handler for path destruction
2457 * @param cls closure
2458 * @param message message
2459 * @param peer peer identity this notification is about
2460 * @param atsi performance data
2462 * @return GNUNET_OK to keep the connection open,
2463 * GNUNET_SYSERR to close it (signal serious error)
2466 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2467 const struct GNUNET_MessageHeader *message,
2468 const struct GNUNET_ATS_Information *atsi)
2470 struct GNUNET_MESH_ManipulatePath *msg;
2471 struct GNUNET_PeerIdentity *pi;
2472 struct MeshPeerPath *path;
2473 struct MeshTunnel *t;
2474 unsigned int own_pos;
2478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2479 "MESH: Received a PATH DESTROY msg from %s\n",
2481 size = ntohs (message->size);
2482 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2484 GNUNET_break_op (0);
2488 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2489 if (size % sizeof (struct GNUNET_PeerIdentity))
2491 GNUNET_break_op (0);
2494 size /= sizeof (struct GNUNET_PeerIdentity);
2497 GNUNET_break_op (0);
2500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2501 "MESH: path has %u hops.\n",
2504 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2505 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2507 "MESH: path is for tunnel %s [%X].\n",
2510 t = tunnel_get (pi, ntohl (msg->tid));
2513 /* TODO notify back: we don't know this tunnel */
2514 GNUNET_break_op (0);
2517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2518 path = path_new (size);
2520 for (i = 0; i < size; i++)
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2523 "MESH: ... adding %s\n",
2524 GNUNET_i2s(&pi[i]));
2525 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2526 if (path->peers[i] == myid)
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2530 "MESH: Own position: %u\n", own_pos);
2531 if (own_pos < path->length - 1)
2532 send_message (message, &pi[own_pos + 1]);
2533 tunnel_delete_peer (t, path->peers[path->length - 1]);
2539 * Core handler for tunnel destruction
2541 * @param cls closure
2542 * @param message message
2543 * @param peer peer identity this notification is about
2544 * @param atsi performance data
2546 * @return GNUNET_OK to keep the connection open,
2547 * GNUNET_SYSERR to close it (signal serious error)
2550 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2551 const struct GNUNET_MessageHeader *message,
2552 const struct GNUNET_ATS_Information *atsi)
2554 struct GNUNET_MESH_TunnelDestroy *msg;
2555 struct MeshTunnel *t;
2557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2558 "MESH: Got a TUNNEL DESTROY packet from %s\n",
2560 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2561 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2564 /* TODO notify back: we don't know this tunnel */
2565 GNUNET_break_op (0);
2568 tunnel_send_destroy (t);
2575 * Core handler for mesh network traffic going from the origin to a peer
2577 * @param cls closure
2578 * @param peer peer identity this notification is about
2579 * @param message message
2580 * @param atsi performance data
2581 * @return GNUNET_OK to keep the connection open,
2582 * GNUNET_SYSERR to close it (signal serious error)
2585 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2586 const struct GNUNET_MessageHeader *message,
2587 const struct GNUNET_ATS_Information *atsi)
2589 struct GNUNET_MESH_Unicast *msg;
2590 struct MeshTunnel *t;
2594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2595 "MESH: got a unicast packet from %s\n",
2597 size = ntohs (message->size);
2599 sizeof (struct GNUNET_MESH_Unicast) +
2600 sizeof (struct GNUNET_MessageHeader))
2605 msg = (struct GNUNET_MESH_Unicast *) message;
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2607 "MESH: of type %u\n",
2608 ntohs (msg[1].header.type));
2609 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2612 /* TODO notify back: we don't know this tunnel */
2613 GNUNET_break_op (0);
2616 tunnel_reset_timeout (t);
2617 pid = GNUNET_PEER_search(&msg->destination);
2620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2621 "MESH: it's for us! sending to clients...\n");
2622 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2626 "MESH: not for us, retransmitting...\n");
2627 send_message (message, path_get_first_hop(t->tree, pid));
2633 * Core handler for mesh network traffic going from the origin to all peers
2635 * @param cls closure
2636 * @param message message
2637 * @param peer peer identity this notification is about
2638 * @param atsi performance data
2639 * @return GNUNET_OK to keep the connection open,
2640 * GNUNET_SYSERR to close it (signal serious error)
2642 * TODO: Check who we got this from, to validate route.
2645 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2646 const struct GNUNET_MessageHeader *message,
2647 const struct GNUNET_ATS_Information *atsi)
2649 struct GNUNET_MESH_Multicast *msg;
2650 struct MeshTunnel *t;
2653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2654 "MESH: got a multicast packet from %s\n",
2656 size = ntohs (message->size);
2657 if (sizeof (struct GNUNET_MESH_Multicast) +
2658 sizeof (struct GNUNET_MessageHeader) > size)
2660 GNUNET_break_op (0);
2663 msg = (struct GNUNET_MESH_Multicast *) message;
2664 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2668 /* TODO notify that we dont know that tunnel */
2669 GNUNET_break_op (0);
2672 tunnel_reset_timeout (t);
2674 /* Transmit to locally interested clients */
2675 if (NULL != t->peers &&
2676 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2678 send_subscribed_clients (message, &msg[1].header);
2680 tunnel_send_multicast(t, message);
2686 * Core handler for mesh network traffic toward the owner of a tunnel
2688 * @param cls closure
2689 * @param message message
2690 * @param peer peer identity this notification is about
2691 * @param atsi performance data
2693 * @return GNUNET_OK to keep the connection open,
2694 * GNUNET_SYSERR to close it (signal serious error)
2697 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2698 const struct GNUNET_MessageHeader *message,
2699 const struct GNUNET_ATS_Information *atsi)
2701 struct GNUNET_MESH_ToOrigin *msg;
2702 struct GNUNET_PeerIdentity id;
2703 struct MeshPeerInfo *peer_info;
2704 struct MeshTunnel *t;
2707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2708 "MESH: got a ToOrigin packet from %s\n",
2710 size = ntohs (message->size);
2711 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2712 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2714 GNUNET_break_op (0);
2717 msg = (struct GNUNET_MESH_ToOrigin *) message;
2718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2719 "MESH: of type %u\n",
2720 ntohs (msg[1].header.type));
2721 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2725 /* TODO notify that we dont know this tunnel (whom)? */
2726 GNUNET_break_op (0);
2730 if (t->id.oid == myid)
2733 struct GNUNET_MESH_ToOrigin *copy;
2735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2736 "MESH: it's for us! sending to clients...\n");
2737 if (NULL == t->client)
2739 /* got data packet for ownerless tunnel */
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2741 "MESH: no clients!\n");
2742 GNUNET_break_op (0);
2745 /* TODO signature verification */
2746 memcpy (cbuf, message, size);
2747 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2748 copy->tid = htonl (t->local_tid);
2749 GNUNET_SERVER_notification_context_unicast (nc,
2755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2756 "MESH: not for us, retransmitting...\n");
2758 peer_info = peer_info_get (&msg->oid);
2759 if (NULL == peer_info)
2761 /* unknown origin of tunnel */
2765 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
2766 send_message (message, &id);
2773 * Core handler for path ACKs
2775 * @param cls closure
2776 * @param message message
2777 * @param peer peer identity this notification is about
2778 * @param atsi performance data
2780 * @return GNUNET_OK to keep the connection open,
2781 * GNUNET_SYSERR to close it (signal serious error)
2784 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2785 const struct GNUNET_MessageHeader *message,
2786 const struct GNUNET_ATS_Information *atsi)
2788 struct GNUNET_MESH_PathACK *msg;
2789 struct GNUNET_PeerIdentity id;
2790 struct MeshTunnelTreeNode *n;
2791 struct MeshPeerInfo *peer_info;
2792 struct MeshTunnel *t;
2794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2795 "MESH: Received a path ACK msg [%s]\n",
2796 GNUNET_i2s(&my_full_id));
2797 msg = (struct GNUNET_MESH_PathACK *) message;
2798 t = tunnel_get (&msg->oid, msg->tid);
2801 /* TODO notify that we don't know the tunnel */
2805 /* Message for us? */
2806 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
2809 if (NULL == t->client)
2811 GNUNET_break_op (0);
2814 peer_info = peer_info_get (&msg->peer_id);
2815 if (NULL == peer_info)
2817 GNUNET_break_op (0);
2820 n = tree_find_peer(t->tree->root, peer_info->id);
2823 GNUNET_break_op (0);
2826 n->status = MESH_PEER_READY;
2827 send_client_peer_connected(t, peer_info->id);
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2832 "MESH: not for us, retransmitting...\n");
2833 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
2834 peer_info = peer_info_get (&msg->oid);
2835 if (NULL == peer_info)
2837 /* If we know the tunnel, we should DEFINITELY know the peer */
2841 send_message (message, &id);
2847 * Functions to handle messages from core
2849 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2850 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2851 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
2852 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
2853 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2854 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2855 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2856 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2857 sizeof (struct GNUNET_MESH_PathACK)},
2863 /******************************************************************************/
2864 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2865 /******************************************************************************/
2868 * deregister_app: iterator for removing each application registered by a client
2870 * @param cls closure
2871 * @param key the hash of the application id (used to access the hashmap)
2872 * @param value the value stored at the key (client)
2874 * @return GNUNET_OK on success
2877 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2879 GNUNET_break (GNUNET_YES ==
2880 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
2886 * notify_client_connection_failure: notify a client that the connection to the
2887 * requested remote peer is not possible (for instance, no route found)
2888 * Function called when the socket is ready to queue more data. "buf" will be
2889 * NULL and "size" zero if the socket was closed for writing in the meantime.
2891 * @param cls closure
2892 * @param size number of bytes available in buf
2893 * @param buf where the callee should write the message
2894 * @return number of bytes written to buf
2897 notify_client_connection_failure (void *cls, size_t size, void *buf)
2900 struct MeshPeerInfo *peer_info;
2901 struct GNUNET_MESH_PeerControl *msg;
2902 struct GNUNET_PeerIdentity id;
2904 if (0 == size && NULL == buf)
2906 // TODO retry? cancel?
2910 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2911 peer_info = (struct MeshPeerInfo *) cls;
2912 msg = (struct GNUNET_MESH_PeerControl *) buf;
2913 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2914 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2915 // msg->tunnel_id = htonl(peer_info->t->tid);
2916 GNUNET_PEER_resolve (peer_info->id, &id);
2917 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2925 * Send keepalive packets for a peer
2927 * @param cls Closure (tunnel for which to send the keepalive).
2928 * @param tc Notification context.
2930 * TODO: implement explicit multicast keepalive?
2933 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2935 struct MeshTunnel *t = cls;
2936 struct GNUNET_MessageHeader *payload;
2937 struct GNUNET_MESH_Multicast *msg;
2938 size_t size = sizeof(struct GNUNET_MESH_Multicast) +
2939 sizeof(struct GNUNET_MessageHeader);
2942 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2946 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2949 "MESH: sending keepalive for tunnel %d\n",
2952 msg = (struct GNUNET_MESH_Multicast *) cbuf;
2953 msg->header.size = htons (size);
2954 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2955 msg->oid = my_full_id;
2956 msg->tid = htonl(t->id.tid);
2957 payload = (struct GNUNET_MessageHeader *) &msg[1];
2958 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2959 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
2960 tunnel_send_multicast (t, &msg->header);
2962 t->path_refresh_task =
2963 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2969 * Function to process paths received for a new peer addition. The recorded
2970 * paths form the initial tunnel, which can be optimized later.
2971 * Called on each result obtained for the DHT search.
2973 * @param cls closure
2974 * @param exp when will this value expire
2975 * @param key key of the result
2976 * @param type type of the result
2977 * @param size number of bytes in data
2978 * @param data pointer to the result data
2980 * TODO: re-issue the request after certain time? cancel after X results?
2983 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2984 const GNUNET_HashCode * key,
2985 const struct GNUNET_PeerIdentity *get_path,
2986 unsigned int get_path_length,
2987 const struct GNUNET_PeerIdentity *put_path,
2988 unsigned int put_path_length,
2989 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2991 struct MeshPathInfo *path_info = cls;
2992 struct MeshPeerPath *p;
2993 struct GNUNET_PeerIdentity pi;
2996 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2997 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2998 "MESH: Got results from DHT for %s\n",
2999 GNUNET_h2s_full(&pi.hashPubKey));
3000 GNUNET_DHT_get_stop(path_info->peer->dhtget);
3001 path_info->peer->dhtget = NULL;
3003 p = path_build_from_dht (get_path, get_path_length,
3004 put_path, put_path_length);
3005 path_add_to_peer (path_info->peer, p);
3006 for (i = 0; i < path_info->peer->ntunnels; i++)
3008 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3009 peer_info_connect(path_info->peer, path_info->t);
3011 GNUNET_free (path_info);
3018 * Function to process paths received for a new peer addition. The recorded
3019 * paths form the initial tunnel, which can be optimized later.
3020 * Called on each result obtained for the DHT search.
3022 * @param cls closure
3023 * @param exp when will this value expire
3024 * @param key key of the result
3025 * @param type type of the result
3026 * @param size number of bytes in data
3027 * @param data pointer to the result data
3030 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3031 const GNUNET_HashCode * key,
3032 const struct GNUNET_PeerIdentity *get_path,
3033 unsigned int get_path_length,
3034 const struct GNUNET_PeerIdentity *put_path,
3035 unsigned int put_path_length,
3036 enum GNUNET_BLOCK_Type type, size_t size,
3039 const struct GNUNET_PeerIdentity *pi = data;
3040 struct GNUNET_PeerIdentity id;
3041 struct MeshTunnel *t = cls;
3042 struct MeshPeerInfo *peer_info;
3043 struct MeshPathInfo *path_info;
3044 struct MeshPeerPath *p;
3047 if (size != sizeof (struct GNUNET_PeerIdentity))
3049 GNUNET_break_op (0);
3052 GNUNET_assert (NULL != t->client);
3053 GNUNET_DHT_get_stop (t->dht_get_type);
3054 t->dht_get_type = NULL;
3055 peer_info = peer_info_get (pi);
3056 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
3057 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3059 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
3060 NULL == peer_info->dhtget)
3062 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3063 path_info->peer = peer_info;
3065 /* we don't have a route to the peer, let's try a direct lookup */
3067 GNUNET_DHT_get_start (dht_handle, /* handle */
3068 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
3069 GNUNET_BLOCK_TYPE_TEST, /* block type */
3070 &pi->hashPubKey, /* key to look up */
3071 10U, /* replication level */
3072 GNUNET_DHT_RO_RECORD_ROUTE |
3073 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3074 /* option to dht: record route */
3076 0, /* xquery bits */
3077 dht_get_id_handler, /* callback */
3078 path_info); /* closure */
3082 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
3083 path_add_to_peer (peer_info, p);
3084 tunnel_add_peer(t, peer_info);
3085 p = tree_get_path_to_peer(t->tree, peer_info->id);
3087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3088 "MESH: new route for tunnel 0x%x found, has %u hops\n",
3089 t->local_tid, p->length);
3090 for (i = 0; i < p->length; i++)
3092 GNUNET_PEER_resolve (p->peers[0], &id);
3093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
3094 GNUNET_h2s_full (&id.hashPubKey));
3100 send_create_path(peer_info, p, t);
3104 send_client_peer_connected(t, myid);
3108 /******************************************************************************/
3109 /********************* MESH LOCAL HANDLES **************************/
3110 /******************************************************************************/
3114 * Handler for client disconnection
3116 * @param cls closure
3117 * @param client identification of the client; NULL
3118 * for the last call when the server is destroyed
3121 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3123 struct MeshClient *c;
3124 struct MeshClient *next;
3126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3132 if (c->handle != client && NULL != client)
3134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
3139 if (NULL != c->tunnels)
3141 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3142 &tunnel_destroy_iterator,
3144 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3147 /* deregister clients applications */
3148 if (NULL != c->apps)
3150 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3151 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3153 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3154 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3156 GNUNET_SCHEDULER_cancel (announce_applications_task);
3157 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3159 if (NULL != c->types)
3160 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3161 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3173 * Handler for new clients
3175 * @param cls closure
3176 * @param client identification of the client
3177 * @param message the actual message, which includes messages the client wants
3180 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3181 const struct GNUNET_MessageHeader *message)
3183 struct GNUNET_MESH_ClientConnect *cc_msg;
3184 struct MeshClient *c;
3185 GNUNET_MESH_ApplicationType *a;
3192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3193 /* Check data sanity */
3194 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3195 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3196 ntypes = ntohs (cc_msg->types);
3197 napps = ntohs (cc_msg->applications);
3199 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3202 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3206 /* Create new client structure */
3207 c = GNUNET_malloc (sizeof (struct MeshClient));
3209 c->id = next_client_id++;
3212 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3215 GNUNET_MESH_ApplicationType at;
3218 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3219 for (i = 0; i < napps; i++)
3222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3223 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3224 /* store in clients hashmap */
3225 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3226 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3227 /* store in global hashmap, for announcements */
3228 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3229 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3231 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3232 announce_applications_task =
3233 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3241 t = (uint16_t *) & a[napps];
3242 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3243 for (i = 0; i < ntypes; i++)
3246 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3248 /* store in clients hashmap */
3249 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3250 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3251 /* store in global hashmap */
3252 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3253 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3257 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3259 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3260 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3261 GNUNET_SERVER_notification_context_add (nc, client);
3263 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3271 * Handler for requests of new tunnels
3273 * @param cls closure
3274 * @param client identification of the client
3275 * @param message the actual message
3278 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3279 const struct GNUNET_MessageHeader *message)
3281 struct GNUNET_MESH_TunnelMessage *t_msg;
3282 struct MeshTunnel *t;
3283 struct MeshClient *c;
3284 GNUNET_HashCode hash;
3286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3288 /* Sanity check for client registration */
3289 if (NULL == (c = client_get (client)))
3292 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3299 /* Message sanity check */
3300 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3307 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3308 /* Sanity check for tunnel numbering */
3309 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3312 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3315 /* Sanity check for duplicate tunnel IDs */
3316 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3319 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3323 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
3325 while (NULL != tunnel_get_by_pi (myid, next_tid))
3326 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3327 t->id.tid = next_tid++;
3329 t->local_tid = ntohl (t_msg->tunnel_id);
3331 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3333 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3335 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3336 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3339 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3343 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3345 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3346 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3349 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3352 t->tree = tree_new (t, myid);
3353 t->tree->refresh = REFRESH_PATH_TIME;
3354 t->tree->root->status = MESH_PEER_READY;
3355 t->tree->me = t->tree->root;
3357 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3363 * Handler for requests of deleting tunnels
3365 * @param cls closure
3366 * @param client identification of the client
3367 * @param message the actual message
3370 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3371 const struct GNUNET_MessageHeader *message)
3373 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3374 struct MeshClient *c;
3375 struct MeshTunnel *t;
3376 MESH_TunnelNumber tid;
3377 GNUNET_HashCode hash;
3379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
3381 /* Sanity check for client registration */
3382 if (NULL == (c = client_get (client)))
3385 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3388 /* Message sanity check */
3389 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3392 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3398 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3400 /* Retrieve tunnel */
3401 tid = ntohl (tunnel_msg->tunnel_id);
3403 /* Remove from local id hashmap */
3404 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3405 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3406 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
3409 tunnel_send_destroy (t);
3411 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3417 * Handler for connection requests to new peers
3419 * @param cls closure
3420 * @param client identification of the client
3421 * @param message the actual message (PeerControl)
3424 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3425 const struct GNUNET_MessageHeader *message)
3427 struct GNUNET_MESH_PeerControl *peer_msg;
3428 struct MeshPeerInfo *peer_info;
3429 struct MeshClient *c;
3430 struct MeshTunnel *t;
3431 MESH_TunnelNumber tid;
3433 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3434 /* Sanity check for client registration */
3435 if (NULL == (c = client_get (client)))
3438 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3442 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3443 /* Sanity check for message size */
3444 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3447 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3451 /* Tunnel exists? */
3452 tid = ntohl (peer_msg->tunnel_id);
3453 t = tunnel_get_by_local_id (c, tid);
3457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3461 /* Does client own tunnel? */
3462 if (t->client->handle != client)
3465 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3468 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3469 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
3470 peer_info = peer_info_get (&peer_msg->peer);
3472 tunnel_add_peer(t, peer_info);
3473 peer_info_connect(peer_info, t);
3475 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3481 * Handler for disconnection requests of peers in a tunnel
3483 * @param cls closure
3484 * @param client identification of the client
3485 * @param message the actual message (PeerControl)
3488 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3489 const struct GNUNET_MessageHeader *message)
3491 struct GNUNET_MESH_PeerControl *peer_msg;
3492 struct MeshPeerInfo *peer_info;
3493 struct MeshClient *c;
3494 struct MeshTunnel *t;
3495 MESH_TunnelNumber tid;
3497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got a PEER DEL request\n");
3498 /* Sanity check for client registration */
3499 if (NULL == (c = client_get (client)))
3502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3505 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3506 /* Sanity check for message size */
3507 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3510 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3514 /* Tunnel exists? */
3515 tid = ntohl (peer_msg->tunnel_id);
3516 t = tunnel_get_by_local_id (c, tid);
3520 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: on tunnel %X\n", t->id.tid);
3525 /* Does client own tunnel? */
3526 if (t->client->handle != client)
3529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3534 "MESH: for peer %s\n",
3535 GNUNET_i2s(&peer_msg->peer));
3536 /* Is the peer in the tunnel? */
3537 peer_info = GNUNET_CONTAINER_multihashmap_get(t->peers,
3538 &peer_msg->peer.hashPubKey);
3539 if (NULL == peer_info)
3542 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3546 /* Ok, delete peer from tunnel */
3547 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3548 &peer_msg->peer.hashPubKey);
3550 send_destroy_path (t, peer_info->id);
3551 tunnel_delete_peer(t, peer_info->id);
3552 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3558 * Handler for connection requests to new peers by type
3560 * @param cls closure
3561 * @param client identification of the client
3562 * @param message the actual message (ConnectPeerByType)
3565 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3566 const struct GNUNET_MessageHeader *message)
3568 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3569 struct MeshClient *c;
3570 struct MeshTunnel *t;
3571 GNUNET_HashCode hash;
3572 MESH_TunnelNumber tid;
3574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3575 /* Sanity check for client registration */
3576 if (NULL == (c = client_get (client)))
3579 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3583 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3584 /* Sanity check for message size */
3585 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3586 ntohs (connect_msg->header.size))
3589 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3593 /* Tunnel exists? */
3594 tid = ntohl (connect_msg->tunnel_id);
3595 t = tunnel_get_by_local_id (c, tid);
3599 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3603 /* Does client own tunnel? */
3604 if (t->client->handle != client)
3607 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3611 /* Do WE have the service? */
3612 t->type = ntohl (connect_msg->type);
3613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3614 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3615 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3618 /* Yes! Fast forward, add ourselves to the tunnel and send the
3619 * good news to the client
3621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3622 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3623 peer_info_get (&my_full_id),
3624 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3627 send_client_peer_connected(t, myid);
3628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3629 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3632 /* Ok, lets find a peer offering the service */
3633 if (NULL != t->dht_get_type)
3635 GNUNET_DHT_get_stop (t->dht_get_type);
3637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3638 GNUNET_h2s_full (&hash));
3640 GNUNET_DHT_get_start (dht_handle,
3641 GNUNET_TIME_UNIT_FOREVER_REL,
3642 GNUNET_BLOCK_TYPE_TEST,
3645 GNUNET_DHT_RO_RECORD_ROUTE |
3646 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3648 &dht_get_type_handler, t);
3650 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3656 * Handler for client traffic directed to one peer
3658 * @param cls closure
3659 * @param client identification of the client
3660 * @param message the actual message
3663 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3664 const struct GNUNET_MessageHeader *message)
3666 struct MeshClient *c;
3667 struct MeshTunnel *t;
3668 struct MeshPeerInfo *pi;
3669 struct GNUNET_MESH_Unicast *data_msg;
3670 MESH_TunnelNumber tid;
3673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3674 "MESH: Got a unicast request from a client!\n");
3676 /* Sanity check for client registration */
3677 if (NULL == (c = client_get (client)))
3680 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3683 data_msg = (struct GNUNET_MESH_Unicast *) message;
3684 /* Sanity check for message size */
3685 size = ntohs (message->size);
3686 if (sizeof (struct GNUNET_MESH_Unicast) +
3687 sizeof (struct GNUNET_MessageHeader) > size)
3690 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3694 /* Tunnel exists? */
3695 tid = ntohl (data_msg->tid);
3696 t = tunnel_get_by_local_id (c, tid);
3700 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3704 /* Is it a local tunnel? Then, does client own the tunnel? */
3705 if (NULL != t->client &&
3706 NULL != t->client->handle &&
3707 t->client->handle != client)
3710 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3714 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3715 &data_msg->destination.hashPubKey);
3716 /* Is the selected peer in the tunnel? */
3720 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3724 /* Ok, everything is correct, send the message
3725 * (pretend we got it from a mesh peer)
3728 char buf[ntohs (message->size)];
3729 struct GNUNET_MESH_Unicast *copy;
3731 /* Work around const limitation */
3732 copy = (struct GNUNET_MESH_Unicast *) buf;
3733 memcpy (buf, data_msg, size);
3734 copy->oid = my_full_id;
3735 copy->tid = htonl (t->id.tid);
3736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3737 "MESH: calling generic handler...\n");
3738 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
3740 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3746 * Handler for client traffic directed to the origin
3748 * @param cls closure
3749 * @param client identification of the client
3750 * @param message the actual message
3753 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3754 const struct GNUNET_MessageHeader *message)
3756 struct GNUNET_MESH_ToOrigin *data_msg;
3757 struct GNUNET_PeerIdentity id;
3758 struct MeshClient *c;
3759 struct MeshTunnel *t;
3760 MESH_TunnelNumber tid;
3763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3764 "MESH: Got a ToOrigin request from a client!\n");
3766 /* Sanity check for client registration */
3767 if (NULL == (c = client_get (client)))
3770 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3773 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3774 /* Sanity check for message size */
3775 size = ntohs (message->size);
3776 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3777 sizeof (struct GNUNET_MessageHeader) > size)
3780 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3784 /* Tunnel exists? */
3785 tid = ntohl (data_msg->tid);
3786 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3789 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3792 t = tunnel_get_by_local_id (c, tid);
3796 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3800 /* It shouldn't be a local tunnel. */
3801 if (NULL != t->client)
3804 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3807 GNUNET_PEER_resolve(t->id.oid, &id);
3809 /* Ok, everything is correct, send the message
3810 * (pretend we got it from a mesh peer)
3813 char buf[ntohs (message->size)];
3814 struct GNUNET_MESH_ToOrigin *copy;
3816 /* Work around const limitation */
3817 copy = (struct GNUNET_MESH_ToOrigin *) buf;
3818 memcpy (buf, data_msg, size);
3820 copy->tid = htonl (t->id.tid);
3821 copy->sender = my_full_id;
3822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3823 "MESH: calling generic handler...\n");
3824 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL);
3826 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3832 * Handler for client traffic directed to all peers in a tunnel
3834 * @param cls closure
3835 * @param client identification of the client
3836 * @param message the actual message
3839 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3840 const struct GNUNET_MessageHeader *message)
3842 struct MeshClient *c;
3843 struct MeshTunnel *t;
3844 struct GNUNET_MESH_Multicast *data_msg;
3845 MESH_TunnelNumber tid;
3847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3848 "MESH: Got a multicast request from a client!\n");
3850 /* Sanity check for client registration */
3851 if (NULL == (c = client_get (client)))
3854 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3857 data_msg = (struct GNUNET_MESH_Multicast *) message;
3858 /* Sanity check for message size */
3859 if (sizeof (struct GNUNET_MESH_Multicast) +
3860 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
3863 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3867 /* Tunnel exists? */
3868 tid = ntohl (data_msg->tid);
3869 t = tunnel_get_by_local_id (c, tid);
3873 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3877 /* Does client own tunnel? */
3878 if (t->client->handle != client)
3881 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3886 char buf[ntohs(message->size)];
3887 struct GNUNET_MESH_Multicast *copy;
3889 copy = (struct GNUNET_MESH_Multicast *)buf;
3890 memcpy(buf, message, ntohs(message->size));
3891 copy->oid = my_full_id;
3892 copy->tid = htonl(t->id.tid);
3893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3894 "MESH: calling generic handler...\n");
3895 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3898 /* receive done gets called when last copy is sent to a neighbor */
3903 * Functions to handle messages from clients
3905 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3906 {&handle_local_new_client, NULL,
3907 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3908 {&handle_local_tunnel_create, NULL,
3909 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3910 sizeof (struct GNUNET_MESH_TunnelMessage)},
3911 {&handle_local_tunnel_destroy, NULL,
3912 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3913 sizeof (struct GNUNET_MESH_TunnelMessage)},
3914 {&handle_local_connect_add, NULL,
3915 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3916 sizeof (struct GNUNET_MESH_PeerControl)},
3917 {&handle_local_connect_del, NULL,
3918 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3919 sizeof (struct GNUNET_MESH_PeerControl)},
3920 {&handle_local_connect_by_type, NULL,
3921 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3922 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3923 {&handle_local_unicast, NULL,
3924 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3925 {&handle_local_to_origin, NULL,
3926 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3927 {&handle_local_multicast, NULL,
3928 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3934 * To be called on core init/fail.
3936 * @param cls service closure
3937 * @param server handle to the server for this service
3938 * @param identity the public identity of this peer
3941 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3942 const struct GNUNET_PeerIdentity *identity)
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3945 core_handle = server;
3946 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3948 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3949 GNUNET_SCHEDULER_shutdown();
3955 * Method called whenever a given peer connects.
3957 * @param cls closure
3958 * @param peer peer identity this notification is about
3959 * @param atsi performance data for the connection
3962 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3963 const struct GNUNET_ATS_Information *atsi)
3965 struct MeshPeerInfo *peer_info;
3966 struct MeshPeerPath *path;
3968 #if MESH_DEBUG_CONNECTION
3969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3971 GNUNET_h2s(&my_full_id.hashPubKey));
3973 peer_info = peer_info_get (peer);
3974 if (myid == peer_info->id)
3976 #if MESH_DEBUG_CONNECTION
3977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3981 #if MESH_DEBUG_CONNECTION
3984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3985 GNUNET_h2s(&peer->hashPubKey));
3988 path = path_new (2);
3989 path->peers[0] = myid;
3990 path->peers[1] = peer_info->id;
3991 GNUNET_PEER_change_rc(myid, 1);
3992 GNUNET_PEER_change_rc(peer_info->id, 1);
3993 path_add_to_peer (peer_info, path);
3998 * Method called whenever a peer disconnects.
4000 * @param cls closure
4001 * @param peer peer identity this notification is about
4004 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4006 struct MeshPeerInfo *pi;
4009 #if MESH_DEBUG_CONNECTION
4010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
4012 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4018 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4020 peer_info_cancel_transmission(pi, i);
4022 path_remove_from_peer (pi, pi->id, myid);
4023 #if MESH_DEBUG_CONNECTION
4026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4033 /******************************************************************************/
4034 /************************ MAIN FUNCTIONS ****************************/
4035 /******************************************************************************/
4038 * Iterator over hash map entries.
4040 * @param cls closure
4041 * @param key current key code
4042 * @param value value in the hash map
4043 * @return GNUNET_YES if we should continue to
4048 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4050 struct MeshTunnel *t = value;
4056 * Task run during shutdown.
4062 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4065 /* TODO: destroy tunnels? */
4066 if (core_handle != NULL)
4068 GNUNET_CORE_disconnect (core_handle);
4071 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
4072 if (dht_handle != NULL)
4074 GNUNET_DHT_disconnect (dht_handle);
4079 GNUNET_SERVER_notification_context_destroy (nc);
4082 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4084 GNUNET_SCHEDULER_cancel (announce_id_task);
4085 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4091 * Process mesh requests.
4093 * @param cls closure
4094 * @param server the initialized server
4095 * @param c configuration to use
4098 run (void *cls, struct GNUNET_SERVER_Handle *server,
4099 const struct GNUNET_CONFIGURATION_Handle *c)
4101 struct MeshPeerInfo *peer;
4102 struct MeshPeerPath *p;
4105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4106 server_handle = server;
4107 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4108 CORE_QUEUE_SIZE, /* queue size */
4109 NULL, /* Closure passed to MESH functions */
4110 &core_init, /* Call core_init once connected */
4111 &core_connect, /* Handle connects */
4112 &core_disconnect, /* remove peers on disconnects */
4113 NULL, /* Don't notify about all incoming messages */
4114 GNUNET_NO, /* For header only in notification */
4115 NULL, /* Don't notify about all outbound messages */
4116 GNUNET_NO, /* For header-only out notification */
4117 core_handlers); /* Register these handlers */
4118 if (core_handle == NULL)
4121 GNUNET_SCHEDULER_shutdown ();
4126 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4131 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4132 GNUNET_SCHEDULER_shutdown ();
4135 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4136 GNUNET_free (keyfile);
4137 if (my_private_key == NULL)
4139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4140 _("Mesh service could not access hostkey. Exiting.\n"));
4141 GNUNET_SCHEDULER_shutdown ();
4144 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4145 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4146 &my_full_id.hashPubKey);
4147 myid = GNUNET_PEER_intern (&my_full_id);
4149 dht_handle = GNUNET_DHT_connect (c, 64);
4150 if (dht_handle == NULL)
4156 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4158 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4159 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4160 peers = GNUNET_CONTAINER_multihashmap_create (32);
4161 applications = GNUNET_CONTAINER_multihashmap_create (32);
4162 types = GNUNET_CONTAINER_multihashmap_create (32);
4164 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4165 nc = GNUNET_SERVER_notification_context_create (server_handle,
4167 GNUNET_SERVER_disconnect_notify (server_handle,
4168 &handle_local_client_disconnect,
4173 clients_tail = NULL;
4178 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4179 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4181 /* Create a peer_info for the local peer */
4182 peer = peer_info_get(&my_full_id);
4185 path_add_to_peer(peer, p);
4187 /* Scheduled the task to clean up when shutdown is called */
4188 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4195 * The main function for the mesh service.
4197 * @param argc number of arguments from the command line
4198 * @param argv command line arguments
4199 * @return 0 ok, 1 on error
4202 main (int argc, char *const *argv)
4207 // fprintf (stderr, "main ()\n");
4209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4212 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
4216 // fprintf (stderr, "main () END\n");