2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
41 * - relay corking down to core
42 * - set ttl relative to tree depth
43 * - Add data ACK count in path ACK
44 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
50 #include "mesh2_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "gnunet_dht_service.h"
54 #include "gnunet_statistics_service.h"
56 #define MESH_BLOOM_SIZE 128
58 #define MESH_DEBUG_DHT GNUNET_NO
59 #define MESH_DEBUG_CONNECTION GNUNET_NO
60 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
62 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
63 GNUNET_TIME_UNIT_MINUTES,\
66 #if MESH_DEBUG_CONNECTION
67 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
69 #define DEBUG_CONN(...)
73 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
75 #define DEBUG_DHT(...)
82 struct timespec __mesh_start;
83 struct timespec __mesh_end;
84 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
85 #define INTERVAL_END \
87 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
88 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
89 if (__diff < 0) __diff += 1000000000;\
93 #define INTERVAL_SHOW \
95 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
97 #define INTERVAL_START
102 /******************************************************************************/
103 /************************ DATA STRUCTURES ****************************/
104 /******************************************************************************/
106 /** FWD declaration */
112 * Struct containing info about a queued transmission to this peer
119 struct MeshPeerQueue *next;
124 struct MeshPeerQueue *prev;
127 * Peer this transmission is directed to.
129 struct MeshPeerInfo *peer;
132 * Tunnel this message belongs to.
134 struct MeshTunnel *tunnel;
137 * Pointer to info stucture used as cls.
147 * Size of the message
154 * Struct containing all info possibly needed to build a package when called
157 struct MeshTransmissionDescriptor
159 /** ID of the tunnel this packet travels in */
160 struct MESH_TunnelID *origin;
162 /** Who was this message being sent to */
163 struct MeshPeerInfo *peer;
165 /** Ultimate destination of the packet */
166 GNUNET_PEER_Id destination;
168 /** Tunnel it belongs to. */
169 struct MeshTunnel *t;
171 /** Size of the data. */
180 * Struct containing all information regarding a given peer
190 * Last time we heard from this peer
192 struct GNUNET_TIME_Absolute last_contact;
195 * Number of attempts to reconnect so far
197 int n_reconnect_attempts;
200 * Paths to reach the peer, ordered by ascending hop count
202 struct MeshPeerPath *path_head;
205 * Paths to reach the peer, ordered by ascending hop count
207 struct MeshPeerPath *path_tail;
210 * Handle to stop the DHT search for a path to this peer
212 struct GNUNET_DHT_GetHandle *dhtget;
215 * Array of tunnels this peer is the target of.
216 * Most probably a small amount, therefore not a hashmap.
217 * When the path to the peer changes, notify these tunnels to let them
218 * re-adjust their path trees.
220 struct MeshTunnel **tunnels;
223 * Number of tunnels this peers participates in
225 unsigned int ntunnels;
228 * Transmission queue to core DLL head
230 struct MeshPeerQueue *queue_head;
233 * Transmission queue to core DLL tail
235 struct MeshPeerQueue *queue_tail;
238 * How many messages are in the queue to this peer.
240 unsigned int queue_n;
243 * Handle to for queued transmissions
245 struct GNUNET_CORE_TransmitHandle *core_transmit;
250 * Struct to encapsulate all the Flow Control information to a peer in the
251 * context of a tunnel: Same peer in different tunnels will have independent
252 * flow control structures, allowing to choke/free tunnels according to its
255 struct MeshFlowControl
258 * ID of the last packet sent towards the peer.
260 uint32_t last_pid_sent;
263 * ID of the last packet received from the peer.
265 uint32_t last_pid_recv;
268 * Last ACK sent to the peer (peer can't send more than this PID).
270 uint32_t last_ack_sent;
273 * Last ACK sent towards the origin (for traffic towards leaf node).
275 uint32_t last_ack_recv;
278 * How many messages are in the queue towards this peer.
283 * Task to poll the peer in case of a lost ACK causes stall.
285 GNUNET_SCHEDULER_TaskIdentifier poll_task;
288 * How frequently to poll for ACKs.
290 struct GNUNET_TIME_Relative poll_time;
293 * On which tunnel to poll.
294 * Using an explicit poll_ctx would not help memory wise,
295 * since the allocated context would have to be stored in the
296 * fc struct in order to free it upon cancelling poll_task.
298 struct MeshTunnel *t;
303 * Globally unique tunnel identification (owner + number)
304 * DO NOT USE OVER THE NETWORK
309 * Node that owns the tunnel
314 * Tunnel number to differentiate all the tunnels owned by the node oid
315 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
317 MESH_TunnelNumber tid;
322 * Struct containing all information regarding a tunnel
323 * For an intermediate node the improtant info used will be:
324 * - id Tunnel unique identification
325 * - paths[0] To know where to send it next
326 * - metainfo: ready, speeds, accounting
333 struct MESH_TunnelID id;
336 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
338 MESH_TunnelNumber local_tid;
341 * Local tunnel number for local destination clients (incoming number)
342 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
345 MESH_TunnelNumber local_tid_dest;
348 * Is the tunnel bufferless (minimum latency)?
353 * Force sending ACK? Flag to allow duplicate ACK on POLL.
358 * How many messages do we accept in the forward queue.
360 unsigned int queue_max;
363 * Last time the tunnel was used
365 struct GNUNET_TIME_Absolute timestamp;
368 * Destination of the tunnel.
373 * Next hop in the tunnel. If 0, @c client must be set.
375 GNUNET_PEER_Id next_hop;
378 * Previous hop in the tunnel. If 0, @c owner must be set.
380 GNUNET_PEER_Id prev_hop;
383 * Flow control information about @c next_hop or @c client.
385 struct MeshFlowControl next_fc;
388 * Flow control information about @c prev_hop or @c owner.
390 struct MeshFlowControl prev_fc;
393 * Client owner of the tunnel, if any
395 struct MeshClient *owner;
398 * Client destination of the tunnel, if any.
400 struct MeshClient *client;
403 * Task to keep the used paths alive at the owner,
404 * time tunnel out on all the other peers.
406 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
409 * Path being used for the tunnel.
411 struct MeshPeerPath *path;
414 * Flag to signal the destruction of the tunnel.
415 * If this is set GNUNET_YES the tunnel will be destroyed
416 * when the queue is empty.
421 * Total messages pending for this tunnels, payload or not.
423 unsigned int pending_messages;
428 * Struct containing information about a client of the service
430 * TODO: add a list of 'waiting' types
437 struct MeshClient *next;
442 struct MeshClient *prev;
445 * Tunnels that belong to this client, indexed by local id
447 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
450 * Tunnels this client has accepted, indexed by incoming local id
452 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
455 * Handle to communicate with the client
457 struct GNUNET_SERVER_Client *handle;
460 * Messages that this client has declared interest in.
461 * Indexed by a GMC_hash32 (type), contains *Client.
463 struct GNUNET_CONTAINER_MultiHashMap *types;
466 * Whether the client is active or shutting down (don't send confirmations
467 * to a client that is shutting down.
472 * ID of the client, mainly for debug messages
479 /******************************************************************************/
480 /************************ DEBUG FUNCTIONS ****************************/
481 /******************************************************************************/
485 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
486 * @param cls string to print
487 * @param success GNUNET_OK if the PUT was transmitted,
488 * GNUNET_NO on timeout,
489 * GNUNET_SYSERR on disconnect from service
490 * after the PUT message was transmitted
491 * (so we don't know if it was received or not)
496 mesh_debug (void *cls, int success)
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
504 unsigned int debug_fwd_ack;
505 unsigned int debug_bck_ack;
509 /******************************************************************************/
510 /*********************** GLOBAL VARIABLES ****************************/
511 /******************************************************************************/
513 /************************** Configuration parameters **************************/
516 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
518 static struct GNUNET_TIME_Relative refresh_path_time;
521 * How often to PUT own ID in the DHT.
523 static struct GNUNET_TIME_Relative id_announce_time;
526 * Maximum time allowed to connect to a peer found by string.
528 static struct GNUNET_TIME_Relative connect_timeout;
531 * Default TTL for payload packets.
533 static unsigned long long default_ttl;
536 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
538 static unsigned long long dht_replication_level;
541 * How many tunnels are we willing to maintain.
542 * Local tunnels are always allowed, even if there are more tunnels than max.
544 static unsigned long long max_tunnels;
547 * How many messages *in total* are we willing to queue, divided by number of
548 * tunnels to get tunnel queue size.
550 static unsigned long long max_msgs_queue;
553 * How many peers do we want to remember?
555 static unsigned long long max_peers;
558 /*************************** Static global variables **************************/
561 * Hostkey generation context
563 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
566 * DLL with all the clients, head.
568 static struct MeshClient *clients_head;
571 * DLL with all the clients, tail.
573 static struct MeshClient *clients_tail;
576 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
578 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
581 * Number of tunnels known.
583 static unsigned long long n_tunnels;
586 * Tunnels incoming, indexed by MESH_TunnelNumber
587 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
589 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
592 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
594 static struct GNUNET_CONTAINER_MultiHashMap *peers;
597 * Handle to communicate with transport
599 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
602 * Handle to communicate with core.
604 static struct GNUNET_CORE_Handle *core_handle;
609 static struct GNUNET_DHT_Handle *dht_handle;
614 static struct GNUNET_SERVER_Handle *server_handle;
617 * Handle to the statistics service.
619 static struct GNUNET_STATISTICS_Handle *stats;
622 * Notification context, to send messages to local clients.
624 static struct GNUNET_SERVER_NotificationContext *nc;
627 * Local peer own ID (memory efficient handle).
629 static GNUNET_PEER_Id myid;
632 * Local peer own ID (full value).
634 static struct GNUNET_PeerIdentity my_full_id;
639 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
644 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
647 * Tunnel ID for the next created tunnel (global tunnel number).
649 static MESH_TunnelNumber next_tid;
652 * Tunnel ID for the next incoming tunnel (local tunnel number).
654 static MESH_TunnelNumber next_local_tid;
657 * All message types clients of this peer are interested in.
659 static struct GNUNET_CONTAINER_MultiHashMap *types;
662 * Task to periodically announce itself in the network.
664 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
667 * Next ID to assign to a client.
669 unsigned int next_client_id;
672 /******************************************************************************/
673 /*********************** DECLARATIONS **************************/
674 /******************************************************************************/
677 * Function to process paths received for a new peer addition. The recorded
678 * paths form the initial tunnel, which can be optimized later.
679 * Called on each result obtained for the DHT search.
682 * @param exp when will this value expire
683 * @param key key of the result
684 * @param type type of the result
685 * @param size number of bytes in data
686 * @param data pointer to the result data
689 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
690 const struct GNUNET_HashCode * key,
691 const struct GNUNET_PeerIdentity *get_path,
692 unsigned int get_path_length,
693 const struct GNUNET_PeerIdentity *put_path,
694 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
695 size_t size, const void *data);
699 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
700 * and insert it in the appropiate structures if the peer is not known yet.
702 * @param peer Full identity of the peer.
704 * @return Existing or newly created peer info.
706 static struct MeshPeerInfo *
707 peer_get (const struct GNUNET_PeerIdentity *peer);
711 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
712 * and insert it in the appropiate structures if the peer is not known yet.
714 * @param peer Short identity of the peer.
716 * @return Existing or newly created peer info.
718 static struct MeshPeerInfo *
719 peer_get_short (const GNUNET_PEER_Id peer);
723 * Build a PeerPath from the paths returned from the DHT, reversing the paths
724 * to obtain a local peer -> destination path and interning the peer ids.
726 * @return Newly allocated and created path
728 static struct MeshPeerPath *
729 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
730 unsigned int get_path_length,
731 const struct GNUNET_PeerIdentity *put_path,
732 unsigned int put_path_length);
736 * Adds a path to the peer_infos of all the peers in the path
738 * @param p Path to process.
739 * @param confirmed Whether we know if the path works or not.
742 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
747 * Search for a tunnel by global ID using full PeerIdentities.
749 * @param oid owner of the tunnel.
750 * @param tid global tunnel number.
752 * @return tunnel handler, NULL if doesn't exist.
754 static struct MeshTunnel *
755 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
759 * Notify a tunnel that a connection has broken that affects at least
762 * @param t Tunnel affected.
763 * @param p1 Peer that got disconnected from p2.
764 * @param p2 Peer that got disconnected from p1.
766 * @return Short ID of the peer disconnected (either p1 or p2).
767 * 0 if the tunnel remained unaffected.
769 static GNUNET_PEER_Id
770 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
775 * Use the given path for the tunnel.
777 * @param t Tunnel to update.
778 * @param p Path to use.
781 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
784 * @brief Queue and pass message to core when possible.
786 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
787 * and accounts for it. In case the queue is full, the message is dropped and
790 * Otherwise, message is treated as internal and allowed to go regardless of
793 * @param cls Closure (@c type dependant). It will be used by queue_send to
794 * build the message to be sent if not already prebuilt.
795 * @param type Type of the message, 0 for a raw message.
796 * @param size Size of the message.
797 * @param dst Neighbor to send message to.
798 * @param t Tunnel this message belongs to.
801 queue_add (void *cls, uint16_t type, size_t size,
802 struct MeshPeerInfo *dst, struct MeshTunnel *t);
806 * Free a transmission that was already queued with all resources
807 * associated to the request.
809 * @param queue Queue handler to cancel.
810 * @param clear_cls Is it necessary to free associated cls?
813 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
817 * @brief Get the next transmittable message from the queue.
819 * This will be the head, except in the case of being a data packet
820 * not allowed by the destination peer.
822 * @param peer Destination peer.
824 * @return The next viable MeshPeerQueue element to send to that peer.
825 * NULL when there are no transmittable messages.
827 struct MeshPeerQueue *
828 queue_get_next (const struct MeshPeerInfo *peer);
832 * Core callback to write a queued packet to core buffer
834 * @param cls Closure (peer info).
835 * @param size Number of bytes available in buf.
836 * @param buf Where the to write the message.
838 * @return number of bytes written to buf
841 queue_send (void *cls, size_t size, void *buf);
844 /******************************************************************************/
845 /************************ PERIODIC FUNCTIONS ****************************/
846 /******************************************************************************/
849 * Periodically announce self id in the DHT
852 * @param tc task context
855 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
859 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
861 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
865 * - Set data expiration in function of X
868 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
870 block.id = my_full_id;
871 block.type = htonl (0);
872 GNUNET_DHT_put (dht_handle, /* DHT handle */
873 &my_full_id.hashPubKey, /* Key to use */
874 dht_replication_level, /* Replication level */
875 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
876 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
877 sizeof (block), /* Size of the data */
878 (const char *) &block, /* Data itself */
879 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
880 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
881 NULL, /* Continuation */
882 NULL); /* Continuation closure */
884 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
888 /******************************************************************************/
889 /****************** GENERAL HELPER FUNCTIONS ************************/
890 /******************************************************************************/
894 * Check if client has registered with the service and has not disconnected
896 * @param client the client to check
898 * @return non-NULL if client exists in the global DLL
900 static struct MeshClient *
901 client_get (struct GNUNET_SERVER_Client *client)
903 struct MeshClient *c;
908 if (c->handle == client)
917 * Deletes a tunnel from a client (either owner or destination). To be used on
920 * @param c Client whose tunnel to delete.
921 * @param t Tunnel which should be deleted.
924 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
926 struct GNUNET_HashCode hash;
930 GMC_hash32 (t->local_tid, &hash);
931 GNUNET_assert (GNUNET_YES ==
932 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
936 else if (c == t->client)
938 GMC_hash32 (t->local_tid_dest, &hash);
939 GNUNET_assert (GNUNET_YES ==
940 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
952 * Notify all clients (not depending on registration status) that the incoming
953 * tunnel is no longer valid.
955 * @param t Tunnel that was destroyed.
958 send_client_tunnel_destroy (struct MeshTunnel *t)
960 struct GNUNET_MESH_TunnelMessage msg;
962 if (NULL == t->client)
964 msg.header.size = htons (sizeof (msg));
965 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
966 msg.tunnel_id = htonl (t->local_tid_dest);
967 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
968 &msg.header, GNUNET_NO);
973 * Iterator over all the peers to remove the oldest not-used entry.
975 * @param cls Closure (unsued).
976 * @param key ID of the peer.
977 * @param value Peer_Info of the peer.
982 peer_info_timeout (void *cls,
983 const struct GNUNET_HashCode *key,
990 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
991 * and insert it in the appropiate structures if the peer is not known yet.
993 * @param peer Full identity of the peer.
995 * @return Existing or newly created peer info.
997 static struct MeshPeerInfo *
998 peer_get (const struct GNUNET_PeerIdentity *peer)
1000 struct MeshPeerInfo *peer_info;
1002 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1003 if (NULL == peer_info)
1006 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1007 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1009 GNUNET_CONTAINER_multihashmap_iterate (peers,
1013 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1014 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1015 peer_info->id = GNUNET_PEER_intern (peer);
1017 peer_info->last_contact = GNUNET_TIME_absolute_get();
1024 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1025 * and insert it in the appropiate structures if the peer is not known yet.
1027 * @param peer Short identity of the peer.
1029 * @return Existing or newly created peer info.
1031 static struct MeshPeerInfo *
1032 peer_get_short (const GNUNET_PEER_Id peer)
1034 struct GNUNET_PeerIdentity id;
1036 GNUNET_PEER_resolve (peer, &id);
1037 return peer_get (&id);
1042 * Choose the best path towards a peer considering the tunnel properties.
1044 * @param peer The destination peer.
1045 * @param t The tunnel the path is for.
1047 * @return Best current known path towards the peer, if any.
1049 static struct MeshPeerPath *
1050 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1052 struct MeshPeerPath *best_p;
1053 struct MeshPeerPath *p;
1054 unsigned int best_cost;
1057 best_p = p = peer->path_head;
1058 best_cost = cost = p->length;
1061 if ((cost = p->length) < best_cost)
1073 * Remove the tunnel from the list of tunnels to which a peer is target.
1075 * @param peer PeerInfo of the peer.
1076 * @param t Tunnel to remove.
1079 peer_remove_tunnel (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1083 for (i = 0; i < peer->ntunnels; i++)
1086 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1089 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1091 GNUNET_realloc (peer->tunnels,
1092 peer->ntunnels * sizeof(struct MeshTunnel *));
1100 * Core callback to write a pre-constructed data packet to core buffer
1102 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1103 * @param size Number of bytes available in buf.
1104 * @param buf Where the to write the message.
1106 * @return number of bytes written to buf
1109 send_core_data_raw (void *cls, size_t size, void *buf)
1111 struct MeshTransmissionDescriptor *info = cls;
1112 struct GNUNET_MessageHeader *msg;
1115 GNUNET_assert (NULL != info);
1116 msg = (struct GNUNET_MessageHeader *) info->data;
1117 total_size = ntohs (msg->size);
1119 if (total_size > size)
1124 memcpy (buf, msg, total_size);
1125 GNUNET_free (info->data);
1132 * Sends an already built message to a peer, properly registrating
1133 * all used resources.
1135 * @param message Message to send. Function makes a copy of it.
1136 * @param peer Short ID of the neighbor whom to send the message.
1137 * @param t Tunnel on which this message is transmitted.
1140 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1141 GNUNET_PEER_Id peer,
1142 struct MeshTunnel *t)
1144 struct MeshTransmissionDescriptor *info;
1145 struct GNUNET_PeerIdentity id;
1146 struct MeshPeerInfo *neighbor;
1147 struct MeshPeerPath *p;
1151 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1156 size = ntohs (message->size);
1157 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1158 info->data = GNUNET_malloc (size);
1159 memcpy (info->data, message, size);
1160 type = ntohs(message->type);
1163 struct GNUNET_MESH_Unicast *m;
1164 struct GNUNET_MESH_ToOrigin *to;
1166 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1167 m = (struct GNUNET_MESH_Unicast *) info->data;
1168 m->ttl = htonl (ntohl (m->ttl) - 1);
1170 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1171 to = (struct GNUNET_MESH_ToOrigin *) info->data;
1172 t->prev_fc.last_pid_sent++; /* FIXME per hop? */
1173 to->pid = htonl (t->prev_fc.last_pid_sent);
1175 info->data_len = size;
1176 GNUNET_PEER_resolve (peer, &id);
1177 neighbor = peer_get (&id);
1178 for (p = neighbor->path_head; NULL != p; p = p->next)
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 " %s IS NOT DIRECTLY CONNECTED\n",
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1194 for (p = neighbor->path_head; NULL != p; p = p->next)
1196 struct GNUNET_PeerIdentity debug_id;
1199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1200 " path with %u hops through:\n",
1202 for (i = 0; i < p->length; i++)
1204 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 i, GNUNET_i2s(&debug_id));
1211 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1213 " no direct connection to %s\n",
1215 GNUNET_free (info->data);
1219 info->peer = neighbor;
1220 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1231 * Sends a CREATE PATH message for a path to a peer, properly registrating
1232 * all used resources.
1234 * @param peer PeerInfo of the final peer for whom this path is being created.
1235 * @param p Path itself.
1236 * @param t Tunnel for which the path is created.
1239 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1240 struct MeshTunnel *t)
1242 struct MeshPeerInfo *neighbor;
1250 neighbor = peer_get_short (t->next_hop);
1252 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1253 sizeof (struct GNUNET_MESH_ManipulatePath) +
1254 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1261 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1263 * @param t Tunnel which to confirm.
1266 send_path_ack (struct MeshTunnel *t)
1268 struct MeshTransmissionDescriptor *info;
1269 struct GNUNET_PeerIdentity id;
1271 GNUNET_PEER_resolve (t->prev_hop, &id);
1272 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1273 info->origin = &t->id;
1274 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1275 GNUNET_assert (NULL != info->peer);
1278 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1279 sizeof (struct GNUNET_MESH_PathACK),
1286 * Try to establish a new connection to this peer in the fiven tunnel.
1287 * If the peer doesn't have any path to it yet, try to get one.
1288 * If the peer already has some path, send a CREATE PATH towards it.
1290 * @param peer PeerInfo of the peer.
1291 * @param t Tunnel for which to create the path, if possible.
1294 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1296 struct MeshPeerPath *p;
1298 if (NULL != peer->path_head)
1300 p = peer_get_best_path (peer, t);
1301 tunnel_use_path (t, p);
1302 send_create_path (peer, p, t);
1304 else if (NULL == peer->dhtget)
1306 struct GNUNET_PeerIdentity id;
1308 GNUNET_PEER_resolve (peer->id, &id);
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1310 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1311 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1312 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1313 &id.hashPubKey, /* key to search */
1314 dht_replication_level, /* replication level */
1315 GNUNET_DHT_RO_RECORD_ROUTE |
1316 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1318 0, /* xquery bits */
1319 &dht_get_id_handler, peer);
1321 /* Otherwise, there is no path but the DHT get is already started. */
1326 * Destroy the peer_info and free any allocated resources linked to it
1328 * @param pi The peer_info to destroy.
1330 * @return GNUNET_OK on success
1333 peer_info_destroy (struct MeshPeerInfo *pi)
1335 struct GNUNET_PeerIdentity id;
1336 struct MeshPeerPath *p;
1337 struct MeshPeerPath *nextp;
1339 GNUNET_PEER_resolve (pi->id, &id);
1340 GNUNET_PEER_change_rc (pi->id, -1);
1343 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1346 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1347 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1349 if (NULL != pi->dhtget)
1351 GNUNET_DHT_get_stop (pi->dhtget);
1357 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1367 * Remove all paths that rely on a direct connection between p1 and p2
1368 * from the peer itself and notify all tunnels about it.
1370 * @param peer PeerInfo of affected peer.
1371 * @param p1 GNUNET_PEER_Id of one peer.
1372 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1375 * TODO: optimize (see below)
1378 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1381 struct MeshPeerPath *p;
1382 struct MeshPeerPath *next;
1383 struct MeshPeerInfo *peer_d;
1385 unsigned int destroyed;
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1390 for (p = peer->path_head; NULL != p; p = next)
1393 for (i = 0; i < (p->length - 1); i++)
1395 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1396 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1398 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1408 for (i = 0; i < peer->ntunnels; i++)
1410 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1414 peer_d = peer_get_short (d);
1415 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1416 tunnel_use_path (peer->tunnels[i], next);
1417 peer_connect (peer_d, peer->tunnels[i]);
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1424 * Add the path to the peer and update the path used to reach it in case this
1427 * @param peer_info Destination peer to add the path to.
1428 * @param path New path to add. Last peer must be the peer in arg 1.
1429 * Path will be either used of freed if already known.
1430 * @param trusted Do we trust that this path is real?
1433 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1436 struct MeshPeerPath *aux;
1440 if ((NULL == peer_info) || (NULL == path))
1443 path_destroy (path);
1446 if (path->peers[path->length - 1] != peer_info->id)
1449 path_destroy (path);
1452 if (path->length <= 2 && GNUNET_NO == trusted)
1454 /* Only allow CORE to tell us about direct paths */
1455 path_destroy (path);
1458 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1459 for (l = 1; l < path->length; l++)
1461 if (path->peers[l] == myid)
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1464 for (l2 = 0; l2 < path->length - l; l2++)
1466 path->peers[l2] = path->peers[l + l2];
1471 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1476 struct GNUNET_PeerIdentity id;
1478 GNUNET_PEER_resolve (peer_info->id, &id);
1479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1480 path->length, GNUNET_i2s (&id));
1483 l = path_get_length (path);
1490 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1491 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1493 l2 = path_get_length (aux);
1496 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1497 peer_info->path_tail, aux, path);
1502 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1504 path_destroy (path);
1509 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1516 * Add the path to the origin peer and update the path used to reach it in case
1517 * this is the shortest.
1518 * The path is given in peer_info -> destination, therefore we turn the path
1519 * upside down first.
1521 * @param peer_info Peer to add the path to, being the origin of the path.
1522 * @param path New path to add after being inversed.
1523 * @param trusted Do we trust that this path is real?
1526 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1527 struct MeshPeerPath *path, int trusted)
1530 peer_info_add_path (peer_info, path, trusted);
1535 * Function called if the connection to the peer has been stalled for a while,
1536 * possibly due to a missed ACK. Poll the peer about its ACK status.
1538 * @param cls Closure (poll ctx).
1539 * @param tc TaskContext.
1542 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1544 struct MeshFlowControl *fc = cls;
1545 struct GNUNET_MESH_Poll msg;
1546 struct MeshTunnel *t = fc->t;
1547 GNUNET_PEER_Id peer;
1549 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1550 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1555 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1556 msg.header.size = htons (sizeof (msg));
1557 msg.tid = htonl (t->id.tid);
1558 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1559 msg.last_ack = htonl (fc->last_ack_recv);
1561 if (fc == &t->prev_fc)
1569 send_prebuilt_message (&msg.header, peer, t);
1570 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1571 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1577 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1578 * to obtain a local peer -> destination path and interning the peer ids.
1580 * @return Newly allocated and created path
1582 static struct MeshPeerPath *
1583 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1584 unsigned int get_path_length,
1585 const struct GNUNET_PeerIdentity *put_path,
1586 unsigned int put_path_length)
1588 struct MeshPeerPath *p;
1594 GNUNET_PEER_change_rc (myid, 1);
1595 i = get_path_length;
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1597 for (i--; i >= 0; i--)
1599 id = GNUNET_PEER_intern (&get_path[i]);
1600 if (p->length > 0 && id == p->peers[p->length - 1])
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1603 GNUNET_PEER_change_rc (id, -1);
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1608 GNUNET_i2s (&get_path[i]));
1610 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1611 p->peers[p->length - 1] = id;
1614 i = put_path_length;
1615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1616 for (i--; i >= 0; i--)
1618 id = GNUNET_PEER_intern (&put_path[i]);
1621 /* PUT path went through us, so discard the path up until now and start
1622 * from here to get a much shorter (and loop-free) path.
1627 if (p->length > 0 && id == p->peers[p->length - 1])
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1630 GNUNET_PEER_change_rc (id, -1);
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1635 GNUNET_i2s (&put_path[i]));
1637 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1638 p->peers[p->length - 1] = id;
1642 if (get_path_length > 0)
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1644 GNUNET_i2s (&get_path[0]));
1645 if (put_path_length > 0)
1646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1647 GNUNET_i2s (&put_path[0]));
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1650 for (i = 0; i < p->length; i++)
1652 struct GNUNET_PeerIdentity peer_id;
1654 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1656 GNUNET_i2s (&peer_id));
1664 * Adds a path to the peer_infos of all the peers in the path
1666 * @param p Path to process.
1667 * @param confirmed Whether we know if the path works or not.
1670 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1674 /* TODO: invert and add */
1675 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1676 for (i++; i < p->length; i++)
1678 struct MeshPeerInfo *aux;
1679 struct MeshPeerPath *copy;
1681 aux = peer_get_short (p->peers[i]);
1682 copy = path_duplicate (p);
1683 copy->length = i + 1;
1684 peer_info_add_path (aux, copy, GNUNET_NO);
1690 * Send keepalive packets for a peer
1692 * @param cls Closure (tunnel for which to send the keepalive).
1693 * @param tc Notification context.
1696 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1700 * Search for a tunnel among the incoming tunnels
1702 * @param tid the local id of the tunnel
1704 * @return tunnel handler, NULL if doesn't exist
1706 static struct MeshTunnel *
1707 tunnel_get_incoming (MESH_TunnelNumber tid)
1709 struct GNUNET_HashCode hash;
1711 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1712 GMC_hash32 (tid, &hash);
1713 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1718 * Search for a tunnel among the tunnels for a client
1720 * @param c the client whose tunnels to search in
1721 * @param tid the local id of the tunnel
1723 * @return tunnel handler, NULL if doesn't exist
1725 static struct MeshTunnel *
1726 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1728 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1730 return tunnel_get_incoming (tid);
1734 struct GNUNET_HashCode hash;
1736 GMC_hash32 (tid, &hash);
1737 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1743 * Search for a tunnel by global ID using PEER_ID
1745 * @param pi owner of the tunnel
1746 * @param tid global tunnel number
1748 * @return tunnel handler, NULL if doesn't exist
1750 static struct MeshTunnel *
1751 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1753 struct MESH_TunnelID id;
1754 struct GNUNET_HashCode hash;
1759 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1760 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1765 * Search for a tunnel by global ID using full PeerIdentities
1767 * @param oid owner of the tunnel
1768 * @param tid global tunnel number
1770 * @return tunnel handler, NULL if doesn't exist
1772 static struct MeshTunnel *
1773 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1775 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1780 * Add a client to a tunnel, initializing all needed data structures.
1782 * FIXME: make static after implementing port numbers
1784 * @param t Tunnel to which add the client.
1785 * @param c Client which to add to the tunnel.
1788 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1790 if (NULL != t->client)
1795 if (0 != t->next_hop)
1801 t->next_fc.last_ack_sent = t->prev_fc.last_pid_recv + 1;
1802 t->next_fc.last_pid_sent = t->prev_fc.last_pid_recv;
1803 t->next_fc.last_ack_recv = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
1804 t->next_fc.last_pid_recv = (uint32_t) -1; /* Expected next: 0 */
1805 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
1806 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
1807 t->next_fc.queue_n = 0;
1812 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1816 for (i = 0; i < p->length; i++)
1818 if (p->peers[i] == myid)
1821 if (i > p->length - 1)
1827 if (i < p->length - 1)
1828 t->next_hop = p->peers[i + 1];
1832 t->prev_hop = p->peers[i - 1];
1836 if (NULL != t->path)
1837 path_destroy (t->path);
1838 t->path = path_duplicate (p);
1839 if (GNUNET_SCHEDULER_NO_TASK == t->maintenance_task)
1840 t->maintenance_task =
1841 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
1846 * Notifies a tunnel that a connection has broken that affects at least
1847 * some of its peers. Sends a notification towards the root of the tree.
1848 * In case the peer is the owner of the tree, notifies the client that owns
1849 * the tunnel and tries to reconnect.
1851 * @param t Tunnel affected.
1852 * @param p1 Peer that got disconnected from p2.
1853 * @param p2 Peer that got disconnected from p1.
1855 * @return Short ID of the peer disconnected (either p1 or p2).
1856 * 0 if the tunnel remained unaffected.
1858 static GNUNET_PEER_Id
1859 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1862 // if (myid != p1 && myid != p2) FIXME
1867 // if (tree_get_predecessor (t->tree) != 0)
1869 // /* We are the peer still connected, notify owner of the disconnection. */
1870 // struct GNUNET_MESH_PathBroken msg;
1871 // struct GNUNET_PeerIdentity neighbor;
1873 // msg.header.size = htons (sizeof (msg));
1874 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1875 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1876 // msg.tid = htonl (t->id.tid);
1877 // msg.peer1 = my_full_id;
1878 // GNUNET_PEER_resolve (pid, &msg.peer2);
1879 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1880 // send_prebuilt_message (&msg.header, &neighbor, t);
1887 * Build a local ACK message and send it to a local client.
1889 * @param t Tunnel on which to send the ACK.
1890 * @param c Client to whom send the ACK.
1891 * @param ack Value of the ACK.
1894 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
1896 struct GNUNET_MESH_LocalAck msg;
1898 msg.header.size = htons (sizeof (msg));
1899 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1900 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
1901 msg.max_pid = htonl (ack);
1902 GNUNET_SERVER_notification_context_unicast(nc,
1909 * Build an ACK message and queue it to send to the given peer.
1911 * @param t Tunnel on which to send the ACK.
1912 * @param peer Peer to whom send the ACK.
1913 * @param ack Value of the ACK.
1916 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1918 struct GNUNET_MESH_ACK msg;
1920 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1921 msg.header.size = htons (sizeof (msg));
1922 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1923 msg.pid = htonl (ack);
1924 msg.tid = htonl (t->id.tid);
1926 send_prebuilt_message (&msg.header, peer, t);
1931 * Send an ACK informing the predecessor about the available buffer space.
1932 * In case there is no predecessor, inform the owning client.
1933 * If buffering is off, send only on behalf of children or self if endpoint.
1934 * If buffering is on, send when sent to children and buffer space is free.
1935 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
1936 * the ACK itself goes "back" (towards root).
1938 * @param t Tunnel on which to send the ACK.
1939 * @param type Type of message that triggered the ACK transmission.
1942 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
1946 /* Is it after unicast retransmission? */
1949 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1951 "ACK due to FWD DATA retransmission\n");
1952 if (GNUNET_YES == t->nobuffer)
1954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
1958 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1959 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1961 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1962 t->force_ack = GNUNET_YES;
1968 /* Check if we need to transmit the ACK */
1969 if (t->queue_max > t->next_fc.queue_n * 4 &&
1970 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
1971 GNUNET_NO == t->force_ack)
1973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
1974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1975 " t->qmax: %u, t->qn: %u\n",
1976 t->queue_max, t->next_fc.queue_n);
1977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1978 " t->pid: %u, t->ack: %u\n",
1979 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
1983 /* Ok, ACK might be necessary, what PID to ACK? */
1984 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
1985 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
1987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
1991 t->prev_fc.last_ack_sent = ack;
1992 if (0 != t->prev_hop)
1993 send_ack (t, t->prev_hop, ack);
1994 else if (NULL != t->owner)
1995 send_local_ack (t, t->owner, ack);
1999 t->force_ack = GNUNET_NO;
2004 * Send an ACK informing the children node/client about the available
2006 * If buffering is off, send only on behalf of root (can be self).
2007 * If buffering is on, send when sent to predecessor and buffer space is free.
2008 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2009 * the ACK itself goes "forward" (towards children/clients).
2011 * @param t Tunnel on which to send the ACK.
2012 * @param type Type of message that triggered the ACK transmission.
2015 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2019 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2020 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2021 /* Is it after data to_origin retransmission? */
2024 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2025 if (GNUNET_YES == t->nobuffer)
2027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2028 " Not sending ACK, nobuffer + traffic\n");
2032 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2033 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2035 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2036 t->force_ack = GNUNET_YES;
2042 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2044 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2047 " Not sending ACK, not needed\n");
2050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2051 " Sending BCK ACK %u (last sent: %u)\n",
2052 ack, t->next_fc.last_ack_sent);
2053 t->next_fc.last_ack_sent = ack;
2055 if (0 != t->next_hop)
2056 send_ack (t, t->next_hop, ack);
2057 else if (NULL != t->client)
2058 send_local_ack (t, t->client, ack);
2061 t->force_ack = GNUNET_NO;
2066 * @brief Re-initiate traffic to this peer if necessary.
2068 * Check if there is traffic queued towards this peer
2069 * and the core transmit handle is NULL (traffic was stalled).
2070 * If so, call core tmt rdy.
2072 * @param peer_id Short ID of peer to which initiate traffic.
2075 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2077 struct MeshPeerInfo *peer;
2078 struct GNUNET_PeerIdentity id;
2079 struct MeshPeerQueue *q;
2082 peer = peer_get_short (peer_id);
2083 if (NULL != peer->core_transmit)
2086 q = queue_get_next (peer);
2089 /* Might br multicast traffic already sent to this particular peer but
2090 * not to other children in this tunnel.
2091 * This way t->queue_n would be > 0 but the queue of this particular peer
2097 GNUNET_PEER_resolve (peer->id, &id);
2098 peer->core_transmit =
2099 GNUNET_CORE_notify_transmit_ready(core_handle,
2102 GNUNET_TIME_UNIT_FOREVER_REL,
2112 * Send a message to all peers in this tunnel that the tunnel is no longer
2115 * @param t The tunnel whose peers to notify.
2118 tunnel_send_destroy (struct MeshTunnel *t)
2120 struct GNUNET_MESH_TunnelDestroy msg;
2121 struct GNUNET_PeerIdentity id;
2123 msg.header.size = htons (sizeof (msg));
2124 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2125 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2126 msg.tid = htonl (t->id.tid);
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2128 " sending tunnel destroy for tunnel: %s [%X]\n",
2129 GNUNET_i2s (&msg.oid), t->id.tid);
2131 if (0 != t->next_hop)
2133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2134 GNUNET_PEER_resolve (t->next_hop, &id);
2135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2136 " sending forward to %s\n",
2138 send_prebuilt_message (&msg.header, t->next_hop, t);
2140 if (0 != t->prev_hop)
2142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2143 GNUNET_PEER_resolve (t->prev_hop, &id);
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2145 " sending back to %s\n",
2147 send_prebuilt_message (&msg.header, t->prev_hop, t);
2153 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2155 * @param t Tunnel which to cancel.
2156 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2159 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2161 struct MeshPeerInfo *peer_info;
2162 struct MeshPeerQueue *pq;
2163 struct MeshPeerQueue *next;
2165 peer_info = peer_get_short (neighbor);
2166 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2169 if (pq->tunnel == t)
2171 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2172 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2174 // Should have been removed on destroy children
2177 queue_destroy (pq, GNUNET_YES);
2180 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2182 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2183 peer_info->core_transmit = NULL;
2189 * Destroy the tunnel and free any allocated resources linked to it.
2191 * @param t the tunnel to destroy
2193 * @return GNUNET_OK on success
2196 tunnel_destroy (struct MeshTunnel *t)
2198 struct MeshClient *c;
2199 struct GNUNET_HashCode hash;
2209 struct GNUNET_PeerIdentity id;
2211 GNUNET_PEER_resolve (t->id.oid, &id);
2212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2213 GNUNET_i2s (&id), t->id.tid);
2215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2219 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2220 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2228 GMC_hash32 (t->local_tid, &hash);
2230 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2237 if (NULL != t->client)
2239 GMC_hash32 (t->local_tid_dest, &hash);
2241 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2248 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2254 peer_cancel_queues (t->next_hop, t);
2255 peer_cancel_queues (t->prev_hop, t);
2257 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2258 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2261 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2266 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
2269 * Tunnel is empty: destroy it.
2271 * @param t Tunnel to destroy.
2274 tunnel_destroy_empty (struct MeshTunnel *t)
2278 struct GNUNET_PeerIdentity id;
2280 GNUNET_PEER_resolve (t->id.oid, &id);
2281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2282 "executing destruction of empty tunnel %s [%X]\n",
2283 GNUNET_i2s (&id), t->id.tid);
2287 tunnel_send_destroy (t);
2288 if (0 == t->pending_messages)
2291 t->destroy = GNUNET_YES;
2296 * Create a new tunnel
2298 * @param owner Who is the owner of the tunnel (short ID).
2299 * @param tid Tunnel Number of the tunnel.
2300 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2301 * @param local Tunnel Number for the tunnel, for the client point of view.
2303 * @return A new initialized tunnel. NULL on error.
2305 static struct MeshTunnel *
2306 tunnel_new (GNUNET_PEER_Id owner,
2307 MESH_TunnelNumber tid,
2308 struct MeshClient *client,
2309 MESH_TunnelNumber local)
2311 struct MeshTunnel *t;
2312 struct GNUNET_HashCode hash;
2314 if (n_tunnels >= max_tunnels && NULL == client)
2317 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2320 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2322 t->next_fc.last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2323 t->next_fc.last_pid_recv = (uint32_t) -1;
2324 t->prev_fc.last_pid_sent = (uint32_t) -1;
2325 t->prev_fc.last_pid_recv = (uint32_t) -1;
2326 t->next_fc.last_ack_sent = INITIAL_WINDOW_SIZE - 1;
2327 t->next_fc.last_ack_recv = INITIAL_WINDOW_SIZE - 1;
2328 t->prev_fc.last_ack_sent = INITIAL_WINDOW_SIZE - 1;
2329 t->prev_fc.last_ack_recv = INITIAL_WINDOW_SIZE - 1;
2330 t->local_tid = local;
2332 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2334 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2336 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2337 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2344 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2351 GMC_hash32 (t->local_tid, &hash);
2353 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2354 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2358 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2369 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2370 * client when the client disconnects. If the client is not the owner, the
2371 * owner will get notified if no more clients are in the tunnel and the client
2372 * get removed from the tunnel's list.
2374 * @param cls closure (client that is disconnecting)
2375 * @param key the hash of the local tunnel id (used to access the hashmap)
2376 * @param value the value stored at the key (tunnel to destroy)
2378 * @return GNUNET_OK, keep iterating.
2381 tunnel_destroy_iterator (void *cls,
2382 const struct GNUNET_HashCode * key,
2385 struct MeshTunnel *t = value;
2386 struct MeshClient *c = cls;
2388 send_client_tunnel_destroy (t);
2391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
2392 client_delete_tunnel (c, t);
2393 tunnel_destroy_empty (t);
2396 tunnel_send_destroy (t);
2398 t->destroy = GNUNET_YES;
2405 * Timeout function, destroys tunnel if called
2407 * @param cls Closure (tunnel to destroy).
2408 * @param tc TaskContext
2411 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2413 struct MeshTunnel *t = cls;
2414 struct GNUNET_PeerIdentity id;
2416 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2417 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2419 GNUNET_PEER_resolve(t->id.oid, &id);
2420 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2421 "Tunnel %s [%X] timed out. Destroying.\n",
2422 GNUNET_i2s(&id), t->id.tid);
2423 send_client_tunnel_destroy (t);
2429 * Resets the tunnel timeout. Starts it if no timeout was running.
2431 * @param t Tunnel whose timeout to reset.
2433 * TODO use heap to improve efficiency of scheduler.
2436 tunnel_reset_timeout (struct MeshTunnel *t)
2438 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2440 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2441 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2442 t->maintenance_task =
2443 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2444 (refresh_path_time, 4), &tunnel_timeout, t);
2448 /******************************************************************************/
2449 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2450 /******************************************************************************/
2453 * Function to send a create path packet to a peer.
2455 * @param cls closure
2456 * @param size number of bytes available in buf
2457 * @param buf where the callee should write the message
2458 * @return number of bytes written to buf
2461 send_core_path_create (void *cls, size_t size, void *buf)
2463 struct MeshTunnel *t = cls;
2464 struct GNUNET_MESH_ManipulatePath *msg;
2465 struct GNUNET_PeerIdentity *peer_ptr;
2466 struct MeshPeerPath *p = t->path;
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2473 sizeof (struct GNUNET_MESH_ManipulatePath) +
2474 p->length * sizeof (struct GNUNET_PeerIdentity);
2476 if (size < size_needed || NULL == buf)
2481 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2482 msg->header.size = htons (size_needed);
2483 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2484 msg->tid = ntohl (t->id.tid);
2487 if (GNUNET_YES == t->nobuffer)
2488 opt |= MESH_TUNNEL_OPT_NOBUFFER;
2489 msg->opt = htonl(opt);
2492 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2493 for (i = 0; i < p->length; i++)
2495 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2499 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2505 * Creates a path ack message in buf and frees all unused resources.
2507 * @param cls closure (MeshTransmissionDescriptor)
2508 * @param size number of bytes available in buf
2509 * @param buf where the callee should write the message
2510 * @return number of bytes written to buf
2513 send_core_path_ack (void *cls, size_t size, void *buf)
2515 struct MeshTransmissionDescriptor *info = cls;
2516 struct GNUNET_MESH_PathACK *msg = buf;
2518 GNUNET_assert (NULL != info);
2519 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2524 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2525 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2526 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2527 msg->tid = htonl (info->origin->tid);
2528 msg->peer_id = my_full_id;
2531 /* TODO add signature */
2533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2534 return sizeof (struct GNUNET_MESH_PathACK);
2539 * Free a transmission that was already queued with all resources
2540 * associated to the request.
2542 * @param queue Queue handler to cancel.
2543 * @param clear_cls Is it necessary to free associated cls?
2546 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2548 struct MeshTransmissionDescriptor *dd;
2549 // unsigned int max;
2551 if (GNUNET_YES == clear_cls)
2553 switch (queue->type)
2555 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2556 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2557 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2559 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2560 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2561 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2562 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2563 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2565 " prebuilt message\n");
2566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2568 GNUNET_MESH_DEBUG_M2S(queue->type));
2570 GNUNET_free (dd->data);
2572 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2577 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2578 " type %s unknown!\n",
2579 GNUNET_MESH_DEBUG_M2S(queue->type));
2581 GNUNET_free_non_null (queue->cls);
2583 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2584 queue->peer->queue_tail,
2587 /* Delete from child_fc in the appropiate tunnel */
2588 // max = queue->tunnel->fwd_queue_max;
2589 // GNUNET_PEER_resolve (queue->peer->id, &id);
2590 // if (NULL != cinfo)
2592 // for (i = 0; i < cinfo->send_buffer_n; i++)
2594 // i2 = (cinfo->send_buffer_start + i) % max;
2595 // if (cinfo->send_buffer[i2] == queue)
2597 // /* Found corresponding entry in the send_buffer. Move all others back. */
2601 // for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
2603 // j2 = (cinfo->send_buffer_start + j) % max;
2604 // j3 = (cinfo->send_buffer_start + j + 1) % max;
2605 // cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
2608 // cinfo->send_buffer[j3] = NULL;
2609 // cinfo->send_buffer_n--;
2614 GNUNET_free (queue);
2619 * @brief Get the next transmittable message from the queue.
2621 * This will be the head, except in the case of being a data packet
2622 * not allowed by the destination peer.
2624 * @param peer Destination peer.
2626 * @return The next viable MeshPeerQueue element to send to that peer.
2627 * NULL when there are no transmittable messages.
2629 struct MeshPeerQueue *
2630 queue_get_next (const struct MeshPeerInfo *peer)
2632 struct MeshPeerQueue *q;
2634 struct MeshTransmissionDescriptor *info;
2635 // struct GNUNET_MESH_Unicast *ucast;
2636 // struct GNUNET_MESH_ToOrigin *to_orig;
2637 struct GNUNET_PeerIdentity id;
2639 // uint32_t ack; FIXME
2641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
2642 for (q = peer->queue_head; NULL != q; q = q->next)
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2648 GNUNET_MESH_DEBUG_M2S(q->type));
2651 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2652 // ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2653 // pid = ntohl (ucast->pid);
2654 GNUNET_PEER_resolve (info->peer->id, &id);
2655 // ack = cinfo->fwd_ack;
2657 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2658 // to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2659 // pid = ntohl (to_orig->pid);
2660 // ack = t->bck_ack;
2663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2667 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2668 // "********* ACK: %u, PID: %u\n",
2670 // if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
2672 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2673 // "********* OK!\n");
2678 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2679 // "********* NEXT!\n");
2682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2683 "********* nothing found\n");
2689 queue_send (void *cls, size_t size, void *buf)
2691 struct MeshPeerInfo *peer = cls;
2692 struct GNUNET_MessageHeader *msg;
2693 struct MeshPeerQueue *queue;
2694 struct MeshTunnel *t;
2695 struct GNUNET_PeerIdentity dst_id;
2696 struct MeshFlowControl *fc;
2699 peer->core_transmit = NULL;
2701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
2702 queue = queue_get_next (peer);
2704 /* Queue has no internal mesh traffic nor sendable payload */
2707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
2708 if (NULL == peer->queue_head)
2709 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
2712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
2714 GNUNET_PEER_resolve (peer->id, &dst_id);
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2716 "********* towards %s\n",
2717 GNUNET_i2s(&dst_id));
2718 /* Check if buffer size is enough for the message */
2719 if (queue->size > size)
2721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2722 "********* not enough room, reissue\n");
2723 peer->core_transmit =
2724 GNUNET_CORE_notify_transmit_ready (core_handle,
2727 GNUNET_TIME_UNIT_FOREVER_REL,
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
2737 GNUNET_assert (0 < t->pending_messages);
2738 t->pending_messages--;
2739 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
2741 t->next_fc.queue_n--;
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2743 "********* unicast: t->q (%u/%u)\n",
2744 t->next_fc.queue_n, t->queue_max);
2746 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
2748 t->prev_fc.queue_n--;
2749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2750 "********* to orig: t->q (%u/%u)\n",
2751 t->prev_fc.queue_n, t->queue_max);
2755 switch (queue->type)
2758 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2759 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2760 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
2761 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
2762 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2763 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2765 "********* raw: %s\n",
2766 GNUNET_MESH_DEBUG_M2S (queue->type));
2768 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2769 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2770 data_size = send_core_data_raw (queue->cls, size, buf);
2771 msg = (struct GNUNET_MessageHeader *) buf;
2772 switch (ntohs (msg->type)) // Type of preconstructed message
2774 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2775 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
2777 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2778 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
2784 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
2786 data_size = send_core_path_create (queue->cls, size, buf);
2788 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
2790 data_size = send_core_path_ack (queue->cls, size, buf);
2794 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2795 "********* type unknown: %u\n",
2799 switch (queue->type)
2801 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2802 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2803 // if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
2805 // GNUNET_break (0);
2806 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2807 // "at pos %u (%p) != %p\n",
2808 // cinfo->send_buffer_start,
2809 // cinfo->send_buffer[cinfo->send_buffer_start],
2812 // if (cinfo->send_buffer_n > 0)
2814 // cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
2815 // cinfo->send_buffer_n--;
2816 // cinfo->send_buffer_start++;
2817 // cinfo->send_buffer_start %= t->fwd_queue_max;
2821 // GNUNET_break (0);
2828 /* Free queue, but cls was freed by send_core_* */
2829 queue_destroy (queue, GNUNET_NO);
2831 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
2833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
2837 /* If more data in queue, send next */
2838 queue = queue_get_next (peer);
2841 struct GNUNET_PeerIdentity id;
2843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
2844 GNUNET_PEER_resolve (peer->id, &id);
2845 peer->core_transmit =
2846 GNUNET_CORE_notify_transmit_ready(core_handle,
2849 GNUNET_TIME_UNIT_FOREVER_REL,
2857 if (NULL != peer->queue_head)
2859 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2860 "********* %s stalled\n",
2861 GNUNET_i2s(&my_full_id));
2862 if (peer->id == t->next_hop)
2866 if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
2869 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
2880 * @brief Queue and pass message to core when possible.
2882 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
2883 * accounts for it. In case the queue is full, the message is dropped and
2886 * Otherwise, message is treated as internal and allowed to go regardless of
2889 * @param cls Closure (@c type dependant). It will be used by queue_send to
2890 * build the message to be sent if not already prebuilt.
2891 * @param type Type of the message, 0 for a raw message.
2892 * @param size Size of the message.
2893 * @param dst Neighbor to send message to.
2894 * @param t Tunnel this message belongs to.
2897 queue_add (void *cls, uint16_t type, size_t size,
2898 struct MeshPeerInfo *dst, struct MeshTunnel *t)
2900 struct MeshPeerQueue *queue;
2901 struct GNUNET_PeerIdentity id;
2905 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2907 n = &t->next_fc.queue_n;
2909 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
2911 n = &t->prev_fc.queue_n;
2915 if (*n >= t->queue_max)
2918 GNUNET_STATISTICS_update(stats,
2919 "# messages dropped (buffer full)",
2921 return; // Drop message
2925 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2931 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
2932 if (NULL == dst->core_transmit)
2934 GNUNET_PEER_resolve (dst->id, &id);
2935 dst->core_transmit =
2936 GNUNET_CORE_notify_transmit_ready (core_handle,
2939 GNUNET_TIME_UNIT_FOREVER_REL,
2945 t->pending_messages++;
2949 /******************************************************************************/
2950 /******************** MESH NETWORK HANDLERS **************************/
2951 /******************************************************************************/
2955 * Core handler for path creation
2957 * @param cls closure
2958 * @param message message
2959 * @param peer peer identity this notification is about
2961 * @return GNUNET_OK to keep the connection open,
2962 * GNUNET_SYSERR to close it (signal serious error)
2965 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2966 const struct GNUNET_MessageHeader *message)
2968 unsigned int own_pos;
2971 MESH_TunnelNumber tid;
2972 struct GNUNET_MESH_ManipulatePath *msg;
2973 struct GNUNET_PeerIdentity *pi;
2974 struct GNUNET_HashCode hash;
2975 struct MeshPeerPath *path;
2976 struct MeshPeerInfo *dest_peer_info;
2977 struct MeshPeerInfo *orig_peer_info;
2978 struct MeshTunnel *t;
2980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2981 "Received a path create msg [%s]\n",
2982 GNUNET_i2s (&my_full_id));
2983 size = ntohs (message->size);
2984 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2986 GNUNET_break_op (0);
2990 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2991 if (size % sizeof (struct GNUNET_PeerIdentity))
2993 GNUNET_break_op (0);
2996 size /= sizeof (struct GNUNET_PeerIdentity);
2999 GNUNET_break_op (0);
3002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3003 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3005 tid = ntohl (msg->tid);
3006 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3008 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
3009 t = tunnel_get (pi, tid);
3010 if (NULL == t) // FIXME only for INCOMING tunnels?
3014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3015 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3018 // FIXME notify failure
3021 opt = ntohl (msg->opt);
3022 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
3024 t->nobuffer = GNUNET_YES;
3025 t->prev_fc.last_ack_sent = t->prev_fc.last_pid_recv + 1;
3027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3029 if (GNUNET_YES == t->nobuffer)
3034 // FIXME only assign a local tid if a local client is interested (on demand)
3035 while (NULL != tunnel_get_incoming (next_local_tid))
3036 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3037 t->local_tid_dest = next_local_tid++;
3038 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3041 tunnel_reset_timeout (t);
3042 GMC_hash32 (t->local_tid_dest, &hash);
3044 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
3045 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
3053 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3054 if (NULL == dest_peer_info)
3056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3057 " Creating PeerInfo for destination.\n");
3058 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3059 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3060 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3062 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3064 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3065 if (NULL == orig_peer_info)
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3068 " Creating PeerInfo for origin.\n");
3069 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3070 orig_peer_info->id = GNUNET_PEER_intern (pi);
3071 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3072 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3075 path = path_new (size);
3077 for (i = 0; i < size; i++)
3079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3080 GNUNET_i2s (&pi[i]));
3081 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3082 if (path->peers[i] == myid)
3085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3088 /* cannot be self, must be 'not found' */
3089 /* create path: self not found in path through self */
3090 GNUNET_break_op (0);
3091 path_destroy (path);
3095 path_add_to_peers (path, GNUNET_NO);
3096 t->prev_hop = path->peers[own_pos - 1];
3097 GNUNET_PEER_change_rc (t->prev_hop, 1);
3098 if (own_pos == size - 1)
3100 /* It is for us! Send ack. */
3101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3102 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3108 struct MeshPeerPath *path2;
3110 t->next_hop = path->peers[own_pos + 1];
3111 GNUNET_PEER_change_rc(t->next_hop, 1);
3113 /* It's for somebody else! Retransmit. */
3114 path2 = path_duplicate (path);
3115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3116 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3117 path2 = path_duplicate (path);
3118 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3119 send_create_path (dest_peer_info, path, t);
3126 * Core handler for path destruction
3128 * @param cls closure
3129 * @param message message
3130 * @param peer peer identity this notification is about
3132 * @return GNUNET_OK to keep the connection open,
3133 * GNUNET_SYSERR to close it (signal serious error)
3136 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3137 const struct GNUNET_MessageHeader *message)
3139 struct GNUNET_MESH_ManipulatePath *msg;
3140 struct GNUNET_PeerIdentity *pi;
3141 struct MeshPeerPath *path;
3142 struct MeshTunnel *t;
3143 unsigned int own_pos;
3147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3148 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3149 size = ntohs (message->size);
3150 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3152 GNUNET_break_op (0);
3156 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3157 if (size % sizeof (struct GNUNET_PeerIdentity))
3159 GNUNET_break_op (0);
3162 size /= sizeof (struct GNUNET_PeerIdentity);
3165 GNUNET_break_op (0);
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3170 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3171 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3173 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3175 t = tunnel_get (pi, ntohl (msg->tid));
3178 /* TODO notify back: we don't know this tunnel */
3179 GNUNET_break_op (0);
3182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3183 path = path_new (size);
3185 for (i = 0; i < size; i++)
3187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3188 GNUNET_i2s (&pi[i]));
3189 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3190 if (path->peers[i] == myid)
3193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3194 if (own_pos < path->length - 1)
3195 send_prebuilt_message (message, path->peers[own_pos + 1], t);
3197 send_client_tunnel_destroy (t);
3199 // tunnel_delete_peer (t, path->peers[path->length - 1]); FIXME
3200 path_destroy (path);
3206 * Core handler for notifications of broken paths
3208 * @param cls closure
3209 * @param message message
3210 * @param peer peer identity this notification is about
3212 * @return GNUNET_OK to keep the connection open,
3213 * GNUNET_SYSERR to close it (signal serious error)
3216 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3217 const struct GNUNET_MessageHeader *message)
3219 struct GNUNET_MESH_PathBroken *msg;
3220 struct MeshTunnel *t;
3222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3223 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3224 msg = (struct GNUNET_MESH_PathBroken *) message;
3225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3226 GNUNET_i2s (&msg->peer1));
3227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3228 GNUNET_i2s (&msg->peer2));
3229 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3232 GNUNET_break_op (0);
3235 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3236 GNUNET_PEER_search (&msg->peer2));
3243 * Core handler for tunnel destruction
3245 * @param cls closure
3246 * @param message message
3247 * @param peer peer identity this notification is about
3249 * @return GNUNET_OK to keep the connection open,
3250 * GNUNET_SYSERR to close it (signal serious error)
3253 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3254 const struct GNUNET_MessageHeader *message)
3256 struct GNUNET_MESH_TunnelDestroy *msg;
3257 struct MeshTunnel *t;
3259 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3261 "Got a TUNNEL DESTROY packet from %s\n",
3263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3264 " for tunnel %s [%u]\n",
3265 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3266 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3267 /* Check signature */
3270 /* Probably already got the message from another path,
3271 * destroyed the tunnel and retransmitted to children.
3274 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3278 // TODO check owner's signature
3279 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3281 /* Tunnel was incoming, notify clients */
3282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3283 t->local_tid, t->local_tid_dest);
3284 send_client_tunnel_destroy (t);
3286 tunnel_send_destroy (t);
3287 t->destroy = GNUNET_YES;
3288 // TODO: add timeout to destroy the tunnel anyway
3294 * Core handler for mesh network traffic going from the origin to a peer
3296 * @param cls closure
3297 * @param peer peer identity this notification is about
3298 * @param message message
3299 * @return GNUNET_OK to keep the connection open,
3300 * GNUNET_SYSERR to close it (signal serious error)
3303 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3304 const struct GNUNET_MessageHeader *message)
3306 struct GNUNET_MESH_Unicast *msg;
3307 struct MeshTunnel *t;
3312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3315 size = ntohs (message->size);
3317 sizeof (struct GNUNET_MESH_Unicast) +
3318 sizeof (struct GNUNET_MessageHeader))
3323 msg = (struct GNUNET_MESH_Unicast *) message;
3324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3325 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3327 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3330 /* TODO notify back: we don't know this tunnel */
3331 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3332 GNUNET_break_op (0);
3335 pid = ntohl (msg->pid);
3336 if (t->prev_fc.last_pid_recv == pid)
3338 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3340 " Already seen pid %u, DROPPING!\n", pid);
3345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3346 " pid %u not seen yet, forwarding\n", pid);
3349 t->prev_fc.last_pid_recv = pid;
3351 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3353 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3354 GNUNET_break_op (0);
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3356 "Received PID %u, ACK %u\n",
3357 pid, t->prev_fc.last_ack_sent);
3361 tunnel_reset_timeout (t);
3362 if (t->dest == myid)
3364 if (NULL == t->client)
3369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3370 " it's for us! sending to clients...\n");
3371 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3372 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
3373 message, GNUNET_NO);
3374 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3377 if (0 == t->next_hop)
3382 ttl = ntohl (msg->ttl);
3383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3386 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3387 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3388 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3392 " not for us, retransmitting...\n");
3394 if (GNUNET_YES == t->nobuffer &&
3395 GNUNET_YES == GMC_is_pid_bigger (pid, t->next_fc.last_ack_recv))
3397 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3398 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n",
3399 pid, t->next_fc.last_ack_recv);
3400 GNUNET_break_op (0);
3403 send_prebuilt_message (message, t->next_hop, t);
3404 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3410 * Core handler for mesh network traffic toward the owner of a tunnel
3412 * @param cls closure
3413 * @param message message
3414 * @param peer peer identity this notification is about
3416 * @return GNUNET_OK to keep the connection open,
3417 * GNUNET_SYSERR to close it (signal serious error)
3420 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3421 const struct GNUNET_MessageHeader *message)
3423 struct GNUNET_MESH_ToOrigin *msg;
3424 struct MeshPeerInfo *peer_info;
3425 struct MeshTunnel *t;
3429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3431 size = ntohs (message->size);
3432 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3433 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3435 GNUNET_break_op (0);
3438 msg = (struct GNUNET_MESH_ToOrigin *) message;
3439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3440 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3441 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3442 pid = ntohl (msg->pid);
3446 /* TODO notify that we dont know this tunnel (whom)? */
3447 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3449 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3450 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3455 if (t->next_fc.last_pid_recv == pid)
3457 /* already seen this packet, drop */
3458 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3460 " Already seen pid %u, DROPPING!\n", pid);
3461 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3466 " pid %u not seen yet, forwarding\n", pid);
3467 t->next_fc.last_pid_recv = pid;
3469 if (NULL != t->owner)
3472 struct GNUNET_MESH_ToOrigin *copy;
3474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3475 " it's for us! sending to clients...\n");
3476 /* TODO signature verification */
3477 memcpy (cbuf, message, size);
3478 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3479 copy->tid = htonl (t->local_tid);
3480 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3481 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3482 ©->header, GNUNET_NO);
3483 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3487 " not for us, retransmitting...\n");
3489 peer_info = peer_get (&msg->oid);
3490 if (NULL == peer_info)
3492 /* unknown origin of tunnel */
3496 if (0 == t->prev_hop) /* No owner AND no prev hop */
3498 if (GNUNET_YES == t->destroy)
3500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3501 "to orig received on a dying tunnel %s [%X]\n",
3502 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3505 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3506 "unknown to origin at %s\n",
3507 GNUNET_i2s (&my_full_id));
3508 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3511 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3512 "for tunnel %s [%X]\n",
3513 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3516 send_prebuilt_message (message, t->prev_hop, t);
3517 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3524 * Core handler for mesh network traffic point-to-point acks.
3526 * @param cls closure
3527 * @param message message
3528 * @param peer peer identity this notification is about
3530 * @return GNUNET_OK to keep the connection open,
3531 * GNUNET_SYSERR to close it (signal serious error)
3534 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3535 const struct GNUNET_MessageHeader *message)
3537 struct GNUNET_MESH_ACK *msg;
3538 struct MeshTunnel *t;
3541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3543 msg = (struct GNUNET_MESH_ACK *) message;
3545 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3549 /* TODO notify that we dont know this tunnel (whom)? */
3550 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3553 ack = ntohl (msg->pid);
3554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3556 /* Is this a forward or backward ACK? */
3557 if (t->prev_hop != GNUNET_PEER_search (peer))
3560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3561 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task &&
3562 GMC_is_pid_bigger (ack, t->next_fc.last_ack_recv))
3564 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3565 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3566 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3568 t->next_fc.last_ack_recv = ack;
3569 peer_unlock_queue (t->next_hop);
3570 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3575 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
3576 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
3578 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3579 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3580 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3582 t->prev_fc.last_ack_recv = ack;
3583 peer_unlock_queue (t->prev_hop);
3584 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3591 * Core handler for mesh network traffic point-to-point ack polls.
3593 * @param cls closure
3594 * @param message message
3595 * @param peer peer identity this notification is about
3597 * @return GNUNET_OK to keep the connection open,
3598 * GNUNET_SYSERR to close it (signal serious error)
3601 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3602 const struct GNUNET_MessageHeader *message)
3604 struct GNUNET_MESH_Poll *msg;
3605 struct MeshTunnel *t;
3607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3610 msg = (struct GNUNET_MESH_Poll *) message;
3612 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3616 /* TODO notify that we dont know this tunnel (whom)? */
3617 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
3618 GNUNET_break_op (0);
3622 /* Is this a forward or backward ACK? */
3623 if (t->prev_hop != GNUNET_PEER_search(peer))
3625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
3626 /* FIXME cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send */
3627 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
3632 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3639 * Core handler for path ACKs
3641 * @param cls closure
3642 * @param message message
3643 * @param peer peer identity this notification is about
3645 * @return GNUNET_OK to keep the connection open,
3646 * GNUNET_SYSERR to close it (signal serious error)
3649 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3650 const struct GNUNET_MessageHeader *message)
3652 struct GNUNET_MESH_PathACK *msg;
3653 struct MeshPeerInfo *peer_info;
3654 struct MeshPeerPath *p;
3655 struct MeshTunnel *t;
3657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3658 GNUNET_i2s (&my_full_id));
3659 msg = (struct GNUNET_MESH_PathACK *) message;
3660 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3663 /* TODO notify that we don't know the tunnel */
3664 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3666 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3670 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3672 peer_info = peer_get (&msg->peer_id);
3673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3674 GNUNET_i2s (&msg->peer_id));
3675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3678 /* Add path to peers? */
3682 path_add_to_peers (p, GNUNET_YES);
3689 /* Message for us? */
3690 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3693 if (NULL == t->owner)
3695 GNUNET_break_op (0);
3698 if (NULL != peer_info->dhtget)
3700 GNUNET_DHT_get_stop (peer_info->dhtget);
3701 peer_info->dhtget = NULL;
3706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3707 " not for us, retransmitting...\n");
3708 peer_info = peer_get (&msg->oid);
3709 send_prebuilt_message (message, t->prev_hop, t);
3715 * Core handler for mesh keepalives.
3717 * @param cls closure
3718 * @param message message
3719 * @param peer peer identity this notification is about
3720 * @return GNUNET_OK to keep the connection open,
3721 * GNUNET_SYSERR to close it (signal serious error)
3723 * TODO: Check who we got this from, to validate route.
3726 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
3727 const struct GNUNET_MessageHeader *message)
3729 struct GNUNET_MESH_TunnelKeepAlive *msg;
3730 struct MeshTunnel *t;
3732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
3735 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
3736 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3740 /* TODO notify that we dont know that tunnel */
3741 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
3746 tunnel_reset_timeout (t);
3748 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
3749 send_prebuilt_message (message, t->next_hop, t);
3756 * Functions to handle messages from core
3758 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3759 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3760 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3761 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3762 sizeof (struct GNUNET_MESH_PathBroken)},
3763 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
3764 sizeof (struct GNUNET_MESH_TunnelDestroy)},
3765 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3766 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
3767 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
3768 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3769 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
3770 sizeof (struct GNUNET_MESH_ACK)},
3771 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
3772 sizeof (struct GNUNET_MESH_Poll)},
3773 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3774 sizeof (struct GNUNET_MESH_PathACK)},
3780 /******************************************************************************/
3781 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3782 /******************************************************************************/
3787 * notify_client_connection_failure: notify a client that the connection to the
3788 * requested remote peer is not possible (for instance, no route found)
3789 * Function called when the socket is ready to queue more data. "buf" will be
3790 * NULL and "size" zero if the socket was closed for writing in the meantime.
3792 * @param cls closure
3793 * @param size number of bytes available in buf
3794 * @param buf where the callee should write the message
3795 * @return number of bytes written to buf
3798 notify_client_connection_failure (void *cls, size_t size, void *buf)
3801 struct MeshPeerInfo *peer_info;
3802 struct GNUNET_MESH_PeerControl *msg;
3803 struct GNUNET_PeerIdentity id;
3805 if (0 == size && NULL == buf)
3807 // TODO retry? cancel?
3811 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3812 peer_info = (struct MeshPeerInfo *) cls;
3813 msg = (struct GNUNET_MESH_PeerControl *) buf;
3814 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3815 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3816 // msg->tunnel_id = htonl(peer_info->t->tid);
3817 GNUNET_PEER_resolve (peer_info->id, &id);
3818 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3826 * Send keepalive packets for a tunnel.
3828 * @param cls Closure (tunnel for which to send the keepalive).
3829 * @param tc Notification context.
3832 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3834 struct MeshTunnel *t = cls;
3835 struct GNUNET_MESH_TunnelKeepAlive *msg;
3836 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
3839 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3840 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
3841 NULL == t->owner || 0 == t->local_tid || 0 != t->prev_hop)
3846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3847 "sending keepalive for tunnel %d\n", t->id.tid);
3849 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
3850 msg->header.size = htons (size);
3851 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3852 msg->oid = my_full_id;
3853 msg->tid = htonl (t->id.tid);
3854 send_prebuilt_message (&msg->header, t->next_hop, t);
3856 t->maintenance_task =
3857 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3862 * Function to process paths received for a new peer addition. The recorded
3863 * paths form the initial tunnel, which can be optimized later.
3864 * Called on each result obtained for the DHT search.
3866 * @param cls closure
3867 * @param exp when will this value expire
3868 * @param key key of the result
3869 * @param get_path path of the get request
3870 * @param get_path_length lenght of get_path
3871 * @param put_path path of the put request
3872 * @param put_path_length length of the put_path
3873 * @param type type of the result
3874 * @param size number of bytes in data
3875 * @param data pointer to the result data
3877 * TODO: re-issue the request after certain time? cancel after X results?
3880 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3881 const struct GNUNET_HashCode * key,
3882 const struct GNUNET_PeerIdentity *get_path,
3883 unsigned int get_path_length,
3884 const struct GNUNET_PeerIdentity *put_path,
3885 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3886 size_t size, const void *data)
3888 struct MeshPeerInfo *peer = cls;
3889 struct MeshPeerPath *p;
3890 struct GNUNET_PeerIdentity pi;
3893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3894 GNUNET_PEER_resolve (peer->id, &pi);
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3897 p = path_build_from_dht (get_path, get_path_length,
3898 put_path, put_path_length);
3899 path_add_to_peers (p, GNUNET_NO);
3901 for (i = 0; i < peer->ntunnels; i++)
3903 peer_connect (peer, peer->tunnels[i]); // FIXME add if
3910 /******************************************************************************/
3911 /********************* MESH LOCAL HANDLES **************************/
3912 /******************************************************************************/
3916 * Handler for client disconnection
3918 * @param cls closure
3919 * @param client identification of the client; NULL
3920 * for the last call when the server is destroyed
3923 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3925 struct MeshClient *c;
3926 struct MeshClient *next;
3928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3938 if (c->handle != client)
3940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3946 GNUNET_SERVER_client_drop (c->handle);
3947 c->shutting_down = GNUNET_YES;
3948 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3949 &tunnel_destroy_iterator, c);
3950 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3951 &tunnel_destroy_iterator, c);
3952 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3953 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3955 if (NULL != c->types)
3956 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3958 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
3959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3961 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
3964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
3970 * Handler for new clients
3972 * @param cls closure
3973 * @param client identification of the client
3974 * @param message the actual message, which includes messages the client wants
3977 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3978 const struct GNUNET_MessageHeader *message)
3980 struct GNUNET_MESH_ClientConnect *cc_msg;
3981 struct MeshClient *c;
3987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3989 /* Check data sanity */
3990 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3991 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3992 ntypes = ntohs (cc_msg->types);
3993 if (size != ntypes * sizeof (uint16_t))
3996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4000 /* Create new client structure */
4001 c = GNUNET_malloc (sizeof (struct MeshClient));
4002 c->id = next_client_id++; // overflow not important: just for debug
4003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
4004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u types\n", ntypes);
4006 GNUNET_SERVER_client_keep (client);
4010 struct GNUNET_HashCode hc;
4012 t = (uint16_t *) &cc_msg[1];
4013 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
4014 for (i = 0; i < ntypes; i++)
4017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
4018 GMC_hash32 ((uint32_t) u16, &hc);
4020 /* store in clients hashmap */
4021 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
4022 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4023 /* store in global hashmap */
4024 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
4025 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4029 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4030 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4031 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4032 GNUNET_SERVER_notification_context_add (nc, client);
4033 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4035 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4041 * Handler for requests of new tunnels
4043 * @param cls Closure.
4044 * @param client Identification of the client.
4045 * @param message The actual message.
4048 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4049 const struct GNUNET_MessageHeader *message)
4051 struct GNUNET_MESH_TunnelMessage *t_msg;
4052 struct MeshPeerInfo *peer_info;
4053 struct MeshTunnel *t;
4054 struct MeshClient *c;
4055 MESH_TunnelNumber tid;
4057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4059 /* Sanity check for client registration */
4060 if (NULL == (c = client_get (client)))
4063 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4068 /* Message sanity check */
4069 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4072 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4076 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4078 GNUNET_i2s (&t_msg->peer));
4079 /* Sanity check for tunnel numbering */
4080 tid = ntohl (t_msg->tunnel_id);
4081 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4084 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4087 /* Sanity check for duplicate tunnel IDs */
4088 if (NULL != tunnel_get_by_local_id (c, tid))
4091 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4095 while (NULL != tunnel_get_by_pi (myid, next_tid))
4096 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4097 t = tunnel_new (myid, next_tid, c, tid);
4098 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4102 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
4106 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4108 peer_info = peer_get (&t_msg->peer);
4109 GNUNET_array_append (peer_info->tunnels, peer_info->ntunnels, t);
4110 peer_connect (peer_info, t);
4111 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4117 * Handler for requests of deleting tunnels
4119 * @param cls closure
4120 * @param client identification of the client
4121 * @param message the actual message
4124 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4125 const struct GNUNET_MessageHeader *message)
4127 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4128 struct MeshClient *c;
4129 struct MeshTunnel *t;
4130 MESH_TunnelNumber tid;
4132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4133 "Got a DESTROY TUNNEL from client!\n");
4135 /* Sanity check for client registration */
4136 if (NULL == (c = client_get (client)))
4139 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4144 /* Message sanity check */
4145 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4148 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4152 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4154 /* Retrieve tunnel */
4155 tid = ntohl (tunnel_msg->tunnel_id);
4156 t = tunnel_get_by_local_id(c, tid);
4159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4161 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4166 tunnel_destroy_empty (t);
4167 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4170 send_client_tunnel_destroy (t);
4171 client_delete_tunnel (c, t);
4173 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4175 tunnel_send_destroy (t);
4176 peer_remove_tunnel (peer_get_short(t->dest), t);
4177 t->destroy = GNUNET_YES;
4178 /* The tunnel will be destroyed when the last message is transmitted. */
4179 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4185 * Handler for requests of seeting tunnel's buffering policy.
4187 * @param cls Closure (unused).
4188 * @param client Identification of the client.
4189 * @param message The actual message.
4192 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
4193 const struct GNUNET_MessageHeader *message)
4195 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4196 struct MeshClient *c;
4197 struct MeshTunnel *t;
4198 MESH_TunnelNumber tid;
4200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4201 "Got a BUFFER request from client!\n");
4203 /* Sanity check for client registration */
4204 if (NULL == (c = client_get (client)))
4207 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4212 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4214 /* Retrieve tunnel */
4215 tid = ntohl (tunnel_msg->tunnel_id);
4216 t = tunnel_get_by_local_id(c, tid);
4219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4221 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4225 switch (ntohs(message->type))
4227 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
4228 t->nobuffer = GNUNET_NO;
4230 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
4231 t->nobuffer = GNUNET_YES;
4237 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4242 * Handler for client traffic directed to one peer
4244 * @param cls closure
4245 * @param client identification of the client
4246 * @param message the actual message
4249 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4250 const struct GNUNET_MessageHeader *message)
4252 struct MeshClient *c;
4253 struct MeshTunnel *t;
4254 struct GNUNET_MESH_Unicast *data_msg;
4255 MESH_TunnelNumber tid;
4258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4259 "Got a unicast request from a client!\n");
4261 /* Sanity check for client registration */
4262 if (NULL == (c = client_get (client)))
4265 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4270 data_msg = (struct GNUNET_MESH_Unicast *) message;
4272 /* Sanity check for message size */
4273 size = ntohs (message->size);
4274 if (sizeof (struct GNUNET_MESH_Unicast) +
4275 sizeof (struct GNUNET_MessageHeader) > size)
4278 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4282 /* Tunnel exists? */
4283 tid = ntohl (data_msg->tid);
4284 t = tunnel_get_by_local_id (c, tid);
4288 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4292 /* Is it a local tunnel? Then, does client own the tunnel? */
4293 if (t->owner->handle != client)
4296 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4300 /* PID should be as expected: client<->service communication */
4301 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4304 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4305 "Unicast PID, expected %u, got %u\n",
4306 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4307 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4311 /* Ok, everything is correct, send the message
4312 * (pretend we got it from a mesh peer)
4315 /* Work around const limitation */
4316 char buf[ntohs (message->size)] GNUNET_ALIGN;
4317 struct GNUNET_MESH_Unicast *copy;
4319 copy = (struct GNUNET_MESH_Unicast *) buf;
4320 memcpy (buf, data_msg, size);
4321 copy->oid = my_full_id;
4322 copy->tid = htonl (t->id.tid);
4323 copy->ttl = htonl (default_ttl);
4324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4325 " calling generic handler...\n");
4326 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
4328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4329 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4336 * Handler for client traffic directed to the origin
4338 * @param cls closure
4339 * @param client identification of the client
4340 * @param message the actual message
4343 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4344 const struct GNUNET_MessageHeader *message)
4346 struct GNUNET_MESH_ToOrigin *data_msg;
4347 struct MeshFlowControl *fc;
4348 struct MeshClient *c;
4349 struct MeshTunnel *t;
4350 MESH_TunnelNumber tid;
4353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4354 "Got a ToOrigin request from a client!\n");
4355 /* Sanity check for client registration */
4356 if (NULL == (c = client_get (client)))
4359 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4364 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4366 /* Sanity check for message size */
4367 size = ntohs (message->size);
4368 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4369 sizeof (struct GNUNET_MessageHeader) > size)
4372 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4376 /* Tunnel exists? */
4377 tid = ntohl (data_msg->tid);
4378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4379 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4382 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4385 t = tunnel_get_by_local_id (c, tid);
4388 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4389 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4391 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4395 /* It should be sent by someone who has this as incoming tunnel. */
4399 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4403 /* PID should be as expected */
4405 if (ntohl (data_msg->pid) != fc->last_pid_recv + 1)
4408 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4409 "To Origin PID, expected %u, got %u\n",
4410 fc->last_pid_recv + 1,
4411 ntohl (data_msg->pid));
4412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4415 fc->last_pid_recv++;
4417 /* Ok, everything is correct, send the message
4418 * (pretend we got it from a mesh peer)
4421 char buf[ntohs (message->size)] GNUNET_ALIGN;
4422 struct GNUNET_MESH_ToOrigin *copy;
4424 /* Work around 'const' limitation */
4425 memcpy (buf, data_msg, size);
4426 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4427 GNUNET_PEER_resolve (t->id.oid, ©->oid);
4428 copy->tid = htonl (t->id.tid);
4429 copy->ttl = htonl (default_ttl);
4430 copy->pid = htonl (t->prev_fc.last_pid_sent + 1);
4432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4433 " calling generic handler...\n");
4434 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
4436 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4443 * Handler for client's ACKs for payload traffic.
4445 * @param cls Closure (unused).
4446 * @param client Identification of the client.
4447 * @param message The actual message.
4450 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4451 const struct GNUNET_MessageHeader *message)
4453 struct GNUNET_MESH_LocalAck *msg;
4454 struct MeshTunnel *t;
4455 struct MeshClient *c;
4456 MESH_TunnelNumber tid;
4459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4460 /* Sanity check for client registration */
4461 if (NULL == (c = client_get (client)))
4464 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4469 msg = (struct GNUNET_MESH_LocalAck *) message;
4471 /* Tunnel exists? */
4472 tid = ntohl (msg->tunnel_id);
4473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4474 t = tunnel_get_by_local_id (c, tid);
4478 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4479 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4480 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4484 ack = ntohl (msg->max_pid);
4485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4487 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4490 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4491 t->prev_fc.last_ack_recv = ack;
4492 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4496 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4497 t->next_fc.last_ack_recv = ack;
4498 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4501 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4509 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4511 * @param cls Closure (client handle).
4512 * @param key Key (hashed tunnel ID, unused).
4513 * @param value Tunnel info.
4515 * @return GNUNET_YES, to keep iterating.
4518 monitor_all_tunnels_iterator (void *cls,
4519 const struct GNUNET_HashCode * key,
4522 struct GNUNET_SERVER_Client *client = cls;
4523 struct MeshTunnel *t = value;
4524 struct GNUNET_MESH_LocalMonitor *msg;
4526 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4527 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4528 msg->tunnel_id = htonl (t->id.tid);
4529 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4530 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4531 GNUNET_PEER_resolve (t->dest, &msg->destination);
4533 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4534 "* sending info about tunnel %s [%u]\n",
4535 GNUNET_i2s (&msg->owner), t->id.tid);
4537 GNUNET_SERVER_notification_context_unicast (nc, client,
4538 &msg->header, GNUNET_NO);
4544 * Handler for client's MONITOR request.
4546 * @param cls Closure (unused).
4547 * @param client Identification of the client.
4548 * @param message The actual message.
4551 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4552 const struct GNUNET_MessageHeader *message)
4554 struct MeshClient *c;
4556 /* Sanity check for client registration */
4557 if (NULL == (c = client_get (client)))
4560 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4564 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4565 "Received get tunnels request from client %u\n",
4567 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4568 monitor_all_tunnels_iterator,
4570 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4571 "Get tunnels request from client %u completed\n",
4573 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4578 * Handler for client's MONITOR_TUNNEL request.
4580 * @param cls Closure (unused).
4581 * @param client Identification of the client.
4582 * @param message The actual message.
4585 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4586 const struct GNUNET_MessageHeader *message)
4588 const struct GNUNET_MESH_LocalMonitor *msg;
4589 struct GNUNET_MESH_LocalMonitor *resp;
4590 struct MeshClient *c;
4591 struct MeshTunnel *t;
4593 /* Sanity check for client registration */
4594 if (NULL == (c = client_get (client)))
4597 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4601 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4602 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4603 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4606 ntohl (msg->tunnel_id));
4607 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4610 /* We don't know the tunnel FIXME */
4611 struct GNUNET_MESH_LocalMonitor warn;
4614 GNUNET_SERVER_notification_context_unicast (nc, client,
4617 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4621 /* Initialize context */
4622 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4624 GNUNET_PEER_resolve (t->dest, &resp->destination);
4625 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4626 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4627 &resp->header, GNUNET_NO);
4630 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4631 "Monitor tunnel request from client %u completed\n",
4633 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4638 * Functions to handle messages from clients
4640 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4641 {&handle_local_new_client, NULL,
4642 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4643 {&handle_local_tunnel_create, NULL,
4644 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4645 sizeof (struct GNUNET_MESH_TunnelMessage)},
4646 {&handle_local_tunnel_destroy, NULL,
4647 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4648 sizeof (struct GNUNET_MESH_TunnelMessage)},
4649 {&handle_local_tunnel_buffer, NULL,
4650 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
4651 sizeof (struct GNUNET_MESH_TunnelMessage)},
4652 {&handle_local_tunnel_buffer, NULL,
4653 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
4654 sizeof (struct GNUNET_MESH_TunnelMessage)},
4655 {&handle_local_unicast, NULL,
4656 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4657 {&handle_local_to_origin, NULL,
4658 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4659 {&handle_local_ack, NULL,
4660 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4661 sizeof (struct GNUNET_MESH_LocalAck)},
4662 {&handle_local_get_tunnels, NULL,
4663 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4664 sizeof (struct GNUNET_MessageHeader)},
4665 {&handle_local_show_tunnel, NULL,
4666 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4667 sizeof (struct GNUNET_MESH_LocalMonitor)},
4673 * To be called on core init/fail.
4675 * @param cls service closure
4676 * @param server handle to the server for this service
4677 * @param identity the public identity of this peer
4680 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4681 const struct GNUNET_PeerIdentity *identity)
4684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4685 core_handle = server;
4686 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4689 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4690 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4692 GNUNET_i2s (identity));
4693 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4695 GNUNET_i2s (&my_full_id));
4696 GNUNET_SCHEDULER_shutdown (); // Try gracefully
4698 GNUNET_abort(); // Try harder
4705 * Method called whenever a given peer connects.
4707 * @param cls closure
4708 * @param peer peer identity this notification is about
4711 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4713 struct MeshPeerInfo *peer_info;
4714 struct MeshPeerPath *path;
4716 DEBUG_CONN ("Peer connected\n");
4717 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4718 peer_info = peer_get (peer);
4719 if (myid == peer_info->id)
4721 DEBUG_CONN (" (self)\n");
4726 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4728 path = path_new (2);
4729 path->peers[0] = myid;
4730 path->peers[1] = peer_info->id;
4731 GNUNET_PEER_change_rc (myid, 1);
4732 GNUNET_PEER_change_rc (peer_info->id, 1);
4733 peer_info_add_path (peer_info, path, GNUNET_YES);
4734 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4740 * Method called whenever a peer disconnects.
4742 * @param cls closure
4743 * @param peer peer identity this notification is about
4746 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4748 struct MeshPeerInfo *pi;
4749 struct MeshPeerQueue *q;
4750 struct MeshPeerQueue *n;
4752 DEBUG_CONN ("Peer disconnected\n");
4753 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4763 /* TODO try to reroute this traffic instead */
4764 queue_destroy(q, GNUNET_YES);
4767 if (NULL != pi->core_transmit)
4769 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4770 pi->core_transmit = NULL;
4772 peer_remove_path (pi, pi->id, myid);
4775 DEBUG_CONN (" (self)\n");
4777 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
4782 /******************************************************************************/
4783 /************************ MAIN FUNCTIONS ****************************/
4784 /******************************************************************************/
4787 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4789 * @param cls closure
4790 * @param key current key code
4791 * @param value value in the hash map
4792 * @return GNUNET_YES if we should continue to iterate,
4796 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
4798 struct MeshTunnel *t = value;
4805 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4807 * @param cls closure
4808 * @param key current key code
4809 * @param value value in the hash map
4810 * @return GNUNET_YES if we should continue to iterate,
4814 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
4816 struct MeshPeerInfo *p = value;
4817 struct MeshPeerQueue *q;
4818 struct MeshPeerQueue *n;
4826 queue_destroy(q, GNUNET_YES);
4830 peer_info_destroy (p);
4836 * Task run during shutdown.
4842 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4846 if (core_handle != NULL)
4848 GNUNET_CORE_disconnect (core_handle);
4853 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
4856 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4857 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4858 if (dht_handle != NULL)
4860 GNUNET_DHT_disconnect (dht_handle);
4865 GNUNET_SERVER_notification_context_destroy (nc);
4868 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4870 GNUNET_SCHEDULER_cancel (announce_id_task);
4871 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4878 * Callback for hostkey read/generation.
4880 * @param cls Closure (Configuration handle).
4881 * @param pk The ECC private key.
4882 * @param emsg Error message, if any.
4885 key_generation_cb (void *cls,
4886 struct GNUNET_CRYPTO_EccPrivateKey *pk,
4889 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4890 struct MeshPeerInfo *peer;
4891 struct MeshPeerPath *p;
4896 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4897 _("Mesh service could not access hostkey: %s. Exiting.\n"),
4899 GNUNET_SCHEDULER_shutdown ();
4902 my_private_key = pk;
4903 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
4904 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4905 &my_full_id.hashPubKey);
4906 myid = GNUNET_PEER_intern (&my_full_id);
4907 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4908 "Mesh for peer [%s] starting\n",
4909 GNUNET_i2s(&my_full_id));
4911 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4912 NULL, /* Closure passed to MESH functions */
4913 &core_init, /* Call core_init once connected */
4914 &core_connect, /* Handle connects */
4915 &core_disconnect, /* remove peers on disconnects */
4916 NULL, /* Don't notify about all incoming messages */
4917 GNUNET_NO, /* For header only in notification */
4918 NULL, /* Don't notify about all outbound messages */
4919 GNUNET_NO, /* For header-only out notification */
4920 core_handlers); /* Register these handlers */
4922 if (core_handle == NULL)
4925 GNUNET_SCHEDULER_shutdown ();
4930 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4933 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4934 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
4935 GNUNET_SERVER_disconnect_notify (server_handle,
4936 &handle_local_client_disconnect, NULL);
4939 clients_head = NULL;
4940 clients_tail = NULL;
4943 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4945 /* Create a peer_info for the local peer */
4946 peer = peer_get (&my_full_id);
4949 GNUNET_PEER_change_rc (myid, 1);
4950 peer_info_add_path (peer, p, GNUNET_YES);
4951 GNUNET_SERVER_resume (server_handle);
4952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
4957 * Process mesh requests.
4959 * @param cls closure
4960 * @param server the initialized server
4961 * @param c configuration to use
4964 run (void *cls, struct GNUNET_SERVER_Handle *server,
4965 const struct GNUNET_CONFIGURATION_Handle *c)
4969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4970 server_handle = server;
4973 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
4976 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4978 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4979 "mesh", "peer/privatekey");
4980 GNUNET_SCHEDULER_shutdown ();
4985 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
4986 &refresh_path_time))
4988 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4990 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4991 "mesh", "refresh path time");
4992 GNUNET_SCHEDULER_shutdown ();
4997 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5000 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5002 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5003 "mesh", "id announce time");
5004 GNUNET_SCHEDULER_shutdown ();
5009 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5012 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5014 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5015 "mesh", "connect timeout");
5016 GNUNET_SCHEDULER_shutdown ();
5021 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5024 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5026 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5027 "mesh", "max msgs queue");
5028 GNUNET_SCHEDULER_shutdown ();
5033 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5036 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5038 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5039 "mesh", "max tunnels");
5040 GNUNET_SCHEDULER_shutdown ();
5045 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5048 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5050 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5051 "mesh", "default ttl", 64);
5056 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5059 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5060 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5061 "mesh", "max peers", 1000);
5066 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5067 &dht_replication_level))
5069 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5071 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5072 "mesh", "dht replication level", 3);
5073 dht_replication_level = 3;
5076 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5077 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5078 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5079 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5081 dht_handle = GNUNET_DHT_connect (c, 64);
5082 if (NULL == dht_handle)
5086 stats = GNUNET_STATISTICS_create ("mesh", c);
5088 GNUNET_SERVER_suspend (server_handle);
5089 /* Scheduled the task to clean up when shutdown is called */
5090 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5092 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
5095 GNUNET_free (keyfile);
5100 * The main function for the mesh service.
5102 * @param argc number of arguments from the command line
5103 * @param argv command line arguments
5104 * @return 0 ok, 1 on error
5107 main (int argc, char *const *argv)
5112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5113 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5115 ret = (GNUNET_OK == r) ? 0 : 1;
5116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5120 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5121 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5122 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);