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
31 * - keep queues until receiving ACK
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 * Port of the tunnel.
302 * State of the tunnel.
304 enum MeshTunnelState state;
307 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
309 MESH_TunnelNumber local_tid;
312 * Local tunnel number for local destination clients (incoming number)
313 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
316 MESH_TunnelNumber local_tid_dest;
319 * Is the tunnel bufferless (minimum latency)?
324 * Force sending ACK? Flag to allow duplicate ACK on POLL.
329 * How many messages do we accept in the forward queue.
331 unsigned int queue_max;
334 * Last time the tunnel was used
336 struct GNUNET_TIME_Absolute timestamp;
339 * Destination of the tunnel.
344 * Next hop in the tunnel. If 0, @c client must be set.
346 GNUNET_PEER_Id next_hop;
349 * Previous hop in the tunnel. If 0, @c owner must be set.
351 GNUNET_PEER_Id prev_hop;
354 * Flow control information about @c next_hop or @c client.
356 struct MeshFlowControl next_fc;
359 * Flow control information about @c prev_hop or @c owner.
361 struct MeshFlowControl prev_fc;
364 * Client owner of the tunnel, if any
366 struct MeshClient *owner;
369 * Client destination of the tunnel, if any.
371 struct MeshClient *client;
374 * Task to keep the used paths alive at the owner,
375 * time tunnel out on all the other peers.
377 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
380 * Path being used for the tunnel.
382 struct MeshPeerPath *path;
385 * Flag to signal the destruction of the tunnel.
386 * If this is set GNUNET_YES the tunnel will be destroyed
387 * when the queue is empty.
392 * Total messages pending for this tunnels, payload or not.
394 unsigned int pending_messages;
399 * Struct containing information about a client of the service
401 * TODO: add a list of 'waiting' ports
408 struct MeshClient *next;
413 struct MeshClient *prev;
416 * Tunnels that belong to this client, indexed by local id
418 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
421 * Tunnels this client has accepted, indexed by incoming local id
423 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
426 * Handle to communicate with the client
428 struct GNUNET_SERVER_Client *handle;
431 * Ports that this client has declared interest in.
432 * Indexed by a GMC_hash32 (type), contains *Client.
434 struct GNUNET_CONTAINER_MultiHashMap *ports;
437 * Whether the client is active or shutting down (don't send confirmations
438 * to a client that is shutting down.
443 * ID of the client, mainly for debug messages
450 /******************************************************************************/
451 /************************ DEBUG FUNCTIONS ****************************/
452 /******************************************************************************/
456 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
457 * @param cls string to print
458 * @param success GNUNET_OK if the PUT was transmitted,
459 * GNUNET_NO on timeout,
460 * GNUNET_SYSERR on disconnect from service
461 * after the PUT message was transmitted
462 * (so we don't know if it was received or not)
467 mesh_debug (void *cls, int success)
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
476 unsigned int debug_fwd_ack;
477 unsigned int debug_bck_ack;
481 /******************************************************************************/
482 /*********************** GLOBAL VARIABLES ****************************/
483 /******************************************************************************/
485 /************************** Configuration parameters **************************/
488 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
490 static struct GNUNET_TIME_Relative refresh_path_time;
493 * How often to PUT own ID in the DHT.
495 static struct GNUNET_TIME_Relative id_announce_time;
498 * Maximum time allowed to connect to a peer found by string.
500 static struct GNUNET_TIME_Relative connect_timeout;
503 * Default TTL for payload packets.
505 static unsigned long long default_ttl;
508 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
510 static unsigned long long dht_replication_level;
513 * How many tunnels are we willing to maintain.
514 * Local tunnels are always allowed, even if there are more tunnels than max.
516 static unsigned long long max_tunnels;
519 * How many messages *in total* are we willing to queue, divided by number of
520 * tunnels to get tunnel queue size.
522 static unsigned long long max_msgs_queue;
525 * How many peers do we want to remember?
527 static unsigned long long max_peers;
530 /*************************** Static global variables **************************/
533 * Hostkey generation context
535 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
538 * DLL with all the clients, head.
540 static struct MeshClient *clients_head;
543 * DLL with all the clients, tail.
545 static struct MeshClient *clients_tail;
548 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
550 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
553 * Number of tunnels known.
555 static unsigned long long n_tunnels;
558 * Tunnels incoming, indexed by MESH_TunnelNumber
559 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
561 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
564 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
566 static struct GNUNET_CONTAINER_MultiHashMap *peers;
569 * Handle to communicate with transport
571 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
574 * Handle to communicate with core.
576 static struct GNUNET_CORE_Handle *core_handle;
581 static struct GNUNET_DHT_Handle *dht_handle;
586 static struct GNUNET_SERVER_Handle *server_handle;
589 * Handle to the statistics service.
591 static struct GNUNET_STATISTICS_Handle *stats;
594 * Notification context, to send messages to local clients.
596 static struct GNUNET_SERVER_NotificationContext *nc;
599 * Local peer own ID (memory efficient handle).
601 static GNUNET_PEER_Id myid;
604 * Local peer own ID (full value).
606 static struct GNUNET_PeerIdentity my_full_id;
611 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
616 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
619 * Tunnel ID for the next created tunnel (global tunnel number).
621 static MESH_TunnelNumber next_tid;
624 * Tunnel ID for the next incoming tunnel (local tunnel number).
626 static MESH_TunnelNumber next_local_tid;
629 * All ports clients of this peer have opened.
631 static struct GNUNET_CONTAINER_MultiHashMap *ports;
634 * Task to periodically announce itself in the network.
636 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
639 * Next ID to assign to a client.
641 unsigned int next_client_id;
644 /******************************************************************************/
645 /*********************** DECLARATIONS **************************/
646 /******************************************************************************/
649 * Function to process paths received for a new peer addition. The recorded
650 * paths form the initial tunnel, which can be optimized later.
651 * Called on each result obtained for the DHT search.
654 * @param exp when will this value expire
655 * @param key key of the result
656 * @param type type of the result
657 * @param size number of bytes in data
658 * @param data pointer to the result data
661 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
662 const struct GNUNET_HashCode * key,
663 const struct GNUNET_PeerIdentity *get_path,
664 unsigned int get_path_length,
665 const struct GNUNET_PeerIdentity *put_path,
666 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
667 size_t size, const void *data);
671 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
672 * and insert it in the appropriate structures if the peer is not known yet.
674 * @param peer Full identity of the peer.
676 * @return Existing or newly created peer info.
678 static struct MeshPeerInfo *
679 peer_get (const struct GNUNET_PeerIdentity *peer);
683 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
684 * and insert it in the appropriate structures if the peer is not known yet.
686 * @param peer Short identity of the peer.
688 * @return Existing or newly created peer info.
690 static struct MeshPeerInfo *
691 peer_get_short (const GNUNET_PEER_Id peer);
695 * Build a PeerPath from the paths returned from the DHT, reversing the paths
696 * to obtain a local peer -> destination path and interning the peer ids.
698 * @return Newly allocated and created path
700 static struct MeshPeerPath *
701 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
702 unsigned int get_path_length,
703 const struct GNUNET_PeerIdentity *put_path,
704 unsigned int put_path_length);
708 * Adds a path to the peer_infos of all the peers in the path
710 * @param p Path to process.
711 * @param confirmed Whether we know if the path works or not.
714 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
719 * Search for a tunnel by global ID using full PeerIdentities.
721 * @param oid owner of the tunnel.
722 * @param tid global tunnel number.
724 * @return tunnel handler, NULL if doesn't exist.
726 static struct MeshTunnel *
727 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
731 * Notify a tunnel that a connection has broken that affects at least
734 * @param t Tunnel affected.
735 * @param p1 Peer that got disconnected from p2.
736 * @param p2 Peer that got disconnected from p1.
738 * @return Short ID of the peer disconnected (either p1 or p2).
739 * 0 if the tunnel remained unaffected.
741 static GNUNET_PEER_Id
742 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
747 * @brief Use the given path for the tunnel.
748 * Update the next and prev hops (and RCs).
749 * (Re)start the path refresh in case the tunnel is locally owned.
751 * @param t Tunnel to update.
752 * @param p Path to use.
755 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
758 * @brief Queue and pass message to core when possible.
760 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
761 * and accounts for it. In case the queue is full, the message is dropped and
764 * Otherwise, message is treated as internal and allowed to go regardless of
767 * @param cls Closure (@c type dependant). It will be used by queue_send to
768 * build the message to be sent if not already prebuilt.
769 * @param type Type of the message, 0 for a raw message.
770 * @param size Size of the message.
771 * @param dst Neighbor to send message to.
772 * @param t Tunnel this message belongs to.
775 queue_add (void *cls, uint16_t type, size_t size,
776 struct MeshPeerInfo *dst, struct MeshTunnel *t);
780 * Free a transmission that was already queued with all resources
781 * associated to the request.
783 * @param queue Queue handler to cancel.
784 * @param clear_cls Is it necessary to free associated cls?
787 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
791 * @brief Get the next transmittable message from the queue.
793 * This will be the head, except in the case of being a data packet
794 * not allowed by the destination peer.
796 * @param peer Destination peer.
798 * @return The next viable MeshPeerQueue element to send to that peer.
799 * NULL when there are no transmittable messages.
801 struct MeshPeerQueue *
802 queue_get_next (const struct MeshPeerInfo *peer);
806 * Core callback to write a queued packet to core buffer
808 * @param cls Closure (peer info).
809 * @param size Number of bytes available in buf.
810 * @param buf Where the to write the message.
812 * @return number of bytes written to buf
815 queue_send (void *cls, size_t size, void *buf);
818 /******************************************************************************/
819 /************************ PERIODIC FUNCTIONS ****************************/
820 /******************************************************************************/
823 * Periodically announce self id in the DHT
826 * @param tc task context
829 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
833 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
835 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
839 * - Set data expiration in function of X
842 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
844 block.id = my_full_id;
845 block.type = htonl (0);
846 GNUNET_DHT_put (dht_handle, /* DHT handle */
847 &my_full_id.hashPubKey, /* Key to use */
848 dht_replication_level, /* Replication level */
849 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
850 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
851 sizeof (block), /* Size of the data */
852 (const char *) &block, /* Data itself */
853 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
854 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
855 NULL, /* Continuation */
856 NULL); /* Continuation closure */
858 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
862 /******************************************************************************/
863 /****************** GENERAL HELPER FUNCTIONS ************************/
864 /******************************************************************************/
868 * Check if client has registered with the service and has not disconnected
870 * @param client the client to check
872 * @return non-NULL if client exists in the global DLL
874 static struct MeshClient *
875 client_get (struct GNUNET_SERVER_Client *client)
877 struct MeshClient *c;
882 if (c->handle == client)
891 * Deletes a tunnel from a client (either owner or destination). To be used on
894 * @param c Client whose tunnel to delete.
895 * @param t Tunnel which should be deleted.
898 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
900 struct GNUNET_HashCode hash;
904 GMC_hash32 (t->local_tid, &hash);
905 GNUNET_assert (GNUNET_YES ==
906 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
910 else if (c == t->client)
912 GMC_hash32 (t->local_tid_dest, &hash);
913 GNUNET_assert (GNUNET_YES ==
914 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
925 * Notify the appropriate client that a new incoming tunnel was created.
927 * @param t Tunnel that was created.
930 send_client_tunnel_create (struct MeshTunnel *t)
932 struct GNUNET_MESH_TunnelMessage msg;
934 if (NULL == t->client)
936 msg.header.size = htons (sizeof (msg));
937 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
938 msg.tunnel_id = htonl (t->local_tid_dest);
939 msg.port = htonl (t->port);
940 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
941 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
942 &msg.header, GNUNET_NO);
947 * Notify dest client that the incoming tunnel is no longer valid.
949 * @param c Client to notify..
950 * @param t Tunnel that is destroyed.
953 send_client_tunnel_destroy (struct MeshClient *c, struct MeshTunnel *t)
955 struct GNUNET_MESH_TunnelMessage msg;
962 if (c != t->client && c != t->owner)
967 msg.header.size = htons (sizeof (msg));
968 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
969 msg.tunnel_id = htonl (t->local_tid_dest);
970 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
971 &msg.header, GNUNET_NO);
976 * Iterator over all the peers to remove the oldest not-used entry.
978 * @param cls Closure (unsued).
979 * @param key ID of the peer.
980 * @param value Peer_Info of the peer.
985 peer_info_timeout (void *cls,
986 const struct GNUNET_HashCode *key,
993 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
994 * and insert it in the appropriate structures if the peer is not known yet.
996 * @param peer Full identity of the peer.
998 * @return Existing or newly created peer info.
1000 static struct MeshPeerInfo *
1001 peer_get (const struct GNUNET_PeerIdentity *peer)
1003 struct MeshPeerInfo *peer_info;
1005 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1006 if (NULL == peer_info)
1009 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1010 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1012 GNUNET_CONTAINER_multihashmap_iterate (peers,
1016 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1017 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1018 peer_info->id = GNUNET_PEER_intern (peer);
1020 peer_info->last_contact = GNUNET_TIME_absolute_get();
1027 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1028 * and insert it in the appropriate structures if the peer is not known yet.
1030 * @param peer Short identity of the peer.
1032 * @return Existing or newly created peer info.
1034 static struct MeshPeerInfo *
1035 peer_get_short (const GNUNET_PEER_Id peer)
1037 struct GNUNET_PeerIdentity id;
1039 GNUNET_PEER_resolve (peer, &id);
1040 return peer_get (&id);
1045 * Choose the best path towards a peer considering the tunnel properties.
1047 * @param peer The destination peer.
1048 * @param t The tunnel the path is for.
1050 * @return Best current known path towards the peer, if any.
1052 static struct MeshPeerPath *
1053 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1055 struct MeshPeerPath *best_p;
1056 struct MeshPeerPath *p;
1057 unsigned int best_cost;
1060 best_p = p = peer->path_head;
1061 best_cost = cost = p->length;
1064 if ((cost = p->length) < best_cost)
1075 * Core callback to write a pre-constructed data packet to core buffer
1077 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1078 * @param size Number of bytes available in buf.
1079 * @param buf Where the to write the message.
1081 * @return number of bytes written to buf
1084 send_core_data_raw (void *cls, size_t size, void *buf)
1086 struct GNUNET_MessageHeader *msg = cls;
1089 GNUNET_assert (NULL != msg);
1090 total_size = ntohs (msg->size);
1092 if (total_size > size)
1097 memcpy (buf, msg, total_size);
1104 * Sends an already built message to a peer, properly registrating
1105 * all used resources.
1107 * @param message Message to send. Function makes a copy of it.
1108 * @param peer Short ID of the neighbor whom to send the message.
1109 * @param t Tunnel on which this message is transmitted.
1112 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1113 GNUNET_PEER_Id peer,
1114 struct MeshTunnel *t)
1116 struct GNUNET_PeerIdentity id;
1117 struct MeshPeerInfo *neighbor;
1118 struct MeshPeerPath *p;
1123 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1128 size = ntohs (message->size);
1129 data = GNUNET_malloc (size);
1130 memcpy (data, message, size);
1131 type = ntohs(message->type);
1134 struct GNUNET_MESH_Unicast *u;
1136 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1137 u = (struct GNUNET_MESH_Unicast *) data;
1138 u->ttl = htonl (ntohl (u->ttl) - 1);
1141 GNUNET_PEER_resolve (peer, &id);
1142 neighbor = peer_get (&id);
1143 for (p = neighbor->path_head; NULL != p; p = p->next)
1153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1154 " %s IS NOT DIRECTLY CONNECTED\n",
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1159 for (p = neighbor->path_head; NULL != p; p = p->next)
1161 struct GNUNET_PeerIdentity debug_id;
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1165 " path with %u hops through:\n",
1167 for (i = 0; i < p->length; i++)
1169 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1172 i, GNUNET_i2s(&debug_id));
1176 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1177 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1178 " no direct connection to %s\n",
1183 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1194 * Sends a CREATE PATH message for a path to a peer, properly registrating
1195 * all used resources.
1197 * @param t Tunnel for which the path is created.
1200 send_create_path (struct MeshTunnel *t)
1202 struct MeshPeerInfo *neighbor;
1204 neighbor = peer_get_short (t->next_hop);
1206 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1207 sizeof (struct GNUNET_MESH_CreateTunnel) +
1208 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1211 t->state = MESH_TUNNEL_WAITING;
1216 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1218 * @param t Tunnel which to confirm.
1221 send_path_ack (struct MeshTunnel *t)
1223 struct MeshPeerInfo *peer;
1225 peer = peer_get_short (t->prev_hop);
1228 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1229 sizeof (struct GNUNET_MESH_PathACK),
1236 * Try to establish a new connection to this peer in the given tunnel.
1237 * If the peer doesn't have any path to it yet, try to get one.
1238 * If the peer already has some path, send a CREATE PATH towards it.
1240 * @param peer PeerInfo of the peer.
1241 * @param t Tunnel for which to create the path, if possible.
1244 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1246 struct MeshPeerPath *p;
1248 if (NULL != peer->path_head)
1250 p = peer_get_best_path (peer, t);
1251 tunnel_use_path (t, p);
1252 send_create_path (t);
1254 else if (NULL == peer->dhtget)
1256 struct GNUNET_PeerIdentity id;
1258 GNUNET_PEER_resolve (peer->id, &id);
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1260 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1261 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1262 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1263 &id.hashPubKey, /* key to search */
1264 dht_replication_level, /* replication level */
1265 GNUNET_DHT_RO_RECORD_ROUTE |
1266 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1268 0, /* xquery bits */
1269 &dht_get_id_handler, peer);
1270 t->state = MESH_TUNNEL_SEARCHING;
1272 /* Otherwise, there is no path but the DHT get is already started. */
1277 * Destroy the peer_info and free any allocated resources linked to it
1279 * @param pi The peer_info to destroy.
1281 * @return GNUNET_OK on success
1284 peer_info_destroy (struct MeshPeerInfo *pi)
1286 struct GNUNET_PeerIdentity id;
1287 struct MeshPeerPath *p;
1288 struct MeshPeerPath *nextp;
1290 GNUNET_PEER_resolve (pi->id, &id);
1291 GNUNET_PEER_change_rc (pi->id, -1);
1294 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1297 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1298 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1300 if (NULL != pi->dhtget)
1302 GNUNET_DHT_get_stop (pi->dhtget);
1308 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1318 * Remove all paths that rely on a direct connection between p1 and p2
1319 * from the peer itself and notify all tunnels about it.
1321 * @param peer PeerInfo of affected peer.
1322 * @param p1 GNUNET_PEER_Id of one peer.
1323 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1326 * TODO: optimize (see below)
1329 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1332 struct MeshPeerPath *p;
1333 struct MeshPeerPath *next;
1334 struct MeshPeerInfo *peer_d;
1336 unsigned int destroyed;
1339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1341 for (p = peer->path_head; NULL != p; p = next)
1344 for (i = 0; i < (p->length - 1); i++)
1346 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1347 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1349 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1359 for (i = 0; i < peer->ntunnels; i++)
1361 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1365 peer_d = peer_get_short (d);
1366 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1367 tunnel_use_path (peer->tunnels[i], next);
1368 peer_connect (peer_d, peer->tunnels[i]);
1370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1375 * Add the path to the peer and update the path used to reach it in case this
1378 * @param peer_info Destination peer to add the path to.
1379 * @param path New path to add. Last peer must be the peer in arg 1.
1380 * Path will be either used of freed if already known.
1381 * @param trusted Do we trust that this path is real?
1384 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1387 struct MeshPeerPath *aux;
1391 if ((NULL == peer_info) || (NULL == path))
1394 path_destroy (path);
1397 if (path->peers[path->length - 1] != peer_info->id)
1400 path_destroy (path);
1403 if (path->length <= 2 && GNUNET_NO == trusted)
1405 /* Only allow CORE to tell us about direct paths */
1406 path_destroy (path);
1409 for (l = 1; l < path->length; l++)
1411 if (path->peers[l] == myid)
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1414 for (l2 = 0; l2 < path->length - l; l2++)
1416 path->peers[l2] = path->peers[l + l2];
1421 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1426 struct GNUNET_PeerIdentity id;
1428 GNUNET_PEER_resolve (peer_info->id, &id);
1429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1430 path->length, GNUNET_i2s (&id));
1433 l = path_get_length (path);
1440 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1441 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1443 l2 = path_get_length (aux);
1446 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1447 peer_info->path_tail, aux, path);
1452 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1454 path_destroy (path);
1459 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1466 * Add the path to the origin peer and update the path used to reach it in case
1467 * this is the shortest.
1468 * The path is given in peer_info -> destination, therefore we turn the path
1469 * upside down first.
1471 * @param peer_info Peer to add the path to, being the origin of the path.
1472 * @param path New path to add after being inversed.
1473 * Path will be either used or freed.
1474 * @param trusted Do we trust that this path is real?
1477 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1478 struct MeshPeerPath *path, int trusted)
1481 peer_info_add_path (peer_info, path, trusted);
1487 * Remove a tunnel from the list of tunnels a peer participates in.
1489 * @param p Peer to clean.
1490 * @param t Tunnel to remove.
1493 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1497 for (i = 0; i < p->ntunnels; i++)
1499 if (p->tunnels[i] == t)
1501 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1502 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1510 * Function called if the connection to the peer has been stalled for a while,
1511 * possibly due to a missed ACK. Poll the peer about its ACK status.
1513 * @param cls Closure (poll ctx).
1514 * @param tc TaskContext.
1517 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1519 struct MeshFlowControl *fc = cls;
1520 struct GNUNET_MESH_Poll msg;
1521 struct MeshTunnel *t = fc->t;
1522 GNUNET_PEER_Id peer;
1524 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1525 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1530 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1531 msg.header.size = htons (sizeof (msg));
1532 msg.tid = htonl (t->id.tid);
1533 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1535 if (fc == &t->prev_fc)
1539 else if (fc == &t->next_fc)
1548 send_prebuilt_message (&msg.header, peer, t);
1549 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1550 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1556 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1557 * to obtain a local peer -> destination path and interning the peer ids.
1559 * @return Newly allocated and created path
1561 static struct MeshPeerPath *
1562 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1563 unsigned int get_path_length,
1564 const struct GNUNET_PeerIdentity *put_path,
1565 unsigned int put_path_length)
1567 struct MeshPeerPath *p;
1573 GNUNET_PEER_change_rc (myid, 1);
1574 i = get_path_length;
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1576 for (i--; i >= 0; i--)
1578 id = GNUNET_PEER_intern (&get_path[i]);
1579 if (p->length > 0 && id == p->peers[p->length - 1])
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1582 GNUNET_PEER_change_rc (id, -1);
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1587 GNUNET_i2s (&get_path[i]));
1589 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1590 p->peers[p->length - 1] = id;
1593 i = put_path_length;
1594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1595 for (i--; i >= 0; i--)
1597 id = GNUNET_PEER_intern (&put_path[i]);
1600 /* PUT path went through us, so discard the path up until now and start
1601 * from here to get a much shorter (and loop-free) path.
1606 if (p->length > 0 && id == p->peers[p->length - 1])
1608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1609 GNUNET_PEER_change_rc (id, -1);
1613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1614 GNUNET_i2s (&put_path[i]));
1616 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1617 p->peers[p->length - 1] = id;
1621 if (get_path_length > 0)
1622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1623 GNUNET_i2s (&get_path[0]));
1624 if (put_path_length > 0)
1625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1626 GNUNET_i2s (&put_path[0]));
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1629 for (i = 0; i < p->length; i++)
1631 struct GNUNET_PeerIdentity peer_id;
1633 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1635 GNUNET_i2s (&peer_id));
1643 * Adds a path to the peer_infos of all the peers in the path
1645 * @param p Path to process.
1646 * @param confirmed Whether we know if the path works or not.
1649 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1653 /* TODO: invert and add */
1654 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1655 for (i++; i < p->length; i++)
1657 struct MeshPeerInfo *aux;
1658 struct MeshPeerPath *copy;
1660 aux = peer_get_short (p->peers[i]);
1661 copy = path_duplicate (p);
1662 copy->length = i + 1;
1663 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1669 * Send keepalive packets for a peer
1671 * @param cls Closure (tunnel for which to send the keepalive).
1672 * @param tc Notification context.
1675 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1679 * Search for a tunnel among the incoming tunnels
1681 * @param tid the local id of the tunnel
1683 * @return tunnel handler, NULL if doesn't exist
1685 static struct MeshTunnel *
1686 tunnel_get_incoming (MESH_TunnelNumber tid)
1688 struct GNUNET_HashCode hash;
1690 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1691 GMC_hash32 (tid, &hash);
1692 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1697 * Search for a tunnel among the tunnels for a client
1699 * @param c the client whose tunnels to search in
1700 * @param tid the local id of the tunnel
1702 * @return tunnel handler, NULL if doesn't exist
1704 static struct MeshTunnel *
1705 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1707 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1709 return tunnel_get_incoming (tid);
1713 struct GNUNET_HashCode hash;
1715 GMC_hash32 (tid, &hash);
1716 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1722 * Search for a tunnel by global ID using PEER_ID
1724 * @param pi owner of the tunnel
1725 * @param tid global tunnel number
1727 * @return tunnel handler, NULL if doesn't exist
1729 static struct MeshTunnel *
1730 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1732 struct MESH_TunnelID id;
1733 struct GNUNET_HashCode hash;
1738 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1739 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1744 * Search for a tunnel by global ID using full PeerIdentities
1746 * @param oid owner of the tunnel
1747 * @param tid global tunnel number
1749 * @return tunnel handler, NULL if doesn't exist
1751 static struct MeshTunnel *
1752 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1754 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1759 * Add a client to a tunnel, initializing all needed data structures.
1761 * @param t Tunnel to which add the client.
1762 * @param c Client which to add to the tunnel.
1765 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1767 struct GNUNET_HashCode hash;
1769 if (NULL != t->client)
1774 GMC_hash32 (t->local_tid_dest, &hash);
1776 GNUNET_CONTAINER_multihashmap_put (c->incoming_tunnels, &hash, t,
1777 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1783 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1784 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1794 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1796 unsigned int own_pos;
1798 for (own_pos = 0; own_pos < p->length; own_pos++)
1800 if (p->peers[own_pos] == myid)
1803 if (own_pos > p->length - 1)
1809 if (own_pos < p->length - 1)
1810 t->next_hop = p->peers[own_pos + 1];
1812 t->next_hop = p->peers[own_pos];
1813 GNUNET_PEER_change_rc (t->next_hop, 1);
1815 t->prev_hop = p->peers[own_pos - 1];
1817 t->prev_hop = p->peers[0];
1818 GNUNET_PEER_change_rc (t->prev_hop, 1);
1820 if (NULL != t->path)
1821 path_destroy (t->path);
1822 t->path = path_duplicate (p);
1825 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1826 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1827 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
1834 * Notifies a tunnel that a connection has broken that affects at least
1835 * some of its peers. Sends a notification towards the root of the tree.
1836 * In case the peer is the owner of the tree, notifies the client that owns
1837 * the tunnel and tries to reconnect.
1839 * @param t Tunnel affected.
1840 * @param p1 Peer that got disconnected from p2.
1841 * @param p2 Peer that got disconnected from p1.
1843 * @return Short ID of the peer disconnected (either p1 or p2).
1844 * 0 if the tunnel remained unaffected.
1846 static GNUNET_PEER_Id
1847 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1850 // if (myid != p1 && myid != p2) FIXME
1855 // if (tree_get_predecessor (t->tree) != 0)
1857 // /* We are the peer still connected, notify owner of the disconnection. */
1858 // struct GNUNET_MESH_PathBroken msg;
1859 // struct GNUNET_PeerIdentity neighbor;
1861 // msg.header.size = htons (sizeof (msg));
1862 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1863 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1864 // msg.tid = htonl (t->id.tid);
1865 // msg.peer1 = my_full_id;
1866 // GNUNET_PEER_resolve (pid, &msg.peer2);
1867 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1868 // send_prebuilt_message (&msg.header, &neighbor, t);
1875 * Build a local ACK message and send it to a local client.
1877 * @param t Tunnel on which to send the ACK.
1878 * @param c Client to whom send the ACK.
1879 * @param ack Value of the ACK.
1882 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
1884 struct GNUNET_MESH_LocalAck msg;
1886 msg.header.size = htons (sizeof (msg));
1887 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1888 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
1889 msg.ack = htonl (ack);
1890 GNUNET_SERVER_notification_context_unicast(nc,
1897 * Build an ACK message and queue it to send to the given peer.
1899 * @param t Tunnel on which to send the ACK.
1900 * @param peer Peer to whom send the ACK.
1901 * @param ack Value of the ACK.
1904 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1906 struct GNUNET_MESH_ACK msg;
1908 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1909 msg.header.size = htons (sizeof (msg));
1910 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1911 msg.pid = htonl (ack);
1912 msg.tid = htonl (t->id.tid);
1914 send_prebuilt_message (&msg.header, peer, t);
1919 * Send an ACK informing the predecessor about the available buffer space.
1920 * In case there is no predecessor, inform the owning client.
1921 * If buffering is off, send only on behalf of children or self if endpoint.
1922 * If buffering is on, send when sent to children and buffer space is free.
1923 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
1924 * the ACK itself goes "back" (towards root).
1926 * @param t Tunnel on which to send the ACK.
1927 * @param type Type of message that triggered the ACK transmission.
1930 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
1934 /* Is it after unicast retransmission? */
1937 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1939 "ACK due to FWD DATA retransmission\n");
1940 if (GNUNET_YES == t->nobuffer)
1942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
1946 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1947 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1949 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
1950 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1951 t->force_ack = GNUNET_YES;
1957 /* Check if we need to transmit the ACK */
1958 if (t->queue_max > t->next_fc.queue_n * 4 &&
1959 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
1960 GNUNET_NO == t->force_ack)
1962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
1963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1964 " t->qmax: %u, t->qn: %u\n",
1965 t->queue_max, t->next_fc.queue_n);
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1967 " t->pid: %u, t->ack: %u\n",
1968 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
1972 /* Ok, ACK might be necessary, what PID to ACK? */
1973 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
1974 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
1976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
1980 t->prev_fc.last_ack_sent = ack;
1981 if (NULL != t->owner)
1982 send_local_ack (t, t->owner, ack);
1983 else if (0 != t->prev_hop)
1984 send_ack (t, t->prev_hop, ack);
1988 t->force_ack = GNUNET_NO;
1993 * Send an ACK informing the children node/client about the available
1995 * If buffering is off, send only on behalf of root (can be self).
1996 * If buffering is on, send when sent to predecessor and buffer space is free.
1997 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
1998 * the ACK itself goes "forward" (towards children/clients).
2000 * @param t Tunnel on which to send the ACK.
2001 * @param type Type of message that triggered the ACK transmission.
2004 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2008 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2009 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2010 /* Is it after data to_origin retransmission? */
2013 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2014 if (GNUNET_YES == t->nobuffer)
2016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2017 " Not sending ACK, nobuffer + traffic\n");
2021 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2022 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2024 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2025 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2026 t->force_ack = GNUNET_YES;
2032 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2034 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2036 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2039 " Not sending ACK, not needed\n");
2042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2043 " Sending BCK ACK %u (last sent: %u)\n",
2044 ack, t->next_fc.last_ack_sent);
2045 t->next_fc.last_ack_sent = ack;
2047 if (NULL != t->client)
2048 send_local_ack (t, t->client, ack);
2049 else if (0 != t->next_hop)
2050 send_ack (t, t->next_hop, ack);
2053 t->force_ack = GNUNET_NO;
2058 * Modify the unicast message TID from global to local and send to client.
2060 * @param t Tunnel on which to send the message.
2061 * @param msg Message to modify and send.
2064 tunnel_send_client_ucast (struct MeshTunnel *t,
2065 const struct GNUNET_MESH_Unicast *msg)
2067 struct GNUNET_MESH_Unicast *copy;
2068 uint16_t size = ntohs (msg->header.size);
2071 if (size < sizeof (struct GNUNET_MESH_Unicast) +
2072 sizeof (struct GNUNET_MessageHeader))
2074 GNUNET_break_op (0);
2077 if (NULL == t->client)
2082 copy = (struct GNUNET_MESH_Unicast *) cbuf;
2083 memcpy (copy, msg, size);
2084 copy->tid = htonl (t->local_tid_dest);
2085 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
2086 ©->header, GNUNET_NO);
2091 * Modify the to_origin message TID from global to local and send to client.
2093 * @param t Tunnel on which to send the message.
2094 * @param msg Message to modify and send.
2097 tunnel_send_client_to_orig (struct MeshTunnel *t,
2098 const struct GNUNET_MESH_ToOrigin *msg)
2100 struct GNUNET_MESH_ToOrigin *copy;
2101 uint16_t size = ntohs (msg->header.size);
2104 if (size < sizeof (struct GNUNET_MESH_ToOrigin) +
2105 sizeof (struct GNUNET_MessageHeader))
2107 GNUNET_break_op (0);
2110 if (NULL == t->owner)
2115 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2116 memcpy (cbuf, msg, size);
2117 copy->tid = htonl (t->local_tid);
2118 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2119 ©->header, GNUNET_NO);
2124 * @brief Re-initiate traffic to this peer if necessary.
2126 * Check if there is traffic queued towards this peer
2127 * and the core transmit handle is NULL (traffic was stalled).
2128 * If so, call core tmt rdy.
2130 * @param peer_id Short ID of peer to which initiate traffic.
2133 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2135 struct MeshPeerInfo *peer;
2136 struct GNUNET_PeerIdentity id;
2137 struct MeshPeerQueue *q;
2140 peer = peer_get_short (peer_id);
2141 if (NULL != peer->core_transmit)
2144 q = queue_get_next (peer);
2147 /* Might br multicast traffic already sent to this particular peer but
2148 * not to other children in this tunnel.
2149 * This way t->queue_n would be > 0 but the queue of this particular peer
2155 GNUNET_PEER_resolve (peer->id, &id);
2156 peer->core_transmit =
2157 GNUNET_CORE_notify_transmit_ready(core_handle,
2160 GNUNET_TIME_UNIT_FOREVER_REL,
2170 * Send a message to all peers and clients in this tunnel that the tunnel
2171 * is no longer valid. If some peer or client should not receive the message,
2172 * should be zero'ed out before calling this function.
2174 * @param t The tunnel whose peers and clients to notify.
2177 tunnel_send_destroy (struct MeshTunnel *t)
2179 struct GNUNET_MESH_TunnelDestroy msg;
2180 struct GNUNET_PeerIdentity id;
2182 msg.header.size = htons (sizeof (msg));
2183 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2184 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2185 msg.tid = htonl (t->id.tid);
2186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2187 " sending tunnel destroy for tunnel: %s [%X]\n",
2188 GNUNET_i2s (&msg.oid), t->id.tid);
2190 if (NULL == t->client && 0 != t->next_hop)
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2193 GNUNET_PEER_resolve (t->next_hop, &id);
2194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2195 " sending forward to %s\n",
2197 send_prebuilt_message (&msg.header, t->next_hop, t);
2199 if (NULL == t->owner && 0 != t->prev_hop)
2201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2202 GNUNET_PEER_resolve (t->prev_hop, &id);
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2204 " sending back to %s\n",
2206 send_prebuilt_message (&msg.header, t->prev_hop, t);
2208 if (NULL != t->owner)
2210 send_client_tunnel_destroy (t->owner, t);
2212 if (NULL != t->client)
2214 send_client_tunnel_destroy (t->client, t);
2220 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2222 * @param t Tunnel which to cancel.
2223 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2226 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2228 struct MeshPeerInfo *peer_info;
2229 struct MeshPeerQueue *pq;
2230 struct MeshPeerQueue *next;
2233 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2234 peer_info = peer_get_short (neighbor);
2235 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2238 if (pq->tunnel == t)
2240 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2241 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2243 /* Should have been removed on destroy children */
2246 queue_destroy (pq, GNUNET_YES);
2249 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2251 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2252 peer_info->core_transmit = NULL;
2258 * Destroy the tunnel.
2260 * This function does not generate any warning traffic to clients or peers.
2263 * Remove the tunnel from peer_info's and clients' hashmaps.
2264 * Cancel messages belonging to this tunnel queued to neighbors.
2265 * Free any allocated resources linked to the tunnel.
2267 * @param t the tunnel to destroy
2269 * @return GNUNET_OK on success
2272 tunnel_destroy (struct MeshTunnel *t)
2274 struct MeshClient *c;
2275 struct GNUNET_HashCode hash;
2285 struct GNUNET_PeerIdentity id;
2287 GNUNET_PEER_resolve (t->id.oid, &id);
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2289 GNUNET_i2s (&id), t->id.tid);
2291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2295 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2296 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2304 GMC_hash32 (t->local_tid, &hash);
2306 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2313 if (NULL != t->client)
2316 GMC_hash32 (t->local_tid_dest, &hash);
2318 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2324 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2331 if (0 != t->prev_hop)
2333 peer_cancel_queues (t->prev_hop, t);
2334 GNUNET_PEER_change_rc (t->prev_hop, -1);
2336 if (0 != t->next_hop)
2338 peer_cancel_queues (t->next_hop, t);
2339 GNUNET_PEER_change_rc (t->next_hop, -1);
2342 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2343 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2346 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2347 path_destroy (t->path);
2353 * Tunnel is empty: destroy it.
2355 * Notifies all participants (peers, cleints) about the destruction.
2357 * @param t Tunnel to destroy.
2360 tunnel_destroy_empty (struct MeshTunnel *t)
2364 struct GNUNET_PeerIdentity id;
2366 GNUNET_PEER_resolve (t->id.oid, &id);
2367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2368 "executing destruction of empty tunnel %s [%X]\n",
2369 GNUNET_i2s (&id), t->id.tid);
2373 if (GNUNET_NO == t->destroy)
2374 tunnel_send_destroy (t);
2375 if (0 == t->pending_messages)
2378 t->destroy = GNUNET_YES;
2382 * Initialize a Flow Control structure to the initial state.
2384 * @param fc Flow Control structure to initialize.
2387 fc_init (struct MeshFlowControl *fc)
2389 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2390 fc->last_pid_recv = (uint32_t) -1;
2391 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2392 fc->last_ack_recv = (uint32_t) -1;
2393 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2394 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2399 * Create a new tunnel
2401 * @param owner Who is the owner of the tunnel (short ID).
2402 * @param tid Tunnel Number of the tunnel.
2403 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2404 * @param local Tunnel Number for the tunnel, for the client point of view.
2406 * @return A new initialized tunnel. NULL on error.
2408 static struct MeshTunnel *
2409 tunnel_new (GNUNET_PEER_Id owner,
2410 MESH_TunnelNumber tid,
2411 struct MeshClient *client,
2412 MESH_TunnelNumber local)
2414 struct MeshTunnel *t;
2415 struct GNUNET_HashCode hash;
2417 if (n_tunnels >= max_tunnels && NULL == client)
2420 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2423 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2425 fc_init (&t->next_fc);
2426 fc_init (&t->prev_fc);
2427 t->local_tid = local;
2429 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2431 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2433 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2434 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2441 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2448 GMC_hash32 (t->local_tid, &hash);
2450 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2451 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2455 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2466 * Iterator for deleting each tunnel whose client endpoint disconnected.
2468 * @param cls Closure (client that has disconnected).
2469 * @param key The hash of the local tunnel id (used to access the hashmap).
2470 * @param value The value stored at the key (tunnel to destroy).
2472 * @return GNUNET_OK, keep iterating.
2475 tunnel_destroy_iterator (void *cls,
2476 const struct GNUNET_HashCode * key,
2479 struct MeshTunnel *t = value;
2480 struct MeshClient *c = cls;
2482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2483 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2484 t->local_tid, t->local_tid_dest, c->id);
2485 client_delete_tunnel (c, t);
2488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2490 GNUNET_PEER_change_rc (t->next_hop, -1);
2493 else if (c == t->owner)
2495 struct MeshPeerInfo *p;
2496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2498 GNUNET_PEER_change_rc (t->prev_hop, -1);
2500 p = peer_get_short(t->dest);
2501 peer_info_remove_tunnel (p, t);
2507 tunnel_destroy_empty (t);
2514 * Timeout function, destroys tunnel if called
2516 * @param cls Closure (tunnel to destroy).
2517 * @param tc TaskContext
2520 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2522 struct MeshTunnel *t = cls;
2523 struct GNUNET_PeerIdentity id;
2525 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2526 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2528 GNUNET_PEER_resolve(t->id.oid, &id);
2529 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2530 "Tunnel %s [%X] timed out. Destroying.\n",
2531 GNUNET_i2s(&id), t->id.tid);
2532 if (NULL != t->client)
2533 send_client_tunnel_destroy (t->client, t);
2534 tunnel_destroy (t); /* Do not notify other */
2539 * Resets the tunnel timeout. Starts it if no timeout was running.
2541 * @param t Tunnel whose timeout to reset.
2543 * TODO use heap to improve efficiency of scheduler.
2546 tunnel_reset_timeout (struct MeshTunnel *t)
2548 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2550 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2551 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2552 t->maintenance_task =
2553 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2554 (refresh_path_time, 4), &tunnel_timeout, t);
2558 /******************************************************************************/
2559 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2560 /******************************************************************************/
2563 * Function to send a create path packet to a peer.
2565 * @param cls closure
2566 * @param size number of bytes available in buf
2567 * @param buf where the callee should write the message
2568 * @return number of bytes written to buf
2571 send_core_path_create (void *cls, size_t size, void *buf)
2573 struct MeshTunnel *t = cls;
2574 struct GNUNET_MESH_CreateTunnel *msg;
2575 struct GNUNET_PeerIdentity *peer_ptr;
2576 struct MeshPeerPath *p = t->path;
2581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2583 sizeof (struct GNUNET_MESH_CreateTunnel) +
2584 p->length * sizeof (struct GNUNET_PeerIdentity);
2586 if (size < size_needed || NULL == buf)
2591 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2592 msg->header.size = htons (size_needed);
2593 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2594 msg->tid = ntohl (t->id.tid);
2597 if (GNUNET_YES == t->nobuffer)
2598 opt |= MESH_TUNNEL_OPT_NOBUFFER;
2599 msg->opt = htonl (opt);
2600 msg->port = htonl (t->port);
2602 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2603 for (i = 0; i < p->length; i++)
2605 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2609 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2615 * Creates a path ack message in buf and frees all unused resources.
2617 * @param cls closure (MeshTransmissionDescriptor)
2618 * @param size number of bytes available in buf
2619 * @param buf where the callee should write the message
2620 * @return number of bytes written to buf
2623 send_core_path_ack (void *cls, size_t size, void *buf)
2625 struct MeshTunnel *t = cls;
2626 struct GNUNET_MESH_PathACK *msg = buf;
2628 GNUNET_assert (NULL != t);
2629 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2634 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
2635 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2636 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2637 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
2638 msg->tid = htonl (t->id.tid);
2639 msg->peer_id = my_full_id;
2640 msg->ack = htonl (t->prev_fc.last_ack_sent);
2642 /* TODO add signature */
2644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2645 return sizeof (struct GNUNET_MESH_PathACK);
2650 * Free a transmission that was already queued with all resources
2651 * associated to the request.
2653 * @param queue Queue handler to cancel.
2654 * @param clear_cls Is it necessary to free associated cls?
2657 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2659 struct MeshFlowControl *fc;
2661 if (GNUNET_YES == clear_cls)
2663 switch (queue->type)
2665 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2666 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2667 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2669 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2670 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2671 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2672 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2673 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2675 " prebuilt message\n");
2676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2678 GNUNET_MESH_DEBUG_M2S (queue->type));
2680 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2685 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2686 " type %s unknown!\n",
2687 GNUNET_MESH_DEBUG_M2S (queue->type));
2689 GNUNET_free_non_null (queue->cls);
2691 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2692 queue->peer->queue_tail,
2695 /* Delete from appropriate fc in the tunnel */
2696 if (queue->peer->id == queue->tunnel->next_hop)
2697 fc = &queue->tunnel->next_fc;
2698 else if (queue->peer->id == queue->tunnel->next_hop)
2699 fc = &queue->tunnel->next_fc;
2706 GNUNET_free (queue);
2711 * @brief Get the next transmittable message from the queue.
2713 * This will be the head, except in the case of being a data packet
2714 * not allowed by the destination peer.
2716 * @param peer Destination peer.
2718 * @return The next viable MeshPeerQueue element to send to that peer.
2719 * NULL when there are no transmittable messages.
2721 struct MeshPeerQueue *
2722 queue_get_next (const struct MeshPeerInfo *peer)
2724 struct MeshPeerQueue *q;
2726 struct GNUNET_MESH_Unicast *ucast;
2727 struct GNUNET_MESH_ToOrigin *to_orig;
2728 struct MeshTunnel* t;
2732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
2733 for (q = peer->queue_head; NULL != q; q = q->next)
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2738 GNUNET_MESH_DEBUG_M2S(q->type));
2741 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2742 ucast = (struct GNUNET_MESH_Unicast *) q->cls;
2743 pid = ntohl (ucast->pid);
2744 ack = t->next_fc.last_ack_recv;
2746 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2747 to_orig = (struct GNUNET_MESH_ToOrigin *) q->cls;
2748 pid = ntohl (to_orig->pid);
2749 ack = t->prev_fc.last_ack_recv;
2752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2757 "* ACK: %u, PID: %u\n",
2759 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2772 "* nothing found\n");
2778 queue_send (void *cls, size_t size, void *buf)
2780 struct MeshPeerInfo *peer = cls;
2781 struct GNUNET_MessageHeader *msg;
2782 struct MeshPeerQueue *queue;
2783 struct MeshTunnel *t;
2784 struct GNUNET_PeerIdentity dst_id;
2785 struct MeshFlowControl *fc;
2789 peer->core_transmit = NULL;
2791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
2792 queue = queue_get_next (peer);
2794 /* Queue has no internal mesh traffic nor sendable payload */
2797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
2798 if (NULL == peer->queue_head)
2799 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
2802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
2804 GNUNET_PEER_resolve (peer->id, &dst_id);
2805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2807 GNUNET_i2s (&dst_id));
2808 /* Check if buffer size is enough for the message */
2809 if (queue->size > size)
2811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2812 "* not enough room, reissue\n");
2813 peer->core_transmit =
2814 GNUNET_CORE_notify_transmit_ready (core_handle,
2817 GNUNET_TIME_UNIT_FOREVER_REL,
2824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
2827 GNUNET_assert (0 < t->pending_messages);
2828 t->pending_messages--;
2832 switch (queue->type)
2835 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2836 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2837 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
2838 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
2839 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2840 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2843 GNUNET_MESH_DEBUG_M2S (queue->type));
2845 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2846 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2847 data_size = send_core_data_raw (queue->cls, size, buf);
2848 msg = (struct GNUNET_MessageHeader *) buf;
2849 type = ntohs (msg->type);
2851 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
2853 data_size = send_core_path_create (queue->cls, size, buf);
2855 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
2857 data_size = send_core_path_ack (queue->cls, size, buf);
2861 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2862 "* type unknown: %u\n",
2867 /* Free queue, but cls was freed by send_core_* */
2868 queue_destroy (queue, GNUNET_NO);
2870 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
2873 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2874 t->next_fc.last_pid_sent = ((struct GNUNET_MESH_Unicast *) buf)->pid;
2875 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
2877 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2878 t->prev_fc.last_pid_sent = ((struct GNUNET_MESH_ToOrigin *) buf)->pid;
2879 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
2885 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
2887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
2891 /* If more data in queue, send next */
2892 queue = queue_get_next (peer);
2895 struct GNUNET_PeerIdentity id;
2897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
2898 GNUNET_PEER_resolve (peer->id, &id);
2899 peer->core_transmit =
2900 GNUNET_CORE_notify_transmit_ready(core_handle,
2903 GNUNET_TIME_UNIT_FOREVER_REL,
2909 else if (NULL != peer->queue_head)
2911 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2913 GNUNET_i2s (&my_full_id));
2914 if (peer->id == t->next_hop)
2916 else if (peer->id == t->prev_hop)
2923 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
2926 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
2936 * @brief Queue and pass message to core when possible.
2938 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
2939 * accounts for it. In case the queue is full, the message is dropped and
2942 * Otherwise, message is treated as internal and allowed to go regardless of
2945 * @param cls Closure (@c type dependant). It will be used by queue_send to
2946 * build the message to be sent if not already prebuilt.
2947 * @param type Type of the message, 0 for a raw message.
2948 * @param size Size of the message.
2949 * @param dst Neighbor to send message to.
2950 * @param t Tunnel this message belongs to.
2953 queue_add (void *cls, uint16_t type, size_t size,
2954 struct MeshPeerInfo *dst, struct MeshTunnel *t)
2956 struct MeshPeerQueue *queue;
2957 struct GNUNET_PeerIdentity id;
2961 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2963 n = &t->next_fc.queue_n;
2965 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
2967 n = &t->prev_fc.queue_n;
2971 if (*n >= t->queue_max)
2974 GNUNET_STATISTICS_update(stats,
2975 "# messages dropped (buffer full)",
2977 return; /* Drop message */
2981 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2987 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
2988 if (NULL == dst->core_transmit)
2990 GNUNET_PEER_resolve (dst->id, &id);
2991 dst->core_transmit =
2992 GNUNET_CORE_notify_transmit_ready (core_handle,
2995 GNUNET_TIME_UNIT_FOREVER_REL,
3001 t->pending_messages++;
3005 /******************************************************************************/
3006 /******************** MESH NETWORK HANDLERS **************************/
3007 /******************************************************************************/
3011 * Core handler for path creation
3013 * @param cls closure
3014 * @param message message
3015 * @param peer peer identity this notification is about
3017 * @return GNUNET_OK to keep the connection open,
3018 * GNUNET_SYSERR to close it (signal serious error)
3021 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3022 const struct GNUNET_MessageHeader *message)
3024 unsigned int own_pos;
3027 MESH_TunnelNumber tid;
3028 struct GNUNET_MESH_CreateTunnel *msg;
3029 struct GNUNET_PeerIdentity *pi;
3030 struct MeshPeerPath *path;
3031 struct MeshPeerInfo *dest_peer_info;
3032 struct MeshPeerInfo *orig_peer_info;
3033 struct MeshTunnel *t;
3035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3036 "Received a path create msg [%s]\n",
3037 GNUNET_i2s (&my_full_id));
3038 size = ntohs (message->size);
3039 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3041 GNUNET_break_op (0);
3045 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3046 if (size % sizeof (struct GNUNET_PeerIdentity))
3048 GNUNET_break_op (0);
3051 size /= sizeof (struct GNUNET_PeerIdentity);
3054 GNUNET_break_op (0);
3057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3058 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3060 tid = ntohl (msg->tid);
3061 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3063 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3064 t = tunnel_get (pi, tid);
3065 if (NULL == t) /* might be a local tunnel */
3069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3070 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3076 t->port = ntohl (msg->port);
3077 opt = ntohl (msg->opt);
3078 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
3080 t->nobuffer = GNUNET_YES;
3081 t->prev_fc.last_ack_sent = t->prev_fc.last_pid_recv + 1;
3082 t->next_fc.last_ack_sent = t->next_fc.last_pid_recv + 1;
3085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3087 tunnel_reset_timeout (t);
3089 t->state = MESH_TUNNEL_WAITING;
3091 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3092 if (NULL == dest_peer_info)
3094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3095 " Creating PeerInfo for destination.\n");
3096 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3097 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3098 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3100 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3102 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3103 if (NULL == orig_peer_info)
3105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3106 " Creating PeerInfo for origin.\n");
3107 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3108 orig_peer_info->id = GNUNET_PEER_intern (pi);
3109 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3110 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3113 path = path_new (size);
3115 for (i = 0; i < size; i++)
3117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3118 GNUNET_i2s (&pi[i]));
3119 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3120 if (path->peers[i] == myid)
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3124 if (own_pos == 0 && path->peers[own_pos] != myid)
3126 /* create path: self not found in path through self */
3127 GNUNET_break_op (0);
3128 path_destroy (path);
3132 path_add_to_peers (path, GNUNET_NO);
3133 tunnel_use_path (t, path);
3135 if (own_pos == size - 1)
3137 struct MeshClient *c;
3138 struct GNUNET_HashCode hc;
3140 /* Find target client */
3141 GMC_hash32 (t->port, &hc);
3142 c = GNUNET_CONTAINER_multihashmap_get (ports, &hc);
3145 /* TODO send reject */
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3150 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3153 /* Assign local tid */
3154 while (NULL != tunnel_get_incoming (next_local_tid))
3155 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3156 t->local_tid_dest = next_local_tid++;
3157 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3159 tunnel_add_client (t, c);
3160 send_client_tunnel_create (t);
3165 struct MeshPeerPath *path2;
3167 t->next_hop = path->peers[own_pos + 1];
3168 GNUNET_PEER_change_rc(t->next_hop, 1);
3170 /* It's for somebody else! Retransmit. */
3171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3172 path2 = path_duplicate (path);
3173 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3174 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3175 send_create_path (t);
3183 * Core handler for path ACKs
3185 * @param cls closure
3186 * @param message message
3187 * @param peer peer identity this notification is about
3189 * @return GNUNET_OK to keep the connection open,
3190 * GNUNET_SYSERR to close it (signal serious error)
3193 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3194 const struct GNUNET_MessageHeader *message)
3196 struct GNUNET_MESH_PathACK *msg;
3197 struct MeshPeerInfo *peer_info;
3198 struct MeshPeerPath *p;
3199 struct MeshTunnel *t;
3201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3202 GNUNET_i2s (&my_full_id));
3203 msg = (struct GNUNET_MESH_PathACK *) message;
3204 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3207 /* TODO notify that we don't know the tunnel */
3208 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3210 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3214 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3216 peer_info = peer_get (&msg->peer_id);
3217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3218 GNUNET_i2s (&msg->peer_id));
3219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3222 /* Add path to peers? */
3226 path_add_to_peers (p, GNUNET_YES);
3232 t->state = MESH_TUNNEL_READY;
3233 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3234 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3236 /* Message for us? */
3237 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3240 if (NULL == t->owner)
3242 GNUNET_break_op (0);
3245 if (NULL != peer_info->dhtget)
3247 GNUNET_DHT_get_stop (peer_info->dhtget);
3248 peer_info->dhtget = NULL;
3250 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3251 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3256 " not for us, retransmitting...\n");
3257 peer_info = peer_get (&msg->oid);
3258 send_prebuilt_message (message, t->prev_hop, t);
3264 * Core handler for notifications of broken paths
3266 * @param cls closure
3267 * @param message message
3268 * @param peer peer identity this notification is about
3270 * @return GNUNET_OK to keep the connection open,
3271 * GNUNET_SYSERR to close it (signal serious error)
3274 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3275 const struct GNUNET_MessageHeader *message)
3277 struct GNUNET_MESH_PathBroken *msg;
3278 struct MeshTunnel *t;
3280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3281 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3282 msg = (struct GNUNET_MESH_PathBroken *) message;
3283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3284 GNUNET_i2s (&msg->peer1));
3285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3286 GNUNET_i2s (&msg->peer2));
3287 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3290 GNUNET_break_op (0);
3293 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3294 GNUNET_PEER_search (&msg->peer2));
3301 * Core handler for tunnel destruction
3303 * @param cls closure
3304 * @param message message
3305 * @param peer peer identity this notification is about
3307 * @return GNUNET_OK to keep the connection open,
3308 * GNUNET_SYSERR to close it (signal serious error)
3311 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3312 const struct GNUNET_MessageHeader *message)
3314 struct GNUNET_MESH_TunnelDestroy *msg;
3315 struct MeshTunnel *t;
3317 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3319 "Got a TUNNEL DESTROY packet from %s\n",
3321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3322 " for tunnel %s [%u]\n",
3323 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3324 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3327 /* Probably already got the message from another path,
3328 * destroyed the tunnel and retransmitted to children.
3331 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3335 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3338 t->local_tid, t->local_tid_dest);
3340 if (GNUNET_PEER_search (peer) == t->prev_hop)
3342 // TODO check owner's signature
3343 // TODO add owner's signatue to tunnel for retransmission
3344 peer_cancel_queues (t->prev_hop, t);
3345 GNUNET_PEER_change_rc (t->prev_hop, -1);
3348 else if (GNUNET_PEER_search (peer) == t->next_hop)
3350 // TODO check dest's signature
3351 // TODO add dest's signatue to tunnel for retransmission
3352 peer_cancel_queues (t->next_hop, t);
3353 GNUNET_PEER_change_rc (t->next_hop, -1);
3358 GNUNET_break_op (0);
3359 // TODO check both owner AND destination's signature to see which matches
3360 // TODO restransmit in appropriate direction
3363 tunnel_destroy_empty (t);
3365 // TODO: add timeout to destroy the tunnel anyway
3371 * Core handler for mesh network traffic going from the origin to a peer
3373 * @param cls closure
3374 * @param peer peer identity this notification is about
3375 * @param message message
3376 * @return GNUNET_OK to keep the connection open,
3377 * GNUNET_SYSERR to close it (signal serious error)
3380 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3381 const struct GNUNET_MessageHeader *message)
3383 struct GNUNET_MESH_Unicast *msg;
3384 struct MeshTunnel *t;
3389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3392 size = ntohs (message->size);
3394 sizeof (struct GNUNET_MESH_Unicast) +
3395 sizeof (struct GNUNET_MessageHeader))
3400 msg = (struct GNUNET_MESH_Unicast *) message;
3401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3402 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3404 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3407 /* TODO notify back: we don't know this tunnel */
3408 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3409 GNUNET_break_op (0);
3412 pid = ntohl (msg->pid);
3413 if (t->prev_fc.last_pid_recv == pid)
3415 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3416 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3417 " Already seen pid %u, DROPPING!\n", pid);
3422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3423 " pid %u not seen yet, forwarding\n", pid);
3426 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3428 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3429 GNUNET_break_op (0);
3430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3431 "Received PID %u, ACK %u\n",
3432 pid, t->prev_fc.last_ack_sent);
3433 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3436 t->prev_fc.last_pid_recv = pid;
3438 tunnel_reset_timeout (t);
3439 if (t->dest == myid)
3441 /* TODO signature verification */
3442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3443 " it's for us! sending to clients...\n");
3444 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3445 tunnel_send_client_ucast (t, msg);
3446 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3449 if (0 == t->next_hop)
3454 ttl = ntohl (msg->ttl);
3455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3458 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3459 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3460 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3464 " not for us, retransmitting...\n");
3466 send_prebuilt_message (message, t->next_hop, t);
3467 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3473 * Core handler for mesh network traffic toward the owner of a tunnel
3475 * @param cls closure
3476 * @param message message
3477 * @param peer peer identity this notification is about
3479 * @return GNUNET_OK to keep the connection open,
3480 * GNUNET_SYSERR to close it (signal serious error)
3483 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3484 const struct GNUNET_MessageHeader *message)
3486 struct GNUNET_MESH_ToOrigin *msg;
3487 struct MeshTunnel *t;
3491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3493 size = ntohs (message->size);
3494 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3495 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3497 GNUNET_break_op (0);
3500 msg = (struct GNUNET_MESH_ToOrigin *) message;
3501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3502 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3503 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3504 pid = ntohl (msg->pid);
3508 /* TODO notify that we dont know this tunnel (whom)? */
3509 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3511 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3512 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3516 if (t->next_fc.last_pid_recv == pid)
3518 /* already seen this packet, drop */
3519 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3521 " Already seen pid %u, DROPPING!\n", pid);
3522 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3526 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3528 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3529 GNUNET_break_op (0);
3530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3531 "Received PID %u, ACK %u\n",
3532 pid, t->next_fc.last_ack_sent);
3533 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3536 t->next_fc.last_pid_recv = pid;
3537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3538 " pid %u not seen yet, forwarding\n", pid);
3540 if (myid == t->id.oid)
3542 /* TODO signature verification */
3543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3544 " it's for us! sending to clients...\n");
3545 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3546 tunnel_send_client_to_orig (t, msg);
3547 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3551 " not for us, retransmitting...\n");
3553 if (0 == t->prev_hop) /* No owner AND no prev hop */
3555 if (GNUNET_YES == t->destroy)
3557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3558 "to orig received on a dying tunnel %s [%X]\n",
3559 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3563 "unknown to origin at %s\n",
3564 GNUNET_i2s (&my_full_id));
3565 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3568 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3569 "on tunnel %s [%X]\n",
3570 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3573 send_prebuilt_message (message, t->prev_hop, t);
3574 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3581 * Core handler for mesh network traffic point-to-point acks.
3583 * @param cls closure
3584 * @param message message
3585 * @param peer peer identity this notification is about
3587 * @return GNUNET_OK to keep the connection open,
3588 * GNUNET_SYSERR to close it (signal serious error)
3591 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3592 const struct GNUNET_MessageHeader *message)
3594 struct GNUNET_MESH_ACK *msg;
3595 struct MeshTunnel *t;
3599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3601 msg = (struct GNUNET_MESH_ACK *) message;
3603 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3607 /* TODO notify that we dont know this tunnel (whom)? */
3608 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3611 ack = ntohl (msg->pid);
3612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3614 /* Is this a forward or backward ACK? */
3615 id = GNUNET_PEER_search (peer);
3616 if (t->next_hop == id)
3619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3620 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task &&
3621 GMC_is_pid_bigger (ack, t->next_fc.last_ack_recv))
3623 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3624 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3625 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3627 t->next_fc.last_ack_recv = ack;
3628 peer_unlock_queue (t->next_hop);
3629 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3631 else if (t->prev_hop == id)
3634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3635 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
3636 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
3638 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3639 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3640 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3642 t->prev_fc.last_ack_recv = ack;
3643 peer_unlock_queue (t->prev_hop);
3644 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3647 GNUNET_break_op (0);
3653 * Core handler for mesh network traffic point-to-point ack polls.
3655 * @param cls closure
3656 * @param message message
3657 * @param peer peer identity this notification is about
3659 * @return GNUNET_OK to keep the connection open,
3660 * GNUNET_SYSERR to close it (signal serious error)
3663 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3664 const struct GNUNET_MessageHeader *message)
3666 struct GNUNET_MESH_Poll *msg;
3667 struct MeshTunnel *t;
3670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3673 msg = (struct GNUNET_MESH_Poll *) message;
3675 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3679 /* TODO notify that we dont know this tunnel (whom)? */
3680 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
3681 GNUNET_break_op (0);
3685 /* Is this a forward or backward ACK? */
3686 id = GNUNET_PEER_search(peer);
3687 if (t->next_hop == id)
3689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
3690 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3692 else if (t->prev_hop == id)
3694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
3695 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3705 * Core handler for mesh keepalives.
3707 * @param cls closure
3708 * @param message message
3709 * @param peer peer identity this notification is about
3710 * @return GNUNET_OK to keep the connection open,
3711 * GNUNET_SYSERR to close it (signal serious error)
3713 * TODO: Check who we got this from, to validate route.
3716 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
3717 const struct GNUNET_MessageHeader *message)
3719 struct GNUNET_MESH_TunnelKeepAlive *msg;
3720 struct MeshTunnel *t;
3722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
3725 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
3726 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3730 /* TODO notify that we dont know that tunnel */
3731 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
3736 tunnel_reset_timeout (t);
3738 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
3739 send_prebuilt_message (message, t->next_hop, t);
3746 * Functions to handle messages from core
3748 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3749 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3750 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3751 sizeof (struct GNUNET_MESH_PathBroken)},
3752 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
3753 sizeof (struct GNUNET_MESH_TunnelDestroy)},
3754 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3755 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
3756 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
3757 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3758 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
3759 sizeof (struct GNUNET_MESH_ACK)},
3760 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
3761 sizeof (struct GNUNET_MESH_Poll)},
3762 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3763 sizeof (struct GNUNET_MESH_PathACK)},
3769 /******************************************************************************/
3770 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3771 /******************************************************************************/
3776 * notify_client_connection_failure: notify a client that the connection to the
3777 * requested remote peer is not possible (for instance, no route found)
3778 * Function called when the socket is ready to queue more data. "buf" will be
3779 * NULL and "size" zero if the socket was closed for writing in the meantime.
3781 * @param cls closure
3782 * @param size number of bytes available in buf
3783 * @param buf where the callee should write the message
3784 * @return number of bytes written to buf
3787 notify_client_connection_failure (void *cls, size_t size, void *buf)
3790 struct MeshPeerInfo *peer_info;
3791 struct GNUNET_MESH_PeerControl *msg;
3792 struct GNUNET_PeerIdentity id;
3794 if (0 == size && NULL == buf)
3796 // TODO retry? cancel?
3800 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3801 peer_info = (struct MeshPeerInfo *) cls;
3802 msg = (struct GNUNET_MESH_PeerControl *) buf;
3803 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3804 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3805 // msg->tunnel_id = htonl(peer_info->t->tid);
3806 GNUNET_PEER_resolve (peer_info->id, &id);
3807 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3815 * Send keepalive packets for a tunnel.
3817 * @param cls Closure (tunnel for which to send the keepalive).
3818 * @param tc Notification context.
3821 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3823 struct MeshTunnel *t = cls;
3824 struct GNUNET_MESH_TunnelKeepAlive *msg;
3825 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
3828 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3829 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
3830 NULL == t->owner || 0 == t->local_tid || 0 != t->prev_hop)
3835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3836 "sending keepalive for tunnel %d\n", t->id.tid);
3838 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
3839 msg->header.size = htons (size);
3840 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3841 msg->oid = my_full_id;
3842 msg->tid = htonl (t->id.tid);
3843 send_prebuilt_message (&msg->header, t->next_hop, t);
3845 t->maintenance_task =
3846 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3851 * Function to process paths received for a new peer addition. The recorded
3852 * paths form the initial tunnel, which can be optimized later.
3853 * Called on each result obtained for the DHT search.
3855 * @param cls closure
3856 * @param exp when will this value expire
3857 * @param key key of the result
3858 * @param get_path path of the get request
3859 * @param get_path_length lenght of get_path
3860 * @param put_path path of the put request
3861 * @param put_path_length length of the put_path
3862 * @param type type of the result
3863 * @param size number of bytes in data
3864 * @param data pointer to the result data
3867 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3868 const struct GNUNET_HashCode * key,
3869 const struct GNUNET_PeerIdentity *get_path,
3870 unsigned int get_path_length,
3871 const struct GNUNET_PeerIdentity *put_path,
3872 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3873 size_t size, const void *data)
3875 struct MeshPeerInfo *peer = cls;
3876 struct MeshPeerPath *p;
3877 struct GNUNET_PeerIdentity pi;
3880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3881 GNUNET_PEER_resolve (peer->id, &pi);
3882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3884 p = path_build_from_dht (get_path, get_path_length,
3885 put_path, put_path_length);
3886 path_add_to_peers (p, GNUNET_NO);
3888 for (i = 0; i < peer->ntunnels; i++)
3890 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
3891 peer_connect (peer, peer->tunnels[i]);
3898 /******************************************************************************/
3899 /********************* MESH LOCAL HANDLES **************************/
3900 /******************************************************************************/
3904 * Handler for client disconnection
3906 * @param cls closure
3907 * @param client identification of the client; NULL
3908 * for the last call when the server is destroyed
3911 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3913 struct MeshClient *c;
3914 struct MeshClient *next;
3916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3926 if (c->handle != client)
3928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3934 GNUNET_SERVER_client_drop (c->handle);
3935 c->shutting_down = GNUNET_YES;
3936 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3937 &tunnel_destroy_iterator, c);
3938 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3939 &tunnel_destroy_iterator, c);
3940 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3941 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3943 if (NULL != c->ports)
3944 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
3946 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
3947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3949 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
3952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
3958 * Handler for new clients
3960 * @param cls closure
3961 * @param client identification of the client
3962 * @param message the actual message, which includes messages the client wants
3965 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3966 const struct GNUNET_MessageHeader *message)
3968 struct GNUNET_MESH_ClientConnect *cc_msg;
3969 struct MeshClient *c;
3974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3976 /* Check data sanity */
3977 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3978 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3979 if (0 != (size % sizeof (uint32_t)))
3982 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3985 size /= sizeof (uint32_t);
3987 /* Create new client structure */
3988 c = GNUNET_malloc (sizeof (struct MeshClient));
3989 c->id = next_client_id++; /* overflow not important: just for debug */
3990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
3991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
3993 GNUNET_SERVER_client_keep (client);
3997 struct GNUNET_HashCode hc;
3999 p = (uint32_t *) &cc_msg[1];
4000 c->ports = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
4001 for (i = 0; i < size; i++)
4004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4005 GMC_hash32 (u32, &hc);
4007 /* store in client's hashmap */
4008 GNUNET_CONTAINER_multihashmap_put (c->ports, &hc, c,
4009 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4010 /* store in global hashmap */
4011 /* FIXME only allow one client to have the port open,
4012 * have a backup hashmap with waiting clients */
4013 GNUNET_CONTAINER_multihashmap_put (ports, &hc, c,
4014 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4018 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4019 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4020 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4021 GNUNET_SERVER_notification_context_add (nc, client);
4022 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4024 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4030 * Handler for requests of new tunnels
4032 * @param cls Closure.
4033 * @param client Identification of the client.
4034 * @param message The actual message.
4037 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4038 const struct GNUNET_MessageHeader *message)
4040 struct GNUNET_MESH_TunnelMessage *t_msg;
4041 struct MeshPeerInfo *peer_info;
4042 struct MeshTunnel *t;
4043 struct MeshClient *c;
4044 MESH_TunnelNumber tid;
4046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4048 /* Sanity check for client registration */
4049 if (NULL == (c = client_get (client)))
4052 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4057 /* Message sanity check */
4058 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4061 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4065 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4067 GNUNET_i2s (&t_msg->peer));
4068 /* Sanity check for tunnel numbering */
4069 tid = ntohl (t_msg->tunnel_id);
4070 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4076 /* Sanity check for duplicate tunnel IDs */
4077 if (NULL != tunnel_get_by_local_id (c, tid))
4080 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4084 while (NULL != tunnel_get_by_pi (myid, next_tid))
4085 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4086 t = tunnel_new (myid, next_tid, c, tid);
4087 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4091 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4094 t->port = ntohl (t_msg->port);
4095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4096 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4098 peer_info = peer_get (&t_msg->peer);
4099 GNUNET_array_append (peer_info->tunnels, peer_info->ntunnels, t);
4100 peer_connect (peer_info, t);
4101 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4107 * Handler for requests of deleting tunnels
4109 * @param cls closure
4110 * @param client identification of the client
4111 * @param message the actual message
4114 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4115 const struct GNUNET_MessageHeader *message)
4117 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4118 struct MeshClient *c;
4119 struct MeshTunnel *t;
4120 MESH_TunnelNumber tid;
4122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4123 "Got a DESTROY TUNNEL from client!\n");
4125 /* Sanity check for client registration */
4126 if (NULL == (c = client_get (client)))
4129 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4134 /* Message sanity check */
4135 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4138 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4142 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4144 /* Retrieve tunnel */
4145 tid = ntohl (tunnel_msg->tunnel_id);
4146 t = tunnel_get_by_local_id(c, tid);
4149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4151 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4155 /* Cleanup after the tunnel */
4156 client_delete_tunnel (c, t);
4161 else if (c == t->owner)
4163 peer_info_remove_tunnel (peer_get_short(t->dest), t);
4167 /* The tunnel will be destroyed when the last message is transmitted. */
4168 tunnel_destroy_empty (t);
4170 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4176 * Handler for requests of seeting tunnel's buffering policy.
4178 * @param cls Closure (unused).
4179 * @param client Identification of the client.
4180 * @param message The actual message.
4183 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
4184 const struct GNUNET_MessageHeader *message)
4186 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4187 struct MeshClient *c;
4188 struct MeshTunnel *t;
4189 MESH_TunnelNumber tid;
4191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4192 "Got a BUFFER request from client!\n");
4194 /* Sanity check for client registration */
4195 if (NULL == (c = client_get (client)))
4198 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4203 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4205 /* Retrieve tunnel */
4206 tid = ntohl (tunnel_msg->tunnel_id);
4207 t = tunnel_get_by_local_id(c, tid);
4210 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4212 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4216 switch (ntohs(message->type))
4218 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
4219 t->nobuffer = GNUNET_NO;
4221 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
4222 t->nobuffer = GNUNET_YES;
4228 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4233 * Handler for client traffic directed to one peer
4235 * @param cls closure
4236 * @param client identification of the client
4237 * @param message the actual message
4240 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4241 const struct GNUNET_MessageHeader *message)
4243 struct MeshClient *c;
4244 struct MeshTunnel *t;
4245 struct GNUNET_MESH_Unicast *data_msg;
4246 MESH_TunnelNumber tid;
4249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4250 "Got a unicast request from a client!\n");
4252 /* Sanity check for client registration */
4253 if (NULL == (c = client_get (client)))
4256 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4261 data_msg = (struct GNUNET_MESH_Unicast *) message;
4263 /* Sanity check for message size */
4264 size = ntohs (message->size);
4265 if (sizeof (struct GNUNET_MESH_Unicast) +
4266 sizeof (struct GNUNET_MessageHeader) > size)
4269 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4273 /* Tunnel exists? */
4274 tid = ntohl (data_msg->tid);
4275 t = tunnel_get_by_local_id (c, tid);
4279 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4283 /* Is it a local tunnel? Then, does client own the tunnel? */
4284 if (t->owner->handle != client)
4287 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4291 /* PID should be as expected: client<->service communication */
4292 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4295 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4296 "Unicast PID, expected %u, got %u\n",
4297 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4298 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4302 /* Ok, everything is correct, send the message
4303 * (pretend we got it from a mesh peer)
4306 /* Work around const limitation */
4307 char buf[ntohs (message->size)] GNUNET_ALIGN;
4308 struct GNUNET_MESH_Unicast *copy;
4310 copy = (struct GNUNET_MESH_Unicast *) buf;
4311 memcpy (buf, data_msg, size);
4312 copy->oid = my_full_id;
4313 copy->tid = htonl (t->id.tid);
4314 copy->ttl = htonl (default_ttl);
4315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4316 " calling generic handler...\n");
4317 handle_mesh_unicast (NULL, &my_full_id, ©->header);
4319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4320 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4327 * Handler for client traffic directed to the origin
4329 * @param cls closure
4330 * @param client identification of the client
4331 * @param message the actual message
4334 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4335 const struct GNUNET_MessageHeader *message)
4337 struct GNUNET_MESH_ToOrigin *data_msg;
4338 struct MeshFlowControl *fc;
4339 struct MeshClient *c;
4340 struct MeshTunnel *t;
4341 MESH_TunnelNumber tid;
4344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4345 "Got a ToOrigin request from a client!\n");
4346 /* Sanity check for client registration */
4347 if (NULL == (c = client_get (client)))
4350 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4355 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4357 /* Sanity check for message size */
4358 size = ntohs (message->size);
4359 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4360 sizeof (struct GNUNET_MessageHeader) > size)
4363 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4367 /* Tunnel exists? */
4368 tid = ntohl (data_msg->tid);
4369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4370 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4373 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4376 t = tunnel_get_by_local_id (c, tid);
4379 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4380 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4382 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4386 /* It should be sent by someone who has this as incoming tunnel. */
4390 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4394 /* PID should be as expected */
4396 if (ntohl (data_msg->pid) != fc->last_pid_recv + 1)
4399 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4400 "To Origin PID, expected %u, got %u\n",
4401 fc->last_pid_recv + 1,
4402 ntohl (data_msg->pid));
4403 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4407 /* Ok, everything is correct, send the message
4408 * (pretend we got it from a mesh peer)
4411 char buf[ntohs (message->size)] GNUNET_ALIGN;
4412 struct GNUNET_MESH_ToOrigin *copy;
4414 /* Work around 'const' limitation */
4415 memcpy (buf, data_msg, size);
4416 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4417 GNUNET_PEER_resolve (t->id.oid, ©->oid);
4418 copy->tid = htonl (t->id.tid);
4419 copy->ttl = htonl (default_ttl);
4421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4422 " calling generic handler...\n");
4423 handle_mesh_to_orig (NULL, &my_full_id, ©->header);
4425 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4432 * Handler for client's ACKs for payload traffic.
4434 * @param cls Closure (unused).
4435 * @param client Identification of the client.
4436 * @param message The actual message.
4439 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4440 const struct GNUNET_MessageHeader *message)
4442 struct GNUNET_MESH_LocalAck *msg;
4443 struct MeshTunnel *t;
4444 struct MeshClient *c;
4445 MESH_TunnelNumber tid;
4448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4449 /* Sanity check for client registration */
4450 if (NULL == (c = client_get (client)))
4453 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4458 msg = (struct GNUNET_MESH_LocalAck *) message;
4460 /* Tunnel exists? */
4461 tid = ntohl (msg->tunnel_id);
4462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4463 t = tunnel_get_by_local_id (c, tid);
4467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4468 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4469 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4473 ack = ntohl (msg->ack);
4474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4476 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4479 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4480 t->prev_fc.last_ack_recv = ack;
4481 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4485 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4486 t->next_fc.last_ack_recv = ack;
4487 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4490 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4498 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4500 * @param cls Closure (client handle).
4501 * @param key Key (hashed tunnel ID, unused).
4502 * @param value Tunnel info.
4504 * @return GNUNET_YES, to keep iterating.
4507 monitor_all_tunnels_iterator (void *cls,
4508 const struct GNUNET_HashCode * key,
4511 struct GNUNET_SERVER_Client *client = cls;
4512 struct MeshTunnel *t = value;
4513 struct GNUNET_MESH_LocalMonitor *msg;
4515 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4516 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4517 msg->tunnel_id = htonl (t->id.tid);
4518 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4519 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4520 GNUNET_PEER_resolve (t->dest, &msg->destination);
4522 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4523 "* sending info about tunnel %s [%u]\n",
4524 GNUNET_i2s (&msg->owner), t->id.tid);
4526 GNUNET_SERVER_notification_context_unicast (nc, client,
4527 &msg->header, GNUNET_NO);
4533 * Handler for client's MONITOR request.
4535 * @param cls Closure (unused).
4536 * @param client Identification of the client.
4537 * @param message The actual message.
4540 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4541 const struct GNUNET_MessageHeader *message)
4543 struct MeshClient *c;
4545 /* Sanity check for client registration */
4546 if (NULL == (c = client_get (client)))
4549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4553 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4554 "Received get tunnels request from client %u\n",
4556 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4557 monitor_all_tunnels_iterator,
4559 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4560 "Get tunnels request from client %u completed\n",
4562 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4567 * Handler for client's MONITOR_TUNNEL request.
4569 * @param cls Closure (unused).
4570 * @param client Identification of the client.
4571 * @param message The actual message.
4574 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4575 const struct GNUNET_MessageHeader *message)
4577 const struct GNUNET_MESH_LocalMonitor *msg;
4578 struct GNUNET_MESH_LocalMonitor *resp;
4579 struct MeshClient *c;
4580 struct MeshTunnel *t;
4582 /* Sanity check for client registration */
4583 if (NULL == (c = client_get (client)))
4586 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4590 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4591 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4592 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4595 ntohl (msg->tunnel_id));
4596 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4599 /* We don't know the tunnel FIXME */
4600 struct GNUNET_MESH_LocalMonitor warn;
4603 GNUNET_SERVER_notification_context_unicast (nc, client,
4606 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4610 /* Initialize context */
4611 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4613 GNUNET_PEER_resolve (t->dest, &resp->destination);
4614 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4615 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4616 &resp->header, GNUNET_NO);
4619 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4620 "Monitor tunnel request from client %u completed\n",
4622 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4627 * Functions to handle messages from clients
4629 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4630 {&handle_local_new_client, NULL,
4631 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4632 {&handle_local_tunnel_create, NULL,
4633 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4634 sizeof (struct GNUNET_MESH_TunnelMessage)},
4635 {&handle_local_tunnel_destroy, NULL,
4636 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4637 sizeof (struct GNUNET_MESH_TunnelMessage)},
4638 {&handle_local_tunnel_buffer, NULL,
4639 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
4640 sizeof (struct GNUNET_MESH_TunnelMessage)},
4641 {&handle_local_tunnel_buffer, NULL,
4642 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
4643 sizeof (struct GNUNET_MESH_TunnelMessage)},
4644 {&handle_local_unicast, NULL,
4645 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4646 {&handle_local_to_origin, NULL,
4647 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4648 {&handle_local_ack, NULL,
4649 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4650 sizeof (struct GNUNET_MESH_LocalAck)},
4651 {&handle_local_get_tunnels, NULL,
4652 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4653 sizeof (struct GNUNET_MessageHeader)},
4654 {&handle_local_show_tunnel, NULL,
4655 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4656 sizeof (struct GNUNET_MESH_LocalMonitor)},
4662 * Method called whenever a given peer connects.
4664 * @param cls closure
4665 * @param peer peer identity this notification is about
4668 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4670 struct MeshPeerInfo *peer_info;
4671 struct MeshPeerPath *path;
4673 DEBUG_CONN ("Peer connected\n");
4674 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4675 peer_info = peer_get (peer);
4676 if (myid == peer_info->id)
4678 DEBUG_CONN (" (self)\n");
4683 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4685 path = path_new (2);
4686 path->peers[0] = myid;
4687 path->peers[1] = peer_info->id;
4688 GNUNET_PEER_change_rc (myid, 1);
4689 GNUNET_PEER_change_rc (peer_info->id, 1);
4690 peer_info_add_path (peer_info, path, GNUNET_YES);
4691 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4697 * Method called whenever a peer disconnects.
4699 * @param cls closure
4700 * @param peer peer identity this notification is about
4703 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4705 struct MeshPeerInfo *pi;
4706 struct MeshPeerQueue *q;
4707 struct MeshPeerQueue *n;
4709 DEBUG_CONN ("Peer disconnected\n");
4710 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4720 /* TODO try to reroute this traffic instead */
4721 queue_destroy(q, GNUNET_YES);
4724 if (NULL != pi->core_transmit)
4726 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4727 pi->core_transmit = NULL;
4729 peer_remove_path (pi, pi->id, myid);
4732 DEBUG_CONN (" (self)\n");
4734 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
4740 * Install server (service) handlers and start listening to clients.
4745 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4746 GNUNET_SERVER_disconnect_notify (server_handle,
4747 &handle_local_client_disconnect, NULL);
4748 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
4750 clients_head = NULL;
4751 clients_tail = NULL;
4753 GNUNET_SERVER_resume (server_handle);
4758 * To be called on core init/fail.
4760 * @param cls Closure (config)
4761 * @param server handle to the server for this service
4762 * @param identity the public identity of this peer
4765 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4766 const struct GNUNET_PeerIdentity *identity)
4768 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4772 GNUNET_break (core_handle == server);
4773 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4776 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4777 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4779 GNUNET_i2s (identity));
4780 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4782 GNUNET_i2s (&my_full_id));
4783 GNUNET_CORE_disconnect (core_handle);
4784 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4785 NULL, /* Closure passed to MESH functions */
4786 &core_init, /* Call core_init once connected */
4787 &core_connect, /* Handle connects */
4788 &core_disconnect, /* remove peers on disconnects */
4789 NULL, /* Don't notify about all incoming messages */
4790 GNUNET_NO, /* For header only in notification */
4791 NULL, /* Don't notify about all outbound messages */
4792 GNUNET_NO, /* For header-only out notification */
4793 core_handlers); /* Register these handlers */
4802 /******************************************************************************/
4803 /************************ MAIN FUNCTIONS ****************************/
4804 /******************************************************************************/
4807 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4809 * @param cls closure
4810 * @param key current key code
4811 * @param value value in the hash map
4812 * @return GNUNET_YES if we should continue to iterate,
4816 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
4818 struct MeshTunnel *t = value;
4825 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4827 * @param cls closure
4828 * @param key current key code
4829 * @param value value in the hash map
4830 * @return GNUNET_YES if we should continue to iterate,
4834 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
4836 struct MeshPeerInfo *p = value;
4837 struct MeshPeerQueue *q;
4838 struct MeshPeerQueue *n;
4846 queue_destroy(q, GNUNET_YES);
4850 peer_info_destroy (p);
4856 * Task run during shutdown.
4862 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4866 if (core_handle != NULL)
4868 GNUNET_CORE_disconnect (core_handle);
4873 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
4876 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4877 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4878 if (dht_handle != NULL)
4880 GNUNET_DHT_disconnect (dht_handle);
4885 GNUNET_SERVER_notification_context_destroy (nc);
4888 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4890 GNUNET_SCHEDULER_cancel (announce_id_task);
4891 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4898 * Callback for hostkey read/generation.
4900 * @param cls Closure (Configuration handle).
4901 * @param pk The ECC private key.
4902 * @param emsg Error message, if any.
4905 key_generation_cb (void *cls,
4906 struct GNUNET_CRYPTO_EccPrivateKey *pk,
4909 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4910 struct MeshPeerInfo *peer;
4911 struct MeshPeerPath *p;
4916 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4917 _("Could not access hostkey: %s. Exiting.\n"),
4919 GNUNET_SCHEDULER_shutdown ();
4922 my_private_key = pk;
4923 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
4924 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4925 &my_full_id.hashPubKey);
4926 myid = GNUNET_PEER_intern (&my_full_id);
4927 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4928 "Mesh for peer [%s] starting\n",
4929 GNUNET_i2s(&my_full_id));
4931 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4932 NULL, /* Closure passed to MESH functions */
4933 &core_init, /* Call core_init once connected */
4934 &core_connect, /* Handle connects */
4935 &core_disconnect, /* remove peers on disconnects */
4936 NULL, /* Don't notify about all incoming messages */
4937 GNUNET_NO, /* For header only in notification */
4938 NULL, /* Don't notify about all outbound messages */
4939 GNUNET_NO, /* For header-only out notification */
4940 core_handlers); /* Register these handlers */
4941 if (core_handle == NULL)
4944 GNUNET_SCHEDULER_shutdown ();
4949 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4951 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4953 /* Create a peer_info for the local peer */
4954 peer = peer_get (&my_full_id);
4957 GNUNET_PEER_change_rc (myid, 1);
4958 peer_info_add_path (peer, p, GNUNET_YES);
4960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
4965 * Process mesh requests.
4967 * @param cls closure
4968 * @param server the initialized server
4969 * @param c configuration to use
4972 run (void *cls, struct GNUNET_SERVER_Handle *server,
4973 const struct GNUNET_CONFIGURATION_Handle *c)
4977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4978 server_handle = server;
4979 GNUNET_SERVER_suspend (server_handle);
4982 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
4985 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4987 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4988 "mesh", "peer/privatekey");
4989 GNUNET_SCHEDULER_shutdown ();
4994 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
4995 &refresh_path_time))
4997 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4999 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5000 "mesh", "refresh path time");
5001 GNUNET_SCHEDULER_shutdown ();
5006 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5009 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5011 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5012 "mesh", "id announce time");
5013 GNUNET_SCHEDULER_shutdown ();
5018 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5021 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5023 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5024 "mesh", "connect timeout");
5025 GNUNET_SCHEDULER_shutdown ();
5030 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5033 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5035 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5036 "mesh", "max msgs queue");
5037 GNUNET_SCHEDULER_shutdown ();
5042 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5045 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5047 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5048 "mesh", "max tunnels");
5049 GNUNET_SCHEDULER_shutdown ();
5054 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5057 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5059 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5060 "mesh", "default ttl", 64);
5065 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5068 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5069 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5070 "mesh", "max peers", 1000);
5075 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5076 &dht_replication_level))
5078 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5080 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5081 "mesh", "dht replication level", 3);
5082 dht_replication_level = 3;
5085 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5086 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5087 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5088 ports = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5090 dht_handle = GNUNET_DHT_connect (c, 64);
5091 if (NULL == dht_handle)
5095 stats = GNUNET_STATISTICS_create ("mesh", c);
5097 /* Scheduled the task to clean up when shutdown is called */
5098 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5100 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
5103 GNUNET_free (keyfile);
5108 * The main function for the mesh service.
5110 * @param argc number of arguments from the command line
5111 * @param argv command line arguments
5112 * @return 0 ok, 1 on error
5115 main (int argc, char *const *argv)
5120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5121 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5123 ret = (GNUNET_OK == r) ? 0 : 1;
5124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5128 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5129 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5130 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);