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_POLL:
3155 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3157 " prebuilt message\n");
3158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3160 GNUNET_MESH_DEBUG_M2S (queue->type));
3162 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3168 " type %s unknown!\n",
3169 GNUNET_MESH_DEBUG_M2S (queue->type));
3171 GNUNET_free_non_null (queue->cls);
3173 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3174 queue->peer->queue_tail,
3177 /* Delete from appropriate fc in the tunnel */
3178 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
3179 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
3181 if (queue->peer->id == queue->tunnel->prev_hop)
3182 fc = &queue->tunnel->prev_fc;
3183 else if (queue->peer->id == queue->tunnel->next_hop)
3184 fc = &queue->tunnel->next_fc;
3188 GNUNET_free (queue);
3193 GNUNET_free (queue);
3198 * @brief Get the next transmittable message from the queue.
3200 * This will be the head, except in the case of being a data packet
3201 * not allowed by the destination peer.
3203 * @param peer Destination peer.
3205 * @return The next viable MeshPeerQueue element to send to that peer.
3206 * NULL when there are no transmittable messages.
3208 struct MeshPeerQueue *
3209 queue_get_next (const struct MeshPeerInfo *peer)
3211 struct MeshPeerQueue *q;
3213 struct GNUNET_MESH_Data *dmsg;
3214 struct MeshTunnel* t;
3218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
3219 for (q = peer->queue_head; NULL != q; q = q->next)
3222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3224 GNUNET_MESH_DEBUG_M2S (q->type));
3225 dmsg = (struct GNUNET_MESH_Data *) q->cls;
3226 pid = ntohl (dmsg->pid);
3229 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3230 ack = t->next_fc.last_ack_recv;
3232 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3233 ack = t->prev_fc.last_ack_recv;
3236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3241 "* ACK: %u, PID: %u, MID: %u\n",
3242 ack, pid, ntohl (dmsg->mid));
3243 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3256 "* nothing found\n");
3262 queue_send (void *cls, size_t size, void *buf)
3264 struct MeshPeerInfo *peer = cls;
3265 struct GNUNET_MessageHeader *msg;
3266 struct MeshPeerQueue *queue;
3267 struct MeshTunnel *t;
3268 struct GNUNET_PeerIdentity dst_id;
3269 struct MeshFlowControl *fc;
3274 peer->core_transmit = NULL;
3276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3277 queue = queue_get_next (peer);
3279 /* Queue has no internal mesh traffic nor sendable payload */
3282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3283 if (NULL == peer->queue_head)
3284 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3289 GNUNET_PEER_resolve (peer->id, &dst_id);
3290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3292 GNUNET_i2s (&dst_id));
3293 /* Check if buffer size is enough for the message */
3294 if (queue->size > size)
3296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3297 "* not enough room, reissue\n");
3298 peer->core_transmit =
3299 GNUNET_CORE_notify_transmit_ready (core_handle,
3302 GNUNET_TIME_UNIT_FOREVER_REL,
3309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3312 GNUNET_assert (0 < t->pending_messages);
3313 t->pending_messages--;
3317 switch (queue->type)
3320 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3321 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3322 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3323 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3324 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3325 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3326 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3327 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3330 GNUNET_MESH_DEBUG_M2S (queue->type));
3332 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3333 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3334 data_size = send_core_data_raw (queue->cls, size, buf);
3335 msg = (struct GNUNET_MessageHeader *) buf;
3336 type = ntohs (msg->type);
3338 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3340 data_size = send_core_path_create (queue->cls, size, buf);
3342 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3344 data_size = send_core_path_ack (queue->cls, size, buf);
3348 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3349 "* type unknown: %u\n",
3354 if (0 < drop_percent &&
3355 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3357 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3358 "Dropping message of type %s\n",
3359 GNUNET_MESH_DEBUG_M2S(queue->type));
3362 /* Free queue, but cls was freed by send_core_* */
3363 queue_destroy (queue, GNUNET_NO);
3365 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3366 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3370 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3371 t->next_fc.last_pid_sent = pid;
3372 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, GNUNET_YES);
3373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3375 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3377 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3378 t->prev_fc.last_pid_sent = pid;
3379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3381 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3382 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, GNUNET_NO);
3388 /* If more data in queue, send next */
3389 queue = queue_get_next (peer);
3392 struct GNUNET_PeerIdentity id;
3394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3395 GNUNET_PEER_resolve (peer->id, &id);
3396 peer->core_transmit =
3397 GNUNET_CORE_notify_transmit_ready(core_handle,
3400 GNUNET_TIME_UNIT_FOREVER_REL,
3406 if (peer->id == t->next_hop)
3408 else if (peer->id == t->prev_hop)
3413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id: %u, next: %u, prev: %u\n",
3414 peer->id, t->next_hop, t->prev_hop);
3417 if (NULL != peer->queue_head)
3419 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task && fc->queue_n > 0)
3421 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3422 "* %s starting poll timeout\n",
3423 GNUNET_i2s (&my_full_id));
3425 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3431 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3433 GNUNET_SCHEDULER_cancel (fc->poll_task);
3434 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3437 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3448 * @brief Queue and pass message to core when possible.
3450 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3451 * accounts for it. In case the queue is full, the message is dropped and
3454 * Otherwise, message is treated as internal and allowed to go regardless of
3457 * @param cls Closure (@c type dependant). It will be used by queue_send to
3458 * build the message to be sent if not already prebuilt.
3459 * @param type Type of the message, 0 for a raw message.
3460 * @param size Size of the message.
3461 * @param dst Neighbor to send message to.
3462 * @param t Tunnel this message belongs to.
3465 queue_add (void *cls, uint16_t type, size_t size,
3466 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3468 struct MeshPeerQueue *queue;
3469 struct GNUNET_PeerIdentity id;
3470 struct MeshFlowControl *fc;
3474 priority = GNUNET_NO;
3475 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3479 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3485 if (fc->queue_n >= t->queue_max)
3487 /* If this isn't a retransmission, drop the message */
3488 if (GNUNET_NO == t->reliable ||
3489 (NULL == t->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) ||
3490 (NULL == t->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type))
3492 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3496 "queue full: %u/%u\n",
3497 fc->queue_n, t->queue_max);
3498 return; /* Drop this message */
3500 priority = GNUNET_YES;
3503 if (GMC_is_pid_bigger(fc->last_pid_sent + 1, fc->last_ack_recv) &&
3504 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3505 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3509 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3515 if (GNUNET_YES == priority)
3517 struct GNUNET_MESH_Data *d;
3521 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
3522 d = (struct GNUNET_MESH_Data *) queue->cls;
3524 for (queue = dst->queue_tail; NULL != queue; queue = queue->prev)
3526 if (queue->type != type)
3528 d = (struct GNUNET_MESH_Data *) queue->cls;
3535 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3537 if (NULL == dst->core_transmit)
3539 GNUNET_PEER_resolve (dst->id, &id);
3540 dst->core_transmit =
3541 GNUNET_CORE_notify_transmit_ready (core_handle,
3544 GNUNET_TIME_UNIT_FOREVER_REL,
3550 t->pending_messages++;
3554 /******************************************************************************/
3555 /******************** MESH NETWORK HANDLERS **************************/
3556 /******************************************************************************/
3560 * Core handler for path creation
3562 * @param cls closure
3563 * @param message message
3564 * @param peer peer identity this notification is about
3566 * @return GNUNET_OK to keep the connection open,
3567 * GNUNET_SYSERR to close it (signal serious error)
3570 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3571 const struct GNUNET_MessageHeader *message)
3573 unsigned int own_pos;
3576 MESH_TunnelNumber tid;
3577 struct GNUNET_MESH_CreateTunnel *msg;
3578 struct GNUNET_PeerIdentity *pi;
3579 struct MeshPeerPath *path;
3580 struct MeshPeerInfo *dest_peer_info;
3581 struct MeshPeerInfo *orig_peer_info;
3582 struct MeshTunnel *t;
3584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3585 "Received a path create msg [%s]\n",
3586 GNUNET_i2s (&my_full_id));
3587 size = ntohs (message->size);
3588 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3590 GNUNET_break_op (0);
3594 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3595 if (size % sizeof (struct GNUNET_PeerIdentity))
3597 GNUNET_break_op (0);
3600 size /= sizeof (struct GNUNET_PeerIdentity);
3603 GNUNET_break_op (0);
3606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3607 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3609 tid = ntohl (msg->tid);
3610 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3612 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3613 t = tunnel_get (pi, tid);
3614 if (NULL == t) /* might be a local tunnel */
3618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3619 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3625 t->port = ntohl (msg->port);
3626 opt = ntohl (msg->opt);
3627 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3629 t->nobuffer = GNUNET_YES;
3632 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3634 t->reliable = GNUNET_YES;
3636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3638 tunnel_reset_timeout (t, GNUNET_YES); // FIXME
3640 t->state = MESH_TUNNEL_WAITING;
3642 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3643 if (NULL == dest_peer_info)
3645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3646 " Creating PeerInfo for destination.\n");
3647 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3648 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3649 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3651 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3653 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3654 if (NULL == orig_peer_info)
3656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3657 " Creating PeerInfo for origin.\n");
3658 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3659 orig_peer_info->id = GNUNET_PEER_intern (pi);
3660 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3661 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3664 path = path_new (size);
3666 for (i = 0; i < size; i++)
3668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3669 GNUNET_i2s (&pi[i]));
3670 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3671 if (path->peers[i] == myid)
3674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3675 if (own_pos == 0 && path->peers[own_pos] != myid)
3677 /* create path: self not found in path through self */
3678 GNUNET_break_op (0);
3679 path_destroy (path);
3683 path_add_to_peers (path, GNUNET_NO);
3684 tunnel_use_path (t, path);
3686 peer_info_add_tunnel (dest_peer_info, t);
3688 if (own_pos == size - 1)
3690 struct MeshClient *c;
3692 /* Find target client */
3693 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
3696 /* TODO send reject */
3700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3701 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3703 /* Assign local tid */
3704 while (NULL != tunnel_get_incoming (next_local_tid))
3705 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3706 t->local_tid_dest = next_local_tid++;
3707 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3709 if (GNUNET_YES == t->reliable)
3711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
3712 t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
3714 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
3717 tunnel_add_client (t, c);
3718 send_client_tunnel_create (t);
3723 struct MeshPeerPath *path2;
3725 t->next_hop = path->peers[own_pos + 1];
3726 GNUNET_PEER_change_rc(t->next_hop, 1);
3728 /* It's for somebody else! Retransmit. */
3729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3730 path2 = path_duplicate (path);
3731 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3732 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3733 send_create_path (t);
3741 * Core handler for path ACKs
3743 * @param cls closure
3744 * @param message message
3745 * @param peer peer identity this notification is about
3747 * @return GNUNET_OK to keep the connection open,
3748 * GNUNET_SYSERR to close it (signal serious error)
3751 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3752 const struct GNUNET_MessageHeader *message)
3754 struct GNUNET_MESH_PathACK *msg;
3755 struct MeshPeerInfo *peer_info;
3756 struct MeshPeerPath *p;
3757 struct MeshTunnel *t;
3759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3760 GNUNET_i2s (&my_full_id));
3761 msg = (struct GNUNET_MESH_PathACK *) message;
3762 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3765 /* TODO notify that we don't know the tunnel */
3766 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3768 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3772 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3774 peer_info = peer_get (&msg->peer_id);
3775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3776 GNUNET_i2s (&msg->peer_id));
3777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3780 /* Add path to peers? */
3784 path_add_to_peers (p, GNUNET_YES);
3790 t->state = MESH_TUNNEL_READY;
3791 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3792 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3794 /* Message for us? */
3795 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3798 if (NULL == t->owner)
3800 GNUNET_break_op (0);
3803 if (NULL != peer_info->dhtget)
3805 GNUNET_DHT_get_stop (peer_info->dhtget);
3806 peer_info->dhtget = NULL;
3808 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_YES);
3809 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_NO);
3813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3814 " not for us, retransmitting...\n");
3815 peer_info = peer_get (&msg->oid);
3816 send_prebuilt_message (message, t->prev_hop, t);
3822 * Core handler for notifications of broken paths
3824 * @param cls closure
3825 * @param message message
3826 * @param peer peer identity this notification is about
3828 * @return GNUNET_OK to keep the connection open,
3829 * GNUNET_SYSERR to close it (signal serious error)
3832 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3833 const struct GNUNET_MessageHeader *message)
3835 struct GNUNET_MESH_PathBroken *msg;
3836 struct MeshTunnel *t;
3838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3839 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3840 msg = (struct GNUNET_MESH_PathBroken *) message;
3841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3842 GNUNET_i2s (&msg->peer1));
3843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3844 GNUNET_i2s (&msg->peer2));
3845 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3848 GNUNET_break_op (0);
3851 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3852 GNUNET_PEER_search (&msg->peer2));
3859 * Core handler for tunnel destruction
3861 * @param cls closure
3862 * @param message message
3863 * @param peer peer identity this notification is about
3865 * @return GNUNET_OK to keep the connection open,
3866 * GNUNET_SYSERR to close it (signal serious error)
3869 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3870 const struct GNUNET_MessageHeader *message)
3872 struct GNUNET_MESH_TunnelDestroy *msg;
3873 struct MeshTunnel *t;
3875 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3877 "Got a TUNNEL DESTROY packet from %s\n",
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3880 " for tunnel %s [%u]\n",
3881 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3882 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3885 /* Probably already got the message from another path,
3886 * destroyed the tunnel and retransmitted to children.
3889 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3893 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3896 t->local_tid, t->local_tid_dest);
3898 if (GNUNET_PEER_search (peer) == t->prev_hop)
3900 // TODO check owner's signature
3901 // TODO add owner's signatue to tunnel for retransmission
3902 peer_cancel_queues (t->prev_hop, t);
3903 GNUNET_PEER_change_rc (t->prev_hop, -1);
3906 else if (GNUNET_PEER_search (peer) == t->next_hop)
3908 // TODO check dest's signature
3909 // TODO add dest's signatue to tunnel for retransmission
3910 peer_cancel_queues (t->next_hop, t);
3911 GNUNET_PEER_change_rc (t->next_hop, -1);
3916 GNUNET_break_op (0);
3917 // TODO check both owner AND destination's signature to see which matches
3918 // TODO restransmit in appropriate direction
3921 tunnel_destroy_empty (t);
3923 // TODO: add timeout to destroy the tunnel anyway
3929 * Generic handler for mesh network payload traffic.
3931 * @param peer Peer identity this notification is about.
3932 * @param message Data message.
3933 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
3935 * @return GNUNET_OK to keep the connection open,
3936 * GNUNET_SYSERR to close it (signal serious error)
3939 handle_mesh_data (const struct GNUNET_PeerIdentity *peer,
3940 const struct GNUNET_MessageHeader *message,
3943 struct GNUNET_MESH_Data *msg;
3944 struct MeshFlowControl *fc;
3945 struct MeshTunnelReliability *rel;
3946 struct MeshTunnel *t;
3947 struct MeshClient *c;
3953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
3954 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)),
3957 size = ntohs (message->size);
3959 sizeof (struct GNUNET_MESH_Data) +
3960 sizeof (struct GNUNET_MessageHeader))
3965 msg = (struct GNUNET_MESH_Data *) message;
3966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
3967 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3969 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3972 /* TODO notify back: we don't know this tunnel */
3973 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3974 GNUNET_break_op (0);
3978 /* Initialize FWD/BCK data */
3979 pid = ntohl (msg->pid);
3980 fc = fwd ? &t->prev_fc : &t->next_fc;
3981 c = fwd ? t->client : t->owner;
3982 rel = fwd ? t->bck_rel : t->fwd_rel;
3983 hop = fwd ? t->next_hop : t->prev_hop;
3984 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
3986 GNUNET_STATISTICS_update (stats, "# unsolicited data", 1, GNUNET_NO);
3987 GNUNET_break_op (0);
3988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3989 "Received PID %u, (prev %u), ACK %u\n",
3990 pid, fc->last_pid_recv, fc->last_ack_sent);
3994 tunnel_reset_timeout (t, fwd);
3997 /* TODO signature verification */
3998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us! sending to client\n");
3999 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4000 if (GMC_is_pid_bigger (pid, fc->last_pid_recv))
4004 mid = ntohl (msg->mid);
4005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4006 " pid %u (mid %u) not seen yet\n", pid, mid);
4007 fc->last_pid_recv = pid;
4009 if (GNUNET_NO == t->reliable ||
4010 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
4011 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
4013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4014 "!!! RECV %u\n", ntohl (msg->mid));
4015 if (GNUNET_YES == t->reliable)
4017 /* Is this the exact next expected messasge? */
4018 if (mid == rel->mid_recv)
4020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4022 tunnel_send_client_data (t, msg, fwd);
4023 tunnel_send_client_buffered_data (t, c, rel);
4027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4028 tunnel_add_buffered_data (t, msg, rel);
4031 else /* Tunnel unreliable, send to clients directly */
4033 tunnel_send_client_data (t, msg, fwd);
4038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4039 " MID %u not expected (%u - %u), dropping!\n",
4040 ntohl (msg->mid), rel->mid_recv, rel->mid_recv + 64);
4045 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4047 " Pid %u not expected (%u+), dropping!\n",
4048 pid, fc->last_pid_recv + 1);
4050 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, fwd);
4053 fc->last_pid_recv = pid;
4056 GNUNET_STATISTICS_update (stats, "# data on dying tunnel", 1, GNUNET_NO);
4057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "data on dying tunnel %s[%X]\n",
4058 GNUNET_PEER_resolve2 (t->id.oid), ntohl (msg->tid));
4059 return GNUNET_OK; /* Next hop has destoyed the tunnel, drop */
4061 ttl = ntohl (msg->ttl);
4062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4065 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4066 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4067 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, fwd);
4073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4074 send_prebuilt_message (message, hop, t);
4075 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4082 * Core handler for mesh network traffic going from the origin to a peer
4084 * @param cls Closure (unused).
4085 * @param message Message received.
4086 * @param peer Peer who sent the message.
4088 * @return GNUNET_OK to keep the connection open,
4089 * GNUNET_SYSERR to close it (signal serious error)
4092 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4093 const struct GNUNET_MessageHeader *message)
4095 return handle_mesh_data (peer, message, GNUNET_YES);
4099 * Core handler for mesh network traffic towards the owner of a tunnel.
4101 * @param cls Closure (unused).
4102 * @param message Message received.
4103 * @param peer Peer who sent the message.
4105 * @return GNUNET_OK to keep the connection open,
4106 * GNUNET_SYSERR to close it (signal serious error)
4109 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4110 const struct GNUNET_MessageHeader *message)
4112 return handle_mesh_data (peer, message, GNUNET_NO);
4117 * Core handler for mesh network traffic end-to-end ACKs.
4119 * @param cls Closure.
4120 * @param message Message.
4121 * @param peer Peer identity this notification is about.
4123 * @return GNUNET_OK to keep the connection open,
4124 * GNUNET_SYSERR to close it (signal serious error)
4127 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4128 const struct GNUNET_MessageHeader *message)
4130 struct GNUNET_MESH_DataACK *msg;
4131 struct MeshTunnelReliability *rel;
4132 struct MeshReliableMessage *copy;
4133 struct MeshReliableMessage *next;
4134 struct MeshTunnel *t;
4140 type = ntohs (message->type);
4141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
4142 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4143 msg = (struct GNUNET_MESH_DataACK *) message;
4145 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4148 /* TODO notify that we dont know this tunnel (whom)? */
4149 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4152 ack = ntohl (msg->mid);
4153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4155 /* Is this a forward or backward ACK? */
4156 id = GNUNET_PEER_search (peer);
4157 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4160 if (NULL == t->owner)
4162 send_prebuilt_message (message, t->prev_hop, t);
4167 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4170 if (NULL == t->client)
4172 send_prebuilt_message (message, t->next_hop, t);
4179 GNUNET_break_op (0);
4183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %u\n", ack);
4184 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4186 if (GMC_is_pid_bigger (copy->mid, ack))
4188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4189 tunnel_free_sent_reliable (t, msg, rel);
4193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4195 tunnel_free_reliable_message (copy);
4197 /* Once buffers have been free'd, send ACK */
4198 tunnel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4200 /* If some message was free'd, update the retransmission delay*/
4201 if (GNUNET_YES == work)
4203 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4205 GNUNET_SCHEDULER_cancel (rel->retry_task);
4206 if (NULL == rel->head_sent)
4208 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4212 struct GNUNET_TIME_Absolute new_target;
4213 struct GNUNET_TIME_Relative delay;
4215 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4216 MESH_RETRANSMIT_MARGIN);
4217 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4219 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4221 GNUNET_SCHEDULER_add_delayed (delay,
4222 &tunnel_retransmit_message,
4233 * Core handler for mesh network traffic point-to-point acks.
4235 * @param cls closure
4236 * @param message message
4237 * @param peer peer identity this notification is about
4239 * @return GNUNET_OK to keep the connection open,
4240 * GNUNET_SYSERR to close it (signal serious error)
4243 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4244 const struct GNUNET_MessageHeader *message)
4246 struct GNUNET_MESH_ACK *msg;
4247 struct MeshTunnel *t;
4248 struct MeshFlowControl *fc;
4252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4254 msg = (struct GNUNET_MESH_ACK *) message;
4256 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4260 /* TODO notify that we dont know this tunnel (whom)? */
4261 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4264 ack = ntohl (msg->pid);
4265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4267 /* Is this a forward or backward ACK? */
4268 id = GNUNET_PEER_search (peer);
4269 if (t->next_hop == id)
4271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4274 else if (t->prev_hop == id)
4276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4281 GNUNET_break_op (0);
4285 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4286 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4288 GNUNET_SCHEDULER_cancel (fc->poll_task);
4289 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4290 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4292 fc->last_ack_recv = ack;
4293 peer_unlock_queue (id);
4295 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, t->next_hop == id);
4302 * Core handler for mesh network traffic point-to-point ack polls.
4304 * @param cls closure
4305 * @param message message
4306 * @param peer peer identity this notification is about
4308 * @return GNUNET_OK to keep the connection open,
4309 * GNUNET_SYSERR to close it (signal serious error)
4312 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4313 const struct GNUNET_MessageHeader *message)
4315 struct GNUNET_MESH_Poll *msg;
4316 struct MeshTunnel *t;
4317 struct MeshFlowControl *fc;
4322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4325 msg = (struct GNUNET_MESH_Poll *) message;
4327 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4331 /* TODO notify that we dont know this tunnel (whom)? */
4332 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4333 GNUNET_break_op (0);
4337 /* Is this a forward or backward ACK? */
4338 id = GNUNET_PEER_search (peer);
4339 pid = ntohl (msg->pid);
4340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4341 if (t->next_hop == id)
4343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4345 old = fc->last_pid_recv;
4347 else if (t->prev_hop == id)
4349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4351 old = fc->last_pid_recv;
4359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", fc->last_pid_recv);
4360 fc->last_pid_recv = pid;
4361 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, t->prev_hop == id);
4363 if (GNUNET_YES == t->reliable)
4364 fc->last_pid_recv = old;
4371 * Core handler for mesh keepalives.
4373 * @param cls closure
4374 * @param message message
4375 * @param peer peer identity this notification is about
4376 * @return GNUNET_OK to keep the connection open,
4377 * GNUNET_SYSERR to close it (signal serious error)
4379 * TODO: Check who we got this from, to validate route.
4382 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4383 const struct GNUNET_MessageHeader *message)
4385 struct GNUNET_MESH_TunnelKeepAlive *msg;
4386 struct MeshTunnel *t;
4388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4391 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4392 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4396 /* TODO notify that we dont know that tunnel */
4397 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4402 tunnel_reset_timeout (t, GNUNET_YES); // FIXME
4403 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4406 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4407 send_prebuilt_message (message, t->next_hop, t);
4414 * Functions to handle messages from core
4416 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4417 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4418 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4419 sizeof (struct GNUNET_MESH_PathBroken)},
4420 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4421 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4422 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4423 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4424 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4425 sizeof (struct GNUNET_MESH_DataACK)},
4426 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4427 sizeof (struct GNUNET_MESH_DataACK)},
4428 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4429 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4430 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4431 sizeof (struct GNUNET_MESH_ACK)},
4432 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4433 sizeof (struct GNUNET_MESH_Poll)},
4434 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4435 sizeof (struct GNUNET_MESH_PathACK)},
4441 /******************************************************************************/
4442 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4443 /******************************************************************************/
4448 * notify_client_connection_failure: notify a client that the connection to the
4449 * requested remote peer is not possible (for instance, no route found)
4450 * Function called when the socket is ready to queue more data. "buf" will be
4451 * NULL and "size" zero if the socket was closed for writing in the meantime.
4453 * @param cls closure
4454 * @param size number of bytes available in buf
4455 * @param buf where the callee should write the message
4456 * @return number of bytes written to buf
4459 notify_client_connection_failure (void *cls, size_t size, void *buf)
4462 struct MeshPeerInfo *peer_info;
4463 struct GNUNET_MESH_PeerControl *msg;
4464 struct GNUNET_PeerIdentity id;
4466 if (0 == size && NULL == buf)
4468 // TODO retry? cancel?
4472 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4473 peer_info = (struct MeshPeerInfo *) cls;
4474 msg = (struct GNUNET_MESH_PeerControl *) buf;
4475 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4476 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4477 // msg->tunnel_id = htonl(peer_info->t->tid);
4478 GNUNET_PEER_resolve (peer_info->id, &id);
4479 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4487 * Send keepalive packets for a tunnel.
4489 * @param cls Closure (tunnel for which to send the keepalive).
4490 * @param tc Notification context.
4492 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4495 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4497 struct MeshTunnel *t = cls;
4498 struct GNUNET_MESH_TunnelKeepAlive *msg;
4499 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4502 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4503 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4504 NULL == t->owner || 0 == t->local_tid)
4509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4510 "sending keepalive for tunnel %d\n", t->id.tid);
4512 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4513 msg->header.size = htons (size);
4514 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4515 msg->oid = my_full_id;
4516 msg->tid = htonl (t->id.tid);
4517 send_prebuilt_message (&msg->header, t->next_hop, t);
4519 t->maintenance_task =
4520 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4525 * Function to process paths received for a new peer addition. The recorded
4526 * paths form the initial tunnel, which can be optimized later.
4527 * Called on each result obtained for the DHT search.
4529 * @param cls closure
4530 * @param exp when will this value expire
4531 * @param key key of the result
4532 * @param get_path path of the get request
4533 * @param get_path_length lenght of get_path
4534 * @param put_path path of the put request
4535 * @param put_path_length length of the put_path
4536 * @param type type of the result
4537 * @param size number of bytes in data
4538 * @param data pointer to the result data
4541 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4542 const struct GNUNET_HashCode * key,
4543 const struct GNUNET_PeerIdentity *get_path,
4544 unsigned int get_path_length,
4545 const struct GNUNET_PeerIdentity *put_path,
4546 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4547 size_t size, const void *data)
4549 struct MeshPeerInfo *peer = cls;
4550 struct MeshPeerPath *p;
4551 struct GNUNET_PeerIdentity pi;
4554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4555 GNUNET_PEER_resolve (peer->id, &pi);
4556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4558 p = path_build_from_dht (get_path, get_path_length,
4559 put_path, put_path_length);
4560 path_add_to_peers (p, GNUNET_NO);
4562 for (i = 0; i < peer->ntunnels; i++)
4564 struct GNUNET_PeerIdentity id;
4566 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4568 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4569 peer->tunnels[i]->local_tid,
4570 peer->tunnels[i]->local_tid_dest);
4571 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4574 peer_connect (peer, peer->tunnels[i]);
4582 /******************************************************************************/
4583 /********************* MESH LOCAL HANDLES **************************/
4584 /******************************************************************************/
4588 * Handler for client connection.
4590 * @param cls Closure (unused).
4591 * @param client Client handler.
4594 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
4596 struct MeshClient *c;
4600 c = GNUNET_malloc (sizeof (struct MeshClient));
4602 GNUNET_SERVER_client_keep (client);
4603 GNUNET_SERVER_client_set_user_context (client, c);
4604 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4609 * Handler for client disconnection
4611 * @param cls closure
4612 * @param client identification of the client; NULL
4613 * for the last call when the server is destroyed
4616 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4618 struct MeshClient *c;
4619 struct MeshClient *next;
4621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4628 c = client_get (client);
4631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4633 GNUNET_SERVER_client_drop (c->handle);
4634 c->shutting_down = GNUNET_YES;
4635 if (NULL != c->own_tunnels)
4637 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4638 &tunnel_destroy_iterator, c);
4639 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4642 if (NULL != c->incoming_tunnels)
4644 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4645 &tunnel_destroy_iterator, c);
4646 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4649 if (NULL != c->ports)
4650 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4652 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4655 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4660 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
4662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4668 * Handler for new clients
4670 * @param cls closure
4671 * @param client identification of the client
4672 * @param message the actual message, which includes messages the client wants
4675 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4676 const struct GNUNET_MessageHeader *message)
4678 struct GNUNET_MESH_ClientConnect *cc_msg;
4679 struct MeshClient *c;
4684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4686 /* Check data sanity */
4687 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4688 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4689 if (0 != (size % sizeof (uint32_t)))
4692 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4695 size /= sizeof (uint32_t);
4697 /* Initialize new client structure */
4698 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
4699 c->id = next_client_id++; /* overflow not important: just for debug */
4700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4706 p = (uint32_t *) &cc_msg[1];
4707 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4708 for (i = 0; i < size; i++)
4711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4713 /* store in client's hashmap */
4714 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4715 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4716 /* store in global hashmap */
4717 /* FIXME only allow one client to have the port open,
4718 * have a backup hashmap with waiting clients */
4719 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4720 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4724 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4725 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4726 GNUNET_SERVER_notification_context_add (nc, client);
4727 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4729 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4735 * Handler for requests of new tunnels
4737 * @param cls Closure.
4738 * @param client Identification of the client.
4739 * @param message The actual message.
4742 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4743 const struct GNUNET_MessageHeader *message)
4745 struct GNUNET_MESH_TunnelMessage *t_msg;
4746 struct MeshPeerInfo *peer_info;
4747 struct MeshTunnel *t;
4748 struct MeshClient *c;
4749 MESH_TunnelNumber tid;
4751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4753 /* Sanity check for client registration */
4754 if (NULL == (c = client_get (client)))
4757 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4762 /* Message size sanity check */
4763 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4766 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4770 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4772 GNUNET_i2s (&t_msg->peer));
4773 /* Sanity check for tunnel numbering */
4774 tid = ntohl (t_msg->tunnel_id);
4775 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4778 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4781 /* Sanity check for duplicate tunnel IDs */
4782 if (NULL != tunnel_get_by_local_id (c, tid))
4785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4790 while (NULL != tunnel_get_by_pi (myid, next_tid))
4791 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4792 t = tunnel_new (myid, next_tid, c, tid);
4793 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4797 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4800 t->port = ntohl (t_msg->port);
4801 tunnel_set_options (t, ntohl (t_msg->opt));
4802 if (GNUNET_YES == t->reliable)
4804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4805 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
4807 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
4810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4811 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4813 peer_info = peer_get (&t_msg->peer);
4814 peer_info_add_tunnel (peer_info, t);
4815 peer_connect (peer_info, t);
4816 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4822 * Handler for requests of deleting tunnels
4824 * @param cls closure
4825 * @param client identification of the client
4826 * @param message the actual message
4829 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4830 const struct GNUNET_MessageHeader *message)
4832 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4833 struct MeshClient *c;
4834 struct MeshTunnel *t;
4835 MESH_TunnelNumber tid;
4837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4838 "Got a DESTROY TUNNEL from client!\n");
4840 /* Sanity check for client registration */
4841 if (NULL == (c = client_get (client)))
4844 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4849 /* Message sanity check */
4850 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4853 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4857 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4859 /* Retrieve tunnel */
4860 tid = ntohl (tunnel_msg->tunnel_id);
4861 t = tunnel_get_by_local_id(c, tid);
4864 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4866 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4870 /* Cleanup after the tunnel */
4871 client_delete_tunnel (c, t);
4878 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4882 /* The tunnel will be destroyed when the last message is transmitted. */
4883 tunnel_destroy_empty (t);
4885 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4891 * Handler for client traffic
4893 * @param cls closure
4894 * @param client identification of the client
4895 * @param message the actual message
4898 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
4899 const struct GNUNET_MessageHeader *message)
4901 struct GNUNET_MESH_LocalData *data_msg;
4902 struct MeshClient *c;
4903 struct MeshTunnel *t;
4904 struct MeshFlowControl *fc;
4905 MESH_TunnelNumber tid;
4908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4909 "Got data from a client!\n");
4911 /* Sanity check for client registration */
4912 if (NULL == (c = client_get (client)))
4915 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4920 data_msg = (struct GNUNET_MESH_LocalData *) message;
4922 /* Sanity check for message size */
4923 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
4924 if (size < sizeof (struct GNUNET_MessageHeader))
4927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4931 /* Tunnel exists? */
4932 tid = ntohl (data_msg->tid);
4933 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
4936 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4939 t = tunnel_get_by_local_id (c, tid);
4943 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4947 /* Is the client in the tunnel? */
4948 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4950 t->owner->handle == client)
4952 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4954 t->client->handle == client) ) )
4957 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4961 /* Ok, everything is correct, send the message
4962 * (pretend we got it from a mesh peer)
4965 struct GNUNET_MESH_Data *payload;
4966 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
4968 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
4969 if (GNUNET_YES == t->reliable)
4971 struct MeshTunnelReliability *rel;
4972 struct MeshReliableMessage *copy;
4974 rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
4975 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
4976 + sizeof(struct GNUNET_MESH_Data)
4978 copy->mid = rel->mid_sent++;
4979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid);
4980 copy->timestamp = GNUNET_TIME_absolute_get ();
4983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
4984 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
4985 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
4988 GNUNET_TIME_relative_multiply (rel->expected_delay,
4989 MESH_RETRANSMIT_MARGIN);
4991 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
4992 &tunnel_retransmit_message,
4995 payload = (struct GNUNET_MESH_Data *) ©[1];
4996 payload->mid = htonl (copy->mid);
5000 payload = (struct GNUNET_MESH_Data *) cbuf;
5001 payload->mid = htonl (fc->last_pid_recv + 1);
5003 memcpy (&payload[1], &data_msg[1], size);
5004 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
5005 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
5006 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
5007 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5008 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
5009 payload->tid = htonl (t->id.tid);
5010 payload->ttl = htonl (default_ttl);
5011 payload->pid = htonl (fc->last_pid_recv + 1);
5012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5013 " calling generic handler...\n");
5014 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5015 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
5017 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
5019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5020 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5027 * Handler for client's ACKs for payload traffic.
5029 * @param cls Closure (unused).
5030 * @param client Identification of the client.
5031 * @param message The actual message.
5034 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5035 const struct GNUNET_MessageHeader *message)
5037 struct GNUNET_MESH_LocalAck *msg;
5038 struct MeshTunnel *t;
5039 struct MeshClient *c;
5040 MESH_TunnelNumber tid;
5042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5043 /* Sanity check for client registration */
5044 if (NULL == (c = client_get (client)))
5047 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5052 msg = (struct GNUNET_MESH_LocalAck *) message;
5054 /* Tunnel exists? */
5055 tid = ntohl (msg->tunnel_id);
5056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5057 t = tunnel_get_by_local_id (c, tid);
5061 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5062 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5063 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5067 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5068 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5070 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5071 t->prev_fc.last_ack_recv++;
5072 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_NO);
5076 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5077 t->next_fc.last_ack_recv++;
5078 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_YES);
5081 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5089 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5091 * @param cls Closure (client handle).
5092 * @param key Key (hashed tunnel ID, unused).
5093 * @param value Tunnel info.
5095 * @return GNUNET_YES, to keep iterating.
5098 monitor_all_tunnels_iterator (void *cls,
5099 const struct GNUNET_HashCode * key,
5102 struct GNUNET_SERVER_Client *client = cls;
5103 struct MeshTunnel *t = value;
5104 struct GNUNET_MESH_LocalMonitor *msg;
5106 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5107 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5108 msg->tunnel_id = htonl (t->id.tid);
5109 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5110 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5111 GNUNET_PEER_resolve (t->dest, &msg->destination);
5113 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5114 "* sending info about tunnel %s [%u]\n",
5115 GNUNET_i2s (&msg->owner), t->id.tid);
5117 GNUNET_SERVER_notification_context_unicast (nc, client,
5118 &msg->header, GNUNET_NO);
5124 * Handler for client's MONITOR request.
5126 * @param cls Closure (unused).
5127 * @param client Identification of the client.
5128 * @param message The actual message.
5131 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5132 const struct GNUNET_MessageHeader *message)
5134 struct MeshClient *c;
5136 /* Sanity check for client registration */
5137 if (NULL == (c = client_get (client)))
5140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5144 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5145 "Received get tunnels request from client %u\n",
5147 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5148 monitor_all_tunnels_iterator,
5150 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5151 "Get tunnels request from client %u completed\n",
5153 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5158 * Handler for client's MONITOR_TUNNEL request.
5160 * @param cls Closure (unused).
5161 * @param client Identification of the client.
5162 * @param message The actual message.
5165 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5166 const struct GNUNET_MessageHeader *message)
5168 const struct GNUNET_MESH_LocalMonitor *msg;
5169 struct GNUNET_MESH_LocalMonitor *resp;
5170 struct MeshClient *c;
5171 struct MeshTunnel *t;
5173 /* Sanity check for client registration */
5174 if (NULL == (c = client_get (client)))
5177 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5181 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5182 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5183 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5186 ntohl (msg->tunnel_id));
5187 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5190 /* We don't know the tunnel FIXME */
5191 struct GNUNET_MESH_LocalMonitor warn;
5194 GNUNET_SERVER_notification_context_unicast (nc, client,
5197 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5201 /* Initialize context */
5202 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5204 GNUNET_PEER_resolve (t->dest, &resp->destination);
5205 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5206 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5207 &resp->header, GNUNET_NO);
5210 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5211 "Monitor tunnel request from client %u completed\n",
5213 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5218 * Functions to handle messages from clients
5220 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5221 {&handle_local_new_client, NULL,
5222 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5223 {&handle_local_tunnel_create, NULL,
5224 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5225 sizeof (struct GNUNET_MESH_TunnelMessage)},
5226 {&handle_local_tunnel_destroy, NULL,
5227 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5228 sizeof (struct GNUNET_MESH_TunnelMessage)},
5229 {&handle_local_data, NULL,
5230 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5231 {&handle_local_ack, NULL,
5232 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5233 sizeof (struct GNUNET_MESH_LocalAck)},
5234 {&handle_local_get_tunnels, NULL,
5235 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5236 sizeof (struct GNUNET_MessageHeader)},
5237 {&handle_local_show_tunnel, NULL,
5238 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5239 sizeof (struct GNUNET_MESH_LocalMonitor)},
5245 * Method called whenever a given peer connects.
5247 * @param cls closure
5248 * @param peer peer identity this notification is about
5251 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5253 struct MeshPeerInfo *peer_info;
5254 struct MeshPeerPath *path;
5256 DEBUG_CONN ("Peer connected\n");
5257 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5258 peer_info = peer_get (peer);
5259 if (myid == peer_info->id)
5261 DEBUG_CONN (" (self)\n");
5262 path = path_new (1);
5266 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5267 path = path_new (2);
5268 path->peers[1] = peer_info->id;
5269 GNUNET_PEER_change_rc (peer_info->id, 1);
5270 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5272 path->peers[0] = myid;
5273 GNUNET_PEER_change_rc (myid, 1);
5274 peer_info_add_path (peer_info, path, GNUNET_YES);
5280 * Method called whenever a peer disconnects.
5282 * @param cls closure
5283 * @param peer peer identity this notification is about
5286 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5288 struct MeshPeerInfo *pi;
5289 struct MeshPeerQueue *q;
5290 struct MeshPeerQueue *n;
5292 DEBUG_CONN ("Peer disconnected\n");
5293 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5303 /* TODO try to reroute this traffic instead */
5304 queue_destroy(q, GNUNET_YES);
5307 if (NULL != pi->core_transmit)
5309 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5310 pi->core_transmit = NULL;
5312 peer_remove_path (pi, pi->id, myid);
5315 DEBUG_CONN (" (self)\n");
5317 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5323 * Install server (service) handlers and start listening to clients.
5328 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5329 GNUNET_SERVER_connect_notify (server_handle,
5330 &handle_local_client_connect, NULL);
5331 GNUNET_SERVER_disconnect_notify (server_handle,
5332 &handle_local_client_disconnect, NULL);
5333 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5335 clients_head = NULL;
5336 clients_tail = NULL;
5338 GNUNET_SERVER_resume (server_handle);
5343 * To be called on core init/fail.
5345 * @param cls Closure (config)
5346 * @param server handle to the server for this service
5347 * @param identity the public identity of this peer
5350 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5351 const struct GNUNET_PeerIdentity *identity)
5353 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5357 GNUNET_break (core_handle == server);
5358 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5361 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5364 GNUNET_i2s (identity));
5365 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5367 GNUNET_i2s (&my_full_id));
5368 GNUNET_CORE_disconnect (core_handle);
5369 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5370 NULL, /* Closure passed to MESH functions */
5371 &core_init, /* Call core_init once connected */
5372 &core_connect, /* Handle connects */
5373 &core_disconnect, /* remove peers on disconnects */
5374 NULL, /* Don't notify about all incoming messages */
5375 GNUNET_NO, /* For header only in notification */
5376 NULL, /* Don't notify about all outbound messages */
5377 GNUNET_NO, /* For header-only out notification */
5378 core_handlers); /* Register these handlers */
5387 /******************************************************************************/
5388 /************************ MAIN FUNCTIONS ****************************/
5389 /******************************************************************************/
5392 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5394 * @param cls closure
5395 * @param key current key code
5396 * @param value value in the hash map
5397 * @return GNUNET_YES if we should continue to iterate,
5401 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5403 struct MeshTunnel *t = value;
5410 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5412 * @param cls closure
5413 * @param key current key code
5414 * @param value value in the hash map
5415 * @return GNUNET_YES if we should continue to iterate,
5419 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5421 struct MeshPeerInfo *p = value;
5422 struct MeshPeerQueue *q;
5423 struct MeshPeerQueue *n;
5431 queue_destroy(q, GNUNET_YES);
5435 peer_info_destroy (p);
5441 * Task run during shutdown.
5447 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5451 if (core_handle != NULL)
5453 GNUNET_CORE_disconnect (core_handle);
5456 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5457 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5458 if (dht_handle != NULL)
5460 GNUNET_DHT_disconnect (dht_handle);
5465 GNUNET_SERVER_notification_context_destroy (nc);
5468 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5470 GNUNET_SCHEDULER_cancel (announce_id_task);
5471 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5478 * Process mesh requests.
5480 * @param cls closure
5481 * @param server the initialized server
5482 * @param c configuration to use
5485 run (void *cls, struct GNUNET_SERVER_Handle *server,
5486 const struct GNUNET_CONFIGURATION_Handle *c)
5489 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5492 server_handle = server;
5493 GNUNET_SERVER_suspend (server_handle);
5496 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5501 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5502 "mesh", "peer/privatekey");
5503 GNUNET_SCHEDULER_shutdown ();
5508 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5509 &refresh_path_time))
5511 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5513 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5514 "mesh", "refresh path time");
5515 GNUNET_SCHEDULER_shutdown ();
5520 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5523 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5525 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5526 "mesh", "id announce time");
5527 GNUNET_SCHEDULER_shutdown ();
5532 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5535 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5537 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5538 "mesh", "connect timeout");
5539 GNUNET_SCHEDULER_shutdown ();
5544 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5549 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5550 "mesh", "max msgs queue");
5551 GNUNET_SCHEDULER_shutdown ();
5556 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5561 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5562 "mesh", "max tunnels");
5563 GNUNET_SCHEDULER_shutdown ();
5568 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5571 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5573 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5574 "mesh", "default ttl", 64);
5579 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5582 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5583 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5584 "mesh", "max peers", 1000);
5589 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5596 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5597 "Mesh is running with drop mode enabled. "
5598 "This is NOT a good idea! "
5599 "Remove the DROP_PERCENT option from your configuration.\n");
5603 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5604 &dht_replication_level))
5606 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5608 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5609 "mesh", "dht replication level", 3);
5610 dht_replication_level = 3;
5613 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5614 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5615 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5616 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5618 dht_handle = GNUNET_DHT_connect (c, 64);
5619 if (NULL == dht_handle)
5623 stats = GNUNET_STATISTICS_create ("mesh", c);
5625 /* Scheduled the task to clean up when shutdown is called */
5626 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5628 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5629 GNUNET_free (keyfile);
5630 GNUNET_assert (NULL != pk);
5631 my_private_key = pk;
5632 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5633 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5634 &my_full_id.hashPubKey);
5635 myid = GNUNET_PEER_intern (&my_full_id);
5636 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5637 "Mesh for peer [%s] starting\n",
5638 GNUNET_i2s(&my_full_id));
5640 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5641 NULL, /* Closure passed to MESH functions */
5642 &core_init, /* Call core_init once connected */
5643 &core_connect, /* Handle connects */
5644 &core_disconnect, /* remove peers on disconnects */
5645 NULL, /* Don't notify about all incoming messages */
5646 GNUNET_NO, /* For header only in notification */
5647 NULL, /* Don't notify about all outbound messages */
5648 GNUNET_NO, /* For header-only out notification */
5649 core_handlers); /* Register these handlers */
5650 if (NULL == core_handle)
5653 GNUNET_SCHEDULER_shutdown ();
5657 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5658 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5664 * The main function for the mesh service.
5666 * @param argc number of arguments from the command line
5667 * @param argv command line arguments
5668 * @return 0 ok, 1 on error
5671 main (int argc, char *const *argv)
5676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5677 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5679 ret = (GNUNET_OK == r) ? 0 : 1;
5680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");