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 * Select which PID to POLL for, to compensate for lost messages.
1154 * @param pi Peer we want to poll.
1155 * @param t Tunnel about which we want to poll.
1157 * @return PID to use, either last sent or first_in_queue - 1
1160 peer_get_first_payload_pid (struct MeshPeerInfo *pi, struct MeshTunnel *t)
1162 struct MeshPeerQueue *q;
1165 type = pi->id == t->next_hop ? GNUNET_MESSAGE_TYPE_MESH_UNICAST :
1166 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN;
1168 for (q = pi->queue_head; NULL != q; q = q->next)
1170 if (q->type == type && q->tunnel == t)
1172 struct GNUNET_MESH_Data *msg = q->cls;
1174 /* Pretend that the last one sent was the previous to this */
1175 return ntohl (msg->pid) - 1;
1179 /* No data in queue, use last sent */
1181 struct MeshFlowControl *fc;
1183 fc = pi->id == t->next_hop ? &t->next_fc : &t->prev_fc;
1184 return fc->last_pid_sent;
1190 * Choose the best path towards a peer considering the tunnel properties.
1192 * @param peer The destination peer.
1193 * @param t The tunnel the path is for.
1195 * @return Best current known path towards the peer, if any.
1197 static struct MeshPeerPath *
1198 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1200 struct MeshPeerPath *best_p;
1201 struct MeshPeerPath *p;
1202 unsigned int best_cost;
1205 best_p = p = peer->path_head;
1206 best_cost = cost = p->length;
1209 if ((cost = p->length) < best_cost)
1220 * Core callback to write a pre-constructed data packet to core buffer
1222 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1223 * @param size Number of bytes available in buf.
1224 * @param buf Where the to write the message.
1226 * @return number of bytes written to buf
1229 send_core_data_raw (void *cls, size_t size, void *buf)
1231 struct GNUNET_MessageHeader *msg = cls;
1234 GNUNET_assert (NULL != msg);
1235 total_size = ntohs (msg->size);
1237 if (total_size > size)
1242 memcpy (buf, msg, total_size);
1249 * Sends an already built message to a peer, properly registrating
1250 * all used resources.
1252 * @param message Message to send. Function makes a copy of it.
1253 * @param peer Short ID of the neighbor whom to send the message.
1254 * @param t Tunnel on which this message is transmitted.
1257 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1258 GNUNET_PEER_Id peer,
1259 struct MeshTunnel *t)
1261 struct GNUNET_PeerIdentity id;
1262 struct MeshPeerInfo *neighbor;
1263 struct MeshPeerPath *p;
1268 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1273 size = ntohs (message->size);
1274 data = GNUNET_malloc (size);
1275 memcpy (data, message, size);
1276 type = ntohs(message->type);
1277 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1278 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1280 struct GNUNET_MESH_Data *u;
1282 u = (struct GNUNET_MESH_Data *) data;
1283 u->ttl = htonl (ntohl (u->ttl) - 1);
1285 GNUNET_PEER_resolve (peer, &id);
1286 neighbor = peer_get (&id);
1287 for (p = neighbor->path_head; NULL != p; p = p->next)
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1298 " %s IS NOT DIRECTLY CONNECTED\n",
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 for (p = neighbor->path_head; NULL != p; p = p->next)
1305 struct GNUNET_PeerIdentity debug_id;
1308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1309 " path with %u hops through:\n",
1311 for (i = 0; i < p->length; i++)
1313 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1316 i, GNUNET_i2s(&debug_id));
1320 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1322 " no direct connection to %s\n",
1327 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1338 * Sends a CREATE PATH message for a path to a peer, properly registrating
1339 * all used resources.
1341 * @param t Tunnel for which the path is created.
1344 send_create_path (struct MeshTunnel *t)
1346 struct MeshPeerInfo *neighbor;
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1349 neighbor = peer_get_short (t->next_hop);
1351 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1352 sizeof (struct GNUNET_MESH_CreateTunnel) +
1353 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1356 t->state = MESH_TUNNEL_WAITING;
1361 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1363 * @param t Tunnel which to confirm.
1366 send_path_ack (struct MeshTunnel *t)
1368 struct MeshPeerInfo *peer;
1370 peer = peer_get_short (t->prev_hop);
1373 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1374 sizeof (struct GNUNET_MESH_PathACK),
1381 * Try to establish a new connection to this peer in the given tunnel.
1382 * If the peer doesn't have any path to it yet, try to get one.
1383 * If the peer already has some path, send a CREATE PATH towards it.
1385 * @param peer PeerInfo of the peer.
1386 * @param t Tunnel for which to create the path, if possible.
1389 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1391 struct MeshPeerPath *p;
1393 if (NULL != peer->path_head)
1395 p = peer_get_best_path (peer, t);
1396 tunnel_use_path (t, p);
1397 send_create_path (t);
1399 else if (NULL == peer->dhtget)
1401 struct GNUNET_PeerIdentity id;
1403 GNUNET_PEER_resolve (peer->id, &id);
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1406 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1407 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1408 &id.hashPubKey, /* key to search */
1409 dht_replication_level, /* replication level */
1410 GNUNET_DHT_RO_RECORD_ROUTE |
1411 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1413 0, /* xquery bits */
1414 &dht_get_id_handler, peer);
1415 t->state = MESH_TUNNEL_SEARCHING;
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1420 "There is no path but the DHT GET is already started.\n");
1426 * Destroy the peer_info and free any allocated resources linked to it
1428 * @param pi The peer_info to destroy.
1430 * @return GNUNET_OK on success
1433 peer_info_destroy (struct MeshPeerInfo *pi)
1435 struct GNUNET_PeerIdentity id;
1436 struct MeshPeerPath *p;
1437 struct MeshPeerPath *nextp;
1440 GNUNET_PEER_resolve (pi->id, &id);
1441 GNUNET_PEER_change_rc (pi->id, -1);
1444 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1447 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1448 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1450 if (NULL != pi->dhtget)
1452 GNUNET_DHT_get_stop (pi->dhtget);
1458 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1462 for (i = 0; i < pi->ntunnels; i++)
1463 tunnel_destroy_empty (pi->tunnels[i]);
1464 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1471 * Remove all paths that rely on a direct connection between p1 and p2
1472 * from the peer itself and notify all tunnels about it.
1474 * @param peer PeerInfo of affected peer.
1475 * @param p1 GNUNET_PEER_Id of one peer.
1476 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1479 * TODO: optimize (see below)
1482 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1485 struct MeshPeerPath *p;
1486 struct MeshPeerPath *next;
1487 struct MeshPeerInfo *peer_d;
1489 unsigned int destroyed;
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1494 for (p = peer->path_head; NULL != p; p = next)
1497 for (i = 0; i < (p->length - 1); i++)
1499 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1500 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1502 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1512 for (i = 0; i < peer->ntunnels; i++)
1514 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1518 peer_d = peer_get_short (d);
1519 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1520 tunnel_use_path (peer->tunnels[i], next);
1521 peer_connect (peer_d, peer->tunnels[i]);
1523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1528 * Add the path to the peer and update the path used to reach it in case this
1531 * @param peer_info Destination peer to add the path to.
1532 * @param path New path to add. Last peer must be the peer in arg 1.
1533 * Path will be either used of freed if already known.
1534 * @param trusted Do we trust that this path is real?
1537 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1540 struct MeshPeerPath *aux;
1544 if ((NULL == peer_info) || (NULL == path))
1547 path_destroy (path);
1550 if (path->peers[path->length - 1] != peer_info->id)
1553 path_destroy (path);
1556 if (2 >= path->length && GNUNET_NO == trusted)
1558 /* Only allow CORE to tell us about direct paths */
1559 path_destroy (path);
1562 for (l = 1; l < path->length; l++)
1564 if (path->peers[l] == myid)
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1567 for (l2 = 0; l2 < path->length - l; l2++)
1569 path->peers[l2] = path->peers[l + l2];
1574 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1579 struct GNUNET_PeerIdentity id;
1581 GNUNET_PEER_resolve (peer_info->id, &id);
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1583 path->length, GNUNET_i2s (&id));
1586 l = path_get_length (path);
1589 path_destroy (path);
1593 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1594 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1596 l2 = path_get_length (aux);
1599 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1600 peer_info->path_tail, aux, path);
1605 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1607 path_destroy (path);
1612 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1619 * Add the path to the origin peer and update the path used to reach it in case
1620 * this is the shortest.
1621 * The path is given in peer_info -> destination, therefore we turn the path
1622 * upside down first.
1624 * @param peer_info Peer to add the path to, being the origin of the path.
1625 * @param path New path to add after being inversed.
1626 * Path will be either used or freed.
1627 * @param trusted Do we trust that this path is real?
1630 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1631 struct MeshPeerPath *path, int trusted)
1634 peer_info_add_path (peer_info, path, trusted);
1639 * Add a tunnel to the list of tunnels a peer participates in.
1640 * Update the tunnel's destination.
1642 * @param p Peer to add to.
1643 * @param t Tunnel to add.
1646 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1650 GNUNET_break (t->dest == p->id);
1654 GNUNET_PEER_change_rc (t->dest, 1);
1655 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1660 * Remove a tunnel from the list of tunnels a peer participates in.
1661 * Free the tunnel's destination.
1663 * @param p Peer to clean.
1664 * @param t Tunnel to remove.
1667 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1671 if (t->dest == p->id)
1673 GNUNET_PEER_change_rc (t->dest, -1);
1676 for (i = 0; i < p->ntunnels; i++)
1678 if (p->tunnels[i] == t)
1680 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1681 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1689 * Function called if the connection to the peer has been stalled for a while,
1690 * possibly due to a missed ACK. Poll the peer about its ACK status.
1692 * @param cls Closure (poll ctx).
1693 * @param tc TaskContext.
1696 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1698 struct MeshFlowControl *fc = cls;
1699 struct GNUNET_MESH_Poll msg;
1700 struct MeshTunnel *t = fc->t;
1701 GNUNET_PEER_Id peer;
1703 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1704 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1711 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1713 if (fc == &t->prev_fc)
1715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
1718 else if (fc == &t->next_fc)
1720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
1728 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1729 msg.header.size = htons (sizeof (msg));
1730 msg.tid = htonl (t->id.tid);
1731 msg.pid = htonl (peer_get_first_payload_pid (peer_get_short (peer), t));
1732 send_prebuilt_message (&msg.header, peer, t);
1733 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1734 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1740 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1741 * to obtain a local peer -> destination path and interning the peer ids.
1743 * @return Newly allocated and created path
1745 static struct MeshPeerPath *
1746 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1747 unsigned int get_path_length,
1748 const struct GNUNET_PeerIdentity *put_path,
1749 unsigned int put_path_length)
1751 struct MeshPeerPath *p;
1757 GNUNET_PEER_change_rc (myid, 1);
1758 i = get_path_length;
1759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1760 for (i--; i >= 0; i--)
1762 id = GNUNET_PEER_intern (&get_path[i]);
1763 if (p->length > 0 && id == p->peers[p->length - 1])
1765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1766 GNUNET_PEER_change_rc (id, -1);
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1771 GNUNET_i2s (&get_path[i]));
1773 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1774 p->peers[p->length - 1] = id;
1777 i = put_path_length;
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1779 for (i--; i >= 0; i--)
1781 id = GNUNET_PEER_intern (&put_path[i]);
1784 /* PUT path went through us, so discard the path up until now and start
1785 * from here to get a much shorter (and loop-free) path.
1790 if (p->length > 0 && id == p->peers[p->length - 1])
1792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1793 GNUNET_PEER_change_rc (id, -1);
1797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1798 GNUNET_i2s (&put_path[i]));
1800 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1801 p->peers[p->length - 1] = id;
1805 if (get_path_length > 0)
1806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1807 GNUNET_i2s (&get_path[0]));
1808 if (put_path_length > 0)
1809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1810 GNUNET_i2s (&put_path[0]));
1811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1813 for (i = 0; i < p->length; i++)
1815 struct GNUNET_PeerIdentity peer_id;
1817 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1819 GNUNET_i2s (&peer_id));
1827 * Adds a path to the peer_infos of all the peers in the path
1829 * @param p Path to process.
1830 * @param confirmed Whether we know if the path works or not.
1833 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1837 /* TODO: invert and add */
1838 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1839 for (i++; i < p->length; i++)
1841 struct MeshPeerInfo *aux;
1842 struct MeshPeerPath *copy;
1844 aux = peer_get_short (p->peers[i]);
1845 copy = path_duplicate (p);
1846 copy->length = i + 1;
1847 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1853 * Send keepalive packets for a peer
1855 * @param cls Closure (tunnel for which to send the keepalive).
1856 * @param tc Notification context.
1859 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1863 * Search for a tunnel among the incoming tunnels
1865 * @param tid the local id of the tunnel
1867 * @return tunnel handler, NULL if doesn't exist
1869 static struct MeshTunnel *
1870 tunnel_get_incoming (MESH_TunnelNumber tid)
1872 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1873 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
1878 * Search for a tunnel among the tunnels for a client
1880 * @param c the client whose tunnels to search in
1881 * @param tid the local id of the tunnel
1883 * @return tunnel handler, NULL if doesn't exist
1885 static struct MeshTunnel *
1886 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1888 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1890 return tunnel_get_incoming (tid);
1894 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI);
1895 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
1901 * Search for a tunnel by global ID using PEER_ID
1903 * @param pi owner of the tunnel
1904 * @param tid global tunnel number
1906 * @return tunnel handler, NULL if doesn't exist
1908 static struct MeshTunnel *
1909 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1911 struct MESH_TunnelID id;
1912 struct GNUNET_HashCode hash;
1917 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1918 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1923 * Search for a tunnel by global ID using full PeerIdentities
1925 * @param oid owner of the tunnel
1926 * @param tid global tunnel number
1928 * @return tunnel handler, NULL if doesn't exist
1930 static struct MeshTunnel *
1931 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1933 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1938 * Add a client to a tunnel, initializing all needed data structures.
1940 * @param t Tunnel to which add the client.
1941 * @param c Client which to add to the tunnel.
1944 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1946 if (NULL != t->client)
1952 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
1953 t->local_tid_dest, t,
1954 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1960 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
1961 t->local_tid_dest, t,
1962 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1972 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1974 unsigned int own_pos;
1976 for (own_pos = 0; own_pos < p->length; own_pos++)
1978 if (p->peers[own_pos] == myid)
1981 if (own_pos > p->length - 1)
1987 if (own_pos < p->length - 1)
1988 t->next_hop = p->peers[own_pos + 1];
1990 t->next_hop = p->peers[own_pos];
1991 GNUNET_PEER_change_rc (t->next_hop, 1);
1993 t->prev_hop = p->peers[own_pos - 1];
1995 t->prev_hop = p->peers[0];
1996 GNUNET_PEER_change_rc (t->prev_hop, 1);
1998 if (NULL != t->path)
1999 path_destroy (t->path);
2000 t->path = path_duplicate (p);
2003 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2004 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2005 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
2012 * Notifies a tunnel that a connection has broken that affects at least
2013 * some of its peers. Sends a notification towards the root of the tree.
2014 * In case the peer is the owner of the tree, notifies the client that owns
2015 * the tunnel and tries to reconnect.
2017 * @param t Tunnel affected.
2018 * @param p1 Peer that got disconnected from p2.
2019 * @param p2 Peer that got disconnected from p1.
2021 * @return Short ID of the peer disconnected (either p1 or p2).
2022 * 0 if the tunnel remained unaffected.
2024 static GNUNET_PEER_Id
2025 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2028 // if (myid != p1 && myid != p2) FIXME
2033 // if (tree_get_predecessor (t->tree) != 0)
2035 // /* We are the peer still connected, notify owner of the disconnection. */
2036 // struct GNUNET_MESH_PathBroken msg;
2037 // struct GNUNET_PeerIdentity neighbor;
2039 // msg.header.size = htons (sizeof (msg));
2040 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2041 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2042 // msg.tid = htonl (t->id.tid);
2043 // msg.peer1 = my_full_id;
2044 // GNUNET_PEER_resolve (pid, &msg.peer2);
2045 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2046 // send_prebuilt_message (&msg.header, &neighbor, t);
2053 * Build a local ACK message and send it to a local client.
2055 * @param t Tunnel on which to send the ACK.
2056 * @param c Client to whom send the ACK.
2057 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
2060 send_local_ack (struct MeshTunnel *t,
2061 struct MeshClient *c,
2064 struct GNUNET_MESH_LocalAck msg;
2066 msg.header.size = htons (sizeof (msg));
2067 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2068 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
2069 GNUNET_SERVER_notification_context_unicast (nc,
2076 * Build an ACK message and queue it to send to the given peer.
2078 * @param t Tunnel on which to send the ACK.
2079 * @param peer Peer to whom send the ACK.
2080 * @param ack Value of the ACK.
2083 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
2085 struct GNUNET_MESH_ACK msg;
2087 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2088 msg.header.size = htons (sizeof (msg));
2089 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2090 msg.pid = htonl (ack);
2091 msg.tid = htonl (t->id.tid);
2093 send_prebuilt_message (&msg.header, peer, t);
2098 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2100 * @param t Tunnel this is about.
2103 tunnel_send_fwd_data_ack (struct MeshTunnel *t)
2105 struct GNUNET_MESH_DataACK msg;
2106 struct MeshTunnelReliability *rel;
2107 struct MeshReliableMessage *copy;
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2112 "send_fwd_data_ack for %llu\n",
2113 t->bck_rel->mid_recv - 1);
2115 if (GNUNET_NO == t->reliable)
2117 GNUNET_break_op (0);
2120 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK);
2121 msg.header.size = htons (sizeof (msg));
2122 msg.tid = htonl (t->id.tid);
2123 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2124 msg.mid = GNUNET_htonll (t->bck_rel->mid_recv - 1);
2127 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2129 delta = copy->mid - t->bck_rel->mid_recv;
2132 mask = 0x1LL << delta;
2133 msg.futures |= mask;
2134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2135 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2136 copy->mid, delta, mask, msg.futures);
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2140 send_prebuilt_message (&msg.header, t->prev_hop, t);
2141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_fwd_data_ack END\n");
2146 * Send an end-to-end BCK ACK message for the most recent in-sequence payload.
2148 * @param t Tunnel this is about.
2151 tunnel_send_bck_data_ack (struct MeshTunnel *t)
2153 struct GNUNET_MESH_DataACK msg;
2155 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2156 msg.header.size = htons (sizeof (msg));
2157 msg.tid = htonl (t->id.tid);
2158 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2159 msg.mid = GNUNET_htonll (t->fwd_rel->mid_recv - 1);
2160 msg.futures = 0; // FIXME set bits of other newer messages received
2162 send_prebuilt_message (&msg.header, t->next_hop, t);
2167 * Send an ACK informing the predecessor about the available buffer space.
2168 * In case there is no predecessor, inform the owning client.
2169 * If buffering is off, send only on behalf of children or self if endpoint.
2170 * If buffering is on, send when sent to children and buffer space is free.
2171 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2172 * the ACK itself goes "back" (towards root).
2174 * @param t Tunnel on which to send the ACK.
2175 * @param type Type of message that triggered the ACK transmission.
2178 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2180 struct MeshTunnelReliability *rel = t->fwd_rel;
2185 /* Is it after unicast retransmission? */
2188 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2190 "ACK due to FWD DATA retransmission\n");
2191 if (GNUNET_YES == t->nobuffer)
2193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2196 if (GNUNET_YES == t->reliable && NULL != t->client)
2197 tunnel_send_fwd_data_ack (t);
2199 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2200 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2201 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2203 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2204 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2205 t->force_ack = GNUNET_YES;
2211 /* Check if we need to transmit the ACK */
2212 if (0 && NULL == t->owner &&
2213 t->queue_max > t->next_fc.queue_n * 4 &&
2214 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
2215 GNUNET_NO == t->force_ack)
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, buffer free\n");
2218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2219 " t->qmax: %u, t->qn: %u\n",
2220 t->queue_max, t->next_fc.queue_n);
2221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2222 " t->pid: %u, t->ack: %u\n",
2223 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
2227 /* Ok, ACK might be necessary, what PID to ACK? */
2228 delta = t->queue_max - t->next_fc.queue_n;
2229 if (NULL != t->owner && GNUNET_YES == t->reliable && NULL != rel->head_sent)
2230 delta_mid = rel->mid_sent - rel->head_sent->mid;
2233 if (0 > delta || (GNUNET_YES == t->reliable &&
2235 (rel->n_sent > 10 || delta_mid > 64)))
2237 if (NULL != t->owner && delta > 1)
2239 ack = t->prev_fc.last_pid_recv + delta;
2240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK %u\n", ack);
2241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2242 " last pid %u, last ack %u, qmax %u, q %u\n",
2243 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent,
2244 t->queue_max, t->next_fc.queue_n);
2245 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
2247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2251 t->prev_fc.last_ack_sent = ack;
2252 if (NULL != t->owner)
2253 send_local_ack (t, t->owner, GNUNET_YES);
2254 else if (0 != t->prev_hop)
2255 send_ack (t, t->prev_hop, ack);
2259 t->force_ack = GNUNET_NO;
2264 * Send an ACK informing the children node/client about the available
2266 * If buffering is off, send only on behalf of root (can be self).
2267 * If buffering is on, send when sent to predecessor and buffer space is free.
2268 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2269 * the ACK itself goes "forward" (towards children/clients).
2271 * @param t Tunnel on which to send the ACK.
2272 * @param type Type of message that triggered the ACK transmission.
2275 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2279 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2280 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2281 /* Is it after data to_origin retransmission? */
2284 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2285 if (GNUNET_YES == t->nobuffer)
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2288 " Not sending ACK, nobuffer + traffic\n");
2291 if (GNUNET_YES == t->reliable && NULL != t->owner)
2292 tunnel_send_bck_data_ack (t);
2294 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2295 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2297 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2298 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2299 t->force_ack = GNUNET_YES;
2305 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2307 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2309 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2312 " Not sending ACK, not needed, last ack sent was %u\n",
2313 t->next_fc.last_ack_sent);
2316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2317 " Sending BCK ACK %u (last sent: %u)\n",
2318 ack, t->next_fc.last_ack_sent);
2319 t->next_fc.last_ack_sent = ack;
2321 if (NULL != t->client)
2322 send_local_ack (t, t->client, GNUNET_NO);
2323 else if (0 != t->next_hop)
2324 send_ack (t, t->next_hop, ack);
2327 t->force_ack = GNUNET_NO;
2332 * Modify the mesh message TID from global to local and send to client.
2334 * @param t Tunnel on which to send the message.
2335 * @param msg Message to modify and send.
2336 * @param c Client to send to.
2337 * @param tid Tunnel ID to use (c can be both owner and client).
2340 tunnel_send_client_data (struct MeshTunnel *t,
2341 const struct GNUNET_MESH_Data *msg,
2342 struct MeshClient *c, MESH_TunnelNumber tid)
2344 struct GNUNET_MESH_LocalData *copy;
2345 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2346 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2348 if (size < sizeof (struct GNUNET_MessageHeader))
2350 GNUNET_break_op (0);
2358 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2359 memcpy (©[1], &msg[1], size);
2360 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2361 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2362 copy->tid = htonl (tid);
2363 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2364 ©->header, GNUNET_NO);
2368 * Modify the unicast message TID from global to local and send to client.
2370 * @param t Tunnel on which to send the message.
2371 * @param msg Message to modify and send.
2374 tunnel_send_client_ucast (struct MeshTunnel *t,
2375 const struct GNUNET_MESH_Data *msg)
2377 tunnel_send_client_data (t, msg, t->client, t->local_tid_dest);
2382 * Send up to 64 buffered messages to the client for in order delivery.
2384 * @param t Tunnel on which to empty the message buffer.
2387 tunnel_send_client_buffered_ucast (struct MeshTunnel *t)
2389 struct MeshTunnelReliability *rel;
2390 struct MeshReliableMessage *copy;
2391 struct MeshReliableMessage *next;
2393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_unicast\n");
2395 for (copy = rel->head_recv; NULL != copy; copy = next)
2398 if (copy->mid == rel->mid_recv)
2400 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
2402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2403 " have %llu! now expecting %llu\n",
2404 copy->mid, rel->mid_recv + 1LL);
2405 tunnel_send_client_ucast (t, msg);
2407 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2413 " don't have %llu, (%llu)\n",
2419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_unicast END\n");
2424 * We have received a message out of order, buffer it until we receive
2425 * the missing one and we can feed the rest to the client.
2428 tunnel_add_buffer_ucast (struct MeshTunnel *t,
2429 const struct GNUNET_MESH_Data *msg)
2431 struct MeshTunnelReliability *rel;
2432 struct MeshReliableMessage *copy;
2433 struct MeshReliableMessage *prev;
2438 size = ntohs (msg->header.size);
2439 mid = GNUNET_ntohll (msg->mid);
2440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffer_ucast %llu\n", mid);
2442 copy = GNUNET_malloc (sizeof (*copy) + size);
2445 memcpy (©[1], msg, size);
2447 // FIXME do something better than O(n), although n < 64...
2448 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
2450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %llu\n", prev->mid);
2451 if (mid < prev->mid)
2453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
2454 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
2459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
2460 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
2461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffer_ucast END\n");
2466 * Destroy a reliable message after it has been acknowledged, either by
2467 * direct mid ACK or bitfield. Updates the appropriate data structures and
2468 * timers and frees all memory.
2470 * @param copy Message that is no longer needed: remote peer got it.
2473 tunnel_free_reliable_message (struct MeshReliableMessage *copy)
2475 struct MeshTunnelReliability *rel;
2476 struct GNUNET_TIME_Relative time;
2479 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
2480 rel->expected_delay.rel_value *= 7;
2481 rel->expected_delay.rel_value += time.rel_value;
2482 rel->expected_delay.rel_value /= 8;
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %llu\n", copy->mid);
2485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
2487 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
2488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
2489 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
2491 rel->retry_timer = rel->expected_delay;
2492 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2498 * Mark future messages as ACK'd.
2500 * @param t Tunnel whose sent buffer to clean.
2501 * @param msg DataACK message with a bitfield of future ACK'd messages.
2504 tunnel_free_buffer_ucast (struct MeshTunnel *t,
2505 const struct GNUNET_MESH_DataACK *msg)
2507 struct MeshTunnelReliability *rel;
2508 struct MeshReliableMessage *copy;
2509 struct MeshReliableMessage *next;
2516 bitfield = msg->futures;
2517 mid = GNUNET_ntohll (msg->mid);
2519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2520 "free_sent_buffer %llu %llX\n",
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2523 " rel %p, head %p\n",
2524 rel, rel->head_sent);
2525 for (i = 0, copy = rel->head_sent;
2526 i < 64 && NULL != copy && 0 != bitfield;
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2530 " trying bit %u (mid %llu)\n",
2533 if (0 == (bitfield & mask))
2536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
2537 /* Bit was set, clear the bit from the bitfield */
2540 /* The i-th bit was set. Do we have that copy? */
2541 /* Skip copies with mid < target */
2542 target = mid + i + 1;
2543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %llu\n", target);
2544 while (NULL != copy && copy->mid < target)
2547 /* Did we run out of copies? (previously freed, it's ok) */
2550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
2554 /* Did we overshoot the target? (previously freed, it's ok) */
2555 if (copy->mid > target)
2557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %llu\n", copy->mid);
2561 /* Now copy->mid == target, free it */
2563 tunnel_free_reliable_message (copy);
2566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_buffer END\n");
2571 * Modify the to_origin message TID from global to local and send to client.
2573 * @param t Tunnel on which to send the message.
2574 * @param msg Message to modify and send.
2577 tunnel_send_client_to_orig (struct MeshTunnel *t,
2578 const struct GNUNET_MESH_Data *msg)
2580 tunnel_send_client_data (t, msg, t->owner, t->local_tid);
2585 * We haven't received an ACK after a certain time: restransmit the message.
2587 * @param cls Closure (MeshReliableMessage with the message to restransmit)
2588 * @param tc TaskContext.
2591 tunnel_retransmit_message (void *cls,
2592 const struct GNUNET_SCHEDULER_TaskContext *tc)
2594 struct MeshTunnelReliability *rel = cls;
2595 struct MeshReliableMessage *copy;
2596 struct MeshFlowControl *fc;
2597 struct MeshPeerQueue *q;
2598 struct MeshPeerInfo *pi;
2599 struct MeshTunnel *t;
2600 struct GNUNET_MESH_Data *payload;
2603 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2604 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2608 copy = rel->head_sent;
2615 /* Search the message to be retransmitted in the outgoing queue */
2616 payload = (struct GNUNET_MESH_Data *) ©[1];
2617 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
2618 fc = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc;
2619 pi = peer_get_short (hop);
2620 for (q = pi->queue_head; NULL != q; q = q->next)
2622 if (ntohs (payload->header.type) == q->type)
2624 struct GNUNET_MESH_Data *queued_data = q->cls;
2626 if (queued_data->mid == payload->mid)
2631 /* Message not found in the queue */
2634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %llu\n", copy->mid);
2636 fc->last_ack_sent++;
2637 fc->last_pid_recv++;
2638 payload->pid = htonl (fc->last_pid_recv);
2639 send_prebuilt_message (&payload->header, hop, t);
2640 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! STILL IN QUEUE %llu\n", copy->mid);
2647 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
2648 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
2649 &tunnel_retransmit_message,
2655 * @brief Re-initiate traffic to this peer if necessary.
2657 * Check if there is traffic queued towards this peer
2658 * and the core transmit handle is NULL (traffic was stalled).
2659 * If so, call core tmt rdy.
2661 * @param peer_id Short ID of peer to which initiate traffic.
2664 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2666 struct MeshPeerInfo *peer;
2667 struct GNUNET_PeerIdentity id;
2668 struct MeshPeerQueue *q;
2671 peer = peer_get_short (peer_id);
2672 if (NULL != peer->core_transmit)
2675 q = queue_get_next (peer);
2678 /* Might br multicast traffic already sent to this particular peer but
2679 * not to other children in this tunnel.
2680 * This way t->queue_n would be > 0 but the queue of this particular peer
2686 GNUNET_PEER_resolve (peer->id, &id);
2687 peer->core_transmit =
2688 GNUNET_CORE_notify_transmit_ready(core_handle,
2691 GNUNET_TIME_UNIT_FOREVER_REL,
2701 * Send a message to all peers and clients in this tunnel that the tunnel
2702 * is no longer valid. If some peer or client should not receive the message,
2703 * should be zero'ed out before calling this function.
2705 * @param t The tunnel whose peers and clients to notify.
2708 tunnel_send_destroy (struct MeshTunnel *t)
2710 struct GNUNET_MESH_TunnelDestroy msg;
2711 struct GNUNET_PeerIdentity id;
2713 msg.header.size = htons (sizeof (msg));
2714 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2715 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2716 msg.tid = htonl (t->id.tid);
2717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2718 " sending tunnel destroy for tunnel: %s [%X]\n",
2719 GNUNET_i2s (&msg.oid), t->id.tid);
2721 if (NULL == t->client && 0 != t->next_hop)
2723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2724 GNUNET_PEER_resolve (t->next_hop, &id);
2725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2726 " sending forward to %s\n",
2728 send_prebuilt_message (&msg.header, t->next_hop, t);
2730 if (NULL == t->owner && 0 != t->prev_hop)
2732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2733 GNUNET_PEER_resolve (t->prev_hop, &id);
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2735 " sending back to %s\n",
2737 send_prebuilt_message (&msg.header, t->prev_hop, t);
2739 if (NULL != t->owner)
2741 send_client_tunnel_destroy (t->owner, t);
2743 if (NULL != t->client)
2745 send_client_tunnel_destroy (t->client, t);
2751 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2753 * @param t Tunnel which to cancel.
2754 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2757 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2759 struct MeshPeerInfo *peer_info;
2760 struct MeshPeerQueue *pq;
2761 struct MeshPeerQueue *next;
2764 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2765 peer_info = peer_get_short (neighbor);
2766 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2769 if (pq->tunnel == t)
2771 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2772 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2774 /* Should have been removed on destroy children */
2777 queue_destroy (pq, GNUNET_YES);
2780 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2782 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2783 peer_info->core_transmit = NULL;
2789 * Destroy the tunnel.
2791 * This function does not generate any warning traffic to clients or peers.
2794 * Remove the tunnel from peer_info's and clients' hashmaps.
2795 * Cancel messages belonging to this tunnel queued to neighbors.
2796 * Free any allocated resources linked to the tunnel.
2798 * @param t the tunnel to destroy
2800 * @return GNUNET_OK on success
2803 tunnel_destroy (struct MeshTunnel *t)
2805 struct MeshClient *c;
2806 struct GNUNET_HashCode hash;
2816 struct GNUNET_PeerIdentity id;
2818 GNUNET_PEER_resolve (t->id.oid, &id);
2819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2820 GNUNET_i2s (&id), t->id.tid);
2822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2826 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2827 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2836 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels, t->local_tid, t))
2843 if (NULL != t->client)
2847 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
2848 t->local_tid_dest, t))
2854 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
2855 t->local_tid_dest, t))
2862 if (0 != t->prev_hop)
2864 peer_cancel_queues (t->prev_hop, t);
2865 GNUNET_PEER_change_rc (t->prev_hop, -1);
2867 if (0 != t->next_hop)
2869 peer_cancel_queues (t->next_hop, t);
2870 GNUNET_PEER_change_rc (t->next_hop, -1);
2873 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2876 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2877 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2880 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2881 path_destroy (t->path);
2887 * Tunnel is empty: destroy it.
2889 * Notifies all participants (peers, cleints) about the destruction.
2891 * @param t Tunnel to destroy.
2894 tunnel_destroy_empty (struct MeshTunnel *t)
2898 struct GNUNET_PeerIdentity id;
2900 GNUNET_PEER_resolve (t->id.oid, &id);
2901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2902 "executing destruction of empty tunnel %s [%X]\n",
2903 GNUNET_i2s (&id), t->id.tid);
2907 if (GNUNET_NO == t->destroy)
2908 tunnel_send_destroy (t);
2909 if (0 == t->pending_messages)
2912 t->destroy = GNUNET_YES;
2916 * Initialize a Flow Control structure to the initial state.
2918 * @param fc Flow Control structure to initialize.
2921 fc_init (struct MeshFlowControl *fc)
2923 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2924 fc->last_pid_recv = (uint32_t) -1;
2925 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2926 fc->last_ack_recv = (uint32_t) -1;
2927 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2928 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2933 * Create a new tunnel
2935 * @param owner Who is the owner of the tunnel (short ID).
2936 * @param tid Tunnel Number of the tunnel.
2937 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2938 * @param local Tunnel Number for the tunnel, for the client point of view.
2940 * @return A new initialized tunnel. NULL on error.
2942 static struct MeshTunnel *
2943 tunnel_new (GNUNET_PEER_Id owner,
2944 MESH_TunnelNumber tid,
2945 struct MeshClient *client,
2946 MESH_TunnelNumber local)
2948 struct MeshTunnel *t;
2949 struct GNUNET_HashCode hash;
2951 if (n_tunnels >= max_tunnels && NULL == client)
2954 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2957 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2959 fc_init (&t->next_fc);
2960 fc_init (&t->prev_fc);
2961 t->local_tid = local;
2963 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2965 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2967 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2968 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2975 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2983 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
2985 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2989 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2999 * Set options in a tunnel, extracted from a bit flag field
3001 * @param t Tunnel to set options to.
3002 * @param options Bit array in host byte order.
3005 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
3007 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
3008 GNUNET_YES : GNUNET_NO;
3009 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
3010 GNUNET_YES : GNUNET_NO;
3015 * Iterator for deleting each tunnel whose client endpoint disconnected.
3017 * @param cls Closure (client that has disconnected).
3018 * @param key The local tunnel id (used to access the hashmap).
3019 * @param value The value stored at the key (tunnel to destroy).
3021 * @return GNUNET_OK, keep iterating.
3024 tunnel_destroy_iterator (void *cls,
3028 struct MeshTunnel *t = value;
3029 struct MeshClient *c = cls;
3031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3032 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
3033 t->local_tid, t->local_tid_dest, c->id);
3034 client_delete_tunnel (c, t);
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
3039 if (0 != t->next_hop) { /* destroy could come before a path is used */
3040 GNUNET_PEER_change_rc (t->next_hop, -1);
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
3048 if (0 != t->prev_hop) { /* destroy could come before a path is used */
3049 GNUNET_PEER_change_rc (t->prev_hop, -1);
3054 tunnel_destroy_empty (t);
3061 * Timeout function, destroys tunnel if called
3063 * @param cls Closure (tunnel to destroy).
3064 * @param tc TaskContext
3067 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3069 struct MeshTunnel *t = cls;
3070 struct GNUNET_PeerIdentity id;
3072 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3073 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3075 GNUNET_PEER_resolve(t->id.oid, &id);
3076 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3077 "Tunnel %s [%X] timed out. Destroying.\n",
3078 GNUNET_i2s(&id), t->id.tid);
3079 if (NULL != t->client)
3080 send_client_tunnel_destroy (t->client, t);
3081 tunnel_destroy (t); /* Do not notify other */
3086 * Resets the tunnel timeout. Starts it if no timeout was running.
3088 * @param t Tunnel whose timeout to reset.
3090 * TODO use heap to improve efficiency of scheduler.
3093 tunnel_reset_timeout (struct MeshTunnel *t)
3095 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
3097 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
3098 GNUNET_SCHEDULER_cancel (t->maintenance_task);
3099 t->maintenance_task =
3100 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3101 (refresh_path_time, 4), &tunnel_timeout, t);
3105 /******************************************************************************/
3106 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3107 /******************************************************************************/
3110 * Function to send a create path packet to a peer.
3112 * @param cls closure
3113 * @param size number of bytes available in buf
3114 * @param buf where the callee should write the message
3115 * @return number of bytes written to buf
3118 send_core_path_create (void *cls, size_t size, void *buf)
3120 struct MeshTunnel *t = cls;
3121 struct GNUNET_MESH_CreateTunnel *msg;
3122 struct GNUNET_PeerIdentity *peer_ptr;
3123 struct MeshPeerPath *p = t->path;
3128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3130 sizeof (struct GNUNET_MESH_CreateTunnel) +
3131 p->length * sizeof (struct GNUNET_PeerIdentity);
3133 if (size < size_needed || NULL == buf)
3138 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
3139 msg->header.size = htons (size_needed);
3140 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3141 msg->tid = ntohl (t->id.tid);
3144 if (GNUNET_YES == t->nobuffer)
3145 opt |= GNUNET_MESH_OPTION_NOBUFFER;
3146 if (GNUNET_YES == t->reliable)
3147 opt |= GNUNET_MESH_OPTION_RELIABLE;
3148 msg->opt = htonl (opt);
3149 msg->port = htonl (t->port);
3151 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3152 for (i = 0; i < p->length; i++)
3154 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3158 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3164 * Creates a path ack message in buf and frees all unused resources.
3166 * @param cls closure (MeshTransmissionDescriptor)
3167 * @param size number of bytes available in buf
3168 * @param buf where the callee should write the message
3169 * @return number of bytes written to buf
3172 send_core_path_ack (void *cls, size_t size, void *buf)
3174 struct MeshTunnel *t = cls;
3175 struct GNUNET_MESH_PathACK *msg = buf;
3177 GNUNET_assert (NULL != t);
3178 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3183 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
3184 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3185 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3186 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
3187 msg->tid = htonl (t->id.tid);
3188 msg->peer_id = my_full_id;
3189 msg->ack = htonl (t->prev_fc.last_ack_sent);
3191 /* TODO add signature */
3193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3194 return sizeof (struct GNUNET_MESH_PathACK);
3199 * Free a transmission that was already queued with all resources
3200 * associated to the request.
3202 * @param queue Queue handler to cancel.
3203 * @param clear_cls Is it necessary to free associated cls?
3206 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3208 struct MeshFlowControl *fc;
3210 if (GNUNET_YES == clear_cls)
3212 switch (queue->type)
3214 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3215 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3216 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3218 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3219 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3220 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3221 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3222 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3224 " prebuilt message\n");
3225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3227 GNUNET_MESH_DEBUG_M2S (queue->type));
3229 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3235 " type %s unknown!\n",
3236 GNUNET_MESH_DEBUG_M2S (queue->type));
3238 GNUNET_free_non_null (queue->cls);
3240 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3241 queue->peer->queue_tail,
3244 /* Delete from appropriate fc in the tunnel */
3245 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
3246 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
3248 if (queue->peer->id == queue->tunnel->prev_hop)
3249 fc = &queue->tunnel->prev_fc;
3250 else if (queue->peer->id == queue->tunnel->next_hop)
3251 fc = &queue->tunnel->next_fc;
3255 GNUNET_free (queue);
3260 GNUNET_free (queue);
3265 * @brief Get the next transmittable message from the queue.
3267 * This will be the head, except in the case of being a data packet
3268 * not allowed by the destination peer.
3270 * @param peer Destination peer.
3272 * @return The next viable MeshPeerQueue element to send to that peer.
3273 * NULL when there are no transmittable messages.
3275 struct MeshPeerQueue *
3276 queue_get_next (const struct MeshPeerInfo *peer)
3278 struct MeshPeerQueue *q;
3280 struct GNUNET_MESH_Data *dmsg;
3281 struct MeshTunnel* t;
3285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
3286 for (q = peer->queue_head; NULL != q; q = q->next)
3289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3291 GNUNET_MESH_DEBUG_M2S (q->type));
3292 dmsg = (struct GNUNET_MESH_Data *) q->cls;
3293 pid = ntohl (dmsg->pid);
3296 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3297 ack = t->next_fc.last_ack_recv;
3299 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3300 ack = t->prev_fc.last_ack_recv;
3303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3308 "* ACK: %u, PID: %u, MID: %llu\n",
3309 ack, pid, GNUNET_ntohll (dmsg->mid));
3310 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3323 "* nothing found\n");
3329 queue_send (void *cls, size_t size, void *buf)
3331 struct MeshPeerInfo *peer = cls;
3332 struct GNUNET_MessageHeader *msg;
3333 struct MeshPeerQueue *queue;
3334 struct MeshTunnel *t;
3335 struct GNUNET_PeerIdentity dst_id;
3336 struct MeshFlowControl *fc;
3341 peer->core_transmit = NULL;
3343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3344 queue = queue_get_next (peer);
3346 /* Queue has no internal mesh traffic nor sendable payload */
3349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3350 if (NULL == peer->queue_head)
3351 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3356 GNUNET_PEER_resolve (peer->id, &dst_id);
3357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3359 GNUNET_i2s (&dst_id));
3360 /* Check if buffer size is enough for the message */
3361 if (queue->size > size)
3363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3364 "* not enough room, reissue\n");
3365 peer->core_transmit =
3366 GNUNET_CORE_notify_transmit_ready (core_handle,
3369 GNUNET_TIME_UNIT_FOREVER_REL,
3376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3379 GNUNET_assert (0 < t->pending_messages);
3380 t->pending_messages--;
3384 switch (queue->type)
3387 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3388 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3389 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3390 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3391 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3392 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3393 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3394 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3397 GNUNET_MESH_DEBUG_M2S (queue->type));
3399 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3400 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3401 data_size = send_core_data_raw (queue->cls, size, buf);
3402 msg = (struct GNUNET_MessageHeader *) buf;
3403 type = ntohs (msg->type);
3405 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3407 data_size = send_core_path_create (queue->cls, size, buf);
3409 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3411 data_size = send_core_path_ack (queue->cls, size, buf);
3415 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3416 "* type unknown: %u\n",
3421 if (0 < drop_percent &&
3422 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3424 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3425 "Dropping message of type %s\n",
3426 GNUNET_MESH_DEBUG_M2S(queue->type));
3429 /* Free queue, but cls was freed by send_core_* */
3430 queue_destroy (queue, GNUNET_NO);
3432 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3433 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3437 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3438 t->next_fc.last_pid_sent = pid;
3439 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3442 GNUNET_ntohll ( ((struct GNUNET_MESH_Data *) buf)->mid ));
3444 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3445 t->prev_fc.last_pid_sent = pid;
3446 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3452 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3458 /* If more data in queue, send next */
3459 queue = queue_get_next (peer);
3462 struct GNUNET_PeerIdentity id;
3464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3465 GNUNET_PEER_resolve (peer->id, &id);
3466 peer->core_transmit =
3467 GNUNET_CORE_notify_transmit_ready(core_handle,
3470 GNUNET_TIME_UNIT_FOREVER_REL,
3476 else if (NULL != peer->queue_head)
3478 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3480 GNUNET_i2s (&my_full_id));
3481 if (peer->id == t->next_hop)
3483 else if (peer->id == t->prev_hop)
3490 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3493 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3503 * @brief Queue and pass message to core when possible.
3505 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3506 * accounts for it. In case the queue is full, the message is dropped and
3509 * Otherwise, message is treated as internal and allowed to go regardless of
3512 * @param cls Closure (@c type dependant). It will be used by queue_send to
3513 * build the message to be sent if not already prebuilt.
3514 * @param type Type of the message, 0 for a raw message.
3515 * @param size Size of the message.
3516 * @param dst Neighbor to send message to.
3517 * @param t Tunnel this message belongs to.
3520 queue_add (void *cls, uint16_t type, size_t size,
3521 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3523 struct MeshPeerQueue *queue;
3524 struct GNUNET_PeerIdentity id;
3525 struct MeshFlowControl *fc;
3529 priority = GNUNET_NO;
3530 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3534 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3540 if (fc->queue_n >= t->queue_max)
3542 /* If this isn't a retransmission, drop the message */
3543 if (GNUNET_NO == t->reliable ||
3544 (NULL == t->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) ||
3545 (NULL == t->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type))
3547 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3551 "queue full: %u/%u\n",
3552 fc->queue_n, t->queue_max);
3553 return; /* Drop this message */
3555 priority = GNUNET_YES;
3559 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3565 if (GNUNET_YES == priority)
3567 struct GNUNET_MESH_Data *d;
3571 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
3572 d = (struct GNUNET_MESH_Data *) queue->cls;
3574 for (queue = dst->queue_tail; NULL != queue; queue = queue->prev)
3576 if (queue->type != type)
3578 d = (struct GNUNET_MESH_Data *) queue->cls;
3585 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3586 if (NULL == dst->core_transmit)
3588 GNUNET_PEER_resolve (dst->id, &id);
3589 dst->core_transmit =
3590 GNUNET_CORE_notify_transmit_ready (core_handle,
3593 GNUNET_TIME_UNIT_FOREVER_REL,
3599 t->pending_messages++;
3603 /******************************************************************************/
3604 /******************** MESH NETWORK HANDLERS **************************/
3605 /******************************************************************************/
3609 * Core handler for path creation
3611 * @param cls closure
3612 * @param message message
3613 * @param peer peer identity this notification is about
3615 * @return GNUNET_OK to keep the connection open,
3616 * GNUNET_SYSERR to close it (signal serious error)
3619 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3620 const struct GNUNET_MessageHeader *message)
3622 unsigned int own_pos;
3625 MESH_TunnelNumber tid;
3626 struct GNUNET_MESH_CreateTunnel *msg;
3627 struct GNUNET_PeerIdentity *pi;
3628 struct MeshPeerPath *path;
3629 struct MeshPeerInfo *dest_peer_info;
3630 struct MeshPeerInfo *orig_peer_info;
3631 struct MeshTunnel *t;
3633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3634 "Received a path create msg [%s]\n",
3635 GNUNET_i2s (&my_full_id));
3636 size = ntohs (message->size);
3637 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3639 GNUNET_break_op (0);
3643 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3644 if (size % sizeof (struct GNUNET_PeerIdentity))
3646 GNUNET_break_op (0);
3649 size /= sizeof (struct GNUNET_PeerIdentity);
3652 GNUNET_break_op (0);
3655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3656 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3658 tid = ntohl (msg->tid);
3659 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3661 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3662 t = tunnel_get (pi, tid);
3663 if (NULL == t) /* might be a local tunnel */
3667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3668 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3674 t->port = ntohl (msg->port);
3675 opt = ntohl (msg->opt);
3676 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3678 t->nobuffer = GNUNET_YES;
3681 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3683 t->reliable = GNUNET_YES;
3685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3687 tunnel_reset_timeout (t);
3689 t->state = MESH_TUNNEL_WAITING;
3691 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3692 if (NULL == dest_peer_info)
3694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3695 " Creating PeerInfo for destination.\n");
3696 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3697 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3698 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3700 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3702 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3703 if (NULL == orig_peer_info)
3705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3706 " Creating PeerInfo for origin.\n");
3707 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3708 orig_peer_info->id = GNUNET_PEER_intern (pi);
3709 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3710 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3713 path = path_new (size);
3715 for (i = 0; i < size; i++)
3717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3718 GNUNET_i2s (&pi[i]));
3719 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3720 if (path->peers[i] == myid)
3723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3724 if (own_pos == 0 && path->peers[own_pos] != myid)
3726 /* create path: self not found in path through self */
3727 GNUNET_break_op (0);
3728 path_destroy (path);
3732 path_add_to_peers (path, GNUNET_NO);
3733 tunnel_use_path (t, path);
3735 peer_info_add_tunnel (dest_peer_info, t);
3737 if (own_pos == size - 1)
3739 struct MeshClient *c;
3741 /* Find target client */
3742 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
3745 /* TODO send reject */
3749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3750 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3752 /* Assign local tid */
3753 while (NULL != tunnel_get_incoming (next_local_tid))
3754 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3755 t->local_tid_dest = next_local_tid++;
3756 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3758 if (GNUNET_YES == t->reliable)
3760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
3761 t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
3763 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
3766 tunnel_add_client (t, c);
3767 send_client_tunnel_create (t);
3772 struct MeshPeerPath *path2;
3774 t->next_hop = path->peers[own_pos + 1];
3775 GNUNET_PEER_change_rc(t->next_hop, 1);
3777 /* It's for somebody else! Retransmit. */
3778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3779 path2 = path_duplicate (path);
3780 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3781 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3782 send_create_path (t);
3790 * Core handler for path ACKs
3792 * @param cls closure
3793 * @param message message
3794 * @param peer peer identity this notification is about
3796 * @return GNUNET_OK to keep the connection open,
3797 * GNUNET_SYSERR to close it (signal serious error)
3800 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3801 const struct GNUNET_MessageHeader *message)
3803 struct GNUNET_MESH_PathACK *msg;
3804 struct MeshPeerInfo *peer_info;
3805 struct MeshPeerPath *p;
3806 struct MeshTunnel *t;
3808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3809 GNUNET_i2s (&my_full_id));
3810 msg = (struct GNUNET_MESH_PathACK *) message;
3811 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3814 /* TODO notify that we don't know the tunnel */
3815 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3817 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3821 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3823 peer_info = peer_get (&msg->peer_id);
3824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3825 GNUNET_i2s (&msg->peer_id));
3826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3829 /* Add path to peers? */
3833 path_add_to_peers (p, GNUNET_YES);
3839 t->state = MESH_TUNNEL_READY;
3840 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3841 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3843 /* Message for us? */
3844 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3847 if (NULL == t->owner)
3849 GNUNET_break_op (0);
3852 if (NULL != peer_info->dhtget)
3854 GNUNET_DHT_get_stop (peer_info->dhtget);
3855 peer_info->dhtget = NULL;
3857 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3858 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3863 " not for us, retransmitting...\n");
3864 peer_info = peer_get (&msg->oid);
3865 send_prebuilt_message (message, t->prev_hop, t);
3871 * Core handler for notifications of broken paths
3873 * @param cls closure
3874 * @param message message
3875 * @param peer peer identity this notification is about
3877 * @return GNUNET_OK to keep the connection open,
3878 * GNUNET_SYSERR to close it (signal serious error)
3881 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3882 const struct GNUNET_MessageHeader *message)
3884 struct GNUNET_MESH_PathBroken *msg;
3885 struct MeshTunnel *t;
3887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3888 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3889 msg = (struct GNUNET_MESH_PathBroken *) message;
3890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3891 GNUNET_i2s (&msg->peer1));
3892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3893 GNUNET_i2s (&msg->peer2));
3894 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3897 GNUNET_break_op (0);
3900 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3901 GNUNET_PEER_search (&msg->peer2));
3908 * Core handler for tunnel destruction
3910 * @param cls closure
3911 * @param message message
3912 * @param peer peer identity this notification is about
3914 * @return GNUNET_OK to keep the connection open,
3915 * GNUNET_SYSERR to close it (signal serious error)
3918 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3919 const struct GNUNET_MessageHeader *message)
3921 struct GNUNET_MESH_TunnelDestroy *msg;
3922 struct MeshTunnel *t;
3924 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3926 "Got a TUNNEL DESTROY packet from %s\n",
3928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3929 " for tunnel %s [%u]\n",
3930 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3931 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3934 /* Probably already got the message from another path,
3935 * destroyed the tunnel and retransmitted to children.
3938 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3942 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3945 t->local_tid, t->local_tid_dest);
3947 if (GNUNET_PEER_search (peer) == t->prev_hop)
3949 // TODO check owner's signature
3950 // TODO add owner's signatue to tunnel for retransmission
3951 peer_cancel_queues (t->prev_hop, t);
3952 GNUNET_PEER_change_rc (t->prev_hop, -1);
3955 else if (GNUNET_PEER_search (peer) == t->next_hop)
3957 // TODO check dest's signature
3958 // TODO add dest's signatue to tunnel for retransmission
3959 peer_cancel_queues (t->next_hop, t);
3960 GNUNET_PEER_change_rc (t->next_hop, -1);
3965 GNUNET_break_op (0);
3966 // TODO check both owner AND destination's signature to see which matches
3967 // TODO restransmit in appropriate direction
3970 tunnel_destroy_empty (t);
3972 // TODO: add timeout to destroy the tunnel anyway
3978 * Core handler for mesh network traffic going from the origin to a peer
3980 * @param cls closure
3981 * @param peer peer identity this notification is about
3982 * @param message message
3983 * @return GNUNET_OK to keep the connection open,
3984 * GNUNET_SYSERR to close it (signal serious error)
3987 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3988 const struct GNUNET_MessageHeader *message)
3990 struct GNUNET_MESH_Data *msg;
3991 struct MeshTunnel *t;
3996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3999 size = ntohs (message->size);
4001 sizeof (struct GNUNET_MESH_Data) +
4002 sizeof (struct GNUNET_MessageHeader))
4007 msg = (struct GNUNET_MESH_Data *) message;
4008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4009 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4011 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4014 /* TODO notify back: we don't know this tunnel */
4015 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4016 GNUNET_break_op (0);
4019 pid = ntohl (msg->pid);
4020 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
4022 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4023 GNUNET_break_op (0);
4024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4025 "Received PID %u, (prev %u), ACK %u\n",
4026 pid, t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
4030 tunnel_reset_timeout (t);
4031 if (t->dest == myid)
4033 /* TODO signature verification */
4034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4035 " it's for us! sending to clients...\n");
4036 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4037 // if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv)) FIXME use
4038 if (GMC_is_pid_bigger (pid, t->prev_fc.last_pid_recv))
4042 mid = GNUNET_ntohll (msg->mid);
4043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4044 " pid %u (%llu) not seen yet\n", pid, mid);
4045 t->prev_fc.last_pid_recv = pid;
4047 if (GNUNET_NO == t->reliable ||
4048 (mid >= t->bck_rel->mid_recv && mid <= t->bck_rel->mid_recv + 64))
4050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4051 "!!! RECV %llu\n", GNUNET_ntohll(msg->mid));
4052 if (GNUNET_YES == t->reliable)
4054 /* Is this the exact next expected messasge? */
4055 if (mid == t->bck_rel->mid_recv)
4057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4058 t->bck_rel->mid_recv++;
4059 tunnel_send_client_ucast (t, msg);
4060 tunnel_send_client_buffered_ucast (t);
4064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4065 tunnel_add_buffer_ucast (t, msg);
4071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4072 " MID %llu not expected (%llu - %llu), dropping!\n",
4073 GNUNET_ntohll (msg->mid),
4074 t->bck_rel->mid_recv, t->bck_rel->mid_recv + 64LL);
4079 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4081 " Pid %u not expected (%u), dropping!\n",
4082 pid, t->prev_fc.last_pid_recv + 1);
4084 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4087 t->prev_fc.last_pid_recv = pid;
4088 if (0 == t->next_hop)
4093 ttl = ntohl (msg->ttl);
4094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4097 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4098 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4099 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4103 " not for us, retransmitting...\n");
4105 send_prebuilt_message (message, t->next_hop, t);
4106 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4112 * Core handler for mesh network traffic toward the owner of a tunnel
4114 * @param cls closure
4115 * @param message message
4116 * @param peer peer identity this notification is about
4118 * @return GNUNET_OK to keep the connection open,
4119 * GNUNET_SYSERR to close it (signal serious error)
4122 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4123 const struct GNUNET_MessageHeader *message)
4125 struct GNUNET_MESH_Data *msg;
4126 struct MeshTunnel *t;
4131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4133 size = ntohs (message->size);
4134 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
4135 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4137 GNUNET_break_op (0);
4140 msg = (struct GNUNET_MESH_Data *) message;
4141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4142 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4143 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4144 pid = ntohl (msg->pid);
4147 /* TODO notify that we dont know this tunnel (whom)? */
4148 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4150 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
4151 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4155 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
4157 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
4158 GNUNET_break_op (0);
4159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4160 "Received PID %u, ACK %u\n",
4161 pid, t->next_fc.last_ack_sent);
4162 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4166 if (myid == t->id.oid)
4168 /* TODO signature verification */
4169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4170 " it's for us! sending to clients...\n");
4171 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4172 if ( (GNUNET_NO == t->reliable &&
4173 GMC_is_pid_bigger(pid, t->next_fc.last_pid_recv))
4175 (GNUNET_YES == t->reliable &&
4176 pid == t->next_fc.last_pid_recv + 1) ) // FIXME use "futures" as accepting
4178 t->next_fc.last_pid_recv = pid;
4179 tunnel_send_client_to_orig (t, msg);
4183 // GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
4184 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4185 " Pid %u not expected, sending FWD ACK!\n", pid);
4187 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4191 " not for us, retransmitting...\n");
4192 t->next_fc.last_pid_recv = pid;
4193 if (0 == t->prev_hop) /* No owner AND no prev hop */
4195 if (GNUNET_YES == t->destroy)
4197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4198 "to orig received on a dying tunnel %s [%X]\n",
4199 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4203 "unknown to origin at %s\n",
4204 GNUNET_i2s (&my_full_id));
4205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4209 "on tunnel %s [%X]\n",
4210 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4213 ttl = ntohl (msg->ttl);
4214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4217 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4218 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4219 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4222 send_prebuilt_message (message, t->prev_hop, t);
4223 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4230 * Core handler for mesh network traffic end-to-end ACKs.
4232 * @param cls Closure.
4233 * @param message Message.
4234 * @param peer Peer identity this notification is about.
4236 * @return GNUNET_OK to keep the connection open,
4237 * GNUNET_SYSERR to close it (signal serious error)
4240 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4241 const struct GNUNET_MessageHeader *message)
4243 struct GNUNET_MESH_DataACK *msg;
4244 struct MeshTunnelReliability *rel;
4245 struct MeshReliableMessage *copy;
4246 struct MeshReliableMessage *next;
4247 struct MeshTunnel *t;
4253 type = ntohs (message->type);
4254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
4255 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4256 msg = (struct GNUNET_MESH_DataACK *) message;
4258 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4261 /* TODO notify that we dont know this tunnel (whom)? */
4262 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4265 ack = GNUNET_ntohll (msg->mid);
4266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %llu\n", ack);
4268 /* Is this a forward or backward ACK? */
4269 id = GNUNET_PEER_search (peer);
4270 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4273 if (NULL == t->owner)
4275 send_prebuilt_message (message, t->prev_hop, t);
4279 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4281 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4284 if (NULL == t->client)
4286 send_prebuilt_message (message, t->next_hop, t);
4290 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4294 GNUNET_break_op (0);
4298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %llu\n", ack);
4299 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4301 if (copy->mid > ack)
4303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %llu, out!\n", copy->mid);
4304 tunnel_free_buffer_ucast (t, msg);
4308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %llu\n", copy->mid);
4310 tunnel_free_reliable_message (copy);
4313 if (GNUNET_YES == work)
4315 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4317 GNUNET_SCHEDULER_cancel (rel->retry_task);
4318 if (NULL == rel->head_sent)
4320 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4324 struct GNUNET_TIME_Absolute new_target;
4325 struct GNUNET_TIME_Relative delay;
4327 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4328 MESH_RETRANSMIT_MARGIN);
4329 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4331 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4333 GNUNET_SCHEDULER_add_delayed (delay,
4334 &tunnel_retransmit_message,
4340 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK);
4346 * Core handler for mesh network traffic point-to-point acks.
4348 * @param cls closure
4349 * @param message message
4350 * @param peer peer identity this notification is about
4352 * @return GNUNET_OK to keep the connection open,
4353 * GNUNET_SYSERR to close it (signal serious error)
4356 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4357 const struct GNUNET_MessageHeader *message)
4359 struct GNUNET_MESH_ACK *msg;
4360 struct MeshTunnel *t;
4361 struct MeshFlowControl *fc;
4365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4367 msg = (struct GNUNET_MESH_ACK *) message;
4369 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4373 /* TODO notify that we dont know this tunnel (whom)? */
4374 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4377 ack = ntohl (msg->pid);
4378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4380 /* Is this a forward or backward ACK? */
4381 id = GNUNET_PEER_search (peer);
4382 if (t->next_hop == id)
4385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4388 else if (t->prev_hop == id)
4391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4396 GNUNET_break_op (0);
4400 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4401 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4403 GNUNET_SCHEDULER_cancel (fc->poll_task);
4404 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4405 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4407 fc->last_ack_recv = ack;
4408 peer_unlock_queue (id);
4410 if (t->next_hop == id)
4411 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4413 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4420 * Core handler for mesh network traffic point-to-point ack polls.
4422 * @param cls closure
4423 * @param message message
4424 * @param peer peer identity this notification is about
4426 * @return GNUNET_OK to keep the connection open,
4427 * GNUNET_SYSERR to close it (signal serious error)
4430 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4431 const struct GNUNET_MessageHeader *message)
4433 struct GNUNET_MESH_Poll *msg;
4434 struct MeshTunnel *t;
4435 struct MeshFlowControl *fc;
4440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4443 msg = (struct GNUNET_MESH_Poll *) message;
4445 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4449 /* TODO notify that we dont know this tunnel (whom)? */
4450 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4451 GNUNET_break_op (0);
4455 /* Is this a forward or backward ACK? */
4456 id = GNUNET_PEER_search(peer);
4457 pid = ntohl (msg->pid);
4458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4459 if (t->next_hop == id)
4461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", t->next_fc.last_pid_recv);
4464 old = fc->last_pid_recv;
4465 fc->last_pid_recv = pid;
4466 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4468 else if (t->prev_hop == id)
4470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", t->prev_fc.last_pid_recv);
4473 old = fc->last_pid_recv;
4474 fc->last_pid_recv = pid;
4475 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4480 if (GNUNET_YES == t->reliable)
4481 fc->last_pid_recv = old;
4488 * Core handler for mesh keepalives.
4490 * @param cls closure
4491 * @param message message
4492 * @param peer peer identity this notification is about
4493 * @return GNUNET_OK to keep the connection open,
4494 * GNUNET_SYSERR to close it (signal serious error)
4496 * TODO: Check who we got this from, to validate route.
4499 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4500 const struct GNUNET_MessageHeader *message)
4502 struct GNUNET_MESH_TunnelKeepAlive *msg;
4503 struct MeshTunnel *t;
4505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4508 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4509 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4513 /* TODO notify that we dont know that tunnel */
4514 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4519 tunnel_reset_timeout (t);
4520 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4523 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4524 send_prebuilt_message (message, t->next_hop, t);
4531 * Functions to handle messages from core
4533 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4534 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4535 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4536 sizeof (struct GNUNET_MESH_PathBroken)},
4537 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4538 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4539 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4540 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4541 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4542 sizeof (struct GNUNET_MESH_DataACK)},
4543 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4544 sizeof (struct GNUNET_MESH_DataACK)},
4545 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4546 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4547 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4548 sizeof (struct GNUNET_MESH_ACK)},
4549 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4550 sizeof (struct GNUNET_MESH_Poll)},
4551 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4552 sizeof (struct GNUNET_MESH_PathACK)},
4558 /******************************************************************************/
4559 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4560 /******************************************************************************/
4565 * notify_client_connection_failure: notify a client that the connection to the
4566 * requested remote peer is not possible (for instance, no route found)
4567 * Function called when the socket is ready to queue more data. "buf" will be
4568 * NULL and "size" zero if the socket was closed for writing in the meantime.
4570 * @param cls closure
4571 * @param size number of bytes available in buf
4572 * @param buf where the callee should write the message
4573 * @return number of bytes written to buf
4576 notify_client_connection_failure (void *cls, size_t size, void *buf)
4579 struct MeshPeerInfo *peer_info;
4580 struct GNUNET_MESH_PeerControl *msg;
4581 struct GNUNET_PeerIdentity id;
4583 if (0 == size && NULL == buf)
4585 // TODO retry? cancel?
4589 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4590 peer_info = (struct MeshPeerInfo *) cls;
4591 msg = (struct GNUNET_MESH_PeerControl *) buf;
4592 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4593 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4594 // msg->tunnel_id = htonl(peer_info->t->tid);
4595 GNUNET_PEER_resolve (peer_info->id, &id);
4596 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4604 * Send keepalive packets for a tunnel.
4606 * @param cls Closure (tunnel for which to send the keepalive).
4607 * @param tc Notification context.
4609 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4612 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4614 struct MeshTunnel *t = cls;
4615 struct GNUNET_MESH_TunnelKeepAlive *msg;
4616 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4619 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4620 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4621 NULL == t->owner || 0 == t->local_tid)
4626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4627 "sending keepalive for tunnel %d\n", t->id.tid);
4629 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4630 msg->header.size = htons (size);
4631 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4632 msg->oid = my_full_id;
4633 msg->tid = htonl (t->id.tid);
4634 send_prebuilt_message (&msg->header, t->next_hop, t);
4636 t->maintenance_task =
4637 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4642 * Function to process paths received for a new peer addition. The recorded
4643 * paths form the initial tunnel, which can be optimized later.
4644 * Called on each result obtained for the DHT search.
4646 * @param cls closure
4647 * @param exp when will this value expire
4648 * @param key key of the result
4649 * @param get_path path of the get request
4650 * @param get_path_length lenght of get_path
4651 * @param put_path path of the put request
4652 * @param put_path_length length of the put_path
4653 * @param type type of the result
4654 * @param size number of bytes in data
4655 * @param data pointer to the result data
4658 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4659 const struct GNUNET_HashCode * key,
4660 const struct GNUNET_PeerIdentity *get_path,
4661 unsigned int get_path_length,
4662 const struct GNUNET_PeerIdentity *put_path,
4663 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4664 size_t size, const void *data)
4666 struct MeshPeerInfo *peer = cls;
4667 struct MeshPeerPath *p;
4668 struct GNUNET_PeerIdentity pi;
4671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4672 GNUNET_PEER_resolve (peer->id, &pi);
4673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4675 p = path_build_from_dht (get_path, get_path_length,
4676 put_path, put_path_length);
4677 path_add_to_peers (p, GNUNET_NO);
4679 for (i = 0; i < peer->ntunnels; i++)
4681 struct GNUNET_PeerIdentity id;
4683 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4685 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4686 peer->tunnels[i]->local_tid,
4687 peer->tunnels[i]->local_tid_dest);
4688 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4691 peer_connect (peer, peer->tunnels[i]);
4699 /******************************************************************************/
4700 /********************* MESH LOCAL HANDLES **************************/
4701 /******************************************************************************/
4705 * Handler for client connection.
4707 * @param cls Closure (unused).
4708 * @param client Client handler.
4711 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
4713 struct MeshClient *c;
4717 c = GNUNET_malloc (sizeof (struct MeshClient));
4719 GNUNET_SERVER_client_keep (client);
4720 GNUNET_SERVER_client_set_user_context (client, c);
4721 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4726 * Handler for client disconnection
4728 * @param cls closure
4729 * @param client identification of the client; NULL
4730 * for the last call when the server is destroyed
4733 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4735 struct MeshClient *c;
4736 struct MeshClient *next;
4738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4745 c = client_get (client);
4748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4750 GNUNET_SERVER_client_drop (c->handle);
4751 c->shutting_down = GNUNET_YES;
4752 if (NULL != c->own_tunnels)
4754 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4755 &tunnel_destroy_iterator, c);
4756 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4759 if (NULL != c->incoming_tunnels)
4761 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4762 &tunnel_destroy_iterator, c);
4763 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4766 if (NULL != c->ports)
4767 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4769 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4772 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4777 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
4779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4785 * Handler for new clients
4787 * @param cls closure
4788 * @param client identification of the client
4789 * @param message the actual message, which includes messages the client wants
4792 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4793 const struct GNUNET_MessageHeader *message)
4795 struct GNUNET_MESH_ClientConnect *cc_msg;
4796 struct MeshClient *c;
4801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4803 /* Check data sanity */
4804 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4805 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4806 if (0 != (size % sizeof (uint32_t)))
4809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4812 size /= sizeof (uint32_t);
4814 /* Initialize new client structure */
4815 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
4816 c->id = next_client_id++; /* overflow not important: just for debug */
4817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4823 p = (uint32_t *) &cc_msg[1];
4824 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4825 for (i = 0; i < size; i++)
4828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4830 /* store in client's hashmap */
4831 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4832 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4833 /* store in global hashmap */
4834 /* FIXME only allow one client to have the port open,
4835 * have a backup hashmap with waiting clients */
4836 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4837 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4841 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4842 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4843 GNUNET_SERVER_notification_context_add (nc, client);
4844 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4846 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4852 * Handler for requests of new tunnels
4854 * @param cls Closure.
4855 * @param client Identification of the client.
4856 * @param message The actual message.
4859 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4860 const struct GNUNET_MessageHeader *message)
4862 struct GNUNET_MESH_TunnelMessage *t_msg;
4863 struct MeshPeerInfo *peer_info;
4864 struct MeshTunnel *t;
4865 struct MeshClient *c;
4866 MESH_TunnelNumber tid;
4868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4870 /* Sanity check for client registration */
4871 if (NULL == (c = client_get (client)))
4874 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4879 /* Message size sanity check */
4880 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4883 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4887 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4889 GNUNET_i2s (&t_msg->peer));
4890 /* Sanity check for tunnel numbering */
4891 tid = ntohl (t_msg->tunnel_id);
4892 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4898 /* Sanity check for duplicate tunnel IDs */
4899 if (NULL != tunnel_get_by_local_id (c, tid))
4902 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4907 while (NULL != tunnel_get_by_pi (myid, next_tid))
4908 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4909 t = tunnel_new (myid, next_tid, c, tid);
4910 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4914 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4917 t->port = ntohl (t_msg->port);
4918 tunnel_set_options (t, ntohl (t_msg->opt));
4919 if (GNUNET_YES == t->reliable)
4921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4922 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
4924 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
4927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4928 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4930 peer_info = peer_get (&t_msg->peer);
4931 peer_info_add_tunnel (peer_info, t);
4932 peer_connect (peer_info, t);
4933 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4939 * Handler for requests of deleting tunnels
4941 * @param cls closure
4942 * @param client identification of the client
4943 * @param message the actual message
4946 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4947 const struct GNUNET_MessageHeader *message)
4949 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4950 struct MeshClient *c;
4951 struct MeshTunnel *t;
4952 MESH_TunnelNumber tid;
4954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4955 "Got a DESTROY TUNNEL from client!\n");
4957 /* Sanity check for client registration */
4958 if (NULL == (c = client_get (client)))
4961 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4966 /* Message sanity check */
4967 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4970 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4974 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4976 /* Retrieve tunnel */
4977 tid = ntohl (tunnel_msg->tunnel_id);
4978 t = tunnel_get_by_local_id(c, tid);
4981 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4983 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4987 /* Cleanup after the tunnel */
4988 client_delete_tunnel (c, t);
4995 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4999 /* The tunnel will be destroyed when the last message is transmitted. */
5000 tunnel_destroy_empty (t);
5002 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5008 * Handler for client traffic
5010 * @param cls closure
5011 * @param client identification of the client
5012 * @param message the actual message
5015 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
5016 const struct GNUNET_MessageHeader *message)
5018 struct GNUNET_MESH_LocalData *data_msg;
5019 struct MeshClient *c;
5020 struct MeshTunnel *t;
5021 struct MeshFlowControl *fc;
5022 MESH_TunnelNumber tid;
5025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5026 "Got data from a client!\n");
5028 /* Sanity check for client registration */
5029 if (NULL == (c = client_get (client)))
5032 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5037 data_msg = (struct GNUNET_MESH_LocalData *) message;
5039 /* Sanity check for message size */
5040 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
5041 if (size < sizeof (struct GNUNET_MessageHeader))
5044 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5048 /* Tunnel exists? */
5049 tid = ntohl (data_msg->tid);
5050 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
5053 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5056 t = tunnel_get_by_local_id (c, tid);
5060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5064 /* Is the client in the tunnel? */
5065 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5067 t->owner->handle == client)
5069 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
5071 t->client->handle == client) ) )
5074 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5078 /* Ok, everything is correct, send the message
5079 * (pretend we got it from a mesh peer)
5082 struct GNUNET_MESH_Data *payload;
5083 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
5085 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
5086 if (GNUNET_YES == t->reliable)
5088 struct MeshTunnelReliability *rel;
5089 struct MeshReliableMessage *copy;
5091 rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
5092 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
5093 + sizeof(struct GNUNET_MESH_Data)
5095 copy->mid = rel->mid_sent++;
5096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %llu\n", copy->mid);
5097 copy->timestamp = GNUNET_TIME_absolute_get ();
5100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
5101 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
5102 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
5105 GNUNET_TIME_relative_multiply (rel->expected_delay,
5106 MESH_RETRANSMIT_MARGIN);
5108 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
5109 &tunnel_retransmit_message,
5112 payload = (struct GNUNET_MESH_Data *) ©[1];
5113 payload->mid = GNUNET_htonll (copy->mid);
5117 payload = (struct GNUNET_MESH_Data *) cbuf;
5119 // FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
5120 // use different struct for unreliable traffic, save 8 bytes
5122 memcpy (&payload[1], &data_msg[1], size);
5123 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
5124 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
5125 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
5126 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5127 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
5128 payload->tid = htonl (t->id.tid);
5129 payload->ttl = htonl (default_ttl);
5130 payload->pid = htonl (fc->last_pid_recv + 1);
5131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5132 " calling generic handler...\n");
5133 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5134 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
5136 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
5138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5139 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5146 * Handler for client's ACKs for payload traffic.
5148 * @param cls Closure (unused).
5149 * @param client Identification of the client.
5150 * @param message The actual message.
5153 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5154 const struct GNUNET_MessageHeader *message)
5156 struct GNUNET_MESH_LocalAck *msg;
5157 struct MeshTunnel *t;
5158 struct MeshClient *c;
5159 MESH_TunnelNumber tid;
5161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5162 /* Sanity check for client registration */
5163 if (NULL == (c = client_get (client)))
5166 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5171 msg = (struct GNUNET_MESH_LocalAck *) message;
5173 /* Tunnel exists? */
5174 tid = ntohl (msg->tunnel_id);
5175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5176 t = tunnel_get_by_local_id (c, tid);
5180 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5181 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5182 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5186 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5189 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5190 t->prev_fc.last_ack_recv++;
5191 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5195 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5196 t->next_fc.last_ack_recv++;
5197 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
5200 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5208 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5210 * @param cls Closure (client handle).
5211 * @param key Key (hashed tunnel ID, unused).
5212 * @param value Tunnel info.
5214 * @return GNUNET_YES, to keep iterating.
5217 monitor_all_tunnels_iterator (void *cls,
5218 const struct GNUNET_HashCode * key,
5221 struct GNUNET_SERVER_Client *client = cls;
5222 struct MeshTunnel *t = value;
5223 struct GNUNET_MESH_LocalMonitor *msg;
5225 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5226 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5227 msg->tunnel_id = htonl (t->id.tid);
5228 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5229 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5230 GNUNET_PEER_resolve (t->dest, &msg->destination);
5232 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5233 "* sending info about tunnel %s [%u]\n",
5234 GNUNET_i2s (&msg->owner), t->id.tid);
5236 GNUNET_SERVER_notification_context_unicast (nc, client,
5237 &msg->header, GNUNET_NO);
5243 * Handler for client's MONITOR request.
5245 * @param cls Closure (unused).
5246 * @param client Identification of the client.
5247 * @param message The actual message.
5250 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5251 const struct GNUNET_MessageHeader *message)
5253 struct MeshClient *c;
5255 /* Sanity check for client registration */
5256 if (NULL == (c = client_get (client)))
5259 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5263 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5264 "Received get tunnels request from client %u\n",
5266 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5267 monitor_all_tunnels_iterator,
5269 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5270 "Get tunnels request from client %u completed\n",
5272 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5277 * Handler for client's MONITOR_TUNNEL request.
5279 * @param cls Closure (unused).
5280 * @param client Identification of the client.
5281 * @param message The actual message.
5284 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5285 const struct GNUNET_MessageHeader *message)
5287 const struct GNUNET_MESH_LocalMonitor *msg;
5288 struct GNUNET_MESH_LocalMonitor *resp;
5289 struct MeshClient *c;
5290 struct MeshTunnel *t;
5292 /* Sanity check for client registration */
5293 if (NULL == (c = client_get (client)))
5296 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5300 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5301 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5302 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5305 ntohl (msg->tunnel_id));
5306 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5309 /* We don't know the tunnel FIXME */
5310 struct GNUNET_MESH_LocalMonitor warn;
5313 GNUNET_SERVER_notification_context_unicast (nc, client,
5316 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5320 /* Initialize context */
5321 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5323 GNUNET_PEER_resolve (t->dest, &resp->destination);
5324 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5325 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5326 &resp->header, GNUNET_NO);
5329 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5330 "Monitor tunnel request from client %u completed\n",
5332 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5337 * Functions to handle messages from clients
5339 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5340 {&handle_local_new_client, NULL,
5341 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5342 {&handle_local_tunnel_create, NULL,
5343 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5344 sizeof (struct GNUNET_MESH_TunnelMessage)},
5345 {&handle_local_tunnel_destroy, NULL,
5346 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5347 sizeof (struct GNUNET_MESH_TunnelMessage)},
5348 {&handle_local_data, NULL,
5349 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5350 {&handle_local_ack, NULL,
5351 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5352 sizeof (struct GNUNET_MESH_LocalAck)},
5353 {&handle_local_get_tunnels, NULL,
5354 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5355 sizeof (struct GNUNET_MessageHeader)},
5356 {&handle_local_show_tunnel, NULL,
5357 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5358 sizeof (struct GNUNET_MESH_LocalMonitor)},
5364 * Method called whenever a given peer connects.
5366 * @param cls closure
5367 * @param peer peer identity this notification is about
5370 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5372 struct MeshPeerInfo *peer_info;
5373 struct MeshPeerPath *path;
5375 DEBUG_CONN ("Peer connected\n");
5376 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5377 peer_info = peer_get (peer);
5378 if (myid == peer_info->id)
5380 DEBUG_CONN (" (self)\n");
5381 path = path_new (1);
5385 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5386 path = path_new (2);
5387 path->peers[1] = peer_info->id;
5388 GNUNET_PEER_change_rc (peer_info->id, 1);
5389 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5391 path->peers[0] = myid;
5392 GNUNET_PEER_change_rc (myid, 1);
5393 peer_info_add_path (peer_info, path, GNUNET_YES);
5399 * Method called whenever a peer disconnects.
5401 * @param cls closure
5402 * @param peer peer identity this notification is about
5405 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5407 struct MeshPeerInfo *pi;
5408 struct MeshPeerQueue *q;
5409 struct MeshPeerQueue *n;
5411 DEBUG_CONN ("Peer disconnected\n");
5412 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5422 /* TODO try to reroute this traffic instead */
5423 queue_destroy(q, GNUNET_YES);
5426 if (NULL != pi->core_transmit)
5428 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5429 pi->core_transmit = NULL;
5431 peer_remove_path (pi, pi->id, myid);
5434 DEBUG_CONN (" (self)\n");
5436 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5442 * Install server (service) handlers and start listening to clients.
5447 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5448 GNUNET_SERVER_connect_notify (server_handle,
5449 &handle_local_client_connect, NULL);
5450 GNUNET_SERVER_disconnect_notify (server_handle,
5451 &handle_local_client_disconnect, NULL);
5452 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5454 clients_head = NULL;
5455 clients_tail = NULL;
5457 GNUNET_SERVER_resume (server_handle);
5462 * To be called on core init/fail.
5464 * @param cls Closure (config)
5465 * @param server handle to the server for this service
5466 * @param identity the public identity of this peer
5469 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5470 const struct GNUNET_PeerIdentity *identity)
5472 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5476 GNUNET_break (core_handle == server);
5477 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5480 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5481 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5483 GNUNET_i2s (identity));
5484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5486 GNUNET_i2s (&my_full_id));
5487 GNUNET_CORE_disconnect (core_handle);
5488 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5489 NULL, /* Closure passed to MESH functions */
5490 &core_init, /* Call core_init once connected */
5491 &core_connect, /* Handle connects */
5492 &core_disconnect, /* remove peers on disconnects */
5493 NULL, /* Don't notify about all incoming messages */
5494 GNUNET_NO, /* For header only in notification */
5495 NULL, /* Don't notify about all outbound messages */
5496 GNUNET_NO, /* For header-only out notification */
5497 core_handlers); /* Register these handlers */
5506 /******************************************************************************/
5507 /************************ MAIN FUNCTIONS ****************************/
5508 /******************************************************************************/
5511 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5513 * @param cls closure
5514 * @param key current key code
5515 * @param value value in the hash map
5516 * @return GNUNET_YES if we should continue to iterate,
5520 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5522 struct MeshTunnel *t = value;
5529 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5531 * @param cls closure
5532 * @param key current key code
5533 * @param value value in the hash map
5534 * @return GNUNET_YES if we should continue to iterate,
5538 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5540 struct MeshPeerInfo *p = value;
5541 struct MeshPeerQueue *q;
5542 struct MeshPeerQueue *n;
5550 queue_destroy(q, GNUNET_YES);
5554 peer_info_destroy (p);
5560 * Task run during shutdown.
5566 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5570 if (core_handle != NULL)
5572 GNUNET_CORE_disconnect (core_handle);
5575 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5576 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5577 if (dht_handle != NULL)
5579 GNUNET_DHT_disconnect (dht_handle);
5584 GNUNET_SERVER_notification_context_destroy (nc);
5587 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5589 GNUNET_SCHEDULER_cancel (announce_id_task);
5590 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5597 * Process mesh requests.
5599 * @param cls closure
5600 * @param server the initialized server
5601 * @param c configuration to use
5604 run (void *cls, struct GNUNET_SERVER_Handle *server,
5605 const struct GNUNET_CONFIGURATION_Handle *c)
5608 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5611 server_handle = server;
5612 GNUNET_SERVER_suspend (server_handle);
5615 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5618 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5620 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5621 "mesh", "peer/privatekey");
5622 GNUNET_SCHEDULER_shutdown ();
5627 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5628 &refresh_path_time))
5630 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5632 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5633 "mesh", "refresh path time");
5634 GNUNET_SCHEDULER_shutdown ();
5639 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5642 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5644 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5645 "mesh", "id announce time");
5646 GNUNET_SCHEDULER_shutdown ();
5651 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5656 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5657 "mesh", "connect timeout");
5658 GNUNET_SCHEDULER_shutdown ();
5663 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5666 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5668 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5669 "mesh", "max msgs queue");
5670 GNUNET_SCHEDULER_shutdown ();
5675 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5678 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5680 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5681 "mesh", "max tunnels");
5682 GNUNET_SCHEDULER_shutdown ();
5687 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5690 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5692 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5693 "mesh", "default ttl", 64);
5698 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5701 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5702 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5703 "mesh", "max peers", 1000);
5708 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5715 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5716 "Mesh is running with drop mode enabled. "
5717 "This is NOT a good idea! "
5718 "Remove the DROP_PERCENT option from your configuration.\n");
5722 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5723 &dht_replication_level))
5725 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5727 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5728 "mesh", "dht replication level", 3);
5729 dht_replication_level = 3;
5732 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5733 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5734 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5735 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5737 dht_handle = GNUNET_DHT_connect (c, 64);
5738 if (NULL == dht_handle)
5742 stats = GNUNET_STATISTICS_create ("mesh", c);
5744 /* Scheduled the task to clean up when shutdown is called */
5745 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5747 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5748 GNUNET_free (keyfile);
5749 GNUNET_assert (NULL != pk);
5750 my_private_key = pk;
5751 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5752 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5753 &my_full_id.hashPubKey);
5754 myid = GNUNET_PEER_intern (&my_full_id);
5755 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5756 "Mesh for peer [%s] starting\n",
5757 GNUNET_i2s(&my_full_id));
5759 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5760 NULL, /* Closure passed to MESH functions */
5761 &core_init, /* Call core_init once connected */
5762 &core_connect, /* Handle connects */
5763 &core_disconnect, /* remove peers on disconnects */
5764 NULL, /* Don't notify about all incoming messages */
5765 GNUNET_NO, /* For header only in notification */
5766 NULL, /* Don't notify about all outbound messages */
5767 GNUNET_NO, /* For header-only out notification */
5768 core_handlers); /* Register these handlers */
5769 if (NULL == core_handle)
5772 GNUNET_SCHEDULER_shutdown ();
5776 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5777 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5783 * The main function for the mesh service.
5785 * @param argc number of arguments from the command line
5786 * @param argv command line arguments
5787 * @return 0 ok, 1 on error
5790 main (int argc, char *const *argv)
5795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5796 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5798 ret = (GNUNET_OK == r) ? 0 : 1;
5799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5803 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5804 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5805 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);