2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - relay corking down to core
44 * - set ttl relative to tree depth
50 #include "mesh_protocol.h"
51 #include "gnunet_dht_service.h"
52 #include "mesh_tunnel_tree.h"
54 /* TODO: move into configuration file */
55 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
56 GNUNET_TIME_UNIT_SECONDS,\
58 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
59 GNUNET_TIME_UNIT_SECONDS,\
62 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
63 GNUNET_TIME_UNIT_SECONDS,\
66 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
67 GNUNET_TIME_UNIT_SECONDS,\
69 #define DEFAULT_TTL 64
73 #define MESH_DEBUG_DHT GNUNET_YES
74 #define MESH_DEBUG_CONNECTION GNUNET_NO
76 #if MESH_DEBUG_CONNECTION
77 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
79 #define DEBUG_CONN(...)
83 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
85 #define DEBUG_DHT(...)
88 /******************************************************************************/
89 /************************ DATA STRUCTURES ****************************/
90 /******************************************************************************/
92 /** FWD declaration */
97 * Struct representing a piece of data being sent to other peers
101 /** Tunnel it belongs to. */
102 struct MeshTunnel *t;
104 /** In case of a multicast, task to allow a client to send more data if
105 * some neighbor is too slow. */
106 GNUNET_SCHEDULER_TaskIdentifier *task;
108 /** How many remaining neighbors we need to send this to. */
109 unsigned int *reference_counter;
111 /** Size of the data. */
120 * Struct containing info about a queued transmission to this peer
127 struct MeshPeerQueue *next;
132 struct MeshPeerQueue *prev;
135 * Peer this transmission is directed to.
137 struct MeshPeerInfo *peer;
140 * Handle to stop queued transmission
142 struct GNUNET_CORE_TransmitHandle *core_transmit;
145 * Pointer to info stucture used as cls.
157 * Struct containing all info possibly needed to build a package when called
160 struct MeshTransmissionDescriptor
162 /** ID of the tunnel this packet travels in */
163 struct MESH_TunnelID *origin;
165 /** Who was this message being sent to */
166 struct MeshPeerInfo *peer;
168 /** Ultimate destination of the packet */
169 GNUNET_PEER_Id destination;
171 /** Which handler was used to request the transmission */
172 struct MeshPeerQueue *queue;
174 /** Data descriptor */
175 struct MeshData* mesh_data;
180 * Struct containing all information regarding a given peer
190 * Last time we heard from this peer
192 struct GNUNET_TIME_Absolute last_contact;
195 * Number of attempts to reconnect so far
197 int n_reconnect_attempts;
200 * Paths to reach the peer, ordered by ascending hop count
202 struct MeshPeerPath *path_head;
205 * Paths to reach the peer, ordered by ascending hop count
207 struct MeshPeerPath *path_tail;
210 * Handle to stop the DHT search for a path to this peer
212 struct GNUNET_DHT_GetHandle *dhtget;
215 * Closure given to the DHT GET
217 struct MeshPathInfo *dhtgetcls;
220 * Transmission queue to this peer, head
222 struct MeshPeerQueue *queue_head;
225 * Transmission queue to this peer, tail
227 struct MeshPeerQueue *queue_tail;
230 * Array of tunnels this peer participates in
231 * (most probably a small amount, therefore not a hashmap)
232 * When the path to the peer changes, notify these tunnels to let them
233 * re-adjust their path trees.
235 struct MeshTunnel **tunnels;
238 * Number of tunnels this peers participates in
240 unsigned int ntunnels;
245 * Data scheduled to transmit (to local client or remote peer)
252 struct MeshQueue *next;
253 struct MeshQueue *prev;
256 * Target of the data (NULL if target is client)
258 struct MeshPeerInfo *peer;
261 * Client to send the data to (NULL if target is peer)
263 struct MeshClient *client;
266 * Size of the message to transmit
271 * How old is the data?
273 struct GNUNET_TIME_Absolute timestamp;
278 struct GNUNET_MessageHeader *data;
282 * Globally unique tunnel identification (owner + number)
283 * DO NOT USE OVER THE NETWORK
288 * Node that owns the tunnel
293 * Tunnel number to differentiate all the tunnels owned by the node oid
294 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
296 MESH_TunnelNumber tid;
300 struct MeshClient; /* FWD declaration */
303 * Struct containing all information regarding a tunnel
304 * For an intermediate node the improtant info used will be:
305 * - id Tunnel unique identification
306 * - paths[0] To know where to send it next
307 * - metainfo: ready, speeds, accounting
314 struct MESH_TunnelID id;
317 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
319 MESH_TunnelNumber local_tid;
322 * Local tunnel number for local destination clients (incoming number)
323 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
326 MESH_TunnelNumber local_tid_dest;
329 * ID of the last multicast packet seen/sent.
334 * Last time the tunnel was used
336 struct GNUNET_TIME_Absolute timestamp;
339 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
340 * containing peers added by id or by type, not intermediate peers.
342 struct GNUNET_CONTAINER_MultiHashMap *peers;
345 * Number of peers that are connected and potentially ready to receive data
347 unsigned int peers_ready;
350 * Number of peers that have been added to the tunnel
352 unsigned int peers_total;
355 * Client owner of the tunnel, if any
357 struct MeshClient *owner;
360 * Clients that have been informed about the tunnel, if any
362 struct MeshClient **clients;
365 * Number of elements in clients
367 unsigned int nclients;
370 * Clients that have requested to leave the tunnel
372 struct MeshClient **ignore;
375 * Number of elements in clients
377 unsigned int nignore;
380 * Messages ready to transmit
382 struct MeshQueue *queue_head;
383 struct MeshQueue *queue_tail;
388 struct MeshTunnelTree *tree;
391 * Application type we are looking for in this tunnel
393 GNUNET_MESH_ApplicationType type;
396 * Used to search peers offering a service
398 struct GNUNET_DHT_GetHandle *dht_get_type;
401 * Task to keep the used paths alive
403 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
406 * Task to destroy the tunnel after timeout
408 * FIXME: merge the two? a tunnel will have either
409 * a path refresh OR a timeout, never both!
411 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
416 * Info needed to work with tunnel paths and peers
423 struct MeshTunnel *t;
426 * Neighbouring peer to whom we send the packet to
428 struct MeshPeerInfo *peer;
433 struct MeshPeerPath *path;
436 * Position in peer's transmit queue
438 struct MeshPeerQueue *queue;
443 * Struct containing information about a client of the service
450 struct MeshClient *next;
455 struct MeshClient *prev;
458 * Tunnels that belong to this client, indexed by local id
460 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
463 * Tunnels this client has accepted, indexed by incoming local id
465 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
468 * Tunnels this client has rejected, indexed by incoming local id
470 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
472 * Handle to communicate with the client
474 struct GNUNET_SERVER_Client *handle;
477 * Applications that this client has claimed to provide
479 struct GNUNET_CONTAINER_MultiHashMap *apps;
482 * Messages that this client has declared interest in
484 struct GNUNET_CONTAINER_MultiHashMap *types;
487 * Whether the client is active or shutting down (don't send confirmations
488 * to a client that is shutting down.
493 * ID of the client, mainly for debug messages
501 /******************************************************************************/
502 /************************ DEBUG FUNCTIONS ****************************/
503 /******************************************************************************/
507 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
508 * @param cls string to print
509 * @param success GNUNET_OK if the PUT was transmitted,
510 * GNUNET_NO on timeout,
511 * GNUNET_SYSERR on disconnect from service
512 * after the PUT message was transmitted
513 * (so we don't know if it was received or not)
518 mesh_debug (void *cls, int success)
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
528 /******************************************************************************/
529 /*********************** GLOBAL VARIABLES ****************************/
530 /******************************************************************************/
535 static struct MeshClient *clients;
536 static struct MeshClient *clients_tail;
539 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
541 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
544 * Tunnels incoming, indexed by MESH_TunnelNumber
545 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
547 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
550 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
552 static struct GNUNET_CONTAINER_MultiHashMap *peers;
555 * Handle to communicate with core
557 static struct GNUNET_CORE_Handle *core_handle;
560 * Handle to communicate with transport
562 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
567 static struct GNUNET_DHT_Handle *dht_handle;
572 static struct GNUNET_SERVER_Handle *server_handle;
575 * Notification context, to send messages to local clients
577 static struct GNUNET_SERVER_NotificationContext *nc;
580 * Local peer own ID (memory efficient handle)
582 static GNUNET_PEER_Id myid;
585 * Local peer own ID (full value)
587 static struct GNUNET_PeerIdentity my_full_id;
592 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
597 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
600 * Tunnel ID for the next created tunnel (global tunnel number)
602 static MESH_TunnelNumber next_tid;
605 * Tunnel ID for the next incoming tunnel (local tunnel number)
607 static MESH_TunnelNumber next_local_tid;
610 * All application types provided by this peer
612 static struct GNUNET_CONTAINER_MultiHashMap *applications;
615 * All message types clients of this peer are interested in
617 static struct GNUNET_CONTAINER_MultiHashMap *types;
620 * Task to periodically announce provided applications
622 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
625 * Task to periodically announce itself in the network
627 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
630 * Next ID to assign to a client
632 unsigned int next_client_id;
636 /******************************************************************************/
637 /************************ ITERATORS ****************************/
638 /******************************************************************************/
640 /* FIXME move iterators here */
643 /******************************************************************************/
644 /************************ PERIODIC FUNCTIONS ****************************/
645 /******************************************************************************/
648 * Announce iterator over for each application provided by the peer
651 * @param key current key code
652 * @param value value in the hash map
653 * @return GNUNET_YES if we should continue to
658 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
660 /* FIXME are hashes in multihash map equal on all aquitectures? */
661 /* FIXME: keep return value of 'put' to possibly cancel!? */
662 GNUNET_DHT_put (dht_handle, key, 10,
663 GNUNET_DHT_RO_RECORD_ROUTE |
664 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
665 sizeof (struct GNUNET_PeerIdentity),
666 (const char *) &my_full_id,
667 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
669 APP_ANNOUNCE_TIME, NULL, NULL);
675 * Periodically announce what applications are provided by local clients
678 * @param tc task context
681 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
683 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
685 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
689 DEBUG_DHT ("Starting PUT for apps\n");
691 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
693 announce_applications_task =
694 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
696 DEBUG_DHT ("Finished PUT for apps\n");
703 * Periodically announce self id in the DHT
706 * @param tc task context
709 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
711 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
713 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
717 * - Set data expiration in function of X
720 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
722 GNUNET_DHT_put (dht_handle, /* DHT handle */
723 &my_full_id.hashPubKey, /* Key to use */
724 10, /* Replication level */
725 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
726 GNUNET_BLOCK_TYPE_TEST, /* Block type */
727 sizeof (my_full_id), /* Size of the data */
728 (char *) &my_full_id, /* Data itself */
729 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
730 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
731 NULL, /* Continuation */
732 NULL); /* Continuation closure */
734 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
739 * Function to process paths received for a new peer addition. The recorded
740 * paths form the initial tunnel, which can be optimized later.
741 * Called on each result obtained for the DHT search.
744 * @param exp when will this value expire
745 * @param key key of the result
746 * @param type type of the result
747 * @param size number of bytes in data
748 * @param data pointer to the result data
751 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
752 const GNUNET_HashCode * key,
753 const struct GNUNET_PeerIdentity *get_path,
754 unsigned int get_path_length,
755 const struct GNUNET_PeerIdentity *put_path,
756 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
757 size_t size, const void *data);
760 /******************************************************************************/
761 /****************** GENERAL HELPER FUNCTIONS ************************/
762 /******************************************************************************/
765 * Search for a tunnel by global ID using full PeerIdentities
767 * @param oid owner of the tunnel
768 * @param tid global tunnel number
770 * @return tunnel handler, NULL if doesn't exist
772 static struct MeshTunnel *
773 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
777 * Delete an active client from the tunnel.
783 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
786 * Notify a tunnel that a connection has broken that affects at least
789 * @param t Tunnel affected.
790 * @param p1 Peer that got disconnected from p2.
791 * @param p2 Peer that got disconnected from p1.
793 * @return Short ID of the peer disconnected (either p1 or p2).
794 * 0 if the tunnel remained unaffected.
796 static GNUNET_PEER_Id
797 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
802 * Check if client has registered with the service and has not disconnected
804 * @param client the client to check
806 * @return non-NULL if client exists in the global DLL
808 static struct MeshClient *
809 client_get (struct GNUNET_SERVER_Client *client)
811 struct MeshClient *c;
816 if (c->handle == client)
825 * Checks if a given client has subscribed to certain message type
827 * @param message_type Type of message to check
828 * @param c Client to check
830 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
835 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
839 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
840 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
845 * Allow a client to send more data after transmitting a multicast message
846 * which some neighbor has not yet accepted altough a reasonable time has
849 * @param cls Closure (DataDescriptor containing the task identifier)
850 * @param tc Task Context
852 * FIXME reference counter cshould be just int
855 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
857 struct MeshData *mdata = cls;
859 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
861 GNUNET_assert (NULL != mdata->reference_counter);
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
864 *(mdata->reference_counter));
865 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
866 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
871 * Check whether client wants traffic from a tunnel.
873 * @param c Client to check.
874 * @param t Tunnel to be found.
876 * @return GNUNET_YES if client knows tunnel.
878 * TODO look in client hashmap
881 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
885 for (i = 0; i < t->nclients; i++)
886 if (t->clients[i] == c)
893 * Check whether client has been informed about a tunnel.
895 * @param c Client to check.
896 * @param t Tunnel to be found.
898 * @return GNUNET_YES if client knows tunnel.
900 * TODO look in client hashmap
903 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
907 for (i = 0; i < t->nignore; i++)
908 if (t->ignore[i] == c)
910 return client_wants_tunnel(c, t);
915 * Marks a client as uninterested in traffic from the tunnel, updating both
916 * client and tunnel to reflect this.
918 * @param c Client that doesn't want traffic anymore.
919 * @param t Tunnel which should be ignored.
921 * FIXME when to delete an incoming tunnel?
924 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
926 GNUNET_HashCode hash;
928 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
929 GNUNET_break (GNUNET_YES ==
930 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
932 GNUNET_break (GNUNET_YES ==
933 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
934 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
935 tunnel_delete_active_client (t, c);
936 GNUNET_array_append (t->ignore, t->nignore, c);
941 * Deletes a tunnel from a client (either owner or destination). To be used on
942 * tunnel destroy, otherwise, use client_ignore_tunnel.
944 * @param c Client whose tunnel to delete.
945 * @param t Tunnel which should be deleted.
948 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
950 GNUNET_HashCode hash;
954 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
955 GNUNET_assert (GNUNET_YES ==
956 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
962 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
964 GNUNET_assert (GNUNET_YES ==
965 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
969 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
978 * Send the message to all clients that have subscribed to its type
980 * @param msg Pointer to the message itself
981 * @param payload Pointer to the payload of the message.
982 * @return number of clients this message was sent to
985 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
986 const struct GNUNET_MessageHeader *payload)
988 struct GNUNET_PeerIdentity *oid;
989 struct MeshClient *c;
990 struct MeshTunnel *t;
991 MESH_TunnelNumber *tid;
994 char cbuf[htons (msg->size)];
996 type = ntohs (payload->type);
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1000 memcpy (cbuf, msg, sizeof (cbuf));
1001 switch (htons (msg->type))
1003 struct GNUNET_MESH_Unicast *uc;
1004 struct GNUNET_MESH_Multicast *mc;
1005 struct GNUNET_MESH_ToOrigin *to;
1007 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1008 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1012 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1013 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1017 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1018 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1026 t = tunnel_get (oid, ntohl (*tid));
1033 for (count = 0, c = clients; c != NULL; c = c->next)
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1036 if (client_is_subscribed (type, c))
1038 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1042 *tid = htonl (t->local_tid);
1046 if (GNUNET_NO == client_knows_tunnel (c, t))
1048 /* This client doesn't know the tunnel */
1049 struct GNUNET_MESH_TunnelNotification tmsg;
1050 GNUNET_HashCode hash;
1052 tmsg.header.size = htons (sizeof (tmsg));
1053 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1054 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1055 tmsg.tunnel_id = htonl (t->local_tid_dest);
1056 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1057 &tmsg.header, GNUNET_NO);
1058 GNUNET_array_append (t->clients, t->nclients, c);
1059 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1061 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1062 c->incoming_tunnels, &hash, t,
1063 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1065 *tid = htonl (t->local_tid_dest);
1068 /* Check if the client wants to get traffic from the tunnel */
1069 if (GNUNET_NO == client_wants_tunnel(c, t))
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1073 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1074 (struct GNUNET_MessageHeader
1075 *) cbuf, GNUNET_YES);
1083 * Notify the client that owns the tunnel that a peer has connected to it
1084 * (the requested path to it has been confirmed).
1086 * @param t Tunnel whose owner to notify
1087 * @param id Short id of the peer that has connected
1090 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1092 struct GNUNET_MESH_PeerControl pc;
1094 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1095 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1096 pc.tunnel_id = htonl (t->local_tid);
1097 GNUNET_PEER_resolve (id, &pc.peer);
1098 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1104 * Notify all clients (not depending on registration status) that the incoming
1105 * tunnel is no longer valid.
1107 * @param t Tunnel that was destroyed.
1110 send_clients_tunnel_destroy (struct MeshTunnel *t)
1112 struct GNUNET_MESH_TunnelMessage msg;
1114 msg.header.size = htons (sizeof (msg));
1115 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1116 msg.tunnel_id = htonl (t->local_tid_dest);
1117 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1122 * Notify clients of tunnel disconnections, if needed.
1123 * In case the origin disconnects, the destination clients get a tunnel destroy
1124 * notification. If the last destination disconnects (only one remaining client
1125 * in tunnel), the origin gets a (local ID) peer disconnected.
1126 * Note that the function must be called BEFORE removing the client from
1129 * @param t Tunnel that was destroyed.
1130 * @param c Client that disconnected.
1133 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1139 struct GNUNET_MESH_TunnelMessage msg;
1141 msg.header.size = htons (sizeof (msg));
1142 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1143 msg.tunnel_id = htonl (t->local_tid_dest);
1144 for (i = 0; i < t->nclients; i++)
1145 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1146 &msg.header, GNUNET_NO);
1148 // FIXME when to disconnect an incoming tunnel?
1149 else if (1 == t->nclients && NULL != t->owner)
1151 struct GNUNET_MESH_PeerControl msg;
1153 msg.header.size = htons (sizeof (msg));
1154 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1155 msg.tunnel_id = htonl (t->local_tid);
1156 msg.peer = my_full_id;
1157 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1158 &msg.header, GNUNET_NO);
1164 * Function called to notify a client about the socket
1165 * being ready to queue more data. "buf" will be
1166 * NULL and "size" zero if the socket was closed for
1167 * writing in the meantime.
1169 * @param cls closure
1170 * @param size number of bytes available in buf
1171 * @param buf where the callee should write the message
1172 * @return number of bytes written to buf
1175 send_core_create_path (void *cls, size_t size, void *buf);
1179 * Function called to notify a client about the socket
1180 * being ready to queue more data. "buf" will be
1181 * NULL and "size" zero if the socket was closed for
1182 * writing in the meantime.
1184 * @param cls closure (data itself)
1185 * @param size number of bytes available in buf
1186 * @param buf where the callee should write the message
1188 * @return number of bytes written to buf
1191 send_core_data_multicast (void *cls, size_t size, void *buf);
1195 * Decrements the reference counter and frees all resources if needed
1197 * @param mesh_data Data Descriptor used in a multicast message.
1198 * Freed no longer needed (last message).
1201 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
1203 /* Make sure it's a multicast packet */
1204 GNUNET_assert (NULL != mesh_data->reference_counter);
1206 if (0 == --(*(mesh_data->reference_counter)))
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1209 if (NULL != mesh_data->task)
1211 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1213 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1215 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1217 GNUNET_free (mesh_data->task);
1219 GNUNET_free (mesh_data->reference_counter);
1220 GNUNET_free (mesh_data->data);
1221 GNUNET_free (mesh_data);
1227 * Cancel a core transmission that was already queued, free all resources
1228 * associated to the request and cancel all external requests (core, ...).
1230 * @param queue Queue handler to cancel.
1233 peer_info_cancel_transmission (struct MeshPeerQueue *queue)
1235 struct MeshTransmissionDescriptor *dd;
1236 struct MeshPathInfo *path_info;
1237 struct MeshPeerInfo *peer;
1240 if (NULL != queue->core_transmit)
1244 struct GNUNET_PeerIdentity id;
1246 GNUNET_PEER_resolve (peer->id, &id);
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 " Cancelling data transmission at %s\n",
1250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message type %u\n",
1254 GNUNET_CORE_notify_transmit_ready_cancel (queue->core_transmit);
1256 /* TODO: notify that transmission has failed */
1257 switch (queue->type)
1259 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1260 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1261 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
1264 data_descriptor_decrement_multicast (dd->mesh_data);
1266 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
1267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
1268 path_info = queue->cls;
1269 path_destroy (path_info->path);
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
1275 GNUNET_free_non_null (queue->cls);
1276 GNUNET_CONTAINER_DLL_remove (peer->queue_head, peer->queue_tail, queue);
1282 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1283 * and insert it in the appropiate structures if the peer is not known yet.
1285 * @param peer Full identity of the peer.
1287 * @return Existing or newly created peer info.
1289 static struct MeshPeerInfo *
1290 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1292 struct MeshPeerInfo *peer_info;
1294 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1295 if (NULL == peer_info)
1298 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1299 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1300 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1301 peer_info->id = GNUNET_PEER_intern (peer);
1309 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1310 * and insert it in the appropiate structures if the peer is not known yet.
1312 * @param peer Short identity of the peer.
1314 * @return Existing or newly created peer info.
1316 static struct MeshPeerInfo *
1317 peer_info_get_short (const GNUNET_PEER_Id peer)
1319 struct GNUNET_PeerIdentity id;
1321 GNUNET_PEER_resolve (peer, &id);
1322 return peer_info_get (&id);
1327 * Iterator to remove the tunnel from the list of tunnels a peer participates
1330 * @param cls Closure (tunnel info)
1331 * @param key GNUNET_PeerIdentity of the peer (unused)
1332 * @param value PeerInfo of the peer
1334 * @return always GNUNET_YES, to keep iterating
1337 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1339 struct MeshTunnel *t = cls;
1340 struct MeshPeerInfo *peer = value;
1343 for (i = 0; i < peer->ntunnels; i++)
1346 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1349 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1350 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1359 * Core callback to write a pre-constructed data packet to core buffer
1361 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1362 * @param size Number of bytes available in buf.
1363 * @param buf Where the to write the message.
1365 * @return number of bytes written to buf
1368 send_core_data_raw (void *cls, size_t size, void *buf)
1370 struct MeshTransmissionDescriptor *info = cls;
1371 struct GNUNET_MessageHeader *msg;
1372 struct MeshPeerQueue *queue;
1375 GNUNET_assert (NULL != info);
1376 GNUNET_assert (NULL != info->mesh_data);
1377 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1378 total_size = ntohs (msg->size);
1379 queue = info->queue;
1381 if (total_size > size)
1383 struct GNUNET_PeerIdentity id;
1385 GNUNET_PEER_resolve (info->peer->id, &id);
1386 queue->core_transmit =
1387 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1388 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1389 size, &send_core_data_raw, info);
1392 memcpy (buf, msg, total_size);
1393 GNUNET_free (info->mesh_data);
1395 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
1396 queue->peer->queue_tail,
1398 GNUNET_free (queue);
1404 * Sends an already built message to a peer, properly registrating
1405 * all used resources.
1407 * @param message Message to send. Fucntion makes a copy of it.
1408 * @param peer Short ID of the neighbor whom to send the message.
1413 send_message (const struct GNUNET_MessageHeader *message,
1414 const struct GNUNET_PeerIdentity *peer)
1416 struct MeshTransmissionDescriptor *info;
1417 struct MeshPeerQueue *queue;
1418 struct MeshPeerInfo *neighbor;
1419 struct MeshPeerPath *p;
1422 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1424 size = ntohs (message->size);
1425 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1426 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1427 info->mesh_data->data = GNUNET_malloc (size);
1428 memcpy (info->mesh_data->data, message, size);
1429 info->mesh_data->data_len = size;
1430 neighbor = peer_info_get (peer);
1431 for (p = neighbor->path_head; NULL != p; p = p->next)
1441 GNUNET_free (info->mesh_data->data);
1442 GNUNET_free (info->mesh_data);
1446 queue = GNUNET_malloc (sizeof(struct MeshPeerQueue));
1447 queue->peer = neighbor;
1448 info->queue = queue;
1449 info->peer = neighbor;
1450 queue->type = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1452 queue->core_transmit =
1453 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1454 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1455 size, &send_core_data_raw, info);
1456 GNUNET_CONTAINER_DLL_insert (neighbor->queue_head,
1457 neighbor->queue_tail,
1463 * Sends a CREATE PATH message for a path to a peer, properly registrating
1464 * all used resources.
1466 * @param peer PeerInfo of the final peer for whom this path is being created.
1467 * @param p Path itself.
1468 * @param t Tunnel for which the path is created.
1471 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1472 struct MeshTunnel *t)
1474 struct GNUNET_PeerIdentity id;
1475 struct MeshPathInfo *path_info;
1476 struct MeshPeerInfo *neighbor;
1477 struct MeshPeerQueue *queue;
1483 p = tree_get_path_to_peer (t->tree, peer->id);
1490 for (i = 0; i < p->length; i++)
1492 if (p->peers[i] == myid)
1495 if (i >= p->length - 1)
1501 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1503 queue = GNUNET_malloc (sizeof(struct MeshPeerQueue));
1504 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1505 path_info->path = p;
1507 neighbor = peer_info_get (&id);
1508 path_info->peer = neighbor;
1509 path_info->queue = queue;
1510 queue->type = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1511 queue->cls = path_info;
1512 queue->core_transmit =
1513 GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
1516 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1518 sizeof (struct GNUNET_MESH_ManipulatePath) +
1519 (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1520 &send_core_create_path, /* callback */
1521 path_info); /* cls */
1522 GNUNET_CONTAINER_DLL_insert (neighbor->queue_head,
1523 neighbor->queue_tail,
1529 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1531 * @param t Tunnel whose path to destroy.
1532 * @param destination Short ID of the peer to whom the path to destroy.
1535 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1537 struct MeshPeerPath *p;
1540 p = tree_get_path_to_peer (t->tree, destination);
1546 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1547 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1549 struct GNUNET_MESH_ManipulatePath *msg;
1550 struct GNUNET_PeerIdentity *pi;
1554 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1555 msg->header.size = htons (size);
1556 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1557 msg->tid = htonl (t->id.tid);
1558 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1559 for (i = 0; i < p->length; i++)
1561 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1563 send_message (&msg->header, tree_get_first_hop (t->tree, destination));
1570 * Try to establish a new connection to this peer.
1571 * Use the best path for the given tunnel.
1572 * If the peer doesn't have any path to it yet, try to get one.
1573 * If the peer already has some path, send a CREATE PATH towards it.
1575 * @param peer PeerInfo of the peer.
1576 * @param t Tunnel for which to create the path, if possible.
1579 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1581 struct MeshPeerPath *p;
1582 struct MeshPathInfo *path_info;
1584 if (NULL != peer->path_head)
1586 p = tree_get_path_to_peer (t->tree, peer->id);
1593 // FIXME always send create path to self
1596 send_create_path (peer, p, t);
1600 GNUNET_HashCode hash;
1603 send_client_peer_connected (t, myid);
1604 t->local_tid_dest = next_local_tid++;
1605 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1608 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1609 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1616 else if (NULL == peer->dhtget)
1618 struct GNUNET_PeerIdentity id;
1620 GNUNET_PEER_resolve (peer->id, &id);
1621 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1622 path_info->peer = peer;
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1625 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1626 peer->dhtgetcls = path_info;
1627 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1628 GNUNET_BLOCK_TYPE_TEST, /* type */
1629 &id.hashPubKey, /* key to search */
1630 10, /* replication level */
1631 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1632 0, /* xquery bits */
1633 &dht_get_id_handler, path_info);
1635 /* Otherwise, there is no path but the DHT get is already started. */
1640 * Task to delay the connection of a peer
1642 * @param cls Closure (path info with tunnel and peer to connect).
1643 * Will be free'd on exection.
1644 * @param tc TaskContext
1647 peer_info_connect_task (void *cls,
1648 const struct GNUNET_SCHEDULER_TaskContext *tc)
1650 struct MeshPathInfo *path_info = cls;
1652 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1657 peer_info_connect (path_info->peer, path_info->t);
1663 * Destroy the peer_info and free any allocated resources linked to it
1665 * @param pi The peer_info to destroy.
1667 * @return GNUNET_OK on success
1670 peer_info_destroy (struct MeshPeerInfo *pi)
1672 struct GNUNET_PeerIdentity id;
1673 struct MeshPeerPath *p;
1674 struct MeshPeerPath *nextp;
1677 GNUNET_PEER_resolve (pi->id, &id);
1678 GNUNET_PEER_change_rc (pi->id, -1);
1681 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1684 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1685 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1687 if (NULL != pi->dhtget)
1689 GNUNET_DHT_get_stop (pi->dhtget);
1690 GNUNET_free (pi->dhtgetcls);
1692 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1694 // FIXME CORE_QUEUE_SIZE
1700 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1710 * Remove all paths that rely on a direct connection between p1 and p2
1711 * from the peer itself and notify all tunnels about it.
1713 * @param peer PeerInfo of affected peer.
1714 * @param p1 GNUNET_PEER_Id of one peer.
1715 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1718 * TODO: optimize (see below)
1721 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1724 struct MeshPeerPath *p;
1725 struct MeshPeerPath *aux;
1726 struct MeshPeerInfo *peer_d;
1728 unsigned int destroyed;
1734 p = peer->path_head;
1738 for (i = 0; i < (p->length - 1); i++)
1740 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1741 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1743 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1754 for (i = 0; i < peer->ntunnels; i++)
1756 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1760 * Problem: one or more peers have been deleted from the tunnel tree.
1761 * We don't know who they are to try to add them again.
1762 * We need to try to find a new path for each of the disconnected peers.
1763 * Some of them might already have a path to reach them that does not
1764 * involve p1 and p2. Adding all anew might render in a better tree than
1765 * the trivial immediate fix.
1767 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1768 * its children will be reachable trough him.
1770 peer_d = peer_info_get_short (d);
1773 for (p = peer_d->path_head; NULL != p; p = p->next)
1775 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1783 /* No callback, as peer will be already disconnected and a connection
1784 * scheduled by tunnel_notify_connection_broken.
1786 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1790 peer_info_connect (peer_d, peer->tunnels[i]);
1797 * Add the path to the peer and update the path used to reach it in case this
1800 * @param peer_info Destination peer to add the path to.
1801 * @param path New path to add. Last peer must be the peer in arg 1.
1802 * Path will be either used of freed if already known.
1803 * @param trusted Do we trust that this path is real?
1806 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1809 struct MeshPeerPath *aux;
1813 if ((NULL == peer_info) || (NULL == path))
1816 path_destroy (path);
1819 if (path->peers[path->length - 1] != peer_info->id)
1822 path_destroy (path);
1825 if (path->length <= 2 && GNUNET_NO == trusted)
1827 /* Only allow CORE to tell us about direct paths */
1828 path_destroy (path);
1831 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1832 for (l = 1; l < path->length; l++)
1834 if (path->peers[l] == myid)
1836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1837 for (l2 = 0; l2 < path->length - l; l2++)
1839 path->peers[l2] = path->peers[l + l2];
1844 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1849 struct GNUNET_PeerIdentity id;
1851 GNUNET_PEER_resolve (peer_info->id, &id);
1852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1853 path->length, GNUNET_i2s (&id));
1856 l = path_get_length (path);
1863 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1864 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1866 l2 = path_get_length (aux);
1869 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1870 peer_info->path_tail, aux, path);
1875 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1877 path_destroy (path);
1882 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1889 * Add the path to the origin peer and update the path used to reach it in case
1890 * this is the shortest.
1891 * The path is given in peer_info -> destination, therefore we turn the path
1892 * upside down first.
1894 * @param peer_info Peer to add the path to, being the origin of the path.
1895 * @param path New path to add after being inversed.
1896 * @param trusted Do we trust that this path is real?
1899 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1900 struct MeshPeerPath *path, int trusted)
1903 peer_info_add_path (peer_info, path, trusted);
1908 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1909 * to obtain a local peer -> destination path and interning the peer ids.
1911 * @return Newly allocated and created path
1913 static struct MeshPeerPath *
1914 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1915 unsigned int get_path_length,
1916 const struct GNUNET_PeerIdentity *put_path,
1917 unsigned int put_path_length)
1919 struct MeshPeerPath *p;
1925 GNUNET_PEER_change_rc (myid, 1);
1926 i = get_path_length;
1927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1928 for (i--; i >= 0; i--)
1930 id = GNUNET_PEER_intern (&get_path[i]);
1931 if (p->length > 0 && id == p->peers[p->length - 1])
1933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1934 GNUNET_PEER_change_rc (id, -1);
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1939 GNUNET_i2s (&get_path[i]));
1941 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1942 p->peers[p->length - 1] = id;
1945 i = put_path_length;
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1947 for (i--; i >= 0; i--)
1949 id = GNUNET_PEER_intern (&put_path[i]);
1952 /* PUT path went through us, so discard the path up until now and start
1953 * from here to get a much shorter (and loop-free) path.
1958 if (p->length > 0 && id == p->peers[p->length - 1])
1960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1961 GNUNET_PEER_change_rc (id, -1);
1965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1966 GNUNET_i2s (&put_path[i]));
1968 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1969 p->peers[p->length - 1] = id;
1973 if (get_path_length > 0)
1974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1975 GNUNET_i2s (&get_path[0]));
1976 if (put_path_length > 0)
1977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1978 GNUNET_i2s (&put_path[0]));
1979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1981 for (i = 0; i < p->length; i++)
1983 struct GNUNET_PeerIdentity peer_id;
1985 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1987 GNUNET_i2s (&peer_id));
1995 * Adds a path to the peer_infos of all the peers in the path
1997 * @param p Path to process.
1998 * @param confirmed Whether we know if the path works or not. FIXME use
2001 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2005 /* TODO: invert and add */
2006 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2007 for (i++; i < p->length; i++)
2009 struct MeshPeerInfo *aux;
2010 struct MeshPeerPath *copy;
2012 aux = peer_info_get_short (p->peers[i]);
2013 copy = path_duplicate (p);
2014 copy->length = i + 1;
2015 peer_info_add_path (aux, copy, GNUNET_NO);
2021 * Send keepalive packets for a peer
2023 * @param cls Closure (tunnel for which to send the keepalive).
2024 * @param tc Notification context.
2026 * TODO: implement explicit multicast keepalive?
2029 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2033 * Search for a tunnel among the incoming tunnels
2035 * @param tid the local id of the tunnel
2037 * @return tunnel handler, NULL if doesn't exist
2039 static struct MeshTunnel *
2040 tunnel_get_incoming (MESH_TunnelNumber tid)
2042 GNUNET_HashCode hash;
2044 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2045 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2046 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2051 * Search for a tunnel among the tunnels for a client
2053 * @param c the client whose tunnels to search in
2054 * @param tid the local id of the tunnel
2056 * @return tunnel handler, NULL if doesn't exist
2058 static struct MeshTunnel *
2059 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2061 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2063 return tunnel_get_incoming (tid);
2067 GNUNET_HashCode hash;
2069 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2070 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2076 * Search for a tunnel by global ID using PEER_ID
2078 * @param pi owner of the tunnel
2079 * @param tid global tunnel number
2081 * @return tunnel handler, NULL if doesn't exist
2083 static struct MeshTunnel *
2084 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2086 struct MESH_TunnelID id;
2087 GNUNET_HashCode hash;
2092 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2093 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2098 * Search for a tunnel by global ID using full PeerIdentities
2100 * @param oid owner of the tunnel
2101 * @param tid global tunnel number
2103 * @return tunnel handler, NULL if doesn't exist
2105 static struct MeshTunnel *
2106 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2108 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2113 * Delete an active client from the tunnel.
2119 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2123 for (i = 0; i < t->nclients; i++)
2125 if (t->clients[i] == c)
2127 t->clients[i] = t->clients[t->nclients - 1];
2128 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2136 * Delete an ignored client from the tunnel.
2142 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2146 for (i = 0; i < t->nignore; i++)
2148 if (t->ignore[i] == c)
2150 t->ignore[i] = t->ignore[t->nignore - 1];
2151 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2159 * Delete a client from the tunnel. It should be only done on
2160 * client disconnection, otherwise use client_ignore_tunnel.
2166 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2168 tunnel_delete_ignored_client (t, c);
2169 tunnel_delete_active_client (t, c);
2174 * Callback used to notify a client owner of a tunnel that a peer has
2175 * disconnected, most likely because of a path change.
2177 * @param cls Closure (tunnel this notification is about).
2178 * @param peer_id Short ID of disconnected peer.
2181 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2183 struct MeshTunnel *t = cls;
2184 struct MeshPeerInfo *peer;
2185 struct MeshPathInfo *path_info;
2187 if (NULL != t->owner && NULL != nc)
2189 struct GNUNET_MESH_PeerControl msg;
2191 msg.header.size = htons (sizeof (msg));
2192 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2193 msg.tunnel_id = htonl (t->local_tid);
2194 GNUNET_PEER_resolve (peer_id, &msg.peer);
2195 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2196 &msg.header, GNUNET_NO);
2198 peer = peer_info_get_short (peer_id);
2199 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2200 path_info->peer = peer;
2202 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2207 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2208 * needed rescources.
2209 * If peer already exists, reevaluate shortest path and change if different.
2211 * @param t Tunnel we want to add a new peer to
2212 * @param peer PeerInfo of the peer being added
2216 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2218 struct GNUNET_PeerIdentity id;
2219 struct MeshPeerPath *best_p;
2220 struct MeshPeerPath *p;
2221 unsigned int best_cost;
2224 GNUNET_PEER_resolve (peer->id, &id);
2226 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2229 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2230 GNUNET_assert (GNUNET_OK ==
2231 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2233 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2236 if (NULL != (p = peer->path_head))
2239 best_cost = tree_get_path_cost (t->tree, p);
2242 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2249 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2250 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2251 t->path_refresh_task =
2252 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2256 /* Start a DHT get */
2257 peer_info_connect (peer, t);
2262 * Add a path to a tunnel which we don't own, just to remember the next hop.
2263 * If destination node was already in the tunnel, the first hop information
2264 * will be replaced with the new path.
2266 * @param t Tunnel we want to add a new peer to
2267 * @param p Path to add
2268 * @param own_pos Position of local node in path.
2272 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2273 unsigned int own_pos)
2275 struct GNUNET_PeerIdentity id;
2277 GNUNET_assert (0 != own_pos);
2278 tree_add_path (t->tree, p, NULL, NULL);
2279 if (own_pos < p->length - 1)
2281 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2282 tree_update_first_hops (t->tree, myid, &id);
2288 * Notifies a tunnel that a connection has broken that affects at least
2289 * some of its peers. Sends a notification towards the root of the tree.
2290 * In case the peer is the owner of the tree, notifies the client that owns
2291 * the tunnel and tries to reconnect.
2293 * @param t Tunnel affected.
2294 * @param p1 Peer that got disconnected from p2.
2295 * @param p2 Peer that got disconnected from p1.
2297 * @return Short ID of the peer disconnected (either p1 or p2).
2298 * 0 if the tunnel remained unaffected.
2300 static GNUNET_PEER_Id
2301 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2307 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2309 if (myid != p1 && myid != p2)
2315 if (tree_get_predecessor (t->tree) != 0)
2317 /* We are the peer still connected, notify owner of the disconnection. */
2318 struct GNUNET_MESH_PathBroken msg;
2319 struct GNUNET_PeerIdentity neighbor;
2321 msg.header.size = htons (sizeof (msg));
2322 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2323 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2324 msg.tid = htonl (t->id.tid);
2325 msg.peer1 = my_full_id;
2326 GNUNET_PEER_resolve (pid, &msg.peer2);
2327 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2328 send_message (&msg.header, &neighbor);
2336 * Send a multicast packet to a neighbor.
2338 * @param cls Closure (Info about the multicast packet)
2339 * @param neighbor_id Short ID of the neighbor to send the packet to.
2342 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2344 struct MeshData *mdata = cls;
2345 struct MeshTransmissionDescriptor *info;
2346 struct GNUNET_PeerIdentity neighbor;
2347 struct MeshPeerQueue *queue;
2349 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2351 info->mesh_data = mdata;
2352 (*(mdata->reference_counter)) ++;
2353 info->destination = neighbor_id;
2354 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2356 GNUNET_i2s (&neighbor));
2357 info->peer = peer_info_get (&neighbor);
2358 GNUNET_assert (NULL != info->peer);
2359 queue = GNUNET_malloc (sizeof(struct MeshPeerQueue));
2360 info->queue = queue;
2362 queue->type = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2363 queue->core_transmit =
2364 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2365 GNUNET_TIME_UNIT_FOREVER_REL,
2366 &neighbor, info->mesh_data->data_len,
2367 &send_core_data_multicast, info);
2368 queue->peer = info->peer;
2369 GNUNET_CONTAINER_DLL_insert (queue->peer->queue_head,
2370 queue->peer->queue_tail,
2375 * Send a message in a tunnel in multicast, sending a copy to each child node
2376 * down the local one in the tunnel tree.
2378 * @param t Tunnel in which to send the data.
2379 * @param msg Message to be sent.
2380 * @param internal Has the service generated this message?
2383 tunnel_send_multicast (struct MeshTunnel *t,
2384 const struct GNUNET_MessageHeader *msg,
2387 struct MeshData *mdata;
2389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2390 " sending a multicast packet...\n");
2391 mdata = GNUNET_malloc (sizeof (struct MeshData));
2392 mdata->data_len = ntohs (msg->size);
2393 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2395 mdata->data = GNUNET_malloc (mdata->data_len);
2396 memcpy (mdata->data, msg, mdata->data_len);
2397 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2399 struct GNUNET_MESH_Multicast *mcast;
2401 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2402 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2404 ntohl (mcast->ttl));
2408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2410 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2411 && GNUNET_NO == internal)
2413 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2415 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2421 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2422 if (*(mdata->reference_counter) == 0)
2424 GNUNET_free (mdata->data);
2425 GNUNET_free (mdata->reference_counter);
2426 if (NULL != mdata->task)
2428 GNUNET_SCHEDULER_cancel(*(mdata->task));
2429 GNUNET_free (mdata->task);
2430 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2432 // FIXME change order?
2433 GNUNET_free (mdata);
2435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2436 " sending a multicast packet done\n");
2442 * Send a message to all peers in this tunnel that the tunnel is no longer
2445 * @param t The tunnel whose peers to notify.
2448 tunnel_send_destroy (struct MeshTunnel *t)
2450 struct GNUNET_MESH_TunnelDestroy msg;
2452 msg.header.size = htons (sizeof (msg));
2453 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2454 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2455 msg.tid = htonl (t->id.tid);
2456 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2462 * Destroy the tunnel and free any allocated resources linked to it.
2464 * @param t the tunnel to destroy
2466 * @return GNUNET_OK on success
2469 tunnel_destroy (struct MeshTunnel *t)
2471 struct MeshClient *c;
2472 struct MeshQueue *q;
2473 struct MeshQueue *qn;
2474 GNUNET_HashCode hash;
2485 struct GNUNET_PeerIdentity id;
2487 GNUNET_PEER_resolve (t->id.oid, &id);
2488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2489 GNUNET_i2s (&id), t->id.tid);
2491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2495 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2496 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2501 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2504 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2508 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2509 for (i = 0; i < t->nclients; i++)
2513 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2518 for (i = 0; i < t->nignore; i++)
2522 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2527 if (t->nclients > 0)
2530 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2534 GNUNET_free (t->clients);
2536 if (NULL != t->peers)
2538 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2540 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2545 if (NULL != q->data)
2546 GNUNET_free (q->data);
2550 /* TODO cancel core transmit ready in case it was active */
2552 tree_destroy (t->tree);
2553 if (NULL != t->dht_get_type)
2554 GNUNET_DHT_get_stop (t->dht_get_type);
2555 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2556 GNUNET_SCHEDULER_cancel (t->timeout_task);
2557 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2558 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2565 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2566 * that are no longer needed, as well as nodes of no longer reachable peers.
2567 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2569 * @param t Tunnel from which to remove the path.
2570 * @param peer Short id of the peer which should be removed.
2573 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2575 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2581 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2582 * client when the client disconnects. If the client is not the owner, the
2583 * owner will get notified if no more clients are in the tunnel and the client
2584 * get removed from the tunnel's list.
2586 * @param cls closure (client that is disconnecting)
2587 * @param key the hash of the local tunnel id (used to access the hashmap)
2588 * @param value the value stored at the key (tunnel to destroy)
2590 * @return GNUNET_OK on success
2593 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2595 struct MeshTunnel *t = value;
2596 struct MeshClient *c = cls;
2599 send_client_tunnel_disconnect(t, c);
2602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2603 "Client %u is destination, keeping the tunnel alive.\n", c->id);
2604 tunnel_delete_client(t, c);
2605 client_delete_tunnel(c, t);
2608 tunnel_send_destroy(t);
2609 r = tunnel_destroy (t);
2615 * Timeout function, destroys tunnel if called
2617 * @param cls Closure (tunnel to destroy).
2618 * @param tc TaskContext
2621 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2623 struct MeshTunnel *t = cls;
2625 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2627 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2632 * Resets the tunnel timeout. Starts it if no timeout was running.
2634 * @param t Tunnel whose timeout to reset.
2637 tunnel_reset_timeout (struct MeshTunnel *t)
2639 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2640 GNUNET_SCHEDULER_cancel (t->timeout_task);
2642 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2643 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2647 /******************************************************************************/
2648 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2649 /******************************************************************************/
2652 * Function called to notify a client about the socket
2653 * being ready to queue more data. "buf" will be
2654 * NULL and "size" zero if the socket was closed for
2655 * writing in the meantime.
2657 * @param cls closure
2658 * @param size number of bytes available in buf
2659 * @param buf where the callee should write the message
2660 * @return number of bytes written to buf
2663 send_core_create_path (void *cls, size_t size, void *buf)
2665 struct MeshPathInfo *info = cls;
2666 struct GNUNET_MESH_ManipulatePath *msg;
2667 struct GNUNET_PeerIdentity *peer_ptr;
2668 struct MeshPeerInfo *peer = info->peer;
2669 struct MeshTunnel *t = info->t;
2670 struct MeshPeerPath *p = info->path;
2674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2676 sizeof (struct GNUNET_MESH_ManipulatePath) +
2677 p->length * sizeof (struct GNUNET_PeerIdentity);
2679 if (size < size_needed || NULL == buf)
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "create path retransmit!\n");
2682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buf: %p\n", buf);
2683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " size: (%u/%u)\n", size,
2685 info->queue->core_transmit =
2686 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2687 GNUNET_TIME_UNIT_FOREVER_REL,
2688 tree_get_first_hop (t->tree,
2690 size_needed, &send_core_create_path,
2694 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2695 msg->header.size = htons (size_needed);
2696 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2697 msg->tid = ntohl (t->id.tid);
2699 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2700 for (i = 0; i < p->length; i++)
2702 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2706 GNUNET_CONTAINER_DLL_remove(info->queue->peer->queue_head,
2707 info->queue->peer->queue_tail,
2709 GNUNET_free (info->queue);
2712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2713 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2719 * Function called to notify a client about the socket
2720 * being ready to queue more data. "buf" will be
2721 * NULL and "size" zero if the socket was closed for
2722 * writing in the meantime.
2724 * @param cls closure (data itself)
2725 * @param size number of bytes available in buf
2726 * @param buf where the callee should write the message
2728 * @return number of bytes written to buf
2731 send_core_data_multicast (void *cls, size_t size, void *buf)
2733 struct MeshTransmissionDescriptor *info = cls;
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
2737 GNUNET_assert (NULL != info);
2738 GNUNET_assert (NULL != info->peer);
2739 total_size = info->mesh_data->data_len;
2740 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2742 if (total_size > size)
2745 struct GNUNET_PeerIdentity id;
2747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2748 "Multicast: retransmitting... (%u/%u)\n", size,
2750 GNUNET_PEER_resolve (info->peer->id, &id);
2751 info->queue->core_transmit =
2752 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2753 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2755 &send_core_data_multicast, info);
2758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
2759 memcpy (buf, info->mesh_data->data, total_size);
2762 struct GNUNET_MESH_Multicast *mc;
2763 struct GNUNET_MessageHeader *mh;
2766 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2768 mc = (struct GNUNET_MESH_Multicast *) mh;
2769 mh = (struct GNUNET_MessageHeader *) &mc[1];
2770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2771 " multicast, payload type %u\n", ntohs (mh->type));
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2773 " multicast, payload size %u\n", ntohs (mh->size));
2777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
2782 data_descriptor_decrement_multicast (info->mesh_data);
2783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
2784 GNUNET_CONTAINER_DLL_remove(info->queue->peer->queue_head,
2785 info->queue->peer->queue_tail,
2787 GNUNET_free (info->queue);
2789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
2795 * Function called to notify a client about the socket
2796 * being ready to queue more data. "buf" will be
2797 * NULL and "size" zero if the socket was closed for
2798 * writing in the meantime.
2800 * @param cls closure (MeshTransmissionDescriptor)
2801 * @param size number of bytes available in buf
2802 * @param buf where the callee should write the message
2803 * @return number of bytes written to buf
2806 send_core_path_ack (void *cls, size_t size, void *buf)
2808 struct MeshTransmissionDescriptor *info = cls;
2809 struct GNUNET_MESH_PathACK *msg = buf;
2811 GNUNET_assert (NULL != info);
2812 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2817 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2818 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2819 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2820 msg->tid = htonl (info->origin->tid);
2821 msg->peer_id = my_full_id;
2825 GNUNET_CONTAINER_DLL_remove(info->queue->peer->queue_head,
2826 info->queue->peer->queue_tail,
2828 GNUNET_free (info->queue);
2831 /* TODO add signature */
2833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2834 return sizeof (struct GNUNET_MESH_PathACK);
2838 /******************************************************************************/
2839 /******************** MESH NETWORK HANDLERS **************************/
2840 /******************************************************************************/
2844 * Core handler for path creation
2846 * @param cls closure
2847 * @param message message
2848 * @param peer peer identity this notification is about
2849 * @param atsi performance data
2850 * @param atsi_count number of records in 'atsi'
2852 * @return GNUNET_OK to keep the connection open,
2853 * GNUNET_SYSERR to close it (signal serious error)
2856 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2857 const struct GNUNET_MessageHeader *message,
2858 const struct GNUNET_ATS_Information *atsi,
2859 unsigned int atsi_count)
2861 unsigned int own_pos;
2864 MESH_TunnelNumber tid;
2865 struct GNUNET_MESH_ManipulatePath *msg;
2866 struct GNUNET_PeerIdentity *pi;
2867 GNUNET_HashCode hash;
2868 struct MeshPeerPath *path;
2869 struct MeshPeerInfo *dest_peer_info;
2870 struct MeshPeerInfo *orig_peer_info;
2871 struct MeshTunnel *t;
2873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2874 "Received a path create msg [%s]\n",
2875 GNUNET_i2s (&my_full_id));
2876 size = ntohs (message->size);
2877 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2879 GNUNET_break_op (0);
2883 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2884 if (size % sizeof (struct GNUNET_PeerIdentity))
2886 GNUNET_break_op (0);
2889 size /= sizeof (struct GNUNET_PeerIdentity);
2892 GNUNET_break_op (0);
2895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2896 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2898 tid = ntohl (msg->tid);
2899 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2901 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2902 t = tunnel_get (pi, tid);
2905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2906 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2907 t->id.oid = GNUNET_PEER_intern (pi);
2909 while (NULL != tunnel_get_incoming (next_local_tid))
2910 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2911 t->local_tid_dest = next_local_tid++;
2912 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2913 t->tree = tree_new (t->id.oid);
2915 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2917 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2918 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2924 tunnel_reset_timeout (t);
2925 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2927 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2928 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2936 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2937 if (NULL == dest_peer_info)
2939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2940 " Creating PeerInfo for destination.\n");
2941 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2942 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2943 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2945 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2947 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2948 if (NULL == orig_peer_info)
2950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2951 " Creating PeerInfo for origin.\n");
2952 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2953 orig_peer_info->id = GNUNET_PEER_intern (pi);
2954 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2955 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
2958 path = path_new (size);
2960 for (i = 0; i < size; i++)
2962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
2963 GNUNET_i2s (&pi[i]));
2964 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2965 if (path->peers[i] == myid)
2968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
2971 /* cannot be self, must be 'not found' */
2972 /* create path: self not found in path through self */
2973 GNUNET_break_op (0);
2974 path_destroy (path);
2975 /* FIXME error. destroy tunnel? leave for timeout? */
2978 path_add_to_peers (path, GNUNET_NO);
2979 tunnel_add_path (t, path, own_pos);
2980 if (own_pos == size - 1)
2982 /* It is for us! Send ack. */
2983 struct MeshTransmissionDescriptor *info;
2984 struct MeshPeerQueue *queue;
2986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2987 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2988 if (NULL == t->peers)
2990 /* New tunnel! Notify clients on data. */
2991 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2993 GNUNET_break (GNUNET_OK ==
2994 GNUNET_CONTAINER_multihashmap_put (t->peers,
2995 &my_full_id.hashPubKey,
2998 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
2999 /* FIXME use send_message */
3000 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3001 info->origin = &t->id;
3002 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3003 GNUNET_assert (NULL != info->peer);
3004 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3005 info->queue = queue;
3006 queue->peer = info->peer;
3007 queue->type = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
3009 queue->core_transmit =
3010 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 10,
3011 GNUNET_TIME_UNIT_FOREVER_REL, peer,
3012 sizeof (struct GNUNET_MESH_PathACK),
3013 &send_core_path_ack, info);
3014 GNUNET_CONTAINER_DLL_insert (queue->peer->queue_head,
3015 queue->peer->queue_tail,
3020 struct MeshPeerPath *path2;
3022 /* It's for somebody else! Retransmit. */
3023 path2 = path_duplicate (path);
3024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3025 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3026 path2 = path_duplicate (path);
3027 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3028 send_create_path (dest_peer_info, path, t);
3035 * Core handler for path destruction
3037 * @param cls closure
3038 * @param message message
3039 * @param peer peer identity this notification is about
3040 * @param atsi performance data
3041 * @param atsi_count number of records in 'atsi'
3043 * @return GNUNET_OK to keep the connection open,
3044 * GNUNET_SYSERR to close it (signal serious error)
3047 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3048 const struct GNUNET_MessageHeader *message,
3049 const struct GNUNET_ATS_Information *atsi,
3050 unsigned int atsi_count)
3052 struct GNUNET_MESH_ManipulatePath *msg;
3053 struct GNUNET_PeerIdentity *pi;
3054 struct MeshPeerPath *path;
3055 struct MeshTunnel *t;
3056 unsigned int own_pos;
3060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3061 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3062 size = ntohs (message->size);
3063 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3065 GNUNET_break_op (0);
3069 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3070 if (size % sizeof (struct GNUNET_PeerIdentity))
3072 GNUNET_break_op (0);
3075 size /= sizeof (struct GNUNET_PeerIdentity);
3078 GNUNET_break_op (0);
3081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3083 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3084 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3086 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3088 t = tunnel_get (pi, ntohl (msg->tid));
3091 /* TODO notify back: we don't know this tunnel */
3092 GNUNET_break_op (0);
3095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3096 path = path_new (size);
3098 for (i = 0; i < size; i++)
3100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3101 GNUNET_i2s (&pi[i]));
3102 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3103 if (path->peers[i] == myid)
3106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3107 if (own_pos < path->length - 1)
3108 send_message (message, &pi[own_pos + 1]);
3110 send_client_tunnel_disconnect(t, NULL);
3112 tunnel_delete_peer (t, path->peers[path->length - 1]);
3113 path_destroy (path);
3119 * Core handler for notifications of broken paths
3121 * @param cls closure
3122 * @param message message
3123 * @param peer peer identity this notification is about
3124 * @param atsi performance data
3125 * @param atsi_count number of records in 'atsi'
3127 * @return GNUNET_OK to keep the connection open,
3128 * GNUNET_SYSERR to close it (signal serious error)
3131 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3132 const struct GNUNET_MessageHeader *message,
3133 const struct GNUNET_ATS_Information *atsi,
3134 unsigned int atsi_count)
3136 struct GNUNET_MESH_PathBroken *msg;
3137 struct MeshTunnel *t;
3139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3140 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3141 msg = (struct GNUNET_MESH_PathBroken *) message;
3142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3143 GNUNET_i2s (&msg->peer1));
3144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3145 GNUNET_i2s (&msg->peer2));
3146 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3149 GNUNET_break_op (0);
3152 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3153 GNUNET_PEER_search (&msg->peer2));
3160 * Core handler for tunnel destruction
3162 * @param cls closure
3163 * @param message message
3164 * @param peer peer identity this notification is about
3165 * @param atsi performance data
3166 * @param atsi_count number of records in 'atsi'
3168 * @return GNUNET_OK to keep the connection open,
3169 * GNUNET_SYSERR to close it (signal serious error)
3172 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3173 const struct GNUNET_MessageHeader *message,
3174 const struct GNUNET_ATS_Information *atsi,
3175 unsigned int atsi_count)
3177 struct GNUNET_MESH_TunnelDestroy *msg;
3178 struct MeshTunnel *t;
3180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3181 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3182 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3184 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3185 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3188 /* Probably already got the message from another path,
3189 * destroyed the tunnel and retransmitted to children.
3194 if (t->id.oid == myid)
3196 GNUNET_break_op (0);
3199 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3201 /* Tunnel was incoming, notify clients */
3202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3203 t->local_tid, t->local_tid_dest);
3204 send_clients_tunnel_destroy (t);
3206 tunnel_send_destroy (t);
3213 * Core handler for mesh network traffic going from the origin to a peer
3215 * @param cls closure
3216 * @param peer peer identity this notification is about
3217 * @param message message
3218 * @param atsi performance data
3219 * @param atsi_count number of records in 'atsi'
3220 * @return GNUNET_OK to keep the connection open,
3221 * GNUNET_SYSERR to close it (signal serious error)
3224 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3225 const struct GNUNET_MessageHeader *message,
3226 const struct GNUNET_ATS_Information *atsi,
3227 unsigned int atsi_count)
3229 struct GNUNET_MESH_Unicast *msg;
3230 struct MeshTunnel *t;
3234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3236 size = ntohs (message->size);
3238 sizeof (struct GNUNET_MESH_Unicast) +
3239 sizeof (struct GNUNET_MessageHeader))
3244 msg = (struct GNUNET_MESH_Unicast *) message;
3245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3246 ntohs (msg[1].header.type));
3247 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3250 /* TODO notify back: we don't know this tunnel */
3251 GNUNET_break_op (0);
3254 tunnel_reset_timeout (t);
3255 pid = GNUNET_PEER_search (&msg->destination);
3258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3259 " it's for us! sending to clients...\n");
3260 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3264 " not for us, retransmitting...\n");
3265 send_message (message, tree_get_first_hop (t->tree, pid));
3271 * Core handler for mesh network traffic going from the origin to all peers
3273 * @param cls closure
3274 * @param message message
3275 * @param peer peer identity this notification is about
3276 * @param atsi performance data
3277 * @param atsi_count number of records in 'atsi'
3278 * @return GNUNET_OK to keep the connection open,
3279 * GNUNET_SYSERR to close it (signal serious error)
3281 * TODO: Check who we got this from, to validate route.
3284 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3285 const struct GNUNET_MessageHeader *message,
3286 const struct GNUNET_ATS_Information *atsi,
3287 unsigned int atsi_count)
3289 struct GNUNET_MESH_Multicast *msg;
3290 struct MeshTunnel *t;
3293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
3295 size = ntohs (message->size);
3296 if (sizeof (struct GNUNET_MESH_Multicast) +
3297 sizeof (struct GNUNET_MessageHeader) > size)
3299 GNUNET_break_op (0);
3302 msg = (struct GNUNET_MESH_Multicast *) message;
3303 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3307 /* TODO notify that we dont know that tunnel */
3308 GNUNET_break_op (0);
3311 if (t->mid == ntohl (msg->mid))
3313 /* FIXME: already seen this packet, log dropping */
3314 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3315 " Already seen mid %u, DROPPING!\n", t->mid);
3320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3321 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3323 t->mid = ntohl (msg->mid);
3324 tunnel_reset_timeout (t);
3326 /* Transmit to locally interested clients */
3327 if (NULL != t->peers &&
3328 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3330 send_subscribed_clients (message, &msg[1].header);
3332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
3333 if (ntohl (msg->ttl) == 0)
3335 /* FIXME: ttl is 0, log dropping */
3336 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3339 tunnel_send_multicast (t, message, GNUNET_NO);
3345 * Core handler for mesh network traffic toward the owner of a tunnel
3347 * @param cls closure
3348 * @param message message
3349 * @param peer peer identity this notification is about
3350 * @param atsi performance data
3351 * @param atsi_count number of records in 'atsi'
3353 * @return GNUNET_OK to keep the connection open,
3354 * GNUNET_SYSERR to close it (signal serious error)
3357 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3358 const struct GNUNET_MessageHeader *message,
3359 const struct GNUNET_ATS_Information *atsi,
3360 unsigned int atsi_count)
3362 struct GNUNET_MESH_ToOrigin *msg;
3363 struct GNUNET_PeerIdentity id;
3364 struct MeshPeerInfo *peer_info;
3365 struct MeshTunnel *t;
3368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3370 size = ntohs (message->size);
3371 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3372 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3374 GNUNET_break_op (0);
3377 msg = (struct GNUNET_MESH_ToOrigin *) message;
3378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3379 ntohs (msg[1].header.type));
3380 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3384 /* TODO notify that we dont know this tunnel (whom)? */
3385 GNUNET_break_op (0);
3389 if (t->id.oid == myid)
3392 struct GNUNET_MESH_ToOrigin *copy;
3394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3395 " it's for us! sending to clients...\n");
3396 if (NULL == t->owner)
3398 /* got data packet for ownerless tunnel */
3399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
3400 GNUNET_break_op (0);
3403 /* TODO signature verification */
3404 memcpy (cbuf, message, size);
3405 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3406 copy->tid = htonl (t->local_tid);
3407 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3408 ©->header, GNUNET_YES);
3411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3412 " not for us, retransmitting...\n");
3414 peer_info = peer_info_get (&msg->oid);
3415 if (NULL == peer_info)
3417 /* unknown origin of tunnel */
3421 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3422 send_message (message, &id);
3429 * Core handler for path ACKs
3431 * @param cls closure
3432 * @param message message
3433 * @param peer peer identity this notification is about
3434 * @param atsi performance data
3435 * @param atsi_count number of records in 'atsi'
3437 * @return GNUNET_OK to keep the connection open,
3438 * GNUNET_SYSERR to close it (signal serious error)
3441 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3442 const struct GNUNET_MessageHeader *message,
3443 const struct GNUNET_ATS_Information *atsi,
3444 unsigned int atsi_count)
3446 struct GNUNET_MESH_PathACK *msg;
3447 struct GNUNET_PeerIdentity id;
3448 struct MeshPeerInfo *peer_info;
3449 struct MeshPeerPath *p;
3450 struct MeshTunnel *t;
3452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3453 GNUNET_i2s (&my_full_id));
3454 msg = (struct GNUNET_MESH_PathACK *) message;
3455 t = tunnel_get (&msg->oid, msg->tid);
3458 /* TODO notify that we don't know the tunnel */
3462 peer_info = peer_info_get (&msg->peer_id);
3464 /* Add paths to peers? */
3465 p = tree_get_path_to_peer (t->tree, peer_info->id);
3468 path_add_to_peers (p, GNUNET_YES);
3476 /* Message for us? */
3477 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3480 if (NULL == t->owner)
3482 GNUNET_break_op (0);
3485 if (NULL != t->dht_get_type)
3487 GNUNET_DHT_get_stop (t->dht_get_type);
3488 t->dht_get_type = NULL;
3490 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3492 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3493 send_client_peer_connected (t, peer_info->id);
3498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3499 " not for us, retransmitting...\n");
3500 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3501 peer_info = peer_info_get (&msg->oid);
3502 if (NULL == peer_info)
3504 /* If we know the tunnel, we should DEFINITELY know the peer */
3508 send_message (message, &id);
3514 * Functions to handle messages from core
3516 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3517 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3518 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3519 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3520 sizeof (struct GNUNET_MESH_PathBroken)},
3521 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3522 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3523 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3524 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3525 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3526 sizeof (struct GNUNET_MESH_PathACK)},
3532 /******************************************************************************/
3533 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3534 /******************************************************************************/
3537 * deregister_app: iterator for removing each application registered by a client
3539 * @param cls closure
3540 * @param key the hash of the application id (used to access the hashmap)
3541 * @param value the value stored at the key (client)
3543 * @return GNUNET_OK on success
3546 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3548 GNUNET_break (GNUNET_YES ==
3549 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3556 * notify_client_connection_failure: notify a client that the connection to the
3557 * requested remote peer is not possible (for instance, no route found)
3558 * Function called when the socket is ready to queue more data. "buf" will be
3559 * NULL and "size" zero if the socket was closed for writing in the meantime.
3561 * @param cls closure
3562 * @param size number of bytes available in buf
3563 * @param buf where the callee should write the message
3564 * @return number of bytes written to buf
3567 notify_client_connection_failure (void *cls, size_t size, void *buf)
3570 struct MeshPeerInfo *peer_info;
3571 struct GNUNET_MESH_PeerControl *msg;
3572 struct GNUNET_PeerIdentity id;
3574 if (0 == size && NULL == buf)
3576 // TODO retry? cancel?
3580 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3581 peer_info = (struct MeshPeerInfo *) cls;
3582 msg = (struct GNUNET_MESH_PeerControl *) buf;
3583 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3584 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3585 // msg->tunnel_id = htonl(peer_info->t->tid);
3586 GNUNET_PEER_resolve (peer_info->id, &id);
3587 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3595 * Send keepalive packets for a peer
3597 * @param cls Closure (tunnel for which to send the keepalive).
3598 * @param tc Notification context.
3600 * TODO: implement explicit multicast keepalive?
3603 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3605 struct MeshTunnel *t = cls;
3606 struct GNUNET_MessageHeader *payload;
3607 struct GNUNET_MESH_Multicast *msg;
3609 sizeof (struct GNUNET_MESH_Multicast) +
3610 sizeof (struct GNUNET_MessageHeader);
3613 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3617 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3620 "sending keepalive for tunnel %d\n", t->id.tid);
3622 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3623 msg->header.size = htons (size);
3624 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3625 msg->oid = my_full_id;
3626 msg->tid = htonl (t->id.tid);
3627 msg->ttl = htonl (DEFAULT_TTL);
3628 msg->mid = htonl (t->mid + 1);
3630 payload = (struct GNUNET_MessageHeader *) &msg[1];
3631 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3632 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3633 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
3635 t->path_refresh_task =
3636 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3642 * Function to process paths received for a new peer addition. The recorded
3643 * paths form the initial tunnel, which can be optimized later.
3644 * Called on each result obtained for the DHT search.
3646 * @param cls closure
3647 * @param exp when will this value expire
3648 * @param key key of the result
3649 * @param get_path path of the get request
3650 * @param get_path_length lenght of get_path
3651 * @param put_path path of the put request
3652 * @param put_path_length length of the put_path
3653 * @param type type of the result
3654 * @param size number of bytes in data
3655 * @param data pointer to the result data
3657 * TODO: re-issue the request after certain time? cancel after X results?
3660 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3661 const GNUNET_HashCode * key,
3662 const struct GNUNET_PeerIdentity *get_path,
3663 unsigned int get_path_length,
3664 const struct GNUNET_PeerIdentity *put_path,
3665 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3666 size_t size, const void *data)
3668 struct MeshPathInfo *path_info = cls;
3669 struct MeshPeerPath *p;
3670 struct GNUNET_PeerIdentity pi;
3673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3674 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3677 p = path_build_from_dht (get_path, get_path_length, put_path,
3679 path_add_to_peers (p, GNUNET_NO);
3681 for (i = 0; i < path_info->peer->ntunnels; i++)
3683 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3684 peer_info_connect (path_info->peer, path_info->t);
3692 * Function to process paths received for a new peer addition. The recorded
3693 * paths form the initial tunnel, which can be optimized later.
3694 * Called on each result obtained for the DHT search.
3696 * @param cls closure
3697 * @param exp when will this value expire
3698 * @param key key of the result
3699 * @param get_path path of the get request
3700 * @param get_path_length lenght of get_path
3701 * @param put_path path of the put request
3702 * @param put_path_length length of the put_path
3703 * @param type type of the result
3704 * @param size number of bytes in data
3705 * @param data pointer to the result data
3708 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3709 const GNUNET_HashCode * key,
3710 const struct GNUNET_PeerIdentity *get_path,
3711 unsigned int get_path_length,
3712 const struct GNUNET_PeerIdentity *put_path,
3713 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3714 size_t size, const void *data)
3716 const struct GNUNET_PeerIdentity *pi = data;
3717 struct MeshTunnel *t = cls;
3718 struct MeshPeerInfo *peer_info;
3719 struct MeshPeerPath *p;
3721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
3722 if (size != sizeof (struct GNUNET_PeerIdentity))
3724 GNUNET_break_op (0);
3727 GNUNET_assert (NULL != t->owner);
3728 peer_info = peer_info_get (pi);
3729 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3731 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3733 p = path_build_from_dht (get_path, get_path_length, put_path,
3735 path_add_to_peers (p, GNUNET_NO);
3737 tunnel_add_peer (t, peer_info);
3738 peer_info_connect (peer_info, t);
3742 /******************************************************************************/
3743 /********************* MESH LOCAL HANDLES **************************/
3744 /******************************************************************************/
3748 * Handler for client disconnection
3750 * @param cls closure
3751 * @param client identification of the client; NULL
3752 * for the last call when the server is destroyed
3755 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3757 struct MeshClient *c;
3758 struct MeshClient *next;
3760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3769 if (c->handle != client)
3771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3777 GNUNET_SERVER_client_drop (c->handle);
3778 c->shutting_down = GNUNET_YES;
3779 GNUNET_assert (NULL != c->own_tunnels);
3780 GNUNET_assert (NULL != c->incoming_tunnels);
3781 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3782 &tunnel_destroy_iterator, c);
3783 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3784 &tunnel_destroy_iterator, c);
3785 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
3786 &tunnel_destroy_iterator, c);
3787 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3788 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3789 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
3791 /* deregister clients applications */
3792 if (NULL != c->apps)
3794 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3795 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3797 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3798 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3800 GNUNET_SCHEDULER_cancel (announce_applications_task);
3801 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3803 if (NULL != c->types)
3804 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3806 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
3817 * Handler for new clients
3819 * @param cls closure
3820 * @param client identification of the client
3821 * @param message the actual message, which includes messages the client wants
3824 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3825 const struct GNUNET_MessageHeader *message)
3827 struct GNUNET_MESH_ClientConnect *cc_msg;
3828 struct MeshClient *c;
3829 GNUNET_MESH_ApplicationType *a;
3836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3837 /* Check data sanity */
3838 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3839 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3840 ntypes = ntohs (cc_msg->types);
3841 napps = ntohs (cc_msg->applications);
3843 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3846 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3850 /* Create new client structure */
3851 c = GNUNET_malloc (sizeof (struct MeshClient));
3852 c->id = next_client_id++;
3853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
3855 GNUNET_SERVER_client_keep (client);
3856 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3859 GNUNET_MESH_ApplicationType at;
3862 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3863 for (i = 0; i < napps; i++)
3866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
3867 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3868 /* store in clients hashmap */
3869 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3870 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3871 /* store in global hashmap, for announcements */
3872 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3873 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3875 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3876 announce_applications_task =
3877 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3885 t = (uint16_t *) & a[napps];
3886 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3887 for (i = 0; i < ntypes; i++)
3890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
3891 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3893 /* store in clients hashmap */
3894 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3895 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3896 /* store in global hashmap */
3897 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3898 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3902 " client has %u+%u subscriptions\n", napps, ntypes);
3904 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3905 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3906 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3907 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3908 GNUNET_SERVER_notification_context_add (nc, client);
3910 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3916 * Handler for requests of new tunnels
3918 * @param cls closure
3919 * @param client identification of the client
3920 * @param message the actual message
3923 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3924 const struct GNUNET_MessageHeader *message)
3926 struct GNUNET_MESH_TunnelMessage *t_msg;
3927 struct MeshTunnel *t;
3928 struct MeshClient *c;
3929 GNUNET_HashCode hash;
3931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3933 /* Sanity check for client registration */
3934 if (NULL == (c = client_get (client)))
3937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3942 /* Message sanity check */
3943 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3946 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3950 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3951 /* Sanity check for tunnel numbering */
3952 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3955 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3958 /* Sanity check for duplicate tunnel IDs */
3959 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3962 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3966 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3967 while (NULL != tunnel_get_by_pi (myid, next_tid))
3968 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3969 t->id.tid = next_tid++;
3970 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3972 t->local_tid = ntohl (t_msg->tunnel_id);
3973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
3974 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3976 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3978 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3980 GNUNET_CONTAINER_multihashmap_put (c->own_tunnels, &hash, t,
3981 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3984 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3988 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3990 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3991 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3997 t->tree = tree_new (myid);
3999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
4000 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4006 * Handler for requests of deleting tunnels
4008 * @param cls closure
4009 * @param client identification of the client
4010 * @param message the actual message
4013 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4014 const struct GNUNET_MessageHeader *message)
4016 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4017 struct MeshClient *c;
4018 struct MeshTunnel *t;
4019 MESH_TunnelNumber tid;
4021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4022 "Got a DESTROY TUNNEL from client!\n");
4024 /* Sanity check for client registration */
4025 if (NULL == (c = client_get (client)))
4028 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4031 /* Message sanity check */
4032 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4035 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4039 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4041 /* Retrieve tunnel */
4042 tid = ntohl (tunnel_msg->tunnel_id);
4043 t = tunnel_get_by_local_id(c, tid);
4047 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4048 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4051 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4053 client_ignore_tunnel (c, t);
4055 // TODO: when to destroy incoming tunnel?
4056 if (t->nclients == 0)
4058 GNUNET_assert (GNUNET_YES ==
4059 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
4061 GNUNET_assert (GNUNET_YES ==
4062 GNUNET_CONTAINER_multihashmap_remove (t->peers,
4063 &my_full_id.hashPubKey,
4067 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4070 send_client_tunnel_disconnect(t, c);
4071 client_delete_tunnel(c, t);
4073 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4075 tunnel_send_destroy (t);
4077 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4083 * Handler for connection requests to new peers
4085 * @param cls closure
4086 * @param client identification of the client
4087 * @param message the actual message (PeerControl)
4090 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
4091 const struct GNUNET_MessageHeader *message)
4093 struct GNUNET_MESH_PeerControl *peer_msg;
4094 struct MeshPeerInfo *peer_info;
4095 struct MeshClient *c;
4096 struct MeshTunnel *t;
4097 MESH_TunnelNumber tid;
4099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
4100 /* Sanity check for client registration */
4101 if (NULL == (c = client_get (client)))
4104 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4108 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4109 /* Sanity check for message size */
4110 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4113 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4117 /* Tunnel exists? */
4118 tid = ntohl (peer_msg->tunnel_id);
4119 t = tunnel_get_by_local_id (c, tid);
4123 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4127 /* Does client own tunnel? */
4128 if (t->owner->handle != client)
4131 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
4135 GNUNET_i2s (&peer_msg->peer));
4136 peer_info = peer_info_get (&peer_msg->peer);
4138 tunnel_add_peer (t, peer_info);
4139 peer_info_connect (peer_info, t);
4141 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4147 * Handler for disconnection requests of peers in a tunnel
4149 * @param cls closure
4150 * @param client identification of the client
4151 * @param message the actual message (PeerControl)
4154 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
4155 const struct GNUNET_MessageHeader *message)
4157 struct GNUNET_MESH_PeerControl *peer_msg;
4158 struct MeshPeerInfo *peer_info;
4159 struct MeshClient *c;
4160 struct MeshTunnel *t;
4161 MESH_TunnelNumber tid;
4163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
4164 /* Sanity check for client registration */
4165 if (NULL == (c = client_get (client)))
4168 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4171 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4172 /* Sanity check for message size */
4173 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4176 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4180 /* Tunnel exists? */
4181 tid = ntohl (peer_msg->tunnel_id);
4182 t = tunnel_get_by_local_id (c, tid);
4186 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4191 /* Does client own tunnel? */
4192 if (t->owner->handle != client)
4195 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
4200 GNUNET_i2s (&peer_msg->peer));
4201 /* Is the peer in the tunnel? */
4203 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
4204 if (NULL == peer_info)
4207 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4211 /* Ok, delete peer from tunnel */
4212 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
4213 &peer_msg->peer.hashPubKey);
4215 send_destroy_path (t, peer_info->id);
4216 tunnel_delete_peer (t, peer_info->id);
4217 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4223 * Handler for connection requests to new peers by type
4225 * @param cls closure
4226 * @param client identification of the client
4227 * @param message the actual message (ConnectPeerByType)
4230 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
4231 const struct GNUNET_MessageHeader *message)
4233 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
4234 struct MeshClient *c;
4235 struct MeshTunnel *t;
4236 GNUNET_HashCode hash;
4237 MESH_TunnelNumber tid;
4239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
4240 /* Sanity check for client registration */
4241 if (NULL == (c = client_get (client)))
4244 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4248 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
4249 /* Sanity check for message size */
4250 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
4251 ntohs (connect_msg->header.size))
4254 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4258 /* Tunnel exists? */
4259 tid = ntohl (connect_msg->tunnel_id);
4260 t = tunnel_get_by_local_id (c, tid);
4264 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4268 /* Does client own tunnel? */
4269 if (t->owner->handle != client)
4272 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4276 /* Do WE have the service? */
4277 t->type = ntohl (connect_msg->type);
4278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
4279 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
4280 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
4283 /* Yes! Fast forward, add ourselves to the tunnel and send the
4284 * good news to the client, and alert the destination client of
4285 * an incoming tunnel.
4287 * FIXME send a path create to self, avoid code duplication
4289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
4290 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
4291 peer_info_get (&my_full_id),
4292 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
4295 send_client_peer_connected (t, myid);
4296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
4297 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4299 t->local_tid_dest = next_local_tid++;
4300 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4301 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4302 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4306 /* Ok, lets find a peer offering the service */
4307 if (NULL != t->dht_get_type)
4309 GNUNET_DHT_get_stop (t->dht_get_type);
4311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
4312 GNUNET_h2s (&hash));
4314 GNUNET_DHT_get_start (dht_handle,
4315 GNUNET_BLOCK_TYPE_TEST, &hash, 10,
4316 GNUNET_DHT_RO_RECORD_ROUTE |
4317 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
4318 &dht_get_type_handler, t);
4320 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4326 * Handler for client traffic directed to one peer
4328 * @param cls closure
4329 * @param client identification of the client
4330 * @param message the actual message
4333 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4334 const struct GNUNET_MessageHeader *message)
4336 struct MeshClient *c;
4337 struct MeshTunnel *t;
4338 struct MeshPeerInfo *pi;
4339 struct GNUNET_MESH_Unicast *data_msg;
4340 MESH_TunnelNumber tid;
4343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4344 "Got a unicast request from a client!\n");
4346 /* Sanity check for client registration */
4347 if (NULL == (c = client_get (client)))
4350 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4353 data_msg = (struct GNUNET_MESH_Unicast *) message;
4354 /* Sanity check for message size */
4355 size = ntohs (message->size);
4356 if (sizeof (struct GNUNET_MESH_Unicast) +
4357 sizeof (struct GNUNET_MessageHeader) > size)
4360 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4364 /* Tunnel exists? */
4365 tid = ntohl (data_msg->tid);
4366 t = tunnel_get_by_local_id (c, tid);
4370 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4374 /* Is it a local tunnel? Then, does client own the tunnel? */
4375 if (t->owner->handle != client)
4378 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4382 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
4383 &data_msg->destination.hashPubKey);
4384 /* Is the selected peer in the tunnel? */
4388 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4392 /* Ok, everything is correct, send the message
4393 * (pretend we got it from a mesh peer)
4396 char buf[ntohs (message->size)] GNUNET_ALIGN;
4397 struct GNUNET_MESH_Unicast *copy;
4399 /* Work around const limitation */
4400 copy = (struct GNUNET_MESH_Unicast *) buf;
4401 memcpy (buf, data_msg, size);
4402 copy->oid = my_full_id;
4403 copy->tid = htonl (t->id.tid);
4404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4405 " calling generic handler...\n");
4406 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4408 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4414 * Handler for client traffic directed to the origin
4416 * @param cls closure
4417 * @param client identification of the client
4418 * @param message the actual message
4421 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4422 const struct GNUNET_MessageHeader *message)
4424 struct GNUNET_MESH_ToOrigin *data_msg;
4425 struct GNUNET_PeerIdentity id;
4426 struct MeshClient *c;
4427 struct MeshTunnel *t;
4428 MESH_TunnelNumber tid;
4431 /* Sanity check for client registration */
4432 if (NULL == (c = client_get (client)))
4435 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4438 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4439 /* Sanity check for message size */
4440 size = ntohs (message->size);
4441 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4442 sizeof (struct GNUNET_MessageHeader) > size)
4445 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4449 /* Tunnel exists? */
4450 tid = ntohl (data_msg->tid);
4451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4452 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
4453 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4459 t = tunnel_get_by_local_id (c, tid);
4463 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4467 /* It should be sent by someone who has this as incoming tunnel. */
4468 if (-1 == client_knows_tunnel (c, t))
4471 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4474 GNUNET_PEER_resolve (t->id.oid, &id);
4476 /* Ok, everything is correct, send the message
4477 * (pretend we got it from a mesh peer)
4480 char buf[ntohs (message->size)] GNUNET_ALIGN;
4481 struct GNUNET_MESH_ToOrigin *copy;
4483 /* Work around const limitation */
4484 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4485 memcpy (buf, data_msg, size);
4487 copy->tid = htonl (t->id.tid);
4488 copy->sender = my_full_id;
4489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4490 " calling generic handler...\n");
4491 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4493 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4499 * Handler for client traffic directed to all peers in a tunnel
4501 * @param cls closure
4502 * @param client identification of the client
4503 * @param message the actual message
4506 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4507 const struct GNUNET_MessageHeader *message)
4509 struct MeshClient *c;
4510 struct MeshTunnel *t;
4511 struct GNUNET_MESH_Multicast *data_msg;
4512 MESH_TunnelNumber tid;
4514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4515 "Got a multicast request from a client!\n");
4517 /* Sanity check for client registration */
4518 if (NULL == (c = client_get (client)))
4521 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4524 data_msg = (struct GNUNET_MESH_Multicast *) message;
4525 /* Sanity check for message size */
4526 if (sizeof (struct GNUNET_MESH_Multicast) +
4527 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4530 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4534 /* Tunnel exists? */
4535 tid = ntohl (data_msg->tid);
4536 t = tunnel_get_by_local_id (c, tid);
4540 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4544 /* Does client own tunnel? */
4545 if (t->owner->handle != client)
4548 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4553 char buf[ntohs (message->size)] GNUNET_ALIGN;
4554 struct GNUNET_MESH_Multicast *copy;
4556 copy = (struct GNUNET_MESH_Multicast *) buf;
4557 memcpy (buf, message, ntohs (message->size));
4558 copy->oid = my_full_id;
4559 copy->tid = htonl (t->id.tid);
4560 copy->ttl = htonl (DEFAULT_TTL);
4561 copy->mid = htonl (t->mid + 1);
4562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4563 " calling generic handler...\n");
4564 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4567 /* receive done gets called when last copy is sent to a neighbor */
4572 * Functions to handle messages from clients
4574 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4575 {&handle_local_new_client, NULL,
4576 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4577 {&handle_local_tunnel_create, NULL,
4578 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4579 sizeof (struct GNUNET_MESH_TunnelMessage)},
4580 {&handle_local_tunnel_destroy, NULL,
4581 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4582 sizeof (struct GNUNET_MESH_TunnelMessage)},
4583 {&handle_local_connect_add, NULL,
4584 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4585 sizeof (struct GNUNET_MESH_PeerControl)},
4586 {&handle_local_connect_del, NULL,
4587 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4588 sizeof (struct GNUNET_MESH_PeerControl)},
4589 {&handle_local_connect_by_type, NULL,
4590 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4591 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4592 {&handle_local_unicast, NULL,
4593 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4594 {&handle_local_to_origin, NULL,
4595 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4596 {&handle_local_multicast, NULL,
4597 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4603 * To be called on core init/fail.
4605 * @param cls service closure
4606 * @param server handle to the server for this service
4607 * @param identity the public identity of this peer
4610 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4611 const struct GNUNET_PeerIdentity *identity)
4613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4614 core_handle = server;
4615 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4618 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4619 GNUNET_SCHEDULER_shutdown ();
4625 * Method called whenever a given peer connects.
4627 * @param cls closure
4628 * @param peer peer identity this notification is about
4629 * @param atsi performance data for the connection
4630 * @param atsi_count number of records in 'atsi'
4633 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4634 const struct GNUNET_ATS_Information *atsi,
4635 unsigned int atsi_count)
4637 struct MeshPeerInfo *peer_info;
4638 struct MeshPeerPath *path;
4640 DEBUG_CONN ("Peer connected\n");
4641 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4642 peer_info = peer_info_get (peer);
4643 if (myid == peer_info->id)
4645 DEBUG_CONN (" (self)\n");
4650 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4652 path = path_new (2);
4653 path->peers[0] = myid;
4654 path->peers[1] = peer_info->id;
4655 GNUNET_PEER_change_rc (myid, 1);
4656 GNUNET_PEER_change_rc (peer_info->id, 1);
4657 peer_info_add_path (peer_info, path, GNUNET_YES);
4662 * Method called whenever a peer disconnects.
4664 * @param cls closure
4665 * @param peer peer identity this notification is about
4668 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4670 struct MeshPeerInfo *pi;
4673 DEBUG_CONN ("Peer disconnected\n");
4674 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4680 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4682 /* TODO: notify that the transmission failed */
4683 // FIXME CORE_QUEUE_SIZE
4685 peer_info_remove_path (pi, pi->id, myid);
4688 DEBUG_CONN (" (self)\n");
4694 /******************************************************************************/
4695 /************************ MAIN FUNCTIONS ****************************/
4696 /******************************************************************************/
4699 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4701 * @param cls closure
4702 * @param key current key code
4703 * @param value value in the hash map
4704 * @return GNUNET_YES if we should continue to iterate,
4708 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4710 struct MeshTunnel *t = value;
4717 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4719 * @param cls closure
4720 * @param key current key code
4721 * @param value value in the hash map
4722 * @return GNUNET_YES if we should continue to iterate,
4726 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4728 struct MeshPeerInfo *p = value;
4730 peer_info_destroy (p);
4735 * Task run during shutdown.
4741 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4745 if (core_handle != NULL)
4747 GNUNET_CORE_disconnect (core_handle);
4750 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4751 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4752 if (dht_handle != NULL)
4754 GNUNET_DHT_disconnect (dht_handle);
4759 GNUNET_SERVER_notification_context_destroy (nc);
4762 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4764 GNUNET_SCHEDULER_cancel (announce_id_task);
4765 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4771 * Process mesh requests.
4773 * @param cls closure
4774 * @param server the initialized server
4775 * @param c configuration to use
4778 run (void *cls, struct GNUNET_SERVER_Handle *server,
4779 const struct GNUNET_CONFIGURATION_Handle *c)
4781 struct MeshPeerInfo *peer;
4782 struct MeshPeerPath *p;
4785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4786 server_handle = server;
4787 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4789 NULL, /* Closure passed to MESH functions */
4790 &core_init, /* Call core_init once connected */
4791 &core_connect, /* Handle connects */
4792 &core_disconnect, /* remove peers on disconnects */
4793 NULL, /* Don't notify about all incoming messages */
4794 GNUNET_NO, /* For header only in notification */
4795 NULL, /* Don't notify about all outbound messages */
4796 GNUNET_NO, /* For header-only out notification */
4797 core_handlers); /* Register these handlers */
4799 if (core_handle == NULL)
4802 GNUNET_SCHEDULER_shutdown ();
4807 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4810 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4812 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4813 GNUNET_SCHEDULER_shutdown ();
4816 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4817 GNUNET_free (keyfile);
4818 if (my_private_key == NULL)
4820 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4821 _("Mesh service could not access hostkey. Exiting.\n"));
4822 GNUNET_SCHEDULER_shutdown ();
4825 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4826 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4827 &my_full_id.hashPubKey);
4828 myid = GNUNET_PEER_intern (&my_full_id);
4830 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4837 dht_handle = GNUNET_DHT_connect (c, 64);
4838 if (dht_handle == NULL)
4844 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4846 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4847 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4848 peers = GNUNET_CONTAINER_multihashmap_create (32);
4849 applications = GNUNET_CONTAINER_multihashmap_create (32);
4850 types = GNUNET_CONTAINER_multihashmap_create (32);
4852 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4853 nc = GNUNET_SERVER_notification_context_create (server_handle,
4855 GNUNET_SERVER_disconnect_notify (server_handle,
4856 &handle_local_client_disconnect, NULL);
4860 clients_tail = NULL;
4863 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4864 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4866 /* Create a peer_info for the local peer */
4867 peer = peer_info_get (&my_full_id);
4870 GNUNET_PEER_change_rc (myid, 1);
4871 peer_info_add_path (peer, p, GNUNET_YES);
4873 /* Scheduled the task to clean up when shutdown is called */
4874 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
4881 * The main function for the mesh service.
4883 * @param argc number of arguments from the command line
4884 * @param argv command line arguments
4885 * @return 0 ok, 1 on error
4888 main (int argc, char *const *argv)
4892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
4895 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");