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
49 #include "mesh_protocol.h"
50 #include "gnunet_dht_service.h"
51 #include "mesh_tunnel_tree.h"
53 /* TODO: move into configuration file */
54 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
55 GNUNET_TIME_UNIT_SECONDS,\
57 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
58 GNUNET_TIME_UNIT_SECONDS,\
61 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
62 GNUNET_TIME_UNIT_SECONDS,\
65 #define GET_RESTART_TIME GNUNET_TIME_relative_multiply(\
66 GNUNET_TIME_UNIT_SECONDS,\
69 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
70 GNUNET_TIME_UNIT_SECONDS,\
72 #define DEFAULT_TTL 64
74 #define MESH_DEBUG_DHT GNUNET_NO
78 /******************************************************************************/
79 /************************ DATA STRUCTURES ****************************/
80 /******************************************************************************/
82 /** FWD declaration */
87 * Struct representing a piece of data being sent to other peers
91 /** Tunnel it belongs to. */
94 /** In case of a multicast, task to allow a client to send more data if
95 * some neighbor is too slow. */
96 GNUNET_SCHEDULER_TaskIdentifier *task;
98 /** How many remaining neighbors we need to send this to. */
99 unsigned int *reference_counter;
101 /** Size of the data. */
110 * Struct containing all info possibly needed to build a package when called
113 struct MeshTransmissionDescriptor
115 /** ID of the tunnel this packet travels in */
116 struct MESH_TunnelID *origin;
118 /** Who was this message being sent to */
119 struct MeshPeerInfo *peer;
121 /** Ultimate destination of the packet */
122 GNUNET_PEER_Id destination;
124 /** Which handler was used to request the transmission */
125 unsigned int handler_n;
127 /** Data descriptor */
128 struct MeshData* mesh_data;
133 * Struct containing all information regarding a given peer
143 * Last time we heard from this peer
145 struct GNUNET_TIME_Absolute last_contact;
148 * Number of attempts to reconnect so far
150 int n_reconnect_attempts;
153 * Paths to reach the peer, ordered by ascending hop count
155 struct MeshPeerPath *path_head;
158 * Paths to reach the peer, ordered by ascending hop count
160 struct MeshPeerPath *path_tail;
163 * Handle to stop the DHT search for a path to this peer
165 struct GNUNET_DHT_GetHandle *dhtget;
168 * Closure given to the DHT GET
170 struct MeshPathInfo *dhtgetcls;
173 * Handles to stop queued transmissions for this peer
175 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
178 * Pointer to info stuctures used as cls for queued transmissions
180 void *infos[CORE_QUEUE_SIZE];
183 * Type of message being in each transmission
185 uint16_t types[CORE_QUEUE_SIZE];
188 * Array of tunnels this peer participates in
189 * (most probably a small amount, therefore not a hashmap)
190 * When the path to the peer changes, notify these tunnels to let them
191 * re-adjust their path trees.
193 struct MeshTunnel **tunnels;
196 * Number of tunnels this peers participates in
198 unsigned int ntunnels;
203 * Data scheduled to transmit (to local client or remote peer)
210 struct MeshQueue *next;
211 struct MeshQueue *prev;
214 * Target of the data (NULL if target is client)
216 struct MeshPeerInfo *peer;
219 * Client to send the data to (NULL if target is peer)
221 struct MeshClient *client;
224 * Size of the message to transmit
229 * How old is the data?
231 struct GNUNET_TIME_Absolute timestamp;
236 struct GNUNET_MessageHeader *data;
240 * Globally unique tunnel identification (owner + number)
241 * DO NOT USE OVER THE NETWORK
246 * Node that owns the tunnel
251 * Tunnel number to differentiate all the tunnels owned by the node oid
252 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
254 MESH_TunnelNumber tid;
258 struct MeshClient; /* FWD declaration */
261 * Struct containing all information regarding a tunnel
262 * For an intermediate node the improtant info used will be:
263 * - id Tunnel unique identification
264 * - paths[0] To know where to send it next
265 * - metainfo: ready, speeds, accounting
272 struct MESH_TunnelID id;
275 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
277 MESH_TunnelNumber local_tid;
280 * Local tunnel number for local destination clients
282 MESH_TunnelNumber local_tid_dest;
285 * ID of the last multicast packet seen/sent.
290 * Last time the tunnel was used
292 struct GNUNET_TIME_Absolute timestamp;
295 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
296 * containing peers added by id or by type, not intermediate peers.
298 struct GNUNET_CONTAINER_MultiHashMap *peers;
301 * Number of peers that are connected and potentially ready to receive data
303 unsigned int peers_ready;
306 * Number of peers that have been added to the tunnel
308 unsigned int peers_total;
311 * Client owner of the tunnel, if any
313 struct MeshClient *client;
316 * Client destination of the tunnel, if any
318 struct MeshClient *client_dest;
321 * Messages ready to transmit
323 struct MeshQueue *queue_head;
324 struct MeshQueue *queue_tail;
329 struct MeshTunnelTree *tree;
332 * Application type we are looking for in this tunnel
334 GNUNET_MESH_ApplicationType type;
337 * Used to search peers offering a service
339 struct GNUNET_DHT_GetHandle *dht_get_type;
342 * Task to keep the used paths alive
344 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
347 * Task to destroy the tunnel after timeout
349 * FIXME: merge the two? a tunnel will have either
350 * a path refresh OR a timeout, never both!
352 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
357 * Info needed to work with tunnel paths and peers
364 struct MeshTunnel *t;
367 * Neighbouring peer to whom we send the packet to
369 struct MeshPeerInfo *peer;
374 struct MeshPeerPath *path;
377 * Position in peer's transmit queue
384 * Struct containing information about a client of the service
391 struct MeshClient *next;
392 struct MeshClient *prev;
395 * Tunnels that belong to this client, indexed by local id
397 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
400 * Handle to communicate with the client
402 struct GNUNET_SERVER_Client *handle;
405 * Applications that this client has claimed to provide
407 struct GNUNET_CONTAINER_MultiHashMap *apps;
410 * Messages that this client has declared interest in
412 struct GNUNET_CONTAINER_MultiHashMap *types;
416 * ID of the client, for debug messages
425 /******************************************************************************/
426 /************************ DEBUG FUNCTIONS ****************************/
427 /******************************************************************************/
431 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
432 * @param cls string to print
433 * @param tc task context
436 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
440 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
448 /******************************************************************************/
449 /*********************** GLOBAL VARIABLES ****************************/
450 /******************************************************************************/
455 static struct MeshClient *clients;
456 static struct MeshClient *clients_tail;
459 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
461 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
464 * Tunnels incoming, indexed by MESH_TunnelNumber
465 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
467 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
470 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
472 static struct GNUNET_CONTAINER_MultiHashMap *peers;
475 * Handle to communicate with core
477 static struct GNUNET_CORE_Handle *core_handle;
480 * Handle to communicate with transport
482 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
487 static struct GNUNET_DHT_Handle *dht_handle;
492 static struct GNUNET_SERVER_Handle *server_handle;
495 * Notification context, to send messages to local clients
497 static struct GNUNET_SERVER_NotificationContext *nc;
500 * Local peer own ID (memory efficient handle)
502 static GNUNET_PEER_Id myid;
505 * Local peer own ID (full value)
507 static struct GNUNET_PeerIdentity my_full_id;
512 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
517 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
520 * Tunnel ID for the next created tunnel (global tunnel number)
522 static MESH_TunnelNumber next_tid;
525 * Tunnel ID for the next incoming tunnel (local tunnel number)
527 static MESH_TunnelNumber next_local_tid;
530 * All application types provided by this peer
532 static struct GNUNET_CONTAINER_MultiHashMap *applications;
535 * All message types clients of this peer are interested in
537 static struct GNUNET_CONTAINER_MultiHashMap *types;
540 * Task to periodically announce provided applications
542 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
545 * Task to periodically announce itself in the network
547 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
550 unsigned int next_client_id;
554 /******************************************************************************/
555 /************************ ITERATORS ****************************/
556 /******************************************************************************/
558 /* FIXME move iterators here */
561 /******************************************************************************/
562 /************************ PERIODIC FUNCTIONS ****************************/
563 /******************************************************************************/
566 * Announce iterator over for each application provided by the peer
569 * @param key current key code
570 * @param value value in the hash map
571 * @return GNUNET_YES if we should continue to
576 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
578 /* FIXME are hashes in multihash map equal on all aquitectures? */
579 GNUNET_DHT_put (dht_handle, key, 10U,
580 GNUNET_DHT_RO_RECORD_ROUTE |
581 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
582 sizeof (struct GNUNET_PeerIdentity),
583 (const char *) &my_full_id,
585 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
586 &mesh_debug, "DHT_put for app completed");
588 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
590 APP_ANNOUNCE_TIME, NULL, NULL);
597 * Periodically announce what applications are provided by local clients
600 * @param tc task context
603 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
605 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
607 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
613 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
615 announce_applications_task =
616 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
626 * Periodically announce self id in the DHT
629 * @param tc task context
632 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
634 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
636 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
640 * - Set data expiration in function of X
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
645 GNUNET_i2s (&my_full_id));
647 GNUNET_DHT_put (dht_handle, /* DHT handle */
648 &my_full_id.hashPubKey, /* Key to use */
649 10U, /* Replication level */
650 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
651 GNUNET_BLOCK_TYPE_TEST, /* Block type */
652 sizeof (my_full_id), /* Size of the data */
653 (char *) &my_full_id, /* Data itself */
654 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
655 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
657 &mesh_debug, "DHT_put for id completed");
659 NULL, /* Continuation */
660 NULL); /* Continuation closure */
663 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
668 * Function to process paths received for a new peer addition. The recorded
669 * paths form the initial tunnel, which can be optimized later.
670 * Called on each result obtained for the DHT search.
673 * @param exp when will this value expire
674 * @param key key of the result
675 * @param type type of the result
676 * @param size number of bytes in data
677 * @param data pointer to the result data
680 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
681 const GNUNET_HashCode * key,
682 const struct GNUNET_PeerIdentity *get_path,
683 unsigned int get_path_length,
684 const struct GNUNET_PeerIdentity *put_path,
685 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
686 size_t size, const void *data);
689 /******************************************************************************/
690 /****************** GENERAL HELPER FUNCTIONS ************************/
691 /******************************************************************************/
694 * Check if client has registered with the service and has not disconnected
696 * @param client the client to check
698 * @return non-NULL if client exists in the global DLL
700 static struct MeshClient *
701 client_get (struct GNUNET_SERVER_Client *client)
703 struct MeshClient *c;
708 if (c->handle == client)
717 * Checks if a given client has subscribed to certain message type
719 * @param message_type Type of message to check
720 * @param c Client to check
722 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
727 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
731 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
732 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
737 * Allow a client to send more data after transmitting a multicast message
738 * which some neighbor has not yet accepted altough a reasonable time has
741 * @param cls Closure (DataDescriptor containing the task identifier)
742 * @param tc Task Context
745 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
747 struct MeshData *mdata = cls;
749 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
752 GNUNET_assert (NULL != mdata->reference_counter);
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754 "MESH: CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
755 mdata->reference_counter);
757 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
758 GNUNET_SERVER_receive_done (mdata->t->client->handle, GNUNET_OK);
763 * Search for a tunnel by global ID using full PeerIdentities
765 * @param oid owner of the tunnel
766 * @param tid global tunnel number
768 * @return tunnel handler, NULL if doesn't exist
770 static struct MeshTunnel *
771 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
775 * Notify a tunnel that a connection has broken that affects at least
778 * @param t Tunnel affected.
779 * @param p1 Peer that got disconnected from p2.
780 * @param p2 Peer that got disconnected from p1.
782 * @return Short ID of the peer disconnected (either p1 or p2).
783 * 0 if the tunnel remained unaffected.
785 static GNUNET_PEER_Id
786 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
791 * Send the message to all clients that have subscribed to its type
793 * @param msg Pointer to the message itself
794 * @param payload Pointer to the payload of the message.
795 * @return number of clients this message was sent to
798 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
799 const struct GNUNET_MessageHeader *payload)
801 struct GNUNET_PeerIdentity *oid;
802 struct MeshClient *c;
803 struct MeshTunnel *t;
804 MESH_TunnelNumber *tid;
807 char cbuf[htons (msg->size)];
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
811 type = ntohs (payload->type);
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
815 memcpy (cbuf, msg, sizeof (cbuf));
816 switch (htons (msg->type))
818 struct GNUNET_MESH_Unicast *uc;
819 struct GNUNET_MESH_Multicast *mc;
820 struct GNUNET_MESH_ToOrigin *to;
822 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
823 uc = (struct GNUNET_MESH_Unicast *) cbuf;
827 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
828 mc = (struct GNUNET_MESH_Multicast *) cbuf;
832 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
833 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
841 t = tunnel_get (oid, ntohl (*tid));
847 // FIXME proper client differentiation mechanism required
848 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
849 *tid = htonl (t->local_tid);
851 *tid = htonl (t->local_tid_dest != 0 ? t->local_tid_dest : t->local_tid);
852 for (count = 0, c = clients; c != NULL; c = c->next)
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
857 if (client_is_subscribed (type, c))
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
861 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
862 (struct GNUNET_MessageHeader
863 *) cbuf, GNUNET_YES);
871 * Notify the client that owns the tunnel that a peer has connected to it
872 * (the requested path to it has been confirmed).
874 * @param t Tunnel whose owner to notify
875 * @param id Short id of the peer that has connected
878 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
880 struct GNUNET_MESH_PeerControl pc;
882 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
883 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
884 pc.tunnel_id = htonl (t->local_tid);
885 GNUNET_PEER_resolve (id, &pc.peer);
886 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, &pc.header,
892 * Notify all clients (not depending on registration status) that the incoming
893 * tunnel is no longer valid.
895 * @param t Tunnel that was destroyed.
898 send_clients_tunnel_destroy (struct MeshTunnel *t)
900 struct GNUNET_MESH_TunnelMessage msg;
902 msg.header.size = htons (sizeof (msg));
903 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
904 msg.tunnel_id = htonl (t->local_tid);
905 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
910 * Notify a client that the other local client disconnected, if needed.
911 * In case the origin disconnects, the destination get a tunnel destroy
912 * notification. Otherwise, the origin gets a (local ID) peer disconnected.
914 * @param t Tunnel that was destroyed.
915 * @param c Client that disconnected
918 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
920 if (c == t->client_dest)
922 struct GNUNET_MESH_PeerControl msg;
924 msg.header.size = htons (sizeof (msg));
925 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
926 msg.tunnel_id = htonl (t->local_tid);
927 msg.peer = my_full_id;
928 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
929 &msg.header, GNUNET_NO);
931 else if (NULL != t->client_dest && c == t->client)
933 struct GNUNET_MESH_TunnelMessage msg;
935 msg.header.size = htons (sizeof (msg));
936 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
937 msg.tunnel_id = htonl (t->local_tid_dest);
938 GNUNET_SERVER_notification_context_unicast (nc, t->client_dest->handle,
939 &msg.header, GNUNET_NO);
945 * Function called to notify a client about the socket
946 * being ready to queue more data. "buf" will be
947 * NULL and "size" zero if the socket was closed for
948 * writing in the meantime.
951 * @param size number of bytes available in buf
952 * @param buf where the callee should write the message
953 * @return number of bytes written to buf
956 send_core_create_path (void *cls, size_t size, void *buf);
960 * Function called to notify a client about the socket
961 * being ready to queue more data. "buf" will be
962 * NULL and "size" zero if the socket was closed for
963 * writing in the meantime.
965 * @param cls closure (data itself)
966 * @param size number of bytes available in buf
967 * @param buf where the callee should write the message
969 * @return number of bytes written to buf
972 send_core_data_multicast (void *cls, size_t size, void *buf);
976 * Decrements the reference counter and frees all resources if needed
978 * @param dd Data Descriptor used in a multicast message. Freed if needed.
981 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
983 /* Make sure it's a multicast packet */
984 GNUNET_assert (NULL != mesh_data->reference_counter);
986 if (0 == --(*(mesh_data->reference_counter)))
988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
989 if (NULL != mesh_data->task)
991 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
993 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
995 GNUNET_SERVER_receive_done (mesh_data->t->client->handle, GNUNET_OK);
997 GNUNET_free (mesh_data->task);
999 GNUNET_free (mesh_data->reference_counter);
1000 GNUNET_free (mesh_data->data);
1001 GNUNET_free (mesh_data);
1007 * Cancel a core transmission that was already requested and free all resources
1008 * associated to the request.
1010 * @param peer PeeInfo of the peer whose transmission is cancelled.
1011 * @param i Position of the transmission to be cancelled.
1014 peer_info_cancel_transmission (struct MeshPeerInfo *peer, unsigned int i)
1016 if (NULL != peer->core_transmit[i])
1018 struct MeshTransmissionDescriptor *dd;
1019 struct MeshPathInfo *path_info;
1023 struct GNUNET_PeerIdentity id;
1025 GNUNET_PEER_resolve (peer->id, &id);
1026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1027 "MESH: Cancelling data transmission at %s [%u]\n",
1028 GNUNET_i2s (&id), i);
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message type %u\n",
1033 /* TODO: notify that tranmission has failed */
1034 switch (peer->types[i])
1036 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1037 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1038 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
1040 dd = peer->infos[i];
1041 data_descriptor_decrement_multicast (dd->mesh_data);
1043 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
1044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
1045 path_info = peer->infos[i];
1046 path_destroy (path_info->path);
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
1052 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
1053 peer->core_transmit[i] = NULL;
1054 GNUNET_free (peer->infos[i]);
1060 * Get a unused CORE slot to transmit a message to a peer. If all the slots
1061 * are used, cancel one and return it's position.
1063 * @param peer PeerInfo of the neighbor we want to transmit to.
1065 * @return The index of an available slot to transmit to the neighbor.
1068 peer_info_transmit_slot (struct MeshPeerInfo *peer)
1072 for (i = 0; peer->core_transmit[i]; i++)
1074 if (i == (CORE_QUEUE_SIZE - 1))
1076 /* All positions are taken! Overwriting! */
1078 peer_info_cancel_transmission (peer, 0);
1087 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1088 * and insert it in the appropiate structures if the peer is not known yet.
1090 * @param peer Full identity of the peer.
1092 * @return Existing or newly created peer info.
1094 static struct MeshPeerInfo *
1095 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1097 struct MeshPeerInfo *peer_info;
1099 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1100 if (NULL == peer_info)
1103 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1104 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1105 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1106 peer_info->id = GNUNET_PEER_intern (peer);
1114 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1115 * and insert it in the appropiate structures if the peer is not known yet.
1117 * @param peer Short identity of the peer.
1119 * @return Existing or newly created peer info.
1121 static struct MeshPeerInfo *
1122 peer_info_get_short (const GNUNET_PEER_Id peer)
1124 struct GNUNET_PeerIdentity id;
1126 GNUNET_PEER_resolve (peer, &id);
1127 return peer_info_get (&id);
1132 * Iterator to remove the tunnel from the list of tunnels a peer participates
1135 * @param cls Closure (tunnel info)
1136 * @param key GNUNET_PeerIdentity of the peer (unused)
1137 * @param value PeerInfo of the peer
1139 * @return always GNUNET_YES, to keep iterating
1142 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1144 struct MeshTunnel *t = cls;
1145 struct MeshPeerInfo *peer = value;
1148 for (i = 0; i < peer->ntunnels; i++)
1151 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1154 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1155 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1164 * Core callback to write a
1166 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1167 * @param size Number of bytes available in buf.
1168 * @param buf Where the to write the message.
1170 * @return number of bytes written to buf
1173 send_core_data_raw (void *cls, size_t size, void *buf)
1175 struct MeshTransmissionDescriptor *info = cls;
1176 struct GNUNET_MessageHeader *msg;
1179 GNUNET_assert (NULL != info);
1180 GNUNET_assert (NULL != info->mesh_data);
1181 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1182 total_size = ntohs (msg->size);
1184 if (total_size > size)
1186 struct GNUNET_PeerIdentity id;
1188 GNUNET_PEER_resolve (info->peer->id, &id);
1189 info->peer->core_transmit[info->handler_n] =
1190 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1191 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1192 size, &send_core_data_raw, info);
1195 info->peer->core_transmit[info->handler_n] = NULL;
1196 memcpy (buf, msg, total_size);
1197 GNUNET_free (info->mesh_data);
1204 * Sends an already built message to a peer, properly registrating
1205 * all used resources.
1207 * @param message Message to send. Fucntion makes a copy of it.
1208 * @param peer Short ID of the neighbor whom to send the message.
1213 send_message (const struct GNUNET_MessageHeader *message,
1214 const struct GNUNET_PeerIdentity *peer)
1216 struct MeshTransmissionDescriptor *info;
1217 struct MeshPeerInfo *neighbor;
1218 struct MeshPeerPath *p;
1222 // GNUNET_TRANSPORT_try_connect();
1224 size = ntohs (message->size);
1225 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1226 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1227 info->mesh_data->data = GNUNET_malloc (size);
1228 memcpy (info->mesh_data->data, message, size);
1229 info->mesh_data->data_len = size;
1230 neighbor = peer_info_get (peer);
1231 for (p = neighbor->path_head; NULL != p; p = p->next)
1241 GNUNET_free (info->mesh_data->data);
1242 GNUNET_free (info->mesh_data);
1246 i = peer_info_transmit_slot (neighbor);
1247 info->handler_n = i;
1248 info->peer = neighbor;
1249 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1250 neighbor->infos[i] = info;
1251 neighbor->core_transmit[i] =
1252 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1253 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1254 size, &send_core_data_raw, info);
1260 * Sends a CREATE PATH message for a path to a peer, properly registrating
1261 * all used resources.
1263 * @param peer PeerInfo of the final peer for whom this path is being created.
1264 * @param p Path itself.
1265 * @param t Tunnel for which the path is created.
1268 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1269 struct MeshTunnel *t)
1271 struct GNUNET_PeerIdentity id;
1272 struct MeshPathInfo *path_info;
1273 struct MeshPeerInfo *neighbor;
1278 p = tree_get_path_to_peer (t->tree, peer->id);
1285 for (i = 0; i < p->length; i++)
1287 if (p->peers[i] == myid)
1290 if (i >= p->length - 1)
1296 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1298 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1299 path_info->path = p;
1301 neighbor = peer_info_get (&id);
1302 path_info->peer = neighbor;
1303 path_info->pos = peer_info_transmit_slot (neighbor);
1304 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1305 neighbor->infos[path_info->pos] = path_info;
1306 neighbor->core_transmit[path_info->pos] = GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
1309 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1311 sizeof (struct GNUNET_MESH_ManipulatePath) + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1312 &send_core_create_path, /* callback */
1313 path_info); /* cls */
1318 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1320 * @param t Tunnel whose path to destroy.
1321 * @param destination Short ID of the peer to whom the path to destroy.
1324 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1326 struct MeshPeerPath *p;
1329 p = tree_get_path_to_peer (t->tree, destination);
1335 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1336 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1338 struct GNUNET_MESH_ManipulatePath *msg;
1339 struct GNUNET_PeerIdentity *pi;
1343 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1344 msg->header.size = htons (size);
1345 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1346 msg->tid = htonl (t->id.tid);
1347 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1348 for (i = 0; i < p->length; i++)
1350 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1352 send_message (&msg->header, tree_get_first_hop (t->tree, destination));
1359 * Try to establish a new connection to this peer.
1360 * Use the best path for the given tunnel.
1361 * If the peer doesn't have any path to it yet, try to get one.
1362 * If the peer already has some path, send a CREATE PATH towards it.
1364 * @param peer PeerInfo of the peer.
1365 * @param t Tunnel for which to create the path, if possible.
1368 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1370 struct MeshPeerPath *p;
1371 struct MeshPathInfo *path_info;
1373 if (NULL != peer->path_head)
1375 p = tree_get_path_to_peer (t->tree, peer->id);
1384 send_create_path (peer, p, t);
1389 send_client_peer_connected (t, myid);
1392 else if (NULL == peer->dhtget)
1394 struct GNUNET_PeerIdentity id;
1396 GNUNET_PEER_resolve (peer->id, &id);
1397 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1398 path_info->peer = peer;
1400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1401 "MESH: Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1402 peer->dhtgetcls = path_info;
1403 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1404 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1405 GNUNET_BLOCK_TYPE_TEST, /* type */
1406 &id.hashPubKey, /* key to search */
1407 4, /* replication level */
1408 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1409 0, /* xquery bits */
1410 &dht_get_id_handler, path_info);
1412 /* Otherwise, there is no path but the DHT get is already started. */
1417 * Task to delay the connection of a peer
1419 * @param cls Closure (path info with tunnel and peer to connect).
1420 * Will be free'd on exection.
1421 * @param tc TaskContext
1424 peer_info_connect_task (void *cls,
1425 const struct GNUNET_SCHEDULER_TaskContext *tc)
1427 struct MeshPathInfo *path_info = cls;
1429 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1434 peer_info_connect (path_info->peer, path_info->t);
1440 * Destroy the peer_info and free any allocated resources linked to it
1442 * @param pi The peer_info to destroy.
1444 * @return GNUNET_OK on success
1447 peer_info_destroy (struct MeshPeerInfo *pi)
1449 struct GNUNET_PeerIdentity id;
1450 struct MeshPeerPath *p;
1451 struct MeshPeerPath *nextp;
1454 GNUNET_PEER_resolve (pi->id, &id);
1455 GNUNET_PEER_change_rc (pi->id, -1);
1458 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1461 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1462 "MESH: removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1464 if (NULL != pi->dhtget)
1466 GNUNET_DHT_get_stop (pi->dhtget);
1467 GNUNET_free (pi->dhtgetcls);
1469 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1471 peer_info_cancel_transmission (pi, i);
1477 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1487 * Remove all paths that rely on a direct connection between p1 and p2
1488 * from the peer itself and notify all tunnels about it.
1490 * @param peer PeerInfo of affected peer.
1491 * @param p1 GNUNET_PEER_Id of one peer.
1492 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1495 * TODO: optimize (see below)
1498 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1501 struct MeshPeerPath *p;
1502 struct MeshPeerPath *aux;
1503 struct MeshPeerInfo *peer_d;
1505 unsigned int destroyed;
1511 p = peer->path_head;
1515 for (i = 0; i < (p->length - 1); i++)
1517 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1518 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1520 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1531 for (i = 0; i < peer->ntunnels; i++)
1533 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1537 * Problem: one or more peers have been deleted from the tunnel tree.
1538 * We don't know who they are to try to add them again.
1539 * We need to try to find a new path for each of the disconnected peers.
1540 * Some of them might already have a path to reach them that does not
1541 * involve p1 and p2. Adding all anew might render in a better tree than
1542 * the trivial immediate fix.
1544 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1545 * its children will be reachable trough him.
1547 peer_d = peer_info_get_short (d);
1550 for (p = peer_d->path_head; NULL != p; p = p->next)
1552 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1560 /* No callback, as peer will be already disconnected and a connection
1561 * scheduled by tunnel_notify_connection_broken.
1563 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1567 peer_info_connect (peer_d, peer->tunnels[i]);
1574 * Add the path to the peer and update the path used to reach it in case this
1577 * @param peer_info Destination peer to add the path to.
1578 * @param path New path to add. Last peer must be the peer in arg 1.
1579 * Path will be either used of freed if already known.
1580 * @param trusted Do we trust that this path is real?
1583 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1586 struct MeshPeerPath *aux;
1590 if ((NULL == peer_info) || (NULL == path))
1593 path_destroy (path);
1596 if (path->peers[path->length - 1] != peer_info->id)
1599 path_destroy (path);
1602 if (path->length <= 2 && GNUNET_NO == trusted)
1604 /* Only allow CORE to tell us about direct paths */
1605 path_destroy (path);
1608 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1609 for (l = 1; l < path->length; l++)
1611 if (path->peers[l] == myid)
1613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shortening path by %u\n", l);
1614 for (l2 = 0; l2 < path->length - l; l2++)
1616 path->peers[l2] = path->peers[l + l2];
1621 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1626 struct GNUNET_PeerIdentity id;
1628 GNUNET_PEER_resolve (peer_info->id, &id);
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: adding path [%u] to peer %s\n",
1630 path->length, GNUNET_i2s (&id));
1633 l = path_get_length (path);
1640 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1641 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1643 l2 = path_get_length (aux);
1646 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1647 peer_info->path_tail, aux, path);
1652 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1654 path_destroy (path);
1659 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1666 * Add the path to the origin peer and update the path used to reach it in case
1667 * this is the shortest.
1668 * The path is given in peer_info -> destination, therefore we turn the path
1669 * upside down first.
1671 * @param peer_info Peer to add the path to, being the origin of the path.
1672 * @param path New path to add after being inversed.
1673 * @param trusted Do we trust that this path is real?
1676 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1677 struct MeshPeerPath *path, int trusted)
1680 peer_info_add_path (peer_info, path, trusted);
1685 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1686 * to obtain a local peer -> destination path and interning the peer ids.
1688 * @return Newly allocated and created path
1690 static struct MeshPeerPath *
1691 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1692 unsigned int get_path_length,
1693 const struct GNUNET_PeerIdentity *put_path,
1694 unsigned int put_path_length)
1696 struct MeshPeerPath *p;
1702 GNUNET_PEER_change_rc (myid, 1);
1703 i = get_path_length;
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1705 for (i--; i >= 0; i--)
1707 id = GNUNET_PEER_intern (&get_path[i]);
1708 if (p->length > 0 && id == p->peers[p->length - 1])
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1711 GNUNET_PEER_change_rc (id, -1);
1715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Adding from GET: %s.\n",
1716 GNUNET_i2s (&get_path[i]));
1718 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1719 p->peers[p->length - 1] = id;
1722 i = put_path_length;
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1724 for (i--; i >= 0; i--)
1726 id = GNUNET_PEER_intern (&put_path[i]);
1729 /* PUT path went through us, so discard the path up until now and start
1730 * from here to get a much shorter (and loop-free) path.
1735 if (p->length > 0 && id == p->peers[p->length - 1])
1737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1738 GNUNET_PEER_change_rc (id, -1);
1742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Adding from PUT: %s.\n",
1743 GNUNET_i2s (&put_path[i]));
1745 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1746 p->peers[p->length - 1] = id;
1750 if (get_path_length > 0)
1751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (first of GET: %s)\n",
1752 GNUNET_i2s (&get_path[0]));
1753 if (put_path_length > 0)
1754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (first of PUT: %s)\n",
1755 GNUNET_i2s (&put_path[0]));
1756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: In total: %d hops\n",
1758 for (i = 0; i < p->length; i++)
1760 struct GNUNET_PeerIdentity peer_id;
1762 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %u: %s\n", p->peers[i],
1764 GNUNET_i2s (&peer_id));
1772 * Adds a path to the peer_infos of all the peers in the path
1774 * @param p Path to process.
1775 * @param confirmed Whether we know if the path works or not. FIXME use
1778 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1782 /* TODO: invert and add */
1783 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1784 for (i++; i < p->length; i++)
1786 struct MeshPeerInfo *aux;
1787 struct MeshPeerPath *copy;
1789 aux = peer_info_get_short (p->peers[i]);
1790 copy = path_duplicate (p);
1791 copy->length = i + 1;
1792 peer_info_add_path (aux, copy, GNUNET_NO);
1798 * Send keepalive packets for a peer
1800 * @param cls Closure (tunnel for which to send the keepalive).
1801 * @param tc Notification context.
1803 * TODO: implement explicit multicast keepalive?
1806 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1810 * Search for a tunnel among the incoming tunnels
1812 * @param tid the local id of the tunnel
1814 * @return tunnel handler, NULL if doesn't exist
1816 static struct MeshTunnel *
1817 tunnel_get_incoming (MESH_TunnelNumber tid)
1819 GNUNET_HashCode hash;
1821 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1822 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1823 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1828 * Search for a tunnel among the tunnels for a client
1830 * @param c the client whose tunnels to search in
1831 * @param tid the local id of the tunnel
1833 * @return tunnel handler, NULL if doesn't exist
1835 static struct MeshTunnel *
1836 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1838 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1840 return tunnel_get_incoming (tid);
1844 GNUNET_HashCode hash;
1846 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1847 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1853 * Search for a tunnel by global ID using PEER_ID
1855 * @param pi owner of the tunnel
1856 * @param tid global tunnel number
1858 * @return tunnel handler, NULL if doesn't exist
1860 static struct MeshTunnel *
1861 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1863 struct MESH_TunnelID id;
1864 GNUNET_HashCode hash;
1869 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1870 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1875 * Search for a tunnel by global ID using full PeerIdentities
1877 * @param oid owner of the tunnel
1878 * @param tid global tunnel number
1880 * @return tunnel handler, NULL if doesn't exist
1882 static struct MeshTunnel *
1883 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1885 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1890 * Callback used to notify a client owner of a tunnel that a peer has
1891 * disconnected, most likely because of a path change.
1893 * @param cls Closure (tunnel this notification is about).
1894 * @param peer_id Short ID of disconnected peer.
1897 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
1899 struct MeshTunnel *t = cls;
1900 struct MeshPeerInfo *peer;
1901 struct MeshPathInfo *path_info;
1903 if (NULL != t->client && NULL != nc)
1905 struct GNUNET_MESH_PeerControl msg;
1907 msg.header.size = htons (sizeof (msg));
1908 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1909 msg.tunnel_id = htonl (t->local_tid);
1910 GNUNET_PEER_resolve (peer_id, &msg.peer);
1911 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1912 &msg.header, GNUNET_NO);
1914 peer = peer_info_get_short (peer_id);
1915 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1916 path_info->peer = peer;
1918 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
1923 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1924 * needed rescources.
1925 * If peer already exists, reevaluate shortest path and change if different.
1927 * @param t Tunnel we want to add a new peer to
1928 * @param peer PeerInfo of the peer being added
1932 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1934 struct GNUNET_PeerIdentity id;
1935 struct MeshPeerPath *best_p;
1936 struct MeshPeerPath *p;
1937 unsigned int best_cost;
1940 GNUNET_PEER_resolve (peer->id, &id);
1942 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
1945 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1946 GNUNET_assert (GNUNET_OK ==
1947 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
1949 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1952 if (NULL != (p = peer->path_head))
1955 best_cost = tree_get_path_cost (t->tree, p);
1958 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
1965 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
1966 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1967 t->path_refresh_task =
1968 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
1972 /* Start a DHT get */
1973 peer_info_connect (peer, t);
1978 * Add a path to a tunnel which we don't own, just to remember the next hop.
1979 * If destination node was already in the tunnel, the first hop information
1980 * will be replaced with the new path.
1982 * @param t Tunnel we want to add a new peer to
1983 * @param p Path to add
1984 * @param own_pos Position of local node in path.
1988 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
1989 unsigned int own_pos)
1991 struct GNUNET_PeerIdentity id;
1993 GNUNET_assert (0 != own_pos);
1994 tree_add_path (t->tree, p, NULL, NULL);
1995 if (own_pos < p->length - 1)
1997 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1998 tree_update_first_hops (t->tree, myid, &id);
2004 * Notifies a tunnel that a connection has broken that affects at least
2005 * some of its peers. Sends a notification towards the root of the tree.
2006 * In case the peer is the owner of the tree, notifies the client that owns
2007 * the tunnel and tries to reconnect.
2009 * @param t Tunnel affected.
2010 * @param p1 Peer that got disconnected from p2.
2011 * @param p2 Peer that got disconnected from p1.
2013 * @return Short ID of the peer disconnected (either p1 or p2).
2014 * 0 if the tunnel remained unaffected.
2016 static GNUNET_PEER_Id
2017 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2023 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2025 if (myid != p1 && myid != p2)
2031 if (tree_get_predecessor (t->tree) != 0)
2033 /* We are the peer still connected, notify owner of the disconnection. */
2034 struct GNUNET_MESH_PathBroken msg;
2035 struct GNUNET_PeerIdentity neighbor;
2037 msg.header.size = htons (sizeof (msg));
2038 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2039 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2040 msg.tid = htonl (t->id.tid);
2041 msg.peer1 = my_full_id;
2042 GNUNET_PEER_resolve (pid, &msg.peer2);
2043 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2044 send_message (&msg.header, &neighbor);
2052 * Send a multicast packet to a neighbor.
2054 * @param cls Closure (Info about the multicast packet)
2055 * @param neighbor_id Short ID of the neighbor to send the packet to.
2058 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2060 struct MeshData *mdata = cls;
2061 struct MeshTransmissionDescriptor *info;
2062 struct GNUNET_PeerIdentity neighbor;
2065 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2067 info->mesh_data = mdata;
2068 (*(mdata->reference_counter)) ++;
2069 info->destination = neighbor_id;
2070 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending to %s...\n",
2073 GNUNET_i2s (&neighbor));
2075 info->peer = peer_info_get (&neighbor);
2076 GNUNET_assert (NULL != info->peer);
2077 i = peer_info_transmit_slot (info->peer);
2078 info->handler_n = i;
2079 info->peer->infos[i] = info;
2080 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2081 info->peer->core_transmit[i] =
2082 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2083 GNUNET_TIME_UNIT_FOREVER_REL,
2084 &neighbor, info->mesh_data->data_len,
2085 &send_core_data_multicast, info);
2089 * Send a message in a tunnel in multicast, sending a copy to each child node
2090 * down the local one in the tunnel tree.
2092 * @param t Tunnel in which to send the data.
2093 * @param msg Message to be sent
2096 tunnel_send_multicast (struct MeshTunnel *t,
2097 const struct GNUNET_MessageHeader *msg)
2099 struct MeshData *mdata;
2102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2103 "MESH: sending a multicast packet...\n");
2105 mdata = GNUNET_malloc (sizeof (struct MeshData));
2106 mdata->data_len = ntohs (msg->size);
2107 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2109 mdata->data = GNUNET_malloc (mdata->data_len);
2110 memcpy (mdata->data, msg, mdata->data_len);
2111 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2113 struct GNUNET_MESH_Multicast *mcast;
2115 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2116 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: data packet, ttl: %u\n",
2119 ntohl (mcast->ttl));
2123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: not a data packet, no ttl\n");
2126 if (NULL != t->client)
2128 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2130 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: timeout task %u\n",
2136 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2137 if (*(mdata->reference_counter) == 0)
2139 GNUNET_free (mdata->data);
2140 GNUNET_free (mdata->reference_counter);
2141 if (NULL != mdata->task)
2143 GNUNET_SCHEDULER_cancel(*(mdata->task));
2144 GNUNET_free (mdata->task);
2145 GNUNET_SERVER_receive_done(t->client->handle, GNUNET_OK);
2147 // FIXME change order?
2148 GNUNET_free (mdata);
2151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2152 "MESH: sending a multicast packet done\n");
2159 * Send a message to all peers in this tunnel that the tunnel is no longer
2162 * @param t The tunnel whose peers to notify.
2165 tunnel_send_destroy (struct MeshTunnel *t)
2167 struct GNUNET_MESH_TunnelDestroy msg;
2169 msg.header.size = htons (sizeof (msg));
2170 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2171 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2172 msg.tid = htonl (t->id.tid);
2173 tunnel_send_multicast (t, &msg.header);
2179 * Destroy the tunnel and free any allocated resources linked to it
2181 * @param t the tunnel to destroy
2183 * @return GNUNET_OK on success
2186 tunnel_destroy (struct MeshTunnel *t)
2188 struct MeshClient *c;
2189 struct MeshQueue *q;
2190 struct MeshQueue *qn;
2191 GNUNET_HashCode hash;
2200 struct GNUNET_PeerIdentity id;
2202 GNUNET_PEER_resolve (t->id.oid, &id);
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel %s [%x]\n",
2204 GNUNET_i2s (&id), t->id.tid);
2206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2210 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2211 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2216 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2218 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
2222 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2224 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2225 GNUNET_break (GNUNET_YES ==
2226 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash,
2229 if (NULL != t->peers)
2231 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2233 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2238 if (NULL != q->data)
2239 GNUNET_free (q->data);
2243 /* TODO cancel core transmit ready in case it was active */
2245 tree_destroy (t->tree);
2246 if (NULL != t->dht_get_type)
2247 GNUNET_DHT_get_stop (t->dht_get_type);
2248 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2249 GNUNET_SCHEDULER_cancel (t->timeout_task);
2250 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2251 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2258 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2259 * that are no longer needed, as well as nodes of no longer reachable peers.
2260 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2262 * @param t Tunnel from which to remove the path.
2263 * @param peer Short id of the peer which should be removed.
2266 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2268 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2274 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2275 * client when the client disconnects.
2277 * @param cls closure (client that is disconnecting)
2278 * @param key the hash of the local tunnel id (used to access the hashmap)
2279 * @param value the value stored at the key (tunnel to destroy)
2281 * @return GNUNET_OK on success
2284 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2286 struct MeshTunnel *t = value;
2287 struct MeshClient *c = cls;
2290 send_client_tunnel_disconnect(t, c);
2291 if (c == t->client_dest)
2293 t->client_dest = NULL;
2294 t->local_tid_dest = 0;
2297 tunnel_send_destroy(t);
2298 r = tunnel_destroy (t);
2304 * Timeout function, destroys tunnel if called
2306 * @param cls Closure (tunnel to destroy).
2307 * @param tc TaskContext
2310 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2312 struct MeshTunnel *t = cls;
2314 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2316 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2321 * Resets the tunnel timeout. Starts it if no timeout was running.
2323 * @param t Tunnel whose timeout to reset.
2326 tunnel_reset_timeout (struct MeshTunnel *t)
2328 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2329 GNUNET_SCHEDULER_cancel (t->timeout_task);
2331 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2332 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2336 /******************************************************************************/
2337 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2338 /******************************************************************************/
2341 * Function called to notify a client about the socket
2342 * being ready to queue more data. "buf" will be
2343 * NULL and "size" zero if the socket was closed for
2344 * writing in the meantime.
2346 * @param cls closure
2347 * @param size number of bytes available in buf
2348 * @param buf where the callee should write the message
2349 * @return number of bytes written to buf
2352 send_core_create_path (void *cls, size_t size, void *buf)
2354 struct MeshPathInfo *info = cls;
2355 struct GNUNET_MESH_ManipulatePath *msg;
2356 struct GNUNET_PeerIdentity *peer_ptr;
2357 struct MeshPeerInfo *peer = info->peer;
2358 struct MeshTunnel *t = info->t;
2359 struct MeshPeerPath *p = info->path;
2363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATE PATH sending...\n");
2365 sizeof (struct GNUNET_MESH_ManipulatePath) +
2366 p->length * sizeof (struct GNUNET_PeerIdentity);
2368 if (size < size_needed || NULL == buf)
2370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n", size,
2374 info->peer->core_transmit[info->pos] =
2375 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2376 GNUNET_TIME_UNIT_FOREVER_REL,
2377 tree_get_first_hop (t->tree,
2379 size_needed, &send_core_create_path,
2383 info->peer->core_transmit[info->pos] = NULL;
2386 struct GNUNET_PeerIdentity id;
2388 GNUNET_PEER_resolve (peer->id, &id);
2389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2390 "MESH: setting core_transmit %s [%u] to NULL\n",
2391 GNUNET_i2s (&id), info->pos);
2394 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2395 msg->header.size = htons (size_needed);
2396 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2397 msg->tid = ntohl (t->id.tid);
2399 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2400 for (i = 0; i < p->length; i++)
2402 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2409 "MESH: CREATE PATH (%u bytes long) sent!\n", size_needed);
2415 * Function called to notify a client about the socket
2416 * being ready to queue more data. "buf" will be
2417 * NULL and "size" zero if the socket was closed for
2418 * writing in the meantime.
2420 * @param cls closure (data itself)
2421 * @param size number of bytes available in buf
2422 * @param buf where the callee should write the message
2424 * @return number of bytes written to buf
2427 send_core_data_multicast (void *cls, size_t size, void *buf)
2429 struct MeshTransmissionDescriptor *info = cls;
2432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2433 GNUNET_assert (NULL != info);
2434 GNUNET_assert (NULL != info->peer);
2435 total_size = info->mesh_data->data_len;
2436 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2438 if (total_size > size)
2441 struct GNUNET_PeerIdentity id;
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2444 "MESH: Multicast: retransmitting... (%u/%u)\n", size,
2446 GNUNET_PEER_resolve (info->peer->id, &id);
2447 info->peer->core_transmit[info->handler_n] =
2448 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2449 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2451 &send_core_data_multicast, info);
2454 info->peer->core_transmit[info->handler_n] = NULL;
2455 info->peer->infos[info->handler_n] = NULL;
2456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2457 memcpy (buf, info->mesh_data->data, total_size);
2460 struct GNUNET_MESH_Multicast *mc;
2461 struct GNUNET_MessageHeader *mh;
2464 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2466 mc = (struct GNUNET_MESH_Multicast *) mh;
2467 mh = (struct GNUNET_MessageHeader *) &mc[1];
2468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2469 "MESH: multicast, payload type %u\n", ntohs (mh->type));
2470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2471 "MESH: multicast, payload size %u\n", ntohs (mh->size));
2475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type %u\n",
2480 data_descriptor_decrement_multicast (info->mesh_data);
2481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2489 * Function called to notify a client about the socket
2490 * being ready to queue more data. "buf" will be
2491 * NULL and "size" zero if the socket was closed for
2492 * writing in the meantime.
2494 * @param cls closure (MeshTransmissionDescriptor)
2495 * @param size number of bytes available in buf
2496 * @param buf where the callee should write the message
2497 * @return number of bytes written to buf
2500 send_core_path_ack (void *cls, size_t size, void *buf)
2502 struct MeshTransmissionDescriptor *info = cls;
2503 struct GNUNET_MESH_PathACK *msg = buf;
2505 GNUNET_assert (NULL != info);
2508 info->peer->core_transmit[info->handler_n] = NULL;
2510 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2515 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2516 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2517 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2518 msg->tid = htonl (info->origin->tid);
2519 msg->peer_id = my_full_id;
2521 /* TODO add signature */
2523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2524 return sizeof (struct GNUNET_MESH_PathACK);
2528 /******************************************************************************/
2529 /******************** MESH NETWORK HANDLERS **************************/
2530 /******************************************************************************/
2534 * Core handler for path creation
2536 * @param cls closure
2537 * @param message message
2538 * @param peer peer identity this notification is about
2539 * @param atsi performance data
2540 * @param atsi_count number of records in 'atsi'
2542 * @return GNUNET_OK to keep the connection open,
2543 * GNUNET_SYSERR to close it (signal serious error)
2546 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2547 const struct GNUNET_MessageHeader *message,
2548 const struct GNUNET_ATS_Information *atsi,
2549 unsigned int atsi_count)
2551 unsigned int own_pos;
2554 MESH_TunnelNumber tid;
2555 struct GNUNET_MESH_ManipulatePath *msg;
2556 struct GNUNET_PeerIdentity *pi;
2557 GNUNET_HashCode hash;
2558 struct MeshPeerPath *path;
2559 struct MeshPeerInfo *dest_peer_info;
2560 struct MeshPeerInfo *orig_peer_info;
2561 struct MeshTunnel *t;
2563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2564 "MESH: Received a path create msg [%s]\n",
2565 GNUNET_i2s (&my_full_id));
2566 size = ntohs (message->size);
2567 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2569 GNUNET_break_op (0);
2573 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2574 if (size % sizeof (struct GNUNET_PeerIdentity))
2576 GNUNET_break_op (0);
2579 size /= sizeof (struct GNUNET_PeerIdentity);
2582 GNUNET_break_op (0);
2585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
2586 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2588 tid = ntohl (msg->tid);
2589 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2591 "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2592 t = tunnel_get (pi, tid);
2595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2596 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2597 t->id.oid = GNUNET_PEER_intern (pi);
2599 while (NULL != tunnel_get_incoming (next_local_tid))
2600 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2601 t->local_tid = next_local_tid++;
2602 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2603 t->tree = tree_new (t->id.oid);
2605 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2607 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2608 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2614 tunnel_reset_timeout (t);
2615 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2617 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2618 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2626 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2627 if (NULL == dest_peer_info)
2629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2630 "MESH: Creating PeerInfo for destination.\n");
2631 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2632 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2633 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2635 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2637 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2638 if (NULL == orig_peer_info)
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2641 "MESH: Creating PeerInfo for origin.\n");
2642 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2643 orig_peer_info->id = GNUNET_PEER_intern (pi);
2644 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2645 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2648 path = path_new (size);
2650 for (i = 0; i < size; i++)
2652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
2653 GNUNET_i2s (&pi[i]));
2654 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2655 if (path->peers[i] == myid)
2658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
2661 /* cannot be self, must be 'not found' */
2662 /* create path: self not found in path through self */
2663 GNUNET_break_op (0);
2664 path_destroy (path);
2665 /* FIXME error. destroy tunnel? leave for timeout? */
2668 path_add_to_peers (path, GNUNET_NO);
2669 tunnel_add_path (t, path, own_pos);
2670 if (own_pos == size - 1)
2672 /* It is for us! Send ack. */
2673 struct MeshTransmissionDescriptor *info;
2676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
2677 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2678 if (NULL == t->peers)
2680 /* New tunnel! Notify clients! */
2681 struct GNUNET_MESH_TunnelNotification cmsg;
2683 cmsg.header.size = htons (sizeof (cmsg));
2684 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2685 GNUNET_PEER_resolve (t->id.oid, &cmsg.peer);
2686 cmsg.tunnel_id = htonl (t->local_tid);
2687 GNUNET_SERVER_notification_context_broadcast (nc, &cmsg.header,
2690 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2692 GNUNET_break (GNUNET_OK ==
2693 GNUNET_CONTAINER_multihashmap_put (t->peers,
2694 &my_full_id.hashPubKey,
2697 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
2698 /* FIXME use send_message */
2699 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2700 info->origin = &t->id;
2701 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2702 GNUNET_assert (NULL != info->peer);
2703 j = peer_info_transmit_slot (info->peer);
2704 info->handler_n = j;
2705 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2706 info->peer->infos[j] = info;
2707 info->peer->core_transmit[j] =
2708 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2709 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2710 sizeof (struct GNUNET_MESH_PathACK),
2711 &send_core_path_ack, info);
2715 struct MeshPeerPath *path2;
2717 /* It's for somebody else! Retransmit. */
2718 path2 = path_duplicate (path);
2719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting.\n");
2720 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
2721 path2 = path_duplicate (path);
2722 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
2723 send_create_path (dest_peer_info, path, t);
2730 * Core handler for path destruction
2732 * @param cls closure
2733 * @param message message
2734 * @param peer peer identity this notification is about
2735 * @param atsi performance data
2736 * @param atsi_count number of records in 'atsi'
2738 * @return GNUNET_OK to keep the connection open,
2739 * GNUNET_SYSERR to close it (signal serious error)
2742 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2743 const struct GNUNET_MessageHeader *message,
2744 const struct GNUNET_ATS_Information *atsi,
2745 unsigned int atsi_count)
2747 struct GNUNET_MESH_ManipulatePath *msg;
2748 struct GNUNET_PeerIdentity *pi;
2749 struct MeshPeerPath *path;
2750 struct MeshTunnel *t;
2751 unsigned int own_pos;
2755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2756 "MESH: Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
2757 size = ntohs (message->size);
2758 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2760 GNUNET_break_op (0);
2764 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2765 if (size % sizeof (struct GNUNET_PeerIdentity))
2767 GNUNET_break_op (0);
2770 size /= sizeof (struct GNUNET_PeerIdentity);
2773 GNUNET_break_op (0);
2776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
2778 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2779 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2781 "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
2783 t = tunnel_get (pi, ntohl (msg->tid));
2786 /* TODO notify back: we don't know this tunnel */
2787 GNUNET_break_op (0);
2790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2791 path = path_new (size);
2793 for (i = 0; i < size; i++)
2795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
2796 GNUNET_i2s (&pi[i]));
2797 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2798 if (path->peers[i] == myid)
2801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
2802 if (own_pos < path->length - 1)
2803 send_message (message, &pi[own_pos + 1]);
2804 tunnel_delete_peer (t, path->peers[path->length - 1]);
2805 path_destroy (path);
2811 * Core handler for notifications of broken paths
2813 * @param cls closure
2814 * @param message message
2815 * @param peer peer identity this notification is about
2816 * @param atsi performance data
2817 * @param atsi_count number of records in 'atsi'
2819 * @return GNUNET_OK to keep the connection open,
2820 * GNUNET_SYSERR to close it (signal serious error)
2823 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
2824 const struct GNUNET_MessageHeader *message,
2825 const struct GNUNET_ATS_Information *atsi,
2826 unsigned int atsi_count)
2828 struct GNUNET_MESH_PathBroken *msg;
2829 struct MeshTunnel *t;
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2832 "MESH: Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
2833 msg = (struct GNUNET_MESH_PathBroken *) message;
2834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
2835 GNUNET_i2s (&msg->peer1));
2836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
2837 GNUNET_i2s (&msg->peer2));
2838 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2841 GNUNET_break_op (0);
2844 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
2845 GNUNET_PEER_search (&msg->peer2));
2852 * Core handler for tunnel destruction
2854 * @param cls closure
2855 * @param message message
2856 * @param peer peer identity this notification is about
2857 * @param atsi performance data
2858 * @param atsi_count number of records in 'atsi'
2860 * @return GNUNET_OK to keep the connection open,
2861 * GNUNET_SYSERR to close it (signal serious error)
2864 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2865 const struct GNUNET_MessageHeader *message,
2866 const struct GNUNET_ATS_Information *atsi,
2867 unsigned int atsi_count)
2869 struct GNUNET_MESH_TunnelDestroy *msg;
2870 struct MeshTunnel *t;
2872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2873 "MESH: Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
2874 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for tunnel %s [%u]\n",
2876 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
2877 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2880 /* Probably already got the message from another path,
2881 * destroyed the tunnel and retransmitted to children.
2886 if (t->id.oid == myid)
2888 GNUNET_break_op (0);
2891 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2893 /* Tunnel was incoming, notify clients */
2894 send_clients_tunnel_destroy (t);
2896 tunnel_send_destroy (t);
2903 * Core handler for mesh network traffic going from the origin to a peer
2905 * @param cls closure
2906 * @param peer peer identity this notification is about
2907 * @param message message
2908 * @param atsi performance data
2909 * @param atsi_count number of records in 'atsi'
2910 * @return GNUNET_OK to keep the connection open,
2911 * GNUNET_SYSERR to close it (signal serious error)
2914 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2915 const struct GNUNET_MessageHeader *message,
2916 const struct GNUNET_ATS_Information *atsi,
2917 unsigned int atsi_count)
2919 struct GNUNET_MESH_Unicast *msg;
2920 struct MeshTunnel *t;
2924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a unicast packet from %s\n",
2926 size = ntohs (message->size);
2928 sizeof (struct GNUNET_MESH_Unicast) +
2929 sizeof (struct GNUNET_MessageHeader))
2934 msg = (struct GNUNET_MESH_Unicast *) message;
2935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: of type %u\n",
2936 ntohs (msg[1].header.type));
2937 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2940 /* TODO notify back: we don't know this tunnel */
2941 GNUNET_break_op (0);
2944 tunnel_reset_timeout (t);
2945 pid = GNUNET_PEER_search (&msg->destination);
2948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2949 "MESH: it's for us! sending to clients...\n");
2950 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2954 "MESH: not for us, retransmitting...\n");
2955 send_message (message, tree_get_first_hop (t->tree, pid));
2961 * Core handler for mesh network traffic going from the origin to all peers
2963 * @param cls closure
2964 * @param message message
2965 * @param peer peer identity this notification is about
2966 * @param atsi performance data
2967 * @param atsi_count number of records in 'atsi'
2968 * @return GNUNET_OK to keep the connection open,
2969 * GNUNET_SYSERR to close it (signal serious error)
2971 * TODO: Check who we got this from, to validate route.
2974 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2975 const struct GNUNET_MessageHeader *message,
2976 const struct GNUNET_ATS_Information *atsi,
2977 unsigned int atsi_count)
2979 struct GNUNET_MESH_Multicast *msg;
2980 struct MeshTunnel *t;
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a multicast packet from %s\n",
2985 size = ntohs (message->size);
2986 if (sizeof (struct GNUNET_MESH_Multicast) +
2987 sizeof (struct GNUNET_MessageHeader) > size)
2989 GNUNET_break_op (0);
2992 msg = (struct GNUNET_MESH_Multicast *) message;
2993 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2997 /* TODO notify that we dont know that tunnel */
2998 GNUNET_break_op (0);
3001 if (t->mid == ntohl (msg->mid))
3003 /* FIXME: already seen this packet, log dropping */
3004 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3005 "MESH: Already seen mid %u, DROPPING!\n", t->mid);
3011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3012 "MESH: mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3015 t->mid = ntohl (msg->mid);
3016 tunnel_reset_timeout (t);
3018 /* Transmit to locally interested clients */
3019 if (NULL != t->peers &&
3020 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3022 send_subscribed_clients (message, &msg[1].header);
3025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ttl: %u\n", ntohl (msg->ttl));
3027 if (ntohl (msg->ttl) == 0)
3029 /* FIXME: ttl is 0, log dropping */
3030 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "MESH: TTL is 0, DROPPING!\n");
3033 tunnel_send_multicast (t, message);
3039 * Core handler for mesh network traffic toward the owner of a tunnel
3041 * @param cls closure
3042 * @param message message
3043 * @param peer peer identity this notification is about
3044 * @param atsi performance data
3045 * @param atsi_count number of records in 'atsi'
3047 * @return GNUNET_OK to keep the connection open,
3048 * GNUNET_SYSERR to close it (signal serious error)
3051 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3052 const struct GNUNET_MessageHeader *message,
3053 const struct GNUNET_ATS_Information *atsi,
3054 unsigned int atsi_count)
3056 struct GNUNET_MESH_ToOrigin *msg;
3057 struct GNUNET_PeerIdentity id;
3058 struct MeshPeerInfo *peer_info;
3059 struct MeshTunnel *t;
3062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a ToOrigin packet from %s\n",
3064 size = ntohs (message->size);
3065 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3066 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3068 GNUNET_break_op (0);
3071 msg = (struct GNUNET_MESH_ToOrigin *) message;
3072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: of type %u\n",
3073 ntohs (msg[1].header.type));
3074 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3078 /* TODO notify that we dont know this tunnel (whom)? */
3079 GNUNET_break_op (0);
3083 if (t->id.oid == myid)
3086 struct GNUNET_MESH_ToOrigin *copy;
3088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3089 "MESH: it's for us! sending to clients...\n");
3090 if (NULL == t->client)
3092 /* got data packet for ownerless tunnel */
3093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: no clients!\n");
3094 GNUNET_break_op (0);
3097 /* TODO signature verification */
3098 memcpy (cbuf, message, size);
3099 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3100 copy->tid = htonl (t->local_tid);
3101 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
3102 ©->header, GNUNET_YES);
3105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3106 "MESH: not for us, retransmitting...\n");
3108 peer_info = peer_info_get (&msg->oid);
3109 if (NULL == peer_info)
3111 /* unknown origin of tunnel */
3115 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3116 send_message (message, &id);
3123 * Core handler for path ACKs
3125 * @param cls closure
3126 * @param message message
3127 * @param peer peer identity this notification is about
3128 * @param atsi performance data
3129 * @param atsi_count number of records in 'atsi'
3131 * @return GNUNET_OK to keep the connection open,
3132 * GNUNET_SYSERR to close it (signal serious error)
3135 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3136 const struct GNUNET_MessageHeader *message,
3137 const struct GNUNET_ATS_Information *atsi,
3138 unsigned int atsi_count)
3140 struct GNUNET_MESH_PathACK *msg;
3141 struct GNUNET_PeerIdentity id;
3142 struct MeshPeerInfo *peer_info;
3143 struct MeshPeerPath *p;
3144 struct MeshTunnel *t;
3146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Received a path ACK msg [%s]\n",
3147 GNUNET_i2s (&my_full_id));
3148 msg = (struct GNUNET_MESH_PathACK *) message;
3149 t = tunnel_get (&msg->oid, msg->tid);
3152 /* TODO notify that we don't know the tunnel */
3156 peer_info = peer_info_get (&msg->peer_id);
3158 /* Add paths to peers? */
3159 p = tree_get_path_to_peer (t->tree, peer_info->id);
3162 path_add_to_peers (p, GNUNET_YES);
3170 /* Message for us? */
3171 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
3174 if (NULL == t->client)
3176 GNUNET_break_op (0);
3179 if (NULL != t->dht_get_type)
3181 GNUNET_DHT_get_stop (t->dht_get_type);
3182 t->dht_get_type = NULL;
3184 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3186 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3187 send_client_peer_connected (t, peer_info->id);
3192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3193 "MESH: not for us, retransmitting...\n");
3194 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3195 peer_info = peer_info_get (&msg->oid);
3196 if (NULL == peer_info)
3198 /* If we know the tunnel, we should DEFINITELY know the peer */
3202 send_message (message, &id);
3208 * Functions to handle messages from core
3210 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3211 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3212 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3213 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3214 sizeof (struct GNUNET_MESH_PathBroken)},
3215 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3216 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3217 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3218 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3219 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3220 sizeof (struct GNUNET_MESH_PathACK)},
3226 /******************************************************************************/
3227 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3228 /******************************************************************************/
3231 * deregister_app: iterator for removing each application registered by a client
3233 * @param cls closure
3234 * @param key the hash of the application id (used to access the hashmap)
3235 * @param value the value stored at the key (client)
3237 * @return GNUNET_OK on success
3240 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3242 GNUNET_break (GNUNET_YES ==
3243 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3250 * notify_client_connection_failure: notify a client that the connection to the
3251 * requested remote peer is not possible (for instance, no route found)
3252 * Function called when the socket is ready to queue more data. "buf" will be
3253 * NULL and "size" zero if the socket was closed for writing in the meantime.
3255 * @param cls closure
3256 * @param size number of bytes available in buf
3257 * @param buf where the callee should write the message
3258 * @return number of bytes written to buf
3261 notify_client_connection_failure (void *cls, size_t size, void *buf)
3264 struct MeshPeerInfo *peer_info;
3265 struct GNUNET_MESH_PeerControl *msg;
3266 struct GNUNET_PeerIdentity id;
3268 if (0 == size && NULL == buf)
3270 // TODO retry? cancel?
3274 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3275 peer_info = (struct MeshPeerInfo *) cls;
3276 msg = (struct GNUNET_MESH_PeerControl *) buf;
3277 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3278 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3279 // msg->tunnel_id = htonl(peer_info->t->tid);
3280 GNUNET_PEER_resolve (peer_info->id, &id);
3281 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3289 * Send keepalive packets for a peer
3291 * @param cls Closure (tunnel for which to send the keepalive).
3292 * @param tc Notification context.
3294 * TODO: implement explicit multicast keepalive?
3297 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3299 struct MeshTunnel *t = cls;
3300 struct GNUNET_MessageHeader *payload;
3301 struct GNUNET_MESH_Multicast *msg;
3303 sizeof (struct GNUNET_MESH_Multicast) +
3304 sizeof (struct GNUNET_MessageHeader);
3307 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3311 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3314 "MESH: sending keepalive for tunnel %d\n", t->id.tid);
3316 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3317 msg->header.size = htons (size);
3318 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3319 msg->oid = my_full_id;
3320 msg->tid = htonl (t->id.tid);
3321 payload = (struct GNUNET_MessageHeader *) &msg[1];
3322 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3323 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3324 tunnel_send_multicast (t, &msg->header);
3326 t->path_refresh_task =
3327 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3333 * Function to process paths received for a new peer addition. The recorded
3334 * paths form the initial tunnel, which can be optimized later.
3335 * Called on each result obtained for the DHT search.
3337 * @param cls closure
3338 * @param exp when will this value expire
3339 * @param key key of the result
3340 * @param get_path path of the get request
3341 * @param get_path_length lenght of get_path
3342 * @param put_path path of the put request
3343 * @param put_path_length length of the put_path
3344 * @param type type of the result
3345 * @param size number of bytes in data
3346 * @param data pointer to the result data
3348 * TODO: re-issue the request after certain time? cancel after X results?
3351 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3352 const GNUNET_HashCode * key,
3353 const struct GNUNET_PeerIdentity *get_path,
3354 unsigned int get_path_length,
3355 const struct GNUNET_PeerIdentity *put_path,
3356 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3357 size_t size, const void *data)
3359 struct MeshPathInfo *path_info = cls;
3360 struct MeshPeerPath *p;
3361 struct GNUNET_PeerIdentity pi;
3364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got results from DHT!\n");
3365 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n", GNUNET_i2s (&pi));
3368 p = path_build_from_dht (get_path, get_path_length, put_path,
3370 path_add_to_peers (p, GNUNET_NO);
3372 for (i = 0; i < path_info->peer->ntunnels; i++)
3374 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3375 peer_info_connect (path_info->peer, path_info->t);
3383 * Function to process paths received for a new peer addition. The recorded
3384 * paths form the initial tunnel, which can be optimized later.
3385 * Called on each result obtained for the DHT search.
3387 * @param cls closure
3388 * @param exp when will this value expire
3389 * @param key key of the result
3390 * @param get_path path of the get request
3391 * @param get_path_length lenght of get_path
3392 * @param put_path path of the put request
3393 * @param put_path_length length of the put_path
3394 * @param type type of the result
3395 * @param size number of bytes in data
3396 * @param data pointer to the result data
3399 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3400 const GNUNET_HashCode * key,
3401 const struct GNUNET_PeerIdentity *get_path,
3402 unsigned int get_path_length,
3403 const struct GNUNET_PeerIdentity *put_path,
3404 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3405 size_t size, const void *data)
3407 const struct GNUNET_PeerIdentity *pi = data;
3408 struct MeshTunnel *t = cls;
3409 struct MeshPeerInfo *peer_info;
3410 struct MeshPeerPath *p;
3412 if (size != sizeof (struct GNUNET_PeerIdentity))
3414 GNUNET_break_op (0);
3417 GNUNET_assert (NULL != t->client);
3418 peer_info = peer_info_get (pi);
3419 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3421 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3423 p = path_build_from_dht (get_path, get_path_length, put_path,
3425 path_add_to_peers (p, GNUNET_NO);
3427 tunnel_add_peer (t, peer_info);
3428 peer_info_connect (peer_info, t);
3432 /******************************************************************************/
3433 /********************* MESH LOCAL HANDLES **************************/
3434 /******************************************************************************/
3438 * Handler for client disconnection
3440 * @param cls closure
3441 * @param client identification of the client; NULL
3442 * for the last call when the server is destroyed
3445 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3447 struct MeshClient *c;
3448 struct MeshClient *next;
3450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3459 if (c->handle != client)
3461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found (%u)\n",
3469 GNUNET_SERVER_client_drop (c->handle);
3470 if (NULL != c->tunnels)
3472 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3473 &tunnel_destroy_iterator, c);
3474 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3477 /* deregister clients applications */
3478 if (NULL != c->apps)
3480 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3481 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3483 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3484 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3486 GNUNET_SCHEDULER_cancel (announce_applications_task);
3487 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3489 if (NULL != c->types)
3490 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3492 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT FREE at %p\n", c);
3498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3504 * Handler for new clients
3506 * @param cls closure
3507 * @param client identification of the client
3508 * @param message the actual message, which includes messages the client wants
3511 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3512 const struct GNUNET_MessageHeader *message)
3514 struct GNUNET_MESH_ClientConnect *cc_msg;
3515 struct MeshClient *c;
3516 GNUNET_MESH_ApplicationType *a;
3523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3524 /* Check data sanity */
3525 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3526 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3527 ntypes = ntohs (cc_msg->types);
3528 napps = ntohs (cc_msg->applications);
3530 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3533 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3537 /* Create new client structure */
3538 c = GNUNET_malloc (sizeof (struct MeshClient));
3540 c->id = next_client_id++;
3541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT NEW %u at %p\n", c->id,
3545 GNUNET_SERVER_client_keep (client);
3546 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3549 GNUNET_MESH_ApplicationType at;
3552 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3553 for (i = 0; i < napps; i++)
3556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3557 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3558 /* store in clients hashmap */
3559 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3560 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3561 /* store in global hashmap, for announcements */
3562 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3563 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3565 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3566 announce_applications_task =
3567 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3575 t = (uint16_t *) & a[napps];
3576 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3577 for (i = 0; i < ntypes; i++)
3580 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3582 /* store in clients hashmap */
3583 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3584 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3585 /* store in global hashmap */
3586 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3587 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3591 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3593 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3594 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3595 GNUNET_SERVER_notification_context_add (nc, client);
3597 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3605 * Handler for requests of new tunnels
3607 * @param cls closure
3608 * @param client identification of the client
3609 * @param message the actual message
3612 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3613 const struct GNUNET_MessageHeader *message)
3615 struct GNUNET_MESH_TunnelMessage *t_msg;
3616 struct MeshTunnel *t;
3617 struct MeshClient *c;
3618 GNUNET_HashCode hash;
3620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3622 /* Sanity check for client registration */
3623 if (NULL == (c = client_get (client)))
3626 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3633 /* Message sanity check */
3634 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3637 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3641 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3642 /* Sanity check for tunnel numbering */
3643 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3646 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3649 /* Sanity check for duplicate tunnel IDs */
3650 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3653 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3657 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3658 while (NULL != tunnel_get_by_pi (myid, next_tid))
3659 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3660 t->id.tid = next_tid++;
3661 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3663 t->local_tid = ntohl (t_msg->tunnel_id);
3665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL %s [%x] (%x)\n",
3666 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3669 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3671 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3673 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3674 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3677 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3681 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3683 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3687 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3690 t->tree = tree_new (myid);
3692 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3698 * Handler for requests of deleting tunnels
3700 * @param cls closure
3701 * @param client identification of the client
3702 * @param message the actual message
3705 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3706 const struct GNUNET_MessageHeader *message)
3708 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3709 struct MeshClient *c;
3710 struct MeshTunnel *t;
3711 MESH_TunnelNumber tid;
3712 GNUNET_HashCode hash;
3714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3715 "MESH: Got a DESTROY TUNNEL from client!\n");
3717 /* Sanity check for client registration */
3718 if (NULL == (c = client_get (client)))
3721 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3724 /* Message sanity check */
3725 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3728 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3734 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3736 /* Retrieve tunnel */
3737 tid = ntohl (tunnel_msg->tunnel_id);
3739 /* Remove from local id hashmap */
3740 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3741 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3742 send_client_tunnel_disconnect(t, c);
3743 GNUNET_assert (GNUNET_YES ==
3744 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t));
3747 tunnel_send_destroy (t);
3749 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3755 * Handler for connection requests to new peers
3757 * @param cls closure
3758 * @param client identification of the client
3759 * @param message the actual message (PeerControl)
3762 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3763 const struct GNUNET_MessageHeader *message)
3765 struct GNUNET_MESH_PeerControl *peer_msg;
3766 struct MeshPeerInfo *peer_info;
3767 struct MeshClient *c;
3768 struct MeshTunnel *t;
3769 MESH_TunnelNumber tid;
3771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3772 /* Sanity check for client registration */
3773 if (NULL == (c = client_get (client)))
3776 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3780 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3781 /* Sanity check for message size */
3782 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3789 /* Tunnel exists? */
3790 tid = ntohl (peer_msg->tunnel_id);
3791 t = tunnel_get_by_local_id (c, tid);
3795 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3799 /* Does client own tunnel? */
3800 if (t->client->handle != client)
3803 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3807 GNUNET_i2s (&peer_msg->peer));
3808 peer_info = peer_info_get (&peer_msg->peer);
3810 tunnel_add_peer (t, peer_info);
3811 peer_info_connect (peer_info, t);
3813 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3819 * Handler for disconnection requests of peers in a tunnel
3821 * @param cls closure
3822 * @param client identification of the client
3823 * @param message the actual message (PeerControl)
3826 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3827 const struct GNUNET_MessageHeader *message)
3829 struct GNUNET_MESH_PeerControl *peer_msg;
3830 struct MeshPeerInfo *peer_info;
3831 struct MeshClient *c;
3832 struct MeshTunnel *t;
3833 MESH_TunnelNumber tid;
3835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got a PEER DEL request\n");
3836 /* Sanity check for client registration */
3837 if (NULL == (c = client_get (client)))
3840 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3843 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3844 /* Sanity check for message size */
3845 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3852 /* Tunnel exists? */
3853 tid = ntohl (peer_msg->tunnel_id);
3854 t = tunnel_get_by_local_id (c, tid);
3858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: on tunnel %X\n", t->id.tid);
3863 /* Does client own tunnel? */
3864 if (t->client->handle != client)
3867 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for peer %s\n",
3872 GNUNET_i2s (&peer_msg->peer));
3873 /* Is the peer in the tunnel? */
3875 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
3876 if (NULL == peer_info)
3879 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3883 /* Ok, delete peer from tunnel */
3884 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3885 &peer_msg->peer.hashPubKey);
3887 send_destroy_path (t, peer_info->id);
3888 tunnel_delete_peer (t, peer_info->id);
3889 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3895 * Handler for connection requests to new peers by type
3897 * @param cls closure
3898 * @param client identification of the client
3899 * @param message the actual message (ConnectPeerByType)
3902 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3903 const struct GNUNET_MessageHeader *message)
3905 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3906 struct MeshClient *c;
3907 struct MeshTunnel *t;
3908 GNUNET_HashCode hash;
3909 MESH_TunnelNumber tid;
3911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3912 /* Sanity check for client registration */
3913 if (NULL == (c = client_get (client)))
3916 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3920 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3921 /* Sanity check for message size */
3922 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3923 ntohs (connect_msg->header.size))
3926 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3930 /* Tunnel exists? */
3931 tid = ntohl (connect_msg->tunnel_id);
3932 t = tunnel_get_by_local_id (c, tid);
3936 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3940 /* Does client own tunnel? */
3941 if (t->client->handle != client)
3944 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3948 /* Do WE have the service? */
3949 t->type = ntohl (connect_msg->type);
3950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3951 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3952 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3955 /* Yes! Fast forward, add ourselves to the tunnel and send the
3956 * good news to the client, and alert the destination client of
3957 * an incoming tunnel.
3959 struct GNUNET_MESH_TunnelNotification cmsg;
3960 struct MeshClient *c;
3962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3963 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3964 peer_info_get (&my_full_id),
3965 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3968 send_client_peer_connected (t, myid);
3969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3970 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3972 /* FIXME implement a proper handling of this case,
3973 a client differentiation mechanism */
3974 cmsg.header.size = htons (sizeof (cmsg));
3975 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
3976 cmsg.peer = my_full_id;
3977 t->local_tid_dest = next_local_tid++;
3978 cmsg.tunnel_id = htonl (t->local_tid_dest);
3979 c = (struct MeshClient *) GNUNET_CONTAINER_multihashmap_get(applications,
3982 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3983 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3984 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3985 GNUNET_SERVER_notification_context_unicast (nc, c->handle, &cmsg.header,
3991 /* Ok, lets find a peer offering the service */
3992 if (NULL != t->dht_get_type)
3994 GNUNET_DHT_get_stop (t->dht_get_type);
3996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3997 GNUNET_h2s (&hash));
3999 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
4000 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
4001 GNUNET_DHT_RO_RECORD_ROUTE |
4002 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
4003 &dht_get_type_handler, t);
4005 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4011 * Handler for client traffic directed to one peer
4013 * @param cls closure
4014 * @param client identification of the client
4015 * @param message the actual message
4018 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4019 const struct GNUNET_MessageHeader *message)
4021 struct MeshClient *c;
4022 struct MeshTunnel *t;
4023 struct MeshPeerInfo *pi;
4024 struct GNUNET_MESH_Unicast *data_msg;
4025 MESH_TunnelNumber tid;
4028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4029 "MESH: Got a unicast request from a client!\n");
4031 /* Sanity check for client registration */
4032 if (NULL == (c = client_get (client)))
4035 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4038 data_msg = (struct GNUNET_MESH_Unicast *) message;
4039 /* Sanity check for message size */
4040 size = ntohs (message->size);
4041 if (sizeof (struct GNUNET_MESH_Unicast) +
4042 sizeof (struct GNUNET_MessageHeader) > size)
4045 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4049 /* Tunnel exists? */
4050 tid = ntohl (data_msg->tid);
4051 t = tunnel_get_by_local_id (c, tid);
4055 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4059 /* Is it a local tunnel? Then, does client own the tunnel? */
4060 if (NULL != t->client && NULL != t->client->handle &&
4061 t->client->handle != client)
4064 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4068 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
4069 &data_msg->destination.hashPubKey);
4070 /* Is the selected peer in the tunnel? */
4074 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4078 /* Ok, everything is correct, send the message
4079 * (pretend we got it from a mesh peer)
4082 char buf[ntohs (message->size)];
4083 struct GNUNET_MESH_Unicast *copy;
4085 /* Work around const limitation */
4086 copy = (struct GNUNET_MESH_Unicast *) buf;
4087 memcpy (buf, data_msg, size);
4088 copy->oid = my_full_id;
4089 copy->tid = htonl (t->id.tid);
4090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4091 "MESH: calling generic handler...\n");
4092 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4094 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4100 * Handler for client traffic directed to the origin
4102 * @param cls closure
4103 * @param client identification of the client
4104 * @param message the actual message
4107 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4108 const struct GNUNET_MessageHeader *message)
4110 struct GNUNET_MESH_ToOrigin *data_msg;
4111 struct GNUNET_PeerIdentity id;
4112 struct MeshClient *c;
4113 struct MeshTunnel *t;
4114 MESH_TunnelNumber tid;
4117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4118 "MESH: Got a ToOrigin request from a client!\n");
4120 /* Sanity check for client registration */
4121 if (NULL == (c = client_get (client)))
4124 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4127 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4128 /* Sanity check for message size */
4129 size = ntohs (message->size);
4130 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4131 sizeof (struct GNUNET_MessageHeader) > size)
4134 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4138 /* Tunnel exists? */
4139 tid = ntohl (data_msg->tid);
4140 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4143 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4146 t = tunnel_get_by_local_id (c, tid);
4150 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4154 /* It shouldn't be a local tunnel. */
4155 if (NULL != t->client)
4158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4161 GNUNET_PEER_resolve (t->id.oid, &id);
4163 /* Ok, everything is correct, send the message
4164 * (pretend we got it from a mesh peer)
4167 char buf[ntohs (message->size)];
4168 struct GNUNET_MESH_ToOrigin *copy;
4170 /* Work around const limitation */
4171 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4172 memcpy (buf, data_msg, size);
4174 copy->tid = htonl (t->id.tid);
4175 copy->sender = my_full_id;
4176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4177 "MESH: calling generic handler...\n");
4178 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4180 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4186 * Handler for client traffic directed to all peers in a tunnel
4188 * @param cls closure
4189 * @param client identification of the client
4190 * @param message the actual message
4193 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4194 const struct GNUNET_MessageHeader *message)
4196 struct MeshClient *c;
4197 struct MeshTunnel *t;
4198 struct GNUNET_MESH_Multicast *data_msg;
4199 MESH_TunnelNumber tid;
4201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4202 "MESH: Got a multicast request from a client!\n");
4204 /* Sanity check for client registration */
4205 if (NULL == (c = client_get (client)))
4208 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4211 data_msg = (struct GNUNET_MESH_Multicast *) message;
4212 /* Sanity check for message size */
4213 if (sizeof (struct GNUNET_MESH_Multicast) +
4214 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4217 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4221 /* Tunnel exists? */
4222 tid = ntohl (data_msg->tid);
4223 t = tunnel_get_by_local_id (c, tid);
4227 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4231 /* Does client own tunnel? */
4232 if (t->client->handle != client)
4235 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4240 char buf[ntohs (message->size)];
4241 struct GNUNET_MESH_Multicast *copy;
4243 copy = (struct GNUNET_MESH_Multicast *) buf;
4244 memcpy (buf, message, ntohs (message->size));
4245 copy->oid = my_full_id;
4246 copy->tid = htonl (t->id.tid);
4247 copy->ttl = htonl (DEFAULT_TTL);
4248 copy->mid = htonl (t->mid + 1);
4249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4250 "MESH: calling generic handler...\n");
4251 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4254 /* receive done gets called when last copy is sent to a neighbor */
4259 * Functions to handle messages from clients
4261 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4262 {&handle_local_new_client, NULL,
4263 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4264 {&handle_local_tunnel_create, NULL,
4265 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4266 sizeof (struct GNUNET_MESH_TunnelMessage)},
4267 {&handle_local_tunnel_destroy, NULL,
4268 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4269 sizeof (struct GNUNET_MESH_TunnelMessage)},
4270 {&handle_local_connect_add, NULL,
4271 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4272 sizeof (struct GNUNET_MESH_PeerControl)},
4273 {&handle_local_connect_del, NULL,
4274 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4275 sizeof (struct GNUNET_MESH_PeerControl)},
4276 {&handle_local_connect_by_type, NULL,
4277 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4278 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4279 {&handle_local_unicast, NULL,
4280 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4281 {&handle_local_to_origin, NULL,
4282 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4283 {&handle_local_multicast, NULL,
4284 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4290 * To be called on core init/fail.
4292 * @param cls service closure
4293 * @param server handle to the server for this service
4294 * @param identity the public identity of this peer
4297 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4298 const struct GNUNET_PeerIdentity *identity)
4300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
4301 core_handle = server;
4302 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
4306 GNUNET_SCHEDULER_shutdown ();
4312 * Method called whenever a given peer connects.
4314 * @param cls closure
4315 * @param peer peer identity this notification is about
4316 * @param atsi performance data for the connection
4317 * @param atsi_count number of records in 'atsi'
4320 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4321 const struct GNUNET_ATS_Information *atsi,
4322 unsigned int atsi_count)
4324 struct MeshPeerInfo *peer_info;
4325 struct MeshPeerPath *path;
4327 #if MESH_DEBUG_CONNECTION
4328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
4329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4330 GNUNET_i2s (&my_full_id));
4332 peer_info = peer_info_get (peer);
4333 if (myid == peer_info->id)
4335 #if MESH_DEBUG_CONNECTION
4336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4340 #if MESH_DEBUG_CONNECTION
4343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", GNUNET_i2s (peer));
4346 path = path_new (2);
4347 path->peers[0] = myid;
4348 path->peers[1] = peer_info->id;
4349 GNUNET_PEER_change_rc (myid, 1);
4350 GNUNET_PEER_change_rc (peer_info->id, 1);
4351 peer_info_add_path (peer_info, path, GNUNET_YES);
4356 * Method called whenever a peer disconnects.
4358 * @param cls closure
4359 * @param peer peer identity this notification is about
4362 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4364 struct MeshPeerInfo *pi;
4367 #if MESH_DEBUG_CONNECTION
4368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
4370 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4376 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4378 /* TODO: notify that the transmission failed */
4379 peer_info_cancel_transmission (pi, i);
4381 peer_info_remove_path (pi, pi->id, myid);
4382 #if MESH_DEBUG_CONNECTION
4385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4392 /******************************************************************************/
4393 /************************ MAIN FUNCTIONS ****************************/
4394 /******************************************************************************/
4397 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4399 * @param cls closure
4400 * @param key current key code
4401 * @param value value in the hash map
4402 * @return GNUNET_YES if we should continue to iterate,
4406 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4408 struct MeshTunnel *t = value;
4415 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4417 * @param cls closure
4418 * @param key current key code
4419 * @param value value in the hash map
4420 * @return GNUNET_YES if we should continue to iterate,
4424 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4426 struct MeshPeerInfo *p = value;
4428 peer_info_destroy (p);
4433 * Task run during shutdown.
4439 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4443 if (core_handle != NULL)
4445 GNUNET_CORE_disconnect (core_handle);
4448 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4449 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4450 if (dht_handle != NULL)
4452 GNUNET_DHT_disconnect (dht_handle);
4457 GNUNET_SERVER_notification_context_destroy (nc);
4460 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4462 GNUNET_SCHEDULER_cancel (announce_id_task);
4463 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4469 * Process mesh requests.
4471 * @param cls closure
4472 * @param server the initialized server
4473 * @param c configuration to use
4476 run (void *cls, struct GNUNET_SERVER_Handle *server,
4477 const struct GNUNET_CONFIGURATION_Handle *c)
4479 struct MeshPeerInfo *peer;
4480 struct MeshPeerPath *p;
4483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4484 server_handle = server;
4485 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4486 CORE_QUEUE_SIZE, /* queue size */
4487 NULL, /* Closure passed to MESH functions */
4488 &core_init, /* Call core_init once connected */
4489 &core_connect, /* Handle connects */
4490 &core_disconnect, /* remove peers on disconnects */
4491 NULL, /* Don't notify about all incoming messages */
4492 GNUNET_NO, /* For header only in notification */
4493 NULL, /* Don't notify about all outbound messages */
4494 GNUNET_NO, /* For header-only out notification */
4495 core_handlers); /* Register these handlers */
4497 if (core_handle == NULL)
4500 GNUNET_SCHEDULER_shutdown ();
4505 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4508 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4510 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4511 GNUNET_SCHEDULER_shutdown ();
4514 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4515 GNUNET_free (keyfile);
4516 if (my_private_key == NULL)
4518 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4519 _("Mesh service could not access hostkey. Exiting.\n"));
4520 GNUNET_SCHEDULER_shutdown ();
4523 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4524 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4525 &my_full_id.hashPubKey);
4526 myid = GNUNET_PEER_intern (&my_full_id);
4528 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4535 dht_handle = GNUNET_DHT_connect (c, 64);
4536 if (dht_handle == NULL)
4542 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4544 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4545 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4546 peers = GNUNET_CONTAINER_multihashmap_create (32);
4547 applications = GNUNET_CONTAINER_multihashmap_create (32);
4548 types = GNUNET_CONTAINER_multihashmap_create (32);
4550 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4551 nc = GNUNET_SERVER_notification_context_create (server_handle,
4553 GNUNET_SERVER_disconnect_notify (server_handle,
4554 &handle_local_client_disconnect, NULL);
4558 clients_tail = NULL;
4563 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4564 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4566 /* Create a peer_info for the local peer */
4567 peer = peer_info_get (&my_full_id);
4570 GNUNET_PEER_change_rc (myid, 1);
4571 peer_info_add_path (peer, p, GNUNET_YES);
4573 /* Scheduled the task to clean up when shutdown is called */
4574 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4581 * The main function for the mesh service.
4583 * @param argc number of arguments from the command line
4584 * @param argv command line arguments
4585 * @return 0 ok, 1 on error
4588 main (int argc, char *const *argv)
4592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4595 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");