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;
450 static struct GNUNET_DHT_Handle *dht_handle;
455 static struct GNUNET_SERVER_Handle *server_handle;
458 * Notification context, to send messages to local clients
460 static struct GNUNET_SERVER_NotificationContext *nc;
463 * Local peer own ID (memory efficient handle)
465 static GNUNET_PEER_Id myid;
468 * Local peer own ID (full value)
470 static struct GNUNET_PeerIdentity my_full_id;
475 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
480 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
483 * Tunnel ID for the next created tunnel (global tunnel number)
485 static MESH_TunnelNumber next_tid;
488 * Tunnel ID for the next incoming tunnel (local tunnel number)
490 static MESH_TunnelNumber next_local_tid;
493 * All application types provided by this peer
495 static struct GNUNET_CONTAINER_MultiHashMap *applications;
498 * All message types clients of this peer are interested in
500 static struct GNUNET_CONTAINER_MultiHashMap *types;
503 * Task to periodically announce provided applications
505 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
508 * Task to periodically announce itself in the network
510 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
513 unsigned int next_client_id;
517 /******************************************************************************/
518 /************************ ITERATORS ****************************/
519 /******************************************************************************/
521 /* FIXME move iterators here */
524 /******************************************************************************/
525 /************************ PERIODIC FUNCTIONS ****************************/
526 /******************************************************************************/
529 * Announce iterator over for each application provided by the peer
532 * @param key current key code
533 * @param value value in the hash map
534 * @return GNUNET_YES if we should continue to
539 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
541 /* FIXME are hashes in multihash map equal on all aquitectures? */
542 GNUNET_DHT_put (dht_handle,
545 GNUNET_DHT_RO_RECORD_ROUTE |
546 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
547 GNUNET_BLOCK_TYPE_TEST,
548 sizeof (struct GNUNET_PeerIdentity),
549 (const char *) &my_full_id,
551 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
552 &mesh_debug, "DHT_put for app completed");
554 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
556 APP_ANNOUNCE_TIME, NULL, NULL);
563 * Periodically announce what applications are provided by local clients
566 * @param tc task context
569 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
571 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
573 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
577 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
579 announce_applications_task =
580 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
588 * Periodically announce self id in the DHT
591 * @param tc task context
594 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
596 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
598 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
602 * - Set data expiration in function of X
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
606 GNUNET_h2s_full (&my_full_id.hashPubKey));
607 GNUNET_DHT_put (dht_handle, /* DHT handle */
608 &my_full_id.hashPubKey, /* Key to use */
609 10U, /* Replication level */
610 GNUNET_DHT_RO_RECORD_ROUTE |
611 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
612 GNUNET_BLOCK_TYPE_TEST, /* Block type */
613 sizeof(my_full_id), /* Size of the data */
614 (char *)&my_full_id, /* Data itself */
615 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
616 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
618 &mesh_debug, "DHT_put for id completed");
620 NULL, /* Continuation */
621 NULL); /* Continuation closure */
624 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
629 * Function to process paths received for a new peer addition. The recorded
630 * paths form the initial tunnel, which can be optimized later.
631 * Called on each result obtained for the DHT search.
634 * @param exp when will this value expire
635 * @param key key of the result
636 * @param type type of the result
637 * @param size number of bytes in data
638 * @param data pointer to the result data
641 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
642 const GNUNET_HashCode * key,
643 const struct GNUNET_PeerIdentity *get_path,
644 unsigned int get_path_length,
645 const struct GNUNET_PeerIdentity *put_path,
646 unsigned int put_path_length,
647 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
650 /******************************************************************************/
651 /****************** GENERAL HELPER FUNCTIONS ************************/
652 /******************************************************************************/
655 * Check if client has registered with the service and has not disconnected
657 * @param client the client to check
659 * @return non-NULL if client exists in the global DLL
661 static struct MeshClient *
662 client_get (struct GNUNET_SERVER_Client *client)
664 struct MeshClient *c;
669 if (c->handle == client)
678 * Checks if a given client has subscribed to certain message type
680 * @param message_type Type of message to check
681 * @param c Client to check
683 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
688 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
692 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
693 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
697 client_allow_send(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
699 struct MeshDataDescriptor *info = cls;
701 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
703 info->timeout_task = GNUNET_SCHEDULER_NO_TASK;
704 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
709 * Search for a tunnel by global ID using full PeerIdentities
711 * @param oid owner of the tunnel
712 * @param tid global tunnel number
714 * @return tunnel handler, NULL if doesn't exist
716 static struct MeshTunnel *
717 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
721 * Notify a tunnel that a connection has broken that affects at least
724 * @param t Tunnel affected.
725 * @param p1 Peer that got disconnected from p2.
726 * @param p2 Peer that got disconnected from p1.
728 * @return Short ID of the peer disconnected (either p1 or p2).
729 * 0 if the tunnel remained unaffected.
731 static GNUNET_PEER_Id
732 tunnel_notify_connection_broken (struct MeshTunnel *t,
738 * Send the message to all clients that have subscribed to its type
740 * @param msg Pointer to the message itself
741 * @return number of clients this message was sent to
744 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
745 const struct GNUNET_MessageHeader *payload)
747 struct GNUNET_PeerIdentity *oid;
748 struct MeshClient *c;
749 struct MeshTunnel *t;
750 MESH_TunnelNumber *tid;
753 char cbuf[htons(msg->size)];
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
756 type = ntohs (payload->type);
757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
759 memcpy (cbuf, msg, sizeof(cbuf));
760 switch (htons(msg->type))
762 struct GNUNET_MESH_Unicast *uc;
763 struct GNUNET_MESH_Multicast *mc;
764 struct GNUNET_MESH_ToOrigin *to;
766 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
767 uc = (struct GNUNET_MESH_Unicast *) cbuf;
771 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
772 mc = (struct GNUNET_MESH_Multicast *) cbuf;
776 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
777 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
785 t = tunnel_get (oid, ntohl(*tid));
791 *tid = htonl (t->local_tid);
792 for (count = 0, c = clients; c != NULL; c = c->next)
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
795 if (client_is_subscribed (type, c))
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
799 GNUNET_SERVER_notification_context_unicast (
802 (struct GNUNET_MessageHeader *) cbuf,
811 * Notify the client that owns the tunnel that a peer has connected to it
812 * (the requested path to it has been confirmed).
814 * @param t Tunnel whose owner to notify
815 * @param id Short id of the peer that has connected
818 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
820 struct GNUNET_MESH_PeerControl pc;
822 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
823 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
824 pc.tunnel_id = htonl (t->local_tid);
825 GNUNET_PEER_resolve (id, &pc.peer);
826 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
827 &pc.header, GNUNET_NO);
832 * Notify all clients (not depending on registration status) that the incoming
833 * tunnel is no longer valid.
835 * @param t Tunnel that was destroyed.
838 send_clients_tunnel_destroy (struct MeshTunnel *t)
840 struct GNUNET_MESH_TunnelMessage msg;
842 msg.header.size = htons (sizeof (msg));
843 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
844 msg.tunnel_id = htonl (t->local_tid);
845 GNUNET_SERVER_notification_context_broadcast(nc, &msg.header, GNUNET_NO);
850 * Function called to notify a client about the socket
851 * being ready to queue more data. "buf" will be
852 * NULL and "size" zero if the socket was closed for
853 * writing in the meantime.
856 * @param size number of bytes available in buf
857 * @param buf where the callee should write the message
858 * @return number of bytes written to buf
861 send_core_create_path (void *cls, size_t size, void *buf);
865 * Function called to notify a client about the socket
866 * being ready to queue more data. "buf" will be
867 * NULL and "size" zero if the socket was closed for
868 * writing in the meantime.
870 * @param cls closure (data itself)
871 * @param size number of bytes available in buf
872 * @param buf where the callee should write the message
874 * @return number of bytes written to buf
877 send_core_data_multicast (void *cls, size_t size, void *buf);
881 * Cancel a core transmission that was already requested and free all resources
882 * associated to the request.
884 * @param peer PeeInfo of the peer whose transmission is cancelled.
885 * @param i Position of the transmission to be cancelled.
888 peer_info_cancel_transmission(struct MeshPeerInfo *peer, unsigned int i)
890 if (NULL != peer->core_transmit[i])
892 struct MeshDataDescriptor *dd;
893 struct MeshPathInfo *path_info;
897 struct GNUNET_PeerIdentity id;
899 GNUNET_PEER_resolve (peer->id, &id);
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "MESH: Cancelling data transmission at %s [%u]\n",
904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
905 "MESH: message type %u\n",
909 /* TODO: notify that tranmission has failed */
910 switch (peer->types[i])
912 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
913 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
914 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
917 if (0 == --(*dd->copies))
919 GNUNET_free (dd->copies);
920 GNUNET_free (dd->data);
923 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
925 path_info = peer->infos[i];
926 path_destroy(path_info->path);
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
932 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
933 peer->core_transmit[i] = NULL;
934 GNUNET_free (peer->infos[i]);
940 * Get a unused CORE slot to transmit a message to a peer. If all the slots
941 * are used, cancel one and return it's position.
943 * @param peer PeerInfo of the neighbor we want to transmit to.
945 * @return The index of an available slot to transmit to the neighbor.
948 peer_info_transmit_slot (struct MeshPeerInfo *peer)
952 for (i = 0; peer->core_transmit[i]; i++)
954 if (i == (CORE_QUEUE_SIZE - 1))
956 /* All positions are taken! Overwriting! */
958 peer_info_cancel_transmission(peer, 0);
967 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
968 * and insert it in the appropiate structures if the peer is not known yet.
970 * @param peer Full identity of the peer.
972 * @return Existing or newly created peer info.
974 static struct MeshPeerInfo *
975 peer_info_get (const struct GNUNET_PeerIdentity *peer)
977 struct MeshPeerInfo *peer_info;
979 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
980 if (NULL == peer_info)
983 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
984 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
985 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
986 peer_info->id = GNUNET_PEER_intern (peer);
994 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
995 * and insert it in the appropiate structures if the peer is not known yet.
997 * @param peer Short identity of the peer.
999 * @return Existing or newly created peer info.
1001 static struct MeshPeerInfo *
1002 peer_info_get_short (const GNUNET_PEER_Id peer)
1004 struct GNUNET_PeerIdentity id;
1006 GNUNET_PEER_resolve(peer, &id);
1007 return peer_info_get(&id);
1012 * Iterator to remove the tunnel from the list of tunnels a peer participates
1015 * @param cls Closure (tunnel info)
1016 * @param key GNUNET_PeerIdentity of the peer (unused)
1017 * @param value PeerInfo of the peer
1019 * @return always GNUNET_YES, to keep iterating
1022 peer_info_delete_tunnel (void* cls, const GNUNET_HashCode* key, void* value)
1024 struct MeshTunnel *t = cls;
1025 struct MeshPeerInfo *peer = value;
1028 for (i = 0; i < peer->ntunnels; i++)
1030 if (0 == memcmp(&peer->tunnels[i]->id,
1032 sizeof(struct MESH_TunnelID)))
1035 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1036 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1045 * Core callback to write a
1047 * @param cls Closure (MeshDataDescriptor with data in "data" member).
1048 * @param size Number of bytes available in buf.
1049 * @param buf Where the to write the message.
1051 * @return number of bytes written to buf
1054 send_core_data_raw (void *cls, size_t size, void *buf)
1056 struct MeshDataDescriptor *info = cls;
1057 struct GNUNET_MessageHeader *msg;
1060 GNUNET_assert (NULL != info);
1061 GNUNET_assert (NULL != info->data);
1062 msg = (struct GNUNET_MessageHeader *) info->data;
1063 total_size = ntohs (msg->size);
1065 if (total_size > size)
1067 struct GNUNET_PeerIdentity id;
1069 GNUNET_PEER_resolve (info->peer->id, &id);
1070 info->peer->core_transmit[info->handler_n] =
1071 GNUNET_CORE_notify_transmit_ready(core_handle,
1074 GNUNET_TIME_UNIT_FOREVER_REL,
1077 &send_core_data_raw,
1081 info->peer->core_transmit[info->handler_n] = NULL;
1082 memcpy (buf, msg, total_size);
1083 GNUNET_free (info->data);
1090 * Sends an already built message to a peer, properly registrating
1091 * all used resources.
1093 * @param message Message to send.
1094 * @param peer Short ID of the neighbor whom to send the message.
1097 send_message (const struct GNUNET_MessageHeader *message,
1098 const struct GNUNET_PeerIdentity *peer)
1100 struct MeshDataDescriptor *info;
1101 struct MeshPeerInfo *neighbor;
1105 size = ntohs (message->size);
1106 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1107 info->data = GNUNET_malloc (size);
1108 memcpy (info->data, message, size);
1109 neighbor = peer_info_get (peer);
1110 i = peer_info_transmit_slot (neighbor);
1111 info->handler_n = i;
1112 info->peer = neighbor;
1113 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1114 neighbor->infos[i] = info;
1115 neighbor->core_transmit[i] =
1116 GNUNET_CORE_notify_transmit_ready(core_handle,
1119 GNUNET_TIME_UNIT_FOREVER_REL,
1122 &send_core_data_raw,
1129 * Sends a CREATE PATH message for a path to a peer, properly registrating
1130 * all used resources.
1132 * @param peer PeerInfo of the final peer for whom this path is being created.
1133 * @param p Path itself.
1134 * @param t Tunnel for which the path is created.
1137 send_create_path (struct MeshPeerInfo *peer,
1138 struct MeshPeerPath *p,
1139 struct MeshTunnel *t)
1141 struct GNUNET_PeerIdentity id;
1142 struct MeshPathInfo *path_info;
1143 struct MeshPeerInfo *neighbor;
1148 p = tree_get_path_to_peer(t->tree, peer->id);
1155 for (i = 0; i < p->length; i++)
1157 if (p->peers[i] == myid)
1160 if (i >= p->length - 1)
1166 GNUNET_PEER_resolve(p->peers[i + 1], &id);
1168 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1169 path_info->path = p;
1171 neighbor = peer_info_get(&id);
1172 path_info->peer = neighbor;
1173 path_info->pos = peer_info_transmit_slot(neighbor);
1174 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1175 neighbor->infos[path_info->pos] = path_info;
1176 neighbor->core_transmit[path_info->pos] =
1177 GNUNET_CORE_notify_transmit_ready (
1178 core_handle, /* handle */
1181 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1183 sizeof (struct GNUNET_MESH_ManipulatePath)
1184 + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1185 &send_core_create_path, /* callback */
1186 path_info); /* cls */
1191 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1193 * @param t Tunnel whose path to destroy.
1194 * @param destination Short ID of the peer to whom the path to destroy.
1197 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1199 struct GNUNET_MESH_ManipulatePath *msg;
1200 struct GNUNET_PeerIdentity *pi;
1201 struct MeshPeerPath *p;
1205 p = tree_get_path_to_peer(t->tree, destination);
1211 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1212 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1213 msg = GNUNET_malloc (size);
1214 msg->header.size = htons (size);
1215 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1216 msg->tid = htonl (t->id.tid);
1217 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1218 for (i = 0; i < p->length; i++)
1220 GNUNET_PEER_resolve(p->peers[i], &pi[i]);
1222 send_message (&msg->header, path_get_first_hop(t->tree, destination));
1228 * Try to establish a new connection to this peer.
1229 * Use the best path for the given tunnel.
1230 * If the peer doesn't have any path to it yet, try to get one.
1231 * If the peer already has some path, send a CREATE PATH towards it.
1233 * @param peer PeerInfo of the peer.
1234 * @param t Tunnel for which to create the path, if possible.
1237 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1239 struct MeshPeerPath *p;
1240 struct MeshPathInfo *path_info;
1242 if (NULL != peer->path_head)
1244 p = tree_get_path_to_peer(t->tree, peer->id);
1247 send_create_path(peer, p, t);
1252 send_client_peer_connected(t, myid);
1255 else if (NULL == peer->dhtget)
1257 struct GNUNET_PeerIdentity id;
1259 GNUNET_PEER_resolve(peer->id, &id);
1260 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1261 path_info->peer = peer;
1263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1264 "MESH: Starting DHT GET for peer %s\n",
1266 peer->dhtgetcls = path_info;
1268 GNUNET_DHT_get_start(dht_handle, /* handle */
1269 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1270 GNUNET_BLOCK_TYPE_TEST, /* type */
1271 &id.hashPubKey, /* key to search */
1272 4, /* replication level */
1273 GNUNET_DHT_RO_RECORD_ROUTE |
1274 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1276 0, /* xquery bits */
1277 &dht_get_id_handler,
1280 /* Otherwise, there is no path but the DHT get is already started. */
1285 * Destroy the peer_info and free any allocated resources linked to it
1287 * @param pi The peer_info to destroy.
1289 * @return GNUNET_OK on success
1292 peer_info_destroy (struct MeshPeerInfo *pi)
1294 struct GNUNET_PeerIdentity id;
1295 struct MeshPeerPath *p;
1296 struct MeshPeerPath *nextp;
1299 GNUNET_PEER_resolve (pi->id, &id);
1300 GNUNET_PEER_change_rc (pi->id, -1);
1303 GNUNET_CONTAINER_multihashmap_remove (peers,&id.hashPubKey, pi))
1306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1307 "MESH: removing peer %s, not in hashmap\n",
1310 if (NULL != pi->dhtget)
1312 GNUNET_DHT_get_stop(pi->dhtget);
1313 GNUNET_free (pi->dhtgetcls);
1315 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1317 peer_info_cancel_transmission(pi, i);
1323 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1333 * Remove all paths that rely on a direct connection between p1 and p2
1334 * from the peer itself and notify all tunnels about it.
1336 * @param peer PeerInfo of affected peer.
1337 * @param p1 GNUNET_PEER_Id of one peer.
1338 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1341 * TODO: optimize (see below)
1344 path_remove_from_peer (struct MeshPeerInfo *peer,
1348 struct MeshPeerPath *p;
1349 struct MeshPeerPath *aux;
1350 struct MeshPeerInfo *peer_d;
1352 unsigned int destroyed;
1358 p = peer->path_head;
1362 for (i = 0; i < (p->length - 1); i++)
1364 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1365 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1367 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1378 for (i = 0; i < peer->ntunnels; i++)
1380 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1382 * Problem: one or more peers have been deleted from the tunnel tree.
1383 * We don't know who they are to try to add them again.
1384 * We need to try to find a new path for each of the disconnected peers.
1385 * Some of them might already have a path to reach them that does not
1386 * involve p1 and p2. Adding all anew might render in a better tree than
1387 * the trivial immediate fix.
1389 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1390 * its children will be reachable trough him.
1392 peer_d = peer_info_get_short(d);
1395 for (p = peer_d->path_head; NULL != p; p = p->next)
1397 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
1405 /* No callback, as peer will be already disconnected */
1406 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
1410 peer_info_connect (peer_d, peer->tunnels[i]);
1417 * Add the path to the peer and update the path used to reach it in case this
1420 * @param peer_info Destination peer to add the path to.
1421 * @param path New path to add. Last peer must be the peer in arg 1.
1422 * Path will be either used of freed if already known.
1424 * TODO: trim the part from origin to us? Add it as path to origin?
1427 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1429 struct MeshPeerPath *aux;
1433 if (NULL == peer_info || NULL == path)
1439 l = path_get_length (path);
1446 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1448 l2 = path_get_length (aux);
1451 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1452 peer_info->path_tail, aux, path);
1456 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1463 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1470 * Add the path to the origin peer and update the path used to reach it in case
1471 * this is the shortest.
1472 * The path is given in peer_info -> destination, therefore we turn the path
1473 * upside down first.
1475 * @param peer_info Peer to add the path to, being the origin of the path.
1476 * @param path New path to add after being inversed.
1479 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1482 path_add_to_peer (peer_info, path);
1487 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1488 * to obtain a local peer -> destination path and interning the peer ids.
1490 * @return Newly allocated and created path
1492 static struct MeshPeerPath *
1493 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1494 unsigned int get_path_length,
1495 const struct GNUNET_PeerIdentity *put_path,
1496 unsigned int put_path_length)
1498 struct MeshPeerPath *p;
1504 GNUNET_PEER_change_rc(myid, 1);
1505 i = get_path_length;
1506 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1507 for (i--; i >= 0; i--)
1509 id = GNUNET_PEER_intern (&get_path[i]);
1510 if (p->length > 0 && id == p->peers[p->length - 1])
1512 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1513 GNUNET_PEER_change_rc(id, -1);
1517 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1518 "MESH: Adding from GET: %s.\n",
1519 GNUNET_i2s(&get_path[i]));
1521 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1522 p->peers[p->length - 1] = id;
1525 i = put_path_length;
1526 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1527 for (i--; i >= 0; i--)
1529 id = GNUNET_PEER_intern (&put_path[i]);
1532 /* PUT path went through us, so discard the path up until now and start
1533 * from here to get a much shorter (and loop-free) path.
1538 if (p->length > 0 && id == p->peers[p->length - 1])
1540 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1541 GNUNET_PEER_change_rc(id, -1);
1545 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1546 "MESH: Adding from PUT: %s.\n",
1547 GNUNET_i2s(&put_path[i]));
1549 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1550 p->peers[p->length - 1] = id;
1554 if (get_path_length > 0)
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556 "MESH: (first of GET: %s)\n",
1557 GNUNET_h2s_full(&get_path[0].hashPubKey));
1558 if (put_path_length > 0)
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 "MESH: (first of PUT: %s)\n",
1561 GNUNET_h2s_full(&put_path[0].hashPubKey));
1562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1563 "MESH: In total: %d hops\n",
1565 for (i = 0; i < p->length; i++)
1567 struct GNUNET_PeerIdentity peer_id;
1569 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1573 GNUNET_h2s_full(&peer_id.hashPubKey));
1581 * Send keepalive packets for a peer
1583 * @param cls Closure (tunnel for which to send the keepalive).
1584 * @param tc Notification context.
1586 * TODO: implement explicit multicast keepalive?
1589 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1593 * Search for a tunnel among the tunnels for a client
1595 * @param c the client whose tunnels to search in
1596 * @param tid the local id of the tunnel
1598 * @return tunnel handler, NULL if doesn't exist
1600 static struct MeshTunnel *
1601 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1603 GNUNET_HashCode hash;
1605 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1606 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1608 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1610 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1615 * Search for a tunnel by global ID using PEER_ID
1617 * @param pi owner of the tunnel
1618 * @param tid global tunnel number
1620 * @return tunnel handler, NULL if doesn't exist
1622 static struct MeshTunnel *
1623 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1625 struct MESH_TunnelID id;
1626 GNUNET_HashCode hash;
1631 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1632 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1637 * Search for a tunnel by global ID using full PeerIdentities
1639 * @param oid owner of the tunnel
1640 * @param tid global tunnel number
1642 * @return tunnel handler, NULL if doesn't exist
1644 static struct MeshTunnel *
1645 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1647 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1652 * Callback used to notify a client owner of a tunnel that a peer has
1653 * disconnected, most likely because of a path change.
1655 * @param n Node in the tree representing the disconnected peer
1657 * FIXME: pass tunnel via cls, make param just a peer identity
1660 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1662 struct MeshPeerInfo *peer;
1664 if (NULL != n->t->client && NULL != nc)
1666 struct GNUNET_MESH_PeerControl msg;
1667 msg.header.size = htons (sizeof (msg));
1668 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1669 msg.tunnel_id = htonl (n->t->local_tid);
1670 GNUNET_PEER_resolve (n->peer, &msg.peer);
1671 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1672 &msg.header, GNUNET_NO);
1674 peer = peer_info_get_short(n->peer);
1675 peer_info_connect(peer, n->t);
1680 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1681 * needed rescources.
1682 * If peer already exists, reevaluate shortest path and change if different.
1684 * @param t Tunnel we want to add a new peer to
1685 * @param peer PeerInfo of the peer being added
1689 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1691 struct GNUNET_PeerIdentity id;
1692 struct MeshPeerPath *best_p;
1693 struct MeshPeerPath *p;
1694 unsigned int best_cost;
1697 GNUNET_PEER_resolve(peer->id, &id);
1699 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1702 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1703 GNUNET_CONTAINER_multihashmap_put(
1707 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1710 if (NULL != (p = peer->path_head))
1713 best_cost = path_get_cost(t->tree, p);
1716 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1723 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1724 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1725 t->path_refresh_task =
1726 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1730 /* Start a DHT get if necessary */
1731 peer_info_connect(peer, t);
1736 * Add a path to a tunnel which we don't own, just to remember the next hop.
1737 * If destination node was already in the tunnel, the first hop information
1738 * will be replaced with the new path.
1740 * @param t Tunnel we want to add a new peer to
1741 * @param p Path to add
1742 * @param own_pos Position of local node in path.
1746 tunnel_add_path (struct MeshTunnel *t,
1747 struct MeshPeerPath *p,
1748 unsigned int own_pos)
1750 struct GNUNET_PeerIdentity id;
1752 GNUNET_assert (0 != own_pos);
1753 tree_add_path(t->tree, p, NULL);
1754 if (NULL == t->tree->me)
1755 t->tree->me = tree_find_peer(t->tree->root, p->peers[own_pos]);
1756 if (own_pos < p->length - 1)
1758 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1759 tree_update_first_hops(t->tree, t->tree->me, &id);
1765 * Notifies a tunnel that a connection has broken that affects at least
1766 * some of its peers. Sends a notification towards the root of the tree.
1767 * In case the peer is the owner of the tree, notifies the client that owns
1768 * the tunnel and tries to reconnect.
1770 * @param t Tunnel affected.
1771 * @param p1 Peer that got disconnected from p2.
1772 * @param p2 Peer that got disconnected from p1.
1774 * @return Short ID of the peer disconnected (either p1 or p2).
1775 * 0 if the tunnel remained unaffected.
1777 static GNUNET_PEER_Id
1778 tunnel_notify_connection_broken (struct MeshTunnel *t,
1784 pid = tree_notify_connection_broken (t->tree,
1787 ¬ify_peer_disconnected);
1788 if (myid != p1 && myid != p2)
1794 if (NULL != t->tree->me->parent)
1796 /* We are the peer still connected, notify owner of the disconnection. */
1797 struct GNUNET_MESH_PathBroken msg;
1798 struct GNUNET_PeerIdentity neighbor;
1800 msg.header.size = htons (sizeof (msg));
1801 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1802 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1803 msg.tid = htonl (t->id.tid);
1804 msg.peer1 = my_full_id;
1805 GNUNET_PEER_resolve (pid, &msg.peer2);
1806 GNUNET_PEER_resolve (t->tree->me->parent->peer, &neighbor);
1807 send_message (&msg.header, &neighbor);
1815 * Send a message in a tunnel in multicast, sending a copy to each child node
1816 * down the local one in the tunnel tree.
1818 * @param t Tunnel in which to send the data.
1819 * @param msg Message to be sent
1821 * @return Number of copies sent.
1824 tunnel_send_multicast (struct MeshTunnel *t,
1825 const struct GNUNET_MessageHeader *msg)
1827 struct GNUNET_PeerIdentity neighbor;
1828 struct MeshDataDescriptor *info;
1829 struct MeshTunnelTreeNode *n;
1830 unsigned int *copies;
1835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1836 "MESH: sending a multicast packet...\n");
1837 size = ntohs (msg->size);
1838 GNUNET_assert (NULL != t->tree->me);
1839 n = t->tree->me->children_head;
1842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1843 "MESH: no children in the tree, no one to send.\n");
1846 copies = GNUNET_malloc (sizeof (unsigned int));
1847 for (*copies = 0; NULL != n; n = n->next)
1849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (%u copies)\n", *copies);
1850 n = t->tree->me->children_head;
1851 data = GNUNET_malloc (size);
1852 memcpy (data, msg, size);
1855 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1858 info->copies = copies;
1859 if (NULL != t->client)
1861 info->client = t->client->handle;
1862 info->timeout_task = GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT,
1866 info->destination = n->peer;
1867 GNUNET_PEER_resolve (n->peer, &neighbor);
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1869 "MESH: sending to %s...\n",
1870 GNUNET_i2s (&neighbor));
1871 info->peer = peer_info_get(&neighbor);
1872 GNUNET_assert (NULL != info->peer);
1873 i = peer_info_transmit_slot(info->peer);
1874 info->handler_n = i;
1875 info->peer->infos[i] = info;
1876 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1877 info->peer->core_transmit[i] =
1878 GNUNET_CORE_notify_transmit_ready (core_handle,
1881 GNUNET_TIME_UNIT_FOREVER_REL,
1884 &send_core_data_multicast, info);
1892 * Send a message to all peers in this tunnel that the tunnel is no longer
1895 * @param t The tunnel whose peers to notify.
1898 tunnel_send_destroy (struct MeshTunnel *t)
1900 struct GNUNET_MESH_TunnelDestroy msg;
1902 msg.header.size = htons (sizeof (msg));
1903 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
1904 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1905 msg.tid = htonl (t->id.tid);
1906 tunnel_send_multicast (t, &msg.header);
1912 * Destroy the tunnel and free any allocated resources linked to it
1914 * @param t the tunnel to destroy
1916 * @return GNUNET_OK on success
1919 tunnel_destroy (struct MeshTunnel *t)
1921 struct MeshClient *c;
1922 struct MeshQueue *q;
1923 struct MeshQueue *qn;
1924 GNUNET_HashCode hash;
1933 struct GNUNET_PeerIdentity id;
1935 GNUNET_PEER_resolve(t->id.oid, &id);
1936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1937 "MESH: destroying tunnel %s [%x]\n",
1941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
1945 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
1946 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
1951 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1953 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
1957 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1959 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1960 GNUNET_break (GNUNET_YES ==
1961 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t));
1964 if (NULL != t->peers)
1966 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
1967 &peer_info_delete_tunnel, t);
1968 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
1973 if (NULL != q->data)
1974 GNUNET_free (q->data);
1978 /* TODO cancel core transmit ready in case it was active */
1980 tree_destroy(t->tree);
1981 if (NULL != t->dht_get_type)
1982 GNUNET_DHT_get_stop(t->dht_get_type);
1983 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
1984 GNUNET_SCHEDULER_cancel(t->timeout_task);
1985 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
1986 GNUNET_SCHEDULER_cancel(t->path_refresh_task);
1993 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1994 * that are no longer needed, as well as nodes of no longer reachable peers.
1995 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1997 * @param t Tunnel from which to remove the path.
1998 * @param p Peer which should be removed.
2001 tunnel_delete_peer (struct MeshTunnel *t,
2002 GNUNET_PEER_Id peer)
2004 GNUNET_break (GNUNET_OK == tree_del_peer (t->tree, peer, NULL));
2005 if (NULL == t->tree->root)
2011 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2012 * client when the client disconnects.
2014 * @param cls closure (client that is disconnecting)
2015 * @param key the hash of the local tunnel id (used to access the hashmap)
2016 * @param value the value stored at the key (tunnel to destroy)
2018 * @return GNUNET_OK on success
2021 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2023 struct MeshTunnel *t = value;
2026 if (NULL != t->dht_get_type)
2028 GNUNET_DHT_get_stop (t->dht_get_type);
2030 r = tunnel_destroy (t);
2036 * Timeout function, destroys tunnel if called
2038 * @param cls Closure (tunnel to destroy).
2039 * @param tc TaskContext
2042 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2044 struct MeshTunnel *t = cls;
2046 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2048 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2053 * Resets the tunnel timeout. Starts it if no timeout was running.
2055 * @param t Tunnel whose timeout to reset.
2058 tunnel_reset_timeout (struct MeshTunnel *t)
2060 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2061 GNUNET_SCHEDULER_cancel (t->timeout_task);
2062 t->timeout_task = GNUNET_SCHEDULER_add_delayed (
2063 GNUNET_TIME_relative_multiply(REFRESH_PATH_TIME, 4),
2069 /******************************************************************************/
2070 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2071 /******************************************************************************/
2074 * Function called to notify a client about the socket
2075 * being ready to queue more data. "buf" will be
2076 * NULL and "size" zero if the socket was closed for
2077 * writing in the meantime.
2079 * @param cls closure
2080 * @param size number of bytes available in buf
2081 * @param buf where the callee should write the message
2082 * @return number of bytes written to buf
2085 send_core_create_path (void *cls, size_t size, void *buf)
2087 struct MeshPathInfo *info = cls;
2088 struct GNUNET_MESH_ManipulatePath *msg;
2089 struct GNUNET_PeerIdentity *peer_ptr;
2090 struct MeshPeerInfo *peer = info->peer;
2091 struct MeshTunnel *t = info->t;
2092 struct MeshPeerPath *p = info->path;
2096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2097 "MESH: CREATE PATH sending...\n");
2099 sizeof (struct GNUNET_MESH_ManipulatePath) +
2100 p->length * sizeof (struct GNUNET_PeerIdentity);
2102 if (size < size_needed || NULL == buf)
2104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
2108 info->peer->core_transmit[info->pos] =
2109 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2110 GNUNET_TIME_UNIT_FOREVER_REL,
2111 path_get_first_hop (t->tree, peer->id),
2113 &send_core_create_path,
2117 info->peer->core_transmit[info->pos] = NULL;
2120 struct GNUNET_PeerIdentity id;
2122 GNUNET_PEER_resolve (peer->id, &id);
2123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2124 "MESH: setting core_transmit %s [%u] to NULL\n",
2129 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2130 msg->header.size = htons (size_needed);
2131 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2132 msg->tid = ntohl (t->id.tid);
2134 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2135 for (i = 0; i < p->length; i++)
2137 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2144 "MESH: CREATE PATH (%u bytes long) sent!\n",
2151 * Function called to notify a client about the socket
2152 * being ready to queue more data. "buf" will be
2153 * NULL and "size" zero if the socket was closed for
2154 * writing in the meantime.
2156 * @param cls closure (data itself)
2157 * @param size number of bytes available in buf
2158 * @param buf where the callee should write the message
2160 * @return number of bytes written to buf
2163 send_core_data_multicast (void *cls, size_t size, void *buf)
2165 struct MeshDataDescriptor *info = cls;
2168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2169 GNUNET_assert (NULL != info);
2170 GNUNET_assert (NULL != info->peer);
2171 total_size = info->size;
2172 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2174 if (total_size > size)
2177 struct GNUNET_PeerIdentity id;
2179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2180 "MESH: Multicast: retransmitting... (%u/%u)\n",
2182 GNUNET_PEER_resolve(info->peer->id, &id);
2183 info->peer->core_transmit[info->handler_n] =
2184 GNUNET_CORE_notify_transmit_ready (core_handle,
2187 GNUNET_TIME_UNIT_FOREVER_REL,
2190 &send_core_data_multicast,
2194 info->peer->core_transmit[info->handler_n] = NULL;
2195 info->peer->infos[info->handler_n] = NULL;
2196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2197 memcpy (buf, info->data, total_size);
2200 struct GNUNET_MESH_Multicast *mc;
2201 struct GNUNET_MessageHeader *mh;
2204 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2206 mc = (struct GNUNET_MESH_Multicast *) mh;
2207 mh = (struct GNUNET_MessageHeader *) &mc[1];
2208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2209 "MESH: multicast, payload type %u\n",
2211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2212 "MESH: multicast, payload size %u\n",
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2223 if (0 == --(*info->copies))
2225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
2226 if (NULL != info->client)
2228 if (GNUNET_SCHEDULER_NO_TASK != info->timeout_task)
2230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2231 "MESH: cancelling client timeout (%u)...\n",
2232 info->timeout_task);
2233 GNUNET_SCHEDULER_cancel(info->timeout_task);
2234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
2235 GNUNET_SERVER_receive_done (info->client, GNUNET_OK);
2238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing memory...\n");
2239 GNUNET_free (info->data);
2240 GNUNET_free (info->copies);
2242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2250 * Function called to notify a client about the socket
2251 * being ready to queue more data. "buf" will be
2252 * NULL and "size" zero if the socket was closed for
2253 * writing in the meantime.
2255 * @param cls closure (MeshDataDescriptor)
2256 * @param size number of bytes available in buf
2257 * @param buf where the callee should write the message
2258 * @return number of bytes written to buf
2261 send_core_path_ack (void *cls, size_t size, void *buf)
2263 struct MeshDataDescriptor *info = cls;
2264 struct GNUNET_MESH_PathACK *msg = buf;
2266 GNUNET_assert (NULL != info);
2269 info->peer->core_transmit[info->handler_n] = NULL;
2271 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2276 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2277 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2278 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2279 msg->tid = htonl (info->origin->tid);
2280 msg->peer_id = my_full_id;
2282 /* TODO add signature */
2284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2285 return sizeof (struct GNUNET_MESH_PathACK);
2289 /******************************************************************************/
2290 /******************** MESH NETWORK HANDLERS **************************/
2291 /******************************************************************************/
2295 * Core handler for path creation
2297 * @param cls closure
2298 * @param message message
2299 * @param peer peer identity this notification is about
2300 * @param atsi performance data
2301 * @param atsi_count number of records in 'atsi'
2303 * @return GNUNET_OK to keep the connection open,
2304 * GNUNET_SYSERR to close it (signal serious error)
2307 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2308 const struct GNUNET_MessageHeader *message,
2309 const struct GNUNET_ATS_Information *atsi,
2310 unsigned int atsi_count)
2312 unsigned int own_pos;
2315 MESH_TunnelNumber tid;
2316 struct GNUNET_MESH_ManipulatePath *msg;
2317 struct GNUNET_PeerIdentity *pi;
2318 GNUNET_HashCode hash;
2319 struct MeshPeerPath *path;
2320 struct MeshPeerInfo *dest_peer_info;
2321 struct MeshPeerInfo *orig_peer_info;
2322 struct MeshTunnel *t;
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 "MESH: Received a path create msg [%s]\n",
2326 GNUNET_i2s(&my_full_id));
2327 size = ntohs (message->size);
2328 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2330 GNUNET_break_op (0);
2334 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2335 if (size % sizeof (struct GNUNET_PeerIdentity))
2337 GNUNET_break_op (0);
2340 size /= sizeof (struct GNUNET_PeerIdentity);
2343 GNUNET_break_op (0);
2346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2347 "MESH: path has %u hops.\n",
2349 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2351 tid = ntohl (msg->tid);
2352 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2353 t = tunnel_get (pi, tid);
2354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2355 "MESH: path is for tunnel %s [%X].\n",
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2361 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2362 t->id.oid = GNUNET_PEER_intern (pi);
2364 t->local_tid = next_local_tid++;
2365 /* FIXME test if taken */
2366 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2367 t->tree = tree_new(t, t->id.oid);
2369 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2371 GNUNET_CONTAINER_multihashmap_put (
2375 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2381 tunnel_reset_timeout (t);
2382 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2384 GNUNET_CONTAINER_multihashmap_put (
2388 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2396 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2397 if (NULL == dest_peer_info)
2399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2400 "MESH: Creating PeerInfo for destination.\n");
2401 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2402 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2403 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2405 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2407 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2408 if (NULL == orig_peer_info)
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2411 "MESH: Creating PeerInfo for origin.\n");
2412 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2413 orig_peer_info->id = GNUNET_PEER_intern (pi);
2414 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2415 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2418 path = path_new (size);
2420 for (i = 0; i < size; i++)
2422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2423 "MESH: ... adding %s\n",
2424 GNUNET_i2s(&pi[i]));
2425 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2426 if (path->peers[i] == myid)
2429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2430 "MESH: Own position: %u\n", own_pos);
2433 /* cannot be self, must be 'not found' */
2434 /* create path: self not found in path through self */
2435 GNUNET_break_op (0);
2436 path_destroy (path);
2437 /* FIXME error. destroy tunnel? leave for timeout? */
2440 tunnel_add_path (t, path, own_pos);
2441 if (own_pos == size - 1)
2443 /* It is for us! Send ack. */
2444 struct GNUNET_MESH_TunnelNotification cmsg;
2445 struct MeshDataDescriptor *info;
2448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2449 "MESH: It's for us!\n");
2450 path_add_to_origin (orig_peer_info, path);
2451 t->peers = GNUNET_CONTAINER_multihashmap_create(4);
2452 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2454 &my_full_id.hashPubKey,
2455 peer_info_get(&my_full_id),
2456 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2457 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2458 info->origin = &t->id;
2459 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2460 GNUNET_assert (NULL != info->peer);
2461 j = peer_info_transmit_slot(info->peer);
2462 info->handler_n = j;
2463 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2464 info->peer->infos[j] = info;
2465 info->peer->core_transmit[j] =
2466 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2467 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2468 sizeof (struct GNUNET_MESH_PathACK),
2469 &send_core_path_ack, info);
2470 cmsg.header.size = htons(sizeof(cmsg));
2471 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2472 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2473 cmsg.tunnel_id = htonl(t->local_tid);
2474 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2478 struct MeshPeerPath *path2;
2480 /* It's for somebody else! Retransmit. */
2481 path2 = path_duplicate(path);
2482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2483 "MESH: Retransmitting.\n");
2484 path_add_to_peer(dest_peer_info, path);
2485 path = path_duplicate(path2);
2486 path_add_to_origin(orig_peer_info, path2);
2487 send_create_path(dest_peer_info, path, t);
2494 * Core handler for path destruction
2496 * @param cls closure
2497 * @param message message
2498 * @param peer peer identity this notification is about
2499 * @param atsi performance data
2500 * @param atsi_count number of records in 'atsi'
2502 * @return GNUNET_OK to keep the connection open,
2503 * GNUNET_SYSERR to close it (signal serious error)
2506 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2507 const struct GNUNET_MessageHeader *message,
2508 const struct GNUNET_ATS_Information *atsi,
2509 unsigned int atsi_count)
2511 struct GNUNET_MESH_ManipulatePath *msg;
2512 struct GNUNET_PeerIdentity *pi;
2513 struct MeshPeerPath *path;
2514 struct MeshTunnel *t;
2515 unsigned int own_pos;
2519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2520 "MESH: Received a PATH DESTROY msg from %s\n",
2522 size = ntohs (message->size);
2523 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2525 GNUNET_break_op (0);
2529 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2530 if (size % sizeof (struct GNUNET_PeerIdentity))
2532 GNUNET_break_op (0);
2535 size /= sizeof (struct GNUNET_PeerIdentity);
2538 GNUNET_break_op (0);
2541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2542 "MESH: path has %u hops.\n",
2545 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2546 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2548 "MESH: path is for tunnel %s [%X].\n",
2551 t = tunnel_get (pi, ntohl (msg->tid));
2554 /* TODO notify back: we don't know this tunnel */
2555 GNUNET_break_op (0);
2558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2559 path = path_new (size);
2561 for (i = 0; i < size; i++)
2563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2564 "MESH: ... adding %s\n",
2565 GNUNET_i2s(&pi[i]));
2566 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2567 if (path->peers[i] == myid)
2570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2571 "MESH: Own position: %u\n", own_pos);
2572 if (own_pos < path->length - 1)
2573 send_message (message, &pi[own_pos + 1]);
2574 tunnel_delete_peer (t, path->peers[path->length - 1]);
2580 * Core handler for notifications of broken paths
2582 * @param cls closure
2583 * @param message message
2584 * @param peer peer identity this notification is about
2585 * @param atsi performance data
2586 * @param atsi_count number of records in 'atsi'
2588 * @return GNUNET_OK to keep the connection open,
2589 * GNUNET_SYSERR to close it (signal serious error)
2592 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
2593 const struct GNUNET_MessageHeader *message,
2594 const struct GNUNET_ATS_Information *atsi,
2595 unsigned int atsi_count)
2597 struct GNUNET_MESH_PathBroken *msg;
2598 struct MeshTunnel *t;
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2601 "MESH: Received a PATH BROKEN msg from %s\n",
2603 msg = (struct GNUNET_MESH_PathBroken *) message;
2604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2605 "MESH: regarding %s\n",
2606 GNUNET_i2s(&msg->peer1));
2607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2608 "MESH: regarding %s\n",
2609 GNUNET_i2s(&msg->peer2));
2610 t = tunnel_get(&msg->oid, ntohl (msg->tid));
2613 GNUNET_break_op (0);
2616 tunnel_notify_connection_broken(t,
2617 GNUNET_PEER_search(&msg->peer1),
2618 GNUNET_PEER_search(&msg->peer2));
2625 * Core handler for tunnel destruction
2627 * @param cls closure
2628 * @param message message
2629 * @param peer peer identity this notification is about
2630 * @param atsi performance data
2631 * @param atsi_count number of records in 'atsi'
2633 * @return GNUNET_OK to keep the connection open,
2634 * GNUNET_SYSERR to close it (signal serious error)
2637 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2638 const struct GNUNET_MessageHeader *message,
2639 const struct GNUNET_ATS_Information *atsi,
2640 unsigned int atsi_count)
2642 struct GNUNET_MESH_TunnelDestroy *msg;
2643 struct MeshTunnel *t;
2645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2646 "MESH: Got a TUNNEL DESTROY packet from %s\n",
2648 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2650 "MESH: for tunnel %s [%u]\n",
2651 GNUNET_i2s (&msg->oid),
2653 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2656 /* TODO notify back: we don't know this tunnel */
2657 GNUNET_break_op (0);
2660 if (t->id.oid == myid)
2662 GNUNET_break_op (0);
2665 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2667 /* Tunnel was incoming, notify clients */
2668 send_clients_tunnel_destroy (t);
2670 tunnel_send_destroy (t);
2677 * Core handler for mesh network traffic going from the origin to a peer
2679 * @param cls closure
2680 * @param peer peer identity this notification is about
2681 * @param message message
2682 * @param atsi performance data
2683 * @param atsi_count number of records in 'atsi'
2684 * @return GNUNET_OK to keep the connection open,
2685 * GNUNET_SYSERR to close it (signal serious error)
2688 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2689 const struct GNUNET_MessageHeader *message,
2690 const struct GNUNET_ATS_Information *atsi,
2691 unsigned int atsi_count)
2693 struct GNUNET_MESH_Unicast *msg;
2694 struct MeshTunnel *t;
2698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2699 "MESH: got a unicast packet from %s\n",
2701 size = ntohs (message->size);
2703 sizeof (struct GNUNET_MESH_Unicast) +
2704 sizeof (struct GNUNET_MessageHeader))
2709 msg = (struct GNUNET_MESH_Unicast *) message;
2710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2711 "MESH: of type %u\n",
2712 ntohs (msg[1].header.type));
2713 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2716 /* TODO notify back: we don't know this tunnel */
2717 GNUNET_break_op (0);
2720 tunnel_reset_timeout (t);
2721 pid = GNUNET_PEER_search(&msg->destination);
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2725 "MESH: it's for us! sending to clients...\n");
2726 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2730 "MESH: not for us, retransmitting...\n");
2731 send_message (message, path_get_first_hop(t->tree, pid));
2737 * Core handler for mesh network traffic going from the origin to all peers
2739 * @param cls closure
2740 * @param message message
2741 * @param peer peer identity this notification is about
2742 * @param atsi performance data
2743 * @param atsi_count number of records in 'atsi'
2744 * @return GNUNET_OK to keep the connection open,
2745 * GNUNET_SYSERR to close it (signal serious error)
2747 * TODO: Check who we got this from, to validate route.
2750 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2751 const struct GNUNET_MessageHeader *message,
2752 const struct GNUNET_ATS_Information *atsi,
2753 unsigned int atsi_count)
2755 struct GNUNET_MESH_Multicast *msg;
2756 struct MeshTunnel *t;
2759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2760 "MESH: got a multicast packet from %s\n",
2762 size = ntohs (message->size);
2763 if (sizeof (struct GNUNET_MESH_Multicast) +
2764 sizeof (struct GNUNET_MessageHeader) > size)
2766 GNUNET_break_op (0);
2769 msg = (struct GNUNET_MESH_Multicast *) message;
2770 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2774 /* TODO notify that we dont know that tunnel */
2775 GNUNET_break_op (0);
2778 tunnel_reset_timeout (t);
2780 /* Transmit to locally interested clients */
2781 if (NULL != t->peers &&
2782 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2784 send_subscribed_clients (message, &msg[1].header);
2786 tunnel_send_multicast(t, message);
2792 * Core handler for mesh network traffic toward the owner of a tunnel
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_data_to_orig (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_ToOrigin *msg;
2810 struct GNUNET_PeerIdentity id;
2811 struct MeshPeerInfo *peer_info;
2812 struct MeshTunnel *t;
2815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2816 "MESH: got a ToOrigin packet from %s\n",
2818 size = ntohs (message->size);
2819 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2820 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2822 GNUNET_break_op (0);
2825 msg = (struct GNUNET_MESH_ToOrigin *) message;
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2827 "MESH: of type %u\n",
2828 ntohs (msg[1].header.type));
2829 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2833 /* TODO notify that we dont know this tunnel (whom)? */
2834 GNUNET_break_op (0);
2838 if (t->id.oid == myid)
2841 struct GNUNET_MESH_ToOrigin *copy;
2843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2844 "MESH: it's for us! sending to clients...\n");
2845 if (NULL == t->client)
2847 /* got data packet for ownerless tunnel */
2848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2849 "MESH: no clients!\n");
2850 GNUNET_break_op (0);
2853 /* TODO signature verification */
2854 memcpy (cbuf, message, size);
2855 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2856 copy->tid = htonl (t->local_tid);
2857 GNUNET_SERVER_notification_context_unicast (nc,
2863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2864 "MESH: not for us, retransmitting...\n");
2866 peer_info = peer_info_get (&msg->oid);
2867 if (NULL == peer_info)
2869 /* unknown origin of tunnel */
2873 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
2874 send_message (message, &id);
2881 * Core handler for path ACKs
2883 * @param cls closure
2884 * @param message message
2885 * @param peer peer identity this notification is about
2886 * @param atsi performance data
2887 * @param atsi_count number of records in 'atsi'
2889 * @return GNUNET_OK to keep the connection open,
2890 * GNUNET_SYSERR to close it (signal serious error)
2893 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2894 const struct GNUNET_MessageHeader *message,
2895 const struct GNUNET_ATS_Information *atsi,
2896 unsigned int atsi_count)
2898 struct GNUNET_MESH_PathACK *msg;
2899 struct GNUNET_PeerIdentity id;
2900 struct MeshTunnelTreeNode *n;
2901 struct MeshPeerInfo *peer_info;
2902 struct MeshTunnel *t;
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2905 "MESH: Received a path ACK msg [%s]\n",
2906 GNUNET_i2s(&my_full_id));
2907 msg = (struct GNUNET_MESH_PathACK *) message;
2908 t = tunnel_get (&msg->oid, msg->tid);
2911 /* TODO notify that we don't know the tunnel */
2915 /* Message for us? */
2916 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
2918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
2919 if (NULL == t->client)
2921 GNUNET_break_op (0);
2924 if (NULL != t->dht_get_type)
2926 GNUNET_DHT_get_stop (t->dht_get_type);
2927 t->dht_get_type = NULL;
2929 peer_info = peer_info_get (&msg->peer_id);
2930 n = tree_find_peer(t->tree->root, peer_info->id);
2933 GNUNET_break_op (0);
2936 n->status = MESH_PEER_READY;
2937 send_client_peer_connected(t, peer_info->id);
2941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2942 "MESH: not for us, retransmitting...\n");
2943 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
2944 peer_info = peer_info_get (&msg->oid);
2945 if (NULL == peer_info)
2947 /* If we know the tunnel, we should DEFINITELY know the peer */
2951 send_message (message, &id);
2957 * Functions to handle messages from core
2959 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
2960 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
2961 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
2962 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
2963 sizeof (struct GNUNET_MESH_PathBroken)},
2964 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
2965 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
2966 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
2967 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
2968 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2969 sizeof (struct GNUNET_MESH_PathACK)},
2975 /******************************************************************************/
2976 /**************** MESH LOCAL HANDLER HELPERS ***********************/
2977 /******************************************************************************/
2980 * deregister_app: iterator for removing each application registered by a client
2982 * @param cls closure
2983 * @param key the hash of the application id (used to access the hashmap)
2984 * @param value the value stored at the key (client)
2986 * @return GNUNET_OK on success
2989 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
2991 GNUNET_break (GNUNET_YES ==
2992 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
2998 * notify_client_connection_failure: notify a client that the connection to the
2999 * requested remote peer is not possible (for instance, no route found)
3000 * Function called when the socket is ready to queue more data. "buf" will be
3001 * NULL and "size" zero if the socket was closed for writing in the meantime.
3003 * @param cls closure
3004 * @param size number of bytes available in buf
3005 * @param buf where the callee should write the message
3006 * @return number of bytes written to buf
3009 notify_client_connection_failure (void *cls, size_t size, void *buf)
3012 struct MeshPeerInfo *peer_info;
3013 struct GNUNET_MESH_PeerControl *msg;
3014 struct GNUNET_PeerIdentity id;
3016 if (0 == size && NULL == buf)
3018 // TODO retry? cancel?
3022 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3023 peer_info = (struct MeshPeerInfo *) cls;
3024 msg = (struct GNUNET_MESH_PeerControl *) buf;
3025 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3026 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3027 // msg->tunnel_id = htonl(peer_info->t->tid);
3028 GNUNET_PEER_resolve (peer_info->id, &id);
3029 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3037 * Send keepalive packets for a peer
3039 * @param cls Closure (tunnel for which to send the keepalive).
3040 * @param tc Notification context.
3042 * TODO: implement explicit multicast keepalive?
3045 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3047 struct MeshTunnel *t = cls;
3048 struct GNUNET_MessageHeader *payload;
3049 struct GNUNET_MESH_Multicast *msg;
3050 size_t size = sizeof(struct GNUNET_MESH_Multicast) +
3051 sizeof(struct GNUNET_MessageHeader);
3054 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3058 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3061 "MESH: sending keepalive for tunnel %d\n",
3064 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3065 msg->header.size = htons (size);
3066 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3067 msg->oid = my_full_id;
3068 msg->tid = htonl(t->id.tid);
3069 payload = (struct GNUNET_MessageHeader *) &msg[1];
3070 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
3071 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3072 tunnel_send_multicast (t, &msg->header);
3074 t->path_refresh_task =
3075 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
3081 * Function to process paths received for a new peer addition. The recorded
3082 * paths form the initial tunnel, which can be optimized later.
3083 * Called on each result obtained for the DHT search.
3085 * @param cls closure
3086 * @param exp when will this value expire
3087 * @param key key of the result
3088 * @param type type of the result
3089 * @param size number of bytes in data
3090 * @param data pointer to the result data
3092 * TODO: re-issue the request after certain time? cancel after X results?
3095 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3096 const GNUNET_HashCode * key,
3097 const struct GNUNET_PeerIdentity *get_path,
3098 unsigned int get_path_length,
3099 const struct GNUNET_PeerIdentity *put_path,
3100 unsigned int put_path_length,
3101 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
3103 struct MeshPathInfo *path_info = cls;
3104 struct MeshPeerPath *p;
3105 struct GNUNET_PeerIdentity pi;
3108 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3109 "MESH: Got results from DHT!\n");
3110 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3111 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3113 GNUNET_h2s_full(&pi.hashPubKey));
3114 // GNUNET_DHT_get_stop(path_info->peer->dhtget);
3115 // path_info->peer->dhtget = NULL;
3117 p = path_build_from_dht (get_path, get_path_length,
3118 put_path, put_path_length);
3119 path_add_to_peer (path_info->peer, p);
3120 for (i = 0; i < path_info->peer->ntunnels; i++)
3122 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3123 peer_info_connect(path_info->peer, path_info->t);
3125 // GNUNET_free (path_info);
3132 * Function to process paths received for a new peer addition. The recorded
3133 * paths form the initial tunnel, which can be optimized later.
3134 * Called on each result obtained for the DHT search.
3136 * @param cls closure
3137 * @param exp when will this value expire
3138 * @param key key of the result
3139 * @param type type of the result
3140 * @param size number of bytes in data
3141 * @param data pointer to the result data
3144 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3145 const GNUNET_HashCode * key,
3146 const struct GNUNET_PeerIdentity *get_path,
3147 unsigned int get_path_length,
3148 const struct GNUNET_PeerIdentity *put_path,
3149 unsigned int put_path_length,
3150 enum GNUNET_BLOCK_Type type, size_t size,
3153 const struct GNUNET_PeerIdentity *pi = data;
3154 struct MeshTunnel *t = cls;
3155 struct MeshPeerInfo *peer_info;
3156 struct MeshPeerPath *p;
3158 if (size != sizeof (struct GNUNET_PeerIdentity))
3160 GNUNET_break_op (0);
3163 GNUNET_assert (NULL != t->client);
3164 peer_info = peer_info_get (pi);
3165 (void) GNUNET_CONTAINER_multihashmap_put (
3169 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3171 p = path_build_from_dht (get_path, get_path_length,
3172 put_path, put_path_length);
3173 path_add_to_peer (peer_info, p);
3174 tunnel_add_peer(t, peer_info);
3175 peer_info_connect (peer_info, t);
3179 /******************************************************************************/
3180 /********************* MESH LOCAL HANDLES **************************/
3181 /******************************************************************************/
3185 * Handler for client disconnection
3187 * @param cls closure
3188 * @param client identification of the client; NULL
3189 * for the last call when the server is destroyed
3192 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3194 struct MeshClient *c;
3195 struct MeshClient *next;
3197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3203 if (c->handle != client && NULL != client)
3205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
3210 if (NULL != c->tunnels)
3212 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3213 &tunnel_destroy_iterator,
3215 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3218 /* deregister clients applications */
3219 if (NULL != c->apps)
3221 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3222 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3224 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3225 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3227 GNUNET_SCHEDULER_cancel (announce_applications_task);
3228 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3230 if (NULL != c->types)
3231 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3233 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3244 * Handler for new clients
3246 * @param cls closure
3247 * @param client identification of the client
3248 * @param message the actual message, which includes messages the client wants
3251 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3252 const struct GNUNET_MessageHeader *message)
3254 struct GNUNET_MESH_ClientConnect *cc_msg;
3255 struct MeshClient *c;
3256 GNUNET_MESH_ApplicationType *a;
3263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3264 /* Check data sanity */
3265 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3266 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3267 ntypes = ntohs (cc_msg->types);
3268 napps = ntohs (cc_msg->applications);
3270 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3273 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3277 /* Create new client structure */
3278 c = GNUNET_malloc (sizeof (struct MeshClient));
3280 c->id = next_client_id++;
3283 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3286 GNUNET_MESH_ApplicationType at;
3289 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3290 for (i = 0; i < napps; i++)
3293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3294 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3295 /* store in clients hashmap */
3296 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3297 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3298 /* store in global hashmap, for announcements */
3299 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3300 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3302 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3303 announce_applications_task =
3304 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3312 t = (uint16_t *) & a[napps];
3313 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3314 for (i = 0; i < ntypes; i++)
3317 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3319 /* store in clients hashmap */
3320 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3321 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3322 /* store in global hashmap */
3323 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3324 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3328 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3330 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3331 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3332 GNUNET_SERVER_notification_context_add (nc, client);
3334 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3342 * Handler for requests of new tunnels
3344 * @param cls closure
3345 * @param client identification of the client
3346 * @param message the actual message
3349 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3350 const struct GNUNET_MessageHeader *message)
3352 struct GNUNET_MESH_TunnelMessage *t_msg;
3353 struct MeshTunnel *t;
3354 struct MeshClient *c;
3355 GNUNET_HashCode hash;
3357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3359 /* Sanity check for client registration */
3360 if (NULL == (c = client_get (client)))
3363 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3370 /* Message sanity check */
3371 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3374 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3378 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3379 /* Sanity check for tunnel numbering */
3380 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3383 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3386 /* Sanity check for duplicate tunnel IDs */
3387 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3390 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3394 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL at %p\n", t);
3396 while (NULL != tunnel_get_by_pi (myid, next_tid))
3397 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3398 t->id.tid = next_tid++;
3400 t->local_tid = ntohl (t_msg->tunnel_id);
3402 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3404 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3406 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3407 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3410 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3414 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3416 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3417 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3420 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3423 t->tree = tree_new (t, myid);
3424 t->tree->refresh = REFRESH_PATH_TIME;
3425 t->tree->root->status = MESH_PEER_READY;
3426 t->tree->me = t->tree->root;
3428 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3434 * Handler for requests of deleting tunnels
3436 * @param cls closure
3437 * @param client identification of the client
3438 * @param message the actual message
3441 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3442 const struct GNUNET_MessageHeader *message)
3444 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3445 struct MeshClient *c;
3446 struct MeshTunnel *t;
3447 MESH_TunnelNumber tid;
3448 GNUNET_HashCode hash;
3450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3451 "MESH: Got a DESTROY TUNNEL from client!\n");
3453 /* Sanity check for client registration */
3454 if (NULL == (c = client_get (client)))
3457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3460 /* Message sanity check */
3461 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3464 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3470 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3472 /* Retrieve tunnel */
3473 tid = ntohl (tunnel_msg->tunnel_id);
3475 /* Remove from local id hashmap */
3476 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3477 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3478 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
3481 tunnel_send_destroy (t);
3483 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3489 * Handler for connection requests to new peers
3491 * @param cls closure
3492 * @param client identification of the client
3493 * @param message the actual message (PeerControl)
3496 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3497 const struct GNUNET_MessageHeader *message)
3499 struct GNUNET_MESH_PeerControl *peer_msg;
3500 struct MeshPeerInfo *peer_info;
3501 struct MeshClient *c;
3502 struct MeshTunnel *t;
3503 MESH_TunnelNumber tid;
3505 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3506 /* Sanity check for client registration */
3507 if (NULL == (c = client_get (client)))
3510 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3514 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3515 /* Sanity check for message size */
3516 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3519 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3523 /* Tunnel exists? */
3524 tid = ntohl (peer_msg->tunnel_id);
3525 t = tunnel_get_by_local_id (c, tid);
3529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3533 /* Does client own tunnel? */
3534 if (t->client->handle != client)
3537 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3540 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3541 GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
3542 peer_info = peer_info_get (&peer_msg->peer);
3544 tunnel_add_peer(t, peer_info);
3545 peer_info_connect(peer_info, t);
3547 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3553 * Handler for disconnection requests of peers in a tunnel
3555 * @param cls closure
3556 * @param client identification of the client
3557 * @param message the actual message (PeerControl)
3560 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3561 const struct GNUNET_MessageHeader *message)
3563 struct GNUNET_MESH_PeerControl *peer_msg;
3564 struct MeshPeerInfo *peer_info;
3565 struct MeshClient *c;
3566 struct MeshTunnel *t;
3567 MESH_TunnelNumber tid;
3569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got a PEER DEL request\n");
3570 /* Sanity check for client registration */
3571 if (NULL == (c = client_get (client)))
3574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3577 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3578 /* Sanity check for message size */
3579 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3586 /* Tunnel exists? */
3587 tid = ntohl (peer_msg->tunnel_id);
3588 t = tunnel_get_by_local_id (c, tid);
3592 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: on tunnel %X\n", t->id.tid);
3597 /* Does client own tunnel? */
3598 if (t->client->handle != client)
3601 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3606 "MESH: for peer %s\n",
3607 GNUNET_i2s(&peer_msg->peer));
3608 /* Is the peer in the tunnel? */
3609 peer_info = GNUNET_CONTAINER_multihashmap_get(t->peers,
3610 &peer_msg->peer.hashPubKey);
3611 if (NULL == peer_info)
3614 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3618 /* Ok, delete peer from tunnel */
3619 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3620 &peer_msg->peer.hashPubKey);
3622 send_destroy_path (t, peer_info->id);
3623 tunnel_delete_peer(t, peer_info->id);
3624 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3630 * Handler for connection requests to new peers by type
3632 * @param cls closure
3633 * @param client identification of the client
3634 * @param message the actual message (ConnectPeerByType)
3637 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3638 const struct GNUNET_MessageHeader *message)
3640 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3641 struct MeshClient *c;
3642 struct MeshTunnel *t;
3643 GNUNET_HashCode hash;
3644 MESH_TunnelNumber tid;
3646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3647 /* Sanity check for client registration */
3648 if (NULL == (c = client_get (client)))
3651 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3655 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3656 /* Sanity check for message size */
3657 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3658 ntohs (connect_msg->header.size))
3661 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3665 /* Tunnel exists? */
3666 tid = ntohl (connect_msg->tunnel_id);
3667 t = tunnel_get_by_local_id (c, tid);
3671 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3675 /* Does client own tunnel? */
3676 if (t->client->handle != client)
3679 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3683 /* Do WE have the service? */
3684 t->type = ntohl (connect_msg->type);
3685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3686 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3687 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3690 /* Yes! Fast forward, add ourselves to the tunnel and send the
3691 * good news to the client
3693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3694 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3695 peer_info_get (&my_full_id),
3696 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3699 send_client_peer_connected(t, myid);
3700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3701 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3704 /* Ok, lets find a peer offering the service */
3705 if (NULL != t->dht_get_type)
3707 GNUNET_DHT_get_stop (t->dht_get_type);
3709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3710 GNUNET_h2s_full (&hash));
3712 GNUNET_DHT_get_start (dht_handle,
3713 GNUNET_TIME_UNIT_FOREVER_REL,
3714 GNUNET_BLOCK_TYPE_TEST,
3717 GNUNET_DHT_RO_RECORD_ROUTE |
3718 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3720 &dht_get_type_handler, t);
3722 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3728 * Handler for client traffic directed to one peer
3730 * @param cls closure
3731 * @param client identification of the client
3732 * @param message the actual message
3735 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3736 const struct GNUNET_MessageHeader *message)
3738 struct MeshClient *c;
3739 struct MeshTunnel *t;
3740 struct MeshPeerInfo *pi;
3741 struct GNUNET_MESH_Unicast *data_msg;
3742 MESH_TunnelNumber tid;
3745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3746 "MESH: Got a unicast request from a client!\n");
3748 /* Sanity check for client registration */
3749 if (NULL == (c = client_get (client)))
3752 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3755 data_msg = (struct GNUNET_MESH_Unicast *) message;
3756 /* Sanity check for message size */
3757 size = ntohs (message->size);
3758 if (sizeof (struct GNUNET_MESH_Unicast) +
3759 sizeof (struct GNUNET_MessageHeader) > size)
3762 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3766 /* Tunnel exists? */
3767 tid = ntohl (data_msg->tid);
3768 t = tunnel_get_by_local_id (c, tid);
3772 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3776 /* Is it a local tunnel? Then, does client own the tunnel? */
3777 if (NULL != t->client &&
3778 NULL != t->client->handle &&
3779 t->client->handle != client)
3782 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3786 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3787 &data_msg->destination.hashPubKey);
3788 /* Is the selected peer in the tunnel? */
3792 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3796 /* Ok, everything is correct, send the message
3797 * (pretend we got it from a mesh peer)
3800 char buf[ntohs (message->size)];
3801 struct GNUNET_MESH_Unicast *copy;
3803 /* Work around const limitation */
3804 copy = (struct GNUNET_MESH_Unicast *) buf;
3805 memcpy (buf, data_msg, size);
3806 copy->oid = my_full_id;
3807 copy->tid = htonl (t->id.tid);
3808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3809 "MESH: calling generic handler...\n");
3810 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
3812 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3818 * Handler for client traffic directed to the origin
3820 * @param cls closure
3821 * @param client identification of the client
3822 * @param message the actual message
3825 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3826 const struct GNUNET_MessageHeader *message)
3828 struct GNUNET_MESH_ToOrigin *data_msg;
3829 struct GNUNET_PeerIdentity id;
3830 struct MeshClient *c;
3831 struct MeshTunnel *t;
3832 MESH_TunnelNumber tid;
3835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3836 "MESH: Got a ToOrigin request from a client!\n");
3838 /* Sanity check for client registration */
3839 if (NULL == (c = client_get (client)))
3842 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3845 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3846 /* Sanity check for message size */
3847 size = ntohs (message->size);
3848 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3849 sizeof (struct GNUNET_MessageHeader) > size)
3852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3856 /* Tunnel exists? */
3857 tid = ntohl (data_msg->tid);
3858 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3861 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3864 t = tunnel_get_by_local_id (c, tid);
3868 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3872 /* It shouldn't be a local tunnel. */
3873 if (NULL != t->client)
3876 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3879 GNUNET_PEER_resolve(t->id.oid, &id);
3881 /* Ok, everything is correct, send the message
3882 * (pretend we got it from a mesh peer)
3885 char buf[ntohs (message->size)];
3886 struct GNUNET_MESH_ToOrigin *copy;
3888 /* Work around const limitation */
3889 copy = (struct GNUNET_MESH_ToOrigin *) buf;
3890 memcpy (buf, data_msg, size);
3892 copy->tid = htonl (t->id.tid);
3893 copy->sender = my_full_id;
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3895 "MESH: calling generic handler...\n");
3896 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
3898 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3904 * Handler for client traffic directed to all peers in a tunnel
3906 * @param cls closure
3907 * @param client identification of the client
3908 * @param message the actual message
3911 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
3912 const struct GNUNET_MessageHeader *message)
3914 struct MeshClient *c;
3915 struct MeshTunnel *t;
3916 struct GNUNET_MESH_Multicast *data_msg;
3917 MESH_TunnelNumber tid;
3919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3920 "MESH: Got a multicast request from a client!\n");
3922 /* Sanity check for client registration */
3923 if (NULL == (c = client_get (client)))
3926 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3929 data_msg = (struct GNUNET_MESH_Multicast *) message;
3930 /* Sanity check for message size */
3931 if (sizeof (struct GNUNET_MESH_Multicast) +
3932 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
3935 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3939 /* Tunnel exists? */
3940 tid = ntohl (data_msg->tid);
3941 t = tunnel_get_by_local_id (c, tid);
3945 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3949 /* Does client own tunnel? */
3950 if (t->client->handle != client)
3953 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3958 char buf[ntohs(message->size)];
3959 struct GNUNET_MESH_Multicast *copy;
3961 copy = (struct GNUNET_MESH_Multicast *)buf;
3962 memcpy(buf, message, ntohs(message->size));
3963 copy->oid = my_full_id;
3964 copy->tid = htonl(t->id.tid);
3965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3966 "MESH: calling generic handler...\n");
3967 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL, 0);
3970 /* receive done gets called when last copy is sent to a neighbor */
3975 * Functions to handle messages from clients
3977 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
3978 {&handle_local_new_client, NULL,
3979 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
3980 {&handle_local_tunnel_create, NULL,
3981 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
3982 sizeof (struct GNUNET_MESH_TunnelMessage)},
3983 {&handle_local_tunnel_destroy, NULL,
3984 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
3985 sizeof (struct GNUNET_MESH_TunnelMessage)},
3986 {&handle_local_connect_add, NULL,
3987 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
3988 sizeof (struct GNUNET_MESH_PeerControl)},
3989 {&handle_local_connect_del, NULL,
3990 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
3991 sizeof (struct GNUNET_MESH_PeerControl)},
3992 {&handle_local_connect_by_type, NULL,
3993 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
3994 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
3995 {&handle_local_unicast, NULL,
3996 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3997 {&handle_local_to_origin, NULL,
3998 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3999 {&handle_local_multicast, NULL,
4000 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4006 * To be called on core init/fail.
4008 * @param cls service closure
4009 * @param server handle to the server for this service
4010 * @param identity the public identity of this peer
4013 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4014 const struct GNUNET_PeerIdentity *identity)
4016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
4017 core_handle = server;
4018 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
4020 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
4021 GNUNET_SCHEDULER_shutdown();
4027 * Method called whenever a given peer connects.
4029 * @param cls closure
4030 * @param peer peer identity this notification is about
4031 * @param atsi performance data for the connection
4032 * @param atsi_count number of records in 'atsi'
4035 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4036 const struct GNUNET_ATS_Information *atsi,
4037 unsigned int atsi_count)
4039 struct MeshPeerInfo *peer_info;
4040 struct MeshPeerPath *path;
4042 #if MESH_DEBUG_CONNECTION
4043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4045 GNUNET_h2s(&my_full_id.hashPubKey));
4047 peer_info = peer_info_get (peer);
4048 if (myid == peer_info->id)
4050 #if MESH_DEBUG_CONNECTION
4051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4055 #if MESH_DEBUG_CONNECTION
4058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4059 GNUNET_h2s(&peer->hashPubKey));
4062 path = path_new (2);
4063 path->peers[0] = myid;
4064 path->peers[1] = peer_info->id;
4065 GNUNET_PEER_change_rc(myid, 1);
4066 GNUNET_PEER_change_rc(peer_info->id, 1);
4067 path_add_to_peer (peer_info, path);
4072 * Method called whenever a peer disconnects.
4074 * @param cls closure
4075 * @param peer peer identity this notification is about
4078 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4080 struct MeshPeerInfo *pi;
4083 #if MESH_DEBUG_CONNECTION
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
4086 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4092 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4094 /* TODO: notify that the transmission failed */
4095 peer_info_cancel_transmission(pi, i);
4097 path_remove_from_peer (pi, pi->id, myid);
4098 #if MESH_DEBUG_CONNECTION
4101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4108 /******************************************************************************/
4109 /************************ MAIN FUNCTIONS ****************************/
4110 /******************************************************************************/
4113 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4115 * @param cls closure
4116 * @param key current key code
4117 * @param value value in the hash map
4118 * @return GNUNET_YES if we should continue to iterate,
4122 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4124 struct MeshTunnel *t = value;
4130 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4132 * @param cls closure
4133 * @param key current key code
4134 * @param value value in the hash map
4135 * @return GNUNET_YES if we should continue to iterate,
4139 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4141 struct MeshPeerInfo *p = value;
4142 peer_info_destroy (p);
4147 * Task run during shutdown.
4153 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4156 /* TODO: destroy tunnels? */
4157 if (core_handle != NULL)
4159 GNUNET_CORE_disconnect (core_handle);
4162 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
4163 GNUNET_CONTAINER_multihashmap_iterate(peers, &shutdown_peer, NULL);
4164 if (dht_handle != NULL)
4166 GNUNET_DHT_disconnect (dht_handle);
4171 GNUNET_SERVER_notification_context_destroy (nc);
4174 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4176 GNUNET_SCHEDULER_cancel (announce_id_task);
4177 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4183 * Process mesh requests.
4185 * @param cls closure
4186 * @param server the initialized server
4187 * @param c configuration to use
4190 run (void *cls, struct GNUNET_SERVER_Handle *server,
4191 const struct GNUNET_CONFIGURATION_Handle *c)
4193 struct MeshPeerInfo *peer;
4194 struct MeshPeerPath *p;
4197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4198 server_handle = server;
4199 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4200 CORE_QUEUE_SIZE, /* queue size */
4201 NULL, /* Closure passed to MESH functions */
4202 &core_init, /* Call core_init once connected */
4203 &core_connect, /* Handle connects */
4204 &core_disconnect, /* remove peers on disconnects */
4205 NULL, /* Don't notify about all incoming messages */
4206 GNUNET_NO, /* For header only in notification */
4207 NULL, /* Don't notify about all outbound messages */
4208 GNUNET_NO, /* For header-only out notification */
4209 core_handlers); /* Register these handlers */
4210 if (core_handle == NULL)
4213 GNUNET_SCHEDULER_shutdown ();
4218 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4221 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4223 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4224 GNUNET_SCHEDULER_shutdown ();
4227 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4228 GNUNET_free (keyfile);
4229 if (my_private_key == NULL)
4231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4232 _("Mesh service could not access hostkey. Exiting.\n"));
4233 GNUNET_SCHEDULER_shutdown ();
4236 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4237 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4238 &my_full_id.hashPubKey);
4239 myid = GNUNET_PEER_intern (&my_full_id);
4241 dht_handle = GNUNET_DHT_connect (c, 64);
4242 if (dht_handle == NULL)
4248 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4250 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4251 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4252 peers = GNUNET_CONTAINER_multihashmap_create (32);
4253 applications = GNUNET_CONTAINER_multihashmap_create (32);
4254 types = GNUNET_CONTAINER_multihashmap_create (32);
4256 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4257 nc = GNUNET_SERVER_notification_context_create (server_handle,
4259 GNUNET_SERVER_disconnect_notify (server_handle,
4260 &handle_local_client_disconnect,
4265 clients_tail = NULL;
4270 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4271 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4273 /* Create a peer_info for the local peer */
4274 peer = peer_info_get(&my_full_id);
4277 path_add_to_peer(peer, p);
4279 /* Scheduled the task to clean up when shutdown is called */
4280 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4287 * The main function for the mesh service.
4289 * @param argc number of arguments from the command line
4290 * @param argv command line arguments
4291 * @return 0 ok, 1 on error
4294 main (int argc, char *const *argv)
4298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4301 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");