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 * - when sending in-order buffered data, wait for client ACKs
28 * - retransmit PATH_CREATE and PATH_ACK in case of need
31 * - relay corking down to core
32 * - set ttl relative to path length
40 #include "mesh_protocol.h"
41 #include "mesh_path.h"
42 #include "block_mesh.h"
43 #include "gnunet_dht_service.h"
44 #include "gnunet_statistics_service.h"
46 #define MESH_BLOOM_SIZE 128
48 #define MESH_DEBUG_DHT GNUNET_NO
49 #define MESH_DEBUG_CONNECTION GNUNET_NO
50 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
52 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
53 GNUNET_TIME_UNIT_MINUTES,\
55 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
56 #define MESH_RETRANSMIT_MARGIN 4
58 #if MESH_DEBUG_CONNECTION
59 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
61 #define DEBUG_CONN(...)
65 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
67 #define DEBUG_DHT(...)
74 struct timespec __mesh_start;
75 struct timespec __mesh_end;
76 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
77 #define INTERVAL_END \
79 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
80 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
81 if (__diff < 0) __diff += 1000000000;\
85 #define INTERVAL_SHOW \
87 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
89 #define INTERVAL_START
94 /******************************************************************************/
95 /************************ DATA STRUCTURES ****************************/
96 /******************************************************************************/
98 /** FWD declaration */
104 * Struct containing info about a queued transmission to this peer
111 struct MeshPeerQueue *next;
116 struct MeshPeerQueue *prev;
119 * Peer this transmission is directed to.
121 struct MeshPeerInfo *peer;
124 * Tunnel this message belongs to.
126 struct MeshTunnel *tunnel;
129 * Pointer to info stucture used as cls.
139 * Size of the message
146 * Struct containing all information regarding a given peer
156 * Last time we heard from this peer
158 struct GNUNET_TIME_Absolute last_contact;
161 * Number of attempts to reconnect so far
163 int n_reconnect_attempts;
166 * Paths to reach the peer, ordered by ascending hop count
168 struct MeshPeerPath *path_head;
171 * Paths to reach the peer, ordered by ascending hop count
173 struct MeshPeerPath *path_tail;
176 * Handle to stop the DHT search for a path to this peer
178 struct GNUNET_DHT_GetHandle *dhtget;
181 * Array of tunnels this peer is the target of.
182 * Most probably a small amount, therefore not a hashmap.
183 * When the path to the peer changes, notify these tunnels to let them
184 * re-adjust their path trees.
186 struct MeshTunnel **tunnels;
189 * Number of tunnels this peers participates in
191 unsigned int ntunnels;
194 * Transmission queue to core DLL head
196 struct MeshPeerQueue *queue_head;
199 * Transmission queue to core DLL tail
201 struct MeshPeerQueue *queue_tail;
204 * How many messages are in the queue to this peer.
206 unsigned int queue_n;
209 * Handle for queued transmissions
211 struct GNUNET_CORE_TransmitHandle *core_transmit;
216 * Struct to encapsulate all the Flow Control information to a peer in the
217 * context of a tunnel: Same peer in different tunnels will have independent
218 * flow control structures, allowing to choke/free tunnels according to its
221 struct MeshFlowControl
224 * ID of the last packet sent towards the peer.
226 uint32_t last_pid_sent;
229 * ID of the last packet received from the peer.
231 uint32_t last_pid_recv;
234 * Last ACK sent to the peer (peer can't send more than this PID).
236 uint32_t last_ack_sent;
239 * Last ACK sent towards the origin (for traffic towards leaf node).
241 uint32_t last_ack_recv;
244 * How many payload messages are in the queue towards this peer.
249 * Task to poll the peer in case of a lost ACK causes stall.
251 GNUNET_SCHEDULER_TaskIdentifier poll_task;
254 * How frequently to poll for ACKs.
256 struct GNUNET_TIME_Relative poll_time;
259 * On which tunnel to poll.
260 * Using an explicit poll_ctx would not help memory wise,
261 * since the allocated context would have to be stored in the
262 * fc struct in order to free it upon cancelling poll_task.
264 struct MeshTunnel *t;
269 * Globally unique tunnel identification (owner + number)
270 * DO NOT USE OVER THE NETWORK
275 * Node that owns the tunnel
280 * Tunnel number to differentiate all the tunnels owned by the node oid
281 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
283 MESH_TunnelNumber tid;
288 * Data needed for reliable tunnel endpoint retransmission management.
290 struct MeshTunnelReliability;
294 * Info needed to retry a message in case it gets lost.
296 struct MeshReliableMessage
299 * Double linked list, FIFO style
301 struct MeshReliableMessage *next;
302 struct MeshReliableMessage *prev;
305 * Tunnel Reliability queue this message is in.
307 struct MeshTunnelReliability *rel;
310 * ID of the message (ACK needed to free)
315 * When was this message issued (to calculate ACK delay) FIXME update with traffic
317 struct GNUNET_TIME_Absolute timestamp;
319 /* struct GNUNET_MESH_Data with payload */
323 struct MeshTunnelReliability
326 * Tunnel this is about.
328 struct MeshTunnel *t;
331 * DLL of messages sent and not yet ACK'd.
333 struct MeshReliableMessage *head_sent;
334 struct MeshReliableMessage *tail_sent;
347 * DLL of messages received out of order.
349 struct MeshReliableMessage *head_recv;
350 struct MeshReliableMessage *tail_recv;
358 * Task to resend/poll in case no ACK is received.
360 GNUNET_SCHEDULER_TaskIdentifier retry_task;
363 * Counter for exponential backoff.
365 struct GNUNET_TIME_Relative retry_timer;
368 * How long does it usually take to get an ACK. FIXME update with traffic
370 struct GNUNET_TIME_Relative expected_delay;
375 * Struct containing all information regarding a tunnel
376 * For an intermediate node the improtant info used will be:
377 * - id Tunnel unique identification
378 * - paths[0] To know where to send it next
379 * - metainfo: ready, speeds, accounting
386 struct MESH_TunnelID id;
389 * Port of the tunnel.
394 * State of the tunnel.
396 enum MeshTunnelState state;
399 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
401 MESH_TunnelNumber local_tid;
404 * Local tunnel number for local destination clients (incoming number)
405 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
408 MESH_TunnelNumber local_tid_dest;
411 * Is the tunnel bufferless (minimum latency)?
416 * Is the tunnel reliable?
421 * Force sending ACK? Flag to allow duplicate ACK on POLL.
426 * How many messages do we accept in the forward queue.
428 unsigned int queue_max;
431 * Last time the tunnel was used
433 struct GNUNET_TIME_Absolute timestamp;
436 * Destination of the tunnel.
441 * Next hop in the tunnel. If 0, @c client must be set.
443 GNUNET_PEER_Id next_hop;
446 * Previous hop in the tunnel. If 0, @c owner must be set.
448 GNUNET_PEER_Id prev_hop;
451 * Flow control information about @c next_hop or @c client.
453 struct MeshFlowControl next_fc;
456 * Flow control information about @c prev_hop or @c owner.
458 struct MeshFlowControl prev_fc;
461 * Client owner of the tunnel, if any
463 struct MeshClient *owner;
466 * Client destination of the tunnel, if any.
468 struct MeshClient *client;
471 * Task to keep the used paths alive at the owner,
472 * time tunnel out on all the other peers.
474 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
477 * Path being used for the tunnel.
479 struct MeshPeerPath *path;
482 * Flag to signal the destruction of the tunnel.
483 * If this is set GNUNET_YES the tunnel will be destroyed
484 * when the queue is empty.
489 * Total messages pending for this tunnels, payload or not.
491 unsigned int pending_messages;
495 * Only present (non-NULL) at the owner of a tunnel.
497 struct MeshTunnelReliability *fwd_rel;
501 * Only present (non-NULL) at the destination of a tunnel.
503 struct MeshTunnelReliability *bck_rel;
508 * Struct containing information about a client of the service
510 * TODO: add a list of 'waiting' ports
517 struct MeshClient *next;
522 struct MeshClient *prev;
525 * Tunnels that belong to this client, indexed by local id
527 struct GNUNET_CONTAINER_MultiHashMap32 *own_tunnels;
530 * Tunnels this client has accepted, indexed by incoming local id
532 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
535 * Handle to communicate with the client
537 struct GNUNET_SERVER_Client *handle;
540 * Ports that this client has declared interest in.
541 * Indexed by port, contains *Client.
543 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
546 * Whether the client is active or shutting down (don't send confirmations
547 * to a client that is shutting down.
552 * ID of the client, mainly for debug messages
559 /******************************************************************************/
560 /************************ DEBUG FUNCTIONS ****************************/
561 /******************************************************************************/
565 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
566 * @param cls string to print
567 * @param success GNUNET_OK if the PUT was transmitted,
568 * GNUNET_NO on timeout,
569 * GNUNET_SYSERR on disconnect from service
570 * after the PUT message was transmitted
571 * (so we don't know if it was received or not)
576 mesh_debug (void *cls, int success)
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
586 /******************************************************************************/
587 /*********************** GLOBAL VARIABLES ****************************/
588 /******************************************************************************/
590 /************************** Configuration parameters **************************/
593 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
595 static struct GNUNET_TIME_Relative refresh_path_time;
598 * How often to PUT own ID in the DHT.
600 static struct GNUNET_TIME_Relative id_announce_time;
603 * Maximum time allowed to connect to a peer found by string.
605 static struct GNUNET_TIME_Relative connect_timeout;
608 * Default TTL for payload packets.
610 static unsigned long long default_ttl;
613 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
615 static unsigned long long dht_replication_level;
618 * How many tunnels are we willing to maintain.
619 * Local tunnels are always allowed, even if there are more tunnels than max.
621 static unsigned long long max_tunnels;
624 * How many messages *in total* are we willing to queue, divided by number of
625 * tunnels to get tunnel queue size.
627 static unsigned long long max_msgs_queue;
630 * How many peers do we want to remember?
632 static unsigned long long max_peers;
635 * Percentage of messages that will be dropped (for test purposes only).
637 static unsigned long long drop_percent;
639 /*************************** Static global variables **************************/
642 * DLL with all the clients, head.
644 static struct MeshClient *clients_head;
647 * DLL with all the clients, tail.
649 static struct MeshClient *clients_tail;
652 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
654 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
657 * Number of tunnels known.
659 static unsigned long long n_tunnels;
662 * Tunnels incoming, indexed by MESH_TunnelNumber
663 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
665 static struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
668 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
670 static struct GNUNET_CONTAINER_MultiHashMap *peers;
673 * Handle to communicate with transport
675 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
678 * Handle to communicate with core.
680 static struct GNUNET_CORE_Handle *core_handle;
685 static struct GNUNET_DHT_Handle *dht_handle;
690 static struct GNUNET_SERVER_Handle *server_handle;
693 * Handle to the statistics service.
695 static struct GNUNET_STATISTICS_Handle *stats;
698 * Notification context, to send messages to local clients.
700 static struct GNUNET_SERVER_NotificationContext *nc;
703 * Local peer own ID (memory efficient handle).
705 static GNUNET_PEER_Id myid;
708 * Local peer own ID (full value).
710 static struct GNUNET_PeerIdentity my_full_id;
715 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
720 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
723 * Tunnel ID for the next created tunnel (global tunnel number).
725 static MESH_TunnelNumber next_tid;
728 * Tunnel ID for the next incoming tunnel (local tunnel number).
730 static MESH_TunnelNumber next_local_tid;
733 * All ports clients of this peer have opened.
735 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
738 * Task to periodically announce itself in the network.
740 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
743 * Next ID to assign to a client.
745 unsigned int next_client_id;
748 /******************************************************************************/
749 /*********************** DECLARATIONS **************************/
750 /******************************************************************************/
753 * Function to process paths received for a new peer addition. The recorded
754 * paths form the initial tunnel, which can be optimized later.
755 * Called on each result obtained for the DHT search.
758 * @param exp when will this value expire
759 * @param key key of the result
760 * @param type type of the result
761 * @param size number of bytes in data
762 * @param data pointer to the result data
765 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
766 const struct GNUNET_HashCode * key,
767 const struct GNUNET_PeerIdentity *get_path,
768 unsigned int get_path_length,
769 const struct GNUNET_PeerIdentity *put_path,
770 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
771 size_t size, const void *data);
775 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
776 * and insert it in the appropriate structures if the peer is not known yet.
778 * @param peer Full identity of the peer.
780 * @return Existing or newly created peer info.
782 static struct MeshPeerInfo *
783 peer_get (const struct GNUNET_PeerIdentity *peer);
787 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
788 * and insert it in the appropriate structures if the peer is not known yet.
790 * @param peer Short identity of the peer.
792 * @return Existing or newly created peer info.
794 static struct MeshPeerInfo *
795 peer_get_short (const GNUNET_PEER_Id peer);
799 * Build a PeerPath from the paths returned from the DHT, reversing the paths
800 * to obtain a local peer -> destination path and interning the peer ids.
802 * @return Newly allocated and created path
804 static struct MeshPeerPath *
805 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
806 unsigned int get_path_length,
807 const struct GNUNET_PeerIdentity *put_path,
808 unsigned int put_path_length);
812 * Adds a path to the peer_infos of all the peers in the path
814 * @param p Path to process.
815 * @param confirmed Whether we know if the path works or not.
818 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
823 * Search for a tunnel by global ID using full PeerIdentities.
825 * @param oid owner of the tunnel.
826 * @param tid global tunnel number.
828 * @return tunnel handler, NULL if doesn't exist.
830 static struct MeshTunnel *
831 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
835 * Notify a tunnel that a connection has broken that affects at least
838 * @param t Tunnel affected.
839 * @param p1 Peer that got disconnected from p2.
840 * @param p2 Peer that got disconnected from p1.
842 * @return Short ID of the peer disconnected (either p1 or p2).
843 * 0 if the tunnel remained unaffected.
845 static GNUNET_PEER_Id
846 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
851 * @brief Use the given path for the tunnel.
852 * Update the next and prev hops (and RCs).
853 * (Re)start the path refresh in case the tunnel is locally owned.
855 * @param t Tunnel to update.
856 * @param p Path to use.
859 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
862 * Tunnel is empty: destroy it.
864 * Notifies all participants (peers, cleints) about the destruction.
866 * @param t Tunnel to destroy.
869 tunnel_destroy_empty (struct MeshTunnel *t);
872 * @brief Queue and pass message to core when possible.
874 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
875 * and accounts for it. In case the queue is full, the message is dropped and
878 * Otherwise, message is treated as internal and allowed to go regardless of
881 * @param cls Closure (@c type dependant). It will be used by queue_send to
882 * build the message to be sent if not already prebuilt.
883 * @param type Type of the message, 0 for a raw message.
884 * @param size Size of the message.
885 * @param dst Neighbor to send message to.
886 * @param t Tunnel this message belongs to.
889 queue_add (void *cls, uint16_t type, size_t size,
890 struct MeshPeerInfo *dst, struct MeshTunnel *t);
894 * Free a transmission that was already queued with all resources
895 * associated to the request.
897 * @param queue Queue handler to cancel.
898 * @param clear_cls Is it necessary to free associated cls?
901 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
905 * @brief Get the next transmittable message from the queue.
907 * This will be the head, except in the case of being a data packet
908 * not allowed by the destination peer.
910 * @param peer Destination peer.
912 * @return The next viable MeshPeerQueue element to send to that peer.
913 * NULL when there are no transmittable messages.
915 struct MeshPeerQueue *
916 queue_get_next (const struct MeshPeerInfo *peer);
920 * Core callback to write a queued packet to core buffer
922 * @param cls Closure (peer info).
923 * @param size Number of bytes available in buf.
924 * @param buf Where the to write the message.
926 * @return number of bytes written to buf
929 queue_send (void *cls, size_t size, void *buf);
932 /******************************************************************************/
933 /************************ PERIODIC FUNCTIONS ****************************/
934 /******************************************************************************/
937 * Periodically announce self id in the DHT
940 * @param tc task context
943 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
947 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
949 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
953 * - Set data expiration in function of X
956 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
958 block.id = my_full_id;
959 GNUNET_DHT_put (dht_handle, /* DHT handle */
960 &my_full_id.hashPubKey, /* Key to use */
961 dht_replication_level, /* Replication level */
962 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
963 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
964 sizeof (block), /* Size of the data */
965 (const char *) &block, /* Data itself */
966 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
967 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
968 NULL, /* Continuation */
969 NULL); /* Continuation closure */
971 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
975 /******************************************************************************/
976 /****************** GENERAL HELPER FUNCTIONS ************************/
977 /******************************************************************************/
981 * Check if client has registered with the service and has not disconnected
983 * @param client the client to check
985 * @return non-NULL if client exists in the global DLL
987 static struct MeshClient *
988 client_get (struct GNUNET_SERVER_Client *client)
990 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
995 * Deletes a tunnel from a client (either owner or destination). To be used on
998 * @param c Client whose tunnel to delete.
999 * @param t Tunnel which should be deleted.
1002 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1006 GNUNET_assert (GNUNET_YES ==
1007 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
1013 GNUNET_assert (GNUNET_YES ==
1014 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
1021 * Notify the appropriate client that a new incoming tunnel was created.
1023 * @param t Tunnel that was created.
1026 send_client_tunnel_create (struct MeshTunnel *t)
1028 struct GNUNET_MESH_TunnelMessage msg;
1030 if (NULL == t->client)
1032 msg.header.size = htons (sizeof (msg));
1033 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1034 msg.tunnel_id = htonl (t->local_tid_dest);
1035 msg.port = htonl (t->port);
1037 msg.opt |= GNUNET_YES == t->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1038 msg.opt |= GNUNET_YES == t->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1039 msg.opt = htonl (msg.opt);
1040 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
1041 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1042 &msg.header, GNUNET_NO);
1047 * Notify dest client that the incoming tunnel is no longer valid.
1049 * @param t Tunnel that is destroyed.
1050 * @param fwd Forward notification (owner->dest)?
1053 send_client_tunnel_destroy (struct MeshTunnel *t, int fwd)
1055 struct GNUNET_MESH_TunnelMessage msg;
1056 struct MeshClient *c;
1058 c = fwd ? t->client : t->owner;
1064 msg.header.size = htons (sizeof (msg));
1065 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1066 msg.tunnel_id = htonl (fwd ? t->local_tid_dest : t->local_tid);
1067 msg.port = htonl (0);
1068 memset (&msg.peer, 0, sizeof (msg.peer));
1069 msg.opt = htonl (0);
1070 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1071 &msg.header, GNUNET_NO);
1076 * Iterator over all the peers to remove the oldest not-used entry.
1078 * @param cls Closure (unsued).
1079 * @param key ID of the peer.
1080 * @param value Peer_Info of the peer.
1085 peer_info_timeout (void *cls,
1086 const struct GNUNET_HashCode *key,
1093 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1094 * and insert it in the appropriate structures if the peer is not known yet.
1096 * @param peer Full identity of the peer.
1098 * @return Existing or newly created peer info.
1100 static struct MeshPeerInfo *
1101 peer_get (const struct GNUNET_PeerIdentity *peer)
1103 struct MeshPeerInfo *peer_info;
1105 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1106 if (NULL == peer_info)
1109 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1110 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1112 GNUNET_CONTAINER_multihashmap_iterate (peers,
1116 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1117 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1118 peer_info->id = GNUNET_PEER_intern (peer);
1120 peer_info->last_contact = GNUNET_TIME_absolute_get();
1127 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1128 * and insert it in the appropriate structures if the peer is not known yet.
1130 * @param peer Short identity of the peer.
1132 * @return Existing or newly created peer info.
1134 static struct MeshPeerInfo *
1135 peer_get_short (const GNUNET_PEER_Id peer)
1137 struct GNUNET_PeerIdentity id;
1139 GNUNET_PEER_resolve (peer, &id);
1140 return peer_get (&id);
1145 * Select which PID to POLL for, to compensate for lost messages.
1147 * @param pi Peer we want to poll.
1148 * @param t Tunnel about which we want to poll.
1150 * @return PID to use, either last sent or first_in_queue - 1
1153 peer_get_first_payload_pid (struct MeshPeerInfo *pi, struct MeshTunnel *t)
1155 struct MeshPeerQueue *q;
1158 type = pi->id == t->next_hop ? GNUNET_MESSAGE_TYPE_MESH_UNICAST :
1159 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN;
1161 for (q = pi->queue_head; NULL != q; q = q->next)
1163 if (q->type == type && q->tunnel == t)
1165 struct GNUNET_MESH_Data *msg = q->cls;
1167 /* Pretend that the last one sent was the previous to this */
1168 return ntohl (msg->pid) - 1;
1172 /* No data in queue, use last sent */
1174 struct MeshFlowControl *fc;
1176 fc = pi->id == t->next_hop ? &t->next_fc : &t->prev_fc;
1177 return fc->last_pid_sent;
1183 * Choose the best path towards a peer considering the tunnel properties.
1185 * @param peer The destination peer.
1186 * @param t The tunnel the path is for.
1188 * @return Best current known path towards the peer, if any.
1190 static struct MeshPeerPath *
1191 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1193 struct MeshPeerPath *best_p;
1194 struct MeshPeerPath *p;
1195 unsigned int best_cost;
1198 best_p = p = peer->path_head;
1199 best_cost = cost = p->length;
1202 if ((cost = p->length) < best_cost)
1213 * Core callback to write a pre-constructed data packet to core buffer
1215 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1216 * @param size Number of bytes available in buf.
1217 * @param buf Where the to write the message.
1219 * @return number of bytes written to buf
1222 send_core_data_raw (void *cls, size_t size, void *buf)
1224 struct GNUNET_MessageHeader *msg = cls;
1227 GNUNET_assert (NULL != msg);
1228 total_size = ntohs (msg->size);
1230 if (total_size > size)
1235 memcpy (buf, msg, total_size);
1242 * Sends an already built message to a peer, properly registrating
1243 * all used resources.
1245 * @param message Message to send. Function makes a copy of it.
1246 * @param peer Short ID of the neighbor whom to send the message.
1247 * @param t Tunnel on which this message is transmitted.
1250 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1251 GNUNET_PEER_Id peer,
1252 struct MeshTunnel *t)
1254 struct GNUNET_PeerIdentity id;
1255 struct MeshPeerInfo *neighbor;
1256 struct MeshPeerPath *p;
1261 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1266 size = ntohs (message->size);
1267 data = GNUNET_malloc (size);
1268 memcpy (data, message, size);
1269 type = ntohs(message->type);
1270 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1271 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1273 struct GNUNET_MESH_Data *u;
1275 u = (struct GNUNET_MESH_Data *) data;
1276 u->ttl = htonl (ntohl (u->ttl) - 1);
1278 GNUNET_PEER_resolve (peer, &id);
1279 neighbor = peer_get (&id);
1280 for (p = neighbor->path_head; NULL != p; p = p->next)
1290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1291 " %s IS NOT DIRECTLY CONNECTED\n",
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1296 for (p = neighbor->path_head; NULL != p; p = p->next)
1298 struct GNUNET_PeerIdentity debug_id;
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1302 " path with %u hops through:\n",
1304 for (i = 0; i < p->length; i++)
1306 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1309 i, GNUNET_i2s(&debug_id));
1313 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1314 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1315 " no direct connection to %s\n",
1320 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1331 * Sends a CREATE PATH message for a path to a peer, properly registrating
1332 * all used resources.
1334 * @param t Tunnel for which the path is created.
1337 send_create_path (struct MeshTunnel *t)
1339 struct MeshPeerInfo *neighbor;
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1342 neighbor = peer_get_short (t->next_hop);
1344 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1345 sizeof (struct GNUNET_MESH_CreateTunnel) +
1346 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1349 t->state = MESH_TUNNEL_WAITING;
1354 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1356 * @param t Tunnel which to confirm.
1359 send_path_ack (struct MeshTunnel *t)
1361 struct MeshPeerInfo *peer;
1363 peer = peer_get_short (t->prev_hop);
1366 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1367 sizeof (struct GNUNET_MESH_PathACK),
1374 * Try to establish a new connection to this peer in the given tunnel.
1375 * If the peer doesn't have any path to it yet, try to get one.
1376 * If the peer already has some path, send a CREATE PATH towards it.
1378 * @param peer PeerInfo of the peer.
1379 * @param t Tunnel for which to create the path, if possible.
1382 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1384 struct MeshPeerPath *p;
1386 if (NULL != peer->path_head)
1388 p = peer_get_best_path (peer, t);
1389 tunnel_use_path (t, p);
1390 send_create_path (t);
1392 else if (NULL == peer->dhtget)
1394 struct GNUNET_PeerIdentity id;
1396 GNUNET_PEER_resolve (peer->id, &id);
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1398 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1399 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1400 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1401 &id.hashPubKey, /* key to search */
1402 dht_replication_level, /* replication level */
1403 GNUNET_DHT_RO_RECORD_ROUTE |
1404 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1406 0, /* xquery bits */
1407 &dht_get_id_handler, peer);
1408 t->state = MESH_TUNNEL_SEARCHING;
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1413 "There is no path but the DHT GET is already started.\n");
1419 * Destroy the peer_info and free any allocated resources linked to it
1421 * @param pi The peer_info to destroy.
1423 * @return GNUNET_OK on success
1426 peer_info_destroy (struct MeshPeerInfo *pi)
1428 struct GNUNET_PeerIdentity id;
1429 struct MeshPeerPath *p;
1430 struct MeshPeerPath *nextp;
1433 GNUNET_PEER_resolve (pi->id, &id);
1434 GNUNET_PEER_change_rc (pi->id, -1);
1437 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1440 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1441 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1443 if (NULL != pi->dhtget)
1445 GNUNET_DHT_get_stop (pi->dhtget);
1451 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1455 for (i = 0; i < pi->ntunnels; i++)
1456 tunnel_destroy_empty (pi->tunnels[i]);
1457 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1464 * Remove all paths that rely on a direct connection between p1 and p2
1465 * from the peer itself and notify all tunnels about it.
1467 * @param peer PeerInfo of affected peer.
1468 * @param p1 GNUNET_PEER_Id of one peer.
1469 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1472 * TODO: optimize (see below)
1475 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1478 struct MeshPeerPath *p;
1479 struct MeshPeerPath *next;
1480 struct MeshPeerInfo *peer_d;
1482 unsigned int destroyed;
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1487 for (p = peer->path_head; NULL != p; p = next)
1490 for (i = 0; i < (p->length - 1); i++)
1492 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1493 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1495 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1505 for (i = 0; i < peer->ntunnels; i++)
1507 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1511 peer_d = peer_get_short (d);
1512 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1513 tunnel_use_path (peer->tunnels[i], next);
1514 peer_connect (peer_d, peer->tunnels[i]);
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1521 * Add the path to the peer and update the path used to reach it in case this
1524 * @param peer_info Destination peer to add the path to.
1525 * @param path New path to add. Last peer must be the peer in arg 1.
1526 * Path will be either used of freed if already known.
1527 * @param trusted Do we trust that this path is real?
1530 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1533 struct MeshPeerPath *aux;
1537 if ((NULL == peer_info) || (NULL == path))
1540 path_destroy (path);
1543 if (path->peers[path->length - 1] != peer_info->id)
1546 path_destroy (path);
1549 if (2 >= path->length && GNUNET_NO == trusted)
1551 /* Only allow CORE to tell us about direct paths */
1552 path_destroy (path);
1555 for (l = 1; l < path->length; l++)
1557 if (path->peers[l] == myid)
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1560 for (l2 = 0; l2 < path->length - l; l2++)
1562 path->peers[l2] = path->peers[l + l2];
1567 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1572 struct GNUNET_PeerIdentity id;
1574 GNUNET_PEER_resolve (peer_info->id, &id);
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1576 path->length, GNUNET_i2s (&id));
1579 l = path_get_length (path);
1582 path_destroy (path);
1586 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1587 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1589 l2 = path_get_length (aux);
1592 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1593 peer_info->path_tail, aux, path);
1598 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1600 path_destroy (path);
1605 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1612 * Add the path to the origin peer and update the path used to reach it in case
1613 * this is the shortest.
1614 * The path is given in peer_info -> destination, therefore we turn the path
1615 * upside down first.
1617 * @param peer_info Peer to add the path to, being the origin of the path.
1618 * @param path New path to add after being inversed.
1619 * Path will be either used or freed.
1620 * @param trusted Do we trust that this path is real?
1623 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1624 struct MeshPeerPath *path, int trusted)
1627 peer_info_add_path (peer_info, path, trusted);
1632 * Add a tunnel to the list of tunnels a peer participates in.
1633 * Update the tunnel's destination.
1635 * @param p Peer to add to.
1636 * @param t Tunnel to add.
1639 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1643 GNUNET_break (t->dest == p->id);
1647 GNUNET_PEER_change_rc (t->dest, 1);
1648 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1653 * Remove a tunnel from the list of tunnels a peer participates in.
1654 * Free the tunnel's destination.
1656 * @param p Peer to clean.
1657 * @param t Tunnel to remove.
1660 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1664 if (t->dest == p->id)
1666 GNUNET_PEER_change_rc (t->dest, -1);
1669 for (i = 0; i < p->ntunnels; i++)
1671 if (p->tunnels[i] == t)
1673 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1674 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1682 * Function called if the connection to the peer has been stalled for a while,
1683 * possibly due to a missed ACK. Poll the peer about its ACK status.
1685 * @param cls Closure (poll ctx).
1686 * @param tc TaskContext.
1689 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1691 struct MeshFlowControl *fc = cls;
1692 struct GNUNET_MESH_Poll msg;
1693 struct MeshTunnel *t = fc->t;
1694 GNUNET_PEER_Id peer;
1696 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1697 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1704 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1706 if (fc == &t->prev_fc)
1708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
1711 else if (fc == &t->next_fc)
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
1721 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1722 msg.header.size = htons (sizeof (msg));
1723 msg.tid = htonl (t->id.tid);
1724 msg.pid = htonl (peer_get_first_payload_pid (peer_get_short (peer), t));
1725 send_prebuilt_message (&msg.header, peer, t);
1726 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1727 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1733 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1734 * to obtain a local peer -> destination path and interning the peer ids.
1736 * @return Newly allocated and created path
1738 static struct MeshPeerPath *
1739 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1740 unsigned int get_path_length,
1741 const struct GNUNET_PeerIdentity *put_path,
1742 unsigned int put_path_length)
1744 struct MeshPeerPath *p;
1750 GNUNET_PEER_change_rc (myid, 1);
1751 i = get_path_length;
1752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1753 for (i--; i >= 0; i--)
1755 id = GNUNET_PEER_intern (&get_path[i]);
1756 if (p->length > 0 && id == p->peers[p->length - 1])
1758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1759 GNUNET_PEER_change_rc (id, -1);
1763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1764 GNUNET_i2s (&get_path[i]));
1766 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1767 p->peers[p->length - 1] = id;
1770 i = put_path_length;
1771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1772 for (i--; i >= 0; i--)
1774 id = GNUNET_PEER_intern (&put_path[i]);
1777 /* PUT path went through us, so discard the path up until now and start
1778 * from here to get a much shorter (and loop-free) path.
1783 if (p->length > 0 && id == p->peers[p->length - 1])
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1786 GNUNET_PEER_change_rc (id, -1);
1790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1791 GNUNET_i2s (&put_path[i]));
1793 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1794 p->peers[p->length - 1] = id;
1798 if (get_path_length > 0)
1799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1800 GNUNET_i2s (&get_path[0]));
1801 if (put_path_length > 0)
1802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1803 GNUNET_i2s (&put_path[0]));
1804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1806 for (i = 0; i < p->length; i++)
1808 struct GNUNET_PeerIdentity peer_id;
1810 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1812 GNUNET_i2s (&peer_id));
1820 * Adds a path to the peer_infos of all the peers in the path
1822 * @param p Path to process.
1823 * @param confirmed Whether we know if the path works or not.
1826 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1830 /* TODO: invert and add */
1831 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1832 for (i++; i < p->length; i++)
1834 struct MeshPeerInfo *aux;
1835 struct MeshPeerPath *copy;
1837 aux = peer_get_short (p->peers[i]);
1838 copy = path_duplicate (p);
1839 copy->length = i + 1;
1840 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1846 * Send keepalive packets for a peer
1848 * @param cls Closure (tunnel for which to send the keepalive).
1849 * @param tc Notification context.
1852 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1856 * Search for a tunnel among the incoming tunnels
1858 * @param tid the local id of the tunnel
1860 * @return tunnel handler, NULL if doesn't exist
1862 static struct MeshTunnel *
1863 tunnel_get_incoming (MESH_TunnelNumber tid)
1865 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1866 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
1871 * Search for a tunnel among the tunnels for a client
1873 * @param c the client whose tunnels to search in
1874 * @param tid the local id of the tunnel
1876 * @return tunnel handler, NULL if doesn't exist
1878 static struct MeshTunnel *
1879 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1881 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1883 return tunnel_get_incoming (tid);
1887 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI);
1888 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
1894 * Search for a tunnel by global ID using PEER_ID
1896 * @param pi owner of the tunnel
1897 * @param tid global tunnel number
1899 * @return tunnel handler, NULL if doesn't exist
1901 static struct MeshTunnel *
1902 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1904 struct MESH_TunnelID id;
1905 struct GNUNET_HashCode hash;
1910 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1911 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1916 * Search for a tunnel by global ID using full PeerIdentities
1918 * @param oid owner of the tunnel
1919 * @param tid global tunnel number
1921 * @return tunnel handler, NULL if doesn't exist
1923 static struct MeshTunnel *
1924 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1926 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1931 * Add a client to a tunnel, initializing all needed data structures.
1933 * @param t Tunnel to which add the client.
1934 * @param c Client which to add to the tunnel.
1937 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1939 if (NULL != t->client)
1945 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
1946 t->local_tid_dest, t,
1947 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1953 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
1954 t->local_tid_dest, t,
1955 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1965 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1967 unsigned int own_pos;
1969 for (own_pos = 0; own_pos < p->length; own_pos++)
1971 if (p->peers[own_pos] == myid)
1974 if (own_pos > p->length - 1)
1980 if (own_pos < p->length - 1)
1981 t->next_hop = p->peers[own_pos + 1];
1983 t->next_hop = p->peers[own_pos];
1984 GNUNET_PEER_change_rc (t->next_hop, 1);
1986 t->prev_hop = p->peers[own_pos - 1];
1988 t->prev_hop = p->peers[0];
1989 GNUNET_PEER_change_rc (t->prev_hop, 1);
1991 if (NULL != t->path)
1992 path_destroy (t->path);
1993 t->path = path_duplicate (p);
1996 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1997 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1998 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
2005 * Notifies a tunnel that a connection has broken that affects at least
2006 * some of its peers. Sends a notification towards the root of the tree.
2007 * In case the peer is the owner of the tree, notifies the client that owns
2008 * the tunnel and tries to reconnect.
2010 * @param t Tunnel affected.
2011 * @param p1 Peer that got disconnected from p2.
2012 * @param p2 Peer that got disconnected from p1.
2014 * @return Short ID of the peer disconnected (either p1 or p2).
2015 * 0 if the tunnel remained unaffected.
2017 static GNUNET_PEER_Id
2018 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2021 // if (myid != p1 && myid != p2) FIXME
2026 // if (tree_get_predecessor (t->tree) != 0)
2028 // /* We are the peer still connected, notify owner of the disconnection. */
2029 // struct GNUNET_MESH_PathBroken msg;
2030 // struct GNUNET_PeerIdentity neighbor;
2032 // msg.header.size = htons (sizeof (msg));
2033 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2034 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2035 // msg.tid = htonl (t->id.tid);
2036 // msg.peer1 = my_full_id;
2037 // GNUNET_PEER_resolve (pid, &msg.peer2);
2038 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2039 // send_prebuilt_message (&msg.header, &neighbor, t);
2046 * Build a local ACK message and send it to a local client.
2048 * @param t Tunnel on which to send the ACK.
2049 * @param c Client to whom send the ACK.
2050 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
2053 send_local_ack (struct MeshTunnel *t,
2054 struct MeshClient *c,
2057 struct GNUNET_MESH_LocalAck msg;
2059 msg.header.size = htons (sizeof (msg));
2060 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2061 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
2062 GNUNET_SERVER_notification_context_unicast (nc,
2069 * Build an ACK message and queue it to send to the given peer.
2071 * @param t Tunnel on which to send the ACK.
2072 * @param peer Peer to whom send the ACK.
2073 * @param ack Value of the ACK.
2076 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
2078 struct GNUNET_MESH_ACK msg;
2080 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2081 msg.header.size = htons (sizeof (msg));
2082 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2083 msg.pid = htonl (ack);
2084 msg.tid = htonl (t->id.tid);
2086 send_prebuilt_message (&msg.header, peer, t);
2091 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2093 * @param t Tunnel this is about.
2094 * @param fwd Is for FWD traffic? (ACK dest->owner)
2097 tunnel_send_data_ack (struct MeshTunnel *t, int fwd)
2099 struct GNUNET_MESH_DataACK msg;
2100 struct MeshTunnelReliability *rel;
2101 struct MeshReliableMessage *copy;
2106 rel = fwd ? t->bck_rel : t->fwd_rel;
2107 hop = fwd ? t->prev_hop : t->next_hop;
2108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2109 "send_data_ack for %u\n",
2112 if (GNUNET_NO == t->reliable)
2114 GNUNET_break_op (0);
2117 msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK :
2118 GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2119 msg.header.size = htons (sizeof (msg));
2120 msg.tid = htonl (t->id.tid);
2121 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2122 msg.mid = htonl (rel->mid_recv - 1);
2124 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2126 delta = copy->mid - rel->mid_recv;
2129 mask = 0x1LL << delta;
2130 msg.futures |= mask;
2131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2132 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2133 copy->mid, delta, mask, msg.futures);
2135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2137 send_prebuilt_message (&msg.header, hop, t);
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
2143 * Send an ACK informing the predecessor about the available buffer space.
2144 * In case there is no predecessor, inform the owning client.
2145 * If buffering is off, send only on behalf of children or self if endpoint.
2146 * If buffering is on, send when sent to children and buffer space is free.
2147 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2148 * the ACK itself goes "back" (towards root).
2150 * @param t Tunnel on which to send the ACK.
2151 * @param type Type of message that triggered the ACK transmission.
2152 * @param fwd Is this FWD ACK? (Going dest->owner)
2155 tunnel_send_ack (struct MeshTunnel *t, uint16_t type, int fwd)
2157 struct MeshTunnelReliability *rel;
2158 struct MeshFlowControl *next_fc;
2159 struct MeshFlowControl *prev_fc;
2160 struct MeshClient *c;
2161 struct MeshClient *o;
2167 rel = fwd ? t->fwd_rel : t->bck_rel;
2168 c = fwd ? t->client : t->owner;
2169 o = fwd ? t->owner : t->client;
2170 next_fc = fwd ? &t->next_fc : &t->prev_fc;
2171 prev_fc = fwd ? &t->prev_fc : &t->next_fc;
2172 hop = fwd ? t->prev_hop : t->next_hop;
2176 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2177 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2180 GNUNET_MESH_DEBUG_M2S (type));
2181 if (GNUNET_YES == t->nobuffer)
2183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2186 if (GNUNET_YES == t->reliable && NULL != c)
2187 tunnel_send_data_ack (t, fwd);
2189 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2190 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
2191 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2192 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2194 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2195 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2196 t->force_ack = GNUNET_YES;
2202 /* Check if we need to transmit the ACK */
2204 if (0 && NULL == o &&
2205 t->queue_max > next_fc->queue_n * 4 &&
2206 GMC_is_pid_bigger (prev_fc->last_ack_sent, prev_fc->last_pid_recv) &&
2207 GNUNET_NO == t->force_ack)
2209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2211 " t->qmax: %u, t->qn: %u\n",
2212 t->queue_max, next_fc->queue_n);
2213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2214 " t->pid: %u, t->ack: %u\n",
2215 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2219 /* Ok, ACK might be necessary, what PID to ACK? */
2220 delta = t->queue_max - next_fc->queue_n;
2221 if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent)
2222 delta_mid = rel->mid_sent - rel->head_sent->mid;
2225 if (0 > delta || (GNUNET_YES == t->reliable &&
2227 (10 < rel->n_sent || 64 <= delta_mid)))
2229 if (NULL != o && delta > 1)
2231 ack = prev_fc->last_pid_recv + delta;
2232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2234 " last pid %u, last ack %u, qmax %u, q %u\n",
2235 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2236 t->queue_max, next_fc->queue_n);
2237 if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack)
2239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2243 prev_fc->last_ack_sent = ack;
2245 send_local_ack (t, o, fwd);
2247 send_ack (t, hop, ack);
2249 GNUNET_break (GNUNET_YES == t->destroy);
2250 t->force_ack = GNUNET_NO;
2255 * Modify the mesh message TID from global to local and send to client.
2257 * @param t Tunnel on which to send the message.
2258 * @param msg Message to modify and send.
2259 * @param c Client to send to.
2260 * @param tid Tunnel ID to use (c can be both owner and client).
2263 tunnel_send_client_to_tid (struct MeshTunnel *t,
2264 const struct GNUNET_MESH_Data *msg,
2265 struct MeshClient *c, MESH_TunnelNumber tid)
2267 struct GNUNET_MESH_LocalData *copy;
2268 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2269 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2271 if (size < sizeof (struct GNUNET_MessageHeader))
2273 GNUNET_break_op (0);
2281 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2282 memcpy (©[1], &msg[1], size);
2283 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2284 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2285 copy->tid = htonl (tid);
2286 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2287 ©->header, GNUNET_NO);
2291 * Modify the unicast message TID from global to local and send to client.
2293 * @param t Tunnel on which to send the message.
2294 * @param msg Message to modify and send.
2295 * @param fwd Forward?
2298 tunnel_send_client_data (struct MeshTunnel *t,
2299 const struct GNUNET_MESH_Data *msg,
2303 tunnel_send_client_to_tid (t, msg, t->client, t->local_tid_dest);
2305 tunnel_send_client_to_tid (t, msg, t->owner, t->local_tid);
2310 * Send up to 64 buffered messages to the client for in order delivery.
2312 * @param t Tunnel on which to empty the message buffer.
2313 * @param c Client to send to.
2314 * @param rel Reliability structure to corresponding peer.
2315 * If rel == t->bck_rel, this is FWD data.
2318 tunnel_send_client_buffered_data (struct MeshTunnel *t, struct MeshClient *c,
2319 struct MeshTunnelReliability *rel)
2322 struct MeshReliableMessage *copy;
2323 struct MeshReliableMessage *next;
2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
2326 for (copy = rel->head_recv; NULL != copy; copy = next)
2329 if (copy->mid == rel->mid_recv)
2331 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
2333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2334 " have %u! now expecting %u\n",
2335 copy->mid, rel->mid_recv + 1);
2336 tunnel_send_client_data (t, msg, (rel == t->bck_rel));
2338 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2344 " don't have %u, next is %u\n",
2350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
2355 * We have received a message out of order, buffer it until we receive
2356 * the missing one and we can feed the rest to the client.
2358 * @param t Tunnel to add to.
2359 * @param msg Message to buffer.
2360 * @param rel Reliability data to the corresponding direction.
2363 tunnel_add_buffered_data (struct MeshTunnel *t,
2364 const struct GNUNET_MESH_Data *msg,
2365 struct MeshTunnelReliability *rel)
2367 struct MeshReliableMessage *copy;
2368 struct MeshReliableMessage *prev;
2372 size = ntohs (msg->header.size);
2373 mid = ntohl (msg->mid);
2374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
2376 copy = GNUNET_malloc (sizeof (*copy) + size);
2379 memcpy (©[1], msg, size);
2381 // FIXME do something better than O(n), although n < 64...
2382 // FIXME start from the end (most messages are the latest ones)
2383 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
2385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
2386 if (GMC_is_pid_bigger (prev->mid, mid))
2388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
2389 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
2394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
2395 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
2396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
2401 * Destroy a reliable message after it has been acknowledged, either by
2402 * direct mid ACK or bitfield. Updates the appropriate data structures and
2403 * timers and frees all memory.
2405 * @param copy Message that is no longer needed: remote peer got it.
2408 tunnel_free_reliable_message (struct MeshReliableMessage *copy)
2410 struct MeshTunnelReliability *rel;
2411 struct GNUNET_TIME_Relative time;
2414 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
2415 rel->expected_delay.rel_value *= 7;
2416 rel->expected_delay.rel_value += time.rel_value;
2417 rel->expected_delay.rel_value /= 8;
2419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
2420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
2421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
2422 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
2423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
2424 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
2426 rel->retry_timer = rel->expected_delay;
2427 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2433 * Mark future messages as ACK'd.
2435 * @param t Tunnel whose sent buffer to clean.
2436 * @param msg DataACK message with a bitfield of future ACK'd messages.
2437 * @param rel Reliability data.
2440 tunnel_free_sent_reliable (struct MeshTunnel *t,
2441 const struct GNUNET_MESH_DataACK *msg,
2442 struct MeshTunnelReliability *rel)
2444 struct MeshReliableMessage *copy;
2445 struct MeshReliableMessage *next;
2452 bitfield = msg->futures;
2453 mid = ntohl (msg->mid);
2454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2455 "free_sent_reliable %u %llX\n",
2457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2458 " rel %p, head %p\n",
2459 rel, rel->head_sent);
2460 for (i = 0, copy = rel->head_sent;
2461 i < 64 && NULL != copy && 0 != bitfield;
2464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2465 " trying bit %u (mid %u)\n",
2468 if (0 == (bitfield & mask))
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
2472 /* Bit was set, clear the bit from the bitfield */
2475 /* The i-th bit was set. Do we have that copy? */
2476 /* Skip copies with mid < target */
2477 target = mid + i + 1;
2478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
2479 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
2482 /* Did we run out of copies? (previously freed, it's ok) */
2485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
2489 /* Did we overshoot the target? (previously freed, it's ok) */
2490 if (GMC_is_pid_bigger (copy->mid, target))
2492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
2496 /* Now copy->mid == target, free it */
2498 tunnel_free_reliable_message (copy);
2501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
2506 * We haven't received an ACK after a certain time: restransmit the message.
2508 * @param cls Closure (MeshReliableMessage with the message to restransmit)
2509 * @param tc TaskContext.
2512 tunnel_retransmit_message (void *cls,
2513 const struct GNUNET_SCHEDULER_TaskContext *tc)
2515 struct MeshTunnelReliability *rel = cls;
2516 struct MeshReliableMessage *copy;
2517 struct MeshFlowControl *fc;
2518 struct MeshPeerQueue *q;
2519 struct MeshPeerInfo *pi;
2520 struct MeshTunnel *t;
2521 struct GNUNET_MESH_Data *payload;
2524 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2525 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2529 copy = rel->head_sent;
2536 /* Search the message to be retransmitted in the outgoing queue */
2537 payload = (struct GNUNET_MESH_Data *) ©[1];
2538 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
2539 fc = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc;
2540 pi = peer_get_short (hop);
2541 for (q = pi->queue_head; NULL != q; q = q->next)
2543 if (ntohs (payload->header.type) == q->type)
2545 struct GNUNET_MESH_Data *queued_data = q->cls;
2547 if (queued_data->mid == payload->mid)
2552 /* Message not found in the queue */
2555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
2557 fc->last_ack_sent++;
2558 fc->last_pid_recv++;
2559 payload->pid = htonl (fc->last_pid_recv);
2560 send_prebuilt_message (&payload->header, hop, t);
2561 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! STILL IN QUEUE %u\n", copy->mid);
2568 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
2569 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
2570 &tunnel_retransmit_message,
2576 * @brief Re-initiate traffic to this peer if necessary.
2578 * Check if there is traffic queued towards this peer
2579 * and the core transmit handle is NULL (traffic was stalled).
2580 * If so, call core tmt rdy.
2582 * @param peer_id Short ID of peer to which initiate traffic.
2585 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2587 struct MeshPeerInfo *peer;
2588 struct GNUNET_PeerIdentity id;
2589 struct MeshPeerQueue *q;
2592 peer = peer_get_short (peer_id);
2593 if (NULL != peer->core_transmit)
2596 q = queue_get_next (peer);
2599 /* Might br multicast traffic already sent to this particular peer but
2600 * not to other children in this tunnel.
2601 * This way t->queue_n would be > 0 but the queue of this particular peer
2607 GNUNET_PEER_resolve (peer->id, &id);
2608 peer->core_transmit =
2609 GNUNET_CORE_notify_transmit_ready(core_handle,
2612 GNUNET_TIME_UNIT_FOREVER_REL,
2622 * Send a message to all peers and clients in this tunnel that the tunnel
2623 * is no longer valid. If some peer or client should not receive the message,
2624 * should be zero'ed out before calling this function.
2626 * @param t The tunnel whose peers and clients to notify.
2629 tunnel_send_destroy (struct MeshTunnel *t)
2631 struct GNUNET_MESH_TunnelDestroy msg;
2632 struct GNUNET_PeerIdentity id;
2634 msg.header.size = htons (sizeof (msg));
2635 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2636 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2637 msg.tid = htonl (t->id.tid);
2638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2639 " sending tunnel destroy for tunnel: %s [%X]\n",
2640 GNUNET_i2s (&msg.oid), t->id.tid);
2642 if (NULL == t->client && 0 != t->next_hop)
2644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2645 GNUNET_PEER_resolve (t->next_hop, &id);
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2647 " sending forward to %s\n",
2649 send_prebuilt_message (&msg.header, t->next_hop, t);
2651 if (NULL == t->owner && 0 != t->prev_hop)
2653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2654 GNUNET_PEER_resolve (t->prev_hop, &id);
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2656 " sending back to %s\n",
2658 send_prebuilt_message (&msg.header, t->prev_hop, t);
2660 if (NULL != t->owner)
2662 send_client_tunnel_destroy (t, GNUNET_NO);
2664 if (NULL != t->client)
2666 send_client_tunnel_destroy (t, GNUNET_YES);
2672 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
2674 * @param t Tunnel which to cancel.
2675 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2678 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2680 struct MeshPeerInfo *peer_info;
2681 struct MeshPeerQueue *pq;
2682 struct MeshPeerQueue *next;
2685 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2686 peer_info = peer_get_short (neighbor);
2687 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2690 if (pq->tunnel == t)
2692 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2693 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2695 /* Should have been removed on destroy children */
2698 queue_destroy (pq, GNUNET_YES);
2701 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2703 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2704 peer_info->core_transmit = NULL;
2710 * Destroy the tunnel.
2712 * This function does not generate any warning traffic to clients or peers.
2715 * Remove the tunnel from peer_info's and clients' hashmaps.
2716 * Cancel messages belonging to this tunnel queued to neighbors.
2717 * Free any allocated resources linked to the tunnel.
2719 * @param t the tunnel to destroy
2721 * @return GNUNET_OK on success
2724 tunnel_destroy (struct MeshTunnel *t)
2726 struct MeshClient *c;
2727 struct GNUNET_HashCode hash;
2737 struct GNUNET_PeerIdentity id;
2739 GNUNET_PEER_resolve (t->id.oid, &id);
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2741 GNUNET_i2s (&id), t->id.tid);
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2747 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2748 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2757 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels, t->local_tid, t))
2764 if (NULL != t->client)
2768 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
2769 t->local_tid_dest, t))
2775 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
2776 t->local_tid_dest, t))
2783 if (0 != t->prev_hop)
2785 peer_cancel_queues (t->prev_hop, t);
2786 GNUNET_PEER_change_rc (t->prev_hop, -1);
2788 if (0 != t->next_hop)
2790 peer_cancel_queues (t->next_hop, t);
2791 GNUNET_PEER_change_rc (t->next_hop, -1);
2793 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task)
2795 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
2796 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
2798 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task)
2800 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
2801 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
2804 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2807 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2808 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2811 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2812 path_destroy (t->path);
2818 * Tunnel is empty: destroy it.
2820 * Notifies all participants (peers, cleints) about the destruction.
2822 * @param t Tunnel to destroy.
2825 tunnel_destroy_empty (struct MeshTunnel *t)
2829 struct GNUNET_PeerIdentity id;
2831 GNUNET_PEER_resolve (t->id.oid, &id);
2832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2833 "executing destruction of empty tunnel %s [%X]\n",
2834 GNUNET_i2s (&id), t->id.tid);
2838 if (GNUNET_NO == t->destroy)
2839 tunnel_send_destroy (t);
2840 if (0 == t->pending_messages)
2843 t->destroy = GNUNET_YES;
2847 * Initialize a Flow Control structure to the initial state.
2849 * @param fc Flow Control structure to initialize.
2852 fc_init (struct MeshFlowControl *fc)
2854 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2855 fc->last_pid_recv = (uint32_t) -1;
2856 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2857 fc->last_ack_recv = (uint32_t) -1;
2858 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2859 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2864 * Create a new tunnel
2866 * @param owner Who is the owner of the tunnel (short ID).
2867 * @param tid Tunnel Number of the tunnel.
2868 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2869 * @param local Tunnel Number for the tunnel, for the client point of view.
2871 * @return A new initialized tunnel. NULL on error.
2873 static struct MeshTunnel *
2874 tunnel_new (GNUNET_PEER_Id owner,
2875 MESH_TunnelNumber tid,
2876 struct MeshClient *client,
2877 MESH_TunnelNumber local)
2879 struct MeshTunnel *t;
2880 struct GNUNET_HashCode hash;
2882 if (n_tunnels >= max_tunnels && NULL == client)
2885 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2888 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2890 fc_init (&t->next_fc);
2891 fc_init (&t->prev_fc);
2892 t->local_tid = local;
2894 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2896 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2898 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2899 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2906 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2914 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
2916 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2920 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2930 * Set options in a tunnel, extracted from a bit flag field
2932 * @param t Tunnel to set options to.
2933 * @param options Bit array in host byte order.
2936 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
2938 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
2939 GNUNET_YES : GNUNET_NO;
2940 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
2941 GNUNET_YES : GNUNET_NO;
2946 * Iterator for deleting each tunnel whose client endpoint disconnected.
2948 * @param cls Closure (client that has disconnected).
2949 * @param key The local tunnel id (used to access the hashmap).
2950 * @param value The value stored at the key (tunnel to destroy).
2952 * @return GNUNET_OK, keep iterating.
2955 tunnel_destroy_iterator (void *cls,
2959 struct MeshTunnel *t = value;
2960 struct MeshClient *c = cls;
2962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2963 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2964 t->local_tid, t->local_tid_dest, c->id);
2965 client_delete_tunnel (c, t);
2968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2970 if (0 != t->next_hop) { /* destroy could come before a path is used */
2971 GNUNET_PEER_change_rc (t->next_hop, -1);
2977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2979 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2980 GNUNET_PEER_change_rc (t->prev_hop, -1);
2985 tunnel_destroy_empty (t);
2992 * Timeout function, destroys tunnel if called
2994 * @param cls Closure (tunnel to destroy).
2995 * @param tc TaskContext
2998 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3000 struct MeshTunnel *t = cls;
3001 struct GNUNET_PeerIdentity id;
3003 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3004 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3006 GNUNET_PEER_resolve(t->id.oid, &id);
3007 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3008 "Tunnel %s [%X] timed out. Destroying.\n",
3009 GNUNET_i2s(&id), t->id.tid);
3010 if (NULL != t->client)
3011 send_client_tunnel_destroy (t, GNUNET_YES);
3012 tunnel_destroy (t); /* Do not notify other */
3017 * Resets the tunnel timeout. Starts it if no timeout was running.
3019 * @param t Tunnel whose timeout to reset.
3020 * @param fwd Is this forward?
3022 * TODO use heap to improve efficiency of scheduler.
3023 * FIXME use fwd, keep 2 timers
3026 tunnel_reset_timeout (struct MeshTunnel *t, int fwd)
3028 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
3030 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
3031 GNUNET_SCHEDULER_cancel (t->maintenance_task);
3032 t->maintenance_task =
3033 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3034 (refresh_path_time, 4), &tunnel_timeout, t);
3038 /******************************************************************************/
3039 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3040 /******************************************************************************/
3043 * Function to send a create path packet to a peer.
3045 * @param cls closure
3046 * @param size number of bytes available in buf
3047 * @param buf where the callee should write the message
3048 * @return number of bytes written to buf
3051 send_core_path_create (void *cls, size_t size, void *buf)
3053 struct MeshTunnel *t = cls;
3054 struct GNUNET_MESH_CreateTunnel *msg;
3055 struct GNUNET_PeerIdentity *peer_ptr;
3056 struct MeshPeerPath *p = t->path;
3061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3063 sizeof (struct GNUNET_MESH_CreateTunnel) +
3064 p->length * sizeof (struct GNUNET_PeerIdentity);
3066 if (size < size_needed || NULL == buf)
3071 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
3072 msg->header.size = htons (size_needed);
3073 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3074 msg->tid = ntohl (t->id.tid);
3077 if (GNUNET_YES == t->nobuffer)
3078 opt |= GNUNET_MESH_OPTION_NOBUFFER;
3079 if (GNUNET_YES == t->reliable)
3080 opt |= GNUNET_MESH_OPTION_RELIABLE;
3081 msg->opt = htonl (opt);
3082 msg->port = htonl (t->port);
3084 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3085 for (i = 0; i < p->length; i++)
3087 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3091 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3097 * Creates a path ack message in buf and frees all unused resources.
3099 * @param cls closure (MeshTransmissionDescriptor)
3100 * @param size number of bytes available in buf
3101 * @param buf where the callee should write the message
3102 * @return number of bytes written to buf
3105 send_core_path_ack (void *cls, size_t size, void *buf)
3107 struct MeshTunnel *t = cls;
3108 struct GNUNET_MESH_PathACK *msg = buf;
3110 GNUNET_assert (NULL != t);
3111 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3116 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
3117 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3118 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3119 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
3120 msg->tid = htonl (t->id.tid);
3121 msg->peer_id = my_full_id;
3122 msg->ack = htonl (t->prev_fc.last_ack_sent);
3124 /* TODO add signature */
3126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3127 return sizeof (struct GNUNET_MESH_PathACK);
3132 * Free a transmission that was already queued with all resources
3133 * associated to the request.
3135 * @param queue Queue handler to cancel.
3136 * @param clear_cls Is it necessary to free associated cls?
3139 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3141 struct MeshFlowControl *fc;
3143 if (GNUNET_YES == clear_cls)
3145 switch (queue->type)
3147 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3148 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3149 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3151 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3152 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3153 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3154 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3155 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3156 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3157 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3159 " prebuilt message\n");
3160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3162 GNUNET_MESH_DEBUG_M2S (queue->type));
3164 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3170 " type %s unknown!\n",
3171 GNUNET_MESH_DEBUG_M2S (queue->type));
3173 GNUNET_free_non_null (queue->cls);
3175 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3176 queue->peer->queue_tail,
3179 /* Delete from appropriate fc in the tunnel */
3180 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
3181 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
3183 if (queue->peer->id == queue->tunnel->prev_hop)
3184 fc = &queue->tunnel->prev_fc;
3185 else if (queue->peer->id == queue->tunnel->next_hop)
3186 fc = &queue->tunnel->next_fc;
3190 GNUNET_free (queue);
3195 GNUNET_free (queue);
3200 * @brief Get the next transmittable message from the queue.
3202 * This will be the head, except in the case of being a data packet
3203 * not allowed by the destination peer.
3205 * @param peer Destination peer.
3207 * @return The next viable MeshPeerQueue element to send to that peer.
3208 * NULL when there are no transmittable messages.
3210 struct MeshPeerQueue *
3211 queue_get_next (const struct MeshPeerInfo *peer)
3213 struct MeshPeerQueue *q;
3215 struct GNUNET_MESH_Data *dmsg;
3216 struct MeshTunnel* t;
3220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
3221 for (q = peer->queue_head; NULL != q; q = q->next)
3224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3226 GNUNET_MESH_DEBUG_M2S (q->type));
3227 dmsg = (struct GNUNET_MESH_Data *) q->cls;
3228 pid = ntohl (dmsg->pid);
3231 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3232 ack = t->next_fc.last_ack_recv;
3234 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3235 ack = t->prev_fc.last_ack_recv;
3238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3243 "* ACK: %u, PID: %u, MID: %u\n",
3244 ack, pid, ntohl (dmsg->mid));
3245 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3258 "* nothing found\n");
3264 queue_send (void *cls, size_t size, void *buf)
3266 struct MeshPeerInfo *peer = cls;
3267 struct GNUNET_MessageHeader *msg;
3268 struct MeshPeerQueue *queue;
3269 struct MeshTunnel *t;
3270 struct GNUNET_PeerIdentity dst_id;
3271 struct MeshFlowControl *fc;
3276 peer->core_transmit = NULL;
3278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3279 queue = queue_get_next (peer);
3281 /* Queue has no internal mesh traffic nor sendable payload */
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3285 if (NULL == peer->queue_head)
3286 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3291 GNUNET_PEER_resolve (peer->id, &dst_id);
3292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3294 GNUNET_i2s (&dst_id));
3295 /* Check if buffer size is enough for the message */
3296 if (queue->size > size)
3298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3299 "* not enough room, reissue\n");
3300 peer->core_transmit =
3301 GNUNET_CORE_notify_transmit_ready (core_handle,
3304 GNUNET_TIME_UNIT_FOREVER_REL,
3311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3314 GNUNET_assert (0 < t->pending_messages);
3315 t->pending_messages--;
3319 switch (queue->type)
3322 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3323 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3324 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3325 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3326 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3327 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3328 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3329 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3332 GNUNET_MESH_DEBUG_M2S (queue->type));
3334 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3335 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3336 data_size = send_core_data_raw (queue->cls, size, buf);
3337 msg = (struct GNUNET_MessageHeader *) buf;
3338 type = ntohs (msg->type);
3340 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3342 data_size = send_core_path_create (queue->cls, size, buf);
3344 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3346 data_size = send_core_path_ack (queue->cls, size, buf);
3350 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3351 "* type unknown: %u\n",
3356 if (0 < drop_percent &&
3357 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3359 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3360 "Dropping message of type %s\n",
3361 GNUNET_MESH_DEBUG_M2S(queue->type));
3364 /* Free queue, but cls was freed by send_core_* */
3365 queue_destroy (queue, GNUNET_NO);
3367 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3368 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3372 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3373 t->next_fc.last_pid_sent = pid;
3374 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, GNUNET_YES);
3375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3377 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3379 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3380 t->prev_fc.last_pid_sent = pid;
3381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3383 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3384 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, GNUNET_NO);
3390 /* If more data in queue, send next */
3391 queue = queue_get_next (peer);
3394 struct GNUNET_PeerIdentity id;
3396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3397 GNUNET_PEER_resolve (peer->id, &id);
3398 peer->core_transmit =
3399 GNUNET_CORE_notify_transmit_ready(core_handle,
3402 GNUNET_TIME_UNIT_FOREVER_REL,
3408 if (peer->id == t->next_hop)
3410 else if (peer->id == t->prev_hop)
3415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id: %u, next: %u, prev: %u\n",
3416 peer->id, t->next_hop, t->prev_hop);
3419 if (NULL != peer->queue_head)
3421 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task && fc->queue_n > 0)
3423 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3424 "* %s starting poll timeout\n",
3425 GNUNET_i2s (&my_full_id));
3427 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3433 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3435 GNUNET_SCHEDULER_cancel (fc->poll_task);
3436 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3439 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3450 * @brief Queue and pass message to core when possible.
3452 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3453 * accounts for it. In case the queue is full, the message is dropped and
3456 * Otherwise, message is treated as internal and allowed to go regardless of
3459 * @param cls Closure (@c type dependant). It will be used by queue_send to
3460 * build the message to be sent if not already prebuilt.
3461 * @param type Type of the message, 0 for a raw message.
3462 * @param size Size of the message.
3463 * @param dst Neighbor to send message to.
3464 * @param t Tunnel this message belongs to.
3467 queue_add (void *cls, uint16_t type, size_t size,
3468 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3470 struct MeshPeerQueue *queue;
3471 struct GNUNET_PeerIdentity id;
3472 struct MeshFlowControl *fc;
3476 priority = GNUNET_NO;
3477 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3481 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3487 if (fc->queue_n >= t->queue_max)
3489 /* If this isn't a retransmission, drop the message */
3490 if (GNUNET_NO == t->reliable ||
3491 (NULL == t->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) ||
3492 (NULL == t->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type))
3494 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3498 "queue full: %u/%u\n",
3499 fc->queue_n, t->queue_max);
3500 return; /* Drop this message */
3502 priority = GNUNET_YES;
3505 if (GMC_is_pid_bigger(fc->last_pid_sent + 1, fc->last_ack_recv) &&
3506 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3507 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3511 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3517 if (GNUNET_YES == priority)
3519 struct GNUNET_MESH_Data *d;
3523 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
3524 d = (struct GNUNET_MESH_Data *) queue->cls;
3526 for (queue = dst->queue_tail; NULL != queue; queue = queue->prev)
3528 if (queue->type != type)
3530 d = (struct GNUNET_MESH_Data *) queue->cls;
3537 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3539 if (NULL == dst->core_transmit)
3541 GNUNET_PEER_resolve (dst->id, &id);
3542 dst->core_transmit =
3543 GNUNET_CORE_notify_transmit_ready (core_handle,
3546 GNUNET_TIME_UNIT_FOREVER_REL,
3552 t->pending_messages++;
3556 /******************************************************************************/
3557 /******************** MESH NETWORK HANDLERS **************************/
3558 /******************************************************************************/
3562 * Core handler for path creation
3564 * @param cls closure
3565 * @param message message
3566 * @param peer peer identity this notification is about
3568 * @return GNUNET_OK to keep the connection open,
3569 * GNUNET_SYSERR to close it (signal serious error)
3572 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3573 const struct GNUNET_MessageHeader *message)
3575 unsigned int own_pos;
3578 MESH_TunnelNumber tid;
3579 struct GNUNET_MESH_CreateTunnel *msg;
3580 struct GNUNET_PeerIdentity *pi;
3581 struct MeshPeerPath *path;
3582 struct MeshPeerInfo *dest_peer_info;
3583 struct MeshPeerInfo *orig_peer_info;
3584 struct MeshTunnel *t;
3586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3587 "Received a path create msg [%s]\n",
3588 GNUNET_i2s (&my_full_id));
3589 size = ntohs (message->size);
3590 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3592 GNUNET_break_op (0);
3596 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3597 if (size % sizeof (struct GNUNET_PeerIdentity))
3599 GNUNET_break_op (0);
3602 size /= sizeof (struct GNUNET_PeerIdentity);
3605 GNUNET_break_op (0);
3608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3609 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3611 tid = ntohl (msg->tid);
3612 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3614 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3615 t = tunnel_get (pi, tid);
3616 if (NULL == t) /* might be a local tunnel */
3620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3621 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3627 t->port = ntohl (msg->port);
3628 opt = ntohl (msg->opt);
3629 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3631 t->nobuffer = GNUNET_YES;
3634 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3636 t->reliable = GNUNET_YES;
3638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3640 tunnel_reset_timeout (t, GNUNET_YES); // FIXME
3642 t->state = MESH_TUNNEL_WAITING;
3644 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3645 if (NULL == dest_peer_info)
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3648 " Creating PeerInfo for destination.\n");
3649 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3650 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3651 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3653 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3655 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3656 if (NULL == orig_peer_info)
3658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3659 " Creating PeerInfo for origin.\n");
3660 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3661 orig_peer_info->id = GNUNET_PEER_intern (pi);
3662 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3663 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3666 path = path_new (size);
3668 for (i = 0; i < size; i++)
3670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3671 GNUNET_i2s (&pi[i]));
3672 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3673 if (path->peers[i] == myid)
3676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3677 if (own_pos == 0 && path->peers[own_pos] != myid)
3679 /* create path: self not found in path through self */
3680 GNUNET_break_op (0);
3681 path_destroy (path);
3685 path_add_to_peers (path, GNUNET_NO);
3686 tunnel_use_path (t, path);
3688 peer_info_add_tunnel (dest_peer_info, t);
3690 if (own_pos == size - 1)
3692 struct MeshClient *c;
3694 /* Find target client */
3695 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
3698 /* TODO send reject */
3702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3703 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3705 /* Assign local tid */
3706 while (NULL != tunnel_get_incoming (next_local_tid))
3707 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3708 t->local_tid_dest = next_local_tid++;
3709 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3711 if (GNUNET_YES == t->reliable)
3713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
3714 t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
3716 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
3719 tunnel_add_client (t, c);
3720 send_client_tunnel_create (t);
3725 struct MeshPeerPath *path2;
3727 t->next_hop = path->peers[own_pos + 1];
3728 GNUNET_PEER_change_rc(t->next_hop, 1);
3730 /* It's for somebody else! Retransmit. */
3731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3732 path2 = path_duplicate (path);
3733 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3734 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3735 send_create_path (t);
3743 * Core handler for path ACKs
3745 * @param cls closure
3746 * @param message message
3747 * @param peer peer identity this notification is about
3749 * @return GNUNET_OK to keep the connection open,
3750 * GNUNET_SYSERR to close it (signal serious error)
3753 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3754 const struct GNUNET_MessageHeader *message)
3756 struct GNUNET_MESH_PathACK *msg;
3757 struct MeshPeerInfo *peer_info;
3758 struct MeshPeerPath *p;
3759 struct MeshTunnel *t;
3761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3762 GNUNET_i2s (&my_full_id));
3763 msg = (struct GNUNET_MESH_PathACK *) message;
3764 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3767 /* TODO notify that we don't know the tunnel */
3768 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3770 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3774 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3776 peer_info = peer_get (&msg->peer_id);
3777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3778 GNUNET_i2s (&msg->peer_id));
3779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3782 /* Add path to peers? */
3786 path_add_to_peers (p, GNUNET_YES);
3792 t->state = MESH_TUNNEL_READY;
3793 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3794 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3796 /* Message for us? */
3797 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3800 if (NULL == t->owner)
3802 GNUNET_break_op (0);
3805 if (NULL != peer_info->dhtget)
3807 GNUNET_DHT_get_stop (peer_info->dhtget);
3808 peer_info->dhtget = NULL;
3810 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_YES);
3811 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_NO);
3815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3816 " not for us, retransmitting...\n");
3817 peer_info = peer_get (&msg->oid);
3818 send_prebuilt_message (message, t->prev_hop, t);
3824 * Core handler for notifications of broken paths
3826 * @param cls closure
3827 * @param message message
3828 * @param peer peer identity this notification is about
3830 * @return GNUNET_OK to keep the connection open,
3831 * GNUNET_SYSERR to close it (signal serious error)
3834 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3835 const struct GNUNET_MessageHeader *message)
3837 struct GNUNET_MESH_PathBroken *msg;
3838 struct MeshTunnel *t;
3840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3841 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3842 msg = (struct GNUNET_MESH_PathBroken *) message;
3843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3844 GNUNET_i2s (&msg->peer1));
3845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3846 GNUNET_i2s (&msg->peer2));
3847 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3850 GNUNET_break_op (0);
3853 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3854 GNUNET_PEER_search (&msg->peer2));
3861 * Core handler for tunnel destruction
3863 * @param cls closure
3864 * @param message message
3865 * @param peer peer identity this notification is about
3867 * @return GNUNET_OK to keep the connection open,
3868 * GNUNET_SYSERR to close it (signal serious error)
3871 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3872 const struct GNUNET_MessageHeader *message)
3874 struct GNUNET_MESH_TunnelDestroy *msg;
3875 struct MeshTunnel *t;
3877 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3879 "Got a TUNNEL DESTROY packet from %s\n",
3881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3882 " for tunnel %s [%u]\n",
3883 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3884 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3887 /* Probably already got the message from another path,
3888 * destroyed the tunnel and retransmitted to children.
3891 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3895 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3898 t->local_tid, t->local_tid_dest);
3900 if (GNUNET_PEER_search (peer) == t->prev_hop)
3902 // TODO check owner's signature
3903 // TODO add owner's signatue to tunnel for retransmission
3904 peer_cancel_queues (t->prev_hop, t);
3905 GNUNET_PEER_change_rc (t->prev_hop, -1);
3908 else if (GNUNET_PEER_search (peer) == t->next_hop)
3910 // TODO check dest's signature
3911 // TODO add dest's signatue to tunnel for retransmission
3912 peer_cancel_queues (t->next_hop, t);
3913 GNUNET_PEER_change_rc (t->next_hop, -1);
3918 GNUNET_break_op (0);
3919 // TODO check both owner AND destination's signature to see which matches
3920 // TODO restransmit in appropriate direction
3923 tunnel_destroy_empty (t);
3925 // TODO: add timeout to destroy the tunnel anyway
3931 * Generic handler for mesh network payload traffic.
3933 * @param peer Peer identity this notification is about.
3934 * @param message Data message.
3935 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
3937 * @return GNUNET_OK to keep the connection open,
3938 * GNUNET_SYSERR to close it (signal serious error)
3941 handle_mesh_data (const struct GNUNET_PeerIdentity *peer,
3942 const struct GNUNET_MessageHeader *message,
3945 struct GNUNET_MESH_Data *msg;
3946 struct MeshFlowControl *fc;
3947 struct MeshTunnelReliability *rel;
3948 struct MeshTunnel *t;
3949 struct MeshClient *c;
3957 size = ntohs (message->size);
3959 sizeof (struct GNUNET_MESH_Data) +
3960 sizeof (struct GNUNET_MessageHeader))
3965 type =ntohs (message->type);
3966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
3967 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
3968 msg = (struct GNUNET_MESH_Data *) message;
3969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
3970 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3972 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3975 /* TODO notify back: we don't know this tunnel */
3976 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3977 GNUNET_break_op (0);
3981 /* Initialize FWD/BCK data */
3982 pid = ntohl (msg->pid);
3983 fc = fwd ? &t->prev_fc : &t->next_fc;
3984 c = fwd ? t->client : t->owner;
3985 rel = fwd ? t->bck_rel : t->fwd_rel;
3986 hop = fwd ? t->next_hop : t->prev_hop;
3987 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
3989 GNUNET_STATISTICS_update (stats, "# unsolicited data", 1, GNUNET_NO);
3990 GNUNET_break_op (0);
3991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3992 "Received PID %u, (prev %u), ACK %u\n",
3993 pid, fc->last_pid_recv, fc->last_ack_sent);
3997 tunnel_reset_timeout (t, fwd);
4000 /* TODO signature verification */
4001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us! sending to client\n");
4002 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4003 if (GMC_is_pid_bigger (pid, fc->last_pid_recv))
4007 mid = ntohl (msg->mid);
4008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4009 " pid %u (mid %u) not seen yet\n", pid, mid);
4010 fc->last_pid_recv = pid;
4012 if (GNUNET_NO == t->reliable ||
4013 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
4014 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
4016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4017 "!!! RECV %u\n", ntohl (msg->mid));
4018 if (GNUNET_YES == t->reliable)
4020 /* Is this the exact next expected messasge? */
4021 if (mid == rel->mid_recv)
4023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4025 tunnel_send_client_data (t, msg, fwd);
4026 tunnel_send_client_buffered_data (t, c, rel);
4030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4031 tunnel_add_buffered_data (t, msg, rel);
4034 else /* Tunnel unreliable, send to clients directly */
4036 tunnel_send_client_data (t, msg, fwd);
4041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4042 " MID %u not expected (%u - %u), dropping!\n",
4043 ntohl (msg->mid), rel->mid_recv, rel->mid_recv + 64);
4048 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4050 " Pid %u not expected (%u+), dropping!\n",
4051 pid, fc->last_pid_recv + 1);
4053 tunnel_send_ack (t, type, fwd);
4056 fc->last_pid_recv = pid;
4059 GNUNET_STATISTICS_update (stats, "# data on dying tunnel", 1, GNUNET_NO);
4060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "data on dying tunnel %s[%X]\n",
4061 GNUNET_PEER_resolve2 (t->id.oid), ntohl (msg->tid));
4062 return GNUNET_OK; /* Next hop has destoyed the tunnel, drop */
4064 ttl = ntohl (msg->ttl);
4065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4068 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4069 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4070 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, fwd);
4076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4077 send_prebuilt_message (message, hop, t);
4078 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4085 * Core handler for mesh network traffic going from the origin to a peer
4087 * @param cls Closure (unused).
4088 * @param message Message received.
4089 * @param peer Peer who sent the message.
4091 * @return GNUNET_OK to keep the connection open,
4092 * GNUNET_SYSERR to close it (signal serious error)
4095 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4096 const struct GNUNET_MessageHeader *message)
4098 return handle_mesh_data (peer, message, GNUNET_YES);
4102 * Core handler for mesh network traffic towards the owner of a tunnel.
4104 * @param cls Closure (unused).
4105 * @param message Message received.
4106 * @param peer Peer who sent the message.
4108 * @return GNUNET_OK to keep the connection open,
4109 * GNUNET_SYSERR to close it (signal serious error)
4112 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4113 const struct GNUNET_MessageHeader *message)
4115 return handle_mesh_data (peer, message, GNUNET_NO);
4120 * Core handler for mesh network traffic end-to-end ACKs.
4122 * @param cls Closure.
4123 * @param message Message.
4124 * @param peer Peer identity this notification is about.
4126 * @return GNUNET_OK to keep the connection open,
4127 * GNUNET_SYSERR to close it (signal serious error)
4130 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4131 const struct GNUNET_MessageHeader *message)
4133 struct GNUNET_MESH_DataACK *msg;
4134 struct MeshTunnelReliability *rel;
4135 struct MeshReliableMessage *copy;
4136 struct MeshReliableMessage *next;
4137 struct MeshTunnel *t;
4143 type = ntohs (message->type);
4144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
4145 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4146 msg = (struct GNUNET_MESH_DataACK *) message;
4148 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4151 /* TODO notify that we dont know this tunnel (whom)? */
4152 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4155 ack = ntohl (msg->mid);
4156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4158 /* Is this a forward or backward ACK? */
4159 id = GNUNET_PEER_search (peer);
4160 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4163 if (NULL == t->owner)
4165 send_prebuilt_message (message, t->prev_hop, t);
4170 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4173 if (NULL == t->client)
4175 send_prebuilt_message (message, t->next_hop, t);
4182 GNUNET_break_op (0);
4186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %u\n", ack);
4187 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4189 if (GMC_is_pid_bigger (copy->mid, ack))
4191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4192 tunnel_free_sent_reliable (t, msg, rel);
4196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4198 tunnel_free_reliable_message (copy);
4200 /* Once buffers have been free'd, send ACK */
4201 tunnel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4203 /* If some message was free'd, update the retransmission delay*/
4204 if (GNUNET_YES == work)
4206 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4208 GNUNET_SCHEDULER_cancel (rel->retry_task);
4209 if (NULL == rel->head_sent)
4211 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4215 struct GNUNET_TIME_Absolute new_target;
4216 struct GNUNET_TIME_Relative delay;
4218 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4219 MESH_RETRANSMIT_MARGIN);
4220 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4222 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4224 GNUNET_SCHEDULER_add_delayed (delay,
4225 &tunnel_retransmit_message,
4236 * Core handler for mesh network traffic point-to-point acks.
4238 * @param cls closure
4239 * @param message message
4240 * @param peer peer identity this notification is about
4242 * @return GNUNET_OK to keep the connection open,
4243 * GNUNET_SYSERR to close it (signal serious error)
4246 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4247 const struct GNUNET_MessageHeader *message)
4249 struct GNUNET_MESH_ACK *msg;
4250 struct MeshTunnel *t;
4251 struct MeshFlowControl *fc;
4255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4257 msg = (struct GNUNET_MESH_ACK *) message;
4259 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4263 /* TODO notify that we dont know this tunnel (whom)? */
4264 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4267 ack = ntohl (msg->pid);
4268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4270 /* Is this a forward or backward ACK? */
4271 id = GNUNET_PEER_search (peer);
4272 if (t->next_hop == id)
4274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4277 else if (t->prev_hop == id)
4279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4284 GNUNET_break_op (0);
4288 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4289 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4291 GNUNET_SCHEDULER_cancel (fc->poll_task);
4292 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4293 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4295 fc->last_ack_recv = ack;
4296 peer_unlock_queue (id);
4298 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, t->next_hop == id);
4305 * Core handler for mesh network traffic point-to-point ack polls.
4307 * @param cls closure
4308 * @param message message
4309 * @param peer peer identity this notification is about
4311 * @return GNUNET_OK to keep the connection open,
4312 * GNUNET_SYSERR to close it (signal serious error)
4315 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4316 const struct GNUNET_MessageHeader *message)
4318 struct GNUNET_MESH_Poll *msg;
4319 struct MeshTunnel *t;
4320 struct MeshFlowControl *fc;
4325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4328 msg = (struct GNUNET_MESH_Poll *) message;
4330 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4334 /* TODO notify that we dont know this tunnel (whom)? */
4335 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4336 GNUNET_break_op (0);
4340 /* Is this a forward or backward ACK? */
4341 id = GNUNET_PEER_search (peer);
4342 pid = ntohl (msg->pid);
4343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4344 if (t->next_hop == id)
4346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4348 old = fc->last_pid_recv;
4350 else if (t->prev_hop == id)
4352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4354 old = fc->last_pid_recv;
4362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", fc->last_pid_recv);
4363 fc->last_pid_recv = pid;
4364 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, t->prev_hop == id);
4366 if (GNUNET_YES == t->reliable)
4367 fc->last_pid_recv = old;
4374 * Core handler for mesh keepalives.
4376 * @param cls closure
4377 * @param message message
4378 * @param peer peer identity this notification is about
4379 * @return GNUNET_OK to keep the connection open,
4380 * GNUNET_SYSERR to close it (signal serious error)
4382 * TODO: Check who we got this from, to validate route.
4385 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4386 const struct GNUNET_MessageHeader *message)
4388 struct GNUNET_MESH_TunnelKeepAlive *msg;
4389 struct MeshTunnel *t;
4391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4394 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4395 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4399 /* TODO notify that we dont know that tunnel */
4400 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4405 tunnel_reset_timeout (t, GNUNET_YES); // FIXME
4406 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4409 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4410 send_prebuilt_message (message, t->next_hop, t);
4417 * Functions to handle messages from core
4419 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4420 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4421 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4422 sizeof (struct GNUNET_MESH_PathBroken)},
4423 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4424 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4425 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4426 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4427 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4428 sizeof (struct GNUNET_MESH_DataACK)},
4429 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4430 sizeof (struct GNUNET_MESH_DataACK)},
4431 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4432 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4433 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4434 sizeof (struct GNUNET_MESH_ACK)},
4435 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4436 sizeof (struct GNUNET_MESH_Poll)},
4437 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4438 sizeof (struct GNUNET_MESH_PathACK)},
4444 /******************************************************************************/
4445 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4446 /******************************************************************************/
4451 * notify_client_connection_failure: notify a client that the connection to the
4452 * requested remote peer is not possible (for instance, no route found)
4453 * Function called when the socket is ready to queue more data. "buf" will be
4454 * NULL and "size" zero if the socket was closed for writing in the meantime.
4456 * @param cls closure
4457 * @param size number of bytes available in buf
4458 * @param buf where the callee should write the message
4459 * @return number of bytes written to buf
4462 notify_client_connection_failure (void *cls, size_t size, void *buf)
4465 struct MeshPeerInfo *peer_info;
4466 struct GNUNET_MESH_PeerControl *msg;
4467 struct GNUNET_PeerIdentity id;
4469 if (0 == size && NULL == buf)
4471 // TODO retry? cancel?
4475 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4476 peer_info = (struct MeshPeerInfo *) cls;
4477 msg = (struct GNUNET_MESH_PeerControl *) buf;
4478 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4479 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4480 // msg->tunnel_id = htonl(peer_info->t->tid);
4481 GNUNET_PEER_resolve (peer_info->id, &id);
4482 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4490 * Send keepalive packets for a tunnel.
4492 * @param cls Closure (tunnel for which to send the keepalive).
4493 * @param tc Notification context.
4495 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4498 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4500 struct MeshTunnel *t = cls;
4501 struct GNUNET_MESH_TunnelKeepAlive *msg;
4502 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4505 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4506 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4507 NULL == t->owner || 0 == t->local_tid)
4512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4513 "sending keepalive for tunnel %d\n", t->id.tid);
4515 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4516 msg->header.size = htons (size);
4517 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4518 msg->oid = my_full_id;
4519 msg->tid = htonl (t->id.tid);
4520 send_prebuilt_message (&msg->header, t->next_hop, t);
4522 t->maintenance_task =
4523 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4528 * Function to process paths received for a new peer addition. The recorded
4529 * paths form the initial tunnel, which can be optimized later.
4530 * Called on each result obtained for the DHT search.
4532 * @param cls closure
4533 * @param exp when will this value expire
4534 * @param key key of the result
4535 * @param get_path path of the get request
4536 * @param get_path_length lenght of get_path
4537 * @param put_path path of the put request
4538 * @param put_path_length length of the put_path
4539 * @param type type of the result
4540 * @param size number of bytes in data
4541 * @param data pointer to the result data
4544 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4545 const struct GNUNET_HashCode * key,
4546 const struct GNUNET_PeerIdentity *get_path,
4547 unsigned int get_path_length,
4548 const struct GNUNET_PeerIdentity *put_path,
4549 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4550 size_t size, const void *data)
4552 struct MeshPeerInfo *peer = cls;
4553 struct MeshPeerPath *p;
4554 struct GNUNET_PeerIdentity pi;
4557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4558 GNUNET_PEER_resolve (peer->id, &pi);
4559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4561 p = path_build_from_dht (get_path, get_path_length,
4562 put_path, put_path_length);
4563 path_add_to_peers (p, GNUNET_NO);
4565 for (i = 0; i < peer->ntunnels; i++)
4567 struct GNUNET_PeerIdentity id;
4569 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4571 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4572 peer->tunnels[i]->local_tid,
4573 peer->tunnels[i]->local_tid_dest);
4574 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4577 peer_connect (peer, peer->tunnels[i]);
4585 /******************************************************************************/
4586 /********************* MESH LOCAL HANDLES **************************/
4587 /******************************************************************************/
4591 * Handler for client connection.
4593 * @param cls Closure (unused).
4594 * @param client Client handler.
4597 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
4599 struct MeshClient *c;
4603 c = GNUNET_malloc (sizeof (struct MeshClient));
4605 GNUNET_SERVER_client_keep (client);
4606 GNUNET_SERVER_client_set_user_context (client, c);
4607 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4612 * Handler for client disconnection
4614 * @param cls closure
4615 * @param client identification of the client; NULL
4616 * for the last call when the server is destroyed
4619 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4621 struct MeshClient *c;
4622 struct MeshClient *next;
4624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4631 c = client_get (client);
4634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4636 GNUNET_SERVER_client_drop (c->handle);
4637 c->shutting_down = GNUNET_YES;
4638 if (NULL != c->own_tunnels)
4640 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4641 &tunnel_destroy_iterator, c);
4642 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4645 if (NULL != c->incoming_tunnels)
4647 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4648 &tunnel_destroy_iterator, c);
4649 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4652 if (NULL != c->ports)
4653 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4655 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4658 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4663 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
4665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4671 * Handler for new clients
4673 * @param cls closure
4674 * @param client identification of the client
4675 * @param message the actual message, which includes messages the client wants
4678 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4679 const struct GNUNET_MessageHeader *message)
4681 struct GNUNET_MESH_ClientConnect *cc_msg;
4682 struct MeshClient *c;
4687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4689 /* Check data sanity */
4690 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4691 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4692 if (0 != (size % sizeof (uint32_t)))
4695 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4698 size /= sizeof (uint32_t);
4700 /* Initialize new client structure */
4701 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
4702 c->id = next_client_id++; /* overflow not important: just for debug */
4703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4709 p = (uint32_t *) &cc_msg[1];
4710 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4711 for (i = 0; i < size; i++)
4714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4716 /* store in client's hashmap */
4717 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4718 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4719 /* store in global hashmap */
4720 /* FIXME only allow one client to have the port open,
4721 * have a backup hashmap with waiting clients */
4722 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4723 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4727 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4728 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4729 GNUNET_SERVER_notification_context_add (nc, client);
4730 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4732 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4738 * Handler for requests of new tunnels
4740 * @param cls Closure.
4741 * @param client Identification of the client.
4742 * @param message The actual message.
4745 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4746 const struct GNUNET_MessageHeader *message)
4748 struct GNUNET_MESH_TunnelMessage *t_msg;
4749 struct MeshPeerInfo *peer_info;
4750 struct MeshTunnel *t;
4751 struct MeshClient *c;
4752 MESH_TunnelNumber tid;
4754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4756 /* Sanity check for client registration */
4757 if (NULL == (c = client_get (client)))
4760 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4765 /* Message size sanity check */
4766 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4769 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4773 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4775 GNUNET_i2s (&t_msg->peer));
4776 /* Sanity check for tunnel numbering */
4777 tid = ntohl (t_msg->tunnel_id);
4778 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4784 /* Sanity check for duplicate tunnel IDs */
4785 if (NULL != tunnel_get_by_local_id (c, tid))
4788 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4793 while (NULL != tunnel_get_by_pi (myid, next_tid))
4794 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4795 t = tunnel_new (myid, next_tid, c, tid);
4796 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4800 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4803 t->port = ntohl (t_msg->port);
4804 tunnel_set_options (t, ntohl (t_msg->opt));
4805 if (GNUNET_YES == t->reliable)
4807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4808 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
4810 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
4813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4814 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4816 peer_info = peer_get (&t_msg->peer);
4817 peer_info_add_tunnel (peer_info, t);
4818 peer_connect (peer_info, t);
4819 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4825 * Handler for requests of deleting tunnels
4827 * @param cls closure
4828 * @param client identification of the client
4829 * @param message the actual message
4832 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4833 const struct GNUNET_MessageHeader *message)
4835 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4836 struct MeshClient *c;
4837 struct MeshTunnel *t;
4838 MESH_TunnelNumber tid;
4840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4841 "Got a DESTROY TUNNEL from client!\n");
4843 /* Sanity check for client registration */
4844 if (NULL == (c = client_get (client)))
4847 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4852 /* Message sanity check */
4853 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4856 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4860 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4862 /* Retrieve tunnel */
4863 tid = ntohl (tunnel_msg->tunnel_id);
4864 t = tunnel_get_by_local_id(c, tid);
4867 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4869 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4873 /* Cleanup after the tunnel */
4874 client_delete_tunnel (c, t);
4881 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4885 /* The tunnel will be destroyed when the last message is transmitted. */
4886 tunnel_destroy_empty (t);
4888 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4894 * Handler for client traffic
4896 * @param cls closure
4897 * @param client identification of the client
4898 * @param message the actual message
4901 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
4902 const struct GNUNET_MessageHeader *message)
4904 struct GNUNET_MESH_LocalData *data_msg;
4905 struct MeshClient *c;
4906 struct MeshTunnel *t;
4907 struct MeshFlowControl *fc;
4908 MESH_TunnelNumber tid;
4911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4912 "Got data from a client!\n");
4914 /* Sanity check for client registration */
4915 if (NULL == (c = client_get (client)))
4918 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4923 data_msg = (struct GNUNET_MESH_LocalData *) message;
4925 /* Sanity check for message size */
4926 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
4927 if (size < sizeof (struct GNUNET_MessageHeader))
4930 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4934 /* Tunnel exists? */
4935 tid = ntohl (data_msg->tid);
4936 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
4939 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4942 t = tunnel_get_by_local_id (c, tid);
4946 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4950 /* Is the client in the tunnel? */
4951 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4953 t->owner->handle == client)
4955 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4957 t->client->handle == client) ) )
4960 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4964 /* Ok, everything is correct, send the message
4965 * (pretend we got it from a mesh peer)
4968 struct GNUNET_MESH_Data *payload;
4969 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
4971 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
4972 if (GNUNET_YES == t->reliable)
4974 struct MeshTunnelReliability *rel;
4975 struct MeshReliableMessage *copy;
4977 rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
4978 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
4979 + sizeof(struct GNUNET_MESH_Data)
4981 copy->mid = rel->mid_sent++;
4982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid);
4983 copy->timestamp = GNUNET_TIME_absolute_get ();
4986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
4987 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
4988 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
4991 GNUNET_TIME_relative_multiply (rel->expected_delay,
4992 MESH_RETRANSMIT_MARGIN);
4994 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
4995 &tunnel_retransmit_message,
4998 payload = (struct GNUNET_MESH_Data *) ©[1];
4999 payload->mid = htonl (copy->mid);
5003 payload = (struct GNUNET_MESH_Data *) cbuf;
5004 payload->mid = htonl (fc->last_pid_recv + 1);
5006 memcpy (&payload[1], &data_msg[1], size);
5007 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
5008 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
5009 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
5010 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5011 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
5012 payload->tid = htonl (t->id.tid);
5013 payload->ttl = htonl (default_ttl);
5014 payload->pid = htonl (fc->last_pid_recv + 1);
5015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5016 " calling generic handler...\n");
5017 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5018 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
5020 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
5022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5023 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5030 * Handler for client's ACKs for payload traffic.
5032 * @param cls Closure (unused).
5033 * @param client Identification of the client.
5034 * @param message The actual message.
5037 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5038 const struct GNUNET_MessageHeader *message)
5040 struct GNUNET_MESH_LocalAck *msg;
5041 struct MeshTunnel *t;
5042 struct MeshClient *c;
5043 MESH_TunnelNumber tid;
5045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5046 /* Sanity check for client registration */
5047 if (NULL == (c = client_get (client)))
5050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5055 msg = (struct GNUNET_MESH_LocalAck *) message;
5057 /* Tunnel exists? */
5058 tid = ntohl (msg->tunnel_id);
5059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5060 t = tunnel_get_by_local_id (c, tid);
5064 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5065 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5066 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5070 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5071 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5073 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5074 t->prev_fc.last_ack_recv++;
5075 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_NO);
5079 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5080 t->next_fc.last_ack_recv++;
5081 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_YES);
5084 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5092 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5094 * @param cls Closure (client handle).
5095 * @param key Key (hashed tunnel ID, unused).
5096 * @param value Tunnel info.
5098 * @return GNUNET_YES, to keep iterating.
5101 monitor_all_tunnels_iterator (void *cls,
5102 const struct GNUNET_HashCode * key,
5105 struct GNUNET_SERVER_Client *client = cls;
5106 struct MeshTunnel *t = value;
5107 struct GNUNET_MESH_LocalMonitor *msg;
5109 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5110 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5111 msg->tunnel_id = htonl (t->id.tid);
5112 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5113 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5114 GNUNET_PEER_resolve (t->dest, &msg->destination);
5116 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5117 "* sending info about tunnel %s [%u]\n",
5118 GNUNET_i2s (&msg->owner), t->id.tid);
5120 GNUNET_SERVER_notification_context_unicast (nc, client,
5121 &msg->header, GNUNET_NO);
5127 * Handler for client's MONITOR request.
5129 * @param cls Closure (unused).
5130 * @param client Identification of the client.
5131 * @param message The actual message.
5134 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5135 const struct GNUNET_MessageHeader *message)
5137 struct MeshClient *c;
5139 /* Sanity check for client registration */
5140 if (NULL == (c = client_get (client)))
5143 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5147 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5148 "Received get tunnels request from client %u\n",
5150 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5151 monitor_all_tunnels_iterator,
5153 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5154 "Get tunnels request from client %u completed\n",
5156 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5161 * Handler for client's MONITOR_TUNNEL request.
5163 * @param cls Closure (unused).
5164 * @param client Identification of the client.
5165 * @param message The actual message.
5168 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5169 const struct GNUNET_MessageHeader *message)
5171 const struct GNUNET_MESH_LocalMonitor *msg;
5172 struct GNUNET_MESH_LocalMonitor *resp;
5173 struct MeshClient *c;
5174 struct MeshTunnel *t;
5176 /* Sanity check for client registration */
5177 if (NULL == (c = client_get (client)))
5180 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5184 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5186 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5189 ntohl (msg->tunnel_id));
5190 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5193 /* We don't know the tunnel FIXME */
5194 struct GNUNET_MESH_LocalMonitor warn;
5197 GNUNET_SERVER_notification_context_unicast (nc, client,
5200 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5204 /* Initialize context */
5205 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5207 GNUNET_PEER_resolve (t->dest, &resp->destination);
5208 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5209 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5210 &resp->header, GNUNET_NO);
5213 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5214 "Monitor tunnel request from client %u completed\n",
5216 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5221 * Functions to handle messages from clients
5223 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5224 {&handle_local_new_client, NULL,
5225 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5226 {&handle_local_tunnel_create, NULL,
5227 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5228 sizeof (struct GNUNET_MESH_TunnelMessage)},
5229 {&handle_local_tunnel_destroy, NULL,
5230 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5231 sizeof (struct GNUNET_MESH_TunnelMessage)},
5232 {&handle_local_data, NULL,
5233 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5234 {&handle_local_ack, NULL,
5235 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5236 sizeof (struct GNUNET_MESH_LocalAck)},
5237 {&handle_local_get_tunnels, NULL,
5238 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5239 sizeof (struct GNUNET_MessageHeader)},
5240 {&handle_local_show_tunnel, NULL,
5241 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5242 sizeof (struct GNUNET_MESH_LocalMonitor)},
5248 * Method called whenever a given peer connects.
5250 * @param cls closure
5251 * @param peer peer identity this notification is about
5254 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5256 struct MeshPeerInfo *peer_info;
5257 struct MeshPeerPath *path;
5259 DEBUG_CONN ("Peer connected\n");
5260 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5261 peer_info = peer_get (peer);
5262 if (myid == peer_info->id)
5264 DEBUG_CONN (" (self)\n");
5265 path = path_new (1);
5269 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5270 path = path_new (2);
5271 path->peers[1] = peer_info->id;
5272 GNUNET_PEER_change_rc (peer_info->id, 1);
5273 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5275 path->peers[0] = myid;
5276 GNUNET_PEER_change_rc (myid, 1);
5277 peer_info_add_path (peer_info, path, GNUNET_YES);
5283 * Method called whenever a peer disconnects.
5285 * @param cls closure
5286 * @param peer peer identity this notification is about
5289 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5291 struct MeshPeerInfo *pi;
5292 struct MeshPeerQueue *q;
5293 struct MeshPeerQueue *n;
5295 DEBUG_CONN ("Peer disconnected\n");
5296 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5306 /* TODO try to reroute this traffic instead */
5307 queue_destroy(q, GNUNET_YES);
5310 if (NULL != pi->core_transmit)
5312 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5313 pi->core_transmit = NULL;
5315 peer_remove_path (pi, pi->id, myid);
5318 DEBUG_CONN (" (self)\n");
5320 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5326 * Install server (service) handlers and start listening to clients.
5331 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5332 GNUNET_SERVER_connect_notify (server_handle,
5333 &handle_local_client_connect, NULL);
5334 GNUNET_SERVER_disconnect_notify (server_handle,
5335 &handle_local_client_disconnect, NULL);
5336 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5338 clients_head = NULL;
5339 clients_tail = NULL;
5341 GNUNET_SERVER_resume (server_handle);
5346 * To be called on core init/fail.
5348 * @param cls Closure (config)
5349 * @param server handle to the server for this service
5350 * @param identity the public identity of this peer
5353 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5354 const struct GNUNET_PeerIdentity *identity)
5356 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5360 GNUNET_break (core_handle == server);
5361 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5364 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5365 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5367 GNUNET_i2s (identity));
5368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5370 GNUNET_i2s (&my_full_id));
5371 GNUNET_CORE_disconnect (core_handle);
5372 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5373 NULL, /* Closure passed to MESH functions */
5374 &core_init, /* Call core_init once connected */
5375 &core_connect, /* Handle connects */
5376 &core_disconnect, /* remove peers on disconnects */
5377 NULL, /* Don't notify about all incoming messages */
5378 GNUNET_NO, /* For header only in notification */
5379 NULL, /* Don't notify about all outbound messages */
5380 GNUNET_NO, /* For header-only out notification */
5381 core_handlers); /* Register these handlers */
5390 /******************************************************************************/
5391 /************************ MAIN FUNCTIONS ****************************/
5392 /******************************************************************************/
5395 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5397 * @param cls closure
5398 * @param key current key code
5399 * @param value value in the hash map
5400 * @return GNUNET_YES if we should continue to iterate,
5404 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5406 struct MeshTunnel *t = value;
5413 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5415 * @param cls closure
5416 * @param key current key code
5417 * @param value value in the hash map
5418 * @return GNUNET_YES if we should continue to iterate,
5422 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5424 struct MeshPeerInfo *p = value;
5425 struct MeshPeerQueue *q;
5426 struct MeshPeerQueue *n;
5434 queue_destroy(q, GNUNET_YES);
5438 peer_info_destroy (p);
5444 * Task run during shutdown.
5450 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5454 if (core_handle != NULL)
5456 GNUNET_CORE_disconnect (core_handle);
5459 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5460 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5461 if (dht_handle != NULL)
5463 GNUNET_DHT_disconnect (dht_handle);
5468 GNUNET_SERVER_notification_context_destroy (nc);
5471 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5473 GNUNET_SCHEDULER_cancel (announce_id_task);
5474 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5481 * Process mesh requests.
5483 * @param cls closure
5484 * @param server the initialized server
5485 * @param c configuration to use
5488 run (void *cls, struct GNUNET_SERVER_Handle *server,
5489 const struct GNUNET_CONFIGURATION_Handle *c)
5492 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5495 server_handle = server;
5496 GNUNET_SERVER_suspend (server_handle);
5499 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5502 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5504 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5505 "mesh", "peer/privatekey");
5506 GNUNET_SCHEDULER_shutdown ();
5511 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5512 &refresh_path_time))
5514 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5516 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5517 "mesh", "refresh path time");
5518 GNUNET_SCHEDULER_shutdown ();
5523 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5526 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5528 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5529 "mesh", "id announce time");
5530 GNUNET_SCHEDULER_shutdown ();
5535 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5538 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5540 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5541 "mesh", "connect timeout");
5542 GNUNET_SCHEDULER_shutdown ();
5547 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5550 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5552 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5553 "mesh", "max msgs queue");
5554 GNUNET_SCHEDULER_shutdown ();
5559 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5564 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5565 "mesh", "max tunnels");
5566 GNUNET_SCHEDULER_shutdown ();
5571 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5574 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5576 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5577 "mesh", "default ttl", 64);
5582 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5585 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5586 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5587 "mesh", "max peers", 1000);
5592 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5599 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5600 "Mesh is running with drop mode enabled. "
5601 "This is NOT a good idea! "
5602 "Remove the DROP_PERCENT option from your configuration.\n");
5606 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5607 &dht_replication_level))
5609 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5611 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5612 "mesh", "dht replication level", 3);
5613 dht_replication_level = 3;
5616 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5617 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5618 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5619 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5621 dht_handle = GNUNET_DHT_connect (c, 64);
5622 if (NULL == dht_handle)
5626 stats = GNUNET_STATISTICS_create ("mesh", c);
5628 /* Scheduled the task to clean up when shutdown is called */
5629 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5631 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5632 GNUNET_free (keyfile);
5633 GNUNET_assert (NULL != pk);
5634 my_private_key = pk;
5635 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5636 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5637 &my_full_id.hashPubKey);
5638 myid = GNUNET_PEER_intern (&my_full_id);
5639 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5640 "Mesh for peer [%s] starting\n",
5641 GNUNET_i2s(&my_full_id));
5643 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5644 NULL, /* Closure passed to MESH functions */
5645 &core_init, /* Call core_init once connected */
5646 &core_connect, /* Handle connects */
5647 &core_disconnect, /* remove peers on disconnects */
5648 NULL, /* Don't notify about all incoming messages */
5649 GNUNET_NO, /* For header only in notification */
5650 NULL, /* Don't notify about all outbound messages */
5651 GNUNET_NO, /* For header-only out notification */
5652 core_handlers); /* Register these handlers */
5653 if (NULL == core_handle)
5656 GNUNET_SCHEDULER_shutdown ();
5660 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5661 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5667 * The main function for the mesh service.
5669 * @param argc number of arguments from the command line
5670 * @param argv command line arguments
5671 * @return 0 ok, 1 on error
5674 main (int argc, char *const *argv)
5679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5680 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5682 ret = (GNUNET_OK == r) ? 0 : 1;
5683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");