2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - relay corking down to core
44 * - set ttl relative to tree depth
49 #include "mesh_protocol.h"
50 #include "gnunet_dht_service.h"
51 #include "mesh_tunnel_tree.h"
53 /* TODO: move into configuration file */
54 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
55 GNUNET_TIME_UNIT_SECONDS,\
57 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
58 GNUNET_TIME_UNIT_SECONDS,\
61 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
62 GNUNET_TIME_UNIT_SECONDS,\
65 #define GET_RESTART_TIME GNUNET_TIME_relative_multiply(\
66 GNUNET_TIME_UNIT_SECONDS,\
69 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
70 GNUNET_TIME_UNIT_SECONDS,\
72 #define DEFAULT_TTL 64
74 #define MESH_DEBUG_DHT GNUNET_NO
78 /******************************************************************************/
79 /************************ DATA STRUCTURES ****************************/
80 /******************************************************************************/
82 /** FWD declaration */
86 * Struct containing all info possibly needed to build a package when called
89 struct MeshDataDescriptor
91 /** ID of the tunnel this packet travels in */
92 struct MESH_TunnelID *origin;
97 /** Client that asked for the transmission, if any */
98 struct GNUNET_SERVER_Client *client;
100 /** Who was this message being sent to */
101 struct MeshPeerInfo *peer;
103 /** Ultimate destination of the packet */
104 GNUNET_PEER_Id destination;
106 /** Number of identical messages sent to different hops (multicast) */
107 unsigned int *copies;
109 /** Which handler was used to request the transmission */
110 unsigned int handler_n;
112 /** Size of the data */
115 /** Used to allow a client send more traffic to the service after a
116 * previous packet was tried to be sent to a neighbor and couldn't */
117 GNUNET_SCHEDULER_TaskIdentifier *timeout_task;
122 * Struct containing all information regarding a given peer
132 * Last time we heard from this peer
134 struct GNUNET_TIME_Absolute last_contact;
137 * Number of attempts to reconnect so far
139 int n_reconnect_attempts;
142 * Paths to reach the peer, ordered by ascending hop count
144 struct MeshPeerPath *path_head;
147 * Paths to reach the peer, ordered by ascending hop count
149 struct MeshPeerPath *path_tail;
152 * Handle to stop the DHT search for a path to this peer
154 struct GNUNET_DHT_GetHandle *dhtget;
157 * Closure given to the DHT GET
159 struct MeshPathInfo *dhtgetcls;
162 * Handles to stop queued transmissions for this peer
164 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
167 * Pointer to info stuctures used as cls for queued transmissions
169 void *infos[CORE_QUEUE_SIZE];
172 * Type of message being in each transmission
174 uint16_t types[CORE_QUEUE_SIZE];
177 * Array of tunnels this peer participates in
178 * (most probably a small amount, therefore not a hashmap)
179 * When the path to the peer changes, notify these tunnels to let them
180 * re-adjust their path trees.
182 struct MeshTunnel **tunnels;
185 * Number of tunnels this peers participates in
187 unsigned int ntunnels;
192 * Data scheduled to transmit (to local client or remote peer)
199 struct MeshQueue *next;
200 struct MeshQueue *prev;
203 * Target of the data (NULL if target is client)
205 struct MeshPeerInfo *peer;
208 * Client to send the data to (NULL if target is peer)
210 struct MeshClient *client;
213 * Size of the message to transmit
218 * How old is the data?
220 struct GNUNET_TIME_Absolute timestamp;
225 struct GNUNET_MessageHeader *data;
229 * Globally unique tunnel identification (owner + number)
230 * DO NOT USE OVER THE NETWORK
235 * Node that owns the tunnel
240 * Tunnel number to differentiate all the tunnels owned by the node oid
241 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
243 MESH_TunnelNumber tid;
247 struct MeshClient; /* FWD declaration */
250 * Struct containing all information regarding a tunnel
251 * For an intermediate node the improtant info used will be:
252 * - id Tunnel unique identification
253 * - paths[0] To know where to send it next
254 * - metainfo: ready, speeds, accounting
261 struct MESH_TunnelID id;
264 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
266 MESH_TunnelNumber local_tid;
269 * ID of the last multicast packet seen/sent.
274 * Last time the tunnel was used
276 struct GNUNET_TIME_Absolute timestamp;
279 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
280 * containing peers added by id or by type, not intermediate peers.
282 struct GNUNET_CONTAINER_MultiHashMap *peers;
285 * Number of peers that are connected and potentially ready to receive data
287 unsigned int peers_ready;
290 * Number of peers that have been added to the tunnel
292 unsigned int peers_total;
295 * Client owner of the tunnel, if any
297 struct MeshClient *client;
300 * Messages ready to transmit
302 struct MeshQueue *queue_head;
303 struct MeshQueue *queue_tail;
308 struct MeshTunnelTree *tree;
311 * Application type we are looking for in this tunnel
313 GNUNET_MESH_ApplicationType type;
316 * Used to search peers offering a service
318 struct GNUNET_DHT_GetHandle *dht_get_type;
321 * Task to keep the used paths alive
323 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
326 * Task to destroy the tunnel after timeout
328 * FIXME: merge the two? a tunnel will have either
329 * a path refresh OR a timeout, never both!
331 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
336 * Info needed to work with tunnel paths and peers
343 struct MeshTunnel *t;
346 * Neighbouring peer to whom we send the packet to
348 struct MeshPeerInfo *peer;
353 struct MeshPeerPath *path;
356 * Position in peer's transmit queue
363 * Struct containing information about a client of the service
370 struct MeshClient *next;
371 struct MeshClient *prev;
374 * Tunnels that belong to this client, indexed by local id
376 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
379 * Handle to communicate with the client
381 struct GNUNET_SERVER_Client *handle;
384 * Applications that this client has claimed to provide
386 struct GNUNET_CONTAINER_MultiHashMap *apps;
389 * Messages that this client has declared interest in
391 struct GNUNET_CONTAINER_MultiHashMap *types;
395 * ID of the client, for debug messages
404 /******************************************************************************/
405 /************************ DEBUG FUNCTIONS ****************************/
406 /******************************************************************************/
410 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
411 * @param cls string to print
412 * @param tc task context
415 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
419 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
427 /******************************************************************************/
428 /*********************** GLOBAL VARIABLES ****************************/
429 /******************************************************************************/
434 static struct MeshClient *clients;
435 static struct MeshClient *clients_tail;
438 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
440 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
443 * Tunnels incoming, indexed by MESH_TunnelNumber
444 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
446 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
449 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
451 static struct GNUNET_CONTAINER_MultiHashMap *peers;
454 * Handle to communicate with core
456 static struct GNUNET_CORE_Handle *core_handle;
459 * Handle to communicate with transport
461 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
466 static struct GNUNET_DHT_Handle *dht_handle;
471 static struct GNUNET_SERVER_Handle *server_handle;
474 * Notification context, to send messages to local clients
476 static struct GNUNET_SERVER_NotificationContext *nc;
479 * Local peer own ID (memory efficient handle)
481 static GNUNET_PEER_Id myid;
484 * Local peer own ID (full value)
486 static struct GNUNET_PeerIdentity my_full_id;
491 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
496 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
499 * Tunnel ID for the next created tunnel (global tunnel number)
501 static MESH_TunnelNumber next_tid;
504 * Tunnel ID for the next incoming tunnel (local tunnel number)
506 static MESH_TunnelNumber next_local_tid;
509 * All application types provided by this peer
511 static struct GNUNET_CONTAINER_MultiHashMap *applications;
514 * All message types clients of this peer are interested in
516 static struct GNUNET_CONTAINER_MultiHashMap *types;
519 * Task to periodically announce provided applications
521 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
524 * Task to periodically announce itself in the network
526 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
529 unsigned int next_client_id;
533 /******************************************************************************/
534 /************************ ITERATORS ****************************/
535 /******************************************************************************/
537 /* FIXME move iterators here */
540 /******************************************************************************/
541 /************************ PERIODIC FUNCTIONS ****************************/
542 /******************************************************************************/
545 * Announce iterator over for each application provided by the peer
548 * @param key current key code
549 * @param value value in the hash map
550 * @return GNUNET_YES if we should continue to
555 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
557 /* FIXME are hashes in multihash map equal on all aquitectures? */
558 GNUNET_DHT_put (dht_handle, key, 10U,
559 GNUNET_DHT_RO_RECORD_ROUTE |
560 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
561 sizeof (struct GNUNET_PeerIdentity),
562 (const char *) &my_full_id,
564 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
565 &mesh_debug, "DHT_put for app completed");
567 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
569 APP_ANNOUNCE_TIME, NULL, NULL);
576 * Periodically announce what applications are provided by local clients
579 * @param tc task context
582 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
584 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
586 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
592 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
594 announce_applications_task =
595 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
605 * Periodically announce self id in the DHT
608 * @param tc task context
611 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
613 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
615 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
619 * - Set data expiration in function of X
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
624 GNUNET_i2s (&my_full_id));
626 GNUNET_DHT_put (dht_handle, /* DHT handle */
627 &my_full_id.hashPubKey, /* Key to use */
628 10U, /* Replication level */
629 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
630 GNUNET_BLOCK_TYPE_TEST, /* Block type */
631 sizeof (my_full_id), /* Size of the data */
632 (char *) &my_full_id, /* Data itself */
633 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
634 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
636 &mesh_debug, "DHT_put for id completed");
638 NULL, /* Continuation */
639 NULL); /* Continuation closure */
642 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
647 * Function to process paths received for a new peer addition. The recorded
648 * paths form the initial tunnel, which can be optimized later.
649 * Called on each result obtained for the DHT search.
652 * @param exp when will this value expire
653 * @param key key of the result
654 * @param type type of the result
655 * @param size number of bytes in data
656 * @param data pointer to the result data
659 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
660 const GNUNET_HashCode * key,
661 const struct GNUNET_PeerIdentity *get_path,
662 unsigned int get_path_length,
663 const struct GNUNET_PeerIdentity *put_path,
664 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
665 size_t size, const void *data);
668 /******************************************************************************/
669 /****************** GENERAL HELPER FUNCTIONS ************************/
670 /******************************************************************************/
673 * Check if client has registered with the service and has not disconnected
675 * @param client the client to check
677 * @return non-NULL if client exists in the global DLL
679 static struct MeshClient *
680 client_get (struct GNUNET_SERVER_Client *client)
682 struct MeshClient *c;
687 if (c->handle == client)
696 * Checks if a given client has subscribed to certain message type
698 * @param message_type Type of message to check
699 * @param c Client to check
701 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
706 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
710 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
711 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
716 * Allow a client to send more data after transmitting a multicast message
717 * which some neighbor has not yet accepted altough a reasonable time has
720 * @param cls Closure (DataDescriptor containing the task identifier)
721 * @param tc Task Context
724 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
726 struct MeshDataDescriptor *info = cls;
728 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "MESH: CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
735 *(info->timeout_task) = GNUNET_SCHEDULER_NO_TASK;
736 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
741 * Search for a tunnel by global ID using full PeerIdentities
743 * @param oid owner of the tunnel
744 * @param tid global tunnel number
746 * @return tunnel handler, NULL if doesn't exist
748 static struct MeshTunnel *
749 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
753 * Notify a tunnel that a connection has broken that affects at least
756 * @param t Tunnel affected.
757 * @param p1 Peer that got disconnected from p2.
758 * @param p2 Peer that got disconnected from p1.
760 * @return Short ID of the peer disconnected (either p1 or p2).
761 * 0 if the tunnel remained unaffected.
763 static GNUNET_PEER_Id
764 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
769 * Send the message to all clients that have subscribed to its type
771 * @param msg Pointer to the message itself
772 * @return number of clients this message was sent to
775 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
776 const struct GNUNET_MessageHeader *payload)
778 struct GNUNET_PeerIdentity *oid;
779 struct MeshClient *c;
780 struct MeshTunnel *t;
781 MESH_TunnelNumber *tid;
784 char cbuf[htons (msg->size)];
786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
787 type = ntohs (payload->type);
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
790 memcpy (cbuf, msg, sizeof (cbuf));
791 switch (htons (msg->type))
793 struct GNUNET_MESH_Unicast *uc;
794 struct GNUNET_MESH_Multicast *mc;
795 struct GNUNET_MESH_ToOrigin *to;
797 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
798 uc = (struct GNUNET_MESH_Unicast *) cbuf;
802 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
803 mc = (struct GNUNET_MESH_Multicast *) cbuf;
807 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
808 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
816 t = tunnel_get (oid, ntohl (*tid));
822 *tid = htonl (t->local_tid);
823 for (count = 0, c = clients; c != NULL; c = c->next)
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
828 if (client_is_subscribed (type, c))
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
832 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
833 (struct GNUNET_MessageHeader
834 *) cbuf, GNUNET_YES);
842 * Notify the client that owns the tunnel that a peer has connected to it
843 * (the requested path to it has been confirmed).
845 * @param t Tunnel whose owner to notify
846 * @param id Short id of the peer that has connected
849 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
851 struct GNUNET_MESH_PeerControl pc;
853 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
854 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
855 pc.tunnel_id = htonl (t->local_tid);
856 GNUNET_PEER_resolve (id, &pc.peer);
857 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, &pc.header,
863 * Notify all clients (not depending on registration status) that the incoming
864 * tunnel is no longer valid.
866 * @param t Tunnel that was destroyed.
869 send_clients_tunnel_destroy (struct MeshTunnel *t)
871 struct GNUNET_MESH_TunnelMessage msg;
873 msg.header.size = htons (sizeof (msg));
874 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
875 msg.tunnel_id = htonl (t->local_tid);
876 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
881 * Function called to notify a client about the socket
882 * being ready to queue more data. "buf" will be
883 * NULL and "size" zero if the socket was closed for
884 * writing in the meantime.
887 * @param size number of bytes available in buf
888 * @param buf where the callee should write the message
889 * @return number of bytes written to buf
892 send_core_create_path (void *cls, size_t size, void *buf);
896 * Function called to notify a client about the socket
897 * being ready to queue more data. "buf" will be
898 * NULL and "size" zero if the socket was closed for
899 * writing in the meantime.
901 * @param cls closure (data itself)
902 * @param size number of bytes available in buf
903 * @param buf where the callee should write the message
905 * @return number of bytes written to buf
908 send_core_data_multicast (void *cls, size_t size, void *buf);
912 * Decrements the reference counter and frees all resources if needed
914 * @param dd Data Descriptor used in a multicast message
917 data_descriptor_decrement_multicast (struct MeshDataDescriptor *dd)
919 if (0 == --(*(dd->copies)))
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
922 if (NULL != dd->client)
924 if (GNUNET_SCHEDULER_NO_TASK != *(dd->timeout_task))
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
927 "MESH: cancelling client timeout (%u)...\n",
928 *(dd->timeout_task));
929 GNUNET_SCHEDULER_cancel (*(dd->timeout_task));
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
931 GNUNET_SERVER_receive_done (dd->client, GNUNET_OK);
933 GNUNET_free (dd->timeout_task);
935 GNUNET_free (dd->copies);
936 GNUNET_free (dd->data);
942 * Cancel a core transmission that was already requested and free all resources
943 * associated to the request.
945 * @param peer PeeInfo of the peer whose transmission is cancelled.
946 * @param i Position of the transmission to be cancelled.
949 peer_info_cancel_transmission (struct MeshPeerInfo *peer, unsigned int i)
951 if (NULL != peer->core_transmit[i])
953 struct MeshDataDescriptor *dd;
954 struct MeshPathInfo *path_info;
958 struct GNUNET_PeerIdentity id;
960 GNUNET_PEER_resolve (peer->id, &id);
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "MESH: Cancelling data transmission at %s [%u]\n",
963 GNUNET_i2s (&id), i);
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message type %u\n",
968 /* TODO: notify that tranmission has failed */
969 switch (peer->types[i])
971 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
972 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
973 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
976 data_descriptor_decrement_multicast (dd);
978 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
980 path_info = peer->infos[i];
981 path_destroy (path_info->path);
985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
987 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
988 peer->core_transmit[i] = NULL;
989 GNUNET_free (peer->infos[i]);
995 * Get a unused CORE slot to transmit a message to a peer. If all the slots
996 * are used, cancel one and return it's position.
998 * @param peer PeerInfo of the neighbor we want to transmit to.
1000 * @return The index of an available slot to transmit to the neighbor.
1003 peer_info_transmit_slot (struct MeshPeerInfo *peer)
1007 for (i = 0; peer->core_transmit[i]; i++)
1009 if (i == (CORE_QUEUE_SIZE - 1))
1011 /* All positions are taken! Overwriting! */
1013 peer_info_cancel_transmission (peer, 0);
1022 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1023 * and insert it in the appropiate structures if the peer is not known yet.
1025 * @param peer Full identity of the peer.
1027 * @return Existing or newly created peer info.
1029 static struct MeshPeerInfo *
1030 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1032 struct MeshPeerInfo *peer_info;
1034 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1035 if (NULL == peer_info)
1038 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1039 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1040 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1041 peer_info->id = GNUNET_PEER_intern (peer);
1049 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1050 * and insert it in the appropiate structures if the peer is not known yet.
1052 * @param peer Short identity of the peer.
1054 * @return Existing or newly created peer info.
1056 static struct MeshPeerInfo *
1057 peer_info_get_short (const GNUNET_PEER_Id peer)
1059 struct GNUNET_PeerIdentity id;
1061 GNUNET_PEER_resolve (peer, &id);
1062 return peer_info_get (&id);
1067 * Iterator to remove the tunnel from the list of tunnels a peer participates
1070 * @param cls Closure (tunnel info)
1071 * @param key GNUNET_PeerIdentity of the peer (unused)
1072 * @param value PeerInfo of the peer
1074 * @return always GNUNET_YES, to keep iterating
1077 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1079 struct MeshTunnel *t = cls;
1080 struct MeshPeerInfo *peer = value;
1083 for (i = 0; i < peer->ntunnels; i++)
1086 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1089 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1090 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1099 * Core callback to write a
1101 * @param cls Closure (MeshDataDescriptor with data in "data" member).
1102 * @param size Number of bytes available in buf.
1103 * @param buf Where the to write the message.
1105 * @return number of bytes written to buf
1108 send_core_data_raw (void *cls, size_t size, void *buf)
1110 struct MeshDataDescriptor *info = cls;
1111 struct GNUNET_MessageHeader *msg;
1114 GNUNET_assert (NULL != info);
1115 GNUNET_assert (NULL != info->data);
1116 msg = (struct GNUNET_MessageHeader *) info->data;
1117 total_size = ntohs (msg->size);
1119 if (total_size > size)
1121 struct GNUNET_PeerIdentity id;
1123 GNUNET_PEER_resolve (info->peer->id, &id);
1124 info->peer->core_transmit[info->handler_n] =
1125 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1126 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1127 size, &send_core_data_raw, info);
1130 info->peer->core_transmit[info->handler_n] = NULL;
1131 memcpy (buf, msg, total_size);
1132 GNUNET_free (info->data);
1139 * Sends an already built message to a peer, properly registrating
1140 * all used resources.
1142 * @param message Message to send. Fucntion makes a copy of it.
1143 * @param peer Short ID of the neighbor whom to send the message.
1146 send_message (const struct GNUNET_MessageHeader *message,
1147 const struct GNUNET_PeerIdentity *peer)
1149 struct MeshDataDescriptor *info;
1150 struct MeshPeerInfo *neighbor;
1151 struct MeshPeerPath *p;
1155 // GNUNET_TRANSPORT_try_connect();
1157 size = ntohs (message->size);
1158 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1159 info->data = GNUNET_malloc (size);
1160 memcpy (info->data, message, size);
1161 neighbor = peer_info_get (peer);
1162 for (p = neighbor->path_head; NULL != p; p = p->next)
1175 i = peer_info_transmit_slot (neighbor);
1176 info->handler_n = i;
1177 info->peer = neighbor;
1178 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1179 neighbor->infos[i] = info;
1180 neighbor->core_transmit[i] =
1181 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1182 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1183 size, &send_core_data_raw, info);
1189 * Sends a CREATE PATH message for a path to a peer, properly registrating
1190 * all used resources.
1192 * @param peer PeerInfo of the final peer for whom this path is being created.
1193 * @param p Path itself.
1194 * @param t Tunnel for which the path is created.
1197 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1198 struct MeshTunnel *t)
1200 struct GNUNET_PeerIdentity id;
1201 struct MeshPathInfo *path_info;
1202 struct MeshPeerInfo *neighbor;
1207 p = tree_get_path_to_peer (t->tree, peer->id);
1214 for (i = 0; i < p->length; i++)
1216 if (p->peers[i] == myid)
1219 if (i >= p->length - 1)
1225 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1227 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1228 path_info->path = p;
1230 neighbor = peer_info_get (&id);
1231 path_info->peer = neighbor;
1232 path_info->pos = peer_info_transmit_slot (neighbor);
1233 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1234 neighbor->infos[path_info->pos] = path_info;
1235 neighbor->core_transmit[path_info->pos] = GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
1238 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1240 sizeof (struct GNUNET_MESH_ManipulatePath) + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1241 &send_core_create_path, /* callback */
1242 path_info); /* cls */
1247 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1249 * @param t Tunnel whose path to destroy.
1250 * @param destination Short ID of the peer to whom the path to destroy.
1253 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1255 struct MeshPeerPath *p;
1258 p = tree_get_path_to_peer (t->tree, destination);
1264 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1265 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1267 struct GNUNET_MESH_ManipulatePath *msg;
1268 struct GNUNET_PeerIdentity *pi;
1272 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1273 msg->header.size = htons (size);
1274 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1275 msg->tid = htonl (t->id.tid);
1276 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1277 for (i = 0; i < p->length; i++)
1279 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1281 send_message (&msg->header, tree_get_first_hop (t->tree, destination));
1288 * Try to establish a new connection to this peer.
1289 * Use the best path for the given tunnel.
1290 * If the peer doesn't have any path to it yet, try to get one.
1291 * If the peer already has some path, send a CREATE PATH towards it.
1293 * @param peer PeerInfo of the peer.
1294 * @param t Tunnel for which to create the path, if possible.
1297 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1299 struct MeshPeerPath *p;
1300 struct MeshPathInfo *path_info;
1302 if (NULL != peer->path_head)
1304 p = tree_get_path_to_peer (t->tree, peer->id);
1313 send_create_path (peer, p, t);
1318 send_client_peer_connected (t, myid);
1321 else if (NULL == peer->dhtget)
1323 struct GNUNET_PeerIdentity id;
1325 GNUNET_PEER_resolve (peer->id, &id);
1326 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1327 path_info->peer = peer;
1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330 "MESH: Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1331 peer->dhtgetcls = path_info;
1332 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1333 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1334 GNUNET_BLOCK_TYPE_TEST, /* type */
1335 &id.hashPubKey, /* key to search */
1336 4, /* replication level */
1337 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1338 0, /* xquery bits */
1339 &dht_get_id_handler, path_info);
1341 /* Otherwise, there is no path but the DHT get is already started. */
1346 * Task to delay the connection of a peer
1348 * @param cls Closure (path info with tunnel and peer to connect).
1349 * Will be free'd on exection.
1350 * @param tc TaskContext
1353 peer_info_connect_task (void *cls,
1354 const struct GNUNET_SCHEDULER_TaskContext *tc)
1356 struct MeshPathInfo *path_info = cls;
1358 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1363 peer_info_connect (path_info->peer, path_info->t);
1369 * Destroy the peer_info and free any allocated resources linked to it
1371 * @param pi The peer_info to destroy.
1373 * @return GNUNET_OK on success
1376 peer_info_destroy (struct MeshPeerInfo *pi)
1378 struct GNUNET_PeerIdentity id;
1379 struct MeshPeerPath *p;
1380 struct MeshPeerPath *nextp;
1383 GNUNET_PEER_resolve (pi->id, &id);
1384 GNUNET_PEER_change_rc (pi->id, -1);
1387 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1390 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1391 "MESH: removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1393 if (NULL != pi->dhtget)
1395 GNUNET_DHT_get_stop (pi->dhtget);
1396 GNUNET_free (pi->dhtgetcls);
1398 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1400 peer_info_cancel_transmission (pi, i);
1406 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1416 * Remove all paths that rely on a direct connection between p1 and p2
1417 * from the peer itself and notify all tunnels about it.
1419 * @param peer PeerInfo of affected peer.
1420 * @param p1 GNUNET_PEER_Id of one peer.
1421 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1424 * TODO: optimize (see below)
1427 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1430 struct MeshPeerPath *p;
1431 struct MeshPeerPath *aux;
1432 struct MeshPeerInfo *peer_d;
1434 unsigned int destroyed;
1440 p = peer->path_head;
1444 for (i = 0; i < (p->length - 1); i++)
1446 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1447 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1449 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1460 for (i = 0; i < peer->ntunnels; i++)
1462 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1463 if (0 == d) continue;
1465 * Problem: one or more peers have been deleted from the tunnel tree.
1466 * We don't know who they are to try to add them again.
1467 * We need to try to find a new path for each of the disconnected peers.
1468 * Some of them might already have a path to reach them that does not
1469 * involve p1 and p2. Adding all anew might render in a better tree than
1470 * the trivial immediate fix.
1472 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1473 * its children will be reachable trough him.
1475 peer_d = peer_info_get_short (d);
1478 for (p = peer_d->path_head; NULL != p; p = p->next)
1480 if ((cost = path_get_cost (peer->tunnels[i]->tree, p)) < best)
1488 /* No callback, as peer will be already disconnected and a connection
1489 * scheduled by tunnel_notify_connection_broken.
1491 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1495 peer_info_connect (peer_d, peer->tunnels[i]);
1502 * Add the path to the peer and update the path used to reach it in case this
1505 * @param peer_info Destination peer to add the path to.
1506 * @param path New path to add. Last peer must be the peer in arg 1.
1507 * Path will be either used of freed if already known.
1508 * @param trusted Do we trust that this path is real?
1511 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1514 struct MeshPeerPath *aux;
1518 if ((NULL == peer_info) || (NULL == path))
1521 path_destroy (path);
1524 if (path->length <= 2 && GNUNET_NO == trusted)
1526 /* Only allow CORE to tell us about direct paths */
1527 path_destroy (path);
1530 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1531 for (l = 1; l < path->length; l++)
1533 if (path->peers[l] == myid)
1535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shortening path by %u\n", l);
1536 for (l2 = 0; l2 < path->length - l; l2++)
1538 path->peers[l2] = path->peers[l + l2];
1543 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1548 struct GNUNET_PeerIdentity id;
1550 GNUNET_PEER_resolve (peer_info->id, &id);
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: adding path [%u] to peer %s\n",
1552 path->length, GNUNET_i2s (&id));
1555 l = path_get_length (path);
1562 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1563 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1565 l2 = path_get_length (aux);
1568 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1569 peer_info->path_tail, aux, path);
1574 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1576 path_destroy (path);
1581 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1588 * Add the path to the origin peer and update the path used to reach it in case
1589 * this is the shortest.
1590 * The path is given in peer_info -> destination, therefore we turn the path
1591 * upside down first.
1593 * @param peer_info Peer to add the path to, being the origin of the path.
1594 * @param path New path to add after being inversed.
1595 * @param trusted Do we trust that this path is real?
1598 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1599 struct MeshPeerPath *path, int trusted)
1602 peer_info_add_path (peer_info, path, trusted);
1607 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1608 * to obtain a local peer -> destination path and interning the peer ids.
1610 * @return Newly allocated and created path
1612 static struct MeshPeerPath *
1613 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1614 unsigned int get_path_length,
1615 const struct GNUNET_PeerIdentity *put_path,
1616 unsigned int put_path_length)
1618 struct MeshPeerPath *p;
1624 GNUNET_PEER_change_rc (myid, 1);
1625 i = get_path_length;
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1627 for (i--; i >= 0; i--)
1629 id = GNUNET_PEER_intern (&get_path[i]);
1630 if (p->length > 0 && id == p->peers[p->length - 1])
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1633 GNUNET_PEER_change_rc (id, -1);
1637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Adding from GET: %s.\n",
1638 GNUNET_i2s (&get_path[i]));
1640 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1641 p->peers[p->length - 1] = id;
1644 i = put_path_length;
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1646 for (i--; i >= 0; i--)
1648 id = GNUNET_PEER_intern (&put_path[i]);
1651 /* PUT path went through us, so discard the path up until now and start
1652 * from here to get a much shorter (and loop-free) path.
1657 if (p->length > 0 && id == p->peers[p->length - 1])
1659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1660 GNUNET_PEER_change_rc (id, -1);
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Adding from PUT: %s.\n",
1665 GNUNET_i2s (&put_path[i]));
1667 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1668 p->peers[p->length - 1] = id;
1672 if (get_path_length > 0)
1673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (first of GET: %s)\n",
1674 GNUNET_i2s (&get_path[0]));
1675 if (put_path_length > 0)
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (first of PUT: %s)\n",
1677 GNUNET_i2s (&put_path[0]));
1678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: In total: %d hops\n",
1680 for (i = 0; i < p->length; i++)
1682 struct GNUNET_PeerIdentity peer_id;
1684 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %u: %s\n", p->peers[i],
1686 GNUNET_i2s (&peer_id));
1694 * Send keepalive packets for a peer
1696 * @param cls Closure (tunnel for which to send the keepalive).
1697 * @param tc Notification context.
1699 * TODO: implement explicit multicast keepalive?
1702 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1706 * Search for a tunnel among the incoming tunnels
1708 * @param tid the local id of the tunnel
1710 * @return tunnel handler, NULL if doesn't exist
1712 static struct MeshTunnel *
1713 tunnel_get_incoming (MESH_TunnelNumber tid)
1715 GNUNET_HashCode hash;
1717 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1718 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1719 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1724 * Search for a tunnel among the tunnels for a client
1726 * @param c the client whose tunnels to search in
1727 * @param tid the local id of the tunnel
1729 * @return tunnel handler, NULL if doesn't exist
1731 static struct MeshTunnel *
1732 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1734 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1736 return tunnel_get_incoming (tid);
1740 GNUNET_HashCode hash;
1742 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1743 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1749 * Search for a tunnel by global ID using PEER_ID
1751 * @param pi owner of the tunnel
1752 * @param tid global tunnel number
1754 * @return tunnel handler, NULL if doesn't exist
1756 static struct MeshTunnel *
1757 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1759 struct MESH_TunnelID id;
1760 GNUNET_HashCode hash;
1765 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1766 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1771 * Search for a tunnel by global ID using full PeerIdentities
1773 * @param oid owner of the tunnel
1774 * @param tid global tunnel number
1776 * @return tunnel handler, NULL if doesn't exist
1778 static struct MeshTunnel *
1779 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1781 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1786 * Callback used to notify a client owner of a tunnel that a peer has
1787 * disconnected, most likely because of a path change.
1789 * @param cls Closure (tunnel this notification is about).
1790 * @param peer_id Short ID of disconnected peer.
1793 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
1795 struct MeshTunnel *t = cls;
1796 struct MeshPeerInfo *peer;
1797 struct MeshPathInfo *path_info;
1799 if (NULL != t->client && NULL != nc)
1801 struct GNUNET_MESH_PeerControl msg;
1803 msg.header.size = htons (sizeof (msg));
1804 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1805 msg.tunnel_id = htonl (t->local_tid);
1806 GNUNET_PEER_resolve (peer_id, &msg.peer);
1807 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1808 &msg.header, GNUNET_NO);
1810 peer = peer_info_get_short (peer_id);
1811 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1812 path_info->peer = peer;
1814 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
1819 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1820 * needed rescources.
1821 * If peer already exists, reevaluate shortest path and change if different.
1823 * @param t Tunnel we want to add a new peer to
1824 * @param peer PeerInfo of the peer being added
1828 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1830 struct GNUNET_PeerIdentity id;
1831 struct MeshPeerPath *best_p;
1832 struct MeshPeerPath *p;
1833 unsigned int best_cost;
1836 GNUNET_PEER_resolve (peer->id, &id);
1838 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
1841 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1842 GNUNET_assert (GNUNET_OK ==
1843 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
1845 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1848 if (NULL != (p = peer->path_head))
1851 best_cost = path_get_cost (t->tree, p);
1854 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1861 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
1862 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1863 t->path_refresh_task =
1864 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
1868 /* Start a DHT get if necessary */
1869 peer_info_connect (peer, t);
1874 * Add a path to a tunnel which we don't own, just to remember the next hop.
1875 * If destination node was already in the tunnel, the first hop information
1876 * will be replaced with the new path.
1878 * @param t Tunnel we want to add a new peer to
1879 * @param p Path to add
1880 * @param own_pos Position of local node in path.
1884 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
1885 unsigned int own_pos)
1887 struct GNUNET_PeerIdentity id;
1889 GNUNET_assert (0 != own_pos);
1890 tree_add_path (t->tree, p, NULL, NULL);
1891 if (own_pos < p->length - 1)
1893 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1894 tree_update_first_hops (t->tree, myid, &id);
1900 * Notifies a tunnel that a connection has broken that affects at least
1901 * some of its peers. Sends a notification towards the root of the tree.
1902 * In case the peer is the owner of the tree, notifies the client that owns
1903 * the tunnel and tries to reconnect.
1905 * @param t Tunnel affected.
1906 * @param p1 Peer that got disconnected from p2.
1907 * @param p2 Peer that got disconnected from p1.
1909 * @return Short ID of the peer disconnected (either p1 or p2).
1910 * 0 if the tunnel remained unaffected.
1912 static GNUNET_PEER_Id
1913 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1919 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
1921 if (myid != p1 && myid != p2)
1927 if (tree_get_predecessor (t->tree) != 0)
1929 /* We are the peer still connected, notify owner of the disconnection. */
1930 struct GNUNET_MESH_PathBroken msg;
1931 struct GNUNET_PeerIdentity neighbor;
1933 msg.header.size = htons (sizeof (msg));
1934 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1935 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1936 msg.tid = htonl (t->id.tid);
1937 msg.peer1 = my_full_id;
1938 GNUNET_PEER_resolve (pid, &msg.peer2);
1939 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1940 send_message (&msg.header, &neighbor);
1947 struct MeshMulticastData
1949 struct MeshTunnel *t;
1951 GNUNET_SCHEDULER_TaskIdentifier *task;
1953 unsigned int *reference_counter;
1962 * Send a multicast packet to a neighbor.
1965 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
1967 struct MeshMulticastData *mdata = cls;
1968 struct MeshDataDescriptor *info;
1969 struct GNUNET_PeerIdentity neighbor;
1972 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1974 info->data = mdata->data;
1975 info->size = mdata->data_len;
1976 info->copies = mdata->reference_counter;
1977 (*(mdata->reference_counter))++;
1979 if (NULL != mdata->t->client)
1981 info->client = mdata->t->client->handle;
1982 info->timeout_task = mdata->task;
1984 info->destination = neighbor_id;
1985 GNUNET_PEER_resolve (neighbor_id, &neighbor);
1987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending to %s...\n",
1988 GNUNET_i2s (&neighbor));
1990 info->peer = peer_info_get (&neighbor);
1991 GNUNET_assert (NULL != info->peer);
1992 i = peer_info_transmit_slot (info->peer);
1993 info->handler_n = i;
1994 info->peer->infos[i] = info;
1995 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1996 info->peer->core_transmit[i] =
1997 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1998 GNUNET_TIME_UNIT_FOREVER_REL,
1999 &neighbor, info->size,
2000 &send_core_data_multicast, info);
2004 * Send a message in a tunnel in multicast, sending a copy to each child node
2005 * down the local one in the tunnel tree.
2007 * @param t Tunnel in which to send the data.
2008 * @param msg Message to be sent
2011 tunnel_send_multicast (struct MeshTunnel *t,
2012 const struct GNUNET_MessageHeader *msg)
2014 struct MeshMulticastData *mdata;
2017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2018 "MESH: sending a multicast packet...\n");
2020 mdata = GNUNET_malloc (sizeof (struct MeshMulticastData));
2021 mdata->data_len = ntohs (msg->size);
2022 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2024 mdata->data = GNUNET_malloc (mdata->data_len);
2025 memcpy (mdata->data, msg, mdata->data_len);
2026 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2028 struct GNUNET_MESH_Multicast *mcast;
2030 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2031 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2034 "MESH: data packet, ttl: %u\n",
2035 ntohl (mcast->ttl));
2039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2040 "MESH: not a data packet, no ttl\n");
2043 if (NULL != t->client)
2045 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2047 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2051 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2052 if (*(mdata->reference_counter) == 0)
2054 GNUNET_free (mdata->data);
2055 GNUNET_free (mdata->reference_counter);
2056 if (NULL != mdata->task)
2058 GNUNET_free (mdata->task);
2061 GNUNET_free (mdata);
2063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2064 "MESH: sending a multicast packet done\n");
2071 * Send a message to all peers in this tunnel that the tunnel is no longer
2074 * @param t The tunnel whose peers to notify.
2077 tunnel_send_destroy (struct MeshTunnel *t)
2079 struct GNUNET_MESH_TunnelDestroy msg;
2081 msg.header.size = htons (sizeof (msg));
2082 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2083 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2084 msg.tid = htonl (t->id.tid);
2085 tunnel_send_multicast (t, &msg.header);
2091 * Destroy the tunnel and free any allocated resources linked to it
2093 * @param t the tunnel to destroy
2095 * @return GNUNET_OK on success
2098 tunnel_destroy (struct MeshTunnel *t)
2100 struct MeshClient *c;
2101 struct MeshQueue *q;
2102 struct MeshQueue *qn;
2103 GNUNET_HashCode hash;
2112 struct GNUNET_PeerIdentity id;
2114 GNUNET_PEER_resolve (t->id.oid, &id);
2115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel %s [%x]\n",
2116 GNUNET_i2s (&id), t->id.tid);
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2122 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2123 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2128 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2130 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
2134 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2136 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2137 GNUNET_break (GNUNET_YES ==
2138 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash,
2141 if (NULL != t->peers)
2143 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2145 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2150 if (NULL != q->data)
2151 GNUNET_free (q->data);
2155 /* TODO cancel core transmit ready in case it was active */
2157 tree_destroy (t->tree);
2158 if (NULL != t->dht_get_type)
2159 GNUNET_DHT_get_stop (t->dht_get_type);
2160 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2161 GNUNET_SCHEDULER_cancel (t->timeout_task);
2162 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2163 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2170 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2171 * that are no longer needed, as well as nodes of no longer reachable peers.
2172 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2174 * @param t Tunnel from which to remove the path.
2175 * @param p Peer which should be removed.
2178 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2180 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2186 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2187 * client when the client disconnects.
2189 * @param cls closure (client that is disconnecting)
2190 * @param key the hash of the local tunnel id (used to access the hashmap)
2191 * @param value the value stored at the key (tunnel to destroy)
2193 * @return GNUNET_OK on success
2196 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2198 struct MeshTunnel *t = value;
2201 r = tunnel_destroy (t);
2207 * Timeout function, destroys tunnel if called
2209 * @param cls Closure (tunnel to destroy).
2210 * @param tc TaskContext
2213 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2215 struct MeshTunnel *t = cls;
2217 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2219 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2224 * Resets the tunnel timeout. Starts it if no timeout was running.
2226 * @param t Tunnel whose timeout to reset.
2229 tunnel_reset_timeout (struct MeshTunnel *t)
2231 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2232 GNUNET_SCHEDULER_cancel (t->timeout_task);
2234 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2235 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2239 /******************************************************************************/
2240 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2241 /******************************************************************************/
2244 * Function called to notify a client about the socket
2245 * being ready to queue more data. "buf" will be
2246 * NULL and "size" zero if the socket was closed for
2247 * writing in the meantime.
2249 * @param cls closure
2250 * @param size number of bytes available in buf
2251 * @param buf where the callee should write the message
2252 * @return number of bytes written to buf
2255 send_core_create_path (void *cls, size_t size, void *buf)
2257 struct MeshPathInfo *info = cls;
2258 struct GNUNET_MESH_ManipulatePath *msg;
2259 struct GNUNET_PeerIdentity *peer_ptr;
2260 struct MeshPeerInfo *peer = info->peer;
2261 struct MeshTunnel *t = info->t;
2262 struct MeshPeerPath *p = info->path;
2266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATE PATH sending...\n");
2268 sizeof (struct GNUNET_MESH_ManipulatePath) +
2269 p->length * sizeof (struct GNUNET_PeerIdentity);
2271 if (size < size_needed || NULL == buf)
2273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n", size,
2277 info->peer->core_transmit[info->pos] =
2278 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2279 GNUNET_TIME_UNIT_FOREVER_REL,
2280 tree_get_first_hop (t->tree,
2282 size_needed, &send_core_create_path,
2286 info->peer->core_transmit[info->pos] = NULL;
2289 struct GNUNET_PeerIdentity id;
2291 GNUNET_PEER_resolve (peer->id, &id);
2292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2293 "MESH: setting core_transmit %s [%u] to NULL\n",
2294 GNUNET_i2s (&id), info->pos);
2297 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2298 msg->header.size = htons (size_needed);
2299 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2300 msg->tid = ntohl (t->id.tid);
2302 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2303 for (i = 0; i < p->length; i++)
2305 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2312 "MESH: CREATE PATH (%u bytes long) sent!\n", size_needed);
2318 * Function called to notify a client about the socket
2319 * being ready to queue more data. "buf" will be
2320 * NULL and "size" zero if the socket was closed for
2321 * writing in the meantime.
2323 * @param cls closure (data itself)
2324 * @param size number of bytes available in buf
2325 * @param buf where the callee should write the message
2327 * @return number of bytes written to buf
2330 send_core_data_multicast (void *cls, size_t size, void *buf)
2332 struct MeshDataDescriptor *info = cls;
2335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2336 GNUNET_assert (NULL != info);
2337 GNUNET_assert (NULL != info->peer);
2338 total_size = info->size;
2339 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2341 if (total_size > size)
2344 struct GNUNET_PeerIdentity id;
2346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2347 "MESH: Multicast: retransmitting... (%u/%u)\n", size,
2349 GNUNET_PEER_resolve (info->peer->id, &id);
2350 info->peer->core_transmit[info->handler_n] =
2351 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2352 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2354 &send_core_data_multicast, info);
2357 info->peer->core_transmit[info->handler_n] = NULL;
2358 info->peer->infos[info->handler_n] = NULL;
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2360 memcpy (buf, info->data, total_size);
2363 struct GNUNET_MESH_Multicast *mc;
2364 struct GNUNET_MessageHeader *mh;
2367 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2369 mc = (struct GNUNET_MESH_Multicast *) mh;
2370 mh = (struct GNUNET_MessageHeader *) &mc[1];
2371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2372 "MESH: multicast, payload type %u\n", ntohs (mh->type));
2373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2374 "MESH: multicast, payload size %u\n", ntohs (mh->size));
2378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type %u\n",
2383 data_descriptor_decrement_multicast (info);
2384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2392 * Function called to notify a client about the socket
2393 * being ready to queue more data. "buf" will be
2394 * NULL and "size" zero if the socket was closed for
2395 * writing in the meantime.
2397 * @param cls closure (MeshDataDescriptor)
2398 * @param size number of bytes available in buf
2399 * @param buf where the callee should write the message
2400 * @return number of bytes written to buf
2403 send_core_path_ack (void *cls, size_t size, void *buf)
2405 struct MeshDataDescriptor *info = cls;
2406 struct GNUNET_MESH_PathACK *msg = buf;
2408 GNUNET_assert (NULL != info);
2411 info->peer->core_transmit[info->handler_n] = NULL;
2413 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2418 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2419 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2420 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2421 msg->tid = htonl (info->origin->tid);
2422 msg->peer_id = my_full_id;
2424 /* TODO add signature */
2426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2427 return sizeof (struct GNUNET_MESH_PathACK);
2431 /******************************************************************************/
2432 /******************** MESH NETWORK HANDLERS **************************/
2433 /******************************************************************************/
2437 * Core handler for path creation
2439 * @param cls closure
2440 * @param message message
2441 * @param peer peer identity this notification is about
2442 * @param atsi performance data
2443 * @param atsi_count number of records in 'atsi'
2445 * @return GNUNET_OK to keep the connection open,
2446 * GNUNET_SYSERR to close it (signal serious error)
2449 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2450 const struct GNUNET_MessageHeader *message,
2451 const struct GNUNET_ATS_Information *atsi,
2452 unsigned int atsi_count)
2454 unsigned int own_pos;
2457 MESH_TunnelNumber tid;
2458 struct GNUNET_MESH_ManipulatePath *msg;
2459 struct GNUNET_PeerIdentity *pi;
2460 GNUNET_HashCode hash;
2461 struct MeshPeerPath *path;
2462 struct MeshPeerInfo *dest_peer_info;
2463 struct MeshPeerInfo *orig_peer_info;
2464 struct MeshTunnel *t;
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2467 "MESH: Received a path create msg [%s]\n",
2468 GNUNET_i2s (&my_full_id));
2469 size = ntohs (message->size);
2470 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2472 GNUNET_break_op (0);
2476 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2477 if (size % sizeof (struct GNUNET_PeerIdentity))
2479 GNUNET_break_op (0);
2482 size /= sizeof (struct GNUNET_PeerIdentity);
2485 GNUNET_break_op (0);
2488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
2489 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2491 tid = ntohl (msg->tid);
2492 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2493 t = tunnel_get (pi, tid);
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2499 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2500 t->id.oid = GNUNET_PEER_intern (pi);
2502 while (NULL != tunnel_get_incoming (next_local_tid))
2503 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2504 t->local_tid = next_local_tid++;
2505 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2506 t->tree = tree_new (t->id.oid);
2508 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2510 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2511 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2517 tunnel_reset_timeout (t);
2518 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2520 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2521 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2529 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2530 if (NULL == dest_peer_info)
2532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2533 "MESH: Creating PeerInfo for destination.\n");
2534 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2535 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2536 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2538 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2540 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2541 if (NULL == orig_peer_info)
2543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2544 "MESH: Creating PeerInfo for origin.\n");
2545 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2546 orig_peer_info->id = GNUNET_PEER_intern (pi);
2547 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2548 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2551 path = path_new (size);
2553 for (i = 0; i < size; i++)
2555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
2556 GNUNET_i2s (&pi[i]));
2557 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2558 if (path->peers[i] == myid)
2561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
2564 /* cannot be self, must be 'not found' */
2565 /* create path: self not found in path through self */
2566 GNUNET_break_op (0);
2567 path_destroy (path);
2568 /* FIXME error. destroy tunnel? leave for timeout? */
2571 tunnel_add_path (t, path, own_pos);
2572 if (own_pos == size - 1)
2574 /* It is for us! Send ack. */
2575 struct GNUNET_MESH_TunnelNotification cmsg;
2576 struct MeshDataDescriptor *info;
2579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
2580 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2581 if (NULL == t->peers)
2582 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2583 GNUNET_break (GNUNET_OK ==
2584 GNUNET_CONTAINER_multihashmap_put (t->peers,
2585 &my_full_id.hashPubKey,
2588 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2589 /* FIXME use send_message */
2590 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2591 info->origin = &t->id;
2592 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2593 GNUNET_assert (NULL != info->peer);
2594 j = peer_info_transmit_slot (info->peer);
2595 info->handler_n = j;
2596 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2597 info->peer->infos[j] = info;
2598 info->peer->core_transmit[j] =
2599 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2600 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2601 sizeof (struct GNUNET_MESH_PathACK),
2602 &send_core_path_ack, info);
2603 cmsg.header.size = htons (sizeof (cmsg));
2604 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2605 GNUNET_PEER_resolve (t->id.oid, &cmsg.peer);
2606 cmsg.tunnel_id = htonl (t->local_tid);
2607 GNUNET_SERVER_notification_context_broadcast (nc, &cmsg.header, GNUNET_NO);
2611 struct MeshPeerPath *path2;
2613 /* It's for somebody else! Retransmit. */
2614 path2 = path_duplicate (path);
2615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting.\n");
2616 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
2617 path2 = path_duplicate (path);
2618 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
2619 send_create_path (dest_peer_info, path, t);
2626 * Core handler for path destruction
2628 * @param cls closure
2629 * @param message message
2630 * @param peer peer identity this notification is about
2631 * @param atsi performance data
2632 * @param atsi_count number of records in 'atsi'
2634 * @return GNUNET_OK to keep the connection open,
2635 * GNUNET_SYSERR to close it (signal serious error)
2638 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2639 const struct GNUNET_MessageHeader *message,
2640 const struct GNUNET_ATS_Information *atsi,
2641 unsigned int atsi_count)
2643 struct GNUNET_MESH_ManipulatePath *msg;
2644 struct GNUNET_PeerIdentity *pi;
2645 struct MeshPeerPath *path;
2646 struct MeshTunnel *t;
2647 unsigned int own_pos;
2651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2652 "MESH: Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
2653 size = ntohs (message->size);
2654 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2656 GNUNET_break_op (0);
2660 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2661 if (size % sizeof (struct GNUNET_PeerIdentity))
2663 GNUNET_break_op (0);
2666 size /= sizeof (struct GNUNET_PeerIdentity);
2669 GNUNET_break_op (0);
2672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
2674 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2675 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2677 "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
2679 t = tunnel_get (pi, ntohl (msg->tid));
2682 /* TODO notify back: we don't know this tunnel */
2683 GNUNET_break_op (0);
2686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2687 path = path_new (size);
2689 for (i = 0; i < size; i++)
2691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
2692 GNUNET_i2s (&pi[i]));
2693 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2694 if (path->peers[i] == myid)
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
2698 if (own_pos < path->length - 1)
2699 send_message (message, &pi[own_pos + 1]);
2700 tunnel_delete_peer (t, path->peers[path->length - 1]);
2701 path_destroy (path);
2707 * Core handler for notifications of broken paths
2709 * @param cls closure
2710 * @param message message
2711 * @param peer peer identity this notification is about
2712 * @param atsi performance data
2713 * @param atsi_count number of records in 'atsi'
2715 * @return GNUNET_OK to keep the connection open,
2716 * GNUNET_SYSERR to close it (signal serious error)
2719 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
2720 const struct GNUNET_MessageHeader *message,
2721 const struct GNUNET_ATS_Information *atsi,
2722 unsigned int atsi_count)
2724 struct GNUNET_MESH_PathBroken *msg;
2725 struct MeshTunnel *t;
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2728 "MESH: Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
2729 msg = (struct GNUNET_MESH_PathBroken *) message;
2730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
2731 GNUNET_i2s (&msg->peer1));
2732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
2733 GNUNET_i2s (&msg->peer2));
2734 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2737 GNUNET_break_op (0);
2740 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
2741 GNUNET_PEER_search (&msg->peer2));
2748 * Core handler for tunnel destruction
2750 * @param cls closure
2751 * @param message message
2752 * @param peer peer identity this notification is about
2753 * @param atsi performance data
2754 * @param atsi_count number of records in 'atsi'
2756 * @return GNUNET_OK to keep the connection open,
2757 * GNUNET_SYSERR to close it (signal serious error)
2760 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2761 const struct GNUNET_MessageHeader *message,
2762 const struct GNUNET_ATS_Information *atsi,
2763 unsigned int atsi_count)
2765 struct GNUNET_MESH_TunnelDestroy *msg;
2766 struct MeshTunnel *t;
2768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2769 "MESH: Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
2770 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for tunnel %s [%u]\n",
2772 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
2773 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2776 /* Probably already got the message from another path,
2777 * destroyed the tunnel and retransmitted to children.
2782 if (t->id.oid == myid)
2784 GNUNET_break_op (0);
2787 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2789 /* Tunnel was incoming, notify clients */
2790 send_clients_tunnel_destroy (t);
2792 tunnel_send_destroy (t);
2799 * Core handler for mesh network traffic going from the origin to a peer
2801 * @param cls closure
2802 * @param peer peer identity this notification is about
2803 * @param message message
2804 * @param atsi performance data
2805 * @param atsi_count number of records in 'atsi'
2806 * @return GNUNET_OK to keep the connection open,
2807 * GNUNET_SYSERR to close it (signal serious error)
2810 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2811 const struct GNUNET_MessageHeader *message,
2812 const struct GNUNET_ATS_Information *atsi,
2813 unsigned int atsi_count)
2815 struct GNUNET_MESH_Unicast *msg;
2816 struct MeshTunnel *t;
2820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a unicast packet from %s\n",
2822 size = ntohs (message->size);
2824 sizeof (struct GNUNET_MESH_Unicast) +
2825 sizeof (struct GNUNET_MessageHeader))
2830 msg = (struct GNUNET_MESH_Unicast *) message;
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: of type %u\n",
2832 ntohs (msg[1].header.type));
2833 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2836 /* TODO notify back: we don't know this tunnel */
2837 GNUNET_break_op (0);
2840 tunnel_reset_timeout (t);
2841 pid = GNUNET_PEER_search (&msg->destination);
2844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2845 "MESH: it's for us! sending to clients...\n");
2846 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2850 "MESH: not for us, retransmitting...\n");
2851 send_message (message, tree_get_first_hop (t->tree, pid));
2857 * Core handler for mesh network traffic going from the origin to all peers
2859 * @param cls closure
2860 * @param message message
2861 * @param peer peer identity this notification is about
2862 * @param atsi performance data
2863 * @param atsi_count number of records in 'atsi'
2864 * @return GNUNET_OK to keep the connection open,
2865 * GNUNET_SYSERR to close it (signal serious error)
2867 * TODO: Check who we got this from, to validate route.
2870 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2871 const struct GNUNET_MessageHeader *message,
2872 const struct GNUNET_ATS_Information *atsi,
2873 unsigned int atsi_count)
2875 struct GNUNET_MESH_Multicast *msg;
2876 struct MeshTunnel *t;
2879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a multicast packet from %s\n",
2881 size = ntohs (message->size);
2882 if (sizeof (struct GNUNET_MESH_Multicast) +
2883 sizeof (struct GNUNET_MessageHeader) > size)
2885 GNUNET_break_op (0);
2888 msg = (struct GNUNET_MESH_Multicast *) message;
2889 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2893 /* TODO notify that we dont know that tunnel */
2894 GNUNET_break_op (0);
2897 if (t->mid == ntohl (msg->mid))
2899 /* FIXME: already seen this packet, log dropping */
2900 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2901 "MESH: Already seen mid %u, DROPPING!\n",
2908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2909 "MESH: mid %u not seen yet, forwarding\n",
2913 t->mid = ntohl (msg->mid);
2914 tunnel_reset_timeout (t);
2916 /* Transmit to locally interested clients */
2917 if (NULL != t->peers &&
2918 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2920 send_subscribed_clients (message, &msg[1].header);
2923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2927 if (ntohl (msg->ttl) == 0)
2929 /* FIXME: ttl is 0, log dropping */
2930 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2931 "MESH: TTL is 0, DROPPING!\n");
2934 tunnel_send_multicast (t, message);
2940 * Core handler for mesh network traffic toward the owner of a tunnel
2942 * @param cls closure
2943 * @param message message
2944 * @param peer peer identity this notification is about
2945 * @param atsi performance data
2946 * @param atsi_count number of records in 'atsi'
2948 * @return GNUNET_OK to keep the connection open,
2949 * GNUNET_SYSERR to close it (signal serious error)
2952 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2953 const struct GNUNET_MessageHeader *message,
2954 const struct GNUNET_ATS_Information *atsi,
2955 unsigned int atsi_count)
2957 struct GNUNET_MESH_ToOrigin *msg;
2958 struct GNUNET_PeerIdentity id;
2959 struct MeshPeerInfo *peer_info;
2960 struct MeshTunnel *t;
2963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a ToOrigin packet from %s\n",
2965 size = ntohs (message->size);
2966 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2967 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2969 GNUNET_break_op (0);
2972 msg = (struct GNUNET_MESH_ToOrigin *) message;
2973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: of type %u\n",
2974 ntohs (msg[1].header.type));
2975 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2979 /* TODO notify that we dont know this tunnel (whom)? */
2980 GNUNET_break_op (0);
2984 if (t->id.oid == myid)
2987 struct GNUNET_MESH_ToOrigin *copy;
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2990 "MESH: it's for us! sending to clients...\n");
2991 if (NULL == t->client)
2993 /* got data packet for ownerless tunnel */
2994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: no clients!\n");
2995 GNUNET_break_op (0);
2998 /* TODO signature verification */
2999 memcpy (cbuf, message, size);
3000 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3001 copy->tid = htonl (t->local_tid);
3002 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
3003 ©->header, GNUNET_YES);
3006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3007 "MESH: not for us, retransmitting...\n");
3009 peer_info = peer_info_get (&msg->oid);
3010 if (NULL == peer_info)
3012 /* unknown origin of tunnel */
3016 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3017 send_message (message, &id);
3024 * Core handler for path ACKs
3026 * @param cls closure
3027 * @param message message
3028 * @param peer peer identity this notification is about
3029 * @param atsi performance data
3030 * @param atsi_count number of records in 'atsi'
3032 * @return GNUNET_OK to keep the connection open,
3033 * GNUNET_SYSERR to close it (signal serious error)
3036 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3037 const struct GNUNET_MessageHeader *message,
3038 const struct GNUNET_ATS_Information *atsi,
3039 unsigned int atsi_count)
3041 struct GNUNET_MESH_PathACK *msg;
3042 struct GNUNET_PeerIdentity id;
3043 struct MeshPeerInfo *peer_info;
3044 struct MeshTunnel *t;
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Received a path ACK msg [%s]\n",
3047 GNUNET_i2s (&my_full_id));
3048 msg = (struct GNUNET_MESH_PathACK *) message;
3049 t = tunnel_get (&msg->oid, msg->tid);
3052 /* TODO notify that we don't know the tunnel */
3056 /* Message for us? */
3057 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
3060 if (NULL == t->client)
3062 GNUNET_break_op (0);
3065 if (NULL != t->dht_get_type)
3067 GNUNET_DHT_get_stop (t->dht_get_type);
3068 t->dht_get_type = NULL;
3070 peer_info = peer_info_get (&msg->peer_id);
3071 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3072 send_client_peer_connected (t, peer_info->id);
3076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3077 "MESH: not for us, retransmitting...\n");
3078 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3079 peer_info = peer_info_get (&msg->oid);
3080 if (NULL == peer_info)
3082 /* If we know the tunnel, we should DEFINITELY know the peer */
3086 send_message (message, &id);
3092 * Functions to handle messages from core
3094 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3095 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3096 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3097 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3098 sizeof (struct GNUNET_MESH_PathBroken)},
3099 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3100 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3101 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3102 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3103 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3104 sizeof (struct GNUNET_MESH_PathACK)},
3110 /******************************************************************************/
3111 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3112 /******************************************************************************/
3115 * deregister_app: iterator for removing each application registered by a client
3117 * @param cls closure
3118 * @param key the hash of the application id (used to access the hashmap)
3119 * @param value the value stored at the key (client)
3121 * @return GNUNET_OK on success
3124 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3126 GNUNET_break (GNUNET_YES ==
3127 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3134 * notify_client_connection_failure: notify a client that the connection to the
3135 * requested remote peer is not possible (for instance, no route found)
3136 * Function called when the socket is ready to queue more data. "buf" will be
3137 * NULL and "size" zero if the socket was closed for writing in the meantime.
3139 * @param cls closure
3140 * @param size number of bytes available in buf
3141 * @param buf where the callee should write the message
3142 * @return number of bytes written to buf
3145 notify_client_connection_failure (void *cls, size_t size, void *buf)
3148 struct MeshPeerInfo *peer_info;
3149 struct GNUNET_MESH_PeerControl *msg;
3150 struct GNUNET_PeerIdentity id;
3152 if (0 == size && NULL == buf)
3154 // TODO retry? cancel?
3158 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3159 peer_info = (struct MeshPeerInfo *) cls;
3160 msg = (struct GNUNET_MESH_PeerControl *) buf;
3161 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3162 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3163 // msg->tunnel_id = htonl(peer_info->t->tid);
3164 GNUNET_PEER_resolve (peer_info->id, &id);
3165 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3173 * Send keepalive packets for a peer
3175 * @param cls Closure (tunnel for which to send the keepalive).
3176 * @param tc Notification context.
3178 * TODO: implement explicit multicast keepalive?
3181 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3183 struct MeshTunnel *t = cls;
3184 struct GNUNET_MessageHeader *payload;
3185 struct GNUNET_MESH_Multicast *msg;
3187 sizeof (struct GNUNET_MESH_Multicast) +
3188 sizeof (struct GNUNET_MessageHeader);
3191 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3195 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3198 "MESH: sending keepalive for tunnel %d\n", t->id.tid);
3200 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3201 msg->header.size = htons (size);
3202 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3203 msg->oid = my_full_id;
3204 msg->tid = htonl (t->id.tid);
3205 payload = (struct GNUNET_MessageHeader *) &msg[1];
3206 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3207 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3208 tunnel_send_multicast (t, &msg->header);
3210 t->path_refresh_task =
3211 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3217 * Function to process paths received for a new peer addition. The recorded
3218 * paths form the initial tunnel, which can be optimized later.
3219 * Called on each result obtained for the DHT search.
3221 * @param cls closure
3222 * @param exp when will this value expire
3223 * @param key key of the result
3224 * @param type type of the result
3225 * @param size number of bytes in data
3226 * @param data pointer to the result data
3228 * TODO: re-issue the request after certain time? cancel after X results?
3231 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3232 const GNUNET_HashCode * key,
3233 const struct GNUNET_PeerIdentity *get_path,
3234 unsigned int get_path_length,
3235 const struct GNUNET_PeerIdentity *put_path,
3236 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3237 size_t size, const void *data)
3239 struct MeshPathInfo *path_info = cls;
3240 struct MeshPeerPath *p;
3241 struct GNUNET_PeerIdentity pi;
3244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got results from DHT!\n");
3245 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n", GNUNET_i2s (&pi));
3247 // GNUNET_DHT_get_stop(path_info->peer->dhtget);
3248 // path_info->peer->dhtget = NULL;
3250 p = path_build_from_dht (get_path, get_path_length, put_path,
3252 peer_info_add_path (path_info->peer, p, GNUNET_NO);
3253 for (i = 0; i < path_info->peer->ntunnels; i++)
3255 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3256 peer_info_connect (path_info->peer, path_info->t);
3258 // GNUNET_free (path_info);
3265 * Function to process paths received for a new peer addition. The recorded
3266 * paths form the initial tunnel, which can be optimized later.
3267 * Called on each result obtained for the DHT search.
3269 * @param cls closure
3270 * @param exp when will this value expire
3271 * @param key key of the result
3272 * @param type type of the result
3273 * @param size number of bytes in data
3274 * @param data pointer to the result data
3277 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3278 const GNUNET_HashCode * key,
3279 const struct GNUNET_PeerIdentity *get_path,
3280 unsigned int get_path_length,
3281 const struct GNUNET_PeerIdentity *put_path,
3282 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3283 size_t size, const void *data)
3285 const struct GNUNET_PeerIdentity *pi = data;
3286 struct MeshTunnel *t = cls;
3287 struct MeshPeerInfo *peer_info;
3288 struct MeshPeerPath *p;
3290 if (size != sizeof (struct GNUNET_PeerIdentity))
3292 GNUNET_break_op (0);
3295 GNUNET_assert (NULL != t->client);
3296 peer_info = peer_info_get (pi);
3297 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3299 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3301 p = path_build_from_dht (get_path, get_path_length, put_path,
3303 peer_info_add_path (peer_info, p, GNUNET_NO);
3304 tunnel_add_peer (t, peer_info);
3305 peer_info_connect (peer_info, t);
3309 /******************************************************************************/
3310 /********************* MESH LOCAL HANDLES **************************/
3311 /******************************************************************************/
3315 * Handler for client disconnection
3317 * @param cls closure
3318 * @param client identification of the client; NULL
3319 * for the last call when the server is destroyed
3322 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3324 struct MeshClient *c;
3325 struct MeshClient *next;
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3336 if (c->handle != client && NULL != client)
3338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
3343 GNUNET_SERVER_client_drop (c->handle);
3344 if (NULL != c->tunnels)
3346 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3347 &tunnel_destroy_iterator, c);
3348 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3351 /* deregister clients applications */
3352 if (NULL != c->apps)
3354 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3355 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3357 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3358 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3360 GNUNET_SCHEDULER_cancel (announce_applications_task);
3361 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3363 if (NULL != c->types)
3364 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3366 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT FREE at %p\n", c);
3372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3378 * Handler for new clients
3380 * @param cls closure
3381 * @param client identification of the client
3382 * @param message the actual message, which includes messages the client wants
3385 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3386 const struct GNUNET_MessageHeader *message)
3388 struct GNUNET_MESH_ClientConnect *cc_msg;
3389 struct MeshClient *c;
3390 GNUNET_MESH_ApplicationType *a;
3397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3398 /* Check data sanity */
3399 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3400 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3401 ntypes = ntohs (cc_msg->types);
3402 napps = ntohs (cc_msg->applications);
3404 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3411 /* Create new client structure */
3412 c = GNUNET_malloc (sizeof (struct MeshClient));
3414 c->id = next_client_id++;
3415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT NEW %u at %p\n", c->id,
3419 GNUNET_SERVER_client_keep (client);
3420 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3423 GNUNET_MESH_ApplicationType at;
3426 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3427 for (i = 0; i < napps; i++)
3430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3431 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3432 /* store in clients hashmap */
3433 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3434 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3435 /* store in global hashmap, for announcements */
3436 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3437 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3439 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3440 announce_applications_task =
3441 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3449 t = (uint16_t *) & a[napps];
3450 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3451 for (i = 0; i < ntypes; i++)
3454 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3456 /* store in clients hashmap */
3457 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3458 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3459 /* store in global hashmap */
3460 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3461 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3465 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3467 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3468 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3469 GNUNET_SERVER_notification_context_add (nc, client);
3471 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3479 * Handler for requests of new tunnels
3481 * @param cls closure
3482 * @param client identification of the client
3483 * @param message the actual message
3486 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3487 const struct GNUNET_MessageHeader *message)
3489 struct GNUNET_MESH_TunnelMessage *t_msg;
3490 struct MeshTunnel *t;
3491 struct MeshClient *c;
3492 GNUNET_HashCode hash;
3494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3496 /* Sanity check for client registration */
3497 if (NULL == (c = client_get (client)))
3500 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3507 /* Message sanity check */
3508 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3511 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3515 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3516 /* Sanity check for tunnel numbering */
3517 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3520 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3523 /* Sanity check for duplicate tunnel IDs */
3524 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3527 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3531 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3532 while (NULL != tunnel_get_by_pi (myid, next_tid))
3533 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3534 t->id.tid = next_tid++;
3535 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3537 t->local_tid = ntohl (t_msg->tunnel_id);
3539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL %s [%x] (%x)\n",
3540 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3543 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3545 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3547 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3548 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3551 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3555 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3557 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3558 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3561 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3564 t->tree = tree_new (myid);
3566 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3572 * Handler for requests of deleting tunnels
3574 * @param cls closure
3575 * @param client identification of the client
3576 * @param message the actual message
3579 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3580 const struct GNUNET_MessageHeader *message)
3582 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3583 struct MeshClient *c;
3584 struct MeshTunnel *t;
3585 MESH_TunnelNumber tid;
3586 GNUNET_HashCode hash;
3588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3589 "MESH: Got a DESTROY TUNNEL from client!\n");
3591 /* Sanity check for client registration */
3592 if (NULL == (c = client_get (client)))
3595 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3598 /* Message sanity check */
3599 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3602 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3608 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3610 /* Retrieve tunnel */
3611 tid = ntohl (tunnel_msg->tunnel_id);
3613 /* Remove from local id hashmap */
3614 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3615 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3616 GNUNET_assert (GNUNET_YES ==
3617 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t));
3620 tunnel_send_destroy (t);
3622 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3628 * Handler for connection requests to new peers
3630 * @param cls closure
3631 * @param client identification of the client
3632 * @param message the actual message (PeerControl)
3635 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3636 const struct GNUNET_MessageHeader *message)
3638 struct GNUNET_MESH_PeerControl *peer_msg;
3639 struct MeshPeerInfo *peer_info;
3640 struct MeshClient *c;
3641 struct MeshTunnel *t;
3642 MESH_TunnelNumber tid;
3644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3645 /* Sanity check for client registration */
3646 if (NULL == (c = client_get (client)))
3649 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3653 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3654 /* Sanity check for message size */
3655 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3658 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3662 /* Tunnel exists? */
3663 tid = ntohl (peer_msg->tunnel_id);
3664 t = tunnel_get_by_local_id (c, tid);
3668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3672 /* Does client own tunnel? */
3673 if (t->client->handle != client)
3676 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3680 GNUNET_i2s (&peer_msg->peer));
3681 peer_info = peer_info_get (&peer_msg->peer);
3683 tunnel_add_peer (t, peer_info);
3684 peer_info_connect (peer_info, t);
3686 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3692 * Handler for disconnection requests of peers in a tunnel
3694 * @param cls closure
3695 * @param client identification of the client
3696 * @param message the actual message (PeerControl)
3699 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3700 const struct GNUNET_MessageHeader *message)
3702 struct GNUNET_MESH_PeerControl *peer_msg;
3703 struct MeshPeerInfo *peer_info;
3704 struct MeshClient *c;
3705 struct MeshTunnel *t;
3706 MESH_TunnelNumber tid;
3708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got a PEER DEL request\n");
3709 /* Sanity check for client registration */
3710 if (NULL == (c = client_get (client)))
3713 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3716 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3717 /* Sanity check for message size */
3718 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3721 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3725 /* Tunnel exists? */
3726 tid = ntohl (peer_msg->tunnel_id);
3727 t = tunnel_get_by_local_id (c, tid);
3731 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: on tunnel %X\n", t->id.tid);
3736 /* Does client own tunnel? */
3737 if (t->client->handle != client)
3740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for peer %s\n",
3745 GNUNET_i2s (&peer_msg->peer));
3746 /* Is the peer in the tunnel? */
3748 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
3749 if (NULL == peer_info)
3752 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3756 /* Ok, delete peer from tunnel */
3757 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3758 &peer_msg->peer.hashPubKey);
3760 send_destroy_path (t, peer_info->id);
3761 tunnel_delete_peer (t, peer_info->id);
3762 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3768 * Handler for connection requests to new peers by type
3770 * @param cls closure
3771 * @param client identification of the client
3772 * @param message the actual message (ConnectPeerByType)
3775 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3776 const struct GNUNET_MessageHeader *message)
3778 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3779 struct MeshClient *c;
3780 struct MeshTunnel *t;
3781 GNUNET_HashCode hash;
3782 MESH_TunnelNumber tid;
3784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3785 /* Sanity check for client registration */
3786 if (NULL == (c = client_get (client)))
3789 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3793 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3794 /* Sanity check for message size */
3795 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3796 ntohs (connect_msg->header.size))
3799 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3803 /* Tunnel exists? */
3804 tid = ntohl (connect_msg->tunnel_id);
3805 t = tunnel_get_by_local_id (c, tid);
3809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3813 /* Does client own tunnel? */
3814 if (t->client->handle != client)
3817 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3821 /* Do WE have the service? */
3822 t->type = ntohl (connect_msg->type);
3823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3824 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3825 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3828 /* Yes! Fast forward, add ourselves to the tunnel and send the
3829 * good news to the client
3831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3832 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3833 peer_info_get (&my_full_id),
3834 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3837 send_client_peer_connected (t, myid);
3838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3839 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3842 /* Ok, lets find a peer offering the service */
3843 if (NULL != t->dht_get_type)
3845 GNUNET_DHT_get_stop (t->dht_get_type);
3847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3848 GNUNET_h2s (&hash));
3850 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
3851 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
3852 GNUNET_DHT_RO_RECORD_ROUTE |
3853 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
3854 &dht_get_type_handler, t);
3856 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3862 * Handler for client traffic directed to one peer
3864 * @param cls closure
3865 * @param client identification of the client
3866 * @param message the actual message
3869 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3870 const struct GNUNET_MessageHeader *message)
3872 struct MeshClient *c;
3873 struct MeshTunnel *t;
3874 struct MeshPeerInfo *pi;
3875 struct GNUNET_MESH_Unicast *data_msg;
3876 MESH_TunnelNumber tid;
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3880 "MESH: Got a unicast request from a client!\n");
3882 /* Sanity check for client registration */
3883 if (NULL == (c = client_get (client)))
3886 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3889 data_msg = (struct GNUNET_MESH_Unicast *) message;
3890 /* Sanity check for message size */
3891 size = ntohs (message->size);
3892 if (sizeof (struct GNUNET_MESH_Unicast) +
3893 sizeof (struct GNUNET_MessageHeader) > size)
3896 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3900 /* Tunnel exists? */
3901 tid = ntohl (data_msg->tid);
3902 t = tunnel_get_by_local_id (c, tid);
3906 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3910 /* Is it a local tunnel? Then, does client own the tunnel? */
3911 if (NULL != t->client && NULL != t->client->handle &&
3912 t->client->handle != client)
3915 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3919 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3920 &data_msg->destination.hashPubKey);
3921 /* Is the selected peer in the tunnel? */
3925 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3929 /* Ok, everything is correct, send the message
3930 * (pretend we got it from a mesh peer)
3933 char buf[ntohs (message->size)];
3934 struct GNUNET_MESH_Unicast *copy;
3936 /* Work around const limitation */
3937 copy = (struct GNUNET_MESH_Unicast *) buf;
3938 memcpy (buf, data_msg, size);
3939 copy->oid = my_full_id;
3940 copy->tid = htonl (t->id.tid);
3941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3942 "MESH: calling generic handler...\n");
3943 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
3945 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3951 * Handler for client traffic directed to the origin
3953 * @param cls closure
3954 * @param client identification of the client
3955 * @param message the actual message
3958 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3959 const struct GNUNET_MessageHeader *message)
3961 struct GNUNET_MESH_ToOrigin *data_msg;
3962 struct GNUNET_PeerIdentity id;
3963 struct MeshClient *c;
3964 struct MeshTunnel *t;
3965 MESH_TunnelNumber tid;
3968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3969 "MESH: Got a ToOrigin request from a client!\n");
3971 /* Sanity check for client registration */
3972 if (NULL == (c = client_get (client)))
3975 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3978 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3979 /* Sanity check for message size */
3980 size = ntohs (message->size);
3981 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3982 sizeof (struct GNUNET_MessageHeader) > size)
3985 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3989 /* Tunnel exists? */
3990 tid = ntohl (data_msg->tid);
3991 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3997 t = tunnel_get_by_local_id (c, tid);
4001 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4005 /* It shouldn't be a local tunnel. */
4006 if (NULL != t->client)
4009 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4012 GNUNET_PEER_resolve (t->id.oid, &id);
4014 /* Ok, everything is correct, send the message
4015 * (pretend we got it from a mesh peer)
4018 char buf[ntohs (message->size)];
4019 struct GNUNET_MESH_ToOrigin *copy;
4021 /* Work around const limitation */
4022 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4023 memcpy (buf, data_msg, size);
4025 copy->tid = htonl (t->id.tid);
4026 copy->sender = my_full_id;
4027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4028 "MESH: calling generic handler...\n");
4029 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4031 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4037 * Handler for client traffic directed to all peers in a tunnel
4039 * @param cls closure
4040 * @param client identification of the client
4041 * @param message the actual message
4044 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4045 const struct GNUNET_MessageHeader *message)
4047 struct MeshClient *c;
4048 struct MeshTunnel *t;
4049 struct GNUNET_MESH_Multicast *data_msg;
4050 MESH_TunnelNumber tid;
4052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4053 "MESH: Got a multicast request from a client!\n");
4055 /* Sanity check for client registration */
4056 if (NULL == (c = client_get (client)))
4059 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4062 data_msg = (struct GNUNET_MESH_Multicast *) message;
4063 /* Sanity check for message size */
4064 if (sizeof (struct GNUNET_MESH_Multicast) +
4065 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4068 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4072 /* Tunnel exists? */
4073 tid = ntohl (data_msg->tid);
4074 t = tunnel_get_by_local_id (c, tid);
4078 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4082 /* Does client own tunnel? */
4083 if (t->client->handle != client)
4086 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4091 char buf[ntohs (message->size)];
4092 struct GNUNET_MESH_Multicast *copy;
4094 copy = (struct GNUNET_MESH_Multicast *) buf;
4095 memcpy (buf, message, ntohs (message->size));
4096 copy->oid = my_full_id;
4097 copy->tid = htonl (t->id.tid);
4098 copy->ttl = htonl (DEFAULT_TTL);
4099 copy->mid = htonl (t->mid + 1);
4100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4101 "MESH: calling generic handler...\n");
4102 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4105 /* receive done gets called when last copy is sent to a neighbor */
4110 * Functions to handle messages from clients
4112 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4113 {&handle_local_new_client, NULL,
4114 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4115 {&handle_local_tunnel_create, NULL,
4116 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4117 sizeof (struct GNUNET_MESH_TunnelMessage)},
4118 {&handle_local_tunnel_destroy, NULL,
4119 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4120 sizeof (struct GNUNET_MESH_TunnelMessage)},
4121 {&handle_local_connect_add, NULL,
4122 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4123 sizeof (struct GNUNET_MESH_PeerControl)},
4124 {&handle_local_connect_del, NULL,
4125 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4126 sizeof (struct GNUNET_MESH_PeerControl)},
4127 {&handle_local_connect_by_type, NULL,
4128 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4129 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4130 {&handle_local_unicast, NULL,
4131 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4132 {&handle_local_to_origin, NULL,
4133 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4134 {&handle_local_multicast, NULL,
4135 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4141 * To be called on core init/fail.
4143 * @param cls service closure
4144 * @param server handle to the server for this service
4145 * @param identity the public identity of this peer
4148 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4149 const struct GNUNET_PeerIdentity *identity)
4151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
4152 core_handle = server;
4153 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
4157 GNUNET_SCHEDULER_shutdown ();
4163 * Method called whenever a given peer connects.
4165 * @param cls closure
4166 * @param peer peer identity this notification is about
4167 * @param atsi performance data for the connection
4168 * @param atsi_count number of records in 'atsi'
4171 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4172 const struct GNUNET_ATS_Information *atsi,
4173 unsigned int atsi_count)
4175 struct MeshPeerInfo *peer_info;
4176 struct MeshPeerPath *path;
4178 #if MESH_DEBUG_CONNECTION
4179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
4180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4181 GNUNET_i2s (&my_full_id));
4183 peer_info = peer_info_get (peer);
4184 if (myid == peer_info->id)
4186 #if MESH_DEBUG_CONNECTION
4187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4191 #if MESH_DEBUG_CONNECTION
4194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", GNUNET_i2s (peer));
4197 path = path_new (2);
4198 path->peers[0] = myid;
4199 path->peers[1] = peer_info->id;
4200 GNUNET_PEER_change_rc (myid, 1);
4201 GNUNET_PEER_change_rc (peer_info->id, 1);
4202 peer_info_add_path (peer_info, path, GNUNET_YES);
4207 * Method called whenever a peer disconnects.
4209 * @param cls closure
4210 * @param peer peer identity this notification is about
4213 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4215 struct MeshPeerInfo *pi;
4218 #if MESH_DEBUG_CONNECTION
4219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
4221 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4227 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4229 /* TODO: notify that the transmission failed */
4230 peer_info_cancel_transmission (pi, i);
4232 peer_info_remove_path (pi, pi->id, myid);
4233 #if MESH_DEBUG_CONNECTION
4236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4243 /******************************************************************************/
4244 /************************ MAIN FUNCTIONS ****************************/
4245 /******************************************************************************/
4248 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4250 * @param cls closure
4251 * @param key current key code
4252 * @param value value in the hash map
4253 * @return GNUNET_YES if we should continue to iterate,
4257 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4259 struct MeshTunnel *t = value;
4266 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4268 * @param cls closure
4269 * @param key current key code
4270 * @param value value in the hash map
4271 * @return GNUNET_YES if we should continue to iterate,
4275 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4277 struct MeshPeerInfo *p = value;
4279 peer_info_destroy (p);
4284 * Task run during shutdown.
4290 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4293 /* TODO: destroy tunnels? */
4294 if (core_handle != NULL)
4296 GNUNET_CORE_disconnect (core_handle);
4299 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4300 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4301 if (dht_handle != NULL)
4303 GNUNET_DHT_disconnect (dht_handle);
4308 GNUNET_SERVER_notification_context_destroy (nc);
4311 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4313 GNUNET_SCHEDULER_cancel (announce_id_task);
4314 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4320 * Process mesh requests.
4322 * @param cls closure
4323 * @param server the initialized server
4324 * @param c configuration to use
4327 run (void *cls, struct GNUNET_SERVER_Handle *server,
4328 const struct GNUNET_CONFIGURATION_Handle *c)
4330 struct MeshPeerInfo *peer;
4331 struct MeshPeerPath *p;
4334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4335 server_handle = server;
4336 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4337 CORE_QUEUE_SIZE, /* queue size */
4338 NULL, /* Closure passed to MESH functions */
4339 &core_init, /* Call core_init once connected */
4340 &core_connect, /* Handle connects */
4341 &core_disconnect, /* remove peers on disconnects */
4342 NULL, /* Don't notify about all incoming messages */
4343 GNUNET_NO, /* For header only in notification */
4344 NULL, /* Don't notify about all outbound messages */
4345 GNUNET_NO, /* For header-only out notification */
4346 core_handlers); /* Register these handlers */
4348 if (core_handle == NULL)
4351 GNUNET_SCHEDULER_shutdown ();
4356 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4361 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4362 GNUNET_SCHEDULER_shutdown ();
4365 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4366 GNUNET_free (keyfile);
4367 if (my_private_key == NULL)
4369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4370 _("Mesh service could not access hostkey. Exiting.\n"));
4371 GNUNET_SCHEDULER_shutdown ();
4374 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4375 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4376 &my_full_id.hashPubKey);
4377 myid = GNUNET_PEER_intern (&my_full_id);
4379 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4386 dht_handle = GNUNET_DHT_connect (c, 64);
4387 if (dht_handle == NULL)
4393 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4395 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4396 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4397 peers = GNUNET_CONTAINER_multihashmap_create (32);
4398 applications = GNUNET_CONTAINER_multihashmap_create (32);
4399 types = GNUNET_CONTAINER_multihashmap_create (32);
4401 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4402 nc = GNUNET_SERVER_notification_context_create (server_handle,
4404 GNUNET_SERVER_disconnect_notify (server_handle,
4405 &handle_local_client_disconnect, NULL);
4409 clients_tail = NULL;
4414 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4415 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4417 /* Create a peer_info for the local peer */
4418 peer = peer_info_get (&my_full_id);
4421 GNUNET_PEER_change_rc (myid, 1);
4422 peer_info_add_path (peer, p, GNUNET_YES);
4424 /* Scheduled the task to clean up when shutdown is called */
4425 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4432 * The main function for the mesh service.
4434 * @param argc number of arguments from the command line
4435 * @param argv command line arguments
4436 * @return 0 ok, 1 on error
4439 main (int argc, char *const *argv)
4443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4446 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");