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 (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
3078 GNUNET_SCHEDULER_cancel (t->maintenance_task);
3079 t->maintenance_task =
3080 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3081 (refresh_path_time, 4), &tunnel_timeout, t);
3085 /******************************************************************************/
3086 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3087 /******************************************************************************/
3090 * Function to send a create path packet to a peer.
3092 * @param cls closure
3093 * @param size number of bytes available in buf
3094 * @param buf where the callee should write the message
3095 * @return number of bytes written to buf
3098 send_core_path_create (void *cls, size_t size, void *buf)
3100 struct MeshTunnel *t = cls;
3101 struct GNUNET_MESH_ManipulatePath *msg;
3102 struct GNUNET_PeerIdentity *peer_ptr;
3103 struct MeshPeerPath *p = t->path;
3108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3110 sizeof (struct GNUNET_MESH_ManipulatePath) +
3111 p->length * sizeof (struct GNUNET_PeerIdentity);
3113 if (size < size_needed || NULL == buf)
3118 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3119 msg->header.size = htons (size_needed);
3120 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3121 msg->tid = ntohl (t->id.tid);
3124 if (GNUNET_YES == t->nobuffer)
3125 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3126 msg->opt = htonl(opt);
3129 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3130 for (i = 0; i < p->length; i++)
3132 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3136 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3142 * Creates a path ack message in buf and frees all unused resources.
3144 * @param cls closure (MeshTransmissionDescriptor)
3145 * @param size number of bytes available in buf
3146 * @param buf where the callee should write the message
3147 * @return number of bytes written to buf
3150 send_core_path_ack (void *cls, size_t size, void *buf)
3152 struct MeshTransmissionDescriptor *info = cls;
3153 struct GNUNET_MESH_PathACK *msg = buf;
3155 GNUNET_assert (NULL != info);
3156 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3161 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3162 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3163 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
3164 msg->tid = htonl (info->origin->tid);
3165 msg->peer_id = my_full_id;
3168 /* TODO add signature */
3170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3171 return sizeof (struct GNUNET_MESH_PathACK);
3176 * Free a transmission that was already queued with all resources
3177 * associated to the request.
3179 * @param queue Queue handler to cancel.
3180 * @param clear_cls Is it necessary to free associated cls?
3183 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3185 struct MeshTransmissionDescriptor *dd;
3186 // unsigned int max;
3188 if (GNUNET_YES == clear_cls)
3190 switch (queue->type)
3192 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3193 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3194 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3196 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3197 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3198 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3199 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3200 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3202 " prebuilt message\n");
3203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3205 GNUNET_MESH_DEBUG_M2S(queue->type));
3207 GNUNET_free (dd->data);
3209 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3214 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3215 " type %s unknown!\n",
3216 GNUNET_MESH_DEBUG_M2S(queue->type));
3218 GNUNET_free_non_null (queue->cls);
3220 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3221 queue->peer->queue_tail,
3224 /* Delete from child_fc in the appropiate tunnel */
3225 // max = queue->tunnel->fwd_queue_max;
3226 // GNUNET_PEER_resolve (queue->peer->id, &id);
3227 // if (NULL != cinfo)
3229 // for (i = 0; i < cinfo->send_buffer_n; i++)
3231 // i2 = (cinfo->send_buffer_start + i) % max;
3232 // if (cinfo->send_buffer[i2] == queue)
3234 // /* Found corresponding entry in the send_buffer. Move all others back. */
3238 // for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
3240 // j2 = (cinfo->send_buffer_start + j) % max;
3241 // j3 = (cinfo->send_buffer_start + j + 1) % max;
3242 // cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
3245 // cinfo->send_buffer[j3] = NULL;
3246 // cinfo->send_buffer_n--;
3251 GNUNET_free (queue);
3256 * @brief Get the next transmittable message from the queue.
3258 * This will be the head, except in the case of being a data packet
3259 * not allowed by the destination peer.
3261 * @param peer Destination peer.
3263 * @return The next viable MeshPeerQueue element to send to that peer.
3264 * NULL when there are no transmittable messages.
3266 struct MeshPeerQueue *
3267 queue_get_next (const struct MeshPeerInfo *peer)
3269 struct MeshPeerQueue *q;
3271 struct MeshTransmissionDescriptor *info;
3272 // struct GNUNET_MESH_Unicast *ucast;
3273 // struct GNUNET_MESH_ToOrigin *to_orig;
3274 struct GNUNET_PeerIdentity id;
3276 // uint32_t ack; FIXME
3278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
3279 for (q = peer->queue_head; NULL != q; q = q->next)
3283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3285 GNUNET_MESH_DEBUG_M2S(q->type));
3288 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3289 // ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
3290 // pid = ntohl (ucast->pid);
3291 GNUNET_PEER_resolve (info->peer->id, &id);
3292 // ack = cinfo->fwd_ack;
3294 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3295 // to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
3296 // pid = ntohl (to_orig->pid);
3297 // ack = t->bck_ack;
3300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3304 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3305 // "********* ACK: %u, PID: %u\n",
3307 // if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
3309 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3310 // "********* OK!\n");
3315 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3316 // "********* NEXT!\n");
3319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3320 "********* nothing found\n");
3326 queue_send (void *cls, size_t size, void *buf)
3328 struct MeshPeerInfo *peer = cls;
3329 struct GNUNET_MessageHeader *msg;
3330 struct MeshPeerQueue *queue;
3331 struct MeshTunnel *t;
3332 struct GNUNET_PeerIdentity dst_id;
3333 struct MeshFlowControl *fc;
3336 peer->core_transmit = NULL;
3338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
3339 queue = queue_get_next (peer);
3341 /* Queue has no internal mesh traffic nor sendable payload */
3344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
3345 if (NULL == peer->queue_head)
3346 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
3351 GNUNET_PEER_resolve (peer->id, &dst_id);
3352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3353 "********* towards %s\n",
3354 GNUNET_i2s(&dst_id));
3355 /* Check if buffer size is enough for the message */
3356 if (queue->size > size)
3358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3359 "********* not enough room, reissue\n");
3360 peer->core_transmit =
3361 GNUNET_CORE_notify_transmit_ready (core_handle,
3364 GNUNET_TIME_UNIT_FOREVER_REL,
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
3374 GNUNET_assert (0 < t->pending_messages);
3375 t->pending_messages--;
3376 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
3378 t->next_fc.queue_n--;
3379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3380 "********* unicast: t->q (%u/%u)\n",
3381 t->next_fc.queue_n, t->queue_max);
3383 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
3385 t->prev_fc.queue_n--;
3386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3387 "********* to orig: t->q (%u/%u)\n",
3388 t->prev_fc.queue_n, t->queue_max);
3392 switch (queue->type)
3395 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3396 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3397 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3398 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3399 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3400 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3402 "********* raw: %s\n",
3403 GNUNET_MESH_DEBUG_M2S (queue->type));
3405 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3406 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3407 data_size = send_core_data_raw (queue->cls, size, buf);
3408 msg = (struct GNUNET_MessageHeader *) buf;
3409 switch (ntohs (msg->type)) // Type of preconstructed message
3411 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3412 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3414 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3415 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3421 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
3423 data_size = send_core_path_create (queue->cls, size, buf);
3425 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
3427 data_size = send_core_path_ack (queue->cls, size, buf);
3431 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3432 "********* type unknown: %u\n",
3436 switch (queue->type)
3438 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3439 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3440 // if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
3442 // GNUNET_break (0);
3443 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3444 // "at pos %u (%p) != %p\n",
3445 // cinfo->send_buffer_start,
3446 // cinfo->send_buffer[cinfo->send_buffer_start],
3449 // if (cinfo->send_buffer_n > 0)
3451 // cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
3452 // cinfo->send_buffer_n--;
3453 // cinfo->send_buffer_start++;
3454 // cinfo->send_buffer_start %= t->fwd_queue_max;
3458 // GNUNET_break (0);
3465 /* Free queue, but cls was freed by send_core_* */
3466 queue_destroy (queue, GNUNET_NO);
3468 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
3474 /* If more data in queue, send next */
3475 queue = queue_get_next (peer);
3478 struct GNUNET_PeerIdentity id;
3480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
3481 GNUNET_PEER_resolve (peer->id, &id);
3482 peer->core_transmit =
3483 GNUNET_CORE_notify_transmit_ready(core_handle,
3486 GNUNET_TIME_UNIT_FOREVER_REL,
3494 if (NULL != peer->queue_head)
3496 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3497 "********* %s stalled\n",
3498 GNUNET_i2s(&my_full_id));
3499 if (peer->id == t->next_hop)
3503 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3506 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
3517 * @brief Queue and pass message to core when possible.
3519 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3520 * accounts for it. In case the queue is full, the message is dropped and
3523 * Otherwise, message is treated as internal and allowed to go regardless of
3526 * @param cls Closure (@c type dependant). It will be used by queue_send to
3527 * build the message to be sent if not already prebuilt.
3528 * @param type Type of the message, 0 for a raw message.
3529 * @param size Size of the message.
3530 * @param dst Neighbor to send message to.
3531 * @param t Tunnel this message belongs to.
3534 queue_add (void *cls, uint16_t type, size_t size,
3535 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3537 struct MeshPeerQueue *queue;
3538 struct GNUNET_PeerIdentity id;
3542 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3544 n = &t->next_fc.queue_n;
3546 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3548 n = &t->prev_fc.queue_n;
3552 if (*n >= t->queue_max)
3555 GNUNET_STATISTICS_update(stats,
3556 "# messages dropped (buffer full)",
3558 return; // Drop message
3562 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3568 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3569 if (NULL == dst->core_transmit)
3571 GNUNET_PEER_resolve (dst->id, &id);
3572 dst->core_transmit =
3573 GNUNET_CORE_notify_transmit_ready (core_handle,
3576 GNUNET_TIME_UNIT_FOREVER_REL,
3582 t->pending_messages++;
3586 /******************************************************************************/
3587 /******************** MESH NETWORK HANDLERS **************************/
3588 /******************************************************************************/
3592 * Core handler for path creation
3594 * @param cls closure
3595 * @param message message
3596 * @param peer peer identity this notification is about
3598 * @return GNUNET_OK to keep the connection open,
3599 * GNUNET_SYSERR to close it (signal serious error)
3602 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3603 const struct GNUNET_MessageHeader *message)
3605 unsigned int own_pos;
3608 MESH_TunnelNumber tid;
3609 struct GNUNET_MESH_ManipulatePath *msg;
3610 struct GNUNET_PeerIdentity *pi;
3611 struct GNUNET_HashCode hash;
3612 struct MeshPeerPath *path;
3613 struct MeshPeerInfo *dest_peer_info;
3614 struct MeshPeerInfo *orig_peer_info;
3615 struct MeshTunnel *t;
3617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3618 "Received a path create msg [%s]\n",
3619 GNUNET_i2s (&my_full_id));
3620 size = ntohs (message->size);
3621 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3623 GNUNET_break_op (0);
3627 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3628 if (size % sizeof (struct GNUNET_PeerIdentity))
3630 GNUNET_break_op (0);
3633 size /= sizeof (struct GNUNET_PeerIdentity);
3636 GNUNET_break_op (0);
3639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3640 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3642 tid = ntohl (msg->tid);
3643 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3645 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
3646 t = tunnel_get (pi, tid);
3647 if (NULL == t) // FIXME only for INCOMING tunnels?
3651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3652 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3655 // FIXME notify failure
3658 opt = ntohl (msg->opt);
3659 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
3661 t->nobuffer = GNUNET_YES;
3662 t->prev_fc.last_ack_sent = t->prev_fc.last_pid_recv + 1;
3664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3666 if (GNUNET_YES == t->nobuffer)
3671 // FIXME only assign a local tid if a local client is interested (on demand)
3672 while (NULL != tunnel_get_incoming (next_local_tid))
3673 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3674 t->local_tid_dest = next_local_tid++;
3675 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3678 tunnel_reset_timeout (t);
3679 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3681 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
3682 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3690 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3691 if (NULL == dest_peer_info)
3693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3694 " Creating PeerInfo for destination.\n");
3695 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3696 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3697 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3699 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3701 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3702 if (NULL == orig_peer_info)
3704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3705 " Creating PeerInfo for origin.\n");
3706 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3707 orig_peer_info->id = GNUNET_PEER_intern (pi);
3708 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3709 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3712 path = path_new (size);
3714 for (i = 0; i < size; i++)
3716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3717 GNUNET_i2s (&pi[i]));
3718 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3719 if (path->peers[i] == myid)
3722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3725 /* cannot be self, must be 'not found' */
3726 /* create path: self not found in path through self */
3727 GNUNET_break_op (0);
3728 path_destroy (path);
3732 path_add_to_peers (path, GNUNET_NO);
3733 t->prev_hop = path->peers[own_pos - 1];
3734 GNUNET_PEER_change_rc (t->prev_hop, 1);
3735 if (own_pos == size - 1)
3737 /* It is for us! Send ack. */
3738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3739 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3745 struct MeshPeerPath *path2;
3747 t->next_hop = path->peers[own_pos + 1];
3748 GNUNET_PEER_change_rc(t->next_hop, 1);
3750 /* It's for somebody else! Retransmit. */
3751 path2 = path_duplicate (path);
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3753 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3754 path2 = path_duplicate (path);
3755 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3756 send_create_path (dest_peer_info, path, t);
3763 * Core handler for path destruction
3765 * @param cls closure
3766 * @param message message
3767 * @param peer peer identity this notification is about
3769 * @return GNUNET_OK to keep the connection open,
3770 * GNUNET_SYSERR to close it (signal serious error)
3773 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3774 const struct GNUNET_MessageHeader *message)
3776 struct GNUNET_MESH_ManipulatePath *msg;
3777 struct GNUNET_PeerIdentity *pi;
3778 struct MeshPeerPath *path;
3779 struct MeshTunnel *t;
3780 unsigned int own_pos;
3784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3785 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3786 size = ntohs (message->size);
3787 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3789 GNUNET_break_op (0);
3793 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3794 if (size % sizeof (struct GNUNET_PeerIdentity))
3796 GNUNET_break_op (0);
3799 size /= sizeof (struct GNUNET_PeerIdentity);
3802 GNUNET_break_op (0);
3805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3807 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3808 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3810 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3812 t = tunnel_get (pi, ntohl (msg->tid));
3815 /* TODO notify back: we don't know this tunnel */
3816 GNUNET_break_op (0);
3819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3820 path = path_new (size);
3822 for (i = 0; i < size; i++)
3824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3825 GNUNET_i2s (&pi[i]));
3826 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3827 if (path->peers[i] == myid)
3830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3831 if (own_pos < path->length - 1)
3832 send_prebuilt_message (message, path->peers[own_pos + 1], t);
3834 send_client_tunnel_disconnect(t, NULL);
3836 // tunnel_delete_peer (t, path->peers[path->length - 1]); FIXME
3837 path_destroy (path);
3843 * Core handler for notifications of broken paths
3845 * @param cls closure
3846 * @param message message
3847 * @param peer peer identity this notification is about
3849 * @return GNUNET_OK to keep the connection open,
3850 * GNUNET_SYSERR to close it (signal serious error)
3853 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3854 const struct GNUNET_MessageHeader *message)
3856 struct GNUNET_MESH_PathBroken *msg;
3857 struct MeshTunnel *t;
3859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3860 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3861 msg = (struct GNUNET_MESH_PathBroken *) message;
3862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3863 GNUNET_i2s (&msg->peer1));
3864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3865 GNUNET_i2s (&msg->peer2));
3866 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3869 GNUNET_break_op (0);
3872 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3873 GNUNET_PEER_search (&msg->peer2));
3880 * Core handler for tunnel destruction
3882 * @param cls closure
3883 * @param message message
3884 * @param peer peer identity this notification is about
3886 * @return GNUNET_OK to keep the connection open,
3887 * GNUNET_SYSERR to close it (signal serious error)
3890 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3891 const struct GNUNET_MessageHeader *message)
3893 struct GNUNET_MESH_TunnelDestroy *msg;
3894 struct MeshTunnel *t;
3897 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3899 "Got a TUNNEL DESTROY packet from %s\n",
3901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3902 " for tunnel %s [%u]\n",
3903 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3904 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3905 /* Check signature */
3908 /* Probably already got the message from another path,
3909 * destroyed the tunnel and retransmitted to children.
3912 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3916 pid = GNUNET_PEER_search (peer);
3917 if (pid != t->prev_hop && 0 < t->nclients)
3919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3920 "still in use by %u clients\n",
3924 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3926 /* Tunnel was incoming, notify clients */
3927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3928 t->local_tid, t->local_tid_dest);
3929 send_clients_tunnel_destroy (t);
3931 tunnel_send_destroy (t);
3932 t->destroy = GNUNET_YES;
3933 // TODO: add timeout to destroy the tunnel anyway
3939 * Core handler for mesh network traffic going from the origin to a peer
3941 * @param cls closure
3942 * @param peer peer identity this notification is about
3943 * @param message message
3944 * @return GNUNET_OK to keep the connection open,
3945 * GNUNET_SYSERR to close it (signal serious error)
3948 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3949 const struct GNUNET_MessageHeader *message)
3951 struct GNUNET_MESH_Unicast *msg;
3952 struct MeshTunnel *t;
3957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3960 size = ntohs (message->size);
3962 sizeof (struct GNUNET_MESH_Unicast) +
3963 sizeof (struct GNUNET_MessageHeader))
3968 msg = (struct GNUNET_MESH_Unicast *) message;
3969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3970 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3972 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3975 /* TODO notify back: we don't know this tunnel */
3976 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3977 GNUNET_break_op (0);
3980 pid = ntohl (msg->pid);
3981 if (t->prev_fc.last_pid_recv == pid)
3983 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3984 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3985 " Already seen pid %u, DROPPING!\n", pid);
3990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3991 " pid %u not seen yet, forwarding\n", pid);
3994 t->prev_fc.last_pid_recv = pid;
3996 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3998 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3999 GNUNET_break_op (0);
4000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4001 "Received PID %u, ACK %u\n",
4002 pid, t->prev_fc.last_ack_sent);
4006 tunnel_reset_timeout (t);
4007 if (t->dest == myid)
4009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4010 " it's for us! sending to clients...\n");
4011 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4012 send_subscribed_clients (message, &msg[1].header, t);
4013 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4016 ttl = ntohl (msg->ttl);
4017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4020 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4021 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4022 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4026 " not for us, retransmitting...\n");
4028 if (GNUNET_YES == t->nobuffer &&
4029 GNUNET_YES == GMC_is_pid_bigger (pid, t->next_fc.last_ack_recv))
4031 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4032 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n",
4033 pid, t->next_fc.last_ack_recv);
4034 GNUNET_break_op (0);
4037 send_prebuilt_message (message, t->next_hop, t);
4038 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4044 * Core handler for mesh network traffic toward the owner of a tunnel
4046 * @param cls closure
4047 * @param message message
4048 * @param peer peer identity this notification is about
4050 * @return GNUNET_OK to keep the connection open,
4051 * GNUNET_SYSERR to close it (signal serious error)
4054 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4055 const struct GNUNET_MessageHeader *message)
4057 struct GNUNET_MESH_ToOrigin *msg;
4058 struct MeshPeerInfo *peer_info;
4059 struct MeshTunnel *t;
4063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4065 size = ntohs (message->size);
4066 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4067 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4069 GNUNET_break_op (0);
4072 msg = (struct GNUNET_MESH_ToOrigin *) message;
4073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4074 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4075 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4076 pid = ntohl (msg->pid);
4080 /* TODO notify that we dont know this tunnel (whom)? */
4081 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4083 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
4084 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4089 if (t->next_fc.last_pid_recv == pid)
4091 /* already seen this packet, drop */
4092 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
4093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4094 " Already seen pid %u, DROPPING!\n", pid);
4095 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4100 " pid %u not seen yet, forwarding\n", pid);
4101 t->next_fc.last_pid_recv = pid;
4103 if (NULL != t->owner)
4106 struct GNUNET_MESH_ToOrigin *copy;
4108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4109 " it's for us! sending to clients...\n");
4110 /* TODO signature verification */
4111 memcpy (cbuf, message, size);
4112 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
4113 copy->tid = htonl (t->local_tid);
4114 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4115 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
4116 ©->header, GNUNET_NO);
4117 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4121 " not for us, retransmitting...\n");
4123 peer_info = peer_get (&msg->oid);
4124 if (NULL == peer_info)
4126 /* unknown origin of tunnel */
4130 if (0 == t->prev_hop) /* No owner AND no prev hop */
4132 if (GNUNET_YES == t->destroy)
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4135 "to orig received on a dying tunnel %s [%X]\n",
4136 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4140 "unknown to origin at %s\n",
4141 GNUNET_i2s (&my_full_id));
4142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4146 "for tunnel %s [%X]\n",
4147 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4150 send_prebuilt_message (message, t->prev_hop, t);
4151 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4158 * Core handler for mesh network traffic point-to-point acks.
4160 * @param cls closure
4161 * @param message message
4162 * @param peer peer identity this notification is about
4164 * @return GNUNET_OK to keep the connection open,
4165 * GNUNET_SYSERR to close it (signal serious error)
4168 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4169 const struct GNUNET_MessageHeader *message)
4171 struct GNUNET_MESH_ACK *msg;
4172 struct MeshTunnel *t;
4175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4177 msg = (struct GNUNET_MESH_ACK *) message;
4179 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4183 /* TODO notify that we dont know this tunnel (whom)? */
4184 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4187 ack = ntohl (msg->pid);
4188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4190 /* Is this a forward or backward ACK? */
4191 if (t->prev_hop != GNUNET_PEER_search (peer))
4194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4195 t->next_fc.last_ack_recv = ack;
4196 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4197 peer_unlock_queue (t->next_hop);
4198 // if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll) FIXME
4200 // GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
4201 // cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
4202 // cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
4207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4208 t->prev_fc.last_ack_recv = ack;
4209 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4210 peer_unlock_queue (t->prev_hop);
4217 * Core handler for mesh network traffic point-to-point ack polls.
4219 * @param cls closure
4220 * @param message message
4221 * @param peer peer identity this notification is about
4223 * @return GNUNET_OK to keep the connection open,
4224 * GNUNET_SYSERR to close it (signal serious error)
4227 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4228 const struct GNUNET_MessageHeader *message)
4230 struct GNUNET_MESH_Poll *msg;
4231 struct MeshTunnel *t;
4233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4236 msg = (struct GNUNET_MESH_Poll *) message;
4238 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4242 /* TODO notify that we dont know this tunnel (whom)? */
4243 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4244 GNUNET_break_op (0);
4248 /* Is this a forward or backward ACK? */
4249 if (t->prev_hop != GNUNET_PEER_search(peer))
4251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4252 /* FIXME cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send */
4253 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4258 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4265 * Core handler for path ACKs
4267 * @param cls closure
4268 * @param message message
4269 * @param peer peer identity this notification is about
4271 * @return GNUNET_OK to keep the connection open,
4272 * GNUNET_SYSERR to close it (signal serious error)
4275 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4276 const struct GNUNET_MessageHeader *message)
4278 struct GNUNET_MESH_PathACK *msg;
4279 struct MeshPeerInfo *peer_info;
4280 struct MeshPeerPath *p;
4281 struct MeshTunnel *t;
4283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
4284 GNUNET_i2s (&my_full_id));
4285 msg = (struct GNUNET_MESH_PathACK *) message;
4286 t = tunnel_get (&msg->oid, ntohl(msg->tid));
4289 /* TODO notify that we don't know the tunnel */
4290 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
4292 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
4296 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4298 peer_info = peer_get (&msg->peer_id);
4299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
4300 GNUNET_i2s (&msg->peer_id));
4301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
4304 /* Add path to peers? */
4308 path_add_to_peers (p, GNUNET_YES);
4315 /* Message for us? */
4316 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
4318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4319 if (NULL == t->owner)
4321 GNUNET_break_op (0);
4324 if (NULL != peer_info->dhtget)
4326 GNUNET_DHT_get_stop (peer_info->dhtget);
4327 peer_info->dhtget = NULL;
4332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4333 " not for us, retransmitting...\n");
4334 peer_info = peer_get (&msg->oid);
4335 send_prebuilt_message (message, t->prev_hop, t);
4341 * Core handler for mesh keepalives.
4343 * @param cls closure
4344 * @param message message
4345 * @param peer peer identity this notification is about
4346 * @return GNUNET_OK to keep the connection open,
4347 * GNUNET_SYSERR to close it (signal serious error)
4349 * TODO: Check who we got this from, to validate route.
4352 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4353 const struct GNUNET_MessageHeader *message)
4355 struct GNUNET_MESH_TunnelKeepAlive *msg;
4356 struct MeshTunnel *t;
4358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4361 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4362 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4366 /* TODO notify that we dont know that tunnel */
4367 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4372 tunnel_reset_timeout (t);
4374 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4375 send_prebuilt_message (message, t->next_hop, t);
4382 * Functions to handle messages from core
4384 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4385 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4386 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
4387 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4388 sizeof (struct GNUNET_MESH_PathBroken)},
4389 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4390 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4391 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4392 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4393 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4394 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4395 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4396 sizeof (struct GNUNET_MESH_ACK)},
4397 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4398 sizeof (struct GNUNET_MESH_Poll)},
4399 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4400 sizeof (struct GNUNET_MESH_PathACK)},
4406 /******************************************************************************/
4407 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4408 /******************************************************************************/
4413 * notify_client_connection_failure: notify a client that the connection to the
4414 * requested remote peer is not possible (for instance, no route found)
4415 * Function called when the socket is ready to queue more data. "buf" will be
4416 * NULL and "size" zero if the socket was closed for writing in the meantime.
4418 * @param cls closure
4419 * @param size number of bytes available in buf
4420 * @param buf where the callee should write the message
4421 * @return number of bytes written to buf
4424 notify_client_connection_failure (void *cls, size_t size, void *buf)
4427 struct MeshPeerInfo *peer_info;
4428 struct GNUNET_MESH_PeerControl *msg;
4429 struct GNUNET_PeerIdentity id;
4431 if (0 == size && NULL == buf)
4433 // TODO retry? cancel?
4437 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4438 peer_info = (struct MeshPeerInfo *) cls;
4439 msg = (struct GNUNET_MESH_PeerControl *) buf;
4440 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4441 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4442 // msg->tunnel_id = htonl(peer_info->t->tid);
4443 GNUNET_PEER_resolve (peer_info->id, &id);
4444 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4452 * Send keepalive packets for a tunnel.
4454 * @param cls Closure (tunnel for which to send the keepalive).
4455 * @param tc Notification context.
4458 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4460 struct MeshTunnel *t = cls;
4461 struct GNUNET_MESH_TunnelKeepAlive *msg;
4462 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4465 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4466 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4472 "sending keepalive for tunnel %d\n", t->id.tid);
4474 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4475 msg->header.size = htons (size);
4476 // FIXME change to tunnel keepalive
4477 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4478 msg->oid = my_full_id;
4479 msg->tid = htonl (t->id.tid);
4480 send_prebuilt_message (&msg->header, t->next_hop, t);
4482 t->maintenance_task =
4483 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4484 tunnel_reset_timeout (t);
4489 * Function to process paths received for a new peer addition. The recorded
4490 * paths form the initial tunnel, which can be optimized later.
4491 * Called on each result obtained for the DHT search.
4493 * @param cls closure
4494 * @param exp when will this value expire
4495 * @param key key of the result
4496 * @param get_path path of the get request
4497 * @param get_path_length lenght of get_path
4498 * @param put_path path of the put request
4499 * @param put_path_length length of the put_path
4500 * @param type type of the result
4501 * @param size number of bytes in data
4502 * @param data pointer to the result data
4504 * TODO: re-issue the request after certain time? cancel after X results?
4507 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4508 const struct GNUNET_HashCode * key,
4509 const struct GNUNET_PeerIdentity *get_path,
4510 unsigned int get_path_length,
4511 const struct GNUNET_PeerIdentity *put_path,
4512 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4513 size_t size, const void *data)
4515 struct MeshPeerInfo *peer = cls;
4516 struct MeshPeerPath *p;
4517 struct GNUNET_PeerIdentity pi;
4520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4521 GNUNET_PEER_resolve (peer->id, &pi);
4522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4524 p = path_build_from_dht (get_path, get_path_length,
4525 put_path, put_path_length);
4526 path_add_to_peers (p, GNUNET_NO);
4528 for (i = 0; i < peer->ntunnels; i++)
4530 peer_connect (peer, peer->tunnels[i]); // FIXME add if
4537 /******************************************************************************/
4538 /********************* MESH LOCAL HANDLES **************************/
4539 /******************************************************************************/
4543 * Handler for client disconnection
4545 * @param cls closure
4546 * @param client identification of the client; NULL
4547 * for the last call when the server is destroyed
4550 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4552 struct MeshClient *c;
4553 struct MeshClient *next;
4555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
4558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4565 if (c->handle != client)
4567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
4571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4573 GNUNET_SERVER_client_drop (c->handle);
4574 c->shutting_down = GNUNET_YES;
4575 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4576 &tunnel_destroy_iterator, c);
4577 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4578 &tunnel_destroy_iterator, c);
4579 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
4580 &tunnel_destroy_iterator, c);
4581 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4582 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4583 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
4585 if (NULL != c->types)
4586 GNUNET_CONTAINER_multihashmap_destroy (c->types);
4588 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4591 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4600 * Handler for new clients
4602 * @param cls closure
4603 * @param client identification of the client
4604 * @param message the actual message, which includes messages the client wants
4607 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4608 const struct GNUNET_MessageHeader *message)
4610 struct GNUNET_MESH_ClientConnect *cc_msg;
4611 struct MeshClient *c;
4617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
4619 /* Check data sanity */
4620 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4621 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4622 ntypes = ntohs (cc_msg->types);
4623 if (size != ntypes * sizeof (uint16_t))
4626 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4630 /* Create new client structure */
4631 c = GNUNET_malloc (sizeof (struct MeshClient));
4632 c->id = next_client_id++; // overflow not important: just for debug
4633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
4634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u types\n", ntypes);
4636 GNUNET_SERVER_client_keep (client);
4640 struct GNUNET_HashCode hc;
4642 t = (uint16_t *) &cc_msg[1];
4643 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
4644 for (i = 0; i < ntypes; i++)
4647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
4648 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc); // FIXME pseudo hash
4650 /* store in clients hashmap */
4651 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
4652 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4653 /* store in global hashmap */
4654 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
4655 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4659 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4660 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4661 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4662 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4663 GNUNET_SERVER_notification_context_add (nc, client);
4664 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4666 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4672 * Handler for requests of new tunnels
4674 * @param cls Closure.
4675 * @param client Identification of the client.
4676 * @param message The actual message.
4679 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4680 const struct GNUNET_MessageHeader *message)
4682 struct GNUNET_MESH_TunnelMessage *t_msg;
4683 struct MeshPeerInfo *peer_info;
4684 struct MeshTunnel *t;
4685 struct MeshClient *c;
4686 MESH_TunnelNumber tid;
4688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4690 /* Sanity check for client registration */
4691 if (NULL == (c = client_get (client)))
4694 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4699 /* Message sanity check */
4700 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4703 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4707 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4709 GNUNET_i2s (&t_msg->peer));
4710 /* Sanity check for tunnel numbering */
4711 tid = ntohl (t_msg->tunnel_id);
4712 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4715 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4718 /* Sanity check for duplicate tunnel IDs */
4719 if (NULL != tunnel_get_by_local_id (c, tid))
4722 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4726 while (NULL != tunnel_get_by_pi (myid, next_tid))
4727 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4728 t = tunnel_new (myid, next_tid, c, tid);
4729 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4733 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
4737 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4739 peer_info = peer_get (&t_msg->peer);
4740 GNUNET_array_append (peer_info->tunnels, peer_info->ntunnels, t);
4741 peer_connect (peer_info, t);
4742 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4748 * Handler for requests of deleting tunnels
4750 * @param cls closure
4751 * @param client identification of the client
4752 * @param message the actual message
4755 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4756 const struct GNUNET_MessageHeader *message)
4758 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4759 struct MeshClient *c;
4760 struct MeshTunnel *t;
4761 MESH_TunnelNumber tid;
4763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4764 "Got a DESTROY TUNNEL from client!\n");
4766 /* Sanity check for client registration */
4767 if (NULL == (c = client_get (client)))
4770 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4775 /* Message sanity check */
4776 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4779 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4783 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4785 /* Retrieve tunnel */
4786 tid = ntohl (tunnel_msg->tunnel_id);
4787 t = tunnel_get_by_local_id(c, tid);
4790 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4792 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4795 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4797 client_ignore_tunnel (c, t);
4798 tunnel_destroy_empty (t);
4799 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4802 send_client_tunnel_disconnect (t, c);
4803 client_delete_tunnel (c, t);
4805 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4807 tunnel_send_destroy (t);
4808 peer_remove_tunnel (peer_get_short(t->dest), t);
4809 t->destroy = GNUNET_YES;
4810 /* The tunnel will be destroyed when the last message is transmitted. */
4811 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4817 * Handler for requests of seeting tunnel's buffering policy.
4819 * @param cls Closure (unused).
4820 * @param client Identification of the client.
4821 * @param message The actual message.
4824 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
4825 const struct GNUNET_MessageHeader *message)
4827 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4828 struct MeshClient *c;
4829 struct MeshTunnel *t;
4830 MESH_TunnelNumber tid;
4832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4833 "Got a BUFFER request from client!\n");
4835 /* Sanity check for client registration */
4836 if (NULL == (c = client_get (client)))
4839 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4844 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4846 /* Retrieve tunnel */
4847 tid = ntohl (tunnel_msg->tunnel_id);
4848 t = tunnel_get_by_local_id(c, tid);
4851 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4853 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4857 switch (ntohs(message->type))
4859 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
4860 t->nobuffer = GNUNET_NO;
4862 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
4863 t->nobuffer = GNUNET_YES;
4869 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4874 * Handler for client traffic directed to one peer
4876 * @param cls closure
4877 * @param client identification of the client
4878 * @param message the actual message
4881 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4882 const struct GNUNET_MessageHeader *message)
4884 struct MeshClient *c;
4885 struct MeshTunnel *t;
4886 struct GNUNET_MESH_Unicast *data_msg;
4887 MESH_TunnelNumber tid;
4890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4891 "Got a unicast request from a client!\n");
4893 /* Sanity check for client registration */
4894 if (NULL == (c = client_get (client)))
4897 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4902 data_msg = (struct GNUNET_MESH_Unicast *) message;
4904 /* Sanity check for message size */
4905 size = ntohs (message->size);
4906 if (sizeof (struct GNUNET_MESH_Unicast) +
4907 sizeof (struct GNUNET_MessageHeader) > size)
4910 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4914 /* Tunnel exists? */
4915 tid = ntohl (data_msg->tid);
4916 t = tunnel_get_by_local_id (c, tid);
4920 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4924 /* Is it a local tunnel? Then, does client own the tunnel? */
4925 if (t->owner->handle != client)
4928 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4932 /* PID should be as expected: client<->service communication */
4933 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4936 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4937 "Unicast PID, expected %u, got %u\n",
4938 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4939 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4943 /* Ok, everything is correct, send the message
4944 * (pretend we got it from a mesh peer)
4947 /* Work around const limitation */
4948 char buf[ntohs (message->size)] GNUNET_ALIGN;
4949 struct GNUNET_MESH_Unicast *copy;
4951 copy = (struct GNUNET_MESH_Unicast *) buf;
4952 memcpy (buf, data_msg, size);
4953 copy->oid = my_full_id;
4954 copy->tid = htonl (t->id.tid);
4955 copy->ttl = htonl (default_ttl);
4956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4957 " calling generic handler...\n");
4958 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
4960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4961 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4968 * Handler for client traffic directed to the origin
4970 * @param cls closure
4971 * @param client identification of the client
4972 * @param message the actual message
4975 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4976 const struct GNUNET_MessageHeader *message)
4978 struct GNUNET_MESH_ToOrigin *data_msg;
4979 struct MeshTunnelClientInfo *clinfo;
4980 struct MeshClient *c;
4981 struct MeshTunnel *t;
4982 MESH_TunnelNumber tid;
4985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4986 "Got a ToOrigin request from a client!\n");
4987 /* Sanity check for client registration */
4988 if (NULL == (c = client_get (client)))
4991 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4996 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4998 /* Sanity check for message size */
4999 size = ntohs (message->size);
5000 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5001 sizeof (struct GNUNET_MessageHeader) > size)
5004 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5008 /* Tunnel exists? */
5009 tid = ntohl (data_msg->tid);
5010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5011 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5017 t = tunnel_get_by_local_id (c, tid);
5020 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5021 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5023 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5027 /* It should be sent by someone who has this as incoming tunnel. */
5028 if (GNUNET_NO == client_knows_tunnel (c, t))
5031 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5035 /* PID should be as expected */
5036 clinfo = tunnel_get_client_fc (t, c);
5037 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
5040 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5041 "To Origin PID, expected %u, got %u\n",
5042 clinfo->bck_pid + 1,
5043 ntohl (data_msg->pid));
5044 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5049 /* Ok, everything is correct, send the message
5050 * (pretend we got it from a mesh peer)
5053 char buf[ntohs (message->size)] GNUNET_ALIGN;
5054 struct GNUNET_MESH_ToOrigin *copy;
5056 /* Work around 'const' limitation */
5057 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5058 memcpy (buf, data_msg, size);
5059 GNUNET_PEER_resolve (t->id.oid, ©->oid);
5060 copy->tid = htonl (t->id.tid);
5061 copy->ttl = htonl (default_ttl);
5062 copy->pid = htonl (t->prev_fc.last_pid_sent + 1);
5064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5065 " calling generic handler...\n");
5066 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
5068 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5075 * Handler for client's ACKs for payload traffic.
5077 * @param cls Closure (unused).
5078 * @param client Identification of the client.
5079 * @param message The actual message.
5082 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5083 const struct GNUNET_MessageHeader *message)
5085 struct GNUNET_MESH_LocalAck *msg;
5086 struct MeshTunnel *t;
5087 struct MeshClient *c;
5088 MESH_TunnelNumber tid;
5091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5092 /* Sanity check for client registration */
5093 if (NULL == (c = client_get (client)))
5096 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5101 msg = (struct GNUNET_MESH_LocalAck *) message;
5103 /* Tunnel exists? */
5104 tid = ntohl (msg->tunnel_id);
5105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5106 t = tunnel_get_by_local_id (c, tid);
5110 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5111 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5112 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5116 ack = ntohl (msg->max_pid);
5117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
5119 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5120 if (NULL != t->owner && t->owner->handle == client)
5122 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5123 t->next_fc.last_ack_sent = ack;
5124 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5128 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5129 tunnel_set_client_fwd_ack (t, c, ack);
5130 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5133 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5141 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5143 * @param cls Closure (client handle).
5144 * @param key Key (hashed tunnel ID, unused).
5145 * @param value Tunnel info.
5147 * @return GNUNET_YES, to keep iterating.
5150 monitor_all_tunnels_iterator (void *cls,
5151 const struct GNUNET_HashCode * key,
5154 struct GNUNET_SERVER_Client *client = cls;
5155 struct MeshTunnel *t = value;
5156 struct GNUNET_MESH_LocalMonitor *msg;
5158 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5159 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5160 msg->tunnel_id = htonl (t->id.tid);
5161 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5162 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5163 GNUNET_PEER_resolve (t->dest, &msg->destination);
5165 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5166 "* sending info about tunnel %s [%u]\n",
5167 GNUNET_i2s (&msg->owner), t->id.tid);
5169 GNUNET_SERVER_notification_context_unicast (nc, client,
5170 &msg->header, GNUNET_NO);
5176 * Handler for client's MONITOR request.
5178 * @param cls Closure (unused).
5179 * @param client Identification of the client.
5180 * @param message The actual message.
5183 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5184 const struct GNUNET_MessageHeader *message)
5186 struct MeshClient *c;
5188 /* Sanity check for client registration */
5189 if (NULL == (c = client_get (client)))
5192 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5196 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5197 "Received get tunnels request from client %u\n",
5199 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5200 monitor_all_tunnels_iterator,
5202 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5203 "Get tunnels request from client %u completed\n",
5205 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5210 * Handler for client's MONITOR_TUNNEL request.
5212 * @param cls Closure (unused).
5213 * @param client Identification of the client.
5214 * @param message The actual message.
5217 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5218 const struct GNUNET_MessageHeader *message)
5220 const struct GNUNET_MESH_LocalMonitor *msg;
5221 struct GNUNET_MESH_LocalMonitor *resp;
5222 struct MeshClient *c;
5223 struct MeshTunnel *t;
5225 /* Sanity check for client registration */
5226 if (NULL == (c = client_get (client)))
5229 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5233 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5235 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5238 ntohl (msg->tunnel_id));
5239 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5242 /* We don't know the tunnel FIXME */
5243 struct GNUNET_MESH_LocalMonitor warn;
5246 GNUNET_SERVER_notification_context_unicast (nc, client,
5249 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5253 /* Initialize context */
5254 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5256 GNUNET_PEER_resolve (t->dest, &resp->destination);
5257 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5258 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5259 &resp->header, GNUNET_NO);
5262 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5263 "Monitor tunnel request from client %u completed\n",
5265 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5270 * Functions to handle messages from clients
5272 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5273 {&handle_local_new_client, NULL,
5274 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5275 {&handle_local_tunnel_create, NULL,
5276 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5277 sizeof (struct GNUNET_MESH_TunnelMessage)},
5278 {&handle_local_tunnel_destroy, NULL,
5279 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5280 sizeof (struct GNUNET_MESH_TunnelMessage)},
5281 {&handle_local_tunnel_buffer, NULL,
5282 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
5283 sizeof (struct GNUNET_MESH_TunnelMessage)},
5284 {&handle_local_tunnel_buffer, NULL,
5285 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
5286 sizeof (struct GNUNET_MESH_TunnelMessage)},
5287 {&handle_local_unicast, NULL,
5288 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5289 {&handle_local_to_origin, NULL,
5290 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5291 {&handle_local_ack, NULL,
5292 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5293 sizeof (struct GNUNET_MESH_LocalAck)},
5294 {&handle_local_get_tunnels, NULL,
5295 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5296 sizeof (struct GNUNET_MessageHeader)},
5297 {&handle_local_show_tunnel, NULL,
5298 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5299 sizeof (struct GNUNET_MESH_LocalMonitor)},
5305 * To be called on core init/fail.
5307 * @param cls service closure
5308 * @param server handle to the server for this service
5309 * @param identity the public identity of this peer
5312 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5313 const struct GNUNET_PeerIdentity *identity)
5316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5317 core_handle = server;
5318 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5324 GNUNET_i2s (identity));
5325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5327 GNUNET_i2s (&my_full_id));
5328 GNUNET_SCHEDULER_shutdown (); // Try gracefully
5330 GNUNET_abort(); // Try harder
5337 * Method called whenever a given peer connects.
5339 * @param cls closure
5340 * @param peer peer identity this notification is about
5343 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5345 struct MeshPeerInfo *peer_info;
5346 struct MeshPeerPath *path;
5348 DEBUG_CONN ("Peer connected\n");
5349 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5350 peer_info = peer_get (peer);
5351 if (myid == peer_info->id)
5353 DEBUG_CONN (" (self)\n");
5358 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5360 path = path_new (2);
5361 path->peers[0] = myid;
5362 path->peers[1] = peer_info->id;
5363 GNUNET_PEER_change_rc (myid, 1);
5364 GNUNET_PEER_change_rc (peer_info->id, 1);
5365 peer_info_add_path (peer_info, path, GNUNET_YES);
5366 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5372 * Method called whenever a peer disconnects.
5374 * @param cls closure
5375 * @param peer peer identity this notification is about
5378 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5380 struct MeshPeerInfo *pi;
5381 struct MeshPeerQueue *q;
5382 struct MeshPeerQueue *n;
5384 DEBUG_CONN ("Peer disconnected\n");
5385 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5395 /* TODO try to reroute this traffic instead */
5396 queue_destroy(q, GNUNET_YES);
5399 if (NULL != pi->core_transmit)
5401 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5402 pi->core_transmit = NULL;
5404 peer_remove_path (pi, pi->id, myid);
5407 DEBUG_CONN (" (self)\n");
5409 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5414 /******************************************************************************/
5415 /************************ MAIN FUNCTIONS ****************************/
5416 /******************************************************************************/
5419 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5421 * @param cls closure
5422 * @param key current key code
5423 * @param value value in the hash map
5424 * @return GNUNET_YES if we should continue to iterate,
5428 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5430 struct MeshTunnel *t = value;
5437 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5439 * @param cls closure
5440 * @param key current key code
5441 * @param value value in the hash map
5442 * @return GNUNET_YES if we should continue to iterate,
5446 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5448 struct MeshPeerInfo *p = value;
5449 struct MeshPeerQueue *q;
5450 struct MeshPeerQueue *n;
5458 queue_destroy(q, GNUNET_YES);
5462 peer_info_destroy (p);
5468 * Task run during shutdown.
5474 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5478 if (core_handle != NULL)
5480 GNUNET_CORE_disconnect (core_handle);
5485 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
5488 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5489 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5490 if (dht_handle != NULL)
5492 GNUNET_DHT_disconnect (dht_handle);
5497 GNUNET_SERVER_notification_context_destroy (nc);
5500 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5502 GNUNET_SCHEDULER_cancel (announce_id_task);
5503 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5510 * Callback for hostkey read/generation.
5512 * @param cls Closure (Configuration handle).
5513 * @param pk The ECC private key.
5514 * @param emsg Error message, if any.
5517 key_generation_cb (void *cls,
5518 struct GNUNET_CRYPTO_EccPrivateKey *pk,
5521 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5522 struct MeshPeerInfo *peer;
5523 struct MeshPeerPath *p;
5528 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5529 _("Mesh service could not access hostkey: %s. Exiting.\n"),
5531 GNUNET_SCHEDULER_shutdown ();
5534 my_private_key = pk;
5535 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5536 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5537 &my_full_id.hashPubKey);
5538 myid = GNUNET_PEER_intern (&my_full_id);
5539 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5540 "Mesh for peer [%s] starting\n",
5541 GNUNET_i2s(&my_full_id));
5543 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5544 NULL, /* Closure passed to MESH functions */
5545 &core_init, /* Call core_init once connected */
5546 &core_connect, /* Handle connects */
5547 &core_disconnect, /* remove peers on disconnects */
5548 NULL, /* Don't notify about all incoming messages */
5549 GNUNET_NO, /* For header only in notification */
5550 NULL, /* Don't notify about all outbound messages */
5551 GNUNET_NO, /* For header-only out notification */
5552 core_handlers); /* Register these handlers */
5554 if (core_handle == NULL)
5557 GNUNET_SCHEDULER_shutdown ();
5562 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5565 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5566 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5567 GNUNET_SERVER_disconnect_notify (server_handle,
5568 &handle_local_client_disconnect, NULL);
5571 clients_head = NULL;
5572 clients_tail = NULL;
5575 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5577 /* Create a peer_info for the local peer */
5578 peer = peer_get (&my_full_id);
5581 GNUNET_PEER_change_rc (myid, 1);
5582 peer_info_add_path (peer, p, GNUNET_YES);
5583 GNUNET_SERVER_resume (server_handle);
5584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5589 * Process mesh requests.
5591 * @param cls closure
5592 * @param server the initialized server
5593 * @param c configuration to use
5596 run (void *cls, struct GNUNET_SERVER_Handle *server,
5597 const struct GNUNET_CONFIGURATION_Handle *c)
5601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5602 server_handle = server;
5605 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5610 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5611 "mesh", "peer/privatekey");
5612 GNUNET_SCHEDULER_shutdown ();
5617 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5618 &refresh_path_time))
5620 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5622 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5623 "mesh", "refresh path time");
5624 GNUNET_SCHEDULER_shutdown ();
5629 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5632 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5634 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5635 "mesh", "id announce time");
5636 GNUNET_SCHEDULER_shutdown ();
5641 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5644 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5646 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5647 "mesh", "connect timeout");
5648 GNUNET_SCHEDULER_shutdown ();
5653 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5656 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5658 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5659 "mesh", "max msgs queue");
5660 GNUNET_SCHEDULER_shutdown ();
5665 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5668 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5670 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5671 "mesh", "max tunnels");
5672 GNUNET_SCHEDULER_shutdown ();
5677 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5680 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5682 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5683 "mesh", "default ttl", 64);
5688 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5691 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5692 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5693 "mesh", "max peers", 1000);
5698 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5699 &dht_replication_level))
5701 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5703 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5704 "mesh", "dht replication level", 3);
5705 dht_replication_level = 3;
5708 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5709 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5710 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5711 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5713 dht_handle = GNUNET_DHT_connect (c, 64);
5714 if (NULL == dht_handle)
5718 stats = GNUNET_STATISTICS_create ("mesh", c);
5720 GNUNET_SERVER_suspend (server_handle);
5721 /* Scheduled the task to clean up when shutdown is called */
5722 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5724 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
5727 GNUNET_free (keyfile);
5732 * The main function for the mesh service.
5734 * @param argc number of arguments from the command line
5735 * @param argv command line arguments
5736 * @return 0 ok, 1 on error
5739 main (int argc, char *const *argv)
5744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5745 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5747 ret = (GNUNET_OK == r) ? 0 : 1;
5748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5752 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5753 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5754 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);