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 t Tunnel that is destroyed.
1049 * @param fwd Forward notification (owner->dest)?
1052 send_client_tunnel_destroy (struct MeshTunnel *t, int fwd)
1054 struct GNUNET_MESH_TunnelMessage msg;
1055 struct MeshClient *c;
1057 c = fwd ? t->client : t->owner;
1063 msg.header.size = htons (sizeof (msg));
1064 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1065 msg.tunnel_id = htonl (fwd ? t->local_tid_dest : t->local_tid);
1066 msg.port = htonl (0);
1067 memset (&msg.peer, 0, sizeof (msg.peer));
1068 msg.opt = htonl (0);
1069 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1070 &msg.header, GNUNET_NO);
1075 * Iterator over all the peers to remove the oldest not-used entry.
1077 * @param cls Closure (unsued).
1078 * @param key ID of the peer.
1079 * @param value Peer_Info of the peer.
1084 peer_info_timeout (void *cls,
1085 const struct GNUNET_HashCode *key,
1092 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1093 * and insert it in the appropriate structures if the peer is not known yet.
1095 * @param peer Full identity of the peer.
1097 * @return Existing or newly created peer info.
1099 static struct MeshPeerInfo *
1100 peer_get (const struct GNUNET_PeerIdentity *peer)
1102 struct MeshPeerInfo *peer_info;
1104 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1105 if (NULL == peer_info)
1108 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1109 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1111 GNUNET_CONTAINER_multihashmap_iterate (peers,
1115 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1116 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1117 peer_info->id = GNUNET_PEER_intern (peer);
1119 peer_info->last_contact = GNUNET_TIME_absolute_get();
1126 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1127 * and insert it in the appropriate structures if the peer is not known yet.
1129 * @param peer Short identity of the peer.
1131 * @return Existing or newly created peer info.
1133 static struct MeshPeerInfo *
1134 peer_get_short (const GNUNET_PEER_Id peer)
1136 struct GNUNET_PeerIdentity id;
1138 GNUNET_PEER_resolve (peer, &id);
1139 return peer_get (&id);
1144 * Select which PID to POLL for, to compensate for lost messages.
1146 * @param pi Peer we want to poll.
1147 * @param t Tunnel about which we want to poll.
1149 * @return PID to use, either last sent or first_in_queue - 1
1152 peer_get_first_payload_pid (struct MeshPeerInfo *pi, struct MeshTunnel *t)
1154 struct MeshPeerQueue *q;
1157 type = pi->id == t->next_hop ? GNUNET_MESSAGE_TYPE_MESH_UNICAST :
1158 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN;
1160 for (q = pi->queue_head; NULL != q; q = q->next)
1162 if (q->type == type && q->tunnel == t)
1164 struct GNUNET_MESH_Data *msg = q->cls;
1166 /* Pretend that the last one sent was the previous to this */
1167 return ntohl (msg->pid) - 1;
1171 /* No data in queue, use last sent */
1173 struct MeshFlowControl *fc;
1175 fc = pi->id == t->next_hop ? &t->next_fc : &t->prev_fc;
1176 return fc->last_pid_sent;
1182 * Choose the best path towards a peer considering the tunnel properties.
1184 * @param peer The destination peer.
1185 * @param t The tunnel the path is for.
1187 * @return Best current known path towards the peer, if any.
1189 static struct MeshPeerPath *
1190 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1192 struct MeshPeerPath *best_p;
1193 struct MeshPeerPath *p;
1194 unsigned int best_cost;
1197 best_p = p = peer->path_head;
1198 best_cost = cost = p->length;
1201 if ((cost = p->length) < best_cost)
1212 * Core callback to write a pre-constructed data packet to core buffer
1214 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1215 * @param size Number of bytes available in buf.
1216 * @param buf Where the to write the message.
1218 * @return number of bytes written to buf
1221 send_core_data_raw (void *cls, size_t size, void *buf)
1223 struct GNUNET_MessageHeader *msg = cls;
1226 GNUNET_assert (NULL != msg);
1227 total_size = ntohs (msg->size);
1229 if (total_size > size)
1234 memcpy (buf, msg, total_size);
1241 * Sends an already built message to a peer, properly registrating
1242 * all used resources.
1244 * @param message Message to send. Function makes a copy of it.
1245 * @param peer Short ID of the neighbor whom to send the message.
1246 * @param t Tunnel on which this message is transmitted.
1249 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1250 GNUNET_PEER_Id peer,
1251 struct MeshTunnel *t)
1253 struct GNUNET_PeerIdentity id;
1254 struct MeshPeerInfo *neighbor;
1255 struct MeshPeerPath *p;
1260 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1265 size = ntohs (message->size);
1266 data = GNUNET_malloc (size);
1267 memcpy (data, message, size);
1268 type = ntohs(message->type);
1269 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1270 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1272 struct GNUNET_MESH_Data *u;
1274 u = (struct GNUNET_MESH_Data *) data;
1275 u->ttl = htonl (ntohl (u->ttl) - 1);
1277 GNUNET_PEER_resolve (peer, &id);
1278 neighbor = peer_get (&id);
1279 for (p = neighbor->path_head; NULL != p; p = p->next)
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 " %s IS NOT DIRECTLY CONNECTED\n",
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1295 for (p = neighbor->path_head; NULL != p; p = p->next)
1297 struct GNUNET_PeerIdentity debug_id;
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1301 " path with %u hops through:\n",
1303 for (i = 0; i < p->length; i++)
1305 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1308 i, GNUNET_i2s(&debug_id));
1312 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1314 " no direct connection to %s\n",
1319 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1330 * Sends a CREATE PATH message for a path to a peer, properly registrating
1331 * all used resources.
1333 * @param t Tunnel for which the path is created.
1336 send_create_path (struct MeshTunnel *t)
1338 struct MeshPeerInfo *neighbor;
1340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1341 neighbor = peer_get_short (t->next_hop);
1343 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1344 sizeof (struct GNUNET_MESH_CreateTunnel) +
1345 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1348 t->state = MESH_TUNNEL_WAITING;
1353 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1355 * @param t Tunnel which to confirm.
1358 send_path_ack (struct MeshTunnel *t)
1360 struct MeshPeerInfo *peer;
1362 peer = peer_get_short (t->prev_hop);
1365 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1366 sizeof (struct GNUNET_MESH_PathACK),
1373 * Try to establish a new connection to this peer in the given tunnel.
1374 * If the peer doesn't have any path to it yet, try to get one.
1375 * If the peer already has some path, send a CREATE PATH towards it.
1377 * @param peer PeerInfo of the peer.
1378 * @param t Tunnel for which to create the path, if possible.
1381 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1383 struct MeshPeerPath *p;
1385 if (NULL != peer->path_head)
1387 p = peer_get_best_path (peer, t);
1388 tunnel_use_path (t, p);
1389 send_create_path (t);
1391 else if (NULL == peer->dhtget)
1393 struct GNUNET_PeerIdentity id;
1395 GNUNET_PEER_resolve (peer->id, &id);
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1398 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1399 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1400 &id.hashPubKey, /* key to search */
1401 dht_replication_level, /* replication level */
1402 GNUNET_DHT_RO_RECORD_ROUTE |
1403 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1405 0, /* xquery bits */
1406 &dht_get_id_handler, peer);
1407 t->state = MESH_TUNNEL_SEARCHING;
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412 "There is no path but the DHT GET is already started.\n");
1418 * Destroy the peer_info and free any allocated resources linked to it
1420 * @param pi The peer_info to destroy.
1422 * @return GNUNET_OK on success
1425 peer_info_destroy (struct MeshPeerInfo *pi)
1427 struct GNUNET_PeerIdentity id;
1428 struct MeshPeerPath *p;
1429 struct MeshPeerPath *nextp;
1432 GNUNET_PEER_resolve (pi->id, &id);
1433 GNUNET_PEER_change_rc (pi->id, -1);
1436 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1440 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1442 if (NULL != pi->dhtget)
1444 GNUNET_DHT_get_stop (pi->dhtget);
1450 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1454 for (i = 0; i < pi->ntunnels; i++)
1455 tunnel_destroy_empty (pi->tunnels[i]);
1456 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1463 * Remove all paths that rely on a direct connection between p1 and p2
1464 * from the peer itself and notify all tunnels about it.
1466 * @param peer PeerInfo of affected peer.
1467 * @param p1 GNUNET_PEER_Id of one peer.
1468 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1471 * TODO: optimize (see below)
1474 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1477 struct MeshPeerPath *p;
1478 struct MeshPeerPath *next;
1479 struct MeshPeerInfo *peer_d;
1481 unsigned int destroyed;
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1486 for (p = peer->path_head; NULL != p; p = next)
1489 for (i = 0; i < (p->length - 1); i++)
1491 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1492 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1494 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1504 for (i = 0; i < peer->ntunnels; i++)
1506 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1510 peer_d = peer_get_short (d);
1511 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1512 tunnel_use_path (peer->tunnels[i], next);
1513 peer_connect (peer_d, peer->tunnels[i]);
1515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1520 * Add the path to the peer and update the path used to reach it in case this
1523 * @param peer_info Destination peer to add the path to.
1524 * @param path New path to add. Last peer must be the peer in arg 1.
1525 * Path will be either used of freed if already known.
1526 * @param trusted Do we trust that this path is real?
1529 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1532 struct MeshPeerPath *aux;
1536 if ((NULL == peer_info) || (NULL == path))
1539 path_destroy (path);
1542 if (path->peers[path->length - 1] != peer_info->id)
1545 path_destroy (path);
1548 if (2 >= path->length && GNUNET_NO == trusted)
1550 /* Only allow CORE to tell us about direct paths */
1551 path_destroy (path);
1554 for (l = 1; l < path->length; l++)
1556 if (path->peers[l] == myid)
1558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1559 for (l2 = 0; l2 < path->length - l; l2++)
1561 path->peers[l2] = path->peers[l + l2];
1566 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1571 struct GNUNET_PeerIdentity id;
1573 GNUNET_PEER_resolve (peer_info->id, &id);
1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1575 path->length, GNUNET_i2s (&id));
1578 l = path_get_length (path);
1581 path_destroy (path);
1585 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1586 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1588 l2 = path_get_length (aux);
1591 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1592 peer_info->path_tail, aux, path);
1597 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1599 path_destroy (path);
1604 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1611 * Add the path to the origin peer and update the path used to reach it in case
1612 * this is the shortest.
1613 * The path is given in peer_info -> destination, therefore we turn the path
1614 * upside down first.
1616 * @param peer_info Peer to add the path to, being the origin of the path.
1617 * @param path New path to add after being inversed.
1618 * Path will be either used or freed.
1619 * @param trusted Do we trust that this path is real?
1622 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1623 struct MeshPeerPath *path, int trusted)
1626 peer_info_add_path (peer_info, path, trusted);
1631 * Add a tunnel to the list of tunnels a peer participates in.
1632 * Update the tunnel's destination.
1634 * @param p Peer to add to.
1635 * @param t Tunnel to add.
1638 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1642 GNUNET_break (t->dest == p->id);
1646 GNUNET_PEER_change_rc (t->dest, 1);
1647 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1652 * Remove a tunnel from the list of tunnels a peer participates in.
1653 * Free the tunnel's destination.
1655 * @param p Peer to clean.
1656 * @param t Tunnel to remove.
1659 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1663 if (t->dest == p->id)
1665 GNUNET_PEER_change_rc (t->dest, -1);
1668 for (i = 0; i < p->ntunnels; i++)
1670 if (p->tunnels[i] == t)
1672 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1673 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1681 * Function called if the connection to the peer has been stalled for a while,
1682 * possibly due to a missed ACK. Poll the peer about its ACK status.
1684 * @param cls Closure (poll ctx).
1685 * @param tc TaskContext.
1688 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1690 struct MeshFlowControl *fc = cls;
1691 struct GNUNET_MESH_Poll msg;
1692 struct MeshTunnel *t = fc->t;
1693 GNUNET_PEER_Id peer;
1695 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1696 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
1703 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1705 if (fc == &t->prev_fc)
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
1710 else if (fc == &t->next_fc)
1712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
1720 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1721 msg.header.size = htons (sizeof (msg));
1722 msg.tid = htonl (t->id.tid);
1723 msg.pid = htonl (peer_get_first_payload_pid (peer_get_short (peer), t));
1724 send_prebuilt_message (&msg.header, peer, t);
1725 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1726 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1732 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1733 * to obtain a local peer -> destination path and interning the peer ids.
1735 * @return Newly allocated and created path
1737 static struct MeshPeerPath *
1738 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1739 unsigned int get_path_length,
1740 const struct GNUNET_PeerIdentity *put_path,
1741 unsigned int put_path_length)
1743 struct MeshPeerPath *p;
1749 GNUNET_PEER_change_rc (myid, 1);
1750 i = get_path_length;
1751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1752 for (i--; i >= 0; i--)
1754 id = GNUNET_PEER_intern (&get_path[i]);
1755 if (p->length > 0 && id == p->peers[p->length - 1])
1757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1758 GNUNET_PEER_change_rc (id, -1);
1762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1763 GNUNET_i2s (&get_path[i]));
1765 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1766 p->peers[p->length - 1] = id;
1769 i = put_path_length;
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1771 for (i--; i >= 0; i--)
1773 id = GNUNET_PEER_intern (&put_path[i]);
1776 /* PUT path went through us, so discard the path up until now and start
1777 * from here to get a much shorter (and loop-free) path.
1782 if (p->length > 0 && id == p->peers[p->length - 1])
1784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1785 GNUNET_PEER_change_rc (id, -1);
1789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1790 GNUNET_i2s (&put_path[i]));
1792 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1793 p->peers[p->length - 1] = id;
1797 if (get_path_length > 0)
1798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1799 GNUNET_i2s (&get_path[0]));
1800 if (put_path_length > 0)
1801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1802 GNUNET_i2s (&put_path[0]));
1803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1805 for (i = 0; i < p->length; i++)
1807 struct GNUNET_PeerIdentity peer_id;
1809 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1811 GNUNET_i2s (&peer_id));
1819 * Adds a path to the peer_infos of all the peers in the path
1821 * @param p Path to process.
1822 * @param confirmed Whether we know if the path works or not.
1825 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1829 /* TODO: invert and add */
1830 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1831 for (i++; i < p->length; i++)
1833 struct MeshPeerInfo *aux;
1834 struct MeshPeerPath *copy;
1836 aux = peer_get_short (p->peers[i]);
1837 copy = path_duplicate (p);
1838 copy->length = i + 1;
1839 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1845 * Send keepalive packets for a peer
1847 * @param cls Closure (tunnel for which to send the keepalive).
1848 * @param tc Notification context.
1851 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1855 * Search for a tunnel among the incoming tunnels
1857 * @param tid the local id of the tunnel
1859 * @return tunnel handler, NULL if doesn't exist
1861 static struct MeshTunnel *
1862 tunnel_get_incoming (MESH_TunnelNumber tid)
1864 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1865 return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
1870 * Search for a tunnel among the tunnels for a client
1872 * @param c the client whose tunnels to search in
1873 * @param tid the local id of the tunnel
1875 * @return tunnel handler, NULL if doesn't exist
1877 static struct MeshTunnel *
1878 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1880 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1882 return tunnel_get_incoming (tid);
1886 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI);
1887 return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
1893 * Search for a tunnel by global ID using PEER_ID
1895 * @param pi owner of the tunnel
1896 * @param tid global tunnel number
1898 * @return tunnel handler, NULL if doesn't exist
1900 static struct MeshTunnel *
1901 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1903 struct MESH_TunnelID id;
1904 struct GNUNET_HashCode hash;
1909 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1910 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1915 * Search for a tunnel by global ID using full PeerIdentities
1917 * @param oid owner of the tunnel
1918 * @param tid global tunnel number
1920 * @return tunnel handler, NULL if doesn't exist
1922 static struct MeshTunnel *
1923 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1925 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1930 * Add a client to a tunnel, initializing all needed data structures.
1932 * @param t Tunnel to which add the client.
1933 * @param c Client which to add to the tunnel.
1936 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1938 if (NULL != t->client)
1944 GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
1945 t->local_tid_dest, t,
1946 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1952 GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
1953 t->local_tid_dest, t,
1954 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1964 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1966 unsigned int own_pos;
1968 for (own_pos = 0; own_pos < p->length; own_pos++)
1970 if (p->peers[own_pos] == myid)
1973 if (own_pos > p->length - 1)
1979 if (own_pos < p->length - 1)
1980 t->next_hop = p->peers[own_pos + 1];
1982 t->next_hop = p->peers[own_pos];
1983 GNUNET_PEER_change_rc (t->next_hop, 1);
1985 t->prev_hop = p->peers[own_pos - 1];
1987 t->prev_hop = p->peers[0];
1988 GNUNET_PEER_change_rc (t->prev_hop, 1);
1990 if (NULL != t->path)
1991 path_destroy (t->path);
1992 t->path = path_duplicate (p);
1995 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1996 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1997 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
2004 * Notifies a tunnel that a connection has broken that affects at least
2005 * some of its peers. Sends a notification towards the root of the tree.
2006 * In case the peer is the owner of the tree, notifies the client that owns
2007 * the tunnel and tries to reconnect.
2009 * @param t Tunnel affected.
2010 * @param p1 Peer that got disconnected from p2.
2011 * @param p2 Peer that got disconnected from p1.
2013 * @return Short ID of the peer disconnected (either p1 or p2).
2014 * 0 if the tunnel remained unaffected.
2016 static GNUNET_PEER_Id
2017 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2020 // if (myid != p1 && myid != p2) FIXME
2025 // if (tree_get_predecessor (t->tree) != 0)
2027 // /* We are the peer still connected, notify owner of the disconnection. */
2028 // struct GNUNET_MESH_PathBroken msg;
2029 // struct GNUNET_PeerIdentity neighbor;
2031 // msg.header.size = htons (sizeof (msg));
2032 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2033 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2034 // msg.tid = htonl (t->id.tid);
2035 // msg.peer1 = my_full_id;
2036 // GNUNET_PEER_resolve (pid, &msg.peer2);
2037 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2038 // send_prebuilt_message (&msg.header, &neighbor, t);
2045 * Build a local ACK message and send it to a local client.
2047 * @param t Tunnel on which to send the ACK.
2048 * @param c Client to whom send the ACK.
2049 * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
2052 send_local_ack (struct MeshTunnel *t,
2053 struct MeshClient *c,
2056 struct GNUNET_MESH_LocalAck msg;
2058 msg.header.size = htons (sizeof (msg));
2059 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2060 msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
2061 GNUNET_SERVER_notification_context_unicast (nc,
2068 * Build an ACK message and queue it to send to the given peer.
2070 * @param t Tunnel on which to send the ACK.
2071 * @param peer Peer to whom send the ACK.
2072 * @param ack Value of the ACK.
2075 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
2077 struct GNUNET_MESH_ACK msg;
2079 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2080 msg.header.size = htons (sizeof (msg));
2081 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
2082 msg.pid = htonl (ack);
2083 msg.tid = htonl (t->id.tid);
2085 send_prebuilt_message (&msg.header, peer, t);
2090 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2092 * @param t Tunnel this is about.
2093 * @param fwd Is for FWD traffic? (ACK dest->owner)
2096 tunnel_send_data_ack (struct MeshTunnel *t, int fwd)
2098 struct GNUNET_MESH_DataACK msg;
2099 struct MeshTunnelReliability *rel;
2100 struct MeshReliableMessage *copy;
2105 rel = fwd ? t->bck_rel : t->fwd_rel;
2106 hop = fwd ? t->prev_hop : t->next_hop;
2107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2108 "send_data_ack for %u\n",
2111 if (GNUNET_NO == t->reliable)
2113 GNUNET_break_op (0);
2116 msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK :
2117 GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2118 msg.header.size = htons (sizeof (msg));
2119 msg.tid = htonl (t->id.tid);
2120 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2121 msg.mid = htonl (rel->mid_recv - 1);
2123 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2125 delta = copy->mid - rel->mid_recv;
2128 mask = 0x1LL << delta;
2129 msg.futures |= mask;
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131 " setting bit for %u (delta %u) (%llX) -> %llX\n",
2132 copy->mid, delta, mask, msg.futures);
2134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2136 send_prebuilt_message (&msg.header, hop, t);
2137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
2142 * Send an ACK informing the predecessor about the available buffer space.
2143 * In case there is no predecessor, inform the owning client.
2144 * If buffering is off, send only on behalf of children or self if endpoint.
2145 * If buffering is on, send when sent to children and buffer space is free.
2146 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
2147 * the ACK itself goes "back" (towards root).
2149 * @param t Tunnel on which to send the ACK.
2150 * @param type Type of message that triggered the ACK transmission.
2151 * @param fwd Is this FWD ACK? (Going dest->owner)
2154 tunnel_send_ack (struct MeshTunnel *t, uint16_t type, int fwd)
2156 struct MeshTunnelReliability *rel;
2157 struct MeshFlowControl *next_fc;
2158 struct MeshFlowControl *prev_fc;
2159 struct MeshClient *c;
2160 struct MeshClient *o;
2166 rel = fwd ? t->fwd_rel : t->bck_rel;
2167 c = fwd ? t->client : t->owner;
2168 o = fwd ? t->owner : t->client;
2169 next_fc = fwd ? &t->next_fc : &t->prev_fc;
2170 prev_fc = fwd ? &t->prev_fc : &t->next_fc;
2171 hop = fwd ? t->prev_hop : t->next_hop;
2175 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2176 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2179 GNUNET_MESH_DEBUG_M2S (type));
2180 if (GNUNET_YES == t->nobuffer)
2182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2185 if (GNUNET_YES == t->reliable && NULL != c)
2186 tunnel_send_data_ack (t, fwd);
2188 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
2189 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
2190 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2191 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2193 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2194 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2195 t->force_ack = GNUNET_YES;
2201 /* Check if we need to transmit the ACK */
2203 if (0 && NULL == o &&
2204 t->queue_max > next_fc->queue_n * 4 &&
2205 GMC_is_pid_bigger (prev_fc->last_ack_sent, prev_fc->last_pid_recv) &&
2206 GNUNET_NO == t->force_ack)
2208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2210 " t->qmax: %u, t->qn: %u\n",
2211 t->queue_max, next_fc->queue_n);
2212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2213 " t->pid: %u, t->ack: %u\n",
2214 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2218 /* Ok, ACK might be necessary, what PID to ACK? */
2219 delta = t->queue_max - next_fc->queue_n;
2220 if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent)
2221 delta_mid = rel->mid_sent - rel->head_sent->mid;
2224 if (0 > delta || (GNUNET_YES == t->reliable &&
2226 (10 < rel->n_sent || 64 <= delta_mid)))
2228 if (NULL != o && delta > 1)
2230 ack = prev_fc->last_pid_recv + delta;
2231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2233 " last pid %u, last ack %u, qmax %u, q %u\n",
2234 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2235 t->queue_max, next_fc->queue_n);
2236 if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack)
2238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2242 prev_fc->last_ack_sent = ack;
2244 send_local_ack (t, o, fwd);
2246 send_ack (t, hop, ack);
2248 GNUNET_break (GNUNET_YES == t->destroy);
2249 t->force_ack = GNUNET_NO;
2254 * Modify the mesh message TID from global to local and send to client.
2256 * @param t Tunnel on which to send the message.
2257 * @param msg Message to modify and send.
2258 * @param c Client to send to.
2259 * @param tid Tunnel ID to use (c can be both owner and client).
2262 tunnel_send_client_to_tid (struct MeshTunnel *t,
2263 const struct GNUNET_MESH_Data *msg,
2264 struct MeshClient *c, MESH_TunnelNumber tid)
2266 struct GNUNET_MESH_LocalData *copy;
2267 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
2268 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
2270 if (size < sizeof (struct GNUNET_MessageHeader))
2272 GNUNET_break_op (0);
2280 copy = (struct GNUNET_MESH_LocalData *) cbuf;
2281 memcpy (©[1], &msg[1], size);
2282 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
2283 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
2284 copy->tid = htonl (tid);
2285 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2286 ©->header, GNUNET_NO);
2290 * Modify the unicast message TID from global to local and send to client.
2292 * @param t Tunnel on which to send the message.
2293 * @param msg Message to modify and send.
2294 * @param fwd Forward?
2297 tunnel_send_client_data (struct MeshTunnel *t,
2298 const struct GNUNET_MESH_Data *msg,
2302 tunnel_send_client_to_tid (t, msg, t->client, t->local_tid_dest);
2304 tunnel_send_client_to_tid (t, msg, t->owner, t->local_tid);
2309 * Send up to 64 buffered messages to the client for in order delivery.
2311 * @param t Tunnel on which to empty the message buffer.
2312 * @param c Client to send to.
2313 * @param rel Reliability structure to corresponding peer.
2314 * If rel == t->bck_rel, this is FWD data.
2317 tunnel_send_client_buffered_data (struct MeshTunnel *t, struct MeshClient *c,
2318 struct MeshTunnelReliability *rel)
2321 struct MeshReliableMessage *copy;
2322 struct MeshReliableMessage *next;
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
2325 for (copy = rel->head_recv; NULL != copy; copy = next)
2328 if (copy->mid == rel->mid_recv)
2330 struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1];
2332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2333 " have %u! now expecting %u\n",
2334 copy->mid, rel->mid_recv + 1);
2335 tunnel_send_client_data (t, msg, (rel == t->bck_rel));
2337 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2343 " don't have %u, next is %u\n",
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
2354 * We have received a message out of order, buffer it until we receive
2355 * the missing one and we can feed the rest to the client.
2357 * @param t Tunnel to add to.
2358 * @param msg Message to buffer.
2359 * @param rel Reliability data to the corresponding direction.
2362 tunnel_add_buffered_data (struct MeshTunnel *t,
2363 const struct GNUNET_MESH_Data *msg,
2364 struct MeshTunnelReliability *rel)
2366 struct MeshReliableMessage *copy;
2367 struct MeshReliableMessage *prev;
2371 size = ntohs (msg->header.size);
2372 mid = ntohl (msg->mid);
2373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
2375 copy = GNUNET_malloc (sizeof (*copy) + size);
2378 memcpy (©[1], msg, size);
2380 // FIXME do something better than O(n), although n < 64...
2381 // FIXME start from the end (most messages are the latest ones)
2382 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
2384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
2385 if (GMC_is_pid_bigger (prev->mid, mid))
2387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
2388 GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
2393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
2394 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
2395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
2400 * Destroy a reliable message after it has been acknowledged, either by
2401 * direct mid ACK or bitfield. Updates the appropriate data structures and
2402 * timers and frees all memory.
2404 * @param copy Message that is no longer needed: remote peer got it.
2407 tunnel_free_reliable_message (struct MeshReliableMessage *copy)
2409 struct MeshTunnelReliability *rel;
2410 struct GNUNET_TIME_Relative time;
2413 time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
2414 rel->expected_delay.rel_value *= 7;
2415 rel->expected_delay.rel_value += time.rel_value;
2416 rel->expected_delay.rel_value /= 8;
2418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
2419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
2420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n",
2421 GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
2422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n",
2423 GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
2425 rel->retry_timer = rel->expected_delay;
2426 GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
2432 * Mark future messages as ACK'd.
2434 * @param t Tunnel whose sent buffer to clean.
2435 * @param msg DataACK message with a bitfield of future ACK'd messages.
2436 * @param rel Reliability data.
2439 tunnel_free_sent_reliable (struct MeshTunnel *t,
2440 const struct GNUNET_MESH_DataACK *msg,
2441 struct MeshTunnelReliability *rel)
2443 struct MeshReliableMessage *copy;
2444 struct MeshReliableMessage *next;
2451 bitfield = msg->futures;
2452 mid = ntohl (msg->mid);
2453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2454 "free_sent_reliable %u %llX\n",
2456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2457 " rel %p, head %p\n",
2458 rel, rel->head_sent);
2459 for (i = 0, copy = rel->head_sent;
2460 i < 64 && NULL != copy && 0 != bitfield;
2463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2464 " trying bit %u (mid %u)\n",
2467 if (0 == (bitfield & mask))
2470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
2471 /* Bit was set, clear the bit from the bitfield */
2474 /* The i-th bit was set. Do we have that copy? */
2475 /* Skip copies with mid < target */
2476 target = mid + i + 1;
2477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
2478 while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
2481 /* Did we run out of copies? (previously freed, it's ok) */
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
2488 /* Did we overshoot the target? (previously freed, it's ok) */
2489 if (GMC_is_pid_bigger (copy->mid, target))
2491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
2495 /* Now copy->mid == target, free it */
2497 tunnel_free_reliable_message (copy);
2500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
2505 * We haven't received an ACK after a certain time: restransmit the message.
2507 * @param cls Closure (MeshReliableMessage with the message to restransmit)
2508 * @param tc TaskContext.
2511 tunnel_retransmit_message (void *cls,
2512 const struct GNUNET_SCHEDULER_TaskContext *tc)
2514 struct MeshTunnelReliability *rel = cls;
2515 struct MeshReliableMessage *copy;
2516 struct MeshFlowControl *fc;
2517 struct MeshPeerQueue *q;
2518 struct MeshPeerInfo *pi;
2519 struct MeshTunnel *t;
2520 struct GNUNET_MESH_Data *payload;
2523 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
2524 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2528 copy = rel->head_sent;
2535 /* Search the message to be retransmitted in the outgoing queue */
2536 payload = (struct GNUNET_MESH_Data *) ©[1];
2537 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
2538 fc = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc;
2539 pi = peer_get_short (hop);
2540 for (q = pi->queue_head; NULL != q; q = q->next)
2542 if (ntohs (payload->header.type) == q->type)
2544 struct GNUNET_MESH_Data *queued_data = q->cls;
2546 if (queued_data->mid == payload->mid)
2551 /* Message not found in the queue */
2554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
2556 fc->last_ack_sent++;
2557 fc->last_pid_recv++;
2558 payload->pid = htonl (fc->last_pid_recv);
2559 send_prebuilt_message (&payload->header, hop, t);
2560 GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
2564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! STILL IN QUEUE %u\n", copy->mid);
2567 rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
2568 rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
2569 &tunnel_retransmit_message,
2575 * @brief Re-initiate traffic to this peer if necessary.
2577 * Check if there is traffic queued towards this peer
2578 * and the core transmit handle is NULL (traffic was stalled).
2579 * If so, call core tmt rdy.
2581 * @param peer_id Short ID of peer to which initiate traffic.
2584 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2586 struct MeshPeerInfo *peer;
2587 struct GNUNET_PeerIdentity id;
2588 struct MeshPeerQueue *q;
2591 peer = peer_get_short (peer_id);
2592 if (NULL != peer->core_transmit)
2595 q = queue_get_next (peer);
2598 /* Might br multicast traffic already sent to this particular peer but
2599 * not to other children in this tunnel.
2600 * This way t->queue_n would be > 0 but the queue of this particular peer
2606 GNUNET_PEER_resolve (peer->id, &id);
2607 peer->core_transmit =
2608 GNUNET_CORE_notify_transmit_ready(core_handle,
2611 GNUNET_TIME_UNIT_FOREVER_REL,
2621 * Send a message to all peers and clients in this tunnel that the tunnel
2622 * is no longer valid. If some peer or client should not receive the message,
2623 * should be zero'ed out before calling this function.
2625 * @param t The tunnel whose peers and clients to notify.
2628 tunnel_send_destroy (struct MeshTunnel *t)
2630 struct GNUNET_MESH_TunnelDestroy msg;
2631 struct GNUNET_PeerIdentity id;
2633 msg.header.size = htons (sizeof (msg));
2634 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2635 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2636 msg.tid = htonl (t->id.tid);
2637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2638 " sending tunnel destroy for tunnel: %s [%X]\n",
2639 GNUNET_i2s (&msg.oid), t->id.tid);
2641 if (NULL == t->client && 0 != t->next_hop)
2643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2644 GNUNET_PEER_resolve (t->next_hop, &id);
2645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2646 " sending forward to %s\n",
2648 send_prebuilt_message (&msg.header, t->next_hop, t);
2650 if (NULL == t->owner && 0 != t->prev_hop)
2652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2653 GNUNET_PEER_resolve (t->prev_hop, &id);
2654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2655 " sending back to %s\n",
2657 send_prebuilt_message (&msg.header, t->prev_hop, t);
2659 if (NULL != t->owner)
2661 send_client_tunnel_destroy (t, GNUNET_NO);
2663 if (NULL != t->client)
2665 send_client_tunnel_destroy (t, GNUNET_YES);
2671 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2673 * @param t Tunnel which to cancel.
2674 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2677 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2679 struct MeshPeerInfo *peer_info;
2680 struct MeshPeerQueue *pq;
2681 struct MeshPeerQueue *next;
2684 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2685 peer_info = peer_get_short (neighbor);
2686 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2689 if (pq->tunnel == t)
2691 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2692 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2694 /* Should have been removed on destroy children */
2697 queue_destroy (pq, GNUNET_YES);
2700 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2702 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2703 peer_info->core_transmit = NULL;
2709 * Destroy the tunnel.
2711 * This function does not generate any warning traffic to clients or peers.
2714 * Remove the tunnel from peer_info's and clients' hashmaps.
2715 * Cancel messages belonging to this tunnel queued to neighbors.
2716 * Free any allocated resources linked to the tunnel.
2718 * @param t the tunnel to destroy
2720 * @return GNUNET_OK on success
2723 tunnel_destroy (struct MeshTunnel *t)
2725 struct MeshClient *c;
2726 struct GNUNET_HashCode hash;
2736 struct GNUNET_PeerIdentity id;
2738 GNUNET_PEER_resolve (t->id.oid, &id);
2739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2740 GNUNET_i2s (&id), t->id.tid);
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2746 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2747 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2756 GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels, t->local_tid, t))
2763 if (NULL != t->client)
2767 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
2768 t->local_tid_dest, t))
2774 GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
2775 t->local_tid_dest, t))
2782 if (0 != t->prev_hop)
2784 peer_cancel_queues (t->prev_hop, t);
2785 GNUNET_PEER_change_rc (t->prev_hop, -1);
2787 if (0 != t->next_hop)
2789 peer_cancel_queues (t->next_hop, t);
2790 GNUNET_PEER_change_rc (t->next_hop, -1);
2792 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task)
2794 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
2795 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
2797 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task)
2799 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
2800 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
2803 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2806 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2807 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2810 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2811 path_destroy (t->path);
2817 * Tunnel is empty: destroy it.
2819 * Notifies all participants (peers, cleints) about the destruction.
2821 * @param t Tunnel to destroy.
2824 tunnel_destroy_empty (struct MeshTunnel *t)
2828 struct GNUNET_PeerIdentity id;
2830 GNUNET_PEER_resolve (t->id.oid, &id);
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2832 "executing destruction of empty tunnel %s [%X]\n",
2833 GNUNET_i2s (&id), t->id.tid);
2837 if (GNUNET_NO == t->destroy)
2838 tunnel_send_destroy (t);
2839 if (0 == t->pending_messages)
2842 t->destroy = GNUNET_YES;
2846 * Initialize a Flow Control structure to the initial state.
2848 * @param fc Flow Control structure to initialize.
2851 fc_init (struct MeshFlowControl *fc)
2853 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2854 fc->last_pid_recv = (uint32_t) -1;
2855 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2856 fc->last_ack_recv = (uint32_t) -1;
2857 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2858 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2863 * Create a new tunnel
2865 * @param owner Who is the owner of the tunnel (short ID).
2866 * @param tid Tunnel Number of the tunnel.
2867 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2868 * @param local Tunnel Number for the tunnel, for the client point of view.
2870 * @return A new initialized tunnel. NULL on error.
2872 static struct MeshTunnel *
2873 tunnel_new (GNUNET_PEER_Id owner,
2874 MESH_TunnelNumber tid,
2875 struct MeshClient *client,
2876 MESH_TunnelNumber local)
2878 struct MeshTunnel *t;
2879 struct GNUNET_HashCode hash;
2881 if (n_tunnels >= max_tunnels && NULL == client)
2884 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2887 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2889 fc_init (&t->next_fc);
2890 fc_init (&t->prev_fc);
2891 t->local_tid = local;
2893 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2895 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2897 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2898 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2905 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2913 GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
2915 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2919 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2929 * Set options in a tunnel, extracted from a bit flag field
2931 * @param t Tunnel to set options to.
2932 * @param options Bit array in host byte order.
2935 tunnel_set_options (struct MeshTunnel *t, uint32_t options)
2937 t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
2938 GNUNET_YES : GNUNET_NO;
2939 t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
2940 GNUNET_YES : GNUNET_NO;
2945 * Iterator for deleting each tunnel whose client endpoint disconnected.
2947 * @param cls Closure (client that has disconnected).
2948 * @param key The local tunnel id (used to access the hashmap).
2949 * @param value The value stored at the key (tunnel to destroy).
2951 * @return GNUNET_OK, keep iterating.
2954 tunnel_destroy_iterator (void *cls,
2958 struct MeshTunnel *t = value;
2959 struct MeshClient *c = cls;
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2962 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2963 t->local_tid, t->local_tid_dest, c->id);
2964 client_delete_tunnel (c, t);
2967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2969 if (0 != t->next_hop) { /* destroy could come before a path is used */
2970 GNUNET_PEER_change_rc (t->next_hop, -1);
2976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2978 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2979 GNUNET_PEER_change_rc (t->prev_hop, -1);
2984 tunnel_destroy_empty (t);
2991 * Timeout function, destroys tunnel if called
2993 * @param cls Closure (tunnel to destroy).
2994 * @param tc TaskContext
2997 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2999 struct MeshTunnel *t = cls;
3000 struct GNUNET_PeerIdentity id;
3002 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3003 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3005 GNUNET_PEER_resolve(t->id.oid, &id);
3006 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3007 "Tunnel %s [%X] timed out. Destroying.\n",
3008 GNUNET_i2s(&id), t->id.tid);
3009 if (NULL != t->client)
3010 send_client_tunnel_destroy (t, GNUNET_YES);
3011 tunnel_destroy (t); /* Do not notify other */
3016 * Resets the tunnel timeout. Starts it if no timeout was running.
3018 * @param t Tunnel whose timeout to reset.
3019 * @param fwd Is this forward?
3021 * TODO use heap to improve efficiency of scheduler.
3022 * FIXME use fwd, keep 2 timers
3025 tunnel_reset_timeout (struct MeshTunnel *t, int fwd)
3027 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
3029 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
3030 GNUNET_SCHEDULER_cancel (t->maintenance_task);
3031 t->maintenance_task =
3032 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3033 (refresh_path_time, 4), &tunnel_timeout, t);
3037 /******************************************************************************/
3038 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3039 /******************************************************************************/
3042 * Function to send a create path packet to a peer.
3044 * @param cls closure
3045 * @param size number of bytes available in buf
3046 * @param buf where the callee should write the message
3047 * @return number of bytes written to buf
3050 send_core_path_create (void *cls, size_t size, void *buf)
3052 struct MeshTunnel *t = cls;
3053 struct GNUNET_MESH_CreateTunnel *msg;
3054 struct GNUNET_PeerIdentity *peer_ptr;
3055 struct MeshPeerPath *p = t->path;
3060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3062 sizeof (struct GNUNET_MESH_CreateTunnel) +
3063 p->length * sizeof (struct GNUNET_PeerIdentity);
3065 if (size < size_needed || NULL == buf)
3070 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
3071 msg->header.size = htons (size_needed);
3072 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3073 msg->tid = ntohl (t->id.tid);
3076 if (GNUNET_YES == t->nobuffer)
3077 opt |= GNUNET_MESH_OPTION_NOBUFFER;
3078 if (GNUNET_YES == t->reliable)
3079 opt |= GNUNET_MESH_OPTION_RELIABLE;
3080 msg->opt = htonl (opt);
3081 msg->port = htonl (t->port);
3083 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3084 for (i = 0; i < p->length; i++)
3086 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3090 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3096 * Creates a path ack message in buf and frees all unused resources.
3098 * @param cls closure (MeshTransmissionDescriptor)
3099 * @param size number of bytes available in buf
3100 * @param buf where the callee should write the message
3101 * @return number of bytes written to buf
3104 send_core_path_ack (void *cls, size_t size, void *buf)
3106 struct MeshTunnel *t = cls;
3107 struct GNUNET_MESH_PathACK *msg = buf;
3109 GNUNET_assert (NULL != t);
3110 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3115 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
3116 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
3117 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3118 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
3119 msg->tid = htonl (t->id.tid);
3120 msg->peer_id = my_full_id;
3121 msg->ack = htonl (t->prev_fc.last_ack_sent);
3123 /* TODO add signature */
3125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
3126 return sizeof (struct GNUNET_MESH_PathACK);
3131 * Free a transmission that was already queued with all resources
3132 * associated to the request.
3134 * @param queue Queue handler to cancel.
3135 * @param clear_cls Is it necessary to free associated cls?
3138 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
3140 struct MeshFlowControl *fc;
3142 if (GNUNET_YES == clear_cls)
3144 switch (queue->type)
3146 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3147 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
3148 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
3150 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3151 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3152 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3153 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3154 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3156 " prebuilt message\n");
3157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3159 GNUNET_MESH_DEBUG_M2S (queue->type));
3161 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
3166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3167 " type %s unknown!\n",
3168 GNUNET_MESH_DEBUG_M2S (queue->type));
3170 GNUNET_free_non_null (queue->cls);
3172 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
3173 queue->peer->queue_tail,
3176 /* Delete from appropriate fc in the tunnel */
3177 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
3178 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
3180 if (queue->peer->id == queue->tunnel->prev_hop)
3181 fc = &queue->tunnel->prev_fc;
3182 else if (queue->peer->id == queue->tunnel->next_hop)
3183 fc = &queue->tunnel->next_fc;
3187 GNUNET_free (queue);
3192 GNUNET_free (queue);
3197 * @brief Get the next transmittable message from the queue.
3199 * This will be the head, except in the case of being a data packet
3200 * not allowed by the destination peer.
3202 * @param peer Destination peer.
3204 * @return The next viable MeshPeerQueue element to send to that peer.
3205 * NULL when there are no transmittable messages.
3207 struct MeshPeerQueue *
3208 queue_get_next (const struct MeshPeerInfo *peer)
3210 struct MeshPeerQueue *q;
3212 struct GNUNET_MESH_Data *dmsg;
3213 struct MeshTunnel* t;
3217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
3218 for (q = peer->queue_head; NULL != q; q = q->next)
3221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3223 GNUNET_MESH_DEBUG_M2S (q->type));
3224 dmsg = (struct GNUNET_MESH_Data *) q->cls;
3225 pid = ntohl (dmsg->pid);
3228 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3229 ack = t->next_fc.last_ack_recv;
3231 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3232 ack = t->prev_fc.last_ack_recv;
3235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3240 "* ACK: %u, PID: %u, MID: %u\n",
3241 ack, pid, ntohl (dmsg->mid));
3242 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
3244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3255 "* nothing found\n");
3261 queue_send (void *cls, size_t size, void *buf)
3263 struct MeshPeerInfo *peer = cls;
3264 struct GNUNET_MessageHeader *msg;
3265 struct MeshPeerQueue *queue;
3266 struct MeshTunnel *t;
3267 struct GNUNET_PeerIdentity dst_id;
3268 struct MeshFlowControl *fc;
3273 peer->core_transmit = NULL;
3275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
3276 queue = queue_get_next (peer);
3278 /* Queue has no internal mesh traffic nor sendable payload */
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
3282 if (NULL == peer->queue_head)
3283 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
3286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
3288 GNUNET_PEER_resolve (peer->id, &dst_id);
3289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3291 GNUNET_i2s (&dst_id));
3292 /* Check if buffer size is enough for the message */
3293 if (queue->size > size)
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3296 "* not enough room, reissue\n");
3297 peer->core_transmit =
3298 GNUNET_CORE_notify_transmit_ready (core_handle,
3301 GNUNET_TIME_UNIT_FOREVER_REL,
3308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
3311 GNUNET_assert (0 < t->pending_messages);
3312 t->pending_messages--;
3316 switch (queue->type)
3319 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3320 case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
3321 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
3322 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3323 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
3324 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
3325 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
3326 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3329 GNUNET_MESH_DEBUG_M2S (queue->type));
3331 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3332 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3333 data_size = send_core_data_raw (queue->cls, size, buf);
3334 msg = (struct GNUNET_MessageHeader *) buf;
3335 type = ntohs (msg->type);
3337 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
3338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
3339 data_size = send_core_path_create (queue->cls, size, buf);
3341 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
3343 data_size = send_core_path_ack (queue->cls, size, buf);
3347 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3348 "* type unknown: %u\n",
3353 if (0 < drop_percent &&
3354 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
3356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3357 "Dropping message of type %s\n",
3358 GNUNET_MESH_DEBUG_M2S(queue->type));
3361 /* Free queue, but cls was freed by send_core_* */
3362 queue_destroy (queue, GNUNET_NO);
3364 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
3365 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
3369 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3370 t->next_fc.last_pid_sent = pid;
3371 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, GNUNET_YES);
3372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3374 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3376 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3377 t->prev_fc.last_pid_sent = pid;
3378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3380 ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
3381 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, GNUNET_NO);
3387 /* If more data in queue, send next */
3388 queue = queue_get_next (peer);
3391 struct GNUNET_PeerIdentity id;
3393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
3394 GNUNET_PEER_resolve (peer->id, &id);
3395 peer->core_transmit =
3396 GNUNET_CORE_notify_transmit_ready(core_handle,
3399 GNUNET_TIME_UNIT_FOREVER_REL,
3405 if (peer->id == t->next_hop)
3407 else if (peer->id == t->prev_hop)
3412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id: %u, next: %u, prev: %u\n",
3413 peer->id, t->next_hop, t->prev_hop);
3416 if (NULL != peer->queue_head)
3418 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task && fc->queue_n > 0)
3420 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3421 "* %s starting poll timeout\n",
3422 GNUNET_i2s (&my_full_id));
3424 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3430 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
3432 GNUNET_SCHEDULER_cancel (fc->poll_task);
3433 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
3436 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
3438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
3441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3447 * @brief Queue and pass message to core when possible.
3449 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3450 * accounts for it. In case the queue is full, the message is dropped and
3453 * Otherwise, message is treated as internal and allowed to go regardless of
3456 * @param cls Closure (@c type dependant). It will be used by queue_send to
3457 * build the message to be sent if not already prebuilt.
3458 * @param type Type of the message, 0 for a raw message.
3459 * @param size Size of the message.
3460 * @param dst Neighbor to send message to.
3461 * @param t Tunnel this message belongs to.
3464 queue_add (void *cls, uint16_t type, size_t size,
3465 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3467 struct MeshPeerQueue *queue;
3468 struct GNUNET_PeerIdentity id;
3469 struct MeshFlowControl *fc;
3473 priority = GNUNET_NO;
3474 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3478 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3484 if (fc->queue_n >= t->queue_max)
3486 /* If this isn't a retransmission, drop the message */
3487 if (GNUNET_NO == t->reliable ||
3488 (NULL == t->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) ||
3489 (NULL == t->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type))
3491 GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
3494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3495 "queue full: %u/%u\n",
3496 fc->queue_n, t->queue_max);
3497 return; /* Drop this message */
3499 priority = GNUNET_YES;
3502 if (GMC_is_pid_bigger(fc->last_pid_sent + 1, fc->last_ack_recv) &&
3503 GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
3504 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3508 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3514 if (GNUNET_YES == priority)
3516 struct GNUNET_MESH_Data *d;
3520 GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
3521 d = (struct GNUNET_MESH_Data *) queue->cls;
3523 for (queue = dst->queue_tail; NULL != queue; queue = queue->prev)
3525 if (queue->type != type)
3527 d = (struct GNUNET_MESH_Data *) queue->cls;
3534 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3536 if (NULL == dst->core_transmit)
3538 GNUNET_PEER_resolve (dst->id, &id);
3539 dst->core_transmit =
3540 GNUNET_CORE_notify_transmit_ready (core_handle,
3543 GNUNET_TIME_UNIT_FOREVER_REL,
3549 t->pending_messages++;
3553 /******************************************************************************/
3554 /******************** MESH NETWORK HANDLERS **************************/
3555 /******************************************************************************/
3559 * Core handler for path creation
3561 * @param cls closure
3562 * @param message message
3563 * @param peer peer identity this notification is about
3565 * @return GNUNET_OK to keep the connection open,
3566 * GNUNET_SYSERR to close it (signal serious error)
3569 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3570 const struct GNUNET_MessageHeader *message)
3572 unsigned int own_pos;
3575 MESH_TunnelNumber tid;
3576 struct GNUNET_MESH_CreateTunnel *msg;
3577 struct GNUNET_PeerIdentity *pi;
3578 struct MeshPeerPath *path;
3579 struct MeshPeerInfo *dest_peer_info;
3580 struct MeshPeerInfo *orig_peer_info;
3581 struct MeshTunnel *t;
3583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3584 "Received a path create msg [%s]\n",
3585 GNUNET_i2s (&my_full_id));
3586 size = ntohs (message->size);
3587 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3589 GNUNET_break_op (0);
3593 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3594 if (size % sizeof (struct GNUNET_PeerIdentity))
3596 GNUNET_break_op (0);
3599 size /= sizeof (struct GNUNET_PeerIdentity);
3602 GNUNET_break_op (0);
3605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3606 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3608 tid = ntohl (msg->tid);
3609 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3611 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3612 t = tunnel_get (pi, tid);
3613 if (NULL == t) /* might be a local tunnel */
3617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3618 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3624 t->port = ntohl (msg->port);
3625 opt = ntohl (msg->opt);
3626 if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
3628 t->nobuffer = GNUNET_YES;
3631 if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
3633 t->reliable = GNUNET_YES;
3635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3637 tunnel_reset_timeout (t, GNUNET_YES); // FIXME
3639 t->state = MESH_TUNNEL_WAITING;
3641 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3642 if (NULL == dest_peer_info)
3644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3645 " Creating PeerInfo for destination.\n");
3646 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3647 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3648 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3650 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3652 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3653 if (NULL == orig_peer_info)
3655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3656 " Creating PeerInfo for origin.\n");
3657 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3658 orig_peer_info->id = GNUNET_PEER_intern (pi);
3659 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3660 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3663 path = path_new (size);
3665 for (i = 0; i < size; i++)
3667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3668 GNUNET_i2s (&pi[i]));
3669 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3670 if (path->peers[i] == myid)
3673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3674 if (own_pos == 0 && path->peers[own_pos] != myid)
3676 /* create path: self not found in path through self */
3677 GNUNET_break_op (0);
3678 path_destroy (path);
3682 path_add_to_peers (path, GNUNET_NO);
3683 tunnel_use_path (t, path);
3685 peer_info_add_tunnel (dest_peer_info, t);
3687 if (own_pos == size - 1)
3689 struct MeshClient *c;
3691 /* Find target client */
3692 c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
3695 /* TODO send reject */
3699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3700 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3702 /* Assign local tid */
3703 while (NULL != tunnel_get_incoming (next_local_tid))
3704 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3705 t->local_tid_dest = next_local_tid++;
3706 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3708 if (GNUNET_YES == t->reliable)
3710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
3711 t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
3713 t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
3716 tunnel_add_client (t, c);
3717 send_client_tunnel_create (t);
3722 struct MeshPeerPath *path2;
3724 t->next_hop = path->peers[own_pos + 1];
3725 GNUNET_PEER_change_rc(t->next_hop, 1);
3727 /* It's for somebody else! Retransmit. */
3728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3729 path2 = path_duplicate (path);
3730 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3731 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3732 send_create_path (t);
3740 * Core handler for path ACKs
3742 * @param cls closure
3743 * @param message message
3744 * @param peer peer identity this notification is about
3746 * @return GNUNET_OK to keep the connection open,
3747 * GNUNET_SYSERR to close it (signal serious error)
3750 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3751 const struct GNUNET_MessageHeader *message)
3753 struct GNUNET_MESH_PathACK *msg;
3754 struct MeshPeerInfo *peer_info;
3755 struct MeshPeerPath *p;
3756 struct MeshTunnel *t;
3758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3759 GNUNET_i2s (&my_full_id));
3760 msg = (struct GNUNET_MESH_PathACK *) message;
3761 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3764 /* TODO notify that we don't know the tunnel */
3765 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3767 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3771 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3773 peer_info = peer_get (&msg->peer_id);
3774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3775 GNUNET_i2s (&msg->peer_id));
3776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3779 /* Add path to peers? */
3783 path_add_to_peers (p, GNUNET_YES);
3789 t->state = MESH_TUNNEL_READY;
3790 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3791 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3793 /* Message for us? */
3794 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3797 if (NULL == t->owner)
3799 GNUNET_break_op (0);
3802 if (NULL != peer_info->dhtget)
3804 GNUNET_DHT_get_stop (peer_info->dhtget);
3805 peer_info->dhtget = NULL;
3807 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_YES);
3808 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_NO);
3812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3813 " not for us, retransmitting...\n");
3814 peer_info = peer_get (&msg->oid);
3815 send_prebuilt_message (message, t->prev_hop, t);
3821 * Core handler for notifications of broken paths
3823 * @param cls closure
3824 * @param message message
3825 * @param peer peer identity this notification is about
3827 * @return GNUNET_OK to keep the connection open,
3828 * GNUNET_SYSERR to close it (signal serious error)
3831 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3832 const struct GNUNET_MessageHeader *message)
3834 struct GNUNET_MESH_PathBroken *msg;
3835 struct MeshTunnel *t;
3837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3838 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3839 msg = (struct GNUNET_MESH_PathBroken *) message;
3840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3841 GNUNET_i2s (&msg->peer1));
3842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3843 GNUNET_i2s (&msg->peer2));
3844 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3847 GNUNET_break_op (0);
3850 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3851 GNUNET_PEER_search (&msg->peer2));
3858 * Core handler for tunnel destruction
3860 * @param cls closure
3861 * @param message message
3862 * @param peer peer identity this notification is about
3864 * @return GNUNET_OK to keep the connection open,
3865 * GNUNET_SYSERR to close it (signal serious error)
3868 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3869 const struct GNUNET_MessageHeader *message)
3871 struct GNUNET_MESH_TunnelDestroy *msg;
3872 struct MeshTunnel *t;
3874 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3876 "Got a TUNNEL DESTROY packet from %s\n",
3878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3879 " for tunnel %s [%u]\n",
3880 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3881 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3884 /* Probably already got the message from another path,
3885 * destroyed the tunnel and retransmitted to children.
3888 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3892 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3895 t->local_tid, t->local_tid_dest);
3897 if (GNUNET_PEER_search (peer) == t->prev_hop)
3899 // TODO check owner's signature
3900 // TODO add owner's signatue to tunnel for retransmission
3901 peer_cancel_queues (t->prev_hop, t);
3902 GNUNET_PEER_change_rc (t->prev_hop, -1);
3905 else if (GNUNET_PEER_search (peer) == t->next_hop)
3907 // TODO check dest's signature
3908 // TODO add dest's signatue to tunnel for retransmission
3909 peer_cancel_queues (t->next_hop, t);
3910 GNUNET_PEER_change_rc (t->next_hop, -1);
3915 GNUNET_break_op (0);
3916 // TODO check both owner AND destination's signature to see which matches
3917 // TODO restransmit in appropriate direction
3920 tunnel_destroy_empty (t);
3922 // TODO: add timeout to destroy the tunnel anyway
3928 * Generic handler for mesh network payload traffic.
3930 * @param peer Peer identity this notification is about.
3931 * @param message Data message.
3932 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
3934 * @return GNUNET_OK to keep the connection open,
3935 * GNUNET_SYSERR to close it (signal serious error)
3938 handle_mesh_data (const struct GNUNET_PeerIdentity *peer,
3939 const struct GNUNET_MessageHeader *message,
3942 struct GNUNET_MESH_Data *msg;
3943 struct MeshFlowControl *fc;
3944 struct MeshTunnelReliability *rel;
3945 struct MeshTunnel *t;
3946 struct MeshClient *c;
3952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
3953 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)),
3956 size = ntohs (message->size);
3958 sizeof (struct GNUNET_MESH_Data) +
3959 sizeof (struct GNUNET_MessageHeader))
3964 msg = (struct GNUNET_MESH_Data *) message;
3965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
3966 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3968 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3971 /* TODO notify back: we don't know this tunnel */
3972 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3973 GNUNET_break_op (0);
3977 /* Initialize FWD/BCK data */
3978 pid = ntohl (msg->pid);
3979 fc = fwd ? &t->prev_fc : &t->next_fc;
3980 c = fwd ? t->client : t->owner;
3981 rel = fwd ? t->bck_rel : t->fwd_rel;
3982 hop = fwd ? t->next_hop : t->prev_hop;
3983 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
3985 GNUNET_STATISTICS_update (stats, "# unsolicited data", 1, GNUNET_NO);
3986 GNUNET_break_op (0);
3987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3988 "Received PID %u, (prev %u), ACK %u\n",
3989 pid, fc->last_pid_recv, fc->last_ack_sent);
3993 tunnel_reset_timeout (t, fwd);
3996 /* TODO signature verification */
3997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us! sending to client\n");
3998 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
3999 if (GMC_is_pid_bigger (pid, fc->last_pid_recv))
4003 mid = ntohl (msg->mid);
4004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4005 " pid %u (mid %u) not seen yet\n", pid, mid);
4006 fc->last_pid_recv = pid;
4008 if (GNUNET_NO == t->reliable ||
4009 ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
4010 GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
4012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4013 "!!! RECV %u\n", ntohl (msg->mid));
4014 if (GNUNET_YES == t->reliable)
4016 /* Is this the exact next expected messasge? */
4017 if (mid == rel->mid_recv)
4019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4021 tunnel_send_client_data (t, msg, fwd);
4022 tunnel_send_client_buffered_data (t, c, rel);
4026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4027 tunnel_add_buffered_data (t, msg, rel);
4030 else /* Tunnel unreliable, send to clients directly */
4032 tunnel_send_client_data (t, msg, fwd);
4037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4038 " MID %u not expected (%u - %u), dropping!\n",
4039 ntohl (msg->mid), rel->mid_recv, rel->mid_recv + 64);
4044 // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4046 " Pid %u not expected (%u+), dropping!\n",
4047 pid, fc->last_pid_recv + 1);
4049 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, fwd);
4052 fc->last_pid_recv = pid;
4055 GNUNET_STATISTICS_update (stats, "# data on dying tunnel", 1, GNUNET_NO);
4056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "data on dying tunnel %s[%X]\n",
4057 GNUNET_PEER_resolve2 (t->id.oid), ntohl (msg->tid));
4058 return GNUNET_OK; /* Next hop has destoyed the tunnel, drop */
4060 ttl = ntohl (msg->ttl);
4061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4064 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4065 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4066 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, fwd);
4072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4073 send_prebuilt_message (message, hop, t);
4074 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4081 * Core handler for mesh network traffic going from the origin to a peer
4083 * @param cls Closure (unused).
4084 * @param message Message received.
4085 * @param peer Peer who sent the message.
4087 * @return GNUNET_OK to keep the connection open,
4088 * GNUNET_SYSERR to close it (signal serious error)
4091 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4092 const struct GNUNET_MessageHeader *message)
4094 return handle_mesh_data (peer, message, GNUNET_YES);
4098 * Core handler for mesh network traffic towards the owner of a tunnel.
4100 * @param cls Closure (unused).
4101 * @param message Message received.
4102 * @param peer Peer who sent the message.
4104 * @return GNUNET_OK to keep the connection open,
4105 * GNUNET_SYSERR to close it (signal serious error)
4108 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4109 const struct GNUNET_MessageHeader *message)
4111 return handle_mesh_data (peer, message, GNUNET_NO);
4116 * Core handler for mesh network traffic end-to-end ACKs.
4118 * @param cls Closure.
4119 * @param message Message.
4120 * @param peer Peer identity this notification is about.
4122 * @return GNUNET_OK to keep the connection open,
4123 * GNUNET_SYSERR to close it (signal serious error)
4126 handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4127 const struct GNUNET_MessageHeader *message)
4129 struct GNUNET_MESH_DataACK *msg;
4130 struct MeshTunnelReliability *rel;
4131 struct MeshReliableMessage *copy;
4132 struct MeshReliableMessage *next;
4133 struct MeshTunnel *t;
4139 type = ntohs (message->type);
4140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
4141 GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
4142 msg = (struct GNUNET_MESH_DataACK *) message;
4144 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4147 /* TODO notify that we dont know this tunnel (whom)? */
4148 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4151 ack = ntohl (msg->mid);
4152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4154 /* Is this a forward or backward ACK? */
4155 id = GNUNET_PEER_search (peer);
4156 if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
4158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4159 if (NULL == t->owner)
4161 send_prebuilt_message (message, t->prev_hop, t);
4166 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4169 if (NULL == t->client)
4171 send_prebuilt_message (message, t->next_hop, t);
4178 GNUNET_break_op (0);
4182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %u\n", ack);
4183 for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
4185 if (GMC_is_pid_bigger (copy->mid, ack))
4187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid);
4188 tunnel_free_sent_reliable (t, msg, rel);
4192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid);
4194 tunnel_free_reliable_message (copy);
4196 /* Once buffers have been free'd, send ACK */
4197 tunnel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
4199 /* If some message was free'd, update the retransmission delay*/
4200 if (GNUNET_YES == work)
4202 if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
4204 GNUNET_SCHEDULER_cancel (rel->retry_task);
4205 if (NULL == rel->head_sent)
4207 rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
4211 struct GNUNET_TIME_Absolute new_target;
4212 struct GNUNET_TIME_Relative delay;
4214 delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
4215 MESH_RETRANSMIT_MARGIN);
4216 new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
4218 delay = GNUNET_TIME_absolute_get_remaining (new_target);
4220 GNUNET_SCHEDULER_add_delayed (delay,
4221 &tunnel_retransmit_message,
4232 * Core handler for mesh network traffic point-to-point acks.
4234 * @param cls closure
4235 * @param message message
4236 * @param peer peer identity this notification is about
4238 * @return GNUNET_OK to keep the connection open,
4239 * GNUNET_SYSERR to close it (signal serious error)
4242 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4243 const struct GNUNET_MessageHeader *message)
4245 struct GNUNET_MESH_ACK *msg;
4246 struct MeshTunnel *t;
4247 struct MeshFlowControl *fc;
4251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
4253 msg = (struct GNUNET_MESH_ACK *) message;
4255 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4259 /* TODO notify that we dont know this tunnel (whom)? */
4260 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
4263 ack = ntohl (msg->pid);
4264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
4266 /* Is this a forward or backward ACK? */
4267 id = GNUNET_PEER_search (peer);
4268 if (t->next_hop == id)
4270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4273 else if (t->prev_hop == id)
4275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4280 GNUNET_break_op (0);
4284 if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
4285 GMC_is_pid_bigger (ack, fc->last_ack_recv))
4287 GNUNET_SCHEDULER_cancel (fc->poll_task);
4288 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
4289 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
4291 fc->last_ack_recv = ack;
4292 peer_unlock_queue (id);
4294 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, t->next_hop == id);
4301 * Core handler for mesh network traffic point-to-point ack polls.
4303 * @param cls closure
4304 * @param message message
4305 * @param peer peer identity this notification is about
4307 * @return GNUNET_OK to keep the connection open,
4308 * GNUNET_SYSERR to close it (signal serious error)
4311 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4312 const struct GNUNET_MessageHeader *message)
4314 struct GNUNET_MESH_Poll *msg;
4315 struct MeshTunnel *t;
4316 struct MeshFlowControl *fc;
4321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
4324 msg = (struct GNUNET_MESH_Poll *) message;
4326 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4330 /* TODO notify that we dont know this tunnel (whom)? */
4331 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
4332 GNUNET_break_op (0);
4336 /* Is this a forward or backward ACK? */
4337 id = GNUNET_PEER_search (peer);
4338 pid = ntohl (msg->pid);
4339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid);
4340 if (t->next_hop == id)
4342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
4344 old = fc->last_pid_recv;
4346 else if (t->prev_hop == id)
4348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
4350 old = fc->last_pid_recv;
4358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", fc->last_pid_recv);
4359 fc->last_pid_recv = pid;
4360 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, t->prev_hop == id);
4362 if (GNUNET_YES == t->reliable)
4363 fc->last_pid_recv = old;
4370 * Core handler for mesh keepalives.
4372 * @param cls closure
4373 * @param message message
4374 * @param peer peer identity this notification is about
4375 * @return GNUNET_OK to keep the connection open,
4376 * GNUNET_SYSERR to close it (signal serious error)
4378 * TODO: Check who we got this from, to validate route.
4381 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4382 const struct GNUNET_MessageHeader *message)
4384 struct GNUNET_MESH_TunnelKeepAlive *msg;
4385 struct MeshTunnel *t;
4387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
4390 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
4391 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4395 /* TODO notify that we dont know that tunnel */
4396 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
4401 tunnel_reset_timeout (t, GNUNET_YES); // FIXME
4402 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4405 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
4406 send_prebuilt_message (message, t->next_hop, t);
4413 * Functions to handle messages from core
4415 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4416 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
4417 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
4418 sizeof (struct GNUNET_MESH_PathBroken)},
4419 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
4420 sizeof (struct GNUNET_MESH_TunnelDestroy)},
4421 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4422 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4423 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
4424 sizeof (struct GNUNET_MESH_DataACK)},
4425 {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
4426 sizeof (struct GNUNET_MESH_DataACK)},
4427 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
4428 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
4429 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
4430 sizeof (struct GNUNET_MESH_ACK)},
4431 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
4432 sizeof (struct GNUNET_MESH_Poll)},
4433 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4434 sizeof (struct GNUNET_MESH_PathACK)},
4440 /******************************************************************************/
4441 /**************** MESH LOCAL HANDLER HELPERS ***********************/
4442 /******************************************************************************/
4447 * notify_client_connection_failure: notify a client that the connection to the
4448 * requested remote peer is not possible (for instance, no route found)
4449 * Function called when the socket is ready to queue more data. "buf" will be
4450 * NULL and "size" zero if the socket was closed for writing in the meantime.
4452 * @param cls closure
4453 * @param size number of bytes available in buf
4454 * @param buf where the callee should write the message
4455 * @return number of bytes written to buf
4458 notify_client_connection_failure (void *cls, size_t size, void *buf)
4461 struct MeshPeerInfo *peer_info;
4462 struct GNUNET_MESH_PeerControl *msg;
4463 struct GNUNET_PeerIdentity id;
4465 if (0 == size && NULL == buf)
4467 // TODO retry? cancel?
4471 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
4472 peer_info = (struct MeshPeerInfo *) cls;
4473 msg = (struct GNUNET_MESH_PeerControl *) buf;
4474 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
4475 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
4476 // msg->tunnel_id = htonl(peer_info->t->tid);
4477 GNUNET_PEER_resolve (peer_info->id, &id);
4478 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
4486 * Send keepalive packets for a tunnel.
4488 * @param cls Closure (tunnel for which to send the keepalive).
4489 * @param tc Notification context.
4491 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
4494 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4496 struct MeshTunnel *t = cls;
4497 struct GNUNET_MESH_TunnelKeepAlive *msg;
4498 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
4501 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
4502 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
4503 NULL == t->owner || 0 == t->local_tid)
4508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4509 "sending keepalive for tunnel %d\n", t->id.tid);
4511 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
4512 msg->header.size = htons (size);
4513 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
4514 msg->oid = my_full_id;
4515 msg->tid = htonl (t->id.tid);
4516 send_prebuilt_message (&msg->header, t->next_hop, t);
4518 t->maintenance_task =
4519 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
4524 * Function to process paths received for a new peer addition. The recorded
4525 * paths form the initial tunnel, which can be optimized later.
4526 * Called on each result obtained for the DHT search.
4528 * @param cls closure
4529 * @param exp when will this value expire
4530 * @param key key of the result
4531 * @param get_path path of the get request
4532 * @param get_path_length lenght of get_path
4533 * @param put_path path of the put request
4534 * @param put_path_length length of the put_path
4535 * @param type type of the result
4536 * @param size number of bytes in data
4537 * @param data pointer to the result data
4540 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
4541 const struct GNUNET_HashCode * key,
4542 const struct GNUNET_PeerIdentity *get_path,
4543 unsigned int get_path_length,
4544 const struct GNUNET_PeerIdentity *put_path,
4545 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
4546 size_t size, const void *data)
4548 struct MeshPeerInfo *peer = cls;
4549 struct MeshPeerPath *p;
4550 struct GNUNET_PeerIdentity pi;
4553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
4554 GNUNET_PEER_resolve (peer->id, &pi);
4555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
4557 p = path_build_from_dht (get_path, get_path_length,
4558 put_path, put_path_length);
4559 path_add_to_peers (p, GNUNET_NO);
4561 for (i = 0; i < peer->ntunnels; i++)
4563 struct GNUNET_PeerIdentity id;
4565 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
4566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
4567 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
4568 peer->tunnels[i]->local_tid,
4569 peer->tunnels[i]->local_tid_dest);
4570 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
4572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
4573 peer_connect (peer, peer->tunnels[i]);
4581 /******************************************************************************/
4582 /********************* MESH LOCAL HANDLES **************************/
4583 /******************************************************************************/
4587 * Handler for client connection.
4589 * @param cls Closure (unused).
4590 * @param client Client handler.
4593 handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
4595 struct MeshClient *c;
4599 c = GNUNET_malloc (sizeof (struct MeshClient));
4601 GNUNET_SERVER_client_keep (client);
4602 GNUNET_SERVER_client_set_user_context (client, c);
4603 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4608 * Handler for client disconnection
4610 * @param cls closure
4611 * @param client identification of the client; NULL
4612 * for the last call when the server is destroyed
4615 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4617 struct MeshClient *c;
4618 struct MeshClient *next;
4620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4627 c = client_get (client);
4630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4632 GNUNET_SERVER_client_drop (c->handle);
4633 c->shutting_down = GNUNET_YES;
4634 if (NULL != c->own_tunnels)
4636 GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
4637 &tunnel_destroy_iterator, c);
4638 GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
4641 if (NULL != c->incoming_tunnels)
4643 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
4644 &tunnel_destroy_iterator, c);
4645 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
4648 if (NULL != c->ports)
4649 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
4651 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4654 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4659 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
4661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4667 * Handler for new clients
4669 * @param cls closure
4670 * @param client identification of the client
4671 * @param message the actual message, which includes messages the client wants
4674 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4675 const struct GNUNET_MessageHeader *message)
4677 struct GNUNET_MESH_ClientConnect *cc_msg;
4678 struct MeshClient *c;
4683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4685 /* Check data sanity */
4686 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4687 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4688 if (0 != (size % sizeof (uint32_t)))
4691 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4694 size /= sizeof (uint32_t);
4696 /* Initialize new client structure */
4697 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
4698 c->id = next_client_id++; /* overflow not important: just for debug */
4699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4705 p = (uint32_t *) &cc_msg[1];
4706 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
4707 for (i = 0; i < size; i++)
4710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4712 /* store in client's hashmap */
4713 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
4714 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4715 /* store in global hashmap */
4716 /* FIXME only allow one client to have the port open,
4717 * have a backup hashmap with waiting clients */
4718 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
4719 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4723 c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4724 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
4725 GNUNET_SERVER_notification_context_add (nc, client);
4726 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4728 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4734 * Handler for requests of new tunnels
4736 * @param cls Closure.
4737 * @param client Identification of the client.
4738 * @param message The actual message.
4741 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4742 const struct GNUNET_MessageHeader *message)
4744 struct GNUNET_MESH_TunnelMessage *t_msg;
4745 struct MeshPeerInfo *peer_info;
4746 struct MeshTunnel *t;
4747 struct MeshClient *c;
4748 MESH_TunnelNumber tid;
4750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4752 /* Sanity check for client registration */
4753 if (NULL == (c = client_get (client)))
4756 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4761 /* Message size sanity check */
4762 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4765 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4769 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4771 GNUNET_i2s (&t_msg->peer));
4772 /* Sanity check for tunnel numbering */
4773 tid = ntohl (t_msg->tunnel_id);
4774 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4777 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4780 /* Sanity check for duplicate tunnel IDs */
4781 if (NULL != tunnel_get_by_local_id (c, tid))
4784 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4789 while (NULL != tunnel_get_by_pi (myid, next_tid))
4790 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4791 t = tunnel_new (myid, next_tid, c, tid);
4792 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4796 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4799 t->port = ntohl (t_msg->port);
4800 tunnel_set_options (t, ntohl (t_msg->opt));
4801 if (GNUNET_YES == t->reliable)
4803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
4804 t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
4806 t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
4809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4810 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4812 peer_info = peer_get (&t_msg->peer);
4813 peer_info_add_tunnel (peer_info, t);
4814 peer_connect (peer_info, t);
4815 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4821 * Handler for requests of deleting tunnels
4823 * @param cls closure
4824 * @param client identification of the client
4825 * @param message the actual message
4828 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4829 const struct GNUNET_MessageHeader *message)
4831 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4832 struct MeshClient *c;
4833 struct MeshTunnel *t;
4834 MESH_TunnelNumber tid;
4836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4837 "Got a DESTROY TUNNEL from client!\n");
4839 /* Sanity check for client registration */
4840 if (NULL == (c = client_get (client)))
4843 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4848 /* Message sanity check */
4849 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4856 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4858 /* Retrieve tunnel */
4859 tid = ntohl (tunnel_msg->tunnel_id);
4860 t = tunnel_get_by_local_id(c, tid);
4863 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4865 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4869 /* Cleanup after the tunnel */
4870 client_delete_tunnel (c, t);
4877 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4881 /* The tunnel will be destroyed when the last message is transmitted. */
4882 tunnel_destroy_empty (t);
4884 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4890 * Handler for client traffic
4892 * @param cls closure
4893 * @param client identification of the client
4894 * @param message the actual message
4897 handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
4898 const struct GNUNET_MessageHeader *message)
4900 struct GNUNET_MESH_LocalData *data_msg;
4901 struct MeshClient *c;
4902 struct MeshTunnel *t;
4903 struct MeshFlowControl *fc;
4904 MESH_TunnelNumber tid;
4907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4908 "Got data from a client!\n");
4910 /* Sanity check for client registration */
4911 if (NULL == (c = client_get (client)))
4914 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4919 data_msg = (struct GNUNET_MESH_LocalData *) message;
4921 /* Sanity check for message size */
4922 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
4923 if (size < sizeof (struct GNUNET_MessageHeader))
4926 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4930 /* Tunnel exists? */
4931 tid = ntohl (data_msg->tid);
4932 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)
4935 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4938 t = tunnel_get_by_local_id (c, tid);
4942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4946 /* Is the client in the tunnel? */
4947 if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4949 t->owner->handle == client)
4951 (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
4953 t->client->handle == client) ) )
4956 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4960 /* Ok, everything is correct, send the message
4961 * (pretend we got it from a mesh peer)
4964 struct GNUNET_MESH_Data *payload;
4965 char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
4967 fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
4968 if (GNUNET_YES == t->reliable)
4970 struct MeshTunnelReliability *rel;
4971 struct MeshReliableMessage *copy;
4973 rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
4974 copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
4975 + sizeof(struct GNUNET_MESH_Data)
4977 copy->mid = rel->mid_sent++;
4978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid);
4979 copy->timestamp = GNUNET_TIME_absolute_get ();
4982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
4983 GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
4984 if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
4987 GNUNET_TIME_relative_multiply (rel->expected_delay,
4988 MESH_RETRANSMIT_MARGIN);
4990 GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
4991 &tunnel_retransmit_message,
4994 payload = (struct GNUNET_MESH_Data *) ©[1];
4995 payload->mid = htonl (copy->mid);
4999 payload = (struct GNUNET_MESH_Data *) cbuf;
5000 payload->mid = htonl (fc->last_pid_recv + 1);
5002 memcpy (&payload[1], &data_msg[1], size);
5003 payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
5004 payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
5005 GNUNET_MESSAGE_TYPE_MESH_UNICAST :
5006 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5007 GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
5008 payload->tid = htonl (t->id.tid);
5009 payload->ttl = htonl (default_ttl);
5010 payload->pid = htonl (fc->last_pid_recv + 1);
5011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5012 " calling generic handler...\n");
5013 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5014 handle_mesh_unicast (NULL, &my_full_id, &payload->header);
5016 handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
5018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5019 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5026 * Handler for client's ACKs for payload traffic.
5028 * @param cls Closure (unused).
5029 * @param client Identification of the client.
5030 * @param message The actual message.
5033 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5034 const struct GNUNET_MessageHeader *message)
5036 struct GNUNET_MESH_LocalAck *msg;
5037 struct MeshTunnel *t;
5038 struct MeshClient *c;
5039 MESH_TunnelNumber tid;
5041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
5042 /* Sanity check for client registration */
5043 if (NULL == (c = client_get (client)))
5046 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5051 msg = (struct GNUNET_MESH_LocalAck *) message;
5053 /* Tunnel exists? */
5054 tid = ntohl (msg->tunnel_id);
5055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5056 t = tunnel_get_by_local_id (c, tid);
5060 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5061 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5062 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5066 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5067 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5069 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5070 t->prev_fc.last_ack_recv++;
5071 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_NO);
5075 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5076 t->next_fc.last_ack_recv++;
5077 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_YES);
5080 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5088 * Iterator over all tunnels to send a monitoring client info about each tunnel.
5090 * @param cls Closure (client handle).
5091 * @param key Key (hashed tunnel ID, unused).
5092 * @param value Tunnel info.
5094 * @return GNUNET_YES, to keep iterating.
5097 monitor_all_tunnels_iterator (void *cls,
5098 const struct GNUNET_HashCode * key,
5101 struct GNUNET_SERVER_Client *client = cls;
5102 struct MeshTunnel *t = value;
5103 struct GNUNET_MESH_LocalMonitor *msg;
5105 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
5106 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
5107 msg->tunnel_id = htonl (t->id.tid);
5108 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5109 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
5110 GNUNET_PEER_resolve (t->dest, &msg->destination);
5112 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5113 "* sending info about tunnel %s [%u]\n",
5114 GNUNET_i2s (&msg->owner), t->id.tid);
5116 GNUNET_SERVER_notification_context_unicast (nc, client,
5117 &msg->header, GNUNET_NO);
5123 * Handler for client's MONITOR request.
5125 * @param cls Closure (unused).
5126 * @param client Identification of the client.
5127 * @param message The actual message.
5130 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
5131 const struct GNUNET_MessageHeader *message)
5133 struct MeshClient *c;
5135 /* Sanity check for client registration */
5136 if (NULL == (c = client_get (client)))
5139 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5143 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5144 "Received get tunnels request from client %u\n",
5146 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
5147 monitor_all_tunnels_iterator,
5149 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5150 "Get tunnels request from client %u completed\n",
5152 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5157 * Handler for client's MONITOR_TUNNEL request.
5159 * @param cls Closure (unused).
5160 * @param client Identification of the client.
5161 * @param message The actual message.
5164 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
5165 const struct GNUNET_MessageHeader *message)
5167 const struct GNUNET_MESH_LocalMonitor *msg;
5168 struct GNUNET_MESH_LocalMonitor *resp;
5169 struct MeshClient *c;
5170 struct MeshTunnel *t;
5172 /* Sanity check for client registration */
5173 if (NULL == (c = client_get (client)))
5176 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5180 msg = (struct GNUNET_MESH_LocalMonitor *) message;
5181 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5182 "Received tunnel info request from client %u for tunnel %s[%X]\n",
5185 ntohl (msg->tunnel_id));
5186 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
5189 /* We don't know the tunnel FIXME */
5190 struct GNUNET_MESH_LocalMonitor warn;
5193 GNUNET_SERVER_notification_context_unicast (nc, client,
5196 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5200 /* Initialize context */
5201 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
5203 GNUNET_PEER_resolve (t->dest, &resp->destination);
5204 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
5205 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
5206 &resp->header, GNUNET_NO);
5209 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5210 "Monitor tunnel request from client %u completed\n",
5212 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5217 * Functions to handle messages from clients
5219 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
5220 {&handle_local_new_client, NULL,
5221 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
5222 {&handle_local_tunnel_create, NULL,
5223 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
5224 sizeof (struct GNUNET_MESH_TunnelMessage)},
5225 {&handle_local_tunnel_destroy, NULL,
5226 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
5227 sizeof (struct GNUNET_MESH_TunnelMessage)},
5228 {&handle_local_data, NULL,
5229 GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
5230 {&handle_local_ack, NULL,
5231 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
5232 sizeof (struct GNUNET_MESH_LocalAck)},
5233 {&handle_local_get_tunnels, NULL,
5234 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
5235 sizeof (struct GNUNET_MessageHeader)},
5236 {&handle_local_show_tunnel, NULL,
5237 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
5238 sizeof (struct GNUNET_MESH_LocalMonitor)},
5244 * Method called whenever a given peer connects.
5246 * @param cls closure
5247 * @param peer peer identity this notification is about
5250 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
5252 struct MeshPeerInfo *peer_info;
5253 struct MeshPeerPath *path;
5255 DEBUG_CONN ("Peer connected\n");
5256 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
5257 peer_info = peer_get (peer);
5258 if (myid == peer_info->id)
5260 DEBUG_CONN (" (self)\n");
5261 path = path_new (1);
5265 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
5266 path = path_new (2);
5267 path->peers[1] = peer_info->id;
5268 GNUNET_PEER_change_rc (peer_info->id, 1);
5269 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
5271 path->peers[0] = myid;
5272 GNUNET_PEER_change_rc (myid, 1);
5273 peer_info_add_path (peer_info, path, GNUNET_YES);
5279 * Method called whenever a peer disconnects.
5281 * @param cls closure
5282 * @param peer peer identity this notification is about
5285 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
5287 struct MeshPeerInfo *pi;
5288 struct MeshPeerQueue *q;
5289 struct MeshPeerQueue *n;
5291 DEBUG_CONN ("Peer disconnected\n");
5292 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5302 /* TODO try to reroute this traffic instead */
5303 queue_destroy(q, GNUNET_YES);
5306 if (NULL != pi->core_transmit)
5308 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
5309 pi->core_transmit = NULL;
5311 peer_remove_path (pi, pi->id, myid);
5314 DEBUG_CONN (" (self)\n");
5316 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
5322 * Install server (service) handlers and start listening to clients.
5327 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
5328 GNUNET_SERVER_connect_notify (server_handle,
5329 &handle_local_client_connect, NULL);
5330 GNUNET_SERVER_disconnect_notify (server_handle,
5331 &handle_local_client_disconnect, NULL);
5332 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
5334 clients_head = NULL;
5335 clients_tail = NULL;
5337 GNUNET_SERVER_resume (server_handle);
5342 * To be called on core init/fail.
5344 * @param cls Closure (config)
5345 * @param server handle to the server for this service
5346 * @param identity the public identity of this peer
5349 core_init (void *cls, struct GNUNET_CORE_Handle *server,
5350 const struct GNUNET_PeerIdentity *identity)
5352 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
5356 GNUNET_break (core_handle == server);
5357 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
5360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
5361 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5363 GNUNET_i2s (identity));
5364 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5366 GNUNET_i2s (&my_full_id));
5367 GNUNET_CORE_disconnect (core_handle);
5368 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5369 NULL, /* Closure passed to MESH functions */
5370 &core_init, /* Call core_init once connected */
5371 &core_connect, /* Handle connects */
5372 &core_disconnect, /* remove peers on disconnects */
5373 NULL, /* Don't notify about all incoming messages */
5374 GNUNET_NO, /* For header only in notification */
5375 NULL, /* Don't notify about all outbound messages */
5376 GNUNET_NO, /* For header-only out notification */
5377 core_handlers); /* Register these handlers */
5386 /******************************************************************************/
5387 /************************ MAIN FUNCTIONS ****************************/
5388 /******************************************************************************/
5391 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
5393 * @param cls closure
5394 * @param key current key code
5395 * @param value value in the hash map
5396 * @return GNUNET_YES if we should continue to iterate,
5400 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
5402 struct MeshTunnel *t = value;
5409 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
5411 * @param cls closure
5412 * @param key current key code
5413 * @param value value in the hash map
5414 * @return GNUNET_YES if we should continue to iterate,
5418 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
5420 struct MeshPeerInfo *p = value;
5421 struct MeshPeerQueue *q;
5422 struct MeshPeerQueue *n;
5430 queue_destroy(q, GNUNET_YES);
5434 peer_info_destroy (p);
5440 * Task run during shutdown.
5446 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5450 if (core_handle != NULL)
5452 GNUNET_CORE_disconnect (core_handle);
5455 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5456 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5457 if (dht_handle != NULL)
5459 GNUNET_DHT_disconnect (dht_handle);
5464 GNUNET_SERVER_notification_context_destroy (nc);
5467 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5469 GNUNET_SCHEDULER_cancel (announce_id_task);
5470 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5477 * Process mesh requests.
5479 * @param cls closure
5480 * @param server the initialized server
5481 * @param c configuration to use
5484 run (void *cls, struct GNUNET_SERVER_Handle *server,
5485 const struct GNUNET_CONFIGURATION_Handle *c)
5488 struct GNUNET_CRYPTO_EccPrivateKey *pk;
5490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5491 server_handle = server;
5492 GNUNET_SERVER_suspend (server_handle);
5495 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5500 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5501 "mesh", "peer/privatekey");
5502 GNUNET_SCHEDULER_shutdown ();
5507 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5508 &refresh_path_time))
5510 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5512 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5513 "mesh", "refresh path time");
5514 GNUNET_SCHEDULER_shutdown ();
5519 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5522 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5524 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5525 "mesh", "id announce time");
5526 GNUNET_SCHEDULER_shutdown ();
5531 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5534 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5536 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5537 "mesh", "connect timeout");
5538 GNUNET_SCHEDULER_shutdown ();
5543 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5546 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5548 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5549 "mesh", "max msgs queue");
5550 GNUNET_SCHEDULER_shutdown ();
5555 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5558 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5560 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5561 "mesh", "max tunnels");
5562 GNUNET_SCHEDULER_shutdown ();
5567 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5570 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5572 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5573 "mesh", "default ttl", 64);
5578 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5581 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5582 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5583 "mesh", "max peers", 1000);
5588 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5595 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5596 "Mesh is running with drop mode enabled. "
5597 "This is NOT a good idea! "
5598 "Remove the DROP_PERCENT option from your configuration.\n");
5602 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5603 &dht_replication_level))
5605 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5607 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5608 "mesh", "dht replication level", 3);
5609 dht_replication_level = 3;
5612 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5613 incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
5614 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5615 ports = GNUNET_CONTAINER_multihashmap32_create (32);
5617 dht_handle = GNUNET_DHT_connect (c, 64);
5618 if (NULL == dht_handle)
5622 stats = GNUNET_STATISTICS_create ("mesh", c);
5624 /* Scheduled the task to clean up when shutdown is called */
5625 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5627 pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
5628 GNUNET_free (keyfile);
5629 GNUNET_assert (NULL != pk);
5630 my_private_key = pk;
5631 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5632 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5633 &my_full_id.hashPubKey);
5634 myid = GNUNET_PEER_intern (&my_full_id);
5635 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5636 "Mesh for peer [%s] starting\n",
5637 GNUNET_i2s(&my_full_id));
5639 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5640 NULL, /* Closure passed to MESH functions */
5641 &core_init, /* Call core_init once connected */
5642 &core_connect, /* Handle connects */
5643 &core_disconnect, /* remove peers on disconnects */
5644 NULL, /* Don't notify about all incoming messages */
5645 GNUNET_NO, /* For header only in notification */
5646 NULL, /* Don't notify about all outbound messages */
5647 GNUNET_NO, /* For header-only out notification */
5648 core_handlers); /* Register these handlers */
5649 if (NULL == core_handle)
5652 GNUNET_SCHEDULER_shutdown ();
5656 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5657 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5663 * The main function for the mesh service.
5665 * @param argc number of arguments from the command line
5666 * @param argv command line arguments
5667 * @return 0 ok, 1 on error
5670 main (int argc, char *const *argv)
5675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5676 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5678 ret = (GNUNET_OK == r) ? 0 : 1;
5679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");