2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - relay corking down to core
44 * - set ttl relative to tree depth
50 #include "mesh_protocol.h"
51 #include "gnunet_dht_service.h"
52 #include "mesh_tunnel_tree.h"
54 /* TODO: move into configuration file */
55 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
56 GNUNET_TIME_UNIT_SECONDS,\
58 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
59 GNUNET_TIME_UNIT_SECONDS,\
62 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
63 GNUNET_TIME_UNIT_SECONDS,\
66 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
67 GNUNET_TIME_UNIT_SECONDS,\
69 #define DEFAULT_TTL 64
73 #define MESH_DEBUG_DHT GNUNET_YES
74 #define MESH_DEBUG_CONNECTION GNUNET_NO
76 #if MESH_DEBUG_CONNECTION
77 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
79 #define DEBUG_CONN(...)
83 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
85 #define DEBUG_DHT(...)
88 /******************************************************************************/
89 /************************ DATA STRUCTURES ****************************/
90 /******************************************************************************/
92 /** FWD declaration */
97 * Struct representing a piece of data being sent to other peers
101 /** Tunnel it belongs to. */
102 struct MeshTunnel *t;
104 /** In case of a multicast, task to allow a client to send more data if
105 * some neighbor is too slow. */
106 GNUNET_SCHEDULER_TaskIdentifier *task;
108 /** How many remaining neighbors we need to send this to. */
109 unsigned int *reference_counter;
111 /** Size of the data. */
120 * Struct containing info about a queued transmission to this peer
127 struct MeshPeerQueue *next;
132 struct MeshPeerQueue *prev;
135 * Peer this transmission is directed to.
137 struct MeshPeerInfo *peer;
140 * Pointer to info stucture used as cls.
150 * Size of the message
157 * Struct containing all info possibly needed to build a package when called
160 struct MeshTransmissionDescriptor
162 /** ID of the tunnel this packet travels in */
163 struct MESH_TunnelID *origin;
165 /** Who was this message being sent to */
166 struct MeshPeerInfo *peer;
168 /** Ultimate destination of the packet */
169 GNUNET_PEER_Id destination;
171 /** Data descriptor */
172 struct MeshData* mesh_data;
177 * Struct containing all information regarding a given peer
187 * Last time we heard from this peer
189 struct GNUNET_TIME_Absolute last_contact;
192 * Number of attempts to reconnect so far
194 int n_reconnect_attempts;
197 * Paths to reach the peer, ordered by ascending hop count
199 struct MeshPeerPath *path_head;
202 * Paths to reach the peer, ordered by ascending hop count
204 struct MeshPeerPath *path_tail;
207 * Handle to stop the DHT search for a path to this peer
209 struct GNUNET_DHT_GetHandle *dhtget;
212 * Closure given to the DHT GET
214 struct MeshPathInfo *dhtgetcls;
217 * Array of tunnels this peer participates in
218 * (most probably a small amount, therefore not a hashmap)
219 * When the path to the peer changes, notify these tunnels to let them
220 * re-adjust their path trees.
222 struct MeshTunnel **tunnels;
225 * Number of tunnels this peers participates in
227 unsigned int ntunnels;
230 * Transmission queue to core DLL head
232 struct MeshPeerQueue *queue_head;
235 * Transmission queue to core DLL tail
237 struct MeshPeerQueue *queue_tail;
240 * Handle to for queued transmissions
242 struct GNUNET_CORE_TransmitHandle *core_transmit;
247 * Globally unique tunnel identification (owner + number)
248 * DO NOT USE OVER THE NETWORK
253 * Node that owns the tunnel
258 * Tunnel number to differentiate all the tunnels owned by the node oid
259 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
261 MESH_TunnelNumber tid;
265 struct MeshClient; /* FWD declaration */
268 * Struct containing all information regarding a tunnel
269 * For an intermediate node the improtant info used will be:
270 * - id Tunnel unique identification
271 * - paths[0] To know where to send it next
272 * - metainfo: ready, speeds, accounting
279 struct MESH_TunnelID id;
282 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
284 MESH_TunnelNumber local_tid;
287 * Local tunnel number for local destination clients (incoming number)
288 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
291 MESH_TunnelNumber local_tid_dest;
294 * ID of the last multicast packet seen/sent.
299 * Last time the tunnel was used
301 struct GNUNET_TIME_Absolute timestamp;
304 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
305 * containing peers added by id or by type, not intermediate peers.
307 struct GNUNET_CONTAINER_MultiHashMap *peers;
310 * Number of peers that are connected and potentially ready to receive data
312 unsigned int peers_ready;
315 * Number of peers that have been added to the tunnel
317 unsigned int peers_total;
320 * Client owner of the tunnel, if any
322 struct MeshClient *owner;
325 * Clients that have been informed about the tunnel, if any
327 struct MeshClient **clients;
330 * Number of elements in clients
332 unsigned int nclients;
335 * Clients that have requested to leave the tunnel
337 struct MeshClient **ignore;
340 * Number of elements in clients
342 unsigned int nignore;
347 struct MeshTunnelTree *tree;
350 * Application type we are looking for in this tunnel
352 GNUNET_MESH_ApplicationType type;
355 * Used to search peers offering a service
357 struct GNUNET_DHT_GetHandle *dht_get_type;
360 * Task to keep the used paths alive
362 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
365 * Task to destroy the tunnel after timeout
367 * FIXME: merge the two? a tunnel will have either
368 * a path refresh OR a timeout, never both!
370 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
375 * Info needed to work with tunnel paths and peers
382 struct MeshTunnel *t;
385 * Neighbouring peer to whom we send the packet to
387 struct MeshPeerInfo *peer;
392 struct MeshPeerPath *path;
397 * Struct containing information about a client of the service
404 struct MeshClient *next;
409 struct MeshClient *prev;
412 * Tunnels that belong to this client, indexed by local id
414 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
417 * Tunnels this client has accepted, indexed by incoming local id
419 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
422 * Tunnels this client has rejected, indexed by incoming local id
424 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
426 * Handle to communicate with the client
428 struct GNUNET_SERVER_Client *handle;
431 * Applications that this client has claimed to provide
433 struct GNUNET_CONTAINER_MultiHashMap *apps;
436 * Messages that this client has declared interest in
438 struct GNUNET_CONTAINER_MultiHashMap *types;
441 * Whether the client is active or shutting down (don't send confirmations
442 * to a client that is shutting down.
447 * ID of the client, mainly for debug messages
455 /******************************************************************************/
456 /************************ DEBUG FUNCTIONS ****************************/
457 /******************************************************************************/
461 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
462 * @param cls string to print
463 * @param success GNUNET_OK if the PUT was transmitted,
464 * GNUNET_NO on timeout,
465 * GNUNET_SYSERR on disconnect from service
466 * after the PUT message was transmitted
467 * (so we don't know if it was received or not)
472 mesh_debug (void *cls, int success)
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
482 /******************************************************************************/
483 /*********************** GLOBAL VARIABLES ****************************/
484 /******************************************************************************/
489 static struct MeshClient *clients;
490 static struct MeshClient *clients_tail;
493 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
495 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
498 * Tunnels incoming, indexed by MESH_TunnelNumber
499 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
501 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
504 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
506 static struct GNUNET_CONTAINER_MultiHashMap *peers;
509 * Handle to communicate with transport
511 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
514 * Handle to communicate with core
516 static struct GNUNET_CORE_Handle *core_handle;
521 static struct GNUNET_DHT_Handle *dht_handle;
526 static struct GNUNET_SERVER_Handle *server_handle;
529 * Notification context, to send messages to local clients
531 static struct GNUNET_SERVER_NotificationContext *nc;
534 * Local peer own ID (memory efficient handle)
536 static GNUNET_PEER_Id myid;
539 * Local peer own ID (full value)
541 static struct GNUNET_PeerIdentity my_full_id;
546 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
551 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
554 * Tunnel ID for the next created tunnel (global tunnel number)
556 static MESH_TunnelNumber next_tid;
559 * Tunnel ID for the next incoming tunnel (local tunnel number)
561 static MESH_TunnelNumber next_local_tid;
564 * All application types provided by this peer
566 static struct GNUNET_CONTAINER_MultiHashMap *applications;
569 * All message types clients of this peer are interested in
571 static struct GNUNET_CONTAINER_MultiHashMap *types;
574 * Task to periodically announce provided applications
576 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
579 * Task to periodically announce itself in the network
581 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
584 * Next ID to assign to a client
586 unsigned int next_client_id;
590 /******************************************************************************/
591 /************************ ITERATORS ****************************/
592 /******************************************************************************/
594 /* FIXME move iterators here */
597 /******************************************************************************/
598 /************************ PERIODIC FUNCTIONS ****************************/
599 /******************************************************************************/
602 * Announce iterator over for each application provided by the peer
605 * @param key current key code
606 * @param value value in the hash map
607 * @return GNUNET_YES if we should continue to
612 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
614 /* FIXME are hashes in multihash map equal on all aquitectures? */
615 /* FIXME: keep return value of 'put' to possibly cancel!? */
616 GNUNET_DHT_put (dht_handle, key, 10,
617 GNUNET_DHT_RO_RECORD_ROUTE |
618 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
619 sizeof (struct GNUNET_PeerIdentity),
620 (const char *) &my_full_id,
621 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
623 APP_ANNOUNCE_TIME, NULL, NULL);
629 * Periodically announce what applications are provided by local clients
632 * @param tc task context
635 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
637 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
639 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
643 DEBUG_DHT ("Starting PUT for apps\n");
645 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
647 announce_applications_task =
648 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
650 DEBUG_DHT ("Finished PUT for apps\n");
657 * Periodically announce self id in the DHT
660 * @param tc task context
663 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
665 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
667 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
671 * - Set data expiration in function of X
674 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
676 GNUNET_DHT_put (dht_handle, /* DHT handle */
677 &my_full_id.hashPubKey, /* Key to use */
678 10, /* Replication level */
679 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
680 GNUNET_BLOCK_TYPE_TEST, /* Block type */
681 sizeof (my_full_id), /* Size of the data */
682 (char *) &my_full_id, /* Data itself */
683 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
684 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
685 NULL, /* Continuation */
686 NULL); /* Continuation closure */
688 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
693 * Function to process paths received for a new peer addition. The recorded
694 * paths form the initial tunnel, which can be optimized later.
695 * Called on each result obtained for the DHT search.
698 * @param exp when will this value expire
699 * @param key key of the result
700 * @param type type of the result
701 * @param size number of bytes in data
702 * @param data pointer to the result data
705 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
706 const GNUNET_HashCode * key,
707 const struct GNUNET_PeerIdentity *get_path,
708 unsigned int get_path_length,
709 const struct GNUNET_PeerIdentity *put_path,
710 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
711 size_t size, const void *data);
714 /******************************************************************************/
715 /****************** GENERAL HELPER FUNCTIONS ************************/
716 /******************************************************************************/
719 * Search for a tunnel by global ID using full PeerIdentities
721 * @param oid owner of the tunnel
722 * @param tid global tunnel number
724 * @return tunnel handler, NULL if doesn't exist
726 static struct MeshTunnel *
727 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
731 * Delete an active client from the tunnel.
737 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
740 * Notify a tunnel that a connection has broken that affects at least
743 * @param t Tunnel affected.
744 * @param p1 Peer that got disconnected from p2.
745 * @param p2 Peer that got disconnected from p1.
747 * @return Short ID of the peer disconnected (either p1 or p2).
748 * 0 if the tunnel remained unaffected.
750 static GNUNET_PEER_Id
751 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
756 * Check if client has registered with the service and has not disconnected
758 * @param client the client to check
760 * @return non-NULL if client exists in the global DLL
762 static struct MeshClient *
763 client_get (struct GNUNET_SERVER_Client *client)
765 struct MeshClient *c;
770 if (c->handle == client)
779 * Checks if a given client has subscribed to certain message type
781 * @param message_type Type of message to check
782 * @param c Client to check
784 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
789 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
793 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
794 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
799 * Allow a client to send more data after transmitting a multicast message
800 * which some neighbor has not yet accepted altough a reasonable time has
803 * @param cls Closure (DataDescriptor containing the task identifier)
804 * @param tc Task Context
806 * FIXME reference counter cshould be just int
809 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
811 struct MeshData *mdata = cls;
813 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
815 GNUNET_assert (NULL != mdata->reference_counter);
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
817 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
818 *(mdata->reference_counter));
819 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
820 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
825 * Check whether client wants traffic from a tunnel.
827 * @param c Client to check.
828 * @param t Tunnel to be found.
830 * @return GNUNET_YES if client knows tunnel.
832 * TODO look in client hashmap
835 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
839 for (i = 0; i < t->nclients; i++)
840 if (t->clients[i] == c)
847 * Check whether client has been informed about a tunnel.
849 * @param c Client to check.
850 * @param t Tunnel to be found.
852 * @return GNUNET_YES if client knows tunnel.
854 * TODO look in client hashmap
857 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
861 for (i = 0; i < t->nignore; i++)
862 if (t->ignore[i] == c)
864 return client_wants_tunnel(c, t);
869 * Marks a client as uninterested in traffic from the tunnel, updating both
870 * client and tunnel to reflect this.
872 * @param c Client that doesn't want traffic anymore.
873 * @param t Tunnel which should be ignored.
875 * FIXME when to delete an incoming tunnel?
878 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
880 GNUNET_HashCode hash;
882 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
883 GNUNET_break (GNUNET_YES ==
884 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
886 GNUNET_break (GNUNET_YES ==
887 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
888 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
889 tunnel_delete_active_client (t, c);
890 GNUNET_array_append (t->ignore, t->nignore, c);
895 * Deletes a tunnel from a client (either owner or destination). To be used on
896 * tunnel destroy, otherwise, use client_ignore_tunnel.
898 * @param c Client whose tunnel to delete.
899 * @param t Tunnel which should be deleted.
902 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
904 GNUNET_HashCode hash;
908 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
909 GNUNET_assert (GNUNET_YES ==
910 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
916 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
918 GNUNET_assert (GNUNET_YES ==
919 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
923 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
932 * Send the message to all clients that have subscribed to its type
934 * @param msg Pointer to the message itself
935 * @param payload Pointer to the payload of the message.
936 * @return number of clients this message was sent to
939 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
940 const struct GNUNET_MessageHeader *payload)
942 struct GNUNET_PeerIdentity *oid;
943 struct MeshClient *c;
944 struct MeshTunnel *t;
945 MESH_TunnelNumber *tid;
948 char cbuf[htons (msg->size)];
950 type = ntohs (payload->type);
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
954 memcpy (cbuf, msg, sizeof (cbuf));
955 switch (htons (msg->type))
957 struct GNUNET_MESH_Unicast *uc;
958 struct GNUNET_MESH_Multicast *mc;
959 struct GNUNET_MESH_ToOrigin *to;
961 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
962 uc = (struct GNUNET_MESH_Unicast *) cbuf;
966 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
967 mc = (struct GNUNET_MESH_Multicast *) cbuf;
971 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
972 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
980 t = tunnel_get (oid, ntohl (*tid));
987 for (count = 0, c = clients; c != NULL; c = c->next)
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
990 if (client_is_subscribed (type, c))
992 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
996 *tid = htonl (t->local_tid);
1000 if (GNUNET_NO == client_knows_tunnel (c, t))
1002 /* This client doesn't know the tunnel */
1003 struct GNUNET_MESH_TunnelNotification tmsg;
1004 GNUNET_HashCode hash;
1006 tmsg.header.size = htons (sizeof (tmsg));
1007 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1008 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1009 tmsg.tunnel_id = htonl (t->local_tid_dest);
1010 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1011 &tmsg.header, GNUNET_NO);
1012 GNUNET_array_append (t->clients, t->nclients, c);
1013 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1015 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1016 c->incoming_tunnels, &hash, t,
1017 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1019 *tid = htonl (t->local_tid_dest);
1022 /* Check if the client wants to get traffic from the tunnel */
1023 if (GNUNET_NO == client_wants_tunnel(c, t))
1026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1027 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1028 (struct GNUNET_MessageHeader
1029 *) cbuf, GNUNET_YES);
1037 * Notify the client that owns the tunnel that a peer has connected to it
1038 * (the requested path to it has been confirmed).
1040 * @param t Tunnel whose owner to notify
1041 * @param id Short id of the peer that has connected
1044 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1046 struct GNUNET_MESH_PeerControl pc;
1048 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1049 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1050 pc.tunnel_id = htonl (t->local_tid);
1051 GNUNET_PEER_resolve (id, &pc.peer);
1052 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1058 * Notify all clients (not depending on registration status) that the incoming
1059 * tunnel is no longer valid.
1061 * @param t Tunnel that was destroyed.
1064 send_clients_tunnel_destroy (struct MeshTunnel *t)
1066 struct GNUNET_MESH_TunnelMessage msg;
1068 msg.header.size = htons (sizeof (msg));
1069 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1070 msg.tunnel_id = htonl (t->local_tid_dest);
1071 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1076 * Notify clients of tunnel disconnections, if needed.
1077 * In case the origin disconnects, the destination clients get a tunnel destroy
1078 * notification. If the last destination disconnects (only one remaining client
1079 * in tunnel), the origin gets a (local ID) peer disconnected.
1080 * Note that the function must be called BEFORE removing the client from
1083 * @param t Tunnel that was destroyed.
1084 * @param c Client that disconnected.
1087 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1093 struct GNUNET_MESH_TunnelMessage msg;
1095 msg.header.size = htons (sizeof (msg));
1096 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1097 msg.tunnel_id = htonl (t->local_tid_dest);
1098 for (i = 0; i < t->nclients; i++)
1099 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1100 &msg.header, GNUNET_NO);
1102 // FIXME when to disconnect an incoming tunnel?
1103 else if (1 == t->nclients && NULL != t->owner)
1105 struct GNUNET_MESH_PeerControl msg;
1107 msg.header.size = htons (sizeof (msg));
1108 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1109 msg.tunnel_id = htonl (t->local_tid);
1110 msg.peer = my_full_id;
1111 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1112 &msg.header, GNUNET_NO);
1118 * Decrements the reference counter and frees all resources if needed
1120 * @param mesh_data Data Descriptor used in a multicast message.
1121 * Freed no longer needed (last message).
1124 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
1126 /* Make sure it's a multicast packet */
1127 GNUNET_assert (NULL != mesh_data->reference_counter);
1129 if (0 == --(*(mesh_data->reference_counter)))
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1132 if (NULL != mesh_data->task)
1134 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1136 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1138 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1140 GNUNET_free (mesh_data->task);
1142 GNUNET_free (mesh_data->reference_counter);
1143 GNUNET_free (mesh_data->data);
1144 GNUNET_free (mesh_data);
1150 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1151 * and insert it in the appropiate structures if the peer is not known yet.
1153 * @param peer Full identity of the peer.
1155 * @return Existing or newly created peer info.
1157 static struct MeshPeerInfo *
1158 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1160 struct MeshPeerInfo *peer_info;
1162 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1163 if (NULL == peer_info)
1166 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1167 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1168 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1169 peer_info->id = GNUNET_PEER_intern (peer);
1177 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1178 * and insert it in the appropiate structures if the peer is not known yet.
1180 * @param peer Short identity of the peer.
1182 * @return Existing or newly created peer info.
1184 static struct MeshPeerInfo *
1185 peer_info_get_short (const GNUNET_PEER_Id peer)
1187 struct GNUNET_PeerIdentity id;
1189 GNUNET_PEER_resolve (peer, &id);
1190 return peer_info_get (&id);
1195 * Iterator to remove the tunnel from the list of tunnels a peer participates
1198 * @param cls Closure (tunnel info)
1199 * @param key GNUNET_PeerIdentity of the peer (unused)
1200 * @param value PeerInfo of the peer
1202 * @return always GNUNET_YES, to keep iterating
1205 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1207 struct MeshTunnel *t = cls;
1208 struct MeshPeerInfo *peer = value;
1211 for (i = 0; i < peer->ntunnels; i++)
1214 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1217 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1218 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1227 * Queue and pass message to core when possible.
1229 * @param cls Closure (type dependant).
1230 * @param type Type of the message.
1231 * @param size Size of the message.
1232 * @param dst Neighbor to send message to.
1235 queue_add (void *cls, uint16_t type, size_t size, struct MeshPeerInfo *dst);
1238 * Core callback to write a pre-constructed data packet to core buffer
1240 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1241 * @param size Number of bytes available in buf.
1242 * @param buf Where the to write the message.
1244 * @return number of bytes written to buf
1247 send_core_data_raw (void *cls, size_t size, void *buf)
1249 struct MeshTransmissionDescriptor *info = cls;
1250 struct GNUNET_MessageHeader *msg;
1253 GNUNET_assert (NULL != info);
1254 GNUNET_assert (NULL != info->mesh_data);
1255 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1256 total_size = ntohs (msg->size);
1258 if (total_size > size)
1263 memcpy (buf, msg, total_size);
1264 GNUNET_free (info->mesh_data);
1271 * Sends an already built unicast message to a peer, properly registrating
1272 * all used resources.
1274 * @param message Message to send. Function makes a copy of it.
1275 * @param peer Short ID of the neighbor whom to send the message.
1280 send_message (const struct GNUNET_MessageHeader *message,
1281 const struct GNUNET_PeerIdentity *peer)
1283 struct MeshTransmissionDescriptor *info;
1284 struct MeshPeerInfo *neighbor;
1285 struct MeshPeerPath *p;
1288 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1290 size = ntohs (message->size);
1291 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1292 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1293 info->mesh_data->data = GNUNET_malloc (size);
1294 memcpy (info->mesh_data->data, message, size);
1295 info->mesh_data->data_len = size;
1296 neighbor = peer_info_get (peer);
1297 for (p = neighbor->path_head; NULL != p; p = p->next)
1307 GNUNET_free (info->mesh_data->data);
1308 GNUNET_free (info->mesh_data);
1312 info->peer = neighbor;
1314 GNUNET_MESSAGE_TYPE_MESH_UNICAST,
1321 * Sends a CREATE PATH message for a path to a peer, properly registrating
1322 * all used resources.
1324 * @param peer PeerInfo of the final peer for whom this path is being created.
1325 * @param p Path itself.
1326 * @param t Tunnel for which the path is created.
1329 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1330 struct MeshTunnel *t)
1332 struct GNUNET_PeerIdentity id;
1333 struct MeshPathInfo *path_info;
1334 struct MeshPeerInfo *neighbor;
1340 p = tree_get_path_to_peer (t->tree, peer->id);
1347 for (i = 0; i < p->length; i++)
1349 if (p->peers[i] == myid)
1352 if (i >= p->length - 1)
1358 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1360 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1361 path_info->path = p;
1363 neighbor = peer_info_get (&id);
1364 path_info->peer = neighbor;
1365 queue_add (path_info,
1366 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1367 sizeof (struct GNUNET_MESH_ManipulatePath) +
1368 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1374 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1376 * @param t Tunnel whose path to destroy.
1377 * @param destination Short ID of the peer to whom the path to destroy.
1380 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1382 struct MeshPeerPath *p;
1385 p = tree_get_path_to_peer (t->tree, destination);
1391 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1392 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1394 struct GNUNET_MESH_ManipulatePath *msg;
1395 struct GNUNET_PeerIdentity *pi;
1399 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1400 msg->header.size = htons (size);
1401 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1402 msg->tid = htonl (t->id.tid);
1403 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1404 for (i = 0; i < p->length; i++)
1406 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1408 send_message (&msg->header, tree_get_first_hop (t->tree, destination));
1415 * Try to establish a new connection to this peer.
1416 * Use the best path for the given tunnel.
1417 * If the peer doesn't have any path to it yet, try to get one.
1418 * If the peer already has some path, send a CREATE PATH towards it.
1420 * @param peer PeerInfo of the peer.
1421 * @param t Tunnel for which to create the path, if possible.
1424 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1426 struct MeshPeerPath *p;
1427 struct MeshPathInfo *path_info;
1429 if (NULL != peer->path_head)
1431 p = tree_get_path_to_peer (t->tree, peer->id);
1438 // FIXME always send create path to self
1441 send_create_path (peer, p, t);
1445 GNUNET_HashCode hash;
1448 send_client_peer_connected (t, myid);
1449 t->local_tid_dest = next_local_tid++;
1450 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1453 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1454 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1461 else if (NULL == peer->dhtget)
1463 struct GNUNET_PeerIdentity id;
1465 GNUNET_PEER_resolve (peer->id, &id);
1466 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1467 path_info->peer = peer;
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1470 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1471 peer->dhtgetcls = path_info;
1472 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1473 GNUNET_BLOCK_TYPE_TEST, /* type */
1474 &id.hashPubKey, /* key to search */
1475 10, /* replication level */
1476 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1477 0, /* xquery bits */
1478 &dht_get_id_handler, path_info);
1480 /* Otherwise, there is no path but the DHT get is already started. */
1485 * Task to delay the connection of a peer
1487 * @param cls Closure (path info with tunnel and peer to connect).
1488 * Will be free'd on exection.
1489 * @param tc TaskContext
1492 peer_info_connect_task (void *cls,
1493 const struct GNUNET_SCHEDULER_TaskContext *tc)
1495 struct MeshPathInfo *path_info = cls;
1497 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1502 peer_info_connect (path_info->peer, path_info->t);
1508 * Destroy the peer_info and free any allocated resources linked to it
1510 * @param pi The peer_info to destroy.
1512 * @return GNUNET_OK on success
1515 peer_info_destroy (struct MeshPeerInfo *pi)
1517 struct GNUNET_PeerIdentity id;
1518 struct MeshPeerPath *p;
1519 struct MeshPeerPath *nextp;
1521 GNUNET_PEER_resolve (pi->id, &id);
1522 GNUNET_PEER_change_rc (pi->id, -1);
1525 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1528 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1529 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1531 if (NULL != pi->dhtget)
1533 GNUNET_DHT_get_stop (pi->dhtget);
1534 GNUNET_free (pi->dhtgetcls);
1540 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1550 * Remove all paths that rely on a direct connection between p1 and p2
1551 * from the peer itself and notify all tunnels about it.
1553 * @param peer PeerInfo of affected peer.
1554 * @param p1 GNUNET_PEER_Id of one peer.
1555 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1558 * TODO: optimize (see below)
1561 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1564 struct MeshPeerPath *p;
1565 struct MeshPeerPath *aux;
1566 struct MeshPeerInfo *peer_d;
1568 unsigned int destroyed;
1574 p = peer->path_head;
1578 for (i = 0; i < (p->length - 1); i++)
1580 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1581 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1583 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1594 for (i = 0; i < peer->ntunnels; i++)
1596 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1600 * Problem: one or more peers have been deleted from the tunnel tree.
1601 * We don't know who they are to try to add them again.
1602 * We need to try to find a new path for each of the disconnected peers.
1603 * Some of them might already have a path to reach them that does not
1604 * involve p1 and p2. Adding all anew might render in a better tree than
1605 * the trivial immediate fix.
1607 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1608 * its children will be reachable trough him.
1610 peer_d = peer_info_get_short (d);
1613 for (p = peer_d->path_head; NULL != p; p = p->next)
1615 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1623 /* No callback, as peer will be already disconnected and a connection
1624 * scheduled by tunnel_notify_connection_broken.
1626 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1630 peer_info_connect (peer_d, peer->tunnels[i]);
1637 * Add the path to the peer and update the path used to reach it in case this
1640 * @param peer_info Destination peer to add the path to.
1641 * @param path New path to add. Last peer must be the peer in arg 1.
1642 * Path will be either used of freed if already known.
1643 * @param trusted Do we trust that this path is real?
1646 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1649 struct MeshPeerPath *aux;
1653 if ((NULL == peer_info) || (NULL == path))
1656 path_destroy (path);
1659 if (path->peers[path->length - 1] != peer_info->id)
1662 path_destroy (path);
1665 if (path->length <= 2 && GNUNET_NO == trusted)
1667 /* Only allow CORE to tell us about direct paths */
1668 path_destroy (path);
1671 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1672 for (l = 1; l < path->length; l++)
1674 if (path->peers[l] == myid)
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1677 for (l2 = 0; l2 < path->length - l; l2++)
1679 path->peers[l2] = path->peers[l + l2];
1684 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1689 struct GNUNET_PeerIdentity id;
1691 GNUNET_PEER_resolve (peer_info->id, &id);
1692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1693 path->length, GNUNET_i2s (&id));
1696 l = path_get_length (path);
1703 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1704 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1706 l2 = path_get_length (aux);
1709 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1710 peer_info->path_tail, aux, path);
1715 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1717 path_destroy (path);
1722 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1729 * Add the path to the origin peer and update the path used to reach it in case
1730 * this is the shortest.
1731 * The path is given in peer_info -> destination, therefore we turn the path
1732 * upside down first.
1734 * @param peer_info Peer to add the path to, being the origin of the path.
1735 * @param path New path to add after being inversed.
1736 * @param trusted Do we trust that this path is real?
1739 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1740 struct MeshPeerPath *path, int trusted)
1743 peer_info_add_path (peer_info, path, trusted);
1748 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1749 * to obtain a local peer -> destination path and interning the peer ids.
1751 * @return Newly allocated and created path
1753 static struct MeshPeerPath *
1754 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1755 unsigned int get_path_length,
1756 const struct GNUNET_PeerIdentity *put_path,
1757 unsigned int put_path_length)
1759 struct MeshPeerPath *p;
1765 GNUNET_PEER_change_rc (myid, 1);
1766 i = get_path_length;
1767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1768 for (i--; i >= 0; i--)
1770 id = GNUNET_PEER_intern (&get_path[i]);
1771 if (p->length > 0 && id == p->peers[p->length - 1])
1773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1774 GNUNET_PEER_change_rc (id, -1);
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1779 GNUNET_i2s (&get_path[i]));
1781 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1782 p->peers[p->length - 1] = id;
1785 i = put_path_length;
1786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1787 for (i--; i >= 0; i--)
1789 id = GNUNET_PEER_intern (&put_path[i]);
1792 /* PUT path went through us, so discard the path up until now and start
1793 * from here to get a much shorter (and loop-free) path.
1798 if (p->length > 0 && id == p->peers[p->length - 1])
1800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1801 GNUNET_PEER_change_rc (id, -1);
1805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1806 GNUNET_i2s (&put_path[i]));
1808 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1809 p->peers[p->length - 1] = id;
1813 if (get_path_length > 0)
1814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1815 GNUNET_i2s (&get_path[0]));
1816 if (put_path_length > 0)
1817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1818 GNUNET_i2s (&put_path[0]));
1819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1821 for (i = 0; i < p->length; i++)
1823 struct GNUNET_PeerIdentity peer_id;
1825 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1827 GNUNET_i2s (&peer_id));
1835 * Adds a path to the peer_infos of all the peers in the path
1837 * @param p Path to process.
1838 * @param confirmed Whether we know if the path works or not. FIXME use
1841 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1845 /* TODO: invert and add */
1846 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1847 for (i++; i < p->length; i++)
1849 struct MeshPeerInfo *aux;
1850 struct MeshPeerPath *copy;
1852 aux = peer_info_get_short (p->peers[i]);
1853 copy = path_duplicate (p);
1854 copy->length = i + 1;
1855 peer_info_add_path (aux, copy, GNUNET_NO);
1861 * Send keepalive packets for a peer
1863 * @param cls Closure (tunnel for which to send the keepalive).
1864 * @param tc Notification context.
1866 * TODO: implement explicit multicast keepalive?
1869 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1873 * Search for a tunnel among the incoming tunnels
1875 * @param tid the local id of the tunnel
1877 * @return tunnel handler, NULL if doesn't exist
1879 static struct MeshTunnel *
1880 tunnel_get_incoming (MESH_TunnelNumber tid)
1882 GNUNET_HashCode hash;
1884 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1885 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1886 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1891 * Search for a tunnel among the tunnels for a client
1893 * @param c the client whose tunnels to search in
1894 * @param tid the local id of the tunnel
1896 * @return tunnel handler, NULL if doesn't exist
1898 static struct MeshTunnel *
1899 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1901 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1903 return tunnel_get_incoming (tid);
1907 GNUNET_HashCode hash;
1909 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1910 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1916 * Search for a tunnel by global ID using PEER_ID
1918 * @param pi owner of the tunnel
1919 * @param tid global tunnel number
1921 * @return tunnel handler, NULL if doesn't exist
1923 static struct MeshTunnel *
1924 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1926 struct MESH_TunnelID id;
1927 GNUNET_HashCode hash;
1932 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1933 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1938 * Search for a tunnel by global ID using full PeerIdentities
1940 * @param oid owner of the tunnel
1941 * @param tid global tunnel number
1943 * @return tunnel handler, NULL if doesn't exist
1945 static struct MeshTunnel *
1946 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1948 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1953 * Delete an active client from the tunnel.
1959 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
1963 for (i = 0; i < t->nclients; i++)
1965 if (t->clients[i] == c)
1967 t->clients[i] = t->clients[t->nclients - 1];
1968 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
1976 * Delete an ignored client from the tunnel.
1982 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
1986 for (i = 0; i < t->nignore; i++)
1988 if (t->ignore[i] == c)
1990 t->ignore[i] = t->ignore[t->nignore - 1];
1991 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
1999 * Delete a client from the tunnel. It should be only done on
2000 * client disconnection, otherwise use client_ignore_tunnel.
2006 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2008 tunnel_delete_ignored_client (t, c);
2009 tunnel_delete_active_client (t, c);
2014 * Callback used to notify a client owner of a tunnel that a peer has
2015 * disconnected, most likely because of a path change.
2017 * @param cls Closure (tunnel this notification is about).
2018 * @param peer_id Short ID of disconnected peer.
2021 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2023 struct MeshTunnel *t = cls;
2024 struct MeshPeerInfo *peer;
2025 struct MeshPathInfo *path_info;
2027 if (NULL != t->owner && NULL != nc)
2029 struct GNUNET_MESH_PeerControl msg;
2031 msg.header.size = htons (sizeof (msg));
2032 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2033 msg.tunnel_id = htonl (t->local_tid);
2034 GNUNET_PEER_resolve (peer_id, &msg.peer);
2035 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2036 &msg.header, GNUNET_NO);
2038 peer = peer_info_get_short (peer_id);
2039 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2040 path_info->peer = peer;
2042 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2047 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2048 * needed rescources.
2049 * If peer already exists, reevaluate shortest path and change if different.
2051 * @param t Tunnel we want to add a new peer to
2052 * @param peer PeerInfo of the peer being added
2056 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2058 struct GNUNET_PeerIdentity id;
2059 struct MeshPeerPath *best_p;
2060 struct MeshPeerPath *p;
2061 unsigned int best_cost;
2064 GNUNET_PEER_resolve (peer->id, &id);
2066 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2069 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2070 GNUNET_assert (GNUNET_OK ==
2071 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2073 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2076 if (NULL != (p = peer->path_head))
2079 best_cost = tree_get_path_cost (t->tree, p);
2082 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2089 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2090 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2091 t->path_refresh_task =
2092 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2096 /* Start a DHT get */
2097 peer_info_connect (peer, t);
2102 * Add a path to a tunnel which we don't own, just to remember the next hop.
2103 * If destination node was already in the tunnel, the first hop information
2104 * will be replaced with the new path.
2106 * @param t Tunnel we want to add a new peer to
2107 * @param p Path to add
2108 * @param own_pos Position of local node in path.
2112 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2113 unsigned int own_pos)
2115 struct GNUNET_PeerIdentity id;
2117 GNUNET_assert (0 != own_pos);
2118 tree_add_path (t->tree, p, NULL, NULL);
2119 if (own_pos < p->length - 1)
2121 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2122 tree_update_first_hops (t->tree, myid, &id);
2128 * Notifies a tunnel that a connection has broken that affects at least
2129 * some of its peers. Sends a notification towards the root of the tree.
2130 * In case the peer is the owner of the tree, notifies the client that owns
2131 * the tunnel and tries to reconnect.
2133 * @param t Tunnel affected.
2134 * @param p1 Peer that got disconnected from p2.
2135 * @param p2 Peer that got disconnected from p1.
2137 * @return Short ID of the peer disconnected (either p1 or p2).
2138 * 0 if the tunnel remained unaffected.
2140 static GNUNET_PEER_Id
2141 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2147 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2149 if (myid != p1 && myid != p2)
2155 if (tree_get_predecessor (t->tree) != 0)
2157 /* We are the peer still connected, notify owner of the disconnection. */
2158 struct GNUNET_MESH_PathBroken msg;
2159 struct GNUNET_PeerIdentity neighbor;
2161 msg.header.size = htons (sizeof (msg));
2162 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2163 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2164 msg.tid = htonl (t->id.tid);
2165 msg.peer1 = my_full_id;
2166 GNUNET_PEER_resolve (pid, &msg.peer2);
2167 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2168 send_message (&msg.header, &neighbor);
2176 * Send a multicast packet to a neighbor.
2178 * @param cls Closure (Info about the multicast packet)
2179 * @param neighbor_id Short ID of the neighbor to send the packet to.
2182 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2184 struct MeshData *mdata = cls;
2185 struct MeshTransmissionDescriptor *info;
2186 struct GNUNET_PeerIdentity neighbor;
2188 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2190 info->mesh_data = mdata;
2191 (*(mdata->reference_counter)) ++;
2192 info->destination = neighbor_id;
2193 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2195 GNUNET_i2s (&neighbor));
2196 info->peer = peer_info_get (&neighbor);
2197 GNUNET_assert (NULL != info->peer);
2199 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
2200 info->mesh_data->data_len,
2206 * Send a message in a tunnel in multicast, sending a copy to each child node
2207 * down the local one in the tunnel tree.
2209 * @param t Tunnel in which to send the data.
2210 * @param msg Message to be sent.
2211 * @param internal Has the service generated this message?
2214 tunnel_send_multicast (struct MeshTunnel *t,
2215 const struct GNUNET_MessageHeader *msg,
2218 struct MeshData *mdata;
2220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2221 " sending a multicast packet...\n");
2222 mdata = GNUNET_malloc (sizeof (struct MeshData));
2223 mdata->data_len = ntohs (msg->size);
2224 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2226 mdata->data = GNUNET_malloc (mdata->data_len);
2227 memcpy (mdata->data, msg, mdata->data_len);
2228 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2230 struct GNUNET_MESH_Multicast *mcast;
2232 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2233 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2235 ntohl (mcast->ttl));
2239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2241 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2242 && GNUNET_NO == internal)
2244 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2246 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2252 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2253 if (*(mdata->reference_counter) == 0)
2255 GNUNET_free (mdata->data);
2256 GNUNET_free (mdata->reference_counter);
2257 if (NULL != mdata->task)
2259 GNUNET_SCHEDULER_cancel(*(mdata->task));
2260 GNUNET_free (mdata->task);
2261 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2263 // FIXME change order?
2264 GNUNET_free (mdata);
2266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2267 " sending a multicast packet done\n");
2273 * Send a message to all peers in this tunnel that the tunnel is no longer
2276 * @param t The tunnel whose peers to notify.
2279 tunnel_send_destroy (struct MeshTunnel *t)
2281 struct GNUNET_MESH_TunnelDestroy msg;
2283 msg.header.size = htons (sizeof (msg));
2284 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2285 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2286 msg.tid = htonl (t->id.tid);
2287 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2293 * Destroy the tunnel and free any allocated resources linked to it.
2295 * @param t the tunnel to destroy
2297 * @return GNUNET_OK on success
2300 tunnel_destroy (struct MeshTunnel *t)
2302 struct MeshClient *c;
2303 GNUNET_HashCode hash;
2314 struct GNUNET_PeerIdentity id;
2316 GNUNET_PEER_resolve (t->id.oid, &id);
2317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2318 GNUNET_i2s (&id), t->id.tid);
2320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2324 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2325 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2330 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2333 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2337 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2338 for (i = 0; i < t->nclients; i++)
2342 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2347 for (i = 0; i < t->nignore; i++)
2351 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2356 if (t->nclients > 0)
2359 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2363 GNUNET_free (t->clients);
2365 if (NULL != t->peers)
2367 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2369 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2372 tree_destroy (t->tree);
2373 if (NULL != t->dht_get_type)
2374 GNUNET_DHT_get_stop (t->dht_get_type);
2375 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2376 GNUNET_SCHEDULER_cancel (t->timeout_task);
2377 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2378 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2385 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2386 * that are no longer needed, as well as nodes of no longer reachable peers.
2387 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2389 * @param t Tunnel from which to remove the path.
2390 * @param peer Short id of the peer which should be removed.
2393 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2395 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2401 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2402 * client when the client disconnects. If the client is not the owner, the
2403 * owner will get notified if no more clients are in the tunnel and the client
2404 * get removed from the tunnel's list.
2406 * @param cls closure (client that is disconnecting)
2407 * @param key the hash of the local tunnel id (used to access the hashmap)
2408 * @param value the value stored at the key (tunnel to destroy)
2410 * @return GNUNET_OK on success
2413 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2415 struct MeshTunnel *t = value;
2416 struct MeshClient *c = cls;
2419 send_client_tunnel_disconnect(t, c);
2422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2423 "Client %u is destination, keeping the tunnel alive.\n", c->id);
2424 tunnel_delete_client(t, c);
2425 client_delete_tunnel(c, t);
2428 tunnel_send_destroy(t);
2429 r = tunnel_destroy (t);
2435 * Timeout function, destroys tunnel if called
2437 * @param cls Closure (tunnel to destroy).
2438 * @param tc TaskContext
2441 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2443 struct MeshTunnel *t = cls;
2445 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2447 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2452 * Resets the tunnel timeout. Starts it if no timeout was running.
2454 * @param t Tunnel whose timeout to reset.
2457 tunnel_reset_timeout (struct MeshTunnel *t)
2459 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2460 GNUNET_SCHEDULER_cancel (t->timeout_task);
2462 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2463 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2467 /******************************************************************************/
2468 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2469 /******************************************************************************/
2472 * Function to send a create path packet to a peer.
2474 * @param cls closure
2475 * @param size number of bytes available in buf
2476 * @param buf where the callee should write the message
2477 * @return number of bytes written to buf
2480 send_core_path_create (void *cls, size_t size, void *buf)
2482 struct MeshPathInfo *info = cls;
2483 struct GNUNET_MESH_ManipulatePath *msg;
2484 struct GNUNET_PeerIdentity *peer_ptr;
2485 struct MeshTunnel *t = info->t;
2486 struct MeshPeerPath *p = info->path;
2490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2492 sizeof (struct GNUNET_MESH_ManipulatePath) +
2493 p->length * sizeof (struct GNUNET_PeerIdentity);
2495 if (size < size_needed || NULL == buf)
2500 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2501 msg->header.size = htons (size_needed);
2502 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2503 msg->tid = ntohl (t->id.tid);
2505 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2506 for (i = 0; i < p->length; i++)
2508 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2515 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2521 * Fill the core buffer
2523 * @param cls closure (data itself)
2524 * @param size number of bytes available in buf
2525 * @param buf where the callee should write the message
2527 * @return number of bytes written to buf
2530 send_core_data_multicast (void *cls, size_t size, void *buf)
2532 struct MeshTransmissionDescriptor *info = cls;
2535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
2536 GNUNET_assert (NULL != info);
2537 GNUNET_assert (NULL != info->peer);
2538 total_size = info->mesh_data->data_len;
2539 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2541 if (total_size > size)
2546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
2547 memcpy (buf, info->mesh_data->data, total_size);
2550 struct GNUNET_MESH_Multicast *mc;
2551 struct GNUNET_MessageHeader *mh;
2554 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2556 mc = (struct GNUNET_MESH_Multicast *) mh;
2557 mh = (struct GNUNET_MessageHeader *) &mc[1];
2558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2559 " multicast, payload type %u\n", ntohs (mh->type));
2560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2561 " multicast, payload size %u\n", ntohs (mh->size));
2565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
2570 data_descriptor_decrement_multicast (info->mesh_data);
2571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
2573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
2579 * Creates a path ack message in buf and frees all unused resources.
2581 * @param cls closure (MeshTransmissionDescriptor)
2582 * @param size number of bytes available in buf
2583 * @param buf where the callee should write the message
2584 * @return number of bytes written to buf
2587 send_core_path_ack (void *cls, size_t size, void *buf)
2589 struct MeshTransmissionDescriptor *info = cls;
2590 struct GNUNET_MESH_PathACK *msg = buf;
2592 GNUNET_assert (NULL != info);
2593 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2598 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2599 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2600 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2601 msg->tid = htonl (info->origin->tid);
2602 msg->peer_id = my_full_id;
2605 /* TODO add signature */
2607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2608 return sizeof (struct GNUNET_MESH_PathACK);
2613 * Free a transmission that was already queued with all resources
2614 * associated to the request.
2616 * @param queue Queue handler to cancel.
2617 * @param clear_cls Is it necessary to free associated cls?
2620 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2622 struct MeshTransmissionDescriptor *dd;
2623 struct MeshPathInfo *path_info;
2625 if (GNUNET_YES == clear_cls)
2627 switch (queue->type)
2629 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2630 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
2633 data_descriptor_decrement_multicast (dd->mesh_data);
2635 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2637 path_info = queue->cls;
2638 path_destroy (path_info->path);
2642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
2644 GNUNET_free_non_null (queue->cls);
2646 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2647 queue->peer->queue_tail,
2649 GNUNET_free (queue);
2654 * Core callback to write a queued packet to core buffer
2656 * @param cls Closure (peer info).
2657 * @param size Number of bytes available in buf.
2658 * @param buf Where the to write the message.
2660 * @return number of bytes written to buf
2663 queue_send (void *cls, size_t size, void *buf)
2665 struct MeshPeerInfo *peer = cls;
2666 struct MeshPeerQueue *queue;
2669 peer->core_transmit = NULL;
2670 queue = peer->queue_head;
2672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
2675 /* If queue is empty, send should have been cancelled */
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
2683 /* Check if buffer size is enough for the message */
2684 if (queue->size > size)
2686 struct GNUNET_PeerIdentity id;
2688 GNUNET_PEER_resolve (peer->id, &id);
2689 peer->core_transmit =
2690 GNUNET_CORE_notify_transmit_ready(core_handle,
2693 GNUNET_TIME_UNIT_FOREVER_REL,
2700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
2703 switch (queue->type)
2705 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast\n");
2707 data_size = send_core_data_raw (queue->cls, size, buf);
2709 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
2711 data_size = send_core_data_multicast(queue->cls, size, buf);
2713 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
2715 data_size = send_core_path_create(queue->cls, size, buf);
2717 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
2719 data_size = send_core_path_ack(queue->cls, size, buf);
2723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
2727 /* Free queue, but cls was freed by send_core_* */
2728 queue_destroy(queue, GNUNET_NO);
2730 /* If more data in queue, send next */
2731 if (NULL != peer->queue_head)
2733 struct GNUNET_PeerIdentity id;
2735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
2736 GNUNET_PEER_resolve (peer->id, &id);
2737 peer->core_transmit =
2738 GNUNET_CORE_notify_transmit_ready(core_handle,
2741 GNUNET_TIME_UNIT_FOREVER_REL,
2743 peer->queue_head->size,
2747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
2753 * Queue and pass message to core when possible.
2755 * @param cls Closure (type dependant).
2756 * @param type Type of the message.
2757 * @param size Size of the message.
2758 * @param dst Neighbor to send message to.
2761 queue_add (void *cls, uint16_t type, size_t size, struct MeshPeerInfo *dst)
2763 struct MeshPeerQueue *queue;
2765 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2770 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
2771 if (NULL == dst->core_transmit)
2773 struct GNUNET_PeerIdentity id;
2775 GNUNET_PEER_resolve (dst->id, &id);
2776 dst->core_transmit =
2777 GNUNET_CORE_notify_transmit_ready(core_handle,
2780 GNUNET_TIME_UNIT_FOREVER_REL,
2789 /******************************************************************************/
2790 /******************** MESH NETWORK HANDLERS **************************/
2791 /******************************************************************************/
2795 * Core handler for path creation
2797 * @param cls closure
2798 * @param message message
2799 * @param peer peer identity this notification is about
2800 * @param atsi performance data
2801 * @param atsi_count number of records in 'atsi'
2803 * @return GNUNET_OK to keep the connection open,
2804 * GNUNET_SYSERR to close it (signal serious error)
2807 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2808 const struct GNUNET_MessageHeader *message,
2809 const struct GNUNET_ATS_Information *atsi,
2810 unsigned int atsi_count)
2812 unsigned int own_pos;
2815 MESH_TunnelNumber tid;
2816 struct GNUNET_MESH_ManipulatePath *msg;
2817 struct GNUNET_PeerIdentity *pi;
2818 GNUNET_HashCode hash;
2819 struct MeshPeerPath *path;
2820 struct MeshPeerInfo *dest_peer_info;
2821 struct MeshPeerInfo *orig_peer_info;
2822 struct MeshTunnel *t;
2824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2825 "Received a path create msg [%s]\n",
2826 GNUNET_i2s (&my_full_id));
2827 size = ntohs (message->size);
2828 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2830 GNUNET_break_op (0);
2834 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2835 if (size % sizeof (struct GNUNET_PeerIdentity))
2837 GNUNET_break_op (0);
2840 size /= sizeof (struct GNUNET_PeerIdentity);
2843 GNUNET_break_op (0);
2846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2847 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2849 tid = ntohl (msg->tid);
2850 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2852 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2853 t = tunnel_get (pi, tid);
2856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2857 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2858 t->id.oid = GNUNET_PEER_intern (pi);
2860 while (NULL != tunnel_get_incoming (next_local_tid))
2861 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2862 t->local_tid_dest = next_local_tid++;
2863 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2864 t->tree = tree_new (t->id.oid);
2866 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2868 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2869 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2875 tunnel_reset_timeout (t);
2876 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2878 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2879 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2887 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2888 if (NULL == dest_peer_info)
2890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2891 " Creating PeerInfo for destination.\n");
2892 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2893 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2894 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2896 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2898 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2899 if (NULL == orig_peer_info)
2901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2902 " Creating PeerInfo for origin.\n");
2903 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2904 orig_peer_info->id = GNUNET_PEER_intern (pi);
2905 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2906 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
2909 path = path_new (size);
2911 for (i = 0; i < size; i++)
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
2914 GNUNET_i2s (&pi[i]));
2915 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2916 if (path->peers[i] == myid)
2919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
2922 /* cannot be self, must be 'not found' */
2923 /* create path: self not found in path through self */
2924 GNUNET_break_op (0);
2925 path_destroy (path);
2926 /* FIXME error. destroy tunnel? leave for timeout? */
2929 path_add_to_peers (path, GNUNET_NO);
2930 tunnel_add_path (t, path, own_pos);
2931 if (own_pos == size - 1)
2933 /* It is for us! Send ack. */
2934 struct MeshTransmissionDescriptor *info;
2936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2937 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2938 if (NULL == t->peers)
2940 /* New tunnel! Notify clients on data. */
2941 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2943 GNUNET_break (GNUNET_OK ==
2944 GNUNET_CONTAINER_multihashmap_put (t->peers,
2945 &my_full_id.hashPubKey,
2948 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
2949 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2950 info->origin = &t->id;
2951 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2952 GNUNET_assert (NULL != info->peer);
2954 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2955 sizeof (struct GNUNET_MESH_PathACK),
2960 struct MeshPeerPath *path2;
2962 /* It's for somebody else! Retransmit. */
2963 path2 = path_duplicate (path);
2964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
2965 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
2966 path2 = path_duplicate (path);
2967 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
2968 send_create_path (dest_peer_info, path, t);
2975 * Core handler for path destruction
2977 * @param cls closure
2978 * @param message message
2979 * @param peer peer identity this notification is about
2980 * @param atsi performance data
2981 * @param atsi_count number of records in 'atsi'
2983 * @return GNUNET_OK to keep the connection open,
2984 * GNUNET_SYSERR to close it (signal serious error)
2987 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2988 const struct GNUNET_MessageHeader *message,
2989 const struct GNUNET_ATS_Information *atsi,
2990 unsigned int atsi_count)
2992 struct GNUNET_MESH_ManipulatePath *msg;
2993 struct GNUNET_PeerIdentity *pi;
2994 struct MeshPeerPath *path;
2995 struct MeshTunnel *t;
2996 unsigned int own_pos;
3000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3001 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3002 size = ntohs (message->size);
3003 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3005 GNUNET_break_op (0);
3009 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3010 if (size % sizeof (struct GNUNET_PeerIdentity))
3012 GNUNET_break_op (0);
3015 size /= sizeof (struct GNUNET_PeerIdentity);
3018 GNUNET_break_op (0);
3021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3023 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3024 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3026 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3028 t = tunnel_get (pi, ntohl (msg->tid));
3031 /* TODO notify back: we don't know this tunnel */
3032 GNUNET_break_op (0);
3035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3036 path = path_new (size);
3038 for (i = 0; i < size; i++)
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3041 GNUNET_i2s (&pi[i]));
3042 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3043 if (path->peers[i] == myid)
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3047 if (own_pos < path->length - 1)
3048 send_message (message, &pi[own_pos + 1]);
3050 send_client_tunnel_disconnect(t, NULL);
3052 tunnel_delete_peer (t, path->peers[path->length - 1]);
3053 path_destroy (path);
3059 * Core handler for notifications of broken paths
3061 * @param cls closure
3062 * @param message message
3063 * @param peer peer identity this notification is about
3064 * @param atsi performance data
3065 * @param atsi_count number of records in 'atsi'
3067 * @return GNUNET_OK to keep the connection open,
3068 * GNUNET_SYSERR to close it (signal serious error)
3071 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3072 const struct GNUNET_MessageHeader *message,
3073 const struct GNUNET_ATS_Information *atsi,
3074 unsigned int atsi_count)
3076 struct GNUNET_MESH_PathBroken *msg;
3077 struct MeshTunnel *t;
3079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3080 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3081 msg = (struct GNUNET_MESH_PathBroken *) message;
3082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3083 GNUNET_i2s (&msg->peer1));
3084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3085 GNUNET_i2s (&msg->peer2));
3086 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3089 GNUNET_break_op (0);
3092 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3093 GNUNET_PEER_search (&msg->peer2));
3100 * Core handler for tunnel destruction
3102 * @param cls closure
3103 * @param message message
3104 * @param peer peer identity this notification is about
3105 * @param atsi performance data
3106 * @param atsi_count number of records in 'atsi'
3108 * @return GNUNET_OK to keep the connection open,
3109 * GNUNET_SYSERR to close it (signal serious error)
3112 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3113 const struct GNUNET_MessageHeader *message,
3114 const struct GNUNET_ATS_Information *atsi,
3115 unsigned int atsi_count)
3117 struct GNUNET_MESH_TunnelDestroy *msg;
3118 struct MeshTunnel *t;
3120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3121 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3122 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3124 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3125 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3128 /* Probably already got the message from another path,
3129 * destroyed the tunnel and retransmitted to children.
3134 if (t->id.oid == myid)
3136 GNUNET_break_op (0);
3139 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3141 /* Tunnel was incoming, notify clients */
3142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3143 t->local_tid, t->local_tid_dest);
3144 send_clients_tunnel_destroy (t);
3146 tunnel_send_destroy (t);
3153 * Core handler for mesh network traffic going from the origin to a peer
3155 * @param cls closure
3156 * @param peer peer identity this notification is about
3157 * @param message message
3158 * @param atsi performance data
3159 * @param atsi_count number of records in 'atsi'
3160 * @return GNUNET_OK to keep the connection open,
3161 * GNUNET_SYSERR to close it (signal serious error)
3164 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3165 const struct GNUNET_MessageHeader *message,
3166 const struct GNUNET_ATS_Information *atsi,
3167 unsigned int atsi_count)
3169 struct GNUNET_MESH_Unicast *msg;
3170 struct MeshTunnel *t;
3174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3176 size = ntohs (message->size);
3178 sizeof (struct GNUNET_MESH_Unicast) +
3179 sizeof (struct GNUNET_MessageHeader))
3184 msg = (struct GNUNET_MESH_Unicast *) message;
3185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3186 ntohs (msg[1].header.type));
3187 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3190 /* TODO notify back: we don't know this tunnel */
3191 GNUNET_break_op (0);
3194 tunnel_reset_timeout (t);
3195 pid = GNUNET_PEER_search (&msg->destination);
3198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3199 " it's for us! sending to clients...\n");
3200 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3204 " not for us, retransmitting...\n");
3205 send_message (message, tree_get_first_hop (t->tree, pid));
3211 * Core handler for mesh network traffic going from the origin to all peers
3213 * @param cls closure
3214 * @param message message
3215 * @param peer peer identity this notification is about
3216 * @param atsi performance data
3217 * @param atsi_count number of records in 'atsi'
3218 * @return GNUNET_OK to keep the connection open,
3219 * GNUNET_SYSERR to close it (signal serious error)
3221 * TODO: Check who we got this from, to validate route.
3224 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3225 const struct GNUNET_MessageHeader *message,
3226 const struct GNUNET_ATS_Information *atsi,
3227 unsigned int atsi_count)
3229 struct GNUNET_MESH_Multicast *msg;
3230 struct MeshTunnel *t;
3233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
3235 size = ntohs (message->size);
3236 if (sizeof (struct GNUNET_MESH_Multicast) +
3237 sizeof (struct GNUNET_MessageHeader) > size)
3239 GNUNET_break_op (0);
3242 msg = (struct GNUNET_MESH_Multicast *) message;
3243 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3247 /* TODO notify that we dont know that tunnel */
3248 GNUNET_break_op (0);
3251 if (t->mid == ntohl (msg->mid))
3253 /* FIXME: already seen this packet, log dropping */
3254 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3255 " Already seen mid %u, DROPPING!\n", t->mid);
3260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3261 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3263 t->mid = ntohl (msg->mid);
3264 tunnel_reset_timeout (t);
3266 /* Transmit to locally interested clients */
3267 if (NULL != t->peers &&
3268 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3270 send_subscribed_clients (message, &msg[1].header);
3272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
3273 if (ntohl (msg->ttl) == 0)
3275 /* FIXME: ttl is 0, log dropping */
3276 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3279 tunnel_send_multicast (t, message, GNUNET_NO);
3285 * Core handler for mesh network traffic toward the owner of a tunnel
3287 * @param cls closure
3288 * @param message message
3289 * @param peer peer identity this notification is about
3290 * @param atsi performance data
3291 * @param atsi_count number of records in 'atsi'
3293 * @return GNUNET_OK to keep the connection open,
3294 * GNUNET_SYSERR to close it (signal serious error)
3297 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3298 const struct GNUNET_MessageHeader *message,
3299 const struct GNUNET_ATS_Information *atsi,
3300 unsigned int atsi_count)
3302 struct GNUNET_MESH_ToOrigin *msg;
3303 struct GNUNET_PeerIdentity id;
3304 struct MeshPeerInfo *peer_info;
3305 struct MeshTunnel *t;
3308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3310 size = ntohs (message->size);
3311 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3312 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3314 GNUNET_break_op (0);
3317 msg = (struct GNUNET_MESH_ToOrigin *) message;
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3319 ntohs (msg[1].header.type));
3320 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3324 /* TODO notify that we dont know this tunnel (whom)? */
3325 GNUNET_break_op (0);
3329 if (t->id.oid == myid)
3332 struct GNUNET_MESH_ToOrigin *copy;
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3335 " it's for us! sending to clients...\n");
3336 if (NULL == t->owner)
3338 /* got data packet for ownerless tunnel */
3339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
3340 GNUNET_break_op (0);
3343 /* TODO signature verification */
3344 memcpy (cbuf, message, size);
3345 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3346 copy->tid = htonl (t->local_tid);
3347 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3348 ©->header, GNUNET_YES);
3351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3352 " not for us, retransmitting...\n");
3354 peer_info = peer_info_get (&msg->oid);
3355 if (NULL == peer_info)
3357 /* unknown origin of tunnel */
3361 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3362 send_message (message, &id);
3369 * Core handler for path ACKs
3371 * @param cls closure
3372 * @param message message
3373 * @param peer peer identity this notification is about
3374 * @param atsi performance data
3375 * @param atsi_count number of records in 'atsi'
3377 * @return GNUNET_OK to keep the connection open,
3378 * GNUNET_SYSERR to close it (signal serious error)
3381 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3382 const struct GNUNET_MessageHeader *message,
3383 const struct GNUNET_ATS_Information *atsi,
3384 unsigned int atsi_count)
3386 struct GNUNET_MESH_PathACK *msg;
3387 struct GNUNET_PeerIdentity id;
3388 struct MeshPeerInfo *peer_info;
3389 struct MeshPeerPath *p;
3390 struct MeshTunnel *t;
3392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3393 GNUNET_i2s (&my_full_id));
3394 msg = (struct GNUNET_MESH_PathACK *) message;
3395 t = tunnel_get (&msg->oid, msg->tid);
3398 /* TODO notify that we don't know the tunnel */
3402 peer_info = peer_info_get (&msg->peer_id);
3404 /* Add paths to peers? */
3405 p = tree_get_path_to_peer (t->tree, peer_info->id);
3408 path_add_to_peers (p, GNUNET_YES);
3416 /* Message for us? */
3417 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3420 if (NULL == t->owner)
3422 GNUNET_break_op (0);
3425 if (NULL != t->dht_get_type)
3427 GNUNET_DHT_get_stop (t->dht_get_type);
3428 t->dht_get_type = NULL;
3430 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3432 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3433 send_client_peer_connected (t, peer_info->id);
3438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3439 " not for us, retransmitting...\n");
3440 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3441 peer_info = peer_info_get (&msg->oid);
3442 if (NULL == peer_info)
3444 /* If we know the tunnel, we should DEFINITELY know the peer */
3448 send_message (message, &id);
3454 * Functions to handle messages from core
3456 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3457 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3458 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3459 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3460 sizeof (struct GNUNET_MESH_PathBroken)},
3461 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3462 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3463 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3464 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3465 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3466 sizeof (struct GNUNET_MESH_PathACK)},
3472 /******************************************************************************/
3473 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3474 /******************************************************************************/
3477 * deregister_app: iterator for removing each application registered by a client
3479 * @param cls closure
3480 * @param key the hash of the application id (used to access the hashmap)
3481 * @param value the value stored at the key (client)
3483 * @return GNUNET_OK on success
3486 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3488 GNUNET_break (GNUNET_YES ==
3489 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3496 * notify_client_connection_failure: notify a client that the connection to the
3497 * requested remote peer is not possible (for instance, no route found)
3498 * Function called when the socket is ready to queue more data. "buf" will be
3499 * NULL and "size" zero if the socket was closed for writing in the meantime.
3501 * @param cls closure
3502 * @param size number of bytes available in buf
3503 * @param buf where the callee should write the message
3504 * @return number of bytes written to buf
3507 notify_client_connection_failure (void *cls, size_t size, void *buf)
3510 struct MeshPeerInfo *peer_info;
3511 struct GNUNET_MESH_PeerControl *msg;
3512 struct GNUNET_PeerIdentity id;
3514 if (0 == size && NULL == buf)
3516 // TODO retry? cancel?
3520 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3521 peer_info = (struct MeshPeerInfo *) cls;
3522 msg = (struct GNUNET_MESH_PeerControl *) buf;
3523 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3524 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3525 // msg->tunnel_id = htonl(peer_info->t->tid);
3526 GNUNET_PEER_resolve (peer_info->id, &id);
3527 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3535 * Send keepalive packets for a peer
3537 * @param cls Closure (tunnel for which to send the keepalive).
3538 * @param tc Notification context.
3540 * TODO: implement explicit multicast keepalive?
3543 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3545 struct MeshTunnel *t = cls;
3546 struct GNUNET_MessageHeader *payload;
3547 struct GNUNET_MESH_Multicast *msg;
3549 sizeof (struct GNUNET_MESH_Multicast) +
3550 sizeof (struct GNUNET_MessageHeader);
3553 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3557 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3560 "sending keepalive for tunnel %d\n", t->id.tid);
3562 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3563 msg->header.size = htons (size);
3564 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3565 msg->oid = my_full_id;
3566 msg->tid = htonl (t->id.tid);
3567 msg->ttl = htonl (DEFAULT_TTL);
3568 msg->mid = htonl (t->mid + 1);
3570 payload = (struct GNUNET_MessageHeader *) &msg[1];
3571 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3572 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3573 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
3575 t->path_refresh_task =
3576 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3582 * Function to process paths received for a new peer addition. The recorded
3583 * paths form the initial tunnel, which can be optimized later.
3584 * Called on each result obtained for the DHT search.
3586 * @param cls closure
3587 * @param exp when will this value expire
3588 * @param key key of the result
3589 * @param get_path path of the get request
3590 * @param get_path_length lenght of get_path
3591 * @param put_path path of the put request
3592 * @param put_path_length length of the put_path
3593 * @param type type of the result
3594 * @param size number of bytes in data
3595 * @param data pointer to the result data
3597 * TODO: re-issue the request after certain time? cancel after X results?
3600 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3601 const GNUNET_HashCode * key,
3602 const struct GNUNET_PeerIdentity *get_path,
3603 unsigned int get_path_length,
3604 const struct GNUNET_PeerIdentity *put_path,
3605 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3606 size_t size, const void *data)
3608 struct MeshPathInfo *path_info = cls;
3609 struct MeshPeerPath *p;
3610 struct GNUNET_PeerIdentity pi;
3613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3614 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3617 p = path_build_from_dht (get_path, get_path_length, put_path,
3619 path_add_to_peers (p, GNUNET_NO);
3621 for (i = 0; i < path_info->peer->ntunnels; i++)
3623 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3624 peer_info_connect (path_info->peer, path_info->t);
3632 * Function to process paths received for a new peer addition. The recorded
3633 * paths form the initial tunnel, which can be optimized later.
3634 * Called on each result obtained for the DHT search.
3636 * @param cls closure
3637 * @param exp when will this value expire
3638 * @param key key of the result
3639 * @param get_path path of the get request
3640 * @param get_path_length lenght of get_path
3641 * @param put_path path of the put request
3642 * @param put_path_length length of the put_path
3643 * @param type type of the result
3644 * @param size number of bytes in data
3645 * @param data pointer to the result data
3648 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3649 const GNUNET_HashCode * key,
3650 const struct GNUNET_PeerIdentity *get_path,
3651 unsigned int get_path_length,
3652 const struct GNUNET_PeerIdentity *put_path,
3653 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3654 size_t size, const void *data)
3656 const struct GNUNET_PeerIdentity *pi = data;
3657 struct MeshTunnel *t = cls;
3658 struct MeshPeerInfo *peer_info;
3659 struct MeshPeerPath *p;
3661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
3662 if (size != sizeof (struct GNUNET_PeerIdentity))
3664 GNUNET_break_op (0);
3667 GNUNET_assert (NULL != t->owner);
3668 peer_info = peer_info_get (pi);
3669 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3671 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3673 p = path_build_from_dht (get_path, get_path_length, put_path,
3675 path_add_to_peers (p, GNUNET_NO);
3677 tunnel_add_peer (t, peer_info);
3678 peer_info_connect (peer_info, t);
3682 /******************************************************************************/
3683 /********************* MESH LOCAL HANDLES **************************/
3684 /******************************************************************************/
3688 * Handler for client disconnection
3690 * @param cls closure
3691 * @param client identification of the client; NULL
3692 * for the last call when the server is destroyed
3695 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3697 struct MeshClient *c;
3698 struct MeshClient *next;
3700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3709 if (c->handle != client)
3711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3717 GNUNET_SERVER_client_drop (c->handle);
3718 c->shutting_down = GNUNET_YES;
3719 GNUNET_assert (NULL != c->own_tunnels);
3720 GNUNET_assert (NULL != c->incoming_tunnels);
3721 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3722 &tunnel_destroy_iterator, c);
3723 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3724 &tunnel_destroy_iterator, c);
3725 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
3726 &tunnel_destroy_iterator, c);
3727 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3728 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3729 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
3731 /* deregister clients applications */
3732 if (NULL != c->apps)
3734 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3735 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3737 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3738 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3740 GNUNET_SCHEDULER_cancel (announce_applications_task);
3741 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3743 if (NULL != c->types)
3744 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3746 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
3757 * Handler for new clients
3759 * @param cls closure
3760 * @param client identification of the client
3761 * @param message the actual message, which includes messages the client wants
3764 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3765 const struct GNUNET_MessageHeader *message)
3767 struct GNUNET_MESH_ClientConnect *cc_msg;
3768 struct MeshClient *c;
3769 GNUNET_MESH_ApplicationType *a;
3776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3777 /* Check data sanity */
3778 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3779 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3780 ntypes = ntohs (cc_msg->types);
3781 napps = ntohs (cc_msg->applications);
3783 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3786 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3790 /* Create new client structure */
3791 c = GNUNET_malloc (sizeof (struct MeshClient));
3792 c->id = next_client_id++;
3793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
3795 GNUNET_SERVER_client_keep (client);
3796 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3799 GNUNET_MESH_ApplicationType at;
3802 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3803 for (i = 0; i < napps; i++)
3806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
3807 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3808 /* store in clients hashmap */
3809 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3810 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3811 /* store in global hashmap, for announcements */
3812 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3813 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3815 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3816 announce_applications_task =
3817 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3825 t = (uint16_t *) & a[napps];
3826 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3827 for (i = 0; i < ntypes; i++)
3830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
3831 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3833 /* store in clients hashmap */
3834 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3835 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3836 /* store in global hashmap */
3837 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3838 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3842 " client has %u+%u subscriptions\n", napps, ntypes);
3844 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3845 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3846 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3847 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3848 GNUNET_SERVER_notification_context_add (nc, client);
3850 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3856 * Handler for requests of new tunnels
3858 * @param cls closure
3859 * @param client identification of the client
3860 * @param message the actual message
3863 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3864 const struct GNUNET_MessageHeader *message)
3866 struct GNUNET_MESH_TunnelMessage *t_msg;
3867 struct MeshTunnel *t;
3868 struct MeshClient *c;
3869 GNUNET_HashCode hash;
3871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3873 /* Sanity check for client registration */
3874 if (NULL == (c = client_get (client)))
3877 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3882 /* Message sanity check */
3883 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3886 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3890 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3891 /* Sanity check for tunnel numbering */
3892 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3898 /* Sanity check for duplicate tunnel IDs */
3899 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3902 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3906 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3907 while (NULL != tunnel_get_by_pi (myid, next_tid))
3908 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3909 t->id.tid = next_tid++;
3910 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3912 t->local_tid = ntohl (t_msg->tunnel_id);
3913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
3914 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3916 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3918 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3920 GNUNET_CONTAINER_multihashmap_put (c->own_tunnels, &hash, t,
3921 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3924 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3928 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3930 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3931 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3934 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3937 t->tree = tree_new (myid);
3939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
3940 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3946 * Handler for requests of deleting tunnels
3948 * @param cls closure
3949 * @param client identification of the client
3950 * @param message the actual message
3953 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3954 const struct GNUNET_MessageHeader *message)
3956 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3957 struct MeshClient *c;
3958 struct MeshTunnel *t;
3959 MESH_TunnelNumber tid;
3961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3962 "Got a DESTROY TUNNEL from client!\n");
3964 /* Sanity check for client registration */
3965 if (NULL == (c = client_get (client)))
3968 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3971 /* Message sanity check */
3972 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3975 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3979 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3981 /* Retrieve tunnel */
3982 tid = ntohl (tunnel_msg->tunnel_id);
3983 t = tunnel_get_by_local_id(c, tid);
3987 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
3988 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3991 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3993 client_ignore_tunnel (c, t);
3995 // TODO: when to destroy incoming tunnel?
3996 if (t->nclients == 0)
3998 GNUNET_assert (GNUNET_YES ==
3999 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
4001 GNUNET_assert (GNUNET_YES ==
4002 GNUNET_CONTAINER_multihashmap_remove (t->peers,
4003 &my_full_id.hashPubKey,
4007 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4010 send_client_tunnel_disconnect(t, c);
4011 client_delete_tunnel(c, t);
4013 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4015 tunnel_send_destroy (t);
4017 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4023 * Handler for connection requests to new peers
4025 * @param cls closure
4026 * @param client identification of the client
4027 * @param message the actual message (PeerControl)
4030 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
4031 const struct GNUNET_MessageHeader *message)
4033 struct GNUNET_MESH_PeerControl *peer_msg;
4034 struct MeshPeerInfo *peer_info;
4035 struct MeshClient *c;
4036 struct MeshTunnel *t;
4037 MESH_TunnelNumber tid;
4039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
4040 /* Sanity check for client registration */
4041 if (NULL == (c = client_get (client)))
4044 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4048 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4049 /* Sanity check for message size */
4050 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4053 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4057 /* Tunnel exists? */
4058 tid = ntohl (peer_msg->tunnel_id);
4059 t = tunnel_get_by_local_id (c, tid);
4063 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4067 /* Does client own tunnel? */
4068 if (t->owner->handle != client)
4071 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
4075 GNUNET_i2s (&peer_msg->peer));
4076 peer_info = peer_info_get (&peer_msg->peer);
4078 tunnel_add_peer (t, peer_info);
4079 peer_info_connect (peer_info, t);
4081 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4087 * Handler for disconnection requests of peers in a tunnel
4089 * @param cls closure
4090 * @param client identification of the client
4091 * @param message the actual message (PeerControl)
4094 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
4095 const struct GNUNET_MessageHeader *message)
4097 struct GNUNET_MESH_PeerControl *peer_msg;
4098 struct MeshPeerInfo *peer_info;
4099 struct MeshClient *c;
4100 struct MeshTunnel *t;
4101 MESH_TunnelNumber tid;
4103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
4104 /* Sanity check for client registration */
4105 if (NULL == (c = client_get (client)))
4108 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4111 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4112 /* Sanity check for message size */
4113 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4116 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4120 /* Tunnel exists? */
4121 tid = ntohl (peer_msg->tunnel_id);
4122 t = tunnel_get_by_local_id (c, tid);
4126 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4131 /* Does client own tunnel? */
4132 if (t->owner->handle != client)
4135 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
4140 GNUNET_i2s (&peer_msg->peer));
4141 /* Is the peer in the tunnel? */
4143 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
4144 if (NULL == peer_info)
4147 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4151 /* Ok, delete peer from tunnel */
4152 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
4153 &peer_msg->peer.hashPubKey);
4155 send_destroy_path (t, peer_info->id);
4156 tunnel_delete_peer (t, peer_info->id);
4157 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4163 * Handler for connection requests to new peers by type
4165 * @param cls closure
4166 * @param client identification of the client
4167 * @param message the actual message (ConnectPeerByType)
4170 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
4171 const struct GNUNET_MessageHeader *message)
4173 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
4174 struct MeshClient *c;
4175 struct MeshTunnel *t;
4176 GNUNET_HashCode hash;
4177 MESH_TunnelNumber tid;
4179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
4180 /* Sanity check for client registration */
4181 if (NULL == (c = client_get (client)))
4184 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4188 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
4189 /* Sanity check for message size */
4190 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
4191 ntohs (connect_msg->header.size))
4194 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4198 /* Tunnel exists? */
4199 tid = ntohl (connect_msg->tunnel_id);
4200 t = tunnel_get_by_local_id (c, tid);
4204 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4208 /* Does client own tunnel? */
4209 if (t->owner->handle != client)
4212 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4216 /* Do WE have the service? */
4217 t->type = ntohl (connect_msg->type);
4218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
4219 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
4220 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
4223 /* Yes! Fast forward, add ourselves to the tunnel and send the
4224 * good news to the client, and alert the destination client of
4225 * an incoming tunnel.
4227 * FIXME send a path create to self, avoid code duplication
4229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
4230 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
4231 peer_info_get (&my_full_id),
4232 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
4235 send_client_peer_connected (t, myid);
4236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
4237 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4239 t->local_tid_dest = next_local_tid++;
4240 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4241 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4242 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4246 /* Ok, lets find a peer offering the service */
4247 if (NULL != t->dht_get_type)
4249 GNUNET_DHT_get_stop (t->dht_get_type);
4251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
4252 GNUNET_h2s (&hash));
4254 GNUNET_DHT_get_start (dht_handle,
4255 GNUNET_BLOCK_TYPE_TEST, &hash, 10,
4256 GNUNET_DHT_RO_RECORD_ROUTE |
4257 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
4258 &dht_get_type_handler, t);
4260 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4266 * Handler for client traffic directed to one peer
4268 * @param cls closure
4269 * @param client identification of the client
4270 * @param message the actual message
4273 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4274 const struct GNUNET_MessageHeader *message)
4276 struct MeshClient *c;
4277 struct MeshTunnel *t;
4278 struct MeshPeerInfo *pi;
4279 struct GNUNET_MESH_Unicast *data_msg;
4280 MESH_TunnelNumber tid;
4283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4284 "Got a unicast request from a client!\n");
4286 /* Sanity check for client registration */
4287 if (NULL == (c = client_get (client)))
4290 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4293 data_msg = (struct GNUNET_MESH_Unicast *) message;
4294 /* Sanity check for message size */
4295 size = ntohs (message->size);
4296 if (sizeof (struct GNUNET_MESH_Unicast) +
4297 sizeof (struct GNUNET_MessageHeader) > size)
4300 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4304 /* Tunnel exists? */
4305 tid = ntohl (data_msg->tid);
4306 t = tunnel_get_by_local_id (c, tid);
4310 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4314 /* Is it a local tunnel? Then, does client own the tunnel? */
4315 if (t->owner->handle != client)
4318 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4322 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
4323 &data_msg->destination.hashPubKey);
4324 /* Is the selected peer in the tunnel? */
4328 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4332 /* Ok, everything is correct, send the message
4333 * (pretend we got it from a mesh peer)
4336 char buf[ntohs (message->size)] GNUNET_ALIGN;
4337 struct GNUNET_MESH_Unicast *copy;
4339 /* Work around const limitation */
4340 copy = (struct GNUNET_MESH_Unicast *) buf;
4341 memcpy (buf, data_msg, size);
4342 copy->oid = my_full_id;
4343 copy->tid = htonl (t->id.tid);
4344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4345 " calling generic handler...\n");
4346 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4348 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4354 * Handler for client traffic directed to the origin
4356 * @param cls closure
4357 * @param client identification of the client
4358 * @param message the actual message
4361 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4362 const struct GNUNET_MessageHeader *message)
4364 struct GNUNET_MESH_ToOrigin *data_msg;
4365 struct GNUNET_PeerIdentity id;
4366 struct MeshClient *c;
4367 struct MeshTunnel *t;
4368 MESH_TunnelNumber tid;
4371 /* Sanity check for client registration */
4372 if (NULL == (c = client_get (client)))
4375 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4378 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4379 /* Sanity check for message size */
4380 size = ntohs (message->size);
4381 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4382 sizeof (struct GNUNET_MessageHeader) > size)
4385 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4389 /* Tunnel exists? */
4390 tid = ntohl (data_msg->tid);
4391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4392 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
4393 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4396 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4399 t = tunnel_get_by_local_id (c, tid);
4403 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4407 /* It should be sent by someone who has this as incoming tunnel. */
4408 if (-1 == client_knows_tunnel (c, t))
4411 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4414 GNUNET_PEER_resolve (t->id.oid, &id);
4416 /* Ok, everything is correct, send the message
4417 * (pretend we got it from a mesh peer)
4420 char buf[ntohs (message->size)] GNUNET_ALIGN;
4421 struct GNUNET_MESH_ToOrigin *copy;
4423 /* Work around const limitation */
4424 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4425 memcpy (buf, data_msg, size);
4427 copy->tid = htonl (t->id.tid);
4428 copy->sender = my_full_id;
4429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4430 " calling generic handler...\n");
4431 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4433 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4439 * Handler for client traffic directed to all peers in a tunnel
4441 * @param cls closure
4442 * @param client identification of the client
4443 * @param message the actual message
4446 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4447 const struct GNUNET_MessageHeader *message)
4449 struct MeshClient *c;
4450 struct MeshTunnel *t;
4451 struct GNUNET_MESH_Multicast *data_msg;
4452 MESH_TunnelNumber tid;
4454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4455 "Got a multicast request from a client!\n");
4457 /* Sanity check for client registration */
4458 if (NULL == (c = client_get (client)))
4461 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4464 data_msg = (struct GNUNET_MESH_Multicast *) message;
4465 /* Sanity check for message size */
4466 if (sizeof (struct GNUNET_MESH_Multicast) +
4467 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4470 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4474 /* Tunnel exists? */
4475 tid = ntohl (data_msg->tid);
4476 t = tunnel_get_by_local_id (c, tid);
4480 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4484 /* Does client own tunnel? */
4485 if (t->owner->handle != client)
4488 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4493 char buf[ntohs (message->size)] GNUNET_ALIGN;
4494 struct GNUNET_MESH_Multicast *copy;
4496 copy = (struct GNUNET_MESH_Multicast *) buf;
4497 memcpy (buf, message, ntohs (message->size));
4498 copy->oid = my_full_id;
4499 copy->tid = htonl (t->id.tid);
4500 copy->ttl = htonl (DEFAULT_TTL);
4501 copy->mid = htonl (t->mid + 1);
4502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4503 " calling generic handler...\n");
4504 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4507 /* receive done gets called when last copy is sent to a neighbor */
4512 * Functions to handle messages from clients
4514 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4515 {&handle_local_new_client, NULL,
4516 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4517 {&handle_local_tunnel_create, NULL,
4518 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4519 sizeof (struct GNUNET_MESH_TunnelMessage)},
4520 {&handle_local_tunnel_destroy, NULL,
4521 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4522 sizeof (struct GNUNET_MESH_TunnelMessage)},
4523 {&handle_local_connect_add, NULL,
4524 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4525 sizeof (struct GNUNET_MESH_PeerControl)},
4526 {&handle_local_connect_del, NULL,
4527 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4528 sizeof (struct GNUNET_MESH_PeerControl)},
4529 {&handle_local_connect_by_type, NULL,
4530 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4531 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4532 {&handle_local_unicast, NULL,
4533 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4534 {&handle_local_to_origin, NULL,
4535 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4536 {&handle_local_multicast, NULL,
4537 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4543 * To be called on core init/fail.
4545 * @param cls service closure
4546 * @param server handle to the server for this service
4547 * @param identity the public identity of this peer
4550 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4551 const struct GNUNET_PeerIdentity *identity)
4553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4554 core_handle = server;
4555 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4558 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4559 GNUNET_SCHEDULER_shutdown ();
4565 * Method called whenever a given peer connects.
4567 * @param cls closure
4568 * @param peer peer identity this notification is about
4569 * @param atsi performance data for the connection
4570 * @param atsi_count number of records in 'atsi'
4573 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4574 const struct GNUNET_ATS_Information *atsi,
4575 unsigned int atsi_count)
4577 struct MeshPeerInfo *peer_info;
4578 struct MeshPeerPath *path;
4580 DEBUG_CONN ("Peer connected\n");
4581 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4582 peer_info = peer_info_get (peer);
4583 if (myid == peer_info->id)
4585 DEBUG_CONN (" (self)\n");
4590 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4592 path = path_new (2);
4593 path->peers[0] = myid;
4594 path->peers[1] = peer_info->id;
4595 GNUNET_PEER_change_rc (myid, 1);
4596 GNUNET_PEER_change_rc (peer_info->id, 1);
4597 peer_info_add_path (peer_info, path, GNUNET_YES);
4602 * Method called whenever a peer disconnects.
4604 * @param cls closure
4605 * @param peer peer identity this notification is about
4608 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4610 struct MeshPeerInfo *pi;
4611 struct MeshPeerQueue *q;
4612 struct MeshPeerQueue *n;
4614 DEBUG_CONN ("Peer disconnected\n");
4615 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4627 /* try to reroute this traffic instead */
4628 queue_destroy(q, GNUNET_YES);
4632 peer_info_remove_path (pi, pi->id, myid);
4635 DEBUG_CONN (" (self)\n");
4641 /******************************************************************************/
4642 /************************ MAIN FUNCTIONS ****************************/
4643 /******************************************************************************/
4646 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4648 * @param cls closure
4649 * @param key current key code
4650 * @param value value in the hash map
4651 * @return GNUNET_YES if we should continue to iterate,
4655 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4657 struct MeshTunnel *t = value;
4664 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4666 * @param cls closure
4667 * @param key current key code
4668 * @param value value in the hash map
4669 * @return GNUNET_YES if we should continue to iterate,
4673 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4675 struct MeshPeerInfo *p = value;
4676 struct MeshPeerQueue *q;
4677 struct MeshPeerQueue *n;
4685 queue_destroy(q, GNUNET_YES);
4689 peer_info_destroy (p);
4694 * Task run during shutdown.
4700 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4704 if (core_handle != NULL)
4706 GNUNET_CORE_disconnect (core_handle);
4709 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4710 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4711 if (dht_handle != NULL)
4713 GNUNET_DHT_disconnect (dht_handle);
4718 GNUNET_SERVER_notification_context_destroy (nc);
4721 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4723 GNUNET_SCHEDULER_cancel (announce_id_task);
4724 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4730 * Process mesh requests.
4732 * @param cls closure
4733 * @param server the initialized server
4734 * @param c configuration to use
4737 run (void *cls, struct GNUNET_SERVER_Handle *server,
4738 const struct GNUNET_CONFIGURATION_Handle *c)
4740 struct MeshPeerInfo *peer;
4741 struct MeshPeerPath *p;
4744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4745 server_handle = server;
4746 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4748 NULL, /* Closure passed to MESH functions */
4749 &core_init, /* Call core_init once connected */
4750 &core_connect, /* Handle connects */
4751 &core_disconnect, /* remove peers on disconnects */
4752 NULL, /* Don't notify about all incoming messages */
4753 GNUNET_NO, /* For header only in notification */
4754 NULL, /* Don't notify about all outbound messages */
4755 GNUNET_NO, /* For header-only out notification */
4756 core_handlers); /* Register these handlers */
4758 if (core_handle == NULL)
4761 GNUNET_SCHEDULER_shutdown ();
4766 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4769 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4771 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4772 GNUNET_SCHEDULER_shutdown ();
4775 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4776 GNUNET_free (keyfile);
4777 if (my_private_key == NULL)
4779 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4780 _("Mesh service could not access hostkey. Exiting.\n"));
4781 GNUNET_SCHEDULER_shutdown ();
4784 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4785 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4786 &my_full_id.hashPubKey);
4787 myid = GNUNET_PEER_intern (&my_full_id);
4789 // transport_handle = GNUNET_TRANSPORT_connect(c,
4796 dht_handle = GNUNET_DHT_connect (c, 64);
4797 if (dht_handle == NULL)
4803 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4805 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4806 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4807 peers = GNUNET_CONTAINER_multihashmap_create (32);
4808 applications = GNUNET_CONTAINER_multihashmap_create (32);
4809 types = GNUNET_CONTAINER_multihashmap_create (32);
4811 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4812 nc = GNUNET_SERVER_notification_context_create (server_handle,
4814 GNUNET_SERVER_disconnect_notify (server_handle,
4815 &handle_local_client_disconnect, NULL);
4819 clients_tail = NULL;
4822 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4823 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4825 /* Create a peer_info for the local peer */
4826 peer = peer_info_get (&my_full_id);
4829 GNUNET_PEER_change_rc (myid, 1);
4830 peer_info_add_path (peer, p, GNUNET_YES);
4832 /* Scheduled the task to clean up when shutdown is called */
4833 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
4840 * The main function for the mesh service.
4842 * @param argc number of arguments from the command line
4843 * @param argv command line arguments
4844 * @return 0 ok, 1 on error
4847 main (int argc, char *const *argv)
4851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
4854 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");