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, 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 */
1536 path_destroy (path);
1539 for (l = 1; l < path->length; l++)
1541 if (path->peers[l] == myid)
1543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1544 "MESH: shortening path by %u\n",
1546 for (l2 = 0; l2 < path->length - l - 1 ; l2++)
1548 path->peers[l2] = path->peers[l + l2];
1552 path->peers = GNUNET_realloc (path->peers,
1553 path->length * sizeof (GNUNET_PEER_Id));
1558 struct GNUNET_PeerIdentity id;
1560 GNUNET_PEER_resolve (peer_info->id, &id);
1561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1562 "MESH: adding path [%u] to peer %s\n",
1567 l = path_get_length (path);
1574 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1576 l2 = path_get_length (aux);
1579 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1580 peer_info->path_tail,
1587 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1594 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head,
1595 peer_info->path_tail,
1602 * Add the path to the origin peer and update the path used to reach it in case
1603 * this is the shortest.
1604 * The path is given in peer_info -> destination, therefore we turn the path
1605 * upside down first.
1607 * @param peer_info Peer to add the path to, being the origin of the path.
1608 * @param path New path to add after being inversed.
1609 * @param trusted Do we trust that this path is real?
1612 path_add_to_origin (struct MeshPeerInfo *peer_info,
1613 struct MeshPeerPath *path,
1617 path_add_to_peer (peer_info, path, trusted);
1622 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1623 * to obtain a local peer -> destination path and interning the peer ids.
1625 * @return Newly allocated and created path
1627 static struct MeshPeerPath *
1628 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1629 unsigned int get_path_length,
1630 const struct GNUNET_PeerIdentity *put_path,
1631 unsigned int put_path_length)
1633 struct MeshPeerPath *p;
1639 GNUNET_PEER_change_rc(myid, 1);
1640 i = get_path_length;
1641 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1642 for (i--; i >= 0; i--)
1644 id = GNUNET_PEER_intern (&get_path[i]);
1645 if (p->length > 0 && id == p->peers[p->length - 1])
1647 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1648 GNUNET_PEER_change_rc(id, -1);
1652 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1653 "MESH: Adding from GET: %s.\n",
1654 GNUNET_i2s(&get_path[i]));
1656 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1657 p->peers[p->length - 1] = id;
1660 i = put_path_length;
1661 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1662 for (i--; i >= 0; i--)
1664 id = GNUNET_PEER_intern (&put_path[i]);
1667 /* PUT path went through us, so discard the path up until now and start
1668 * from here to get a much shorter (and loop-free) path.
1673 if (p->length > 0 && id == p->peers[p->length - 1])
1675 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1676 GNUNET_PEER_change_rc(id, -1);
1680 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1681 "MESH: Adding from PUT: %s.\n",
1682 GNUNET_i2s(&put_path[i]));
1684 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1685 p->peers[p->length - 1] = id;
1689 if (get_path_length > 0)
1690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1691 "MESH: (first of GET: %s)\n",
1692 GNUNET_i2s(&get_path[0]));
1693 if (put_path_length > 0)
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "MESH: (first of PUT: %s)\n",
1696 GNUNET_i2s(&put_path[0]));
1697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1698 "MESH: In total: %d hops\n",
1700 for (i = 0; i < p->length; i++)
1702 struct GNUNET_PeerIdentity peer_id;
1704 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1708 GNUNET_i2s(&peer_id));
1716 * Send keepalive packets for a peer
1718 * @param cls Closure (tunnel for which to send the keepalive).
1719 * @param tc Notification context.
1721 * TODO: implement explicit multicast keepalive?
1724 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1728 * Search for a tunnel among the incoming tunnels
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_incoming (MESH_TunnelNumber tid)
1737 GNUNET_HashCode hash;
1739 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1740 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1741 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1746 * Search for a tunnel among the tunnels for a client
1748 * @param c the client whose tunnels to search in
1749 * @param tid the local id of the tunnel
1751 * @return tunnel handler, NULL if doesn't exist
1753 static struct MeshTunnel *
1754 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1756 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1758 return tunnel_get_incoming (tid);
1762 GNUNET_HashCode hash;
1764 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1765 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1771 * Search for a tunnel by global ID using PEER_ID
1773 * @param pi 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_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1781 struct MESH_TunnelID id;
1782 GNUNET_HashCode hash;
1787 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1788 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1793 * Search for a tunnel by global ID using full PeerIdentities
1795 * @param oid owner of the tunnel
1796 * @param tid global tunnel number
1798 * @return tunnel handler, NULL if doesn't exist
1800 static struct MeshTunnel *
1801 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1803 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1808 * Callback used to notify a client owner of a tunnel that a peer has
1809 * disconnected, most likely because of a path change.
1811 * @param cls Closure (tunnel this notification is about).
1812 * @param peer_id Short ID of disconnected peer.
1815 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
1817 struct MeshTunnel *t = cls;
1818 struct MeshPeerInfo *peer;
1819 struct MeshPathInfo *path_info;
1821 if (NULL != t->client && NULL != nc)
1823 struct GNUNET_MESH_PeerControl msg;
1824 msg.header.size = htons (sizeof (msg));
1825 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1826 msg.tunnel_id = htonl (t->local_tid);
1827 GNUNET_PEER_resolve (peer_id, &msg.peer);
1828 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1829 &msg.header, GNUNET_NO);
1831 peer = peer_info_get_short(peer_id);
1832 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1833 path_info->peer = peer;
1835 GNUNET_SCHEDULER_add_now(&peer_info_connect_task, path_info);
1840 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1841 * needed rescources.
1842 * If peer already exists, reevaluate shortest path and change if different.
1844 * @param t Tunnel we want to add a new peer to
1845 * @param peer PeerInfo of the peer being added
1849 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1851 struct GNUNET_PeerIdentity id;
1852 struct MeshPeerPath *best_p;
1853 struct MeshPeerPath *p;
1854 unsigned int best_cost;
1857 GNUNET_PEER_resolve(peer->id, &id);
1859 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1862 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1863 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(
1867 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1870 if (NULL != (p = peer->path_head))
1873 best_cost = path_get_cost(t->tree, p);
1876 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1883 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
1884 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1885 t->path_refresh_task =
1886 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1890 /* Start a DHT get if necessary */
1891 peer_info_connect(peer, t);
1896 * Add a path to a tunnel which we don't own, just to remember the next hop.
1897 * If destination node was already in the tunnel, the first hop information
1898 * will be replaced with the new path.
1900 * @param t Tunnel we want to add a new peer to
1901 * @param p Path to add
1902 * @param own_pos Position of local node in path.
1906 tunnel_add_path (struct MeshTunnel *t,
1907 struct MeshPeerPath *p,
1908 unsigned int own_pos)
1910 struct GNUNET_PeerIdentity id;
1912 GNUNET_assert (0 != own_pos);
1913 tree_add_path(t->tree, p, NULL, NULL);
1914 if (NULL == t->tree->me)
1915 t->tree->me = tree_find_peer(t->tree->root, p->peers[own_pos]);
1916 if (own_pos < p->length - 1)
1918 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1919 tree_update_first_hops(t->tree, t->tree->me, &id);
1925 * Notifies a tunnel that a connection has broken that affects at least
1926 * some of its peers. Sends a notification towards the root of the tree.
1927 * In case the peer is the owner of the tree, notifies the client that owns
1928 * the tunnel and tries to reconnect.
1930 * @param t Tunnel affected.
1931 * @param p1 Peer that got disconnected from p2.
1932 * @param p2 Peer that got disconnected from p1.
1934 * @return Short ID of the peer disconnected (either p1 or p2).
1935 * 0 if the tunnel remained unaffected.
1937 static GNUNET_PEER_Id
1938 tunnel_notify_connection_broken (struct MeshTunnel *t,
1944 pid = tree_notify_connection_broken (t->tree,
1947 ¬ify_peer_disconnected,
1949 if (myid != p1 && myid != p2)
1955 if (NULL != t->tree->me->parent)
1957 /* We are the peer still connected, notify owner of the disconnection. */
1958 struct GNUNET_MESH_PathBroken msg;
1959 struct GNUNET_PeerIdentity neighbor;
1961 msg.header.size = htons (sizeof (msg));
1962 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1963 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1964 msg.tid = htonl (t->id.tid);
1965 msg.peer1 = my_full_id;
1966 GNUNET_PEER_resolve (pid, &msg.peer2);
1967 GNUNET_PEER_resolve (t->tree->me->parent->peer, &neighbor);
1968 send_message (&msg.header, &neighbor);
1977 unsigned int reference_counter;
1986 * Send a message in a tunnel in multicast, sending a copy to each child node
1987 * down the local one in the tunnel tree.
1989 * @param t Tunnel in which to send the data.
1990 * @param msg Message to be sent
1992 * @return Number of copies sent.
1994 * TODO: unifiy shared resources and reference counter management
1997 tunnel_send_multicast (struct MeshTunnel *t,
1998 const struct GNUNET_MessageHeader *msg)
2000 struct GNUNET_PeerIdentity neighbor;
2001 struct MeshDataDescriptor *info;
2002 struct MeshTunnelTreeNode *n;
2003 struct MeshTunnelTreeNode *counter;
2004 GNUNET_SCHEDULER_TaskIdentifier *task;
2005 unsigned int *copies;
2010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2011 "MESH: sending a multicast packet...\n");
2012 size = ntohs (msg->size);
2013 GNUNET_assert (NULL != t->tree->me);
2014 n = t->tree->me->children_head;
2017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2018 "MESH: no children in the tree, no one to send.\n");
2021 copies = GNUNET_malloc (sizeof (unsigned int));
2022 for (counter = n; NULL != counter; counter = counter->next)
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (%u copies)\n", *copies);
2025 data = GNUNET_malloc (size);
2026 memcpy (data, msg, size);
2027 if (NULL != t->client)
2029 task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2030 *task = GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT,
2035 task = NULL; // So GCC shuts up about task being potentially uninitialized
2038 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2039 // info->shared_data = aliased_data;
2040 // info->shared_data->reference_counter++;
2044 info->copies = copies;
2046 if (NULL != t->client)
2048 info->client = t->client->handle;
2049 info->timeout_task = task;
2051 info->destination = n->peer;
2052 GNUNET_PEER_resolve (n->peer, &neighbor);
2053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2054 "MESH: sending to %s...\n",
2055 GNUNET_i2s (&neighbor));
2056 info->peer = peer_info_get(&neighbor);
2057 GNUNET_assert (NULL != info->peer);
2058 i = peer_info_transmit_slot(info->peer);
2059 info->handler_n = i;
2060 info->peer->infos[i] = info;
2061 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2062 info->peer->core_transmit[i] =
2063 GNUNET_CORE_notify_transmit_ready (core_handle,
2066 GNUNET_TIME_UNIT_FOREVER_REL,
2069 &send_core_data_multicast, info);
2077 * Send a message to all peers in this tunnel that the tunnel is no longer
2080 * @param t The tunnel whose peers to notify.
2083 tunnel_send_destroy (struct MeshTunnel *t)
2085 struct GNUNET_MESH_TunnelDestroy msg;
2087 msg.header.size = htons (sizeof (msg));
2088 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2089 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2090 msg.tid = htonl (t->id.tid);
2091 tunnel_send_multicast (t, &msg.header);
2097 * Destroy the tunnel and free any allocated resources linked to it
2099 * @param t the tunnel to destroy
2101 * @return GNUNET_OK on success
2104 tunnel_destroy (struct MeshTunnel *t)
2106 struct MeshClient *c;
2107 struct MeshQueue *q;
2108 struct MeshQueue *qn;
2109 GNUNET_HashCode hash;
2118 struct GNUNET_PeerIdentity id;
2120 GNUNET_PEER_resolve(t->id.oid, &id);
2121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2122 "MESH: destroying tunnel %s [%x]\n",
2126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2130 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2131 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2136 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2138 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
2142 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2144 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2145 GNUNET_break (GNUNET_YES ==
2146 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t));
2149 if (NULL != t->peers)
2151 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
2152 &peer_info_delete_tunnel, t);
2153 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2158 if (NULL != q->data)
2159 GNUNET_free (q->data);
2163 /* TODO cancel core transmit ready in case it was active */
2165 tree_destroy(t->tree);
2166 if (NULL != t->dht_get_type)
2167 GNUNET_DHT_get_stop(t->dht_get_type);
2168 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2169 GNUNET_SCHEDULER_cancel(t->timeout_task);
2170 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2171 GNUNET_SCHEDULER_cancel(t->path_refresh_task);
2178 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2179 * that are no longer needed, as well as nodes of no longer reachable peers.
2180 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2182 * @param t Tunnel from which to remove the path.
2183 * @param p Peer which should be removed.
2186 tunnel_delete_peer (struct MeshTunnel *t,
2187 GNUNET_PEER_Id peer)
2189 GNUNET_break (GNUNET_OK == tree_del_peer (t->tree, peer, NULL, NULL));
2190 if (NULL == t->tree->root)
2196 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2197 * client when the client disconnects.
2199 * @param cls closure (client that is disconnecting)
2200 * @param key the hash of the local tunnel id (used to access the hashmap)
2201 * @param value the value stored at the key (tunnel to destroy)
2203 * @return GNUNET_OK on success
2206 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2208 struct MeshTunnel *t = value;
2211 r = tunnel_destroy (t);
2217 * Timeout function, destroys tunnel if called
2219 * @param cls Closure (tunnel to destroy).
2220 * @param tc TaskContext
2223 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2225 struct MeshTunnel *t = cls;
2227 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2229 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2234 * Resets the tunnel timeout. Starts it if no timeout was running.
2236 * @param t Tunnel whose timeout to reset.
2239 tunnel_reset_timeout (struct MeshTunnel *t)
2241 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2242 GNUNET_SCHEDULER_cancel (t->timeout_task);
2243 t->timeout_task = GNUNET_SCHEDULER_add_delayed (
2244 GNUNET_TIME_relative_multiply(REFRESH_PATH_TIME, 4),
2250 /******************************************************************************/
2251 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2252 /******************************************************************************/
2255 * Function called to notify a client about the socket
2256 * being ready to queue more data. "buf" will be
2257 * NULL and "size" zero if the socket was closed for
2258 * writing in the meantime.
2260 * @param cls closure
2261 * @param size number of bytes available in buf
2262 * @param buf where the callee should write the message
2263 * @return number of bytes written to buf
2266 send_core_create_path (void *cls, size_t size, void *buf)
2268 struct MeshPathInfo *info = cls;
2269 struct GNUNET_MESH_ManipulatePath *msg;
2270 struct GNUNET_PeerIdentity *peer_ptr;
2271 struct MeshPeerInfo *peer = info->peer;
2272 struct MeshTunnel *t = info->t;
2273 struct MeshPeerPath *p = info->path;
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2278 "MESH: CREATE PATH sending...\n");
2280 sizeof (struct GNUNET_MESH_ManipulatePath) +
2281 p->length * sizeof (struct GNUNET_PeerIdentity);
2283 if (size < size_needed || NULL == buf)
2285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
2289 info->peer->core_transmit[info->pos] =
2290 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2291 GNUNET_TIME_UNIT_FOREVER_REL,
2292 path_get_first_hop (t->tree, peer->id),
2294 &send_core_create_path,
2298 info->peer->core_transmit[info->pos] = NULL;
2301 struct GNUNET_PeerIdentity id;
2303 GNUNET_PEER_resolve (peer->id, &id);
2304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2305 "MESH: setting core_transmit %s [%u] to NULL\n",
2310 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2311 msg->header.size = htons (size_needed);
2312 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2313 msg->tid = ntohl (t->id.tid);
2315 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2316 for (i = 0; i < p->length; i++)
2318 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 "MESH: CREATE PATH (%u bytes long) sent!\n",
2332 * Function called to notify a client about the socket
2333 * being ready to queue more data. "buf" will be
2334 * NULL and "size" zero if the socket was closed for
2335 * writing in the meantime.
2337 * @param cls closure (data itself)
2338 * @param size number of bytes available in buf
2339 * @param buf where the callee should write the message
2341 * @return number of bytes written to buf
2344 send_core_data_multicast (void *cls, size_t size, void *buf)
2346 struct MeshDataDescriptor *info = cls;
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2350 GNUNET_assert (NULL != info);
2351 GNUNET_assert (NULL != info->peer);
2352 total_size = info->size;
2353 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2355 if (total_size > size)
2358 struct GNUNET_PeerIdentity id;
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2361 "MESH: Multicast: retransmitting... (%u/%u)\n",
2363 GNUNET_PEER_resolve(info->peer->id, &id);
2364 info->peer->core_transmit[info->handler_n] =
2365 GNUNET_CORE_notify_transmit_ready (core_handle,
2368 GNUNET_TIME_UNIT_FOREVER_REL,
2371 &send_core_data_multicast,
2375 info->peer->core_transmit[info->handler_n] = NULL;
2376 info->peer->infos[info->handler_n] = NULL;
2377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2378 memcpy (buf, info->data, total_size);
2381 struct GNUNET_MESH_Multicast *mc;
2382 struct GNUNET_MessageHeader *mh;
2385 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2387 mc = (struct GNUNET_MESH_Multicast *) mh;
2388 mh = (struct GNUNET_MessageHeader *) &mc[1];
2389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2390 "MESH: multicast, payload type %u\n",
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2393 "MESH: multicast, payload size %u\n",
2398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2404 data_descriptor_decrement_multicast (info);
2405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2413 * Function called to notify a client about the socket
2414 * being ready to queue more data. "buf" will be
2415 * NULL and "size" zero if the socket was closed for
2416 * writing in the meantime.
2418 * @param cls closure (MeshDataDescriptor)
2419 * @param size number of bytes available in buf
2420 * @param buf where the callee should write the message
2421 * @return number of bytes written to buf
2424 send_core_path_ack (void *cls, size_t size, void *buf)
2426 struct MeshDataDescriptor *info = cls;
2427 struct GNUNET_MESH_PathACK *msg = buf;
2429 GNUNET_assert (NULL != info);
2432 info->peer->core_transmit[info->handler_n] = NULL;
2434 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2439 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2440 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2441 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2442 msg->tid = htonl (info->origin->tid);
2443 msg->peer_id = my_full_id;
2445 /* TODO add signature */
2447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2448 return sizeof (struct GNUNET_MESH_PathACK);
2452 /******************************************************************************/
2453 /******************** MESH NETWORK HANDLERS **************************/
2454 /******************************************************************************/
2458 * Core handler for path creation
2460 * @param cls closure
2461 * @param message message
2462 * @param peer peer identity this notification is about
2463 * @param atsi performance data
2464 * @param atsi_count number of records in 'atsi'
2466 * @return GNUNET_OK to keep the connection open,
2467 * GNUNET_SYSERR to close it (signal serious error)
2470 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2471 const struct GNUNET_MessageHeader *message,
2472 const struct GNUNET_ATS_Information *atsi,
2473 unsigned int atsi_count)
2475 unsigned int own_pos;
2478 MESH_TunnelNumber tid;
2479 struct GNUNET_MESH_ManipulatePath *msg;
2480 struct GNUNET_PeerIdentity *pi;
2481 GNUNET_HashCode hash;
2482 struct MeshPeerPath *path;
2483 struct MeshPeerInfo *dest_peer_info;
2484 struct MeshPeerInfo *orig_peer_info;
2485 struct MeshTunnel *t;
2487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2488 "MESH: Received a path create msg [%s]\n",
2489 GNUNET_i2s(&my_full_id));
2490 size = ntohs (message->size);
2491 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2493 GNUNET_break_op (0);
2497 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2498 if (size % sizeof (struct GNUNET_PeerIdentity))
2500 GNUNET_break_op (0);
2503 size /= sizeof (struct GNUNET_PeerIdentity);
2506 GNUNET_break_op (0);
2509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2510 "MESH: path has %u hops.\n",
2512 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2514 tid = ntohl (msg->tid);
2515 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2516 t = tunnel_get (pi, tid);
2517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2518 "MESH: path is for tunnel %s [%X].\n",
2523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2524 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2525 t->id.oid = GNUNET_PEER_intern (pi);
2527 while (NULL != tunnel_get_incoming (next_local_tid))
2528 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2529 t->local_tid = next_local_tid++;
2530 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2531 t->tree = tree_new(t->id.oid);
2533 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2535 GNUNET_CONTAINER_multihashmap_put (
2539 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2545 tunnel_reset_timeout (t);
2546 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2548 GNUNET_CONTAINER_multihashmap_put (
2552 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2560 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2561 if (NULL == dest_peer_info)
2563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2564 "MESH: Creating PeerInfo for destination.\n");
2565 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2566 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2567 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2569 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2571 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2572 if (NULL == orig_peer_info)
2574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2575 "MESH: Creating PeerInfo for origin.\n");
2576 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2577 orig_peer_info->id = GNUNET_PEER_intern (pi);
2578 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2579 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2582 path = path_new (size);
2584 for (i = 0; i < size; i++)
2586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2587 "MESH: ... adding %s\n",
2588 GNUNET_i2s(&pi[i]));
2589 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2590 if (path->peers[i] == myid)
2593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2594 "MESH: Own position: %u\n", own_pos);
2597 /* cannot be self, must be 'not found' */
2598 /* create path: self not found in path through self */
2599 GNUNET_break_op (0);
2600 path_destroy (path);
2601 /* FIXME error. destroy tunnel? leave for timeout? */
2604 tunnel_add_path (t, path, own_pos);
2605 if (own_pos == size - 1)
2607 /* It is for us! Send ack. */
2608 struct GNUNET_MESH_TunnelNotification cmsg;
2609 struct MeshDataDescriptor *info;
2612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2613 "MESH: It's for us!\n");
2614 path_add_to_origin (orig_peer_info, path, GNUNET_NO);
2615 if (NULL == t->peers)
2616 t->peers = GNUNET_CONTAINER_multihashmap_create(4);
2617 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2619 &my_full_id.hashPubKey,
2620 peer_info_get(&my_full_id),
2621 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2622 /* FIXME use send_message */
2623 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2624 info->origin = &t->id;
2625 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2626 GNUNET_assert (NULL != info->peer);
2627 j = peer_info_transmit_slot(info->peer);
2628 info->handler_n = j;
2629 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2630 info->peer->infos[j] = info;
2631 info->peer->core_transmit[j] =
2632 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2633 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2634 sizeof (struct GNUNET_MESH_PathACK),
2635 &send_core_path_ack, info);
2636 cmsg.header.size = htons(sizeof(cmsg));
2637 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2638 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2639 cmsg.tunnel_id = htonl(t->local_tid);
2640 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2644 struct MeshPeerPath *path2;
2646 /* It's for somebody else! Retransmit. */
2647 path2 = path_duplicate(path);
2648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2649 "MESH: Retransmitting.\n");
2650 path_add_to_peer(dest_peer_info, path2, GNUNET_NO);
2651 path2 = path_duplicate(path);
2652 path_add_to_origin(orig_peer_info, path2, GNUNET_NO);
2653 send_create_path(dest_peer_info, path, t);
2660 * Core handler for path destruction
2662 * @param cls closure
2663 * @param message message
2664 * @param peer peer identity this notification is about
2665 * @param atsi performance data
2666 * @param atsi_count number of records in 'atsi'
2668 * @return GNUNET_OK to keep the connection open,
2669 * GNUNET_SYSERR to close it (signal serious error)
2672 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2673 const struct GNUNET_MessageHeader *message,
2674 const struct GNUNET_ATS_Information *atsi,
2675 unsigned int atsi_count)
2677 struct GNUNET_MESH_ManipulatePath *msg;
2678 struct GNUNET_PeerIdentity *pi;
2679 struct MeshPeerPath *path;
2680 struct MeshTunnel *t;
2681 unsigned int own_pos;
2685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2686 "MESH: Received a PATH DESTROY msg from %s\n",
2688 size = ntohs (message->size);
2689 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2691 GNUNET_break_op (0);
2695 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2696 if (size % sizeof (struct GNUNET_PeerIdentity))
2698 GNUNET_break_op (0);
2701 size /= sizeof (struct GNUNET_PeerIdentity);
2704 GNUNET_break_op (0);
2707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2708 "MESH: path has %u hops.\n",
2711 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2712 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2714 "MESH: path is for tunnel %s [%X].\n",
2717 t = tunnel_get (pi, ntohl (msg->tid));
2720 /* TODO notify back: we don't know this tunnel */
2721 GNUNET_break_op (0);
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2725 path = path_new (size);
2727 for (i = 0; i < size; i++)
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2730 "MESH: ... adding %s\n",
2731 GNUNET_i2s(&pi[i]));
2732 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2733 if (path->peers[i] == myid)
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2737 "MESH: Own position: %u\n", own_pos);
2738 if (own_pos < path->length - 1)
2739 send_message (message, &pi[own_pos + 1]);
2740 tunnel_delete_peer (t, path->peers[path->length - 1]);
2741 path_destroy (path);
2747 * Core handler for notifications of broken paths
2749 * @param cls closure
2750 * @param message message
2751 * @param peer peer identity this notification is about
2752 * @param atsi performance data
2753 * @param atsi_count number of records in 'atsi'
2755 * @return GNUNET_OK to keep the connection open,
2756 * GNUNET_SYSERR to close it (signal serious error)
2759 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
2760 const struct GNUNET_MessageHeader *message,
2761 const struct GNUNET_ATS_Information *atsi,
2762 unsigned int atsi_count)
2764 struct GNUNET_MESH_PathBroken *msg;
2765 struct MeshTunnel *t;
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2768 "MESH: Received a PATH BROKEN msg from %s\n",
2770 msg = (struct GNUNET_MESH_PathBroken *) message;
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2772 "MESH: regarding %s\n",
2773 GNUNET_i2s(&msg->peer1));
2774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2775 "MESH: regarding %s\n",
2776 GNUNET_i2s(&msg->peer2));
2777 t = tunnel_get(&msg->oid, ntohl (msg->tid));
2780 GNUNET_break_op (0);
2783 tunnel_notify_connection_broken(t,
2784 GNUNET_PEER_search(&msg->peer1),
2785 GNUNET_PEER_search(&msg->peer2));
2792 * Core handler for tunnel destruction
2794 * @param cls closure
2795 * @param message message
2796 * @param peer peer identity this notification is about
2797 * @param atsi performance data
2798 * @param atsi_count number of records in 'atsi'
2800 * @return GNUNET_OK to keep the connection open,
2801 * GNUNET_SYSERR to close it (signal serious error)
2804 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2805 const struct GNUNET_MessageHeader *message,
2806 const struct GNUNET_ATS_Information *atsi,
2807 unsigned int atsi_count)
2809 struct GNUNET_MESH_TunnelDestroy *msg;
2810 struct MeshTunnel *t;
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2813 "MESH: Got a TUNNEL DESTROY packet from %s\n",
2815 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2817 "MESH: for tunnel %s [%u]\n",
2818 GNUNET_i2s (&msg->oid),
2820 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2823 /* Probably already got the message from another path,
2824 * destroyed the tunnel and retransmitted to children.
2829 if (t->id.oid == myid)
2831 GNUNET_break_op (0);
2834 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2836 /* Tunnel was incoming, notify clients */
2837 send_clients_tunnel_destroy (t);
2839 tunnel_send_destroy (t);
2846 * Core handler for mesh network traffic going from the origin to a peer
2848 * @param cls closure
2849 * @param peer peer identity this notification is about
2850 * @param message message
2851 * @param atsi performance data
2852 * @param atsi_count number of records in 'atsi'
2853 * @return GNUNET_OK to keep the connection open,
2854 * GNUNET_SYSERR to close it (signal serious error)
2857 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2858 const struct GNUNET_MessageHeader *message,
2859 const struct GNUNET_ATS_Information *atsi,
2860 unsigned int atsi_count)
2862 struct GNUNET_MESH_Unicast *msg;
2863 struct MeshTunnel *t;
2867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2868 "MESH: got a unicast packet from %s\n",
2870 size = ntohs (message->size);
2872 sizeof (struct GNUNET_MESH_Unicast) +
2873 sizeof (struct GNUNET_MessageHeader))
2878 msg = (struct GNUNET_MESH_Unicast *) message;
2879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2880 "MESH: of type %u\n",
2881 ntohs (msg[1].header.type));
2882 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2885 /* TODO notify back: we don't know this tunnel */
2886 GNUNET_break_op (0);
2889 tunnel_reset_timeout (t);
2890 pid = GNUNET_PEER_search(&msg->destination);
2893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2894 "MESH: it's for us! sending to clients...\n");
2895 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2899 "MESH: not for us, retransmitting...\n");
2900 send_message (message, path_get_first_hop(t->tree, pid));
2906 * Core handler for mesh network traffic going from the origin to all peers
2908 * @param cls closure
2909 * @param message message
2910 * @param peer peer identity this notification is about
2911 * @param atsi performance data
2912 * @param atsi_count number of records in 'atsi'
2913 * @return GNUNET_OK to keep the connection open,
2914 * GNUNET_SYSERR to close it (signal serious error)
2916 * TODO: Check who we got this from, to validate route.
2919 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2920 const struct GNUNET_MessageHeader *message,
2921 const struct GNUNET_ATS_Information *atsi,
2922 unsigned int atsi_count)
2924 struct GNUNET_MESH_Multicast *msg;
2925 struct MeshTunnel *t;
2928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2929 "MESH: got a multicast packet from %s\n",
2931 size = ntohs (message->size);
2932 if (sizeof (struct GNUNET_MESH_Multicast) +
2933 sizeof (struct GNUNET_MessageHeader) > size)
2935 GNUNET_break_op (0);
2938 msg = (struct GNUNET_MESH_Multicast *) message;
2939 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2943 /* TODO notify that we dont know that tunnel */
2944 GNUNET_break_op (0);
2947 tunnel_reset_timeout (t);
2949 /* Transmit to locally interested clients */
2950 if (NULL != t->peers &&
2951 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2953 send_subscribed_clients (message, &msg[1].header);
2955 tunnel_send_multicast(t, message);
2961 * Core handler for mesh network traffic toward the owner of a tunnel
2963 * @param cls closure
2964 * @param message message
2965 * @param peer peer identity this notification is about
2966 * @param atsi performance data
2967 * @param atsi_count number of records in 'atsi'
2969 * @return GNUNET_OK to keep the connection open,
2970 * GNUNET_SYSERR to close it (signal serious error)
2973 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2974 const struct GNUNET_MessageHeader *message,
2975 const struct GNUNET_ATS_Information *atsi,
2976 unsigned int atsi_count)
2978 struct GNUNET_MESH_ToOrigin *msg;
2979 struct GNUNET_PeerIdentity id;
2980 struct MeshPeerInfo *peer_info;
2981 struct MeshTunnel *t;
2984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2985 "MESH: got a ToOrigin packet from %s\n",
2987 size = ntohs (message->size);
2988 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2989 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2991 GNUNET_break_op (0);
2994 msg = (struct GNUNET_MESH_ToOrigin *) message;
2995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2996 "MESH: of type %u\n",
2997 ntohs (msg[1].header.type));
2998 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3002 /* TODO notify that we dont know this tunnel (whom)? */
3003 GNUNET_break_op (0);
3007 if (t->id.oid == myid)
3010 struct GNUNET_MESH_ToOrigin *copy;
3012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3013 "MESH: it's for us! sending to clients...\n");
3014 if (NULL == t->client)
3016 /* got data packet for ownerless tunnel */
3017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3018 "MESH: no clients!\n");
3019 GNUNET_break_op (0);
3022 /* TODO signature verification */
3023 memcpy (cbuf, message, size);
3024 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3025 copy->tid = htonl (t->local_tid);
3026 GNUNET_SERVER_notification_context_unicast (nc,
3032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3033 "MESH: not for us, retransmitting...\n");
3035 peer_info = peer_info_get (&msg->oid);
3036 if (NULL == peer_info)
3038 /* unknown origin of tunnel */
3042 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
3043 send_message (message, &id);
3050 * Core handler for path ACKs
3052 * @param cls closure
3053 * @param message message
3054 * @param peer peer identity this notification is about
3055 * @param atsi performance data
3056 * @param atsi_count number of records in 'atsi'
3058 * @return GNUNET_OK to keep the connection open,
3059 * GNUNET_SYSERR to close it (signal serious error)
3062 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3063 const struct GNUNET_MessageHeader *message,
3064 const struct GNUNET_ATS_Information *atsi,
3065 unsigned int atsi_count)
3067 struct GNUNET_MESH_PathACK *msg;
3068 struct GNUNET_PeerIdentity id;
3069 struct MeshTunnelTreeNode *n;
3070 struct MeshPeerInfo *peer_info;
3071 struct MeshTunnel *t;
3073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3074 "MESH: Received a path ACK msg [%s]\n",
3075 GNUNET_i2s(&my_full_id));
3076 msg = (struct GNUNET_MESH_PathACK *) message;
3077 t = tunnel_get (&msg->oid, msg->tid);
3080 /* TODO notify that we don't know the tunnel */
3084 /* Message for us? */
3085 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
3088 if (NULL == t->client)
3090 GNUNET_break_op (0);
3093 if (NULL != t->dht_get_type)
3095 GNUNET_DHT_get_stop (t->dht_get_type);
3096 t->dht_get_type = NULL;
3098 peer_info = peer_info_get (&msg->peer_id);
3099 n = tree_find_peer(t->tree->root, peer_info->id);
3102 GNUNET_break_op (0);
3105 n->status = MESH_PEER_READY;
3106 send_client_peer_connected(t, peer_info->id);
3110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3111 "MESH: not for us, retransmitting...\n");
3112 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
3113 peer_info = peer_info_get (&msg->oid);
3114 if (NULL == peer_info)
3116 /* If we know the tunnel, we should DEFINITELY know the peer */
3120 send_message (message, &id);
3126 * Functions to handle messages from core
3128 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3129 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3130 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3131 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3132 sizeof (struct GNUNET_MESH_PathBroken)},
3133 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3134 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3135 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3136 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3137 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3138 sizeof (struct GNUNET_MESH_PathACK)},
3144 /******************************************************************************/
3145 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3146 /******************************************************************************/
3149 * deregister_app: iterator for removing each application registered by a client
3151 * @param cls closure
3152 * @param key the hash of the application id (used to access the hashmap)
3153 * @param value the value stored at the key (client)
3155 * @return GNUNET_OK on success
3158 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3160 GNUNET_break (GNUNET_YES ==
3161 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
3167 * notify_client_connection_failure: notify a client that the connection to the
3168 * requested remote peer is not possible (for instance, no route found)
3169 * Function called when the socket is ready to queue more data. "buf" will be
3170 * NULL and "size" zero if the socket was closed for writing in the meantime.
3172 * @param cls closure
3173 * @param size number of bytes available in buf
3174 * @param buf where the callee should write the message
3175 * @return number of bytes written to buf
3178 notify_client_connection_failure (void *cls, size_t size, void *buf)
3181 struct MeshPeerInfo *peer_info;
3182 struct GNUNET_MESH_PeerControl *msg;
3183 struct GNUNET_PeerIdentity id;
3185 if (0 == size && NULL == buf)
3187 // TODO retry? cancel?
3191 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3192 peer_info = (struct MeshPeerInfo *) cls;
3193 msg = (struct GNUNET_MESH_PeerControl *) buf;
3194 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3195 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3196 // msg->tunnel_id = htonl(peer_info->t->tid);
3197 GNUNET_PEER_resolve (peer_info->id, &id);
3198 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3206 * Send keepalive packets for a peer
3208 * @param cls Closure (tunnel for which to send the keepalive).
3209 * @param tc Notification context.
3211 * TODO: implement explicit multicast keepalive?
3214 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3216 struct MeshTunnel *t = cls;
3217 struct GNUNET_MessageHeader *payload;
3218 struct GNUNET_MESH_Multicast *msg;
3219 size_t size = sizeof(struct GNUNET_MESH_Multicast) +
3220 sizeof(struct GNUNET_MessageHeader);
3223 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3227 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3230 "MESH: sending keepalive for tunnel %d\n",
3233 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3234 msg->header.size = htons (size);
3235 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3236 msg->oid = my_full_id;
3237 msg->tid = htonl(t->id.tid);
3238 payload = (struct GNUNET_MessageHeader *) &msg[1];
3239 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
3240 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3241 tunnel_send_multicast (t, &msg->header);
3243 t->path_refresh_task =
3244 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
3250 * Function to process paths received for a new peer addition. The recorded
3251 * paths form the initial tunnel, which can be optimized later.
3252 * Called on each result obtained for the DHT search.
3254 * @param cls closure
3255 * @param exp when will this value expire
3256 * @param key key of the result
3257 * @param type type of the result
3258 * @param size number of bytes in data
3259 * @param data pointer to the result data
3261 * TODO: re-issue the request after certain time? cancel after X results?
3264 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3265 const GNUNET_HashCode * key,
3266 const struct GNUNET_PeerIdentity *get_path,
3267 unsigned int get_path_length,
3268 const struct GNUNET_PeerIdentity *put_path,
3269 unsigned int put_path_length,
3270 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
3272 struct MeshPathInfo *path_info = cls;
3273 struct MeshPeerPath *p;
3274 struct GNUNET_PeerIdentity pi;
3277 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3278 "MESH: Got results from DHT!\n");
3279 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3280 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3283 // GNUNET_DHT_get_stop(path_info->peer->dhtget);
3284 // path_info->peer->dhtget = NULL;
3286 p = path_build_from_dht (get_path, get_path_length,
3287 put_path, put_path_length);
3288 path_add_to_peer (path_info->peer, p, GNUNET_NO);
3289 for (i = 0; i < path_info->peer->ntunnels; i++)
3291 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3292 peer_info_connect(path_info->peer, path_info->t);
3294 // GNUNET_free (path_info);
3301 * Function to process paths received for a new peer addition. The recorded
3302 * paths form the initial tunnel, which can be optimized later.
3303 * Called on each result obtained for the DHT search.
3305 * @param cls closure
3306 * @param exp when will this value expire
3307 * @param key key of the result
3308 * @param type type of the result
3309 * @param size number of bytes in data
3310 * @param data pointer to the result data
3313 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3314 const GNUNET_HashCode * key,
3315 const struct GNUNET_PeerIdentity *get_path,
3316 unsigned int get_path_length,
3317 const struct GNUNET_PeerIdentity *put_path,
3318 unsigned int put_path_length,
3319 enum GNUNET_BLOCK_Type type, size_t size,
3322 const struct GNUNET_PeerIdentity *pi = data;
3323 struct MeshTunnel *t = cls;
3324 struct MeshPeerInfo *peer_info;
3325 struct MeshPeerPath *p;
3327 if (size != sizeof (struct GNUNET_PeerIdentity))
3329 GNUNET_break_op (0);
3332 GNUNET_assert (NULL != t->client);
3333 peer_info = peer_info_get (pi);
3334 (void) GNUNET_CONTAINER_multihashmap_put (
3338 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3340 p = path_build_from_dht (get_path, get_path_length,
3341 put_path, put_path_length);
3342 path_add_to_peer (peer_info, p, GNUNET_NO);
3343 tunnel_add_peer(t, peer_info);
3344 peer_info_connect (peer_info, t);
3348 /******************************************************************************/
3349 /********************* MESH LOCAL HANDLES **************************/
3350 /******************************************************************************/
3354 * Handler for client disconnection
3356 * @param cls closure
3357 * @param client identification of the client; NULL
3358 * for the last call when the server is destroyed
3361 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3363 struct MeshClient *c;
3364 struct MeshClient *next;
3366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3375 if (c->handle != client && NULL != client)
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
3382 GNUNET_SERVER_client_drop (c->handle);
3383 if (NULL != c->tunnels)
3385 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3386 &tunnel_destroy_iterator,
3388 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3391 /* deregister clients applications */
3392 if (NULL != c->apps)
3394 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3395 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3397 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3398 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3400 GNUNET_SCHEDULER_cancel (announce_applications_task);
3401 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3403 if (NULL != c->types)
3404 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3406 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT FREE at %p\n", c);
3412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3418 * Handler for new clients
3420 * @param cls closure
3421 * @param client identification of the client
3422 * @param message the actual message, which includes messages the client wants
3425 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3426 const struct GNUNET_MessageHeader *message)
3428 struct GNUNET_MESH_ClientConnect *cc_msg;
3429 struct MeshClient *c;
3430 GNUNET_MESH_ApplicationType *a;
3437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3438 /* Check data sanity */
3439 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3440 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3441 ntypes = ntohs (cc_msg->types);
3442 napps = ntohs (cc_msg->applications);
3444 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3447 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3451 /* Create new client structure */
3452 c = GNUNET_malloc (sizeof (struct MeshClient));
3454 c->id = next_client_id++;
3456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT NEW %u at %p\n", c->id, c);
3458 GNUNET_SERVER_client_keep (client);
3459 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3462 GNUNET_MESH_ApplicationType at;
3465 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3466 for (i = 0; i < napps; i++)
3469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3470 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3471 /* store in clients hashmap */
3472 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3473 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3474 /* store in global hashmap, for announcements */
3475 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3476 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3478 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3479 announce_applications_task =
3480 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3488 t = (uint16_t *) & a[napps];
3489 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3490 for (i = 0; i < ntypes; i++)
3493 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3495 /* store in clients hashmap */
3496 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3497 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3498 /* store in global hashmap */
3499 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3500 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3504 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3506 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3507 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3508 GNUNET_SERVER_notification_context_add (nc, client);
3510 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3518 * Handler for requests of new tunnels
3520 * @param cls closure
3521 * @param client identification of the client
3522 * @param message the actual message
3525 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3526 const struct GNUNET_MessageHeader *message)
3528 struct GNUNET_MESH_TunnelMessage *t_msg;
3529 struct MeshTunnel *t;
3530 struct MeshClient *c;
3531 GNUNET_HashCode hash;
3533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3535 /* Sanity check for client registration */
3536 if (NULL == (c = client_get (client)))
3539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3546 /* Message sanity check */
3547 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3550 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3554 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3555 /* Sanity check for tunnel numbering */
3556 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3559 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3562 /* Sanity check for duplicate tunnel IDs */
3563 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3570 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3571 while (NULL != tunnel_get_by_pi (myid, next_tid))
3572 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3573 t->id.tid = next_tid++;
3574 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3576 t->local_tid = ntohl (t_msg->tunnel_id);
3578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3579 "MESH: CREATED TUNNEL %s [%x] (%x)\n",
3580 GNUNET_i2s (&my_full_id),
3585 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3587 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3589 GNUNET_CONTAINER_multihashmap_put (
3590 c->tunnels, &hash, t,
3591 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3594 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3598 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3600 GNUNET_CONTAINER_multihashmap_put (
3602 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3605 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3608 t->tree = tree_new (myid);
3609 t->tree->refresh = REFRESH_PATH_TIME;
3610 t->tree->root->status = MESH_PEER_READY;
3611 t->tree->me = t->tree->root;
3613 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3619 * Handler for requests of deleting tunnels
3621 * @param cls closure
3622 * @param client identification of the client
3623 * @param message the actual message
3626 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3627 const struct GNUNET_MessageHeader *message)
3629 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3630 struct MeshClient *c;
3631 struct MeshTunnel *t;
3632 MESH_TunnelNumber tid;
3633 GNUNET_HashCode hash;
3635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3636 "MESH: Got a DESTROY TUNNEL from client!\n");
3638 /* Sanity check for client registration */
3639 if (NULL == (c = client_get (client)))
3642 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3645 /* Message sanity check */
3646 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3649 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3655 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3657 /* Retrieve tunnel */
3658 tid = ntohl (tunnel_msg->tunnel_id);
3660 /* Remove from local id hashmap */
3661 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3662 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3663 GNUNET_assert (GNUNET_YES ==
3664 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t));
3667 tunnel_send_destroy (t);
3669 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3675 * Handler for connection requests to new peers
3677 * @param cls closure
3678 * @param client identification of the client
3679 * @param message the actual message (PeerControl)
3682 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3683 const struct GNUNET_MessageHeader *message)
3685 struct GNUNET_MESH_PeerControl *peer_msg;
3686 struct MeshPeerInfo *peer_info;
3687 struct MeshClient *c;
3688 struct MeshTunnel *t;
3689 MESH_TunnelNumber tid;
3691 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3692 /* Sanity check for client registration */
3693 if (NULL == (c = client_get (client)))
3696 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3700 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3701 /* Sanity check for message size */
3702 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3705 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3709 /* Tunnel exists? */
3710 tid = ntohl (peer_msg->tunnel_id);
3711 t = tunnel_get_by_local_id (c, tid);
3715 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3719 /* Does client own tunnel? */
3720 if (t->client->handle != client)
3723 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3726 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3727 GNUNET_i2s(&peer_msg->peer));
3728 peer_info = peer_info_get (&peer_msg->peer);
3730 tunnel_add_peer(t, peer_info);
3731 peer_info_connect(peer_info, t);
3733 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3739 * Handler for disconnection requests of peers in a tunnel
3741 * @param cls closure
3742 * @param client identification of the client
3743 * @param message the actual message (PeerControl)
3746 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3747 const struct GNUNET_MessageHeader *message)
3749 struct GNUNET_MESH_PeerControl *peer_msg;
3750 struct MeshPeerInfo *peer_info;
3751 struct MeshClient *c;
3752 struct MeshTunnel *t;
3753 MESH_TunnelNumber tid;
3755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got a PEER DEL request\n");
3756 /* Sanity check for client registration */
3757 if (NULL == (c = client_get (client)))
3760 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3763 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3764 /* Sanity check for message size */
3765 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3768 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3772 /* Tunnel exists? */
3773 tid = ntohl (peer_msg->tunnel_id);
3774 t = tunnel_get_by_local_id (c, tid);
3778 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: on tunnel %X\n", t->id.tid);
3783 /* Does client own tunnel? */
3784 if (t->client->handle != client)
3787 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3792 "MESH: for peer %s\n",
3793 GNUNET_i2s(&peer_msg->peer));
3794 /* Is the peer in the tunnel? */
3795 peer_info = GNUNET_CONTAINER_multihashmap_get(t->peers,
3796 &peer_msg->peer.hashPubKey);
3797 if (NULL == peer_info)
3800 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3804 /* Ok, delete peer from tunnel */
3805 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3806 &peer_msg->peer.hashPubKey);
3808 send_destroy_path (t, peer_info->id);
3809 tunnel_delete_peer(t, peer_info->id);
3810 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3816 * Handler for connection requests to new peers by type
3818 * @param cls closure
3819 * @param client identification of the client
3820 * @param message the actual message (ConnectPeerByType)
3823 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3824 const struct GNUNET_MessageHeader *message)
3826 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3827 struct MeshClient *c;
3828 struct MeshTunnel *t;
3829 GNUNET_HashCode hash;
3830 MESH_TunnelNumber tid;
3832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3833 /* Sanity check for client registration */
3834 if (NULL == (c = client_get (client)))
3837 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3841 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3842 /* Sanity check for message size */
3843 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3844 ntohs (connect_msg->header.size))
3847 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3851 /* Tunnel exists? */
3852 tid = ntohl (connect_msg->tunnel_id);
3853 t = tunnel_get_by_local_id (c, tid);
3857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3861 /* Does client own tunnel? */
3862 if (t->client->handle != client)
3865 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3869 /* Do WE have the service? */
3870 t->type = ntohl (connect_msg->type);
3871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3872 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3873 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3876 /* Yes! Fast forward, add ourselves to the tunnel and send the
3877 * good news to the client
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3880 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3881 peer_info_get (&my_full_id),
3882 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3885 send_client_peer_connected(t, myid);
3886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3887 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3890 /* Ok, lets find a peer offering the service */
3891 if (NULL != t->dht_get_type)
3893 GNUNET_DHT_get_stop (t->dht_get_type);
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3896 GNUNET_h2s (&hash));
3898 GNUNET_DHT_get_start (dht_handle,
3899 GNUNET_TIME_UNIT_FOREVER_REL,
3900 GNUNET_BLOCK_TYPE_TEST,
3903 GNUNET_DHT_RO_RECORD_ROUTE |
3904 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3906 &dht_get_type_handler, t);
3908 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3914 * Handler for client traffic directed to one peer
3916 * @param cls closure
3917 * @param client identification of the client
3918 * @param message the actual message
3921 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3922 const struct GNUNET_MessageHeader *message)
3924 struct MeshClient *c;
3925 struct MeshTunnel *t;
3926 struct MeshPeerInfo *pi;
3927 struct GNUNET_MESH_Unicast *data_msg;
3928 MESH_TunnelNumber tid;
3931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3932 "MESH: Got a unicast request from a client!\n");
3934 /* Sanity check for client registration */
3935 if (NULL == (c = client_get (client)))
3938 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3941 data_msg = (struct GNUNET_MESH_Unicast *) message;
3942 /* Sanity check for message size */
3943 size = ntohs (message->size);
3944 if (sizeof (struct GNUNET_MESH_Unicast) +
3945 sizeof (struct GNUNET_MessageHeader) > size)
3948 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3952 /* Tunnel exists? */
3953 tid = ntohl (data_msg->tid);
3954 t = tunnel_get_by_local_id (c, tid);
3958 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3962 /* Is it a local tunnel? Then, does client own the tunnel? */
3963 if (NULL != t->client &&
3964 NULL != t->client->handle &&
3965 t->client->handle != client)
3968 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3972 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3973 &data_msg->destination.hashPubKey);
3974 /* Is the selected peer in the tunnel? */
3978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3982 /* Ok, everything is correct, send the message
3983 * (pretend we got it from a mesh peer)
3986 char buf[ntohs (message->size)];
3987 struct GNUNET_MESH_Unicast *copy;
3989 /* Work around const limitation */
3990 copy = (struct GNUNET_MESH_Unicast *) buf;
3991 memcpy (buf, data_msg, size);
3992 copy->oid = my_full_id;
3993 copy->tid = htonl (t->id.tid);
3994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3995 "MESH: calling generic handler...\n");
3996 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
3998 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4004 * Handler for client traffic directed to the origin
4006 * @param cls closure
4007 * @param client identification of the client
4008 * @param message the actual message
4011 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4012 const struct GNUNET_MessageHeader *message)
4014 struct GNUNET_MESH_ToOrigin *data_msg;
4015 struct GNUNET_PeerIdentity id;
4016 struct MeshClient *c;
4017 struct MeshTunnel *t;
4018 MESH_TunnelNumber tid;
4021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4022 "MESH: Got a ToOrigin request from a client!\n");
4024 /* Sanity check for client registration */
4025 if (NULL == (c = client_get (client)))
4028 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4031 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4032 /* Sanity check for message size */
4033 size = ntohs (message->size);
4034 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4035 sizeof (struct GNUNET_MessageHeader) > size)
4038 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4042 /* Tunnel exists? */
4043 tid = ntohl (data_msg->tid);
4044 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4047 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4050 t = tunnel_get_by_local_id (c, tid);
4054 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4058 /* It shouldn't be a local tunnel. */
4059 if (NULL != t->client)
4062 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4065 GNUNET_PEER_resolve(t->id.oid, &id);
4067 /* Ok, everything is correct, send the message
4068 * (pretend we got it from a mesh peer)
4071 char buf[ntohs (message->size)];
4072 struct GNUNET_MESH_ToOrigin *copy;
4074 /* Work around const limitation */
4075 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4076 memcpy (buf, data_msg, size);
4078 copy->tid = htonl (t->id.tid);
4079 copy->sender = my_full_id;
4080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4081 "MESH: calling generic handler...\n");
4082 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4084 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4090 * Handler for client traffic directed to all peers in a tunnel
4092 * @param cls closure
4093 * @param client identification of the client
4094 * @param message the actual message
4097 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4098 const struct GNUNET_MessageHeader *message)
4100 struct MeshClient *c;
4101 struct MeshTunnel *t;
4102 struct GNUNET_MESH_Multicast *data_msg;
4103 MESH_TunnelNumber tid;
4105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4106 "MESH: Got a multicast request from a client!\n");
4108 /* Sanity check for client registration */
4109 if (NULL == (c = client_get (client)))
4112 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4115 data_msg = (struct GNUNET_MESH_Multicast *) message;
4116 /* Sanity check for message size */
4117 if (sizeof (struct GNUNET_MESH_Multicast) +
4118 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4121 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4125 /* Tunnel exists? */
4126 tid = ntohl (data_msg->tid);
4127 t = tunnel_get_by_local_id (c, tid);
4131 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4135 /* Does client own tunnel? */
4136 if (t->client->handle != client)
4139 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4144 char buf[ntohs(message->size)];
4145 struct GNUNET_MESH_Multicast *copy;
4147 copy = (struct GNUNET_MESH_Multicast *)buf;
4148 memcpy(buf, message, ntohs(message->size));
4149 copy->oid = my_full_id;
4150 copy->tid = htonl(t->id.tid);
4151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4152 "MESH: calling generic handler...\n");
4153 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL, 0);
4156 /* receive done gets called when last copy is sent to a neighbor */
4161 * Functions to handle messages from clients
4163 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4164 {&handle_local_new_client, NULL,
4165 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4166 {&handle_local_tunnel_create, NULL,
4167 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4168 sizeof (struct GNUNET_MESH_TunnelMessage)},
4169 {&handle_local_tunnel_destroy, NULL,
4170 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4171 sizeof (struct GNUNET_MESH_TunnelMessage)},
4172 {&handle_local_connect_add, NULL,
4173 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4174 sizeof (struct GNUNET_MESH_PeerControl)},
4175 {&handle_local_connect_del, NULL,
4176 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4177 sizeof (struct GNUNET_MESH_PeerControl)},
4178 {&handle_local_connect_by_type, NULL,
4179 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4180 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4181 {&handle_local_unicast, NULL,
4182 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4183 {&handle_local_to_origin, NULL,
4184 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4185 {&handle_local_multicast, NULL,
4186 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4192 * To be called on core init/fail.
4194 * @param cls service closure
4195 * @param server handle to the server for this service
4196 * @param identity the public identity of this peer
4199 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4200 const struct GNUNET_PeerIdentity *identity)
4202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
4203 core_handle = server;
4204 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
4206 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
4207 GNUNET_SCHEDULER_shutdown();
4213 * Method called whenever a given peer connects.
4215 * @param cls closure
4216 * @param peer peer identity this notification is about
4217 * @param atsi performance data for the connection
4218 * @param atsi_count number of records in 'atsi'
4221 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4222 const struct GNUNET_ATS_Information *atsi,
4223 unsigned int atsi_count)
4225 struct MeshPeerInfo *peer_info;
4226 struct MeshPeerPath *path;
4228 #if MESH_DEBUG_CONNECTION
4229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
4230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4231 GNUNET_i2s(&my_full_id));
4233 peer_info = peer_info_get (peer);
4234 if (myid == peer_info->id)
4236 #if MESH_DEBUG_CONNECTION
4237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4241 #if MESH_DEBUG_CONNECTION
4244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4248 path = path_new (2);
4249 path->peers[0] = myid;
4250 path->peers[1] = peer_info->id;
4251 GNUNET_PEER_change_rc(myid, 1);
4252 GNUNET_PEER_change_rc(peer_info->id, 1);
4253 path_add_to_peer (peer_info, path, GNUNET_YES);
4258 * Method called whenever a peer disconnects.
4260 * @param cls closure
4261 * @param peer peer identity this notification is about
4264 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4266 struct MeshPeerInfo *pi;
4269 #if MESH_DEBUG_CONNECTION
4270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
4272 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4278 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4280 /* TODO: notify that the transmission failed */
4281 peer_info_cancel_transmission(pi, i);
4283 path_remove_from_peer (pi, pi->id, myid);
4284 #if MESH_DEBUG_CONNECTION
4287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4294 /******************************************************************************/
4295 /************************ MAIN FUNCTIONS ****************************/
4296 /******************************************************************************/
4299 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4301 * @param cls closure
4302 * @param key current key code
4303 * @param value value in the hash map
4304 * @return GNUNET_YES if we should continue to iterate,
4308 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4310 struct MeshTunnel *t = value;
4316 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4318 * @param cls closure
4319 * @param key current key code
4320 * @param value value in the hash map
4321 * @return GNUNET_YES if we should continue to iterate,
4325 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4327 struct MeshPeerInfo *p = value;
4328 peer_info_destroy (p);
4333 * Task run during shutdown.
4339 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4342 /* TODO: destroy tunnels? */
4343 if (core_handle != NULL)
4345 GNUNET_CORE_disconnect (core_handle);
4348 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
4349 GNUNET_CONTAINER_multihashmap_iterate(peers, &shutdown_peer, NULL);
4350 if (dht_handle != NULL)
4352 GNUNET_DHT_disconnect (dht_handle);
4357 GNUNET_SERVER_notification_context_destroy (nc);
4360 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4362 GNUNET_SCHEDULER_cancel (announce_id_task);
4363 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4369 * Process mesh requests.
4371 * @param cls closure
4372 * @param server the initialized server
4373 * @param c configuration to use
4376 run (void *cls, struct GNUNET_SERVER_Handle *server,
4377 const struct GNUNET_CONFIGURATION_Handle *c)
4379 struct MeshPeerInfo *peer;
4380 struct MeshPeerPath *p;
4383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4384 server_handle = server;
4385 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4386 CORE_QUEUE_SIZE, /* queue size */
4387 NULL, /* Closure passed to MESH functions */
4388 &core_init, /* Call core_init once connected */
4389 &core_connect, /* Handle connects */
4390 &core_disconnect, /* remove peers on disconnects */
4391 NULL, /* Don't notify about all incoming messages */
4392 GNUNET_NO, /* For header only in notification */
4393 NULL, /* Don't notify about all outbound messages */
4394 GNUNET_NO, /* For header-only out notification */
4395 core_handlers); /* Register these handlers */
4397 if (core_handle == NULL)
4400 GNUNET_SCHEDULER_shutdown ();
4405 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4410 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4411 GNUNET_SCHEDULER_shutdown ();
4414 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4415 GNUNET_free (keyfile);
4416 if (my_private_key == NULL)
4418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4419 _("Mesh service could not access hostkey. Exiting.\n"));
4420 GNUNET_SCHEDULER_shutdown ();
4423 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4424 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4425 &my_full_id.hashPubKey);
4426 myid = GNUNET_PEER_intern (&my_full_id);
4428 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4435 dht_handle = GNUNET_DHT_connect (c, 64);
4436 if (dht_handle == NULL)
4442 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4444 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4445 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4446 peers = GNUNET_CONTAINER_multihashmap_create (32);
4447 applications = GNUNET_CONTAINER_multihashmap_create (32);
4448 types = GNUNET_CONTAINER_multihashmap_create (32);
4450 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4451 nc = GNUNET_SERVER_notification_context_create (server_handle,
4453 GNUNET_SERVER_disconnect_notify (server_handle,
4454 &handle_local_client_disconnect,
4459 clients_tail = NULL;
4464 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4465 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4467 /* Create a peer_info for the local peer */
4468 peer = peer_info_get(&my_full_id);
4471 GNUNET_PEER_change_rc (myid, 1);
4472 path_add_to_peer(peer, p, GNUNET_YES);
4474 /* Scheduled the task to clean up when shutdown is called */
4475 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4482 * The main function for the mesh service.
4484 * @param argc number of arguments from the command line
4485 * @param argv command line arguments
4486 * @return 0 ok, 1 on error
4489 main (int argc, char *const *argv)
4493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4496 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");