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;
415 * Whether the client is active or shutting down (don't send confirmations
416 * to a client that is shutting down.
422 * ID of the client, for debug messages
431 /******************************************************************************/
432 /************************ DEBUG FUNCTIONS ****************************/
433 /******************************************************************************/
437 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
438 * @param cls string to print
439 * @param tc task context
442 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
446 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
454 /******************************************************************************/
455 /*********************** GLOBAL VARIABLES ****************************/
456 /******************************************************************************/
461 static struct MeshClient *clients;
462 static struct MeshClient *clients_tail;
465 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
467 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
470 * Tunnels incoming, indexed by MESH_TunnelNumber
471 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
473 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
476 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
478 static struct GNUNET_CONTAINER_MultiHashMap *peers;
481 * Handle to communicate with core
483 static struct GNUNET_CORE_Handle *core_handle;
486 * Handle to communicate with transport
488 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
493 static struct GNUNET_DHT_Handle *dht_handle;
498 static struct GNUNET_SERVER_Handle *server_handle;
501 * Notification context, to send messages to local clients
503 static struct GNUNET_SERVER_NotificationContext *nc;
506 * Local peer own ID (memory efficient handle)
508 static GNUNET_PEER_Id myid;
511 * Local peer own ID (full value)
513 static struct GNUNET_PeerIdentity my_full_id;
518 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
523 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
526 * Tunnel ID for the next created tunnel (global tunnel number)
528 static MESH_TunnelNumber next_tid;
531 * Tunnel ID for the next incoming tunnel (local tunnel number)
533 static MESH_TunnelNumber next_local_tid;
536 * All application types provided by this peer
538 static struct GNUNET_CONTAINER_MultiHashMap *applications;
541 * All message types clients of this peer are interested in
543 static struct GNUNET_CONTAINER_MultiHashMap *types;
546 * Task to periodically announce provided applications
548 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
551 * Task to periodically announce itself in the network
553 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
556 unsigned int next_client_id;
560 /******************************************************************************/
561 /************************ ITERATORS ****************************/
562 /******************************************************************************/
564 /* FIXME move iterators here */
567 /******************************************************************************/
568 /************************ PERIODIC FUNCTIONS ****************************/
569 /******************************************************************************/
572 * Announce iterator over for each application provided by the peer
575 * @param key current key code
576 * @param value value in the hash map
577 * @return GNUNET_YES if we should continue to
582 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
584 /* FIXME are hashes in multihash map equal on all aquitectures? */
585 GNUNET_DHT_put (dht_handle, key, 10U,
586 GNUNET_DHT_RO_RECORD_ROUTE |
587 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
588 sizeof (struct GNUNET_PeerIdentity),
589 (const char *) &my_full_id,
591 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
592 &mesh_debug, "DHT_put for app completed");
594 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
596 APP_ANNOUNCE_TIME, NULL, NULL);
603 * Periodically announce what applications are provided by local clients
606 * @param tc task context
609 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
611 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
613 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
619 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
621 announce_applications_task =
622 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
632 * Periodically announce self id in the DHT
635 * @param tc task context
638 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
640 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
642 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
646 * - Set data expiration in function of X
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
651 GNUNET_i2s (&my_full_id));
653 GNUNET_DHT_put (dht_handle, /* DHT handle */
654 &my_full_id.hashPubKey, /* Key to use */
655 10U, /* Replication level */
656 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
657 GNUNET_BLOCK_TYPE_TEST, /* Block type */
658 sizeof (my_full_id), /* Size of the data */
659 (char *) &my_full_id, /* Data itself */
660 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
661 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
663 &mesh_debug, "DHT_put for id completed");
665 NULL, /* Continuation */
666 NULL); /* Continuation closure */
669 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
674 * Function to process paths received for a new peer addition. The recorded
675 * paths form the initial tunnel, which can be optimized later.
676 * Called on each result obtained for the DHT search.
679 * @param exp when will this value expire
680 * @param key key of the result
681 * @param type type of the result
682 * @param size number of bytes in data
683 * @param data pointer to the result data
686 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
687 const GNUNET_HashCode * key,
688 const struct GNUNET_PeerIdentity *get_path,
689 unsigned int get_path_length,
690 const struct GNUNET_PeerIdentity *put_path,
691 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
692 size_t size, const void *data);
695 /******************************************************************************/
696 /****************** GENERAL HELPER FUNCTIONS ************************/
697 /******************************************************************************/
700 * Check if client has registered with the service and has not disconnected
702 * @param client the client to check
704 * @return non-NULL if client exists in the global DLL
706 static struct MeshClient *
707 client_get (struct GNUNET_SERVER_Client *client)
709 struct MeshClient *c;
714 if (c->handle == client)
723 * Checks if a given client has subscribed to certain message type
725 * @param message_type Type of message to check
726 * @param c Client to check
728 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
733 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
737 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
738 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
743 * Allow a client to send more data after transmitting a multicast message
744 * which some neighbor has not yet accepted altough a reasonable time has
747 * @param cls Closure (DataDescriptor containing the task identifier)
748 * @param tc Task Context
751 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
753 struct MeshData *mdata = cls;
755 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
758 GNUNET_assert (NULL != mdata->reference_counter);
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "MESH: CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
761 mdata->reference_counter);
763 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
764 GNUNET_SERVER_receive_done (mdata->t->client->handle, GNUNET_OK);
769 * Search for a tunnel by global ID using full PeerIdentities
771 * @param oid owner of the tunnel
772 * @param tid global tunnel number
774 * @return tunnel handler, NULL if doesn't exist
776 static struct MeshTunnel *
777 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
781 * Notify a tunnel that a connection has broken that affects at least
784 * @param t Tunnel affected.
785 * @param p1 Peer that got disconnected from p2.
786 * @param p2 Peer that got disconnected from p1.
788 * @return Short ID of the peer disconnected (either p1 or p2).
789 * 0 if the tunnel remained unaffected.
791 static GNUNET_PEER_Id
792 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
797 * Send the message to all clients that have subscribed to its type
799 * @param msg Pointer to the message itself
800 * @param payload Pointer to the payload of the message.
801 * @return number of clients this message was sent to
804 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
805 const struct GNUNET_MessageHeader *payload)
807 struct GNUNET_PeerIdentity *oid;
808 struct MeshClient *c;
809 struct MeshTunnel *t;
810 MESH_TunnelNumber *tid;
813 char cbuf[htons (msg->size)];
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
817 type = ntohs (payload->type);
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
821 memcpy (cbuf, msg, sizeof (cbuf));
822 switch (htons (msg->type))
824 struct GNUNET_MESH_Unicast *uc;
825 struct GNUNET_MESH_Multicast *mc;
826 struct GNUNET_MESH_ToOrigin *to;
828 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
829 uc = (struct GNUNET_MESH_Unicast *) cbuf;
833 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
834 mc = (struct GNUNET_MESH_Multicast *) cbuf;
838 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
839 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
847 t = tunnel_get (oid, ntohl (*tid));
854 for (count = 0, c = clients; c != NULL; c = c->next)
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
859 if (client_is_subscribed (type, c))
861 // FIXME proper client differentiation mechanism required
862 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
863 *tid = htonl (t->local_tid);
864 else if(c == t->client)
865 *tid = htonl (t->local_tid);
866 else if(c == t->client_dest)
867 *tid = htonl (t->local_tid_dest);
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
875 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
876 (struct GNUNET_MessageHeader
877 *) cbuf, GNUNET_YES);
885 * Notify the client that owns the tunnel that a peer has connected to it
886 * (the requested path to it has been confirmed).
888 * @param t Tunnel whose owner to notify
889 * @param id Short id of the peer that has connected
892 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
894 struct GNUNET_MESH_PeerControl pc;
896 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
897 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
898 pc.tunnel_id = htonl (t->local_tid);
899 GNUNET_PEER_resolve (id, &pc.peer);
900 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, &pc.header,
906 * Notify all clients (not depending on registration status) that the incoming
907 * tunnel is no longer valid.
909 * @param t Tunnel that was destroyed.
912 send_clients_tunnel_destroy (struct MeshTunnel *t)
914 struct GNUNET_MESH_TunnelMessage msg;
916 msg.header.size = htons (sizeof (msg));
917 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
918 msg.tunnel_id = htonl (t->local_tid);
919 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
924 * Notify a client that the other local client disconnected, if needed.
925 * In case the origin disconnects, the destination get a tunnel destroy
926 * notification. Otherwise, the origin gets a (local ID) peer disconnected.
928 * @param t Tunnel that was destroyed.
929 * @param c Client that disconnected.
932 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
934 if (c == t->client_dest)
936 struct GNUNET_MESH_PeerControl msg;
938 msg.header.size = htons (sizeof (msg));
939 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
940 msg.tunnel_id = htonl (t->local_tid);
941 msg.peer = my_full_id;
942 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
943 &msg.header, GNUNET_NO);
945 else if (NULL != t->client_dest && c == t->client)
947 struct GNUNET_MESH_TunnelMessage msg;
949 msg.header.size = htons (sizeof (msg));
950 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
951 msg.tunnel_id = htonl (t->local_tid_dest);
952 GNUNET_SERVER_notification_context_unicast (nc, t->client_dest->handle,
953 &msg.header, GNUNET_NO);
959 * Function called to notify a client about the socket
960 * being ready to queue more data. "buf" will be
961 * NULL and "size" zero if the socket was closed for
962 * writing in the meantime.
965 * @param size number of bytes available in buf
966 * @param buf where the callee should write the message
967 * @return number of bytes written to buf
970 send_core_create_path (void *cls, size_t size, void *buf);
974 * Function called to notify a client about the socket
975 * being ready to queue more data. "buf" will be
976 * NULL and "size" zero if the socket was closed for
977 * writing in the meantime.
979 * @param cls closure (data itself)
980 * @param size number of bytes available in buf
981 * @param buf where the callee should write the message
983 * @return number of bytes written to buf
986 send_core_data_multicast (void *cls, size_t size, void *buf);
990 * Decrements the reference counter and frees all resources if needed
992 * @param dd Data Descriptor used in a multicast message. Freed if needed.
995 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
997 /* Make sure it's a multicast packet */
998 GNUNET_assert (NULL != mesh_data->reference_counter);
1000 if (0 == --(*(mesh_data->reference_counter)))
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
1003 if (NULL != mesh_data->task)
1005 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1007 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
1009 GNUNET_SERVER_receive_done (mesh_data->t->client->handle, GNUNET_OK);
1011 GNUNET_free (mesh_data->task);
1013 GNUNET_free (mesh_data->reference_counter);
1014 GNUNET_free (mesh_data->data);
1015 GNUNET_free (mesh_data);
1021 * Cancel a core transmission that was already requested and free all resources
1022 * associated to the request.
1024 * @param peer PeeInfo of the peer whose transmission is cancelled.
1025 * @param i Position of the transmission to be cancelled.
1028 peer_info_cancel_transmission (struct MeshPeerInfo *peer, unsigned int i)
1030 if (NULL != peer->core_transmit[i])
1032 struct MeshTransmissionDescriptor *dd;
1033 struct MeshPathInfo *path_info;
1037 struct GNUNET_PeerIdentity id;
1039 GNUNET_PEER_resolve (peer->id, &id);
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1041 "MESH: Cancelling data transmission at %s [%u]\n",
1042 GNUNET_i2s (&id), i);
1043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message type %u\n",
1047 /* TODO: notify that tranmission has failed */
1048 switch (peer->types[i])
1050 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1051 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1052 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
1054 dd = peer->infos[i];
1055 data_descriptor_decrement_multicast (dd->mesh_data);
1057 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
1059 path_info = peer->infos[i];
1060 path_destroy (path_info->path);
1064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
1066 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
1067 peer->core_transmit[i] = NULL;
1068 GNUNET_free (peer->infos[i]);
1074 * Get a unused CORE slot to transmit a message to a peer. If all the slots
1075 * are used, cancel one and return it's position.
1077 * @param peer PeerInfo of the neighbor we want to transmit to.
1079 * @return The index of an available slot to transmit to the neighbor.
1082 peer_info_transmit_slot (struct MeshPeerInfo *peer)
1086 for (i = 0; peer->core_transmit[i]; i++)
1088 if (i == (CORE_QUEUE_SIZE - 1))
1090 /* All positions are taken! Overwriting! */
1092 peer_info_cancel_transmission (peer, 0);
1101 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1102 * and insert it in the appropiate structures if the peer is not known yet.
1104 * @param peer Full identity of the peer.
1106 * @return Existing or newly created peer info.
1108 static struct MeshPeerInfo *
1109 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1111 struct MeshPeerInfo *peer_info;
1113 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1114 if (NULL == peer_info)
1117 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1118 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1119 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1120 peer_info->id = GNUNET_PEER_intern (peer);
1128 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1129 * and insert it in the appropiate structures if the peer is not known yet.
1131 * @param peer Short identity of the peer.
1133 * @return Existing or newly created peer info.
1135 static struct MeshPeerInfo *
1136 peer_info_get_short (const GNUNET_PEER_Id peer)
1138 struct GNUNET_PeerIdentity id;
1140 GNUNET_PEER_resolve (peer, &id);
1141 return peer_info_get (&id);
1146 * Iterator to remove the tunnel from the list of tunnels a peer participates
1149 * @param cls Closure (tunnel info)
1150 * @param key GNUNET_PeerIdentity of the peer (unused)
1151 * @param value PeerInfo of the peer
1153 * @return always GNUNET_YES, to keep iterating
1156 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1158 struct MeshTunnel *t = cls;
1159 struct MeshPeerInfo *peer = value;
1162 for (i = 0; i < peer->ntunnels; i++)
1165 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1168 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1169 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1178 * Core callback to write a
1180 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1181 * @param size Number of bytes available in buf.
1182 * @param buf Where the to write the message.
1184 * @return number of bytes written to buf
1187 send_core_data_raw (void *cls, size_t size, void *buf)
1189 struct MeshTransmissionDescriptor *info = cls;
1190 struct GNUNET_MessageHeader *msg;
1193 GNUNET_assert (NULL != info);
1194 GNUNET_assert (NULL != info->mesh_data);
1195 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1196 total_size = ntohs (msg->size);
1198 if (total_size > size)
1200 struct GNUNET_PeerIdentity id;
1202 GNUNET_PEER_resolve (info->peer->id, &id);
1203 info->peer->core_transmit[info->handler_n] =
1204 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1205 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1206 size, &send_core_data_raw, info);
1209 info->peer->core_transmit[info->handler_n] = NULL;
1210 memcpy (buf, msg, total_size);
1211 GNUNET_free (info->mesh_data);
1218 * Sends an already built message to a peer, properly registrating
1219 * all used resources.
1221 * @param message Message to send. Fucntion makes a copy of it.
1222 * @param peer Short ID of the neighbor whom to send the message.
1227 send_message (const struct GNUNET_MessageHeader *message,
1228 const struct GNUNET_PeerIdentity *peer)
1230 struct MeshTransmissionDescriptor *info;
1231 struct MeshPeerInfo *neighbor;
1232 struct MeshPeerPath *p;
1236 // GNUNET_TRANSPORT_try_connect();
1238 size = ntohs (message->size);
1239 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1240 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1241 info->mesh_data->data = GNUNET_malloc (size);
1242 memcpy (info->mesh_data->data, message, size);
1243 info->mesh_data->data_len = size;
1244 neighbor = peer_info_get (peer);
1245 for (p = neighbor->path_head; NULL != p; p = p->next)
1255 GNUNET_free (info->mesh_data->data);
1256 GNUNET_free (info->mesh_data);
1260 i = peer_info_transmit_slot (neighbor);
1261 info->handler_n = i;
1262 info->peer = neighbor;
1263 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1264 neighbor->infos[i] = info;
1265 neighbor->core_transmit[i] =
1266 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1267 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1268 size, &send_core_data_raw, info);
1274 * Sends a CREATE PATH message for a path to a peer, properly registrating
1275 * all used resources.
1277 * @param peer PeerInfo of the final peer for whom this path is being created.
1278 * @param p Path itself.
1279 * @param t Tunnel for which the path is created.
1282 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1283 struct MeshTunnel *t)
1285 struct GNUNET_PeerIdentity id;
1286 struct MeshPathInfo *path_info;
1287 struct MeshPeerInfo *neighbor;
1292 p = tree_get_path_to_peer (t->tree, peer->id);
1299 for (i = 0; i < p->length; i++)
1301 if (p->peers[i] == myid)
1304 if (i >= p->length - 1)
1310 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1312 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1313 path_info->path = p;
1315 neighbor = peer_info_get (&id);
1316 path_info->peer = neighbor;
1317 path_info->pos = peer_info_transmit_slot (neighbor);
1318 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1319 neighbor->infos[path_info->pos] = path_info;
1320 neighbor->core_transmit[path_info->pos] = GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
1323 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1325 sizeof (struct GNUNET_MESH_ManipulatePath) + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1326 &send_core_create_path, /* callback */
1327 path_info); /* cls */
1332 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1334 * @param t Tunnel whose path to destroy.
1335 * @param destination Short ID of the peer to whom the path to destroy.
1338 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1340 struct MeshPeerPath *p;
1343 p = tree_get_path_to_peer (t->tree, destination);
1349 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1350 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1352 struct GNUNET_MESH_ManipulatePath *msg;
1353 struct GNUNET_PeerIdentity *pi;
1357 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1358 msg->header.size = htons (size);
1359 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1360 msg->tid = htonl (t->id.tid);
1361 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1362 for (i = 0; i < p->length; i++)
1364 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1366 send_message (&msg->header, tree_get_first_hop (t->tree, destination));
1373 * Try to establish a new connection to this peer.
1374 * Use the best path for the given tunnel.
1375 * If the peer doesn't have any path to it yet, try to get one.
1376 * If the peer already has some path, send a CREATE PATH towards it.
1378 * @param peer PeerInfo of the peer.
1379 * @param t Tunnel for which to create the path, if possible.
1382 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1384 struct MeshPeerPath *p;
1385 struct MeshPathInfo *path_info;
1387 if (NULL != peer->path_head)
1389 p = tree_get_path_to_peer (t->tree, peer->id);
1398 send_create_path (peer, p, t);
1403 send_client_peer_connected (t, myid);
1406 else if (NULL == peer->dhtget)
1408 struct GNUNET_PeerIdentity id;
1410 GNUNET_PEER_resolve (peer->id, &id);
1411 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1412 path_info->peer = peer;
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1415 "MESH: Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1416 peer->dhtgetcls = path_info;
1417 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1418 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1419 GNUNET_BLOCK_TYPE_TEST, /* type */
1420 &id.hashPubKey, /* key to search */
1421 4, /* replication level */
1422 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1423 0, /* xquery bits */
1424 &dht_get_id_handler, path_info);
1426 /* Otherwise, there is no path but the DHT get is already started. */
1431 * Task to delay the connection of a peer
1433 * @param cls Closure (path info with tunnel and peer to connect).
1434 * Will be free'd on exection.
1435 * @param tc TaskContext
1438 peer_info_connect_task (void *cls,
1439 const struct GNUNET_SCHEDULER_TaskContext *tc)
1441 struct MeshPathInfo *path_info = cls;
1443 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1448 peer_info_connect (path_info->peer, path_info->t);
1454 * Destroy the peer_info and free any allocated resources linked to it
1456 * @param pi The peer_info to destroy.
1458 * @return GNUNET_OK on success
1461 peer_info_destroy (struct MeshPeerInfo *pi)
1463 struct GNUNET_PeerIdentity id;
1464 struct MeshPeerPath *p;
1465 struct MeshPeerPath *nextp;
1468 GNUNET_PEER_resolve (pi->id, &id);
1469 GNUNET_PEER_change_rc (pi->id, -1);
1472 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1476 "MESH: removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1478 if (NULL != pi->dhtget)
1480 GNUNET_DHT_get_stop (pi->dhtget);
1481 GNUNET_free (pi->dhtgetcls);
1483 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1485 peer_info_cancel_transmission (pi, i);
1491 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1501 * Remove all paths that rely on a direct connection between p1 and p2
1502 * from the peer itself and notify all tunnels about it.
1504 * @param peer PeerInfo of affected peer.
1505 * @param p1 GNUNET_PEER_Id of one peer.
1506 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1509 * TODO: optimize (see below)
1512 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1515 struct MeshPeerPath *p;
1516 struct MeshPeerPath *aux;
1517 struct MeshPeerInfo *peer_d;
1519 unsigned int destroyed;
1525 p = peer->path_head;
1529 for (i = 0; i < (p->length - 1); i++)
1531 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1532 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1534 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1545 for (i = 0; i < peer->ntunnels; i++)
1547 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1551 * Problem: one or more peers have been deleted from the tunnel tree.
1552 * We don't know who they are to try to add them again.
1553 * We need to try to find a new path for each of the disconnected peers.
1554 * Some of them might already have a path to reach them that does not
1555 * involve p1 and p2. Adding all anew might render in a better tree than
1556 * the trivial immediate fix.
1558 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1559 * its children will be reachable trough him.
1561 peer_d = peer_info_get_short (d);
1564 for (p = peer_d->path_head; NULL != p; p = p->next)
1566 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1574 /* No callback, as peer will be already disconnected and a connection
1575 * scheduled by tunnel_notify_connection_broken.
1577 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1581 peer_info_connect (peer_d, peer->tunnels[i]);
1588 * Add the path to the peer and update the path used to reach it in case this
1591 * @param peer_info Destination peer to add the path to.
1592 * @param path New path to add. Last peer must be the peer in arg 1.
1593 * Path will be either used of freed if already known.
1594 * @param trusted Do we trust that this path is real?
1597 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1600 struct MeshPeerPath *aux;
1604 if ((NULL == peer_info) || (NULL == path))
1607 path_destroy (path);
1610 if (path->peers[path->length - 1] != peer_info->id)
1613 path_destroy (path);
1616 if (path->length <= 2 && GNUNET_NO == trusted)
1618 /* Only allow CORE to tell us about direct paths */
1619 path_destroy (path);
1622 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1623 for (l = 1; l < path->length; l++)
1625 if (path->peers[l] == myid)
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shortening path by %u\n", l);
1628 for (l2 = 0; l2 < path->length - l; l2++)
1630 path->peers[l2] = path->peers[l + l2];
1635 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1640 struct GNUNET_PeerIdentity id;
1642 GNUNET_PEER_resolve (peer_info->id, &id);
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: adding path [%u] to peer %s\n",
1644 path->length, GNUNET_i2s (&id));
1647 l = path_get_length (path);
1654 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1655 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1657 l2 = path_get_length (aux);
1660 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1661 peer_info->path_tail, aux, path);
1666 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1668 path_destroy (path);
1673 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1680 * Add the path to the origin peer and update the path used to reach it in case
1681 * this is the shortest.
1682 * The path is given in peer_info -> destination, therefore we turn the path
1683 * upside down first.
1685 * @param peer_info Peer to add the path to, being the origin of the path.
1686 * @param path New path to add after being inversed.
1687 * @param trusted Do we trust that this path is real?
1690 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1691 struct MeshPeerPath *path, int trusted)
1694 peer_info_add_path (peer_info, path, trusted);
1699 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1700 * to obtain a local peer -> destination path and interning the peer ids.
1702 * @return Newly allocated and created path
1704 static struct MeshPeerPath *
1705 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1706 unsigned int get_path_length,
1707 const struct GNUNET_PeerIdentity *put_path,
1708 unsigned int put_path_length)
1710 struct MeshPeerPath *p;
1716 GNUNET_PEER_change_rc (myid, 1);
1717 i = get_path_length;
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1719 for (i--; i >= 0; i--)
1721 id = GNUNET_PEER_intern (&get_path[i]);
1722 if (p->length > 0 && id == p->peers[p->length - 1])
1724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1725 GNUNET_PEER_change_rc (id, -1);
1729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Adding from GET: %s.\n",
1730 GNUNET_i2s (&get_path[i]));
1732 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1733 p->peers[p->length - 1] = id;
1736 i = put_path_length;
1737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1738 for (i--; i >= 0; i--)
1740 id = GNUNET_PEER_intern (&put_path[i]);
1743 /* PUT path went through us, so discard the path up until now and start
1744 * from here to get a much shorter (and loop-free) path.
1749 if (p->length > 0 && id == p->peers[p->length - 1])
1751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1752 GNUNET_PEER_change_rc (id, -1);
1756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Adding from PUT: %s.\n",
1757 GNUNET_i2s (&put_path[i]));
1759 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1760 p->peers[p->length - 1] = id;
1764 if (get_path_length > 0)
1765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (first of GET: %s)\n",
1766 GNUNET_i2s (&get_path[0]));
1767 if (put_path_length > 0)
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (first of PUT: %s)\n",
1769 GNUNET_i2s (&put_path[0]));
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: In total: %d hops\n",
1772 for (i = 0; i < p->length; i++)
1774 struct GNUNET_PeerIdentity peer_id;
1776 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %u: %s\n", p->peers[i],
1778 GNUNET_i2s (&peer_id));
1786 * Adds a path to the peer_infos of all the peers in the path
1788 * @param p Path to process.
1789 * @param confirmed Whether we know if the path works or not. FIXME use
1792 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1796 /* TODO: invert and add */
1797 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1798 for (i++; i < p->length; i++)
1800 struct MeshPeerInfo *aux;
1801 struct MeshPeerPath *copy;
1803 aux = peer_info_get_short (p->peers[i]);
1804 copy = path_duplicate (p);
1805 copy->length = i + 1;
1806 peer_info_add_path (aux, copy, GNUNET_NO);
1812 * Send keepalive packets for a peer
1814 * @param cls Closure (tunnel for which to send the keepalive).
1815 * @param tc Notification context.
1817 * TODO: implement explicit multicast keepalive?
1820 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1824 * Search for a tunnel among the incoming tunnels
1826 * @param tid the local id of the tunnel
1828 * @return tunnel handler, NULL if doesn't exist
1830 static struct MeshTunnel *
1831 tunnel_get_incoming (MESH_TunnelNumber tid)
1833 GNUNET_HashCode hash;
1835 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1836 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1837 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1842 * Search for a tunnel among the tunnels for a client
1844 * @param c the client whose tunnels to search in
1845 * @param tid the local id of the tunnel
1847 * @return tunnel handler, NULL if doesn't exist
1849 static struct MeshTunnel *
1850 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1852 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1854 return tunnel_get_incoming (tid);
1858 GNUNET_HashCode hash;
1860 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1861 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1867 * Search for a tunnel by global ID using PEER_ID
1869 * @param pi owner of the tunnel
1870 * @param tid global tunnel number
1872 * @return tunnel handler, NULL if doesn't exist
1874 static struct MeshTunnel *
1875 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1877 struct MESH_TunnelID id;
1878 GNUNET_HashCode hash;
1883 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1884 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1889 * Search for a tunnel by global ID using full PeerIdentities
1891 * @param oid owner of the tunnel
1892 * @param tid global tunnel number
1894 * @return tunnel handler, NULL if doesn't exist
1896 static struct MeshTunnel *
1897 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1899 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1904 * Callback used to notify a client owner of a tunnel that a peer has
1905 * disconnected, most likely because of a path change.
1907 * @param cls Closure (tunnel this notification is about).
1908 * @param peer_id Short ID of disconnected peer.
1911 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
1913 struct MeshTunnel *t = cls;
1914 struct MeshPeerInfo *peer;
1915 struct MeshPathInfo *path_info;
1917 if (NULL != t->client && NULL != nc)
1919 struct GNUNET_MESH_PeerControl msg;
1921 msg.header.size = htons (sizeof (msg));
1922 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1923 msg.tunnel_id = htonl (t->local_tid);
1924 GNUNET_PEER_resolve (peer_id, &msg.peer);
1925 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1926 &msg.header, GNUNET_NO);
1928 peer = peer_info_get_short (peer_id);
1929 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1930 path_info->peer = peer;
1932 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
1937 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1938 * needed rescources.
1939 * If peer already exists, reevaluate shortest path and change if different.
1941 * @param t Tunnel we want to add a new peer to
1942 * @param peer PeerInfo of the peer being added
1946 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1948 struct GNUNET_PeerIdentity id;
1949 struct MeshPeerPath *best_p;
1950 struct MeshPeerPath *p;
1951 unsigned int best_cost;
1954 GNUNET_PEER_resolve (peer->id, &id);
1956 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
1959 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1960 GNUNET_assert (GNUNET_OK ==
1961 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
1963 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1966 if (NULL != (p = peer->path_head))
1969 best_cost = tree_get_path_cost (t->tree, p);
1972 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
1979 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
1980 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1981 t->path_refresh_task =
1982 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
1986 /* Start a DHT get */
1987 peer_info_connect (peer, t);
1992 * Add a path to a tunnel which we don't own, just to remember the next hop.
1993 * If destination node was already in the tunnel, the first hop information
1994 * will be replaced with the new path.
1996 * @param t Tunnel we want to add a new peer to
1997 * @param p Path to add
1998 * @param own_pos Position of local node in path.
2002 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2003 unsigned int own_pos)
2005 struct GNUNET_PeerIdentity id;
2007 GNUNET_assert (0 != own_pos);
2008 tree_add_path (t->tree, p, NULL, NULL);
2009 if (own_pos < p->length - 1)
2011 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2012 tree_update_first_hops (t->tree, myid, &id);
2018 * Notifies a tunnel that a connection has broken that affects at least
2019 * some of its peers. Sends a notification towards the root of the tree.
2020 * In case the peer is the owner of the tree, notifies the client that owns
2021 * the tunnel and tries to reconnect.
2023 * @param t Tunnel affected.
2024 * @param p1 Peer that got disconnected from p2.
2025 * @param p2 Peer that got disconnected from p1.
2027 * @return Short ID of the peer disconnected (either p1 or p2).
2028 * 0 if the tunnel remained unaffected.
2030 static GNUNET_PEER_Id
2031 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2037 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2039 if (myid != p1 && myid != p2)
2045 if (tree_get_predecessor (t->tree) != 0)
2047 /* We are the peer still connected, notify owner of the disconnection. */
2048 struct GNUNET_MESH_PathBroken msg;
2049 struct GNUNET_PeerIdentity neighbor;
2051 msg.header.size = htons (sizeof (msg));
2052 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2053 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2054 msg.tid = htonl (t->id.tid);
2055 msg.peer1 = my_full_id;
2056 GNUNET_PEER_resolve (pid, &msg.peer2);
2057 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2058 send_message (&msg.header, &neighbor);
2066 * Send a multicast packet to a neighbor.
2068 * @param cls Closure (Info about the multicast packet)
2069 * @param neighbor_id Short ID of the neighbor to send the packet to.
2072 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2074 struct MeshData *mdata = cls;
2075 struct MeshTransmissionDescriptor *info;
2076 struct GNUNET_PeerIdentity neighbor;
2079 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2081 info->mesh_data = mdata;
2082 (*(mdata->reference_counter)) ++;
2083 info->destination = neighbor_id;
2084 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending to %s...\n",
2087 GNUNET_i2s (&neighbor));
2089 info->peer = peer_info_get (&neighbor);
2090 GNUNET_assert (NULL != info->peer);
2091 i = peer_info_transmit_slot (info->peer);
2092 info->handler_n = i;
2093 info->peer->infos[i] = info;
2094 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2095 info->peer->core_transmit[i] =
2096 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2097 GNUNET_TIME_UNIT_FOREVER_REL,
2098 &neighbor, info->mesh_data->data_len,
2099 &send_core_data_multicast, info);
2103 * Send a message in a tunnel in multicast, sending a copy to each child node
2104 * down the local one in the tunnel tree.
2106 * @param t Tunnel in which to send the data.
2107 * @param msg Message to be sent
2110 tunnel_send_multicast (struct MeshTunnel *t,
2111 const struct GNUNET_MessageHeader *msg)
2113 struct MeshData *mdata;
2116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2117 "MESH: sending a multicast packet...\n");
2119 mdata = GNUNET_malloc (sizeof (struct MeshData));
2120 mdata->data_len = ntohs (msg->size);
2121 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2123 mdata->data = GNUNET_malloc (mdata->data_len);
2124 memcpy (mdata->data, msg, mdata->data_len);
2125 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2127 struct GNUNET_MESH_Multicast *mcast;
2129 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2130 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: data packet, ttl: %u\n",
2133 ntohl (mcast->ttl));
2137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: not a data packet, no ttl\n");
2140 if (NULL != t->client && GNUNET_YES != t->client->shutting_down)
2142 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2144 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: timeout task %u\n",
2150 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2151 if (*(mdata->reference_counter) == 0)
2153 GNUNET_free (mdata->data);
2154 GNUNET_free (mdata->reference_counter);
2155 if (NULL != mdata->task)
2157 GNUNET_SCHEDULER_cancel(*(mdata->task));
2158 GNUNET_free (mdata->task);
2159 GNUNET_SERVER_receive_done(t->client->handle, GNUNET_OK);
2161 // FIXME change order?
2162 GNUNET_free (mdata);
2165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2166 "MESH: sending a multicast packet done\n");
2173 * Send a message to all peers in this tunnel that the tunnel is no longer
2176 * @param t The tunnel whose peers to notify.
2179 tunnel_send_destroy (struct MeshTunnel *t)
2181 struct GNUNET_MESH_TunnelDestroy msg;
2183 msg.header.size = htons (sizeof (msg));
2184 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2185 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2186 msg.tid = htonl (t->id.tid);
2187 tunnel_send_multicast (t, &msg.header);
2193 * Destroy the tunnel and free any allocated resources linked to it
2195 * @param t the tunnel to destroy
2197 * @return GNUNET_OK on success
2200 tunnel_destroy (struct MeshTunnel *t)
2202 struct MeshClient *c;
2203 struct MeshQueue *q;
2204 struct MeshQueue *qn;
2205 GNUNET_HashCode hash;
2215 struct GNUNET_PeerIdentity id;
2217 GNUNET_PEER_resolve (t->id.oid, &id);
2218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel %s [%x]\n",
2219 GNUNET_i2s (&id), t->id.tid);
2221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2225 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2226 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2231 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2233 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
2238 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2240 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
2244 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2246 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2247 GNUNET_break (GNUNET_YES ==
2248 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash,
2251 if (NULL != t->peers)
2253 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2255 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2260 if (NULL != q->data)
2261 GNUNET_free (q->data);
2265 /* TODO cancel core transmit ready in case it was active */
2267 tree_destroy (t->tree);
2268 if (NULL != t->dht_get_type)
2269 GNUNET_DHT_get_stop (t->dht_get_type);
2270 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2271 GNUNET_SCHEDULER_cancel (t->timeout_task);
2272 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2273 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2280 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2281 * that are no longer needed, as well as nodes of no longer reachable peers.
2282 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2284 * @param t Tunnel from which to remove the path.
2285 * @param peer Short id of the peer which should be removed.
2288 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2290 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2296 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2297 * client when the client disconnects.
2299 * @param cls closure (client that is disconnecting)
2300 * @param key the hash of the local tunnel id (used to access the hashmap)
2301 * @param value the value stored at the key (tunnel to destroy)
2303 * @return GNUNET_OK on success
2306 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2308 struct MeshTunnel *t = value;
2309 struct MeshClient *c = cls;
2312 send_client_tunnel_disconnect(t, c);
2313 if (c == t->client_dest)
2316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2317 "Client %u is destination, keeping the tunnel alive.\n", c->id);
2319 t->client_dest = NULL;
2320 t->local_tid_dest = 0;
2323 tunnel_send_destroy(t);
2324 r = tunnel_destroy (t);
2330 * Timeout function, destroys tunnel if called
2332 * @param cls Closure (tunnel to destroy).
2333 * @param tc TaskContext
2336 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2338 struct MeshTunnel *t = cls;
2340 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2342 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2347 * Resets the tunnel timeout. Starts it if no timeout was running.
2349 * @param t Tunnel whose timeout to reset.
2352 tunnel_reset_timeout (struct MeshTunnel *t)
2354 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2355 GNUNET_SCHEDULER_cancel (t->timeout_task);
2357 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2358 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2362 /******************************************************************************/
2363 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2364 /******************************************************************************/
2367 * Function called to notify a client about the socket
2368 * being ready to queue more data. "buf" will be
2369 * NULL and "size" zero if the socket was closed for
2370 * writing in the meantime.
2372 * @param cls closure
2373 * @param size number of bytes available in buf
2374 * @param buf where the callee should write the message
2375 * @return number of bytes written to buf
2378 send_core_create_path (void *cls, size_t size, void *buf)
2380 struct MeshPathInfo *info = cls;
2381 struct GNUNET_MESH_ManipulatePath *msg;
2382 struct GNUNET_PeerIdentity *peer_ptr;
2383 struct MeshPeerInfo *peer = info->peer;
2384 struct MeshTunnel *t = info->t;
2385 struct MeshPeerPath *p = info->path;
2389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATE PATH sending...\n");
2391 sizeof (struct GNUNET_MESH_ManipulatePath) +
2392 p->length * sizeof (struct GNUNET_PeerIdentity);
2394 if (size < size_needed || NULL == buf)
2396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n", size,
2400 info->peer->core_transmit[info->pos] =
2401 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2402 GNUNET_TIME_UNIT_FOREVER_REL,
2403 tree_get_first_hop (t->tree,
2405 size_needed, &send_core_create_path,
2409 info->peer->core_transmit[info->pos] = NULL;
2412 struct GNUNET_PeerIdentity id;
2414 GNUNET_PEER_resolve (peer->id, &id);
2415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2416 "MESH: setting core_transmit %s [%u] to NULL\n",
2417 GNUNET_i2s (&id), info->pos);
2420 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2421 msg->header.size = htons (size_needed);
2422 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2423 msg->tid = ntohl (t->id.tid);
2425 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2426 for (i = 0; i < p->length; i++)
2428 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2435 "MESH: CREATE PATH (%u bytes long) sent!\n", size_needed);
2441 * Function called to notify a client about the socket
2442 * being ready to queue more data. "buf" will be
2443 * NULL and "size" zero if the socket was closed for
2444 * writing in the meantime.
2446 * @param cls closure (data itself)
2447 * @param size number of bytes available in buf
2448 * @param buf where the callee should write the message
2450 * @return number of bytes written to buf
2453 send_core_data_multicast (void *cls, size_t size, void *buf)
2455 struct MeshTransmissionDescriptor *info = cls;
2458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2459 GNUNET_assert (NULL != info);
2460 GNUNET_assert (NULL != info->peer);
2461 total_size = info->mesh_data->data_len;
2462 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2464 if (total_size > size)
2467 struct GNUNET_PeerIdentity id;
2469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2470 "MESH: Multicast: retransmitting... (%u/%u)\n", size,
2472 GNUNET_PEER_resolve (info->peer->id, &id);
2473 info->peer->core_transmit[info->handler_n] =
2474 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2475 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2477 &send_core_data_multicast, info);
2480 info->peer->core_transmit[info->handler_n] = NULL;
2481 info->peer->infos[info->handler_n] = NULL;
2482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2483 memcpy (buf, info->mesh_data->data, total_size);
2486 struct GNUNET_MESH_Multicast *mc;
2487 struct GNUNET_MessageHeader *mh;
2490 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2492 mc = (struct GNUNET_MESH_Multicast *) mh;
2493 mh = (struct GNUNET_MessageHeader *) &mc[1];
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 "MESH: multicast, payload type %u\n", ntohs (mh->type));
2496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2497 "MESH: multicast, payload size %u\n", ntohs (mh->size));
2501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type %u\n",
2506 data_descriptor_decrement_multicast (info->mesh_data);
2507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2515 * Function called to notify a client about the socket
2516 * being ready to queue more data. "buf" will be
2517 * NULL and "size" zero if the socket was closed for
2518 * writing in the meantime.
2520 * @param cls closure (MeshTransmissionDescriptor)
2521 * @param size number of bytes available in buf
2522 * @param buf where the callee should write the message
2523 * @return number of bytes written to buf
2526 send_core_path_ack (void *cls, size_t size, void *buf)
2528 struct MeshTransmissionDescriptor *info = cls;
2529 struct GNUNET_MESH_PathACK *msg = buf;
2531 GNUNET_assert (NULL != info);
2534 info->peer->core_transmit[info->handler_n] = NULL;
2536 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2541 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2542 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2543 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2544 msg->tid = htonl (info->origin->tid);
2545 msg->peer_id = my_full_id;
2547 /* TODO add signature */
2549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2550 return sizeof (struct GNUNET_MESH_PathACK);
2554 /******************************************************************************/
2555 /******************** MESH NETWORK HANDLERS **************************/
2556 /******************************************************************************/
2560 * Core handler for path creation
2562 * @param cls closure
2563 * @param message message
2564 * @param peer peer identity this notification is about
2565 * @param atsi performance data
2566 * @param atsi_count number of records in 'atsi'
2568 * @return GNUNET_OK to keep the connection open,
2569 * GNUNET_SYSERR to close it (signal serious error)
2572 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2573 const struct GNUNET_MessageHeader *message,
2574 const struct GNUNET_ATS_Information *atsi,
2575 unsigned int atsi_count)
2577 unsigned int own_pos;
2580 MESH_TunnelNumber tid;
2581 struct GNUNET_MESH_ManipulatePath *msg;
2582 struct GNUNET_PeerIdentity *pi;
2583 GNUNET_HashCode hash;
2584 struct MeshPeerPath *path;
2585 struct MeshPeerInfo *dest_peer_info;
2586 struct MeshPeerInfo *orig_peer_info;
2587 struct MeshTunnel *t;
2589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2590 "MESH: Received a path create msg [%s]\n",
2591 GNUNET_i2s (&my_full_id));
2592 size = ntohs (message->size);
2593 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2595 GNUNET_break_op (0);
2599 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2600 if (size % sizeof (struct GNUNET_PeerIdentity))
2602 GNUNET_break_op (0);
2605 size /= sizeof (struct GNUNET_PeerIdentity);
2608 GNUNET_break_op (0);
2611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
2612 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2614 tid = ntohl (msg->tid);
2615 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2617 "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2618 t = tunnel_get (pi, tid);
2621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2622 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2623 t->id.oid = GNUNET_PEER_intern (pi);
2625 while (NULL != tunnel_get_incoming (next_local_tid))
2626 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2627 t->local_tid = next_local_tid++;
2628 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2629 t->tree = tree_new (t->id.oid);
2631 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2633 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2634 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2640 tunnel_reset_timeout (t);
2641 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2643 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2644 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2652 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2653 if (NULL == dest_peer_info)
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2656 "MESH: Creating PeerInfo for destination.\n");
2657 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2658 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2659 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2661 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2663 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2664 if (NULL == orig_peer_info)
2666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2667 "MESH: Creating PeerInfo for origin.\n");
2668 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2669 orig_peer_info->id = GNUNET_PEER_intern (pi);
2670 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2671 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2674 path = path_new (size);
2676 for (i = 0; i < size; i++)
2678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
2679 GNUNET_i2s (&pi[i]));
2680 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2681 if (path->peers[i] == myid)
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
2687 /* cannot be self, must be 'not found' */
2688 /* create path: self not found in path through self */
2689 GNUNET_break_op (0);
2690 path_destroy (path);
2691 /* FIXME error. destroy tunnel? leave for timeout? */
2694 path_add_to_peers (path, GNUNET_NO);
2695 tunnel_add_path (t, path, own_pos);
2696 if (own_pos == size - 1)
2698 /* It is for us! Send ack. */
2699 struct MeshTransmissionDescriptor *info;
2702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
2703 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2704 if (NULL == t->peers)
2706 /* New tunnel! Notify clients! */
2707 struct GNUNET_MESH_TunnelNotification cmsg;
2709 cmsg.header.size = htons (sizeof (cmsg));
2710 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2711 GNUNET_PEER_resolve (t->id.oid, &cmsg.peer);
2712 cmsg.tunnel_id = htonl (t->local_tid);
2713 GNUNET_SERVER_notification_context_broadcast (nc, &cmsg.header,
2716 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2718 GNUNET_break (GNUNET_OK ==
2719 GNUNET_CONTAINER_multihashmap_put (t->peers,
2720 &my_full_id.hashPubKey,
2723 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
2724 /* FIXME use send_message */
2725 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2726 info->origin = &t->id;
2727 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2728 GNUNET_assert (NULL != info->peer);
2729 j = peer_info_transmit_slot (info->peer);
2730 info->handler_n = j;
2731 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2732 info->peer->infos[j] = info;
2733 info->peer->core_transmit[j] =
2734 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2735 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2736 sizeof (struct GNUNET_MESH_PathACK),
2737 &send_core_path_ack, info);
2741 struct MeshPeerPath *path2;
2743 /* It's for somebody else! Retransmit. */
2744 path2 = path_duplicate (path);
2745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting.\n");
2746 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
2747 path2 = path_duplicate (path);
2748 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
2749 send_create_path (dest_peer_info, path, t);
2756 * Core handler for path destruction
2758 * @param cls closure
2759 * @param message message
2760 * @param peer peer identity this notification is about
2761 * @param atsi performance data
2762 * @param atsi_count number of records in 'atsi'
2764 * @return GNUNET_OK to keep the connection open,
2765 * GNUNET_SYSERR to close it (signal serious error)
2768 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2769 const struct GNUNET_MessageHeader *message,
2770 const struct GNUNET_ATS_Information *atsi,
2771 unsigned int atsi_count)
2773 struct GNUNET_MESH_ManipulatePath *msg;
2774 struct GNUNET_PeerIdentity *pi;
2775 struct MeshPeerPath *path;
2776 struct MeshTunnel *t;
2777 unsigned int own_pos;
2781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2782 "MESH: Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
2783 size = ntohs (message->size);
2784 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2786 GNUNET_break_op (0);
2790 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2791 if (size % sizeof (struct GNUNET_PeerIdentity))
2793 GNUNET_break_op (0);
2796 size /= sizeof (struct GNUNET_PeerIdentity);
2799 GNUNET_break_op (0);
2802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
2804 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2805 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2807 "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
2809 t = tunnel_get (pi, ntohl (msg->tid));
2812 /* TODO notify back: we don't know this tunnel */
2813 GNUNET_break_op (0);
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2817 path = path_new (size);
2819 for (i = 0; i < size; i++)
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
2822 GNUNET_i2s (&pi[i]));
2823 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2824 if (path->peers[i] == myid)
2827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
2828 if (own_pos < path->length - 1)
2829 send_message (message, &pi[own_pos + 1]);
2830 tunnel_delete_peer (t, path->peers[path->length - 1]);
2831 path_destroy (path);
2837 * Core handler for notifications of broken paths
2839 * @param cls closure
2840 * @param message message
2841 * @param peer peer identity this notification is about
2842 * @param atsi performance data
2843 * @param atsi_count number of records in 'atsi'
2845 * @return GNUNET_OK to keep the connection open,
2846 * GNUNET_SYSERR to close it (signal serious error)
2849 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
2850 const struct GNUNET_MessageHeader *message,
2851 const struct GNUNET_ATS_Information *atsi,
2852 unsigned int atsi_count)
2854 struct GNUNET_MESH_PathBroken *msg;
2855 struct MeshTunnel *t;
2857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2858 "MESH: Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
2859 msg = (struct GNUNET_MESH_PathBroken *) message;
2860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
2861 GNUNET_i2s (&msg->peer1));
2862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
2863 GNUNET_i2s (&msg->peer2));
2864 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2867 GNUNET_break_op (0);
2870 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
2871 GNUNET_PEER_search (&msg->peer2));
2878 * Core handler for tunnel destruction
2880 * @param cls closure
2881 * @param message message
2882 * @param peer peer identity this notification is about
2883 * @param atsi performance data
2884 * @param atsi_count number of records in 'atsi'
2886 * @return GNUNET_OK to keep the connection open,
2887 * GNUNET_SYSERR to close it (signal serious error)
2890 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2891 const struct GNUNET_MessageHeader *message,
2892 const struct GNUNET_ATS_Information *atsi,
2893 unsigned int atsi_count)
2895 struct GNUNET_MESH_TunnelDestroy *msg;
2896 struct MeshTunnel *t;
2898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2899 "MESH: Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
2900 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for tunnel %s [%u]\n",
2902 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
2903 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2906 /* Probably already got the message from another path,
2907 * destroyed the tunnel and retransmitted to children.
2912 if (t->id.oid == myid)
2914 GNUNET_break_op (0);
2917 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2919 /* Tunnel was incoming, notify clients */
2920 send_clients_tunnel_destroy (t);
2922 tunnel_send_destroy (t);
2929 * Core handler for mesh network traffic going from the origin to a peer
2931 * @param cls closure
2932 * @param peer peer identity this notification is about
2933 * @param message message
2934 * @param atsi performance data
2935 * @param atsi_count number of records in 'atsi'
2936 * @return GNUNET_OK to keep the connection open,
2937 * GNUNET_SYSERR to close it (signal serious error)
2940 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2941 const struct GNUNET_MessageHeader *message,
2942 const struct GNUNET_ATS_Information *atsi,
2943 unsigned int atsi_count)
2945 struct GNUNET_MESH_Unicast *msg;
2946 struct MeshTunnel *t;
2950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a unicast packet from %s\n",
2952 size = ntohs (message->size);
2954 sizeof (struct GNUNET_MESH_Unicast) +
2955 sizeof (struct GNUNET_MessageHeader))
2960 msg = (struct GNUNET_MESH_Unicast *) message;
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: of type %u\n",
2962 ntohs (msg[1].header.type));
2963 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2966 /* TODO notify back: we don't know this tunnel */
2967 GNUNET_break_op (0);
2970 tunnel_reset_timeout (t);
2971 pid = GNUNET_PEER_search (&msg->destination);
2974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2975 "MESH: it's for us! sending to clients...\n");
2976 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2980 "MESH: not for us, retransmitting...\n");
2981 send_message (message, tree_get_first_hop (t->tree, pid));
2987 * Core handler for mesh network traffic going from the origin to all peers
2989 * @param cls closure
2990 * @param message message
2991 * @param peer peer identity this notification is about
2992 * @param atsi performance data
2993 * @param atsi_count number of records in 'atsi'
2994 * @return GNUNET_OK to keep the connection open,
2995 * GNUNET_SYSERR to close it (signal serious error)
2997 * TODO: Check who we got this from, to validate route.
3000 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3001 const struct GNUNET_MessageHeader *message,
3002 const struct GNUNET_ATS_Information *atsi,
3003 unsigned int atsi_count)
3005 struct GNUNET_MESH_Multicast *msg;
3006 struct MeshTunnel *t;
3009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a multicast packet from %s\n",
3011 size = ntohs (message->size);
3012 if (sizeof (struct GNUNET_MESH_Multicast) +
3013 sizeof (struct GNUNET_MessageHeader) > size)
3015 GNUNET_break_op (0);
3018 msg = (struct GNUNET_MESH_Multicast *) message;
3019 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3023 /* TODO notify that we dont know that tunnel */
3024 GNUNET_break_op (0);
3027 if (t->mid == ntohl (msg->mid))
3029 /* FIXME: already seen this packet, log dropping */
3030 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3031 "MESH: Already seen mid %u, DROPPING!\n", t->mid);
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3038 "MESH: mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3041 t->mid = ntohl (msg->mid);
3042 tunnel_reset_timeout (t);
3044 /* Transmit to locally interested clients */
3045 if (NULL != t->peers &&
3046 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3048 send_subscribed_clients (message, &msg[1].header);
3051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ttl: %u\n", ntohl (msg->ttl));
3053 if (ntohl (msg->ttl) == 0)
3055 /* FIXME: ttl is 0, log dropping */
3056 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "MESH: TTL is 0, DROPPING!\n");
3059 tunnel_send_multicast (t, message);
3065 * Core handler for mesh network traffic toward the owner of a tunnel
3067 * @param cls closure
3068 * @param message message
3069 * @param peer peer identity this notification is about
3070 * @param atsi performance data
3071 * @param atsi_count number of records in 'atsi'
3073 * @return GNUNET_OK to keep the connection open,
3074 * GNUNET_SYSERR to close it (signal serious error)
3077 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3078 const struct GNUNET_MessageHeader *message,
3079 const struct GNUNET_ATS_Information *atsi,
3080 unsigned int atsi_count)
3082 struct GNUNET_MESH_ToOrigin *msg;
3083 struct GNUNET_PeerIdentity id;
3084 struct MeshPeerInfo *peer_info;
3085 struct MeshTunnel *t;
3088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a ToOrigin packet from %s\n",
3090 size = ntohs (message->size);
3091 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3092 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3094 GNUNET_break_op (0);
3097 msg = (struct GNUNET_MESH_ToOrigin *) message;
3098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: of type %u\n",
3099 ntohs (msg[1].header.type));
3100 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3104 /* TODO notify that we dont know this tunnel (whom)? */
3105 GNUNET_break_op (0);
3109 if (t->id.oid == myid)
3112 struct GNUNET_MESH_ToOrigin *copy;
3114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3115 "MESH: it's for us! sending to clients...\n");
3116 if (NULL == t->client)
3118 /* got data packet for ownerless tunnel */
3119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: no clients!\n");
3120 GNUNET_break_op (0);
3123 /* TODO signature verification */
3124 memcpy (cbuf, message, size);
3125 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3126 copy->tid = htonl (t->local_tid);
3127 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
3128 ©->header, GNUNET_YES);
3131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3132 "MESH: not for us, retransmitting...\n");
3134 peer_info = peer_info_get (&msg->oid);
3135 if (NULL == peer_info)
3137 /* unknown origin of tunnel */
3141 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3142 send_message (message, &id);
3149 * Core handler for path ACKs
3151 * @param cls closure
3152 * @param message message
3153 * @param peer peer identity this notification is about
3154 * @param atsi performance data
3155 * @param atsi_count number of records in 'atsi'
3157 * @return GNUNET_OK to keep the connection open,
3158 * GNUNET_SYSERR to close it (signal serious error)
3161 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3162 const struct GNUNET_MessageHeader *message,
3163 const struct GNUNET_ATS_Information *atsi,
3164 unsigned int atsi_count)
3166 struct GNUNET_MESH_PathACK *msg;
3167 struct GNUNET_PeerIdentity id;
3168 struct MeshPeerInfo *peer_info;
3169 struct MeshPeerPath *p;
3170 struct MeshTunnel *t;
3172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Received a path ACK msg [%s]\n",
3173 GNUNET_i2s (&my_full_id));
3174 msg = (struct GNUNET_MESH_PathACK *) message;
3175 t = tunnel_get (&msg->oid, msg->tid);
3178 /* TODO notify that we don't know the tunnel */
3182 peer_info = peer_info_get (&msg->peer_id);
3184 /* Add paths to peers? */
3185 p = tree_get_path_to_peer (t->tree, peer_info->id);
3188 path_add_to_peers (p, GNUNET_YES);
3196 /* Message for us? */
3197 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
3200 if (NULL == t->client)
3202 GNUNET_break_op (0);
3205 if (NULL != t->dht_get_type)
3207 GNUNET_DHT_get_stop (t->dht_get_type);
3208 t->dht_get_type = NULL;
3210 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3212 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3213 send_client_peer_connected (t, peer_info->id);
3218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3219 "MESH: not for us, retransmitting...\n");
3220 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3221 peer_info = peer_info_get (&msg->oid);
3222 if (NULL == peer_info)
3224 /* If we know the tunnel, we should DEFINITELY know the peer */
3228 send_message (message, &id);
3234 * Functions to handle messages from core
3236 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3237 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3238 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3239 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3240 sizeof (struct GNUNET_MESH_PathBroken)},
3241 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3242 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3243 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3244 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3245 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3246 sizeof (struct GNUNET_MESH_PathACK)},
3252 /******************************************************************************/
3253 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3254 /******************************************************************************/
3257 * deregister_app: iterator for removing each application registered by a client
3259 * @param cls closure
3260 * @param key the hash of the application id (used to access the hashmap)
3261 * @param value the value stored at the key (client)
3263 * @return GNUNET_OK on success
3266 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3268 GNUNET_break (GNUNET_YES ==
3269 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3276 * notify_client_connection_failure: notify a client that the connection to the
3277 * requested remote peer is not possible (for instance, no route found)
3278 * Function called when the socket is ready to queue more data. "buf" will be
3279 * NULL and "size" zero if the socket was closed for writing in the meantime.
3281 * @param cls closure
3282 * @param size number of bytes available in buf
3283 * @param buf where the callee should write the message
3284 * @return number of bytes written to buf
3287 notify_client_connection_failure (void *cls, size_t size, void *buf)
3290 struct MeshPeerInfo *peer_info;
3291 struct GNUNET_MESH_PeerControl *msg;
3292 struct GNUNET_PeerIdentity id;
3294 if (0 == size && NULL == buf)
3296 // TODO retry? cancel?
3300 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3301 peer_info = (struct MeshPeerInfo *) cls;
3302 msg = (struct GNUNET_MESH_PeerControl *) buf;
3303 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3304 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3305 // msg->tunnel_id = htonl(peer_info->t->tid);
3306 GNUNET_PEER_resolve (peer_info->id, &id);
3307 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3315 * Send keepalive packets for a peer
3317 * @param cls Closure (tunnel for which to send the keepalive).
3318 * @param tc Notification context.
3320 * TODO: implement explicit multicast keepalive?
3323 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3325 struct MeshTunnel *t = cls;
3326 struct GNUNET_MessageHeader *payload;
3327 struct GNUNET_MESH_Multicast *msg;
3329 sizeof (struct GNUNET_MESH_Multicast) +
3330 sizeof (struct GNUNET_MessageHeader);
3333 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3337 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3340 "MESH: sending keepalive for tunnel %d\n", t->id.tid);
3342 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3343 msg->header.size = htons (size);
3344 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3345 msg->oid = my_full_id;
3346 msg->tid = htonl (t->id.tid);
3347 payload = (struct GNUNET_MessageHeader *) &msg[1];
3348 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3349 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3350 tunnel_send_multicast (t, &msg->header);
3352 t->path_refresh_task =
3353 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3359 * Function to process paths received for a new peer addition. The recorded
3360 * paths form the initial tunnel, which can be optimized later.
3361 * Called on each result obtained for the DHT search.
3363 * @param cls closure
3364 * @param exp when will this value expire
3365 * @param key key of the result
3366 * @param get_path path of the get request
3367 * @param get_path_length lenght of get_path
3368 * @param put_path path of the put request
3369 * @param put_path_length length of the put_path
3370 * @param type type of the result
3371 * @param size number of bytes in data
3372 * @param data pointer to the result data
3374 * TODO: re-issue the request after certain time? cancel after X results?
3377 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3378 const GNUNET_HashCode * key,
3379 const struct GNUNET_PeerIdentity *get_path,
3380 unsigned int get_path_length,
3381 const struct GNUNET_PeerIdentity *put_path,
3382 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3383 size_t size, const void *data)
3385 struct MeshPathInfo *path_info = cls;
3386 struct MeshPeerPath *p;
3387 struct GNUNET_PeerIdentity pi;
3390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got results from DHT!\n");
3391 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n", GNUNET_i2s (&pi));
3394 p = path_build_from_dht (get_path, get_path_length, put_path,
3396 path_add_to_peers (p, GNUNET_NO);
3398 for (i = 0; i < path_info->peer->ntunnels; i++)
3400 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3401 peer_info_connect (path_info->peer, path_info->t);
3409 * Function to process paths received for a new peer addition. The recorded
3410 * paths form the initial tunnel, which can be optimized later.
3411 * Called on each result obtained for the DHT search.
3413 * @param cls closure
3414 * @param exp when will this value expire
3415 * @param key key of the result
3416 * @param get_path path of the get request
3417 * @param get_path_length lenght of get_path
3418 * @param put_path path of the put request
3419 * @param put_path_length length of the put_path
3420 * @param type type of the result
3421 * @param size number of bytes in data
3422 * @param data pointer to the result data
3425 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3426 const GNUNET_HashCode * key,
3427 const struct GNUNET_PeerIdentity *get_path,
3428 unsigned int get_path_length,
3429 const struct GNUNET_PeerIdentity *put_path,
3430 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3431 size_t size, const void *data)
3433 const struct GNUNET_PeerIdentity *pi = data;
3434 struct MeshTunnel *t = cls;
3435 struct MeshPeerInfo *peer_info;
3436 struct MeshPeerPath *p;
3438 if (size != sizeof (struct GNUNET_PeerIdentity))
3440 GNUNET_break_op (0);
3443 GNUNET_assert (NULL != t->client);
3444 peer_info = peer_info_get (pi);
3445 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3447 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3449 p = path_build_from_dht (get_path, get_path_length, put_path,
3451 path_add_to_peers (p, GNUNET_NO);
3453 tunnel_add_peer (t, peer_info);
3454 peer_info_connect (peer_info, t);
3458 /******************************************************************************/
3459 /********************* MESH LOCAL HANDLES **************************/
3460 /******************************************************************************/
3464 * Handler for client disconnection
3466 * @param cls closure
3467 * @param client identification of the client; NULL
3468 * for the last call when the server is destroyed
3471 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3473 struct MeshClient *c;
3474 struct MeshClient *next;
3477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3487 if (c->handle != client)
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found (%u)\n",
3499 GNUNET_SERVER_client_drop (c->handle);
3500 c->shutting_down = GNUNET_YES;
3501 if (NULL != c->tunnels)
3503 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3504 &tunnel_destroy_iterator, c);
3505 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3508 /* deregister clients applications */
3509 if (NULL != c->apps)
3511 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3512 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3514 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3515 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3517 GNUNET_SCHEDULER_cancel (announce_applications_task);
3518 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3520 if (NULL != c->types)
3521 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3523 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT FREE at %p\n", c);
3532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3539 * Handler for new clients
3541 * @param cls closure
3542 * @param client identification of the client
3543 * @param message the actual message, which includes messages the client wants
3546 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3547 const struct GNUNET_MessageHeader *message)
3549 struct GNUNET_MESH_ClientConnect *cc_msg;
3550 struct MeshClient *c;
3551 GNUNET_MESH_ApplicationType *a;
3558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3559 /* Check data sanity */
3560 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3561 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3562 ntypes = ntohs (cc_msg->types);
3563 napps = ntohs (cc_msg->applications);
3565 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3568 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3572 /* Create new client structure */
3573 c = GNUNET_malloc (sizeof (struct MeshClient));
3575 c->id = next_client_id++;
3576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT NEW %u at %p\n", c->id,
3580 GNUNET_SERVER_client_keep (client);
3581 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3584 GNUNET_MESH_ApplicationType at;
3587 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3588 for (i = 0; i < napps; i++)
3591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3592 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3593 /* store in clients hashmap */
3594 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3595 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3596 /* store in global hashmap, for announcements */
3597 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3598 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3600 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3601 announce_applications_task =
3602 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3610 t = (uint16_t *) & a[napps];
3611 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3612 for (i = 0; i < ntypes; i++)
3615 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3617 /* store in clients hashmap */
3618 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3619 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3620 /* store in global hashmap */
3621 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3622 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3626 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3628 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3629 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3630 GNUNET_SERVER_notification_context_add (nc, client);
3632 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3640 * Handler for requests of new tunnels
3642 * @param cls closure
3643 * @param client identification of the client
3644 * @param message the actual message
3647 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3648 const struct GNUNET_MessageHeader *message)
3650 struct GNUNET_MESH_TunnelMessage *t_msg;
3651 struct MeshTunnel *t;
3652 struct MeshClient *c;
3653 GNUNET_HashCode hash;
3655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3657 /* Sanity check for client registration */
3658 if (NULL == (c = client_get (client)))
3661 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3668 /* Message sanity check */
3669 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3672 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3676 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3677 /* Sanity check for tunnel numbering */
3678 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3681 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3684 /* Sanity check for duplicate tunnel IDs */
3685 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3688 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3692 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3693 while (NULL != tunnel_get_by_pi (myid, next_tid))
3694 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3695 t->id.tid = next_tid++;
3696 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3698 t->local_tid = ntohl (t_msg->tunnel_id);
3700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL %s [%x] (%x)\n",
3701 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3704 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3706 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3708 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3709 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3712 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3716 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3718 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3719 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3722 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3725 t->tree = tree_new (myid);
3727 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3733 * Handler for requests of deleting tunnels
3735 * @param cls closure
3736 * @param client identification of the client
3737 * @param message the actual message
3740 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3741 const struct GNUNET_MessageHeader *message)
3743 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3744 struct MeshClient *c;
3745 struct MeshTunnel *t;
3746 MESH_TunnelNumber tid;
3747 GNUNET_HashCode hash;
3749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3750 "MESH: Got a DESTROY TUNNEL from client!\n");
3752 /* Sanity check for client registration */
3753 if (NULL == (c = client_get (client)))
3756 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3759 /* Message sanity check */
3760 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3763 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3769 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3771 /* Retrieve tunnel */
3772 tid = ntohl (tunnel_msg->tunnel_id);
3774 /* Remove from local id hashmap */
3775 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3776 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3777 send_client_tunnel_disconnect(t, c);
3778 GNUNET_assert (GNUNET_YES ==
3779 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t));
3782 tunnel_send_destroy (t);
3784 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3790 * Handler for connection requests to new peers
3792 * @param cls closure
3793 * @param client identification of the client
3794 * @param message the actual message (PeerControl)
3797 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3798 const struct GNUNET_MessageHeader *message)
3800 struct GNUNET_MESH_PeerControl *peer_msg;
3801 struct MeshPeerInfo *peer_info;
3802 struct MeshClient *c;
3803 struct MeshTunnel *t;
3804 MESH_TunnelNumber tid;
3806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3807 /* Sanity check for client registration */
3808 if (NULL == (c = client_get (client)))
3811 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3815 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3816 /* Sanity check for message size */
3817 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3820 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3824 /* Tunnel exists? */
3825 tid = ntohl (peer_msg->tunnel_id);
3826 t = tunnel_get_by_local_id (c, tid);
3830 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3834 /* Does client own tunnel? */
3835 if (t->client->handle != client)
3838 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3842 GNUNET_i2s (&peer_msg->peer));
3843 peer_info = peer_info_get (&peer_msg->peer);
3845 tunnel_add_peer (t, peer_info);
3846 peer_info_connect (peer_info, t);
3848 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3854 * Handler for disconnection requests of peers in a tunnel
3856 * @param cls closure
3857 * @param client identification of the client
3858 * @param message the actual message (PeerControl)
3861 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3862 const struct GNUNET_MessageHeader *message)
3864 struct GNUNET_MESH_PeerControl *peer_msg;
3865 struct MeshPeerInfo *peer_info;
3866 struct MeshClient *c;
3867 struct MeshTunnel *t;
3868 MESH_TunnelNumber tid;
3870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got a PEER DEL request\n");
3871 /* Sanity check for client registration */
3872 if (NULL == (c = client_get (client)))
3875 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3878 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3879 /* Sanity check for message size */
3880 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3883 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3887 /* Tunnel exists? */
3888 tid = ntohl (peer_msg->tunnel_id);
3889 t = tunnel_get_by_local_id (c, tid);
3893 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: on tunnel %X\n", t->id.tid);
3898 /* Does client own tunnel? */
3899 if (t->client->handle != client)
3902 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for peer %s\n",
3907 GNUNET_i2s (&peer_msg->peer));
3908 /* Is the peer in the tunnel? */
3910 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
3911 if (NULL == peer_info)
3914 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3918 /* Ok, delete peer from tunnel */
3919 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3920 &peer_msg->peer.hashPubKey);
3922 send_destroy_path (t, peer_info->id);
3923 tunnel_delete_peer (t, peer_info->id);
3924 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3930 * Handler for connection requests to new peers by type
3932 * @param cls closure
3933 * @param client identification of the client
3934 * @param message the actual message (ConnectPeerByType)
3937 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3938 const struct GNUNET_MessageHeader *message)
3940 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3941 struct MeshClient *c;
3942 struct MeshTunnel *t;
3943 GNUNET_HashCode hash;
3944 MESH_TunnelNumber tid;
3946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3947 /* Sanity check for client registration */
3948 if (NULL == (c = client_get (client)))
3951 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3955 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3956 /* Sanity check for message size */
3957 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3958 ntohs (connect_msg->header.size))
3961 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3965 /* Tunnel exists? */
3966 tid = ntohl (connect_msg->tunnel_id);
3967 t = tunnel_get_by_local_id (c, tid);
3971 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3975 /* Does client own tunnel? */
3976 if (t->client->handle != client)
3979 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3983 /* Do WE have the service? */
3984 t->type = ntohl (connect_msg->type);
3985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3986 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3987 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3990 /* Yes! Fast forward, add ourselves to the tunnel and send the
3991 * good news to the client, and alert the destination client of
3992 * an incoming tunnel.
3994 struct GNUNET_MESH_TunnelNotification cmsg;
3995 struct MeshClient *c;
3997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3998 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3999 peer_info_get (&my_full_id),
4000 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
4003 send_client_peer_connected (t, myid);
4004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
4005 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4007 /* FIXME implement a proper handling of this case,
4008 a client differentiation mechanism */
4009 cmsg.header.size = htons (sizeof (cmsg));
4010 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
4011 cmsg.peer = my_full_id;
4012 t->local_tid_dest = next_local_tid++;
4013 cmsg.tunnel_id = htonl (t->local_tid_dest);
4014 c = (struct MeshClient *) GNUNET_CONTAINER_multihashmap_get(applications,
4017 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4018 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
4019 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4020 GNUNET_SERVER_notification_context_unicast (nc, c->handle, &cmsg.header,
4026 /* Ok, lets find a peer offering the service */
4027 if (NULL != t->dht_get_type)
4029 GNUNET_DHT_get_stop (t->dht_get_type);
4031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
4032 GNUNET_h2s (&hash));
4034 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
4035 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
4036 GNUNET_DHT_RO_RECORD_ROUTE |
4037 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
4038 &dht_get_type_handler, t);
4040 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4046 * Handler for client traffic directed to one peer
4048 * @param cls closure
4049 * @param client identification of the client
4050 * @param message the actual message
4053 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4054 const struct GNUNET_MessageHeader *message)
4056 struct MeshClient *c;
4057 struct MeshTunnel *t;
4058 struct MeshPeerInfo *pi;
4059 struct GNUNET_MESH_Unicast *data_msg;
4060 MESH_TunnelNumber tid;
4063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4064 "MESH: Got a unicast request from a client!\n");
4066 /* Sanity check for client registration */
4067 if (NULL == (c = client_get (client)))
4070 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4073 data_msg = (struct GNUNET_MESH_Unicast *) message;
4074 /* Sanity check for message size */
4075 size = ntohs (message->size);
4076 if (sizeof (struct GNUNET_MESH_Unicast) +
4077 sizeof (struct GNUNET_MessageHeader) > size)
4080 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4084 /* Tunnel exists? */
4085 tid = ntohl (data_msg->tid);
4086 t = tunnel_get_by_local_id (c, tid);
4090 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4094 /* Is it a local tunnel? Then, does client own the tunnel? */
4095 if (NULL != t->client && NULL != t->client->handle &&
4096 t->client->handle != client)
4099 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4103 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
4104 &data_msg->destination.hashPubKey);
4105 /* Is the selected peer in the tunnel? */
4109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4113 /* Ok, everything is correct, send the message
4114 * (pretend we got it from a mesh peer)
4117 char buf[ntohs (message->size)];
4118 struct GNUNET_MESH_Unicast *copy;
4120 /* Work around const limitation */
4121 copy = (struct GNUNET_MESH_Unicast *) buf;
4122 memcpy (buf, data_msg, size);
4123 copy->oid = my_full_id;
4124 copy->tid = htonl (t->id.tid);
4125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4126 "MESH: calling generic handler...\n");
4127 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4129 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4135 * Handler for client traffic directed to the origin
4137 * @param cls closure
4138 * @param client identification of the client
4139 * @param message the actual message
4142 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4143 const struct GNUNET_MessageHeader *message)
4145 struct GNUNET_MESH_ToOrigin *data_msg;
4146 struct GNUNET_PeerIdentity id;
4147 struct MeshClient *c;
4148 struct MeshTunnel *t;
4149 MESH_TunnelNumber tid;
4152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4153 "MESH: Got a ToOrigin request from a client!\n");
4155 /* Sanity check for client registration */
4156 if (NULL == (c = client_get (client)))
4159 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4162 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4163 /* Sanity check for message size */
4164 size = ntohs (message->size);
4165 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4166 sizeof (struct GNUNET_MessageHeader) > size)
4169 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4173 /* Tunnel exists? */
4174 tid = ntohl (data_msg->tid);
4175 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4178 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4181 t = tunnel_get_by_local_id (c, tid);
4185 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4189 /* It shouldn't be a local tunnel. */
4190 if (NULL != t->client)
4193 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4196 GNUNET_PEER_resolve (t->id.oid, &id);
4198 /* Ok, everything is correct, send the message
4199 * (pretend we got it from a mesh peer)
4202 char buf[ntohs (message->size)];
4203 struct GNUNET_MESH_ToOrigin *copy;
4205 /* Work around const limitation */
4206 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4207 memcpy (buf, data_msg, size);
4209 copy->tid = htonl (t->id.tid);
4210 copy->sender = my_full_id;
4211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4212 "MESH: calling generic handler...\n");
4213 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4215 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4221 * Handler for client traffic directed to all peers in a tunnel
4223 * @param cls closure
4224 * @param client identification of the client
4225 * @param message the actual message
4228 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4229 const struct GNUNET_MessageHeader *message)
4231 struct MeshClient *c;
4232 struct MeshTunnel *t;
4233 struct GNUNET_MESH_Multicast *data_msg;
4234 MESH_TunnelNumber tid;
4236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4237 "MESH: Got a multicast request from a client!\n");
4239 /* Sanity check for client registration */
4240 if (NULL == (c = client_get (client)))
4243 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4246 data_msg = (struct GNUNET_MESH_Multicast *) message;
4247 /* Sanity check for message size */
4248 if (sizeof (struct GNUNET_MESH_Multicast) +
4249 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4252 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4256 /* Tunnel exists? */
4257 tid = ntohl (data_msg->tid);
4258 t = tunnel_get_by_local_id (c, tid);
4262 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4266 /* Does client own tunnel? */
4267 if (t->client->handle != client)
4270 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4275 char buf[ntohs (message->size)];
4276 struct GNUNET_MESH_Multicast *copy;
4278 copy = (struct GNUNET_MESH_Multicast *) buf;
4279 memcpy (buf, message, ntohs (message->size));
4280 copy->oid = my_full_id;
4281 copy->tid = htonl (t->id.tid);
4282 copy->ttl = htonl (DEFAULT_TTL);
4283 copy->mid = htonl (t->mid + 1);
4284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4285 "MESH: calling generic handler...\n");
4286 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4289 /* receive done gets called when last copy is sent to a neighbor */
4294 * Functions to handle messages from clients
4296 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4297 {&handle_local_new_client, NULL,
4298 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4299 {&handle_local_tunnel_create, NULL,
4300 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4301 sizeof (struct GNUNET_MESH_TunnelMessage)},
4302 {&handle_local_tunnel_destroy, NULL,
4303 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4304 sizeof (struct GNUNET_MESH_TunnelMessage)},
4305 {&handle_local_connect_add, NULL,
4306 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4307 sizeof (struct GNUNET_MESH_PeerControl)},
4308 {&handle_local_connect_del, NULL,
4309 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4310 sizeof (struct GNUNET_MESH_PeerControl)},
4311 {&handle_local_connect_by_type, NULL,
4312 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4313 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4314 {&handle_local_unicast, NULL,
4315 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4316 {&handle_local_to_origin, NULL,
4317 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4318 {&handle_local_multicast, NULL,
4319 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4325 * To be called on core init/fail.
4327 * @param cls service closure
4328 * @param server handle to the server for this service
4329 * @param identity the public identity of this peer
4332 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4333 const struct GNUNET_PeerIdentity *identity)
4335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
4336 core_handle = server;
4337 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
4341 GNUNET_SCHEDULER_shutdown ();
4347 * Method called whenever a given peer connects.
4349 * @param cls closure
4350 * @param peer peer identity this notification is about
4351 * @param atsi performance data for the connection
4352 * @param atsi_count number of records in 'atsi'
4355 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4356 const struct GNUNET_ATS_Information *atsi,
4357 unsigned int atsi_count)
4359 struct MeshPeerInfo *peer_info;
4360 struct MeshPeerPath *path;
4362 #if MESH_DEBUG_CONNECTION
4363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
4364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4365 GNUNET_i2s (&my_full_id));
4367 peer_info = peer_info_get (peer);
4368 if (myid == peer_info->id)
4370 #if MESH_DEBUG_CONNECTION
4371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4375 #if MESH_DEBUG_CONNECTION
4378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", GNUNET_i2s (peer));
4381 path = path_new (2);
4382 path->peers[0] = myid;
4383 path->peers[1] = peer_info->id;
4384 GNUNET_PEER_change_rc (myid, 1);
4385 GNUNET_PEER_change_rc (peer_info->id, 1);
4386 peer_info_add_path (peer_info, path, GNUNET_YES);
4391 * Method called whenever a peer disconnects.
4393 * @param cls closure
4394 * @param peer peer identity this notification is about
4397 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4399 struct MeshPeerInfo *pi;
4402 #if MESH_DEBUG_CONNECTION
4403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
4405 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4411 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4413 /* TODO: notify that the transmission failed */
4414 peer_info_cancel_transmission (pi, i);
4416 peer_info_remove_path (pi, pi->id, myid);
4417 #if MESH_DEBUG_CONNECTION
4420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4427 /******************************************************************************/
4428 /************************ MAIN FUNCTIONS ****************************/
4429 /******************************************************************************/
4432 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4434 * @param cls closure
4435 * @param key current key code
4436 * @param value value in the hash map
4437 * @return GNUNET_YES if we should continue to iterate,
4441 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4443 struct MeshTunnel *t = value;
4450 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4452 * @param cls closure
4453 * @param key current key code
4454 * @param value value in the hash map
4455 * @return GNUNET_YES if we should continue to iterate,
4459 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4461 struct MeshPeerInfo *p = value;
4463 peer_info_destroy (p);
4468 * Task run during shutdown.
4474 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4478 if (core_handle != NULL)
4480 GNUNET_CORE_disconnect (core_handle);
4483 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4484 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4485 if (dht_handle != NULL)
4487 GNUNET_DHT_disconnect (dht_handle);
4492 GNUNET_SERVER_notification_context_destroy (nc);
4495 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4497 GNUNET_SCHEDULER_cancel (announce_id_task);
4498 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4504 * Process mesh requests.
4506 * @param cls closure
4507 * @param server the initialized server
4508 * @param c configuration to use
4511 run (void *cls, struct GNUNET_SERVER_Handle *server,
4512 const struct GNUNET_CONFIGURATION_Handle *c)
4514 struct MeshPeerInfo *peer;
4515 struct MeshPeerPath *p;
4518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4519 server_handle = server;
4520 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4521 CORE_QUEUE_SIZE, /* queue size */
4522 NULL, /* Closure passed to MESH functions */
4523 &core_init, /* Call core_init once connected */
4524 &core_connect, /* Handle connects */
4525 &core_disconnect, /* remove peers on disconnects */
4526 NULL, /* Don't notify about all incoming messages */
4527 GNUNET_NO, /* For header only in notification */
4528 NULL, /* Don't notify about all outbound messages */
4529 GNUNET_NO, /* For header-only out notification */
4530 core_handlers); /* Register these handlers */
4532 if (core_handle == NULL)
4535 GNUNET_SCHEDULER_shutdown ();
4540 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4543 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4545 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4546 GNUNET_SCHEDULER_shutdown ();
4549 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4550 GNUNET_free (keyfile);
4551 if (my_private_key == NULL)
4553 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4554 _("Mesh service could not access hostkey. Exiting.\n"));
4555 GNUNET_SCHEDULER_shutdown ();
4558 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4559 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4560 &my_full_id.hashPubKey);
4561 myid = GNUNET_PEER_intern (&my_full_id);
4563 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4570 dht_handle = GNUNET_DHT_connect (c, 64);
4571 if (dht_handle == NULL)
4577 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4579 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4580 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4581 peers = GNUNET_CONTAINER_multihashmap_create (32);
4582 applications = GNUNET_CONTAINER_multihashmap_create (32);
4583 types = GNUNET_CONTAINER_multihashmap_create (32);
4585 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4586 nc = GNUNET_SERVER_notification_context_create (server_handle,
4588 GNUNET_SERVER_disconnect_notify (server_handle,
4589 &handle_local_client_disconnect, NULL);
4593 clients_tail = NULL;
4598 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4599 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4601 /* Create a peer_info for the local peer */
4602 peer = peer_info_get (&my_full_id);
4605 GNUNET_PEER_change_rc (myid, 1);
4606 peer_info_add_path (peer, p, GNUNET_YES);
4608 /* Scheduled the task to clean up when shutdown is called */
4609 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4616 * The main function for the mesh service.
4618 * @param argc number of arguments from the command line
4619 * @param argv command line arguments
4620 * @return 0 ok, 1 on error
4623 main (int argc, char *const *argv)
4627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4630 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");