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;
1565 GNUNET_PEER_resolve (pi->id, &id);
1566 GNUNET_PEER_change_rc (pi->id, -1);
1569 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1572 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1573 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1575 if (NULL != pi->dhtget)
1577 GNUNET_DHT_get_stop (pi->dhtget);
1578 GNUNET_free (pi->dhtgetcls);
1580 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1582 // FIXME CORE_QUEUE_SIZE
1588 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1598 * Remove all paths that rely on a direct connection between p1 and p2
1599 * from the peer itself and notify all tunnels about it.
1601 * @param peer PeerInfo of affected peer.
1602 * @param p1 GNUNET_PEER_Id of one peer.
1603 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1606 * TODO: optimize (see below)
1609 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1612 struct MeshPeerPath *p;
1613 struct MeshPeerPath *aux;
1614 struct MeshPeerInfo *peer_d;
1616 unsigned int destroyed;
1622 p = peer->path_head;
1626 for (i = 0; i < (p->length - 1); i++)
1628 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1629 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1631 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1642 for (i = 0; i < peer->ntunnels; i++)
1644 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1648 * Problem: one or more peers have been deleted from the tunnel tree.
1649 * We don't know who they are to try to add them again.
1650 * We need to try to find a new path for each of the disconnected peers.
1651 * Some of them might already have a path to reach them that does not
1652 * involve p1 and p2. Adding all anew might render in a better tree than
1653 * the trivial immediate fix.
1655 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1656 * its children will be reachable trough him.
1658 peer_d = peer_info_get_short (d);
1661 for (p = peer_d->path_head; NULL != p; p = p->next)
1663 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1671 /* No callback, as peer will be already disconnected and a connection
1672 * scheduled by tunnel_notify_connection_broken.
1674 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1678 peer_info_connect (peer_d, peer->tunnels[i]);
1685 * Add the path to the peer and update the path used to reach it in case this
1688 * @param peer_info Destination peer to add the path to.
1689 * @param path New path to add. Last peer must be the peer in arg 1.
1690 * Path will be either used of freed if already known.
1691 * @param trusted Do we trust that this path is real?
1694 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1697 struct MeshPeerPath *aux;
1701 if ((NULL == peer_info) || (NULL == path))
1704 path_destroy (path);
1707 if (path->peers[path->length - 1] != peer_info->id)
1710 path_destroy (path);
1713 if (path->length <= 2 && GNUNET_NO == trusted)
1715 /* Only allow CORE to tell us about direct paths */
1716 path_destroy (path);
1719 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1720 for (l = 1; l < path->length; l++)
1722 if (path->peers[l] == myid)
1724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1725 for (l2 = 0; l2 < path->length - l; l2++)
1727 path->peers[l2] = path->peers[l + l2];
1732 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1737 struct GNUNET_PeerIdentity id;
1739 GNUNET_PEER_resolve (peer_info->id, &id);
1740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1741 path->length, GNUNET_i2s (&id));
1744 l = path_get_length (path);
1751 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1752 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1754 l2 = path_get_length (aux);
1757 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1758 peer_info->path_tail, aux, path);
1763 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1765 path_destroy (path);
1770 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1777 * Add the path to the origin peer and update the path used to reach it in case
1778 * this is the shortest.
1779 * The path is given in peer_info -> destination, therefore we turn the path
1780 * upside down first.
1782 * @param peer_info Peer to add the path to, being the origin of the path.
1783 * @param path New path to add after being inversed.
1784 * @param trusted Do we trust that this path is real?
1787 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1788 struct MeshPeerPath *path, int trusted)
1791 peer_info_add_path (peer_info, path, trusted);
1796 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1797 * to obtain a local peer -> destination path and interning the peer ids.
1799 * @return Newly allocated and created path
1801 static struct MeshPeerPath *
1802 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1803 unsigned int get_path_length,
1804 const struct GNUNET_PeerIdentity *put_path,
1805 unsigned int put_path_length)
1807 struct MeshPeerPath *p;
1813 GNUNET_PEER_change_rc (myid, 1);
1814 i = get_path_length;
1815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1816 for (i--; i >= 0; i--)
1818 id = GNUNET_PEER_intern (&get_path[i]);
1819 if (p->length > 0 && id == p->peers[p->length - 1])
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1822 GNUNET_PEER_change_rc (id, -1);
1826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1827 GNUNET_i2s (&get_path[i]));
1829 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1830 p->peers[p->length - 1] = id;
1833 i = put_path_length;
1834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1835 for (i--; i >= 0; i--)
1837 id = GNUNET_PEER_intern (&put_path[i]);
1840 /* PUT path went through us, so discard the path up until now and start
1841 * from here to get a much shorter (and loop-free) path.
1846 if (p->length > 0 && id == p->peers[p->length - 1])
1848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1849 GNUNET_PEER_change_rc (id, -1);
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1854 GNUNET_i2s (&put_path[i]));
1856 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1857 p->peers[p->length - 1] = id;
1861 if (get_path_length > 0)
1862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1863 GNUNET_i2s (&get_path[0]));
1864 if (put_path_length > 0)
1865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1866 GNUNET_i2s (&put_path[0]));
1867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1869 for (i = 0; i < p->length; i++)
1871 struct GNUNET_PeerIdentity peer_id;
1873 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1875 GNUNET_i2s (&peer_id));
1883 * Adds a path to the peer_infos of all the peers in the path
1885 * @param p Path to process.
1886 * @param confirmed Whether we know if the path works or not. FIXME use
1889 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1893 /* TODO: invert and add */
1894 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1895 for (i++; i < p->length; i++)
1897 struct MeshPeerInfo *aux;
1898 struct MeshPeerPath *copy;
1900 aux = peer_info_get_short (p->peers[i]);
1901 copy = path_duplicate (p);
1902 copy->length = i + 1;
1903 peer_info_add_path (aux, copy, GNUNET_NO);
1909 * Send keepalive packets for a peer
1911 * @param cls Closure (tunnel for which to send the keepalive).
1912 * @param tc Notification context.
1914 * TODO: implement explicit multicast keepalive?
1917 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1921 * Search for a tunnel among the incoming tunnels
1923 * @param tid the local id of the tunnel
1925 * @return tunnel handler, NULL if doesn't exist
1927 static struct MeshTunnel *
1928 tunnel_get_incoming (MESH_TunnelNumber tid)
1930 GNUNET_HashCode hash;
1932 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1933 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1934 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1939 * Search for a tunnel among the tunnels for a client
1941 * @param c the client whose tunnels to search in
1942 * @param tid the local id of the tunnel
1944 * @return tunnel handler, NULL if doesn't exist
1946 static struct MeshTunnel *
1947 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1949 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1951 return tunnel_get_incoming (tid);
1955 GNUNET_HashCode hash;
1957 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1958 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1964 * Search for a tunnel by global ID using PEER_ID
1966 * @param pi owner of the tunnel
1967 * @param tid global tunnel number
1969 * @return tunnel handler, NULL if doesn't exist
1971 static struct MeshTunnel *
1972 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1974 struct MESH_TunnelID id;
1975 GNUNET_HashCode hash;
1980 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1981 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1986 * Search for a tunnel by global ID using full PeerIdentities
1988 * @param oid owner of the tunnel
1989 * @param tid global tunnel number
1991 * @return tunnel handler, NULL if doesn't exist
1993 static struct MeshTunnel *
1994 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1996 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2001 * Delete an active client from the tunnel.
2007 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2011 for (i = 0; i < t->nclients; i++)
2013 if (t->clients[i] == c)
2015 t->clients[i] = t->clients[t->nclients - 1];
2016 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2024 * Delete an ignored client from the tunnel.
2030 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2034 for (i = 0; i < t->nignore; i++)
2036 if (t->ignore[i] == c)
2038 t->ignore[i] = t->ignore[t->nignore - 1];
2039 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2047 * Delete a client from the tunnel. It should be only done on
2048 * client disconnection, otherwise use client_ignore_tunnel.
2054 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2056 tunnel_delete_ignored_client (t, c);
2057 tunnel_delete_active_client (t, c);
2062 * Callback used to notify a client owner of a tunnel that a peer has
2063 * disconnected, most likely because of a path change.
2065 * @param cls Closure (tunnel this notification is about).
2066 * @param peer_id Short ID of disconnected peer.
2069 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2071 struct MeshTunnel *t = cls;
2072 struct MeshPeerInfo *peer;
2073 struct MeshPathInfo *path_info;
2075 if (NULL != t->owner && NULL != nc)
2077 struct GNUNET_MESH_PeerControl msg;
2079 msg.header.size = htons (sizeof (msg));
2080 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2081 msg.tunnel_id = htonl (t->local_tid);
2082 GNUNET_PEER_resolve (peer_id, &msg.peer);
2083 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2084 &msg.header, GNUNET_NO);
2086 peer = peer_info_get_short (peer_id);
2087 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2088 path_info->peer = peer;
2090 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2095 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2096 * needed rescources.
2097 * If peer already exists, reevaluate shortest path and change if different.
2099 * @param t Tunnel we want to add a new peer to
2100 * @param peer PeerInfo of the peer being added
2104 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2106 struct GNUNET_PeerIdentity id;
2107 struct MeshPeerPath *best_p;
2108 struct MeshPeerPath *p;
2109 unsigned int best_cost;
2112 GNUNET_PEER_resolve (peer->id, &id);
2114 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2117 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2118 GNUNET_assert (GNUNET_OK ==
2119 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2121 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2124 if (NULL != (p = peer->path_head))
2127 best_cost = tree_get_path_cost (t->tree, p);
2130 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2137 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2138 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2139 t->path_refresh_task =
2140 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2144 /* Start a DHT get */
2145 peer_info_connect (peer, t);
2150 * Add a path to a tunnel which we don't own, just to remember the next hop.
2151 * If destination node was already in the tunnel, the first hop information
2152 * will be replaced with the new path.
2154 * @param t Tunnel we want to add a new peer to
2155 * @param p Path to add
2156 * @param own_pos Position of local node in path.
2160 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2161 unsigned int own_pos)
2163 struct GNUNET_PeerIdentity id;
2165 GNUNET_assert (0 != own_pos);
2166 tree_add_path (t->tree, p, NULL, NULL);
2167 if (own_pos < p->length - 1)
2169 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2170 tree_update_first_hops (t->tree, myid, &id);
2176 * Notifies a tunnel that a connection has broken that affects at least
2177 * some of its peers. Sends a notification towards the root of the tree.
2178 * In case the peer is the owner of the tree, notifies the client that owns
2179 * the tunnel and tries to reconnect.
2181 * @param t Tunnel affected.
2182 * @param p1 Peer that got disconnected from p2.
2183 * @param p2 Peer that got disconnected from p1.
2185 * @return Short ID of the peer disconnected (either p1 or p2).
2186 * 0 if the tunnel remained unaffected.
2188 static GNUNET_PEER_Id
2189 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2195 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2197 if (myid != p1 && myid != p2)
2203 if (tree_get_predecessor (t->tree) != 0)
2205 /* We are the peer still connected, notify owner of the disconnection. */
2206 struct GNUNET_MESH_PathBroken msg;
2207 struct GNUNET_PeerIdentity neighbor;
2209 msg.header.size = htons (sizeof (msg));
2210 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2211 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2212 msg.tid = htonl (t->id.tid);
2213 msg.peer1 = my_full_id;
2214 GNUNET_PEER_resolve (pid, &msg.peer2);
2215 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2216 send_message (&msg.header, &neighbor);
2224 * Send a multicast packet to a neighbor.
2226 * @param cls Closure (Info about the multicast packet)
2227 * @param neighbor_id Short ID of the neighbor to send the packet to.
2230 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2232 struct MeshData *mdata = cls;
2233 struct MeshTransmissionDescriptor *info;
2234 struct GNUNET_PeerIdentity neighbor;
2236 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2238 info->mesh_data = mdata;
2239 (*(mdata->reference_counter)) ++;
2240 info->destination = neighbor_id;
2241 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2243 GNUNET_i2s (&neighbor));
2244 info->peer = peer_info_get (&neighbor);
2245 GNUNET_assert (NULL != info->peer);
2247 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
2248 info->mesh_data->data_len,
2254 * Send a message in a tunnel in multicast, sending a copy to each child node
2255 * down the local one in the tunnel tree.
2257 * @param t Tunnel in which to send the data.
2258 * @param msg Message to be sent.
2259 * @param internal Has the service generated this message?
2262 tunnel_send_multicast (struct MeshTunnel *t,
2263 const struct GNUNET_MessageHeader *msg,
2266 struct MeshData *mdata;
2268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2269 " sending a multicast packet...\n");
2270 mdata = GNUNET_malloc (sizeof (struct MeshData));
2271 mdata->data_len = ntohs (msg->size);
2272 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2274 mdata->data = GNUNET_malloc (mdata->data_len);
2275 memcpy (mdata->data, msg, mdata->data_len);
2276 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2278 struct GNUNET_MESH_Multicast *mcast;
2280 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2281 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2283 ntohl (mcast->ttl));
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2289 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2290 && GNUNET_NO == internal)
2292 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2294 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2300 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2301 if (*(mdata->reference_counter) == 0)
2303 GNUNET_free (mdata->data);
2304 GNUNET_free (mdata->reference_counter);
2305 if (NULL != mdata->task)
2307 GNUNET_SCHEDULER_cancel(*(mdata->task));
2308 GNUNET_free (mdata->task);
2309 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2311 // FIXME change order?
2312 GNUNET_free (mdata);
2314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2315 " sending a multicast packet done\n");
2321 * Send a message to all peers in this tunnel that the tunnel is no longer
2324 * @param t The tunnel whose peers to notify.
2327 tunnel_send_destroy (struct MeshTunnel *t)
2329 struct GNUNET_MESH_TunnelDestroy msg;
2331 msg.header.size = htons (sizeof (msg));
2332 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2333 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2334 msg.tid = htonl (t->id.tid);
2335 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2341 * Destroy the tunnel and free any allocated resources linked to it.
2343 * @param t the tunnel to destroy
2345 * @return GNUNET_OK on success
2348 tunnel_destroy (struct MeshTunnel *t)
2350 struct MeshClient *c;
2351 struct MeshQueue *q;
2352 struct MeshQueue *qn;
2353 GNUNET_HashCode hash;
2364 struct GNUNET_PeerIdentity id;
2366 GNUNET_PEER_resolve (t->id.oid, &id);
2367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2368 GNUNET_i2s (&id), t->id.tid);
2370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2374 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2375 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2380 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2383 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2387 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2388 for (i = 0; i < t->nclients; i++)
2392 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2397 for (i = 0; i < t->nignore; i++)
2401 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2406 if (t->nclients > 0)
2409 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2413 GNUNET_free (t->clients);
2415 if (NULL != t->peers)
2417 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2419 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2424 if (NULL != q->data)
2425 GNUNET_free (q->data);
2429 /* TODO cancel core transmit ready in case it was active */
2431 tree_destroy (t->tree);
2432 if (NULL != t->dht_get_type)
2433 GNUNET_DHT_get_stop (t->dht_get_type);
2434 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2435 GNUNET_SCHEDULER_cancel (t->timeout_task);
2436 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2437 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2444 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2445 * that are no longer needed, as well as nodes of no longer reachable peers.
2446 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2448 * @param t Tunnel from which to remove the path.
2449 * @param peer Short id of the peer which should be removed.
2452 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2454 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2460 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2461 * client when the client disconnects. If the client is not the owner, the
2462 * owner will get notified if no more clients are in the tunnel and the client
2463 * get removed from the tunnel's list.
2465 * @param cls closure (client that is disconnecting)
2466 * @param key the hash of the local tunnel id (used to access the hashmap)
2467 * @param value the value stored at the key (tunnel to destroy)
2469 * @return GNUNET_OK on success
2472 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2474 struct MeshTunnel *t = value;
2475 struct MeshClient *c = cls;
2478 send_client_tunnel_disconnect(t, c);
2481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2482 "Client %u is destination, keeping the tunnel alive.\n", c->id);
2483 tunnel_delete_client(t, c);
2484 client_delete_tunnel(c, t);
2487 tunnel_send_destroy(t);
2488 r = tunnel_destroy (t);
2494 * Timeout function, destroys tunnel if called
2496 * @param cls Closure (tunnel to destroy).
2497 * @param tc TaskContext
2500 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2502 struct MeshTunnel *t = cls;
2504 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2506 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2511 * Resets the tunnel timeout. Starts it if no timeout was running.
2513 * @param t Tunnel whose timeout to reset.
2516 tunnel_reset_timeout (struct MeshTunnel *t)
2518 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2519 GNUNET_SCHEDULER_cancel (t->timeout_task);
2521 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2522 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2526 /******************************************************************************/
2527 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2528 /******************************************************************************/
2531 * Function to send a create path packet to a peer.
2533 * @param cls closure
2534 * @param size number of bytes available in buf
2535 * @param buf where the callee should write the message
2536 * @return number of bytes written to buf
2539 send_core_path_create (void *cls, size_t size, void *buf)
2541 struct MeshPathInfo *info = cls;
2542 struct GNUNET_MESH_ManipulatePath *msg;
2543 struct GNUNET_PeerIdentity *peer_ptr;
2544 struct MeshTunnel *t = info->t;
2545 struct MeshPeerPath *p = info->path;
2549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2551 sizeof (struct GNUNET_MESH_ManipulatePath) +
2552 p->length * sizeof (struct GNUNET_PeerIdentity);
2554 if (size < size_needed || NULL == buf)
2559 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2560 msg->header.size = htons (size_needed);
2561 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2562 msg->tid = ntohl (t->id.tid);
2564 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2565 for (i = 0; i < p->length; i++)
2567 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2574 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2580 * Fill the core buffer
2582 * @param cls closure (data itself)
2583 * @param size number of bytes available in buf
2584 * @param buf where the callee should write the message
2586 * @return number of bytes written to buf
2589 send_core_data_multicast (void *cls, size_t size, void *buf)
2591 struct MeshTransmissionDescriptor *info = cls;
2594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
2595 GNUNET_assert (NULL != info);
2596 GNUNET_assert (NULL != info->peer);
2597 total_size = info->mesh_data->data_len;
2598 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2600 if (total_size > size)
2605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
2606 memcpy (buf, info->mesh_data->data, total_size);
2609 struct GNUNET_MESH_Multicast *mc;
2610 struct GNUNET_MessageHeader *mh;
2613 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2615 mc = (struct GNUNET_MESH_Multicast *) mh;
2616 mh = (struct GNUNET_MessageHeader *) &mc[1];
2617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2618 " multicast, payload type %u\n", ntohs (mh->type));
2619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2620 " multicast, payload size %u\n", ntohs (mh->size));
2624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
2629 data_descriptor_decrement_multicast (info->mesh_data);
2630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
2632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
2638 * Creates a path ack message in buf and frees all unused resources.
2640 * @param cls closure (MeshTransmissionDescriptor)
2641 * @param size number of bytes available in buf
2642 * @param buf where the callee should write the message
2643 * @return number of bytes written to buf
2646 send_core_path_ack (void *cls, size_t size, void *buf)
2648 struct MeshTransmissionDescriptor *info = cls;
2649 struct GNUNET_MESH_PathACK *msg = buf;
2651 GNUNET_assert (NULL != info);
2652 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2657 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2658 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2659 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2660 msg->tid = htonl (info->origin->tid);
2661 msg->peer_id = my_full_id;
2664 /* TODO add signature */
2666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2667 return sizeof (struct GNUNET_MESH_PathACK);
2672 * Free a transmission that was already queued with all resources
2673 * associated to the request.
2675 * @param queue Queue handler to cancel.
2678 queue_destroy (struct MeshPeerQueue *queue)
2680 struct MeshTransmissionDescriptor *dd;
2681 struct MeshPathInfo *path_info;
2682 struct MeshPeerInfo *peer;
2685 switch (queue->type)
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);
2702 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
2708 * Core callback to write a queued packet to core buffer
2710 * @param cls Closure (peer info).
2711 * @param size Number of bytes available in buf.
2712 * @param buf Where the to write the message.
2714 * @return number of bytes written to buf
2717 queue_send (void *cls, size_t size, void *buf)
2719 struct MeshPeerInfo *peer = cls;
2720 struct MeshPeerQueue *queue;
2723 peer->core_transmit = NULL;
2724 queue = peer->queue_head;
2726 /* If queue is empty, send should have been cancelled */
2733 /* Check if buffer size is enough for the message */
2734 if (queue->size < size)
2736 struct GNUNET_PeerIdentity id;
2738 GNUNET_PEER_resolve (peer->id, &id);
2739 peer->core_transmit =
2740 GNUNET_CORE_notify_transmit_ready(core_handle,
2743 GNUNET_TIME_UNIT_FOREVER_REL,
2752 switch (queue->type)
2754 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2755 data_size = send_core_data_raw (queue->cls, size, buf);
2757 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2758 data_size = send_core_data_multicast(queue->cls, size, buf);
2760 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2763 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2764 data_size = send_core_path_create(queue->cls, size, buf);
2766 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2767 data_size = send_core_path_ack(queue->cls, size, buf);
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
2775 /* Free resources */
2776 queue_destroy(queue);
2778 /* If more data in queue, send next */
2779 if (NULL != peer->queue_head)
2781 struct GNUNET_PeerIdentity id;
2783 GNUNET_PEER_resolve (peer->id, &id);
2784 peer->core_transmit =
2785 GNUNET_CORE_notify_transmit_ready(core_handle,
2788 GNUNET_TIME_UNIT_FOREVER_REL,
2790 peer->queue_head->size,
2799 * Queue and pass message to core when possible.
2801 * @param cls Closure (type dependant).
2802 * @param type Type of the message.
2803 * @param size Size of the message.
2804 * @param dst Neighbor to send message to.
2807 queue_add (void *cls, uint16_t type, size_t size, struct MeshPeerInfo *dst)
2809 struct MeshPeerQueue *queue;
2811 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2816 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
2817 if (NULL == dst->core_transmit)
2819 struct GNUNET_PeerIdentity id;
2821 GNUNET_PEER_resolve (dst->id, &id);
2822 dst->core_transmit =
2823 GNUNET_CORE_notify_transmit_ready(core_handle,
2826 GNUNET_TIME_UNIT_FOREVER_REL,
2835 /******************************************************************************/
2836 /******************** MESH NETWORK HANDLERS **************************/
2837 /******************************************************************************/
2841 * Core handler for path creation
2843 * @param cls closure
2844 * @param message message
2845 * @param peer peer identity this notification is about
2846 * @param atsi performance data
2847 * @param atsi_count number of records in 'atsi'
2849 * @return GNUNET_OK to keep the connection open,
2850 * GNUNET_SYSERR to close it (signal serious error)
2853 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2854 const struct GNUNET_MessageHeader *message,
2855 const struct GNUNET_ATS_Information *atsi,
2856 unsigned int atsi_count)
2858 unsigned int own_pos;
2861 MESH_TunnelNumber tid;
2862 struct GNUNET_MESH_ManipulatePath *msg;
2863 struct GNUNET_PeerIdentity *pi;
2864 GNUNET_HashCode hash;
2865 struct MeshPeerPath *path;
2866 struct MeshPeerInfo *dest_peer_info;
2867 struct MeshPeerInfo *orig_peer_info;
2868 struct MeshTunnel *t;
2870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2871 "Received a path create msg [%s]\n",
2872 GNUNET_i2s (&my_full_id));
2873 size = ntohs (message->size);
2874 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2876 GNUNET_break_op (0);
2880 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2881 if (size % sizeof (struct GNUNET_PeerIdentity))
2883 GNUNET_break_op (0);
2886 size /= sizeof (struct GNUNET_PeerIdentity);
2889 GNUNET_break_op (0);
2892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2893 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2895 tid = ntohl (msg->tid);
2896 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2898 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2899 t = tunnel_get (pi, tid);
2902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2903 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2904 t->id.oid = GNUNET_PEER_intern (pi);
2906 while (NULL != tunnel_get_incoming (next_local_tid))
2907 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2908 t->local_tid_dest = next_local_tid++;
2909 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2910 t->tree = tree_new (t->id.oid);
2912 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2914 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2915 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2921 tunnel_reset_timeout (t);
2922 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2924 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2925 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2933 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2934 if (NULL == dest_peer_info)
2936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2937 " Creating PeerInfo for destination.\n");
2938 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2939 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2940 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2942 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2944 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2945 if (NULL == orig_peer_info)
2947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2948 " Creating PeerInfo for origin.\n");
2949 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2950 orig_peer_info->id = GNUNET_PEER_intern (pi);
2951 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2952 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
2955 path = path_new (size);
2957 for (i = 0; i < size; i++)
2959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
2960 GNUNET_i2s (&pi[i]));
2961 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2962 if (path->peers[i] == myid)
2965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
2968 /* cannot be self, must be 'not found' */
2969 /* create path: self not found in path through self */
2970 GNUNET_break_op (0);
2971 path_destroy (path);
2972 /* FIXME error. destroy tunnel? leave for timeout? */
2975 path_add_to_peers (path, GNUNET_NO);
2976 tunnel_add_path (t, path, own_pos);
2977 if (own_pos == size - 1)
2979 /* It is for us! Send ack. */
2980 struct MeshTransmissionDescriptor *info;
2982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2983 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2984 if (NULL == t->peers)
2986 /* New tunnel! Notify clients on data. */
2987 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2989 GNUNET_break (GNUNET_OK ==
2990 GNUNET_CONTAINER_multihashmap_put (t->peers,
2991 &my_full_id.hashPubKey,
2994 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
2995 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2996 info->origin = &t->id;
2997 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2998 GNUNET_assert (NULL != info->peer);
3000 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3001 sizeof (struct GNUNET_MESH_PathACK),
3006 struct MeshPeerPath *path2;
3008 /* It's for somebody else! Retransmit. */
3009 path2 = path_duplicate (path);
3010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3011 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3012 path2 = path_duplicate (path);
3013 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3014 send_create_path (dest_peer_info, path, t);
3021 * Core handler for path destruction
3023 * @param cls closure
3024 * @param message message
3025 * @param peer peer identity this notification is about
3026 * @param atsi performance data
3027 * @param atsi_count number of records in 'atsi'
3029 * @return GNUNET_OK to keep the connection open,
3030 * GNUNET_SYSERR to close it (signal serious error)
3033 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3034 const struct GNUNET_MessageHeader *message,
3035 const struct GNUNET_ATS_Information *atsi,
3036 unsigned int atsi_count)
3038 struct GNUNET_MESH_ManipulatePath *msg;
3039 struct GNUNET_PeerIdentity *pi;
3040 struct MeshPeerPath *path;
3041 struct MeshTunnel *t;
3042 unsigned int own_pos;
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3047 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3048 size = ntohs (message->size);
3049 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3051 GNUNET_break_op (0);
3055 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3056 if (size % sizeof (struct GNUNET_PeerIdentity))
3058 GNUNET_break_op (0);
3061 size /= sizeof (struct GNUNET_PeerIdentity);
3064 GNUNET_break_op (0);
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3069 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3070 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3072 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3074 t = tunnel_get (pi, ntohl (msg->tid));
3077 /* TODO notify back: we don't know this tunnel */
3078 GNUNET_break_op (0);
3081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3082 path = path_new (size);
3084 for (i = 0; i < size; i++)
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3087 GNUNET_i2s (&pi[i]));
3088 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3089 if (path->peers[i] == myid)
3092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3093 if (own_pos < path->length - 1)
3094 send_message (message, &pi[own_pos + 1]);
3096 send_client_tunnel_disconnect(t, NULL);
3098 tunnel_delete_peer (t, path->peers[path->length - 1]);
3099 path_destroy (path);
3105 * Core handler for notifications of broken paths
3107 * @param cls closure
3108 * @param message message
3109 * @param peer peer identity this notification is about
3110 * @param atsi performance data
3111 * @param atsi_count number of records in 'atsi'
3113 * @return GNUNET_OK to keep the connection open,
3114 * GNUNET_SYSERR to close it (signal serious error)
3117 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3118 const struct GNUNET_MessageHeader *message,
3119 const struct GNUNET_ATS_Information *atsi,
3120 unsigned int atsi_count)
3122 struct GNUNET_MESH_PathBroken *msg;
3123 struct MeshTunnel *t;
3125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3126 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3127 msg = (struct GNUNET_MESH_PathBroken *) message;
3128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3129 GNUNET_i2s (&msg->peer1));
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3131 GNUNET_i2s (&msg->peer2));
3132 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3135 GNUNET_break_op (0);
3138 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3139 GNUNET_PEER_search (&msg->peer2));
3146 * Core handler for tunnel destruction
3148 * @param cls closure
3149 * @param message message
3150 * @param peer peer identity this notification is about
3151 * @param atsi performance data
3152 * @param atsi_count number of records in 'atsi'
3154 * @return GNUNET_OK to keep the connection open,
3155 * GNUNET_SYSERR to close it (signal serious error)
3158 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3159 const struct GNUNET_MessageHeader *message,
3160 const struct GNUNET_ATS_Information *atsi,
3161 unsigned int atsi_count)
3163 struct GNUNET_MESH_TunnelDestroy *msg;
3164 struct MeshTunnel *t;
3166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3167 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3168 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3170 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3171 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3174 /* Probably already got the message from another path,
3175 * destroyed the tunnel and retransmitted to children.
3180 if (t->id.oid == myid)
3182 GNUNET_break_op (0);
3185 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3187 /* Tunnel was incoming, notify clients */
3188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3189 t->local_tid, t->local_tid_dest);
3190 send_clients_tunnel_destroy (t);
3192 tunnel_send_destroy (t);
3199 * Core handler for mesh network traffic going from the origin to a peer
3201 * @param cls closure
3202 * @param peer peer identity this notification is about
3203 * @param message message
3204 * @param atsi performance data
3205 * @param atsi_count number of records in 'atsi'
3206 * @return GNUNET_OK to keep the connection open,
3207 * GNUNET_SYSERR to close it (signal serious error)
3210 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3211 const struct GNUNET_MessageHeader *message,
3212 const struct GNUNET_ATS_Information *atsi,
3213 unsigned int atsi_count)
3215 struct GNUNET_MESH_Unicast *msg;
3216 struct MeshTunnel *t;
3220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3222 size = ntohs (message->size);
3224 sizeof (struct GNUNET_MESH_Unicast) +
3225 sizeof (struct GNUNET_MessageHeader))
3230 msg = (struct GNUNET_MESH_Unicast *) message;
3231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3232 ntohs (msg[1].header.type));
3233 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3236 /* TODO notify back: we don't know this tunnel */
3237 GNUNET_break_op (0);
3240 tunnel_reset_timeout (t);
3241 pid = GNUNET_PEER_search (&msg->destination);
3244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3245 " it's for us! sending to clients...\n");
3246 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3250 " not for us, retransmitting...\n");
3251 send_message (message, tree_get_first_hop (t->tree, pid));
3257 * Core handler for mesh network traffic going from the origin to all peers
3259 * @param cls closure
3260 * @param message message
3261 * @param peer peer identity this notification is about
3262 * @param atsi performance data
3263 * @param atsi_count number of records in 'atsi'
3264 * @return GNUNET_OK to keep the connection open,
3265 * GNUNET_SYSERR to close it (signal serious error)
3267 * TODO: Check who we got this from, to validate route.
3270 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3271 const struct GNUNET_MessageHeader *message,
3272 const struct GNUNET_ATS_Information *atsi,
3273 unsigned int atsi_count)
3275 struct GNUNET_MESH_Multicast *msg;
3276 struct MeshTunnel *t;
3279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
3281 size = ntohs (message->size);
3282 if (sizeof (struct GNUNET_MESH_Multicast) +
3283 sizeof (struct GNUNET_MessageHeader) > size)
3285 GNUNET_break_op (0);
3288 msg = (struct GNUNET_MESH_Multicast *) message;
3289 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3293 /* TODO notify that we dont know that tunnel */
3294 GNUNET_break_op (0);
3297 if (t->mid == ntohl (msg->mid))
3299 /* FIXME: already seen this packet, log dropping */
3300 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3301 " Already seen mid %u, DROPPING!\n", t->mid);
3306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3307 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3309 t->mid = ntohl (msg->mid);
3310 tunnel_reset_timeout (t);
3312 /* Transmit to locally interested clients */
3313 if (NULL != t->peers &&
3314 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3316 send_subscribed_clients (message, &msg[1].header);
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
3319 if (ntohl (msg->ttl) == 0)
3321 /* FIXME: ttl is 0, log dropping */
3322 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3325 tunnel_send_multicast (t, message, GNUNET_NO);
3331 * Core handler for mesh network traffic toward the owner of a tunnel
3333 * @param cls closure
3334 * @param message message
3335 * @param peer peer identity this notification is about
3336 * @param atsi performance data
3337 * @param atsi_count number of records in 'atsi'
3339 * @return GNUNET_OK to keep the connection open,
3340 * GNUNET_SYSERR to close it (signal serious error)
3343 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3344 const struct GNUNET_MessageHeader *message,
3345 const struct GNUNET_ATS_Information *atsi,
3346 unsigned int atsi_count)
3348 struct GNUNET_MESH_ToOrigin *msg;
3349 struct GNUNET_PeerIdentity id;
3350 struct MeshPeerInfo *peer_info;
3351 struct MeshTunnel *t;
3354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3356 size = ntohs (message->size);
3357 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3358 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3360 GNUNET_break_op (0);
3363 msg = (struct GNUNET_MESH_ToOrigin *) message;
3364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3365 ntohs (msg[1].header.type));
3366 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3370 /* TODO notify that we dont know this tunnel (whom)? */
3371 GNUNET_break_op (0);
3375 if (t->id.oid == myid)
3378 struct GNUNET_MESH_ToOrigin *copy;
3380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3381 " it's for us! sending to clients...\n");
3382 if (NULL == t->owner)
3384 /* got data packet for ownerless tunnel */
3385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
3386 GNUNET_break_op (0);
3389 /* TODO signature verification */
3390 memcpy (cbuf, message, size);
3391 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3392 copy->tid = htonl (t->local_tid);
3393 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3394 ©->header, GNUNET_YES);
3397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3398 " not for us, retransmitting...\n");
3400 peer_info = peer_info_get (&msg->oid);
3401 if (NULL == peer_info)
3403 /* unknown origin of tunnel */
3407 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3408 send_message (message, &id);
3415 * Core handler for path ACKs
3417 * @param cls closure
3418 * @param message message
3419 * @param peer peer identity this notification is about
3420 * @param atsi performance data
3421 * @param atsi_count number of records in 'atsi'
3423 * @return GNUNET_OK to keep the connection open,
3424 * GNUNET_SYSERR to close it (signal serious error)
3427 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3428 const struct GNUNET_MessageHeader *message,
3429 const struct GNUNET_ATS_Information *atsi,
3430 unsigned int atsi_count)
3432 struct GNUNET_MESH_PathACK *msg;
3433 struct GNUNET_PeerIdentity id;
3434 struct MeshPeerInfo *peer_info;
3435 struct MeshPeerPath *p;
3436 struct MeshTunnel *t;
3438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3439 GNUNET_i2s (&my_full_id));
3440 msg = (struct GNUNET_MESH_PathACK *) message;
3441 t = tunnel_get (&msg->oid, msg->tid);
3444 /* TODO notify that we don't know the tunnel */
3448 peer_info = peer_info_get (&msg->peer_id);
3450 /* Add paths to peers? */
3451 p = tree_get_path_to_peer (t->tree, peer_info->id);
3454 path_add_to_peers (p, GNUNET_YES);
3462 /* Message for us? */
3463 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3466 if (NULL == t->owner)
3468 GNUNET_break_op (0);
3471 if (NULL != t->dht_get_type)
3473 GNUNET_DHT_get_stop (t->dht_get_type);
3474 t->dht_get_type = NULL;
3476 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3478 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3479 send_client_peer_connected (t, peer_info->id);
3484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3485 " not for us, retransmitting...\n");
3486 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3487 peer_info = peer_info_get (&msg->oid);
3488 if (NULL == peer_info)
3490 /* If we know the tunnel, we should DEFINITELY know the peer */
3494 send_message (message, &id);
3500 * Functions to handle messages from core
3502 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3503 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3504 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3505 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3506 sizeof (struct GNUNET_MESH_PathBroken)},
3507 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3508 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3509 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3510 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3511 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3512 sizeof (struct GNUNET_MESH_PathACK)},
3518 /******************************************************************************/
3519 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3520 /******************************************************************************/
3523 * deregister_app: iterator for removing each application registered by a client
3525 * @param cls closure
3526 * @param key the hash of the application id (used to access the hashmap)
3527 * @param value the value stored at the key (client)
3529 * @return GNUNET_OK on success
3532 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3534 GNUNET_break (GNUNET_YES ==
3535 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3542 * notify_client_connection_failure: notify a client that the connection to the
3543 * requested remote peer is not possible (for instance, no route found)
3544 * Function called when the socket is ready to queue more data. "buf" will be
3545 * NULL and "size" zero if the socket was closed for writing in the meantime.
3547 * @param cls closure
3548 * @param size number of bytes available in buf
3549 * @param buf where the callee should write the message
3550 * @return number of bytes written to buf
3553 notify_client_connection_failure (void *cls, size_t size, void *buf)
3556 struct MeshPeerInfo *peer_info;
3557 struct GNUNET_MESH_PeerControl *msg;
3558 struct GNUNET_PeerIdentity id;
3560 if (0 == size && NULL == buf)
3562 // TODO retry? cancel?
3566 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3567 peer_info = (struct MeshPeerInfo *) cls;
3568 msg = (struct GNUNET_MESH_PeerControl *) buf;
3569 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3570 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3571 // msg->tunnel_id = htonl(peer_info->t->tid);
3572 GNUNET_PEER_resolve (peer_info->id, &id);
3573 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3581 * Send keepalive packets for a peer
3583 * @param cls Closure (tunnel for which to send the keepalive).
3584 * @param tc Notification context.
3586 * TODO: implement explicit multicast keepalive?
3589 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3591 struct MeshTunnel *t = cls;
3592 struct GNUNET_MessageHeader *payload;
3593 struct GNUNET_MESH_Multicast *msg;
3595 sizeof (struct GNUNET_MESH_Multicast) +
3596 sizeof (struct GNUNET_MessageHeader);
3599 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3603 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3606 "sending keepalive for tunnel %d\n", t->id.tid);
3608 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3609 msg->header.size = htons (size);
3610 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3611 msg->oid = my_full_id;
3612 msg->tid = htonl (t->id.tid);
3613 msg->ttl = htonl (DEFAULT_TTL);
3614 msg->mid = htonl (t->mid + 1);
3616 payload = (struct GNUNET_MessageHeader *) &msg[1];
3617 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3618 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3619 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
3621 t->path_refresh_task =
3622 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3628 * Function to process paths received for a new peer addition. The recorded
3629 * paths form the initial tunnel, which can be optimized later.
3630 * Called on each result obtained for the DHT search.
3632 * @param cls closure
3633 * @param exp when will this value expire
3634 * @param key key of the result
3635 * @param get_path path of the get request
3636 * @param get_path_length lenght of get_path
3637 * @param put_path path of the put request
3638 * @param put_path_length length of the put_path
3639 * @param type type of the result
3640 * @param size number of bytes in data
3641 * @param data pointer to the result data
3643 * TODO: re-issue the request after certain time? cancel after X results?
3646 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3647 const GNUNET_HashCode * key,
3648 const struct GNUNET_PeerIdentity *get_path,
3649 unsigned int get_path_length,
3650 const struct GNUNET_PeerIdentity *put_path,
3651 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3652 size_t size, const void *data)
3654 struct MeshPathInfo *path_info = cls;
3655 struct MeshPeerPath *p;
3656 struct GNUNET_PeerIdentity pi;
3659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3660 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3663 p = path_build_from_dht (get_path, get_path_length, put_path,
3665 path_add_to_peers (p, GNUNET_NO);
3667 for (i = 0; i < path_info->peer->ntunnels; i++)
3669 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3670 peer_info_connect (path_info->peer, path_info->t);
3678 * Function to process paths received for a new peer addition. The recorded
3679 * paths form the initial tunnel, which can be optimized later.
3680 * Called on each result obtained for the DHT search.
3682 * @param cls closure
3683 * @param exp when will this value expire
3684 * @param key key of the result
3685 * @param get_path path of the get request
3686 * @param get_path_length lenght of get_path
3687 * @param put_path path of the put request
3688 * @param put_path_length length of the put_path
3689 * @param type type of the result
3690 * @param size number of bytes in data
3691 * @param data pointer to the result data
3694 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3695 const GNUNET_HashCode * key,
3696 const struct GNUNET_PeerIdentity *get_path,
3697 unsigned int get_path_length,
3698 const struct GNUNET_PeerIdentity *put_path,
3699 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3700 size_t size, const void *data)
3702 const struct GNUNET_PeerIdentity *pi = data;
3703 struct MeshTunnel *t = cls;
3704 struct MeshPeerInfo *peer_info;
3705 struct MeshPeerPath *p;
3707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
3708 if (size != sizeof (struct GNUNET_PeerIdentity))
3710 GNUNET_break_op (0);
3713 GNUNET_assert (NULL != t->owner);
3714 peer_info = peer_info_get (pi);
3715 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3717 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3719 p = path_build_from_dht (get_path, get_path_length, put_path,
3721 path_add_to_peers (p, GNUNET_NO);
3723 tunnel_add_peer (t, peer_info);
3724 peer_info_connect (peer_info, t);
3728 /******************************************************************************/
3729 /********************* MESH LOCAL HANDLES **************************/
3730 /******************************************************************************/
3734 * Handler for client disconnection
3736 * @param cls closure
3737 * @param client identification of the client; NULL
3738 * for the last call when the server is destroyed
3741 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3743 struct MeshClient *c;
3744 struct MeshClient *next;
3746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3755 if (c->handle != client)
3757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3763 GNUNET_SERVER_client_drop (c->handle);
3764 c->shutting_down = GNUNET_YES;
3765 GNUNET_assert (NULL != c->own_tunnels);
3766 GNUNET_assert (NULL != c->incoming_tunnels);
3767 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3768 &tunnel_destroy_iterator, c);
3769 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3770 &tunnel_destroy_iterator, c);
3771 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
3772 &tunnel_destroy_iterator, c);
3773 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3774 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3775 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
3777 /* deregister clients applications */
3778 if (NULL != c->apps)
3780 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3781 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3783 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3784 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3786 GNUNET_SCHEDULER_cancel (announce_applications_task);
3787 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3789 if (NULL != c->types)
3790 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3792 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
3803 * Handler for new clients
3805 * @param cls closure
3806 * @param client identification of the client
3807 * @param message the actual message, which includes messages the client wants
3810 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3811 const struct GNUNET_MessageHeader *message)
3813 struct GNUNET_MESH_ClientConnect *cc_msg;
3814 struct MeshClient *c;
3815 GNUNET_MESH_ApplicationType *a;
3822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3823 /* Check data sanity */
3824 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3825 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3826 ntypes = ntohs (cc_msg->types);
3827 napps = ntohs (cc_msg->applications);
3829 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3832 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3836 /* Create new client structure */
3837 c = GNUNET_malloc (sizeof (struct MeshClient));
3838 c->id = next_client_id++;
3839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
3841 GNUNET_SERVER_client_keep (client);
3842 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3845 GNUNET_MESH_ApplicationType at;
3848 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3849 for (i = 0; i < napps; i++)
3852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
3853 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3854 /* store in clients hashmap */
3855 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3856 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3857 /* store in global hashmap, for announcements */
3858 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3859 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3861 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3862 announce_applications_task =
3863 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3871 t = (uint16_t *) & a[napps];
3872 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3873 for (i = 0; i < ntypes; i++)
3876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
3877 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3879 /* store in clients hashmap */
3880 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3881 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3882 /* store in global hashmap */
3883 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3884 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3888 " client has %u+%u subscriptions\n", napps, ntypes);
3890 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3891 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3892 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3893 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3894 GNUNET_SERVER_notification_context_add (nc, client);
3896 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3902 * Handler for requests of new tunnels
3904 * @param cls closure
3905 * @param client identification of the client
3906 * @param message the actual message
3909 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3910 const struct GNUNET_MessageHeader *message)
3912 struct GNUNET_MESH_TunnelMessage *t_msg;
3913 struct MeshTunnel *t;
3914 struct MeshClient *c;
3915 GNUNET_HashCode hash;
3917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3919 /* Sanity check for client registration */
3920 if (NULL == (c = client_get (client)))
3923 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3928 /* Message sanity check */
3929 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3932 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3936 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3937 /* Sanity check for tunnel numbering */
3938 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3941 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3944 /* Sanity check for duplicate tunnel IDs */
3945 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3948 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3952 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3953 while (NULL != tunnel_get_by_pi (myid, next_tid))
3954 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3955 t->id.tid = next_tid++;
3956 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3958 t->local_tid = ntohl (t_msg->tunnel_id);
3959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
3960 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3962 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3964 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3966 GNUNET_CONTAINER_multihashmap_put (c->own_tunnels, &hash, t,
3967 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3970 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3974 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3976 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3977 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3980 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3983 t->tree = tree_new (myid);
3985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
3986 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3992 * Handler for requests of deleting tunnels
3994 * @param cls closure
3995 * @param client identification of the client
3996 * @param message the actual message
3999 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4000 const struct GNUNET_MessageHeader *message)
4002 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4003 struct MeshClient *c;
4004 struct MeshTunnel *t;
4005 MESH_TunnelNumber tid;
4007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4008 "Got a DESTROY TUNNEL from client!\n");
4010 /* Sanity check for client registration */
4011 if (NULL == (c = client_get (client)))
4014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4017 /* Message sanity check */
4018 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4021 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4025 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4027 /* Retrieve tunnel */
4028 tid = ntohl (tunnel_msg->tunnel_id);
4029 t = tunnel_get_by_local_id(c, tid);
4033 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4034 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4037 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4039 client_ignore_tunnel (c, t);
4041 // TODO: when to destroy incoming tunnel?
4042 if (t->nclients == 0)
4044 GNUNET_assert (GNUNET_YES ==
4045 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
4047 GNUNET_assert (GNUNET_YES ==
4048 GNUNET_CONTAINER_multihashmap_remove (t->peers,
4049 &my_full_id.hashPubKey,
4053 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4056 send_client_tunnel_disconnect(t, c);
4057 client_delete_tunnel(c, t);
4059 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4061 tunnel_send_destroy (t);
4063 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4069 * Handler for connection requests to new peers
4071 * @param cls closure
4072 * @param client identification of the client
4073 * @param message the actual message (PeerControl)
4076 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
4077 const struct GNUNET_MessageHeader *message)
4079 struct GNUNET_MESH_PeerControl *peer_msg;
4080 struct MeshPeerInfo *peer_info;
4081 struct MeshClient *c;
4082 struct MeshTunnel *t;
4083 MESH_TunnelNumber tid;
4085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
4086 /* Sanity check for client registration */
4087 if (NULL == (c = client_get (client)))
4090 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4094 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4095 /* Sanity check for message size */
4096 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4099 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4103 /* Tunnel exists? */
4104 tid = ntohl (peer_msg->tunnel_id);
4105 t = tunnel_get_by_local_id (c, tid);
4109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4113 /* Does client own tunnel? */
4114 if (t->owner->handle != client)
4117 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
4121 GNUNET_i2s (&peer_msg->peer));
4122 peer_info = peer_info_get (&peer_msg->peer);
4124 tunnel_add_peer (t, peer_info);
4125 peer_info_connect (peer_info, t);
4127 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4133 * Handler for disconnection requests of peers in a tunnel
4135 * @param cls closure
4136 * @param client identification of the client
4137 * @param message the actual message (PeerControl)
4140 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
4141 const struct GNUNET_MessageHeader *message)
4143 struct GNUNET_MESH_PeerControl *peer_msg;
4144 struct MeshPeerInfo *peer_info;
4145 struct MeshClient *c;
4146 struct MeshTunnel *t;
4147 MESH_TunnelNumber tid;
4149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
4150 /* Sanity check for client registration */
4151 if (NULL == (c = client_get (client)))
4154 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4157 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4158 /* Sanity check for message size */
4159 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4162 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4166 /* Tunnel exists? */
4167 tid = ntohl (peer_msg->tunnel_id);
4168 t = tunnel_get_by_local_id (c, tid);
4172 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4177 /* Does client own tunnel? */
4178 if (t->owner->handle != client)
4181 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
4186 GNUNET_i2s (&peer_msg->peer));
4187 /* Is the peer in the tunnel? */
4189 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
4190 if (NULL == peer_info)
4193 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4197 /* Ok, delete peer from tunnel */
4198 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
4199 &peer_msg->peer.hashPubKey);
4201 send_destroy_path (t, peer_info->id);
4202 tunnel_delete_peer (t, peer_info->id);
4203 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4209 * Handler for connection requests to new peers by type
4211 * @param cls closure
4212 * @param client identification of the client
4213 * @param message the actual message (ConnectPeerByType)
4216 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
4217 const struct GNUNET_MessageHeader *message)
4219 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
4220 struct MeshClient *c;
4221 struct MeshTunnel *t;
4222 GNUNET_HashCode hash;
4223 MESH_TunnelNumber tid;
4225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
4226 /* Sanity check for client registration */
4227 if (NULL == (c = client_get (client)))
4230 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4234 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
4235 /* Sanity check for message size */
4236 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
4237 ntohs (connect_msg->header.size))
4240 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4244 /* Tunnel exists? */
4245 tid = ntohl (connect_msg->tunnel_id);
4246 t = tunnel_get_by_local_id (c, tid);
4250 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4254 /* Does client own tunnel? */
4255 if (t->owner->handle != client)
4258 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4262 /* Do WE have the service? */
4263 t->type = ntohl (connect_msg->type);
4264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
4265 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
4266 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
4269 /* Yes! Fast forward, add ourselves to the tunnel and send the
4270 * good news to the client, and alert the destination client of
4271 * an incoming tunnel.
4273 * FIXME send a path create to self, avoid code duplication
4275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
4276 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
4277 peer_info_get (&my_full_id),
4278 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
4281 send_client_peer_connected (t, myid);
4282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
4283 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4285 t->local_tid_dest = next_local_tid++;
4286 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4287 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4288 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4292 /* Ok, lets find a peer offering the service */
4293 if (NULL != t->dht_get_type)
4295 GNUNET_DHT_get_stop (t->dht_get_type);
4297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
4298 GNUNET_h2s (&hash));
4300 GNUNET_DHT_get_start (dht_handle,
4301 GNUNET_BLOCK_TYPE_TEST, &hash, 10,
4302 GNUNET_DHT_RO_RECORD_ROUTE |
4303 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
4304 &dht_get_type_handler, t);
4306 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4312 * Handler for client traffic directed to one peer
4314 * @param cls closure
4315 * @param client identification of the client
4316 * @param message the actual message
4319 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4320 const struct GNUNET_MessageHeader *message)
4322 struct MeshClient *c;
4323 struct MeshTunnel *t;
4324 struct MeshPeerInfo *pi;
4325 struct GNUNET_MESH_Unicast *data_msg;
4326 MESH_TunnelNumber tid;
4329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4330 "Got a unicast request from a client!\n");
4332 /* Sanity check for client registration */
4333 if (NULL == (c = client_get (client)))
4336 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4339 data_msg = (struct GNUNET_MESH_Unicast *) message;
4340 /* Sanity check for message size */
4341 size = ntohs (message->size);
4342 if (sizeof (struct GNUNET_MESH_Unicast) +
4343 sizeof (struct GNUNET_MessageHeader) > size)
4346 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4350 /* Tunnel exists? */
4351 tid = ntohl (data_msg->tid);
4352 t = tunnel_get_by_local_id (c, tid);
4356 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4360 /* Is it a local tunnel? Then, does client own the tunnel? */
4361 if (t->owner->handle != client)
4364 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4368 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
4369 &data_msg->destination.hashPubKey);
4370 /* Is the selected peer in the tunnel? */
4374 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4378 /* Ok, everything is correct, send the message
4379 * (pretend we got it from a mesh peer)
4382 char buf[ntohs (message->size)] GNUNET_ALIGN;
4383 struct GNUNET_MESH_Unicast *copy;
4385 /* Work around const limitation */
4386 copy = (struct GNUNET_MESH_Unicast *) buf;
4387 memcpy (buf, data_msg, size);
4388 copy->oid = my_full_id;
4389 copy->tid = htonl (t->id.tid);
4390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4391 " calling generic handler...\n");
4392 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4394 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4400 * Handler for client traffic directed to the origin
4402 * @param cls closure
4403 * @param client identification of the client
4404 * @param message the actual message
4407 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4408 const struct GNUNET_MessageHeader *message)
4410 struct GNUNET_MESH_ToOrigin *data_msg;
4411 struct GNUNET_PeerIdentity id;
4412 struct MeshClient *c;
4413 struct MeshTunnel *t;
4414 MESH_TunnelNumber tid;
4417 /* Sanity check for client registration */
4418 if (NULL == (c = client_get (client)))
4421 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4424 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4425 /* Sanity check for message size */
4426 size = ntohs (message->size);
4427 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4428 sizeof (struct GNUNET_MessageHeader) > size)
4431 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4435 /* Tunnel exists? */
4436 tid = ntohl (data_msg->tid);
4437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4438 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
4439 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4445 t = tunnel_get_by_local_id (c, tid);
4449 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4453 /* It should be sent by someone who has this as incoming tunnel. */
4454 if (-1 == client_knows_tunnel (c, t))
4457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4460 GNUNET_PEER_resolve (t->id.oid, &id);
4462 /* Ok, everything is correct, send the message
4463 * (pretend we got it from a mesh peer)
4466 char buf[ntohs (message->size)] GNUNET_ALIGN;
4467 struct GNUNET_MESH_ToOrigin *copy;
4469 /* Work around const limitation */
4470 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4471 memcpy (buf, data_msg, size);
4473 copy->tid = htonl (t->id.tid);
4474 copy->sender = my_full_id;
4475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4476 " calling generic handler...\n");
4477 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4479 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4485 * Handler for client traffic directed to all peers in a tunnel
4487 * @param cls closure
4488 * @param client identification of the client
4489 * @param message the actual message
4492 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4493 const struct GNUNET_MessageHeader *message)
4495 struct MeshClient *c;
4496 struct MeshTunnel *t;
4497 struct GNUNET_MESH_Multicast *data_msg;
4498 MESH_TunnelNumber tid;
4500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4501 "Got a multicast request from a client!\n");
4503 /* Sanity check for client registration */
4504 if (NULL == (c = client_get (client)))
4507 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4510 data_msg = (struct GNUNET_MESH_Multicast *) message;
4511 /* Sanity check for message size */
4512 if (sizeof (struct GNUNET_MESH_Multicast) +
4513 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4516 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4520 /* Tunnel exists? */
4521 tid = ntohl (data_msg->tid);
4522 t = tunnel_get_by_local_id (c, tid);
4526 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4530 /* Does client own tunnel? */
4531 if (t->owner->handle != client)
4534 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4539 char buf[ntohs (message->size)] GNUNET_ALIGN;
4540 struct GNUNET_MESH_Multicast *copy;
4542 copy = (struct GNUNET_MESH_Multicast *) buf;
4543 memcpy (buf, message, ntohs (message->size));
4544 copy->oid = my_full_id;
4545 copy->tid = htonl (t->id.tid);
4546 copy->ttl = htonl (DEFAULT_TTL);
4547 copy->mid = htonl (t->mid + 1);
4548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4549 " calling generic handler...\n");
4550 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4553 /* receive done gets called when last copy is sent to a neighbor */
4558 * Functions to handle messages from clients
4560 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4561 {&handle_local_new_client, NULL,
4562 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4563 {&handle_local_tunnel_create, NULL,
4564 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4565 sizeof (struct GNUNET_MESH_TunnelMessage)},
4566 {&handle_local_tunnel_destroy, NULL,
4567 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4568 sizeof (struct GNUNET_MESH_TunnelMessage)},
4569 {&handle_local_connect_add, NULL,
4570 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4571 sizeof (struct GNUNET_MESH_PeerControl)},
4572 {&handle_local_connect_del, NULL,
4573 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4574 sizeof (struct GNUNET_MESH_PeerControl)},
4575 {&handle_local_connect_by_type, NULL,
4576 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4577 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4578 {&handle_local_unicast, NULL,
4579 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4580 {&handle_local_to_origin, NULL,
4581 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4582 {&handle_local_multicast, NULL,
4583 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4589 * To be called on core init/fail.
4591 * @param cls service closure
4592 * @param server handle to the server for this service
4593 * @param identity the public identity of this peer
4596 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4597 const struct GNUNET_PeerIdentity *identity)
4599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4600 core_handle = server;
4601 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4604 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4605 GNUNET_SCHEDULER_shutdown ();
4611 * Method called whenever a given peer connects.
4613 * @param cls closure
4614 * @param peer peer identity this notification is about
4615 * @param atsi performance data for the connection
4616 * @param atsi_count number of records in 'atsi'
4619 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4620 const struct GNUNET_ATS_Information *atsi,
4621 unsigned int atsi_count)
4623 struct MeshPeerInfo *peer_info;
4624 struct MeshPeerPath *path;
4626 DEBUG_CONN ("Peer connected\n");
4627 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4628 peer_info = peer_info_get (peer);
4629 if (myid == peer_info->id)
4631 DEBUG_CONN (" (self)\n");
4636 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4638 path = path_new (2);
4639 path->peers[0] = myid;
4640 path->peers[1] = peer_info->id;
4641 GNUNET_PEER_change_rc (myid, 1);
4642 GNUNET_PEER_change_rc (peer_info->id, 1);
4643 peer_info_add_path (peer_info, path, GNUNET_YES);
4648 * Method called whenever a peer disconnects.
4650 * @param cls closure
4651 * @param peer peer identity this notification is about
4654 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4656 struct MeshPeerInfo *pi;
4659 DEBUG_CONN ("Peer disconnected\n");
4660 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4666 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4668 /* TODO: notify that the transmission failed */
4669 // FIXME CORE_QUEUE_SIZE
4671 peer_info_remove_path (pi, pi->id, myid);
4674 DEBUG_CONN (" (self)\n");
4680 /******************************************************************************/
4681 /************************ MAIN FUNCTIONS ****************************/
4682 /******************************************************************************/
4685 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4687 * @param cls closure
4688 * @param key current key code
4689 * @param value value in the hash map
4690 * @return GNUNET_YES if we should continue to iterate,
4694 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4696 struct MeshTunnel *t = value;
4703 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4705 * @param cls closure
4706 * @param key current key code
4707 * @param value value in the hash map
4708 * @return GNUNET_YES if we should continue to iterate,
4712 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4714 struct MeshPeerInfo *p = value;
4716 peer_info_destroy (p);
4721 * Task run during shutdown.
4727 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4731 if (core_handle != NULL)
4733 GNUNET_CORE_disconnect (core_handle);
4736 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4737 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4738 if (dht_handle != NULL)
4740 GNUNET_DHT_disconnect (dht_handle);
4745 GNUNET_SERVER_notification_context_destroy (nc);
4748 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4750 GNUNET_SCHEDULER_cancel (announce_id_task);
4751 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4757 * Process mesh requests.
4759 * @param cls closure
4760 * @param server the initialized server
4761 * @param c configuration to use
4764 run (void *cls, struct GNUNET_SERVER_Handle *server,
4765 const struct GNUNET_CONFIGURATION_Handle *c)
4767 struct MeshPeerInfo *peer;
4768 struct MeshPeerPath *p;
4771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4772 server_handle = server;
4773 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4775 NULL, /* Closure passed to MESH functions */
4776 &core_init, /* Call core_init once connected */
4777 &core_connect, /* Handle connects */
4778 &core_disconnect, /* remove peers on disconnects */
4779 NULL, /* Don't notify about all incoming messages */
4780 GNUNET_NO, /* For header only in notification */
4781 NULL, /* Don't notify about all outbound messages */
4782 GNUNET_NO, /* For header-only out notification */
4783 core_handlers); /* Register these handlers */
4785 if (core_handle == NULL)
4788 GNUNET_SCHEDULER_shutdown ();
4793 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4796 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4798 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4799 GNUNET_SCHEDULER_shutdown ();
4802 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4803 GNUNET_free (keyfile);
4804 if (my_private_key == NULL)
4806 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4807 _("Mesh service could not access hostkey. Exiting.\n"));
4808 GNUNET_SCHEDULER_shutdown ();
4811 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4812 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4813 &my_full_id.hashPubKey);
4814 myid = GNUNET_PEER_intern (&my_full_id);
4816 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4823 dht_handle = GNUNET_DHT_connect (c, 64);
4824 if (dht_handle == NULL)
4830 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4832 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4833 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4834 peers = GNUNET_CONTAINER_multihashmap_create (32);
4835 applications = GNUNET_CONTAINER_multihashmap_create (32);
4836 types = GNUNET_CONTAINER_multihashmap_create (32);
4838 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4839 nc = GNUNET_SERVER_notification_context_create (server_handle,
4841 GNUNET_SERVER_disconnect_notify (server_handle,
4842 &handle_local_client_disconnect, NULL);
4846 clients_tail = NULL;
4849 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4850 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4852 /* Create a peer_info for the local peer */
4853 peer = peer_info_get (&my_full_id);
4856 GNUNET_PEER_change_rc (myid, 1);
4857 peer_info_add_path (peer, p, GNUNET_YES);
4859 /* Scheduled the task to clean up when shutdown is called */
4860 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
4867 * The main function for the mesh service.
4869 * @param argc number of arguments from the command line
4870 * @param argv command line arguments
4871 * @return 0 ok, 1 on error
4874 main (int argc, char *const *argv)
4878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
4881 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");