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 * Struct to encapsulate all the Flow Control information to a peer in the
251 * context of a tunnel: Same peer in different tunnels will have independent
252 * flow control structures, allowing to choke/free tunnels according to its
255 struct MeshFlowControl
258 * ID of the last packet sent towards the peer.
260 uint32_t last_pid_sent;
263 * ID of the last packet received from the peer.
265 uint32_t last_pid_recv;
268 * Last ACK sent to the peer (peer can't send more than this PID).
270 uint32_t last_ack_sent;
273 * Last ACK sent towards the origin (for traffic towards leaf node).
275 uint32_t last_ack_recv;
278 * How many messages are in the queue towards this peer.
283 * Task to poll the peer in case of a lost ACK causes stall.
285 GNUNET_SCHEDULER_TaskIdentifier poll_task;
288 * How frequently to poll for ACKs.
290 struct GNUNET_TIME_Relative poll_time;
293 * On which tunnel to poll.
294 * Using an explicit poll_ctx would not help memory wise,
295 * since the allocated context would have to be stored in the
296 * fc struct in order to free it upon cancelling poll_task.
298 struct MeshTunnel *t;
303 * Globally unique tunnel identification (owner + number)
304 * DO NOT USE OVER THE NETWORK
309 * Node that owns the tunnel
314 * Tunnel number to differentiate all the tunnels owned by the node oid
315 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
317 MESH_TunnelNumber tid;
322 * Struct containing all information regarding a tunnel
323 * For an intermediate node the improtant info used will be:
324 * - id Tunnel unique identification
325 * - paths[0] To know where to send it next
326 * - metainfo: ready, speeds, accounting
333 struct MESH_TunnelID id;
336 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
338 MESH_TunnelNumber local_tid;
341 * Local tunnel number for local destination clients (incoming number)
342 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
345 MESH_TunnelNumber local_tid_dest;
348 * Is the tunnel bufferless (minimum latency)?
353 * Force sending ACK? Flag to allow duplicate ACK on POLL.
358 * How many messages do we accept in the forward queue.
360 unsigned int queue_max;
363 * Last time the tunnel was used
365 struct GNUNET_TIME_Absolute timestamp;
368 * Destination of the tunnel.
373 * Next hop in the tunnel.
375 GNUNET_PEER_Id next_hop;
378 * Previous hop in the tunnel.
380 GNUNET_PEER_Id prev_hop;
383 * Flow control information about @c next_hop.
385 struct MeshFlowControl next_fc;
388 * Flow control information about @c prev_hop.
390 struct MeshFlowControl prev_fc;
393 * Client owner of the tunnel, if any
395 struct MeshClient *owner;
398 * Task to keep the used paths alive at the owner,
399 * time tunnel out on all the other peers.
401 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
404 * Clients that have been informed about and want to stay in the tunnel.
406 struct MeshClient **clients;
409 * Flow control info for each client.
411 struct MeshTunnelClientInfo *clients_fc;
414 * Number of elements in clients/clients_fc
416 unsigned int nclients;
419 * Clients that have been informed but requested to leave the tunnel.
421 struct MeshClient **ignore;
424 * Number of elements in clients
426 unsigned int nignore;
429 * Path being used for the tunnel.
431 struct MeshPeerPath *path;
434 * Flag to signal the destruction of the tunnel.
435 * If this is set GNUNET_YES the tunnel will be destroyed
436 * when the queue is empty.
441 * Total messages pending for this tunnels, payload or not.
443 unsigned int pending_messages;
446 * If the tunnel is empty, destoy it.
448 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
453 * Info about a leaf client of a tunnel, needed to perform flow control.
455 struct MeshTunnelClientInfo
458 * PID of the last packet sent to the client (FWD).
463 * PID of the last packet received from the client (BCK).
468 * Maximum PID allowed (FWD ACK received).
473 * Last ACK sent to that child (BCK ACK).
480 * Struct containing information about a client of the service
487 struct MeshClient *next;
492 struct MeshClient *prev;
495 * Tunnels that belong to this client, indexed by local id
497 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
500 * Tunnels this client has accepted, indexed by incoming local id
502 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
505 * Tunnels this client has rejected, indexed by incoming local id
507 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
509 * Handle to communicate with the client
511 struct GNUNET_SERVER_Client *handle;
514 * Messages that this client has declared interest in
516 struct GNUNET_CONTAINER_MultiHashMap *types;
519 * Whether the client is active or shutting down (don't send confirmations
520 * to a client that is shutting down.
525 * ID of the client, mainly for debug messages
532 /******************************************************************************/
533 /************************ DEBUG FUNCTIONS ****************************/
534 /******************************************************************************/
538 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
539 * @param cls string to print
540 * @param success GNUNET_OK if the PUT was transmitted,
541 * GNUNET_NO on timeout,
542 * GNUNET_SYSERR on disconnect from service
543 * after the PUT message was transmitted
544 * (so we don't know if it was received or not)
549 mesh_debug (void *cls, int success)
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
557 unsigned int debug_fwd_ack;
558 unsigned int debug_bck_ack;
562 /******************************************************************************/
563 /*********************** GLOBAL VARIABLES ****************************/
564 /******************************************************************************/
566 /************************** Configuration parameters **************************/
569 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
571 static struct GNUNET_TIME_Relative refresh_path_time;
574 * How often to PUT own ID in the DHT.
576 static struct GNUNET_TIME_Relative id_announce_time;
579 * Maximum time allowed to connect to a peer found by string.
581 static struct GNUNET_TIME_Relative connect_timeout;
584 * Default TTL for payload packets.
586 static unsigned long long default_ttl;
589 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
591 static unsigned long long dht_replication_level;
594 * How many tunnels are we willing to maintain.
595 * Local tunnels are always allowed, even if there are more tunnels than max.
597 static unsigned long long max_tunnels;
600 * How many messages *in total* are we willing to queue, divided by number of
601 * tunnels to get tunnel queue size.
603 static unsigned long long max_msgs_queue;
606 * How many peers do we want to remember?
608 static unsigned long long max_peers;
611 /*************************** Static global variables **************************/
614 * Hostkey generation context
616 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
619 * DLL with all the clients, head.
621 static struct MeshClient *clients_head;
624 * DLL with all the clients, tail.
626 static struct MeshClient *clients_tail;
629 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
631 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
634 * Number of tunnels known.
636 static unsigned long long n_tunnels;
639 * Tunnels incoming, indexed by MESH_TunnelNumber
640 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
642 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
645 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
647 static struct GNUNET_CONTAINER_MultiHashMap *peers;
650 * Handle to communicate with transport
652 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
655 * Handle to communicate with core.
657 static struct GNUNET_CORE_Handle *core_handle;
662 static struct GNUNET_DHT_Handle *dht_handle;
667 static struct GNUNET_SERVER_Handle *server_handle;
670 * Handle to the statistics service.
672 static struct GNUNET_STATISTICS_Handle *stats;
675 * Notification context, to send messages to local clients.
677 static struct GNUNET_SERVER_NotificationContext *nc;
680 * Local peer own ID (memory efficient handle).
682 static GNUNET_PEER_Id myid;
685 * Local peer own ID (full value).
687 static struct GNUNET_PeerIdentity my_full_id;
692 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
697 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
700 * Tunnel ID for the next created tunnel (global tunnel number).
702 static MESH_TunnelNumber next_tid;
705 * Tunnel ID for the next incoming tunnel (local tunnel number).
707 static MESH_TunnelNumber next_local_tid;
710 * All message types clients of this peer are interested in.
712 static struct GNUNET_CONTAINER_MultiHashMap *types;
715 * Task to periodically announce itself in the network.
717 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
720 * Next ID to assign to a client.
722 unsigned int next_client_id;
725 /******************************************************************************/
726 /*********************** DECLARATIONS **************************/
727 /******************************************************************************/
730 * Function to process paths received for a new peer addition. The recorded
731 * paths form the initial tunnel, which can be optimized later.
732 * Called on each result obtained for the DHT search.
735 * @param exp when will this value expire
736 * @param key key of the result
737 * @param type type of the result
738 * @param size number of bytes in data
739 * @param data pointer to the result data
742 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
743 const struct GNUNET_HashCode * key,
744 const struct GNUNET_PeerIdentity *get_path,
745 unsigned int get_path_length,
746 const struct GNUNET_PeerIdentity *put_path,
747 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
748 size_t size, const void *data);
752 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
753 * and insert it in the appropiate structures if the peer is not known yet.
755 * @param peer Full identity of the peer.
757 * @return Existing or newly created peer info.
759 static struct MeshPeerInfo *
760 peer_get (const struct GNUNET_PeerIdentity *peer);
764 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
765 * and insert it in the appropiate structures if the peer is not known yet.
767 * @param peer Short identity of the peer.
769 * @return Existing or newly created peer info.
771 static struct MeshPeerInfo *
772 peer_get_short (const GNUNET_PEER_Id peer);
776 * Build a PeerPath from the paths returned from the DHT, reversing the paths
777 * to obtain a local peer -> destination path and interning the peer ids.
779 * @return Newly allocated and created path
781 static struct MeshPeerPath *
782 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
783 unsigned int get_path_length,
784 const struct GNUNET_PeerIdentity *put_path,
785 unsigned int put_path_length);
789 * Adds a path to the peer_infos of all the peers in the path
791 * @param p Path to process.
792 * @param confirmed Whether we know if the path works or not.
795 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
800 * Search for a tunnel by global ID using full PeerIdentities.
802 * @param oid owner of the tunnel.
803 * @param tid global tunnel number.
805 * @return tunnel handler, NULL if doesn't exist.
807 static struct MeshTunnel *
808 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
812 * Delete an active client from the tunnel.
818 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
821 * Notify a tunnel that a connection has broken that affects at least
824 * @param t Tunnel affected.
825 * @param p1 Peer that got disconnected from p2.
826 * @param p2 Peer that got disconnected from p1.
828 * @return Short ID of the peer disconnected (either p1 or p2).
829 * 0 if the tunnel remained unaffected.
831 static GNUNET_PEER_Id
832 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
837 * Get the current ack value for a tunnel, for data going from root to leaves,
838 * taking in account the tunnel mode and the status of all children and clients.
842 * @return Maximum PID allowed.
845 tunnel_get_fwd_ack (struct MeshTunnel *t);
849 * Add a client to a tunnel, initializing all needed data structures.
851 * @param t Tunnel to which add the client.
852 * @param c Client which to add to the tunnel.
855 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
859 * Use the given path for the tunnel.
861 * @param t Tunnel to update.
862 * @param p Path to use.
865 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
868 * @brief Queue and pass message to core when possible.
870 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
871 * and accounts for it. In case the queue is full, the message is dropped and
874 * Otherwise, message is treated as internal and allowed to go regardless of
877 * @param cls Closure (@c type dependant). It will be used by queue_send to
878 * build the message to be sent if not already prebuilt.
879 * @param type Type of the message, 0 for a raw message.
880 * @param size Size of the message.
881 * @param dst Neighbor to send message to.
882 * @param t Tunnel this message belongs to.
885 queue_add (void *cls, uint16_t type, size_t size,
886 struct MeshPeerInfo *dst, struct MeshTunnel *t);
890 * Free a transmission that was already queued with all resources
891 * associated to the request.
893 * @param queue Queue handler to cancel.
894 * @param clear_cls Is it necessary to free associated cls?
897 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
901 * @brief Get the next transmittable message from the queue.
903 * This will be the head, except in the case of being a data packet
904 * not allowed by the destination peer.
906 * @param peer Destination peer.
908 * @return The next viable MeshPeerQueue element to send to that peer.
909 * NULL when there are no transmittable messages.
911 struct MeshPeerQueue *
912 queue_get_next (const struct MeshPeerInfo *peer);
916 * Core callback to write a queued packet to core buffer
918 * @param cls Closure (peer info).
919 * @param size Number of bytes available in buf.
920 * @param buf Where the to write the message.
922 * @return number of bytes written to buf
925 queue_send (void *cls, size_t size, void *buf);
928 /******************************************************************************/
929 /************************ PERIODIC FUNCTIONS ****************************/
930 /******************************************************************************/
933 * Periodically announce self id in the DHT
936 * @param tc task context
939 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
943 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
945 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
949 * - Set data expiration in function of X
952 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
954 block.id = my_full_id;
955 block.type = htonl (0);
956 GNUNET_DHT_put (dht_handle, /* DHT handle */
957 &my_full_id.hashPubKey, /* Key to use */
958 dht_replication_level, /* Replication level */
959 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
960 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
961 sizeof (block), /* Size of the data */
962 (const char *) &block, /* Data itself */
963 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
964 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
965 NULL, /* Continuation */
966 NULL); /* Continuation closure */
968 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
972 /******************************************************************************/
973 /****************** GENERAL HELPER FUNCTIONS ************************/
974 /******************************************************************************/
978 * Check if client has registered with the service and has not disconnected
980 * @param client the client to check
982 * @return non-NULL if client exists in the global DLL
984 static struct MeshClient *
985 client_get (struct GNUNET_SERVER_Client *client)
987 struct MeshClient *c;
992 if (c->handle == client)
1001 * Checks if a given client has subscribed to certain message type
1003 * @param message_type Type of message to check
1004 * @param c Client to check
1006 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1008 * FIXME: use of crypto_hash slows it down
1009 * The hash function alone takes 8-10us out of the ~55us for the whole
1010 * process of retransmitting the message from one local client to another.
1011 * Find faster implementation!
1014 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1016 struct GNUNET_HashCode hc;
1018 if (NULL == c->types)
1021 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1022 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1027 * Check whether client wants traffic from a tunnel.
1029 * @param c Client to check.
1030 * @param t Tunnel to be found.
1032 * @return GNUNET_YES if client knows tunnel.
1034 * TODO look in client hashmap
1037 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1041 for (i = 0; i < t->nclients; i++)
1042 if (t->clients[i] == c)
1049 * Check whether client has been informed about a tunnel.
1051 * @param c Client to check.
1052 * @param t Tunnel to be found.
1054 * @return GNUNET_YES if client knows tunnel.
1056 * TODO look in client hashmap
1059 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1063 for (i = 0; i < t->nignore; i++)
1064 if (t->ignore[i] == c)
1066 return client_wants_tunnel(c, t);
1071 * Marks a client as uninterested in traffic from the tunnel, updating both
1072 * client and tunnel to reflect this.
1074 * @param c Client that doesn't want traffic anymore.
1075 * @param t Tunnel which should be ignored.
1077 * FIXME when to delete an incoming tunnel?
1080 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1082 struct GNUNET_HashCode hash;
1084 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1085 GNUNET_break (GNUNET_YES ==
1086 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1088 GNUNET_break (GNUNET_YES ==
1089 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1090 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1091 tunnel_delete_active_client (t, c);
1092 GNUNET_array_append (t->ignore, t->nignore, c);
1097 * Deletes a tunnel from a client (either owner or destination). To be used on
1098 * tunnel destroy, otherwise, use client_ignore_tunnel.
1100 * @param c Client whose tunnel to delete.
1101 * @param t Tunnel which should be deleted.
1104 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1106 struct GNUNET_HashCode hash;
1110 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1111 GNUNET_assert (GNUNET_YES ==
1112 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1118 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1120 GNUNET_assert (GNUNET_YES ==
1121 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1125 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1133 * Send the message to all clients that have subscribed to its type
1135 * @param msg Pointer to the message itself
1136 * @param payload Pointer to the payload of the message.
1137 * @param t The tunnel to whose clients this message goes.
1139 * @return number of clients this message was sent to
1142 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1143 const struct GNUNET_MessageHeader *payload,
1144 struct MeshTunnel *t)
1146 struct MeshClient *c;
1147 MESH_TunnelNumber *tid;
1150 char cbuf[htons (msg->size)];
1152 type = ntohs (payload->type);
1153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1155 GNUNET_MESH_DEBUG_M2S (type));
1157 memcpy (cbuf, msg, sizeof (cbuf));
1158 switch (htons (msg->type))
1160 struct GNUNET_MESH_Unicast *uc;
1161 struct GNUNET_MESH_ToOrigin *to;
1163 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1164 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1167 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1168 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1176 for (count = 0, c = clients_head; c != NULL; c = c->next)
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1179 if (client_is_subscribed (type, c))
1181 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1185 *tid = htonl (t->local_tid);
1189 if (GNUNET_NO == client_knows_tunnel (c, t))
1191 /* This client doesn't know the tunnel */
1192 struct GNUNET_MESH_TunnelNotification tmsg;
1193 struct GNUNET_HashCode hash;
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1196 tmsg.header.size = htons (sizeof (tmsg));
1197 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1198 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1199 tmsg.tunnel_id = htonl (t->local_tid_dest);
1201 if (GNUNET_YES == t->nobuffer)
1202 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
1203 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1204 &tmsg.header, GNUNET_NO);
1205 tunnel_add_client (t, c);
1206 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1208 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1209 c->incoming_tunnels, &hash, t,
1210 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1212 *tid = htonl (t->local_tid_dest);
1215 /* Check if the client wants to get traffic from the tunnel */
1216 if (GNUNET_NO == client_wants_tunnel(c, t))
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1220 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1221 (struct GNUNET_MessageHeader
1222 *) cbuf, GNUNET_NO);
1231 * Notify all clients (not depending on registration status) that the incoming
1232 * tunnel is no longer valid.
1234 * @param t Tunnel that was destroyed.
1237 send_clients_tunnel_destroy (struct MeshTunnel *t)
1239 struct GNUNET_MESH_TunnelMessage msg;
1241 msg.header.size = htons (sizeof (msg));
1242 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1243 msg.tunnel_id = htonl (t->local_tid_dest);
1244 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1249 * Notify clients of tunnel disconnections, if needed.
1250 * In case the origin disconnects, the destination clients get a tunnel destroy
1251 * notification. If the last destination disconnects (only one remaining client
1252 * in tunnel), the origin gets a (local ID) peer disconnected.
1253 * Note that the function must be called BEFORE removing the client from
1256 * @param t Tunnel that was destroyed.
1257 * @param c Client that disconnected.
1260 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1266 struct GNUNET_MESH_TunnelMessage msg;
1268 msg.header.size = htons (sizeof (msg));
1269 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1270 msg.tunnel_id = htonl (t->local_tid_dest);
1271 for (i = 0; i < t->nclients; i++)
1272 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1273 &msg.header, GNUNET_NO);
1275 // FIXME when to disconnect an incoming tunnel?
1280 * Iterator over all the peers to remove the oldest not-used entry.
1282 * @param cls Closure (unsued).
1283 * @param key ID of the peer.
1284 * @param value Peer_Info of the peer.
1289 peer_info_timeout (void *cls,
1290 const struct GNUNET_HashCode *key,
1297 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1298 * and insert it in the appropiate structures if the peer is not known yet.
1300 * @param peer Full identity of the peer.
1302 * @return Existing or newly created peer info.
1304 static struct MeshPeerInfo *
1305 peer_get (const struct GNUNET_PeerIdentity *peer)
1307 struct MeshPeerInfo *peer_info;
1309 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1310 if (NULL == peer_info)
1313 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1314 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1316 GNUNET_CONTAINER_multihashmap_iterate (peers,
1320 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1321 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1322 peer_info->id = GNUNET_PEER_intern (peer);
1324 peer_info->last_contact = GNUNET_TIME_absolute_get();
1331 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1332 * and insert it in the appropiate structures if the peer is not known yet.
1334 * @param peer Short identity of the peer.
1336 * @return Existing or newly created peer info.
1338 static struct MeshPeerInfo *
1339 peer_get_short (const GNUNET_PEER_Id peer)
1341 struct GNUNET_PeerIdentity id;
1343 GNUNET_PEER_resolve (peer, &id);
1344 return peer_get (&id);
1349 * Choose the best path towards a peer considering the tunnel properties.
1351 * @param peer The destination peer.
1352 * @param t The tunnel the path is for.
1354 * @return Best current known path towards the peer, if any.
1356 static struct MeshPeerPath *
1357 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1359 struct MeshPeerPath *best_p;
1360 struct MeshPeerPath *p;
1361 unsigned int best_cost;
1364 best_p = p = peer->path_head;
1365 best_cost = cost = p->length;
1368 if ((cost = p->length) < best_cost)
1380 * Remove the tunnel from the list of tunnels to which a peer is target.
1382 * @param peer PeerInfo of the peer.
1383 * @param t Tunnel to remove.
1386 peer_remove_tunnel (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1390 for (i = 0; i < peer->ntunnels; i++)
1393 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1396 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1398 GNUNET_realloc (peer->tunnels,
1399 peer->ntunnels * sizeof(struct MeshTunnel *));
1407 * Core callback to write a pre-constructed data packet to core buffer
1409 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1410 * @param size Number of bytes available in buf.
1411 * @param buf Where the to write the message.
1413 * @return number of bytes written to buf
1416 send_core_data_raw (void *cls, size_t size, void *buf)
1418 struct MeshTransmissionDescriptor *info = cls;
1419 struct GNUNET_MessageHeader *msg;
1422 GNUNET_assert (NULL != info);
1423 msg = (struct GNUNET_MessageHeader *) info->data;
1424 total_size = ntohs (msg->size);
1426 if (total_size > size)
1431 memcpy (buf, msg, total_size);
1432 GNUNET_free (info->data);
1439 * Sends an already built message to a peer, properly registrating
1440 * all used resources.
1442 * @param message Message to send. Function makes a copy of it.
1443 * @param peer Short ID of the neighbor whom to send the message.
1444 * @param t Tunnel on which this message is transmitted.
1447 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1448 GNUNET_PEER_Id peer,
1449 struct MeshTunnel *t)
1451 struct MeshTransmissionDescriptor *info;
1452 struct GNUNET_PeerIdentity id;
1453 struct MeshPeerInfo *neighbor;
1454 struct MeshPeerPath *p;
1458 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1463 size = ntohs (message->size);
1464 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1465 info->data = GNUNET_malloc (size);
1466 memcpy (info->data, message, size);
1467 type = ntohs(message->type);
1470 struct GNUNET_MESH_Unicast *m;
1471 struct GNUNET_MESH_ToOrigin *to;
1473 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1474 m = (struct GNUNET_MESH_Unicast *) info->data;
1475 m->ttl = htonl (ntohl (m->ttl) - 1);
1477 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1478 to = (struct GNUNET_MESH_ToOrigin *) info->data;
1479 t->prev_fc.last_pid_sent++; /* FIXME per hop? */
1480 to->pid = htonl (t->prev_fc.last_pid_sent);
1482 info->data_len = size;
1483 GNUNET_PEER_resolve (peer, &id);
1484 neighbor = peer_get (&id);
1485 for (p = neighbor->path_head; NULL != p; p = p->next)
1495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1496 " %s IS NOT DIRECTLY CONNECTED\n",
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1501 for (p = neighbor->path_head; NULL != p; p = p->next)
1503 struct GNUNET_PeerIdentity debug_id;
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1507 " path with %u hops through:\n",
1509 for (i = 0; i < p->length; i++)
1511 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514 i, GNUNET_i2s(&debug_id));
1518 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1519 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1520 " no direct connection to %s\n",
1522 GNUNET_free (info->data);
1526 info->peer = neighbor;
1527 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1538 * Sends a CREATE PATH message for a path to a peer, properly registrating
1539 * all used resources.
1541 * @param peer PeerInfo of the final peer for whom this path is being created.
1542 * @param p Path itself.
1543 * @param t Tunnel for which the path is created.
1546 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1547 struct MeshTunnel *t)
1549 struct MeshPeerInfo *neighbor;
1557 neighbor = peer_get_short (t->next_hop);
1559 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1560 sizeof (struct GNUNET_MESH_ManipulatePath) +
1561 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1568 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1570 * @param t Tunnel which to confirm.
1573 send_path_ack (struct MeshTunnel *t)
1575 struct MeshTransmissionDescriptor *info;
1576 struct GNUNET_PeerIdentity id;
1578 GNUNET_PEER_resolve (t->prev_hop, &id);
1579 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1580 info->origin = &t->id;
1581 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1582 GNUNET_assert (NULL != info->peer);
1585 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1586 sizeof (struct GNUNET_MESH_PathACK),
1593 * Try to establish a new connection to this peer in the fiven tunnel.
1594 * If the peer doesn't have any path to it yet, try to get one.
1595 * If the peer already has some path, send a CREATE PATH towards it.
1597 * @param peer PeerInfo of the peer.
1598 * @param t Tunnel for which to create the path, if possible.
1601 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1603 struct MeshPeerPath *p;
1605 if (NULL != peer->path_head)
1607 p = peer_get_best_path (peer, t);
1608 tunnel_use_path (t, p);
1609 send_create_path (peer, p, t);
1611 else if (NULL == peer->dhtget)
1613 struct GNUNET_PeerIdentity id;
1615 GNUNET_PEER_resolve (peer->id, &id);
1616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1617 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1618 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1619 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1620 &id.hashPubKey, /* key to search */
1621 dht_replication_level, /* replication level */
1622 GNUNET_DHT_RO_RECORD_ROUTE |
1623 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1625 0, /* xquery bits */
1626 &dht_get_id_handler, peer);
1628 /* Otherwise, there is no path but the DHT get is already started. */
1633 * Destroy the peer_info and free any allocated resources linked to it
1635 * @param pi The peer_info to destroy.
1637 * @return GNUNET_OK on success
1640 peer_info_destroy (struct MeshPeerInfo *pi)
1642 struct GNUNET_PeerIdentity id;
1643 struct MeshPeerPath *p;
1644 struct MeshPeerPath *nextp;
1646 GNUNET_PEER_resolve (pi->id, &id);
1647 GNUNET_PEER_change_rc (pi->id, -1);
1650 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1653 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1654 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1656 if (NULL != pi->dhtget)
1658 GNUNET_DHT_get_stop (pi->dhtget);
1664 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1674 * Remove all paths that rely on a direct connection between p1 and p2
1675 * from the peer itself and notify all tunnels about it.
1677 * @param peer PeerInfo of affected peer.
1678 * @param p1 GNUNET_PEER_Id of one peer.
1679 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1682 * TODO: optimize (see below)
1685 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1688 struct MeshPeerPath *p;
1689 struct MeshPeerPath *next;
1690 struct MeshPeerInfo *peer_d;
1692 unsigned int destroyed;
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1697 for (p = peer->path_head; NULL != p; p = next)
1700 for (i = 0; i < (p->length - 1); i++)
1702 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1703 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1705 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1715 for (i = 0; i < peer->ntunnels; i++)
1717 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1721 peer_d = peer_get_short (d);
1722 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1723 tunnel_use_path (peer->tunnels[i], next);
1724 peer_connect (peer_d, peer->tunnels[i]);
1726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1731 * Add the path to the peer and update the path used to reach it in case this
1734 * @param peer_info Destination peer to add the path to.
1735 * @param path New path to add. Last peer must be the peer in arg 1.
1736 * Path will be either used of freed if already known.
1737 * @param trusted Do we trust that this path is real?
1740 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1743 struct MeshPeerPath *aux;
1747 if ((NULL == peer_info) || (NULL == path))
1750 path_destroy (path);
1753 if (path->peers[path->length - 1] != peer_info->id)
1756 path_destroy (path);
1759 if (path->length <= 2 && GNUNET_NO == trusted)
1761 /* Only allow CORE to tell us about direct paths */
1762 path_destroy (path);
1765 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1766 for (l = 1; l < path->length; l++)
1768 if (path->peers[l] == myid)
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1771 for (l2 = 0; l2 < path->length - l; l2++)
1773 path->peers[l2] = path->peers[l + l2];
1778 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1783 struct GNUNET_PeerIdentity id;
1785 GNUNET_PEER_resolve (peer_info->id, &id);
1786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1787 path->length, GNUNET_i2s (&id));
1790 l = path_get_length (path);
1797 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1798 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1800 l2 = path_get_length (aux);
1803 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1804 peer_info->path_tail, aux, path);
1809 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1811 path_destroy (path);
1816 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1823 * Add the path to the origin peer and update the path used to reach it in case
1824 * this is the shortest.
1825 * The path is given in peer_info -> destination, therefore we turn the path
1826 * upside down first.
1828 * @param peer_info Peer to add the path to, being the origin of the path.
1829 * @param path New path to add after being inversed.
1830 * @param trusted Do we trust that this path is real?
1833 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1834 struct MeshPeerPath *path, int trusted)
1837 peer_info_add_path (peer_info, path, trusted);
1842 * Function called if the connection to the peer has been stalled for a while,
1843 * possibly due to a missed ACK. Poll the peer about its ACK status.
1845 * @param cls Closure (poll ctx).
1846 * @param tc TaskContext.
1849 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1851 struct MeshFlowControl *fc = cls;
1852 struct GNUNET_MESH_Poll msg;
1853 struct MeshTunnel *t = fc->t;
1854 GNUNET_PEER_Id peer;
1856 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1857 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1862 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1863 msg.header.size = htons (sizeof (msg));
1864 msg.tid = htonl (t->id.tid);
1865 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1866 msg.last_ack = htonl (fc->last_ack_recv);
1868 if (fc == &t->prev_fc)
1876 send_prebuilt_message (&msg.header, peer, t);
1877 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1878 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1884 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1885 * to obtain a local peer -> destination path and interning the peer ids.
1887 * @return Newly allocated and created path
1889 static struct MeshPeerPath *
1890 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1891 unsigned int get_path_length,
1892 const struct GNUNET_PeerIdentity *put_path,
1893 unsigned int put_path_length)
1895 struct MeshPeerPath *p;
1901 GNUNET_PEER_change_rc (myid, 1);
1902 i = get_path_length;
1903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1904 for (i--; i >= 0; i--)
1906 id = GNUNET_PEER_intern (&get_path[i]);
1907 if (p->length > 0 && id == p->peers[p->length - 1])
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1910 GNUNET_PEER_change_rc (id, -1);
1914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1915 GNUNET_i2s (&get_path[i]));
1917 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1918 p->peers[p->length - 1] = id;
1921 i = put_path_length;
1922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1923 for (i--; i >= 0; i--)
1925 id = GNUNET_PEER_intern (&put_path[i]);
1928 /* PUT path went through us, so discard the path up until now and start
1929 * from here to get a much shorter (and loop-free) path.
1934 if (p->length > 0 && id == p->peers[p->length - 1])
1936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1937 GNUNET_PEER_change_rc (id, -1);
1941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1942 GNUNET_i2s (&put_path[i]));
1944 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1945 p->peers[p->length - 1] = id;
1949 if (get_path_length > 0)
1950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1951 GNUNET_i2s (&get_path[0]));
1952 if (put_path_length > 0)
1953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1954 GNUNET_i2s (&put_path[0]));
1955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1957 for (i = 0; i < p->length; i++)
1959 struct GNUNET_PeerIdentity peer_id;
1961 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1963 GNUNET_i2s (&peer_id));
1971 * Adds a path to the peer_infos of all the peers in the path
1973 * @param p Path to process.
1974 * @param confirmed Whether we know if the path works or not.
1977 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1981 /* TODO: invert and add */
1982 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1983 for (i++; i < p->length; i++)
1985 struct MeshPeerInfo *aux;
1986 struct MeshPeerPath *copy;
1988 aux = peer_get_short (p->peers[i]);
1989 copy = path_duplicate (p);
1990 copy->length = i + 1;
1991 peer_info_add_path (aux, copy, GNUNET_NO);
1997 * Send keepalive packets for a peer
1999 * @param cls Closure (tunnel for which to send the keepalive).
2000 * @param tc Notification context.
2003 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2007 * Search for a tunnel among the incoming tunnels
2009 * @param tid the local id of the tunnel
2011 * @return tunnel handler, NULL if doesn't exist
2013 static struct MeshTunnel *
2014 tunnel_get_incoming (MESH_TunnelNumber tid)
2016 struct GNUNET_HashCode hash;
2018 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2019 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2020 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2025 * Search for a tunnel among the tunnels for a client
2027 * @param c the client whose tunnels to search in
2028 * @param tid the local id of the tunnel
2030 * @return tunnel handler, NULL if doesn't exist
2032 static struct MeshTunnel *
2033 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2035 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2037 return tunnel_get_incoming (tid);
2041 struct GNUNET_HashCode hash;
2043 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2044 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2050 * Search for a tunnel by global ID using PEER_ID
2052 * @param pi owner of the tunnel
2053 * @param tid global tunnel number
2055 * @return tunnel handler, NULL if doesn't exist
2057 static struct MeshTunnel *
2058 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2060 struct MESH_TunnelID id;
2061 struct GNUNET_HashCode hash;
2066 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2067 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2072 * Search for a tunnel by global ID using full PeerIdentities
2074 * @param oid owner of the tunnel
2075 * @param tid global tunnel number
2077 * @return tunnel handler, NULL if doesn't exist
2079 static struct MeshTunnel *
2080 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2082 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2087 * Delete an active client from the tunnel.
2093 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2097 for (i = 0; i < t->nclients; i++)
2099 if (t->clients[i] == c)
2101 t->clients[i] = t->clients[t->nclients - 1];
2102 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2103 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2105 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2113 * Delete an ignored client from the tunnel.
2119 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2123 for (i = 0; i < t->nignore; i++)
2125 if (t->ignore[i] == c)
2127 t->ignore[i] = t->ignore[t->nignore - 1];
2128 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2136 * Delete a client from the tunnel. It should be only done on
2137 * client disconnection, otherwise use client_ignore_tunnel.
2143 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2145 tunnel_delete_ignored_client (t, c);
2146 tunnel_delete_active_client (t, c);
2149 /* FIXME check initial values */
2151 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
2153 struct MeshTunnelClientInfo clinfo;
2155 GNUNET_array_append (t->clients, t->nclients, c);
2156 clinfo.fwd_ack = t->prev_fc.last_pid_recv + 1;
2157 clinfo.fwd_pid = t->prev_fc.last_pid_recv;
2158 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
2159 clinfo.bck_pid = (uint32_t) -1; /* Expected next: 0 */
2160 t->nclients--; /* Double append */
2161 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
2166 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
2170 for (i = 0; i < p->length; i++)
2172 if (p->peers[i] == myid)
2175 if (i > p->length - 1)
2181 if (i < p->length - 1)
2182 t->next_hop = p->peers[i + 1];
2186 t->prev_hop = p->peers[i - 1];
2190 if (NULL != t->path)
2191 path_destroy (t->path);
2192 t->path = path_duplicate (p);
2193 if (GNUNET_SCHEDULER_NO_TASK == t->maintenance_task)
2194 t->maintenance_task =
2195 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
2200 * Notifies a tunnel that a connection has broken that affects at least
2201 * some of its peers. Sends a notification towards the root of the tree.
2202 * In case the peer is the owner of the tree, notifies the client that owns
2203 * the tunnel and tries to reconnect.
2205 * @param t Tunnel affected.
2206 * @param p1 Peer that got disconnected from p2.
2207 * @param p2 Peer that got disconnected from p1.
2209 * @return Short ID of the peer disconnected (either p1 or p2).
2210 * 0 if the tunnel remained unaffected.
2212 static GNUNET_PEER_Id
2213 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2216 // if (myid != p1 && myid != p2) FIXME
2221 // if (tree_get_predecessor (t->tree) != 0)
2223 // /* We are the peer still connected, notify owner of the disconnection. */
2224 // struct GNUNET_MESH_PathBroken msg;
2225 // struct GNUNET_PeerIdentity neighbor;
2227 // msg.header.size = htons (sizeof (msg));
2228 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2229 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2230 // msg.tid = htonl (t->id.tid);
2231 // msg.peer1 = my_full_id;
2232 // GNUNET_PEER_resolve (pid, &msg.peer2);
2233 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2234 // send_prebuilt_message (&msg.header, &neighbor, t);
2241 * Get the Flow Control info of a client.
2243 * @param t Tunnel on which to look.
2244 * @param c Client whose ACK to get.
2246 * @return ACK value.
2248 static struct MeshTunnelClientInfo *
2249 tunnel_get_client_fc (struct MeshTunnel *t,
2250 struct MeshClient *c)
2254 for (i = 0; i < t->nclients; i++)
2256 if (t->clients[i] != c)
2258 return &t->clients_fc[i];
2261 return NULL; // avoid compiler / coverity complaints
2266 * Set the FWD ACK value of a client in a particular tunnel.
2268 * @param t Tunnel affected.
2269 * @param c Client whose ACK to set.
2270 * @param ack ACK value.
2273 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
2274 struct MeshClient *c,
2279 for (i = 0; i < t->nclients; i++)
2281 if (t->clients[i] != c)
2283 t->clients_fc[i].fwd_ack = ack;
2291 * Get the highest ACK value of all clients in a particular tunnel,
2292 * according to the buffering/speed settings.
2294 * @param t Tunnel on which to look.
2296 * @return Corresponding ACK value (max uint32_t).
2297 * If no clients are suscribed, -1LL.
2300 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
2305 if (0 == t->nclients)
2307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2308 " tunnel has no clients, no FWD ACK\n");
2312 for (ack = -1LL, i = 0; i < t->nclients; i++)
2315 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack))
2317 ack = t->clients_fc[i].fwd_ack;
2321 if (GNUNET_YES == t->nobuffer &&
2322 GMC_is_pid_bigger(ack, t->prev_fc.last_pid_recv))
2323 ack = (uint32_t) t->prev_fc.last_pid_recv + 1; // Might overflow, it's ok.
2325 return (uint32_t) ack;
2330 * Get the current fwd ack value for a tunnel, taking in account the tunnel
2331 * mode and the status of all children nodes.
2335 * @return Maximum PID allowed.
2338 tunnel_get_fwd_ack (struct MeshTunnel *t)
2341 uint32_t buffer_free;
2344 buffer_free = t->queue_max - t->next_fc.queue_n;
2345 client_ack = tunnel_get_clients_fwd_ack (t);
2346 if (GNUNET_YES == t->nobuffer)
2348 ack = t->prev_fc.last_ack_sent;
2352 ack = t->prev_fc.last_ack_sent + buffer_free; // Overflow? OK!
2354 if (-1LL == client_ack)
2359 ack = GMC_max_pid ((uint32_t) client_ack, ack);
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2362 "c %u, bf %u, cl %lld, ACK: %u\n",
2363 t->prev_fc.last_ack_sent, buffer_free, client_ack, ack);
2369 * Build a local ACK message and send it to a local client.
2371 * @param t Tunnel on which to send the ACK.
2372 * @param c Client to whom send the ACK.
2373 * @param ack Value of the ACK.
2376 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
2378 struct GNUNET_MESH_LocalAck msg;
2380 msg.header.size = htons (sizeof (msg));
2381 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2382 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
2383 msg.max_pid = htonl (ack);
2384 GNUNET_SERVER_notification_context_unicast(nc,
2391 * Build an ACK message and queue it to send to the given peer.
2393 * @param t Tunnel on which to send the ACK.
2394 * @param peer Peer to whom send the ACK.
2395 * @param ack Value of the ACK.
2398 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
2400 struct GNUNET_MESH_ACK msg;
2402 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2403 msg.header.size = htons (sizeof (msg));
2404 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2405 msg.pid = htonl (ack);
2406 msg.tid = htonl (t->id.tid);
2408 send_prebuilt_message (&msg.header, peer, t);
2413 * Notify a the owner of a tunnel about how many more
2414 * payload packages will we accept on a given tunnel.
2416 * @param t Tunnel on which to send the ACK.
2419 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
2423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2424 "Sending client FWD ACK on tunnel %X\n",
2427 ack = tunnel_get_fwd_ack (t);
2429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
2430 if (t->prev_fc.last_ack_sent == ack)
2432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
2436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
2437 t->prev_fc.last_ack_sent = ack;
2438 send_local_ack (t, t->owner, ack);
2443 * Send an ACK informing the predecessor about the available buffer space.
2444 * In case there is no predecessor, inform the owning client.
2445 * If buffering is off, send only on behalf of children or self if endpoint.
2446 * If buffering is on, send when sent to children and buffer space is free.
2447 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2448 * the ACK itself goes "back" (towards root).
2450 * @param t Tunnel on which to send the ACK.
2451 * @param type Type of message that triggered the ACK transmission.
2454 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2458 if (NULL != t->owner)
2460 tunnel_send_client_fwd_ack (t);
2463 /* Is it after unicast retransmission? */
2466 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2468 "ACK due to FWD DATA retransmission\n");
2469 if (GNUNET_YES == t->nobuffer)
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2475 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2476 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2478 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2479 t->force_ack = GNUNET_YES;
2485 /* Check if we need to transmit the ACK */
2486 if (t->queue_max > t->next_fc.queue_n * 4 &&
2487 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
2488 GNUNET_NO == t->force_ack)
2490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2492 " t->qmax: %u, t->qn: %u\n",
2493 t->queue_max, t->next_fc.queue_n);
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 " t->pid: %u, t->ack: %u\n",
2496 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
2500 /* Ok, ACK might be necessary, what PID to ACK? */
2501 ack = tunnel_get_fwd_ack (t);
2502 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
2504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
2508 t->prev_fc.last_ack_sent = ack;
2509 send_ack (t, t->prev_hop, ack);
2511 t->force_ack = GNUNET_NO;
2516 * Send a child node a BCK ACK to allow him to send more to_origin data.
2519 * @param id Id of the child node.
2522 tunnel_send_mesh_bck_ack (struct MeshTunnel *t,
2523 GNUNET_PEER_Id peer)
2525 uint32_t ack = 0; // FIXME
2527 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2529 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2532 " Not sending ACK, not needed\n");
2535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2536 " Sending BCK ACK %u (last sent: %u)\n",
2537 ack, t->next_fc.last_ack_sent);
2538 t->next_fc.last_ack_sent = ack;
2540 send_ack (t, peer, ack);
2545 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
2547 * Iterates over all clients and sends BCK ACKs to the ones that need it.
2549 * FIXME fc: what happens if we have 2 clients but q_size is 1?
2550 * - implement a size 1 buffer in each client_fc AND children_fc
2551 * to hold at least 1 message per "child".
2552 * problem: violates no buffer policy
2553 * - ack 0 and make "children" poll for transmission slots
2554 * problem: big overhead, extra latency even in low traffic
2557 * @param t Tunnel on which to send the BCK ACKs.
2560 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
2563 unsigned int tunnel_delta;
2565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
2567 tunnel_delta = t->queue_max - t->next_fc.queue_n;
2568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
2570 /* Find client whom to allow to send to origin (with lowest buffer space) */
2571 for (i = 0; i < t->nclients; i++)
2573 struct MeshTunnelClientInfo *clinfo;
2576 clinfo = &t->clients_fc[i];
2577 delta = clinfo->bck_ack - clinfo->bck_pid;
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
2579 t->clients[i]->id, delta);
2581 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
2582 (GNUNET_YES == t->nobuffer && 0 == delta))
2586 ack = clinfo->bck_pid;
2587 ack += t->nobuffer ? 1 : tunnel_delta;
2588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2589 " sending ack to client %u: %u\n",
2590 t->clients[i]->id, ack);
2591 send_local_ack (t, t->clients[i], ack);
2592 clinfo->bck_ack = ack;
2596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2597 " not sending ack to client %u (td %u, d %u)\n",
2598 t->clients[i]->id, tunnel_delta, delta);
2605 * Send an ACK informing the children nodes and destination clients about
2606 * the available buffer space.
2607 * If buffering is off, send only on behalf of root (can be self).
2608 * If buffering is on, send when sent to predecessor and buffer space is free.
2609 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2610 * the ACK itself goes "forward" (towards children/clients).
2612 * @param t Tunnel on which to send the ACK.
2613 * @param type Type of message that triggered the ACK transmission.
2616 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2619 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2620 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2621 /* Is it after data to_origin retransmission? */
2624 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2625 if (GNUNET_YES == t->nobuffer)
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2628 " Not sending ACK, nobuffer\n");
2632 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2633 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2635 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2636 t->force_ack = GNUNET_YES;
2642 tunnel_send_clients_bck_ack (t);
2643 tunnel_send_mesh_bck_ack (t, t->next_hop);
2644 t->force_ack = GNUNET_NO;
2649 * @brief Re-initiate traffic to this peer if necessary.
2651 * Check if there is traffic queued towards this peer
2652 * and the core transmit handle is NULL (traffic was stalled).
2653 * If so, call core tmt rdy.
2655 * @param peer_id Short ID of peer to which initiate traffic.
2658 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2660 struct MeshPeerInfo *peer;
2661 struct GNUNET_PeerIdentity id;
2662 struct MeshPeerQueue *q;
2665 peer = peer_get_short (peer_id);
2666 if (NULL != peer->core_transmit)
2669 q = queue_get_next (peer);
2672 /* Might br multicast traffic already sent to this particular peer but
2673 * not to other children in this tunnel.
2674 * This way t->queue_n would be > 0 but the queue of this particular peer
2680 GNUNET_PEER_resolve (peer->id, &id);
2681 peer->core_transmit =
2682 GNUNET_CORE_notify_transmit_ready(core_handle,
2685 GNUNET_TIME_UNIT_FOREVER_REL,
2695 * Send a message to all peers in this tunnel that the tunnel is no longer
2698 * @param t The tunnel whose peers to notify.
2701 tunnel_send_destroy (struct MeshTunnel *t)
2703 struct GNUNET_MESH_TunnelDestroy msg;
2704 struct GNUNET_PeerIdentity id;
2706 msg.header.size = htons (sizeof (msg));
2707 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2708 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2709 msg.tid = htonl (t->id.tid);
2710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2711 " sending tunnel destroy for tunnel: %s [%X]\n",
2712 GNUNET_i2s (&msg.oid), t->id.tid);
2714 if (0 != t->next_hop)
2716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2717 GNUNET_PEER_resolve (t->next_hop, &id);
2718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2719 " sending forward to %s\n",
2721 send_prebuilt_message (&msg.header, t->next_hop, t);
2723 if (0 != t->prev_hop)
2725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2726 GNUNET_PEER_resolve (t->prev_hop, &id);
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2728 " sending back to %s\n",
2730 send_prebuilt_message (&msg.header, t->prev_hop, t);
2736 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2738 * @param t Tunnel which to cancel.
2739 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2742 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2744 struct MeshPeerInfo *peer_info;
2745 struct MeshPeerQueue *pq;
2746 struct MeshPeerQueue *next;
2748 peer_info = peer_get_short (neighbor);
2749 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2752 if (pq->tunnel == t)
2754 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2755 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2757 // Should have been removed on destroy children
2760 queue_destroy (pq, GNUNET_YES);
2763 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2765 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2766 peer_info->core_transmit = NULL;
2772 * Destroy the tunnel and free any allocated resources linked to it.
2774 * @param t the tunnel to destroy
2776 * @return GNUNET_OK on success
2779 tunnel_destroy (struct MeshTunnel *t)
2781 struct MeshClient *c;
2782 struct GNUNET_HashCode hash;
2793 struct GNUNET_PeerIdentity id;
2795 GNUNET_PEER_resolve (t->id.oid, &id);
2796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2797 GNUNET_i2s (&id), t->id.tid);
2799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2803 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2804 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2812 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2814 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2821 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2822 for (i = 0; i < t->nclients; i++)
2826 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2832 for (i = 0; i < t->nignore; i++)
2836 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2843 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
2844 GNUNET_free_non_null (t->clients);
2845 GNUNET_free_non_null (t->ignore);
2846 GNUNET_free_non_null (t->clients_fc);
2848 peer_cancel_queues (t->next_hop, t);
2849 peer_cancel_queues (t->prev_hop, t);
2851 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2852 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2855 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2860 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
2863 * Tunnel is empty: destroy it.
2865 * @param cls Closure (Tunnel).
2866 * @param tc TaskContext.
2869 tunnel_destroy_empty_delayed (void *cls,
2870 const struct GNUNET_SCHEDULER_TaskContext *tc)
2872 struct MeshTunnel *t = cls;
2874 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
2875 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2878 if (0 != t->nclients)
2883 struct GNUNET_PeerIdentity id;
2885 GNUNET_PEER_resolve (t->id.oid, &id);
2886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2887 "executing destruction of empty tunnel %s [%X]\n",
2888 GNUNET_i2s (&id), t->id.tid);
2892 tunnel_send_destroy (t);
2893 if (0 == t->pending_messages)
2896 t->destroy = GNUNET_YES;
2901 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
2903 * @param t Tunnel to destroy if empty.
2906 tunnel_destroy_empty (struct MeshTunnel *t)
2908 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u %u\n",
2912 t->delayed_destroy, t->nclients);
2918 struct GNUNET_PeerIdentity id;
2920 GNUNET_PEER_resolve (t->id.oid, &id);
2921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2922 "scheduling destruction of empty tunnel %s [%X]\n",
2923 GNUNET_i2s (&id), t->id.tid);
2927 t->delayed_destroy =
2928 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
2929 &tunnel_destroy_empty_delayed,
2935 * Create a new tunnel
2937 * @param owner Who is the owner of the tunnel (short ID).
2938 * @param tid Tunnel Number of the tunnel.
2939 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2940 * @param local Tunnel Number for the tunnel, for the client point of view.
2942 * @return A new initialized tunnel. NULL on error.
2944 static struct MeshTunnel *
2945 tunnel_new (GNUNET_PEER_Id owner,
2946 MESH_TunnelNumber tid,
2947 struct MeshClient *client,
2948 MESH_TunnelNumber local)
2950 struct MeshTunnel *t;
2951 struct GNUNET_HashCode hash;
2953 if (n_tunnels >= max_tunnels && NULL == client)
2956 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2959 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2961 t->next_fc.last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2962 t->next_fc.last_pid_recv = (uint32_t) -1;
2963 t->prev_fc.last_pid_sent = (uint32_t) -1;
2964 t->prev_fc.last_pid_recv = (uint32_t) -1;
2965 t->next_fc.last_ack_sent = INITIAL_WINDOW_SIZE - 1;
2966 t->next_fc.last_ack_recv = INITIAL_WINDOW_SIZE - 1;
2967 t->prev_fc.last_ack_sent = INITIAL_WINDOW_SIZE - 1;
2968 t->prev_fc.last_ack_recv = INITIAL_WINDOW_SIZE - 1;
2969 t->local_tid = local;
2971 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2973 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2975 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2976 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2983 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2990 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2992 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2993 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2997 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3008 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3009 * client when the client disconnects. If the client is not the owner, the
3010 * owner will get notified if no more clients are in the tunnel and the client
3011 * get removed from the tunnel's list.
3013 * @param cls closure (client that is disconnecting)
3014 * @param key the hash of the local tunnel id (used to access the hashmap)
3015 * @param value the value stored at the key (tunnel to destroy)
3017 * @return GNUNET_OK, keep iterating.
3020 tunnel_destroy_iterator (void *cls,
3021 const struct GNUNET_HashCode * key,
3024 struct MeshTunnel *t = value;
3025 struct MeshClient *c = cls;
3027 send_client_tunnel_disconnect (t, c);
3030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
3031 tunnel_delete_client (t, c);
3032 client_delete_tunnel (c, t);
3033 tunnel_destroy_empty (t);
3036 tunnel_send_destroy (t);
3038 t->destroy = GNUNET_YES;
3045 * Timeout function, destroys tunnel if called
3047 * @param cls Closure (tunnel to destroy).
3048 * @param tc TaskContext
3051 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3053 struct MeshTunnel *t = cls;
3054 struct GNUNET_PeerIdentity id;
3056 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3057 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3059 GNUNET_PEER_resolve(t->id.oid, &id);
3060 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3061 "Tunnel %s [%X] timed out. Destroying.\n",
3062 GNUNET_i2s(&id), t->id.tid);
3063 send_clients_tunnel_destroy (t);
3068 * Resets the tunnel timeout. Starts it if no timeout was running.
3070 * @param t Tunnel whose timeout to reset.
3072 * TODO use heap to improve efficiency of scheduler.
3075 tunnel_reset_timeout (struct MeshTunnel *t)
3077 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
3079 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
3080 GNUNET_SCHEDULER_cancel (t->maintenance_task);
3081 t->maintenance_task =
3082 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3083 (refresh_path_time, 4), &tunnel_timeout, t);
3087 /******************************************************************************/
3088 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3089 /******************************************************************************/
3092 * Function to send a create path packet to a peer.
3094 * @param cls closure
3095 * @param size number of bytes available in buf
3096 * @param buf where the callee should write the message
3097 * @return number of bytes written to buf
3100 send_core_path_create (void *cls, size_t size, void *buf)
3102 struct MeshTunnel *t = cls;
3103 struct GNUNET_MESH_ManipulatePath *msg;
3104 struct GNUNET_PeerIdentity *peer_ptr;
3105 struct MeshPeerPath *p = t->path;
3110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3112 sizeof (struct GNUNET_MESH_ManipulatePath) +
3113 p->length * sizeof (struct GNUNET_PeerIdentity);
3115 if (size < size_needed || NULL == buf)
3120 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3121 msg->header.size = htons (size_needed);
3122 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3123 msg->tid = ntohl (t->id.tid);
3126 if (GNUNET_YES == t->nobuffer)
3127 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3128 msg->opt = htonl(opt);
3131 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3132 for (i = 0; i < p->length; i++)
3134 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3138 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3144 * Creates a path ack message in buf and frees all unused resources.
3146 * @param cls closure (MeshTransmissionDescriptor)
3147 * @param size number of bytes available in buf
3148 * @param buf where the callee should write the message
3149 * @return number of bytes written to buf
3152 send_core_path_ack (void *cls, size_t size, void *buf)
3154 struct MeshTransmissionDescriptor *info = cls;
3155 struct GNUNET_MESH_PathACK *msg = buf;
3157 GNUNET_assert (NULL != info);
3158 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3163 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3164 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3165 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3166 msg->tid = htonl (info->origin->tid);
3167 msg->peer_id = my_full_id;
3170 /* TODO add signature */
3172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3173 return sizeof (struct GNUNET_MESH_PathACK);
3178 * Free a transmission that was already queued with all resources
3179 * associated to the request.
3181 * @param queue Queue handler to cancel.
3182 * @param clear_cls Is it necessary to free associated cls?
3185 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3187 struct MeshTransmissionDescriptor *dd;
3188 // unsigned int max;
3190 if (GNUNET_YES == clear_cls)
3192 switch (queue->type)
3194 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3195 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3196 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3198 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3199 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3200 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3201 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3202 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3204 " prebuilt message\n");
3205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3207 GNUNET_MESH_DEBUG_M2S(queue->type));
3209 GNUNET_free (dd->data);
3211 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3217 " type %s unknown!\n",
3218 GNUNET_MESH_DEBUG_M2S(queue->type));
3220 GNUNET_free_non_null (queue->cls);
3222 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3223 queue->peer->queue_tail,
3226 /* Delete from child_fc in the appropiate tunnel */
3227 // max = queue->tunnel->fwd_queue_max;
3228 // GNUNET_PEER_resolve (queue->peer->id, &id);
3229 // if (NULL != cinfo)
3231 // for (i = 0; i < cinfo->send_buffer_n; i++)
3233 // i2 = (cinfo->send_buffer_start + i) % max;
3234 // if (cinfo->send_buffer[i2] == queue)
3236 // /* Found corresponding entry in the send_buffer. Move all others back. */
3240 // for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
3242 // j2 = (cinfo->send_buffer_start + j) % max;
3243 // j3 = (cinfo->send_buffer_start + j + 1) % max;
3244 // cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
3247 // cinfo->send_buffer[j3] = NULL;
3248 // cinfo->send_buffer_n--;
3253 GNUNET_free (queue);
3258 * @brief Get the next transmittable message from the queue.
3260 * This will be the head, except in the case of being a data packet
3261 * not allowed by the destination peer.
3263 * @param peer Destination peer.
3265 * @return The next viable MeshPeerQueue element to send to that peer.
3266 * NULL when there are no transmittable messages.
3268 struct MeshPeerQueue *
3269 queue_get_next (const struct MeshPeerInfo *peer)
3271 struct MeshPeerQueue *q;
3273 struct MeshTransmissionDescriptor *info;
3274 // struct GNUNET_MESH_Unicast *ucast;
3275 // struct GNUNET_MESH_ToOrigin *to_orig;
3276 struct GNUNET_PeerIdentity id;
3278 // uint32_t ack; FIXME
3280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
3281 for (q = peer->queue_head; NULL != q; q = q->next)
3285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3287 GNUNET_MESH_DEBUG_M2S(q->type));
3290 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3291 // ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
3292 // pid = ntohl (ucast->pid);
3293 GNUNET_PEER_resolve (info->peer->id, &id);
3294 // ack = cinfo->fwd_ack;
3296 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3297 // to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
3298 // pid = ntohl (to_orig->pid);
3299 // ack = t->bck_ack;
3302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3306 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3307 // "********* ACK: %u, PID: %u\n",
3309 // if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
3311 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3312 // "********* OK!\n");
3317 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3318 // "********* NEXT!\n");
3321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3322 "********* nothing found\n");
3328 queue_send (void *cls, size_t size, void *buf)
3330 struct MeshPeerInfo *peer = cls;
3331 struct GNUNET_MessageHeader *msg;
3332 struct MeshPeerQueue *queue;
3333 struct MeshTunnel *t;
3334 struct GNUNET_PeerIdentity dst_id;
3335 struct MeshFlowControl *fc;
3338 peer->core_transmit = NULL;
3340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3341 queue = queue_get_next (peer);
3343 /* Queue has no internal mesh traffic nor sendable payload */
3346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
3347 if (NULL == peer->queue_head)
3348 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3353 GNUNET_PEER_resolve (peer->id, &dst_id);
3354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3355 "********* towards %s\n",
3356 GNUNET_i2s(&dst_id));
3357 /* Check if buffer size is enough for the message */
3358 if (queue->size > size)
3360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3361 "********* not enough room, reissue\n");
3362 peer->core_transmit =
3363 GNUNET_CORE_notify_transmit_ready (core_handle,
3366 GNUNET_TIME_UNIT_FOREVER_REL,
3373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3376 GNUNET_assert (0 < t->pending_messages);
3377 t->pending_messages--;
3378 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
3380 t->next_fc.queue_n--;
3381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3382 "********* unicast: t->q (%u/%u)\n",
3383 t->next_fc.queue_n, t->queue_max);
3385 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
3387 t->prev_fc.queue_n--;
3388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3389 "********* to orig: t->q (%u/%u)\n",
3390 t->prev_fc.queue_n, t->queue_max);
3394 switch (queue->type)
3397 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3398 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3399 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3400 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3401 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3402 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3404 "********* raw: %s\n",
3405 GNUNET_MESH_DEBUG_M2S (queue->type));
3407 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3408 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3409 data_size = send_core_data_raw (queue->cls, size, buf);
3410 msg = (struct GNUNET_MessageHeader *) buf;
3411 switch (ntohs (msg->type)) // Type of preconstructed message
3413 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3414 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3416 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3417 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3423 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3425 data_size = send_core_path_create (queue->cls, size, buf);
3427 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3429 data_size = send_core_path_ack (queue->cls, size, buf);
3433 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3434 "********* type unknown: %u\n",
3438 switch (queue->type)
3440 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3441 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3442 // if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
3444 // GNUNET_break (0);
3445 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3446 // "at pos %u (%p) != %p\n",
3447 // cinfo->send_buffer_start,
3448 // cinfo->send_buffer[cinfo->send_buffer_start],
3451 // if (cinfo->send_buffer_n > 0)
3453 // cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
3454 // cinfo->send_buffer_n--;
3455 // cinfo->send_buffer_start++;
3456 // cinfo->send_buffer_start %= t->fwd_queue_max;
3460 // GNUNET_break (0);
3467 /* Free queue, but cls was freed by send_core_* */
3468 queue_destroy (queue, GNUNET_NO);
3470 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
3476 /* If more data in queue, send next */
3477 queue = queue_get_next (peer);
3480 struct GNUNET_PeerIdentity id;
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3483 GNUNET_PEER_resolve (peer->id, &id);
3484 peer->core_transmit =
3485 GNUNET_CORE_notify_transmit_ready(core_handle,
3488 GNUNET_TIME_UNIT_FOREVER_REL,
3496 if (NULL != peer->queue_head)
3498 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3499 "********* %s stalled\n",
3500 GNUNET_i2s(&my_full_id));
3501 if (peer->id == t->next_hop)
3505 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3508 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3519 * @brief Queue and pass message to core when possible.
3521 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3522 * accounts for it. In case the queue is full, the message is dropped and
3525 * Otherwise, message is treated as internal and allowed to go regardless of
3528 * @param cls Closure (@c type dependant). It will be used by queue_send to
3529 * build the message to be sent if not already prebuilt.
3530 * @param type Type of the message, 0 for a raw message.
3531 * @param size Size of the message.
3532 * @param dst Neighbor to send message to.
3533 * @param t Tunnel this message belongs to.
3536 queue_add (void *cls, uint16_t type, size_t size,
3537 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3539 struct MeshPeerQueue *queue;
3540 struct GNUNET_PeerIdentity id;
3544 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3546 n = &t->next_fc.queue_n;
3548 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3550 n = &t->prev_fc.queue_n;
3554 if (*n >= t->queue_max)
3557 GNUNET_STATISTICS_update(stats,
3558 "# messages dropped (buffer full)",
3560 return; // Drop message
3564 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3570 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3571 if (NULL == dst->core_transmit)
3573 GNUNET_PEER_resolve (dst->id, &id);
3574 dst->core_transmit =
3575 GNUNET_CORE_notify_transmit_ready (core_handle,
3578 GNUNET_TIME_UNIT_FOREVER_REL,
3584 t->pending_messages++;
3588 /******************************************************************************/
3589 /******************** MESH NETWORK HANDLERS **************************/
3590 /******************************************************************************/
3594 * Core handler for path creation
3596 * @param cls closure
3597 * @param message message
3598 * @param peer peer identity this notification is about
3600 * @return GNUNET_OK to keep the connection open,
3601 * GNUNET_SYSERR to close it (signal serious error)
3604 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3605 const struct GNUNET_MessageHeader *message)
3607 unsigned int own_pos;
3610 MESH_TunnelNumber tid;
3611 struct GNUNET_MESH_ManipulatePath *msg;
3612 struct GNUNET_PeerIdentity *pi;
3613 struct GNUNET_HashCode hash;
3614 struct MeshPeerPath *path;
3615 struct MeshPeerInfo *dest_peer_info;
3616 struct MeshPeerInfo *orig_peer_info;
3617 struct MeshTunnel *t;
3619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3620 "Received a path create msg [%s]\n",
3621 GNUNET_i2s (&my_full_id));
3622 size = ntohs (message->size);
3623 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3625 GNUNET_break_op (0);
3629 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3630 if (size % sizeof (struct GNUNET_PeerIdentity))
3632 GNUNET_break_op (0);
3635 size /= sizeof (struct GNUNET_PeerIdentity);
3638 GNUNET_break_op (0);
3641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3642 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3644 tid = ntohl (msg->tid);
3645 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3647 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
3648 t = tunnel_get (pi, tid);
3649 if (NULL == t) // FIXME only for INCOMING tunnels?
3653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3654 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3657 // FIXME notify failure
3660 opt = ntohl (msg->opt);
3661 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
3663 t->nobuffer = GNUNET_YES;
3664 t->prev_fc.last_ack_sent = t->prev_fc.last_pid_recv + 1;
3666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3668 if (GNUNET_YES == t->nobuffer)
3673 // FIXME only assign a local tid if a local client is interested (on demand)
3674 while (NULL != tunnel_get_incoming (next_local_tid))
3675 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3676 t->local_tid_dest = next_local_tid++;
3677 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3680 tunnel_reset_timeout (t);
3681 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3683 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
3684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3692 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3693 if (NULL == dest_peer_info)
3695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3696 " Creating PeerInfo for destination.\n");
3697 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3698 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3699 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3701 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3703 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3704 if (NULL == orig_peer_info)
3706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3707 " Creating PeerInfo for origin.\n");
3708 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3709 orig_peer_info->id = GNUNET_PEER_intern (pi);
3710 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3711 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3714 path = path_new (size);
3716 for (i = 0; i < size; i++)
3718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3719 GNUNET_i2s (&pi[i]));
3720 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3721 if (path->peers[i] == myid)
3724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3727 /* cannot be self, must be 'not found' */
3728 /* create path: self not found in path through self */
3729 GNUNET_break_op (0);
3730 path_destroy (path);
3734 path_add_to_peers (path, GNUNET_NO);
3735 t->prev_hop = path->peers[own_pos - 1];
3736 GNUNET_PEER_change_rc (t->prev_hop, 1);
3737 if (own_pos == size - 1)
3739 /* It is for us! Send ack. */
3740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3741 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3747 struct MeshPeerPath *path2;
3749 t->next_hop = path->peers[own_pos + 1];
3750 GNUNET_PEER_change_rc(t->next_hop, 1);
3752 /* It's for somebody else! Retransmit. */
3753 path2 = path_duplicate (path);
3754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3755 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3756 path2 = path_duplicate (path);
3757 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3758 send_create_path (dest_peer_info, path, t);
3765 * Core handler for path destruction
3767 * @param cls closure
3768 * @param message message
3769 * @param peer peer identity this notification is about
3771 * @return GNUNET_OK to keep the connection open,
3772 * GNUNET_SYSERR to close it (signal serious error)
3775 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3776 const struct GNUNET_MessageHeader *message)
3778 struct GNUNET_MESH_ManipulatePath *msg;
3779 struct GNUNET_PeerIdentity *pi;
3780 struct MeshPeerPath *path;
3781 struct MeshTunnel *t;
3782 unsigned int own_pos;
3786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3787 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3788 size = ntohs (message->size);
3789 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3791 GNUNET_break_op (0);
3795 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3796 if (size % sizeof (struct GNUNET_PeerIdentity))
3798 GNUNET_break_op (0);
3801 size /= sizeof (struct GNUNET_PeerIdentity);
3804 GNUNET_break_op (0);
3807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3809 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3810 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3812 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3814 t = tunnel_get (pi, ntohl (msg->tid));
3817 /* TODO notify back: we don't know this tunnel */
3818 GNUNET_break_op (0);
3821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3822 path = path_new (size);
3824 for (i = 0; i < size; i++)
3826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3827 GNUNET_i2s (&pi[i]));
3828 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3829 if (path->peers[i] == myid)
3832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3833 if (own_pos < path->length - 1)
3834 send_prebuilt_message (message, path->peers[own_pos + 1], t);
3836 send_client_tunnel_disconnect(t, NULL);
3838 // tunnel_delete_peer (t, path->peers[path->length - 1]); FIXME
3839 path_destroy (path);
3845 * Core handler for notifications of broken paths
3847 * @param cls closure
3848 * @param message message
3849 * @param peer peer identity this notification is about
3851 * @return GNUNET_OK to keep the connection open,
3852 * GNUNET_SYSERR to close it (signal serious error)
3855 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3856 const struct GNUNET_MessageHeader *message)
3858 struct GNUNET_MESH_PathBroken *msg;
3859 struct MeshTunnel *t;
3861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3862 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3863 msg = (struct GNUNET_MESH_PathBroken *) message;
3864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3865 GNUNET_i2s (&msg->peer1));
3866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3867 GNUNET_i2s (&msg->peer2));
3868 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3871 GNUNET_break_op (0);
3874 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3875 GNUNET_PEER_search (&msg->peer2));
3882 * Core handler for tunnel destruction
3884 * @param cls closure
3885 * @param message message
3886 * @param peer peer identity this notification is about
3888 * @return GNUNET_OK to keep the connection open,
3889 * GNUNET_SYSERR to close it (signal serious error)
3892 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3893 const struct GNUNET_MessageHeader *message)
3895 struct GNUNET_MESH_TunnelDestroy *msg;
3896 struct MeshTunnel *t;
3899 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3901 "Got a TUNNEL DESTROY packet from %s\n",
3903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3904 " for tunnel %s [%u]\n",
3905 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3906 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3907 /* Check signature */
3910 /* Probably already got the message from another path,
3911 * destroyed the tunnel and retransmitted to children.
3914 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3918 pid = GNUNET_PEER_search (peer);
3919 if (pid != t->prev_hop && 0 < t->nclients)
3921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3922 "still in use by %u clients\n",
3926 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3928 /* Tunnel was incoming, notify clients */
3929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3930 t->local_tid, t->local_tid_dest);
3931 send_clients_tunnel_destroy (t);
3933 tunnel_send_destroy (t);
3934 t->destroy = GNUNET_YES;
3935 // TODO: add timeout to destroy the tunnel anyway
3941 * Core handler for mesh network traffic going from the origin to a peer
3943 * @param cls closure
3944 * @param peer peer identity this notification is about
3945 * @param message message
3946 * @return GNUNET_OK to keep the connection open,
3947 * GNUNET_SYSERR to close it (signal serious error)
3950 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3951 const struct GNUNET_MessageHeader *message)
3953 struct GNUNET_MESH_Unicast *msg;
3954 struct MeshTunnel *t;
3959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3962 size = ntohs (message->size);
3964 sizeof (struct GNUNET_MESH_Unicast) +
3965 sizeof (struct GNUNET_MessageHeader))
3970 msg = (struct GNUNET_MESH_Unicast *) message;
3971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3972 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3974 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3977 /* TODO notify back: we don't know this tunnel */
3978 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3979 GNUNET_break_op (0);
3982 pid = ntohl (msg->pid);
3983 if (t->prev_fc.last_pid_recv == pid)
3985 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3986 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3987 " Already seen pid %u, DROPPING!\n", pid);
3992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3993 " pid %u not seen yet, forwarding\n", pid);
3996 t->prev_fc.last_pid_recv = pid;
3998 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
4000 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4001 GNUNET_break_op (0);
4002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4003 "Received PID %u, ACK %u\n",
4004 pid, t->prev_fc.last_ack_sent);
4008 tunnel_reset_timeout (t);
4009 if (t->dest == myid)
4011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4012 " it's for us! sending to clients...\n");
4013 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4014 send_subscribed_clients (message, &msg[1].header, t);
4015 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4018 ttl = ntohl (msg->ttl);
4019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4022 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4023 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4024 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4028 " not for us, retransmitting...\n");
4030 if (GNUNET_YES == t->nobuffer &&
4031 GNUNET_YES == GMC_is_pid_bigger (pid, t->next_fc.last_ack_recv))
4033 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4034 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n",
4035 pid, t->next_fc.last_ack_recv);
4036 GNUNET_break_op (0);
4039 send_prebuilt_message (message, t->next_hop, t);
4040 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4046 * Core handler for mesh network traffic toward the owner of a tunnel
4048 * @param cls closure
4049 * @param message message
4050 * @param peer peer identity this notification is about
4052 * @return GNUNET_OK to keep the connection open,
4053 * GNUNET_SYSERR to close it (signal serious error)
4056 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4057 const struct GNUNET_MessageHeader *message)
4059 struct GNUNET_MESH_ToOrigin *msg;
4060 struct MeshPeerInfo *peer_info;
4061 struct MeshTunnel *t;
4065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4067 size = ntohs (message->size);
4068 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4069 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4071 GNUNET_break_op (0);
4074 msg = (struct GNUNET_MESH_ToOrigin *) message;
4075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4076 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4077 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4078 pid = ntohl (msg->pid);
4082 /* TODO notify that we dont know this tunnel (whom)? */
4083 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4085 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
4086 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4091 if (t->next_fc.last_pid_recv == pid)
4093 /* already seen this packet, drop */
4094 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
4095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4096 " Already seen pid %u, DROPPING!\n", pid);
4097 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4102 " pid %u not seen yet, forwarding\n", pid);
4103 t->next_fc.last_pid_recv = pid;
4105 if (NULL != t->owner)
4108 struct GNUNET_MESH_ToOrigin *copy;
4110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4111 " it's for us! sending to clients...\n");
4112 /* TODO signature verification */
4113 memcpy (cbuf, message, size);
4114 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4115 copy->tid = htonl (t->local_tid);
4116 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4117 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4118 ©->header, GNUNET_NO);
4119 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4123 " not for us, retransmitting...\n");
4125 peer_info = peer_get (&msg->oid);
4126 if (NULL == peer_info)
4128 /* unknown origin of tunnel */
4132 if (0 == t->prev_hop) /* No owner AND no prev hop */
4134 if (GNUNET_YES == t->destroy)
4136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4137 "to orig received on a dying tunnel %s [%X]\n",
4138 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4142 "unknown to origin at %s\n",
4143 GNUNET_i2s (&my_full_id));
4144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4148 "for tunnel %s [%X]\n",
4149 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4152 send_prebuilt_message (message, t->prev_hop, t);
4153 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4160 * Core handler for mesh network traffic point-to-point acks.
4162 * @param cls closure
4163 * @param message message
4164 * @param peer peer identity this notification is about
4166 * @return GNUNET_OK to keep the connection open,
4167 * GNUNET_SYSERR to close it (signal serious error)
4170 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4171 const struct GNUNET_MessageHeader *message)
4173 struct GNUNET_MESH_ACK *msg;
4174 struct MeshTunnel *t;
4177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4179 msg = (struct GNUNET_MESH_ACK *) message;
4181 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4185 /* TODO notify that we dont know this tunnel (whom)? */
4186 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4189 ack = ntohl (msg->pid);
4190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4192 /* Is this a forward or backward ACK? */
4193 if (t->prev_hop != GNUNET_PEER_search (peer))
4196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4197 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task &&
4198 GMC_is_pid_bigger (ack, t->next_fc.last_ack_recv))
4200 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
4201 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
4202 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
4204 t->next_fc.last_ack_recv = ack;
4205 peer_unlock_queue (t->next_hop);
4206 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4211 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
4212 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
4214 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
4215 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
4216 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
4218 t->prev_fc.last_ack_recv = ack;
4219 peer_unlock_queue (t->prev_hop);
4220 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4227 * Core handler for mesh network traffic point-to-point ack polls.
4229 * @param cls closure
4230 * @param message message
4231 * @param peer peer identity this notification is about
4233 * @return GNUNET_OK to keep the connection open,
4234 * GNUNET_SYSERR to close it (signal serious error)
4237 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4238 const struct GNUNET_MessageHeader *message)
4240 struct GNUNET_MESH_Poll *msg;
4241 struct MeshTunnel *t;
4243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4246 msg = (struct GNUNET_MESH_Poll *) message;
4248 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4252 /* TODO notify that we dont know this tunnel (whom)? */
4253 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4254 GNUNET_break_op (0);
4258 /* Is this a forward or backward ACK? */
4259 if (t->prev_hop != GNUNET_PEER_search(peer))
4261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4262 /* FIXME cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send */
4263 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4268 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4275 * Core handler for path ACKs
4277 * @param cls closure
4278 * @param message message
4279 * @param peer peer identity this notification is about
4281 * @return GNUNET_OK to keep the connection open,
4282 * GNUNET_SYSERR to close it (signal serious error)
4285 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4286 const struct GNUNET_MessageHeader *message)
4288 struct GNUNET_MESH_PathACK *msg;
4289 struct MeshPeerInfo *peer_info;
4290 struct MeshPeerPath *p;
4291 struct MeshTunnel *t;
4293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4294 GNUNET_i2s (&my_full_id));
4295 msg = (struct GNUNET_MESH_PathACK *) message;
4296 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4299 /* TODO notify that we don't know the tunnel */
4300 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4302 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4306 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4308 peer_info = peer_get (&msg->peer_id);
4309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4310 GNUNET_i2s (&msg->peer_id));
4311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4314 /* Add path to peers? */
4318 path_add_to_peers (p, GNUNET_YES);
4325 /* Message for us? */
4326 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4329 if (NULL == t->owner)
4331 GNUNET_break_op (0);
4334 if (NULL != peer_info->dhtget)
4336 GNUNET_DHT_get_stop (peer_info->dhtget);
4337 peer_info->dhtget = NULL;
4342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4343 " not for us, retransmitting...\n");
4344 peer_info = peer_get (&msg->oid);
4345 send_prebuilt_message (message, t->prev_hop, t);
4351 * Core handler for mesh keepalives.
4353 * @param cls closure
4354 * @param message message
4355 * @param peer peer identity this notification is about
4356 * @return GNUNET_OK to keep the connection open,
4357 * GNUNET_SYSERR to close it (signal serious error)
4359 * TODO: Check who we got this from, to validate route.
4362 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4363 const struct GNUNET_MessageHeader *message)
4365 struct GNUNET_MESH_TunnelKeepAlive *msg;
4366 struct MeshTunnel *t;
4368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4371 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4372 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4376 /* TODO notify that we dont know that tunnel */
4377 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4382 tunnel_reset_timeout (t);
4384 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4385 send_prebuilt_message (message, t->next_hop, t);
4392 * Functions to handle messages from core
4394 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4395 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4396 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4397 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4398 sizeof (struct GNUNET_MESH_PathBroken)},
4399 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4400 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4401 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4402 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4403 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4404 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4405 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4406 sizeof (struct GNUNET_MESH_ACK)},
4407 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4408 sizeof (struct GNUNET_MESH_Poll)},
4409 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4410 sizeof (struct GNUNET_MESH_PathACK)},
4416 /******************************************************************************/
4417 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4418 /******************************************************************************/
4423 * notify_client_connection_failure: notify a client that the connection to the
4424 * requested remote peer is not possible (for instance, no route found)
4425 * Function called when the socket is ready to queue more data. "buf" will be
4426 * NULL and "size" zero if the socket was closed for writing in the meantime.
4428 * @param cls closure
4429 * @param size number of bytes available in buf
4430 * @param buf where the callee should write the message
4431 * @return number of bytes written to buf
4434 notify_client_connection_failure (void *cls, size_t size, void *buf)
4437 struct MeshPeerInfo *peer_info;
4438 struct GNUNET_MESH_PeerControl *msg;
4439 struct GNUNET_PeerIdentity id;
4441 if (0 == size && NULL == buf)
4443 // TODO retry? cancel?
4447 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4448 peer_info = (struct MeshPeerInfo *) cls;
4449 msg = (struct GNUNET_MESH_PeerControl *) buf;
4450 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4451 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4452 // msg->tunnel_id = htonl(peer_info->t->tid);
4453 GNUNET_PEER_resolve (peer_info->id, &id);
4454 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4462 * Send keepalive packets for a tunnel.
4464 * @param cls Closure (tunnel for which to send the keepalive).
4465 * @param tc Notification context.
4468 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4470 struct MeshTunnel *t = cls;
4471 struct GNUNET_MESH_TunnelKeepAlive *msg;
4472 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4475 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4476 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4477 NULL == t->owner || 0 == t->local_tid || 0 != t->prev_hop)
4482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4483 "sending keepalive for tunnel %d\n", t->id.tid);
4485 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4486 msg->header.size = htons (size);
4487 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4488 msg->oid = my_full_id;
4489 msg->tid = htonl (t->id.tid);
4490 send_prebuilt_message (&msg->header, t->next_hop, t);
4492 t->maintenance_task =
4493 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4498 * Function to process paths received for a new peer addition. The recorded
4499 * paths form the initial tunnel, which can be optimized later.
4500 * Called on each result obtained for the DHT search.
4502 * @param cls closure
4503 * @param exp when will this value expire
4504 * @param key key of the result
4505 * @param get_path path of the get request
4506 * @param get_path_length lenght of get_path
4507 * @param put_path path of the put request
4508 * @param put_path_length length of the put_path
4509 * @param type type of the result
4510 * @param size number of bytes in data
4511 * @param data pointer to the result data
4513 * TODO: re-issue the request after certain time? cancel after X results?
4516 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4517 const struct GNUNET_HashCode * key,
4518 const struct GNUNET_PeerIdentity *get_path,
4519 unsigned int get_path_length,
4520 const struct GNUNET_PeerIdentity *put_path,
4521 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4522 size_t size, const void *data)
4524 struct MeshPeerInfo *peer = cls;
4525 struct MeshPeerPath *p;
4526 struct GNUNET_PeerIdentity pi;
4529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4530 GNUNET_PEER_resolve (peer->id, &pi);
4531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4533 p = path_build_from_dht (get_path, get_path_length,
4534 put_path, put_path_length);
4535 path_add_to_peers (p, GNUNET_NO);
4537 for (i = 0; i < peer->ntunnels; i++)
4539 peer_connect (peer, peer->tunnels[i]); // FIXME add if
4546 /******************************************************************************/
4547 /********************* MESH LOCAL HANDLES **************************/
4548 /******************************************************************************/
4552 * Handler for client disconnection
4554 * @param cls closure
4555 * @param client identification of the client; NULL
4556 * for the last call when the server is destroyed
4559 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4561 struct MeshClient *c;
4562 struct MeshClient *next;
4564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
4567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4574 if (c->handle != client)
4576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
4580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4582 GNUNET_SERVER_client_drop (c->handle);
4583 c->shutting_down = GNUNET_YES;
4584 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4585 &tunnel_destroy_iterator, c);
4586 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4587 &tunnel_destroy_iterator, c);
4588 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
4589 &tunnel_destroy_iterator, c);
4590 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4591 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4592 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
4594 if (NULL != c->types)
4595 GNUNET_CONTAINER_multihashmap_destroy (c->types);
4597 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4600 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4609 * Handler for new clients
4611 * @param cls closure
4612 * @param client identification of the client
4613 * @param message the actual message, which includes messages the client wants
4616 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4617 const struct GNUNET_MessageHeader *message)
4619 struct GNUNET_MESH_ClientConnect *cc_msg;
4620 struct MeshClient *c;
4626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
4628 /* Check data sanity */
4629 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4630 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4631 ntypes = ntohs (cc_msg->types);
4632 if (size != ntypes * sizeof (uint16_t))
4635 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4639 /* Create new client structure */
4640 c = GNUNET_malloc (sizeof (struct MeshClient));
4641 c->id = next_client_id++; // overflow not important: just for debug
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
4643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u types\n", ntypes);
4645 GNUNET_SERVER_client_keep (client);
4649 struct GNUNET_HashCode hc;
4651 t = (uint16_t *) &cc_msg[1];
4652 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
4653 for (i = 0; i < ntypes; i++)
4656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
4657 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc); // FIXME pseudo hash
4659 /* store in clients hashmap */
4660 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
4661 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4662 /* store in global hashmap */
4663 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
4664 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4668 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4669 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4670 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4671 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4672 GNUNET_SERVER_notification_context_add (nc, client);
4673 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4675 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4681 * Handler for requests of new tunnels
4683 * @param cls Closure.
4684 * @param client Identification of the client.
4685 * @param message The actual message.
4688 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4689 const struct GNUNET_MessageHeader *message)
4691 struct GNUNET_MESH_TunnelMessage *t_msg;
4692 struct MeshPeerInfo *peer_info;
4693 struct MeshTunnel *t;
4694 struct MeshClient *c;
4695 MESH_TunnelNumber tid;
4697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4699 /* Sanity check for client registration */
4700 if (NULL == (c = client_get (client)))
4703 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4708 /* Message sanity check */
4709 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4712 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4716 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4718 GNUNET_i2s (&t_msg->peer));
4719 /* Sanity check for tunnel numbering */
4720 tid = ntohl (t_msg->tunnel_id);
4721 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4724 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4727 /* Sanity check for duplicate tunnel IDs */
4728 if (NULL != tunnel_get_by_local_id (c, tid))
4731 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4735 while (NULL != tunnel_get_by_pi (myid, next_tid))
4736 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4737 t = tunnel_new (myid, next_tid, c, tid);
4738 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4742 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
4746 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4748 peer_info = peer_get (&t_msg->peer);
4749 GNUNET_array_append (peer_info->tunnels, peer_info->ntunnels, t);
4750 peer_connect (peer_info, t);
4751 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4757 * Handler for requests of deleting tunnels
4759 * @param cls closure
4760 * @param client identification of the client
4761 * @param message the actual message
4764 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4765 const struct GNUNET_MessageHeader *message)
4767 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4768 struct MeshClient *c;
4769 struct MeshTunnel *t;
4770 MESH_TunnelNumber tid;
4772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4773 "Got a DESTROY TUNNEL from client!\n");
4775 /* Sanity check for client registration */
4776 if (NULL == (c = client_get (client)))
4779 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4784 /* Message sanity check */
4785 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4788 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4792 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4794 /* Retrieve tunnel */
4795 tid = ntohl (tunnel_msg->tunnel_id);
4796 t = tunnel_get_by_local_id(c, tid);
4799 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4801 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4804 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4806 client_ignore_tunnel (c, t);
4807 tunnel_destroy_empty (t);
4808 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4811 send_client_tunnel_disconnect (t, c);
4812 client_delete_tunnel (c, t);
4814 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4816 tunnel_send_destroy (t);
4817 peer_remove_tunnel (peer_get_short(t->dest), t);
4818 t->destroy = GNUNET_YES;
4819 /* The tunnel will be destroyed when the last message is transmitted. */
4820 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4826 * Handler for requests of seeting tunnel's buffering policy.
4828 * @param cls Closure (unused).
4829 * @param client Identification of the client.
4830 * @param message The actual message.
4833 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
4834 const struct GNUNET_MessageHeader *message)
4836 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4837 struct MeshClient *c;
4838 struct MeshTunnel *t;
4839 MESH_TunnelNumber tid;
4841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4842 "Got a BUFFER request from client!\n");
4844 /* Sanity check for client registration */
4845 if (NULL == (c = client_get (client)))
4848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4853 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4855 /* Retrieve tunnel */
4856 tid = ntohl (tunnel_msg->tunnel_id);
4857 t = tunnel_get_by_local_id(c, tid);
4860 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4862 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4866 switch (ntohs(message->type))
4868 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
4869 t->nobuffer = GNUNET_NO;
4871 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
4872 t->nobuffer = GNUNET_YES;
4878 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4883 * Handler for client traffic directed to one peer
4885 * @param cls closure
4886 * @param client identification of the client
4887 * @param message the actual message
4890 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4891 const struct GNUNET_MessageHeader *message)
4893 struct MeshClient *c;
4894 struct MeshTunnel *t;
4895 struct GNUNET_MESH_Unicast *data_msg;
4896 MESH_TunnelNumber tid;
4899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4900 "Got a unicast request from a client!\n");
4902 /* Sanity check for client registration */
4903 if (NULL == (c = client_get (client)))
4906 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4911 data_msg = (struct GNUNET_MESH_Unicast *) message;
4913 /* Sanity check for message size */
4914 size = ntohs (message->size);
4915 if (sizeof (struct GNUNET_MESH_Unicast) +
4916 sizeof (struct GNUNET_MessageHeader) > size)
4919 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4923 /* Tunnel exists? */
4924 tid = ntohl (data_msg->tid);
4925 t = tunnel_get_by_local_id (c, tid);
4929 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4933 /* Is it a local tunnel? Then, does client own the tunnel? */
4934 if (t->owner->handle != client)
4937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4941 /* PID should be as expected: client<->service communication */
4942 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4945 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4946 "Unicast PID, expected %u, got %u\n",
4947 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4948 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4952 /* Ok, everything is correct, send the message
4953 * (pretend we got it from a mesh peer)
4956 /* Work around const limitation */
4957 char buf[ntohs (message->size)] GNUNET_ALIGN;
4958 struct GNUNET_MESH_Unicast *copy;
4960 copy = (struct GNUNET_MESH_Unicast *) buf;
4961 memcpy (buf, data_msg, size);
4962 copy->oid = my_full_id;
4963 copy->tid = htonl (t->id.tid);
4964 copy->ttl = htonl (default_ttl);
4965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4966 " calling generic handler...\n");
4967 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
4969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4970 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4977 * Handler for client traffic directed to the origin
4979 * @param cls closure
4980 * @param client identification of the client
4981 * @param message the actual message
4984 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4985 const struct GNUNET_MessageHeader *message)
4987 struct GNUNET_MESH_ToOrigin *data_msg;
4988 struct MeshTunnelClientInfo *clinfo;
4989 struct MeshClient *c;
4990 struct MeshTunnel *t;
4991 MESH_TunnelNumber tid;
4994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4995 "Got a ToOrigin request from a client!\n");
4996 /* Sanity check for client registration */
4997 if (NULL == (c = client_get (client)))
5000 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5005 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
5007 /* Sanity check for message size */
5008 size = ntohs (message->size);
5009 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5010 sizeof (struct GNUNET_MessageHeader) > size)
5013 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5017 /* Tunnel exists? */
5018 tid = ntohl (data_msg->tid);
5019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5020 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5023 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5026 t = tunnel_get_by_local_id (c, tid);
5029 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5030 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5032 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5036 /* It should be sent by someone who has this as incoming tunnel. */
5037 if (GNUNET_NO == client_knows_tunnel (c, t))
5040 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5044 /* PID should be as expected */
5045 clinfo = tunnel_get_client_fc (t, c);
5046 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
5049 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5050 "To Origin PID, expected %u, got %u\n",
5051 clinfo->bck_pid + 1,
5052 ntohl (data_msg->pid));
5053 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5058 /* Ok, everything is correct, send the message
5059 * (pretend we got it from a mesh peer)
5062 char buf[ntohs (message->size)] GNUNET_ALIGN;
5063 struct GNUNET_MESH_ToOrigin *copy;
5065 /* Work around 'const' limitation */
5066 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5067 memcpy (buf, data_msg, size);
5068 GNUNET_PEER_resolve (t->id.oid, ©->oid);
5069 copy->tid = htonl (t->id.tid);
5070 copy->ttl = htonl (default_ttl);
5071 copy->pid = htonl (t->prev_fc.last_pid_sent + 1);
5073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5074 " calling generic handler...\n");
5075 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
5077 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5084 * Handler for client's ACKs for payload traffic.
5086 * @param cls Closure (unused).
5087 * @param client Identification of the client.
5088 * @param message The actual message.
5091 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5092 const struct GNUNET_MessageHeader *message)
5094 struct GNUNET_MESH_LocalAck *msg;
5095 struct MeshTunnel *t;
5096 struct MeshClient *c;
5097 MESH_TunnelNumber tid;
5100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5101 /* Sanity check for client registration */
5102 if (NULL == (c = client_get (client)))
5105 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5110 msg = (struct GNUNET_MESH_LocalAck *) message;
5112 /* Tunnel exists? */
5113 tid = ntohl (msg->tunnel_id);
5114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5115 t = tunnel_get_by_local_id (c, tid);
5119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5120 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5121 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5125 ack = ntohl (msg->max_pid);
5126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
5128 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5129 if (NULL != t->owner && t->owner->handle == client)
5131 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5132 t->next_fc.last_ack_sent = ack;
5133 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5137 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5138 tunnel_set_client_fwd_ack (t, c, ack);
5139 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5142 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5150 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5152 * @param cls Closure (client handle).
5153 * @param key Key (hashed tunnel ID, unused).
5154 * @param value Tunnel info.
5156 * @return GNUNET_YES, to keep iterating.
5159 monitor_all_tunnels_iterator (void *cls,
5160 const struct GNUNET_HashCode * key,
5163 struct GNUNET_SERVER_Client *client = cls;
5164 struct MeshTunnel *t = value;
5165 struct GNUNET_MESH_LocalMonitor *msg;
5167 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5168 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5169 msg->tunnel_id = htonl (t->id.tid);
5170 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5171 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5172 GNUNET_PEER_resolve (t->dest, &msg->destination);
5174 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5175 "* sending info about tunnel %s [%u]\n",
5176 GNUNET_i2s (&msg->owner), t->id.tid);
5178 GNUNET_SERVER_notification_context_unicast (nc, client,
5179 &msg->header, GNUNET_NO);
5185 * Handler for client's MONITOR request.
5187 * @param cls Closure (unused).
5188 * @param client Identification of the client.
5189 * @param message The actual message.
5192 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5193 const struct GNUNET_MessageHeader *message)
5195 struct MeshClient *c;
5197 /* Sanity check for client registration */
5198 if (NULL == (c = client_get (client)))
5201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5205 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5206 "Received get tunnels request from client %u\n",
5208 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5209 monitor_all_tunnels_iterator,
5211 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5212 "Get tunnels request from client %u completed\n",
5214 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5219 * Handler for client's MONITOR_TUNNEL request.
5221 * @param cls Closure (unused).
5222 * @param client Identification of the client.
5223 * @param message The actual message.
5226 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5227 const struct GNUNET_MessageHeader *message)
5229 const struct GNUNET_MESH_LocalMonitor *msg;
5230 struct GNUNET_MESH_LocalMonitor *resp;
5231 struct MeshClient *c;
5232 struct MeshTunnel *t;
5234 /* Sanity check for client registration */
5235 if (NULL == (c = client_get (client)))
5238 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5242 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5243 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5244 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5247 ntohl (msg->tunnel_id));
5248 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5251 /* We don't know the tunnel FIXME */
5252 struct GNUNET_MESH_LocalMonitor warn;
5255 GNUNET_SERVER_notification_context_unicast (nc, client,
5258 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5262 /* Initialize context */
5263 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5265 GNUNET_PEER_resolve (t->dest, &resp->destination);
5266 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5267 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5268 &resp->header, GNUNET_NO);
5271 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5272 "Monitor tunnel request from client %u completed\n",
5274 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5279 * Functions to handle messages from clients
5281 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5282 {&handle_local_new_client, NULL,
5283 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5284 {&handle_local_tunnel_create, NULL,
5285 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5286 sizeof (struct GNUNET_MESH_TunnelMessage)},
5287 {&handle_local_tunnel_destroy, NULL,
5288 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5289 sizeof (struct GNUNET_MESH_TunnelMessage)},
5290 {&handle_local_tunnel_buffer, NULL,
5291 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
5292 sizeof (struct GNUNET_MESH_TunnelMessage)},
5293 {&handle_local_tunnel_buffer, NULL,
5294 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
5295 sizeof (struct GNUNET_MESH_TunnelMessage)},
5296 {&handle_local_unicast, NULL,
5297 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5298 {&handle_local_to_origin, NULL,
5299 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5300 {&handle_local_ack, NULL,
5301 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5302 sizeof (struct GNUNET_MESH_LocalAck)},
5303 {&handle_local_get_tunnels, NULL,
5304 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5305 sizeof (struct GNUNET_MessageHeader)},
5306 {&handle_local_show_tunnel, NULL,
5307 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5308 sizeof (struct GNUNET_MESH_LocalMonitor)},
5314 * To be called on core init/fail.
5316 * @param cls service closure
5317 * @param server handle to the server for this service
5318 * @param identity the public identity of this peer
5321 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5322 const struct GNUNET_PeerIdentity *identity)
5325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5326 core_handle = server;
5327 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5333 GNUNET_i2s (identity));
5334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5336 GNUNET_i2s (&my_full_id));
5337 GNUNET_SCHEDULER_shutdown (); // Try gracefully
5339 GNUNET_abort(); // Try harder
5346 * Method called whenever a given peer connects.
5348 * @param cls closure
5349 * @param peer peer identity this notification is about
5352 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5354 struct MeshPeerInfo *peer_info;
5355 struct MeshPeerPath *path;
5357 DEBUG_CONN ("Peer connected\n");
5358 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5359 peer_info = peer_get (peer);
5360 if (myid == peer_info->id)
5362 DEBUG_CONN (" (self)\n");
5367 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5369 path = path_new (2);
5370 path->peers[0] = myid;
5371 path->peers[1] = peer_info->id;
5372 GNUNET_PEER_change_rc (myid, 1);
5373 GNUNET_PEER_change_rc (peer_info->id, 1);
5374 peer_info_add_path (peer_info, path, GNUNET_YES);
5375 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5381 * Method called whenever a peer disconnects.
5383 * @param cls closure
5384 * @param peer peer identity this notification is about
5387 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5389 struct MeshPeerInfo *pi;
5390 struct MeshPeerQueue *q;
5391 struct MeshPeerQueue *n;
5393 DEBUG_CONN ("Peer disconnected\n");
5394 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5404 /* TODO try to reroute this traffic instead */
5405 queue_destroy(q, GNUNET_YES);
5408 if (NULL != pi->core_transmit)
5410 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5411 pi->core_transmit = NULL;
5413 peer_remove_path (pi, pi->id, myid);
5416 DEBUG_CONN (" (self)\n");
5418 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5423 /******************************************************************************/
5424 /************************ MAIN FUNCTIONS ****************************/
5425 /******************************************************************************/
5428 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5430 * @param cls closure
5431 * @param key current key code
5432 * @param value value in the hash map
5433 * @return GNUNET_YES if we should continue to iterate,
5437 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5439 struct MeshTunnel *t = value;
5446 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5448 * @param cls closure
5449 * @param key current key code
5450 * @param value value in the hash map
5451 * @return GNUNET_YES if we should continue to iterate,
5455 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5457 struct MeshPeerInfo *p = value;
5458 struct MeshPeerQueue *q;
5459 struct MeshPeerQueue *n;
5467 queue_destroy(q, GNUNET_YES);
5471 peer_info_destroy (p);
5477 * Task run during shutdown.
5483 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5487 if (core_handle != NULL)
5489 GNUNET_CORE_disconnect (core_handle);
5494 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
5497 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5498 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5499 if (dht_handle != NULL)
5501 GNUNET_DHT_disconnect (dht_handle);
5506 GNUNET_SERVER_notification_context_destroy (nc);
5509 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5511 GNUNET_SCHEDULER_cancel (announce_id_task);
5512 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5519 * Callback for hostkey read/generation.
5521 * @param cls Closure (Configuration handle).
5522 * @param pk The ECC private key.
5523 * @param emsg Error message, if any.
5526 key_generation_cb (void *cls,
5527 struct GNUNET_CRYPTO_EccPrivateKey *pk,
5530 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5531 struct MeshPeerInfo *peer;
5532 struct MeshPeerPath *p;
5537 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5538 _("Mesh service could not access hostkey: %s. Exiting.\n"),
5540 GNUNET_SCHEDULER_shutdown ();
5543 my_private_key = pk;
5544 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5545 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5546 &my_full_id.hashPubKey);
5547 myid = GNUNET_PEER_intern (&my_full_id);
5548 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5549 "Mesh for peer [%s] starting\n",
5550 GNUNET_i2s(&my_full_id));
5552 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5553 NULL, /* Closure passed to MESH functions */
5554 &core_init, /* Call core_init once connected */
5555 &core_connect, /* Handle connects */
5556 &core_disconnect, /* remove peers on disconnects */
5557 NULL, /* Don't notify about all incoming messages */
5558 GNUNET_NO, /* For header only in notification */
5559 NULL, /* Don't notify about all outbound messages */
5560 GNUNET_NO, /* For header-only out notification */
5561 core_handlers); /* Register these handlers */
5563 if (core_handle == NULL)
5566 GNUNET_SCHEDULER_shutdown ();
5571 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5574 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5575 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5576 GNUNET_SERVER_disconnect_notify (server_handle,
5577 &handle_local_client_disconnect, NULL);
5580 clients_head = NULL;
5581 clients_tail = NULL;
5584 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5586 /* Create a peer_info for the local peer */
5587 peer = peer_get (&my_full_id);
5590 GNUNET_PEER_change_rc (myid, 1);
5591 peer_info_add_path (peer, p, GNUNET_YES);
5592 GNUNET_SERVER_resume (server_handle);
5593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5598 * Process mesh requests.
5600 * @param cls closure
5601 * @param server the initialized server
5602 * @param c configuration to use
5605 run (void *cls, struct GNUNET_SERVER_Handle *server,
5606 const struct GNUNET_CONFIGURATION_Handle *c)
5610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5611 server_handle = server;
5614 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5619 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5620 "mesh", "peer/privatekey");
5621 GNUNET_SCHEDULER_shutdown ();
5626 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5627 &refresh_path_time))
5629 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5631 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5632 "mesh", "refresh path time");
5633 GNUNET_SCHEDULER_shutdown ();
5638 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5643 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5644 "mesh", "id announce time");
5645 GNUNET_SCHEDULER_shutdown ();
5650 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5653 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5655 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5656 "mesh", "connect timeout");
5657 GNUNET_SCHEDULER_shutdown ();
5662 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5667 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5668 "mesh", "max msgs queue");
5669 GNUNET_SCHEDULER_shutdown ();
5674 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5677 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5679 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5680 "mesh", "max tunnels");
5681 GNUNET_SCHEDULER_shutdown ();
5686 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5689 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5691 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5692 "mesh", "default ttl", 64);
5697 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5700 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5701 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5702 "mesh", "max peers", 1000);
5707 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5708 &dht_replication_level))
5710 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5712 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5713 "mesh", "dht replication level", 3);
5714 dht_replication_level = 3;
5717 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5718 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5719 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5720 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5722 dht_handle = GNUNET_DHT_connect (c, 64);
5723 if (NULL == dht_handle)
5727 stats = GNUNET_STATISTICS_create ("mesh", c);
5729 GNUNET_SERVER_suspend (server_handle);
5730 /* Scheduled the task to clean up when shutdown is called */
5731 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5733 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
5736 GNUNET_free (keyfile);
5741 * The main function for the mesh service.
5743 * @param argc number of arguments from the command line
5744 * @param argv command line arguments
5745 * @return 0 ok, 1 on error
5748 main (int argc, char *const *argv)
5753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5754 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5756 ret = (GNUNET_OK == r) ? 0 : 1;
5757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5761 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5762 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5763 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);