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);
1505 * Remove a tunnel from the list of tunnels a peer participates in.
1507 * @param p Peer to clean.
1508 * @param t Tunnel to remove.
1511 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1515 for (i = 0; i < p->ntunnels; i++)
1517 if (p->tunnels[i] == t)
1519 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1520 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1528 * Function called if the connection to the peer has been stalled for a while,
1529 * possibly due to a missed ACK. Poll the peer about its ACK status.
1531 * @param cls Closure (poll ctx).
1532 * @param tc TaskContext.
1535 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1537 struct MeshFlowControl *fc = cls;
1538 struct GNUNET_MESH_Poll msg;
1539 struct MeshTunnel *t = fc->t;
1540 GNUNET_PEER_Id peer;
1542 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1543 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1548 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1549 msg.header.size = htons (sizeof (msg));
1550 msg.tid = htonl (t->id.tid);
1551 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1553 if (fc == &t->prev_fc)
1557 else if (fc == &t->next_fc)
1566 send_prebuilt_message (&msg.header, peer, t);
1567 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1568 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1574 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1575 * to obtain a local peer -> destination path and interning the peer ids.
1577 * @return Newly allocated and created path
1579 static struct MeshPeerPath *
1580 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1581 unsigned int get_path_length,
1582 const struct GNUNET_PeerIdentity *put_path,
1583 unsigned int put_path_length)
1585 struct MeshPeerPath *p;
1591 GNUNET_PEER_change_rc (myid, 1);
1592 i = get_path_length;
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1594 for (i--; i >= 0; i--)
1596 id = GNUNET_PEER_intern (&get_path[i]);
1597 if (p->length > 0 && id == p->peers[p->length - 1])
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1600 GNUNET_PEER_change_rc (id, -1);
1604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1605 GNUNET_i2s (&get_path[i]));
1607 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1608 p->peers[p->length - 1] = id;
1611 i = put_path_length;
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1613 for (i--; i >= 0; i--)
1615 id = GNUNET_PEER_intern (&put_path[i]);
1618 /* PUT path went through us, so discard the path up until now and start
1619 * from here to get a much shorter (and loop-free) path.
1624 if (p->length > 0 && id == p->peers[p->length - 1])
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1627 GNUNET_PEER_change_rc (id, -1);
1631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1632 GNUNET_i2s (&put_path[i]));
1634 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1635 p->peers[p->length - 1] = id;
1639 if (get_path_length > 0)
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1641 GNUNET_i2s (&get_path[0]));
1642 if (put_path_length > 0)
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1644 GNUNET_i2s (&put_path[0]));
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1647 for (i = 0; i < p->length; i++)
1649 struct GNUNET_PeerIdentity peer_id;
1651 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1653 GNUNET_i2s (&peer_id));
1661 * Adds a path to the peer_infos of all the peers in the path
1663 * @param p Path to process.
1664 * @param confirmed Whether we know if the path works or not.
1667 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1671 /* TODO: invert and add */
1672 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1673 for (i++; i < p->length; i++)
1675 struct MeshPeerInfo *aux;
1676 struct MeshPeerPath *copy;
1678 aux = peer_get_short (p->peers[i]);
1679 copy = path_duplicate (p);
1680 copy->length = i + 1;
1681 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1687 * Send keepalive packets for a peer
1689 * @param cls Closure (tunnel for which to send the keepalive).
1690 * @param tc Notification context.
1693 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1697 * Search for a tunnel among the incoming tunnels
1699 * @param tid the local id of the tunnel
1701 * @return tunnel handler, NULL if doesn't exist
1703 static struct MeshTunnel *
1704 tunnel_get_incoming (MESH_TunnelNumber tid)
1706 struct GNUNET_HashCode hash;
1708 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1709 GMC_hash32 (tid, &hash);
1710 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1715 * Search for a tunnel among the tunnels for a client
1717 * @param c the client whose tunnels to search in
1718 * @param tid the local id of the tunnel
1720 * @return tunnel handler, NULL if doesn't exist
1722 static struct MeshTunnel *
1723 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1725 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1727 return tunnel_get_incoming (tid);
1731 struct GNUNET_HashCode hash;
1733 GMC_hash32 (tid, &hash);
1734 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1740 * Search for a tunnel by global ID using PEER_ID
1742 * @param pi owner of the tunnel
1743 * @param tid global tunnel number
1745 * @return tunnel handler, NULL if doesn't exist
1747 static struct MeshTunnel *
1748 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1750 struct MESH_TunnelID id;
1751 struct GNUNET_HashCode hash;
1756 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1757 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1762 * Search for a tunnel by global ID using full PeerIdentities
1764 * @param oid owner of the tunnel
1765 * @param tid global tunnel number
1767 * @return tunnel handler, NULL if doesn't exist
1769 static struct MeshTunnel *
1770 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1772 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1777 * Add a client to a tunnel, initializing all needed data structures.
1779 * @param t Tunnel to which add the client.
1780 * @param c Client which to add to the tunnel.
1783 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1785 struct GNUNET_HashCode hash;
1787 if (NULL != t->client)
1792 if (0 != t->next_hop)
1797 GMC_hash32 (t->local_tid_dest, &hash);
1799 GNUNET_CONTAINER_multihashmap_put (c->incoming_tunnels, &hash, t,
1800 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1806 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1807 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1817 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1819 unsigned int own_pos;
1821 for (own_pos = 0; own_pos < p->length; own_pos++)
1823 if (p->peers[own_pos] == myid)
1826 if (own_pos > p->length - 1)
1832 if (own_pos < p->length - 1)
1833 t->next_hop = p->peers[own_pos + 1];
1835 t->next_hop = p->peers[own_pos];
1836 GNUNET_PEER_change_rc (t->next_hop, 1);
1838 t->prev_hop = p->peers[own_pos - 1];
1840 t->prev_hop = p->peers[0];
1841 GNUNET_PEER_change_rc (t->prev_hop, 1);
1843 if (NULL != t->path)
1844 path_destroy (t->path);
1845 t->path = path_duplicate (p);
1848 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1849 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1850 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
1857 * Notifies a tunnel that a connection has broken that affects at least
1858 * some of its peers. Sends a notification towards the root of the tree.
1859 * In case the peer is the owner of the tree, notifies the client that owns
1860 * the tunnel and tries to reconnect.
1862 * @param t Tunnel affected.
1863 * @param p1 Peer that got disconnected from p2.
1864 * @param p2 Peer that got disconnected from p1.
1866 * @return Short ID of the peer disconnected (either p1 or p2).
1867 * 0 if the tunnel remained unaffected.
1869 static GNUNET_PEER_Id
1870 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1873 // if (myid != p1 && myid != p2) FIXME
1878 // if (tree_get_predecessor (t->tree) != 0)
1880 // /* We are the peer still connected, notify owner of the disconnection. */
1881 // struct GNUNET_MESH_PathBroken msg;
1882 // struct GNUNET_PeerIdentity neighbor;
1884 // msg.header.size = htons (sizeof (msg));
1885 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1886 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1887 // msg.tid = htonl (t->id.tid);
1888 // msg.peer1 = my_full_id;
1889 // GNUNET_PEER_resolve (pid, &msg.peer2);
1890 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1891 // send_prebuilt_message (&msg.header, &neighbor, t);
1898 * Build a local ACK message and send it to a local client.
1900 * @param t Tunnel on which to send the ACK.
1901 * @param c Client to whom send the ACK.
1902 * @param ack Value of the ACK.
1905 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
1907 struct GNUNET_MESH_LocalAck msg;
1909 msg.header.size = htons (sizeof (msg));
1910 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1911 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
1912 msg.max_pid = htonl (ack);
1913 GNUNET_SERVER_notification_context_unicast(nc,
1920 * Build an ACK message and queue it to send to the given peer.
1922 * @param t Tunnel on which to send the ACK.
1923 * @param peer Peer to whom send the ACK.
1924 * @param ack Value of the ACK.
1927 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1929 struct GNUNET_MESH_ACK msg;
1931 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1932 msg.header.size = htons (sizeof (msg));
1933 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1934 msg.pid = htonl (ack);
1935 msg.tid = htonl (t->id.tid);
1937 send_prebuilt_message (&msg.header, peer, t);
1942 * Send an ACK informing the predecessor about the available buffer space.
1943 * In case there is no predecessor, inform the owning client.
1944 * If buffering is off, send only on behalf of children or self if endpoint.
1945 * If buffering is on, send when sent to children and buffer space is free.
1946 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
1947 * the ACK itself goes "back" (towards root).
1949 * @param t Tunnel on which to send the ACK.
1950 * @param type Type of message that triggered the ACK transmission.
1953 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
1957 /* Is it after unicast retransmission? */
1960 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1962 "ACK due to FWD DATA retransmission\n");
1963 if (GNUNET_YES == t->nobuffer)
1965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
1969 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1970 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1972 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1973 t->force_ack = GNUNET_YES;
1979 /* Check if we need to transmit the ACK */
1980 if (t->queue_max > t->next_fc.queue_n * 4 &&
1981 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
1982 GNUNET_NO == t->force_ack)
1984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
1985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1986 " t->qmax: %u, t->qn: %u\n",
1987 t->queue_max, t->next_fc.queue_n);
1988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1989 " t->pid: %u, t->ack: %u\n",
1990 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
1994 /* Ok, ACK might be necessary, what PID to ACK? */
1995 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
1996 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
1998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2002 t->prev_fc.last_ack_sent = ack;
2003 if (NULL != t->owner)
2004 send_local_ack (t, t->owner, ack);
2005 else if (0 != t->prev_hop)
2006 send_ack (t, t->prev_hop, ack);
2010 t->force_ack = GNUNET_NO;
2015 * Send an ACK informing the children node/client about the available
2017 * If buffering is off, send only on behalf of root (can be self).
2018 * If buffering is on, send when sent to predecessor and buffer space is free.
2019 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2020 * the ACK itself goes "forward" (towards children/clients).
2022 * @param t Tunnel on which to send the ACK.
2023 * @param type Type of message that triggered the ACK transmission.
2026 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2030 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2031 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2032 /* Is it after data to_origin retransmission? */
2035 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2036 if (GNUNET_YES == t->nobuffer)
2038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2039 " Not sending ACK, nobuffer + traffic\n");
2043 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2044 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2046 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2047 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2048 t->force_ack = GNUNET_YES;
2054 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2056 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2058 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2061 " Not sending ACK, not needed\n");
2064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2065 " Sending BCK ACK %u (last sent: %u)\n",
2066 ack, t->next_fc.last_ack_sent);
2067 t->next_fc.last_ack_sent = ack;
2069 if (NULL != t->client)
2070 send_local_ack (t, t->client, ack);
2071 else if (0 != t->next_hop)
2072 send_ack (t, t->next_hop, ack);
2075 t->force_ack = GNUNET_NO;
2080 * @brief Re-initiate traffic to this peer if necessary.
2082 * Check if there is traffic queued towards this peer
2083 * and the core transmit handle is NULL (traffic was stalled).
2084 * If so, call core tmt rdy.
2086 * @param peer_id Short ID of peer to which initiate traffic.
2089 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2091 struct MeshPeerInfo *peer;
2092 struct GNUNET_PeerIdentity id;
2093 struct MeshPeerQueue *q;
2096 peer = peer_get_short (peer_id);
2097 if (NULL != peer->core_transmit)
2100 q = queue_get_next (peer);
2103 /* Might br multicast traffic already sent to this particular peer but
2104 * not to other children in this tunnel.
2105 * This way t->queue_n would be > 0 but the queue of this particular peer
2111 GNUNET_PEER_resolve (peer->id, &id);
2112 peer->core_transmit =
2113 GNUNET_CORE_notify_transmit_ready(core_handle,
2116 GNUNET_TIME_UNIT_FOREVER_REL,
2126 * Send a message to all peers in this tunnel that the tunnel is no longer
2129 * @param t The tunnel whose peers to notify.
2132 tunnel_send_destroy (struct MeshTunnel *t)
2134 struct GNUNET_MESH_TunnelDestroy msg;
2135 struct GNUNET_PeerIdentity id;
2137 msg.header.size = htons (sizeof (msg));
2138 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2139 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2140 msg.tid = htonl (t->id.tid);
2141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2142 " sending tunnel destroy for tunnel: %s [%X]\n",
2143 GNUNET_i2s (&msg.oid), t->id.tid);
2145 if (NULL == t->client && 0 != t->next_hop)
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2148 GNUNET_PEER_resolve (t->next_hop, &id);
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2150 " sending forward to %s\n",
2152 send_prebuilt_message (&msg.header, t->next_hop, t);
2154 if (NULL == t->owner && 0 != t->prev_hop)
2156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2157 GNUNET_PEER_resolve (t->prev_hop, &id);
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2159 " sending back to %s\n",
2161 send_prebuilt_message (&msg.header, t->prev_hop, t);
2167 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2169 * @param t Tunnel which to cancel.
2170 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2173 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2175 struct MeshPeerInfo *peer_info;
2176 struct MeshPeerQueue *pq;
2177 struct MeshPeerQueue *next;
2179 peer_info = peer_get_short (neighbor);
2180 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2183 if (pq->tunnel == t)
2185 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2186 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2188 /* Should have been removed on destroy children */
2191 queue_destroy (pq, GNUNET_YES);
2194 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2196 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2197 peer_info->core_transmit = NULL;
2203 * Destroy the tunnel and free any allocated resources linked to it.
2205 * @param t the tunnel to destroy
2207 * @return GNUNET_OK on success
2210 tunnel_destroy (struct MeshTunnel *t)
2212 struct MeshClient *c;
2213 struct GNUNET_HashCode hash;
2223 struct GNUNET_PeerIdentity id;
2225 GNUNET_PEER_resolve (t->id.oid, &id);
2226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2227 GNUNET_i2s (&id), t->id.tid);
2229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2233 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2234 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2242 GMC_hash32 (t->local_tid, &hash);
2244 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2251 if (NULL != t->client)
2254 GMC_hash32 (t->local_tid_dest, &hash);
2256 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2262 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2269 peer_cancel_queues (t->next_hop, t);
2270 peer_cancel_queues (t->prev_hop, t);
2271 GNUNET_PEER_change_rc(t->next_hop, -1);
2272 GNUNET_PEER_change_rc(t->prev_hop, -1);
2274 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2275 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2278 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2279 path_destroy (t->path);
2285 * Tunnel is empty: destroy it.
2287 * @param t Tunnel to destroy.
2290 tunnel_destroy_empty (struct MeshTunnel *t)
2294 struct GNUNET_PeerIdentity id;
2296 GNUNET_PEER_resolve (t->id.oid, &id);
2297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2298 "executing destruction of empty tunnel %s [%X]\n",
2299 GNUNET_i2s (&id), t->id.tid);
2303 if (GNUNET_NO == t->destroy)
2304 tunnel_send_destroy (t);
2305 if (0 == t->pending_messages)
2308 t->destroy = GNUNET_YES;
2312 * Initialize a Flow Control structure to the initial state.
2314 * @param fc Flow Control structure to initialize.
2317 fc_init (struct MeshFlowControl *fc)
2319 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2320 fc->last_pid_recv = (uint32_t) -1;
2321 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2322 fc->last_ack_recv = (uint32_t) -1;
2323 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2324 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2329 * Create a new tunnel
2331 * @param owner Who is the owner of the tunnel (short ID).
2332 * @param tid Tunnel Number of the tunnel.
2333 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2334 * @param local Tunnel Number for the tunnel, for the client point of view.
2336 * @return A new initialized tunnel. NULL on error.
2338 static struct MeshTunnel *
2339 tunnel_new (GNUNET_PEER_Id owner,
2340 MESH_TunnelNumber tid,
2341 struct MeshClient *client,
2342 MESH_TunnelNumber local)
2344 struct MeshTunnel *t;
2345 struct GNUNET_HashCode hash;
2347 if (n_tunnels >= max_tunnels && NULL == client)
2350 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2353 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2355 fc_init (&t->next_fc);
2356 fc_init (&t->prev_fc);
2357 t->local_tid = local;
2359 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2361 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2363 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2364 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2371 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2378 GMC_hash32 (t->local_tid, &hash);
2380 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2381 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2385 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2396 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2397 * client when the client disconnects. If the client is not the owner, the
2398 * owner will get notified if no more clients are in the tunnel and the client
2399 * get removed from the tunnel's list.
2401 * @param cls closure (client that is disconnecting)
2402 * @param key the hash of the local tunnel id (used to access the hashmap)
2403 * @param value the value stored at the key (tunnel to destroy)
2405 * @return GNUNET_OK, keep iterating.
2408 tunnel_destroy_iterator (void *cls,
2409 const struct GNUNET_HashCode * key,
2412 struct MeshTunnel *t = value;
2413 struct MeshClient *c = cls;
2415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2416 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2417 t->local_tid, t->local_tid_dest, c->id);
2418 if (GNUNET_NO == c->shutting_down)
2419 send_client_tunnel_destroy (t);
2420 client_delete_tunnel (c, t);
2423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2427 else if (c == t->owner)
2429 struct MeshPeerInfo *p;
2430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2433 p = peer_get_short(t->dest);
2434 peer_info_remove_tunnel (p, t);
2440 tunnel_destroy_empty (t);
2447 * Timeout function, destroys tunnel if called
2449 * @param cls Closure (tunnel to destroy).
2450 * @param tc TaskContext
2453 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2455 struct MeshTunnel *t = cls;
2456 struct GNUNET_PeerIdentity id;
2458 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2459 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2461 GNUNET_PEER_resolve(t->id.oid, &id);
2462 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2463 "Tunnel %s [%X] timed out. Destroying.\n",
2464 GNUNET_i2s(&id), t->id.tid);
2465 send_client_tunnel_destroy (t);
2471 * Resets the tunnel timeout. Starts it if no timeout was running.
2473 * @param t Tunnel whose timeout to reset.
2475 * TODO use heap to improve efficiency of scheduler.
2478 tunnel_reset_timeout (struct MeshTunnel *t)
2480 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2482 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2483 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2484 t->maintenance_task =
2485 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2486 (refresh_path_time, 4), &tunnel_timeout, t);
2490 /******************************************************************************/
2491 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2492 /******************************************************************************/
2495 * Function to send a create path packet to a peer.
2497 * @param cls closure
2498 * @param size number of bytes available in buf
2499 * @param buf where the callee should write the message
2500 * @return number of bytes written to buf
2503 send_core_path_create (void *cls, size_t size, void *buf)
2505 struct MeshTunnel *t = cls;
2506 struct GNUNET_MESH_CreateTunnel *msg;
2507 struct GNUNET_PeerIdentity *peer_ptr;
2508 struct MeshPeerPath *p = t->path;
2513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2515 sizeof (struct GNUNET_MESH_CreateTunnel) +
2516 p->length * sizeof (struct GNUNET_PeerIdentity);
2518 if (size < size_needed || NULL == buf)
2523 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2524 msg->header.size = htons (size_needed);
2525 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2526 msg->tid = ntohl (t->id.tid);
2529 if (GNUNET_YES == t->nobuffer)
2530 opt |= MESH_TUNNEL_OPT_NOBUFFER;
2531 msg->opt = htonl (opt);
2532 msg->port = htonl (t->port);
2534 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2535 for (i = 0; i < p->length; i++)
2537 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2541 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2547 * Creates a path ack message in buf and frees all unused resources.
2549 * @param cls closure (MeshTransmissionDescriptor)
2550 * @param size number of bytes available in buf
2551 * @param buf where the callee should write the message
2552 * @return number of bytes written to buf
2555 send_core_path_ack (void *cls, size_t size, void *buf)
2557 struct MeshTunnel *t = cls;
2558 struct GNUNET_MESH_PathACK *msg = buf;
2560 GNUNET_assert (NULL != t);
2561 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2566 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
2567 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2568 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2569 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
2570 msg->tid = htonl (t->id.tid);
2571 msg->peer_id = my_full_id;
2572 msg->ack = htonl (t->prev_fc.last_ack_sent);
2574 /* TODO add signature */
2576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2577 return sizeof (struct GNUNET_MESH_PathACK);
2582 * Free a transmission that was already queued with all resources
2583 * associated to the request.
2585 * @param queue Queue handler to cancel.
2586 * @param clear_cls Is it necessary to free associated cls?
2589 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2591 struct MeshFlowControl *fc;
2593 if (GNUNET_YES == clear_cls)
2595 switch (queue->type)
2597 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2598 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2599 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2601 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2602 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2603 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2604 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2605 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2607 " prebuilt message\n");
2608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2610 GNUNET_MESH_DEBUG_M2S (queue->type));
2612 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2618 " type %s unknown!\n",
2619 GNUNET_MESH_DEBUG_M2S (queue->type));
2621 GNUNET_free_non_null (queue->cls);
2623 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2624 queue->peer->queue_tail,
2627 /* Delete from appropiate fc in the tunnel */
2628 if (queue->peer->id == queue->tunnel->next_hop)
2629 fc = &queue->tunnel->next_fc;
2630 else if (queue->peer->id == queue->tunnel->next_hop)
2631 fc = &queue->tunnel->next_fc;
2638 GNUNET_free (queue);
2643 * @brief Get the next transmittable message from the queue.
2645 * This will be the head, except in the case of being a data packet
2646 * not allowed by the destination peer.
2648 * @param peer Destination peer.
2650 * @return The next viable MeshPeerQueue element to send to that peer.
2651 * NULL when there are no transmittable messages.
2653 struct MeshPeerQueue *
2654 queue_get_next (const struct MeshPeerInfo *peer)
2656 struct MeshPeerQueue *q;
2658 struct GNUNET_MESH_Unicast *ucast;
2659 struct GNUNET_MESH_ToOrigin *to_orig;
2660 struct MeshTunnel* t;
2664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
2665 for (q = peer->queue_head; NULL != q; q = q->next)
2668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2670 GNUNET_MESH_DEBUG_M2S(q->type));
2673 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2674 ucast = (struct GNUNET_MESH_Unicast *) q->cls;
2675 pid = ntohl (ucast->pid);
2676 ack = t->next_fc.last_ack_recv;
2678 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2679 to_orig = (struct GNUNET_MESH_ToOrigin *) q->cls;
2680 pid = ntohl (to_orig->pid);
2681 ack = t->prev_fc.last_ack_recv;
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2689 "* ACK: %u, PID: %u\n",
2691 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2704 "* nothing found\n");
2710 queue_send (void *cls, size_t size, void *buf)
2712 struct MeshPeerInfo *peer = cls;
2713 struct GNUNET_MessageHeader *msg;
2714 struct MeshPeerQueue *queue;
2715 struct MeshTunnel *t;
2716 struct GNUNET_PeerIdentity dst_id;
2717 struct MeshFlowControl *fc;
2721 peer->core_transmit = NULL;
2723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
2724 queue = queue_get_next (peer);
2726 /* Queue has no internal mesh traffic nor sendable payload */
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
2730 if (NULL == peer->queue_head)
2731 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
2736 GNUNET_PEER_resolve (peer->id, &dst_id);
2737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2739 GNUNET_i2s (&dst_id));
2740 /* Check if buffer size is enough for the message */
2741 if (queue->size > size)
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2744 "* not enough room, reissue\n");
2745 peer->core_transmit =
2746 GNUNET_CORE_notify_transmit_ready (core_handle,
2749 GNUNET_TIME_UNIT_FOREVER_REL,
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
2759 GNUNET_assert (0 < t->pending_messages);
2760 t->pending_messages--;
2764 switch (queue->type)
2767 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2768 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2769 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
2770 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
2771 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2772 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2775 GNUNET_MESH_DEBUG_M2S (queue->type));
2777 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2778 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2779 data_size = send_core_data_raw (queue->cls, size, buf);
2780 msg = (struct GNUNET_MessageHeader *) buf;
2781 type = ntohs (msg->type);
2783 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
2785 data_size = send_core_path_create (queue->cls, size, buf);
2787 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
2789 data_size = send_core_path_ack (queue->cls, size, buf);
2793 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2794 "* type unknown: %u\n",
2799 /* Free queue, but cls was freed by send_core_* */
2800 queue_destroy (queue, GNUNET_NO);
2802 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
2805 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2806 t->next_fc.last_pid_sent = ((struct GNUNET_MESH_Unicast *) buf)->pid;
2807 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
2809 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2810 t->prev_fc.last_pid_sent = ((struct GNUNET_MESH_ToOrigin *) buf)->pid;
2811 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
2817 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
2819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
2823 /* If more data in queue, send next */
2824 queue = queue_get_next (peer);
2827 struct GNUNET_PeerIdentity id;
2829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
2830 GNUNET_PEER_resolve (peer->id, &id);
2831 peer->core_transmit =
2832 GNUNET_CORE_notify_transmit_ready(core_handle,
2835 GNUNET_TIME_UNIT_FOREVER_REL,
2841 else if (NULL != peer->queue_head)
2843 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2845 GNUNET_i2s (&my_full_id));
2846 if (peer->id == t->next_hop)
2848 else if (peer->id == t->prev_hop)
2855 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
2858 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
2868 * @brief Queue and pass message to core when possible.
2870 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
2871 * accounts for it. In case the queue is full, the message is dropped and
2874 * Otherwise, message is treated as internal and allowed to go regardless of
2877 * @param cls Closure (@c type dependant). It will be used by queue_send to
2878 * build the message to be sent if not already prebuilt.
2879 * @param type Type of the message, 0 for a raw message.
2880 * @param size Size of the message.
2881 * @param dst Neighbor to send message to.
2882 * @param t Tunnel this message belongs to.
2885 queue_add (void *cls, uint16_t type, size_t size,
2886 struct MeshPeerInfo *dst, struct MeshTunnel *t)
2888 struct MeshPeerQueue *queue;
2889 struct GNUNET_PeerIdentity id;
2893 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2895 n = &t->next_fc.queue_n;
2897 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
2899 n = &t->prev_fc.queue_n;
2903 if (*n >= t->queue_max)
2906 GNUNET_STATISTICS_update(stats,
2907 "# messages dropped (buffer full)",
2909 return; /* Drop message */
2913 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2919 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
2920 if (NULL == dst->core_transmit)
2922 GNUNET_PEER_resolve (dst->id, &id);
2923 dst->core_transmit =
2924 GNUNET_CORE_notify_transmit_ready (core_handle,
2927 GNUNET_TIME_UNIT_FOREVER_REL,
2933 t->pending_messages++;
2937 /******************************************************************************/
2938 /******************** MESH NETWORK HANDLERS **************************/
2939 /******************************************************************************/
2943 * Core handler for path creation
2945 * @param cls closure
2946 * @param message message
2947 * @param peer peer identity this notification is about
2949 * @return GNUNET_OK to keep the connection open,
2950 * GNUNET_SYSERR to close it (signal serious error)
2953 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2954 const struct GNUNET_MessageHeader *message)
2956 unsigned int own_pos;
2959 MESH_TunnelNumber tid;
2960 struct GNUNET_MESH_CreateTunnel *msg;
2961 struct GNUNET_PeerIdentity *pi;
2962 struct MeshPeerPath *path;
2963 struct MeshPeerInfo *dest_peer_info;
2964 struct MeshPeerInfo *orig_peer_info;
2965 struct MeshTunnel *t;
2967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2968 "Received a path create msg [%s]\n",
2969 GNUNET_i2s (&my_full_id));
2970 size = ntohs (message->size);
2971 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
2973 GNUNET_break_op (0);
2977 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
2978 if (size % sizeof (struct GNUNET_PeerIdentity))
2980 GNUNET_break_op (0);
2983 size /= sizeof (struct GNUNET_PeerIdentity);
2986 GNUNET_break_op (0);
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2990 msg = (struct GNUNET_MESH_CreateTunnel *) message;
2992 tid = ntohl (msg->tid);
2993 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2995 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
2996 t = tunnel_get (pi, tid);
2997 if (NULL == t) /* might be a local tunnel */
3001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3002 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3008 t->port = ntohl (msg->port);
3009 opt = ntohl (msg->opt);
3010 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
3012 t->nobuffer = GNUNET_YES;
3013 t->prev_fc.last_ack_sent = t->prev_fc.last_pid_recv + 1;
3014 t->next_fc.last_ack_sent = t->next_fc.last_pid_recv + 1;
3017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3019 tunnel_reset_timeout (t);
3021 t->state = MESH_TUNNEL_WAITING;
3023 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3024 if (NULL == dest_peer_info)
3026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3027 " Creating PeerInfo for destination.\n");
3028 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3029 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3030 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3032 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3034 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3035 if (NULL == orig_peer_info)
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3038 " Creating PeerInfo for origin.\n");
3039 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3040 orig_peer_info->id = GNUNET_PEER_intern (pi);
3041 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3042 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3045 path = path_new (size);
3047 for (i = 0; i < size; i++)
3049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3050 GNUNET_i2s (&pi[i]));
3051 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3052 if (path->peers[i] == myid)
3055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3056 if (own_pos == 0 && path->peers[own_pos] != myid)
3058 /* create path: self not found in path through self */
3059 GNUNET_break_op (0);
3060 path_destroy (path);
3064 path_add_to_peers (path, GNUNET_NO);
3065 tunnel_use_path (t, path);
3067 if (own_pos == size - 1)
3069 struct MeshClient *c;
3070 struct GNUNET_HashCode hc;
3072 /* Find target client */
3073 GMC_hash32 (t->port, &hc);
3074 c = GNUNET_CONTAINER_multihashmap_get (ports, &hc);
3077 /* TODO send reject */
3081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3082 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3085 /* Assign local tid */
3086 while (NULL != tunnel_get_incoming (next_local_tid))
3087 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3088 t->local_tid_dest = next_local_tid++;
3089 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3091 tunnel_add_client (t, c);
3092 send_client_tunnel_create (t);
3097 struct MeshPeerPath *path2;
3099 t->next_hop = path->peers[own_pos + 1];
3100 GNUNET_PEER_change_rc(t->next_hop, 1);
3102 /* It's for somebody else! Retransmit. */
3103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3104 path2 = path_duplicate (path);
3105 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3106 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3107 send_create_path (t);
3115 * Core handler for path ACKs
3117 * @param cls closure
3118 * @param message message
3119 * @param peer peer identity this notification is about
3121 * @return GNUNET_OK to keep the connection open,
3122 * GNUNET_SYSERR to close it (signal serious error)
3125 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3126 const struct GNUNET_MessageHeader *message)
3128 struct GNUNET_MESH_PathACK *msg;
3129 struct MeshPeerInfo *peer_info;
3130 struct MeshPeerPath *p;
3131 struct MeshTunnel *t;
3133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3134 GNUNET_i2s (&my_full_id));
3135 msg = (struct GNUNET_MESH_PathACK *) message;
3136 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3139 /* TODO notify that we don't know the tunnel */
3140 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3142 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3146 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3148 peer_info = peer_get (&msg->peer_id);
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3150 GNUNET_i2s (&msg->peer_id));
3151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3154 /* Add path to peers? */
3158 path_add_to_peers (p, GNUNET_YES);
3164 t->state = MESH_TUNNEL_READY;
3165 t->next_fc.last_ack_recv = ntohl (msg->ack);
3166 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3168 /* Message for us? */
3169 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3172 if (NULL == t->owner)
3174 GNUNET_break_op (0);
3177 if (NULL != peer_info->dhtget)
3179 GNUNET_DHT_get_stop (peer_info->dhtget);
3180 peer_info->dhtget = NULL;
3182 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3187 " not for us, retransmitting...\n");
3188 peer_info = peer_get (&msg->oid);
3189 send_prebuilt_message (message, t->prev_hop, t);
3195 * Core handler for notifications of broken paths
3197 * @param cls closure
3198 * @param message message
3199 * @param peer peer identity this notification is about
3201 * @return GNUNET_OK to keep the connection open,
3202 * GNUNET_SYSERR to close it (signal serious error)
3205 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3206 const struct GNUNET_MessageHeader *message)
3208 struct GNUNET_MESH_PathBroken *msg;
3209 struct MeshTunnel *t;
3211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3212 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3213 msg = (struct GNUNET_MESH_PathBroken *) message;
3214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3215 GNUNET_i2s (&msg->peer1));
3216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3217 GNUNET_i2s (&msg->peer2));
3218 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3221 GNUNET_break_op (0);
3224 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3225 GNUNET_PEER_search (&msg->peer2));
3232 * Core handler for tunnel destruction
3234 * @param cls closure
3235 * @param message message
3236 * @param peer peer identity this notification is about
3238 * @return GNUNET_OK to keep the connection open,
3239 * GNUNET_SYSERR to close it (signal serious error)
3242 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3243 const struct GNUNET_MessageHeader *message)
3245 struct GNUNET_MESH_TunnelDestroy *msg;
3246 struct MeshTunnel *t;
3248 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3250 "Got a TUNNEL DESTROY packet from %s\n",
3252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3253 " for tunnel %s [%u]\n",
3254 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3255 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3258 /* Probably already got the message from another path,
3259 * destroyed the tunnel and retransmitted to children.
3262 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3266 // TODO check owner's signature
3267 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3269 /* Tunnel was incoming, notify clients */
3270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3271 t->local_tid, t->local_tid_dest);
3272 send_client_tunnel_destroy (t);
3274 tunnel_send_destroy (t);
3275 t->destroy = GNUNET_YES;
3276 // TODO: add timeout to destroy the tunnel anyway
3282 * Core handler for mesh network traffic going from the origin to a peer
3284 * @param cls closure
3285 * @param peer peer identity this notification is about
3286 * @param message message
3287 * @return GNUNET_OK to keep the connection open,
3288 * GNUNET_SYSERR to close it (signal serious error)
3291 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3292 const struct GNUNET_MessageHeader *message)
3294 struct GNUNET_MESH_Unicast *msg;
3295 struct MeshTunnel *t;
3300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3303 size = ntohs (message->size);
3305 sizeof (struct GNUNET_MESH_Unicast) +
3306 sizeof (struct GNUNET_MessageHeader))
3311 msg = (struct GNUNET_MESH_Unicast *) message;
3312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3313 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3315 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3318 /* TODO notify back: we don't know this tunnel */
3319 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3320 GNUNET_break_op (0);
3323 pid = ntohl (msg->pid);
3324 if (t->prev_fc.last_pid_recv == pid)
3326 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3327 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3328 " Already seen pid %u, DROPPING!\n", pid);
3333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3334 " pid %u not seen yet, forwarding\n", pid);
3337 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3339 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3340 GNUNET_break_op (0);
3341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3342 "Received PID %u, ACK %u\n",
3343 pid, t->prev_fc.last_ack_sent);
3344 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3347 t->prev_fc.last_pid_recv = pid;
3349 tunnel_reset_timeout (t);
3350 if (t->dest == myid)
3352 if (NULL == t->client)
3357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3358 " it's for us! sending to clients...\n");
3359 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3360 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
3361 message, GNUNET_NO);
3362 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3365 if (0 == t->next_hop)
3370 ttl = ntohl (msg->ttl);
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3374 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3375 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3376 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3380 " not for us, retransmitting...\n");
3382 send_prebuilt_message (message, t->next_hop, t);
3383 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3389 * Core handler for mesh network traffic toward the owner of a tunnel
3391 * @param cls closure
3392 * @param message message
3393 * @param peer peer identity this notification is about
3395 * @return GNUNET_OK to keep the connection open,
3396 * GNUNET_SYSERR to close it (signal serious error)
3399 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3400 const struct GNUNET_MessageHeader *message)
3402 struct GNUNET_MESH_ToOrigin *msg;
3403 struct MeshTunnel *t;
3407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3409 size = ntohs (message->size);
3410 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3411 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3413 GNUNET_break_op (0);
3416 msg = (struct GNUNET_MESH_ToOrigin *) message;
3417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3418 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3419 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3420 pid = ntohl (msg->pid);
3424 /* TODO notify that we dont know this tunnel (whom)? */
3425 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3427 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3428 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3432 if (t->next_fc.last_pid_recv == pid)
3434 /* already seen this packet, drop */
3435 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3437 " Already seen pid %u, DROPPING!\n", pid);
3438 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3442 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3444 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3445 GNUNET_break_op (0);
3446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3447 "Received PID %u, ACK %u\n",
3448 pid, t->next_fc.last_ack_sent);
3449 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3452 t->next_fc.last_pid_recv = pid;
3453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3454 " pid %u not seen yet, forwarding\n", pid);
3456 if (NULL != t->owner)
3459 struct GNUNET_MESH_ToOrigin *copy;
3461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3462 " it's for us! sending to clients...\n");
3463 /* TODO signature verification */
3464 memcpy (cbuf, message, size);
3465 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3466 copy->tid = htonl (t->local_tid);
3467 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3468 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3469 ©->header, GNUNET_NO);
3470 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3474 " not for us, retransmitting...\n");
3476 if (0 == t->prev_hop) /* No owner AND no prev hop */
3478 if (GNUNET_YES == t->destroy)
3480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3481 "to orig received on a dying tunnel %s [%X]\n",
3482 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3486 "unknown to origin at %s\n",
3487 GNUNET_i2s (&my_full_id));
3488 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3492 "on tunnel %s [%X]\n",
3493 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3496 send_prebuilt_message (message, t->prev_hop, t);
3497 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3504 * Core handler for mesh network traffic point-to-point acks.
3506 * @param cls closure
3507 * @param message message
3508 * @param peer peer identity this notification is about
3510 * @return GNUNET_OK to keep the connection open,
3511 * GNUNET_SYSERR to close it (signal serious error)
3514 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3515 const struct GNUNET_MessageHeader *message)
3517 struct GNUNET_MESH_ACK *msg;
3518 struct MeshTunnel *t;
3522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3524 msg = (struct GNUNET_MESH_ACK *) message;
3526 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3530 /* TODO notify that we dont know this tunnel (whom)? */
3531 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3534 ack = ntohl (msg->pid);
3535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3537 /* Is this a forward or backward ACK? */
3538 id = GNUNET_PEER_search (peer);
3539 if (t->next_hop == id)
3542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3543 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task &&
3544 GMC_is_pid_bigger (ack, t->next_fc.last_ack_recv))
3546 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3547 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3548 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3550 t->next_fc.last_ack_recv = ack;
3551 peer_unlock_queue (t->next_hop);
3552 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3554 else if (t->prev_hop == id)
3557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3558 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
3559 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
3561 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3562 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3563 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3565 t->prev_fc.last_ack_recv = ack;
3566 peer_unlock_queue (t->prev_hop);
3567 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3570 GNUNET_break_op (0);
3576 * Core handler for mesh network traffic point-to-point ack polls.
3578 * @param cls closure
3579 * @param message message
3580 * @param peer peer identity this notification is about
3582 * @return GNUNET_OK to keep the connection open,
3583 * GNUNET_SYSERR to close it (signal serious error)
3586 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3587 const struct GNUNET_MessageHeader *message)
3589 struct GNUNET_MESH_Poll *msg;
3590 struct MeshTunnel *t;
3593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3596 msg = (struct GNUNET_MESH_Poll *) message;
3598 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3602 /* TODO notify that we dont know this tunnel (whom)? */
3603 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
3604 GNUNET_break_op (0);
3608 /* Is this a forward or backward ACK? */
3609 id = GNUNET_PEER_search(peer);
3610 if (t->next_hop == id)
3612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
3613 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3615 else if (t->prev_hop == id)
3617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
3618 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3628 * Core handler for mesh keepalives.
3630 * @param cls closure
3631 * @param message message
3632 * @param peer peer identity this notification is about
3633 * @return GNUNET_OK to keep the connection open,
3634 * GNUNET_SYSERR to close it (signal serious error)
3636 * TODO: Check who we got this from, to validate route.
3639 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
3640 const struct GNUNET_MessageHeader *message)
3642 struct GNUNET_MESH_TunnelKeepAlive *msg;
3643 struct MeshTunnel *t;
3645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
3648 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
3649 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3653 /* TODO notify that we dont know that tunnel */
3654 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
3659 tunnel_reset_timeout (t);
3661 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
3662 send_prebuilt_message (message, t->next_hop, t);
3669 * Functions to handle messages from core
3671 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3672 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3673 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3674 sizeof (struct GNUNET_MESH_PathBroken)},
3675 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
3676 sizeof (struct GNUNET_MESH_TunnelDestroy)},
3677 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3678 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
3679 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
3680 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3681 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
3682 sizeof (struct GNUNET_MESH_ACK)},
3683 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
3684 sizeof (struct GNUNET_MESH_Poll)},
3685 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3686 sizeof (struct GNUNET_MESH_PathACK)},
3692 /******************************************************************************/
3693 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3694 /******************************************************************************/
3699 * notify_client_connection_failure: notify a client that the connection to the
3700 * requested remote peer is not possible (for instance, no route found)
3701 * Function called when the socket is ready to queue more data. "buf" will be
3702 * NULL and "size" zero if the socket was closed for writing in the meantime.
3704 * @param cls closure
3705 * @param size number of bytes available in buf
3706 * @param buf where the callee should write the message
3707 * @return number of bytes written to buf
3710 notify_client_connection_failure (void *cls, size_t size, void *buf)
3713 struct MeshPeerInfo *peer_info;
3714 struct GNUNET_MESH_PeerControl *msg;
3715 struct GNUNET_PeerIdentity id;
3717 if (0 == size && NULL == buf)
3719 // TODO retry? cancel?
3723 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3724 peer_info = (struct MeshPeerInfo *) cls;
3725 msg = (struct GNUNET_MESH_PeerControl *) buf;
3726 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3727 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3728 // msg->tunnel_id = htonl(peer_info->t->tid);
3729 GNUNET_PEER_resolve (peer_info->id, &id);
3730 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3738 * Send keepalive packets for a tunnel.
3740 * @param cls Closure (tunnel for which to send the keepalive).
3741 * @param tc Notification context.
3744 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3746 struct MeshTunnel *t = cls;
3747 struct GNUNET_MESH_TunnelKeepAlive *msg;
3748 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
3751 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3752 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
3753 NULL == t->owner || 0 == t->local_tid || 0 != t->prev_hop)
3758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3759 "sending keepalive for tunnel %d\n", t->id.tid);
3761 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
3762 msg->header.size = htons (size);
3763 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3764 msg->oid = my_full_id;
3765 msg->tid = htonl (t->id.tid);
3766 send_prebuilt_message (&msg->header, t->next_hop, t);
3768 t->maintenance_task =
3769 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3774 * Function to process paths received for a new peer addition. The recorded
3775 * paths form the initial tunnel, which can be optimized later.
3776 * Called on each result obtained for the DHT search.
3778 * @param cls closure
3779 * @param exp when will this value expire
3780 * @param key key of the result
3781 * @param get_path path of the get request
3782 * @param get_path_length lenght of get_path
3783 * @param put_path path of the put request
3784 * @param put_path_length length of the put_path
3785 * @param type type of the result
3786 * @param size number of bytes in data
3787 * @param data pointer to the result data
3790 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3791 const struct GNUNET_HashCode * key,
3792 const struct GNUNET_PeerIdentity *get_path,
3793 unsigned int get_path_length,
3794 const struct GNUNET_PeerIdentity *put_path,
3795 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3796 size_t size, const void *data)
3798 struct MeshPeerInfo *peer = cls;
3799 struct MeshPeerPath *p;
3800 struct GNUNET_PeerIdentity pi;
3803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3804 GNUNET_PEER_resolve (peer->id, &pi);
3805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3807 p = path_build_from_dht (get_path, get_path_length,
3808 put_path, put_path_length);
3809 path_add_to_peers (p, GNUNET_NO);
3811 for (i = 0; i < peer->ntunnels; i++)
3813 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
3814 peer_connect (peer, peer->tunnels[i]);
3821 /******************************************************************************/
3822 /********************* MESH LOCAL HANDLES **************************/
3823 /******************************************************************************/
3827 * Handler for client disconnection
3829 * @param cls closure
3830 * @param client identification of the client; NULL
3831 * for the last call when the server is destroyed
3834 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3836 struct MeshClient *c;
3837 struct MeshClient *next;
3839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3849 if (c->handle != client)
3851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3857 GNUNET_SERVER_client_drop (c->handle);
3858 c->shutting_down = GNUNET_YES;
3859 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3860 &tunnel_destroy_iterator, c);
3861 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3862 &tunnel_destroy_iterator, c);
3863 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3864 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3866 if (NULL != c->ports)
3867 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
3869 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
3870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3872 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
3875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
3881 * Handler for new clients
3883 * @param cls closure
3884 * @param client identification of the client
3885 * @param message the actual message, which includes messages the client wants
3888 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3889 const struct GNUNET_MessageHeader *message)
3891 struct GNUNET_MESH_ClientConnect *cc_msg;
3892 struct MeshClient *c;
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3899 /* Check data sanity */
3900 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3901 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3902 if (0 != (size % sizeof (uint32_t)))
3905 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3908 size /= sizeof (uint32_t);
3910 /* Create new client structure */
3911 c = GNUNET_malloc (sizeof (struct MeshClient));
3912 c->id = next_client_id++; /* overflow not important: just for debug */
3913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
3914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
3916 GNUNET_SERVER_client_keep (client);
3920 struct GNUNET_HashCode hc;
3922 p = (uint32_t *) &cc_msg[1];
3923 c->ports = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
3924 for (i = 0; i < size; i++)
3927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
3928 GMC_hash32 (u32, &hc);
3930 /* store in client's hashmap */
3931 GNUNET_CONTAINER_multihashmap_put (c->ports, &hc, c,
3932 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3933 /* store in global hashmap */
3934 /* FIXME only allow one client to have the port open,
3935 * have a backup hashmap with waiting clients */
3936 GNUNET_CONTAINER_multihashmap_put (ports, &hc, c,
3937 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3941 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
3942 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
3943 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
3944 GNUNET_SERVER_notification_context_add (nc, client);
3945 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
3947 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3953 * Handler for requests of new tunnels
3955 * @param cls Closure.
3956 * @param client Identification of the client.
3957 * @param message The actual message.
3960 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3961 const struct GNUNET_MessageHeader *message)
3963 struct GNUNET_MESH_TunnelMessage *t_msg;
3964 struct MeshPeerInfo *peer_info;
3965 struct MeshTunnel *t;
3966 struct MeshClient *c;
3967 MESH_TunnelNumber tid;
3969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3971 /* Sanity check for client registration */
3972 if (NULL == (c = client_get (client)))
3975 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3980 /* Message sanity check */
3981 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3984 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3988 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
3990 GNUNET_i2s (&t_msg->peer));
3991 /* Sanity check for tunnel numbering */
3992 tid = ntohl (t_msg->tunnel_id);
3993 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3999 /* Sanity check for duplicate tunnel IDs */
4000 if (NULL != tunnel_get_by_local_id (c, tid))
4003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4007 while (NULL != tunnel_get_by_pi (myid, next_tid))
4008 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4009 t = tunnel_new (myid, next_tid, c, tid);
4010 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4017 t->port = ntohl (t_msg->port);
4018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4019 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4021 peer_info = peer_get (&t_msg->peer);
4022 GNUNET_array_append (peer_info->tunnels, peer_info->ntunnels, t);
4023 peer_connect (peer_info, t);
4024 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4030 * Handler for requests of deleting tunnels
4032 * @param cls closure
4033 * @param client identification of the client
4034 * @param message the actual message
4037 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4038 const struct GNUNET_MessageHeader *message)
4040 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4041 struct MeshClient *c;
4042 struct MeshTunnel *t;
4043 MESH_TunnelNumber tid;
4045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4046 "Got a DESTROY TUNNEL from client!\n");
4048 /* Sanity check for client registration */
4049 if (NULL == (c = client_get (client)))
4052 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4057 /* Message sanity check */
4058 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4061 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4065 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4067 /* Retrieve tunnel */
4068 tid = ntohl (tunnel_msg->tunnel_id);
4069 t = tunnel_get_by_local_id(c, tid);
4072 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4074 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4079 tunnel_destroy_empty (t);
4080 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4083 send_client_tunnel_destroy (t);
4084 client_delete_tunnel (c, t);
4086 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4088 tunnel_send_destroy (t);
4089 peer_remove_tunnel (peer_get_short(t->dest), t);
4090 t->destroy = GNUNET_YES;
4091 /* The tunnel will be destroyed when the last message is transmitted. */
4092 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4098 * Handler for requests of seeting tunnel's buffering policy.
4100 * @param cls Closure (unused).
4101 * @param client Identification of the client.
4102 * @param message The actual message.
4105 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
4106 const struct GNUNET_MessageHeader *message)
4108 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4109 struct MeshClient *c;
4110 struct MeshTunnel *t;
4111 MESH_TunnelNumber tid;
4113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4114 "Got a BUFFER request from client!\n");
4116 /* Sanity check for client registration */
4117 if (NULL == (c = client_get (client)))
4120 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4125 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4127 /* Retrieve tunnel */
4128 tid = ntohl (tunnel_msg->tunnel_id);
4129 t = tunnel_get_by_local_id(c, tid);
4132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4134 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4138 switch (ntohs(message->type))
4140 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
4141 t->nobuffer = GNUNET_NO;
4143 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
4144 t->nobuffer = GNUNET_YES;
4150 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4155 * Handler for client traffic directed to one peer
4157 * @param cls closure
4158 * @param client identification of the client
4159 * @param message the actual message
4162 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4163 const struct GNUNET_MessageHeader *message)
4165 struct MeshClient *c;
4166 struct MeshTunnel *t;
4167 struct GNUNET_MESH_Unicast *data_msg;
4168 MESH_TunnelNumber tid;
4171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4172 "Got a unicast request from a client!\n");
4174 /* Sanity check for client registration */
4175 if (NULL == (c = client_get (client)))
4178 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4183 data_msg = (struct GNUNET_MESH_Unicast *) message;
4185 /* Sanity check for message size */
4186 size = ntohs (message->size);
4187 if (sizeof (struct GNUNET_MESH_Unicast) +
4188 sizeof (struct GNUNET_MessageHeader) > size)
4191 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4195 /* Tunnel exists? */
4196 tid = ntohl (data_msg->tid);
4197 t = tunnel_get_by_local_id (c, tid);
4201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4205 /* Is it a local tunnel? Then, does client own the tunnel? */
4206 if (t->owner->handle != client)
4209 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4213 /* PID should be as expected: client<->service communication */
4214 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4218 "Unicast PID, expected %u, got %u\n",
4219 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4220 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4224 /* Ok, everything is correct, send the message
4225 * (pretend we got it from a mesh peer)
4228 /* Work around const limitation */
4229 char buf[ntohs (message->size)] GNUNET_ALIGN;
4230 struct GNUNET_MESH_Unicast *copy;
4232 copy = (struct GNUNET_MESH_Unicast *) buf;
4233 memcpy (buf, data_msg, size);
4234 copy->oid = my_full_id;
4235 copy->tid = htonl (t->id.tid);
4236 copy->ttl = htonl (default_ttl);
4237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4238 " calling generic handler...\n");
4239 handle_mesh_unicast (NULL, &my_full_id, ©->header);
4241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4242 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4249 * Handler for client traffic directed to the origin
4251 * @param cls closure
4252 * @param client identification of the client
4253 * @param message the actual message
4256 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4257 const struct GNUNET_MessageHeader *message)
4259 struct GNUNET_MESH_ToOrigin *data_msg;
4260 struct MeshFlowControl *fc;
4261 struct MeshClient *c;
4262 struct MeshTunnel *t;
4263 MESH_TunnelNumber tid;
4266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4267 "Got a ToOrigin request from a client!\n");
4268 /* Sanity check for client registration */
4269 if (NULL == (c = client_get (client)))
4272 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4277 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4279 /* Sanity check for message size */
4280 size = ntohs (message->size);
4281 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4282 sizeof (struct GNUNET_MessageHeader) > size)
4285 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4289 /* Tunnel exists? */
4290 tid = ntohl (data_msg->tid);
4291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4292 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4295 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4298 t = tunnel_get_by_local_id (c, tid);
4301 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4302 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4308 /* It should be sent by someone who has this as incoming tunnel. */
4312 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4316 /* PID should be as expected */
4318 if (ntohl (data_msg->pid) != fc->last_pid_recv + 1)
4321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4322 "To Origin PID, expected %u, got %u\n",
4323 fc->last_pid_recv + 1,
4324 ntohl (data_msg->pid));
4325 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4329 /* Ok, everything is correct, send the message
4330 * (pretend we got it from a mesh peer)
4333 char buf[ntohs (message->size)] GNUNET_ALIGN;
4334 struct GNUNET_MESH_ToOrigin *copy;
4336 /* Work around 'const' limitation */
4337 memcpy (buf, data_msg, size);
4338 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4339 GNUNET_PEER_resolve (t->id.oid, ©->oid);
4340 copy->tid = htonl (t->id.tid);
4341 copy->ttl = htonl (default_ttl);
4343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4344 " calling generic handler...\n");
4345 handle_mesh_to_orig (NULL, &my_full_id, ©->header);
4347 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4354 * Handler for client's ACKs for payload traffic.
4356 * @param cls Closure (unused).
4357 * @param client Identification of the client.
4358 * @param message The actual message.
4361 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4362 const struct GNUNET_MessageHeader *message)
4364 struct GNUNET_MESH_LocalAck *msg;
4365 struct MeshTunnel *t;
4366 struct MeshClient *c;
4367 MESH_TunnelNumber tid;
4370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4371 /* Sanity check for client registration */
4372 if (NULL == (c = client_get (client)))
4375 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4380 msg = (struct GNUNET_MESH_LocalAck *) message;
4382 /* Tunnel exists? */
4383 tid = ntohl (msg->tunnel_id);
4384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4385 t = tunnel_get_by_local_id (c, tid);
4389 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4390 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4391 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4395 ack = ntohl (msg->max_pid);
4396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4398 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4401 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4402 t->prev_fc.last_ack_recv = ack;
4403 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4407 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4408 t->next_fc.last_ack_recv = ack;
4409 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4412 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4420 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4422 * @param cls Closure (client handle).
4423 * @param key Key (hashed tunnel ID, unused).
4424 * @param value Tunnel info.
4426 * @return GNUNET_YES, to keep iterating.
4429 monitor_all_tunnels_iterator (void *cls,
4430 const struct GNUNET_HashCode * key,
4433 struct GNUNET_SERVER_Client *client = cls;
4434 struct MeshTunnel *t = value;
4435 struct GNUNET_MESH_LocalMonitor *msg;
4437 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4438 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4439 msg->tunnel_id = htonl (t->id.tid);
4440 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4441 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4442 GNUNET_PEER_resolve (t->dest, &msg->destination);
4444 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4445 "* sending info about tunnel %s [%u]\n",
4446 GNUNET_i2s (&msg->owner), t->id.tid);
4448 GNUNET_SERVER_notification_context_unicast (nc, client,
4449 &msg->header, GNUNET_NO);
4455 * Handler for client's MONITOR request.
4457 * @param cls Closure (unused).
4458 * @param client Identification of the client.
4459 * @param message The actual message.
4462 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4463 const struct GNUNET_MessageHeader *message)
4465 struct MeshClient *c;
4467 /* Sanity check for client registration */
4468 if (NULL == (c = client_get (client)))
4471 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4475 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4476 "Received get tunnels request from client %u\n",
4478 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4479 monitor_all_tunnels_iterator,
4481 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4482 "Get tunnels request from client %u completed\n",
4484 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4489 * Handler for client's MONITOR_TUNNEL request.
4491 * @param cls Closure (unused).
4492 * @param client Identification of the client.
4493 * @param message The actual message.
4496 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4497 const struct GNUNET_MessageHeader *message)
4499 const struct GNUNET_MESH_LocalMonitor *msg;
4500 struct GNUNET_MESH_LocalMonitor *resp;
4501 struct MeshClient *c;
4502 struct MeshTunnel *t;
4504 /* Sanity check for client registration */
4505 if (NULL == (c = client_get (client)))
4508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4512 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4513 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4514 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4517 ntohl (msg->tunnel_id));
4518 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4521 /* We don't know the tunnel FIXME */
4522 struct GNUNET_MESH_LocalMonitor warn;
4525 GNUNET_SERVER_notification_context_unicast (nc, client,
4528 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4532 /* Initialize context */
4533 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4535 GNUNET_PEER_resolve (t->dest, &resp->destination);
4536 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4537 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4538 &resp->header, GNUNET_NO);
4541 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4542 "Monitor tunnel request from client %u completed\n",
4544 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4549 * Functions to handle messages from clients
4551 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4552 {&handle_local_new_client, NULL,
4553 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4554 {&handle_local_tunnel_create, NULL,
4555 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4556 sizeof (struct GNUNET_MESH_TunnelMessage)},
4557 {&handle_local_tunnel_destroy, NULL,
4558 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4559 sizeof (struct GNUNET_MESH_TunnelMessage)},
4560 {&handle_local_tunnel_buffer, NULL,
4561 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
4562 sizeof (struct GNUNET_MESH_TunnelMessage)},
4563 {&handle_local_tunnel_buffer, NULL,
4564 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
4565 sizeof (struct GNUNET_MESH_TunnelMessage)},
4566 {&handle_local_unicast, NULL,
4567 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4568 {&handle_local_to_origin, NULL,
4569 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4570 {&handle_local_ack, NULL,
4571 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4572 sizeof (struct GNUNET_MESH_LocalAck)},
4573 {&handle_local_get_tunnels, NULL,
4574 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4575 sizeof (struct GNUNET_MessageHeader)},
4576 {&handle_local_show_tunnel, NULL,
4577 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4578 sizeof (struct GNUNET_MESH_LocalMonitor)},
4584 * Method called whenever a given peer connects.
4586 * @param cls closure
4587 * @param peer peer identity this notification is about
4590 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4592 struct MeshPeerInfo *peer_info;
4593 struct MeshPeerPath *path;
4595 DEBUG_CONN ("Peer connected\n");
4596 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4597 peer_info = peer_get (peer);
4598 if (myid == peer_info->id)
4600 DEBUG_CONN (" (self)\n");
4605 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4607 path = path_new (2);
4608 path->peers[0] = myid;
4609 path->peers[1] = peer_info->id;
4610 GNUNET_PEER_change_rc (myid, 1);
4611 GNUNET_PEER_change_rc (peer_info->id, 1);
4612 peer_info_add_path (peer_info, path, GNUNET_YES);
4613 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4619 * Method called whenever a peer disconnects.
4621 * @param cls closure
4622 * @param peer peer identity this notification is about
4625 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4627 struct MeshPeerInfo *pi;
4628 struct MeshPeerQueue *q;
4629 struct MeshPeerQueue *n;
4631 DEBUG_CONN ("Peer disconnected\n");
4632 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4642 /* TODO try to reroute this traffic instead */
4643 queue_destroy(q, GNUNET_YES);
4646 if (NULL != pi->core_transmit)
4648 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4649 pi->core_transmit = NULL;
4651 peer_remove_path (pi, pi->id, myid);
4654 DEBUG_CONN (" (self)\n");
4656 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
4662 * Install server (service) handlers and start listening to clients.
4667 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4668 GNUNET_SERVER_disconnect_notify (server_handle,
4669 &handle_local_client_disconnect, NULL);
4670 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
4672 clients_head = NULL;
4673 clients_tail = NULL;
4675 GNUNET_SERVER_resume (server_handle);
4680 * To be called on core init/fail.
4682 * @param cls Closure (config)
4683 * @param server handle to the server for this service
4684 * @param identity the public identity of this peer
4687 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4688 const struct GNUNET_PeerIdentity *identity)
4690 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4694 GNUNET_break (core_handle == server);
4695 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4698 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4699 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4701 GNUNET_i2s (identity));
4702 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4704 GNUNET_i2s (&my_full_id));
4705 GNUNET_CORE_disconnect (core_handle);
4706 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4707 NULL, /* Closure passed to MESH functions */
4708 &core_init, /* Call core_init once connected */
4709 &core_connect, /* Handle connects */
4710 &core_disconnect, /* remove peers on disconnects */
4711 NULL, /* Don't notify about all incoming messages */
4712 GNUNET_NO, /* For header only in notification */
4713 NULL, /* Don't notify about all outbound messages */
4714 GNUNET_NO, /* For header-only out notification */
4715 core_handlers); /* Register these handlers */
4724 /******************************************************************************/
4725 /************************ MAIN FUNCTIONS ****************************/
4726 /******************************************************************************/
4729 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4731 * @param cls closure
4732 * @param key current key code
4733 * @param value value in the hash map
4734 * @return GNUNET_YES if we should continue to iterate,
4738 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
4740 struct MeshTunnel *t = value;
4747 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4749 * @param cls closure
4750 * @param key current key code
4751 * @param value value in the hash map
4752 * @return GNUNET_YES if we should continue to iterate,
4756 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
4758 struct MeshPeerInfo *p = value;
4759 struct MeshPeerQueue *q;
4760 struct MeshPeerQueue *n;
4768 queue_destroy(q, GNUNET_YES);
4772 peer_info_destroy (p);
4778 * Task run during shutdown.
4784 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4788 if (core_handle != NULL)
4790 GNUNET_CORE_disconnect (core_handle);
4795 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
4798 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4799 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4800 if (dht_handle != NULL)
4802 GNUNET_DHT_disconnect (dht_handle);
4807 GNUNET_SERVER_notification_context_destroy (nc);
4810 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4812 GNUNET_SCHEDULER_cancel (announce_id_task);
4813 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4820 * Callback for hostkey read/generation.
4822 * @param cls Closure (Configuration handle).
4823 * @param pk The ECC private key.
4824 * @param emsg Error message, if any.
4827 key_generation_cb (void *cls,
4828 struct GNUNET_CRYPTO_EccPrivateKey *pk,
4831 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4832 struct MeshPeerInfo *peer;
4833 struct MeshPeerPath *p;
4838 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4839 _("Could not access hostkey: %s. Exiting.\n"),
4841 GNUNET_SCHEDULER_shutdown ();
4844 my_private_key = pk;
4845 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
4846 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4847 &my_full_id.hashPubKey);
4848 myid = GNUNET_PEER_intern (&my_full_id);
4849 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4850 "Mesh for peer [%s] starting\n",
4851 GNUNET_i2s(&my_full_id));
4853 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4854 NULL, /* Closure passed to MESH functions */
4855 &core_init, /* Call core_init once connected */
4856 &core_connect, /* Handle connects */
4857 &core_disconnect, /* remove peers on disconnects */
4858 NULL, /* Don't notify about all incoming messages */
4859 GNUNET_NO, /* For header only in notification */
4860 NULL, /* Don't notify about all outbound messages */
4861 GNUNET_NO, /* For header-only out notification */
4862 core_handlers); /* Register these handlers */
4863 if (core_handle == NULL)
4866 GNUNET_SCHEDULER_shutdown ();
4871 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4873 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4875 /* Create a peer_info for the local peer */
4876 peer = peer_get (&my_full_id);
4879 GNUNET_PEER_change_rc (myid, 1);
4880 peer_info_add_path (peer, p, GNUNET_YES);
4882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
4887 * Process mesh requests.
4889 * @param cls closure
4890 * @param server the initialized server
4891 * @param c configuration to use
4894 run (void *cls, struct GNUNET_SERVER_Handle *server,
4895 const struct GNUNET_CONFIGURATION_Handle *c)
4899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4900 server_handle = server;
4901 GNUNET_SERVER_suspend (server_handle);
4904 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
4907 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4909 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4910 "mesh", "peer/privatekey");
4911 GNUNET_SCHEDULER_shutdown ();
4916 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
4917 &refresh_path_time))
4919 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4921 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4922 "mesh", "refresh path time");
4923 GNUNET_SCHEDULER_shutdown ();
4928 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
4931 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4933 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4934 "mesh", "id announce time");
4935 GNUNET_SCHEDULER_shutdown ();
4940 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
4943 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4945 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4946 "mesh", "connect timeout");
4947 GNUNET_SCHEDULER_shutdown ();
4952 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
4955 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4957 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4958 "mesh", "max msgs queue");
4959 GNUNET_SCHEDULER_shutdown ();
4964 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
4967 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4969 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4970 "mesh", "max tunnels");
4971 GNUNET_SCHEDULER_shutdown ();
4976 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
4979 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4981 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4982 "mesh", "default ttl", 64);
4987 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
4990 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4991 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4992 "mesh", "max peers", 1000);
4997 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
4998 &dht_replication_level))
5000 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5002 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5003 "mesh", "dht replication level", 3);
5004 dht_replication_level = 3;
5007 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5008 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5009 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5010 ports = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5012 dht_handle = GNUNET_DHT_connect (c, 64);
5013 if (NULL == dht_handle)
5017 stats = GNUNET_STATISTICS_create ("mesh", c);
5019 /* Scheduled the task to clean up when shutdown is called */
5020 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5022 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
5025 GNUNET_free (keyfile);
5030 * The main function for the mesh service.
5032 * @param argc number of arguments from the command line
5033 * @param argv command line arguments
5034 * @return 0 ok, 1 on error
5037 main (int argc, char *const *argv)
5042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5043 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5045 ret = (GNUNET_OK == r) ? 0 : 1;
5046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5050 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5051 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5052 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);