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 all info possibly needed to build a package when called
123 struct MeshTransmissionDescriptor
125 /** ID of the tunnel this packet travels in */
126 struct MESH_TunnelID *origin;
128 /** Who was this message being sent to */
129 struct MeshPeerInfo *peer;
131 /** Ultimate destination of the packet */
132 GNUNET_PEER_Id destination;
134 /** Which handler was used to request the transmission */
135 unsigned int handler_n;
137 /** Data descriptor */
138 struct MeshData* mesh_data;
143 * Struct containing all information regarding a given peer
153 * Last time we heard from this peer
155 struct GNUNET_TIME_Absolute last_contact;
158 * Number of attempts to reconnect so far
160 int n_reconnect_attempts;
163 * Paths to reach the peer, ordered by ascending hop count
165 struct MeshPeerPath *path_head;
168 * Paths to reach the peer, ordered by ascending hop count
170 struct MeshPeerPath *path_tail;
173 * Handle to stop the DHT search for a path to this peer
175 struct GNUNET_DHT_GetHandle *dhtget;
178 * Closure given to the DHT GET
180 struct MeshPathInfo *dhtgetcls;
183 * Handles to stop queued transmissions for this peer
185 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
188 * Pointer to info stuctures used as cls for queued transmissions
190 void *infos[CORE_QUEUE_SIZE];
193 * Type of message being in each transmission
195 uint16_t types[CORE_QUEUE_SIZE];
198 * Array of tunnels this peer participates in
199 * (most probably a small amount, therefore not a hashmap)
200 * When the path to the peer changes, notify these tunnels to let them
201 * re-adjust their path trees.
203 struct MeshTunnel **tunnels;
206 * Number of tunnels this peers participates in
208 unsigned int ntunnels;
213 * Data scheduled to transmit (to local client or remote peer)
220 struct MeshQueue *next;
221 struct MeshQueue *prev;
224 * Target of the data (NULL if target is client)
226 struct MeshPeerInfo *peer;
229 * Client to send the data to (NULL if target is peer)
231 struct MeshClient *client;
234 * Size of the message to transmit
239 * How old is the data?
241 struct GNUNET_TIME_Absolute timestamp;
246 struct GNUNET_MessageHeader *data;
250 * Globally unique tunnel identification (owner + number)
251 * DO NOT USE OVER THE NETWORK
256 * Node that owns the tunnel
261 * Tunnel number to differentiate all the tunnels owned by the node oid
262 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
264 MESH_TunnelNumber tid;
268 struct MeshClient; /* FWD declaration */
271 * Struct containing all information regarding a tunnel
272 * For an intermediate node the improtant info used will be:
273 * - id Tunnel unique identification
274 * - paths[0] To know where to send it next
275 * - metainfo: ready, speeds, accounting
282 struct MESH_TunnelID id;
285 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
287 MESH_TunnelNumber local_tid;
290 * Local tunnel number for local destination clients (incoming number)
291 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
294 MESH_TunnelNumber local_tid_dest;
297 * ID of the last multicast packet seen/sent.
302 * Last time the tunnel was used
304 struct GNUNET_TIME_Absolute timestamp;
307 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
308 * containing peers added by id or by type, not intermediate peers.
310 struct GNUNET_CONTAINER_MultiHashMap *peers;
313 * Number of peers that are connected and potentially ready to receive data
315 unsigned int peers_ready;
318 * Number of peers that have been added to the tunnel
320 unsigned int peers_total;
323 * Client owner of the tunnel, if any
325 struct MeshClient *owner;
328 * Clients that have been informed about the tunnel, if any
330 struct MeshClient **clients;
333 * Number of elements in clients
335 unsigned int nclients;
338 * Clients that have requested to leave the tunnel
340 struct MeshClient **ignore;
343 * Number of elements in clients
345 unsigned int nignore;
348 * Messages ready to transmit
350 struct MeshQueue *queue_head;
351 struct MeshQueue *queue_tail;
356 struct MeshTunnelTree *tree;
359 * Application type we are looking for in this tunnel
361 GNUNET_MESH_ApplicationType type;
364 * Used to search peers offering a service
366 struct GNUNET_DHT_GetHandle *dht_get_type;
369 * Task to keep the used paths alive
371 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
374 * Task to destroy the tunnel after timeout
376 * FIXME: merge the two? a tunnel will have either
377 * a path refresh OR a timeout, never both!
379 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
384 * Info needed to work with tunnel paths and peers
391 struct MeshTunnel *t;
394 * Neighbouring peer to whom we send the packet to
396 struct MeshPeerInfo *peer;
401 struct MeshPeerPath *path;
404 * Position in peer's transmit queue
411 * Struct containing information about a client of the service
418 struct MeshClient *next;
423 struct MeshClient *prev;
426 * Tunnels that belong to this client, indexed by local id
428 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
431 * Tunnels this client has accepted, indexed by incoming local id
433 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
436 * Tunnels this client has rejected, indexed by incoming local id
438 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
440 * Handle to communicate with the client
442 struct GNUNET_SERVER_Client *handle;
445 * Applications that this client has claimed to provide
447 struct GNUNET_CONTAINER_MultiHashMap *apps;
450 * Messages that this client has declared interest in
452 struct GNUNET_CONTAINER_MultiHashMap *types;
455 * Whether the client is active or shutting down (don't send confirmations
456 * to a client that is shutting down.
461 * ID of the client, mainly for debug messages
469 /******************************************************************************/
470 /************************ DEBUG FUNCTIONS ****************************/
471 /******************************************************************************/
475 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
476 * @param cls string to print
477 * @param success GNUNET_OK if the PUT was transmitted,
478 * GNUNET_NO on timeout,
479 * GNUNET_SYSERR on disconnect from service
480 * after the PUT message was transmitted
481 * (so we don't know if it was received or not)
486 mesh_debug (void *cls, int success)
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
496 /******************************************************************************/
497 /*********************** GLOBAL VARIABLES ****************************/
498 /******************************************************************************/
503 static struct MeshClient *clients;
504 static struct MeshClient *clients_tail;
507 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
509 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
512 * Tunnels incoming, indexed by MESH_TunnelNumber
513 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
515 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
518 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
520 static struct GNUNET_CONTAINER_MultiHashMap *peers;
523 * Handle to communicate with core
525 static struct GNUNET_CORE_Handle *core_handle;
528 * Handle to communicate with transport
530 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
535 static struct GNUNET_DHT_Handle *dht_handle;
540 static struct GNUNET_SERVER_Handle *server_handle;
543 * Notification context, to send messages to local clients
545 static struct GNUNET_SERVER_NotificationContext *nc;
548 * Local peer own ID (memory efficient handle)
550 static GNUNET_PEER_Id myid;
553 * Local peer own ID (full value)
555 static struct GNUNET_PeerIdentity my_full_id;
560 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
565 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
568 * Tunnel ID for the next created tunnel (global tunnel number)
570 static MESH_TunnelNumber next_tid;
573 * Tunnel ID for the next incoming tunnel (local tunnel number)
575 static MESH_TunnelNumber next_local_tid;
578 * All application types provided by this peer
580 static struct GNUNET_CONTAINER_MultiHashMap *applications;
583 * All message types clients of this peer are interested in
585 static struct GNUNET_CONTAINER_MultiHashMap *types;
588 * Task to periodically announce provided applications
590 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
593 * Task to periodically announce itself in the network
595 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
598 * Next ID to assign to a client
600 unsigned int next_client_id;
604 /******************************************************************************/
605 /************************ ITERATORS ****************************/
606 /******************************************************************************/
608 /* FIXME move iterators here */
611 /******************************************************************************/
612 /************************ PERIODIC FUNCTIONS ****************************/
613 /******************************************************************************/
616 * Announce iterator over for each application provided by the peer
619 * @param key current key code
620 * @param value value in the hash map
621 * @return GNUNET_YES if we should continue to
626 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
628 /* FIXME are hashes in multihash map equal on all aquitectures? */
629 /* FIXME: keep return value of 'put' to possibly cancel!? */
630 GNUNET_DHT_put (dht_handle, key, 10,
631 GNUNET_DHT_RO_RECORD_ROUTE |
632 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
633 sizeof (struct GNUNET_PeerIdentity),
634 (const char *) &my_full_id,
635 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
637 APP_ANNOUNCE_TIME, NULL, NULL);
643 * Periodically announce what applications are provided by local clients
646 * @param tc task context
649 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
651 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
653 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
657 DEBUG_DHT ("Starting PUT for apps\n");
659 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
661 announce_applications_task =
662 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
664 DEBUG_DHT ("Finished PUT for apps\n");
671 * Periodically announce self id in the DHT
674 * @param tc task context
677 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
679 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
681 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
685 * - Set data expiration in function of X
688 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
690 GNUNET_DHT_put (dht_handle, /* DHT handle */
691 &my_full_id.hashPubKey, /* Key to use */
692 10, /* Replication level */
693 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
694 GNUNET_BLOCK_TYPE_TEST, /* Block type */
695 sizeof (my_full_id), /* Size of the data */
696 (char *) &my_full_id, /* Data itself */
697 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
698 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
699 NULL, /* Continuation */
700 NULL); /* Continuation closure */
702 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
707 * Function to process paths received for a new peer addition. The recorded
708 * paths form the initial tunnel, which can be optimized later.
709 * Called on each result obtained for the DHT search.
712 * @param exp when will this value expire
713 * @param key key of the result
714 * @param type type of the result
715 * @param size number of bytes in data
716 * @param data pointer to the result data
719 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
720 const GNUNET_HashCode * key,
721 const struct GNUNET_PeerIdentity *get_path,
722 unsigned int get_path_length,
723 const struct GNUNET_PeerIdentity *put_path,
724 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
725 size_t size, const void *data);
728 /******************************************************************************/
729 /****************** GENERAL HELPER FUNCTIONS ************************/
730 /******************************************************************************/
733 * Search for a tunnel by global ID using full PeerIdentities
735 * @param oid owner of the tunnel
736 * @param tid global tunnel number
738 * @return tunnel handler, NULL if doesn't exist
740 static struct MeshTunnel *
741 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
745 * Delete an active client from the tunnel.
751 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
754 * Notify a tunnel that a connection has broken that affects at least
757 * @param t Tunnel affected.
758 * @param p1 Peer that got disconnected from p2.
759 * @param p2 Peer that got disconnected from p1.
761 * @return Short ID of the peer disconnected (either p1 or p2).
762 * 0 if the tunnel remained unaffected.
764 static GNUNET_PEER_Id
765 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
770 * Check if client has registered with the service and has not disconnected
772 * @param client the client to check
774 * @return non-NULL if client exists in the global DLL
776 static struct MeshClient *
777 client_get (struct GNUNET_SERVER_Client *client)
779 struct MeshClient *c;
784 if (c->handle == client)
793 * Checks if a given client has subscribed to certain message type
795 * @param message_type Type of message to check
796 * @param c Client to check
798 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
803 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
807 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
808 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
813 * Allow a client to send more data after transmitting a multicast message
814 * which some neighbor has not yet accepted altough a reasonable time has
817 * @param cls Closure (DataDescriptor containing the task identifier)
818 * @param tc Task Context
820 * FIXME reference counter cshould be just int
823 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
825 struct MeshData *mdata = cls;
827 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
829 GNUNET_assert (NULL != mdata->reference_counter);
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
831 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
832 *(mdata->reference_counter));
833 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
834 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
839 * Check whether client wants traffic from a tunnel.
841 * @param c Client to check.
842 * @param t Tunnel to be found.
844 * @return GNUNET_YES if client knows tunnel.
846 * TODO look in client hashmap
849 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
853 for (i = 0; i < t->nclients; i++)
854 if (t->clients[i] == c)
861 * Check whether client has been informed about a tunnel.
863 * @param c Client to check.
864 * @param t Tunnel to be found.
866 * @return GNUNET_YES if client knows tunnel.
868 * TODO look in client hashmap
871 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
875 for (i = 0; i < t->nignore; i++)
876 if (t->ignore[i] == c)
878 return client_wants_tunnel(c, t);
883 * Marks a client as uninterested in traffic from the tunnel, updating both
884 * client and tunnel to reflect this.
886 * @param c Client that doesn't want traffic anymore.
887 * @param t Tunnel which should be ignored.
889 * FIXME when to delete an incoming tunnel?
892 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
894 GNUNET_HashCode hash;
896 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
897 GNUNET_break (GNUNET_YES ==
898 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
900 GNUNET_break (GNUNET_YES ==
901 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
902 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
903 tunnel_delete_active_client (t, c);
904 GNUNET_array_append (t->ignore, t->nignore, c);
909 * Deletes a tunnel from a client (either owner or destination). To be used on
910 * tunnel destroy, otherwise, use client_ignore_tunnel.
912 * @param c Client whose tunnel to delete.
913 * @param t Tunnel which should be deleted.
916 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
918 GNUNET_HashCode hash;
922 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
923 GNUNET_assert (GNUNET_YES ==
924 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
930 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
932 GNUNET_assert (GNUNET_YES ==
933 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
937 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
946 * Send the message to all clients that have subscribed to its type
948 * @param msg Pointer to the message itself
949 * @param payload Pointer to the payload of the message.
950 * @return number of clients this message was sent to
953 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
954 const struct GNUNET_MessageHeader *payload)
956 struct GNUNET_PeerIdentity *oid;
957 struct MeshClient *c;
958 struct MeshTunnel *t;
959 MESH_TunnelNumber *tid;
962 char cbuf[htons (msg->size)];
964 type = ntohs (payload->type);
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
968 memcpy (cbuf, msg, sizeof (cbuf));
969 switch (htons (msg->type))
971 struct GNUNET_MESH_Unicast *uc;
972 struct GNUNET_MESH_Multicast *mc;
973 struct GNUNET_MESH_ToOrigin *to;
975 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
976 uc = (struct GNUNET_MESH_Unicast *) cbuf;
980 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
981 mc = (struct GNUNET_MESH_Multicast *) cbuf;
985 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
986 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
994 t = tunnel_get (oid, ntohl (*tid));
1001 for (count = 0, c = clients; c != NULL; c = c->next)
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1004 if (client_is_subscribed (type, c))
1006 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1010 *tid = htonl (t->local_tid);
1014 if (GNUNET_NO == client_knows_tunnel (c, t))
1016 /* This client doesn't know the tunnel */
1017 struct GNUNET_MESH_TunnelNotification tmsg;
1018 GNUNET_HashCode hash;
1020 tmsg.header.size = htons (sizeof (tmsg));
1021 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1022 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1023 tmsg.tunnel_id = htonl (t->local_tid_dest);
1024 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1025 &tmsg.header, GNUNET_NO);
1026 GNUNET_array_append (t->clients, t->nclients, c);
1027 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1029 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1030 c->incoming_tunnels, &hash, t,
1031 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1033 *tid = htonl (t->local_tid_dest);
1036 /* Check if the client wants to get traffic from the tunnel */
1037 if (GNUNET_NO == client_wants_tunnel(c, t))
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1041 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1042 (struct GNUNET_MessageHeader
1043 *) cbuf, GNUNET_YES);
1051 * Notify the client that owns the tunnel that a peer has connected to it
1052 * (the requested path to it has been confirmed).
1054 * @param t Tunnel whose owner to notify
1055 * @param id Short id of the peer that has connected
1058 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1060 struct GNUNET_MESH_PeerControl pc;
1062 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1063 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1064 pc.tunnel_id = htonl (t->local_tid);
1065 GNUNET_PEER_resolve (id, &pc.peer);
1066 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1072 * Notify all clients (not depending on registration status) that the incoming
1073 * tunnel is no longer valid.
1075 * @param t Tunnel that was destroyed.
1078 send_clients_tunnel_destroy (struct MeshTunnel *t)
1080 struct GNUNET_MESH_TunnelMessage msg;
1082 msg.header.size = htons (sizeof (msg));
1083 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1084 msg.tunnel_id = htonl (t->local_tid_dest);
1085 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1090 * Notify clients of tunnel disconnections, if needed.
1091 * In case the origin disconnects, the destination clients get a tunnel destroy
1092 * notification. If the last destination disconnects (only one remaining client
1093 * in tunnel), the origin gets a (local ID) peer disconnected.
1094 * Note that the function must be called BEFORE removing the client from
1097 * @param t Tunnel that was destroyed.
1098 * @param c Client that disconnected.
1101 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1107 struct GNUNET_MESH_TunnelMessage msg;
1109 msg.header.size = htons (sizeof (msg));
1110 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1111 msg.tunnel_id = htonl (t->local_tid_dest);
1112 for (i = 0; i < t->nclients; i++)
1113 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1114 &msg.header, GNUNET_NO);
1116 // FIXME when to disconnect an incoming tunnel?
1117 else if (1 == t->nclients && NULL != t->owner)
1119 struct GNUNET_MESH_PeerControl msg;
1121 msg.header.size = htons (sizeof (msg));
1122 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1123 msg.tunnel_id = htonl (t->local_tid);
1124 msg.peer = my_full_id;
1125 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1126 &msg.header, GNUNET_NO);
1132 * Function called to notify a client about the socket
1133 * being ready to queue more data. "buf" will be
1134 * NULL and "size" zero if the socket was closed for
1135 * writing in the meantime.
1137 * @param cls closure
1138 * @param size number of bytes available in buf
1139 * @param buf where the callee should write the message
1140 * @return number of bytes written to buf
1143 send_core_create_path (void *cls, size_t size, void *buf);
1147 * Function called to notify a client about the socket
1148 * being ready to queue more data. "buf" will be
1149 * NULL and "size" zero if the socket was closed for
1150 * writing in the meantime.
1152 * @param cls closure (data itself)
1153 * @param size number of bytes available in buf
1154 * @param buf where the callee should write the message
1156 * @return number of bytes written to buf
1159 send_core_data_multicast (void *cls, size_t size, void *buf);
1163 * Decrements the reference counter and frees all resources if needed
1165 * @param mesh_data Data Descriptor used in a multicast message.
1166 * Freed no longer needed (last message).
1169 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
1171 /* Make sure it's a multicast packet */
1172 GNUNET_assert (NULL != mesh_data->reference_counter);
1174 if (0 == --(*(mesh_data->reference_counter)))
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1177 if (NULL != mesh_data->task)
1179 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1181 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1183 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1185 GNUNET_free (mesh_data->task);
1187 GNUNET_free (mesh_data->reference_counter);
1188 GNUNET_free (mesh_data->data);
1189 GNUNET_free (mesh_data);
1195 * Cancel a core transmission that was already requested and free all resources
1196 * associated to the request.
1198 * @param peer PeeInfo of the peer whose transmission is cancelled.
1199 * @param i Position of the transmission to be cancelled.
1202 peer_info_cancel_transmission (struct MeshPeerInfo *peer, unsigned int i)
1204 if (NULL != peer->core_transmit[i])
1206 struct MeshTransmissionDescriptor *dd;
1207 struct MeshPathInfo *path_info;
1211 struct GNUNET_PeerIdentity id;
1213 GNUNET_PEER_resolve (peer->id, &id);
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 " Cancelling data transmission at %s [%u]\n",
1216 GNUNET_i2s (&id), i);
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message type %u\n",
1221 /* TODO: notify that tranmission has failed */
1222 switch (peer->types[i])
1224 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1225 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1226 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
1228 dd = peer->infos[i];
1229 data_descriptor_decrement_multicast (dd->mesh_data);
1231 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
1233 path_info = peer->infos[i];
1234 path_destroy (path_info->path);
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
1240 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
1241 peer->core_transmit[i] = NULL;
1242 GNUNET_free (peer->infos[i]);
1248 * Get a unused CORE slot to transmit a message to a peer. If all the slots
1249 * are used, cancel one and return it's position.
1251 * @param peer PeerInfo of the neighbor we want to transmit to.
1253 * @return The index of an available slot to transmit to the neighbor.
1256 peer_info_transmit_slot (struct MeshPeerInfo *peer)
1260 for (i = 0; peer->core_transmit[i]; i++)
1262 if (i == (CORE_QUEUE_SIZE - 1))
1264 /* All positions are taken! Overwriting! */
1266 peer_info_cancel_transmission (peer, 0);
1275 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1276 * and insert it in the appropiate structures if the peer is not known yet.
1278 * @param peer Full identity of the peer.
1280 * @return Existing or newly created peer info.
1282 static struct MeshPeerInfo *
1283 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1285 struct MeshPeerInfo *peer_info;
1287 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1288 if (NULL == peer_info)
1291 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1292 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1293 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1294 peer_info->id = GNUNET_PEER_intern (peer);
1302 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1303 * and insert it in the appropiate structures if the peer is not known yet.
1305 * @param peer Short identity of the peer.
1307 * @return Existing or newly created peer info.
1309 static struct MeshPeerInfo *
1310 peer_info_get_short (const GNUNET_PEER_Id peer)
1312 struct GNUNET_PeerIdentity id;
1314 GNUNET_PEER_resolve (peer, &id);
1315 return peer_info_get (&id);
1320 * Iterator to remove the tunnel from the list of tunnels a peer participates
1323 * @param cls Closure (tunnel info)
1324 * @param key GNUNET_PeerIdentity of the peer (unused)
1325 * @param value PeerInfo of the peer
1327 * @return always GNUNET_YES, to keep iterating
1330 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1332 struct MeshTunnel *t = cls;
1333 struct MeshPeerInfo *peer = value;
1336 for (i = 0; i < peer->ntunnels; i++)
1339 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1342 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1343 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1352 * Core callback to write a
1354 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1355 * @param size Number of bytes available in buf.
1356 * @param buf Where the to write the message.
1358 * @return number of bytes written to buf
1361 send_core_data_raw (void *cls, size_t size, void *buf)
1363 struct MeshTransmissionDescriptor *info = cls;
1364 struct GNUNET_MessageHeader *msg;
1367 GNUNET_assert (NULL != info);
1368 GNUNET_assert (NULL != info->mesh_data);
1369 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1370 total_size = ntohs (msg->size);
1372 if (total_size > size)
1374 struct GNUNET_PeerIdentity id;
1376 GNUNET_PEER_resolve (info->peer->id, &id);
1377 info->peer->core_transmit[info->handler_n] =
1378 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1379 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1380 size, &send_core_data_raw, info);
1383 info->peer->core_transmit[info->handler_n] = NULL;
1384 memcpy (buf, msg, total_size);
1385 GNUNET_free (info->mesh_data);
1392 * Sends an already built message to a peer, properly registrating
1393 * all used resources.
1395 * @param message Message to send. Fucntion makes a copy of it.
1396 * @param peer Short ID of the neighbor whom to send the message.
1401 send_message (const struct GNUNET_MessageHeader *message,
1402 const struct GNUNET_PeerIdentity *peer)
1404 struct MeshTransmissionDescriptor *info;
1405 struct MeshPeerInfo *neighbor;
1406 struct MeshPeerPath *p;
1410 // GNUNET_TRANSPORT_try_connect();
1412 size = ntohs (message->size);
1413 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1414 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1415 info->mesh_data->data = GNUNET_malloc (size);
1416 memcpy (info->mesh_data->data, message, size);
1417 info->mesh_data->data_len = size;
1418 neighbor = peer_info_get (peer);
1419 for (p = neighbor->path_head; NULL != p; p = p->next)
1429 GNUNET_free (info->mesh_data->data);
1430 GNUNET_free (info->mesh_data);
1434 i = peer_info_transmit_slot (neighbor);
1435 info->handler_n = i;
1436 info->peer = neighbor;
1437 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1438 neighbor->infos[i] = info;
1439 neighbor->core_transmit[i] =
1440 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1441 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1442 size, &send_core_data_raw, info);
1448 * Sends a CREATE PATH message for a path to a peer, properly registrating
1449 * all used resources.
1451 * @param peer PeerInfo of the final peer for whom this path is being created.
1452 * @param p Path itself.
1453 * @param t Tunnel for which the path is created.
1456 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1457 struct MeshTunnel *t)
1459 struct GNUNET_PeerIdentity id;
1460 struct MeshPathInfo *path_info;
1461 struct MeshPeerInfo *neighbor;
1466 p = tree_get_path_to_peer (t->tree, peer->id);
1473 for (i = 0; i < p->length; i++)
1475 if (p->peers[i] == myid)
1478 if (i >= p->length - 1)
1484 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1486 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1487 path_info->path = p;
1489 neighbor = peer_info_get (&id);
1490 path_info->peer = neighbor;
1491 path_info->pos = peer_info_transmit_slot (neighbor);
1492 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1493 neighbor->infos[path_info->pos] = path_info;
1494 neighbor->core_transmit[path_info->pos] =
1495 GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
1498 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1500 sizeof (struct GNUNET_MESH_ManipulatePath) +
1501 (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1502 &send_core_create_path, /* callback */
1503 path_info); /* cls */
1508 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1510 * @param t Tunnel whose path to destroy.
1511 * @param destination Short ID of the peer to whom the path to destroy.
1514 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1516 struct MeshPeerPath *p;
1519 p = tree_get_path_to_peer (t->tree, destination);
1525 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1526 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1528 struct GNUNET_MESH_ManipulatePath *msg;
1529 struct GNUNET_PeerIdentity *pi;
1533 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1534 msg->header.size = htons (size);
1535 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1536 msg->tid = htonl (t->id.tid);
1537 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1538 for (i = 0; i < p->length; i++)
1540 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1542 send_message (&msg->header, tree_get_first_hop (t->tree, destination));
1549 * Try to establish a new connection to this peer.
1550 * Use the best path for the given tunnel.
1551 * If the peer doesn't have any path to it yet, try to get one.
1552 * If the peer already has some path, send a CREATE PATH towards it.
1554 * @param peer PeerInfo of the peer.
1555 * @param t Tunnel for which to create the path, if possible.
1558 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1560 struct MeshPeerPath *p;
1561 struct MeshPathInfo *path_info;
1563 if (NULL != peer->path_head)
1565 p = tree_get_path_to_peer (t->tree, peer->id);
1572 // FIXME always send create path to self
1575 send_create_path (peer, p, t);
1579 GNUNET_HashCode hash;
1582 send_client_peer_connected (t, myid);
1583 t->local_tid_dest = next_local_tid++;
1584 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1587 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1588 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1595 else if (NULL == peer->dhtget)
1597 struct GNUNET_PeerIdentity id;
1599 GNUNET_PEER_resolve (peer->id, &id);
1600 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1601 path_info->peer = peer;
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1605 peer->dhtgetcls = path_info;
1606 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1607 GNUNET_BLOCK_TYPE_TEST, /* type */
1608 &id.hashPubKey, /* key to search */
1609 10, /* replication level */
1610 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1611 0, /* xquery bits */
1612 &dht_get_id_handler, path_info);
1614 /* Otherwise, there is no path but the DHT get is already started. */
1619 * Task to delay the connection of a peer
1621 * @param cls Closure (path info with tunnel and peer to connect).
1622 * Will be free'd on exection.
1623 * @param tc TaskContext
1626 peer_info_connect_task (void *cls,
1627 const struct GNUNET_SCHEDULER_TaskContext *tc)
1629 struct MeshPathInfo *path_info = cls;
1631 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1636 peer_info_connect (path_info->peer, path_info->t);
1642 * Destroy the peer_info and free any allocated resources linked to it
1644 * @param pi The peer_info to destroy.
1646 * @return GNUNET_OK on success
1649 peer_info_destroy (struct MeshPeerInfo *pi)
1651 struct GNUNET_PeerIdentity id;
1652 struct MeshPeerPath *p;
1653 struct MeshPeerPath *nextp;
1656 GNUNET_PEER_resolve (pi->id, &id);
1657 GNUNET_PEER_change_rc (pi->id, -1);
1660 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1663 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1664 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1666 if (NULL != pi->dhtget)
1668 GNUNET_DHT_get_stop (pi->dhtget);
1669 GNUNET_free (pi->dhtgetcls);
1671 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1673 peer_info_cancel_transmission (pi, i);
1679 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1689 * Remove all paths that rely on a direct connection between p1 and p2
1690 * from the peer itself and notify all tunnels about it.
1692 * @param peer PeerInfo of affected peer.
1693 * @param p1 GNUNET_PEER_Id of one peer.
1694 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1697 * TODO: optimize (see below)
1700 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1703 struct MeshPeerPath *p;
1704 struct MeshPeerPath *aux;
1705 struct MeshPeerInfo *peer_d;
1707 unsigned int destroyed;
1713 p = peer->path_head;
1717 for (i = 0; i < (p->length - 1); i++)
1719 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1720 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1722 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1733 for (i = 0; i < peer->ntunnels; i++)
1735 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1739 * Problem: one or more peers have been deleted from the tunnel tree.
1740 * We don't know who they are to try to add them again.
1741 * We need to try to find a new path for each of the disconnected peers.
1742 * Some of them might already have a path to reach them that does not
1743 * involve p1 and p2. Adding all anew might render in a better tree than
1744 * the trivial immediate fix.
1746 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1747 * its children will be reachable trough him.
1749 peer_d = peer_info_get_short (d);
1752 for (p = peer_d->path_head; NULL != p; p = p->next)
1754 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1762 /* No callback, as peer will be already disconnected and a connection
1763 * scheduled by tunnel_notify_connection_broken.
1765 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1769 peer_info_connect (peer_d, peer->tunnels[i]);
1776 * Add the path to the peer and update the path used to reach it in case this
1779 * @param peer_info Destination peer to add the path to.
1780 * @param path New path to add. Last peer must be the peer in arg 1.
1781 * Path will be either used of freed if already known.
1782 * @param trusted Do we trust that this path is real?
1785 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1788 struct MeshPeerPath *aux;
1792 if ((NULL == peer_info) || (NULL == path))
1795 path_destroy (path);
1798 if (path->peers[path->length - 1] != peer_info->id)
1801 path_destroy (path);
1804 if (path->length <= 2 && GNUNET_NO == trusted)
1806 /* Only allow CORE to tell us about direct paths */
1807 path_destroy (path);
1810 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1811 for (l = 1; l < path->length; l++)
1813 if (path->peers[l] == myid)
1815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1816 for (l2 = 0; l2 < path->length - l; l2++)
1818 path->peers[l2] = path->peers[l + l2];
1823 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1828 struct GNUNET_PeerIdentity id;
1830 GNUNET_PEER_resolve (peer_info->id, &id);
1831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1832 path->length, GNUNET_i2s (&id));
1835 l = path_get_length (path);
1842 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1843 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1845 l2 = path_get_length (aux);
1848 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1849 peer_info->path_tail, aux, path);
1854 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1856 path_destroy (path);
1861 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1868 * Add the path to the origin peer and update the path used to reach it in case
1869 * this is the shortest.
1870 * The path is given in peer_info -> destination, therefore we turn the path
1871 * upside down first.
1873 * @param peer_info Peer to add the path to, being the origin of the path.
1874 * @param path New path to add after being inversed.
1875 * @param trusted Do we trust that this path is real?
1878 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1879 struct MeshPeerPath *path, int trusted)
1882 peer_info_add_path (peer_info, path, trusted);
1887 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1888 * to obtain a local peer -> destination path and interning the peer ids.
1890 * @return Newly allocated and created path
1892 static struct MeshPeerPath *
1893 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1894 unsigned int get_path_length,
1895 const struct GNUNET_PeerIdentity *put_path,
1896 unsigned int put_path_length)
1898 struct MeshPeerPath *p;
1904 GNUNET_PEER_change_rc (myid, 1);
1905 i = get_path_length;
1906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1907 for (i--; i >= 0; i--)
1909 id = GNUNET_PEER_intern (&get_path[i]);
1910 if (p->length > 0 && id == p->peers[p->length - 1])
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1913 GNUNET_PEER_change_rc (id, -1);
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1918 GNUNET_i2s (&get_path[i]));
1920 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1921 p->peers[p->length - 1] = id;
1924 i = put_path_length;
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1926 for (i--; i >= 0; i--)
1928 id = GNUNET_PEER_intern (&put_path[i]);
1931 /* PUT path went through us, so discard the path up until now and start
1932 * from here to get a much shorter (and loop-free) path.
1937 if (p->length > 0 && id == p->peers[p->length - 1])
1939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1940 GNUNET_PEER_change_rc (id, -1);
1944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1945 GNUNET_i2s (&put_path[i]));
1947 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1948 p->peers[p->length - 1] = id;
1952 if (get_path_length > 0)
1953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1954 GNUNET_i2s (&get_path[0]));
1955 if (put_path_length > 0)
1956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1957 GNUNET_i2s (&put_path[0]));
1958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1960 for (i = 0; i < p->length; i++)
1962 struct GNUNET_PeerIdentity peer_id;
1964 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1966 GNUNET_i2s (&peer_id));
1974 * Adds a path to the peer_infos of all the peers in the path
1976 * @param p Path to process.
1977 * @param confirmed Whether we know if the path works or not. FIXME use
1980 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1984 /* TODO: invert and add */
1985 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1986 for (i++; i < p->length; i++)
1988 struct MeshPeerInfo *aux;
1989 struct MeshPeerPath *copy;
1991 aux = peer_info_get_short (p->peers[i]);
1992 copy = path_duplicate (p);
1993 copy->length = i + 1;
1994 peer_info_add_path (aux, copy, GNUNET_NO);
2000 * Send keepalive packets for a peer
2002 * @param cls Closure (tunnel for which to send the keepalive).
2003 * @param tc Notification context.
2005 * TODO: implement explicit multicast keepalive?
2008 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2012 * Search for a tunnel among the incoming tunnels
2014 * @param tid the local id of the tunnel
2016 * @return tunnel handler, NULL if doesn't exist
2018 static struct MeshTunnel *
2019 tunnel_get_incoming (MESH_TunnelNumber tid)
2021 GNUNET_HashCode hash;
2023 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2024 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2025 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2030 * Search for a tunnel among the tunnels for a client
2032 * @param c the client whose tunnels to search in
2033 * @param tid the local id of the tunnel
2035 * @return tunnel handler, NULL if doesn't exist
2037 static struct MeshTunnel *
2038 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2040 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2042 return tunnel_get_incoming (tid);
2046 GNUNET_HashCode hash;
2048 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2049 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2055 * Search for a tunnel by global ID using PEER_ID
2057 * @param pi owner of the tunnel
2058 * @param tid global tunnel number
2060 * @return tunnel handler, NULL if doesn't exist
2062 static struct MeshTunnel *
2063 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2065 struct MESH_TunnelID id;
2066 GNUNET_HashCode hash;
2071 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2072 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2077 * Search for a tunnel by global ID using full PeerIdentities
2079 * @param oid owner of the tunnel
2080 * @param tid global tunnel number
2082 * @return tunnel handler, NULL if doesn't exist
2084 static struct MeshTunnel *
2085 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2087 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2092 * Delete an active client from the tunnel.
2098 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2102 for (i = 0; i < t->nclients; i++)
2104 if (t->clients[i] == c)
2106 t->clients[i] = t->clients[t->nclients - 1];
2107 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2115 * Delete an ignored client from the tunnel.
2121 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2125 for (i = 0; i < t->nignore; i++)
2127 if (t->ignore[i] == c)
2129 t->ignore[i] = t->ignore[t->nignore - 1];
2130 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2138 * Delete a client from the tunnel. It should be only done on
2139 * client disconnection, otherwise use client_ignore_tunnel.
2145 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2147 tunnel_delete_ignored_client (t, c);
2148 tunnel_delete_active_client (t, c);
2153 * Callback used to notify a client owner of a tunnel that a peer has
2154 * disconnected, most likely because of a path change.
2156 * @param cls Closure (tunnel this notification is about).
2157 * @param peer_id Short ID of disconnected peer.
2160 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2162 struct MeshTunnel *t = cls;
2163 struct MeshPeerInfo *peer;
2164 struct MeshPathInfo *path_info;
2166 if (NULL != t->owner && NULL != nc)
2168 struct GNUNET_MESH_PeerControl msg;
2170 msg.header.size = htons (sizeof (msg));
2171 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2172 msg.tunnel_id = htonl (t->local_tid);
2173 GNUNET_PEER_resolve (peer_id, &msg.peer);
2174 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2175 &msg.header, GNUNET_NO);
2177 peer = peer_info_get_short (peer_id);
2178 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2179 path_info->peer = peer;
2181 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2186 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2187 * needed rescources.
2188 * If peer already exists, reevaluate shortest path and change if different.
2190 * @param t Tunnel we want to add a new peer to
2191 * @param peer PeerInfo of the peer being added
2195 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2197 struct GNUNET_PeerIdentity id;
2198 struct MeshPeerPath *best_p;
2199 struct MeshPeerPath *p;
2200 unsigned int best_cost;
2203 GNUNET_PEER_resolve (peer->id, &id);
2205 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2208 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2209 GNUNET_assert (GNUNET_OK ==
2210 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2212 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2215 if (NULL != (p = peer->path_head))
2218 best_cost = tree_get_path_cost (t->tree, p);
2221 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2228 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2229 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2230 t->path_refresh_task =
2231 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2235 /* Start a DHT get */
2236 peer_info_connect (peer, t);
2241 * Add a path to a tunnel which we don't own, just to remember the next hop.
2242 * If destination node was already in the tunnel, the first hop information
2243 * will be replaced with the new path.
2245 * @param t Tunnel we want to add a new peer to
2246 * @param p Path to add
2247 * @param own_pos Position of local node in path.
2251 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2252 unsigned int own_pos)
2254 struct GNUNET_PeerIdentity id;
2256 GNUNET_assert (0 != own_pos);
2257 tree_add_path (t->tree, p, NULL, NULL);
2258 if (own_pos < p->length - 1)
2260 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2261 tree_update_first_hops (t->tree, myid, &id);
2267 * Notifies a tunnel that a connection has broken that affects at least
2268 * some of its peers. Sends a notification towards the root of the tree.
2269 * In case the peer is the owner of the tree, notifies the client that owns
2270 * the tunnel and tries to reconnect.
2272 * @param t Tunnel affected.
2273 * @param p1 Peer that got disconnected from p2.
2274 * @param p2 Peer that got disconnected from p1.
2276 * @return Short ID of the peer disconnected (either p1 or p2).
2277 * 0 if the tunnel remained unaffected.
2279 static GNUNET_PEER_Id
2280 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2286 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2288 if (myid != p1 && myid != p2)
2294 if (tree_get_predecessor (t->tree) != 0)
2296 /* We are the peer still connected, notify owner of the disconnection. */
2297 struct GNUNET_MESH_PathBroken msg;
2298 struct GNUNET_PeerIdentity neighbor;
2300 msg.header.size = htons (sizeof (msg));
2301 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2302 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2303 msg.tid = htonl (t->id.tid);
2304 msg.peer1 = my_full_id;
2305 GNUNET_PEER_resolve (pid, &msg.peer2);
2306 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2307 send_message (&msg.header, &neighbor);
2315 * Send a multicast packet to a neighbor.
2317 * @param cls Closure (Info about the multicast packet)
2318 * @param neighbor_id Short ID of the neighbor to send the packet to.
2321 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2323 struct MeshData *mdata = cls;
2324 struct MeshTransmissionDescriptor *info;
2325 struct GNUNET_PeerIdentity neighbor;
2328 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2330 info->mesh_data = mdata;
2331 (*(mdata->reference_counter)) ++;
2332 info->destination = neighbor_id;
2333 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2335 GNUNET_i2s (&neighbor));
2336 info->peer = peer_info_get (&neighbor);
2337 GNUNET_assert (NULL != info->peer);
2338 i = peer_info_transmit_slot (info->peer);
2339 info->handler_n = i;
2340 info->peer->infos[i] = info;
2341 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2342 info->peer->core_transmit[i] =
2343 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2344 GNUNET_TIME_UNIT_FOREVER_REL,
2345 &neighbor, info->mesh_data->data_len,
2346 &send_core_data_multicast, info);
2350 * Send a message in a tunnel in multicast, sending a copy to each child node
2351 * down the local one in the tunnel tree.
2353 * @param t Tunnel in which to send the data.
2354 * @param msg Message to be sent.
2355 * @param internal Has the service generated this message?
2358 tunnel_send_multicast (struct MeshTunnel *t,
2359 const struct GNUNET_MessageHeader *msg,
2362 struct MeshData *mdata;
2364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2365 " sending a multicast packet...\n");
2366 mdata = GNUNET_malloc (sizeof (struct MeshData));
2367 mdata->data_len = ntohs (msg->size);
2368 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2370 mdata->data = GNUNET_malloc (mdata->data_len);
2371 memcpy (mdata->data, msg, mdata->data_len);
2372 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2374 struct GNUNET_MESH_Multicast *mcast;
2376 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2377 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2379 ntohl (mcast->ttl));
2383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2385 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2386 && GNUNET_NO == internal)
2388 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2390 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2396 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2397 if (*(mdata->reference_counter) == 0)
2399 GNUNET_free (mdata->data);
2400 GNUNET_free (mdata->reference_counter);
2401 if (NULL != mdata->task)
2403 GNUNET_SCHEDULER_cancel(*(mdata->task));
2404 GNUNET_free (mdata->task);
2405 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2407 // FIXME change order?
2408 GNUNET_free (mdata);
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2411 " sending a multicast packet done\n");
2417 * Send a message to all peers in this tunnel that the tunnel is no longer
2420 * @param t The tunnel whose peers to notify.
2423 tunnel_send_destroy (struct MeshTunnel *t)
2425 struct GNUNET_MESH_TunnelDestroy msg;
2427 msg.header.size = htons (sizeof (msg));
2428 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2429 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2430 msg.tid = htonl (t->id.tid);
2431 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2437 * Destroy the tunnel and free any allocated resources linked to it.
2439 * @param t the tunnel to destroy
2441 * @return GNUNET_OK on success
2444 tunnel_destroy (struct MeshTunnel *t)
2446 struct MeshClient *c;
2447 struct MeshQueue *q;
2448 struct MeshQueue *qn;
2449 GNUNET_HashCode hash;
2460 struct GNUNET_PeerIdentity id;
2462 GNUNET_PEER_resolve (t->id.oid, &id);
2463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2464 GNUNET_i2s (&id), t->id.tid);
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2470 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2471 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2476 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2479 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2483 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2484 for (i = 0; i < t->nclients; i++)
2488 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2493 for (i = 0; i < t->nignore; i++)
2497 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2502 if (t->nclients > 0)
2505 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2509 GNUNET_free (t->clients);
2511 if (NULL != t->peers)
2513 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2515 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2520 if (NULL != q->data)
2521 GNUNET_free (q->data);
2525 /* TODO cancel core transmit ready in case it was active */
2527 tree_destroy (t->tree);
2528 if (NULL != t->dht_get_type)
2529 GNUNET_DHT_get_stop (t->dht_get_type);
2530 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2531 GNUNET_SCHEDULER_cancel (t->timeout_task);
2532 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2533 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2540 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2541 * that are no longer needed, as well as nodes of no longer reachable peers.
2542 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2544 * @param t Tunnel from which to remove the path.
2545 * @param peer Short id of the peer which should be removed.
2548 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2550 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2556 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2557 * client when the client disconnects. If the client is not the owner, the
2558 * owner will get notified if no more clients are in the tunnel and the client
2559 * get removed from the tunnel's list.
2561 * @param cls closure (client that is disconnecting)
2562 * @param key the hash of the local tunnel id (used to access the hashmap)
2563 * @param value the value stored at the key (tunnel to destroy)
2565 * @return GNUNET_OK on success
2568 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2570 struct MeshTunnel *t = value;
2571 struct MeshClient *c = cls;
2574 send_client_tunnel_disconnect(t, c);
2577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2578 "Client %u is destination, keeping the tunnel alive.\n", c->id);
2579 tunnel_delete_client(t, c);
2580 client_delete_tunnel(c, t);
2583 tunnel_send_destroy(t);
2584 r = tunnel_destroy (t);
2590 * Timeout function, destroys tunnel if called
2592 * @param cls Closure (tunnel to destroy).
2593 * @param tc TaskContext
2596 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2598 struct MeshTunnel *t = cls;
2600 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2602 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2607 * Resets the tunnel timeout. Starts it if no timeout was running.
2609 * @param t Tunnel whose timeout to reset.
2612 tunnel_reset_timeout (struct MeshTunnel *t)
2614 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2615 GNUNET_SCHEDULER_cancel (t->timeout_task);
2617 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2618 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2622 /******************************************************************************/
2623 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2624 /******************************************************************************/
2627 * Function called to notify a client about the socket
2628 * being ready to queue more data. "buf" will be
2629 * NULL and "size" zero if the socket was closed for
2630 * writing in the meantime.
2632 * @param cls closure
2633 * @param size number of bytes available in buf
2634 * @param buf where the callee should write the message
2635 * @return number of bytes written to buf
2638 send_core_create_path (void *cls, size_t size, void *buf)
2640 struct MeshPathInfo *info = cls;
2641 struct GNUNET_MESH_ManipulatePath *msg;
2642 struct GNUNET_PeerIdentity *peer_ptr;
2643 struct MeshPeerInfo *peer = info->peer;
2644 struct MeshTunnel *t = info->t;
2645 struct MeshPeerPath *p = info->path;
2649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2651 sizeof (struct GNUNET_MESH_ManipulatePath) +
2652 p->length * sizeof (struct GNUNET_PeerIdentity);
2654 if (size < size_needed || NULL == buf)
2656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "create path retransmit!\n");
2657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buf: %p\n", buf);
2658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " size: (%u/%u)\n", size,
2660 info->peer->core_transmit[info->pos] =
2661 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2662 GNUNET_TIME_UNIT_FOREVER_REL,
2663 tree_get_first_hop (t->tree,
2665 size_needed, &send_core_create_path,
2669 info->peer->core_transmit[info->pos] = NULL;
2672 struct GNUNET_PeerIdentity id;
2674 GNUNET_PEER_resolve (peer->id, &id);
2675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2676 " setting core_transmit %s [%u] to NULL\n",
2677 GNUNET_i2s (&id), info->pos);
2680 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2681 msg->header.size = htons (size_needed);
2682 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2683 msg->tid = ntohl (t->id.tid);
2685 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2686 for (i = 0; i < p->length; i++)
2688 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2695 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2701 * Function called to notify a client about the socket
2702 * being ready to queue more data. "buf" will be
2703 * NULL and "size" zero if the socket was closed for
2704 * writing in the meantime.
2706 * @param cls closure (data itself)
2707 * @param size number of bytes available in buf
2708 * @param buf where the callee should write the message
2710 * @return number of bytes written to buf
2713 send_core_data_multicast (void *cls, size_t size, void *buf)
2715 struct MeshTransmissionDescriptor *info = cls;
2718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
2719 GNUNET_assert (NULL != info);
2720 GNUNET_assert (NULL != info->peer);
2721 total_size = info->mesh_data->data_len;
2722 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2724 if (total_size > size)
2727 struct GNUNET_PeerIdentity id;
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2730 "Multicast: retransmitting... (%u/%u)\n", size,
2732 GNUNET_PEER_resolve (info->peer->id, &id);
2733 info->peer->core_transmit[info->handler_n] =
2734 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2735 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2737 &send_core_data_multicast, info);
2740 info->peer->core_transmit[info->handler_n] = NULL;
2741 info->peer->infos[info->handler_n] = NULL;
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
2743 memcpy (buf, info->mesh_data->data, total_size);
2746 struct GNUNET_MESH_Multicast *mc;
2747 struct GNUNET_MessageHeader *mh;
2750 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2752 mc = (struct GNUNET_MESH_Multicast *) mh;
2753 mh = (struct GNUNET_MessageHeader *) &mc[1];
2754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2755 " multicast, payload type %u\n", ntohs (mh->type));
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2757 " multicast, payload size %u\n", ntohs (mh->size));
2761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
2766 data_descriptor_decrement_multicast (info->mesh_data);
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
2769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
2775 * Function called to notify a client about the socket
2776 * being ready to queue more data. "buf" will be
2777 * NULL and "size" zero if the socket was closed for
2778 * writing in the meantime.
2780 * @param cls closure (MeshTransmissionDescriptor)
2781 * @param size number of bytes available in buf
2782 * @param buf where the callee should write the message
2783 * @return number of bytes written to buf
2786 send_core_path_ack (void *cls, size_t size, void *buf)
2788 struct MeshTransmissionDescriptor *info = cls;
2789 struct GNUNET_MESH_PathACK *msg = buf;
2791 GNUNET_assert (NULL != info);
2794 info->peer->core_transmit[info->handler_n] = NULL;
2796 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2801 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2802 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2803 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2804 msg->tid = htonl (info->origin->tid);
2805 msg->peer_id = my_full_id;
2807 /* TODO add signature */
2809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2810 return sizeof (struct GNUNET_MESH_PathACK);
2814 /******************************************************************************/
2815 /******************** MESH NETWORK HANDLERS **************************/
2816 /******************************************************************************/
2820 * Core handler for path creation
2822 * @param cls closure
2823 * @param message message
2824 * @param peer peer identity this notification is about
2825 * @param atsi performance data
2826 * @param atsi_count number of records in 'atsi'
2828 * @return GNUNET_OK to keep the connection open,
2829 * GNUNET_SYSERR to close it (signal serious error)
2832 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2833 const struct GNUNET_MessageHeader *message,
2834 const struct GNUNET_ATS_Information *atsi,
2835 unsigned int atsi_count)
2837 unsigned int own_pos;
2840 MESH_TunnelNumber tid;
2841 struct GNUNET_MESH_ManipulatePath *msg;
2842 struct GNUNET_PeerIdentity *pi;
2843 GNUNET_HashCode hash;
2844 struct MeshPeerPath *path;
2845 struct MeshPeerInfo *dest_peer_info;
2846 struct MeshPeerInfo *orig_peer_info;
2847 struct MeshTunnel *t;
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2850 "Received a path create msg [%s]\n",
2851 GNUNET_i2s (&my_full_id));
2852 size = ntohs (message->size);
2853 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2855 GNUNET_break_op (0);
2859 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2860 if (size % sizeof (struct GNUNET_PeerIdentity))
2862 GNUNET_break_op (0);
2865 size /= sizeof (struct GNUNET_PeerIdentity);
2868 GNUNET_break_op (0);
2871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2872 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2874 tid = ntohl (msg->tid);
2875 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2877 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2878 t = tunnel_get (pi, tid);
2881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2882 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2883 t->id.oid = GNUNET_PEER_intern (pi);
2885 while (NULL != tunnel_get_incoming (next_local_tid))
2886 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2887 t->local_tid_dest = next_local_tid++;
2888 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2889 t->tree = tree_new (t->id.oid);
2891 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2893 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2894 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2900 tunnel_reset_timeout (t);
2901 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2903 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2904 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2912 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2913 if (NULL == dest_peer_info)
2915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2916 " Creating PeerInfo for destination.\n");
2917 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2918 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2919 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2921 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2923 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2924 if (NULL == orig_peer_info)
2926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2927 " Creating PeerInfo for origin.\n");
2928 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2929 orig_peer_info->id = GNUNET_PEER_intern (pi);
2930 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2931 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
2934 path = path_new (size);
2936 for (i = 0; i < size; i++)
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
2939 GNUNET_i2s (&pi[i]));
2940 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2941 if (path->peers[i] == myid)
2944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
2947 /* cannot be self, must be 'not found' */
2948 /* create path: self not found in path through self */
2949 GNUNET_break_op (0);
2950 path_destroy (path);
2951 /* FIXME error. destroy tunnel? leave for timeout? */
2954 path_add_to_peers (path, GNUNET_NO);
2955 tunnel_add_path (t, path, own_pos);
2956 if (own_pos == size - 1)
2958 /* It is for us! Send ack. */
2959 struct MeshTransmissionDescriptor *info;
2962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2963 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2964 if (NULL == t->peers)
2966 /* New tunnel! Notify clients on data. */
2967 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2969 GNUNET_break (GNUNET_OK ==
2970 GNUNET_CONTAINER_multihashmap_put (t->peers,
2971 &my_full_id.hashPubKey,
2974 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
2975 /* FIXME use send_message */
2976 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2977 info->origin = &t->id;
2978 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2979 GNUNET_assert (NULL != info->peer);
2980 j = peer_info_transmit_slot (info->peer);
2981 info->handler_n = j;
2982 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2983 info->peer->infos[j] = info;
2984 info->peer->core_transmit[j] =
2985 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 10,
2986 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2987 sizeof (struct GNUNET_MESH_PathACK),
2988 &send_core_path_ack, info);
2992 struct MeshPeerPath *path2;
2994 /* It's for somebody else! Retransmit. */
2995 path2 = path_duplicate (path);
2996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
2997 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
2998 path2 = path_duplicate (path);
2999 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3000 send_create_path (dest_peer_info, path, t);
3007 * Core handler for path destruction
3009 * @param cls closure
3010 * @param message message
3011 * @param peer peer identity this notification is about
3012 * @param atsi performance data
3013 * @param atsi_count number of records in 'atsi'
3015 * @return GNUNET_OK to keep the connection open,
3016 * GNUNET_SYSERR to close it (signal serious error)
3019 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3020 const struct GNUNET_MessageHeader *message,
3021 const struct GNUNET_ATS_Information *atsi,
3022 unsigned int atsi_count)
3024 struct GNUNET_MESH_ManipulatePath *msg;
3025 struct GNUNET_PeerIdentity *pi;
3026 struct MeshPeerPath *path;
3027 struct MeshTunnel *t;
3028 unsigned int own_pos;
3032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3033 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3034 size = ntohs (message->size);
3035 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3037 GNUNET_break_op (0);
3041 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3042 if (size % sizeof (struct GNUNET_PeerIdentity))
3044 GNUNET_break_op (0);
3047 size /= sizeof (struct GNUNET_PeerIdentity);
3050 GNUNET_break_op (0);
3053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3055 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3056 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3058 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3060 t = tunnel_get (pi, ntohl (msg->tid));
3063 /* TODO notify back: we don't know this tunnel */
3064 GNUNET_break_op (0);
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3068 path = path_new (size);
3070 for (i = 0; i < size; i++)
3072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3073 GNUNET_i2s (&pi[i]));
3074 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3075 if (path->peers[i] == myid)
3078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3079 if (own_pos < path->length - 1)
3080 send_message (message, &pi[own_pos + 1]);
3082 send_client_tunnel_disconnect(t, NULL);
3084 tunnel_delete_peer (t, path->peers[path->length - 1]);
3085 path_destroy (path);
3091 * Core handler for notifications of broken paths
3093 * @param cls closure
3094 * @param message message
3095 * @param peer peer identity this notification is about
3096 * @param atsi performance data
3097 * @param atsi_count number of records in 'atsi'
3099 * @return GNUNET_OK to keep the connection open,
3100 * GNUNET_SYSERR to close it (signal serious error)
3103 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3104 const struct GNUNET_MessageHeader *message,
3105 const struct GNUNET_ATS_Information *atsi,
3106 unsigned int atsi_count)
3108 struct GNUNET_MESH_PathBroken *msg;
3109 struct MeshTunnel *t;
3111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3112 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3113 msg = (struct GNUNET_MESH_PathBroken *) message;
3114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3115 GNUNET_i2s (&msg->peer1));
3116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3117 GNUNET_i2s (&msg->peer2));
3118 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3121 GNUNET_break_op (0);
3124 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3125 GNUNET_PEER_search (&msg->peer2));
3132 * Core handler for tunnel destruction
3134 * @param cls closure
3135 * @param message message
3136 * @param peer peer identity this notification is about
3137 * @param atsi performance data
3138 * @param atsi_count number of records in 'atsi'
3140 * @return GNUNET_OK to keep the connection open,
3141 * GNUNET_SYSERR to close it (signal serious error)
3144 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3145 const struct GNUNET_MessageHeader *message,
3146 const struct GNUNET_ATS_Information *atsi,
3147 unsigned int atsi_count)
3149 struct GNUNET_MESH_TunnelDestroy *msg;
3150 struct MeshTunnel *t;
3152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3153 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3154 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3156 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3157 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3160 /* Probably already got the message from another path,
3161 * destroyed the tunnel and retransmitted to children.
3166 if (t->id.oid == myid)
3168 GNUNET_break_op (0);
3171 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3173 /* Tunnel was incoming, notify clients */
3174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3175 t->local_tid, t->local_tid_dest);
3176 send_clients_tunnel_destroy (t);
3178 tunnel_send_destroy (t);
3185 * Core handler for mesh network traffic going from the origin to a peer
3187 * @param cls closure
3188 * @param peer peer identity this notification is about
3189 * @param message message
3190 * @param atsi performance data
3191 * @param atsi_count number of records in 'atsi'
3192 * @return GNUNET_OK to keep the connection open,
3193 * GNUNET_SYSERR to close it (signal serious error)
3196 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3197 const struct GNUNET_MessageHeader *message,
3198 const struct GNUNET_ATS_Information *atsi,
3199 unsigned int atsi_count)
3201 struct GNUNET_MESH_Unicast *msg;
3202 struct MeshTunnel *t;
3206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3208 size = ntohs (message->size);
3210 sizeof (struct GNUNET_MESH_Unicast) +
3211 sizeof (struct GNUNET_MessageHeader))
3216 msg = (struct GNUNET_MESH_Unicast *) message;
3217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3218 ntohs (msg[1].header.type));
3219 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3222 /* TODO notify back: we don't know this tunnel */
3223 GNUNET_break_op (0);
3226 tunnel_reset_timeout (t);
3227 pid = GNUNET_PEER_search (&msg->destination);
3230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3231 " it's for us! sending to clients...\n");
3232 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3236 " not for us, retransmitting...\n");
3237 send_message (message, tree_get_first_hop (t->tree, pid));
3243 * Core handler for mesh network traffic going from the origin to all peers
3245 * @param cls closure
3246 * @param message message
3247 * @param peer peer identity this notification is about
3248 * @param atsi performance data
3249 * @param atsi_count number of records in 'atsi'
3250 * @return GNUNET_OK to keep the connection open,
3251 * GNUNET_SYSERR to close it (signal serious error)
3253 * TODO: Check who we got this from, to validate route.
3256 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3257 const struct GNUNET_MessageHeader *message,
3258 const struct GNUNET_ATS_Information *atsi,
3259 unsigned int atsi_count)
3261 struct GNUNET_MESH_Multicast *msg;
3262 struct MeshTunnel *t;
3265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
3267 size = ntohs (message->size);
3268 if (sizeof (struct GNUNET_MESH_Multicast) +
3269 sizeof (struct GNUNET_MessageHeader) > size)
3271 GNUNET_break_op (0);
3274 msg = (struct GNUNET_MESH_Multicast *) message;
3275 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3279 /* TODO notify that we dont know that tunnel */
3280 GNUNET_break_op (0);
3283 if (t->mid == ntohl (msg->mid))
3285 /* FIXME: already seen this packet, log dropping */
3286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3287 " Already seen mid %u, DROPPING!\n", t->mid);
3292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3293 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3295 t->mid = ntohl (msg->mid);
3296 tunnel_reset_timeout (t);
3298 /* Transmit to locally interested clients */
3299 if (NULL != t->peers &&
3300 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3302 send_subscribed_clients (message, &msg[1].header);
3304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
3305 if (ntohl (msg->ttl) == 0)
3307 /* FIXME: ttl is 0, log dropping */
3308 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3311 tunnel_send_multicast (t, message, GNUNET_NO);
3317 * Core handler for mesh network traffic toward the owner of a tunnel
3319 * @param cls closure
3320 * @param message message
3321 * @param peer peer identity this notification is about
3322 * @param atsi performance data
3323 * @param atsi_count number of records in 'atsi'
3325 * @return GNUNET_OK to keep the connection open,
3326 * GNUNET_SYSERR to close it (signal serious error)
3329 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3330 const struct GNUNET_MessageHeader *message,
3331 const struct GNUNET_ATS_Information *atsi,
3332 unsigned int atsi_count)
3334 struct GNUNET_MESH_ToOrigin *msg;
3335 struct GNUNET_PeerIdentity id;
3336 struct MeshPeerInfo *peer_info;
3337 struct MeshTunnel *t;
3340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3342 size = ntohs (message->size);
3343 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3344 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3346 GNUNET_break_op (0);
3349 msg = (struct GNUNET_MESH_ToOrigin *) message;
3350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3351 ntohs (msg[1].header.type));
3352 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3356 /* TODO notify that we dont know this tunnel (whom)? */
3357 GNUNET_break_op (0);
3361 if (t->id.oid == myid)
3364 struct GNUNET_MESH_ToOrigin *copy;
3366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3367 " it's for us! sending to clients...\n");
3368 if (NULL == t->owner)
3370 /* got data packet for ownerless tunnel */
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
3372 GNUNET_break_op (0);
3375 /* TODO signature verification */
3376 memcpy (cbuf, message, size);
3377 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3378 copy->tid = htonl (t->local_tid);
3379 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3380 ©->header, GNUNET_YES);
3383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3384 " not for us, retransmitting...\n");
3386 peer_info = peer_info_get (&msg->oid);
3387 if (NULL == peer_info)
3389 /* unknown origin of tunnel */
3393 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3394 send_message (message, &id);
3401 * Core handler for path ACKs
3403 * @param cls closure
3404 * @param message message
3405 * @param peer peer identity this notification is about
3406 * @param atsi performance data
3407 * @param atsi_count number of records in 'atsi'
3409 * @return GNUNET_OK to keep the connection open,
3410 * GNUNET_SYSERR to close it (signal serious error)
3413 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3414 const struct GNUNET_MessageHeader *message,
3415 const struct GNUNET_ATS_Information *atsi,
3416 unsigned int atsi_count)
3418 struct GNUNET_MESH_PathACK *msg;
3419 struct GNUNET_PeerIdentity id;
3420 struct MeshPeerInfo *peer_info;
3421 struct MeshPeerPath *p;
3422 struct MeshTunnel *t;
3424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3425 GNUNET_i2s (&my_full_id));
3426 msg = (struct GNUNET_MESH_PathACK *) message;
3427 t = tunnel_get (&msg->oid, msg->tid);
3430 /* TODO notify that we don't know the tunnel */
3434 peer_info = peer_info_get (&msg->peer_id);
3436 /* Add paths to peers? */
3437 p = tree_get_path_to_peer (t->tree, peer_info->id);
3440 path_add_to_peers (p, GNUNET_YES);
3448 /* Message for us? */
3449 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3452 if (NULL == t->owner)
3454 GNUNET_break_op (0);
3457 if (NULL != t->dht_get_type)
3459 GNUNET_DHT_get_stop (t->dht_get_type);
3460 t->dht_get_type = NULL;
3462 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3464 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3465 send_client_peer_connected (t, peer_info->id);
3470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3471 " not for us, retransmitting...\n");
3472 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3473 peer_info = peer_info_get (&msg->oid);
3474 if (NULL == peer_info)
3476 /* If we know the tunnel, we should DEFINITELY know the peer */
3480 send_message (message, &id);
3486 * Functions to handle messages from core
3488 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3489 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3490 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3491 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3492 sizeof (struct GNUNET_MESH_PathBroken)},
3493 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3494 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3495 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3496 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3497 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3498 sizeof (struct GNUNET_MESH_PathACK)},
3504 /******************************************************************************/
3505 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3506 /******************************************************************************/
3509 * deregister_app: iterator for removing each application registered by a client
3511 * @param cls closure
3512 * @param key the hash of the application id (used to access the hashmap)
3513 * @param value the value stored at the key (client)
3515 * @return GNUNET_OK on success
3518 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3520 GNUNET_break (GNUNET_YES ==
3521 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3528 * notify_client_connection_failure: notify a client that the connection to the
3529 * requested remote peer is not possible (for instance, no route found)
3530 * Function called when the socket is ready to queue more data. "buf" will be
3531 * NULL and "size" zero if the socket was closed for writing in the meantime.
3533 * @param cls closure
3534 * @param size number of bytes available in buf
3535 * @param buf where the callee should write the message
3536 * @return number of bytes written to buf
3539 notify_client_connection_failure (void *cls, size_t size, void *buf)
3542 struct MeshPeerInfo *peer_info;
3543 struct GNUNET_MESH_PeerControl *msg;
3544 struct GNUNET_PeerIdentity id;
3546 if (0 == size && NULL == buf)
3548 // TODO retry? cancel?
3552 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3553 peer_info = (struct MeshPeerInfo *) cls;
3554 msg = (struct GNUNET_MESH_PeerControl *) buf;
3555 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3556 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3557 // msg->tunnel_id = htonl(peer_info->t->tid);
3558 GNUNET_PEER_resolve (peer_info->id, &id);
3559 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3567 * Send keepalive packets for a peer
3569 * @param cls Closure (tunnel for which to send the keepalive).
3570 * @param tc Notification context.
3572 * TODO: implement explicit multicast keepalive?
3575 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3577 struct MeshTunnel *t = cls;
3578 struct GNUNET_MessageHeader *payload;
3579 struct GNUNET_MESH_Multicast *msg;
3581 sizeof (struct GNUNET_MESH_Multicast) +
3582 sizeof (struct GNUNET_MessageHeader);
3585 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3589 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3592 "sending keepalive for tunnel %d\n", t->id.tid);
3594 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3595 msg->header.size = htons (size);
3596 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3597 msg->oid = my_full_id;
3598 msg->tid = htonl (t->id.tid);
3599 msg->ttl = htonl (DEFAULT_TTL);
3600 msg->mid = htonl (t->mid + 1);
3602 payload = (struct GNUNET_MessageHeader *) &msg[1];
3603 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3604 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3605 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
3607 t->path_refresh_task =
3608 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3614 * Function to process paths received for a new peer addition. The recorded
3615 * paths form the initial tunnel, which can be optimized later.
3616 * Called on each result obtained for the DHT search.
3618 * @param cls closure
3619 * @param exp when will this value expire
3620 * @param key key of the result
3621 * @param get_path path of the get request
3622 * @param get_path_length lenght of get_path
3623 * @param put_path path of the put request
3624 * @param put_path_length length of the put_path
3625 * @param type type of the result
3626 * @param size number of bytes in data
3627 * @param data pointer to the result data
3629 * TODO: re-issue the request after certain time? cancel after X results?
3632 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3633 const GNUNET_HashCode * key,
3634 const struct GNUNET_PeerIdentity *get_path,
3635 unsigned int get_path_length,
3636 const struct GNUNET_PeerIdentity *put_path,
3637 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3638 size_t size, const void *data)
3640 struct MeshPathInfo *path_info = cls;
3641 struct MeshPeerPath *p;
3642 struct GNUNET_PeerIdentity pi;
3645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3646 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3649 p = path_build_from_dht (get_path, get_path_length, put_path,
3651 path_add_to_peers (p, GNUNET_NO);
3653 for (i = 0; i < path_info->peer->ntunnels; i++)
3655 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3656 peer_info_connect (path_info->peer, path_info->t);
3664 * Function to process paths received for a new peer addition. The recorded
3665 * paths form the initial tunnel, which can be optimized later.
3666 * Called on each result obtained for the DHT search.
3668 * @param cls closure
3669 * @param exp when will this value expire
3670 * @param key key of the result
3671 * @param get_path path of the get request
3672 * @param get_path_length lenght of get_path
3673 * @param put_path path of the put request
3674 * @param put_path_length length of the put_path
3675 * @param type type of the result
3676 * @param size number of bytes in data
3677 * @param data pointer to the result data
3680 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3681 const GNUNET_HashCode * key,
3682 const struct GNUNET_PeerIdentity *get_path,
3683 unsigned int get_path_length,
3684 const struct GNUNET_PeerIdentity *put_path,
3685 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3686 size_t size, const void *data)
3688 const struct GNUNET_PeerIdentity *pi = data;
3689 struct MeshTunnel *t = cls;
3690 struct MeshPeerInfo *peer_info;
3691 struct MeshPeerPath *p;
3693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
3694 if (size != sizeof (struct GNUNET_PeerIdentity))
3696 GNUNET_break_op (0);
3699 GNUNET_assert (NULL != t->owner);
3700 peer_info = peer_info_get (pi);
3701 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3703 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3705 p = path_build_from_dht (get_path, get_path_length, put_path,
3707 path_add_to_peers (p, GNUNET_NO);
3709 tunnel_add_peer (t, peer_info);
3710 peer_info_connect (peer_info, t);
3714 /******************************************************************************/
3715 /********************* MESH LOCAL HANDLES **************************/
3716 /******************************************************************************/
3720 * Handler for client disconnection
3722 * @param cls closure
3723 * @param client identification of the client; NULL
3724 * for the last call when the server is destroyed
3727 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3729 struct MeshClient *c;
3730 struct MeshClient *next;
3732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3741 if (c->handle != client)
3743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3749 GNUNET_SERVER_client_drop (c->handle);
3750 c->shutting_down = GNUNET_YES;
3751 GNUNET_assert (NULL != c->own_tunnels);
3752 GNUNET_assert (NULL != c->incoming_tunnels);
3753 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3754 &tunnel_destroy_iterator, c);
3755 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3756 &tunnel_destroy_iterator, c);
3757 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
3758 &tunnel_destroy_iterator, c);
3759 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3760 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3761 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
3763 /* deregister clients applications */
3764 if (NULL != c->apps)
3766 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3767 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3769 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3770 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3772 GNUNET_SCHEDULER_cancel (announce_applications_task);
3773 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3775 if (NULL != c->types)
3776 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3778 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
3789 * Handler for new clients
3791 * @param cls closure
3792 * @param client identification of the client
3793 * @param message the actual message, which includes messages the client wants
3796 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3797 const struct GNUNET_MessageHeader *message)
3799 struct GNUNET_MESH_ClientConnect *cc_msg;
3800 struct MeshClient *c;
3801 GNUNET_MESH_ApplicationType *a;
3808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3809 /* Check data sanity */
3810 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3811 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3812 ntypes = ntohs (cc_msg->types);
3813 napps = ntohs (cc_msg->applications);
3815 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3818 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3822 /* Create new client structure */
3823 c = GNUNET_malloc (sizeof (struct MeshClient));
3824 c->id = next_client_id++;
3825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
3827 GNUNET_SERVER_client_keep (client);
3828 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3831 GNUNET_MESH_ApplicationType at;
3834 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3835 for (i = 0; i < napps; i++)
3838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
3839 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3840 /* store in clients hashmap */
3841 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3842 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3843 /* store in global hashmap, for announcements */
3844 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3845 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3847 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3848 announce_applications_task =
3849 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3857 t = (uint16_t *) & a[napps];
3858 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3859 for (i = 0; i < ntypes; i++)
3862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
3863 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3865 /* store in clients hashmap */
3866 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3867 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3868 /* store in global hashmap */
3869 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3870 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3874 " client has %u+%u subscriptions\n", napps, ntypes);
3876 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3877 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3878 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3879 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3880 GNUNET_SERVER_notification_context_add (nc, client);
3882 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3888 * Handler for requests of new tunnels
3890 * @param cls closure
3891 * @param client identification of the client
3892 * @param message the actual message
3895 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3896 const struct GNUNET_MessageHeader *message)
3898 struct GNUNET_MESH_TunnelMessage *t_msg;
3899 struct MeshTunnel *t;
3900 struct MeshClient *c;
3901 GNUNET_HashCode hash;
3903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3905 /* Sanity check for client registration */
3906 if (NULL == (c = client_get (client)))
3909 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3914 /* Message sanity check */
3915 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3918 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3922 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3923 /* Sanity check for tunnel numbering */
3924 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3930 /* Sanity check for duplicate tunnel IDs */
3931 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3934 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3938 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3939 while (NULL != tunnel_get_by_pi (myid, next_tid))
3940 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3941 t->id.tid = next_tid++;
3942 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3944 t->local_tid = ntohl (t_msg->tunnel_id);
3945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
3946 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3948 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3950 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3952 GNUNET_CONTAINER_multihashmap_put (c->own_tunnels, &hash, t,
3953 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3956 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3960 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3962 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3963 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3966 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3969 t->tree = tree_new (myid);
3971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
3972 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3978 * Handler for requests of deleting tunnels
3980 * @param cls closure
3981 * @param client identification of the client
3982 * @param message the actual message
3985 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3986 const struct GNUNET_MessageHeader *message)
3988 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3989 struct MeshClient *c;
3990 struct MeshTunnel *t;
3991 MESH_TunnelNumber tid;
3993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3994 "Got a DESTROY TUNNEL from client!\n");
3996 /* Sanity check for client registration */
3997 if (NULL == (c = client_get (client)))
4000 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4003 /* Message sanity check */
4004 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4007 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4011 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4013 /* Retrieve tunnel */
4014 tid = ntohl (tunnel_msg->tunnel_id);
4015 t = tunnel_get_by_local_id(c, tid);
4019 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4020 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4023 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4025 client_ignore_tunnel (c, t);
4027 // TODO: when to destroy incoming tunnel?
4028 if (t->nclients == 0)
4030 GNUNET_assert (GNUNET_YES ==
4031 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
4033 GNUNET_assert (GNUNET_YES ==
4034 GNUNET_CONTAINER_multihashmap_remove (t->peers,
4035 &my_full_id.hashPubKey,
4039 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4042 send_client_tunnel_disconnect(t, c);
4043 client_delete_tunnel(c, t);
4045 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4047 tunnel_send_destroy (t);
4049 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4055 * Handler for connection requests to new peers
4057 * @param cls closure
4058 * @param client identification of the client
4059 * @param message the actual message (PeerControl)
4062 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
4063 const struct GNUNET_MessageHeader *message)
4065 struct GNUNET_MESH_PeerControl *peer_msg;
4066 struct MeshPeerInfo *peer_info;
4067 struct MeshClient *c;
4068 struct MeshTunnel *t;
4069 MESH_TunnelNumber tid;
4071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
4072 /* Sanity check for client registration */
4073 if (NULL == (c = client_get (client)))
4076 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4080 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4081 /* Sanity check for message size */
4082 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4085 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4089 /* Tunnel exists? */
4090 tid = ntohl (peer_msg->tunnel_id);
4091 t = tunnel_get_by_local_id (c, tid);
4095 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4099 /* Does client own tunnel? */
4100 if (t->owner->handle != client)
4103 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
4107 GNUNET_i2s (&peer_msg->peer));
4108 peer_info = peer_info_get (&peer_msg->peer);
4110 tunnel_add_peer (t, peer_info);
4111 peer_info_connect (peer_info, t);
4113 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4119 * Handler for disconnection requests of peers in a tunnel
4121 * @param cls closure
4122 * @param client identification of the client
4123 * @param message the actual message (PeerControl)
4126 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
4127 const struct GNUNET_MessageHeader *message)
4129 struct GNUNET_MESH_PeerControl *peer_msg;
4130 struct MeshPeerInfo *peer_info;
4131 struct MeshClient *c;
4132 struct MeshTunnel *t;
4133 MESH_TunnelNumber tid;
4135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
4136 /* Sanity check for client registration */
4137 if (NULL == (c = client_get (client)))
4140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4143 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4144 /* Sanity check for message size */
4145 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4148 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4152 /* Tunnel exists? */
4153 tid = ntohl (peer_msg->tunnel_id);
4154 t = tunnel_get_by_local_id (c, tid);
4158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4163 /* Does client own tunnel? */
4164 if (t->owner->handle != client)
4167 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
4172 GNUNET_i2s (&peer_msg->peer));
4173 /* Is the peer in the tunnel? */
4175 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
4176 if (NULL == peer_info)
4179 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4183 /* Ok, delete peer from tunnel */
4184 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
4185 &peer_msg->peer.hashPubKey);
4187 send_destroy_path (t, peer_info->id);
4188 tunnel_delete_peer (t, peer_info->id);
4189 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4195 * Handler for connection requests to new peers by type
4197 * @param cls closure
4198 * @param client identification of the client
4199 * @param message the actual message (ConnectPeerByType)
4202 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
4203 const struct GNUNET_MessageHeader *message)
4205 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
4206 struct MeshClient *c;
4207 struct MeshTunnel *t;
4208 GNUNET_HashCode hash;
4209 MESH_TunnelNumber tid;
4211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
4212 /* Sanity check for client registration */
4213 if (NULL == (c = client_get (client)))
4216 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4220 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
4221 /* Sanity check for message size */
4222 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
4223 ntohs (connect_msg->header.size))
4226 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4230 /* Tunnel exists? */
4231 tid = ntohl (connect_msg->tunnel_id);
4232 t = tunnel_get_by_local_id (c, tid);
4236 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4240 /* Does client own tunnel? */
4241 if (t->owner->handle != client)
4244 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4248 /* Do WE have the service? */
4249 t->type = ntohl (connect_msg->type);
4250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
4251 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
4252 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
4255 /* Yes! Fast forward, add ourselves to the tunnel and send the
4256 * good news to the client, and alert the destination client of
4257 * an incoming tunnel.
4259 * FIXME send a path create to self, avoid code duplication
4261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
4262 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
4263 peer_info_get (&my_full_id),
4264 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
4267 send_client_peer_connected (t, myid);
4268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
4269 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4271 t->local_tid_dest = next_local_tid++;
4272 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4273 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4274 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4278 /* Ok, lets find a peer offering the service */
4279 if (NULL != t->dht_get_type)
4281 GNUNET_DHT_get_stop (t->dht_get_type);
4283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
4284 GNUNET_h2s (&hash));
4286 GNUNET_DHT_get_start (dht_handle,
4287 GNUNET_BLOCK_TYPE_TEST, &hash, 10,
4288 GNUNET_DHT_RO_RECORD_ROUTE |
4289 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
4290 &dht_get_type_handler, t);
4292 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4298 * Handler for client traffic directed to one peer
4300 * @param cls closure
4301 * @param client identification of the client
4302 * @param message the actual message
4305 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4306 const struct GNUNET_MessageHeader *message)
4308 struct MeshClient *c;
4309 struct MeshTunnel *t;
4310 struct MeshPeerInfo *pi;
4311 struct GNUNET_MESH_Unicast *data_msg;
4312 MESH_TunnelNumber tid;
4315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4316 "Got a unicast request from a client!\n");
4318 /* Sanity check for client registration */
4319 if (NULL == (c = client_get (client)))
4322 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4325 data_msg = (struct GNUNET_MESH_Unicast *) message;
4326 /* Sanity check for message size */
4327 size = ntohs (message->size);
4328 if (sizeof (struct GNUNET_MESH_Unicast) +
4329 sizeof (struct GNUNET_MessageHeader) > size)
4332 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4336 /* Tunnel exists? */
4337 tid = ntohl (data_msg->tid);
4338 t = tunnel_get_by_local_id (c, tid);
4342 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4346 /* Is it a local tunnel? Then, does client own the tunnel? */
4347 if (t->owner->handle != client)
4350 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4354 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
4355 &data_msg->destination.hashPubKey);
4356 /* Is the selected peer in the tunnel? */
4360 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4364 /* Ok, everything is correct, send the message
4365 * (pretend we got it from a mesh peer)
4368 char buf[ntohs (message->size)] GNUNET_ALIGN;
4369 struct GNUNET_MESH_Unicast *copy;
4371 /* Work around const limitation */
4372 copy = (struct GNUNET_MESH_Unicast *) buf;
4373 memcpy (buf, data_msg, size);
4374 copy->oid = my_full_id;
4375 copy->tid = htonl (t->id.tid);
4376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4377 " calling generic handler...\n");
4378 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4380 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4386 * Handler for client traffic directed to the origin
4388 * @param cls closure
4389 * @param client identification of the client
4390 * @param message the actual message
4393 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4394 const struct GNUNET_MessageHeader *message)
4396 struct GNUNET_MESH_ToOrigin *data_msg;
4397 struct GNUNET_PeerIdentity id;
4398 struct MeshClient *c;
4399 struct MeshTunnel *t;
4400 MESH_TunnelNumber tid;
4403 /* Sanity check for client registration */
4404 if (NULL == (c = client_get (client)))
4407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4410 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4411 /* Sanity check for message size */
4412 size = ntohs (message->size);
4413 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4414 sizeof (struct GNUNET_MessageHeader) > size)
4417 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4421 /* Tunnel exists? */
4422 tid = ntohl (data_msg->tid);
4423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4424 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
4425 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4431 t = tunnel_get_by_local_id (c, tid);
4435 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4439 /* It should be sent by someone who has this as incoming tunnel. */
4440 if (-1 == client_knows_tunnel (c, t))
4443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4446 GNUNET_PEER_resolve (t->id.oid, &id);
4448 /* Ok, everything is correct, send the message
4449 * (pretend we got it from a mesh peer)
4452 char buf[ntohs (message->size)] GNUNET_ALIGN;
4453 struct GNUNET_MESH_ToOrigin *copy;
4455 /* Work around const limitation */
4456 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4457 memcpy (buf, data_msg, size);
4459 copy->tid = htonl (t->id.tid);
4460 copy->sender = my_full_id;
4461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4462 " calling generic handler...\n");
4463 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4465 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4471 * Handler for client traffic directed to all peers in a tunnel
4473 * @param cls closure
4474 * @param client identification of the client
4475 * @param message the actual message
4478 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4479 const struct GNUNET_MessageHeader *message)
4481 struct MeshClient *c;
4482 struct MeshTunnel *t;
4483 struct GNUNET_MESH_Multicast *data_msg;
4484 MESH_TunnelNumber tid;
4486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4487 "Got a multicast request from a client!\n");
4489 /* Sanity check for client registration */
4490 if (NULL == (c = client_get (client)))
4493 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4496 data_msg = (struct GNUNET_MESH_Multicast *) message;
4497 /* Sanity check for message size */
4498 if (sizeof (struct GNUNET_MESH_Multicast) +
4499 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4506 /* Tunnel exists? */
4507 tid = ntohl (data_msg->tid);
4508 t = tunnel_get_by_local_id (c, tid);
4512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4516 /* Does client own tunnel? */
4517 if (t->owner->handle != client)
4520 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4525 char buf[ntohs (message->size)] GNUNET_ALIGN;
4526 struct GNUNET_MESH_Multicast *copy;
4528 copy = (struct GNUNET_MESH_Multicast *) buf;
4529 memcpy (buf, message, ntohs (message->size));
4530 copy->oid = my_full_id;
4531 copy->tid = htonl (t->id.tid);
4532 copy->ttl = htonl (DEFAULT_TTL);
4533 copy->mid = htonl (t->mid + 1);
4534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4535 " calling generic handler...\n");
4536 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4539 /* receive done gets called when last copy is sent to a neighbor */
4544 * Functions to handle messages from clients
4546 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4547 {&handle_local_new_client, NULL,
4548 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4549 {&handle_local_tunnel_create, NULL,
4550 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4551 sizeof (struct GNUNET_MESH_TunnelMessage)},
4552 {&handle_local_tunnel_destroy, NULL,
4553 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4554 sizeof (struct GNUNET_MESH_TunnelMessage)},
4555 {&handle_local_connect_add, NULL,
4556 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4557 sizeof (struct GNUNET_MESH_PeerControl)},
4558 {&handle_local_connect_del, NULL,
4559 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4560 sizeof (struct GNUNET_MESH_PeerControl)},
4561 {&handle_local_connect_by_type, NULL,
4562 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4563 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4564 {&handle_local_unicast, NULL,
4565 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4566 {&handle_local_to_origin, NULL,
4567 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4568 {&handle_local_multicast, NULL,
4569 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4575 * To be called on core init/fail.
4577 * @param cls service closure
4578 * @param server handle to the server for this service
4579 * @param identity the public identity of this peer
4582 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4583 const struct GNUNET_PeerIdentity *identity)
4585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4586 core_handle = server;
4587 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4590 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4591 GNUNET_SCHEDULER_shutdown ();
4597 * Method called whenever a given peer connects.
4599 * @param cls closure
4600 * @param peer peer identity this notification is about
4601 * @param atsi performance data for the connection
4602 * @param atsi_count number of records in 'atsi'
4605 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4606 const struct GNUNET_ATS_Information *atsi,
4607 unsigned int atsi_count)
4609 struct MeshPeerInfo *peer_info;
4610 struct MeshPeerPath *path;
4612 DEBUG_CONN ("Peer connected\n");
4613 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4614 peer_info = peer_info_get (peer);
4615 if (myid == peer_info->id)
4617 DEBUG_CONN (" (self)\n");
4622 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4624 path = path_new (2);
4625 path->peers[0] = myid;
4626 path->peers[1] = peer_info->id;
4627 GNUNET_PEER_change_rc (myid, 1);
4628 GNUNET_PEER_change_rc (peer_info->id, 1);
4629 peer_info_add_path (peer_info, path, GNUNET_YES);
4634 * Method called whenever a peer disconnects.
4636 * @param cls closure
4637 * @param peer peer identity this notification is about
4640 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4642 struct MeshPeerInfo *pi;
4645 DEBUG_CONN ("Peer disconnected\n");
4646 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4652 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4654 /* TODO: notify that the transmission failed */
4655 peer_info_cancel_transmission (pi, i);
4657 peer_info_remove_path (pi, pi->id, myid);
4660 DEBUG_CONN (" (self)\n");
4666 /******************************************************************************/
4667 /************************ MAIN FUNCTIONS ****************************/
4668 /******************************************************************************/
4671 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4673 * @param cls closure
4674 * @param key current key code
4675 * @param value value in the hash map
4676 * @return GNUNET_YES if we should continue to iterate,
4680 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4682 struct MeshTunnel *t = value;
4689 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4691 * @param cls closure
4692 * @param key current key code
4693 * @param value value in the hash map
4694 * @return GNUNET_YES if we should continue to iterate,
4698 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4700 struct MeshPeerInfo *p = value;
4702 peer_info_destroy (p);
4707 * Task run during shutdown.
4713 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4717 if (core_handle != NULL)
4719 GNUNET_CORE_disconnect (core_handle);
4722 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4723 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4724 if (dht_handle != NULL)
4726 GNUNET_DHT_disconnect (dht_handle);
4731 GNUNET_SERVER_notification_context_destroy (nc);
4734 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4736 GNUNET_SCHEDULER_cancel (announce_id_task);
4737 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4743 * Process mesh requests.
4745 * @param cls closure
4746 * @param server the initialized server
4747 * @param c configuration to use
4750 run (void *cls, struct GNUNET_SERVER_Handle *server,
4751 const struct GNUNET_CONFIGURATION_Handle *c)
4753 struct MeshPeerInfo *peer;
4754 struct MeshPeerPath *p;
4757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4758 server_handle = server;
4759 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4760 NULL, /* Closure passed to MESH functions */
4761 &core_init, /* Call core_init once connected */
4762 &core_connect, /* Handle connects */
4763 &core_disconnect, /* remove peers on disconnects */
4764 NULL, /* Don't notify about all incoming messages */
4765 GNUNET_NO, /* For header only in notification */
4766 NULL, /* Don't notify about all outbound messages */
4767 GNUNET_NO, /* For header-only out notification */
4768 core_handlers); /* Register these handlers */
4770 if (core_handle == NULL)
4773 GNUNET_SCHEDULER_shutdown ();
4778 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4781 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4783 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4784 GNUNET_SCHEDULER_shutdown ();
4787 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4788 GNUNET_free (keyfile);
4789 if (my_private_key == NULL)
4791 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4792 _("Mesh service could not access hostkey. Exiting.\n"));
4793 GNUNET_SCHEDULER_shutdown ();
4796 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4797 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4798 &my_full_id.hashPubKey);
4799 myid = GNUNET_PEER_intern (&my_full_id);
4801 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4808 dht_handle = GNUNET_DHT_connect (c, 64);
4809 if (dht_handle == NULL)
4815 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4817 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4818 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4819 peers = GNUNET_CONTAINER_multihashmap_create (32);
4820 applications = GNUNET_CONTAINER_multihashmap_create (32);
4821 types = GNUNET_CONTAINER_multihashmap_create (32);
4823 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4824 nc = GNUNET_SERVER_notification_context_create (server_handle,
4826 GNUNET_SERVER_disconnect_notify (server_handle,
4827 &handle_local_client_disconnect, NULL);
4831 clients_tail = NULL;
4834 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4835 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4837 /* Create a peer_info for the local peer */
4838 peer = peer_info_get (&my_full_id);
4841 GNUNET_PEER_change_rc (myid, 1);
4842 peer_info_add_path (peer, p, GNUNET_YES);
4844 /* Scheduled the task to clean up when shutdown is called */
4845 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
4852 * The main function for the mesh service.
4854 * @param argc number of arguments from the command line
4855 * @param argv command line arguments
4856 * @return 0 ok, 1 on error
4859 main (int argc, char *const *argv)
4863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
4866 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");