2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - relay corking down to core
48 #include "mesh_protocol.h"
49 #include "gnunet_dht_service.h"
50 #include "mesh_tunnel_tree.h"
52 /* TODO: move into configuration file */
53 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
54 GNUNET_TIME_UNIT_SECONDS,\
56 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
57 GNUNET_TIME_UNIT_SECONDS,\
60 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
61 GNUNET_TIME_UNIT_SECONDS,\
64 #define GET_RESTART_TIME GNUNET_TIME_relative_multiply(\
65 GNUNET_TIME_UNIT_SECONDS,\
68 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
69 GNUNET_TIME_UNIT_SECONDS,\
72 /******************************************************************************/
73 /************************ DATA STRUCTURES ****************************/
74 /******************************************************************************/
76 /** FWD declaration */
80 * Struct containing all info possibly needed to build a package when called
83 struct MeshDataDescriptor
85 /** ID of the tunnel this packet travels in */
86 struct MESH_TunnelID *origin;
91 /** Client that asked for the transmission, if any */
92 struct GNUNET_SERVER_Client *client;
94 /** Who was this message being sent to */
95 struct MeshPeerInfo *peer;
97 /** Ultimate destination of the packet */
98 GNUNET_PEER_Id destination;
100 /** Number of identical messages sent to different hops (multicast) */
101 unsigned int *copies;
103 /** Which handler was used to request the transmission */
104 unsigned int handler_n;
106 /** Size of the data */
109 /** Used to allow a client send more traffic to the service after a
110 * previous packet was tried to be sent to a neighbor and couldn't */
111 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
116 * Struct containing all information regarding a given peer
126 * Last time we heard from this peer
128 struct GNUNET_TIME_Absolute last_contact;
131 * Number of attempts to reconnect so far
133 int n_reconnect_attempts;
136 * Paths to reach the peer, ordered by ascending hop count
138 struct MeshPeerPath *path_head;
141 * Paths to reach the peer, ordered by ascending hop count
143 struct MeshPeerPath *path_tail;
146 * Handle to stop the DHT search for a path to this peer
148 struct GNUNET_DHT_GetHandle *dhtget;
151 * Handles to stop queued transmissions for this peer
153 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
156 * Pointer to info stuctures used as cls for queued transmissions
158 void *infos[CORE_QUEUE_SIZE];
161 * Type of message being in each transmission
163 uint16_t types[CORE_QUEUE_SIZE];
166 * Array of tunnels this peer participates in
167 * (most probably a small amount, therefore not a hashmap)
168 * When the path to the peer changes, notify these tunnels to let them
169 * re-adjust their path trees.
171 struct MeshTunnel **tunnels;
174 * Number of tunnels this peers participates in
176 unsigned int ntunnels;
181 * Data scheduled to transmit (to local client or remote peer)
188 struct MeshQueue *next;
189 struct MeshQueue *prev;
192 * Target of the data (NULL if target is client)
194 struct MeshPeerInfo *peer;
197 * Client to send the data to (NULL if target is peer)
199 struct MeshClient *client;
202 * Size of the message to transmit
207 * How old is the data?
209 struct GNUNET_TIME_Absolute timestamp;
214 struct GNUNET_MessageHeader *data;
218 * Globally unique tunnel identification (owner + number)
219 * DO NOT USE OVER THE NETWORK
224 * Node that owns the tunnel
229 * Tunnel number to differentiate all the tunnels owned by the node oid
230 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
232 MESH_TunnelNumber tid;
236 struct MeshClient; /* FWD declaration */
239 * Struct containing all information regarding a tunnel
240 * For an intermediate node the improtant info used will be:
241 * - id Tunnel unique identification
242 * - paths[0] To know where to send it next
243 * - metainfo: ready, speeds, accounting
250 struct MESH_TunnelID id;
253 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
255 MESH_TunnelNumber local_tid;
258 * Last time the tunnel was used
260 struct GNUNET_TIME_Absolute timestamp;
263 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
264 * containing peers added by id or by type, not intermediate peers.
266 struct GNUNET_CONTAINER_MultiHashMap *peers;
269 * Number of peers that are connected and potentially ready to receive data
271 unsigned int peers_ready;
274 * Number of peers that have been added to the tunnel
276 unsigned int peers_total;
279 * Client owner of the tunnel, if any
281 struct MeshClient *client;
284 * Messages ready to transmit
286 struct MeshQueue *queue_head;
287 struct MeshQueue *queue_tail;
292 struct MeshTunnelTree *tree;
295 * Application type we are looking for in this tunnel
297 GNUNET_MESH_ApplicationType type;
300 * Used to search peers offering a service
302 struct GNUNET_DHT_GetHandle *dht_get_type;
305 * Task to keep the used paths alive
307 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
312 * Info needed to work with tunnel paths and peers
319 struct MeshTunnel *t;
322 * Neighbouring peer to whom we send the packet to
324 struct MeshPeerInfo *peer;
329 struct MeshPeerPath *path;
332 * Position in peer's transmit queue
339 * Struct containing information about a client of the service
346 struct MeshClient *next;
347 struct MeshClient *prev;
350 * Tunnels that belong to this client, indexed by local id
352 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
355 * Handle to communicate with the client
357 struct GNUNET_SERVER_Client *handle;
360 * Applications that this client has claimed to provide
362 struct GNUNET_CONTAINER_MultiHashMap *apps;
365 * Messages that this client has declared interest in
367 struct GNUNET_CONTAINER_MultiHashMap *types;
371 * ID of the client, for debug messages
380 /******************************************************************************/
381 /************************ DEBUG FUNCTIONS ****************************/
382 /******************************************************************************/
386 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
387 * @param cls string to print
388 * @param tc task context
391 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
395 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
403 /******************************************************************************/
404 /*********************** GLOBAL VARIABLES ****************************/
405 /******************************************************************************/
410 static struct MeshClient *clients;
411 static struct MeshClient *clients_tail;
414 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
416 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
419 * Tunnels incoming, indexed by MESH_TunnelNumber
420 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
422 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
425 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
427 static struct GNUNET_CONTAINER_MultiHashMap *peers;
430 * Handle to communicate with core
432 static struct GNUNET_CORE_Handle *core_handle;
437 static struct GNUNET_DHT_Handle *dht_handle;
442 static struct GNUNET_SERVER_Handle *server_handle;
445 * Notification context, to send messages to local clients
447 static struct GNUNET_SERVER_NotificationContext *nc;
450 * Local peer own ID (memory efficient handle)
452 static GNUNET_PEER_Id myid;
455 * Local peer own ID (full value)
457 static struct GNUNET_PeerIdentity my_full_id;
462 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
467 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
470 * Tunnel ID for the next created tunnel (global tunnel number)
472 static MESH_TunnelNumber next_tid;
475 * Tunnel ID for the next incoming tunnel (local tunnel number)
477 static MESH_TunnelNumber next_local_tid;
480 * All application types provided by this peer
482 static struct GNUNET_CONTAINER_MultiHashMap *applications;
485 * All message types clients of this peer are interested in
487 static struct GNUNET_CONTAINER_MultiHashMap *types;
490 * Task to periodically announce provided applications
492 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
495 * Task to periodically announce itself in the network
497 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
500 unsigned int next_client_id;
504 /******************************************************************************/
505 /************************ ITERATORS ****************************/
506 /******************************************************************************/
508 /* FIXME move iterators here */
511 /******************************************************************************/
512 /************************ PERIODIC FUNCTIONS ****************************/
513 /******************************************************************************/
516 * Announce iterator over for each application provided by the peer
519 * @param key current key code
520 * @param value value in the hash map
521 * @return GNUNET_YES if we should continue to
526 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
528 /* FIXME are hashes in multihash map equal on all aquitectures? */
529 GNUNET_DHT_put (dht_handle,
532 GNUNET_DHT_RO_RECORD_ROUTE |
533 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
534 GNUNET_BLOCK_TYPE_TEST,
535 sizeof (struct GNUNET_PeerIdentity),
536 (const char *) &my_full_id,
538 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
539 &mesh_debug, "DHT_put for app completed");
541 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
543 APP_ANNOUNCE_TIME, NULL, NULL);
550 * Periodically announce what applications are provided by local clients
553 * @param tc task context
556 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
558 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
560 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
564 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
566 announce_applications_task =
567 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
575 * Periodically announce self id in the DHT
578 * @param tc task context
581 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
583 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
585 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
589 * - Set data expiration in function of X
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
593 GNUNET_h2s_full (&my_full_id.hashPubKey));
594 GNUNET_DHT_put (dht_handle, /* DHT handle */
595 &my_full_id.hashPubKey, /* Key to use */
596 10U, /* Replication level */
597 GNUNET_DHT_RO_RECORD_ROUTE |
598 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
599 GNUNET_BLOCK_TYPE_TEST, /* Block type */
600 sizeof(my_full_id), /* Size of the data */
601 (char *)&my_full_id, /* Data itself */
602 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
603 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
605 &mesh_debug, "DHT_put for id completed");
607 NULL, /* Continuation */
608 NULL); /* Continuation closure */
611 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
616 * Function to process paths received for a new peer addition. The recorded
617 * paths form the initial tunnel, which can be optimized later.
618 * Called on each result obtained for the DHT search.
621 * @param exp when will this value expire
622 * @param key key of the result
623 * @param type type of the result
624 * @param size number of bytes in data
625 * @param data pointer to the result data
628 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
629 const GNUNET_HashCode * key,
630 const struct GNUNET_PeerIdentity *get_path,
631 unsigned int get_path_length,
632 const struct GNUNET_PeerIdentity *put_path,
633 unsigned int put_path_length,
634 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
637 /******************************************************************************/
638 /****************** GENERAL HELPER FUNCTIONS ************************/
639 /******************************************************************************/
642 * Check if client has registered with the service and has not disconnected
644 * @param client the client to check
646 * @return non-NULL if client exists in the global DLL
648 static struct MeshClient *
649 client_get (struct GNUNET_SERVER_Client *client)
651 struct MeshClient *c;
656 if (c->handle == client)
665 * Checks if a given client has subscribed to certain message type
667 * @param message_type Type of message to check
668 * @param c Client to check
670 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
675 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
679 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
680 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
684 client_allow_send(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
686 struct MeshDataDescriptor *info = cls;
688 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
690 info->timeout_task = GNUNET_SCHEDULER_NO_TASK;
691 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
696 * Search for a tunnel by global ID using full PeerIdentities
698 * @param oid owner of the tunnel
699 * @param tid global tunnel number
701 * @return tunnel handler, NULL if doesn't exist
703 static struct MeshTunnel *
704 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
708 * Send the message to all clients that have subscribed to its type
710 * @param msg Pointer to the message itself
711 * @return number of clients this message was sent to
714 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
715 const struct GNUNET_MessageHeader *payload)
717 struct GNUNET_PeerIdentity *oid;
718 struct MeshClient *c;
719 struct MeshTunnel *t;
720 MESH_TunnelNumber *tid;
723 char cbuf[htons(msg->size)];
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
726 type = ntohs (payload->type);
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
729 memcpy (cbuf, msg, sizeof(cbuf));
730 switch (htons(msg->type))
732 struct GNUNET_MESH_Unicast *uc;
733 struct GNUNET_MESH_Multicast *mc;
734 struct GNUNET_MESH_ToOrigin *to;
736 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
737 uc = (struct GNUNET_MESH_Unicast *) cbuf;
741 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
742 mc = (struct GNUNET_MESH_Multicast *) cbuf;
746 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
747 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
755 t = tunnel_get (oid, ntohl(*tid));
761 *tid = htonl (t->local_tid);
762 for (count = 0, c = clients; c != NULL; c = c->next)
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
765 if (client_is_subscribed (type, c))
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
769 GNUNET_SERVER_notification_context_unicast (
772 (struct GNUNET_MessageHeader *) cbuf,
781 * Notify the client that owns the tunnel that a peer has connected to it
783 * @param t Tunnel whose owner to notify
784 * @param id Short id of the peer that has connected
787 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
789 struct GNUNET_MESH_PeerControl pc;
791 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
792 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
793 pc.tunnel_id = htonl (t->local_tid);
794 GNUNET_PEER_resolve (id, &pc.peer);
795 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
796 &pc.header, GNUNET_NO);
801 * Function called to notify a client about the socket
802 * being ready to queue more data. "buf" will be
803 * NULL and "size" zero if the socket was closed for
804 * writing in the meantime.
807 * @param size number of bytes available in buf
808 * @param buf where the callee should write the message
809 * @return number of bytes written to buf
812 send_core_create_path (void *cls, size_t size, void *buf);
816 * Function called to notify a client about the socket
817 * being ready to queue more data. "buf" will be
818 * NULL and "size" zero if the socket was closed for
819 * writing in the meantime.
821 * @param cls closure (data itself)
822 * @param size number of bytes available in buf
823 * @param buf where the callee should write the message
825 * @return number of bytes written to buf
828 send_core_data_multicast (void *cls, size_t size, void *buf);
832 * Cancel a core transmission that was already requested and free all resources
833 * associated to the request.
835 * @param peer PeeInfo of the peer whose transmission is cancelled.
836 * @param i Position of the transmission to be cancelled.
839 peer_info_cancel_transmission(struct MeshPeerInfo *peer, unsigned int i)
841 if (NULL != peer->core_transmit[i])
843 struct MeshDataDescriptor *dd;
844 struct MeshPathInfo *path_info;
848 struct GNUNET_PeerIdentity id;
850 GNUNET_PEER_resolve (peer->id, &id);
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
852 "MESH: Cancelling data transmission at %s [%u]\n",
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
856 "MESH: message type %u\n",
860 /* TODO: notify that tranmission has failed */
861 switch (peer->types[i])
863 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
864 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
865 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
868 if (0 == --(*dd->copies))
870 GNUNET_free (dd->copies);
871 GNUNET_free (dd->data);
874 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
876 path_info = peer->infos[i];
877 path_destroy(path_info->path);
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
883 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
884 peer->core_transmit[i] = NULL;
885 GNUNET_free (peer->infos[i]);
891 * Get a unused CORE slot to transmit a message to a peer. If all the slots
892 * are used, cancel one and return it's position.
894 * @param peer PeerInfo of the neighbor we want to transmit to.
896 * @return The index of an available slot to transmit to the neighbor.
899 peer_info_transmit_slot (struct MeshPeerInfo *peer)
903 for (i = 0; peer->core_transmit[i]; i++)
905 if (i == (CORE_QUEUE_SIZE - 1))
907 /* All positions are taken! Overwriting! */
909 peer_info_cancel_transmission(peer, 0);
918 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
919 * and insert it in the appropiate structures if the peer is not known yet.
921 * @param peer Full identity of the peer.
923 * @return Existing or newly created peer info.
925 static struct MeshPeerInfo *
926 peer_info_get (const struct GNUNET_PeerIdentity *peer)
928 struct MeshPeerInfo *peer_info;
930 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
931 if (NULL == peer_info)
934 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
935 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
936 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
937 peer_info->id = GNUNET_PEER_intern (peer);
945 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
946 * and insert it in the appropiate structures if the peer is not known yet.
948 * @param peer Short identity of the peer.
950 * @return Existing or newly created peer info.
952 static struct MeshPeerInfo *
953 peer_info_get_short (const GNUNET_PEER_Id peer)
955 struct GNUNET_PeerIdentity id;
957 GNUNET_PEER_resolve(peer, &id);
958 return peer_info_get(&id);
963 * Iterator to remove the tunnel from the list of tunnels a peer participates
966 * @param cls Closure (tunnel info)
967 * @param key GNUNET_PeerIdentity of the peer (unused)
968 * @param value PeerInfo of the peer
970 * @return always GNUNET_YES, to keep iterating
973 peer_info_delete_tunnel (void* cls, const GNUNET_HashCode* key, void* value)
975 struct MeshTunnel *t = cls;
976 struct MeshPeerInfo *peer = value;
979 for (i = 0; i < peer->ntunnels; i++)
981 if (0 == memcmp(&peer->tunnels[i]->id,
983 sizeof(struct MESH_TunnelID)))
986 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
987 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
996 * Sends a CREATE PATH message for a path to a peer, properly registrating
997 * all used resources.
999 * @param peer PeerInfo of the final peer for whom this path is being created.
1000 * @param p Path itself.
1001 * @param t Tunnel for which the path is created.
1004 send_create_path (struct MeshPeerInfo *peer,
1005 struct MeshPeerPath *p,
1006 struct MeshTunnel *t)
1008 struct GNUNET_PeerIdentity id;
1009 struct MeshPathInfo *path_info;
1010 struct MeshPeerInfo *neighbor;
1015 p = tree_get_path_to_peer(t->tree, peer->id);
1022 for (i = 0; i < p->length; i++)
1024 if (p->peers[i] == myid)
1027 if (i >= p->length - 1)
1033 GNUNET_PEER_resolve(p->peers[i + 1], &id);
1035 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1036 path_info->path = p;
1038 neighbor = peer_info_get(&id);
1039 path_info->peer = neighbor;
1040 path_info->pos = peer_info_transmit_slot(neighbor);
1041 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1042 neighbor->infos[path_info->pos] = path_info;
1043 neighbor->core_transmit[path_info->pos] =
1044 GNUNET_CORE_notify_transmit_ready (
1045 core_handle, /* handle */
1048 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1050 sizeof (struct GNUNET_MESH_ManipulatePath)
1051 + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1052 &send_core_create_path, /* callback */
1053 path_info); /* cls */
1058 * Try to establish a new connection to this peer.
1059 * Use the best path for the given tunnel.
1060 * If the peer doesn't have any path to it yet, try to get one.
1061 * If the peer already has some path, send a CREATE PATH towards it.
1063 * @param peer PeerInfo of the peer.
1064 * @param t Tunnel for which to create the path, if possible.
1067 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1069 struct MeshPeerPath *p;
1070 struct MeshPathInfo *path_info;
1072 if (NULL != peer->path_head)
1074 p = tree_get_path_to_peer(t->tree, peer->id);
1077 send_create_path(peer, p, t);
1082 send_client_peer_connected(t, myid);
1085 else if (NULL == peer->dhtget)
1087 struct GNUNET_PeerIdentity id;
1089 GNUNET_PEER_resolve(peer->id, &id);
1090 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1091 path_info->peer = peer;
1094 GNUNET_DHT_get_start(dht_handle, /* handle */
1095 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1096 GNUNET_BLOCK_TYPE_TEST, /* type */
1097 &id.hashPubKey, /* key to search */
1098 4, /* replication level */
1099 GNUNET_DHT_RO_RECORD_ROUTE |
1100 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1102 0, /* xquery bits */
1103 &dht_get_id_handler,
1106 /* Otherwise, there is no path but the DHT get is already started. */
1112 * Destroy the peer_info and free any allocated resources linked to it
1113 * @param t tunnel the path belongs to
1114 * @param pi the peer_info to destroy
1115 * @return GNUNET_OK on success
1118 peer_info_destroy (struct MeshPeerInfo *pi)
1120 GNUNET_HashCode hash;
1121 struct GNUNET_PeerIdentity id;
1123 GNUNET_PEER_resolve (pi->id, &id);
1124 GNUNET_PEER_change_rc (pi->id, -1);
1125 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
1127 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
1128 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
1136 * Notify a tunnel that a connection has broken that affects at least
1137 * some of its peers.
1139 * @param t Tunnel affected.
1140 * @param peer Peer that (at least) has been affected by the disconnection.
1141 * @param p1 Peer that got disconnected from p2.
1142 * @param p2 Peer that got disconnected from p1.
1144 * @return Short ID of the peer disconnected (either p1 or p2).
1145 * 0 if the tunnel remained unaffected.
1147 static GNUNET_PEER_Id
1148 tunnel_notify_connection_broken (struct MeshTunnel *t,
1149 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1153 * Remove all paths that rely on a direct connection between p1 and p2
1154 * from the peer itself and notify all tunnels about it.
1156 * @param peer PeerInfo of affected peer.
1157 * @param p1 GNUNET_PEER_Id of one peer.
1158 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1161 * TODO: optimize (see below)
1164 path_remove_from_peer (struct MeshPeerInfo *peer,
1168 struct GNUNET_PeerIdentity id;
1169 struct MeshPeerPath *p;
1170 struct MeshPeerPath *aux;
1171 struct MeshPeerInfo *peer_d;
1173 unsigned int destroyed;
1179 p = peer->path_head;
1183 for (i = 0; i < (p->length - 1); i++)
1185 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1186 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1198 for (i = 0; i < peer->ntunnels; i++)
1200 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
1202 * Problem: one or more peers have been deleted from the tunnel tree.
1203 * We don't know who they are to try to add them again.
1204 * We need to try to find a new path for each of the disconnected peers.
1205 * Some of them might already have a path to reach them that does not
1206 * involve p1 and p2. Adding all anew might render in a better tree than
1207 * the trivial immediate fix.
1209 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1210 * its children will be reachable trough him.
1212 peer_d = peer_info_get_short(d);
1215 for (p = peer_d->path_head; NULL != p; p = p->next)
1217 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
1225 /* No callback, as peer will be already disconnected */
1226 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
1230 struct MeshPathInfo *path_info;
1232 if (NULL != peer_d->dhtget)
1234 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1235 path_info->path = p;
1236 path_info->peer = peer_d;
1237 path_info->t = peer->tunnels[i];
1239 GNUNET_DHT_get_start(dht_handle, /* handle */
1240 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1241 GNUNET_BLOCK_TYPE_TEST, /* type */
1242 &id.hashPubKey, /*key to search */
1243 4, /* replication level */
1244 GNUNET_DHT_RO_RECORD_ROUTE |
1245 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1247 0, /* xquery bits */
1248 &dht_get_id_handler,
1249 (void *) path_info);
1256 * Add the path to the peer and update the path used to reach it in case this
1259 * @param peer_info Destination peer to add the path to.
1260 * @param path New path to add. Last peer must be the peer in arg 1.
1261 * Path will be either used of freed if already known.
1263 * TODO: trim the part from origin to us? Add it as path to origin?
1266 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1268 struct MeshPeerPath *aux;
1272 if (NULL == peer_info || NULL == path)
1278 l = path_get_length (path);
1285 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1287 l2 = path_get_length (aux);
1290 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1291 peer_info->path_tail, aux, path);
1295 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1302 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1309 * Add the path to the origin peer and update the path used to reach it in case
1310 * this is the shortest.
1311 * The path is given in peer_info -> destination, therefore we turn the path
1312 * upside down first.
1314 * @param peer_info Peer to add the path to, being the origin of the path.
1315 * @param path New path to add after being inversed.
1318 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1321 path_add_to_peer (peer_info, path);
1326 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1327 * to obtain a local peer -> destination path and interning the peer ids.
1329 * @return Newly allocated and created path
1331 static struct MeshPeerPath *
1332 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1333 unsigned int get_path_length,
1334 const struct GNUNET_PeerIdentity *put_path,
1335 unsigned int put_path_length)
1337 struct MeshPeerPath *p;
1343 GNUNET_PEER_change_rc(myid, 1);
1344 i = get_path_length;
1345 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1346 for (i--; i >= 0; i--)
1348 id = GNUNET_PEER_intern (&get_path[i]);
1349 if (p->length > 0 && id == p->peers[p->length - 1])
1351 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1352 GNUNET_PEER_change_rc(id, -1);
1356 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1357 "MESH: Adding from GET: %s.\n",
1358 GNUNET_i2s(&get_path[i]));
1360 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1361 p->peers[p->length - 1] = id;
1364 i = put_path_length;
1365 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1366 for (i--; i >= 0; i--)
1368 id = GNUNET_PEER_intern (&put_path[i]);
1371 /* PUT path went through us, so discard the path up until now and start
1372 * from here to get a much shorter (and loop-free) path.
1377 if (p->length > 0 && id == p->peers[p->length - 1])
1379 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1380 GNUNET_PEER_change_rc(id, -1);
1384 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1385 "MESH: Adding from PUT: %s.\n",
1386 GNUNET_i2s(&put_path[i]));
1388 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1389 p->peers[p->length - 1] = id;
1393 if (get_path_length > 0)
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "MESH: (first of GET: %s)\n",
1396 GNUNET_h2s_full(&get_path[0].hashPubKey));
1397 if (put_path_length > 0)
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1399 "MESH: (first of PUT: %s)\n",
1400 GNUNET_h2s_full(&put_path[0].hashPubKey));
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "MESH: In total: %d hops\n",
1404 for (i = 0; i < p->length; i++)
1406 struct GNUNET_PeerIdentity peer_id;
1408 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412 GNUNET_h2s_full(&peer_id.hashPubKey));
1420 * Send keepalive packets for a peer
1422 * @param cls Closure (tunnel for which to send the keepalive).
1423 * @param tc Notification context.
1425 * TODO: implement explicit multicast keepalive?
1428 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1432 * Search for a tunnel among the tunnels for a client
1434 * @param c the client whose tunnels to search in
1435 * @param tid the local id of the tunnel
1437 * @return tunnel handler, NULL if doesn't exist
1439 static struct MeshTunnel *
1440 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1442 GNUNET_HashCode hash;
1444 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1445 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1447 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1449 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1454 * Search for a tunnel by global ID using PEER_ID
1456 * @param pi owner of the tunnel
1457 * @param tid global tunnel number
1459 * @return tunnel handler, NULL if doesn't exist
1461 static struct MeshTunnel *
1462 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1464 struct MESH_TunnelID id;
1465 GNUNET_HashCode hash;
1470 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1471 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1476 * Search for a tunnel by global ID using full PeerIdentities
1478 * @param oid owner of the tunnel
1479 * @param tid global tunnel number
1481 * @return tunnel handler, NULL if doesn't exist
1483 static struct MeshTunnel *
1484 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1486 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1491 * Callback used to notify a client owner of a tunnel that a peer has
1492 * disconnected, most likely because of a path change.
1494 * @param n Node in the tree representing the disconnected peer
1497 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1499 struct MeshPeerInfo *peer;
1501 if (NULL != n->t->client && NULL != nc)
1503 struct GNUNET_MESH_PeerControl msg;
1504 msg.header.size = htons (sizeof (msg));
1505 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1506 msg.tunnel_id = htonl (n->t->local_tid);
1507 GNUNET_PEER_resolve (n->peer, &msg.peer);
1508 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1509 &msg.header, GNUNET_NO);
1511 peer = peer_info_get_short(n->peer);
1512 peer_info_connect(peer, n->t);
1517 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1518 * needed rescources.
1519 * If peer already exists, reevaluate shortest path and change if different.
1521 * @param t Tunnel we want to add a new peer to
1522 * @param peer PeerInfo of the peer being added
1526 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1528 struct GNUNET_PeerIdentity id;
1529 struct MeshPeerPath *best_p;
1530 struct MeshPeerPath *p;
1531 unsigned int best_cost;
1534 GNUNET_PEER_resolve(peer->id, &id);
1536 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1539 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1540 GNUNET_CONTAINER_multihashmap_put(
1544 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1547 if (NULL != (p = peer->path_head))
1550 best_cost = path_get_cost(t->tree, p);
1553 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1560 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1561 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1562 t->path_refresh_task =
1563 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1567 /* Start a DHT get if necessary */
1568 peer_info_connect(peer, t);
1573 * Add a path to a tunnel which we don't own, just to remember the next hop.
1574 * If destination node was already in the tunnel, the first hop information
1575 * will be replaced with the new path.
1577 * @param t Tunnel we want to add a new peer to
1578 * @param p Path to add
1579 * @param own_pos Position of local node in path.
1583 tunnel_add_path (struct MeshTunnel *t,
1584 struct MeshPeerPath *p,
1585 unsigned int own_pos)
1587 struct GNUNET_PeerIdentity id;
1589 GNUNET_assert (0 != own_pos);
1590 tree_add_path(t->tree, p, NULL);
1591 if (NULL == t->tree->me)
1592 t->tree->me = tree_find_peer(t->tree->root, p->peers[own_pos]);
1593 if (own_pos < p->length - 1)
1595 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1596 tree_update_first_hops(t->tree, t->tree->me, &id);
1602 * Notify a tunnel that a connection has broken that affects at least
1603 * some of its peers.
1605 * @param t Tunnel affected.
1606 * @param peer Peer that (at least) has been affected by the disconnection.
1607 * @param p1 Peer that got disconnected from p2.
1608 * @param p2 Peer that got disconnected from p1.
1610 * @return Short ID of the peer disconnected (either p1 or p2).
1611 * 0 if the tunnel remained unaffected.
1613 static GNUNET_PEER_Id
1614 tunnel_notify_connection_broken (struct MeshTunnel *t,
1615 struct MeshPeerInfo *peer,
1619 return tree_notify_connection_broken (t->tree, p1, p2,
1620 ¬ify_peer_disconnected);
1625 * Send a message in a tunnel in multicast, sending a copy to each child node
1626 * down the local one in the tunnel tree.
1628 * @param t Tunnel in which to send the data.
1629 * @param msg Message to be sent
1631 * @return Number of copies sent.
1634 tunnel_send_multicast (struct MeshTunnel *t,
1635 const struct GNUNET_MessageHeader *msg)
1637 struct GNUNET_PeerIdentity neighbor;
1638 struct MeshDataDescriptor *info;
1639 struct MeshTunnelTreeNode *n;
1640 unsigned int *copies;
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1646 "MESH: sending a multicast packet...\n");
1647 size = ntohs (msg->size);
1648 GNUNET_assert (NULL != t->tree->me);
1649 n = t->tree->me->children_head;
1652 copies = GNUNET_malloc (sizeof (unsigned int));
1653 for (*copies = 0; NULL != n; n = n->next)
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (%u copies)\n", *copies);
1656 n = t->tree->me->children_head;
1657 data = GNUNET_malloc (size);
1658 memcpy (data, msg, size);
1661 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1662 info->origin = &t->id;
1665 info->copies = copies;
1666 if (NULL != t->client)
1668 info->client = t->client->handle;
1669 info->timeout_task = GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT,
1673 info->destination = n->peer;
1674 GNUNET_PEER_resolve (n->peer, &neighbor);
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "MESH: sending to %s...\n",
1677 GNUNET_i2s (&neighbor));
1678 info->peer = peer_info_get(&neighbor);
1679 GNUNET_assert (NULL != info->peer);
1680 i = peer_info_transmit_slot(info->peer);
1681 info->handler_n = i;
1682 info->peer->infos[i] = info;
1683 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1684 info->peer->core_transmit[i] =
1685 GNUNET_CORE_notify_transmit_ready (core_handle,
1688 GNUNET_TIME_UNIT_FOREVER_REL,
1691 &send_core_data_multicast, info);
1699 * Destroy the tunnel and free any allocated resources linked to it
1701 * @param t the tunnel to destroy
1703 * @return GNUNET_OK on success
1706 tunnel_destroy (struct MeshTunnel *t)
1708 struct MeshClient *c;
1709 struct MeshQueue *q;
1710 struct MeshQueue *qn;
1711 GNUNET_HashCode hash;
1720 struct GNUNET_PeerIdentity id;
1722 GNUNET_PEER_resolve(t->id.oid, &id);
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1724 "MESH: destroying tunnel %s [%x]\n",
1728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1732 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1733 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1738 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1740 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1744 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1746 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1747 GNUNET_break (GNUNET_YES ==
1748 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t));
1751 if (NULL != t->peers)
1753 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
1754 &peer_info_delete_tunnel, t);
1755 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1760 if (NULL != q->data)
1761 GNUNET_free (q->data);
1765 /* TODO cancel core transmit ready in case it was active */
1767 tree_destroy(t->tree);
1768 if (NULL != t->dht_get_type)
1769 GNUNET_DHT_get_stop(t->dht_get_type);
1776 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1777 * client when the client disconnects.
1779 * @param cls closure (client that is disconnecting)
1780 * @param key the hash of the local tunnel id (used to access the hashmap)
1781 * @param value the value stored at the key (tunnel to destroy)
1783 * @return GNUNET_OK on success
1786 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1788 struct MeshTunnel *t = value;
1791 if (NULL != t->dht_get_type)
1793 GNUNET_DHT_get_stop (t->dht_get_type);
1795 r = tunnel_destroy (t);
1800 /******************************************************************************/
1801 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1802 /******************************************************************************/
1805 * Function called to notify a client about the socket
1806 * being ready to queue more data. "buf" will be
1807 * NULL and "size" zero if the socket was closed for
1808 * writing in the meantime.
1810 * @param cls closure
1811 * @param size number of bytes available in buf
1812 * @param buf where the callee should write the message
1813 * @return number of bytes written to buf
1816 send_core_create_path (void *cls, size_t size, void *buf)
1818 struct MeshPathInfo *info = cls;
1819 struct GNUNET_MESH_ManipulatePath *msg;
1820 struct GNUNET_PeerIdentity *peer_ptr;
1821 struct MeshPeerInfo *peer = info->peer;
1822 struct MeshTunnel *t = info->t;
1823 struct MeshPeerPath *p = info->path;
1827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1828 "MESH: CREATE PATH sending...\n");
1830 sizeof (struct GNUNET_MESH_ManipulatePath) +
1831 p->length * sizeof (struct GNUNET_PeerIdentity);
1833 if (size < size_needed || NULL == buf)
1835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
1836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
1837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
1839 info->peer->core_transmit[info->pos] =
1840 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1841 GNUNET_TIME_UNIT_FOREVER_REL,
1842 path_get_first_hop (t->tree, peer->id),
1844 &send_core_create_path,
1848 info->peer->core_transmit[info->pos] = NULL;
1851 struct GNUNET_PeerIdentity id;
1853 GNUNET_PEER_resolve (peer->id, &id);
1854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1855 "MESH: setting core_transmit %s [%u] to NULL\n",
1860 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1861 msg->header.size = htons (size_needed);
1862 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1863 msg->tid = ntohl (t->id.tid);
1865 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1866 for (i = 0; i < p->length; i++)
1868 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1875 "MESH: CREATE PATH (%u bytes long) sent!\n",
1883 * Function called to notify a client about the socket
1884 * being ready to queue more data. "buf" will be
1885 * NULL and "size" zero if the socket was closed for
1886 * writing in the meantime.
1888 * @param cls closure (MeshDataDescriptor with all info to build packet)
1889 * @param size number of bytes available in buf
1890 * @param buf where the callee should write the message
1891 * @return number of bytes written to buf
1894 send_core_data_to_origin (void *cls, size_t size, void *buf)
1896 struct MeshDataDescriptor *info = cls;
1897 struct GNUNET_MESH_ToOrigin *msg = buf;
1900 GNUNET_assert (NULL != info);
1901 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1902 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1904 if (total_size > size)
1906 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1907 "not enough buffer to send data to origin\n");
1910 msg->header.size = htons (total_size);
1911 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1912 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1913 msg->tid = htonl (info->origin->tid);
1914 if (0 != info->size)
1916 memcpy (&msg[1], &info[1], info->size);
1918 if (NULL != info->client)
1920 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1929 * Function called to notify a client about the socket
1930 * being ready to queue more data. "buf" will be
1931 * NULL and "size" zero if the socket was closed for
1932 * writing in the meantime.
1934 * @param cls closure (data itself)
1935 * @param size number of bytes available in buf
1936 * @param buf where the callee should write the message
1938 * @return number of bytes written to buf
1941 send_core_data_multicast (void *cls, size_t size, void *buf)
1943 struct MeshDataDescriptor *info = cls;
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
1947 GNUNET_assert (NULL != info);
1948 GNUNET_assert (NULL != info->peer);
1949 total_size = info->size;
1950 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1952 if (total_size > size)
1955 struct GNUNET_PeerIdentity id;
1957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1958 "MESH: Multicast: retransmitting... (%u/%u)\n",
1960 GNUNET_PEER_resolve(info->peer->id, &id);
1961 info->peer->core_transmit[info->handler_n] =
1962 GNUNET_CORE_notify_transmit_ready (core_handle,
1965 GNUNET_TIME_UNIT_FOREVER_REL,
1968 &send_core_data_multicast,
1972 info->peer->core_transmit[info->handler_n] = NULL;
1973 info->peer->infos[info->handler_n] = NULL;
1974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
1975 memcpy (buf, info->data, total_size);
1978 struct GNUNET_MESH_Multicast *mc;
1979 struct GNUNET_MessageHeader *mh;
1982 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
1984 mc = (struct GNUNET_MESH_Multicast *) mh;
1985 mh = (struct GNUNET_MessageHeader *) &mc[1];
1986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1987 "MESH: multicast, payload type %u\n",
1989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1990 "MESH: multicast, payload size %u\n",
1995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2001 if (0 == --(*info->copies))
2003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
2004 if (NULL != info->client)
2006 if (GNUNET_SCHEDULER_NO_TASK != info->timeout_task)
2008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2009 "MESH: cancelling client timeout (%u)...\n",
2010 info->timeout_task);
2011 GNUNET_SCHEDULER_cancel(info->timeout_task);
2012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
2013 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
2016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing memory...\n");
2017 GNUNET_free (info->data);
2018 GNUNET_free (info->copies);
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2028 * Function called to notify a client about the socket
2029 * being ready to queue more data. "buf" will be
2030 * NULL and "size" zero if the socket was closed for
2031 * writing in the meantime.
2033 * @param cls closure (MeshDataDescriptor)
2034 * @param size number of bytes available in buf
2035 * @param buf where the callee should write the message
2036 * @return number of bytes written to buf
2039 send_core_path_ack (void *cls, size_t size, void *buf)
2041 struct MeshDataDescriptor *info = cls;
2042 struct GNUNET_MESH_PathACK *msg = buf;
2044 GNUNET_assert (NULL != info);
2047 info->peer->core_transmit[info->handler_n] = NULL;
2049 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2054 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2055 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2056 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2057 msg->tid = htonl (info->origin->tid);
2058 msg->peer_id = my_full_id;
2060 /* TODO add signature */
2062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2063 return sizeof (struct GNUNET_MESH_PathACK);
2068 * Function called to notify a client about the socket
2069 * being ready to queue more data. "buf" will be
2070 * NULL and "size" zero if the socket was closed for
2071 * writing in the meantime.
2073 * @param cls closure (data itself)
2074 * @param size number of bytes available in buf
2075 * @param buf where the callee should write the message
2076 * @return number of bytes written to buf
2079 send_core_data_raw (void *cls, size_t size, void *buf)
2081 struct GNUNET_MessageHeader *msg = cls;
2084 GNUNET_assert (NULL != msg);
2085 total_size = ntohs (msg->size);
2087 if (total_size > size)
2092 memcpy (buf, msg, total_size);
2100 * Send another peer a notification to destroy a tunnel
2101 * @param cls The tunnel to destroy
2102 * @param size Size in the buffer
2103 * @param buf Memory where to put the data to transmit
2104 * @return Size of data put in buffer
2107 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
2109 struct MeshTunnel *t = cls;
2110 struct MeshClient *c;
2111 struct GNUNET_MESH_TunnelMessage *msg;
2115 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2116 /*FIXME*/ msg->header.size =
2117 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
2118 msg->tunnel_id = htonl (t->id.tid);
2120 tunnel_destroy (c, t);
2121 return sizeof (struct GNUNET_MESH_TunnelMessage);
2126 /******************************************************************************/
2127 /******************** MESH NETWORK HANDLERS **************************/
2128 /******************************************************************************/
2132 * Core handler for path creation
2133 * struct GNUNET_CORE_MessageHandler
2135 * @param cls closure
2136 * @param message message
2137 * @param peer peer identity this notification is about
2138 * @param atsi performance data
2139 * @return GNUNET_OK to keep the connection open,
2140 * GNUNET_SYSERR to close it (signal serious error)
2144 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2145 const struct GNUNET_MessageHeader *message,
2146 const struct GNUNET_ATS_Information *atsi)
2148 unsigned int own_pos;
2151 MESH_TunnelNumber tid;
2152 struct GNUNET_MESH_ManipulatePath *msg;
2153 struct GNUNET_PeerIdentity *pi;
2154 GNUNET_HashCode hash;
2155 struct MeshPeerPath *path;
2156 struct MeshPeerInfo *dest_peer_info;
2157 struct MeshPeerInfo *orig_peer_info;
2158 struct MeshTunnel *t;
2160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2161 "MESH: Received a path create msg [%s]\n",
2162 GNUNET_i2s(&my_full_id));
2163 size = ntohs (message->size);
2164 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2166 GNUNET_break_op (0);
2170 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2171 if (size % sizeof (struct GNUNET_PeerIdentity))
2173 GNUNET_break_op (0);
2176 size /= sizeof (struct GNUNET_PeerIdentity);
2179 GNUNET_break_op (0);
2182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2183 "MESH: path has %u hops.\n",
2185 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2187 tid = ntohl (msg->tid);
2188 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2189 t = tunnel_get (pi, tid);
2190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2191 "MESH: path is for tunnel %s [%X].\n",
2196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2197 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2198 t->id.oid = GNUNET_PEER_intern (pi);
2200 t->local_tid = next_local_tid++;
2201 /* FIXME test if taken */
2202 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2203 t->tree = tree_new(t, t->id.oid);
2205 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2207 GNUNET_CONTAINER_multihashmap_put (
2211 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2217 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2219 GNUNET_CONTAINER_multihashmap_put (
2223 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2231 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2232 if (NULL == dest_peer_info)
2234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2235 "MESH: Creating PeerInfo for destination.\n");
2236 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2237 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2238 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2240 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2242 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2243 if (NULL == orig_peer_info)
2245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2246 "MESH: Creating PeerInfo for origin.\n");
2247 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2248 orig_peer_info->id = GNUNET_PEER_intern (pi);
2249 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2250 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2253 path = path_new (size);
2255 for (i = 0; i < size; i++)
2257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2258 "MESH: ... adding %s\n",
2259 GNUNET_i2s(&pi[i]));
2260 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2261 if (path->peers[i] == myid)
2264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2265 "MESH: Own position: %u\n", own_pos);
2268 /* cannot be self, must be 'not found' */
2269 /* create path: self not found in path through self */
2270 GNUNET_break_op (0);
2271 path_destroy (path);
2272 /* FIXME error. destroy tunnel? leave for timeout? */
2275 tunnel_add_path (t, path, own_pos);
2276 t->tree->me = tree_find_peer(t->tree->root, myid);
2277 if (own_pos == size - 1)
2279 /* It is for us! Send ack. */
2280 struct GNUNET_MESH_TunnelNotification cmsg;
2281 struct MeshDataDescriptor *info;
2284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2285 "MESH: It's for us!\n");
2286 path_add_to_origin (orig_peer_info, path);
2287 t->peers = GNUNET_CONTAINER_multihashmap_create(4);
2288 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2290 &my_full_id.hashPubKey,
2291 peer_info_get(&my_full_id),
2292 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2293 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2294 info->origin = &t->id;
2295 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2296 GNUNET_assert (NULL != info->peer);
2297 j = peer_info_transmit_slot(info->peer);
2298 info->handler_n = j;
2299 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2300 info->peer->infos[j] = info;
2301 info->peer->core_transmit[j] =
2302 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2303 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2304 sizeof (struct GNUNET_MESH_PathACK),
2305 &send_core_path_ack, info);
2306 cmsg.header.size = htons(sizeof(cmsg));
2307 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2308 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2309 cmsg.tunnel_id = htonl(t->local_tid);
2310 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2314 struct MeshPeerPath *path2;
2316 /* It's for somebody else! Retransmit. */
2317 path2 = path_duplicate(path);
2318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2319 "MESH: Retransmitting.\n");
2320 path_add_to_peer(dest_peer_info, path);
2321 path = path_duplicate(path2);
2322 path_add_to_origin(orig_peer_info, path2);
2323 send_create_path(dest_peer_info, path, t);
2330 * Core handler for mesh network traffic going from the origin to a peer
2332 * @param cls closure
2333 * @param peer peer identity this notification is about
2334 * @param message message
2335 * @param atsi performance data
2336 * @return GNUNET_OK to keep the connection open,
2337 * GNUNET_SYSERR to close it (signal serious error)
2340 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2341 const struct GNUNET_MessageHeader *message,
2342 const struct GNUNET_ATS_Information *atsi)
2344 struct GNUNET_MESH_Unicast *msg;
2345 struct MeshTunnel *t;
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2350 "MESH: got a unicast packet from %s\n",
2352 size = ntohs (message->size);
2354 sizeof (struct GNUNET_MESH_Unicast) +
2355 sizeof (struct GNUNET_MessageHeader))
2360 msg = (struct GNUNET_MESH_Unicast *) message;
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2362 "MESH: of type %u\n",
2363 ntohs (msg[1].header.type));
2364 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2367 /* TODO notify back: we don't know this tunnel */
2368 GNUNET_break_op (0);
2371 pid = GNUNET_PEER_search(&msg->destination);
2374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2375 "MESH: it's for us! sending to clients...\n");
2376 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2380 "MESH: not for us, retransmitting...\n");
2381 msg = GNUNET_malloc (size);
2382 memcpy (msg, message, size);
2383 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2384 GNUNET_TIME_UNIT_FOREVER_REL,
2385 path_get_first_hop (t->tree, pid),
2387 &send_core_data_raw, msg);
2393 * Core handler for mesh network traffic going from the origin to all peers
2395 * @param cls closure
2396 * @param message message
2397 * @param peer peer identity this notification is about
2398 * @param atsi performance data
2399 * @return GNUNET_OK to keep the connection open,
2400 * GNUNET_SYSERR to close it (signal serious error)
2402 * TODO: Check who we got this from, to validate route.
2405 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2406 const struct GNUNET_MessageHeader *message,
2407 const struct GNUNET_ATS_Information *atsi)
2409 struct GNUNET_MESH_Multicast *msg;
2410 struct MeshTunnel *t;
2413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2414 "MESH: got a multicast packet from %s\n",
2416 size = ntohs (message->size);
2417 if (sizeof (struct GNUNET_MESH_Multicast) +
2418 sizeof (struct GNUNET_MessageHeader) > size)
2420 GNUNET_break_op (0);
2423 msg = (struct GNUNET_MESH_Multicast *) message;
2424 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2428 /* TODO notify that we dont know that tunnel */
2429 GNUNET_break_op (0);
2433 /* Transmit to locally interested clients */
2434 if (NULL != t->peers &&
2435 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2437 send_subscribed_clients (message, &msg[1].header);
2439 tunnel_send_multicast(t, message);
2445 * Core handler for mesh network traffic toward the owner of a tunnel
2447 * @param cls closure
2448 * @param message message
2449 * @param peer peer identity this notification is about
2450 * @param atsi performance data
2452 * @return GNUNET_OK to keep the connection open,
2453 * GNUNET_SYSERR to close it (signal serious error)
2456 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2457 const struct GNUNET_MessageHeader *message,
2458 const struct GNUNET_ATS_Information *atsi)
2460 struct GNUNET_MESH_ToOrigin *msg;
2461 struct GNUNET_PeerIdentity id;
2462 struct MeshPeerInfo *peer_info;
2463 struct MeshTunnel *t;
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2467 "MESH: got a ToOrigin packet from %s\n",
2469 size = ntohs (message->size);
2470 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2471 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2473 GNUNET_break_op (0);
2476 msg = (struct GNUNET_MESH_ToOrigin *) message;
2477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2478 "MESH: of type %u\n",
2479 ntohs (msg[1].header.type));
2480 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2484 /* TODO notify that we dont know this tunnel (whom)? */
2485 GNUNET_break_op (0);
2489 if (t->id.oid == myid)
2492 struct GNUNET_MESH_ToOrigin *copy;
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 "MESH: it's for us! sending to clients...\n");
2496 if (NULL == t->client)
2498 /* got data packet for ownerless tunnel */
2499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2500 "MESH: no clients!\n");
2501 GNUNET_break_op (0);
2504 /* TODO signature verification */
2505 memcpy (cbuf, message, size);
2506 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2507 copy->tid = htonl (t->local_tid);
2508 GNUNET_SERVER_notification_context_unicast (nc,
2514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2515 "MESH: not for us, retransmitting...\n");
2517 peer_info = peer_info_get (&msg->oid);
2518 if (NULL == peer_info)
2520 /* unknown origin of tunnel */
2524 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
2525 msg = GNUNET_malloc (size);
2526 memcpy (msg, message, size);
2527 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2528 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
2529 &send_core_data_raw, msg);
2536 * Core handler for path ACKs
2538 * @param cls closure
2539 * @param message message
2540 * @param peer peer identity this notification is about
2541 * @param atsi performance data
2543 * @return GNUNET_OK to keep the connection open,
2544 * GNUNET_SYSERR to close it (signal serious error)
2547 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2548 const struct GNUNET_MessageHeader *message,
2549 const struct GNUNET_ATS_Information *atsi)
2551 struct GNUNET_MESH_PathACK *msg;
2552 struct GNUNET_PeerIdentity id;
2553 struct MeshTunnelTreeNode *n;
2554 struct MeshPeerInfo *peer_info;
2555 struct MeshTunnel *t;
2557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2558 "MESH: Received a path ACK msg [%s]\n",
2559 GNUNET_i2s(&my_full_id));
2560 msg = (struct GNUNET_MESH_PathACK *) message;
2561 t = tunnel_get (&msg->oid, msg->tid);
2564 /* TODO notify that we don't know the tunnel */
2568 /* Message for us? */
2569 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2572 "MESH: It's for us!\n");
2573 if (NULL == t->client)
2575 GNUNET_break_op (0);
2578 peer_info = peer_info_get (&msg->peer_id);
2579 if (NULL == peer_info)
2581 GNUNET_break_op (0);
2584 n = tree_find_peer(t->tree->root, peer_info->id);
2587 GNUNET_break_op (0);
2590 n->status = MESH_PEER_READY;
2591 send_client_peer_connected(t, peer_info->id);
2595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2596 "MESH: not for us, retransmitting...\n");
2597 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
2598 peer_info = peer_info_get (&msg->oid);
2599 if (NULL == peer_info)
2601 /* If we know the tunnel, we should DEFINITELY know the peer */
2605 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2606 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2607 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2608 GNUNET_TIME_UNIT_FOREVER_REL,
2610 sizeof (struct GNUNET_MESH_PathACK),
2611 &send_core_data_raw, msg);
2617 * Functions to handle messages from core
2619 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2620 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2621 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2622 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2623 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2624 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2625 sizeof (struct GNUNET_MESH_PathACK)},
2631 /******************************************************************************/
2632 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2633 /******************************************************************************/
2636 * deregister_app: iterator for removing each application registered by a client
2638 * @param cls closure
2639 * @param key the hash of the application id (used to access the hashmap)
2640 * @param value the value stored at the key (client)
2642 * @return GNUNET_OK on success
2645 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2647 GNUNET_break (GNUNET_YES ==
2648 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
2654 * notify_client_connection_failure: notify a client that the connection to the
2655 * requested remote peer is not possible (for instance, no route found)
2656 * Function called when the socket is ready to queue more data. "buf" will be
2657 * NULL and "size" zero if the socket was closed for writing in the meantime.
2659 * @param cls closure
2660 * @param size number of bytes available in buf
2661 * @param buf where the callee should write the message
2662 * @return number of bytes written to buf
2665 notify_client_connection_failure (void *cls, size_t size, void *buf)
2668 struct MeshPeerInfo *peer_info;
2669 struct GNUNET_MESH_PeerControl *msg;
2670 struct GNUNET_PeerIdentity id;
2672 if (0 == size && NULL == buf)
2674 // TODO retry? cancel?
2678 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2679 peer_info = (struct MeshPeerInfo *) cls;
2680 msg = (struct GNUNET_MESH_PeerControl *) buf;
2681 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2682 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2683 // msg->tunnel_id = htonl(peer_info->t->tid);
2684 GNUNET_PEER_resolve (peer_info->id, &id);
2685 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2693 * Send keepalive packets for a peer
2695 * @param cls Closure (tunnel for which to send the keepalive).
2696 * @param tc Notification context.
2698 * TODO: implement explicit multicast keepalive?
2701 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2703 struct MeshTunnel *t = cls;
2704 struct GNUNET_MessageHeader *payload;
2705 struct GNUNET_MESH_Multicast *msg;
2708 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2712 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2715 "MESH: sending keepalive for tunnel %d\n",
2718 size = sizeof(struct GNUNET_MESH_Multicast) +
2719 sizeof(struct GNUNET_MessageHeader);
2720 msg = GNUNET_malloc (size);
2721 msg->header.size = htons (size);
2722 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2723 msg->oid = my_full_id;
2724 msg->tid = htonl(t->id.tid);
2725 payload = (struct GNUNET_MessageHeader *) &msg[1];
2726 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2727 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2728 handle_mesh_data_multicast (NULL, &my_full_id, &msg->header, NULL);
2731 t->path_refresh_task =
2732 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2738 * Function to process paths received for a new peer addition. The recorded
2739 * paths form the initial tunnel, which can be optimized later.
2740 * Called on each result obtained for the DHT search.
2742 * @param cls closure
2743 * @param exp when will this value expire
2744 * @param key key of the result
2745 * @param type type of the result
2746 * @param size number of bytes in data
2747 * @param data pointer to the result data
2749 * TODO: re-issue the request after certain time? cancel after X results?
2752 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2753 const GNUNET_HashCode * key,
2754 const struct GNUNET_PeerIdentity *get_path,
2755 unsigned int get_path_length,
2756 const struct GNUNET_PeerIdentity *put_path,
2757 unsigned int put_path_length,
2758 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2760 struct MeshPathInfo *path_info = cls;
2761 struct MeshPeerPath *p;
2762 struct GNUNET_PeerIdentity pi;
2765 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2766 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2767 "MESH: Got results from DHT for %s\n",
2768 GNUNET_h2s_full(&pi.hashPubKey));
2769 GNUNET_DHT_get_stop(path_info->peer->dhtget);
2770 path_info->peer->dhtget = NULL;
2772 p = path_build_from_dht (get_path, get_path_length,
2773 put_path, put_path_length);
2774 path_add_to_peer (path_info->peer, p);
2775 for (i = 0; i < path_info->peer->ntunnels; i++)
2777 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2778 peer_info_connect(path_info->peer, path_info->t);
2780 GNUNET_free (path_info);
2787 * Function to process paths received for a new peer addition. The recorded
2788 * paths form the initial tunnel, which can be optimized later.
2789 * Called on each result obtained for the DHT search.
2791 * @param cls closure
2792 * @param exp when will this value expire
2793 * @param key key of the result
2794 * @param type type of the result
2795 * @param size number of bytes in data
2796 * @param data pointer to the result data
2799 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2800 const GNUNET_HashCode * key,
2801 const struct GNUNET_PeerIdentity *get_path,
2802 unsigned int get_path_length,
2803 const struct GNUNET_PeerIdentity *put_path,
2804 unsigned int put_path_length,
2805 enum GNUNET_BLOCK_Type type, size_t size,
2808 const struct GNUNET_PeerIdentity *pi = data;
2809 struct GNUNET_PeerIdentity id;
2810 struct MeshTunnel *t = cls;
2811 struct MeshPeerInfo *peer_info;
2812 struct MeshPathInfo *path_info;
2813 struct MeshPeerPath *p;
2816 if (size != sizeof (struct GNUNET_PeerIdentity))
2818 GNUNET_break_op (0);
2821 GNUNET_assert (NULL != t->client);
2822 GNUNET_DHT_get_stop (t->dht_get_type);
2823 t->dht_get_type = NULL;
2824 peer_info = peer_info_get (pi);
2825 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2826 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2828 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2829 NULL == peer_info->dhtget)
2831 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2832 path_info->peer = peer_info;
2834 /* we don't have a route to the peer, let's try a direct lookup */
2836 GNUNET_DHT_get_start (dht_handle, /* handle */
2837 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2838 GNUNET_BLOCK_TYPE_TEST, /* block type */
2839 &pi->hashPubKey, /* key to look up */
2840 10U, /* replication level */
2841 GNUNET_DHT_RO_RECORD_ROUTE |
2842 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2843 /* option to dht: record route */
2845 0, /* xquery bits */
2846 dht_get_id_handler, /* callback */
2847 path_info); /* closure */
2851 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2852 path_add_to_peer (peer_info, p);
2853 tunnel_add_peer(t, peer_info);
2854 p = tree_get_path_to_peer(t->tree, peer_info->id);
2856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2857 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2858 t->local_tid, p->length);
2859 for (i = 0; i < p->length; i++)
2861 GNUNET_PEER_resolve (p->peers[0], &id);
2862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2863 GNUNET_h2s_full (&id.hashPubKey));
2869 send_create_path(peer_info, p, t);
2873 send_client_peer_connected(t, myid);
2877 /******************************************************************************/
2878 /********************* MESH LOCAL HANDLES **************************/
2879 /******************************************************************************/
2883 * Handler for client disconnection
2885 * @param cls closure
2886 * @param client identification of the client; NULL
2887 * for the last call when the server is destroyed
2890 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2892 struct MeshClient *c;
2893 struct MeshClient *next;
2895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2901 if (c->handle != client && NULL != client)
2903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2908 if (NULL != c->tunnels)
2910 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2911 &tunnel_destroy_iterator,
2913 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2916 /* deregister clients applications */
2917 if (NULL != c->apps)
2919 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2920 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2922 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2923 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2925 GNUNET_SCHEDULER_cancel (announce_applications_task);
2926 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2928 if (NULL != c->types)
2929 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2930 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2942 * Handler for new clients
2944 * @param cls closure
2945 * @param client identification of the client
2946 * @param message the actual message, which includes messages the client wants
2949 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2950 const struct GNUNET_MessageHeader *message)
2952 struct GNUNET_MESH_ClientConnect *cc_msg;
2953 struct MeshClient *c;
2954 GNUNET_MESH_ApplicationType *a;
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2962 /* Check data sanity */
2963 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2964 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2965 ntypes = ntohs (cc_msg->types);
2966 napps = ntohs (cc_msg->applications);
2968 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2971 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2975 /* Create new client structure */
2976 c = GNUNET_malloc (sizeof (struct MeshClient));
2978 c->id = next_client_id++;
2981 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2984 GNUNET_MESH_ApplicationType at;
2987 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2988 for (i = 0; i < napps; i++)
2991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2992 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2993 /* store in clients hashmap */
2994 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2995 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2996 /* store in global hashmap, for announcements */
2997 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2998 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3000 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3001 announce_applications_task =
3002 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3010 t = (uint16_t *) & a[napps];
3011 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3012 for (i = 0; i < ntypes; i++)
3015 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3017 /* store in clients hashmap */
3018 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3019 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3020 /* store in global hashmap */
3021 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3022 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3026 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3028 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3029 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3030 GNUNET_SERVER_notification_context_add (nc, client);
3032 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3040 * Handler for requests of new tunnels
3042 * @param cls closure
3043 * @param client identification of the client
3044 * @param message the actual message
3047 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3048 const struct GNUNET_MessageHeader *message)
3050 struct GNUNET_MESH_TunnelMessage *t_msg;
3051 struct MeshTunnel *t;
3052 struct MeshClient *c;
3053 GNUNET_HashCode hash;
3055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3057 /* Sanity check for client registration */
3058 if (NULL == (c = client_get (client)))
3061 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3068 /* Message sanity check */
3069 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3072 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3076 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3077 /* Sanity check for tunnel numbering */
3078 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3081 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3084 /* Sanity check for duplicate tunnel IDs */
3085 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3088 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3092 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
3094 while (NULL != tunnel_get_by_pi (myid, next_tid))
3095 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3096 t->id.tid = next_tid++;
3098 t->local_tid = ntohl (t_msg->tunnel_id);
3100 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3102 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3104 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3105 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3108 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3112 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3114 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3115 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3118 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3121 t->tree = tree_new (t, myid);
3122 t->tree->refresh = REFRESH_PATH_TIME;
3123 t->tree->root->status = MESH_PEER_READY;
3124 t->tree->me = t->tree->root;
3126 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3132 * Handler for requests of deleting tunnels
3134 * @param cls closure
3135 * @param client identification of the client
3136 * @param message the actual message
3139 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3140 const struct GNUNET_MessageHeader *message)
3142 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3143 struct MeshClient *c;
3144 struct MeshTunnel *t;
3145 MESH_TunnelNumber tid;
3146 GNUNET_HashCode hash;
3148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
3150 /* Sanity check for client registration */
3151 if (NULL == (c = client_get (client)))
3154 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3157 /* Message sanity check */
3158 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3161 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3167 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3169 /* Retrieve tunnel */
3170 tid = ntohl (tunnel_msg->tunnel_id);
3172 /* Remove from local id hashmap */
3173 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3174 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3175 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
3177 // notify_tunnel_destroy(t);
3179 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3185 * Handler for connection requests to new peers
3187 * @param cls closure
3188 * @param client identification of the client
3189 * @param message the actual message (PeerControl)
3192 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3193 const struct GNUNET_MessageHeader *message)
3195 struct GNUNET_MESH_PeerControl *peer_msg;
3196 struct MeshPeerInfo *peer_info;
3197 struct MeshClient *c;
3198 struct MeshTunnel *t;
3199 MESH_TunnelNumber tid;
3201 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3202 /* Sanity check for client registration */
3203 if (NULL == (c = client_get (client)))
3206 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3210 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3211 /* Sanity check for message size */
3212 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3215 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3219 /* Tunnel exists? */
3220 tid = ntohl (peer_msg->tunnel_id);
3221 t = tunnel_get_by_local_id (c, tid);
3225 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3229 /* Does client own tunnel? */
3230 if (t->client->handle != client)
3233 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3236 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3237 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
3238 peer_info = peer_info_get (&peer_msg->peer);
3240 tunnel_add_peer(t, peer_info);
3241 peer_info_connect(peer_info, t);
3243 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3249 * Handler for disconnection requests of peers in a tunnel
3251 * @param cls closure
3252 * @param client identification of the client
3253 * @param message the actual message (PeerControl)
3256 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3257 const struct GNUNET_MessageHeader *message)
3259 struct GNUNET_MESH_PeerControl *peer_msg;
3260 struct MeshClient *c;
3261 struct MeshTunnel *t;
3262 MESH_TunnelNumber tid;
3264 /* Sanity check for client registration */
3265 if (NULL == (c = client_get (client)))
3268 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3271 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3272 /* Sanity check for message size */
3273 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3276 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3280 /* Tunnel exists? */
3281 tid = ntohl (peer_msg->tunnel_id);
3282 t = tunnel_get_by_local_id (c, tid);
3286 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3290 /* Does client own tunnel? */
3291 if (t->client->handle != client)
3294 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3298 /* Ok, delete peer from tunnel */
3299 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3300 &peer_msg->peer.hashPubKey);
3302 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3308 * Handler for connection requests to new peers by type
3310 * @param cls closure
3311 * @param client identification of the client
3312 * @param message the actual message (ConnectPeerByType)
3315 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3316 const struct GNUNET_MessageHeader *message)
3318 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3319 struct MeshClient *c;
3320 struct MeshTunnel *t;
3321 GNUNET_HashCode hash;
3322 MESH_TunnelNumber tid;
3324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3325 /* Sanity check for client registration */
3326 if (NULL == (c = client_get (client)))
3329 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3333 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3334 /* Sanity check for message size */
3335 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3336 ntohs (connect_msg->header.size))
3339 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3343 /* Tunnel exists? */
3344 tid = ntohl (connect_msg->tunnel_id);
3345 t = tunnel_get_by_local_id (c, tid);
3349 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3353 /* Does client own tunnel? */
3354 if (t->client->handle != client)
3357 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3361 /* Do WE have the service? */
3362 t->type = ntohl (connect_msg->type);
3363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3364 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3365 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3368 /* Yes! Fast forward, add ourselves to the tunnel and send the
3369 * good news to the client
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3372 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3373 peer_info_get (&my_full_id),
3374 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3377 send_client_peer_connected(t, myid);
3378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3379 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3382 /* Ok, lets find a peer offering the service */
3383 if (NULL != t->dht_get_type)
3385 GNUNET_DHT_get_stop (t->dht_get_type);
3387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3388 GNUNET_h2s_full (&hash));
3390 GNUNET_DHT_get_start (dht_handle,
3391 GNUNET_TIME_UNIT_FOREVER_REL,
3392 GNUNET_BLOCK_TYPE_TEST,
3395 GNUNET_DHT_RO_RECORD_ROUTE |
3396 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3398 &dht_get_type_handler, t);
3400 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3406 * Handler for client traffic directed to one peer
3408 * @param cls closure
3409 * @param client identification of the client
3410 * @param message the actual message
3413 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3414 const struct GNUNET_MessageHeader *message)
3416 struct MeshClient *c;
3417 struct MeshTunnel *t;
3418 struct MeshPeerInfo *pi;
3419 struct GNUNET_MESH_Unicast *data_msg;
3420 MESH_TunnelNumber tid;
3423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3424 "MESH: Got a unicast request from a client!\n");
3426 /* Sanity check for client registration */
3427 if (NULL == (c = client_get (client)))
3430 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3433 data_msg = (struct GNUNET_MESH_Unicast *) message;
3434 /* Sanity check for message size */
3435 size = ntohs (message->size);
3436 if (sizeof (struct GNUNET_MESH_Unicast) +
3437 sizeof (struct GNUNET_MessageHeader) > size)
3440 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3444 /* Tunnel exists? */
3445 tid = ntohl (data_msg->tid);
3446 t = tunnel_get_by_local_id (c, tid);
3450 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3454 /* Is it a local tunnel? Then, does client own the tunnel? */
3455 if (NULL != t->client &&
3456 NULL != t->client->handle &&
3457 t->client->handle != client)
3460 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3464 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3465 &data_msg->destination.hashPubKey);
3466 /* Is the selected peer in the tunnel? */
3470 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3474 /* Ok, everything is correct, send the message
3475 * (pretend we got it from a mesh peer)
3478 char buf[ntohs (message->size)];
3479 struct GNUNET_MESH_Unicast *copy;
3481 /* Work around const limitation */
3482 copy = (struct GNUNET_MESH_Unicast *) buf;
3483 memcpy (buf, data_msg, size);
3484 copy->oid = my_full_id;
3485 copy->tid = htonl (t->id.tid);
3486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3487 "MESH: calling generic handler...\n");
3488 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
3490 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3496 * Handler for client traffic directed to the origin
3498 * @param cls closure
3499 * @param client identification of the client
3500 * @param message the actual message
3503 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3504 const struct GNUNET_MessageHeader *message)
3506 struct GNUNET_MESH_ToOrigin *data_msg;
3507 struct GNUNET_PeerIdentity id;
3508 struct MeshClient *c;
3509 struct MeshTunnel *t;
3510 MESH_TunnelNumber tid;
3513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3514 "MESH: Got a ToOrigin request from a client!\n");
3516 /* Sanity check for client registration */
3517 if (NULL == (c = client_get (client)))
3520 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3523 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3524 /* Sanity check for message size */
3525 size = ntohs (message->size);
3526 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3527 sizeof (struct GNUNET_MessageHeader) > size)
3530 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3534 /* Tunnel exists? */
3535 tid = ntohl (data_msg->tid);
3536 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3542 t = tunnel_get_by_local_id (c, tid);
3546 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3550 /* It shouldn't be a local tunnel. */
3551 if (NULL != t->client)
3554 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3557 GNUNET_PEER_resolve(t->id.oid, &id);
3559 /* Ok, everything is correct, send the message
3560 * (pretend we got it from a mesh peer)
3563 char buf[ntohs (message->size)];
3564 struct GNUNET_MESH_ToOrigin *copy;
3566 /* Work around const limitation */
3567 copy = (struct GNUNET_MESH_ToOrigin *) buf;
3568 memcpy (buf, data_msg, size);
3570 copy->tid = htonl (t->id.tid);
3571 copy->sender = my_full_id;
3572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3573 "MESH: calling generic handler...\n");
3574 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL);
3576 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3582 * Handler for client traffic directed to all peers in a tunnel
3584 * @param cls closure
3585 * @param client identification of the client
3586 * @param message the actual message
3589 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3590 const struct GNUNET_MessageHeader *message)
3592 struct MeshClient *c;
3593 struct MeshTunnel *t;
3594 struct GNUNET_MESH_Multicast *data_msg;
3595 MESH_TunnelNumber tid;
3597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3598 "MESH: Got a multicast request from a client!\n");
3600 /* Sanity check for client registration */
3601 if (NULL == (c = client_get (client)))
3604 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3607 data_msg = (struct GNUNET_MESH_Multicast *) message;
3608 /* Sanity check for message size */
3609 if (sizeof (struct GNUNET_MESH_Multicast) +
3610 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
3613 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3617 /* Tunnel exists? */
3618 tid = ntohl (data_msg->tid);
3619 t = tunnel_get_by_local_id (c, tid);
3623 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3627 /* Does client own tunnel? */
3628 if (t->client->handle != client)
3631 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3636 char buf[ntohs(message->size)];
3637 struct GNUNET_MESH_Multicast *copy;
3639 copy = (struct GNUNET_MESH_Multicast *)buf;
3640 memcpy(buf, message, ntohs(message->size));
3641 copy->oid = my_full_id;
3642 copy->tid = htonl(t->id.tid);
3643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3644 "MESH: calling generic handler...\n");
3645 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3648 /* receive done gets called when last copy is sent to a neighbor */
3653 * Functions to handle messages from clients
3655 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3656 {&handle_local_new_client, NULL,
3657 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3658 {&handle_local_tunnel_create, NULL,
3659 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3660 sizeof (struct GNUNET_MESH_TunnelMessage)},
3661 {&handle_local_tunnel_destroy, NULL,
3662 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3663 sizeof (struct GNUNET_MESH_TunnelMessage)},
3664 {&handle_local_connect_add, NULL,
3665 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3666 sizeof (struct GNUNET_MESH_PeerControl)},
3667 {&handle_local_connect_del, NULL,
3668 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3669 sizeof (struct GNUNET_MESH_PeerControl)},
3670 {&handle_local_connect_by_type, NULL,
3671 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3672 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3673 {&handle_local_unicast, NULL,
3674 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3675 {&handle_local_to_origin, NULL,
3676 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3677 {&handle_local_multicast, NULL,
3678 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3684 * To be called on core init/fail.
3686 * @param cls service closure
3687 * @param server handle to the server for this service
3688 * @param identity the public identity of this peer
3691 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3692 const struct GNUNET_PeerIdentity *identity)
3694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3695 core_handle = server;
3696 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3698 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3699 GNUNET_SCHEDULER_shutdown();
3705 * Method called whenever a given peer connects.
3707 * @param cls closure
3708 * @param peer peer identity this notification is about
3709 * @param atsi performance data for the connection
3712 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3713 const struct GNUNET_ATS_Information *atsi)
3715 struct MeshPeerInfo *peer_info;
3716 struct MeshPeerPath *path;
3718 #if MESH_DEBUG_CONNECTION
3719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3721 GNUNET_h2s(&my_full_id.hashPubKey));
3723 peer_info = peer_info_get (peer);
3724 if (myid == peer_info->id)
3726 #if MESH_DEBUG_CONNECTION
3727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3731 #if MESH_DEBUG_CONNECTION
3734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3735 GNUNET_h2s(&peer->hashPubKey));
3738 path = path_new (2);
3739 path->peers[0] = myid;
3740 path->peers[1] = peer_info->id;
3741 GNUNET_PEER_change_rc(myid, 1);
3742 GNUNET_PEER_change_rc(peer_info->id, 1);
3743 path_add_to_peer (peer_info, path);
3748 * Method called whenever a peer disconnects.
3750 * @param cls closure
3751 * @param peer peer identity this notification is about
3754 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3756 struct MeshPeerInfo *pi;
3759 #if MESH_DEBUG_CONNECTION
3760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3762 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3768 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3770 peer_info_cancel_transmission(pi, i);
3772 path_remove_from_peer (pi, pi->id, myid);
3773 #if MESH_DEBUG_CONNECTION
3776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3783 /******************************************************************************/
3784 /************************ MAIN FUNCTIONS ****************************/
3785 /******************************************************************************/
3788 * Iterator over hash map entries.
3790 * @param cls closure
3791 * @param key current key code
3792 * @param value value in the hash map
3793 * @return GNUNET_YES if we should continue to
3798 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
3800 struct MeshTunnel *t = value;
3806 * Task run during shutdown.
3812 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3815 /* TODO: destroy tunnels? */
3816 if (core_handle != NULL)
3818 GNUNET_CORE_disconnect (core_handle);
3821 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
3822 if (dht_handle != NULL)
3824 GNUNET_DHT_disconnect (dht_handle);
3829 GNUNET_SERVER_notification_context_destroy (nc);
3832 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3834 GNUNET_SCHEDULER_cancel (announce_id_task);
3835 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3841 * Process mesh requests.
3843 * @param cls closure
3844 * @param server the initialized server
3845 * @param c configuration to use
3848 run (void *cls, struct GNUNET_SERVER_Handle *server,
3849 const struct GNUNET_CONFIGURATION_Handle *c)
3851 struct MeshPeerInfo *peer;
3852 struct MeshPeerPath *p;
3855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3856 server_handle = server;
3857 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3858 CORE_QUEUE_SIZE, /* queue size */
3859 NULL, /* Closure passed to MESH functions */
3860 &core_init, /* Call core_init once connected */
3861 &core_connect, /* Handle connects */
3862 &core_disconnect, /* remove peers on disconnects */
3863 NULL, /* Don't notify about all incoming messages */
3864 GNUNET_NO, /* For header only in notification */
3865 NULL, /* Don't notify about all outbound messages */
3866 GNUNET_NO, /* For header-only out notification */
3867 core_handlers); /* Register these handlers */
3868 if (core_handle == NULL)
3871 GNUNET_SCHEDULER_shutdown ();
3876 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3879 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3881 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3882 GNUNET_SCHEDULER_shutdown ();
3885 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3886 GNUNET_free (keyfile);
3887 if (my_private_key == NULL)
3889 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3890 _("Mesh service could not access hostkey. Exiting.\n"));
3891 GNUNET_SCHEDULER_shutdown ();
3894 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3895 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3896 &my_full_id.hashPubKey);
3897 myid = GNUNET_PEER_intern (&my_full_id);
3899 dht_handle = GNUNET_DHT_connect (c, 64);
3900 if (dht_handle == NULL)
3906 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3908 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3909 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3910 peers = GNUNET_CONTAINER_multihashmap_create (32);
3911 applications = GNUNET_CONTAINER_multihashmap_create (32);
3912 types = GNUNET_CONTAINER_multihashmap_create (32);
3914 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3915 nc = GNUNET_SERVER_notification_context_create (server_handle,
3917 GNUNET_SERVER_disconnect_notify (server_handle,
3918 &handle_local_client_disconnect,
3923 clients_tail = NULL;
3928 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3929 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3931 /* Create a peer_info for the local peer */
3932 peer = peer_info_get(&my_full_id);
3935 path_add_to_peer(peer, p);
3937 /* Scheduled the task to clean up when shutdown is called */
3938 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3945 * The main function for the mesh service.
3947 * @param argc number of arguments from the command line
3948 * @param argv command line arguments
3949 * @return 0 ok, 1 on error
3952 main (int argc, char *const *argv)
3957 // fprintf (stderr, "main ()\n");
3959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3962 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3966 // fprintf (stderr, "main () END\n");