2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - relay corking down to core
48 #include "mesh_protocol.h"
49 #include "gnunet_dht_service.h"
50 #include "mesh_tunnel_tree.h"
52 /* TODO: move into configuration file */
53 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
54 GNUNET_TIME_UNIT_SECONDS,\
56 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
57 GNUNET_TIME_UNIT_SECONDS,\
60 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
61 GNUNET_TIME_UNIT_SECONDS,\
64 #define GET_RESTART_TIME GNUNET_TIME_relative_multiply(\
65 GNUNET_TIME_UNIT_SECONDS,\
68 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
69 GNUNET_TIME_UNIT_SECONDS,\
72 /******************************************************************************/
73 /************************ DATA STRUCTURES ****************************/
74 /******************************************************************************/
76 /** FWD declaration */
80 * Struct containing all info possibly needed to build a package when called
83 struct MeshDataDescriptor
85 /** ID of the tunnel this packet travels in */
86 struct MESH_TunnelID *origin;
91 /** Client that asked for the transmission, if any */
92 struct GNUNET_SERVER_Client *client;
94 /** Who was this message being sent to */
95 struct MeshPeerInfo *peer;
97 /** Ultimate destination of the packet */
98 GNUNET_PEER_Id destination;
100 /** Number of identical messages sent to different hops (multicast) */
101 unsigned int *copies;
103 /** Which handler was used to request the transmission */
104 unsigned int handler_n;
106 /** Size of the data */
109 /** Used to allow a client send more traffic to the service after a
110 * previous packet was tried to be sent to a neighbor and couldn't */
111 GNUNET_SCHEDULER_TaskIdentifier *timeout_task;
116 * Struct containing all information regarding a given peer
126 * Last time we heard from this peer
128 struct GNUNET_TIME_Absolute last_contact;
131 * Number of attempts to reconnect so far
133 int n_reconnect_attempts;
136 * Paths to reach the peer, ordered by ascending hop count
138 struct MeshPeerPath *path_head;
141 * Paths to reach the peer, ordered by ascending hop count
143 struct MeshPeerPath *path_tail;
146 * Handle to stop the DHT search for a path to this peer
148 struct GNUNET_DHT_GetHandle *dhtget;
151 * Closure given to the DHT GET
153 struct MeshPathInfo *dhtgetcls;
156 * Handles to stop queued transmissions for this peer
158 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
161 * Pointer to info stuctures used as cls for queued transmissions
163 void *infos[CORE_QUEUE_SIZE];
166 * Type of message being in each transmission
168 uint16_t types[CORE_QUEUE_SIZE];
171 * Array of tunnels this peer participates in
172 * (most probably a small amount, therefore not a hashmap)
173 * When the path to the peer changes, notify these tunnels to let them
174 * re-adjust their path trees.
176 struct MeshTunnel **tunnels;
179 * Number of tunnels this peers participates in
181 unsigned int ntunnels;
186 * Data scheduled to transmit (to local client or remote peer)
193 struct MeshQueue *next;
194 struct MeshQueue *prev;
197 * Target of the data (NULL if target is client)
199 struct MeshPeerInfo *peer;
202 * Client to send the data to (NULL if target is peer)
204 struct MeshClient *client;
207 * Size of the message to transmit
212 * How old is the data?
214 struct GNUNET_TIME_Absolute timestamp;
219 struct GNUNET_MessageHeader *data;
223 * Globally unique tunnel identification (owner + number)
224 * DO NOT USE OVER THE NETWORK
229 * Node that owns the tunnel
234 * Tunnel number to differentiate all the tunnels owned by the node oid
235 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
237 MESH_TunnelNumber tid;
241 struct MeshClient; /* FWD declaration */
244 * Struct containing all information regarding a tunnel
245 * For an intermediate node the improtant info used will be:
246 * - id Tunnel unique identification
247 * - paths[0] To know where to send it next
248 * - metainfo: ready, speeds, accounting
255 struct MESH_TunnelID id;
258 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
260 MESH_TunnelNumber local_tid;
263 * Last time the tunnel was used
265 struct GNUNET_TIME_Absolute timestamp;
268 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
269 * containing peers added by id or by type, not intermediate peers.
271 struct GNUNET_CONTAINER_MultiHashMap *peers;
274 * Number of peers that are connected and potentially ready to receive data
276 unsigned int peers_ready;
279 * Number of peers that have been added to the tunnel
281 unsigned int peers_total;
284 * Client owner of the tunnel, if any
286 struct MeshClient *client;
289 * Messages ready to transmit
291 struct MeshQueue *queue_head;
292 struct MeshQueue *queue_tail;
297 struct MeshTunnelTree *tree;
300 * Application type we are looking for in this tunnel
302 GNUNET_MESH_ApplicationType type;
305 * Used to search peers offering a service
307 struct GNUNET_DHT_GetHandle *dht_get_type;
310 * Task to keep the used paths alive
312 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
315 * Task to destroy the tunnel after timeout
317 * FIXME: merge the two? a tunnel will have either
318 * a path refresh OR a timeout, never both!
320 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
325 * Info needed to work with tunnel paths and peers
332 struct MeshTunnel *t;
335 * Neighbouring peer to whom we send the packet to
337 struct MeshPeerInfo *peer;
342 struct MeshPeerPath *path;
345 * Position in peer's transmit queue
352 * Struct containing information about a client of the service
359 struct MeshClient *next;
360 struct MeshClient *prev;
363 * Tunnels that belong to this client, indexed by local id
365 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
368 * Handle to communicate with the client
370 struct GNUNET_SERVER_Client *handle;
373 * Applications that this client has claimed to provide
375 struct GNUNET_CONTAINER_MultiHashMap *apps;
378 * Messages that this client has declared interest in
380 struct GNUNET_CONTAINER_MultiHashMap *types;
384 * ID of the client, for debug messages
393 /******************************************************************************/
394 /************************ DEBUG FUNCTIONS ****************************/
395 /******************************************************************************/
399 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
400 * @param cls string to print
401 * @param tc task context
404 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
408 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
416 /******************************************************************************/
417 /*********************** GLOBAL VARIABLES ****************************/
418 /******************************************************************************/
423 static struct MeshClient *clients;
424 static struct MeshClient *clients_tail;
427 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
429 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
432 * Tunnels incoming, indexed by MESH_TunnelNumber
433 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
435 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
438 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
440 static struct GNUNET_CONTAINER_MultiHashMap *peers;
443 * Handle to communicate with core
445 static struct GNUNET_CORE_Handle *core_handle;
448 * Handle to communicate with transport
450 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
455 static struct GNUNET_DHT_Handle *dht_handle;
460 static struct GNUNET_SERVER_Handle *server_handle;
463 * Notification context, to send messages to local clients
465 static struct GNUNET_SERVER_NotificationContext *nc;
468 * Local peer own ID (memory efficient handle)
470 static GNUNET_PEER_Id myid;
473 * Local peer own ID (full value)
475 static struct GNUNET_PeerIdentity my_full_id;
480 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
485 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
488 * Tunnel ID for the next created tunnel (global tunnel number)
490 static MESH_TunnelNumber next_tid;
493 * Tunnel ID for the next incoming tunnel (local tunnel number)
495 static MESH_TunnelNumber next_local_tid;
498 * All application types provided by this peer
500 static struct GNUNET_CONTAINER_MultiHashMap *applications;
503 * All message types clients of this peer are interested in
505 static struct GNUNET_CONTAINER_MultiHashMap *types;
508 * Task to periodically announce provided applications
510 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
513 * Task to periodically announce itself in the network
515 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
518 unsigned int next_client_id;
522 /******************************************************************************/
523 /************************ ITERATORS ****************************/
524 /******************************************************************************/
526 /* FIXME move iterators here */
529 /******************************************************************************/
530 /************************ PERIODIC FUNCTIONS ****************************/
531 /******************************************************************************/
534 * Announce iterator over for each application provided by the peer
537 * @param key current key code
538 * @param value value in the hash map
539 * @return GNUNET_YES if we should continue to
544 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
546 /* FIXME are hashes in multihash map equal on all aquitectures? */
547 GNUNET_DHT_put (dht_handle,
550 GNUNET_DHT_RO_RECORD_ROUTE |
551 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
552 GNUNET_BLOCK_TYPE_TEST,
553 sizeof (struct GNUNET_PeerIdentity),
554 (const char *) &my_full_id,
556 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
557 &mesh_debug, "DHT_put for app completed");
559 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
561 APP_ANNOUNCE_TIME, NULL, NULL);
568 * Periodically announce what applications are provided by local clients
571 * @param tc task context
574 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
576 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
578 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
582 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
584 announce_applications_task =
585 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
593 * Periodically announce self id in the DHT
596 * @param tc task context
599 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
601 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
603 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
607 * - Set data expiration in function of X
610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
611 GNUNET_i2s (&my_full_id));
612 GNUNET_DHT_put (dht_handle, /* DHT handle */
613 &my_full_id.hashPubKey, /* Key to use */
614 10U, /* Replication level */
615 GNUNET_DHT_RO_RECORD_ROUTE |
616 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
617 GNUNET_BLOCK_TYPE_TEST, /* Block type */
618 sizeof(my_full_id), /* Size of the data */
619 (char *)&my_full_id, /* Data itself */
620 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
621 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
623 &mesh_debug, "DHT_put for id completed");
625 NULL, /* Continuation */
626 NULL); /* Continuation closure */
629 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
634 * Function to process paths received for a new peer addition. The recorded
635 * paths form the initial tunnel, which can be optimized later.
636 * Called on each result obtained for the DHT search.
639 * @param exp when will this value expire
640 * @param key key of the result
641 * @param type type of the result
642 * @param size number of bytes in data
643 * @param data pointer to the result data
646 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
647 const GNUNET_HashCode * key,
648 const struct GNUNET_PeerIdentity *get_path,
649 unsigned int get_path_length,
650 const struct GNUNET_PeerIdentity *put_path,
651 unsigned int put_path_length,
652 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
655 /******************************************************************************/
656 /****************** GENERAL HELPER FUNCTIONS ************************/
657 /******************************************************************************/
660 * Check if client has registered with the service and has not disconnected
662 * @param client the client to check
664 * @return non-NULL if client exists in the global DLL
666 static struct MeshClient *
667 client_get (struct GNUNET_SERVER_Client *client)
669 struct MeshClient *c;
674 if (c->handle == client)
683 * Checks if a given client has subscribed to certain message type
685 * @param message_type Type of message to check
686 * @param c Client to check
688 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
693 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
697 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
698 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
703 * Allow a client to send more data after transmitting a multicast message
704 * which some neighbor has not yet accepted altough a reasonable time has
707 * @param cls Closure (DataDescriptor containing the task identifier)
708 * @param tc Task Context
711 client_allow_send(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
713 struct MeshDataDescriptor *info = cls;
715 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "MESH: CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
722 *(info->timeout_task) = GNUNET_SCHEDULER_NO_TASK;
723 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
728 * Search for a tunnel by global ID using full PeerIdentities
730 * @param oid owner of the tunnel
731 * @param tid global tunnel number
733 * @return tunnel handler, NULL if doesn't exist
735 static struct MeshTunnel *
736 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
740 * Notify a tunnel that a connection has broken that affects at least
743 * @param t Tunnel affected.
744 * @param p1 Peer that got disconnected from p2.
745 * @param p2 Peer that got disconnected from p1.
747 * @return Short ID of the peer disconnected (either p1 or p2).
748 * 0 if the tunnel remained unaffected.
750 static GNUNET_PEER_Id
751 tunnel_notify_connection_broken (struct MeshTunnel *t,
757 * Send the message to all clients that have subscribed to its type
759 * @param msg Pointer to the message itself
760 * @return number of clients this message was sent to
763 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
764 const struct GNUNET_MessageHeader *payload)
766 struct GNUNET_PeerIdentity *oid;
767 struct MeshClient *c;
768 struct MeshTunnel *t;
769 MESH_TunnelNumber *tid;
772 char cbuf[htons(msg->size)];
774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
775 type = ntohs (payload->type);
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
778 memcpy (cbuf, msg, sizeof(cbuf));
779 switch (htons(msg->type))
781 struct GNUNET_MESH_Unicast *uc;
782 struct GNUNET_MESH_Multicast *mc;
783 struct GNUNET_MESH_ToOrigin *to;
785 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
786 uc = (struct GNUNET_MESH_Unicast *) cbuf;
790 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
791 mc = (struct GNUNET_MESH_Multicast *) cbuf;
795 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
796 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
804 t = tunnel_get (oid, ntohl(*tid));
810 *tid = htonl (t->local_tid);
811 for (count = 0, c = clients; c != NULL; c = c->next)
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
814 if (client_is_subscribed (type, c))
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
818 GNUNET_SERVER_notification_context_unicast (
821 (struct GNUNET_MessageHeader *) cbuf,
830 * Notify the client that owns the tunnel that a peer has connected to it
831 * (the requested path to it has been confirmed).
833 * @param t Tunnel whose owner to notify
834 * @param id Short id of the peer that has connected
837 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
839 struct GNUNET_MESH_PeerControl pc;
841 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
842 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
843 pc.tunnel_id = htonl (t->local_tid);
844 GNUNET_PEER_resolve (id, &pc.peer);
845 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
846 &pc.header, GNUNET_NO);
851 * Notify all clients (not depending on registration status) that the incoming
852 * tunnel is no longer valid.
854 * @param t Tunnel that was destroyed.
857 send_clients_tunnel_destroy (struct MeshTunnel *t)
859 struct GNUNET_MESH_TunnelMessage msg;
861 msg.header.size = htons (sizeof (msg));
862 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
863 msg.tunnel_id = htonl (t->local_tid);
864 GNUNET_SERVER_notification_context_broadcast(nc, &msg.header, GNUNET_NO);
869 * Function called to notify a client about the socket
870 * being ready to queue more data. "buf" will be
871 * NULL and "size" zero if the socket was closed for
872 * writing in the meantime.
875 * @param size number of bytes available in buf
876 * @param buf where the callee should write the message
877 * @return number of bytes written to buf
880 send_core_create_path (void *cls, size_t size, void *buf);
884 * Function called to notify a client about the socket
885 * being ready to queue more data. "buf" will be
886 * NULL and "size" zero if the socket was closed for
887 * writing in the meantime.
889 * @param cls closure (data itself)
890 * @param size number of bytes available in buf
891 * @param buf where the callee should write the message
893 * @return number of bytes written to buf
896 send_core_data_multicast (void *cls, size_t size, void *buf);
900 * Decrements the reference counter and frees all resources if needed
902 * @param dd Data Descriptor used in a multicast message
905 data_descriptor_decrement_multicast (struct MeshDataDescriptor *dd)
907 if (0 == --(*(dd->copies)))
909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
910 if (NULL != dd->client)
912 if (GNUNET_SCHEDULER_NO_TASK != *(dd->timeout_task))
914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
915 "MESH: cancelling client timeout (%u)...\n",
916 *(dd->timeout_task));
917 GNUNET_SCHEDULER_cancel(*(dd->timeout_task));
918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
919 GNUNET_SERVER_receive_done (dd->client, GNUNET_OK);
921 GNUNET_free (dd->timeout_task);
923 GNUNET_free (dd->copies);
924 GNUNET_free (dd->data);
930 * Cancel a core transmission that was already requested and free all resources
931 * associated to the request.
933 * @param peer PeeInfo of the peer whose transmission is cancelled.
934 * @param i Position of the transmission to be cancelled.
937 peer_info_cancel_transmission(struct MeshPeerInfo *peer, unsigned int i)
939 if (NULL != peer->core_transmit[i])
941 struct MeshDataDescriptor *dd;
942 struct MeshPathInfo *path_info;
946 struct GNUNET_PeerIdentity id;
948 GNUNET_PEER_resolve (peer->id, &id);
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 "MESH: Cancelling data transmission at %s [%u]\n",
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
954 "MESH: message type %u\n",
958 /* TODO: notify that tranmission has failed */
959 switch (peer->types[i])
961 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
962 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
963 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
966 data_descriptor_decrement_multicast (dd);
968 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
970 path_info = peer->infos[i];
971 path_destroy(path_info->path);
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
977 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
978 peer->core_transmit[i] = NULL;
979 GNUNET_free (peer->infos[i]);
985 * Get a unused CORE slot to transmit a message to a peer. If all the slots
986 * are used, cancel one and return it's position.
988 * @param peer PeerInfo of the neighbor we want to transmit to.
990 * @return The index of an available slot to transmit to the neighbor.
993 peer_info_transmit_slot (struct MeshPeerInfo *peer)
997 for (i = 0; peer->core_transmit[i]; i++)
999 if (i == (CORE_QUEUE_SIZE - 1))
1001 /* All positions are taken! Overwriting! */
1003 peer_info_cancel_transmission(peer, 0);
1012 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1013 * and insert it in the appropiate structures if the peer is not known yet.
1015 * @param peer Full identity of the peer.
1017 * @return Existing or newly created peer info.
1019 static struct MeshPeerInfo *
1020 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1022 struct MeshPeerInfo *peer_info;
1024 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1025 if (NULL == peer_info)
1028 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1029 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1030 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1031 peer_info->id = GNUNET_PEER_intern (peer);
1039 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1040 * and insert it in the appropiate structures if the peer is not known yet.
1042 * @param peer Short identity of the peer.
1044 * @return Existing or newly created peer info.
1046 static struct MeshPeerInfo *
1047 peer_info_get_short (const GNUNET_PEER_Id peer)
1049 struct GNUNET_PeerIdentity id;
1051 GNUNET_PEER_resolve(peer, &id);
1052 return peer_info_get(&id);
1057 * Iterator to remove the tunnel from the list of tunnels a peer participates
1060 * @param cls Closure (tunnel info)
1061 * @param key GNUNET_PeerIdentity of the peer (unused)
1062 * @param value PeerInfo of the peer
1064 * @return always GNUNET_YES, to keep iterating
1067 peer_info_delete_tunnel (void* cls, const GNUNET_HashCode* key, void* value)
1069 struct MeshTunnel *t = cls;
1070 struct MeshPeerInfo *peer = value;
1073 for (i = 0; i < peer->ntunnels; i++)
1075 if (0 == memcmp(&peer->tunnels[i]->id,
1077 sizeof(struct MESH_TunnelID)))
1080 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1081 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1090 * Core callback to write a
1092 * @param cls Closure (MeshDataDescriptor with data in "data" member).
1093 * @param size Number of bytes available in buf.
1094 * @param buf Where the to write the message.
1096 * @return number of bytes written to buf
1099 send_core_data_raw (void *cls, size_t size, void *buf)
1101 struct MeshDataDescriptor *info = cls;
1102 struct GNUNET_MessageHeader *msg;
1105 GNUNET_assert (NULL != info);
1106 GNUNET_assert (NULL != info->data);
1107 msg = (struct GNUNET_MessageHeader *) info->data;
1108 total_size = ntohs (msg->size);
1110 if (total_size > size)
1112 struct GNUNET_PeerIdentity id;
1114 GNUNET_PEER_resolve (info->peer->id, &id);
1115 info->peer->core_transmit[info->handler_n] =
1116 GNUNET_CORE_notify_transmit_ready(core_handle,
1119 GNUNET_TIME_UNIT_FOREVER_REL,
1122 &send_core_data_raw,
1126 info->peer->core_transmit[info->handler_n] = NULL;
1127 memcpy (buf, msg, total_size);
1128 GNUNET_free (info->data);
1135 * Sends an already built message to a peer, properly registrating
1136 * all used resources.
1138 * @param message Message to send. Fucntion makes a copy of it.
1139 * @param peer Short ID of the neighbor whom to send the message.
1142 send_message (const struct GNUNET_MessageHeader *message,
1143 const struct GNUNET_PeerIdentity *peer)
1145 struct MeshDataDescriptor *info;
1146 struct MeshPeerInfo *neighbor;
1147 struct MeshPeerPath *p;
1151 // GNUNET_TRANSPORT_try_connect();
1153 size = ntohs (message->size);
1154 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1155 info->data = GNUNET_malloc (size);
1156 memcpy (info->data, message, size);
1157 neighbor = peer_info_get (peer);
1158 for (p = neighbor->path_head; NULL != p; p = p->next)
1170 i = peer_info_transmit_slot (neighbor);
1171 info->handler_n = i;
1172 info->peer = neighbor;
1173 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1174 neighbor->infos[i] = info;
1175 neighbor->core_transmit[i] =
1176 GNUNET_CORE_notify_transmit_ready(core_handle,
1179 GNUNET_TIME_UNIT_FOREVER_REL,
1182 &send_core_data_raw,
1189 * Sends a CREATE PATH message for a path to a peer, properly registrating
1190 * all used resources.
1192 * @param peer PeerInfo of the final peer for whom this path is being created.
1193 * @param p Path itself.
1194 * @param t Tunnel for which the path is created.
1197 send_create_path (struct MeshPeerInfo *peer,
1198 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] =
1237 GNUNET_CORE_notify_transmit_ready (
1238 core_handle, /* handle */
1241 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1243 sizeof (struct GNUNET_MESH_ManipulatePath)
1244 + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1245 &send_core_create_path, /* callback */
1246 path_info); /* cls */
1251 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1253 * @param t Tunnel whose path to destroy.
1254 * @param destination Short ID of the peer to whom the path to destroy.
1257 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1259 struct MeshPeerPath *p;
1262 p = tree_get_path_to_peer(t->tree, destination);
1268 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1269 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1271 struct GNUNET_MESH_ManipulatePath *msg;
1272 struct GNUNET_PeerIdentity *pi;
1276 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1277 msg->header.size = htons (size);
1278 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1279 msg->tid = htonl (t->id.tid);
1280 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1281 for (i = 0; i < p->length; i++)
1283 GNUNET_PEER_resolve(p->peers[i], &pi[i]);
1285 send_message (&msg->header, path_get_first_hop(t->tree, destination));
1292 * Try to establish a new connection to this peer.
1293 * Use the best path for the given tunnel.
1294 * If the peer doesn't have any path to it yet, try to get one.
1295 * If the peer already has some path, send a CREATE PATH towards it.
1297 * @param peer PeerInfo of the peer.
1298 * @param t Tunnel for which to create the path, if possible.
1301 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1303 struct MeshPeerPath *p;
1304 struct MeshPathInfo *path_info;
1306 if (NULL != peer->path_head)
1308 p = tree_get_path_to_peer(t->tree, peer->id);
1317 send_create_path(peer, p, t);
1322 send_client_peer_connected(t, myid);
1325 else if (NULL == peer->dhtget)
1327 struct GNUNET_PeerIdentity id;
1329 GNUNET_PEER_resolve(peer->id, &id);
1330 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1331 path_info->peer = peer;
1333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1334 "MESH: Starting DHT GET for peer %s\n",
1336 peer->dhtgetcls = path_info;
1338 GNUNET_DHT_get_start(dht_handle, /* handle */
1339 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1340 GNUNET_BLOCK_TYPE_TEST, /* type */
1341 &id.hashPubKey, /* key to search */
1342 4, /* replication level */
1343 GNUNET_DHT_RO_RECORD_ROUTE |
1344 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1346 0, /* xquery bits */
1347 &dht_get_id_handler,
1350 /* Otherwise, there is no path but the DHT get is already started. */
1355 * Task to delay the connection of a peer
1357 * @param cls Closure (path info with tunnel and peer to connect).
1358 * Will be free'd on exection.
1359 * @param tc TaskContext
1362 peer_info_connect_task (void *cls,
1363 const struct GNUNET_SCHEDULER_TaskContext *tc)
1365 struct MeshPathInfo *path_info = cls;
1367 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1372 peer_info_connect (path_info->peer, path_info->t);
1378 * Destroy the peer_info and free any allocated resources linked to it
1380 * @param pi The peer_info to destroy.
1382 * @return GNUNET_OK on success
1385 peer_info_destroy (struct MeshPeerInfo *pi)
1387 struct GNUNET_PeerIdentity id;
1388 struct MeshPeerPath *p;
1389 struct MeshPeerPath *nextp;
1392 GNUNET_PEER_resolve (pi->id, &id);
1393 GNUNET_PEER_change_rc (pi->id, -1);
1396 GNUNET_CONTAINER_multihashmap_remove (peers,&id.hashPubKey, pi))
1399 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1400 "MESH: removing peer %s, not in hashmap\n",
1403 if (NULL != pi->dhtget)
1405 GNUNET_DHT_get_stop(pi->dhtget);
1406 GNUNET_free (pi->dhtgetcls);
1408 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1410 peer_info_cancel_transmission(pi, i);
1416 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1426 * Remove all paths that rely on a direct connection between p1 and p2
1427 * from the peer itself and notify all tunnels about it.
1429 * @param peer PeerInfo of affected peer.
1430 * @param p1 GNUNET_PEER_Id of one peer.
1431 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1434 * TODO: optimize (see below)
1437 path_remove_from_peer (struct MeshPeerInfo *peer,
1441 struct MeshPeerPath *p;
1442 struct MeshPeerPath *aux;
1443 struct MeshPeerInfo *peer_d;
1445 unsigned int destroyed;
1451 p = peer->path_head;
1455 for (i = 0; i < (p->length - 1); i++)
1457 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1458 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1460 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1471 for (i = 0; i < peer->ntunnels; i++)
1473 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1475 * Problem: one or more peers have been deleted from the tunnel tree.
1476 * We don't know who they are to try to add them again.
1477 * We need to try to find a new path for each of the disconnected peers.
1478 * Some of them might already have a path to reach them that does not
1479 * involve p1 and p2. Adding all anew might render in a better tree than
1480 * the trivial immediate fix.
1482 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1483 * its children will be reachable trough him.
1485 peer_d = peer_info_get_short(d);
1488 for (p = peer_d->path_head; NULL != p; p = p->next)
1490 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
1498 /* No callback, as peer will be already disconnected */
1499 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
1503 peer_info_connect (peer_d, peer->tunnels[i]);
1510 * Add the path to the peer and update the path used to reach it in case this
1513 * @param peer_info Destination peer to add the path to.
1514 * @param path New path to add. Last peer must be the peer in arg 1.
1515 * Path will be either used of freed if already known.
1516 * @param trusted Do we trust that this path is real?
1519 path_add_to_peer (struct MeshPeerInfo *peer_info,
1520 struct MeshPeerPath *path,
1523 struct MeshPeerPath *aux;
1527 if ((NULL == peer_info) || (NULL == path))
1530 path_destroy (path);
1533 if (path->length <= 2 && GNUNET_NO == trusted)
1535 /* Only allow CORE to tell us about direct paths */
1538 for (l = 1; l < path->length; l++)
1540 if (path->peers[l] == myid)
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "MESH: shortening path by %u\n",
1545 for (l2 = 0; l2 < path->length - l - 1 ; l2++)
1547 path->peers[l2] = path->peers[l + l2];
1551 path->peers = GNUNET_realloc (path->peers,
1552 path->length * sizeof (GNUNET_PEER_Id));
1557 struct GNUNET_PeerIdentity id;
1559 GNUNET_PEER_resolve (peer_info->id, &id);
1560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1561 "MESH: adding path [%u] to peer %s\n",
1566 l = path_get_length (path);
1573 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1575 l2 = path_get_length (aux);
1578 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1579 peer_info->path_tail,
1586 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1593 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head,
1594 peer_info->path_tail,
1601 * Add the path to the origin peer and update the path used to reach it in case
1602 * this is the shortest.
1603 * The path is given in peer_info -> destination, therefore we turn the path
1604 * upside down first.
1606 * @param peer_info Peer to add the path to, being the origin of the path.
1607 * @param path New path to add after being inversed.
1608 * @param trusted Do we trust that this path is real?
1611 path_add_to_origin (struct MeshPeerInfo *peer_info,
1612 struct MeshPeerPath *path,
1616 path_add_to_peer (peer_info, path, trusted);
1621 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1622 * to obtain a local peer -> destination path and interning the peer ids.
1624 * @return Newly allocated and created path
1626 static struct MeshPeerPath *
1627 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1628 unsigned int get_path_length,
1629 const struct GNUNET_PeerIdentity *put_path,
1630 unsigned int put_path_length)
1632 struct MeshPeerPath *p;
1638 GNUNET_PEER_change_rc(myid, 1);
1639 i = get_path_length;
1640 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1641 for (i--; i >= 0; i--)
1643 id = GNUNET_PEER_intern (&get_path[i]);
1644 if (p->length > 0 && id == p->peers[p->length - 1])
1646 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1647 GNUNET_PEER_change_rc(id, -1);
1651 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1652 "MESH: Adding from GET: %s.\n",
1653 GNUNET_i2s(&get_path[i]));
1655 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1656 p->peers[p->length - 1] = id;
1659 i = put_path_length;
1660 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1661 for (i--; i >= 0; i--)
1663 id = GNUNET_PEER_intern (&put_path[i]);
1666 /* PUT path went through us, so discard the path up until now and start
1667 * from here to get a much shorter (and loop-free) path.
1672 if (p->length > 0 && id == p->peers[p->length - 1])
1674 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1675 GNUNET_PEER_change_rc(id, -1);
1679 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1680 "MESH: Adding from PUT: %s.\n",
1681 GNUNET_i2s(&put_path[i]));
1683 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1684 p->peers[p->length - 1] = id;
1688 if (get_path_length > 0)
1689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1690 "MESH: (first of GET: %s)\n",
1691 GNUNET_i2s(&get_path[0]));
1692 if (put_path_length > 0)
1693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1694 "MESH: (first of PUT: %s)\n",
1695 GNUNET_i2s(&put_path[0]));
1696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1697 "MESH: In total: %d hops\n",
1699 for (i = 0; i < p->length; i++)
1701 struct GNUNET_PeerIdentity peer_id;
1703 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1707 GNUNET_i2s(&peer_id));
1715 * Send keepalive packets for a peer
1717 * @param cls Closure (tunnel for which to send the keepalive).
1718 * @param tc Notification context.
1720 * TODO: implement explicit multicast keepalive?
1723 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1727 * Search for a tunnel among the tunnels for a client
1729 * @param c the client whose tunnels to search in
1730 * @param tid the local id of the tunnel
1732 * @return tunnel handler, NULL if doesn't exist
1734 static struct MeshTunnel *
1735 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1737 GNUNET_HashCode hash;
1739 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1740 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1742 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1744 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1749 * Search for a tunnel by global ID using PEER_ID
1751 * @param pi owner of the tunnel
1752 * @param tid global tunnel number
1754 * @return tunnel handler, NULL if doesn't exist
1756 static struct MeshTunnel *
1757 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1759 struct MESH_TunnelID id;
1760 GNUNET_HashCode hash;
1765 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1766 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1771 * Search for a tunnel by global ID using full PeerIdentities
1773 * @param oid owner of the tunnel
1774 * @param tid global tunnel number
1776 * @return tunnel handler, NULL if doesn't exist
1778 static struct MeshTunnel *
1779 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1781 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1786 * Callback used to notify a client owner of a tunnel that a peer has
1787 * disconnected, most likely because of a path change.
1789 * @param n Node in the tree representing the disconnected peer
1791 * FIXME: pass tunnel via cls, make param just a peer identity
1794 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1796 struct MeshPeerInfo *peer;
1797 struct MeshPathInfo *path_info;
1799 if (NULL != n->t->client && NULL != nc)
1801 struct GNUNET_MESH_PeerControl msg;
1802 msg.header.size = htons (sizeof (msg));
1803 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1804 msg.tunnel_id = htonl (n->t->local_tid);
1805 GNUNET_PEER_resolve (n->peer, &msg.peer);
1806 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1807 &msg.header, GNUNET_NO);
1809 peer = peer_info_get_short(n->peer);
1810 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1811 path_info->peer = peer;
1812 path_info->t = n->t;
1813 GNUNET_SCHEDULER_add_now(&peer_info_connect_task, path_info);
1818 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1819 * needed rescources.
1820 * If peer already exists, reevaluate shortest path and change if different.
1822 * @param t Tunnel we want to add a new peer to
1823 * @param peer PeerInfo of the peer being added
1827 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1829 struct GNUNET_PeerIdentity id;
1830 struct MeshPeerPath *best_p;
1831 struct MeshPeerPath *p;
1832 unsigned int best_cost;
1835 GNUNET_PEER_resolve(peer->id, &id);
1837 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1840 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1841 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(
1845 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1848 if (NULL != (p = peer->path_head))
1851 best_cost = path_get_cost(t->tree, p);
1854 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1861 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1862 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1863 t->path_refresh_task =
1864 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1868 /* Start a DHT get if necessary */
1869 peer_info_connect(peer, t);
1874 * Add a path to a tunnel which we don't own, just to remember the next hop.
1875 * If destination node was already in the tunnel, the first hop information
1876 * will be replaced with the new path.
1878 * @param t Tunnel we want to add a new peer to
1879 * @param p Path to add
1880 * @param own_pos Position of local node in path.
1884 tunnel_add_path (struct MeshTunnel *t,
1885 struct MeshPeerPath *p,
1886 unsigned int own_pos)
1888 struct GNUNET_PeerIdentity id;
1890 GNUNET_assert (0 != own_pos);
1891 tree_add_path(t->tree, p, NULL);
1892 if (NULL == t->tree->me)
1893 t->tree->me = tree_find_peer(t->tree->root, p->peers[own_pos]);
1894 if (own_pos < p->length - 1)
1896 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1897 tree_update_first_hops(t->tree, t->tree->me, &id);
1903 * Notifies a tunnel that a connection has broken that affects at least
1904 * some of its peers. Sends a notification towards the root of the tree.
1905 * In case the peer is the owner of the tree, notifies the client that owns
1906 * the tunnel and tries to reconnect.
1908 * @param t Tunnel affected.
1909 * @param p1 Peer that got disconnected from p2.
1910 * @param p2 Peer that got disconnected from p1.
1912 * @return Short ID of the peer disconnected (either p1 or p2).
1913 * 0 if the tunnel remained unaffected.
1915 static GNUNET_PEER_Id
1916 tunnel_notify_connection_broken (struct MeshTunnel *t,
1922 pid = tree_notify_connection_broken (t->tree,
1925 ¬ify_peer_disconnected);
1926 if (myid != p1 && myid != p2)
1932 if (NULL != t->tree->me->parent)
1934 /* We are the peer still connected, notify owner of the disconnection. */
1935 struct GNUNET_MESH_PathBroken msg;
1936 struct GNUNET_PeerIdentity neighbor;
1938 msg.header.size = htons (sizeof (msg));
1939 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1940 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1941 msg.tid = htonl (t->id.tid);
1942 msg.peer1 = my_full_id;
1943 GNUNET_PEER_resolve (pid, &msg.peer2);
1944 GNUNET_PEER_resolve (t->tree->me->parent->peer, &neighbor);
1945 send_message (&msg.header, &neighbor);
1953 * Send a message in a tunnel in multicast, sending a copy to each child node
1954 * down the local one in the tunnel tree.
1956 * @param t Tunnel in which to send the data.
1957 * @param msg Message to be sent
1959 * @return Number of copies sent.
1961 * TODO: unifiy shared resources and reference counter management
1964 tunnel_send_multicast (struct MeshTunnel *t,
1965 const struct GNUNET_MessageHeader *msg)
1967 struct GNUNET_PeerIdentity neighbor;
1968 struct MeshDataDescriptor *info;
1969 struct MeshTunnelTreeNode *n;
1970 struct MeshTunnelTreeNode *counter;
1971 GNUNET_SCHEDULER_TaskIdentifier *task;
1972 unsigned int *copies;
1977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1978 "MESH: sending a multicast packet...\n");
1979 size = ntohs (msg->size);
1980 GNUNET_assert (NULL != t->tree->me);
1981 n = t->tree->me->children_head;
1984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1985 "MESH: no children in the tree, no one to send.\n");
1988 copies = GNUNET_malloc (sizeof (unsigned int));
1989 for (counter = n; NULL != counter; counter = counter->next)
1991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (%u copies)\n", *copies);
1992 data = GNUNET_malloc (size);
1993 memcpy (data, msg, size);
1994 if (NULL != t->client)
1996 task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
1997 *task = GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT,
2002 task = NULL; // So GCC shuts up about task being potentially uninitialized
2005 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2008 info->copies = copies;
2009 if (NULL != t->client)
2011 info->client = t->client->handle;
2012 info->timeout_task = task;
2014 info->destination = n->peer;
2015 GNUNET_PEER_resolve (n->peer, &neighbor);
2016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2017 "MESH: sending to %s...\n",
2018 GNUNET_i2s (&neighbor));
2019 info->peer = peer_info_get(&neighbor);
2020 GNUNET_assert (NULL != info->peer);
2021 i = peer_info_transmit_slot(info->peer);
2022 info->handler_n = i;
2023 info->peer->infos[i] = info;
2024 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2025 info->peer->core_transmit[i] =
2026 GNUNET_CORE_notify_transmit_ready (core_handle,
2029 GNUNET_TIME_UNIT_FOREVER_REL,
2032 &send_core_data_multicast, info);
2040 * Send a message to all peers in this tunnel that the tunnel is no longer
2043 * @param t The tunnel whose peers to notify.
2046 tunnel_send_destroy (struct MeshTunnel *t)
2048 struct GNUNET_MESH_TunnelDestroy msg;
2050 msg.header.size = htons (sizeof (msg));
2051 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2052 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2053 msg.tid = htonl (t->id.tid);
2054 tunnel_send_multicast (t, &msg.header);
2060 * Destroy the tunnel and free any allocated resources linked to it
2062 * @param t the tunnel to destroy
2064 * @return GNUNET_OK on success
2067 tunnel_destroy (struct MeshTunnel *t)
2069 struct MeshClient *c;
2070 struct MeshQueue *q;
2071 struct MeshQueue *qn;
2072 GNUNET_HashCode hash;
2081 struct GNUNET_PeerIdentity id;
2083 GNUNET_PEER_resolve(t->id.oid, &id);
2084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2085 "MESH: destroying tunnel %s [%x]\n",
2089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2093 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2094 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2099 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2101 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
2105 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2107 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2108 GNUNET_break (GNUNET_YES ==
2109 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t));
2112 if (NULL != t->peers)
2114 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
2115 &peer_info_delete_tunnel, t);
2116 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2121 if (NULL != q->data)
2122 GNUNET_free (q->data);
2126 /* TODO cancel core transmit ready in case it was active */
2128 tree_destroy(t->tree);
2129 if (NULL != t->dht_get_type)
2130 GNUNET_DHT_get_stop(t->dht_get_type);
2131 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2132 GNUNET_SCHEDULER_cancel(t->timeout_task);
2133 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2134 GNUNET_SCHEDULER_cancel(t->path_refresh_task);
2141 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2142 * that are no longer needed, as well as nodes of no longer reachable peers.
2143 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2145 * @param t Tunnel from which to remove the path.
2146 * @param p Peer which should be removed.
2149 tunnel_delete_peer (struct MeshTunnel *t,
2150 GNUNET_PEER_Id peer)
2152 GNUNET_break (GNUNET_OK == tree_del_peer (t->tree, peer, NULL));
2153 if (NULL == t->tree->root)
2159 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2160 * client when the client disconnects.
2162 * @param cls closure (client that is disconnecting)
2163 * @param key the hash of the local tunnel id (used to access the hashmap)
2164 * @param value the value stored at the key (tunnel to destroy)
2166 * @return GNUNET_OK on success
2169 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2171 struct MeshTunnel *t = value;
2174 if (NULL != t->dht_get_type)
2176 GNUNET_DHT_get_stop (t->dht_get_type);
2178 r = tunnel_destroy (t);
2184 * Timeout function, destroys tunnel if called
2186 * @param cls Closure (tunnel to destroy).
2187 * @param tc TaskContext
2190 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2192 struct MeshTunnel *t = cls;
2194 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2196 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2201 * Resets the tunnel timeout. Starts it if no timeout was running.
2203 * @param t Tunnel whose timeout to reset.
2206 tunnel_reset_timeout (struct MeshTunnel *t)
2208 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2209 GNUNET_SCHEDULER_cancel (t->timeout_task);
2210 t->timeout_task = GNUNET_SCHEDULER_add_delayed (
2211 GNUNET_TIME_relative_multiply(REFRESH_PATH_TIME, 4),
2217 /******************************************************************************/
2218 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2219 /******************************************************************************/
2222 * Function called to notify a client about the socket
2223 * being ready to queue more data. "buf" will be
2224 * NULL and "size" zero if the socket was closed for
2225 * writing in the meantime.
2227 * @param cls closure
2228 * @param size number of bytes available in buf
2229 * @param buf where the callee should write the message
2230 * @return number of bytes written to buf
2233 send_core_create_path (void *cls, size_t size, void *buf)
2235 struct MeshPathInfo *info = cls;
2236 struct GNUNET_MESH_ManipulatePath *msg;
2237 struct GNUNET_PeerIdentity *peer_ptr;
2238 struct MeshPeerInfo *peer = info->peer;
2239 struct MeshTunnel *t = info->t;
2240 struct MeshPeerPath *p = info->path;
2244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2245 "MESH: CREATE PATH sending...\n");
2247 sizeof (struct GNUNET_MESH_ManipulatePath) +
2248 p->length * sizeof (struct GNUNET_PeerIdentity);
2250 if (size < size_needed || NULL == buf)
2252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
2256 info->peer->core_transmit[info->pos] =
2257 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2258 GNUNET_TIME_UNIT_FOREVER_REL,
2259 path_get_first_hop (t->tree, peer->id),
2261 &send_core_create_path,
2265 info->peer->core_transmit[info->pos] = NULL;
2268 struct GNUNET_PeerIdentity id;
2270 GNUNET_PEER_resolve (peer->id, &id);
2271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2272 "MESH: setting core_transmit %s [%u] to NULL\n",
2277 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2278 msg->header.size = htons (size_needed);
2279 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2280 msg->tid = ntohl (t->id.tid);
2282 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2283 for (i = 0; i < p->length; i++)
2285 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2292 "MESH: CREATE PATH (%u bytes long) sent!\n",
2299 * Function called to notify a client about the socket
2300 * being ready to queue more data. "buf" will be
2301 * NULL and "size" zero if the socket was closed for
2302 * writing in the meantime.
2304 * @param cls closure (data itself)
2305 * @param size number of bytes available in buf
2306 * @param buf where the callee should write the message
2308 * @return number of bytes written to buf
2311 send_core_data_multicast (void *cls, size_t size, void *buf)
2313 struct MeshDataDescriptor *info = cls;
2316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2317 GNUNET_assert (NULL != info);
2318 GNUNET_assert (NULL != info->peer);
2319 total_size = info->size;
2320 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2322 if (total_size > size)
2325 struct GNUNET_PeerIdentity id;
2327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2328 "MESH: Multicast: retransmitting... (%u/%u)\n",
2330 GNUNET_PEER_resolve(info->peer->id, &id);
2331 info->peer->core_transmit[info->handler_n] =
2332 GNUNET_CORE_notify_transmit_ready (core_handle,
2335 GNUNET_TIME_UNIT_FOREVER_REL,
2338 &send_core_data_multicast,
2342 info->peer->core_transmit[info->handler_n] = NULL;
2343 info->peer->infos[info->handler_n] = NULL;
2344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2345 memcpy (buf, info->data, total_size);
2348 struct GNUNET_MESH_Multicast *mc;
2349 struct GNUNET_MessageHeader *mh;
2352 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2354 mc = (struct GNUNET_MESH_Multicast *) mh;
2355 mh = (struct GNUNET_MessageHeader *) &mc[1];
2356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2357 "MESH: multicast, payload type %u\n",
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2360 "MESH: multicast, payload size %u\n",
2365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2371 data_descriptor_decrement_multicast (info);
2372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2380 * Function called to notify a client about the socket
2381 * being ready to queue more data. "buf" will be
2382 * NULL and "size" zero if the socket was closed for
2383 * writing in the meantime.
2385 * @param cls closure (MeshDataDescriptor)
2386 * @param size number of bytes available in buf
2387 * @param buf where the callee should write the message
2388 * @return number of bytes written to buf
2391 send_core_path_ack (void *cls, size_t size, void *buf)
2393 struct MeshDataDescriptor *info = cls;
2394 struct GNUNET_MESH_PathACK *msg = buf;
2396 GNUNET_assert (NULL != info);
2399 info->peer->core_transmit[info->handler_n] = NULL;
2401 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2406 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2407 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2408 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2409 msg->tid = htonl (info->origin->tid);
2410 msg->peer_id = my_full_id;
2412 /* TODO add signature */
2414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2415 return sizeof (struct GNUNET_MESH_PathACK);
2419 /******************************************************************************/
2420 /******************** MESH NETWORK HANDLERS **************************/
2421 /******************************************************************************/
2425 * Core handler for path creation
2427 * @param cls closure
2428 * @param message message
2429 * @param peer peer identity this notification is about
2430 * @param atsi performance data
2431 * @param atsi_count number of records in 'atsi'
2433 * @return GNUNET_OK to keep the connection open,
2434 * GNUNET_SYSERR to close it (signal serious error)
2437 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2438 const struct GNUNET_MessageHeader *message,
2439 const struct GNUNET_ATS_Information *atsi,
2440 unsigned int atsi_count)
2442 unsigned int own_pos;
2445 MESH_TunnelNumber tid;
2446 struct GNUNET_MESH_ManipulatePath *msg;
2447 struct GNUNET_PeerIdentity *pi;
2448 GNUNET_HashCode hash;
2449 struct MeshPeerPath *path;
2450 struct MeshPeerInfo *dest_peer_info;
2451 struct MeshPeerInfo *orig_peer_info;
2452 struct MeshTunnel *t;
2454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2455 "MESH: Received a path create msg [%s]\n",
2456 GNUNET_i2s(&my_full_id));
2457 size = ntohs (message->size);
2458 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2460 GNUNET_break_op (0);
2464 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2465 if (size % sizeof (struct GNUNET_PeerIdentity))
2467 GNUNET_break_op (0);
2470 size /= sizeof (struct GNUNET_PeerIdentity);
2473 GNUNET_break_op (0);
2476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2477 "MESH: path has %u hops.\n",
2479 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2481 tid = ntohl (msg->tid);
2482 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2483 t = tunnel_get (pi, tid);
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2485 "MESH: path is for tunnel %s [%X].\n",
2490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2491 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2492 t->id.oid = GNUNET_PEER_intern (pi);
2494 t->local_tid = next_local_tid++;
2495 /* FIXME test if taken */
2496 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2497 t->tree = tree_new(t, t->id.oid);
2499 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2501 GNUNET_CONTAINER_multihashmap_put (
2505 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2511 tunnel_reset_timeout (t);
2512 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2514 GNUNET_CONTAINER_multihashmap_put (
2518 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2526 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2527 if (NULL == dest_peer_info)
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2530 "MESH: Creating PeerInfo for destination.\n");
2531 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2532 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2533 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2535 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2537 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2538 if (NULL == orig_peer_info)
2540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2541 "MESH: Creating PeerInfo for origin.\n");
2542 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2543 orig_peer_info->id = GNUNET_PEER_intern (pi);
2544 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2545 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2548 path = path_new (size);
2550 for (i = 0; i < size; i++)
2552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2553 "MESH: ... adding %s\n",
2554 GNUNET_i2s(&pi[i]));
2555 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2556 if (path->peers[i] == myid)
2559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2560 "MESH: Own position: %u\n", own_pos);
2563 /* cannot be self, must be 'not found' */
2564 /* create path: self not found in path through self */
2565 GNUNET_break_op (0);
2566 path_destroy (path);
2567 /* FIXME error. destroy tunnel? leave for timeout? */
2570 tunnel_add_path (t, path, own_pos);
2571 if (own_pos == size - 1)
2573 /* It is for us! Send ack. */
2574 struct GNUNET_MESH_TunnelNotification cmsg;
2575 struct MeshDataDescriptor *info;
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2579 "MESH: It's for us!\n");
2580 path_add_to_origin (orig_peer_info, path, GNUNET_NO);
2581 if (NULL == t->peers)
2582 t->peers = GNUNET_CONTAINER_multihashmap_create(4);
2583 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2585 &my_full_id.hashPubKey,
2586 peer_info_get(&my_full_id),
2587 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2588 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2589 info->origin = &t->id;
2590 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2591 GNUNET_assert (NULL != info->peer);
2592 j = peer_info_transmit_slot(info->peer);
2593 info->handler_n = j;
2594 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2595 info->peer->infos[j] = info;
2596 info->peer->core_transmit[j] =
2597 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2598 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2599 sizeof (struct GNUNET_MESH_PathACK),
2600 &send_core_path_ack, info);
2601 cmsg.header.size = htons(sizeof(cmsg));
2602 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2603 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2604 cmsg.tunnel_id = htonl(t->local_tid);
2605 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2609 struct MeshPeerPath *path2;
2611 /* It's for somebody else! Retransmit. */
2612 path2 = path_duplicate(path);
2613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2614 "MESH: Retransmitting.\n");
2615 path_add_to_peer(dest_peer_info, path, GNUNET_NO);
2616 path = path_duplicate(path2);
2617 path_add_to_origin(orig_peer_info, path2, GNUNET_NO);
2618 send_create_path(dest_peer_info, path, t);
2625 * Core handler for path destruction
2627 * @param cls closure
2628 * @param message message
2629 * @param peer peer identity this notification is about
2630 * @param atsi performance data
2631 * @param atsi_count number of records in 'atsi'
2633 * @return GNUNET_OK to keep the connection open,
2634 * GNUNET_SYSERR to close it (signal serious error)
2637 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2638 const struct GNUNET_MessageHeader *message,
2639 const struct GNUNET_ATS_Information *atsi,
2640 unsigned int atsi_count)
2642 struct GNUNET_MESH_ManipulatePath *msg;
2643 struct GNUNET_PeerIdentity *pi;
2644 struct MeshPeerPath *path;
2645 struct MeshTunnel *t;
2646 unsigned int own_pos;
2650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2651 "MESH: Received a PATH DESTROY msg from %s\n",
2653 size = ntohs (message->size);
2654 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2656 GNUNET_break_op (0);
2660 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2661 if (size % sizeof (struct GNUNET_PeerIdentity))
2663 GNUNET_break_op (0);
2666 size /= sizeof (struct GNUNET_PeerIdentity);
2669 GNUNET_break_op (0);
2672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2673 "MESH: path has %u hops.\n",
2676 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2677 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2679 "MESH: path is for tunnel %s [%X].\n",
2682 t = tunnel_get (pi, ntohl (msg->tid));
2685 /* TODO notify back: we don't know this tunnel */
2686 GNUNET_break_op (0);
2689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2690 path = path_new (size);
2692 for (i = 0; i < size; i++)
2694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2695 "MESH: ... adding %s\n",
2696 GNUNET_i2s(&pi[i]));
2697 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2698 if (path->peers[i] == myid)
2701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2702 "MESH: Own position: %u\n", own_pos);
2703 if (own_pos < path->length - 1)
2704 send_message (message, &pi[own_pos + 1]);
2705 tunnel_delete_peer (t, path->peers[path->length - 1]);
2706 path_destroy (path);
2712 * Core handler for notifications of broken paths
2714 * @param cls closure
2715 * @param message message
2716 * @param peer peer identity this notification is about
2717 * @param atsi performance data
2718 * @param atsi_count number of records in 'atsi'
2720 * @return GNUNET_OK to keep the connection open,
2721 * GNUNET_SYSERR to close it (signal serious error)
2724 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
2725 const struct GNUNET_MessageHeader *message,
2726 const struct GNUNET_ATS_Information *atsi,
2727 unsigned int atsi_count)
2729 struct GNUNET_MESH_PathBroken *msg;
2730 struct MeshTunnel *t;
2732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2733 "MESH: Received a PATH BROKEN msg from %s\n",
2735 msg = (struct GNUNET_MESH_PathBroken *) message;
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2737 "MESH: regarding %s\n",
2738 GNUNET_i2s(&msg->peer1));
2739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2740 "MESH: regarding %s\n",
2741 GNUNET_i2s(&msg->peer2));
2742 t = tunnel_get(&msg->oid, ntohl (msg->tid));
2745 GNUNET_break_op (0);
2748 tunnel_notify_connection_broken(t,
2749 GNUNET_PEER_search(&msg->peer1),
2750 GNUNET_PEER_search(&msg->peer2));
2757 * Core handler for tunnel destruction
2759 * @param cls closure
2760 * @param message message
2761 * @param peer peer identity this notification is about
2762 * @param atsi performance data
2763 * @param atsi_count number of records in 'atsi'
2765 * @return GNUNET_OK to keep the connection open,
2766 * GNUNET_SYSERR to close it (signal serious error)
2769 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2770 const struct GNUNET_MessageHeader *message,
2771 const struct GNUNET_ATS_Information *atsi,
2772 unsigned int atsi_count)
2774 struct GNUNET_MESH_TunnelDestroy *msg;
2775 struct MeshTunnel *t;
2777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2778 "MESH: Got a TUNNEL DESTROY packet from %s\n",
2780 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2782 "MESH: for tunnel %s [%u]\n",
2783 GNUNET_i2s (&msg->oid),
2785 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2788 /* Probably already got the message from another path,
2789 * destroyed the tunnel and retransmitted to children.
2794 if (t->id.oid == myid)
2796 GNUNET_break_op (0);
2799 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2801 /* Tunnel was incoming, notify clients */
2802 send_clients_tunnel_destroy (t);
2804 tunnel_send_destroy (t);
2811 * Core handler for mesh network traffic going from the origin to a peer
2813 * @param cls closure
2814 * @param peer peer identity this notification is about
2815 * @param message message
2816 * @param atsi performance data
2817 * @param atsi_count number of records in 'atsi'
2818 * @return GNUNET_OK to keep the connection open,
2819 * GNUNET_SYSERR to close it (signal serious error)
2822 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2823 const struct GNUNET_MessageHeader *message,
2824 const struct GNUNET_ATS_Information *atsi,
2825 unsigned int atsi_count)
2827 struct GNUNET_MESH_Unicast *msg;
2828 struct MeshTunnel *t;
2832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2833 "MESH: got a unicast packet from %s\n",
2835 size = ntohs (message->size);
2837 sizeof (struct GNUNET_MESH_Unicast) +
2838 sizeof (struct GNUNET_MessageHeader))
2843 msg = (struct GNUNET_MESH_Unicast *) message;
2844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2845 "MESH: of type %u\n",
2846 ntohs (msg[1].header.type));
2847 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2850 /* TODO notify back: we don't know this tunnel */
2851 GNUNET_break_op (0);
2854 tunnel_reset_timeout (t);
2855 pid = GNUNET_PEER_search(&msg->destination);
2858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2859 "MESH: it's for us! sending to clients...\n");
2860 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2864 "MESH: not for us, retransmitting...\n");
2865 send_message (message, path_get_first_hop(t->tree, pid));
2871 * Core handler for mesh network traffic going from the origin to all peers
2873 * @param cls closure
2874 * @param message message
2875 * @param peer peer identity this notification is about
2876 * @param atsi performance data
2877 * @param atsi_count number of records in 'atsi'
2878 * @return GNUNET_OK to keep the connection open,
2879 * GNUNET_SYSERR to close it (signal serious error)
2881 * TODO: Check who we got this from, to validate route.
2884 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2885 const struct GNUNET_MessageHeader *message,
2886 const struct GNUNET_ATS_Information *atsi,
2887 unsigned int atsi_count)
2889 struct GNUNET_MESH_Multicast *msg;
2890 struct MeshTunnel *t;
2893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2894 "MESH: got a multicast packet from %s\n",
2896 size = ntohs (message->size);
2897 if (sizeof (struct GNUNET_MESH_Multicast) +
2898 sizeof (struct GNUNET_MessageHeader) > size)
2900 GNUNET_break_op (0);
2903 msg = (struct GNUNET_MESH_Multicast *) message;
2904 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2908 /* TODO notify that we dont know that tunnel */
2909 GNUNET_break_op (0);
2912 tunnel_reset_timeout (t);
2914 /* Transmit to locally interested clients */
2915 if (NULL != t->peers &&
2916 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2918 send_subscribed_clients (message, &msg[1].header);
2920 tunnel_send_multicast(t, message);
2926 * Core handler for mesh network traffic toward the owner of a tunnel
2928 * @param cls closure
2929 * @param message message
2930 * @param peer peer identity this notification is about
2931 * @param atsi performance data
2932 * @param atsi_count number of records in 'atsi'
2934 * @return GNUNET_OK to keep the connection open,
2935 * GNUNET_SYSERR to close it (signal serious error)
2938 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2939 const struct GNUNET_MessageHeader *message,
2940 const struct GNUNET_ATS_Information *atsi,
2941 unsigned int atsi_count)
2943 struct GNUNET_MESH_ToOrigin *msg;
2944 struct GNUNET_PeerIdentity id;
2945 struct MeshPeerInfo *peer_info;
2946 struct MeshTunnel *t;
2949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2950 "MESH: got a ToOrigin packet from %s\n",
2952 size = ntohs (message->size);
2953 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2954 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2956 GNUNET_break_op (0);
2959 msg = (struct GNUNET_MESH_ToOrigin *) message;
2960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2961 "MESH: of type %u\n",
2962 ntohs (msg[1].header.type));
2963 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2967 /* TODO notify that we dont know this tunnel (whom)? */
2968 GNUNET_break_op (0);
2972 if (t->id.oid == myid)
2975 struct GNUNET_MESH_ToOrigin *copy;
2977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2978 "MESH: it's for us! sending to clients...\n");
2979 if (NULL == t->client)
2981 /* got data packet for ownerless tunnel */
2982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2983 "MESH: no clients!\n");
2984 GNUNET_break_op (0);
2987 /* TODO signature verification */
2988 memcpy (cbuf, message, size);
2989 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2990 copy->tid = htonl (t->local_tid);
2991 GNUNET_SERVER_notification_context_unicast (nc,
2997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2998 "MESH: not for us, retransmitting...\n");
3000 peer_info = peer_info_get (&msg->oid);
3001 if (NULL == peer_info)
3003 /* unknown origin of tunnel */
3007 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
3008 send_message (message, &id);
3015 * Core handler for path ACKs
3017 * @param cls closure
3018 * @param message message
3019 * @param peer peer identity this notification is about
3020 * @param atsi performance data
3021 * @param atsi_count number of records in 'atsi'
3023 * @return GNUNET_OK to keep the connection open,
3024 * GNUNET_SYSERR to close it (signal serious error)
3027 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3028 const struct GNUNET_MessageHeader *message,
3029 const struct GNUNET_ATS_Information *atsi,
3030 unsigned int atsi_count)
3032 struct GNUNET_MESH_PathACK *msg;
3033 struct GNUNET_PeerIdentity id;
3034 struct MeshTunnelTreeNode *n;
3035 struct MeshPeerInfo *peer_info;
3036 struct MeshTunnel *t;
3038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3039 "MESH: Received a path ACK msg [%s]\n",
3040 GNUNET_i2s(&my_full_id));
3041 msg = (struct GNUNET_MESH_PathACK *) message;
3042 t = tunnel_get (&msg->oid, msg->tid);
3045 /* TODO notify that we don't know the tunnel */
3049 /* Message for us? */
3050 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
3053 if (NULL == t->client)
3055 GNUNET_break_op (0);
3058 if (NULL != t->dht_get_type)
3060 GNUNET_DHT_get_stop (t->dht_get_type);
3061 t->dht_get_type = NULL;
3063 peer_info = peer_info_get (&msg->peer_id);
3064 n = tree_find_peer(t->tree->root, peer_info->id);
3067 GNUNET_break_op (0);
3070 n->status = MESH_PEER_READY;
3071 send_client_peer_connected(t, peer_info->id);
3075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3076 "MESH: not for us, retransmitting...\n");
3077 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
3078 peer_info = peer_info_get (&msg->oid);
3079 if (NULL == peer_info)
3081 /* If we know the tunnel, we should DEFINITELY know the peer */
3085 send_message (message, &id);
3091 * Functions to handle messages from core
3093 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3094 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3095 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3096 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3097 sizeof (struct GNUNET_MESH_PathBroken)},
3098 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3099 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3100 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3101 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3102 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3103 sizeof (struct GNUNET_MESH_PathACK)},
3109 /******************************************************************************/
3110 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3111 /******************************************************************************/
3114 * deregister_app: iterator for removing each application registered by a client
3116 * @param cls closure
3117 * @param key the hash of the application id (used to access the hashmap)
3118 * @param value the value stored at the key (client)
3120 * @return GNUNET_OK on success
3123 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3125 GNUNET_break (GNUNET_YES ==
3126 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
3132 * notify_client_connection_failure: notify a client that the connection to the
3133 * requested remote peer is not possible (for instance, no route found)
3134 * Function called when the socket is ready to queue more data. "buf" will be
3135 * NULL and "size" zero if the socket was closed for writing in the meantime.
3137 * @param cls closure
3138 * @param size number of bytes available in buf
3139 * @param buf where the callee should write the message
3140 * @return number of bytes written to buf
3143 notify_client_connection_failure (void *cls, size_t size, void *buf)
3146 struct MeshPeerInfo *peer_info;
3147 struct GNUNET_MESH_PeerControl *msg;
3148 struct GNUNET_PeerIdentity id;
3150 if (0 == size && NULL == buf)
3152 // TODO retry? cancel?
3156 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3157 peer_info = (struct MeshPeerInfo *) cls;
3158 msg = (struct GNUNET_MESH_PeerControl *) buf;
3159 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3160 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3161 // msg->tunnel_id = htonl(peer_info->t->tid);
3162 GNUNET_PEER_resolve (peer_info->id, &id);
3163 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3171 * Send keepalive packets for a peer
3173 * @param cls Closure (tunnel for which to send the keepalive).
3174 * @param tc Notification context.
3176 * TODO: implement explicit multicast keepalive?
3179 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3181 struct MeshTunnel *t = cls;
3182 struct GNUNET_MessageHeader *payload;
3183 struct GNUNET_MESH_Multicast *msg;
3184 size_t size = sizeof(struct GNUNET_MESH_Multicast) +
3185 sizeof(struct GNUNET_MessageHeader);
3188 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3192 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3195 "MESH: sending keepalive for tunnel %d\n",
3198 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3199 msg->header.size = htons (size);
3200 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3201 msg->oid = my_full_id;
3202 msg->tid = htonl(t->id.tid);
3203 payload = (struct GNUNET_MessageHeader *) &msg[1];
3204 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
3205 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3206 tunnel_send_multicast (t, &msg->header);
3208 t->path_refresh_task =
3209 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
3215 * Function to process paths received for a new peer addition. The recorded
3216 * paths form the initial tunnel, which can be optimized later.
3217 * Called on each result obtained for the DHT search.
3219 * @param cls closure
3220 * @param exp when will this value expire
3221 * @param key key of the result
3222 * @param type type of the result
3223 * @param size number of bytes in data
3224 * @param data pointer to the result data
3226 * TODO: re-issue the request after certain time? cancel after X results?
3229 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3230 const GNUNET_HashCode * key,
3231 const struct GNUNET_PeerIdentity *get_path,
3232 unsigned int get_path_length,
3233 const struct GNUNET_PeerIdentity *put_path,
3234 unsigned int put_path_length,
3235 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
3237 struct MeshPathInfo *path_info = cls;
3238 struct MeshPeerPath *p;
3239 struct GNUNET_PeerIdentity pi;
3242 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3243 "MESH: Got results from DHT!\n");
3244 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3245 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3248 // GNUNET_DHT_get_stop(path_info->peer->dhtget);
3249 // path_info->peer->dhtget = NULL;
3251 p = path_build_from_dht (get_path, get_path_length,
3252 put_path, put_path_length);
3253 path_add_to_peer (path_info->peer, p, GNUNET_NO);
3254 for (i = 0; i < path_info->peer->ntunnels; i++)
3256 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3257 peer_info_connect(path_info->peer, path_info->t);
3259 // GNUNET_free (path_info);
3266 * Function to process paths received for a new peer addition. The recorded
3267 * paths form the initial tunnel, which can be optimized later.
3268 * Called on each result obtained for the DHT search.
3270 * @param cls closure
3271 * @param exp when will this value expire
3272 * @param key key of the result
3273 * @param type type of the result
3274 * @param size number of bytes in data
3275 * @param data pointer to the result data
3278 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3279 const GNUNET_HashCode * key,
3280 const struct GNUNET_PeerIdentity *get_path,
3281 unsigned int get_path_length,
3282 const struct GNUNET_PeerIdentity *put_path,
3283 unsigned int put_path_length,
3284 enum GNUNET_BLOCK_Type type, size_t size,
3287 const struct GNUNET_PeerIdentity *pi = data;
3288 struct MeshTunnel *t = cls;
3289 struct MeshPeerInfo *peer_info;
3290 struct MeshPeerPath *p;
3292 if (size != sizeof (struct GNUNET_PeerIdentity))
3294 GNUNET_break_op (0);
3297 GNUNET_assert (NULL != t->client);
3298 peer_info = peer_info_get (pi);
3299 (void) GNUNET_CONTAINER_multihashmap_put (
3303 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3305 p = path_build_from_dht (get_path, get_path_length,
3306 put_path, put_path_length);
3307 path_add_to_peer (peer_info, p, GNUNET_NO);
3308 tunnel_add_peer(t, peer_info);
3309 peer_info_connect (peer_info, t);
3313 /******************************************************************************/
3314 /********************* MESH LOCAL HANDLES **************************/
3315 /******************************************************************************/
3319 * Handler for client disconnection
3321 * @param cls closure
3322 * @param client identification of the client; NULL
3323 * for the last call when the server is destroyed
3326 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3328 struct MeshClient *c;
3329 struct MeshClient *next;
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3340 if (c->handle != client && NULL != client)
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
3347 GNUNET_SERVER_client_drop (c->handle);
3348 if (NULL != c->tunnels)
3350 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3351 &tunnel_destroy_iterator,
3353 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3356 /* deregister clients applications */
3357 if (NULL != c->apps)
3359 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3360 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3362 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3363 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3365 GNUNET_SCHEDULER_cancel (announce_applications_task);
3366 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3368 if (NULL != c->types)
3369 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3371 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT FREE at %p\n", c);
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3383 * Handler for new clients
3385 * @param cls closure
3386 * @param client identification of the client
3387 * @param message the actual message, which includes messages the client wants
3390 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3391 const struct GNUNET_MessageHeader *message)
3393 struct GNUNET_MESH_ClientConnect *cc_msg;
3394 struct MeshClient *c;
3395 GNUNET_MESH_ApplicationType *a;
3402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3403 /* Check data sanity */
3404 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3405 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3406 ntypes = ntohs (cc_msg->types);
3407 napps = ntohs (cc_msg->applications);
3409 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3416 /* Create new client structure */
3417 c = GNUNET_malloc (sizeof (struct MeshClient));
3419 c->id = next_client_id++;
3421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT NEW %u at %p\n", c->id, c);
3423 GNUNET_SERVER_client_keep (client);
3424 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3427 GNUNET_MESH_ApplicationType at;
3430 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3431 for (i = 0; i < napps; i++)
3434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3435 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3436 /* store in clients hashmap */
3437 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3438 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3439 /* store in global hashmap, for announcements */
3440 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3441 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3443 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3444 announce_applications_task =
3445 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3453 t = (uint16_t *) & a[napps];
3454 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3455 for (i = 0; i < ntypes; i++)
3458 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3460 /* store in clients hashmap */
3461 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3462 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3463 /* store in global hashmap */
3464 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3465 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3469 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3471 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3472 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3473 GNUNET_SERVER_notification_context_add (nc, client);
3475 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3483 * Handler for requests of new tunnels
3485 * @param cls closure
3486 * @param client identification of the client
3487 * @param message the actual message
3490 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3491 const struct GNUNET_MessageHeader *message)
3493 struct GNUNET_MESH_TunnelMessage *t_msg;
3494 struct MeshTunnel *t;
3495 struct MeshClient *c;
3496 GNUNET_HashCode hash;
3498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3500 /* Sanity check for client registration */
3501 if (NULL == (c = client_get (client)))
3504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3511 /* Message sanity check */
3512 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3515 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3519 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3520 /* Sanity check for tunnel numbering */
3521 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3524 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3527 /* Sanity check for duplicate tunnel IDs */
3528 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3535 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3536 while (NULL != tunnel_get_by_pi (myid, next_tid))
3537 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3538 t->id.tid = next_tid++;
3540 t->local_tid = ntohl (t_msg->tunnel_id);
3542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3543 "MESH: CREATED TUNNEL %s [%x] (%x)\n",
3544 GNUNET_i2s (&my_full_id),
3549 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3551 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3553 GNUNET_CONTAINER_multihashmap_put (
3554 c->tunnels, &hash, t,
3555 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3558 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3562 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3564 GNUNET_CONTAINER_multihashmap_put (
3566 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3572 t->tree = tree_new (t, myid);
3573 t->tree->refresh = REFRESH_PATH_TIME;
3574 t->tree->root->status = MESH_PEER_READY;
3575 t->tree->me = t->tree->root;
3577 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3583 * Handler for requests of deleting tunnels
3585 * @param cls closure
3586 * @param client identification of the client
3587 * @param message the actual message
3590 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3591 const struct GNUNET_MessageHeader *message)
3593 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3594 struct MeshClient *c;
3595 struct MeshTunnel *t;
3596 MESH_TunnelNumber tid;
3597 GNUNET_HashCode hash;
3599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3600 "MESH: Got a DESTROY TUNNEL from client!\n");
3602 /* Sanity check for client registration */
3603 if (NULL == (c = client_get (client)))
3606 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3609 /* Message sanity check */
3610 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3613 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3619 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3621 /* Retrieve tunnel */
3622 tid = ntohl (tunnel_msg->tunnel_id);
3624 /* Remove from local id hashmap */
3625 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3626 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3627 GNUNET_assert (GNUNET_YES ==
3628 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t));
3631 tunnel_send_destroy (t);
3633 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3639 * Handler for connection requests to new peers
3641 * @param cls closure
3642 * @param client identification of the client
3643 * @param message the actual message (PeerControl)
3646 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3647 const struct GNUNET_MessageHeader *message)
3649 struct GNUNET_MESH_PeerControl *peer_msg;
3650 struct MeshPeerInfo *peer_info;
3651 struct MeshClient *c;
3652 struct MeshTunnel *t;
3653 MESH_TunnelNumber tid;
3655 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3656 /* Sanity check for client registration */
3657 if (NULL == (c = client_get (client)))
3660 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3664 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3665 /* Sanity check for message size */
3666 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3669 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3673 /* Tunnel exists? */
3674 tid = ntohl (peer_msg->tunnel_id);
3675 t = tunnel_get_by_local_id (c, tid);
3679 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3683 /* Does client own tunnel? */
3684 if (t->client->handle != client)
3687 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3690 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3691 GNUNET_i2s(&peer_msg->peer));
3692 peer_info = peer_info_get (&peer_msg->peer);
3694 tunnel_add_peer(t, peer_info);
3695 peer_info_connect(peer_info, t);
3697 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3703 * Handler for disconnection requests of peers in a tunnel
3705 * @param cls closure
3706 * @param client identification of the client
3707 * @param message the actual message (PeerControl)
3710 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3711 const struct GNUNET_MessageHeader *message)
3713 struct GNUNET_MESH_PeerControl *peer_msg;
3714 struct MeshPeerInfo *peer_info;
3715 struct MeshClient *c;
3716 struct MeshTunnel *t;
3717 MESH_TunnelNumber tid;
3719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got a PEER DEL request\n");
3720 /* Sanity check for client registration */
3721 if (NULL == (c = client_get (client)))
3724 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3727 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3728 /* Sanity check for message size */
3729 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3732 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3736 /* Tunnel exists? */
3737 tid = ntohl (peer_msg->tunnel_id);
3738 t = tunnel_get_by_local_id (c, tid);
3742 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: on tunnel %X\n", t->id.tid);
3747 /* Does client own tunnel? */
3748 if (t->client->handle != client)
3751 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3756 "MESH: for peer %s\n",
3757 GNUNET_i2s(&peer_msg->peer));
3758 /* Is the peer in the tunnel? */
3759 peer_info = GNUNET_CONTAINER_multihashmap_get(t->peers,
3760 &peer_msg->peer.hashPubKey);
3761 if (NULL == peer_info)
3764 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3768 /* Ok, delete peer from tunnel */
3769 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3770 &peer_msg->peer.hashPubKey);
3772 send_destroy_path (t, peer_info->id);
3773 tunnel_delete_peer(t, peer_info->id);
3774 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3780 * Handler for connection requests to new peers by type
3782 * @param cls closure
3783 * @param client identification of the client
3784 * @param message the actual message (ConnectPeerByType)
3787 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3788 const struct GNUNET_MessageHeader *message)
3790 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3791 struct MeshClient *c;
3792 struct MeshTunnel *t;
3793 GNUNET_HashCode hash;
3794 MESH_TunnelNumber tid;
3796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3797 /* Sanity check for client registration */
3798 if (NULL == (c = client_get (client)))
3801 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3805 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3806 /* Sanity check for message size */
3807 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3808 ntohs (connect_msg->header.size))
3811 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3815 /* Tunnel exists? */
3816 tid = ntohl (connect_msg->tunnel_id);
3817 t = tunnel_get_by_local_id (c, tid);
3821 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3825 /* Does client own tunnel? */
3826 if (t->client->handle != client)
3829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3833 /* Do WE have the service? */
3834 t->type = ntohl (connect_msg->type);
3835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3836 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3837 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3840 /* Yes! Fast forward, add ourselves to the tunnel and send the
3841 * good news to the client
3843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3844 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3845 peer_info_get (&my_full_id),
3846 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3849 send_client_peer_connected(t, myid);
3850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3851 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3854 /* Ok, lets find a peer offering the service */
3855 if (NULL != t->dht_get_type)
3857 GNUNET_DHT_get_stop (t->dht_get_type);
3859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3860 GNUNET_h2s (&hash));
3862 GNUNET_DHT_get_start (dht_handle,
3863 GNUNET_TIME_UNIT_FOREVER_REL,
3864 GNUNET_BLOCK_TYPE_TEST,
3867 GNUNET_DHT_RO_RECORD_ROUTE |
3868 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3870 &dht_get_type_handler, t);
3872 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3878 * Handler for client traffic directed to one peer
3880 * @param cls closure
3881 * @param client identification of the client
3882 * @param message the actual message
3885 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3886 const struct GNUNET_MessageHeader *message)
3888 struct MeshClient *c;
3889 struct MeshTunnel *t;
3890 struct MeshPeerInfo *pi;
3891 struct GNUNET_MESH_Unicast *data_msg;
3892 MESH_TunnelNumber tid;
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3896 "MESH: Got a unicast request from a client!\n");
3898 /* Sanity check for client registration */
3899 if (NULL == (c = client_get (client)))
3902 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3905 data_msg = (struct GNUNET_MESH_Unicast *) message;
3906 /* Sanity check for message size */
3907 size = ntohs (message->size);
3908 if (sizeof (struct GNUNET_MESH_Unicast) +
3909 sizeof (struct GNUNET_MessageHeader) > size)
3912 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3916 /* Tunnel exists? */
3917 tid = ntohl (data_msg->tid);
3918 t = tunnel_get_by_local_id (c, tid);
3922 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3926 /* Is it a local tunnel? Then, does client own the tunnel? */
3927 if (NULL != t->client &&
3928 NULL != t->client->handle &&
3929 t->client->handle != client)
3932 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3936 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3937 &data_msg->destination.hashPubKey);
3938 /* Is the selected peer in the tunnel? */
3942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3946 /* Ok, everything is correct, send the message
3947 * (pretend we got it from a mesh peer)
3950 char buf[ntohs (message->size)];
3951 struct GNUNET_MESH_Unicast *copy;
3953 /* Work around const limitation */
3954 copy = (struct GNUNET_MESH_Unicast *) buf;
3955 memcpy (buf, data_msg, size);
3956 copy->oid = my_full_id;
3957 copy->tid = htonl (t->id.tid);
3958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3959 "MESH: calling generic handler...\n");
3960 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
3962 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3968 * Handler for client traffic directed to the origin
3970 * @param cls closure
3971 * @param client identification of the client
3972 * @param message the actual message
3975 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3976 const struct GNUNET_MessageHeader *message)
3978 struct GNUNET_MESH_ToOrigin *data_msg;
3979 struct GNUNET_PeerIdentity id;
3980 struct MeshClient *c;
3981 struct MeshTunnel *t;
3982 MESH_TunnelNumber tid;
3985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3986 "MESH: Got a ToOrigin request from a client!\n");
3988 /* Sanity check for client registration */
3989 if (NULL == (c = client_get (client)))
3992 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3995 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3996 /* Sanity check for message size */
3997 size = ntohs (message->size);
3998 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3999 sizeof (struct GNUNET_MessageHeader) > size)
4002 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4006 /* Tunnel exists? */
4007 tid = ntohl (data_msg->tid);
4008 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4011 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4014 t = tunnel_get_by_local_id (c, tid);
4018 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4022 /* It shouldn't be a local tunnel. */
4023 if (NULL != t->client)
4026 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4029 GNUNET_PEER_resolve(t->id.oid, &id);
4031 /* Ok, everything is correct, send the message
4032 * (pretend we got it from a mesh peer)
4035 char buf[ntohs (message->size)];
4036 struct GNUNET_MESH_ToOrigin *copy;
4038 /* Work around const limitation */
4039 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4040 memcpy (buf, data_msg, size);
4042 copy->tid = htonl (t->id.tid);
4043 copy->sender = my_full_id;
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4045 "MESH: calling generic handler...\n");
4046 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4048 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4054 * Handler for client traffic directed to all peers in a tunnel
4056 * @param cls closure
4057 * @param client identification of the client
4058 * @param message the actual message
4061 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4062 const struct GNUNET_MessageHeader *message)
4064 struct MeshClient *c;
4065 struct MeshTunnel *t;
4066 struct GNUNET_MESH_Multicast *data_msg;
4067 MESH_TunnelNumber tid;
4069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4070 "MESH: Got a multicast request from a client!\n");
4072 /* Sanity check for client registration */
4073 if (NULL == (c = client_get (client)))
4076 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4079 data_msg = (struct GNUNET_MESH_Multicast *) message;
4080 /* Sanity check for message size */
4081 if (sizeof (struct GNUNET_MESH_Multicast) +
4082 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4085 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4089 /* Tunnel exists? */
4090 tid = ntohl (data_msg->tid);
4091 t = tunnel_get_by_local_id (c, tid);
4095 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4099 /* Does client own tunnel? */
4100 if (t->client->handle != client)
4103 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4108 char buf[ntohs(message->size)];
4109 struct GNUNET_MESH_Multicast *copy;
4111 copy = (struct GNUNET_MESH_Multicast *)buf;
4112 memcpy(buf, message, ntohs(message->size));
4113 copy->oid = my_full_id;
4114 copy->tid = htonl(t->id.tid);
4115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4116 "MESH: calling generic handler...\n");
4117 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL, 0);
4120 /* receive done gets called when last copy is sent to a neighbor */
4125 * Functions to handle messages from clients
4127 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4128 {&handle_local_new_client, NULL,
4129 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4130 {&handle_local_tunnel_create, NULL,
4131 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4132 sizeof (struct GNUNET_MESH_TunnelMessage)},
4133 {&handle_local_tunnel_destroy, NULL,
4134 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4135 sizeof (struct GNUNET_MESH_TunnelMessage)},
4136 {&handle_local_connect_add, NULL,
4137 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4138 sizeof (struct GNUNET_MESH_PeerControl)},
4139 {&handle_local_connect_del, NULL,
4140 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4141 sizeof (struct GNUNET_MESH_PeerControl)},
4142 {&handle_local_connect_by_type, NULL,
4143 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4144 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4145 {&handle_local_unicast, NULL,
4146 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4147 {&handle_local_to_origin, NULL,
4148 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4149 {&handle_local_multicast, NULL,
4150 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4156 * To be called on core init/fail.
4158 * @param cls service closure
4159 * @param server handle to the server for this service
4160 * @param identity the public identity of this peer
4163 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4164 const struct GNUNET_PeerIdentity *identity)
4166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
4167 core_handle = server;
4168 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
4170 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
4171 GNUNET_SCHEDULER_shutdown();
4177 * Method called whenever a given peer connects.
4179 * @param cls closure
4180 * @param peer peer identity this notification is about
4181 * @param atsi performance data for the connection
4182 * @param atsi_count number of records in 'atsi'
4185 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4186 const struct GNUNET_ATS_Information *atsi,
4187 unsigned int atsi_count)
4189 struct MeshPeerInfo *peer_info;
4190 struct MeshPeerPath *path;
4192 #if MESH_DEBUG_CONNECTION
4193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
4194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4195 GNUNET_i2s(&my_full_id));
4197 peer_info = peer_info_get (peer);
4198 if (myid == peer_info->id)
4200 #if MESH_DEBUG_CONNECTION
4201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4205 #if MESH_DEBUG_CONNECTION
4208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4212 path = path_new (2);
4213 path->peers[0] = myid;
4214 path->peers[1] = peer_info->id;
4215 GNUNET_PEER_change_rc(myid, 1);
4216 GNUNET_PEER_change_rc(peer_info->id, 1);
4217 path_add_to_peer (peer_info, path, GNUNET_YES);
4222 * Method called whenever a peer disconnects.
4224 * @param cls closure
4225 * @param peer peer identity this notification is about
4228 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4230 struct MeshPeerInfo *pi;
4233 #if MESH_DEBUG_CONNECTION
4234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
4236 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4242 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4244 /* TODO: notify that the transmission failed */
4245 peer_info_cancel_transmission(pi, i);
4247 path_remove_from_peer (pi, pi->id, myid);
4248 #if MESH_DEBUG_CONNECTION
4251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4258 /******************************************************************************/
4259 /************************ MAIN FUNCTIONS ****************************/
4260 /******************************************************************************/
4263 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4265 * @param cls closure
4266 * @param key current key code
4267 * @param value value in the hash map
4268 * @return GNUNET_YES if we should continue to iterate,
4272 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4274 struct MeshTunnel *t = value;
4280 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4282 * @param cls closure
4283 * @param key current key code
4284 * @param value value in the hash map
4285 * @return GNUNET_YES if we should continue to iterate,
4289 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4291 struct MeshPeerInfo *p = value;
4292 peer_info_destroy (p);
4297 * Task run during shutdown.
4303 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4306 /* TODO: destroy tunnels? */
4307 if (core_handle != NULL)
4309 GNUNET_CORE_disconnect (core_handle);
4312 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
4313 GNUNET_CONTAINER_multihashmap_iterate(peers, &shutdown_peer, NULL);
4314 if (dht_handle != NULL)
4316 GNUNET_DHT_disconnect (dht_handle);
4321 GNUNET_SERVER_notification_context_destroy (nc);
4324 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4326 GNUNET_SCHEDULER_cancel (announce_id_task);
4327 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4333 * Process mesh requests.
4335 * @param cls closure
4336 * @param server the initialized server
4337 * @param c configuration to use
4340 run (void *cls, struct GNUNET_SERVER_Handle *server,
4341 const struct GNUNET_CONFIGURATION_Handle *c)
4343 struct MeshPeerInfo *peer;
4344 struct MeshPeerPath *p;
4347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4348 server_handle = server;
4349 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4350 CORE_QUEUE_SIZE, /* queue size */
4351 NULL, /* Closure passed to MESH functions */
4352 &core_init, /* Call core_init once connected */
4353 &core_connect, /* Handle connects */
4354 &core_disconnect, /* remove peers on disconnects */
4355 NULL, /* Don't notify about all incoming messages */
4356 GNUNET_NO, /* For header only in notification */
4357 NULL, /* Don't notify about all outbound messages */
4358 GNUNET_NO, /* For header-only out notification */
4359 core_handlers); /* Register these handlers */
4361 if (core_handle == NULL)
4364 GNUNET_SCHEDULER_shutdown ();
4369 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4372 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4374 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4375 GNUNET_SCHEDULER_shutdown ();
4378 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4379 GNUNET_free (keyfile);
4380 if (my_private_key == NULL)
4382 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4383 _("Mesh service could not access hostkey. Exiting.\n"));
4384 GNUNET_SCHEDULER_shutdown ();
4387 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4388 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4389 &my_full_id.hashPubKey);
4390 myid = GNUNET_PEER_intern (&my_full_id);
4392 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4399 dht_handle = GNUNET_DHT_connect (c, 64);
4400 if (dht_handle == NULL)
4406 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4408 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4409 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4410 peers = GNUNET_CONTAINER_multihashmap_create (32);
4411 applications = GNUNET_CONTAINER_multihashmap_create (32);
4412 types = GNUNET_CONTAINER_multihashmap_create (32);
4414 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4415 nc = GNUNET_SERVER_notification_context_create (server_handle,
4417 GNUNET_SERVER_disconnect_notify (server_handle,
4418 &handle_local_client_disconnect,
4423 clients_tail = NULL;
4428 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4429 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4431 /* Create a peer_info for the local peer */
4432 peer = peer_info_get(&my_full_id);
4435 path_add_to_peer(peer, p, GNUNET_YES);
4437 /* Scheduled the task to clean up when shutdown is called */
4438 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4445 * The main function for the mesh service.
4447 * @param argc number of arguments from the command line
4448 * @param argv command line arguments
4449 * @return 0 ok, 1 on error
4452 main (int argc, char *const *argv)
4456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4459 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");