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-new.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
27 * - relay corking down to core
28 * - set ttl relative to path length
37 #include "mesh2_protocol.h"
38 #include "mesh_path.h"
39 #include "block_mesh.h"
40 #include "gnunet_dht_service.h"
41 #include "gnunet_statistics_service.h"
43 #define MESH_BLOOM_SIZE 128
45 #define MESH_DEBUG_DHT GNUNET_NO
46 #define MESH_DEBUG_CONNECTION GNUNET_NO
47 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
49 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
50 GNUNET_TIME_UNIT_MINUTES,\
53 #if MESH_DEBUG_CONNECTION
54 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
56 #define DEBUG_CONN(...)
60 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
62 #define DEBUG_DHT(...)
69 struct timespec __mesh_start;
70 struct timespec __mesh_end;
71 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
72 #define INTERVAL_END \
74 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
75 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
76 if (__diff < 0) __diff += 1000000000;\
80 #define INTERVAL_SHOW \
82 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
84 #define INTERVAL_START
89 /******************************************************************************/
90 /************************ DATA STRUCTURES ****************************/
91 /******************************************************************************/
93 /** FWD declaration */
99 * Struct containing info about a queued transmission to this peer
106 struct MeshPeerQueue *next;
111 struct MeshPeerQueue *prev;
114 * Peer this transmission is directed to.
116 struct MeshPeerInfo *peer;
119 * Tunnel this message belongs to.
121 struct MeshTunnel *tunnel;
124 * Pointer to info stucture used as cls.
134 * Size of the message
141 * Struct containing all information regarding a given peer
151 * Last time we heard from this peer
153 struct GNUNET_TIME_Absolute last_contact;
156 * Number of attempts to reconnect so far
158 int n_reconnect_attempts;
161 * Paths to reach the peer, ordered by ascending hop count
163 struct MeshPeerPath *path_head;
166 * Paths to reach the peer, ordered by ascending hop count
168 struct MeshPeerPath *path_tail;
171 * Handle to stop the DHT search for a path to this peer
173 struct GNUNET_DHT_GetHandle *dhtget;
176 * Array of tunnels this peer is the target of.
177 * Most probably a small amount, therefore not a hashmap.
178 * When the path to the peer changes, notify these tunnels to let them
179 * re-adjust their path trees.
181 struct MeshTunnel **tunnels;
184 * Number of tunnels this peers participates in
186 unsigned int ntunnels;
189 * Transmission queue to core DLL head
191 struct MeshPeerQueue *queue_head;
194 * Transmission queue to core DLL tail
196 struct MeshPeerQueue *queue_tail;
199 * How many messages are in the queue to this peer.
201 unsigned int queue_n;
204 * Handle for queued transmissions
206 struct GNUNET_CORE_TransmitHandle *core_transmit;
211 * Struct to encapsulate all the Flow Control information to a peer in the
212 * context of a tunnel: Same peer in different tunnels will have independent
213 * flow control structures, allowing to choke/free tunnels according to its
216 struct MeshFlowControl
219 * ID of the last packet sent towards the peer.
221 uint32_t last_pid_sent;
224 * ID of the last packet received from the peer.
226 uint32_t last_pid_recv;
229 * Last ACK sent to the peer (peer can't send more than this PID).
231 uint32_t last_ack_sent;
234 * Last ACK sent towards the origin (for traffic towards leaf node).
236 uint32_t last_ack_recv;
239 * How many messages are in the queue towards this peer.
244 * Task to poll the peer in case of a lost ACK causes stall.
246 GNUNET_SCHEDULER_TaskIdentifier poll_task;
249 * How frequently to poll for ACKs.
251 struct GNUNET_TIME_Relative poll_time;
254 * On which tunnel to poll.
255 * Using an explicit poll_ctx would not help memory wise,
256 * since the allocated context would have to be stored in the
257 * fc struct in order to free it upon cancelling poll_task.
259 struct MeshTunnel *t;
264 * Globally unique tunnel identification (owner + number)
265 * DO NOT USE OVER THE NETWORK
270 * Node that owns the tunnel
275 * Tunnel number to differentiate all the tunnels owned by the node oid
276 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
278 MESH_TunnelNumber tid;
283 * Struct containing all information regarding a tunnel
284 * For an intermediate node the improtant info used will be:
285 * - id Tunnel unique identification
286 * - paths[0] To know where to send it next
287 * - metainfo: ready, speeds, accounting
294 struct MESH_TunnelID id;
297 * State of the tunnel.
299 enum MeshTunnelState state;
302 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
304 MESH_TunnelNumber local_tid;
307 * Local tunnel number for local destination clients (incoming number)
308 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
311 MESH_TunnelNumber local_tid_dest;
314 * Is the tunnel bufferless (minimum latency)?
319 * Force sending ACK? Flag to allow duplicate ACK on POLL.
324 * How many messages do we accept in the forward queue.
326 unsigned int queue_max;
329 * Last time the tunnel was used
331 struct GNUNET_TIME_Absolute timestamp;
334 * Destination of the tunnel.
339 * Next hop in the tunnel. If 0, @c client must be set.
341 GNUNET_PEER_Id next_hop;
344 * Previous hop in the tunnel. If 0, @c owner must be set.
346 GNUNET_PEER_Id prev_hop;
349 * Flow control information about @c next_hop or @c client.
351 struct MeshFlowControl next_fc;
354 * Flow control information about @c prev_hop or @c owner.
356 struct MeshFlowControl prev_fc;
359 * Client owner of the tunnel, if any
361 struct MeshClient *owner;
364 * Client destination of the tunnel, if any.
366 struct MeshClient *client;
369 * Task to keep the used paths alive at the owner,
370 * time tunnel out on all the other peers.
372 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
375 * Path being used for the tunnel.
377 struct MeshPeerPath *path;
380 * Flag to signal the destruction of the tunnel.
381 * If this is set GNUNET_YES the tunnel will be destroyed
382 * when the queue is empty.
387 * Total messages pending for this tunnels, payload or not.
389 unsigned int pending_messages;
394 * Struct containing information about a client of the service
396 * TODO: add a list of 'waiting' types
403 struct MeshClient *next;
408 struct MeshClient *prev;
411 * Tunnels that belong to this client, indexed by local id
413 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
416 * Tunnels this client has accepted, indexed by incoming local id
418 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
421 * Handle to communicate with the client
423 struct GNUNET_SERVER_Client *handle;
426 * Messages that this client has declared interest in.
427 * Indexed by a GMC_hash32 (type), contains *Client.
429 struct GNUNET_CONTAINER_MultiHashMap *types;
432 * Whether the client is active or shutting down (don't send confirmations
433 * to a client that is shutting down.
438 * ID of the client, mainly for debug messages
445 /******************************************************************************/
446 /************************ DEBUG FUNCTIONS ****************************/
447 /******************************************************************************/
451 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
452 * @param cls string to print
453 * @param success GNUNET_OK if the PUT was transmitted,
454 * GNUNET_NO on timeout,
455 * GNUNET_SYSERR on disconnect from service
456 * after the PUT message was transmitted
457 * (so we don't know if it was received or not)
462 mesh_debug (void *cls, int success)
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
470 unsigned int debug_fwd_ack;
471 unsigned int debug_bck_ack;
475 /******************************************************************************/
476 /*********************** GLOBAL VARIABLES ****************************/
477 /******************************************************************************/
479 /************************** Configuration parameters **************************/
482 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
484 static struct GNUNET_TIME_Relative refresh_path_time;
487 * How often to PUT own ID in the DHT.
489 static struct GNUNET_TIME_Relative id_announce_time;
492 * Maximum time allowed to connect to a peer found by string.
494 static struct GNUNET_TIME_Relative connect_timeout;
497 * Default TTL for payload packets.
499 static unsigned long long default_ttl;
502 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
504 static unsigned long long dht_replication_level;
507 * How many tunnels are we willing to maintain.
508 * Local tunnels are always allowed, even if there are more tunnels than max.
510 static unsigned long long max_tunnels;
513 * How many messages *in total* are we willing to queue, divided by number of
514 * tunnels to get tunnel queue size.
516 static unsigned long long max_msgs_queue;
519 * How many peers do we want to remember?
521 static unsigned long long max_peers;
524 /*************************** Static global variables **************************/
527 * Hostkey generation context
529 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
532 * DLL with all the clients, head.
534 static struct MeshClient *clients_head;
537 * DLL with all the clients, tail.
539 static struct MeshClient *clients_tail;
542 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
544 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
547 * Number of tunnels known.
549 static unsigned long long n_tunnels;
552 * Tunnels incoming, indexed by MESH_TunnelNumber
553 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
555 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
558 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
560 static struct GNUNET_CONTAINER_MultiHashMap *peers;
563 * Handle to communicate with transport
565 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
568 * Handle to communicate with core.
570 static struct GNUNET_CORE_Handle *core_handle;
575 static struct GNUNET_DHT_Handle *dht_handle;
580 static struct GNUNET_SERVER_Handle *server_handle;
583 * Handle to the statistics service.
585 static struct GNUNET_STATISTICS_Handle *stats;
588 * Notification context, to send messages to local clients.
590 static struct GNUNET_SERVER_NotificationContext *nc;
593 * Local peer own ID (memory efficient handle).
595 static GNUNET_PEER_Id myid;
598 * Local peer own ID (full value).
600 static struct GNUNET_PeerIdentity my_full_id;
605 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
610 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
613 * Tunnel ID for the next created tunnel (global tunnel number).
615 static MESH_TunnelNumber next_tid;
618 * Tunnel ID for the next incoming tunnel (local tunnel number).
620 static MESH_TunnelNumber next_local_tid;
623 * All message types clients of this peer are interested in.
625 static struct GNUNET_CONTAINER_MultiHashMap *types;
628 * Task to periodically announce itself in the network.
630 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
633 * Next ID to assign to a client.
635 unsigned int next_client_id;
638 /******************************************************************************/
639 /*********************** DECLARATIONS **************************/
640 /******************************************************************************/
643 * Function to process paths received for a new peer addition. The recorded
644 * paths form the initial tunnel, which can be optimized later.
645 * Called on each result obtained for the DHT search.
648 * @param exp when will this value expire
649 * @param key key of the result
650 * @param type type of the result
651 * @param size number of bytes in data
652 * @param data pointer to the result data
655 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
656 const struct GNUNET_HashCode * key,
657 const struct GNUNET_PeerIdentity *get_path,
658 unsigned int get_path_length,
659 const struct GNUNET_PeerIdentity *put_path,
660 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
661 size_t size, const void *data);
665 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
666 * and insert it in the appropiate structures if the peer is not known yet.
668 * @param peer Full identity of the peer.
670 * @return Existing or newly created peer info.
672 static struct MeshPeerInfo *
673 peer_get (const struct GNUNET_PeerIdentity *peer);
677 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
678 * and insert it in the appropiate structures if the peer is not known yet.
680 * @param peer Short identity of the peer.
682 * @return Existing or newly created peer info.
684 static struct MeshPeerInfo *
685 peer_get_short (const GNUNET_PEER_Id peer);
689 * Build a PeerPath from the paths returned from the DHT, reversing the paths
690 * to obtain a local peer -> destination path and interning the peer ids.
692 * @return Newly allocated and created path
694 static struct MeshPeerPath *
695 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
696 unsigned int get_path_length,
697 const struct GNUNET_PeerIdentity *put_path,
698 unsigned int put_path_length);
702 * Adds a path to the peer_infos of all the peers in the path
704 * @param p Path to process.
705 * @param confirmed Whether we know if the path works or not.
708 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
713 * Search for a tunnel by global ID using full PeerIdentities.
715 * @param oid owner of the tunnel.
716 * @param tid global tunnel number.
718 * @return tunnel handler, NULL if doesn't exist.
720 static struct MeshTunnel *
721 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
725 * Notify a tunnel that a connection has broken that affects at least
728 * @param t Tunnel affected.
729 * @param p1 Peer that got disconnected from p2.
730 * @param p2 Peer that got disconnected from p1.
732 * @return Short ID of the peer disconnected (either p1 or p2).
733 * 0 if the tunnel remained unaffected.
735 static GNUNET_PEER_Id
736 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
741 * Use the given path for the tunnel.
743 * @param t Tunnel to update.
744 * @param p Path to use.
747 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
750 * @brief Queue and pass message to core when possible.
752 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
753 * and accounts for it. In case the queue is full, the message is dropped and
756 * Otherwise, message is treated as internal and allowed to go regardless of
759 * @param cls Closure (@c type dependant). It will be used by queue_send to
760 * build the message to be sent if not already prebuilt.
761 * @param type Type of the message, 0 for a raw message.
762 * @param size Size of the message.
763 * @param dst Neighbor to send message to.
764 * @param t Tunnel this message belongs to.
767 queue_add (void *cls, uint16_t type, size_t size,
768 struct MeshPeerInfo *dst, struct MeshTunnel *t);
772 * Free a transmission that was already queued with all resources
773 * associated to the request.
775 * @param queue Queue handler to cancel.
776 * @param clear_cls Is it necessary to free associated cls?
779 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
783 * @brief Get the next transmittable message from the queue.
785 * This will be the head, except in the case of being a data packet
786 * not allowed by the destination peer.
788 * @param peer Destination peer.
790 * @return The next viable MeshPeerQueue element to send to that peer.
791 * NULL when there are no transmittable messages.
793 struct MeshPeerQueue *
794 queue_get_next (const struct MeshPeerInfo *peer);
798 * Core callback to write a queued packet to core buffer
800 * @param cls Closure (peer info).
801 * @param size Number of bytes available in buf.
802 * @param buf Where the to write the message.
804 * @return number of bytes written to buf
807 queue_send (void *cls, size_t size, void *buf);
810 /******************************************************************************/
811 /************************ PERIODIC FUNCTIONS ****************************/
812 /******************************************************************************/
815 * Periodically announce self id in the DHT
818 * @param tc task context
821 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
825 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
827 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
831 * - Set data expiration in function of X
834 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
836 block.id = my_full_id;
837 block.type = htonl (0);
838 GNUNET_DHT_put (dht_handle, /* DHT handle */
839 &my_full_id.hashPubKey, /* Key to use */
840 dht_replication_level, /* Replication level */
841 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
842 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
843 sizeof (block), /* Size of the data */
844 (const char *) &block, /* Data itself */
845 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
846 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
847 NULL, /* Continuation */
848 NULL); /* Continuation closure */
850 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
854 /******************************************************************************/
855 /****************** GENERAL HELPER FUNCTIONS ************************/
856 /******************************************************************************/
860 * Check if client has registered with the service and has not disconnected
862 * @param client the client to check
864 * @return non-NULL if client exists in the global DLL
866 static struct MeshClient *
867 client_get (struct GNUNET_SERVER_Client *client)
869 struct MeshClient *c;
874 if (c->handle == client)
883 * Deletes a tunnel from a client (either owner or destination). To be used on
886 * @param c Client whose tunnel to delete.
887 * @param t Tunnel which should be deleted.
890 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
892 struct GNUNET_HashCode hash;
896 GMC_hash32 (t->local_tid, &hash);
897 GNUNET_assert (GNUNET_YES ==
898 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
902 else if (c == t->client)
904 GMC_hash32 (t->local_tid_dest, &hash);
905 GNUNET_assert (GNUNET_YES ==
906 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
918 * Notify all clients (not depending on registration status) that the incoming
919 * tunnel is no longer valid.
921 * @param t Tunnel that was destroyed.
924 send_client_tunnel_destroy (struct MeshTunnel *t)
926 struct GNUNET_MESH_TunnelMessage msg;
928 if (NULL == t->client)
930 msg.header.size = htons (sizeof (msg));
931 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
932 msg.tunnel_id = htonl (t->local_tid_dest);
933 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
934 &msg.header, GNUNET_NO);
939 * Iterator over all the peers to remove the oldest not-used entry.
941 * @param cls Closure (unsued).
942 * @param key ID of the peer.
943 * @param value Peer_Info of the peer.
948 peer_info_timeout (void *cls,
949 const struct GNUNET_HashCode *key,
956 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
957 * and insert it in the appropiate structures if the peer is not known yet.
959 * @param peer Full identity of the peer.
961 * @return Existing or newly created peer info.
963 static struct MeshPeerInfo *
964 peer_get (const struct GNUNET_PeerIdentity *peer)
966 struct MeshPeerInfo *peer_info;
968 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
969 if (NULL == peer_info)
972 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
973 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
975 GNUNET_CONTAINER_multihashmap_iterate (peers,
979 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
980 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
981 peer_info->id = GNUNET_PEER_intern (peer);
983 peer_info->last_contact = GNUNET_TIME_absolute_get();
990 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
991 * and insert it in the appropiate structures if the peer is not known yet.
993 * @param peer Short identity of the peer.
995 * @return Existing or newly created peer info.
997 static struct MeshPeerInfo *
998 peer_get_short (const GNUNET_PEER_Id peer)
1000 struct GNUNET_PeerIdentity id;
1002 GNUNET_PEER_resolve (peer, &id);
1003 return peer_get (&id);
1008 * Choose the best path towards a peer considering the tunnel properties.
1010 * @param peer The destination peer.
1011 * @param t The tunnel the path is for.
1013 * @return Best current known path towards the peer, if any.
1015 static struct MeshPeerPath *
1016 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1018 struct MeshPeerPath *best_p;
1019 struct MeshPeerPath *p;
1020 unsigned int best_cost;
1023 best_p = p = peer->path_head;
1024 best_cost = cost = p->length;
1027 if ((cost = p->length) < best_cost)
1039 * Remove the tunnel from the list of tunnels to which a peer is target.
1041 * @param peer PeerInfo of the peer.
1042 * @param t Tunnel to remove.
1045 peer_remove_tunnel (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1049 for (i = 0; i < peer->ntunnels; i++)
1052 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1055 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1057 GNUNET_realloc (peer->tunnels,
1058 peer->ntunnels * sizeof(struct MeshTunnel *));
1066 * Core callback to write a pre-constructed data packet to core buffer
1068 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1069 * @param size Number of bytes available in buf.
1070 * @param buf Where the to write the message.
1072 * @return number of bytes written to buf
1075 send_core_data_raw (void *cls, size_t size, void *buf)
1077 struct GNUNET_MessageHeader *msg = cls;
1080 GNUNET_assert (NULL != msg);
1081 total_size = ntohs (msg->size);
1083 if (total_size > size)
1088 memcpy (buf, msg, total_size);
1095 * Sends an already built message to a peer, properly registrating
1096 * all used resources.
1098 * @param message Message to send. Function makes a copy of it.
1099 * @param peer Short ID of the neighbor whom to send the message.
1100 * @param t Tunnel on which this message is transmitted.
1103 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1104 GNUNET_PEER_Id peer,
1105 struct MeshTunnel *t)
1107 struct GNUNET_PeerIdentity id;
1108 struct MeshPeerInfo *neighbor;
1109 struct MeshPeerPath *p;
1114 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1119 size = ntohs (message->size);
1120 data = GNUNET_malloc (size);
1121 memcpy (data, message, size);
1122 type = ntohs(message->type);
1125 struct GNUNET_MESH_Unicast *u;
1127 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1128 u = (struct GNUNET_MESH_Unicast *) data;
1129 u->ttl = htonl (ntohl (u->ttl) - 1);
1132 GNUNET_PEER_resolve (peer, &id);
1133 neighbor = peer_get (&id);
1134 for (p = neighbor->path_head; NULL != p; p = p->next)
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145 " %s IS NOT DIRECTLY CONNECTED\n",
1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1150 for (p = neighbor->path_head; NULL != p; p = p->next)
1152 struct GNUNET_PeerIdentity debug_id;
1155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1156 " path with %u hops through:\n",
1158 for (i = 0; i < p->length; i++)
1160 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1163 i, GNUNET_i2s(&debug_id));
1167 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1168 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1169 " no direct connection to %s\n",
1174 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1185 * Sends a CREATE PATH message for a path to a peer, properly registrating
1186 * all used resources.
1188 * @param t Tunnel for which the path is created.
1191 send_create_path (struct MeshTunnel *t)
1193 struct MeshPeerInfo *neighbor;
1195 neighbor = peer_get_short (t->next_hop);
1197 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1198 sizeof (struct GNUNET_MESH_ManipulatePath) +
1199 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1202 t->state = MESH_TUNNEL_WAITING;
1207 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1209 * @param t Tunnel which to confirm.
1212 send_path_ack (struct MeshTunnel *t)
1214 struct MeshPeerInfo *peer;
1216 if (0 == t->prev_hop)
1222 peer = peer_get_short (t->prev_hop);
1225 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1226 sizeof (struct GNUNET_MESH_PathACK),
1233 * Try to establish a new connection to this peer in the given tunnel.
1234 * If the peer doesn't have any path to it yet, try to get one.
1235 * If the peer already has some path, send a CREATE PATH towards it.
1237 * @param peer PeerInfo of the peer.
1238 * @param t Tunnel for which to create the path, if possible.
1241 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1243 struct MeshPeerPath *p;
1245 if (NULL != peer->path_head)
1247 p = peer_get_best_path (peer, t);
1248 tunnel_use_path (t, p);
1249 send_create_path (t);
1251 else if (NULL == peer->dhtget)
1253 struct GNUNET_PeerIdentity id;
1255 GNUNET_PEER_resolve (peer->id, &id);
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1257 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1258 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1259 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1260 &id.hashPubKey, /* key to search */
1261 dht_replication_level, /* replication level */
1262 GNUNET_DHT_RO_RECORD_ROUTE |
1263 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1265 0, /* xquery bits */
1266 &dht_get_id_handler, peer);
1267 t->state = MESH_TUNNEL_SEARCHING;
1269 /* Otherwise, there is no path but the DHT get is already started. */
1274 * Destroy the peer_info and free any allocated resources linked to it
1276 * @param pi The peer_info to destroy.
1278 * @return GNUNET_OK on success
1281 peer_info_destroy (struct MeshPeerInfo *pi)
1283 struct GNUNET_PeerIdentity id;
1284 struct MeshPeerPath *p;
1285 struct MeshPeerPath *nextp;
1287 GNUNET_PEER_resolve (pi->id, &id);
1288 GNUNET_PEER_change_rc (pi->id, -1);
1291 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1294 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1295 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1297 if (NULL != pi->dhtget)
1299 GNUNET_DHT_get_stop (pi->dhtget);
1305 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1315 * Remove all paths that rely on a direct connection between p1 and p2
1316 * from the peer itself and notify all tunnels about it.
1318 * @param peer PeerInfo of affected peer.
1319 * @param p1 GNUNET_PEER_Id of one peer.
1320 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1323 * TODO: optimize (see below)
1326 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1329 struct MeshPeerPath *p;
1330 struct MeshPeerPath *next;
1331 struct MeshPeerInfo *peer_d;
1333 unsigned int destroyed;
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1338 for (p = peer->path_head; NULL != p; p = next)
1341 for (i = 0; i < (p->length - 1); i++)
1343 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1344 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1346 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1356 for (i = 0; i < peer->ntunnels; i++)
1358 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1362 peer_d = peer_get_short (d);
1363 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1364 tunnel_use_path (peer->tunnels[i], next);
1365 peer_connect (peer_d, peer->tunnels[i]);
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1372 * Add the path to the peer and update the path used to reach it in case this
1375 * @param peer_info Destination peer to add the path to.
1376 * @param path New path to add. Last peer must be the peer in arg 1.
1377 * Path will be either used of freed if already known.
1378 * @param trusted Do we trust that this path is real?
1381 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1384 struct MeshPeerPath *aux;
1388 if ((NULL == peer_info) || (NULL == path))
1391 path_destroy (path);
1394 if (path->peers[path->length - 1] != peer_info->id)
1397 path_destroy (path);
1400 if (path->length <= 2 && GNUNET_NO == trusted)
1402 /* Only allow CORE to tell us about direct paths */
1403 path_destroy (path);
1406 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1407 for (l = 1; l < path->length; l++)
1409 if (path->peers[l] == myid)
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1412 for (l2 = 0; l2 < path->length - l; l2++)
1414 path->peers[l2] = path->peers[l + l2];
1419 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1424 struct GNUNET_PeerIdentity id;
1426 GNUNET_PEER_resolve (peer_info->id, &id);
1427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1428 path->length, GNUNET_i2s (&id));
1431 l = path_get_length (path);
1438 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1439 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1441 l2 = path_get_length (aux);
1444 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1445 peer_info->path_tail, aux, path);
1450 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1452 path_destroy (path);
1457 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1464 * Add the path to the origin peer and update the path used to reach it in case
1465 * this is the shortest.
1466 * The path is given in peer_info -> destination, therefore we turn the path
1467 * upside down first.
1469 * @param peer_info Peer to add the path to, being the origin of the path.
1470 * @param path New path to add after being inversed.
1471 * @param trusted Do we trust that this path is real?
1474 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1475 struct MeshPeerPath *path, int trusted)
1478 peer_info_add_path (peer_info, path, trusted);
1483 * Function called if the connection to the peer has been stalled for a while,
1484 * possibly due to a missed ACK. Poll the peer about its ACK status.
1486 * @param cls Closure (poll ctx).
1487 * @param tc TaskContext.
1490 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1492 struct MeshFlowControl *fc = cls;
1493 struct GNUNET_MESH_Poll msg;
1494 struct MeshTunnel *t = fc->t;
1495 GNUNET_PEER_Id peer;
1497 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1498 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1503 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1504 msg.header.size = htons (sizeof (msg));
1505 msg.tid = htonl (t->id.tid);
1506 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1507 msg.last_ack = htonl (fc->last_ack_recv);
1509 if (fc == &t->prev_fc)
1517 send_prebuilt_message (&msg.header, peer, t);
1518 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1519 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1525 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1526 * to obtain a local peer -> destination path and interning the peer ids.
1528 * @return Newly allocated and created path
1530 static struct MeshPeerPath *
1531 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1532 unsigned int get_path_length,
1533 const struct GNUNET_PeerIdentity *put_path,
1534 unsigned int put_path_length)
1536 struct MeshPeerPath *p;
1542 GNUNET_PEER_change_rc (myid, 1);
1543 i = get_path_length;
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1545 for (i--; i >= 0; i--)
1547 id = GNUNET_PEER_intern (&get_path[i]);
1548 if (p->length > 0 && id == p->peers[p->length - 1])
1550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1551 GNUNET_PEER_change_rc (id, -1);
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1556 GNUNET_i2s (&get_path[i]));
1558 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1559 p->peers[p->length - 1] = id;
1562 i = put_path_length;
1563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1564 for (i--; i >= 0; i--)
1566 id = GNUNET_PEER_intern (&put_path[i]);
1569 /* PUT path went through us, so discard the path up until now and start
1570 * from here to get a much shorter (and loop-free) path.
1575 if (p->length > 0 && id == p->peers[p->length - 1])
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1578 GNUNET_PEER_change_rc (id, -1);
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1583 GNUNET_i2s (&put_path[i]));
1585 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1586 p->peers[p->length - 1] = id;
1590 if (get_path_length > 0)
1591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1592 GNUNET_i2s (&get_path[0]));
1593 if (put_path_length > 0)
1594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1595 GNUNET_i2s (&put_path[0]));
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1598 for (i = 0; i < p->length; i++)
1600 struct GNUNET_PeerIdentity peer_id;
1602 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1604 GNUNET_i2s (&peer_id));
1612 * Adds a path to the peer_infos of all the peers in the path
1614 * @param p Path to process.
1615 * @param confirmed Whether we know if the path works or not.
1618 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1622 /* TODO: invert and add */
1623 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1624 for (i++; i < p->length; i++)
1626 struct MeshPeerInfo *aux;
1627 struct MeshPeerPath *copy;
1629 aux = peer_get_short (p->peers[i]);
1630 copy = path_duplicate (p);
1631 copy->length = i + 1;
1632 peer_info_add_path (aux, copy, GNUNET_NO);
1638 * Send keepalive packets for a peer
1640 * @param cls Closure (tunnel for which to send the keepalive).
1641 * @param tc Notification context.
1644 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1648 * Search for a tunnel among the incoming tunnels
1650 * @param tid the local id of the tunnel
1652 * @return tunnel handler, NULL if doesn't exist
1654 static struct MeshTunnel *
1655 tunnel_get_incoming (MESH_TunnelNumber tid)
1657 struct GNUNET_HashCode hash;
1659 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1660 GMC_hash32 (tid, &hash);
1661 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1666 * Search for a tunnel among the tunnels for a client
1668 * @param c the client whose tunnels to search in
1669 * @param tid the local id of the tunnel
1671 * @return tunnel handler, NULL if doesn't exist
1673 static struct MeshTunnel *
1674 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1676 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1678 return tunnel_get_incoming (tid);
1682 struct GNUNET_HashCode hash;
1684 GMC_hash32 (tid, &hash);
1685 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1691 * Search for a tunnel by global ID using PEER_ID
1693 * @param pi owner of the tunnel
1694 * @param tid global tunnel number
1696 * @return tunnel handler, NULL if doesn't exist
1698 static struct MeshTunnel *
1699 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1701 struct MESH_TunnelID id;
1702 struct GNUNET_HashCode hash;
1707 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1708 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1713 * Search for a tunnel by global ID using full PeerIdentities
1715 * @param oid owner of the tunnel
1716 * @param tid global tunnel number
1718 * @return tunnel handler, NULL if doesn't exist
1720 static struct MeshTunnel *
1721 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1723 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1728 * Add a client to a tunnel, initializing all needed data structures.
1730 * FIXME: make static after implementing port numbers
1732 * @param t Tunnel to which add the client.
1733 * @param c Client which to add to the tunnel.
1736 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1738 if (NULL != t->client)
1743 if (0 != t->next_hop)
1753 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1757 for (i = 0; i < p->length; i++)
1759 if (p->peers[i] == myid)
1762 if (i > p->length - 1)
1768 if (i < p->length - 1)
1769 t->next_hop = p->peers[i + 1];
1773 t->prev_hop = p->peers[i - 1];
1777 if (NULL != t->path)
1778 path_destroy (t->path);
1779 t->path = path_duplicate (p);
1780 if (GNUNET_SCHEDULER_NO_TASK == t->maintenance_task)
1781 t->maintenance_task =
1782 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
1787 * Notifies a tunnel that a connection has broken that affects at least
1788 * some of its peers. Sends a notification towards the root of the tree.
1789 * In case the peer is the owner of the tree, notifies the client that owns
1790 * the tunnel and tries to reconnect.
1792 * @param t Tunnel affected.
1793 * @param p1 Peer that got disconnected from p2.
1794 * @param p2 Peer that got disconnected from p1.
1796 * @return Short ID of the peer disconnected (either p1 or p2).
1797 * 0 if the tunnel remained unaffected.
1799 static GNUNET_PEER_Id
1800 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1803 // if (myid != p1 && myid != p2) FIXME
1808 // if (tree_get_predecessor (t->tree) != 0)
1810 // /* We are the peer still connected, notify owner of the disconnection. */
1811 // struct GNUNET_MESH_PathBroken msg;
1812 // struct GNUNET_PeerIdentity neighbor;
1814 // msg.header.size = htons (sizeof (msg));
1815 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1816 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1817 // msg.tid = htonl (t->id.tid);
1818 // msg.peer1 = my_full_id;
1819 // GNUNET_PEER_resolve (pid, &msg.peer2);
1820 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1821 // send_prebuilt_message (&msg.header, &neighbor, t);
1828 * Build a local ACK message and send it to a local client.
1830 * @param t Tunnel on which to send the ACK.
1831 * @param c Client to whom send the ACK.
1832 * @param ack Value of the ACK.
1835 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
1837 struct GNUNET_MESH_LocalAck msg;
1839 msg.header.size = htons (sizeof (msg));
1840 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1841 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
1842 msg.max_pid = htonl (ack);
1843 GNUNET_SERVER_notification_context_unicast(nc,
1850 * Build an ACK message and queue it to send to the given peer.
1852 * @param t Tunnel on which to send the ACK.
1853 * @param peer Peer to whom send the ACK.
1854 * @param ack Value of the ACK.
1857 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1859 struct GNUNET_MESH_ACK msg;
1861 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1862 msg.header.size = htons (sizeof (msg));
1863 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1864 msg.pid = htonl (ack);
1865 msg.tid = htonl (t->id.tid);
1867 send_prebuilt_message (&msg.header, peer, t);
1872 * Send an ACK informing the predecessor about the available buffer space.
1873 * In case there is no predecessor, inform the owning client.
1874 * If buffering is off, send only on behalf of children or self if endpoint.
1875 * If buffering is on, send when sent to children and buffer space is free.
1876 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
1877 * the ACK itself goes "back" (towards root).
1879 * @param t Tunnel on which to send the ACK.
1880 * @param type Type of message that triggered the ACK transmission.
1883 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
1887 /* Is it after unicast retransmission? */
1890 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1892 "ACK due to FWD DATA retransmission\n");
1893 if (GNUNET_YES == t->nobuffer)
1895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
1899 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1900 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1902 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1903 t->force_ack = GNUNET_YES;
1909 /* Check if we need to transmit the ACK */
1910 if (t->queue_max > t->next_fc.queue_n * 4 &&
1911 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
1912 GNUNET_NO == t->force_ack)
1914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
1915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1916 " t->qmax: %u, t->qn: %u\n",
1917 t->queue_max, t->next_fc.queue_n);
1918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1919 " t->pid: %u, t->ack: %u\n",
1920 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
1924 /* Ok, ACK might be necessary, what PID to ACK? */
1925 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
1926 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
1928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
1932 t->prev_fc.last_ack_sent = ack;
1933 if (0 != t->prev_hop)
1934 send_ack (t, t->prev_hop, ack);
1935 else if (NULL != t->owner)
1936 send_local_ack (t, t->owner, ack);
1940 t->force_ack = GNUNET_NO;
1945 * Send an ACK informing the children node/client about the available
1947 * If buffering is off, send only on behalf of root (can be self).
1948 * If buffering is on, send when sent to predecessor and buffer space is free.
1949 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
1950 * the ACK itself goes "forward" (towards children/clients).
1952 * @param t Tunnel on which to send the ACK.
1953 * @param type Type of message that triggered the ACK transmission.
1956 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
1959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1960 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
1961 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
1962 /* Is it after data to_origin retransmission? */
1965 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1966 if (GNUNET_YES == t->nobuffer)
1968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1969 " Not sending ACK, nobuffer + traffic\n");
1973 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1974 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1976 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1977 t->force_ack = GNUNET_YES;
1983 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
1985 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
1987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1988 " Not sending ACK, not needed\n");
1991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1992 " Sending BCK ACK %u (last sent: %u)\n",
1993 ack, t->next_fc.last_ack_sent);
1994 t->next_fc.last_ack_sent = ack;
1996 if (0 != t->next_hop)
1997 send_ack (t, t->next_hop, ack);
1998 else if (NULL != t->client)
1999 send_local_ack (t, t->client, ack);
2002 t->force_ack = GNUNET_NO;
2007 * @brief Re-initiate traffic to this peer if necessary.
2009 * Check if there is traffic queued towards this peer
2010 * and the core transmit handle is NULL (traffic was stalled).
2011 * If so, call core tmt rdy.
2013 * @param peer_id Short ID of peer to which initiate traffic.
2016 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2018 struct MeshPeerInfo *peer;
2019 struct GNUNET_PeerIdentity id;
2020 struct MeshPeerQueue *q;
2023 peer = peer_get_short (peer_id);
2024 if (NULL != peer->core_transmit)
2027 q = queue_get_next (peer);
2030 /* Might br multicast traffic already sent to this particular peer but
2031 * not to other children in this tunnel.
2032 * This way t->queue_n would be > 0 but the queue of this particular peer
2038 GNUNET_PEER_resolve (peer->id, &id);
2039 peer->core_transmit =
2040 GNUNET_CORE_notify_transmit_ready(core_handle,
2043 GNUNET_TIME_UNIT_FOREVER_REL,
2053 * Send a message to all peers in this tunnel that the tunnel is no longer
2056 * @param t The tunnel whose peers to notify.
2059 tunnel_send_destroy (struct MeshTunnel *t)
2061 struct GNUNET_MESH_TunnelDestroy msg;
2062 struct GNUNET_PeerIdentity id;
2064 msg.header.size = htons (sizeof (msg));
2065 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2066 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2067 msg.tid = htonl (t->id.tid);
2068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2069 " sending tunnel destroy for tunnel: %s [%X]\n",
2070 GNUNET_i2s (&msg.oid), t->id.tid);
2072 if (0 != t->next_hop)
2074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2075 GNUNET_PEER_resolve (t->next_hop, &id);
2076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2077 " sending forward to %s\n",
2079 send_prebuilt_message (&msg.header, t->next_hop, t);
2081 if (0 != t->prev_hop)
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2084 GNUNET_PEER_resolve (t->prev_hop, &id);
2085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2086 " sending back to %s\n",
2088 send_prebuilt_message (&msg.header, t->prev_hop, t);
2094 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2096 * @param t Tunnel which to cancel.
2097 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2100 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2102 struct MeshPeerInfo *peer_info;
2103 struct MeshPeerQueue *pq;
2104 struct MeshPeerQueue *next;
2106 peer_info = peer_get_short (neighbor);
2107 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2110 if (pq->tunnel == t)
2112 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2113 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2115 /* Should have been removed on destroy children */
2118 queue_destroy (pq, GNUNET_YES);
2121 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2123 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2124 peer_info->core_transmit = NULL;
2130 * Destroy the tunnel and free any allocated resources linked to it.
2132 * @param t the tunnel to destroy
2134 * @return GNUNET_OK on success
2137 tunnel_destroy (struct MeshTunnel *t)
2139 struct MeshClient *c;
2140 struct GNUNET_HashCode hash;
2150 struct GNUNET_PeerIdentity id;
2152 GNUNET_PEER_resolve (t->id.oid, &id);
2153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2154 GNUNET_i2s (&id), t->id.tid);
2156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2160 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2161 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2169 GMC_hash32 (t->local_tid, &hash);
2171 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2178 if (NULL != t->client)
2180 GMC_hash32 (t->local_tid_dest, &hash);
2182 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2189 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2195 peer_cancel_queues (t->next_hop, t);
2196 peer_cancel_queues (t->prev_hop, t);
2198 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2199 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2202 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2207 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
2210 * Tunnel is empty: destroy it.
2212 * @param t Tunnel to destroy.
2215 tunnel_destroy_empty (struct MeshTunnel *t)
2219 struct GNUNET_PeerIdentity id;
2221 GNUNET_PEER_resolve (t->id.oid, &id);
2222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2223 "executing destruction of empty tunnel %s [%X]\n",
2224 GNUNET_i2s (&id), t->id.tid);
2228 tunnel_send_destroy (t);
2229 if (0 == t->pending_messages)
2232 t->destroy = GNUNET_YES;
2236 * Initialize a Flow Control structure to the initial state.
2238 * @param fc Flow Control structure to initialize.
2241 fc_init (struct MeshFlowControl *fc)
2243 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2244 fc->last_pid_recv = (uint32_t) -1;
2245 fc->last_ack_sent = INITIAL_WINDOW_SIZE - 1;
2246 fc->last_ack_recv = INITIAL_WINDOW_SIZE - 1;
2247 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2248 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2253 * Create a new tunnel
2255 * @param owner Who is the owner of the tunnel (short ID).
2256 * @param tid Tunnel Number of the tunnel.
2257 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2258 * @param local Tunnel Number for the tunnel, for the client point of view.
2260 * @return A new initialized tunnel. NULL on error.
2262 static struct MeshTunnel *
2263 tunnel_new (GNUNET_PEER_Id owner,
2264 MESH_TunnelNumber tid,
2265 struct MeshClient *client,
2266 MESH_TunnelNumber local)
2268 struct MeshTunnel *t;
2269 struct GNUNET_HashCode hash;
2271 if (n_tunnels >= max_tunnels && NULL == client)
2274 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2277 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2279 fc_init (&t->next_fc);
2280 fc_init (&t->prev_fc);
2281 t->local_tid = local;
2283 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2285 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2287 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2288 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2295 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2302 GMC_hash32 (t->local_tid, &hash);
2304 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2305 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2309 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2320 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2321 * client when the client disconnects. If the client is not the owner, the
2322 * owner will get notified if no more clients are in the tunnel and the client
2323 * get removed from the tunnel's list.
2325 * @param cls closure (client that is disconnecting)
2326 * @param key the hash of the local tunnel id (used to access the hashmap)
2327 * @param value the value stored at the key (tunnel to destroy)
2329 * @return GNUNET_OK, keep iterating.
2332 tunnel_destroy_iterator (void *cls,
2333 const struct GNUNET_HashCode * key,
2336 struct MeshTunnel *t = value;
2337 struct MeshClient *c = cls;
2339 send_client_tunnel_destroy (t);
2342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
2343 client_delete_tunnel (c, t);
2344 tunnel_destroy_empty (t);
2347 tunnel_send_destroy (t);
2349 t->destroy = GNUNET_YES;
2356 * Timeout function, destroys tunnel if called
2358 * @param cls Closure (tunnel to destroy).
2359 * @param tc TaskContext
2362 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2364 struct MeshTunnel *t = cls;
2365 struct GNUNET_PeerIdentity id;
2367 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2368 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2370 GNUNET_PEER_resolve(t->id.oid, &id);
2371 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2372 "Tunnel %s [%X] timed out. Destroying.\n",
2373 GNUNET_i2s(&id), t->id.tid);
2374 send_client_tunnel_destroy (t);
2380 * Resets the tunnel timeout. Starts it if no timeout was running.
2382 * @param t Tunnel whose timeout to reset.
2384 * TODO use heap to improve efficiency of scheduler.
2387 tunnel_reset_timeout (struct MeshTunnel *t)
2389 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2391 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2392 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2393 t->maintenance_task =
2394 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2395 (refresh_path_time, 4), &tunnel_timeout, t);
2399 /******************************************************************************/
2400 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2401 /******************************************************************************/
2404 * Function to send a create path packet to a peer.
2406 * @param cls closure
2407 * @param size number of bytes available in buf
2408 * @param buf where the callee should write the message
2409 * @return number of bytes written to buf
2412 send_core_path_create (void *cls, size_t size, void *buf)
2414 struct MeshTunnel *t = cls;
2415 struct GNUNET_MESH_ManipulatePath *msg;
2416 struct GNUNET_PeerIdentity *peer_ptr;
2417 struct MeshPeerPath *p = t->path;
2422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2424 sizeof (struct GNUNET_MESH_ManipulatePath) +
2425 p->length * sizeof (struct GNUNET_PeerIdentity);
2427 if (size < size_needed || NULL == buf)
2432 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2433 msg->header.size = htons (size_needed);
2434 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2435 msg->tid = ntohl (t->id.tid);
2438 if (GNUNET_YES == t->nobuffer)
2439 opt |= MESH_TUNNEL_OPT_NOBUFFER;
2440 msg->opt = htonl(opt);
2443 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2444 for (i = 0; i < p->length; i++)
2446 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2450 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2456 * Creates a path ack message in buf and frees all unused resources.
2458 * @param cls closure (MeshTransmissionDescriptor)
2459 * @param size number of bytes available in buf
2460 * @param buf where the callee should write the message
2461 * @return number of bytes written to buf
2464 send_core_path_ack (void *cls, size_t size, void *buf)
2466 struct MESH_TunnelID *id = cls;
2467 struct GNUNET_MESH_PathACK *msg = buf;
2469 GNUNET_assert (NULL != id);
2470 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2475 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2476 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2477 GNUNET_PEER_resolve (id->oid, &msg->oid);
2478 msg->tid = htonl (id->tid);
2479 msg->peer_id = my_full_id;
2481 /* TODO add signature */
2483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2484 return sizeof (struct GNUNET_MESH_PathACK);
2489 * Free a transmission that was already queued with all resources
2490 * associated to the request.
2492 * @param queue Queue handler to cancel.
2493 * @param clear_cls Is it necessary to free associated cls?
2496 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2498 struct MeshFlowControl *fc;
2500 if (GNUNET_YES == clear_cls)
2502 switch (queue->type)
2504 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2505 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2506 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2508 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2509 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2510 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2511 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2512 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2514 " prebuilt message\n");
2515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2517 GNUNET_MESH_DEBUG_M2S (queue->type));
2519 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2524 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2525 " type %s unknown!\n",
2526 GNUNET_MESH_DEBUG_M2S (queue->type));
2528 GNUNET_free_non_null (queue->cls);
2530 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2531 queue->peer->queue_tail,
2534 /* Delete from appropiate fc in the tunnel */
2535 if (queue->peer->id == queue->tunnel->next_hop)
2536 fc = &queue->tunnel->next_fc;
2537 else if (queue->peer->id == queue->tunnel->next_hop)
2538 fc = &queue->tunnel->next_fc;
2545 GNUNET_free (queue);
2550 * @brief Get the next transmittable message from the queue.
2552 * This will be the head, except in the case of being a data packet
2553 * not allowed by the destination peer.
2555 * @param peer Destination peer.
2557 * @return The next viable MeshPeerQueue element to send to that peer.
2558 * NULL when there are no transmittable messages.
2560 struct MeshPeerQueue *
2561 queue_get_next (const struct MeshPeerInfo *peer)
2563 struct MeshPeerQueue *q;
2565 struct GNUNET_MESH_Unicast *ucast;
2566 struct GNUNET_MESH_ToOrigin *to_orig;
2567 struct MeshTunnel* t;
2571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
2572 for (q = peer->queue_head; NULL != q; q = q->next)
2575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2577 GNUNET_MESH_DEBUG_M2S(q->type));
2580 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2581 ucast = (struct GNUNET_MESH_Unicast *) q->cls;
2582 pid = ntohl (ucast->pid);
2583 ack = t->next_fc.last_ack_recv;
2585 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2586 to_orig = (struct GNUNET_MESH_ToOrigin *) q->cls;
2587 pid = ntohl (to_orig->pid);
2588 ack = t->prev_fc.last_ack_recv;
2591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2596 "********* ACK: %u, PID: %u\n",
2598 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2607 "********* NEXT!\n");
2610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2611 "********* nothing found\n");
2617 queue_send (void *cls, size_t size, void *buf)
2619 struct MeshPeerInfo *peer = cls;
2620 struct GNUNET_MessageHeader *msg;
2621 struct MeshPeerQueue *queue;
2622 struct MeshTunnel *t;
2623 struct GNUNET_PeerIdentity dst_id;
2624 struct MeshFlowControl *fc;
2628 peer->core_transmit = NULL;
2630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
2631 queue = queue_get_next (peer);
2633 /* Queue has no internal mesh traffic nor sendable payload */
2636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
2637 if (NULL == peer->queue_head)
2638 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
2641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
2643 GNUNET_PEER_resolve (peer->id, &dst_id);
2644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2645 "********* towards %s\n",
2646 GNUNET_i2s (&dst_id));
2647 /* Check if buffer size is enough for the message */
2648 if (queue->size > size)
2650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2651 "********* not enough room, reissue\n");
2652 peer->core_transmit =
2653 GNUNET_CORE_notify_transmit_ready (core_handle,
2656 GNUNET_TIME_UNIT_FOREVER_REL,
2663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
2666 GNUNET_assert (0 < t->pending_messages);
2667 t->pending_messages--;
2671 switch (queue->type)
2674 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2675 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2676 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
2677 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
2678 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2679 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2681 "********* raw: %s\n",
2682 GNUNET_MESH_DEBUG_M2S (queue->type));
2684 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2685 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2686 data_size = send_core_data_raw (queue->cls, size, buf);
2687 msg = (struct GNUNET_MessageHeader *) buf;
2688 type = ntohs (msg->type);
2690 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
2692 data_size = send_core_path_create (queue->cls, size, buf);
2694 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
2696 data_size = send_core_path_ack (queue->cls, size, buf);
2700 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2701 "********* type unknown: %u\n",
2706 /* Free queue, but cls was freed by send_core_* */
2707 queue_destroy (queue, GNUNET_NO);
2709 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
2712 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2713 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
2715 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2716 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
2722 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
2728 /* If more data in queue, send next */
2729 queue = queue_get_next (peer);
2732 struct GNUNET_PeerIdentity id;
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
2735 GNUNET_PEER_resolve (peer->id, &id);
2736 peer->core_transmit =
2737 GNUNET_CORE_notify_transmit_ready(core_handle,
2740 GNUNET_TIME_UNIT_FOREVER_REL,
2746 else if (NULL != peer->queue_head)
2748 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2749 "********* %s stalled\n",
2750 GNUNET_i2s (&my_full_id));
2751 if (peer->id == t->next_hop)
2753 else if (peer->id == t->prev_hop)
2760 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
2763 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Return %d\n", data_size);
2773 * @brief Queue and pass message to core when possible.
2775 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
2776 * accounts for it. In case the queue is full, the message is dropped and
2779 * Otherwise, message is treated as internal and allowed to go regardless of
2782 * @param cls Closure (@c type dependant). It will be used by queue_send to
2783 * build the message to be sent if not already prebuilt.
2784 * @param type Type of the message, 0 for a raw message.
2785 * @param size Size of the message.
2786 * @param dst Neighbor to send message to.
2787 * @param t Tunnel this message belongs to.
2790 queue_add (void *cls, uint16_t type, size_t size,
2791 struct MeshPeerInfo *dst, struct MeshTunnel *t)
2793 struct MeshPeerQueue *queue;
2794 struct GNUNET_PeerIdentity id;
2798 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2800 n = &t->next_fc.queue_n;
2802 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
2804 n = &t->prev_fc.queue_n;
2808 if (*n >= t->queue_max)
2811 GNUNET_STATISTICS_update(stats,
2812 "# messages dropped (buffer full)",
2814 return; /* Drop message */
2818 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2824 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
2825 if (NULL == dst->core_transmit)
2827 GNUNET_PEER_resolve (dst->id, &id);
2828 dst->core_transmit =
2829 GNUNET_CORE_notify_transmit_ready (core_handle,
2832 GNUNET_TIME_UNIT_FOREVER_REL,
2838 t->pending_messages++;
2842 /******************************************************************************/
2843 /******************** MESH NETWORK HANDLERS **************************/
2844 /******************************************************************************/
2848 * Core handler for path creation
2850 * @param cls closure
2851 * @param message message
2852 * @param peer peer identity this notification is about
2854 * @return GNUNET_OK to keep the connection open,
2855 * GNUNET_SYSERR to close it (signal serious error)
2858 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2859 const struct GNUNET_MessageHeader *message)
2861 unsigned int own_pos;
2864 MESH_TunnelNumber tid;
2865 struct GNUNET_MESH_ManipulatePath *msg;
2866 struct GNUNET_PeerIdentity *pi;
2867 struct GNUNET_HashCode hash;
2868 struct MeshPeerPath *path;
2869 struct MeshPeerInfo *dest_peer_info;
2870 struct MeshPeerInfo *orig_peer_info;
2871 struct MeshTunnel *t;
2873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2874 "Received a path create msg [%s]\n",
2875 GNUNET_i2s (&my_full_id));
2876 size = ntohs (message->size);
2877 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2879 GNUNET_break_op (0);
2883 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2884 if (size % sizeof (struct GNUNET_PeerIdentity))
2886 GNUNET_break_op (0);
2889 size /= sizeof (struct GNUNET_PeerIdentity);
2892 GNUNET_break_op (0);
2895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2896 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2898 tid = ntohl (msg->tid);
2899 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2901 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2902 t = tunnel_get (pi, tid);
2907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2908 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
2914 opt = ntohl (msg->opt);
2915 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
2917 t->nobuffer = GNUNET_YES;
2918 t->prev_fc.last_ack_sent = t->prev_fc.last_pid_recv + 1;
2920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
2922 if (GNUNET_YES == t->nobuffer)
2927 tunnel_reset_timeout (t);
2928 GMC_hash32 (t->local_tid_dest, &hash);
2930 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2931 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2938 t->state = MESH_TUNNEL_WAITING;
2940 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2941 if (NULL == dest_peer_info)
2943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2944 " Creating PeerInfo for destination.\n");
2945 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2946 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2947 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2949 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2951 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2952 if (NULL == orig_peer_info)
2954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2955 " Creating PeerInfo for origin.\n");
2956 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2957 orig_peer_info->id = GNUNET_PEER_intern (pi);
2958 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2959 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
2962 path = path_new (size);
2964 for (i = 0; i < size; i++)
2966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
2967 GNUNET_i2s (&pi[i]));
2968 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2969 if (path->peers[i] == myid)
2972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
2975 /* cannot be self, must be 'not found' */
2976 /* create path: self not found in path through self */
2977 GNUNET_break_op (0);
2978 path_destroy (path);
2982 path_add_to_peers (path, GNUNET_NO);
2983 t->prev_hop = path->peers[own_pos - 1];
2984 GNUNET_PEER_change_rc (t->prev_hop, 1);
2985 if (own_pos == size - 1)
2987 /* It is for us! Send ack. */
2988 // TODO: check port number / client registration
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2990 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2993 while (NULL != tunnel_get_incoming (next_local_tid))
2994 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2995 t->local_tid_dest = next_local_tid++;
2996 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3002 struct MeshPeerPath *path2;
3004 t->next_hop = path->peers[own_pos + 1];
3005 GNUNET_PEER_change_rc(t->next_hop, 1);
3007 /* It's for somebody else! Retransmit. */
3008 path2 = path_duplicate (path);
3009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3010 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3011 path2 = path_duplicate (path);
3012 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3013 send_create_path (t);
3020 * Core handler for path destruction
3022 * @param cls closure
3023 * @param message message
3024 * @param peer peer identity this notification is about
3026 * @return GNUNET_OK to keep the connection open,
3027 * GNUNET_SYSERR to close it (signal serious error)
3030 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3031 const struct GNUNET_MessageHeader *message)
3033 struct GNUNET_MESH_ManipulatePath *msg;
3034 struct GNUNET_PeerIdentity *pi;
3035 struct MeshTunnel *t;
3036 unsigned int own_pos;
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3041 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3042 size = ntohs (message->size);
3043 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3045 GNUNET_break_op (0);
3049 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3050 if (size % sizeof (struct GNUNET_PeerIdentity))
3052 GNUNET_break_op (0);
3055 size /= sizeof (struct GNUNET_PeerIdentity);
3058 GNUNET_break_op (0);
3061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3063 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3064 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3066 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3068 t = tunnel_get (pi, ntohl (msg->tid));
3071 /* TODO notify back: we don't know this tunnel */
3072 GNUNET_break_op (0);
3076 for (i = 0; i < size; i++)
3078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", i, GNUNET_i2s (&pi[i]));
3079 if (GNUNET_PEER_search (&pi[i]) == myid)
3082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3083 if (own_pos < size - 1)
3084 send_prebuilt_message (message, t->next_hop, t);
3086 send_client_tunnel_destroy (t);
3093 * Core handler for notifications of broken paths
3095 * @param cls closure
3096 * @param message message
3097 * @param peer peer identity this notification is about
3099 * @return GNUNET_OK to keep the connection open,
3100 * GNUNET_SYSERR to close it (signal serious error)
3103 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3104 const struct GNUNET_MessageHeader *message)
3106 struct GNUNET_MESH_PathBroken *msg;
3107 struct MeshTunnel *t;
3109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3110 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3111 msg = (struct GNUNET_MESH_PathBroken *) message;
3112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3113 GNUNET_i2s (&msg->peer1));
3114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3115 GNUNET_i2s (&msg->peer2));
3116 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3119 GNUNET_break_op (0);
3122 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3123 GNUNET_PEER_search (&msg->peer2));
3130 * Core handler for tunnel destruction
3132 * @param cls closure
3133 * @param message message
3134 * @param peer peer identity this notification is about
3136 * @return GNUNET_OK to keep the connection open,
3137 * GNUNET_SYSERR to close it (signal serious error)
3140 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3141 const struct GNUNET_MessageHeader *message)
3143 struct GNUNET_MESH_TunnelDestroy *msg;
3144 struct MeshTunnel *t;
3146 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3148 "Got a TUNNEL DESTROY packet from %s\n",
3150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3151 " for tunnel %s [%u]\n",
3152 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3153 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3156 /* Probably already got the message from another path,
3157 * destroyed the tunnel and retransmitted to children.
3160 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3164 // TODO check owner's signature
3165 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3167 /* Tunnel was incoming, notify clients */
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3169 t->local_tid, t->local_tid_dest);
3170 send_client_tunnel_destroy (t);
3172 tunnel_send_destroy (t);
3173 t->destroy = GNUNET_YES;
3174 // TODO: add timeout to destroy the tunnel anyway
3180 * Core handler for mesh network traffic going from the origin to a peer
3182 * @param cls closure
3183 * @param peer peer identity this notification is about
3184 * @param message message
3185 * @return GNUNET_OK to keep the connection open,
3186 * GNUNET_SYSERR to close it (signal serious error)
3189 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3190 const struct GNUNET_MessageHeader *message)
3192 struct GNUNET_MESH_Unicast *msg;
3193 struct MeshTunnel *t;
3198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3201 size = ntohs (message->size);
3203 sizeof (struct GNUNET_MESH_Unicast) +
3204 sizeof (struct GNUNET_MessageHeader))
3209 msg = (struct GNUNET_MESH_Unicast *) message;
3210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3211 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3213 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3216 /* TODO notify back: we don't know this tunnel */
3217 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3218 GNUNET_break_op (0);
3221 pid = ntohl (msg->pid);
3222 if (t->prev_fc.last_pid_recv == pid)
3224 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3225 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3226 " Already seen pid %u, DROPPING!\n", pid);
3231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3232 " pid %u not seen yet, forwarding\n", pid);
3235 t->prev_fc.last_pid_recv = pid;
3237 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3239 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3240 GNUNET_break_op (0);
3241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3242 "Received PID %u, ACK %u\n",
3243 pid, t->prev_fc.last_ack_sent);
3247 tunnel_reset_timeout (t);
3248 if (t->dest == myid)
3250 if (NULL == t->client)
3255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3256 " it's for us! sending to clients...\n");
3257 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3258 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
3259 message, GNUNET_NO);
3260 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3263 if (0 == t->next_hop)
3268 ttl = ntohl (msg->ttl);
3269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3272 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3273 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3274 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3278 " not for us, retransmitting...\n");
3280 if (GNUNET_YES == t->nobuffer &&
3281 GNUNET_YES == GMC_is_pid_bigger (pid, t->next_fc.last_ack_recv))
3283 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3284 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n",
3285 pid, t->next_fc.last_ack_recv);
3286 GNUNET_break_op (0);
3289 send_prebuilt_message (message, t->next_hop, t);
3290 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3296 * Core handler for mesh network traffic toward the owner of a tunnel
3298 * @param cls closure
3299 * @param message message
3300 * @param peer peer identity this notification is about
3302 * @return GNUNET_OK to keep the connection open,
3303 * GNUNET_SYSERR to close it (signal serious error)
3306 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3307 const struct GNUNET_MessageHeader *message)
3309 struct GNUNET_MESH_ToOrigin *msg;
3310 struct MeshPeerInfo *peer_info;
3311 struct MeshTunnel *t;
3315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3317 size = ntohs (message->size);
3318 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3319 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3321 GNUNET_break_op (0);
3324 msg = (struct GNUNET_MESH_ToOrigin *) message;
3325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3326 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3327 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3328 pid = ntohl (msg->pid);
3332 /* TODO notify that we dont know this tunnel (whom)? */
3333 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3335 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3336 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3341 if (t->next_fc.last_pid_recv == pid)
3343 /* already seen this packet, drop */
3344 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3346 " Already seen pid %u, DROPPING!\n", pid);
3347 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3352 " pid %u not seen yet, forwarding\n", pid);
3353 t->next_fc.last_pid_recv = pid;
3355 if (NULL != t->owner)
3358 struct GNUNET_MESH_ToOrigin *copy;
3360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3361 " it's for us! sending to clients...\n");
3362 /* TODO signature verification */
3363 memcpy (cbuf, message, size);
3364 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3365 copy->tid = htonl (t->local_tid);
3366 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3367 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3368 ©->header, GNUNET_NO);
3369 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3373 " not for us, retransmitting...\n");
3375 peer_info = peer_get (&msg->oid);
3376 if (NULL == peer_info)
3378 /* unknown origin of tunnel */
3382 if (0 == t->prev_hop) /* No owner AND no prev hop */
3384 if (GNUNET_YES == t->destroy)
3386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3387 "to orig received on a dying tunnel %s [%X]\n",
3388 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3392 "unknown to origin at %s\n",
3393 GNUNET_i2s (&my_full_id));
3394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3397 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3398 "for tunnel %s [%X]\n",
3399 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3402 send_prebuilt_message (message, t->prev_hop, t);
3403 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3410 * Core handler for mesh network traffic point-to-point acks.
3412 * @param cls closure
3413 * @param message message
3414 * @param peer peer identity this notification is about
3416 * @return GNUNET_OK to keep the connection open,
3417 * GNUNET_SYSERR to close it (signal serious error)
3420 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3421 const struct GNUNET_MessageHeader *message)
3423 struct GNUNET_MESH_ACK *msg;
3424 struct MeshTunnel *t;
3427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3429 msg = (struct GNUNET_MESH_ACK *) message;
3431 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3435 /* TODO notify that we dont know this tunnel (whom)? */
3436 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3439 ack = ntohl (msg->pid);
3440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3442 /* Is this a forward or backward ACK? */
3443 if (t->prev_hop != GNUNET_PEER_search (peer))
3446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3447 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task &&
3448 GMC_is_pid_bigger (ack, t->next_fc.last_ack_recv))
3450 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3451 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3452 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3454 t->next_fc.last_ack_recv = ack;
3455 peer_unlock_queue (t->next_hop);
3456 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3461 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
3462 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
3464 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3465 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3466 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3468 t->prev_fc.last_ack_recv = ack;
3469 peer_unlock_queue (t->prev_hop);
3470 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3477 * Core handler for mesh network traffic point-to-point ack polls.
3479 * @param cls closure
3480 * @param message message
3481 * @param peer peer identity this notification is about
3483 * @return GNUNET_OK to keep the connection open,
3484 * GNUNET_SYSERR to close it (signal serious error)
3487 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3488 const struct GNUNET_MessageHeader *message)
3490 struct GNUNET_MESH_Poll *msg;
3491 struct MeshTunnel *t;
3494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3497 msg = (struct GNUNET_MESH_Poll *) message;
3499 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3503 /* TODO notify that we dont know this tunnel (whom)? */
3504 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
3505 GNUNET_break_op (0);
3509 /* Is this a forward or backward ACK? */
3510 id = GNUNET_PEER_search(peer);
3511 if (t->next_hop == id)
3513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
3514 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3516 else if (t->prev_hop == id)
3518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
3519 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3528 * Core handler for path ACKs
3530 * @param cls closure
3531 * @param message message
3532 * @param peer peer identity this notification is about
3534 * @return GNUNET_OK to keep the connection open,
3535 * GNUNET_SYSERR to close it (signal serious error)
3538 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3539 const struct GNUNET_MessageHeader *message)
3541 struct GNUNET_MESH_PathACK *msg;
3542 struct MeshPeerInfo *peer_info;
3543 struct MeshPeerPath *p;
3544 struct MeshTunnel *t;
3546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3547 GNUNET_i2s (&my_full_id));
3548 msg = (struct GNUNET_MESH_PathACK *) message;
3549 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3552 /* TODO notify that we don't know the tunnel */
3553 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3555 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3559 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3561 peer_info = peer_get (&msg->peer_id);
3562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3563 GNUNET_i2s (&msg->peer_id));
3564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3567 /* Add path to peers? */
3571 path_add_to_peers (p, GNUNET_YES);
3577 t->state = MESH_TUNNEL_READY;
3579 /* Message for us? */
3580 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3583 if (NULL == t->owner)
3585 GNUNET_break_op (0);
3588 if (NULL != peer_info->dhtget)
3590 GNUNET_DHT_get_stop (peer_info->dhtget);
3591 peer_info->dhtget = NULL;
3596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3597 " not for us, retransmitting...\n");
3598 peer_info = peer_get (&msg->oid);
3599 send_prebuilt_message (message, t->prev_hop, t);
3605 * Core handler for mesh keepalives.
3607 * @param cls closure
3608 * @param message message
3609 * @param peer peer identity this notification is about
3610 * @return GNUNET_OK to keep the connection open,
3611 * GNUNET_SYSERR to close it (signal serious error)
3613 * TODO: Check who we got this from, to validate route.
3616 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
3617 const struct GNUNET_MessageHeader *message)
3619 struct GNUNET_MESH_TunnelKeepAlive *msg;
3620 struct MeshTunnel *t;
3622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
3625 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
3626 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3630 /* TODO notify that we dont know that tunnel */
3631 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
3636 tunnel_reset_timeout (t);
3638 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
3639 send_prebuilt_message (message, t->next_hop, t);
3646 * Functions to handle messages from core
3648 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3649 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3650 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3651 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3652 sizeof (struct GNUNET_MESH_PathBroken)},
3653 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
3654 sizeof (struct GNUNET_MESH_TunnelDestroy)},
3655 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3656 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
3657 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
3658 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3659 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
3660 sizeof (struct GNUNET_MESH_ACK)},
3661 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
3662 sizeof (struct GNUNET_MESH_Poll)},
3663 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3664 sizeof (struct GNUNET_MESH_PathACK)},
3670 /******************************************************************************/
3671 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3672 /******************************************************************************/
3677 * notify_client_connection_failure: notify a client that the connection to the
3678 * requested remote peer is not possible (for instance, no route found)
3679 * Function called when the socket is ready to queue more data. "buf" will be
3680 * NULL and "size" zero if the socket was closed for writing in the meantime.
3682 * @param cls closure
3683 * @param size number of bytes available in buf
3684 * @param buf where the callee should write the message
3685 * @return number of bytes written to buf
3688 notify_client_connection_failure (void *cls, size_t size, void *buf)
3691 struct MeshPeerInfo *peer_info;
3692 struct GNUNET_MESH_PeerControl *msg;
3693 struct GNUNET_PeerIdentity id;
3695 if (0 == size && NULL == buf)
3697 // TODO retry? cancel?
3701 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3702 peer_info = (struct MeshPeerInfo *) cls;
3703 msg = (struct GNUNET_MESH_PeerControl *) buf;
3704 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3705 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3706 // msg->tunnel_id = htonl(peer_info->t->tid);
3707 GNUNET_PEER_resolve (peer_info->id, &id);
3708 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3716 * Send keepalive packets for a tunnel.
3718 * @param cls Closure (tunnel for which to send the keepalive).
3719 * @param tc Notification context.
3722 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3724 struct MeshTunnel *t = cls;
3725 struct GNUNET_MESH_TunnelKeepAlive *msg;
3726 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
3729 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3730 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
3731 NULL == t->owner || 0 == t->local_tid || 0 != t->prev_hop)
3736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3737 "sending keepalive for tunnel %d\n", t->id.tid);
3739 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
3740 msg->header.size = htons (size);
3741 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3742 msg->oid = my_full_id;
3743 msg->tid = htonl (t->id.tid);
3744 send_prebuilt_message (&msg->header, t->next_hop, t);
3746 t->maintenance_task =
3747 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3752 * Function to process paths received for a new peer addition. The recorded
3753 * paths form the initial tunnel, which can be optimized later.
3754 * Called on each result obtained for the DHT search.
3756 * @param cls closure
3757 * @param exp when will this value expire
3758 * @param key key of the result
3759 * @param get_path path of the get request
3760 * @param get_path_length lenght of get_path
3761 * @param put_path path of the put request
3762 * @param put_path_length length of the put_path
3763 * @param type type of the result
3764 * @param size number of bytes in data
3765 * @param data pointer to the result data
3768 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3769 const struct GNUNET_HashCode * key,
3770 const struct GNUNET_PeerIdentity *get_path,
3771 unsigned int get_path_length,
3772 const struct GNUNET_PeerIdentity *put_path,
3773 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3774 size_t size, const void *data)
3776 struct MeshPeerInfo *peer = cls;
3777 struct MeshPeerPath *p;
3778 struct GNUNET_PeerIdentity pi;
3781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3782 GNUNET_PEER_resolve (peer->id, &pi);
3783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3785 p = path_build_from_dht (get_path, get_path_length,
3786 put_path, put_path_length);
3787 path_add_to_peers (p, GNUNET_NO);
3789 for (i = 0; i < peer->ntunnels; i++)
3791 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
3792 peer_connect (peer, peer->tunnels[i]);
3799 /******************************************************************************/
3800 /********************* MESH LOCAL HANDLES **************************/
3801 /******************************************************************************/
3805 * Handler for client disconnection
3807 * @param cls closure
3808 * @param client identification of the client; NULL
3809 * for the last call when the server is destroyed
3812 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3814 struct MeshClient *c;
3815 struct MeshClient *next;
3817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3827 if (c->handle != client)
3829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3835 GNUNET_SERVER_client_drop (c->handle);
3836 c->shutting_down = GNUNET_YES;
3837 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3838 &tunnel_destroy_iterator, c);
3839 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3840 &tunnel_destroy_iterator, c);
3841 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3842 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3844 if (NULL != c->types)
3845 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3847 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3850 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
3853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
3859 * Handler for new clients
3861 * @param cls closure
3862 * @param client identification of the client
3863 * @param message the actual message, which includes messages the client wants
3866 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3867 const struct GNUNET_MessageHeader *message)
3869 struct GNUNET_MESH_ClientConnect *cc_msg;
3870 struct MeshClient *c;
3876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3878 /* Check data sanity */
3879 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3880 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3881 ntypes = ntohs (cc_msg->types);
3882 if (size != ntypes * sizeof (uint16_t))
3885 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3889 /* Create new client structure */
3890 c = GNUNET_malloc (sizeof (struct MeshClient));
3891 c->id = next_client_id++; /* overflow not important: just for debug */
3892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
3893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u types\n", ntypes);
3895 GNUNET_SERVER_client_keep (client);
3899 struct GNUNET_HashCode hc;
3901 t = (uint16_t *) &cc_msg[1];
3902 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
3903 for (i = 0; i < ntypes; i++)
3906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
3907 GMC_hash32 ((uint32_t) u16, &hc);
3909 /* store in clients hashmap */
3910 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3911 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3912 /* store in global hashmap */
3913 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3914 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3918 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
3919 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
3920 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
3921 GNUNET_SERVER_notification_context_add (nc, client);
3922 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
3924 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3930 * Handler for requests of new tunnels
3932 * @param cls Closure.
3933 * @param client Identification of the client.
3934 * @param message The actual message.
3937 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3938 const struct GNUNET_MessageHeader *message)
3940 struct GNUNET_MESH_TunnelMessage *t_msg;
3941 struct MeshPeerInfo *peer_info;
3942 struct MeshTunnel *t;
3943 struct MeshClient *c;
3944 MESH_TunnelNumber tid;
3946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3948 /* Sanity check for client registration */
3949 if (NULL == (c = client_get (client)))
3952 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3957 /* Message sanity check */
3958 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3961 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3965 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
3967 GNUNET_i2s (&t_msg->peer));
3968 /* Sanity check for tunnel numbering */
3969 tid = ntohl (t_msg->tunnel_id);
3970 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3973 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3976 /* Sanity check for duplicate tunnel IDs */
3977 if (NULL != tunnel_get_by_local_id (c, tid))
3980 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3984 while (NULL != tunnel_get_by_pi (myid, next_tid))
3985 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3986 t = tunnel_new (myid, next_tid, c, tid);
3987 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3991 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
3995 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3997 peer_info = peer_get (&t_msg->peer);
3998 GNUNET_array_append (peer_info->tunnels, peer_info->ntunnels, t);
3999 peer_connect (peer_info, t);
4000 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4006 * Handler for requests of deleting tunnels
4008 * @param cls closure
4009 * @param client identification of the client
4010 * @param message the actual message
4013 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4014 const struct GNUNET_MessageHeader *message)
4016 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4017 struct MeshClient *c;
4018 struct MeshTunnel *t;
4019 MESH_TunnelNumber tid;
4021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4022 "Got a DESTROY TUNNEL from client!\n");
4024 /* Sanity check for client registration */
4025 if (NULL == (c = client_get (client)))
4028 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4033 /* Message sanity check */
4034 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4037 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4041 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4043 /* Retrieve tunnel */
4044 tid = ntohl (tunnel_msg->tunnel_id);
4045 t = tunnel_get_by_local_id(c, tid);
4048 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4055 tunnel_destroy_empty (t);
4056 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4059 send_client_tunnel_destroy (t);
4060 client_delete_tunnel (c, t);
4062 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4064 tunnel_send_destroy (t);
4065 peer_remove_tunnel (peer_get_short(t->dest), t);
4066 t->destroy = GNUNET_YES;
4067 /* The tunnel will be destroyed when the last message is transmitted. */
4068 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4074 * Handler for requests of seeting tunnel's buffering policy.
4076 * @param cls Closure (unused).
4077 * @param client Identification of the client.
4078 * @param message The actual message.
4081 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
4082 const struct GNUNET_MessageHeader *message)
4084 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4085 struct MeshClient *c;
4086 struct MeshTunnel *t;
4087 MESH_TunnelNumber tid;
4089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4090 "Got a BUFFER request from client!\n");
4092 /* Sanity check for client registration */
4093 if (NULL == (c = client_get (client)))
4096 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4101 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4103 /* Retrieve tunnel */
4104 tid = ntohl (tunnel_msg->tunnel_id);
4105 t = tunnel_get_by_local_id(c, tid);
4108 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4110 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4114 switch (ntohs(message->type))
4116 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
4117 t->nobuffer = GNUNET_NO;
4119 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
4120 t->nobuffer = GNUNET_YES;
4126 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4131 * Handler for client traffic directed to one peer
4133 * @param cls closure
4134 * @param client identification of the client
4135 * @param message the actual message
4138 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4139 const struct GNUNET_MessageHeader *message)
4141 struct MeshClient *c;
4142 struct MeshTunnel *t;
4143 struct GNUNET_MESH_Unicast *data_msg;
4144 MESH_TunnelNumber tid;
4147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4148 "Got a unicast request from a client!\n");
4150 /* Sanity check for client registration */
4151 if (NULL == (c = client_get (client)))
4154 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4159 data_msg = (struct GNUNET_MESH_Unicast *) message;
4161 /* Sanity check for message size */
4162 size = ntohs (message->size);
4163 if (sizeof (struct GNUNET_MESH_Unicast) +
4164 sizeof (struct GNUNET_MessageHeader) > size)
4167 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4171 /* Tunnel exists? */
4172 tid = ntohl (data_msg->tid);
4173 t = tunnel_get_by_local_id (c, tid);
4177 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4181 /* Is it a local tunnel? Then, does client own the tunnel? */
4182 if (t->owner->handle != client)
4185 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4189 /* PID should be as expected: client<->service communication */
4190 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4193 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4194 "Unicast PID, expected %u, got %u\n",
4195 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4196 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4200 /* Ok, everything is correct, send the message
4201 * (pretend we got it from a mesh peer)
4204 /* Work around const limitation */
4205 char buf[ntohs (message->size)] GNUNET_ALIGN;
4206 struct GNUNET_MESH_Unicast *copy;
4208 copy = (struct GNUNET_MESH_Unicast *) buf;
4209 memcpy (buf, data_msg, size);
4210 copy->oid = my_full_id;
4211 copy->tid = htonl (t->id.tid);
4212 copy->ttl = htonl (default_ttl);
4213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4214 " calling generic handler...\n");
4215 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
4217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4218 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4225 * Handler for client traffic directed to the origin
4227 * @param cls closure
4228 * @param client identification of the client
4229 * @param message the actual message
4232 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4233 const struct GNUNET_MessageHeader *message)
4235 struct GNUNET_MESH_ToOrigin *data_msg;
4236 struct MeshFlowControl *fc;
4237 struct MeshClient *c;
4238 struct MeshTunnel *t;
4239 MESH_TunnelNumber tid;
4242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4243 "Got a ToOrigin request from a client!\n");
4244 /* Sanity check for client registration */
4245 if (NULL == (c = client_get (client)))
4248 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4253 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4255 /* Sanity check for message size */
4256 size = ntohs (message->size);
4257 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4258 sizeof (struct GNUNET_MessageHeader) > size)
4261 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4265 /* Tunnel exists? */
4266 tid = ntohl (data_msg->tid);
4267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4268 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4271 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4274 t = tunnel_get_by_local_id (c, tid);
4277 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4278 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4280 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4284 /* It should be sent by someone who has this as incoming tunnel. */
4288 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4292 /* PID should be as expected */
4294 if (ntohl (data_msg->pid) != fc->last_pid_recv + 1)
4297 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4298 "To Origin PID, expected %u, got %u\n",
4299 fc->last_pid_recv + 1,
4300 ntohl (data_msg->pid));
4301 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4304 fc->last_pid_recv++;
4306 /* Ok, everything is correct, send the message
4307 * (pretend we got it from a mesh peer)
4310 char buf[ntohs (message->size)] GNUNET_ALIGN;
4311 struct GNUNET_MESH_ToOrigin *copy;
4313 /* Work around 'const' limitation */
4314 memcpy (buf, data_msg, size);
4315 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4316 GNUNET_PEER_resolve (t->id.oid, ©->oid);
4317 copy->tid = htonl (t->id.tid);
4318 copy->ttl = htonl (default_ttl);
4319 copy->pid = htonl (t->prev_fc.last_pid_sent + 1);
4321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4322 " calling generic handler...\n");
4323 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
4325 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4332 * Handler for client's ACKs for payload traffic.
4334 * @param cls Closure (unused).
4335 * @param client Identification of the client.
4336 * @param message The actual message.
4339 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4340 const struct GNUNET_MessageHeader *message)
4342 struct GNUNET_MESH_LocalAck *msg;
4343 struct MeshTunnel *t;
4344 struct MeshClient *c;
4345 MESH_TunnelNumber tid;
4348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4349 /* Sanity check for client registration */
4350 if (NULL == (c = client_get (client)))
4353 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4358 msg = (struct GNUNET_MESH_LocalAck *) message;
4360 /* Tunnel exists? */
4361 tid = ntohl (msg->tunnel_id);
4362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4363 t = tunnel_get_by_local_id (c, tid);
4367 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4368 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4369 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4373 ack = ntohl (msg->max_pid);
4374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4376 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4379 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4380 t->prev_fc.last_ack_recv = ack;
4381 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4385 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4386 t->next_fc.last_ack_recv = ack;
4387 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4390 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4398 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4400 * @param cls Closure (client handle).
4401 * @param key Key (hashed tunnel ID, unused).
4402 * @param value Tunnel info.
4404 * @return GNUNET_YES, to keep iterating.
4407 monitor_all_tunnels_iterator (void *cls,
4408 const struct GNUNET_HashCode * key,
4411 struct GNUNET_SERVER_Client *client = cls;
4412 struct MeshTunnel *t = value;
4413 struct GNUNET_MESH_LocalMonitor *msg;
4415 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4416 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4417 msg->tunnel_id = htonl (t->id.tid);
4418 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4419 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4420 GNUNET_PEER_resolve (t->dest, &msg->destination);
4422 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4423 "* sending info about tunnel %s [%u]\n",
4424 GNUNET_i2s (&msg->owner), t->id.tid);
4426 GNUNET_SERVER_notification_context_unicast (nc, client,
4427 &msg->header, GNUNET_NO);
4433 * Handler for client's MONITOR request.
4435 * @param cls Closure (unused).
4436 * @param client Identification of the client.
4437 * @param message The actual message.
4440 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4441 const struct GNUNET_MessageHeader *message)
4443 struct MeshClient *c;
4445 /* Sanity check for client registration */
4446 if (NULL == (c = client_get (client)))
4449 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4453 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4454 "Received get tunnels request from client %u\n",
4456 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4457 monitor_all_tunnels_iterator,
4459 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4460 "Get tunnels request from client %u completed\n",
4462 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4467 * Handler for client's MONITOR_TUNNEL request.
4469 * @param cls Closure (unused).
4470 * @param client Identification of the client.
4471 * @param message The actual message.
4474 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4475 const struct GNUNET_MessageHeader *message)
4477 const struct GNUNET_MESH_LocalMonitor *msg;
4478 struct GNUNET_MESH_LocalMonitor *resp;
4479 struct MeshClient *c;
4480 struct MeshTunnel *t;
4482 /* Sanity check for client registration */
4483 if (NULL == (c = client_get (client)))
4486 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4490 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4491 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4492 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4495 ntohl (msg->tunnel_id));
4496 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4499 /* We don't know the tunnel FIXME */
4500 struct GNUNET_MESH_LocalMonitor warn;
4503 GNUNET_SERVER_notification_context_unicast (nc, client,
4506 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4510 /* Initialize context */
4511 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4513 GNUNET_PEER_resolve (t->dest, &resp->destination);
4514 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4515 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4516 &resp->header, GNUNET_NO);
4519 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4520 "Monitor tunnel request from client %u completed\n",
4522 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4527 * Functions to handle messages from clients
4529 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4530 {&handle_local_new_client, NULL,
4531 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4532 {&handle_local_tunnel_create, NULL,
4533 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4534 sizeof (struct GNUNET_MESH_TunnelMessage)},
4535 {&handle_local_tunnel_destroy, NULL,
4536 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4537 sizeof (struct GNUNET_MESH_TunnelMessage)},
4538 {&handle_local_tunnel_buffer, NULL,
4539 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
4540 sizeof (struct GNUNET_MESH_TunnelMessage)},
4541 {&handle_local_tunnel_buffer, NULL,
4542 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
4543 sizeof (struct GNUNET_MESH_TunnelMessage)},
4544 {&handle_local_unicast, NULL,
4545 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4546 {&handle_local_to_origin, NULL,
4547 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4548 {&handle_local_ack, NULL,
4549 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4550 sizeof (struct GNUNET_MESH_LocalAck)},
4551 {&handle_local_get_tunnels, NULL,
4552 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4553 sizeof (struct GNUNET_MessageHeader)},
4554 {&handle_local_show_tunnel, NULL,
4555 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4556 sizeof (struct GNUNET_MESH_LocalMonitor)},
4562 * Method called whenever a given peer connects.
4564 * @param cls closure
4565 * @param peer peer identity this notification is about
4568 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4570 struct MeshPeerInfo *peer_info;
4571 struct MeshPeerPath *path;
4573 DEBUG_CONN ("Peer connected\n");
4574 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4575 peer_info = peer_get (peer);
4576 if (myid == peer_info->id)
4578 DEBUG_CONN (" (self)\n");
4583 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4585 path = path_new (2);
4586 path->peers[0] = myid;
4587 path->peers[1] = peer_info->id;
4588 GNUNET_PEER_change_rc (myid, 1);
4589 GNUNET_PEER_change_rc (peer_info->id, 1);
4590 peer_info_add_path (peer_info, path, GNUNET_YES);
4591 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4597 * Method called whenever a peer disconnects.
4599 * @param cls closure
4600 * @param peer peer identity this notification is about
4603 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4605 struct MeshPeerInfo *pi;
4606 struct MeshPeerQueue *q;
4607 struct MeshPeerQueue *n;
4609 DEBUG_CONN ("Peer disconnected\n");
4610 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4620 /* TODO try to reroute this traffic instead */
4621 queue_destroy(q, GNUNET_YES);
4624 if (NULL != pi->core_transmit)
4626 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4627 pi->core_transmit = NULL;
4629 peer_remove_path (pi, pi->id, myid);
4632 DEBUG_CONN (" (self)\n");
4634 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
4640 * To be called on core init/fail.
4642 * @param cls Closure (config)
4643 * @param server handle to the server for this service
4644 * @param identity the public identity of this peer
4647 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4648 const struct GNUNET_PeerIdentity *identity)
4650 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4654 core_handle = server;
4655 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4658 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4659 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4661 GNUNET_i2s (identity));
4662 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4664 GNUNET_i2s (&my_full_id));
4665 GNUNET_CORE_disconnect (core_handle);
4666 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4667 NULL, /* Closure passed to MESH functions */
4668 &core_init, /* Call core_init once connected */
4669 &core_connect, /* Handle connects */
4670 &core_disconnect, /* remove peers on disconnects */
4671 NULL, /* Don't notify about all incoming messages */
4672 GNUNET_NO, /* For header only in notification */
4673 NULL, /* Don't notify about all outbound messages */
4674 GNUNET_NO, /* For header-only out notification */
4675 core_handlers); /* Register these handlers */
4683 /******************************************************************************/
4684 /************************ MAIN FUNCTIONS ****************************/
4685 /******************************************************************************/
4688 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4690 * @param cls closure
4691 * @param key current key code
4692 * @param value value in the hash map
4693 * @return GNUNET_YES if we should continue to iterate,
4697 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
4699 struct MeshTunnel *t = value;
4706 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4708 * @param cls closure
4709 * @param key current key code
4710 * @param value value in the hash map
4711 * @return GNUNET_YES if we should continue to iterate,
4715 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
4717 struct MeshPeerInfo *p = value;
4718 struct MeshPeerQueue *q;
4719 struct MeshPeerQueue *n;
4727 queue_destroy(q, GNUNET_YES);
4731 peer_info_destroy (p);
4737 * Task run during shutdown.
4743 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4747 if (core_handle != NULL)
4749 GNUNET_CORE_disconnect (core_handle);
4754 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
4757 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4758 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4759 if (dht_handle != NULL)
4761 GNUNET_DHT_disconnect (dht_handle);
4766 GNUNET_SERVER_notification_context_destroy (nc);
4769 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4771 GNUNET_SCHEDULER_cancel (announce_id_task);
4772 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4779 * Callback for hostkey read/generation.
4781 * @param cls Closure (Configuration handle).
4782 * @param pk The ECC private key.
4783 * @param emsg Error message, if any.
4786 key_generation_cb (void *cls,
4787 struct GNUNET_CRYPTO_EccPrivateKey *pk,
4790 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4791 struct MeshPeerInfo *peer;
4792 struct MeshPeerPath *p;
4797 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4798 _("Mesh service could not access hostkey: %s. Exiting.\n"),
4800 GNUNET_SCHEDULER_shutdown ();
4803 my_private_key = pk;
4804 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
4805 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4806 &my_full_id.hashPubKey);
4807 myid = GNUNET_PEER_intern (&my_full_id);
4808 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4809 "Mesh for peer [%s] starting\n",
4810 GNUNET_i2s(&my_full_id));
4812 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4813 NULL, /* Closure passed to MESH functions */
4814 &core_init, /* Call core_init once connected */
4815 &core_connect, /* Handle connects */
4816 &core_disconnect, /* remove peers on disconnects */
4817 NULL, /* Don't notify about all incoming messages */
4818 GNUNET_NO, /* For header only in notification */
4819 NULL, /* Don't notify about all outbound messages */
4820 GNUNET_NO, /* For header-only out notification */
4821 core_handlers); /* Register these handlers */
4822 if (core_handle == NULL)
4825 GNUNET_SCHEDULER_shutdown ();
4830 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4833 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4834 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
4835 GNUNET_SERVER_disconnect_notify (server_handle,
4836 &handle_local_client_disconnect, NULL);
4839 clients_head = NULL;
4840 clients_tail = NULL;
4843 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4845 /* Create a peer_info for the local peer */
4846 peer = peer_get (&my_full_id);
4849 GNUNET_PEER_change_rc (myid, 1);
4850 peer_info_add_path (peer, p, GNUNET_YES);
4851 GNUNET_SERVER_resume (server_handle);
4852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
4857 * Process mesh requests.
4859 * @param cls closure
4860 * @param server the initialized server
4861 * @param c configuration to use
4864 run (void *cls, struct GNUNET_SERVER_Handle *server,
4865 const struct GNUNET_CONFIGURATION_Handle *c)
4869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4870 server_handle = server;
4873 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
4876 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4878 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4879 "mesh", "peer/privatekey");
4880 GNUNET_SCHEDULER_shutdown ();
4885 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
4886 &refresh_path_time))
4888 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4890 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4891 "mesh", "refresh path time");
4892 GNUNET_SCHEDULER_shutdown ();
4897 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
4900 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4902 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4903 "mesh", "id announce time");
4904 GNUNET_SCHEDULER_shutdown ();
4909 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
4912 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4914 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4915 "mesh", "connect timeout");
4916 GNUNET_SCHEDULER_shutdown ();
4921 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
4924 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4926 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4927 "mesh", "max msgs queue");
4928 GNUNET_SCHEDULER_shutdown ();
4933 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
4936 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4938 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4939 "mesh", "max tunnels");
4940 GNUNET_SCHEDULER_shutdown ();
4945 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
4948 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4950 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4951 "mesh", "default ttl", 64);
4956 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
4959 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4960 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4961 "mesh", "max peers", 1000);
4966 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
4967 &dht_replication_level))
4969 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4971 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4972 "mesh", "dht replication level", 3);
4973 dht_replication_level = 3;
4976 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4977 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4978 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4979 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4981 dht_handle = GNUNET_DHT_connect (c, 64);
4982 if (NULL == dht_handle)
4986 stats = GNUNET_STATISTICS_create ("mesh", c);
4988 GNUNET_SERVER_suspend (server_handle);
4989 /* Scheduled the task to clean up when shutdown is called */
4990 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4992 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
4995 GNUNET_free (keyfile);
5000 * The main function for the mesh service.
5002 * @param argc number of arguments from the command line
5003 * @param argv command line arguments
5004 * @return 0 ok, 1 on error
5007 main (int argc, char *const *argv)
5012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5013 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5015 ret = (GNUNET_OK == r) ? 0 : 1;
5016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5020 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5021 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5022 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);