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);
471 unsigned int debug_fwd_ack;
472 unsigned int debug_bck_ack;
476 /******************************************************************************/
477 /*********************** GLOBAL VARIABLES ****************************/
478 /******************************************************************************/
480 /************************** Configuration parameters **************************/
483 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
485 static struct GNUNET_TIME_Relative refresh_path_time;
488 * How often to PUT own ID in the DHT.
490 static struct GNUNET_TIME_Relative id_announce_time;
493 * Maximum time allowed to connect to a peer found by string.
495 static struct GNUNET_TIME_Relative connect_timeout;
498 * Default TTL for payload packets.
500 static unsigned long long default_ttl;
503 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
505 static unsigned long long dht_replication_level;
508 * How many tunnels are we willing to maintain.
509 * Local tunnels are always allowed, even if there are more tunnels than max.
511 static unsigned long long max_tunnels;
514 * How many messages *in total* are we willing to queue, divided by number of
515 * tunnels to get tunnel queue size.
517 static unsigned long long max_msgs_queue;
520 * How many peers do we want to remember?
522 static unsigned long long max_peers;
525 /*************************** Static global variables **************************/
528 * Hostkey generation context
530 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
533 * DLL with all the clients, head.
535 static struct MeshClient *clients_head;
538 * DLL with all the clients, tail.
540 static struct MeshClient *clients_tail;
543 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
545 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
548 * Number of tunnels known.
550 static unsigned long long n_tunnels;
553 * Tunnels incoming, indexed by MESH_TunnelNumber
554 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
556 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
559 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
561 static struct GNUNET_CONTAINER_MultiHashMap *peers;
564 * Handle to communicate with transport
566 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
569 * Handle to communicate with core.
571 static struct GNUNET_CORE_Handle *core_handle;
576 static struct GNUNET_DHT_Handle *dht_handle;
581 static struct GNUNET_SERVER_Handle *server_handle;
584 * Handle to the statistics service.
586 static struct GNUNET_STATISTICS_Handle *stats;
589 * Notification context, to send messages to local clients.
591 static struct GNUNET_SERVER_NotificationContext *nc;
594 * Local peer own ID (memory efficient handle).
596 static GNUNET_PEER_Id myid;
599 * Local peer own ID (full value).
601 static struct GNUNET_PeerIdentity my_full_id;
606 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
611 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
614 * Tunnel ID for the next created tunnel (global tunnel number).
616 static MESH_TunnelNumber next_tid;
619 * Tunnel ID for the next incoming tunnel (local tunnel number).
621 static MESH_TunnelNumber next_local_tid;
624 * All message types clients of this peer are interested in.
626 static struct GNUNET_CONTAINER_MultiHashMap *types;
629 * Task to periodically announce itself in the network.
631 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
634 * Next ID to assign to a client.
636 unsigned int next_client_id;
639 /******************************************************************************/
640 /*********************** DECLARATIONS **************************/
641 /******************************************************************************/
644 * Function to process paths received for a new peer addition. The recorded
645 * paths form the initial tunnel, which can be optimized later.
646 * Called on each result obtained for the DHT search.
649 * @param exp when will this value expire
650 * @param key key of the result
651 * @param type type of the result
652 * @param size number of bytes in data
653 * @param data pointer to the result data
656 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
657 const struct GNUNET_HashCode * key,
658 const struct GNUNET_PeerIdentity *get_path,
659 unsigned int get_path_length,
660 const struct GNUNET_PeerIdentity *put_path,
661 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
662 size_t size, const void *data);
666 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
667 * and insert it in the appropiate structures if the peer is not known yet.
669 * @param peer Full identity of the peer.
671 * @return Existing or newly created peer info.
673 static struct MeshPeerInfo *
674 peer_get (const struct GNUNET_PeerIdentity *peer);
678 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
679 * and insert it in the appropiate structures if the peer is not known yet.
681 * @param peer Short identity of the peer.
683 * @return Existing or newly created peer info.
685 static struct MeshPeerInfo *
686 peer_get_short (const GNUNET_PEER_Id peer);
690 * Build a PeerPath from the paths returned from the DHT, reversing the paths
691 * to obtain a local peer -> destination path and interning the peer ids.
693 * @return Newly allocated and created path
695 static struct MeshPeerPath *
696 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
697 unsigned int get_path_length,
698 const struct GNUNET_PeerIdentity *put_path,
699 unsigned int put_path_length);
703 * Adds a path to the peer_infos of all the peers in the path
705 * @param p Path to process.
706 * @param confirmed Whether we know if the path works or not.
709 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
714 * Search for a tunnel by global ID using full PeerIdentities.
716 * @param oid owner of the tunnel.
717 * @param tid global tunnel number.
719 * @return tunnel handler, NULL if doesn't exist.
721 static struct MeshTunnel *
722 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
726 * Notify a tunnel that a connection has broken that affects at least
729 * @param t Tunnel affected.
730 * @param p1 Peer that got disconnected from p2.
731 * @param p2 Peer that got disconnected from p1.
733 * @return Short ID of the peer disconnected (either p1 or p2).
734 * 0 if the tunnel remained unaffected.
736 static GNUNET_PEER_Id
737 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
742 * Use the given path for the tunnel.
744 * @param t Tunnel to update.
745 * @param p Path to use.
748 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
751 * @brief Queue and pass message to core when possible.
753 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
754 * and accounts for it. In case the queue is full, the message is dropped and
757 * Otherwise, message is treated as internal and allowed to go regardless of
760 * @param cls Closure (@c type dependant). It will be used by queue_send to
761 * build the message to be sent if not already prebuilt.
762 * @param type Type of the message, 0 for a raw message.
763 * @param size Size of the message.
764 * @param dst Neighbor to send message to.
765 * @param t Tunnel this message belongs to.
768 queue_add (void *cls, uint16_t type, size_t size,
769 struct MeshPeerInfo *dst, struct MeshTunnel *t);
773 * Free a transmission that was already queued with all resources
774 * associated to the request.
776 * @param queue Queue handler to cancel.
777 * @param clear_cls Is it necessary to free associated cls?
780 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
784 * @brief Get the next transmittable message from the queue.
786 * This will be the head, except in the case of being a data packet
787 * not allowed by the destination peer.
789 * @param peer Destination peer.
791 * @return The next viable MeshPeerQueue element to send to that peer.
792 * NULL when there are no transmittable messages.
794 struct MeshPeerQueue *
795 queue_get_next (const struct MeshPeerInfo *peer);
799 * Core callback to write a queued packet to core buffer
801 * @param cls Closure (peer info).
802 * @param size Number of bytes available in buf.
803 * @param buf Where the to write the message.
805 * @return number of bytes written to buf
808 queue_send (void *cls, size_t size, void *buf);
811 /******************************************************************************/
812 /************************ PERIODIC FUNCTIONS ****************************/
813 /******************************************************************************/
816 * Periodically announce self id in the DHT
819 * @param tc task context
822 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
826 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
828 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
832 * - Set data expiration in function of X
835 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
837 block.id = my_full_id;
838 block.type = htonl (0);
839 GNUNET_DHT_put (dht_handle, /* DHT handle */
840 &my_full_id.hashPubKey, /* Key to use */
841 dht_replication_level, /* Replication level */
842 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
843 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
844 sizeof (block), /* Size of the data */
845 (const char *) &block, /* Data itself */
846 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
847 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
848 NULL, /* Continuation */
849 NULL); /* Continuation closure */
851 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
855 /******************************************************************************/
856 /****************** GENERAL HELPER FUNCTIONS ************************/
857 /******************************************************************************/
861 * Check if client has registered with the service and has not disconnected
863 * @param client the client to check
865 * @return non-NULL if client exists in the global DLL
867 static struct MeshClient *
868 client_get (struct GNUNET_SERVER_Client *client)
870 struct MeshClient *c;
875 if (c->handle == client)
884 * Deletes a tunnel from a client (either owner or destination). To be used on
887 * @param c Client whose tunnel to delete.
888 * @param t Tunnel which should be deleted.
891 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
893 struct GNUNET_HashCode hash;
897 GMC_hash32 (t->local_tid, &hash);
898 GNUNET_assert (GNUNET_YES ==
899 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
903 else if (c == t->client)
905 GMC_hash32 (t->local_tid_dest, &hash);
906 GNUNET_assert (GNUNET_YES ==
907 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
919 * Notify all clients (not depending on registration status) that the incoming
920 * tunnel is no longer valid.
922 * @param t Tunnel that was destroyed.
925 send_client_tunnel_destroy (struct MeshTunnel *t)
927 struct GNUNET_MESH_TunnelMessage msg;
929 if (NULL == t->client)
931 msg.header.size = htons (sizeof (msg));
932 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
933 msg.tunnel_id = htonl (t->local_tid_dest);
934 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
935 &msg.header, GNUNET_NO);
940 * Iterator over all the peers to remove the oldest not-used entry.
942 * @param cls Closure (unsued).
943 * @param key ID of the peer.
944 * @param value Peer_Info of the peer.
949 peer_info_timeout (void *cls,
950 const struct GNUNET_HashCode *key,
957 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
958 * and insert it in the appropiate structures if the peer is not known yet.
960 * @param peer Full identity of the peer.
962 * @return Existing or newly created peer info.
964 static struct MeshPeerInfo *
965 peer_get (const struct GNUNET_PeerIdentity *peer)
967 struct MeshPeerInfo *peer_info;
969 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
970 if (NULL == peer_info)
973 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
974 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
976 GNUNET_CONTAINER_multihashmap_iterate (peers,
980 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
981 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
982 peer_info->id = GNUNET_PEER_intern (peer);
984 peer_info->last_contact = GNUNET_TIME_absolute_get();
991 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
992 * and insert it in the appropiate structures if the peer is not known yet.
994 * @param peer Short identity of the peer.
996 * @return Existing or newly created peer info.
998 static struct MeshPeerInfo *
999 peer_get_short (const GNUNET_PEER_Id peer)
1001 struct GNUNET_PeerIdentity id;
1003 GNUNET_PEER_resolve (peer, &id);
1004 return peer_get (&id);
1009 * Choose the best path towards a peer considering the tunnel properties.
1011 * @param peer The destination peer.
1012 * @param t The tunnel the path is for.
1014 * @return Best current known path towards the peer, if any.
1016 static struct MeshPeerPath *
1017 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1019 struct MeshPeerPath *best_p;
1020 struct MeshPeerPath *p;
1021 unsigned int best_cost;
1024 best_p = p = peer->path_head;
1025 best_cost = cost = p->length;
1028 if ((cost = p->length) < best_cost)
1040 * Remove the tunnel from the list of tunnels to which a peer is target.
1042 * @param peer PeerInfo of the peer.
1043 * @param t Tunnel to remove.
1046 peer_remove_tunnel (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1050 for (i = 0; i < peer->ntunnels; i++)
1053 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1056 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1058 GNUNET_realloc (peer->tunnels,
1059 peer->ntunnels * sizeof(struct MeshTunnel *));
1067 * Core callback to write a pre-constructed data packet to core buffer
1069 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1070 * @param size Number of bytes available in buf.
1071 * @param buf Where the to write the message.
1073 * @return number of bytes written to buf
1076 send_core_data_raw (void *cls, size_t size, void *buf)
1078 struct GNUNET_MessageHeader *msg = cls;
1081 GNUNET_assert (NULL != msg);
1082 total_size = ntohs (msg->size);
1084 if (total_size > size)
1089 memcpy (buf, msg, total_size);
1096 * Sends an already built message to a peer, properly registrating
1097 * all used resources.
1099 * @param message Message to send. Function makes a copy of it.
1100 * @param peer Short ID of the neighbor whom to send the message.
1101 * @param t Tunnel on which this message is transmitted.
1104 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1105 GNUNET_PEER_Id peer,
1106 struct MeshTunnel *t)
1108 struct GNUNET_PeerIdentity id;
1109 struct MeshPeerInfo *neighbor;
1110 struct MeshPeerPath *p;
1115 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1120 size = ntohs (message->size);
1121 data = GNUNET_malloc (size);
1122 memcpy (data, message, size);
1123 type = ntohs(message->type);
1126 struct GNUNET_MESH_Unicast *u;
1128 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1129 u = (struct GNUNET_MESH_Unicast *) data;
1130 u->ttl = htonl (ntohl (u->ttl) - 1);
1133 GNUNET_PEER_resolve (peer, &id);
1134 neighbor = peer_get (&id);
1135 for (p = neighbor->path_head; NULL != p; p = p->next)
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 " %s IS NOT DIRECTLY CONNECTED\n",
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1151 for (p = neighbor->path_head; NULL != p; p = p->next)
1153 struct GNUNET_PeerIdentity debug_id;
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1157 " path with %u hops through:\n",
1159 for (i = 0; i < p->length; i++)
1161 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 i, GNUNET_i2s(&debug_id));
1168 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1169 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1170 " no direct connection to %s\n",
1175 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1186 * Sends a CREATE PATH message for a path to a peer, properly registrating
1187 * all used resources.
1189 * @param t Tunnel for which the path is created.
1192 send_create_path (struct MeshTunnel *t)
1194 struct MeshPeerInfo *neighbor;
1196 neighbor = peer_get_short (t->next_hop);
1198 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1199 sizeof (struct GNUNET_MESH_ManipulatePath) +
1200 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1203 t->state = MESH_TUNNEL_WAITING;
1208 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1210 * @param t Tunnel which to confirm.
1213 send_path_ack (struct MeshTunnel *t)
1215 struct MeshPeerInfo *peer;
1217 if (0 == t->prev_hop)
1223 peer = peer_get_short (t->prev_hop);
1226 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1227 sizeof (struct GNUNET_MESH_PathACK),
1234 * Try to establish a new connection to this peer in the given tunnel.
1235 * If the peer doesn't have any path to it yet, try to get one.
1236 * If the peer already has some path, send a CREATE PATH towards it.
1238 * @param peer PeerInfo of the peer.
1239 * @param t Tunnel for which to create the path, if possible.
1242 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1244 struct MeshPeerPath *p;
1246 if (NULL != peer->path_head)
1248 p = peer_get_best_path (peer, t);
1249 tunnel_use_path (t, p);
1250 send_create_path (t);
1252 else if (NULL == peer->dhtget)
1254 struct GNUNET_PeerIdentity id;
1256 GNUNET_PEER_resolve (peer->id, &id);
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1259 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1260 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1261 &id.hashPubKey, /* key to search */
1262 dht_replication_level, /* replication level */
1263 GNUNET_DHT_RO_RECORD_ROUTE |
1264 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1266 0, /* xquery bits */
1267 &dht_get_id_handler, peer);
1268 t->state = MESH_TUNNEL_SEARCHING;
1270 /* Otherwise, there is no path but the DHT get is already started. */
1275 * Destroy the peer_info and free any allocated resources linked to it
1277 * @param pi The peer_info to destroy.
1279 * @return GNUNET_OK on success
1282 peer_info_destroy (struct MeshPeerInfo *pi)
1284 struct GNUNET_PeerIdentity id;
1285 struct MeshPeerPath *p;
1286 struct MeshPeerPath *nextp;
1288 GNUNET_PEER_resolve (pi->id, &id);
1289 GNUNET_PEER_change_rc (pi->id, -1);
1292 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1295 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1296 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1298 if (NULL != pi->dhtget)
1300 GNUNET_DHT_get_stop (pi->dhtget);
1306 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1316 * Remove all paths that rely on a direct connection between p1 and p2
1317 * from the peer itself and notify all tunnels about it.
1319 * @param peer PeerInfo of affected peer.
1320 * @param p1 GNUNET_PEER_Id of one peer.
1321 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1324 * TODO: optimize (see below)
1327 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1330 struct MeshPeerPath *p;
1331 struct MeshPeerPath *next;
1332 struct MeshPeerInfo *peer_d;
1334 unsigned int destroyed;
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1339 for (p = peer->path_head; NULL != p; p = next)
1342 for (i = 0; i < (p->length - 1); i++)
1344 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1345 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1347 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1357 for (i = 0; i < peer->ntunnels; i++)
1359 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1363 peer_d = peer_get_short (d);
1364 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1365 tunnel_use_path (peer->tunnels[i], next);
1366 peer_connect (peer_d, peer->tunnels[i]);
1368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1373 * Add the path to the peer and update the path used to reach it in case this
1376 * @param peer_info Destination peer to add the path to.
1377 * @param path New path to add. Last peer must be the peer in arg 1.
1378 * Path will be either used of freed if already known.
1379 * @param trusted Do we trust that this path is real?
1382 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1385 struct MeshPeerPath *aux;
1389 if ((NULL == peer_info) || (NULL == path))
1392 path_destroy (path);
1395 if (path->peers[path->length - 1] != peer_info->id)
1398 path_destroy (path);
1401 if (path->length <= 2 && GNUNET_NO == trusted)
1403 /* Only allow CORE to tell us about direct paths */
1404 path_destroy (path);
1407 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1408 for (l = 1; l < path->length; l++)
1410 if (path->peers[l] == myid)
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1413 for (l2 = 0; l2 < path->length - l; l2++)
1415 path->peers[l2] = path->peers[l + l2];
1420 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1425 struct GNUNET_PeerIdentity id;
1427 GNUNET_PEER_resolve (peer_info->id, &id);
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1429 path->length, GNUNET_i2s (&id));
1432 l = path_get_length (path);
1439 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1440 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1442 l2 = path_get_length (aux);
1445 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1446 peer_info->path_tail, aux, path);
1451 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1453 path_destroy (path);
1458 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1465 * Add the path to the origin peer and update the path used to reach it in case
1466 * this is the shortest.
1467 * The path is given in peer_info -> destination, therefore we turn the path
1468 * upside down first.
1470 * @param peer_info Peer to add the path to, being the origin of the path.
1471 * @param path New path to add after being inversed.
1472 * @param trusted Do we trust that this path is real?
1475 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1476 struct MeshPeerPath *path, int trusted)
1479 peer_info_add_path (peer_info, path, trusted);
1484 * Function called if the connection to the peer has been stalled for a while,
1485 * possibly due to a missed ACK. Poll the peer about its ACK status.
1487 * @param cls Closure (poll ctx).
1488 * @param tc TaskContext.
1491 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1493 struct MeshFlowControl *fc = cls;
1494 struct GNUNET_MESH_Poll msg;
1495 struct MeshTunnel *t = fc->t;
1496 GNUNET_PEER_Id peer;
1498 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1499 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1504 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1505 msg.header.size = htons (sizeof (msg));
1506 msg.tid = htonl (t->id.tid);
1507 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1509 if (fc == &t->prev_fc)
1513 else if (fc == &t->next_fc)
1522 send_prebuilt_message (&msg.header, peer, t);
1523 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1524 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1530 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1531 * to obtain a local peer -> destination path and interning the peer ids.
1533 * @return Newly allocated and created path
1535 static struct MeshPeerPath *
1536 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1537 unsigned int get_path_length,
1538 const struct GNUNET_PeerIdentity *put_path,
1539 unsigned int put_path_length)
1541 struct MeshPeerPath *p;
1547 GNUNET_PEER_change_rc (myid, 1);
1548 i = get_path_length;
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1550 for (i--; i >= 0; i--)
1552 id = GNUNET_PEER_intern (&get_path[i]);
1553 if (p->length > 0 && id == p->peers[p->length - 1])
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1556 GNUNET_PEER_change_rc (id, -1);
1560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1561 GNUNET_i2s (&get_path[i]));
1563 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1564 p->peers[p->length - 1] = id;
1567 i = put_path_length;
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1569 for (i--; i >= 0; i--)
1571 id = GNUNET_PEER_intern (&put_path[i]);
1574 /* PUT path went through us, so discard the path up until now and start
1575 * from here to get a much shorter (and loop-free) path.
1580 if (p->length > 0 && id == p->peers[p->length - 1])
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1583 GNUNET_PEER_change_rc (id, -1);
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1588 GNUNET_i2s (&put_path[i]));
1590 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1591 p->peers[p->length - 1] = id;
1595 if (get_path_length > 0)
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1597 GNUNET_i2s (&get_path[0]));
1598 if (put_path_length > 0)
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1600 GNUNET_i2s (&put_path[0]));
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1603 for (i = 0; i < p->length; i++)
1605 struct GNUNET_PeerIdentity peer_id;
1607 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1609 GNUNET_i2s (&peer_id));
1617 * Adds a path to the peer_infos of all the peers in the path
1619 * @param p Path to process.
1620 * @param confirmed Whether we know if the path works or not.
1623 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1627 /* TODO: invert and add */
1628 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1629 for (i++; i < p->length; i++)
1631 struct MeshPeerInfo *aux;
1632 struct MeshPeerPath *copy;
1634 aux = peer_get_short (p->peers[i]);
1635 copy = path_duplicate (p);
1636 copy->length = i + 1;
1637 peer_info_add_path (aux, copy, GNUNET_NO);
1643 * Send keepalive packets for a peer
1645 * @param cls Closure (tunnel for which to send the keepalive).
1646 * @param tc Notification context.
1649 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1653 * Search for a tunnel among the incoming tunnels
1655 * @param tid the local id of the tunnel
1657 * @return tunnel handler, NULL if doesn't exist
1659 static struct MeshTunnel *
1660 tunnel_get_incoming (MESH_TunnelNumber tid)
1662 struct GNUNET_HashCode hash;
1664 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1665 GMC_hash32 (tid, &hash);
1666 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1671 * Search for a tunnel among the tunnels for a client
1673 * @param c the client whose tunnels to search in
1674 * @param tid the local id of the tunnel
1676 * @return tunnel handler, NULL if doesn't exist
1678 static struct MeshTunnel *
1679 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1681 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1683 return tunnel_get_incoming (tid);
1687 struct GNUNET_HashCode hash;
1689 GMC_hash32 (tid, &hash);
1690 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1696 * Search for a tunnel by global ID using PEER_ID
1698 * @param pi owner of the tunnel
1699 * @param tid global tunnel number
1701 * @return tunnel handler, NULL if doesn't exist
1703 static struct MeshTunnel *
1704 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1706 struct MESH_TunnelID id;
1707 struct GNUNET_HashCode hash;
1712 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1713 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1718 * Search for a tunnel by global ID using full PeerIdentities
1720 * @param oid owner of the tunnel
1721 * @param tid global tunnel number
1723 * @return tunnel handler, NULL if doesn't exist
1725 static struct MeshTunnel *
1726 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1728 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1733 * Add a client to a tunnel, initializing all needed data structures.
1735 * FIXME: make static after implementing port numbers
1737 * @param t Tunnel to which add the client.
1738 * @param c Client which to add to the tunnel.
1741 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1743 if (NULL != t->client)
1748 if (0 != t->next_hop)
1758 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1762 for (i = 0; i < p->length; i++)
1764 if (p->peers[i] == myid)
1767 if (i > p->length - 1)
1773 if (i < p->length - 1)
1774 t->next_hop = p->peers[i + 1];
1778 t->prev_hop = p->peers[i - 1];
1782 if (NULL != t->path)
1783 path_destroy (t->path);
1784 t->path = path_duplicate (p);
1785 if (GNUNET_SCHEDULER_NO_TASK == t->maintenance_task)
1786 t->maintenance_task =
1787 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
1792 * Notifies a tunnel that a connection has broken that affects at least
1793 * some of its peers. Sends a notification towards the root of the tree.
1794 * In case the peer is the owner of the tree, notifies the client that owns
1795 * the tunnel and tries to reconnect.
1797 * @param t Tunnel affected.
1798 * @param p1 Peer that got disconnected from p2.
1799 * @param p2 Peer that got disconnected from p1.
1801 * @return Short ID of the peer disconnected (either p1 or p2).
1802 * 0 if the tunnel remained unaffected.
1804 static GNUNET_PEER_Id
1805 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1808 // if (myid != p1 && myid != p2) FIXME
1813 // if (tree_get_predecessor (t->tree) != 0)
1815 // /* We are the peer still connected, notify owner of the disconnection. */
1816 // struct GNUNET_MESH_PathBroken msg;
1817 // struct GNUNET_PeerIdentity neighbor;
1819 // msg.header.size = htons (sizeof (msg));
1820 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1821 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1822 // msg.tid = htonl (t->id.tid);
1823 // msg.peer1 = my_full_id;
1824 // GNUNET_PEER_resolve (pid, &msg.peer2);
1825 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1826 // send_prebuilt_message (&msg.header, &neighbor, t);
1833 * Build a local ACK message and send it to a local client.
1835 * @param t Tunnel on which to send the ACK.
1836 * @param c Client to whom send the ACK.
1837 * @param ack Value of the ACK.
1840 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
1842 struct GNUNET_MESH_LocalAck msg;
1844 msg.header.size = htons (sizeof (msg));
1845 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1846 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
1847 msg.max_pid = htonl (ack);
1848 GNUNET_SERVER_notification_context_unicast(nc,
1855 * Build an ACK message and queue it to send to the given peer.
1857 * @param t Tunnel on which to send the ACK.
1858 * @param peer Peer to whom send the ACK.
1859 * @param ack Value of the ACK.
1862 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1864 struct GNUNET_MESH_ACK msg;
1866 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1867 msg.header.size = htons (sizeof (msg));
1868 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1869 msg.pid = htonl (ack);
1870 msg.tid = htonl (t->id.tid);
1872 send_prebuilt_message (&msg.header, peer, t);
1877 * Send an ACK informing the predecessor about the available buffer space.
1878 * In case there is no predecessor, inform the owning client.
1879 * If buffering is off, send only on behalf of children or self if endpoint.
1880 * If buffering is on, send when sent to children and buffer space is free.
1881 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
1882 * the ACK itself goes "back" (towards root).
1884 * @param t Tunnel on which to send the ACK.
1885 * @param type Type of message that triggered the ACK transmission.
1888 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
1892 /* Is it after unicast retransmission? */
1895 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1897 "ACK due to FWD DATA retransmission\n");
1898 if (GNUNET_YES == t->nobuffer)
1900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
1904 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1905 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1907 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1908 t->force_ack = GNUNET_YES;
1914 /* Check if we need to transmit the ACK */
1915 if (t->queue_max > t->next_fc.queue_n * 4 &&
1916 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
1917 GNUNET_NO == t->force_ack)
1919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
1920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1921 " t->qmax: %u, t->qn: %u\n",
1922 t->queue_max, t->next_fc.queue_n);
1923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1924 " t->pid: %u, t->ack: %u\n",
1925 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
1929 /* Ok, ACK might be necessary, what PID to ACK? */
1930 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
1931 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
1933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
1937 t->prev_fc.last_ack_sent = ack;
1938 if (0 != t->prev_hop)
1939 send_ack (t, t->prev_hop, ack);
1940 else if (NULL != t->owner)
1941 send_local_ack (t, t->owner, ack);
1945 t->force_ack = GNUNET_NO;
1950 * Send an ACK informing the children node/client about the available
1952 * If buffering is off, send only on behalf of root (can be self).
1953 * If buffering is on, send when sent to predecessor and buffer space is free.
1954 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
1955 * the ACK itself goes "forward" (towards children/clients).
1957 * @param t Tunnel on which to send the ACK.
1958 * @param type Type of message that triggered the ACK transmission.
1961 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
1964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1965 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
1966 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
1967 /* Is it after data to_origin retransmission? */
1970 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1971 if (GNUNET_YES == t->nobuffer)
1973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1974 " Not sending ACK, nobuffer + traffic\n");
1978 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1979 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1981 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1982 t->force_ack = GNUNET_YES;
1988 /* TODO: Check if we need to transmit the ACK (as in fwd) */
1990 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
1992 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
1994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1995 " Not sending ACK, not needed\n");
1998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1999 " Sending BCK ACK %u (last sent: %u)\n",
2000 ack, t->next_fc.last_ack_sent);
2001 t->next_fc.last_ack_sent = ack;
2003 if (0 != t->next_hop)
2004 send_ack (t, t->next_hop, ack);
2005 else if (NULL != t->client)
2006 send_local_ack (t, t->client, ack);
2009 t->force_ack = GNUNET_NO;
2014 * @brief Re-initiate traffic to this peer if necessary.
2016 * Check if there is traffic queued towards this peer
2017 * and the core transmit handle is NULL (traffic was stalled).
2018 * If so, call core tmt rdy.
2020 * @param peer_id Short ID of peer to which initiate traffic.
2023 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2025 struct MeshPeerInfo *peer;
2026 struct GNUNET_PeerIdentity id;
2027 struct MeshPeerQueue *q;
2030 peer = peer_get_short (peer_id);
2031 if (NULL != peer->core_transmit)
2034 q = queue_get_next (peer);
2037 /* Might br multicast traffic already sent to this particular peer but
2038 * not to other children in this tunnel.
2039 * This way t->queue_n would be > 0 but the queue of this particular peer
2045 GNUNET_PEER_resolve (peer->id, &id);
2046 peer->core_transmit =
2047 GNUNET_CORE_notify_transmit_ready(core_handle,
2050 GNUNET_TIME_UNIT_FOREVER_REL,
2060 * Send a message to all peers in this tunnel that the tunnel is no longer
2063 * @param t The tunnel whose peers to notify.
2066 tunnel_send_destroy (struct MeshTunnel *t)
2068 struct GNUNET_MESH_TunnelDestroy msg;
2069 struct GNUNET_PeerIdentity id;
2071 msg.header.size = htons (sizeof (msg));
2072 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2073 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2074 msg.tid = htonl (t->id.tid);
2075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2076 " sending tunnel destroy for tunnel: %s [%X]\n",
2077 GNUNET_i2s (&msg.oid), t->id.tid);
2079 if (0 != t->next_hop)
2081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2082 GNUNET_PEER_resolve (t->next_hop, &id);
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2084 " sending forward to %s\n",
2086 send_prebuilt_message (&msg.header, t->next_hop, t);
2088 if (0 != t->prev_hop)
2090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2091 GNUNET_PEER_resolve (t->prev_hop, &id);
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2093 " sending back to %s\n",
2095 send_prebuilt_message (&msg.header, t->prev_hop, t);
2101 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2103 * @param t Tunnel which to cancel.
2104 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2107 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2109 struct MeshPeerInfo *peer_info;
2110 struct MeshPeerQueue *pq;
2111 struct MeshPeerQueue *next;
2113 peer_info = peer_get_short (neighbor);
2114 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2117 if (pq->tunnel == t)
2119 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2120 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2122 /* Should have been removed on destroy children */
2125 queue_destroy (pq, GNUNET_YES);
2128 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2130 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2131 peer_info->core_transmit = NULL;
2137 * Destroy the tunnel and free any allocated resources linked to it.
2139 * @param t the tunnel to destroy
2141 * @return GNUNET_OK on success
2144 tunnel_destroy (struct MeshTunnel *t)
2146 struct MeshClient *c;
2147 struct GNUNET_HashCode hash;
2157 struct GNUNET_PeerIdentity id;
2159 GNUNET_PEER_resolve (t->id.oid, &id);
2160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2161 GNUNET_i2s (&id), t->id.tid);
2163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2167 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2168 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2176 GMC_hash32 (t->local_tid, &hash);
2178 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2185 if (NULL != t->client)
2187 GMC_hash32 (t->local_tid_dest, &hash);
2189 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2196 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2202 peer_cancel_queues (t->next_hop, t);
2203 peer_cancel_queues (t->prev_hop, t);
2205 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2206 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2209 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2214 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
2217 * Tunnel is empty: destroy it.
2219 * @param t Tunnel to destroy.
2222 tunnel_destroy_empty (struct MeshTunnel *t)
2226 struct GNUNET_PeerIdentity id;
2228 GNUNET_PEER_resolve (t->id.oid, &id);
2229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2230 "executing destruction of empty tunnel %s [%X]\n",
2231 GNUNET_i2s (&id), t->id.tid);
2235 tunnel_send_destroy (t);
2236 if (0 == t->pending_messages)
2239 t->destroy = GNUNET_YES;
2243 * Initialize a Flow Control structure to the initial state.
2245 * @param fc Flow Control structure to initialize.
2248 fc_init (struct MeshFlowControl *fc)
2250 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2251 fc->last_pid_recv = (uint32_t) -1;
2252 fc->last_ack_sent = INITIAL_WINDOW_SIZE - 1;
2253 fc->last_ack_recv = INITIAL_WINDOW_SIZE - 1;
2254 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2255 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2260 * Create a new tunnel
2262 * @param owner Who is the owner of the tunnel (short ID).
2263 * @param tid Tunnel Number of the tunnel.
2264 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2265 * @param local Tunnel Number for the tunnel, for the client point of view.
2267 * @return A new initialized tunnel. NULL on error.
2269 static struct MeshTunnel *
2270 tunnel_new (GNUNET_PEER_Id owner,
2271 MESH_TunnelNumber tid,
2272 struct MeshClient *client,
2273 MESH_TunnelNumber local)
2275 struct MeshTunnel *t;
2276 struct GNUNET_HashCode hash;
2278 if (n_tunnels >= max_tunnels && NULL == client)
2281 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2284 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2286 fc_init (&t->next_fc);
2287 fc_init (&t->prev_fc);
2288 t->local_tid = local;
2290 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2292 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2294 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2295 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2302 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2309 GMC_hash32 (t->local_tid, &hash);
2311 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2312 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2316 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2327 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2328 * client when the client disconnects. If the client is not the owner, the
2329 * owner will get notified if no more clients are in the tunnel and the client
2330 * get removed from the tunnel's list.
2332 * @param cls closure (client that is disconnecting)
2333 * @param key the hash of the local tunnel id (used to access the hashmap)
2334 * @param value the value stored at the key (tunnel to destroy)
2336 * @return GNUNET_OK, keep iterating.
2339 tunnel_destroy_iterator (void *cls,
2340 const struct GNUNET_HashCode * key,
2343 struct MeshTunnel *t = value;
2344 struct MeshClient *c = cls;
2346 send_client_tunnel_destroy (t);
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
2350 client_delete_tunnel (c, t);
2351 tunnel_destroy_empty (t);
2354 tunnel_send_destroy (t);
2356 t->destroy = GNUNET_YES;
2363 * Timeout function, destroys tunnel if called
2365 * @param cls Closure (tunnel to destroy).
2366 * @param tc TaskContext
2369 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2371 struct MeshTunnel *t = cls;
2372 struct GNUNET_PeerIdentity id;
2374 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2375 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2377 GNUNET_PEER_resolve(t->id.oid, &id);
2378 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2379 "Tunnel %s [%X] timed out. Destroying.\n",
2380 GNUNET_i2s(&id), t->id.tid);
2381 send_client_tunnel_destroy (t);
2387 * Resets the tunnel timeout. Starts it if no timeout was running.
2389 * @param t Tunnel whose timeout to reset.
2391 * TODO use heap to improve efficiency of scheduler.
2394 tunnel_reset_timeout (struct MeshTunnel *t)
2396 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2398 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2399 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2400 t->maintenance_task =
2401 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2402 (refresh_path_time, 4), &tunnel_timeout, t);
2406 /******************************************************************************/
2407 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2408 /******************************************************************************/
2411 * Function to send a create path packet to a peer.
2413 * @param cls closure
2414 * @param size number of bytes available in buf
2415 * @param buf where the callee should write the message
2416 * @return number of bytes written to buf
2419 send_core_path_create (void *cls, size_t size, void *buf)
2421 struct MeshTunnel *t = cls;
2422 struct GNUNET_MESH_ManipulatePath *msg;
2423 struct GNUNET_PeerIdentity *peer_ptr;
2424 struct MeshPeerPath *p = t->path;
2429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2431 sizeof (struct GNUNET_MESH_ManipulatePath) +
2432 p->length * sizeof (struct GNUNET_PeerIdentity);
2434 if (size < size_needed || NULL == buf)
2439 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2440 msg->header.size = htons (size_needed);
2441 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2442 msg->tid = ntohl (t->id.tid);
2445 if (GNUNET_YES == t->nobuffer)
2446 opt |= MESH_TUNNEL_OPT_NOBUFFER;
2447 msg->opt = htonl(opt);
2450 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2451 for (i = 0; i < p->length; i++)
2453 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2457 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2463 * Creates a path ack message in buf and frees all unused resources.
2465 * @param cls closure (MeshTransmissionDescriptor)
2466 * @param size number of bytes available in buf
2467 * @param buf where the callee should write the message
2468 * @return number of bytes written to buf
2471 send_core_path_ack (void *cls, size_t size, void *buf)
2473 struct MESH_TunnelID *id = cls;
2474 struct GNUNET_MESH_PathACK *msg = buf;
2476 GNUNET_assert (NULL != id);
2477 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2482 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2483 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2484 GNUNET_PEER_resolve (id->oid, &msg->oid);
2485 msg->tid = htonl (id->tid);
2486 msg->peer_id = my_full_id;
2488 /* TODO add signature */
2490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2491 return sizeof (struct GNUNET_MESH_PathACK);
2496 * Free a transmission that was already queued with all resources
2497 * associated to the request.
2499 * @param queue Queue handler to cancel.
2500 * @param clear_cls Is it necessary to free associated cls?
2503 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2505 struct MeshFlowControl *fc;
2507 if (GNUNET_YES == clear_cls)
2509 switch (queue->type)
2511 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2512 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2513 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2515 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2516 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2517 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2518 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2519 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2521 " prebuilt message\n");
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2524 GNUNET_MESH_DEBUG_M2S (queue->type));
2526 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2531 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2532 " type %s unknown!\n",
2533 GNUNET_MESH_DEBUG_M2S (queue->type));
2535 GNUNET_free_non_null (queue->cls);
2537 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2538 queue->peer->queue_tail,
2541 /* Delete from appropiate fc in the tunnel */
2542 if (queue->peer->id == queue->tunnel->next_hop)
2543 fc = &queue->tunnel->next_fc;
2544 else if (queue->peer->id == queue->tunnel->next_hop)
2545 fc = &queue->tunnel->next_fc;
2552 GNUNET_free (queue);
2557 * @brief Get the next transmittable message from the queue.
2559 * This will be the head, except in the case of being a data packet
2560 * not allowed by the destination peer.
2562 * @param peer Destination peer.
2564 * @return The next viable MeshPeerQueue element to send to that peer.
2565 * NULL when there are no transmittable messages.
2567 struct MeshPeerQueue *
2568 queue_get_next (const struct MeshPeerInfo *peer)
2570 struct MeshPeerQueue *q;
2572 struct GNUNET_MESH_Unicast *ucast;
2573 struct GNUNET_MESH_ToOrigin *to_orig;
2574 struct MeshTunnel* t;
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
2579 for (q = peer->queue_head; NULL != q; q = q->next)
2582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2584 GNUNET_MESH_DEBUG_M2S(q->type));
2587 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2588 ucast = (struct GNUNET_MESH_Unicast *) q->cls;
2589 pid = ntohl (ucast->pid);
2590 ack = t->next_fc.last_ack_recv;
2592 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2593 to_orig = (struct GNUNET_MESH_ToOrigin *) q->cls;
2594 pid = ntohl (to_orig->pid);
2595 ack = t->prev_fc.last_ack_recv;
2598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2603 "********* ACK: %u, PID: %u\n",
2605 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2614 "********* NEXT!\n");
2617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2618 "********* nothing found\n");
2624 queue_send (void *cls, size_t size, void *buf)
2626 struct MeshPeerInfo *peer = cls;
2627 struct GNUNET_MessageHeader *msg;
2628 struct MeshPeerQueue *queue;
2629 struct MeshTunnel *t;
2630 struct GNUNET_PeerIdentity dst_id;
2631 struct MeshFlowControl *fc;
2635 peer->core_transmit = NULL;
2637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
2638 queue = queue_get_next (peer);
2640 /* Queue has no internal mesh traffic nor sendable payload */
2643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
2644 if (NULL == peer->queue_head)
2645 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
2648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
2650 GNUNET_PEER_resolve (peer->id, &dst_id);
2651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2652 "********* towards %s\n",
2653 GNUNET_i2s (&dst_id));
2654 /* Check if buffer size is enough for the message */
2655 if (queue->size > size)
2657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2658 "********* not enough room, reissue\n");
2659 peer->core_transmit =
2660 GNUNET_CORE_notify_transmit_ready (core_handle,
2663 GNUNET_TIME_UNIT_FOREVER_REL,
2670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
2673 GNUNET_assert (0 < t->pending_messages);
2674 t->pending_messages--;
2678 switch (queue->type)
2681 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2682 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2683 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
2684 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
2685 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2686 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2688 "********* raw: %s\n",
2689 GNUNET_MESH_DEBUG_M2S (queue->type));
2691 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2692 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2693 data_size = send_core_data_raw (queue->cls, size, buf);
2694 msg = (struct GNUNET_MessageHeader *) buf;
2695 type = ntohs (msg->type);
2697 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
2699 data_size = send_core_path_create (queue->cls, size, buf);
2701 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
2703 data_size = send_core_path_ack (queue->cls, size, buf);
2707 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2708 "********* type unknown: %u\n",
2713 /* Free queue, but cls was freed by send_core_* */
2714 queue_destroy (queue, GNUNET_NO);
2716 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
2719 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2720 t->next_fc.last_pid_sent = ((struct GNUNET_MESH_Unicast *) buf)->pid;
2721 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
2723 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2724 t->prev_fc.last_pid_sent = ((struct GNUNET_MESH_ToOrigin *) buf)->pid;
2725 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
2731 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
2733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
2737 /* If more data in queue, send next */
2738 queue = queue_get_next (peer);
2741 struct GNUNET_PeerIdentity id;
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
2744 GNUNET_PEER_resolve (peer->id, &id);
2745 peer->core_transmit =
2746 GNUNET_CORE_notify_transmit_ready(core_handle,
2749 GNUNET_TIME_UNIT_FOREVER_REL,
2755 else if (NULL != peer->queue_head)
2757 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2758 "********* %s stalled\n",
2759 GNUNET_i2s (&my_full_id));
2760 if (peer->id == t->next_hop)
2762 else if (peer->id == t->prev_hop)
2769 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
2772 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Return %d\n", data_size);
2782 * @brief Queue and pass message to core when possible.
2784 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
2785 * accounts for it. In case the queue is full, the message is dropped and
2788 * Otherwise, message is treated as internal and allowed to go regardless of
2791 * @param cls Closure (@c type dependant). It will be used by queue_send to
2792 * build the message to be sent if not already prebuilt.
2793 * @param type Type of the message, 0 for a raw message.
2794 * @param size Size of the message.
2795 * @param dst Neighbor to send message to.
2796 * @param t Tunnel this message belongs to.
2799 queue_add (void *cls, uint16_t type, size_t size,
2800 struct MeshPeerInfo *dst, struct MeshTunnel *t)
2802 struct MeshPeerQueue *queue;
2803 struct GNUNET_PeerIdentity id;
2807 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2809 n = &t->next_fc.queue_n;
2811 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
2813 n = &t->prev_fc.queue_n;
2817 if (*n >= t->queue_max)
2820 GNUNET_STATISTICS_update(stats,
2821 "# messages dropped (buffer full)",
2823 return; /* Drop message */
2827 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2833 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
2834 if (NULL == dst->core_transmit)
2836 GNUNET_PEER_resolve (dst->id, &id);
2837 dst->core_transmit =
2838 GNUNET_CORE_notify_transmit_ready (core_handle,
2841 GNUNET_TIME_UNIT_FOREVER_REL,
2847 t->pending_messages++;
2851 /******************************************************************************/
2852 /******************** MESH NETWORK HANDLERS **************************/
2853 /******************************************************************************/
2857 * Core handler for path creation
2859 * @param cls closure
2860 * @param message message
2861 * @param peer peer identity this notification is about
2863 * @return GNUNET_OK to keep the connection open,
2864 * GNUNET_SYSERR to close it (signal serious error)
2867 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2868 const struct GNUNET_MessageHeader *message)
2870 unsigned int own_pos;
2873 MESH_TunnelNumber tid;
2874 struct GNUNET_MESH_ManipulatePath *msg;
2875 struct GNUNET_PeerIdentity *pi;
2876 struct GNUNET_HashCode hash;
2877 struct MeshPeerPath *path;
2878 struct MeshPeerInfo *dest_peer_info;
2879 struct MeshPeerInfo *orig_peer_info;
2880 struct MeshTunnel *t;
2882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2883 "Received a path create msg [%s]\n",
2884 GNUNET_i2s (&my_full_id));
2885 size = ntohs (message->size);
2886 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2888 GNUNET_break_op (0);
2892 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2893 if (size % sizeof (struct GNUNET_PeerIdentity))
2895 GNUNET_break_op (0);
2898 size /= sizeof (struct GNUNET_PeerIdentity);
2901 GNUNET_break_op (0);
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2905 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2907 tid = ntohl (msg->tid);
2908 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2910 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2911 t = tunnel_get (pi, tid);
2916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2917 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
2923 opt = ntohl (msg->opt);
2924 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
2926 t->nobuffer = GNUNET_YES;
2927 t->prev_fc.last_ack_sent = t->prev_fc.last_pid_recv + 1;
2928 t->next_fc.last_ack_sent = t->next_fc.last_pid_recv + 1;
2931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
2933 tunnel_reset_timeout (t);
2934 GMC_hash32 (t->local_tid_dest, &hash);
2936 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2937 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2944 t->state = MESH_TUNNEL_WAITING;
2946 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2947 if (NULL == dest_peer_info)
2949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2950 " Creating PeerInfo for destination.\n");
2951 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2952 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2953 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2955 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2957 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2958 if (NULL == orig_peer_info)
2960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2961 " Creating PeerInfo for origin.\n");
2962 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2963 orig_peer_info->id = GNUNET_PEER_intern (pi);
2964 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2965 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
2968 path = path_new (size);
2970 for (i = 0; i < size; i++)
2972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
2973 GNUNET_i2s (&pi[i]));
2974 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2975 if (path->peers[i] == myid)
2978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
2981 /* cannot be self, must be 'not found' */
2982 /* create path: self not found in path through self */
2983 GNUNET_break_op (0);
2984 path_destroy (path);
2988 path_add_to_peers (path, GNUNET_NO);
2989 t->prev_hop = path->peers[own_pos - 1];
2990 GNUNET_PEER_change_rc (t->prev_hop, 1);
2991 if (own_pos == size - 1)
2993 /* It is for us! Send ack. */
2994 // TODO: check port number / client registration
2995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2996 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2999 while (NULL != tunnel_get_incoming (next_local_tid))
3000 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3001 t->local_tid_dest = next_local_tid++;
3002 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3008 struct MeshPeerPath *path2;
3010 t->next_hop = path->peers[own_pos + 1];
3011 GNUNET_PEER_change_rc(t->next_hop, 1);
3013 /* It's for somebody else! Retransmit. */
3014 path2 = path_duplicate (path);
3015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3016 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3017 path2 = path_duplicate (path);
3018 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3019 send_create_path (t);
3026 * Core handler for path destruction
3028 * @param cls closure
3029 * @param message message
3030 * @param peer peer identity this notification is about
3032 * @return GNUNET_OK to keep the connection open,
3033 * GNUNET_SYSERR to close it (signal serious error)
3036 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3037 const struct GNUNET_MessageHeader *message)
3039 struct GNUNET_MESH_ManipulatePath *msg;
3040 struct GNUNET_PeerIdentity *pi;
3041 struct MeshTunnel *t;
3042 unsigned int own_pos;
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3047 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3048 size = ntohs (message->size);
3049 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3051 GNUNET_break_op (0);
3055 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3056 if (size % sizeof (struct GNUNET_PeerIdentity))
3058 GNUNET_break_op (0);
3061 size /= sizeof (struct GNUNET_PeerIdentity);
3064 GNUNET_break_op (0);
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3069 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3070 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3072 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3074 t = tunnel_get (pi, ntohl (msg->tid));
3077 /* TODO notify back: we don't know this tunnel */
3078 GNUNET_break_op (0);
3082 for (i = 0; i < size; i++)
3084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", i, GNUNET_i2s (&pi[i]));
3085 if (GNUNET_PEER_search (&pi[i]) == myid)
3088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3089 if (own_pos < size - 1)
3090 send_prebuilt_message (message, t->next_hop, t);
3092 send_client_tunnel_destroy (t);
3099 * Core handler for notifications of broken paths
3101 * @param cls closure
3102 * @param message message
3103 * @param peer peer identity this notification is about
3105 * @return GNUNET_OK to keep the connection open,
3106 * GNUNET_SYSERR to close it (signal serious error)
3109 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3110 const struct GNUNET_MessageHeader *message)
3112 struct GNUNET_MESH_PathBroken *msg;
3113 struct MeshTunnel *t;
3115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3116 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3117 msg = (struct GNUNET_MESH_PathBroken *) message;
3118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3119 GNUNET_i2s (&msg->peer1));
3120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3121 GNUNET_i2s (&msg->peer2));
3122 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3125 GNUNET_break_op (0);
3128 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3129 GNUNET_PEER_search (&msg->peer2));
3136 * Core handler for tunnel destruction
3138 * @param cls closure
3139 * @param message message
3140 * @param peer peer identity this notification is about
3142 * @return GNUNET_OK to keep the connection open,
3143 * GNUNET_SYSERR to close it (signal serious error)
3146 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3147 const struct GNUNET_MessageHeader *message)
3149 struct GNUNET_MESH_TunnelDestroy *msg;
3150 struct MeshTunnel *t;
3152 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3154 "Got a TUNNEL DESTROY packet from %s\n",
3156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3157 " for tunnel %s [%u]\n",
3158 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3159 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3162 /* Probably already got the message from another path,
3163 * destroyed the tunnel and retransmitted to children.
3166 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3170 // TODO check owner's signature
3171 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3173 /* Tunnel was incoming, notify clients */
3174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3175 t->local_tid, t->local_tid_dest);
3176 send_client_tunnel_destroy (t);
3178 tunnel_send_destroy (t);
3179 t->destroy = GNUNET_YES;
3180 // TODO: add timeout to destroy the tunnel anyway
3186 * Core handler for mesh network traffic going from the origin to a peer
3188 * @param cls closure
3189 * @param peer peer identity this notification is about
3190 * @param message message
3191 * @return GNUNET_OK to keep the connection open,
3192 * GNUNET_SYSERR to close it (signal serious error)
3195 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3196 const struct GNUNET_MessageHeader *message)
3198 struct GNUNET_MESH_Unicast *msg;
3199 struct MeshTunnel *t;
3204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3207 size = ntohs (message->size);
3209 sizeof (struct GNUNET_MESH_Unicast) +
3210 sizeof (struct GNUNET_MessageHeader))
3215 msg = (struct GNUNET_MESH_Unicast *) message;
3216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3217 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3219 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3222 /* TODO notify back: we don't know this tunnel */
3223 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3224 GNUNET_break_op (0);
3227 pid = ntohl (msg->pid);
3228 if (t->prev_fc.last_pid_recv == pid)
3230 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3231 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3232 " Already seen pid %u, DROPPING!\n", pid);
3237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3238 " pid %u not seen yet, forwarding\n", pid);
3241 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3243 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3244 GNUNET_break_op (0);
3245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3246 "Received PID %u, ACK %u\n",
3247 pid, t->prev_fc.last_ack_sent);
3248 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3251 t->prev_fc.last_pid_recv = pid;
3253 tunnel_reset_timeout (t);
3254 if (t->dest == myid)
3256 if (NULL == t->client)
3261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3262 " it's for us! sending to clients...\n");
3263 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3264 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
3265 message, GNUNET_NO);
3266 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3269 if (0 == t->next_hop)
3274 ttl = ntohl (msg->ttl);
3275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3278 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3279 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3280 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3284 " not for us, retransmitting...\n");
3286 send_prebuilt_message (message, t->next_hop, t);
3287 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3293 * Core handler for mesh network traffic toward the owner of a tunnel
3295 * @param cls closure
3296 * @param message message
3297 * @param peer peer identity this notification is about
3299 * @return GNUNET_OK to keep the connection open,
3300 * GNUNET_SYSERR to close it (signal serious error)
3303 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3304 const struct GNUNET_MessageHeader *message)
3306 struct GNUNET_MESH_ToOrigin *msg;
3307 struct MeshTunnel *t;
3311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3313 size = ntohs (message->size);
3314 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3315 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3317 GNUNET_break_op (0);
3320 msg = (struct GNUNET_MESH_ToOrigin *) message;
3321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3322 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3323 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3324 pid = ntohl (msg->pid);
3328 /* TODO notify that we dont know this tunnel (whom)? */
3329 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3331 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3332 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3336 if (t->next_fc.last_pid_recv == pid)
3338 /* already seen this packet, drop */
3339 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3341 " Already seen pid %u, DROPPING!\n", pid);
3342 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3346 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3348 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3349 GNUNET_break_op (0);
3350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3351 "Received PID %u, ACK %u\n",
3352 pid, t->next_fc.last_ack_sent);
3353 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3356 t->next_fc.last_pid_recv = pid;
3357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3358 " pid %u not seen yet, forwarding\n", pid);
3360 if (NULL != t->owner)
3363 struct GNUNET_MESH_ToOrigin *copy;
3365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3366 " it's for us! sending to clients...\n");
3367 /* TODO signature verification */
3368 memcpy (cbuf, message, size);
3369 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3370 copy->tid = htonl (t->local_tid);
3371 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3372 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3373 ©->header, GNUNET_NO);
3374 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3378 " not for us, retransmitting...\n");
3380 if (0 == t->prev_hop) /* No owner AND no prev hop */
3382 if (GNUNET_YES == t->destroy)
3384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3385 "to orig received on a dying tunnel %s [%X]\n",
3386 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3390 "unknown to origin at %s\n",
3391 GNUNET_i2s (&my_full_id));
3392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3396 "on tunnel %s [%X]\n",
3397 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3400 send_prebuilt_message (message, t->prev_hop, t);
3401 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3408 * Core handler for mesh network traffic point-to-point acks.
3410 * @param cls closure
3411 * @param message message
3412 * @param peer peer identity this notification is about
3414 * @return GNUNET_OK to keep the connection open,
3415 * GNUNET_SYSERR to close it (signal serious error)
3418 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3419 const struct GNUNET_MessageHeader *message)
3421 struct GNUNET_MESH_ACK *msg;
3422 struct MeshTunnel *t;
3426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3428 msg = (struct GNUNET_MESH_ACK *) message;
3430 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3434 /* TODO notify that we dont know this tunnel (whom)? */
3435 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3438 ack = ntohl (msg->pid);
3439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3441 /* Is this a forward or backward ACK? */
3442 id = GNUNET_PEER_search (peer);
3443 if (t->next_hop == id)
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);
3458 else if (t->prev_hop == id)
3461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3462 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
3463 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
3465 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3466 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3467 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3469 t->prev_fc.last_ack_recv = ack;
3470 peer_unlock_queue (t->prev_hop);
3471 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3474 GNUNET_break_op (0);
3480 * Core handler for mesh network traffic point-to-point ack polls.
3482 * @param cls closure
3483 * @param message message
3484 * @param peer peer identity this notification is about
3486 * @return GNUNET_OK to keep the connection open,
3487 * GNUNET_SYSERR to close it (signal serious error)
3490 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3491 const struct GNUNET_MessageHeader *message)
3493 struct GNUNET_MESH_Poll *msg;
3494 struct MeshTunnel *t;
3497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3500 msg = (struct GNUNET_MESH_Poll *) message;
3502 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3506 /* TODO notify that we dont know this tunnel (whom)? */
3507 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
3508 GNUNET_break_op (0);
3512 /* Is this a forward or backward ACK? */
3513 id = GNUNET_PEER_search(peer);
3514 if (t->next_hop == id)
3516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
3517 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3519 else if (t->prev_hop == id)
3521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
3522 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3531 * Core handler for path ACKs
3533 * @param cls closure
3534 * @param message message
3535 * @param peer peer identity this notification is about
3537 * @return GNUNET_OK to keep the connection open,
3538 * GNUNET_SYSERR to close it (signal serious error)
3541 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3542 const struct GNUNET_MessageHeader *message)
3544 struct GNUNET_MESH_PathACK *msg;
3545 struct MeshPeerInfo *peer_info;
3546 struct MeshPeerPath *p;
3547 struct MeshTunnel *t;
3549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3550 GNUNET_i2s (&my_full_id));
3551 msg = (struct GNUNET_MESH_PathACK *) message;
3552 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3555 /* TODO notify that we don't know the tunnel */
3556 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3558 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3562 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3564 peer_info = peer_get (&msg->peer_id);
3565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3566 GNUNET_i2s (&msg->peer_id));
3567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3570 /* Add path to peers? */
3574 path_add_to_peers (p, GNUNET_YES);
3580 t->state = MESH_TUNNEL_READY;
3582 /* Message for us? */
3583 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3586 if (NULL == t->owner)
3588 GNUNET_break_op (0);
3591 if (NULL != peer_info->dhtget)
3593 GNUNET_DHT_get_stop (peer_info->dhtget);
3594 peer_info->dhtget = NULL;
3599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3600 " not for us, retransmitting...\n");
3601 peer_info = peer_get (&msg->oid);
3602 send_prebuilt_message (message, t->prev_hop, t);
3608 * Core handler for mesh keepalives.
3610 * @param cls closure
3611 * @param message message
3612 * @param peer peer identity this notification is about
3613 * @return GNUNET_OK to keep the connection open,
3614 * GNUNET_SYSERR to close it (signal serious error)
3616 * TODO: Check who we got this from, to validate route.
3619 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
3620 const struct GNUNET_MessageHeader *message)
3622 struct GNUNET_MESH_TunnelKeepAlive *msg;
3623 struct MeshTunnel *t;
3625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
3628 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
3629 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3633 /* TODO notify that we dont know that tunnel */
3634 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
3639 tunnel_reset_timeout (t);
3641 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
3642 send_prebuilt_message (message, t->next_hop, t);
3649 * Functions to handle messages from core
3651 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3652 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3653 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3654 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3655 sizeof (struct GNUNET_MESH_PathBroken)},
3656 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
3657 sizeof (struct GNUNET_MESH_TunnelDestroy)},
3658 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3659 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
3660 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
3661 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3662 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
3663 sizeof (struct GNUNET_MESH_ACK)},
3664 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
3665 sizeof (struct GNUNET_MESH_Poll)},
3666 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3667 sizeof (struct GNUNET_MESH_PathACK)},
3673 /******************************************************************************/
3674 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3675 /******************************************************************************/
3680 * notify_client_connection_failure: notify a client that the connection to the
3681 * requested remote peer is not possible (for instance, no route found)
3682 * Function called when the socket is ready to queue more data. "buf" will be
3683 * NULL and "size" zero if the socket was closed for writing in the meantime.
3685 * @param cls closure
3686 * @param size number of bytes available in buf
3687 * @param buf where the callee should write the message
3688 * @return number of bytes written to buf
3691 notify_client_connection_failure (void *cls, size_t size, void *buf)
3694 struct MeshPeerInfo *peer_info;
3695 struct GNUNET_MESH_PeerControl *msg;
3696 struct GNUNET_PeerIdentity id;
3698 if (0 == size && NULL == buf)
3700 // TODO retry? cancel?
3704 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3705 peer_info = (struct MeshPeerInfo *) cls;
3706 msg = (struct GNUNET_MESH_PeerControl *) buf;
3707 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3708 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3709 // msg->tunnel_id = htonl(peer_info->t->tid);
3710 GNUNET_PEER_resolve (peer_info->id, &id);
3711 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3719 * Send keepalive packets for a tunnel.
3721 * @param cls Closure (tunnel for which to send the keepalive).
3722 * @param tc Notification context.
3725 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3727 struct MeshTunnel *t = cls;
3728 struct GNUNET_MESH_TunnelKeepAlive *msg;
3729 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
3732 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3733 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
3734 NULL == t->owner || 0 == t->local_tid || 0 != t->prev_hop)
3739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3740 "sending keepalive for tunnel %d\n", t->id.tid);
3742 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
3743 msg->header.size = htons (size);
3744 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3745 msg->oid = my_full_id;
3746 msg->tid = htonl (t->id.tid);
3747 send_prebuilt_message (&msg->header, t->next_hop, t);
3749 t->maintenance_task =
3750 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3755 * Function to process paths received for a new peer addition. The recorded
3756 * paths form the initial tunnel, which can be optimized later.
3757 * Called on each result obtained for the DHT search.
3759 * @param cls closure
3760 * @param exp when will this value expire
3761 * @param key key of the result
3762 * @param get_path path of the get request
3763 * @param get_path_length lenght of get_path
3764 * @param put_path path of the put request
3765 * @param put_path_length length of the put_path
3766 * @param type type of the result
3767 * @param size number of bytes in data
3768 * @param data pointer to the result data
3771 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3772 const struct GNUNET_HashCode * key,
3773 const struct GNUNET_PeerIdentity *get_path,
3774 unsigned int get_path_length,
3775 const struct GNUNET_PeerIdentity *put_path,
3776 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3777 size_t size, const void *data)
3779 struct MeshPeerInfo *peer = cls;
3780 struct MeshPeerPath *p;
3781 struct GNUNET_PeerIdentity pi;
3784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3785 GNUNET_PEER_resolve (peer->id, &pi);
3786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3788 p = path_build_from_dht (get_path, get_path_length,
3789 put_path, put_path_length);
3790 path_add_to_peers (p, GNUNET_NO);
3792 for (i = 0; i < peer->ntunnels; i++)
3794 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
3795 peer_connect (peer, peer->tunnels[i]);
3802 /******************************************************************************/
3803 /********************* MESH LOCAL HANDLES **************************/
3804 /******************************************************************************/
3808 * Handler for client disconnection
3810 * @param cls closure
3811 * @param client identification of the client; NULL
3812 * for the last call when the server is destroyed
3815 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3817 struct MeshClient *c;
3818 struct MeshClient *next;
3820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3830 if (c->handle != client)
3832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3838 GNUNET_SERVER_client_drop (c->handle);
3839 c->shutting_down = GNUNET_YES;
3840 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3841 &tunnel_destroy_iterator, c);
3842 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3843 &tunnel_destroy_iterator, c);
3844 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3845 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3847 if (NULL != c->types)
3848 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3850 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
3851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3853 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
3856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
3862 * Handler for new clients
3864 * @param cls closure
3865 * @param client identification of the client
3866 * @param message the actual message, which includes messages the client wants
3869 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3870 const struct GNUNET_MessageHeader *message)
3872 struct GNUNET_MESH_ClientConnect *cc_msg;
3873 struct MeshClient *c;
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3881 /* Check data sanity */
3882 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3883 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3884 ntypes = ntohs (cc_msg->types);
3885 if (size != ntypes * sizeof (uint16_t))
3888 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3892 /* Create new client structure */
3893 c = GNUNET_malloc (sizeof (struct MeshClient));
3894 c->id = next_client_id++; /* overflow not important: just for debug */
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
3896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u types\n", ntypes);
3898 GNUNET_SERVER_client_keep (client);
3902 struct GNUNET_HashCode hc;
3904 t = (uint16_t *) &cc_msg[1];
3905 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
3906 for (i = 0; i < ntypes; i++)
3909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
3910 GMC_hash32 ((uint32_t) u16, &hc);
3912 /* store in clients hashmap */
3913 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3914 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3915 /* store in global hashmap */
3916 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3917 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3921 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
3922 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
3923 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
3924 GNUNET_SERVER_notification_context_add (nc, client);
3925 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
3927 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3933 * Handler for requests of new tunnels
3935 * @param cls Closure.
3936 * @param client Identification of the client.
3937 * @param message The actual message.
3940 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3941 const struct GNUNET_MessageHeader *message)
3943 struct GNUNET_MESH_TunnelMessage *t_msg;
3944 struct MeshPeerInfo *peer_info;
3945 struct MeshTunnel *t;
3946 struct MeshClient *c;
3947 MESH_TunnelNumber tid;
3949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3951 /* Sanity check for client registration */
3952 if (NULL == (c = client_get (client)))
3955 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3960 /* Message sanity check */
3961 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3964 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3968 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
3970 GNUNET_i2s (&t_msg->peer));
3971 /* Sanity check for tunnel numbering */
3972 tid = ntohl (t_msg->tunnel_id);
3973 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3976 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3979 /* Sanity check for duplicate tunnel IDs */
3980 if (NULL != tunnel_get_by_local_id (c, tid))
3983 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3987 while (NULL != tunnel_get_by_pi (myid, next_tid))
3988 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3989 t = tunnel_new (myid, next_tid, c, tid);
3990 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
3998 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4000 peer_info = peer_get (&t_msg->peer);
4001 GNUNET_array_append (peer_info->tunnels, peer_info->ntunnels, t);
4002 peer_connect (peer_info, t);
4003 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4009 * Handler for requests of deleting tunnels
4011 * @param cls closure
4012 * @param client identification of the client
4013 * @param message the actual message
4016 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4017 const struct GNUNET_MessageHeader *message)
4019 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4020 struct MeshClient *c;
4021 struct MeshTunnel *t;
4022 MESH_TunnelNumber tid;
4024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4025 "Got a DESTROY TUNNEL from client!\n");
4027 /* Sanity check for client registration */
4028 if (NULL == (c = client_get (client)))
4031 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4036 /* Message sanity check */
4037 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4040 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4044 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4046 /* Retrieve tunnel */
4047 tid = ntohl (tunnel_msg->tunnel_id);
4048 t = tunnel_get_by_local_id(c, tid);
4051 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4053 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4058 tunnel_destroy_empty (t);
4059 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4062 send_client_tunnel_destroy (t);
4063 client_delete_tunnel (c, t);
4065 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4067 tunnel_send_destroy (t);
4068 peer_remove_tunnel (peer_get_short(t->dest), t);
4069 t->destroy = GNUNET_YES;
4070 /* The tunnel will be destroyed when the last message is transmitted. */
4071 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4077 * Handler for requests of seeting tunnel's buffering policy.
4079 * @param cls Closure (unused).
4080 * @param client Identification of the client.
4081 * @param message The actual message.
4084 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
4085 const struct GNUNET_MessageHeader *message)
4087 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4088 struct MeshClient *c;
4089 struct MeshTunnel *t;
4090 MESH_TunnelNumber tid;
4092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4093 "Got a BUFFER request from client!\n");
4095 /* Sanity check for client registration */
4096 if (NULL == (c = client_get (client)))
4099 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4104 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4106 /* Retrieve tunnel */
4107 tid = ntohl (tunnel_msg->tunnel_id);
4108 t = tunnel_get_by_local_id(c, tid);
4111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4113 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4117 switch (ntohs(message->type))
4119 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
4120 t->nobuffer = GNUNET_NO;
4122 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
4123 t->nobuffer = GNUNET_YES;
4129 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4134 * Handler for client traffic directed to one peer
4136 * @param cls closure
4137 * @param client identification of the client
4138 * @param message the actual message
4141 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4142 const struct GNUNET_MessageHeader *message)
4144 struct MeshClient *c;
4145 struct MeshTunnel *t;
4146 struct GNUNET_MESH_Unicast *data_msg;
4147 MESH_TunnelNumber tid;
4150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4151 "Got a unicast request from a client!\n");
4153 /* Sanity check for client registration */
4154 if (NULL == (c = client_get (client)))
4157 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4162 data_msg = (struct GNUNET_MESH_Unicast *) message;
4164 /* Sanity check for message size */
4165 size = ntohs (message->size);
4166 if (sizeof (struct GNUNET_MESH_Unicast) +
4167 sizeof (struct GNUNET_MessageHeader) > size)
4170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4174 /* Tunnel exists? */
4175 tid = ntohl (data_msg->tid);
4176 t = tunnel_get_by_local_id (c, tid);
4180 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4184 /* Is it a local tunnel? Then, does client own the tunnel? */
4185 if (t->owner->handle != client)
4188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4192 /* PID should be as expected: client<->service communication */
4193 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4197 "Unicast PID, expected %u, got %u\n",
4198 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4199 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4203 /* Ok, everything is correct, send the message
4204 * (pretend we got it from a mesh peer)
4207 /* Work around const limitation */
4208 char buf[ntohs (message->size)] GNUNET_ALIGN;
4209 struct GNUNET_MESH_Unicast *copy;
4211 copy = (struct GNUNET_MESH_Unicast *) buf;
4212 memcpy (buf, data_msg, size);
4213 copy->oid = my_full_id;
4214 copy->tid = htonl (t->id.tid);
4215 copy->ttl = htonl (default_ttl);
4216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4217 " calling generic handler...\n");
4218 handle_mesh_unicast (NULL, &my_full_id, ©->header);
4220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4221 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4228 * Handler for client traffic directed to the origin
4230 * @param cls closure
4231 * @param client identification of the client
4232 * @param message the actual message
4235 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4236 const struct GNUNET_MessageHeader *message)
4238 struct GNUNET_MESH_ToOrigin *data_msg;
4239 struct MeshFlowControl *fc;
4240 struct MeshClient *c;
4241 struct MeshTunnel *t;
4242 MESH_TunnelNumber tid;
4245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4246 "Got a ToOrigin request from a client!\n");
4247 /* Sanity check for client registration */
4248 if (NULL == (c = client_get (client)))
4251 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4256 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4258 /* Sanity check for message size */
4259 size = ntohs (message->size);
4260 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4261 sizeof (struct GNUNET_MessageHeader) > size)
4264 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4268 /* Tunnel exists? */
4269 tid = ntohl (data_msg->tid);
4270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4271 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4274 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4277 t = tunnel_get_by_local_id (c, tid);
4280 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4281 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4283 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4287 /* It should be sent by someone who has this as incoming tunnel. */
4291 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4295 /* PID should be as expected */
4297 if (ntohl (data_msg->pid) != fc->last_pid_recv + 1)
4300 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4301 "To Origin PID, expected %u, got %u\n",
4302 fc->last_pid_recv + 1,
4303 ntohl (data_msg->pid));
4304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4308 /* Ok, everything is correct, send the message
4309 * (pretend we got it from a mesh peer)
4312 char buf[ntohs (message->size)] GNUNET_ALIGN;
4313 struct GNUNET_MESH_ToOrigin *copy;
4315 /* Work around 'const' limitation */
4316 memcpy (buf, data_msg, size);
4317 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4318 GNUNET_PEER_resolve (t->id.oid, ©->oid);
4319 copy->tid = htonl (t->id.tid);
4320 copy->ttl = htonl (default_ttl);
4322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4323 " calling generic handler...\n");
4324 handle_mesh_to_orig (NULL, &my_full_id, ©->header);
4326 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4333 * Handler for client's ACKs for payload traffic.
4335 * @param cls Closure (unused).
4336 * @param client Identification of the client.
4337 * @param message The actual message.
4340 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4341 const struct GNUNET_MessageHeader *message)
4343 struct GNUNET_MESH_LocalAck *msg;
4344 struct MeshTunnel *t;
4345 struct MeshClient *c;
4346 MESH_TunnelNumber tid;
4349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4350 /* Sanity check for client registration */
4351 if (NULL == (c = client_get (client)))
4354 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4359 msg = (struct GNUNET_MESH_LocalAck *) message;
4361 /* Tunnel exists? */
4362 tid = ntohl (msg->tunnel_id);
4363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4364 t = tunnel_get_by_local_id (c, tid);
4368 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4370 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4374 ack = ntohl (msg->max_pid);
4375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4377 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4380 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4381 t->prev_fc.last_ack_recv = ack;
4382 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4386 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4387 t->next_fc.last_ack_recv = ack;
4388 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4391 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4399 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4401 * @param cls Closure (client handle).
4402 * @param key Key (hashed tunnel ID, unused).
4403 * @param value Tunnel info.
4405 * @return GNUNET_YES, to keep iterating.
4408 monitor_all_tunnels_iterator (void *cls,
4409 const struct GNUNET_HashCode * key,
4412 struct GNUNET_SERVER_Client *client = cls;
4413 struct MeshTunnel *t = value;
4414 struct GNUNET_MESH_LocalMonitor *msg;
4416 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4417 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4418 msg->tunnel_id = htonl (t->id.tid);
4419 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4420 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4421 GNUNET_PEER_resolve (t->dest, &msg->destination);
4423 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4424 "* sending info about tunnel %s [%u]\n",
4425 GNUNET_i2s (&msg->owner), t->id.tid);
4427 GNUNET_SERVER_notification_context_unicast (nc, client,
4428 &msg->header, GNUNET_NO);
4434 * Handler for client's MONITOR request.
4436 * @param cls Closure (unused).
4437 * @param client Identification of the client.
4438 * @param message The actual message.
4441 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4442 const struct GNUNET_MessageHeader *message)
4444 struct MeshClient *c;
4446 /* Sanity check for client registration */
4447 if (NULL == (c = client_get (client)))
4450 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4454 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4455 "Received get tunnels request from client %u\n",
4457 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4458 monitor_all_tunnels_iterator,
4460 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4461 "Get tunnels request from client %u completed\n",
4463 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4468 * Handler for client's MONITOR_TUNNEL request.
4470 * @param cls Closure (unused).
4471 * @param client Identification of the client.
4472 * @param message The actual message.
4475 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4476 const struct GNUNET_MessageHeader *message)
4478 const struct GNUNET_MESH_LocalMonitor *msg;
4479 struct GNUNET_MESH_LocalMonitor *resp;
4480 struct MeshClient *c;
4481 struct MeshTunnel *t;
4483 /* Sanity check for client registration */
4484 if (NULL == (c = client_get (client)))
4487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4491 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4492 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4493 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4496 ntohl (msg->tunnel_id));
4497 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4500 /* We don't know the tunnel FIXME */
4501 struct GNUNET_MESH_LocalMonitor warn;
4504 GNUNET_SERVER_notification_context_unicast (nc, client,
4507 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4511 /* Initialize context */
4512 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4514 GNUNET_PEER_resolve (t->dest, &resp->destination);
4515 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4516 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4517 &resp->header, GNUNET_NO);
4520 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4521 "Monitor tunnel request from client %u completed\n",
4523 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4528 * Functions to handle messages from clients
4530 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4531 {&handle_local_new_client, NULL,
4532 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4533 {&handle_local_tunnel_create, NULL,
4534 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4535 sizeof (struct GNUNET_MESH_TunnelMessage)},
4536 {&handle_local_tunnel_destroy, NULL,
4537 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4538 sizeof (struct GNUNET_MESH_TunnelMessage)},
4539 {&handle_local_tunnel_buffer, NULL,
4540 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
4541 sizeof (struct GNUNET_MESH_TunnelMessage)},
4542 {&handle_local_tunnel_buffer, NULL,
4543 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
4544 sizeof (struct GNUNET_MESH_TunnelMessage)},
4545 {&handle_local_unicast, NULL,
4546 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4547 {&handle_local_to_origin, NULL,
4548 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4549 {&handle_local_ack, NULL,
4550 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4551 sizeof (struct GNUNET_MESH_LocalAck)},
4552 {&handle_local_get_tunnels, NULL,
4553 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4554 sizeof (struct GNUNET_MessageHeader)},
4555 {&handle_local_show_tunnel, NULL,
4556 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4557 sizeof (struct GNUNET_MESH_LocalMonitor)},
4563 * Method called whenever a given peer connects.
4565 * @param cls closure
4566 * @param peer peer identity this notification is about
4569 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4571 struct MeshPeerInfo *peer_info;
4572 struct MeshPeerPath *path;
4574 DEBUG_CONN ("Peer connected\n");
4575 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4576 peer_info = peer_get (peer);
4577 if (myid == peer_info->id)
4579 DEBUG_CONN (" (self)\n");
4584 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4586 path = path_new (2);
4587 path->peers[0] = myid;
4588 path->peers[1] = peer_info->id;
4589 GNUNET_PEER_change_rc (myid, 1);
4590 GNUNET_PEER_change_rc (peer_info->id, 1);
4591 peer_info_add_path (peer_info, path, GNUNET_YES);
4592 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4598 * Method called whenever a peer disconnects.
4600 * @param cls closure
4601 * @param peer peer identity this notification is about
4604 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4606 struct MeshPeerInfo *pi;
4607 struct MeshPeerQueue *q;
4608 struct MeshPeerQueue *n;
4610 DEBUG_CONN ("Peer disconnected\n");
4611 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4621 /* TODO try to reroute this traffic instead */
4622 queue_destroy(q, GNUNET_YES);
4625 if (NULL != pi->core_transmit)
4627 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4628 pi->core_transmit = NULL;
4630 peer_remove_path (pi, pi->id, myid);
4633 DEBUG_CONN (" (self)\n");
4635 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
4641 * To be called on core init/fail.
4643 * @param cls Closure (config)
4644 * @param server handle to the server for this service
4645 * @param identity the public identity of this peer
4648 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4649 const struct GNUNET_PeerIdentity *identity)
4651 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4655 core_handle = server;
4656 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4659 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4660 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4662 GNUNET_i2s (identity));
4663 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4665 GNUNET_i2s (&my_full_id));
4666 GNUNET_CORE_disconnect (core_handle);
4667 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4668 NULL, /* Closure passed to MESH functions */
4669 &core_init, /* Call core_init once connected */
4670 &core_connect, /* Handle connects */
4671 &core_disconnect, /* remove peers on disconnects */
4672 NULL, /* Don't notify about all incoming messages */
4673 GNUNET_NO, /* For header only in notification */
4674 NULL, /* Don't notify about all outbound messages */
4675 GNUNET_NO, /* For header-only out notification */
4676 core_handlers); /* Register these handlers */
4684 /******************************************************************************/
4685 /************************ MAIN FUNCTIONS ****************************/
4686 /******************************************************************************/
4689 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4691 * @param cls closure
4692 * @param key current key code
4693 * @param value value in the hash map
4694 * @return GNUNET_YES if we should continue to iterate,
4698 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
4700 struct MeshTunnel *t = value;
4707 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4709 * @param cls closure
4710 * @param key current key code
4711 * @param value value in the hash map
4712 * @return GNUNET_YES if we should continue to iterate,
4716 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
4718 struct MeshPeerInfo *p = value;
4719 struct MeshPeerQueue *q;
4720 struct MeshPeerQueue *n;
4728 queue_destroy(q, GNUNET_YES);
4732 peer_info_destroy (p);
4738 * Task run during shutdown.
4744 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4748 if (core_handle != NULL)
4750 GNUNET_CORE_disconnect (core_handle);
4755 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
4758 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4759 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4760 if (dht_handle != NULL)
4762 GNUNET_DHT_disconnect (dht_handle);
4767 GNUNET_SERVER_notification_context_destroy (nc);
4770 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4772 GNUNET_SCHEDULER_cancel (announce_id_task);
4773 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4780 * Callback for hostkey read/generation.
4782 * @param cls Closure (Configuration handle).
4783 * @param pk The ECC private key.
4784 * @param emsg Error message, if any.
4787 key_generation_cb (void *cls,
4788 struct GNUNET_CRYPTO_EccPrivateKey *pk,
4791 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4792 struct MeshPeerInfo *peer;
4793 struct MeshPeerPath *p;
4798 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4799 _("Mesh service could not access hostkey: %s. Exiting.\n"),
4801 GNUNET_SCHEDULER_shutdown ();
4804 my_private_key = pk;
4805 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
4806 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4807 &my_full_id.hashPubKey);
4808 myid = GNUNET_PEER_intern (&my_full_id);
4809 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4810 "Mesh for peer [%s] starting\n",
4811 GNUNET_i2s(&my_full_id));
4813 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4814 NULL, /* Closure passed to MESH functions */
4815 &core_init, /* Call core_init once connected */
4816 &core_connect, /* Handle connects */
4817 &core_disconnect, /* remove peers on disconnects */
4818 NULL, /* Don't notify about all incoming messages */
4819 GNUNET_NO, /* For header only in notification */
4820 NULL, /* Don't notify about all outbound messages */
4821 GNUNET_NO, /* For header-only out notification */
4822 core_handlers); /* Register these handlers */
4823 if (core_handle == NULL)
4826 GNUNET_SCHEDULER_shutdown ();
4831 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4834 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4835 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
4836 GNUNET_SERVER_disconnect_notify (server_handle,
4837 &handle_local_client_disconnect, NULL);
4840 clients_head = NULL;
4841 clients_tail = NULL;
4844 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4846 /* Create a peer_info for the local peer */
4847 peer = peer_get (&my_full_id);
4850 GNUNET_PEER_change_rc (myid, 1);
4851 peer_info_add_path (peer, p, GNUNET_YES);
4852 GNUNET_SERVER_resume (server_handle);
4853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
4858 * Process mesh requests.
4860 * @param cls closure
4861 * @param server the initialized server
4862 * @param c configuration to use
4865 run (void *cls, struct GNUNET_SERVER_Handle *server,
4866 const struct GNUNET_CONFIGURATION_Handle *c)
4870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4871 server_handle = server;
4874 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
4877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4879 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4880 "mesh", "peer/privatekey");
4881 GNUNET_SCHEDULER_shutdown ();
4886 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
4887 &refresh_path_time))
4889 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4891 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4892 "mesh", "refresh path time");
4893 GNUNET_SCHEDULER_shutdown ();
4898 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
4901 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4903 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4904 "mesh", "id announce time");
4905 GNUNET_SCHEDULER_shutdown ();
4910 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
4913 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4915 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4916 "mesh", "connect timeout");
4917 GNUNET_SCHEDULER_shutdown ();
4922 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
4925 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4927 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4928 "mesh", "max msgs queue");
4929 GNUNET_SCHEDULER_shutdown ();
4934 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
4937 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4939 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4940 "mesh", "max tunnels");
4941 GNUNET_SCHEDULER_shutdown ();
4946 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
4949 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4951 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4952 "mesh", "default ttl", 64);
4957 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
4960 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4961 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4962 "mesh", "max peers", 1000);
4967 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
4968 &dht_replication_level))
4970 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4972 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4973 "mesh", "dht replication level", 3);
4974 dht_replication_level = 3;
4977 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4978 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4979 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4980 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4982 dht_handle = GNUNET_DHT_connect (c, 64);
4983 if (NULL == dht_handle)
4987 stats = GNUNET_STATISTICS_create ("mesh", c);
4989 GNUNET_SERVER_suspend (server_handle);
4990 /* Scheduled the task to clean up when shutdown is called */
4991 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4993 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
4996 GNUNET_free (keyfile);
5001 * The main function for the mesh service.
5003 * @param argc number of arguments from the command line
5004 * @param argv command line arguments
5005 * @return 0 ok, 1 on error
5008 main (int argc, char *const *argv)
5013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5014 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5016 ret = (GNUNET_OK == r) ? 0 : 1;
5017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5021 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5022 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5023 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);