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);
815 * Function called to notify a client about the socket
816 * being ready to queue more data. "buf" will be
817 * NULL and "size" zero if the socket was closed for
818 * writing in the meantime.
820 * @param cls closure (data itself)
821 * @param size number of bytes available in buf
822 * @param buf where the callee should write the message
824 * @return number of bytes written to buf
827 send_core_data_multicast (void *cls, size_t size, void *buf);
830 * Cancel a core transmission that was already requested and free all resources
831 * associated to the request.
833 * @param peer PeeInfo of the peer whose transmission is cancelled.
834 * @param i Position of the transmission to be cancelled.
837 peer_info_cancel_transmission(struct MeshPeerInfo *peer, unsigned int i)
839 if (NULL != peer->core_transmit[i])
841 struct MeshDataDescriptor *dd;
842 struct MeshPathInfo *path_info;
846 struct GNUNET_PeerIdentity id;
848 GNUNET_PEER_resolve (peer->id, &id);
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
850 "MESH: Cancelling data transmission at %s [%u]\n",
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "MESH: message type %u\n",
858 /* TODO: notify that tranmission has failed */
859 switch (peer->types[i])
861 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
862 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
863 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
866 if (0 == --(*dd->copies))
868 GNUNET_free (dd->copies);
869 GNUNET_free (dd->data);
872 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
874 path_info = peer->infos[i];
875 path_destroy(path_info->path);
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
881 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
882 peer->core_transmit[i] = NULL;
883 GNUNET_free (peer->infos[i]);
892 peer_info_transmit_slot (struct MeshPeerInfo *peer)
896 for (i = 0; peer->core_transmit[i]; i++)
898 if (i == (CORE_QUEUE_SIZE - 1))
900 /* All positions are taken! Overwriting! */
902 peer_info_cancel_transmission(peer, 0);
911 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
912 * and insert it in the appropiate structures if the peer is not known yet.
914 * @param peer Full identity of the peer.
916 * @return Existing or newly created peer info.
918 static struct MeshPeerInfo *
919 peer_info_get (const struct GNUNET_PeerIdentity *peer)
921 struct MeshPeerInfo *peer_info;
923 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
924 if (NULL == peer_info)
927 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
928 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
929 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
930 peer_info->id = GNUNET_PEER_intern (peer);
938 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
939 * and insert it in the appropiate structures if the peer is not known yet.
941 * @param peer Short identity of the peer.
943 * @return Existing or newly created peer info.
945 static struct MeshPeerInfo *
946 peer_info_get_short (const GNUNET_PEER_Id peer)
948 struct GNUNET_PeerIdentity id;
950 GNUNET_PEER_resolve(peer, &id);
951 return peer_info_get(&id);
956 * Iterator to remove the tunnel from the list of tunnels a peer participates
959 * @param cls Closure (tunnel info)
960 * @param key GNUNET_PeerIdentity of the peer (unused)
961 * @param value PeerInfo of the peer
963 * @return always GNUNET_YES, to keep iterating
966 peer_info_delete_tunnel (void* cls, const GNUNET_HashCode* key, void* value)
968 struct MeshTunnel *t = cls;
969 struct MeshPeerInfo *peer = value;
972 for (i = 0; i < peer->ntunnels; i++)
974 if (0 == memcmp(&peer->tunnels[i]->id,
976 sizeof(struct MESH_TunnelID)))
979 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
980 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
989 * Sends a CREATE PATH message for a path to a peer, properly registrating
990 * all used resources.
992 * @param peer PeerInfo of the final peer for whom this path is being created.
993 * @param p Path itself.
994 * @param t Tunnel for which the path is created.
997 send_create_path (struct MeshPeerInfo *peer,
998 struct MeshPeerPath *p,
999 struct MeshTunnel *t)
1001 struct GNUNET_PeerIdentity id;
1002 struct MeshPathInfo *path_info;
1003 struct MeshPeerInfo *neighbor;
1008 p = tree_get_path_to_peer(t->tree, peer->id);
1015 for (i = 0; i < p->length; i++)
1017 if (p->peers[i] == myid)
1020 if (i >= p->length - 1)
1026 GNUNET_PEER_resolve(p->peers[i + 1], &id);
1028 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1029 path_info->path = p;
1031 neighbor = peer_info_get(&id);
1032 path_info->peer = neighbor;
1033 path_info->pos = peer_info_transmit_slot(neighbor);
1034 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1035 neighbor->infos[path_info->pos] = path_info;
1036 neighbor->core_transmit[path_info->pos] =
1037 GNUNET_CORE_notify_transmit_ready (
1038 core_handle, /* handle */
1041 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1043 sizeof (struct GNUNET_MESH_ManipulatePath)
1044 + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1045 &send_core_create_path, /* callback */
1046 path_info); /* cls */
1051 * Try to establish a new connection to this peer.
1052 * Use the best path for the given tunnel.
1053 * If the peer doesn't have any path to it yet, try to get one.
1054 * If the peer already has some path, send a CREATE PATH towards it.
1056 * @param peer PeerInfo of the peer.
1057 * @param t Tunnel for which to create the path, if possible.
1060 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1062 struct MeshPeerPath *p;
1063 struct MeshPathInfo *path_info;
1065 if (NULL != peer->path_head)
1067 p = tree_get_path_to_peer(t->tree, peer->id);
1070 send_create_path(peer, p, t);
1075 send_client_peer_connected(t, myid);
1078 else if (NULL == peer->dhtget)
1080 struct GNUNET_PeerIdentity id;
1082 GNUNET_PEER_resolve(peer->id, &id);
1083 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1084 path_info->peer = peer;
1087 GNUNET_DHT_get_start(dht_handle, /* handle */
1088 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1089 GNUNET_BLOCK_TYPE_TEST, /* type */
1090 &id.hashPubKey, /* key to search */
1091 4, /* replication level */
1092 GNUNET_DHT_RO_RECORD_ROUTE |
1093 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1095 0, /* xquery bits */
1096 &dht_get_id_handler,
1099 /* Otherwise, there is no path but the DHT get is already started. */
1105 * Destroy the peer_info and free any allocated resources linked to it
1106 * @param t tunnel the path belongs to
1107 * @param pi the peer_info to destroy
1108 * @return GNUNET_OK on success
1111 peer_info_destroy (struct MeshPeerInfo *pi)
1113 GNUNET_HashCode hash;
1114 struct GNUNET_PeerIdentity id;
1116 GNUNET_PEER_resolve (pi->id, &id);
1117 GNUNET_PEER_change_rc (pi->id, -1);
1118 GNUNET_CRYPTO_hash (&id, sizeof (struct GNUNET_PeerIdentity), &hash);
1120 GNUNET_CONTAINER_multihashmap_remove (peers, &hash, pi);
1121 GNUNET_SCHEDULER_cancel (pi->path_refresh_task);
1129 * Notify a tunnel that a connection has broken that affects at least
1130 * some of its peers.
1132 * @param t Tunnel affected.
1133 * @param peer Peer that (at least) has been affected by the disconnection.
1134 * @param p1 Peer that got disconnected from p2.
1135 * @param p2 Peer that got disconnected from p1.
1137 * @return Short ID of the peer disconnected (either p1 or p2).
1138 * 0 if the tunnel remained unaffected.
1140 static GNUNET_PEER_Id
1141 tunnel_notify_connection_broken (struct MeshTunnel *t,
1142 struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1146 * Remove all paths that rely on a direct connection between p1 and p2
1147 * from the peer itself and notify all tunnels about it.
1149 * @param peer PeerInfo of affected peer.
1150 * @param p1 GNUNET_PEER_Id of one peer.
1151 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1154 * TODO: optimize (see below)
1157 path_remove_from_peer (struct MeshPeerInfo *peer,
1161 struct GNUNET_PeerIdentity id;
1162 struct MeshPeerPath *p;
1163 struct MeshPeerPath *aux;
1164 struct MeshPeerInfo *peer_d;
1166 unsigned int destroyed;
1172 p = peer->path_head;
1176 for (i = 0; i < (p->length - 1); i++)
1178 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1179 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1191 for (i = 0; i < peer->ntunnels; i++)
1193 d = tunnel_notify_connection_broken (peer->tunnels[i], peer, p1, p2);
1195 * Problem: one or more peers have been deleted from the tunnel tree.
1196 * We don't know who they are to try to add them again.
1197 * We need to try to find a new path for each of the disconnected peers.
1198 * Some of them might already have a path to reach them that does not
1199 * involve p1 and p2. Adding all anew might render in a better tree than
1200 * the trivial immediate fix.
1202 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1203 * its children will be reachable trough him.
1205 peer_d = peer_info_get_short(d);
1208 for (p = peer_d->path_head; NULL != p; p = p->next)
1210 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
1218 /* No callback, as peer will be already disconnected */
1219 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
1223 struct MeshPathInfo *path_info;
1225 if (NULL != peer_d->dhtget)
1227 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1228 path_info->path = p;
1229 path_info->peer = peer_d;
1230 path_info->t = peer->tunnels[i];
1232 GNUNET_DHT_get_start(dht_handle, /* handle */
1233 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1234 GNUNET_BLOCK_TYPE_TEST, /* type */
1235 &id.hashPubKey, /*key to search */
1236 4, /* replication level */
1237 GNUNET_DHT_RO_RECORD_ROUTE |
1238 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1240 0, /* xquery bits */
1241 &dht_get_id_handler,
1242 (void *) path_info);
1249 * Add the path to the peer and update the path used to reach it in case this
1252 * @param peer_info Destination peer to add the path to.
1253 * @param path New path to add. Last peer must be the peer in arg 1.
1254 * Path will be either used of freed if already known.
1256 * TODO: trim the part from origin to us? Add it as path to origin?
1259 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1261 struct MeshPeerPath *aux;
1265 if (NULL == peer_info || NULL == path)
1271 l = path_get_length (path);
1278 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1280 l2 = path_get_length (aux);
1283 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1284 peer_info->path_tail, aux, path);
1288 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1295 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1302 * Add the path to the origin peer and update the path used to reach it in case
1303 * this is the shortest.
1304 * The path is given in peer_info -> destination, therefore we turn the path
1305 * upside down first.
1307 * @param peer_info Peer to add the path to, being the origin of the path.
1308 * @param path New path to add after being inversed.
1311 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1314 path_add_to_peer (peer_info, path);
1319 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1320 * to obtain a local peer -> destination path and interning the peer ids.
1322 * @return Newly allocated and created path
1324 static struct MeshPeerPath *
1325 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1326 unsigned int get_path_length,
1327 const struct GNUNET_PeerIdentity *put_path,
1328 unsigned int put_path_length)
1330 struct MeshPeerPath *p;
1336 GNUNET_PEER_change_rc(myid, 1);
1337 i = get_path_length;
1338 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1339 for (i--; i >= 0; i--)
1341 id = GNUNET_PEER_intern (&get_path[i]);
1342 if (p->length > 0 && id == p->peers[p->length - 1])
1344 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1345 GNUNET_PEER_change_rc(id, -1);
1349 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1350 "MESH: Adding from GET: %s.\n",
1351 GNUNET_i2s(&get_path[i]));
1353 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1354 p->peers[p->length - 1] = id;
1357 i = put_path_length;
1358 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1359 for (i--; i >= 0; i--)
1361 id = GNUNET_PEER_intern (&put_path[i]);
1364 /* PUT path went through us, so discard the path up until now and start
1365 * from here to get a much shorter (and loop-free) path.
1370 if (p->length > 0 && id == p->peers[p->length - 1])
1372 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1373 GNUNET_PEER_change_rc(id, -1);
1377 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1378 "MESH: Adding from PUT: %s.\n",
1379 GNUNET_i2s(&put_path[i]));
1381 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1382 p->peers[p->length - 1] = id;
1386 if (get_path_length > 0)
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "MESH: (first of GET: %s)\n",
1389 GNUNET_h2s_full(&get_path[0].hashPubKey));
1390 if (put_path_length > 0)
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 "MESH: (first of PUT: %s)\n",
1393 GNUNET_h2s_full(&put_path[0].hashPubKey));
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "MESH: In total: %d hops\n",
1397 for (i = 0; i < p->length; i++)
1399 struct GNUNET_PeerIdentity peer_id;
1401 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 GNUNET_h2s_full(&peer_id.hashPubKey));
1413 * Send keepalive packets for a peer
1415 * @param cls Closure (tunnel for which to send the keepalive).
1416 * @param tc Notification context.
1418 * TODO: implement explicit multicast keepalive?
1421 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1425 * Search for a tunnel among the tunnels for a client
1427 * @param c the client whose tunnels to search in
1428 * @param tid the local id of the tunnel
1430 * @return tunnel handler, NULL if doesn't exist
1432 static struct MeshTunnel *
1433 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1435 GNUNET_HashCode hash;
1437 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1438 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1440 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1442 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1447 * Search for a tunnel by global ID using PEER_ID
1449 * @param pi owner of the tunnel
1450 * @param tid global tunnel number
1452 * @return tunnel handler, NULL if doesn't exist
1454 static struct MeshTunnel *
1455 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1457 struct MESH_TunnelID id;
1458 GNUNET_HashCode hash;
1463 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1464 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1469 * Search for a tunnel by global ID using full PeerIdentities
1471 * @param oid owner of the tunnel
1472 * @param tid global tunnel number
1474 * @return tunnel handler, NULL if doesn't exist
1476 static struct MeshTunnel *
1477 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1479 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1484 * Callback used to notify a client owner of a tunnel that a peer has
1485 * disconnected, most likely because of a path change.
1487 * @param n Node in the tree representing the disconnected peer
1490 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1492 struct MeshPeerInfo *peer;
1494 if (NULL != n->t->client && NULL != nc)
1496 struct GNUNET_MESH_PeerControl msg;
1497 msg.header.size = htons (sizeof (msg));
1498 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1499 msg.tunnel_id = htonl (n->t->local_tid);
1500 GNUNET_PEER_resolve (n->peer, &msg.peer);
1501 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1502 &msg.header, GNUNET_NO);
1504 peer = peer_info_get_short(n->peer);
1505 peer_info_connect(peer, n->t);
1510 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1511 * needed rescources.
1512 * If peer already exists, reevaluate shortest path and change if different.
1514 * @param t Tunnel we want to add a new peer to
1515 * @param peer PeerInfo of the peer being added
1519 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1521 struct GNUNET_PeerIdentity id;
1522 struct MeshPeerPath *best_p;
1523 struct MeshPeerPath *p;
1524 unsigned int best_cost;
1527 GNUNET_PEER_resolve(peer->id, &id);
1529 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1532 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1533 GNUNET_CONTAINER_multihashmap_put(
1537 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1540 if (NULL != (p = peer->path_head))
1543 best_cost = path_get_cost(t->tree, p);
1546 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1553 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1554 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1555 t->path_refresh_task =
1556 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1560 /* Start a DHT get if necessary */
1561 peer_info_connect(peer, t);
1566 * Add a path to a tunnel which we don't own, just to remember the next hop.
1567 * If destination node was already in the tunnel, the first hop information
1568 * will be replaced with the new path.
1570 * @param t Tunnel we want to add a new peer to
1571 * @param p Path to add
1572 * @param own_pos Position of local node in path.
1576 tunnel_add_path (struct MeshTunnel *t,
1577 struct MeshPeerPath *p,
1578 unsigned int own_pos)
1580 struct GNUNET_PeerIdentity id;
1582 GNUNET_assert (0 != own_pos);
1583 tree_add_path(t->tree, p, NULL);
1584 if (NULL == t->tree->me)
1585 t->tree->me = tree_find_peer(t->tree->root, p->peers[own_pos]);
1586 if (own_pos < p->length - 1)
1588 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1589 tree_update_first_hops(t->tree, t->tree->me, &id);
1595 * Notify a tunnel that a connection has broken that affects at least
1596 * some of its peers.
1598 * @param t Tunnel affected.
1599 * @param peer Peer that (at least) has been affected by the disconnection.
1600 * @param p1 Peer that got disconnected from p2.
1601 * @param p2 Peer that got disconnected from p1.
1603 * @return Short ID of the peer disconnected (either p1 or p2).
1604 * 0 if the tunnel remained unaffected.
1606 static GNUNET_PEER_Id
1607 tunnel_notify_connection_broken (struct MeshTunnel *t,
1608 struct MeshPeerInfo *peer,
1612 return tree_notify_connection_broken (t->tree, p1, p2,
1613 ¬ify_peer_disconnected);
1618 * Send a message in a tunnel in multicast, sending a copy to each child node
1619 * down the local one in the tunnel tree.
1621 * @param t Tunnel in which to send the data.
1622 * @param msg Message to be sent
1624 * @return Number of copies sent.
1627 tunnel_send_multicast (struct MeshTunnel *t,
1628 const struct GNUNET_MessageHeader *msg)
1630 struct GNUNET_PeerIdentity *neighbor;
1631 struct MeshDataDescriptor *info;
1632 struct MeshTunnelTreeNode *n;
1633 unsigned int *copies;
1638 size = ntohs (msg->size);
1639 GNUNET_assert (NULL != t->tree->me);
1640 n = t->tree->me->children_head;
1643 copies = GNUNET_malloc (sizeof (unsigned int));
1644 for (*copies = 0; NULL != n; n = n->next)
1646 n = t->tree->me->children_head;
1647 data = GNUNET_malloc (size);
1648 memcpy (data, &msg, size);
1651 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1652 info->origin = &t->id;
1655 info->copies = copies;
1656 if (NULL != t->client->handle)
1658 info->client = t->client->handle;
1660 info->timeout_task = GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT,
1664 info->destination = n->peer;
1665 neighbor = path_get_first_hop(t->tree, n->peer);
1666 info->peer = peer_info_get(neighbor);
1667 GNUNET_assert (NULL != info->peer);
1668 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
1670 if (i == (CORE_QUEUE_SIZE - 1))
1677 info->handler_n = i;
1678 info->peer->infos[i] = info;
1679 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1680 info->peer->core_transmit[i] =
1681 GNUNET_CORE_notify_transmit_ready (core_handle,
1684 GNUNET_TIME_UNIT_FOREVER_REL,
1687 &send_core_data_multicast, info);
1694 * Destroy the tunnel and free any allocated resources linked to it
1696 * @param t the tunnel to destroy
1698 * @return GNUNET_OK on success
1701 tunnel_destroy (struct MeshTunnel *t)
1703 struct MeshClient *c;
1704 struct MeshQueue *q;
1705 struct MeshQueue *qn;
1706 GNUNET_HashCode hash;
1715 struct GNUNET_PeerIdentity id;
1717 GNUNET_PEER_resolve(t->id.oid, &id);
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1719 "MESH: destroying tunnel %s [%x]\n",
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1727 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1728 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1733 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1735 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1739 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1741 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1742 GNUNET_break (GNUNET_YES ==
1743 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t));
1746 if (NULL != t->peers)
1748 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
1749 &peer_info_delete_tunnel, t);
1750 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1755 if (NULL != q->data)
1756 GNUNET_free (q->data);
1760 /* TODO cancel core transmit ready in case it was active */
1762 tree_destroy(t->tree);
1763 if (NULL != t->dht_get_type)
1764 GNUNET_DHT_get_stop(t->dht_get_type);
1771 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1772 * client when the client disconnects.
1774 * @param cls closure (client that is disconnecting)
1775 * @param key the hash of the local tunnel id (used to access the hashmap)
1776 * @param value the value stored at the key (tunnel to destroy)
1778 * @return GNUNET_OK on success
1781 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1783 struct MeshTunnel *t = value;
1786 if (NULL != t->dht_get_type)
1788 GNUNET_DHT_get_stop (t->dht_get_type);
1790 r = tunnel_destroy (t);
1795 /******************************************************************************/
1796 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1797 /******************************************************************************/
1800 * Function called to notify a client about the socket
1801 * being ready to queue more data. "buf" will be
1802 * NULL and "size" zero if the socket was closed for
1803 * writing in the meantime.
1805 * @param cls closure
1806 * @param size number of bytes available in buf
1807 * @param buf where the callee should write the message
1808 * @return number of bytes written to buf
1811 send_core_create_path (void *cls, size_t size, void *buf)
1813 struct MeshPathInfo *info = cls;
1814 struct GNUNET_MESH_ManipulatePath *msg;
1815 struct GNUNET_PeerIdentity *peer_ptr;
1816 struct MeshPeerInfo *peer = info->peer;
1817 struct MeshTunnel *t = info->t;
1818 struct MeshPeerPath *p = info->path;
1822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1823 "MESH: CREATE PATH sending...\n");
1825 sizeof (struct GNUNET_MESH_ManipulatePath) +
1826 p->length * sizeof (struct GNUNET_PeerIdentity);
1828 if (size < size_needed || NULL == buf)
1830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
1831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
1834 info->peer->core_transmit[info->pos] =
1835 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1836 GNUNET_TIME_UNIT_FOREVER_REL,
1837 path_get_first_hop (t->tree, peer->id),
1839 &send_core_create_path,
1843 info->peer->core_transmit[info->pos] = NULL;
1846 struct GNUNET_PeerIdentity id;
1848 GNUNET_PEER_resolve (peer->id, &id);
1849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1850 "MESH: setting core_transmit %s [%u] to NULL\n",
1855 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1856 msg->header.size = htons (size_needed);
1857 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1858 msg->tid = ntohl (t->id.tid);
1860 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1861 for (i = 0; i < p->length; i++)
1863 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1870 "MESH: CREATE PATH (%u bytes long) sent!\n",
1878 * Function called to notify a client about the socket
1879 * being ready to queue more data. "buf" will be
1880 * NULL and "size" zero if the socket was closed for
1881 * writing in the meantime.
1883 * @param cls closure (MeshDataDescriptor with all info to build packet)
1884 * @param size number of bytes available in buf
1885 * @param buf where the callee should write the message
1886 * @return number of bytes written to buf
1889 send_core_data_to_origin (void *cls, size_t size, void *buf)
1891 struct MeshDataDescriptor *info = cls;
1892 struct GNUNET_MESH_ToOrigin *msg = buf;
1895 GNUNET_assert (NULL != info);
1896 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1897 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1899 if (total_size > size)
1901 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1902 "not enough buffer to send data to origin\n");
1905 msg->header.size = htons (total_size);
1906 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1907 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1908 msg->tid = htonl (info->origin->tid);
1909 if (0 != info->size)
1911 memcpy (&msg[1], &info[1], info->size);
1913 if (NULL != info->client)
1915 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1924 * Function called to notify a client about the socket
1925 * being ready to queue more data. "buf" will be
1926 * NULL and "size" zero if the socket was closed for
1927 * writing in the meantime.
1929 * @param cls closure (data itself)
1930 * @param size number of bytes available in buf
1931 * @param buf where the callee should write the message
1933 * @return number of bytes written to buf
1936 send_core_data_multicast (void *cls, size_t size, void *buf)
1938 struct MeshDataDescriptor *info = cls;
1939 struct GNUNET_MessageHeader *msg = buf;
1942 GNUNET_assert (NULL != info);
1943 GNUNET_assert (NULL != info->peer);
1944 total_size = info->size;
1945 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1947 if (total_size > size)
1950 struct GNUNET_PeerIdentity id;
1952 GNUNET_PEER_resolve(info->peer->id, &id);
1953 info->peer->core_transmit[info->handler_n] =
1954 GNUNET_CORE_notify_transmit_ready (core_handle,
1957 GNUNET_TIME_UNIT_FOREVER_REL,
1960 &send_core_data_multicast,
1964 info->peer->core_transmit[info->handler_n] = NULL;
1965 info->peer->infos[info->handler_n] = NULL;
1966 memcpy (&msg, info->data, total_size);
1967 if (0 == --(*info->copies))
1969 if (NULL != info->client)
1971 if (GNUNET_SCHEDULER_NO_TASK != info->timeout_task)
1972 GNUNET_SCHEDULER_cancel(info->timeout_task);
1973 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1975 GNUNET_free (info->data);
1976 GNUNET_free (info->copies);
1984 * Function called to notify a client about the socket
1985 * being ready to queue more data. "buf" will be
1986 * NULL and "size" zero if the socket was closed for
1987 * writing in the meantime.
1989 * @param cls closure (MeshDataDescriptor)
1990 * @param size number of bytes available in buf
1991 * @param buf where the callee should write the message
1992 * @return number of bytes written to buf
1995 send_core_path_ack (void *cls, size_t size, void *buf)
1997 struct MeshDataDescriptor *info = cls;
1998 struct GNUNET_MESH_PathACK *msg = buf;
2000 GNUNET_assert (NULL != info);
2003 info->peer->core_transmit[info->handler_n] = NULL;
2005 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2010 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2011 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2012 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2013 msg->tid = htonl (info->origin->tid);
2014 msg->peer_id = my_full_id;
2016 /* TODO add signature */
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2019 return sizeof (struct GNUNET_MESH_PathACK);
2024 * Function called to notify a client about the socket
2025 * being ready to queue more data. "buf" will be
2026 * NULL and "size" zero if the socket was closed for
2027 * writing in the meantime.
2029 * @param cls closure (data itself)
2030 * @param size number of bytes available in buf
2031 * @param buf where the callee should write the message
2032 * @return number of bytes written to buf
2035 send_core_data_raw (void *cls, size_t size, void *buf)
2037 struct GNUNET_MessageHeader *msg = cls;
2040 GNUNET_assert (NULL != msg);
2041 total_size = ntohs (msg->size);
2043 if (total_size > size)
2048 memcpy (buf, msg, total_size);
2056 * Send another peer a notification to destroy a tunnel
2057 * @param cls The tunnel to destroy
2058 * @param size Size in the buffer
2059 * @param buf Memory where to put the data to transmit
2060 * @return Size of data put in buffer
2063 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
2065 struct MeshTunnel *t = cls;
2066 struct MeshClient *c;
2067 struct GNUNET_MESH_TunnelMessage *msg;
2071 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2072 /*FIXME*/ msg->header.size =
2073 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
2074 msg->tunnel_id = htonl (t->id.tid);
2076 tunnel_destroy (c, t);
2077 return sizeof (struct GNUNET_MESH_TunnelMessage);
2082 /******************************************************************************/
2083 /******************** MESH NETWORK HANDLERS **************************/
2084 /******************************************************************************/
2088 * Core handler for path creation
2089 * struct GNUNET_CORE_MessageHandler
2091 * @param cls closure
2092 * @param message message
2093 * @param peer peer identity this notification is about
2094 * @param atsi performance data
2095 * @return GNUNET_OK to keep the connection open,
2096 * GNUNET_SYSERR to close it (signal serious error)
2100 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2101 const struct GNUNET_MessageHeader *message,
2102 const struct GNUNET_ATS_Information *atsi)
2104 unsigned int own_pos;
2107 MESH_TunnelNumber tid;
2108 struct GNUNET_MESH_ManipulatePath *msg;
2109 struct GNUNET_PeerIdentity *pi;
2110 GNUNET_HashCode hash;
2111 struct MeshPeerPath *path;
2112 struct MeshPeerInfo *dest_peer_info;
2113 struct MeshPeerInfo *orig_peer_info;
2114 struct MeshTunnel *t;
2116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2117 "MESH: Received a path create msg [%s]\n",
2118 GNUNET_i2s(&my_full_id));
2119 size = ntohs (message->size);
2120 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2122 GNUNET_break_op (0);
2126 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2127 if (size % sizeof (struct GNUNET_PeerIdentity))
2129 GNUNET_break_op (0);
2132 size /= sizeof (struct GNUNET_PeerIdentity);
2135 GNUNET_break_op (0);
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2139 "MESH: path has %u hops.\n",
2141 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2143 tid = ntohl (msg->tid);
2144 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2145 t = tunnel_get (pi, tid);
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2147 "MESH: path is for tunnel %s [%X].\n",
2152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2153 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2154 t->id.oid = GNUNET_PEER_intern (pi);
2156 t->local_tid = next_local_tid++;
2157 /* FIXME test if taken */
2158 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2159 t->tree = tree_new(t, t->id.oid);
2161 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2163 GNUNET_CONTAINER_multihashmap_put (
2167 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2173 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2175 GNUNET_CONTAINER_multihashmap_put (
2179 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2187 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2188 if (NULL == dest_peer_info)
2190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2191 "MESH: Creating PeerInfo for destination.\n");
2192 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2193 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2194 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2196 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2198 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2199 if (NULL == orig_peer_info)
2201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2202 "MESH: Creating PeerInfo for origin.\n");
2203 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2204 orig_peer_info->id = GNUNET_PEER_intern (pi);
2205 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2206 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2209 path = path_new (size);
2211 for (i = 0; i < size; i++)
2213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2214 "MESH: ... adding %s\n",
2215 GNUNET_i2s(&pi[i]));
2216 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2217 if (path->peers[i] == myid)
2220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2221 "MESH: Own position: %u\n", own_pos);
2224 /* cannot be self, must be 'not found' */
2225 /* create path: self not found in path through self */
2226 GNUNET_break_op (0);
2227 path_destroy (path);
2228 /* FIXME error. destroy tunnel? leave for timeout? */
2231 tunnel_add_path (t, path, own_pos);
2232 t->tree->me = tree_find_peer(t->tree->root, myid);
2233 if (own_pos == size - 1)
2235 /* It is for us! Send ack. */
2236 struct GNUNET_MESH_TunnelNotification cmsg;
2237 struct MeshDataDescriptor *info;
2240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2241 "MESH: It's for us!\n");
2242 path_add_to_origin (orig_peer_info, path);
2243 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2244 info->origin = &t->id;
2245 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2246 GNUNET_assert (NULL != info->peer);
2247 j = peer_info_transmit_slot(info->peer);
2248 info->handler_n = j;
2249 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2250 info->peer->infos[j] = info;
2251 info->peer->core_transmit[j] =
2252 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2253 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2254 sizeof (struct GNUNET_MESH_PathACK),
2255 &send_core_path_ack, info);
2256 cmsg.header.size = htons(sizeof(cmsg));
2257 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2258 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2259 cmsg.tunnel_id = htonl(t->local_tid);
2260 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2264 struct MeshPeerPath *path2;
2266 /* It's for somebody else! Retransmit. */
2267 path2 = path_duplicate(path);
2268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2269 "MESH: Retransmitting.\n");
2270 path_add_to_peer(dest_peer_info, path);
2271 path = path_duplicate(path2);
2272 path_add_to_origin(orig_peer_info, path2);
2273 send_create_path(dest_peer_info, path, t);
2280 * Core handler for mesh network traffic going from the origin to a peer
2282 * @param cls closure
2283 * @param peer peer identity this notification is about
2284 * @param message message
2285 * @param atsi performance data
2286 * @return GNUNET_OK to keep the connection open,
2287 * GNUNET_SYSERR to close it (signal serious error)
2290 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2291 const struct GNUNET_MessageHeader *message,
2292 const struct GNUNET_ATS_Information *atsi)
2294 struct GNUNET_MESH_Unicast *msg;
2295 struct MeshTunnel *t;
2299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2300 "MESH: got a unicast packet from %s\n",
2302 size = ntohs (message->size);
2304 sizeof (struct GNUNET_MESH_Unicast) +
2305 sizeof (struct GNUNET_MessageHeader))
2310 msg = (struct GNUNET_MESH_Unicast *) message;
2311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2312 "MESH: of type %u\n",
2313 ntohs (msg[1].header.type));
2314 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2317 /* TODO notify back: we don't know this tunnel */
2318 GNUNET_break_op (0);
2321 pid = GNUNET_PEER_search(&msg->destination);
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 "MESH: it's for us! sending to clients...\n");
2326 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2330 "MESH: not for us, retransmitting...\n");
2331 msg = GNUNET_malloc (size);
2332 memcpy (msg, message, size);
2333 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2334 GNUNET_TIME_UNIT_FOREVER_REL,
2335 path_get_first_hop (t->tree, pid),
2337 &send_core_data_raw, msg);
2343 * Core handler for mesh network traffic going from the origin to all peers
2345 * @param cls closure
2346 * @param message message
2347 * @param peer peer identity this notification is about
2348 * @param atsi performance data
2349 * @return GNUNET_OK to keep the connection open,
2350 * GNUNET_SYSERR to close it (signal serious error)
2352 * TODO: Check who we got this from, to validate route.
2355 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2356 const struct GNUNET_MessageHeader *message,
2357 const struct GNUNET_ATS_Information *atsi)
2359 struct GNUNET_MESH_Multicast *msg;
2360 struct MeshTunnel *t;
2363 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
2364 if (size < sizeof (struct GNUNET_MessageHeader))
2366 GNUNET_break_op (0);
2369 msg = (struct GNUNET_MESH_Multicast *) message;
2370 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2374 /* TODO notify that we dont know that tunnel */
2375 GNUNET_break_op (0);
2379 /* Transmit to locally interested clients */
2380 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2382 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2384 tunnel_send_multicast(t, message);
2391 * Core handler for mesh network traffic toward the owner of a tunnel
2393 * @param cls closure
2394 * @param message message
2395 * @param peer peer identity this notification is about
2396 * @param atsi performance data
2398 * @return GNUNET_OK to keep the connection open,
2399 * GNUNET_SYSERR to close it (signal serious error)
2402 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2403 const struct GNUNET_MessageHeader *message,
2404 const struct GNUNET_ATS_Information *atsi)
2406 struct GNUNET_MESH_ToOrigin *msg;
2407 struct GNUNET_PeerIdentity id;
2408 struct MeshPeerInfo *peer_info;
2409 struct MeshTunnel *t;
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2413 "MESH: got a ToOrigin packet from %s\n",
2415 size = ntohs (message->size);
2416 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2417 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2419 GNUNET_break_op (0);
2422 msg = (struct GNUNET_MESH_ToOrigin *) message;
2423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2424 "MESH: of type %u\n",
2425 ntohs (msg[1].header.type));
2426 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2430 /* TODO notify that we dont know this tunnel (whom)? */
2431 GNUNET_break_op (0);
2435 if (t->id.oid == myid)
2438 struct GNUNET_MESH_ToOrigin *copy;
2440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2441 "MESH: it's for us! sending to clients...\n");
2442 if (NULL == t->client)
2444 /* got data packet for ownerless tunnel */
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 "MESH: no clients!\n");
2447 GNUNET_break_op (0);
2450 /* TODO signature verification */
2451 memcpy (cbuf, message, size);
2452 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2453 copy->tid = htonl (t->local_tid);
2454 GNUNET_SERVER_notification_context_unicast (nc,
2460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2461 "MESH: not for us, retransmitting...\n");
2463 peer_info = peer_info_get (&msg->oid);
2464 if (NULL == peer_info)
2466 /* unknown origin of tunnel */
2470 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
2471 msg = GNUNET_malloc (size);
2472 memcpy (msg, message, size);
2473 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2474 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
2475 &send_core_data_raw, msg);
2482 * Core handler for path ACKs
2484 * @param cls closure
2485 * @param message message
2486 * @param peer peer identity this notification is about
2487 * @param atsi performance data
2489 * @return GNUNET_OK to keep the connection open,
2490 * GNUNET_SYSERR to close it (signal serious error)
2493 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2494 const struct GNUNET_MessageHeader *message,
2495 const struct GNUNET_ATS_Information *atsi)
2497 struct GNUNET_MESH_PathACK *msg;
2498 struct GNUNET_PeerIdentity id;
2499 struct MeshTunnelTreeNode *n;
2500 struct MeshPeerInfo *peer_info;
2501 struct MeshTunnel *t;
2503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2504 "MESH: Received a path ACK msg [%s]\n",
2505 GNUNET_i2s(&my_full_id));
2506 msg = (struct GNUNET_MESH_PathACK *) message;
2507 t = tunnel_get (&msg->oid, msg->tid);
2510 /* TODO notify that we don't know the tunnel */
2514 /* Message for us? */
2515 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2518 "MESH: It's for us!\n");
2519 if (NULL == t->client)
2521 GNUNET_break_op (0);
2524 peer_info = peer_info_get (&msg->peer_id);
2525 if (NULL == peer_info)
2527 GNUNET_break_op (0);
2530 n = tree_find_peer(t->tree->root, peer_info->id);
2533 GNUNET_break_op (0);
2536 n->status = MESH_PEER_READY;
2537 send_client_peer_connected(t, peer_info->id);
2541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2542 "MESH: not for us, retransmitting...\n");
2543 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
2544 peer_info = peer_info_get (&msg->oid);
2545 if (NULL == peer_info)
2547 /* If we know the tunnel, we should DEFINITELY know the peer */
2551 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2552 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2553 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2554 GNUNET_TIME_UNIT_FOREVER_REL,
2556 sizeof (struct GNUNET_MESH_PathACK),
2557 &send_core_data_raw, msg);
2563 * Functions to handle messages from core
2565 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2566 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2567 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2568 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2569 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2570 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2571 sizeof (struct GNUNET_MESH_PathACK)},
2577 /******************************************************************************/
2578 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2579 /******************************************************************************/
2582 * deregister_app: iterator for removing each application registered by a client
2584 * @param cls closure
2585 * @param key the hash of the application id (used to access the hashmap)
2586 * @param value the value stored at the key (client)
2588 * @return GNUNET_OK on success
2591 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2593 GNUNET_break (GNUNET_YES ==
2594 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
2600 * notify_client_connection_failure: notify a client that the connection to the
2601 * requested remote peer is not possible (for instance, no route found)
2602 * Function called when the socket is ready to queue more data. "buf" will be
2603 * NULL and "size" zero if the socket was closed for writing in the meantime.
2605 * @param cls closure
2606 * @param size number of bytes available in buf
2607 * @param buf where the callee should write the message
2608 * @return number of bytes written to buf
2611 notify_client_connection_failure (void *cls, size_t size, void *buf)
2614 struct MeshPeerInfo *peer_info;
2615 struct GNUNET_MESH_PeerControl *msg;
2616 struct GNUNET_PeerIdentity id;
2618 if (0 == size && NULL == buf)
2620 // TODO retry? cancel?
2624 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2625 peer_info = (struct MeshPeerInfo *) cls;
2626 msg = (struct GNUNET_MESH_PeerControl *) buf;
2627 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2628 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2629 // msg->tunnel_id = htonl(peer_info->t->tid);
2630 GNUNET_PEER_resolve (peer_info->id, &id);
2631 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2639 * Send keepalive packets for a peer
2641 * @param cls Closure (tunnel for which to send the keepalive).
2642 * @param tc Notification context.
2644 * TODO: implement explicit multicast keepalive?
2647 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2649 struct MeshTunnel *t = cls;
2650 struct GNUNET_MessageHeader *payload;
2651 struct GNUNET_MESH_Multicast *msg;
2654 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2658 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2661 "MESH: sending keepalive for tunnel %d\n",
2664 size = sizeof(struct GNUNET_MESH_Multicast) +
2665 sizeof(struct GNUNET_MessageHeader);
2666 msg = GNUNET_malloc (size);
2667 msg->header.size = htons (size);
2668 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2669 msg->oid = my_full_id;
2670 msg->tid = htonl(t->id.tid);
2671 payload = (struct GNUNET_MessageHeader *) &msg[1];
2672 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2673 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2674 handle_mesh_data_multicast (NULL, &my_full_id, &msg->header, NULL);
2677 t->path_refresh_task =
2678 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2684 * Function to process paths received for a new peer addition. The recorded
2685 * paths form the initial tunnel, which can be optimized later.
2686 * Called on each result obtained for the DHT search.
2688 * @param cls closure
2689 * @param exp when will this value expire
2690 * @param key key of the result
2691 * @param type type of the result
2692 * @param size number of bytes in data
2693 * @param data pointer to the result data
2695 * TODO: re-issue the request after certain time? cancel after X results?
2698 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2699 const GNUNET_HashCode * key,
2700 const struct GNUNET_PeerIdentity *get_path,
2701 unsigned int get_path_length,
2702 const struct GNUNET_PeerIdentity *put_path,
2703 unsigned int put_path_length,
2704 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2706 struct MeshPathInfo *path_info = cls;
2707 struct MeshPeerPath *p;
2708 struct GNUNET_PeerIdentity pi;
2711 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2712 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2713 "MESH: Got results from DHT for %s\n",
2714 GNUNET_h2s_full(&pi.hashPubKey));
2715 GNUNET_DHT_get_stop(path_info->peer->dhtget);
2716 path_info->peer->dhtget = NULL;
2718 p = path_build_from_dht (get_path, get_path_length,
2719 put_path, put_path_length);
2720 path_add_to_peer (path_info->peer, p);
2721 for (i = 0; i < path_info->peer->ntunnels; i++)
2723 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2724 peer_info_connect(path_info->peer, path_info->t);
2726 GNUNET_free (path_info);
2733 * Function to process paths received for a new peer addition. The recorded
2734 * paths form the initial tunnel, which can be optimized later.
2735 * Called on each result obtained for the DHT search.
2737 * @param cls closure
2738 * @param exp when will this value expire
2739 * @param key key of the result
2740 * @param type type of the result
2741 * @param size number of bytes in data
2742 * @param data pointer to the result data
2745 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2746 const GNUNET_HashCode * key,
2747 const struct GNUNET_PeerIdentity *get_path,
2748 unsigned int get_path_length,
2749 const struct GNUNET_PeerIdentity *put_path,
2750 unsigned int put_path_length,
2751 enum GNUNET_BLOCK_Type type, size_t size,
2754 const struct GNUNET_PeerIdentity *pi = data;
2755 struct GNUNET_PeerIdentity id;
2756 struct MeshTunnel *t = cls;
2757 struct MeshPeerInfo *peer_info;
2758 struct MeshPathInfo *path_info;
2759 struct MeshPeerPath *p;
2762 if (size != sizeof (struct GNUNET_PeerIdentity))
2764 GNUNET_break_op (0);
2767 GNUNET_assert (NULL != t->client);
2768 GNUNET_DHT_get_stop (t->dht_get_type);
2769 t->dht_get_type = NULL;
2770 peer_info = peer_info_get (pi);
2771 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2772 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2774 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2775 NULL == peer_info->dhtget)
2777 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2778 path_info->peer = peer_info;
2780 /* we don't have a route to the peer, let's try a direct lookup */
2782 GNUNET_DHT_get_start (dht_handle, /* handle */
2783 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2784 GNUNET_BLOCK_TYPE_TEST, /* block type */
2785 &pi->hashPubKey, /* key to look up */
2786 10U, /* replication level */
2787 GNUNET_DHT_RO_RECORD_ROUTE |
2788 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2789 /* option to dht: record route */
2791 0, /* xquery bits */
2792 dht_get_id_handler, /* callback */
2793 path_info); /* closure */
2797 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2798 path_add_to_peer (peer_info, p);
2799 tunnel_add_peer(t, peer_info);
2800 p = tree_get_path_to_peer(t->tree, peer_info->id);
2802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2803 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2804 t->local_tid, p->length);
2805 for (i = 0; i < p->length; i++)
2807 GNUNET_PEER_resolve (p->peers[0], &id);
2808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2809 GNUNET_h2s_full (&id.hashPubKey));
2815 send_create_path(peer_info, p, t);
2819 send_client_peer_connected(t, myid);
2823 /******************************************************************************/
2824 /********************* MESH LOCAL HANDLES **************************/
2825 /******************************************************************************/
2829 * Handler for client disconnection
2831 * @param cls closure
2832 * @param client identification of the client; NULL
2833 * for the last call when the server is destroyed
2836 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2838 struct MeshClient *c;
2839 struct MeshClient *next;
2841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2847 if (c->handle != client && NULL != client)
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2854 if (NULL != c->tunnels)
2856 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2857 &tunnel_destroy_iterator,
2859 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2862 /* deregister clients applications */
2863 if (NULL != c->apps)
2865 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2866 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2868 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2869 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2871 GNUNET_SCHEDULER_cancel (announce_applications_task);
2872 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2874 if (NULL != c->types)
2875 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2876 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2888 * Handler for new clients
2890 * @param cls closure
2891 * @param client identification of the client
2892 * @param message the actual message, which includes messages the client wants
2895 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2896 const struct GNUNET_MessageHeader *message)
2898 struct GNUNET_MESH_ClientConnect *cc_msg;
2899 struct MeshClient *c;
2900 GNUNET_MESH_ApplicationType *a;
2907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2908 /* Check data sanity */
2909 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2910 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2911 ntypes = ntohs (cc_msg->types);
2912 napps = ntohs (cc_msg->applications);
2914 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2917 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2921 /* Create new client structure */
2922 c = GNUNET_malloc (sizeof (struct MeshClient));
2924 c->id = next_client_id++;
2927 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2930 GNUNET_MESH_ApplicationType at;
2933 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2934 for (i = 0; i < napps; i++)
2937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2938 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2939 /* store in clients hashmap */
2940 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2941 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2942 /* store in global hashmap, for announcements */
2943 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2944 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2946 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2947 announce_applications_task =
2948 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2956 t = (uint16_t *) & a[napps];
2957 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2958 for (i = 0; i < ntypes; i++)
2961 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2963 /* store in clients hashmap */
2964 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2965 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2966 /* store in global hashmap */
2967 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2968 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2972 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2974 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2975 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2976 GNUNET_SERVER_notification_context_add (nc, client);
2978 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2986 * Handler for requests of new tunnels
2988 * @param cls closure
2989 * @param client identification of the client
2990 * @param message the actual message
2993 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
2994 const struct GNUNET_MessageHeader *message)
2996 struct GNUNET_MESH_TunnelMessage *t_msg;
2997 struct MeshTunnel *t;
2998 struct MeshClient *c;
2999 GNUNET_HashCode hash;
3001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3003 /* Sanity check for client registration */
3004 if (NULL == (c = client_get (client)))
3007 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3014 /* Message sanity check */
3015 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3018 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3022 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3023 /* Sanity check for tunnel numbering */
3024 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3030 /* Sanity check for duplicate tunnel IDs */
3031 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3034 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3038 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
3040 while (NULL != tunnel_get_by_pi (myid, next_tid))
3041 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3042 t->id.tid = next_tid++;
3044 t->local_tid = ntohl (t_msg->tunnel_id);
3046 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3048 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3050 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3051 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3054 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3058 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3060 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3061 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3064 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3067 t->tree = tree_new (t, myid);
3068 t->tree->refresh = REFRESH_PATH_TIME;
3069 t->tree->root->status = MESH_PEER_READY;
3070 t->tree->me = t->tree->root;
3072 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3078 * Handler for requests of deleting tunnels
3080 * @param cls closure
3081 * @param client identification of the client
3082 * @param message the actual message
3085 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3086 const struct GNUNET_MessageHeader *message)
3088 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3089 struct MeshClient *c;
3090 struct MeshTunnel *t;
3091 MESH_TunnelNumber tid;
3092 GNUNET_HashCode hash;
3094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
3096 /* Sanity check for client registration */
3097 if (NULL == (c = client_get (client)))
3100 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3103 /* Message sanity check */
3104 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3107 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3113 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3115 /* Retrieve tunnel */
3116 tid = ntohl (tunnel_msg->tunnel_id);
3118 /* Remove from local id hashmap */
3119 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3120 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3121 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
3123 // notify_tunnel_destroy(t);
3125 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3131 * Handler for connection requests to new peers
3133 * @param cls closure
3134 * @param client identification of the client
3135 * @param message the actual message (PeerControl)
3138 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3139 const struct GNUNET_MessageHeader *message)
3141 struct GNUNET_MESH_PeerControl *peer_msg;
3142 struct MeshPeerInfo *peer_info;
3143 struct MeshClient *c;
3144 struct MeshTunnel *t;
3145 MESH_TunnelNumber tid;
3147 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3148 /* Sanity check for client registration */
3149 if (NULL == (c = client_get (client)))
3152 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3156 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3157 /* Sanity check for message size */
3158 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3161 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3165 /* Tunnel exists? */
3166 tid = ntohl (peer_msg->tunnel_id);
3167 t = tunnel_get_by_local_id (c, tid);
3171 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3175 /* Does client own tunnel? */
3176 if (t->client->handle != client)
3179 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3182 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3183 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
3184 peer_info = peer_info_get (&peer_msg->peer);
3186 tunnel_add_peer(t, peer_info);
3187 peer_info_connect(peer_info, t);
3189 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3195 * Handler for disconnection requests of peers in a tunnel
3197 * @param cls closure
3198 * @param client identification of the client
3199 * @param message the actual message (PeerControl)
3202 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3203 const struct GNUNET_MessageHeader *message)
3205 struct GNUNET_MESH_PeerControl *peer_msg;
3206 struct MeshClient *c;
3207 struct MeshTunnel *t;
3208 MESH_TunnelNumber tid;
3210 /* Sanity check for client registration */
3211 if (NULL == (c = client_get (client)))
3214 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3217 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3218 /* Sanity check for message size */
3219 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3222 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3226 /* Tunnel exists? */
3227 tid = ntohl (peer_msg->tunnel_id);
3228 t = tunnel_get_by_local_id (c, tid);
3232 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3236 /* Does client own tunnel? */
3237 if (t->client->handle != client)
3240 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3244 /* Ok, delete peer from tunnel */
3245 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3246 &peer_msg->peer.hashPubKey);
3248 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3254 * Handler for connection requests to new peers by type
3256 * @param cls closure
3257 * @param client identification of the client
3258 * @param message the actual message (ConnectPeerByType)
3261 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3262 const struct GNUNET_MessageHeader *message)
3264 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3265 struct MeshClient *c;
3266 struct MeshTunnel *t;
3267 GNUNET_HashCode hash;
3268 MESH_TunnelNumber tid;
3270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3271 /* Sanity check for client registration */
3272 if (NULL == (c = client_get (client)))
3275 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3279 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3280 /* Sanity check for message size */
3281 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3282 ntohs (connect_msg->header.size))
3285 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3289 /* Tunnel exists? */
3290 tid = ntohl (connect_msg->tunnel_id);
3291 t = tunnel_get_by_local_id (c, tid);
3295 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3299 /* Does client own tunnel? */
3300 if (t->client->handle != client)
3303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3307 /* Do WE have the service? */
3308 t->type = ntohl (connect_msg->type);
3309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3310 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3311 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3314 /* Yes! Fast forward, add ourselves to the tunnel and send the
3315 * good news to the client
3317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3318 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3319 peer_info_get (&my_full_id),
3320 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3323 send_client_peer_connected(t, myid);
3324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3325 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3328 /* Ok, lets find a peer offering the service */
3329 if (NULL != t->dht_get_type)
3331 GNUNET_DHT_get_stop (t->dht_get_type);
3333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3334 GNUNET_h2s_full (&hash));
3336 GNUNET_DHT_get_start (dht_handle,
3337 GNUNET_TIME_UNIT_FOREVER_REL,
3338 GNUNET_BLOCK_TYPE_TEST,
3341 GNUNET_DHT_RO_RECORD_ROUTE |
3342 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3344 &dht_get_type_handler, t);
3346 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3352 * Handler for client traffic directed to one peer
3354 * @param cls closure
3355 * @param client identification of the client
3356 * @param message the actual message
3359 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3360 const struct GNUNET_MessageHeader *message)
3362 struct MeshClient *c;
3363 struct MeshTunnel *t;
3364 struct MeshPeerInfo *pi;
3365 struct GNUNET_MESH_Unicast *data_msg;
3366 MESH_TunnelNumber tid;
3369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3370 "MESH: Got a unicast request from a client!\n");
3372 /* Sanity check for client registration */
3373 if (NULL == (c = client_get (client)))
3376 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3379 data_msg = (struct GNUNET_MESH_Unicast *) message;
3380 /* Sanity check for message size */
3381 size = ntohs (message->size);
3382 if (sizeof (struct GNUNET_MESH_Unicast) +
3383 sizeof (struct GNUNET_MessageHeader) > size)
3386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3390 /* Tunnel exists? */
3391 tid = ntohl (data_msg->tid);
3392 t = tunnel_get_by_local_id (c, tid);
3396 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3400 /* Is it a local tunnel? Then, does client own the tunnel? */
3401 if (NULL != t->client &&
3402 NULL != t->client->handle &&
3403 t->client->handle != client)
3406 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3410 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3411 &data_msg->destination.hashPubKey);
3412 /* Is the selected peer in the tunnel? */
3416 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3420 /* Ok, everything is correct, send the message
3421 * (pretend we got it from a mesh peer)
3424 char buf[ntohs (message->size)];
3425 struct GNUNET_MESH_Unicast *copy;
3427 /* Work around const limitation */
3428 copy = (struct GNUNET_MESH_Unicast *) buf;
3429 memcpy (buf, data_msg, size);
3430 copy->oid = my_full_id;
3431 copy->tid = htonl (t->id.tid);
3432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3433 "MESH: calling generic handler...\n");
3434 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
3436 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3442 * Handler for client traffic directed to the origin
3444 * @param cls closure
3445 * @param client identification of the client
3446 * @param message the actual message
3449 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3450 const struct GNUNET_MessageHeader *message)
3452 struct GNUNET_MESH_ToOrigin *data_msg;
3453 struct GNUNET_PeerIdentity id;
3454 struct MeshClient *c;
3455 struct MeshTunnel *t;
3456 MESH_TunnelNumber tid;
3459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3460 "MESH: Got a ToOrigin request from a client!\n");
3462 /* Sanity check for client registration */
3463 if (NULL == (c = client_get (client)))
3466 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3469 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3470 /* Sanity check for message size */
3471 size = ntohs (message->size);
3472 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3473 sizeof (struct GNUNET_MessageHeader) > size)
3476 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3480 /* Tunnel exists? */
3481 tid = ntohl (data_msg->tid);
3482 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3485 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3488 t = tunnel_get_by_local_id (c, tid);
3492 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3496 /* It shouldn't be a local tunnel. */
3497 if (NULL != t->client)
3500 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3503 GNUNET_PEER_resolve(t->id.oid, &id);
3505 /* Ok, everything is correct, send the message
3506 * (pretend we got it from a mesh peer)
3509 char buf[ntohs (message->size)];
3510 struct GNUNET_MESH_ToOrigin *copy;
3512 /* Work around const limitation */
3513 copy = (struct GNUNET_MESH_ToOrigin *) buf;
3514 memcpy (buf, data_msg, size);
3516 copy->tid = htonl (t->id.tid);
3517 copy->sender = my_full_id;
3518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3519 "MESH: calling generic handler...\n");
3520 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL);
3522 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3528 * Handler for client traffic directed to all peers in a tunnel
3530 * @param cls closure
3531 * @param client identification of the client
3532 * @param message the actual message
3535 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3536 const struct GNUNET_MessageHeader *message)
3538 struct MeshClient *c;
3539 struct MeshTunnel *t;
3540 struct GNUNET_MESH_Multicast *data_msg;
3541 MESH_TunnelNumber tid;
3543 /* Sanity check for client registration */
3544 if (NULL == (c = client_get (client)))
3547 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3550 data_msg = (struct GNUNET_MESH_Multicast *) message;
3551 /* Sanity check for message size */
3552 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
3555 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3559 /* Tunnel exists? */
3560 tid = ntohl (data_msg->tid);
3561 t = tunnel_get_by_local_id (c, tid);
3565 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3569 /* Does client own tunnel? */
3570 if (t->client->handle != client)
3573 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3578 char buf[ntohs(message->size)];
3579 struct GNUNET_MESH_Multicast *copy;
3581 copy = (struct GNUNET_MESH_Multicast *)buf;
3582 memcpy(buf, message, ntohs(message->size));
3583 copy->oid = my_full_id;
3584 copy->tid = htonl(t->id.tid);
3585 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3588 /* receive done gets called when last copy is sent */
3593 * Functions to handle messages from clients
3595 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3596 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3597 {&handle_local_tunnel_create, NULL,
3598 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3599 sizeof (struct GNUNET_MESH_TunnelMessage)},
3600 {&handle_local_tunnel_destroy, NULL,
3601 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3602 sizeof (struct GNUNET_MESH_TunnelMessage)},
3603 {&handle_local_connect_add, NULL,
3604 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3605 sizeof (struct GNUNET_MESH_PeerControl)},
3606 {&handle_local_connect_del, NULL,
3607 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3608 sizeof (struct GNUNET_MESH_PeerControl)},
3609 {&handle_local_connect_by_type, NULL,
3610 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3611 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3612 {&handle_local_unicast, NULL,
3613 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3614 {&handle_local_to_origin, NULL,
3615 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3616 {&handle_local_multicast, NULL,
3617 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3623 * To be called on core init/fail.
3625 * @param cls service closure
3626 * @param server handle to the server for this service
3627 * @param identity the public identity of this peer
3630 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3631 const struct GNUNET_PeerIdentity *identity)
3633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3634 core_handle = server;
3635 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3637 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3638 GNUNET_SCHEDULER_shutdown();
3644 * Method called whenever a given peer connects.
3646 * @param cls closure
3647 * @param peer peer identity this notification is about
3648 * @param atsi performance data for the connection
3651 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3652 const struct GNUNET_ATS_Information *atsi)
3654 struct MeshPeerInfo *peer_info;
3655 struct MeshPeerPath *path;
3657 #if MESH_DEBUG_CONNECTION
3658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3660 GNUNET_h2s(&my_full_id.hashPubKey));
3662 peer_info = peer_info_get (peer);
3663 if (myid == peer_info->id)
3665 #if MESH_DEBUG_CONNECTION
3666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3670 #if MESH_DEBUG_CONNECTION
3673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3674 GNUNET_h2s(&peer->hashPubKey));
3677 path = path_new (2);
3678 path->peers[0] = myid;
3679 path->peers[1] = peer_info->id;
3680 GNUNET_PEER_change_rc(myid, 1);
3681 GNUNET_PEER_change_rc(peer_info->id, 1);
3682 path_add_to_peer (peer_info, path);
3687 * Method called whenever a peer disconnects.
3689 * @param cls closure
3690 * @param peer peer identity this notification is about
3693 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3695 struct MeshPeerInfo *pi;
3698 #if MESH_DEBUG_CONNECTION
3699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3701 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3707 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3709 peer_info_cancel_transmission(pi, i);
3711 path_remove_from_peer (pi, pi->id, myid);
3712 #if MESH_DEBUG_CONNECTION
3715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3722 /******************************************************************************/
3723 /************************ MAIN FUNCTIONS ****************************/
3724 /******************************************************************************/
3727 * Iterator over hash map entries.
3729 * @param cls closure
3730 * @param key current key code
3731 * @param value value in the hash map
3732 * @return GNUNET_YES if we should continue to
3737 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
3739 struct MeshTunnel *t = value;
3745 * Task run during shutdown.
3751 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3754 /* TODO: destroy tunnels? */
3755 if (core_handle != NULL)
3757 GNUNET_CORE_disconnect (core_handle);
3760 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
3761 if (dht_handle != NULL)
3763 GNUNET_DHT_disconnect (dht_handle);
3768 GNUNET_SERVER_notification_context_destroy (nc);
3771 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3773 GNUNET_SCHEDULER_cancel (announce_id_task);
3774 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3780 * Process mesh requests.
3782 * @param cls closure
3783 * @param server the initialized server
3784 * @param c configuration to use
3787 run (void *cls, struct GNUNET_SERVER_Handle *server,
3788 const struct GNUNET_CONFIGURATION_Handle *c)
3790 struct MeshPeerInfo *peer;
3791 struct MeshPeerPath *p;
3794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3795 server_handle = server;
3796 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3797 CORE_QUEUE_SIZE, /* queue size */
3798 NULL, /* Closure passed to MESH functions */
3799 &core_init, /* Call core_init once connected */
3800 &core_connect, /* Handle connects */
3801 &core_disconnect, /* remove peers on disconnects */
3802 NULL, /* Don't notify about all incoming messages */
3803 GNUNET_NO, /* For header only in notification */
3804 NULL, /* Don't notify about all outbound messages */
3805 GNUNET_NO, /* For header-only out notification */
3806 core_handlers); /* Register these handlers */
3807 if (core_handle == NULL)
3810 GNUNET_SCHEDULER_shutdown ();
3815 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3818 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3820 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3821 GNUNET_SCHEDULER_shutdown ();
3824 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3825 GNUNET_free (keyfile);
3826 if (my_private_key == NULL)
3828 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3829 _("Mesh service could not access hostkey. Exiting.\n"));
3830 GNUNET_SCHEDULER_shutdown ();
3833 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3834 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3835 &my_full_id.hashPubKey);
3836 myid = GNUNET_PEER_intern (&my_full_id);
3838 dht_handle = GNUNET_DHT_connect (c, 64);
3839 if (dht_handle == NULL)
3845 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3847 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3848 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3849 peers = GNUNET_CONTAINER_multihashmap_create (32);
3850 applications = GNUNET_CONTAINER_multihashmap_create (32);
3851 types = GNUNET_CONTAINER_multihashmap_create (32);
3853 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3854 nc = GNUNET_SERVER_notification_context_create (server_handle,
3856 GNUNET_SERVER_disconnect_notify (server_handle,
3857 &handle_local_client_disconnect,
3862 clients_tail = NULL;
3867 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3868 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3870 /* Create a peer_info for the local peer */
3871 peer = peer_info_get(&my_full_id);
3874 path_add_to_peer(peer, p);
3876 /* Scheduled the task to clean up when shutdown is called */
3877 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3884 * The main function for the mesh service.
3886 * @param argc number of arguments from the command line
3887 * @param argv command line arguments
3888 * @return 0 ok, 1 on error
3891 main (int argc, char *const *argv)
3896 // fprintf (stderr, "main ()\n");
3898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3901 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3905 // fprintf (stderr, "main () END\n");