2 This file is part of GNUnet.
3 (C) 2001-2012 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?
41 * - relay corking down to core
42 * - set ttl relative to tree depth
43 * - Add data ACK count in path ACK
44 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
50 #include "mesh2_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "gnunet_dht_service.h"
54 #include "gnunet_statistics_service.h"
56 #define MESH_BLOOM_SIZE 128
58 #define MESH_DEBUG_DHT GNUNET_NO
59 #define MESH_DEBUG_CONNECTION GNUNET_NO
60 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
62 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
63 GNUNET_TIME_UNIT_MINUTES,\
66 #if MESH_DEBUG_CONNECTION
67 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
69 #define DEBUG_CONN(...)
73 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
75 #define DEBUG_DHT(...)
82 struct timespec __mesh_start;
83 struct timespec __mesh_end;
84 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
85 #define INTERVAL_END \
87 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
88 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
89 if (__diff < 0) __diff += 1000000000;\
93 #define INTERVAL_SHOW \
95 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
97 #define INTERVAL_START
102 /******************************************************************************/
103 /************************ DATA STRUCTURES ****************************/
104 /******************************************************************************/
106 /** FWD declaration */
112 * Struct representing a piece of data being sent to other peers
116 /** Tunnel it belongs to. */
117 struct MeshTunnel *t;
119 /** How many remaining neighbors still hav't got it. */
120 unsigned int reference_counter;
122 /** How many remaining neighbors we need to send this to. */
123 unsigned int total_out;
125 /** Size of the data. */
134 * Struct containing info about a queued transmission to this peer
141 struct MeshPeerQueue *next;
146 struct MeshPeerQueue *prev;
149 * Peer this transmission is directed to.
151 struct MeshPeerInfo *peer;
154 * Tunnel this message belongs to.
156 struct MeshTunnel *tunnel;
159 * Pointer to info stucture used as cls.
169 * Size of the message
176 * Struct containing all info possibly needed to build a package when called
179 struct MeshTransmissionDescriptor
181 /** ID of the tunnel this packet travels in */
182 struct MESH_TunnelID *origin;
184 /** Who was this message being sent to */
185 struct MeshPeerInfo *peer;
187 /** Ultimate destination of the packet */
188 GNUNET_PEER_Id destination;
190 /** Data descriptor */
191 struct MeshData* mesh_data;
196 * Struct containing all information regarding a given peer
206 * Last time we heard from this peer
208 struct GNUNET_TIME_Absolute last_contact;
211 * Task handler for delayed connect task;
213 GNUNET_SCHEDULER_TaskIdentifier connect_task;
216 * Number of attempts to reconnect so far
218 int n_reconnect_attempts;
221 * Paths to reach the peer, ordered by ascending hop count
223 struct MeshPeerPath *path_head;
226 * Paths to reach the peer, ordered by ascending hop count
228 struct MeshPeerPath *path_tail;
231 * Handle to stop the DHT search for a path to this peer
233 struct GNUNET_DHT_GetHandle *dhtget;
236 * Closure given to the DHT GET
238 struct MeshPathInfo *dhtgetcls;
241 * Array of tunnels this peer participates in
242 * (most probably a small amount, therefore not a hashmap)
243 * When the path to the peer changes, notify these tunnels to let them
244 * re-adjust their path trees.
246 struct MeshTunnel **tunnels;
249 * Number of tunnels this peers participates in
251 unsigned int ntunnels;
254 * Transmission queue to core DLL head
256 struct MeshPeerQueue *queue_head;
259 * Transmission queue to core DLL tail
261 struct MeshPeerQueue *queue_tail;
264 * How many messages are in the queue to this peer.
266 unsigned int queue_n;
269 * Handle to for queued transmissions
271 struct GNUNET_CORE_TransmitHandle *core_transmit;
276 * Globally unique tunnel identification (owner + number)
277 * DO NOT USE OVER THE NETWORK
282 * Node that owns the tunnel
287 * Tunnel number to differentiate all the tunnels owned by the node oid
288 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
290 MESH_TunnelNumber tid;
295 * Struct containing all information regarding a tunnel
296 * For an intermediate node the improtant info used will be:
297 * - id Tunnel unique identification
298 * - paths[0] To know where to send it next
299 * - metainfo: ready, speeds, accounting
306 struct MESH_TunnelID id;
309 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
311 MESH_TunnelNumber local_tid;
314 * Local tunnel number for local destination clients (incoming number)
315 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
318 MESH_TunnelNumber local_tid_dest;
321 * Is the tunnel bufferless (minimum latency)?
326 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
331 * Packet ID of the last bck packet sent (unique counter per hop).
336 * Force sending ACK? Flag to allow duplicate ACK on POLL.
341 * Last ACK sent towards the origin (for traffic towards leaf node).
343 uint32_t last_fwd_ack;
346 * BCK ACK value received from the hop towards the owner of the tunnel,
347 * (previous node / owner): up to what message PID can we sent back to him.
352 * How many messages are in the forward queue (towards leaves).
354 unsigned int fwd_queue_n;
357 * How many messages do we accept in the forward queue.
359 unsigned int fwd_queue_max;
362 * How many messages are in the backward queue (towards origin).
364 unsigned int bck_queue_n;
367 * How many messages do we accept in the backward queue.
369 unsigned int bck_queue_max;
372 * Task to poll peer in case of a stall.
374 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
377 * Last time the tunnel was used
379 struct GNUNET_TIME_Absolute timestamp;
382 * Peer of the tunnel.
387 * Next hop in the tunnel.
389 GNUNET_PEER_Id next_hop;
392 * Previous hop in the tunnel.
394 GNUNET_PEER_Id prev_hop;
397 * Client owner of the tunnel, if any
399 struct MeshClient *owner;
402 * Clients that have been informed about and want to stay in the tunnel.
404 struct MeshClient **clients;
407 * Flow control info for each client.
409 struct MeshTunnelClientInfo *clients_fc;
412 * Number of elements in clients/clients_fc
414 unsigned int nclients;
417 * Clients that have been informed but requested to leave the tunnel.
419 struct MeshClient **ignore;
422 * Number of elements in clients
424 unsigned int nignore;
427 * Path being used for the tunnel.
429 struct MeshPeerPath *path;
432 * Task to keep the used paths alive
434 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
437 * Task to destroy the tunnel after timeout
439 * FIXME: merge the two? a tunnel will have either
440 * a path refresh OR a timeout, never both!
442 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
445 * Flag to signal the destruction of the tunnel.
446 * If this is set GNUNET_YES the tunnel will be destroyed
447 * when the queue is empty.
452 * Total messages pending for this tunnels, payload or not.
454 unsigned int pending_messages;
457 * If the tunnel is empty, destoy it.
459 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
464 * Info about a leaf client of a tunnel, needed to perform flow control.
466 struct MeshTunnelClientInfo
469 * PID of the last packet sent to the client (FWD).
474 * PID of the last packet received from the client (BCK).
479 * Maximum PID allowed (FWD ACK received).
484 * Last ACK sent to that child (BCK ACK).
491 * Info needed to work with tunnel paths and peers
498 struct MeshTunnel *t;
501 * Neighbouring peer to whom we send the packet to
503 struct MeshPeerInfo *peer;
508 struct MeshPeerPath *path;
513 * Struct containing information about a client of the service
520 struct MeshClient *next;
525 struct MeshClient *prev;
528 * Tunnels that belong to this client, indexed by local id
530 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
533 * Tunnels this client has accepted, indexed by incoming local id
535 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
538 * Tunnels this client has rejected, indexed by incoming local id
540 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
542 * Handle to communicate with the client
544 struct GNUNET_SERVER_Client *handle;
547 * Messages that this client has declared interest in
549 struct GNUNET_CONTAINER_MultiHashMap *types;
552 * Whether the client is active or shutting down (don't send confirmations
553 * to a client that is shutting down.
558 * ID of the client, mainly for debug messages
565 /******************************************************************************/
566 /************************ DEBUG FUNCTIONS ****************************/
567 /******************************************************************************/
571 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
572 * @param cls string to print
573 * @param success GNUNET_OK if the PUT was transmitted,
574 * GNUNET_NO on timeout,
575 * GNUNET_SYSERR on disconnect from service
576 * after the PUT message was transmitted
577 * (so we don't know if it was received or not)
582 mesh_debug (void *cls, int success)
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
590 unsigned int debug_fwd_ack;
591 unsigned int debug_bck_ack;
595 /******************************************************************************/
596 /*********************** GLOBAL VARIABLES ****************************/
597 /******************************************************************************/
599 /************************** Configuration parameters **************************/
602 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
604 static struct GNUNET_TIME_Relative refresh_path_time;
607 * How often to PUT own ID in the DHT.
609 static struct GNUNET_TIME_Relative id_announce_time;
612 * Maximum time allowed to connect to a peer found by string.
614 static struct GNUNET_TIME_Relative connect_timeout;
617 * Default TTL for payload packets.
619 static unsigned long long default_ttl;
622 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
624 static unsigned long long dht_replication_level;
627 * How many tunnels are we willing to maintain.
628 * Local tunnels are always allowed, even if there are more tunnels than max.
630 static unsigned long long max_tunnels;
633 * How many messages *in total* are we willing to queue, divided by number of
634 * tunnels to get tunnel queue size.
636 static unsigned long long max_msgs_queue;
639 * How many peers do we want to remember?
641 static unsigned long long max_peers;
644 /*************************** Static global variables **************************/
647 * Hostkey generation context
649 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
652 * DLL with all the clients, head.
654 static struct MeshClient *clients_head;
657 * DLL with all the clients, tail.
659 static struct MeshClient *clients_tail;
662 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
664 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
667 * Number of tunnels known.
669 static unsigned long long n_tunnels;
672 * Tunnels incoming, indexed by MESH_TunnelNumber
673 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
675 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
678 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
680 static struct GNUNET_CONTAINER_MultiHashMap *peers;
683 * Handle to communicate with transport
685 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
688 * Handle to communicate with core.
690 static struct GNUNET_CORE_Handle *core_handle;
695 static struct GNUNET_DHT_Handle *dht_handle;
700 static struct GNUNET_SERVER_Handle *server_handle;
703 * Handle to the statistics service.
705 static struct GNUNET_STATISTICS_Handle *stats;
708 * Notification context, to send messages to local clients.
710 static struct GNUNET_SERVER_NotificationContext *nc;
713 * Local peer own ID (memory efficient handle).
715 static GNUNET_PEER_Id myid;
718 * Local peer own ID (full value).
720 static struct GNUNET_PeerIdentity my_full_id;
725 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
730 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
733 * Tunnel ID for the next created tunnel (global tunnel number).
735 static MESH_TunnelNumber next_tid;
738 * Tunnel ID for the next incoming tunnel (local tunnel number).
740 static MESH_TunnelNumber next_local_tid;
743 * All message types clients of this peer are interested in.
745 static struct GNUNET_CONTAINER_MultiHashMap *types;
748 * Task to periodically announce itself in the network.
750 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
753 * Next ID to assign to a client.
755 unsigned int next_client_id;
758 /******************************************************************************/
759 /*********************** DECLARATIONS **************************/
760 /******************************************************************************/
763 * Function to process paths received for a new peer addition. The recorded
764 * paths form the initial tunnel, which can be optimized later.
765 * Called on each result obtained for the DHT search.
768 * @param exp when will this value expire
769 * @param key key of the result
770 * @param type type of the result
771 * @param size number of bytes in data
772 * @param data pointer to the result data
775 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
776 const struct GNUNET_HashCode * key,
777 const struct GNUNET_PeerIdentity *get_path,
778 unsigned int get_path_length,
779 const struct GNUNET_PeerIdentity *put_path,
780 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
781 size_t size, const void *data);
785 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
786 * and insert it in the appropiate structures if the peer is not known yet.
788 * @param peer Full identity of the peer.
790 * @return Existing or newly created peer info.
792 static struct MeshPeerInfo *
793 peer_get (const struct GNUNET_PeerIdentity *peer);
797 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
798 * and insert it in the appropiate structures if the peer is not known yet.
800 * @param peer Short identity of the peer.
802 * @return Existing or newly created peer info.
804 static struct MeshPeerInfo *
805 peer_get_short (const GNUNET_PEER_Id peer);
809 * Build a PeerPath from the paths returned from the DHT, reversing the paths
810 * to obtain a local peer -> destination path and interning the peer ids.
812 * @return Newly allocated and created path
814 static struct MeshPeerPath *
815 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
816 unsigned int get_path_length,
817 const struct GNUNET_PeerIdentity *put_path,
818 unsigned int put_path_length);
822 * Adds a path to the peer_infos of all the peers in the path
824 * @param p Path to process.
825 * @param confirmed Whether we know if the path works or not.
828 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
833 * Search for a tunnel by global ID using full PeerIdentities.
835 * @param oid owner of the tunnel.
836 * @param tid global tunnel number.
838 * @return tunnel handler, NULL if doesn't exist.
840 static struct MeshTunnel *
841 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
845 * Delete an active client from the tunnel.
851 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
854 * Notify a tunnel that a connection has broken that affects at least
857 * @param t Tunnel affected.
858 * @param p1 Peer that got disconnected from p2.
859 * @param p2 Peer that got disconnected from p1.
861 * @return Short ID of the peer disconnected (either p1 or p2).
862 * 0 if the tunnel remained unaffected.
864 static GNUNET_PEER_Id
865 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
870 * Get the current ack value for a tunnel, for data going from root to leaves,
871 * taking in account the tunnel mode and the status of all children and clients.
875 * @return Maximum PID allowed.
878 tunnel_get_fwd_ack (struct MeshTunnel *t);
882 * Add a client to a tunnel, initializing all needed data structures.
884 * @param t Tunnel to which add the client.
885 * @param c Client which to add to the tunnel.
888 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
892 * .Use the given path for the tunnel.
894 * @param t Tunnel to update.
895 * @param p Path to use.
898 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
901 * @brief Queue and pass message to core when possible.
903 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
904 * and accounts for it. In case the queue is full, the message is dropped and
907 * Otherwise, message is treated as internal and allowed to go regardless of
910 * @param cls Closure (@c type dependant). It will be used by queue_send to
911 * build the message to be sent if not already prebuilt.
912 * @param type Type of the message, 0 for a raw message.
913 * @param size Size of the message.
914 * @param dst Neighbor to send message to.
915 * @param t Tunnel this message belongs to.
918 queue_add (void *cls, uint16_t type, size_t size,
919 struct MeshPeerInfo *dst, struct MeshTunnel *t);
923 * Free a transmission that was already queued with all resources
924 * associated to the request.
926 * @param queue Queue handler to cancel.
927 * @param clear_cls Is it necessary to free associated cls?
930 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
934 * @brief Get the next transmittable message from the queue.
936 * This will be the head, except in the case of being a data packet
937 * not allowed by the destination peer.
939 * @param peer Destination peer.
941 * @return The next viable MeshPeerQueue element to send to that peer.
942 * NULL when there are no transmittable messages.
944 struct MeshPeerQueue *
945 queue_get_next (const struct MeshPeerInfo *peer);
949 * Core callback to write a queued packet to core buffer
951 * @param cls Closure (peer info).
952 * @param size Number of bytes available in buf.
953 * @param buf Where the to write the message.
955 * @return number of bytes written to buf
958 queue_send (void *cls, size_t size, void *buf);
961 /******************************************************************************/
962 /************************ PERIODIC FUNCTIONS ****************************/
963 /******************************************************************************/
966 * Periodically announce self id in the DHT
969 * @param tc task context
972 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
976 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
978 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
982 * - Set data expiration in function of X
985 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
987 block.id = my_full_id;
988 block.type = htonl (0);
989 GNUNET_DHT_put (dht_handle, /* DHT handle */
990 &my_full_id.hashPubKey, /* Key to use */
991 dht_replication_level, /* Replication level */
992 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
993 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
994 sizeof (block), /* Size of the data */
995 (const char *) &block, /* Data itself */
996 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
997 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
998 NULL, /* Continuation */
999 NULL); /* Continuation closure */
1001 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1005 /******************************************************************************/
1006 /****************** GENERAL HELPER FUNCTIONS ************************/
1007 /******************************************************************************/
1010 * Decrements the reference counter and frees all resources if needed
1012 * @param mesh_data Data Descriptor used in a multicast message.
1013 * Freed no longer needed (last message).
1016 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1018 if (0 == --(mesh_data->reference_counter))
1020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1021 GNUNET_free (mesh_data->data);
1022 GNUNET_free (mesh_data);
1028 * Check if client has registered with the service and has not disconnected
1030 * @param client the client to check
1032 * @return non-NULL if client exists in the global DLL
1034 static struct MeshClient *
1035 client_get (struct GNUNET_SERVER_Client *client)
1037 struct MeshClient *c;
1042 if (c->handle == client)
1051 * Checks if a given client has subscribed to certain message type
1053 * @param message_type Type of message to check
1054 * @param c Client to check
1056 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1058 * FIXME: use of crypto_hash slows it down
1059 * The hash function alone takes 8-10us out of the ~55us for the whole
1060 * process of retransmitting the message from one local client to another.
1061 * Find faster implementation!
1064 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1066 struct GNUNET_HashCode hc;
1068 if (NULL == c->types)
1071 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1072 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1077 * Check whether client wants traffic from a tunnel.
1079 * @param c Client to check.
1080 * @param t Tunnel to be found.
1082 * @return GNUNET_YES if client knows tunnel.
1084 * TODO look in client hashmap
1087 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1091 for (i = 0; i < t->nclients; i++)
1092 if (t->clients[i] == c)
1099 * Check whether client has been informed about a tunnel.
1101 * @param c Client to check.
1102 * @param t Tunnel to be found.
1104 * @return GNUNET_YES if client knows tunnel.
1106 * TODO look in client hashmap
1109 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1113 for (i = 0; i < t->nignore; i++)
1114 if (t->ignore[i] == c)
1116 return client_wants_tunnel(c, t);
1121 * Marks a client as uninterested in traffic from the tunnel, updating both
1122 * client and tunnel to reflect this.
1124 * @param c Client that doesn't want traffic anymore.
1125 * @param t Tunnel which should be ignored.
1127 * FIXME when to delete an incoming tunnel?
1130 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1132 struct GNUNET_HashCode hash;
1134 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1135 GNUNET_break (GNUNET_YES ==
1136 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1138 GNUNET_break (GNUNET_YES ==
1139 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1140 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1141 tunnel_delete_active_client (t, c);
1142 GNUNET_array_append (t->ignore, t->nignore, c);
1147 * Deletes a tunnel from a client (either owner or destination). To be used on
1148 * tunnel destroy, otherwise, use client_ignore_tunnel.
1150 * @param c Client whose tunnel to delete.
1151 * @param t Tunnel which should be deleted.
1154 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1156 struct GNUNET_HashCode hash;
1160 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1161 GNUNET_assert (GNUNET_YES ==
1162 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1168 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1170 GNUNET_assert (GNUNET_YES ==
1171 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1175 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1183 * Send the message to all clients that have subscribed to its type
1185 * @param msg Pointer to the message itself
1186 * @param payload Pointer to the payload of the message.
1187 * @param t The tunnel to whose clients this message goes.
1189 * @return number of clients this message was sent to
1192 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1193 const struct GNUNET_MessageHeader *payload,
1194 struct MeshTunnel *t)
1196 struct MeshClient *c;
1197 MESH_TunnelNumber *tid;
1200 char cbuf[htons (msg->size)];
1202 type = ntohs (payload->type);
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1205 GNUNET_MESH_DEBUG_M2S (type));
1207 memcpy (cbuf, msg, sizeof (cbuf));
1208 switch (htons (msg->type))
1210 struct GNUNET_MESH_Unicast *uc;
1211 struct GNUNET_MESH_ToOrigin *to;
1213 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1214 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1217 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1218 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1226 for (count = 0, c = clients_head; c != NULL; c = c->next)
1228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1229 if (client_is_subscribed (type, c))
1231 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1235 *tid = htonl (t->local_tid);
1239 if (GNUNET_NO == client_knows_tunnel (c, t))
1241 /* This client doesn't know the tunnel */
1242 struct GNUNET_MESH_TunnelNotification tmsg;
1243 struct GNUNET_HashCode hash;
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1246 tmsg.header.size = htons (sizeof (tmsg));
1247 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1248 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1249 tmsg.tunnel_id = htonl (t->local_tid_dest);
1251 if (GNUNET_YES == t->nobuffer)
1252 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
1253 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1254 &tmsg.header, GNUNET_NO);
1255 tunnel_add_client (t, c);
1256 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1258 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1259 c->incoming_tunnels, &hash, t,
1260 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1262 *tid = htonl (t->local_tid_dest);
1265 /* Check if the client wants to get traffic from the tunnel */
1266 if (GNUNET_NO == client_wants_tunnel(c, t))
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1270 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1271 (struct GNUNET_MessageHeader
1272 *) cbuf, GNUNET_NO);
1281 * Notify all clients (not depending on registration status) that the incoming
1282 * tunnel is no longer valid.
1284 * @param t Tunnel that was destroyed.
1287 send_clients_tunnel_destroy (struct MeshTunnel *t)
1289 struct GNUNET_MESH_TunnelMessage msg;
1291 msg.header.size = htons (sizeof (msg));
1292 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1293 msg.tunnel_id = htonl (t->local_tid_dest);
1294 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1299 * Notify clients of tunnel disconnections, if needed.
1300 * In case the origin disconnects, the destination clients get a tunnel destroy
1301 * notification. If the last destination disconnects (only one remaining client
1302 * in tunnel), the origin gets a (local ID) peer disconnected.
1303 * Note that the function must be called BEFORE removing the client from
1306 * @param t Tunnel that was destroyed.
1307 * @param c Client that disconnected.
1310 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1316 struct GNUNET_MESH_TunnelMessage msg;
1318 msg.header.size = htons (sizeof (msg));
1319 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1320 msg.tunnel_id = htonl (t->local_tid_dest);
1321 for (i = 0; i < t->nclients; i++)
1322 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1323 &msg.header, GNUNET_NO);
1325 // FIXME when to disconnect an incoming tunnel?
1330 * Iterator over all the peers to remove the oldest not-used entry.
1332 * @param cls Closure (unsued).
1333 * @param key ID of the peer.
1334 * @param value Peer_Info of the peer.
1339 peer_info_timeout (void *cls,
1340 const struct GNUNET_HashCode *key,
1347 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1348 * and insert it in the appropiate structures if the peer is not known yet.
1350 * @param peer Full identity of the peer.
1352 * @return Existing or newly created peer info.
1354 static struct MeshPeerInfo *
1355 peer_get (const struct GNUNET_PeerIdentity *peer)
1357 struct MeshPeerInfo *peer_info;
1359 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1360 if (NULL == peer_info)
1363 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1364 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1366 GNUNET_CONTAINER_multihashmap_iterate (peers,
1370 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1371 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1372 peer_info->id = GNUNET_PEER_intern (peer);
1374 peer_info->last_contact = GNUNET_TIME_absolute_get();
1381 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1382 * and insert it in the appropiate structures if the peer is not known yet.
1384 * @param peer Short identity of the peer.
1386 * @return Existing or newly created peer info.
1388 static struct MeshPeerInfo *
1389 peer_get_short (const GNUNET_PEER_Id peer)
1391 struct GNUNET_PeerIdentity id;
1393 GNUNET_PEER_resolve (peer, &id);
1394 return peer_get (&id);
1399 * Choose the best path towards a peer considering the tunnel properties.
1401 * @param peer The destination peer.
1402 * @param t The tunnel the path is for.
1404 * @return Best current known path towards the peer, if any.
1406 static struct MeshPeerPath *
1407 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1409 struct MeshPeerPath *best_p;
1410 struct MeshPeerPath *p;
1411 unsigned int best_cost;
1414 best_p = p = peer->path_head;
1415 best_cost = cost = p->length;
1418 if ((cost = p->length) < best_cost)
1430 * Iterator to remove the tunnel from the list of tunnels a peer participates
1433 * @param cls Closure (tunnel info)
1434 * @param key GNUNET_PeerIdentity of the peer (unused)
1435 * @param value PeerInfo of the peer
1437 * @return always GNUNET_YES, to keep iterating
1440 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1442 struct MeshTunnel *t = cls;
1443 struct MeshPeerInfo *peer = value;
1446 for (i = 0; i < peer->ntunnels; i++)
1449 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1452 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1454 GNUNET_realloc (peer->tunnels,
1455 peer->ntunnels * sizeof(struct MeshTunnel *));
1464 * Core callback to write a pre-constructed data packet to core buffer
1466 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1467 * @param size Number of bytes available in buf.
1468 * @param buf Where the to write the message.
1470 * @return number of bytes written to buf
1473 send_core_data_raw (void *cls, size_t size, void *buf)
1475 struct MeshTransmissionDescriptor *info = cls;
1476 struct GNUNET_MessageHeader *msg;
1479 GNUNET_assert (NULL != info);
1480 GNUNET_assert (NULL != info->mesh_data);
1481 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1482 total_size = ntohs (msg->size);
1484 if (total_size > size)
1489 memcpy (buf, msg, total_size);
1490 data_descriptor_decrement_rc (info->mesh_data);
1497 * Sends an already built non-multicast message to a peer,
1498 * properly registrating all used resources.
1500 * @param message Message to send. Function makes a copy of it.
1501 * @param peer Short ID of the neighbor whom to send the message.
1502 * @param t Tunnel on which this message is transmitted.
1505 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1506 const struct GNUNET_PeerIdentity *peer,
1507 struct MeshTunnel *t)
1509 struct MeshTransmissionDescriptor *info;
1510 struct MeshPeerInfo *neighbor;
1511 struct MeshPeerPath *p;
1515 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1517 size = ntohs (message->size);
1518 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1519 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1520 info->mesh_data->data = GNUNET_malloc (size);
1521 memcpy (info->mesh_data->data, message, size);
1522 type = ntohs(message->type);
1525 struct GNUNET_MESH_Unicast *m;
1526 struct GNUNET_MESH_ToOrigin *to;
1528 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1529 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
1530 m->ttl = htonl (ntohl (m->ttl) - 1);
1532 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1533 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
1535 to->pid = htonl(t->bck_pid);
1537 info->mesh_data->data_len = size;
1538 info->mesh_data->reference_counter = 1;
1539 info->mesh_data->total_out = 1;
1540 neighbor = peer_get (peer);
1541 for (p = neighbor->path_head; NULL != p; p = p->next)
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 " %s IS NOT DIRECTLY CONNECTED\n",
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1557 for (p = neighbor->path_head; NULL != p; p = p->next)
1559 struct GNUNET_PeerIdentity debug_id;
1562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1563 " path with %u hops through:\n",
1565 for (i = 0; i < p->length; i++)
1567 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1570 i, GNUNET_i2s(&debug_id));
1574 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1575 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1576 " no direct connection to %s\n",
1578 GNUNET_free (info->mesh_data->data);
1579 GNUNET_free (info->mesh_data);
1583 info->peer = neighbor;
1584 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1595 * Sends a CREATE PATH message for a path to a peer, properly registrating
1596 * all used resources.
1598 * @param peer PeerInfo of the final peer for whom this path is being created.
1599 * @param p Path itself.
1600 * @param t Tunnel for which the path is created.
1603 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1604 struct MeshTunnel *t)
1606 struct MeshPathInfo *path_info;
1607 struct MeshPeerInfo *neighbor;
1615 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1616 path_info->path = p;
1618 neighbor = peer_get_short (t->next_hop);
1619 path_info->peer = neighbor;
1620 queue_add (path_info,
1621 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1622 sizeof (struct GNUNET_MESH_ManipulatePath) +
1623 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1630 * Sends a DESTROY PATH message to free resources for a path in a tunnel.
1631 * Does not free the path itself.
1633 * @param t Tunnel whose path to destroy.
1634 * @param destination Short ID of the peer to whom the path to destroy.
1637 send_destroy_path (struct MeshTunnel *t)
1639 struct MeshPeerPath *p;
1648 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1649 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1651 struct GNUNET_MESH_ManipulatePath *msg;
1652 struct GNUNET_PeerIdentity *pi;
1653 struct GNUNET_PeerIdentity id;
1657 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1658 msg->header.size = htons (size);
1659 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1660 msg->tid = htonl (t->id.tid);
1661 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1662 for (i = 0; i < p->length; i++)
1664 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1666 GNUNET_PEER_resolve (t->next_hop, &id);
1667 send_prebuilt_message (&msg->header, &id, t);
1673 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1675 * @param t Tunnel which to confirm.
1678 send_path_ack (struct MeshTunnel *t)
1680 struct MeshTransmissionDescriptor *info;
1681 struct GNUNET_PeerIdentity id;
1683 GNUNET_PEER_resolve (t->prev_hop, &id);
1684 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1685 info->origin = &t->id;
1686 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1687 GNUNET_assert (NULL != info->peer);
1690 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1691 sizeof (struct GNUNET_MESH_PathACK),
1698 * Try to establish a new connection to this peer in the fiven tunnel.
1699 * If the peer doesn't have any path to it yet, try to get one.
1700 * If the peer already has some path, send a CREATE PATH towards it.
1702 * @param peer PeerInfo of the peer.
1703 * @param t Tunnel for which to create the path, if possible.
1706 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1708 struct MeshPathInfo *path_info;
1709 struct MeshPeerPath *p;
1711 if (NULL != peer->path_head)
1713 p = peer_get_best_path (peer, t);
1714 tunnel_use_path (t, p);
1715 send_create_path (peer, p, t);
1717 else if (NULL == peer->dhtget)
1719 struct GNUNET_PeerIdentity id;
1721 GNUNET_PEER_resolve (peer->id, &id);
1722 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1723 path_info->peer = peer;
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1726 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1727 peer->dhtgetcls = path_info;
1728 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1729 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1730 &id.hashPubKey, /* key to search */
1731 dht_replication_level, /* replication level */
1732 GNUNET_DHT_RO_RECORD_ROUTE |
1733 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1734 NULL, /* xquery */ // FIXME BLOOMFILTER
1735 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1736 &dht_get_id_handler, path_info);
1738 /* Otherwise, there is no path but the DHT get is already started. */
1743 * Task to delay the connection of a peer
1745 * @param cls Closure (path info with tunnel and peer to connect).
1746 * Will be free'd on exection.
1747 * @param tc TaskContext
1750 peer_info_connect_task (void *cls,
1751 const struct GNUNET_SCHEDULER_TaskContext *tc)
1753 struct MeshPathInfo *path_info = cls;
1755 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
1757 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1762 peer_connect (path_info->peer, path_info->t);
1768 * Destroy the peer_info and free any allocated resources linked to it
1770 * @param pi The peer_info to destroy.
1772 * @return GNUNET_OK on success
1775 peer_info_destroy (struct MeshPeerInfo *pi)
1777 struct GNUNET_PeerIdentity id;
1778 struct MeshPeerPath *p;
1779 struct MeshPeerPath *nextp;
1781 GNUNET_PEER_resolve (pi->id, &id);
1782 GNUNET_PEER_change_rc (pi->id, -1);
1785 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1788 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1789 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1791 if (NULL != pi->dhtget)
1793 GNUNET_DHT_get_stop (pi->dhtget);
1794 GNUNET_free (pi->dhtgetcls);
1800 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1804 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
1806 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
1814 * Remove all paths that rely on a direct connection between p1 and p2
1815 * from the peer itself and notify all tunnels about it.
1817 * @param peer PeerInfo of affected peer.
1818 * @param p1 GNUNET_PEER_Id of one peer.
1819 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1822 * TODO: optimize (see below)
1825 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1828 struct MeshPeerPath *p;
1829 struct MeshPeerPath *next;
1830 struct MeshPeerInfo *peer_d;
1832 unsigned int destroyed;
1835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1837 for (p = peer->path_head; NULL != p; p = next)
1840 for (i = 0; i < (p->length - 1); i++)
1842 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1843 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1845 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1855 for (i = 0; i < peer->ntunnels; i++)
1857 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1861 peer_d = peer_get_short (d);
1862 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1863 tunnel_use_path (peer->tunnels[i], next);
1864 peer_connect (peer_d, peer->tunnels[i]);
1866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1871 * Add the path to the peer and update the path used to reach it in case this
1874 * @param peer_info Destination peer to add the path to.
1875 * @param path New path to add. Last peer must be the peer in arg 1.
1876 * Path will be either used of freed if already known.
1877 * @param trusted Do we trust that this path is real?
1880 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1883 struct MeshPeerPath *aux;
1887 if ((NULL == peer_info) || (NULL == path))
1890 path_destroy (path);
1893 if (path->peers[path->length - 1] != peer_info->id)
1896 path_destroy (path);
1899 if (path->length <= 2 && GNUNET_NO == trusted)
1901 /* Only allow CORE to tell us about direct paths */
1902 path_destroy (path);
1905 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1906 for (l = 1; l < path->length; l++)
1908 if (path->peers[l] == myid)
1910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1911 for (l2 = 0; l2 < path->length - l; l2++)
1913 path->peers[l2] = path->peers[l + l2];
1918 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1923 struct GNUNET_PeerIdentity id;
1925 GNUNET_PEER_resolve (peer_info->id, &id);
1926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1927 path->length, GNUNET_i2s (&id));
1930 l = path_get_length (path);
1937 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1938 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1940 l2 = path_get_length (aux);
1943 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1944 peer_info->path_tail, aux, path);
1949 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1951 path_destroy (path);
1956 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1963 * Add the path to the origin peer and update the path used to reach it in case
1964 * this is the shortest.
1965 * The path is given in peer_info -> destination, therefore we turn the path
1966 * upside down first.
1968 * @param peer_info Peer to add the path to, being the origin of the path.
1969 * @param path New path to add after being inversed.
1970 * @param trusted Do we trust that this path is real?
1973 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1974 struct MeshPeerPath *path, int trusted)
1977 peer_info_add_path (peer_info, path, trusted);
1982 * Function called if the connection to the peer has been stalled for a while,
1983 * possibly due to a missed ACK. Poll the peer about its ACK status.
1985 * @param cls Closure (cinfo).
1986 * @param tc TaskContext.
1989 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1991 // struct GNUNET_MESH_Poll msg;
1992 // struct GNUNET_PeerIdentity id;
1993 // struct MeshTunnel *t;
1995 // cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
1996 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2002 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2003 // msg.header.size = htons (sizeof (msg));
2004 // msg.tid = htonl (t->id.tid);
2005 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2006 // msg.last_ack = htonl (cinfo->fwd_ack);
2008 // GNUNET_PEER_resolve (cinfo->id, &id);
2009 // send_prebuilt_message (&msg.header, &id, cinfo->t);
2010 // cinfo->fc_poll_time = GNUNET_TIME_relative_min (
2011 // MESH_MAX_POLL_TIME,
2012 // GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
2013 // cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
2014 // &tunnel_poll, cinfo);
2019 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2020 * to obtain a local peer -> destination path and interning the peer ids.
2022 * @return Newly allocated and created path
2024 static struct MeshPeerPath *
2025 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2026 unsigned int get_path_length,
2027 const struct GNUNET_PeerIdentity *put_path,
2028 unsigned int put_path_length)
2030 struct MeshPeerPath *p;
2036 GNUNET_PEER_change_rc (myid, 1);
2037 i = get_path_length;
2038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2039 for (i--; i >= 0; i--)
2041 id = GNUNET_PEER_intern (&get_path[i]);
2042 if (p->length > 0 && id == p->peers[p->length - 1])
2044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2045 GNUNET_PEER_change_rc (id, -1);
2049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2050 GNUNET_i2s (&get_path[i]));
2052 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2053 p->peers[p->length - 1] = id;
2056 i = put_path_length;
2057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2058 for (i--; i >= 0; i--)
2060 id = GNUNET_PEER_intern (&put_path[i]);
2063 /* PUT path went through us, so discard the path up until now and start
2064 * from here to get a much shorter (and loop-free) path.
2069 if (p->length > 0 && id == p->peers[p->length - 1])
2071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2072 GNUNET_PEER_change_rc (id, -1);
2076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2077 GNUNET_i2s (&put_path[i]));
2079 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2080 p->peers[p->length - 1] = id;
2084 if (get_path_length > 0)
2085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2086 GNUNET_i2s (&get_path[0]));
2087 if (put_path_length > 0)
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2089 GNUNET_i2s (&put_path[0]));
2090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2092 for (i = 0; i < p->length; i++)
2094 struct GNUNET_PeerIdentity peer_id;
2096 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2098 GNUNET_i2s (&peer_id));
2106 * Adds a path to the peer_infos of all the peers in the path
2108 * @param p Path to process.
2109 * @param confirmed Whether we know if the path works or not.
2112 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2116 /* TODO: invert and add */
2117 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2118 for (i++; i < p->length; i++)
2120 struct MeshPeerInfo *aux;
2121 struct MeshPeerPath *copy;
2123 aux = peer_get_short (p->peers[i]);
2124 copy = path_duplicate (p);
2125 copy->length = i + 1;
2126 peer_info_add_path (aux, copy, GNUNET_NO);
2132 * Send keepalive packets for a peer
2134 * @param cls Closure (tunnel for which to send the keepalive).
2135 * @param tc Notification context.
2137 * TODO: implement explicit multicast keepalive?
2140 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2144 * Search for a tunnel among the incoming tunnels
2146 * @param tid the local id of the tunnel
2148 * @return tunnel handler, NULL if doesn't exist
2150 static struct MeshTunnel *
2151 tunnel_get_incoming (MESH_TunnelNumber tid)
2153 struct GNUNET_HashCode hash;
2155 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2156 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2157 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2162 * Search for a tunnel among the tunnels for a client
2164 * @param c the client whose tunnels to search in
2165 * @param tid the local id of the tunnel
2167 * @return tunnel handler, NULL if doesn't exist
2169 static struct MeshTunnel *
2170 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2172 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2174 return tunnel_get_incoming (tid);
2178 struct GNUNET_HashCode hash;
2180 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2181 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2187 * Search for a tunnel by global ID using PEER_ID
2189 * @param pi owner of the tunnel
2190 * @param tid global tunnel number
2192 * @return tunnel handler, NULL if doesn't exist
2194 static struct MeshTunnel *
2195 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2197 struct MESH_TunnelID id;
2198 struct GNUNET_HashCode hash;
2203 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2204 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2209 * Search for a tunnel by global ID using full PeerIdentities
2211 * @param oid owner of the tunnel
2212 * @param tid global tunnel number
2214 * @return tunnel handler, NULL if doesn't exist
2216 static struct MeshTunnel *
2217 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2219 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2224 * Delete an active client from the tunnel.
2230 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2234 for (i = 0; i < t->nclients; i++)
2236 if (t->clients[i] == c)
2238 t->clients[i] = t->clients[t->nclients - 1];
2239 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2240 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2242 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2250 * Delete an ignored client from the tunnel.
2256 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2260 for (i = 0; i < t->nignore; i++)
2262 if (t->ignore[i] == c)
2264 t->ignore[i] = t->ignore[t->nignore - 1];
2265 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2273 * Delete a client from the tunnel. It should be only done on
2274 * client disconnection, otherwise use client_ignore_tunnel.
2280 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2282 tunnel_delete_ignored_client (t, c);
2283 tunnel_delete_active_client (t, c);
2288 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
2290 struct MeshTunnelClientInfo clinfo;
2292 GNUNET_array_append (t->clients, t->nclients, c);
2293 clinfo.fwd_ack = t->fwd_pid + 1;
2294 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
2295 clinfo.fwd_pid = t->fwd_pid;
2296 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
2298 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
2303 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
2307 for (i = 0; i < p->length; i++)
2309 if (p->peers[i] == myid)
2312 if (i > p->length - 1)
2317 if (i < p->length - 1)
2318 t->next_hop = p->peers[i + 1];
2322 t->prev_hop = p->peers[i - 1];
2325 if (NULL != t->path)
2326 path_destroy (t->path);
2327 t->path = path_duplicate (p);
2332 * Notifies a tunnel that a connection has broken that affects at least
2333 * some of its peers. Sends a notification towards the root of the tree.
2334 * In case the peer is the owner of the tree, notifies the client that owns
2335 * the tunnel and tries to reconnect.
2337 * @param t Tunnel affected.
2338 * @param p1 Peer that got disconnected from p2.
2339 * @param p2 Peer that got disconnected from p1.
2341 * @return Short ID of the peer disconnected (either p1 or p2).
2342 * 0 if the tunnel remained unaffected.
2344 static GNUNET_PEER_Id
2345 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2348 // if (myid != p1 && myid != p2) FIXME
2353 // if (tree_get_predecessor (t->tree) != 0)
2355 // /* We are the peer still connected, notify owner of the disconnection. */
2356 // struct GNUNET_MESH_PathBroken msg;
2357 // struct GNUNET_PeerIdentity neighbor;
2359 // msg.header.size = htons (sizeof (msg));
2360 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2361 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2362 // msg.tid = htonl (t->id.tid);
2363 // msg.peer1 = my_full_id;
2364 // GNUNET_PEER_resolve (pid, &msg.peer2);
2365 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2366 // send_prebuilt_message (&msg.header, &neighbor, t);
2373 * Get the Flow Control info of a client.
2375 * @param t Tunnel on which to look.
2376 * @param c Client whose ACK to get.
2378 * @return ACK value.
2380 static struct MeshTunnelClientInfo *
2381 tunnel_get_client_fc (struct MeshTunnel *t,
2382 struct MeshClient *c)
2386 for (i = 0; i < t->nclients; i++)
2388 if (t->clients[i] != c)
2390 return &t->clients_fc[i];
2393 return NULL; // avoid compiler / coverity complaints
2398 * Set the FWD ACK value of a client in a particular tunnel.
2400 * @param t Tunnel affected.
2401 * @param c Client whose ACK to set.
2402 * @param ack ACK value.
2405 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
2406 struct MeshClient *c,
2411 for (i = 0; i < t->nclients; i++)
2413 if (t->clients[i] != c)
2415 t->clients_fc[i].fwd_ack = ack;
2423 * Get the highest ACK value of all clients in a particular tunnel,
2424 * according to the buffering/speed settings.
2426 * @param t Tunnel on which to look.
2428 * @return Corresponding ACK value (max uint32_t).
2429 * If no clients are suscribed, -1LL.
2432 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
2437 if (0 == t->nclients)
2439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2440 " tunnel has no clients, no FWD ACK\n");
2444 for (ack = -1LL, i = 0; i < t->nclients; i++)
2447 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack))
2449 ack = t->clients_fc[i].fwd_ack;
2453 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
2454 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
2456 return (uint32_t) ack;
2461 * Get the current fwd ack value for a tunnel, taking in account the tunnel
2462 * mode and the status of all children nodes.
2466 * @return Maximum PID allowed.
2469 tunnel_get_fwd_ack (struct MeshTunnel *t)
2473 uint32_t buffer_free;
2477 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
2478 client_ack = tunnel_get_clients_fwd_ack (t);
2479 if (GNUNET_YES == t->nobuffer)
2485 ack = count + buffer_free; // Overflow? OK!
2487 if (-1LL == client_ack)
2492 ack = GMC_max_pid ((uint32_t) client_ack, ack);
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 "c %u, bf %u, cl %lld, ACK: %u\n",
2496 count, buffer_free, client_ack, ack);
2502 * Build a local ACK message and send it to a local client.
2504 * @param t Tunnel on which to send the ACK.
2505 * @param c Client to whom send the ACK.
2506 * @param ack Value of the ACK.
2509 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
2511 struct GNUNET_MESH_LocalAck msg;
2513 msg.header.size = htons (sizeof (msg));
2514 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2515 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
2516 msg.max_pid = htonl (ack);
2517 GNUNET_SERVER_notification_context_unicast(nc,
2524 * Build an ACK message and queue it to send to the given peer.
2526 * @param t Tunnel on which to send the ACK.
2527 * @param peer Peer to whom send the ACK.
2528 * @param ack Value of the ACK.
2531 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
2533 struct GNUNET_MESH_ACK msg;
2535 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2536 msg.header.size = htons (sizeof (msg));
2537 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2538 msg.pid = htonl (ack);
2539 msg.tid = htonl (t->id.tid);
2541 send_prebuilt_message (&msg.header, peer, t);
2546 * Notify a the owner of a tunnel about how many more
2547 * payload packages will we accept on a given tunnel.
2549 * @param t Tunnel on which to send the ACK.
2552 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
2556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2557 "Sending client FWD ACK on tunnel %X\n",
2560 ack = tunnel_get_fwd_ack (t);
2562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
2563 if (t->last_fwd_ack == ack)
2565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
2569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
2570 t->last_fwd_ack = ack;
2571 send_local_ack (t, t->owner, ack);
2576 * Send an ACK informing the predecessor about the available buffer space.
2577 * In case there is no predecessor, inform the owning client.
2578 * If buffering is off, send only on behalf of children or self if endpoint.
2579 * If buffering is on, send when sent to children and buffer space is free.
2580 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2581 * the ACK itself goes "back" (towards root).
2583 * @param t Tunnel on which to send the ACK.
2584 * @param type Type of message that triggered the ACK transmission.
2587 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2589 struct GNUNET_PeerIdentity id;
2592 if (NULL != t->owner)
2594 tunnel_send_client_fwd_ack (t);
2597 /* Is it after unicast / multicast retransmission? */
2600 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2602 "ACK due to FWD DATA retransmission\n");
2603 if (GNUNET_YES == t->nobuffer)
2605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2609 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2610 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2612 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2613 t->force_ack = GNUNET_YES;
2619 /* Check if we need to transmit the ACK */
2620 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
2621 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
2622 GNUNET_NO == t->force_ack)
2624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2626 " t->qmax: %u, t->qn: %u\n",
2627 t->fwd_queue_max, t->fwd_queue_n);
2628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2629 " t->pid: %u, t->ack: %u\n",
2630 t->fwd_pid, t->last_fwd_ack);
2634 /* Ok, ACK might be necessary, what PID to ACK? */
2635 ack = tunnel_get_fwd_ack (t);
2637 /* If speed_min and not all children have ack'd, dont send yet */
2638 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
2644 t->last_fwd_ack = ack;
2645 GNUNET_PEER_resolve (t->prev_hop, &id);
2646 send_ack (t, &id, ack);
2648 t->force_ack = GNUNET_NO;
2653 * Send a child node a BCK ACK to allow him to send more to_origin data.
2656 * @param id Id of the child node.
2659 tunnel_send_child_bck_ack (struct MeshTunnel *t,
2662 struct GNUNET_PeerIdentity peer;
2663 uint32_t ack = 0; // FIXME
2665 GNUNET_PEER_resolve (id, &peer);
2666 // ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
2668 // if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
2670 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2671 // " Not sending ACK, not needed\n");
2674 // cinfo->bck_ack = ack;
2676 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2677 // " Sending BCK ACK %u (last sent: %u)\n",
2678 // ack, cinfo->bck_ack);
2679 send_ack (t, &peer, ack);
2684 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
2686 * Iterates over all clients and sends BCK ACKs to the ones that need it.
2688 * FIXME fc: what happens if we have 2 clients but q_size is 1?
2689 * - implement a size 1 buffer in each client_fc AND children_fc
2690 * to hold at least 1 message per "child".
2691 * problem: violates no buffer policy
2692 * - ack 0 and make "children" poll for transmission slots
2693 * problem: big overhead, extra latency even in low traffic
2696 * @param t Tunnel on which to send the BCK ACKs.
2699 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
2702 unsigned int tunnel_delta;
2704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
2706 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
2707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
2709 /* Find client whom to allow to send to origin (with lowest buffer space) */
2710 for (i = 0; i < t->nclients; i++)
2712 struct MeshTunnelClientInfo *clinfo;
2715 clinfo = &t->clients_fc[i];
2716 delta = clinfo->bck_ack - clinfo->bck_pid;
2717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
2718 t->clients[i]->id, delta);
2720 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
2721 (GNUNET_YES == t->nobuffer && 0 == delta))
2725 ack = clinfo->bck_pid;
2726 ack += t->nobuffer ? 1 : tunnel_delta;
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2728 " sending ack to client %u: %u\n",
2729 t->clients[i]->id, ack);
2730 send_local_ack (t, t->clients[i], ack);
2731 clinfo->bck_ack = ack;
2735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2736 " not sending ack to client %u (td %u, d %u)\n",
2737 t->clients[i]->id, tunnel_delta, delta);
2744 * Send an ACK informing the children nodes and destination clients about
2745 * the available buffer space.
2746 * If buffering is off, send only on behalf of root (can be self).
2747 * If buffering is on, send when sent to predecessor and buffer space is free.
2748 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2749 * the ACK itself goes "forward" (towards children/clients).
2751 * @param t Tunnel on which to send the ACK.
2752 * @param type Type of message that triggered the ACK transmission.
2755 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2758 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2759 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2760 /* Is it after data to_origin retransmission? */
2763 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2764 if (GNUNET_YES == t->nobuffer)
2766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2767 " Not sending ACK, nobuffer\n");
2771 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2772 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2774 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2775 t->force_ack = GNUNET_YES;
2781 tunnel_send_clients_bck_ack (t);
2782 tunnel_send_child_bck_ack (t, t->next_hop);
2783 t->force_ack = GNUNET_NO;
2788 * @brief Re-initiate traffic to this peer if necessary.
2790 * Check if there is traffic queued towards this peer
2791 * and the core transmit handle is NULL (traffic was stalled).
2792 * If so, call core tmt rdy.
2794 * @param peer_id Short ID of peer to which initiate traffic.
2797 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2799 struct MeshPeerInfo *peer;
2800 struct GNUNET_PeerIdentity id;
2801 struct MeshPeerQueue *q;
2804 peer = peer_get_short (peer_id);
2805 if (NULL != peer->core_transmit)
2808 q = queue_get_next(peer);
2811 /* Might br multicast traffic already sent to this particular peer but
2812 * not to other children in this tunnel.
2813 * This way t->queue_n would be > 0 but the queue of this particular peer
2819 GNUNET_PEER_resolve (peer->id, &id);
2820 peer->core_transmit =
2821 GNUNET_CORE_notify_transmit_ready(core_handle,
2824 GNUNET_TIME_UNIT_FOREVER_REL,
2834 * Send a message to all peers in this tunnel that the tunnel is no longer
2837 * @param t The tunnel whose peers to notify.
2840 tunnel_send_destroy (struct MeshTunnel *t)
2842 struct GNUNET_MESH_TunnelDestroy msg;
2843 struct GNUNET_PeerIdentity id;
2845 msg.header.size = htons (sizeof (msg));
2846 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2847 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2848 msg.tid = htonl (t->id.tid);
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2850 " sending tunnel destroy for tunnel: %s [%X]\n",
2851 GNUNET_i2s (&msg.oid), t->id.tid);
2853 if (0 != t->next_hop)
2855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2856 GNUNET_PEER_resolve (t->next_hop, &id);
2857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2858 " sending back to %s\n",
2860 send_prebuilt_message (&msg.header, &id, t);
2862 if (0 != t->prev_hop)
2864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2865 GNUNET_PEER_resolve (t->prev_hop, &id);
2866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2867 " sending back to %s\n",
2869 send_prebuilt_message (&msg.header, &id, t);
2875 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2877 * @param t Tunnel which to cancel.
2878 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2881 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2883 struct MeshPeerInfo *peer_info;
2884 struct MeshPeerQueue *pq;
2885 struct MeshPeerQueue *next;
2887 peer_info = peer_get_short (neighbor);
2888 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2891 if (pq->tunnel == t)
2893 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2894 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2896 // Should have been removed on destroy children
2899 queue_destroy (pq, GNUNET_YES);
2902 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2904 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2905 peer_info->core_transmit = NULL;
2911 * Destroy the tunnel and free any allocated resources linked to it.
2913 * @param t the tunnel to destroy
2915 * @return GNUNET_OK on success
2918 tunnel_destroy (struct MeshTunnel *t)
2920 struct MeshClient *c;
2921 struct GNUNET_HashCode hash;
2932 struct GNUNET_PeerIdentity id;
2934 GNUNET_PEER_resolve (t->id.oid, &id);
2935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2936 GNUNET_i2s (&id), t->id.tid);
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2942 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2943 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2951 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2953 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2960 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2961 for (i = 0; i < t->nclients; i++)
2965 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2971 for (i = 0; i < t->nignore; i++)
2975 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2982 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
2983 GNUNET_free_non_null (t->clients);
2984 GNUNET_free_non_null (t->ignore);
2985 GNUNET_free_non_null (t->clients_fc);
2987 peer_cancel_queues (t->next_hop, t);
2988 peer_cancel_queues (t->prev_hop, t);
2990 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2991 GNUNET_SCHEDULER_cancel (t->timeout_task);
2992 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2993 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2996 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3001 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
3004 * Tunnel is empty: destroy it.
3006 * @param cls Closure (Tunnel).
3007 * @param tc TaskContext.
3010 tunnel_destroy_empty_delayed (void *cls,
3011 const struct GNUNET_SCHEDULER_TaskContext *tc)
3013 struct MeshTunnel *t = cls;
3015 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
3016 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3019 if (0 != t->nclients)
3024 struct GNUNET_PeerIdentity id;
3026 GNUNET_PEER_resolve (t->id.oid, &id);
3027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3028 "executing destruction of empty tunnel %s [%X]\n",
3029 GNUNET_i2s (&id), t->id.tid);
3033 tunnel_send_destroy (t);
3034 if (0 == t->pending_messages)
3037 t->destroy = GNUNET_YES;
3042 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
3044 * @param t Tunnel to destroy if empty.
3047 tunnel_destroy_empty (struct MeshTunnel *t)
3049 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
3052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u %u\n",
3053 t->delayed_destroy, t->nclients);
3059 struct GNUNET_PeerIdentity id;
3061 GNUNET_PEER_resolve (t->id.oid, &id);
3062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3063 "scheduling destruction of empty tunnel %s [%X]\n",
3064 GNUNET_i2s (&id), t->id.tid);
3068 t->delayed_destroy =
3069 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
3070 &tunnel_destroy_empty_delayed,
3076 * Create a new tunnel
3078 * @param owner Who is the owner of the tunnel (short ID).
3079 * @param tid Tunnel Number of the tunnel.
3080 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3081 * @param local Tunnel Number for the tunnel, for the client point of view.
3083 * @return A new initialized tunnel. NULL on error.
3085 static struct MeshTunnel *
3086 tunnel_new (GNUNET_PEER_Id owner,
3087 MESH_TunnelNumber tid,
3088 struct MeshClient *client,
3089 MESH_TunnelNumber local)
3091 struct MeshTunnel *t;
3092 struct GNUNET_HashCode hash;
3094 if (n_tunnels >= max_tunnels && NULL == client)
3097 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3100 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
3101 t->bck_queue_max = t->fwd_queue_max;
3103 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
3104 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
3105 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
3106 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
3107 t->local_tid = local;
3109 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3111 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3113 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3114 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3121 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3128 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3130 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3131 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3135 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3146 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3147 * client when the client disconnects. If the client is not the owner, the
3148 * owner will get notified if no more clients are in the tunnel and the client
3149 * get removed from the tunnel's list.
3151 * @param cls closure (client that is disconnecting)
3152 * @param key the hash of the local tunnel id (used to access the hashmap)
3153 * @param value the value stored at the key (tunnel to destroy)
3155 * @return GNUNET_OK, keep iterating.
3158 tunnel_destroy_iterator (void *cls,
3159 const struct GNUNET_HashCode * key,
3162 struct MeshTunnel *t = value;
3163 struct MeshClient *c = cls;
3165 send_client_tunnel_disconnect (t, c);
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
3169 tunnel_delete_client (t, c);
3170 client_delete_tunnel (c, t);
3171 tunnel_destroy_empty (t);
3174 tunnel_send_destroy (t);
3176 t->destroy = GNUNET_YES;
3183 * Timeout function, destroys tunnel if called
3185 * @param cls Closure (tunnel to destroy).
3186 * @param tc TaskContext
3189 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3191 struct MeshTunnel *t = cls;
3192 struct GNUNET_PeerIdentity id;
3194 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3195 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3197 GNUNET_PEER_resolve(t->id.oid, &id);
3198 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3199 "Tunnel %s [%X] timed out. Destroying.\n",
3200 GNUNET_i2s(&id), t->id.tid);
3201 send_clients_tunnel_destroy (t);
3206 * Resets the tunnel timeout. Starts it if no timeout was running.
3208 * @param t Tunnel whose timeout to reset.
3210 * TODO use heap to improve efficiency of scheduler.
3213 tunnel_reset_timeout (struct MeshTunnel *t)
3215 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3216 GNUNET_SCHEDULER_cancel (t->timeout_task);
3218 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3219 (refresh_path_time, 4), &tunnel_timeout, t);
3223 /******************************************************************************/
3224 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3225 /******************************************************************************/
3228 * Function to send a create path packet to a peer.
3230 * @param cls closure
3231 * @param size number of bytes available in buf
3232 * @param buf where the callee should write the message
3233 * @return number of bytes written to buf
3236 send_core_path_create (void *cls, size_t size, void *buf)
3238 struct MeshPathInfo *info = cls;
3239 struct GNUNET_MESH_ManipulatePath *msg;
3240 struct GNUNET_PeerIdentity *peer_ptr;
3241 struct MeshTunnel *t = info->t;
3242 struct MeshPeerPath *p = info->path;
3247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3249 sizeof (struct GNUNET_MESH_ManipulatePath) +
3250 p->length * sizeof (struct GNUNET_PeerIdentity);
3252 if (size < size_needed || NULL == buf)
3257 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3258 msg->header.size = htons (size_needed);
3259 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3260 msg->tid = ntohl (t->id.tid);
3263 if (GNUNET_YES == t->nobuffer)
3264 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3265 msg->opt = htonl(opt);
3268 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3269 for (i = 0; i < p->length; i++)
3271 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3278 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3284 * Fill the core buffer
3286 * @param cls closure (data itself)
3287 * @param size number of bytes available in buf
3288 * @param buf where the callee should write the message
3290 * @return number of bytes written to buf
3293 send_core_data_multicast (void *cls, size_t size, void *buf)
3295 struct MeshTransmissionDescriptor *info = cls;
3298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3299 GNUNET_assert (NULL != info);
3300 GNUNET_assert (NULL != info->peer);
3301 total_size = info->mesh_data->data_len;
3302 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3304 if (total_size > size)
3309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3310 memcpy (buf, info->mesh_data->data, total_size);
3313 struct GNUNET_MessageHeader *mh;
3316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
3317 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
3320 data_descriptor_decrement_rc (info->mesh_data);
3321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3329 * Creates a path ack message in buf and frees all unused resources.
3331 * @param cls closure (MeshTransmissionDescriptor)
3332 * @param size number of bytes available in buf
3333 * @param buf where the callee should write the message
3334 * @return number of bytes written to buf
3337 send_core_path_ack (void *cls, size_t size, void *buf)
3339 struct MeshTransmissionDescriptor *info = cls;
3340 struct GNUNET_MESH_PathACK *msg = buf;
3342 GNUNET_assert (NULL != info);
3343 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3348 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3349 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3350 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3351 msg->tid = htonl (info->origin->tid);
3352 msg->peer_id = my_full_id;
3355 /* TODO add signature */
3357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3358 return sizeof (struct GNUNET_MESH_PathACK);
3363 * Free a transmission that was already queued with all resources
3364 * associated to the request.
3366 * @param queue Queue handler to cancel.
3367 * @param clear_cls Is it necessary to free associated cls?
3370 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3372 struct MeshTransmissionDescriptor *dd;
3373 struct MeshPathInfo *path_info;
3374 // unsigned int max;
3376 if (GNUNET_YES == clear_cls)
3378 switch (queue->type)
3380 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3381 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3382 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3384 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3385 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3386 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3387 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3388 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3390 " prebuilt message\n");
3391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3393 GNUNET_MESH_DEBUG_M2S(queue->type));
3395 data_descriptor_decrement_rc (dd->mesh_data);
3397 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3399 path_info = queue->cls;
3400 path_destroy (path_info->path);
3404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3405 " type %s unknown!\n",
3406 GNUNET_MESH_DEBUG_M2S(queue->type));
3408 GNUNET_free_non_null (queue->cls);
3410 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3411 queue->peer->queue_tail,
3414 /* Delete from child_fc in the appropiate tunnel */
3415 // max = queue->tunnel->fwd_queue_max;
3416 // GNUNET_PEER_resolve (queue->peer->id, &id);
3417 // if (NULL != cinfo)
3419 // for (i = 0; i < cinfo->send_buffer_n; i++)
3421 // i2 = (cinfo->send_buffer_start + i) % max;
3422 // if (cinfo->send_buffer[i2] == queue)
3424 // /* Found corresponding entry in the send_buffer. Move all others back. */
3428 // for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
3430 // j2 = (cinfo->send_buffer_start + j) % max;
3431 // j3 = (cinfo->send_buffer_start + j + 1) % max;
3432 // cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
3435 // cinfo->send_buffer[j3] = NULL;
3436 // cinfo->send_buffer_n--;
3441 GNUNET_free (queue);
3446 * @brief Get the next transmittable message from the queue.
3448 * This will be the head, except in the case of being a data packet
3449 * not allowed by the destination peer.
3451 * @param peer Destination peer.
3453 * @return The next viable MeshPeerQueue element to send to that peer.
3454 * NULL when there are no transmittable messages.
3456 struct MeshPeerQueue *
3457 queue_get_next (const struct MeshPeerInfo *peer)
3459 struct MeshPeerQueue *q;
3461 struct MeshTransmissionDescriptor *info;
3462 // struct GNUNET_MESH_Unicast *ucast;
3463 // struct GNUNET_MESH_ToOrigin *to_orig;
3464 struct GNUNET_PeerIdentity id;
3466 // uint32_t ack; FIXME
3468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
3469 for (q = peer->queue_head; NULL != q; q = q->next)
3473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3475 GNUNET_MESH_DEBUG_M2S(q->type));
3478 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3479 // ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
3480 // pid = ntohl (ucast->pid);
3481 GNUNET_PEER_resolve (info->peer->id, &id);
3482 // ack = cinfo->fwd_ack;
3484 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3485 // to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
3486 // pid = ntohl (to_orig->pid);
3487 // ack = t->bck_ack;
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3494 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3495 // "********* ACK: %u, PID: %u\n",
3497 // if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
3499 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3500 // "********* OK!\n");
3505 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3506 // "********* NEXT!\n");
3509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3510 "********* nothing found\n");
3516 queue_send (void *cls, size_t size, void *buf)
3518 struct MeshPeerInfo *peer = cls;
3519 struct GNUNET_MessageHeader *msg;
3520 struct MeshPeerQueue *queue;
3521 struct MeshTunnel *t;
3522 struct GNUNET_PeerIdentity dst_id;
3525 peer->core_transmit = NULL;
3527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3528 queue = queue_get_next (peer);
3530 /* Queue has no internal mesh traffic nor sendable payload */
3533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
3534 if (NULL == peer->queue_head)
3535 GNUNET_break (0); // Should've been canceled
3538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3540 GNUNET_PEER_resolve (peer->id, &dst_id);
3541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3542 "********* towards %s\n",
3543 GNUNET_i2s(&dst_id));
3544 /* Check if buffer size is enough for the message */
3545 if (queue->size > size)
3547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3548 "********* not enough room, reissue\n");
3549 peer->core_transmit =
3550 GNUNET_CORE_notify_transmit_ready (core_handle,
3553 GNUNET_TIME_UNIT_FOREVER_REL,
3560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3563 GNUNET_assert (0 < t->pending_messages);
3564 t->pending_messages--;
3565 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
3568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3569 "********* unicast: t->q (%u/%u)\n",
3570 t->fwd_queue_n, t->fwd_queue_max);
3572 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
3575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
3579 switch (queue->type)
3582 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3583 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3584 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3585 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3586 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3588 "********* raw: %s\n",
3589 GNUNET_MESH_DEBUG_M2S (queue->type));
3591 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3592 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3593 data_size = send_core_data_raw (queue->cls, size, buf);
3594 msg = (struct GNUNET_MessageHeader *) buf;
3595 switch (ntohs (msg->type)) // Type of preconstructed message
3597 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3598 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3600 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3601 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3607 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3609 data_size = send_core_path_create (queue->cls, size, buf);
3611 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3613 data_size = send_core_path_ack (queue->cls, size, buf);
3615 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
3617 data_size = send_core_data_multicast (queue->cls, size, buf);
3621 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3622 "********* type unknown: %u\n",
3626 switch (queue->type)
3628 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3629 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3630 // if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
3632 // GNUNET_break (0);
3633 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3634 // "at pos %u (%p) != %p\n",
3635 // cinfo->send_buffer_start,
3636 // cinfo->send_buffer[cinfo->send_buffer_start],
3639 // if (cinfo->send_buffer_n > 0)
3641 // cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
3642 // cinfo->send_buffer_n--;
3643 // cinfo->send_buffer_start++;
3644 // cinfo->send_buffer_start %= t->fwd_queue_max;
3648 // GNUNET_break (0);
3655 /* Free queue, but cls was freed by send_core_* */
3656 queue_destroy (queue, GNUNET_NO);
3658 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
3664 /* If more data in queue, send next */
3665 queue = queue_get_next(peer);
3668 struct GNUNET_PeerIdentity id;
3670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3671 GNUNET_PEER_resolve (peer->id, &id);
3672 peer->core_transmit =
3673 GNUNET_CORE_notify_transmit_ready(core_handle,
3676 GNUNET_TIME_UNIT_FOREVER_REL,
3684 if (NULL != peer->queue_head)
3686 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3687 "********* %s stalled\n",
3688 GNUNET_i2s(&my_full_id));
3689 // if (NULL == cinfo) FIXME
3690 // cinfo = tunnel_get_neighbor_fc (t, &dst_id);
3691 // // FIXME unify bck/fwd structures, bck does not have cinfo right now
3692 // if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
3694 // cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
3695 // &tunnel_poll, cinfo);
3699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3705 * @brief Queue and pass message to core when possible.
3707 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3708 * accounts for it. In case the queue is full, the message is dropped and
3711 * Otherwise, message is treated as internal and allowed to go regardless of
3714 * @param cls Closure (@c type dependant). It will be used by queue_send to
3715 * build the message to be sent if not already prebuilt.
3716 * @param type Type of the message, 0 for a raw message.
3717 * @param size Size of the message.
3718 * @param dst Neighbor to send message to.
3719 * @param t Tunnel this message belongs to.
3722 queue_add (void *cls, uint16_t type, size_t size,
3723 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3725 struct MeshPeerQueue *queue;
3726 struct GNUNET_PeerIdentity id;
3731 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3733 n = &t->fwd_queue_n;
3734 max = &t->fwd_queue_max;
3736 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3738 n = &t->bck_queue_n;
3739 max = &t->bck_queue_max;
3746 GNUNET_STATISTICS_update(stats,
3747 "# messages dropped (buffer full)",
3749 return; // Drop message
3753 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3759 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3760 if (NULL == dst->core_transmit)
3762 GNUNET_PEER_resolve (dst->id, &id);
3763 dst->core_transmit =
3764 GNUNET_CORE_notify_transmit_ready (core_handle,
3767 GNUNET_TIME_UNIT_FOREVER_REL,
3773 t->pending_messages++;
3777 /******************************************************************************/
3778 /******************** MESH NETWORK HANDLERS **************************/
3779 /******************************************************************************/
3783 * Core handler for path creation
3785 * @param cls closure
3786 * @param message message
3787 * @param peer peer identity this notification is about
3789 * @return GNUNET_OK to keep the connection open,
3790 * GNUNET_SYSERR to close it (signal serious error)
3793 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3794 const struct GNUNET_MessageHeader *message)
3796 unsigned int own_pos;
3799 MESH_TunnelNumber tid;
3800 struct GNUNET_MESH_ManipulatePath *msg;
3801 struct GNUNET_PeerIdentity *pi;
3802 struct GNUNET_HashCode hash;
3803 struct MeshPeerPath *path;
3804 struct MeshPeerInfo *dest_peer_info;
3805 struct MeshPeerInfo *orig_peer_info;
3806 struct MeshTunnel *t;
3808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3809 "Received a path create msg [%s]\n",
3810 GNUNET_i2s (&my_full_id));
3811 size = ntohs (message->size);
3812 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3814 GNUNET_break_op (0);
3818 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3819 if (size % sizeof (struct GNUNET_PeerIdentity))
3821 GNUNET_break_op (0);
3824 size /= sizeof (struct GNUNET_PeerIdentity);
3827 GNUNET_break_op (0);
3830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3831 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3833 tid = ntohl (msg->tid);
3834 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3836 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
3837 t = tunnel_get (pi, tid);
3838 if (NULL == t) // FIXME only for INCOMING tunnels?
3842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3843 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3846 // FIXME notify failure
3849 opt = ntohl (msg->opt);
3850 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
3852 t->nobuffer = GNUNET_YES;
3853 t->last_fwd_ack = t->fwd_pid + 1;
3855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3857 if (GNUNET_YES == t->nobuffer)
3859 t->bck_queue_max = 1;
3860 t->fwd_queue_max = 1;
3863 // FIXME only assign a local tid if a local client is interested (on demand)
3864 while (NULL != tunnel_get_incoming (next_local_tid))
3865 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3866 t->local_tid_dest = next_local_tid++;
3867 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3870 tunnel_reset_timeout (t);
3871 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3873 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
3874 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3882 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3883 if (NULL == dest_peer_info)
3885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3886 " Creating PeerInfo for destination.\n");
3887 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3888 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3889 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3891 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3893 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3894 if (NULL == orig_peer_info)
3896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3897 " Creating PeerInfo for origin.\n");
3898 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3899 orig_peer_info->id = GNUNET_PEER_intern (pi);
3900 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3901 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3904 path = path_new (size);
3906 for (i = 0; i < size; i++)
3908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3909 GNUNET_i2s (&pi[i]));
3910 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3911 if (path->peers[i] == myid)
3914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3917 /* cannot be self, must be 'not found' */
3918 /* create path: self not found in path through self */
3919 GNUNET_break_op (0);
3920 path_destroy (path);
3924 path_add_to_peers (path, GNUNET_NO);
3925 t->prev_hop = path->peers[own_pos - 1];
3926 GNUNET_PEER_change_rc (t->prev_hop, 1);
3927 if (own_pos == size - 1)
3929 /* It is for us! Send ack. */
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3931 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3932 t->peer = path->peers[0];
3937 struct MeshPeerPath *path2;
3939 t->next_hop = path->peers[own_pos + 1];
3940 GNUNET_PEER_change_rc(t->next_hop, 1);
3942 /* It's for somebody else! Retransmit. */
3943 path2 = path_duplicate (path);
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3945 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3946 path2 = path_duplicate (path);
3947 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3948 send_create_path (dest_peer_info, path, t);
3955 * Core handler for path destruction
3957 * @param cls closure
3958 * @param message message
3959 * @param peer peer identity this notification is about
3961 * @return GNUNET_OK to keep the connection open,
3962 * GNUNET_SYSERR to close it (signal serious error)
3965 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3966 const struct GNUNET_MessageHeader *message)
3968 struct GNUNET_MESH_ManipulatePath *msg;
3969 struct GNUNET_PeerIdentity *pi;
3970 struct MeshPeerPath *path;
3971 struct MeshTunnel *t;
3972 unsigned int own_pos;
3976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3977 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3978 size = ntohs (message->size);
3979 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3981 GNUNET_break_op (0);
3985 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3986 if (size % sizeof (struct GNUNET_PeerIdentity))
3988 GNUNET_break_op (0);
3991 size /= sizeof (struct GNUNET_PeerIdentity);
3994 GNUNET_break_op (0);
3997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3999 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4000 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4002 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4004 t = tunnel_get (pi, ntohl (msg->tid));
4007 /* TODO notify back: we don't know this tunnel */
4008 GNUNET_break_op (0);
4011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4012 path = path_new (size);
4014 for (i = 0; i < size; i++)
4016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4017 GNUNET_i2s (&pi[i]));
4018 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4019 if (path->peers[i] == myid)
4022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4023 if (own_pos < path->length - 1)
4024 send_prebuilt_message (message, &pi[own_pos + 1], t);
4026 send_client_tunnel_disconnect(t, NULL);
4028 // tunnel_delete_peer (t, path->peers[path->length - 1]); FIXME
4029 path_destroy (path);
4035 * Core handler for notifications of broken paths
4037 * @param cls closure
4038 * @param message message
4039 * @param peer peer identity this notification is about
4041 * @return GNUNET_OK to keep the connection open,
4042 * GNUNET_SYSERR to close it (signal serious error)
4045 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4046 const struct GNUNET_MessageHeader *message)
4048 struct GNUNET_MESH_PathBroken *msg;
4049 struct MeshTunnel *t;
4051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4052 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4053 msg = (struct GNUNET_MESH_PathBroken *) message;
4054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4055 GNUNET_i2s (&msg->peer1));
4056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4057 GNUNET_i2s (&msg->peer2));
4058 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4061 GNUNET_break_op (0);
4064 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4065 GNUNET_PEER_search (&msg->peer2));
4072 * Core handler for tunnel destruction
4074 * @param cls closure
4075 * @param message message
4076 * @param peer peer identity this notification is about
4078 * @return GNUNET_OK to keep the connection open,
4079 * GNUNET_SYSERR to close it (signal serious error)
4082 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4083 const struct GNUNET_MessageHeader *message)
4085 struct GNUNET_MESH_TunnelDestroy *msg;
4086 struct MeshTunnel *t;
4089 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4091 "Got a TUNNEL DESTROY packet from %s\n",
4093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4094 " for tunnel %s [%u]\n",
4095 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4096 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4097 /* Check signature */
4100 /* Probably already got the message from another path,
4101 * destroyed the tunnel and retransmitted to children.
4104 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
4108 pid = GNUNET_PEER_search (peer);
4109 if (pid != t->prev_hop && 0 < t->nclients)
4111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4112 "still in use by %u clients\n",
4116 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4118 /* Tunnel was incoming, notify clients */
4119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4120 t->local_tid, t->local_tid_dest);
4121 send_clients_tunnel_destroy (t);
4123 tunnel_send_destroy (t);
4124 t->destroy = GNUNET_YES;
4125 // TODO: add timeout to destroy the tunnel anyway
4131 * Core handler for mesh network traffic going from the origin to a peer
4133 * @param cls closure
4134 * @param peer peer identity this notification is about
4135 * @param message message
4136 * @return GNUNET_OK to keep the connection open,
4137 * GNUNET_SYSERR to close it (signal serious error)
4140 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4141 const struct GNUNET_MessageHeader *message)
4143 struct GNUNET_MESH_Unicast *msg;
4144 struct GNUNET_PeerIdentity neighbor;
4145 struct MeshTunnel *t;
4146 GNUNET_PEER_Id dest_id;
4151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4154 size = ntohs (message->size);
4156 sizeof (struct GNUNET_MESH_Unicast) +
4157 sizeof (struct GNUNET_MessageHeader))
4162 msg = (struct GNUNET_MESH_Unicast *) message;
4163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4164 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4166 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4169 /* TODO notify back: we don't know this tunnel */
4170 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4171 GNUNET_break_op (0);
4174 pid = ntohl (msg->pid);
4175 if (t->fwd_pid == pid)
4177 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
4178 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4179 " Already seen pid %u, DROPPING!\n", pid);
4184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4185 " pid %u not seen yet, forwarding\n", pid);
4190 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
4192 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4193 GNUNET_break_op (0);
4194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4195 "Received PID %u, ACK %u\n",
4196 pid, t->last_fwd_ack);
4200 tunnel_reset_timeout (t);
4202 if (dest_id == myid)
4204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4205 " it's for us! sending to clients...\n");
4206 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4207 send_subscribed_clients (message, &msg[1].header, t);
4208 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4211 ttl = ntohl (msg->ttl);
4212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4215 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4216 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4217 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4221 " not for us, retransmitting...\n");
4223 GNUNET_PEER_resolve (t->next_hop, &neighbor);
4225 /* cinfo->fwd_pid = pid; FIXME
4227 if (GNUNET_YES == t->nobuffer &&
4228 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
4230 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4231 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
4232 GNUNET_break_op (0);
4235 send_prebuilt_message (message, &neighbor, t);
4236 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4242 * Core handler for mesh network traffic toward the owner of a tunnel
4244 * @param cls closure
4245 * @param message message
4246 * @param peer peer identity this notification is about
4248 * @return GNUNET_OK to keep the connection open,
4249 * GNUNET_SYSERR to close it (signal serious error)
4252 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4253 const struct GNUNET_MessageHeader *message)
4255 struct GNUNET_MESH_ToOrigin *msg;
4256 struct GNUNET_PeerIdentity id;
4257 struct MeshPeerInfo *peer_info;
4258 struct MeshTunnel *t;
4262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4264 size = ntohs (message->size);
4265 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4266 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4268 GNUNET_break_op (0);
4271 msg = (struct GNUNET_MESH_ToOrigin *) message;
4272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4273 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4274 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4275 pid = ntohl (msg->pid);
4279 /* TODO notify that we dont know this tunnel (whom)? */
4280 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4282 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
4283 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4288 // if (cinfo->bck_pid == pid) FIXME
4290 // /* already seen this packet, drop */
4291 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
4292 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4293 // " Already seen pid %u, DROPPING!\n", pid);
4294 // tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4295 // return GNUNET_OK;
4298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4299 " pid %u not seen yet, forwarding\n", pid);
4300 // cinfo->bck_pid = pid; FIXME
4302 if (NULL != t->owner)
4305 struct GNUNET_MESH_ToOrigin *copy;
4307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4308 " it's for us! sending to clients...\n");
4309 /* TODO signature verification */
4310 memcpy (cbuf, message, size);
4311 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4312 copy->tid = htonl (t->local_tid);
4314 copy->pid = htonl (t->bck_pid);
4315 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4316 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4317 ©->header, GNUNET_NO);
4318 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4322 " not for us, retransmitting...\n");
4324 peer_info = peer_get (&msg->oid);
4325 if (NULL == peer_info)
4327 /* unknown origin of tunnel */
4331 if (0 == t->prev_hop)
4333 if (GNUNET_YES == t->destroy)
4335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4336 "to orig received on a dying tunnel %s [%X]\n",
4337 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4341 "unknown to origin at %s\n",
4342 GNUNET_i2s (&my_full_id));
4343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4346 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4347 "for tunnel %s [%X]\n",
4348 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4351 GNUNET_PEER_resolve (t->prev_hop, &id);
4352 send_prebuilt_message (message, &id, t);
4353 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4360 * Core handler for mesh network traffic point-to-point acks.
4362 * @param cls closure
4363 * @param message message
4364 * @param peer peer identity this notification is about
4366 * @return GNUNET_OK to keep the connection open,
4367 * GNUNET_SYSERR to close it (signal serious error)
4370 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4371 const struct GNUNET_MessageHeader *message)
4373 struct GNUNET_MESH_ACK *msg;
4374 struct MeshTunnel *t;
4377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4379 msg = (struct GNUNET_MESH_ACK *) message;
4381 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4385 /* TODO notify that we dont know this tunnel (whom)? */
4386 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4389 ack = ntohl (msg->pid);
4390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4392 /* Is this a forward or backward ACK? */
4393 if (t->prev_hop != GNUNET_PEER_search(peer))
4397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4398 // cinfo->fwd_ack = ack; FIXME
4399 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4400 peer_unlock_queue (t->next_hop);
4401 // if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll) FIXME
4403 // GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
4404 // cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
4405 // cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
4410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4412 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4413 peer_unlock_queue (t->prev_hop);
4420 * Core handler for mesh network traffic point-to-point ack polls.
4422 * @param cls closure
4423 * @param message message
4424 * @param peer peer identity this notification is about
4426 * @return GNUNET_OK to keep the connection open,
4427 * GNUNET_SYSERR to close it (signal serious error)
4430 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4431 const struct GNUNET_MessageHeader *message)
4433 struct GNUNET_MESH_Poll *msg;
4434 struct MeshTunnel *t;
4436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4439 msg = (struct GNUNET_MESH_Poll *) message;
4441 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4445 /* TODO notify that we dont know this tunnel (whom)? */
4446 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4447 GNUNET_break_op (0);
4451 /* Is this a forward or backward ACK? */
4452 if (t->prev_hop != GNUNET_PEER_search(peer))
4454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4455 /* FIXME cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send */
4456 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4461 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4468 * Core handler for path ACKs
4470 * @param cls closure
4471 * @param message message
4472 * @param peer peer identity this notification is about
4474 * @return GNUNET_OK to keep the connection open,
4475 * GNUNET_SYSERR to close it (signal serious error)
4478 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4479 const struct GNUNET_MessageHeader *message)
4481 struct GNUNET_MESH_PathACK *msg;
4482 struct GNUNET_PeerIdentity id;
4483 struct MeshPeerInfo *peer_info;
4484 struct MeshPeerPath *p;
4485 struct MeshTunnel *t;
4487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4488 GNUNET_i2s (&my_full_id));
4489 msg = (struct GNUNET_MESH_PathACK *) message;
4490 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4493 /* TODO notify that we don't know the tunnel */
4494 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4496 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4500 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4502 peer_info = peer_get (&msg->peer_id);
4503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4504 GNUNET_i2s (&msg->peer_id));
4505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4508 /* Add path to peers? */
4512 path_add_to_peers (p, GNUNET_YES);
4519 /* Message for us? */
4520 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4523 if (NULL == t->owner)
4525 GNUNET_break_op (0);
4528 if (NULL != peer_info->dhtget)
4530 GNUNET_DHT_get_stop (peer_info->dhtget);
4531 peer_info->dhtget = NULL;
4536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4537 " not for us, retransmitting...\n");
4538 GNUNET_PEER_resolve (t->prev_hop, &id);
4539 peer_info = peer_get (&msg->oid);
4540 send_prebuilt_message (message, &id, t);
4546 * Core handler for mesh keepalives.
4548 * @param cls closure
4549 * @param message message
4550 * @param peer peer identity this notification is about
4551 * @return GNUNET_OK to keep the connection open,
4552 * GNUNET_SYSERR to close it (signal serious error)
4554 * TODO: Check who we got this from, to validate route.
4557 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4558 const struct GNUNET_MessageHeader *message)
4560 struct GNUNET_MESH_TunnelKeepAlive *msg;
4561 struct MeshTunnel *t;
4563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4566 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4567 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4571 /* TODO notify that we dont know that tunnel */
4572 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4577 tunnel_reset_timeout (t);
4579 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4580 /* FIXME tunnel_send_multicast (t, message); */
4587 * Functions to handle messages from core
4589 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4590 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4591 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4592 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4593 sizeof (struct GNUNET_MESH_PathBroken)},
4594 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4595 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4596 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4597 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4598 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4599 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4600 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4601 sizeof (struct GNUNET_MESH_ACK)},
4602 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4603 sizeof (struct GNUNET_MESH_Poll)},
4604 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4605 sizeof (struct GNUNET_MESH_PathACK)},
4611 /******************************************************************************/
4612 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4613 /******************************************************************************/
4618 * notify_client_connection_failure: notify a client that the connection to the
4619 * requested remote peer is not possible (for instance, no route found)
4620 * Function called when the socket is ready to queue more data. "buf" will be
4621 * NULL and "size" zero if the socket was closed for writing in the meantime.
4623 * @param cls closure
4624 * @param size number of bytes available in buf
4625 * @param buf where the callee should write the message
4626 * @return number of bytes written to buf
4629 notify_client_connection_failure (void *cls, size_t size, void *buf)
4632 struct MeshPeerInfo *peer_info;
4633 struct GNUNET_MESH_PeerControl *msg;
4634 struct GNUNET_PeerIdentity id;
4636 if (0 == size && NULL == buf)
4638 // TODO retry? cancel?
4642 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4643 peer_info = (struct MeshPeerInfo *) cls;
4644 msg = (struct GNUNET_MESH_PeerControl *) buf;
4645 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4646 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4647 // msg->tunnel_id = htonl(peer_info->t->tid);
4648 GNUNET_PEER_resolve (peer_info->id, &id);
4649 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4657 * Send keepalive packets for a tunnel.
4659 * @param cls Closure (tunnel for which to send the keepalive).
4660 * @param tc Notification context.
4663 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4665 struct MeshTunnel *t = cls;
4666 struct GNUNET_MESH_TunnelKeepAlive *msg;
4667 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4670 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4671 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4677 "sending keepalive for tunnel %d\n", t->id.tid);
4679 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4680 msg->header.size = htons (size);
4681 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4682 msg->oid = my_full_id;
4683 msg->tid = htonl (t->id.tid);
4684 /* FIXME tunnel_send_multicast (t, &msg->header); */
4686 t->path_refresh_task =
4687 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4688 tunnel_reset_timeout(t);
4693 * Function to process paths received for a new peer addition. The recorded
4694 * paths form the initial tunnel, which can be optimized later.
4695 * Called on each result obtained for the DHT search.
4697 * @param cls closure
4698 * @param exp when will this value expire
4699 * @param key key of the result
4700 * @param get_path path of the get request
4701 * @param get_path_length lenght of get_path
4702 * @param put_path path of the put request
4703 * @param put_path_length length of the put_path
4704 * @param type type of the result
4705 * @param size number of bytes in data
4706 * @param data pointer to the result data
4708 * TODO: re-issue the request after certain time? cancel after X results?
4711 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4712 const struct GNUNET_HashCode * key,
4713 const struct GNUNET_PeerIdentity *get_path,
4714 unsigned int get_path_length,
4715 const struct GNUNET_PeerIdentity *put_path,
4716 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4717 size_t size, const void *data)
4719 struct MeshPathInfo *path_info = cls;
4720 struct MeshPeerPath *p;
4721 struct GNUNET_PeerIdentity pi;
4724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4725 GNUNET_PEER_resolve (path_info->peer->id, &pi);
4726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4728 p = path_build_from_dht (get_path, get_path_length,
4729 put_path, put_path_length);
4730 path_add_to_peers (p, GNUNET_NO);
4732 for (i = 0; i < path_info->peer->ntunnels; i++)
4734 peer_connect (path_info->peer, path_info->t);
4741 /******************************************************************************/
4742 /********************* MESH LOCAL HANDLES **************************/
4743 /******************************************************************************/
4747 * Handler for client disconnection
4749 * @param cls closure
4750 * @param client identification of the client; NULL
4751 * for the last call when the server is destroyed
4754 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4756 struct MeshClient *c;
4757 struct MeshClient *next;
4759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
4762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4769 if (c->handle != client)
4771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
4775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4777 GNUNET_SERVER_client_drop (c->handle);
4778 c->shutting_down = GNUNET_YES;
4779 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4780 &tunnel_destroy_iterator, c);
4781 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4782 &tunnel_destroy_iterator, c);
4783 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
4784 &tunnel_destroy_iterator, c);
4785 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4786 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4787 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
4789 if (NULL != c->types)
4790 GNUNET_CONTAINER_multihashmap_destroy (c->types);
4792 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4795 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4804 * Handler for new clients
4806 * @param cls closure
4807 * @param client identification of the client
4808 * @param message the actual message, which includes messages the client wants
4811 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4812 const struct GNUNET_MessageHeader *message)
4814 struct GNUNET_MESH_ClientConnect *cc_msg;
4815 struct MeshClient *c;
4821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
4823 /* Check data sanity */
4824 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4825 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4826 ntypes = ntohs (cc_msg->types);
4827 if (size != ntypes * sizeof (uint16_t))
4830 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4834 /* Create new client structure */
4835 c = GNUNET_malloc (sizeof (struct MeshClient));
4836 c->id = next_client_id++; // overflow not important: just for debug
4837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
4838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u types\n", ntypes);
4840 GNUNET_SERVER_client_keep (client);
4844 struct GNUNET_HashCode hc;
4846 t = (uint16_t *) &cc_msg[1];
4847 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
4848 for (i = 0; i < ntypes; i++)
4851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
4852 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc); // FIXME pseudo hash
4854 /* store in clients hashmap */
4855 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
4856 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4857 /* store in global hashmap */
4858 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
4859 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4863 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4864 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4865 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4866 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4867 GNUNET_SERVER_notification_context_add (nc, client);
4868 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4870 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4876 * Handler for requests of new tunnels
4878 * @param cls Closure.
4879 * @param client Identification of the client.
4880 * @param message The actual message.
4883 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4884 const struct GNUNET_MessageHeader *message)
4886 struct GNUNET_MESH_TunnelMessage *t_msg;
4887 struct MeshPeerInfo *peer_info;
4888 struct MeshTunnel *t;
4889 struct MeshClient *c;
4890 MESH_TunnelNumber tid;
4892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4894 /* Sanity check for client registration */
4895 if (NULL == (c = client_get (client)))
4898 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4903 /* Message sanity check */
4904 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4907 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4911 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4913 GNUNET_i2s (&t_msg->peer));
4914 /* Sanity check for tunnel numbering */
4915 tid = ntohl (t_msg->tunnel_id);
4916 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4919 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4922 /* Sanity check for duplicate tunnel IDs */
4923 if (NULL != tunnel_get_by_local_id (c, tid))
4926 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4930 while (NULL != tunnel_get_by_pi (myid, next_tid))
4931 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4932 t = tunnel_new (myid, next_tid, c, tid);
4933 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
4941 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4943 peer_info = peer_get (&t_msg->peer);
4944 GNUNET_array_append (peer_info->tunnels, peer_info->ntunnels, t);
4945 peer_connect (peer_info, t);
4946 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4952 * Handler for requests of deleting tunnels
4954 * @param cls closure
4955 * @param client identification of the client
4956 * @param message the actual message
4959 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4960 const struct GNUNET_MessageHeader *message)
4962 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4963 struct MeshClient *c;
4964 struct MeshTunnel *t;
4965 MESH_TunnelNumber tid;
4967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4968 "Got a DESTROY TUNNEL from client!\n");
4970 /* Sanity check for client registration */
4971 if (NULL == (c = client_get (client)))
4974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4979 /* Message sanity check */
4980 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4983 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4987 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4989 /* Retrieve tunnel */
4990 tid = ntohl (tunnel_msg->tunnel_id);
4991 t = tunnel_get_by_local_id(c, tid);
4994 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4999 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5001 client_ignore_tunnel (c, t);
5002 tunnel_destroy_empty (t);
5003 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5006 send_client_tunnel_disconnect (t, c);
5007 client_delete_tunnel (c, t);
5009 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5011 tunnel_send_destroy (t);
5012 t->destroy = GNUNET_YES;
5013 /* The tunnel will be destroyed when the last message is transmitted. */
5014 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5020 * Handler for requests of seeting tunnel's buffering policy.
5022 * @param cls Closure (unused).
5023 * @param client Identification of the client.
5024 * @param message The actual message.
5027 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
5028 const struct GNUNET_MessageHeader *message)
5030 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5031 struct MeshClient *c;
5032 struct MeshTunnel *t;
5033 MESH_TunnelNumber tid;
5035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5036 "Got a BUFFER request from client!\n");
5038 /* Sanity check for client registration */
5039 if (NULL == (c = client_get (client)))
5042 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5047 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5049 /* Retrieve tunnel */
5050 tid = ntohl (tunnel_msg->tunnel_id);
5051 t = tunnel_get_by_local_id(c, tid);
5054 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5056 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5060 switch (ntohs(message->type))
5062 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
5063 t->nobuffer = GNUNET_NO;
5065 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
5066 t->nobuffer = GNUNET_YES;
5072 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5077 * Handler for client traffic directed to one peer
5079 * @param cls closure
5080 * @param client identification of the client
5081 * @param message the actual message
5084 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
5085 const struct GNUNET_MessageHeader *message)
5087 struct MeshClient *c;
5088 struct MeshTunnel *t;
5089 struct GNUNET_MESH_Unicast *data_msg;
5090 MESH_TunnelNumber tid;
5093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5094 "Got a unicast request from a client!\n");
5096 /* Sanity check for client registration */
5097 if (NULL == (c = client_get (client)))
5100 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5105 data_msg = (struct GNUNET_MESH_Unicast *) message;
5107 /* Sanity check for message size */
5108 size = ntohs (message->size);
5109 if (sizeof (struct GNUNET_MESH_Unicast) +
5110 sizeof (struct GNUNET_MessageHeader) > size)
5113 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5117 /* Tunnel exists? */
5118 tid = ntohl (data_msg->tid);
5119 t = tunnel_get_by_local_id (c, tid);
5123 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5127 /* Is it a local tunnel? Then, does client own the tunnel? */
5128 if (t->owner->handle != client)
5131 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5135 /* PID should be as expected */
5136 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
5139 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5140 "Unicast PID, expected %u, got %u\n",
5141 t->fwd_pid + 1, ntohl (data_msg->pid));
5142 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5146 /* Ok, everything is correct, send the message
5147 * (pretend we got it from a mesh peer)
5150 /* Work around const limitation */
5151 char buf[ntohs (message->size)] GNUNET_ALIGN;
5152 struct GNUNET_MESH_Unicast *copy;
5154 copy = (struct GNUNET_MESH_Unicast *) buf;
5155 memcpy (buf, data_msg, size);
5156 copy->oid = my_full_id;
5157 copy->tid = htonl (t->id.tid);
5158 copy->ttl = htonl (default_ttl);
5159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5160 " calling generic handler...\n");
5161 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
5163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5164 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5171 * Handler for client traffic directed to the origin
5173 * @param cls closure
5174 * @param client identification of the client
5175 * @param message the actual message
5178 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
5179 const struct GNUNET_MessageHeader *message)
5181 struct GNUNET_MESH_ToOrigin *data_msg;
5182 struct MeshTunnelClientInfo *clinfo;
5183 struct MeshClient *c;
5184 struct MeshTunnel *t;
5185 MESH_TunnelNumber tid;
5188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5189 "Got a ToOrigin request from a client!\n");
5190 /* Sanity check for client registration */
5191 if (NULL == (c = client_get (client)))
5194 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5199 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
5201 /* Sanity check for message size */
5202 size = ntohs (message->size);
5203 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5204 sizeof (struct GNUNET_MessageHeader) > size)
5207 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5211 /* Tunnel exists? */
5212 tid = ntohl (data_msg->tid);
5213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5214 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5217 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5220 t = tunnel_get_by_local_id (c, tid);
5223 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5224 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5226 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5230 /* It should be sent by someone who has this as incoming tunnel. */
5231 if (GNUNET_NO == client_knows_tunnel (c, t))
5234 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5238 /* PID should be as expected */
5239 clinfo = tunnel_get_client_fc (t, c);
5240 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
5243 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5244 "To Origin PID, expected %u, got %u\n",
5245 clinfo->bck_pid + 1,
5246 ntohl (data_msg->pid));
5247 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5252 /* Ok, everything is correct, send the message
5253 * (pretend we got it from a mesh peer)
5256 char buf[ntohs (message->size)] GNUNET_ALIGN;
5257 struct GNUNET_MESH_ToOrigin *copy;
5259 /* Work around const limitation */
5260 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5261 memcpy (buf, data_msg, size);
5262 GNUNET_PEER_resolve (t->id.oid, ©->oid);
5263 copy->tid = htonl (t->id.tid);
5264 copy->ttl = htonl (default_ttl);
5265 copy->pid = htonl (t->bck_pid + 1);
5267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5268 " calling generic handler...\n");
5269 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
5271 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5278 * Handler for client's ACKs for payload traffic.
5280 * @param cls Closure (unused).
5281 * @param client Identification of the client.
5282 * @param message The actual message.
5285 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5286 const struct GNUNET_MessageHeader *message)
5288 struct GNUNET_MESH_LocalAck *msg;
5289 struct MeshTunnel *t;
5290 struct MeshClient *c;
5291 MESH_TunnelNumber tid;
5294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5295 /* Sanity check for client registration */
5296 if (NULL == (c = client_get (client)))
5299 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5304 msg = (struct GNUNET_MESH_LocalAck *) message;
5306 /* Tunnel exists? */
5307 tid = ntohl (msg->tunnel_id);
5308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5309 t = tunnel_get_by_local_id (c, tid);
5313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5314 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5319 ack = ntohl (msg->max_pid);
5320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
5322 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5323 if (NULL != t->owner && t->owner->handle == client)
5325 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5327 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5331 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5332 tunnel_set_client_fwd_ack (t, c, ack);
5333 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5336 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5344 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5346 * @param cls Closure (client handle).
5347 * @param key Key (hashed tunnel ID, unused).
5348 * @param value Tunnel info.
5350 * @return GNUNET_YES, to keep iterating.
5353 monitor_all_tunnels_iterator (void *cls,
5354 const struct GNUNET_HashCode * key,
5357 struct GNUNET_SERVER_Client *client = cls;
5358 struct MeshTunnel *t = value;
5359 struct GNUNET_MESH_LocalMonitor *msg;
5361 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5362 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5363 msg->tunnel_id = htonl (t->id.tid);
5364 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5365 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5366 GNUNET_PEER_resolve (t->peer, &msg->destination);
5368 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5369 "* sending info about tunnel %s [%u]\n",
5370 GNUNET_i2s (&msg->owner), t->id.tid);
5372 GNUNET_SERVER_notification_context_unicast (nc, client,
5373 &msg->header, GNUNET_NO);
5379 * Handler for client's MONITOR request.
5381 * @param cls Closure (unused).
5382 * @param client Identification of the client.
5383 * @param message The actual message.
5386 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5387 const struct GNUNET_MessageHeader *message)
5389 struct MeshClient *c;
5391 /* Sanity check for client registration */
5392 if (NULL == (c = client_get (client)))
5395 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5399 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5400 "Received get tunnels request from client %u\n",
5402 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5403 monitor_all_tunnels_iterator,
5405 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5406 "Get tunnels request from client %u completed\n",
5408 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5413 * Handler for client's MONITOR_TUNNEL request.
5415 * @param cls Closure (unused).
5416 * @param client Identification of the client.
5417 * @param message The actual message.
5420 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5421 const struct GNUNET_MessageHeader *message)
5423 const struct GNUNET_MESH_LocalMonitor *msg;
5424 struct GNUNET_MESH_LocalMonitor *resp;
5425 struct MeshClient *c;
5426 struct MeshTunnel *t;
5428 /* Sanity check for client registration */
5429 if (NULL == (c = client_get (client)))
5432 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5436 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5437 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5438 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5441 ntohl (msg->tunnel_id));
5442 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5445 /* We don't know the tunnel FIXME */
5446 struct GNUNET_MESH_LocalMonitor warn;
5449 GNUNET_SERVER_notification_context_unicast (nc, client,
5452 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5456 /* Initialize context */
5457 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5459 GNUNET_PEER_resolve (t->peer, &resp->destination);
5460 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5461 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5462 &resp->header, GNUNET_NO);
5465 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5466 "Monitor tunnel request from client %u completed\n",
5468 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5473 * Functions to handle messages from clients
5475 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5476 {&handle_local_new_client, NULL,
5477 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5478 {&handle_local_tunnel_create, NULL,
5479 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5480 sizeof (struct GNUNET_MESH_TunnelMessage)},
5481 {&handle_local_tunnel_destroy, NULL,
5482 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5483 sizeof (struct GNUNET_MESH_TunnelMessage)},
5484 {&handle_local_tunnel_buffer, NULL,
5485 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
5486 sizeof (struct GNUNET_MESH_TunnelMessage)},
5487 {&handle_local_tunnel_buffer, NULL,
5488 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
5489 sizeof (struct GNUNET_MESH_TunnelMessage)},
5490 {&handle_local_unicast, NULL,
5491 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5492 {&handle_local_to_origin, NULL,
5493 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5494 {&handle_local_ack, NULL,
5495 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5496 sizeof (struct GNUNET_MESH_LocalAck)},
5497 {&handle_local_get_tunnels, NULL,
5498 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5499 sizeof (struct GNUNET_MessageHeader)},
5500 {&handle_local_show_tunnel, NULL,
5501 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5502 sizeof (struct GNUNET_MESH_LocalMonitor)},
5508 * To be called on core init/fail.
5510 * @param cls service closure
5511 * @param server handle to the server for this service
5512 * @param identity the public identity of this peer
5515 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5516 const struct GNUNET_PeerIdentity *identity)
5519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5520 core_handle = server;
5521 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5524 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5525 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5527 GNUNET_i2s (identity));
5528 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5530 GNUNET_i2s (&my_full_id));
5531 GNUNET_SCHEDULER_shutdown (); // Try gracefully
5533 GNUNET_abort(); // Try harder
5540 * Method called whenever a given peer connects.
5542 * @param cls closure
5543 * @param peer peer identity this notification is about
5546 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5548 struct MeshPeerInfo *peer_info;
5549 struct MeshPeerPath *path;
5551 DEBUG_CONN ("Peer connected\n");
5552 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5553 peer_info = peer_get (peer);
5554 if (myid == peer_info->id)
5556 DEBUG_CONN (" (self)\n");
5561 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5563 path = path_new (2);
5564 path->peers[0] = myid;
5565 path->peers[1] = peer_info->id;
5566 GNUNET_PEER_change_rc (myid, 1);
5567 GNUNET_PEER_change_rc (peer_info->id, 1);
5568 peer_info_add_path (peer_info, path, GNUNET_YES);
5569 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5575 * Method called whenever a peer disconnects.
5577 * @param cls closure
5578 * @param peer peer identity this notification is about
5581 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5583 struct MeshPeerInfo *pi;
5584 struct MeshPeerQueue *q;
5585 struct MeshPeerQueue *n;
5587 DEBUG_CONN ("Peer disconnected\n");
5588 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5598 /* TODO try to reroute this traffic instead */
5599 queue_destroy(q, GNUNET_YES);
5602 if (NULL != pi->core_transmit)
5604 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5605 pi->core_transmit = NULL;
5607 peer_info_remove_path (pi, pi->id, myid);
5610 DEBUG_CONN (" (self)\n");
5612 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5617 /******************************************************************************/
5618 /************************ MAIN FUNCTIONS ****************************/
5619 /******************************************************************************/
5622 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5624 * @param cls closure
5625 * @param key current key code
5626 * @param value value in the hash map
5627 * @return GNUNET_YES if we should continue to iterate,
5631 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5633 struct MeshTunnel *t = value;
5640 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5642 * @param cls closure
5643 * @param key current key code
5644 * @param value value in the hash map
5645 * @return GNUNET_YES if we should continue to iterate,
5649 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5651 struct MeshPeerInfo *p = value;
5652 struct MeshPeerQueue *q;
5653 struct MeshPeerQueue *n;
5661 queue_destroy(q, GNUNET_YES);
5665 peer_info_destroy (p);
5671 * Task run during shutdown.
5677 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5681 if (core_handle != NULL)
5683 GNUNET_CORE_disconnect (core_handle);
5688 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
5691 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5692 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5693 if (dht_handle != NULL)
5695 GNUNET_DHT_disconnect (dht_handle);
5700 GNUNET_SERVER_notification_context_destroy (nc);
5703 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5705 GNUNET_SCHEDULER_cancel (announce_id_task);
5706 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5713 * Callback for hostkey read/generation.
5715 * @param cls Closure (Configuration handle).
5716 * @param pk The ECC private key.
5717 * @param emsg Error message, if any.
5720 key_generation_cb (void *cls,
5721 struct GNUNET_CRYPTO_EccPrivateKey *pk,
5724 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5725 struct MeshPeerInfo *peer;
5726 struct MeshPeerPath *p;
5731 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5732 _("Mesh service could not access hostkey: %s. Exiting.\n"),
5734 GNUNET_SCHEDULER_shutdown ();
5737 my_private_key = pk;
5738 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5739 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5740 &my_full_id.hashPubKey);
5741 myid = GNUNET_PEER_intern (&my_full_id);
5742 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5743 "Mesh for peer [%s] starting\n",
5744 GNUNET_i2s(&my_full_id));
5746 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5747 NULL, /* Closure passed to MESH functions */
5748 &core_init, /* Call core_init once connected */
5749 &core_connect, /* Handle connects */
5750 &core_disconnect, /* remove peers on disconnects */
5751 NULL, /* Don't notify about all incoming messages */
5752 GNUNET_NO, /* For header only in notification */
5753 NULL, /* Don't notify about all outbound messages */
5754 GNUNET_NO, /* For header-only out notification */
5755 core_handlers); /* Register these handlers */
5757 if (core_handle == NULL)
5760 GNUNET_SCHEDULER_shutdown ();
5765 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5768 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5769 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5770 GNUNET_SERVER_disconnect_notify (server_handle,
5771 &handle_local_client_disconnect, NULL);
5774 clients_head = NULL;
5775 clients_tail = NULL;
5778 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5780 /* Create a peer_info for the local peer */
5781 peer = peer_get (&my_full_id);
5784 GNUNET_PEER_change_rc (myid, 1);
5785 peer_info_add_path (peer, p, GNUNET_YES);
5786 GNUNET_SERVER_resume (server_handle);
5787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5792 * Process mesh requests.
5794 * @param cls closure
5795 * @param server the initialized server
5796 * @param c configuration to use
5799 run (void *cls, struct GNUNET_SERVER_Handle *server,
5800 const struct GNUNET_CONFIGURATION_Handle *c)
5804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5805 server_handle = server;
5808 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5811 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5813 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5814 "mesh", "peer/privatekey");
5815 GNUNET_SCHEDULER_shutdown ();
5820 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5821 &refresh_path_time))
5823 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5825 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5826 "mesh", "refresh path time");
5827 GNUNET_SCHEDULER_shutdown ();
5832 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5835 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5837 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5838 "mesh", "id announce time");
5839 GNUNET_SCHEDULER_shutdown ();
5844 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5849 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5850 "mesh", "connect timeout");
5851 GNUNET_SCHEDULER_shutdown ();
5856 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5859 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5861 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5862 "mesh", "max msgs queue");
5863 GNUNET_SCHEDULER_shutdown ();
5868 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5871 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5873 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5874 "mesh", "max tunnels");
5875 GNUNET_SCHEDULER_shutdown ();
5880 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5883 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5885 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5886 "mesh", "default ttl", 64);
5891 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5894 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5895 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5896 "mesh", "max peers", 1000);
5901 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5902 &dht_replication_level))
5904 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5906 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5907 "mesh", "dht replication level", 3);
5908 dht_replication_level = 3;
5911 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5912 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5913 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5914 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5916 dht_handle = GNUNET_DHT_connect (c, 64);
5917 if (NULL == dht_handle)
5921 stats = GNUNET_STATISTICS_create ("mesh", c);
5923 GNUNET_SERVER_suspend (server_handle);
5924 /* Scheduled the task to clean up when shutdown is called */
5925 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5927 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
5930 GNUNET_free (keyfile);
5935 * The main function for the mesh service.
5937 * @param argc number of arguments from the command line
5938 * @param argv command line arguments
5939 * @return 0 ok, 1 on error
5942 main (int argc, char *const *argv)
5947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5948 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5950 ret = (GNUNET_OK == r) ? 0 : 1;
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5955 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5956 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5957 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);