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 containing info about a queued transmission to this peer
119 struct MeshPeerQueue *next;
124 struct MeshPeerQueue *prev;
127 * Peer this transmission is directed to.
129 struct MeshPeerInfo *peer;
132 * Tunnel this message belongs to.
134 struct MeshTunnel *tunnel;
137 * Pointer to info stucture used as cls.
147 * Size of the message
154 * Struct containing all info possibly needed to build a package when called
157 struct MeshTransmissionDescriptor
159 /** ID of the tunnel this packet travels in */
160 struct MESH_TunnelID *origin;
162 /** Who was this message being sent to */
163 struct MeshPeerInfo *peer;
165 /** Ultimate destination of the packet */
166 GNUNET_PEER_Id destination;
168 /** Tunnel it belongs to. */
169 struct MeshTunnel *t;
171 /** Size of the data. */
180 * Struct containing all information regarding a given peer
190 * Last time we heard from this peer
192 struct GNUNET_TIME_Absolute last_contact;
195 * Number of attempts to reconnect so far
197 int n_reconnect_attempts;
200 * Paths to reach the peer, ordered by ascending hop count
202 struct MeshPeerPath *path_head;
205 * Paths to reach the peer, ordered by ascending hop count
207 struct MeshPeerPath *path_tail;
210 * Handle to stop the DHT search for a path to this peer
212 struct GNUNET_DHT_GetHandle *dhtget;
215 * Array of tunnels this peer is the target of.
216 * Most probably a small amount, therefore not a hashmap.
217 * When the path to the peer changes, notify these tunnels to let them
218 * re-adjust their path trees.
220 struct MeshTunnel **tunnels;
223 * Number of tunnels this peers participates in
225 unsigned int ntunnels;
228 * Transmission queue to core DLL head
230 struct MeshPeerQueue *queue_head;
233 * Transmission queue to core DLL tail
235 struct MeshPeerQueue *queue_tail;
238 * How many messages are in the queue to this peer.
240 unsigned int queue_n;
243 * Handle to for queued transmissions
245 struct GNUNET_CORE_TransmitHandle *core_transmit;
250 * Globally unique tunnel identification (owner + number)
251 * DO NOT USE OVER THE NETWORK
256 * Node that owns the tunnel
261 * Tunnel number to differentiate all the tunnels owned by the node oid
262 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
264 MESH_TunnelNumber tid;
269 * Struct containing all information regarding a tunnel
270 * For an intermediate node the improtant info used will be:
271 * - id Tunnel unique identification
272 * - paths[0] To know where to send it next
273 * - metainfo: ready, speeds, accounting
280 struct MESH_TunnelID id;
283 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
285 MESH_TunnelNumber local_tid;
288 * Local tunnel number for local destination clients (incoming number)
289 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
292 MESH_TunnelNumber local_tid_dest;
295 * Is the tunnel bufferless (minimum latency)?
300 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
305 * Packet ID of the last bck packet sent (unique counter per hop).
310 * Force sending ACK? Flag to allow duplicate ACK on POLL.
315 * Last ACK sent towards the origin (for traffic towards leaf node).
317 uint32_t last_fwd_ack;
320 * BCK ACK value received from the hop towards the owner of the tunnel,
321 * (previous node / owner): up to what message PID can we sent back to him.
326 * How many messages are in the forward queue (towards leaves).
328 unsigned int fwd_queue_n;
331 * How many messages do we accept in the forward queue.
333 unsigned int fwd_queue_max;
336 * How many messages are in the backward queue (towards origin).
338 unsigned int bck_queue_n;
341 * How many messages do we accept in the backward queue.
343 unsigned int bck_queue_max;
346 * Task to poll peer in case of a stall.
348 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
351 * Last time the tunnel was used
353 struct GNUNET_TIME_Absolute timestamp;
356 * Peer of the tunnel.
361 * Next hop in the tunnel.
363 GNUNET_PEER_Id next_hop;
366 * Previous hop in the tunnel.
368 GNUNET_PEER_Id prev_hop;
371 * Client owner of the tunnel, if any
373 struct MeshClient *owner;
376 * Clients that have been informed about and want to stay in the tunnel.
378 struct MeshClient **clients;
381 * Flow control info for each client.
383 struct MeshTunnelClientInfo *clients_fc;
386 * Number of elements in clients/clients_fc
388 unsigned int nclients;
391 * Clients that have been informed but requested to leave the tunnel.
393 struct MeshClient **ignore;
396 * Number of elements in clients
398 unsigned int nignore;
401 * Path being used for the tunnel.
403 struct MeshPeerPath *path;
406 * Task to keep the used paths alive
408 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
411 * Task to destroy the tunnel after timeout
413 * FIXME: merge the two? a tunnel will have either
414 * a path refresh OR a timeout, never both!
416 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
419 * Flag to signal the destruction of the tunnel.
420 * If this is set GNUNET_YES the tunnel will be destroyed
421 * when the queue is empty.
426 * Total messages pending for this tunnels, payload or not.
428 unsigned int pending_messages;
431 * If the tunnel is empty, destoy it.
433 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
438 * Info about a leaf client of a tunnel, needed to perform flow control.
440 struct MeshTunnelClientInfo
443 * PID of the last packet sent to the client (FWD).
448 * PID of the last packet received from the client (BCK).
453 * Maximum PID allowed (FWD ACK received).
458 * Last ACK sent to that child (BCK ACK).
465 * Struct containing information about a client of the service
472 struct MeshClient *next;
477 struct MeshClient *prev;
480 * Tunnels that belong to this client, indexed by local id
482 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
485 * Tunnels this client has accepted, indexed by incoming local id
487 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
490 * Tunnels this client has rejected, indexed by incoming local id
492 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
494 * Handle to communicate with the client
496 struct GNUNET_SERVER_Client *handle;
499 * Messages that this client has declared interest in
501 struct GNUNET_CONTAINER_MultiHashMap *types;
504 * Whether the client is active or shutting down (don't send confirmations
505 * to a client that is shutting down.
510 * ID of the client, mainly for debug messages
517 /******************************************************************************/
518 /************************ DEBUG FUNCTIONS ****************************/
519 /******************************************************************************/
523 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
524 * @param cls string to print
525 * @param success GNUNET_OK if the PUT was transmitted,
526 * GNUNET_NO on timeout,
527 * GNUNET_SYSERR on disconnect from service
528 * after the PUT message was transmitted
529 * (so we don't know if it was received or not)
534 mesh_debug (void *cls, int success)
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
542 unsigned int debug_fwd_ack;
543 unsigned int debug_bck_ack;
547 /******************************************************************************/
548 /*********************** GLOBAL VARIABLES ****************************/
549 /******************************************************************************/
551 /************************** Configuration parameters **************************/
554 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
556 static struct GNUNET_TIME_Relative refresh_path_time;
559 * How often to PUT own ID in the DHT.
561 static struct GNUNET_TIME_Relative id_announce_time;
564 * Maximum time allowed to connect to a peer found by string.
566 static struct GNUNET_TIME_Relative connect_timeout;
569 * Default TTL for payload packets.
571 static unsigned long long default_ttl;
574 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
576 static unsigned long long dht_replication_level;
579 * How many tunnels are we willing to maintain.
580 * Local tunnels are always allowed, even if there are more tunnels than max.
582 static unsigned long long max_tunnels;
585 * How many messages *in total* are we willing to queue, divided by number of
586 * tunnels to get tunnel queue size.
588 static unsigned long long max_msgs_queue;
591 * How many peers do we want to remember?
593 static unsigned long long max_peers;
596 /*************************** Static global variables **************************/
599 * Hostkey generation context
601 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
604 * DLL with all the clients, head.
606 static struct MeshClient *clients_head;
609 * DLL with all the clients, tail.
611 static struct MeshClient *clients_tail;
614 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
616 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
619 * Number of tunnels known.
621 static unsigned long long n_tunnels;
624 * Tunnels incoming, indexed by MESH_TunnelNumber
625 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
627 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
630 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
632 static struct GNUNET_CONTAINER_MultiHashMap *peers;
635 * Handle to communicate with transport
637 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
640 * Handle to communicate with core.
642 static struct GNUNET_CORE_Handle *core_handle;
647 static struct GNUNET_DHT_Handle *dht_handle;
652 static struct GNUNET_SERVER_Handle *server_handle;
655 * Handle to the statistics service.
657 static struct GNUNET_STATISTICS_Handle *stats;
660 * Notification context, to send messages to local clients.
662 static struct GNUNET_SERVER_NotificationContext *nc;
665 * Local peer own ID (memory efficient handle).
667 static GNUNET_PEER_Id myid;
670 * Local peer own ID (full value).
672 static struct GNUNET_PeerIdentity my_full_id;
677 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
682 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
685 * Tunnel ID for the next created tunnel (global tunnel number).
687 static MESH_TunnelNumber next_tid;
690 * Tunnel ID for the next incoming tunnel (local tunnel number).
692 static MESH_TunnelNumber next_local_tid;
695 * All message types clients of this peer are interested in.
697 static struct GNUNET_CONTAINER_MultiHashMap *types;
700 * Task to periodically announce itself in the network.
702 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
705 * Next ID to assign to a client.
707 unsigned int next_client_id;
710 /******************************************************************************/
711 /*********************** DECLARATIONS **************************/
712 /******************************************************************************/
715 * Function to process paths received for a new peer addition. The recorded
716 * paths form the initial tunnel, which can be optimized later.
717 * Called on each result obtained for the DHT search.
720 * @param exp when will this value expire
721 * @param key key of the result
722 * @param type type of the result
723 * @param size number of bytes in data
724 * @param data pointer to the result data
727 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
728 const struct GNUNET_HashCode * key,
729 const struct GNUNET_PeerIdentity *get_path,
730 unsigned int get_path_length,
731 const struct GNUNET_PeerIdentity *put_path,
732 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
733 size_t size, const void *data);
737 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
738 * and insert it in the appropiate structures if the peer is not known yet.
740 * @param peer Full identity of the peer.
742 * @return Existing or newly created peer info.
744 static struct MeshPeerInfo *
745 peer_get (const struct GNUNET_PeerIdentity *peer);
749 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
750 * and insert it in the appropiate structures if the peer is not known yet.
752 * @param peer Short identity of the peer.
754 * @return Existing or newly created peer info.
756 static struct MeshPeerInfo *
757 peer_get_short (const GNUNET_PEER_Id peer);
761 * Build a PeerPath from the paths returned from the DHT, reversing the paths
762 * to obtain a local peer -> destination path and interning the peer ids.
764 * @return Newly allocated and created path
766 static struct MeshPeerPath *
767 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
768 unsigned int get_path_length,
769 const struct GNUNET_PeerIdentity *put_path,
770 unsigned int put_path_length);
774 * Adds a path to the peer_infos of all the peers in the path
776 * @param p Path to process.
777 * @param confirmed Whether we know if the path works or not.
780 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
785 * Search for a tunnel by global ID using full PeerIdentities.
787 * @param oid owner of the tunnel.
788 * @param tid global tunnel number.
790 * @return tunnel handler, NULL if doesn't exist.
792 static struct MeshTunnel *
793 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
797 * Delete an active client from the tunnel.
803 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
806 * Notify a tunnel that a connection has broken that affects at least
809 * @param t Tunnel affected.
810 * @param p1 Peer that got disconnected from p2.
811 * @param p2 Peer that got disconnected from p1.
813 * @return Short ID of the peer disconnected (either p1 or p2).
814 * 0 if the tunnel remained unaffected.
816 static GNUNET_PEER_Id
817 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
822 * Get the current ack value for a tunnel, for data going from root to leaves,
823 * taking in account the tunnel mode and the status of all children and clients.
827 * @return Maximum PID allowed.
830 tunnel_get_fwd_ack (struct MeshTunnel *t);
834 * Add a client to a tunnel, initializing all needed data structures.
836 * @param t Tunnel to which add the client.
837 * @param c Client which to add to the tunnel.
840 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
844 * .Use the given path for the tunnel.
846 * @param t Tunnel to update.
847 * @param p Path to use.
850 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
853 * @brief Queue and pass message to core when possible.
855 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
856 * and accounts for it. In case the queue is full, the message is dropped and
859 * Otherwise, message is treated as internal and allowed to go regardless of
862 * @param cls Closure (@c type dependant). It will be used by queue_send to
863 * build the message to be sent if not already prebuilt.
864 * @param type Type of the message, 0 for a raw message.
865 * @param size Size of the message.
866 * @param dst Neighbor to send message to.
867 * @param t Tunnel this message belongs to.
870 queue_add (void *cls, uint16_t type, size_t size,
871 struct MeshPeerInfo *dst, struct MeshTunnel *t);
875 * Free a transmission that was already queued with all resources
876 * associated to the request.
878 * @param queue Queue handler to cancel.
879 * @param clear_cls Is it necessary to free associated cls?
882 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
886 * @brief Get the next transmittable message from the queue.
888 * This will be the head, except in the case of being a data packet
889 * not allowed by the destination peer.
891 * @param peer Destination peer.
893 * @return The next viable MeshPeerQueue element to send to that peer.
894 * NULL when there are no transmittable messages.
896 struct MeshPeerQueue *
897 queue_get_next (const struct MeshPeerInfo *peer);
901 * Core callback to write a queued packet to core buffer
903 * @param cls Closure (peer info).
904 * @param size Number of bytes available in buf.
905 * @param buf Where the to write the message.
907 * @return number of bytes written to buf
910 queue_send (void *cls, size_t size, void *buf);
913 /******************************************************************************/
914 /************************ PERIODIC FUNCTIONS ****************************/
915 /******************************************************************************/
918 * Periodically announce self id in the DHT
921 * @param tc task context
924 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
928 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
930 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
934 * - Set data expiration in function of X
937 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
939 block.id = my_full_id;
940 block.type = htonl (0);
941 GNUNET_DHT_put (dht_handle, /* DHT handle */
942 &my_full_id.hashPubKey, /* Key to use */
943 dht_replication_level, /* Replication level */
944 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
945 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
946 sizeof (block), /* Size of the data */
947 (const char *) &block, /* Data itself */
948 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
949 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
950 NULL, /* Continuation */
951 NULL); /* Continuation closure */
953 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
957 /******************************************************************************/
958 /****************** GENERAL HELPER FUNCTIONS ************************/
959 /******************************************************************************/
963 * Check if client has registered with the service and has not disconnected
965 * @param client the client to check
967 * @return non-NULL if client exists in the global DLL
969 static struct MeshClient *
970 client_get (struct GNUNET_SERVER_Client *client)
972 struct MeshClient *c;
977 if (c->handle == client)
986 * Checks if a given client has subscribed to certain message type
988 * @param message_type Type of message to check
989 * @param c Client to check
991 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
993 * FIXME: use of crypto_hash slows it down
994 * The hash function alone takes 8-10us out of the ~55us for the whole
995 * process of retransmitting the message from one local client to another.
996 * Find faster implementation!
999 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1001 struct GNUNET_HashCode hc;
1003 if (NULL == c->types)
1006 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1007 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1012 * Check whether client wants traffic from a tunnel.
1014 * @param c Client to check.
1015 * @param t Tunnel to be found.
1017 * @return GNUNET_YES if client knows tunnel.
1019 * TODO look in client hashmap
1022 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1026 for (i = 0; i < t->nclients; i++)
1027 if (t->clients[i] == c)
1034 * Check whether client has been informed about a tunnel.
1036 * @param c Client to check.
1037 * @param t Tunnel to be found.
1039 * @return GNUNET_YES if client knows tunnel.
1041 * TODO look in client hashmap
1044 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1048 for (i = 0; i < t->nignore; i++)
1049 if (t->ignore[i] == c)
1051 return client_wants_tunnel(c, t);
1056 * Marks a client as uninterested in traffic from the tunnel, updating both
1057 * client and tunnel to reflect this.
1059 * @param c Client that doesn't want traffic anymore.
1060 * @param t Tunnel which should be ignored.
1062 * FIXME when to delete an incoming tunnel?
1065 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1067 struct GNUNET_HashCode hash;
1069 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1070 GNUNET_break (GNUNET_YES ==
1071 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1073 GNUNET_break (GNUNET_YES ==
1074 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1075 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1076 tunnel_delete_active_client (t, c);
1077 GNUNET_array_append (t->ignore, t->nignore, c);
1082 * Deletes a tunnel from a client (either owner or destination). To be used on
1083 * tunnel destroy, otherwise, use client_ignore_tunnel.
1085 * @param c Client whose tunnel to delete.
1086 * @param t Tunnel which should be deleted.
1089 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1091 struct GNUNET_HashCode hash;
1095 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1096 GNUNET_assert (GNUNET_YES ==
1097 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1103 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1105 GNUNET_assert (GNUNET_YES ==
1106 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1110 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1118 * Send the message to all clients that have subscribed to its type
1120 * @param msg Pointer to the message itself
1121 * @param payload Pointer to the payload of the message.
1122 * @param t The tunnel to whose clients this message goes.
1124 * @return number of clients this message was sent to
1127 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1128 const struct GNUNET_MessageHeader *payload,
1129 struct MeshTunnel *t)
1131 struct MeshClient *c;
1132 MESH_TunnelNumber *tid;
1135 char cbuf[htons (msg->size)];
1137 type = ntohs (payload->type);
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1140 GNUNET_MESH_DEBUG_M2S (type));
1142 memcpy (cbuf, msg, sizeof (cbuf));
1143 switch (htons (msg->type))
1145 struct GNUNET_MESH_Unicast *uc;
1146 struct GNUNET_MESH_ToOrigin *to;
1148 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1149 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1152 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1153 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1161 for (count = 0, c = clients_head; c != NULL; c = c->next)
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1164 if (client_is_subscribed (type, c))
1166 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1170 *tid = htonl (t->local_tid);
1174 if (GNUNET_NO == client_knows_tunnel (c, t))
1176 /* This client doesn't know the tunnel */
1177 struct GNUNET_MESH_TunnelNotification tmsg;
1178 struct GNUNET_HashCode hash;
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1181 tmsg.header.size = htons (sizeof (tmsg));
1182 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1183 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1184 tmsg.tunnel_id = htonl (t->local_tid_dest);
1186 if (GNUNET_YES == t->nobuffer)
1187 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
1188 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1189 &tmsg.header, GNUNET_NO);
1190 tunnel_add_client (t, c);
1191 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1193 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1194 c->incoming_tunnels, &hash, t,
1195 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1197 *tid = htonl (t->local_tid_dest);
1200 /* Check if the client wants to get traffic from the tunnel */
1201 if (GNUNET_NO == client_wants_tunnel(c, t))
1204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1205 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1206 (struct GNUNET_MessageHeader
1207 *) cbuf, GNUNET_NO);
1216 * Notify all clients (not depending on registration status) that the incoming
1217 * tunnel is no longer valid.
1219 * @param t Tunnel that was destroyed.
1222 send_clients_tunnel_destroy (struct MeshTunnel *t)
1224 struct GNUNET_MESH_TunnelMessage msg;
1226 msg.header.size = htons (sizeof (msg));
1227 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1228 msg.tunnel_id = htonl (t->local_tid_dest);
1229 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1234 * Notify clients of tunnel disconnections, if needed.
1235 * In case the origin disconnects, the destination clients get a tunnel destroy
1236 * notification. If the last destination disconnects (only one remaining client
1237 * in tunnel), the origin gets a (local ID) peer disconnected.
1238 * Note that the function must be called BEFORE removing the client from
1241 * @param t Tunnel that was destroyed.
1242 * @param c Client that disconnected.
1245 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1251 struct GNUNET_MESH_TunnelMessage msg;
1253 msg.header.size = htons (sizeof (msg));
1254 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1255 msg.tunnel_id = htonl (t->local_tid_dest);
1256 for (i = 0; i < t->nclients; i++)
1257 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1258 &msg.header, GNUNET_NO);
1260 // FIXME when to disconnect an incoming tunnel?
1265 * Iterator over all the peers to remove the oldest not-used entry.
1267 * @param cls Closure (unsued).
1268 * @param key ID of the peer.
1269 * @param value Peer_Info of the peer.
1274 peer_info_timeout (void *cls,
1275 const struct GNUNET_HashCode *key,
1282 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1283 * and insert it in the appropiate structures if the peer is not known yet.
1285 * @param peer Full identity of the peer.
1287 * @return Existing or newly created peer info.
1289 static struct MeshPeerInfo *
1290 peer_get (const struct GNUNET_PeerIdentity *peer)
1292 struct MeshPeerInfo *peer_info;
1294 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1295 if (NULL == peer_info)
1298 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1299 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1301 GNUNET_CONTAINER_multihashmap_iterate (peers,
1305 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1306 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1307 peer_info->id = GNUNET_PEER_intern (peer);
1309 peer_info->last_contact = GNUNET_TIME_absolute_get();
1316 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1317 * and insert it in the appropiate structures if the peer is not known yet.
1319 * @param peer Short identity of the peer.
1321 * @return Existing or newly created peer info.
1323 static struct MeshPeerInfo *
1324 peer_get_short (const GNUNET_PEER_Id peer)
1326 struct GNUNET_PeerIdentity id;
1328 GNUNET_PEER_resolve (peer, &id);
1329 return peer_get (&id);
1334 * Choose the best path towards a peer considering the tunnel properties.
1336 * @param peer The destination peer.
1337 * @param t The tunnel the path is for.
1339 * @return Best current known path towards the peer, if any.
1341 static struct MeshPeerPath *
1342 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1344 struct MeshPeerPath *best_p;
1345 struct MeshPeerPath *p;
1346 unsigned int best_cost;
1349 best_p = p = peer->path_head;
1350 best_cost = cost = p->length;
1353 if ((cost = p->length) < best_cost)
1365 * Remove the tunnel from the list of tunnels to which a peer is target.
1367 * @param peer PeerInfo of the peer.
1368 * @param t Tunnel to remove.
1371 peer_remove_tunnel (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1375 for (i = 0; i < peer->ntunnels; i++)
1378 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1381 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1383 GNUNET_realloc (peer->tunnels,
1384 peer->ntunnels * sizeof(struct MeshTunnel *));
1392 * Core callback to write a pre-constructed data packet to core buffer
1394 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1395 * @param size Number of bytes available in buf.
1396 * @param buf Where the to write the message.
1398 * @return number of bytes written to buf
1401 send_core_data_raw (void *cls, size_t size, void *buf)
1403 struct MeshTransmissionDescriptor *info = cls;
1404 struct GNUNET_MessageHeader *msg;
1407 GNUNET_assert (NULL != info);
1408 msg = (struct GNUNET_MessageHeader *) info->data;
1409 total_size = ntohs (msg->size);
1411 if (total_size > size)
1416 memcpy (buf, msg, total_size);
1417 GNUNET_free (info->data);
1424 * Sends an already built non-multicast message to a peer,
1425 * properly registrating all used resources.
1427 * @param message Message to send. Function makes a copy of it.
1428 * @param peer Short ID of the neighbor whom to send the message.
1429 * @param t Tunnel on which this message is transmitted.
1432 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1433 const struct GNUNET_PeerIdentity *peer,
1434 struct MeshTunnel *t)
1436 struct MeshTransmissionDescriptor *info;
1437 struct MeshPeerInfo *neighbor;
1438 struct MeshPeerPath *p;
1442 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1444 size = ntohs (message->size);
1445 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1446 info->data = GNUNET_malloc (size);
1447 memcpy (info->data, message, size);
1448 type = ntohs(message->type);
1451 struct GNUNET_MESH_Unicast *m;
1452 struct GNUNET_MESH_ToOrigin *to;
1454 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1455 m = (struct GNUNET_MESH_Unicast *) info->data;
1456 m->ttl = htonl (ntohl (m->ttl) - 1);
1458 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1459 to = (struct GNUNET_MESH_ToOrigin *) info->data;
1461 to->pid = htonl(t->bck_pid);
1463 info->data_len = size;
1464 neighbor = peer_get (peer);
1465 for (p = neighbor->path_head; NULL != p; p = p->next)
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1476 " %s IS NOT DIRECTLY CONNECTED\n",
1478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1481 for (p = neighbor->path_head; NULL != p; p = p->next)
1483 struct GNUNET_PeerIdentity debug_id;
1486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1487 " path with %u hops through:\n",
1489 for (i = 0; i < p->length; i++)
1491 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1494 i, GNUNET_i2s(&debug_id));
1498 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1500 " no direct connection to %s\n",
1502 GNUNET_free (info->data);
1506 info->peer = neighbor;
1507 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1518 * Sends a CREATE PATH message for a path to a peer, properly registrating
1519 * all used resources.
1521 * @param peer PeerInfo of the final peer for whom this path is being created.
1522 * @param p Path itself.
1523 * @param t Tunnel for which the path is created.
1526 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1527 struct MeshTunnel *t)
1529 struct MeshPeerInfo *neighbor;
1537 neighbor = peer_get_short (t->next_hop);
1539 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1540 sizeof (struct GNUNET_MESH_ManipulatePath) +
1541 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1548 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1550 * @param t Tunnel which to confirm.
1553 send_path_ack (struct MeshTunnel *t)
1555 struct MeshTransmissionDescriptor *info;
1556 struct GNUNET_PeerIdentity id;
1558 GNUNET_PEER_resolve (t->prev_hop, &id);
1559 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1560 info->origin = &t->id;
1561 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1562 GNUNET_assert (NULL != info->peer);
1565 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1566 sizeof (struct GNUNET_MESH_PathACK),
1573 * Try to establish a new connection to this peer in the fiven tunnel.
1574 * If the peer doesn't have any path to it yet, try to get one.
1575 * If the peer already has some path, send a CREATE PATH towards it.
1577 * @param peer PeerInfo of the peer.
1578 * @param t Tunnel for which to create the path, if possible.
1581 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1583 struct MeshPeerPath *p;
1585 if (NULL != peer->path_head)
1587 p = peer_get_best_path (peer, t);
1588 tunnel_use_path (t, p);
1589 send_create_path (peer, p, t);
1591 else if (NULL == peer->dhtget)
1593 struct GNUNET_PeerIdentity id;
1595 GNUNET_PEER_resolve (peer->id, &id);
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1597 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1598 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1599 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1600 &id.hashPubKey, /* key to search */
1601 dht_replication_level, /* replication level */
1602 GNUNET_DHT_RO_RECORD_ROUTE |
1603 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1605 0, /* xquery bits */
1606 &dht_get_id_handler, peer);
1608 /* Otherwise, there is no path but the DHT get is already started. */
1613 * Destroy the peer_info and free any allocated resources linked to it
1615 * @param pi The peer_info to destroy.
1617 * @return GNUNET_OK on success
1620 peer_info_destroy (struct MeshPeerInfo *pi)
1622 struct GNUNET_PeerIdentity id;
1623 struct MeshPeerPath *p;
1624 struct MeshPeerPath *nextp;
1626 GNUNET_PEER_resolve (pi->id, &id);
1627 GNUNET_PEER_change_rc (pi->id, -1);
1630 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1633 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1634 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1636 if (NULL != pi->dhtget)
1638 GNUNET_DHT_get_stop (pi->dhtget);
1644 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1654 * Remove all paths that rely on a direct connection between p1 and p2
1655 * from the peer itself and notify all tunnels about it.
1657 * @param peer PeerInfo of affected peer.
1658 * @param p1 GNUNET_PEER_Id of one peer.
1659 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1662 * TODO: optimize (see below)
1665 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1668 struct MeshPeerPath *p;
1669 struct MeshPeerPath *next;
1670 struct MeshPeerInfo *peer_d;
1672 unsigned int destroyed;
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1677 for (p = peer->path_head; NULL != p; p = next)
1680 for (i = 0; i < (p->length - 1); i++)
1682 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1683 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1685 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1695 for (i = 0; i < peer->ntunnels; i++)
1697 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1701 peer_d = peer_get_short (d);
1702 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1703 tunnel_use_path (peer->tunnels[i], next);
1704 peer_connect (peer_d, peer->tunnels[i]);
1706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1711 * Add the path to the peer and update the path used to reach it in case this
1714 * @param peer_info Destination peer to add the path to.
1715 * @param path New path to add. Last peer must be the peer in arg 1.
1716 * Path will be either used of freed if already known.
1717 * @param trusted Do we trust that this path is real?
1720 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1723 struct MeshPeerPath *aux;
1727 if ((NULL == peer_info) || (NULL == path))
1730 path_destroy (path);
1733 if (path->peers[path->length - 1] != peer_info->id)
1736 path_destroy (path);
1739 if (path->length <= 2 && GNUNET_NO == trusted)
1741 /* Only allow CORE to tell us about direct paths */
1742 path_destroy (path);
1745 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1746 for (l = 1; l < path->length; l++)
1748 if (path->peers[l] == myid)
1750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1751 for (l2 = 0; l2 < path->length - l; l2++)
1753 path->peers[l2] = path->peers[l + l2];
1758 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1763 struct GNUNET_PeerIdentity id;
1765 GNUNET_PEER_resolve (peer_info->id, &id);
1766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1767 path->length, GNUNET_i2s (&id));
1770 l = path_get_length (path);
1777 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1778 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1780 l2 = path_get_length (aux);
1783 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1784 peer_info->path_tail, aux, path);
1789 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1791 path_destroy (path);
1796 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1803 * Add the path to the origin peer and update the path used to reach it in case
1804 * this is the shortest.
1805 * The path is given in peer_info -> destination, therefore we turn the path
1806 * upside down first.
1808 * @param peer_info Peer to add the path to, being the origin of the path.
1809 * @param path New path to add after being inversed.
1810 * @param trusted Do we trust that this path is real?
1813 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1814 struct MeshPeerPath *path, int trusted)
1817 peer_info_add_path (peer_info, path, trusted);
1822 * Function called if the connection to the peer has been stalled for a while,
1823 * possibly due to a missed ACK. Poll the peer about its ACK status.
1825 * @param cls Closure (cinfo).
1826 * @param tc TaskContext.
1829 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1831 // struct GNUNET_MESH_Poll msg;
1832 // struct GNUNET_PeerIdentity id;
1833 // struct MeshTunnel *t;
1835 // cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
1836 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1842 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1843 // msg.header.size = htons (sizeof (msg));
1844 // msg.tid = htonl (t->id.tid);
1845 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1846 // msg.last_ack = htonl (cinfo->fwd_ack);
1848 // GNUNET_PEER_resolve (cinfo->id, &id);
1849 // send_prebuilt_message (&msg.header, &id, cinfo->t);
1850 // cinfo->fc_poll_time = GNUNET_TIME_relative_min (
1851 // MESH_MAX_POLL_TIME,
1852 // GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
1853 // cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
1854 // &tunnel_poll, cinfo);
1859 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1860 * to obtain a local peer -> destination path and interning the peer ids.
1862 * @return Newly allocated and created path
1864 static struct MeshPeerPath *
1865 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1866 unsigned int get_path_length,
1867 const struct GNUNET_PeerIdentity *put_path,
1868 unsigned int put_path_length)
1870 struct MeshPeerPath *p;
1876 GNUNET_PEER_change_rc (myid, 1);
1877 i = get_path_length;
1878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1879 for (i--; i >= 0; i--)
1881 id = GNUNET_PEER_intern (&get_path[i]);
1882 if (p->length > 0 && id == p->peers[p->length - 1])
1884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1885 GNUNET_PEER_change_rc (id, -1);
1889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1890 GNUNET_i2s (&get_path[i]));
1892 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1893 p->peers[p->length - 1] = id;
1896 i = put_path_length;
1897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1898 for (i--; i >= 0; i--)
1900 id = GNUNET_PEER_intern (&put_path[i]);
1903 /* PUT path went through us, so discard the path up until now and start
1904 * from here to get a much shorter (and loop-free) path.
1909 if (p->length > 0 && id == p->peers[p->length - 1])
1911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1912 GNUNET_PEER_change_rc (id, -1);
1916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1917 GNUNET_i2s (&put_path[i]));
1919 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1920 p->peers[p->length - 1] = id;
1924 if (get_path_length > 0)
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1926 GNUNET_i2s (&get_path[0]));
1927 if (put_path_length > 0)
1928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1929 GNUNET_i2s (&put_path[0]));
1930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1932 for (i = 0; i < p->length; i++)
1934 struct GNUNET_PeerIdentity peer_id;
1936 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1938 GNUNET_i2s (&peer_id));
1946 * Adds a path to the peer_infos of all the peers in the path
1948 * @param p Path to process.
1949 * @param confirmed Whether we know if the path works or not.
1952 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1956 /* TODO: invert and add */
1957 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1958 for (i++; i < p->length; i++)
1960 struct MeshPeerInfo *aux;
1961 struct MeshPeerPath *copy;
1963 aux = peer_get_short (p->peers[i]);
1964 copy = path_duplicate (p);
1965 copy->length = i + 1;
1966 peer_info_add_path (aux, copy, GNUNET_NO);
1972 * Send keepalive packets for a peer
1974 * @param cls Closure (tunnel for which to send the keepalive).
1975 * @param tc Notification context.
1977 * TODO: implement explicit multicast keepalive?
1980 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1984 * Search for a tunnel among the incoming tunnels
1986 * @param tid the local id of the tunnel
1988 * @return tunnel handler, NULL if doesn't exist
1990 static struct MeshTunnel *
1991 tunnel_get_incoming (MESH_TunnelNumber tid)
1993 struct GNUNET_HashCode hash;
1995 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1996 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1997 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2002 * Search for a tunnel among the tunnels for a client
2004 * @param c the client whose tunnels to search in
2005 * @param tid the local id of the tunnel
2007 * @return tunnel handler, NULL if doesn't exist
2009 static struct MeshTunnel *
2010 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2012 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2014 return tunnel_get_incoming (tid);
2018 struct GNUNET_HashCode hash;
2020 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2021 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2027 * Search for a tunnel by global ID using PEER_ID
2029 * @param pi owner of the tunnel
2030 * @param tid global tunnel number
2032 * @return tunnel handler, NULL if doesn't exist
2034 static struct MeshTunnel *
2035 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2037 struct MESH_TunnelID id;
2038 struct GNUNET_HashCode hash;
2043 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2044 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2049 * Search for a tunnel by global ID using full PeerIdentities
2051 * @param oid owner of the tunnel
2052 * @param tid global tunnel number
2054 * @return tunnel handler, NULL if doesn't exist
2056 static struct MeshTunnel *
2057 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2059 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2064 * Delete an active client from the tunnel.
2070 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2074 for (i = 0; i < t->nclients; i++)
2076 if (t->clients[i] == c)
2078 t->clients[i] = t->clients[t->nclients - 1];
2079 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2080 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2082 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2090 * Delete an ignored client from the tunnel.
2096 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2100 for (i = 0; i < t->nignore; i++)
2102 if (t->ignore[i] == c)
2104 t->ignore[i] = t->ignore[t->nignore - 1];
2105 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2113 * Delete a client from the tunnel. It should be only done on
2114 * client disconnection, otherwise use client_ignore_tunnel.
2120 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2122 tunnel_delete_ignored_client (t, c);
2123 tunnel_delete_active_client (t, c);
2128 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
2130 struct MeshTunnelClientInfo clinfo;
2132 GNUNET_array_append (t->clients, t->nclients, c);
2133 clinfo.fwd_ack = t->fwd_pid + 1;
2134 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
2135 clinfo.fwd_pid = t->fwd_pid;
2136 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
2138 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
2143 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
2147 for (i = 0; i < p->length; i++)
2149 if (p->peers[i] == myid)
2152 if (i > p->length - 1)
2158 if (i < p->length - 1)
2159 t->next_hop = p->peers[i + 1];
2163 t->prev_hop = p->peers[i - 1];
2167 if (NULL != t->path)
2168 path_destroy (t->path);
2169 t->path = path_duplicate (p);
2170 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2171 t->path_refresh_task =
2172 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
2177 * Notifies a tunnel that a connection has broken that affects at least
2178 * some of its peers. Sends a notification towards the root of the tree.
2179 * In case the peer is the owner of the tree, notifies the client that owns
2180 * the tunnel and tries to reconnect.
2182 * @param t Tunnel affected.
2183 * @param p1 Peer that got disconnected from p2.
2184 * @param p2 Peer that got disconnected from p1.
2186 * @return Short ID of the peer disconnected (either p1 or p2).
2187 * 0 if the tunnel remained unaffected.
2189 static GNUNET_PEER_Id
2190 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2193 // if (myid != p1 && myid != p2) FIXME
2198 // if (tree_get_predecessor (t->tree) != 0)
2200 // /* We are the peer still connected, notify owner of the disconnection. */
2201 // struct GNUNET_MESH_PathBroken msg;
2202 // struct GNUNET_PeerIdentity neighbor;
2204 // msg.header.size = htons (sizeof (msg));
2205 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2206 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2207 // msg.tid = htonl (t->id.tid);
2208 // msg.peer1 = my_full_id;
2209 // GNUNET_PEER_resolve (pid, &msg.peer2);
2210 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2211 // send_prebuilt_message (&msg.header, &neighbor, t);
2218 * Get the Flow Control info of a client.
2220 * @param t Tunnel on which to look.
2221 * @param c Client whose ACK to get.
2223 * @return ACK value.
2225 static struct MeshTunnelClientInfo *
2226 tunnel_get_client_fc (struct MeshTunnel *t,
2227 struct MeshClient *c)
2231 for (i = 0; i < t->nclients; i++)
2233 if (t->clients[i] != c)
2235 return &t->clients_fc[i];
2238 return NULL; // avoid compiler / coverity complaints
2243 * Set the FWD ACK value of a client in a particular tunnel.
2245 * @param t Tunnel affected.
2246 * @param c Client whose ACK to set.
2247 * @param ack ACK value.
2250 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
2251 struct MeshClient *c,
2256 for (i = 0; i < t->nclients; i++)
2258 if (t->clients[i] != c)
2260 t->clients_fc[i].fwd_ack = ack;
2268 * Get the highest ACK value of all clients in a particular tunnel,
2269 * according to the buffering/speed settings.
2271 * @param t Tunnel on which to look.
2273 * @return Corresponding ACK value (max uint32_t).
2274 * If no clients are suscribed, -1LL.
2277 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
2282 if (0 == t->nclients)
2284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2285 " tunnel has no clients, no FWD ACK\n");
2289 for (ack = -1LL, i = 0; i < t->nclients; i++)
2292 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack))
2294 ack = t->clients_fc[i].fwd_ack;
2298 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
2299 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
2301 return (uint32_t) ack;
2306 * Get the current fwd ack value for a tunnel, taking in account the tunnel
2307 * mode and the status of all children nodes.
2311 * @return Maximum PID allowed.
2314 tunnel_get_fwd_ack (struct MeshTunnel *t)
2318 uint32_t buffer_free;
2322 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
2323 client_ack = tunnel_get_clients_fwd_ack (t);
2324 if (GNUNET_YES == t->nobuffer)
2330 ack = count + buffer_free; // Overflow? OK!
2332 if (-1LL == client_ack)
2337 ack = GMC_max_pid ((uint32_t) client_ack, ack);
2339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2340 "c %u, bf %u, cl %lld, ACK: %u\n",
2341 count, buffer_free, client_ack, ack);
2347 * Build a local ACK message and send it to a local client.
2349 * @param t Tunnel on which to send the ACK.
2350 * @param c Client to whom send the ACK.
2351 * @param ack Value of the ACK.
2354 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
2356 struct GNUNET_MESH_LocalAck msg;
2358 msg.header.size = htons (sizeof (msg));
2359 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2360 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
2361 msg.max_pid = htonl (ack);
2362 GNUNET_SERVER_notification_context_unicast(nc,
2369 * Build an ACK message and queue it to send to the given peer.
2371 * @param t Tunnel on which to send the ACK.
2372 * @param peer Peer to whom send the ACK.
2373 * @param ack Value of the ACK.
2376 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
2378 struct GNUNET_MESH_ACK msg;
2380 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2381 msg.header.size = htons (sizeof (msg));
2382 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2383 msg.pid = htonl (ack);
2384 msg.tid = htonl (t->id.tid);
2386 send_prebuilt_message (&msg.header, peer, t);
2391 * Notify a the owner of a tunnel about how many more
2392 * payload packages will we accept on a given tunnel.
2394 * @param t Tunnel on which to send the ACK.
2397 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
2401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2402 "Sending client FWD ACK on tunnel %X\n",
2405 ack = tunnel_get_fwd_ack (t);
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
2408 if (t->last_fwd_ack == ack)
2410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
2414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
2415 t->last_fwd_ack = ack;
2416 send_local_ack (t, t->owner, ack);
2421 * Send an ACK informing the predecessor about the available buffer space.
2422 * In case there is no predecessor, inform the owning client.
2423 * If buffering is off, send only on behalf of children or self if endpoint.
2424 * If buffering is on, send when sent to children and buffer space is free.
2425 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2426 * the ACK itself goes "back" (towards root).
2428 * @param t Tunnel on which to send the ACK.
2429 * @param type Type of message that triggered the ACK transmission.
2432 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2434 struct GNUNET_PeerIdentity id;
2437 if (NULL != t->owner)
2439 tunnel_send_client_fwd_ack (t);
2442 /* Is it after unicast / multicast retransmission? */
2445 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2447 "ACK due to FWD DATA retransmission\n");
2448 if (GNUNET_YES == t->nobuffer)
2450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2454 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2455 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2457 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2458 t->force_ack = GNUNET_YES;
2464 /* Check if we need to transmit the ACK */
2465 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
2466 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
2467 GNUNET_NO == t->force_ack)
2469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2471 " t->qmax: %u, t->qn: %u\n",
2472 t->fwd_queue_max, t->fwd_queue_n);
2473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2474 " t->pid: %u, t->ack: %u\n",
2475 t->fwd_pid, t->last_fwd_ack);
2479 /* Ok, ACK might be necessary, what PID to ACK? */
2480 ack = tunnel_get_fwd_ack (t);
2482 /* If speed_min and not all children have ack'd, dont send yet */
2483 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
2485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
2489 t->last_fwd_ack = ack;
2490 GNUNET_PEER_resolve (t->prev_hop, &id);
2491 send_ack (t, &id, ack);
2493 t->force_ack = GNUNET_NO;
2498 * Send a child node a BCK ACK to allow him to send more to_origin data.
2501 * @param id Id of the child node.
2504 tunnel_send_child_bck_ack (struct MeshTunnel *t,
2507 struct GNUNET_PeerIdentity peer;
2508 uint32_t ack = 0; // FIXME
2510 GNUNET_PEER_resolve (id, &peer);
2511 // ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
2513 // if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
2515 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2516 // " Not sending ACK, not needed\n");
2519 // cinfo->bck_ack = ack;
2521 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2522 // " Sending BCK ACK %u (last sent: %u)\n",
2523 // ack, cinfo->bck_ack);
2524 send_ack (t, &peer, ack);
2529 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
2531 * Iterates over all clients and sends BCK ACKs to the ones that need it.
2533 * FIXME fc: what happens if we have 2 clients but q_size is 1?
2534 * - implement a size 1 buffer in each client_fc AND children_fc
2535 * to hold at least 1 message per "child".
2536 * problem: violates no buffer policy
2537 * - ack 0 and make "children" poll for transmission slots
2538 * problem: big overhead, extra latency even in low traffic
2541 * @param t Tunnel on which to send the BCK ACKs.
2544 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
2547 unsigned int tunnel_delta;
2549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
2551 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
2552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
2554 /* Find client whom to allow to send to origin (with lowest buffer space) */
2555 for (i = 0; i < t->nclients; i++)
2557 struct MeshTunnelClientInfo *clinfo;
2560 clinfo = &t->clients_fc[i];
2561 delta = clinfo->bck_ack - clinfo->bck_pid;
2562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
2563 t->clients[i]->id, delta);
2565 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
2566 (GNUNET_YES == t->nobuffer && 0 == delta))
2570 ack = clinfo->bck_pid;
2571 ack += t->nobuffer ? 1 : tunnel_delta;
2572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2573 " sending ack to client %u: %u\n",
2574 t->clients[i]->id, ack);
2575 send_local_ack (t, t->clients[i], ack);
2576 clinfo->bck_ack = ack;
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2581 " not sending ack to client %u (td %u, d %u)\n",
2582 t->clients[i]->id, tunnel_delta, delta);
2589 * Send an ACK informing the children nodes and destination clients about
2590 * the available buffer space.
2591 * If buffering is off, send only on behalf of root (can be self).
2592 * If buffering is on, send when sent to predecessor and buffer space is free.
2593 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2594 * the ACK itself goes "forward" (towards children/clients).
2596 * @param t Tunnel on which to send the ACK.
2597 * @param type Type of message that triggered the ACK transmission.
2600 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2603 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2604 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2605 /* Is it after data to_origin retransmission? */
2608 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2609 if (GNUNET_YES == t->nobuffer)
2611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2612 " Not sending ACK, nobuffer\n");
2616 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2617 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2619 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2620 t->force_ack = GNUNET_YES;
2626 tunnel_send_clients_bck_ack (t);
2627 tunnel_send_child_bck_ack (t, t->next_hop);
2628 t->force_ack = GNUNET_NO;
2633 * @brief Re-initiate traffic to this peer if necessary.
2635 * Check if there is traffic queued towards this peer
2636 * and the core transmit handle is NULL (traffic was stalled).
2637 * If so, call core tmt rdy.
2639 * @param peer_id Short ID of peer to which initiate traffic.
2642 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2644 struct MeshPeerInfo *peer;
2645 struct GNUNET_PeerIdentity id;
2646 struct MeshPeerQueue *q;
2649 peer = peer_get_short (peer_id);
2650 if (NULL != peer->core_transmit)
2653 q = queue_get_next(peer);
2656 /* Might br multicast traffic already sent to this particular peer but
2657 * not to other children in this tunnel.
2658 * This way t->queue_n would be > 0 but the queue of this particular peer
2664 GNUNET_PEER_resolve (peer->id, &id);
2665 peer->core_transmit =
2666 GNUNET_CORE_notify_transmit_ready(core_handle,
2669 GNUNET_TIME_UNIT_FOREVER_REL,
2679 * Send a message to all peers in this tunnel that the tunnel is no longer
2682 * @param t The tunnel whose peers to notify.
2685 tunnel_send_destroy (struct MeshTunnel *t)
2687 struct GNUNET_MESH_TunnelDestroy msg;
2688 struct GNUNET_PeerIdentity id;
2690 msg.header.size = htons (sizeof (msg));
2691 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2692 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2693 msg.tid = htonl (t->id.tid);
2694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2695 " sending tunnel destroy for tunnel: %s [%X]\n",
2696 GNUNET_i2s (&msg.oid), t->id.tid);
2698 if (0 != t->next_hop)
2700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2701 GNUNET_PEER_resolve (t->next_hop, &id);
2702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2703 " sending back to %s\n",
2705 send_prebuilt_message (&msg.header, &id, t);
2707 if (0 != t->prev_hop)
2709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2710 GNUNET_PEER_resolve (t->prev_hop, &id);
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2712 " sending back to %s\n",
2714 send_prebuilt_message (&msg.header, &id, t);
2720 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2722 * @param t Tunnel which to cancel.
2723 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2726 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2728 struct MeshPeerInfo *peer_info;
2729 struct MeshPeerQueue *pq;
2730 struct MeshPeerQueue *next;
2732 peer_info = peer_get_short (neighbor);
2733 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2736 if (pq->tunnel == t)
2738 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2739 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2741 // Should have been removed on destroy children
2744 queue_destroy (pq, GNUNET_YES);
2747 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2749 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2750 peer_info->core_transmit = NULL;
2756 * Destroy the tunnel and free any allocated resources linked to it.
2758 * @param t the tunnel to destroy
2760 * @return GNUNET_OK on success
2763 tunnel_destroy (struct MeshTunnel *t)
2765 struct MeshClient *c;
2766 struct GNUNET_HashCode hash;
2777 struct GNUNET_PeerIdentity id;
2779 GNUNET_PEER_resolve (t->id.oid, &id);
2780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2781 GNUNET_i2s (&id), t->id.tid);
2783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2787 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2788 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2796 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2798 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2805 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2806 for (i = 0; i < t->nclients; i++)
2810 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2816 for (i = 0; i < t->nignore; i++)
2820 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2827 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
2828 GNUNET_free_non_null (t->clients);
2829 GNUNET_free_non_null (t->ignore);
2830 GNUNET_free_non_null (t->clients_fc);
2832 peer_cancel_queues (t->next_hop, t);
2833 peer_cancel_queues (t->prev_hop, t);
2835 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2836 GNUNET_SCHEDULER_cancel (t->timeout_task);
2837 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2838 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2841 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2846 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
2849 * Tunnel is empty: destroy it.
2851 * @param cls Closure (Tunnel).
2852 * @param tc TaskContext.
2855 tunnel_destroy_empty_delayed (void *cls,
2856 const struct GNUNET_SCHEDULER_TaskContext *tc)
2858 struct MeshTunnel *t = cls;
2860 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
2861 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2864 if (0 != t->nclients)
2869 struct GNUNET_PeerIdentity id;
2871 GNUNET_PEER_resolve (t->id.oid, &id);
2872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2873 "executing destruction of empty tunnel %s [%X]\n",
2874 GNUNET_i2s (&id), t->id.tid);
2878 tunnel_send_destroy (t);
2879 if (0 == t->pending_messages)
2882 t->destroy = GNUNET_YES;
2887 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
2889 * @param t Tunnel to destroy if empty.
2892 tunnel_destroy_empty (struct MeshTunnel *t)
2894 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
2897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u %u\n",
2898 t->delayed_destroy, t->nclients);
2904 struct GNUNET_PeerIdentity id;
2906 GNUNET_PEER_resolve (t->id.oid, &id);
2907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2908 "scheduling destruction of empty tunnel %s [%X]\n",
2909 GNUNET_i2s (&id), t->id.tid);
2913 t->delayed_destroy =
2914 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
2915 &tunnel_destroy_empty_delayed,
2921 * Create a new tunnel
2923 * @param owner Who is the owner of the tunnel (short ID).
2924 * @param tid Tunnel Number of the tunnel.
2925 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2926 * @param local Tunnel Number for the tunnel, for the client point of view.
2928 * @return A new initialized tunnel. NULL on error.
2930 static struct MeshTunnel *
2931 tunnel_new (GNUNET_PEER_Id owner,
2932 MESH_TunnelNumber tid,
2933 struct MeshClient *client,
2934 MESH_TunnelNumber local)
2936 struct MeshTunnel *t;
2937 struct GNUNET_HashCode hash;
2939 if (n_tunnels >= max_tunnels && NULL == client)
2942 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2945 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
2946 t->bck_queue_max = t->fwd_queue_max;
2948 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
2949 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
2950 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
2951 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
2952 t->local_tid = local;
2954 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2956 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2958 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2959 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2966 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2973 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2975 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2976 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2980 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2991 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2992 * client when the client disconnects. If the client is not the owner, the
2993 * owner will get notified if no more clients are in the tunnel and the client
2994 * get removed from the tunnel's list.
2996 * @param cls closure (client that is disconnecting)
2997 * @param key the hash of the local tunnel id (used to access the hashmap)
2998 * @param value the value stored at the key (tunnel to destroy)
3000 * @return GNUNET_OK, keep iterating.
3003 tunnel_destroy_iterator (void *cls,
3004 const struct GNUNET_HashCode * key,
3007 struct MeshTunnel *t = value;
3008 struct MeshClient *c = cls;
3010 send_client_tunnel_disconnect (t, c);
3013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
3014 tunnel_delete_client (t, c);
3015 client_delete_tunnel (c, t);
3016 tunnel_destroy_empty (t);
3019 tunnel_send_destroy (t);
3021 t->destroy = GNUNET_YES;
3028 * Timeout function, destroys tunnel if called
3030 * @param cls Closure (tunnel to destroy).
3031 * @param tc TaskContext
3034 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3036 struct MeshTunnel *t = cls;
3037 struct GNUNET_PeerIdentity id;
3039 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3040 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3042 GNUNET_PEER_resolve(t->id.oid, &id);
3043 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3044 "Tunnel %s [%X] timed out. Destroying.\n",
3045 GNUNET_i2s(&id), t->id.tid);
3046 send_clients_tunnel_destroy (t);
3051 * Resets the tunnel timeout. Starts it if no timeout was running.
3053 * @param t Tunnel whose timeout to reset.
3055 * TODO use heap to improve efficiency of scheduler.
3058 tunnel_reset_timeout (struct MeshTunnel *t)
3060 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3061 GNUNET_SCHEDULER_cancel (t->timeout_task);
3063 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3064 (refresh_path_time, 4), &tunnel_timeout, t);
3068 /******************************************************************************/
3069 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3070 /******************************************************************************/
3073 * Function to send a create path packet to a peer.
3075 * @param cls closure
3076 * @param size number of bytes available in buf
3077 * @param buf where the callee should write the message
3078 * @return number of bytes written to buf
3081 send_core_path_create (void *cls, size_t size, void *buf)
3083 struct MeshTunnel *t = cls;
3084 struct GNUNET_MESH_ManipulatePath *msg;
3085 struct GNUNET_PeerIdentity *peer_ptr;
3086 struct MeshPeerPath *p = t->path;
3091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3093 sizeof (struct GNUNET_MESH_ManipulatePath) +
3094 p->length * sizeof (struct GNUNET_PeerIdentity);
3096 if (size < size_needed || NULL == buf)
3101 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3102 msg->header.size = htons (size_needed);
3103 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3104 msg->tid = ntohl (t->id.tid);
3107 if (GNUNET_YES == t->nobuffer)
3108 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3109 msg->opt = htonl(opt);
3112 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3113 for (i = 0; i < p->length; i++)
3115 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3119 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3125 * Creates a path ack message in buf and frees all unused resources.
3127 * @param cls closure (MeshTransmissionDescriptor)
3128 * @param size number of bytes available in buf
3129 * @param buf where the callee should write the message
3130 * @return number of bytes written to buf
3133 send_core_path_ack (void *cls, size_t size, void *buf)
3135 struct MeshTransmissionDescriptor *info = cls;
3136 struct GNUNET_MESH_PathACK *msg = buf;
3138 GNUNET_assert (NULL != info);
3139 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3144 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3145 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3146 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3147 msg->tid = htonl (info->origin->tid);
3148 msg->peer_id = my_full_id;
3151 /* TODO add signature */
3153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3154 return sizeof (struct GNUNET_MESH_PathACK);
3159 * Free a transmission that was already queued with all resources
3160 * associated to the request.
3162 * @param queue Queue handler to cancel.
3163 * @param clear_cls Is it necessary to free associated cls?
3166 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3168 struct MeshTransmissionDescriptor *dd;
3169 // unsigned int max;
3171 if (GNUNET_YES == clear_cls)
3173 switch (queue->type)
3175 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3176 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3177 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3179 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3180 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3181 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3182 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3183 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3185 " prebuilt message\n");
3186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3188 GNUNET_MESH_DEBUG_M2S(queue->type));
3190 GNUNET_free (dd->data);
3192 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3197 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3198 " type %s unknown!\n",
3199 GNUNET_MESH_DEBUG_M2S(queue->type));
3201 GNUNET_free_non_null (queue->cls);
3203 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3204 queue->peer->queue_tail,
3207 /* Delete from child_fc in the appropiate tunnel */
3208 // max = queue->tunnel->fwd_queue_max;
3209 // GNUNET_PEER_resolve (queue->peer->id, &id);
3210 // if (NULL != cinfo)
3212 // for (i = 0; i < cinfo->send_buffer_n; i++)
3214 // i2 = (cinfo->send_buffer_start + i) % max;
3215 // if (cinfo->send_buffer[i2] == queue)
3217 // /* Found corresponding entry in the send_buffer. Move all others back. */
3221 // for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
3223 // j2 = (cinfo->send_buffer_start + j) % max;
3224 // j3 = (cinfo->send_buffer_start + j + 1) % max;
3225 // cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
3228 // cinfo->send_buffer[j3] = NULL;
3229 // cinfo->send_buffer_n--;
3234 GNUNET_free (queue);
3239 * @brief Get the next transmittable message from the queue.
3241 * This will be the head, except in the case of being a data packet
3242 * not allowed by the destination peer.
3244 * @param peer Destination peer.
3246 * @return The next viable MeshPeerQueue element to send to that peer.
3247 * NULL when there are no transmittable messages.
3249 struct MeshPeerQueue *
3250 queue_get_next (const struct MeshPeerInfo *peer)
3252 struct MeshPeerQueue *q;
3254 struct MeshTransmissionDescriptor *info;
3255 // struct GNUNET_MESH_Unicast *ucast;
3256 // struct GNUNET_MESH_ToOrigin *to_orig;
3257 struct GNUNET_PeerIdentity id;
3259 // uint32_t ack; FIXME
3261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
3262 for (q = peer->queue_head; NULL != q; q = q->next)
3266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3268 GNUNET_MESH_DEBUG_M2S(q->type));
3271 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3272 // ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
3273 // pid = ntohl (ucast->pid);
3274 GNUNET_PEER_resolve (info->peer->id, &id);
3275 // ack = cinfo->fwd_ack;
3277 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3278 // to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
3279 // pid = ntohl (to_orig->pid);
3280 // ack = t->bck_ack;
3283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3287 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3288 // "********* ACK: %u, PID: %u\n",
3290 // if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
3292 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3293 // "********* OK!\n");
3298 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3299 // "********* NEXT!\n");
3302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3303 "********* nothing found\n");
3309 queue_send (void *cls, size_t size, void *buf)
3311 struct MeshPeerInfo *peer = cls;
3312 struct GNUNET_MessageHeader *msg;
3313 struct MeshPeerQueue *queue;
3314 struct MeshTunnel *t;
3315 struct GNUNET_PeerIdentity dst_id;
3318 peer->core_transmit = NULL;
3320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3321 queue = queue_get_next (peer);
3323 /* Queue has no internal mesh traffic nor sendable payload */
3326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
3327 if (NULL == peer->queue_head)
3328 GNUNET_break (0); // Should've been canceled
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3333 GNUNET_PEER_resolve (peer->id, &dst_id);
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3335 "********* towards %s\n",
3336 GNUNET_i2s(&dst_id));
3337 /* Check if buffer size is enough for the message */
3338 if (queue->size > size)
3340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3341 "********* not enough room, reissue\n");
3342 peer->core_transmit =
3343 GNUNET_CORE_notify_transmit_ready (core_handle,
3346 GNUNET_TIME_UNIT_FOREVER_REL,
3353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3356 GNUNET_assert (0 < t->pending_messages);
3357 t->pending_messages--;
3358 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
3361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3362 "********* unicast: t->q (%u/%u)\n",
3363 t->fwd_queue_n, t->fwd_queue_max);
3365 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
3368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
3372 switch (queue->type)
3375 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3376 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3377 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3378 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3379 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3380 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3382 "********* raw: %s\n",
3383 GNUNET_MESH_DEBUG_M2S (queue->type));
3385 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3386 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3387 data_size = send_core_data_raw (queue->cls, size, buf);
3388 msg = (struct GNUNET_MessageHeader *) buf;
3389 switch (ntohs (msg->type)) // Type of preconstructed message
3391 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3392 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3394 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3395 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3401 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3403 data_size = send_core_path_create (queue->cls, size, buf);
3405 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3407 data_size = send_core_path_ack (queue->cls, size, buf);
3411 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3412 "********* type unknown: %u\n",
3416 switch (queue->type)
3418 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3419 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3420 // if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
3422 // GNUNET_break (0);
3423 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3424 // "at pos %u (%p) != %p\n",
3425 // cinfo->send_buffer_start,
3426 // cinfo->send_buffer[cinfo->send_buffer_start],
3429 // if (cinfo->send_buffer_n > 0)
3431 // cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
3432 // cinfo->send_buffer_n--;
3433 // cinfo->send_buffer_start++;
3434 // cinfo->send_buffer_start %= t->fwd_queue_max;
3438 // GNUNET_break (0);
3445 /* Free queue, but cls was freed by send_core_* */
3446 queue_destroy (queue, GNUNET_NO);
3448 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
3454 /* If more data in queue, send next */
3455 queue = queue_get_next(peer);
3458 struct GNUNET_PeerIdentity id;
3460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3461 GNUNET_PEER_resolve (peer->id, &id);
3462 peer->core_transmit =
3463 GNUNET_CORE_notify_transmit_ready(core_handle,
3466 GNUNET_TIME_UNIT_FOREVER_REL,
3474 if (NULL != peer->queue_head)
3476 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3477 "********* %s stalled\n",
3478 GNUNET_i2s(&my_full_id));
3479 // if (NULL == cinfo) FIXME
3480 // cinfo = tunnel_get_neighbor_fc (t, &dst_id);
3481 // // FIXME unify bck/fwd structures, bck does not have cinfo right now
3482 // if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
3484 // cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
3485 // &tunnel_poll, cinfo);
3489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3495 * @brief Queue and pass message to core when possible.
3497 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3498 * accounts for it. In case the queue is full, the message is dropped and
3501 * Otherwise, message is treated as internal and allowed to go regardless of
3504 * @param cls Closure (@c type dependant). It will be used by queue_send to
3505 * build the message to be sent if not already prebuilt.
3506 * @param type Type of the message, 0 for a raw message.
3507 * @param size Size of the message.
3508 * @param dst Neighbor to send message to.
3509 * @param t Tunnel this message belongs to.
3512 queue_add (void *cls, uint16_t type, size_t size,
3513 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3515 struct MeshPeerQueue *queue;
3516 struct GNUNET_PeerIdentity id;
3521 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3523 n = &t->fwd_queue_n;
3524 max = &t->fwd_queue_max;
3526 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3528 n = &t->bck_queue_n;
3529 max = &t->bck_queue_max;
3536 GNUNET_STATISTICS_update(stats,
3537 "# messages dropped (buffer full)",
3539 return; // Drop message
3543 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3549 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3550 if (NULL == dst->core_transmit)
3552 GNUNET_PEER_resolve (dst->id, &id);
3553 dst->core_transmit =
3554 GNUNET_CORE_notify_transmit_ready (core_handle,
3557 GNUNET_TIME_UNIT_FOREVER_REL,
3563 t->pending_messages++;
3567 /******************************************************************************/
3568 /******************** MESH NETWORK HANDLERS **************************/
3569 /******************************************************************************/
3573 * Core handler for path creation
3575 * @param cls closure
3576 * @param message message
3577 * @param peer peer identity this notification is about
3579 * @return GNUNET_OK to keep the connection open,
3580 * GNUNET_SYSERR to close it (signal serious error)
3583 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3584 const struct GNUNET_MessageHeader *message)
3586 unsigned int own_pos;
3589 MESH_TunnelNumber tid;
3590 struct GNUNET_MESH_ManipulatePath *msg;
3591 struct GNUNET_PeerIdentity *pi;
3592 struct GNUNET_HashCode hash;
3593 struct MeshPeerPath *path;
3594 struct MeshPeerInfo *dest_peer_info;
3595 struct MeshPeerInfo *orig_peer_info;
3596 struct MeshTunnel *t;
3598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3599 "Received a path create msg [%s]\n",
3600 GNUNET_i2s (&my_full_id));
3601 size = ntohs (message->size);
3602 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3604 GNUNET_break_op (0);
3608 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3609 if (size % sizeof (struct GNUNET_PeerIdentity))
3611 GNUNET_break_op (0);
3614 size /= sizeof (struct GNUNET_PeerIdentity);
3617 GNUNET_break_op (0);
3620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3621 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3623 tid = ntohl (msg->tid);
3624 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3626 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
3627 t = tunnel_get (pi, tid);
3628 if (NULL == t) // FIXME only for INCOMING tunnels?
3632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3633 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3636 // FIXME notify failure
3639 opt = ntohl (msg->opt);
3640 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
3642 t->nobuffer = GNUNET_YES;
3643 t->last_fwd_ack = t->fwd_pid + 1;
3645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3647 if (GNUNET_YES == t->nobuffer)
3649 t->bck_queue_max = 1;
3650 t->fwd_queue_max = 1;
3653 // FIXME only assign a local tid if a local client is interested (on demand)
3654 while (NULL != tunnel_get_incoming (next_local_tid))
3655 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3656 t->local_tid_dest = next_local_tid++;
3657 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3660 tunnel_reset_timeout (t);
3661 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3663 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
3664 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3672 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3673 if (NULL == dest_peer_info)
3675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3676 " Creating PeerInfo for destination.\n");
3677 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3678 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3679 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3681 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3683 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3684 if (NULL == orig_peer_info)
3686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3687 " Creating PeerInfo for origin.\n");
3688 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3689 orig_peer_info->id = GNUNET_PEER_intern (pi);
3690 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3691 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3694 path = path_new (size);
3696 for (i = 0; i < size; i++)
3698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3699 GNUNET_i2s (&pi[i]));
3700 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3701 if (path->peers[i] == myid)
3704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3707 /* cannot be self, must be 'not found' */
3708 /* create path: self not found in path through self */
3709 GNUNET_break_op (0);
3710 path_destroy (path);
3714 path_add_to_peers (path, GNUNET_NO);
3715 t->prev_hop = path->peers[own_pos - 1];
3716 GNUNET_PEER_change_rc (t->prev_hop, 1);
3717 if (own_pos == size - 1)
3719 /* It is for us! Send ack. */
3720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3721 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3722 t->peer = path->peers[0];
3727 struct MeshPeerPath *path2;
3729 t->next_hop = path->peers[own_pos + 1];
3730 GNUNET_PEER_change_rc(t->next_hop, 1);
3732 /* It's for somebody else! Retransmit. */
3733 path2 = path_duplicate (path);
3734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3735 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3736 path2 = path_duplicate (path);
3737 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3738 send_create_path (dest_peer_info, path, t);
3745 * Core handler for path destruction
3747 * @param cls closure
3748 * @param message message
3749 * @param peer peer identity this notification is about
3751 * @return GNUNET_OK to keep the connection open,
3752 * GNUNET_SYSERR to close it (signal serious error)
3755 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3756 const struct GNUNET_MessageHeader *message)
3758 struct GNUNET_MESH_ManipulatePath *msg;
3759 struct GNUNET_PeerIdentity *pi;
3760 struct MeshPeerPath *path;
3761 struct MeshTunnel *t;
3762 unsigned int own_pos;
3766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3767 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3768 size = ntohs (message->size);
3769 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3771 GNUNET_break_op (0);
3775 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3776 if (size % sizeof (struct GNUNET_PeerIdentity))
3778 GNUNET_break_op (0);
3781 size /= sizeof (struct GNUNET_PeerIdentity);
3784 GNUNET_break_op (0);
3787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3789 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3790 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3792 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3794 t = tunnel_get (pi, ntohl (msg->tid));
3797 /* TODO notify back: we don't know this tunnel */
3798 GNUNET_break_op (0);
3801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3802 path = path_new (size);
3804 for (i = 0; i < size; i++)
3806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3807 GNUNET_i2s (&pi[i]));
3808 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3809 if (path->peers[i] == myid)
3812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3813 if (own_pos < path->length - 1)
3814 send_prebuilt_message (message, &pi[own_pos + 1], t);
3816 send_client_tunnel_disconnect(t, NULL);
3818 // tunnel_delete_peer (t, path->peers[path->length - 1]); FIXME
3819 path_destroy (path);
3825 * Core handler for notifications of broken paths
3827 * @param cls closure
3828 * @param message message
3829 * @param peer peer identity this notification is about
3831 * @return GNUNET_OK to keep the connection open,
3832 * GNUNET_SYSERR to close it (signal serious error)
3835 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3836 const struct GNUNET_MessageHeader *message)
3838 struct GNUNET_MESH_PathBroken *msg;
3839 struct MeshTunnel *t;
3841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3842 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3843 msg = (struct GNUNET_MESH_PathBroken *) message;
3844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3845 GNUNET_i2s (&msg->peer1));
3846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3847 GNUNET_i2s (&msg->peer2));
3848 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3851 GNUNET_break_op (0);
3854 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3855 GNUNET_PEER_search (&msg->peer2));
3862 * Core handler for tunnel destruction
3864 * @param cls closure
3865 * @param message message
3866 * @param peer peer identity this notification is about
3868 * @return GNUNET_OK to keep the connection open,
3869 * GNUNET_SYSERR to close it (signal serious error)
3872 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3873 const struct GNUNET_MessageHeader *message)
3875 struct GNUNET_MESH_TunnelDestroy *msg;
3876 struct MeshTunnel *t;
3879 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3881 "Got a TUNNEL DESTROY packet from %s\n",
3883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3884 " for tunnel %s [%u]\n",
3885 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3886 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3887 /* Check signature */
3890 /* Probably already got the message from another path,
3891 * destroyed the tunnel and retransmitted to children.
3894 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3898 pid = GNUNET_PEER_search (peer);
3899 if (pid != t->prev_hop && 0 < t->nclients)
3901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3902 "still in use by %u clients\n",
3906 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3908 /* Tunnel was incoming, notify clients */
3909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3910 t->local_tid, t->local_tid_dest);
3911 send_clients_tunnel_destroy (t);
3913 tunnel_send_destroy (t);
3914 t->destroy = GNUNET_YES;
3915 // TODO: add timeout to destroy the tunnel anyway
3921 * Core handler for mesh network traffic going from the origin to a peer
3923 * @param cls closure
3924 * @param peer peer identity this notification is about
3925 * @param message message
3926 * @return GNUNET_OK to keep the connection open,
3927 * GNUNET_SYSERR to close it (signal serious error)
3930 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3931 const struct GNUNET_MessageHeader *message)
3933 struct GNUNET_MESH_Unicast *msg;
3934 struct GNUNET_PeerIdentity neighbor;
3935 struct MeshTunnel *t;
3936 GNUNET_PEER_Id dest_id;
3941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3944 size = ntohs (message->size);
3946 sizeof (struct GNUNET_MESH_Unicast) +
3947 sizeof (struct GNUNET_MessageHeader))
3952 msg = (struct GNUNET_MESH_Unicast *) message;
3953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3954 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3956 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3959 /* TODO notify back: we don't know this tunnel */
3960 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3961 GNUNET_break_op (0);
3964 pid = ntohl (msg->pid);
3965 if (t->fwd_pid == pid)
3967 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3968 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3969 " Already seen pid %u, DROPPING!\n", pid);
3974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3975 " pid %u not seen yet, forwarding\n", pid);
3980 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
3982 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3983 GNUNET_break_op (0);
3984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3985 "Received PID %u, ACK %u\n",
3986 pid, t->last_fwd_ack);
3990 tunnel_reset_timeout (t);
3992 if (dest_id == myid)
3994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3995 " it's for us! sending to clients...\n");
3996 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3997 send_subscribed_clients (message, &msg[1].header, t);
3998 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4001 ttl = ntohl (msg->ttl);
4002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4005 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4006 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4007 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4011 " not for us, retransmitting...\n");
4013 GNUNET_PEER_resolve (t->next_hop, &neighbor);
4015 /* cinfo->fwd_pid = pid; FIXME
4017 if (GNUNET_YES == t->nobuffer &&
4018 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
4020 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4021 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
4022 GNUNET_break_op (0);
4025 send_prebuilt_message (message, &neighbor, t);
4026 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4032 * Core handler for mesh network traffic toward the owner of a tunnel
4034 * @param cls closure
4035 * @param message message
4036 * @param peer peer identity this notification is about
4038 * @return GNUNET_OK to keep the connection open,
4039 * GNUNET_SYSERR to close it (signal serious error)
4042 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4043 const struct GNUNET_MessageHeader *message)
4045 struct GNUNET_MESH_ToOrigin *msg;
4046 struct GNUNET_PeerIdentity id;
4047 struct MeshPeerInfo *peer_info;
4048 struct MeshTunnel *t;
4052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4054 size = ntohs (message->size);
4055 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4056 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4058 GNUNET_break_op (0);
4061 msg = (struct GNUNET_MESH_ToOrigin *) message;
4062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4063 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4064 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4065 pid = ntohl (msg->pid);
4069 /* TODO notify that we dont know this tunnel (whom)? */
4070 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4072 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
4073 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4078 // if (cinfo->bck_pid == pid) FIXME
4080 // /* already seen this packet, drop */
4081 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
4082 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4083 // " Already seen pid %u, DROPPING!\n", pid);
4084 // tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4085 // return GNUNET_OK;
4088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4089 " pid %u not seen yet, forwarding\n", pid);
4090 // cinfo->bck_pid = pid; FIXME
4092 if (NULL != t->owner)
4095 struct GNUNET_MESH_ToOrigin *copy;
4097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4098 " it's for us! sending to clients...\n");
4099 /* TODO signature verification */
4100 memcpy (cbuf, message, size);
4101 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4102 copy->tid = htonl (t->local_tid);
4104 copy->pid = htonl (t->bck_pid);
4105 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4106 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4107 ©->header, GNUNET_NO);
4108 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4112 " not for us, retransmitting...\n");
4114 peer_info = peer_get (&msg->oid);
4115 if (NULL == peer_info)
4117 /* unknown origin of tunnel */
4121 if (0 == t->prev_hop)
4123 if (GNUNET_YES == t->destroy)
4125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4126 "to orig received on a dying tunnel %s [%X]\n",
4127 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4131 "unknown to origin at %s\n",
4132 GNUNET_i2s (&my_full_id));
4133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4137 "for tunnel %s [%X]\n",
4138 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4141 GNUNET_PEER_resolve (t->prev_hop, &id);
4142 send_prebuilt_message (message, &id, t);
4143 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4150 * Core handler for mesh network traffic point-to-point acks.
4152 * @param cls closure
4153 * @param message message
4154 * @param peer peer identity this notification is about
4156 * @return GNUNET_OK to keep the connection open,
4157 * GNUNET_SYSERR to close it (signal serious error)
4160 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4161 const struct GNUNET_MessageHeader *message)
4163 struct GNUNET_MESH_ACK *msg;
4164 struct MeshTunnel *t;
4167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4169 msg = (struct GNUNET_MESH_ACK *) message;
4171 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4175 /* TODO notify that we dont know this tunnel (whom)? */
4176 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4179 ack = ntohl (msg->pid);
4180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4182 /* Is this a forward or backward ACK? */
4183 if (t->prev_hop != GNUNET_PEER_search(peer))
4187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4188 // cinfo->fwd_ack = ack; FIXME
4189 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4190 peer_unlock_queue (t->next_hop);
4191 // if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll) FIXME
4193 // GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
4194 // cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
4195 // cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
4200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4202 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4203 peer_unlock_queue (t->prev_hop);
4210 * Core handler for mesh network traffic point-to-point ack polls.
4212 * @param cls closure
4213 * @param message message
4214 * @param peer peer identity this notification is about
4216 * @return GNUNET_OK to keep the connection open,
4217 * GNUNET_SYSERR to close it (signal serious error)
4220 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4221 const struct GNUNET_MessageHeader *message)
4223 struct GNUNET_MESH_Poll *msg;
4224 struct MeshTunnel *t;
4226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4229 msg = (struct GNUNET_MESH_Poll *) message;
4231 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4235 /* TODO notify that we dont know this tunnel (whom)? */
4236 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4237 GNUNET_break_op (0);
4241 /* Is this a forward or backward ACK? */
4242 if (t->prev_hop != GNUNET_PEER_search(peer))
4244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4245 /* FIXME cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send */
4246 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4251 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4258 * Core handler for path ACKs
4260 * @param cls closure
4261 * @param message message
4262 * @param peer peer identity this notification is about
4264 * @return GNUNET_OK to keep the connection open,
4265 * GNUNET_SYSERR to close it (signal serious error)
4268 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4269 const struct GNUNET_MessageHeader *message)
4271 struct GNUNET_MESH_PathACK *msg;
4272 struct GNUNET_PeerIdentity id;
4273 struct MeshPeerInfo *peer_info;
4274 struct MeshPeerPath *p;
4275 struct MeshTunnel *t;
4277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4278 GNUNET_i2s (&my_full_id));
4279 msg = (struct GNUNET_MESH_PathACK *) message;
4280 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4283 /* TODO notify that we don't know the tunnel */
4284 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4286 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4290 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4292 peer_info = peer_get (&msg->peer_id);
4293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4294 GNUNET_i2s (&msg->peer_id));
4295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4298 /* Add path to peers? */
4302 path_add_to_peers (p, GNUNET_YES);
4309 /* Message for us? */
4310 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4313 if (NULL == t->owner)
4315 GNUNET_break_op (0);
4318 if (NULL != peer_info->dhtget)
4320 GNUNET_DHT_get_stop (peer_info->dhtget);
4321 peer_info->dhtget = NULL;
4326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4327 " not for us, retransmitting...\n");
4328 GNUNET_PEER_resolve (t->prev_hop, &id);
4329 peer_info = peer_get (&msg->oid);
4330 send_prebuilt_message (message, &id, t);
4336 * Core handler for mesh keepalives.
4338 * @param cls closure
4339 * @param message message
4340 * @param peer peer identity this notification is about
4341 * @return GNUNET_OK to keep the connection open,
4342 * GNUNET_SYSERR to close it (signal serious error)
4344 * TODO: Check who we got this from, to validate route.
4347 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4348 const struct GNUNET_MessageHeader *message)
4350 struct GNUNET_MESH_TunnelKeepAlive *msg;
4351 struct MeshTunnel *t;
4353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4356 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4357 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4361 /* TODO notify that we dont know that tunnel */
4362 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4367 tunnel_reset_timeout (t);
4369 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4370 /* FIXME tunnel_send_multicast (t, message); */
4377 * Functions to handle messages from core
4379 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4380 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4381 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4382 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4383 sizeof (struct GNUNET_MESH_PathBroken)},
4384 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4385 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4386 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4387 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4388 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4389 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4390 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4391 sizeof (struct GNUNET_MESH_ACK)},
4392 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4393 sizeof (struct GNUNET_MESH_Poll)},
4394 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4395 sizeof (struct GNUNET_MESH_PathACK)},
4401 /******************************************************************************/
4402 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4403 /******************************************************************************/
4408 * notify_client_connection_failure: notify a client that the connection to the
4409 * requested remote peer is not possible (for instance, no route found)
4410 * Function called when the socket is ready to queue more data. "buf" will be
4411 * NULL and "size" zero if the socket was closed for writing in the meantime.
4413 * @param cls closure
4414 * @param size number of bytes available in buf
4415 * @param buf where the callee should write the message
4416 * @return number of bytes written to buf
4419 notify_client_connection_failure (void *cls, size_t size, void *buf)
4422 struct MeshPeerInfo *peer_info;
4423 struct GNUNET_MESH_PeerControl *msg;
4424 struct GNUNET_PeerIdentity id;
4426 if (0 == size && NULL == buf)
4428 // TODO retry? cancel?
4432 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4433 peer_info = (struct MeshPeerInfo *) cls;
4434 msg = (struct GNUNET_MESH_PeerControl *) buf;
4435 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4436 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4437 // msg->tunnel_id = htonl(peer_info->t->tid);
4438 GNUNET_PEER_resolve (peer_info->id, &id);
4439 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4447 * Send keepalive packets for a tunnel.
4449 * @param cls Closure (tunnel for which to send the keepalive).
4450 * @param tc Notification context.
4453 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4455 struct MeshTunnel *t = cls;
4456 struct GNUNET_PeerIdentity id;
4457 struct GNUNET_MESH_TunnelKeepAlive *msg;
4458 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4461 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
4462 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4468 "sending keepalive for tunnel %d\n", t->id.tid);
4470 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4471 msg->header.size = htons (size);
4472 // FIXME change to tunnel keepalive
4473 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4474 msg->oid = my_full_id;
4475 msg->tid = htonl (t->id.tid);
4476 GNUNET_PEER_resolve (t->next_hop, &id);
4477 send_prebuilt_message (&msg->header, &id, t);
4479 t->path_refresh_task =
4480 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4481 tunnel_reset_timeout (t);
4486 * Function to process paths received for a new peer addition. The recorded
4487 * paths form the initial tunnel, which can be optimized later.
4488 * Called on each result obtained for the DHT search.
4490 * @param cls closure
4491 * @param exp when will this value expire
4492 * @param key key of the result
4493 * @param get_path path of the get request
4494 * @param get_path_length lenght of get_path
4495 * @param put_path path of the put request
4496 * @param put_path_length length of the put_path
4497 * @param type type of the result
4498 * @param size number of bytes in data
4499 * @param data pointer to the result data
4501 * TODO: re-issue the request after certain time? cancel after X results?
4504 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4505 const struct GNUNET_HashCode * key,
4506 const struct GNUNET_PeerIdentity *get_path,
4507 unsigned int get_path_length,
4508 const struct GNUNET_PeerIdentity *put_path,
4509 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4510 size_t size, const void *data)
4512 struct MeshPeerInfo *peer = cls;
4513 struct MeshPeerPath *p;
4514 struct GNUNET_PeerIdentity pi;
4517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4518 GNUNET_PEER_resolve (peer->id, &pi);
4519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4521 p = path_build_from_dht (get_path, get_path_length,
4522 put_path, put_path_length);
4523 path_add_to_peers (p, GNUNET_NO);
4525 for (i = 0; i < peer->ntunnels; i++)
4527 peer_connect (peer, peer->tunnels[i]); // FIXME add if
4534 /******************************************************************************/
4535 /********************* MESH LOCAL HANDLES **************************/
4536 /******************************************************************************/
4540 * Handler for client disconnection
4542 * @param cls closure
4543 * @param client identification of the client; NULL
4544 * for the last call when the server is destroyed
4547 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4549 struct MeshClient *c;
4550 struct MeshClient *next;
4552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
4555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4562 if (c->handle != client)
4564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
4568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4570 GNUNET_SERVER_client_drop (c->handle);
4571 c->shutting_down = GNUNET_YES;
4572 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4573 &tunnel_destroy_iterator, c);
4574 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4575 &tunnel_destroy_iterator, c);
4576 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
4577 &tunnel_destroy_iterator, c);
4578 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4579 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4580 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
4582 if (NULL != c->types)
4583 GNUNET_CONTAINER_multihashmap_destroy (c->types);
4585 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4588 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4597 * Handler for new clients
4599 * @param cls closure
4600 * @param client identification of the client
4601 * @param message the actual message, which includes messages the client wants
4604 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4605 const struct GNUNET_MessageHeader *message)
4607 struct GNUNET_MESH_ClientConnect *cc_msg;
4608 struct MeshClient *c;
4614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
4616 /* Check data sanity */
4617 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4618 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4619 ntypes = ntohs (cc_msg->types);
4620 if (size != ntypes * sizeof (uint16_t))
4623 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4627 /* Create new client structure */
4628 c = GNUNET_malloc (sizeof (struct MeshClient));
4629 c->id = next_client_id++; // overflow not important: just for debug
4630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
4631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u types\n", ntypes);
4633 GNUNET_SERVER_client_keep (client);
4637 struct GNUNET_HashCode hc;
4639 t = (uint16_t *) &cc_msg[1];
4640 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
4641 for (i = 0; i < ntypes; i++)
4644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
4645 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc); // FIXME pseudo hash
4647 /* store in clients hashmap */
4648 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
4649 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4650 /* store in global hashmap */
4651 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
4652 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4656 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4657 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4658 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4659 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4660 GNUNET_SERVER_notification_context_add (nc, client);
4661 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4663 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4669 * Handler for requests of new tunnels
4671 * @param cls Closure.
4672 * @param client Identification of the client.
4673 * @param message The actual message.
4676 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4677 const struct GNUNET_MessageHeader *message)
4679 struct GNUNET_MESH_TunnelMessage *t_msg;
4680 struct MeshPeerInfo *peer_info;
4681 struct MeshTunnel *t;
4682 struct MeshClient *c;
4683 MESH_TunnelNumber tid;
4685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4687 /* Sanity check for client registration */
4688 if (NULL == (c = client_get (client)))
4691 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4696 /* Message sanity check */
4697 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4700 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4704 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4706 GNUNET_i2s (&t_msg->peer));
4707 /* Sanity check for tunnel numbering */
4708 tid = ntohl (t_msg->tunnel_id);
4709 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4712 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4715 /* Sanity check for duplicate tunnel IDs */
4716 if (NULL != tunnel_get_by_local_id (c, tid))
4719 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4723 while (NULL != tunnel_get_by_pi (myid, next_tid))
4724 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4725 t = tunnel_new (myid, next_tid, c, tid);
4726 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4730 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
4734 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4736 peer_info = peer_get (&t_msg->peer);
4737 GNUNET_array_append (peer_info->tunnels, peer_info->ntunnels, t);
4738 peer_connect (peer_info, t);
4739 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4745 * Handler for requests of deleting tunnels
4747 * @param cls closure
4748 * @param client identification of the client
4749 * @param message the actual message
4752 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4753 const struct GNUNET_MessageHeader *message)
4755 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4756 struct MeshClient *c;
4757 struct MeshTunnel *t;
4758 MESH_TunnelNumber tid;
4760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4761 "Got a DESTROY TUNNEL from client!\n");
4763 /* Sanity check for client registration */
4764 if (NULL == (c = client_get (client)))
4767 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4772 /* Message sanity check */
4773 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4776 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4780 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4782 /* Retrieve tunnel */
4783 tid = ntohl (tunnel_msg->tunnel_id);
4784 t = tunnel_get_by_local_id(c, tid);
4787 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4789 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4792 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4794 client_ignore_tunnel (c, t);
4795 tunnel_destroy_empty (t);
4796 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4799 send_client_tunnel_disconnect (t, c);
4800 client_delete_tunnel (c, t);
4802 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4804 tunnel_send_destroy (t);
4805 peer_remove_tunnel (peer_get_short(t->peer), t);
4806 t->destroy = GNUNET_YES;
4807 /* The tunnel will be destroyed when the last message is transmitted. */
4808 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4814 * Handler for requests of seeting tunnel's buffering policy.
4816 * @param cls Closure (unused).
4817 * @param client Identification of the client.
4818 * @param message The actual message.
4821 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
4822 const struct GNUNET_MessageHeader *message)
4824 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4825 struct MeshClient *c;
4826 struct MeshTunnel *t;
4827 MESH_TunnelNumber tid;
4829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4830 "Got a BUFFER request from client!\n");
4832 /* Sanity check for client registration */
4833 if (NULL == (c = client_get (client)))
4836 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4841 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4843 /* Retrieve tunnel */
4844 tid = ntohl (tunnel_msg->tunnel_id);
4845 t = tunnel_get_by_local_id(c, tid);
4848 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4850 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4854 switch (ntohs(message->type))
4856 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
4857 t->nobuffer = GNUNET_NO;
4859 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
4860 t->nobuffer = GNUNET_YES;
4866 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4871 * Handler for client traffic directed to one peer
4873 * @param cls closure
4874 * @param client identification of the client
4875 * @param message the actual message
4878 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4879 const struct GNUNET_MessageHeader *message)
4881 struct MeshClient *c;
4882 struct MeshTunnel *t;
4883 struct GNUNET_MESH_Unicast *data_msg;
4884 MESH_TunnelNumber tid;
4887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4888 "Got a unicast request from a client!\n");
4890 /* Sanity check for client registration */
4891 if (NULL == (c = client_get (client)))
4894 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4899 data_msg = (struct GNUNET_MESH_Unicast *) message;
4901 /* Sanity check for message size */
4902 size = ntohs (message->size);
4903 if (sizeof (struct GNUNET_MESH_Unicast) +
4904 sizeof (struct GNUNET_MessageHeader) > size)
4907 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4911 /* Tunnel exists? */
4912 tid = ntohl (data_msg->tid);
4913 t = tunnel_get_by_local_id (c, tid);
4917 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4921 /* Is it a local tunnel? Then, does client own the tunnel? */
4922 if (t->owner->handle != client)
4925 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4929 /* PID should be as expected */
4930 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
4933 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4934 "Unicast PID, expected %u, got %u\n",
4935 t->fwd_pid + 1, ntohl (data_msg->pid));
4936 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4940 /* Ok, everything is correct, send the message
4941 * (pretend we got it from a mesh peer)
4944 /* Work around const limitation */
4945 char buf[ntohs (message->size)] GNUNET_ALIGN;
4946 struct GNUNET_MESH_Unicast *copy;
4948 copy = (struct GNUNET_MESH_Unicast *) buf;
4949 memcpy (buf, data_msg, size);
4950 copy->oid = my_full_id;
4951 copy->tid = htonl (t->id.tid);
4952 copy->ttl = htonl (default_ttl);
4953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4954 " calling generic handler...\n");
4955 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
4957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4958 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4965 * Handler for client traffic directed to the origin
4967 * @param cls closure
4968 * @param client identification of the client
4969 * @param message the actual message
4972 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4973 const struct GNUNET_MessageHeader *message)
4975 struct GNUNET_MESH_ToOrigin *data_msg;
4976 struct MeshTunnelClientInfo *clinfo;
4977 struct MeshClient *c;
4978 struct MeshTunnel *t;
4979 MESH_TunnelNumber tid;
4982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4983 "Got a ToOrigin request from a client!\n");
4984 /* Sanity check for client registration */
4985 if (NULL == (c = client_get (client)))
4988 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4993 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4995 /* Sanity check for message size */
4996 size = ntohs (message->size);
4997 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4998 sizeof (struct GNUNET_MessageHeader) > size)
5001 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5005 /* Tunnel exists? */
5006 tid = ntohl (data_msg->tid);
5007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5008 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5011 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5014 t = tunnel_get_by_local_id (c, tid);
5017 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5018 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5020 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5024 /* It should be sent by someone who has this as incoming tunnel. */
5025 if (GNUNET_NO == client_knows_tunnel (c, t))
5028 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5032 /* PID should be as expected */
5033 clinfo = tunnel_get_client_fc (t, c);
5034 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
5037 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5038 "To Origin PID, expected %u, got %u\n",
5039 clinfo->bck_pid + 1,
5040 ntohl (data_msg->pid));
5041 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5046 /* Ok, everything is correct, send the message
5047 * (pretend we got it from a mesh peer)
5050 char buf[ntohs (message->size)] GNUNET_ALIGN;
5051 struct GNUNET_MESH_ToOrigin *copy;
5053 /* Work around const limitation */
5054 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5055 memcpy (buf, data_msg, size);
5056 GNUNET_PEER_resolve (t->id.oid, ©->oid);
5057 copy->tid = htonl (t->id.tid);
5058 copy->ttl = htonl (default_ttl);
5059 copy->pid = htonl (t->bck_pid + 1);
5061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5062 " calling generic handler...\n");
5063 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
5065 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5072 * Handler for client's ACKs for payload traffic.
5074 * @param cls Closure (unused).
5075 * @param client Identification of the client.
5076 * @param message The actual message.
5079 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5080 const struct GNUNET_MessageHeader *message)
5082 struct GNUNET_MESH_LocalAck *msg;
5083 struct MeshTunnel *t;
5084 struct MeshClient *c;
5085 MESH_TunnelNumber tid;
5088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5089 /* Sanity check for client registration */
5090 if (NULL == (c = client_get (client)))
5093 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5098 msg = (struct GNUNET_MESH_LocalAck *) message;
5100 /* Tunnel exists? */
5101 tid = ntohl (msg->tunnel_id);
5102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5103 t = tunnel_get_by_local_id (c, tid);
5107 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5108 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5113 ack = ntohl (msg->max_pid);
5114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
5116 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5117 if (NULL != t->owner && t->owner->handle == client)
5119 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5121 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5125 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5126 tunnel_set_client_fwd_ack (t, c, ack);
5127 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5130 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5138 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5140 * @param cls Closure (client handle).
5141 * @param key Key (hashed tunnel ID, unused).
5142 * @param value Tunnel info.
5144 * @return GNUNET_YES, to keep iterating.
5147 monitor_all_tunnels_iterator (void *cls,
5148 const struct GNUNET_HashCode * key,
5151 struct GNUNET_SERVER_Client *client = cls;
5152 struct MeshTunnel *t = value;
5153 struct GNUNET_MESH_LocalMonitor *msg;
5155 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5156 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5157 msg->tunnel_id = htonl (t->id.tid);
5158 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5159 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5160 GNUNET_PEER_resolve (t->peer, &msg->destination);
5162 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5163 "* sending info about tunnel %s [%u]\n",
5164 GNUNET_i2s (&msg->owner), t->id.tid);
5166 GNUNET_SERVER_notification_context_unicast (nc, client,
5167 &msg->header, GNUNET_NO);
5173 * Handler for client's MONITOR request.
5175 * @param cls Closure (unused).
5176 * @param client Identification of the client.
5177 * @param message The actual message.
5180 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5181 const struct GNUNET_MessageHeader *message)
5183 struct MeshClient *c;
5185 /* Sanity check for client registration */
5186 if (NULL == (c = client_get (client)))
5189 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5193 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5194 "Received get tunnels request from client %u\n",
5196 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5197 monitor_all_tunnels_iterator,
5199 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5200 "Get tunnels request from client %u completed\n",
5202 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5207 * Handler for client's MONITOR_TUNNEL request.
5209 * @param cls Closure (unused).
5210 * @param client Identification of the client.
5211 * @param message The actual message.
5214 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5215 const struct GNUNET_MessageHeader *message)
5217 const struct GNUNET_MESH_LocalMonitor *msg;
5218 struct GNUNET_MESH_LocalMonitor *resp;
5219 struct MeshClient *c;
5220 struct MeshTunnel *t;
5222 /* Sanity check for client registration */
5223 if (NULL == (c = client_get (client)))
5226 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5230 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5231 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5232 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5235 ntohl (msg->tunnel_id));
5236 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5239 /* We don't know the tunnel FIXME */
5240 struct GNUNET_MESH_LocalMonitor warn;
5243 GNUNET_SERVER_notification_context_unicast (nc, client,
5246 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5250 /* Initialize context */
5251 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5253 GNUNET_PEER_resolve (t->peer, &resp->destination);
5254 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5255 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5256 &resp->header, GNUNET_NO);
5259 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5260 "Monitor tunnel request from client %u completed\n",
5262 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5267 * Functions to handle messages from clients
5269 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5270 {&handle_local_new_client, NULL,
5271 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5272 {&handle_local_tunnel_create, NULL,
5273 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5274 sizeof (struct GNUNET_MESH_TunnelMessage)},
5275 {&handle_local_tunnel_destroy, NULL,
5276 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5277 sizeof (struct GNUNET_MESH_TunnelMessage)},
5278 {&handle_local_tunnel_buffer, NULL,
5279 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
5280 sizeof (struct GNUNET_MESH_TunnelMessage)},
5281 {&handle_local_tunnel_buffer, NULL,
5282 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
5283 sizeof (struct GNUNET_MESH_TunnelMessage)},
5284 {&handle_local_unicast, NULL,
5285 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5286 {&handle_local_to_origin, NULL,
5287 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5288 {&handle_local_ack, NULL,
5289 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5290 sizeof (struct GNUNET_MESH_LocalAck)},
5291 {&handle_local_get_tunnels, NULL,
5292 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5293 sizeof (struct GNUNET_MessageHeader)},
5294 {&handle_local_show_tunnel, NULL,
5295 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5296 sizeof (struct GNUNET_MESH_LocalMonitor)},
5302 * To be called on core init/fail.
5304 * @param cls service closure
5305 * @param server handle to the server for this service
5306 * @param identity the public identity of this peer
5309 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5310 const struct GNUNET_PeerIdentity *identity)
5313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5314 core_handle = server;
5315 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5321 GNUNET_i2s (identity));
5322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5324 GNUNET_i2s (&my_full_id));
5325 GNUNET_SCHEDULER_shutdown (); // Try gracefully
5327 GNUNET_abort(); // Try harder
5334 * Method called whenever a given peer connects.
5336 * @param cls closure
5337 * @param peer peer identity this notification is about
5340 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5342 struct MeshPeerInfo *peer_info;
5343 struct MeshPeerPath *path;
5345 DEBUG_CONN ("Peer connected\n");
5346 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5347 peer_info = peer_get (peer);
5348 if (myid == peer_info->id)
5350 DEBUG_CONN (" (self)\n");
5355 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5357 path = path_new (2);
5358 path->peers[0] = myid;
5359 path->peers[1] = peer_info->id;
5360 GNUNET_PEER_change_rc (myid, 1);
5361 GNUNET_PEER_change_rc (peer_info->id, 1);
5362 peer_info_add_path (peer_info, path, GNUNET_YES);
5363 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5369 * Method called whenever a peer disconnects.
5371 * @param cls closure
5372 * @param peer peer identity this notification is about
5375 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5377 struct MeshPeerInfo *pi;
5378 struct MeshPeerQueue *q;
5379 struct MeshPeerQueue *n;
5381 DEBUG_CONN ("Peer disconnected\n");
5382 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5392 /* TODO try to reroute this traffic instead */
5393 queue_destroy(q, GNUNET_YES);
5396 if (NULL != pi->core_transmit)
5398 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5399 pi->core_transmit = NULL;
5401 peer_remove_path (pi, pi->id, myid);
5404 DEBUG_CONN (" (self)\n");
5406 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5411 /******************************************************************************/
5412 /************************ MAIN FUNCTIONS ****************************/
5413 /******************************************************************************/
5416 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5418 * @param cls closure
5419 * @param key current key code
5420 * @param value value in the hash map
5421 * @return GNUNET_YES if we should continue to iterate,
5425 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5427 struct MeshTunnel *t = value;
5434 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5436 * @param cls closure
5437 * @param key current key code
5438 * @param value value in the hash map
5439 * @return GNUNET_YES if we should continue to iterate,
5443 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5445 struct MeshPeerInfo *p = value;
5446 struct MeshPeerQueue *q;
5447 struct MeshPeerQueue *n;
5455 queue_destroy(q, GNUNET_YES);
5459 peer_info_destroy (p);
5465 * Task run during shutdown.
5471 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5475 if (core_handle != NULL)
5477 GNUNET_CORE_disconnect (core_handle);
5482 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
5485 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5486 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5487 if (dht_handle != NULL)
5489 GNUNET_DHT_disconnect (dht_handle);
5494 GNUNET_SERVER_notification_context_destroy (nc);
5497 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5499 GNUNET_SCHEDULER_cancel (announce_id_task);
5500 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5507 * Callback for hostkey read/generation.
5509 * @param cls Closure (Configuration handle).
5510 * @param pk The ECC private key.
5511 * @param emsg Error message, if any.
5514 key_generation_cb (void *cls,
5515 struct GNUNET_CRYPTO_EccPrivateKey *pk,
5518 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5519 struct MeshPeerInfo *peer;
5520 struct MeshPeerPath *p;
5525 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5526 _("Mesh service could not access hostkey: %s. Exiting.\n"),
5528 GNUNET_SCHEDULER_shutdown ();
5531 my_private_key = pk;
5532 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5533 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5534 &my_full_id.hashPubKey);
5535 myid = GNUNET_PEER_intern (&my_full_id);
5536 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5537 "Mesh for peer [%s] starting\n",
5538 GNUNET_i2s(&my_full_id));
5540 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5541 NULL, /* Closure passed to MESH functions */
5542 &core_init, /* Call core_init once connected */
5543 &core_connect, /* Handle connects */
5544 &core_disconnect, /* remove peers on disconnects */
5545 NULL, /* Don't notify about all incoming messages */
5546 GNUNET_NO, /* For header only in notification */
5547 NULL, /* Don't notify about all outbound messages */
5548 GNUNET_NO, /* For header-only out notification */
5549 core_handlers); /* Register these handlers */
5551 if (core_handle == NULL)
5554 GNUNET_SCHEDULER_shutdown ();
5559 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5562 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5563 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5564 GNUNET_SERVER_disconnect_notify (server_handle,
5565 &handle_local_client_disconnect, NULL);
5568 clients_head = NULL;
5569 clients_tail = NULL;
5572 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5574 /* Create a peer_info for the local peer */
5575 peer = peer_get (&my_full_id);
5578 GNUNET_PEER_change_rc (myid, 1);
5579 peer_info_add_path (peer, p, GNUNET_YES);
5580 GNUNET_SERVER_resume (server_handle);
5581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5586 * Process mesh requests.
5588 * @param cls closure
5589 * @param server the initialized server
5590 * @param c configuration to use
5593 run (void *cls, struct GNUNET_SERVER_Handle *server,
5594 const struct GNUNET_CONFIGURATION_Handle *c)
5598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5599 server_handle = server;
5602 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5607 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5608 "mesh", "peer/privatekey");
5609 GNUNET_SCHEDULER_shutdown ();
5614 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5615 &refresh_path_time))
5617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5619 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5620 "mesh", "refresh path time");
5621 GNUNET_SCHEDULER_shutdown ();
5626 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5629 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5631 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5632 "mesh", "id announce time");
5633 GNUNET_SCHEDULER_shutdown ();
5638 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5643 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5644 "mesh", "connect timeout");
5645 GNUNET_SCHEDULER_shutdown ();
5650 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5653 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5655 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5656 "mesh", "max msgs queue");
5657 GNUNET_SCHEDULER_shutdown ();
5662 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5667 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5668 "mesh", "max tunnels");
5669 GNUNET_SCHEDULER_shutdown ();
5674 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5677 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5679 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5680 "mesh", "default ttl", 64);
5685 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5688 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5689 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5690 "mesh", "max peers", 1000);
5695 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5696 &dht_replication_level))
5698 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5700 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5701 "mesh", "dht replication level", 3);
5702 dht_replication_level = 3;
5705 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5706 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5707 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5708 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5710 dht_handle = GNUNET_DHT_connect (c, 64);
5711 if (NULL == dht_handle)
5715 stats = GNUNET_STATISTICS_create ("mesh", c);
5717 GNUNET_SERVER_suspend (server_handle);
5718 /* Scheduled the task to clean up when shutdown is called */
5719 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5721 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
5724 GNUNET_free (keyfile);
5729 * The main function for the mesh service.
5731 * @param argc number of arguments from the command line
5732 * @param argv command line arguments
5733 * @return 0 ok, 1 on error
5736 main (int argc, char *const *argv)
5741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5742 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5744 ret = (GNUNET_OK == r) ? 0 : 1;
5745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5749 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5750 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5751 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);