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
44 * - set ttl relative to tree depth
50 #include "mesh_protocol.h"
51 #include "gnunet_dht_service.h"
52 #include "mesh_tunnel_tree.h"
54 /* TODO: move into configuration file */
55 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
56 GNUNET_TIME_UNIT_SECONDS,\
58 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
59 GNUNET_TIME_UNIT_SECONDS,\
62 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
63 GNUNET_TIME_UNIT_SECONDS,\
66 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
67 GNUNET_TIME_UNIT_SECONDS,\
69 #define DEFAULT_TTL 64
73 #define MESH_DEBUG_DHT GNUNET_YES
74 #define MESH_DEBUG_CONNECTION GNUNET_NO
76 #if MESH_DEBUG_CONNECTION
77 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
79 #define DEBUG_CONN(...)
83 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
85 #define DEBUG_DHT(...)
88 /******************************************************************************/
89 /************************ DATA STRUCTURES ****************************/
90 /******************************************************************************/
92 /** FWD declaration */
97 * Struct representing a piece of data being sent to other peers
101 /** Tunnel it belongs to. */
102 struct MeshTunnel *t;
104 /** In case of a multicast, task to allow a client to send more data if
105 * some neighbor is too slow. */
106 GNUNET_SCHEDULER_TaskIdentifier *task;
108 /** How many remaining neighbors we need to send this to. */
109 unsigned int *reference_counter;
111 /** Size of the data. */
120 * Struct containing info about a queued transmission to this peer
127 struct MeshPeerQueue *next;
132 struct MeshPeerQueue *prev;
135 * Peer this transmission is directed to.
137 struct MeshPeerInfo *peer;
140 * Pointer to info stucture used as cls.
150 * Size of the message
157 * Struct containing all info possibly needed to build a package when called
160 struct MeshTransmissionDescriptor
162 /** ID of the tunnel this packet travels in */
163 struct MESH_TunnelID *origin;
165 /** Who was this message being sent to */
166 struct MeshPeerInfo *peer;
168 /** Ultimate destination of the packet */
169 GNUNET_PEER_Id destination;
171 /** Data descriptor */
172 struct MeshData* mesh_data;
177 * Struct containing all information regarding a given peer
187 * Last time we heard from this peer
189 struct GNUNET_TIME_Absolute last_contact;
192 * Number of attempts to reconnect so far
194 int n_reconnect_attempts;
197 * Paths to reach the peer, ordered by ascending hop count
199 struct MeshPeerPath *path_head;
202 * Paths to reach the peer, ordered by ascending hop count
204 struct MeshPeerPath *path_tail;
207 * Handle to stop the DHT search for a path to this peer
209 struct GNUNET_DHT_GetHandle *dhtget;
212 * Closure given to the DHT GET
214 struct MeshPathInfo *dhtgetcls;
217 * Transmission queue to this peer, head
219 struct MeshPeerQueue *queue_head;
222 * Transmission queue to this peer, tail
224 struct MeshPeerQueue *queue_tail;
227 * Array of tunnels this peer participates in
228 * (most probably a small amount, therefore not a hashmap)
229 * When the path to the peer changes, notify these tunnels to let them
230 * re-adjust their path trees.
232 struct MeshTunnel **tunnels;
235 * Number of tunnels this peers participates in
237 unsigned int ntunnels;
240 * Handle to stop queued transmission
242 struct GNUNET_CORE_TransmitHandle *core_transmit;
247 * Data scheduled to transmit (to local client or remote peer)
254 struct MeshQueue *next;
255 struct MeshQueue *prev;
258 * Target of the data (NULL if target is client)
260 struct MeshPeerInfo *peer;
263 * Client to send the data to (NULL if target is peer)
265 struct MeshClient *client;
268 * Size of the message to transmit
273 * How old is the data?
275 struct GNUNET_TIME_Absolute timestamp;
280 struct GNUNET_MessageHeader *data;
284 * Globally unique tunnel identification (owner + number)
285 * DO NOT USE OVER THE NETWORK
290 * Node that owns the tunnel
295 * Tunnel number to differentiate all the tunnels owned by the node oid
296 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
298 MESH_TunnelNumber tid;
302 struct MeshClient; /* FWD declaration */
305 * Struct containing all information regarding a tunnel
306 * For an intermediate node the improtant info used will be:
307 * - id Tunnel unique identification
308 * - paths[0] To know where to send it next
309 * - metainfo: ready, speeds, accounting
316 struct MESH_TunnelID id;
319 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
321 MESH_TunnelNumber local_tid;
324 * Local tunnel number for local destination clients (incoming number)
325 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
328 MESH_TunnelNumber local_tid_dest;
331 * ID of the last multicast packet seen/sent.
336 * Last time the tunnel was used
338 struct GNUNET_TIME_Absolute timestamp;
341 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
342 * containing peers added by id or by type, not intermediate peers.
344 struct GNUNET_CONTAINER_MultiHashMap *peers;
347 * Number of peers that are connected and potentially ready to receive data
349 unsigned int peers_ready;
352 * Number of peers that have been added to the tunnel
354 unsigned int peers_total;
357 * Client owner of the tunnel, if any
359 struct MeshClient *owner;
362 * Clients that have been informed about the tunnel, if any
364 struct MeshClient **clients;
367 * Number of elements in clients
369 unsigned int nclients;
372 * Clients that have requested to leave the tunnel
374 struct MeshClient **ignore;
377 * Number of elements in clients
379 unsigned int nignore;
382 * Messages ready to transmit
384 struct MeshQueue *queue_head;
385 struct MeshQueue *queue_tail;
390 struct MeshTunnelTree *tree;
393 * Application type we are looking for in this tunnel
395 GNUNET_MESH_ApplicationType type;
398 * Used to search peers offering a service
400 struct GNUNET_DHT_GetHandle *dht_get_type;
403 * Task to keep the used paths alive
405 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
408 * Task to destroy the tunnel after timeout
410 * FIXME: merge the two? a tunnel will have either
411 * a path refresh OR a timeout, never both!
413 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
418 * Info needed to work with tunnel paths and peers
425 struct MeshTunnel *t;
428 * Neighbouring peer to whom we send the packet to
430 struct MeshPeerInfo *peer;
435 struct MeshPeerPath *path;
440 * Struct containing information about a client of the service
447 struct MeshClient *next;
452 struct MeshClient *prev;
455 * Tunnels that belong to this client, indexed by local id
457 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
460 * Tunnels this client has accepted, indexed by incoming local id
462 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
465 * Tunnels this client has rejected, indexed by incoming local id
467 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
469 * Handle to communicate with the client
471 struct GNUNET_SERVER_Client *handle;
474 * Applications that this client has claimed to provide
476 struct GNUNET_CONTAINER_MultiHashMap *apps;
479 * Messages that this client has declared interest in
481 struct GNUNET_CONTAINER_MultiHashMap *types;
484 * Whether the client is active or shutting down (don't send confirmations
485 * to a client that is shutting down.
490 * ID of the client, mainly for debug messages
498 /******************************************************************************/
499 /************************ DEBUG FUNCTIONS ****************************/
500 /******************************************************************************/
504 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
505 * @param cls string to print
506 * @param success GNUNET_OK if the PUT was transmitted,
507 * GNUNET_NO on timeout,
508 * GNUNET_SYSERR on disconnect from service
509 * after the PUT message was transmitted
510 * (so we don't know if it was received or not)
515 mesh_debug (void *cls, int success)
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
525 /******************************************************************************/
526 /*********************** GLOBAL VARIABLES ****************************/
527 /******************************************************************************/
532 static struct MeshClient *clients;
533 static struct MeshClient *clients_tail;
536 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
538 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
541 * Tunnels incoming, indexed by MESH_TunnelNumber
542 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
544 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
547 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
549 static struct GNUNET_CONTAINER_MultiHashMap *peers;
552 * Handle to communicate with core
554 static struct GNUNET_CORE_Handle *core_handle;
557 * Handle to communicate with transport
559 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
564 static struct GNUNET_DHT_Handle *dht_handle;
569 static struct GNUNET_SERVER_Handle *server_handle;
572 * Notification context, to send messages to local clients
574 static struct GNUNET_SERVER_NotificationContext *nc;
577 * Local peer own ID (memory efficient handle)
579 static GNUNET_PEER_Id myid;
582 * Local peer own ID (full value)
584 static struct GNUNET_PeerIdentity my_full_id;
589 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
594 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
597 * Tunnel ID for the next created tunnel (global tunnel number)
599 static MESH_TunnelNumber next_tid;
602 * Tunnel ID for the next incoming tunnel (local tunnel number)
604 static MESH_TunnelNumber next_local_tid;
607 * All application types provided by this peer
609 static struct GNUNET_CONTAINER_MultiHashMap *applications;
612 * All message types clients of this peer are interested in
614 static struct GNUNET_CONTAINER_MultiHashMap *types;
617 * Task to periodically announce provided applications
619 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
622 * Task to periodically announce itself in the network
624 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
627 * Next ID to assign to a client
629 unsigned int next_client_id;
633 /******************************************************************************/
634 /************************ ITERATORS ****************************/
635 /******************************************************************************/
637 /* FIXME move iterators here */
640 /******************************************************************************/
641 /************************ PERIODIC FUNCTIONS ****************************/
642 /******************************************************************************/
645 * Announce iterator over for each application provided by the peer
648 * @param key current key code
649 * @param value value in the hash map
650 * @return GNUNET_YES if we should continue to
655 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
657 /* FIXME are hashes in multihash map equal on all aquitectures? */
658 /* FIXME: keep return value of 'put' to possibly cancel!? */
659 GNUNET_DHT_put (dht_handle, key, 10,
660 GNUNET_DHT_RO_RECORD_ROUTE |
661 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
662 sizeof (struct GNUNET_PeerIdentity),
663 (const char *) &my_full_id,
664 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
666 APP_ANNOUNCE_TIME, NULL, NULL);
672 * Periodically announce what applications are provided by local clients
675 * @param tc task context
678 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
680 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
682 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
686 DEBUG_DHT ("Starting PUT for apps\n");
688 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
690 announce_applications_task =
691 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
693 DEBUG_DHT ("Finished PUT for apps\n");
700 * Periodically announce self id in the DHT
703 * @param tc task context
706 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
708 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
710 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
714 * - Set data expiration in function of X
717 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
719 GNUNET_DHT_put (dht_handle, /* DHT handle */
720 &my_full_id.hashPubKey, /* Key to use */
721 10, /* Replication level */
722 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
723 GNUNET_BLOCK_TYPE_TEST, /* Block type */
724 sizeof (my_full_id), /* Size of the data */
725 (char *) &my_full_id, /* Data itself */
726 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
727 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
728 NULL, /* Continuation */
729 NULL); /* Continuation closure */
731 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
736 * Function to process paths received for a new peer addition. The recorded
737 * paths form the initial tunnel, which can be optimized later.
738 * Called on each result obtained for the DHT search.
741 * @param exp when will this value expire
742 * @param key key of the result
743 * @param type type of the result
744 * @param size number of bytes in data
745 * @param data pointer to the result data
748 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
749 const GNUNET_HashCode * key,
750 const struct GNUNET_PeerIdentity *get_path,
751 unsigned int get_path_length,
752 const struct GNUNET_PeerIdentity *put_path,
753 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
754 size_t size, const void *data);
757 /******************************************************************************/
758 /****************** GENERAL HELPER FUNCTIONS ************************/
759 /******************************************************************************/
762 * Search for a tunnel by global ID using full PeerIdentities
764 * @param oid owner of the tunnel
765 * @param tid global tunnel number
767 * @return tunnel handler, NULL if doesn't exist
769 static struct MeshTunnel *
770 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
774 * Delete an active client from the tunnel.
780 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
783 * Notify a tunnel that a connection has broken that affects at least
786 * @param t Tunnel affected.
787 * @param p1 Peer that got disconnected from p2.
788 * @param p2 Peer that got disconnected from p1.
790 * @return Short ID of the peer disconnected (either p1 or p2).
791 * 0 if the tunnel remained unaffected.
793 static GNUNET_PEER_Id
794 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
799 * Check if client has registered with the service and has not disconnected
801 * @param client the client to check
803 * @return non-NULL if client exists in the global DLL
805 static struct MeshClient *
806 client_get (struct GNUNET_SERVER_Client *client)
808 struct MeshClient *c;
813 if (c->handle == client)
822 * Checks if a given client has subscribed to certain message type
824 * @param message_type Type of message to check
825 * @param c Client to check
827 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
832 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
836 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
837 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
842 * Allow a client to send more data after transmitting a multicast message
843 * which some neighbor has not yet accepted altough a reasonable time has
846 * @param cls Closure (DataDescriptor containing the task identifier)
847 * @param tc Task Context
849 * FIXME reference counter cshould be just int
852 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
854 struct MeshData *mdata = cls;
856 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
858 GNUNET_assert (NULL != mdata->reference_counter);
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
861 *(mdata->reference_counter));
862 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
863 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
868 * Check whether client wants traffic from a tunnel.
870 * @param c Client to check.
871 * @param t Tunnel to be found.
873 * @return GNUNET_YES if client knows tunnel.
875 * TODO look in client hashmap
878 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
882 for (i = 0; i < t->nclients; i++)
883 if (t->clients[i] == c)
890 * Check whether client has been informed about a tunnel.
892 * @param c Client to check.
893 * @param t Tunnel to be found.
895 * @return GNUNET_YES if client knows tunnel.
897 * TODO look in client hashmap
900 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
904 for (i = 0; i < t->nignore; i++)
905 if (t->ignore[i] == c)
907 return client_wants_tunnel(c, t);
912 * Marks a client as uninterested in traffic from the tunnel, updating both
913 * client and tunnel to reflect this.
915 * @param c Client that doesn't want traffic anymore.
916 * @param t Tunnel which should be ignored.
918 * FIXME when to delete an incoming tunnel?
921 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
923 GNUNET_HashCode hash;
925 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
926 GNUNET_break (GNUNET_YES ==
927 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
929 GNUNET_break (GNUNET_YES ==
930 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
931 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
932 tunnel_delete_active_client (t, c);
933 GNUNET_array_append (t->ignore, t->nignore, c);
938 * Deletes a tunnel from a client (either owner or destination). To be used on
939 * tunnel destroy, otherwise, use client_ignore_tunnel.
941 * @param c Client whose tunnel to delete.
942 * @param t Tunnel which should be deleted.
945 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
947 GNUNET_HashCode hash;
951 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
952 GNUNET_assert (GNUNET_YES ==
953 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
959 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
961 GNUNET_assert (GNUNET_YES ==
962 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
966 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
975 * Send the message to all clients that have subscribed to its type
977 * @param msg Pointer to the message itself
978 * @param payload Pointer to the payload of the message.
979 * @return number of clients this message was sent to
982 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
983 const struct GNUNET_MessageHeader *payload)
985 struct GNUNET_PeerIdentity *oid;
986 struct MeshClient *c;
987 struct MeshTunnel *t;
988 MESH_TunnelNumber *tid;
991 char cbuf[htons (msg->size)];
993 type = ntohs (payload->type);
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
997 memcpy (cbuf, msg, sizeof (cbuf));
998 switch (htons (msg->type))
1000 struct GNUNET_MESH_Unicast *uc;
1001 struct GNUNET_MESH_Multicast *mc;
1002 struct GNUNET_MESH_ToOrigin *to;
1004 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1005 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1009 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1010 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1014 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1015 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1023 t = tunnel_get (oid, ntohl (*tid));
1030 for (count = 0, c = clients; c != NULL; c = c->next)
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1033 if (client_is_subscribed (type, c))
1035 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1039 *tid = htonl (t->local_tid);
1043 if (GNUNET_NO == client_knows_tunnel (c, t))
1045 /* This client doesn't know the tunnel */
1046 struct GNUNET_MESH_TunnelNotification tmsg;
1047 GNUNET_HashCode hash;
1049 tmsg.header.size = htons (sizeof (tmsg));
1050 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1051 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1052 tmsg.tunnel_id = htonl (t->local_tid_dest);
1053 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1054 &tmsg.header, GNUNET_NO);
1055 GNUNET_array_append (t->clients, t->nclients, c);
1056 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1058 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1059 c->incoming_tunnels, &hash, t,
1060 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1062 *tid = htonl (t->local_tid_dest);
1065 /* Check if the client wants to get traffic from the tunnel */
1066 if (GNUNET_NO == client_wants_tunnel(c, t))
1069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1070 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1071 (struct GNUNET_MessageHeader
1072 *) cbuf, GNUNET_YES);
1080 * Notify the client that owns the tunnel that a peer has connected to it
1081 * (the requested path to it has been confirmed).
1083 * @param t Tunnel whose owner to notify
1084 * @param id Short id of the peer that has connected
1087 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1089 struct GNUNET_MESH_PeerControl pc;
1091 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1092 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1093 pc.tunnel_id = htonl (t->local_tid);
1094 GNUNET_PEER_resolve (id, &pc.peer);
1095 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1101 * Notify all clients (not depending on registration status) that the incoming
1102 * tunnel is no longer valid.
1104 * @param t Tunnel that was destroyed.
1107 send_clients_tunnel_destroy (struct MeshTunnel *t)
1109 struct GNUNET_MESH_TunnelMessage msg;
1111 msg.header.size = htons (sizeof (msg));
1112 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1113 msg.tunnel_id = htonl (t->local_tid_dest);
1114 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1119 * Notify clients of tunnel disconnections, if needed.
1120 * In case the origin disconnects, the destination clients get a tunnel destroy
1121 * notification. If the last destination disconnects (only one remaining client
1122 * in tunnel), the origin gets a (local ID) peer disconnected.
1123 * Note that the function must be called BEFORE removing the client from
1126 * @param t Tunnel that was destroyed.
1127 * @param c Client that disconnected.
1130 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1136 struct GNUNET_MESH_TunnelMessage msg;
1138 msg.header.size = htons (sizeof (msg));
1139 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1140 msg.tunnel_id = htonl (t->local_tid_dest);
1141 for (i = 0; i < t->nclients; i++)
1142 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1143 &msg.header, GNUNET_NO);
1145 // FIXME when to disconnect an incoming tunnel?
1146 else if (1 == t->nclients && NULL != t->owner)
1148 struct GNUNET_MESH_PeerControl msg;
1150 msg.header.size = htons (sizeof (msg));
1151 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1152 msg.tunnel_id = htonl (t->local_tid);
1153 msg.peer = my_full_id;
1154 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1155 &msg.header, GNUNET_NO);
1161 * Decrements the reference counter and frees all resources if needed
1163 * @param mesh_data Data Descriptor used in a multicast message.
1164 * Freed no longer needed (last message).
1167 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
1169 /* Make sure it's a multicast packet */
1170 GNUNET_assert (NULL != mesh_data->reference_counter);
1172 if (0 == --(*(mesh_data->reference_counter)))
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1175 if (NULL != mesh_data->task)
1177 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1179 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1181 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1183 GNUNET_free (mesh_data->task);
1185 GNUNET_free (mesh_data->reference_counter);
1186 GNUNET_free (mesh_data->data);
1187 GNUNET_free (mesh_data);
1193 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1194 * and insert it in the appropiate structures if the peer is not known yet.
1196 * @param peer Full identity of the peer.
1198 * @return Existing or newly created peer info.
1200 static struct MeshPeerInfo *
1201 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1203 struct MeshPeerInfo *peer_info;
1205 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1206 if (NULL == peer_info)
1209 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1210 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1211 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1212 peer_info->id = GNUNET_PEER_intern (peer);
1220 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1221 * and insert it in the appropiate structures if the peer is not known yet.
1223 * @param peer Short identity of the peer.
1225 * @return Existing or newly created peer info.
1227 static struct MeshPeerInfo *
1228 peer_info_get_short (const GNUNET_PEER_Id peer)
1230 struct GNUNET_PeerIdentity id;
1232 GNUNET_PEER_resolve (peer, &id);
1233 return peer_info_get (&id);
1238 * Iterator to remove the tunnel from the list of tunnels a peer participates
1241 * @param cls Closure (tunnel info)
1242 * @param key GNUNET_PeerIdentity of the peer (unused)
1243 * @param value PeerInfo of the peer
1245 * @return always GNUNET_YES, to keep iterating
1248 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1250 struct MeshTunnel *t = cls;
1251 struct MeshPeerInfo *peer = value;
1254 for (i = 0; i < peer->ntunnels; i++)
1257 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1260 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1261 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1270 * Queue and pass message to core when possible.
1272 * @param cls Closure (type dependant).
1273 * @param type Type of the message.
1274 * @param size Size of the message.
1275 * @param dst Neighbor to send message to.
1278 queue_add (void *cls, uint16_t type, size_t size, struct MeshPeerInfo *dst);
1281 * Core callback to write a pre-constructed data packet to core buffer
1283 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1284 * @param size Number of bytes available in buf.
1285 * @param buf Where the to write the message.
1287 * @return number of bytes written to buf
1290 send_core_data_raw (void *cls, size_t size, void *buf)
1292 struct MeshTransmissionDescriptor *info = cls;
1293 struct GNUNET_MessageHeader *msg;
1296 GNUNET_assert (NULL != info);
1297 GNUNET_assert (NULL != info->mesh_data);
1298 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1299 total_size = ntohs (msg->size);
1301 if (total_size > size)
1306 memcpy (buf, msg, total_size);
1307 GNUNET_free (info->mesh_data);
1314 * Sends an already built message to a peer, properly registrating
1315 * all used resources.
1317 * @param message Message to send. Fucntion makes a copy of it.
1318 * @param peer Short ID of the neighbor whom to send the message.
1323 send_message (const struct GNUNET_MessageHeader *message,
1324 const struct GNUNET_PeerIdentity *peer)
1326 struct MeshTransmissionDescriptor *info;
1327 struct MeshPeerInfo *neighbor;
1328 struct MeshPeerPath *p;
1331 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1333 size = ntohs (message->size);
1334 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1335 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1336 info->mesh_data->data = GNUNET_malloc (size);
1337 memcpy (info->mesh_data->data, message, size);
1338 info->mesh_data->data_len = size;
1339 neighbor = peer_info_get (peer);
1340 for (p = neighbor->path_head; NULL != p; p = p->next)
1350 GNUNET_free (info->mesh_data->data);
1351 GNUNET_free (info->mesh_data);
1355 info->peer = neighbor;
1357 GNUNET_MESSAGE_TYPE_MESH_UNICAST,
1364 * Sends a CREATE PATH message for a path to a peer, properly registrating
1365 * all used resources.
1367 * @param peer PeerInfo of the final peer for whom this path is being created.
1368 * @param p Path itself.
1369 * @param t Tunnel for which the path is created.
1372 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1373 struct MeshTunnel *t)
1375 struct GNUNET_PeerIdentity id;
1376 struct MeshPathInfo *path_info;
1377 struct MeshPeerInfo *neighbor;
1383 p = tree_get_path_to_peer (t->tree, peer->id);
1390 for (i = 0; i < p->length; i++)
1392 if (p->peers[i] == myid)
1395 if (i >= p->length - 1)
1401 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1403 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1404 path_info->path = p;
1406 neighbor = peer_info_get (&id);
1407 path_info->peer = neighbor;
1408 queue_add (path_info,
1409 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1410 sizeof (struct GNUNET_MESH_ManipulatePath) +
1411 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1417 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1419 * @param t Tunnel whose path to destroy.
1420 * @param destination Short ID of the peer to whom the path to destroy.
1423 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1425 struct MeshPeerPath *p;
1428 p = tree_get_path_to_peer (t->tree, destination);
1434 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1435 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1437 struct GNUNET_MESH_ManipulatePath *msg;
1438 struct GNUNET_PeerIdentity *pi;
1442 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1443 msg->header.size = htons (size);
1444 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1445 msg->tid = htonl (t->id.tid);
1446 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1447 for (i = 0; i < p->length; i++)
1449 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1451 send_message (&msg->header, tree_get_first_hop (t->tree, destination));
1458 * Try to establish a new connection to this peer.
1459 * Use the best path for the given tunnel.
1460 * If the peer doesn't have any path to it yet, try to get one.
1461 * If the peer already has some path, send a CREATE PATH towards it.
1463 * @param peer PeerInfo of the peer.
1464 * @param t Tunnel for which to create the path, if possible.
1467 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1469 struct MeshPeerPath *p;
1470 struct MeshPathInfo *path_info;
1472 if (NULL != peer->path_head)
1474 p = tree_get_path_to_peer (t->tree, peer->id);
1481 // FIXME always send create path to self
1484 send_create_path (peer, p, t);
1488 GNUNET_HashCode hash;
1491 send_client_peer_connected (t, myid);
1492 t->local_tid_dest = next_local_tid++;
1493 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1496 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1497 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1504 else if (NULL == peer->dhtget)
1506 struct GNUNET_PeerIdentity id;
1508 GNUNET_PEER_resolve (peer->id, &id);
1509 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1510 path_info->peer = peer;
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1513 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1514 peer->dhtgetcls = path_info;
1515 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1516 GNUNET_BLOCK_TYPE_TEST, /* type */
1517 &id.hashPubKey, /* key to search */
1518 10, /* replication level */
1519 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1520 0, /* xquery bits */
1521 &dht_get_id_handler, path_info);
1523 /* Otherwise, there is no path but the DHT get is already started. */
1528 * Task to delay the connection of a peer
1530 * @param cls Closure (path info with tunnel and peer to connect).
1531 * Will be free'd on exection.
1532 * @param tc TaskContext
1535 peer_info_connect_task (void *cls,
1536 const struct GNUNET_SCHEDULER_TaskContext *tc)
1538 struct MeshPathInfo *path_info = cls;
1540 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1545 peer_info_connect (path_info->peer, path_info->t);
1551 * Destroy the peer_info and free any allocated resources linked to it
1553 * @param pi The peer_info to destroy.
1555 * @return GNUNET_OK on success
1558 peer_info_destroy (struct MeshPeerInfo *pi)
1560 struct GNUNET_PeerIdentity id;
1561 struct MeshPeerPath *p;
1562 struct MeshPeerPath *nextp;
1564 GNUNET_PEER_resolve (pi->id, &id);
1565 GNUNET_PEER_change_rc (pi->id, -1);
1568 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1571 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1572 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1574 if (NULL != pi->dhtget)
1576 GNUNET_DHT_get_stop (pi->dhtget);
1577 GNUNET_free (pi->dhtgetcls);
1583 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1593 * Remove all paths that rely on a direct connection between p1 and p2
1594 * from the peer itself and notify all tunnels about it.
1596 * @param peer PeerInfo of affected peer.
1597 * @param p1 GNUNET_PEER_Id of one peer.
1598 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1601 * TODO: optimize (see below)
1604 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1607 struct MeshPeerPath *p;
1608 struct MeshPeerPath *aux;
1609 struct MeshPeerInfo *peer_d;
1611 unsigned int destroyed;
1617 p = peer->path_head;
1621 for (i = 0; i < (p->length - 1); i++)
1623 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1624 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1626 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1637 for (i = 0; i < peer->ntunnels; i++)
1639 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1643 * Problem: one or more peers have been deleted from the tunnel tree.
1644 * We don't know who they are to try to add them again.
1645 * We need to try to find a new path for each of the disconnected peers.
1646 * Some of them might already have a path to reach them that does not
1647 * involve p1 and p2. Adding all anew might render in a better tree than
1648 * the trivial immediate fix.
1650 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1651 * its children will be reachable trough him.
1653 peer_d = peer_info_get_short (d);
1656 for (p = peer_d->path_head; NULL != p; p = p->next)
1658 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1666 /* No callback, as peer will be already disconnected and a connection
1667 * scheduled by tunnel_notify_connection_broken.
1669 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1673 peer_info_connect (peer_d, peer->tunnels[i]);
1680 * Add the path to the peer and update the path used to reach it in case this
1683 * @param peer_info Destination peer to add the path to.
1684 * @param path New path to add. Last peer must be the peer in arg 1.
1685 * Path will be either used of freed if already known.
1686 * @param trusted Do we trust that this path is real?
1689 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1692 struct MeshPeerPath *aux;
1696 if ((NULL == peer_info) || (NULL == path))
1699 path_destroy (path);
1702 if (path->peers[path->length - 1] != peer_info->id)
1705 path_destroy (path);
1708 if (path->length <= 2 && GNUNET_NO == trusted)
1710 /* Only allow CORE to tell us about direct paths */
1711 path_destroy (path);
1714 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1715 for (l = 1; l < path->length; l++)
1717 if (path->peers[l] == myid)
1719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1720 for (l2 = 0; l2 < path->length - l; l2++)
1722 path->peers[l2] = path->peers[l + l2];
1727 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1732 struct GNUNET_PeerIdentity id;
1734 GNUNET_PEER_resolve (peer_info->id, &id);
1735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1736 path->length, GNUNET_i2s (&id));
1739 l = path_get_length (path);
1746 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1747 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1749 l2 = path_get_length (aux);
1752 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1753 peer_info->path_tail, aux, path);
1758 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1760 path_destroy (path);
1765 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1772 * Add the path to the origin peer and update the path used to reach it in case
1773 * this is the shortest.
1774 * The path is given in peer_info -> destination, therefore we turn the path
1775 * upside down first.
1777 * @param peer_info Peer to add the path to, being the origin of the path.
1778 * @param path New path to add after being inversed.
1779 * @param trusted Do we trust that this path is real?
1782 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1783 struct MeshPeerPath *path, int trusted)
1786 peer_info_add_path (peer_info, path, trusted);
1791 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1792 * to obtain a local peer -> destination path and interning the peer ids.
1794 * @return Newly allocated and created path
1796 static struct MeshPeerPath *
1797 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1798 unsigned int get_path_length,
1799 const struct GNUNET_PeerIdentity *put_path,
1800 unsigned int put_path_length)
1802 struct MeshPeerPath *p;
1808 GNUNET_PEER_change_rc (myid, 1);
1809 i = get_path_length;
1810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1811 for (i--; i >= 0; i--)
1813 id = GNUNET_PEER_intern (&get_path[i]);
1814 if (p->length > 0 && id == p->peers[p->length - 1])
1816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1817 GNUNET_PEER_change_rc (id, -1);
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1822 GNUNET_i2s (&get_path[i]));
1824 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1825 p->peers[p->length - 1] = id;
1828 i = put_path_length;
1829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1830 for (i--; i >= 0; i--)
1832 id = GNUNET_PEER_intern (&put_path[i]);
1835 /* PUT path went through us, so discard the path up until now and start
1836 * from here to get a much shorter (and loop-free) path.
1841 if (p->length > 0 && id == p->peers[p->length - 1])
1843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1844 GNUNET_PEER_change_rc (id, -1);
1848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1849 GNUNET_i2s (&put_path[i]));
1851 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1852 p->peers[p->length - 1] = id;
1856 if (get_path_length > 0)
1857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1858 GNUNET_i2s (&get_path[0]));
1859 if (put_path_length > 0)
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1861 GNUNET_i2s (&put_path[0]));
1862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1864 for (i = 0; i < p->length; i++)
1866 struct GNUNET_PeerIdentity peer_id;
1868 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1870 GNUNET_i2s (&peer_id));
1878 * Adds a path to the peer_infos of all the peers in the path
1880 * @param p Path to process.
1881 * @param confirmed Whether we know if the path works or not. FIXME use
1884 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1888 /* TODO: invert and add */
1889 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1890 for (i++; i < p->length; i++)
1892 struct MeshPeerInfo *aux;
1893 struct MeshPeerPath *copy;
1895 aux = peer_info_get_short (p->peers[i]);
1896 copy = path_duplicate (p);
1897 copy->length = i + 1;
1898 peer_info_add_path (aux, copy, GNUNET_NO);
1904 * Send keepalive packets for a peer
1906 * @param cls Closure (tunnel for which to send the keepalive).
1907 * @param tc Notification context.
1909 * TODO: implement explicit multicast keepalive?
1912 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1916 * Search for a tunnel among the incoming tunnels
1918 * @param tid the local id of the tunnel
1920 * @return tunnel handler, NULL if doesn't exist
1922 static struct MeshTunnel *
1923 tunnel_get_incoming (MESH_TunnelNumber tid)
1925 GNUNET_HashCode hash;
1927 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1928 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1929 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1934 * Search for a tunnel among the tunnels for a client
1936 * @param c the client whose tunnels to search in
1937 * @param tid the local id of the tunnel
1939 * @return tunnel handler, NULL if doesn't exist
1941 static struct MeshTunnel *
1942 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1944 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1946 return tunnel_get_incoming (tid);
1950 GNUNET_HashCode hash;
1952 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1953 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1959 * Search for a tunnel by global ID using PEER_ID
1961 * @param pi owner of the tunnel
1962 * @param tid global tunnel number
1964 * @return tunnel handler, NULL if doesn't exist
1966 static struct MeshTunnel *
1967 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1969 struct MESH_TunnelID id;
1970 GNUNET_HashCode hash;
1975 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1976 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1981 * Search for a tunnel by global ID using full PeerIdentities
1983 * @param oid owner of the tunnel
1984 * @param tid global tunnel number
1986 * @return tunnel handler, NULL if doesn't exist
1988 static struct MeshTunnel *
1989 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1991 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1996 * Delete an active client from the tunnel.
2002 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2006 for (i = 0; i < t->nclients; i++)
2008 if (t->clients[i] == c)
2010 t->clients[i] = t->clients[t->nclients - 1];
2011 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2019 * Delete an ignored client from the tunnel.
2025 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2029 for (i = 0; i < t->nignore; i++)
2031 if (t->ignore[i] == c)
2033 t->ignore[i] = t->ignore[t->nignore - 1];
2034 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2042 * Delete a client from the tunnel. It should be only done on
2043 * client disconnection, otherwise use client_ignore_tunnel.
2049 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2051 tunnel_delete_ignored_client (t, c);
2052 tunnel_delete_active_client (t, c);
2057 * Callback used to notify a client owner of a tunnel that a peer has
2058 * disconnected, most likely because of a path change.
2060 * @param cls Closure (tunnel this notification is about).
2061 * @param peer_id Short ID of disconnected peer.
2064 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2066 struct MeshTunnel *t = cls;
2067 struct MeshPeerInfo *peer;
2068 struct MeshPathInfo *path_info;
2070 if (NULL != t->owner && NULL != nc)
2072 struct GNUNET_MESH_PeerControl msg;
2074 msg.header.size = htons (sizeof (msg));
2075 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2076 msg.tunnel_id = htonl (t->local_tid);
2077 GNUNET_PEER_resolve (peer_id, &msg.peer);
2078 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2079 &msg.header, GNUNET_NO);
2081 peer = peer_info_get_short (peer_id);
2082 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2083 path_info->peer = peer;
2085 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2090 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2091 * needed rescources.
2092 * If peer already exists, reevaluate shortest path and change if different.
2094 * @param t Tunnel we want to add a new peer to
2095 * @param peer PeerInfo of the peer being added
2099 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2101 struct GNUNET_PeerIdentity id;
2102 struct MeshPeerPath *best_p;
2103 struct MeshPeerPath *p;
2104 unsigned int best_cost;
2107 GNUNET_PEER_resolve (peer->id, &id);
2109 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2112 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2113 GNUNET_assert (GNUNET_OK ==
2114 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2116 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2119 if (NULL != (p = peer->path_head))
2122 best_cost = tree_get_path_cost (t->tree, p);
2125 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2132 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2133 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2134 t->path_refresh_task =
2135 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2139 /* Start a DHT get */
2140 peer_info_connect (peer, t);
2145 * Add a path to a tunnel which we don't own, just to remember the next hop.
2146 * If destination node was already in the tunnel, the first hop information
2147 * will be replaced with the new path.
2149 * @param t Tunnel we want to add a new peer to
2150 * @param p Path to add
2151 * @param own_pos Position of local node in path.
2155 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2156 unsigned int own_pos)
2158 struct GNUNET_PeerIdentity id;
2160 GNUNET_assert (0 != own_pos);
2161 tree_add_path (t->tree, p, NULL, NULL);
2162 if (own_pos < p->length - 1)
2164 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2165 tree_update_first_hops (t->tree, myid, &id);
2171 * Notifies a tunnel that a connection has broken that affects at least
2172 * some of its peers. Sends a notification towards the root of the tree.
2173 * In case the peer is the owner of the tree, notifies the client that owns
2174 * the tunnel and tries to reconnect.
2176 * @param t Tunnel affected.
2177 * @param p1 Peer that got disconnected from p2.
2178 * @param p2 Peer that got disconnected from p1.
2180 * @return Short ID of the peer disconnected (either p1 or p2).
2181 * 0 if the tunnel remained unaffected.
2183 static GNUNET_PEER_Id
2184 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2190 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2192 if (myid != p1 && myid != p2)
2198 if (tree_get_predecessor (t->tree) != 0)
2200 /* We are the peer still connected, notify owner of the disconnection. */
2201 struct GNUNET_MESH_PathBroken msg;
2202 struct GNUNET_PeerIdentity neighbor;
2204 msg.header.size = htons (sizeof (msg));
2205 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2206 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2207 msg.tid = htonl (t->id.tid);
2208 msg.peer1 = my_full_id;
2209 GNUNET_PEER_resolve (pid, &msg.peer2);
2210 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2211 send_message (&msg.header, &neighbor);
2219 * Send a multicast packet to a neighbor.
2221 * @param cls Closure (Info about the multicast packet)
2222 * @param neighbor_id Short ID of the neighbor to send the packet to.
2225 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2227 struct MeshData *mdata = cls;
2228 struct MeshTransmissionDescriptor *info;
2229 struct GNUNET_PeerIdentity neighbor;
2231 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2233 info->mesh_data = mdata;
2234 (*(mdata->reference_counter)) ++;
2235 info->destination = neighbor_id;
2236 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2238 GNUNET_i2s (&neighbor));
2239 info->peer = peer_info_get (&neighbor);
2240 GNUNET_assert (NULL != info->peer);
2242 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
2243 info->mesh_data->data_len,
2249 * Send a message in a tunnel in multicast, sending a copy to each child node
2250 * down the local one in the tunnel tree.
2252 * @param t Tunnel in which to send the data.
2253 * @param msg Message to be sent.
2254 * @param internal Has the service generated this message?
2257 tunnel_send_multicast (struct MeshTunnel *t,
2258 const struct GNUNET_MessageHeader *msg,
2261 struct MeshData *mdata;
2263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2264 " sending a multicast packet...\n");
2265 mdata = GNUNET_malloc (sizeof (struct MeshData));
2266 mdata->data_len = ntohs (msg->size);
2267 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2269 mdata->data = GNUNET_malloc (mdata->data_len);
2270 memcpy (mdata->data, msg, mdata->data_len);
2271 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2273 struct GNUNET_MESH_Multicast *mcast;
2275 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2276 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2278 ntohl (mcast->ttl));
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2284 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2285 && GNUNET_NO == internal)
2287 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2289 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2295 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2296 if (*(mdata->reference_counter) == 0)
2298 GNUNET_free (mdata->data);
2299 GNUNET_free (mdata->reference_counter);
2300 if (NULL != mdata->task)
2302 GNUNET_SCHEDULER_cancel(*(mdata->task));
2303 GNUNET_free (mdata->task);
2304 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2306 // FIXME change order?
2307 GNUNET_free (mdata);
2309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2310 " sending a multicast packet done\n");
2316 * Send a message to all peers in this tunnel that the tunnel is no longer
2319 * @param t The tunnel whose peers to notify.
2322 tunnel_send_destroy (struct MeshTunnel *t)
2324 struct GNUNET_MESH_TunnelDestroy msg;
2326 msg.header.size = htons (sizeof (msg));
2327 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2328 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2329 msg.tid = htonl (t->id.tid);
2330 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2336 * Destroy the tunnel and free any allocated resources linked to it.
2338 * @param t the tunnel to destroy
2340 * @return GNUNET_OK on success
2343 tunnel_destroy (struct MeshTunnel *t)
2345 struct MeshClient *c;
2346 struct MeshQueue *q;
2347 struct MeshQueue *qn;
2348 GNUNET_HashCode hash;
2359 struct GNUNET_PeerIdentity id;
2361 GNUNET_PEER_resolve (t->id.oid, &id);
2362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2363 GNUNET_i2s (&id), t->id.tid);
2365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2369 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2370 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2375 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2378 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2382 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2383 for (i = 0; i < t->nclients; i++)
2387 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2392 for (i = 0; i < t->nignore; i++)
2396 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2401 if (t->nclients > 0)
2404 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2408 GNUNET_free (t->clients);
2410 if (NULL != t->peers)
2412 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2414 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2419 if (NULL != q->data)
2420 GNUNET_free (q->data);
2424 /* TODO cancel core transmit ready in case it was active */
2426 tree_destroy (t->tree);
2427 if (NULL != t->dht_get_type)
2428 GNUNET_DHT_get_stop (t->dht_get_type);
2429 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2430 GNUNET_SCHEDULER_cancel (t->timeout_task);
2431 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2432 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2439 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2440 * that are no longer needed, as well as nodes of no longer reachable peers.
2441 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2443 * @param t Tunnel from which to remove the path.
2444 * @param peer Short id of the peer which should be removed.
2447 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2449 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2455 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2456 * client when the client disconnects. If the client is not the owner, the
2457 * owner will get notified if no more clients are in the tunnel and the client
2458 * get removed from the tunnel's list.
2460 * @param cls closure (client that is disconnecting)
2461 * @param key the hash of the local tunnel id (used to access the hashmap)
2462 * @param value the value stored at the key (tunnel to destroy)
2464 * @return GNUNET_OK on success
2467 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2469 struct MeshTunnel *t = value;
2470 struct MeshClient *c = cls;
2473 send_client_tunnel_disconnect(t, c);
2476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2477 "Client %u is destination, keeping the tunnel alive.\n", c->id);
2478 tunnel_delete_client(t, c);
2479 client_delete_tunnel(c, t);
2482 tunnel_send_destroy(t);
2483 r = tunnel_destroy (t);
2489 * Timeout function, destroys tunnel if called
2491 * @param cls Closure (tunnel to destroy).
2492 * @param tc TaskContext
2495 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2497 struct MeshTunnel *t = cls;
2499 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2501 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2506 * Resets the tunnel timeout. Starts it if no timeout was running.
2508 * @param t Tunnel whose timeout to reset.
2511 tunnel_reset_timeout (struct MeshTunnel *t)
2513 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2514 GNUNET_SCHEDULER_cancel (t->timeout_task);
2516 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2517 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2521 /******************************************************************************/
2522 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2523 /******************************************************************************/
2526 * Function to send a create path packet to a peer.
2528 * @param cls closure
2529 * @param size number of bytes available in buf
2530 * @param buf where the callee should write the message
2531 * @return number of bytes written to buf
2534 send_core_path_create (void *cls, size_t size, void *buf)
2536 struct MeshPathInfo *info = cls;
2537 struct GNUNET_MESH_ManipulatePath *msg;
2538 struct GNUNET_PeerIdentity *peer_ptr;
2539 struct MeshTunnel *t = info->t;
2540 struct MeshPeerPath *p = info->path;
2544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2546 sizeof (struct GNUNET_MESH_ManipulatePath) +
2547 p->length * sizeof (struct GNUNET_PeerIdentity);
2549 if (size < size_needed || NULL == buf)
2554 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2555 msg->header.size = htons (size_needed);
2556 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2557 msg->tid = ntohl (t->id.tid);
2559 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2560 for (i = 0; i < p->length; i++)
2562 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2569 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2575 * Fill the core buffer
2577 * @param cls closure (data itself)
2578 * @param size number of bytes available in buf
2579 * @param buf where the callee should write the message
2581 * @return number of bytes written to buf
2584 send_core_data_multicast (void *cls, size_t size, void *buf)
2586 struct MeshTransmissionDescriptor *info = cls;
2589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
2590 GNUNET_assert (NULL != info);
2591 GNUNET_assert (NULL != info->peer);
2592 total_size = info->mesh_data->data_len;
2593 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2595 if (total_size > size)
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
2601 memcpy (buf, info->mesh_data->data, total_size);
2604 struct GNUNET_MESH_Multicast *mc;
2605 struct GNUNET_MessageHeader *mh;
2608 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2610 mc = (struct GNUNET_MESH_Multicast *) mh;
2611 mh = (struct GNUNET_MessageHeader *) &mc[1];
2612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2613 " multicast, payload type %u\n", ntohs (mh->type));
2614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2615 " multicast, payload size %u\n", ntohs (mh->size));
2619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
2624 data_descriptor_decrement_multicast (info->mesh_data);
2625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
2633 * Creates a path ack message in buf and frees all unused resources.
2635 * @param cls closure (MeshTransmissionDescriptor)
2636 * @param size number of bytes available in buf
2637 * @param buf where the callee should write the message
2638 * @return number of bytes written to buf
2641 send_core_path_ack (void *cls, size_t size, void *buf)
2643 struct MeshTransmissionDescriptor *info = cls;
2644 struct GNUNET_MESH_PathACK *msg = buf;
2646 GNUNET_assert (NULL != info);
2647 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2652 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2653 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2654 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2655 msg->tid = htonl (info->origin->tid);
2656 msg->peer_id = my_full_id;
2659 /* TODO add signature */
2661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2662 return sizeof (struct GNUNET_MESH_PathACK);
2667 * Free a transmission that was already queued with all resources
2668 * associated to the request.
2670 * @param queue Queue handler to cancel.
2671 * @param clear_cls Is it necessary to free associated cls?
2674 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2676 struct MeshTransmissionDescriptor *dd;
2677 struct MeshPathInfo *path_info;
2678 struct MeshPeerInfo *peer;
2681 if (GNUNET_YES == clear_cls)
2683 switch (queue->type)
2685 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2686 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2687 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
2690 data_descriptor_decrement_multicast (dd->mesh_data);
2692 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2694 path_info = queue->cls;
2695 path_destroy (path_info->path);
2699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
2701 GNUNET_free_non_null (queue->cls);
2703 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
2709 * Core callback to write a queued packet to core buffer
2711 * @param cls Closure (peer info).
2712 * @param size Number of bytes available in buf.
2713 * @param buf Where the to write the message.
2715 * @return number of bytes written to buf
2718 queue_send (void *cls, size_t size, void *buf)
2720 struct MeshPeerInfo *peer = cls;
2721 struct MeshPeerQueue *queue;
2724 peer->core_transmit = NULL;
2725 queue = peer->queue_head;
2727 /* If queue is empty, send should have been cancelled */
2734 /* Check if buffer size is enough for the message */
2735 if (queue->size < size)
2737 struct GNUNET_PeerIdentity id;
2739 GNUNET_PEER_resolve (peer->id, &id);
2740 peer->core_transmit =
2741 GNUNET_CORE_notify_transmit_ready(core_handle,
2744 GNUNET_TIME_UNIT_FOREVER_REL,
2753 switch (queue->type)
2755 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2756 data_size = send_core_data_raw (queue->cls, size, buf);
2758 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2759 data_size = send_core_data_multicast(queue->cls, size, buf);
2761 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2764 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2765 data_size = send_core_path_create(queue->cls, size, buf);
2767 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2768 data_size = send_core_path_ack(queue->cls, size, buf);
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
2776 /* Free queue, but cls was freed by send_core_* */
2777 queue_destroy(queue, GNUNET_NO);
2779 /* If more data in queue, send next */
2780 if (NULL != peer->queue_head)
2782 struct GNUNET_PeerIdentity id;
2784 GNUNET_PEER_resolve (peer->id, &id);
2785 peer->core_transmit =
2786 GNUNET_CORE_notify_transmit_ready(core_handle,
2789 GNUNET_TIME_UNIT_FOREVER_REL,
2791 peer->queue_head->size,
2800 * Queue and pass message to core when possible.
2802 * @param cls Closure (type dependant).
2803 * @param type Type of the message.
2804 * @param size Size of the message.
2805 * @param dst Neighbor to send message to.
2808 queue_add (void *cls, uint16_t type, size_t size, struct MeshPeerInfo *dst)
2810 struct MeshPeerQueue *queue;
2812 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2817 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
2818 if (NULL == dst->core_transmit)
2820 struct GNUNET_PeerIdentity id;
2822 GNUNET_PEER_resolve (dst->id, &id);
2823 dst->core_transmit =
2824 GNUNET_CORE_notify_transmit_ready(core_handle,
2827 GNUNET_TIME_UNIT_FOREVER_REL,
2836 /******************************************************************************/
2837 /******************** MESH NETWORK HANDLERS **************************/
2838 /******************************************************************************/
2842 * Core handler for path creation
2844 * @param cls closure
2845 * @param message message
2846 * @param peer peer identity this notification is about
2847 * @param atsi performance data
2848 * @param atsi_count number of records in 'atsi'
2850 * @return GNUNET_OK to keep the connection open,
2851 * GNUNET_SYSERR to close it (signal serious error)
2854 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2855 const struct GNUNET_MessageHeader *message,
2856 const struct GNUNET_ATS_Information *atsi,
2857 unsigned int atsi_count)
2859 unsigned int own_pos;
2862 MESH_TunnelNumber tid;
2863 struct GNUNET_MESH_ManipulatePath *msg;
2864 struct GNUNET_PeerIdentity *pi;
2865 GNUNET_HashCode hash;
2866 struct MeshPeerPath *path;
2867 struct MeshPeerInfo *dest_peer_info;
2868 struct MeshPeerInfo *orig_peer_info;
2869 struct MeshTunnel *t;
2871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2872 "Received a path create msg [%s]\n",
2873 GNUNET_i2s (&my_full_id));
2874 size = ntohs (message->size);
2875 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2877 GNUNET_break_op (0);
2881 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2882 if (size % sizeof (struct GNUNET_PeerIdentity))
2884 GNUNET_break_op (0);
2887 size /= sizeof (struct GNUNET_PeerIdentity);
2890 GNUNET_break_op (0);
2893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2894 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2896 tid = ntohl (msg->tid);
2897 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2899 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2900 t = tunnel_get (pi, tid);
2903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2904 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2905 t->id.oid = GNUNET_PEER_intern (pi);
2907 while (NULL != tunnel_get_incoming (next_local_tid))
2908 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2909 t->local_tid_dest = next_local_tid++;
2910 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2911 t->tree = tree_new (t->id.oid);
2913 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2915 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2916 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2922 tunnel_reset_timeout (t);
2923 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2925 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2926 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2934 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2935 if (NULL == dest_peer_info)
2937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2938 " Creating PeerInfo for destination.\n");
2939 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2940 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2941 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2943 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2945 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2946 if (NULL == orig_peer_info)
2948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2949 " Creating PeerInfo for origin.\n");
2950 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2951 orig_peer_info->id = GNUNET_PEER_intern (pi);
2952 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2953 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
2956 path = path_new (size);
2958 for (i = 0; i < size; i++)
2960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
2961 GNUNET_i2s (&pi[i]));
2962 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2963 if (path->peers[i] == myid)
2966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
2969 /* cannot be self, must be 'not found' */
2970 /* create path: self not found in path through self */
2971 GNUNET_break_op (0);
2972 path_destroy (path);
2973 /* FIXME error. destroy tunnel? leave for timeout? */
2976 path_add_to_peers (path, GNUNET_NO);
2977 tunnel_add_path (t, path, own_pos);
2978 if (own_pos == size - 1)
2980 /* It is for us! Send ack. */
2981 struct MeshTransmissionDescriptor *info;
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2984 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2985 if (NULL == t->peers)
2987 /* New tunnel! Notify clients on data. */
2988 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2990 GNUNET_break (GNUNET_OK ==
2991 GNUNET_CONTAINER_multihashmap_put (t->peers,
2992 &my_full_id.hashPubKey,
2995 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
2996 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2997 info->origin = &t->id;
2998 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2999 GNUNET_assert (NULL != info->peer);
3001 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3002 sizeof (struct GNUNET_MESH_PathACK),
3007 struct MeshPeerPath *path2;
3009 /* It's for somebody else! Retransmit. */
3010 path2 = path_duplicate (path);
3011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3012 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3013 path2 = path_duplicate (path);
3014 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3015 send_create_path (dest_peer_info, path, t);
3022 * Core handler for path destruction
3024 * @param cls closure
3025 * @param message message
3026 * @param peer peer identity this notification is about
3027 * @param atsi performance data
3028 * @param atsi_count number of records in 'atsi'
3030 * @return GNUNET_OK to keep the connection open,
3031 * GNUNET_SYSERR to close it (signal serious error)
3034 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3035 const struct GNUNET_MessageHeader *message,
3036 const struct GNUNET_ATS_Information *atsi,
3037 unsigned int atsi_count)
3039 struct GNUNET_MESH_ManipulatePath *msg;
3040 struct GNUNET_PeerIdentity *pi;
3041 struct MeshPeerPath *path;
3042 struct MeshTunnel *t;
3043 unsigned int own_pos;
3047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3048 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3049 size = ntohs (message->size);
3050 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3052 GNUNET_break_op (0);
3056 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3057 if (size % sizeof (struct GNUNET_PeerIdentity))
3059 GNUNET_break_op (0);
3062 size /= sizeof (struct GNUNET_PeerIdentity);
3065 GNUNET_break_op (0);
3068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3070 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3071 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3073 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3075 t = tunnel_get (pi, ntohl (msg->tid));
3078 /* TODO notify back: we don't know this tunnel */
3079 GNUNET_break_op (0);
3082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3083 path = path_new (size);
3085 for (i = 0; i < size; i++)
3087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3088 GNUNET_i2s (&pi[i]));
3089 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3090 if (path->peers[i] == myid)
3093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3094 if (own_pos < path->length - 1)
3095 send_message (message, &pi[own_pos + 1]);
3097 send_client_tunnel_disconnect(t, NULL);
3099 tunnel_delete_peer (t, path->peers[path->length - 1]);
3100 path_destroy (path);
3106 * Core handler for notifications of broken paths
3108 * @param cls closure
3109 * @param message message
3110 * @param peer peer identity this notification is about
3111 * @param atsi performance data
3112 * @param atsi_count number of records in 'atsi'
3114 * @return GNUNET_OK to keep the connection open,
3115 * GNUNET_SYSERR to close it (signal serious error)
3118 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3119 const struct GNUNET_MessageHeader *message,
3120 const struct GNUNET_ATS_Information *atsi,
3121 unsigned int atsi_count)
3123 struct GNUNET_MESH_PathBroken *msg;
3124 struct MeshTunnel *t;
3126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3127 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3128 msg = (struct GNUNET_MESH_PathBroken *) message;
3129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3130 GNUNET_i2s (&msg->peer1));
3131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3132 GNUNET_i2s (&msg->peer2));
3133 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3136 GNUNET_break_op (0);
3139 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3140 GNUNET_PEER_search (&msg->peer2));
3147 * Core handler for tunnel destruction
3149 * @param cls closure
3150 * @param message message
3151 * @param peer peer identity this notification is about
3152 * @param atsi performance data
3153 * @param atsi_count number of records in 'atsi'
3155 * @return GNUNET_OK to keep the connection open,
3156 * GNUNET_SYSERR to close it (signal serious error)
3159 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3160 const struct GNUNET_MessageHeader *message,
3161 const struct GNUNET_ATS_Information *atsi,
3162 unsigned int atsi_count)
3164 struct GNUNET_MESH_TunnelDestroy *msg;
3165 struct MeshTunnel *t;
3167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3168 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3169 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3171 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3172 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3175 /* Probably already got the message from another path,
3176 * destroyed the tunnel and retransmitted to children.
3181 if (t->id.oid == myid)
3183 GNUNET_break_op (0);
3186 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3188 /* Tunnel was incoming, notify clients */
3189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3190 t->local_tid, t->local_tid_dest);
3191 send_clients_tunnel_destroy (t);
3193 tunnel_send_destroy (t);
3200 * Core handler for mesh network traffic going from the origin to a peer
3202 * @param cls closure
3203 * @param peer peer identity this notification is about
3204 * @param message message
3205 * @param atsi performance data
3206 * @param atsi_count number of records in 'atsi'
3207 * @return GNUNET_OK to keep the connection open,
3208 * GNUNET_SYSERR to close it (signal serious error)
3211 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3212 const struct GNUNET_MessageHeader *message,
3213 const struct GNUNET_ATS_Information *atsi,
3214 unsigned int atsi_count)
3216 struct GNUNET_MESH_Unicast *msg;
3217 struct MeshTunnel *t;
3221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3223 size = ntohs (message->size);
3225 sizeof (struct GNUNET_MESH_Unicast) +
3226 sizeof (struct GNUNET_MessageHeader))
3231 msg = (struct GNUNET_MESH_Unicast *) message;
3232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3233 ntohs (msg[1].header.type));
3234 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3237 /* TODO notify back: we don't know this tunnel */
3238 GNUNET_break_op (0);
3241 tunnel_reset_timeout (t);
3242 pid = GNUNET_PEER_search (&msg->destination);
3245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3246 " it's for us! sending to clients...\n");
3247 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3251 " not for us, retransmitting...\n");
3252 send_message (message, tree_get_first_hop (t->tree, pid));
3258 * Core handler for mesh network traffic going from the origin to all peers
3260 * @param cls closure
3261 * @param message message
3262 * @param peer peer identity this notification is about
3263 * @param atsi performance data
3264 * @param atsi_count number of records in 'atsi'
3265 * @return GNUNET_OK to keep the connection open,
3266 * GNUNET_SYSERR to close it (signal serious error)
3268 * TODO: Check who we got this from, to validate route.
3271 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3272 const struct GNUNET_MessageHeader *message,
3273 const struct GNUNET_ATS_Information *atsi,
3274 unsigned int atsi_count)
3276 struct GNUNET_MESH_Multicast *msg;
3277 struct MeshTunnel *t;
3280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
3282 size = ntohs (message->size);
3283 if (sizeof (struct GNUNET_MESH_Multicast) +
3284 sizeof (struct GNUNET_MessageHeader) > size)
3286 GNUNET_break_op (0);
3289 msg = (struct GNUNET_MESH_Multicast *) message;
3290 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3294 /* TODO notify that we dont know that tunnel */
3295 GNUNET_break_op (0);
3298 if (t->mid == ntohl (msg->mid))
3300 /* FIXME: already seen this packet, log dropping */
3301 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3302 " Already seen mid %u, DROPPING!\n", t->mid);
3307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3308 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3310 t->mid = ntohl (msg->mid);
3311 tunnel_reset_timeout (t);
3313 /* Transmit to locally interested clients */
3314 if (NULL != t->peers &&
3315 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3317 send_subscribed_clients (message, &msg[1].header);
3319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
3320 if (ntohl (msg->ttl) == 0)
3322 /* FIXME: ttl is 0, log dropping */
3323 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3326 tunnel_send_multicast (t, message, GNUNET_NO);
3332 * Core handler for mesh network traffic toward the owner of a tunnel
3334 * @param cls closure
3335 * @param message message
3336 * @param peer peer identity this notification is about
3337 * @param atsi performance data
3338 * @param atsi_count number of records in 'atsi'
3340 * @return GNUNET_OK to keep the connection open,
3341 * GNUNET_SYSERR to close it (signal serious error)
3344 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3345 const struct GNUNET_MessageHeader *message,
3346 const struct GNUNET_ATS_Information *atsi,
3347 unsigned int atsi_count)
3349 struct GNUNET_MESH_ToOrigin *msg;
3350 struct GNUNET_PeerIdentity id;
3351 struct MeshPeerInfo *peer_info;
3352 struct MeshTunnel *t;
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3357 size = ntohs (message->size);
3358 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3359 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3361 GNUNET_break_op (0);
3364 msg = (struct GNUNET_MESH_ToOrigin *) message;
3365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3366 ntohs (msg[1].header.type));
3367 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3371 /* TODO notify that we dont know this tunnel (whom)? */
3372 GNUNET_break_op (0);
3376 if (t->id.oid == myid)
3379 struct GNUNET_MESH_ToOrigin *copy;
3381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3382 " it's for us! sending to clients...\n");
3383 if (NULL == t->owner)
3385 /* got data packet for ownerless tunnel */
3386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
3387 GNUNET_break_op (0);
3390 /* TODO signature verification */
3391 memcpy (cbuf, message, size);
3392 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3393 copy->tid = htonl (t->local_tid);
3394 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3395 ©->header, GNUNET_YES);
3398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3399 " not for us, retransmitting...\n");
3401 peer_info = peer_info_get (&msg->oid);
3402 if (NULL == peer_info)
3404 /* unknown origin of tunnel */
3408 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3409 send_message (message, &id);
3416 * Core handler for path ACKs
3418 * @param cls closure
3419 * @param message message
3420 * @param peer peer identity this notification is about
3421 * @param atsi performance data
3422 * @param atsi_count number of records in 'atsi'
3424 * @return GNUNET_OK to keep the connection open,
3425 * GNUNET_SYSERR to close it (signal serious error)
3428 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3429 const struct GNUNET_MessageHeader *message,
3430 const struct GNUNET_ATS_Information *atsi,
3431 unsigned int atsi_count)
3433 struct GNUNET_MESH_PathACK *msg;
3434 struct GNUNET_PeerIdentity id;
3435 struct MeshPeerInfo *peer_info;
3436 struct MeshPeerPath *p;
3437 struct MeshTunnel *t;
3439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3440 GNUNET_i2s (&my_full_id));
3441 msg = (struct GNUNET_MESH_PathACK *) message;
3442 t = tunnel_get (&msg->oid, msg->tid);
3445 /* TODO notify that we don't know the tunnel */
3449 peer_info = peer_info_get (&msg->peer_id);
3451 /* Add paths to peers? */
3452 p = tree_get_path_to_peer (t->tree, peer_info->id);
3455 path_add_to_peers (p, GNUNET_YES);
3463 /* Message for us? */
3464 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3467 if (NULL == t->owner)
3469 GNUNET_break_op (0);
3472 if (NULL != t->dht_get_type)
3474 GNUNET_DHT_get_stop (t->dht_get_type);
3475 t->dht_get_type = NULL;
3477 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3479 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3480 send_client_peer_connected (t, peer_info->id);
3485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3486 " not for us, retransmitting...\n");
3487 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3488 peer_info = peer_info_get (&msg->oid);
3489 if (NULL == peer_info)
3491 /* If we know the tunnel, we should DEFINITELY know the peer */
3495 send_message (message, &id);
3501 * Functions to handle messages from core
3503 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3504 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3505 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3506 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3507 sizeof (struct GNUNET_MESH_PathBroken)},
3508 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3509 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3510 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3511 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3512 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3513 sizeof (struct GNUNET_MESH_PathACK)},
3519 /******************************************************************************/
3520 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3521 /******************************************************************************/
3524 * deregister_app: iterator for removing each application registered by a client
3526 * @param cls closure
3527 * @param key the hash of the application id (used to access the hashmap)
3528 * @param value the value stored at the key (client)
3530 * @return GNUNET_OK on success
3533 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3535 GNUNET_break (GNUNET_YES ==
3536 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3543 * notify_client_connection_failure: notify a client that the connection to the
3544 * requested remote peer is not possible (for instance, no route found)
3545 * Function called when the socket is ready to queue more data. "buf" will be
3546 * NULL and "size" zero if the socket was closed for writing in the meantime.
3548 * @param cls closure
3549 * @param size number of bytes available in buf
3550 * @param buf where the callee should write the message
3551 * @return number of bytes written to buf
3554 notify_client_connection_failure (void *cls, size_t size, void *buf)
3557 struct MeshPeerInfo *peer_info;
3558 struct GNUNET_MESH_PeerControl *msg;
3559 struct GNUNET_PeerIdentity id;
3561 if (0 == size && NULL == buf)
3563 // TODO retry? cancel?
3567 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3568 peer_info = (struct MeshPeerInfo *) cls;
3569 msg = (struct GNUNET_MESH_PeerControl *) buf;
3570 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3571 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3572 // msg->tunnel_id = htonl(peer_info->t->tid);
3573 GNUNET_PEER_resolve (peer_info->id, &id);
3574 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3582 * Send keepalive packets for a peer
3584 * @param cls Closure (tunnel for which to send the keepalive).
3585 * @param tc Notification context.
3587 * TODO: implement explicit multicast keepalive?
3590 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3592 struct MeshTunnel *t = cls;
3593 struct GNUNET_MessageHeader *payload;
3594 struct GNUNET_MESH_Multicast *msg;
3596 sizeof (struct GNUNET_MESH_Multicast) +
3597 sizeof (struct GNUNET_MessageHeader);
3600 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3604 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3607 "sending keepalive for tunnel %d\n", t->id.tid);
3609 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3610 msg->header.size = htons (size);
3611 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3612 msg->oid = my_full_id;
3613 msg->tid = htonl (t->id.tid);
3614 msg->ttl = htonl (DEFAULT_TTL);
3615 msg->mid = htonl (t->mid + 1);
3617 payload = (struct GNUNET_MessageHeader *) &msg[1];
3618 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3619 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3620 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
3622 t->path_refresh_task =
3623 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3629 * Function to process paths received for a new peer addition. The recorded
3630 * paths form the initial tunnel, which can be optimized later.
3631 * Called on each result obtained for the DHT search.
3633 * @param cls closure
3634 * @param exp when will this value expire
3635 * @param key key of the result
3636 * @param get_path path of the get request
3637 * @param get_path_length lenght of get_path
3638 * @param put_path path of the put request
3639 * @param put_path_length length of the put_path
3640 * @param type type of the result
3641 * @param size number of bytes in data
3642 * @param data pointer to the result data
3644 * TODO: re-issue the request after certain time? cancel after X results?
3647 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3648 const GNUNET_HashCode * key,
3649 const struct GNUNET_PeerIdentity *get_path,
3650 unsigned int get_path_length,
3651 const struct GNUNET_PeerIdentity *put_path,
3652 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3653 size_t size, const void *data)
3655 struct MeshPathInfo *path_info = cls;
3656 struct MeshPeerPath *p;
3657 struct GNUNET_PeerIdentity pi;
3660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3661 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3664 p = path_build_from_dht (get_path, get_path_length, put_path,
3666 path_add_to_peers (p, GNUNET_NO);
3668 for (i = 0; i < path_info->peer->ntunnels; i++)
3670 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3671 peer_info_connect (path_info->peer, path_info->t);
3679 * Function to process paths received for a new peer addition. The recorded
3680 * paths form the initial tunnel, which can be optimized later.
3681 * Called on each result obtained for the DHT search.
3683 * @param cls closure
3684 * @param exp when will this value expire
3685 * @param key key of the result
3686 * @param get_path path of the get request
3687 * @param get_path_length lenght of get_path
3688 * @param put_path path of the put request
3689 * @param put_path_length length of the put_path
3690 * @param type type of the result
3691 * @param size number of bytes in data
3692 * @param data pointer to the result data
3695 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3696 const GNUNET_HashCode * key,
3697 const struct GNUNET_PeerIdentity *get_path,
3698 unsigned int get_path_length,
3699 const struct GNUNET_PeerIdentity *put_path,
3700 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3701 size_t size, const void *data)
3703 const struct GNUNET_PeerIdentity *pi = data;
3704 struct MeshTunnel *t = cls;
3705 struct MeshPeerInfo *peer_info;
3706 struct MeshPeerPath *p;
3708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
3709 if (size != sizeof (struct GNUNET_PeerIdentity))
3711 GNUNET_break_op (0);
3714 GNUNET_assert (NULL != t->owner);
3715 peer_info = peer_info_get (pi);
3716 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3718 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3720 p = path_build_from_dht (get_path, get_path_length, put_path,
3722 path_add_to_peers (p, GNUNET_NO);
3724 tunnel_add_peer (t, peer_info);
3725 peer_info_connect (peer_info, t);
3729 /******************************************************************************/
3730 /********************* MESH LOCAL HANDLES **************************/
3731 /******************************************************************************/
3735 * Handler for client disconnection
3737 * @param cls closure
3738 * @param client identification of the client; NULL
3739 * for the last call when the server is destroyed
3742 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3744 struct MeshClient *c;
3745 struct MeshClient *next;
3747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3756 if (c->handle != client)
3758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3764 GNUNET_SERVER_client_drop (c->handle);
3765 c->shutting_down = GNUNET_YES;
3766 GNUNET_assert (NULL != c->own_tunnels);
3767 GNUNET_assert (NULL != c->incoming_tunnels);
3768 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3769 &tunnel_destroy_iterator, c);
3770 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3771 &tunnel_destroy_iterator, c);
3772 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
3773 &tunnel_destroy_iterator, c);
3774 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3775 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3776 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
3778 /* deregister clients applications */
3779 if (NULL != c->apps)
3781 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3782 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3784 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3785 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3787 GNUNET_SCHEDULER_cancel (announce_applications_task);
3788 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3790 if (NULL != c->types)
3791 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3793 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
3804 * Handler for new clients
3806 * @param cls closure
3807 * @param client identification of the client
3808 * @param message the actual message, which includes messages the client wants
3811 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3812 const struct GNUNET_MessageHeader *message)
3814 struct GNUNET_MESH_ClientConnect *cc_msg;
3815 struct MeshClient *c;
3816 GNUNET_MESH_ApplicationType *a;
3823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3824 /* Check data sanity */
3825 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3826 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3827 ntypes = ntohs (cc_msg->types);
3828 napps = ntohs (cc_msg->applications);
3830 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3833 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3837 /* Create new client structure */
3838 c = GNUNET_malloc (sizeof (struct MeshClient));
3839 c->id = next_client_id++;
3840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
3842 GNUNET_SERVER_client_keep (client);
3843 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3846 GNUNET_MESH_ApplicationType at;
3849 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3850 for (i = 0; i < napps; i++)
3853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
3854 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3855 /* store in clients hashmap */
3856 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3857 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3858 /* store in global hashmap, for announcements */
3859 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3860 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3862 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3863 announce_applications_task =
3864 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3872 t = (uint16_t *) & a[napps];
3873 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3874 for (i = 0; i < ntypes; i++)
3877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
3878 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3880 /* store in clients hashmap */
3881 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3882 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3883 /* store in global hashmap */
3884 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3885 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3889 " client has %u+%u subscriptions\n", napps, ntypes);
3891 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3892 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3893 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3894 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3895 GNUNET_SERVER_notification_context_add (nc, client);
3897 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3903 * Handler for requests of new tunnels
3905 * @param cls closure
3906 * @param client identification of the client
3907 * @param message the actual message
3910 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3911 const struct GNUNET_MessageHeader *message)
3913 struct GNUNET_MESH_TunnelMessage *t_msg;
3914 struct MeshTunnel *t;
3915 struct MeshClient *c;
3916 GNUNET_HashCode hash;
3918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3920 /* Sanity check for client registration */
3921 if (NULL == (c = client_get (client)))
3924 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3929 /* Message sanity check */
3930 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3933 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3937 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3938 /* Sanity check for tunnel numbering */
3939 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3945 /* Sanity check for duplicate tunnel IDs */
3946 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3949 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3953 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3954 while (NULL != tunnel_get_by_pi (myid, next_tid))
3955 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3956 t->id.tid = next_tid++;
3957 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3959 t->local_tid = ntohl (t_msg->tunnel_id);
3960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
3961 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3963 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3965 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3967 GNUNET_CONTAINER_multihashmap_put (c->own_tunnels, &hash, t,
3968 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3971 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3975 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3977 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3978 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3981 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3984 t->tree = tree_new (myid);
3986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
3987 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3993 * Handler for requests of deleting tunnels
3995 * @param cls closure
3996 * @param client identification of the client
3997 * @param message the actual message
4000 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4001 const struct GNUNET_MessageHeader *message)
4003 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4004 struct MeshClient *c;
4005 struct MeshTunnel *t;
4006 MESH_TunnelNumber tid;
4008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4009 "Got a DESTROY TUNNEL from client!\n");
4011 /* Sanity check for client registration */
4012 if (NULL == (c = client_get (client)))
4015 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4018 /* Message sanity check */
4019 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4022 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4026 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4028 /* Retrieve tunnel */
4029 tid = ntohl (tunnel_msg->tunnel_id);
4030 t = tunnel_get_by_local_id(c, tid);
4034 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4035 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4038 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4040 client_ignore_tunnel (c, t);
4042 // TODO: when to destroy incoming tunnel?
4043 if (t->nclients == 0)
4045 GNUNET_assert (GNUNET_YES ==
4046 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
4048 GNUNET_assert (GNUNET_YES ==
4049 GNUNET_CONTAINER_multihashmap_remove (t->peers,
4050 &my_full_id.hashPubKey,
4054 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4057 send_client_tunnel_disconnect(t, c);
4058 client_delete_tunnel(c, t);
4060 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4062 tunnel_send_destroy (t);
4064 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4070 * Handler for connection requests to new peers
4072 * @param cls closure
4073 * @param client identification of the client
4074 * @param message the actual message (PeerControl)
4077 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
4078 const struct GNUNET_MessageHeader *message)
4080 struct GNUNET_MESH_PeerControl *peer_msg;
4081 struct MeshPeerInfo *peer_info;
4082 struct MeshClient *c;
4083 struct MeshTunnel *t;
4084 MESH_TunnelNumber tid;
4086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
4087 /* Sanity check for client registration */
4088 if (NULL == (c = client_get (client)))
4091 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4095 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4096 /* Sanity check for message size */
4097 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4100 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4104 /* Tunnel exists? */
4105 tid = ntohl (peer_msg->tunnel_id);
4106 t = tunnel_get_by_local_id (c, tid);
4110 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4114 /* Does client own tunnel? */
4115 if (t->owner->handle != client)
4118 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
4122 GNUNET_i2s (&peer_msg->peer));
4123 peer_info = peer_info_get (&peer_msg->peer);
4125 tunnel_add_peer (t, peer_info);
4126 peer_info_connect (peer_info, t);
4128 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4134 * Handler for disconnection requests of peers in a tunnel
4136 * @param cls closure
4137 * @param client identification of the client
4138 * @param message the actual message (PeerControl)
4141 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
4142 const struct GNUNET_MessageHeader *message)
4144 struct GNUNET_MESH_PeerControl *peer_msg;
4145 struct MeshPeerInfo *peer_info;
4146 struct MeshClient *c;
4147 struct MeshTunnel *t;
4148 MESH_TunnelNumber tid;
4150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
4151 /* Sanity check for client registration */
4152 if (NULL == (c = client_get (client)))
4155 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4158 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4159 /* Sanity check for message size */
4160 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4163 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4167 /* Tunnel exists? */
4168 tid = ntohl (peer_msg->tunnel_id);
4169 t = tunnel_get_by_local_id (c, tid);
4173 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4178 /* Does client own tunnel? */
4179 if (t->owner->handle != client)
4182 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
4187 GNUNET_i2s (&peer_msg->peer));
4188 /* Is the peer in the tunnel? */
4190 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
4191 if (NULL == peer_info)
4194 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4198 /* Ok, delete peer from tunnel */
4199 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
4200 &peer_msg->peer.hashPubKey);
4202 send_destroy_path (t, peer_info->id);
4203 tunnel_delete_peer (t, peer_info->id);
4204 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4210 * Handler for connection requests to new peers by type
4212 * @param cls closure
4213 * @param client identification of the client
4214 * @param message the actual message (ConnectPeerByType)
4217 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
4218 const struct GNUNET_MessageHeader *message)
4220 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
4221 struct MeshClient *c;
4222 struct MeshTunnel *t;
4223 GNUNET_HashCode hash;
4224 MESH_TunnelNumber tid;
4226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
4227 /* Sanity check for client registration */
4228 if (NULL == (c = client_get (client)))
4231 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4235 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
4236 /* Sanity check for message size */
4237 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
4238 ntohs (connect_msg->header.size))
4241 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4245 /* Tunnel exists? */
4246 tid = ntohl (connect_msg->tunnel_id);
4247 t = tunnel_get_by_local_id (c, tid);
4251 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4255 /* Does client own tunnel? */
4256 if (t->owner->handle != client)
4259 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4263 /* Do WE have the service? */
4264 t->type = ntohl (connect_msg->type);
4265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
4266 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
4267 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
4270 /* Yes! Fast forward, add ourselves to the tunnel and send the
4271 * good news to the client, and alert the destination client of
4272 * an incoming tunnel.
4274 * FIXME send a path create to self, avoid code duplication
4276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
4277 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
4278 peer_info_get (&my_full_id),
4279 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
4282 send_client_peer_connected (t, myid);
4283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
4284 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4286 t->local_tid_dest = next_local_tid++;
4287 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4288 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4289 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4293 /* Ok, lets find a peer offering the service */
4294 if (NULL != t->dht_get_type)
4296 GNUNET_DHT_get_stop (t->dht_get_type);
4298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
4299 GNUNET_h2s (&hash));
4301 GNUNET_DHT_get_start (dht_handle,
4302 GNUNET_BLOCK_TYPE_TEST, &hash, 10,
4303 GNUNET_DHT_RO_RECORD_ROUTE |
4304 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
4305 &dht_get_type_handler, t);
4307 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4313 * Handler for client traffic directed to one peer
4315 * @param cls closure
4316 * @param client identification of the client
4317 * @param message the actual message
4320 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4321 const struct GNUNET_MessageHeader *message)
4323 struct MeshClient *c;
4324 struct MeshTunnel *t;
4325 struct MeshPeerInfo *pi;
4326 struct GNUNET_MESH_Unicast *data_msg;
4327 MESH_TunnelNumber tid;
4330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4331 "Got a unicast request from a client!\n");
4333 /* Sanity check for client registration */
4334 if (NULL == (c = client_get (client)))
4337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4340 data_msg = (struct GNUNET_MESH_Unicast *) message;
4341 /* Sanity check for message size */
4342 size = ntohs (message->size);
4343 if (sizeof (struct GNUNET_MESH_Unicast) +
4344 sizeof (struct GNUNET_MessageHeader) > size)
4347 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4351 /* Tunnel exists? */
4352 tid = ntohl (data_msg->tid);
4353 t = tunnel_get_by_local_id (c, tid);
4357 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4361 /* Is it a local tunnel? Then, does client own the tunnel? */
4362 if (t->owner->handle != client)
4365 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4369 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
4370 &data_msg->destination.hashPubKey);
4371 /* Is the selected peer in the tunnel? */
4375 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4379 /* Ok, everything is correct, send the message
4380 * (pretend we got it from a mesh peer)
4383 char buf[ntohs (message->size)] GNUNET_ALIGN;
4384 struct GNUNET_MESH_Unicast *copy;
4386 /* Work around const limitation */
4387 copy = (struct GNUNET_MESH_Unicast *) buf;
4388 memcpy (buf, data_msg, size);
4389 copy->oid = my_full_id;
4390 copy->tid = htonl (t->id.tid);
4391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4392 " calling generic handler...\n");
4393 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4395 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4401 * Handler for client traffic directed to the origin
4403 * @param cls closure
4404 * @param client identification of the client
4405 * @param message the actual message
4408 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4409 const struct GNUNET_MessageHeader *message)
4411 struct GNUNET_MESH_ToOrigin *data_msg;
4412 struct GNUNET_PeerIdentity id;
4413 struct MeshClient *c;
4414 struct MeshTunnel *t;
4415 MESH_TunnelNumber tid;
4418 /* Sanity check for client registration */
4419 if (NULL == (c = client_get (client)))
4422 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4425 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4426 /* Sanity check for message size */
4427 size = ntohs (message->size);
4428 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4429 sizeof (struct GNUNET_MessageHeader) > size)
4432 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4436 /* Tunnel exists? */
4437 tid = ntohl (data_msg->tid);
4438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4439 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
4440 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4446 t = tunnel_get_by_local_id (c, tid);
4450 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4454 /* It should be sent by someone who has this as incoming tunnel. */
4455 if (-1 == client_knows_tunnel (c, t))
4458 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4461 GNUNET_PEER_resolve (t->id.oid, &id);
4463 /* Ok, everything is correct, send the message
4464 * (pretend we got it from a mesh peer)
4467 char buf[ntohs (message->size)] GNUNET_ALIGN;
4468 struct GNUNET_MESH_ToOrigin *copy;
4470 /* Work around const limitation */
4471 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4472 memcpy (buf, data_msg, size);
4474 copy->tid = htonl (t->id.tid);
4475 copy->sender = my_full_id;
4476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4477 " calling generic handler...\n");
4478 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4480 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4486 * Handler for client traffic directed to all peers in a tunnel
4488 * @param cls closure
4489 * @param client identification of the client
4490 * @param message the actual message
4493 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4494 const struct GNUNET_MessageHeader *message)
4496 struct MeshClient *c;
4497 struct MeshTunnel *t;
4498 struct GNUNET_MESH_Multicast *data_msg;
4499 MESH_TunnelNumber tid;
4501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4502 "Got a multicast request from a client!\n");
4504 /* Sanity check for client registration */
4505 if (NULL == (c = client_get (client)))
4508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4511 data_msg = (struct GNUNET_MESH_Multicast *) message;
4512 /* Sanity check for message size */
4513 if (sizeof (struct GNUNET_MESH_Multicast) +
4514 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4521 /* Tunnel exists? */
4522 tid = ntohl (data_msg->tid);
4523 t = tunnel_get_by_local_id (c, tid);
4527 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4531 /* Does client own tunnel? */
4532 if (t->owner->handle != client)
4535 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4540 char buf[ntohs (message->size)] GNUNET_ALIGN;
4541 struct GNUNET_MESH_Multicast *copy;
4543 copy = (struct GNUNET_MESH_Multicast *) buf;
4544 memcpy (buf, message, ntohs (message->size));
4545 copy->oid = my_full_id;
4546 copy->tid = htonl (t->id.tid);
4547 copy->ttl = htonl (DEFAULT_TTL);
4548 copy->mid = htonl (t->mid + 1);
4549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4550 " calling generic handler...\n");
4551 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4554 /* receive done gets called when last copy is sent to a neighbor */
4559 * Functions to handle messages from clients
4561 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4562 {&handle_local_new_client, NULL,
4563 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4564 {&handle_local_tunnel_create, NULL,
4565 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4566 sizeof (struct GNUNET_MESH_TunnelMessage)},
4567 {&handle_local_tunnel_destroy, NULL,
4568 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4569 sizeof (struct GNUNET_MESH_TunnelMessage)},
4570 {&handle_local_connect_add, NULL,
4571 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4572 sizeof (struct GNUNET_MESH_PeerControl)},
4573 {&handle_local_connect_del, NULL,
4574 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4575 sizeof (struct GNUNET_MESH_PeerControl)},
4576 {&handle_local_connect_by_type, NULL,
4577 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4578 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4579 {&handle_local_unicast, NULL,
4580 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4581 {&handle_local_to_origin, NULL,
4582 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4583 {&handle_local_multicast, NULL,
4584 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4590 * To be called on core init/fail.
4592 * @param cls service closure
4593 * @param server handle to the server for this service
4594 * @param identity the public identity of this peer
4597 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4598 const struct GNUNET_PeerIdentity *identity)
4600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4601 core_handle = server;
4602 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4606 GNUNET_SCHEDULER_shutdown ();
4612 * Method called whenever a given peer connects.
4614 * @param cls closure
4615 * @param peer peer identity this notification is about
4616 * @param atsi performance data for the connection
4617 * @param atsi_count number of records in 'atsi'
4620 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4621 const struct GNUNET_ATS_Information *atsi,
4622 unsigned int atsi_count)
4624 struct MeshPeerInfo *peer_info;
4625 struct MeshPeerPath *path;
4627 DEBUG_CONN ("Peer connected\n");
4628 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4629 peer_info = peer_info_get (peer);
4630 if (myid == peer_info->id)
4632 DEBUG_CONN (" (self)\n");
4637 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4639 path = path_new (2);
4640 path->peers[0] = myid;
4641 path->peers[1] = peer_info->id;
4642 GNUNET_PEER_change_rc (myid, 1);
4643 GNUNET_PEER_change_rc (peer_info->id, 1);
4644 peer_info_add_path (peer_info, path, GNUNET_YES);
4649 * Method called whenever a peer disconnects.
4651 * @param cls closure
4652 * @param peer peer identity this notification is about
4655 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4657 struct MeshPeerInfo *pi;
4659 DEBUG_CONN ("Peer disconnected\n");
4660 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4666 while (NULL != pi->queue_head)
4668 queue_destroy(pi->queue_head, GNUNET_YES);
4670 peer_info_remove_path (pi, pi->id, myid);
4673 DEBUG_CONN (" (self)\n");
4679 /******************************************************************************/
4680 /************************ MAIN FUNCTIONS ****************************/
4681 /******************************************************************************/
4684 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4686 * @param cls closure
4687 * @param key current key code
4688 * @param value value in the hash map
4689 * @return GNUNET_YES if we should continue to iterate,
4693 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4695 struct MeshTunnel *t = value;
4702 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4704 * @param cls closure
4705 * @param key current key code
4706 * @param value value in the hash map
4707 * @return GNUNET_YES if we should continue to iterate,
4711 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4713 struct MeshPeerInfo *p = value;
4715 while (NULL != p->queue_head)
4717 queue_destroy(p->queue_head, GNUNET_YES);
4719 peer_info_destroy (p);
4724 * Task run during shutdown.
4730 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4734 if (core_handle != NULL)
4736 GNUNET_CORE_disconnect (core_handle);
4739 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4740 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4741 if (dht_handle != NULL)
4743 GNUNET_DHT_disconnect (dht_handle);
4748 GNUNET_SERVER_notification_context_destroy (nc);
4751 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4753 GNUNET_SCHEDULER_cancel (announce_id_task);
4754 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4760 * Process mesh requests.
4762 * @param cls closure
4763 * @param server the initialized server
4764 * @param c configuration to use
4767 run (void *cls, struct GNUNET_SERVER_Handle *server,
4768 const struct GNUNET_CONFIGURATION_Handle *c)
4770 struct MeshPeerInfo *peer;
4771 struct MeshPeerPath *p;
4774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4775 server_handle = server;
4776 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4778 NULL, /* Closure passed to MESH functions */
4779 &core_init, /* Call core_init once connected */
4780 &core_connect, /* Handle connects */
4781 &core_disconnect, /* remove peers on disconnects */
4782 NULL, /* Don't notify about all incoming messages */
4783 GNUNET_NO, /* For header only in notification */
4784 NULL, /* Don't notify about all outbound messages */
4785 GNUNET_NO, /* For header-only out notification */
4786 core_handlers); /* Register these handlers */
4788 if (core_handle == NULL)
4791 GNUNET_SCHEDULER_shutdown ();
4796 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4799 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4801 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4802 GNUNET_SCHEDULER_shutdown ();
4805 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4806 GNUNET_free (keyfile);
4807 if (my_private_key == NULL)
4809 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4810 _("Mesh service could not access hostkey. Exiting.\n"));
4811 GNUNET_SCHEDULER_shutdown ();
4814 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4815 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4816 &my_full_id.hashPubKey);
4817 myid = GNUNET_PEER_intern (&my_full_id);
4819 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4826 dht_handle = GNUNET_DHT_connect (c, 64);
4827 if (dht_handle == NULL)
4833 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4835 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4836 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4837 peers = GNUNET_CONTAINER_multihashmap_create (32);
4838 applications = GNUNET_CONTAINER_multihashmap_create (32);
4839 types = GNUNET_CONTAINER_multihashmap_create (32);
4841 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4842 nc = GNUNET_SERVER_notification_context_create (server_handle,
4844 GNUNET_SERVER_disconnect_notify (server_handle,
4845 &handle_local_client_disconnect, NULL);
4849 clients_tail = NULL;
4852 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4853 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4855 /* Create a peer_info for the local peer */
4856 peer = peer_info_get (&my_full_id);
4859 GNUNET_PEER_change_rc (myid, 1);
4860 peer_info_add_path (peer, p, GNUNET_YES);
4862 /* Scheduled the task to clean up when shutdown is called */
4863 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
4870 * The main function for the mesh service.
4872 * @param argc number of arguments from the command line
4873 * @param argv command line arguments
4874 * @return 0 ok, 1 on error
4877 main (int argc, char *const *argv)
4881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
4884 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");