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
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
41 * - relay corking down to core
42 * - set ttl relative to tree depth
43 * - Add data ACK count in path ACK
44 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
50 #include "mesh2_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "gnunet_dht_service.h"
54 #include "gnunet_statistics_service.h"
56 #define MESH_BLOOM_SIZE 128
58 #define MESH_DEBUG_DHT GNUNET_NO
59 #define MESH_DEBUG_CONNECTION GNUNET_NO
60 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
62 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
63 GNUNET_TIME_UNIT_MINUTES,\
66 #if MESH_DEBUG_CONNECTION
67 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
69 #define DEBUG_CONN(...)
73 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
75 #define DEBUG_DHT(...)
82 struct timespec __mesh_start;
83 struct timespec __mesh_end;
84 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
85 #define INTERVAL_END \
87 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
88 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
89 if (__diff < 0) __diff += 1000000000;\
93 #define INTERVAL_SHOW \
95 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
97 #define INTERVAL_START
102 /******************************************************************************/
103 /************************ DATA STRUCTURES ****************************/
104 /******************************************************************************/
106 /** FWD declaration */
112 * Struct containing info about a queued transmission to this peer
119 struct MeshPeerQueue *next;
124 struct MeshPeerQueue *prev;
127 * Peer this transmission is directed to.
129 struct MeshPeerInfo *peer;
132 * Tunnel this message belongs to.
134 struct MeshTunnel *tunnel;
137 * Pointer to info stucture used as cls.
147 * Size of the message
154 * Struct containing all information regarding a given peer
164 * Last time we heard from this peer
166 struct GNUNET_TIME_Absolute last_contact;
169 * Number of attempts to reconnect so far
171 int n_reconnect_attempts;
174 * Paths to reach the peer, ordered by ascending hop count
176 struct MeshPeerPath *path_head;
179 * Paths to reach the peer, ordered by ascending hop count
181 struct MeshPeerPath *path_tail;
184 * Handle to stop the DHT search for a path to this peer
186 struct GNUNET_DHT_GetHandle *dhtget;
189 * Array of tunnels this peer is the target of.
190 * Most probably a small amount, therefore not a hashmap.
191 * When the path to the peer changes, notify these tunnels to let them
192 * re-adjust their path trees.
194 struct MeshTunnel **tunnels;
197 * Number of tunnels this peers participates in
199 unsigned int ntunnels;
202 * Transmission queue to core DLL head
204 struct MeshPeerQueue *queue_head;
207 * Transmission queue to core DLL tail
209 struct MeshPeerQueue *queue_tail;
212 * How many messages are in the queue to this peer.
214 unsigned int queue_n;
217 * Handle for queued transmissions
219 struct GNUNET_CORE_TransmitHandle *core_transmit;
224 * Struct to encapsulate all the Flow Control information to a peer in the
225 * context of a tunnel: Same peer in different tunnels will have independent
226 * flow control structures, allowing to choke/free tunnels according to its
229 struct MeshFlowControl
232 * ID of the last packet sent towards the peer.
234 uint32_t last_pid_sent;
237 * ID of the last packet received from the peer.
239 uint32_t last_pid_recv;
242 * Last ACK sent to the peer (peer can't send more than this PID).
244 uint32_t last_ack_sent;
247 * Last ACK sent towards the origin (for traffic towards leaf node).
249 uint32_t last_ack_recv;
252 * How many messages are in the queue towards this peer.
257 * Task to poll the peer in case of a lost ACK causes stall.
259 GNUNET_SCHEDULER_TaskIdentifier poll_task;
262 * How frequently to poll for ACKs.
264 struct GNUNET_TIME_Relative poll_time;
267 * On which tunnel to poll.
268 * Using an explicit poll_ctx would not help memory wise,
269 * since the allocated context would have to be stored in the
270 * fc struct in order to free it upon cancelling poll_task.
272 struct MeshTunnel *t;
277 * Globally unique tunnel identification (owner + number)
278 * DO NOT USE OVER THE NETWORK
283 * Node that owns the tunnel
288 * Tunnel number to differentiate all the tunnels owned by the node oid
289 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
291 MESH_TunnelNumber tid;
296 * Struct containing all information regarding a tunnel
297 * For an intermediate node the improtant info used will be:
298 * - id Tunnel unique identification
299 * - paths[0] To know where to send it next
300 * - metainfo: ready, speeds, accounting
307 struct MESH_TunnelID id;
310 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
312 MESH_TunnelNumber local_tid;
315 * Local tunnel number for local destination clients (incoming number)
316 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
319 MESH_TunnelNumber local_tid_dest;
322 * Is the tunnel bufferless (minimum latency)?
327 * Force sending ACK? Flag to allow duplicate ACK on POLL.
332 * How many messages do we accept in the forward queue.
334 unsigned int queue_max;
337 * Last time the tunnel was used
339 struct GNUNET_TIME_Absolute timestamp;
342 * Destination of the tunnel.
347 * Next hop in the tunnel. If 0, @c client must be set.
349 GNUNET_PEER_Id next_hop;
352 * Previous hop in the tunnel. If 0, @c owner must be set.
354 GNUNET_PEER_Id prev_hop;
357 * Flow control information about @c next_hop or @c client.
359 struct MeshFlowControl next_fc;
362 * Flow control information about @c prev_hop or @c owner.
364 struct MeshFlowControl prev_fc;
367 * Client owner of the tunnel, if any
369 struct MeshClient *owner;
372 * Client destination of the tunnel, if any.
374 struct MeshClient *client;
377 * Task to keep the used paths alive at the owner,
378 * time tunnel out on all the other peers.
380 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
383 * Path being used for the tunnel.
385 struct MeshPeerPath *path;
388 * Flag to signal the destruction of the tunnel.
389 * If this is set GNUNET_YES the tunnel will be destroyed
390 * when the queue is empty.
395 * Total messages pending for this tunnels, payload or not.
397 unsigned int pending_messages;
402 * Struct containing information about a client of the service
404 * TODO: add a list of 'waiting' types
411 struct MeshClient *next;
416 struct MeshClient *prev;
419 * Tunnels that belong to this client, indexed by local id
421 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
424 * Tunnels this client has accepted, indexed by incoming local id
426 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
429 * Handle to communicate with the client
431 struct GNUNET_SERVER_Client *handle;
434 * Messages that this client has declared interest in.
435 * Indexed by a GMC_hash32 (type), contains *Client.
437 struct GNUNET_CONTAINER_MultiHashMap *types;
440 * Whether the client is active or shutting down (don't send confirmations
441 * to a client that is shutting down.
446 * ID of the client, mainly for debug messages
453 /******************************************************************************/
454 /************************ DEBUG FUNCTIONS ****************************/
455 /******************************************************************************/
459 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
460 * @param cls string to print
461 * @param success GNUNET_OK if the PUT was transmitted,
462 * GNUNET_NO on timeout,
463 * GNUNET_SYSERR on disconnect from service
464 * after the PUT message was transmitted
465 * (so we don't know if it was received or not)
470 mesh_debug (void *cls, int success)
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
478 unsigned int debug_fwd_ack;
479 unsigned int debug_bck_ack;
483 /******************************************************************************/
484 /*********************** GLOBAL VARIABLES ****************************/
485 /******************************************************************************/
487 /************************** Configuration parameters **************************/
490 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
492 static struct GNUNET_TIME_Relative refresh_path_time;
495 * How often to PUT own ID in the DHT.
497 static struct GNUNET_TIME_Relative id_announce_time;
500 * Maximum time allowed to connect to a peer found by string.
502 static struct GNUNET_TIME_Relative connect_timeout;
505 * Default TTL for payload packets.
507 static unsigned long long default_ttl;
510 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
512 static unsigned long long dht_replication_level;
515 * How many tunnels are we willing to maintain.
516 * Local tunnels are always allowed, even if there are more tunnels than max.
518 static unsigned long long max_tunnels;
521 * How many messages *in total* are we willing to queue, divided by number of
522 * tunnels to get tunnel queue size.
524 static unsigned long long max_msgs_queue;
527 * How many peers do we want to remember?
529 static unsigned long long max_peers;
532 /*************************** Static global variables **************************/
535 * Hostkey generation context
537 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
540 * DLL with all the clients, head.
542 static struct MeshClient *clients_head;
545 * DLL with all the clients, tail.
547 static struct MeshClient *clients_tail;
550 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
552 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
555 * Number of tunnels known.
557 static unsigned long long n_tunnels;
560 * Tunnels incoming, indexed by MESH_TunnelNumber
561 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
563 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
566 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
568 static struct GNUNET_CONTAINER_MultiHashMap *peers;
571 * Handle to communicate with transport
573 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
576 * Handle to communicate with core.
578 static struct GNUNET_CORE_Handle *core_handle;
583 static struct GNUNET_DHT_Handle *dht_handle;
588 static struct GNUNET_SERVER_Handle *server_handle;
591 * Handle to the statistics service.
593 static struct GNUNET_STATISTICS_Handle *stats;
596 * Notification context, to send messages to local clients.
598 static struct GNUNET_SERVER_NotificationContext *nc;
601 * Local peer own ID (memory efficient handle).
603 static GNUNET_PEER_Id myid;
606 * Local peer own ID (full value).
608 static struct GNUNET_PeerIdentity my_full_id;
613 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
618 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
621 * Tunnel ID for the next created tunnel (global tunnel number).
623 static MESH_TunnelNumber next_tid;
626 * Tunnel ID for the next incoming tunnel (local tunnel number).
628 static MESH_TunnelNumber next_local_tid;
631 * All message types clients of this peer are interested in.
633 static struct GNUNET_CONTAINER_MultiHashMap *types;
636 * Task to periodically announce itself in the network.
638 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
641 * Next ID to assign to a client.
643 unsigned int next_client_id;
646 /******************************************************************************/
647 /*********************** DECLARATIONS **************************/
648 /******************************************************************************/
651 * Function to process paths received for a new peer addition. The recorded
652 * paths form the initial tunnel, which can be optimized later.
653 * Called on each result obtained for the DHT search.
656 * @param exp when will this value expire
657 * @param key key of the result
658 * @param type type of the result
659 * @param size number of bytes in data
660 * @param data pointer to the result data
663 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
664 const struct GNUNET_HashCode * key,
665 const struct GNUNET_PeerIdentity *get_path,
666 unsigned int get_path_length,
667 const struct GNUNET_PeerIdentity *put_path,
668 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
669 size_t size, const void *data);
673 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
674 * and insert it in the appropiate structures if the peer is not known yet.
676 * @param peer Full identity of the peer.
678 * @return Existing or newly created peer info.
680 static struct MeshPeerInfo *
681 peer_get (const struct GNUNET_PeerIdentity *peer);
685 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
686 * and insert it in the appropiate structures if the peer is not known yet.
688 * @param peer Short identity of the peer.
690 * @return Existing or newly created peer info.
692 static struct MeshPeerInfo *
693 peer_get_short (const GNUNET_PEER_Id peer);
697 * Build a PeerPath from the paths returned from the DHT, reversing the paths
698 * to obtain a local peer -> destination path and interning the peer ids.
700 * @return Newly allocated and created path
702 static struct MeshPeerPath *
703 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
704 unsigned int get_path_length,
705 const struct GNUNET_PeerIdentity *put_path,
706 unsigned int put_path_length);
710 * Adds a path to the peer_infos of all the peers in the path
712 * @param p Path to process.
713 * @param confirmed Whether we know if the path works or not.
716 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
721 * Search for a tunnel by global ID using full PeerIdentities.
723 * @param oid owner of the tunnel.
724 * @param tid global tunnel number.
726 * @return tunnel handler, NULL if doesn't exist.
728 static struct MeshTunnel *
729 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
733 * Notify a tunnel that a connection has broken that affects at least
736 * @param t Tunnel affected.
737 * @param p1 Peer that got disconnected from p2.
738 * @param p2 Peer that got disconnected from p1.
740 * @return Short ID of the peer disconnected (either p1 or p2).
741 * 0 if the tunnel remained unaffected.
743 static GNUNET_PEER_Id
744 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
749 * Use the given path for the tunnel.
751 * @param t Tunnel to update.
752 * @param p Path to use.
755 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
758 * @brief Queue and pass message to core when possible.
760 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
761 * and accounts for it. In case the queue is full, the message is dropped and
764 * Otherwise, message is treated as internal and allowed to go regardless of
767 * @param cls Closure (@c type dependant). It will be used by queue_send to
768 * build the message to be sent if not already prebuilt.
769 * @param type Type of the message, 0 for a raw message.
770 * @param size Size of the message.
771 * @param dst Neighbor to send message to.
772 * @param t Tunnel this message belongs to.
775 queue_add (void *cls, uint16_t type, size_t size,
776 struct MeshPeerInfo *dst, struct MeshTunnel *t);
780 * Free a transmission that was already queued with all resources
781 * associated to the request.
783 * @param queue Queue handler to cancel.
784 * @param clear_cls Is it necessary to free associated cls?
787 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
791 * @brief Get the next transmittable message from the queue.
793 * This will be the head, except in the case of being a data packet
794 * not allowed by the destination peer.
796 * @param peer Destination peer.
798 * @return The next viable MeshPeerQueue element to send to that peer.
799 * NULL when there are no transmittable messages.
801 struct MeshPeerQueue *
802 queue_get_next (const struct MeshPeerInfo *peer);
806 * Core callback to write a queued packet to core buffer
808 * @param cls Closure (peer info).
809 * @param size Number of bytes available in buf.
810 * @param buf Where the to write the message.
812 * @return number of bytes written to buf
815 queue_send (void *cls, size_t size, void *buf);
818 /******************************************************************************/
819 /************************ PERIODIC FUNCTIONS ****************************/
820 /******************************************************************************/
823 * Periodically announce self id in the DHT
826 * @param tc task context
829 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
833 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
835 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
839 * - Set data expiration in function of X
842 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
844 block.id = my_full_id;
845 block.type = htonl (0);
846 GNUNET_DHT_put (dht_handle, /* DHT handle */
847 &my_full_id.hashPubKey, /* Key to use */
848 dht_replication_level, /* Replication level */
849 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
850 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
851 sizeof (block), /* Size of the data */
852 (const char *) &block, /* Data itself */
853 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
854 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
855 NULL, /* Continuation */
856 NULL); /* Continuation closure */
858 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
862 /******************************************************************************/
863 /****************** GENERAL HELPER FUNCTIONS ************************/
864 /******************************************************************************/
868 * Check if client has registered with the service and has not disconnected
870 * @param client the client to check
872 * @return non-NULL if client exists in the global DLL
874 static struct MeshClient *
875 client_get (struct GNUNET_SERVER_Client *client)
877 struct MeshClient *c;
882 if (c->handle == client)
891 * Deletes a tunnel from a client (either owner or destination). To be used on
894 * @param c Client whose tunnel to delete.
895 * @param t Tunnel which should be deleted.
898 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
900 struct GNUNET_HashCode hash;
904 GMC_hash32 (t->local_tid, &hash);
905 GNUNET_assert (GNUNET_YES ==
906 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
910 else if (c == t->client)
912 GMC_hash32 (t->local_tid_dest, &hash);
913 GNUNET_assert (GNUNET_YES ==
914 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
926 * Notify all clients (not depending on registration status) that the incoming
927 * tunnel is no longer valid.
929 * @param t Tunnel that was destroyed.
932 send_client_tunnel_destroy (struct MeshTunnel *t)
934 struct GNUNET_MESH_TunnelMessage msg;
936 if (NULL == t->client)
938 msg.header.size = htons (sizeof (msg));
939 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
940 msg.tunnel_id = htonl (t->local_tid_dest);
941 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
942 &msg.header, GNUNET_NO);
947 * Iterator over all the peers to remove the oldest not-used entry.
949 * @param cls Closure (unsued).
950 * @param key ID of the peer.
951 * @param value Peer_Info of the peer.
956 peer_info_timeout (void *cls,
957 const struct GNUNET_HashCode *key,
964 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
965 * and insert it in the appropiate structures if the peer is not known yet.
967 * @param peer Full identity of the peer.
969 * @return Existing or newly created peer info.
971 static struct MeshPeerInfo *
972 peer_get (const struct GNUNET_PeerIdentity *peer)
974 struct MeshPeerInfo *peer_info;
976 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
977 if (NULL == peer_info)
980 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
981 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
983 GNUNET_CONTAINER_multihashmap_iterate (peers,
987 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
988 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
989 peer_info->id = GNUNET_PEER_intern (peer);
991 peer_info->last_contact = GNUNET_TIME_absolute_get();
998 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
999 * and insert it in the appropiate structures if the peer is not known yet.
1001 * @param peer Short identity of the peer.
1003 * @return Existing or newly created peer info.
1005 static struct MeshPeerInfo *
1006 peer_get_short (const GNUNET_PEER_Id peer)
1008 struct GNUNET_PeerIdentity id;
1010 GNUNET_PEER_resolve (peer, &id);
1011 return peer_get (&id);
1016 * Choose the best path towards a peer considering the tunnel properties.
1018 * @param peer The destination peer.
1019 * @param t The tunnel the path is for.
1021 * @return Best current known path towards the peer, if any.
1023 static struct MeshPeerPath *
1024 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1026 struct MeshPeerPath *best_p;
1027 struct MeshPeerPath *p;
1028 unsigned int best_cost;
1031 best_p = p = peer->path_head;
1032 best_cost = cost = p->length;
1035 if ((cost = p->length) < best_cost)
1047 * Remove the tunnel from the list of tunnels to which a peer is target.
1049 * @param peer PeerInfo of the peer.
1050 * @param t Tunnel to remove.
1053 peer_remove_tunnel (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1057 for (i = 0; i < peer->ntunnels; i++)
1060 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1063 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1065 GNUNET_realloc (peer->tunnels,
1066 peer->ntunnels * sizeof(struct MeshTunnel *));
1074 * Core callback to write a pre-constructed data packet to core buffer
1076 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1077 * @param size Number of bytes available in buf.
1078 * @param buf Where the to write the message.
1080 * @return number of bytes written to buf
1083 send_core_data_raw (void *cls, size_t size, void *buf)
1085 struct GNUNET_MessageHeader *msg = cls;
1088 GNUNET_assert (NULL != msg);
1089 total_size = ntohs (msg->size);
1091 if (total_size > size)
1096 memcpy (buf, msg, total_size);
1103 * Sends an already built message to a peer, properly registrating
1104 * all used resources.
1106 * @param message Message to send. Function makes a copy of it.
1107 * @param peer Short ID of the neighbor whom to send the message.
1108 * @param t Tunnel on which this message is transmitted.
1111 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1112 GNUNET_PEER_Id peer,
1113 struct MeshTunnel *t)
1115 struct GNUNET_PeerIdentity id;
1116 struct MeshPeerInfo *neighbor;
1117 struct MeshPeerPath *p;
1122 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1127 size = ntohs (message->size);
1128 data = GNUNET_malloc (size);
1129 memcpy (data, message, size);
1130 type = ntohs(message->type);
1133 struct GNUNET_MESH_Unicast *u;
1134 struct GNUNET_MESH_ToOrigin *to;
1136 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1137 u = (struct GNUNET_MESH_Unicast *) data;
1138 u->ttl = htonl (ntohl (u->ttl) - 1);
1140 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1141 to = (struct GNUNET_MESH_ToOrigin *) data;
1142 t->prev_fc.last_pid_sent++; /* FIXME per hop? */
1143 to->pid = htonl (t->prev_fc.last_pid_sent);
1145 GNUNET_PEER_resolve (peer, &id);
1146 neighbor = peer_get (&id);
1147 for (p = neighbor->path_head; NULL != p; p = p->next)
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158 " %s IS NOT DIRECTLY CONNECTED\n",
1160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1163 for (p = neighbor->path_head; NULL != p; p = p->next)
1165 struct GNUNET_PeerIdentity debug_id;
1168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1169 " path with %u hops through:\n",
1171 for (i = 0; i < p->length; i++)
1173 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 i, GNUNET_i2s(&debug_id));
1180 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1181 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1182 " no direct connection to %s\n",
1187 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1198 * Sends a CREATE PATH message for a path to a peer, properly registrating
1199 * all used resources.
1201 * @param peer PeerInfo of the final peer for whom this path is being created.
1202 * @param p Path itself.
1203 * @param t Tunnel for which the path is created.
1206 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1207 struct MeshTunnel *t)
1209 struct MeshPeerInfo *neighbor;
1217 neighbor = peer_get_short (t->next_hop);
1219 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1220 sizeof (struct GNUNET_MESH_ManipulatePath) +
1221 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1228 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1230 * @param t Tunnel which to confirm.
1233 send_path_ack (struct MeshTunnel *t)
1235 struct MeshPeerInfo *peer;
1237 if (0 == t->prev_hop)
1243 peer = peer_get_short (t->prev_hop);
1246 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1247 sizeof (struct GNUNET_MESH_PathACK),
1254 * Try to establish a new connection to this peer in the fiven tunnel.
1255 * If the peer doesn't have any path to it yet, try to get one.
1256 * If the peer already has some path, send a CREATE PATH towards it.
1258 * @param peer PeerInfo of the peer.
1259 * @param t Tunnel for which to create the path, if possible.
1262 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1264 struct MeshPeerPath *p;
1266 if (NULL != peer->path_head)
1268 p = peer_get_best_path (peer, t);
1269 tunnel_use_path (t, p);
1270 send_create_path (peer, p, t);
1272 else if (NULL == peer->dhtget)
1274 struct GNUNET_PeerIdentity id;
1276 GNUNET_PEER_resolve (peer->id, &id);
1277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1278 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1279 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1280 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1281 &id.hashPubKey, /* key to search */
1282 dht_replication_level, /* replication level */
1283 GNUNET_DHT_RO_RECORD_ROUTE |
1284 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1286 0, /* xquery bits */
1287 &dht_get_id_handler, peer);
1289 /* Otherwise, there is no path but the DHT get is already started. */
1294 * Destroy the peer_info and free any allocated resources linked to it
1296 * @param pi The peer_info to destroy.
1298 * @return GNUNET_OK on success
1301 peer_info_destroy (struct MeshPeerInfo *pi)
1303 struct GNUNET_PeerIdentity id;
1304 struct MeshPeerPath *p;
1305 struct MeshPeerPath *nextp;
1307 GNUNET_PEER_resolve (pi->id, &id);
1308 GNUNET_PEER_change_rc (pi->id, -1);
1311 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1314 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1315 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1317 if (NULL != pi->dhtget)
1319 GNUNET_DHT_get_stop (pi->dhtget);
1325 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1335 * Remove all paths that rely on a direct connection between p1 and p2
1336 * from the peer itself and notify all tunnels about it.
1338 * @param peer PeerInfo of affected peer.
1339 * @param p1 GNUNET_PEER_Id of one peer.
1340 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1343 * TODO: optimize (see below)
1346 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1349 struct MeshPeerPath *p;
1350 struct MeshPeerPath *next;
1351 struct MeshPeerInfo *peer_d;
1353 unsigned int destroyed;
1356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1358 for (p = peer->path_head; NULL != p; p = next)
1361 for (i = 0; i < (p->length - 1); i++)
1363 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1364 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1366 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1376 for (i = 0; i < peer->ntunnels; i++)
1378 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1382 peer_d = peer_get_short (d);
1383 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1384 tunnel_use_path (peer->tunnels[i], next);
1385 peer_connect (peer_d, peer->tunnels[i]);
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1392 * Add the path to the peer and update the path used to reach it in case this
1395 * @param peer_info Destination peer to add the path to.
1396 * @param path New path to add. Last peer must be the peer in arg 1.
1397 * Path will be either used of freed if already known.
1398 * @param trusted Do we trust that this path is real?
1401 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1404 struct MeshPeerPath *aux;
1408 if ((NULL == peer_info) || (NULL == path))
1411 path_destroy (path);
1414 if (path->peers[path->length - 1] != peer_info->id)
1417 path_destroy (path);
1420 if (path->length <= 2 && GNUNET_NO == trusted)
1422 /* Only allow CORE to tell us about direct paths */
1423 path_destroy (path);
1426 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1427 for (l = 1; l < path->length; l++)
1429 if (path->peers[l] == myid)
1431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1432 for (l2 = 0; l2 < path->length - l; l2++)
1434 path->peers[l2] = path->peers[l + l2];
1439 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1444 struct GNUNET_PeerIdentity id;
1446 GNUNET_PEER_resolve (peer_info->id, &id);
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1448 path->length, GNUNET_i2s (&id));
1451 l = path_get_length (path);
1458 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1459 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1461 l2 = path_get_length (aux);
1464 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1465 peer_info->path_tail, aux, path);
1470 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1472 path_destroy (path);
1477 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1484 * Add the path to the origin peer and update the path used to reach it in case
1485 * this is the shortest.
1486 * The path is given in peer_info -> destination, therefore we turn the path
1487 * upside down first.
1489 * @param peer_info Peer to add the path to, being the origin of the path.
1490 * @param path New path to add after being inversed.
1491 * @param trusted Do we trust that this path is real?
1494 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1495 struct MeshPeerPath *path, int trusted)
1498 peer_info_add_path (peer_info, path, trusted);
1503 * Function called if the connection to the peer has been stalled for a while,
1504 * possibly due to a missed ACK. Poll the peer about its ACK status.
1506 * @param cls Closure (poll ctx).
1507 * @param tc TaskContext.
1510 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1512 struct MeshFlowControl *fc = cls;
1513 struct GNUNET_MESH_Poll msg;
1514 struct MeshTunnel *t = fc->t;
1515 GNUNET_PEER_Id peer;
1517 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1518 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1523 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1524 msg.header.size = htons (sizeof (msg));
1525 msg.tid = htonl (t->id.tid);
1526 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1527 msg.last_ack = htonl (fc->last_ack_recv);
1529 if (fc == &t->prev_fc)
1537 send_prebuilt_message (&msg.header, peer, t);
1538 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1539 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1545 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1546 * to obtain a local peer -> destination path and interning the peer ids.
1548 * @return Newly allocated and created path
1550 static struct MeshPeerPath *
1551 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1552 unsigned int get_path_length,
1553 const struct GNUNET_PeerIdentity *put_path,
1554 unsigned int put_path_length)
1556 struct MeshPeerPath *p;
1562 GNUNET_PEER_change_rc (myid, 1);
1563 i = get_path_length;
1564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1565 for (i--; i >= 0; i--)
1567 id = GNUNET_PEER_intern (&get_path[i]);
1568 if (p->length > 0 && id == p->peers[p->length - 1])
1570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1571 GNUNET_PEER_change_rc (id, -1);
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1576 GNUNET_i2s (&get_path[i]));
1578 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1579 p->peers[p->length - 1] = id;
1582 i = put_path_length;
1583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1584 for (i--; i >= 0; i--)
1586 id = GNUNET_PEER_intern (&put_path[i]);
1589 /* PUT path went through us, so discard the path up until now and start
1590 * from here to get a much shorter (and loop-free) path.
1595 if (p->length > 0 && id == p->peers[p->length - 1])
1597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1598 GNUNET_PEER_change_rc (id, -1);
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1603 GNUNET_i2s (&put_path[i]));
1605 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1606 p->peers[p->length - 1] = id;
1610 if (get_path_length > 0)
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1612 GNUNET_i2s (&get_path[0]));
1613 if (put_path_length > 0)
1614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1615 GNUNET_i2s (&put_path[0]));
1616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1618 for (i = 0; i < p->length; i++)
1620 struct GNUNET_PeerIdentity peer_id;
1622 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1624 GNUNET_i2s (&peer_id));
1632 * Adds a path to the peer_infos of all the peers in the path
1634 * @param p Path to process.
1635 * @param confirmed Whether we know if the path works or not.
1638 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1642 /* TODO: invert and add */
1643 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1644 for (i++; i < p->length; i++)
1646 struct MeshPeerInfo *aux;
1647 struct MeshPeerPath *copy;
1649 aux = peer_get_short (p->peers[i]);
1650 copy = path_duplicate (p);
1651 copy->length = i + 1;
1652 peer_info_add_path (aux, copy, GNUNET_NO);
1658 * Send keepalive packets for a peer
1660 * @param cls Closure (tunnel for which to send the keepalive).
1661 * @param tc Notification context.
1664 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1668 * Search for a tunnel among the incoming tunnels
1670 * @param tid the local id of the tunnel
1672 * @return tunnel handler, NULL if doesn't exist
1674 static struct MeshTunnel *
1675 tunnel_get_incoming (MESH_TunnelNumber tid)
1677 struct GNUNET_HashCode hash;
1679 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1680 GMC_hash32 (tid, &hash);
1681 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1686 * Search for a tunnel among the tunnels for a client
1688 * @param c the client whose tunnels to search in
1689 * @param tid the local id of the tunnel
1691 * @return tunnel handler, NULL if doesn't exist
1693 static struct MeshTunnel *
1694 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1696 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1698 return tunnel_get_incoming (tid);
1702 struct GNUNET_HashCode hash;
1704 GMC_hash32 (tid, &hash);
1705 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1711 * Search for a tunnel by global ID using PEER_ID
1713 * @param pi owner of the tunnel
1714 * @param tid global tunnel number
1716 * @return tunnel handler, NULL if doesn't exist
1718 static struct MeshTunnel *
1719 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1721 struct MESH_TunnelID id;
1722 struct GNUNET_HashCode hash;
1727 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1728 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1733 * Search for a tunnel by global ID using full PeerIdentities
1735 * @param oid owner of the tunnel
1736 * @param tid global tunnel number
1738 * @return tunnel handler, NULL if doesn't exist
1740 static struct MeshTunnel *
1741 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1743 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1748 * Add a client to a tunnel, initializing all needed data structures.
1750 * FIXME: make static after implementing port numbers
1752 * @param t Tunnel to which add the client.
1753 * @param c Client which to add to the tunnel.
1756 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1758 if (NULL != t->client)
1763 if (0 != t->next_hop)
1773 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1777 for (i = 0; i < p->length; i++)
1779 if (p->peers[i] == myid)
1782 if (i > p->length - 1)
1788 if (i < p->length - 1)
1789 t->next_hop = p->peers[i + 1];
1793 t->prev_hop = p->peers[i - 1];
1797 if (NULL != t->path)
1798 path_destroy (t->path);
1799 t->path = path_duplicate (p);
1800 if (GNUNET_SCHEDULER_NO_TASK == t->maintenance_task)
1801 t->maintenance_task =
1802 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
1807 * Notifies a tunnel that a connection has broken that affects at least
1808 * some of its peers. Sends a notification towards the root of the tree.
1809 * In case the peer is the owner of the tree, notifies the client that owns
1810 * the tunnel and tries to reconnect.
1812 * @param t Tunnel affected.
1813 * @param p1 Peer that got disconnected from p2.
1814 * @param p2 Peer that got disconnected from p1.
1816 * @return Short ID of the peer disconnected (either p1 or p2).
1817 * 0 if the tunnel remained unaffected.
1819 static GNUNET_PEER_Id
1820 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1823 // if (myid != p1 && myid != p2) FIXME
1828 // if (tree_get_predecessor (t->tree) != 0)
1830 // /* We are the peer still connected, notify owner of the disconnection. */
1831 // struct GNUNET_MESH_PathBroken msg;
1832 // struct GNUNET_PeerIdentity neighbor;
1834 // msg.header.size = htons (sizeof (msg));
1835 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1836 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1837 // msg.tid = htonl (t->id.tid);
1838 // msg.peer1 = my_full_id;
1839 // GNUNET_PEER_resolve (pid, &msg.peer2);
1840 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1841 // send_prebuilt_message (&msg.header, &neighbor, t);
1848 * Build a local ACK message and send it to a local client.
1850 * @param t Tunnel on which to send the ACK.
1851 * @param c Client to whom send the ACK.
1852 * @param ack Value of the ACK.
1855 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
1857 struct GNUNET_MESH_LocalAck msg;
1859 msg.header.size = htons (sizeof (msg));
1860 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1861 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
1862 msg.max_pid = htonl (ack);
1863 GNUNET_SERVER_notification_context_unicast(nc,
1870 * Build an ACK message and queue it to send to the given peer.
1872 * @param t Tunnel on which to send the ACK.
1873 * @param peer Peer to whom send the ACK.
1874 * @param ack Value of the ACK.
1877 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1879 struct GNUNET_MESH_ACK msg;
1881 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1882 msg.header.size = htons (sizeof (msg));
1883 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1884 msg.pid = htonl (ack);
1885 msg.tid = htonl (t->id.tid);
1887 send_prebuilt_message (&msg.header, peer, t);
1892 * Send an ACK informing the predecessor about the available buffer space.
1893 * In case there is no predecessor, inform the owning client.
1894 * If buffering is off, send only on behalf of children or self if endpoint.
1895 * If buffering is on, send when sent to children and buffer space is free.
1896 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
1897 * the ACK itself goes "back" (towards root).
1899 * @param t Tunnel on which to send the ACK.
1900 * @param type Type of message that triggered the ACK transmission.
1903 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
1907 /* Is it after unicast retransmission? */
1910 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1912 "ACK due to FWD DATA retransmission\n");
1913 if (GNUNET_YES == t->nobuffer)
1915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
1919 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1920 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1922 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1923 t->force_ack = GNUNET_YES;
1929 /* Check if we need to transmit the ACK */
1930 if (t->queue_max > t->next_fc.queue_n * 4 &&
1931 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
1932 GNUNET_NO == t->force_ack)
1934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
1935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1936 " t->qmax: %u, t->qn: %u\n",
1937 t->queue_max, t->next_fc.queue_n);
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1939 " t->pid: %u, t->ack: %u\n",
1940 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
1944 /* Ok, ACK might be necessary, what PID to ACK? */
1945 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
1946 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
1948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
1952 t->prev_fc.last_ack_sent = ack;
1953 if (0 != t->prev_hop)
1954 send_ack (t, t->prev_hop, ack);
1955 else if (NULL != t->owner)
1956 send_local_ack (t, t->owner, ack);
1960 t->force_ack = GNUNET_NO;
1965 * Send an ACK informing the children node/client about the available
1967 * If buffering is off, send only on behalf of root (can be self).
1968 * If buffering is on, send when sent to predecessor and buffer space is free.
1969 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
1970 * the ACK itself goes "forward" (towards children/clients).
1972 * @param t Tunnel on which to send the ACK.
1973 * @param type Type of message that triggered the ACK transmission.
1976 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
1979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1980 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
1981 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
1982 /* Is it after data to_origin retransmission? */
1985 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1986 if (GNUNET_YES == t->nobuffer)
1988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1989 " Not sending ACK, nobuffer + traffic\n");
1993 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1994 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1996 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1997 t->force_ack = GNUNET_YES;
2003 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2005 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2008 " Not sending ACK, not needed\n");
2011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2012 " Sending BCK ACK %u (last sent: %u)\n",
2013 ack, t->next_fc.last_ack_sent);
2014 t->next_fc.last_ack_sent = ack;
2016 if (0 != t->next_hop)
2017 send_ack (t, t->next_hop, ack);
2018 else if (NULL != t->client)
2019 send_local_ack (t, t->client, ack);
2022 t->force_ack = GNUNET_NO;
2027 * @brief Re-initiate traffic to this peer if necessary.
2029 * Check if there is traffic queued towards this peer
2030 * and the core transmit handle is NULL (traffic was stalled).
2031 * If so, call core tmt rdy.
2033 * @param peer_id Short ID of peer to which initiate traffic.
2036 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2038 struct MeshPeerInfo *peer;
2039 struct GNUNET_PeerIdentity id;
2040 struct MeshPeerQueue *q;
2043 peer = peer_get_short (peer_id);
2044 if (NULL != peer->core_transmit)
2047 q = queue_get_next (peer);
2050 /* Might br multicast traffic already sent to this particular peer but
2051 * not to other children in this tunnel.
2052 * This way t->queue_n would be > 0 but the queue of this particular peer
2058 GNUNET_PEER_resolve (peer->id, &id);
2059 peer->core_transmit =
2060 GNUNET_CORE_notify_transmit_ready(core_handle,
2063 GNUNET_TIME_UNIT_FOREVER_REL,
2073 * Send a message to all peers in this tunnel that the tunnel is no longer
2076 * @param t The tunnel whose peers to notify.
2079 tunnel_send_destroy (struct MeshTunnel *t)
2081 struct GNUNET_MESH_TunnelDestroy msg;
2082 struct GNUNET_PeerIdentity id;
2084 msg.header.size = htons (sizeof (msg));
2085 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2086 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2087 msg.tid = htonl (t->id.tid);
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2089 " sending tunnel destroy for tunnel: %s [%X]\n",
2090 GNUNET_i2s (&msg.oid), t->id.tid);
2092 if (0 != t->next_hop)
2094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2095 GNUNET_PEER_resolve (t->next_hop, &id);
2096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2097 " sending forward to %s\n",
2099 send_prebuilt_message (&msg.header, t->next_hop, t);
2101 if (0 != t->prev_hop)
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2104 GNUNET_PEER_resolve (t->prev_hop, &id);
2105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2106 " sending back to %s\n",
2108 send_prebuilt_message (&msg.header, t->prev_hop, t);
2114 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2116 * @param t Tunnel which to cancel.
2117 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2120 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2122 struct MeshPeerInfo *peer_info;
2123 struct MeshPeerQueue *pq;
2124 struct MeshPeerQueue *next;
2126 peer_info = peer_get_short (neighbor);
2127 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2130 if (pq->tunnel == t)
2132 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2133 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2135 // Should have been removed on destroy children
2138 queue_destroy (pq, GNUNET_YES);
2141 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2143 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2144 peer_info->core_transmit = NULL;
2150 * Destroy the tunnel and free any allocated resources linked to it.
2152 * @param t the tunnel to destroy
2154 * @return GNUNET_OK on success
2157 tunnel_destroy (struct MeshTunnel *t)
2159 struct MeshClient *c;
2160 struct GNUNET_HashCode hash;
2170 struct GNUNET_PeerIdentity id;
2172 GNUNET_PEER_resolve (t->id.oid, &id);
2173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2174 GNUNET_i2s (&id), t->id.tid);
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2180 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2181 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2189 GMC_hash32 (t->local_tid, &hash);
2191 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2198 if (NULL != t->client)
2200 GMC_hash32 (t->local_tid_dest, &hash);
2202 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2209 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2215 peer_cancel_queues (t->next_hop, t);
2216 peer_cancel_queues (t->prev_hop, t);
2218 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2219 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2222 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2227 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
2230 * Tunnel is empty: destroy it.
2232 * @param t Tunnel to destroy.
2235 tunnel_destroy_empty (struct MeshTunnel *t)
2239 struct GNUNET_PeerIdentity id;
2241 GNUNET_PEER_resolve (t->id.oid, &id);
2242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2243 "executing destruction of empty tunnel %s [%X]\n",
2244 GNUNET_i2s (&id), t->id.tid);
2248 tunnel_send_destroy (t);
2249 if (0 == t->pending_messages)
2252 t->destroy = GNUNET_YES;
2256 * Initialize a Flow Control structure to the initial state.
2258 * @param fc Flow Control structure to initialize.
2261 fc_init (struct MeshFlowControl *fc)
2263 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2264 fc->last_pid_recv = (uint32_t) -1;
2265 fc->last_ack_sent = INITIAL_WINDOW_SIZE - 1;
2266 fc->last_ack_recv = INITIAL_WINDOW_SIZE - 1;
2267 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2268 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2273 * Create a new tunnel
2275 * @param owner Who is the owner of the tunnel (short ID).
2276 * @param tid Tunnel Number of the tunnel.
2277 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2278 * @param local Tunnel Number for the tunnel, for the client point of view.
2280 * @return A new initialized tunnel. NULL on error.
2282 static struct MeshTunnel *
2283 tunnel_new (GNUNET_PEER_Id owner,
2284 MESH_TunnelNumber tid,
2285 struct MeshClient *client,
2286 MESH_TunnelNumber local)
2288 struct MeshTunnel *t;
2289 struct GNUNET_HashCode hash;
2291 if (n_tunnels >= max_tunnels && NULL == client)
2294 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2297 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2299 fc_init (&t->next_fc);
2300 fc_init (&t->prev_fc);
2301 t->local_tid = local;
2303 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2305 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2307 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2308 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2315 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2322 GMC_hash32 (t->local_tid, &hash);
2324 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2325 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2329 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2340 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2341 * client when the client disconnects. If the client is not the owner, the
2342 * owner will get notified if no more clients are in the tunnel and the client
2343 * get removed from the tunnel's list.
2345 * @param cls closure (client that is disconnecting)
2346 * @param key the hash of the local tunnel id (used to access the hashmap)
2347 * @param value the value stored at the key (tunnel to destroy)
2349 * @return GNUNET_OK, keep iterating.
2352 tunnel_destroy_iterator (void *cls,
2353 const struct GNUNET_HashCode * key,
2356 struct MeshTunnel *t = value;
2357 struct MeshClient *c = cls;
2359 send_client_tunnel_destroy (t);
2362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
2363 client_delete_tunnel (c, t);
2364 tunnel_destroy_empty (t);
2367 tunnel_send_destroy (t);
2369 t->destroy = GNUNET_YES;
2376 * Timeout function, destroys tunnel if called
2378 * @param cls Closure (tunnel to destroy).
2379 * @param tc TaskContext
2382 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2384 struct MeshTunnel *t = cls;
2385 struct GNUNET_PeerIdentity id;
2387 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2388 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2390 GNUNET_PEER_resolve(t->id.oid, &id);
2391 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2392 "Tunnel %s [%X] timed out. Destroying.\n",
2393 GNUNET_i2s(&id), t->id.tid);
2394 send_client_tunnel_destroy (t);
2400 * Resets the tunnel timeout. Starts it if no timeout was running.
2402 * @param t Tunnel whose timeout to reset.
2404 * TODO use heap to improve efficiency of scheduler.
2407 tunnel_reset_timeout (struct MeshTunnel *t)
2409 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2411 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2412 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2413 t->maintenance_task =
2414 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2415 (refresh_path_time, 4), &tunnel_timeout, t);
2419 /******************************************************************************/
2420 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2421 /******************************************************************************/
2424 * Function to send a create path packet to a peer.
2426 * @param cls closure
2427 * @param size number of bytes available in buf
2428 * @param buf where the callee should write the message
2429 * @return number of bytes written to buf
2432 send_core_path_create (void *cls, size_t size, void *buf)
2434 struct MeshTunnel *t = cls;
2435 struct GNUNET_MESH_ManipulatePath *msg;
2436 struct GNUNET_PeerIdentity *peer_ptr;
2437 struct MeshPeerPath *p = t->path;
2442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2444 sizeof (struct GNUNET_MESH_ManipulatePath) +
2445 p->length * sizeof (struct GNUNET_PeerIdentity);
2447 if (size < size_needed || NULL == buf)
2452 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2453 msg->header.size = htons (size_needed);
2454 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2455 msg->tid = ntohl (t->id.tid);
2458 if (GNUNET_YES == t->nobuffer)
2459 opt |= MESH_TUNNEL_OPT_NOBUFFER;
2460 msg->opt = htonl(opt);
2463 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2464 for (i = 0; i < p->length; i++)
2466 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2470 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2476 * Creates a path ack message in buf and frees all unused resources.
2478 * @param cls closure (MeshTransmissionDescriptor)
2479 * @param size number of bytes available in buf
2480 * @param buf where the callee should write the message
2481 * @return number of bytes written to buf
2484 send_core_path_ack (void *cls, size_t size, void *buf)
2486 struct MESH_TunnelID *id = cls;
2487 struct GNUNET_MESH_PathACK *msg = buf;
2489 GNUNET_assert (NULL != id);
2490 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2495 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2496 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2497 GNUNET_PEER_resolve (id->oid, &msg->oid);
2498 msg->tid = htonl (id->tid);
2499 msg->peer_id = my_full_id;
2501 /* TODO add signature */
2503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2504 return sizeof (struct GNUNET_MESH_PathACK);
2509 * Free a transmission that was already queued with all resources
2510 * associated to the request.
2512 * @param queue Queue handler to cancel.
2513 * @param clear_cls Is it necessary to free associated cls?
2516 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2518 struct MeshFlowControl *fc;
2520 if (GNUNET_YES == clear_cls)
2522 switch (queue->type)
2524 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2525 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2526 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2528 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2529 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2530 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2531 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2532 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2534 " prebuilt message\n");
2535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2537 GNUNET_MESH_DEBUG_M2S (queue->type));
2539 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2544 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2545 " type %s unknown!\n",
2546 GNUNET_MESH_DEBUG_M2S (queue->type));
2548 GNUNET_free_non_null (queue->cls);
2550 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2551 queue->peer->queue_tail,
2554 /* Delete from appropiate fc in the tunnel */
2555 if (queue->peer->id == queue->tunnel->next_hop)
2556 fc = &queue->tunnel->next_fc;
2557 else if (queue->peer->id == queue->tunnel->next_hop)
2558 fc = &queue->tunnel->next_fc;
2565 GNUNET_free (queue);
2570 * @brief Get the next transmittable message from the queue.
2572 * This will be the head, except in the case of being a data packet
2573 * not allowed by the destination peer.
2575 * @param peer Destination peer.
2577 * @return The next viable MeshPeerQueue element to send to that peer.
2578 * NULL when there are no transmittable messages.
2580 struct MeshPeerQueue *
2581 queue_get_next (const struct MeshPeerInfo *peer)
2583 struct MeshPeerQueue *q;
2585 struct GNUNET_MESH_Unicast *ucast;
2586 struct GNUNET_MESH_ToOrigin *to_orig;
2587 struct MeshTunnel* t;
2591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
2592 for (q = peer->queue_head; NULL != q; q = q->next)
2595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2597 GNUNET_MESH_DEBUG_M2S(q->type));
2600 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2601 ucast = (struct GNUNET_MESH_Unicast *) q->cls;
2602 pid = ntohl (ucast->pid);
2603 ack = t->next_fc.last_ack_recv;
2605 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2606 to_orig = (struct GNUNET_MESH_ToOrigin *) q->cls;
2607 pid = ntohl (to_orig->pid);
2608 ack = t->prev_fc.last_ack_recv;
2611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2616 "********* ACK: %u, PID: %u\n",
2618 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2627 "********* NEXT!\n");
2630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2631 "********* nothing found\n");
2637 queue_send (void *cls, size_t size, void *buf)
2639 struct MeshPeerInfo *peer = cls;
2640 struct GNUNET_MessageHeader *msg;
2641 struct MeshPeerQueue *queue;
2642 struct MeshTunnel *t;
2643 struct GNUNET_PeerIdentity dst_id;
2644 struct MeshFlowControl *fc;
2648 peer->core_transmit = NULL;
2650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
2651 queue = queue_get_next (peer);
2653 /* Queue has no internal mesh traffic nor sendable payload */
2656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
2657 if (NULL == peer->queue_head)
2658 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
2661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
2663 GNUNET_PEER_resolve (peer->id, &dst_id);
2664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2665 "********* towards %s\n",
2666 GNUNET_i2s (&dst_id));
2667 /* Check if buffer size is enough for the message */
2668 if (queue->size > size)
2670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2671 "********* not enough room, reissue\n");
2672 peer->core_transmit =
2673 GNUNET_CORE_notify_transmit_ready (core_handle,
2676 GNUNET_TIME_UNIT_FOREVER_REL,
2683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
2686 GNUNET_assert (0 < t->pending_messages);
2687 t->pending_messages--;
2691 switch (queue->type)
2694 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2695 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2696 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
2697 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
2698 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2699 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2701 "********* raw: %s\n",
2702 GNUNET_MESH_DEBUG_M2S (queue->type));
2704 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2705 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2706 data_size = send_core_data_raw (queue->cls, size, buf);
2707 msg = (struct GNUNET_MessageHeader *) buf;
2708 type = ntohs (msg->type);
2710 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
2712 data_size = send_core_path_create (queue->cls, size, buf);
2714 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
2716 data_size = send_core_path_ack (queue->cls, size, buf);
2720 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2721 "********* type unknown: %u\n",
2726 /* Free queue, but cls was freed by send_core_* */
2727 queue_destroy (queue, GNUNET_NO);
2729 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
2732 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2733 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
2735 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2736 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
2742 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
2744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
2748 /* If more data in queue, send next */
2749 queue = queue_get_next (peer);
2752 struct GNUNET_PeerIdentity id;
2754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
2755 GNUNET_PEER_resolve (peer->id, &id);
2756 peer->core_transmit =
2757 GNUNET_CORE_notify_transmit_ready(core_handle,
2760 GNUNET_TIME_UNIT_FOREVER_REL,
2766 else if (NULL != peer->queue_head)
2768 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2769 "********* %s stalled\n",
2770 GNUNET_i2s (&my_full_id));
2771 if (peer->id == t->next_hop)
2773 else if (peer->id == t->prev_hop)
2780 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
2783 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Return %d\n", data_size);
2793 * @brief Queue and pass message to core when possible.
2795 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
2796 * accounts for it. In case the queue is full, the message is dropped and
2799 * Otherwise, message is treated as internal and allowed to go regardless of
2802 * @param cls Closure (@c type dependant). It will be used by queue_send to
2803 * build the message to be sent if not already prebuilt.
2804 * @param type Type of the message, 0 for a raw message.
2805 * @param size Size of the message.
2806 * @param dst Neighbor to send message to.
2807 * @param t Tunnel this message belongs to.
2810 queue_add (void *cls, uint16_t type, size_t size,
2811 struct MeshPeerInfo *dst, struct MeshTunnel *t)
2813 struct MeshPeerQueue *queue;
2814 struct GNUNET_PeerIdentity id;
2818 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2820 n = &t->next_fc.queue_n;
2822 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
2824 n = &t->prev_fc.queue_n;
2828 if (*n >= t->queue_max)
2831 GNUNET_STATISTICS_update(stats,
2832 "# messages dropped (buffer full)",
2834 return; // Drop message
2838 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2844 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
2845 if (NULL == dst->core_transmit)
2847 GNUNET_PEER_resolve (dst->id, &id);
2848 dst->core_transmit =
2849 GNUNET_CORE_notify_transmit_ready (core_handle,
2852 GNUNET_TIME_UNIT_FOREVER_REL,
2858 t->pending_messages++;
2862 /******************************************************************************/
2863 /******************** MESH NETWORK HANDLERS **************************/
2864 /******************************************************************************/
2868 * Core handler for path creation
2870 * @param cls closure
2871 * @param message message
2872 * @param peer peer identity this notification is about
2874 * @return GNUNET_OK to keep the connection open,
2875 * GNUNET_SYSERR to close it (signal serious error)
2878 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2879 const struct GNUNET_MessageHeader *message)
2881 unsigned int own_pos;
2884 MESH_TunnelNumber tid;
2885 struct GNUNET_MESH_ManipulatePath *msg;
2886 struct GNUNET_PeerIdentity *pi;
2887 struct GNUNET_HashCode hash;
2888 struct MeshPeerPath *path;
2889 struct MeshPeerInfo *dest_peer_info;
2890 struct MeshPeerInfo *orig_peer_info;
2891 struct MeshTunnel *t;
2893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2894 "Received a path create msg [%s]\n",
2895 GNUNET_i2s (&my_full_id));
2896 size = ntohs (message->size);
2897 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2899 GNUNET_break_op (0);
2903 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2904 if (size % sizeof (struct GNUNET_PeerIdentity))
2906 GNUNET_break_op (0);
2909 size /= sizeof (struct GNUNET_PeerIdentity);
2912 GNUNET_break_op (0);
2915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2916 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2918 tid = ntohl (msg->tid);
2919 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2921 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2922 t = tunnel_get (pi, tid);
2923 if (NULL == t) // FIXME only for INCOMING tunnels?
2927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2928 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
2931 // FIXME notify failure
2934 opt = ntohl (msg->opt);
2935 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
2937 t->nobuffer = GNUNET_YES;
2938 t->prev_fc.last_ack_sent = t->prev_fc.last_pid_recv + 1;
2940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
2942 if (GNUNET_YES == t->nobuffer)
2947 // FIXME only assign a local tid if a local client is interested (on demand)
2948 while (NULL != tunnel_get_incoming (next_local_tid))
2949 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2950 t->local_tid_dest = next_local_tid++;
2951 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2954 tunnel_reset_timeout (t);
2955 GMC_hash32 (t->local_tid_dest, &hash);
2957 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2958 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2966 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2967 if (NULL == dest_peer_info)
2969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2970 " Creating PeerInfo for destination.\n");
2971 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2972 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2973 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2975 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2977 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2978 if (NULL == orig_peer_info)
2980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2981 " Creating PeerInfo for origin.\n");
2982 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2983 orig_peer_info->id = GNUNET_PEER_intern (pi);
2984 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2985 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
2988 path = path_new (size);
2990 for (i = 0; i < size; i++)
2992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
2993 GNUNET_i2s (&pi[i]));
2994 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2995 if (path->peers[i] == myid)
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3001 /* cannot be self, must be 'not found' */
3002 /* create path: self not found in path through self */
3003 GNUNET_break_op (0);
3004 path_destroy (path);
3008 path_add_to_peers (path, GNUNET_NO);
3009 t->prev_hop = path->peers[own_pos - 1];
3010 GNUNET_PEER_change_rc (t->prev_hop, 1);
3011 if (own_pos == size - 1)
3013 /* It is for us! Send ack. */
3014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3015 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3021 struct MeshPeerPath *path2;
3023 t->next_hop = path->peers[own_pos + 1];
3024 GNUNET_PEER_change_rc(t->next_hop, 1);
3026 /* It's for somebody else! Retransmit. */
3027 path2 = path_duplicate (path);
3028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3029 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3030 path2 = path_duplicate (path);
3031 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3032 send_create_path (dest_peer_info, path, t);
3039 * Core handler for path destruction
3041 * @param cls closure
3042 * @param message message
3043 * @param peer peer identity this notification is about
3045 * @return GNUNET_OK to keep the connection open,
3046 * GNUNET_SYSERR to close it (signal serious error)
3049 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3050 const struct GNUNET_MessageHeader *message)
3052 struct GNUNET_MESH_ManipulatePath *msg;
3053 struct GNUNET_PeerIdentity *pi;
3054 struct MeshPeerPath *path;
3055 struct MeshTunnel *t;
3056 unsigned int own_pos;
3060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3061 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3062 size = ntohs (message->size);
3063 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3065 GNUNET_break_op (0);
3069 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3070 if (size % sizeof (struct GNUNET_PeerIdentity))
3072 GNUNET_break_op (0);
3075 size /= sizeof (struct GNUNET_PeerIdentity);
3078 GNUNET_break_op (0);
3081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3083 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3084 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3086 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3088 t = tunnel_get (pi, ntohl (msg->tid));
3091 /* TODO notify back: we don't know this tunnel */
3092 GNUNET_break_op (0);
3095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3096 path = path_new (size);
3098 for (i = 0; i < size; i++)
3100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3101 GNUNET_i2s (&pi[i]));
3102 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3103 if (path->peers[i] == myid)
3106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3107 if (own_pos < path->length - 1)
3108 send_prebuilt_message (message, path->peers[own_pos + 1], t);
3110 send_client_tunnel_destroy (t);
3112 // tunnel_delete_peer (t, path->peers[path->length - 1]); FIXME
3113 path_destroy (path);
3119 * Core handler for notifications of broken paths
3121 * @param cls closure
3122 * @param message message
3123 * @param peer peer identity this notification is about
3125 * @return GNUNET_OK to keep the connection open,
3126 * GNUNET_SYSERR to close it (signal serious error)
3129 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3130 const struct GNUNET_MessageHeader *message)
3132 struct GNUNET_MESH_PathBroken *msg;
3133 struct MeshTunnel *t;
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3136 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3137 msg = (struct GNUNET_MESH_PathBroken *) message;
3138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3139 GNUNET_i2s (&msg->peer1));
3140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3141 GNUNET_i2s (&msg->peer2));
3142 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3145 GNUNET_break_op (0);
3148 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3149 GNUNET_PEER_search (&msg->peer2));
3156 * Core handler for tunnel destruction
3158 * @param cls closure
3159 * @param message message
3160 * @param peer peer identity this notification is about
3162 * @return GNUNET_OK to keep the connection open,
3163 * GNUNET_SYSERR to close it (signal serious error)
3166 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3167 const struct GNUNET_MessageHeader *message)
3169 struct GNUNET_MESH_TunnelDestroy *msg;
3170 struct MeshTunnel *t;
3172 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3174 "Got a TUNNEL DESTROY packet from %s\n",
3176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3177 " for tunnel %s [%u]\n",
3178 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3179 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3182 /* Probably already got the message from another path,
3183 * destroyed the tunnel and retransmitted to children.
3186 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3190 // TODO check owner's signature
3191 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3193 /* Tunnel was incoming, notify clients */
3194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3195 t->local_tid, t->local_tid_dest);
3196 send_client_tunnel_destroy (t);
3198 tunnel_send_destroy (t);
3199 t->destroy = GNUNET_YES;
3200 // TODO: add timeout to destroy the tunnel anyway
3206 * Core handler for mesh network traffic going from the origin to a peer
3208 * @param cls closure
3209 * @param peer peer identity this notification is about
3210 * @param message message
3211 * @return GNUNET_OK to keep the connection open,
3212 * GNUNET_SYSERR to close it (signal serious error)
3215 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3216 const struct GNUNET_MessageHeader *message)
3218 struct GNUNET_MESH_Unicast *msg;
3219 struct MeshTunnel *t;
3224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3227 size = ntohs (message->size);
3229 sizeof (struct GNUNET_MESH_Unicast) +
3230 sizeof (struct GNUNET_MessageHeader))
3235 msg = (struct GNUNET_MESH_Unicast *) message;
3236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3237 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3239 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3242 /* TODO notify back: we don't know this tunnel */
3243 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3244 GNUNET_break_op (0);
3247 pid = ntohl (msg->pid);
3248 if (t->prev_fc.last_pid_recv == pid)
3250 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3251 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3252 " Already seen pid %u, DROPPING!\n", pid);
3257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3258 " pid %u not seen yet, forwarding\n", pid);
3261 t->prev_fc.last_pid_recv = pid;
3263 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3265 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3266 GNUNET_break_op (0);
3267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3268 "Received PID %u, ACK %u\n",
3269 pid, t->prev_fc.last_ack_sent);
3273 tunnel_reset_timeout (t);
3274 if (t->dest == myid)
3276 if (NULL == t->client)
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3282 " it's for us! sending to clients...\n");
3283 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3284 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
3285 message, GNUNET_NO);
3286 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3289 if (0 == t->next_hop)
3294 ttl = ntohl (msg->ttl);
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3298 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3299 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3300 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3304 " not for us, retransmitting...\n");
3306 if (GNUNET_YES == t->nobuffer &&
3307 GNUNET_YES == GMC_is_pid_bigger (pid, t->next_fc.last_ack_recv))
3309 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3310 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n",
3311 pid, t->next_fc.last_ack_recv);
3312 GNUNET_break_op (0);
3315 send_prebuilt_message (message, t->next_hop, t);
3316 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3322 * Core handler for mesh network traffic toward the owner of a tunnel
3324 * @param cls closure
3325 * @param message message
3326 * @param peer peer identity this notification is about
3328 * @return GNUNET_OK to keep the connection open,
3329 * GNUNET_SYSERR to close it (signal serious error)
3332 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3333 const struct GNUNET_MessageHeader *message)
3335 struct GNUNET_MESH_ToOrigin *msg;
3336 struct MeshPeerInfo *peer_info;
3337 struct MeshTunnel *t;
3341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3343 size = ntohs (message->size);
3344 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3345 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3347 GNUNET_break_op (0);
3350 msg = (struct GNUNET_MESH_ToOrigin *) message;
3351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3352 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3353 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3354 pid = ntohl (msg->pid);
3358 /* TODO notify that we dont know this tunnel (whom)? */
3359 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3361 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3362 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3367 if (t->next_fc.last_pid_recv == pid)
3369 /* already seen this packet, drop */
3370 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3372 " Already seen pid %u, DROPPING!\n", pid);
3373 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3378 " pid %u not seen yet, forwarding\n", pid);
3379 t->next_fc.last_pid_recv = pid;
3381 if (NULL != t->owner)
3384 struct GNUNET_MESH_ToOrigin *copy;
3386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3387 " it's for us! sending to clients...\n");
3388 /* TODO signature verification */
3389 memcpy (cbuf, message, size);
3390 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3391 copy->tid = htonl (t->local_tid);
3392 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3393 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3394 ©->header, GNUNET_NO);
3395 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3399 " not for us, retransmitting...\n");
3401 peer_info = peer_get (&msg->oid);
3402 if (NULL == peer_info)
3404 /* unknown origin of tunnel */
3408 if (0 == t->prev_hop) /* No owner AND no prev hop */
3410 if (GNUNET_YES == t->destroy)
3412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3413 "to orig received on a dying tunnel %s [%X]\n",
3414 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3418 "unknown to origin at %s\n",
3419 GNUNET_i2s (&my_full_id));
3420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3424 "for tunnel %s [%X]\n",
3425 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3428 send_prebuilt_message (message, t->prev_hop, t);
3429 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3436 * Core handler for mesh network traffic point-to-point acks.
3438 * @param cls closure
3439 * @param message message
3440 * @param peer peer identity this notification is about
3442 * @return GNUNET_OK to keep the connection open,
3443 * GNUNET_SYSERR to close it (signal serious error)
3446 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3447 const struct GNUNET_MessageHeader *message)
3449 struct GNUNET_MESH_ACK *msg;
3450 struct MeshTunnel *t;
3453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3455 msg = (struct GNUNET_MESH_ACK *) message;
3457 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3461 /* TODO notify that we dont know this tunnel (whom)? */
3462 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3465 ack = ntohl (msg->pid);
3466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3468 /* Is this a forward or backward ACK? */
3469 if (t->prev_hop != GNUNET_PEER_search (peer))
3472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3473 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task &&
3474 GMC_is_pid_bigger (ack, t->next_fc.last_ack_recv))
3476 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3477 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3478 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3480 t->next_fc.last_ack_recv = ack;
3481 peer_unlock_queue (t->next_hop);
3482 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3487 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
3488 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
3490 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3491 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3492 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3494 t->prev_fc.last_ack_recv = ack;
3495 peer_unlock_queue (t->prev_hop);
3496 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3503 * Core handler for mesh network traffic point-to-point ack polls.
3505 * @param cls closure
3506 * @param message message
3507 * @param peer peer identity this notification is about
3509 * @return GNUNET_OK to keep the connection open,
3510 * GNUNET_SYSERR to close it (signal serious error)
3513 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3514 const struct GNUNET_MessageHeader *message)
3516 struct GNUNET_MESH_Poll *msg;
3517 struct MeshTunnel *t;
3519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3522 msg = (struct GNUNET_MESH_Poll *) message;
3524 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3528 /* TODO notify that we dont know this tunnel (whom)? */
3529 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
3530 GNUNET_break_op (0);
3534 /* Is this a forward or backward ACK? */
3535 if (t->prev_hop != GNUNET_PEER_search(peer))
3537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
3538 /* FIXME cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send */
3539 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
3544 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3551 * Core handler for path ACKs
3553 * @param cls closure
3554 * @param message message
3555 * @param peer peer identity this notification is about
3557 * @return GNUNET_OK to keep the connection open,
3558 * GNUNET_SYSERR to close it (signal serious error)
3561 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3562 const struct GNUNET_MessageHeader *message)
3564 struct GNUNET_MESH_PathACK *msg;
3565 struct MeshPeerInfo *peer_info;
3566 struct MeshPeerPath *p;
3567 struct MeshTunnel *t;
3569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3570 GNUNET_i2s (&my_full_id));
3571 msg = (struct GNUNET_MESH_PathACK *) message;
3572 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3575 /* TODO notify that we don't know the tunnel */
3576 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3578 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3582 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3584 peer_info = peer_get (&msg->peer_id);
3585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3586 GNUNET_i2s (&msg->peer_id));
3587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3590 /* Add path to peers? */
3594 path_add_to_peers (p, GNUNET_YES);
3601 /* Message for us? */
3602 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3605 if (NULL == t->owner)
3607 GNUNET_break_op (0);
3610 if (NULL != peer_info->dhtget)
3612 GNUNET_DHT_get_stop (peer_info->dhtget);
3613 peer_info->dhtget = NULL;
3618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3619 " not for us, retransmitting...\n");
3620 peer_info = peer_get (&msg->oid);
3621 send_prebuilt_message (message, t->prev_hop, t);
3627 * Core handler for mesh keepalives.
3629 * @param cls closure
3630 * @param message message
3631 * @param peer peer identity this notification is about
3632 * @return GNUNET_OK to keep the connection open,
3633 * GNUNET_SYSERR to close it (signal serious error)
3635 * TODO: Check who we got this from, to validate route.
3638 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
3639 const struct GNUNET_MessageHeader *message)
3641 struct GNUNET_MESH_TunnelKeepAlive *msg;
3642 struct MeshTunnel *t;
3644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
3647 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
3648 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3652 /* TODO notify that we dont know that tunnel */
3653 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
3658 tunnel_reset_timeout (t);
3660 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
3661 send_prebuilt_message (message, t->next_hop, t);
3668 * Functions to handle messages from core
3670 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3671 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3672 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3673 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3674 sizeof (struct GNUNET_MESH_PathBroken)},
3675 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
3676 sizeof (struct GNUNET_MESH_TunnelDestroy)},
3677 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3678 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
3679 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
3680 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3681 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
3682 sizeof (struct GNUNET_MESH_ACK)},
3683 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
3684 sizeof (struct GNUNET_MESH_Poll)},
3685 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3686 sizeof (struct GNUNET_MESH_PathACK)},
3692 /******************************************************************************/
3693 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3694 /******************************************************************************/
3699 * notify_client_connection_failure: notify a client that the connection to the
3700 * requested remote peer is not possible (for instance, no route found)
3701 * Function called when the socket is ready to queue more data. "buf" will be
3702 * NULL and "size" zero if the socket was closed for writing in the meantime.
3704 * @param cls closure
3705 * @param size number of bytes available in buf
3706 * @param buf where the callee should write the message
3707 * @return number of bytes written to buf
3710 notify_client_connection_failure (void *cls, size_t size, void *buf)
3713 struct MeshPeerInfo *peer_info;
3714 struct GNUNET_MESH_PeerControl *msg;
3715 struct GNUNET_PeerIdentity id;
3717 if (0 == size && NULL == buf)
3719 // TODO retry? cancel?
3723 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3724 peer_info = (struct MeshPeerInfo *) cls;
3725 msg = (struct GNUNET_MESH_PeerControl *) buf;
3726 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3727 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3728 // msg->tunnel_id = htonl(peer_info->t->tid);
3729 GNUNET_PEER_resolve (peer_info->id, &id);
3730 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3738 * Send keepalive packets for a tunnel.
3740 * @param cls Closure (tunnel for which to send the keepalive).
3741 * @param tc Notification context.
3744 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3746 struct MeshTunnel *t = cls;
3747 struct GNUNET_MESH_TunnelKeepAlive *msg;
3748 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
3751 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3752 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
3753 NULL == t->owner || 0 == t->local_tid || 0 != t->prev_hop)
3758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3759 "sending keepalive for tunnel %d\n", t->id.tid);
3761 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
3762 msg->header.size = htons (size);
3763 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3764 msg->oid = my_full_id;
3765 msg->tid = htonl (t->id.tid);
3766 send_prebuilt_message (&msg->header, t->next_hop, t);
3768 t->maintenance_task =
3769 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3774 * Function to process paths received for a new peer addition. The recorded
3775 * paths form the initial tunnel, which can be optimized later.
3776 * Called on each result obtained for the DHT search.
3778 * @param cls closure
3779 * @param exp when will this value expire
3780 * @param key key of the result
3781 * @param get_path path of the get request
3782 * @param get_path_length lenght of get_path
3783 * @param put_path path of the put request
3784 * @param put_path_length length of the put_path
3785 * @param type type of the result
3786 * @param size number of bytes in data
3787 * @param data pointer to the result data
3789 * TODO: re-issue the request after certain time? cancel after X results?
3792 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3793 const struct GNUNET_HashCode * key,
3794 const struct GNUNET_PeerIdentity *get_path,
3795 unsigned int get_path_length,
3796 const struct GNUNET_PeerIdentity *put_path,
3797 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3798 size_t size, const void *data)
3800 struct MeshPeerInfo *peer = cls;
3801 struct MeshPeerPath *p;
3802 struct GNUNET_PeerIdentity pi;
3805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3806 GNUNET_PEER_resolve (peer->id, &pi);
3807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3809 p = path_build_from_dht (get_path, get_path_length,
3810 put_path, put_path_length);
3811 path_add_to_peers (p, GNUNET_NO);
3813 for (i = 0; i < peer->ntunnels; i++)
3815 peer_connect (peer, peer->tunnels[i]); // FIXME add if
3822 /******************************************************************************/
3823 /********************* MESH LOCAL HANDLES **************************/
3824 /******************************************************************************/
3828 * Handler for client disconnection
3830 * @param cls closure
3831 * @param client identification of the client; NULL
3832 * for the last call when the server is destroyed
3835 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3837 struct MeshClient *c;
3838 struct MeshClient *next;
3840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3850 if (c->handle != client)
3852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3858 GNUNET_SERVER_client_drop (c->handle);
3859 c->shutting_down = GNUNET_YES;
3860 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3861 &tunnel_destroy_iterator, c);
3862 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3863 &tunnel_destroy_iterator, c);
3864 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3865 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3867 if (NULL != c->types)
3868 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3870 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
3871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3873 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
3876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
3882 * Handler for new clients
3884 * @param cls closure
3885 * @param client identification of the client
3886 * @param message the actual message, which includes messages the client wants
3889 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3890 const struct GNUNET_MessageHeader *message)
3892 struct GNUNET_MESH_ClientConnect *cc_msg;
3893 struct MeshClient *c;
3899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3901 /* Check data sanity */
3902 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3903 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3904 ntypes = ntohs (cc_msg->types);
3905 if (size != ntypes * sizeof (uint16_t))
3908 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3912 /* Create new client structure */
3913 c = GNUNET_malloc (sizeof (struct MeshClient));
3914 c->id = next_client_id++; // overflow not important: just for debug
3915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
3916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u types\n", ntypes);
3918 GNUNET_SERVER_client_keep (client);
3922 struct GNUNET_HashCode hc;
3924 t = (uint16_t *) &cc_msg[1];
3925 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
3926 for (i = 0; i < ntypes; i++)
3929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
3930 GMC_hash32 ((uint32_t) u16, &hc);
3932 /* store in clients hashmap */
3933 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3934 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3935 /* store in global hashmap */
3936 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3937 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3941 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
3942 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
3943 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
3944 GNUNET_SERVER_notification_context_add (nc, client);
3945 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
3947 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3953 * Handler for requests of new tunnels
3955 * @param cls Closure.
3956 * @param client Identification of the client.
3957 * @param message The actual message.
3960 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3961 const struct GNUNET_MessageHeader *message)
3963 struct GNUNET_MESH_TunnelMessage *t_msg;
3964 struct MeshPeerInfo *peer_info;
3965 struct MeshTunnel *t;
3966 struct MeshClient *c;
3967 MESH_TunnelNumber tid;
3969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3971 /* Sanity check for client registration */
3972 if (NULL == (c = client_get (client)))
3975 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3980 /* Message sanity check */
3981 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3984 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3988 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
3990 GNUNET_i2s (&t_msg->peer));
3991 /* Sanity check for tunnel numbering */
3992 tid = ntohl (t_msg->tunnel_id);
3993 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3999 /* Sanity check for duplicate tunnel IDs */
4000 if (NULL != tunnel_get_by_local_id (c, tid))
4003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4007 while (NULL != tunnel_get_by_pi (myid, next_tid))
4008 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4009 t = tunnel_new (myid, next_tid, c, tid);
4010 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
4018 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4020 peer_info = peer_get (&t_msg->peer);
4021 GNUNET_array_append (peer_info->tunnels, peer_info->ntunnels, t);
4022 peer_connect (peer_info, t);
4023 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4029 * Handler for requests of deleting tunnels
4031 * @param cls closure
4032 * @param client identification of the client
4033 * @param message the actual message
4036 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4037 const struct GNUNET_MessageHeader *message)
4039 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4040 struct MeshClient *c;
4041 struct MeshTunnel *t;
4042 MESH_TunnelNumber tid;
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4045 "Got a DESTROY TUNNEL from client!\n");
4047 /* Sanity check for client registration */
4048 if (NULL == (c = client_get (client)))
4051 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4056 /* Message sanity check */
4057 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4064 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4066 /* Retrieve tunnel */
4067 tid = ntohl (tunnel_msg->tunnel_id);
4068 t = tunnel_get_by_local_id(c, tid);
4071 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4078 tunnel_destroy_empty (t);
4079 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4082 send_client_tunnel_destroy (t);
4083 client_delete_tunnel (c, t);
4085 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4087 tunnel_send_destroy (t);
4088 peer_remove_tunnel (peer_get_short(t->dest), t);
4089 t->destroy = GNUNET_YES;
4090 /* The tunnel will be destroyed when the last message is transmitted. */
4091 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4097 * Handler for requests of seeting tunnel's buffering policy.
4099 * @param cls Closure (unused).
4100 * @param client Identification of the client.
4101 * @param message The actual message.
4104 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
4105 const struct GNUNET_MessageHeader *message)
4107 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4108 struct MeshClient *c;
4109 struct MeshTunnel *t;
4110 MESH_TunnelNumber tid;
4112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4113 "Got a BUFFER request from client!\n");
4115 /* Sanity check for client registration */
4116 if (NULL == (c = client_get (client)))
4119 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4124 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4126 /* Retrieve tunnel */
4127 tid = ntohl (tunnel_msg->tunnel_id);
4128 t = tunnel_get_by_local_id(c, tid);
4131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4133 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4137 switch (ntohs(message->type))
4139 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
4140 t->nobuffer = GNUNET_NO;
4142 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
4143 t->nobuffer = GNUNET_YES;
4149 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4154 * Handler for client traffic directed to one peer
4156 * @param cls closure
4157 * @param client identification of the client
4158 * @param message the actual message
4161 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4162 const struct GNUNET_MessageHeader *message)
4164 struct MeshClient *c;
4165 struct MeshTunnel *t;
4166 struct GNUNET_MESH_Unicast *data_msg;
4167 MESH_TunnelNumber tid;
4170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4171 "Got a unicast request from a client!\n");
4173 /* Sanity check for client registration */
4174 if (NULL == (c = client_get (client)))
4177 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4182 data_msg = (struct GNUNET_MESH_Unicast *) message;
4184 /* Sanity check for message size */
4185 size = ntohs (message->size);
4186 if (sizeof (struct GNUNET_MESH_Unicast) +
4187 sizeof (struct GNUNET_MessageHeader) > size)
4190 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4194 /* Tunnel exists? */
4195 tid = ntohl (data_msg->tid);
4196 t = tunnel_get_by_local_id (c, tid);
4200 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4204 /* Is it a local tunnel? Then, does client own the tunnel? */
4205 if (t->owner->handle != client)
4208 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4212 /* PID should be as expected: client<->service communication */
4213 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4216 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4217 "Unicast PID, expected %u, got %u\n",
4218 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4219 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4223 /* Ok, everything is correct, send the message
4224 * (pretend we got it from a mesh peer)
4227 /* Work around const limitation */
4228 char buf[ntohs (message->size)] GNUNET_ALIGN;
4229 struct GNUNET_MESH_Unicast *copy;
4231 copy = (struct GNUNET_MESH_Unicast *) buf;
4232 memcpy (buf, data_msg, size);
4233 copy->oid = my_full_id;
4234 copy->tid = htonl (t->id.tid);
4235 copy->ttl = htonl (default_ttl);
4236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4237 " calling generic handler...\n");
4238 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
4240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4241 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4248 * Handler for client traffic directed to the origin
4250 * @param cls closure
4251 * @param client identification of the client
4252 * @param message the actual message
4255 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4256 const struct GNUNET_MessageHeader *message)
4258 struct GNUNET_MESH_ToOrigin *data_msg;
4259 struct MeshFlowControl *fc;
4260 struct MeshClient *c;
4261 struct MeshTunnel *t;
4262 MESH_TunnelNumber tid;
4265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4266 "Got a ToOrigin request from a client!\n");
4267 /* Sanity check for client registration */
4268 if (NULL == (c = client_get (client)))
4271 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4276 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4278 /* Sanity check for message size */
4279 size = ntohs (message->size);
4280 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4281 sizeof (struct GNUNET_MessageHeader) > size)
4284 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4288 /* Tunnel exists? */
4289 tid = ntohl (data_msg->tid);
4290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4291 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4294 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4297 t = tunnel_get_by_local_id (c, tid);
4300 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4301 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4307 /* It should be sent by someone who has this as incoming tunnel. */
4311 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4315 /* PID should be as expected */
4317 if (ntohl (data_msg->pid) != fc->last_pid_recv + 1)
4320 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4321 "To Origin PID, expected %u, got %u\n",
4322 fc->last_pid_recv + 1,
4323 ntohl (data_msg->pid));
4324 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4327 fc->last_pid_recv++;
4329 /* Ok, everything is correct, send the message
4330 * (pretend we got it from a mesh peer)
4333 char buf[ntohs (message->size)] GNUNET_ALIGN;
4334 struct GNUNET_MESH_ToOrigin *copy;
4336 /* Work around 'const' limitation */
4337 memcpy (buf, data_msg, size);
4338 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4339 GNUNET_PEER_resolve (t->id.oid, ©->oid);
4340 copy->tid = htonl (t->id.tid);
4341 copy->ttl = htonl (default_ttl);
4342 copy->pid = htonl (t->prev_fc.last_pid_sent + 1);
4344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4345 " calling generic handler...\n");
4346 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
4348 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4355 * Handler for client's ACKs for payload traffic.
4357 * @param cls Closure (unused).
4358 * @param client Identification of the client.
4359 * @param message The actual message.
4362 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4363 const struct GNUNET_MessageHeader *message)
4365 struct GNUNET_MESH_LocalAck *msg;
4366 struct MeshTunnel *t;
4367 struct MeshClient *c;
4368 MESH_TunnelNumber tid;
4371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4372 /* Sanity check for client registration */
4373 if (NULL == (c = client_get (client)))
4376 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4381 msg = (struct GNUNET_MESH_LocalAck *) message;
4383 /* Tunnel exists? */
4384 tid = ntohl (msg->tunnel_id);
4385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4386 t = tunnel_get_by_local_id (c, tid);
4390 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4391 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4392 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4396 ack = ntohl (msg->max_pid);
4397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4399 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4402 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4403 t->prev_fc.last_ack_recv = ack;
4404 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4408 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4409 t->next_fc.last_ack_recv = ack;
4410 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4413 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4421 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4423 * @param cls Closure (client handle).
4424 * @param key Key (hashed tunnel ID, unused).
4425 * @param value Tunnel info.
4427 * @return GNUNET_YES, to keep iterating.
4430 monitor_all_tunnels_iterator (void *cls,
4431 const struct GNUNET_HashCode * key,
4434 struct GNUNET_SERVER_Client *client = cls;
4435 struct MeshTunnel *t = value;
4436 struct GNUNET_MESH_LocalMonitor *msg;
4438 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4439 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4440 msg->tunnel_id = htonl (t->id.tid);
4441 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4442 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4443 GNUNET_PEER_resolve (t->dest, &msg->destination);
4445 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4446 "* sending info about tunnel %s [%u]\n",
4447 GNUNET_i2s (&msg->owner), t->id.tid);
4449 GNUNET_SERVER_notification_context_unicast (nc, client,
4450 &msg->header, GNUNET_NO);
4456 * Handler for client's MONITOR request.
4458 * @param cls Closure (unused).
4459 * @param client Identification of the client.
4460 * @param message The actual message.
4463 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4464 const struct GNUNET_MessageHeader *message)
4466 struct MeshClient *c;
4468 /* Sanity check for client registration */
4469 if (NULL == (c = client_get (client)))
4472 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4476 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4477 "Received get tunnels request from client %u\n",
4479 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4480 monitor_all_tunnels_iterator,
4482 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4483 "Get tunnels request from client %u completed\n",
4485 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4490 * Handler for client's MONITOR_TUNNEL request.
4492 * @param cls Closure (unused).
4493 * @param client Identification of the client.
4494 * @param message The actual message.
4497 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4498 const struct GNUNET_MessageHeader *message)
4500 const struct GNUNET_MESH_LocalMonitor *msg;
4501 struct GNUNET_MESH_LocalMonitor *resp;
4502 struct MeshClient *c;
4503 struct MeshTunnel *t;
4505 /* Sanity check for client registration */
4506 if (NULL == (c = client_get (client)))
4509 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4513 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4514 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4515 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4518 ntohl (msg->tunnel_id));
4519 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4522 /* We don't know the tunnel FIXME */
4523 struct GNUNET_MESH_LocalMonitor warn;
4526 GNUNET_SERVER_notification_context_unicast (nc, client,
4529 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4533 /* Initialize context */
4534 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4536 GNUNET_PEER_resolve (t->dest, &resp->destination);
4537 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4538 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4539 &resp->header, GNUNET_NO);
4542 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4543 "Monitor tunnel request from client %u completed\n",
4545 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4550 * Functions to handle messages from clients
4552 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4553 {&handle_local_new_client, NULL,
4554 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4555 {&handle_local_tunnel_create, NULL,
4556 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4557 sizeof (struct GNUNET_MESH_TunnelMessage)},
4558 {&handle_local_tunnel_destroy, NULL,
4559 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4560 sizeof (struct GNUNET_MESH_TunnelMessage)},
4561 {&handle_local_tunnel_buffer, NULL,
4562 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
4563 sizeof (struct GNUNET_MESH_TunnelMessage)},
4564 {&handle_local_tunnel_buffer, NULL,
4565 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
4566 sizeof (struct GNUNET_MESH_TunnelMessage)},
4567 {&handle_local_unicast, NULL,
4568 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4569 {&handle_local_to_origin, NULL,
4570 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4571 {&handle_local_ack, NULL,
4572 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4573 sizeof (struct GNUNET_MESH_LocalAck)},
4574 {&handle_local_get_tunnels, NULL,
4575 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4576 sizeof (struct GNUNET_MessageHeader)},
4577 {&handle_local_show_tunnel, NULL,
4578 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4579 sizeof (struct GNUNET_MESH_LocalMonitor)},
4585 * To be called on core init/fail.
4587 * @param cls service closure
4588 * @param server handle to the server for this service
4589 * @param identity the public identity of this peer
4592 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4593 const struct GNUNET_PeerIdentity *identity)
4596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4597 core_handle = server;
4598 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4601 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4602 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4604 GNUNET_i2s (identity));
4605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4607 GNUNET_i2s (&my_full_id));
4608 GNUNET_SCHEDULER_shutdown (); // Try gracefully
4610 GNUNET_abort(); // Try harder
4617 * Method called whenever a given peer connects.
4619 * @param cls closure
4620 * @param peer peer identity this notification is about
4623 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4625 struct MeshPeerInfo *peer_info;
4626 struct MeshPeerPath *path;
4628 DEBUG_CONN ("Peer connected\n");
4629 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4630 peer_info = peer_get (peer);
4631 if (myid == peer_info->id)
4633 DEBUG_CONN (" (self)\n");
4638 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4640 path = path_new (2);
4641 path->peers[0] = myid;
4642 path->peers[1] = peer_info->id;
4643 GNUNET_PEER_change_rc (myid, 1);
4644 GNUNET_PEER_change_rc (peer_info->id, 1);
4645 peer_info_add_path (peer_info, path, GNUNET_YES);
4646 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4652 * Method called whenever a peer disconnects.
4654 * @param cls closure
4655 * @param peer peer identity this notification is about
4658 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4660 struct MeshPeerInfo *pi;
4661 struct MeshPeerQueue *q;
4662 struct MeshPeerQueue *n;
4664 DEBUG_CONN ("Peer disconnected\n");
4665 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4675 /* TODO try to reroute this traffic instead */
4676 queue_destroy(q, GNUNET_YES);
4679 if (NULL != pi->core_transmit)
4681 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4682 pi->core_transmit = NULL;
4684 peer_remove_path (pi, pi->id, myid);
4687 DEBUG_CONN (" (self)\n");
4689 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
4694 /******************************************************************************/
4695 /************************ MAIN FUNCTIONS ****************************/
4696 /******************************************************************************/
4699 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4701 * @param cls closure
4702 * @param key current key code
4703 * @param value value in the hash map
4704 * @return GNUNET_YES if we should continue to iterate,
4708 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
4710 struct MeshTunnel *t = value;
4717 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4719 * @param cls closure
4720 * @param key current key code
4721 * @param value value in the hash map
4722 * @return GNUNET_YES if we should continue to iterate,
4726 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
4728 struct MeshPeerInfo *p = value;
4729 struct MeshPeerQueue *q;
4730 struct MeshPeerQueue *n;
4738 queue_destroy(q, GNUNET_YES);
4742 peer_info_destroy (p);
4748 * Task run during shutdown.
4754 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4758 if (core_handle != NULL)
4760 GNUNET_CORE_disconnect (core_handle);
4765 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
4768 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4769 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4770 if (dht_handle != NULL)
4772 GNUNET_DHT_disconnect (dht_handle);
4777 GNUNET_SERVER_notification_context_destroy (nc);
4780 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4782 GNUNET_SCHEDULER_cancel (announce_id_task);
4783 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4790 * Callback for hostkey read/generation.
4792 * @param cls Closure (Configuration handle).
4793 * @param pk The ECC private key.
4794 * @param emsg Error message, if any.
4797 key_generation_cb (void *cls,
4798 struct GNUNET_CRYPTO_EccPrivateKey *pk,
4801 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4802 struct MeshPeerInfo *peer;
4803 struct MeshPeerPath *p;
4808 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4809 _("Mesh service could not access hostkey: %s. Exiting.\n"),
4811 GNUNET_SCHEDULER_shutdown ();
4814 my_private_key = pk;
4815 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
4816 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4817 &my_full_id.hashPubKey);
4818 myid = GNUNET_PEER_intern (&my_full_id);
4819 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4820 "Mesh for peer [%s] starting\n",
4821 GNUNET_i2s(&my_full_id));
4823 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4824 NULL, /* Closure passed to MESH functions */
4825 &core_init, /* Call core_init once connected */
4826 &core_connect, /* Handle connects */
4827 &core_disconnect, /* remove peers on disconnects */
4828 NULL, /* Don't notify about all incoming messages */
4829 GNUNET_NO, /* For header only in notification */
4830 NULL, /* Don't notify about all outbound messages */
4831 GNUNET_NO, /* For header-only out notification */
4832 core_handlers); /* Register these handlers */
4834 if (core_handle == NULL)
4837 GNUNET_SCHEDULER_shutdown ();
4842 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4845 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4846 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
4847 GNUNET_SERVER_disconnect_notify (server_handle,
4848 &handle_local_client_disconnect, NULL);
4851 clients_head = NULL;
4852 clients_tail = NULL;
4855 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4857 /* Create a peer_info for the local peer */
4858 peer = peer_get (&my_full_id);
4861 GNUNET_PEER_change_rc (myid, 1);
4862 peer_info_add_path (peer, p, GNUNET_YES);
4863 GNUNET_SERVER_resume (server_handle);
4864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
4869 * Process mesh requests.
4871 * @param cls closure
4872 * @param server the initialized server
4873 * @param c configuration to use
4876 run (void *cls, struct GNUNET_SERVER_Handle *server,
4877 const struct GNUNET_CONFIGURATION_Handle *c)
4881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4882 server_handle = server;
4885 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
4888 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4890 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4891 "mesh", "peer/privatekey");
4892 GNUNET_SCHEDULER_shutdown ();
4897 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
4898 &refresh_path_time))
4900 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4902 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4903 "mesh", "refresh path time");
4904 GNUNET_SCHEDULER_shutdown ();
4909 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
4912 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4914 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4915 "mesh", "id announce time");
4916 GNUNET_SCHEDULER_shutdown ();
4921 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
4924 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4926 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4927 "mesh", "connect timeout");
4928 GNUNET_SCHEDULER_shutdown ();
4933 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
4936 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4938 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4939 "mesh", "max msgs queue");
4940 GNUNET_SCHEDULER_shutdown ();
4945 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
4948 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4950 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4951 "mesh", "max tunnels");
4952 GNUNET_SCHEDULER_shutdown ();
4957 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
4960 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4962 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4963 "mesh", "default ttl", 64);
4968 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
4971 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4972 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4973 "mesh", "max peers", 1000);
4978 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
4979 &dht_replication_level))
4981 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4983 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4984 "mesh", "dht replication level", 3);
4985 dht_replication_level = 3;
4988 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4989 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4990 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4991 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4993 dht_handle = GNUNET_DHT_connect (c, 64);
4994 if (NULL == dht_handle)
4998 stats = GNUNET_STATISTICS_create ("mesh", c);
5000 GNUNET_SERVER_suspend (server_handle);
5001 /* Scheduled the task to clean up when shutdown is called */
5002 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5004 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
5007 GNUNET_free (keyfile);
5012 * The main function for the mesh service.
5014 * @param argc number of arguments from the command line
5015 * @param argv command line arguments
5016 * @return 0 ok, 1 on error
5019 main (int argc, char *const *argv)
5024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5025 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5027 ret = (GNUNET_OK == r) ? 0 : 1;
5028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5032 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5033 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5034 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);