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
48 #include "mesh_protocol.h"
49 #include "gnunet_dht_service.h"
50 #include "mesh_tunnel_tree.h"
52 /* TODO: move into configuration file */
53 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
54 GNUNET_TIME_UNIT_SECONDS,\
56 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
57 GNUNET_TIME_UNIT_SECONDS,\
60 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
61 GNUNET_TIME_UNIT_SECONDS,\
64 #define GET_RESTART_TIME GNUNET_TIME_relative_multiply(\
65 GNUNET_TIME_UNIT_SECONDS,\
68 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
69 GNUNET_TIME_UNIT_SECONDS,\
72 #define MESH_DEBUG_DHT GNUNET_NO
74 /******************************************************************************/
75 /************************ DATA STRUCTURES ****************************/
76 /******************************************************************************/
78 /** FWD declaration */
82 * Struct containing all info possibly needed to build a package when called
85 struct MeshDataDescriptor
87 /** ID of the tunnel this packet travels in */
88 struct MESH_TunnelID *origin;
93 /** Client that asked for the transmission, if any */
94 struct GNUNET_SERVER_Client *client;
96 /** Who was this message being sent to */
97 struct MeshPeerInfo *peer;
99 /** Ultimate destination of the packet */
100 GNUNET_PEER_Id destination;
102 /** Number of identical messages sent to different hops (multicast) */
103 unsigned int *copies;
105 /** Which handler was used to request the transmission */
106 unsigned int handler_n;
108 /** Size of the data */
111 /** Used to allow a client send more traffic to the service after a
112 * previous packet was tried to be sent to a neighbor and couldn't */
113 GNUNET_SCHEDULER_TaskIdentifier *timeout_task;
118 * Struct containing all information regarding a given peer
128 * Last time we heard from this peer
130 struct GNUNET_TIME_Absolute last_contact;
133 * Number of attempts to reconnect so far
135 int n_reconnect_attempts;
138 * Paths to reach the peer, ordered by ascending hop count
140 struct MeshPeerPath *path_head;
143 * Paths to reach the peer, ordered by ascending hop count
145 struct MeshPeerPath *path_tail;
148 * Handle to stop the DHT search for a path to this peer
150 struct GNUNET_DHT_GetHandle *dhtget;
153 * Closure given to the DHT GET
155 struct MeshPathInfo *dhtgetcls;
158 * Handles to stop queued transmissions for this peer
160 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
163 * Pointer to info stuctures used as cls for queued transmissions
165 void *infos[CORE_QUEUE_SIZE];
168 * Type of message being in each transmission
170 uint16_t types[CORE_QUEUE_SIZE];
173 * Array of tunnels this peer participates in
174 * (most probably a small amount, therefore not a hashmap)
175 * When the path to the peer changes, notify these tunnels to let them
176 * re-adjust their path trees.
178 struct MeshTunnel **tunnels;
181 * Number of tunnels this peers participates in
183 unsigned int ntunnels;
188 * Data scheduled to transmit (to local client or remote peer)
195 struct MeshQueue *next;
196 struct MeshQueue *prev;
199 * Target of the data (NULL if target is client)
201 struct MeshPeerInfo *peer;
204 * Client to send the data to (NULL if target is peer)
206 struct MeshClient *client;
209 * Size of the message to transmit
214 * How old is the data?
216 struct GNUNET_TIME_Absolute timestamp;
221 struct GNUNET_MessageHeader *data;
225 * Globally unique tunnel identification (owner + number)
226 * DO NOT USE OVER THE NETWORK
231 * Node that owns the tunnel
236 * Tunnel number to differentiate all the tunnels owned by the node oid
237 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
239 MESH_TunnelNumber tid;
243 struct MeshClient; /* FWD declaration */
246 * Struct containing all information regarding a tunnel
247 * For an intermediate node the improtant info used will be:
248 * - id Tunnel unique identification
249 * - paths[0] To know where to send it next
250 * - metainfo: ready, speeds, accounting
257 struct MESH_TunnelID id;
260 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
262 MESH_TunnelNumber local_tid;
265 * Last time the tunnel was used
267 struct GNUNET_TIME_Absolute timestamp;
270 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
271 * containing peers added by id or by type, not intermediate peers.
273 struct GNUNET_CONTAINER_MultiHashMap *peers;
276 * Number of peers that are connected and potentially ready to receive data
278 unsigned int peers_ready;
281 * Number of peers that have been added to the tunnel
283 unsigned int peers_total;
286 * Client owner of the tunnel, if any
288 struct MeshClient *client;
291 * Messages ready to transmit
293 struct MeshQueue *queue_head;
294 struct MeshQueue *queue_tail;
299 struct MeshTunnelTree *tree;
302 * Application type we are looking for in this tunnel
304 GNUNET_MESH_ApplicationType type;
307 * Used to search peers offering a service
309 struct GNUNET_DHT_GetHandle *dht_get_type;
312 * Task to keep the used paths alive
314 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
317 * Task to destroy the tunnel after timeout
319 * FIXME: merge the two? a tunnel will have either
320 * a path refresh OR a timeout, never both!
322 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
327 * Info needed to work with tunnel paths and peers
334 struct MeshTunnel *t;
337 * Neighbouring peer to whom we send the packet to
339 struct MeshPeerInfo *peer;
344 struct MeshPeerPath *path;
347 * Position in peer's transmit queue
354 * Struct containing information about a client of the service
361 struct MeshClient *next;
362 struct MeshClient *prev;
365 * Tunnels that belong to this client, indexed by local id
367 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
370 * Handle to communicate with the client
372 struct GNUNET_SERVER_Client *handle;
375 * Applications that this client has claimed to provide
377 struct GNUNET_CONTAINER_MultiHashMap *apps;
380 * Messages that this client has declared interest in
382 struct GNUNET_CONTAINER_MultiHashMap *types;
386 * ID of the client, for debug messages
395 /******************************************************************************/
396 /************************ DEBUG FUNCTIONS ****************************/
397 /******************************************************************************/
401 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
402 * @param cls string to print
403 * @param tc task context
406 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
410 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
418 /******************************************************************************/
419 /*********************** GLOBAL VARIABLES ****************************/
420 /******************************************************************************/
425 static struct MeshClient *clients;
426 static struct MeshClient *clients_tail;
429 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
431 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
434 * Tunnels incoming, indexed by MESH_TunnelNumber
435 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
437 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
440 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
442 static struct GNUNET_CONTAINER_MultiHashMap *peers;
445 * Handle to communicate with core
447 static struct GNUNET_CORE_Handle *core_handle;
450 * Handle to communicate with transport
452 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
457 static struct GNUNET_DHT_Handle *dht_handle;
462 static struct GNUNET_SERVER_Handle *server_handle;
465 * Notification context, to send messages to local clients
467 static struct GNUNET_SERVER_NotificationContext *nc;
470 * Local peer own ID (memory efficient handle)
472 static GNUNET_PEER_Id myid;
475 * Local peer own ID (full value)
477 static struct GNUNET_PeerIdentity my_full_id;
482 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
487 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
490 * Tunnel ID for the next created tunnel (global tunnel number)
492 static MESH_TunnelNumber next_tid;
495 * Tunnel ID for the next incoming tunnel (local tunnel number)
497 static MESH_TunnelNumber next_local_tid;
500 * All application types provided by this peer
502 static struct GNUNET_CONTAINER_MultiHashMap *applications;
505 * All message types clients of this peer are interested in
507 static struct GNUNET_CONTAINER_MultiHashMap *types;
510 * Task to periodically announce provided applications
512 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
515 * Task to periodically announce itself in the network
517 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
520 unsigned int next_client_id;
524 /******************************************************************************/
525 /************************ ITERATORS ****************************/
526 /******************************************************************************/
528 /* FIXME move iterators here */
531 /******************************************************************************/
532 /************************ PERIODIC FUNCTIONS ****************************/
533 /******************************************************************************/
536 * Announce iterator over for each application provided by the peer
539 * @param key current key code
540 * @param value value in the hash map
541 * @return GNUNET_YES if we should continue to
546 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
548 /* FIXME are hashes in multihash map equal on all aquitectures? */
549 GNUNET_DHT_put (dht_handle, key, 10U,
550 GNUNET_DHT_RO_RECORD_ROUTE |
551 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
552 sizeof (struct GNUNET_PeerIdentity),
553 (const char *) &my_full_id,
555 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
556 &mesh_debug, "DHT_put for app completed");
558 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
560 APP_ANNOUNCE_TIME, NULL, NULL);
567 * Periodically announce what applications are provided by local clients
570 * @param tc task context
573 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
575 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
577 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
583 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
585 announce_applications_task =
586 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
596 * Periodically announce self id in the DHT
599 * @param tc task context
602 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
604 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
606 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
610 * - Set data expiration in function of X
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
615 GNUNET_i2s (&my_full_id));
617 GNUNET_DHT_put (dht_handle, /* DHT handle */
618 &my_full_id.hashPubKey, /* Key to use */
619 10U, /* Replication level */
620 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
621 GNUNET_BLOCK_TYPE_TEST, /* Block type */
622 sizeof (my_full_id), /* Size of the data */
623 (char *) &my_full_id, /* Data itself */
624 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
625 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
627 &mesh_debug, "DHT_put for id completed");
629 NULL, /* Continuation */
630 NULL); /* Continuation closure */
633 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
638 * Function to process paths received for a new peer addition. The recorded
639 * paths form the initial tunnel, which can be optimized later.
640 * Called on each result obtained for the DHT search.
643 * @param exp when will this value expire
644 * @param key key of the result
645 * @param type type of the result
646 * @param size number of bytes in data
647 * @param data pointer to the result data
650 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
651 const GNUNET_HashCode * key,
652 const struct GNUNET_PeerIdentity *get_path,
653 unsigned int get_path_length,
654 const struct GNUNET_PeerIdentity *put_path,
655 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
656 size_t size, const void *data);
659 /******************************************************************************/
660 /****************** GENERAL HELPER FUNCTIONS ************************/
661 /******************************************************************************/
664 * Check if client has registered with the service and has not disconnected
666 * @param client the client to check
668 * @return non-NULL if client exists in the global DLL
670 static struct MeshClient *
671 client_get (struct GNUNET_SERVER_Client *client)
673 struct MeshClient *c;
678 if (c->handle == client)
687 * Checks if a given client has subscribed to certain message type
689 * @param message_type Type of message to check
690 * @param c Client to check
692 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
697 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
701 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
702 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
707 * Allow a client to send more data after transmitting a multicast message
708 * which some neighbor has not yet accepted altough a reasonable time has
711 * @param cls Closure (DataDescriptor containing the task identifier)
712 * @param tc Task Context
715 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
717 struct MeshDataDescriptor *info = cls;
719 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723 "MESH: CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
726 *(info->timeout_task) = GNUNET_SCHEDULER_NO_TASK;
727 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
732 * Search for a tunnel by global ID using full PeerIdentities
734 * @param oid owner of the tunnel
735 * @param tid global tunnel number
737 * @return tunnel handler, NULL if doesn't exist
739 static struct MeshTunnel *
740 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
744 * Notify a tunnel that a connection has broken that affects at least
747 * @param t Tunnel affected.
748 * @param p1 Peer that got disconnected from p2.
749 * @param p2 Peer that got disconnected from p1.
751 * @return Short ID of the peer disconnected (either p1 or p2).
752 * 0 if the tunnel remained unaffected.
754 static GNUNET_PEER_Id
755 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
760 * Send the message to all clients that have subscribed to its type
762 * @param msg Pointer to the message itself
763 * @return number of clients this message was sent to
766 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
767 const struct GNUNET_MessageHeader *payload)
769 struct GNUNET_PeerIdentity *oid;
770 struct MeshClient *c;
771 struct MeshTunnel *t;
772 MESH_TunnelNumber *tid;
775 char cbuf[htons (msg->size)];
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
778 type = ntohs (payload->type);
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
781 memcpy (cbuf, msg, sizeof (cbuf));
782 switch (htons (msg->type))
784 struct GNUNET_MESH_Unicast *uc;
785 struct GNUNET_MESH_Multicast *mc;
786 struct GNUNET_MESH_ToOrigin *to;
788 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
789 uc = (struct GNUNET_MESH_Unicast *) cbuf;
793 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
794 mc = (struct GNUNET_MESH_Multicast *) cbuf;
798 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
799 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
807 t = tunnel_get (oid, ntohl (*tid));
813 *tid = htonl (t->local_tid);
814 for (count = 0, c = clients; c != NULL; c = c->next)
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
817 if (client_is_subscribed (type, c))
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
821 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
822 (struct GNUNET_MessageHeader
823 *) cbuf, GNUNET_YES);
831 * Notify the client that owns the tunnel that a peer has connected to it
832 * (the requested path to it has been confirmed).
834 * @param t Tunnel whose owner to notify
835 * @param id Short id of the peer that has connected
838 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
840 struct GNUNET_MESH_PeerControl pc;
842 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
843 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
844 pc.tunnel_id = htonl (t->local_tid);
845 GNUNET_PEER_resolve (id, &pc.peer);
846 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, &pc.header,
852 * Notify all clients (not depending on registration status) that the incoming
853 * tunnel is no longer valid.
855 * @param t Tunnel that was destroyed.
858 send_clients_tunnel_destroy (struct MeshTunnel *t)
860 struct GNUNET_MESH_TunnelMessage msg;
862 msg.header.size = htons (sizeof (msg));
863 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
864 msg.tunnel_id = htonl (t->local_tid);
865 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
870 * Function called to notify a client about the socket
871 * being ready to queue more data. "buf" will be
872 * NULL and "size" zero if the socket was closed for
873 * writing in the meantime.
876 * @param size number of bytes available in buf
877 * @param buf where the callee should write the message
878 * @return number of bytes written to buf
881 send_core_create_path (void *cls, size_t size, void *buf);
885 * Function called to notify a client about the socket
886 * being ready to queue more data. "buf" will be
887 * NULL and "size" zero if the socket was closed for
888 * writing in the meantime.
890 * @param cls closure (data itself)
891 * @param size number of bytes available in buf
892 * @param buf where the callee should write the message
894 * @return number of bytes written to buf
897 send_core_data_multicast (void *cls, size_t size, void *buf);
901 * Decrements the reference counter and frees all resources if needed
903 * @param dd Data Descriptor used in a multicast message
906 data_descriptor_decrement_multicast (struct MeshDataDescriptor *dd)
908 if (0 == --(*(dd->copies)))
910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
911 if (NULL != dd->client)
913 if (GNUNET_SCHEDULER_NO_TASK != *(dd->timeout_task))
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 "MESH: cancelling client timeout (%u)...\n",
917 *(dd->timeout_task));
918 GNUNET_SCHEDULER_cancel (*(dd->timeout_task));
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
920 GNUNET_SERVER_receive_done (dd->client, GNUNET_OK);
922 GNUNET_free (dd->timeout_task);
924 GNUNET_free (dd->copies);
925 GNUNET_free (dd->data);
931 * Cancel a core transmission that was already requested and free all resources
932 * associated to the request.
934 * @param peer PeeInfo of the peer whose transmission is cancelled.
935 * @param i Position of the transmission to be cancelled.
938 peer_info_cancel_transmission (struct MeshPeerInfo *peer, unsigned int i)
940 if (NULL != peer->core_transmit[i])
942 struct MeshDataDescriptor *dd;
943 struct MeshPathInfo *path_info;
947 struct GNUNET_PeerIdentity id;
949 GNUNET_PEER_resolve (peer->id, &id);
950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
951 "MESH: Cancelling data transmission at %s [%u]\n",
952 GNUNET_i2s (&id), i);
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message type %u\n",
957 /* TODO: notify that tranmission has failed */
958 switch (peer->types[i])
960 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
961 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
962 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
965 data_descriptor_decrement_multicast (dd);
967 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
969 path_info = peer->infos[i];
970 path_destroy (path_info->path);
974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
976 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
977 peer->core_transmit[i] = NULL;
978 GNUNET_free (peer->infos[i]);
984 * Get a unused CORE slot to transmit a message to a peer. If all the slots
985 * are used, cancel one and return it's position.
987 * @param peer PeerInfo of the neighbor we want to transmit to.
989 * @return The index of an available slot to transmit to the neighbor.
992 peer_info_transmit_slot (struct MeshPeerInfo *peer)
996 for (i = 0; peer->core_transmit[i]; i++)
998 if (i == (CORE_QUEUE_SIZE - 1))
1000 /* All positions are taken! Overwriting! */
1002 peer_info_cancel_transmission (peer, 0);
1011 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1012 * and insert it in the appropiate structures if the peer is not known yet.
1014 * @param peer Full identity of the peer.
1016 * @return Existing or newly created peer info.
1018 static struct MeshPeerInfo *
1019 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1021 struct MeshPeerInfo *peer_info;
1023 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1024 if (NULL == peer_info)
1027 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1028 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1029 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1030 peer_info->id = GNUNET_PEER_intern (peer);
1038 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1039 * and insert it in the appropiate structures if the peer is not known yet.
1041 * @param peer Short identity of the peer.
1043 * @return Existing or newly created peer info.
1045 static struct MeshPeerInfo *
1046 peer_info_get_short (const GNUNET_PEER_Id peer)
1048 struct GNUNET_PeerIdentity id;
1050 GNUNET_PEER_resolve (peer, &id);
1051 return peer_info_get (&id);
1056 * Iterator to remove the tunnel from the list of tunnels a peer participates
1059 * @param cls Closure (tunnel info)
1060 * @param key GNUNET_PeerIdentity of the peer (unused)
1061 * @param value PeerInfo of the peer
1063 * @return always GNUNET_YES, to keep iterating
1066 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1068 struct MeshTunnel *t = cls;
1069 struct MeshPeerInfo *peer = value;
1072 for (i = 0; i < peer->ntunnels; i++)
1075 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1078 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1079 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1088 * Core callback to write a
1090 * @param cls Closure (MeshDataDescriptor with data in "data" member).
1091 * @param size Number of bytes available in buf.
1092 * @param buf Where the to write the message.
1094 * @return number of bytes written to buf
1097 send_core_data_raw (void *cls, size_t size, void *buf)
1099 struct MeshDataDescriptor *info = cls;
1100 struct GNUNET_MessageHeader *msg;
1103 GNUNET_assert (NULL != info);
1104 GNUNET_assert (NULL != info->data);
1105 msg = (struct GNUNET_MessageHeader *) info->data;
1106 total_size = ntohs (msg->size);
1108 if (total_size > size)
1110 struct GNUNET_PeerIdentity id;
1112 GNUNET_PEER_resolve (info->peer->id, &id);
1113 info->peer->core_transmit[info->handler_n] =
1114 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1115 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1116 size, &send_core_data_raw, info);
1119 info->peer->core_transmit[info->handler_n] = NULL;
1120 memcpy (buf, msg, total_size);
1121 GNUNET_free (info->data);
1128 * Sends an already built message to a peer, properly registrating
1129 * all used resources.
1131 * @param message Message to send. Fucntion makes a copy of it.
1132 * @param peer Short ID of the neighbor whom to send the message.
1135 send_message (const struct GNUNET_MessageHeader *message,
1136 const struct GNUNET_PeerIdentity *peer)
1138 struct MeshDataDescriptor *info;
1139 struct MeshPeerInfo *neighbor;
1140 struct MeshPeerPath *p;
1144 // GNUNET_TRANSPORT_try_connect();
1146 size = ntohs (message->size);
1147 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1148 info->data = GNUNET_malloc (size);
1149 memcpy (info->data, message, size);
1150 neighbor = peer_info_get (peer);
1151 for (p = neighbor->path_head; NULL != p; p = p->next)
1164 i = peer_info_transmit_slot (neighbor);
1165 info->handler_n = i;
1166 info->peer = neighbor;
1167 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1168 neighbor->infos[i] = info;
1169 neighbor->core_transmit[i] =
1170 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1171 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1172 size, &send_core_data_raw, info);
1178 * Sends a CREATE PATH message for a path to a peer, properly registrating
1179 * all used resources.
1181 * @param peer PeerInfo of the final peer for whom this path is being created.
1182 * @param p Path itself.
1183 * @param t Tunnel for which the path is created.
1186 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1187 struct MeshTunnel *t)
1189 struct GNUNET_PeerIdentity id;
1190 struct MeshPathInfo *path_info;
1191 struct MeshPeerInfo *neighbor;
1196 p = tree_get_path_to_peer (t->tree, peer->id);
1203 for (i = 0; i < p->length; i++)
1205 if (p->peers[i] == myid)
1208 if (i >= p->length - 1)
1214 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1216 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1217 path_info->path = p;
1219 neighbor = peer_info_get (&id);
1220 path_info->peer = neighbor;
1221 path_info->pos = peer_info_transmit_slot (neighbor);
1222 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1223 neighbor->infos[path_info->pos] = path_info;
1224 neighbor->core_transmit[path_info->pos] = GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
1227 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1229 sizeof (struct GNUNET_MESH_ManipulatePath) + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1230 &send_core_create_path, /* callback */
1231 path_info); /* cls */
1236 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1238 * @param t Tunnel whose path to destroy.
1239 * @param destination Short ID of the peer to whom the path to destroy.
1242 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1244 struct MeshPeerPath *p;
1247 p = tree_get_path_to_peer (t->tree, destination);
1253 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1254 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1256 struct GNUNET_MESH_ManipulatePath *msg;
1257 struct GNUNET_PeerIdentity *pi;
1261 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1262 msg->header.size = htons (size);
1263 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1264 msg->tid = htonl (t->id.tid);
1265 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1266 for (i = 0; i < p->length; i++)
1268 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1270 send_message (&msg->header, path_get_first_hop (t->tree, destination));
1277 * Try to establish a new connection to this peer.
1278 * Use the best path for the given tunnel.
1279 * If the peer doesn't have any path to it yet, try to get one.
1280 * If the peer already has some path, send a CREATE PATH towards it.
1282 * @param peer PeerInfo of the peer.
1283 * @param t Tunnel for which to create the path, if possible.
1286 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1288 struct MeshPeerPath *p;
1289 struct MeshPathInfo *path_info;
1291 if (NULL != peer->path_head)
1293 p = tree_get_path_to_peer (t->tree, peer->id);
1302 send_create_path (peer, p, t);
1307 send_client_peer_connected (t, myid);
1310 else if (NULL == peer->dhtget)
1312 struct GNUNET_PeerIdentity id;
1314 GNUNET_PEER_resolve (peer->id, &id);
1315 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1316 path_info->peer = peer;
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319 "MESH: Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1320 peer->dhtgetcls = path_info;
1321 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1322 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1323 GNUNET_BLOCK_TYPE_TEST, /* type */
1324 &id.hashPubKey, /* key to search */
1325 4, /* replication level */
1326 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1327 0, /* xquery bits */
1328 &dht_get_id_handler, path_info);
1330 /* Otherwise, there is no path but the DHT get is already started. */
1335 * Task to delay the connection of a peer
1337 * @param cls Closure (path info with tunnel and peer to connect).
1338 * Will be free'd on exection.
1339 * @param tc TaskContext
1342 peer_info_connect_task (void *cls,
1343 const struct GNUNET_SCHEDULER_TaskContext *tc)
1345 struct MeshPathInfo *path_info = cls;
1347 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1352 peer_info_connect (path_info->peer, path_info->t);
1358 * Destroy the peer_info and free any allocated resources linked to it
1360 * @param pi The peer_info to destroy.
1362 * @return GNUNET_OK on success
1365 peer_info_destroy (struct MeshPeerInfo *pi)
1367 struct GNUNET_PeerIdentity id;
1368 struct MeshPeerPath *p;
1369 struct MeshPeerPath *nextp;
1372 GNUNET_PEER_resolve (pi->id, &id);
1373 GNUNET_PEER_change_rc (pi->id, -1);
1376 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1379 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1380 "MESH: removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1382 if (NULL != pi->dhtget)
1384 GNUNET_DHT_get_stop (pi->dhtget);
1385 GNUNET_free (pi->dhtgetcls);
1387 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1389 peer_info_cancel_transmission (pi, i);
1395 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1405 * Remove all paths that rely on a direct connection between p1 and p2
1406 * from the peer itself and notify all tunnels about it.
1408 * @param peer PeerInfo of affected peer.
1409 * @param p1 GNUNET_PEER_Id of one peer.
1410 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1413 * TODO: optimize (see below)
1416 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1419 struct MeshPeerPath *p;
1420 struct MeshPeerPath *aux;
1421 struct MeshPeerInfo *peer_d;
1423 unsigned int destroyed;
1429 p = peer->path_head;
1433 for (i = 0; i < (p->length - 1); i++)
1435 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1436 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1438 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1449 for (i = 0; i < peer->ntunnels; i++)
1451 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1453 * Problem: one or more peers have been deleted from the tunnel tree.
1454 * We don't know who they are to try to add them again.
1455 * We need to try to find a new path for each of the disconnected peers.
1456 * Some of them might already have a path to reach them that does not
1457 * involve p1 and p2. Adding all anew might render in a better tree than
1458 * the trivial immediate fix.
1460 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1461 * its children will be reachable trough him.
1463 peer_d = peer_info_get_short (d);
1466 for (p = peer_d->path_head; NULL != p; p = p->next)
1468 if ((cost = path_get_cost (peer->tunnels[i]->tree, p)) < best)
1476 /* No callback, as peer will be already disconnected */
1477 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1481 peer_info_connect (peer_d, peer->tunnels[i]);
1488 * Add the path to the peer and update the path used to reach it in case this
1491 * @param peer_info Destination peer to add the path to.
1492 * @param path New path to add. Last peer must be the peer in arg 1.
1493 * Path will be either used of freed if already known.
1494 * @param trusted Do we trust that this path is real?
1497 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1500 struct MeshPeerPath *aux;
1504 if ((NULL == peer_info) || (NULL == path))
1507 path_destroy (path);
1510 if (path->length <= 2 && GNUNET_NO == trusted)
1512 /* Only allow CORE to tell us about direct paths */
1513 path_destroy (path);
1516 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1517 for (l = 1; l < path->length; l++)
1519 if (path->peers[l] == myid)
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shortening path by %u\n", l);
1522 for (l2 = 0; l2 < path->length - l; l2++)
1524 path->peers[l2] = path->peers[l + l2];
1529 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1534 struct GNUNET_PeerIdentity id;
1536 GNUNET_PEER_resolve (peer_info->id, &id);
1537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: adding path [%u] to peer %s\n",
1538 path->length, GNUNET_i2s (&id));
1541 l = path_get_length (path);
1548 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1549 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1551 l2 = path_get_length (aux);
1554 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1555 peer_info->path_tail, aux, path);
1560 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1562 path_destroy (path);
1567 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1574 * Add the path to the origin peer and update the path used to reach it in case
1575 * this is the shortest.
1576 * The path is given in peer_info -> destination, therefore we turn the path
1577 * upside down first.
1579 * @param peer_info Peer to add the path to, being the origin of the path.
1580 * @param path New path to add after being inversed.
1581 * @param trusted Do we trust that this path is real?
1584 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1585 struct MeshPeerPath *path, int trusted)
1588 peer_info_add_path (peer_info, path, trusted);
1593 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1594 * to obtain a local peer -> destination path and interning the peer ids.
1596 * @return Newly allocated and created path
1598 static struct MeshPeerPath *
1599 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1600 unsigned int get_path_length,
1601 const struct GNUNET_PeerIdentity *put_path,
1602 unsigned int put_path_length)
1604 struct MeshPeerPath *p;
1610 GNUNET_PEER_change_rc (myid, 1);
1611 i = get_path_length;
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1613 for (i--; i >= 0; i--)
1615 id = GNUNET_PEER_intern (&get_path[i]);
1616 if (p->length > 0 && id == p->peers[p->length - 1])
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1619 GNUNET_PEER_change_rc (id, -1);
1623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Adding from GET: %s.\n",
1624 GNUNET_i2s (&get_path[i]));
1626 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1627 p->peers[p->length - 1] = id;
1630 i = put_path_length;
1631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1632 for (i--; i >= 0; i--)
1634 id = GNUNET_PEER_intern (&put_path[i]);
1637 /* PUT path went through us, so discard the path up until now and start
1638 * from here to get a much shorter (and loop-free) path.
1643 if (p->length > 0 && id == p->peers[p->length - 1])
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1646 GNUNET_PEER_change_rc (id, -1);
1650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Adding from PUT: %s.\n",
1651 GNUNET_i2s (&put_path[i]));
1653 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1654 p->peers[p->length - 1] = id;
1658 if (get_path_length > 0)
1659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (first of GET: %s)\n",
1660 GNUNET_i2s (&get_path[0]));
1661 if (put_path_length > 0)
1662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (first of PUT: %s)\n",
1663 GNUNET_i2s (&put_path[0]));
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: In total: %d hops\n",
1666 for (i = 0; i < p->length; i++)
1668 struct GNUNET_PeerIdentity peer_id;
1670 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %u: %s\n", p->peers[i],
1672 GNUNET_i2s (&peer_id));
1680 * Send keepalive packets for a peer
1682 * @param cls Closure (tunnel for which to send the keepalive).
1683 * @param tc Notification context.
1685 * TODO: implement explicit multicast keepalive?
1688 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1692 * Search for a tunnel among the incoming tunnels
1694 * @param tid the local id of the tunnel
1696 * @return tunnel handler, NULL if doesn't exist
1698 static struct MeshTunnel *
1699 tunnel_get_incoming (MESH_TunnelNumber tid)
1701 GNUNET_HashCode hash;
1703 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1704 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1705 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1710 * Search for a tunnel among the tunnels for a client
1712 * @param c the client whose tunnels to search in
1713 * @param tid the local id of the tunnel
1715 * @return tunnel handler, NULL if doesn't exist
1717 static struct MeshTunnel *
1718 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1720 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1722 return tunnel_get_incoming (tid);
1726 GNUNET_HashCode hash;
1728 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1729 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1735 * Search for a tunnel by global ID using PEER_ID
1737 * @param pi owner of the tunnel
1738 * @param tid global tunnel number
1740 * @return tunnel handler, NULL if doesn't exist
1742 static struct MeshTunnel *
1743 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1745 struct MESH_TunnelID id;
1746 GNUNET_HashCode hash;
1751 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1752 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1757 * Search for a tunnel by global ID using full PeerIdentities
1759 * @param oid owner of the tunnel
1760 * @param tid global tunnel number
1762 * @return tunnel handler, NULL if doesn't exist
1764 static struct MeshTunnel *
1765 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1767 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1772 * Callback used to notify a client owner of a tunnel that a peer has
1773 * disconnected, most likely because of a path change.
1775 * @param cls Closure (tunnel this notification is about).
1776 * @param peer_id Short ID of disconnected peer.
1779 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
1781 struct MeshTunnel *t = cls;
1782 struct MeshPeerInfo *peer;
1783 struct MeshPathInfo *path_info;
1785 if (NULL != t->client && NULL != nc)
1787 struct GNUNET_MESH_PeerControl msg;
1789 msg.header.size = htons (sizeof (msg));
1790 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1791 msg.tunnel_id = htonl (t->local_tid);
1792 GNUNET_PEER_resolve (peer_id, &msg.peer);
1793 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1794 &msg.header, GNUNET_NO);
1796 peer = peer_info_get_short (peer_id);
1797 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1798 path_info->peer = peer;
1800 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
1805 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1806 * needed rescources.
1807 * If peer already exists, reevaluate shortest path and change if different.
1809 * @param t Tunnel we want to add a new peer to
1810 * @param peer PeerInfo of the peer being added
1814 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1816 struct GNUNET_PeerIdentity id;
1817 struct MeshPeerPath *best_p;
1818 struct MeshPeerPath *p;
1819 unsigned int best_cost;
1822 GNUNET_PEER_resolve (peer->id, &id);
1824 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
1827 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1828 GNUNET_assert (GNUNET_OK ==
1829 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
1831 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1834 if (NULL != (p = peer->path_head))
1837 best_cost = path_get_cost (t->tree, p);
1840 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1847 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
1848 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1849 t->path_refresh_task =
1850 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
1854 /* Start a DHT get if necessary */
1855 peer_info_connect (peer, t);
1860 * Add a path to a tunnel which we don't own, just to remember the next hop.
1861 * If destination node was already in the tunnel, the first hop information
1862 * will be replaced with the new path.
1864 * @param t Tunnel we want to add a new peer to
1865 * @param p Path to add
1866 * @param own_pos Position of local node in path.
1870 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
1871 unsigned int own_pos)
1873 struct GNUNET_PeerIdentity id;
1875 GNUNET_assert (0 != own_pos);
1876 tree_add_path (t->tree, p, NULL, NULL);
1877 if (tree_get_me (t->tree) == 0)
1878 tree_set_me (t->tree, p->peers[own_pos]);
1879 if (own_pos < p->length - 1)
1881 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1882 tree_update_first_hops (t->tree, tree_get_me (t->tree), &id);
1888 * Notifies a tunnel that a connection has broken that affects at least
1889 * some of its peers. Sends a notification towards the root of the tree.
1890 * In case the peer is the owner of the tree, notifies the client that owns
1891 * the tunnel and tries to reconnect.
1893 * @param t Tunnel affected.
1894 * @param p1 Peer that got disconnected from p2.
1895 * @param p2 Peer that got disconnected from p1.
1897 * @return Short ID of the peer disconnected (either p1 or p2).
1898 * 0 if the tunnel remained unaffected.
1900 static GNUNET_PEER_Id
1901 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1907 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
1909 if (myid != p1 && myid != p2)
1915 if (tree_get_predecessor (t->tree) != 0)
1917 /* We are the peer still connected, notify owner of the disconnection. */
1918 struct GNUNET_MESH_PathBroken msg;
1919 struct GNUNET_PeerIdentity neighbor;
1921 msg.header.size = htons (sizeof (msg));
1922 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1923 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1924 msg.tid = htonl (t->id.tid);
1925 msg.peer1 = my_full_id;
1926 GNUNET_PEER_resolve (pid, &msg.peer2);
1927 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1928 send_message (&msg.header, &neighbor);
1935 struct MeshMulticastData
1937 struct MeshTunnel *t;
1939 GNUNET_SCHEDULER_TaskIdentifier *task;
1941 unsigned int *reference_counter;
1950 * Send a multicast packet to a neighbor.
1953 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
1955 struct MeshMulticastData *mdata = cls;
1956 struct MeshDataDescriptor *info;
1957 struct GNUNET_PeerIdentity neighbor;
1960 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1962 info->data = mdata->data;
1963 info->size = mdata->data_len;
1964 info->copies = mdata->reference_counter;
1965 (*(mdata->reference_counter))++;
1967 if (NULL != mdata->t->client)
1969 info->client = mdata->t->client->handle;
1970 info->timeout_task = mdata->task;
1972 info->destination = neighbor_id;
1973 GNUNET_PEER_resolve (neighbor_id, &neighbor);
1975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending to %s...\n",
1976 GNUNET_i2s (&neighbor));
1978 info->peer = peer_info_get (&neighbor);
1979 GNUNET_assert (NULL != info->peer);
1980 i = peer_info_transmit_slot (info->peer);
1981 info->handler_n = i;
1982 info->peer->infos[i] = info;
1983 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1984 info->peer->core_transmit[i] =
1985 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1986 GNUNET_TIME_UNIT_FOREVER_REL,
1987 &neighbor, info->size,
1988 &send_core_data_multicast, info);
1992 * Send a message in a tunnel in multicast, sending a copy to each child node
1993 * down the local one in the tunnel tree.
1995 * @param t Tunnel in which to send the data.
1996 * @param msg Message to be sent
1999 tunnel_send_multicast (struct MeshTunnel *t,
2000 const struct GNUNET_MessageHeader *msg)
2002 struct MeshMulticastData *mdata;
2005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2006 "MESH: sending a multicast packet...\n");
2008 GNUNET_assert (tree_get_me (t->tree) != 0);
2009 mdata = GNUNET_malloc (sizeof (struct MeshMulticastData));
2010 mdata->data_len = ntohs (msg->size);
2011 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2012 mdata->data = GNUNET_malloc (mdata->data_len);
2014 memcpy (mdata->data, msg, mdata->data_len);
2015 if (NULL != t->client)
2017 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2019 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2023 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2024 if (*(mdata->reference_counter) == 0)
2026 GNUNET_free (mdata->data);
2027 GNUNET_free (mdata->reference_counter);
2028 if (NULL != mdata->task)
2030 GNUNET_free (mdata->task);
2033 GNUNET_free (mdata);
2035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2036 "MESH: sending a multicast packet done\n");
2043 * Send a message to all peers in this tunnel that the tunnel is no longer
2046 * @param t The tunnel whose peers to notify.
2049 tunnel_send_destroy (struct MeshTunnel *t)
2051 struct GNUNET_MESH_TunnelDestroy msg;
2053 msg.header.size = htons (sizeof (msg));
2054 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2055 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2056 msg.tid = htonl (t->id.tid);
2057 tunnel_send_multicast (t, &msg.header);
2063 * Destroy the tunnel and free any allocated resources linked to it
2065 * @param t the tunnel to destroy
2067 * @return GNUNET_OK on success
2070 tunnel_destroy (struct MeshTunnel *t)
2072 struct MeshClient *c;
2073 struct MeshQueue *q;
2074 struct MeshQueue *qn;
2075 GNUNET_HashCode hash;
2084 struct GNUNET_PeerIdentity id;
2086 GNUNET_PEER_resolve (t->id.oid, &id);
2087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel %s [%x]\n",
2088 GNUNET_i2s (&id), t->id.tid);
2090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2094 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2095 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2100 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2102 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
2106 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2108 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2109 GNUNET_break (GNUNET_YES ==
2110 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash,
2113 if (NULL != t->peers)
2115 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2117 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2122 if (NULL != q->data)
2123 GNUNET_free (q->data);
2127 /* TODO cancel core transmit ready in case it was active */
2129 tree_destroy (t->tree);
2130 if (NULL != t->dht_get_type)
2131 GNUNET_DHT_get_stop (t->dht_get_type);
2132 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2133 GNUNET_SCHEDULER_cancel (t->timeout_task);
2134 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2135 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2142 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2143 * that are no longer needed, as well as nodes of no longer reachable peers.
2144 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2146 * @param t Tunnel from which to remove the path.
2147 * @param p Peer which should be removed.
2150 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2152 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2158 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2159 * client when the client disconnects.
2161 * @param cls closure (client that is disconnecting)
2162 * @param key the hash of the local tunnel id (used to access the hashmap)
2163 * @param value the value stored at the key (tunnel to destroy)
2165 * @return GNUNET_OK on success
2168 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2170 struct MeshTunnel *t = value;
2173 r = tunnel_destroy (t);
2179 * Timeout function, destroys tunnel if called
2181 * @param cls Closure (tunnel to destroy).
2182 * @param tc TaskContext
2185 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2187 struct MeshTunnel *t = cls;
2189 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2191 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2196 * Resets the tunnel timeout. Starts it if no timeout was running.
2198 * @param t Tunnel whose timeout to reset.
2201 tunnel_reset_timeout (struct MeshTunnel *t)
2203 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2204 GNUNET_SCHEDULER_cancel (t->timeout_task);
2206 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2207 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2211 /******************************************************************************/
2212 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2213 /******************************************************************************/
2216 * Function called to notify a client about the socket
2217 * being ready to queue more data. "buf" will be
2218 * NULL and "size" zero if the socket was closed for
2219 * writing in the meantime.
2221 * @param cls closure
2222 * @param size number of bytes available in buf
2223 * @param buf where the callee should write the message
2224 * @return number of bytes written to buf
2227 send_core_create_path (void *cls, size_t size, void *buf)
2229 struct MeshPathInfo *info = cls;
2230 struct GNUNET_MESH_ManipulatePath *msg;
2231 struct GNUNET_PeerIdentity *peer_ptr;
2232 struct MeshPeerInfo *peer = info->peer;
2233 struct MeshTunnel *t = info->t;
2234 struct MeshPeerPath *p = info->path;
2238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATE PATH sending...\n");
2240 sizeof (struct GNUNET_MESH_ManipulatePath) +
2241 p->length * sizeof (struct GNUNET_PeerIdentity);
2243 if (size < size_needed || NULL == buf)
2245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n", size,
2249 info->peer->core_transmit[info->pos] =
2250 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2251 GNUNET_TIME_UNIT_FOREVER_REL,
2252 path_get_first_hop (t->tree,
2254 size_needed, &send_core_create_path,
2258 info->peer->core_transmit[info->pos] = NULL;
2261 struct GNUNET_PeerIdentity id;
2263 GNUNET_PEER_resolve (peer->id, &id);
2264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2265 "MESH: setting core_transmit %s [%u] to NULL\n",
2266 GNUNET_i2s (&id), info->pos);
2269 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2270 msg->header.size = htons (size_needed);
2271 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2272 msg->tid = ntohl (t->id.tid);
2274 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2275 for (i = 0; i < p->length; i++)
2277 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2284 "MESH: CREATE PATH (%u bytes long) sent!\n", size_needed);
2290 * Function called to notify a client about the socket
2291 * being ready to queue more data. "buf" will be
2292 * NULL and "size" zero if the socket was closed for
2293 * writing in the meantime.
2295 * @param cls closure (data itself)
2296 * @param size number of bytes available in buf
2297 * @param buf where the callee should write the message
2299 * @return number of bytes written to buf
2302 send_core_data_multicast (void *cls, size_t size, void *buf)
2304 struct MeshDataDescriptor *info = cls;
2307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2308 GNUNET_assert (NULL != info);
2309 GNUNET_assert (NULL != info->peer);
2310 total_size = info->size;
2311 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2313 if (total_size > size)
2316 struct GNUNET_PeerIdentity id;
2318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2319 "MESH: Multicast: retransmitting... (%u/%u)\n", size,
2321 GNUNET_PEER_resolve (info->peer->id, &id);
2322 info->peer->core_transmit[info->handler_n] =
2323 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2324 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2326 &send_core_data_multicast, info);
2329 info->peer->core_transmit[info->handler_n] = NULL;
2330 info->peer->infos[info->handler_n] = NULL;
2331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2332 memcpy (buf, info->data, total_size);
2335 struct GNUNET_MESH_Multicast *mc;
2336 struct GNUNET_MessageHeader *mh;
2339 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2341 mc = (struct GNUNET_MESH_Multicast *) mh;
2342 mh = (struct GNUNET_MessageHeader *) &mc[1];
2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2344 "MESH: multicast, payload type %u\n", ntohs (mh->type));
2345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2346 "MESH: multicast, payload size %u\n", ntohs (mh->size));
2350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type %u\n",
2355 data_descriptor_decrement_multicast (info);
2356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2364 * Function called to notify a client about the socket
2365 * being ready to queue more data. "buf" will be
2366 * NULL and "size" zero if the socket was closed for
2367 * writing in the meantime.
2369 * @param cls closure (MeshDataDescriptor)
2370 * @param size number of bytes available in buf
2371 * @param buf where the callee should write the message
2372 * @return number of bytes written to buf
2375 send_core_path_ack (void *cls, size_t size, void *buf)
2377 struct MeshDataDescriptor *info = cls;
2378 struct GNUNET_MESH_PathACK *msg = buf;
2380 GNUNET_assert (NULL != info);
2383 info->peer->core_transmit[info->handler_n] = NULL;
2385 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2390 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2391 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2392 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2393 msg->tid = htonl (info->origin->tid);
2394 msg->peer_id = my_full_id;
2396 /* TODO add signature */
2398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2399 return sizeof (struct GNUNET_MESH_PathACK);
2403 /******************************************************************************/
2404 /******************** MESH NETWORK HANDLERS **************************/
2405 /******************************************************************************/
2409 * Core handler for path creation
2411 * @param cls closure
2412 * @param message message
2413 * @param peer peer identity this notification is about
2414 * @param atsi performance data
2415 * @param atsi_count number of records in 'atsi'
2417 * @return GNUNET_OK to keep the connection open,
2418 * GNUNET_SYSERR to close it (signal serious error)
2421 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2422 const struct GNUNET_MessageHeader *message,
2423 const struct GNUNET_ATS_Information *atsi,
2424 unsigned int atsi_count)
2426 unsigned int own_pos;
2429 MESH_TunnelNumber tid;
2430 struct GNUNET_MESH_ManipulatePath *msg;
2431 struct GNUNET_PeerIdentity *pi;
2432 GNUNET_HashCode hash;
2433 struct MeshPeerPath *path;
2434 struct MeshPeerInfo *dest_peer_info;
2435 struct MeshPeerInfo *orig_peer_info;
2436 struct MeshTunnel *t;
2438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2439 "MESH: Received a path create msg [%s]\n",
2440 GNUNET_i2s (&my_full_id));
2441 size = ntohs (message->size);
2442 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2444 GNUNET_break_op (0);
2448 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2449 if (size % sizeof (struct GNUNET_PeerIdentity))
2451 GNUNET_break_op (0);
2454 size /= sizeof (struct GNUNET_PeerIdentity);
2457 GNUNET_break_op (0);
2460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
2461 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2463 tid = ntohl (msg->tid);
2464 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2465 t = tunnel_get (pi, tid);
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2467 "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2471 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2472 t->id.oid = GNUNET_PEER_intern (pi);
2474 while (NULL != tunnel_get_incoming (next_local_tid))
2475 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2476 t->local_tid = next_local_tid++;
2477 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2478 t->tree = tree_new (t->id.oid);
2480 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2482 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2483 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2489 tunnel_reset_timeout (t);
2490 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2492 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2493 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2501 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2502 if (NULL == dest_peer_info)
2504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2505 "MESH: Creating PeerInfo for destination.\n");
2506 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2507 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2508 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2510 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2512 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2513 if (NULL == orig_peer_info)
2515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2516 "MESH: Creating PeerInfo for origin.\n");
2517 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2518 orig_peer_info->id = GNUNET_PEER_intern (pi);
2519 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2520 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2523 path = path_new (size);
2525 for (i = 0; i < size; i++)
2527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
2528 GNUNET_i2s (&pi[i]));
2529 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2530 if (path->peers[i] == myid)
2533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
2536 /* cannot be self, must be 'not found' */
2537 /* create path: self not found in path through self */
2538 GNUNET_break_op (0);
2539 path_destroy (path);
2540 /* FIXME error. destroy tunnel? leave for timeout? */
2543 tunnel_add_path (t, path, own_pos);
2544 if (own_pos == size - 1)
2546 /* It is for us! Send ack. */
2547 struct GNUNET_MESH_TunnelNotification cmsg;
2548 struct MeshDataDescriptor *info;
2551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
2552 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2553 if (NULL == t->peers)
2554 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2555 GNUNET_break (GNUNET_OK ==
2556 GNUNET_CONTAINER_multihashmap_put (t->peers,
2557 &my_full_id.hashPubKey,
2560 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2561 /* FIXME use send_message */
2562 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2563 info->origin = &t->id;
2564 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2565 GNUNET_assert (NULL != info->peer);
2566 j = peer_info_transmit_slot (info->peer);
2567 info->handler_n = j;
2568 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2569 info->peer->infos[j] = info;
2570 info->peer->core_transmit[j] =
2571 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2572 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2573 sizeof (struct GNUNET_MESH_PathACK),
2574 &send_core_path_ack, info);
2575 cmsg.header.size = htons (sizeof (cmsg));
2576 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2577 GNUNET_PEER_resolve (t->id.oid, &cmsg.peer);
2578 cmsg.tunnel_id = htonl (t->local_tid);
2579 GNUNET_SERVER_notification_context_broadcast (nc, &cmsg.header, GNUNET_NO);
2583 struct MeshPeerPath *path2;
2585 /* It's for somebody else! Retransmit. */
2586 path2 = path_duplicate (path);
2587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting.\n");
2588 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
2589 path2 = path_duplicate (path);
2590 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
2591 send_create_path (dest_peer_info, path, t);
2598 * Core handler for path destruction
2600 * @param cls closure
2601 * @param message message
2602 * @param peer peer identity this notification is about
2603 * @param atsi performance data
2604 * @param atsi_count number of records in 'atsi'
2606 * @return GNUNET_OK to keep the connection open,
2607 * GNUNET_SYSERR to close it (signal serious error)
2610 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2611 const struct GNUNET_MessageHeader *message,
2612 const struct GNUNET_ATS_Information *atsi,
2613 unsigned int atsi_count)
2615 struct GNUNET_MESH_ManipulatePath *msg;
2616 struct GNUNET_PeerIdentity *pi;
2617 struct MeshPeerPath *path;
2618 struct MeshTunnel *t;
2619 unsigned int own_pos;
2623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2624 "MESH: Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
2625 size = ntohs (message->size);
2626 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2628 GNUNET_break_op (0);
2632 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2633 if (size % sizeof (struct GNUNET_PeerIdentity))
2635 GNUNET_break_op (0);
2638 size /= sizeof (struct GNUNET_PeerIdentity);
2641 GNUNET_break_op (0);
2644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
2646 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2647 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2649 "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
2651 t = tunnel_get (pi, ntohl (msg->tid));
2654 /* TODO notify back: we don't know this tunnel */
2655 GNUNET_break_op (0);
2658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2659 path = path_new (size);
2661 for (i = 0; i < size; i++)
2663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
2664 GNUNET_i2s (&pi[i]));
2665 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2666 if (path->peers[i] == myid)
2669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
2670 if (own_pos < path->length - 1)
2671 send_message (message, &pi[own_pos + 1]);
2672 tunnel_delete_peer (t, path->peers[path->length - 1]);
2673 path_destroy (path);
2679 * Core handler for notifications of broken paths
2681 * @param cls closure
2682 * @param message message
2683 * @param peer peer identity this notification is about
2684 * @param atsi performance data
2685 * @param atsi_count number of records in 'atsi'
2687 * @return GNUNET_OK to keep the connection open,
2688 * GNUNET_SYSERR to close it (signal serious error)
2691 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
2692 const struct GNUNET_MessageHeader *message,
2693 const struct GNUNET_ATS_Information *atsi,
2694 unsigned int atsi_count)
2696 struct GNUNET_MESH_PathBroken *msg;
2697 struct MeshTunnel *t;
2699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2700 "MESH: Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
2701 msg = (struct GNUNET_MESH_PathBroken *) message;
2702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
2703 GNUNET_i2s (&msg->peer1));
2704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
2705 GNUNET_i2s (&msg->peer2));
2706 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2709 GNUNET_break_op (0);
2712 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
2713 GNUNET_PEER_search (&msg->peer2));
2720 * Core handler for tunnel destruction
2722 * @param cls closure
2723 * @param message message
2724 * @param peer peer identity this notification is about
2725 * @param atsi performance data
2726 * @param atsi_count number of records in 'atsi'
2728 * @return GNUNET_OK to keep the connection open,
2729 * GNUNET_SYSERR to close it (signal serious error)
2732 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2733 const struct GNUNET_MessageHeader *message,
2734 const struct GNUNET_ATS_Information *atsi,
2735 unsigned int atsi_count)
2737 struct GNUNET_MESH_TunnelDestroy *msg;
2738 struct MeshTunnel *t;
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2741 "MESH: Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
2742 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for tunnel %s [%u]\n",
2744 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
2745 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2748 /* Probably already got the message from another path,
2749 * destroyed the tunnel and retransmitted to children.
2754 if (t->id.oid == myid)
2756 GNUNET_break_op (0);
2759 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2761 /* Tunnel was incoming, notify clients */
2762 send_clients_tunnel_destroy (t);
2764 tunnel_send_destroy (t);
2771 * Core handler for mesh network traffic going from the origin to a peer
2773 * @param cls closure
2774 * @param peer peer identity this notification is about
2775 * @param message message
2776 * @param atsi performance data
2777 * @param atsi_count number of records in 'atsi'
2778 * @return GNUNET_OK to keep the connection open,
2779 * GNUNET_SYSERR to close it (signal serious error)
2782 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2783 const struct GNUNET_MessageHeader *message,
2784 const struct GNUNET_ATS_Information *atsi,
2785 unsigned int atsi_count)
2787 struct GNUNET_MESH_Unicast *msg;
2788 struct MeshTunnel *t;
2792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a unicast packet from %s\n",
2794 size = ntohs (message->size);
2796 sizeof (struct GNUNET_MESH_Unicast) +
2797 sizeof (struct GNUNET_MessageHeader))
2802 msg = (struct GNUNET_MESH_Unicast *) message;
2803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: of type %u\n",
2804 ntohs (msg[1].header.type));
2805 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2808 /* TODO notify back: we don't know this tunnel */
2809 GNUNET_break_op (0);
2812 tunnel_reset_timeout (t);
2813 pid = GNUNET_PEER_search (&msg->destination);
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2817 "MESH: it's for us! sending to clients...\n");
2818 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2822 "MESH: not for us, retransmitting...\n");
2823 send_message (message, path_get_first_hop (t->tree, pid));
2829 * Core handler for mesh network traffic going from the origin to all peers
2831 * @param cls closure
2832 * @param message message
2833 * @param peer peer identity this notification is about
2834 * @param atsi performance data
2835 * @param atsi_count number of records in 'atsi'
2836 * @return GNUNET_OK to keep the connection open,
2837 * GNUNET_SYSERR to close it (signal serious error)
2839 * TODO: Check who we got this from, to validate route.
2842 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2843 const struct GNUNET_MessageHeader *message,
2844 const struct GNUNET_ATS_Information *atsi,
2845 unsigned int atsi_count)
2847 struct GNUNET_MESH_Multicast *msg;
2848 struct MeshTunnel *t;
2851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a multicast packet from %s\n",
2853 size = ntohs (message->size);
2854 if (sizeof (struct GNUNET_MESH_Multicast) +
2855 sizeof (struct GNUNET_MessageHeader) > size)
2857 GNUNET_break_op (0);
2860 msg = (struct GNUNET_MESH_Multicast *) message;
2861 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2865 /* TODO notify that we dont know that tunnel */
2866 GNUNET_break_op (0);
2869 tunnel_reset_timeout (t);
2871 /* Transmit to locally interested clients */
2872 if (NULL != t->peers &&
2873 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2875 send_subscribed_clients (message, &msg[1].header);
2877 tunnel_send_multicast (t, message);
2883 * Core handler for mesh network traffic toward the owner of a tunnel
2885 * @param cls closure
2886 * @param message message
2887 * @param peer peer identity this notification is about
2888 * @param atsi performance data
2889 * @param atsi_count number of records in 'atsi'
2891 * @return GNUNET_OK to keep the connection open,
2892 * GNUNET_SYSERR to close it (signal serious error)
2895 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2896 const struct GNUNET_MessageHeader *message,
2897 const struct GNUNET_ATS_Information *atsi,
2898 unsigned int atsi_count)
2900 struct GNUNET_MESH_ToOrigin *msg;
2901 struct GNUNET_PeerIdentity id;
2902 struct MeshPeerInfo *peer_info;
2903 struct MeshTunnel *t;
2906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a ToOrigin packet from %s\n",
2908 size = ntohs (message->size);
2909 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2910 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2912 GNUNET_break_op (0);
2915 msg = (struct GNUNET_MESH_ToOrigin *) message;
2916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: of type %u\n",
2917 ntohs (msg[1].header.type));
2918 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2922 /* TODO notify that we dont know this tunnel (whom)? */
2923 GNUNET_break_op (0);
2927 if (t->id.oid == myid)
2930 struct GNUNET_MESH_ToOrigin *copy;
2932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2933 "MESH: it's for us! sending to clients...\n");
2934 if (NULL == t->client)
2936 /* got data packet for ownerless tunnel */
2937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: no clients!\n");
2938 GNUNET_break_op (0);
2941 /* TODO signature verification */
2942 memcpy (cbuf, message, size);
2943 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2944 copy->tid = htonl (t->local_tid);
2945 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
2946 ©->header, GNUNET_YES);
2949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2950 "MESH: not for us, retransmitting...\n");
2952 peer_info = peer_info_get (&msg->oid);
2953 if (NULL == peer_info)
2955 /* unknown origin of tunnel */
2959 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
2960 send_message (message, &id);
2967 * Core handler for path ACKs
2969 * @param cls closure
2970 * @param message message
2971 * @param peer peer identity this notification is about
2972 * @param atsi performance data
2973 * @param atsi_count number of records in 'atsi'
2975 * @return GNUNET_OK to keep the connection open,
2976 * GNUNET_SYSERR to close it (signal serious error)
2979 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2980 const struct GNUNET_MessageHeader *message,
2981 const struct GNUNET_ATS_Information *atsi,
2982 unsigned int atsi_count)
2984 struct GNUNET_MESH_PathACK *msg;
2985 struct GNUNET_PeerIdentity id;
2986 struct MeshPeerInfo *peer_info;
2987 struct MeshTunnel *t;
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Received a path ACK msg [%s]\n",
2990 GNUNET_i2s (&my_full_id));
2991 msg = (struct GNUNET_MESH_PathACK *) message;
2992 t = tunnel_get (&msg->oid, msg->tid);
2995 /* TODO notify that we don't know the tunnel */
2999 /* Message for us? */
3000 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
3003 if (NULL == t->client)
3005 GNUNET_break_op (0);
3008 if (NULL != t->dht_get_type)
3010 GNUNET_DHT_get_stop (t->dht_get_type);
3011 t->dht_get_type = NULL;
3013 peer_info = peer_info_get (&msg->peer_id);
3014 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3015 send_client_peer_connected (t, peer_info->id);
3019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3020 "MESH: not for us, retransmitting...\n");
3021 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3022 peer_info = peer_info_get (&msg->oid);
3023 if (NULL == peer_info)
3025 /* If we know the tunnel, we should DEFINITELY know the peer */
3029 send_message (message, &id);
3035 * Functions to handle messages from core
3037 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3038 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3039 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3040 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3041 sizeof (struct GNUNET_MESH_PathBroken)},
3042 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3043 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3044 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3045 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3046 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3047 sizeof (struct GNUNET_MESH_PathACK)},
3053 /******************************************************************************/
3054 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3055 /******************************************************************************/
3058 * deregister_app: iterator for removing each application registered by a client
3060 * @param cls closure
3061 * @param key the hash of the application id (used to access the hashmap)
3062 * @param value the value stored at the key (client)
3064 * @return GNUNET_OK on success
3067 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3069 GNUNET_break (GNUNET_YES ==
3070 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3077 * notify_client_connection_failure: notify a client that the connection to the
3078 * requested remote peer is not possible (for instance, no route found)
3079 * Function called when the socket is ready to queue more data. "buf" will be
3080 * NULL and "size" zero if the socket was closed for writing in the meantime.
3082 * @param cls closure
3083 * @param size number of bytes available in buf
3084 * @param buf where the callee should write the message
3085 * @return number of bytes written to buf
3088 notify_client_connection_failure (void *cls, size_t size, void *buf)
3091 struct MeshPeerInfo *peer_info;
3092 struct GNUNET_MESH_PeerControl *msg;
3093 struct GNUNET_PeerIdentity id;
3095 if (0 == size && NULL == buf)
3097 // TODO retry? cancel?
3101 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3102 peer_info = (struct MeshPeerInfo *) cls;
3103 msg = (struct GNUNET_MESH_PeerControl *) buf;
3104 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3105 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3106 // msg->tunnel_id = htonl(peer_info->t->tid);
3107 GNUNET_PEER_resolve (peer_info->id, &id);
3108 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3116 * Send keepalive packets for a peer
3118 * @param cls Closure (tunnel for which to send the keepalive).
3119 * @param tc Notification context.
3121 * TODO: implement explicit multicast keepalive?
3124 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3126 struct MeshTunnel *t = cls;
3127 struct GNUNET_MessageHeader *payload;
3128 struct GNUNET_MESH_Multicast *msg;
3130 sizeof (struct GNUNET_MESH_Multicast) +
3131 sizeof (struct GNUNET_MessageHeader);
3134 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3138 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3141 "MESH: sending keepalive for tunnel %d\n", t->id.tid);
3143 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3144 msg->header.size = htons (size);
3145 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3146 msg->oid = my_full_id;
3147 msg->tid = htonl (t->id.tid);
3148 payload = (struct GNUNET_MessageHeader *) &msg[1];
3149 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3150 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3151 tunnel_send_multicast (t, &msg->header);
3153 t->path_refresh_task =
3154 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3160 * Function to process paths received for a new peer addition. The recorded
3161 * paths form the initial tunnel, which can be optimized later.
3162 * Called on each result obtained for the DHT search.
3164 * @param cls closure
3165 * @param exp when will this value expire
3166 * @param key key of the result
3167 * @param type type of the result
3168 * @param size number of bytes in data
3169 * @param data pointer to the result data
3171 * TODO: re-issue the request after certain time? cancel after X results?
3174 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3175 const GNUNET_HashCode * key,
3176 const struct GNUNET_PeerIdentity *get_path,
3177 unsigned int get_path_length,
3178 const struct GNUNET_PeerIdentity *put_path,
3179 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3180 size_t size, const void *data)
3182 struct MeshPathInfo *path_info = cls;
3183 struct MeshPeerPath *p;
3184 struct GNUNET_PeerIdentity pi;
3187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got results from DHT!\n");
3188 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n", GNUNET_i2s (&pi));
3190 // GNUNET_DHT_get_stop(path_info->peer->dhtget);
3191 // path_info->peer->dhtget = NULL;
3193 p = path_build_from_dht (get_path, get_path_length, put_path,
3195 peer_info_add_path (path_info->peer, p, GNUNET_NO);
3196 for (i = 0; i < path_info->peer->ntunnels; i++)
3198 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3199 peer_info_connect (path_info->peer, path_info->t);
3201 // GNUNET_free (path_info);
3208 * Function to process paths received for a new peer addition. The recorded
3209 * paths form the initial tunnel, which can be optimized later.
3210 * Called on each result obtained for the DHT search.
3212 * @param cls closure
3213 * @param exp when will this value expire
3214 * @param key key of the result
3215 * @param type type of the result
3216 * @param size number of bytes in data
3217 * @param data pointer to the result data
3220 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3221 const GNUNET_HashCode * key,
3222 const struct GNUNET_PeerIdentity *get_path,
3223 unsigned int get_path_length,
3224 const struct GNUNET_PeerIdentity *put_path,
3225 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3226 size_t size, const void *data)
3228 const struct GNUNET_PeerIdentity *pi = data;
3229 struct MeshTunnel *t = cls;
3230 struct MeshPeerInfo *peer_info;
3231 struct MeshPeerPath *p;
3233 if (size != sizeof (struct GNUNET_PeerIdentity))
3235 GNUNET_break_op (0);
3238 GNUNET_assert (NULL != t->client);
3239 peer_info = peer_info_get (pi);
3240 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3242 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3244 p = path_build_from_dht (get_path, get_path_length, put_path,
3246 peer_info_add_path (peer_info, p, GNUNET_NO);
3247 tunnel_add_peer (t, peer_info);
3248 peer_info_connect (peer_info, t);
3252 /******************************************************************************/
3253 /********************* MESH LOCAL HANDLES **************************/
3254 /******************************************************************************/
3258 * Handler for client disconnection
3260 * @param cls closure
3261 * @param client identification of the client; NULL
3262 * for the last call when the server is destroyed
3265 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3267 struct MeshClient *c;
3268 struct MeshClient *next;
3270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3279 if (c->handle != client && NULL != client)
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
3286 GNUNET_SERVER_client_drop (c->handle);
3287 if (NULL != c->tunnels)
3289 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3290 &tunnel_destroy_iterator, c);
3291 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3294 /* deregister clients applications */
3295 if (NULL != c->apps)
3297 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3298 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3300 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3301 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3303 GNUNET_SCHEDULER_cancel (announce_applications_task);
3304 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3306 if (NULL != c->types)
3307 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3309 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT FREE at %p\n", c);
3315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3321 * Handler for new clients
3323 * @param cls closure
3324 * @param client identification of the client
3325 * @param message the actual message, which includes messages the client wants
3328 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3329 const struct GNUNET_MessageHeader *message)
3331 struct GNUNET_MESH_ClientConnect *cc_msg;
3332 struct MeshClient *c;
3333 GNUNET_MESH_ApplicationType *a;
3340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3341 /* Check data sanity */
3342 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3343 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3344 ntypes = ntohs (cc_msg->types);
3345 napps = ntohs (cc_msg->applications);
3347 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3350 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3354 /* Create new client structure */
3355 c = GNUNET_malloc (sizeof (struct MeshClient));
3357 c->id = next_client_id++;
3359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT NEW %u at %p\n", c->id,
3362 GNUNET_SERVER_client_keep (client);
3363 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3366 GNUNET_MESH_ApplicationType at;
3369 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3370 for (i = 0; i < napps; i++)
3373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3374 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3375 /* store in clients hashmap */
3376 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3377 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3378 /* store in global hashmap, for announcements */
3379 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3380 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3382 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3383 announce_applications_task =
3384 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3392 t = (uint16_t *) & a[napps];
3393 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3394 for (i = 0; i < ntypes; i++)
3397 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3399 /* store in clients hashmap */
3400 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3401 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3402 /* store in global hashmap */
3403 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3404 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3408 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3410 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3411 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3412 GNUNET_SERVER_notification_context_add (nc, client);
3414 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3422 * Handler for requests of new tunnels
3424 * @param cls closure
3425 * @param client identification of the client
3426 * @param message the actual message
3429 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3430 const struct GNUNET_MessageHeader *message)
3432 struct GNUNET_MESH_TunnelMessage *t_msg;
3433 struct MeshTunnel *t;
3434 struct MeshClient *c;
3435 GNUNET_HashCode hash;
3437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3439 /* Sanity check for client registration */
3440 if (NULL == (c = client_get (client)))
3443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3450 /* Message sanity check */
3451 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3454 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3458 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3459 /* Sanity check for tunnel numbering */
3460 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3463 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3466 /* Sanity check for duplicate tunnel IDs */
3467 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3470 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3474 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3475 while (NULL != tunnel_get_by_pi (myid, next_tid))
3476 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3477 t->id.tid = next_tid++;
3478 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3480 t->local_tid = ntohl (t_msg->tunnel_id);
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL %s [%x] (%x)\n",
3483 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3486 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3488 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3490 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3491 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3494 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3498 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3500 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3501 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3507 t->tree = tree_new (myid);
3508 tree_set_me (t->tree, myid);
3510 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3516 * Handler for requests of deleting tunnels
3518 * @param cls closure
3519 * @param client identification of the client
3520 * @param message the actual message
3523 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3524 const struct GNUNET_MessageHeader *message)
3526 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3527 struct MeshClient *c;
3528 struct MeshTunnel *t;
3529 MESH_TunnelNumber tid;
3530 GNUNET_HashCode hash;
3532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3533 "MESH: Got a DESTROY TUNNEL from client!\n");
3535 /* Sanity check for client registration */
3536 if (NULL == (c = client_get (client)))
3539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3542 /* Message sanity check */
3543 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3546 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3552 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3554 /* Retrieve tunnel */
3555 tid = ntohl (tunnel_msg->tunnel_id);
3557 /* Remove from local id hashmap */
3558 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3559 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3560 GNUNET_assert (GNUNET_YES ==
3561 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t));
3564 tunnel_send_destroy (t);
3566 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3572 * Handler for connection requests to new peers
3574 * @param cls closure
3575 * @param client identification of the client
3576 * @param message the actual message (PeerControl)
3579 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3580 const struct GNUNET_MessageHeader *message)
3582 struct GNUNET_MESH_PeerControl *peer_msg;
3583 struct MeshPeerInfo *peer_info;
3584 struct MeshClient *c;
3585 struct MeshTunnel *t;
3586 MESH_TunnelNumber tid;
3588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3589 /* Sanity check for client registration */
3590 if (NULL == (c = client_get (client)))
3593 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3597 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3598 /* Sanity check for message size */
3599 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3602 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3606 /* Tunnel exists? */
3607 tid = ntohl (peer_msg->tunnel_id);
3608 t = tunnel_get_by_local_id (c, tid);
3612 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3616 /* Does client own tunnel? */
3617 if (t->client->handle != client)
3620 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3624 GNUNET_i2s (&peer_msg->peer));
3625 peer_info = peer_info_get (&peer_msg->peer);
3627 tunnel_add_peer (t, peer_info);
3628 peer_info_connect (peer_info, t);
3630 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3636 * Handler for disconnection requests of peers in a tunnel
3638 * @param cls closure
3639 * @param client identification of the client
3640 * @param message the actual message (PeerControl)
3643 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3644 const struct GNUNET_MessageHeader *message)
3646 struct GNUNET_MESH_PeerControl *peer_msg;
3647 struct MeshPeerInfo *peer_info;
3648 struct MeshClient *c;
3649 struct MeshTunnel *t;
3650 MESH_TunnelNumber tid;
3652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got a PEER DEL request\n");
3653 /* Sanity check for client registration */
3654 if (NULL == (c = client_get (client)))
3657 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3660 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3661 /* Sanity check for message size */
3662 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3665 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3669 /* Tunnel exists? */
3670 tid = ntohl (peer_msg->tunnel_id);
3671 t = tunnel_get_by_local_id (c, tid);
3675 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: on tunnel %X\n", t->id.tid);
3680 /* Does client own tunnel? */
3681 if (t->client->handle != client)
3684 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for peer %s\n",
3689 GNUNET_i2s (&peer_msg->peer));
3690 /* Is the peer in the tunnel? */
3692 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
3693 if (NULL == peer_info)
3696 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3700 /* Ok, delete peer from tunnel */
3701 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3702 &peer_msg->peer.hashPubKey);
3704 send_destroy_path (t, peer_info->id);
3705 tunnel_delete_peer (t, peer_info->id);
3706 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3712 * Handler for connection requests to new peers by type
3714 * @param cls closure
3715 * @param client identification of the client
3716 * @param message the actual message (ConnectPeerByType)
3719 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3720 const struct GNUNET_MessageHeader *message)
3722 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3723 struct MeshClient *c;
3724 struct MeshTunnel *t;
3725 GNUNET_HashCode hash;
3726 MESH_TunnelNumber tid;
3728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3729 /* Sanity check for client registration */
3730 if (NULL == (c = client_get (client)))
3733 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3737 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3738 /* Sanity check for message size */
3739 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3740 ntohs (connect_msg->header.size))
3743 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3747 /* Tunnel exists? */
3748 tid = ntohl (connect_msg->tunnel_id);
3749 t = tunnel_get_by_local_id (c, tid);
3753 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3757 /* Does client own tunnel? */
3758 if (t->client->handle != client)
3761 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3765 /* Do WE have the service? */
3766 t->type = ntohl (connect_msg->type);
3767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3768 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3769 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3772 /* Yes! Fast forward, add ourselves to the tunnel and send the
3773 * good news to the client
3775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3776 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3777 peer_info_get (&my_full_id),
3778 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3781 send_client_peer_connected (t, myid);
3782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3783 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3786 /* Ok, lets find a peer offering the service */
3787 if (NULL != t->dht_get_type)
3789 GNUNET_DHT_get_stop (t->dht_get_type);
3791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3792 GNUNET_h2s (&hash));
3794 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
3795 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
3796 GNUNET_DHT_RO_RECORD_ROUTE |
3797 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
3798 &dht_get_type_handler, t);
3800 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3806 * Handler for client traffic directed to one peer
3808 * @param cls closure
3809 * @param client identification of the client
3810 * @param message the actual message
3813 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3814 const struct GNUNET_MessageHeader *message)
3816 struct MeshClient *c;
3817 struct MeshTunnel *t;
3818 struct MeshPeerInfo *pi;
3819 struct GNUNET_MESH_Unicast *data_msg;
3820 MESH_TunnelNumber tid;
3823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3824 "MESH: Got a unicast request from a client!\n");
3826 /* Sanity check for client registration */
3827 if (NULL == (c = client_get (client)))
3830 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3833 data_msg = (struct GNUNET_MESH_Unicast *) message;
3834 /* Sanity check for message size */
3835 size = ntohs (message->size);
3836 if (sizeof (struct GNUNET_MESH_Unicast) +
3837 sizeof (struct GNUNET_MessageHeader) > size)
3840 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3844 /* Tunnel exists? */
3845 tid = ntohl (data_msg->tid);
3846 t = tunnel_get_by_local_id (c, tid);
3850 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3854 /* Is it a local tunnel? Then, does client own the tunnel? */
3855 if (NULL != t->client && NULL != t->client->handle &&
3856 t->client->handle != client)
3859 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3863 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3864 &data_msg->destination.hashPubKey);
3865 /* Is the selected peer in the tunnel? */
3869 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3873 /* Ok, everything is correct, send the message
3874 * (pretend we got it from a mesh peer)
3877 char buf[ntohs (message->size)];
3878 struct GNUNET_MESH_Unicast *copy;
3880 /* Work around const limitation */
3881 copy = (struct GNUNET_MESH_Unicast *) buf;
3882 memcpy (buf, data_msg, size);
3883 copy->oid = my_full_id;
3884 copy->tid = htonl (t->id.tid);
3885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3886 "MESH: calling generic handler...\n");
3887 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
3889 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3895 * Handler for client traffic directed to the origin
3897 * @param cls closure
3898 * @param client identification of the client
3899 * @param message the actual message
3902 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3903 const struct GNUNET_MessageHeader *message)
3905 struct GNUNET_MESH_ToOrigin *data_msg;
3906 struct GNUNET_PeerIdentity id;
3907 struct MeshClient *c;
3908 struct MeshTunnel *t;
3909 MESH_TunnelNumber tid;
3912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3913 "MESH: Got a ToOrigin request from a client!\n");
3915 /* Sanity check for client registration */
3916 if (NULL == (c = client_get (client)))
3919 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3922 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3923 /* Sanity check for message size */
3924 size = ntohs (message->size);
3925 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3926 sizeof (struct GNUNET_MessageHeader) > size)
3929 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3933 /* Tunnel exists? */
3934 tid = ntohl (data_msg->tid);
3935 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3938 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3941 t = tunnel_get_by_local_id (c, tid);
3945 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3949 /* It shouldn't be a local tunnel. */
3950 if (NULL != t->client)
3953 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3956 GNUNET_PEER_resolve (t->id.oid, &id);
3958 /* Ok, everything is correct, send the message
3959 * (pretend we got it from a mesh peer)
3962 char buf[ntohs (message->size)];
3963 struct GNUNET_MESH_ToOrigin *copy;
3965 /* Work around const limitation */
3966 copy = (struct GNUNET_MESH_ToOrigin *) buf;
3967 memcpy (buf, data_msg, size);
3969 copy->tid = htonl (t->id.tid);
3970 copy->sender = my_full_id;
3971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3972 "MESH: calling generic handler...\n");
3973 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
3975 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3981 * Handler for client traffic directed to all peers in a tunnel
3983 * @param cls closure
3984 * @param client identification of the client
3985 * @param message the actual message
3988 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3989 const struct GNUNET_MessageHeader *message)
3991 struct MeshClient *c;
3992 struct MeshTunnel *t;
3993 struct GNUNET_MESH_Multicast *data_msg;
3994 MESH_TunnelNumber tid;
3996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3997 "MESH: Got a multicast request from a client!\n");
3999 /* Sanity check for client registration */
4000 if (NULL == (c = client_get (client)))
4003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4006 data_msg = (struct GNUNET_MESH_Multicast *) message;
4007 /* Sanity check for message size */
4008 if (sizeof (struct GNUNET_MESH_Multicast) +
4009 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4016 /* Tunnel exists? */
4017 tid = ntohl (data_msg->tid);
4018 t = tunnel_get_by_local_id (c, tid);
4022 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4026 /* Does client own tunnel? */
4027 if (t->client->handle != client)
4030 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4035 char buf[ntohs (message->size)];
4036 struct GNUNET_MESH_Multicast *copy;
4038 copy = (struct GNUNET_MESH_Multicast *) buf;
4039 memcpy (buf, message, ntohs (message->size));
4040 copy->oid = my_full_id;
4041 copy->tid = htonl (t->id.tid);
4042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4043 "MESH: calling generic handler...\n");
4044 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4047 /* receive done gets called when last copy is sent to a neighbor */
4052 * Functions to handle messages from clients
4054 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4055 {&handle_local_new_client, NULL,
4056 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4057 {&handle_local_tunnel_create, NULL,
4058 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4059 sizeof (struct GNUNET_MESH_TunnelMessage)},
4060 {&handle_local_tunnel_destroy, NULL,
4061 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4062 sizeof (struct GNUNET_MESH_TunnelMessage)},
4063 {&handle_local_connect_add, NULL,
4064 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4065 sizeof (struct GNUNET_MESH_PeerControl)},
4066 {&handle_local_connect_del, NULL,
4067 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4068 sizeof (struct GNUNET_MESH_PeerControl)},
4069 {&handle_local_connect_by_type, NULL,
4070 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4071 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4072 {&handle_local_unicast, NULL,
4073 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4074 {&handle_local_to_origin, NULL,
4075 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4076 {&handle_local_multicast, NULL,
4077 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4083 * To be called on core init/fail.
4085 * @param cls service closure
4086 * @param server handle to the server for this service
4087 * @param identity the public identity of this peer
4090 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4091 const struct GNUNET_PeerIdentity *identity)
4093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
4094 core_handle = server;
4095 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4098 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
4099 GNUNET_SCHEDULER_shutdown ();
4105 * Method called whenever a given peer connects.
4107 * @param cls closure
4108 * @param peer peer identity this notification is about
4109 * @param atsi performance data for the connection
4110 * @param atsi_count number of records in 'atsi'
4113 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4114 const struct GNUNET_ATS_Information *atsi,
4115 unsigned int atsi_count)
4117 struct MeshPeerInfo *peer_info;
4118 struct MeshPeerPath *path;
4120 #if MESH_DEBUG_CONNECTION
4121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
4122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4123 GNUNET_i2s (&my_full_id));
4125 peer_info = peer_info_get (peer);
4126 if (myid == peer_info->id)
4128 #if MESH_DEBUG_CONNECTION
4129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4133 #if MESH_DEBUG_CONNECTION
4136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", GNUNET_i2s (peer));
4139 path = path_new (2);
4140 path->peers[0] = myid;
4141 path->peers[1] = peer_info->id;
4142 GNUNET_PEER_change_rc (myid, 1);
4143 GNUNET_PEER_change_rc (peer_info->id, 1);
4144 peer_info_add_path (peer_info, path, GNUNET_YES);
4149 * Method called whenever a peer disconnects.
4151 * @param cls closure
4152 * @param peer peer identity this notification is about
4155 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4157 struct MeshPeerInfo *pi;
4160 #if MESH_DEBUG_CONNECTION
4161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
4163 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4169 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4171 /* TODO: notify that the transmission failed */
4172 peer_info_cancel_transmission (pi, i);
4174 peer_info_remove_path (pi, pi->id, myid);
4175 #if MESH_DEBUG_CONNECTION
4178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4185 /******************************************************************************/
4186 /************************ MAIN FUNCTIONS ****************************/
4187 /******************************************************************************/
4190 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4192 * @param cls closure
4193 * @param key current key code
4194 * @param value value in the hash map
4195 * @return GNUNET_YES if we should continue to iterate,
4199 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4201 struct MeshTunnel *t = value;
4208 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4210 * @param cls closure
4211 * @param key current key code
4212 * @param value value in the hash map
4213 * @return GNUNET_YES if we should continue to iterate,
4217 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4219 struct MeshPeerInfo *p = value;
4221 peer_info_destroy (p);
4226 * Task run during shutdown.
4232 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4235 /* TODO: destroy tunnels? */
4236 if (core_handle != NULL)
4238 GNUNET_CORE_disconnect (core_handle);
4241 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4242 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4243 if (dht_handle != NULL)
4245 GNUNET_DHT_disconnect (dht_handle);
4250 GNUNET_SERVER_notification_context_destroy (nc);
4253 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4255 GNUNET_SCHEDULER_cancel (announce_id_task);
4256 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4262 * Process mesh requests.
4264 * @param cls closure
4265 * @param server the initialized server
4266 * @param c configuration to use
4269 run (void *cls, struct GNUNET_SERVER_Handle *server,
4270 const struct GNUNET_CONFIGURATION_Handle *c)
4272 struct MeshPeerInfo *peer;
4273 struct MeshPeerPath *p;
4276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4277 server_handle = server;
4278 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4279 CORE_QUEUE_SIZE, /* queue size */
4280 NULL, /* Closure passed to MESH functions */
4281 &core_init, /* Call core_init once connected */
4282 &core_connect, /* Handle connects */
4283 &core_disconnect, /* remove peers on disconnects */
4284 NULL, /* Don't notify about all incoming messages */
4285 GNUNET_NO, /* For header only in notification */
4286 NULL, /* Don't notify about all outbound messages */
4287 GNUNET_NO, /* For header-only out notification */
4288 core_handlers); /* Register these handlers */
4290 if (core_handle == NULL)
4293 GNUNET_SCHEDULER_shutdown ();
4298 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4301 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4303 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4304 GNUNET_SCHEDULER_shutdown ();
4307 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4308 GNUNET_free (keyfile);
4309 if (my_private_key == NULL)
4311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4312 _("Mesh service could not access hostkey. Exiting.\n"));
4313 GNUNET_SCHEDULER_shutdown ();
4316 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4317 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4318 &my_full_id.hashPubKey);
4319 myid = GNUNET_PEER_intern (&my_full_id);
4321 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4328 dht_handle = GNUNET_DHT_connect (c, 64);
4329 if (dht_handle == NULL)
4335 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4337 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4338 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4339 peers = GNUNET_CONTAINER_multihashmap_create (32);
4340 applications = GNUNET_CONTAINER_multihashmap_create (32);
4341 types = GNUNET_CONTAINER_multihashmap_create (32);
4343 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4344 nc = GNUNET_SERVER_notification_context_create (server_handle,
4346 GNUNET_SERVER_disconnect_notify (server_handle,
4347 &handle_local_client_disconnect, NULL);
4351 clients_tail = NULL;
4356 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4357 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4359 /* Create a peer_info for the local peer */
4360 peer = peer_info_get (&my_full_id);
4363 GNUNET_PEER_change_rc (myid, 1);
4364 peer_info_add_path (peer, p, GNUNET_YES);
4366 /* Scheduled the task to clean up when shutdown is called */
4367 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4374 * The main function for the mesh service.
4376 * @param argc number of arguments from the command line
4377 * @param argv command line arguments
4378 * @return 0 ok, 1 on error
4381 main (int argc, char *const *argv)
4385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4388 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");