2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - relay corking down to core
44 * - set ttl relative to tree depth
49 #include "mesh_protocol.h"
50 #include "gnunet_dht_service.h"
51 #include "mesh_tunnel_tree.h"
53 /* TODO: move into configuration file */
54 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
55 GNUNET_TIME_UNIT_SECONDS,\
57 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
58 GNUNET_TIME_UNIT_SECONDS,\
61 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
62 GNUNET_TIME_UNIT_SECONDS,\
65 #define GET_RESTART_TIME GNUNET_TIME_relative_multiply(\
66 GNUNET_TIME_UNIT_SECONDS,\
69 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
70 GNUNET_TIME_UNIT_SECONDS,\
72 #define DEFAULT_TTL 64
74 #define MESH_DEBUG_DHT GNUNET_NO
78 /******************************************************************************/
79 /************************ DATA STRUCTURES ****************************/
80 /******************************************************************************/
82 /** FWD declaration */
86 * Struct containing all info possibly needed to build a package when called
89 struct MeshDataDescriptor
91 /** ID of the tunnel this packet travels in */
92 struct MESH_TunnelID *origin;
97 /** Client that asked for the transmission, if any */
98 struct GNUNET_SERVER_Client *client;
100 /** Who was this message being sent to */
101 struct MeshPeerInfo *peer;
103 /** Ultimate destination of the packet */
104 GNUNET_PEER_Id destination;
106 /** Number of identical messages sent to different hops (multicast) */
107 unsigned int *copies;
109 /** Which handler was used to request the transmission */
110 unsigned int handler_n;
112 /** Size of the data */
115 /** Used to allow a client send more traffic to the service after a
116 * previous packet was tried to be sent to a neighbor and couldn't */
117 GNUNET_SCHEDULER_TaskIdentifier *timeout_task;
122 * Struct containing all information regarding a given peer
132 * Last time we heard from this peer
134 struct GNUNET_TIME_Absolute last_contact;
137 * Number of attempts to reconnect so far
139 int n_reconnect_attempts;
142 * Paths to reach the peer, ordered by ascending hop count
144 struct MeshPeerPath *path_head;
147 * Paths to reach the peer, ordered by ascending hop count
149 struct MeshPeerPath *path_tail;
152 * Handle to stop the DHT search for a path to this peer
154 struct GNUNET_DHT_GetHandle *dhtget;
157 * Closure given to the DHT GET
159 struct MeshPathInfo *dhtgetcls;
162 * Handles to stop queued transmissions for this peer
164 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
167 * Pointer to info stuctures used as cls for queued transmissions
169 void *infos[CORE_QUEUE_SIZE];
172 * Type of message being in each transmission
174 uint16_t types[CORE_QUEUE_SIZE];
177 * Array of tunnels this peer participates in
178 * (most probably a small amount, therefore not a hashmap)
179 * When the path to the peer changes, notify these tunnels to let them
180 * re-adjust their path trees.
182 struct MeshTunnel **tunnels;
185 * Number of tunnels this peers participates in
187 unsigned int ntunnels;
192 * Data scheduled to transmit (to local client or remote peer)
199 struct MeshQueue *next;
200 struct MeshQueue *prev;
203 * Target of the data (NULL if target is client)
205 struct MeshPeerInfo *peer;
208 * Client to send the data to (NULL if target is peer)
210 struct MeshClient *client;
213 * Size of the message to transmit
218 * How old is the data?
220 struct GNUNET_TIME_Absolute timestamp;
225 struct GNUNET_MessageHeader *data;
229 * Globally unique tunnel identification (owner + number)
230 * DO NOT USE OVER THE NETWORK
235 * Node that owns the tunnel
240 * Tunnel number to differentiate all the tunnels owned by the node oid
241 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
243 MESH_TunnelNumber tid;
247 struct MeshClient; /* FWD declaration */
250 * Struct containing all information regarding a tunnel
251 * For an intermediate node the improtant info used will be:
252 * - id Tunnel unique identification
253 * - paths[0] To know where to send it next
254 * - metainfo: ready, speeds, accounting
261 struct MESH_TunnelID id;
264 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
266 MESH_TunnelNumber local_tid;
269 * ID of the last multicast packet seen/sent.
274 * Last time the tunnel was used
276 struct GNUNET_TIME_Absolute timestamp;
279 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
280 * containing peers added by id or by type, not intermediate peers.
282 struct GNUNET_CONTAINER_MultiHashMap *peers;
285 * Number of peers that are connected and potentially ready to receive data
287 unsigned int peers_ready;
290 * Number of peers that have been added to the tunnel
292 unsigned int peers_total;
295 * Client owner of the tunnel, if any
297 struct MeshClient *client;
300 * Messages ready to transmit
302 struct MeshQueue *queue_head;
303 struct MeshQueue *queue_tail;
308 struct MeshTunnelTree *tree;
311 * Application type we are looking for in this tunnel
313 GNUNET_MESH_ApplicationType type;
316 * Used to search peers offering a service
318 struct GNUNET_DHT_GetHandle *dht_get_type;
321 * Task to keep the used paths alive
323 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
326 * Task to destroy the tunnel after timeout
328 * FIXME: merge the two? a tunnel will have either
329 * a path refresh OR a timeout, never both!
331 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
336 * Info needed to work with tunnel paths and peers
343 struct MeshTunnel *t;
346 * Neighbouring peer to whom we send the packet to
348 struct MeshPeerInfo *peer;
353 struct MeshPeerPath *path;
356 * Position in peer's transmit queue
363 * Struct containing information about a client of the service
370 struct MeshClient *next;
371 struct MeshClient *prev;
374 * Tunnels that belong to this client, indexed by local id
376 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
379 * Handle to communicate with the client
381 struct GNUNET_SERVER_Client *handle;
384 * Applications that this client has claimed to provide
386 struct GNUNET_CONTAINER_MultiHashMap *apps;
389 * Messages that this client has declared interest in
391 struct GNUNET_CONTAINER_MultiHashMap *types;
395 * ID of the client, for debug messages
404 /******************************************************************************/
405 /************************ DEBUG FUNCTIONS ****************************/
406 /******************************************************************************/
410 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
411 * @param cls string to print
412 * @param tc task context
415 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
419 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
427 /******************************************************************************/
428 /*********************** GLOBAL VARIABLES ****************************/
429 /******************************************************************************/
434 static struct MeshClient *clients;
435 static struct MeshClient *clients_tail;
438 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
440 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
443 * Tunnels incoming, indexed by MESH_TunnelNumber
444 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
446 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
449 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
451 static struct GNUNET_CONTAINER_MultiHashMap *peers;
454 * Handle to communicate with core
456 static struct GNUNET_CORE_Handle *core_handle;
459 * Handle to communicate with transport
461 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
466 static struct GNUNET_DHT_Handle *dht_handle;
471 static struct GNUNET_SERVER_Handle *server_handle;
474 * Notification context, to send messages to local clients
476 static struct GNUNET_SERVER_NotificationContext *nc;
479 * Local peer own ID (memory efficient handle)
481 static GNUNET_PEER_Id myid;
484 * Local peer own ID (full value)
486 static struct GNUNET_PeerIdentity my_full_id;
491 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
496 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
499 * Tunnel ID for the next created tunnel (global tunnel number)
501 static MESH_TunnelNumber next_tid;
504 * Tunnel ID for the next incoming tunnel (local tunnel number)
506 static MESH_TunnelNumber next_local_tid;
509 * All application types provided by this peer
511 static struct GNUNET_CONTAINER_MultiHashMap *applications;
514 * All message types clients of this peer are interested in
516 static struct GNUNET_CONTAINER_MultiHashMap *types;
519 * Task to periodically announce provided applications
521 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
524 * Task to periodically announce itself in the network
526 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
529 unsigned int next_client_id;
533 /******************************************************************************/
534 /************************ ITERATORS ****************************/
535 /******************************************************************************/
537 /* FIXME move iterators here */
540 /******************************************************************************/
541 /************************ PERIODIC FUNCTIONS ****************************/
542 /******************************************************************************/
545 * Announce iterator over for each application provided by the peer
548 * @param key current key code
549 * @param value value in the hash map
550 * @return GNUNET_YES if we should continue to
555 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
557 /* FIXME are hashes in multihash map equal on all aquitectures? */
558 GNUNET_DHT_put (dht_handle, key, 10U,
559 GNUNET_DHT_RO_RECORD_ROUTE |
560 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
561 sizeof (struct GNUNET_PeerIdentity),
562 (const char *) &my_full_id,
564 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
565 &mesh_debug, "DHT_put for app completed");
567 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
569 APP_ANNOUNCE_TIME, NULL, NULL);
576 * Periodically announce what applications are provided by local clients
579 * @param tc task context
582 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
584 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
586 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
592 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
594 announce_applications_task =
595 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
605 * Periodically announce self id in the DHT
608 * @param tc task context
611 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
613 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
615 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
619 * - Set data expiration in function of X
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
624 GNUNET_i2s (&my_full_id));
626 GNUNET_DHT_put (dht_handle, /* DHT handle */
627 &my_full_id.hashPubKey, /* Key to use */
628 10U, /* Replication level */
629 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
630 GNUNET_BLOCK_TYPE_TEST, /* Block type */
631 sizeof (my_full_id), /* Size of the data */
632 (char *) &my_full_id, /* Data itself */
633 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
634 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
636 &mesh_debug, "DHT_put for id completed");
638 NULL, /* Continuation */
639 NULL); /* Continuation closure */
642 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
647 * Function to process paths received for a new peer addition. The recorded
648 * paths form the initial tunnel, which can be optimized later.
649 * Called on each result obtained for the DHT search.
652 * @param exp when will this value expire
653 * @param key key of the result
654 * @param type type of the result
655 * @param size number of bytes in data
656 * @param data pointer to the result data
659 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
660 const GNUNET_HashCode * key,
661 const struct GNUNET_PeerIdentity *get_path,
662 unsigned int get_path_length,
663 const struct GNUNET_PeerIdentity *put_path,
664 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
665 size_t size, const void *data);
668 /******************************************************************************/
669 /****************** GENERAL HELPER FUNCTIONS ************************/
670 /******************************************************************************/
673 * Check if client has registered with the service and has not disconnected
675 * @param client the client to check
677 * @return non-NULL if client exists in the global DLL
679 static struct MeshClient *
680 client_get (struct GNUNET_SERVER_Client *client)
682 struct MeshClient *c;
687 if (c->handle == client)
696 * Checks if a given client has subscribed to certain message type
698 * @param message_type Type of message to check
699 * @param c Client to check
701 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
706 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
710 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
711 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
716 * Allow a client to send more data after transmitting a multicast message
717 * which some neighbor has not yet accepted altough a reasonable time has
720 * @param cls Closure (DataDescriptor containing the task identifier)
721 * @param tc Task Context
724 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
726 struct MeshDataDescriptor *info = cls;
728 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "MESH: CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
735 *(info->timeout_task) = GNUNET_SCHEDULER_NO_TASK;
736 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
741 * Search for a tunnel by global ID using full PeerIdentities
743 * @param oid owner of the tunnel
744 * @param tid global tunnel number
746 * @return tunnel handler, NULL if doesn't exist
748 static struct MeshTunnel *
749 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
753 * Notify a tunnel that a connection has broken that affects at least
756 * @param t Tunnel affected.
757 * @param p1 Peer that got disconnected from p2.
758 * @param p2 Peer that got disconnected from p1.
760 * @return Short ID of the peer disconnected (either p1 or p2).
761 * 0 if the tunnel remained unaffected.
763 static GNUNET_PEER_Id
764 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
769 * Send the message to all clients that have subscribed to its type
771 * @param msg Pointer to the message itself
772 * @param payload Pointer to the payload of the message.
773 * @return number of clients this message was sent to
776 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
777 const struct GNUNET_MessageHeader *payload)
779 struct GNUNET_PeerIdentity *oid;
780 struct MeshClient *c;
781 struct MeshTunnel *t;
782 MESH_TunnelNumber *tid;
785 char cbuf[htons (msg->size)];
787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
788 type = ntohs (payload->type);
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
791 memcpy (cbuf, msg, sizeof (cbuf));
792 switch (htons (msg->type))
794 struct GNUNET_MESH_Unicast *uc;
795 struct GNUNET_MESH_Multicast *mc;
796 struct GNUNET_MESH_ToOrigin *to;
798 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
799 uc = (struct GNUNET_MESH_Unicast *) cbuf;
803 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
804 mc = (struct GNUNET_MESH_Multicast *) cbuf;
808 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
809 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
817 t = tunnel_get (oid, ntohl (*tid));
823 *tid = htonl (t->local_tid);
824 for (count = 0, c = clients; c != NULL; c = c->next)
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
829 if (client_is_subscribed (type, c))
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
833 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
834 (struct GNUNET_MessageHeader
835 *) cbuf, GNUNET_YES);
843 * Notify the client that owns the tunnel that a peer has connected to it
844 * (the requested path to it has been confirmed).
846 * @param t Tunnel whose owner to notify
847 * @param id Short id of the peer that has connected
850 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
852 struct GNUNET_MESH_PeerControl pc;
854 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
855 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
856 pc.tunnel_id = htonl (t->local_tid);
857 GNUNET_PEER_resolve (id, &pc.peer);
858 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, &pc.header,
864 * Notify all clients (not depending on registration status) that the incoming
865 * tunnel is no longer valid.
867 * @param t Tunnel that was destroyed.
870 send_clients_tunnel_destroy (struct MeshTunnel *t)
872 struct GNUNET_MESH_TunnelMessage msg;
874 msg.header.size = htons (sizeof (msg));
875 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
876 msg.tunnel_id = htonl (t->local_tid);
877 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
882 * Function called to notify a client about the socket
883 * being ready to queue more data. "buf" will be
884 * NULL and "size" zero if the socket was closed for
885 * writing in the meantime.
888 * @param size number of bytes available in buf
889 * @param buf where the callee should write the message
890 * @return number of bytes written to buf
893 send_core_create_path (void *cls, size_t size, void *buf);
897 * Function called to notify a client about the socket
898 * being ready to queue more data. "buf" will be
899 * NULL and "size" zero if the socket was closed for
900 * writing in the meantime.
902 * @param cls closure (data itself)
903 * @param size number of bytes available in buf
904 * @param buf where the callee should write the message
906 * @return number of bytes written to buf
909 send_core_data_multicast (void *cls, size_t size, void *buf);
913 * Decrements the reference counter and frees all resources if needed
915 * @param dd Data Descriptor used in a multicast message
918 data_descriptor_decrement_multicast (struct MeshDataDescriptor *dd)
920 if (0 == --(*(dd->copies)))
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
923 if (NULL != dd->client)
925 if (GNUNET_SCHEDULER_NO_TASK != *(dd->timeout_task))
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
928 "MESH: cancelling client timeout (%u)...\n",
929 *(dd->timeout_task));
930 GNUNET_SCHEDULER_cancel (*(dd->timeout_task));
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
932 GNUNET_SERVER_receive_done (dd->client, GNUNET_OK);
934 GNUNET_free (dd->timeout_task);
936 GNUNET_free (dd->copies);
937 GNUNET_free (dd->data);
943 * Cancel a core transmission that was already requested and free all resources
944 * associated to the request.
946 * @param peer PeeInfo of the peer whose transmission is cancelled.
947 * @param i Position of the transmission to be cancelled.
950 peer_info_cancel_transmission (struct MeshPeerInfo *peer, unsigned int i)
952 if (NULL != peer->core_transmit[i])
954 struct MeshDataDescriptor *dd;
955 struct MeshPathInfo *path_info;
959 struct GNUNET_PeerIdentity id;
961 GNUNET_PEER_resolve (peer->id, &id);
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
963 "MESH: Cancelling data transmission at %s [%u]\n",
964 GNUNET_i2s (&id), i);
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message type %u\n",
969 /* TODO: notify that tranmission has failed */
970 switch (peer->types[i])
972 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
973 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
974 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
977 data_descriptor_decrement_multicast (dd);
979 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
981 path_info = peer->infos[i];
982 path_destroy (path_info->path);
986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
988 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
989 peer->core_transmit[i] = NULL;
990 GNUNET_free (peer->infos[i]);
996 * Get a unused CORE slot to transmit a message to a peer. If all the slots
997 * are used, cancel one and return it's position.
999 * @param peer PeerInfo of the neighbor we want to transmit to.
1001 * @return The index of an available slot to transmit to the neighbor.
1004 peer_info_transmit_slot (struct MeshPeerInfo *peer)
1008 for (i = 0; peer->core_transmit[i]; i++)
1010 if (i == (CORE_QUEUE_SIZE - 1))
1012 /* All positions are taken! Overwriting! */
1014 peer_info_cancel_transmission (peer, 0);
1023 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1024 * and insert it in the appropiate structures if the peer is not known yet.
1026 * @param peer Full identity of the peer.
1028 * @return Existing or newly created peer info.
1030 static struct MeshPeerInfo *
1031 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1033 struct MeshPeerInfo *peer_info;
1035 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1036 if (NULL == peer_info)
1039 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1040 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1041 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1042 peer_info->id = GNUNET_PEER_intern (peer);
1050 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1051 * and insert it in the appropiate structures if the peer is not known yet.
1053 * @param peer Short identity of the peer.
1055 * @return Existing or newly created peer info.
1057 static struct MeshPeerInfo *
1058 peer_info_get_short (const GNUNET_PEER_Id peer)
1060 struct GNUNET_PeerIdentity id;
1062 GNUNET_PEER_resolve (peer, &id);
1063 return peer_info_get (&id);
1068 * Iterator to remove the tunnel from the list of tunnels a peer participates
1071 * @param cls Closure (tunnel info)
1072 * @param key GNUNET_PeerIdentity of the peer (unused)
1073 * @param value PeerInfo of the peer
1075 * @return always GNUNET_YES, to keep iterating
1078 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1080 struct MeshTunnel *t = cls;
1081 struct MeshPeerInfo *peer = value;
1084 for (i = 0; i < peer->ntunnels; i++)
1087 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1090 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1091 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1100 * Core callback to write a
1102 * @param cls Closure (MeshDataDescriptor with data in "data" member).
1103 * @param size Number of bytes available in buf.
1104 * @param buf Where the to write the message.
1106 * @return number of bytes written to buf
1109 send_core_data_raw (void *cls, size_t size, void *buf)
1111 struct MeshDataDescriptor *info = cls;
1112 struct GNUNET_MessageHeader *msg;
1115 GNUNET_assert (NULL != info);
1116 GNUNET_assert (NULL != info->data);
1117 msg = (struct GNUNET_MessageHeader *) info->data;
1118 total_size = ntohs (msg->size);
1120 if (total_size > size)
1122 struct GNUNET_PeerIdentity id;
1124 GNUNET_PEER_resolve (info->peer->id, &id);
1125 info->peer->core_transmit[info->handler_n] =
1126 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1127 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1128 size, &send_core_data_raw, info);
1131 info->peer->core_transmit[info->handler_n] = NULL;
1132 memcpy (buf, msg, total_size);
1133 GNUNET_free (info->data);
1140 * Sends an already built message to a peer, properly registrating
1141 * all used resources.
1143 * @param message Message to send. Fucntion makes a copy of it.
1144 * @param peer Short ID of the neighbor whom to send the message.
1147 send_message (const struct GNUNET_MessageHeader *message,
1148 const struct GNUNET_PeerIdentity *peer)
1150 struct MeshDataDescriptor *info;
1151 struct MeshPeerInfo *neighbor;
1152 struct MeshPeerPath *p;
1156 // GNUNET_TRANSPORT_try_connect();
1158 size = ntohs (message->size);
1159 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1160 info->data = GNUNET_malloc (size);
1161 memcpy (info->data, message, size);
1162 neighbor = peer_info_get (peer);
1163 for (p = neighbor->path_head; NULL != p; p = p->next)
1176 i = peer_info_transmit_slot (neighbor);
1177 info->handler_n = i;
1178 info->peer = neighbor;
1179 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1180 neighbor->infos[i] = info;
1181 neighbor->core_transmit[i] =
1182 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1183 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1184 size, &send_core_data_raw, info);
1190 * Sends a CREATE PATH message for a path to a peer, properly registrating
1191 * all used resources.
1193 * @param peer PeerInfo of the final peer for whom this path is being created.
1194 * @param p Path itself.
1195 * @param t Tunnel for which the path is created.
1198 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1199 struct MeshTunnel *t)
1201 struct GNUNET_PeerIdentity id;
1202 struct MeshPathInfo *path_info;
1203 struct MeshPeerInfo *neighbor;
1208 p = tree_get_path_to_peer (t->tree, peer->id);
1215 for (i = 0; i < p->length; i++)
1217 if (p->peers[i] == myid)
1220 if (i >= p->length - 1)
1226 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1228 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1229 path_info->path = p;
1231 neighbor = peer_info_get (&id);
1232 path_info->peer = neighbor;
1233 path_info->pos = peer_info_transmit_slot (neighbor);
1234 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1235 neighbor->infos[path_info->pos] = path_info;
1236 neighbor->core_transmit[path_info->pos] = GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
1239 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1241 sizeof (struct GNUNET_MESH_ManipulatePath) + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1242 &send_core_create_path, /* callback */
1243 path_info); /* cls */
1248 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1250 * @param t Tunnel whose path to destroy.
1251 * @param destination Short ID of the peer to whom the path to destroy.
1254 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1256 struct MeshPeerPath *p;
1259 p = tree_get_path_to_peer (t->tree, destination);
1265 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1266 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1268 struct GNUNET_MESH_ManipulatePath *msg;
1269 struct GNUNET_PeerIdentity *pi;
1273 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1274 msg->header.size = htons (size);
1275 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1276 msg->tid = htonl (t->id.tid);
1277 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1278 for (i = 0; i < p->length; i++)
1280 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1282 send_message (&msg->header, tree_get_first_hop (t->tree, destination));
1289 * Try to establish a new connection to this peer.
1290 * Use the best path for the given tunnel.
1291 * If the peer doesn't have any path to it yet, try to get one.
1292 * If the peer already has some path, send a CREATE PATH towards it.
1294 * @param peer PeerInfo of the peer.
1295 * @param t Tunnel for which to create the path, if possible.
1298 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1300 struct MeshPeerPath *p;
1301 struct MeshPathInfo *path_info;
1303 if (NULL != peer->path_head)
1305 p = tree_get_path_to_peer (t->tree, peer->id);
1314 send_create_path (peer, p, t);
1319 send_client_peer_connected (t, myid);
1322 else if (NULL == peer->dhtget)
1324 struct GNUNET_PeerIdentity id;
1326 GNUNET_PEER_resolve (peer->id, &id);
1327 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1328 path_info->peer = peer;
1330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1331 "MESH: Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1332 peer->dhtgetcls = path_info;
1333 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1334 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1335 GNUNET_BLOCK_TYPE_TEST, /* type */
1336 &id.hashPubKey, /* key to search */
1337 4, /* replication level */
1338 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1339 0, /* xquery bits */
1340 &dht_get_id_handler, path_info);
1342 /* Otherwise, there is no path but the DHT get is already started. */
1347 * Task to delay the connection of a peer
1349 * @param cls Closure (path info with tunnel and peer to connect).
1350 * Will be free'd on exection.
1351 * @param tc TaskContext
1354 peer_info_connect_task (void *cls,
1355 const struct GNUNET_SCHEDULER_TaskContext *tc)
1357 struct MeshPathInfo *path_info = cls;
1359 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1364 peer_info_connect (path_info->peer, path_info->t);
1370 * Destroy the peer_info and free any allocated resources linked to it
1372 * @param pi The peer_info to destroy.
1374 * @return GNUNET_OK on success
1377 peer_info_destroy (struct MeshPeerInfo *pi)
1379 struct GNUNET_PeerIdentity id;
1380 struct MeshPeerPath *p;
1381 struct MeshPeerPath *nextp;
1384 GNUNET_PEER_resolve (pi->id, &id);
1385 GNUNET_PEER_change_rc (pi->id, -1);
1388 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1391 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1392 "MESH: removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1394 if (NULL != pi->dhtget)
1396 GNUNET_DHT_get_stop (pi->dhtget);
1397 GNUNET_free (pi->dhtgetcls);
1399 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1401 peer_info_cancel_transmission (pi, i);
1407 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1417 * Remove all paths that rely on a direct connection between p1 and p2
1418 * from the peer itself and notify all tunnels about it.
1420 * @param peer PeerInfo of affected peer.
1421 * @param p1 GNUNET_PEER_Id of one peer.
1422 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1425 * TODO: optimize (see below)
1428 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1431 struct MeshPeerPath *p;
1432 struct MeshPeerPath *aux;
1433 struct MeshPeerInfo *peer_d;
1435 unsigned int destroyed;
1441 p = peer->path_head;
1445 for (i = 0; i < (p->length - 1); i++)
1447 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1448 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1450 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1461 for (i = 0; i < peer->ntunnels; i++)
1463 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1467 * Problem: one or more peers have been deleted from the tunnel tree.
1468 * We don't know who they are to try to add them again.
1469 * We need to try to find a new path for each of the disconnected peers.
1470 * Some of them might already have a path to reach them that does not
1471 * involve p1 and p2. Adding all anew might render in a better tree than
1472 * the trivial immediate fix.
1474 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1475 * its children will be reachable trough him.
1477 peer_d = peer_info_get_short (d);
1480 for (p = peer_d->path_head; NULL != p; p = p->next)
1482 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1490 /* No callback, as peer will be already disconnected and a connection
1491 * scheduled by tunnel_notify_connection_broken.
1493 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1497 peer_info_connect (peer_d, peer->tunnels[i]);
1504 * Add the path to the peer and update the path used to reach it in case this
1507 * @param peer_info Destination peer to add the path to.
1508 * @param path New path to add. Last peer must be the peer in arg 1.
1509 * Path will be either used of freed if already known.
1510 * @param trusted Do we trust that this path is real?
1513 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1516 struct MeshPeerPath *aux;
1520 if ((NULL == peer_info) || (NULL == path))
1523 path_destroy (path);
1526 if (path->peers[path->length - 1] != peer_info->id)
1529 path_destroy (path);
1532 if (path->length <= 2 && GNUNET_NO == trusted)
1534 /* Only allow CORE to tell us about direct paths */
1535 path_destroy (path);
1538 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1539 for (l = 1; l < path->length; l++)
1541 if (path->peers[l] == myid)
1543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shortening path by %u\n", l);
1544 for (l2 = 0; l2 < path->length - l; l2++)
1546 path->peers[l2] = path->peers[l + l2];
1551 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1556 struct GNUNET_PeerIdentity id;
1558 GNUNET_PEER_resolve (peer_info->id, &id);
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: adding path [%u] to peer %s\n",
1560 path->length, GNUNET_i2s (&id));
1563 l = path_get_length (path);
1570 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1571 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1573 l2 = path_get_length (aux);
1576 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1577 peer_info->path_tail, aux, path);
1582 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1584 path_destroy (path);
1589 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1596 * Add the path to the origin peer and update the path used to reach it in case
1597 * this is the shortest.
1598 * The path is given in peer_info -> destination, therefore we turn the path
1599 * upside down first.
1601 * @param peer_info Peer to add the path to, being the origin of the path.
1602 * @param path New path to add after being inversed.
1603 * @param trusted Do we trust that this path is real?
1606 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1607 struct MeshPeerPath *path, int trusted)
1610 peer_info_add_path (peer_info, path, trusted);
1615 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1616 * to obtain a local peer -> destination path and interning the peer ids.
1618 * @return Newly allocated and created path
1620 static struct MeshPeerPath *
1621 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1622 unsigned int get_path_length,
1623 const struct GNUNET_PeerIdentity *put_path,
1624 unsigned int put_path_length)
1626 struct MeshPeerPath *p;
1632 GNUNET_PEER_change_rc (myid, 1);
1633 i = get_path_length;
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1635 for (i--; i >= 0; i--)
1637 id = GNUNET_PEER_intern (&get_path[i]);
1638 if (p->length > 0 && id == p->peers[p->length - 1])
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1641 GNUNET_PEER_change_rc (id, -1);
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Adding from GET: %s.\n",
1646 GNUNET_i2s (&get_path[i]));
1648 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1649 p->peers[p->length - 1] = id;
1652 i = put_path_length;
1653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1654 for (i--; i >= 0; i--)
1656 id = GNUNET_PEER_intern (&put_path[i]);
1659 /* PUT path went through us, so discard the path up until now and start
1660 * from here to get a much shorter (and loop-free) path.
1665 if (p->length > 0 && id == p->peers[p->length - 1])
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1668 GNUNET_PEER_change_rc (id, -1);
1672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Adding from PUT: %s.\n",
1673 GNUNET_i2s (&put_path[i]));
1675 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1676 p->peers[p->length - 1] = id;
1680 if (get_path_length > 0)
1681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (first of GET: %s)\n",
1682 GNUNET_i2s (&get_path[0]));
1683 if (put_path_length > 0)
1684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (first of PUT: %s)\n",
1685 GNUNET_i2s (&put_path[0]));
1686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: In total: %d hops\n",
1688 for (i = 0; i < p->length; i++)
1690 struct GNUNET_PeerIdentity peer_id;
1692 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %u: %s\n", p->peers[i],
1694 GNUNET_i2s (&peer_id));
1702 * Adds a path to the peer_infos of all the peers in the path
1704 * @param p Path to process.
1705 * @param confirmed Whether we know if the path works or not. FIXME use
1708 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1712 /* TODO: invert and add */
1713 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1714 for (i++; i < p->length; i++)
1716 struct MeshPeerInfo *aux;
1717 struct MeshPeerPath *copy;
1719 aux = peer_info_get_short (p->peers[i]);
1720 copy = path_duplicate (p);
1721 copy->length = i + 1;
1722 peer_info_add_path (aux, copy, GNUNET_NO);
1728 * Send keepalive packets for a peer
1730 * @param cls Closure (tunnel for which to send the keepalive).
1731 * @param tc Notification context.
1733 * TODO: implement explicit multicast keepalive?
1736 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1740 * Search for a tunnel among the incoming tunnels
1742 * @param tid the local id of the tunnel
1744 * @return tunnel handler, NULL if doesn't exist
1746 static struct MeshTunnel *
1747 tunnel_get_incoming (MESH_TunnelNumber tid)
1749 GNUNET_HashCode hash;
1751 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1752 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1753 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1758 * Search for a tunnel among the tunnels for a client
1760 * @param c the client whose tunnels to search in
1761 * @param tid the local id of the tunnel
1763 * @return tunnel handler, NULL if doesn't exist
1765 static struct MeshTunnel *
1766 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1768 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1770 return tunnel_get_incoming (tid);
1774 GNUNET_HashCode hash;
1776 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1777 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1783 * Search for a tunnel by global ID using PEER_ID
1785 * @param pi owner of the tunnel
1786 * @param tid global tunnel number
1788 * @return tunnel handler, NULL if doesn't exist
1790 static struct MeshTunnel *
1791 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1793 struct MESH_TunnelID id;
1794 GNUNET_HashCode hash;
1799 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1800 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1805 * Search for a tunnel by global ID using full PeerIdentities
1807 * @param oid owner of the tunnel
1808 * @param tid global tunnel number
1810 * @return tunnel handler, NULL if doesn't exist
1812 static struct MeshTunnel *
1813 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1815 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1820 * Callback used to notify a client owner of a tunnel that a peer has
1821 * disconnected, most likely because of a path change.
1823 * @param cls Closure (tunnel this notification is about).
1824 * @param peer_id Short ID of disconnected peer.
1827 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
1829 struct MeshTunnel *t = cls;
1830 struct MeshPeerInfo *peer;
1831 struct MeshPathInfo *path_info;
1833 if (NULL != t->client && NULL != nc)
1835 struct GNUNET_MESH_PeerControl msg;
1837 msg.header.size = htons (sizeof (msg));
1838 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1839 msg.tunnel_id = htonl (t->local_tid);
1840 GNUNET_PEER_resolve (peer_id, &msg.peer);
1841 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1842 &msg.header, GNUNET_NO);
1844 peer = peer_info_get_short (peer_id);
1845 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1846 path_info->peer = peer;
1848 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
1853 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1854 * needed rescources.
1855 * If peer already exists, reevaluate shortest path and change if different.
1857 * @param t Tunnel we want to add a new peer to
1858 * @param peer PeerInfo of the peer being added
1862 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1864 struct GNUNET_PeerIdentity id;
1865 struct MeshPeerPath *best_p;
1866 struct MeshPeerPath *p;
1867 unsigned int best_cost;
1870 GNUNET_PEER_resolve (peer->id, &id);
1872 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
1875 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1876 GNUNET_assert (GNUNET_OK ==
1877 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
1879 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1882 if (NULL != (p = peer->path_head))
1885 best_cost = tree_get_path_cost (t->tree, p);
1888 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
1895 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
1896 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1897 t->path_refresh_task =
1898 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
1902 /* Start a DHT get */
1903 peer_info_connect (peer, t);
1908 * Add a path to a tunnel which we don't own, just to remember the next hop.
1909 * If destination node was already in the tunnel, the first hop information
1910 * will be replaced with the new path.
1912 * @param t Tunnel we want to add a new peer to
1913 * @param p Path to add
1914 * @param own_pos Position of local node in path.
1918 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
1919 unsigned int own_pos)
1921 struct GNUNET_PeerIdentity id;
1923 GNUNET_assert (0 != own_pos);
1924 tree_add_path (t->tree, p, NULL, NULL);
1925 if (own_pos < p->length - 1)
1927 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1928 tree_update_first_hops (t->tree, myid, &id);
1934 * Notifies a tunnel that a connection has broken that affects at least
1935 * some of its peers. Sends a notification towards the root of the tree.
1936 * In case the peer is the owner of the tree, notifies the client that owns
1937 * the tunnel and tries to reconnect.
1939 * @param t Tunnel affected.
1940 * @param p1 Peer that got disconnected from p2.
1941 * @param p2 Peer that got disconnected from p1.
1943 * @return Short ID of the peer disconnected (either p1 or p2).
1944 * 0 if the tunnel remained unaffected.
1946 static GNUNET_PEER_Id
1947 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1953 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
1955 if (myid != p1 && myid != p2)
1961 if (tree_get_predecessor (t->tree) != 0)
1963 /* We are the peer still connected, notify owner of the disconnection. */
1964 struct GNUNET_MESH_PathBroken msg;
1965 struct GNUNET_PeerIdentity neighbor;
1967 msg.header.size = htons (sizeof (msg));
1968 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1969 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1970 msg.tid = htonl (t->id.tid);
1971 msg.peer1 = my_full_id;
1972 GNUNET_PEER_resolve (pid, &msg.peer2);
1973 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1974 send_message (&msg.header, &neighbor);
1981 struct MeshMulticastData
1983 struct MeshTunnel *t;
1985 GNUNET_SCHEDULER_TaskIdentifier *task;
1987 unsigned int *reference_counter;
1996 * Send a multicast packet to a neighbor.
1999 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2001 struct MeshMulticastData *mdata = cls;
2002 struct MeshDataDescriptor *info;
2003 struct GNUNET_PeerIdentity neighbor;
2006 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2008 info->data = mdata->data;
2009 info->size = mdata->data_len;
2010 info->copies = mdata->reference_counter;
2011 (*(mdata->reference_counter))++;
2013 if (NULL != mdata->t->client)
2015 info->client = mdata->t->client->handle;
2016 info->timeout_task = mdata->task;
2018 info->destination = neighbor_id;
2019 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending to %s...\n",
2022 GNUNET_i2s (&neighbor));
2024 info->peer = peer_info_get (&neighbor);
2025 GNUNET_assert (NULL != info->peer);
2026 i = peer_info_transmit_slot (info->peer);
2027 info->handler_n = i;
2028 info->peer->infos[i] = info;
2029 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2030 info->peer->core_transmit[i] =
2031 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2032 GNUNET_TIME_UNIT_FOREVER_REL,
2033 &neighbor, info->size,
2034 &send_core_data_multicast, info);
2038 * Send a message in a tunnel in multicast, sending a copy to each child node
2039 * down the local one in the tunnel tree.
2041 * @param t Tunnel in which to send the data.
2042 * @param msg Message to be sent
2045 tunnel_send_multicast (struct MeshTunnel *t,
2046 const struct GNUNET_MessageHeader *msg)
2048 struct MeshMulticastData *mdata;
2051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2052 "MESH: sending a multicast packet...\n");
2054 mdata = GNUNET_malloc (sizeof (struct MeshMulticastData));
2055 mdata->data_len = ntohs (msg->size);
2056 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2058 mdata->data = GNUNET_malloc (mdata->data_len);
2059 memcpy (mdata->data, msg, mdata->data_len);
2060 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2062 struct GNUNET_MESH_Multicast *mcast;
2064 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2065 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: data packet, ttl: %u\n",
2068 ntohl (mcast->ttl));
2072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: not a data packet, no ttl\n");
2075 if (NULL != t->client)
2077 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2079 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2083 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2084 if (*(mdata->reference_counter) == 0)
2086 GNUNET_free (mdata->data);
2087 GNUNET_free (mdata->reference_counter);
2088 if (NULL != mdata->task)
2090 GNUNET_free (mdata->task);
2093 GNUNET_free (mdata);
2095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2096 "MESH: sending a multicast packet done\n");
2103 * Send a message to all peers in this tunnel that the tunnel is no longer
2106 * @param t The tunnel whose peers to notify.
2109 tunnel_send_destroy (struct MeshTunnel *t)
2111 struct GNUNET_MESH_TunnelDestroy msg;
2113 msg.header.size = htons (sizeof (msg));
2114 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2115 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2116 msg.tid = htonl (t->id.tid);
2117 tunnel_send_multicast (t, &msg.header);
2123 * Destroy the tunnel and free any allocated resources linked to it
2125 * @param t the tunnel to destroy
2127 * @return GNUNET_OK on success
2130 tunnel_destroy (struct MeshTunnel *t)
2132 struct MeshClient *c;
2133 struct MeshQueue *q;
2134 struct MeshQueue *qn;
2135 GNUNET_HashCode hash;
2144 struct GNUNET_PeerIdentity id;
2146 GNUNET_PEER_resolve (t->id.oid, &id);
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel %s [%x]\n",
2148 GNUNET_i2s (&id), t->id.tid);
2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2154 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2155 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2160 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2162 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
2166 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2168 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2169 GNUNET_break (GNUNET_YES ==
2170 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash,
2173 if (NULL != t->peers)
2175 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2177 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2182 if (NULL != q->data)
2183 GNUNET_free (q->data);
2187 /* TODO cancel core transmit ready in case it was active */
2189 tree_destroy (t->tree);
2190 if (NULL != t->dht_get_type)
2191 GNUNET_DHT_get_stop (t->dht_get_type);
2192 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2193 GNUNET_SCHEDULER_cancel (t->timeout_task);
2194 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2195 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2202 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2203 * that are no longer needed, as well as nodes of no longer reachable peers.
2204 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2206 * @param t Tunnel from which to remove the path.
2207 * @param peer Short id of the peer which should be removed.
2210 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2212 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2218 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2219 * client when the client disconnects.
2221 * @param cls closure (client that is disconnecting)
2222 * @param key the hash of the local tunnel id (used to access the hashmap)
2223 * @param value the value stored at the key (tunnel to destroy)
2225 * @return GNUNET_OK on success
2228 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2230 struct MeshTunnel *t = value;
2233 r = tunnel_destroy (t);
2239 * Timeout function, destroys tunnel if called
2241 * @param cls Closure (tunnel to destroy).
2242 * @param tc TaskContext
2245 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2247 struct MeshTunnel *t = cls;
2249 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2251 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2256 * Resets the tunnel timeout. Starts it if no timeout was running.
2258 * @param t Tunnel whose timeout to reset.
2261 tunnel_reset_timeout (struct MeshTunnel *t)
2263 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2264 GNUNET_SCHEDULER_cancel (t->timeout_task);
2266 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2267 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2271 /******************************************************************************/
2272 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2273 /******************************************************************************/
2276 * Function called to notify a client about the socket
2277 * being ready to queue more data. "buf" will be
2278 * NULL and "size" zero if the socket was closed for
2279 * writing in the meantime.
2281 * @param cls closure
2282 * @param size number of bytes available in buf
2283 * @param buf where the callee should write the message
2284 * @return number of bytes written to buf
2287 send_core_create_path (void *cls, size_t size, void *buf)
2289 struct MeshPathInfo *info = cls;
2290 struct GNUNET_MESH_ManipulatePath *msg;
2291 struct GNUNET_PeerIdentity *peer_ptr;
2292 struct MeshPeerInfo *peer = info->peer;
2293 struct MeshTunnel *t = info->t;
2294 struct MeshPeerPath *p = info->path;
2298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATE PATH sending...\n");
2300 sizeof (struct GNUNET_MESH_ManipulatePath) +
2301 p->length * sizeof (struct GNUNET_PeerIdentity);
2303 if (size < size_needed || NULL == buf)
2305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n", size,
2309 info->peer->core_transmit[info->pos] =
2310 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2311 GNUNET_TIME_UNIT_FOREVER_REL,
2312 tree_get_first_hop (t->tree,
2314 size_needed, &send_core_create_path,
2318 info->peer->core_transmit[info->pos] = NULL;
2321 struct GNUNET_PeerIdentity id;
2323 GNUNET_PEER_resolve (peer->id, &id);
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 "MESH: setting core_transmit %s [%u] to NULL\n",
2326 GNUNET_i2s (&id), info->pos);
2329 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2330 msg->header.size = htons (size_needed);
2331 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2332 msg->tid = ntohl (t->id.tid);
2334 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2335 for (i = 0; i < p->length; i++)
2337 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2344 "MESH: CREATE PATH (%u bytes long) sent!\n", size_needed);
2350 * Function called to notify a client about the socket
2351 * being ready to queue more data. "buf" will be
2352 * NULL and "size" zero if the socket was closed for
2353 * writing in the meantime.
2355 * @param cls closure (data itself)
2356 * @param size number of bytes available in buf
2357 * @param buf where the callee should write the message
2359 * @return number of bytes written to buf
2362 send_core_data_multicast (void *cls, size_t size, void *buf)
2364 struct MeshDataDescriptor *info = cls;
2367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2368 GNUNET_assert (NULL != info);
2369 GNUNET_assert (NULL != info->peer);
2370 total_size = info->size;
2371 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2373 if (total_size > size)
2376 struct GNUNET_PeerIdentity id;
2378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2379 "MESH: Multicast: retransmitting... (%u/%u)\n", size,
2381 GNUNET_PEER_resolve (info->peer->id, &id);
2382 info->peer->core_transmit[info->handler_n] =
2383 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2384 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2386 &send_core_data_multicast, info);
2389 info->peer->core_transmit[info->handler_n] = NULL;
2390 info->peer->infos[info->handler_n] = NULL;
2391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2392 memcpy (buf, info->data, total_size);
2395 struct GNUNET_MESH_Multicast *mc;
2396 struct GNUNET_MessageHeader *mh;
2399 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2401 mc = (struct GNUNET_MESH_Multicast *) mh;
2402 mh = (struct GNUNET_MessageHeader *) &mc[1];
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2404 "MESH: multicast, payload type %u\n", ntohs (mh->type));
2405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2406 "MESH: multicast, payload size %u\n", ntohs (mh->size));
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type %u\n",
2415 data_descriptor_decrement_multicast (info);
2416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2424 * Function called to notify a client about the socket
2425 * being ready to queue more data. "buf" will be
2426 * NULL and "size" zero if the socket was closed for
2427 * writing in the meantime.
2429 * @param cls closure (MeshDataDescriptor)
2430 * @param size number of bytes available in buf
2431 * @param buf where the callee should write the message
2432 * @return number of bytes written to buf
2435 send_core_path_ack (void *cls, size_t size, void *buf)
2437 struct MeshDataDescriptor *info = cls;
2438 struct GNUNET_MESH_PathACK *msg = buf;
2440 GNUNET_assert (NULL != info);
2443 info->peer->core_transmit[info->handler_n] = NULL;
2445 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2450 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2451 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2452 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2453 msg->tid = htonl (info->origin->tid);
2454 msg->peer_id = my_full_id;
2456 /* TODO add signature */
2458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2459 return sizeof (struct GNUNET_MESH_PathACK);
2463 /******************************************************************************/
2464 /******************** MESH NETWORK HANDLERS **************************/
2465 /******************************************************************************/
2469 * Core handler for path creation
2471 * @param cls closure
2472 * @param message message
2473 * @param peer peer identity this notification is about
2474 * @param atsi performance data
2475 * @param atsi_count number of records in 'atsi'
2477 * @return GNUNET_OK to keep the connection open,
2478 * GNUNET_SYSERR to close it (signal serious error)
2481 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2482 const struct GNUNET_MessageHeader *message,
2483 const struct GNUNET_ATS_Information *atsi,
2484 unsigned int atsi_count)
2486 unsigned int own_pos;
2489 MESH_TunnelNumber tid;
2490 struct GNUNET_MESH_ManipulatePath *msg;
2491 struct GNUNET_PeerIdentity *pi;
2492 GNUNET_HashCode hash;
2493 struct MeshPeerPath *path;
2494 struct MeshPeerInfo *dest_peer_info;
2495 struct MeshPeerInfo *orig_peer_info;
2496 struct MeshTunnel *t;
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2499 "MESH: Received a path create msg [%s]\n",
2500 GNUNET_i2s (&my_full_id));
2501 size = ntohs (message->size);
2502 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2504 GNUNET_break_op (0);
2508 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2509 if (size % sizeof (struct GNUNET_PeerIdentity))
2511 GNUNET_break_op (0);
2514 size /= sizeof (struct GNUNET_PeerIdentity);
2517 GNUNET_break_op (0);
2520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
2521 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2523 tid = ntohl (msg->tid);
2524 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2525 t = tunnel_get (pi, tid);
2526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2527 "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2531 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2532 t->id.oid = GNUNET_PEER_intern (pi);
2534 while (NULL != tunnel_get_incoming (next_local_tid))
2535 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2536 t->local_tid = next_local_tid++;
2537 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2538 t->tree = tree_new (t->id.oid);
2540 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2542 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2543 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2549 tunnel_reset_timeout (t);
2550 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2552 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2553 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2561 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2562 if (NULL == dest_peer_info)
2564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2565 "MESH: Creating PeerInfo for destination.\n");
2566 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2567 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2568 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2570 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2572 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2573 if (NULL == orig_peer_info)
2575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2576 "MESH: Creating PeerInfo for origin.\n");
2577 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2578 orig_peer_info->id = GNUNET_PEER_intern (pi);
2579 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2580 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2583 path = path_new (size);
2585 for (i = 0; i < size; i++)
2587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
2588 GNUNET_i2s (&pi[i]));
2589 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2590 if (path->peers[i] == myid)
2593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
2596 /* cannot be self, must be 'not found' */
2597 /* create path: self not found in path through self */
2598 GNUNET_break_op (0);
2599 path_destroy (path);
2600 /* FIXME error. destroy tunnel? leave for timeout? */
2603 path_add_to_peers (path, GNUNET_NO);
2604 tunnel_add_path (t, path, own_pos);
2605 if (own_pos == size - 1)
2607 /* It is for us! Send ack. */
2608 struct MeshDataDescriptor *info;
2611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
2612 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2613 if (NULL == t->peers)
2615 /* New tunnel! Notify clients! */
2616 struct GNUNET_MESH_TunnelNotification cmsg;
2618 cmsg.header.size = htons (sizeof (cmsg));
2619 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2620 GNUNET_PEER_resolve (t->id.oid, &cmsg.peer);
2621 cmsg.tunnel_id = htonl (t->local_tid);
2622 GNUNET_SERVER_notification_context_broadcast (nc, &cmsg.header,
2625 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2627 GNUNET_break (GNUNET_OK ==
2628 GNUNET_CONTAINER_multihashmap_put (t->peers,
2629 &my_full_id.hashPubKey,
2632 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
2633 /* FIXME use send_message */
2634 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2635 info->origin = &t->id;
2636 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2637 GNUNET_assert (NULL != info->peer);
2638 j = peer_info_transmit_slot (info->peer);
2639 info->handler_n = j;
2640 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2641 info->peer->infos[j] = info;
2642 info->peer->core_transmit[j] =
2643 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2644 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2645 sizeof (struct GNUNET_MESH_PathACK),
2646 &send_core_path_ack, info);
2650 struct MeshPeerPath *path2;
2652 /* It's for somebody else! Retransmit. */
2653 path2 = path_duplicate (path);
2654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting.\n");
2655 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
2656 path2 = path_duplicate (path);
2657 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
2658 send_create_path (dest_peer_info, path, t);
2665 * Core handler for path destruction
2667 * @param cls closure
2668 * @param message message
2669 * @param peer peer identity this notification is about
2670 * @param atsi performance data
2671 * @param atsi_count number of records in 'atsi'
2673 * @return GNUNET_OK to keep the connection open,
2674 * GNUNET_SYSERR to close it (signal serious error)
2677 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2678 const struct GNUNET_MessageHeader *message,
2679 const struct GNUNET_ATS_Information *atsi,
2680 unsigned int atsi_count)
2682 struct GNUNET_MESH_ManipulatePath *msg;
2683 struct GNUNET_PeerIdentity *pi;
2684 struct MeshPeerPath *path;
2685 struct MeshTunnel *t;
2686 unsigned int own_pos;
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2691 "MESH: Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
2692 size = ntohs (message->size);
2693 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2695 GNUNET_break_op (0);
2699 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2700 if (size % sizeof (struct GNUNET_PeerIdentity))
2702 GNUNET_break_op (0);
2705 size /= sizeof (struct GNUNET_PeerIdentity);
2708 GNUNET_break_op (0);
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
2713 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2714 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2716 "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
2718 t = tunnel_get (pi, ntohl (msg->tid));
2721 /* TODO notify back: we don't know this tunnel */
2722 GNUNET_break_op (0);
2725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2726 path = path_new (size);
2728 for (i = 0; i < size; i++)
2730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
2731 GNUNET_i2s (&pi[i]));
2732 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2733 if (path->peers[i] == myid)
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
2737 if (own_pos < path->length - 1)
2738 send_message (message, &pi[own_pos + 1]);
2739 tunnel_delete_peer (t, path->peers[path->length - 1]);
2740 path_destroy (path);
2746 * Core handler for notifications of broken paths
2748 * @param cls closure
2749 * @param message message
2750 * @param peer peer identity this notification is about
2751 * @param atsi performance data
2752 * @param atsi_count number of records in 'atsi'
2754 * @return GNUNET_OK to keep the connection open,
2755 * GNUNET_SYSERR to close it (signal serious error)
2758 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
2759 const struct GNUNET_MessageHeader *message,
2760 const struct GNUNET_ATS_Information *atsi,
2761 unsigned int atsi_count)
2763 struct GNUNET_MESH_PathBroken *msg;
2764 struct MeshTunnel *t;
2766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2767 "MESH: Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
2768 msg = (struct GNUNET_MESH_PathBroken *) message;
2769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
2770 GNUNET_i2s (&msg->peer1));
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
2772 GNUNET_i2s (&msg->peer2));
2773 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2776 GNUNET_break_op (0);
2779 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
2780 GNUNET_PEER_search (&msg->peer2));
2787 * Core handler for tunnel destruction
2789 * @param cls closure
2790 * @param message message
2791 * @param peer peer identity this notification is about
2792 * @param atsi performance data
2793 * @param atsi_count number of records in 'atsi'
2795 * @return GNUNET_OK to keep the connection open,
2796 * GNUNET_SYSERR to close it (signal serious error)
2799 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2800 const struct GNUNET_MessageHeader *message,
2801 const struct GNUNET_ATS_Information *atsi,
2802 unsigned int atsi_count)
2804 struct GNUNET_MESH_TunnelDestroy *msg;
2805 struct MeshTunnel *t;
2807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2808 "MESH: Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
2809 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for tunnel %s [%u]\n",
2811 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
2812 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2815 /* Probably already got the message from another path,
2816 * destroyed the tunnel and retransmitted to children.
2821 if (t->id.oid == myid)
2823 GNUNET_break_op (0);
2826 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2828 /* Tunnel was incoming, notify clients */
2829 send_clients_tunnel_destroy (t);
2831 tunnel_send_destroy (t);
2838 * Core handler for mesh network traffic going from the origin to a peer
2840 * @param cls closure
2841 * @param peer peer identity this notification is about
2842 * @param message message
2843 * @param atsi performance data
2844 * @param atsi_count number of records in 'atsi'
2845 * @return GNUNET_OK to keep the connection open,
2846 * GNUNET_SYSERR to close it (signal serious error)
2849 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2850 const struct GNUNET_MessageHeader *message,
2851 const struct GNUNET_ATS_Information *atsi,
2852 unsigned int atsi_count)
2854 struct GNUNET_MESH_Unicast *msg;
2855 struct MeshTunnel *t;
2859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a unicast packet from %s\n",
2861 size = ntohs (message->size);
2863 sizeof (struct GNUNET_MESH_Unicast) +
2864 sizeof (struct GNUNET_MessageHeader))
2869 msg = (struct GNUNET_MESH_Unicast *) message;
2870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: of type %u\n",
2871 ntohs (msg[1].header.type));
2872 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2875 /* TODO notify back: we don't know this tunnel */
2876 GNUNET_break_op (0);
2879 tunnel_reset_timeout (t);
2880 pid = GNUNET_PEER_search (&msg->destination);
2883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2884 "MESH: it's for us! sending to clients...\n");
2885 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2889 "MESH: not for us, retransmitting...\n");
2890 send_message (message, tree_get_first_hop (t->tree, pid));
2896 * Core handler for mesh network traffic going from the origin to all peers
2898 * @param cls closure
2899 * @param message message
2900 * @param peer peer identity this notification is about
2901 * @param atsi performance data
2902 * @param atsi_count number of records in 'atsi'
2903 * @return GNUNET_OK to keep the connection open,
2904 * GNUNET_SYSERR to close it (signal serious error)
2906 * TODO: Check who we got this from, to validate route.
2909 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2910 const struct GNUNET_MessageHeader *message,
2911 const struct GNUNET_ATS_Information *atsi,
2912 unsigned int atsi_count)
2914 struct GNUNET_MESH_Multicast *msg;
2915 struct MeshTunnel *t;
2918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a multicast packet from %s\n",
2920 size = ntohs (message->size);
2921 if (sizeof (struct GNUNET_MESH_Multicast) +
2922 sizeof (struct GNUNET_MessageHeader) > size)
2924 GNUNET_break_op (0);
2927 msg = (struct GNUNET_MESH_Multicast *) message;
2928 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2932 /* TODO notify that we dont know that tunnel */
2933 GNUNET_break_op (0);
2936 if (t->mid == ntohl (msg->mid))
2938 /* FIXME: already seen this packet, log dropping */
2939 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2940 "MESH: Already seen mid %u, DROPPING!\n", t->mid);
2946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2947 "MESH: mid %u not seen yet, forwarding\n", ntohl (msg->mid));
2950 t->mid = ntohl (msg->mid);
2951 tunnel_reset_timeout (t);
2953 /* Transmit to locally interested clients */
2954 if (NULL != t->peers &&
2955 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2957 send_subscribed_clients (message, &msg[1].header);
2960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ttl: %u\n", ntohl (msg->ttl));
2962 if (ntohl (msg->ttl) == 0)
2964 /* FIXME: ttl is 0, log dropping */
2965 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "MESH: TTL is 0, DROPPING!\n");
2968 tunnel_send_multicast (t, message);
2974 * Core handler for mesh network traffic toward the owner of a tunnel
2976 * @param cls closure
2977 * @param message message
2978 * @param peer peer identity this notification is about
2979 * @param atsi performance data
2980 * @param atsi_count number of records in 'atsi'
2982 * @return GNUNET_OK to keep the connection open,
2983 * GNUNET_SYSERR to close it (signal serious error)
2986 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2987 const struct GNUNET_MessageHeader *message,
2988 const struct GNUNET_ATS_Information *atsi,
2989 unsigned int atsi_count)
2991 struct GNUNET_MESH_ToOrigin *msg;
2992 struct GNUNET_PeerIdentity id;
2993 struct MeshPeerInfo *peer_info;
2994 struct MeshTunnel *t;
2997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a ToOrigin packet from %s\n",
2999 size = ntohs (message->size);
3000 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3001 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3003 GNUNET_break_op (0);
3006 msg = (struct GNUNET_MESH_ToOrigin *) message;
3007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: of type %u\n",
3008 ntohs (msg[1].header.type));
3009 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3013 /* TODO notify that we dont know this tunnel (whom)? */
3014 GNUNET_break_op (0);
3018 if (t->id.oid == myid)
3021 struct GNUNET_MESH_ToOrigin *copy;
3023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3024 "MESH: it's for us! sending to clients...\n");
3025 if (NULL == t->client)
3027 /* got data packet for ownerless tunnel */
3028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: no clients!\n");
3029 GNUNET_break_op (0);
3032 /* TODO signature verification */
3033 memcpy (cbuf, message, size);
3034 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3035 copy->tid = htonl (t->local_tid);
3036 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
3037 ©->header, GNUNET_YES);
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3041 "MESH: not for us, retransmitting...\n");
3043 peer_info = peer_info_get (&msg->oid);
3044 if (NULL == peer_info)
3046 /* unknown origin of tunnel */
3050 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3051 send_message (message, &id);
3058 * Core handler for path ACKs
3060 * @param cls closure
3061 * @param message message
3062 * @param peer peer identity this notification is about
3063 * @param atsi performance data
3064 * @param atsi_count number of records in 'atsi'
3066 * @return GNUNET_OK to keep the connection open,
3067 * GNUNET_SYSERR to close it (signal serious error)
3070 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3071 const struct GNUNET_MessageHeader *message,
3072 const struct GNUNET_ATS_Information *atsi,
3073 unsigned int atsi_count)
3075 struct GNUNET_MESH_PathACK *msg;
3076 struct GNUNET_PeerIdentity id;
3077 struct MeshPeerInfo *peer_info;
3078 struct MeshPeerPath *p;
3079 struct MeshTunnel *t;
3081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Received a path ACK msg [%s]\n",
3082 GNUNET_i2s (&my_full_id));
3083 msg = (struct GNUNET_MESH_PathACK *) message;
3084 t = tunnel_get (&msg->oid, msg->tid);
3087 /* TODO notify that we don't know the tunnel */
3091 peer_info = peer_info_get (&msg->peer_id);
3093 /* Add paths to peers? */
3094 p = tree_get_path_to_peer (t->tree, peer_info->id);
3097 path_add_to_peers (p, GNUNET_YES);
3105 /* Message for us? */
3106 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
3109 if (NULL == t->client)
3111 GNUNET_break_op (0);
3114 if (NULL != t->dht_get_type)
3116 GNUNET_DHT_get_stop (t->dht_get_type);
3117 t->dht_get_type = NULL;
3119 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3121 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3122 send_client_peer_connected (t, peer_info->id);
3127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3128 "MESH: not for us, retransmitting...\n");
3129 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3130 peer_info = peer_info_get (&msg->oid);
3131 if (NULL == peer_info)
3133 /* If we know the tunnel, we should DEFINITELY know the peer */
3137 send_message (message, &id);
3143 * Functions to handle messages from core
3145 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3146 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3147 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3148 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3149 sizeof (struct GNUNET_MESH_PathBroken)},
3150 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3151 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3152 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3153 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3154 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3155 sizeof (struct GNUNET_MESH_PathACK)},
3161 /******************************************************************************/
3162 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3163 /******************************************************************************/
3166 * deregister_app: iterator for removing each application registered by a client
3168 * @param cls closure
3169 * @param key the hash of the application id (used to access the hashmap)
3170 * @param value the value stored at the key (client)
3172 * @return GNUNET_OK on success
3175 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3177 GNUNET_break (GNUNET_YES ==
3178 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3185 * notify_client_connection_failure: notify a client that the connection to the
3186 * requested remote peer is not possible (for instance, no route found)
3187 * Function called when the socket is ready to queue more data. "buf" will be
3188 * NULL and "size" zero if the socket was closed for writing in the meantime.
3190 * @param cls closure
3191 * @param size number of bytes available in buf
3192 * @param buf where the callee should write the message
3193 * @return number of bytes written to buf
3196 notify_client_connection_failure (void *cls, size_t size, void *buf)
3199 struct MeshPeerInfo *peer_info;
3200 struct GNUNET_MESH_PeerControl *msg;
3201 struct GNUNET_PeerIdentity id;
3203 if (0 == size && NULL == buf)
3205 // TODO retry? cancel?
3209 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3210 peer_info = (struct MeshPeerInfo *) cls;
3211 msg = (struct GNUNET_MESH_PeerControl *) buf;
3212 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3213 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3214 // msg->tunnel_id = htonl(peer_info->t->tid);
3215 GNUNET_PEER_resolve (peer_info->id, &id);
3216 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3224 * Send keepalive packets for a peer
3226 * @param cls Closure (tunnel for which to send the keepalive).
3227 * @param tc Notification context.
3229 * TODO: implement explicit multicast keepalive?
3232 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3234 struct MeshTunnel *t = cls;
3235 struct GNUNET_MessageHeader *payload;
3236 struct GNUNET_MESH_Multicast *msg;
3238 sizeof (struct GNUNET_MESH_Multicast) +
3239 sizeof (struct GNUNET_MessageHeader);
3242 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3246 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3249 "MESH: sending keepalive for tunnel %d\n", t->id.tid);
3251 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3252 msg->header.size = htons (size);
3253 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3254 msg->oid = my_full_id;
3255 msg->tid = htonl (t->id.tid);
3256 payload = (struct GNUNET_MessageHeader *) &msg[1];
3257 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3258 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3259 tunnel_send_multicast (t, &msg->header);
3261 t->path_refresh_task =
3262 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3268 * Function to process paths received for a new peer addition. The recorded
3269 * paths form the initial tunnel, which can be optimized later.
3270 * Called on each result obtained for the DHT search.
3272 * @param cls closure
3273 * @param exp when will this value expire
3274 * @param key key of the result
3275 * @param get_path path of the get request
3276 * @param get_path_length lenght of get_path
3277 * @param put_path path of the put request
3278 * @param put_path_length length of the put_path
3279 * @param type type of the result
3280 * @param size number of bytes in data
3281 * @param data pointer to the result data
3283 * TODO: re-issue the request after certain time? cancel after X results?
3286 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3287 const GNUNET_HashCode * key,
3288 const struct GNUNET_PeerIdentity *get_path,
3289 unsigned int get_path_length,
3290 const struct GNUNET_PeerIdentity *put_path,
3291 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3292 size_t size, const void *data)
3294 struct MeshPathInfo *path_info = cls;
3295 struct MeshPeerPath *p;
3296 struct GNUNET_PeerIdentity pi;
3299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got results from DHT!\n");
3300 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n", GNUNET_i2s (&pi));
3303 p = path_build_from_dht (get_path, get_path_length, put_path,
3305 path_add_to_peers (p, GNUNET_NO);
3307 for (i = 0; i < path_info->peer->ntunnels; i++)
3309 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3310 peer_info_connect (path_info->peer, path_info->t);
3318 * Function to process paths received for a new peer addition. The recorded
3319 * paths form the initial tunnel, which can be optimized later.
3320 * Called on each result obtained for the DHT search.
3322 * @param cls closure
3323 * @param exp when will this value expire
3324 * @param key key of the result
3325 * @param get_path path of the get request
3326 * @param get_path_length lenght of get_path
3327 * @param put_path path of the put request
3328 * @param put_path_length length of the put_path
3329 * @param type type of the result
3330 * @param size number of bytes in data
3331 * @param data pointer to the result data
3334 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3335 const GNUNET_HashCode * key,
3336 const struct GNUNET_PeerIdentity *get_path,
3337 unsigned int get_path_length,
3338 const struct GNUNET_PeerIdentity *put_path,
3339 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3340 size_t size, const void *data)
3342 const struct GNUNET_PeerIdentity *pi = data;
3343 struct MeshTunnel *t = cls;
3344 struct MeshPeerInfo *peer_info;
3345 struct MeshPeerPath *p;
3347 if (size != sizeof (struct GNUNET_PeerIdentity))
3349 GNUNET_break_op (0);
3352 GNUNET_assert (NULL != t->client);
3353 peer_info = peer_info_get (pi);
3354 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3356 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3358 p = path_build_from_dht (get_path, get_path_length, put_path,
3360 path_add_to_peers (p, GNUNET_NO);
3362 tunnel_add_peer (t, peer_info);
3363 peer_info_connect (peer_info, t);
3367 /******************************************************************************/
3368 /********************* MESH LOCAL HANDLES **************************/
3369 /******************************************************************************/
3373 * Handler for client disconnection
3375 * @param cls closure
3376 * @param client identification of the client; NULL
3377 * for the last call when the server is destroyed
3380 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3382 struct MeshClient *c;
3383 struct MeshClient *next;
3385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3394 if (c->handle != client && NULL != client)
3396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
3401 GNUNET_SERVER_client_drop (c->handle);
3402 if (NULL != c->tunnels)
3404 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3405 &tunnel_destroy_iterator, c);
3406 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3409 /* deregister clients applications */
3410 if (NULL != c->apps)
3412 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3413 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3415 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3416 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3418 GNUNET_SCHEDULER_cancel (announce_applications_task);
3419 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3421 if (NULL != c->types)
3422 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3424 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT FREE at %p\n", c);
3430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3436 * Handler for new clients
3438 * @param cls closure
3439 * @param client identification of the client
3440 * @param message the actual message, which includes messages the client wants
3443 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3444 const struct GNUNET_MessageHeader *message)
3446 struct GNUNET_MESH_ClientConnect *cc_msg;
3447 struct MeshClient *c;
3448 GNUNET_MESH_ApplicationType *a;
3455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3456 /* Check data sanity */
3457 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3458 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3459 ntypes = ntohs (cc_msg->types);
3460 napps = ntohs (cc_msg->applications);
3462 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3465 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3469 /* Create new client structure */
3470 c = GNUNET_malloc (sizeof (struct MeshClient));
3472 c->id = next_client_id++;
3473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT NEW %u at %p\n", c->id,
3477 GNUNET_SERVER_client_keep (client);
3478 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3481 GNUNET_MESH_ApplicationType at;
3484 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3485 for (i = 0; i < napps; i++)
3488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3489 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3490 /* store in clients hashmap */
3491 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3492 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3493 /* store in global hashmap, for announcements */
3494 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3495 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3497 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3498 announce_applications_task =
3499 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3507 t = (uint16_t *) & a[napps];
3508 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3509 for (i = 0; i < ntypes; i++)
3512 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3514 /* store in clients hashmap */
3515 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3516 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3517 /* store in global hashmap */
3518 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3519 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3523 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3525 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3526 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3527 GNUNET_SERVER_notification_context_add (nc, client);
3529 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3537 * Handler for requests of new tunnels
3539 * @param cls closure
3540 * @param client identification of the client
3541 * @param message the actual message
3544 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3545 const struct GNUNET_MessageHeader *message)
3547 struct GNUNET_MESH_TunnelMessage *t_msg;
3548 struct MeshTunnel *t;
3549 struct MeshClient *c;
3550 GNUNET_HashCode hash;
3552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3554 /* Sanity check for client registration */
3555 if (NULL == (c = client_get (client)))
3558 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3565 /* Message sanity check */
3566 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3573 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3574 /* Sanity check for tunnel numbering */
3575 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3578 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3581 /* Sanity check for duplicate tunnel IDs */
3582 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3585 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3589 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3590 while (NULL != tunnel_get_by_pi (myid, next_tid))
3591 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3592 t->id.tid = next_tid++;
3593 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3595 t->local_tid = ntohl (t_msg->tunnel_id);
3597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL %s [%x] (%x)\n",
3598 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3601 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3603 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3605 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3606 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3609 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3613 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3615 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3616 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3619 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3622 t->tree = tree_new (myid);
3624 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3630 * Handler for requests of deleting tunnels
3632 * @param cls closure
3633 * @param client identification of the client
3634 * @param message the actual message
3637 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3638 const struct GNUNET_MessageHeader *message)
3640 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3641 struct MeshClient *c;
3642 struct MeshTunnel *t;
3643 MESH_TunnelNumber tid;
3644 GNUNET_HashCode hash;
3646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3647 "MESH: Got a DESTROY TUNNEL from client!\n");
3649 /* Sanity check for client registration */
3650 if (NULL == (c = client_get (client)))
3653 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3656 /* Message sanity check */
3657 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3660 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3666 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3668 /* Retrieve tunnel */
3669 tid = ntohl (tunnel_msg->tunnel_id);
3671 /* Remove from local id hashmap */
3672 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3673 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3674 GNUNET_assert (GNUNET_YES ==
3675 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t));
3678 tunnel_send_destroy (t);
3680 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3686 * Handler for connection requests to new peers
3688 * @param cls closure
3689 * @param client identification of the client
3690 * @param message the actual message (PeerControl)
3693 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3694 const struct GNUNET_MessageHeader *message)
3696 struct GNUNET_MESH_PeerControl *peer_msg;
3697 struct MeshPeerInfo *peer_info;
3698 struct MeshClient *c;
3699 struct MeshTunnel *t;
3700 MESH_TunnelNumber tid;
3702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3703 /* Sanity check for client registration */
3704 if (NULL == (c = client_get (client)))
3707 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3711 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3712 /* Sanity check for message size */
3713 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3716 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3720 /* Tunnel exists? */
3721 tid = ntohl (peer_msg->tunnel_id);
3722 t = tunnel_get_by_local_id (c, tid);
3726 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3730 /* Does client own tunnel? */
3731 if (t->client->handle != client)
3734 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3738 GNUNET_i2s (&peer_msg->peer));
3739 peer_info = peer_info_get (&peer_msg->peer);
3741 tunnel_add_peer (t, peer_info);
3742 peer_info_connect (peer_info, t);
3744 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3750 * Handler for disconnection requests of peers in a tunnel
3752 * @param cls closure
3753 * @param client identification of the client
3754 * @param message the actual message (PeerControl)
3757 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3758 const struct GNUNET_MessageHeader *message)
3760 struct GNUNET_MESH_PeerControl *peer_msg;
3761 struct MeshPeerInfo *peer_info;
3762 struct MeshClient *c;
3763 struct MeshTunnel *t;
3764 MESH_TunnelNumber tid;
3766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got a PEER DEL request\n");
3767 /* Sanity check for client registration */
3768 if (NULL == (c = client_get (client)))
3771 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3774 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3775 /* Sanity check for message size */
3776 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3779 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3783 /* Tunnel exists? */
3784 tid = ntohl (peer_msg->tunnel_id);
3785 t = tunnel_get_by_local_id (c, tid);
3789 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: on tunnel %X\n", t->id.tid);
3794 /* Does client own tunnel? */
3795 if (t->client->handle != client)
3798 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for peer %s\n",
3803 GNUNET_i2s (&peer_msg->peer));
3804 /* Is the peer in the tunnel? */
3806 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
3807 if (NULL == peer_info)
3810 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3814 /* Ok, delete peer from tunnel */
3815 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3816 &peer_msg->peer.hashPubKey);
3818 send_destroy_path (t, peer_info->id);
3819 tunnel_delete_peer (t, peer_info->id);
3820 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3826 * Handler for connection requests to new peers by type
3828 * @param cls closure
3829 * @param client identification of the client
3830 * @param message the actual message (ConnectPeerByType)
3833 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3834 const struct GNUNET_MessageHeader *message)
3836 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3837 struct MeshClient *c;
3838 struct MeshTunnel *t;
3839 GNUNET_HashCode hash;
3840 MESH_TunnelNumber tid;
3842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3843 /* Sanity check for client registration */
3844 if (NULL == (c = client_get (client)))
3847 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3851 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3852 /* Sanity check for message size */
3853 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3854 ntohs (connect_msg->header.size))
3857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3861 /* Tunnel exists? */
3862 tid = ntohl (connect_msg->tunnel_id);
3863 t = tunnel_get_by_local_id (c, tid);
3867 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3871 /* Does client own tunnel? */
3872 if (t->client->handle != client)
3875 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3879 /* Do WE have the service? */
3880 t->type = ntohl (connect_msg->type);
3881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3882 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3883 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3886 /* Yes! Fast forward, add ourselves to the tunnel and send the
3887 * good news to the client
3889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3890 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3891 peer_info_get (&my_full_id),
3892 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3895 send_client_peer_connected (t, myid);
3896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3897 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3900 /* Ok, lets find a peer offering the service */
3901 if (NULL != t->dht_get_type)
3903 GNUNET_DHT_get_stop (t->dht_get_type);
3905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3906 GNUNET_h2s (&hash));
3908 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
3909 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
3910 GNUNET_DHT_RO_RECORD_ROUTE |
3911 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
3912 &dht_get_type_handler, t);
3914 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3920 * Handler for client traffic directed to one peer
3922 * @param cls closure
3923 * @param client identification of the client
3924 * @param message the actual message
3927 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3928 const struct GNUNET_MessageHeader *message)
3930 struct MeshClient *c;
3931 struct MeshTunnel *t;
3932 struct MeshPeerInfo *pi;
3933 struct GNUNET_MESH_Unicast *data_msg;
3934 MESH_TunnelNumber tid;
3937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3938 "MESH: Got a unicast request from a client!\n");
3940 /* Sanity check for client registration */
3941 if (NULL == (c = client_get (client)))
3944 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3947 data_msg = (struct GNUNET_MESH_Unicast *) message;
3948 /* Sanity check for message size */
3949 size = ntohs (message->size);
3950 if (sizeof (struct GNUNET_MESH_Unicast) +
3951 sizeof (struct GNUNET_MessageHeader) > size)
3954 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3958 /* Tunnel exists? */
3959 tid = ntohl (data_msg->tid);
3960 t = tunnel_get_by_local_id (c, tid);
3964 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3968 /* Is it a local tunnel? Then, does client own the tunnel? */
3969 if (NULL != t->client && NULL != t->client->handle &&
3970 t->client->handle != client)
3973 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3977 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3978 &data_msg->destination.hashPubKey);
3979 /* Is the selected peer in the tunnel? */
3983 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3987 /* Ok, everything is correct, send the message
3988 * (pretend we got it from a mesh peer)
3991 char buf[ntohs (message->size)];
3992 struct GNUNET_MESH_Unicast *copy;
3994 /* Work around const limitation */
3995 copy = (struct GNUNET_MESH_Unicast *) buf;
3996 memcpy (buf, data_msg, size);
3997 copy->oid = my_full_id;
3998 copy->tid = htonl (t->id.tid);
3999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4000 "MESH: calling generic handler...\n");
4001 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4003 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4009 * Handler for client traffic directed to the origin
4011 * @param cls closure
4012 * @param client identification of the client
4013 * @param message the actual message
4016 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4017 const struct GNUNET_MessageHeader *message)
4019 struct GNUNET_MESH_ToOrigin *data_msg;
4020 struct GNUNET_PeerIdentity id;
4021 struct MeshClient *c;
4022 struct MeshTunnel *t;
4023 MESH_TunnelNumber tid;
4026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4027 "MESH: Got a ToOrigin request from a client!\n");
4029 /* Sanity check for client registration */
4030 if (NULL == (c = client_get (client)))
4033 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4036 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4037 /* Sanity check for message size */
4038 size = ntohs (message->size);
4039 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4040 sizeof (struct GNUNET_MessageHeader) > size)
4043 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4047 /* Tunnel exists? */
4048 tid = ntohl (data_msg->tid);
4049 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4052 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4055 t = tunnel_get_by_local_id (c, tid);
4059 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4063 /* It shouldn't be a local tunnel. */
4064 if (NULL != t->client)
4067 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4070 GNUNET_PEER_resolve (t->id.oid, &id);
4072 /* Ok, everything is correct, send the message
4073 * (pretend we got it from a mesh peer)
4076 char buf[ntohs (message->size)];
4077 struct GNUNET_MESH_ToOrigin *copy;
4079 /* Work around const limitation */
4080 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4081 memcpy (buf, data_msg, size);
4083 copy->tid = htonl (t->id.tid);
4084 copy->sender = my_full_id;
4085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4086 "MESH: calling generic handler...\n");
4087 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4089 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4095 * Handler for client traffic directed to all peers in a tunnel
4097 * @param cls closure
4098 * @param client identification of the client
4099 * @param message the actual message
4102 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4103 const struct GNUNET_MessageHeader *message)
4105 struct MeshClient *c;
4106 struct MeshTunnel *t;
4107 struct GNUNET_MESH_Multicast *data_msg;
4108 MESH_TunnelNumber tid;
4110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4111 "MESH: Got a multicast request from a client!\n");
4113 /* Sanity check for client registration */
4114 if (NULL == (c = client_get (client)))
4117 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4120 data_msg = (struct GNUNET_MESH_Multicast *) message;
4121 /* Sanity check for message size */
4122 if (sizeof (struct GNUNET_MESH_Multicast) +
4123 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4126 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4130 /* Tunnel exists? */
4131 tid = ntohl (data_msg->tid);
4132 t = tunnel_get_by_local_id (c, tid);
4136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4140 /* Does client own tunnel? */
4141 if (t->client->handle != client)
4144 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4149 char buf[ntohs (message->size)];
4150 struct GNUNET_MESH_Multicast *copy;
4152 copy = (struct GNUNET_MESH_Multicast *) buf;
4153 memcpy (buf, message, ntohs (message->size));
4154 copy->oid = my_full_id;
4155 copy->tid = htonl (t->id.tid);
4156 copy->ttl = htonl (DEFAULT_TTL);
4157 copy->mid = htonl (t->mid + 1);
4158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4159 "MESH: calling generic handler...\n");
4160 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4163 /* receive done gets called when last copy is sent to a neighbor */
4168 * Functions to handle messages from clients
4170 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4171 {&handle_local_new_client, NULL,
4172 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4173 {&handle_local_tunnel_create, NULL,
4174 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4175 sizeof (struct GNUNET_MESH_TunnelMessage)},
4176 {&handle_local_tunnel_destroy, NULL,
4177 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4178 sizeof (struct GNUNET_MESH_TunnelMessage)},
4179 {&handle_local_connect_add, NULL,
4180 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4181 sizeof (struct GNUNET_MESH_PeerControl)},
4182 {&handle_local_connect_del, NULL,
4183 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4184 sizeof (struct GNUNET_MESH_PeerControl)},
4185 {&handle_local_connect_by_type, NULL,
4186 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4187 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4188 {&handle_local_unicast, NULL,
4189 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4190 {&handle_local_to_origin, NULL,
4191 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4192 {&handle_local_multicast, NULL,
4193 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4199 * To be called on core init/fail.
4201 * @param cls service closure
4202 * @param server handle to the server for this service
4203 * @param identity the public identity of this peer
4206 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4207 const struct GNUNET_PeerIdentity *identity)
4209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
4210 core_handle = server;
4211 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4214 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
4215 GNUNET_SCHEDULER_shutdown ();
4221 * Method called whenever a given peer connects.
4223 * @param cls closure
4224 * @param peer peer identity this notification is about
4225 * @param atsi performance data for the connection
4226 * @param atsi_count number of records in 'atsi'
4229 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4230 const struct GNUNET_ATS_Information *atsi,
4231 unsigned int atsi_count)
4233 struct MeshPeerInfo *peer_info;
4234 struct MeshPeerPath *path;
4236 #if MESH_DEBUG_CONNECTION
4237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
4238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4239 GNUNET_i2s (&my_full_id));
4241 peer_info = peer_info_get (peer);
4242 if (myid == peer_info->id)
4244 #if MESH_DEBUG_CONNECTION
4245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4249 #if MESH_DEBUG_CONNECTION
4252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", GNUNET_i2s (peer));
4255 path = path_new (2);
4256 path->peers[0] = myid;
4257 path->peers[1] = peer_info->id;
4258 GNUNET_PEER_change_rc (myid, 1);
4259 GNUNET_PEER_change_rc (peer_info->id, 1);
4260 peer_info_add_path (peer_info, path, GNUNET_YES);
4265 * Method called whenever a peer disconnects.
4267 * @param cls closure
4268 * @param peer peer identity this notification is about
4271 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4273 struct MeshPeerInfo *pi;
4276 #if MESH_DEBUG_CONNECTION
4277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
4279 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4285 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4287 /* TODO: notify that the transmission failed */
4288 peer_info_cancel_transmission (pi, i);
4290 peer_info_remove_path (pi, pi->id, myid);
4291 #if MESH_DEBUG_CONNECTION
4294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4301 /******************************************************************************/
4302 /************************ MAIN FUNCTIONS ****************************/
4303 /******************************************************************************/
4306 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4308 * @param cls closure
4309 * @param key current key code
4310 * @param value value in the hash map
4311 * @return GNUNET_YES if we should continue to iterate,
4315 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4317 struct MeshTunnel *t = value;
4324 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4326 * @param cls closure
4327 * @param key current key code
4328 * @param value value in the hash map
4329 * @return GNUNET_YES if we should continue to iterate,
4333 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4335 struct MeshPeerInfo *p = value;
4337 peer_info_destroy (p);
4342 * Task run during shutdown.
4348 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4351 /* TODO: destroy tunnels? */
4352 if (core_handle != NULL)
4354 GNUNET_CORE_disconnect (core_handle);
4357 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4358 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4359 if (dht_handle != NULL)
4361 GNUNET_DHT_disconnect (dht_handle);
4366 GNUNET_SERVER_notification_context_destroy (nc);
4369 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4371 GNUNET_SCHEDULER_cancel (announce_id_task);
4372 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4378 * Process mesh requests.
4380 * @param cls closure
4381 * @param server the initialized server
4382 * @param c configuration to use
4385 run (void *cls, struct GNUNET_SERVER_Handle *server,
4386 const struct GNUNET_CONFIGURATION_Handle *c)
4388 struct MeshPeerInfo *peer;
4389 struct MeshPeerPath *p;
4392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4393 server_handle = server;
4394 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4395 CORE_QUEUE_SIZE, /* queue size */
4396 NULL, /* Closure passed to MESH functions */
4397 &core_init, /* Call core_init once connected */
4398 &core_connect, /* Handle connects */
4399 &core_disconnect, /* remove peers on disconnects */
4400 NULL, /* Don't notify about all incoming messages */
4401 GNUNET_NO, /* For header only in notification */
4402 NULL, /* Don't notify about all outbound messages */
4403 GNUNET_NO, /* For header-only out notification */
4404 core_handlers); /* Register these handlers */
4406 if (core_handle == NULL)
4409 GNUNET_SCHEDULER_shutdown ();
4414 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4419 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4420 GNUNET_SCHEDULER_shutdown ();
4423 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4424 GNUNET_free (keyfile);
4425 if (my_private_key == NULL)
4427 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4428 _("Mesh service could not access hostkey. Exiting.\n"));
4429 GNUNET_SCHEDULER_shutdown ();
4432 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4433 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4434 &my_full_id.hashPubKey);
4435 myid = GNUNET_PEER_intern (&my_full_id);
4437 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4444 dht_handle = GNUNET_DHT_connect (c, 64);
4445 if (dht_handle == NULL)
4451 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4453 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4454 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4455 peers = GNUNET_CONTAINER_multihashmap_create (32);
4456 applications = GNUNET_CONTAINER_multihashmap_create (32);
4457 types = GNUNET_CONTAINER_multihashmap_create (32);
4459 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4460 nc = GNUNET_SERVER_notification_context_create (server_handle,
4462 GNUNET_SERVER_disconnect_notify (server_handle,
4463 &handle_local_client_disconnect, NULL);
4467 clients_tail = NULL;
4472 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4473 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4475 /* Create a peer_info for the local peer */
4476 peer = peer_info_get (&my_full_id);
4479 GNUNET_PEER_change_rc (myid, 1);
4480 peer_info_add_path (peer, p, GNUNET_YES);
4482 /* Scheduled the task to clean up when shutdown is called */
4483 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4490 * The main function for the mesh service.
4492 * @param argc number of arguments from the command line
4493 * @param argv command line arguments
4494 * @return 0 ok, 1 on error
4497 main (int argc, char *const *argv)
4501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4504 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");