2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
27 * - relay corking down to core
28 * - set ttl relative to path length
31 * - keep queues until receiving ACK
37 #include "mesh_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 payload 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 * Is the tunnel reliable?
329 * Force sending ACK? Flag to allow duplicate ACK on POLL.
334 * How many messages do we accept in the forward queue.
336 unsigned int queue_max;
339 * Last time the tunnel was used
341 struct GNUNET_TIME_Absolute timestamp;
344 * Destination of the tunnel.
349 * Next hop in the tunnel. If 0, @c client must be set.
351 GNUNET_PEER_Id next_hop;
354 * Previous hop in the tunnel. If 0, @c owner must be set.
356 GNUNET_PEER_Id prev_hop;
359 * Flow control information about @c next_hop or @c client.
361 struct MeshFlowControl next_fc;
364 * Flow control information about @c prev_hop or @c owner.
366 struct MeshFlowControl prev_fc;
369 * Client owner of the tunnel, if any
371 struct MeshClient *owner;
374 * Client destination of the tunnel, if any.
376 struct MeshClient *client;
379 * Task to keep the used paths alive at the owner,
380 * time tunnel out on all the other peers.
382 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
385 * Path being used for the tunnel.
387 struct MeshPeerPath *path;
390 * Flag to signal the destruction of the tunnel.
391 * If this is set GNUNET_YES the tunnel will be destroyed
392 * when the queue is empty.
397 * Total messages pending for this tunnels, payload or not.
399 unsigned int pending_messages;
404 * Struct containing information about a client of the service
406 * TODO: add a list of 'waiting' ports
413 struct MeshClient *next;
418 struct MeshClient *prev;
421 * Tunnels that belong to this client, indexed by local id
423 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
426 * Tunnels this client has accepted, indexed by incoming local id
428 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
431 * Handle to communicate with the client
433 struct GNUNET_SERVER_Client *handle;
436 * Ports that this client has declared interest in.
437 * Indexed by a GMC_hash32 (type), contains *Client.
439 struct GNUNET_CONTAINER_MultiHashMap *ports;
442 * Whether the client is active or shutting down (don't send confirmations
443 * to a client that is shutting down.
448 * ID of the client, mainly for debug messages
455 /******************************************************************************/
456 /************************ DEBUG FUNCTIONS ****************************/
457 /******************************************************************************/
461 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
462 * @param cls string to print
463 * @param success GNUNET_OK if the PUT was transmitted,
464 * GNUNET_NO on timeout,
465 * GNUNET_SYSERR on disconnect from service
466 * after the PUT message was transmitted
467 * (so we don't know if it was received or not)
472 mesh_debug (void *cls, int success)
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
481 unsigned int debug_fwd_ack;
482 unsigned int debug_bck_ack;
486 /******************************************************************************/
487 /*********************** GLOBAL VARIABLES ****************************/
488 /******************************************************************************/
490 /************************** Configuration parameters **************************/
493 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
495 static struct GNUNET_TIME_Relative refresh_path_time;
498 * How often to PUT own ID in the DHT.
500 static struct GNUNET_TIME_Relative id_announce_time;
503 * Maximum time allowed to connect to a peer found by string.
505 static struct GNUNET_TIME_Relative connect_timeout;
508 * Default TTL for payload packets.
510 static unsigned long long default_ttl;
513 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
515 static unsigned long long dht_replication_level;
518 * How many tunnels are we willing to maintain.
519 * Local tunnels are always allowed, even if there are more tunnels than max.
521 static unsigned long long max_tunnels;
524 * How many messages *in total* are we willing to queue, divided by number of
525 * tunnels to get tunnel queue size.
527 static unsigned long long max_msgs_queue;
530 * How many peers do we want to remember?
532 static unsigned long long max_peers;
535 * Percentage of messages that will be dropped (for test purposes only).
537 static unsigned long long drop_percent;
539 /*************************** Static global variables **************************/
542 * Hostkey generation context
544 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
547 * DLL with all the clients, head.
549 static struct MeshClient *clients_head;
552 * DLL with all the clients, tail.
554 static struct MeshClient *clients_tail;
557 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
559 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
562 * Number of tunnels known.
564 static unsigned long long n_tunnels;
567 * Tunnels incoming, indexed by MESH_TunnelNumber
568 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
570 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
573 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
575 static struct GNUNET_CONTAINER_MultiHashMap *peers;
578 * Handle to communicate with transport
580 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
583 * Handle to communicate with core.
585 static struct GNUNET_CORE_Handle *core_handle;
590 static struct GNUNET_DHT_Handle *dht_handle;
595 static struct GNUNET_SERVER_Handle *server_handle;
598 * Handle to the statistics service.
600 static struct GNUNET_STATISTICS_Handle *stats;
603 * Notification context, to send messages to local clients.
605 static struct GNUNET_SERVER_NotificationContext *nc;
608 * Local peer own ID (memory efficient handle).
610 static GNUNET_PEER_Id myid;
613 * Local peer own ID (full value).
615 static struct GNUNET_PeerIdentity my_full_id;
620 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
625 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
628 * Tunnel ID for the next created tunnel (global tunnel number).
630 static MESH_TunnelNumber next_tid;
633 * Tunnel ID for the next incoming tunnel (local tunnel number).
635 static MESH_TunnelNumber next_local_tid;
638 * All ports clients of this peer have opened.
640 static struct GNUNET_CONTAINER_MultiHashMap *ports;
643 * Task to periodically announce itself in the network.
645 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
648 * Next ID to assign to a client.
650 unsigned int next_client_id;
653 /******************************************************************************/
654 /*********************** DECLARATIONS **************************/
655 /******************************************************************************/
658 * Function to process paths received for a new peer addition. The recorded
659 * paths form the initial tunnel, which can be optimized later.
660 * Called on each result obtained for the DHT search.
663 * @param exp when will this value expire
664 * @param key key of the result
665 * @param type type of the result
666 * @param size number of bytes in data
667 * @param data pointer to the result data
670 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
671 const struct GNUNET_HashCode * key,
672 const struct GNUNET_PeerIdentity *get_path,
673 unsigned int get_path_length,
674 const struct GNUNET_PeerIdentity *put_path,
675 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
676 size_t size, const void *data);
680 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
681 * and insert it in the appropriate structures if the peer is not known yet.
683 * @param peer Full identity of the peer.
685 * @return Existing or newly created peer info.
687 static struct MeshPeerInfo *
688 peer_get (const struct GNUNET_PeerIdentity *peer);
692 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
693 * and insert it in the appropriate structures if the peer is not known yet.
695 * @param peer Short identity of the peer.
697 * @return Existing or newly created peer info.
699 static struct MeshPeerInfo *
700 peer_get_short (const GNUNET_PEER_Id peer);
704 * Build a PeerPath from the paths returned from the DHT, reversing the paths
705 * to obtain a local peer -> destination path and interning the peer ids.
707 * @return Newly allocated and created path
709 static struct MeshPeerPath *
710 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
711 unsigned int get_path_length,
712 const struct GNUNET_PeerIdentity *put_path,
713 unsigned int put_path_length);
717 * Adds a path to the peer_infos of all the peers in the path
719 * @param p Path to process.
720 * @param confirmed Whether we know if the path works or not.
723 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
728 * Search for a tunnel by global ID using full PeerIdentities.
730 * @param oid owner of the tunnel.
731 * @param tid global tunnel number.
733 * @return tunnel handler, NULL if doesn't exist.
735 static struct MeshTunnel *
736 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
740 * Notify a tunnel that a connection has broken that affects at least
743 * @param t Tunnel affected.
744 * @param p1 Peer that got disconnected from p2.
745 * @param p2 Peer that got disconnected from p1.
747 * @return Short ID of the peer disconnected (either p1 or p2).
748 * 0 if the tunnel remained unaffected.
750 static GNUNET_PEER_Id
751 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
756 * @brief Use the given path for the tunnel.
757 * Update the next and prev hops (and RCs).
758 * (Re)start the path refresh in case the tunnel is locally owned.
760 * @param t Tunnel to update.
761 * @param p Path to use.
764 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
767 * Tunnel is empty: destroy it.
769 * Notifies all participants (peers, cleints) about the destruction.
771 * @param t Tunnel to destroy.
774 tunnel_destroy_empty (struct MeshTunnel *t);
777 * @brief Queue and pass message to core when possible.
779 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
780 * and accounts for it. In case the queue is full, the message is dropped and
783 * Otherwise, message is treated as internal and allowed to go regardless of
786 * @param cls Closure (@c type dependant). It will be used by queue_send to
787 * build the message to be sent if not already prebuilt.
788 * @param type Type of the message, 0 for a raw message.
789 * @param size Size of the message.
790 * @param dst Neighbor to send message to.
791 * @param t Tunnel this message belongs to.
794 queue_add (void *cls, uint16_t type, size_t size,
795 struct MeshPeerInfo *dst, struct MeshTunnel *t);
799 * Free a transmission that was already queued with all resources
800 * associated to the request.
802 * @param queue Queue handler to cancel.
803 * @param clear_cls Is it necessary to free associated cls?
806 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
810 * @brief Get the next transmittable message from the queue.
812 * This will be the head, except in the case of being a data packet
813 * not allowed by the destination peer.
815 * @param peer Destination peer.
817 * @return The next viable MeshPeerQueue element to send to that peer.
818 * NULL when there are no transmittable messages.
820 struct MeshPeerQueue *
821 queue_get_next (const struct MeshPeerInfo *peer);
825 * Core callback to write a queued packet to core buffer
827 * @param cls Closure (peer info).
828 * @param size Number of bytes available in buf.
829 * @param buf Where the to write the message.
831 * @return number of bytes written to buf
834 queue_send (void *cls, size_t size, void *buf);
837 /******************************************************************************/
838 /************************ PERIODIC FUNCTIONS ****************************/
839 /******************************************************************************/
842 * Periodically announce self id in the DHT
845 * @param tc task context
848 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
852 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
854 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
858 * - Set data expiration in function of X
861 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
863 block.id = my_full_id;
864 GNUNET_DHT_put (dht_handle, /* DHT handle */
865 &my_full_id.hashPubKey, /* Key to use */
866 dht_replication_level, /* Replication level */
867 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
868 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
869 sizeof (block), /* Size of the data */
870 (const char *) &block, /* Data itself */
871 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
872 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
873 NULL, /* Continuation */
874 NULL); /* Continuation closure */
876 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
880 /******************************************************************************/
881 /****************** GENERAL HELPER FUNCTIONS ************************/
882 /******************************************************************************/
886 * Check if client has registered with the service and has not disconnected
888 * @param client the client to check
890 * @return non-NULL if client exists in the global DLL
892 static struct MeshClient *
893 client_get (struct GNUNET_SERVER_Client *client)
895 struct MeshClient *c;
900 if (c->handle == client)
909 * Deletes a tunnel from a client (either owner or destination). To be used on
912 * @param c Client whose tunnel to delete.
913 * @param t Tunnel which should be deleted.
916 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
918 struct GNUNET_HashCode hash;
922 GMC_hash32 (t->local_tid, &hash);
923 GNUNET_assert (GNUNET_YES ==
924 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
928 else if (c == t->client)
930 GMC_hash32 (t->local_tid_dest, &hash);
931 GNUNET_assert (GNUNET_YES ==
932 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
943 * Notify the appropriate client that a new incoming tunnel was created.
945 * @param t Tunnel that was created.
948 send_client_tunnel_create (struct MeshTunnel *t)
950 struct GNUNET_MESH_TunnelMessage msg;
952 if (NULL == t->client)
954 msg.header.size = htons (sizeof (msg));
955 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
956 msg.tunnel_id = htonl (t->local_tid_dest);
957 msg.port = htonl (t->port);
958 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
959 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
960 &msg.header, GNUNET_NO);
965 * Notify dest client that the incoming tunnel is no longer valid.
967 * @param c Client to notify..
968 * @param t Tunnel that is destroyed.
971 send_client_tunnel_destroy (struct MeshClient *c, struct MeshTunnel *t)
973 struct GNUNET_MESH_TunnelMessage msg;
980 if (c != t->client && c != t->owner)
985 msg.header.size = htons (sizeof (msg));
986 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
987 msg.tunnel_id = htonl (t->local_tid_dest);
988 msg.port = htonl (0);
989 memset(&msg.peer, 0, sizeof (msg.peer));
990 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
991 &msg.header, GNUNET_NO);
996 * Iterator over all the peers to remove the oldest not-used entry.
998 * @param cls Closure (unsued).
999 * @param key ID of the peer.
1000 * @param value Peer_Info of the peer.
1005 peer_info_timeout (void *cls,
1006 const struct GNUNET_HashCode *key,
1013 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1014 * and insert it in the appropriate structures if the peer is not known yet.
1016 * @param peer Full identity of the peer.
1018 * @return Existing or newly created peer info.
1020 static struct MeshPeerInfo *
1021 peer_get (const struct GNUNET_PeerIdentity *peer)
1023 struct MeshPeerInfo *peer_info;
1025 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1026 if (NULL == peer_info)
1029 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1030 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1032 GNUNET_CONTAINER_multihashmap_iterate (peers,
1036 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1037 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1038 peer_info->id = GNUNET_PEER_intern (peer);
1040 peer_info->last_contact = GNUNET_TIME_absolute_get();
1047 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1048 * and insert it in the appropriate structures if the peer is not known yet.
1050 * @param peer Short identity of the peer.
1052 * @return Existing or newly created peer info.
1054 static struct MeshPeerInfo *
1055 peer_get_short (const GNUNET_PEER_Id peer)
1057 struct GNUNET_PeerIdentity id;
1059 GNUNET_PEER_resolve (peer, &id);
1060 return peer_get (&id);
1065 * Choose the best path towards a peer considering the tunnel properties.
1067 * @param peer The destination peer.
1068 * @param t The tunnel the path is for.
1070 * @return Best current known path towards the peer, if any.
1072 static struct MeshPeerPath *
1073 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1075 struct MeshPeerPath *best_p;
1076 struct MeshPeerPath *p;
1077 unsigned int best_cost;
1080 best_p = p = peer->path_head;
1081 best_cost = cost = p->length;
1084 if ((cost = p->length) < best_cost)
1095 * Core callback to write a pre-constructed data packet to core buffer
1097 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1098 * @param size Number of bytes available in buf.
1099 * @param buf Where the to write the message.
1101 * @return number of bytes written to buf
1104 send_core_data_raw (void *cls, size_t size, void *buf)
1106 struct GNUNET_MessageHeader *msg = cls;
1109 GNUNET_assert (NULL != msg);
1110 total_size = ntohs (msg->size);
1112 if (total_size > size)
1117 memcpy (buf, msg, total_size);
1124 * Sends an already built message to a peer, properly registrating
1125 * all used resources.
1127 * @param message Message to send. Function makes a copy of it.
1128 * @param peer Short ID of the neighbor whom to send the message.
1129 * @param t Tunnel on which this message is transmitted.
1132 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1133 GNUNET_PEER_Id peer,
1134 struct MeshTunnel *t)
1136 struct GNUNET_PeerIdentity id;
1137 struct MeshPeerInfo *neighbor;
1138 struct MeshPeerPath *p;
1143 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1148 size = ntohs (message->size);
1149 data = GNUNET_malloc (size);
1150 memcpy (data, message, size);
1151 type = ntohs(message->type);
1152 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1153 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1155 struct GNUNET_MESH_Data *u;
1157 u = (struct GNUNET_MESH_Data *) data;
1158 u->ttl = htonl (ntohl (u->ttl) - 1);
1160 GNUNET_PEER_resolve (peer, &id);
1161 neighbor = peer_get (&id);
1162 for (p = neighbor->path_head; NULL != p; p = p->next)
1172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1173 " %s IS NOT DIRECTLY CONNECTED\n",
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1178 for (p = neighbor->path_head; NULL != p; p = p->next)
1180 struct GNUNET_PeerIdentity debug_id;
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 " path with %u hops through:\n",
1186 for (i = 0; i < p->length; i++)
1188 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1191 i, GNUNET_i2s(&debug_id));
1195 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1197 " no direct connection to %s\n",
1202 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1213 * Sends a CREATE PATH message for a path to a peer, properly registrating
1214 * all used resources.
1216 * @param t Tunnel for which the path is created.
1219 send_create_path (struct MeshTunnel *t)
1221 struct MeshPeerInfo *neighbor;
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1224 neighbor = peer_get_short (t->next_hop);
1226 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1227 sizeof (struct GNUNET_MESH_CreateTunnel) +
1228 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1231 t->state = MESH_TUNNEL_WAITING;
1236 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1238 * @param t Tunnel which to confirm.
1241 send_path_ack (struct MeshTunnel *t)
1243 struct MeshPeerInfo *peer;
1245 peer = peer_get_short (t->prev_hop);
1248 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1249 sizeof (struct GNUNET_MESH_PathACK),
1256 * Try to establish a new connection to this peer in the given tunnel.
1257 * If the peer doesn't have any path to it yet, try to get one.
1258 * If the peer already has some path, send a CREATE PATH towards it.
1260 * @param peer PeerInfo of the peer.
1261 * @param t Tunnel for which to create the path, if possible.
1264 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1266 struct MeshPeerPath *p;
1268 if (NULL != peer->path_head)
1270 p = peer_get_best_path (peer, t);
1271 tunnel_use_path (t, p);
1272 send_create_path (t);
1274 else if (NULL == peer->dhtget)
1276 struct GNUNET_PeerIdentity id;
1278 GNUNET_PEER_resolve (peer->id, &id);
1279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1280 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1281 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1282 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1283 &id.hashPubKey, /* key to search */
1284 dht_replication_level, /* replication level */
1285 GNUNET_DHT_RO_RECORD_ROUTE |
1286 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1288 0, /* xquery bits */
1289 &dht_get_id_handler, peer);
1290 t->state = MESH_TUNNEL_SEARCHING;
1294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1295 "There is no path but the DHT GET is already started.\n");
1301 * Destroy the peer_info and free any allocated resources linked to it
1303 * @param pi The peer_info to destroy.
1305 * @return GNUNET_OK on success
1308 peer_info_destroy (struct MeshPeerInfo *pi)
1310 struct GNUNET_PeerIdentity id;
1311 struct MeshPeerPath *p;
1312 struct MeshPeerPath *nextp;
1315 GNUNET_PEER_resolve (pi->id, &id);
1316 GNUNET_PEER_change_rc (pi->id, -1);
1319 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1322 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1323 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1325 if (NULL != pi->dhtget)
1327 GNUNET_DHT_get_stop (pi->dhtget);
1333 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1337 for (i = 0; i < pi->ntunnels; i++)
1338 tunnel_destroy_empty (pi->tunnels[i]);
1339 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1346 * Remove all paths that rely on a direct connection between p1 and p2
1347 * from the peer itself and notify all tunnels about it.
1349 * @param peer PeerInfo of affected peer.
1350 * @param p1 GNUNET_PEER_Id of one peer.
1351 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1354 * TODO: optimize (see below)
1357 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1360 struct MeshPeerPath *p;
1361 struct MeshPeerPath *next;
1362 struct MeshPeerInfo *peer_d;
1364 unsigned int destroyed;
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1369 for (p = peer->path_head; NULL != p; p = next)
1372 for (i = 0; i < (p->length - 1); i++)
1374 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1375 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1377 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1387 for (i = 0; i < peer->ntunnels; i++)
1389 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1393 peer_d = peer_get_short (d);
1394 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1395 tunnel_use_path (peer->tunnels[i], next);
1396 peer_connect (peer_d, peer->tunnels[i]);
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1403 * Add the path to the peer and update the path used to reach it in case this
1406 * @param peer_info Destination peer to add the path to.
1407 * @param path New path to add. Last peer must be the peer in arg 1.
1408 * Path will be either used of freed if already known.
1409 * @param trusted Do we trust that this path is real?
1412 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1415 struct MeshPeerPath *aux;
1419 if ((NULL == peer_info) || (NULL == path))
1422 path_destroy (path);
1425 if (path->peers[path->length - 1] != peer_info->id)
1428 path_destroy (path);
1431 if (2 >= path->length && GNUNET_NO == trusted)
1433 /* Only allow CORE to tell us about direct paths */
1434 path_destroy (path);
1437 for (l = 1; l < path->length; l++)
1439 if (path->peers[l] == myid)
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1442 for (l2 = 0; l2 < path->length - l; l2++)
1444 path->peers[l2] = path->peers[l + l2];
1449 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1454 struct GNUNET_PeerIdentity id;
1456 GNUNET_PEER_resolve (peer_info->id, &id);
1457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1458 path->length, GNUNET_i2s (&id));
1461 l = path_get_length (path);
1464 path_destroy (path);
1468 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1469 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1471 l2 = path_get_length (aux);
1474 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1475 peer_info->path_tail, aux, path);
1480 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1482 path_destroy (path);
1487 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1494 * Add the path to the origin peer and update the path used to reach it in case
1495 * this is the shortest.
1496 * The path is given in peer_info -> destination, therefore we turn the path
1497 * upside down first.
1499 * @param peer_info Peer to add the path to, being the origin of the path.
1500 * @param path New path to add after being inversed.
1501 * Path will be either used or freed.
1502 * @param trusted Do we trust that this path is real?
1505 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1506 struct MeshPeerPath *path, int trusted)
1509 peer_info_add_path (peer_info, path, trusted);
1514 * Add a tunnel to the list of tunnels a peer participates in.
1515 * Update the tunnel's destination.
1517 * @param p Peer to add to.
1518 * @param t Tunnel to add.
1521 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1525 GNUNET_break (t->dest == p->id);
1529 GNUNET_PEER_change_rc (t->dest, 1);
1530 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1535 * Remove a tunnel from the list of tunnels a peer participates in.
1536 * Free the tunnel's destination.
1538 * @param p Peer to clean.
1539 * @param t Tunnel to remove.
1542 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1546 if (t->dest == p->id)
1548 GNUNET_PEER_change_rc (t->dest, -1);
1551 for (i = 0; i < p->ntunnels; i++)
1553 if (p->tunnels[i] == t)
1555 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1556 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1564 * Function called if the connection to the peer has been stalled for a while,
1565 * possibly due to a missed ACK. Poll the peer about its ACK status.
1567 * @param cls Closure (poll ctx).
1568 * @param tc TaskContext.
1571 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1573 struct MeshFlowControl *fc = cls;
1574 struct GNUNET_MESH_Poll msg;
1575 struct MeshTunnel *t = fc->t;
1576 GNUNET_PEER_Id peer;
1578 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1579 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1584 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1585 msg.header.size = htons (sizeof (msg));
1586 msg.tid = htonl (t->id.tid);
1587 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1589 if (fc == &t->prev_fc)
1593 else if (fc == &t->next_fc)
1602 send_prebuilt_message (&msg.header, peer, t);
1603 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1604 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1610 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1611 * to obtain a local peer -> destination path and interning the peer ids.
1613 * @return Newly allocated and created path
1615 static struct MeshPeerPath *
1616 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1617 unsigned int get_path_length,
1618 const struct GNUNET_PeerIdentity *put_path,
1619 unsigned int put_path_length)
1621 struct MeshPeerPath *p;
1627 GNUNET_PEER_change_rc (myid, 1);
1628 i = get_path_length;
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1630 for (i--; i >= 0; i--)
1632 id = GNUNET_PEER_intern (&get_path[i]);
1633 if (p->length > 0 && id == p->peers[p->length - 1])
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1636 GNUNET_PEER_change_rc (id, -1);
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1641 GNUNET_i2s (&get_path[i]));
1643 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1644 p->peers[p->length - 1] = id;
1647 i = put_path_length;
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1649 for (i--; i >= 0; i--)
1651 id = GNUNET_PEER_intern (&put_path[i]);
1654 /* PUT path went through us, so discard the path up until now and start
1655 * from here to get a much shorter (and loop-free) path.
1660 if (p->length > 0 && id == p->peers[p->length - 1])
1662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1663 GNUNET_PEER_change_rc (id, -1);
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1668 GNUNET_i2s (&put_path[i]));
1670 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1671 p->peers[p->length - 1] = id;
1675 if (get_path_length > 0)
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1677 GNUNET_i2s (&get_path[0]));
1678 if (put_path_length > 0)
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1680 GNUNET_i2s (&put_path[0]));
1681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1683 for (i = 0; i < p->length; i++)
1685 struct GNUNET_PeerIdentity peer_id;
1687 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1689 GNUNET_i2s (&peer_id));
1697 * Adds a path to the peer_infos of all the peers in the path
1699 * @param p Path to process.
1700 * @param confirmed Whether we know if the path works or not.
1703 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1707 /* TODO: invert and add */
1708 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1709 for (i++; i < p->length; i++)
1711 struct MeshPeerInfo *aux;
1712 struct MeshPeerPath *copy;
1714 aux = peer_get_short (p->peers[i]);
1715 copy = path_duplicate (p);
1716 copy->length = i + 1;
1717 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1723 * Send keepalive packets for a peer
1725 * @param cls Closure (tunnel for which to send the keepalive).
1726 * @param tc Notification context.
1729 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1733 * Search for a tunnel among the incoming tunnels
1735 * @param tid the local id of the tunnel
1737 * @return tunnel handler, NULL if doesn't exist
1739 static struct MeshTunnel *
1740 tunnel_get_incoming (MESH_TunnelNumber tid)
1742 struct GNUNET_HashCode hash;
1744 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1745 GMC_hash32 (tid, &hash);
1746 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1751 * Search for a tunnel among the tunnels for a client
1753 * @param c the client whose tunnels to search in
1754 * @param tid the local id of the tunnel
1756 * @return tunnel handler, NULL if doesn't exist
1758 static struct MeshTunnel *
1759 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1761 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1763 return tunnel_get_incoming (tid);
1767 struct GNUNET_HashCode hash;
1769 GMC_hash32 (tid, &hash);
1770 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1776 * Search for a tunnel by global ID using PEER_ID
1778 * @param pi owner of the tunnel
1779 * @param tid global tunnel number
1781 * @return tunnel handler, NULL if doesn't exist
1783 static struct MeshTunnel *
1784 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1786 struct MESH_TunnelID id;
1787 struct GNUNET_HashCode hash;
1792 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1793 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1798 * Search for a tunnel by global ID using full PeerIdentities
1800 * @param oid owner of the tunnel
1801 * @param tid global tunnel number
1803 * @return tunnel handler, NULL if doesn't exist
1805 static struct MeshTunnel *
1806 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1808 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1813 * Add a client to a tunnel, initializing all needed data structures.
1815 * @param t Tunnel to which add the client.
1816 * @param c Client which to add to the tunnel.
1819 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1821 struct GNUNET_HashCode hash;
1823 if (NULL != t->client)
1828 GMC_hash32 (t->local_tid_dest, &hash);
1830 GNUNET_CONTAINER_multihashmap_put (c->incoming_tunnels, &hash, t,
1831 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1837 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1838 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1848 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1850 unsigned int own_pos;
1852 for (own_pos = 0; own_pos < p->length; own_pos++)
1854 if (p->peers[own_pos] == myid)
1857 if (own_pos > p->length - 1)
1863 if (own_pos < p->length - 1)
1864 t->next_hop = p->peers[own_pos + 1];
1866 t->next_hop = p->peers[own_pos];
1867 GNUNET_PEER_change_rc (t->next_hop, 1);
1869 t->prev_hop = p->peers[own_pos - 1];
1871 t->prev_hop = p->peers[0];
1872 GNUNET_PEER_change_rc (t->prev_hop, 1);
1874 if (NULL != t->path)
1875 path_destroy (t->path);
1876 t->path = path_duplicate (p);
1879 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1880 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1881 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
1888 * Notifies a tunnel that a connection has broken that affects at least
1889 * some of its peers. Sends a notification towards the root of the tree.
1890 * In case the peer is the owner of the tree, notifies the client that owns
1891 * the tunnel and tries to reconnect.
1893 * @param t Tunnel affected.
1894 * @param p1 Peer that got disconnected from p2.
1895 * @param p2 Peer that got disconnected from p1.
1897 * @return Short ID of the peer disconnected (either p1 or p2).
1898 * 0 if the tunnel remained unaffected.
1900 static GNUNET_PEER_Id
1901 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1904 // if (myid != p1 && myid != p2) FIXME
1909 // if (tree_get_predecessor (t->tree) != 0)
1911 // /* We are the peer still connected, notify owner of the disconnection. */
1912 // struct GNUNET_MESH_PathBroken msg;
1913 // struct GNUNET_PeerIdentity neighbor;
1915 // msg.header.size = htons (sizeof (msg));
1916 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1917 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1918 // msg.tid = htonl (t->id.tid);
1919 // msg.peer1 = my_full_id;
1920 // GNUNET_PEER_resolve (pid, &msg.peer2);
1921 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1922 // send_prebuilt_message (&msg.header, &neighbor, t);
1929 * Build a local ACK message and send it to a local client.
1931 * @param t Tunnel on which to send the ACK.
1932 * @param c Client to whom send the ACK.
1933 * @param ack Value of the ACK.
1936 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
1938 struct GNUNET_MESH_LocalAck msg;
1940 msg.header.size = htons (sizeof (msg));
1941 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1942 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
1943 msg.ack = htonl (ack);
1944 GNUNET_SERVER_notification_context_unicast(nc,
1951 * Build an ACK message and queue it to send to the given peer.
1953 * @param t Tunnel on which to send the ACK.
1954 * @param peer Peer to whom send the ACK.
1955 * @param ack Value of the ACK.
1958 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1960 struct GNUNET_MESH_ACK msg;
1962 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1963 msg.header.size = htons (sizeof (msg));
1964 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1965 msg.pid = htonl (ack);
1966 msg.tid = htonl (t->id.tid);
1968 send_prebuilt_message (&msg.header, peer, t);
1973 * Send an ACK informing the predecessor about the available buffer space.
1974 * In case there is no predecessor, inform the owning client.
1975 * If buffering is off, send only on behalf of children or self if endpoint.
1976 * If buffering is on, send when sent to children and buffer space is free.
1977 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
1978 * the ACK itself goes "back" (towards root).
1980 * @param t Tunnel on which to send the ACK.
1981 * @param type Type of message that triggered the ACK transmission.
1984 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
1988 /* Is it after unicast retransmission? */
1991 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1993 "ACK due to FWD DATA retransmission\n");
1994 if (GNUNET_YES == t->nobuffer)
1996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2000 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2001 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2003 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2004 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2005 t->force_ack = GNUNET_YES;
2011 /* Check if we need to transmit the ACK */
2012 if (t->queue_max > t->next_fc.queue_n * 4 &&
2013 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
2014 GNUNET_NO == t->force_ack)
2016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2018 " t->qmax: %u, t->qn: %u\n",
2019 t->queue_max, t->next_fc.queue_n);
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2021 " t->pid: %u, t->ack: %u\n",
2022 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
2026 /* Ok, ACK might be necessary, what PID to ACK? */
2027 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
2028 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
2030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2034 t->prev_fc.last_ack_sent = ack;
2035 if (NULL != t->owner)
2036 send_local_ack (t, t->owner, ack);
2037 else if (0 != t->prev_hop)
2038 send_ack (t, t->prev_hop, ack);
2042 t->force_ack = GNUNET_NO;
2047 * Send an ACK informing the children node/client about the available
2049 * If buffering is off, send only on behalf of root (can be self).
2050 * If buffering is on, send when sent to predecessor and buffer space is free.
2051 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2052 * the ACK itself goes "forward" (towards children/clients).
2054 * @param t Tunnel on which to send the ACK.
2055 * @param type Type of message that triggered the ACK transmission.
2058 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2062 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2063 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2064 /* Is it after data to_origin retransmission? */
2067 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2068 if (GNUNET_YES == t->nobuffer)
2070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2071 " Not sending ACK, nobuffer + traffic\n");
2075 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2076 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2078 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2079 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2080 t->force_ack = GNUNET_YES;
2086 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2088 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2090 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2093 " Not sending ACK, not needed, last ack sent was %u\n",
2094 t->next_fc.last_ack_sent);
2097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2098 " Sending BCK ACK %u (last sent: %u)\n",
2099 ack, t->next_fc.last_ack_sent);
2100 t->next_fc.last_ack_sent = ack;
2102 if (NULL != t->client)
2103 send_local_ack (t, t->client, ack);
2104 else if (0 != t->next_hop)
2105 send_ack (t, t->next_hop, ack);
2108 t->force_ack = GNUNET_NO;
2113 * Modify the unicast message TID from global to local and send to client.
2115 * @param t Tunnel on which to send the message.
2116 * @param msg Message to modify and send.
2119 tunnel_send_client_ucast (struct MeshTunnel *t,
2120 const struct GNUNET_MESH_Data *msg)
2122 struct GNUNET_MESH_Data *copy;
2123 uint16_t size = ntohs (msg->header.size);
2126 if (size < sizeof (struct GNUNET_MESH_Data) +
2127 sizeof (struct GNUNET_MessageHeader))
2129 GNUNET_break_op (0);
2132 if (NULL == t->client)
2137 copy = (struct GNUNET_MESH_Data *) cbuf;
2138 memcpy (copy, msg, size);
2139 copy->tid = htonl (t->local_tid_dest);
2140 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
2141 ©->header, GNUNET_NO);
2146 * Modify the to_origin message TID from global to local and send to client.
2148 * @param t Tunnel on which to send the message.
2149 * @param msg Message to modify and send.
2152 tunnel_send_client_to_orig (struct MeshTunnel *t,
2153 const struct GNUNET_MESH_Data *msg)
2155 struct GNUNET_MESH_Data *copy;
2156 uint16_t size = ntohs (msg->header.size);
2159 if (size < sizeof (struct GNUNET_MESH_Data) +
2160 sizeof (struct GNUNET_MessageHeader))
2162 GNUNET_break_op (0);
2165 if (NULL == t->owner)
2170 copy = (struct GNUNET_MESH_Data *) cbuf;
2171 memcpy (cbuf, msg, size);
2172 copy->tid = htonl (t->local_tid);
2173 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2174 ©->header, GNUNET_NO);
2179 * @brief Re-initiate traffic to this peer if necessary.
2181 * Check if there is traffic queued towards this peer
2182 * and the core transmit handle is NULL (traffic was stalled).
2183 * If so, call core tmt rdy.
2185 * @param peer_id Short ID of peer to which initiate traffic.
2188 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2190 struct MeshPeerInfo *peer;
2191 struct GNUNET_PeerIdentity id;
2192 struct MeshPeerQueue *q;
2195 peer = peer_get_short (peer_id);
2196 if (NULL != peer->core_transmit)
2199 q = queue_get_next (peer);
2202 /* Might br multicast traffic already sent to this particular peer but
2203 * not to other children in this tunnel.
2204 * This way t->queue_n would be > 0 but the queue of this particular peer
2210 GNUNET_PEER_resolve (peer->id, &id);
2211 peer->core_transmit =
2212 GNUNET_CORE_notify_transmit_ready(core_handle,
2215 GNUNET_TIME_UNIT_FOREVER_REL,
2225 * Send a message to all peers and clients in this tunnel that the tunnel
2226 * is no longer valid. If some peer or client should not receive the message,
2227 * should be zero'ed out before calling this function.
2229 * @param t The tunnel whose peers and clients to notify.
2232 tunnel_send_destroy (struct MeshTunnel *t)
2234 struct GNUNET_MESH_TunnelDestroy msg;
2235 struct GNUNET_PeerIdentity id;
2237 msg.header.size = htons (sizeof (msg));
2238 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2239 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2240 msg.tid = htonl (t->id.tid);
2241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2242 " sending tunnel destroy for tunnel: %s [%X]\n",
2243 GNUNET_i2s (&msg.oid), t->id.tid);
2245 if (NULL == t->client && 0 != t->next_hop)
2247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2248 GNUNET_PEER_resolve (t->next_hop, &id);
2249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2250 " sending forward to %s\n",
2252 send_prebuilt_message (&msg.header, t->next_hop, t);
2254 if (NULL == t->owner && 0 != t->prev_hop)
2256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2257 GNUNET_PEER_resolve (t->prev_hop, &id);
2258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2259 " sending back to %s\n",
2261 send_prebuilt_message (&msg.header, t->prev_hop, t);
2263 if (NULL != t->owner)
2265 send_client_tunnel_destroy (t->owner, t);
2267 if (NULL != t->client)
2269 send_client_tunnel_destroy (t->client, t);
2275 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2277 * @param t Tunnel which to cancel.
2278 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2281 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2283 struct MeshPeerInfo *peer_info;
2284 struct MeshPeerQueue *pq;
2285 struct MeshPeerQueue *next;
2288 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2289 peer_info = peer_get_short (neighbor);
2290 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2293 if (pq->tunnel == t)
2295 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2296 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2298 /* Should have been removed on destroy children */
2301 queue_destroy (pq, GNUNET_YES);
2304 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2306 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2307 peer_info->core_transmit = NULL;
2313 * Destroy the tunnel.
2315 * This function does not generate any warning traffic to clients or peers.
2318 * Remove the tunnel from peer_info's and clients' hashmaps.
2319 * Cancel messages belonging to this tunnel queued to neighbors.
2320 * Free any allocated resources linked to the tunnel.
2322 * @param t the tunnel to destroy
2324 * @return GNUNET_OK on success
2327 tunnel_destroy (struct MeshTunnel *t)
2329 struct MeshClient *c;
2330 struct GNUNET_HashCode hash;
2340 struct GNUNET_PeerIdentity id;
2342 GNUNET_PEER_resolve (t->id.oid, &id);
2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2344 GNUNET_i2s (&id), t->id.tid);
2346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2350 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2351 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2359 GMC_hash32 (t->local_tid, &hash);
2361 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2368 if (NULL != t->client)
2371 GMC_hash32 (t->local_tid_dest, &hash);
2373 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2379 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2386 if (0 != t->prev_hop)
2388 peer_cancel_queues (t->prev_hop, t);
2389 GNUNET_PEER_change_rc (t->prev_hop, -1);
2391 if (0 != t->next_hop)
2393 peer_cancel_queues (t->next_hop, t);
2394 GNUNET_PEER_change_rc (t->next_hop, -1);
2397 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2400 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2401 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2404 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2405 path_destroy (t->path);
2411 * Tunnel is empty: destroy it.
2413 * Notifies all participants (peers, cleints) about the destruction.
2415 * @param t Tunnel to destroy.
2418 tunnel_destroy_empty (struct MeshTunnel *t)
2422 struct GNUNET_PeerIdentity id;
2424 GNUNET_PEER_resolve (t->id.oid, &id);
2425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2426 "executing destruction of empty tunnel %s [%X]\n",
2427 GNUNET_i2s (&id), t->id.tid);
2431 if (GNUNET_NO == t->destroy)
2432 tunnel_send_destroy (t);
2433 if (0 == t->pending_messages)
2436 t->destroy = GNUNET_YES;
2440 * Initialize a Flow Control structure to the initial state.
2442 * @param fc Flow Control structure to initialize.
2445 fc_init (struct MeshFlowControl *fc)
2447 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2448 fc->last_pid_recv = (uint32_t) -1;
2449 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2450 fc->last_ack_recv = (uint32_t) -1;
2451 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2452 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2457 * Create a new tunnel
2459 * @param owner Who is the owner of the tunnel (short ID).
2460 * @param tid Tunnel Number of the tunnel.
2461 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2462 * @param local Tunnel Number for the tunnel, for the client point of view.
2464 * @return A new initialized tunnel. NULL on error.
2466 static struct MeshTunnel *
2467 tunnel_new (GNUNET_PEER_Id owner,
2468 MESH_TunnelNumber tid,
2469 struct MeshClient *client,
2470 MESH_TunnelNumber local)
2472 struct MeshTunnel *t;
2473 struct GNUNET_HashCode hash;
2475 if (n_tunnels >= max_tunnels && NULL == client)
2478 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2481 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2483 fc_init (&t->next_fc);
2484 fc_init (&t->prev_fc);
2485 t->local_tid = local;
2487 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2489 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2491 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2492 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2499 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2506 GMC_hash32 (t->local_tid, &hash);
2508 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2509 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2513 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2524 * Iterator for deleting each tunnel whose client endpoint disconnected.
2526 * @param cls Closure (client that has disconnected).
2527 * @param key The hash of the local tunnel id (used to access the hashmap).
2528 * @param value The value stored at the key (tunnel to destroy).
2530 * @return GNUNET_OK, keep iterating.
2533 tunnel_destroy_iterator (void *cls,
2534 const struct GNUNET_HashCode * key,
2537 struct MeshTunnel *t = value;
2538 struct MeshClient *c = cls;
2540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2541 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2542 t->local_tid, t->local_tid_dest, c->id);
2543 client_delete_tunnel (c, t);
2546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2548 if (0 != t->next_hop) { /* destroy could come before a path is used */
2549 GNUNET_PEER_change_rc (t->next_hop, -1);
2553 else if (c == t->owner)
2555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2557 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2558 GNUNET_PEER_change_rc (t->prev_hop, -1);
2566 tunnel_destroy_empty (t);
2573 * Timeout function, destroys tunnel if called
2575 * @param cls Closure (tunnel to destroy).
2576 * @param tc TaskContext
2579 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2581 struct MeshTunnel *t = cls;
2582 struct GNUNET_PeerIdentity id;
2584 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2585 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2587 GNUNET_PEER_resolve(t->id.oid, &id);
2588 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2589 "Tunnel %s [%X] timed out. Destroying.\n",
2590 GNUNET_i2s(&id), t->id.tid);
2591 if (NULL != t->client)
2592 send_client_tunnel_destroy (t->client, t);
2593 tunnel_destroy (t); /* Do not notify other */
2598 * Resets the tunnel timeout. Starts it if no timeout was running.
2600 * @param t Tunnel whose timeout to reset.
2602 * TODO use heap to improve efficiency of scheduler.
2605 tunnel_reset_timeout (struct MeshTunnel *t)
2607 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2609 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2610 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2611 t->maintenance_task =
2612 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2613 (refresh_path_time, 4), &tunnel_timeout, t);
2617 /******************************************************************************/
2618 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2619 /******************************************************************************/
2622 * Function to send a create path packet to a peer.
2624 * @param cls closure
2625 * @param size number of bytes available in buf
2626 * @param buf where the callee should write the message
2627 * @return number of bytes written to buf
2630 send_core_path_create (void *cls, size_t size, void *buf)
2632 struct MeshTunnel *t = cls;
2633 struct GNUNET_MESH_CreateTunnel *msg;
2634 struct GNUNET_PeerIdentity *peer_ptr;
2635 struct MeshPeerPath *p = t->path;
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2642 sizeof (struct GNUNET_MESH_CreateTunnel) +
2643 p->length * sizeof (struct GNUNET_PeerIdentity);
2645 if (size < size_needed || NULL == buf)
2650 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2651 msg->header.size = htons (size_needed);
2652 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2653 msg->tid = ntohl (t->id.tid);
2656 if (GNUNET_YES == t->nobuffer)
2657 opt |= MESH_TUNNEL_OPT_NOBUFFER;
2658 msg->opt = htonl (opt);
2659 msg->port = htonl (t->port);
2661 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2662 for (i = 0; i < p->length; i++)
2664 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2668 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2674 * Creates a path ack message in buf and frees all unused resources.
2676 * @param cls closure (MeshTransmissionDescriptor)
2677 * @param size number of bytes available in buf
2678 * @param buf where the callee should write the message
2679 * @return number of bytes written to buf
2682 send_core_path_ack (void *cls, size_t size, void *buf)
2684 struct MeshTunnel *t = cls;
2685 struct GNUNET_MESH_PathACK *msg = buf;
2687 GNUNET_assert (NULL != t);
2688 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2693 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
2694 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2695 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2696 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
2697 msg->tid = htonl (t->id.tid);
2698 msg->peer_id = my_full_id;
2699 msg->ack = htonl (t->prev_fc.last_ack_sent);
2701 /* TODO add signature */
2703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2704 return sizeof (struct GNUNET_MESH_PathACK);
2709 * Free a transmission that was already queued with all resources
2710 * associated to the request.
2712 * @param queue Queue handler to cancel.
2713 * @param clear_cls Is it necessary to free associated cls?
2716 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2718 struct MeshFlowControl *fc;
2720 if (GNUNET_YES == clear_cls)
2722 switch (queue->type)
2724 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2725 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2726 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2728 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2729 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2730 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2731 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2732 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2734 " prebuilt message\n");
2735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2737 GNUNET_MESH_DEBUG_M2S (queue->type));
2739 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2744 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2745 " type %s unknown!\n",
2746 GNUNET_MESH_DEBUG_M2S (queue->type));
2748 GNUNET_free_non_null (queue->cls);
2750 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2751 queue->peer->queue_tail,
2754 /* Delete from appropriate fc in the tunnel */
2755 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
2756 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
2758 if (queue->peer->id == queue->tunnel->prev_hop)
2759 fc = &queue->tunnel->prev_fc;
2760 else if (queue->peer->id == queue->tunnel->next_hop)
2761 fc = &queue->tunnel->next_fc;
2769 GNUNET_free (queue);
2774 * @brief Get the next transmittable message from the queue.
2776 * This will be the head, except in the case of being a data packet
2777 * not allowed by the destination peer.
2779 * @param peer Destination peer.
2781 * @return The next viable MeshPeerQueue element to send to that peer.
2782 * NULL when there are no transmittable messages.
2784 struct MeshPeerQueue *
2785 queue_get_next (const struct MeshPeerInfo *peer)
2787 struct MeshPeerQueue *q;
2789 struct GNUNET_MESH_Data *dmsg;
2790 struct MeshTunnel* t;
2794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
2795 for (q = peer->queue_head; NULL != q; q = q->next)
2798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2800 GNUNET_MESH_DEBUG_M2S (q->type));
2801 dmsg = (struct GNUNET_MESH_Data *) q->cls;
2802 pid = ntohl (dmsg->pid);
2805 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2806 ack = t->next_fc.last_ack_recv;
2808 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2809 ack = t->prev_fc.last_ack_recv;
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2817 "* ACK: %u, PID: %u\n",
2819 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2832 "* nothing found\n");
2838 queue_send (void *cls, size_t size, void *buf)
2840 struct MeshPeerInfo *peer = cls;
2841 struct GNUNET_MessageHeader *msg;
2842 struct MeshPeerQueue *queue;
2843 struct MeshTunnel *t;
2844 struct GNUNET_PeerIdentity dst_id;
2845 struct MeshFlowControl *fc;
2850 peer->core_transmit = NULL;
2852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
2853 queue = queue_get_next (peer);
2855 /* Queue has no internal mesh traffic nor sendable payload */
2858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
2859 if (NULL == peer->queue_head)
2860 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
2863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
2865 GNUNET_PEER_resolve (peer->id, &dst_id);
2866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2868 GNUNET_i2s (&dst_id));
2869 /* Check if buffer size is enough for the message */
2870 if (queue->size > size)
2872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2873 "* not enough room, reissue\n");
2874 peer->core_transmit =
2875 GNUNET_CORE_notify_transmit_ready (core_handle,
2878 GNUNET_TIME_UNIT_FOREVER_REL,
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
2888 GNUNET_assert (0 < t->pending_messages);
2889 t->pending_messages--;
2893 switch (queue->type)
2896 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2897 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2898 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
2899 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
2900 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2901 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2904 GNUNET_MESH_DEBUG_M2S (queue->type));
2906 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2907 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2908 data_size = send_core_data_raw (queue->cls, size, buf);
2909 msg = (struct GNUNET_MessageHeader *) buf;
2910 type = ntohs (msg->type);
2912 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
2914 data_size = send_core_path_create (queue->cls, size, buf);
2916 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
2918 data_size = send_core_path_ack (queue->cls, size, buf);
2922 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2923 "* type unknown: %u\n",
2928 if (0 < drop_percent &&
2929 GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
2931 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2932 "Dropping message of type %s\n",
2933 GNUNET_MESH_DEBUG_M2S(queue->type));
2936 /* Free queue, but cls was freed by send_core_* */
2937 queue_destroy (queue, GNUNET_NO);
2939 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
2940 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
2943 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2944 t->next_fc.last_pid_sent = pid;
2945 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
2947 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2948 t->prev_fc.last_pid_sent = pid;
2949 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
2955 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
2957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
2961 /* If more data in queue, send next */
2962 queue = queue_get_next (peer);
2965 struct GNUNET_PeerIdentity id;
2967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
2968 GNUNET_PEER_resolve (peer->id, &id);
2969 peer->core_transmit =
2970 GNUNET_CORE_notify_transmit_ready(core_handle,
2973 GNUNET_TIME_UNIT_FOREVER_REL,
2979 else if (NULL != peer->queue_head)
2981 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2983 GNUNET_i2s (&my_full_id));
2984 if (peer->id == t->next_hop)
2986 else if (peer->id == t->prev_hop)
2993 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
2996 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
3000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
3006 * @brief Queue and pass message to core when possible.
3008 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
3009 * accounts for it. In case the queue is full, the message is dropped and
3012 * Otherwise, message is treated as internal and allowed to go regardless of
3015 * @param cls Closure (@c type dependant). It will be used by queue_send to
3016 * build the message to be sent if not already prebuilt.
3017 * @param type Type of the message, 0 for a raw message.
3018 * @param size Size of the message.
3019 * @param dst Neighbor to send message to.
3020 * @param t Tunnel this message belongs to.
3023 queue_add (void *cls, uint16_t type, size_t size,
3024 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3026 struct MeshPeerQueue *queue;
3027 struct GNUNET_PeerIdentity id;
3031 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3033 n = &t->next_fc.queue_n;
3035 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3037 n = &t->prev_fc.queue_n;
3041 if (*n >= t->queue_max)
3044 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3045 "queue full: %u/%u\n",
3047 GNUNET_STATISTICS_update(stats,
3048 "# messages dropped (buffer full)",
3050 return; /* Drop message */
3054 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3060 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3061 if (NULL == dst->core_transmit)
3063 GNUNET_PEER_resolve (dst->id, &id);
3064 dst->core_transmit =
3065 GNUNET_CORE_notify_transmit_ready (core_handle,
3068 GNUNET_TIME_UNIT_FOREVER_REL,
3074 t->pending_messages++;
3078 /******************************************************************************/
3079 /******************** MESH NETWORK HANDLERS **************************/
3080 /******************************************************************************/
3084 * Core handler for path creation
3086 * @param cls closure
3087 * @param message message
3088 * @param peer peer identity this notification is about
3090 * @return GNUNET_OK to keep the connection open,
3091 * GNUNET_SYSERR to close it (signal serious error)
3094 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3095 const struct GNUNET_MessageHeader *message)
3097 unsigned int own_pos;
3100 MESH_TunnelNumber tid;
3101 struct GNUNET_MESH_CreateTunnel *msg;
3102 struct GNUNET_PeerIdentity *pi;
3103 struct MeshPeerPath *path;
3104 struct MeshPeerInfo *dest_peer_info;
3105 struct MeshPeerInfo *orig_peer_info;
3106 struct MeshTunnel *t;
3108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3109 "Received a path create msg [%s]\n",
3110 GNUNET_i2s (&my_full_id));
3111 size = ntohs (message->size);
3112 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3114 GNUNET_break_op (0);
3118 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3119 if (size % sizeof (struct GNUNET_PeerIdentity))
3121 GNUNET_break_op (0);
3124 size /= sizeof (struct GNUNET_PeerIdentity);
3127 GNUNET_break_op (0);
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3131 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3133 tid = ntohl (msg->tid);
3134 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3136 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3137 t = tunnel_get (pi, tid);
3138 if (NULL == t) /* might be a local tunnel */
3142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3143 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3149 t->port = ntohl (msg->port);
3150 opt = ntohl (msg->opt);
3151 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
3153 t->nobuffer = GNUNET_YES;
3156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3158 tunnel_reset_timeout (t);
3160 t->state = MESH_TUNNEL_WAITING;
3162 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3163 if (NULL == dest_peer_info)
3165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3166 " Creating PeerInfo for destination.\n");
3167 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3168 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3169 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3171 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3173 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3174 if (NULL == orig_peer_info)
3176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3177 " Creating PeerInfo for origin.\n");
3178 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3179 orig_peer_info->id = GNUNET_PEER_intern (pi);
3180 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3181 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3184 path = path_new (size);
3186 for (i = 0; i < size; i++)
3188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3189 GNUNET_i2s (&pi[i]));
3190 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3191 if (path->peers[i] == myid)
3194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3195 if (own_pos == 0 && path->peers[own_pos] != myid)
3197 /* create path: self not found in path through self */
3198 GNUNET_break_op (0);
3199 path_destroy (path);
3203 path_add_to_peers (path, GNUNET_NO);
3204 tunnel_use_path (t, path);
3206 peer_info_add_tunnel (dest_peer_info, t);
3208 if (own_pos == size - 1)
3210 struct MeshClient *c;
3211 struct GNUNET_HashCode hc;
3213 /* Find target client */
3214 GMC_hash32 (t->port, &hc);
3215 c = GNUNET_CONTAINER_multihashmap_get (ports, &hc);
3218 /* TODO send reject */
3222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3223 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3225 /* Assign local tid */
3226 while (NULL != tunnel_get_incoming (next_local_tid))
3227 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3228 t->local_tid_dest = next_local_tid++;
3229 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3231 tunnel_add_client (t, c);
3232 send_client_tunnel_create (t);
3237 struct MeshPeerPath *path2;
3239 t->next_hop = path->peers[own_pos + 1];
3240 GNUNET_PEER_change_rc(t->next_hop, 1);
3242 /* It's for somebody else! Retransmit. */
3243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3244 path2 = path_duplicate (path);
3245 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3246 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3247 send_create_path (t);
3255 * Core handler for path ACKs
3257 * @param cls closure
3258 * @param message message
3259 * @param peer peer identity this notification is about
3261 * @return GNUNET_OK to keep the connection open,
3262 * GNUNET_SYSERR to close it (signal serious error)
3265 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3266 const struct GNUNET_MessageHeader *message)
3268 struct GNUNET_MESH_PathACK *msg;
3269 struct MeshPeerInfo *peer_info;
3270 struct MeshPeerPath *p;
3271 struct MeshTunnel *t;
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3274 GNUNET_i2s (&my_full_id));
3275 msg = (struct GNUNET_MESH_PathACK *) message;
3276 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3279 /* TODO notify that we don't know the tunnel */
3280 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3282 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3286 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3288 peer_info = peer_get (&msg->peer_id);
3289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3290 GNUNET_i2s (&msg->peer_id));
3291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3294 /* Add path to peers? */
3298 path_add_to_peers (p, GNUNET_YES);
3304 t->state = MESH_TUNNEL_READY;
3305 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3306 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3308 /* Message for us? */
3309 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3312 if (NULL == t->owner)
3314 GNUNET_break_op (0);
3317 if (NULL != peer_info->dhtget)
3319 GNUNET_DHT_get_stop (peer_info->dhtget);
3320 peer_info->dhtget = NULL;
3322 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3323 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3328 " not for us, retransmitting...\n");
3329 peer_info = peer_get (&msg->oid);
3330 send_prebuilt_message (message, t->prev_hop, t);
3336 * Core handler for notifications of broken paths
3338 * @param cls closure
3339 * @param message message
3340 * @param peer peer identity this notification is about
3342 * @return GNUNET_OK to keep the connection open,
3343 * GNUNET_SYSERR to close it (signal serious error)
3346 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3347 const struct GNUNET_MessageHeader *message)
3349 struct GNUNET_MESH_PathBroken *msg;
3350 struct MeshTunnel *t;
3352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3353 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3354 msg = (struct GNUNET_MESH_PathBroken *) message;
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3356 GNUNET_i2s (&msg->peer1));
3357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3358 GNUNET_i2s (&msg->peer2));
3359 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3362 GNUNET_break_op (0);
3365 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3366 GNUNET_PEER_search (&msg->peer2));
3373 * Core handler for tunnel destruction
3375 * @param cls closure
3376 * @param message message
3377 * @param peer peer identity this notification is about
3379 * @return GNUNET_OK to keep the connection open,
3380 * GNUNET_SYSERR to close it (signal serious error)
3383 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3384 const struct GNUNET_MessageHeader *message)
3386 struct GNUNET_MESH_TunnelDestroy *msg;
3387 struct MeshTunnel *t;
3389 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3391 "Got a TUNNEL DESTROY packet from %s\n",
3393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3394 " for tunnel %s [%u]\n",
3395 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3396 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3399 /* Probably already got the message from another path,
3400 * destroyed the tunnel and retransmitted to children.
3403 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3407 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3410 t->local_tid, t->local_tid_dest);
3412 if (GNUNET_PEER_search (peer) == t->prev_hop)
3414 // TODO check owner's signature
3415 // TODO add owner's signatue to tunnel for retransmission
3416 peer_cancel_queues (t->prev_hop, t);
3417 GNUNET_PEER_change_rc (t->prev_hop, -1);
3420 else if (GNUNET_PEER_search (peer) == t->next_hop)
3422 // TODO check dest's signature
3423 // TODO add dest's signatue to tunnel for retransmission
3424 peer_cancel_queues (t->next_hop, t);
3425 GNUNET_PEER_change_rc (t->next_hop, -1);
3430 GNUNET_break_op (0);
3431 // TODO check both owner AND destination's signature to see which matches
3432 // TODO restransmit in appropriate direction
3435 tunnel_destroy_empty (t);
3437 // TODO: add timeout to destroy the tunnel anyway
3443 * Core handler for mesh network traffic going from the origin to a peer
3445 * @param cls closure
3446 * @param peer peer identity this notification is about
3447 * @param message message
3448 * @return GNUNET_OK to keep the connection open,
3449 * GNUNET_SYSERR to close it (signal serious error)
3452 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3453 const struct GNUNET_MessageHeader *message)
3455 struct GNUNET_MESH_Data *msg;
3456 struct MeshTunnel *t;
3461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3464 size = ntohs (message->size);
3466 sizeof (struct GNUNET_MESH_Data) +
3467 sizeof (struct GNUNET_MessageHeader))
3472 msg = (struct GNUNET_MESH_Data *) message;
3473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3474 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3476 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3479 /* TODO notify back: we don't know this tunnel */
3480 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3481 GNUNET_break_op (0);
3484 pid = ntohl (msg->pid);
3485 if (t->prev_fc.last_pid_recv == pid)
3487 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3488 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3489 " Already seen pid %u, DROPPING!\n", pid);
3494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3495 " pid %u not seen yet, forwarding\n", pid);
3498 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3500 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3501 GNUNET_break_op (0);
3502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3503 "Received PID %u, ACK %u\n",
3504 pid, t->prev_fc.last_ack_sent);
3505 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3508 t->prev_fc.last_pid_recv = pid;
3510 tunnel_reset_timeout (t);
3511 if (t->dest == myid)
3513 /* TODO signature verification */
3514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3515 " it's for us! sending to clients...\n");
3516 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3517 tunnel_send_client_ucast (t, msg);
3518 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3521 if (0 == t->next_hop)
3526 ttl = ntohl (msg->ttl);
3527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3530 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3531 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3532 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3536 " not for us, retransmitting...\n");
3538 send_prebuilt_message (message, t->next_hop, t);
3539 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3545 * Core handler for mesh network traffic toward the owner of a tunnel
3547 * @param cls closure
3548 * @param message message
3549 * @param peer peer identity this notification is about
3551 * @return GNUNET_OK to keep the connection open,
3552 * GNUNET_SYSERR to close it (signal serious error)
3555 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3556 const struct GNUNET_MessageHeader *message)
3558 struct GNUNET_MESH_Data *msg;
3559 struct MeshTunnel *t;
3564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3566 size = ntohs (message->size);
3567 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
3568 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3570 GNUNET_break_op (0);
3573 msg = (struct GNUNET_MESH_Data *) message;
3574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3575 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3576 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3577 pid = ntohl (msg->pid);
3581 /* TODO notify that we dont know this tunnel (whom)? */
3582 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3584 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3585 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3589 if (t->next_fc.last_pid_recv == pid)
3591 /* already seen this packet, drop */
3592 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3594 " Already seen pid %u, DROPPING!\n", pid);
3595 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3599 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3601 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3602 GNUNET_break_op (0);
3603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3604 "Received PID %u, ACK %u\n",
3605 pid, t->next_fc.last_ack_sent);
3606 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3609 t->next_fc.last_pid_recv = pid;
3610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3611 " pid %u not seen yet, forwarding\n", pid);
3613 if (myid == t->id.oid)
3615 /* TODO signature verification */
3616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3617 " it's for us! sending to clients...\n");
3618 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3619 tunnel_send_client_to_orig (t, msg);
3620 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3624 " not for us, retransmitting...\n");
3626 if (0 == t->prev_hop) /* No owner AND no prev hop */
3628 if (GNUNET_YES == t->destroy)
3630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3631 "to orig received on a dying tunnel %s [%X]\n",
3632 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3635 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3636 "unknown to origin at %s\n",
3637 GNUNET_i2s (&my_full_id));
3638 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3642 "on tunnel %s [%X]\n",
3643 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3646 ttl = ntohl (msg->ttl);
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3650 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3652 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3655 send_prebuilt_message (message, t->prev_hop, t);
3656 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3663 * Core handler for mesh network traffic point-to-point acks.
3665 * @param cls closure
3666 * @param message message
3667 * @param peer peer identity this notification is about
3669 * @return GNUNET_OK to keep the connection open,
3670 * GNUNET_SYSERR to close it (signal serious error)
3673 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3674 const struct GNUNET_MessageHeader *message)
3676 struct GNUNET_MESH_ACK *msg;
3677 struct MeshTunnel *t;
3681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3683 msg = (struct GNUNET_MESH_ACK *) message;
3685 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3689 /* TODO notify that we dont know this tunnel (whom)? */
3690 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3693 ack = ntohl (msg->pid);
3694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3696 /* Is this a forward or backward ACK? */
3697 id = GNUNET_PEER_search (peer);
3698 if (t->next_hop == id)
3701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3702 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task &&
3703 GMC_is_pid_bigger (ack, t->next_fc.last_ack_recv))
3705 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3706 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3707 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3709 t->next_fc.last_ack_recv = ack;
3710 peer_unlock_queue (t->next_hop);
3711 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3713 else if (t->prev_hop == id)
3716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3717 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
3718 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
3720 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3721 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3722 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3724 t->prev_fc.last_ack_recv = ack;
3725 peer_unlock_queue (t->prev_hop);
3726 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3729 GNUNET_break_op (0);
3735 * Core handler for mesh network traffic point-to-point ack polls.
3737 * @param cls closure
3738 * @param message message
3739 * @param peer peer identity this notification is about
3741 * @return GNUNET_OK to keep the connection open,
3742 * GNUNET_SYSERR to close it (signal serious error)
3745 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3746 const struct GNUNET_MessageHeader *message)
3748 struct GNUNET_MESH_Poll *msg;
3749 struct MeshTunnel *t;
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3755 msg = (struct GNUNET_MESH_Poll *) message;
3757 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3761 /* TODO notify that we dont know this tunnel (whom)? */
3762 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
3763 GNUNET_break_op (0);
3767 /* Is this a forward or backward ACK? */
3768 id = GNUNET_PEER_search(peer);
3769 if (t->next_hop == id)
3771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
3772 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3774 else if (t->prev_hop == id)
3776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
3777 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3787 * Core handler for mesh keepalives.
3789 * @param cls closure
3790 * @param message message
3791 * @param peer peer identity this notification is about
3792 * @return GNUNET_OK to keep the connection open,
3793 * GNUNET_SYSERR to close it (signal serious error)
3795 * TODO: Check who we got this from, to validate route.
3798 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
3799 const struct GNUNET_MessageHeader *message)
3801 struct GNUNET_MESH_TunnelKeepAlive *msg;
3802 struct MeshTunnel *t;
3804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
3807 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
3808 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3812 /* TODO notify that we dont know that tunnel */
3813 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
3818 tunnel_reset_timeout (t);
3820 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
3821 send_prebuilt_message (message, t->next_hop, t);
3828 * Functions to handle messages from core
3830 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3831 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3832 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3833 sizeof (struct GNUNET_MESH_PathBroken)},
3834 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
3835 sizeof (struct GNUNET_MESH_TunnelDestroy)},
3836 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3837 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
3838 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
3839 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3840 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
3841 sizeof (struct GNUNET_MESH_ACK)},
3842 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
3843 sizeof (struct GNUNET_MESH_Poll)},
3844 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3845 sizeof (struct GNUNET_MESH_PathACK)},
3851 /******************************************************************************/
3852 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3853 /******************************************************************************/
3858 * notify_client_connection_failure: notify a client that the connection to the
3859 * requested remote peer is not possible (for instance, no route found)
3860 * Function called when the socket is ready to queue more data. "buf" will be
3861 * NULL and "size" zero if the socket was closed for writing in the meantime.
3863 * @param cls closure
3864 * @param size number of bytes available in buf
3865 * @param buf where the callee should write the message
3866 * @return number of bytes written to buf
3869 notify_client_connection_failure (void *cls, size_t size, void *buf)
3872 struct MeshPeerInfo *peer_info;
3873 struct GNUNET_MESH_PeerControl *msg;
3874 struct GNUNET_PeerIdentity id;
3876 if (0 == size && NULL == buf)
3878 // TODO retry? cancel?
3882 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3883 peer_info = (struct MeshPeerInfo *) cls;
3884 msg = (struct GNUNET_MESH_PeerControl *) buf;
3885 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3886 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3887 // msg->tunnel_id = htonl(peer_info->t->tid);
3888 GNUNET_PEER_resolve (peer_info->id, &id);
3889 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3897 * Send keepalive packets for a tunnel.
3899 * @param cls Closure (tunnel for which to send the keepalive).
3900 * @param tc Notification context.
3902 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
3905 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3907 struct MeshTunnel *t = cls;
3908 struct GNUNET_MESH_TunnelKeepAlive *msg;
3909 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
3912 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3913 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
3914 NULL == t->owner || 0 == t->local_tid)
3919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3920 "sending keepalive for tunnel %d\n", t->id.tid);
3922 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
3923 msg->header.size = htons (size);
3924 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3925 msg->oid = my_full_id;
3926 msg->tid = htonl (t->id.tid);
3927 send_prebuilt_message (&msg->header, t->next_hop, t);
3929 t->maintenance_task =
3930 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3935 * Function to process paths received for a new peer addition. The recorded
3936 * paths form the initial tunnel, which can be optimized later.
3937 * Called on each result obtained for the DHT search.
3939 * @param cls closure
3940 * @param exp when will this value expire
3941 * @param key key of the result
3942 * @param get_path path of the get request
3943 * @param get_path_length lenght of get_path
3944 * @param put_path path of the put request
3945 * @param put_path_length length of the put_path
3946 * @param type type of the result
3947 * @param size number of bytes in data
3948 * @param data pointer to the result data
3951 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3952 const struct GNUNET_HashCode * key,
3953 const struct GNUNET_PeerIdentity *get_path,
3954 unsigned int get_path_length,
3955 const struct GNUNET_PeerIdentity *put_path,
3956 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3957 size_t size, const void *data)
3959 struct MeshPeerInfo *peer = cls;
3960 struct MeshPeerPath *p;
3961 struct GNUNET_PeerIdentity pi;
3964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3965 GNUNET_PEER_resolve (peer->id, &pi);
3966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3968 p = path_build_from_dht (get_path, get_path_length,
3969 put_path, put_path_length);
3970 path_add_to_peers (p, GNUNET_NO);
3972 for (i = 0; i < peer->ntunnels; i++)
3974 struct GNUNET_PeerIdentity id;
3976 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
3977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
3978 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
3979 peer->tunnels[i]->local_tid,
3980 peer->tunnels[i]->local_tid_dest);
3981 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
3983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
3984 peer_connect (peer, peer->tunnels[i]);
3992 /******************************************************************************/
3993 /********************* MESH LOCAL HANDLES **************************/
3994 /******************************************************************************/
3998 * Handler for client disconnection
4000 * @param cls closure
4001 * @param client identification of the client; NULL
4002 * for the last call when the server is destroyed
4005 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
4007 struct MeshClient *c;
4008 struct MeshClient *next;
4010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
4013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4020 if (c->handle != client)
4022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4023 " ... searching %p (%u)\n",
4028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4030 GNUNET_SERVER_client_drop (c->handle);
4031 c->shutting_down = GNUNET_YES;
4032 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4033 &tunnel_destroy_iterator, c);
4034 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4035 &tunnel_destroy_iterator, c);
4036 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4037 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4039 if (NULL != c->ports)
4040 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
4042 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4045 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4054 * Handler for new clients
4056 * @param cls closure
4057 * @param client identification of the client
4058 * @param message the actual message, which includes messages the client wants
4061 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4062 const struct GNUNET_MessageHeader *message)
4064 struct GNUNET_MESH_ClientConnect *cc_msg;
4065 struct MeshClient *c;
4070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4072 /* Check data sanity */
4073 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4074 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4075 if (0 != (size % sizeof (uint32_t)))
4078 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4081 size /= sizeof (uint32_t);
4083 /* Create new client structure */
4084 c = GNUNET_malloc (sizeof (struct MeshClient));
4085 c->id = next_client_id++; /* overflow not important: just for debug */
4086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4089 GNUNET_SERVER_client_keep (client);
4093 struct GNUNET_HashCode hc;
4095 p = (uint32_t *) &cc_msg[1];
4096 c->ports = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
4097 for (i = 0; i < size; i++)
4100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4101 GMC_hash32 (u32, &hc);
4103 /* store in client's hashmap */
4104 GNUNET_CONTAINER_multihashmap_put (c->ports, &hc, c,
4105 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4106 /* store in global hashmap */
4107 /* FIXME only allow one client to have the port open,
4108 * have a backup hashmap with waiting clients */
4109 GNUNET_CONTAINER_multihashmap_put (ports, &hc, c,
4110 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4114 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4115 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4116 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4117 GNUNET_SERVER_notification_context_add (nc, client);
4118 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4120 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4126 * Handler for requests of new tunnels
4128 * @param cls Closure.
4129 * @param client Identification of the client.
4130 * @param message The actual message.
4133 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4134 const struct GNUNET_MessageHeader *message)
4136 struct GNUNET_MESH_TunnelMessage *t_msg;
4137 struct MeshPeerInfo *peer_info;
4138 struct MeshTunnel *t;
4139 struct MeshClient *c;
4140 MESH_TunnelNumber tid;
4142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4144 /* Sanity check for client registration */
4145 if (NULL == (c = client_get (client)))
4148 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4153 /* Message size sanity check */
4154 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4157 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4161 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4163 GNUNET_i2s (&t_msg->peer));
4164 /* Sanity check for tunnel numbering */
4165 tid = ntohl (t_msg->tunnel_id);
4166 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4169 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4172 /* Sanity check for duplicate tunnel IDs */
4173 if (NULL != tunnel_get_by_local_id (c, tid))
4176 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4181 while (NULL != tunnel_get_by_pi (myid, next_tid))
4182 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4183 t = tunnel_new (myid, next_tid, c, tid);
4184 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4191 t->port = ntohl (t_msg->port);
4192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4193 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4195 peer_info = peer_get (&t_msg->peer);
4196 peer_info_add_tunnel (peer_info, t);
4197 peer_connect (peer_info, t);
4198 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4204 * Handler for requests of deleting tunnels
4206 * @param cls closure
4207 * @param client identification of the client
4208 * @param message the actual message
4211 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4212 const struct GNUNET_MessageHeader *message)
4214 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4215 struct MeshClient *c;
4216 struct MeshTunnel *t;
4217 MESH_TunnelNumber tid;
4219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4220 "Got a DESTROY TUNNEL from client!\n");
4222 /* Sanity check for client registration */
4223 if (NULL == (c = client_get (client)))
4226 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4231 /* Message sanity check */
4232 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4235 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4239 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4241 /* Retrieve tunnel */
4242 tid = ntohl (tunnel_msg->tunnel_id);
4243 t = tunnel_get_by_local_id(c, tid);
4246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4248 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4252 /* Cleanup after the tunnel */
4253 client_delete_tunnel (c, t);
4258 else if (c == t->owner)
4260 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4264 /* The tunnel will be destroyed when the last message is transmitted. */
4265 tunnel_destroy_empty (t);
4267 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4273 * Handler for requests of seeting tunnel's buffering policy.
4275 * @param cls Closure (unused).
4276 * @param client Identification of the client.
4277 * @param message The actual message.
4280 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
4281 const struct GNUNET_MessageHeader *message)
4283 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4284 struct MeshClient *c;
4285 struct MeshTunnel *t;
4286 MESH_TunnelNumber tid;
4288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4289 "Got a BUFFER request from client!\n");
4291 /* Sanity check for client registration */
4292 if (NULL == (c = client_get (client)))
4295 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4300 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4302 /* Retrieve tunnel */
4303 tid = ntohl (tunnel_msg->tunnel_id);
4304 t = tunnel_get_by_local_id(c, tid);
4307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4309 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4313 switch (ntohs(message->type))
4315 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
4316 t->nobuffer = GNUNET_NO;
4318 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
4319 t->nobuffer = GNUNET_YES;
4325 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4330 * Handler for requests of seeting tunnel's reliability policy.
4332 * @param cls Closure (unused).
4333 * @param client Identification of the client.
4334 * @param message The actual message.
4337 handle_local_tunnel_reliability (void *cls, struct GNUNET_SERVER_Client *client,
4338 const struct GNUNET_MessageHeader *message)
4340 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4341 struct MeshClient *c;
4342 struct MeshTunnel *t;
4343 MESH_TunnelNumber tid;
4345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4346 "Got a BUFFER request from client!\n");
4348 /* Sanity check for client registration */
4349 if (NULL == (c = client_get (client)))
4352 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4357 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4359 /* Retrieve tunnel */
4360 tid = ntohl (tunnel_msg->tunnel_id);
4361 t = tunnel_get_by_local_id(c, tid);
4364 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4366 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4370 switch (ntohs(message->type))
4372 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_RELIABLE:
4373 t->reliable = GNUNET_YES;
4375 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_UNRELIABLE:
4376 t->reliable = GNUNET_NO;
4382 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4387 * Handler for client traffic directed to one peer
4389 * @param cls closure
4390 * @param client identification of the client
4391 * @param message the actual message
4394 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4395 const struct GNUNET_MessageHeader *message)
4397 struct MeshClient *c;
4398 struct MeshTunnel *t;
4399 struct GNUNET_MESH_Data *data_msg;
4400 MESH_TunnelNumber tid;
4403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4404 "Got a unicast request from a client!\n");
4406 /* Sanity check for client registration */
4407 if (NULL == (c = client_get (client)))
4410 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4415 data_msg = (struct GNUNET_MESH_Data *) message;
4417 /* Sanity check for message size */
4418 size = ntohs (message->size);
4419 if (sizeof (struct GNUNET_MESH_Data) +
4420 sizeof (struct GNUNET_MessageHeader) > size)
4423 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4427 /* Tunnel exists? */
4428 tid = ntohl (data_msg->tid);
4429 t = tunnel_get_by_local_id (c, tid);
4433 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4437 /* Is it a local tunnel? Then, does client own the tunnel? */
4438 if (t->owner->handle != client)
4441 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4445 /* PID should be as expected: client<->service communication */
4446 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4449 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4450 "Unicast PID, expected %u, got %u\n",
4451 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4452 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4456 /* Ok, everything is correct, send the message
4457 * (pretend we got it from a mesh peer)
4460 /* Work around const limitation */
4461 char buf[ntohs (message->size)] GNUNET_ALIGN;
4462 struct GNUNET_MESH_Data *copy;
4464 copy = (struct GNUNET_MESH_Data *) buf;
4465 memcpy (buf, data_msg, size);
4466 copy->oid = my_full_id;
4467 copy->tid = htonl (t->id.tid);
4468 copy->ttl = htonl (default_ttl);
4469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4470 " calling generic handler...\n");
4471 handle_mesh_unicast (NULL, &my_full_id, ©->header);
4473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4474 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4481 * Handler for client traffic directed to the origin
4483 * @param cls closure
4484 * @param client identification of the client
4485 * @param message the actual message
4488 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4489 const struct GNUNET_MessageHeader *message)
4491 struct GNUNET_MESH_Data *data_msg;
4492 struct MeshFlowControl *fc;
4493 struct MeshClient *c;
4494 struct MeshTunnel *t;
4495 MESH_TunnelNumber tid;
4498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4499 "Got a ToOrigin request from a client!\n");
4500 /* Sanity check for client registration */
4501 if (NULL == (c = client_get (client)))
4504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4509 data_msg = (struct GNUNET_MESH_Data *) message;
4511 /* Sanity check for message size */
4512 size = ntohs (message->size);
4513 if (sizeof (struct GNUNET_MESH_Data) +
4514 sizeof (struct GNUNET_MessageHeader) > size)
4517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4521 /* Tunnel exists? */
4522 tid = ntohl (data_msg->tid);
4523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4524 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4527 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4530 t = tunnel_get_by_local_id (c, tid);
4533 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4534 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4536 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4540 /* It should be sent by someone who has this as incoming tunnel. */
4544 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4548 /* PID should be as expected */
4550 if (ntohl (data_msg->pid) != fc->last_pid_recv + 1)
4553 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4554 "To Origin PID, expected %u, got %u\n",
4555 fc->last_pid_recv + 1,
4556 ntohl (data_msg->pid));
4557 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4561 /* Ok, everything is correct, send the message
4562 * (pretend we got it from a mesh peer)
4565 char buf[ntohs (message->size)] GNUNET_ALIGN;
4566 struct GNUNET_MESH_Data *copy;
4568 /* Work around 'const' limitation */
4569 memcpy (buf, data_msg, size);
4570 copy = (struct GNUNET_MESH_Data *) buf;
4571 GNUNET_PEER_resolve (t->id.oid, ©->oid);
4572 copy->tid = htonl (t->id.tid);
4573 copy->ttl = htonl (default_ttl);
4575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4576 " calling generic handler...\n");
4577 handle_mesh_to_orig (NULL, &my_full_id, ©->header);
4579 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4586 * Handler for client's ACKs for payload traffic.
4588 * @param cls Closure (unused).
4589 * @param client Identification of the client.
4590 * @param message The actual message.
4593 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4594 const struct GNUNET_MessageHeader *message)
4596 struct GNUNET_MESH_LocalAck *msg;
4597 struct MeshTunnel *t;
4598 struct MeshClient *c;
4599 MESH_TunnelNumber tid;
4602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4603 /* Sanity check for client registration */
4604 if (NULL == (c = client_get (client)))
4607 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4612 msg = (struct GNUNET_MESH_LocalAck *) message;
4614 /* Tunnel exists? */
4615 tid = ntohl (msg->tunnel_id);
4616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4617 t = tunnel_get_by_local_id (c, tid);
4621 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4622 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4623 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4627 ack = ntohl (msg->ack);
4628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4630 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4633 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4634 t->prev_fc.last_ack_recv = ack;
4635 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4639 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4640 t->next_fc.last_ack_recv = ack;
4641 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4644 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4652 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4654 * @param cls Closure (client handle).
4655 * @param key Key (hashed tunnel ID, unused).
4656 * @param value Tunnel info.
4658 * @return GNUNET_YES, to keep iterating.
4661 monitor_all_tunnels_iterator (void *cls,
4662 const struct GNUNET_HashCode * key,
4665 struct GNUNET_SERVER_Client *client = cls;
4666 struct MeshTunnel *t = value;
4667 struct GNUNET_MESH_LocalMonitor *msg;
4669 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4670 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4671 msg->tunnel_id = htonl (t->id.tid);
4672 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4673 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4674 GNUNET_PEER_resolve (t->dest, &msg->destination);
4676 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4677 "* sending info about tunnel %s [%u]\n",
4678 GNUNET_i2s (&msg->owner), t->id.tid);
4680 GNUNET_SERVER_notification_context_unicast (nc, client,
4681 &msg->header, GNUNET_NO);
4687 * Handler for client's MONITOR request.
4689 * @param cls Closure (unused).
4690 * @param client Identification of the client.
4691 * @param message The actual message.
4694 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4695 const struct GNUNET_MessageHeader *message)
4697 struct MeshClient *c;
4699 /* Sanity check for client registration */
4700 if (NULL == (c = client_get (client)))
4703 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4707 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4708 "Received get tunnels request from client %u\n",
4710 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4711 monitor_all_tunnels_iterator,
4713 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4714 "Get tunnels request from client %u completed\n",
4716 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4721 * Handler for client's MONITOR_TUNNEL request.
4723 * @param cls Closure (unused).
4724 * @param client Identification of the client.
4725 * @param message The actual message.
4728 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4729 const struct GNUNET_MessageHeader *message)
4731 const struct GNUNET_MESH_LocalMonitor *msg;
4732 struct GNUNET_MESH_LocalMonitor *resp;
4733 struct MeshClient *c;
4734 struct MeshTunnel *t;
4736 /* Sanity check for client registration */
4737 if (NULL == (c = client_get (client)))
4740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4744 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4745 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4746 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4749 ntohl (msg->tunnel_id));
4750 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4753 /* We don't know the tunnel FIXME */
4754 struct GNUNET_MESH_LocalMonitor warn;
4757 GNUNET_SERVER_notification_context_unicast (nc, client,
4760 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4764 /* Initialize context */
4765 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4767 GNUNET_PEER_resolve (t->dest, &resp->destination);
4768 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4769 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4770 &resp->header, GNUNET_NO);
4773 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4774 "Monitor tunnel request from client %u completed\n",
4776 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4781 * Functions to handle messages from clients
4783 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4784 {&handle_local_new_client, NULL,
4785 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4786 {&handle_local_tunnel_create, NULL,
4787 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4788 sizeof (struct GNUNET_MESH_TunnelMessage)},
4789 {&handle_local_tunnel_destroy, NULL,
4790 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4791 sizeof (struct GNUNET_MESH_TunnelMessage)},
4792 {&handle_local_tunnel_buffer, NULL,
4793 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
4794 sizeof (struct GNUNET_MESH_TunnelMessage)},
4795 {&handle_local_tunnel_buffer, NULL,
4796 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
4797 sizeof (struct GNUNET_MESH_TunnelMessage)},
4798 {&handle_local_tunnel_reliability, NULL,
4799 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
4800 sizeof (struct GNUNET_MESH_TunnelMessage)},
4801 {&handle_local_tunnel_reliability, NULL,
4802 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
4803 sizeof (struct GNUNET_MESH_TunnelMessage)},
4804 {&handle_local_unicast, NULL,
4805 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4806 {&handle_local_to_origin, NULL,
4807 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4808 {&handle_local_ack, NULL,
4809 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4810 sizeof (struct GNUNET_MESH_LocalAck)},
4811 {&handle_local_get_tunnels, NULL,
4812 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4813 sizeof (struct GNUNET_MessageHeader)},
4814 {&handle_local_show_tunnel, NULL,
4815 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4816 sizeof (struct GNUNET_MESH_LocalMonitor)},
4822 * Method called whenever a given peer connects.
4824 * @param cls closure
4825 * @param peer peer identity this notification is about
4828 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4830 struct MeshPeerInfo *peer_info;
4831 struct MeshPeerPath *path;
4833 DEBUG_CONN ("Peer connected\n");
4834 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4835 peer_info = peer_get (peer);
4836 if (myid == peer_info->id)
4838 DEBUG_CONN (" (self)\n");
4839 path = path_new (1);
4843 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4844 path = path_new (2);
4845 path->peers[1] = peer_info->id;
4846 GNUNET_PEER_change_rc (peer_info->id, 1);
4847 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4849 path->peers[0] = myid;
4850 GNUNET_PEER_change_rc (myid, 1);
4851 peer_info_add_path (peer_info, path, GNUNET_YES);
4857 * Method called whenever a peer disconnects.
4859 * @param cls closure
4860 * @param peer peer identity this notification is about
4863 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4865 struct MeshPeerInfo *pi;
4866 struct MeshPeerQueue *q;
4867 struct MeshPeerQueue *n;
4869 DEBUG_CONN ("Peer disconnected\n");
4870 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4880 /* TODO try to reroute this traffic instead */
4881 queue_destroy(q, GNUNET_YES);
4884 if (NULL != pi->core_transmit)
4886 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4887 pi->core_transmit = NULL;
4889 peer_remove_path (pi, pi->id, myid);
4892 DEBUG_CONN (" (self)\n");
4894 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
4900 * Install server (service) handlers and start listening to clients.
4905 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4906 GNUNET_SERVER_disconnect_notify (server_handle,
4907 &handle_local_client_disconnect, NULL);
4908 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
4910 clients_head = NULL;
4911 clients_tail = NULL;
4913 GNUNET_SERVER_resume (server_handle);
4918 * To be called on core init/fail.
4920 * @param cls Closure (config)
4921 * @param server handle to the server for this service
4922 * @param identity the public identity of this peer
4925 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4926 const struct GNUNET_PeerIdentity *identity)
4928 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4932 GNUNET_break (core_handle == server);
4933 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4936 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4937 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4939 GNUNET_i2s (identity));
4940 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4942 GNUNET_i2s (&my_full_id));
4943 GNUNET_CORE_disconnect (core_handle);
4944 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4945 NULL, /* Closure passed to MESH functions */
4946 &core_init, /* Call core_init once connected */
4947 &core_connect, /* Handle connects */
4948 &core_disconnect, /* remove peers on disconnects */
4949 NULL, /* Don't notify about all incoming messages */
4950 GNUNET_NO, /* For header only in notification */
4951 NULL, /* Don't notify about all outbound messages */
4952 GNUNET_NO, /* For header-only out notification */
4953 core_handlers); /* Register these handlers */
4962 /******************************************************************************/
4963 /************************ MAIN FUNCTIONS ****************************/
4964 /******************************************************************************/
4967 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4969 * @param cls closure
4970 * @param key current key code
4971 * @param value value in the hash map
4972 * @return GNUNET_YES if we should continue to iterate,
4976 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
4978 struct MeshTunnel *t = value;
4985 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4987 * @param cls closure
4988 * @param key current key code
4989 * @param value value in the hash map
4990 * @return GNUNET_YES if we should continue to iterate,
4994 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
4996 struct MeshPeerInfo *p = value;
4997 struct MeshPeerQueue *q;
4998 struct MeshPeerQueue *n;
5006 queue_destroy(q, GNUNET_YES);
5010 peer_info_destroy (p);
5016 * Task run during shutdown.
5022 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
5026 if (core_handle != NULL)
5028 GNUNET_CORE_disconnect (core_handle);
5033 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
5036 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
5037 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
5038 if (dht_handle != NULL)
5040 GNUNET_DHT_disconnect (dht_handle);
5045 GNUNET_SERVER_notification_context_destroy (nc);
5048 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
5050 GNUNET_SCHEDULER_cancel (announce_id_task);
5051 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
5053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
5058 * Callback for hostkey read/generation.
5060 * @param cls Closure (Configuration handle).
5061 * @param pk The ECC private key.
5062 * @param emsg Error message, if any.
5065 key_generation_cb (void *cls,
5066 struct GNUNET_CRYPTO_EccPrivateKey *pk,
5069 const struct GNUNET_CONFIGURATION_Handle *c = cls;
5074 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5075 _("Could not access hostkey: %s. Exiting.\n"),
5077 GNUNET_SCHEDULER_shutdown ();
5080 my_private_key = pk;
5081 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5082 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5083 &my_full_id.hashPubKey);
5084 myid = GNUNET_PEER_intern (&my_full_id);
5085 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5086 "Mesh for peer [%s] starting\n",
5087 GNUNET_i2s(&my_full_id));
5089 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5090 NULL, /* Closure passed to MESH functions */
5091 &core_init, /* Call core_init once connected */
5092 &core_connect, /* Handle connects */
5093 &core_disconnect, /* remove peers on disconnects */
5094 NULL, /* Don't notify about all incoming messages */
5095 GNUNET_NO, /* For header only in notification */
5096 NULL, /* Don't notify about all outbound messages */
5097 GNUNET_NO, /* For header-only out notification */
5098 core_handlers); /* Register these handlers */
5099 if (core_handle == NULL)
5102 GNUNET_SCHEDULER_shutdown ();
5107 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5109 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5116 * Process mesh requests.
5118 * @param cls closure
5119 * @param server the initialized server
5120 * @param c configuration to use
5123 run (void *cls, struct GNUNET_SERVER_Handle *server,
5124 const struct GNUNET_CONFIGURATION_Handle *c)
5128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5129 server_handle = server;
5130 GNUNET_SERVER_suspend (server_handle);
5133 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5138 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5139 "mesh", "peer/privatekey");
5140 GNUNET_SCHEDULER_shutdown ();
5145 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5146 &refresh_path_time))
5148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5150 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5151 "mesh", "refresh path time");
5152 GNUNET_SCHEDULER_shutdown ();
5157 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5162 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5163 "mesh", "id announce time");
5164 GNUNET_SCHEDULER_shutdown ();
5169 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5174 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5175 "mesh", "connect timeout");
5176 GNUNET_SCHEDULER_shutdown ();
5181 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5186 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5187 "mesh", "max msgs queue");
5188 GNUNET_SCHEDULER_shutdown ();
5193 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5198 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5199 "mesh", "max tunnels");
5200 GNUNET_SCHEDULER_shutdown ();
5205 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5208 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5210 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5211 "mesh", "default ttl", 64);
5216 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5219 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5220 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5221 "mesh", "max peers", 1000);
5226 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
5233 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5234 "Mesh is running with drop mode enabled. "
5235 "This is NOT a good idea! "
5236 "Remove the DROP_PERCENT option from your configuration.\n");
5240 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5241 &dht_replication_level))
5243 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5245 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5246 "mesh", "dht replication level", 3);
5247 dht_replication_level = 3;
5250 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5251 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5252 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5253 ports = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5255 dht_handle = GNUNET_DHT_connect (c, 64);
5256 if (NULL == dht_handle)
5260 stats = GNUNET_STATISTICS_create ("mesh", c);
5262 /* Scheduled the task to clean up when shutdown is called */
5263 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5265 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
5268 GNUNET_free (keyfile);
5273 * The main function for the mesh service.
5275 * @param argc number of arguments from the command line
5276 * @param argv command line arguments
5277 * @return 0 ok, 1 on error
5280 main (int argc, char *const *argv)
5285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5286 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5288 ret = (GNUNET_OK == r) ? 0 : 1;
5289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5293 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5294 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5295 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);