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 #define MESH_DEBUG_DHT GNUNET_NO
74 /******************************************************************************/
75 /************************ DATA STRUCTURES ****************************/
76 /******************************************************************************/
78 /** FWD declaration */
82 * Struct containing all info possibly needed to build a package when called
85 struct MeshDataDescriptor
87 /** ID of the tunnel this packet travels in */
88 struct MESH_TunnelID *origin;
93 /** Client that asked for the transmission, if any */
94 struct GNUNET_SERVER_Client *client;
96 /** Who was this message being sent to */
97 struct MeshPeerInfo *peer;
99 /** Ultimate destination of the packet */
100 GNUNET_PEER_Id destination;
102 /** Number of identical messages sent to different hops (multicast) */
103 unsigned int *copies;
105 /** Which handler was used to request the transmission */
106 unsigned int handler_n;
108 /** Size of the data */
111 /** Used to allow a client send more traffic to the service after a
112 * previous packet was tried to be sent to a neighbor and couldn't */
113 GNUNET_SCHEDULER_TaskIdentifier *timeout_task;
118 * Struct containing all information regarding a given peer
128 * Last time we heard from this peer
130 struct GNUNET_TIME_Absolute last_contact;
133 * Number of attempts to reconnect so far
135 int n_reconnect_attempts;
138 * Paths to reach the peer, ordered by ascending hop count
140 struct MeshPeerPath *path_head;
143 * Paths to reach the peer, ordered by ascending hop count
145 struct MeshPeerPath *path_tail;
148 * Handle to stop the DHT search for a path to this peer
150 struct GNUNET_DHT_GetHandle *dhtget;
153 * Closure given to the DHT GET
155 struct MeshPathInfo *dhtgetcls;
158 * Handles to stop queued transmissions for this peer
160 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
163 * Pointer to info stuctures used as cls for queued transmissions
165 void *infos[CORE_QUEUE_SIZE];
168 * Type of message being in each transmission
170 uint16_t types[CORE_QUEUE_SIZE];
173 * Array of tunnels this peer participates in
174 * (most probably a small amount, therefore not a hashmap)
175 * When the path to the peer changes, notify these tunnels to let them
176 * re-adjust their path trees.
178 struct MeshTunnel **tunnels;
181 * Number of tunnels this peers participates in
183 unsigned int ntunnels;
188 * Data scheduled to transmit (to local client or remote peer)
195 struct MeshQueue *next;
196 struct MeshQueue *prev;
199 * Target of the data (NULL if target is client)
201 struct MeshPeerInfo *peer;
204 * Client to send the data to (NULL if target is peer)
206 struct MeshClient *client;
209 * Size of the message to transmit
214 * How old is the data?
216 struct GNUNET_TIME_Absolute timestamp;
221 struct GNUNET_MessageHeader *data;
225 * Globally unique tunnel identification (owner + number)
226 * DO NOT USE OVER THE NETWORK
231 * Node that owns the tunnel
236 * Tunnel number to differentiate all the tunnels owned by the node oid
237 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
239 MESH_TunnelNumber tid;
243 struct MeshClient; /* FWD declaration */
246 * Struct containing all information regarding a tunnel
247 * For an intermediate node the improtant info used will be:
248 * - id Tunnel unique identification
249 * - paths[0] To know where to send it next
250 * - metainfo: ready, speeds, accounting
257 struct MESH_TunnelID id;
260 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
262 MESH_TunnelNumber local_tid;
265 * Last time the tunnel was used
267 struct GNUNET_TIME_Absolute timestamp;
270 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
271 * containing peers added by id or by type, not intermediate peers.
273 struct GNUNET_CONTAINER_MultiHashMap *peers;
276 * Number of peers that are connected and potentially ready to receive data
278 unsigned int peers_ready;
281 * Number of peers that have been added to the tunnel
283 unsigned int peers_total;
286 * Client owner of the tunnel, if any
288 struct MeshClient *client;
291 * Messages ready to transmit
293 struct MeshQueue *queue_head;
294 struct MeshQueue *queue_tail;
299 struct MeshTunnelTree *tree;
302 * Application type we are looking for in this tunnel
304 GNUNET_MESH_ApplicationType type;
307 * Used to search peers offering a service
309 struct GNUNET_DHT_GetHandle *dht_get_type;
312 * Task to keep the used paths alive
314 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
317 * Task to destroy the tunnel after timeout
319 * FIXME: merge the two? a tunnel will have either
320 * a path refresh OR a timeout, never both!
322 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
327 * Info needed to work with tunnel paths and peers
334 struct MeshTunnel *t;
337 * Neighbouring peer to whom we send the packet to
339 struct MeshPeerInfo *peer;
344 struct MeshPeerPath *path;
347 * Position in peer's transmit queue
354 * Struct containing information about a client of the service
361 struct MeshClient *next;
362 struct MeshClient *prev;
365 * Tunnels that belong to this client, indexed by local id
367 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
370 * Handle to communicate with the client
372 struct GNUNET_SERVER_Client *handle;
375 * Applications that this client has claimed to provide
377 struct GNUNET_CONTAINER_MultiHashMap *apps;
380 * Messages that this client has declared interest in
382 struct GNUNET_CONTAINER_MultiHashMap *types;
386 * ID of the client, for debug messages
395 /******************************************************************************/
396 /************************ DEBUG FUNCTIONS ****************************/
397 /******************************************************************************/
401 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
402 * @param cls string to print
403 * @param tc task context
406 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
410 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
418 /******************************************************************************/
419 /*********************** GLOBAL VARIABLES ****************************/
420 /******************************************************************************/
425 static struct MeshClient *clients;
426 static struct MeshClient *clients_tail;
429 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
431 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
434 * Tunnels incoming, indexed by MESH_TunnelNumber
435 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
437 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
440 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
442 static struct GNUNET_CONTAINER_MultiHashMap *peers;
445 * Handle to communicate with core
447 static struct GNUNET_CORE_Handle *core_handle;
450 * Handle to communicate with transport
452 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
457 static struct GNUNET_DHT_Handle *dht_handle;
462 static struct GNUNET_SERVER_Handle *server_handle;
465 * Notification context, to send messages to local clients
467 static struct GNUNET_SERVER_NotificationContext *nc;
470 * Local peer own ID (memory efficient handle)
472 static GNUNET_PEER_Id myid;
475 * Local peer own ID (full value)
477 static struct GNUNET_PeerIdentity my_full_id;
482 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
487 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
490 * Tunnel ID for the next created tunnel (global tunnel number)
492 static MESH_TunnelNumber next_tid;
495 * Tunnel ID for the next incoming tunnel (local tunnel number)
497 static MESH_TunnelNumber next_local_tid;
500 * All application types provided by this peer
502 static struct GNUNET_CONTAINER_MultiHashMap *applications;
505 * All message types clients of this peer are interested in
507 static struct GNUNET_CONTAINER_MultiHashMap *types;
510 * Task to periodically announce provided applications
512 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
515 * Task to periodically announce itself in the network
517 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
520 unsigned int next_client_id;
524 /******************************************************************************/
525 /************************ ITERATORS ****************************/
526 /******************************************************************************/
528 /* FIXME move iterators here */
531 /******************************************************************************/
532 /************************ PERIODIC FUNCTIONS ****************************/
533 /******************************************************************************/
536 * Announce iterator over for each application provided by the peer
539 * @param key current key code
540 * @param value value in the hash map
541 * @return GNUNET_YES if we should continue to
546 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
548 /* FIXME are hashes in multihash map equal on all aquitectures? */
549 GNUNET_DHT_put (dht_handle,
552 GNUNET_DHT_RO_RECORD_ROUTE |
553 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
554 GNUNET_BLOCK_TYPE_TEST,
555 sizeof (struct GNUNET_PeerIdentity),
556 (const char *) &my_full_id,
558 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
559 &mesh_debug, "DHT_put for app completed");
561 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
563 APP_ANNOUNCE_TIME, NULL, NULL);
570 * Periodically announce what applications are provided by local clients
573 * @param tc task context
576 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
578 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
580 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
586 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
588 announce_applications_task =
589 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
599 * Periodically announce self id in the DHT
602 * @param tc task context
605 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
607 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
609 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
613 * - Set data expiration in function of X
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
618 GNUNET_i2s (&my_full_id));
620 GNUNET_DHT_put (dht_handle, /* DHT handle */
621 &my_full_id.hashPubKey, /* Key to use */
622 10U, /* Replication level */
623 GNUNET_DHT_RO_RECORD_ROUTE |
624 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
625 GNUNET_BLOCK_TYPE_TEST, /* Block type */
626 sizeof(my_full_id), /* Size of the data */
627 (char *)&my_full_id, /* Data itself */
628 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
629 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
631 &mesh_debug, "DHT_put for id completed");
633 NULL, /* Continuation */
634 NULL); /* Continuation closure */
637 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
642 * Function to process paths received for a new peer addition. The recorded
643 * paths form the initial tunnel, which can be optimized later.
644 * Called on each result obtained for the DHT search.
647 * @param exp when will this value expire
648 * @param key key of the result
649 * @param type type of the result
650 * @param size number of bytes in data
651 * @param data pointer to the result data
654 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
655 const GNUNET_HashCode * key,
656 const struct GNUNET_PeerIdentity *get_path,
657 unsigned int get_path_length,
658 const struct GNUNET_PeerIdentity *put_path,
659 unsigned int put_path_length,
660 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
663 /******************************************************************************/
664 /****************** GENERAL HELPER FUNCTIONS ************************/
665 /******************************************************************************/
668 * Check if client has registered with the service and has not disconnected
670 * @param client the client to check
672 * @return non-NULL if client exists in the global DLL
674 static struct MeshClient *
675 client_get (struct GNUNET_SERVER_Client *client)
677 struct MeshClient *c;
682 if (c->handle == client)
691 * Checks if a given client has subscribed to certain message type
693 * @param message_type Type of message to check
694 * @param c Client to check
696 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
701 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
705 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
706 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
711 * Allow a client to send more data after transmitting a multicast message
712 * which some neighbor has not yet accepted altough a reasonable time has
715 * @param cls Closure (DataDescriptor containing the task identifier)
716 * @param tc Task Context
719 client_allow_send(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
721 struct MeshDataDescriptor *info = cls;
723 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727 "MESH: CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
730 *(info->timeout_task) = GNUNET_SCHEDULER_NO_TASK;
731 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
736 * Search for a tunnel by global ID using full PeerIdentities
738 * @param oid owner of the tunnel
739 * @param tid global tunnel number
741 * @return tunnel handler, NULL if doesn't exist
743 static struct MeshTunnel *
744 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
748 * Notify a tunnel that a connection has broken that affects at least
751 * @param t Tunnel affected.
752 * @param p1 Peer that got disconnected from p2.
753 * @param p2 Peer that got disconnected from p1.
755 * @return Short ID of the peer disconnected (either p1 or p2).
756 * 0 if the tunnel remained unaffected.
758 static GNUNET_PEER_Id
759 tunnel_notify_connection_broken (struct MeshTunnel *t,
765 * Send the message to all clients that have subscribed to its type
767 * @param msg Pointer to the message itself
768 * @return number of clients this message was sent to
771 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
772 const struct GNUNET_MessageHeader *payload)
774 struct GNUNET_PeerIdentity *oid;
775 struct MeshClient *c;
776 struct MeshTunnel *t;
777 MESH_TunnelNumber *tid;
780 char cbuf[htons(msg->size)];
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
783 type = ntohs (payload->type);
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
786 memcpy (cbuf, msg, sizeof(cbuf));
787 switch (htons(msg->type))
789 struct GNUNET_MESH_Unicast *uc;
790 struct GNUNET_MESH_Multicast *mc;
791 struct GNUNET_MESH_ToOrigin *to;
793 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
794 uc = (struct GNUNET_MESH_Unicast *) cbuf;
798 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
799 mc = (struct GNUNET_MESH_Multicast *) cbuf;
803 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
804 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
812 t = tunnel_get (oid, ntohl(*tid));
818 *tid = htonl (t->local_tid);
819 for (count = 0, c = clients; c != NULL; c = c->next)
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
822 if (client_is_subscribed (type, c))
825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
826 GNUNET_SERVER_notification_context_unicast (
829 (struct GNUNET_MessageHeader *) cbuf,
838 * Notify the client that owns the tunnel that a peer has connected to it
839 * (the requested path to it has been confirmed).
841 * @param t Tunnel whose owner to notify
842 * @param id Short id of the peer that has connected
845 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
847 struct GNUNET_MESH_PeerControl pc;
849 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
850 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
851 pc.tunnel_id = htonl (t->local_tid);
852 GNUNET_PEER_resolve (id, &pc.peer);
853 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
854 &pc.header, GNUNET_NO);
859 * Notify all clients (not depending on registration status) that the incoming
860 * tunnel is no longer valid.
862 * @param t Tunnel that was destroyed.
865 send_clients_tunnel_destroy (struct MeshTunnel *t)
867 struct GNUNET_MESH_TunnelMessage msg;
869 msg.header.size = htons (sizeof (msg));
870 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
871 msg.tunnel_id = htonl (t->local_tid);
872 GNUNET_SERVER_notification_context_broadcast(nc, &msg.header, GNUNET_NO);
877 * Function called to notify a client about the socket
878 * being ready to queue more data. "buf" will be
879 * NULL and "size" zero if the socket was closed for
880 * writing in the meantime.
883 * @param size number of bytes available in buf
884 * @param buf where the callee should write the message
885 * @return number of bytes written to buf
888 send_core_create_path (void *cls, size_t size, void *buf);
892 * Function called to notify a client about the socket
893 * being ready to queue more data. "buf" will be
894 * NULL and "size" zero if the socket was closed for
895 * writing in the meantime.
897 * @param cls closure (data itself)
898 * @param size number of bytes available in buf
899 * @param buf where the callee should write the message
901 * @return number of bytes written to buf
904 send_core_data_multicast (void *cls, size_t size, void *buf);
908 * Decrements the reference counter and frees all resources if needed
910 * @param dd Data Descriptor used in a multicast message
913 data_descriptor_decrement_multicast (struct MeshDataDescriptor *dd)
915 if (0 == --(*(dd->copies)))
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
918 if (NULL != dd->client)
920 if (GNUNET_SCHEDULER_NO_TASK != *(dd->timeout_task))
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 "MESH: cancelling client timeout (%u)...\n",
924 *(dd->timeout_task));
925 GNUNET_SCHEDULER_cancel(*(dd->timeout_task));
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
927 GNUNET_SERVER_receive_done (dd->client, GNUNET_OK);
929 GNUNET_free (dd->timeout_task);
931 GNUNET_free (dd->copies);
932 GNUNET_free (dd->data);
938 * Cancel a core transmission that was already requested and free all resources
939 * associated to the request.
941 * @param peer PeeInfo of the peer whose transmission is cancelled.
942 * @param i Position of the transmission to be cancelled.
945 peer_info_cancel_transmission(struct MeshPeerInfo *peer, unsigned int i)
947 if (NULL != peer->core_transmit[i])
949 struct MeshDataDescriptor *dd;
950 struct MeshPathInfo *path_info;
954 struct GNUNET_PeerIdentity id;
956 GNUNET_PEER_resolve (peer->id, &id);
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 "MESH: Cancelling data transmission at %s [%u]\n",
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "MESH: message type %u\n",
966 /* TODO: notify that tranmission has failed */
967 switch (peer->types[i])
969 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
970 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
971 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
974 data_descriptor_decrement_multicast (dd);
976 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
978 path_info = peer->infos[i];
979 path_destroy(path_info->path);
983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
985 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
986 peer->core_transmit[i] = NULL;
987 GNUNET_free (peer->infos[i]);
993 * Get a unused CORE slot to transmit a message to a peer. If all the slots
994 * are used, cancel one and return it's position.
996 * @param peer PeerInfo of the neighbor we want to transmit to.
998 * @return The index of an available slot to transmit to the neighbor.
1001 peer_info_transmit_slot (struct MeshPeerInfo *peer)
1005 for (i = 0; peer->core_transmit[i]; i++)
1007 if (i == (CORE_QUEUE_SIZE - 1))
1009 /* All positions are taken! Overwriting! */
1011 peer_info_cancel_transmission(peer, 0);
1020 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1021 * and insert it in the appropiate structures if the peer is not known yet.
1023 * @param peer Full identity of the peer.
1025 * @return Existing or newly created peer info.
1027 static struct MeshPeerInfo *
1028 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1030 struct MeshPeerInfo *peer_info;
1032 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1033 if (NULL == peer_info)
1036 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1037 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1038 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1039 peer_info->id = GNUNET_PEER_intern (peer);
1047 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1048 * and insert it in the appropiate structures if the peer is not known yet.
1050 * @param peer Short identity of the peer.
1052 * @return Existing or newly created peer info.
1054 static struct MeshPeerInfo *
1055 peer_info_get_short (const GNUNET_PEER_Id peer)
1057 struct GNUNET_PeerIdentity id;
1059 GNUNET_PEER_resolve(peer, &id);
1060 return peer_info_get(&id);
1065 * Iterator to remove the tunnel from the list of tunnels a peer participates
1068 * @param cls Closure (tunnel info)
1069 * @param key GNUNET_PeerIdentity of the peer (unused)
1070 * @param value PeerInfo of the peer
1072 * @return always GNUNET_YES, to keep iterating
1075 peer_info_delete_tunnel (void* cls, const GNUNET_HashCode* key, void* value)
1077 struct MeshTunnel *t = cls;
1078 struct MeshPeerInfo *peer = value;
1081 for (i = 0; i < peer->ntunnels; i++)
1083 if (0 == memcmp(&peer->tunnels[i]->id,
1085 sizeof(struct MESH_TunnelID)))
1088 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1089 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1098 * Core callback to write a
1100 * @param cls Closure (MeshDataDescriptor with data in "data" member).
1101 * @param size Number of bytes available in buf.
1102 * @param buf Where the to write the message.
1104 * @return number of bytes written to buf
1107 send_core_data_raw (void *cls, size_t size, void *buf)
1109 struct MeshDataDescriptor *info = cls;
1110 struct GNUNET_MessageHeader *msg;
1113 GNUNET_assert (NULL != info);
1114 GNUNET_assert (NULL != info->data);
1115 msg = (struct GNUNET_MessageHeader *) info->data;
1116 total_size = ntohs (msg->size);
1118 if (total_size > size)
1120 struct GNUNET_PeerIdentity id;
1122 GNUNET_PEER_resolve (info->peer->id, &id);
1123 info->peer->core_transmit[info->handler_n] =
1124 GNUNET_CORE_notify_transmit_ready(core_handle,
1127 GNUNET_TIME_UNIT_FOREVER_REL,
1130 &send_core_data_raw,
1134 info->peer->core_transmit[info->handler_n] = NULL;
1135 memcpy (buf, msg, total_size);
1136 GNUNET_free (info->data);
1143 * Sends an already built message to a peer, properly registrating
1144 * all used resources.
1146 * @param message Message to send. Fucntion makes a copy of it.
1147 * @param peer Short ID of the neighbor whom to send the message.
1150 send_message (const struct GNUNET_MessageHeader *message,
1151 const struct GNUNET_PeerIdentity *peer)
1153 struct MeshDataDescriptor *info;
1154 struct MeshPeerInfo *neighbor;
1155 struct MeshPeerPath *p;
1159 // GNUNET_TRANSPORT_try_connect();
1161 size = ntohs (message->size);
1162 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1163 info->data = GNUNET_malloc (size);
1164 memcpy (info->data, message, size);
1165 neighbor = peer_info_get (peer);
1166 for (p = neighbor->path_head; NULL != p; p = p->next)
1178 i = peer_info_transmit_slot (neighbor);
1179 info->handler_n = i;
1180 info->peer = neighbor;
1181 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1182 neighbor->infos[i] = info;
1183 neighbor->core_transmit[i] =
1184 GNUNET_CORE_notify_transmit_ready(core_handle,
1187 GNUNET_TIME_UNIT_FOREVER_REL,
1190 &send_core_data_raw,
1197 * Sends a CREATE PATH message for a path to a peer, properly registrating
1198 * all used resources.
1200 * @param peer PeerInfo of the final peer for whom this path is being created.
1201 * @param p Path itself.
1202 * @param t Tunnel for which the path is created.
1205 send_create_path (struct MeshPeerInfo *peer,
1206 struct MeshPeerPath *p,
1207 struct MeshTunnel *t)
1209 struct GNUNET_PeerIdentity id;
1210 struct MeshPathInfo *path_info;
1211 struct MeshPeerInfo *neighbor;
1216 p = tree_get_path_to_peer(t->tree, peer->id);
1223 for (i = 0; i < p->length; i++)
1225 if (p->peers[i] == myid)
1228 if (i >= p->length - 1)
1234 GNUNET_PEER_resolve(p->peers[i + 1], &id);
1236 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1237 path_info->path = p;
1239 neighbor = peer_info_get(&id);
1240 path_info->peer = neighbor;
1241 path_info->pos = peer_info_transmit_slot(neighbor);
1242 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1243 neighbor->infos[path_info->pos] = path_info;
1244 neighbor->core_transmit[path_info->pos] =
1245 GNUNET_CORE_notify_transmit_ready (
1246 core_handle, /* handle */
1249 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1251 sizeof (struct GNUNET_MESH_ManipulatePath)
1252 + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1253 &send_core_create_path, /* callback */
1254 path_info); /* cls */
1259 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1261 * @param t Tunnel whose path to destroy.
1262 * @param destination Short ID of the peer to whom the path to destroy.
1265 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1267 struct MeshPeerPath *p;
1270 p = tree_get_path_to_peer(t->tree, destination);
1276 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1277 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1279 struct GNUNET_MESH_ManipulatePath *msg;
1280 struct GNUNET_PeerIdentity *pi;
1284 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1285 msg->header.size = htons (size);
1286 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1287 msg->tid = htonl (t->id.tid);
1288 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1289 for (i = 0; i < p->length; i++)
1291 GNUNET_PEER_resolve(p->peers[i], &pi[i]);
1293 send_message (&msg->header, path_get_first_hop(t->tree, destination));
1300 * Try to establish a new connection to this peer.
1301 * Use the best path for the given tunnel.
1302 * If the peer doesn't have any path to it yet, try to get one.
1303 * If the peer already has some path, send a CREATE PATH towards it.
1305 * @param peer PeerInfo of the peer.
1306 * @param t Tunnel for which to create the path, if possible.
1309 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1311 struct MeshPeerPath *p;
1312 struct MeshPathInfo *path_info;
1314 if (NULL != peer->path_head)
1316 p = tree_get_path_to_peer(t->tree, peer->id);
1325 send_create_path(peer, p, t);
1330 send_client_peer_connected(t, myid);
1333 else if (NULL == peer->dhtget)
1335 struct GNUNET_PeerIdentity id;
1337 GNUNET_PEER_resolve(peer->id, &id);
1338 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1339 path_info->peer = peer;
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1342 "MESH: Starting DHT GET for peer %s\n",
1344 peer->dhtgetcls = path_info;
1346 GNUNET_DHT_get_start(dht_handle, /* handle */
1347 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1348 GNUNET_BLOCK_TYPE_TEST, /* type */
1349 &id.hashPubKey, /* key to search */
1350 4, /* replication level */
1351 GNUNET_DHT_RO_RECORD_ROUTE |
1352 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1354 0, /* xquery bits */
1355 &dht_get_id_handler,
1358 /* Otherwise, there is no path but the DHT get is already started. */
1363 * Task to delay the connection of a peer
1365 * @param cls Closure (path info with tunnel and peer to connect).
1366 * Will be free'd on exection.
1367 * @param tc TaskContext
1370 peer_info_connect_task (void *cls,
1371 const struct GNUNET_SCHEDULER_TaskContext *tc)
1373 struct MeshPathInfo *path_info = cls;
1375 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1380 peer_info_connect (path_info->peer, path_info->t);
1386 * Destroy the peer_info and free any allocated resources linked to it
1388 * @param pi The peer_info to destroy.
1390 * @return GNUNET_OK on success
1393 peer_info_destroy (struct MeshPeerInfo *pi)
1395 struct GNUNET_PeerIdentity id;
1396 struct MeshPeerPath *p;
1397 struct MeshPeerPath *nextp;
1400 GNUNET_PEER_resolve (pi->id, &id);
1401 GNUNET_PEER_change_rc (pi->id, -1);
1404 GNUNET_CONTAINER_multihashmap_remove (peers,&id.hashPubKey, pi))
1407 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1408 "MESH: removing peer %s, not in hashmap\n",
1411 if (NULL != pi->dhtget)
1413 GNUNET_DHT_get_stop(pi->dhtget);
1414 GNUNET_free (pi->dhtgetcls);
1416 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1418 peer_info_cancel_transmission(pi, i);
1424 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1434 * Remove all paths that rely on a direct connection between p1 and p2
1435 * from the peer itself and notify all tunnels about it.
1437 * @param peer PeerInfo of affected peer.
1438 * @param p1 GNUNET_PEER_Id of one peer.
1439 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1442 * TODO: optimize (see below)
1445 peer_info_remove_path (struct MeshPeerInfo *peer,
1449 struct MeshPeerPath *p;
1450 struct MeshPeerPath *aux;
1451 struct MeshPeerInfo *peer_d;
1453 unsigned int destroyed;
1459 p = peer->path_head;
1463 for (i = 0; i < (p->length - 1); i++)
1465 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1466 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1468 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1479 for (i = 0; i < peer->ntunnels; i++)
1481 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1483 * Problem: one or more peers have been deleted from the tunnel tree.
1484 * We don't know who they are to try to add them again.
1485 * We need to try to find a new path for each of the disconnected peers.
1486 * Some of them might already have a path to reach them that does not
1487 * involve p1 and p2. Adding all anew might render in a better tree than
1488 * the trivial immediate fix.
1490 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1491 * its children will be reachable trough him.
1493 peer_d = peer_info_get_short(d);
1496 for (p = peer_d->path_head; NULL != p; p = p->next)
1498 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
1506 /* No callback, as peer will be already disconnected */
1507 tree_add_path(peer->tunnels[i]->tree, aux, NULL, NULL);
1511 peer_info_connect (peer_d, peer->tunnels[i]);
1518 * Add the path to the peer and update the path used to reach it in case this
1521 * @param peer_info Destination peer to add the path to.
1522 * @param path New path to add. Last peer must be the peer in arg 1.
1523 * Path will be either used of freed if already known.
1524 * @param trusted Do we trust that this path is real?
1527 peer_info_add_path (struct MeshPeerInfo *peer_info,
1528 struct MeshPeerPath *path,
1531 struct MeshPeerPath *aux;
1535 if ((NULL == peer_info) || (NULL == path))
1538 path_destroy (path);
1541 if (path->length <= 2 && GNUNET_NO == trusted)
1543 /* Only allow CORE to tell us about direct paths */
1544 path_destroy (path);
1547 for (l = 1; l < path->length; l++)
1549 if (path->peers[l] == myid)
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "MESH: shortening path by %u\n",
1554 for (l2 = 0; l2 < path->length - l - 1 ; l2++)
1556 path->peers[l2] = path->peers[l + l2];
1560 path->peers = GNUNET_realloc (path->peers,
1561 path->length * sizeof (GNUNET_PEER_Id));
1566 struct GNUNET_PeerIdentity id;
1568 GNUNET_PEER_resolve (peer_info->id, &id);
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1570 "MESH: adding path [%u] to peer %s\n",
1575 l = path_get_length (path);
1582 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1583 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1585 l2 = path_get_length (aux);
1588 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1589 peer_info->path_tail,
1596 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1603 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head,
1604 peer_info->path_tail,
1611 * Add the path to the origin peer and update the path used to reach it in case
1612 * this is the shortest.
1613 * The path is given in peer_info -> destination, therefore we turn the path
1614 * upside down first.
1616 * @param peer_info Peer to add the path to, being the origin of the path.
1617 * @param path New path to add after being inversed.
1618 * @param trusted Do we trust that this path is real?
1621 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1622 struct MeshPeerPath *path,
1626 peer_info_add_path (peer_info, path, trusted);
1631 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1632 * to obtain a local peer -> destination path and interning the peer ids.
1634 * @return Newly allocated and created path
1636 static struct MeshPeerPath *
1637 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1638 unsigned int get_path_length,
1639 const struct GNUNET_PeerIdentity *put_path,
1640 unsigned int put_path_length)
1642 struct MeshPeerPath *p;
1648 GNUNET_PEER_change_rc(myid, 1);
1649 i = get_path_length;
1650 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1651 for (i--; i >= 0; i--)
1653 id = GNUNET_PEER_intern (&get_path[i]);
1654 if (p->length > 0 && id == p->peers[p->length - 1])
1656 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1657 GNUNET_PEER_change_rc(id, -1);
1661 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1662 "MESH: Adding from GET: %s.\n",
1663 GNUNET_i2s(&get_path[i]));
1665 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1666 p->peers[p->length - 1] = id;
1669 i = put_path_length;
1670 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1671 for (i--; i >= 0; i--)
1673 id = GNUNET_PEER_intern (&put_path[i]);
1676 /* PUT path went through us, so discard the path up until now and start
1677 * from here to get a much shorter (and loop-free) path.
1682 if (p->length > 0 && id == p->peers[p->length - 1])
1684 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1685 GNUNET_PEER_change_rc(id, -1);
1689 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1690 "MESH: Adding from PUT: %s.\n",
1691 GNUNET_i2s(&put_path[i]));
1693 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1694 p->peers[p->length - 1] = id;
1698 if (get_path_length > 0)
1699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1700 "MESH: (first of GET: %s)\n",
1701 GNUNET_i2s(&get_path[0]));
1702 if (put_path_length > 0)
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1704 "MESH: (first of PUT: %s)\n",
1705 GNUNET_i2s(&put_path[0]));
1706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1707 "MESH: In total: %d hops\n",
1709 for (i = 0; i < p->length; i++)
1711 struct GNUNET_PeerIdentity peer_id;
1713 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1717 GNUNET_i2s(&peer_id));
1725 * Send keepalive packets for a peer
1727 * @param cls Closure (tunnel for which to send the keepalive).
1728 * @param tc Notification context.
1730 * TODO: implement explicit multicast keepalive?
1733 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1737 * Search for a tunnel among the incoming tunnels
1739 * @param tid the local id of the tunnel
1741 * @return tunnel handler, NULL if doesn't exist
1743 static struct MeshTunnel *
1744 tunnel_get_incoming (MESH_TunnelNumber tid)
1746 GNUNET_HashCode hash;
1748 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1749 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1750 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1755 * Search for a tunnel among the tunnels for a client
1757 * @param c the client whose tunnels to search in
1758 * @param tid the local id of the tunnel
1760 * @return tunnel handler, NULL if doesn't exist
1762 static struct MeshTunnel *
1763 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1765 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1767 return tunnel_get_incoming (tid);
1771 GNUNET_HashCode hash;
1773 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1774 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1780 * Search for a tunnel by global ID using PEER_ID
1782 * @param pi owner of the tunnel
1783 * @param tid global tunnel number
1785 * @return tunnel handler, NULL if doesn't exist
1787 static struct MeshTunnel *
1788 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1790 struct MESH_TunnelID id;
1791 GNUNET_HashCode hash;
1796 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1797 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1802 * Search for a tunnel by global ID using full PeerIdentities
1804 * @param oid owner of the tunnel
1805 * @param tid global tunnel number
1807 * @return tunnel handler, NULL if doesn't exist
1809 static struct MeshTunnel *
1810 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1812 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1817 * Callback used to notify a client owner of a tunnel that a peer has
1818 * disconnected, most likely because of a path change.
1820 * @param cls Closure (tunnel this notification is about).
1821 * @param peer_id Short ID of disconnected peer.
1824 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
1826 struct MeshTunnel *t = cls;
1827 struct MeshPeerInfo *peer;
1828 struct MeshPathInfo *path_info;
1830 if (NULL != t->client && NULL != nc)
1832 struct GNUNET_MESH_PeerControl msg;
1833 msg.header.size = htons (sizeof (msg));
1834 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1835 msg.tunnel_id = htonl (t->local_tid);
1836 GNUNET_PEER_resolve (peer_id, &msg.peer);
1837 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1838 &msg.header, GNUNET_NO);
1840 peer = peer_info_get_short(peer_id);
1841 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1842 path_info->peer = peer;
1844 GNUNET_SCHEDULER_add_now(&peer_info_connect_task, path_info);
1849 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1850 * needed rescources.
1851 * If peer already exists, reevaluate shortest path and change if different.
1853 * @param t Tunnel we want to add a new peer to
1854 * @param peer PeerInfo of the peer being added
1858 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1860 struct GNUNET_PeerIdentity id;
1861 struct MeshPeerPath *best_p;
1862 struct MeshPeerPath *p;
1863 unsigned int best_cost;
1866 GNUNET_PEER_resolve(peer->id, &id);
1868 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1871 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1872 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(
1876 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1879 if (NULL != (p = peer->path_head))
1882 best_cost = path_get_cost(t->tree, p);
1885 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1892 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
1893 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1894 t->path_refresh_task =
1895 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
1899 /* Start a DHT get if necessary */
1900 peer_info_connect(peer, t);
1905 * Add a path to a tunnel which we don't own, just to remember the next hop.
1906 * If destination node was already in the tunnel, the first hop information
1907 * will be replaced with the new path.
1909 * @param t Tunnel we want to add a new peer to
1910 * @param p Path to add
1911 * @param own_pos Position of local node in path.
1915 tunnel_add_path (struct MeshTunnel *t,
1916 struct MeshPeerPath *p,
1917 unsigned int own_pos)
1919 struct GNUNET_PeerIdentity id;
1921 GNUNET_assert (0 != own_pos);
1922 tree_add_path(t->tree, p, NULL, NULL);
1923 if (tree_get_me (t->tree) == 0)
1924 tree_set_me (t->tree, p->peers[own_pos]);
1925 if (own_pos < p->length - 1)
1927 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1928 tree_update_first_hops(t->tree,
1929 tree_get_me (t->tree),
1936 * Notifies a tunnel that a connection has broken that affects at least
1937 * some of its peers. Sends a notification towards the root of the tree.
1938 * In case the peer is the owner of the tree, notifies the client that owns
1939 * the tunnel and tries to reconnect.
1941 * @param t Tunnel affected.
1942 * @param p1 Peer that got disconnected from p2.
1943 * @param p2 Peer that got disconnected from p1.
1945 * @return Short ID of the peer disconnected (either p1 or p2).
1946 * 0 if the tunnel remained unaffected.
1948 static GNUNET_PEER_Id
1949 tunnel_notify_connection_broken (struct MeshTunnel *t,
1955 pid = tree_notify_connection_broken (t->tree,
1958 ¬ify_peer_disconnected,
1960 if (myid != p1 && myid != p2)
1966 if (tree_get_predecessor(t->tree) != 0)
1968 /* We are the peer still connected, notify owner of the disconnection. */
1969 struct GNUNET_MESH_PathBroken msg;
1970 struct GNUNET_PeerIdentity neighbor;
1972 msg.header.size = htons (sizeof (msg));
1973 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1974 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1975 msg.tid = htonl (t->id.tid);
1976 msg.peer1 = my_full_id;
1977 GNUNET_PEER_resolve (pid, &msg.peer2);
1978 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1979 send_message (&msg.header, &neighbor);
1986 struct MeshMulticastData
1988 struct MeshTunnel *t;
1990 GNUNET_SCHEDULER_TaskIdentifier *task;
1992 unsigned int *reference_counter;
2001 * Send a multicast packet to a neighbor.
2004 tunnel_send_multicast_iterator (void *cls,
2005 GNUNET_PEER_Id neighbor_id)
2007 struct MeshMulticastData *mdata = cls;
2008 struct MeshDataDescriptor *info;
2009 struct GNUNET_PeerIdentity neighbor;
2012 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2014 info->data = mdata->data;
2015 info->size = mdata->data_len;
2016 info->copies = mdata->reference_counter;
2017 (*(mdata->reference_counter))++;
2019 if (NULL != mdata->t->client)
2021 info->client = mdata->t->client->handle;
2022 info->timeout_task = mdata->task;
2024 info->destination = neighbor_id;
2025 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2028 "MESH: sending to %s...\n",
2029 GNUNET_i2s (&neighbor));
2031 info->peer = peer_info_get(&neighbor);
2032 GNUNET_assert (NULL != info->peer);
2033 i = peer_info_transmit_slot(info->peer);
2034 info->handler_n = i;
2035 info->peer->infos[i] = info;
2036 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2037 info->peer->core_transmit[i] =
2038 GNUNET_CORE_notify_transmit_ready (core_handle,
2041 GNUNET_TIME_UNIT_FOREVER_REL,
2044 &send_core_data_multicast, info);
2048 * Send a message in a tunnel in multicast, sending a copy to each child node
2049 * down the local one in the tunnel tree.
2051 * @param t Tunnel in which to send the data.
2052 * @param msg Message to be sent
2055 tunnel_send_multicast (struct MeshTunnel *t,
2056 const struct GNUNET_MessageHeader *msg)
2058 struct MeshMulticastData *mdata;
2061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2062 "MESH: sending a multicast packet...\n");
2064 GNUNET_assert (tree_get_me (t->tree) != 0);
2065 mdata = GNUNET_malloc (sizeof (struct MeshMulticastData));
2066 mdata->data_len = ntohs (msg->size);
2067 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2068 mdata->data = GNUNET_malloc (mdata->data_len);
2070 memcpy (mdata->data, msg, mdata->data_len);
2071 if (NULL != t->client)
2073 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2074 *(mdata->task) = GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT,
2079 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2080 if (*(mdata->reference_counter) == 0)
2082 GNUNET_free (mdata->data);
2083 GNUNET_free (mdata->reference_counter);
2084 if (NULL != mdata->task)
2086 GNUNET_free (mdata->task);
2089 GNUNET_free (mdata);
2091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2092 "MESH: sending a multicast packet done\n");
2099 * Send a message to all peers in this tunnel that the tunnel is no longer
2102 * @param t The tunnel whose peers to notify.
2105 tunnel_send_destroy (struct MeshTunnel *t)
2107 struct GNUNET_MESH_TunnelDestroy msg;
2109 msg.header.size = htons (sizeof (msg));
2110 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2111 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2112 msg.tid = htonl (t->id.tid);
2113 tunnel_send_multicast (t, &msg.header);
2119 * Destroy the tunnel and free any allocated resources linked to it
2121 * @param t the tunnel to destroy
2123 * @return GNUNET_OK on success
2126 tunnel_destroy (struct MeshTunnel *t)
2128 struct MeshClient *c;
2129 struct MeshQueue *q;
2130 struct MeshQueue *qn;
2131 GNUNET_HashCode hash;
2140 struct GNUNET_PeerIdentity id;
2142 GNUNET_PEER_resolve(t->id.oid, &id);
2143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2144 "MESH: destroying tunnel %s [%x]\n",
2148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2152 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2153 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2158 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2160 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
2164 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2166 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2167 GNUNET_break (GNUNET_YES ==
2168 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t));
2170 if (NULL != t->peers)
2172 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
2173 &peer_info_delete_tunnel, t);
2174 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2179 if (NULL != q->data)
2180 GNUNET_free (q->data);
2184 /* TODO cancel core transmit ready in case it was active */
2186 tree_destroy(t->tree);
2187 if (NULL != t->dht_get_type)
2188 GNUNET_DHT_get_stop(t->dht_get_type);
2189 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2190 GNUNET_SCHEDULER_cancel(t->timeout_task);
2191 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2192 GNUNET_SCHEDULER_cancel(t->path_refresh_task);
2199 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2200 * that are no longer needed, as well as nodes of no longer reachable peers.
2201 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2203 * @param t Tunnel from which to remove the path.
2204 * @param p Peer which should be removed.
2207 tunnel_delete_peer (struct MeshTunnel *t,
2208 GNUNET_PEER_Id peer)
2210 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2216 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2217 * client when the client disconnects.
2219 * @param cls closure (client that is disconnecting)
2220 * @param key the hash of the local tunnel id (used to access the hashmap)
2221 * @param value the value stored at the key (tunnel to destroy)
2223 * @return GNUNET_OK on success
2226 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2228 struct MeshTunnel *t = value;
2231 r = tunnel_destroy (t);
2237 * Timeout function, destroys tunnel if called
2239 * @param cls Closure (tunnel to destroy).
2240 * @param tc TaskContext
2243 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2245 struct MeshTunnel *t = cls;
2247 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2249 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2254 * Resets the tunnel timeout. Starts it if no timeout was running.
2256 * @param t Tunnel whose timeout to reset.
2259 tunnel_reset_timeout (struct MeshTunnel *t)
2261 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2262 GNUNET_SCHEDULER_cancel (t->timeout_task);
2263 t->timeout_task = GNUNET_SCHEDULER_add_delayed (
2264 GNUNET_TIME_relative_multiply(REFRESH_PATH_TIME, 4),
2270 /******************************************************************************/
2271 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2272 /******************************************************************************/
2275 * Function called to notify a client about the socket
2276 * being ready to queue more data. "buf" will be
2277 * NULL and "size" zero if the socket was closed for
2278 * writing in the meantime.
2280 * @param cls closure
2281 * @param size number of bytes available in buf
2282 * @param buf where the callee should write the message
2283 * @return number of bytes written to buf
2286 send_core_create_path (void *cls, size_t size, void *buf)
2288 struct MeshPathInfo *info = cls;
2289 struct GNUNET_MESH_ManipulatePath *msg;
2290 struct GNUNET_PeerIdentity *peer_ptr;
2291 struct MeshPeerInfo *peer = info->peer;
2292 struct MeshTunnel *t = info->t;
2293 struct MeshPeerPath *p = info->path;
2297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2298 "MESH: CREATE PATH sending...\n");
2300 sizeof (struct GNUNET_MESH_ManipulatePath) +
2301 p->length * sizeof (struct GNUNET_PeerIdentity);
2303 if (size < size_needed || NULL == buf)
2305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
2309 info->peer->core_transmit[info->pos] =
2310 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2311 GNUNET_TIME_UNIT_FOREVER_REL,
2312 path_get_first_hop (t->tree, peer->id),
2314 &send_core_create_path,
2318 info->peer->core_transmit[info->pos] = NULL;
2321 struct GNUNET_PeerIdentity id;
2323 GNUNET_PEER_resolve (peer->id, &id);
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 "MESH: setting core_transmit %s [%u] to NULL\n",
2330 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2331 msg->header.size = htons (size_needed);
2332 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2333 msg->tid = ntohl (t->id.tid);
2335 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2336 for (i = 0; i < p->length; i++)
2338 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2345 "MESH: CREATE PATH (%u bytes long) sent!\n",
2352 * Function called to notify a client about the socket
2353 * being ready to queue more data. "buf" will be
2354 * NULL and "size" zero if the socket was closed for
2355 * writing in the meantime.
2357 * @param cls closure (data itself)
2358 * @param size number of bytes available in buf
2359 * @param buf where the callee should write the message
2361 * @return number of bytes written to buf
2364 send_core_data_multicast (void *cls, size_t size, void *buf)
2366 struct MeshDataDescriptor *info = cls;
2369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2370 GNUNET_assert (NULL != info);
2371 GNUNET_assert (NULL != info->peer);
2372 total_size = info->size;
2373 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2375 if (total_size > size)
2378 struct GNUNET_PeerIdentity id;
2380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2381 "MESH: Multicast: retransmitting... (%u/%u)\n",
2383 GNUNET_PEER_resolve(info->peer->id, &id);
2384 info->peer->core_transmit[info->handler_n] =
2385 GNUNET_CORE_notify_transmit_ready (core_handle,
2388 GNUNET_TIME_UNIT_FOREVER_REL,
2391 &send_core_data_multicast,
2395 info->peer->core_transmit[info->handler_n] = NULL;
2396 info->peer->infos[info->handler_n] = NULL;
2397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2398 memcpy (buf, info->data, total_size);
2401 struct GNUNET_MESH_Multicast *mc;
2402 struct GNUNET_MessageHeader *mh;
2405 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2407 mc = (struct GNUNET_MESH_Multicast *) mh;
2408 mh = (struct GNUNET_MessageHeader *) &mc[1];
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410 "MESH: multicast, payload type %u\n",
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2413 "MESH: multicast, payload size %u\n",
2418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2424 data_descriptor_decrement_multicast (info);
2425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2433 * Function called to notify a client about the socket
2434 * being ready to queue more data. "buf" will be
2435 * NULL and "size" zero if the socket was closed for
2436 * writing in the meantime.
2438 * @param cls closure (MeshDataDescriptor)
2439 * @param size number of bytes available in buf
2440 * @param buf where the callee should write the message
2441 * @return number of bytes written to buf
2444 send_core_path_ack (void *cls, size_t size, void *buf)
2446 struct MeshDataDescriptor *info = cls;
2447 struct GNUNET_MESH_PathACK *msg = buf;
2449 GNUNET_assert (NULL != info);
2452 info->peer->core_transmit[info->handler_n] = NULL;
2454 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2459 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2460 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2461 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2462 msg->tid = htonl (info->origin->tid);
2463 msg->peer_id = my_full_id;
2465 /* TODO add signature */
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2468 return sizeof (struct GNUNET_MESH_PathACK);
2472 /******************************************************************************/
2473 /******************** MESH NETWORK HANDLERS **************************/
2474 /******************************************************************************/
2478 * Core handler for path creation
2480 * @param cls closure
2481 * @param message message
2482 * @param peer peer identity this notification is about
2483 * @param atsi performance data
2484 * @param atsi_count number of records in 'atsi'
2486 * @return GNUNET_OK to keep the connection open,
2487 * GNUNET_SYSERR to close it (signal serious error)
2490 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2491 const struct GNUNET_MessageHeader *message,
2492 const struct GNUNET_ATS_Information *atsi,
2493 unsigned int atsi_count)
2495 unsigned int own_pos;
2498 MESH_TunnelNumber tid;
2499 struct GNUNET_MESH_ManipulatePath *msg;
2500 struct GNUNET_PeerIdentity *pi;
2501 GNUNET_HashCode hash;
2502 struct MeshPeerPath *path;
2503 struct MeshPeerInfo *dest_peer_info;
2504 struct MeshPeerInfo *orig_peer_info;
2505 struct MeshTunnel *t;
2507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2508 "MESH: Received a path create msg [%s]\n",
2509 GNUNET_i2s(&my_full_id));
2510 size = ntohs (message->size);
2511 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2513 GNUNET_break_op (0);
2517 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2518 if (size % sizeof (struct GNUNET_PeerIdentity))
2520 GNUNET_break_op (0);
2523 size /= sizeof (struct GNUNET_PeerIdentity);
2526 GNUNET_break_op (0);
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2530 "MESH: path has %u hops.\n",
2532 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2534 tid = ntohl (msg->tid);
2535 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2536 t = tunnel_get (pi, tid);
2537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2538 "MESH: path is for tunnel %s [%X].\n",
2543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2544 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2545 t->id.oid = GNUNET_PEER_intern (pi);
2547 while (NULL != tunnel_get_incoming (next_local_tid))
2548 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2549 t->local_tid = next_local_tid++;
2550 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2551 t->tree = tree_new(t->id.oid);
2553 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2555 GNUNET_CONTAINER_multihashmap_put (
2559 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2565 tunnel_reset_timeout (t);
2566 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2568 GNUNET_CONTAINER_multihashmap_put (
2572 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2580 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2581 if (NULL == dest_peer_info)
2583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2584 "MESH: Creating PeerInfo for destination.\n");
2585 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2586 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2587 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2589 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2591 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2592 if (NULL == orig_peer_info)
2594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2595 "MESH: Creating PeerInfo for origin.\n");
2596 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2597 orig_peer_info->id = GNUNET_PEER_intern (pi);
2598 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2599 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2602 path = path_new (size);
2604 for (i = 0; i < size; i++)
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2607 "MESH: ... adding %s\n",
2608 GNUNET_i2s(&pi[i]));
2609 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2610 if (path->peers[i] == myid)
2613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2614 "MESH: Own position: %u\n", own_pos);
2617 /* cannot be self, must be 'not found' */
2618 /* create path: self not found in path through self */
2619 GNUNET_break_op (0);
2620 path_destroy (path);
2621 /* FIXME error. destroy tunnel? leave for timeout? */
2624 tunnel_add_path (t, path, own_pos);
2625 if (own_pos == size - 1)
2627 /* It is for us! Send ack. */
2628 struct GNUNET_MESH_TunnelNotification cmsg;
2629 struct MeshDataDescriptor *info;
2632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2633 "MESH: It's for us!\n");
2634 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2635 if (NULL == t->peers)
2636 t->peers = GNUNET_CONTAINER_multihashmap_create(4);
2637 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2639 &my_full_id.hashPubKey,
2640 peer_info_get(&my_full_id),
2641 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2642 /* FIXME use send_message */
2643 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2644 info->origin = &t->id;
2645 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2646 GNUNET_assert (NULL != info->peer);
2647 j = peer_info_transmit_slot(info->peer);
2648 info->handler_n = j;
2649 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2650 info->peer->infos[j] = info;
2651 info->peer->core_transmit[j] =
2652 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2653 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2654 sizeof (struct GNUNET_MESH_PathACK),
2655 &send_core_path_ack, info);
2656 cmsg.header.size = htons(sizeof(cmsg));
2657 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2658 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2659 cmsg.tunnel_id = htonl(t->local_tid);
2660 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2664 struct MeshPeerPath *path2;
2666 /* It's for somebody else! Retransmit. */
2667 path2 = path_duplicate(path);
2668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2669 "MESH: Retransmitting.\n");
2670 peer_info_add_path(dest_peer_info, path2, GNUNET_NO);
2671 path2 = path_duplicate(path);
2672 peer_info_add_path_to_origin(orig_peer_info, path2, GNUNET_NO);
2673 send_create_path(dest_peer_info, path, t);
2680 * Core handler for path destruction
2682 * @param cls closure
2683 * @param message message
2684 * @param peer peer identity this notification is about
2685 * @param atsi performance data
2686 * @param atsi_count number of records in 'atsi'
2688 * @return GNUNET_OK to keep the connection open,
2689 * GNUNET_SYSERR to close it (signal serious error)
2692 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2693 const struct GNUNET_MessageHeader *message,
2694 const struct GNUNET_ATS_Information *atsi,
2695 unsigned int atsi_count)
2697 struct GNUNET_MESH_ManipulatePath *msg;
2698 struct GNUNET_PeerIdentity *pi;
2699 struct MeshPeerPath *path;
2700 struct MeshTunnel *t;
2701 unsigned int own_pos;
2705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2706 "MESH: Received a PATH DESTROY msg from %s\n",
2708 size = ntohs (message->size);
2709 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2711 GNUNET_break_op (0);
2715 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2716 if (size % sizeof (struct GNUNET_PeerIdentity))
2718 GNUNET_break_op (0);
2721 size /= sizeof (struct GNUNET_PeerIdentity);
2724 GNUNET_break_op (0);
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2728 "MESH: path has %u hops.\n",
2731 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2732 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2734 "MESH: path is for tunnel %s [%X].\n",
2737 t = tunnel_get (pi, ntohl (msg->tid));
2740 /* TODO notify back: we don't know this tunnel */
2741 GNUNET_break_op (0);
2744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2745 path = path_new (size);
2747 for (i = 0; i < size; i++)
2749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2750 "MESH: ... adding %s\n",
2751 GNUNET_i2s(&pi[i]));
2752 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2753 if (path->peers[i] == myid)
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2757 "MESH: Own position: %u\n", own_pos);
2758 if (own_pos < path->length - 1)
2759 send_message (message, &pi[own_pos + 1]);
2760 tunnel_delete_peer (t, path->peers[path->length - 1]);
2761 path_destroy (path);
2767 * Core handler for notifications of broken paths
2769 * @param cls closure
2770 * @param message message
2771 * @param peer peer identity this notification is about
2772 * @param atsi performance data
2773 * @param atsi_count number of records in 'atsi'
2775 * @return GNUNET_OK to keep the connection open,
2776 * GNUNET_SYSERR to close it (signal serious error)
2779 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
2780 const struct GNUNET_MessageHeader *message,
2781 const struct GNUNET_ATS_Information *atsi,
2782 unsigned int atsi_count)
2784 struct GNUNET_MESH_PathBroken *msg;
2785 struct MeshTunnel *t;
2787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2788 "MESH: Received a PATH BROKEN msg from %s\n",
2790 msg = (struct GNUNET_MESH_PathBroken *) message;
2791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2792 "MESH: regarding %s\n",
2793 GNUNET_i2s(&msg->peer1));
2794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2795 "MESH: regarding %s\n",
2796 GNUNET_i2s(&msg->peer2));
2797 t = tunnel_get(&msg->oid, ntohl (msg->tid));
2800 GNUNET_break_op (0);
2803 tunnel_notify_connection_broken(t,
2804 GNUNET_PEER_search(&msg->peer1),
2805 GNUNET_PEER_search(&msg->peer2));
2812 * Core handler for tunnel destruction
2814 * @param cls closure
2815 * @param message message
2816 * @param peer peer identity this notification is about
2817 * @param atsi performance data
2818 * @param atsi_count number of records in 'atsi'
2820 * @return GNUNET_OK to keep the connection open,
2821 * GNUNET_SYSERR to close it (signal serious error)
2824 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2825 const struct GNUNET_MessageHeader *message,
2826 const struct GNUNET_ATS_Information *atsi,
2827 unsigned int atsi_count)
2829 struct GNUNET_MESH_TunnelDestroy *msg;
2830 struct MeshTunnel *t;
2832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2833 "MESH: Got a TUNNEL DESTROY packet from %s\n",
2835 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2837 "MESH: for tunnel %s [%u]\n",
2838 GNUNET_i2s (&msg->oid),
2840 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2843 /* Probably already got the message from another path,
2844 * destroyed the tunnel and retransmitted to children.
2849 if (t->id.oid == myid)
2851 GNUNET_break_op (0);
2854 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2856 /* Tunnel was incoming, notify clients */
2857 send_clients_tunnel_destroy (t);
2859 tunnel_send_destroy (t);
2866 * Core handler for mesh network traffic going from the origin to a peer
2868 * @param cls closure
2869 * @param peer peer identity this notification is about
2870 * @param message message
2871 * @param atsi performance data
2872 * @param atsi_count number of records in 'atsi'
2873 * @return GNUNET_OK to keep the connection open,
2874 * GNUNET_SYSERR to close it (signal serious error)
2877 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2878 const struct GNUNET_MessageHeader *message,
2879 const struct GNUNET_ATS_Information *atsi,
2880 unsigned int atsi_count)
2882 struct GNUNET_MESH_Unicast *msg;
2883 struct MeshTunnel *t;
2887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2888 "MESH: got a unicast packet from %s\n",
2890 size = ntohs (message->size);
2892 sizeof (struct GNUNET_MESH_Unicast) +
2893 sizeof (struct GNUNET_MessageHeader))
2898 msg = (struct GNUNET_MESH_Unicast *) message;
2899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2900 "MESH: of type %u\n",
2901 ntohs (msg[1].header.type));
2902 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2905 /* TODO notify back: we don't know this tunnel */
2906 GNUNET_break_op (0);
2909 tunnel_reset_timeout (t);
2910 pid = GNUNET_PEER_search(&msg->destination);
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2914 "MESH: it's for us! sending to clients...\n");
2915 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2919 "MESH: not for us, retransmitting...\n");
2920 send_message (message, path_get_first_hop(t->tree, pid));
2926 * Core handler for mesh network traffic going from the origin to all peers
2928 * @param cls closure
2929 * @param message message
2930 * @param peer peer identity this notification is about
2931 * @param atsi performance data
2932 * @param atsi_count number of records in 'atsi'
2933 * @return GNUNET_OK to keep the connection open,
2934 * GNUNET_SYSERR to close it (signal serious error)
2936 * TODO: Check who we got this from, to validate route.
2939 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2940 const struct GNUNET_MessageHeader *message,
2941 const struct GNUNET_ATS_Information *atsi,
2942 unsigned int atsi_count)
2944 struct GNUNET_MESH_Multicast *msg;
2945 struct MeshTunnel *t;
2948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2949 "MESH: got a multicast packet from %s\n",
2951 size = ntohs (message->size);
2952 if (sizeof (struct GNUNET_MESH_Multicast) +
2953 sizeof (struct GNUNET_MessageHeader) > size)
2955 GNUNET_break_op (0);
2958 msg = (struct GNUNET_MESH_Multicast *) message;
2959 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2963 /* TODO notify that we dont know that tunnel */
2964 GNUNET_break_op (0);
2967 tunnel_reset_timeout (t);
2969 /* Transmit to locally interested clients */
2970 if (NULL != t->peers &&
2971 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2973 send_subscribed_clients (message, &msg[1].header);
2975 tunnel_send_multicast(t, message);
2981 * Core handler for mesh network traffic toward the owner of a tunnel
2983 * @param cls closure
2984 * @param message message
2985 * @param peer peer identity this notification is about
2986 * @param atsi performance data
2987 * @param atsi_count number of records in 'atsi'
2989 * @return GNUNET_OK to keep the connection open,
2990 * GNUNET_SYSERR to close it (signal serious error)
2993 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2994 const struct GNUNET_MessageHeader *message,
2995 const struct GNUNET_ATS_Information *atsi,
2996 unsigned int atsi_count)
2998 struct GNUNET_MESH_ToOrigin *msg;
2999 struct GNUNET_PeerIdentity id;
3000 struct MeshPeerInfo *peer_info;
3001 struct MeshTunnel *t;
3004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3005 "MESH: got a ToOrigin packet from %s\n",
3007 size = ntohs (message->size);
3008 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3009 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3011 GNUNET_break_op (0);
3014 msg = (struct GNUNET_MESH_ToOrigin *) message;
3015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3016 "MESH: of type %u\n",
3017 ntohs (msg[1].header.type));
3018 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3022 /* TODO notify that we dont know this tunnel (whom)? */
3023 GNUNET_break_op (0);
3027 if (t->id.oid == myid)
3030 struct GNUNET_MESH_ToOrigin *copy;
3032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3033 "MESH: it's for us! sending to clients...\n");
3034 if (NULL == t->client)
3036 /* got data packet for ownerless tunnel */
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3038 "MESH: no clients!\n");
3039 GNUNET_break_op (0);
3042 /* TODO signature verification */
3043 memcpy (cbuf, message, size);
3044 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3045 copy->tid = htonl (t->local_tid);
3046 GNUNET_SERVER_notification_context_unicast (nc,
3052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3053 "MESH: not for us, retransmitting...\n");
3055 peer_info = peer_info_get (&msg->oid);
3056 if (NULL == peer_info)
3058 /* unknown origin of tunnel */
3062 GNUNET_PEER_resolve (tree_get_predecessor(t->tree), &id);
3063 send_message (message, &id);
3070 * Core handler for path ACKs
3072 * @param cls closure
3073 * @param message message
3074 * @param peer peer identity this notification is about
3075 * @param atsi performance data
3076 * @param atsi_count number of records in 'atsi'
3078 * @return GNUNET_OK to keep the connection open,
3079 * GNUNET_SYSERR to close it (signal serious error)
3082 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3083 const struct GNUNET_MessageHeader *message,
3084 const struct GNUNET_ATS_Information *atsi,
3085 unsigned int atsi_count)
3087 struct GNUNET_MESH_PathACK *msg;
3088 struct GNUNET_PeerIdentity id;
3089 struct MeshPeerInfo *peer_info;
3090 struct MeshTunnel *t;
3092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3093 "MESH: Received a path ACK msg [%s]\n",
3094 GNUNET_i2s(&my_full_id));
3095 msg = (struct GNUNET_MESH_PathACK *) message;
3096 t = tunnel_get (&msg->oid, msg->tid);
3099 /* TODO notify that we don't know the tunnel */
3103 /* Message for us? */
3104 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
3107 if (NULL == t->client)
3109 GNUNET_break_op (0);
3112 if (NULL != t->dht_get_type)
3114 GNUNET_DHT_get_stop (t->dht_get_type);
3115 t->dht_get_type = NULL;
3117 peer_info = peer_info_get (&msg->peer_id);
3118 tree_set_status(t->tree, peer_info->id, MESH_PEER_READY);
3119 send_client_peer_connected(t, peer_info->id);
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3124 "MESH: not for us, retransmitting...\n");
3125 GNUNET_PEER_resolve(tree_get_predecessor(t->tree), &id);
3126 peer_info = peer_info_get (&msg->oid);
3127 if (NULL == peer_info)
3129 /* If we know the tunnel, we should DEFINITELY know the peer */
3133 send_message (message, &id);
3139 * Functions to handle messages from core
3141 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3142 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3143 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3144 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3145 sizeof (struct GNUNET_MESH_PathBroken)},
3146 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3147 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3148 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3149 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3150 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3151 sizeof (struct GNUNET_MESH_PathACK)},
3157 /******************************************************************************/
3158 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3159 /******************************************************************************/
3162 * deregister_app: iterator for removing each application registered by a client
3164 * @param cls closure
3165 * @param key the hash of the application id (used to access the hashmap)
3166 * @param value the value stored at the key (client)
3168 * @return GNUNET_OK on success
3171 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3173 GNUNET_break (GNUNET_YES ==
3174 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
3180 * notify_client_connection_failure: notify a client that the connection to the
3181 * requested remote peer is not possible (for instance, no route found)
3182 * Function called when the socket is ready to queue more data. "buf" will be
3183 * NULL and "size" zero if the socket was closed for writing in the meantime.
3185 * @param cls closure
3186 * @param size number of bytes available in buf
3187 * @param buf where the callee should write the message
3188 * @return number of bytes written to buf
3191 notify_client_connection_failure (void *cls, size_t size, void *buf)
3194 struct MeshPeerInfo *peer_info;
3195 struct GNUNET_MESH_PeerControl *msg;
3196 struct GNUNET_PeerIdentity id;
3198 if (0 == size && NULL == buf)
3200 // TODO retry? cancel?
3204 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3205 peer_info = (struct MeshPeerInfo *) cls;
3206 msg = (struct GNUNET_MESH_PeerControl *) buf;
3207 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3208 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3209 // msg->tunnel_id = htonl(peer_info->t->tid);
3210 GNUNET_PEER_resolve (peer_info->id, &id);
3211 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3219 * Send keepalive packets for a peer
3221 * @param cls Closure (tunnel for which to send the keepalive).
3222 * @param tc Notification context.
3224 * TODO: implement explicit multicast keepalive?
3227 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3229 struct MeshTunnel *t = cls;
3230 struct GNUNET_MessageHeader *payload;
3231 struct GNUNET_MESH_Multicast *msg;
3232 size_t size = sizeof(struct GNUNET_MESH_Multicast) +
3233 sizeof(struct GNUNET_MessageHeader);
3236 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3240 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3243 "MESH: sending keepalive for tunnel %d\n",
3246 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3247 msg->header.size = htons (size);
3248 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3249 msg->oid = my_full_id;
3250 msg->tid = htonl(t->id.tid);
3251 payload = (struct GNUNET_MessageHeader *) &msg[1];
3252 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
3253 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3254 tunnel_send_multicast (t, &msg->header);
3256 t->path_refresh_task =
3257 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3263 * Function to process paths received for a new peer addition. The recorded
3264 * paths form the initial tunnel, which can be optimized later.
3265 * Called on each result obtained for the DHT search.
3267 * @param cls closure
3268 * @param exp when will this value expire
3269 * @param key key of the result
3270 * @param type type of the result
3271 * @param size number of bytes in data
3272 * @param data pointer to the result data
3274 * TODO: re-issue the request after certain time? cancel after X results?
3277 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3278 const GNUNET_HashCode * key,
3279 const struct GNUNET_PeerIdentity *get_path,
3280 unsigned int get_path_length,
3281 const struct GNUNET_PeerIdentity *put_path,
3282 unsigned int put_path_length,
3283 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
3285 struct MeshPathInfo *path_info = cls;
3286 struct MeshPeerPath *p;
3287 struct GNUNET_PeerIdentity pi;
3290 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3291 "MESH: Got results from DHT!\n");
3292 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3293 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3296 // GNUNET_DHT_get_stop(path_info->peer->dhtget);
3297 // path_info->peer->dhtget = NULL;
3299 p = path_build_from_dht (get_path, get_path_length,
3300 put_path, put_path_length);
3301 peer_info_add_path (path_info->peer, p, GNUNET_NO);
3302 for (i = 0; i < path_info->peer->ntunnels; i++)
3304 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3305 peer_info_connect(path_info->peer, path_info->t);
3307 // GNUNET_free (path_info);
3314 * Function to process paths received for a new peer addition. The recorded
3315 * paths form the initial tunnel, which can be optimized later.
3316 * Called on each result obtained for the DHT search.
3318 * @param cls closure
3319 * @param exp when will this value expire
3320 * @param key key of the result
3321 * @param type type of the result
3322 * @param size number of bytes in data
3323 * @param data pointer to the result data
3326 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3327 const GNUNET_HashCode * key,
3328 const struct GNUNET_PeerIdentity *get_path,
3329 unsigned int get_path_length,
3330 const struct GNUNET_PeerIdentity *put_path,
3331 unsigned int put_path_length,
3332 enum GNUNET_BLOCK_Type type, size_t size,
3335 const struct GNUNET_PeerIdentity *pi = data;
3336 struct MeshTunnel *t = cls;
3337 struct MeshPeerInfo *peer_info;
3338 struct MeshPeerPath *p;
3340 if (size != sizeof (struct GNUNET_PeerIdentity))
3342 GNUNET_break_op (0);
3345 GNUNET_assert (NULL != t->client);
3346 peer_info = peer_info_get (pi);
3347 (void) GNUNET_CONTAINER_multihashmap_put (
3351 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3353 p = path_build_from_dht (get_path, get_path_length,
3354 put_path, put_path_length);
3355 peer_info_add_path (peer_info, p, GNUNET_NO);
3356 tunnel_add_peer(t, peer_info);
3357 peer_info_connect (peer_info, t);
3361 /******************************************************************************/
3362 /********************* MESH LOCAL HANDLES **************************/
3363 /******************************************************************************/
3367 * Handler for client disconnection
3369 * @param cls closure
3370 * @param client identification of the client; NULL
3371 * for the last call when the server is destroyed
3374 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3376 struct MeshClient *c;
3377 struct MeshClient *next;
3379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3388 if (c->handle != client && NULL != client)
3390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
3395 GNUNET_SERVER_client_drop (c->handle);
3396 if (NULL != c->tunnels)
3398 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3399 &tunnel_destroy_iterator,
3401 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3404 /* deregister clients applications */
3405 if (NULL != c->apps)
3407 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3408 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3410 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3411 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3413 GNUNET_SCHEDULER_cancel (announce_applications_task);
3414 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3416 if (NULL != c->types)
3417 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3419 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT FREE at %p\n", c);
3425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3431 * Handler for new clients
3433 * @param cls closure
3434 * @param client identification of the client
3435 * @param message the actual message, which includes messages the client wants
3438 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3439 const struct GNUNET_MessageHeader *message)
3441 struct GNUNET_MESH_ClientConnect *cc_msg;
3442 struct MeshClient *c;
3443 GNUNET_MESH_ApplicationType *a;
3450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3451 /* Check data sanity */
3452 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3453 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3454 ntypes = ntohs (cc_msg->types);
3455 napps = ntohs (cc_msg->applications);
3457 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3460 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3464 /* Create new client structure */
3465 c = GNUNET_malloc (sizeof (struct MeshClient));
3467 c->id = next_client_id++;
3469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT NEW %u at %p\n", c->id, c);
3471 GNUNET_SERVER_client_keep (client);
3472 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3475 GNUNET_MESH_ApplicationType at;
3478 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3479 for (i = 0; i < napps; i++)
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3483 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3484 /* store in clients hashmap */
3485 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3486 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3487 /* store in global hashmap, for announcements */
3488 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3489 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3491 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3492 announce_applications_task =
3493 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3501 t = (uint16_t *) & a[napps];
3502 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3503 for (i = 0; i < ntypes; i++)
3506 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3508 /* store in clients hashmap */
3509 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3510 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3511 /* store in global hashmap */
3512 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3513 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3517 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3519 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3520 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3521 GNUNET_SERVER_notification_context_add (nc, client);
3523 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3531 * Handler for requests of new tunnels
3533 * @param cls closure
3534 * @param client identification of the client
3535 * @param message the actual message
3538 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3539 const struct GNUNET_MessageHeader *message)
3541 struct GNUNET_MESH_TunnelMessage *t_msg;
3542 struct MeshTunnel *t;
3543 struct MeshClient *c;
3544 GNUNET_HashCode hash;
3546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3548 /* Sanity check for client registration */
3549 if (NULL == (c = client_get (client)))
3552 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3559 /* Message sanity check */
3560 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3563 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3567 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3568 /* Sanity check for tunnel numbering */
3569 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3572 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3575 /* Sanity check for duplicate tunnel IDs */
3576 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3579 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3583 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3584 while (NULL != tunnel_get_by_pi (myid, next_tid))
3585 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3586 t->id.tid = next_tid++;
3587 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3589 t->local_tid = ntohl (t_msg->tunnel_id);
3591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3592 "MESH: CREATED TUNNEL %s [%x] (%x)\n",
3593 GNUNET_i2s (&my_full_id),
3598 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3600 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3602 GNUNET_CONTAINER_multihashmap_put (
3603 c->tunnels, &hash, t,
3604 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3607 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3611 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3613 GNUNET_CONTAINER_multihashmap_put (
3615 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3618 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3621 t->tree = tree_new (myid);
3622 tree_set_me(t->tree, myid);
3624 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3630 * Handler for requests of deleting tunnels
3632 * @param cls closure
3633 * @param client identification of the client
3634 * @param message the actual message
3637 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3638 const struct GNUNET_MessageHeader *message)
3640 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3641 struct MeshClient *c;
3642 struct MeshTunnel *t;
3643 MESH_TunnelNumber tid;
3644 GNUNET_HashCode hash;
3646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3647 "MESH: Got a DESTROY TUNNEL from client!\n");
3649 /* Sanity check for client registration */
3650 if (NULL == (c = client_get (client)))
3653 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3656 /* Message sanity check */
3657 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3660 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3666 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3668 /* Retrieve tunnel */
3669 tid = ntohl (tunnel_msg->tunnel_id);
3671 /* Remove from local id hashmap */
3672 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3673 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3674 GNUNET_assert (GNUNET_YES ==
3675 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t));
3678 tunnel_send_destroy (t);
3680 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3686 * Handler for connection requests to new peers
3688 * @param cls closure
3689 * @param client identification of the client
3690 * @param message the actual message (PeerControl)
3693 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3694 const struct GNUNET_MessageHeader *message)
3696 struct GNUNET_MESH_PeerControl *peer_msg;
3697 struct MeshPeerInfo *peer_info;
3698 struct MeshClient *c;
3699 struct MeshTunnel *t;
3700 MESH_TunnelNumber tid;
3702 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3703 /* Sanity check for client registration */
3704 if (NULL == (c = client_get (client)))
3707 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3711 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3712 /* Sanity check for message size */
3713 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3716 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3720 /* Tunnel exists? */
3721 tid = ntohl (peer_msg->tunnel_id);
3722 t = tunnel_get_by_local_id (c, tid);
3726 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3730 /* Does client own tunnel? */
3731 if (t->client->handle != client)
3734 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3737 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3738 GNUNET_i2s(&peer_msg->peer));
3739 peer_info = peer_info_get (&peer_msg->peer);
3741 tunnel_add_peer(t, peer_info);
3742 peer_info_connect(peer_info, t);
3744 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3750 * Handler for disconnection requests of peers in a tunnel
3752 * @param cls closure
3753 * @param client identification of the client
3754 * @param message the actual message (PeerControl)
3757 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3758 const struct GNUNET_MessageHeader *message)
3760 struct GNUNET_MESH_PeerControl *peer_msg;
3761 struct MeshPeerInfo *peer_info;
3762 struct MeshClient *c;
3763 struct MeshTunnel *t;
3764 MESH_TunnelNumber tid;
3766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got a PEER DEL request\n");
3767 /* Sanity check for client registration */
3768 if (NULL == (c = client_get (client)))
3771 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3774 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3775 /* Sanity check for message size */
3776 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3779 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3783 /* Tunnel exists? */
3784 tid = ntohl (peer_msg->tunnel_id);
3785 t = tunnel_get_by_local_id (c, tid);
3789 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: on tunnel %X\n", t->id.tid);
3794 /* Does client own tunnel? */
3795 if (t->client->handle != client)
3798 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3803 "MESH: for peer %s\n",
3804 GNUNET_i2s(&peer_msg->peer));
3805 /* Is the peer in the tunnel? */
3806 peer_info = GNUNET_CONTAINER_multihashmap_get(t->peers,
3807 &peer_msg->peer.hashPubKey);
3808 if (NULL == peer_info)
3811 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3815 /* Ok, delete peer from tunnel */
3816 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3817 &peer_msg->peer.hashPubKey);
3819 send_destroy_path (t, peer_info->id);
3820 tunnel_delete_peer(t, peer_info->id);
3821 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3827 * Handler for connection requests to new peers by type
3829 * @param cls closure
3830 * @param client identification of the client
3831 * @param message the actual message (ConnectPeerByType)
3834 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3835 const struct GNUNET_MessageHeader *message)
3837 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3838 struct MeshClient *c;
3839 struct MeshTunnel *t;
3840 GNUNET_HashCode hash;
3841 MESH_TunnelNumber tid;
3843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3844 /* Sanity check for client registration */
3845 if (NULL == (c = client_get (client)))
3848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3852 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3853 /* Sanity check for message size */
3854 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3855 ntohs (connect_msg->header.size))
3858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3862 /* Tunnel exists? */
3863 tid = ntohl (connect_msg->tunnel_id);
3864 t = tunnel_get_by_local_id (c, tid);
3868 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3872 /* Does client own tunnel? */
3873 if (t->client->handle != client)
3876 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3880 /* Do WE have the service? */
3881 t->type = ntohl (connect_msg->type);
3882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3883 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3884 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3887 /* Yes! Fast forward, add ourselves to the tunnel and send the
3888 * good news to the client
3890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3891 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3892 peer_info_get (&my_full_id),
3893 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3896 send_client_peer_connected(t, myid);
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3898 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3901 /* Ok, lets find a peer offering the service */
3902 if (NULL != t->dht_get_type)
3904 GNUNET_DHT_get_stop (t->dht_get_type);
3906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3907 GNUNET_h2s (&hash));
3909 GNUNET_DHT_get_start (dht_handle,
3910 GNUNET_TIME_UNIT_FOREVER_REL,
3911 GNUNET_BLOCK_TYPE_TEST,
3914 GNUNET_DHT_RO_RECORD_ROUTE |
3915 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3917 &dht_get_type_handler, t);
3919 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3925 * Handler for client traffic directed to one peer
3927 * @param cls closure
3928 * @param client identification of the client
3929 * @param message the actual message
3932 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3933 const struct GNUNET_MessageHeader *message)
3935 struct MeshClient *c;
3936 struct MeshTunnel *t;
3937 struct MeshPeerInfo *pi;
3938 struct GNUNET_MESH_Unicast *data_msg;
3939 MESH_TunnelNumber tid;
3942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3943 "MESH: Got a unicast request from a client!\n");
3945 /* Sanity check for client registration */
3946 if (NULL == (c = client_get (client)))
3949 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3952 data_msg = (struct GNUNET_MESH_Unicast *) message;
3953 /* Sanity check for message size */
3954 size = ntohs (message->size);
3955 if (sizeof (struct GNUNET_MESH_Unicast) +
3956 sizeof (struct GNUNET_MessageHeader) > size)
3959 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3963 /* Tunnel exists? */
3964 tid = ntohl (data_msg->tid);
3965 t = tunnel_get_by_local_id (c, tid);
3969 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3973 /* Is it a local tunnel? Then, does client own the tunnel? */
3974 if (NULL != t->client &&
3975 NULL != t->client->handle &&
3976 t->client->handle != client)
3979 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3983 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3984 &data_msg->destination.hashPubKey);
3985 /* Is the selected peer in the tunnel? */
3989 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3993 /* Ok, everything is correct, send the message
3994 * (pretend we got it from a mesh peer)
3997 char buf[ntohs (message->size)];
3998 struct GNUNET_MESH_Unicast *copy;
4000 /* Work around const limitation */
4001 copy = (struct GNUNET_MESH_Unicast *) buf;
4002 memcpy (buf, data_msg, size);
4003 copy->oid = my_full_id;
4004 copy->tid = htonl (t->id.tid);
4005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4006 "MESH: calling generic handler...\n");
4007 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4009 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4015 * Handler for client traffic directed to the origin
4017 * @param cls closure
4018 * @param client identification of the client
4019 * @param message the actual message
4022 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4023 const struct GNUNET_MessageHeader *message)
4025 struct GNUNET_MESH_ToOrigin *data_msg;
4026 struct GNUNET_PeerIdentity id;
4027 struct MeshClient *c;
4028 struct MeshTunnel *t;
4029 MESH_TunnelNumber tid;
4032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4033 "MESH: Got a ToOrigin request from a client!\n");
4035 /* Sanity check for client registration */
4036 if (NULL == (c = client_get (client)))
4039 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4042 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4043 /* Sanity check for message size */
4044 size = ntohs (message->size);
4045 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4046 sizeof (struct GNUNET_MessageHeader) > size)
4049 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4053 /* Tunnel exists? */
4054 tid = ntohl (data_msg->tid);
4055 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4058 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4061 t = tunnel_get_by_local_id (c, tid);
4065 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4069 /* It shouldn't be a local tunnel. */
4070 if (NULL != t->client)
4073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4076 GNUNET_PEER_resolve(t->id.oid, &id);
4078 /* Ok, everything is correct, send the message
4079 * (pretend we got it from a mesh peer)
4082 char buf[ntohs (message->size)];
4083 struct GNUNET_MESH_ToOrigin *copy;
4085 /* Work around const limitation */
4086 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4087 memcpy (buf, data_msg, size);
4089 copy->tid = htonl (t->id.tid);
4090 copy->sender = my_full_id;
4091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4092 "MESH: calling generic handler...\n");
4093 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4095 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4101 * Handler for client traffic directed to all peers in a tunnel
4103 * @param cls closure
4104 * @param client identification of the client
4105 * @param message the actual message
4108 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4109 const struct GNUNET_MessageHeader *message)
4111 struct MeshClient *c;
4112 struct MeshTunnel *t;
4113 struct GNUNET_MESH_Multicast *data_msg;
4114 MESH_TunnelNumber tid;
4116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4117 "MESH: Got a multicast request from a client!\n");
4119 /* Sanity check for client registration */
4120 if (NULL == (c = client_get (client)))
4123 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4126 data_msg = (struct GNUNET_MESH_Multicast *) message;
4127 /* Sanity check for message size */
4128 if (sizeof (struct GNUNET_MESH_Multicast) +
4129 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4132 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4136 /* Tunnel exists? */
4137 tid = ntohl (data_msg->tid);
4138 t = tunnel_get_by_local_id (c, tid);
4142 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4146 /* Does client own tunnel? */
4147 if (t->client->handle != client)
4150 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4155 char buf[ntohs(message->size)];
4156 struct GNUNET_MESH_Multicast *copy;
4158 copy = (struct GNUNET_MESH_Multicast *)buf;
4159 memcpy(buf, message, ntohs(message->size));
4160 copy->oid = my_full_id;
4161 copy->tid = htonl(t->id.tid);
4162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4163 "MESH: calling generic handler...\n");
4164 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL, 0);
4167 /* receive done gets called when last copy is sent to a neighbor */
4172 * Functions to handle messages from clients
4174 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4175 {&handle_local_new_client, NULL,
4176 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4177 {&handle_local_tunnel_create, NULL,
4178 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4179 sizeof (struct GNUNET_MESH_TunnelMessage)},
4180 {&handle_local_tunnel_destroy, NULL,
4181 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4182 sizeof (struct GNUNET_MESH_TunnelMessage)},
4183 {&handle_local_connect_add, NULL,
4184 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4185 sizeof (struct GNUNET_MESH_PeerControl)},
4186 {&handle_local_connect_del, NULL,
4187 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4188 sizeof (struct GNUNET_MESH_PeerControl)},
4189 {&handle_local_connect_by_type, NULL,
4190 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4191 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4192 {&handle_local_unicast, NULL,
4193 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4194 {&handle_local_to_origin, NULL,
4195 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4196 {&handle_local_multicast, NULL,
4197 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4203 * To be called on core init/fail.
4205 * @param cls service closure
4206 * @param server handle to the server for this service
4207 * @param identity the public identity of this peer
4210 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4211 const struct GNUNET_PeerIdentity *identity)
4213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
4214 core_handle = server;
4215 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
4217 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
4218 GNUNET_SCHEDULER_shutdown();
4224 * Method called whenever a given peer connects.
4226 * @param cls closure
4227 * @param peer peer identity this notification is about
4228 * @param atsi performance data for the connection
4229 * @param atsi_count number of records in 'atsi'
4232 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4233 const struct GNUNET_ATS_Information *atsi,
4234 unsigned int atsi_count)
4236 struct MeshPeerInfo *peer_info;
4237 struct MeshPeerPath *path;
4239 #if MESH_DEBUG_CONNECTION
4240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
4241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4242 GNUNET_i2s(&my_full_id));
4244 peer_info = peer_info_get (peer);
4245 if (myid == peer_info->id)
4247 #if MESH_DEBUG_CONNECTION
4248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4252 #if MESH_DEBUG_CONNECTION
4255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4259 path = path_new (2);
4260 path->peers[0] = myid;
4261 path->peers[1] = peer_info->id;
4262 GNUNET_PEER_change_rc(myid, 1);
4263 GNUNET_PEER_change_rc(peer_info->id, 1);
4264 peer_info_add_path (peer_info, path, GNUNET_YES);
4269 * Method called whenever a peer disconnects.
4271 * @param cls closure
4272 * @param peer peer identity this notification is about
4275 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4277 struct MeshPeerInfo *pi;
4280 #if MESH_DEBUG_CONNECTION
4281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
4283 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4289 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4291 /* TODO: notify that the transmission failed */
4292 peer_info_cancel_transmission(pi, i);
4294 peer_info_remove_path (pi, pi->id, myid);
4295 #if MESH_DEBUG_CONNECTION
4298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4305 /******************************************************************************/
4306 /************************ MAIN FUNCTIONS ****************************/
4307 /******************************************************************************/
4310 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4312 * @param cls closure
4313 * @param key current key code
4314 * @param value value in the hash map
4315 * @return GNUNET_YES if we should continue to iterate,
4319 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4321 struct MeshTunnel *t = value;
4327 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4329 * @param cls closure
4330 * @param key current key code
4331 * @param value value in the hash map
4332 * @return GNUNET_YES if we should continue to iterate,
4336 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4338 struct MeshPeerInfo *p = value;
4339 peer_info_destroy (p);
4344 * Task run during shutdown.
4350 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4353 /* TODO: destroy tunnels? */
4354 if (core_handle != NULL)
4356 GNUNET_CORE_disconnect (core_handle);
4359 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
4360 GNUNET_CONTAINER_multihashmap_iterate(peers, &shutdown_peer, NULL);
4361 if (dht_handle != NULL)
4363 GNUNET_DHT_disconnect (dht_handle);
4368 GNUNET_SERVER_notification_context_destroy (nc);
4371 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4373 GNUNET_SCHEDULER_cancel (announce_id_task);
4374 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4380 * Process mesh requests.
4382 * @param cls closure
4383 * @param server the initialized server
4384 * @param c configuration to use
4387 run (void *cls, struct GNUNET_SERVER_Handle *server,
4388 const struct GNUNET_CONFIGURATION_Handle *c)
4390 struct MeshPeerInfo *peer;
4391 struct MeshPeerPath *p;
4394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4395 server_handle = server;
4396 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4397 CORE_QUEUE_SIZE, /* queue size */
4398 NULL, /* Closure passed to MESH functions */
4399 &core_init, /* Call core_init once connected */
4400 &core_connect, /* Handle connects */
4401 &core_disconnect, /* remove peers on disconnects */
4402 NULL, /* Don't notify about all incoming messages */
4403 GNUNET_NO, /* For header only in notification */
4404 NULL, /* Don't notify about all outbound messages */
4405 GNUNET_NO, /* For header-only out notification */
4406 core_handlers); /* Register these handlers */
4408 if (core_handle == NULL)
4411 GNUNET_SCHEDULER_shutdown ();
4416 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4421 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4422 GNUNET_SCHEDULER_shutdown ();
4425 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4426 GNUNET_free (keyfile);
4427 if (my_private_key == NULL)
4429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4430 _("Mesh service could not access hostkey. Exiting.\n"));
4431 GNUNET_SCHEDULER_shutdown ();
4434 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4435 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4436 &my_full_id.hashPubKey);
4437 myid = GNUNET_PEER_intern (&my_full_id);
4439 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4446 dht_handle = GNUNET_DHT_connect (c, 64);
4447 if (dht_handle == NULL)
4453 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4455 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4456 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4457 peers = GNUNET_CONTAINER_multihashmap_create (32);
4458 applications = GNUNET_CONTAINER_multihashmap_create (32);
4459 types = GNUNET_CONTAINER_multihashmap_create (32);
4461 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4462 nc = GNUNET_SERVER_notification_context_create (server_handle,
4464 GNUNET_SERVER_disconnect_notify (server_handle,
4465 &handle_local_client_disconnect,
4470 clients_tail = NULL;
4475 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4476 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4478 /* Create a peer_info for the local peer */
4479 peer = peer_info_get(&my_full_id);
4482 GNUNET_PEER_change_rc (myid, 1);
4483 peer_info_add_path(peer, p, GNUNET_YES);
4485 /* Scheduled the task to clean up when shutdown is called */
4486 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4493 * The main function for the mesh service.
4495 * @param argc number of arguments from the command line
4496 * @param argv command line arguments
4497 * @return 0 ok, 1 on error
4500 main (int argc, char *const *argv)
4504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4507 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");