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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1639 "MESH: sending a multicast packet...\n");
1640 size = ntohs (msg->size);
1641 GNUNET_assert (NULL != t->tree->me);
1642 n = t->tree->me->children_head;
1645 copies = GNUNET_malloc (sizeof (unsigned int));
1646 for (*copies = 0; NULL != n; n = n->next)
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (%u copies)\n", copies);
1649 n = t->tree->me->children_head;
1650 data = GNUNET_malloc (size);
1651 memcpy (data, &msg, size);
1654 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1655 info->origin = &t->id;
1658 info->copies = copies;
1659 if (NULL != t->client->handle)
1661 info->client = t->client->handle;
1663 info->timeout_task = GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT,
1667 info->destination = n->peer;
1668 neighbor = path_get_first_hop(t->tree, n->peer);
1669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1670 "MESH: sending to %s...\n",
1671 GNUNET_i2s (neighbor));
1672 info->peer = peer_info_get(neighbor);
1673 GNUNET_assert (NULL != info->peer);
1674 for (i = 0; NULL != info->peer->core_transmit[i]; i++)
1676 if (i == (CORE_QUEUE_SIZE - 1))
1683 info->handler_n = i;
1684 info->peer->infos[i] = info;
1685 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1686 info->peer->core_transmit[i] =
1687 GNUNET_CORE_notify_transmit_ready (core_handle,
1690 GNUNET_TIME_UNIT_FOREVER_REL,
1693 &send_core_data_multicast, info);
1700 * Destroy the tunnel and free any allocated resources linked to it
1702 * @param t the tunnel to destroy
1704 * @return GNUNET_OK on success
1707 tunnel_destroy (struct MeshTunnel *t)
1709 struct MeshClient *c;
1710 struct MeshQueue *q;
1711 struct MeshQueue *qn;
1712 GNUNET_HashCode hash;
1721 struct GNUNET_PeerIdentity id;
1723 GNUNET_PEER_resolve(t->id.oid, &id);
1724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1725 "MESH: destroying tunnel %s [%x]\n",
1729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1733 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1734 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1739 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1741 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1745 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1747 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1748 GNUNET_break (GNUNET_YES ==
1749 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t));
1752 if (NULL != t->peers)
1754 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
1755 &peer_info_delete_tunnel, t);
1756 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1761 if (NULL != q->data)
1762 GNUNET_free (q->data);
1766 /* TODO cancel core transmit ready in case it was active */
1768 tree_destroy(t->tree);
1769 if (NULL != t->dht_get_type)
1770 GNUNET_DHT_get_stop(t->dht_get_type);
1777 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
1778 * client when the client disconnects.
1780 * @param cls closure (client that is disconnecting)
1781 * @param key the hash of the local tunnel id (used to access the hashmap)
1782 * @param value the value stored at the key (tunnel to destroy)
1784 * @return GNUNET_OK on success
1787 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
1789 struct MeshTunnel *t = value;
1792 if (NULL != t->dht_get_type)
1794 GNUNET_DHT_get_stop (t->dht_get_type);
1796 r = tunnel_destroy (t);
1801 /******************************************************************************/
1802 /**************** MESH NETWORK HANDLER HELPERS ***********************/
1803 /******************************************************************************/
1806 * Function called to notify a client about the socket
1807 * being ready to queue more data. "buf" will be
1808 * NULL and "size" zero if the socket was closed for
1809 * writing in the meantime.
1811 * @param cls closure
1812 * @param size number of bytes available in buf
1813 * @param buf where the callee should write the message
1814 * @return number of bytes written to buf
1817 send_core_create_path (void *cls, size_t size, void *buf)
1819 struct MeshPathInfo *info = cls;
1820 struct GNUNET_MESH_ManipulatePath *msg;
1821 struct GNUNET_PeerIdentity *peer_ptr;
1822 struct MeshPeerInfo *peer = info->peer;
1823 struct MeshTunnel *t = info->t;
1824 struct MeshPeerPath *p = info->path;
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1829 "MESH: CREATE PATH sending...\n");
1831 sizeof (struct GNUNET_MESH_ManipulatePath) +
1832 p->length * sizeof (struct GNUNET_PeerIdentity);
1834 if (size < size_needed || NULL == buf)
1836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
1837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
1838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
1840 info->peer->core_transmit[info->pos] =
1841 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
1842 GNUNET_TIME_UNIT_FOREVER_REL,
1843 path_get_first_hop (t->tree, peer->id),
1845 &send_core_create_path,
1849 info->peer->core_transmit[info->pos] = NULL;
1852 struct GNUNET_PeerIdentity id;
1854 GNUNET_PEER_resolve (peer->id, &id);
1855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1856 "MESH: setting core_transmit %s [%u] to NULL\n",
1861 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
1862 msg->header.size = htons (size_needed);
1863 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
1864 msg->tid = ntohl (t->id.tid);
1866 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
1867 for (i = 0; i < p->length; i++)
1869 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 "MESH: CREATE PATH (%u bytes long) sent!\n",
1884 * Function called to notify a client about the socket
1885 * being ready to queue more data. "buf" will be
1886 * NULL and "size" zero if the socket was closed for
1887 * writing in the meantime.
1889 * @param cls closure (MeshDataDescriptor with all info to build packet)
1890 * @param size number of bytes available in buf
1891 * @param buf where the callee should write the message
1892 * @return number of bytes written to buf
1895 send_core_data_to_origin (void *cls, size_t size, void *buf)
1897 struct MeshDataDescriptor *info = cls;
1898 struct GNUNET_MESH_ToOrigin *msg = buf;
1901 GNUNET_assert (NULL != info);
1902 total_size = sizeof (struct GNUNET_MESH_ToOrigin) + info->size;
1903 GNUNET_assert (total_size < 65536); /* UNIT16_MAX */
1905 if (total_size > size)
1907 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1908 "not enough buffer to send data to origin\n");
1911 msg->header.size = htons (total_size);
1912 msg->header.type = htons (GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN);
1913 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
1914 msg->tid = htonl (info->origin->tid);
1915 if (0 != info->size)
1917 memcpy (&msg[1], &info[1], info->size);
1919 if (NULL != info->client)
1921 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1930 * Function called to notify a client about the socket
1931 * being ready to queue more data. "buf" will be
1932 * NULL and "size" zero if the socket was closed for
1933 * writing in the meantime.
1935 * @param cls closure (data itself)
1936 * @param size number of bytes available in buf
1937 * @param buf where the callee should write the message
1939 * @return number of bytes written to buf
1942 send_core_data_multicast (void *cls, size_t size, void *buf)
1944 struct MeshDataDescriptor *info = cls;
1945 struct GNUNET_MessageHeader *msg = buf;
1948 GNUNET_assert (NULL != info);
1949 GNUNET_assert (NULL != info->peer);
1950 total_size = info->size;
1951 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1953 if (total_size > size)
1956 struct GNUNET_PeerIdentity id;
1958 GNUNET_PEER_resolve(info->peer->id, &id);
1959 info->peer->core_transmit[info->handler_n] =
1960 GNUNET_CORE_notify_transmit_ready (core_handle,
1963 GNUNET_TIME_UNIT_FOREVER_REL,
1966 &send_core_data_multicast,
1970 info->peer->core_transmit[info->handler_n] = NULL;
1971 info->peer->infos[info->handler_n] = NULL;
1972 memcpy (&msg, info->data, total_size);
1973 if (0 == --(*info->copies))
1975 if (NULL != info->client)
1977 if (GNUNET_SCHEDULER_NO_TASK != info->timeout_task)
1978 GNUNET_SCHEDULER_cancel(info->timeout_task);
1979 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
1981 GNUNET_free (info->data);
1982 GNUNET_free (info->copies);
1990 * Function called to notify a client about the socket
1991 * being ready to queue more data. "buf" will be
1992 * NULL and "size" zero if the socket was closed for
1993 * writing in the meantime.
1995 * @param cls closure (MeshDataDescriptor)
1996 * @param size number of bytes available in buf
1997 * @param buf where the callee should write the message
1998 * @return number of bytes written to buf
2001 send_core_path_ack (void *cls, size_t size, void *buf)
2003 struct MeshDataDescriptor *info = cls;
2004 struct GNUNET_MESH_PathACK *msg = buf;
2006 GNUNET_assert (NULL != info);
2009 info->peer->core_transmit[info->handler_n] = NULL;
2011 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2016 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2017 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2018 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2019 msg->tid = htonl (info->origin->tid);
2020 msg->peer_id = my_full_id;
2022 /* TODO add signature */
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2025 return sizeof (struct GNUNET_MESH_PathACK);
2030 * Function called to notify a client about the socket
2031 * being ready to queue more data. "buf" will be
2032 * NULL and "size" zero if the socket was closed for
2033 * writing in the meantime.
2035 * @param cls closure (data itself)
2036 * @param size number of bytes available in buf
2037 * @param buf where the callee should write the message
2038 * @return number of bytes written to buf
2041 send_core_data_raw (void *cls, size_t size, void *buf)
2043 struct GNUNET_MessageHeader *msg = cls;
2046 GNUNET_assert (NULL != msg);
2047 total_size = ntohs (msg->size);
2049 if (total_size > size)
2054 memcpy (buf, msg, total_size);
2062 * Send another peer a notification to destroy a tunnel
2063 * @param cls The tunnel to destroy
2064 * @param size Size in the buffer
2065 * @param buf Memory where to put the data to transmit
2066 * @return Size of data put in buffer
2069 send_p2p_tunnel_destroy (void *cls, size_t size, void *buf)
2071 struct MeshTunnel *t = cls;
2072 struct MeshClient *c;
2073 struct GNUNET_MESH_TunnelMessage *msg;
2077 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2078 /*FIXME*/ msg->header.size =
2079 htons (sizeof (struct GNUNET_MESH_TunnelMessage));
2080 msg->tunnel_id = htonl (t->id.tid);
2082 tunnel_destroy (c, t);
2083 return sizeof (struct GNUNET_MESH_TunnelMessage);
2088 /******************************************************************************/
2089 /******************** MESH NETWORK HANDLERS **************************/
2090 /******************************************************************************/
2094 * Core handler for path creation
2095 * struct GNUNET_CORE_MessageHandler
2097 * @param cls closure
2098 * @param message message
2099 * @param peer peer identity this notification is about
2100 * @param atsi performance data
2101 * @return GNUNET_OK to keep the connection open,
2102 * GNUNET_SYSERR to close it (signal serious error)
2106 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2107 const struct GNUNET_MessageHeader *message,
2108 const struct GNUNET_ATS_Information *atsi)
2110 unsigned int own_pos;
2113 MESH_TunnelNumber tid;
2114 struct GNUNET_MESH_ManipulatePath *msg;
2115 struct GNUNET_PeerIdentity *pi;
2116 GNUNET_HashCode hash;
2117 struct MeshPeerPath *path;
2118 struct MeshPeerInfo *dest_peer_info;
2119 struct MeshPeerInfo *orig_peer_info;
2120 struct MeshTunnel *t;
2122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2123 "MESH: Received a path create msg [%s]\n",
2124 GNUNET_i2s(&my_full_id));
2125 size = ntohs (message->size);
2126 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2128 GNUNET_break_op (0);
2132 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2133 if (size % sizeof (struct GNUNET_PeerIdentity))
2135 GNUNET_break_op (0);
2138 size /= sizeof (struct GNUNET_PeerIdentity);
2141 GNUNET_break_op (0);
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2145 "MESH: path has %u hops.\n",
2147 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2149 tid = ntohl (msg->tid);
2150 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2151 t = tunnel_get (pi, tid);
2152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2153 "MESH: path is for tunnel %s [%X].\n",
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2159 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2160 t->id.oid = GNUNET_PEER_intern (pi);
2162 t->local_tid = next_local_tid++;
2163 /* FIXME test if taken */
2164 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2165 t->tree = tree_new(t, t->id.oid);
2167 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2169 GNUNET_CONTAINER_multihashmap_put (
2173 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2179 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2181 GNUNET_CONTAINER_multihashmap_put (
2185 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2193 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2194 if (NULL == dest_peer_info)
2196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2197 "MESH: Creating PeerInfo for destination.\n");
2198 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2199 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2200 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2202 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2204 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2205 if (NULL == orig_peer_info)
2207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2208 "MESH: Creating PeerInfo for origin.\n");
2209 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2210 orig_peer_info->id = GNUNET_PEER_intern (pi);
2211 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2212 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2215 path = path_new (size);
2217 for (i = 0; i < size; i++)
2219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2220 "MESH: ... adding %s\n",
2221 GNUNET_i2s(&pi[i]));
2222 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2223 if (path->peers[i] == myid)
2226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2227 "MESH: Own position: %u\n", own_pos);
2230 /* cannot be self, must be 'not found' */
2231 /* create path: self not found in path through self */
2232 GNUNET_break_op (0);
2233 path_destroy (path);
2234 /* FIXME error. destroy tunnel? leave for timeout? */
2237 tunnel_add_path (t, path, own_pos);
2238 t->tree->me = tree_find_peer(t->tree->root, myid);
2239 if (own_pos == size - 1)
2241 /* It is for us! Send ack. */
2242 struct GNUNET_MESH_TunnelNotification cmsg;
2243 struct MeshDataDescriptor *info;
2246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2247 "MESH: It's for us!\n");
2248 path_add_to_origin (orig_peer_info, path);
2249 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2250 info->origin = &t->id;
2251 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2252 GNUNET_assert (NULL != info->peer);
2253 j = peer_info_transmit_slot(info->peer);
2254 info->handler_n = j;
2255 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2256 info->peer->infos[j] = info;
2257 info->peer->core_transmit[j] =
2258 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2259 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2260 sizeof (struct GNUNET_MESH_PathACK),
2261 &send_core_path_ack, info);
2262 cmsg.header.size = htons(sizeof(cmsg));
2263 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2264 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2265 cmsg.tunnel_id = htonl(t->local_tid);
2266 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2270 struct MeshPeerPath *path2;
2272 /* It's for somebody else! Retransmit. */
2273 path2 = path_duplicate(path);
2274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2275 "MESH: Retransmitting.\n");
2276 path_add_to_peer(dest_peer_info, path);
2277 path = path_duplicate(path2);
2278 path_add_to_origin(orig_peer_info, path2);
2279 send_create_path(dest_peer_info, path, t);
2286 * Core handler for mesh network traffic going from the origin to a peer
2288 * @param cls closure
2289 * @param peer peer identity this notification is about
2290 * @param message message
2291 * @param atsi performance data
2292 * @return GNUNET_OK to keep the connection open,
2293 * GNUNET_SYSERR to close it (signal serious error)
2296 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2297 const struct GNUNET_MessageHeader *message,
2298 const struct GNUNET_ATS_Information *atsi)
2300 struct GNUNET_MESH_Unicast *msg;
2301 struct MeshTunnel *t;
2305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2306 "MESH: got a unicast packet from %s\n",
2308 size = ntohs (message->size);
2310 sizeof (struct GNUNET_MESH_Unicast) +
2311 sizeof (struct GNUNET_MessageHeader))
2316 msg = (struct GNUNET_MESH_Unicast *) message;
2317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2318 "MESH: of type %u\n",
2319 ntohs (msg[1].header.type));
2320 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2323 /* TODO notify back: we don't know this tunnel */
2324 GNUNET_break_op (0);
2327 pid = GNUNET_PEER_search(&msg->destination);
2330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2331 "MESH: it's for us! sending to clients...\n");
2332 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2336 "MESH: not for us, retransmitting...\n");
2337 msg = GNUNET_malloc (size);
2338 memcpy (msg, message, size);
2339 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2340 GNUNET_TIME_UNIT_FOREVER_REL,
2341 path_get_first_hop (t->tree, pid),
2343 &send_core_data_raw, msg);
2349 * Core handler for mesh network traffic going from the origin to all peers
2351 * @param cls closure
2352 * @param message message
2353 * @param peer peer identity this notification is about
2354 * @param atsi performance data
2355 * @return GNUNET_OK to keep the connection open,
2356 * GNUNET_SYSERR to close it (signal serious error)
2358 * TODO: Check who we got this from, to validate route.
2361 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2362 const struct GNUNET_MessageHeader *message,
2363 const struct GNUNET_ATS_Information *atsi)
2365 struct GNUNET_MESH_Multicast *msg;
2366 struct MeshTunnel *t;
2369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2370 "MESH: got a multicast packet from %s\n",
2372 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_Multicast);
2373 if (size < sizeof (struct GNUNET_MessageHeader))
2375 GNUNET_break_op (0);
2378 msg = (struct GNUNET_MESH_Multicast *) message;
2379 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2383 /* TODO notify that we dont know that tunnel */
2384 GNUNET_break_op (0);
2388 /* Transmit to locally interested clients */
2389 if (GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2391 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2393 tunnel_send_multicast(t, message);
2400 * Core handler for mesh network traffic toward the owner of a tunnel
2402 * @param cls closure
2403 * @param message message
2404 * @param peer peer identity this notification is about
2405 * @param atsi performance data
2407 * @return GNUNET_OK to keep the connection open,
2408 * GNUNET_SYSERR to close it (signal serious error)
2411 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2412 const struct GNUNET_MessageHeader *message,
2413 const struct GNUNET_ATS_Information *atsi)
2415 struct GNUNET_MESH_ToOrigin *msg;
2416 struct GNUNET_PeerIdentity id;
2417 struct MeshPeerInfo *peer_info;
2418 struct MeshTunnel *t;
2421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2422 "MESH: got a ToOrigin packet from %s\n",
2424 size = ntohs (message->size);
2425 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2426 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2428 GNUNET_break_op (0);
2431 msg = (struct GNUNET_MESH_ToOrigin *) message;
2432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2433 "MESH: of type %u\n",
2434 ntohs (msg[1].header.type));
2435 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2439 /* TODO notify that we dont know this tunnel (whom)? */
2440 GNUNET_break_op (0);
2444 if (t->id.oid == myid)
2447 struct GNUNET_MESH_ToOrigin *copy;
2449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2450 "MESH: it's for us! sending to clients...\n");
2451 if (NULL == t->client)
2453 /* got data packet for ownerless tunnel */
2454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2455 "MESH: no clients!\n");
2456 GNUNET_break_op (0);
2459 /* TODO signature verification */
2460 memcpy (cbuf, message, size);
2461 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2462 copy->tid = htonl (t->local_tid);
2463 GNUNET_SERVER_notification_context_unicast (nc,
2469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2470 "MESH: not for us, retransmitting...\n");
2472 peer_info = peer_info_get (&msg->oid);
2473 if (NULL == peer_info)
2475 /* unknown origin of tunnel */
2479 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
2480 msg = GNUNET_malloc (size);
2481 memcpy (msg, message, size);
2482 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2483 GNUNET_TIME_UNIT_FOREVER_REL, &id, size,
2484 &send_core_data_raw, msg);
2491 * Core handler for path ACKs
2493 * @param cls closure
2494 * @param message message
2495 * @param peer peer identity this notification is about
2496 * @param atsi performance data
2498 * @return GNUNET_OK to keep the connection open,
2499 * GNUNET_SYSERR to close it (signal serious error)
2502 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2503 const struct GNUNET_MessageHeader *message,
2504 const struct GNUNET_ATS_Information *atsi)
2506 struct GNUNET_MESH_PathACK *msg;
2507 struct GNUNET_PeerIdentity id;
2508 struct MeshTunnelTreeNode *n;
2509 struct MeshPeerInfo *peer_info;
2510 struct MeshTunnel *t;
2512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2513 "MESH: Received a path ACK msg [%s]\n",
2514 GNUNET_i2s(&my_full_id));
2515 msg = (struct GNUNET_MESH_PathACK *) message;
2516 t = tunnel_get (&msg->oid, msg->tid);
2519 /* TODO notify that we don't know the tunnel */
2523 /* Message for us? */
2524 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2527 "MESH: It's for us!\n");
2528 if (NULL == t->client)
2530 GNUNET_break_op (0);
2533 peer_info = peer_info_get (&msg->peer_id);
2534 if (NULL == peer_info)
2536 GNUNET_break_op (0);
2539 n = tree_find_peer(t->tree->root, peer_info->id);
2542 GNUNET_break_op (0);
2545 n->status = MESH_PEER_READY;
2546 send_client_peer_connected(t, peer_info->id);
2550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2551 "MESH: not for us, retransmitting...\n");
2552 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
2553 peer_info = peer_info_get (&msg->oid);
2554 if (NULL == peer_info)
2556 /* If we know the tunnel, we should DEFINITELY know the peer */
2560 msg = GNUNET_malloc (sizeof (struct GNUNET_MESH_PathACK));
2561 memcpy (msg, message, sizeof (struct GNUNET_MESH_PathACK));
2562 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2563 GNUNET_TIME_UNIT_FOREVER_REL,
2565 sizeof (struct GNUNET_MESH_PathACK),
2566 &send_core_data_raw, msg);
2572 * Functions to handle messages from core
2574 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2575 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2576 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2577 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2578 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2579 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2580 sizeof (struct GNUNET_MESH_PathACK)},
2586 /******************************************************************************/
2587 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2588 /******************************************************************************/
2591 * deregister_app: iterator for removing each application registered by a client
2593 * @param cls closure
2594 * @param key the hash of the application id (used to access the hashmap)
2595 * @param value the value stored at the key (client)
2597 * @return GNUNET_OK on success
2600 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2602 GNUNET_break (GNUNET_YES ==
2603 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
2609 * notify_client_connection_failure: notify a client that the connection to the
2610 * requested remote peer is not possible (for instance, no route found)
2611 * Function called when the socket is ready to queue more data. "buf" will be
2612 * NULL and "size" zero if the socket was closed for writing in the meantime.
2614 * @param cls closure
2615 * @param size number of bytes available in buf
2616 * @param buf where the callee should write the message
2617 * @return number of bytes written to buf
2620 notify_client_connection_failure (void *cls, size_t size, void *buf)
2623 struct MeshPeerInfo *peer_info;
2624 struct GNUNET_MESH_PeerControl *msg;
2625 struct GNUNET_PeerIdentity id;
2627 if (0 == size && NULL == buf)
2629 // TODO retry? cancel?
2633 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
2634 peer_info = (struct MeshPeerInfo *) cls;
2635 msg = (struct GNUNET_MESH_PeerControl *) buf;
2636 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2637 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
2638 // msg->tunnel_id = htonl(peer_info->t->tid);
2639 GNUNET_PEER_resolve (peer_info->id, &id);
2640 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
2648 * Send keepalive packets for a peer
2650 * @param cls Closure (tunnel for which to send the keepalive).
2651 * @param tc Notification context.
2653 * TODO: implement explicit multicast keepalive?
2656 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2658 struct MeshTunnel *t = cls;
2659 struct GNUNET_MessageHeader *payload;
2660 struct GNUNET_MESH_Multicast *msg;
2663 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2667 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
2669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2670 "MESH: sending keepalive for tunnel %d\n",
2673 size = sizeof(struct GNUNET_MESH_Multicast) +
2674 sizeof(struct GNUNET_MessageHeader);
2675 msg = GNUNET_malloc (size);
2676 msg->header.size = htons (size);
2677 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
2678 msg->oid = my_full_id;
2679 msg->tid = htonl(t->id.tid);
2680 payload = (struct GNUNET_MessageHeader *) &msg[1];
2681 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
2682 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2683 handle_mesh_data_multicast (NULL, &my_full_id, &msg->header, NULL);
2686 t->path_refresh_task =
2687 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
2693 * Function to process paths received for a new peer addition. The recorded
2694 * paths form the initial tunnel, which can be optimized later.
2695 * Called on each result obtained for the DHT search.
2697 * @param cls closure
2698 * @param exp when will this value expire
2699 * @param key key of the result
2700 * @param type type of the result
2701 * @param size number of bytes in data
2702 * @param data pointer to the result data
2704 * TODO: re-issue the request after certain time? cancel after X results?
2707 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2708 const GNUNET_HashCode * key,
2709 const struct GNUNET_PeerIdentity *get_path,
2710 unsigned int get_path_length,
2711 const struct GNUNET_PeerIdentity *put_path,
2712 unsigned int put_path_length,
2713 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2715 struct MeshPathInfo *path_info = cls;
2716 struct MeshPeerPath *p;
2717 struct GNUNET_PeerIdentity pi;
2720 GNUNET_PEER_resolve (path_info->peer->id, &pi);
2721 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2722 "MESH: Got results from DHT for %s\n",
2723 GNUNET_h2s_full(&pi.hashPubKey));
2724 GNUNET_DHT_get_stop(path_info->peer->dhtget);
2725 path_info->peer->dhtget = NULL;
2727 p = path_build_from_dht (get_path, get_path_length,
2728 put_path, put_path_length);
2729 path_add_to_peer (path_info->peer, p);
2730 for (i = 0; i < path_info->peer->ntunnels; i++)
2732 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
2733 peer_info_connect(path_info->peer, path_info->t);
2735 GNUNET_free (path_info);
2742 * Function to process paths received for a new peer addition. The recorded
2743 * paths form the initial tunnel, which can be optimized later.
2744 * Called on each result obtained for the DHT search.
2746 * @param cls closure
2747 * @param exp when will this value expire
2748 * @param key key of the result
2749 * @param type type of the result
2750 * @param size number of bytes in data
2751 * @param data pointer to the result data
2754 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
2755 const GNUNET_HashCode * key,
2756 const struct GNUNET_PeerIdentity *get_path,
2757 unsigned int get_path_length,
2758 const struct GNUNET_PeerIdentity *put_path,
2759 unsigned int put_path_length,
2760 enum GNUNET_BLOCK_Type type, size_t size,
2763 const struct GNUNET_PeerIdentity *pi = data;
2764 struct GNUNET_PeerIdentity id;
2765 struct MeshTunnel *t = cls;
2766 struct MeshPeerInfo *peer_info;
2767 struct MeshPathInfo *path_info;
2768 struct MeshPeerPath *p;
2771 if (size != sizeof (struct GNUNET_PeerIdentity))
2773 GNUNET_break_op (0);
2776 GNUNET_assert (NULL != t->client);
2777 GNUNET_DHT_get_stop (t->dht_get_type);
2778 t->dht_get_type = NULL;
2779 peer_info = peer_info_get (pi);
2780 GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey, peer_info,
2781 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2783 if ((NULL == get_path || NULL == put_path) && NULL == peer_info->path_head &&
2784 NULL == peer_info->dhtget)
2786 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2787 path_info->peer = peer_info;
2789 /* we don't have a route to the peer, let's try a direct lookup */
2791 GNUNET_DHT_get_start (dht_handle, /* handle */
2792 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
2793 GNUNET_BLOCK_TYPE_TEST, /* block type */
2794 &pi->hashPubKey, /* key to look up */
2795 10U, /* replication level */
2796 GNUNET_DHT_RO_RECORD_ROUTE |
2797 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2798 /* option to dht: record route */
2800 0, /* xquery bits */
2801 dht_get_id_handler, /* callback */
2802 path_info); /* closure */
2806 p = path_build_from_dht (get_path, get_path_length, put_path, put_path_length);
2807 path_add_to_peer (peer_info, p);
2808 tunnel_add_peer(t, peer_info);
2809 p = tree_get_path_to_peer(t->tree, peer_info->id);
2811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2812 "MESH: new route for tunnel 0x%x found, has %u hops\n",
2813 t->local_tid, p->length);
2814 for (i = 0; i < p->length; i++)
2816 GNUNET_PEER_resolve (p->peers[0], &id);
2817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:\t%d\t%s\n", i,
2818 GNUNET_h2s_full (&id.hashPubKey));
2824 send_create_path(peer_info, p, t);
2828 send_client_peer_connected(t, myid);
2832 /******************************************************************************/
2833 /********************* MESH LOCAL HANDLES **************************/
2834 /******************************************************************************/
2838 * Handler for client disconnection
2840 * @param cls closure
2841 * @param client identification of the client; NULL
2842 * for the last call when the server is destroyed
2845 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2847 struct MeshClient *c;
2848 struct MeshClient *next;
2850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
2852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
2856 if (c->handle != client && NULL != client)
2858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
2862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
2863 if (NULL != c->tunnels)
2865 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
2866 &tunnel_destroy_iterator,
2868 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
2871 /* deregister clients applications */
2872 if (NULL != c->apps)
2874 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
2875 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
2877 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
2878 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
2880 GNUNET_SCHEDULER_cancel (announce_applications_task);
2881 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
2883 if (NULL != c->types)
2884 GNUNET_CONTAINER_multihashmap_destroy (c->types);
2885 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
2891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
2897 * Handler for new clients
2899 * @param cls closure
2900 * @param client identification of the client
2901 * @param message the actual message, which includes messages the client wants
2904 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
2905 const struct GNUNET_MessageHeader *message)
2907 struct GNUNET_MESH_ClientConnect *cc_msg;
2908 struct MeshClient *c;
2909 GNUNET_MESH_ApplicationType *a;
2916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
2917 /* Check data sanity */
2918 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
2919 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
2920 ntypes = ntohs (cc_msg->types);
2921 napps = ntohs (cc_msg->applications);
2923 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
2926 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2930 /* Create new client structure */
2931 c = GNUNET_malloc (sizeof (struct MeshClient));
2933 c->id = next_client_id++;
2936 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
2939 GNUNET_MESH_ApplicationType at;
2942 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
2943 for (i = 0; i < napps; i++)
2946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
2947 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
2948 /* store in clients hashmap */
2949 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
2950 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2951 /* store in global hashmap, for announcements */
2952 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
2953 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2955 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
2956 announce_applications_task =
2957 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
2965 t = (uint16_t *) & a[napps];
2966 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
2967 for (i = 0; i < ntypes; i++)
2970 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
2972 /* store in clients hashmap */
2973 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
2974 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2975 /* store in global hashmap */
2976 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
2977 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2981 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
2983 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
2984 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
2985 GNUNET_SERVER_notification_context_add (nc, client);
2987 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
2995 * Handler for requests of new tunnels
2997 * @param cls closure
2998 * @param client identification of the client
2999 * @param message the actual message
3002 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3003 const struct GNUNET_MessageHeader *message)
3005 struct GNUNET_MESH_TunnelMessage *t_msg;
3006 struct MeshTunnel *t;
3007 struct MeshClient *c;
3008 GNUNET_HashCode hash;
3010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3012 /* Sanity check for client registration */
3013 if (NULL == (c = client_get (client)))
3016 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3023 /* Message sanity check */
3024 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3031 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3032 /* Sanity check for tunnel numbering */
3033 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3036 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3039 /* Sanity check for duplicate tunnel IDs */
3040 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3043 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3047 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
3049 while (NULL != tunnel_get_by_pi (myid, next_tid))
3050 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3051 t->id.tid = next_tid++;
3053 t->local_tid = ntohl (t_msg->tunnel_id);
3055 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3057 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3059 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3060 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3063 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3067 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3069 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3070 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3076 t->tree = tree_new (t, myid);
3077 t->tree->refresh = REFRESH_PATH_TIME;
3078 t->tree->root->status = MESH_PEER_READY;
3079 t->tree->me = t->tree->root;
3081 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3087 * Handler for requests of deleting tunnels
3089 * @param cls closure
3090 * @param client identification of the client
3091 * @param message the actual message
3094 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3095 const struct GNUNET_MessageHeader *message)
3097 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3098 struct MeshClient *c;
3099 struct MeshTunnel *t;
3100 MESH_TunnelNumber tid;
3101 GNUNET_HashCode hash;
3103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel\n");
3105 /* Sanity check for client registration */
3106 if (NULL == (c = client_get (client)))
3109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3112 /* Message sanity check */
3113 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3116 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3122 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3124 /* Retrieve tunnel */
3125 tid = ntohl (tunnel_msg->tunnel_id);
3127 /* Remove from local id hashmap */
3128 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3129 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3130 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
3132 // notify_tunnel_destroy(t);
3134 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3140 * Handler for connection requests to new peers
3142 * @param cls closure
3143 * @param client identification of the client
3144 * @param message the actual message (PeerControl)
3147 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3148 const struct GNUNET_MessageHeader *message)
3150 struct GNUNET_MESH_PeerControl *peer_msg;
3151 struct MeshPeerInfo *peer_info;
3152 struct MeshClient *c;
3153 struct MeshTunnel *t;
3154 MESH_TunnelNumber tid;
3156 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3157 /* Sanity check for client registration */
3158 if (NULL == (c = client_get (client)))
3161 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3165 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3166 /* Sanity check for message size */
3167 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3174 /* Tunnel exists? */
3175 tid = ntohl (peer_msg->tunnel_id);
3176 t = tunnel_get_by_local_id (c, tid);
3180 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3184 /* Does client own tunnel? */
3185 if (t->client->handle != client)
3188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3191 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3192 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
3193 peer_info = peer_info_get (&peer_msg->peer);
3195 tunnel_add_peer(t, peer_info);
3196 peer_info_connect(peer_info, t);
3198 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3204 * Handler for disconnection requests of peers in a tunnel
3206 * @param cls closure
3207 * @param client identification of the client
3208 * @param message the actual message (PeerControl)
3211 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3212 const struct GNUNET_MessageHeader *message)
3214 struct GNUNET_MESH_PeerControl *peer_msg;
3215 struct MeshClient *c;
3216 struct MeshTunnel *t;
3217 MESH_TunnelNumber tid;
3219 /* Sanity check for client registration */
3220 if (NULL == (c = client_get (client)))
3223 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3226 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3227 /* Sanity check for message size */
3228 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3231 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3235 /* Tunnel exists? */
3236 tid = ntohl (peer_msg->tunnel_id);
3237 t = tunnel_get_by_local_id (c, tid);
3241 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3245 /* Does client own tunnel? */
3246 if (t->client->handle != client)
3249 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3253 /* Ok, delete peer from tunnel */
3254 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3255 &peer_msg->peer.hashPubKey);
3257 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3263 * Handler for connection requests to new peers by type
3265 * @param cls closure
3266 * @param client identification of the client
3267 * @param message the actual message (ConnectPeerByType)
3270 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3271 const struct GNUNET_MessageHeader *message)
3273 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3274 struct MeshClient *c;
3275 struct MeshTunnel *t;
3276 GNUNET_HashCode hash;
3277 MESH_TunnelNumber tid;
3279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3280 /* Sanity check for client registration */
3281 if (NULL == (c = client_get (client)))
3284 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3288 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3289 /* Sanity check for message size */
3290 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3291 ntohs (connect_msg->header.size))
3294 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3298 /* Tunnel exists? */
3299 tid = ntohl (connect_msg->tunnel_id);
3300 t = tunnel_get_by_local_id (c, tid);
3304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3308 /* Does client own tunnel? */
3309 if (t->client->handle != client)
3312 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3316 /* Do WE have the service? */
3317 t->type = ntohl (connect_msg->type);
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3319 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3320 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3323 /* Yes! Fast forward, add ourselves to the tunnel and send the
3324 * good news to the client
3326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3327 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3328 peer_info_get (&my_full_id),
3329 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3332 send_client_peer_connected(t, myid);
3333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3334 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3337 /* Ok, lets find a peer offering the service */
3338 if (NULL != t->dht_get_type)
3340 GNUNET_DHT_get_stop (t->dht_get_type);
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3343 GNUNET_h2s_full (&hash));
3345 GNUNET_DHT_get_start (dht_handle,
3346 GNUNET_TIME_UNIT_FOREVER_REL,
3347 GNUNET_BLOCK_TYPE_TEST,
3350 GNUNET_DHT_RO_RECORD_ROUTE |
3351 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3353 &dht_get_type_handler, t);
3355 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3361 * Handler for client traffic directed to one peer
3363 * @param cls closure
3364 * @param client identification of the client
3365 * @param message the actual message
3368 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3369 const struct GNUNET_MessageHeader *message)
3371 struct MeshClient *c;
3372 struct MeshTunnel *t;
3373 struct MeshPeerInfo *pi;
3374 struct GNUNET_MESH_Unicast *data_msg;
3375 MESH_TunnelNumber tid;
3378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3379 "MESH: Got a unicast request from a client!\n");
3381 /* Sanity check for client registration */
3382 if (NULL == (c = client_get (client)))
3385 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3388 data_msg = (struct GNUNET_MESH_Unicast *) message;
3389 /* Sanity check for message size */
3390 size = ntohs (message->size);
3391 if (sizeof (struct GNUNET_MESH_Unicast) +
3392 sizeof (struct GNUNET_MessageHeader) > size)
3395 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3399 /* Tunnel exists? */
3400 tid = ntohl (data_msg->tid);
3401 t = tunnel_get_by_local_id (c, tid);
3405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3409 /* Is it a local tunnel? Then, does client own the tunnel? */
3410 if (NULL != t->client &&
3411 NULL != t->client->handle &&
3412 t->client->handle != client)
3415 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3419 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3420 &data_msg->destination.hashPubKey);
3421 /* Is the selected peer in the tunnel? */
3425 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3429 /* Ok, everything is correct, send the message
3430 * (pretend we got it from a mesh peer)
3433 char buf[ntohs (message->size)];
3434 struct GNUNET_MESH_Unicast *copy;
3436 /* Work around const limitation */
3437 copy = (struct GNUNET_MESH_Unicast *) buf;
3438 memcpy (buf, data_msg, size);
3439 copy->oid = my_full_id;
3440 copy->tid = htonl (t->id.tid);
3441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3442 "MESH: calling generic handler...\n");
3443 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL);
3445 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3451 * Handler for client traffic directed to the origin
3453 * @param cls closure
3454 * @param client identification of the client
3455 * @param message the actual message
3458 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3459 const struct GNUNET_MessageHeader *message)
3461 struct GNUNET_MESH_ToOrigin *data_msg;
3462 struct GNUNET_PeerIdentity id;
3463 struct MeshClient *c;
3464 struct MeshTunnel *t;
3465 MESH_TunnelNumber tid;
3468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3469 "MESH: Got a ToOrigin request from a client!\n");
3471 /* Sanity check for client registration */
3472 if (NULL == (c = client_get (client)))
3475 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3478 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3479 /* Sanity check for message size */
3480 size = ntohs (message->size);
3481 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3482 sizeof (struct GNUNET_MessageHeader) > size)
3485 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3489 /* Tunnel exists? */
3490 tid = ntohl (data_msg->tid);
3491 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3494 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3497 t = tunnel_get_by_local_id (c, tid);
3501 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3505 /* It shouldn't be a local tunnel. */
3506 if (NULL != t->client)
3509 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3512 GNUNET_PEER_resolve(t->id.oid, &id);
3514 /* Ok, everything is correct, send the message
3515 * (pretend we got it from a mesh peer)
3518 char buf[ntohs (message->size)];
3519 struct GNUNET_MESH_ToOrigin *copy;
3521 /* Work around const limitation */
3522 copy = (struct GNUNET_MESH_ToOrigin *) buf;
3523 memcpy (buf, data_msg, size);
3525 copy->tid = htonl (t->id.tid);
3526 copy->sender = my_full_id;
3527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3528 "MESH: calling generic handler...\n");
3529 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL);
3531 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3537 * Handler for client traffic directed to all peers in a tunnel
3539 * @param cls closure
3540 * @param client identification of the client
3541 * @param message the actual message
3544 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3545 const struct GNUNET_MessageHeader *message)
3547 struct MeshClient *c;
3548 struct MeshTunnel *t;
3549 struct GNUNET_MESH_Multicast *data_msg;
3550 MESH_TunnelNumber tid;
3552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3553 "MESH: Got a multicast request from a client!\n");
3555 /* Sanity check for client registration */
3556 if (NULL == (c = client_get (client)))
3559 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3562 data_msg = (struct GNUNET_MESH_Multicast *) message;
3563 /* Sanity check for message size */
3564 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (data_msg->header.size))
3567 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3571 /* Tunnel exists? */
3572 tid = ntohl (data_msg->tid);
3573 t = tunnel_get_by_local_id (c, tid);
3577 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3581 /* Does client own tunnel? */
3582 if (t->client->handle != client)
3585 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3590 char buf[ntohs(message->size)];
3591 struct GNUNET_MESH_Multicast *copy;
3593 copy = (struct GNUNET_MESH_Multicast *)buf;
3594 memcpy(buf, message, ntohs(message->size));
3595 copy->oid = my_full_id;
3596 copy->tid = htonl(t->id.tid);
3597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3598 "MESH: calling generic handler...\n");
3599 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL);
3602 /* receive done gets called when last copy is sent to a neighbor */
3607 * Functions to handle messages from clients
3609 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3610 {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3611 {&handle_local_tunnel_create, NULL,
3612 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3613 sizeof (struct GNUNET_MESH_TunnelMessage)},
3614 {&handle_local_tunnel_destroy, NULL,
3615 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3616 sizeof (struct GNUNET_MESH_TunnelMessage)},
3617 {&handle_local_connect_add, NULL,
3618 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3619 sizeof (struct GNUNET_MESH_PeerControl)},
3620 {&handle_local_connect_del, NULL,
3621 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3622 sizeof (struct GNUNET_MESH_PeerControl)},
3623 {&handle_local_connect_by_type, NULL,
3624 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3625 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3626 {&handle_local_unicast, NULL,
3627 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3628 {&handle_local_to_origin, NULL,
3629 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3630 {&handle_local_multicast, NULL,
3631 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3637 * To be called on core init/fail.
3639 * @param cls service closure
3640 * @param server handle to the server for this service
3641 * @param identity the public identity of this peer
3644 core_init (void *cls, struct GNUNET_CORE_Handle *server,
3645 const struct GNUNET_PeerIdentity *identity)
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
3648 core_handle = server;
3649 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
3651 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
3652 GNUNET_SCHEDULER_shutdown();
3658 * Method called whenever a given peer connects.
3660 * @param cls closure
3661 * @param peer peer identity this notification is about
3662 * @param atsi performance data for the connection
3665 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
3666 const struct GNUNET_ATS_Information *atsi)
3668 struct MeshPeerInfo *peer_info;
3669 struct MeshPeerPath *path;
3671 #if MESH_DEBUG_CONNECTION
3672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
3673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3674 GNUNET_h2s(&my_full_id.hashPubKey));
3676 peer_info = peer_info_get (peer);
3677 if (myid == peer_info->id)
3679 #if MESH_DEBUG_CONNECTION
3680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3684 #if MESH_DEBUG_CONNECTION
3687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
3688 GNUNET_h2s(&peer->hashPubKey));
3691 path = path_new (2);
3692 path->peers[0] = myid;
3693 path->peers[1] = peer_info->id;
3694 GNUNET_PEER_change_rc(myid, 1);
3695 GNUNET_PEER_change_rc(peer_info->id, 1);
3696 path_add_to_peer (peer_info, path);
3701 * Method called whenever a peer disconnects.
3703 * @param cls closure
3704 * @param peer peer identity this notification is about
3707 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
3709 struct MeshPeerInfo *pi;
3712 #if MESH_DEBUG_CONNECTION
3713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
3715 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3721 for (i = 0; i < CORE_QUEUE_SIZE; i++)
3723 peer_info_cancel_transmission(pi, i);
3725 path_remove_from_peer (pi, pi->id, myid);
3726 #if MESH_DEBUG_CONNECTION
3729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
3736 /******************************************************************************/
3737 /************************ MAIN FUNCTIONS ****************************/
3738 /******************************************************************************/
3741 * Iterator over hash map entries.
3743 * @param cls closure
3744 * @param key current key code
3745 * @param value value in the hash map
3746 * @return GNUNET_YES if we should continue to
3751 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
3753 struct MeshTunnel *t = value;
3759 * Task run during shutdown.
3765 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
3768 /* TODO: destroy tunnels? */
3769 if (core_handle != NULL)
3771 GNUNET_CORE_disconnect (core_handle);
3774 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
3775 if (dht_handle != NULL)
3777 GNUNET_DHT_disconnect (dht_handle);
3782 GNUNET_SERVER_notification_context_destroy (nc);
3785 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
3787 GNUNET_SCHEDULER_cancel (announce_id_task);
3788 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
3790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
3794 * Process mesh requests.
3796 * @param cls closure
3797 * @param server the initialized server
3798 * @param c configuration to use
3801 run (void *cls, struct GNUNET_SERVER_Handle *server,
3802 const struct GNUNET_CONFIGURATION_Handle *c)
3804 struct MeshPeerInfo *peer;
3805 struct MeshPeerPath *p;
3808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
3809 server_handle = server;
3810 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
3811 CORE_QUEUE_SIZE, /* queue size */
3812 NULL, /* Closure passed to MESH functions */
3813 &core_init, /* Call core_init once connected */
3814 &core_connect, /* Handle connects */
3815 &core_disconnect, /* remove peers on disconnects */
3816 NULL, /* Don't notify about all incoming messages */
3817 GNUNET_NO, /* For header only in notification */
3818 NULL, /* Don't notify about all outbound messages */
3819 GNUNET_NO, /* For header-only out notification */
3820 core_handlers); /* Register these handlers */
3821 if (core_handle == NULL)
3824 GNUNET_SCHEDULER_shutdown ();
3829 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
3832 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3834 ("Mesh service is lacking key configuration settings. Exiting.\n"));
3835 GNUNET_SCHEDULER_shutdown ();
3838 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3839 GNUNET_free (keyfile);
3840 if (my_private_key == NULL)
3842 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3843 _("Mesh service could not access hostkey. Exiting.\n"));
3844 GNUNET_SCHEDULER_shutdown ();
3847 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3848 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
3849 &my_full_id.hashPubKey);
3850 myid = GNUNET_PEER_intern (&my_full_id);
3852 dht_handle = GNUNET_DHT_connect (c, 64);
3853 if (dht_handle == NULL)
3859 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3861 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3862 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3863 peers = GNUNET_CONTAINER_multihashmap_create (32);
3864 applications = GNUNET_CONTAINER_multihashmap_create (32);
3865 types = GNUNET_CONTAINER_multihashmap_create (32);
3867 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
3868 nc = GNUNET_SERVER_notification_context_create (server_handle,
3870 GNUNET_SERVER_disconnect_notify (server_handle,
3871 &handle_local_client_disconnect,
3876 clients_tail = NULL;
3881 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3882 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
3884 /* Create a peer_info for the local peer */
3885 peer = peer_info_get(&my_full_id);
3888 path_add_to_peer(peer, p);
3890 /* Scheduled the task to clean up when shutdown is called */
3891 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
3898 * The main function for the mesh service.
3900 * @param argc number of arguments from the command line
3901 * @param argv command line arguments
3902 * @return 0 ok, 1 on error
3905 main (int argc, char *const *argv)
3910 // fprintf (stderr, "main ()\n");
3912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
3915 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
3917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");
3919 // fprintf (stderr, "main () END\n");