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
30 * - relay corking down to core
31 * - set ttl relative to path length
39 #include "mesh_protocol.h"
40 #include "mesh_path.h"
41 #include "block_mesh.h"
42 #include "gnunet_dht_service.h"
43 #include "gnunet_statistics_service.h"
45 #define MESH_BLOOM_SIZE 128
47 #define MESH_DEBUG_DHT GNUNET_NO
48 #define MESH_DEBUG_CONNECTION GNUNET_NO
49 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
51 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
52 GNUNET_TIME_UNIT_MINUTES,\
54 #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS
55 #define MESH_RETRANSMIT_MARGIN 4
57 #if MESH_DEBUG_CONNECTION
58 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
60 #define DEBUG_CONN(...)
64 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
66 #define DEBUG_DHT(...)
73 struct timespec __mesh_start;
74 struct timespec __mesh_end;
75 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
76 #define INTERVAL_END \
78 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
79 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
80 if (__diff < 0) __diff += 1000000000;\
84 #define INTERVAL_SHOW \
86 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
88 #define INTERVAL_START
93 /******************************************************************************/
94 /************************ DATA STRUCTURES ****************************/
95 /******************************************************************************/
97 /** FWD declaration */
103 * Struct containing info about a queued transmission to this peer
110 struct MeshPeerQueue *next;
115 struct MeshPeerQueue *prev;
118 * Peer this transmission is directed to.
120 struct MeshPeerInfo *peer;
123 * Tunnel this message belongs to.
125 struct MeshTunnel *tunnel;
128 * Pointer to info stucture used as cls.
138 * Size of the message
145 * Struct containing all information regarding a given peer
155 * Last time we heard from this peer
157 struct GNUNET_TIME_Absolute last_contact;
160 * Number of attempts to reconnect so far
162 int n_reconnect_attempts;
165 * Paths to reach the peer, ordered by ascending hop count
167 struct MeshPeerPath *path_head;
170 * Paths to reach the peer, ordered by ascending hop count
172 struct MeshPeerPath *path_tail;
175 * Handle to stop the DHT search for a path to this peer
177 struct GNUNET_DHT_GetHandle *dhtget;
180 * Array of tunnels this peer is the target of.
181 * Most probably a small amount, therefore not a hashmap.
182 * When the path to the peer changes, notify these tunnels to let them
183 * re-adjust their path trees.
185 struct MeshTunnel **tunnels;
188 * Number of tunnels this peers participates in
190 unsigned int ntunnels;
193 * Transmission queue to core DLL head
195 struct MeshPeerQueue *queue_head;
198 * Transmission queue to core DLL tail
200 struct MeshPeerQueue *queue_tail;
203 * How many messages are in the queue to this peer.
205 unsigned int queue_n;
208 * Handle for queued transmissions
210 struct GNUNET_CORE_TransmitHandle *core_transmit;
215 * Struct to encapsulate all the Flow Control information to a peer in the
216 * context of a tunnel: Same peer in different tunnels will have independent
217 * flow control structures, allowing to choke/free tunnels according to its
220 struct MeshFlowControl
223 * ID of the last packet sent towards the peer.
225 uint32_t last_pid_sent;
228 * ID of the last packet received from the peer.
230 uint32_t last_pid_recv;
233 * Last ACK sent to the peer (peer can't send more than this PID).
235 uint32_t last_ack_sent;
238 * Last ACK sent towards the origin (for traffic towards leaf node).
240 uint32_t last_ack_recv;
243 * How many payload messages are in the queue towards this peer.
248 * Task to poll the peer in case of a lost ACK causes stall.
250 GNUNET_SCHEDULER_TaskIdentifier poll_task;
253 * How frequently to poll for ACKs.
255 struct GNUNET_TIME_Relative poll_time;
258 * On which tunnel to poll.
259 * Using an explicit poll_ctx would not help memory wise,
260 * since the allocated context would have to be stored in the
261 * fc struct in order to free it upon cancelling poll_task.
263 struct MeshTunnel *t;
268 * Globally unique tunnel identification (owner + number)
269 * DO NOT USE OVER THE NETWORK
274 * Node that owns the tunnel
279 * Tunnel number to differentiate all the tunnels owned by the node oid
280 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
282 MESH_TunnelNumber tid;
287 * Data needed for reliable tunnel endpoint retransmission management.
289 struct MeshTunnelReliability;
293 * Info needed to retry a message in case it gets lost.
295 struct MeshReliableMessage
298 * Double linked list, FIFO style
300 struct MeshReliableMessage *next;
301 struct MeshReliableMessage *prev;
304 * Tunnel Reliability queue this message is in.
306 struct MeshTunnelReliability *rel;
309 * ID of the message (ACK needed to free)
314 * When was this message issued (to calculate ACK delay) FIXME update with traffic
316 struct GNUNET_TIME_Absolute timestamp;
318 /* struct GNUNET_MESH_Data with payload */
322 struct MeshTunnelReliability
325 * Tunnel this is about.
327 struct MeshTunnel *t;
330 * DLL of messages sent and not yet ACK'd.
332 struct MeshReliableMessage *head_sent;
333 struct MeshReliableMessage *tail_sent;
346 * DLL of messages received out of order.
348 struct MeshReliableMessage *head_recv;
349 struct MeshReliableMessage *tail_recv;
357 * Task to resend/poll in case no ACK is received.
359 GNUNET_SCHEDULER_TaskIdentifier retry_task;
362 * Counter for exponential backoff.
364 struct GNUNET_TIME_Relative retry_timer;
367 * How long does it usually take to get an ACK. FIXME update with traffic
369 struct GNUNET_TIME_Relative expected_delay;
374 * Struct containing all information regarding a tunnel
375 * For an intermediate node the improtant info used will be:
376 * - id Tunnel unique identification
377 * - paths[0] To know where to send it next
378 * - metainfo: ready, speeds, accounting
385 struct MESH_TunnelID id;
388 * Port of the tunnel.
393 * State of the tunnel.
395 enum MeshTunnelState state;
398 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
400 MESH_TunnelNumber local_tid;
403 * Local tunnel number for local destination clients (incoming number)
404 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
407 MESH_TunnelNumber local_tid_dest;
410 * Is the tunnel bufferless (minimum latency)?
415 * Is the tunnel reliable?
420 * Force sending ACK? Flag to allow duplicate ACK on POLL.
425 * How many messages do we accept in the forward queue.
427 unsigned int queue_max;
430 * Last time the tunnel was used
432 struct GNUNET_TIME_Absolute timestamp;
435 * Destination of the tunnel.
440 * Next hop in the tunnel. If 0, @c client must be set.
442 GNUNET_PEER_Id next_hop;
445 * Previous hop in the tunnel. If 0, @c owner must be set.
447 GNUNET_PEER_Id prev_hop;
450 * Flow control information about @c next_hop or @c client.
452 struct MeshFlowControl next_fc;
455 * Flow control information about @c prev_hop or @c owner.
457 struct MeshFlowControl prev_fc;
460 * Client owner of the tunnel, if any
462 struct MeshClient *owner;
465 * Client destination of the tunnel, if any.
467 struct MeshClient *client;
470 * Task to keep the used paths alive at the owner,
471 * time tunnel out on all the other peers.
473 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
476 * Path being used for the tunnel.
478 struct MeshPeerPath *path;
481 * Flag to signal the destruction of the tunnel.
482 * If this is set GNUNET_YES the tunnel will be destroyed
483 * when the queue is empty.
488 * Total messages pending for this tunnels, payload or not.
490 unsigned int pending_messages;
494 * Only present (non-NULL) at the owner of a tunnel.
496 struct MeshTunnelReliability *fwd_rel;
500 * Only present (non-NULL) at the destination of a tunnel.
502 struct MeshTunnelReliability *bck_rel;
507 * Struct containing information about a client of the service
509 * TODO: add a list of 'waiting' ports
516 struct MeshClient *next;
521 struct MeshClient *prev;
524 * Tunnels that belong to this client, indexed by local id
526 struct GNUNET_CONTAINER_MultiHashMap32 *own_tunnels;
529 * Tunnels this client has accepted, indexed by incoming local id
531 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
534 * Handle to communicate with the client
536 struct GNUNET_SERVER_Client *handle;
539 * Ports that this client has declared interest in.
540 * Indexed by port, contains *Client.
542 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
545 * Whether the client is active or shutting down (don't send confirmations
546 * to a client that is shutting down.
551 * ID of the client, mainly for debug messages
558 /******************************************************************************/
559 /************************ DEBUG FUNCTIONS ****************************/
560 /******************************************************************************/
564 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
565 * @param cls string to print
566 * @param success GNUNET_OK if the PUT was transmitted,
567 * GNUNET_NO on timeout,
568 * GNUNET_SYSERR on disconnect from service
569 * after the PUT message was transmitted
570 * (so we don't know if it was received or not)
575 mesh_debug (void *cls, int success)
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
585 /******************************************************************************/
586 /*********************** GLOBAL VARIABLES ****************************/
587 /******************************************************************************/
589 /************************** Configuration parameters **************************/
592 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
594 static struct GNUNET_TIME_Relative refresh_path_time;
597 * How often to PUT own ID in the DHT.
599 static struct GNUNET_TIME_Relative id_announce_time;
602 * Maximum time allowed to connect to a peer found by string.
604 static struct GNUNET_TIME_Relative connect_timeout;
607 * Default TTL for payload packets.
609 static unsigned long long default_ttl;
612 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
614 static unsigned long long dht_replication_level;
617 * How many tunnels are we willing to maintain.
618 * Local tunnels are always allowed, even if there are more tunnels than max.
620 static unsigned long long max_tunnels;
623 * How many messages *in total* are we willing to queue, divided by number of
624 * tunnels to get tunnel queue size.
626 static unsigned long long max_msgs_queue;
629 * How many peers do we want to remember?
631 static unsigned long long max_peers;
634 * Percentage of messages that will be dropped (for test purposes only).
636 static unsigned long long drop_percent;
638 /*************************** Static global variables **************************/
641 * DLL with all the clients, head.
643 static struct MeshClient *clients_head;
646 * DLL with all the clients, tail.
648 static struct MeshClient *clients_tail;
651 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
653 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
656 * Number of tunnels known.
658 static unsigned long long n_tunnels;
661 * Tunnels incoming, indexed by MESH_TunnelNumber
662 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
664 static struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
667 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
669 static struct GNUNET_CONTAINER_MultiHashMap *peers;
672 * Handle to communicate with transport
674 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
677 * Handle to communicate with core.
679 static struct GNUNET_CORE_Handle *core_handle;
684 static struct GNUNET_DHT_Handle *dht_handle;
689 static struct GNUNET_SERVER_Handle *server_handle;
692 * Handle to the statistics service.
694 static struct GNUNET_STATISTICS_Handle *stats;
697 * Notification context, to send messages to local clients.
699 static struct GNUNET_SERVER_NotificationContext *nc;
702 * Local peer own ID (memory efficient handle).
704 static GNUNET_PEER_Id myid;
707 * Local peer own ID (full value).
709 static struct GNUNET_PeerIdentity my_full_id;
714 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
719 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
722 * Tunnel ID for the next created tunnel (global tunnel number).
724 static MESH_TunnelNumber next_tid;
727 * Tunnel ID for the next incoming tunnel (local tunnel number).
729 static MESH_TunnelNumber next_local_tid;
732 * All ports clients of this peer have opened.
734 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
737 * Task to periodically announce itself in the network.
739 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
742 * Next ID to assign to a client.
744 unsigned int next_client_id;
747 /******************************************************************************/
748 /*********************** DECLARATIONS **************************/
749 /******************************************************************************/
752 * Function to process paths received for a new peer addition. The recorded
753 * paths form the initial tunnel, which can be optimized later.
754 * Called on each result obtained for the DHT search.
757 * @param exp when will this value expire
758 * @param key key of the result
759 * @param type type of the result
760 * @param size number of bytes in data
761 * @param data pointer to the result data
764 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
765 const struct GNUNET_HashCode * key,
766 const struct GNUNET_PeerIdentity *get_path,
767 unsigned int get_path_length,
768 const struct GNUNET_PeerIdentity *put_path,
769 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
770 size_t size, const void *data);
774 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
775 * and insert it in the appropriate structures if the peer is not known yet.
777 * @param peer Full identity of the peer.
779 * @return Existing or newly created peer info.
781 static struct MeshPeerInfo *
782 peer_get (const struct GNUNET_PeerIdentity *peer);
786 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
787 * and insert it in the appropriate structures if the peer is not known yet.
789 * @param peer Short identity of the peer.
791 * @return Existing or newly created peer info.
793 static struct MeshPeerInfo *
794 peer_get_short (const GNUNET_PEER_Id peer);
798 * Build a PeerPath from the paths returned from the DHT, reversing the paths
799 * to obtain a local peer -> destination path and interning the peer ids.
801 * @return Newly allocated and created path
803 static struct MeshPeerPath *
804 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
805 unsigned int get_path_length,
806 const struct GNUNET_PeerIdentity *put_path,
807 unsigned int put_path_length);
811 * Adds a path to the peer_infos of all the peers in the path
813 * @param p Path to process.
814 * @param confirmed Whether we know if the path works or not.
817 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
822 * Search for a tunnel by global ID using full PeerIdentities.
824 * @param oid owner of the tunnel.
825 * @param tid global tunnel number.
827 * @return tunnel handler, NULL if doesn't exist.
829 static struct MeshTunnel *
830 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
834 * Notify a tunnel that a connection has broken that affects at least
837 * @param t Tunnel affected.
838 * @param p1 Peer that got disconnected from p2.
839 * @param p2 Peer that got disconnected from p1.
841 * @return Short ID of the peer disconnected (either p1 or p2).
842 * 0 if the tunnel remained unaffected.
844 static GNUNET_PEER_Id
845 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
850 * @brief Use the given path for the tunnel.
851 * Update the next and prev hops (and RCs).
852 * (Re)start the path refresh in case the tunnel is locally owned.
854 * @param t Tunnel to update.
855 * @param p Path to use.
858 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
861 * Tunnel is empty: destroy it.
863 * Notifies all participants (peers, cleints) about the destruction.
865 * @param t Tunnel to destroy.
868 tunnel_destroy_empty (struct MeshTunnel *t);
871 * @brief Queue and pass message to core when possible.
873 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
874 * and accounts for it. In case the queue is full, the message is dropped and
877 * Otherwise, message is treated as internal and allowed to go regardless of
880 * @param cls Closure (@c type dependant). It will be used by queue_send to
881 * build the message to be sent if not already prebuilt.
882 * @param type Type of the message, 0 for a raw message.
883 * @param size Size of the message.
884 * @param dst Neighbor to send message to.
885 * @param t Tunnel this message belongs to.
888 queue_add (void *cls, uint16_t type, size_t size,
889 struct MeshPeerInfo *dst, struct MeshTunnel *t);
893 * Free a transmission that was already queued with all resources
894 * associated to the request.
896 * @param queue Queue handler to cancel.
897 * @param clear_cls Is it necessary to free associated cls?
900 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
904 * @brief Get the next transmittable message from the queue.
906 * This will be the head, except in the case of being a data packet
907 * not allowed by the destination peer.
909 * @param peer Destination peer.
911 * @return The next viable MeshPeerQueue element to send to that peer.
912 * NULL when there are no transmittable messages.
914 struct MeshPeerQueue *
915 queue_get_next (const struct MeshPeerInfo *peer);
919 * Core callback to write a queued packet to core buffer
921 * @param cls Closure (peer info).
922 * @param size Number of bytes available in buf.
923 * @param buf Where the to write the message.
925 * @return number of bytes written to buf
928 queue_send (void *cls, size_t size, void *buf);
931 /******************************************************************************/
932 /************************ PERIODIC FUNCTIONS ****************************/
933 /******************************************************************************/
936 * Periodically announce self id in the DHT
939 * @param tc task context
942 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
946 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
948 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
952 * - Set data expiration in function of X
955 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
957 block.id = my_full_id;
958 GNUNET_DHT_put (dht_handle, /* DHT handle */
959 &my_full_id.hashPubKey, /* Key to use */
960 dht_replication_level, /* Replication level */
961 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
962 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
963 sizeof (block), /* Size of the data */
964 (const char *) &block, /* Data itself */
965 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
966 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
967 NULL, /* Continuation */
968 NULL); /* Continuation closure */
970 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
974 /******************************************************************************/
975 /****************** GENERAL HELPER FUNCTIONS ************************/
976 /******************************************************************************/
980 * Check if client has registered with the service and has not disconnected
982 * @param client the client to check
984 * @return non-NULL if client exists in the global DLL
986 static struct MeshClient *
987 client_get (struct GNUNET_SERVER_Client *client)
989 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
994 * Deletes a tunnel from a client (either owner or destination). To be used on
997 * @param c Client whose tunnel to delete.
998 * @param t Tunnel which should be deleted.
1001 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1005 GNUNET_assert (GNUNET_YES ==
1006 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
1012 GNUNET_assert (GNUNET_YES ==
1013 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
1020 * Notify the appropriate client that a new incoming tunnel was created.
1022 * @param t Tunnel that was created.
1025 send_client_tunnel_create (struct MeshTunnel *t)
1027 struct GNUNET_MESH_TunnelMessage msg;
1029 if (NULL == t->client)
1031 msg.header.size = htons (sizeof (msg));
1032 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1033 msg.tunnel_id = htonl (t->local_tid_dest);
1034 msg.port = htonl (t->port);
1036 msg.opt |= GNUNET_YES == t->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1037 msg.opt |= GNUNET_YES == t->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1038 msg.opt = htonl (msg.opt);
1039 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
1040 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1041 &msg.header, GNUNET_NO);
1046 * Notify dest client that the incoming tunnel is no longer valid.
1048 * @param c Client to notify..
1049 * @param t Tunnel that is destroyed.
1052 send_client_tunnel_destroy (struct MeshClient *c, struct MeshTunnel *t)
1054 struct GNUNET_MESH_TunnelMessage msg;
1061 if (c != t->client && c != t->owner)
1066 msg.header.size = htons (sizeof (msg));
1067 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1068 msg.tunnel_id = htonl (t->local_tid_dest);
1069 msg.port = htonl (0);
1070 memset(&msg.peer, 0, sizeof (msg.peer));
1071 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1072 &msg.header, GNUNET_NO);
1077 * Iterator over all the peers to remove the oldest not-used entry.
1079 * @param cls Closure (unsued).
1080 * @param key ID of the peer.
1081 * @param value Peer_Info of the peer.
1086 peer_info_timeout (void *cls,
1087 const struct GNUNET_HashCode *key,
1094 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1095 * and insert it in the appropriate structures if the peer is not known yet.
1097 * @param peer Full identity of the peer.
1099 * @return Existing or newly created peer info.
1101 static struct MeshPeerInfo *
1102 peer_get (const struct GNUNET_PeerIdentity *peer)
1104 struct MeshPeerInfo *peer_info;
1106 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1107 if (NULL == peer_info)
1110 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1111 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1113 GNUNET_CONTAINER_multihashmap_iterate (peers,
1117 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1118 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1119 peer_info->id = GNUNET_PEER_intern (peer);
1121 peer_info->last_contact = GNUNET_TIME_absolute_get();
1128 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1129 * and insert it in the appropriate structures if the peer is not known yet.
1131 * @param peer Short identity of the peer.
1133 * @return Existing or newly created peer info.
1135 static struct MeshPeerInfo *
1136 peer_get_short (const GNUNET_PEER_Id peer)
1138 struct GNUNET_PeerIdentity id;
1140 GNUNET_PEER_resolve (peer, &id);
1141 return peer_get (&id);
1146 * Select which PID to POLL for, to compensate for lost messages.
1148 * @param pi Peer we want to poll.
1149 * @param t Tunnel about which we want to poll.
1151 * @return PID to use, either last sent or first_in_queue - 1
1154 peer_get_first_payload_pid (struct MeshPeerInfo *pi, struct MeshTunnel *t)
1156 struct MeshPeerQueue *q;
1159 type = pi->id == t->next_hop ? GNUNET_MESSAGE_TYPE_MESH_UNICAST :
1160 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN;
1162 for (q = pi->queue_head; NULL != q; q = q->next)
1164 if (q->type == type && q->tunnel == t)
1166 struct GNUNET_MESH_Data *msg = q->cls;
1168 /* Pretend that the last one sent was the previous to this */
1169 return ntohl (msg->pid) - 1;
1173 /* No data in queue, use last sent */
1175 struct MeshFlowControl *fc;
1177 fc = pi->id == t->next_hop ? &t->next_fc : &t->prev_fc;
1178 return fc->last_pid_sent;
1184 * Choose the best path towards a peer considering the tunnel properties.
1186 * @param peer The destination peer.
1187 * @param t The tunnel the path is for.
1189 * @return Best current known path towards the peer, if any.
1191 static struct MeshPeerPath *
1192 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1194 struct MeshPeerPath *best_p;
1195 struct MeshPeerPath *p;
1196 unsigned int best_cost;
1199 best_p = p = peer->path_head;
1200 best_cost = cost = p->length;
1203 if ((cost = p->length) < best_cost)
1214 * Core callback to write a pre-constructed data packet to core buffer
1216 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1217 * @param size Number of bytes available in buf.
1218 * @param buf Where the to write the message.
1220 * @return number of bytes written to buf
1223 send_core_data_raw (void *cls, size_t size, void *buf)
1225 struct GNUNET_MessageHeader *msg = cls;
1228 GNUNET_assert (NULL != msg);
1229 total_size = ntohs (msg->size);
1231 if (total_size > size)
1236 memcpy (buf, msg, total_size);
1243 * Sends an already built message to a peer, properly registrating
1244 * all used resources.
1246 * @param message Message to send. Function makes a copy of it.
1247 * @param peer Short ID of the neighbor whom to send the message.
1248 * @param t Tunnel on which this message is transmitted.
1251 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1252 GNUNET_PEER_Id peer,
1253 struct MeshTunnel *t)
1255 struct GNUNET_PeerIdentity id;
1256 struct MeshPeerInfo *neighbor;
1257 struct MeshPeerPath *p;
1262 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1267 size = ntohs (message->size);
1268 data = GNUNET_malloc (size);
1269 memcpy (data, message, size);
1270 type = ntohs(message->type);
1271 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1272 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1274 struct GNUNET_MESH_Data *u;
1276 u = (struct GNUNET_MESH_Data *) data;
1277 u->ttl = htonl (ntohl (u->ttl) - 1);
1279 GNUNET_PEER_resolve (peer, &id);
1280 neighbor = peer_get (&id);
1281 for (p = neighbor->path_head; NULL != p; p = p->next)
1291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1292 " %s IS NOT DIRECTLY CONNECTED\n",
1294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1297 for (p = neighbor->path_head; NULL != p; p = p->next)
1299 struct GNUNET_PeerIdentity debug_id;
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 " path with %u hops through:\n",
1305 for (i = 0; i < p->length; i++)
1307 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1310 i, GNUNET_i2s(&debug_id));
1314 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1316 " no direct connection to %s\n",
1321 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1332 * Sends a CREATE PATH message for a path to a peer, properly registrating
1333 * all used resources.
1335 * @param t Tunnel for which the path is created.
1338 send_create_path (struct MeshTunnel *t)
1340 struct MeshPeerInfo *neighbor;
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1343 neighbor = peer_get_short (t->next_hop);
1345 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1346 sizeof (struct GNUNET_MESH_CreateTunnel) +
1347 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1350 t->state = MESH_TUNNEL_WAITING;
1355 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1357 * @param t Tunnel which to confirm.
1360 send_path_ack (struct MeshTunnel *t)
1362 struct MeshPeerInfo *peer;
1364 peer = peer_get_short (t->prev_hop);
1367 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1368 sizeof (struct GNUNET_MESH_PathACK),
1375 * Try to establish a new connection to this peer in the given tunnel.
1376 * If the peer doesn't have any path to it yet, try to get one.
1377 * If the peer already has some path, send a CREATE PATH towards it.
1379 * @param peer PeerInfo of the peer.
1380 * @param t Tunnel for which to create the path, if possible.
1383 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1385 struct MeshPeerPath *p;
1387 if (NULL != peer->path_head)
1389 p = peer_get_best_path (peer, t);
1390 tunnel_use_path (t, p);
1391 send_create_path (t);
1393 else if (NULL == peer->dhtget)
1395 struct GNUNET_PeerIdentity id;
1397 GNUNET_PEER_resolve (peer->id, &id);
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1399 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1400 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1401 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1402 &id.hashPubKey, /* key to search */
1403 dht_replication_level, /* replication level */
1404 GNUNET_DHT_RO_RECORD_ROUTE |
1405 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1407 0, /* xquery bits */
1408 &dht_get_id_handler, peer);
1409 t->state = MESH_TUNNEL_SEARCHING;
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1414 "There is no path but the DHT GET is already started.\n");
1420 * Destroy the peer_info and free any allocated resources linked to it
1422 * @param pi The peer_info to destroy.
1424 * @return GNUNET_OK on success
1427 peer_info_destroy (struct MeshPeerInfo *pi)
1429 struct GNUNET_PeerIdentity id;
1430 struct MeshPeerPath *p;
1431 struct MeshPeerPath *nextp;
1434 GNUNET_PEER_resolve (pi->id, &id);
1435 GNUNET_PEER_change_rc (pi->id, -1);
1438 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1441 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1442 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1444 if (NULL != pi->dhtget)
1446 GNUNET_DHT_get_stop (pi->dhtget);
1452 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1456 for (i = 0; i < pi->ntunnels; i++)
1457 tunnel_destroy_empty (pi->tunnels[i]);
1458 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1465 * Remove all paths that rely on a direct connection between p1 and p2
1466 * from the peer itself and notify all tunnels about it.
1468 * @param peer PeerInfo of affected peer.
1469 * @param p1 GNUNET_PEER_Id of one peer.
1470 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1473 * TODO: optimize (see below)
1476 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1479 struct MeshPeerPath *p;
1480 struct MeshPeerPath *next;
1481 struct MeshPeerInfo *peer_d;
1483 unsigned int destroyed;
1486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1488 for (p = peer->path_head; NULL != p; p = next)
1491 for (i = 0; i < (p->length - 1); i++)
1493 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1494 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1496 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1506 for (i = 0; i < peer->ntunnels; i++)
1508 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1512 peer_d = peer_get_short (d);
1513 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1514 tunnel_use_path (peer->tunnels[i], next);
1515 peer_connect (peer_d, peer->tunnels[i]);
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1522 * Add the path to the peer and update the path used to reach it in case this
1525 * @param peer_info Destination peer to add the path to.
1526 * @param path New path to add. Last peer must be the peer in arg 1.
1527 * Path will be either used of freed if already known.
1528 * @param trusted Do we trust that this path is real?
1531 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1534 struct MeshPeerPath *aux;
1538 if ((NULL == peer_info) || (NULL == path))
1541 path_destroy (path);
1544 if (path->peers[path->length - 1] != peer_info->id)
1547 path_destroy (path);
1550 if (2 >= path->length && GNUNET_NO == trusted)
1552 /* Only allow CORE to tell us about direct paths */
1553 path_destroy (path);
1556 for (l = 1; l < path->length; l++)
1558 if (path->peers[l] == myid)
1560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1561 for (l2 = 0; l2 < path->length - l; l2++)
1563 path->peers[l2] = path->peers[l + l2];
1568 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1573 struct GNUNET_PeerIdentity id;
1575 GNUNET_PEER_resolve (peer_info->id, &id);
1576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1577 path->length, GNUNET_i2s (&id));
1580 l = path_get_length (path);
1583 path_destroy (path);
1587 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1588 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1590 l2 = path_get_length (aux);
1593 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1594 peer_info->path_tail, aux, path);
1599 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1601 path_destroy (path);
1606 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1613 * Add the path to the origin peer and update the path used to reach it in case
1614 * this is the shortest.
1615 * The path is given in peer_info -> destination, therefore we turn the path
1616 * upside down first.
1618 * @param peer_info Peer to add the path to, being the origin of the path.
1619 * @param path New path to add after being inversed.
1620 * Path will be either used or freed.
1621 * @param trusted Do we trust that this path is real?
1624 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1625 struct MeshPeerPath *path, int trusted)
1628 peer_info_add_path (peer_info, path, trusted);
1633 * Add a tunnel to the list of tunnels a peer participates in.
1634 * Update the tunnel's destination.
1636 * @param p Peer to add to.
1637 * @param t Tunnel to add.
1640 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1644 GNUNET_break (t->dest == p->id);
1648 GNUNET_PEER_change_rc (t->dest, 1);
1649 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1654 * Remove a tunnel from the list of tunnels a peer participates in.
1655 * Free the tunnel's destination.
1657 * @param p Peer to clean.
1658 * @param t Tunnel to remove.
1661 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1665 if (t->dest == p->id)
1667 GNUNET_PEER_change_rc (t->dest, -1);
1670 for (i = 0; i < p->ntunnels; i++)
1672 if (p->tunnels[i] == t)
1674 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1675 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1683 * Function called if the connection to the peer has been stalled for a while,
1684 * possibly due to a missed ACK. Poll the peer about its ACK status.
1686 * @param cls Closure (poll ctx).
1687 * @param tc TaskContext.
1690 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1692 struct MeshFlowControl *fc = cls;
1693 struct GNUNET_MESH_Poll msg;
1694 struct MeshTunnel *t = fc->t;
1695 GNUNET_PEER_Id peer;
1697 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1698 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1705 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1707 if (fc == &t->prev_fc)
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
1712 else if (fc == &t->next_fc)
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
1722 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1723 msg.header.size = htons (sizeof (msg));
1724 msg.tid = htonl (t->id.tid);
1725 msg.pid = htonl (peer_get_first_payload_pid (peer_get_short (peer), t));
1726 send_prebuilt_message (&msg.header, peer, t);
1727 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1728 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1734 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1735 * to obtain a local peer -> destination path and interning the peer ids.
1737 * @return Newly allocated and created path
1739 static struct MeshPeerPath *
1740 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1741 unsigned int get_path_length,
1742 const struct GNUNET_PeerIdentity *put_path,
1743 unsigned int put_path_length)
1745 struct MeshPeerPath *p;
1751 GNUNET_PEER_change_rc (myid, 1);
1752 i = get_path_length;
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1754 for (i--; i >= 0; i--)
1756 id = GNUNET_PEER_intern (&get_path[i]);
1757 if (p->length > 0 && id == p->peers[p->length - 1])
1759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1760 GNUNET_PEER_change_rc (id, -1);
1764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1765 GNUNET_i2s (&get_path[i]));
1767 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1768 p->peers[p->length - 1] = id;
1771 i = put_path_length;
1772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1773 for (i--; i >= 0; i--)
1775 id = GNUNET_PEER_intern (&put_path[i]);
1778 /* PUT path went through us, so discard the path up until now and start
1779 * from here to get a much shorter (and loop-free) path.
1784 if (p->length > 0 && id == p->peers[p->length - 1])
1786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1787 GNUNET_PEER_change_rc (id, -1);
1791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1792 GNUNET_i2s (&put_path[i]));
1794 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1795 p->peers[p->length - 1] = id;
1799 if (get_path_length > 0)
1800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1801 GNUNET_i2s (&get_path[0]));
1802 if (put_path_length > 0)
1803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1804 GNUNET_i2s (&put_path[0]));
1805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1807 for (i = 0; i < p->length; i++)
1809 struct GNUNET_PeerIdentity peer_id;
1811 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1813 GNUNET_i2s (&peer_id));
1821 * Adds a path to the peer_infos of all the peers in the path
1823 * @param p Path to process.
1824 * @param confirmed Whether we know if the path works or not.
1827 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1831 /* TODO: invert and add */
1832 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1833 for (i++; i < p->length; i++)
1835 struct MeshPeerInfo *aux;
1836 struct MeshPeerPath *copy;
1838 aux = peer_get_short (p->peers[i]);
1839 copy = path_duplicate (p);
1840 copy->length = i + 1;
1841 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1847 * Send keepalive packets for a peer
1849 * @param cls Closure (tunnel for which to send the keepalive).
1850 * @param tc Notification context.
1853 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1857 * Search for a tunnel among the incoming tunnels
1859 * @param tid the local id of the tunnel
1861 * @return tunnel handler, NULL if doesn't exist
1863 static struct MeshTunnel *
1864 tunnel_get_incoming (MESH_TunnelNumber tid)
1866 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1867 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
1872 * Search for a tunnel among the tunnels for a client
1874 * @param c the client whose tunnels to search in
1875 * @param tid the local id of the tunnel
1877 * @return tunnel handler, NULL if doesn't exist
1879 static struct MeshTunnel *
1880 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1882 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1884 return tunnel_get_incoming (tid);
1888 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI);
1889 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
1895 * Search for a tunnel by global ID using PEER_ID
1897 * @param pi owner of the tunnel
1898 * @param tid global tunnel number
1900 * @return tunnel handler, NULL if doesn't exist
1902 static struct MeshTunnel *
1903 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1905 struct MESH_TunnelID id;
1906 struct GNUNET_HashCode hash;
1911 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1912 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1917 * Search for a tunnel by global ID using full PeerIdentities
1919 * @param oid owner of the tunnel
1920 * @param tid global tunnel number
1922 * @return tunnel handler, NULL if doesn't exist
1924 static struct MeshTunnel *
1925 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1927 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1932 * Add a client to a tunnel, initializing all needed data structures.
1934 * @param t Tunnel to which add the client.
1935 * @param c Client which to add to the tunnel.
1938 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1940 if (NULL != t->client)
1946 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
1947 t->local_tid_dest, t,
1948 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1954 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
1955 t->local_tid_dest, t,
1956 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1966 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1968 unsigned int own_pos;
1970 for (own_pos = 0; own_pos < p->length; own_pos++)
1972 if (p->peers[own_pos] == myid)
1975 if (own_pos > p->length - 1)
1981 if (own_pos < p->length - 1)
1982 t->next_hop = p->peers[own_pos + 1];
1984 t->next_hop = p->peers[own_pos];
1985 GNUNET_PEER_change_rc (t->next_hop, 1);
1987 t->prev_hop = p->peers[own_pos - 1];
1989 t->prev_hop = p->peers[0];
1990 GNUNET_PEER_change_rc (t->prev_hop, 1);
1992 if (NULL != t->path)
1993 path_destroy (t->path);
1994 t->path = path_duplicate (p);
1997 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1998 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1999 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
2006 * Notifies a tunnel that a connection has broken that affects at least
2007 * some of its peers. Sends a notification towards the root of the tree.
2008 * In case the peer is the owner of the tree, notifies the client that owns
2009 * the tunnel and tries to reconnect.
2011 * @param t Tunnel affected.
2012 * @param p1 Peer that got disconnected from p2.
2013 * @param p2 Peer that got disconnected from p1.
2015 * @return Short ID of the peer disconnected (either p1 or p2).
2016 * 0 if the tunnel remained unaffected.
2018 static GNUNET_PEER_Id
2019 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2022 // if (myid != p1 && myid != p2) FIXME
2027 // if (tree_get_predecessor (t->tree) != 0)
2029 // /* We are the peer still connected, notify owner of the disconnection. */
2030 // struct GNUNET_MESH_PathBroken msg;
2031 // struct GNUNET_PeerIdentity neighbor;
2033 // msg.header.size = htons (sizeof (msg));
2034 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2035 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2036 // msg.tid = htonl (t->id.tid);
2037 // msg.peer1 = my_full_id;
2038 // GNUNET_PEER_resolve (pid, &msg.peer2);
2039 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2040 // send_prebuilt_message (&msg.header, &neighbor, t);
2047 * Build a local ACK message and send it to a local client.
2049 * @param t Tunnel on which to send the ACK.
2050 * @param c Client to whom send the ACK.
2051 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
2054 send_local_ack (struct MeshTunnel *t,
2055 struct MeshClient *c,
2058 struct GNUNET_MESH_LocalAck msg;
2060 msg.header.size = htons (sizeof (msg));
2061 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2062 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
2063 GNUNET_SERVER_notification_context_unicast (nc,
2070 * Build an ACK message and queue it to send to the given peer.
2072 * @param t Tunnel on which to send the ACK.
2073 * @param peer Peer to whom send the ACK.
2074 * @param ack Value of the ACK.
2077 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
2079 struct GNUNET_MESH_ACK msg;
2081 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2082 msg.header.size = htons (sizeof (msg));
2083 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2084 msg.pid = htonl (ack);
2085 msg.tid = htonl (t->id.tid);
2087 send_prebuilt_message (&msg.header, peer, t);
2092 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2094 * @param t Tunnel this is about.
2095 * @param fwd Is for FWD traffic? (ACK dest->owner)
2098 tunnel_send_data_ack (struct MeshTunnel *t, int fwd)
2100 struct GNUNET_MESH_DataACK msg;
2101 struct MeshTunnelReliability *rel;
2102 struct MeshReliableMessage *copy;
2107 rel = fwd ? t->bck_rel : t->fwd_rel;
2108 hop = fwd ? t->prev_hop : t->next_hop;
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2110 "send_data_ack for %u\n",
2113 if (GNUNET_NO == t->reliable)
2115 GNUNET_break_op (0);
2118 msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK :
2119 GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2120 msg.header.size = htons (sizeof (msg));
2121 msg.tid = htonl (t->id.tid);
2122 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2123 msg.mid = htonl (rel->mid_recv - 1);
2125 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2127 delta = copy->mid - rel->mid_recv;
2130 mask = 0x1LL << delta;
2131 msg.futures |= mask;
2132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2133 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2134 copy->mid, delta, mask, msg.futures);
2136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2138 send_prebuilt_message (&msg.header, hop, t);
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
2144 * Send an ACK informing the predecessor about the available buffer space.
2145 * In case there is no predecessor, inform the owning client.
2146 * If buffering is off, send only on behalf of children or self if endpoint.
2147 * If buffering is on, send when sent to children and buffer space is free.
2148 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2149 * the ACK itself goes "back" (towards root).
2151 * @param t Tunnel on which to send the ACK.
2152 * @param type Type of message that triggered the ACK transmission.
2153 * @param fwd Is this FWD ACK? (Going dest->owner)
2156 tunnel_send_ack (struct MeshTunnel *t, uint16_t type, int fwd)
2158 struct MeshTunnelReliability *rel;
2159 struct MeshFlowControl *next_fc;
2160 struct MeshFlowControl *prev_fc;
2161 struct MeshClient *c;
2162 struct MeshClient *o;
2168 rel = fwd ? t->fwd_rel : t->bck_rel;
2169 c = fwd ? t->client : t->owner;
2170 o = fwd ? t->owner : t->client;
2171 next_fc = fwd ? &t->next_fc : &t->prev_fc;
2172 prev_fc = fwd ? &t->prev_fc : &t->next_fc;
2173 hop = fwd ? t->prev_hop : t->next_hop;
2177 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2178 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2181 GNUNET_MESH_DEBUG_M2S (type));
2182 if (GNUNET_YES == t->nobuffer)
2184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2187 if (GNUNET_YES == t->reliable && NULL != c)
2188 tunnel_send_data_ack (t, fwd);
2190 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2191 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
2192 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2193 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2195 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2196 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2197 t->force_ack = GNUNET_YES;
2203 /* Check if we need to transmit the ACK */
2205 if (0 && NULL == o &&
2206 t->queue_max > next_fc->queue_n * 4 &&
2207 GMC_is_pid_bigger (prev_fc->last_ack_sent, prev_fc->last_pid_recv) &&
2208 GNUNET_NO == t->force_ack)
2210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2212 " t->qmax: %u, t->qn: %u\n",
2213 t->queue_max, next_fc->queue_n);
2214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2215 " t->pid: %u, t->ack: %u\n",
2216 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2220 /* Ok, ACK might be necessary, what PID to ACK? */
2221 delta = t->queue_max - next_fc->queue_n;
2222 if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent)
2223 delta_mid = rel->mid_sent - rel->head_sent->mid;
2226 if (0 > delta || (GNUNET_YES == t->reliable &&
2228 (10 < rel->n_sent || 64 <= delta_mid)))
2230 if (NULL != o && delta > 1)
2232 ack = prev_fc->last_pid_recv + delta;
2233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2235 " last pid %u, last ack %u, qmax %u, q %u\n",
2236 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2237 t->queue_max, next_fc->queue_n);
2238 if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack)
2240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2244 prev_fc->last_ack_sent = ack;
2246 send_local_ack (t, o, fwd);
2248 send_ack (t, hop, ack);
2251 t->force_ack = GNUNET_NO;
2256 * Modify the mesh message TID from global to local and send to client.
2258 * @param t Tunnel on which to send the message.
2259 * @param msg Message to modify and send.
2260 * @param c Client to send to.
2261 * @param tid Tunnel ID to use (c can be both owner and client).
2264 tunnel_send_client_to_tid (struct MeshTunnel *t,
2265 const struct GNUNET_MESH_Data *msg,
2266 struct MeshClient *c, MESH_TunnelNumber tid)
2268 struct GNUNET_MESH_LocalData *copy;
2269 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2270 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2272 if (size < sizeof (struct GNUNET_MessageHeader))
2274 GNUNET_break_op (0);
2282 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2283 memcpy (©[1], &msg[1], size);
2284 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2285 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2286 copy->tid = htonl (tid);
2287 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2288 ©->header, GNUNET_NO);
2292 * Modify the unicast message TID from global to local and send to client.
2294 * @param t Tunnel on which to send the message.
2295 * @param msg Message to modify and send.
2296 * @param fwd Forward?
2299 tunnel_send_client_data (struct MeshTunnel *t,
2300 const struct GNUNET_MESH_Data *msg,
2304 tunnel_send_client_to_tid (t, msg, t->client, t->local_tid_dest);
2306 tunnel_send_client_to_tid (t, msg, t->owner, t->local_tid);
2311 * Send up to 64 buffered messages to the client for in order delivery.
2313 * @param t Tunnel on which to empty the message buffer.
2314 * @param c Client to send to.
2315 * @param rel Reliability structure to corresponding peer.
2316 * If rel == t->bck_rel, this is FWD data.
2319 tunnel_send_client_buffered_data (struct MeshTunnel *t, struct MeshClient *c,
2320 struct MeshTunnelReliability *rel)
2323 struct MeshReliableMessage *copy;
2324 struct MeshReliableMessage *next;
2326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
2327 for (copy = rel->head_recv; NULL != copy; copy = next)
2330 if (copy->mid == rel->mid_recv)
2332 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
2334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2335 " have %u! now expecting %u\n",
2336 copy->mid, rel->mid_recv + 1);
2337 tunnel_send_client_data (t, msg, (rel == t->bck_rel));
2339 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2345 " don't have %u, next is %u\n",
2351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
2356 * We have received a message out of order, buffer it until we receive
2357 * the missing one and we can feed the rest to the client.
2359 * @param t Tunnel to add to.
2360 * @param msg Message to buffer.
2361 * @param rel Reliability data to the corresponding direction.
2364 tunnel_add_buffered_data (struct MeshTunnel *t,
2365 const struct GNUNET_MESH_Data *msg,
2366 struct MeshTunnelReliability *rel)
2368 struct MeshReliableMessage *copy;
2369 struct MeshReliableMessage *prev;
2373 size = ntohs (msg->header.size);
2374 mid = ntohl (msg->mid);
2375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
2377 copy = GNUNET_malloc (sizeof (*copy) + size);
2380 memcpy (©[1], msg, size);
2382 // FIXME do something better than O(n), although n < 64...
2383 // FIXME start from the end (most messages are the latest ones)
2384 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
2386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
2387 if (GMC_is_pid_bigger (prev->mid, mid))
2389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
2390 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
2395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
2396 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
2397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
2402 * Destroy a reliable message after it has been acknowledged, either by
2403 * direct mid ACK or bitfield. Updates the appropriate data structures and
2404 * timers and frees all memory.
2406 * @param copy Message that is no longer needed: remote peer got it.
2409 tunnel_free_reliable_message (struct MeshReliableMessage *copy)
2411 struct MeshTunnelReliability *rel;
2412 struct GNUNET_TIME_Relative time;
2415 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
2416 rel->expected_delay.rel_value *= 7;
2417 rel->expected_delay.rel_value += time.rel_value;
2418 rel->expected_delay.rel_value /= 8;
2420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
2421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
2422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
2423 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
2425 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
2427 rel->retry_timer = rel->expected_delay;
2428 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2434 * Mark future messages as ACK'd.
2436 * @param t Tunnel whose sent buffer to clean.
2437 * @param msg DataACK message with a bitfield of future ACK'd messages.
2438 * @param rel Reliability data.
2441 tunnel_free_sent_reliable (struct MeshTunnel *t,
2442 const struct GNUNET_MESH_DataACK *msg,
2443 struct MeshTunnelReliability *rel)
2445 struct MeshReliableMessage *copy;
2446 struct MeshReliableMessage *next;
2453 bitfield = msg->futures;
2454 mid = ntohl (msg->mid);
2455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2456 "free_sent_reliable %u %llX\n",
2458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2459 " rel %p, head %p\n",
2460 rel, rel->head_sent);
2461 for (i = 0, copy = rel->head_sent;
2462 i < 64 && NULL != copy && 0 != bitfield;
2465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2466 " trying bit %u (mid %u)\n",
2469 if (0 == (bitfield & mask))
2472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
2473 /* Bit was set, clear the bit from the bitfield */
2476 /* The i-th bit was set. Do we have that copy? */
2477 /* Skip copies with mid < target */
2478 target = mid + i + 1;
2479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
2480 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
2483 /* Did we run out of copies? (previously freed, it's ok) */
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
2490 /* Did we overshoot the target? (previously freed, it's ok) */
2491 if (GMC_is_pid_bigger (copy->mid, target))
2493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
2497 /* Now copy->mid == target, free it */
2499 tunnel_free_reliable_message (copy);
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
2507 * We haven't received an ACK after a certain time: restransmit the message.
2509 * @param cls Closure (MeshReliableMessage with the message to restransmit)
2510 * @param tc TaskContext.
2513 tunnel_retransmit_message (void *cls,
2514 const struct GNUNET_SCHEDULER_TaskContext *tc)
2516 struct MeshTunnelReliability *rel = cls;
2517 struct MeshReliableMessage *copy;
2518 struct MeshFlowControl *fc;
2519 struct MeshPeerQueue *q;
2520 struct MeshPeerInfo *pi;
2521 struct MeshTunnel *t;
2522 struct GNUNET_MESH_Data *payload;
2525 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2526 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2530 copy = rel->head_sent;
2537 /* Search the message to be retransmitted in the outgoing queue */
2538 payload = (struct GNUNET_MESH_Data *) ©[1];
2539 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
2540 fc = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc;
2541 pi = peer_get_short (hop);
2542 for (q = pi->queue_head; NULL != q; q = q->next)
2544 if (ntohs (payload->header.type) == q->type)
2546 struct GNUNET_MESH_Data *queued_data = q->cls;
2548 if (queued_data->mid == payload->mid)
2553 /* Message not found in the queue */
2556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
2558 fc->last_ack_sent++;
2559 fc->last_pid_recv++;
2560 payload->pid = htonl (fc->last_pid_recv);
2561 send_prebuilt_message (&payload->header, hop, t);
2562 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! STILL IN QUEUE %u\n", copy->mid);
2569 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
2570 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
2571 &tunnel_retransmit_message,
2577 * @brief Re-initiate traffic to this peer if necessary.
2579 * Check if there is traffic queued towards this peer
2580 * and the core transmit handle is NULL (traffic was stalled).
2581 * If so, call core tmt rdy.
2583 * @param peer_id Short ID of peer to which initiate traffic.
2586 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2588 struct MeshPeerInfo *peer;
2589 struct GNUNET_PeerIdentity id;
2590 struct MeshPeerQueue *q;
2593 peer = peer_get_short (peer_id);
2594 if (NULL != peer->core_transmit)
2597 q = queue_get_next (peer);
2600 /* Might br multicast traffic already sent to this particular peer but
2601 * not to other children in this tunnel.
2602 * This way t->queue_n would be > 0 but the queue of this particular peer
2608 GNUNET_PEER_resolve (peer->id, &id);
2609 peer->core_transmit =
2610 GNUNET_CORE_notify_transmit_ready(core_handle,
2613 GNUNET_TIME_UNIT_FOREVER_REL,
2623 * Send a message to all peers and clients in this tunnel that the tunnel
2624 * is no longer valid. If some peer or client should not receive the message,
2625 * should be zero'ed out before calling this function.
2627 * @param t The tunnel whose peers and clients to notify.
2630 tunnel_send_destroy (struct MeshTunnel *t)
2632 struct GNUNET_MESH_TunnelDestroy msg;
2633 struct GNUNET_PeerIdentity id;
2635 msg.header.size = htons (sizeof (msg));
2636 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2637 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2638 msg.tid = htonl (t->id.tid);
2639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2640 " sending tunnel destroy for tunnel: %s [%X]\n",
2641 GNUNET_i2s (&msg.oid), t->id.tid);
2643 if (NULL == t->client && 0 != t->next_hop)
2645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2646 GNUNET_PEER_resolve (t->next_hop, &id);
2647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2648 " sending forward to %s\n",
2650 send_prebuilt_message (&msg.header, t->next_hop, t);
2652 if (NULL == t->owner && 0 != t->prev_hop)
2654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2655 GNUNET_PEER_resolve (t->prev_hop, &id);
2656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2657 " sending back to %s\n",
2659 send_prebuilt_message (&msg.header, t->prev_hop, t);
2661 if (NULL != t->owner)
2663 send_client_tunnel_destroy (t->owner, t);
2665 if (NULL != t->client)
2667 send_client_tunnel_destroy (t->client, t);
2673 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2675 * @param t Tunnel which to cancel.
2676 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2679 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2681 struct MeshPeerInfo *peer_info;
2682 struct MeshPeerQueue *pq;
2683 struct MeshPeerQueue *next;
2686 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2687 peer_info = peer_get_short (neighbor);
2688 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2691 if (pq->tunnel == t)
2693 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2694 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2696 /* Should have been removed on destroy children */
2699 queue_destroy (pq, GNUNET_YES);
2702 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2704 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2705 peer_info->core_transmit = NULL;
2711 * Destroy the tunnel.
2713 * This function does not generate any warning traffic to clients or peers.
2716 * Remove the tunnel from peer_info's and clients' hashmaps.
2717 * Cancel messages belonging to this tunnel queued to neighbors.
2718 * Free any allocated resources linked to the tunnel.
2720 * @param t the tunnel to destroy
2722 * @return GNUNET_OK on success
2725 tunnel_destroy (struct MeshTunnel *t)
2727 struct MeshClient *c;
2728 struct GNUNET_HashCode hash;
2738 struct GNUNET_PeerIdentity id;
2740 GNUNET_PEER_resolve (t->id.oid, &id);
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2742 GNUNET_i2s (&id), t->id.tid);
2744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2748 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2749 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2758 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels, t->local_tid, t))
2765 if (NULL != t->client)
2769 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
2770 t->local_tid_dest, t))
2776 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
2777 t->local_tid_dest, t))
2784 if (0 != t->prev_hop)
2786 peer_cancel_queues (t->prev_hop, t);
2787 GNUNET_PEER_change_rc (t->prev_hop, -1);
2789 if (0 != t->next_hop)
2791 peer_cancel_queues (t->next_hop, t);
2792 GNUNET_PEER_change_rc (t->next_hop, -1);
2795 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2798 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2799 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2802 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2803 path_destroy (t->path);
2809 * Tunnel is empty: destroy it.
2811 * Notifies all participants (peers, cleints) about the destruction.
2813 * @param t Tunnel to destroy.
2816 tunnel_destroy_empty (struct MeshTunnel *t)
2820 struct GNUNET_PeerIdentity id;
2822 GNUNET_PEER_resolve (t->id.oid, &id);
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2824 "executing destruction of empty tunnel %s [%X]\n",
2825 GNUNET_i2s (&id), t->id.tid);
2829 if (GNUNET_NO == t->destroy)
2830 tunnel_send_destroy (t);
2831 if (0 == t->pending_messages)
2834 t->destroy = GNUNET_YES;
2838 * Initialize a Flow Control structure to the initial state.
2840 * @param fc Flow Control structure to initialize.
2843 fc_init (struct MeshFlowControl *fc)
2845 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2846 fc->last_pid_recv = (uint32_t) -1;
2847 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2848 fc->last_ack_recv = (uint32_t) -1;
2849 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2850 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2855 * Create a new tunnel
2857 * @param owner Who is the owner of the tunnel (short ID).
2858 * @param tid Tunnel Number of the tunnel.
2859 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2860 * @param local Tunnel Number for the tunnel, for the client point of view.
2862 * @return A new initialized tunnel. NULL on error.
2864 static struct MeshTunnel *
2865 tunnel_new (GNUNET_PEER_Id owner,
2866 MESH_TunnelNumber tid,
2867 struct MeshClient *client,
2868 MESH_TunnelNumber local)
2870 struct MeshTunnel *t;
2871 struct GNUNET_HashCode hash;
2873 if (n_tunnels >= max_tunnels && NULL == client)
2876 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2879 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2881 fc_init (&t->next_fc);
2882 fc_init (&t->prev_fc);
2883 t->local_tid = local;
2885 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2887 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2889 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2890 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2897 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2905 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
2907 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2911 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2921 * Set options in a tunnel, extracted from a bit flag field
2923 * @param t Tunnel to set options to.
2924 * @param options Bit array in host byte order.
2927 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
2929 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
2930 GNUNET_YES : GNUNET_NO;
2931 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
2932 GNUNET_YES : GNUNET_NO;
2937 * Iterator for deleting each tunnel whose client endpoint disconnected.
2939 * @param cls Closure (client that has disconnected).
2940 * @param key The local tunnel id (used to access the hashmap).
2941 * @param value The value stored at the key (tunnel to destroy).
2943 * @return GNUNET_OK, keep iterating.
2946 tunnel_destroy_iterator (void *cls,
2950 struct MeshTunnel *t = value;
2951 struct MeshClient *c = cls;
2953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2954 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2955 t->local_tid, t->local_tid_dest, c->id);
2956 client_delete_tunnel (c, t);
2959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2961 if (0 != t->next_hop) { /* destroy could come before a path is used */
2962 GNUNET_PEER_change_rc (t->next_hop, -1);
2968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2970 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2971 GNUNET_PEER_change_rc (t->prev_hop, -1);
2976 tunnel_destroy_empty (t);
2983 * Timeout function, destroys tunnel if called
2985 * @param cls Closure (tunnel to destroy).
2986 * @param tc TaskContext
2989 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2991 struct MeshTunnel *t = cls;
2992 struct GNUNET_PeerIdentity id;
2994 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2995 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2997 GNUNET_PEER_resolve(t->id.oid, &id);
2998 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2999 "Tunnel %s [%X] timed out. Destroying.\n",
3000 GNUNET_i2s(&id), t->id.tid);
3001 if (NULL != t->client)
3002 send_client_tunnel_destroy (t->client, t);
3003 tunnel_destroy (t); /* Do not notify other */
3008 * Resets the tunnel timeout. Starts it if no timeout was running.
3010 * @param t Tunnel whose timeout to reset.
3011 * @param fwd Is this forward?
3013 * TODO use heap to improve efficiency of scheduler.
3014 * FIXME use fwd, keep 2 timers
3017 tunnel_reset_timeout (struct MeshTunnel *t, int fwd)
3019 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
3021 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
3022 GNUNET_SCHEDULER_cancel (t->maintenance_task);
3023 t->maintenance_task =
3024 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3025 (refresh_path_time, 4), &tunnel_timeout, t);
3029 /******************************************************************************/
3030 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3031 /******************************************************************************/
3034 * Function to send a create path packet to a peer.
3036 * @param cls closure
3037 * @param size number of bytes available in buf
3038 * @param buf where the callee should write the message
3039 * @return number of bytes written to buf
3042 send_core_path_create (void *cls, size_t size, void *buf)
3044 struct MeshTunnel *t = cls;
3045 struct GNUNET_MESH_CreateTunnel *msg;
3046 struct GNUNET_PeerIdentity *peer_ptr;
3047 struct MeshPeerPath *p = t->path;
3052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3054 sizeof (struct GNUNET_MESH_CreateTunnel) +
3055 p->length * sizeof (struct GNUNET_PeerIdentity);
3057 if (size < size_needed || NULL == buf)
3062 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
3063 msg->header.size = htons (size_needed);
3064 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3065 msg->tid = ntohl (t->id.tid);
3068 if (GNUNET_YES == t->nobuffer)
3069 opt |= GNUNET_MESH_OPTION_NOBUFFER;
3070 if (GNUNET_YES == t->reliable)
3071 opt |= GNUNET_MESH_OPTION_RELIABLE;
3072 msg->opt = htonl (opt);
3073 msg->port = htonl (t->port);
3075 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3076 for (i = 0; i < p->length; i++)
3078 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3082 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3088 * Creates a path ack message in buf and frees all unused resources.
3090 * @param cls closure (MeshTransmissionDescriptor)
3091 * @param size number of bytes available in buf
3092 * @param buf where the callee should write the message
3093 * @return number of bytes written to buf
3096 send_core_path_ack (void *cls, size_t size, void *buf)
3098 struct MeshTunnel *t = cls;
3099 struct GNUNET_MESH_PathACK *msg = buf;
3101 GNUNET_assert (NULL != t);
3102 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3107 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
3108 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3109 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3110 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
3111 msg->tid = htonl (t->id.tid);
3112 msg->peer_id = my_full_id;
3113 msg->ack = htonl (t->prev_fc.last_ack_sent);
3115 /* TODO add signature */
3117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3118 return sizeof (struct GNUNET_MESH_PathACK);
3123 * Free a transmission that was already queued with all resources
3124 * associated to the request.
3126 * @param queue Queue handler to cancel.
3127 * @param clear_cls Is it necessary to free associated cls?
3130 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3132 struct MeshFlowControl *fc;
3134 if (GNUNET_YES == clear_cls)
3136 switch (queue->type)
3138 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3139 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3140 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3142 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3143 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3144 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3145 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3146 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3148 " prebuilt message\n");
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3151 GNUNET_MESH_DEBUG_M2S (queue->type));
3153 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3159 " type %s unknown!\n",
3160 GNUNET_MESH_DEBUG_M2S (queue->type));
3162 GNUNET_free_non_null (queue->cls);
3164 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3165 queue->peer->queue_tail,
3168 /* Delete from appropriate fc in the tunnel */
3169 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
3170 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
3172 if (queue->peer->id == queue->tunnel->prev_hop)
3173 fc = &queue->tunnel->prev_fc;
3174 else if (queue->peer->id == queue->tunnel->next_hop)
3175 fc = &queue->tunnel->next_fc;
3179 GNUNET_free (queue);
3184 GNUNET_free (queue);
3189 * @brief Get the next transmittable message from the queue.
3191 * This will be the head, except in the case of being a data packet
3192 * not allowed by the destination peer.
3194 * @param peer Destination peer.
3196 * @return The next viable MeshPeerQueue element to send to that peer.
3197 * NULL when there are no transmittable messages.
3199 struct MeshPeerQueue *
3200 queue_get_next (const struct MeshPeerInfo *peer)
3202 struct MeshPeerQueue *q;
3204 struct GNUNET_MESH_Data *dmsg;
3205 struct MeshTunnel* t;
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
3210 for (q = peer->queue_head; NULL != q; q = q->next)
3213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3215 GNUNET_MESH_DEBUG_M2S (q->type));
3216 dmsg = (struct GNUNET_MESH_Data *) q->cls;
3217 pid = ntohl (dmsg->pid);
3220 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3221 ack = t->next_fc.last_ack_recv;
3223 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3224 ack = t->prev_fc.last_ack_recv;
3227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3232 "* ACK: %u, PID: %u, MID: %u\n",
3233 ack, pid, ntohl (dmsg->mid));
3234 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3247 "* nothing found\n");
3253 queue_send (void *cls, size_t size, void *buf)
3255 struct MeshPeerInfo *peer = cls;
3256 struct GNUNET_MessageHeader *msg;
3257 struct MeshPeerQueue *queue;
3258 struct MeshTunnel *t;
3259 struct GNUNET_PeerIdentity dst_id;
3260 struct MeshFlowControl *fc;
3265 peer->core_transmit = NULL;
3267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3268 queue = queue_get_next (peer);
3270 /* Queue has no internal mesh traffic nor sendable payload */
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3274 if (NULL == peer->queue_head)
3275 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3280 GNUNET_PEER_resolve (peer->id, &dst_id);
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3283 GNUNET_i2s (&dst_id));
3284 /* Check if buffer size is enough for the message */
3285 if (queue->size > size)
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3288 "* not enough room, reissue\n");
3289 peer->core_transmit =
3290 GNUNET_CORE_notify_transmit_ready (core_handle,
3293 GNUNET_TIME_UNIT_FOREVER_REL,
3300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3303 GNUNET_assert (0 < t->pending_messages);
3304 t->pending_messages--;
3308 switch (queue->type)
3311 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3312 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3313 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3314 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3315 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3316 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3317 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3318 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3321 GNUNET_MESH_DEBUG_M2S (queue->type));
3323 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3324 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3325 data_size = send_core_data_raw (queue->cls, size, buf);
3326 msg = (struct GNUNET_MessageHeader *) buf;
3327 type = ntohs (msg->type);
3329 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3331 data_size = send_core_path_create (queue->cls, size, buf);
3333 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3335 data_size = send_core_path_ack (queue->cls, size, buf);
3339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3340 "* type unknown: %u\n",
3345 if (0 < drop_percent &&
3346 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3348 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3349 "Dropping message of type %s\n",
3350 GNUNET_MESH_DEBUG_M2S(queue->type));
3353 /* Free queue, but cls was freed by send_core_* */
3354 queue_destroy (queue, GNUNET_NO);
3356 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3357 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3361 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3362 t->next_fc.last_pid_sent = pid;
3363 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, GNUNET_YES);
3364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3366 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3368 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3369 t->prev_fc.last_pid_sent = pid;
3370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3372 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3373 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, GNUNET_NO);
3379 /* If more data in queue, send next */
3380 queue = queue_get_next (peer);
3383 struct GNUNET_PeerIdentity id;
3385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3386 GNUNET_PEER_resolve (peer->id, &id);
3387 peer->core_transmit =
3388 GNUNET_CORE_notify_transmit_ready(core_handle,
3391 GNUNET_TIME_UNIT_FOREVER_REL,
3397 if (NULL != peer->queue_head)
3399 if (peer->id == t->next_hop)
3401 else if (peer->id == t->prev_hop)
3406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id: %u, next: %u, prev: %u\n",
3407 peer->id, t->next_hop, t->prev_hop);
3410 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task && fc->queue_n > 0)
3412 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3413 "* %s starting poll timeout\n",
3414 GNUNET_i2s (&my_full_id));
3416 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3420 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3431 * @brief Queue and pass message to core when possible.
3433 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3434 * accounts for it. In case the queue is full, the message is dropped and
3437 * Otherwise, message is treated as internal and allowed to go regardless of
3440 * @param cls Closure (@c type dependant). It will be used by queue_send to
3441 * build the message to be sent if not already prebuilt.
3442 * @param type Type of the message, 0 for a raw message.
3443 * @param size Size of the message.
3444 * @param dst Neighbor to send message to.
3445 * @param t Tunnel this message belongs to.
3448 queue_add (void *cls, uint16_t type, size_t size,
3449 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3451 struct MeshPeerQueue *queue;
3452 struct GNUNET_PeerIdentity id;
3453 struct MeshFlowControl *fc;
3457 priority = GNUNET_NO;
3458 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3462 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3468 if (fc->queue_n >= t->queue_max)
3470 /* If this isn't a retransmission, drop the message */
3471 if (GNUNET_NO == t->reliable ||
3472 (NULL == t->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) ||
3473 (NULL == t->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type))
3475 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3479 "queue full: %u/%u\n",
3480 fc->queue_n, t->queue_max);
3481 return; /* Drop this message */
3483 priority = GNUNET_YES;
3487 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3493 if (GNUNET_YES == priority)
3495 struct GNUNET_MESH_Data *d;
3499 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
3500 d = (struct GNUNET_MESH_Data *) queue->cls;
3502 for (queue = dst->queue_tail; NULL != queue; queue = queue->prev)
3504 if (queue->type != type)
3506 d = (struct GNUNET_MESH_Data *) queue->cls;
3513 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3514 if (NULL == dst->core_transmit)
3516 GNUNET_PEER_resolve (dst->id, &id);
3517 dst->core_transmit =
3518 GNUNET_CORE_notify_transmit_ready (core_handle,
3521 GNUNET_TIME_UNIT_FOREVER_REL,
3527 t->pending_messages++;
3531 /******************************************************************************/
3532 /******************** MESH NETWORK HANDLERS **************************/
3533 /******************************************************************************/
3537 * Core handler for path creation
3539 * @param cls closure
3540 * @param message message
3541 * @param peer peer identity this notification is about
3543 * @return GNUNET_OK to keep the connection open,
3544 * GNUNET_SYSERR to close it (signal serious error)
3547 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3548 const struct GNUNET_MessageHeader *message)
3550 unsigned int own_pos;
3553 MESH_TunnelNumber tid;
3554 struct GNUNET_MESH_CreateTunnel *msg;
3555 struct GNUNET_PeerIdentity *pi;
3556 struct MeshPeerPath *path;
3557 struct MeshPeerInfo *dest_peer_info;
3558 struct MeshPeerInfo *orig_peer_info;
3559 struct MeshTunnel *t;
3561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3562 "Received a path create msg [%s]\n",
3563 GNUNET_i2s (&my_full_id));
3564 size = ntohs (message->size);
3565 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3567 GNUNET_break_op (0);
3571 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3572 if (size % sizeof (struct GNUNET_PeerIdentity))
3574 GNUNET_break_op (0);
3577 size /= sizeof (struct GNUNET_PeerIdentity);
3580 GNUNET_break_op (0);
3583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3584 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3586 tid = ntohl (msg->tid);
3587 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3589 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3590 t = tunnel_get (pi, tid);
3591 if (NULL == t) /* might be a local tunnel */
3595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3596 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3602 t->port = ntohl (msg->port);
3603 opt = ntohl (msg->opt);
3604 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3606 t->nobuffer = GNUNET_YES;
3609 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3611 t->reliable = GNUNET_YES;
3613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3615 tunnel_reset_timeout (t, GNUNET_YES); // FIXME
3617 t->state = MESH_TUNNEL_WAITING;
3619 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3620 if (NULL == dest_peer_info)
3622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3623 " Creating PeerInfo for destination.\n");
3624 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3625 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3626 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3628 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3630 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3631 if (NULL == orig_peer_info)
3633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3634 " Creating PeerInfo for origin.\n");
3635 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3636 orig_peer_info->id = GNUNET_PEER_intern (pi);
3637 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3638 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3641 path = path_new (size);
3643 for (i = 0; i < size; i++)
3645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3646 GNUNET_i2s (&pi[i]));
3647 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3648 if (path->peers[i] == myid)
3651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3652 if (own_pos == 0 && path->peers[own_pos] != myid)
3654 /* create path: self not found in path through self */
3655 GNUNET_break_op (0);
3656 path_destroy (path);
3660 path_add_to_peers (path, GNUNET_NO);
3661 tunnel_use_path (t, path);
3663 peer_info_add_tunnel (dest_peer_info, t);
3665 if (own_pos == size - 1)
3667 struct MeshClient *c;
3669 /* Find target client */
3670 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
3673 /* TODO send reject */
3677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3678 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3680 /* Assign local tid */
3681 while (NULL != tunnel_get_incoming (next_local_tid))
3682 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3683 t->local_tid_dest = next_local_tid++;
3684 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3686 if (GNUNET_YES == t->reliable)
3688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
3689 t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
3691 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
3694 tunnel_add_client (t, c);
3695 send_client_tunnel_create (t);
3700 struct MeshPeerPath *path2;
3702 t->next_hop = path->peers[own_pos + 1];
3703 GNUNET_PEER_change_rc(t->next_hop, 1);
3705 /* It's for somebody else! Retransmit. */
3706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3707 path2 = path_duplicate (path);
3708 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3709 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3710 send_create_path (t);
3718 * Core handler for path ACKs
3720 * @param cls closure
3721 * @param message message
3722 * @param peer peer identity this notification is about
3724 * @return GNUNET_OK to keep the connection open,
3725 * GNUNET_SYSERR to close it (signal serious error)
3728 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3729 const struct GNUNET_MessageHeader *message)
3731 struct GNUNET_MESH_PathACK *msg;
3732 struct MeshPeerInfo *peer_info;
3733 struct MeshPeerPath *p;
3734 struct MeshTunnel *t;
3736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3737 GNUNET_i2s (&my_full_id));
3738 msg = (struct GNUNET_MESH_PathACK *) message;
3739 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3742 /* TODO notify that we don't know the tunnel */
3743 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3745 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3749 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3751 peer_info = peer_get (&msg->peer_id);
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3753 GNUNET_i2s (&msg->peer_id));
3754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3757 /* Add path to peers? */
3761 path_add_to_peers (p, GNUNET_YES);
3767 t->state = MESH_TUNNEL_READY;
3768 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3769 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3771 /* Message for us? */
3772 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3775 if (NULL == t->owner)
3777 GNUNET_break_op (0);
3780 if (NULL != peer_info->dhtget)
3782 GNUNET_DHT_get_stop (peer_info->dhtget);
3783 peer_info->dhtget = NULL;
3785 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_YES);
3786 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_NO);
3790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3791 " not for us, retransmitting...\n");
3792 peer_info = peer_get (&msg->oid);
3793 send_prebuilt_message (message, t->prev_hop, t);
3799 * Core handler for notifications of broken paths
3801 * @param cls closure
3802 * @param message message
3803 * @param peer peer identity this notification is about
3805 * @return GNUNET_OK to keep the connection open,
3806 * GNUNET_SYSERR to close it (signal serious error)
3809 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3810 const struct GNUNET_MessageHeader *message)
3812 struct GNUNET_MESH_PathBroken *msg;
3813 struct MeshTunnel *t;
3815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3816 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3817 msg = (struct GNUNET_MESH_PathBroken *) message;
3818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3819 GNUNET_i2s (&msg->peer1));
3820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3821 GNUNET_i2s (&msg->peer2));
3822 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3825 GNUNET_break_op (0);
3828 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3829 GNUNET_PEER_search (&msg->peer2));
3836 * Core handler for tunnel destruction
3838 * @param cls closure
3839 * @param message message
3840 * @param peer peer identity this notification is about
3842 * @return GNUNET_OK to keep the connection open,
3843 * GNUNET_SYSERR to close it (signal serious error)
3846 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3847 const struct GNUNET_MessageHeader *message)
3849 struct GNUNET_MESH_TunnelDestroy *msg;
3850 struct MeshTunnel *t;
3852 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3854 "Got a TUNNEL DESTROY packet from %s\n",
3856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3857 " for tunnel %s [%u]\n",
3858 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3859 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3862 /* Probably already got the message from another path,
3863 * destroyed the tunnel and retransmitted to children.
3866 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3870 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3873 t->local_tid, t->local_tid_dest);
3875 if (GNUNET_PEER_search (peer) == t->prev_hop)
3877 // TODO check owner's signature
3878 // TODO add owner's signatue to tunnel for retransmission
3879 peer_cancel_queues (t->prev_hop, t);
3880 GNUNET_PEER_change_rc (t->prev_hop, -1);
3883 else if (GNUNET_PEER_search (peer) == t->next_hop)
3885 // TODO check dest's signature
3886 // TODO add dest's signatue to tunnel for retransmission
3887 peer_cancel_queues (t->next_hop, t);
3888 GNUNET_PEER_change_rc (t->next_hop, -1);
3893 GNUNET_break_op (0);
3894 // TODO check both owner AND destination's signature to see which matches
3895 // TODO restransmit in appropriate direction
3898 tunnel_destroy_empty (t);
3900 // TODO: add timeout to destroy the tunnel anyway
3906 * Generic handler for mesh network payload traffic.
3908 * @param peer Peer identity this notification is about.
3909 * @param message Data message.
3910 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
3912 * @return GNUNET_OK to keep the connection open,
3913 * GNUNET_SYSERR to close it (signal serious error)
3916 handle_mesh_data (const struct GNUNET_PeerIdentity *peer,
3917 const struct GNUNET_MessageHeader *message,
3920 struct GNUNET_MESH_Data *msg;
3921 struct MeshFlowControl *fc;
3922 struct MeshTunnelReliability *rel;
3923 struct MeshTunnel *t;
3924 struct MeshClient *c;
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
3931 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)),
3934 size = ntohs (message->size);
3936 sizeof (struct GNUNET_MESH_Data) +
3937 sizeof (struct GNUNET_MessageHeader))
3942 msg = (struct GNUNET_MESH_Data *) message;
3943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
3944 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3946 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3949 /* TODO notify back: we don't know this tunnel */
3950 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3951 GNUNET_break_op (0);
3955 /* Initialize FWD/BCK data */
3956 pid = ntohl (msg->pid);
3957 fc = fwd ? &t->prev_fc : &t->next_fc;
3958 c = fwd ? t->client : t->owner;
3959 rel = fwd ? t->bck_rel : t->fwd_rel;
3960 hop = fwd ? t->next_hop : t->prev_hop;
3961 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
3963 GNUNET_STATISTICS_update (stats, "# unsolicited data", 1, GNUNET_NO);
3964 GNUNET_break_op (0);
3965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3966 "Received PID %u, (prev %u), ACK %u\n",
3967 pid, fc->last_pid_recv, fc->last_ack_sent);
3971 tunnel_reset_timeout (t, fwd);
3974 /* TODO signature verification */
3975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us! sending to client\n");
3976 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
3977 if (GMC_is_pid_bigger (pid, fc->last_pid_recv))
3981 mid = ntohl (msg->mid);
3982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3983 " pid %u (mid %u) not seen yet\n", pid, mid);
3984 fc->last_pid_recv = pid;
3986 if (GNUNET_NO == t->reliable ||
3987 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
3988 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
3990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3991 "!!! RECV %u\n", ntohl (msg->mid));
3992 if (GNUNET_YES == t->reliable)
3994 /* Is this the exact next expected messasge? */
3995 if (mid == rel->mid_recv)
3997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
3999 tunnel_send_client_data (t, msg, fwd);
4000 tunnel_send_client_buffered_data (t, c, rel);
4004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4005 tunnel_add_buffered_data (t, msg, rel);
4008 else /* Tunnel unreliable, send to clients directly */
4010 tunnel_send_client_data (t, msg, fwd);
4015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4016 " MID %u not expected (%u - %u), dropping!\n",
4017 ntohl (msg->mid), rel->mid_recv, rel->mid_recv + 64);
4022 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4024 " Pid %u not expected (%u+), dropping!\n",
4025 pid, fc->last_pid_recv + 1);
4027 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, fwd);
4030 fc->last_pid_recv = pid;
4036 ttl = ntohl (msg->ttl);
4037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4040 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4041 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4042 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, fwd);
4048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4049 send_prebuilt_message (message, hop, t);
4050 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4057 * Core handler for mesh network traffic going from the origin to a peer
4059 * @param cls Closure (unused).
4060 * @param message Message received.
4061 * @param peer Peer who sent the message.
4063 * @return GNUNET_OK to keep the connection open,
4064 * GNUNET_SYSERR to close it (signal serious error)
4067 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4068 const struct GNUNET_MessageHeader *message)
4070 return handle_mesh_data (peer, message, GNUNET_YES);
4074 * Core handler for mesh network traffic towards the owner of a tunnel.
4076 * @param cls Closure (unused).
4077 * @param message Message received.
4078 * @param peer Peer who sent the message.
4080 * @return GNUNET_OK to keep the connection open,
4081 * GNUNET_SYSERR to close it (signal serious error)
4084 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4085 const struct GNUNET_MessageHeader *message)
4087 return handle_mesh_data (peer, message, GNUNET_NO);
4092 * Core handler for mesh network traffic end-to-end ACKs.
4094 * @param cls Closure.
4095 * @param message Message.
4096 * @param peer Peer identity this notification is about.
4098 * @return GNUNET_OK to keep the connection open,
4099 * GNUNET_SYSERR to close it (signal serious error)
4102 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4103 const struct GNUNET_MessageHeader *message)
4105 struct GNUNET_MESH_DataACK *msg;
4106 struct MeshTunnelReliability *rel;
4107 struct MeshReliableMessage *copy;
4108 struct MeshReliableMessage *next;
4109 struct MeshTunnel *t;
4115 type = ntohs (message->type);
4116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
4117 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4118 msg = (struct GNUNET_MESH_DataACK *) message;
4120 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4123 /* TODO notify that we dont know this tunnel (whom)? */
4124 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4127 ack = ntohl (msg->mid);
4128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4130 /* Is this a forward or backward ACK? */
4131 id = GNUNET_PEER_search (peer);
4132 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4135 if (NULL == t->owner)
4137 send_prebuilt_message (message, t->prev_hop, t);
4142 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4145 if (NULL == t->client)
4147 send_prebuilt_message (message, t->next_hop, t);
4154 GNUNET_break_op (0);
4158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %u\n", ack);
4159 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4161 if (GMC_is_pid_bigger (copy->mid, ack))
4163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4164 tunnel_free_sent_reliable (t, msg, rel);
4168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4170 tunnel_free_reliable_message (copy);
4172 /* Once buffers have been free'd, send ACK */
4173 tunnel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4175 /* If some message was free'd, update the retransmission delay*/
4176 if (GNUNET_YES == work)
4178 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4180 GNUNET_SCHEDULER_cancel (rel->retry_task);
4181 if (NULL == rel->head_sent)
4183 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4187 struct GNUNET_TIME_Absolute new_target;
4188 struct GNUNET_TIME_Relative delay;
4190 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4191 MESH_RETRANSMIT_MARGIN);
4192 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4194 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4196 GNUNET_SCHEDULER_add_delayed (delay,
4197 &tunnel_retransmit_message,
4208 * Core handler for mesh network traffic point-to-point acks.
4210 * @param cls closure
4211 * @param message message
4212 * @param peer peer identity this notification is about
4214 * @return GNUNET_OK to keep the connection open,
4215 * GNUNET_SYSERR to close it (signal serious error)
4218 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4219 const struct GNUNET_MessageHeader *message)
4221 struct GNUNET_MESH_ACK *msg;
4222 struct MeshTunnel *t;
4223 struct MeshFlowControl *fc;
4227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4229 msg = (struct GNUNET_MESH_ACK *) message;
4231 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4235 /* TODO notify that we dont know this tunnel (whom)? */
4236 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4239 ack = ntohl (msg->pid);
4240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4242 /* Is this a forward or backward ACK? */
4243 id = GNUNET_PEER_search (peer);
4244 if (t->next_hop == id)
4246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4249 else if (t->prev_hop == id)
4251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4256 GNUNET_break_op (0);
4260 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4261 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4263 GNUNET_SCHEDULER_cancel (fc->poll_task);
4264 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4265 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4267 fc->last_ack_recv = ack;
4268 peer_unlock_queue (id);
4270 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, t->next_hop == id);
4277 * Core handler for mesh network traffic point-to-point ack polls.
4279 * @param cls closure
4280 * @param message message
4281 * @param peer peer identity this notification is about
4283 * @return GNUNET_OK to keep the connection open,
4284 * GNUNET_SYSERR to close it (signal serious error)
4287 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4288 const struct GNUNET_MessageHeader *message)
4290 struct GNUNET_MESH_Poll *msg;
4291 struct MeshTunnel *t;
4292 struct MeshFlowControl *fc;
4297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4300 msg = (struct GNUNET_MESH_Poll *) message;
4302 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4306 /* TODO notify that we dont know this tunnel (whom)? */
4307 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4308 GNUNET_break_op (0);
4312 /* Is this a forward or backward ACK? */
4313 id = GNUNET_PEER_search(peer);
4314 pid = ntohl (msg->pid);
4315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4316 if (t->next_hop == id)
4318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", t->next_fc.last_pid_recv);
4321 old = fc->last_pid_recv;
4322 fc->last_pid_recv = pid;
4323 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, GNUNET_NO);
4325 else if (t->prev_hop == id)
4327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", t->prev_fc.last_pid_recv);
4330 old = fc->last_pid_recv;
4331 fc->last_pid_recv = pid;
4332 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, GNUNET_YES);
4337 if (GNUNET_YES == t->reliable)
4338 fc->last_pid_recv = old;
4345 * Core handler for mesh keepalives.
4347 * @param cls closure
4348 * @param message message
4349 * @param peer peer identity this notification is about
4350 * @return GNUNET_OK to keep the connection open,
4351 * GNUNET_SYSERR to close it (signal serious error)
4353 * TODO: Check who we got this from, to validate route.
4356 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4357 const struct GNUNET_MessageHeader *message)
4359 struct GNUNET_MESH_TunnelKeepAlive *msg;
4360 struct MeshTunnel *t;
4362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4365 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4366 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4370 /* TODO notify that we dont know that tunnel */
4371 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4376 tunnel_reset_timeout (t, GNUNET_YES); // FIXME
4377 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4380 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4381 send_prebuilt_message (message, t->next_hop, t);
4388 * Functions to handle messages from core
4390 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4391 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4392 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4393 sizeof (struct GNUNET_MESH_PathBroken)},
4394 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4395 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4396 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4397 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4398 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4399 sizeof (struct GNUNET_MESH_DataACK)},
4400 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4401 sizeof (struct GNUNET_MESH_DataACK)},
4402 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4403 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4404 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4405 sizeof (struct GNUNET_MESH_ACK)},
4406 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4407 sizeof (struct GNUNET_MESH_Poll)},
4408 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4409 sizeof (struct GNUNET_MESH_PathACK)},
4415 /******************************************************************************/
4416 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4417 /******************************************************************************/
4422 * notify_client_connection_failure: notify a client that the connection to the
4423 * requested remote peer is not possible (for instance, no route found)
4424 * Function called when the socket is ready to queue more data. "buf" will be
4425 * NULL and "size" zero if the socket was closed for writing in the meantime.
4427 * @param cls closure
4428 * @param size number of bytes available in buf
4429 * @param buf where the callee should write the message
4430 * @return number of bytes written to buf
4433 notify_client_connection_failure (void *cls, size_t size, void *buf)
4436 struct MeshPeerInfo *peer_info;
4437 struct GNUNET_MESH_PeerControl *msg;
4438 struct GNUNET_PeerIdentity id;
4440 if (0 == size && NULL == buf)
4442 // TODO retry? cancel?
4446 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4447 peer_info = (struct MeshPeerInfo *) cls;
4448 msg = (struct GNUNET_MESH_PeerControl *) buf;
4449 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4450 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4451 // msg->tunnel_id = htonl(peer_info->t->tid);
4452 GNUNET_PEER_resolve (peer_info->id, &id);
4453 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4461 * Send keepalive packets for a tunnel.
4463 * @param cls Closure (tunnel for which to send the keepalive).
4464 * @param tc Notification context.
4466 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4469 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4471 struct MeshTunnel *t = cls;
4472 struct GNUNET_MESH_TunnelKeepAlive *msg;
4473 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4476 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4477 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4478 NULL == t->owner || 0 == t->local_tid)
4483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4484 "sending keepalive for tunnel %d\n", t->id.tid);
4486 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4487 msg->header.size = htons (size);
4488 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4489 msg->oid = my_full_id;
4490 msg->tid = htonl (t->id.tid);
4491 send_prebuilt_message (&msg->header, t->next_hop, t);
4493 t->maintenance_task =
4494 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4499 * Function to process paths received for a new peer addition. The recorded
4500 * paths form the initial tunnel, which can be optimized later.
4501 * Called on each result obtained for the DHT search.
4503 * @param cls closure
4504 * @param exp when will this value expire
4505 * @param key key of the result
4506 * @param get_path path of the get request
4507 * @param get_path_length lenght of get_path
4508 * @param put_path path of the put request
4509 * @param put_path_length length of the put_path
4510 * @param type type of the result
4511 * @param size number of bytes in data
4512 * @param data pointer to the result data
4515 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4516 const struct GNUNET_HashCode * key,
4517 const struct GNUNET_PeerIdentity *get_path,
4518 unsigned int get_path_length,
4519 const struct GNUNET_PeerIdentity *put_path,
4520 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4521 size_t size, const void *data)
4523 struct MeshPeerInfo *peer = cls;
4524 struct MeshPeerPath *p;
4525 struct GNUNET_PeerIdentity pi;
4528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4529 GNUNET_PEER_resolve (peer->id, &pi);
4530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4532 p = path_build_from_dht (get_path, get_path_length,
4533 put_path, put_path_length);
4534 path_add_to_peers (p, GNUNET_NO);
4536 for (i = 0; i < peer->ntunnels; i++)
4538 struct GNUNET_PeerIdentity id;
4540 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4542 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4543 peer->tunnels[i]->local_tid,
4544 peer->tunnels[i]->local_tid_dest);
4545 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4548 peer_connect (peer, peer->tunnels[i]);
4556 /******************************************************************************/
4557 /********************* MESH LOCAL HANDLES **************************/
4558 /******************************************************************************/
4562 * Handler for client connection.
4564 * @param cls Closure (unused).
4565 * @param client Client handler.
4568 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
4570 struct MeshClient *c;
4574 c = GNUNET_malloc (sizeof (struct MeshClient));
4576 GNUNET_SERVER_client_keep (client);
4577 GNUNET_SERVER_client_set_user_context (client, c);
4578 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4583 * Handler for client disconnection
4585 * @param cls closure
4586 * @param client identification of the client; NULL
4587 * for the last call when the server is destroyed
4590 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4592 struct MeshClient *c;
4593 struct MeshClient *next;
4595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4602 c = client_get (client);
4605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4607 GNUNET_SERVER_client_drop (c->handle);
4608 c->shutting_down = GNUNET_YES;
4609 if (NULL != c->own_tunnels)
4611 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4612 &tunnel_destroy_iterator, c);
4613 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4616 if (NULL != c->incoming_tunnels)
4618 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4619 &tunnel_destroy_iterator, c);
4620 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4623 if (NULL != c->ports)
4624 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4626 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4629 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4634 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
4636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4642 * Handler for new clients
4644 * @param cls closure
4645 * @param client identification of the client
4646 * @param message the actual message, which includes messages the client wants
4649 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4650 const struct GNUNET_MessageHeader *message)
4652 struct GNUNET_MESH_ClientConnect *cc_msg;
4653 struct MeshClient *c;
4658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4660 /* Check data sanity */
4661 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4662 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4663 if (0 != (size % sizeof (uint32_t)))
4666 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4669 size /= sizeof (uint32_t);
4671 /* Initialize new client structure */
4672 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
4673 c->id = next_client_id++; /* overflow not important: just for debug */
4674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4680 p = (uint32_t *) &cc_msg[1];
4681 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4682 for (i = 0; i < size; i++)
4685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4687 /* store in client's hashmap */
4688 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4689 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4690 /* store in global hashmap */
4691 /* FIXME only allow one client to have the port open,
4692 * have a backup hashmap with waiting clients */
4693 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4694 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4698 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4699 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4700 GNUNET_SERVER_notification_context_add (nc, client);
4701 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4703 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4709 * Handler for requests of new tunnels
4711 * @param cls Closure.
4712 * @param client Identification of the client.
4713 * @param message The actual message.
4716 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4717 const struct GNUNET_MessageHeader *message)
4719 struct GNUNET_MESH_TunnelMessage *t_msg;
4720 struct MeshPeerInfo *peer_info;
4721 struct MeshTunnel *t;
4722 struct MeshClient *c;
4723 MESH_TunnelNumber tid;
4725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4727 /* Sanity check for client registration */
4728 if (NULL == (c = client_get (client)))
4731 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4736 /* Message size sanity check */
4737 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4744 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4746 GNUNET_i2s (&t_msg->peer));
4747 /* Sanity check for tunnel numbering */
4748 tid = ntohl (t_msg->tunnel_id);
4749 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4752 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4755 /* Sanity check for duplicate tunnel IDs */
4756 if (NULL != tunnel_get_by_local_id (c, tid))
4759 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4764 while (NULL != tunnel_get_by_pi (myid, next_tid))
4765 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4766 t = tunnel_new (myid, next_tid, c, tid);
4767 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4771 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4774 t->port = ntohl (t_msg->port);
4775 tunnel_set_options (t, ntohl (t_msg->opt));
4776 if (GNUNET_YES == t->reliable)
4778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4779 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
4781 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
4784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4785 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4787 peer_info = peer_get (&t_msg->peer);
4788 peer_info_add_tunnel (peer_info, t);
4789 peer_connect (peer_info, t);
4790 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4796 * Handler for requests of deleting tunnels
4798 * @param cls closure
4799 * @param client identification of the client
4800 * @param message the actual message
4803 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4804 const struct GNUNET_MessageHeader *message)
4806 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4807 struct MeshClient *c;
4808 struct MeshTunnel *t;
4809 MESH_TunnelNumber tid;
4811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4812 "Got a DESTROY TUNNEL from client!\n");
4814 /* Sanity check for client registration */
4815 if (NULL == (c = client_get (client)))
4818 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4823 /* Message sanity check */
4824 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4827 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4831 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4833 /* Retrieve tunnel */
4834 tid = ntohl (tunnel_msg->tunnel_id);
4835 t = tunnel_get_by_local_id(c, tid);
4838 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4840 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4844 /* Cleanup after the tunnel */
4845 client_delete_tunnel (c, t);
4852 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4856 /* The tunnel will be destroyed when the last message is transmitted. */
4857 tunnel_destroy_empty (t);
4859 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4865 * Handler for client traffic
4867 * @param cls closure
4868 * @param client identification of the client
4869 * @param message the actual message
4872 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
4873 const struct GNUNET_MessageHeader *message)
4875 struct GNUNET_MESH_LocalData *data_msg;
4876 struct MeshClient *c;
4877 struct MeshTunnel *t;
4878 struct MeshFlowControl *fc;
4879 MESH_TunnelNumber tid;
4882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4883 "Got data from a client!\n");
4885 /* Sanity check for client registration */
4886 if (NULL == (c = client_get (client)))
4889 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4894 data_msg = (struct GNUNET_MESH_LocalData *) message;
4896 /* Sanity check for message size */
4897 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
4898 if (size < sizeof (struct GNUNET_MessageHeader))
4901 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4905 /* Tunnel exists? */
4906 tid = ntohl (data_msg->tid);
4907 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
4910 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4913 t = tunnel_get_by_local_id (c, tid);
4917 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4921 /* Is the client in the tunnel? */
4922 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4924 t->owner->handle == client)
4926 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4928 t->client->handle == client) ) )
4931 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4935 /* Ok, everything is correct, send the message
4936 * (pretend we got it from a mesh peer)
4939 struct GNUNET_MESH_Data *payload;
4940 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
4942 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
4943 if (GNUNET_YES == t->reliable)
4945 struct MeshTunnelReliability *rel;
4946 struct MeshReliableMessage *copy;
4948 rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
4949 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
4950 + sizeof(struct GNUNET_MESH_Data)
4952 copy->mid = rel->mid_sent++;
4953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid);
4954 copy->timestamp = GNUNET_TIME_absolute_get ();
4957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
4958 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
4959 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
4962 GNUNET_TIME_relative_multiply (rel->expected_delay,
4963 MESH_RETRANSMIT_MARGIN);
4965 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
4966 &tunnel_retransmit_message,
4969 payload = (struct GNUNET_MESH_Data *) ©[1];
4970 payload->mid = htonl (copy->mid);
4974 payload = (struct GNUNET_MESH_Data *) cbuf;
4975 payload->mid = htonl (fc->last_pid_recv + 1);
4977 memcpy (&payload[1], &data_msg[1], size);
4978 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
4979 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
4980 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
4981 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4982 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
4983 payload->tid = htonl (t->id.tid);
4984 payload->ttl = htonl (default_ttl);
4985 payload->pid = htonl (fc->last_pid_recv + 1);
4986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4987 " calling generic handler...\n");
4988 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4989 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
4991 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
4993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4994 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5001 * Handler for client's ACKs for payload traffic.
5003 * @param cls Closure (unused).
5004 * @param client Identification of the client.
5005 * @param message The actual message.
5008 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5009 const struct GNUNET_MessageHeader *message)
5011 struct GNUNET_MESH_LocalAck *msg;
5012 struct MeshTunnel *t;
5013 struct MeshClient *c;
5014 MESH_TunnelNumber tid;
5016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5017 /* Sanity check for client registration */
5018 if (NULL == (c = client_get (client)))
5021 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5026 msg = (struct GNUNET_MESH_LocalAck *) message;
5028 /* Tunnel exists? */
5029 tid = ntohl (msg->tunnel_id);
5030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5031 t = tunnel_get_by_local_id (c, tid);
5035 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5036 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5037 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5041 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5042 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5044 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5045 t->prev_fc.last_ack_recv++;
5046 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_NO);
5050 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5051 t->next_fc.last_ack_recv++;
5052 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_YES);
5055 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5063 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5065 * @param cls Closure (client handle).
5066 * @param key Key (hashed tunnel ID, unused).
5067 * @param value Tunnel info.
5069 * @return GNUNET_YES, to keep iterating.
5072 monitor_all_tunnels_iterator (void *cls,
5073 const struct GNUNET_HashCode * key,
5076 struct GNUNET_SERVER_Client *client = cls;
5077 struct MeshTunnel *t = value;
5078 struct GNUNET_MESH_LocalMonitor *msg;
5080 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5081 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5082 msg->tunnel_id = htonl (t->id.tid);
5083 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5084 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5085 GNUNET_PEER_resolve (t->dest, &msg->destination);
5087 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5088 "* sending info about tunnel %s [%u]\n",
5089 GNUNET_i2s (&msg->owner), t->id.tid);
5091 GNUNET_SERVER_notification_context_unicast (nc, client,
5092 &msg->header, GNUNET_NO);
5098 * Handler for client's MONITOR request.
5100 * @param cls Closure (unused).
5101 * @param client Identification of the client.
5102 * @param message The actual message.
5105 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5106 const struct GNUNET_MessageHeader *message)
5108 struct MeshClient *c;
5110 /* Sanity check for client registration */
5111 if (NULL == (c = client_get (client)))
5114 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5118 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5119 "Received get tunnels request from client %u\n",
5121 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5122 monitor_all_tunnels_iterator,
5124 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5125 "Get tunnels request from client %u completed\n",
5127 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5132 * Handler for client's MONITOR_TUNNEL request.
5134 * @param cls Closure (unused).
5135 * @param client Identification of the client.
5136 * @param message The actual message.
5139 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5140 const struct GNUNET_MessageHeader *message)
5142 const struct GNUNET_MESH_LocalMonitor *msg;
5143 struct GNUNET_MESH_LocalMonitor *resp;
5144 struct MeshClient *c;
5145 struct MeshTunnel *t;
5147 /* Sanity check for client registration */
5148 if (NULL == (c = client_get (client)))
5151 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5155 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5156 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5157 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5160 ntohl (msg->tunnel_id));
5161 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5164 /* We don't know the tunnel FIXME */
5165 struct GNUNET_MESH_LocalMonitor warn;
5168 GNUNET_SERVER_notification_context_unicast (nc, client,
5171 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5175 /* Initialize context */
5176 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5178 GNUNET_PEER_resolve (t->dest, &resp->destination);
5179 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5180 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5181 &resp->header, GNUNET_NO);
5184 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5185 "Monitor tunnel request from client %u completed\n",
5187 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5192 * Functions to handle messages from clients
5194 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5195 {&handle_local_new_client, NULL,
5196 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5197 {&handle_local_tunnel_create, NULL,
5198 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5199 sizeof (struct GNUNET_MESH_TunnelMessage)},
5200 {&handle_local_tunnel_destroy, NULL,
5201 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5202 sizeof (struct GNUNET_MESH_TunnelMessage)},
5203 {&handle_local_data, NULL,
5204 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5205 {&handle_local_ack, NULL,
5206 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5207 sizeof (struct GNUNET_MESH_LocalAck)},
5208 {&handle_local_get_tunnels, NULL,
5209 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5210 sizeof (struct GNUNET_MessageHeader)},
5211 {&handle_local_show_tunnel, NULL,
5212 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5213 sizeof (struct GNUNET_MESH_LocalMonitor)},
5219 * Method called whenever a given peer connects.
5221 * @param cls closure
5222 * @param peer peer identity this notification is about
5225 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5227 struct MeshPeerInfo *peer_info;
5228 struct MeshPeerPath *path;
5230 DEBUG_CONN ("Peer connected\n");
5231 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5232 peer_info = peer_get (peer);
5233 if (myid == peer_info->id)
5235 DEBUG_CONN (" (self)\n");
5236 path = path_new (1);
5240 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5241 path = path_new (2);
5242 path->peers[1] = peer_info->id;
5243 GNUNET_PEER_change_rc (peer_info->id, 1);
5244 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5246 path->peers[0] = myid;
5247 GNUNET_PEER_change_rc (myid, 1);
5248 peer_info_add_path (peer_info, path, GNUNET_YES);
5254 * Method called whenever a peer disconnects.
5256 * @param cls closure
5257 * @param peer peer identity this notification is about
5260 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5262 struct MeshPeerInfo *pi;
5263 struct MeshPeerQueue *q;
5264 struct MeshPeerQueue *n;
5266 DEBUG_CONN ("Peer disconnected\n");
5267 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5277 /* TODO try to reroute this traffic instead */
5278 queue_destroy(q, GNUNET_YES);
5281 if (NULL != pi->core_transmit)
5283 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5284 pi->core_transmit = NULL;
5286 peer_remove_path (pi, pi->id, myid);
5289 DEBUG_CONN (" (self)\n");
5291 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5297 * Install server (service) handlers and start listening to clients.
5302 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5303 GNUNET_SERVER_connect_notify (server_handle,
5304 &handle_local_client_connect, NULL);
5305 GNUNET_SERVER_disconnect_notify (server_handle,
5306 &handle_local_client_disconnect, NULL);
5307 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5309 clients_head = NULL;
5310 clients_tail = NULL;
5312 GNUNET_SERVER_resume (server_handle);
5317 * To be called on core init/fail.
5319 * @param cls Closure (config)
5320 * @param server handle to the server for this service
5321 * @param identity the public identity of this peer
5324 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5325 const struct GNUNET_PeerIdentity *identity)
5327 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5331 GNUNET_break (core_handle == server);
5332 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5336 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5338 GNUNET_i2s (identity));
5339 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5341 GNUNET_i2s (&my_full_id));
5342 GNUNET_CORE_disconnect (core_handle);
5343 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5344 NULL, /* Closure passed to MESH functions */
5345 &core_init, /* Call core_init once connected */
5346 &core_connect, /* Handle connects */
5347 &core_disconnect, /* remove peers on disconnects */
5348 NULL, /* Don't notify about all incoming messages */
5349 GNUNET_NO, /* For header only in notification */
5350 NULL, /* Don't notify about all outbound messages */
5351 GNUNET_NO, /* For header-only out notification */
5352 core_handlers); /* Register these handlers */
5361 /******************************************************************************/
5362 /************************ MAIN FUNCTIONS ****************************/
5363 /******************************************************************************/
5366 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5368 * @param cls closure
5369 * @param key current key code
5370 * @param value value in the hash map
5371 * @return GNUNET_YES if we should continue to iterate,
5375 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5377 struct MeshTunnel *t = value;
5384 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5386 * @param cls closure
5387 * @param key current key code
5388 * @param value value in the hash map
5389 * @return GNUNET_YES if we should continue to iterate,
5393 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5395 struct MeshPeerInfo *p = value;
5396 struct MeshPeerQueue *q;
5397 struct MeshPeerQueue *n;
5405 queue_destroy(q, GNUNET_YES);
5409 peer_info_destroy (p);
5415 * Task run during shutdown.
5421 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5425 if (core_handle != NULL)
5427 GNUNET_CORE_disconnect (core_handle);
5430 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5431 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5432 if (dht_handle != NULL)
5434 GNUNET_DHT_disconnect (dht_handle);
5439 GNUNET_SERVER_notification_context_destroy (nc);
5442 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5444 GNUNET_SCHEDULER_cancel (announce_id_task);
5445 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5452 * Process mesh requests.
5454 * @param cls closure
5455 * @param server the initialized server
5456 * @param c configuration to use
5459 run (void *cls, struct GNUNET_SERVER_Handle *server,
5460 const struct GNUNET_CONFIGURATION_Handle *c)
5463 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5466 server_handle = server;
5467 GNUNET_SERVER_suspend (server_handle);
5470 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5473 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5475 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5476 "mesh", "peer/privatekey");
5477 GNUNET_SCHEDULER_shutdown ();
5482 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5483 &refresh_path_time))
5485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5487 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5488 "mesh", "refresh path time");
5489 GNUNET_SCHEDULER_shutdown ();
5494 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5497 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5499 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5500 "mesh", "id announce time");
5501 GNUNET_SCHEDULER_shutdown ();
5506 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5509 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5511 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5512 "mesh", "connect timeout");
5513 GNUNET_SCHEDULER_shutdown ();
5518 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5521 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5523 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5524 "mesh", "max msgs queue");
5525 GNUNET_SCHEDULER_shutdown ();
5530 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5533 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5535 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5536 "mesh", "max tunnels");
5537 GNUNET_SCHEDULER_shutdown ();
5542 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5545 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5547 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5548 "mesh", "default ttl", 64);
5553 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5556 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5557 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5558 "mesh", "max peers", 1000);
5563 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5570 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5571 "Mesh is running with drop mode enabled. "
5572 "This is NOT a good idea! "
5573 "Remove the DROP_PERCENT option from your configuration.\n");
5577 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5578 &dht_replication_level))
5580 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5582 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5583 "mesh", "dht replication level", 3);
5584 dht_replication_level = 3;
5587 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5588 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5589 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5590 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5592 dht_handle = GNUNET_DHT_connect (c, 64);
5593 if (NULL == dht_handle)
5597 stats = GNUNET_STATISTICS_create ("mesh", c);
5599 /* Scheduled the task to clean up when shutdown is called */
5600 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5602 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5603 GNUNET_free (keyfile);
5604 GNUNET_assert (NULL != pk);
5605 my_private_key = pk;
5606 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5607 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5608 &my_full_id.hashPubKey);
5609 myid = GNUNET_PEER_intern (&my_full_id);
5610 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5611 "Mesh for peer [%s] starting\n",
5612 GNUNET_i2s(&my_full_id));
5614 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5615 NULL, /* Closure passed to MESH functions */
5616 &core_init, /* Call core_init once connected */
5617 &core_connect, /* Handle connects */
5618 &core_disconnect, /* remove peers on disconnects */
5619 NULL, /* Don't notify about all incoming messages */
5620 GNUNET_NO, /* For header only in notification */
5621 NULL, /* Don't notify about all outbound messages */
5622 GNUNET_NO, /* For header-only out notification */
5623 core_handlers); /* Register these handlers */
5624 if (NULL == core_handle)
5627 GNUNET_SCHEDULER_shutdown ();
5631 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5632 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5638 * The main function for the mesh service.
5640 * @param argc number of arguments from the command line
5641 * @param argv command line arguments
5642 * @return 0 ok, 1 on error
5645 main (int argc, char *const *argv)
5650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5651 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5653 ret = (GNUNET_OK == r) ? 0 : 1;
5654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");