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
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 * Use the given path for the tunnel.
749 * @param t Tunnel to update.
750 * @param p Path to use.
753 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
756 * @brief Queue and pass message to core when possible.
758 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
759 * and accounts for it. In case the queue is full, the message is dropped and
762 * Otherwise, message is treated as internal and allowed to go regardless of
765 * @param cls Closure (@c type dependant). It will be used by queue_send to
766 * build the message to be sent if not already prebuilt.
767 * @param type Type of the message, 0 for a raw message.
768 * @param size Size of the message.
769 * @param dst Neighbor to send message to.
770 * @param t Tunnel this message belongs to.
773 queue_add (void *cls, uint16_t type, size_t size,
774 struct MeshPeerInfo *dst, struct MeshTunnel *t);
778 * Free a transmission that was already queued with all resources
779 * associated to the request.
781 * @param queue Queue handler to cancel.
782 * @param clear_cls Is it necessary to free associated cls?
785 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
789 * @brief Get the next transmittable message from the queue.
791 * This will be the head, except in the case of being a data packet
792 * not allowed by the destination peer.
794 * @param peer Destination peer.
796 * @return The next viable MeshPeerQueue element to send to that peer.
797 * NULL when there are no transmittable messages.
799 struct MeshPeerQueue *
800 queue_get_next (const struct MeshPeerInfo *peer);
804 * Core callback to write a queued packet to core buffer
806 * @param cls Closure (peer info).
807 * @param size Number of bytes available in buf.
808 * @param buf Where the to write the message.
810 * @return number of bytes written to buf
813 queue_send (void *cls, size_t size, void *buf);
816 /******************************************************************************/
817 /************************ PERIODIC FUNCTIONS ****************************/
818 /******************************************************************************/
821 * Periodically announce self id in the DHT
824 * @param tc task context
827 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
831 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
833 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
837 * - Set data expiration in function of X
840 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
842 block.id = my_full_id;
843 block.type = htonl (0);
844 GNUNET_DHT_put (dht_handle, /* DHT handle */
845 &my_full_id.hashPubKey, /* Key to use */
846 dht_replication_level, /* Replication level */
847 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
848 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
849 sizeof (block), /* Size of the data */
850 (const char *) &block, /* Data itself */
851 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
852 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
853 NULL, /* Continuation */
854 NULL); /* Continuation closure */
856 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
860 /******************************************************************************/
861 /****************** GENERAL HELPER FUNCTIONS ************************/
862 /******************************************************************************/
866 * Check if client has registered with the service and has not disconnected
868 * @param client the client to check
870 * @return non-NULL if client exists in the global DLL
872 static struct MeshClient *
873 client_get (struct GNUNET_SERVER_Client *client)
875 struct MeshClient *c;
880 if (c->handle == client)
889 * Deletes a tunnel from a client (either owner or destination). To be used on
892 * @param c Client whose tunnel to delete.
893 * @param t Tunnel which should be deleted.
896 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
898 struct GNUNET_HashCode hash;
902 GMC_hash32 (t->local_tid, &hash);
903 GNUNET_assert (GNUNET_YES ==
904 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
908 else if (c == t->client)
910 GMC_hash32 (t->local_tid_dest, &hash);
911 GNUNET_assert (GNUNET_YES ==
912 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
923 * Notify the appropiate client that a new incoming tunnel was created.
925 * @param t Tunnel that was created.
928 send_client_tunnel_create (struct MeshTunnel *t)
930 struct GNUNET_MESH_TunnelMessage msg;
932 if (NULL == t->client)
934 msg.header.size = htons (sizeof (msg));
935 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
936 msg.tunnel_id = htonl (t->local_tid_dest);
937 msg.port = htonl (t->port);
938 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
939 &msg.header, GNUNET_NO);
944 * Notify dest client that the incoming tunnel is no longer valid.
946 * @param t Tunnel that was destroyed.
949 send_client_tunnel_destroy (struct MeshTunnel *t)
951 struct GNUNET_MESH_TunnelMessage msg;
953 if (NULL == t->client)
955 msg.header.size = htons (sizeof (msg));
956 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
957 msg.tunnel_id = htonl (t->local_tid_dest);
958 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
959 &msg.header, GNUNET_NO);
964 * Iterator over all the peers to remove the oldest not-used entry.
966 * @param cls Closure (unsued).
967 * @param key ID of the peer.
968 * @param value Peer_Info of the peer.
973 peer_info_timeout (void *cls,
974 const struct GNUNET_HashCode *key,
981 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
982 * and insert it in the appropiate structures if the peer is not known yet.
984 * @param peer Full identity of the peer.
986 * @return Existing or newly created peer info.
988 static struct MeshPeerInfo *
989 peer_get (const struct GNUNET_PeerIdentity *peer)
991 struct MeshPeerInfo *peer_info;
993 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
994 if (NULL == peer_info)
997 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
998 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1000 GNUNET_CONTAINER_multihashmap_iterate (peers,
1004 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1005 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1006 peer_info->id = GNUNET_PEER_intern (peer);
1008 peer_info->last_contact = GNUNET_TIME_absolute_get();
1015 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1016 * and insert it in the appropiate structures if the peer is not known yet.
1018 * @param peer Short identity of the peer.
1020 * @return Existing or newly created peer info.
1022 static struct MeshPeerInfo *
1023 peer_get_short (const GNUNET_PEER_Id peer)
1025 struct GNUNET_PeerIdentity id;
1027 GNUNET_PEER_resolve (peer, &id);
1028 return peer_get (&id);
1033 * Choose the best path towards a peer considering the tunnel properties.
1035 * @param peer The destination peer.
1036 * @param t The tunnel the path is for.
1038 * @return Best current known path towards the peer, if any.
1040 static struct MeshPeerPath *
1041 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1043 struct MeshPeerPath *best_p;
1044 struct MeshPeerPath *p;
1045 unsigned int best_cost;
1048 best_p = p = peer->path_head;
1049 best_cost = cost = p->length;
1052 if ((cost = p->length) < best_cost)
1064 * Remove the tunnel from the list of tunnels to which a peer is target.
1066 * @param peer PeerInfo of the peer.
1067 * @param t Tunnel to remove.
1070 peer_remove_tunnel (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1074 for (i = 0; i < peer->ntunnels; i++)
1077 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1080 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1082 GNUNET_realloc (peer->tunnels,
1083 peer->ntunnels * sizeof(struct MeshTunnel *));
1091 * Core callback to write a pre-constructed data packet to core buffer
1093 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1094 * @param size Number of bytes available in buf.
1095 * @param buf Where the to write the message.
1097 * @return number of bytes written to buf
1100 send_core_data_raw (void *cls, size_t size, void *buf)
1102 struct GNUNET_MessageHeader *msg = cls;
1105 GNUNET_assert (NULL != msg);
1106 total_size = ntohs (msg->size);
1108 if (total_size > size)
1113 memcpy (buf, msg, total_size);
1120 * Sends an already built message to a peer, properly registrating
1121 * all used resources.
1123 * @param message Message to send. Function makes a copy of it.
1124 * @param peer Short ID of the neighbor whom to send the message.
1125 * @param t Tunnel on which this message is transmitted.
1128 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1129 GNUNET_PEER_Id peer,
1130 struct MeshTunnel *t)
1132 struct GNUNET_PeerIdentity id;
1133 struct MeshPeerInfo *neighbor;
1134 struct MeshPeerPath *p;
1139 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1144 size = ntohs (message->size);
1145 data = GNUNET_malloc (size);
1146 memcpy (data, message, size);
1147 type = ntohs(message->type);
1150 struct GNUNET_MESH_Unicast *u;
1152 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1153 u = (struct GNUNET_MESH_Unicast *) data;
1154 u->ttl = htonl (ntohl (u->ttl) - 1);
1157 GNUNET_PEER_resolve (peer, &id);
1158 neighbor = peer_get (&id);
1159 for (p = neighbor->path_head; NULL != p; p = p->next)
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 " %s IS NOT DIRECTLY CONNECTED\n",
1172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1175 for (p = neighbor->path_head; NULL != p; p = p->next)
1177 struct GNUNET_PeerIdentity debug_id;
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 " path with %u hops through:\n",
1183 for (i = 0; i < p->length; i++)
1185 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1188 i, GNUNET_i2s(&debug_id));
1192 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1193 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1194 " no direct connection to %s\n",
1199 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1210 * Sends a CREATE PATH message for a path to a peer, properly registrating
1211 * all used resources.
1213 * @param t Tunnel for which the path is created.
1216 send_create_path (struct MeshTunnel *t)
1218 struct MeshPeerInfo *neighbor;
1220 neighbor = peer_get_short (t->next_hop);
1222 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1223 sizeof (struct GNUNET_MESH_CreateTunnel) +
1224 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1227 t->state = MESH_TUNNEL_WAITING;
1232 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1234 * @param t Tunnel which to confirm.
1237 send_path_ack (struct MeshTunnel *t)
1239 struct MeshPeerInfo *peer;
1241 if (0 == t->prev_hop)
1247 peer = peer_get_short (t->prev_hop);
1250 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1251 sizeof (struct GNUNET_MESH_PathACK),
1258 * Try to establish a new connection to this peer in the given tunnel.
1259 * If the peer doesn't have any path to it yet, try to get one.
1260 * If the peer already has some path, send a CREATE PATH towards it.
1262 * @param peer PeerInfo of the peer.
1263 * @param t Tunnel for which to create the path, if possible.
1266 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1268 struct MeshPeerPath *p;
1270 if (NULL != peer->path_head)
1272 p = peer_get_best_path (peer, t);
1273 tunnel_use_path (t, p);
1274 send_create_path (t);
1276 else if (NULL == peer->dhtget)
1278 struct GNUNET_PeerIdentity id;
1280 GNUNET_PEER_resolve (peer->id, &id);
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1282 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1283 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1284 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1285 &id.hashPubKey, /* key to search */
1286 dht_replication_level, /* replication level */
1287 GNUNET_DHT_RO_RECORD_ROUTE |
1288 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1290 0, /* xquery bits */
1291 &dht_get_id_handler, peer);
1292 t->state = MESH_TUNNEL_SEARCHING;
1294 /* Otherwise, there is no path but the DHT get is already started. */
1299 * Destroy the peer_info and free any allocated resources linked to it
1301 * @param pi The peer_info to destroy.
1303 * @return GNUNET_OK on success
1306 peer_info_destroy (struct MeshPeerInfo *pi)
1308 struct GNUNET_PeerIdentity id;
1309 struct MeshPeerPath *p;
1310 struct MeshPeerPath *nextp;
1312 GNUNET_PEER_resolve (pi->id, &id);
1313 GNUNET_PEER_change_rc (pi->id, -1);
1316 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1319 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1320 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1322 if (NULL != pi->dhtget)
1324 GNUNET_DHT_get_stop (pi->dhtget);
1330 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1340 * Remove all paths that rely on a direct connection between p1 and p2
1341 * from the peer itself and notify all tunnels about it.
1343 * @param peer PeerInfo of affected peer.
1344 * @param p1 GNUNET_PEER_Id of one peer.
1345 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1348 * TODO: optimize (see below)
1351 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1354 struct MeshPeerPath *p;
1355 struct MeshPeerPath *next;
1356 struct MeshPeerInfo *peer_d;
1358 unsigned int destroyed;
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1363 for (p = peer->path_head; NULL != p; p = next)
1366 for (i = 0; i < (p->length - 1); i++)
1368 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1369 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1371 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1381 for (i = 0; i < peer->ntunnels; i++)
1383 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1387 peer_d = peer_get_short (d);
1388 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1389 tunnel_use_path (peer->tunnels[i], next);
1390 peer_connect (peer_d, peer->tunnels[i]);
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1397 * Add the path to the peer and update the path used to reach it in case this
1400 * @param peer_info Destination peer to add the path to.
1401 * @param path New path to add. Last peer must be the peer in arg 1.
1402 * Path will be either used of freed if already known.
1403 * @param trusted Do we trust that this path is real?
1406 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1409 struct MeshPeerPath *aux;
1413 if ((NULL == peer_info) || (NULL == path))
1416 path_destroy (path);
1419 if (path->peers[path->length - 1] != peer_info->id)
1422 path_destroy (path);
1425 if (path->length <= 2 && GNUNET_NO == trusted)
1427 /* Only allow CORE to tell us about direct paths */
1428 path_destroy (path);
1431 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1432 for (l = 1; l < path->length; l++)
1434 if (path->peers[l] == myid)
1436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1437 for (l2 = 0; l2 < path->length - l; l2++)
1439 path->peers[l2] = path->peers[l + l2];
1444 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1449 struct GNUNET_PeerIdentity id;
1451 GNUNET_PEER_resolve (peer_info->id, &id);
1452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1453 path->length, GNUNET_i2s (&id));
1456 l = path_get_length (path);
1463 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1464 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1466 l2 = path_get_length (aux);
1469 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1470 peer_info->path_tail, aux, path);
1475 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1477 path_destroy (path);
1482 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1489 * Add the path to the origin peer and update the path used to reach it in case
1490 * this is the shortest.
1491 * The path is given in peer_info -> destination, therefore we turn the path
1492 * upside down first.
1494 * @param peer_info Peer to add the path to, being the origin of the path.
1495 * @param path New path to add after being inversed.
1496 * @param trusted Do we trust that this path is real?
1499 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1500 struct MeshPeerPath *path, int trusted)
1503 peer_info_add_path (peer_info, path, trusted);
1508 * Function called if the connection to the peer has been stalled for a while,
1509 * possibly due to a missed ACK. Poll the peer about its ACK status.
1511 * @param cls Closure (poll ctx).
1512 * @param tc TaskContext.
1515 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1517 struct MeshFlowControl *fc = cls;
1518 struct GNUNET_MESH_Poll msg;
1519 struct MeshTunnel *t = fc->t;
1520 GNUNET_PEER_Id peer;
1522 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1523 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1528 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1529 msg.header.size = htons (sizeof (msg));
1530 msg.tid = htonl (t->id.tid);
1531 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1533 if (fc == &t->prev_fc)
1537 else if (fc == &t->next_fc)
1546 send_prebuilt_message (&msg.header, peer, t);
1547 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1548 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1554 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1555 * to obtain a local peer -> destination path and interning the peer ids.
1557 * @return Newly allocated and created path
1559 static struct MeshPeerPath *
1560 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1561 unsigned int get_path_length,
1562 const struct GNUNET_PeerIdentity *put_path,
1563 unsigned int put_path_length)
1565 struct MeshPeerPath *p;
1571 GNUNET_PEER_change_rc (myid, 1);
1572 i = get_path_length;
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1574 for (i--; i >= 0; i--)
1576 id = GNUNET_PEER_intern (&get_path[i]);
1577 if (p->length > 0 && id == p->peers[p->length - 1])
1579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1580 GNUNET_PEER_change_rc (id, -1);
1584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1585 GNUNET_i2s (&get_path[i]));
1587 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1588 p->peers[p->length - 1] = id;
1591 i = put_path_length;
1592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1593 for (i--; i >= 0; i--)
1595 id = GNUNET_PEER_intern (&put_path[i]);
1598 /* PUT path went through us, so discard the path up until now and start
1599 * from here to get a much shorter (and loop-free) path.
1604 if (p->length > 0 && id == p->peers[p->length - 1])
1606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1607 GNUNET_PEER_change_rc (id, -1);
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1612 GNUNET_i2s (&put_path[i]));
1614 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1615 p->peers[p->length - 1] = id;
1619 if (get_path_length > 0)
1620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1621 GNUNET_i2s (&get_path[0]));
1622 if (put_path_length > 0)
1623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1624 GNUNET_i2s (&put_path[0]));
1625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1627 for (i = 0; i < p->length; i++)
1629 struct GNUNET_PeerIdentity peer_id;
1631 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1633 GNUNET_i2s (&peer_id));
1641 * Adds a path to the peer_infos of all the peers in the path
1643 * @param p Path to process.
1644 * @param confirmed Whether we know if the path works or not.
1647 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1651 /* TODO: invert and add */
1652 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1653 for (i++; i < p->length; i++)
1655 struct MeshPeerInfo *aux;
1656 struct MeshPeerPath *copy;
1658 aux = peer_get_short (p->peers[i]);
1659 copy = path_duplicate (p);
1660 copy->length = i + 1;
1661 peer_info_add_path (aux, copy, GNUNET_NO);
1667 * Send keepalive packets for a peer
1669 * @param cls Closure (tunnel for which to send the keepalive).
1670 * @param tc Notification context.
1673 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1677 * Search for a tunnel among the incoming tunnels
1679 * @param tid the local id of the tunnel
1681 * @return tunnel handler, NULL if doesn't exist
1683 static struct MeshTunnel *
1684 tunnel_get_incoming (MESH_TunnelNumber tid)
1686 struct GNUNET_HashCode hash;
1688 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1689 GMC_hash32 (tid, &hash);
1690 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1695 * Search for a tunnel among the tunnels for a client
1697 * @param c the client whose tunnels to search in
1698 * @param tid the local id of the tunnel
1700 * @return tunnel handler, NULL if doesn't exist
1702 static struct MeshTunnel *
1703 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1705 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1707 return tunnel_get_incoming (tid);
1711 struct GNUNET_HashCode hash;
1713 GMC_hash32 (tid, &hash);
1714 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1720 * Search for a tunnel by global ID using PEER_ID
1722 * @param pi owner of the tunnel
1723 * @param tid global tunnel number
1725 * @return tunnel handler, NULL if doesn't exist
1727 static struct MeshTunnel *
1728 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1730 struct MESH_TunnelID id;
1731 struct GNUNET_HashCode hash;
1736 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1737 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1742 * Search for a tunnel by global ID using full PeerIdentities
1744 * @param oid owner of the tunnel
1745 * @param tid global tunnel number
1747 * @return tunnel handler, NULL if doesn't exist
1749 static struct MeshTunnel *
1750 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1752 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1757 * Add a client to a tunnel, initializing all needed data structures.
1759 * FIXME: make static after implementing port numbers
1761 * @param t Tunnel to which add the client.
1762 * @param c Client which to add to the tunnel.
1765 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1767 if (NULL != t->client)
1772 if (0 != t->next_hop)
1782 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1786 for (i = 0; i < p->length; i++)
1788 if (p->peers[i] == myid)
1791 if (i > p->length - 1)
1797 if (i < p->length - 1)
1798 t->next_hop = p->peers[i + 1];
1802 t->prev_hop = p->peers[i - 1];
1806 if (NULL != t->path)
1807 path_destroy (t->path);
1808 t->path = path_duplicate (p);
1809 if (GNUNET_SCHEDULER_NO_TASK == t->maintenance_task)
1810 t->maintenance_task =
1811 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
1816 * Notifies a tunnel that a connection has broken that affects at least
1817 * some of its peers. Sends a notification towards the root of the tree.
1818 * In case the peer is the owner of the tree, notifies the client that owns
1819 * the tunnel and tries to reconnect.
1821 * @param t Tunnel affected.
1822 * @param p1 Peer that got disconnected from p2.
1823 * @param p2 Peer that got disconnected from p1.
1825 * @return Short ID of the peer disconnected (either p1 or p2).
1826 * 0 if the tunnel remained unaffected.
1828 static GNUNET_PEER_Id
1829 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1832 // if (myid != p1 && myid != p2) FIXME
1837 // if (tree_get_predecessor (t->tree) != 0)
1839 // /* We are the peer still connected, notify owner of the disconnection. */
1840 // struct GNUNET_MESH_PathBroken msg;
1841 // struct GNUNET_PeerIdentity neighbor;
1843 // msg.header.size = htons (sizeof (msg));
1844 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1845 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1846 // msg.tid = htonl (t->id.tid);
1847 // msg.peer1 = my_full_id;
1848 // GNUNET_PEER_resolve (pid, &msg.peer2);
1849 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1850 // send_prebuilt_message (&msg.header, &neighbor, t);
1857 * Build a local ACK message and send it to a local client.
1859 * @param t Tunnel on which to send the ACK.
1860 * @param c Client to whom send the ACK.
1861 * @param ack Value of the ACK.
1864 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
1866 struct GNUNET_MESH_LocalAck msg;
1868 msg.header.size = htons (sizeof (msg));
1869 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1870 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
1871 msg.max_pid = htonl (ack);
1872 GNUNET_SERVER_notification_context_unicast(nc,
1879 * Build an ACK message and queue it to send to the given peer.
1881 * @param t Tunnel on which to send the ACK.
1882 * @param peer Peer to whom send the ACK.
1883 * @param ack Value of the ACK.
1886 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1888 struct GNUNET_MESH_ACK msg;
1890 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1891 msg.header.size = htons (sizeof (msg));
1892 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1893 msg.pid = htonl (ack);
1894 msg.tid = htonl (t->id.tid);
1896 send_prebuilt_message (&msg.header, peer, t);
1901 * Send an ACK informing the predecessor about the available buffer space.
1902 * In case there is no predecessor, inform the owning client.
1903 * If buffering is off, send only on behalf of children or self if endpoint.
1904 * If buffering is on, send when sent to children and buffer space is free.
1905 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
1906 * the ACK itself goes "back" (towards root).
1908 * @param t Tunnel on which to send the ACK.
1909 * @param type Type of message that triggered the ACK transmission.
1912 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
1916 /* Is it after unicast retransmission? */
1919 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1921 "ACK due to FWD DATA retransmission\n");
1922 if (GNUNET_YES == t->nobuffer)
1924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
1928 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1929 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1931 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1932 t->force_ack = GNUNET_YES;
1938 /* Check if we need to transmit the ACK */
1939 if (t->queue_max > t->next_fc.queue_n * 4 &&
1940 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
1941 GNUNET_NO == t->force_ack)
1943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
1944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1945 " t->qmax: %u, t->qn: %u\n",
1946 t->queue_max, t->next_fc.queue_n);
1947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1948 " t->pid: %u, t->ack: %u\n",
1949 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
1953 /* Ok, ACK might be necessary, what PID to ACK? */
1954 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
1955 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
1957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
1961 t->prev_fc.last_ack_sent = ack;
1962 if (0 != t->prev_hop)
1963 send_ack (t, t->prev_hop, ack);
1964 else if (NULL != t->owner)
1965 send_local_ack (t, t->owner, ack);
1969 t->force_ack = GNUNET_NO;
1974 * Send an ACK informing the children node/client about the available
1976 * If buffering is off, send only on behalf of root (can be self).
1977 * If buffering is on, send when sent to predecessor and buffer space is free.
1978 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
1979 * the ACK itself goes "forward" (towards children/clients).
1981 * @param t Tunnel on which to send the ACK.
1982 * @param type Type of message that triggered the ACK transmission.
1985 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
1988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1989 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
1990 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
1991 /* Is it after data to_origin retransmission? */
1994 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1995 if (GNUNET_YES == t->nobuffer)
1997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1998 " Not sending ACK, nobuffer + traffic\n");
2002 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2003 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2005 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2006 t->force_ack = GNUNET_YES;
2012 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2014 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2016 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2019 " Not sending ACK, not needed\n");
2022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2023 " Sending BCK ACK %u (last sent: %u)\n",
2024 ack, t->next_fc.last_ack_sent);
2025 t->next_fc.last_ack_sent = ack;
2027 if (0 != t->next_hop)
2028 send_ack (t, t->next_hop, ack);
2029 else if (NULL != t->client)
2030 send_local_ack (t, t->client, ack);
2033 t->force_ack = GNUNET_NO;
2038 * @brief Re-initiate traffic to this peer if necessary.
2040 * Check if there is traffic queued towards this peer
2041 * and the core transmit handle is NULL (traffic was stalled).
2042 * If so, call core tmt rdy.
2044 * @param peer_id Short ID of peer to which initiate traffic.
2047 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2049 struct MeshPeerInfo *peer;
2050 struct GNUNET_PeerIdentity id;
2051 struct MeshPeerQueue *q;
2054 peer = peer_get_short (peer_id);
2055 if (NULL != peer->core_transmit)
2058 q = queue_get_next (peer);
2061 /* Might br multicast traffic already sent to this particular peer but
2062 * not to other children in this tunnel.
2063 * This way t->queue_n would be > 0 but the queue of this particular peer
2069 GNUNET_PEER_resolve (peer->id, &id);
2070 peer->core_transmit =
2071 GNUNET_CORE_notify_transmit_ready(core_handle,
2074 GNUNET_TIME_UNIT_FOREVER_REL,
2084 * Send a message to all peers in this tunnel that the tunnel is no longer
2087 * @param t The tunnel whose peers to notify.
2090 tunnel_send_destroy (struct MeshTunnel *t)
2092 struct GNUNET_MESH_TunnelDestroy msg;
2093 struct GNUNET_PeerIdentity id;
2095 msg.header.size = htons (sizeof (msg));
2096 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2097 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2098 msg.tid = htonl (t->id.tid);
2099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2100 " sending tunnel destroy for tunnel: %s [%X]\n",
2101 GNUNET_i2s (&msg.oid), t->id.tid);
2103 if (0 != t->next_hop)
2105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2106 GNUNET_PEER_resolve (t->next_hop, &id);
2107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2108 " sending forward to %s\n",
2110 send_prebuilt_message (&msg.header, t->next_hop, t);
2112 if (0 != t->prev_hop)
2114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2115 GNUNET_PEER_resolve (t->prev_hop, &id);
2116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2117 " sending back to %s\n",
2119 send_prebuilt_message (&msg.header, t->prev_hop, t);
2125 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2127 * @param t Tunnel which to cancel.
2128 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2131 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2133 struct MeshPeerInfo *peer_info;
2134 struct MeshPeerQueue *pq;
2135 struct MeshPeerQueue *next;
2137 peer_info = peer_get_short (neighbor);
2138 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2141 if (pq->tunnel == t)
2143 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2144 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2146 /* Should have been removed on destroy children */
2149 queue_destroy (pq, GNUNET_YES);
2152 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2154 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2155 peer_info->core_transmit = NULL;
2161 * Destroy the tunnel and free any allocated resources linked to it.
2163 * @param t the tunnel to destroy
2165 * @return GNUNET_OK on success
2168 tunnel_destroy (struct MeshTunnel *t)
2170 struct MeshClient *c;
2171 struct GNUNET_HashCode hash;
2181 struct GNUNET_PeerIdentity id;
2183 GNUNET_PEER_resolve (t->id.oid, &id);
2184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2185 GNUNET_i2s (&id), t->id.tid);
2187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2191 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2192 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2200 GMC_hash32 (t->local_tid, &hash);
2202 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2209 if (NULL != t->client)
2211 GMC_hash32 (t->local_tid_dest, &hash);
2213 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2220 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2226 peer_cancel_queues (t->next_hop, t);
2227 peer_cancel_queues (t->prev_hop, t);
2229 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2230 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2233 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2238 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
2241 * Tunnel is empty: destroy it.
2243 * @param t Tunnel to destroy.
2246 tunnel_destroy_empty (struct MeshTunnel *t)
2250 struct GNUNET_PeerIdentity id;
2252 GNUNET_PEER_resolve (t->id.oid, &id);
2253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2254 "executing destruction of empty tunnel %s [%X]\n",
2255 GNUNET_i2s (&id), t->id.tid);
2259 tunnel_send_destroy (t);
2260 if (0 == t->pending_messages)
2263 t->destroy = GNUNET_YES;
2267 * Initialize a Flow Control structure to the initial state.
2269 * @param fc Flow Control structure to initialize.
2272 fc_init (struct MeshFlowControl *fc)
2274 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2275 fc->last_pid_recv = (uint32_t) -1;
2276 fc->last_ack_sent = INITIAL_WINDOW_SIZE - 1;
2277 fc->last_ack_recv = INITIAL_WINDOW_SIZE - 1;
2278 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2279 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2284 * Create a new tunnel
2286 * @param owner Who is the owner of the tunnel (short ID).
2287 * @param tid Tunnel Number of the tunnel.
2288 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2289 * @param local Tunnel Number for the tunnel, for the client point of view.
2291 * @return A new initialized tunnel. NULL on error.
2293 static struct MeshTunnel *
2294 tunnel_new (GNUNET_PEER_Id owner,
2295 MESH_TunnelNumber tid,
2296 struct MeshClient *client,
2297 MESH_TunnelNumber local)
2299 struct MeshTunnel *t;
2300 struct GNUNET_HashCode hash;
2302 if (n_tunnels >= max_tunnels && NULL == client)
2305 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2308 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2310 fc_init (&t->next_fc);
2311 fc_init (&t->prev_fc);
2312 t->local_tid = local;
2314 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2316 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2318 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2319 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2326 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2333 GMC_hash32 (t->local_tid, &hash);
2335 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2336 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2340 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2351 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2352 * client when the client disconnects. If the client is not the owner, the
2353 * owner will get notified if no more clients are in the tunnel and the client
2354 * get removed from the tunnel's list.
2356 * @param cls closure (client that is disconnecting)
2357 * @param key the hash of the local tunnel id (used to access the hashmap)
2358 * @param value the value stored at the key (tunnel to destroy)
2360 * @return GNUNET_OK, keep iterating.
2363 tunnel_destroy_iterator (void *cls,
2364 const struct GNUNET_HashCode * key,
2367 struct MeshTunnel *t = value;
2368 struct MeshClient *c = cls;
2370 send_client_tunnel_destroy (t);
2373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
2374 client_delete_tunnel (c, t);
2375 tunnel_destroy_empty (t);
2378 tunnel_send_destroy (t);
2380 t->destroy = GNUNET_YES;
2387 * Timeout function, destroys tunnel if called
2389 * @param cls Closure (tunnel to destroy).
2390 * @param tc TaskContext
2393 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2395 struct MeshTunnel *t = cls;
2396 struct GNUNET_PeerIdentity id;
2398 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2399 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2401 GNUNET_PEER_resolve(t->id.oid, &id);
2402 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2403 "Tunnel %s [%X] timed out. Destroying.\n",
2404 GNUNET_i2s(&id), t->id.tid);
2405 send_client_tunnel_destroy (t);
2411 * Resets the tunnel timeout. Starts it if no timeout was running.
2413 * @param t Tunnel whose timeout to reset.
2415 * TODO use heap to improve efficiency of scheduler.
2418 tunnel_reset_timeout (struct MeshTunnel *t)
2420 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2422 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2423 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2424 t->maintenance_task =
2425 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2426 (refresh_path_time, 4), &tunnel_timeout, t);
2430 /******************************************************************************/
2431 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2432 /******************************************************************************/
2435 * Function to send a create path packet to a peer.
2437 * @param cls closure
2438 * @param size number of bytes available in buf
2439 * @param buf where the callee should write the message
2440 * @return number of bytes written to buf
2443 send_core_path_create (void *cls, size_t size, void *buf)
2445 struct MeshTunnel *t = cls;
2446 struct GNUNET_MESH_CreateTunnel *msg;
2447 struct GNUNET_PeerIdentity *peer_ptr;
2448 struct MeshPeerPath *p = t->path;
2453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2455 sizeof (struct GNUNET_MESH_CreateTunnel) +
2456 p->length * sizeof (struct GNUNET_PeerIdentity);
2458 if (size < size_needed || NULL == buf)
2463 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2464 msg->header.size = htons (size_needed);
2465 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2466 msg->tid = ntohl (t->id.tid);
2469 if (GNUNET_YES == t->nobuffer)
2470 opt |= MESH_TUNNEL_OPT_NOBUFFER;
2471 msg->opt = htonl (opt);
2472 msg->port = htonl (t->port);
2474 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2475 for (i = 0; i < p->length; i++)
2477 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2481 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2487 * Creates a path ack message in buf and frees all unused resources.
2489 * @param cls closure (MeshTransmissionDescriptor)
2490 * @param size number of bytes available in buf
2491 * @param buf where the callee should write the message
2492 * @return number of bytes written to buf
2495 send_core_path_ack (void *cls, size_t size, void *buf)
2497 struct MESH_TunnelID *id = cls;
2498 struct GNUNET_MESH_PathACK *msg = buf;
2500 GNUNET_assert (NULL != id);
2501 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2506 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2507 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2508 GNUNET_PEER_resolve (id->oid, &msg->oid);
2509 msg->tid = htonl (id->tid);
2510 msg->peer_id = my_full_id;
2512 /* TODO add signature */
2514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2515 return sizeof (struct GNUNET_MESH_PathACK);
2520 * Free a transmission that was already queued with all resources
2521 * associated to the request.
2523 * @param queue Queue handler to cancel.
2524 * @param clear_cls Is it necessary to free associated cls?
2527 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2529 struct MeshFlowControl *fc;
2531 if (GNUNET_YES == clear_cls)
2533 switch (queue->type)
2535 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2536 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2537 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2539 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2540 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2541 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2542 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2543 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2545 " prebuilt message\n");
2546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2548 GNUNET_MESH_DEBUG_M2S (queue->type));
2550 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2555 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2556 " type %s unknown!\n",
2557 GNUNET_MESH_DEBUG_M2S (queue->type));
2559 GNUNET_free_non_null (queue->cls);
2561 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2562 queue->peer->queue_tail,
2565 /* Delete from appropiate fc in the tunnel */
2566 if (queue->peer->id == queue->tunnel->next_hop)
2567 fc = &queue->tunnel->next_fc;
2568 else if (queue->peer->id == queue->tunnel->next_hop)
2569 fc = &queue->tunnel->next_fc;
2576 GNUNET_free (queue);
2581 * @brief Get the next transmittable message from the queue.
2583 * This will be the head, except in the case of being a data packet
2584 * not allowed by the destination peer.
2586 * @param peer Destination peer.
2588 * @return The next viable MeshPeerQueue element to send to that peer.
2589 * NULL when there are no transmittable messages.
2591 struct MeshPeerQueue *
2592 queue_get_next (const struct MeshPeerInfo *peer)
2594 struct MeshPeerQueue *q;
2596 struct GNUNET_MESH_Unicast *ucast;
2597 struct GNUNET_MESH_ToOrigin *to_orig;
2598 struct MeshTunnel* t;
2602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
2603 for (q = peer->queue_head; NULL != q; q = q->next)
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2608 GNUNET_MESH_DEBUG_M2S(q->type));
2611 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2612 ucast = (struct GNUNET_MESH_Unicast *) q->cls;
2613 pid = ntohl (ucast->pid);
2614 ack = t->next_fc.last_ack_recv;
2616 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2617 to_orig = (struct GNUNET_MESH_ToOrigin *) q->cls;
2618 pid = ntohl (to_orig->pid);
2619 ack = t->prev_fc.last_ack_recv;
2622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2627 "********* ACK: %u, PID: %u\n",
2629 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2638 "********* NEXT!\n");
2641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2642 "********* nothing found\n");
2648 queue_send (void *cls, size_t size, void *buf)
2650 struct MeshPeerInfo *peer = cls;
2651 struct GNUNET_MessageHeader *msg;
2652 struct MeshPeerQueue *queue;
2653 struct MeshTunnel *t;
2654 struct GNUNET_PeerIdentity dst_id;
2655 struct MeshFlowControl *fc;
2659 peer->core_transmit = NULL;
2661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
2662 queue = queue_get_next (peer);
2664 /* Queue has no internal mesh traffic nor sendable payload */
2667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
2668 if (NULL == peer->queue_head)
2669 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
2672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
2674 GNUNET_PEER_resolve (peer->id, &dst_id);
2675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2676 "********* towards %s\n",
2677 GNUNET_i2s (&dst_id));
2678 /* Check if buffer size is enough for the message */
2679 if (queue->size > size)
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2682 "********* not enough room, reissue\n");
2683 peer->core_transmit =
2684 GNUNET_CORE_notify_transmit_ready (core_handle,
2687 GNUNET_TIME_UNIT_FOREVER_REL,
2694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
2697 GNUNET_assert (0 < t->pending_messages);
2698 t->pending_messages--;
2702 switch (queue->type)
2705 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2706 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2707 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
2708 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
2709 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2710 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2712 "********* raw: %s\n",
2713 GNUNET_MESH_DEBUG_M2S (queue->type));
2715 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2716 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2717 data_size = send_core_data_raw (queue->cls, size, buf);
2718 msg = (struct GNUNET_MessageHeader *) buf;
2719 type = ntohs (msg->type);
2721 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
2723 data_size = send_core_path_create (queue->cls, size, buf);
2725 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
2727 data_size = send_core_path_ack (queue->cls, size, buf);
2731 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2732 "********* type unknown: %u\n",
2737 /* Free queue, but cls was freed by send_core_* */
2738 queue_destroy (queue, GNUNET_NO);
2740 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
2743 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2744 t->next_fc.last_pid_sent = ((struct GNUNET_MESH_Unicast *) buf)->pid;
2745 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
2747 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2748 t->prev_fc.last_pid_sent = ((struct GNUNET_MESH_ToOrigin *) buf)->pid;
2749 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
2755 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
2757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
2761 /* If more data in queue, send next */
2762 queue = queue_get_next (peer);
2765 struct GNUNET_PeerIdentity id;
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
2768 GNUNET_PEER_resolve (peer->id, &id);
2769 peer->core_transmit =
2770 GNUNET_CORE_notify_transmit_ready(core_handle,
2773 GNUNET_TIME_UNIT_FOREVER_REL,
2779 else if (NULL != peer->queue_head)
2781 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2782 "********* %s stalled\n",
2783 GNUNET_i2s (&my_full_id));
2784 if (peer->id == t->next_hop)
2786 else if (peer->id == t->prev_hop)
2793 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
2796 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Return %d\n", data_size);
2806 * @brief Queue and pass message to core when possible.
2808 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
2809 * accounts for it. In case the queue is full, the message is dropped and
2812 * Otherwise, message is treated as internal and allowed to go regardless of
2815 * @param cls Closure (@c type dependant). It will be used by queue_send to
2816 * build the message to be sent if not already prebuilt.
2817 * @param type Type of the message, 0 for a raw message.
2818 * @param size Size of the message.
2819 * @param dst Neighbor to send message to.
2820 * @param t Tunnel this message belongs to.
2823 queue_add (void *cls, uint16_t type, size_t size,
2824 struct MeshPeerInfo *dst, struct MeshTunnel *t)
2826 struct MeshPeerQueue *queue;
2827 struct GNUNET_PeerIdentity id;
2831 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2833 n = &t->next_fc.queue_n;
2835 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
2837 n = &t->prev_fc.queue_n;
2841 if (*n >= t->queue_max)
2844 GNUNET_STATISTICS_update(stats,
2845 "# messages dropped (buffer full)",
2847 return; /* Drop message */
2851 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2857 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
2858 if (NULL == dst->core_transmit)
2860 GNUNET_PEER_resolve (dst->id, &id);
2861 dst->core_transmit =
2862 GNUNET_CORE_notify_transmit_ready (core_handle,
2865 GNUNET_TIME_UNIT_FOREVER_REL,
2871 t->pending_messages++;
2875 /******************************************************************************/
2876 /******************** MESH NETWORK HANDLERS **************************/
2877 /******************************************************************************/
2881 * Core handler for path creation
2883 * @param cls closure
2884 * @param message message
2885 * @param peer peer identity this notification is about
2887 * @return GNUNET_OK to keep the connection open,
2888 * GNUNET_SYSERR to close it (signal serious error)
2891 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2892 const struct GNUNET_MessageHeader *message)
2894 unsigned int own_pos;
2897 MESH_TunnelNumber tid;
2898 struct GNUNET_MESH_CreateTunnel *msg;
2899 struct GNUNET_PeerIdentity *pi;
2900 struct GNUNET_HashCode hash;
2901 struct MeshPeerPath *path;
2902 struct MeshPeerInfo *dest_peer_info;
2903 struct MeshPeerInfo *orig_peer_info;
2904 struct MeshTunnel *t;
2906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2907 "Received a path create msg [%s]\n",
2908 GNUNET_i2s (&my_full_id));
2909 size = ntohs (message->size);
2910 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
2912 GNUNET_break_op (0);
2916 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
2917 if (size % sizeof (struct GNUNET_PeerIdentity))
2919 GNUNET_break_op (0);
2922 size /= sizeof (struct GNUNET_PeerIdentity);
2925 GNUNET_break_op (0);
2928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2929 msg = (struct GNUNET_MESH_CreateTunnel *) message;
2931 tid = ntohl (msg->tid);
2932 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2934 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2935 t = tunnel_get (pi, tid);
2940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2941 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
2947 t->port = ntohl (msg->port);
2948 opt = ntohl (msg->opt);
2949 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
2951 t->nobuffer = GNUNET_YES;
2952 t->prev_fc.last_ack_sent = t->prev_fc.last_pid_recv + 1;
2953 t->next_fc.last_ack_sent = t->next_fc.last_pid_recv + 1;
2956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
2958 tunnel_reset_timeout (t);
2959 GMC_hash32 (t->local_tid_dest, &hash);
2961 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2962 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2971 GNUNET_break_op (0);
2973 t->state = MESH_TUNNEL_WAITING;
2975 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2976 if (NULL == dest_peer_info)
2978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2979 " Creating PeerInfo for destination.\n");
2980 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2981 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2982 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2984 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2986 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2987 if (NULL == orig_peer_info)
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2990 " Creating PeerInfo for origin.\n");
2991 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2992 orig_peer_info->id = GNUNET_PEER_intern (pi);
2993 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2994 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
2997 path = path_new (size);
2999 for (i = 0; i < size; i++)
3001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3002 GNUNET_i2s (&pi[i]));
3003 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3004 if (path->peers[i] == myid)
3007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3010 /* cannot be self, must be 'not found' */
3011 /* create path: self not found in path through self */
3012 GNUNET_break_op (0);
3013 path_destroy (path);
3017 path_add_to_peers (path, GNUNET_NO);
3018 t->prev_hop = path->peers[own_pos - 1];
3019 GNUNET_PEER_change_rc (t->prev_hop, 1);
3020 if (own_pos == size - 1)
3022 struct MeshClient *c;
3023 struct GNUNET_HashCode hc;
3025 /* Find target client */
3026 GMC_hash32 (t->port, &hc);
3027 c = GNUNET_CONTAINER_multihashmap_get (ports, &hc);
3030 /* TODO send reject */
3035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3036 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3039 /* Assign local tid */
3040 while (NULL != tunnel_get_incoming (next_local_tid))
3041 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3042 t->local_tid_dest = next_local_tid++;
3043 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3045 send_client_tunnel_create (t);
3050 struct MeshPeerPath *path2;
3052 t->next_hop = path->peers[own_pos + 1];
3053 GNUNET_PEER_change_rc(t->next_hop, 1);
3055 /* It's for somebody else! Retransmit. */
3056 path2 = path_duplicate (path);
3057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3058 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3059 path2 = path_duplicate (path);
3060 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3061 send_create_path (t);
3068 * Core handler for notifications of broken paths
3070 * @param cls closure
3071 * @param message message
3072 * @param peer peer identity this notification is about
3074 * @return GNUNET_OK to keep the connection open,
3075 * GNUNET_SYSERR to close it (signal serious error)
3078 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3079 const struct GNUNET_MessageHeader *message)
3081 struct GNUNET_MESH_PathBroken *msg;
3082 struct MeshTunnel *t;
3084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3085 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3086 msg = (struct GNUNET_MESH_PathBroken *) message;
3087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3088 GNUNET_i2s (&msg->peer1));
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3090 GNUNET_i2s (&msg->peer2));
3091 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3094 GNUNET_break_op (0);
3097 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3098 GNUNET_PEER_search (&msg->peer2));
3105 * Core handler for tunnel destruction
3107 * @param cls closure
3108 * @param message message
3109 * @param peer peer identity this notification is about
3111 * @return GNUNET_OK to keep the connection open,
3112 * GNUNET_SYSERR to close it (signal serious error)
3115 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3116 const struct GNUNET_MessageHeader *message)
3118 struct GNUNET_MESH_TunnelDestroy *msg;
3119 struct MeshTunnel *t;
3121 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3123 "Got a TUNNEL DESTROY packet from %s\n",
3125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3126 " for tunnel %s [%u]\n",
3127 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3128 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3131 /* Probably already got the message from another path,
3132 * destroyed the tunnel and retransmitted to children.
3135 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3139 // TODO check owner's signature
3140 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3142 /* Tunnel was incoming, notify clients */
3143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3144 t->local_tid, t->local_tid_dest);
3145 send_client_tunnel_destroy (t);
3147 tunnel_send_destroy (t);
3148 t->destroy = GNUNET_YES;
3149 // TODO: add timeout to destroy the tunnel anyway
3155 * Core handler for mesh network traffic going from the origin to a peer
3157 * @param cls closure
3158 * @param peer peer identity this notification is about
3159 * @param message message
3160 * @return GNUNET_OK to keep the connection open,
3161 * GNUNET_SYSERR to close it (signal serious error)
3164 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3165 const struct GNUNET_MessageHeader *message)
3167 struct GNUNET_MESH_Unicast *msg;
3168 struct MeshTunnel *t;
3173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3176 size = ntohs (message->size);
3178 sizeof (struct GNUNET_MESH_Unicast) +
3179 sizeof (struct GNUNET_MessageHeader))
3184 msg = (struct GNUNET_MESH_Unicast *) message;
3185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3186 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3188 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3191 /* TODO notify back: we don't know this tunnel */
3192 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3193 GNUNET_break_op (0);
3196 pid = ntohl (msg->pid);
3197 if (t->prev_fc.last_pid_recv == pid)
3199 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3200 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3201 " Already seen pid %u, DROPPING!\n", pid);
3206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3207 " pid %u not seen yet, forwarding\n", pid);
3210 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3212 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3213 GNUNET_break_op (0);
3214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3215 "Received PID %u, ACK %u\n",
3216 pid, t->prev_fc.last_ack_sent);
3217 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3220 t->prev_fc.last_pid_recv = pid;
3222 tunnel_reset_timeout (t);
3223 if (t->dest == myid)
3225 if (NULL == t->client)
3230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3231 " it's for us! sending to clients...\n");
3232 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3233 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
3234 message, GNUNET_NO);
3235 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3238 if (0 == t->next_hop)
3243 ttl = ntohl (msg->ttl);
3244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3247 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3248 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3249 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3253 " not for us, retransmitting...\n");
3255 send_prebuilt_message (message, t->next_hop, t);
3256 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3262 * Core handler for mesh network traffic toward the owner of a tunnel
3264 * @param cls closure
3265 * @param message message
3266 * @param peer peer identity this notification is about
3268 * @return GNUNET_OK to keep the connection open,
3269 * GNUNET_SYSERR to close it (signal serious error)
3272 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3273 const struct GNUNET_MessageHeader *message)
3275 struct GNUNET_MESH_ToOrigin *msg;
3276 struct MeshTunnel *t;
3280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3282 size = ntohs (message->size);
3283 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3284 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3286 GNUNET_break_op (0);
3289 msg = (struct GNUNET_MESH_ToOrigin *) message;
3290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3291 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3292 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3293 pid = ntohl (msg->pid);
3297 /* TODO notify that we dont know this tunnel (whom)? */
3298 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3300 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3301 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3305 if (t->next_fc.last_pid_recv == pid)
3307 /* already seen this packet, drop */
3308 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3310 " Already seen pid %u, DROPPING!\n", pid);
3311 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3315 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3317 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3318 GNUNET_break_op (0);
3319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3320 "Received PID %u, ACK %u\n",
3321 pid, t->next_fc.last_ack_sent);
3322 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3325 t->next_fc.last_pid_recv = pid;
3326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3327 " pid %u not seen yet, forwarding\n", pid);
3329 if (NULL != t->owner)
3332 struct GNUNET_MESH_ToOrigin *copy;
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3335 " it's for us! sending to clients...\n");
3336 /* TODO signature verification */
3337 memcpy (cbuf, message, size);
3338 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3339 copy->tid = htonl (t->local_tid);
3340 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3341 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3342 ©->header, GNUNET_NO);
3343 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3347 " not for us, retransmitting...\n");
3349 if (0 == t->prev_hop) /* No owner AND no prev hop */
3351 if (GNUNET_YES == t->destroy)
3353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3354 "to orig received on a dying tunnel %s [%X]\n",
3355 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3358 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3359 "unknown to origin at %s\n",
3360 GNUNET_i2s (&my_full_id));
3361 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3364 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3365 "on tunnel %s [%X]\n",
3366 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3369 send_prebuilt_message (message, t->prev_hop, t);
3370 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3377 * Core handler for mesh network traffic point-to-point acks.
3379 * @param cls closure
3380 * @param message message
3381 * @param peer peer identity this notification is about
3383 * @return GNUNET_OK to keep the connection open,
3384 * GNUNET_SYSERR to close it (signal serious error)
3387 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3388 const struct GNUNET_MessageHeader *message)
3390 struct GNUNET_MESH_ACK *msg;
3391 struct MeshTunnel *t;
3395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3397 msg = (struct GNUNET_MESH_ACK *) message;
3399 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3403 /* TODO notify that we dont know this tunnel (whom)? */
3404 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3407 ack = ntohl (msg->pid);
3408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3410 /* Is this a forward or backward ACK? */
3411 id = GNUNET_PEER_search (peer);
3412 if (t->next_hop == id)
3415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3416 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task &&
3417 GMC_is_pid_bigger (ack, t->next_fc.last_ack_recv))
3419 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3420 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3421 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3423 t->next_fc.last_ack_recv = ack;
3424 peer_unlock_queue (t->next_hop);
3425 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3427 else if (t->prev_hop == id)
3430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3431 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
3432 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
3434 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3435 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3436 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3438 t->prev_fc.last_ack_recv = ack;
3439 peer_unlock_queue (t->prev_hop);
3440 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3443 GNUNET_break_op (0);
3449 * Core handler for mesh network traffic point-to-point ack polls.
3451 * @param cls closure
3452 * @param message message
3453 * @param peer peer identity this notification is about
3455 * @return GNUNET_OK to keep the connection open,
3456 * GNUNET_SYSERR to close it (signal serious error)
3459 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3460 const struct GNUNET_MessageHeader *message)
3462 struct GNUNET_MESH_Poll *msg;
3463 struct MeshTunnel *t;
3466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3469 msg = (struct GNUNET_MESH_Poll *) message;
3471 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3475 /* TODO notify that we dont know this tunnel (whom)? */
3476 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
3477 GNUNET_break_op (0);
3481 /* Is this a forward or backward ACK? */
3482 id = GNUNET_PEER_search(peer);
3483 if (t->next_hop == id)
3485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
3486 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3488 else if (t->prev_hop == id)
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
3491 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3500 * Core handler for path ACKs
3502 * @param cls closure
3503 * @param message message
3504 * @param peer peer identity this notification is about
3506 * @return GNUNET_OK to keep the connection open,
3507 * GNUNET_SYSERR to close it (signal serious error)
3510 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3511 const struct GNUNET_MessageHeader *message)
3513 struct GNUNET_MESH_PathACK *msg;
3514 struct MeshPeerInfo *peer_info;
3515 struct MeshPeerPath *p;
3516 struct MeshTunnel *t;
3518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3519 GNUNET_i2s (&my_full_id));
3520 msg = (struct GNUNET_MESH_PathACK *) message;
3521 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3524 /* TODO notify that we don't know the tunnel */
3525 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3527 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3531 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3533 peer_info = peer_get (&msg->peer_id);
3534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3535 GNUNET_i2s (&msg->peer_id));
3536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3539 /* Add path to peers? */
3543 path_add_to_peers (p, GNUNET_YES);
3549 t->state = MESH_TUNNEL_READY;
3551 /* Message for us? */
3552 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3555 if (NULL == t->owner)
3557 GNUNET_break_op (0);
3560 if (NULL != peer_info->dhtget)
3562 GNUNET_DHT_get_stop (peer_info->dhtget);
3563 peer_info->dhtget = NULL;
3568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3569 " not for us, retransmitting...\n");
3570 peer_info = peer_get (&msg->oid);
3571 send_prebuilt_message (message, t->prev_hop, t);
3577 * Core handler for mesh keepalives.
3579 * @param cls closure
3580 * @param message message
3581 * @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)
3585 * TODO: Check who we got this from, to validate route.
3588 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
3589 const struct GNUNET_MessageHeader *message)
3591 struct GNUNET_MESH_TunnelKeepAlive *msg;
3592 struct MeshTunnel *t;
3594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
3597 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
3598 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3602 /* TODO notify that we dont know that tunnel */
3603 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
3608 tunnel_reset_timeout (t);
3610 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
3611 send_prebuilt_message (message, t->next_hop, t);
3618 * Functions to handle messages from core
3620 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3621 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3622 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3623 sizeof (struct GNUNET_MESH_PathBroken)},
3624 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
3625 sizeof (struct GNUNET_MESH_TunnelDestroy)},
3626 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3627 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
3628 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
3629 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3630 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
3631 sizeof (struct GNUNET_MESH_ACK)},
3632 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
3633 sizeof (struct GNUNET_MESH_Poll)},
3634 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3635 sizeof (struct GNUNET_MESH_PathACK)},
3641 /******************************************************************************/
3642 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3643 /******************************************************************************/
3648 * notify_client_connection_failure: notify a client that the connection to the
3649 * requested remote peer is not possible (for instance, no route found)
3650 * Function called when the socket is ready to queue more data. "buf" will be
3651 * NULL and "size" zero if the socket was closed for writing in the meantime.
3653 * @param cls closure
3654 * @param size number of bytes available in buf
3655 * @param buf where the callee should write the message
3656 * @return number of bytes written to buf
3659 notify_client_connection_failure (void *cls, size_t size, void *buf)
3662 struct MeshPeerInfo *peer_info;
3663 struct GNUNET_MESH_PeerControl *msg;
3664 struct GNUNET_PeerIdentity id;
3666 if (0 == size && NULL == buf)
3668 // TODO retry? cancel?
3672 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3673 peer_info = (struct MeshPeerInfo *) cls;
3674 msg = (struct GNUNET_MESH_PeerControl *) buf;
3675 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3676 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3677 // msg->tunnel_id = htonl(peer_info->t->tid);
3678 GNUNET_PEER_resolve (peer_info->id, &id);
3679 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3687 * Send keepalive packets for a tunnel.
3689 * @param cls Closure (tunnel for which to send the keepalive).
3690 * @param tc Notification context.
3693 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3695 struct MeshTunnel *t = cls;
3696 struct GNUNET_MESH_TunnelKeepAlive *msg;
3697 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
3700 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3701 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
3702 NULL == t->owner || 0 == t->local_tid || 0 != t->prev_hop)
3707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3708 "sending keepalive for tunnel %d\n", t->id.tid);
3710 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
3711 msg->header.size = htons (size);
3712 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3713 msg->oid = my_full_id;
3714 msg->tid = htonl (t->id.tid);
3715 send_prebuilt_message (&msg->header, t->next_hop, t);
3717 t->maintenance_task =
3718 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3723 * Function to process paths received for a new peer addition. The recorded
3724 * paths form the initial tunnel, which can be optimized later.
3725 * Called on each result obtained for the DHT search.
3727 * @param cls closure
3728 * @param exp when will this value expire
3729 * @param key key of the result
3730 * @param get_path path of the get request
3731 * @param get_path_length lenght of get_path
3732 * @param put_path path of the put request
3733 * @param put_path_length length of the put_path
3734 * @param type type of the result
3735 * @param size number of bytes in data
3736 * @param data pointer to the result data
3739 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3740 const struct GNUNET_HashCode * key,
3741 const struct GNUNET_PeerIdentity *get_path,
3742 unsigned int get_path_length,
3743 const struct GNUNET_PeerIdentity *put_path,
3744 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3745 size_t size, const void *data)
3747 struct MeshPeerInfo *peer = cls;
3748 struct MeshPeerPath *p;
3749 struct GNUNET_PeerIdentity pi;
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3753 GNUNET_PEER_resolve (peer->id, &pi);
3754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3756 p = path_build_from_dht (get_path, get_path_length,
3757 put_path, put_path_length);
3758 path_add_to_peers (p, GNUNET_NO);
3760 for (i = 0; i < peer->ntunnels; i++)
3762 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
3763 peer_connect (peer, peer->tunnels[i]);
3770 /******************************************************************************/
3771 /********************* MESH LOCAL HANDLES **************************/
3772 /******************************************************************************/
3776 * Handler for client disconnection
3778 * @param cls closure
3779 * @param client identification of the client; NULL
3780 * for the last call when the server is destroyed
3783 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3785 struct MeshClient *c;
3786 struct MeshClient *next;
3788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3798 if (c->handle != client)
3800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3806 GNUNET_SERVER_client_drop (c->handle);
3807 c->shutting_down = GNUNET_YES;
3808 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3809 &tunnel_destroy_iterator, c);
3810 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3811 &tunnel_destroy_iterator, c);
3812 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3813 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3815 if (NULL != c->ports)
3816 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
3818 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
3819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3821 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
3824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
3830 * Handler for new clients
3832 * @param cls closure
3833 * @param client identification of the client
3834 * @param message the actual message, which includes messages the client wants
3837 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3838 const struct GNUNET_MessageHeader *message)
3840 struct GNUNET_MESH_ClientConnect *cc_msg;
3841 struct MeshClient *c;
3846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3848 /* Check data sanity */
3849 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3850 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3851 if (0 != (size % sizeof (uint32_t)))
3854 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3857 size /= sizeof (uint32_t);
3859 /* Create new client structure */
3860 c = GNUNET_malloc (sizeof (struct MeshClient));
3861 c->id = next_client_id++; /* overflow not important: just for debug */
3862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
3863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
3865 GNUNET_SERVER_client_keep (client);
3869 struct GNUNET_HashCode hc;
3871 p = (uint32_t *) &cc_msg[1];
3872 c->ports = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
3873 for (i = 0; i < size; i++)
3876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
3877 GMC_hash32 (u32, &hc);
3879 /* store in client's hashmap */
3880 GNUNET_CONTAINER_multihashmap_put (c->ports, &hc, c,
3881 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3882 /* store in global hashmap */
3883 /* FIXME only allow one client to have the port open,
3884 * have a backup hashmap with waiting clients */
3885 GNUNET_CONTAINER_multihashmap_put (ports, &hc, c,
3886 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3890 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
3891 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
3892 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
3893 GNUNET_SERVER_notification_context_add (nc, client);
3894 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
3896 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3902 * Handler for requests of new tunnels
3904 * @param cls Closure.
3905 * @param client Identification of the client.
3906 * @param message The actual message.
3909 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3910 const struct GNUNET_MessageHeader *message)
3912 struct GNUNET_MESH_TunnelMessage *t_msg;
3913 struct MeshPeerInfo *peer_info;
3914 struct MeshTunnel *t;
3915 struct MeshClient *c;
3916 MESH_TunnelNumber tid;
3918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3920 /* Sanity check for client registration */
3921 if (NULL == (c = client_get (client)))
3924 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3929 /* Message sanity check */
3930 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3933 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3937 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
3939 GNUNET_i2s (&t_msg->peer));
3940 /* Sanity check for tunnel numbering */
3941 tid = ntohl (t_msg->tunnel_id);
3942 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3945 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3948 /* Sanity check for duplicate tunnel IDs */
3949 if (NULL != tunnel_get_by_local_id (c, tid))
3952 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3956 while (NULL != tunnel_get_by_pi (myid, next_tid))
3957 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3958 t = tunnel_new (myid, next_tid, c, tid);
3959 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3963 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3966 t->port = ntohl (t->port);
3967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
3968 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3970 peer_info = peer_get (&t_msg->peer);
3971 GNUNET_array_append (peer_info->tunnels, peer_info->ntunnels, t);
3972 peer_connect (peer_info, t);
3973 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3979 * Handler for requests of deleting tunnels
3981 * @param cls closure
3982 * @param client identification of the client
3983 * @param message the actual message
3986 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3987 const struct GNUNET_MessageHeader *message)
3989 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3990 struct MeshClient *c;
3991 struct MeshTunnel *t;
3992 MESH_TunnelNumber tid;
3994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3995 "Got a DESTROY TUNNEL from client!\n");
3997 /* Sanity check for client registration */
3998 if (NULL == (c = client_get (client)))
4001 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4006 /* Message sanity check */
4007 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4010 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4014 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4016 /* Retrieve tunnel */
4017 tid = ntohl (tunnel_msg->tunnel_id);
4018 t = tunnel_get_by_local_id(c, tid);
4021 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4023 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4028 tunnel_destroy_empty (t);
4029 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4032 send_client_tunnel_destroy (t);
4033 client_delete_tunnel (c, t);
4035 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4037 tunnel_send_destroy (t);
4038 peer_remove_tunnel (peer_get_short(t->dest), t);
4039 t->destroy = GNUNET_YES;
4040 /* The tunnel will be destroyed when the last message is transmitted. */
4041 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4047 * Handler for requests of seeting tunnel's buffering policy.
4049 * @param cls Closure (unused).
4050 * @param client Identification of the client.
4051 * @param message The actual message.
4054 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
4055 const struct GNUNET_MessageHeader *message)
4057 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4058 struct MeshClient *c;
4059 struct MeshTunnel *t;
4060 MESH_TunnelNumber tid;
4062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4063 "Got a BUFFER request from client!\n");
4065 /* Sanity check for client registration */
4066 if (NULL == (c = client_get (client)))
4069 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4074 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4076 /* Retrieve tunnel */
4077 tid = ntohl (tunnel_msg->tunnel_id);
4078 t = tunnel_get_by_local_id(c, tid);
4081 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4083 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4087 switch (ntohs(message->type))
4089 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
4090 t->nobuffer = GNUNET_NO;
4092 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
4093 t->nobuffer = GNUNET_YES;
4099 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4104 * Handler for client traffic directed to one peer
4106 * @param cls closure
4107 * @param client identification of the client
4108 * @param message the actual message
4111 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4112 const struct GNUNET_MessageHeader *message)
4114 struct MeshClient *c;
4115 struct MeshTunnel *t;
4116 struct GNUNET_MESH_Unicast *data_msg;
4117 MESH_TunnelNumber tid;
4120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4121 "Got a unicast request from a client!\n");
4123 /* Sanity check for client registration */
4124 if (NULL == (c = client_get (client)))
4127 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4132 data_msg = (struct GNUNET_MESH_Unicast *) message;
4134 /* Sanity check for message size */
4135 size = ntohs (message->size);
4136 if (sizeof (struct GNUNET_MESH_Unicast) +
4137 sizeof (struct GNUNET_MessageHeader) > size)
4140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4144 /* Tunnel exists? */
4145 tid = ntohl (data_msg->tid);
4146 t = tunnel_get_by_local_id (c, tid);
4150 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4154 /* Is it a local tunnel? Then, does client own the tunnel? */
4155 if (t->owner->handle != client)
4158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4162 /* PID should be as expected: client<->service communication */
4163 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4166 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4167 "Unicast PID, expected %u, got %u\n",
4168 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4169 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4173 /* Ok, everything is correct, send the message
4174 * (pretend we got it from a mesh peer)
4177 /* Work around const limitation */
4178 char buf[ntohs (message->size)] GNUNET_ALIGN;
4179 struct GNUNET_MESH_Unicast *copy;
4181 copy = (struct GNUNET_MESH_Unicast *) buf;
4182 memcpy (buf, data_msg, size);
4183 copy->oid = my_full_id;
4184 copy->tid = htonl (t->id.tid);
4185 copy->ttl = htonl (default_ttl);
4186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4187 " calling generic handler...\n");
4188 handle_mesh_unicast (NULL, &my_full_id, ©->header);
4190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4191 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4198 * Handler for client traffic directed to the origin
4200 * @param cls closure
4201 * @param client identification of the client
4202 * @param message the actual message
4205 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4206 const struct GNUNET_MessageHeader *message)
4208 struct GNUNET_MESH_ToOrigin *data_msg;
4209 struct MeshFlowControl *fc;
4210 struct MeshClient *c;
4211 struct MeshTunnel *t;
4212 MESH_TunnelNumber tid;
4215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4216 "Got a ToOrigin request from a client!\n");
4217 /* Sanity check for client registration */
4218 if (NULL == (c = client_get (client)))
4221 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4226 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4228 /* Sanity check for message size */
4229 size = ntohs (message->size);
4230 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4231 sizeof (struct GNUNET_MessageHeader) > size)
4234 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4238 /* Tunnel exists? */
4239 tid = ntohl (data_msg->tid);
4240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4241 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4244 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4247 t = tunnel_get_by_local_id (c, tid);
4250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4251 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4253 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4257 /* It should be sent by someone who has this as incoming tunnel. */
4261 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4265 /* PID should be as expected */
4267 if (ntohl (data_msg->pid) != fc->last_pid_recv + 1)
4270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4271 "To Origin PID, expected %u, got %u\n",
4272 fc->last_pid_recv + 1,
4273 ntohl (data_msg->pid));
4274 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4278 /* Ok, everything is correct, send the message
4279 * (pretend we got it from a mesh peer)
4282 char buf[ntohs (message->size)] GNUNET_ALIGN;
4283 struct GNUNET_MESH_ToOrigin *copy;
4285 /* Work around 'const' limitation */
4286 memcpy (buf, data_msg, size);
4287 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4288 GNUNET_PEER_resolve (t->id.oid, ©->oid);
4289 copy->tid = htonl (t->id.tid);
4290 copy->ttl = htonl (default_ttl);
4292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4293 " calling generic handler...\n");
4294 handle_mesh_to_orig (NULL, &my_full_id, ©->header);
4296 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4303 * Handler for client's ACKs for payload traffic.
4305 * @param cls Closure (unused).
4306 * @param client Identification of the client.
4307 * @param message The actual message.
4310 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4311 const struct GNUNET_MessageHeader *message)
4313 struct GNUNET_MESH_LocalAck *msg;
4314 struct MeshTunnel *t;
4315 struct MeshClient *c;
4316 MESH_TunnelNumber tid;
4319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4320 /* Sanity check for client registration */
4321 if (NULL == (c = client_get (client)))
4324 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4329 msg = (struct GNUNET_MESH_LocalAck *) message;
4331 /* Tunnel exists? */
4332 tid = ntohl (msg->tunnel_id);
4333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4334 t = tunnel_get_by_local_id (c, tid);
4338 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4340 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4344 ack = ntohl (msg->max_pid);
4345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4347 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4350 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4351 t->prev_fc.last_ack_recv = ack;
4352 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4356 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4357 t->next_fc.last_ack_recv = ack;
4358 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4361 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4369 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4371 * @param cls Closure (client handle).
4372 * @param key Key (hashed tunnel ID, unused).
4373 * @param value Tunnel info.
4375 * @return GNUNET_YES, to keep iterating.
4378 monitor_all_tunnels_iterator (void *cls,
4379 const struct GNUNET_HashCode * key,
4382 struct GNUNET_SERVER_Client *client = cls;
4383 struct MeshTunnel *t = value;
4384 struct GNUNET_MESH_LocalMonitor *msg;
4386 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4387 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4388 msg->tunnel_id = htonl (t->id.tid);
4389 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4390 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4391 GNUNET_PEER_resolve (t->dest, &msg->destination);
4393 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4394 "* sending info about tunnel %s [%u]\n",
4395 GNUNET_i2s (&msg->owner), t->id.tid);
4397 GNUNET_SERVER_notification_context_unicast (nc, client,
4398 &msg->header, GNUNET_NO);
4404 * Handler for client's MONITOR request.
4406 * @param cls Closure (unused).
4407 * @param client Identification of the client.
4408 * @param message The actual message.
4411 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4412 const struct GNUNET_MessageHeader *message)
4414 struct MeshClient *c;
4416 /* Sanity check for client registration */
4417 if (NULL == (c = client_get (client)))
4420 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4424 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4425 "Received get tunnels request from client %u\n",
4427 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4428 monitor_all_tunnels_iterator,
4430 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4431 "Get tunnels request from client %u completed\n",
4433 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4438 * Handler for client's MONITOR_TUNNEL request.
4440 * @param cls Closure (unused).
4441 * @param client Identification of the client.
4442 * @param message The actual message.
4445 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4446 const struct GNUNET_MessageHeader *message)
4448 const struct GNUNET_MESH_LocalMonitor *msg;
4449 struct GNUNET_MESH_LocalMonitor *resp;
4450 struct MeshClient *c;
4451 struct MeshTunnel *t;
4453 /* Sanity check for client registration */
4454 if (NULL == (c = client_get (client)))
4457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4461 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4462 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4463 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4466 ntohl (msg->tunnel_id));
4467 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4470 /* We don't know the tunnel FIXME */
4471 struct GNUNET_MESH_LocalMonitor warn;
4474 GNUNET_SERVER_notification_context_unicast (nc, client,
4477 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4481 /* Initialize context */
4482 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4484 GNUNET_PEER_resolve (t->dest, &resp->destination);
4485 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4486 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4487 &resp->header, GNUNET_NO);
4490 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4491 "Monitor tunnel request from client %u completed\n",
4493 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4498 * Functions to handle messages from clients
4500 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4501 {&handle_local_new_client, NULL,
4502 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4503 {&handle_local_tunnel_create, NULL,
4504 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4505 sizeof (struct GNUNET_MESH_TunnelMessage)},
4506 {&handle_local_tunnel_destroy, NULL,
4507 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4508 sizeof (struct GNUNET_MESH_TunnelMessage)},
4509 {&handle_local_tunnel_buffer, NULL,
4510 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
4511 sizeof (struct GNUNET_MESH_TunnelMessage)},
4512 {&handle_local_tunnel_buffer, NULL,
4513 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
4514 sizeof (struct GNUNET_MESH_TunnelMessage)},
4515 {&handle_local_unicast, NULL,
4516 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4517 {&handle_local_to_origin, NULL,
4518 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4519 {&handle_local_ack, NULL,
4520 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4521 sizeof (struct GNUNET_MESH_LocalAck)},
4522 {&handle_local_get_tunnels, NULL,
4523 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4524 sizeof (struct GNUNET_MessageHeader)},
4525 {&handle_local_show_tunnel, NULL,
4526 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4527 sizeof (struct GNUNET_MESH_LocalMonitor)},
4533 * Method called whenever a given peer connects.
4535 * @param cls closure
4536 * @param peer peer identity this notification is about
4539 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4541 struct MeshPeerInfo *peer_info;
4542 struct MeshPeerPath *path;
4544 DEBUG_CONN ("Peer connected\n");
4545 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4546 peer_info = peer_get (peer);
4547 if (myid == peer_info->id)
4549 DEBUG_CONN (" (self)\n");
4554 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4556 path = path_new (2);
4557 path->peers[0] = myid;
4558 path->peers[1] = peer_info->id;
4559 GNUNET_PEER_change_rc (myid, 1);
4560 GNUNET_PEER_change_rc (peer_info->id, 1);
4561 peer_info_add_path (peer_info, path, GNUNET_YES);
4562 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4568 * Method called whenever a peer disconnects.
4570 * @param cls closure
4571 * @param peer peer identity this notification is about
4574 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4576 struct MeshPeerInfo *pi;
4577 struct MeshPeerQueue *q;
4578 struct MeshPeerQueue *n;
4580 DEBUG_CONN ("Peer disconnected\n");
4581 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4591 /* TODO try to reroute this traffic instead */
4592 queue_destroy(q, GNUNET_YES);
4595 if (NULL != pi->core_transmit)
4597 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4598 pi->core_transmit = NULL;
4600 peer_remove_path (pi, pi->id, myid);
4603 DEBUG_CONN (" (self)\n");
4605 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
4611 * To be called on core init/fail.
4613 * @param cls Closure (config)
4614 * @param server handle to the server for this service
4615 * @param identity the public identity of this peer
4618 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4619 const struct GNUNET_PeerIdentity *identity)
4621 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4625 core_handle = server;
4626 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4629 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4630 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4632 GNUNET_i2s (identity));
4633 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4635 GNUNET_i2s (&my_full_id));
4636 GNUNET_CORE_disconnect (core_handle);
4637 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4638 NULL, /* Closure passed to MESH functions */
4639 &core_init, /* Call core_init once connected */
4640 &core_connect, /* Handle connects */
4641 &core_disconnect, /* remove peers on disconnects */
4642 NULL, /* Don't notify about all incoming messages */
4643 GNUNET_NO, /* For header only in notification */
4644 NULL, /* Don't notify about all outbound messages */
4645 GNUNET_NO, /* For header-only out notification */
4646 core_handlers); /* Register these handlers */
4654 /******************************************************************************/
4655 /************************ MAIN FUNCTIONS ****************************/
4656 /******************************************************************************/
4659 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4661 * @param cls closure
4662 * @param key current key code
4663 * @param value value in the hash map
4664 * @return GNUNET_YES if we should continue to iterate,
4668 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
4670 struct MeshTunnel *t = value;
4677 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4679 * @param cls closure
4680 * @param key current key code
4681 * @param value value in the hash map
4682 * @return GNUNET_YES if we should continue to iterate,
4686 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
4688 struct MeshPeerInfo *p = value;
4689 struct MeshPeerQueue *q;
4690 struct MeshPeerQueue *n;
4698 queue_destroy(q, GNUNET_YES);
4702 peer_info_destroy (p);
4708 * Task run during shutdown.
4714 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4718 if (core_handle != NULL)
4720 GNUNET_CORE_disconnect (core_handle);
4725 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
4728 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4729 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4730 if (dht_handle != NULL)
4732 GNUNET_DHT_disconnect (dht_handle);
4737 GNUNET_SERVER_notification_context_destroy (nc);
4740 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4742 GNUNET_SCHEDULER_cancel (announce_id_task);
4743 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4750 * Callback for hostkey read/generation.
4752 * @param cls Closure (Configuration handle).
4753 * @param pk The ECC private key.
4754 * @param emsg Error message, if any.
4757 key_generation_cb (void *cls,
4758 struct GNUNET_CRYPTO_EccPrivateKey *pk,
4761 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4762 struct MeshPeerInfo *peer;
4763 struct MeshPeerPath *p;
4768 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4769 _("Mesh service could not access hostkey: %s. Exiting.\n"),
4771 GNUNET_SCHEDULER_shutdown ();
4774 my_private_key = pk;
4775 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
4776 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4777 &my_full_id.hashPubKey);
4778 myid = GNUNET_PEER_intern (&my_full_id);
4779 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4780 "Mesh for peer [%s] starting\n",
4781 GNUNET_i2s(&my_full_id));
4783 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4784 NULL, /* Closure passed to MESH functions */
4785 &core_init, /* Call core_init once connected */
4786 &core_connect, /* Handle connects */
4787 &core_disconnect, /* remove peers on disconnects */
4788 NULL, /* Don't notify about all incoming messages */
4789 GNUNET_NO, /* For header only in notification */
4790 NULL, /* Don't notify about all outbound messages */
4791 GNUNET_NO, /* For header-only out notification */
4792 core_handlers); /* Register these handlers */
4793 if (core_handle == NULL)
4796 GNUNET_SCHEDULER_shutdown ();
4801 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4804 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4805 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
4806 GNUNET_SERVER_disconnect_notify (server_handle,
4807 &handle_local_client_disconnect, NULL);
4810 clients_head = NULL;
4811 clients_tail = NULL;
4814 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4816 /* Create a peer_info for the local peer */
4817 peer = peer_get (&my_full_id);
4820 GNUNET_PEER_change_rc (myid, 1);
4821 peer_info_add_path (peer, p, GNUNET_YES);
4822 GNUNET_SERVER_resume (server_handle);
4823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
4828 * Process mesh requests.
4830 * @param cls closure
4831 * @param server the initialized server
4832 * @param c configuration to use
4835 run (void *cls, struct GNUNET_SERVER_Handle *server,
4836 const struct GNUNET_CONFIGURATION_Handle *c)
4840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4841 server_handle = server;
4844 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
4847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4849 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4850 "mesh", "peer/privatekey");
4851 GNUNET_SCHEDULER_shutdown ();
4856 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
4857 &refresh_path_time))
4859 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4861 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4862 "mesh", "refresh path time");
4863 GNUNET_SCHEDULER_shutdown ();
4868 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
4871 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4873 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4874 "mesh", "id announce time");
4875 GNUNET_SCHEDULER_shutdown ();
4880 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
4883 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4885 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4886 "mesh", "connect timeout");
4887 GNUNET_SCHEDULER_shutdown ();
4892 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
4895 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4897 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4898 "mesh", "max msgs queue");
4899 GNUNET_SCHEDULER_shutdown ();
4904 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
4907 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4909 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4910 "mesh", "max tunnels");
4911 GNUNET_SCHEDULER_shutdown ();
4916 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
4919 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4921 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4922 "mesh", "default ttl", 64);
4927 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
4930 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4931 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4932 "mesh", "max peers", 1000);
4937 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
4938 &dht_replication_level))
4940 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4942 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4943 "mesh", "dht replication level", 3);
4944 dht_replication_level = 3;
4947 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4948 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4949 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4950 ports = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4952 dht_handle = GNUNET_DHT_connect (c, 64);
4953 if (NULL == dht_handle)
4957 stats = GNUNET_STATISTICS_create ("mesh", c);
4959 GNUNET_SERVER_suspend (server_handle);
4960 /* Scheduled the task to clean up when shutdown is called */
4961 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4963 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
4966 GNUNET_free (keyfile);
4971 * The main function for the mesh service.
4973 * @param argc number of arguments from the command line
4974 * @param argv command line arguments
4975 * @return 0 ok, 1 on error
4978 main (int argc, char *const *argv)
4983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
4984 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4986 ret = (GNUNET_OK == r) ? 0 : 1;
4987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
4991 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4992 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
4993 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);