2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh-new.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
27 * - relay corking down to core
28 * - set ttl relative to path length
31 * - keep queues until receiving ACK
37 #include "mesh2_protocol.h"
38 #include "mesh_path.h"
39 #include "block_mesh.h"
40 #include "gnunet_dht_service.h"
41 #include "gnunet_statistics_service.h"
43 #define MESH_BLOOM_SIZE 128
45 #define MESH_DEBUG_DHT GNUNET_NO
46 #define MESH_DEBUG_CONNECTION GNUNET_NO
47 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
49 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
50 GNUNET_TIME_UNIT_MINUTES,\
53 #if MESH_DEBUG_CONNECTION
54 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
56 #define DEBUG_CONN(...)
60 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
62 #define DEBUG_DHT(...)
69 struct timespec __mesh_start;
70 struct timespec __mesh_end;
71 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
72 #define INTERVAL_END \
74 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
75 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
76 if (__diff < 0) __diff += 1000000000;\
80 #define INTERVAL_SHOW \
82 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
84 #define INTERVAL_START
89 /******************************************************************************/
90 /************************ DATA STRUCTURES ****************************/
91 /******************************************************************************/
93 /** FWD declaration */
99 * Struct containing info about a queued transmission to this peer
106 struct MeshPeerQueue *next;
111 struct MeshPeerQueue *prev;
114 * Peer this transmission is directed to.
116 struct MeshPeerInfo *peer;
119 * Tunnel this message belongs to.
121 struct MeshTunnel *tunnel;
124 * Pointer to info stucture used as cls.
134 * Size of the message
141 * Struct containing all information regarding a given peer
151 * Last time we heard from this peer
153 struct GNUNET_TIME_Absolute last_contact;
156 * Number of attempts to reconnect so far
158 int n_reconnect_attempts;
161 * Paths to reach the peer, ordered by ascending hop count
163 struct MeshPeerPath *path_head;
166 * Paths to reach the peer, ordered by ascending hop count
168 struct MeshPeerPath *path_tail;
171 * Handle to stop the DHT search for a path to this peer
173 struct GNUNET_DHT_GetHandle *dhtget;
176 * Array of tunnels this peer is the target of.
177 * Most probably a small amount, therefore not a hashmap.
178 * When the path to the peer changes, notify these tunnels to let them
179 * re-adjust their path trees.
181 struct MeshTunnel **tunnels;
184 * Number of tunnels this peers participates in
186 unsigned int ntunnels;
189 * Transmission queue to core DLL head
191 struct MeshPeerQueue *queue_head;
194 * Transmission queue to core DLL tail
196 struct MeshPeerQueue *queue_tail;
199 * How many messages are in the queue to this peer.
201 unsigned int queue_n;
204 * Handle for queued transmissions
206 struct GNUNET_CORE_TransmitHandle *core_transmit;
211 * Struct to encapsulate all the Flow Control information to a peer in the
212 * context of a tunnel: Same peer in different tunnels will have independent
213 * flow control structures, allowing to choke/free tunnels according to its
216 struct MeshFlowControl
219 * ID of the last packet sent towards the peer.
221 uint32_t last_pid_sent;
224 * ID of the last packet received from the peer.
226 uint32_t last_pid_recv;
229 * Last ACK sent to the peer (peer can't send more than this PID).
231 uint32_t last_ack_sent;
234 * Last ACK sent towards the origin (for traffic towards leaf node).
236 uint32_t last_ack_recv;
239 * How many messages are in the queue towards this peer.
244 * Task to poll the peer in case of a lost ACK causes stall.
246 GNUNET_SCHEDULER_TaskIdentifier poll_task;
249 * How frequently to poll for ACKs.
251 struct GNUNET_TIME_Relative poll_time;
254 * On which tunnel to poll.
255 * Using an explicit poll_ctx would not help memory wise,
256 * since the allocated context would have to be stored in the
257 * fc struct in order to free it upon cancelling poll_task.
259 struct MeshTunnel *t;
264 * Globally unique tunnel identification (owner + number)
265 * DO NOT USE OVER THE NETWORK
270 * Node that owns the tunnel
275 * Tunnel number to differentiate all the tunnels owned by the node oid
276 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
278 MESH_TunnelNumber tid;
283 * Struct containing all information regarding a tunnel
284 * For an intermediate node the improtant info used will be:
285 * - id Tunnel unique identification
286 * - paths[0] To know where to send it next
287 * - metainfo: ready, speeds, accounting
294 struct MESH_TunnelID id;
297 * Port of the tunnel.
302 * State of the tunnel.
304 enum MeshTunnelState state;
307 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
309 MESH_TunnelNumber local_tid;
312 * Local tunnel number for local destination clients (incoming number)
313 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
316 MESH_TunnelNumber local_tid_dest;
319 * Is the tunnel bufferless (minimum latency)?
324 * Force sending ACK? Flag to allow duplicate ACK on POLL.
329 * How many messages do we accept in the forward queue.
331 unsigned int queue_max;
334 * Last time the tunnel was used
336 struct GNUNET_TIME_Absolute timestamp;
339 * Destination of the tunnel.
344 * Next hop in the tunnel. If 0, @c client must be set.
346 GNUNET_PEER_Id next_hop;
349 * Previous hop in the tunnel. If 0, @c owner must be set.
351 GNUNET_PEER_Id prev_hop;
354 * Flow control information about @c next_hop or @c client.
356 struct MeshFlowControl next_fc;
359 * Flow control information about @c prev_hop or @c owner.
361 struct MeshFlowControl prev_fc;
364 * Client owner of the tunnel, if any
366 struct MeshClient *owner;
369 * Client destination of the tunnel, if any.
371 struct MeshClient *client;
374 * Task to keep the used paths alive at the owner,
375 * time tunnel out on all the other peers.
377 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
380 * Path being used for the tunnel.
382 struct MeshPeerPath *path;
385 * Flag to signal the destruction of the tunnel.
386 * If this is set GNUNET_YES the tunnel will be destroyed
387 * when the queue is empty.
392 * Total messages pending for this tunnels, payload or not.
394 unsigned int pending_messages;
399 * Struct containing information about a client of the service
401 * TODO: add a list of 'waiting' ports
408 struct MeshClient *next;
413 struct MeshClient *prev;
416 * Tunnels that belong to this client, indexed by local id
418 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
421 * Tunnels this client has accepted, indexed by incoming local id
423 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
426 * Handle to communicate with the client
428 struct GNUNET_SERVER_Client *handle;
431 * Ports that this client has declared interest in.
432 * Indexed by a GMC_hash32 (type), contains *Client.
434 struct GNUNET_CONTAINER_MultiHashMap *ports;
437 * Whether the client is active or shutting down (don't send confirmations
438 * to a client that is shutting down.
443 * ID of the client, mainly for debug messages
450 /******************************************************************************/
451 /************************ DEBUG FUNCTIONS ****************************/
452 /******************************************************************************/
456 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
457 * @param cls string to print
458 * @param success GNUNET_OK if the PUT was transmitted,
459 * GNUNET_NO on timeout,
460 * GNUNET_SYSERR on disconnect from service
461 * after the PUT message was transmitted
462 * (so we don't know if it was received or not)
467 mesh_debug (void *cls, int success)
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
476 unsigned int debug_fwd_ack;
477 unsigned int debug_bck_ack;
481 /******************************************************************************/
482 /*********************** GLOBAL VARIABLES ****************************/
483 /******************************************************************************/
485 /************************** Configuration parameters **************************/
488 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
490 static struct GNUNET_TIME_Relative refresh_path_time;
493 * How often to PUT own ID in the DHT.
495 static struct GNUNET_TIME_Relative id_announce_time;
498 * Maximum time allowed to connect to a peer found by string.
500 static struct GNUNET_TIME_Relative connect_timeout;
503 * Default TTL for payload packets.
505 static unsigned long long default_ttl;
508 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
510 static unsigned long long dht_replication_level;
513 * How many tunnels are we willing to maintain.
514 * Local tunnels are always allowed, even if there are more tunnels than max.
516 static unsigned long long max_tunnels;
519 * How many messages *in total* are we willing to queue, divided by number of
520 * tunnels to get tunnel queue size.
522 static unsigned long long max_msgs_queue;
525 * How many peers do we want to remember?
527 static unsigned long long max_peers;
530 /*************************** Static global variables **************************/
533 * Hostkey generation context
535 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
538 * DLL with all the clients, head.
540 static struct MeshClient *clients_head;
543 * DLL with all the clients, tail.
545 static struct MeshClient *clients_tail;
548 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
550 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
553 * Number of tunnels known.
555 static unsigned long long n_tunnels;
558 * Tunnels incoming, indexed by MESH_TunnelNumber
559 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
561 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
564 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
566 static struct GNUNET_CONTAINER_MultiHashMap *peers;
569 * Handle to communicate with transport
571 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
574 * Handle to communicate with core.
576 static struct GNUNET_CORE_Handle *core_handle;
581 static struct GNUNET_DHT_Handle *dht_handle;
586 static struct GNUNET_SERVER_Handle *server_handle;
589 * Handle to the statistics service.
591 static struct GNUNET_STATISTICS_Handle *stats;
594 * Notification context, to send messages to local clients.
596 static struct GNUNET_SERVER_NotificationContext *nc;
599 * Local peer own ID (memory efficient handle).
601 static GNUNET_PEER_Id myid;
604 * Local peer own ID (full value).
606 static struct GNUNET_PeerIdentity my_full_id;
611 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
616 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
619 * Tunnel ID for the next created tunnel (global tunnel number).
621 static MESH_TunnelNumber next_tid;
624 * Tunnel ID for the next incoming tunnel (local tunnel number).
626 static MESH_TunnelNumber next_local_tid;
629 * All ports clients of this peer have opened.
631 static struct GNUNET_CONTAINER_MultiHashMap *ports;
634 * Task to periodically announce itself in the network.
636 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
639 * Next ID to assign to a client.
641 unsigned int next_client_id;
644 /******************************************************************************/
645 /*********************** DECLARATIONS **************************/
646 /******************************************************************************/
649 * Function to process paths received for a new peer addition. The recorded
650 * paths form the initial tunnel, which can be optimized later.
651 * Called on each result obtained for the DHT search.
654 * @param exp when will this value expire
655 * @param key key of the result
656 * @param type type of the result
657 * @param size number of bytes in data
658 * @param data pointer to the result data
661 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
662 const struct GNUNET_HashCode * key,
663 const struct GNUNET_PeerIdentity *get_path,
664 unsigned int get_path_length,
665 const struct GNUNET_PeerIdentity *put_path,
666 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
667 size_t size, const void *data);
671 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
672 * and insert it in the appropiate structures if the peer is not known yet.
674 * @param peer Full identity of the peer.
676 * @return Existing or newly created peer info.
678 static struct MeshPeerInfo *
679 peer_get (const struct GNUNET_PeerIdentity *peer);
683 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
684 * and insert it in the appropiate structures if the peer is not known yet.
686 * @param peer Short identity of the peer.
688 * @return Existing or newly created peer info.
690 static struct MeshPeerInfo *
691 peer_get_short (const GNUNET_PEER_Id peer);
695 * Build a PeerPath from the paths returned from the DHT, reversing the paths
696 * to obtain a local peer -> destination path and interning the peer ids.
698 * @return Newly allocated and created path
700 static struct MeshPeerPath *
701 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
702 unsigned int get_path_length,
703 const struct GNUNET_PeerIdentity *put_path,
704 unsigned int put_path_length);
708 * Adds a path to the peer_infos of all the peers in the path
710 * @param p Path to process.
711 * @param confirmed Whether we know if the path works or not.
714 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
719 * Search for a tunnel by global ID using full PeerIdentities.
721 * @param oid owner of the tunnel.
722 * @param tid global tunnel number.
724 * @return tunnel handler, NULL if doesn't exist.
726 static struct MeshTunnel *
727 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
731 * Notify a tunnel that a connection has broken that affects at least
734 * @param t Tunnel affected.
735 * @param p1 Peer that got disconnected from p2.
736 * @param p2 Peer that got disconnected from p1.
738 * @return Short ID of the peer disconnected (either p1 or p2).
739 * 0 if the tunnel remained unaffected.
741 static GNUNET_PEER_Id
742 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
747 * @brief Use the given path for the tunnel.
748 * Update the next and prev hops (and RCs).
749 * (Re)start the path refresh in case the tunnel is locally owned.
751 * @param t Tunnel to update.
752 * @param p Path to use.
755 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
758 * @brief Queue and pass message to core when possible.
760 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
761 * and accounts for it. In case the queue is full, the message is dropped and
764 * Otherwise, message is treated as internal and allowed to go regardless of
767 * @param cls Closure (@c type dependant). It will be used by queue_send to
768 * build the message to be sent if not already prebuilt.
769 * @param type Type of the message, 0 for a raw message.
770 * @param size Size of the message.
771 * @param dst Neighbor to send message to.
772 * @param t Tunnel this message belongs to.
775 queue_add (void *cls, uint16_t type, size_t size,
776 struct MeshPeerInfo *dst, struct MeshTunnel *t);
780 * Free a transmission that was already queued with all resources
781 * associated to the request.
783 * @param queue Queue handler to cancel.
784 * @param clear_cls Is it necessary to free associated cls?
787 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
791 * @brief Get the next transmittable message from the queue.
793 * This will be the head, except in the case of being a data packet
794 * not allowed by the destination peer.
796 * @param peer Destination peer.
798 * @return The next viable MeshPeerQueue element to send to that peer.
799 * NULL when there are no transmittable messages.
801 struct MeshPeerQueue *
802 queue_get_next (const struct MeshPeerInfo *peer);
806 * Core callback to write a queued packet to core buffer
808 * @param cls Closure (peer info).
809 * @param size Number of bytes available in buf.
810 * @param buf Where the to write the message.
812 * @return number of bytes written to buf
815 queue_send (void *cls, size_t size, void *buf);
818 /******************************************************************************/
819 /************************ PERIODIC FUNCTIONS ****************************/
820 /******************************************************************************/
823 * Periodically announce self id in the DHT
826 * @param tc task context
829 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
833 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
835 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
839 * - Set data expiration in function of X
842 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
844 block.id = my_full_id;
845 block.type = htonl (0);
846 GNUNET_DHT_put (dht_handle, /* DHT handle */
847 &my_full_id.hashPubKey, /* Key to use */
848 dht_replication_level, /* Replication level */
849 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
850 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
851 sizeof (block), /* Size of the data */
852 (const char *) &block, /* Data itself */
853 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
854 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
855 NULL, /* Continuation */
856 NULL); /* Continuation closure */
858 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
862 /******************************************************************************/
863 /****************** GENERAL HELPER FUNCTIONS ************************/
864 /******************************************************************************/
868 * Check if client has registered with the service and has not disconnected
870 * @param client the client to check
872 * @return non-NULL if client exists in the global DLL
874 static struct MeshClient *
875 client_get (struct GNUNET_SERVER_Client *client)
877 struct MeshClient *c;
882 if (c->handle == client)
891 * Deletes a tunnel from a client (either owner or destination). To be used on
894 * @param c Client whose tunnel to delete.
895 * @param t Tunnel which should be deleted.
898 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
900 struct GNUNET_HashCode hash;
904 GMC_hash32 (t->local_tid, &hash);
905 GNUNET_assert (GNUNET_YES ==
906 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
910 else if (c == t->client)
912 GMC_hash32 (t->local_tid_dest, &hash);
913 GNUNET_assert (GNUNET_YES ==
914 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
925 * Notify the appropiate client that a new incoming tunnel was created.
927 * @param t Tunnel that was created.
930 send_client_tunnel_create (struct MeshTunnel *t)
932 struct GNUNET_MESH_TunnelMessage msg;
934 if (NULL == t->client)
936 msg.header.size = htons (sizeof (msg));
937 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
938 msg.tunnel_id = htonl (t->local_tid_dest);
939 msg.port = htonl (t->port);
940 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
941 &msg.header, GNUNET_NO);
946 * Notify dest client that the incoming tunnel is no longer valid.
948 * @param t Tunnel that was destroyed.
951 send_client_tunnel_destroy (struct MeshTunnel *t)
953 struct GNUNET_MESH_TunnelMessage msg;
955 if (NULL == t->client)
957 msg.header.size = htons (sizeof (msg));
958 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
959 msg.tunnel_id = htonl (t->local_tid_dest);
960 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
961 &msg.header, GNUNET_NO);
966 * Iterator over all the peers to remove the oldest not-used entry.
968 * @param cls Closure (unsued).
969 * @param key ID of the peer.
970 * @param value Peer_Info of the peer.
975 peer_info_timeout (void *cls,
976 const struct GNUNET_HashCode *key,
983 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
984 * and insert it in the appropiate structures if the peer is not known yet.
986 * @param peer Full identity of the peer.
988 * @return Existing or newly created peer info.
990 static struct MeshPeerInfo *
991 peer_get (const struct GNUNET_PeerIdentity *peer)
993 struct MeshPeerInfo *peer_info;
995 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
996 if (NULL == peer_info)
999 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1000 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1002 GNUNET_CONTAINER_multihashmap_iterate (peers,
1006 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1007 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1008 peer_info->id = GNUNET_PEER_intern (peer);
1010 peer_info->last_contact = GNUNET_TIME_absolute_get();
1017 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1018 * and insert it in the appropiate structures if the peer is not known yet.
1020 * @param peer Short identity of the peer.
1022 * @return Existing or newly created peer info.
1024 static struct MeshPeerInfo *
1025 peer_get_short (const GNUNET_PEER_Id peer)
1027 struct GNUNET_PeerIdentity id;
1029 GNUNET_PEER_resolve (peer, &id);
1030 return peer_get (&id);
1035 * Choose the best path towards a peer considering the tunnel properties.
1037 * @param peer The destination peer.
1038 * @param t The tunnel the path is for.
1040 * @return Best current known path towards the peer, if any.
1042 static struct MeshPeerPath *
1043 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1045 struct MeshPeerPath *best_p;
1046 struct MeshPeerPath *p;
1047 unsigned int best_cost;
1050 best_p = p = peer->path_head;
1051 best_cost = cost = p->length;
1054 if ((cost = p->length) < best_cost)
1066 * Remove the tunnel from the list of tunnels to which a peer is target.
1068 * @param peer PeerInfo of the peer.
1069 * @param t Tunnel to remove.
1072 peer_remove_tunnel (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1076 for (i = 0; i < peer->ntunnels; i++)
1079 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1082 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1084 GNUNET_realloc (peer->tunnels,
1085 peer->ntunnels * sizeof(struct MeshTunnel *));
1093 * Core callback to write a pre-constructed data packet to core buffer
1095 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1096 * @param size Number of bytes available in buf.
1097 * @param buf Where the to write the message.
1099 * @return number of bytes written to buf
1102 send_core_data_raw (void *cls, size_t size, void *buf)
1104 struct GNUNET_MessageHeader *msg = cls;
1107 GNUNET_assert (NULL != msg);
1108 total_size = ntohs (msg->size);
1110 if (total_size > size)
1115 memcpy (buf, msg, total_size);
1122 * Sends an already built message to a peer, properly registrating
1123 * all used resources.
1125 * @param message Message to send. Function makes a copy of it.
1126 * @param peer Short ID of the neighbor whom to send the message.
1127 * @param t Tunnel on which this message is transmitted.
1130 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1131 GNUNET_PEER_Id peer,
1132 struct MeshTunnel *t)
1134 struct GNUNET_PeerIdentity id;
1135 struct MeshPeerInfo *neighbor;
1136 struct MeshPeerPath *p;
1141 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1146 size = ntohs (message->size);
1147 data = GNUNET_malloc (size);
1148 memcpy (data, message, size);
1149 type = ntohs(message->type);
1152 struct GNUNET_MESH_Unicast *u;
1154 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1155 u = (struct GNUNET_MESH_Unicast *) data;
1156 u->ttl = htonl (ntohl (u->ttl) - 1);
1159 GNUNET_PEER_resolve (peer, &id);
1160 neighbor = peer_get (&id);
1161 for (p = neighbor->path_head; NULL != p; p = p->next)
1171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1172 " %s IS NOT DIRECTLY CONNECTED\n",
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1177 for (p = neighbor->path_head; NULL != p; p = p->next)
1179 struct GNUNET_PeerIdentity debug_id;
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 " path with %u hops through:\n",
1185 for (i = 0; i < p->length; i++)
1187 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 i, GNUNET_i2s(&debug_id));
1194 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1195 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1196 " no direct connection to %s\n",
1201 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1212 * Sends a CREATE PATH message for a path to a peer, properly registrating
1213 * all used resources.
1215 * @param t Tunnel for which the path is created.
1218 send_create_path (struct MeshTunnel *t)
1220 struct MeshPeerInfo *neighbor;
1222 neighbor = peer_get_short (t->next_hop);
1224 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1225 sizeof (struct GNUNET_MESH_CreateTunnel) +
1226 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1229 t->state = MESH_TUNNEL_WAITING;
1234 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1236 * @param t Tunnel which to confirm.
1239 send_path_ack (struct MeshTunnel *t)
1241 struct MeshPeerInfo *peer;
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 given 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 (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);
1288 t->state = MESH_TUNNEL_SEARCHING;
1290 /* Otherwise, there is no path but the DHT get is already started. */
1295 * Destroy the peer_info and free any allocated resources linked to it
1297 * @param pi The peer_info to destroy.
1299 * @return GNUNET_OK on success
1302 peer_info_destroy (struct MeshPeerInfo *pi)
1304 struct GNUNET_PeerIdentity id;
1305 struct MeshPeerPath *p;
1306 struct MeshPeerPath *nextp;
1308 GNUNET_PEER_resolve (pi->id, &id);
1309 GNUNET_PEER_change_rc (pi->id, -1);
1312 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1316 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1318 if (NULL != pi->dhtget)
1320 GNUNET_DHT_get_stop (pi->dhtget);
1326 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1336 * Remove all paths that rely on a direct connection between p1 and p2
1337 * from the peer itself and notify all tunnels about it.
1339 * @param peer PeerInfo of affected peer.
1340 * @param p1 GNUNET_PEER_Id of one peer.
1341 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1344 * TODO: optimize (see below)
1347 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1350 struct MeshPeerPath *p;
1351 struct MeshPeerPath *next;
1352 struct MeshPeerInfo *peer_d;
1354 unsigned int destroyed;
1357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1359 for (p = peer->path_head; NULL != p; p = next)
1362 for (i = 0; i < (p->length - 1); i++)
1364 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1365 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1367 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1377 for (i = 0; i < peer->ntunnels; i++)
1379 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1383 peer_d = peer_get_short (d);
1384 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1385 tunnel_use_path (peer->tunnels[i], next);
1386 peer_connect (peer_d, peer->tunnels[i]);
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1393 * Add the path to the peer and update the path used to reach it in case this
1396 * @param peer_info Destination peer to add the path to.
1397 * @param path New path to add. Last peer must be the peer in arg 1.
1398 * Path will be either used of freed if already known.
1399 * @param trusted Do we trust that this path is real?
1402 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1405 struct MeshPeerPath *aux;
1409 if ((NULL == peer_info) || (NULL == path))
1412 path_destroy (path);
1415 if (path->peers[path->length - 1] != peer_info->id)
1418 path_destroy (path);
1421 if (path->length <= 2 && GNUNET_NO == trusted)
1423 /* Only allow CORE to tell us about direct paths */
1424 path_destroy (path);
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 * Path will be either used or freed.
1492 * @param trusted Do we trust that this path is real?
1495 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1496 struct MeshPeerPath *path, int trusted)
1499 peer_info_add_path (peer_info, path, trusted);
1504 * Function called if the connection to the peer has been stalled for a while,
1505 * possibly due to a missed ACK. Poll the peer about its ACK status.
1507 * @param cls Closure (poll ctx).
1508 * @param tc TaskContext.
1511 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1513 struct MeshFlowControl *fc = cls;
1514 struct GNUNET_MESH_Poll msg;
1515 struct MeshTunnel *t = fc->t;
1516 GNUNET_PEER_Id peer;
1518 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1519 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1524 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1525 msg.header.size = htons (sizeof (msg));
1526 msg.tid = htonl (t->id.tid);
1527 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1529 if (fc == &t->prev_fc)
1533 else if (fc == &t->next_fc)
1542 send_prebuilt_message (&msg.header, peer, t);
1543 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1544 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1550 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1551 * to obtain a local peer -> destination path and interning the peer ids.
1553 * @return Newly allocated and created path
1555 static struct MeshPeerPath *
1556 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1557 unsigned int get_path_length,
1558 const struct GNUNET_PeerIdentity *put_path,
1559 unsigned int put_path_length)
1561 struct MeshPeerPath *p;
1567 GNUNET_PEER_change_rc (myid, 1);
1568 i = get_path_length;
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1570 for (i--; i >= 0; i--)
1572 id = GNUNET_PEER_intern (&get_path[i]);
1573 if (p->length > 0 && id == p->peers[p->length - 1])
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1576 GNUNET_PEER_change_rc (id, -1);
1580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1581 GNUNET_i2s (&get_path[i]));
1583 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1584 p->peers[p->length - 1] = id;
1587 i = put_path_length;
1588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1589 for (i--; i >= 0; i--)
1591 id = GNUNET_PEER_intern (&put_path[i]);
1594 /* PUT path went through us, so discard the path up until now and start
1595 * from here to get a much shorter (and loop-free) path.
1600 if (p->length > 0 && id == p->peers[p->length - 1])
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1603 GNUNET_PEER_change_rc (id, -1);
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1608 GNUNET_i2s (&put_path[i]));
1610 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1611 p->peers[p->length - 1] = id;
1615 if (get_path_length > 0)
1616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1617 GNUNET_i2s (&get_path[0]));
1618 if (put_path_length > 0)
1619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1620 GNUNET_i2s (&put_path[0]));
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1623 for (i = 0; i < p->length; i++)
1625 struct GNUNET_PeerIdentity peer_id;
1627 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1629 GNUNET_i2s (&peer_id));
1637 * Adds a path to the peer_infos of all the peers in the path
1639 * @param p Path to process.
1640 * @param confirmed Whether we know if the path works or not.
1643 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1647 /* TODO: invert and add */
1648 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1649 for (i++; i < p->length; i++)
1651 struct MeshPeerInfo *aux;
1652 struct MeshPeerPath *copy;
1654 aux = peer_get_short (p->peers[i]);
1655 copy = path_duplicate (p);
1656 copy->length = i + 1;
1657 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1663 * Send keepalive packets for a peer
1665 * @param cls Closure (tunnel for which to send the keepalive).
1666 * @param tc Notification context.
1669 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1673 * Search for a tunnel among the incoming tunnels
1675 * @param tid the local id of the tunnel
1677 * @return tunnel handler, NULL if doesn't exist
1679 static struct MeshTunnel *
1680 tunnel_get_incoming (MESH_TunnelNumber tid)
1682 struct GNUNET_HashCode hash;
1684 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1685 GMC_hash32 (tid, &hash);
1686 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1691 * Search for a tunnel among the tunnels for a client
1693 * @param c the client whose tunnels to search in
1694 * @param tid the local id of the tunnel
1696 * @return tunnel handler, NULL if doesn't exist
1698 static struct MeshTunnel *
1699 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1701 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1703 return tunnel_get_incoming (tid);
1707 struct GNUNET_HashCode hash;
1709 GMC_hash32 (tid, &hash);
1710 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1716 * Search for a tunnel by global ID using PEER_ID
1718 * @param pi owner of the tunnel
1719 * @param tid global tunnel number
1721 * @return tunnel handler, NULL if doesn't exist
1723 static struct MeshTunnel *
1724 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1726 struct MESH_TunnelID id;
1727 struct GNUNET_HashCode hash;
1732 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1733 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1738 * Search for a tunnel by global ID using full PeerIdentities
1740 * @param oid owner of the tunnel
1741 * @param tid global tunnel number
1743 * @return tunnel handler, NULL if doesn't exist
1745 static struct MeshTunnel *
1746 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1748 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1753 * Add a client to a tunnel, initializing all needed data structures.
1755 * FIXME: make static after implementing port numbers
1757 * @param t Tunnel to which add the client.
1758 * @param c Client which to add to the tunnel.
1761 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1763 if (NULL != t->client)
1768 if (0 != t->next_hop)
1778 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1780 unsigned int own_pos;
1782 for (own_pos = 0; own_pos < p->length; own_pos++)
1784 if (p->peers[own_pos] == myid)
1787 if (own_pos > p->length - 1)
1793 if (own_pos < p->length - 1)
1794 t->next_hop = p->peers[own_pos + 1];
1796 t->next_hop = p->peers[own_pos];
1797 GNUNET_PEER_change_rc (t->next_hop, 1);
1799 t->prev_hop = p->peers[own_pos - 1];
1801 t->prev_hop = p->peers[0];
1802 GNUNET_PEER_change_rc (t->prev_hop, 1);
1804 if (NULL != t->path)
1805 path_destroy (t->path);
1806 t->path = path_duplicate (p);
1809 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1810 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1811 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
1818 * Notifies a tunnel that a connection has broken that affects at least
1819 * some of its peers. Sends a notification towards the root of the tree.
1820 * In case the peer is the owner of the tree, notifies the client that owns
1821 * the tunnel and tries to reconnect.
1823 * @param t Tunnel affected.
1824 * @param p1 Peer that got disconnected from p2.
1825 * @param p2 Peer that got disconnected from p1.
1827 * @return Short ID of the peer disconnected (either p1 or p2).
1828 * 0 if the tunnel remained unaffected.
1830 static GNUNET_PEER_Id
1831 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1834 // if (myid != p1 && myid != p2) FIXME
1839 // if (tree_get_predecessor (t->tree) != 0)
1841 // /* We are the peer still connected, notify owner of the disconnection. */
1842 // struct GNUNET_MESH_PathBroken msg;
1843 // struct GNUNET_PeerIdentity neighbor;
1845 // msg.header.size = htons (sizeof (msg));
1846 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1847 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1848 // msg.tid = htonl (t->id.tid);
1849 // msg.peer1 = my_full_id;
1850 // GNUNET_PEER_resolve (pid, &msg.peer2);
1851 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1852 // send_prebuilt_message (&msg.header, &neighbor, t);
1859 * Build a local ACK message and send it to a local client.
1861 * @param t Tunnel on which to send the ACK.
1862 * @param c Client to whom send the ACK.
1863 * @param ack Value of the ACK.
1866 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
1868 struct GNUNET_MESH_LocalAck msg;
1870 msg.header.size = htons (sizeof (msg));
1871 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1872 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
1873 msg.max_pid = htonl (ack);
1874 GNUNET_SERVER_notification_context_unicast(nc,
1881 * Build an ACK message and queue it to send to the given peer.
1883 * @param t Tunnel on which to send the ACK.
1884 * @param peer Peer to whom send the ACK.
1885 * @param ack Value of the ACK.
1888 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1890 struct GNUNET_MESH_ACK msg;
1892 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1893 msg.header.size = htons (sizeof (msg));
1894 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1895 msg.pid = htonl (ack);
1896 msg.tid = htonl (t->id.tid);
1898 send_prebuilt_message (&msg.header, peer, t);
1903 * Send an ACK informing the predecessor about the available buffer space.
1904 * In case there is no predecessor, inform the owning client.
1905 * If buffering is off, send only on behalf of children or self if endpoint.
1906 * If buffering is on, send when sent to children and buffer space is free.
1907 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
1908 * the ACK itself goes "back" (towards root).
1910 * @param t Tunnel on which to send the ACK.
1911 * @param type Type of message that triggered the ACK transmission.
1914 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
1918 /* Is it after unicast retransmission? */
1921 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1923 "ACK due to FWD DATA retransmission\n");
1924 if (GNUNET_YES == t->nobuffer)
1926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
1930 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1931 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1933 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1934 t->force_ack = GNUNET_YES;
1940 /* Check if we need to transmit the ACK */
1941 if (t->queue_max > t->next_fc.queue_n * 4 &&
1942 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
1943 GNUNET_NO == t->force_ack)
1945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1947 " t->qmax: %u, t->qn: %u\n",
1948 t->queue_max, t->next_fc.queue_n);
1949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1950 " t->pid: %u, t->ack: %u\n",
1951 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
1955 /* Ok, ACK might be necessary, what PID to ACK? */
1956 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
1957 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
1959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
1963 t->prev_fc.last_ack_sent = ack;
1964 if (NULL != t->owner)
1965 send_local_ack (t, t->owner, ack);
1966 else if (0 != t->prev_hop)
1967 send_ack (t, t->prev_hop, ack);
1971 t->force_ack = GNUNET_NO;
1976 * Send an ACK informing the children node/client about the available
1978 * If buffering is off, send only on behalf of root (can be self).
1979 * If buffering is on, send when sent to predecessor and buffer space is free.
1980 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
1981 * the ACK itself goes "forward" (towards children/clients).
1983 * @param t Tunnel on which to send the ACK.
1984 * @param type Type of message that triggered the ACK transmission.
1987 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
1990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1991 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
1992 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
1993 /* Is it after data to_origin retransmission? */
1996 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1997 if (GNUNET_YES == t->nobuffer)
1999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2000 " Not sending ACK, nobuffer + traffic\n");
2004 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2005 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2007 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2008 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2009 t->force_ack = GNUNET_YES;
2015 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2017 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2019 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2022 " Not sending ACK, not needed\n");
2025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2026 " Sending BCK ACK %u (last sent: %u)\n",
2027 ack, t->next_fc.last_ack_sent);
2028 t->next_fc.last_ack_sent = ack;
2030 if (NULL != t->client)
2031 send_local_ack (t, t->client, ack);
2032 else if (0 != t->next_hop)
2033 send_ack (t, t->next_hop, ack);
2036 t->force_ack = GNUNET_NO;
2041 * @brief Re-initiate traffic to this peer if necessary.
2043 * Check if there is traffic queued towards this peer
2044 * and the core transmit handle is NULL (traffic was stalled).
2045 * If so, call core tmt rdy.
2047 * @param peer_id Short ID of peer to which initiate traffic.
2050 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2052 struct MeshPeerInfo *peer;
2053 struct GNUNET_PeerIdentity id;
2054 struct MeshPeerQueue *q;
2057 peer = peer_get_short (peer_id);
2058 if (NULL != peer->core_transmit)
2061 q = queue_get_next (peer);
2064 /* Might br multicast traffic already sent to this particular peer but
2065 * not to other children in this tunnel.
2066 * This way t->queue_n would be > 0 but the queue of this particular peer
2072 GNUNET_PEER_resolve (peer->id, &id);
2073 peer->core_transmit =
2074 GNUNET_CORE_notify_transmit_ready(core_handle,
2077 GNUNET_TIME_UNIT_FOREVER_REL,
2087 * Send a message to all peers in this tunnel that the tunnel is no longer
2090 * @param t The tunnel whose peers to notify.
2093 tunnel_send_destroy (struct MeshTunnel *t)
2095 struct GNUNET_MESH_TunnelDestroy msg;
2096 struct GNUNET_PeerIdentity id;
2098 msg.header.size = htons (sizeof (msg));
2099 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2100 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2101 msg.tid = htonl (t->id.tid);
2102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2103 " sending tunnel destroy for tunnel: %s [%X]\n",
2104 GNUNET_i2s (&msg.oid), t->id.tid);
2106 if (NULL == t->client)
2108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2109 GNUNET_PEER_resolve (t->next_hop, &id);
2110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2111 " sending forward to %s\n",
2113 send_prebuilt_message (&msg.header, t->next_hop, t);
2115 if (NULL == t->owner)
2117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2118 GNUNET_PEER_resolve (t->prev_hop, &id);
2119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2120 " sending back to %s\n",
2122 send_prebuilt_message (&msg.header, t->prev_hop, t);
2128 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2130 * @param t Tunnel which to cancel.
2131 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2134 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2136 struct MeshPeerInfo *peer_info;
2137 struct MeshPeerQueue *pq;
2138 struct MeshPeerQueue *next;
2140 peer_info = peer_get_short (neighbor);
2141 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2144 if (pq->tunnel == t)
2146 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2147 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2149 /* Should have been removed on destroy children */
2152 queue_destroy (pq, GNUNET_YES);
2155 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2157 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2158 peer_info->core_transmit = NULL;
2164 * Destroy the tunnel and free any allocated resources linked to it.
2166 * @param t the tunnel to destroy
2168 * @return GNUNET_OK on success
2171 tunnel_destroy (struct MeshTunnel *t)
2173 struct MeshClient *c;
2174 struct GNUNET_HashCode hash;
2184 struct GNUNET_PeerIdentity id;
2186 GNUNET_PEER_resolve (t->id.oid, &id);
2187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2188 GNUNET_i2s (&id), t->id.tid);
2190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2194 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2195 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2203 GMC_hash32 (t->local_tid, &hash);
2205 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2212 if (NULL != t->client)
2214 GMC_hash32 (t->local_tid_dest, &hash);
2216 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2223 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2229 peer_cancel_queues (t->next_hop, t);
2230 peer_cancel_queues (t->prev_hop, t);
2231 GNUNET_PEER_change_rc(t->next_hop, -1);
2232 GNUNET_PEER_change_rc(t->prev_hop, -1);
2234 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2235 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2238 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2244 * Tunnel is empty: destroy it.
2246 * @param t Tunnel to destroy.
2249 tunnel_destroy_empty (struct MeshTunnel *t)
2253 struct GNUNET_PeerIdentity id;
2255 GNUNET_PEER_resolve (t->id.oid, &id);
2256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2257 "executing destruction of empty tunnel %s [%X]\n",
2258 GNUNET_i2s (&id), t->id.tid);
2262 tunnel_send_destroy (t);
2263 if (0 == t->pending_messages)
2266 t->destroy = GNUNET_YES;
2270 * Initialize a Flow Control structure to the initial state.
2272 * @param fc Flow Control structure to initialize.
2275 fc_init (struct MeshFlowControl *fc)
2277 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2278 fc->last_pid_recv = (uint32_t) -1;
2279 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2280 fc->last_ack_recv = (uint32_t) -1;
2281 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2282 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2287 * Create a new tunnel
2289 * @param owner Who is the owner of the tunnel (short ID).
2290 * @param tid Tunnel Number of the tunnel.
2291 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2292 * @param local Tunnel Number for the tunnel, for the client point of view.
2294 * @return A new initialized tunnel. NULL on error.
2296 static struct MeshTunnel *
2297 tunnel_new (GNUNET_PEER_Id owner,
2298 MESH_TunnelNumber tid,
2299 struct MeshClient *client,
2300 MESH_TunnelNumber local)
2302 struct MeshTunnel *t;
2303 struct GNUNET_HashCode hash;
2305 if (n_tunnels >= max_tunnels && NULL == client)
2308 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2311 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2313 fc_init (&t->next_fc);
2314 fc_init (&t->prev_fc);
2315 t->local_tid = local;
2317 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2319 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2321 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2322 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2329 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2336 GMC_hash32 (t->local_tid, &hash);
2338 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2339 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2343 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2354 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2355 * client when the client disconnects. If the client is not the owner, the
2356 * owner will get notified if no more clients are in the tunnel and the client
2357 * get removed from the tunnel's list.
2359 * @param cls closure (client that is disconnecting)
2360 * @param key the hash of the local tunnel id (used to access the hashmap)
2361 * @param value the value stored at the key (tunnel to destroy)
2363 * @return GNUNET_OK, keep iterating.
2366 tunnel_destroy_iterator (void *cls,
2367 const struct GNUNET_HashCode * key,
2370 struct MeshTunnel *t = value;
2371 struct MeshClient *c = cls;
2373 send_client_tunnel_destroy (t);
2376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
2377 client_delete_tunnel (c, t);
2378 tunnel_destroy_empty (t);
2381 tunnel_send_destroy (t);
2383 t->destroy = GNUNET_YES;
2390 * Timeout function, destroys tunnel if called
2392 * @param cls Closure (tunnel to destroy).
2393 * @param tc TaskContext
2396 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2398 struct MeshTunnel *t = cls;
2399 struct GNUNET_PeerIdentity id;
2401 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2402 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2404 GNUNET_PEER_resolve(t->id.oid, &id);
2405 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2406 "Tunnel %s [%X] timed out. Destroying.\n",
2407 GNUNET_i2s(&id), t->id.tid);
2408 send_client_tunnel_destroy (t);
2414 * Resets the tunnel timeout. Starts it if no timeout was running.
2416 * @param t Tunnel whose timeout to reset.
2418 * TODO use heap to improve efficiency of scheduler.
2421 tunnel_reset_timeout (struct MeshTunnel *t)
2423 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2425 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2426 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2427 t->maintenance_task =
2428 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2429 (refresh_path_time, 4), &tunnel_timeout, t);
2433 /******************************************************************************/
2434 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2435 /******************************************************************************/
2438 * Function to send a create path packet to a peer.
2440 * @param cls closure
2441 * @param size number of bytes available in buf
2442 * @param buf where the callee should write the message
2443 * @return number of bytes written to buf
2446 send_core_path_create (void *cls, size_t size, void *buf)
2448 struct MeshTunnel *t = cls;
2449 struct GNUNET_MESH_CreateTunnel *msg;
2450 struct GNUNET_PeerIdentity *peer_ptr;
2451 struct MeshPeerPath *p = t->path;
2456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2458 sizeof (struct GNUNET_MESH_CreateTunnel) +
2459 p->length * sizeof (struct GNUNET_PeerIdentity);
2461 if (size < size_needed || NULL == buf)
2466 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2467 msg->header.size = htons (size_needed);
2468 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2469 msg->tid = ntohl (t->id.tid);
2472 if (GNUNET_YES == t->nobuffer)
2473 opt |= MESH_TUNNEL_OPT_NOBUFFER;
2474 msg->opt = htonl (opt);
2475 msg->port = htonl (t->port);
2477 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2478 for (i = 0; i < p->length; i++)
2480 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2484 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2490 * Creates a path ack message in buf and frees all unused resources.
2492 * @param cls closure (MeshTransmissionDescriptor)
2493 * @param size number of bytes available in buf
2494 * @param buf where the callee should write the message
2495 * @return number of bytes written to buf
2498 send_core_path_ack (void *cls, size_t size, void *buf)
2500 struct MeshTunnel *t = cls;
2501 struct GNUNET_MESH_PathACK *msg = buf;
2503 GNUNET_assert (NULL != t);
2504 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2509 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : INITIAL_WINDOW_SIZE - 1;
2510 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2511 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2512 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
2513 msg->tid = htonl (t->id.tid);
2514 msg->peer_id = my_full_id;
2515 msg->ack = htonl (t->prev_fc.last_ack_sent);
2517 /* TODO add signature */
2519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2520 return sizeof (struct GNUNET_MESH_PathACK);
2525 * Free a transmission that was already queued with all resources
2526 * associated to the request.
2528 * @param queue Queue handler to cancel.
2529 * @param clear_cls Is it necessary to free associated cls?
2532 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2534 struct MeshFlowControl *fc;
2536 if (GNUNET_YES == clear_cls)
2538 switch (queue->type)
2540 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2541 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2542 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2544 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2545 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2546 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2547 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2548 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2550 " prebuilt message\n");
2551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2553 GNUNET_MESH_DEBUG_M2S (queue->type));
2555 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2560 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2561 " type %s unknown!\n",
2562 GNUNET_MESH_DEBUG_M2S (queue->type));
2564 GNUNET_free_non_null (queue->cls);
2566 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2567 queue->peer->queue_tail,
2570 /* Delete from appropiate fc in the tunnel */
2571 if (queue->peer->id == queue->tunnel->next_hop)
2572 fc = &queue->tunnel->next_fc;
2573 else if (queue->peer->id == queue->tunnel->next_hop)
2574 fc = &queue->tunnel->next_fc;
2581 GNUNET_free (queue);
2586 * @brief Get the next transmittable message from the queue.
2588 * This will be the head, except in the case of being a data packet
2589 * not allowed by the destination peer.
2591 * @param peer Destination peer.
2593 * @return The next viable MeshPeerQueue element to send to that peer.
2594 * NULL when there are no transmittable messages.
2596 struct MeshPeerQueue *
2597 queue_get_next (const struct MeshPeerInfo *peer)
2599 struct MeshPeerQueue *q;
2601 struct GNUNET_MESH_Unicast *ucast;
2602 struct GNUNET_MESH_ToOrigin *to_orig;
2603 struct MeshTunnel* t;
2607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
2608 for (q = peer->queue_head; NULL != q; q = q->next)
2611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2613 GNUNET_MESH_DEBUG_M2S(q->type));
2616 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2617 ucast = (struct GNUNET_MESH_Unicast *) q->cls;
2618 pid = ntohl (ucast->pid);
2619 ack = t->next_fc.last_ack_recv;
2621 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2622 to_orig = (struct GNUNET_MESH_ToOrigin *) q->cls;
2623 pid = ntohl (to_orig->pid);
2624 ack = t->prev_fc.last_ack_recv;
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2632 "********* ACK: %u, PID: %u\n",
2634 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2643 "********* NEXT!\n");
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2647 "********* nothing found\n");
2653 queue_send (void *cls, size_t size, void *buf)
2655 struct MeshPeerInfo *peer = cls;
2656 struct GNUNET_MessageHeader *msg;
2657 struct MeshPeerQueue *queue;
2658 struct MeshTunnel *t;
2659 struct GNUNET_PeerIdentity dst_id;
2660 struct MeshFlowControl *fc;
2664 peer->core_transmit = NULL;
2666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
2667 queue = queue_get_next (peer);
2669 /* Queue has no internal mesh traffic nor sendable payload */
2672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
2673 if (NULL == peer->queue_head)
2674 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
2677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
2679 GNUNET_PEER_resolve (peer->id, &dst_id);
2680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2681 "********* towards %s\n",
2682 GNUNET_i2s (&dst_id));
2683 /* Check if buffer size is enough for the message */
2684 if (queue->size > size)
2686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2687 "********* not enough room, reissue\n");
2688 peer->core_transmit =
2689 GNUNET_CORE_notify_transmit_ready (core_handle,
2692 GNUNET_TIME_UNIT_FOREVER_REL,
2699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
2702 GNUNET_assert (0 < t->pending_messages);
2703 t->pending_messages--;
2707 switch (queue->type)
2710 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2711 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2712 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
2713 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
2714 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2715 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2717 "********* raw: %s\n",
2718 GNUNET_MESH_DEBUG_M2S (queue->type));
2720 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2721 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2722 data_size = send_core_data_raw (queue->cls, size, buf);
2723 msg = (struct GNUNET_MessageHeader *) buf;
2724 type = ntohs (msg->type);
2726 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
2728 data_size = send_core_path_create (queue->cls, size, buf);
2730 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
2732 data_size = send_core_path_ack (queue->cls, size, buf);
2736 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2737 "********* type unknown: %u\n",
2742 /* Free queue, but cls was freed by send_core_* */
2743 queue_destroy (queue, GNUNET_NO);
2745 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
2748 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2749 t->next_fc.last_pid_sent = ((struct GNUNET_MESH_Unicast *) buf)->pid;
2750 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
2752 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2753 t->prev_fc.last_pid_sent = ((struct GNUNET_MESH_ToOrigin *) buf)->pid;
2754 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
2760 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
2762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
2766 /* If more data in queue, send next */
2767 queue = queue_get_next (peer);
2770 struct GNUNET_PeerIdentity id;
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
2773 GNUNET_PEER_resolve (peer->id, &id);
2774 peer->core_transmit =
2775 GNUNET_CORE_notify_transmit_ready(core_handle,
2778 GNUNET_TIME_UNIT_FOREVER_REL,
2784 else if (NULL != peer->queue_head)
2786 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2787 "********* %s stalled\n",
2788 GNUNET_i2s (&my_full_id));
2789 if (peer->id == t->next_hop)
2791 else if (peer->id == t->prev_hop)
2798 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
2801 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Return %d\n", data_size);
2811 * @brief Queue and pass message to core when possible.
2813 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
2814 * accounts for it. In case the queue is full, the message is dropped and
2817 * Otherwise, message is treated as internal and allowed to go regardless of
2820 * @param cls Closure (@c type dependant). It will be used by queue_send to
2821 * build the message to be sent if not already prebuilt.
2822 * @param type Type of the message, 0 for a raw message.
2823 * @param size Size of the message.
2824 * @param dst Neighbor to send message to.
2825 * @param t Tunnel this message belongs to.
2828 queue_add (void *cls, uint16_t type, size_t size,
2829 struct MeshPeerInfo *dst, struct MeshTunnel *t)
2831 struct MeshPeerQueue *queue;
2832 struct GNUNET_PeerIdentity id;
2836 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2838 n = &t->next_fc.queue_n;
2840 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
2842 n = &t->prev_fc.queue_n;
2846 if (*n >= t->queue_max)
2849 GNUNET_STATISTICS_update(stats,
2850 "# messages dropped (buffer full)",
2852 return; /* Drop message */
2856 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2862 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
2863 if (NULL == dst->core_transmit)
2865 GNUNET_PEER_resolve (dst->id, &id);
2866 dst->core_transmit =
2867 GNUNET_CORE_notify_transmit_ready (core_handle,
2870 GNUNET_TIME_UNIT_FOREVER_REL,
2876 t->pending_messages++;
2880 /******************************************************************************/
2881 /******************** MESH NETWORK HANDLERS **************************/
2882 /******************************************************************************/
2886 * Core handler for path creation
2888 * @param cls closure
2889 * @param message message
2890 * @param peer peer identity this notification is about
2892 * @return GNUNET_OK to keep the connection open,
2893 * GNUNET_SYSERR to close it (signal serious error)
2896 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2897 const struct GNUNET_MessageHeader *message)
2899 unsigned int own_pos;
2902 MESH_TunnelNumber tid;
2903 struct GNUNET_MESH_CreateTunnel *msg;
2904 struct GNUNET_PeerIdentity *pi;
2905 struct GNUNET_HashCode hash;
2906 struct MeshPeerPath *path;
2907 struct MeshPeerInfo *dest_peer_info;
2908 struct MeshPeerInfo *orig_peer_info;
2909 struct MeshTunnel *t;
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2912 "Received a path create msg [%s]\n",
2913 GNUNET_i2s (&my_full_id));
2914 size = ntohs (message->size);
2915 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
2917 GNUNET_break_op (0);
2921 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
2922 if (size % sizeof (struct GNUNET_PeerIdentity))
2924 GNUNET_break_op (0);
2927 size /= sizeof (struct GNUNET_PeerIdentity);
2930 GNUNET_break_op (0);
2933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2934 msg = (struct GNUNET_MESH_CreateTunnel *) message;
2936 tid = ntohl (msg->tid);
2937 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2939 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
2940 t = tunnel_get (pi, tid);
2941 if (NULL == t) /* might be a local tunnel */
2945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2946 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
2952 t->port = ntohl (msg->port);
2953 opt = ntohl (msg->opt);
2954 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
2956 t->nobuffer = GNUNET_YES;
2957 t->prev_fc.last_ack_sent = t->prev_fc.last_pid_recv + 1;
2958 t->next_fc.last_ack_sent = t->next_fc.last_pid_recv + 1;
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
2963 tunnel_reset_timeout (t);
2964 GMC_hash32 (t->local_tid_dest, &hash);
2966 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2967 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2974 t->state = MESH_TUNNEL_WAITING;
2976 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2977 if (NULL == dest_peer_info)
2979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2980 " Creating PeerInfo for destination.\n");
2981 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2982 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2983 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2985 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2987 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2988 if (NULL == orig_peer_info)
2990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2991 " Creating PeerInfo for origin.\n");
2992 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2993 orig_peer_info->id = GNUNET_PEER_intern (pi);
2994 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2995 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
2998 path = path_new (size);
3000 for (i = 0; i < size; i++)
3002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3003 GNUNET_i2s (&pi[i]));
3004 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3005 if (path->peers[i] == myid)
3008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3009 if (own_pos == 0 && path->peers[own_pos] != myid)
3011 /* create path: self not found in path through self */
3012 GNUNET_break_op (0);
3013 path_destroy (path);
3017 path_add_to_peers (path, GNUNET_NO);
3018 tunnel_use_path (t, path);
3020 if (own_pos == size - 1)
3022 struct MeshClient *c;
3023 struct GNUNET_HashCode hc;
3025 /* Find target client */
3026 GMC_hash32 (t->port, &hc);
3027 c = GNUNET_CONTAINER_multihashmap_get (ports, &hc);
3030 /* TODO send reject */
3035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3036 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3039 /* Assign local tid */
3040 while (NULL != tunnel_get_incoming (next_local_tid))
3041 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3042 t->local_tid_dest = next_local_tid++;
3043 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3045 send_client_tunnel_create (t);
3050 struct MeshPeerPath *path2;
3052 t->next_hop = path->peers[own_pos + 1];
3053 GNUNET_PEER_change_rc(t->next_hop, 1);
3055 /* It's for somebody else! Retransmit. */
3056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3057 path2 = path_duplicate (path);
3058 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3059 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3060 send_create_path (t);
3067 * Core handler for notifications of broken paths
3069 * @param cls closure
3070 * @param message message
3071 * @param peer peer identity this notification is about
3073 * @return GNUNET_OK to keep the connection open,
3074 * GNUNET_SYSERR to close it (signal serious error)
3077 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3078 const struct GNUNET_MessageHeader *message)
3080 struct GNUNET_MESH_PathBroken *msg;
3081 struct MeshTunnel *t;
3083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3084 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3085 msg = (struct GNUNET_MESH_PathBroken *) message;
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3087 GNUNET_i2s (&msg->peer1));
3088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3089 GNUNET_i2s (&msg->peer2));
3090 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3093 GNUNET_break_op (0);
3096 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3097 GNUNET_PEER_search (&msg->peer2));
3104 * Core handler for tunnel destruction
3106 * @param cls closure
3107 * @param message message
3108 * @param peer peer identity this notification is about
3110 * @return GNUNET_OK to keep the connection open,
3111 * GNUNET_SYSERR to close it (signal serious error)
3114 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3115 const struct GNUNET_MessageHeader *message)
3117 struct GNUNET_MESH_TunnelDestroy *msg;
3118 struct MeshTunnel *t;
3120 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3122 "Got a TUNNEL DESTROY packet from %s\n",
3124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3125 " for tunnel %s [%u]\n",
3126 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3127 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3130 /* Probably already got the message from another path,
3131 * destroyed the tunnel and retransmitted to children.
3134 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3138 // TODO check owner's signature
3139 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3141 /* Tunnel was incoming, notify clients */
3142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3143 t->local_tid, t->local_tid_dest);
3144 send_client_tunnel_destroy (t);
3146 tunnel_send_destroy (t);
3147 t->destroy = GNUNET_YES;
3148 // TODO: add timeout to destroy the tunnel anyway
3154 * Core handler for mesh network traffic going from the origin to a peer
3156 * @param cls closure
3157 * @param peer peer identity this notification is about
3158 * @param message message
3159 * @return GNUNET_OK to keep the connection open,
3160 * GNUNET_SYSERR to close it (signal serious error)
3163 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3164 const struct GNUNET_MessageHeader *message)
3166 struct GNUNET_MESH_Unicast *msg;
3167 struct MeshTunnel *t;
3172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3175 size = ntohs (message->size);
3177 sizeof (struct GNUNET_MESH_Unicast) +
3178 sizeof (struct GNUNET_MessageHeader))
3183 msg = (struct GNUNET_MESH_Unicast *) message;
3184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3185 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3187 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3190 /* TODO notify back: we don't know this tunnel */
3191 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3192 GNUNET_break_op (0);
3195 pid = ntohl (msg->pid);
3196 if (t->prev_fc.last_pid_recv == pid)
3198 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3199 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3200 " Already seen pid %u, DROPPING!\n", pid);
3205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3206 " pid %u not seen yet, forwarding\n", pid);
3209 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3211 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3212 GNUNET_break_op (0);
3213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3214 "Received PID %u, ACK %u\n",
3215 pid, t->prev_fc.last_ack_sent);
3216 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3219 t->prev_fc.last_pid_recv = pid;
3221 tunnel_reset_timeout (t);
3222 if (t->dest == myid)
3224 if (NULL == t->client)
3229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3230 " it's for us! sending to clients...\n");
3231 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3232 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
3233 message, GNUNET_NO);
3234 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3237 if (0 == t->next_hop)
3242 ttl = ntohl (msg->ttl);
3243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3246 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3247 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3248 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3252 " not for us, retransmitting...\n");
3254 send_prebuilt_message (message, t->next_hop, t);
3255 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3261 * Core handler for mesh network traffic toward the owner of a tunnel
3263 * @param cls closure
3264 * @param message message
3265 * @param peer peer identity this notification is about
3267 * @return GNUNET_OK to keep the connection open,
3268 * GNUNET_SYSERR to close it (signal serious error)
3271 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3272 const struct GNUNET_MessageHeader *message)
3274 struct GNUNET_MESH_ToOrigin *msg;
3275 struct MeshTunnel *t;
3279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3281 size = ntohs (message->size);
3282 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3283 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3285 GNUNET_break_op (0);
3288 msg = (struct GNUNET_MESH_ToOrigin *) message;
3289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3290 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3291 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3292 pid = ntohl (msg->pid);
3296 /* TODO notify that we dont know this tunnel (whom)? */
3297 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3299 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3300 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3304 if (t->next_fc.last_pid_recv == pid)
3306 /* already seen this packet, drop */
3307 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3309 " Already seen pid %u, DROPPING!\n", pid);
3310 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3314 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3316 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3317 GNUNET_break_op (0);
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3319 "Received PID %u, ACK %u\n",
3320 pid, t->next_fc.last_ack_sent);
3321 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3324 t->next_fc.last_pid_recv = pid;
3325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3326 " pid %u not seen yet, forwarding\n", pid);
3328 if (NULL != t->owner)
3331 struct GNUNET_MESH_ToOrigin *copy;
3333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3334 " it's for us! sending to clients...\n");
3335 /* TODO signature verification */
3336 memcpy (cbuf, message, size);
3337 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3338 copy->tid = htonl (t->local_tid);
3339 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3340 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3341 ©->header, GNUNET_NO);
3342 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3346 " not for us, retransmitting...\n");
3348 if (0 == t->prev_hop) /* No owner AND no prev hop */
3350 if (GNUNET_YES == t->destroy)
3352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3353 "to orig received on a dying tunnel %s [%X]\n",
3354 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3358 "unknown to origin at %s\n",
3359 GNUNET_i2s (&my_full_id));
3360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3363 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3364 "on tunnel %s [%X]\n",
3365 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3368 send_prebuilt_message (message, t->prev_hop, t);
3369 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3376 * Core handler for mesh network traffic point-to-point acks.
3378 * @param cls closure
3379 * @param message message
3380 * @param peer peer identity this notification is about
3382 * @return GNUNET_OK to keep the connection open,
3383 * GNUNET_SYSERR to close it (signal serious error)
3386 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3387 const struct GNUNET_MessageHeader *message)
3389 struct GNUNET_MESH_ACK *msg;
3390 struct MeshTunnel *t;
3394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3396 msg = (struct GNUNET_MESH_ACK *) message;
3398 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3402 /* TODO notify that we dont know this tunnel (whom)? */
3403 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3406 ack = ntohl (msg->pid);
3407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3409 /* Is this a forward or backward ACK? */
3410 id = GNUNET_PEER_search (peer);
3411 if (t->next_hop == id)
3414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3415 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task &&
3416 GMC_is_pid_bigger (ack, t->next_fc.last_ack_recv))
3418 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3419 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3420 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3422 t->next_fc.last_ack_recv = ack;
3423 peer_unlock_queue (t->next_hop);
3424 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3426 else if (t->prev_hop == id)
3429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3430 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
3431 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
3433 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3434 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3435 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3437 t->prev_fc.last_ack_recv = ack;
3438 peer_unlock_queue (t->prev_hop);
3439 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3442 GNUNET_break_op (0);
3448 * Core handler for mesh network traffic point-to-point ack polls.
3450 * @param cls closure
3451 * @param message message
3452 * @param peer peer identity this notification is about
3454 * @return GNUNET_OK to keep the connection open,
3455 * GNUNET_SYSERR to close it (signal serious error)
3458 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3459 const struct GNUNET_MessageHeader *message)
3461 struct GNUNET_MESH_Poll *msg;
3462 struct MeshTunnel *t;
3465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3468 msg = (struct GNUNET_MESH_Poll *) message;
3470 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3474 /* TODO notify that we dont know this tunnel (whom)? */
3475 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
3476 GNUNET_break_op (0);
3480 /* Is this a forward or backward ACK? */
3481 id = GNUNET_PEER_search(peer);
3482 if (t->next_hop == id)
3484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
3485 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3487 else if (t->prev_hop == id)
3489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
3490 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3499 * Core handler for path ACKs
3501 * @param cls closure
3502 * @param message message
3503 * @param peer peer identity this notification is about
3505 * @return GNUNET_OK to keep the connection open,
3506 * GNUNET_SYSERR to close it (signal serious error)
3509 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3510 const struct GNUNET_MessageHeader *message)
3512 struct GNUNET_MESH_PathACK *msg;
3513 struct MeshPeerInfo *peer_info;
3514 struct MeshPeerPath *p;
3515 struct MeshTunnel *t;
3517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3518 GNUNET_i2s (&my_full_id));
3519 msg = (struct GNUNET_MESH_PathACK *) message;
3520 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3523 /* TODO notify that we don't know the tunnel */
3524 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3526 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3530 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3532 peer_info = peer_get (&msg->peer_id);
3533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3534 GNUNET_i2s (&msg->peer_id));
3535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3538 /* Add path to peers? */
3542 path_add_to_peers (p, GNUNET_YES);
3548 t->state = MESH_TUNNEL_READY;
3549 t->next_fc.last_ack_recv = ntohl (msg->ack);
3550 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3552 /* Message for us? */
3553 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3556 if (NULL == t->owner)
3558 GNUNET_break_op (0);
3561 if (NULL != peer_info->dhtget)
3563 GNUNET_DHT_get_stop (peer_info->dhtget);
3564 peer_info->dhtget = NULL;
3566 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3571 " not for us, retransmitting...\n");
3572 peer_info = peer_get (&msg->oid);
3573 send_prebuilt_message (message, t->prev_hop, t);
3579 * Core handler for mesh keepalives.
3581 * @param cls closure
3582 * @param message message
3583 * @param peer peer identity this notification is about
3584 * @return GNUNET_OK to keep the connection open,
3585 * GNUNET_SYSERR to close it (signal serious error)
3587 * TODO: Check who we got this from, to validate route.
3590 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
3591 const struct GNUNET_MessageHeader *message)
3593 struct GNUNET_MESH_TunnelKeepAlive *msg;
3594 struct MeshTunnel *t;
3596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
3599 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
3600 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3604 /* TODO notify that we dont know that tunnel */
3605 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
3610 tunnel_reset_timeout (t);
3612 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
3613 send_prebuilt_message (message, t->next_hop, t);
3620 * Functions to handle messages from core
3622 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3623 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3624 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3625 sizeof (struct GNUNET_MESH_PathBroken)},
3626 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
3627 sizeof (struct GNUNET_MESH_TunnelDestroy)},
3628 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3629 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
3630 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
3631 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3632 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
3633 sizeof (struct GNUNET_MESH_ACK)},
3634 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
3635 sizeof (struct GNUNET_MESH_Poll)},
3636 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3637 sizeof (struct GNUNET_MESH_PathACK)},
3643 /******************************************************************************/
3644 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3645 /******************************************************************************/
3650 * notify_client_connection_failure: notify a client that the connection to the
3651 * requested remote peer is not possible (for instance, no route found)
3652 * Function called when the socket is ready to queue more data. "buf" will be
3653 * NULL and "size" zero if the socket was closed for writing in the meantime.
3655 * @param cls closure
3656 * @param size number of bytes available in buf
3657 * @param buf where the callee should write the message
3658 * @return number of bytes written to buf
3661 notify_client_connection_failure (void *cls, size_t size, void *buf)
3664 struct MeshPeerInfo *peer_info;
3665 struct GNUNET_MESH_PeerControl *msg;
3666 struct GNUNET_PeerIdentity id;
3668 if (0 == size && NULL == buf)
3670 // TODO retry? cancel?
3674 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3675 peer_info = (struct MeshPeerInfo *) cls;
3676 msg = (struct GNUNET_MESH_PeerControl *) buf;
3677 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3678 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3679 // msg->tunnel_id = htonl(peer_info->t->tid);
3680 GNUNET_PEER_resolve (peer_info->id, &id);
3681 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3689 * Send keepalive packets for a tunnel.
3691 * @param cls Closure (tunnel for which to send the keepalive).
3692 * @param tc Notification context.
3695 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3697 struct MeshTunnel *t = cls;
3698 struct GNUNET_MESH_TunnelKeepAlive *msg;
3699 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
3702 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3703 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
3704 NULL == t->owner || 0 == t->local_tid || 0 != t->prev_hop)
3709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3710 "sending keepalive for tunnel %d\n", t->id.tid);
3712 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
3713 msg->header.size = htons (size);
3714 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3715 msg->oid = my_full_id;
3716 msg->tid = htonl (t->id.tid);
3717 send_prebuilt_message (&msg->header, t->next_hop, t);
3719 t->maintenance_task =
3720 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3725 * Function to process paths received for a new peer addition. The recorded
3726 * paths form the initial tunnel, which can be optimized later.
3727 * Called on each result obtained for the DHT search.
3729 * @param cls closure
3730 * @param exp when will this value expire
3731 * @param key key of the result
3732 * @param get_path path of the get request
3733 * @param get_path_length lenght of get_path
3734 * @param put_path path of the put request
3735 * @param put_path_length length of the put_path
3736 * @param type type of the result
3737 * @param size number of bytes in data
3738 * @param data pointer to the result data
3741 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3742 const struct GNUNET_HashCode * key,
3743 const struct GNUNET_PeerIdentity *get_path,
3744 unsigned int get_path_length,
3745 const struct GNUNET_PeerIdentity *put_path,
3746 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3747 size_t size, const void *data)
3749 struct MeshPeerInfo *peer = cls;
3750 struct MeshPeerPath *p;
3751 struct GNUNET_PeerIdentity pi;
3754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3755 GNUNET_PEER_resolve (peer->id, &pi);
3756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3758 p = path_build_from_dht (get_path, get_path_length,
3759 put_path, put_path_length);
3760 path_add_to_peers (p, GNUNET_NO);
3762 for (i = 0; i < peer->ntunnels; i++)
3764 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
3765 peer_connect (peer, peer->tunnels[i]);
3772 /******************************************************************************/
3773 /********************* MESH LOCAL HANDLES **************************/
3774 /******************************************************************************/
3778 * Handler for client disconnection
3780 * @param cls closure
3781 * @param client identification of the client; NULL
3782 * for the last call when the server is destroyed
3785 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3787 struct MeshClient *c;
3788 struct MeshClient *next;
3790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3800 if (c->handle != client)
3802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3808 GNUNET_SERVER_client_drop (c->handle);
3809 c->shutting_down = GNUNET_YES;
3810 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3811 &tunnel_destroy_iterator, c);
3812 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3813 &tunnel_destroy_iterator, c);
3814 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3815 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3817 if (NULL != c->ports)
3818 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
3820 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
3821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3823 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
3826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
3832 * Handler for new clients
3834 * @param cls closure
3835 * @param client identification of the client
3836 * @param message the actual message, which includes messages the client wants
3839 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3840 const struct GNUNET_MessageHeader *message)
3842 struct GNUNET_MESH_ClientConnect *cc_msg;
3843 struct MeshClient *c;
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3850 /* Check data sanity */
3851 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3852 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3853 if (0 != (size % sizeof (uint32_t)))
3856 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3859 size /= sizeof (uint32_t);
3861 /* Create new client structure */
3862 c = GNUNET_malloc (sizeof (struct MeshClient));
3863 c->id = next_client_id++; /* overflow not important: just for debug */
3864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
3865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
3867 GNUNET_SERVER_client_keep (client);
3871 struct GNUNET_HashCode hc;
3873 p = (uint32_t *) &cc_msg[1];
3874 c->ports = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
3875 for (i = 0; i < size; i++)
3878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
3879 GMC_hash32 (u32, &hc);
3881 /* store in client's hashmap */
3882 GNUNET_CONTAINER_multihashmap_put (c->ports, &hc, c,
3883 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3884 /* store in global hashmap */
3885 /* FIXME only allow one client to have the port open,
3886 * have a backup hashmap with waiting clients */
3887 GNUNET_CONTAINER_multihashmap_put (ports, &hc, c,
3888 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3892 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
3893 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
3894 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
3895 GNUNET_SERVER_notification_context_add (nc, client);
3896 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
3898 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3904 * Handler for requests of new tunnels
3906 * @param cls Closure.
3907 * @param client Identification of the client.
3908 * @param message The actual message.
3911 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3912 const struct GNUNET_MessageHeader *message)
3914 struct GNUNET_MESH_TunnelMessage *t_msg;
3915 struct MeshPeerInfo *peer_info;
3916 struct MeshTunnel *t;
3917 struct MeshClient *c;
3918 MESH_TunnelNumber tid;
3920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3922 /* Sanity check for client registration */
3923 if (NULL == (c = client_get (client)))
3926 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3931 /* Message sanity check */
3932 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3935 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3939 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
3941 GNUNET_i2s (&t_msg->peer));
3942 /* Sanity check for tunnel numbering */
3943 tid = ntohl (t_msg->tunnel_id);
3944 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3947 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3950 /* Sanity check for duplicate tunnel IDs */
3951 if (NULL != tunnel_get_by_local_id (c, tid))
3954 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3958 while (NULL != tunnel_get_by_pi (myid, next_tid))
3959 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3960 t = tunnel_new (myid, next_tid, c, tid);
3961 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3965 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3968 t->port = ntohl (t_msg->port);
3969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
3970 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
3972 peer_info = peer_get (&t_msg->peer);
3973 GNUNET_array_append (peer_info->tunnels, peer_info->ntunnels, t);
3974 peer_connect (peer_info, t);
3975 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3981 * Handler for requests of deleting tunnels
3983 * @param cls closure
3984 * @param client identification of the client
3985 * @param message the actual message
3988 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3989 const struct GNUNET_MessageHeader *message)
3991 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3992 struct MeshClient *c;
3993 struct MeshTunnel *t;
3994 MESH_TunnelNumber tid;
3996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3997 "Got a DESTROY TUNNEL from client!\n");
3999 /* Sanity check for client registration */
4000 if (NULL == (c = client_get (client)))
4003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4008 /* Message sanity check */
4009 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4016 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4018 /* Retrieve tunnel */
4019 tid = ntohl (tunnel_msg->tunnel_id);
4020 t = tunnel_get_by_local_id(c, tid);
4023 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4025 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4030 tunnel_destroy_empty (t);
4031 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4034 send_client_tunnel_destroy (t);
4035 client_delete_tunnel (c, t);
4037 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4039 tunnel_send_destroy (t);
4040 peer_remove_tunnel (peer_get_short(t->dest), t);
4041 t->destroy = GNUNET_YES;
4042 /* The tunnel will be destroyed when the last message is transmitted. */
4043 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4049 * Handler for requests of seeting tunnel's buffering policy.
4051 * @param cls Closure (unused).
4052 * @param client Identification of the client.
4053 * @param message The actual message.
4056 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
4057 const struct GNUNET_MessageHeader *message)
4059 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4060 struct MeshClient *c;
4061 struct MeshTunnel *t;
4062 MESH_TunnelNumber tid;
4064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4065 "Got a BUFFER request from client!\n");
4067 /* Sanity check for client registration */
4068 if (NULL == (c = client_get (client)))
4071 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4076 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4078 /* Retrieve tunnel */
4079 tid = ntohl (tunnel_msg->tunnel_id);
4080 t = tunnel_get_by_local_id(c, tid);
4083 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4085 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4089 switch (ntohs(message->type))
4091 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
4092 t->nobuffer = GNUNET_NO;
4094 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
4095 t->nobuffer = GNUNET_YES;
4101 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4106 * Handler for client traffic directed to one peer
4108 * @param cls closure
4109 * @param client identification of the client
4110 * @param message the actual message
4113 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4114 const struct GNUNET_MessageHeader *message)
4116 struct MeshClient *c;
4117 struct MeshTunnel *t;
4118 struct GNUNET_MESH_Unicast *data_msg;
4119 MESH_TunnelNumber tid;
4122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4123 "Got a unicast request from a client!\n");
4125 /* Sanity check for client registration */
4126 if (NULL == (c = client_get (client)))
4129 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4134 data_msg = (struct GNUNET_MESH_Unicast *) message;
4136 /* Sanity check for message size */
4137 size = ntohs (message->size);
4138 if (sizeof (struct GNUNET_MESH_Unicast) +
4139 sizeof (struct GNUNET_MessageHeader) > size)
4142 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4146 /* Tunnel exists? */
4147 tid = ntohl (data_msg->tid);
4148 t = tunnel_get_by_local_id (c, tid);
4152 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4156 /* Is it a local tunnel? Then, does client own the tunnel? */
4157 if (t->owner->handle != client)
4160 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4164 /* PID should be as expected: client<->service communication */
4165 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4168 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4169 "Unicast PID, expected %u, got %u\n",
4170 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4171 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4175 /* Ok, everything is correct, send the message
4176 * (pretend we got it from a mesh peer)
4179 /* Work around const limitation */
4180 char buf[ntohs (message->size)] GNUNET_ALIGN;
4181 struct GNUNET_MESH_Unicast *copy;
4183 copy = (struct GNUNET_MESH_Unicast *) buf;
4184 memcpy (buf, data_msg, size);
4185 copy->oid = my_full_id;
4186 copy->tid = htonl (t->id.tid);
4187 copy->ttl = htonl (default_ttl);
4188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4189 " calling generic handler...\n");
4190 handle_mesh_unicast (NULL, &my_full_id, ©->header);
4192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4193 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4200 * Handler for client traffic directed to the origin
4202 * @param cls closure
4203 * @param client identification of the client
4204 * @param message the actual message
4207 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4208 const struct GNUNET_MessageHeader *message)
4210 struct GNUNET_MESH_ToOrigin *data_msg;
4211 struct MeshFlowControl *fc;
4212 struct MeshClient *c;
4213 struct MeshTunnel *t;
4214 MESH_TunnelNumber tid;
4217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4218 "Got a ToOrigin request from a client!\n");
4219 /* Sanity check for client registration */
4220 if (NULL == (c = client_get (client)))
4223 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4228 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4230 /* Sanity check for message size */
4231 size = ntohs (message->size);
4232 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4233 sizeof (struct GNUNET_MessageHeader) > size)
4236 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4240 /* Tunnel exists? */
4241 tid = ntohl (data_msg->tid);
4242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4243 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4246 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4249 t = tunnel_get_by_local_id (c, tid);
4252 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4253 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4255 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4259 /* It should be sent by someone who has this as incoming tunnel. */
4263 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4267 /* PID should be as expected */
4269 if (ntohl (data_msg->pid) != fc->last_pid_recv + 1)
4272 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4273 "To Origin PID, expected %u, got %u\n",
4274 fc->last_pid_recv + 1,
4275 ntohl (data_msg->pid));
4276 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4280 /* Ok, everything is correct, send the message
4281 * (pretend we got it from a mesh peer)
4284 char buf[ntohs (message->size)] GNUNET_ALIGN;
4285 struct GNUNET_MESH_ToOrigin *copy;
4287 /* Work around 'const' limitation */
4288 memcpy (buf, data_msg, size);
4289 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4290 GNUNET_PEER_resolve (t->id.oid, ©->oid);
4291 copy->tid = htonl (t->id.tid);
4292 copy->ttl = htonl (default_ttl);
4294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4295 " calling generic handler...\n");
4296 handle_mesh_to_orig (NULL, &my_full_id, ©->header);
4298 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4305 * Handler for client's ACKs for payload traffic.
4307 * @param cls Closure (unused).
4308 * @param client Identification of the client.
4309 * @param message The actual message.
4312 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4313 const struct GNUNET_MessageHeader *message)
4315 struct GNUNET_MESH_LocalAck *msg;
4316 struct MeshTunnel *t;
4317 struct MeshClient *c;
4318 MESH_TunnelNumber tid;
4321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4322 /* Sanity check for client registration */
4323 if (NULL == (c = client_get (client)))
4326 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4331 msg = (struct GNUNET_MESH_LocalAck *) message;
4333 /* Tunnel exists? */
4334 tid = ntohl (msg->tunnel_id);
4335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4336 t = tunnel_get_by_local_id (c, tid);
4340 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4341 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4342 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4346 ack = ntohl (msg->max_pid);
4347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4349 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4352 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4353 t->prev_fc.last_ack_recv = ack;
4354 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4358 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4359 t->next_fc.last_ack_recv = ack;
4360 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4363 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4371 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4373 * @param cls Closure (client handle).
4374 * @param key Key (hashed tunnel ID, unused).
4375 * @param value Tunnel info.
4377 * @return GNUNET_YES, to keep iterating.
4380 monitor_all_tunnels_iterator (void *cls,
4381 const struct GNUNET_HashCode * key,
4384 struct GNUNET_SERVER_Client *client = cls;
4385 struct MeshTunnel *t = value;
4386 struct GNUNET_MESH_LocalMonitor *msg;
4388 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4389 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4390 msg->tunnel_id = htonl (t->id.tid);
4391 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4392 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4393 GNUNET_PEER_resolve (t->dest, &msg->destination);
4395 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4396 "* sending info about tunnel %s [%u]\n",
4397 GNUNET_i2s (&msg->owner), t->id.tid);
4399 GNUNET_SERVER_notification_context_unicast (nc, client,
4400 &msg->header, GNUNET_NO);
4406 * Handler for client's MONITOR request.
4408 * @param cls Closure (unused).
4409 * @param client Identification of the client.
4410 * @param message The actual message.
4413 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4414 const struct GNUNET_MessageHeader *message)
4416 struct MeshClient *c;
4418 /* Sanity check for client registration */
4419 if (NULL == (c = client_get (client)))
4422 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4426 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4427 "Received get tunnels request from client %u\n",
4429 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4430 monitor_all_tunnels_iterator,
4432 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4433 "Get tunnels request from client %u completed\n",
4435 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4440 * Handler for client's MONITOR_TUNNEL request.
4442 * @param cls Closure (unused).
4443 * @param client Identification of the client.
4444 * @param message The actual message.
4447 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4448 const struct GNUNET_MessageHeader *message)
4450 const struct GNUNET_MESH_LocalMonitor *msg;
4451 struct GNUNET_MESH_LocalMonitor *resp;
4452 struct MeshClient *c;
4453 struct MeshTunnel *t;
4455 /* Sanity check for client registration */
4456 if (NULL == (c = client_get (client)))
4459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4463 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4464 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4465 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4468 ntohl (msg->tunnel_id));
4469 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4472 /* We don't know the tunnel FIXME */
4473 struct GNUNET_MESH_LocalMonitor warn;
4476 GNUNET_SERVER_notification_context_unicast (nc, client,
4479 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4483 /* Initialize context */
4484 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4486 GNUNET_PEER_resolve (t->dest, &resp->destination);
4487 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4488 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4489 &resp->header, GNUNET_NO);
4492 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4493 "Monitor tunnel request from client %u completed\n",
4495 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4500 * Functions to handle messages from clients
4502 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4503 {&handle_local_new_client, NULL,
4504 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4505 {&handle_local_tunnel_create, NULL,
4506 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4507 sizeof (struct GNUNET_MESH_TunnelMessage)},
4508 {&handle_local_tunnel_destroy, NULL,
4509 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4510 sizeof (struct GNUNET_MESH_TunnelMessage)},
4511 {&handle_local_tunnel_buffer, NULL,
4512 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
4513 sizeof (struct GNUNET_MESH_TunnelMessage)},
4514 {&handle_local_tunnel_buffer, NULL,
4515 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
4516 sizeof (struct GNUNET_MESH_TunnelMessage)},
4517 {&handle_local_unicast, NULL,
4518 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4519 {&handle_local_to_origin, NULL,
4520 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4521 {&handle_local_ack, NULL,
4522 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4523 sizeof (struct GNUNET_MESH_LocalAck)},
4524 {&handle_local_get_tunnels, NULL,
4525 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4526 sizeof (struct GNUNET_MessageHeader)},
4527 {&handle_local_show_tunnel, NULL,
4528 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4529 sizeof (struct GNUNET_MESH_LocalMonitor)},
4535 * Method called whenever a given peer connects.
4537 * @param cls closure
4538 * @param peer peer identity this notification is about
4541 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4543 struct MeshPeerInfo *peer_info;
4544 struct MeshPeerPath *path;
4546 DEBUG_CONN ("Peer connected\n");
4547 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4548 peer_info = peer_get (peer);
4549 if (myid == peer_info->id)
4551 DEBUG_CONN (" (self)\n");
4556 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4558 path = path_new (2);
4559 path->peers[0] = myid;
4560 path->peers[1] = peer_info->id;
4561 GNUNET_PEER_change_rc (myid, 1);
4562 GNUNET_PEER_change_rc (peer_info->id, 1);
4563 peer_info_add_path (peer_info, path, GNUNET_YES);
4564 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4570 * Method called whenever a peer disconnects.
4572 * @param cls closure
4573 * @param peer peer identity this notification is about
4576 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4578 struct MeshPeerInfo *pi;
4579 struct MeshPeerQueue *q;
4580 struct MeshPeerQueue *n;
4582 DEBUG_CONN ("Peer disconnected\n");
4583 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4593 /* TODO try to reroute this traffic instead */
4594 queue_destroy(q, GNUNET_YES);
4597 if (NULL != pi->core_transmit)
4599 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4600 pi->core_transmit = NULL;
4602 peer_remove_path (pi, pi->id, myid);
4605 DEBUG_CONN (" (self)\n");
4607 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
4613 * Install server (service) handlers and start listening to clients.
4618 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4619 GNUNET_SERVER_disconnect_notify (server_handle,
4620 &handle_local_client_disconnect, NULL);
4621 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
4623 clients_head = NULL;
4624 clients_tail = NULL;
4626 GNUNET_SERVER_resume (server_handle);
4631 * To be called on core init/fail.
4633 * @param cls Closure (config)
4634 * @param server handle to the server for this service
4635 * @param identity the public identity of this peer
4638 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4639 const struct GNUNET_PeerIdentity *identity)
4641 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4645 GNUNET_break (core_handle == server);
4646 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4649 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4650 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4652 GNUNET_i2s (identity));
4653 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4655 GNUNET_i2s (&my_full_id));
4656 GNUNET_CORE_disconnect (core_handle);
4657 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4658 NULL, /* Closure passed to MESH functions */
4659 &core_init, /* Call core_init once connected */
4660 &core_connect, /* Handle connects */
4661 &core_disconnect, /* remove peers on disconnects */
4662 NULL, /* Don't notify about all incoming messages */
4663 GNUNET_NO, /* For header only in notification */
4664 NULL, /* Don't notify about all outbound messages */
4665 GNUNET_NO, /* For header-only out notification */
4666 core_handlers); /* Register these handlers */
4675 /******************************************************************************/
4676 /************************ MAIN FUNCTIONS ****************************/
4677 /******************************************************************************/
4680 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4682 * @param cls closure
4683 * @param key current key code
4684 * @param value value in the hash map
4685 * @return GNUNET_YES if we should continue to iterate,
4689 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
4691 struct MeshTunnel *t = value;
4698 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4700 * @param cls closure
4701 * @param key current key code
4702 * @param value value in the hash map
4703 * @return GNUNET_YES if we should continue to iterate,
4707 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
4709 struct MeshPeerInfo *p = value;
4710 struct MeshPeerQueue *q;
4711 struct MeshPeerQueue *n;
4719 queue_destroy(q, GNUNET_YES);
4723 peer_info_destroy (p);
4729 * Task run during shutdown.
4735 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4739 if (core_handle != NULL)
4741 GNUNET_CORE_disconnect (core_handle);
4746 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
4749 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4750 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4751 if (dht_handle != NULL)
4753 GNUNET_DHT_disconnect (dht_handle);
4758 GNUNET_SERVER_notification_context_destroy (nc);
4761 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4763 GNUNET_SCHEDULER_cancel (announce_id_task);
4764 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4771 * Callback for hostkey read/generation.
4773 * @param cls Closure (Configuration handle).
4774 * @param pk The ECC private key.
4775 * @param emsg Error message, if any.
4778 key_generation_cb (void *cls,
4779 struct GNUNET_CRYPTO_EccPrivateKey *pk,
4782 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4783 struct MeshPeerInfo *peer;
4784 struct MeshPeerPath *p;
4789 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4790 _("Mesh service could not access hostkey: %s. Exiting.\n"),
4792 GNUNET_SCHEDULER_shutdown ();
4795 my_private_key = pk;
4796 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
4797 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4798 &my_full_id.hashPubKey);
4799 myid = GNUNET_PEER_intern (&my_full_id);
4800 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4801 "Mesh for peer [%s] starting\n",
4802 GNUNET_i2s(&my_full_id));
4804 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4805 NULL, /* Closure passed to MESH functions */
4806 &core_init, /* Call core_init once connected */
4807 &core_connect, /* Handle connects */
4808 &core_disconnect, /* remove peers on disconnects */
4809 NULL, /* Don't notify about all incoming messages */
4810 GNUNET_NO, /* For header only in notification */
4811 NULL, /* Don't notify about all outbound messages */
4812 GNUNET_NO, /* For header-only out notification */
4813 core_handlers); /* Register these handlers */
4814 if (core_handle == NULL)
4817 GNUNET_SCHEDULER_shutdown ();
4822 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4824 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4826 /* Create a peer_info for the local peer */
4827 peer = peer_get (&my_full_id);
4830 GNUNET_PEER_change_rc (myid, 1);
4831 peer_info_add_path (peer, p, GNUNET_YES);
4833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
4838 * Process mesh requests.
4840 * @param cls closure
4841 * @param server the initialized server
4842 * @param c configuration to use
4845 run (void *cls, struct GNUNET_SERVER_Handle *server,
4846 const struct GNUNET_CONFIGURATION_Handle *c)
4850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4851 server_handle = server;
4852 GNUNET_SERVER_suspend (server_handle);
4855 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
4858 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4860 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4861 "mesh", "peer/privatekey");
4862 GNUNET_SCHEDULER_shutdown ();
4867 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
4868 &refresh_path_time))
4870 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4872 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4873 "mesh", "refresh path time");
4874 GNUNET_SCHEDULER_shutdown ();
4879 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
4882 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4884 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4885 "mesh", "id announce time");
4886 GNUNET_SCHEDULER_shutdown ();
4891 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
4894 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4896 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4897 "mesh", "connect timeout");
4898 GNUNET_SCHEDULER_shutdown ();
4903 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
4906 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4908 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4909 "mesh", "max msgs queue");
4910 GNUNET_SCHEDULER_shutdown ();
4915 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
4918 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4920 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4921 "mesh", "max tunnels");
4922 GNUNET_SCHEDULER_shutdown ();
4927 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
4930 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4932 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4933 "mesh", "default ttl", 64);
4938 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
4941 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4942 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4943 "mesh", "max peers", 1000);
4948 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
4949 &dht_replication_level))
4951 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4953 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4954 "mesh", "dht replication level", 3);
4955 dht_replication_level = 3;
4958 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4959 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4960 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4961 ports = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4963 dht_handle = GNUNET_DHT_connect (c, 64);
4964 if (NULL == dht_handle)
4968 stats = GNUNET_STATISTICS_create ("mesh", c);
4970 /* Scheduled the task to clean up when shutdown is called */
4971 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4973 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
4976 GNUNET_free (keyfile);
4981 * The main function for the mesh service.
4983 * @param argc number of arguments from the command line
4984 * @param argv command line arguments
4985 * @return 0 ok, 1 on error
4988 main (int argc, char *const *argv)
4993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
4994 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4996 ret = (GNUNET_OK == r) ? 0 : 1;
4997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5001 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5002 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5003 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);