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 * Tunnel this message belongs to.
142 struct MeshTunnel *tunnel;
145 * Pointer to info stucture used as cls.
155 * Size of the message
162 * Struct containing all info possibly needed to build a package when called
165 struct MeshTransmissionDescriptor
167 /** ID of the tunnel this packet travels in */
168 struct MESH_TunnelID *origin;
170 /** Who was this message being sent to */
171 struct MeshPeerInfo *peer;
173 /** Ultimate destination of the packet */
174 GNUNET_PEER_Id destination;
176 /** Data descriptor */
177 struct MeshData* mesh_data;
182 * Struct containing all information regarding a given peer
192 * Last time we heard from this peer
194 struct GNUNET_TIME_Absolute last_contact;
197 * Number of attempts to reconnect so far
199 int n_reconnect_attempts;
202 * Paths to reach the peer, ordered by ascending hop count
204 struct MeshPeerPath *path_head;
207 * Paths to reach the peer, ordered by ascending hop count
209 struct MeshPeerPath *path_tail;
212 * Handle to stop the DHT search for a path to this peer
214 struct GNUNET_DHT_GetHandle *dhtget;
217 * Closure given to the DHT GET
219 struct MeshPathInfo *dhtgetcls;
222 * Array of tunnels this peer participates in
223 * (most probably a small amount, therefore not a hashmap)
224 * When the path to the peer changes, notify these tunnels to let them
225 * re-adjust their path trees.
227 struct MeshTunnel **tunnels;
230 * Number of tunnels this peers participates in
232 unsigned int ntunnels;
235 * Transmission queue to core DLL head
237 struct MeshPeerQueue *queue_head;
240 * Transmission queue to core DLL tail
242 struct MeshPeerQueue *queue_tail;
245 * How many messages are in the queue to this peer.
247 unsigned int queue_n;
250 * Handle to for queued transmissions
252 struct GNUNET_CORE_TransmitHandle *core_transmit;
257 * Globally unique tunnel identification (owner + number)
258 * DO NOT USE OVER THE NETWORK
263 * Node that owns the tunnel
268 * Tunnel number to differentiate all the tunnels owned by the node oid
269 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
271 MESH_TunnelNumber tid;
275 struct MeshClient; /* FWD declaration */
278 * Struct containing all information regarding a tunnel
279 * For an intermediate node the improtant info used will be:
280 * - id Tunnel unique identification
281 * - paths[0] To know where to send it next
282 * - metainfo: ready, speeds, accounting
289 struct MESH_TunnelID id;
292 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
294 MESH_TunnelNumber local_tid;
297 * Local tunnel number for local destination clients (incoming number)
298 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
301 MESH_TunnelNumber local_tid_dest;
304 * Global count ID of the last *multicast* packet seen/sent.
309 * Local count ID of the last packet seen/sent.
314 * SKIP value for this tunnel.
319 * How many messages are in the queue.
321 unsigned int queue_n;
324 * How many messages do we accept in the queue.
326 unsigned int queue_max;
329 * Last time the tunnel was used
331 struct GNUNET_TIME_Absolute timestamp;
334 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
335 * containing peers added by id or by type, not intermediate peers.
337 struct GNUNET_CONTAINER_MultiHashMap *peers;
340 * Number of peers that are connected and potentially ready to receive data
342 unsigned int peers_ready;
345 * Number of peers that have been added to the tunnel
347 unsigned int peers_total;
350 * Client owner of the tunnel, if any
352 struct MeshClient *owner;
355 * Clients that have been informed about the tunnel, if any
357 struct MeshClient **clients;
360 * Number of elements in clients
362 unsigned int nclients;
365 * Clients that have requested to leave the tunnel
367 struct MeshClient **ignore;
370 * Number of elements in clients
372 unsigned int nignore;
377 GNUNET_PEER_Id *blacklisted;
380 * Number of elements in blacklisted
382 unsigned int nblacklisted;
387 struct MeshTunnelTree *tree;
390 * Application type we are looking for in this tunnel
392 GNUNET_MESH_ApplicationType type;
395 * Used to search peers offering a service
397 struct GNUNET_DHT_GetHandle *dht_get_type;
400 * Task to keep the used paths alive
402 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
405 * Task to destroy the tunnel after timeout
407 * FIXME: merge the two? a tunnel will have either
408 * a path refresh OR a timeout, never both!
410 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
415 * Info needed to work with tunnel paths and peers
422 struct MeshTunnel *t;
425 * Neighbouring peer to whom we send the packet to
427 struct MeshPeerInfo *peer;
432 struct MeshPeerPath *path;
437 * Struct containing information about a client of the service
444 struct MeshClient *next;
449 struct MeshClient *prev;
452 * Tunnels that belong to this client, indexed by local id
454 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
457 * Tunnels this client has accepted, indexed by incoming local id
459 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
462 * Tunnels this client has rejected, indexed by incoming local id
464 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
466 * Handle to communicate with the client
468 struct GNUNET_SERVER_Client *handle;
471 * Applications that this client has claimed to provide
473 struct GNUNET_CONTAINER_MultiHashMap *apps;
476 * Messages that this client has declared interest in
478 struct GNUNET_CONTAINER_MultiHashMap *types;
481 * Whether the client is active or shutting down (don't send confirmations
482 * to a client that is shutting down.
487 * ID of the client, mainly for debug messages
495 /******************************************************************************/
496 /************************ DEBUG FUNCTIONS ****************************/
497 /******************************************************************************/
501 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
502 * @param cls string to print
503 * @param success GNUNET_OK if the PUT was transmitted,
504 * GNUNET_NO on timeout,
505 * GNUNET_SYSERR on disconnect from service
506 * after the PUT message was transmitted
507 * (so we don't know if it was received or not)
512 mesh_debug (void *cls, int success)
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
522 /******************************************************************************/
523 /*********************** GLOBAL VARIABLES ****************************/
524 /******************************************************************************/
529 static struct MeshClient *clients;
530 static struct MeshClient *clients_tail;
533 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
535 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
538 * Tunnels incoming, indexed by MESH_TunnelNumber
539 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
541 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
544 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
546 static struct GNUNET_CONTAINER_MultiHashMap *peers;
549 * Handle to communicate with transport
551 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
554 * Handle to communicate with core
556 static struct GNUNET_CORE_Handle *core_handle;
561 static struct GNUNET_DHT_Handle *dht_handle;
566 static struct GNUNET_SERVER_Handle *server_handle;
569 * Notification context, to send messages to local clients
571 static struct GNUNET_SERVER_NotificationContext *nc;
574 * Local peer own ID (memory efficient handle)
576 static GNUNET_PEER_Id myid;
579 * Local peer own ID (full value)
581 static struct GNUNET_PeerIdentity my_full_id;
586 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
591 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
594 * Tunnel ID for the next created tunnel (global tunnel number)
596 static MESH_TunnelNumber next_tid;
599 * Tunnel ID for the next incoming tunnel (local tunnel number)
601 static MESH_TunnelNumber next_local_tid;
604 * All application types provided by this peer
606 static struct GNUNET_CONTAINER_MultiHashMap *applications;
609 * All message types clients of this peer are interested in
611 static struct GNUNET_CONTAINER_MultiHashMap *types;
614 * Task to periodically announce provided applications
616 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
619 * Task to periodically announce itself in the network
621 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
624 * Next ID to assign to a client
626 unsigned int next_client_id;
630 /******************************************************************************/
631 /************************ ITERATORS ****************************/
632 /******************************************************************************/
634 /* FIXME move iterators here */
637 /******************************************************************************/
638 /************************ PERIODIC FUNCTIONS ****************************/
639 /******************************************************************************/
642 * Announce iterator over for each application provided by the peer
645 * @param key current key code
646 * @param value value in the hash map
647 * @return GNUNET_YES if we should continue to
652 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
654 /* FIXME are hashes in multihash map equal on all aquitectures? */
655 /* FIXME: keep return value of 'put' to possibly cancel!? */
656 GNUNET_DHT_put (dht_handle, key, 10,
657 GNUNET_DHT_RO_RECORD_ROUTE |
658 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
659 sizeof (struct GNUNET_PeerIdentity),
660 (const char *) &my_full_id,
661 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
663 APP_ANNOUNCE_TIME, NULL, NULL);
669 * Periodically announce what applications are provided by local clients
672 * @param tc task context
675 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
677 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
679 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
683 DEBUG_DHT ("Starting PUT for apps\n");
685 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
687 announce_applications_task =
688 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
690 DEBUG_DHT ("Finished PUT for apps\n");
697 * Periodically announce self id in the DHT
700 * @param tc task context
703 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
705 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
707 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
711 * - Set data expiration in function of X
714 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
716 GNUNET_DHT_put (dht_handle, /* DHT handle */
717 &my_full_id.hashPubKey, /* Key to use */
718 10, /* Replication level */
719 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
720 GNUNET_BLOCK_TYPE_TEST, /* Block type */
721 sizeof (my_full_id), /* Size of the data */
722 (char *) &my_full_id, /* Data itself */
723 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
724 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
725 NULL, /* Continuation */
726 NULL); /* Continuation closure */
728 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
733 * Function to process paths received for a new peer addition. The recorded
734 * paths form the initial tunnel, which can be optimized later.
735 * Called on each result obtained for the DHT search.
738 * @param exp when will this value expire
739 * @param key key of the result
740 * @param type type of the result
741 * @param size number of bytes in data
742 * @param data pointer to the result data
745 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
746 const struct GNUNET_HashCode * key,
747 const struct GNUNET_PeerIdentity *get_path,
748 unsigned int get_path_length,
749 const struct GNUNET_PeerIdentity *put_path,
750 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
751 size_t size, const void *data);
754 /******************************************************************************/
755 /****************** GENERAL HELPER FUNCTIONS ************************/
756 /******************************************************************************/
759 * Search for a tunnel by global ID using full PeerIdentities
761 * @param oid owner of the tunnel
762 * @param tid global tunnel number
764 * @return tunnel handler, NULL if doesn't exist
766 static struct MeshTunnel *
767 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
771 * Delete an active client from the tunnel.
777 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
780 * Notify a tunnel that a connection has broken that affects at least
783 * @param t Tunnel affected.
784 * @param p1 Peer that got disconnected from p2.
785 * @param p2 Peer that got disconnected from p1.
787 * @return Short ID of the peer disconnected (either p1 or p2).
788 * 0 if the tunnel remained unaffected.
790 static GNUNET_PEER_Id
791 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
796 * Check if client has registered with the service and has not disconnected
798 * @param client the client to check
800 * @return non-NULL if client exists in the global DLL
802 static struct MeshClient *
803 client_get (struct GNUNET_SERVER_Client *client)
805 struct MeshClient *c;
810 if (c->handle == client)
819 * Checks if a given client has subscribed to certain message type
821 * @param message_type Type of message to check
822 * @param c Client to check
824 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
829 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
831 struct GNUNET_HashCode hc;
833 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
834 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
839 * Allow a client to send more data after transmitting a multicast message
840 * which some neighbor has not yet accepted altough a reasonable time has
843 * @param cls Closure (DataDescriptor containing the task identifier)
844 * @param tc Task Context
846 * FIXME reference counter cshould be just int
849 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
851 struct MeshData *mdata = cls;
853 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
855 GNUNET_assert (NULL != mdata->reference_counter);
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
858 *(mdata->reference_counter));
859 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
860 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
865 * Check whether client wants traffic from a tunnel.
867 * @param c Client to check.
868 * @param t Tunnel to be found.
870 * @return GNUNET_YES if client knows tunnel.
872 * TODO look in client hashmap
875 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
879 for (i = 0; i < t->nclients; i++)
880 if (t->clients[i] == c)
887 * Check whether client has been informed about a tunnel.
889 * @param c Client to check.
890 * @param t Tunnel to be found.
892 * @return GNUNET_YES if client knows tunnel.
894 * TODO look in client hashmap
897 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
901 for (i = 0; i < t->nignore; i++)
902 if (t->ignore[i] == c)
904 return client_wants_tunnel(c, t);
909 * Marks a client as uninterested in traffic from the tunnel, updating both
910 * client and tunnel to reflect this.
912 * @param c Client that doesn't want traffic anymore.
913 * @param t Tunnel which should be ignored.
915 * FIXME when to delete an incoming tunnel?
918 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
920 struct GNUNET_HashCode hash;
922 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
923 GNUNET_break (GNUNET_YES ==
924 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
926 GNUNET_break (GNUNET_YES ==
927 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
928 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
929 tunnel_delete_active_client (t, c);
930 GNUNET_array_append (t->ignore, t->nignore, c);
935 * Deletes a tunnel from a client (either owner or destination). To be used on
936 * tunnel destroy, otherwise, use client_ignore_tunnel.
938 * @param c Client whose tunnel to delete.
939 * @param t Tunnel which should be deleted.
942 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
944 struct GNUNET_HashCode hash;
948 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
949 GNUNET_assert (GNUNET_YES ==
950 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
956 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
958 GNUNET_assert (GNUNET_YES ==
959 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
963 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
972 * Send the message to all clients that have subscribed to its type
974 * @param msg Pointer to the message itself
975 * @param payload Pointer to the payload of the message.
976 * @return number of clients this message was sent to
979 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
980 const struct GNUNET_MessageHeader *payload)
982 struct GNUNET_PeerIdentity *oid;
983 struct MeshClient *c;
984 struct MeshTunnel *t;
985 MESH_TunnelNumber *tid;
988 char cbuf[htons (msg->size)];
990 type = ntohs (payload->type);
991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
994 memcpy (cbuf, msg, sizeof (cbuf));
995 switch (htons (msg->type))
997 struct GNUNET_MESH_Unicast *uc;
998 struct GNUNET_MESH_Multicast *mc;
999 struct GNUNET_MESH_ToOrigin *to;
1001 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1002 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1006 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1007 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1011 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1012 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1020 t = tunnel_get (oid, ntohl (*tid));
1027 for (count = 0, c = clients; c != NULL; c = c->next)
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1030 if (client_is_subscribed (type, c))
1032 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1036 *tid = htonl (t->local_tid);
1040 if (GNUNET_NO == client_knows_tunnel (c, t))
1042 /* This client doesn't know the tunnel */
1043 struct GNUNET_MESH_TunnelNotification tmsg;
1044 struct GNUNET_HashCode hash;
1046 tmsg.header.size = htons (sizeof (tmsg));
1047 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1048 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1049 tmsg.tunnel_id = htonl (t->local_tid_dest);
1050 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1051 &tmsg.header, GNUNET_NO);
1052 GNUNET_array_append (t->clients, t->nclients, c);
1053 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1055 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1056 c->incoming_tunnels, &hash, t,
1057 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1059 *tid = htonl (t->local_tid_dest);
1062 /* Check if the client wants to get traffic from the tunnel */
1063 if (GNUNET_NO == client_wants_tunnel(c, t))
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1067 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1068 (struct GNUNET_MessageHeader
1069 *) cbuf, GNUNET_YES);
1077 * Notify the client that owns the tunnel that a peer has connected to it
1078 * (the requested path to it has been confirmed).
1080 * @param t Tunnel whose owner to notify
1081 * @param id Short id of the peer that has connected
1084 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1086 struct GNUNET_MESH_PeerControl pc;
1088 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1089 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1090 pc.tunnel_id = htonl (t->local_tid);
1091 GNUNET_PEER_resolve (id, &pc.peer);
1092 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1098 * Notify all clients (not depending on registration status) that the incoming
1099 * tunnel is no longer valid.
1101 * @param t Tunnel that was destroyed.
1104 send_clients_tunnel_destroy (struct MeshTunnel *t)
1106 struct GNUNET_MESH_TunnelMessage msg;
1108 msg.header.size = htons (sizeof (msg));
1109 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1110 msg.tunnel_id = htonl (t->local_tid_dest);
1111 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1116 * Notify clients of tunnel disconnections, if needed.
1117 * In case the origin disconnects, the destination clients get a tunnel destroy
1118 * notification. If the last destination disconnects (only one remaining client
1119 * in tunnel), the origin gets a (local ID) peer disconnected.
1120 * Note that the function must be called BEFORE removing the client from
1123 * @param t Tunnel that was destroyed.
1124 * @param c Client that disconnected.
1127 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1133 struct GNUNET_MESH_TunnelMessage msg;
1135 msg.header.size = htons (sizeof (msg));
1136 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1137 msg.tunnel_id = htonl (t->local_tid_dest);
1138 for (i = 0; i < t->nclients; i++)
1139 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1140 &msg.header, GNUNET_NO);
1142 // FIXME when to disconnect an incoming tunnel?
1143 else if (1 == t->nclients && NULL != t->owner)
1145 struct GNUNET_MESH_PeerControl msg;
1147 msg.header.size = htons (sizeof (msg));
1148 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1149 msg.tunnel_id = htonl (t->local_tid);
1150 msg.peer = my_full_id;
1151 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1152 &msg.header, GNUNET_NO);
1158 * Decrements the reference counter and frees all resources if needed
1160 * @param mesh_data Data Descriptor used in a multicast message.
1161 * Freed no longer needed (last message).
1164 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
1166 /* Make sure it's a multicast packet */
1167 GNUNET_assert (NULL != mesh_data->reference_counter);
1169 if (0 == --(*(mesh_data->reference_counter)))
1171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1172 if (NULL != mesh_data->task)
1174 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1176 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1178 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1180 GNUNET_free (mesh_data->task);
1182 GNUNET_free (mesh_data->reference_counter);
1183 GNUNET_free (mesh_data->data);
1184 GNUNET_free (mesh_data);
1190 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1191 * and insert it in the appropiate structures if the peer is not known yet.
1193 * @param peer Full identity of the peer.
1195 * @return Existing or newly created peer info.
1197 static struct MeshPeerInfo *
1198 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1200 struct MeshPeerInfo *peer_info;
1202 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1203 if (NULL == peer_info)
1206 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1207 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1208 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1209 peer_info->id = GNUNET_PEER_intern (peer);
1217 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1218 * and insert it in the appropiate structures if the peer is not known yet.
1220 * @param peer Short identity of the peer.
1222 * @return Existing or newly created peer info.
1224 static struct MeshPeerInfo *
1225 peer_info_get_short (const GNUNET_PEER_Id peer)
1227 struct GNUNET_PeerIdentity id;
1229 GNUNET_PEER_resolve (peer, &id);
1230 return peer_info_get (&id);
1235 * Iterator to remove the tunnel from the list of tunnels a peer participates
1238 * @param cls Closure (tunnel info)
1239 * @param key GNUNET_PeerIdentity of the peer (unused)
1240 * @param value PeerInfo of the peer
1242 * @return always GNUNET_YES, to keep iterating
1245 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1247 struct MeshTunnel *t = cls;
1248 struct MeshPeerInfo *peer = value;
1251 for (i = 0; i < peer->ntunnels; i++)
1254 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1257 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1258 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1267 * Queue and pass message to core when possible.
1269 * @param cls Closure (type dependant).
1270 * @param type Type of the message.
1271 * @param size Size of the message.
1272 * @param dst Neighbor to send message to.
1273 * @param t Tunnel this message belongs to.
1276 queue_add (void *cls, uint16_t type, size_t size,
1277 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1280 * Core callback to write a pre-constructed data packet to core buffer
1282 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1283 * @param size Number of bytes available in buf.
1284 * @param buf Where the to write the message.
1286 * @return number of bytes written to buf
1289 send_core_data_raw (void *cls, size_t size, void *buf)
1291 struct MeshTransmissionDescriptor *info = cls;
1292 struct GNUNET_MessageHeader *msg;
1295 GNUNET_assert (NULL != info);
1296 GNUNET_assert (NULL != info->mesh_data);
1297 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1298 total_size = ntohs (msg->size);
1300 if (total_size > size)
1305 memcpy (buf, msg, total_size);
1306 GNUNET_free (info->mesh_data);
1313 * Sends an already built unicast message to a peer, properly registrating
1314 * all used resources.
1316 * @param message Message to send. Function makes a copy of it.
1317 * @param peer Short ID of the neighbor whom to send the message.
1318 * @param t Tunnel on which this message is transmitted.
1321 send_message (const struct GNUNET_MessageHeader *message,
1322 const struct GNUNET_PeerIdentity *peer,
1323 struct MeshTunnel *t)
1325 struct MeshTransmissionDescriptor *info;
1326 struct MeshPeerInfo *neighbor;
1327 struct MeshPeerPath *p;
1330 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1332 size = ntohs (message->size);
1333 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1334 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1335 info->mesh_data->data = GNUNET_malloc (size);
1336 memcpy (info->mesh_data->data, message, size);
1337 info->mesh_data->data_len = size;
1338 neighbor = peer_info_get (peer);
1339 for (p = neighbor->path_head; NULL != p; p = p->next)
1349 GNUNET_free (info->mesh_data->data);
1350 GNUNET_free (info->mesh_data);
1354 info->peer = neighbor;
1356 GNUNET_MESSAGE_TYPE_MESH_UNICAST,
1364 * Sends a CREATE PATH message for a path to a peer, properly registrating
1365 * all used resources.
1367 * @param peer PeerInfo of the final peer for whom this path is being created.
1368 * @param p Path itself.
1369 * @param t Tunnel for which the path is created.
1372 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1373 struct MeshTunnel *t)
1375 struct GNUNET_PeerIdentity id;
1376 struct MeshPathInfo *path_info;
1377 struct MeshPeerInfo *neighbor;
1383 p = tree_get_path_to_peer (t->tree, peer->id);
1390 for (i = 0; i < p->length; i++)
1392 if (p->peers[i] == myid)
1395 if (i >= p->length - 1)
1401 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1403 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1404 path_info->path = p;
1406 neighbor = peer_info_get (&id);
1407 path_info->peer = neighbor;
1408 queue_add (path_info,
1409 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1410 sizeof (struct GNUNET_MESH_ManipulatePath) +
1411 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1418 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1420 * @param t Tunnel whose path to destroy.
1421 * @param destination Short ID of the peer to whom the path to destroy.
1424 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1426 struct MeshPeerPath *p;
1429 p = tree_get_path_to_peer (t->tree, destination);
1435 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1436 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1438 struct GNUNET_MESH_ManipulatePath *msg;
1439 struct GNUNET_PeerIdentity *pi;
1443 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1444 msg->header.size = htons (size);
1445 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1446 msg->tid = htonl (t->id.tid);
1447 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1448 for (i = 0; i < p->length; i++)
1450 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1452 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
1459 * Try to establish a new connection to this peer.
1460 * Use the best path for the given tunnel.
1461 * If the peer doesn't have any path to it yet, try to get one.
1462 * If the peer already has some path, send a CREATE PATH towards it.
1464 * @param peer PeerInfo of the peer.
1465 * @param t Tunnel for which to create the path, if possible.
1468 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1470 struct MeshPeerPath *p;
1471 struct MeshPathInfo *path_info;
1473 if (NULL != peer->path_head)
1475 p = tree_get_path_to_peer (t->tree, peer->id);
1482 // FIXME always send create path to self
1485 send_create_path (peer, p, t);
1489 struct GNUNET_HashCode hash;
1492 send_client_peer_connected (t, myid);
1493 t->local_tid_dest = next_local_tid++;
1494 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1497 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1498 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1505 else if (NULL == peer->dhtget)
1507 struct GNUNET_PeerIdentity id;
1509 GNUNET_PEER_resolve (peer->id, &id);
1510 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1511 path_info->peer = peer;
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1515 peer->dhtgetcls = path_info;
1516 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1517 GNUNET_BLOCK_TYPE_TEST, /* type */
1518 &id.hashPubKey, /* key to search */
1519 10, /* replication level */
1520 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1521 0, /* xquery bits */
1522 &dht_get_id_handler, path_info);
1524 /* Otherwise, there is no path but the DHT get is already started. */
1529 * Task to delay the connection of a peer
1531 * @param cls Closure (path info with tunnel and peer to connect).
1532 * Will be free'd on exection.
1533 * @param tc TaskContext
1536 peer_info_connect_task (void *cls,
1537 const struct GNUNET_SCHEDULER_TaskContext *tc)
1539 struct MeshPathInfo *path_info = cls;
1541 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1546 peer_info_connect (path_info->peer, path_info->t);
1552 * Destroy the peer_info and free any allocated resources linked to it
1554 * @param pi The peer_info to destroy.
1556 * @return GNUNET_OK on success
1559 peer_info_destroy (struct MeshPeerInfo *pi)
1561 struct GNUNET_PeerIdentity id;
1562 struct MeshPeerPath *p;
1563 struct MeshPeerPath *nextp;
1565 GNUNET_PEER_resolve (pi->id, &id);
1566 GNUNET_PEER_change_rc (pi->id, -1);
1569 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1572 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1573 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1575 if (NULL != pi->dhtget)
1577 GNUNET_DHT_get_stop (pi->dhtget);
1578 GNUNET_free (pi->dhtgetcls);
1584 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1594 * Remove all paths that rely on a direct connection between p1 and p2
1595 * from the peer itself and notify all tunnels about it.
1597 * @param peer PeerInfo of affected peer.
1598 * @param p1 GNUNET_PEER_Id of one peer.
1599 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1602 * TODO: optimize (see below)
1605 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1608 struct MeshPeerPath *p;
1609 struct MeshPeerPath *aux;
1610 struct MeshPeerInfo *peer_d;
1612 unsigned int destroyed;
1618 p = peer->path_head;
1622 for (i = 0; i < (p->length - 1); i++)
1624 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1625 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1627 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1638 for (i = 0; i < peer->ntunnels; i++)
1640 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1644 * Problem: one or more peers have been deleted from the tunnel tree.
1645 * We don't know who they are to try to add them again.
1646 * We need to try to find a new path for each of the disconnected peers.
1647 * Some of them might already have a path to reach them that does not
1648 * involve p1 and p2. Adding all anew might render in a better tree than
1649 * the trivial immediate fix.
1651 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1652 * its children will be reachable trough him.
1654 peer_d = peer_info_get_short (d);
1657 for (p = peer_d->path_head; NULL != p; p = p->next)
1659 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1667 /* No callback, as peer will be already disconnected and a connection
1668 * scheduled by tunnel_notify_connection_broken.
1670 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1674 peer_info_connect (peer_d, peer->tunnels[i]);
1681 * Add the path to the peer and update the path used to reach it in case this
1684 * @param peer_info Destination peer to add the path to.
1685 * @param path New path to add. Last peer must be the peer in arg 1.
1686 * Path will be either used of freed if already known.
1687 * @param trusted Do we trust that this path is real?
1690 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1693 struct MeshPeerPath *aux;
1697 if ((NULL == peer_info) || (NULL == path))
1700 path_destroy (path);
1703 if (path->peers[path->length - 1] != peer_info->id)
1706 path_destroy (path);
1709 if (path->length <= 2 && GNUNET_NO == trusted)
1711 /* Only allow CORE to tell us about direct paths */
1712 path_destroy (path);
1715 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1716 for (l = 1; l < path->length; l++)
1718 if (path->peers[l] == myid)
1720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1721 for (l2 = 0; l2 < path->length - l; l2++)
1723 path->peers[l2] = path->peers[l + l2];
1728 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1733 struct GNUNET_PeerIdentity id;
1735 GNUNET_PEER_resolve (peer_info->id, &id);
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1737 path->length, GNUNET_i2s (&id));
1740 l = path_get_length (path);
1747 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1748 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1750 l2 = path_get_length (aux);
1753 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1754 peer_info->path_tail, aux, path);
1759 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1761 path_destroy (path);
1766 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1773 * Add the path to the origin peer and update the path used to reach it in case
1774 * this is the shortest.
1775 * The path is given in peer_info -> destination, therefore we turn the path
1776 * upside down first.
1778 * @param peer_info Peer to add the path to, being the origin of the path.
1779 * @param path New path to add after being inversed.
1780 * @param trusted Do we trust that this path is real?
1783 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1784 struct MeshPeerPath *path, int trusted)
1787 peer_info_add_path (peer_info, path, trusted);
1792 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1793 * to obtain a local peer -> destination path and interning the peer ids.
1795 * @return Newly allocated and created path
1797 static struct MeshPeerPath *
1798 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1799 unsigned int get_path_length,
1800 const struct GNUNET_PeerIdentity *put_path,
1801 unsigned int put_path_length)
1803 struct MeshPeerPath *p;
1809 GNUNET_PEER_change_rc (myid, 1);
1810 i = get_path_length;
1811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1812 for (i--; i >= 0; i--)
1814 id = GNUNET_PEER_intern (&get_path[i]);
1815 if (p->length > 0 && id == p->peers[p->length - 1])
1817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1818 GNUNET_PEER_change_rc (id, -1);
1822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1823 GNUNET_i2s (&get_path[i]));
1825 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1826 p->peers[p->length - 1] = id;
1829 i = put_path_length;
1830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1831 for (i--; i >= 0; i--)
1833 id = GNUNET_PEER_intern (&put_path[i]);
1836 /* PUT path went through us, so discard the path up until now and start
1837 * from here to get a much shorter (and loop-free) path.
1842 if (p->length > 0 && id == p->peers[p->length - 1])
1844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1845 GNUNET_PEER_change_rc (id, -1);
1849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1850 GNUNET_i2s (&put_path[i]));
1852 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1853 p->peers[p->length - 1] = id;
1857 if (get_path_length > 0)
1858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1859 GNUNET_i2s (&get_path[0]));
1860 if (put_path_length > 0)
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1862 GNUNET_i2s (&put_path[0]));
1863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1865 for (i = 0; i < p->length; i++)
1867 struct GNUNET_PeerIdentity peer_id;
1869 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1871 GNUNET_i2s (&peer_id));
1879 * Adds a path to the peer_infos of all the peers in the path
1881 * @param p Path to process.
1882 * @param confirmed Whether we know if the path works or not. FIXME use
1885 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1889 /* TODO: invert and add */
1890 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1891 for (i++; i < p->length; i++)
1893 struct MeshPeerInfo *aux;
1894 struct MeshPeerPath *copy;
1896 aux = peer_info_get_short (p->peers[i]);
1897 copy = path_duplicate (p);
1898 copy->length = i + 1;
1899 peer_info_add_path (aux, copy, GNUNET_NO);
1905 * Send keepalive packets for a peer
1907 * @param cls Closure (tunnel for which to send the keepalive).
1908 * @param tc Notification context.
1910 * TODO: implement explicit multicast keepalive?
1913 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1917 * Search for a tunnel among the incoming tunnels
1919 * @param tid the local id of the tunnel
1921 * @return tunnel handler, NULL if doesn't exist
1923 static struct MeshTunnel *
1924 tunnel_get_incoming (MESH_TunnelNumber tid)
1926 struct GNUNET_HashCode hash;
1928 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1929 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1930 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1935 * Search for a tunnel among the tunnels for a client
1937 * @param c the client whose tunnels to search in
1938 * @param tid the local id of the tunnel
1940 * @return tunnel handler, NULL if doesn't exist
1942 static struct MeshTunnel *
1943 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1945 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1947 return tunnel_get_incoming (tid);
1951 struct GNUNET_HashCode hash;
1953 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1954 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1960 * Search for a tunnel by global ID using PEER_ID
1962 * @param pi owner of the tunnel
1963 * @param tid global tunnel number
1965 * @return tunnel handler, NULL if doesn't exist
1967 static struct MeshTunnel *
1968 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1970 struct MESH_TunnelID id;
1971 struct GNUNET_HashCode hash;
1976 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1977 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1982 * Search for a tunnel by global ID using full PeerIdentities
1984 * @param oid owner of the tunnel
1985 * @param tid global tunnel number
1987 * @return tunnel handler, NULL if doesn't exist
1989 static struct MeshTunnel *
1990 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1992 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1997 * Delete an active client from the tunnel.
2003 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2007 for (i = 0; i < t->nclients; i++)
2009 if (t->clients[i] == c)
2011 t->clients[i] = t->clients[t->nclients - 1];
2012 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2020 * Delete an ignored client from the tunnel.
2026 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2030 for (i = 0; i < t->nignore; i++)
2032 if (t->ignore[i] == c)
2034 t->ignore[i] = t->ignore[t->nignore - 1];
2035 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2043 * Delete a client from the tunnel. It should be only done on
2044 * client disconnection, otherwise use client_ignore_tunnel.
2050 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2052 tunnel_delete_ignored_client (t, c);
2053 tunnel_delete_active_client (t, c);
2058 * Callback used to notify a client owner of a tunnel that a peer has
2059 * disconnected, most likely because of a path change.
2061 * @param cls Closure (tunnel this notification is about).
2062 * @param peer_id Short ID of disconnected peer.
2065 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2067 struct MeshTunnel *t = cls;
2068 struct MeshPeerInfo *peer;
2069 struct MeshPathInfo *path_info;
2071 if (NULL != t->owner && NULL != nc)
2073 struct GNUNET_MESH_PeerControl msg;
2075 msg.header.size = htons (sizeof (msg));
2076 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2077 msg.tunnel_id = htonl (t->local_tid);
2078 GNUNET_PEER_resolve (peer_id, &msg.peer);
2079 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2080 &msg.header, GNUNET_NO);
2082 peer = peer_info_get_short (peer_id);
2083 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2084 path_info->peer = peer;
2086 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2091 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2092 * needed rescources.
2093 * If peer already exists, reevaluate shortest path and change if different.
2095 * @param t Tunnel we want to add a new peer to
2096 * @param peer PeerInfo of the peer being added
2100 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2102 struct GNUNET_PeerIdentity id;
2103 struct MeshPeerPath *best_p;
2104 struct MeshPeerPath *p;
2105 unsigned int best_cost;
2108 GNUNET_PEER_resolve (peer->id, &id);
2110 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2113 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2114 GNUNET_assert (GNUNET_OK ==
2115 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2117 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2120 if (NULL != (p = peer->path_head))
2123 best_cost = tree_get_path_cost (t->tree, p);
2126 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2133 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2134 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2135 t->path_refresh_task =
2136 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2140 /* Start a DHT get */
2141 peer_info_connect (peer, t);
2146 * Add a path to a tunnel which we don't own, just to remember the next hop.
2147 * If destination node was already in the tunnel, the first hop information
2148 * will be replaced with the new path.
2150 * @param t Tunnel we want to add a new peer to
2151 * @param p Path to add
2152 * @param own_pos Position of local node in path.
2156 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2157 unsigned int own_pos)
2159 struct GNUNET_PeerIdentity id;
2161 GNUNET_assert (0 != own_pos);
2162 tree_add_path (t->tree, p, NULL, NULL);
2163 if (own_pos < p->length - 1)
2165 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2166 tree_update_first_hops (t->tree, myid, &id);
2172 * Notifies a tunnel that a connection has broken that affects at least
2173 * some of its peers. Sends a notification towards the root of the tree.
2174 * In case the peer is the owner of the tree, notifies the client that owns
2175 * the tunnel and tries to reconnect.
2177 * @param t Tunnel affected.
2178 * @param p1 Peer that got disconnected from p2.
2179 * @param p2 Peer that got disconnected from p1.
2181 * @return Short ID of the peer disconnected (either p1 or p2).
2182 * 0 if the tunnel remained unaffected.
2184 static GNUNET_PEER_Id
2185 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2191 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2193 if (myid != p1 && myid != p2)
2199 if (tree_get_predecessor (t->tree) != 0)
2201 /* We are the peer still connected, notify owner of the disconnection. */
2202 struct GNUNET_MESH_PathBroken msg;
2203 struct GNUNET_PeerIdentity neighbor;
2205 msg.header.size = htons (sizeof (msg));
2206 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2207 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2208 msg.tid = htonl (t->id.tid);
2209 msg.peer1 = my_full_id;
2210 GNUNET_PEER_resolve (pid, &msg.peer2);
2211 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2212 send_message (&msg.header, &neighbor, t);
2220 * Send a multicast packet to a neighbor.
2222 * @param cls Closure (Info about the multicast packet)
2223 * @param neighbor_id Short ID of the neighbor to send the packet to.
2226 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2228 struct MeshData *mdata = cls;
2229 struct MeshTransmissionDescriptor *info;
2230 struct GNUNET_PeerIdentity neighbor;
2232 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2234 info->mesh_data = mdata;
2235 (*(mdata->reference_counter)) ++;
2236 info->destination = neighbor_id;
2237 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2239 GNUNET_i2s (&neighbor));
2240 info->peer = peer_info_get (&neighbor);
2241 GNUNET_assert (NULL != info->peer);
2243 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
2244 info->mesh_data->data_len,
2250 * Send a message in a tunnel in multicast, sending a copy to each child node
2251 * down the local one in the tunnel tree.
2253 * @param t Tunnel in which to send the data.
2254 * @param msg Message to be sent.
2255 * @param internal Has the service generated this message?
2258 tunnel_send_multicast (struct MeshTunnel *t,
2259 const struct GNUNET_MessageHeader *msg,
2262 struct MeshData *mdata;
2264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2265 " sending a multicast packet...\n");
2266 mdata = GNUNET_malloc (sizeof (struct MeshData));
2267 mdata->data_len = ntohs (msg->size);
2268 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2270 mdata->data = GNUNET_malloc (mdata->data_len);
2271 memcpy (mdata->data, msg, mdata->data_len);
2272 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2274 struct GNUNET_MESH_Multicast *mcast;
2276 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2277 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2279 ntohl (mcast->ttl));
2283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2285 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2286 && GNUNET_NO == internal)
2288 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2290 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2296 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2297 if (*(mdata->reference_counter) == 0)
2299 GNUNET_free (mdata->data);
2300 GNUNET_free (mdata->reference_counter);
2301 if (NULL != mdata->task)
2303 GNUNET_SCHEDULER_cancel(*(mdata->task));
2304 GNUNET_free (mdata->task);
2305 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2307 // FIXME change order?
2308 GNUNET_free (mdata);
2310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2311 " sending a multicast packet done\n");
2317 * Send a message to all peers in this tunnel that the tunnel is no longer
2320 * @param t The tunnel whose peers to notify.
2323 tunnel_send_destroy (struct MeshTunnel *t)
2325 struct GNUNET_MESH_TunnelDestroy msg;
2327 msg.header.size = htons (sizeof (msg));
2328 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2329 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2330 msg.tid = htonl (t->id.tid);
2331 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2337 * Destroy the tunnel and free any allocated resources linked to it.
2339 * @param t the tunnel to destroy
2341 * @return GNUNET_OK on success
2344 tunnel_destroy (struct MeshTunnel *t)
2346 struct MeshClient *c;
2347 struct GNUNET_HashCode hash;
2358 struct GNUNET_PeerIdentity id;
2360 GNUNET_PEER_resolve (t->id.oid, &id);
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2362 GNUNET_i2s (&id), t->id.tid);
2364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2368 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2369 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2374 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2377 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2381 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2382 for (i = 0; i < t->nclients; i++)
2386 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2391 for (i = 0; i < t->nignore; i++)
2395 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2400 if (t->nclients > 0)
2403 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2407 GNUNET_free (t->clients);
2409 if (NULL != t->peers)
2411 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2413 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2416 tree_destroy (t->tree);
2417 if (NULL != t->dht_get_type)
2418 GNUNET_DHT_get_stop (t->dht_get_type);
2419 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2420 GNUNET_SCHEDULER_cancel (t->timeout_task);
2421 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2422 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2429 * Create a new tunnel
2431 * @param owner Who is the owner of the tunnel (short ID).
2432 * @param tid Tunnel Number of the tunnel.
2433 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2434 * @param local Tunnel Number for the tunnel, for the client point of view.
2437 static struct MeshTunnel *
2438 tunnel_new (GNUNET_PEER_Id owner,
2439 MESH_TunnelNumber tid,
2440 struct MeshClient *client,
2441 MESH_TunnelNumber local)
2443 struct MeshTunnel *t;
2444 struct GNUNET_HashCode hash;
2446 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2449 t->queue_max = 1000; // FIXME API parameter
2450 t->tree = tree_new (owner);
2452 t->local_tid = local;
2454 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2456 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2457 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2462 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2468 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2470 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2471 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2475 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2485 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2486 * that are no longer needed, as well as nodes of no longer reachable peers.
2487 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2489 * @param t Tunnel from which to remove the path.
2490 * @param peer Short id of the peer which should be removed.
2493 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2495 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2501 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2502 * client when the client disconnects. If the client is not the owner, the
2503 * owner will get notified if no more clients are in the tunnel and the client
2504 * get removed from the tunnel's list.
2506 * @param cls closure (client that is disconnecting)
2507 * @param key the hash of the local tunnel id (used to access the hashmap)
2508 * @param value the value stored at the key (tunnel to destroy)
2510 * @return GNUNET_OK on success
2513 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
2515 struct MeshTunnel *t = value;
2516 struct MeshClient *c = cls;
2519 send_client_tunnel_disconnect(t, c);
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2523 "Client %u is destination, keeping the tunnel alive.\n", c->id);
2524 tunnel_delete_client(t, c);
2525 client_delete_tunnel(c, t);
2528 tunnel_send_destroy(t);
2529 r = tunnel_destroy (t);
2535 * Timeout function, destroys tunnel if called
2537 * @param cls Closure (tunnel to destroy).
2538 * @param tc TaskContext
2541 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2543 struct MeshTunnel *t = cls;
2545 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2547 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2552 * Resets the tunnel timeout. Starts it if no timeout was running.
2554 * @param t Tunnel whose timeout to reset.
2557 tunnel_reset_timeout (struct MeshTunnel *t)
2559 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2560 GNUNET_SCHEDULER_cancel (t->timeout_task);
2562 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2563 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2567 /******************************************************************************/
2568 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2569 /******************************************************************************/
2572 * Function to send a create path packet to a peer.
2574 * @param cls closure
2575 * @param size number of bytes available in buf
2576 * @param buf where the callee should write the message
2577 * @return number of bytes written to buf
2580 send_core_path_create (void *cls, size_t size, void *buf)
2582 struct MeshPathInfo *info = cls;
2583 struct GNUNET_MESH_ManipulatePath *msg;
2584 struct GNUNET_PeerIdentity *peer_ptr;
2585 struct MeshTunnel *t = info->t;
2586 struct MeshPeerPath *p = info->path;
2590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2592 sizeof (struct GNUNET_MESH_ManipulatePath) +
2593 p->length * sizeof (struct GNUNET_PeerIdentity);
2595 if (size < size_needed || NULL == buf)
2600 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2601 msg->header.size = htons (size_needed);
2602 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2603 msg->tid = ntohl (t->id.tid);
2605 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2606 for (i = 0; i < p->length; i++)
2608 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2615 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2621 * Fill the core buffer
2623 * @param cls closure (data itself)
2624 * @param size number of bytes available in buf
2625 * @param buf where the callee should write the message
2627 * @return number of bytes written to buf
2630 send_core_data_multicast (void *cls, size_t size, void *buf)
2632 struct MeshTransmissionDescriptor *info = cls;
2635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
2636 GNUNET_assert (NULL != info);
2637 GNUNET_assert (NULL != info->peer);
2638 total_size = info->mesh_data->data_len;
2639 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2641 if (total_size > size)
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
2647 memcpy (buf, info->mesh_data->data, total_size);
2650 struct GNUNET_MESH_Multicast *mc;
2651 struct GNUNET_MessageHeader *mh;
2654 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2656 mc = (struct GNUNET_MESH_Multicast *) mh;
2657 mh = (struct GNUNET_MessageHeader *) &mc[1];
2658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2659 " multicast, payload type %u\n", ntohs (mh->type));
2660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2661 " multicast, payload size %u\n", ntohs (mh->size));
2665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
2670 data_descriptor_decrement_multicast (info->mesh_data);
2671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
2673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
2679 * Creates a path ack message in buf and frees all unused resources.
2681 * @param cls closure (MeshTransmissionDescriptor)
2682 * @param size number of bytes available in buf
2683 * @param buf where the callee should write the message
2684 * @return number of bytes written to buf
2687 send_core_path_ack (void *cls, size_t size, void *buf)
2689 struct MeshTransmissionDescriptor *info = cls;
2690 struct GNUNET_MESH_PathACK *msg = buf;
2692 GNUNET_assert (NULL != info);
2693 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2698 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2699 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2700 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2701 msg->tid = htonl (info->origin->tid);
2702 msg->peer_id = my_full_id;
2705 /* TODO add signature */
2707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2708 return sizeof (struct GNUNET_MESH_PathACK);
2713 * Free a transmission that was already queued with all resources
2714 * associated to the request.
2716 * @param queue Queue handler to cancel.
2717 * @param clear_cls Is it necessary to free associated cls?
2720 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2722 struct MeshTransmissionDescriptor *dd;
2723 struct MeshPathInfo *path_info;
2725 if (GNUNET_YES == clear_cls)
2727 switch (queue->type)
2729 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2730 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
2733 data_descriptor_decrement_multicast (dd->mesh_data);
2735 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2737 path_info = queue->cls;
2738 path_destroy (path_info->path);
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
2744 GNUNET_free_non_null (queue->cls);
2746 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2747 queue->peer->queue_tail,
2749 GNUNET_free (queue);
2754 * Core callback to write a queued packet to core buffer
2756 * @param cls Closure (peer info).
2757 * @param size Number of bytes available in buf.
2758 * @param buf Where the to write the message.
2760 * @return number of bytes written to buf
2763 queue_send (void *cls, size_t size, void *buf)
2765 struct MeshPeerInfo *peer = cls;
2766 struct MeshPeerQueue *queue;
2769 peer->core_transmit = NULL;
2770 queue = peer->queue_head;
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
2775 /* If queue is empty, send should have been cancelled */
2781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
2783 /* Check if buffer size is enough for the message */
2784 if (queue->size > size)
2786 struct GNUNET_PeerIdentity id;
2788 GNUNET_PEER_resolve (peer->id, &id);
2789 peer->core_transmit =
2790 GNUNET_CORE_notify_transmit_ready(core_handle,
2793 GNUNET_TIME_UNIT_FOREVER_REL,
2800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
2803 switch (queue->type)
2805 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast\n");
2807 data_size = send_core_data_raw (queue->cls, size, buf);
2809 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
2811 data_size = send_core_data_multicast(queue->cls, size, buf);
2813 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
2815 data_size = send_core_path_create(queue->cls, size, buf);
2817 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
2819 data_size = send_core_path_ack(queue->cls, size, buf);
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
2826 queue->tunnel->queue_n--;
2828 /* Free queue, but cls was freed by send_core_* */
2829 queue_destroy(queue, GNUNET_NO);
2831 /* If more data in queue, send next */
2832 if (NULL != peer->queue_head)
2834 struct GNUNET_PeerIdentity id;
2836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
2837 GNUNET_PEER_resolve (peer->id, &id);
2838 peer->core_transmit =
2839 GNUNET_CORE_notify_transmit_ready(core_handle,
2842 GNUNET_TIME_UNIT_FOREVER_REL,
2844 peer->queue_head->size,
2848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
2854 * Queue and pass message to core when possible.
2856 * @param cls Closure (type dependant).
2857 * @param type Type of the message.
2858 * @param size Size of the message.
2859 * @param dst Neighbor to send message to.
2860 * @param t Tunnel this message belongs to.
2863 queue_add (void *cls, uint16_t type, size_t size,
2864 struct MeshPeerInfo *dst, struct MeshTunnel *t)
2866 struct MeshPeerQueue *queue;
2868 if (t->queue_n >= t->queue_max)
2870 if (NULL == t->owner)
2871 GNUNET_break_op(0); // TODO: kill connection?
2874 return; // Drop message
2877 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2883 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
2884 if (NULL == dst->core_transmit)
2886 struct GNUNET_PeerIdentity id;
2888 GNUNET_PEER_resolve (dst->id, &id);
2889 dst->core_transmit =
2890 GNUNET_CORE_notify_transmit_ready(core_handle,
2893 GNUNET_TIME_UNIT_FOREVER_REL,
2902 /******************************************************************************/
2903 /******************** MESH NETWORK HANDLERS **************************/
2904 /******************************************************************************/
2908 * Core handler for path creation
2910 * @param cls closure
2911 * @param message message
2912 * @param peer peer identity this notification is about
2913 * @param atsi performance data
2914 * @param atsi_count number of records in 'atsi'
2916 * @return GNUNET_OK to keep the connection open,
2917 * GNUNET_SYSERR to close it (signal serious error)
2920 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2921 const struct GNUNET_MessageHeader *message,
2922 const struct GNUNET_ATS_Information *atsi,
2923 unsigned int atsi_count)
2925 unsigned int own_pos;
2928 MESH_TunnelNumber tid;
2929 struct GNUNET_MESH_ManipulatePath *msg;
2930 struct GNUNET_PeerIdentity *pi;
2931 struct GNUNET_HashCode hash;
2932 struct MeshPeerPath *path;
2933 struct MeshPeerInfo *dest_peer_info;
2934 struct MeshPeerInfo *orig_peer_info;
2935 struct MeshTunnel *t;
2937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2938 "Received a path create msg [%s]\n",
2939 GNUNET_i2s (&my_full_id));
2940 size = ntohs (message->size);
2941 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2943 GNUNET_break_op (0);
2947 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2948 if (size % sizeof (struct GNUNET_PeerIdentity))
2950 GNUNET_break_op (0);
2953 size /= sizeof (struct GNUNET_PeerIdentity);
2956 GNUNET_break_op (0);
2959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2960 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2962 tid = ntohl (msg->tid);
2963 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2965 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2966 t = tunnel_get (pi, tid);
2967 if (NULL == t) // FIXME only for INCOMING tunnels?
2969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2970 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
2972 while (NULL != tunnel_get_incoming (next_local_tid))
2973 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2974 t->local_tid_dest = next_local_tid++;
2975 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2977 tunnel_reset_timeout (t);
2978 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2980 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2981 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2989 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2990 if (NULL == dest_peer_info)
2992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2993 " Creating PeerInfo for destination.\n");
2994 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2995 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2996 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2998 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3000 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3001 if (NULL == orig_peer_info)
3003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3004 " Creating PeerInfo for origin.\n");
3005 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3006 orig_peer_info->id = GNUNET_PEER_intern (pi);
3007 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3008 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3011 path = path_new (size);
3013 for (i = 0; i < size; i++)
3015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3016 GNUNET_i2s (&pi[i]));
3017 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3018 if (path->peers[i] == myid)
3021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3024 /* cannot be self, must be 'not found' */
3025 /* create path: self not found in path through self */
3026 GNUNET_break_op (0);
3027 path_destroy (path);
3028 /* FIXME error. destroy tunnel? leave for timeout? */
3031 path_add_to_peers (path, GNUNET_NO);
3032 tunnel_add_path (t, path, own_pos);
3033 if (own_pos == size - 1)
3035 /* It is for us! Send ack. */
3036 struct MeshTransmissionDescriptor *info;
3038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3039 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3040 if (NULL == t->peers)
3042 /* New tunnel! Notify clients on data. */
3043 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
3045 GNUNET_break (GNUNET_OK ==
3046 GNUNET_CONTAINER_multihashmap_put (t->peers,
3047 &my_full_id.hashPubKey,
3050 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
3051 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3052 info->origin = &t->id;
3053 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3054 GNUNET_assert (NULL != info->peer);
3056 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3057 sizeof (struct GNUNET_MESH_PathACK),
3063 struct MeshPeerPath *path2;
3065 /* It's for somebody else! Retransmit. */
3066 path2 = path_duplicate (path);
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3068 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3069 path2 = path_duplicate (path);
3070 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3071 send_create_path (dest_peer_info, path, t);
3078 * Core handler for path destruction
3080 * @param cls closure
3081 * @param message message
3082 * @param peer peer identity this notification is about
3083 * @param atsi performance data
3084 * @param atsi_count number of records in 'atsi'
3086 * @return GNUNET_OK to keep the connection open,
3087 * GNUNET_SYSERR to close it (signal serious error)
3090 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3091 const struct GNUNET_MessageHeader *message,
3092 const struct GNUNET_ATS_Information *atsi,
3093 unsigned int atsi_count)
3095 struct GNUNET_MESH_ManipulatePath *msg;
3096 struct GNUNET_PeerIdentity *pi;
3097 struct MeshPeerPath *path;
3098 struct MeshTunnel *t;
3099 unsigned int own_pos;
3103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3104 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3105 size = ntohs (message->size);
3106 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3108 GNUNET_break_op (0);
3112 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3113 if (size % sizeof (struct GNUNET_PeerIdentity))
3115 GNUNET_break_op (0);
3118 size /= sizeof (struct GNUNET_PeerIdentity);
3121 GNUNET_break_op (0);
3124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3126 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3127 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3129 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3131 t = tunnel_get (pi, ntohl (msg->tid));
3134 /* TODO notify back: we don't know this tunnel */
3135 GNUNET_break_op (0);
3138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3139 path = path_new (size);
3141 for (i = 0; i < size; i++)
3143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3144 GNUNET_i2s (&pi[i]));
3145 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3146 if (path->peers[i] == myid)
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3150 if (own_pos < path->length - 1)
3151 send_message (message, &pi[own_pos + 1], t);
3153 send_client_tunnel_disconnect(t, NULL);
3155 tunnel_delete_peer (t, path->peers[path->length - 1]);
3156 path_destroy (path);
3162 * Core handler for notifications of broken paths
3164 * @param cls closure
3165 * @param message message
3166 * @param peer peer identity this notification is about
3167 * @param atsi performance data
3168 * @param atsi_count number of records in 'atsi'
3170 * @return GNUNET_OK to keep the connection open,
3171 * GNUNET_SYSERR to close it (signal serious error)
3174 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3175 const struct GNUNET_MessageHeader *message,
3176 const struct GNUNET_ATS_Information *atsi,
3177 unsigned int atsi_count)
3179 struct GNUNET_MESH_PathBroken *msg;
3180 struct MeshTunnel *t;
3182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3183 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3184 msg = (struct GNUNET_MESH_PathBroken *) message;
3185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3186 GNUNET_i2s (&msg->peer1));
3187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3188 GNUNET_i2s (&msg->peer2));
3189 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3192 GNUNET_break_op (0);
3195 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3196 GNUNET_PEER_search (&msg->peer2));
3203 * Core handler for tunnel destruction
3205 * @param cls closure
3206 * @param message message
3207 * @param peer peer identity this notification is about
3208 * @param atsi performance data
3209 * @param atsi_count number of records in 'atsi'
3211 * @return GNUNET_OK to keep the connection open,
3212 * GNUNET_SYSERR to close it (signal serious error)
3215 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3216 const struct GNUNET_MessageHeader *message,
3217 const struct GNUNET_ATS_Information *atsi,
3218 unsigned int atsi_count)
3220 struct GNUNET_MESH_TunnelDestroy *msg;
3221 struct MeshTunnel *t;
3223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3224 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3225 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3227 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3228 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3231 /* Probably already got the message from another path,
3232 * destroyed the tunnel and retransmitted to children.
3237 if (t->id.oid == myid)
3239 GNUNET_break_op (0);
3242 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3244 /* Tunnel was incoming, notify clients */
3245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3246 t->local_tid, t->local_tid_dest);
3247 send_clients_tunnel_destroy (t);
3249 tunnel_send_destroy (t);
3256 * Core handler for mesh network traffic going from the origin to a peer
3258 * @param cls closure
3259 * @param peer peer identity this notification is about
3260 * @param message message
3261 * @param atsi performance data
3262 * @param atsi_count number of records in 'atsi'
3263 * @return GNUNET_OK to keep the connection open,
3264 * GNUNET_SYSERR to close it (signal serious error)
3267 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3268 const struct GNUNET_MessageHeader *message,
3269 const struct GNUNET_ATS_Information *atsi,
3270 unsigned int atsi_count)
3272 struct GNUNET_MESH_Unicast *msg;
3273 struct MeshTunnel *t;
3277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3279 size = ntohs (message->size);
3281 sizeof (struct GNUNET_MESH_Unicast) +
3282 sizeof (struct GNUNET_MessageHeader))
3287 msg = (struct GNUNET_MESH_Unicast *) message;
3288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3289 ntohs (msg[1].header.type));
3290 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3293 /* TODO notify back: we don't know this tunnel */
3294 GNUNET_break_op (0);
3297 tunnel_reset_timeout (t);
3298 pid = GNUNET_PEER_search (&msg->destination);
3301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3302 " it's for us! sending to clients...\n");
3303 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3307 " not for us, retransmitting...\n");
3308 send_message (message, tree_get_first_hop (t->tree, pid), t);
3314 * Core handler for mesh network traffic going from the origin to all peers
3316 * @param cls closure
3317 * @param message message
3318 * @param peer peer identity this notification is about
3319 * @param atsi performance data
3320 * @param atsi_count number of records in 'atsi'
3321 * @return GNUNET_OK to keep the connection open,
3322 * GNUNET_SYSERR to close it (signal serious error)
3324 * TODO: Check who we got this from, to validate route.
3327 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3328 const struct GNUNET_MessageHeader *message,
3329 const struct GNUNET_ATS_Information *atsi,
3330 unsigned int atsi_count)
3332 struct GNUNET_MESH_Multicast *msg;
3333 struct MeshTunnel *t;
3336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
3338 size = ntohs (message->size);
3339 if (sizeof (struct GNUNET_MESH_Multicast) +
3340 sizeof (struct GNUNET_MessageHeader) > size)
3342 GNUNET_break_op (0);
3345 msg = (struct GNUNET_MESH_Multicast *) message;
3346 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3350 /* TODO notify that we dont know that tunnel */
3351 GNUNET_break_op (0);
3354 if (t->mid == ntohl (msg->mid))
3356 /* FIXME: already seen this packet, log dropping */
3357 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3358 " Already seen mid %u, DROPPING!\n", t->mid);
3363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3364 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3366 t->mid = ntohl (msg->mid);
3367 tunnel_reset_timeout (t);
3369 /* Transmit to locally interested clients */
3370 if (NULL != t->peers &&
3371 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3373 send_subscribed_clients (message, &msg[1].header);
3375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
3376 if (ntohl (msg->ttl) == 0)
3378 /* FIXME: ttl is 0, log dropping */
3379 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3382 tunnel_send_multicast (t, message, GNUNET_NO);
3388 * Core handler for mesh network traffic toward the owner of a tunnel
3390 * @param cls closure
3391 * @param message message
3392 * @param peer peer identity this notification is about
3393 * @param atsi performance data
3394 * @param atsi_count number of records in 'atsi'
3396 * @return GNUNET_OK to keep the connection open,
3397 * GNUNET_SYSERR to close it (signal serious error)
3400 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3401 const struct GNUNET_MessageHeader *message,
3402 const struct GNUNET_ATS_Information *atsi,
3403 unsigned int atsi_count)
3405 struct GNUNET_MESH_ToOrigin *msg;
3406 struct GNUNET_PeerIdentity id;
3407 struct MeshPeerInfo *peer_info;
3408 struct MeshTunnel *t;
3411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3413 size = ntohs (message->size);
3414 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3415 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3417 GNUNET_break_op (0);
3420 msg = (struct GNUNET_MESH_ToOrigin *) message;
3421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3422 ntohs (msg[1].header.type));
3423 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3427 /* TODO notify that we dont know this tunnel (whom)? */
3428 GNUNET_break_op (0);
3432 if (t->id.oid == myid)
3435 struct GNUNET_MESH_ToOrigin *copy;
3437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3438 " it's for us! sending to clients...\n");
3439 if (NULL == t->owner)
3441 /* got data packet for ownerless tunnel */
3442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
3443 GNUNET_break_op (0);
3446 /* TODO signature verification */
3447 memcpy (cbuf, message, size);
3448 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3449 copy->tid = htonl (t->local_tid);
3450 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3451 ©->header, GNUNET_YES);
3454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3455 " not for us, retransmitting...\n");
3457 peer_info = peer_info_get (&msg->oid);
3458 if (NULL == peer_info)
3460 /* unknown origin of tunnel */
3464 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3465 send_message (message, &id, t);
3472 * Core handler for path ACKs
3474 * @param cls closure
3475 * @param message message
3476 * @param peer peer identity this notification is about
3477 * @param atsi performance data
3478 * @param atsi_count number of records in 'atsi'
3480 * @return GNUNET_OK to keep the connection open,
3481 * GNUNET_SYSERR to close it (signal serious error)
3484 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3485 const struct GNUNET_MessageHeader *message,
3486 const struct GNUNET_ATS_Information *atsi,
3487 unsigned int atsi_count)
3489 struct GNUNET_MESH_PathACK *msg;
3490 struct GNUNET_PeerIdentity id;
3491 struct MeshPeerInfo *peer_info;
3492 struct MeshPeerPath *p;
3493 struct MeshTunnel *t;
3495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3496 GNUNET_i2s (&my_full_id));
3497 msg = (struct GNUNET_MESH_PathACK *) message;
3498 t = tunnel_get (&msg->oid, msg->tid);
3501 /* TODO notify that we don't know the tunnel */
3505 peer_info = peer_info_get (&msg->peer_id);
3507 /* Add paths to peers? */
3508 p = tree_get_path_to_peer (t->tree, peer_info->id);
3511 path_add_to_peers (p, GNUNET_YES);
3519 /* Message for us? */
3520 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3523 if (NULL == t->owner)
3525 GNUNET_break_op (0);
3528 if (NULL != t->dht_get_type)
3530 GNUNET_DHT_get_stop (t->dht_get_type);
3531 t->dht_get_type = NULL;
3533 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3535 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3536 send_client_peer_connected (t, peer_info->id);
3541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3542 " not for us, retransmitting...\n");
3543 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3544 peer_info = peer_info_get (&msg->oid);
3545 if (NULL == peer_info)
3547 /* If we know the tunnel, we should DEFINITELY know the peer */
3551 send_message (message, &id, t);
3557 * Functions to handle messages from core
3559 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3560 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3561 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3562 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3563 sizeof (struct GNUNET_MESH_PathBroken)},
3564 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3565 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3566 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3567 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3568 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3569 sizeof (struct GNUNET_MESH_PathACK)},
3575 /******************************************************************************/
3576 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3577 /******************************************************************************/
3580 * deregister_app: iterator for removing each application registered by a client
3582 * @param cls closure
3583 * @param key the hash of the application id (used to access the hashmap)
3584 * @param value the value stored at the key (client)
3586 * @return GNUNET_OK on success
3589 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
3591 GNUNET_break (GNUNET_YES ==
3592 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3599 * notify_client_connection_failure: notify a client that the connection to the
3600 * requested remote peer is not possible (for instance, no route found)
3601 * Function called when the socket is ready to queue more data. "buf" will be
3602 * NULL and "size" zero if the socket was closed for writing in the meantime.
3604 * @param cls closure
3605 * @param size number of bytes available in buf
3606 * @param buf where the callee should write the message
3607 * @return number of bytes written to buf
3610 notify_client_connection_failure (void *cls, size_t size, void *buf)
3613 struct MeshPeerInfo *peer_info;
3614 struct GNUNET_MESH_PeerControl *msg;
3615 struct GNUNET_PeerIdentity id;
3617 if (0 == size && NULL == buf)
3619 // TODO retry? cancel?
3623 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3624 peer_info = (struct MeshPeerInfo *) cls;
3625 msg = (struct GNUNET_MESH_PeerControl *) buf;
3626 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3627 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3628 // msg->tunnel_id = htonl(peer_info->t->tid);
3629 GNUNET_PEER_resolve (peer_info->id, &id);
3630 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3638 * Send keepalive packets for a peer
3640 * @param cls Closure (tunnel for which to send the keepalive).
3641 * @param tc Notification context.
3643 * TODO: implement explicit multicast keepalive?
3646 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3648 struct MeshTunnel *t = cls;
3649 struct GNUNET_MessageHeader *payload;
3650 struct GNUNET_MESH_Multicast *msg;
3652 sizeof (struct GNUNET_MESH_Multicast) +
3653 sizeof (struct GNUNET_MessageHeader);
3656 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3660 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3663 "sending keepalive for tunnel %d\n", t->id.tid);
3665 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3666 msg->header.size = htons (size);
3667 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3668 msg->oid = my_full_id;
3669 msg->tid = htonl (t->id.tid);
3670 msg->ttl = htonl (DEFAULT_TTL);
3671 msg->mid = htonl (t->mid + 1);
3673 payload = (struct GNUNET_MessageHeader *) &msg[1];
3674 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3675 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3676 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
3678 t->path_refresh_task =
3679 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3685 * Function to process paths received for a new peer addition. The recorded
3686 * paths form the initial tunnel, which can be optimized later.
3687 * Called on each result obtained for the DHT search.
3689 * @param cls closure
3690 * @param exp when will this value expire
3691 * @param key key of the result
3692 * @param get_path path of the get request
3693 * @param get_path_length lenght of get_path
3694 * @param put_path path of the put request
3695 * @param put_path_length length of the put_path
3696 * @param type type of the result
3697 * @param size number of bytes in data
3698 * @param data pointer to the result data
3700 * TODO: re-issue the request after certain time? cancel after X results?
3703 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3704 const struct GNUNET_HashCode * key,
3705 const struct GNUNET_PeerIdentity *get_path,
3706 unsigned int get_path_length,
3707 const struct GNUNET_PeerIdentity *put_path,
3708 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3709 size_t size, const void *data)
3711 struct MeshPathInfo *path_info = cls;
3712 struct MeshPeerPath *p;
3713 struct GNUNET_PeerIdentity pi;
3716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3717 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3720 p = path_build_from_dht (get_path, get_path_length, put_path,
3722 path_add_to_peers (p, GNUNET_NO);
3724 for (i = 0; i < path_info->peer->ntunnels; i++)
3726 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3727 peer_info_connect (path_info->peer, path_info->t);
3735 * Function to process paths received for a new peer addition. The recorded
3736 * paths form the initial tunnel, which can be optimized later.
3737 * Called on each result obtained for the DHT search.
3739 * @param cls closure
3740 * @param exp when will this value expire
3741 * @param key key of the result
3742 * @param get_path path of the get request
3743 * @param get_path_length lenght of get_path
3744 * @param put_path path of the put request
3745 * @param put_path_length length of the put_path
3746 * @param type type of the result
3747 * @param size number of bytes in data
3748 * @param data pointer to the result data
3751 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3752 const struct GNUNET_HashCode * key,
3753 const struct GNUNET_PeerIdentity *get_path,
3754 unsigned int get_path_length,
3755 const struct GNUNET_PeerIdentity *put_path,
3756 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3757 size_t size, const void *data)
3759 const struct GNUNET_PeerIdentity *pi = data;
3760 struct MeshTunnel *t = cls;
3761 struct MeshPeerInfo *peer_info;
3762 struct MeshPeerPath *p;
3764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
3765 if (size != sizeof (struct GNUNET_PeerIdentity))
3767 GNUNET_break_op (0);
3770 GNUNET_assert (NULL != t->owner);
3771 peer_info = peer_info_get (pi);
3772 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3774 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3776 p = path_build_from_dht (get_path, get_path_length, put_path,
3778 path_add_to_peers (p, GNUNET_NO);
3780 tunnel_add_peer (t, peer_info);
3781 peer_info_connect (peer_info, t);
3785 /******************************************************************************/
3786 /********************* MESH LOCAL HANDLES **************************/
3787 /******************************************************************************/
3791 * Handler for client disconnection
3793 * @param cls closure
3794 * @param client identification of the client; NULL
3795 * for the last call when the server is destroyed
3798 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3800 struct MeshClient *c;
3801 struct MeshClient *next;
3803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3812 if (c->handle != client)
3814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3820 GNUNET_SERVER_client_drop (c->handle);
3821 c->shutting_down = GNUNET_YES;
3822 GNUNET_assert (NULL != c->own_tunnels);
3823 GNUNET_assert (NULL != c->incoming_tunnels);
3824 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3825 &tunnel_destroy_iterator, c);
3826 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3827 &tunnel_destroy_iterator, c);
3828 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
3829 &tunnel_destroy_iterator, c);
3830 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3831 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3832 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
3834 /* deregister clients applications */
3835 if (NULL != c->apps)
3837 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3838 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3840 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3841 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3843 GNUNET_SCHEDULER_cancel (announce_applications_task);
3844 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3846 if (NULL != c->types)
3847 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3849 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
3860 * Handler for new clients
3862 * @param cls closure
3863 * @param client identification of the client
3864 * @param message the actual message, which includes messages the client wants
3867 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3868 const struct GNUNET_MessageHeader *message)
3870 struct GNUNET_MESH_ClientConnect *cc_msg;
3871 struct MeshClient *c;
3872 GNUNET_MESH_ApplicationType *a;
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3880 /* Check data sanity */
3881 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3882 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3883 ntypes = ntohs (cc_msg->types);
3884 napps = ntohs (cc_msg->applications);
3886 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3889 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3893 /* Create new client structure */
3894 c = GNUNET_malloc (sizeof (struct MeshClient));
3895 c->id = next_client_id++;
3896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
3898 GNUNET_SERVER_client_keep (client);
3899 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3902 GNUNET_MESH_ApplicationType at;
3903 struct GNUNET_HashCode hc;
3905 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3906 for (i = 0; i < napps; i++)
3909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
3910 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3911 /* store in clients hashmap */
3912 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3913 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3914 /* store in global hashmap, for announcements */
3915 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3916 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3918 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3919 announce_applications_task =
3920 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3926 struct GNUNET_HashCode hc;
3928 t = (uint16_t *) & a[napps];
3929 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3930 for (i = 0; i < ntypes; i++)
3933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
3934 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3936 /* store in clients hashmap */
3937 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3938 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3939 /* store in global hashmap */
3940 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3941 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3945 " client has %u+%u subscriptions\n", napps, ntypes);
3947 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3948 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3949 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3950 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3951 GNUNET_SERVER_notification_context_add (nc, client);
3953 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3959 * Handler for requests of new tunnels
3961 * @param cls closure
3962 * @param client identification of the client
3963 * @param message the actual message
3966 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3967 const struct GNUNET_MessageHeader *message)
3969 struct GNUNET_MESH_TunnelMessage *t_msg;
3970 struct MeshTunnel *t;
3971 struct MeshClient *c;
3973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3975 /* Sanity check for client registration */
3976 if (NULL == (c = client_get (client)))
3979 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3984 /* Message sanity check */
3985 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3988 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3992 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3993 /* Sanity check for tunnel numbering */
3994 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3997 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4000 /* Sanity check for duplicate tunnel IDs */
4001 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
4004 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4008 while (NULL != tunnel_get_by_pi (myid, next_tid))
4009 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4010 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
4011 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
4013 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4014 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
4016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
4017 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4023 * Handler for requests of deleting tunnels
4025 * @param cls closure
4026 * @param client identification of the client
4027 * @param message the actual message
4030 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4031 const struct GNUNET_MessageHeader *message)
4033 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4034 struct MeshClient *c;
4035 struct MeshTunnel *t;
4036 MESH_TunnelNumber tid;
4038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4039 "Got a DESTROY TUNNEL from client!\n");
4041 /* Sanity check for client registration */
4042 if (NULL == (c = client_get (client)))
4045 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4048 /* Message sanity check */
4049 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4052 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4056 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4058 /* Retrieve tunnel */
4059 tid = ntohl (tunnel_msg->tunnel_id);
4060 t = tunnel_get_by_local_id(c, tid);
4064 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4065 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4068 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4070 client_ignore_tunnel (c, t);
4072 // TODO: when to destroy incoming tunnel?
4073 if (t->nclients == 0)
4075 GNUNET_assert (GNUNET_YES ==
4076 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
4078 GNUNET_assert (GNUNET_YES ==
4079 GNUNET_CONTAINER_multihashmap_remove (t->peers,
4080 &my_full_id.hashPubKey,
4084 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4087 send_client_tunnel_disconnect(t, c);
4088 client_delete_tunnel(c, t);
4090 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4092 tunnel_send_destroy (t);
4094 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4100 * Handler for connection requests to new peers
4102 * @param cls closure
4103 * @param client identification of the client
4104 * @param message the actual message (PeerControl)
4107 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
4108 const struct GNUNET_MessageHeader *message)
4110 struct GNUNET_MESH_PeerControl *peer_msg;
4111 struct MeshPeerInfo *peer_info;
4112 struct MeshClient *c;
4113 struct MeshTunnel *t;
4114 MESH_TunnelNumber tid;
4116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
4117 /* Sanity check for client registration */
4118 if (NULL == (c = client_get (client)))
4121 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4125 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4126 /* Sanity check for message size */
4127 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4130 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4134 /* Tunnel exists? */
4135 tid = ntohl (peer_msg->tunnel_id);
4136 t = tunnel_get_by_local_id (c, tid);
4140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4144 /* Does client own tunnel? */
4145 if (t->owner->handle != client)
4148 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
4152 GNUNET_i2s (&peer_msg->peer));
4153 peer_info = peer_info_get (&peer_msg->peer);
4155 tunnel_add_peer (t, peer_info);
4156 peer_info_connect (peer_info, t);
4158 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4164 * Handler for disconnection requests of peers in a tunnel
4166 * @param cls closure
4167 * @param client identification of the client
4168 * @param message the actual message (PeerControl)
4171 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
4172 const struct GNUNET_MessageHeader *message)
4174 struct GNUNET_MESH_PeerControl *peer_msg;
4175 struct MeshPeerInfo *peer_info;
4176 struct MeshClient *c;
4177 struct MeshTunnel *t;
4178 MESH_TunnelNumber tid;
4180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
4181 /* Sanity check for client registration */
4182 if (NULL == (c = client_get (client)))
4185 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4188 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4189 /* Sanity check for message size */
4190 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4193 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4197 /* Tunnel exists? */
4198 tid = ntohl (peer_msg->tunnel_id);
4199 t = tunnel_get_by_local_id (c, tid);
4203 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4208 /* Does client own tunnel? */
4209 if (t->owner->handle != client)
4212 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
4217 GNUNET_i2s (&peer_msg->peer));
4218 /* Is the peer in the tunnel? */
4220 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
4221 if (NULL == peer_info)
4224 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4228 /* Ok, delete peer from tunnel */
4229 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
4230 &peer_msg->peer.hashPubKey);
4232 send_destroy_path (t, peer_info->id);
4233 tunnel_delete_peer (t, peer_info->id);
4234 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4239 * Handler for blacklist requests of peers in a tunnel
4241 * @param cls closure
4242 * @param client identification of the client
4243 * @param message the actual message (PeerControl)
4246 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
4247 const struct GNUNET_MessageHeader *message)
4249 struct GNUNET_MESH_PeerControl *peer_msg;
4250 struct MeshClient *c;
4251 struct MeshTunnel *t;
4252 MESH_TunnelNumber tid;
4254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
4255 /* Sanity check for client registration */
4256 if (NULL == (c = client_get (client)))
4259 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4262 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4264 /* Sanity check for message size */
4265 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4268 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4272 /* Tunnel exists? */
4273 tid = ntohl (peer_msg->tunnel_id);
4274 t = tunnel_get_by_local_id (c, tid);
4278 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4283 GNUNET_array_append(t->blacklisted, t->nblacklisted,
4284 GNUNET_PEER_intern(&peer_msg->peer));
4289 * Handler for unblacklist requests of peers in a tunnel
4291 * @param cls closure
4292 * @param client identification of the client
4293 * @param message the actual message (PeerControl)
4296 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
4297 const struct GNUNET_MessageHeader *message)
4299 struct GNUNET_MESH_PeerControl *peer_msg;
4300 struct MeshClient *c;
4301 struct MeshTunnel *t;
4302 MESH_TunnelNumber tid;
4306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
4307 /* Sanity check for client registration */
4308 if (NULL == (c = client_get (client)))
4311 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4314 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4316 /* Sanity check for message size */
4317 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4320 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4324 /* Tunnel exists? */
4325 tid = ntohl (peer_msg->tunnel_id);
4326 t = tunnel_get_by_local_id (c, tid);
4330 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4335 /* if peer is not known, complain */
4336 pid = GNUNET_PEER_search (&peer_msg->peer);
4343 /* search and remove from list */
4344 for (i = 0; i < t->nblacklisted; i++)
4346 if (t->blacklisted[i] == pid)
4348 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
4349 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
4354 /* if peer hasn't been blacklisted, complain */
4360 * Handler for connection requests to new peers by type
4362 * @param cls closure
4363 * @param client identification of the client
4364 * @param message the actual message (ConnectPeerByType)
4367 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
4368 const struct GNUNET_MessageHeader *message)
4370 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
4371 struct MeshClient *c;
4372 struct MeshTunnel *t;
4373 struct GNUNET_HashCode hash;
4374 MESH_TunnelNumber tid;
4376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
4377 /* Sanity check for client registration */
4378 if (NULL == (c = client_get (client)))
4381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4385 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
4386 /* Sanity check for message size */
4387 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
4388 ntohs (connect_msg->header.size))
4391 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4395 /* Tunnel exists? */
4396 tid = ntohl (connect_msg->tunnel_id);
4397 t = tunnel_get_by_local_id (c, tid);
4401 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4405 /* Does client own tunnel? */
4406 if (t->owner->handle != client)
4409 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4413 /* Do WE have the service? */
4414 t->type = ntohl (connect_msg->type);
4415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
4416 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
4417 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
4420 /* Yes! Fast forward, add ourselves to the tunnel and send the
4421 * good news to the client, and alert the destination client of
4422 * an incoming tunnel.
4424 * FIXME send a path create to self, avoid code duplication
4426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
4427 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
4428 peer_info_get (&my_full_id),
4429 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
4432 send_client_peer_connected (t, myid);
4433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
4434 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4436 t->local_tid_dest = next_local_tid++;
4437 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4438 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4439 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4443 /* Ok, lets find a peer offering the service */
4444 if (NULL != t->dht_get_type)
4446 GNUNET_DHT_get_stop (t->dht_get_type);
4448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
4449 GNUNET_h2s (&hash));
4451 GNUNET_DHT_get_start (dht_handle,
4452 GNUNET_BLOCK_TYPE_TEST, &hash, 10,
4453 GNUNET_DHT_RO_RECORD_ROUTE |
4454 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
4455 &dht_get_type_handler, t);
4457 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4463 * Handler for client traffic directed to one peer
4465 * @param cls closure
4466 * @param client identification of the client
4467 * @param message the actual message
4470 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4471 const struct GNUNET_MessageHeader *message)
4473 struct MeshClient *c;
4474 struct MeshTunnel *t;
4475 struct MeshPeerInfo *pi;
4476 struct GNUNET_MESH_Unicast *data_msg;
4477 MESH_TunnelNumber tid;
4480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4481 "Got a unicast request from a client!\n");
4483 /* Sanity check for client registration */
4484 if (NULL == (c = client_get (client)))
4487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4490 data_msg = (struct GNUNET_MESH_Unicast *) message;
4491 /* Sanity check for message size */
4492 size = ntohs (message->size);
4493 if (sizeof (struct GNUNET_MESH_Unicast) +
4494 sizeof (struct GNUNET_MessageHeader) > size)
4497 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4501 /* Tunnel exists? */
4502 tid = ntohl (data_msg->tid);
4503 t = tunnel_get_by_local_id (c, tid);
4507 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4511 /* Is it a local tunnel? Then, does client own the tunnel? */
4512 if (t->owner->handle != client)
4515 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4519 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
4520 &data_msg->destination.hashPubKey);
4521 /* Is the selected peer in the tunnel? */
4525 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4529 /* Ok, everything is correct, send the message
4530 * (pretend we got it from a mesh peer)
4533 char buf[ntohs (message->size)] GNUNET_ALIGN;
4534 struct GNUNET_MESH_Unicast *copy;
4536 /* Work around const limitation */
4537 copy = (struct GNUNET_MESH_Unicast *) buf;
4538 memcpy (buf, data_msg, size);
4539 copy->oid = my_full_id;
4540 copy->tid = htonl (t->id.tid);
4541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4542 " calling generic handler...\n");
4543 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4545 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4551 * Handler for client traffic directed to the origin
4553 * @param cls closure
4554 * @param client identification of the client
4555 * @param message the actual message
4558 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4559 const struct GNUNET_MessageHeader *message)
4561 struct GNUNET_MESH_ToOrigin *data_msg;
4562 struct GNUNET_PeerIdentity id;
4563 struct MeshClient *c;
4564 struct MeshTunnel *t;
4565 MESH_TunnelNumber tid;
4568 /* Sanity check for client registration */
4569 if (NULL == (c = client_get (client)))
4572 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4575 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4576 /* Sanity check for message size */
4577 size = ntohs (message->size);
4578 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4579 sizeof (struct GNUNET_MessageHeader) > size)
4582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4586 /* Tunnel exists? */
4587 tid = ntohl (data_msg->tid);
4588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4589 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
4590 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4593 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4596 t = tunnel_get_by_local_id (c, tid);
4600 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4604 /* It should be sent by someone who has this as incoming tunnel. */
4605 if (-1 == client_knows_tunnel (c, t))
4608 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4611 GNUNET_PEER_resolve (t->id.oid, &id);
4613 /* Ok, everything is correct, send the message
4614 * (pretend we got it from a mesh peer)
4617 char buf[ntohs (message->size)] GNUNET_ALIGN;
4618 struct GNUNET_MESH_ToOrigin *copy;
4620 /* Work around const limitation */
4621 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4622 memcpy (buf, data_msg, size);
4624 copy->tid = htonl (t->id.tid);
4625 copy->sender = my_full_id;
4626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4627 " calling generic handler...\n");
4628 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4630 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4636 * Handler for client traffic directed to all peers in a tunnel
4638 * @param cls closure
4639 * @param client identification of the client
4640 * @param message the actual message
4643 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4644 const struct GNUNET_MessageHeader *message)
4646 struct MeshClient *c;
4647 struct MeshTunnel *t;
4648 struct GNUNET_MESH_Multicast *data_msg;
4649 MESH_TunnelNumber tid;
4651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4652 "Got a multicast request from a client!\n");
4654 /* Sanity check for client registration */
4655 if (NULL == (c = client_get (client)))
4658 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4661 data_msg = (struct GNUNET_MESH_Multicast *) message;
4662 /* Sanity check for message size */
4663 if (sizeof (struct GNUNET_MESH_Multicast) +
4664 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4667 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4671 /* Tunnel exists? */
4672 tid = ntohl (data_msg->tid);
4673 t = tunnel_get_by_local_id (c, tid);
4677 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4681 /* Does client own tunnel? */
4682 if (t->owner->handle != client)
4685 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4690 char buf[ntohs (message->size)] GNUNET_ALIGN;
4691 struct GNUNET_MESH_Multicast *copy;
4693 copy = (struct GNUNET_MESH_Multicast *) buf;
4694 memcpy (buf, message, ntohs (message->size));
4695 copy->oid = my_full_id;
4696 copy->tid = htonl (t->id.tid);
4697 copy->ttl = htonl (DEFAULT_TTL);
4698 copy->mid = htonl (t->mid + 1);
4699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4700 " calling generic handler...\n");
4701 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4704 /* receive done gets called when last copy is sent to a neighbor */
4709 * Functions to handle messages from clients
4711 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4712 {&handle_local_new_client, NULL,
4713 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4714 {&handle_local_tunnel_create, NULL,
4715 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4716 sizeof (struct GNUNET_MESH_TunnelMessage)},
4717 {&handle_local_tunnel_destroy, NULL,
4718 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4719 sizeof (struct GNUNET_MESH_TunnelMessage)},
4720 {&handle_local_connect_add, NULL,
4721 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4722 sizeof (struct GNUNET_MESH_PeerControl)},
4723 {&handle_local_connect_del, NULL,
4724 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4725 sizeof (struct GNUNET_MESH_PeerControl)},
4726 {&handle_local_blacklist, NULL,
4727 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
4728 sizeof (struct GNUNET_MESH_PeerControl)},
4729 {&handle_local_unblacklist, NULL,
4730 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
4731 sizeof (struct GNUNET_MESH_PeerControl)},
4732 {&handle_local_connect_by_type, NULL,
4733 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4734 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4735 {&handle_local_unicast, NULL,
4736 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4737 {&handle_local_to_origin, NULL,
4738 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4739 {&handle_local_multicast, NULL,
4740 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4746 * To be called on core init/fail.
4748 * @param cls service closure
4749 * @param server handle to the server for this service
4750 * @param identity the public identity of this peer
4753 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4754 const struct GNUNET_PeerIdentity *identity)
4756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4757 core_handle = server;
4758 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4761 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4762 GNUNET_SCHEDULER_shutdown ();
4768 * Method called whenever a given peer connects.
4770 * @param cls closure
4771 * @param peer peer identity this notification is about
4772 * @param atsi performance data for the connection
4773 * @param atsi_count number of records in 'atsi'
4776 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4777 const struct GNUNET_ATS_Information *atsi,
4778 unsigned int atsi_count)
4780 struct MeshPeerInfo *peer_info;
4781 struct MeshPeerPath *path;
4783 DEBUG_CONN ("Peer connected\n");
4784 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4785 peer_info = peer_info_get (peer);
4786 if (myid == peer_info->id)
4788 DEBUG_CONN (" (self)\n");
4793 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4795 path = path_new (2);
4796 path->peers[0] = myid;
4797 path->peers[1] = peer_info->id;
4798 GNUNET_PEER_change_rc (myid, 1);
4799 GNUNET_PEER_change_rc (peer_info->id, 1);
4800 peer_info_add_path (peer_info, path, GNUNET_YES);
4805 * Method called whenever a peer disconnects.
4807 * @param cls closure
4808 * @param peer peer identity this notification is about
4811 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4813 struct MeshPeerInfo *pi;
4814 struct MeshPeerQueue *q;
4815 struct MeshPeerQueue *n;
4817 DEBUG_CONN ("Peer disconnected\n");
4818 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4830 /* try to reroute this traffic instead */
4831 queue_destroy(q, GNUNET_YES);
4835 peer_info_remove_path (pi, pi->id, myid);
4838 DEBUG_CONN (" (self)\n");
4844 /******************************************************************************/
4845 /************************ MAIN FUNCTIONS ****************************/
4846 /******************************************************************************/
4849 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4851 * @param cls closure
4852 * @param key current key code
4853 * @param value value in the hash map
4854 * @return GNUNET_YES if we should continue to iterate,
4858 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
4860 struct MeshTunnel *t = value;
4867 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4869 * @param cls closure
4870 * @param key current key code
4871 * @param value value in the hash map
4872 * @return GNUNET_YES if we should continue to iterate,
4876 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
4878 struct MeshPeerInfo *p = value;
4879 struct MeshPeerQueue *q;
4880 struct MeshPeerQueue *n;
4888 queue_destroy(q, GNUNET_YES);
4892 peer_info_destroy (p);
4897 * Task run during shutdown.
4903 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4907 if (core_handle != NULL)
4909 GNUNET_CORE_disconnect (core_handle);
4912 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4913 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4914 if (dht_handle != NULL)
4916 GNUNET_DHT_disconnect (dht_handle);
4921 GNUNET_SERVER_notification_context_destroy (nc);
4924 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4926 GNUNET_SCHEDULER_cancel (announce_id_task);
4927 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4933 * Process mesh requests.
4935 * @param cls closure
4936 * @param server the initialized server
4937 * @param c configuration to use
4940 run (void *cls, struct GNUNET_SERVER_Handle *server,
4941 const struct GNUNET_CONFIGURATION_Handle *c)
4943 struct MeshPeerInfo *peer;
4944 struct MeshPeerPath *p;
4947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4948 server_handle = server;
4949 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4950 NULL, /* Closure passed to MESH functions */
4951 &core_init, /* Call core_init once connected */
4952 &core_connect, /* Handle connects */
4953 &core_disconnect, /* remove peers on disconnects */
4954 NULL, /* Don't notify about all incoming messages */
4955 GNUNET_NO, /* For header only in notification */
4956 NULL, /* Don't notify about all outbound messages */
4957 GNUNET_NO, /* For header-only out notification */
4958 core_handlers); /* Register these handlers */
4960 if (core_handle == NULL)
4963 GNUNET_SCHEDULER_shutdown ();
4968 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4971 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4973 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4974 GNUNET_SCHEDULER_shutdown ();
4977 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4978 GNUNET_free (keyfile);
4979 if (my_private_key == NULL)
4981 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4982 _("Mesh service could not access hostkey. Exiting.\n"));
4983 GNUNET_SCHEDULER_shutdown ();
4986 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4987 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4988 &my_full_id.hashPubKey);
4989 myid = GNUNET_PEER_intern (&my_full_id);
4991 // transport_handle = GNUNET_TRANSPORT_connect(c,
4998 dht_handle = GNUNET_DHT_connect (c, 64);
4999 if (dht_handle == NULL)
5005 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5007 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5008 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5009 peers = GNUNET_CONTAINER_multihashmap_create (32);
5010 applications = GNUNET_CONTAINER_multihashmap_create (32);
5011 types = GNUNET_CONTAINER_multihashmap_create (32);
5013 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5014 nc = GNUNET_SERVER_notification_context_create (server_handle,
5016 GNUNET_SERVER_disconnect_notify (server_handle,
5017 &handle_local_client_disconnect, NULL);
5021 clients_tail = NULL;
5024 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5025 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5027 /* Create a peer_info for the local peer */
5028 peer = peer_info_get (&my_full_id);
5031 GNUNET_PEER_change_rc (myid, 1);
5032 peer_info_add_path (peer, p, GNUNET_YES);
5034 /* Scheduled the task to clean up when shutdown is called */
5035 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
5042 * The main function for the mesh service.
5044 * @param argc number of arguments from the command line
5045 * @param argv command line arguments
5046 * @return 0 ok, 1 on error
5049 main (int argc, char *const *argv)
5053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5056 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");