2 This file is part of GNUnet.
3 (C) 2001-2013 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
27 * - keep queues until receiving ACK
28 * - when sending in-order buffered data, wait for client ACKs
29 * - refactor unicast to make generic handling, assigning *rel and *fc
32 * - relay corking down to core
33 * - set ttl relative to path length
41 #include "mesh_protocol.h"
42 #include "mesh_path.h"
43 #include "block_mesh.h"
44 #include "gnunet_dht_service.h"
45 #include "gnunet_statistics_service.h"
47 #define MESH_BLOOM_SIZE 128
49 #define MESH_DEBUG_DHT GNUNET_NO
50 #define MESH_DEBUG_CONNECTION GNUNET_NO
51 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
53 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
54 GNUNET_TIME_UNIT_MINUTES,\
56 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
57 #define MESH_RETRANSMIT_MARGIN 4
59 #if MESH_DEBUG_CONNECTION
60 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
62 #define DEBUG_CONN(...)
66 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
68 #define DEBUG_DHT(...)
75 struct timespec __mesh_start;
76 struct timespec __mesh_end;
77 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
78 #define INTERVAL_END \
80 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
81 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
82 if (__diff < 0) __diff += 1000000000;\
86 #define INTERVAL_SHOW \
88 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
90 #define INTERVAL_START
95 /******************************************************************************/
96 /************************ DATA STRUCTURES ****************************/
97 /******************************************************************************/
99 /** FWD declaration */
105 * Struct containing info about a queued transmission to this peer
112 struct MeshPeerQueue *next;
117 struct MeshPeerQueue *prev;
120 * Peer this transmission is directed to.
122 struct MeshPeerInfo *peer;
125 * Tunnel this message belongs to.
127 struct MeshTunnel *tunnel;
130 * Pointer to info stucture used as cls.
140 * Size of the message
147 * Struct containing all information regarding a given peer
157 * Last time we heard from this peer
159 struct GNUNET_TIME_Absolute last_contact;
162 * Number of attempts to reconnect so far
164 int n_reconnect_attempts;
167 * Paths to reach the peer, ordered by ascending hop count
169 struct MeshPeerPath *path_head;
172 * Paths to reach the peer, ordered by ascending hop count
174 struct MeshPeerPath *path_tail;
177 * Handle to stop the DHT search for a path to this peer
179 struct GNUNET_DHT_GetHandle *dhtget;
182 * Array of tunnels this peer is the target of.
183 * Most probably a small amount, therefore not a hashmap.
184 * When the path to the peer changes, notify these tunnels to let them
185 * re-adjust their path trees.
187 struct MeshTunnel **tunnels;
190 * Number of tunnels this peers participates in
192 unsigned int ntunnels;
195 * Transmission queue to core DLL head
197 struct MeshPeerQueue *queue_head;
200 * Transmission queue to core DLL tail
202 struct MeshPeerQueue *queue_tail;
205 * How many messages are in the queue to this peer.
207 unsigned int queue_n;
210 * Handle for queued transmissions
212 struct GNUNET_CORE_TransmitHandle *core_transmit;
217 * Struct to encapsulate all the Flow Control information to a peer in the
218 * context of a tunnel: Same peer in different tunnels will have independent
219 * flow control structures, allowing to choke/free tunnels according to its
222 struct MeshFlowControl
225 * ID of the last packet sent towards the peer.
227 uint32_t last_pid_sent;
230 * ID of the last packet received from the peer.
232 uint32_t last_pid_recv;
235 * Last ACK sent to the peer (peer can't send more than this PID).
237 uint32_t last_ack_sent;
240 * Last ACK sent towards the origin (for traffic towards leaf node).
242 uint32_t last_ack_recv;
245 * How many payload messages are in the queue towards this peer.
250 * Task to poll the peer in case of a lost ACK causes stall.
252 GNUNET_SCHEDULER_TaskIdentifier poll_task;
255 * How frequently to poll for ACKs.
257 struct GNUNET_TIME_Relative poll_time;
260 * On which tunnel to poll.
261 * Using an explicit poll_ctx would not help memory wise,
262 * since the allocated context would have to be stored in the
263 * fc struct in order to free it upon cancelling poll_task.
265 struct MeshTunnel *t;
270 * Globally unique tunnel identification (owner + number)
271 * DO NOT USE OVER THE NETWORK
276 * Node that owns the tunnel
281 * Tunnel number to differentiate all the tunnels owned by the node oid
282 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
284 MESH_TunnelNumber tid;
289 * Data needed for reliable tunnel endpoint retransmission management.
291 struct MeshTunnelReliability;
295 * Info needed to retry a message in case it gets lost.
297 struct MeshReliableMessage
300 * Double linked list, FIFO style
302 struct MeshReliableMessage *next;
303 struct MeshReliableMessage *prev;
306 * Tunnel Reliability queue this message is in.
308 struct MeshTunnelReliability *rel;
311 * ID of the message (ACK needed to free)
316 * When was this message issued (to calculate ACK delay) FIXME update with traffic
318 struct GNUNET_TIME_Absolute timestamp;
320 /* struct GNUNET_MESH_Data with payload */
324 struct MeshTunnelReliability
327 * Tunnel this is about.
329 struct MeshTunnel *t;
332 * DLL of messages sent and not yet ACK'd.
334 struct MeshReliableMessage *head_sent;
335 struct MeshReliableMessage *tail_sent;
348 * DLL of messages received out of order.
350 struct MeshReliableMessage *head_recv;
351 struct MeshReliableMessage *tail_recv;
359 * Task to resend/poll in case no ACK is received.
361 GNUNET_SCHEDULER_TaskIdentifier retry_task;
364 * Counter for exponential backoff.
366 struct GNUNET_TIME_Relative retry_timer;
369 * How long does it usually take to get an ACK. FIXME update with traffic
371 struct GNUNET_TIME_Relative expected_delay;
376 * Struct containing all information regarding a tunnel
377 * For an intermediate node the improtant info used will be:
378 * - id Tunnel unique identification
379 * - paths[0] To know where to send it next
380 * - metainfo: ready, speeds, accounting
387 struct MESH_TunnelID id;
390 * Port of the tunnel.
395 * State of the tunnel.
397 enum MeshTunnelState state;
400 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
402 MESH_TunnelNumber local_tid;
405 * Local tunnel number for local destination clients (incoming number)
406 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
409 MESH_TunnelNumber local_tid_dest;
412 * Is the tunnel bufferless (minimum latency)?
417 * Is the tunnel reliable?
422 * Force sending ACK? Flag to allow duplicate ACK on POLL.
427 * How many messages do we accept in the forward queue.
429 unsigned int queue_max;
432 * Last time the tunnel was used
434 struct GNUNET_TIME_Absolute timestamp;
437 * Destination of the tunnel.
442 * Next hop in the tunnel. If 0, @c client must be set.
444 GNUNET_PEER_Id next_hop;
447 * Previous hop in the tunnel. If 0, @c owner must be set.
449 GNUNET_PEER_Id prev_hop;
452 * Flow control information about @c next_hop or @c client.
454 struct MeshFlowControl next_fc;
457 * Flow control information about @c prev_hop or @c owner.
459 struct MeshFlowControl prev_fc;
462 * Client owner of the tunnel, if any
464 struct MeshClient *owner;
467 * Client destination of the tunnel, if any.
469 struct MeshClient *client;
472 * Task to keep the used paths alive at the owner,
473 * time tunnel out on all the other peers.
475 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
478 * Path being used for the tunnel.
480 struct MeshPeerPath *path;
483 * Flag to signal the destruction of the tunnel.
484 * If this is set GNUNET_YES the tunnel will be destroyed
485 * when the queue is empty.
490 * Total messages pending for this tunnels, payload or not.
492 unsigned int pending_messages;
496 * Only present (non-NULL) at the owner of a tunnel.
498 struct MeshTunnelReliability *fwd_rel;
502 * Only present (non-NULL) at the destination of a tunnel.
504 struct MeshTunnelReliability *bck_rel;
509 * Struct containing information about a client of the service
511 * TODO: add a list of 'waiting' ports
518 struct MeshClient *next;
523 struct MeshClient *prev;
526 * Tunnels that belong to this client, indexed by local id
528 struct GNUNET_CONTAINER_MultiHashMap32 *own_tunnels;
531 * Tunnels this client has accepted, indexed by incoming local id
533 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
536 * Handle to communicate with the client
538 struct GNUNET_SERVER_Client *handle;
541 * Ports that this client has declared interest in.
542 * Indexed by port, contains *Client.
544 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
547 * Whether the client is active or shutting down (don't send confirmations
548 * to a client that is shutting down.
553 * ID of the client, mainly for debug messages
560 /******************************************************************************/
561 /************************ DEBUG FUNCTIONS ****************************/
562 /******************************************************************************/
566 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
567 * @param cls string to print
568 * @param success GNUNET_OK if the PUT was transmitted,
569 * GNUNET_NO on timeout,
570 * GNUNET_SYSERR on disconnect from service
571 * after the PUT message was transmitted
572 * (so we don't know if it was received or not)
577 mesh_debug (void *cls, int success)
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
586 unsigned int debug_fwd_ack;
587 unsigned int debug_bck_ack;
591 /******************************************************************************/
592 /*********************** GLOBAL VARIABLES ****************************/
593 /******************************************************************************/
595 /************************** Configuration parameters **************************/
598 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
600 static struct GNUNET_TIME_Relative refresh_path_time;
603 * How often to PUT own ID in the DHT.
605 static struct GNUNET_TIME_Relative id_announce_time;
608 * Maximum time allowed to connect to a peer found by string.
610 static struct GNUNET_TIME_Relative connect_timeout;
613 * Default TTL for payload packets.
615 static unsigned long long default_ttl;
618 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
620 static unsigned long long dht_replication_level;
623 * How many tunnels are we willing to maintain.
624 * Local tunnels are always allowed, even if there are more tunnels than max.
626 static unsigned long long max_tunnels;
629 * How many messages *in total* are we willing to queue, divided by number of
630 * tunnels to get tunnel queue size.
632 static unsigned long long max_msgs_queue;
635 * How many peers do we want to remember?
637 static unsigned long long max_peers;
640 * Percentage of messages that will be dropped (for test purposes only).
642 static unsigned long long drop_percent;
644 /*************************** Static global variables **************************/
647 * DLL with all the clients, head.
649 static struct MeshClient *clients_head;
652 * DLL with all the clients, tail.
654 static struct MeshClient *clients_tail;
657 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
659 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
662 * Number of tunnels known.
664 static unsigned long long n_tunnels;
667 * Tunnels incoming, indexed by MESH_TunnelNumber
668 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
670 static struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
673 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
675 static struct GNUNET_CONTAINER_MultiHashMap *peers;
678 * Handle to communicate with transport
680 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
683 * Handle to communicate with core.
685 static struct GNUNET_CORE_Handle *core_handle;
690 static struct GNUNET_DHT_Handle *dht_handle;
695 static struct GNUNET_SERVER_Handle *server_handle;
698 * Handle to the statistics service.
700 static struct GNUNET_STATISTICS_Handle *stats;
703 * Notification context, to send messages to local clients.
705 static struct GNUNET_SERVER_NotificationContext *nc;
708 * Local peer own ID (memory efficient handle).
710 static GNUNET_PEER_Id myid;
713 * Local peer own ID (full value).
715 static struct GNUNET_PeerIdentity my_full_id;
720 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
725 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
728 * Tunnel ID for the next created tunnel (global tunnel number).
730 static MESH_TunnelNumber next_tid;
733 * Tunnel ID for the next incoming tunnel (local tunnel number).
735 static MESH_TunnelNumber next_local_tid;
738 * All ports clients of this peer have opened.
740 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
743 * Task to periodically announce itself in the network.
745 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
748 * Next ID to assign to a client.
750 unsigned int next_client_id;
753 /******************************************************************************/
754 /*********************** DECLARATIONS **************************/
755 /******************************************************************************/
758 * Function to process paths received for a new peer addition. The recorded
759 * paths form the initial tunnel, which can be optimized later.
760 * Called on each result obtained for the DHT search.
763 * @param exp when will this value expire
764 * @param key key of the result
765 * @param type type of the result
766 * @param size number of bytes in data
767 * @param data pointer to the result data
770 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
771 const struct GNUNET_HashCode * key,
772 const struct GNUNET_PeerIdentity *get_path,
773 unsigned int get_path_length,
774 const struct GNUNET_PeerIdentity *put_path,
775 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
776 size_t size, const void *data);
780 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
781 * and insert it in the appropriate structures if the peer is not known yet.
783 * @param peer Full identity of the peer.
785 * @return Existing or newly created peer info.
787 static struct MeshPeerInfo *
788 peer_get (const struct GNUNET_PeerIdentity *peer);
792 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
793 * and insert it in the appropriate structures if the peer is not known yet.
795 * @param peer Short identity of the peer.
797 * @return Existing or newly created peer info.
799 static struct MeshPeerInfo *
800 peer_get_short (const GNUNET_PEER_Id peer);
804 * Build a PeerPath from the paths returned from the DHT, reversing the paths
805 * to obtain a local peer -> destination path and interning the peer ids.
807 * @return Newly allocated and created path
809 static struct MeshPeerPath *
810 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
811 unsigned int get_path_length,
812 const struct GNUNET_PeerIdentity *put_path,
813 unsigned int put_path_length);
817 * Adds a path to the peer_infos of all the peers in the path
819 * @param p Path to process.
820 * @param confirmed Whether we know if the path works or not.
823 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
828 * Search for a tunnel by global ID using full PeerIdentities.
830 * @param oid owner of the tunnel.
831 * @param tid global tunnel number.
833 * @return tunnel handler, NULL if doesn't exist.
835 static struct MeshTunnel *
836 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
840 * Notify a tunnel that a connection has broken that affects at least
843 * @param t Tunnel affected.
844 * @param p1 Peer that got disconnected from p2.
845 * @param p2 Peer that got disconnected from p1.
847 * @return Short ID of the peer disconnected (either p1 or p2).
848 * 0 if the tunnel remained unaffected.
850 static GNUNET_PEER_Id
851 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
856 * @brief Use the given path for the tunnel.
857 * Update the next and prev hops (and RCs).
858 * (Re)start the path refresh in case the tunnel is locally owned.
860 * @param t Tunnel to update.
861 * @param p Path to use.
864 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
867 * Tunnel is empty: destroy it.
869 * Notifies all participants (peers, cleints) about the destruction.
871 * @param t Tunnel to destroy.
874 tunnel_destroy_empty (struct MeshTunnel *t);
877 * @brief Queue and pass message to core when possible.
879 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
880 * and accounts for it. In case the queue is full, the message is dropped and
883 * Otherwise, message is treated as internal and allowed to go regardless of
886 * @param cls Closure (@c type dependant). It will be used by queue_send to
887 * build the message to be sent if not already prebuilt.
888 * @param type Type of the message, 0 for a raw message.
889 * @param size Size of the message.
890 * @param dst Neighbor to send message to.
891 * @param t Tunnel this message belongs to.
894 queue_add (void *cls, uint16_t type, size_t size,
895 struct MeshPeerInfo *dst, struct MeshTunnel *t);
899 * Free a transmission that was already queued with all resources
900 * associated to the request.
902 * @param queue Queue handler to cancel.
903 * @param clear_cls Is it necessary to free associated cls?
906 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
910 * @brief Get the next transmittable message from the queue.
912 * This will be the head, except in the case of being a data packet
913 * not allowed by the destination peer.
915 * @param peer Destination peer.
917 * @return The next viable MeshPeerQueue element to send to that peer.
918 * NULL when there are no transmittable messages.
920 struct MeshPeerQueue *
921 queue_get_next (const struct MeshPeerInfo *peer);
925 * Core callback to write a queued packet to core buffer
927 * @param cls Closure (peer info).
928 * @param size Number of bytes available in buf.
929 * @param buf Where the to write the message.
931 * @return number of bytes written to buf
934 queue_send (void *cls, size_t size, void *buf);
937 /******************************************************************************/
938 /************************ PERIODIC FUNCTIONS ****************************/
939 /******************************************************************************/
942 * Periodically announce self id in the DHT
945 * @param tc task context
948 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
952 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
954 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
958 * - Set data expiration in function of X
961 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
963 block.id = my_full_id;
964 GNUNET_DHT_put (dht_handle, /* DHT handle */
965 &my_full_id.hashPubKey, /* Key to use */
966 dht_replication_level, /* Replication level */
967 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
968 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
969 sizeof (block), /* Size of the data */
970 (const char *) &block, /* Data itself */
971 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
972 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
973 NULL, /* Continuation */
974 NULL); /* Continuation closure */
976 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
980 /******************************************************************************/
981 /****************** GENERAL HELPER FUNCTIONS ************************/
982 /******************************************************************************/
986 * Check if client has registered with the service and has not disconnected
988 * @param client the client to check
990 * @return non-NULL if client exists in the global DLL
992 static struct MeshClient *
993 client_get (struct GNUNET_SERVER_Client *client)
995 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1000 * Deletes a tunnel from a client (either owner or destination). To be used on
1003 * @param c Client whose tunnel to delete.
1004 * @param t Tunnel which should be deleted.
1007 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1011 GNUNET_assert (GNUNET_YES ==
1012 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
1018 GNUNET_assert (GNUNET_YES ==
1019 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
1026 * Notify the appropriate client that a new incoming tunnel was created.
1028 * @param t Tunnel that was created.
1031 send_client_tunnel_create (struct MeshTunnel *t)
1033 struct GNUNET_MESH_TunnelMessage msg;
1035 if (NULL == t->client)
1037 msg.header.size = htons (sizeof (msg));
1038 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1039 msg.tunnel_id = htonl (t->local_tid_dest);
1040 msg.port = htonl (t->port);
1042 msg.opt |= GNUNET_YES == t->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1043 msg.opt |= GNUNET_YES == t->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1044 msg.opt = htonl (msg.opt);
1045 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
1046 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1047 &msg.header, GNUNET_NO);
1052 * Notify dest client that the incoming tunnel is no longer valid.
1054 * @param c Client to notify..
1055 * @param t Tunnel that is destroyed.
1058 send_client_tunnel_destroy (struct MeshClient *c, struct MeshTunnel *t)
1060 struct GNUNET_MESH_TunnelMessage msg;
1067 if (c != t->client && c != t->owner)
1072 msg.header.size = htons (sizeof (msg));
1073 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1074 msg.tunnel_id = htonl (t->local_tid_dest);
1075 msg.port = htonl (0);
1076 memset(&msg.peer, 0, sizeof (msg.peer));
1077 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1078 &msg.header, GNUNET_NO);
1083 * Iterator over all the peers to remove the oldest not-used entry.
1085 * @param cls Closure (unsued).
1086 * @param key ID of the peer.
1087 * @param value Peer_Info of the peer.
1092 peer_info_timeout (void *cls,
1093 const struct GNUNET_HashCode *key,
1100 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1101 * and insert it in the appropriate structures if the peer is not known yet.
1103 * @param peer Full identity of the peer.
1105 * @return Existing or newly created peer info.
1107 static struct MeshPeerInfo *
1108 peer_get (const struct GNUNET_PeerIdentity *peer)
1110 struct MeshPeerInfo *peer_info;
1112 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1113 if (NULL == peer_info)
1116 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1117 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1119 GNUNET_CONTAINER_multihashmap_iterate (peers,
1123 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1124 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1125 peer_info->id = GNUNET_PEER_intern (peer);
1127 peer_info->last_contact = GNUNET_TIME_absolute_get();
1134 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1135 * and insert it in the appropriate structures if the peer is not known yet.
1137 * @param peer Short identity of the peer.
1139 * @return Existing or newly created peer info.
1141 static struct MeshPeerInfo *
1142 peer_get_short (const GNUNET_PEER_Id peer)
1144 struct GNUNET_PeerIdentity id;
1146 GNUNET_PEER_resolve (peer, &id);
1147 return peer_get (&id);
1152 * Choose the best path towards a peer considering the tunnel properties.
1154 * @param peer The destination peer.
1155 * @param t The tunnel the path is for.
1157 * @return Best current known path towards the peer, if any.
1159 static struct MeshPeerPath *
1160 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1162 struct MeshPeerPath *best_p;
1163 struct MeshPeerPath *p;
1164 unsigned int best_cost;
1167 best_p = p = peer->path_head;
1168 best_cost = cost = p->length;
1171 if ((cost = p->length) < best_cost)
1182 * Core callback to write a pre-constructed data packet to core buffer
1184 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1185 * @param size Number of bytes available in buf.
1186 * @param buf Where the to write the message.
1188 * @return number of bytes written to buf
1191 send_core_data_raw (void *cls, size_t size, void *buf)
1193 struct GNUNET_MessageHeader *msg = cls;
1196 GNUNET_assert (NULL != msg);
1197 total_size = ntohs (msg->size);
1199 if (total_size > size)
1204 memcpy (buf, msg, total_size);
1211 * Sends an already built message to a peer, properly registrating
1212 * all used resources.
1214 * @param message Message to send. Function makes a copy of it.
1215 * @param peer Short ID of the neighbor whom to send the message.
1216 * @param t Tunnel on which this message is transmitted.
1219 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1220 GNUNET_PEER_Id peer,
1221 struct MeshTunnel *t)
1223 struct GNUNET_PeerIdentity id;
1224 struct MeshPeerInfo *neighbor;
1225 struct MeshPeerPath *p;
1230 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1235 size = ntohs (message->size);
1236 data = GNUNET_malloc (size);
1237 memcpy (data, message, size);
1238 type = ntohs(message->type);
1239 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1240 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1242 struct GNUNET_MESH_Data *u;
1244 u = (struct GNUNET_MESH_Data *) data;
1245 u->ttl = htonl (ntohl (u->ttl) - 1);
1247 GNUNET_PEER_resolve (peer, &id);
1248 neighbor = peer_get (&id);
1249 for (p = neighbor->path_head; NULL != p; p = p->next)
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1260 " %s IS NOT DIRECTLY CONNECTED\n",
1262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 for (p = neighbor->path_head; NULL != p; p = p->next)
1267 struct GNUNET_PeerIdentity debug_id;
1270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1271 " path with %u hops through:\n",
1273 for (i = 0; i < p->length; i++)
1275 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1278 i, GNUNET_i2s(&debug_id));
1282 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1283 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1284 " no direct connection to %s\n",
1289 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1300 * Sends a CREATE PATH message for a path to a peer, properly registrating
1301 * all used resources.
1303 * @param t Tunnel for which the path is created.
1306 send_create_path (struct MeshTunnel *t)
1308 struct MeshPeerInfo *neighbor;
1310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1311 neighbor = peer_get_short (t->next_hop);
1313 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1314 sizeof (struct GNUNET_MESH_CreateTunnel) +
1315 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1318 t->state = MESH_TUNNEL_WAITING;
1323 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1325 * @param t Tunnel which to confirm.
1328 send_path_ack (struct MeshTunnel *t)
1330 struct MeshPeerInfo *peer;
1332 peer = peer_get_short (t->prev_hop);
1335 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1336 sizeof (struct GNUNET_MESH_PathACK),
1343 * Try to establish a new connection to this peer in the given tunnel.
1344 * If the peer doesn't have any path to it yet, try to get one.
1345 * If the peer already has some path, send a CREATE PATH towards it.
1347 * @param peer PeerInfo of the peer.
1348 * @param t Tunnel for which to create the path, if possible.
1351 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1353 struct MeshPeerPath *p;
1355 if (NULL != peer->path_head)
1357 p = peer_get_best_path (peer, t);
1358 tunnel_use_path (t, p);
1359 send_create_path (t);
1361 else if (NULL == peer->dhtget)
1363 struct GNUNET_PeerIdentity id;
1365 GNUNET_PEER_resolve (peer->id, &id);
1366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1368 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1369 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1370 &id.hashPubKey, /* key to search */
1371 dht_replication_level, /* replication level */
1372 GNUNET_DHT_RO_RECORD_ROUTE |
1373 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1375 0, /* xquery bits */
1376 &dht_get_id_handler, peer);
1377 t->state = MESH_TUNNEL_SEARCHING;
1381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1382 "There is no path but the DHT GET is already started.\n");
1388 * Destroy the peer_info and free any allocated resources linked to it
1390 * @param pi The peer_info to destroy.
1392 * @return GNUNET_OK on success
1395 peer_info_destroy (struct MeshPeerInfo *pi)
1397 struct GNUNET_PeerIdentity id;
1398 struct MeshPeerPath *p;
1399 struct MeshPeerPath *nextp;
1402 GNUNET_PEER_resolve (pi->id, &id);
1403 GNUNET_PEER_change_rc (pi->id, -1);
1406 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1409 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1410 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1412 if (NULL != pi->dhtget)
1414 GNUNET_DHT_get_stop (pi->dhtget);
1420 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1424 for (i = 0; i < pi->ntunnels; i++)
1425 tunnel_destroy_empty (pi->tunnels[i]);
1426 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1433 * Remove all paths that rely on a direct connection between p1 and p2
1434 * from the peer itself and notify all tunnels about it.
1436 * @param peer PeerInfo of affected peer.
1437 * @param p1 GNUNET_PEER_Id of one peer.
1438 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1441 * TODO: optimize (see below)
1444 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1447 struct MeshPeerPath *p;
1448 struct MeshPeerPath *next;
1449 struct MeshPeerInfo *peer_d;
1451 unsigned int destroyed;
1454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1456 for (p = peer->path_head; NULL != p; p = next)
1459 for (i = 0; i < (p->length - 1); i++)
1461 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1462 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1464 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1474 for (i = 0; i < peer->ntunnels; i++)
1476 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1480 peer_d = peer_get_short (d);
1481 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1482 tunnel_use_path (peer->tunnels[i], next);
1483 peer_connect (peer_d, peer->tunnels[i]);
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1490 * Add the path to the peer and update the path used to reach it in case this
1493 * @param peer_info Destination peer to add the path to.
1494 * @param path New path to add. Last peer must be the peer in arg 1.
1495 * Path will be either used of freed if already known.
1496 * @param trusted Do we trust that this path is real?
1499 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1502 struct MeshPeerPath *aux;
1506 if ((NULL == peer_info) || (NULL == path))
1509 path_destroy (path);
1512 if (path->peers[path->length - 1] != peer_info->id)
1515 path_destroy (path);
1518 if (2 >= path->length && GNUNET_NO == trusted)
1520 /* Only allow CORE to tell us about direct paths */
1521 path_destroy (path);
1524 for (l = 1; l < path->length; l++)
1526 if (path->peers[l] == myid)
1528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1529 for (l2 = 0; l2 < path->length - l; l2++)
1531 path->peers[l2] = path->peers[l + l2];
1536 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1541 struct GNUNET_PeerIdentity id;
1543 GNUNET_PEER_resolve (peer_info->id, &id);
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1545 path->length, GNUNET_i2s (&id));
1548 l = path_get_length (path);
1551 path_destroy (path);
1555 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1556 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1558 l2 = path_get_length (aux);
1561 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1562 peer_info->path_tail, aux, path);
1567 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1569 path_destroy (path);
1574 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1581 * Add the path to the origin peer and update the path used to reach it in case
1582 * this is the shortest.
1583 * The path is given in peer_info -> destination, therefore we turn the path
1584 * upside down first.
1586 * @param peer_info Peer to add the path to, being the origin of the path.
1587 * @param path New path to add after being inversed.
1588 * Path will be either used or freed.
1589 * @param trusted Do we trust that this path is real?
1592 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1593 struct MeshPeerPath *path, int trusted)
1596 peer_info_add_path (peer_info, path, trusted);
1601 * Add a tunnel to the list of tunnels a peer participates in.
1602 * Update the tunnel's destination.
1604 * @param p Peer to add to.
1605 * @param t Tunnel to add.
1608 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1612 GNUNET_break (t->dest == p->id);
1616 GNUNET_PEER_change_rc (t->dest, 1);
1617 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1622 * Remove a tunnel from the list of tunnels a peer participates in.
1623 * Free the tunnel's destination.
1625 * @param p Peer to clean.
1626 * @param t Tunnel to remove.
1629 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1633 if (t->dest == p->id)
1635 GNUNET_PEER_change_rc (t->dest, -1);
1638 for (i = 0; i < p->ntunnels; i++)
1640 if (p->tunnels[i] == t)
1642 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1643 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1651 * Function called if the connection to the peer has been stalled for a while,
1652 * possibly due to a missed ACK. Poll the peer about its ACK status.
1654 * @param cls Closure (poll ctx).
1655 * @param tc TaskContext.
1658 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1660 struct MeshFlowControl *fc = cls;
1661 struct GNUNET_MESH_Poll msg;
1662 struct MeshTunnel *t = fc->t;
1663 GNUNET_PEER_Id peer;
1665 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1666 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1673 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1674 msg.header.size = htons (sizeof (msg));
1675 msg.tid = htonl (t->id.tid);
1676 msg.pid = htonl (fc->last_pid_sent);
1677 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1679 if (fc == &t->prev_fc)
1681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
1684 else if (fc == &t->next_fc)
1686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
1694 send_prebuilt_message (&msg.header, peer, t);
1695 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1696 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1702 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1703 * to obtain a local peer -> destination path and interning the peer ids.
1705 * @return Newly allocated and created path
1707 static struct MeshPeerPath *
1708 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1709 unsigned int get_path_length,
1710 const struct GNUNET_PeerIdentity *put_path,
1711 unsigned int put_path_length)
1713 struct MeshPeerPath *p;
1719 GNUNET_PEER_change_rc (myid, 1);
1720 i = get_path_length;
1721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1722 for (i--; i >= 0; i--)
1724 id = GNUNET_PEER_intern (&get_path[i]);
1725 if (p->length > 0 && id == p->peers[p->length - 1])
1727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1728 GNUNET_PEER_change_rc (id, -1);
1732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1733 GNUNET_i2s (&get_path[i]));
1735 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1736 p->peers[p->length - 1] = id;
1739 i = put_path_length;
1740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1741 for (i--; i >= 0; i--)
1743 id = GNUNET_PEER_intern (&put_path[i]);
1746 /* PUT path went through us, so discard the path up until now and start
1747 * from here to get a much shorter (and loop-free) path.
1752 if (p->length > 0 && id == p->peers[p->length - 1])
1754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1755 GNUNET_PEER_change_rc (id, -1);
1759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1760 GNUNET_i2s (&put_path[i]));
1762 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1763 p->peers[p->length - 1] = id;
1767 if (get_path_length > 0)
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1769 GNUNET_i2s (&get_path[0]));
1770 if (put_path_length > 0)
1771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1772 GNUNET_i2s (&put_path[0]));
1773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1775 for (i = 0; i < p->length; i++)
1777 struct GNUNET_PeerIdentity peer_id;
1779 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1781 GNUNET_i2s (&peer_id));
1789 * Adds a path to the peer_infos of all the peers in the path
1791 * @param p Path to process.
1792 * @param confirmed Whether we know if the path works or not.
1795 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1799 /* TODO: invert and add */
1800 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1801 for (i++; i < p->length; i++)
1803 struct MeshPeerInfo *aux;
1804 struct MeshPeerPath *copy;
1806 aux = peer_get_short (p->peers[i]);
1807 copy = path_duplicate (p);
1808 copy->length = i + 1;
1809 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1815 * Send keepalive packets for a peer
1817 * @param cls Closure (tunnel for which to send the keepalive).
1818 * @param tc Notification context.
1821 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1825 * Search for a tunnel among the incoming tunnels
1827 * @param tid the local id of the tunnel
1829 * @return tunnel handler, NULL if doesn't exist
1831 static struct MeshTunnel *
1832 tunnel_get_incoming (MESH_TunnelNumber tid)
1834 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1835 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
1840 * Search for a tunnel among the tunnels for a client
1842 * @param c the client whose tunnels to search in
1843 * @param tid the local id of the tunnel
1845 * @return tunnel handler, NULL if doesn't exist
1847 static struct MeshTunnel *
1848 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1850 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1852 return tunnel_get_incoming (tid);
1856 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI);
1857 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
1863 * Search for a tunnel by global ID using PEER_ID
1865 * @param pi owner of the tunnel
1866 * @param tid global tunnel number
1868 * @return tunnel handler, NULL if doesn't exist
1870 static struct MeshTunnel *
1871 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1873 struct MESH_TunnelID id;
1874 struct GNUNET_HashCode hash;
1879 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1880 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1885 * Search for a tunnel by global ID using full PeerIdentities
1887 * @param oid owner of the tunnel
1888 * @param tid global tunnel number
1890 * @return tunnel handler, NULL if doesn't exist
1892 static struct MeshTunnel *
1893 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1895 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1900 * Add a client to a tunnel, initializing all needed data structures.
1902 * @param t Tunnel to which add the client.
1903 * @param c Client which to add to the tunnel.
1906 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1908 if (NULL != t->client)
1914 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
1915 t->local_tid_dest, t,
1916 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1922 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
1923 t->local_tid_dest, t,
1924 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1934 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1936 unsigned int own_pos;
1938 for (own_pos = 0; own_pos < p->length; own_pos++)
1940 if (p->peers[own_pos] == myid)
1943 if (own_pos > p->length - 1)
1949 if (own_pos < p->length - 1)
1950 t->next_hop = p->peers[own_pos + 1];
1952 t->next_hop = p->peers[own_pos];
1953 GNUNET_PEER_change_rc (t->next_hop, 1);
1955 t->prev_hop = p->peers[own_pos - 1];
1957 t->prev_hop = p->peers[0];
1958 GNUNET_PEER_change_rc (t->prev_hop, 1);
1960 if (NULL != t->path)
1961 path_destroy (t->path);
1962 t->path = path_duplicate (p);
1965 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1966 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1967 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
1974 * Notifies a tunnel that a connection has broken that affects at least
1975 * some of its peers. Sends a notification towards the root of the tree.
1976 * In case the peer is the owner of the tree, notifies the client that owns
1977 * the tunnel and tries to reconnect.
1979 * @param t Tunnel affected.
1980 * @param p1 Peer that got disconnected from p2.
1981 * @param p2 Peer that got disconnected from p1.
1983 * @return Short ID of the peer disconnected (either p1 or p2).
1984 * 0 if the tunnel remained unaffected.
1986 static GNUNET_PEER_Id
1987 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1990 // if (myid != p1 && myid != p2) FIXME
1995 // if (tree_get_predecessor (t->tree) != 0)
1997 // /* We are the peer still connected, notify owner of the disconnection. */
1998 // struct GNUNET_MESH_PathBroken msg;
1999 // struct GNUNET_PeerIdentity neighbor;
2001 // msg.header.size = htons (sizeof (msg));
2002 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2003 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2004 // msg.tid = htonl (t->id.tid);
2005 // msg.peer1 = my_full_id;
2006 // GNUNET_PEER_resolve (pid, &msg.peer2);
2007 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2008 // send_prebuilt_message (&msg.header, &neighbor, t);
2015 * Build a local ACK message and send it to a local client.
2017 * @param t Tunnel on which to send the ACK.
2018 * @param c Client to whom send the ACK.
2019 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
2022 send_local_ack (struct MeshTunnel *t,
2023 struct MeshClient *c,
2026 struct GNUNET_MESH_LocalAck msg;
2028 msg.header.size = htons (sizeof (msg));
2029 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2030 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
2031 GNUNET_SERVER_notification_context_unicast (nc,
2038 * Build an ACK message and queue it to send to the given peer.
2040 * @param t Tunnel on which to send the ACK.
2041 * @param peer Peer to whom send the ACK.
2042 * @param ack Value of the ACK.
2045 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
2047 struct GNUNET_MESH_ACK msg;
2049 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2050 msg.header.size = htons (sizeof (msg));
2051 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2052 msg.pid = htonl (ack);
2053 msg.tid = htonl (t->id.tid);
2055 send_prebuilt_message (&msg.header, peer, t);
2060 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2062 * @param t Tunnel this is about.
2065 tunnel_send_fwd_data_ack (struct MeshTunnel *t)
2067 struct GNUNET_MESH_DataACK msg;
2068 struct MeshTunnelReliability *rel;
2069 struct MeshReliableMessage *copy;
2074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2075 "send_fwd_data_ack for %llu\n",
2076 t->bck_rel->mid_recv - 1);
2078 if (GNUNET_NO == t->reliable)
2080 GNUNET_break_op (0);
2083 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK);
2084 msg.header.size = htons (sizeof (msg));
2085 msg.tid = htonl (t->id.tid);
2086 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2087 msg.mid = GNUNET_htonll (t->bck_rel->mid_recv - 1);
2090 for (i = 0, copy = rel->head_recv;
2091 i < 64 && NULL != copy;
2092 i++, copy = copy->next)
2094 delta = copy->mid - t->bck_rel->mid_recv;
2095 mask = 0x1 << delta;
2096 msg.futures |= mask;
2097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2098 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2099 copy->mid, delta, mask, msg.futures);
2101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2103 send_prebuilt_message (&msg.header, t->prev_hop, t);
2104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_fwd_data_ack END\n");
2109 * Send an end-to-end BCK ACK message for the most recent in-sequence payload.
2111 * @param t Tunnel this is about.
2114 tunnel_send_bck_data_ack (struct MeshTunnel *t)
2116 struct GNUNET_MESH_DataACK msg;
2118 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2119 msg.header.size = htons (sizeof (msg));
2120 msg.tid = htonl (t->id.tid);
2121 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2122 msg.mid = GNUNET_htonll (t->fwd_rel->mid_recv - 1);
2123 msg.futures = 0; // FIXME set bits of other newer messages received
2125 send_prebuilt_message (&msg.header, t->next_hop, t);
2130 * Send an ACK informing the predecessor about the available buffer space.
2131 * In case there is no predecessor, inform the owning client.
2132 * If buffering is off, send only on behalf of children or self if endpoint.
2133 * If buffering is on, send when sent to children and buffer space is free.
2134 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2135 * the ACK itself goes "back" (towards root).
2137 * @param t Tunnel on which to send the ACK.
2138 * @param type Type of message that triggered the ACK transmission.
2141 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2146 /* Is it after unicast retransmission? */
2149 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2151 "ACK due to FWD DATA retransmission\n");
2152 if (GNUNET_YES == t->nobuffer)
2154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2157 if (GNUNET_YES == t->reliable && NULL != t->client)
2158 tunnel_send_fwd_data_ack (t);
2160 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2161 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2162 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2164 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2165 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2166 t->force_ack = GNUNET_YES;
2172 /* Check if we need to transmit the ACK */
2173 if (0 && NULL == t->owner &&
2174 t->queue_max > t->next_fc.queue_n * 4 &&
2175 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
2176 GNUNET_NO == t->force_ack)
2178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, buffer free\n");
2179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2180 " t->qmax: %u, t->qn: %u\n",
2181 t->queue_max, t->next_fc.queue_n);
2182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2183 " t->pid: %u, t->ack: %u\n",
2184 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
2188 /* Ok, ACK might be necessary, what PID to ACK? */
2189 delta = t->queue_max - t->next_fc.queue_n;
2190 if (0 > delta || (GNUNET_YES == t->reliable &&
2192 t->fwd_rel->n_sent > 10))
2194 if (NULL != t->owner && delta > 1)
2196 ack = t->prev_fc.last_pid_recv + delta;
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK %u\n", ack);
2198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2199 " last %u, qmax %u, q %u\n",
2200 t->prev_fc.last_pid_recv, t->queue_max, t->next_fc.queue_n);
2201 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2207 t->prev_fc.last_ack_sent = ack;
2208 if (NULL != t->owner)
2209 send_local_ack (t, t->owner, GNUNET_YES);
2210 else if (0 != t->prev_hop)
2211 send_ack (t, t->prev_hop, ack);
2215 t->force_ack = GNUNET_NO;
2220 * Send an ACK informing the children node/client about the available
2222 * If buffering is off, send only on behalf of root (can be self).
2223 * If buffering is on, send when sent to predecessor and buffer space is free.
2224 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2225 * the ACK itself goes "forward" (towards children/clients).
2227 * @param t Tunnel on which to send the ACK.
2228 * @param type Type of message that triggered the ACK transmission.
2231 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2235 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2236 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2237 /* Is it after data to_origin retransmission? */
2240 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2241 if (GNUNET_YES == t->nobuffer)
2243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2244 " Not sending ACK, nobuffer + traffic\n");
2247 if (GNUNET_YES == t->reliable && NULL != t->owner)
2248 tunnel_send_bck_data_ack (t);
2250 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2251 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2253 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2254 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2255 t->force_ack = GNUNET_YES;
2261 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2263 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2265 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2268 " Not sending ACK, not needed, last ack sent was %u\n",
2269 t->next_fc.last_ack_sent);
2272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2273 " Sending BCK ACK %u (last sent: %u)\n",
2274 ack, t->next_fc.last_ack_sent);
2275 t->next_fc.last_ack_sent = ack;
2277 if (NULL != t->client)
2278 send_local_ack (t, t->client, GNUNET_NO);
2279 else if (0 != t->next_hop)
2280 send_ack (t, t->next_hop, ack);
2283 t->force_ack = GNUNET_NO;
2288 * Modify the mesh message TID from global to local and send to client.
2290 * @param t Tunnel on which to send the message.
2291 * @param msg Message to modify and send.
2292 * @param c Client to send to.
2293 * @param tid Tunnel ID to use (c can be both owner and client).
2296 tunnel_send_client_data (struct MeshTunnel *t,
2297 const struct GNUNET_MESH_Data *msg,
2298 struct MeshClient *c, MESH_TunnelNumber tid)
2300 struct GNUNET_MESH_LocalData *copy;
2301 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2302 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2304 if (size < sizeof (struct GNUNET_MessageHeader))
2306 GNUNET_break_op (0);
2314 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2315 memcpy (©[1], &msg[1], size);
2316 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2317 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2318 copy->tid = htonl (tid);
2319 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2320 ©->header, GNUNET_NO);
2324 * Modify the unicast message TID from global to local and send to client.
2326 * @param t Tunnel on which to send the message.
2327 * @param msg Message to modify and send.
2330 tunnel_send_client_ucast (struct MeshTunnel *t,
2331 const struct GNUNET_MESH_Data *msg)
2333 tunnel_send_client_data (t, msg, t->client, t->local_tid_dest);
2338 * Send up to 64 buffered messages to the client for in order delivery.
2340 * @param t Tunnel on which to empty the message buffer.
2343 tunnel_send_client_buffered_ucast (struct MeshTunnel *t)
2345 struct MeshTunnelReliability *rel;
2346 struct MeshReliableMessage *copy;
2347 struct MeshReliableMessage *next;
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_unicast\n");
2351 for (copy = rel->head_recv; NULL != copy; copy = next)
2354 if (copy->mid == rel->mid_recv)
2356 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
2358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2359 " have %llu! now expecting %llu\n",
2360 copy->mid, rel->mid_recv + 1LL);
2361 tunnel_send_client_ucast (t, msg);
2363 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2369 " don't have %llu, (%llu)\n",
2375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_unicast END\n");
2380 * We have received a message out of order, buffer it until we receive
2381 * the missing one and we can feed the rest to the client.
2384 tunnel_add_buffer_ucast (struct MeshTunnel *t,
2385 const struct GNUNET_MESH_Data *msg)
2387 struct MeshTunnelReliability *rel;
2388 struct MeshReliableMessage *copy;
2389 struct MeshReliableMessage *prev;
2394 size = ntohs (msg->header.size);
2395 mid = GNUNET_ntohll (msg->mid);
2396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffer_ucast %llu\n", mid);
2398 copy = GNUNET_malloc (sizeof (*copy) + size);
2401 memcpy (©[1], msg, size);
2403 // FIXME do something better than O(n), although n < 64...
2404 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
2406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %llu\n", prev->mid);
2407 if (mid < prev->mid)
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
2410 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
2415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
2416 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
2417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffer_ucast END\n");
2422 * Mark future messages as ACK'd.
2424 * @param t Tunnel whose sent buffer to clean.
2425 * @param msg DataACK message with a bitfield of future ACK'd messages.
2428 tunnel_free_buffer_ucast (struct MeshTunnel *t,
2429 const struct GNUNET_MESH_DataACK *msg)
2431 struct MeshTunnelReliability *rel;
2432 struct MeshReliableMessage *copy;
2433 struct MeshReliableMessage *next;
2440 bitfield = msg->futures;
2441 mid = GNUNET_ntohll (msg->mid);
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2444 "free_sent_buffer %llu %llX\n",
2446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2447 " rel %p, head %p\n",
2448 rel, rel->head_sent);
2449 for (i = 0, copy = rel->head_sent;
2450 i < 64 && NULL != copy && 0 != bitfield;
2453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2454 " trying bit %u (mid %llu)\n",
2457 if (0 == (bitfield & mask))
2460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
2461 /* Bit was set, clear the bit from the bitfield */
2464 /* The i-th bit was set. Do we have that copy? */
2465 /* Skip copies with mid < target */
2466 target = mid + i + 1;
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %llu\n", target);
2468 while (NULL != copy && copy->mid < target)
2471 /* Did we run out of copies? (previously freed, it's ok) */
2474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
2478 /* Did we overshoot the target? (previously freed, it's ok) */
2479 if (copy->mid > target)
2481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %llu\n", copy->mid);
2485 /* Now copy->mid == target, free it */
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %llu\n", target);
2488 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_buffer END\n");
2497 * Modify the to_origin message TID from global to local and send to client.
2499 * @param t Tunnel on which to send the message.
2500 * @param msg Message to modify and send.
2503 tunnel_send_client_to_orig (struct MeshTunnel *t,
2504 const struct GNUNET_MESH_Data *msg)
2506 tunnel_send_client_data (t, msg, t->owner, t->local_tid);
2511 * We haven't received an ACK after a certain time: restransmit the message.
2513 * @param cls Closure (MeshReliableMessage with the message to restransmit)
2514 * @param tc TaskContext.
2517 tunnel_retransmit_message (void *cls,
2518 const struct GNUNET_SCHEDULER_TaskContext *tc)
2520 struct MeshTunnelReliability *rel = cls;
2521 struct MeshReliableMessage *copy;
2522 struct MeshFlowControl *fc;
2523 struct MeshPeerQueue *q;
2524 struct MeshPeerInfo *pi;
2525 struct MeshTunnel *t;
2526 struct GNUNET_MESH_Data *payload;
2529 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2530 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2534 copy = rel->head_sent;
2541 /* Search the message to be retransmitted in the outgoing queue */
2542 payload = (struct GNUNET_MESH_Data *) ©[1];
2543 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
2544 fc = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc;
2545 pi = peer_get_short (hop);
2546 for (q = pi->queue_head; NULL != q; q = q->next)
2548 if (ntohs (payload->header.type) == q->type)
2550 struct GNUNET_MESH_Data *queued_data = q->cls;
2552 if (queued_data->mid == payload->mid)
2557 /* Message not found in the queue */
2560 struct GNUNET_TIME_Relative diff;
2562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %llu\n", copy->mid);
2563 diff = GNUNET_TIME_absolute_get_duration (copy->timestamp);
2564 diff = GNUNET_TIME_relative_divide (diff, 10);
2565 copy->timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get(),
2568 fc->last_ack_sent++;
2569 payload->pid = htonl (fc->last_pid_recv + 1);
2570 fc->last_pid_recv++;
2571 send_prebuilt_message (&payload->header, hop, t);
2572 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! STILL IN QUEUE %llu\n", copy->mid);
2579 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
2580 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
2581 &tunnel_retransmit_message,
2587 * @brief Re-initiate traffic to this peer if necessary.
2589 * Check if there is traffic queued towards this peer
2590 * and the core transmit handle is NULL (traffic was stalled).
2591 * If so, call core tmt rdy.
2593 * @param peer_id Short ID of peer to which initiate traffic.
2596 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2598 struct MeshPeerInfo *peer;
2599 struct GNUNET_PeerIdentity id;
2600 struct MeshPeerQueue *q;
2603 peer = peer_get_short (peer_id);
2604 if (NULL != peer->core_transmit)
2607 q = queue_get_next (peer);
2610 /* Might br multicast traffic already sent to this particular peer but
2611 * not to other children in this tunnel.
2612 * This way t->queue_n would be > 0 but the queue of this particular peer
2618 GNUNET_PEER_resolve (peer->id, &id);
2619 peer->core_transmit =
2620 GNUNET_CORE_notify_transmit_ready(core_handle,
2623 GNUNET_TIME_UNIT_FOREVER_REL,
2633 * Send a message to all peers and clients in this tunnel that the tunnel
2634 * is no longer valid. If some peer or client should not receive the message,
2635 * should be zero'ed out before calling this function.
2637 * @param t The tunnel whose peers and clients to notify.
2640 tunnel_send_destroy (struct MeshTunnel *t)
2642 struct GNUNET_MESH_TunnelDestroy msg;
2643 struct GNUNET_PeerIdentity id;
2645 msg.header.size = htons (sizeof (msg));
2646 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2647 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2648 msg.tid = htonl (t->id.tid);
2649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2650 " sending tunnel destroy for tunnel: %s [%X]\n",
2651 GNUNET_i2s (&msg.oid), t->id.tid);
2653 if (NULL == t->client && 0 != t->next_hop)
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2656 GNUNET_PEER_resolve (t->next_hop, &id);
2657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2658 " sending forward to %s\n",
2660 send_prebuilt_message (&msg.header, t->next_hop, t);
2662 if (NULL == t->owner && 0 != t->prev_hop)
2664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2665 GNUNET_PEER_resolve (t->prev_hop, &id);
2666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2667 " sending back to %s\n",
2669 send_prebuilt_message (&msg.header, t->prev_hop, t);
2671 if (NULL != t->owner)
2673 send_client_tunnel_destroy (t->owner, t);
2675 if (NULL != t->client)
2677 send_client_tunnel_destroy (t->client, t);
2683 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2685 * @param t Tunnel which to cancel.
2686 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2689 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2691 struct MeshPeerInfo *peer_info;
2692 struct MeshPeerQueue *pq;
2693 struct MeshPeerQueue *next;
2696 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2697 peer_info = peer_get_short (neighbor);
2698 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2701 if (pq->tunnel == t)
2703 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2704 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2706 /* Should have been removed on destroy children */
2709 queue_destroy (pq, GNUNET_YES);
2712 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2714 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2715 peer_info->core_transmit = NULL;
2721 * Destroy the tunnel.
2723 * This function does not generate any warning traffic to clients or peers.
2726 * Remove the tunnel from peer_info's and clients' hashmaps.
2727 * Cancel messages belonging to this tunnel queued to neighbors.
2728 * Free any allocated resources linked to the tunnel.
2730 * @param t the tunnel to destroy
2732 * @return GNUNET_OK on success
2735 tunnel_destroy (struct MeshTunnel *t)
2737 struct MeshClient *c;
2738 struct GNUNET_HashCode hash;
2748 struct GNUNET_PeerIdentity id;
2750 GNUNET_PEER_resolve (t->id.oid, &id);
2751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2752 GNUNET_i2s (&id), t->id.tid);
2754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2758 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2759 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2768 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels, t->local_tid, t))
2775 if (NULL != t->client)
2779 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
2780 t->local_tid_dest, t))
2786 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
2787 t->local_tid_dest, t))
2794 if (0 != t->prev_hop)
2796 peer_cancel_queues (t->prev_hop, t);
2797 GNUNET_PEER_change_rc (t->prev_hop, -1);
2799 if (0 != t->next_hop)
2801 peer_cancel_queues (t->next_hop, t);
2802 GNUNET_PEER_change_rc (t->next_hop, -1);
2805 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2808 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2809 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2812 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2813 path_destroy (t->path);
2819 * Tunnel is empty: destroy it.
2821 * Notifies all participants (peers, cleints) about the destruction.
2823 * @param t Tunnel to destroy.
2826 tunnel_destroy_empty (struct MeshTunnel *t)
2830 struct GNUNET_PeerIdentity id;
2832 GNUNET_PEER_resolve (t->id.oid, &id);
2833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2834 "executing destruction of empty tunnel %s [%X]\n",
2835 GNUNET_i2s (&id), t->id.tid);
2839 if (GNUNET_NO == t->destroy)
2840 tunnel_send_destroy (t);
2841 if (0 == t->pending_messages)
2844 t->destroy = GNUNET_YES;
2848 * Initialize a Flow Control structure to the initial state.
2850 * @param fc Flow Control structure to initialize.
2853 fc_init (struct MeshFlowControl *fc)
2855 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2856 fc->last_pid_recv = (uint32_t) -1;
2857 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2858 fc->last_ack_recv = (uint32_t) -1;
2859 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2860 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2865 * Create a new tunnel
2867 * @param owner Who is the owner of the tunnel (short ID).
2868 * @param tid Tunnel Number of the tunnel.
2869 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2870 * @param local Tunnel Number for the tunnel, for the client point of view.
2872 * @return A new initialized tunnel. NULL on error.
2874 static struct MeshTunnel *
2875 tunnel_new (GNUNET_PEER_Id owner,
2876 MESH_TunnelNumber tid,
2877 struct MeshClient *client,
2878 MESH_TunnelNumber local)
2880 struct MeshTunnel *t;
2881 struct GNUNET_HashCode hash;
2883 if (n_tunnels >= max_tunnels && NULL == client)
2886 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2889 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2891 fc_init (&t->next_fc);
2892 fc_init (&t->prev_fc);
2893 t->local_tid = local;
2895 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2897 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2899 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2900 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2907 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2915 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
2917 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2921 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2931 * Set options in a tunnel, extracted from a bit flag field
2933 * @param t Tunnel to set options to.
2934 * @param options Bit array in host byte order.
2937 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
2939 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
2940 GNUNET_YES : GNUNET_NO;
2941 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
2942 GNUNET_YES : GNUNET_NO;
2947 * Iterator for deleting each tunnel whose client endpoint disconnected.
2949 * @param cls Closure (client that has disconnected).
2950 * @param key The local tunnel id (used to access the hashmap).
2951 * @param value The value stored at the key (tunnel to destroy).
2953 * @return GNUNET_OK, keep iterating.
2956 tunnel_destroy_iterator (void *cls,
2960 struct MeshTunnel *t = value;
2961 struct MeshClient *c = cls;
2963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2964 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2965 t->local_tid, t->local_tid_dest, c->id);
2966 client_delete_tunnel (c, t);
2969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2971 if (0 != t->next_hop) { /* destroy could come before a path is used */
2972 GNUNET_PEER_change_rc (t->next_hop, -1);
2978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2980 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2981 GNUNET_PEER_change_rc (t->prev_hop, -1);
2986 tunnel_destroy_empty (t);
2993 * Timeout function, destroys tunnel if called
2995 * @param cls Closure (tunnel to destroy).
2996 * @param tc TaskContext
2999 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3001 struct MeshTunnel *t = cls;
3002 struct GNUNET_PeerIdentity id;
3004 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3005 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3007 GNUNET_PEER_resolve(t->id.oid, &id);
3008 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3009 "Tunnel %s [%X] timed out. Destroying.\n",
3010 GNUNET_i2s(&id), t->id.tid);
3011 if (NULL != t->client)
3012 send_client_tunnel_destroy (t->client, t);
3013 tunnel_destroy (t); /* Do not notify other */
3018 * Resets the tunnel timeout. Starts it if no timeout was running.
3020 * @param t Tunnel whose timeout to reset.
3022 * TODO use heap to improve efficiency of scheduler.
3025 tunnel_reset_timeout (struct MeshTunnel *t)
3027 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
3029 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
3030 GNUNET_SCHEDULER_cancel (t->maintenance_task);
3031 t->maintenance_task =
3032 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3033 (refresh_path_time, 4), &tunnel_timeout, t);
3037 /******************************************************************************/
3038 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3039 /******************************************************************************/
3042 * Function to send a create path packet to a peer.
3044 * @param cls closure
3045 * @param size number of bytes available in buf
3046 * @param buf where the callee should write the message
3047 * @return number of bytes written to buf
3050 send_core_path_create (void *cls, size_t size, void *buf)
3052 struct MeshTunnel *t = cls;
3053 struct GNUNET_MESH_CreateTunnel *msg;
3054 struct GNUNET_PeerIdentity *peer_ptr;
3055 struct MeshPeerPath *p = t->path;
3060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3062 sizeof (struct GNUNET_MESH_CreateTunnel) +
3063 p->length * sizeof (struct GNUNET_PeerIdentity);
3065 if (size < size_needed || NULL == buf)
3070 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
3071 msg->header.size = htons (size_needed);
3072 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3073 msg->tid = ntohl (t->id.tid);
3076 if (GNUNET_YES == t->nobuffer)
3077 opt |= GNUNET_MESH_OPTION_NOBUFFER;
3078 if (GNUNET_YES == t->reliable)
3079 opt |= GNUNET_MESH_OPTION_RELIABLE;
3080 msg->opt = htonl (opt);
3081 msg->port = htonl (t->port);
3083 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3084 for (i = 0; i < p->length; i++)
3086 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3090 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3096 * Creates a path ack message in buf and frees all unused resources.
3098 * @param cls closure (MeshTransmissionDescriptor)
3099 * @param size number of bytes available in buf
3100 * @param buf where the callee should write the message
3101 * @return number of bytes written to buf
3104 send_core_path_ack (void *cls, size_t size, void *buf)
3106 struct MeshTunnel *t = cls;
3107 struct GNUNET_MESH_PathACK *msg = buf;
3109 GNUNET_assert (NULL != t);
3110 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3115 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
3116 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3117 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3118 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
3119 msg->tid = htonl (t->id.tid);
3120 msg->peer_id = my_full_id;
3121 msg->ack = htonl (t->prev_fc.last_ack_sent);
3123 /* TODO add signature */
3125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3126 return sizeof (struct GNUNET_MESH_PathACK);
3131 * Free a transmission that was already queued with all resources
3132 * associated to the request.
3134 * @param queue Queue handler to cancel.
3135 * @param clear_cls Is it necessary to free associated cls?
3138 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3140 struct MeshFlowControl *fc;
3142 if (GNUNET_YES == clear_cls)
3144 switch (queue->type)
3146 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3147 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3148 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3150 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3151 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3152 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3153 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3154 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3156 " prebuilt message\n");
3157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3159 GNUNET_MESH_DEBUG_M2S (queue->type));
3161 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3167 " type %s unknown!\n",
3168 GNUNET_MESH_DEBUG_M2S (queue->type));
3170 GNUNET_free_non_null (queue->cls);
3172 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3173 queue->peer->queue_tail,
3176 /* Delete from appropriate fc in the tunnel */
3177 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
3178 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
3180 if (queue->peer->id == queue->tunnel->prev_hop)
3181 fc = &queue->tunnel->prev_fc;
3182 else if (queue->peer->id == queue->tunnel->next_hop)
3183 fc = &queue->tunnel->next_fc;
3187 GNUNET_free (queue);
3192 GNUNET_free (queue);
3197 * @brief Get the next transmittable message from the queue.
3199 * This will be the head, except in the case of being a data packet
3200 * not allowed by the destination peer.
3202 * @param peer Destination peer.
3204 * @return The next viable MeshPeerQueue element to send to that peer.
3205 * NULL when there are no transmittable messages.
3207 struct MeshPeerQueue *
3208 queue_get_next (const struct MeshPeerInfo *peer)
3210 struct MeshPeerQueue *q;
3212 struct GNUNET_MESH_Data *dmsg;
3213 struct MeshTunnel* t;
3217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
3218 for (q = peer->queue_head; NULL != q; q = q->next)
3221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3223 GNUNET_MESH_DEBUG_M2S (q->type));
3224 dmsg = (struct GNUNET_MESH_Data *) q->cls;
3225 pid = ntohl (dmsg->pid);
3228 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3229 ack = t->next_fc.last_ack_recv;
3231 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3232 ack = t->prev_fc.last_ack_recv;
3235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3240 "* ACK: %u, PID: %u, MID: %llu\n",
3241 ack, pid, GNUNET_ntohll (dmsg->mid));
3242 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3255 "* nothing found\n");
3261 queue_send (void *cls, size_t size, void *buf)
3263 struct MeshPeerInfo *peer = cls;
3264 struct GNUNET_MessageHeader *msg;
3265 struct MeshPeerQueue *queue;
3266 struct MeshTunnel *t;
3267 struct GNUNET_PeerIdentity dst_id;
3268 struct MeshFlowControl *fc;
3273 peer->core_transmit = NULL;
3275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3276 queue = queue_get_next (peer);
3278 /* Queue has no internal mesh traffic nor sendable payload */
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3282 if (NULL == peer->queue_head)
3283 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3288 GNUNET_PEER_resolve (peer->id, &dst_id);
3289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3291 GNUNET_i2s (&dst_id));
3292 /* Check if buffer size is enough for the message */
3293 if (queue->size > size)
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3296 "* not enough room, reissue\n");
3297 peer->core_transmit =
3298 GNUNET_CORE_notify_transmit_ready (core_handle,
3301 GNUNET_TIME_UNIT_FOREVER_REL,
3308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3311 GNUNET_assert (0 < t->pending_messages);
3312 t->pending_messages--;
3316 switch (queue->type)
3319 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3320 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3321 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3322 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3323 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3324 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3325 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3326 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3329 GNUNET_MESH_DEBUG_M2S (queue->type));
3331 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3332 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3333 data_size = send_core_data_raw (queue->cls, size, buf);
3334 msg = (struct GNUNET_MessageHeader *) buf;
3335 type = ntohs (msg->type);
3337 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3339 data_size = send_core_path_create (queue->cls, size, buf);
3341 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3343 data_size = send_core_path_ack (queue->cls, size, buf);
3347 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3348 "* type unknown: %u\n",
3353 if (0 < drop_percent &&
3354 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3357 "Dropping message of type %s\n",
3358 GNUNET_MESH_DEBUG_M2S(queue->type));
3361 /* Free queue, but cls was freed by send_core_* */
3362 queue_destroy (queue, GNUNET_NO);
3364 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3365 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3369 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3370 t->next_fc.last_pid_sent = pid;
3371 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3373 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3374 t->prev_fc.last_pid_sent = pid;
3375 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3381 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3387 /* If more data in queue, send next */
3388 queue = queue_get_next (peer);
3391 struct GNUNET_PeerIdentity id;
3393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3394 GNUNET_PEER_resolve (peer->id, &id);
3395 peer->core_transmit =
3396 GNUNET_CORE_notify_transmit_ready(core_handle,
3399 GNUNET_TIME_UNIT_FOREVER_REL,
3405 else if (NULL != peer->queue_head)
3407 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3409 GNUNET_i2s (&my_full_id));
3410 if (peer->id == t->next_hop)
3412 else if (peer->id == t->prev_hop)
3419 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3422 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3432 * @brief Queue and pass message to core when possible.
3434 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3435 * accounts for it. In case the queue is full, the message is dropped and
3438 * Otherwise, message is treated as internal and allowed to go regardless of
3441 * @param cls Closure (@c type dependant). It will be used by queue_send to
3442 * build the message to be sent if not already prebuilt.
3443 * @param type Type of the message, 0 for a raw message.
3444 * @param size Size of the message.
3445 * @param dst Neighbor to send message to.
3446 * @param t Tunnel this message belongs to.
3449 queue_add (void *cls, uint16_t type, size_t size,
3450 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3452 struct MeshPeerQueue *queue;
3453 struct GNUNET_PeerIdentity id;
3454 struct MeshFlowControl *fc;
3458 priority = GNUNET_NO;
3459 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3463 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3469 if (fc->queue_n >= t->queue_max)
3471 /* If this isn't a retransmission, drop the message */
3472 if (GNUNET_NO == t->reliable ||
3473 (NULL == t->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) ||
3474 (NULL == t->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type))
3476 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3480 "queue full: %u/%u\n",
3481 fc->queue_n, t->queue_max);
3482 return; /* Drop this message */
3484 priority = GNUNET_YES;
3488 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3494 if (GNUNET_YES == priority)
3496 struct GNUNET_MESH_Data *d;
3500 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
3501 d = (struct GNUNET_MESH_Data *) queue->cls;
3503 for (queue = dst->queue_tail; NULL != queue; queue = queue->prev)
3505 if (queue->type != type)
3507 d = (struct GNUNET_MESH_Data *) queue->cls;
3514 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3515 if (NULL == dst->core_transmit)
3517 GNUNET_PEER_resolve (dst->id, &id);
3518 dst->core_transmit =
3519 GNUNET_CORE_notify_transmit_ready (core_handle,
3522 GNUNET_TIME_UNIT_FOREVER_REL,
3528 t->pending_messages++;
3532 /******************************************************************************/
3533 /******************** MESH NETWORK HANDLERS **************************/
3534 /******************************************************************************/
3538 * Core handler for path creation
3540 * @param cls closure
3541 * @param message message
3542 * @param peer peer identity this notification is about
3544 * @return GNUNET_OK to keep the connection open,
3545 * GNUNET_SYSERR to close it (signal serious error)
3548 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3549 const struct GNUNET_MessageHeader *message)
3551 unsigned int own_pos;
3554 MESH_TunnelNumber tid;
3555 struct GNUNET_MESH_CreateTunnel *msg;
3556 struct GNUNET_PeerIdentity *pi;
3557 struct MeshPeerPath *path;
3558 struct MeshPeerInfo *dest_peer_info;
3559 struct MeshPeerInfo *orig_peer_info;
3560 struct MeshTunnel *t;
3562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3563 "Received a path create msg [%s]\n",
3564 GNUNET_i2s (&my_full_id));
3565 size = ntohs (message->size);
3566 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3568 GNUNET_break_op (0);
3572 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3573 if (size % sizeof (struct GNUNET_PeerIdentity))
3575 GNUNET_break_op (0);
3578 size /= sizeof (struct GNUNET_PeerIdentity);
3581 GNUNET_break_op (0);
3584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3585 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3587 tid = ntohl (msg->tid);
3588 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3590 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3591 t = tunnel_get (pi, tid);
3592 if (NULL == t) /* might be a local tunnel */
3596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3597 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3603 t->port = ntohl (msg->port);
3604 opt = ntohl (msg->opt);
3605 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3607 t->nobuffer = GNUNET_YES;
3610 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3612 t->reliable = GNUNET_YES;
3614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3616 tunnel_reset_timeout (t);
3618 t->state = MESH_TUNNEL_WAITING;
3620 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3621 if (NULL == dest_peer_info)
3623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3624 " Creating PeerInfo for destination.\n");
3625 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3626 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3627 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3629 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3631 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3632 if (NULL == orig_peer_info)
3634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3635 " Creating PeerInfo for origin.\n");
3636 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3637 orig_peer_info->id = GNUNET_PEER_intern (pi);
3638 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3639 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3642 path = path_new (size);
3644 for (i = 0; i < size; i++)
3646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3647 GNUNET_i2s (&pi[i]));
3648 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3649 if (path->peers[i] == myid)
3652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3653 if (own_pos == 0 && path->peers[own_pos] != myid)
3655 /* create path: self not found in path through self */
3656 GNUNET_break_op (0);
3657 path_destroy (path);
3661 path_add_to_peers (path, GNUNET_NO);
3662 tunnel_use_path (t, path);
3664 peer_info_add_tunnel (dest_peer_info, t);
3666 if (own_pos == size - 1)
3668 struct MeshClient *c;
3670 /* Find target client */
3671 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
3674 /* TODO send reject */
3678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3679 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3681 /* Assign local tid */
3682 while (NULL != tunnel_get_incoming (next_local_tid))
3683 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3684 t->local_tid_dest = next_local_tid++;
3685 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3687 if (GNUNET_YES == t->reliable)
3689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
3690 t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
3692 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
3695 tunnel_add_client (t, c);
3696 send_client_tunnel_create (t);
3701 struct MeshPeerPath *path2;
3703 t->next_hop = path->peers[own_pos + 1];
3704 GNUNET_PEER_change_rc(t->next_hop, 1);
3706 /* It's for somebody else! Retransmit. */
3707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3708 path2 = path_duplicate (path);
3709 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3710 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3711 send_create_path (t);
3719 * Core handler for path ACKs
3721 * @param cls closure
3722 * @param message message
3723 * @param peer peer identity this notification is about
3725 * @return GNUNET_OK to keep the connection open,
3726 * GNUNET_SYSERR to close it (signal serious error)
3729 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3730 const struct GNUNET_MessageHeader *message)
3732 struct GNUNET_MESH_PathACK *msg;
3733 struct MeshPeerInfo *peer_info;
3734 struct MeshPeerPath *p;
3735 struct MeshTunnel *t;
3737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3738 GNUNET_i2s (&my_full_id));
3739 msg = (struct GNUNET_MESH_PathACK *) message;
3740 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3743 /* TODO notify that we don't know the tunnel */
3744 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3746 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3750 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3752 peer_info = peer_get (&msg->peer_id);
3753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3754 GNUNET_i2s (&msg->peer_id));
3755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3758 /* Add path to peers? */
3762 path_add_to_peers (p, GNUNET_YES);
3768 t->state = MESH_TUNNEL_READY;
3769 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3770 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3772 /* Message for us? */
3773 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3776 if (NULL == t->owner)
3778 GNUNET_break_op (0);
3781 if (NULL != peer_info->dhtget)
3783 GNUNET_DHT_get_stop (peer_info->dhtget);
3784 peer_info->dhtget = NULL;
3786 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3787 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3792 " not for us, retransmitting...\n");
3793 peer_info = peer_get (&msg->oid);
3794 send_prebuilt_message (message, t->prev_hop, t);
3800 * Core handler for notifications of broken paths
3802 * @param cls closure
3803 * @param message message
3804 * @param peer peer identity this notification is about
3806 * @return GNUNET_OK to keep the connection open,
3807 * GNUNET_SYSERR to close it (signal serious error)
3810 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3811 const struct GNUNET_MessageHeader *message)
3813 struct GNUNET_MESH_PathBroken *msg;
3814 struct MeshTunnel *t;
3816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3817 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3818 msg = (struct GNUNET_MESH_PathBroken *) message;
3819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3820 GNUNET_i2s (&msg->peer1));
3821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3822 GNUNET_i2s (&msg->peer2));
3823 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3826 GNUNET_break_op (0);
3829 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3830 GNUNET_PEER_search (&msg->peer2));
3837 * Core handler for tunnel destruction
3839 * @param cls closure
3840 * @param message message
3841 * @param peer peer identity this notification is about
3843 * @return GNUNET_OK to keep the connection open,
3844 * GNUNET_SYSERR to close it (signal serious error)
3847 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3848 const struct GNUNET_MessageHeader *message)
3850 struct GNUNET_MESH_TunnelDestroy *msg;
3851 struct MeshTunnel *t;
3853 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3855 "Got a TUNNEL DESTROY packet from %s\n",
3857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3858 " for tunnel %s [%u]\n",
3859 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3860 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3863 /* Probably already got the message from another path,
3864 * destroyed the tunnel and retransmitted to children.
3867 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3871 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3874 t->local_tid, t->local_tid_dest);
3876 if (GNUNET_PEER_search (peer) == t->prev_hop)
3878 // TODO check owner's signature
3879 // TODO add owner's signatue to tunnel for retransmission
3880 peer_cancel_queues (t->prev_hop, t);
3881 GNUNET_PEER_change_rc (t->prev_hop, -1);
3884 else if (GNUNET_PEER_search (peer) == t->next_hop)
3886 // TODO check dest's signature
3887 // TODO add dest's signatue to tunnel for retransmission
3888 peer_cancel_queues (t->next_hop, t);
3889 GNUNET_PEER_change_rc (t->next_hop, -1);
3894 GNUNET_break_op (0);
3895 // TODO check both owner AND destination's signature to see which matches
3896 // TODO restransmit in appropriate direction
3899 tunnel_destroy_empty (t);
3901 // TODO: add timeout to destroy the tunnel anyway
3907 * Core handler for mesh network traffic going from the origin to a peer
3909 * @param cls closure
3910 * @param peer peer identity this notification is about
3911 * @param message message
3912 * @return GNUNET_OK to keep the connection open,
3913 * GNUNET_SYSERR to close it (signal serious error)
3916 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3917 const struct GNUNET_MessageHeader *message)
3919 struct GNUNET_MESH_Data *msg;
3920 struct MeshTunnel *t;
3925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3928 size = ntohs (message->size);
3930 sizeof (struct GNUNET_MESH_Data) +
3931 sizeof (struct GNUNET_MessageHeader))
3936 msg = (struct GNUNET_MESH_Data *) message;
3937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3938 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3940 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3943 /* TODO notify back: we don't know this tunnel */
3944 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3945 GNUNET_break_op (0);
3948 pid = ntohl (msg->pid);
3949 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3951 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3952 GNUNET_break_op (0);
3953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3954 "Received PID %u, (prev %u), ACK %u\n",
3955 pid, t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
3956 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3960 tunnel_reset_timeout (t);
3961 if (t->dest == myid)
3963 /* TODO signature verification */
3964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3965 " it's for us! sending to clients...\n");
3966 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3967 // if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv)) FIXME use
3968 if (GMC_is_pid_bigger (pid, t->prev_fc.last_pid_recv))
3972 mid = GNUNET_ntohll (msg->mid);
3973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3974 " pid %u (%llu) not seen yet\n", pid, mid);
3975 t->prev_fc.last_pid_recv = pid;
3977 if (GNUNET_NO == t->reliable ||
3978 (mid >= t->bck_rel->mid_recv && mid <= t->bck_rel->mid_recv + 64))
3980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3981 "!!! RECV %llu\n", GNUNET_ntohll(msg->mid));
3982 if (GNUNET_YES == t->reliable)
3984 /* Is this the exact next expected messasge? */
3985 if (mid == t->bck_rel->mid_recv)
3987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
3988 t->bck_rel->mid_recv++;
3989 tunnel_send_client_ucast (t, msg);
3990 tunnel_send_client_buffered_ucast (t);
3994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
3995 tunnel_add_buffer_ucast (t, msg);
4001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4002 " MID %llu not expected (%llu - %llu), dropping!\n",
4003 GNUNET_ntohll (msg->mid),
4004 t->bck_rel->mid_recv, t->bck_rel->mid_recv + 64LL);
4009 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4011 " Pid %u not expected (%u), dropping!\n",
4012 pid, t->prev_fc.last_pid_recv + 1);
4014 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4017 t->prev_fc.last_pid_recv = pid;
4018 if (0 == t->next_hop)
4023 ttl = ntohl (msg->ttl);
4024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4027 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4028 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4029 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4033 " not for us, retransmitting...\n");
4035 send_prebuilt_message (message, t->next_hop, t);
4036 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4042 * Core handler for mesh network traffic toward the owner of a tunnel
4044 * @param cls closure
4045 * @param message message
4046 * @param peer peer identity this notification is about
4048 * @return GNUNET_OK to keep the connection open,
4049 * GNUNET_SYSERR to close it (signal serious error)
4052 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4053 const struct GNUNET_MessageHeader *message)
4055 struct GNUNET_MESH_Data *msg;
4056 struct MeshTunnel *t;
4061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4063 size = ntohs (message->size);
4064 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
4065 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4067 GNUNET_break_op (0);
4070 msg = (struct GNUNET_MESH_Data *) message;
4071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4072 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4073 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4074 pid = ntohl (msg->pid);
4077 /* TODO notify that we dont know this tunnel (whom)? */
4078 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4080 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
4081 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4085 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
4087 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
4088 GNUNET_break_op (0);
4089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4090 "Received PID %u, ACK %u\n",
4091 pid, t->next_fc.last_ack_sent);
4092 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4096 if (myid == t->id.oid)
4098 /* TODO signature verification */
4099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4100 " it's for us! sending to clients...\n");
4101 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4102 if ( (GNUNET_NO == t->reliable &&
4103 GMC_is_pid_bigger(pid, t->next_fc.last_pid_recv))
4105 (GNUNET_YES == t->reliable &&
4106 pid == t->next_fc.last_pid_recv + 1) ) // FIXME use "futures" as accepting
4108 t->next_fc.last_pid_recv = pid;
4109 tunnel_send_client_to_orig (t, msg);
4113 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
4114 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4115 " Pid %u not expected, sending FWD ACK!\n", pid);
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");
4122 t->next_fc.last_pid_recv = pid;
4123 if (0 == t->prev_hop) /* No owner AND no prev hop */
4125 if (GNUNET_YES == t->destroy)
4127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4128 "to orig received on a dying tunnel %s [%X]\n",
4129 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4133 "unknown to origin at %s\n",
4134 GNUNET_i2s (&my_full_id));
4135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4139 "on tunnel %s [%X]\n",
4140 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4143 ttl = ntohl (msg->ttl);
4144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4147 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4148 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4149 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4152 send_prebuilt_message (message, t->prev_hop, t);
4153 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4160 * Core handler for mesh network traffic end-to-end ACKs.
4162 * @param cls Closure.
4163 * @param message Message.
4164 * @param peer Peer identity this notification is about.
4166 * @return GNUNET_OK to keep the connection open,
4167 * GNUNET_SYSERR to close it (signal serious error)
4170 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4171 const struct GNUNET_MessageHeader *message)
4173 struct GNUNET_MESH_DataACK *msg;
4174 struct MeshTunnelReliability *rel;
4175 struct MeshReliableMessage *copy;
4176 struct MeshReliableMessage *next;
4177 struct MeshTunnel *t;
4183 type = ntohs (message->type);
4184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
4185 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4186 msg = (struct GNUNET_MESH_DataACK *) message;
4188 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4191 /* TODO notify that we dont know this tunnel (whom)? */
4192 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4195 ack = GNUNET_ntohll (msg->mid);
4196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %llu\n", ack);
4198 /* Is this a forward or backward ACK? */
4199 id = GNUNET_PEER_search (peer);
4200 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4203 if (NULL == t->owner)
4205 send_prebuilt_message (message, t->prev_hop, t);
4209 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4211 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4214 if (NULL == t->client)
4216 send_prebuilt_message (message, t->next_hop, t);
4220 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4224 GNUNET_break_op (0);
4228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %llu\n", ack);
4229 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4231 struct GNUNET_TIME_Relative time;
4233 if (copy->mid > ack)
4235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %llu, out!\n", copy->mid);
4236 tunnel_free_buffer_ucast (t, msg);
4240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %llu\n", copy->mid);
4242 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
4243 rel->expected_delay.rel_value += time.rel_value;
4244 rel->expected_delay.rel_value /= 2;
4246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
4247 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
4249 rel->retry_timer = rel->expected_delay;
4250 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
4254 if (GNUNET_YES == work)
4256 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4258 GNUNET_SCHEDULER_cancel (rel->retry_task);
4259 if (NULL == rel->head_sent)
4261 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4265 struct GNUNET_TIME_Absolute new_target;
4266 struct GNUNET_TIME_Relative delay;
4268 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4269 MESH_RETRANSMIT_MARGIN);
4270 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4272 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4274 GNUNET_SCHEDULER_add_delayed (delay,
4275 &tunnel_retransmit_message,
4281 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK);
4287 * Core handler for mesh network traffic point-to-point acks.
4289 * @param cls closure
4290 * @param message message
4291 * @param peer peer identity this notification is about
4293 * @return GNUNET_OK to keep the connection open,
4294 * GNUNET_SYSERR to close it (signal serious error)
4297 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4298 const struct GNUNET_MessageHeader *message)
4300 struct GNUNET_MESH_ACK *msg;
4301 struct MeshTunnel *t;
4302 struct MeshFlowControl *fc;
4306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4308 msg = (struct GNUNET_MESH_ACK *) message;
4310 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4314 /* TODO notify that we dont know this tunnel (whom)? */
4315 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4318 ack = ntohl (msg->pid);
4319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4321 /* Is this a forward or backward ACK? */
4322 id = GNUNET_PEER_search (peer);
4323 if (t->next_hop == id)
4326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4329 else if (t->prev_hop == id)
4332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4337 GNUNET_break_op (0);
4341 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4342 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4344 GNUNET_SCHEDULER_cancel (fc->poll_task);
4345 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4346 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4348 fc->last_ack_recv = ack;
4349 peer_unlock_queue (id);
4351 if (t->next_hop == id)
4352 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4354 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4361 * Core handler for mesh network traffic point-to-point ack polls.
4363 * @param cls closure
4364 * @param message message
4365 * @param peer peer identity this notification is about
4367 * @return GNUNET_OK to keep the connection open,
4368 * GNUNET_SYSERR to close it (signal serious error)
4371 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4372 const struct GNUNET_MessageHeader *message)
4374 struct GNUNET_MESH_Poll *msg;
4375 struct MeshTunnel *t;
4376 struct MeshFlowControl *fc;
4381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4384 msg = (struct GNUNET_MESH_Poll *) message;
4386 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4390 /* TODO notify that we dont know this tunnel (whom)? */
4391 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4392 GNUNET_break_op (0);
4396 /* Is this a forward or backward ACK? */
4397 id = GNUNET_PEER_search(peer);
4398 pid = ntohl (msg->pid);
4399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4400 if (t->next_hop == id)
4402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", t->next_fc.last_pid_recv);
4405 old = fc->last_pid_recv;
4406 fc->last_pid_recv = pid;
4407 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4409 else if (t->prev_hop == id)
4411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", t->prev_fc.last_pid_recv);
4414 old = fc->last_pid_recv;
4415 fc->last_pid_recv = pid;
4416 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4421 if (GNUNET_YES == t->reliable)
4422 fc->last_pid_recv = old;
4429 * Core handler for mesh keepalives.
4431 * @param cls closure
4432 * @param message message
4433 * @param peer peer identity this notification is about
4434 * @return GNUNET_OK to keep the connection open,
4435 * GNUNET_SYSERR to close it (signal serious error)
4437 * TODO: Check who we got this from, to validate route.
4440 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4441 const struct GNUNET_MessageHeader *message)
4443 struct GNUNET_MESH_TunnelKeepAlive *msg;
4444 struct MeshTunnel *t;
4446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4449 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4450 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4454 /* TODO notify that we dont know that tunnel */
4455 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4460 tunnel_reset_timeout (t);
4461 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4464 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4465 send_prebuilt_message (message, t->next_hop, t);
4472 * Functions to handle messages from core
4474 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4475 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4476 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4477 sizeof (struct GNUNET_MESH_PathBroken)},
4478 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4479 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4480 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4481 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4482 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4483 sizeof (struct GNUNET_MESH_DataACK)},
4484 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4485 sizeof (struct GNUNET_MESH_DataACK)},
4486 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4487 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4488 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4489 sizeof (struct GNUNET_MESH_ACK)},
4490 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4491 sizeof (struct GNUNET_MESH_Poll)},
4492 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4493 sizeof (struct GNUNET_MESH_PathACK)},
4499 /******************************************************************************/
4500 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4501 /******************************************************************************/
4506 * notify_client_connection_failure: notify a client that the connection to the
4507 * requested remote peer is not possible (for instance, no route found)
4508 * Function called when the socket is ready to queue more data. "buf" will be
4509 * NULL and "size" zero if the socket was closed for writing in the meantime.
4511 * @param cls closure
4512 * @param size number of bytes available in buf
4513 * @param buf where the callee should write the message
4514 * @return number of bytes written to buf
4517 notify_client_connection_failure (void *cls, size_t size, void *buf)
4520 struct MeshPeerInfo *peer_info;
4521 struct GNUNET_MESH_PeerControl *msg;
4522 struct GNUNET_PeerIdentity id;
4524 if (0 == size && NULL == buf)
4526 // TODO retry? cancel?
4530 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4531 peer_info = (struct MeshPeerInfo *) cls;
4532 msg = (struct GNUNET_MESH_PeerControl *) buf;
4533 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4534 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4535 // msg->tunnel_id = htonl(peer_info->t->tid);
4536 GNUNET_PEER_resolve (peer_info->id, &id);
4537 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4545 * Send keepalive packets for a tunnel.
4547 * @param cls Closure (tunnel for which to send the keepalive).
4548 * @param tc Notification context.
4550 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4553 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4555 struct MeshTunnel *t = cls;
4556 struct GNUNET_MESH_TunnelKeepAlive *msg;
4557 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4560 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4561 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4562 NULL == t->owner || 0 == t->local_tid)
4567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4568 "sending keepalive for tunnel %d\n", t->id.tid);
4570 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4571 msg->header.size = htons (size);
4572 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4573 msg->oid = my_full_id;
4574 msg->tid = htonl (t->id.tid);
4575 send_prebuilt_message (&msg->header, t->next_hop, t);
4577 t->maintenance_task =
4578 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4583 * Function to process paths received for a new peer addition. The recorded
4584 * paths form the initial tunnel, which can be optimized later.
4585 * Called on each result obtained for the DHT search.
4587 * @param cls closure
4588 * @param exp when will this value expire
4589 * @param key key of the result
4590 * @param get_path path of the get request
4591 * @param get_path_length lenght of get_path
4592 * @param put_path path of the put request
4593 * @param put_path_length length of the put_path
4594 * @param type type of the result
4595 * @param size number of bytes in data
4596 * @param data pointer to the result data
4599 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4600 const struct GNUNET_HashCode * key,
4601 const struct GNUNET_PeerIdentity *get_path,
4602 unsigned int get_path_length,
4603 const struct GNUNET_PeerIdentity *put_path,
4604 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4605 size_t size, const void *data)
4607 struct MeshPeerInfo *peer = cls;
4608 struct MeshPeerPath *p;
4609 struct GNUNET_PeerIdentity pi;
4612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4613 GNUNET_PEER_resolve (peer->id, &pi);
4614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4616 p = path_build_from_dht (get_path, get_path_length,
4617 put_path, put_path_length);
4618 path_add_to_peers (p, GNUNET_NO);
4620 for (i = 0; i < peer->ntunnels; i++)
4622 struct GNUNET_PeerIdentity id;
4624 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4626 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4627 peer->tunnels[i]->local_tid,
4628 peer->tunnels[i]->local_tid_dest);
4629 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4632 peer_connect (peer, peer->tunnels[i]);
4640 /******************************************************************************/
4641 /********************* MESH LOCAL HANDLES **************************/
4642 /******************************************************************************/
4646 * Handler for client connection.
4648 * @param cls Closure (unused).
4649 * @param client Client handler.
4652 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
4654 struct MeshClient *c;
4658 c = GNUNET_malloc (sizeof (struct MeshClient));
4660 GNUNET_SERVER_client_keep (client);
4661 GNUNET_SERVER_client_set_user_context (client, c);
4662 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4667 * Handler for client disconnection
4669 * @param cls closure
4670 * @param client identification of the client; NULL
4671 * for the last call when the server is destroyed
4674 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4676 struct MeshClient *c;
4677 struct MeshClient *next;
4679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4686 c = client_get (client);
4689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4691 GNUNET_SERVER_client_drop (c->handle);
4692 c->shutting_down = GNUNET_YES;
4693 if (NULL != c->own_tunnels)
4695 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4696 &tunnel_destroy_iterator, c);
4697 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4700 if (NULL != c->incoming_tunnels)
4702 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4703 &tunnel_destroy_iterator, c);
4704 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4707 if (NULL != c->ports)
4708 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4710 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4713 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4718 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
4720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4726 * Handler for new clients
4728 * @param cls closure
4729 * @param client identification of the client
4730 * @param message the actual message, which includes messages the client wants
4733 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4734 const struct GNUNET_MessageHeader *message)
4736 struct GNUNET_MESH_ClientConnect *cc_msg;
4737 struct MeshClient *c;
4742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4744 /* Check data sanity */
4745 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4746 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4747 if (0 != (size % sizeof (uint32_t)))
4750 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4753 size /= sizeof (uint32_t);
4755 /* Initialize new client structure */
4756 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
4757 c->id = next_client_id++; /* overflow not important: just for debug */
4758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4764 p = (uint32_t *) &cc_msg[1];
4765 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4766 for (i = 0; i < size; i++)
4769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4771 /* store in client's hashmap */
4772 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4773 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4774 /* store in global hashmap */
4775 /* FIXME only allow one client to have the port open,
4776 * have a backup hashmap with waiting clients */
4777 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4778 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4782 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4783 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4784 GNUNET_SERVER_notification_context_add (nc, client);
4785 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4787 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4793 * Handler for requests of new tunnels
4795 * @param cls Closure.
4796 * @param client Identification of the client.
4797 * @param message The actual message.
4800 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4801 const struct GNUNET_MessageHeader *message)
4803 struct GNUNET_MESH_TunnelMessage *t_msg;
4804 struct MeshPeerInfo *peer_info;
4805 struct MeshTunnel *t;
4806 struct MeshClient *c;
4807 MESH_TunnelNumber tid;
4809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4811 /* Sanity check for client registration */
4812 if (NULL == (c = client_get (client)))
4815 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4820 /* Message size sanity check */
4821 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4824 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4828 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4830 GNUNET_i2s (&t_msg->peer));
4831 /* Sanity check for tunnel numbering */
4832 tid = ntohl (t_msg->tunnel_id);
4833 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4836 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4839 /* Sanity check for duplicate tunnel IDs */
4840 if (NULL != tunnel_get_by_local_id (c, tid))
4843 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4848 while (NULL != tunnel_get_by_pi (myid, next_tid))
4849 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4850 t = tunnel_new (myid, next_tid, c, tid);
4851 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4855 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4858 t->port = ntohl (t_msg->port);
4859 tunnel_set_options (t, ntohl (t_msg->opt));
4860 if (GNUNET_YES == t->reliable)
4862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4863 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
4865 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
4868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4869 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4871 peer_info = peer_get (&t_msg->peer);
4872 peer_info_add_tunnel (peer_info, t);
4873 peer_connect (peer_info, t);
4874 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4880 * Handler for requests of deleting tunnels
4882 * @param cls closure
4883 * @param client identification of the client
4884 * @param message the actual message
4887 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4888 const struct GNUNET_MessageHeader *message)
4890 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4891 struct MeshClient *c;
4892 struct MeshTunnel *t;
4893 MESH_TunnelNumber tid;
4895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4896 "Got a DESTROY TUNNEL from client!\n");
4898 /* Sanity check for client registration */
4899 if (NULL == (c = client_get (client)))
4902 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4907 /* Message sanity check */
4908 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4911 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4915 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4917 /* Retrieve tunnel */
4918 tid = ntohl (tunnel_msg->tunnel_id);
4919 t = tunnel_get_by_local_id(c, tid);
4922 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4924 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4928 /* Cleanup after the tunnel */
4929 client_delete_tunnel (c, t);
4936 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4940 /* The tunnel will be destroyed when the last message is transmitted. */
4941 tunnel_destroy_empty (t);
4943 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4949 * Handler for client traffic
4951 * @param cls closure
4952 * @param client identification of the client
4953 * @param message the actual message
4956 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
4957 const struct GNUNET_MessageHeader *message)
4959 struct GNUNET_MESH_LocalData *data_msg;
4960 struct MeshClient *c;
4961 struct MeshTunnel *t;
4962 struct MeshFlowControl *fc;
4963 MESH_TunnelNumber tid;
4966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4967 "Got data from a client!\n");
4969 /* Sanity check for client registration */
4970 if (NULL == (c = client_get (client)))
4973 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4978 data_msg = (struct GNUNET_MESH_LocalData *) message;
4980 /* Sanity check for message size */
4981 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
4982 if (size < sizeof (struct GNUNET_MessageHeader))
4985 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4989 /* Tunnel exists? */
4990 tid = ntohl (data_msg->tid);
4991 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
4994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4997 t = tunnel_get_by_local_id (c, tid);
5001 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5005 /* Is the client in the tunnel? */
5006 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5008 t->owner->handle == client)
5010 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5012 t->client->handle == client) ) )
5015 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5019 /* Ok, everything is correct, send the message
5020 * (pretend we got it from a mesh peer)
5023 struct GNUNET_MESH_Data *payload;
5024 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
5026 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
5027 if (GNUNET_YES == t->reliable)
5029 struct MeshTunnelReliability *rel;
5030 struct MeshReliableMessage *copy;
5032 rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
5033 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
5034 + sizeof(struct GNUNET_MESH_Data)
5036 copy->mid = rel->mid_sent++;
5037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %llu\n", copy->mid);
5038 copy->timestamp = GNUNET_TIME_absolute_get ();
5041 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
5042 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
5045 GNUNET_TIME_relative_multiply (rel->expected_delay,
5046 MESH_RETRANSMIT_MARGIN);
5048 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
5049 &tunnel_retransmit_message,
5052 payload = (struct GNUNET_MESH_Data *) ©[1];
5053 payload->mid = GNUNET_htonll (copy->mid);
5057 payload = (struct GNUNET_MESH_Data *) cbuf;
5059 // FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
5060 // use different struct for unreliable traffic, save 8 bytes
5062 memcpy (&payload[1], &data_msg[1], size);
5063 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
5064 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
5065 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
5066 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5067 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
5068 payload->tid = htonl (t->id.tid);
5069 payload->ttl = htonl (default_ttl);
5070 payload->pid = htonl (fc->last_pid_recv + 1);
5071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5072 " calling generic handler...\n");
5073 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5074 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
5076 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
5078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5079 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5086 * Handler for client's ACKs for payload traffic.
5088 * @param cls Closure (unused).
5089 * @param client Identification of the client.
5090 * @param message The actual message.
5093 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5094 const struct GNUNET_MessageHeader *message)
5096 struct GNUNET_MESH_LocalAck *msg;
5097 struct MeshTunnel *t;
5098 struct MeshClient *c;
5099 MESH_TunnelNumber tid;
5101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5102 /* Sanity check for client registration */
5103 if (NULL == (c = client_get (client)))
5106 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5111 msg = (struct GNUNET_MESH_LocalAck *) message;
5113 /* Tunnel exists? */
5114 tid = ntohl (msg->tunnel_id);
5115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5116 t = tunnel_get_by_local_id (c, tid);
5120 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5121 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5122 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5126 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5129 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5130 t->prev_fc.last_ack_recv++;
5131 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5135 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5136 t->next_fc.last_ack_recv++;
5137 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5140 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5148 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5150 * @param cls Closure (client handle).
5151 * @param key Key (hashed tunnel ID, unused).
5152 * @param value Tunnel info.
5154 * @return GNUNET_YES, to keep iterating.
5157 monitor_all_tunnels_iterator (void *cls,
5158 const struct GNUNET_HashCode * key,
5161 struct GNUNET_SERVER_Client *client = cls;
5162 struct MeshTunnel *t = value;
5163 struct GNUNET_MESH_LocalMonitor *msg;
5165 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5166 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5167 msg->tunnel_id = htonl (t->id.tid);
5168 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5169 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5170 GNUNET_PEER_resolve (t->dest, &msg->destination);
5172 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5173 "* sending info about tunnel %s [%u]\n",
5174 GNUNET_i2s (&msg->owner), t->id.tid);
5176 GNUNET_SERVER_notification_context_unicast (nc, client,
5177 &msg->header, GNUNET_NO);
5183 * Handler for client's MONITOR request.
5185 * @param cls Closure (unused).
5186 * @param client Identification of the client.
5187 * @param message The actual message.
5190 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5191 const struct GNUNET_MessageHeader *message)
5193 struct MeshClient *c;
5195 /* Sanity check for client registration */
5196 if (NULL == (c = client_get (client)))
5199 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5203 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5204 "Received get tunnels request from client %u\n",
5206 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5207 monitor_all_tunnels_iterator,
5209 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5210 "Get tunnels request from client %u completed\n",
5212 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5217 * Handler for client's MONITOR_TUNNEL request.
5219 * @param cls Closure (unused).
5220 * @param client Identification of the client.
5221 * @param message The actual message.
5224 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5225 const struct GNUNET_MessageHeader *message)
5227 const struct GNUNET_MESH_LocalMonitor *msg;
5228 struct GNUNET_MESH_LocalMonitor *resp;
5229 struct MeshClient *c;
5230 struct MeshTunnel *t;
5232 /* Sanity check for client registration */
5233 if (NULL == (c = client_get (client)))
5236 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5240 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5241 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5242 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5245 ntohl (msg->tunnel_id));
5246 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5249 /* We don't know the tunnel FIXME */
5250 struct GNUNET_MESH_LocalMonitor warn;
5253 GNUNET_SERVER_notification_context_unicast (nc, client,
5256 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5260 /* Initialize context */
5261 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5263 GNUNET_PEER_resolve (t->dest, &resp->destination);
5264 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5265 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5266 &resp->header, GNUNET_NO);
5269 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5270 "Monitor tunnel request from client %u completed\n",
5272 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5277 * Functions to handle messages from clients
5279 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5280 {&handle_local_new_client, NULL,
5281 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5282 {&handle_local_tunnel_create, NULL,
5283 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5284 sizeof (struct GNUNET_MESH_TunnelMessage)},
5285 {&handle_local_tunnel_destroy, NULL,
5286 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5287 sizeof (struct GNUNET_MESH_TunnelMessage)},
5288 {&handle_local_data, NULL,
5289 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5290 {&handle_local_ack, NULL,
5291 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5292 sizeof (struct GNUNET_MESH_LocalAck)},
5293 {&handle_local_get_tunnels, NULL,
5294 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5295 sizeof (struct GNUNET_MessageHeader)},
5296 {&handle_local_show_tunnel, NULL,
5297 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5298 sizeof (struct GNUNET_MESH_LocalMonitor)},
5304 * Method called whenever a given peer connects.
5306 * @param cls closure
5307 * @param peer peer identity this notification is about
5310 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5312 struct MeshPeerInfo *peer_info;
5313 struct MeshPeerPath *path;
5315 DEBUG_CONN ("Peer connected\n");
5316 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5317 peer_info = peer_get (peer);
5318 if (myid == peer_info->id)
5320 DEBUG_CONN (" (self)\n");
5321 path = path_new (1);
5325 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5326 path = path_new (2);
5327 path->peers[1] = peer_info->id;
5328 GNUNET_PEER_change_rc (peer_info->id, 1);
5329 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5331 path->peers[0] = myid;
5332 GNUNET_PEER_change_rc (myid, 1);
5333 peer_info_add_path (peer_info, path, GNUNET_YES);
5339 * Method called whenever a peer disconnects.
5341 * @param cls closure
5342 * @param peer peer identity this notification is about
5345 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5347 struct MeshPeerInfo *pi;
5348 struct MeshPeerQueue *q;
5349 struct MeshPeerQueue *n;
5351 DEBUG_CONN ("Peer disconnected\n");
5352 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5362 /* TODO try to reroute this traffic instead */
5363 queue_destroy(q, GNUNET_YES);
5366 if (NULL != pi->core_transmit)
5368 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5369 pi->core_transmit = NULL;
5371 peer_remove_path (pi, pi->id, myid);
5374 DEBUG_CONN (" (self)\n");
5376 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5382 * Install server (service) handlers and start listening to clients.
5387 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5388 GNUNET_SERVER_connect_notify (server_handle,
5389 &handle_local_client_connect, NULL);
5390 GNUNET_SERVER_disconnect_notify (server_handle,
5391 &handle_local_client_disconnect, NULL);
5392 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5394 clients_head = NULL;
5395 clients_tail = NULL;
5397 GNUNET_SERVER_resume (server_handle);
5402 * To be called on core init/fail.
5404 * @param cls Closure (config)
5405 * @param server handle to the server for this service
5406 * @param identity the public identity of this peer
5409 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5410 const struct GNUNET_PeerIdentity *identity)
5412 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5416 GNUNET_break (core_handle == server);
5417 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5423 GNUNET_i2s (identity));
5424 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5426 GNUNET_i2s (&my_full_id));
5427 GNUNET_CORE_disconnect (core_handle);
5428 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5429 NULL, /* Closure passed to MESH functions */
5430 &core_init, /* Call core_init once connected */
5431 &core_connect, /* Handle connects */
5432 &core_disconnect, /* remove peers on disconnects */
5433 NULL, /* Don't notify about all incoming messages */
5434 GNUNET_NO, /* For header only in notification */
5435 NULL, /* Don't notify about all outbound messages */
5436 GNUNET_NO, /* For header-only out notification */
5437 core_handlers); /* Register these handlers */
5446 /******************************************************************************/
5447 /************************ MAIN FUNCTIONS ****************************/
5448 /******************************************************************************/
5451 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5453 * @param cls closure
5454 * @param key current key code
5455 * @param value value in the hash map
5456 * @return GNUNET_YES if we should continue to iterate,
5460 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5462 struct MeshTunnel *t = value;
5469 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5471 * @param cls closure
5472 * @param key current key code
5473 * @param value value in the hash map
5474 * @return GNUNET_YES if we should continue to iterate,
5478 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5480 struct MeshPeerInfo *p = value;
5481 struct MeshPeerQueue *q;
5482 struct MeshPeerQueue *n;
5490 queue_destroy(q, GNUNET_YES);
5494 peer_info_destroy (p);
5500 * Task run during shutdown.
5506 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5510 if (core_handle != NULL)
5512 GNUNET_CORE_disconnect (core_handle);
5515 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5516 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5517 if (dht_handle != NULL)
5519 GNUNET_DHT_disconnect (dht_handle);
5524 GNUNET_SERVER_notification_context_destroy (nc);
5527 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5529 GNUNET_SCHEDULER_cancel (announce_id_task);
5530 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5537 * Process mesh requests.
5539 * @param cls closure
5540 * @param server the initialized server
5541 * @param c configuration to use
5544 run (void *cls, struct GNUNET_SERVER_Handle *server,
5545 const struct GNUNET_CONFIGURATION_Handle *c)
5548 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5551 server_handle = server;
5552 GNUNET_SERVER_suspend (server_handle);
5555 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5558 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5560 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5561 "mesh", "peer/privatekey");
5562 GNUNET_SCHEDULER_shutdown ();
5567 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5568 &refresh_path_time))
5570 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5572 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5573 "mesh", "refresh path time");
5574 GNUNET_SCHEDULER_shutdown ();
5579 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5582 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5584 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5585 "mesh", "id announce time");
5586 GNUNET_SCHEDULER_shutdown ();
5591 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5594 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5596 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5597 "mesh", "connect timeout");
5598 GNUNET_SCHEDULER_shutdown ();
5603 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5606 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5608 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5609 "mesh", "max msgs queue");
5610 GNUNET_SCHEDULER_shutdown ();
5615 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5618 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5620 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5621 "mesh", "max tunnels");
5622 GNUNET_SCHEDULER_shutdown ();
5627 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5630 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5632 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5633 "mesh", "default ttl", 64);
5638 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5641 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5642 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5643 "mesh", "max peers", 1000);
5648 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5655 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5656 "Mesh is running with drop mode enabled. "
5657 "This is NOT a good idea! "
5658 "Remove the DROP_PERCENT option from your configuration.\n");
5662 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5663 &dht_replication_level))
5665 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5667 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5668 "mesh", "dht replication level", 3);
5669 dht_replication_level = 3;
5672 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5673 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5674 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5675 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5677 dht_handle = GNUNET_DHT_connect (c, 64);
5678 if (NULL == dht_handle)
5682 stats = GNUNET_STATISTICS_create ("mesh", c);
5684 /* Scheduled the task to clean up when shutdown is called */
5685 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5687 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5688 GNUNET_free (keyfile);
5689 GNUNET_assert (NULL != pk);
5690 my_private_key = pk;
5691 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5692 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5693 &my_full_id.hashPubKey);
5694 myid = GNUNET_PEER_intern (&my_full_id);
5695 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5696 "Mesh for peer [%s] starting\n",
5697 GNUNET_i2s(&my_full_id));
5699 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5700 NULL, /* Closure passed to MESH functions */
5701 &core_init, /* Call core_init once connected */
5702 &core_connect, /* Handle connects */
5703 &core_disconnect, /* remove peers on disconnects */
5704 NULL, /* Don't notify about all incoming messages */
5705 GNUNET_NO, /* For header only in notification */
5706 NULL, /* Don't notify about all outbound messages */
5707 GNUNET_NO, /* For header-only out notification */
5708 core_handlers); /* Register these handlers */
5709 if (NULL == core_handle)
5712 GNUNET_SCHEDULER_shutdown ();
5716 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5717 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5723 * The main function for the mesh service.
5725 * @param argc number of arguments from the command line
5726 * @param argv command line arguments
5727 * @return 0 ok, 1 on error
5730 main (int argc, char *const *argv)
5735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5736 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5738 ret = (GNUNET_OK == r) ? 0 : 1;
5739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5743 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5744 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5745 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);