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
27 * - relay corking down to core
28 * - set ttl relative to path length
31 * - keep queues until receiving ACK
37 #include "mesh_protocol.h"
38 #include "mesh_path.h"
39 #include "block_mesh.h"
40 #include "gnunet_dht_service.h"
41 #include "gnunet_statistics_service.h"
43 #define MESH_BLOOM_SIZE 128
45 #define MESH_DEBUG_DHT GNUNET_NO
46 #define MESH_DEBUG_CONNECTION GNUNET_NO
47 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
49 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
50 GNUNET_TIME_UNIT_MINUTES,\
53 #if MESH_DEBUG_CONNECTION
54 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
56 #define DEBUG_CONN(...)
60 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
62 #define DEBUG_DHT(...)
69 struct timespec __mesh_start;
70 struct timespec __mesh_end;
71 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
72 #define INTERVAL_END \
74 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
75 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
76 if (__diff < 0) __diff += 1000000000;\
80 #define INTERVAL_SHOW \
82 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
84 #define INTERVAL_START
89 /******************************************************************************/
90 /************************ DATA STRUCTURES ****************************/
91 /******************************************************************************/
93 /** FWD declaration */
99 * Struct containing info about a queued transmission to this peer
106 struct MeshPeerQueue *next;
111 struct MeshPeerQueue *prev;
114 * Peer this transmission is directed to.
116 struct MeshPeerInfo *peer;
119 * Tunnel this message belongs to.
121 struct MeshTunnel *tunnel;
124 * Pointer to info stucture used as cls.
134 * Size of the message
141 * Struct containing all information regarding a given peer
151 * Last time we heard from this peer
153 struct GNUNET_TIME_Absolute last_contact;
156 * Number of attempts to reconnect so far
158 int n_reconnect_attempts;
161 * Paths to reach the peer, ordered by ascending hop count
163 struct MeshPeerPath *path_head;
166 * Paths to reach the peer, ordered by ascending hop count
168 struct MeshPeerPath *path_tail;
171 * Handle to stop the DHT search for a path to this peer
173 struct GNUNET_DHT_GetHandle *dhtget;
176 * Array of tunnels this peer is the target of.
177 * Most probably a small amount, therefore not a hashmap.
178 * When the path to the peer changes, notify these tunnels to let them
179 * re-adjust their path trees.
181 struct MeshTunnel **tunnels;
184 * Number of tunnels this peers participates in
186 unsigned int ntunnels;
189 * Transmission queue to core DLL head
191 struct MeshPeerQueue *queue_head;
194 * Transmission queue to core DLL tail
196 struct MeshPeerQueue *queue_tail;
199 * How many messages are in the queue to this peer.
201 unsigned int queue_n;
204 * Handle for queued transmissions
206 struct GNUNET_CORE_TransmitHandle *core_transmit;
211 * Struct to encapsulate all the Flow Control information to a peer in the
212 * context of a tunnel: Same peer in different tunnels will have independent
213 * flow control structures, allowing to choke/free tunnels according to its
216 struct MeshFlowControl
219 * ID of the last packet sent towards the peer.
221 uint32_t last_pid_sent;
224 * ID of the last packet received from the peer.
226 uint32_t last_pid_recv;
229 * Last ACK sent to the peer (peer can't send more than this PID).
231 uint32_t last_ack_sent;
234 * Last ACK sent towards the origin (for traffic towards leaf node).
236 uint32_t last_ack_recv;
239 * How many payload messages are in the queue towards this peer.
244 * Task to poll the peer in case of a lost ACK causes stall.
246 GNUNET_SCHEDULER_TaskIdentifier poll_task;
249 * How frequently to poll for ACKs.
251 struct GNUNET_TIME_Relative poll_time;
254 * On which tunnel to poll.
255 * Using an explicit poll_ctx would not help memory wise,
256 * since the allocated context would have to be stored in the
257 * fc struct in order to free it upon cancelling poll_task.
259 struct MeshTunnel *t;
264 * Globally unique tunnel identification (owner + number)
265 * DO NOT USE OVER THE NETWORK
270 * Node that owns the tunnel
275 * Tunnel number to differentiate all the tunnels owned by the node oid
276 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
278 MESH_TunnelNumber tid;
283 * Struct containing all information regarding a tunnel
284 * For an intermediate node the improtant info used will be:
285 * - id Tunnel unique identification
286 * - paths[0] To know where to send it next
287 * - metainfo: ready, speeds, accounting
294 struct MESH_TunnelID id;
297 * Port of the tunnel.
302 * State of the tunnel.
304 enum MeshTunnelState state;
307 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
309 MESH_TunnelNumber local_tid;
312 * Local tunnel number for local destination clients (incoming number)
313 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
316 MESH_TunnelNumber local_tid_dest;
319 * Is the tunnel bufferless (minimum latency)?
324 * Force sending ACK? Flag to allow duplicate ACK on POLL.
329 * How many messages do we accept in the forward queue.
331 unsigned int queue_max;
334 * Last time the tunnel was used
336 struct GNUNET_TIME_Absolute timestamp;
339 * Destination of the tunnel.
344 * Next hop in the tunnel. If 0, @c client must be set.
346 GNUNET_PEER_Id next_hop;
349 * Previous hop in the tunnel. If 0, @c owner must be set.
351 GNUNET_PEER_Id prev_hop;
354 * Flow control information about @c next_hop or @c client.
356 struct MeshFlowControl next_fc;
359 * Flow control information about @c prev_hop or @c owner.
361 struct MeshFlowControl prev_fc;
364 * Client owner of the tunnel, if any
366 struct MeshClient *owner;
369 * Client destination of the tunnel, if any.
371 struct MeshClient *client;
374 * Task to keep the used paths alive at the owner,
375 * time tunnel out on all the other peers.
377 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
380 * Path being used for the tunnel.
382 struct MeshPeerPath *path;
385 * Flag to signal the destruction of the tunnel.
386 * If this is set GNUNET_YES the tunnel will be destroyed
387 * when the queue is empty.
392 * Total messages pending for this tunnels, payload or not.
394 unsigned int pending_messages;
399 * Struct containing information about a client of the service
401 * TODO: add a list of 'waiting' ports
408 struct MeshClient *next;
413 struct MeshClient *prev;
416 * Tunnels that belong to this client, indexed by local id
418 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
421 * Tunnels this client has accepted, indexed by incoming local id
423 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
426 * Handle to communicate with the client
428 struct GNUNET_SERVER_Client *handle;
431 * Ports that this client has declared interest in.
432 * Indexed by a GMC_hash32 (type), contains *Client.
434 struct GNUNET_CONTAINER_MultiHashMap *ports;
437 * Whether the client is active or shutting down (don't send confirmations
438 * to a client that is shutting down.
443 * ID of the client, mainly for debug messages
450 /******************************************************************************/
451 /************************ DEBUG FUNCTIONS ****************************/
452 /******************************************************************************/
456 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
457 * @param cls string to print
458 * @param success GNUNET_OK if the PUT was transmitted,
459 * GNUNET_NO on timeout,
460 * GNUNET_SYSERR on disconnect from service
461 * after the PUT message was transmitted
462 * (so we don't know if it was received or not)
467 mesh_debug (void *cls, int success)
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
476 unsigned int debug_fwd_ack;
477 unsigned int debug_bck_ack;
481 /******************************************************************************/
482 /*********************** GLOBAL VARIABLES ****************************/
483 /******************************************************************************/
485 /************************** Configuration parameters **************************/
488 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
490 static struct GNUNET_TIME_Relative refresh_path_time;
493 * How often to PUT own ID in the DHT.
495 static struct GNUNET_TIME_Relative id_announce_time;
498 * Maximum time allowed to connect to a peer found by string.
500 static struct GNUNET_TIME_Relative connect_timeout;
503 * Default TTL for payload packets.
505 static unsigned long long default_ttl;
508 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
510 static unsigned long long dht_replication_level;
513 * How many tunnels are we willing to maintain.
514 * Local tunnels are always allowed, even if there are more tunnels than max.
516 static unsigned long long max_tunnels;
519 * How many messages *in total* are we willing to queue, divided by number of
520 * tunnels to get tunnel queue size.
522 static unsigned long long max_msgs_queue;
525 * How many peers do we want to remember?
527 static unsigned long long max_peers;
530 /*************************** Static global variables **************************/
533 * Hostkey generation context
535 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
538 * DLL with all the clients, head.
540 static struct MeshClient *clients_head;
543 * DLL with all the clients, tail.
545 static struct MeshClient *clients_tail;
548 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
550 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
553 * Number of tunnels known.
555 static unsigned long long n_tunnels;
558 * Tunnels incoming, indexed by MESH_TunnelNumber
559 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
561 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
564 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
566 static struct GNUNET_CONTAINER_MultiHashMap *peers;
569 * Handle to communicate with transport
571 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
574 * Handle to communicate with core.
576 static struct GNUNET_CORE_Handle *core_handle;
581 static struct GNUNET_DHT_Handle *dht_handle;
586 static struct GNUNET_SERVER_Handle *server_handle;
589 * Handle to the statistics service.
591 static struct GNUNET_STATISTICS_Handle *stats;
594 * Notification context, to send messages to local clients.
596 static struct GNUNET_SERVER_NotificationContext *nc;
599 * Local peer own ID (memory efficient handle).
601 static GNUNET_PEER_Id myid;
604 * Local peer own ID (full value).
606 static struct GNUNET_PeerIdentity my_full_id;
611 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
616 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
619 * Tunnel ID for the next created tunnel (global tunnel number).
621 static MESH_TunnelNumber next_tid;
624 * Tunnel ID for the next incoming tunnel (local tunnel number).
626 static MESH_TunnelNumber next_local_tid;
629 * All ports clients of this peer have opened.
631 static struct GNUNET_CONTAINER_MultiHashMap *ports;
634 * Task to periodically announce itself in the network.
636 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
639 * Next ID to assign to a client.
641 unsigned int next_client_id;
644 /******************************************************************************/
645 /*********************** DECLARATIONS **************************/
646 /******************************************************************************/
649 * Function to process paths received for a new peer addition. The recorded
650 * paths form the initial tunnel, which can be optimized later.
651 * Called on each result obtained for the DHT search.
654 * @param exp when will this value expire
655 * @param key key of the result
656 * @param type type of the result
657 * @param size number of bytes in data
658 * @param data pointer to the result data
661 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
662 const struct GNUNET_HashCode * key,
663 const struct GNUNET_PeerIdentity *get_path,
664 unsigned int get_path_length,
665 const struct GNUNET_PeerIdentity *put_path,
666 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
667 size_t size, const void *data);
671 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
672 * and insert it in the appropriate structures if the peer is not known yet.
674 * @param peer Full identity of the peer.
676 * @return Existing or newly created peer info.
678 static struct MeshPeerInfo *
679 peer_get (const struct GNUNET_PeerIdentity *peer);
683 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
684 * and insert it in the appropriate structures if the peer is not known yet.
686 * @param peer Short identity of the peer.
688 * @return Existing or newly created peer info.
690 static struct MeshPeerInfo *
691 peer_get_short (const GNUNET_PEER_Id peer);
695 * Build a PeerPath from the paths returned from the DHT, reversing the paths
696 * to obtain a local peer -> destination path and interning the peer ids.
698 * @return Newly allocated and created path
700 static struct MeshPeerPath *
701 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
702 unsigned int get_path_length,
703 const struct GNUNET_PeerIdentity *put_path,
704 unsigned int put_path_length);
708 * Adds a path to the peer_infos of all the peers in the path
710 * @param p Path to process.
711 * @param confirmed Whether we know if the path works or not.
714 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
719 * Search for a tunnel by global ID using full PeerIdentities.
721 * @param oid owner of the tunnel.
722 * @param tid global tunnel number.
724 * @return tunnel handler, NULL if doesn't exist.
726 static struct MeshTunnel *
727 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
731 * Notify a tunnel that a connection has broken that affects at least
734 * @param t Tunnel affected.
735 * @param p1 Peer that got disconnected from p2.
736 * @param p2 Peer that got disconnected from p1.
738 * @return Short ID of the peer disconnected (either p1 or p2).
739 * 0 if the tunnel remained unaffected.
741 static GNUNET_PEER_Id
742 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
747 * @brief Use the given path for the tunnel.
748 * Update the next and prev hops (and RCs).
749 * (Re)start the path refresh in case the tunnel is locally owned.
751 * @param t Tunnel to update.
752 * @param p Path to use.
755 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
758 * Tunnel is empty: destroy it.
760 * Notifies all participants (peers, cleints) about the destruction.
762 * @param t Tunnel to destroy.
765 tunnel_destroy_empty (struct MeshTunnel *t);
768 * @brief Queue and pass message to core when possible.
770 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
771 * and accounts for it. In case the queue is full, the message is dropped and
774 * Otherwise, message is treated as internal and allowed to go regardless of
777 * @param cls Closure (@c type dependant). It will be used by queue_send to
778 * build the message to be sent if not already prebuilt.
779 * @param type Type of the message, 0 for a raw message.
780 * @param size Size of the message.
781 * @param dst Neighbor to send message to.
782 * @param t Tunnel this message belongs to.
785 queue_add (void *cls, uint16_t type, size_t size,
786 struct MeshPeerInfo *dst, struct MeshTunnel *t);
790 * Free a transmission that was already queued with all resources
791 * associated to the request.
793 * @param queue Queue handler to cancel.
794 * @param clear_cls Is it necessary to free associated cls?
797 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
801 * @brief Get the next transmittable message from the queue.
803 * This will be the head, except in the case of being a data packet
804 * not allowed by the destination peer.
806 * @param peer Destination peer.
808 * @return The next viable MeshPeerQueue element to send to that peer.
809 * NULL when there are no transmittable messages.
811 struct MeshPeerQueue *
812 queue_get_next (const struct MeshPeerInfo *peer);
816 * Core callback to write a queued packet to core buffer
818 * @param cls Closure (peer info).
819 * @param size Number of bytes available in buf.
820 * @param buf Where the to write the message.
822 * @return number of bytes written to buf
825 queue_send (void *cls, size_t size, void *buf);
828 /******************************************************************************/
829 /************************ PERIODIC FUNCTIONS ****************************/
830 /******************************************************************************/
833 * Periodically announce self id in the DHT
836 * @param tc task context
839 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
843 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
845 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
849 * - Set data expiration in function of X
852 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
854 block.id = my_full_id;
855 GNUNET_DHT_put (dht_handle, /* DHT handle */
856 &my_full_id.hashPubKey, /* Key to use */
857 dht_replication_level, /* Replication level */
858 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
859 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
860 sizeof (block), /* Size of the data */
861 (const char *) &block, /* Data itself */
862 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
863 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
864 NULL, /* Continuation */
865 NULL); /* Continuation closure */
867 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
871 /******************************************************************************/
872 /****************** GENERAL HELPER FUNCTIONS ************************/
873 /******************************************************************************/
877 * Check if client has registered with the service and has not disconnected
879 * @param client the client to check
881 * @return non-NULL if client exists in the global DLL
883 static struct MeshClient *
884 client_get (struct GNUNET_SERVER_Client *client)
886 struct MeshClient *c;
891 if (c->handle == client)
900 * Deletes a tunnel from a client (either owner or destination). To be used on
903 * @param c Client whose tunnel to delete.
904 * @param t Tunnel which should be deleted.
907 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
909 struct GNUNET_HashCode hash;
913 GMC_hash32 (t->local_tid, &hash);
914 GNUNET_assert (GNUNET_YES ==
915 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
919 else if (c == t->client)
921 GMC_hash32 (t->local_tid_dest, &hash);
922 GNUNET_assert (GNUNET_YES ==
923 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
934 * Notify the appropriate client that a new incoming tunnel was created.
936 * @param t Tunnel that was created.
939 send_client_tunnel_create (struct MeshTunnel *t)
941 struct GNUNET_MESH_TunnelMessage msg;
943 if (NULL == t->client)
945 msg.header.size = htons (sizeof (msg));
946 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
947 msg.tunnel_id = htonl (t->local_tid_dest);
948 msg.port = htonl (t->port);
949 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
950 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
951 &msg.header, GNUNET_NO);
956 * Notify dest client that the incoming tunnel is no longer valid.
958 * @param c Client to notify..
959 * @param t Tunnel that is destroyed.
962 send_client_tunnel_destroy (struct MeshClient *c, struct MeshTunnel *t)
964 struct GNUNET_MESH_TunnelMessage msg;
971 if (c != t->client && c != t->owner)
976 msg.header.size = htons (sizeof (msg));
977 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
978 msg.tunnel_id = htonl (t->local_tid_dest);
979 msg.port = htonl (0);
980 memset(&msg.peer, 0, sizeof (msg.peer));
981 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
982 &msg.header, GNUNET_NO);
987 * Iterator over all the peers to remove the oldest not-used entry.
989 * @param cls Closure (unsued).
990 * @param key ID of the peer.
991 * @param value Peer_Info of the peer.
996 peer_info_timeout (void *cls,
997 const struct GNUNET_HashCode *key,
1004 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1005 * and insert it in the appropriate structures if the peer is not known yet.
1007 * @param peer Full identity of the peer.
1009 * @return Existing or newly created peer info.
1011 static struct MeshPeerInfo *
1012 peer_get (const struct GNUNET_PeerIdentity *peer)
1014 struct MeshPeerInfo *peer_info;
1016 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1017 if (NULL == peer_info)
1020 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1021 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1023 GNUNET_CONTAINER_multihashmap_iterate (peers,
1027 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1028 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1029 peer_info->id = GNUNET_PEER_intern (peer);
1031 peer_info->last_contact = GNUNET_TIME_absolute_get();
1038 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1039 * and insert it in the appropriate structures if the peer is not known yet.
1041 * @param peer Short identity of the peer.
1043 * @return Existing or newly created peer info.
1045 static struct MeshPeerInfo *
1046 peer_get_short (const GNUNET_PEER_Id peer)
1048 struct GNUNET_PeerIdentity id;
1050 GNUNET_PEER_resolve (peer, &id);
1051 return peer_get (&id);
1056 * Choose the best path towards a peer considering the tunnel properties.
1058 * @param peer The destination peer.
1059 * @param t The tunnel the path is for.
1061 * @return Best current known path towards the peer, if any.
1063 static struct MeshPeerPath *
1064 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1066 struct MeshPeerPath *best_p;
1067 struct MeshPeerPath *p;
1068 unsigned int best_cost;
1071 best_p = p = peer->path_head;
1072 best_cost = cost = p->length;
1075 if ((cost = p->length) < best_cost)
1086 * Core callback to write a pre-constructed data packet to core buffer
1088 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1089 * @param size Number of bytes available in buf.
1090 * @param buf Where the to write the message.
1092 * @return number of bytes written to buf
1095 send_core_data_raw (void *cls, size_t size, void *buf)
1097 struct GNUNET_MessageHeader *msg = cls;
1100 GNUNET_assert (NULL != msg);
1101 total_size = ntohs (msg->size);
1103 if (total_size > size)
1108 memcpy (buf, msg, total_size);
1115 * Sends an already built message to a peer, properly registrating
1116 * all used resources.
1118 * @param message Message to send. Function makes a copy of it.
1119 * @param peer Short ID of the neighbor whom to send the message.
1120 * @param t Tunnel on which this message is transmitted.
1123 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1124 GNUNET_PEER_Id peer,
1125 struct MeshTunnel *t)
1127 struct GNUNET_PeerIdentity id;
1128 struct MeshPeerInfo *neighbor;
1129 struct MeshPeerPath *p;
1134 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1139 size = ntohs (message->size);
1140 data = GNUNET_malloc (size);
1141 memcpy (data, message, size);
1142 type = ntohs(message->type);
1143 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
1144 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
1146 struct GNUNET_MESH_Data *u;
1148 u = (struct GNUNET_MESH_Data *) data;
1149 u->ttl = htonl (ntohl (u->ttl) - 1);
1151 GNUNET_PEER_resolve (peer, &id);
1152 neighbor = peer_get (&id);
1153 for (p = neighbor->path_head; NULL != p; p = p->next)
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 " %s IS NOT DIRECTLY CONNECTED\n",
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1169 for (p = neighbor->path_head; NULL != p; p = p->next)
1171 struct GNUNET_PeerIdentity debug_id;
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1175 " path with %u hops through:\n",
1177 for (i = 0; i < p->length; i++)
1179 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1182 i, GNUNET_i2s(&debug_id));
1186 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1188 " no direct connection to %s\n",
1193 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1204 * Sends a CREATE PATH message for a path to a peer, properly registrating
1205 * all used resources.
1207 * @param t Tunnel for which the path is created.
1210 send_create_path (struct MeshTunnel *t)
1212 struct MeshPeerInfo *neighbor;
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
1215 neighbor = peer_get_short (t->next_hop);
1217 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1218 sizeof (struct GNUNET_MESH_CreateTunnel) +
1219 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1222 t->state = MESH_TUNNEL_WAITING;
1227 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1229 * @param t Tunnel which to confirm.
1232 send_path_ack (struct MeshTunnel *t)
1234 struct MeshPeerInfo *peer;
1236 peer = peer_get_short (t->prev_hop);
1239 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1240 sizeof (struct GNUNET_MESH_PathACK),
1247 * Try to establish a new connection to this peer in the given tunnel.
1248 * If the peer doesn't have any path to it yet, try to get one.
1249 * If the peer already has some path, send a CREATE PATH towards it.
1251 * @param peer PeerInfo of the peer.
1252 * @param t Tunnel for which to create the path, if possible.
1255 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1257 struct MeshPeerPath *p;
1259 if (NULL != peer->path_head)
1261 p = peer_get_best_path (peer, t);
1262 tunnel_use_path (t, p);
1263 send_create_path (t);
1265 else if (NULL == peer->dhtget)
1267 struct GNUNET_PeerIdentity id;
1269 GNUNET_PEER_resolve (peer->id, &id);
1270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1271 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1272 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1273 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1274 &id.hashPubKey, /* key to search */
1275 dht_replication_level, /* replication level */
1276 GNUNET_DHT_RO_RECORD_ROUTE |
1277 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1279 0, /* xquery bits */
1280 &dht_get_id_handler, peer);
1281 t->state = MESH_TUNNEL_SEARCHING;
1285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1286 "There is no path but the DHT GET is already started.\n");
1292 * Destroy the peer_info and free any allocated resources linked to it
1294 * @param pi The peer_info to destroy.
1296 * @return GNUNET_OK on success
1299 peer_info_destroy (struct MeshPeerInfo *pi)
1301 struct GNUNET_PeerIdentity id;
1302 struct MeshPeerPath *p;
1303 struct MeshPeerPath *nextp;
1306 GNUNET_PEER_resolve (pi->id, &id);
1307 GNUNET_PEER_change_rc (pi->id, -1);
1310 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1314 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1316 if (NULL != pi->dhtget)
1318 GNUNET_DHT_get_stop (pi->dhtget);
1324 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1328 for (i = 0; i < pi->ntunnels; i++)
1329 tunnel_destroy_empty (pi->tunnels[i]);
1330 GNUNET_array_grow (pi->tunnels, pi->ntunnels, 0);
1337 * Remove all paths that rely on a direct connection between p1 and p2
1338 * from the peer itself and notify all tunnels about it.
1340 * @param peer PeerInfo of affected peer.
1341 * @param p1 GNUNET_PEER_Id of one peer.
1342 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1345 * TODO: optimize (see below)
1348 peer_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1351 struct MeshPeerPath *p;
1352 struct MeshPeerPath *next;
1353 struct MeshPeerInfo *peer_d;
1355 unsigned int destroyed;
1358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
1360 for (p = peer->path_head; NULL != p; p = next)
1363 for (i = 0; i < (p->length - 1); i++)
1365 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1366 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1368 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1378 for (i = 0; i < peer->ntunnels; i++)
1380 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1384 peer_d = peer_get_short (d);
1385 next = peer_get_best_path (peer_d, peer->tunnels[i]);
1386 tunnel_use_path (peer->tunnels[i], next);
1387 peer_connect (peer_d, peer->tunnels[i]);
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
1394 * Add the path to the peer and update the path used to reach it in case this
1397 * @param peer_info Destination peer to add the path to.
1398 * @param path New path to add. Last peer must be the peer in arg 1.
1399 * Path will be either used of freed if already known.
1400 * @param trusted Do we trust that this path is real?
1403 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1406 struct MeshPeerPath *aux;
1410 if ((NULL == peer_info) || (NULL == path))
1413 path_destroy (path);
1416 if (path->peers[path->length - 1] != peer_info->id)
1419 path_destroy (path);
1422 if (2 >= path->length && GNUNET_NO == trusted)
1424 /* Only allow CORE to tell us about direct paths */
1425 path_destroy (path);
1428 for (l = 1; l < path->length; l++)
1430 if (path->peers[l] == myid)
1432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1433 for (l2 = 0; l2 < path->length - l; l2++)
1435 path->peers[l2] = path->peers[l + l2];
1440 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1445 struct GNUNET_PeerIdentity id;
1447 GNUNET_PEER_resolve (peer_info->id, &id);
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1449 path->length, GNUNET_i2s (&id));
1452 l = path_get_length (path);
1455 path_destroy (path);
1459 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1460 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1462 l2 = path_get_length (aux);
1465 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1466 peer_info->path_tail, aux, path);
1471 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1473 path_destroy (path);
1478 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1485 * Add the path to the origin peer and update the path used to reach it in case
1486 * this is the shortest.
1487 * The path is given in peer_info -> destination, therefore we turn the path
1488 * upside down first.
1490 * @param peer_info Peer to add the path to, being the origin of the path.
1491 * @param path New path to add after being inversed.
1492 * Path will be either used or freed.
1493 * @param trusted Do we trust that this path is real?
1496 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1497 struct MeshPeerPath *path, int trusted)
1500 peer_info_add_path (peer_info, path, trusted);
1505 * Add a tunnel to the list of tunnels a peer participates in.
1506 * Update the tunnel's destination.
1508 * @param p Peer to add to.
1509 * @param t Tunnel to add.
1512 peer_info_add_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1516 GNUNET_break (t->dest == p->id);
1520 GNUNET_PEER_change_rc (t->dest, 1);
1521 GNUNET_array_append (p->tunnels, p->ntunnels, t);
1526 * Remove a tunnel from the list of tunnels a peer participates in.
1527 * Free the tunnel's destination.
1529 * @param p Peer to clean.
1530 * @param t Tunnel to remove.
1533 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1537 if (t->dest == p->id)
1539 GNUNET_PEER_change_rc (t->dest, -1);
1542 for (i = 0; i < p->ntunnels; i++)
1544 if (p->tunnels[i] == t)
1546 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1547 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1555 * Function called if the connection to the peer has been stalled for a while,
1556 * possibly due to a missed ACK. Poll the peer about its ACK status.
1558 * @param cls Closure (poll ctx).
1559 * @param tc TaskContext.
1562 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1564 struct MeshFlowControl *fc = cls;
1565 struct GNUNET_MESH_Poll msg;
1566 struct MeshTunnel *t = fc->t;
1567 GNUNET_PEER_Id peer;
1569 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1570 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1575 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1576 msg.header.size = htons (sizeof (msg));
1577 msg.tid = htonl (t->id.tid);
1578 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1580 if (fc == &t->prev_fc)
1584 else if (fc == &t->next_fc)
1593 send_prebuilt_message (&msg.header, peer, t);
1594 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1595 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1601 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1602 * to obtain a local peer -> destination path and interning the peer ids.
1604 * @return Newly allocated and created path
1606 static struct MeshPeerPath *
1607 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1608 unsigned int get_path_length,
1609 const struct GNUNET_PeerIdentity *put_path,
1610 unsigned int put_path_length)
1612 struct MeshPeerPath *p;
1618 GNUNET_PEER_change_rc (myid, 1);
1619 i = get_path_length;
1620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1621 for (i--; i >= 0; i--)
1623 id = GNUNET_PEER_intern (&get_path[i]);
1624 if (p->length > 0 && id == p->peers[p->length - 1])
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1627 GNUNET_PEER_change_rc (id, -1);
1631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1632 GNUNET_i2s (&get_path[i]));
1634 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1635 p->peers[p->length - 1] = id;
1638 i = put_path_length;
1639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1640 for (i--; i >= 0; i--)
1642 id = GNUNET_PEER_intern (&put_path[i]);
1645 /* PUT path went through us, so discard the path up until now and start
1646 * from here to get a much shorter (and loop-free) path.
1651 if (p->length > 0 && id == p->peers[p->length - 1])
1653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1654 GNUNET_PEER_change_rc (id, -1);
1658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1659 GNUNET_i2s (&put_path[i]));
1661 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1662 p->peers[p->length - 1] = id;
1666 if (get_path_length > 0)
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1668 GNUNET_i2s (&get_path[0]));
1669 if (put_path_length > 0)
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1671 GNUNET_i2s (&put_path[0]));
1672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1674 for (i = 0; i < p->length; i++)
1676 struct GNUNET_PeerIdentity peer_id;
1678 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1680 GNUNET_i2s (&peer_id));
1688 * Adds a path to the peer_infos of all the peers in the path
1690 * @param p Path to process.
1691 * @param confirmed Whether we know if the path works or not.
1694 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1698 /* TODO: invert and add */
1699 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1700 for (i++; i < p->length; i++)
1702 struct MeshPeerInfo *aux;
1703 struct MeshPeerPath *copy;
1705 aux = peer_get_short (p->peers[i]);
1706 copy = path_duplicate (p);
1707 copy->length = i + 1;
1708 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1714 * Send keepalive packets for a peer
1716 * @param cls Closure (tunnel for which to send the keepalive).
1717 * @param tc Notification context.
1720 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1724 * Search for a tunnel among the incoming tunnels
1726 * @param tid the local id of the tunnel
1728 * @return tunnel handler, NULL if doesn't exist
1730 static struct MeshTunnel *
1731 tunnel_get_incoming (MESH_TunnelNumber tid)
1733 struct GNUNET_HashCode hash;
1735 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1736 GMC_hash32 (tid, &hash);
1737 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1742 * Search for a tunnel among the tunnels for a client
1744 * @param c the client whose tunnels to search in
1745 * @param tid the local id of the tunnel
1747 * @return tunnel handler, NULL if doesn't exist
1749 static struct MeshTunnel *
1750 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1752 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1754 return tunnel_get_incoming (tid);
1758 struct GNUNET_HashCode hash;
1760 GMC_hash32 (tid, &hash);
1761 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1767 * Search for a tunnel by global ID using PEER_ID
1769 * @param pi owner of the tunnel
1770 * @param tid global tunnel number
1772 * @return tunnel handler, NULL if doesn't exist
1774 static struct MeshTunnel *
1775 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1777 struct MESH_TunnelID id;
1778 struct GNUNET_HashCode hash;
1783 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1784 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1789 * Search for a tunnel by global ID using full PeerIdentities
1791 * @param oid owner of the tunnel
1792 * @param tid global tunnel number
1794 * @return tunnel handler, NULL if doesn't exist
1796 static struct MeshTunnel *
1797 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1799 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1804 * Add a client to a tunnel, initializing all needed data structures.
1806 * @param t Tunnel to which add the client.
1807 * @param c Client which to add to the tunnel.
1810 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1812 struct GNUNET_HashCode hash;
1814 if (NULL != t->client)
1819 GMC_hash32 (t->local_tid_dest, &hash);
1821 GNUNET_CONTAINER_multihashmap_put (c->incoming_tunnels, &hash, t,
1822 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1828 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1829 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1839 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1841 unsigned int own_pos;
1843 for (own_pos = 0; own_pos < p->length; own_pos++)
1845 if (p->peers[own_pos] == myid)
1848 if (own_pos > p->length - 1)
1854 if (own_pos < p->length - 1)
1855 t->next_hop = p->peers[own_pos + 1];
1857 t->next_hop = p->peers[own_pos];
1858 GNUNET_PEER_change_rc (t->next_hop, 1);
1860 t->prev_hop = p->peers[own_pos - 1];
1862 t->prev_hop = p->peers[0];
1863 GNUNET_PEER_change_rc (t->prev_hop, 1);
1865 if (NULL != t->path)
1866 path_destroy (t->path);
1867 t->path = path_duplicate (p);
1870 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1871 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1872 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
1879 * Notifies a tunnel that a connection has broken that affects at least
1880 * some of its peers. Sends a notification towards the root of the tree.
1881 * In case the peer is the owner of the tree, notifies the client that owns
1882 * the tunnel and tries to reconnect.
1884 * @param t Tunnel affected.
1885 * @param p1 Peer that got disconnected from p2.
1886 * @param p2 Peer that got disconnected from p1.
1888 * @return Short ID of the peer disconnected (either p1 or p2).
1889 * 0 if the tunnel remained unaffected.
1891 static GNUNET_PEER_Id
1892 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1895 // if (myid != p1 && myid != p2) FIXME
1900 // if (tree_get_predecessor (t->tree) != 0)
1902 // /* We are the peer still connected, notify owner of the disconnection. */
1903 // struct GNUNET_MESH_PathBroken msg;
1904 // struct GNUNET_PeerIdentity neighbor;
1906 // msg.header.size = htons (sizeof (msg));
1907 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1908 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1909 // msg.tid = htonl (t->id.tid);
1910 // msg.peer1 = my_full_id;
1911 // GNUNET_PEER_resolve (pid, &msg.peer2);
1912 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1913 // send_prebuilt_message (&msg.header, &neighbor, t);
1920 * Build a local ACK message and send it to a local client.
1922 * @param t Tunnel on which to send the ACK.
1923 * @param c Client to whom send the ACK.
1924 * @param ack Value of the ACK.
1927 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
1929 struct GNUNET_MESH_LocalAck msg;
1931 msg.header.size = htons (sizeof (msg));
1932 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1933 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
1934 msg.ack = htonl (ack);
1935 GNUNET_SERVER_notification_context_unicast(nc,
1942 * Build an ACK message and queue it to send to the given peer.
1944 * @param t Tunnel on which to send the ACK.
1945 * @param peer Peer to whom send the ACK.
1946 * @param ack Value of the ACK.
1949 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1951 struct GNUNET_MESH_ACK msg;
1953 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1954 msg.header.size = htons (sizeof (msg));
1955 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1956 msg.pid = htonl (ack);
1957 msg.tid = htonl (t->id.tid);
1959 send_prebuilt_message (&msg.header, peer, t);
1964 * Send an ACK informing the predecessor about the available buffer space.
1965 * In case there is no predecessor, inform the owning client.
1966 * If buffering is off, send only on behalf of children or self if endpoint.
1967 * If buffering is on, send when sent to children and buffer space is free.
1968 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
1969 * the ACK itself goes "back" (towards root).
1971 * @param t Tunnel on which to send the ACK.
1972 * @param type Type of message that triggered the ACK transmission.
1975 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
1979 /* Is it after unicast retransmission? */
1982 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1984 "ACK due to FWD DATA retransmission\n");
1985 if (GNUNET_YES == t->nobuffer)
1987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
1991 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1992 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1994 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
1995 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1996 t->force_ack = GNUNET_YES;
2002 /* Check if we need to transmit the ACK */
2003 if (t->queue_max > t->next_fc.queue_n * 4 &&
2004 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
2005 GNUNET_NO == t->force_ack)
2007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2009 " t->qmax: %u, t->qn: %u\n",
2010 t->queue_max, t->next_fc.queue_n);
2011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2012 " t->pid: %u, t->ack: %u\n",
2013 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
2017 /* Ok, ACK might be necessary, what PID to ACK? */
2018 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
2019 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
2021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2025 t->prev_fc.last_ack_sent = ack;
2026 if (NULL != t->owner)
2027 send_local_ack (t, t->owner, ack);
2028 else if (0 != t->prev_hop)
2029 send_ack (t, t->prev_hop, ack);
2033 t->force_ack = GNUNET_NO;
2038 * Send an ACK informing the children node/client about the available
2040 * If buffering is off, send only on behalf of root (can be self).
2041 * If buffering is on, send when sent to predecessor and buffer space is free.
2042 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2043 * the ACK itself goes "forward" (towards children/clients).
2045 * @param t Tunnel on which to send the ACK.
2046 * @param type Type of message that triggered the ACK transmission.
2049 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2053 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2054 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2055 /* Is it after data to_origin retransmission? */
2058 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2059 if (GNUNET_YES == t->nobuffer)
2061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2062 " Not sending ACK, nobuffer + traffic\n");
2066 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2067 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2069 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2070 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2071 t->force_ack = GNUNET_YES;
2077 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2079 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2081 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2084 " Not sending ACK, not needed, last ack sent was %u\n",
2085 t->next_fc.last_ack_sent);
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2089 " Sending BCK ACK %u (last sent: %u)\n",
2090 ack, t->next_fc.last_ack_sent);
2091 t->next_fc.last_ack_sent = ack;
2093 if (NULL != t->client)
2094 send_local_ack (t, t->client, ack);
2095 else if (0 != t->next_hop)
2096 send_ack (t, t->next_hop, ack);
2099 t->force_ack = GNUNET_NO;
2104 * Modify the unicast message TID from global to local and send to client.
2106 * @param t Tunnel on which to send the message.
2107 * @param msg Message to modify and send.
2110 tunnel_send_client_ucast (struct MeshTunnel *t,
2111 const struct GNUNET_MESH_Data *msg)
2113 struct GNUNET_MESH_Data *copy;
2114 uint16_t size = ntohs (msg->header.size);
2117 if (size < sizeof (struct GNUNET_MESH_Data) +
2118 sizeof (struct GNUNET_MessageHeader))
2120 GNUNET_break_op (0);
2123 if (NULL == t->client)
2128 copy = (struct GNUNET_MESH_Data *) cbuf;
2129 memcpy (copy, msg, size);
2130 copy->tid = htonl (t->local_tid_dest);
2131 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
2132 ©->header, GNUNET_NO);
2137 * Modify the to_origin message TID from global to local and send to client.
2139 * @param t Tunnel on which to send the message.
2140 * @param msg Message to modify and send.
2143 tunnel_send_client_to_orig (struct MeshTunnel *t,
2144 const struct GNUNET_MESH_Data *msg)
2146 struct GNUNET_MESH_Data *copy;
2147 uint16_t size = ntohs (msg->header.size);
2150 if (size < sizeof (struct GNUNET_MESH_Data) +
2151 sizeof (struct GNUNET_MessageHeader))
2153 GNUNET_break_op (0);
2156 if (NULL == t->owner)
2161 copy = (struct GNUNET_MESH_Data *) cbuf;
2162 memcpy (cbuf, msg, size);
2163 copy->tid = htonl (t->local_tid);
2164 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2165 ©->header, GNUNET_NO);
2170 * @brief Re-initiate traffic to this peer if necessary.
2172 * Check if there is traffic queued towards this peer
2173 * and the core transmit handle is NULL (traffic was stalled).
2174 * If so, call core tmt rdy.
2176 * @param peer_id Short ID of peer to which initiate traffic.
2179 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2181 struct MeshPeerInfo *peer;
2182 struct GNUNET_PeerIdentity id;
2183 struct MeshPeerQueue *q;
2186 peer = peer_get_short (peer_id);
2187 if (NULL != peer->core_transmit)
2190 q = queue_get_next (peer);
2193 /* Might br multicast traffic already sent to this particular peer but
2194 * not to other children in this tunnel.
2195 * This way t->queue_n would be > 0 but the queue of this particular peer
2201 GNUNET_PEER_resolve (peer->id, &id);
2202 peer->core_transmit =
2203 GNUNET_CORE_notify_transmit_ready(core_handle,
2206 GNUNET_TIME_UNIT_FOREVER_REL,
2216 * Send a message to all peers and clients in this tunnel that the tunnel
2217 * is no longer valid. If some peer or client should not receive the message,
2218 * should be zero'ed out before calling this function.
2220 * @param t The tunnel whose peers and clients to notify.
2223 tunnel_send_destroy (struct MeshTunnel *t)
2225 struct GNUNET_MESH_TunnelDestroy msg;
2226 struct GNUNET_PeerIdentity id;
2228 msg.header.size = htons (sizeof (msg));
2229 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2230 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2231 msg.tid = htonl (t->id.tid);
2232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2233 " sending tunnel destroy for tunnel: %s [%X]\n",
2234 GNUNET_i2s (&msg.oid), t->id.tid);
2236 if (NULL == t->client && 0 != t->next_hop)
2238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2239 GNUNET_PEER_resolve (t->next_hop, &id);
2240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2241 " sending forward to %s\n",
2243 send_prebuilt_message (&msg.header, t->next_hop, t);
2245 if (NULL == t->owner && 0 != t->prev_hop)
2247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2248 GNUNET_PEER_resolve (t->prev_hop, &id);
2249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2250 " sending back to %s\n",
2252 send_prebuilt_message (&msg.header, t->prev_hop, t);
2254 if (NULL != t->owner)
2256 send_client_tunnel_destroy (t->owner, t);
2258 if (NULL != t->client)
2260 send_client_tunnel_destroy (t->client, t);
2266 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2268 * @param t Tunnel which to cancel.
2269 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2272 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2274 struct MeshPeerInfo *peer_info;
2275 struct MeshPeerQueue *pq;
2276 struct MeshPeerQueue *next;
2279 return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
2280 peer_info = peer_get_short (neighbor);
2281 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2284 if (pq->tunnel == t)
2286 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2287 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2289 /* Should have been removed on destroy children */
2292 queue_destroy (pq, GNUNET_YES);
2295 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2297 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2298 peer_info->core_transmit = NULL;
2304 * Destroy the tunnel.
2306 * This function does not generate any warning traffic to clients or peers.
2309 * Remove the tunnel from peer_info's and clients' hashmaps.
2310 * Cancel messages belonging to this tunnel queued to neighbors.
2311 * Free any allocated resources linked to the tunnel.
2313 * @param t the tunnel to destroy
2315 * @return GNUNET_OK on success
2318 tunnel_destroy (struct MeshTunnel *t)
2320 struct MeshClient *c;
2321 struct GNUNET_HashCode hash;
2331 struct GNUNET_PeerIdentity id;
2333 GNUNET_PEER_resolve (t->id.oid, &id);
2334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2335 GNUNET_i2s (&id), t->id.tid);
2337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2341 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2342 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2350 GMC_hash32 (t->local_tid, &hash);
2352 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2359 if (NULL != t->client)
2362 GMC_hash32 (t->local_tid_dest, &hash);
2364 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2370 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2377 if (0 != t->prev_hop)
2379 peer_cancel_queues (t->prev_hop, t);
2380 GNUNET_PEER_change_rc (t->prev_hop, -1);
2382 if (0 != t->next_hop)
2384 peer_cancel_queues (t->next_hop, t);
2385 GNUNET_PEER_change_rc (t->next_hop, -1);
2388 peer_info_remove_tunnel (peer_get_short (t->dest), t);
2391 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2392 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2395 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2396 path_destroy (t->path);
2402 * Tunnel is empty: destroy it.
2404 * Notifies all participants (peers, cleints) about the destruction.
2406 * @param t Tunnel to destroy.
2409 tunnel_destroy_empty (struct MeshTunnel *t)
2413 struct GNUNET_PeerIdentity id;
2415 GNUNET_PEER_resolve (t->id.oid, &id);
2416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2417 "executing destruction of empty tunnel %s [%X]\n",
2418 GNUNET_i2s (&id), t->id.tid);
2422 if (GNUNET_NO == t->destroy)
2423 tunnel_send_destroy (t);
2424 if (0 == t->pending_messages)
2427 t->destroy = GNUNET_YES;
2431 * Initialize a Flow Control structure to the initial state.
2433 * @param fc Flow Control structure to initialize.
2436 fc_init (struct MeshFlowControl *fc)
2438 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2439 fc->last_pid_recv = (uint32_t) -1;
2440 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2441 fc->last_ack_recv = (uint32_t) -1;
2442 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2443 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2448 * Create a new tunnel
2450 * @param owner Who is the owner of the tunnel (short ID).
2451 * @param tid Tunnel Number of the tunnel.
2452 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2453 * @param local Tunnel Number for the tunnel, for the client point of view.
2455 * @return A new initialized tunnel. NULL on error.
2457 static struct MeshTunnel *
2458 tunnel_new (GNUNET_PEER_Id owner,
2459 MESH_TunnelNumber tid,
2460 struct MeshClient *client,
2461 MESH_TunnelNumber local)
2463 struct MeshTunnel *t;
2464 struct GNUNET_HashCode hash;
2466 if (n_tunnels >= max_tunnels && NULL == client)
2469 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2472 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2474 fc_init (&t->next_fc);
2475 fc_init (&t->prev_fc);
2476 t->local_tid = local;
2478 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2480 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2482 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2483 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2490 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2497 GMC_hash32 (t->local_tid, &hash);
2499 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2500 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2504 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2515 * Iterator for deleting each tunnel whose client endpoint disconnected.
2517 * @param cls Closure (client that has disconnected).
2518 * @param key The hash of the local tunnel id (used to access the hashmap).
2519 * @param value The value stored at the key (tunnel to destroy).
2521 * @return GNUNET_OK, keep iterating.
2524 tunnel_destroy_iterator (void *cls,
2525 const struct GNUNET_HashCode * key,
2528 struct MeshTunnel *t = value;
2529 struct MeshClient *c = cls;
2531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2532 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2533 t->local_tid, t->local_tid_dest, c->id);
2534 client_delete_tunnel (c, t);
2537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2539 if (0 != t->next_hop) { /* destroy could come before a path is used */
2540 GNUNET_PEER_change_rc (t->next_hop, -1);
2544 else if (c == t->owner)
2546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2548 if (0 != t->prev_hop) { /* destroy could come before a path is used */
2549 GNUNET_PEER_change_rc (t->prev_hop, -1);
2557 tunnel_destroy_empty (t);
2564 * Timeout function, destroys tunnel if called
2566 * @param cls Closure (tunnel to destroy).
2567 * @param tc TaskContext
2570 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2572 struct MeshTunnel *t = cls;
2573 struct GNUNET_PeerIdentity id;
2575 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2576 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2578 GNUNET_PEER_resolve(t->id.oid, &id);
2579 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2580 "Tunnel %s [%X] timed out. Destroying.\n",
2581 GNUNET_i2s(&id), t->id.tid);
2582 if (NULL != t->client)
2583 send_client_tunnel_destroy (t->client, t);
2584 tunnel_destroy (t); /* Do not notify other */
2589 * Resets the tunnel timeout. Starts it if no timeout was running.
2591 * @param t Tunnel whose timeout to reset.
2593 * TODO use heap to improve efficiency of scheduler.
2596 tunnel_reset_timeout (struct MeshTunnel *t)
2598 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2600 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2601 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2602 t->maintenance_task =
2603 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2604 (refresh_path_time, 4), &tunnel_timeout, t);
2608 /******************************************************************************/
2609 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2610 /******************************************************************************/
2613 * Function to send a create path packet to a peer.
2615 * @param cls closure
2616 * @param size number of bytes available in buf
2617 * @param buf where the callee should write the message
2618 * @return number of bytes written to buf
2621 send_core_path_create (void *cls, size_t size, void *buf)
2623 struct MeshTunnel *t = cls;
2624 struct GNUNET_MESH_CreateTunnel *msg;
2625 struct GNUNET_PeerIdentity *peer_ptr;
2626 struct MeshPeerPath *p = t->path;
2631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2633 sizeof (struct GNUNET_MESH_CreateTunnel) +
2634 p->length * sizeof (struct GNUNET_PeerIdentity);
2636 if (size < size_needed || NULL == buf)
2641 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2642 msg->header.size = htons (size_needed);
2643 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2644 msg->tid = ntohl (t->id.tid);
2647 if (GNUNET_YES == t->nobuffer)
2648 opt |= MESH_TUNNEL_OPT_NOBUFFER;
2649 msg->opt = htonl (opt);
2650 msg->port = htonl (t->port);
2652 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2653 for (i = 0; i < p->length; i++)
2655 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2659 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2665 * Creates a path ack message in buf and frees all unused resources.
2667 * @param cls closure (MeshTransmissionDescriptor)
2668 * @param size number of bytes available in buf
2669 * @param buf where the callee should write the message
2670 * @return number of bytes written to buf
2673 send_core_path_ack (void *cls, size_t size, void *buf)
2675 struct MeshTunnel *t = cls;
2676 struct GNUNET_MESH_PathACK *msg = buf;
2678 GNUNET_assert (NULL != t);
2679 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2684 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
2685 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2686 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2687 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
2688 msg->tid = htonl (t->id.tid);
2689 msg->peer_id = my_full_id;
2690 msg->ack = htonl (t->prev_fc.last_ack_sent);
2692 /* TODO add signature */
2694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2695 return sizeof (struct GNUNET_MESH_PathACK);
2700 * Free a transmission that was already queued with all resources
2701 * associated to the request.
2703 * @param queue Queue handler to cancel.
2704 * @param clear_cls Is it necessary to free associated cls?
2707 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2709 struct MeshFlowControl *fc;
2711 if (GNUNET_YES == clear_cls)
2713 switch (queue->type)
2715 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2716 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2717 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2719 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2720 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2721 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2722 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2723 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2725 " prebuilt message\n");
2726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2728 GNUNET_MESH_DEBUG_M2S (queue->type));
2730 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2735 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2736 " type %s unknown!\n",
2737 GNUNET_MESH_DEBUG_M2S (queue->type));
2739 GNUNET_free_non_null (queue->cls);
2741 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2742 queue->peer->queue_tail,
2745 /* Delete from appropriate fc in the tunnel */
2746 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
2747 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
2749 if (queue->peer->id == queue->tunnel->prev_hop)
2750 fc = &queue->tunnel->prev_fc;
2751 else if (queue->peer->id == queue->tunnel->next_hop)
2752 fc = &queue->tunnel->next_fc;
2760 GNUNET_free (queue);
2765 * @brief Get the next transmittable message from the queue.
2767 * This will be the head, except in the case of being a data packet
2768 * not allowed by the destination peer.
2770 * @param peer Destination peer.
2772 * @return The next viable MeshPeerQueue element to send to that peer.
2773 * NULL when there are no transmittable messages.
2775 struct MeshPeerQueue *
2776 queue_get_next (const struct MeshPeerInfo *peer)
2778 struct MeshPeerQueue *q;
2780 struct GNUNET_MESH_Data *dmsg;
2781 struct MeshTunnel* t;
2785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
2786 for (q = peer->queue_head; NULL != q; q = q->next)
2789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2791 GNUNET_MESH_DEBUG_M2S (q->type));
2792 dmsg = (struct GNUNET_MESH_Data *) q->cls;
2793 pid = ntohl (dmsg->pid);
2796 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2797 ack = t->next_fc.last_ack_recv;
2799 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2800 ack = t->prev_fc.last_ack_recv;
2803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2808 "* ACK: %u, PID: %u\n",
2810 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2823 "* nothing found\n");
2829 queue_send (void *cls, size_t size, void *buf)
2831 struct MeshPeerInfo *peer = cls;
2832 struct GNUNET_MessageHeader *msg;
2833 struct MeshPeerQueue *queue;
2834 struct MeshTunnel *t;
2835 struct GNUNET_PeerIdentity dst_id;
2836 struct MeshFlowControl *fc;
2841 peer->core_transmit = NULL;
2843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
2844 queue = queue_get_next (peer);
2846 /* Queue has no internal mesh traffic nor sendable payload */
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
2850 if (NULL == peer->queue_head)
2851 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
2854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
2856 GNUNET_PEER_resolve (peer->id, &dst_id);
2857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2859 GNUNET_i2s (&dst_id));
2860 /* Check if buffer size is enough for the message */
2861 if (queue->size > size)
2863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2864 "* not enough room, reissue\n");
2865 peer->core_transmit =
2866 GNUNET_CORE_notify_transmit_ready (core_handle,
2869 GNUNET_TIME_UNIT_FOREVER_REL,
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
2879 GNUNET_assert (0 < t->pending_messages);
2880 t->pending_messages--;
2884 switch (queue->type)
2887 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2888 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2889 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
2890 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
2891 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2892 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2895 GNUNET_MESH_DEBUG_M2S (queue->type));
2897 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2898 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2899 data_size = send_core_data_raw (queue->cls, size, buf);
2900 msg = (struct GNUNET_MessageHeader *) buf;
2901 type = ntohs (msg->type);
2903 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
2905 data_size = send_core_path_create (queue->cls, size, buf);
2907 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
2909 data_size = send_core_path_ack (queue->cls, size, buf);
2913 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2914 "* type unknown: %u\n",
2919 /* Free queue, but cls was freed by send_core_* */
2920 queue_destroy (queue, GNUNET_NO);
2922 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
2923 pid = ((struct GNUNET_MESH_Data *) buf)->pid;
2926 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2927 t->next_fc.last_pid_sent = pid;
2928 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
2930 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2931 t->prev_fc.last_pid_sent = pid;
2932 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
2938 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
2940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
2944 /* If more data in queue, send next */
2945 queue = queue_get_next (peer);
2948 struct GNUNET_PeerIdentity id;
2950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
2951 GNUNET_PEER_resolve (peer->id, &id);
2952 peer->core_transmit =
2953 GNUNET_CORE_notify_transmit_ready(core_handle,
2956 GNUNET_TIME_UNIT_FOREVER_REL,
2962 else if (NULL != peer->queue_head)
2964 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2966 GNUNET_i2s (&my_full_id));
2967 if (peer->id == t->next_hop)
2969 else if (peer->id == t->prev_hop)
2976 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
2979 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
2989 * @brief Queue and pass message to core when possible.
2991 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
2992 * accounts for it. In case the queue is full, the message is dropped and
2995 * Otherwise, message is treated as internal and allowed to go regardless of
2998 * @param cls Closure (@c type dependant). It will be used by queue_send to
2999 * build the message to be sent if not already prebuilt.
3000 * @param type Type of the message, 0 for a raw message.
3001 * @param size Size of the message.
3002 * @param dst Neighbor to send message to.
3003 * @param t Tunnel this message belongs to.
3006 queue_add (void *cls, uint16_t type, size_t size,
3007 struct MeshPeerInfo *dst, struct MeshTunnel *t)
3009 struct MeshPeerQueue *queue;
3010 struct GNUNET_PeerIdentity id;
3014 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
3016 n = &t->next_fc.queue_n;
3018 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
3020 n = &t->prev_fc.queue_n;
3024 if (*n >= t->queue_max)
3027 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3028 "queue full: %u/%u\n",
3030 GNUNET_STATISTICS_update(stats,
3031 "# messages dropped (buffer full)",
3033 return; /* Drop message */
3037 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
3043 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
3044 if (NULL == dst->core_transmit)
3046 GNUNET_PEER_resolve (dst->id, &id);
3047 dst->core_transmit =
3048 GNUNET_CORE_notify_transmit_ready (core_handle,
3051 GNUNET_TIME_UNIT_FOREVER_REL,
3057 t->pending_messages++;
3061 /******************************************************************************/
3062 /******************** MESH NETWORK HANDLERS **************************/
3063 /******************************************************************************/
3067 * Core handler for path creation
3069 * @param cls closure
3070 * @param message message
3071 * @param peer peer identity this notification is about
3073 * @return GNUNET_OK to keep the connection open,
3074 * GNUNET_SYSERR to close it (signal serious error)
3077 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3078 const struct GNUNET_MessageHeader *message)
3080 unsigned int own_pos;
3083 MESH_TunnelNumber tid;
3084 struct GNUNET_MESH_CreateTunnel *msg;
3085 struct GNUNET_PeerIdentity *pi;
3086 struct MeshPeerPath *path;
3087 struct MeshPeerInfo *dest_peer_info;
3088 struct MeshPeerInfo *orig_peer_info;
3089 struct MeshTunnel *t;
3091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3092 "Received a path create msg [%s]\n",
3093 GNUNET_i2s (&my_full_id));
3094 size = ntohs (message->size);
3095 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
3097 GNUNET_break_op (0);
3101 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
3102 if (size % sizeof (struct GNUNET_PeerIdentity))
3104 GNUNET_break_op (0);
3107 size /= sizeof (struct GNUNET_PeerIdentity);
3110 GNUNET_break_op (0);
3113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3114 msg = (struct GNUNET_MESH_CreateTunnel *) message;
3116 tid = ntohl (msg->tid);
3117 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3119 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
3120 t = tunnel_get (pi, tid);
3121 if (NULL == t) /* might be a local tunnel */
3125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
3126 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3132 t->port = ntohl (msg->port);
3133 opt = ntohl (msg->opt);
3134 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
3136 t->nobuffer = GNUNET_YES;
3139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3141 tunnel_reset_timeout (t);
3143 t->state = MESH_TUNNEL_WAITING;
3145 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3146 if (NULL == dest_peer_info)
3148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3149 " Creating PeerInfo for destination.\n");
3150 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3151 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3152 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3154 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3156 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3157 if (NULL == orig_peer_info)
3159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3160 " Creating PeerInfo for origin.\n");
3161 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3162 orig_peer_info->id = GNUNET_PEER_intern (pi);
3163 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3164 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3167 path = path_new (size);
3169 for (i = 0; i < size; i++)
3171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3172 GNUNET_i2s (&pi[i]));
3173 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3174 if (path->peers[i] == myid)
3177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3178 if (own_pos == 0 && path->peers[own_pos] != myid)
3180 /* create path: self not found in path through self */
3181 GNUNET_break_op (0);
3182 path_destroy (path);
3186 path_add_to_peers (path, GNUNET_NO);
3187 tunnel_use_path (t, path);
3189 peer_info_add_tunnel (dest_peer_info, t);
3191 if (own_pos == size - 1)
3193 struct MeshClient *c;
3194 struct GNUNET_HashCode hc;
3196 /* Find target client */
3197 GMC_hash32 (t->port, &hc);
3198 c = GNUNET_CONTAINER_multihashmap_get (ports, &hc);
3201 /* TODO send reject */
3205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3206 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3208 /* Assign local tid */
3209 while (NULL != tunnel_get_incoming (next_local_tid))
3210 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3211 t->local_tid_dest = next_local_tid++;
3212 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3214 tunnel_add_client (t, c);
3215 send_client_tunnel_create (t);
3220 struct MeshPeerPath *path2;
3222 t->next_hop = path->peers[own_pos + 1];
3223 GNUNET_PEER_change_rc(t->next_hop, 1);
3225 /* It's for somebody else! Retransmit. */
3226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3227 path2 = path_duplicate (path);
3228 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3229 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3230 send_create_path (t);
3238 * Core handler for path ACKs
3240 * @param cls closure
3241 * @param message message
3242 * @param peer peer identity this notification is about
3244 * @return GNUNET_OK to keep the connection open,
3245 * GNUNET_SYSERR to close it (signal serious error)
3248 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3249 const struct GNUNET_MessageHeader *message)
3251 struct GNUNET_MESH_PathACK *msg;
3252 struct MeshPeerInfo *peer_info;
3253 struct MeshPeerPath *p;
3254 struct MeshTunnel *t;
3256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3257 GNUNET_i2s (&my_full_id));
3258 msg = (struct GNUNET_MESH_PathACK *) message;
3259 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3262 /* TODO notify that we don't know the tunnel */
3263 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3265 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3269 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3271 peer_info = peer_get (&msg->peer_id);
3272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3273 GNUNET_i2s (&msg->peer_id));
3274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3277 /* Add path to peers? */
3281 path_add_to_peers (p, GNUNET_YES);
3287 t->state = MESH_TUNNEL_READY;
3288 t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
3289 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3291 /* Message for us? */
3292 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3295 if (NULL == t->owner)
3297 GNUNET_break_op (0);
3300 if (NULL != peer_info->dhtget)
3302 GNUNET_DHT_get_stop (peer_info->dhtget);
3303 peer_info->dhtget = NULL;
3305 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3306 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3311 " not for us, retransmitting...\n");
3312 peer_info = peer_get (&msg->oid);
3313 send_prebuilt_message (message, t->prev_hop, t);
3319 * Core handler for notifications of broken paths
3321 * @param cls closure
3322 * @param message message
3323 * @param peer peer identity this notification is about
3325 * @return GNUNET_OK to keep the connection open,
3326 * GNUNET_SYSERR to close it (signal serious error)
3329 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3330 const struct GNUNET_MessageHeader *message)
3332 struct GNUNET_MESH_PathBroken *msg;
3333 struct MeshTunnel *t;
3335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3336 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3337 msg = (struct GNUNET_MESH_PathBroken *) message;
3338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3339 GNUNET_i2s (&msg->peer1));
3340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3341 GNUNET_i2s (&msg->peer2));
3342 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3345 GNUNET_break_op (0);
3348 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3349 GNUNET_PEER_search (&msg->peer2));
3356 * Core handler for tunnel destruction
3358 * @param cls closure
3359 * @param message message
3360 * @param peer peer identity this notification is about
3362 * @return GNUNET_OK to keep the connection open,
3363 * GNUNET_SYSERR to close it (signal serious error)
3366 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3367 const struct GNUNET_MessageHeader *message)
3369 struct GNUNET_MESH_TunnelDestroy *msg;
3370 struct MeshTunnel *t;
3372 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3374 "Got a TUNNEL DESTROY packet from %s\n",
3376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3377 " for tunnel %s [%u]\n",
3378 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3379 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3382 /* Probably already got the message from another path,
3383 * destroyed the tunnel and retransmitted to children.
3386 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3390 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3393 t->local_tid, t->local_tid_dest);
3395 if (GNUNET_PEER_search (peer) == t->prev_hop)
3397 // TODO check owner's signature
3398 // TODO add owner's signatue to tunnel for retransmission
3399 peer_cancel_queues (t->prev_hop, t);
3400 GNUNET_PEER_change_rc (t->prev_hop, -1);
3403 else if (GNUNET_PEER_search (peer) == t->next_hop)
3405 // TODO check dest's signature
3406 // TODO add dest's signatue to tunnel for retransmission
3407 peer_cancel_queues (t->next_hop, t);
3408 GNUNET_PEER_change_rc (t->next_hop, -1);
3413 GNUNET_break_op (0);
3414 // TODO check both owner AND destination's signature to see which matches
3415 // TODO restransmit in appropriate direction
3418 tunnel_destroy_empty (t);
3420 // TODO: add timeout to destroy the tunnel anyway
3426 * Core handler for mesh network traffic going from the origin to a peer
3428 * @param cls closure
3429 * @param peer peer identity this notification is about
3430 * @param message message
3431 * @return GNUNET_OK to keep the connection open,
3432 * GNUNET_SYSERR to close it (signal serious error)
3435 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3436 const struct GNUNET_MessageHeader *message)
3438 struct GNUNET_MESH_Data *msg;
3439 struct MeshTunnel *t;
3444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3447 size = ntohs (message->size);
3449 sizeof (struct GNUNET_MESH_Data) +
3450 sizeof (struct GNUNET_MessageHeader))
3455 msg = (struct GNUNET_MESH_Data *) message;
3456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3457 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3459 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3462 /* TODO notify back: we don't know this tunnel */
3463 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3464 GNUNET_break_op (0);
3467 pid = ntohl (msg->pid);
3468 if (t->prev_fc.last_pid_recv == pid)
3470 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3471 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3472 " Already seen pid %u, DROPPING!\n", pid);
3477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3478 " pid %u not seen yet, forwarding\n", pid);
3481 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3483 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3484 GNUNET_break_op (0);
3485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3486 "Received PID %u, ACK %u\n",
3487 pid, t->prev_fc.last_ack_sent);
3488 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3491 t->prev_fc.last_pid_recv = pid;
3493 tunnel_reset_timeout (t);
3494 if (t->dest == myid)
3496 /* TODO signature verification */
3497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3498 " it's for us! sending to clients...\n");
3499 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3500 tunnel_send_client_ucast (t, msg);
3501 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3504 if (0 == t->next_hop)
3509 ttl = ntohl (msg->ttl);
3510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3513 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3514 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3515 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3519 " not for us, retransmitting...\n");
3521 send_prebuilt_message (message, t->next_hop, t);
3522 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3528 * Core handler for mesh network traffic toward the owner of a tunnel
3530 * @param cls closure
3531 * @param message message
3532 * @param peer peer identity this notification is about
3534 * @return GNUNET_OK to keep the connection open,
3535 * GNUNET_SYSERR to close it (signal serious error)
3538 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3539 const struct GNUNET_MessageHeader *message)
3541 struct GNUNET_MESH_Data *msg;
3542 struct MeshTunnel *t;
3547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3549 size = ntohs (message->size);
3550 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
3551 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3553 GNUNET_break_op (0);
3556 msg = (struct GNUNET_MESH_Data *) message;
3557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3558 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3559 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3560 pid = ntohl (msg->pid);
3564 /* TODO notify that we dont know this tunnel (whom)? */
3565 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3567 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3568 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3572 if (t->next_fc.last_pid_recv == pid)
3574 /* already seen this packet, drop */
3575 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3577 " Already seen pid %u, DROPPING!\n", pid);
3578 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3582 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3584 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3585 GNUNET_break_op (0);
3586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3587 "Received PID %u, ACK %u\n",
3588 pid, t->next_fc.last_ack_sent);
3589 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3592 t->next_fc.last_pid_recv = pid;
3593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3594 " pid %u not seen yet, forwarding\n", pid);
3596 if (myid == t->id.oid)
3598 /* TODO signature verification */
3599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3600 " it's for us! sending to clients...\n");
3601 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3602 tunnel_send_client_to_orig (t, msg);
3603 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3607 " not for us, retransmitting...\n");
3609 if (0 == t->prev_hop) /* No owner AND no prev hop */
3611 if (GNUNET_YES == t->destroy)
3613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3614 "to orig received on a dying tunnel %s [%X]\n",
3615 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3618 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3619 "unknown to origin at %s\n",
3620 GNUNET_i2s (&my_full_id));
3621 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3624 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3625 "on tunnel %s [%X]\n",
3626 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3629 ttl = ntohl (msg->ttl);
3630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3633 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3634 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3635 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3638 send_prebuilt_message (message, t->prev_hop, t);
3639 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3646 * Core handler for mesh network traffic point-to-point acks.
3648 * @param cls closure
3649 * @param message message
3650 * @param peer peer identity this notification is about
3652 * @return GNUNET_OK to keep the connection open,
3653 * GNUNET_SYSERR to close it (signal serious error)
3656 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3657 const struct GNUNET_MessageHeader *message)
3659 struct GNUNET_MESH_ACK *msg;
3660 struct MeshTunnel *t;
3664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3666 msg = (struct GNUNET_MESH_ACK *) message;
3668 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3672 /* TODO notify that we dont know this tunnel (whom)? */
3673 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3676 ack = ntohl (msg->pid);
3677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3679 /* Is this a forward or backward ACK? */
3680 id = GNUNET_PEER_search (peer);
3681 if (t->next_hop == id)
3684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3685 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task &&
3686 GMC_is_pid_bigger (ack, t->next_fc.last_ack_recv))
3688 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3689 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3690 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3692 t->next_fc.last_ack_recv = ack;
3693 peer_unlock_queue (t->next_hop);
3694 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3696 else if (t->prev_hop == id)
3699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3700 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
3701 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
3703 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3704 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3705 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3707 t->prev_fc.last_ack_recv = ack;
3708 peer_unlock_queue (t->prev_hop);
3709 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3712 GNUNET_break_op (0);
3718 * Core handler for mesh network traffic point-to-point ack polls.
3720 * @param cls closure
3721 * @param message message
3722 * @param peer peer identity this notification is about
3724 * @return GNUNET_OK to keep the connection open,
3725 * GNUNET_SYSERR to close it (signal serious error)
3728 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3729 const struct GNUNET_MessageHeader *message)
3731 struct GNUNET_MESH_Poll *msg;
3732 struct MeshTunnel *t;
3735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3738 msg = (struct GNUNET_MESH_Poll *) message;
3740 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3744 /* TODO notify that we dont know this tunnel (whom)? */
3745 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
3746 GNUNET_break_op (0);
3750 /* Is this a forward or backward ACK? */
3751 id = GNUNET_PEER_search(peer);
3752 if (t->next_hop == id)
3754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
3755 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3757 else if (t->prev_hop == id)
3759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
3760 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3770 * Core handler for mesh keepalives.
3772 * @param cls closure
3773 * @param message message
3774 * @param peer peer identity this notification is about
3775 * @return GNUNET_OK to keep the connection open,
3776 * GNUNET_SYSERR to close it (signal serious error)
3778 * TODO: Check who we got this from, to validate route.
3781 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
3782 const struct GNUNET_MessageHeader *message)
3784 struct GNUNET_MESH_TunnelKeepAlive *msg;
3785 struct MeshTunnel *t;
3787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
3790 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
3791 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3795 /* TODO notify that we dont know that tunnel */
3796 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
3801 tunnel_reset_timeout (t);
3803 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
3804 send_prebuilt_message (message, t->next_hop, t);
3811 * Functions to handle messages from core
3813 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3814 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3815 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3816 sizeof (struct GNUNET_MESH_PathBroken)},
3817 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
3818 sizeof (struct GNUNET_MESH_TunnelDestroy)},
3819 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3820 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
3821 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
3822 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3823 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
3824 sizeof (struct GNUNET_MESH_ACK)},
3825 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
3826 sizeof (struct GNUNET_MESH_Poll)},
3827 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3828 sizeof (struct GNUNET_MESH_PathACK)},
3834 /******************************************************************************/
3835 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3836 /******************************************************************************/
3841 * notify_client_connection_failure: notify a client that the connection to the
3842 * requested remote peer is not possible (for instance, no route found)
3843 * Function called when the socket is ready to queue more data. "buf" will be
3844 * NULL and "size" zero if the socket was closed for writing in the meantime.
3846 * @param cls closure
3847 * @param size number of bytes available in buf
3848 * @param buf where the callee should write the message
3849 * @return number of bytes written to buf
3852 notify_client_connection_failure (void *cls, size_t size, void *buf)
3855 struct MeshPeerInfo *peer_info;
3856 struct GNUNET_MESH_PeerControl *msg;
3857 struct GNUNET_PeerIdentity id;
3859 if (0 == size && NULL == buf)
3861 // TODO retry? cancel?
3865 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3866 peer_info = (struct MeshPeerInfo *) cls;
3867 msg = (struct GNUNET_MESH_PeerControl *) buf;
3868 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3869 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3870 // msg->tunnel_id = htonl(peer_info->t->tid);
3871 GNUNET_PEER_resolve (peer_info->id, &id);
3872 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3880 * Send keepalive packets for a tunnel.
3882 * @param cls Closure (tunnel for which to send the keepalive).
3883 * @param tc Notification context.
3885 * FIXME: add a refresh reset in case of normal unicast traffic is doing the job
3888 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3890 struct MeshTunnel *t = cls;
3891 struct GNUNET_MESH_TunnelKeepAlive *msg;
3892 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
3895 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3896 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
3897 NULL == t->owner || 0 == t->local_tid)
3902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3903 "sending keepalive for tunnel %d\n", t->id.tid);
3905 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
3906 msg->header.size = htons (size);
3907 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3908 msg->oid = my_full_id;
3909 msg->tid = htonl (t->id.tid);
3910 send_prebuilt_message (&msg->header, t->next_hop, t);
3912 t->maintenance_task =
3913 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3918 * Function to process paths received for a new peer addition. The recorded
3919 * paths form the initial tunnel, which can be optimized later.
3920 * Called on each result obtained for the DHT search.
3922 * @param cls closure
3923 * @param exp when will this value expire
3924 * @param key key of the result
3925 * @param get_path path of the get request
3926 * @param get_path_length lenght of get_path
3927 * @param put_path path of the put request
3928 * @param put_path_length length of the put_path
3929 * @param type type of the result
3930 * @param size number of bytes in data
3931 * @param data pointer to the result data
3934 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3935 const struct GNUNET_HashCode * key,
3936 const struct GNUNET_PeerIdentity *get_path,
3937 unsigned int get_path_length,
3938 const struct GNUNET_PeerIdentity *put_path,
3939 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3940 size_t size, const void *data)
3942 struct MeshPeerInfo *peer = cls;
3943 struct MeshPeerPath *p;
3944 struct GNUNET_PeerIdentity pi;
3947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3948 GNUNET_PEER_resolve (peer->id, &pi);
3949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3951 p = path_build_from_dht (get_path, get_path_length,
3952 put_path, put_path_length);
3953 path_add_to_peers (p, GNUNET_NO);
3955 for (i = 0; i < peer->ntunnels; i++)
3957 struct GNUNET_PeerIdentity id;
3959 GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
3960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
3961 GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
3962 peer->tunnels[i]->local_tid,
3963 peer->tunnels[i]->local_tid_dest);
3964 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
3966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
3967 peer_connect (peer, peer->tunnels[i]);
3975 /******************************************************************************/
3976 /********************* MESH LOCAL HANDLES **************************/
3977 /******************************************************************************/
3981 * Handler for client disconnection
3983 * @param cls closure
3984 * @param client identification of the client; NULL
3985 * for the last call when the server is destroyed
3988 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3990 struct MeshClient *c;
3991 struct MeshClient *next;
3993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
3996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
4003 if (c->handle != client)
4005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4006 " ... searching %p (%u)\n",
4011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
4013 GNUNET_SERVER_client_drop (c->handle);
4014 c->shutting_down = GNUNET_YES;
4015 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
4016 &tunnel_destroy_iterator, c);
4017 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
4018 &tunnel_destroy_iterator, c);
4019 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
4020 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
4022 if (NULL != c->ports)
4023 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
4025 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
4026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
4028 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
4031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
4037 * Handler for new clients
4039 * @param cls closure
4040 * @param client identification of the client
4041 * @param message the actual message, which includes messages the client wants
4044 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
4045 const struct GNUNET_MessageHeader *message)
4047 struct GNUNET_MESH_ClientConnect *cc_msg;
4048 struct MeshClient *c;
4053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
4055 /* Check data sanity */
4056 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
4057 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
4058 if (0 != (size % sizeof (uint32_t)))
4061 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4064 size /= sizeof (uint32_t);
4066 /* Create new client structure */
4067 c = GNUNET_malloc (sizeof (struct MeshClient));
4068 c->id = next_client_id++; /* overflow not important: just for debug */
4069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
4070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
4072 GNUNET_SERVER_client_keep (client);
4076 struct GNUNET_HashCode hc;
4078 p = (uint32_t *) &cc_msg[1];
4079 c->ports = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
4080 for (i = 0; i < size; i++)
4083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
4084 GMC_hash32 (u32, &hc);
4086 /* store in client's hashmap */
4087 GNUNET_CONTAINER_multihashmap_put (c->ports, &hc, c,
4088 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4089 /* store in global hashmap */
4090 /* FIXME only allow one client to have the port open,
4091 * have a backup hashmap with waiting clients */
4092 GNUNET_CONTAINER_multihashmap_put (ports, &hc, c,
4093 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4097 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
4098 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4099 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
4100 GNUNET_SERVER_notification_context_add (nc, client);
4101 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
4103 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
4109 * Handler for requests of new tunnels
4111 * @param cls Closure.
4112 * @param client Identification of the client.
4113 * @param message The actual message.
4116 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
4117 const struct GNUNET_MessageHeader *message)
4119 struct GNUNET_MESH_TunnelMessage *t_msg;
4120 struct MeshPeerInfo *peer_info;
4121 struct MeshTunnel *t;
4122 struct MeshClient *c;
4123 MESH_TunnelNumber tid;
4125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
4127 /* Sanity check for client registration */
4128 if (NULL == (c = client_get (client)))
4131 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4136 /* Message size sanity check */
4137 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4144 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
4146 GNUNET_i2s (&t_msg->peer));
4147 /* Sanity check for tunnel numbering */
4148 tid = ntohl (t_msg->tunnel_id);
4149 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
4152 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4155 /* Sanity check for duplicate tunnel IDs */
4156 if (NULL != tunnel_get_by_local_id (c, tid))
4159 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4164 while (NULL != tunnel_get_by_pi (myid, next_tid))
4165 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4166 t = tunnel_new (myid, next_tid, c, tid);
4167 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4171 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4174 t->port = ntohl (t_msg->port);
4175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4176 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4178 peer_info = peer_get (&t_msg->peer);
4179 peer_info_add_tunnel (peer_info, t);
4180 peer_connect (peer_info, t);
4181 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4187 * Handler for requests of deleting tunnels
4189 * @param cls closure
4190 * @param client identification of the client
4191 * @param message the actual message
4194 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4195 const struct GNUNET_MessageHeader *message)
4197 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4198 struct MeshClient *c;
4199 struct MeshTunnel *t;
4200 MESH_TunnelNumber tid;
4202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4203 "Got a DESTROY TUNNEL from client!\n");
4205 /* Sanity check for client registration */
4206 if (NULL == (c = client_get (client)))
4209 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4214 /* Message sanity check */
4215 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4222 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4224 /* Retrieve tunnel */
4225 tid = ntohl (tunnel_msg->tunnel_id);
4226 t = tunnel_get_by_local_id(c, tid);
4229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4231 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4235 /* Cleanup after the tunnel */
4236 client_delete_tunnel (c, t);
4241 else if (c == t->owner)
4243 peer_info_remove_tunnel (peer_get_short (t->dest), t);
4247 /* The tunnel will be destroyed when the last message is transmitted. */
4248 tunnel_destroy_empty (t);
4250 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4256 * Handler for requests of seeting tunnel's buffering policy.
4258 * @param cls Closure (unused).
4259 * @param client Identification of the client.
4260 * @param message The actual message.
4263 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
4264 const struct GNUNET_MessageHeader *message)
4266 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4267 struct MeshClient *c;
4268 struct MeshTunnel *t;
4269 MESH_TunnelNumber tid;
4271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4272 "Got a BUFFER request from client!\n");
4274 /* Sanity check for client registration */
4275 if (NULL == (c = client_get (client)))
4278 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4283 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4285 /* Retrieve tunnel */
4286 tid = ntohl (tunnel_msg->tunnel_id);
4287 t = tunnel_get_by_local_id(c, tid);
4290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4292 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4296 switch (ntohs(message->type))
4298 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
4299 t->nobuffer = GNUNET_NO;
4301 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
4302 t->nobuffer = GNUNET_YES;
4308 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4313 * Handler for client traffic directed to one peer
4315 * @param cls closure
4316 * @param client identification of the client
4317 * @param message the actual message
4320 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4321 const struct GNUNET_MessageHeader *message)
4323 struct MeshClient *c;
4324 struct MeshTunnel *t;
4325 struct GNUNET_MESH_Data *data_msg;
4326 MESH_TunnelNumber tid;
4329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4330 "Got a unicast request from a client!\n");
4332 /* Sanity check for client registration */
4333 if (NULL == (c = client_get (client)))
4336 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4341 data_msg = (struct GNUNET_MESH_Data *) message;
4343 /* Sanity check for message size */
4344 size = ntohs (message->size);
4345 if (sizeof (struct GNUNET_MESH_Data) +
4346 sizeof (struct GNUNET_MessageHeader) > size)
4349 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4353 /* Tunnel exists? */
4354 tid = ntohl (data_msg->tid);
4355 t = tunnel_get_by_local_id (c, tid);
4359 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4363 /* Is it a local tunnel? Then, does client own the tunnel? */
4364 if (t->owner->handle != client)
4367 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4371 /* PID should be as expected: client<->service communication */
4372 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4375 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4376 "Unicast PID, expected %u, got %u\n",
4377 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4378 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4382 /* Ok, everything is correct, send the message
4383 * (pretend we got it from a mesh peer)
4386 /* Work around const limitation */
4387 char buf[ntohs (message->size)] GNUNET_ALIGN;
4388 struct GNUNET_MESH_Data *copy;
4390 copy = (struct GNUNET_MESH_Data *) buf;
4391 memcpy (buf, data_msg, size);
4392 copy->oid = my_full_id;
4393 copy->tid = htonl (t->id.tid);
4394 copy->ttl = htonl (default_ttl);
4395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4396 " calling generic handler...\n");
4397 handle_mesh_unicast (NULL, &my_full_id, ©->header);
4399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4400 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4407 * Handler for client traffic directed to the origin
4409 * @param cls closure
4410 * @param client identification of the client
4411 * @param message the actual message
4414 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4415 const struct GNUNET_MessageHeader *message)
4417 struct GNUNET_MESH_Data *data_msg;
4418 struct MeshFlowControl *fc;
4419 struct MeshClient *c;
4420 struct MeshTunnel *t;
4421 MESH_TunnelNumber tid;
4424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4425 "Got a ToOrigin request from a client!\n");
4426 /* Sanity check for client registration */
4427 if (NULL == (c = client_get (client)))
4430 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4435 data_msg = (struct GNUNET_MESH_Data *) message;
4437 /* Sanity check for message size */
4438 size = ntohs (message->size);
4439 if (sizeof (struct GNUNET_MESH_Data) +
4440 sizeof (struct GNUNET_MessageHeader) > size)
4443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4447 /* Tunnel exists? */
4448 tid = ntohl (data_msg->tid);
4449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4450 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4453 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4456 t = tunnel_get_by_local_id (c, tid);
4459 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4460 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4462 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4466 /* It should be sent by someone who has this as incoming tunnel. */
4470 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4474 /* PID should be as expected */
4476 if (ntohl (data_msg->pid) != fc->last_pid_recv + 1)
4479 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4480 "To Origin PID, expected %u, got %u\n",
4481 fc->last_pid_recv + 1,
4482 ntohl (data_msg->pid));
4483 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4487 /* Ok, everything is correct, send the message
4488 * (pretend we got it from a mesh peer)
4491 char buf[ntohs (message->size)] GNUNET_ALIGN;
4492 struct GNUNET_MESH_Data *copy;
4494 /* Work around 'const' limitation */
4495 memcpy (buf, data_msg, size);
4496 copy = (struct GNUNET_MESH_Data *) buf;
4497 GNUNET_PEER_resolve (t->id.oid, ©->oid);
4498 copy->tid = htonl (t->id.tid);
4499 copy->ttl = htonl (default_ttl);
4501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4502 " calling generic handler...\n");
4503 handle_mesh_to_orig (NULL, &my_full_id, ©->header);
4505 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4512 * Handler for client's ACKs for payload traffic.
4514 * @param cls Closure (unused).
4515 * @param client Identification of the client.
4516 * @param message The actual message.
4519 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4520 const struct GNUNET_MessageHeader *message)
4522 struct GNUNET_MESH_LocalAck *msg;
4523 struct MeshTunnel *t;
4524 struct MeshClient *c;
4525 MESH_TunnelNumber tid;
4528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4529 /* Sanity check for client registration */
4530 if (NULL == (c = client_get (client)))
4533 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4538 msg = (struct GNUNET_MESH_LocalAck *) message;
4540 /* Tunnel exists? */
4541 tid = ntohl (msg->tunnel_id);
4542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4543 t = tunnel_get_by_local_id (c, tid);
4547 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4548 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4553 ack = ntohl (msg->ack);
4554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4556 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4559 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4560 t->prev_fc.last_ack_recv = ack;
4561 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4565 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4566 t->next_fc.last_ack_recv = ack;
4567 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4570 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4578 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4580 * @param cls Closure (client handle).
4581 * @param key Key (hashed tunnel ID, unused).
4582 * @param value Tunnel info.
4584 * @return GNUNET_YES, to keep iterating.
4587 monitor_all_tunnels_iterator (void *cls,
4588 const struct GNUNET_HashCode * key,
4591 struct GNUNET_SERVER_Client *client = cls;
4592 struct MeshTunnel *t = value;
4593 struct GNUNET_MESH_LocalMonitor *msg;
4595 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4596 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4597 msg->tunnel_id = htonl (t->id.tid);
4598 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4599 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4600 GNUNET_PEER_resolve (t->dest, &msg->destination);
4602 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4603 "* sending info about tunnel %s [%u]\n",
4604 GNUNET_i2s (&msg->owner), t->id.tid);
4606 GNUNET_SERVER_notification_context_unicast (nc, client,
4607 &msg->header, GNUNET_NO);
4613 * Handler for client's MONITOR request.
4615 * @param cls Closure (unused).
4616 * @param client Identification of the client.
4617 * @param message The actual message.
4620 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4621 const struct GNUNET_MessageHeader *message)
4623 struct MeshClient *c;
4625 /* Sanity check for client registration */
4626 if (NULL == (c = client_get (client)))
4629 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4633 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4634 "Received get tunnels request from client %u\n",
4636 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4637 monitor_all_tunnels_iterator,
4639 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4640 "Get tunnels request from client %u completed\n",
4642 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4647 * Handler for client's MONITOR_TUNNEL request.
4649 * @param cls Closure (unused).
4650 * @param client Identification of the client.
4651 * @param message The actual message.
4654 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4655 const struct GNUNET_MessageHeader *message)
4657 const struct GNUNET_MESH_LocalMonitor *msg;
4658 struct GNUNET_MESH_LocalMonitor *resp;
4659 struct MeshClient *c;
4660 struct MeshTunnel *t;
4662 /* Sanity check for client registration */
4663 if (NULL == (c = client_get (client)))
4666 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4670 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4671 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4672 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4675 ntohl (msg->tunnel_id));
4676 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4679 /* We don't know the tunnel FIXME */
4680 struct GNUNET_MESH_LocalMonitor warn;
4683 GNUNET_SERVER_notification_context_unicast (nc, client,
4686 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4690 /* Initialize context */
4691 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4693 GNUNET_PEER_resolve (t->dest, &resp->destination);
4694 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4695 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4696 &resp->header, GNUNET_NO);
4699 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4700 "Monitor tunnel request from client %u completed\n",
4702 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4707 * Functions to handle messages from clients
4709 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4710 {&handle_local_new_client, NULL,
4711 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4712 {&handle_local_tunnel_create, NULL,
4713 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4714 sizeof (struct GNUNET_MESH_TunnelMessage)},
4715 {&handle_local_tunnel_destroy, NULL,
4716 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4717 sizeof (struct GNUNET_MESH_TunnelMessage)},
4718 {&handle_local_tunnel_buffer, NULL,
4719 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
4720 sizeof (struct GNUNET_MESH_TunnelMessage)},
4721 {&handle_local_tunnel_buffer, NULL,
4722 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
4723 sizeof (struct GNUNET_MESH_TunnelMessage)},
4724 {&handle_local_unicast, NULL,
4725 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4726 {&handle_local_to_origin, NULL,
4727 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4728 {&handle_local_ack, NULL,
4729 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4730 sizeof (struct GNUNET_MESH_LocalAck)},
4731 {&handle_local_get_tunnels, NULL,
4732 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4733 sizeof (struct GNUNET_MessageHeader)},
4734 {&handle_local_show_tunnel, NULL,
4735 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4736 sizeof (struct GNUNET_MESH_LocalMonitor)},
4742 * Method called whenever a given peer connects.
4744 * @param cls closure
4745 * @param peer peer identity this notification is about
4748 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4750 struct MeshPeerInfo *peer_info;
4751 struct MeshPeerPath *path;
4753 DEBUG_CONN ("Peer connected\n");
4754 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4755 peer_info = peer_get (peer);
4756 if (myid == peer_info->id)
4758 DEBUG_CONN (" (self)\n");
4759 path = path_new (1);
4763 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4764 path = path_new (2);
4765 path->peers[1] = peer_info->id;
4766 GNUNET_PEER_change_rc (peer_info->id, 1);
4767 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4769 path->peers[0] = myid;
4770 GNUNET_PEER_change_rc (myid, 1);
4771 peer_info_add_path (peer_info, path, GNUNET_YES);
4777 * Method called whenever a peer disconnects.
4779 * @param cls closure
4780 * @param peer peer identity this notification is about
4783 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4785 struct MeshPeerInfo *pi;
4786 struct MeshPeerQueue *q;
4787 struct MeshPeerQueue *n;
4789 DEBUG_CONN ("Peer disconnected\n");
4790 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4800 /* TODO try to reroute this traffic instead */
4801 queue_destroy(q, GNUNET_YES);
4804 if (NULL != pi->core_transmit)
4806 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4807 pi->core_transmit = NULL;
4809 peer_remove_path (pi, pi->id, myid);
4812 DEBUG_CONN (" (self)\n");
4814 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
4820 * Install server (service) handlers and start listening to clients.
4825 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4826 GNUNET_SERVER_disconnect_notify (server_handle,
4827 &handle_local_client_disconnect, NULL);
4828 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
4830 clients_head = NULL;
4831 clients_tail = NULL;
4833 GNUNET_SERVER_resume (server_handle);
4838 * To be called on core init/fail.
4840 * @param cls Closure (config)
4841 * @param server handle to the server for this service
4842 * @param identity the public identity of this peer
4845 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4846 const struct GNUNET_PeerIdentity *identity)
4848 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4852 GNUNET_break (core_handle == server);
4853 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4856 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4857 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4859 GNUNET_i2s (identity));
4860 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4862 GNUNET_i2s (&my_full_id));
4863 GNUNET_CORE_disconnect (core_handle);
4864 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4865 NULL, /* Closure passed to MESH functions */
4866 &core_init, /* Call core_init once connected */
4867 &core_connect, /* Handle connects */
4868 &core_disconnect, /* remove peers on disconnects */
4869 NULL, /* Don't notify about all incoming messages */
4870 GNUNET_NO, /* For header only in notification */
4871 NULL, /* Don't notify about all outbound messages */
4872 GNUNET_NO, /* For header-only out notification */
4873 core_handlers); /* Register these handlers */
4882 /******************************************************************************/
4883 /************************ MAIN FUNCTIONS ****************************/
4884 /******************************************************************************/
4887 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4889 * @param cls closure
4890 * @param key current key code
4891 * @param value value in the hash map
4892 * @return GNUNET_YES if we should continue to iterate,
4896 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
4898 struct MeshTunnel *t = value;
4905 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4907 * @param cls closure
4908 * @param key current key code
4909 * @param value value in the hash map
4910 * @return GNUNET_YES if we should continue to iterate,
4914 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
4916 struct MeshPeerInfo *p = value;
4917 struct MeshPeerQueue *q;
4918 struct MeshPeerQueue *n;
4926 queue_destroy(q, GNUNET_YES);
4930 peer_info_destroy (p);
4936 * Task run during shutdown.
4942 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4946 if (core_handle != NULL)
4948 GNUNET_CORE_disconnect (core_handle);
4953 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
4956 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4957 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4958 if (dht_handle != NULL)
4960 GNUNET_DHT_disconnect (dht_handle);
4965 GNUNET_SERVER_notification_context_destroy (nc);
4968 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4970 GNUNET_SCHEDULER_cancel (announce_id_task);
4971 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4978 * Callback for hostkey read/generation.
4980 * @param cls Closure (Configuration handle).
4981 * @param pk The ECC private key.
4982 * @param emsg Error message, if any.
4985 key_generation_cb (void *cls,
4986 struct GNUNET_CRYPTO_EccPrivateKey *pk,
4989 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4994 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4995 _("Could not access hostkey: %s. Exiting.\n"),
4997 GNUNET_SCHEDULER_shutdown ();
5000 my_private_key = pk;
5001 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
5002 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
5003 &my_full_id.hashPubKey);
5004 myid = GNUNET_PEER_intern (&my_full_id);
5005 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5006 "Mesh for peer [%s] starting\n",
5007 GNUNET_i2s(&my_full_id));
5009 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
5010 NULL, /* Closure passed to MESH functions */
5011 &core_init, /* Call core_init once connected */
5012 &core_connect, /* Handle connects */
5013 &core_disconnect, /* remove peers on disconnects */
5014 NULL, /* Don't notify about all incoming messages */
5015 GNUNET_NO, /* For header only in notification */
5016 NULL, /* Don't notify about all outbound messages */
5017 GNUNET_NO, /* For header-only out notification */
5018 core_handlers); /* Register these handlers */
5019 if (core_handle == NULL)
5022 GNUNET_SCHEDULER_shutdown ();
5027 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5029 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
5031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
5036 * Process mesh requests.
5038 * @param cls closure
5039 * @param server the initialized server
5040 * @param c configuration to use
5043 run (void *cls, struct GNUNET_SERVER_Handle *server,
5044 const struct GNUNET_CONFIGURATION_Handle *c)
5048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
5049 server_handle = server;
5050 GNUNET_SERVER_suspend (server_handle);
5053 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
5056 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5058 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5059 "mesh", "peer/privatekey");
5060 GNUNET_SCHEDULER_shutdown ();
5065 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
5066 &refresh_path_time))
5068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5070 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5071 "mesh", "refresh path time");
5072 GNUNET_SCHEDULER_shutdown ();
5077 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
5080 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5082 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5083 "mesh", "id announce time");
5084 GNUNET_SCHEDULER_shutdown ();
5089 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
5092 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5094 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5095 "mesh", "connect timeout");
5096 GNUNET_SCHEDULER_shutdown ();
5101 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
5104 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5106 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5107 "mesh", "max msgs queue");
5108 GNUNET_SCHEDULER_shutdown ();
5113 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
5116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5118 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
5119 "mesh", "max tunnels");
5120 GNUNET_SCHEDULER_shutdown ();
5125 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
5128 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5130 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5131 "mesh", "default ttl", 64);
5136 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
5139 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5140 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5141 "mesh", "max peers", 1000);
5146 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
5147 &dht_replication_level))
5149 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5151 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5152 "mesh", "dht replication level", 3);
5153 dht_replication_level = 3;
5156 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5157 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5158 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5159 ports = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5161 dht_handle = GNUNET_DHT_connect (c, 64);
5162 if (NULL == dht_handle)
5166 stats = GNUNET_STATISTICS_create ("mesh", c);
5168 /* Scheduled the task to clean up when shutdown is called */
5169 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5171 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
5174 GNUNET_free (keyfile);
5179 * The main function for the mesh service.
5181 * @param argc number of arguments from the command line
5182 * @param argv command line arguments
5183 * @return 0 ok, 1 on error
5186 main (int argc, char *const *argv)
5191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5192 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5194 ret = (GNUNET_OK == r) ? 0 : 1;
5195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5199 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5200 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5201 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);