2 This file is part of GNUnet.
3 (C) 2001-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh-new.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
27 * - relay corking down to core
28 * - set ttl relative to path length
31 * - keep queues until receiving ACK
37 #include "mesh2_protocol.h"
38 #include "mesh_path.h"
39 #include "block_mesh.h"
40 #include "gnunet_dht_service.h"
41 #include "gnunet_statistics_service.h"
43 #define MESH_BLOOM_SIZE 128
45 #define MESH_DEBUG_DHT GNUNET_NO
46 #define MESH_DEBUG_CONNECTION GNUNET_NO
47 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
49 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
50 GNUNET_TIME_UNIT_MINUTES,\
53 #if MESH_DEBUG_CONNECTION
54 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
56 #define DEBUG_CONN(...)
60 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
62 #define DEBUG_DHT(...)
69 struct timespec __mesh_start;
70 struct timespec __mesh_end;
71 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
72 #define INTERVAL_END \
74 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
75 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
76 if (__diff < 0) __diff += 1000000000;\
80 #define INTERVAL_SHOW \
82 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
84 #define INTERVAL_START
89 /******************************************************************************/
90 /************************ DATA STRUCTURES ****************************/
91 /******************************************************************************/
93 /** FWD declaration */
99 * Struct containing info about a queued transmission to this peer
106 struct MeshPeerQueue *next;
111 struct MeshPeerQueue *prev;
114 * Peer this transmission is directed to.
116 struct MeshPeerInfo *peer;
119 * Tunnel this message belongs to.
121 struct MeshTunnel *tunnel;
124 * Pointer to info stucture used as cls.
134 * Size of the message
141 * Struct containing all information regarding a given peer
151 * Last time we heard from this peer
153 struct GNUNET_TIME_Absolute last_contact;
156 * Number of attempts to reconnect so far
158 int n_reconnect_attempts;
161 * Paths to reach the peer, ordered by ascending hop count
163 struct MeshPeerPath *path_head;
166 * Paths to reach the peer, ordered by ascending hop count
168 struct MeshPeerPath *path_tail;
171 * Handle to stop the DHT search for a path to this peer
173 struct GNUNET_DHT_GetHandle *dhtget;
176 * Array of tunnels this peer is the target of.
177 * Most probably a small amount, therefore not a hashmap.
178 * When the path to the peer changes, notify these tunnels to let them
179 * re-adjust their path trees.
181 struct MeshTunnel **tunnels;
184 * Number of tunnels this peers participates in
186 unsigned int ntunnels;
189 * Transmission queue to core DLL head
191 struct MeshPeerQueue *queue_head;
194 * Transmission queue to core DLL tail
196 struct MeshPeerQueue *queue_tail;
199 * How many messages are in the queue to this peer.
201 unsigned int queue_n;
204 * Handle for queued transmissions
206 struct GNUNET_CORE_TransmitHandle *core_transmit;
211 * Struct to encapsulate all the Flow Control information to a peer in the
212 * context of a tunnel: Same peer in different tunnels will have independent
213 * flow control structures, allowing to choke/free tunnels according to its
216 struct MeshFlowControl
219 * ID of the last packet sent towards the peer.
221 uint32_t last_pid_sent;
224 * ID of the last packet received from the peer.
226 uint32_t last_pid_recv;
229 * Last ACK sent to the peer (peer can't send more than this PID).
231 uint32_t last_ack_sent;
234 * Last ACK sent towards the origin (for traffic towards leaf node).
236 uint32_t last_ack_recv;
239 * How many messages are in the queue towards this peer.
244 * Task to poll the peer in case of a lost ACK causes stall.
246 GNUNET_SCHEDULER_TaskIdentifier poll_task;
249 * How frequently to poll for ACKs.
251 struct GNUNET_TIME_Relative poll_time;
254 * On which tunnel to poll.
255 * Using an explicit poll_ctx would not help memory wise,
256 * since the allocated context would have to be stored in the
257 * fc struct in order to free it upon cancelling poll_task.
259 struct MeshTunnel *t;
264 * Globally unique tunnel identification (owner + number)
265 * DO NOT USE OVER THE NETWORK
270 * Node that owns the tunnel
275 * Tunnel number to differentiate all the tunnels owned by the node oid
276 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
278 MESH_TunnelNumber tid;
283 * Struct containing all information regarding a tunnel
284 * For an intermediate node the improtant info used will be:
285 * - id Tunnel unique identification
286 * - paths[0] To know where to send it next
287 * - metainfo: ready, speeds, accounting
294 struct MESH_TunnelID id;
297 * Port of the tunnel.
302 * State of the tunnel.
304 enum MeshTunnelState state;
307 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
309 MESH_TunnelNumber local_tid;
312 * Local tunnel number for local destination clients (incoming number)
313 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
316 MESH_TunnelNumber local_tid_dest;
319 * Is the tunnel bufferless (minimum latency)?
324 * Force sending ACK? Flag to allow duplicate ACK on POLL.
329 * How many messages do we accept in the forward queue.
331 unsigned int queue_max;
334 * Last time the tunnel was used
336 struct GNUNET_TIME_Absolute timestamp;
339 * Destination of the tunnel.
344 * Next hop in the tunnel. If 0, @c client must be set.
346 GNUNET_PEER_Id next_hop;
349 * Previous hop in the tunnel. If 0, @c owner must be set.
351 GNUNET_PEER_Id prev_hop;
354 * Flow control information about @c next_hop or @c client.
356 struct MeshFlowControl next_fc;
359 * Flow control information about @c prev_hop or @c owner.
361 struct MeshFlowControl prev_fc;
364 * Client owner of the tunnel, if any
366 struct MeshClient *owner;
369 * Client destination of the tunnel, if any.
371 struct MeshClient *client;
374 * Task to keep the used paths alive at the owner,
375 * time tunnel out on all the other peers.
377 GNUNET_SCHEDULER_TaskIdentifier maintenance_task;
380 * Path being used for the tunnel.
382 struct MeshPeerPath *path;
385 * Flag to signal the destruction of the tunnel.
386 * If this is set GNUNET_YES the tunnel will be destroyed
387 * when the queue is empty.
392 * Total messages pending for this tunnels, payload or not.
394 unsigned int pending_messages;
399 * Struct containing information about a client of the service
401 * TODO: add a list of 'waiting' ports
408 struct MeshClient *next;
413 struct MeshClient *prev;
416 * Tunnels that belong to this client, indexed by local id
418 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
421 * Tunnels this client has accepted, indexed by incoming local id
423 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
426 * Handle to communicate with the client
428 struct GNUNET_SERVER_Client *handle;
431 * Ports that this client has declared interest in.
432 * Indexed by a GMC_hash32 (type), contains *Client.
434 struct GNUNET_CONTAINER_MultiHashMap *ports;
437 * Whether the client is active or shutting down (don't send confirmations
438 * to a client that is shutting down.
443 * ID of the client, mainly for debug messages
450 /******************************************************************************/
451 /************************ DEBUG FUNCTIONS ****************************/
452 /******************************************************************************/
456 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
457 * @param cls string to print
458 * @param success GNUNET_OK if the PUT was transmitted,
459 * GNUNET_NO on timeout,
460 * GNUNET_SYSERR on disconnect from service
461 * after the PUT message was transmitted
462 * (so we don't know if it was received or not)
467 mesh_debug (void *cls, int success)
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
476 unsigned int debug_fwd_ack;
477 unsigned int debug_bck_ack;
481 /******************************************************************************/
482 /*********************** GLOBAL VARIABLES ****************************/
483 /******************************************************************************/
485 /************************** Configuration parameters **************************/
488 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
490 static struct GNUNET_TIME_Relative refresh_path_time;
493 * How often to PUT own ID in the DHT.
495 static struct GNUNET_TIME_Relative id_announce_time;
498 * Maximum time allowed to connect to a peer found by string.
500 static struct GNUNET_TIME_Relative connect_timeout;
503 * Default TTL for payload packets.
505 static unsigned long long default_ttl;
508 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
510 static unsigned long long dht_replication_level;
513 * How many tunnels are we willing to maintain.
514 * Local tunnels are always allowed, even if there are more tunnels than max.
516 static unsigned long long max_tunnels;
519 * How many messages *in total* are we willing to queue, divided by number of
520 * tunnels to get tunnel queue size.
522 static unsigned long long max_msgs_queue;
525 * How many peers do we want to remember?
527 static unsigned long long max_peers;
530 /*************************** Static global variables **************************/
533 * Hostkey generation context
535 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
538 * DLL with all the clients, head.
540 static struct MeshClient *clients_head;
543 * DLL with all the clients, tail.
545 static struct MeshClient *clients_tail;
548 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
550 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
553 * Number of tunnels known.
555 static unsigned long long n_tunnels;
558 * Tunnels incoming, indexed by MESH_TunnelNumber
559 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
561 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
564 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
566 static struct GNUNET_CONTAINER_MultiHashMap *peers;
569 * Handle to communicate with transport
571 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
574 * Handle to communicate with core.
576 static struct GNUNET_CORE_Handle *core_handle;
581 static struct GNUNET_DHT_Handle *dht_handle;
586 static struct GNUNET_SERVER_Handle *server_handle;
589 * Handle to the statistics service.
591 static struct GNUNET_STATISTICS_Handle *stats;
594 * Notification context, to send messages to local clients.
596 static struct GNUNET_SERVER_NotificationContext *nc;
599 * Local peer own ID (memory efficient handle).
601 static GNUNET_PEER_Id myid;
604 * Local peer own ID (full value).
606 static struct GNUNET_PeerIdentity my_full_id;
611 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
616 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
619 * Tunnel ID for the next created tunnel (global tunnel number).
621 static MESH_TunnelNumber next_tid;
624 * Tunnel ID for the next incoming tunnel (local tunnel number).
626 static MESH_TunnelNumber next_local_tid;
629 * All ports clients of this peer have opened.
631 static struct GNUNET_CONTAINER_MultiHashMap *ports;
634 * Task to periodically announce itself in the network.
636 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
639 * Next ID to assign to a client.
641 unsigned int next_client_id;
644 /******************************************************************************/
645 /*********************** DECLARATIONS **************************/
646 /******************************************************************************/
649 * Function to process paths received for a new peer addition. The recorded
650 * paths form the initial tunnel, which can be optimized later.
651 * Called on each result obtained for the DHT search.
654 * @param exp when will this value expire
655 * @param key key of the result
656 * @param type type of the result
657 * @param size number of bytes in data
658 * @param data pointer to the result data
661 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
662 const struct GNUNET_HashCode * key,
663 const struct GNUNET_PeerIdentity *get_path,
664 unsigned int get_path_length,
665 const struct GNUNET_PeerIdentity *put_path,
666 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
667 size_t size, const void *data);
671 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
672 * and insert it in the appropiate structures if the peer is not known yet.
674 * @param peer Full identity of the peer.
676 * @return Existing or newly created peer info.
678 static struct MeshPeerInfo *
679 peer_get (const struct GNUNET_PeerIdentity *peer);
683 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
684 * and insert it in the appropiate structures if the peer is not known yet.
686 * @param peer Short identity of the peer.
688 * @return Existing or newly created peer info.
690 static struct MeshPeerInfo *
691 peer_get_short (const GNUNET_PEER_Id peer);
695 * Build a PeerPath from the paths returned from the DHT, reversing the paths
696 * to obtain a local peer -> destination path and interning the peer ids.
698 * @return Newly allocated and created path
700 static struct MeshPeerPath *
701 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
702 unsigned int get_path_length,
703 const struct GNUNET_PeerIdentity *put_path,
704 unsigned int put_path_length);
708 * Adds a path to the peer_infos of all the peers in the path
710 * @param p Path to process.
711 * @param confirmed Whether we know if the path works or not.
714 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
719 * Search for a tunnel by global ID using full PeerIdentities.
721 * @param oid owner of the tunnel.
722 * @param tid global tunnel number.
724 * @return tunnel handler, NULL if doesn't exist.
726 static struct MeshTunnel *
727 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
731 * Notify a tunnel that a connection has broken that affects at least
734 * @param t Tunnel affected.
735 * @param p1 Peer that got disconnected from p2.
736 * @param p2 Peer that got disconnected from p1.
738 * @return Short ID of the peer disconnected (either p1 or p2).
739 * 0 if the tunnel remained unaffected.
741 static GNUNET_PEER_Id
742 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
747 * @brief Use the given path for the tunnel.
748 * Update the next and prev hops (and RCs).
749 * (Re)start the path refresh in case the tunnel is locally owned.
751 * @param t Tunnel to update.
752 * @param p Path to use.
755 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
758 * @brief Queue and pass message to core when possible.
760 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
761 * and accounts for it. In case the queue is full, the message is dropped and
764 * Otherwise, message is treated as internal and allowed to go regardless of
767 * @param cls Closure (@c type dependant). It will be used by queue_send to
768 * build the message to be sent if not already prebuilt.
769 * @param type Type of the message, 0 for a raw message.
770 * @param size Size of the message.
771 * @param dst Neighbor to send message to.
772 * @param t Tunnel this message belongs to.
775 queue_add (void *cls, uint16_t type, size_t size,
776 struct MeshPeerInfo *dst, struct MeshTunnel *t);
780 * Free a transmission that was already queued with all resources
781 * associated to the request.
783 * @param queue Queue handler to cancel.
784 * @param clear_cls Is it necessary to free associated cls?
787 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
791 * @brief Get the next transmittable message from the queue.
793 * This will be the head, except in the case of being a data packet
794 * not allowed by the destination peer.
796 * @param peer Destination peer.
798 * @return The next viable MeshPeerQueue element to send to that peer.
799 * NULL when there are no transmittable messages.
801 struct MeshPeerQueue *
802 queue_get_next (const struct MeshPeerInfo *peer);
806 * Core callback to write a queued packet to core buffer
808 * @param cls Closure (peer info).
809 * @param size Number of bytes available in buf.
810 * @param buf Where the to write the message.
812 * @return number of bytes written to buf
815 queue_send (void *cls, size_t size, void *buf);
818 /******************************************************************************/
819 /************************ PERIODIC FUNCTIONS ****************************/
820 /******************************************************************************/
823 * Periodically announce self id in the DHT
826 * @param tc task context
829 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
833 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
835 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
839 * - Set data expiration in function of X
842 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
844 block.id = my_full_id;
845 block.type = htonl (0);
846 GNUNET_DHT_put (dht_handle, /* DHT handle */
847 &my_full_id.hashPubKey, /* Key to use */
848 dht_replication_level, /* Replication level */
849 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
850 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
851 sizeof (block), /* Size of the data */
852 (const char *) &block, /* Data itself */
853 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
854 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
855 NULL, /* Continuation */
856 NULL); /* Continuation closure */
858 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
862 /******************************************************************************/
863 /****************** GENERAL HELPER FUNCTIONS ************************/
864 /******************************************************************************/
868 * Check if client has registered with the service and has not disconnected
870 * @param client the client to check
872 * @return non-NULL if client exists in the global DLL
874 static struct MeshClient *
875 client_get (struct GNUNET_SERVER_Client *client)
877 struct MeshClient *c;
882 if (c->handle == client)
891 * Deletes a tunnel from a client (either owner or destination). To be used on
894 * @param c Client whose tunnel to delete.
895 * @param t Tunnel which should be deleted.
898 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
900 struct GNUNET_HashCode hash;
904 GMC_hash32 (t->local_tid, &hash);
905 GNUNET_assert (GNUNET_YES ==
906 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
910 else if (c == t->client)
912 GMC_hash32 (t->local_tid_dest, &hash);
913 GNUNET_assert (GNUNET_YES ==
914 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
925 * Notify the appropiate client that a new incoming tunnel was created.
927 * @param t Tunnel that was created.
930 send_client_tunnel_create (struct MeshTunnel *t)
932 struct GNUNET_MESH_TunnelMessage msg;
934 if (NULL == t->client)
936 msg.header.size = htons (sizeof (msg));
937 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
938 msg.tunnel_id = htonl (t->local_tid_dest);
939 msg.port = htonl (t->port);
940 GNUNET_PEER_resolve (t->id.oid, &msg.peer);
941 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
942 &msg.header, GNUNET_NO);
947 * Notify dest client that the incoming tunnel is no longer valid.
949 * @param t Tunnel that was destroyed.
952 send_client_tunnel_destroy (struct MeshTunnel *t)
954 struct GNUNET_MESH_TunnelMessage msg;
956 if (NULL == t->client)
958 msg.header.size = htons (sizeof (msg));
959 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
960 msg.tunnel_id = htonl (t->local_tid_dest);
961 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
962 &msg.header, GNUNET_NO);
967 * Iterator over all the peers to remove the oldest not-used entry.
969 * @param cls Closure (unsued).
970 * @param key ID of the peer.
971 * @param value Peer_Info of the peer.
976 peer_info_timeout (void *cls,
977 const struct GNUNET_HashCode *key,
984 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
985 * and insert it in the appropiate structures if the peer is not known yet.
987 * @param peer Full identity of the peer.
989 * @return Existing or newly created peer info.
991 static struct MeshPeerInfo *
992 peer_get (const struct GNUNET_PeerIdentity *peer)
994 struct MeshPeerInfo *peer_info;
996 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
997 if (NULL == peer_info)
1000 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1001 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1003 GNUNET_CONTAINER_multihashmap_iterate (peers,
1007 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1008 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1009 peer_info->id = GNUNET_PEER_intern (peer);
1011 peer_info->last_contact = GNUNET_TIME_absolute_get();
1018 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1019 * and insert it in the appropiate structures if the peer is not known yet.
1021 * @param peer Short identity of the peer.
1023 * @return Existing or newly created peer info.
1025 static struct MeshPeerInfo *
1026 peer_get_short (const GNUNET_PEER_Id peer)
1028 struct GNUNET_PeerIdentity id;
1030 GNUNET_PEER_resolve (peer, &id);
1031 return peer_get (&id);
1036 * Choose the best path towards a peer considering the tunnel properties.
1038 * @param peer The destination peer.
1039 * @param t The tunnel the path is for.
1041 * @return Best current known path towards the peer, if any.
1043 static struct MeshPeerPath *
1044 peer_get_best_path (const struct MeshPeerInfo *peer, const struct MeshTunnel *t)
1046 struct MeshPeerPath *best_p;
1047 struct MeshPeerPath *p;
1048 unsigned int best_cost;
1051 best_p = p = peer->path_head;
1052 best_cost = cost = p->length;
1055 if ((cost = p->length) < best_cost)
1067 * Remove the tunnel from the list of tunnels to which a peer is target.
1069 * @param peer PeerInfo of the peer.
1070 * @param t Tunnel to remove.
1073 peer_remove_tunnel (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1077 for (i = 0; i < peer->ntunnels; i++)
1080 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1083 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1085 GNUNET_realloc (peer->tunnels,
1086 peer->ntunnels * sizeof(struct MeshTunnel *));
1094 * Core callback to write a pre-constructed data packet to core buffer
1096 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1097 * @param size Number of bytes available in buf.
1098 * @param buf Where the to write the message.
1100 * @return number of bytes written to buf
1103 send_core_data_raw (void *cls, size_t size, void *buf)
1105 struct GNUNET_MessageHeader *msg = cls;
1108 GNUNET_assert (NULL != msg);
1109 total_size = ntohs (msg->size);
1111 if (total_size > size)
1116 memcpy (buf, msg, total_size);
1123 * Sends an already built message to a peer, properly registrating
1124 * all used resources.
1126 * @param message Message to send. Function makes a copy of it.
1127 * @param peer Short ID of the neighbor whom to send the message.
1128 * @param t Tunnel on which this message is transmitted.
1131 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1132 GNUNET_PEER_Id peer,
1133 struct MeshTunnel *t)
1135 struct GNUNET_PeerIdentity id;
1136 struct MeshPeerInfo *neighbor;
1137 struct MeshPeerPath *p;
1142 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1147 size = ntohs (message->size);
1148 data = GNUNET_malloc (size);
1149 memcpy (data, message, size);
1150 type = ntohs(message->type);
1153 struct GNUNET_MESH_Unicast *u;
1155 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1156 u = (struct GNUNET_MESH_Unicast *) data;
1157 u->ttl = htonl (ntohl (u->ttl) - 1);
1160 GNUNET_PEER_resolve (peer, &id);
1161 neighbor = peer_get (&id);
1162 for (p = neighbor->path_head; NULL != p; p = p->next)
1172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1173 " %s IS NOT DIRECTLY CONNECTED\n",
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1178 for (p = neighbor->path_head; NULL != p; p = p->next)
1180 struct GNUNET_PeerIdentity debug_id;
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 " path with %u hops through:\n",
1186 for (i = 0; i < p->length; i++)
1188 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1191 i, GNUNET_i2s(&debug_id));
1195 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1197 " no direct connection to %s\n",
1202 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1213 * Sends a CREATE PATH message for a path to a peer, properly registrating
1214 * all used resources.
1216 * @param t Tunnel for which the path is created.
1219 send_create_path (struct MeshTunnel *t)
1221 struct MeshPeerInfo *neighbor;
1223 neighbor = peer_get_short (t->next_hop);
1225 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1226 sizeof (struct GNUNET_MESH_CreateTunnel) +
1227 (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
1230 t->state = MESH_TUNNEL_WAITING;
1235 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1237 * @param t Tunnel which to confirm.
1240 send_path_ack (struct MeshTunnel *t)
1242 struct MeshPeerInfo *peer;
1244 peer = peer_get_short (t->prev_hop);
1247 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1248 sizeof (struct GNUNET_MESH_PathACK),
1255 * Try to establish a new connection to this peer in the given tunnel.
1256 * If the peer doesn't have any path to it yet, try to get one.
1257 * If the peer already has some path, send a CREATE PATH towards it.
1259 * @param peer PeerInfo of the peer.
1260 * @param t Tunnel for which to create the path, if possible.
1263 peer_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1265 struct MeshPeerPath *p;
1267 if (NULL != peer->path_head)
1269 p = peer_get_best_path (peer, t);
1270 tunnel_use_path (t, p);
1271 send_create_path (t);
1273 else if (NULL == peer->dhtget)
1275 struct GNUNET_PeerIdentity id;
1277 GNUNET_PEER_resolve (peer->id, &id);
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1280 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1281 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
1282 &id.hashPubKey, /* key to search */
1283 dht_replication_level, /* replication level */
1284 GNUNET_DHT_RO_RECORD_ROUTE |
1285 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1287 0, /* xquery bits */
1288 &dht_get_id_handler, peer);
1289 t->state = MESH_TUNNEL_SEARCHING;
1291 /* Otherwise, there is no path but the DHT get is already started. */
1296 * Destroy the peer_info and free any allocated resources linked to it
1298 * @param pi The peer_info to destroy.
1300 * @return GNUNET_OK on success
1303 peer_info_destroy (struct MeshPeerInfo *pi)
1305 struct GNUNET_PeerIdentity id;
1306 struct MeshPeerPath *p;
1307 struct MeshPeerPath *nextp;
1309 GNUNET_PEER_resolve (pi->id, &id);
1310 GNUNET_PEER_change_rc (pi->id, -1);
1313 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1316 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1317 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1319 if (NULL != pi->dhtget)
1321 GNUNET_DHT_get_stop (pi->dhtget);
1327 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
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 (path->length <= 2 && 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);
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);
1506 * Remove a tunnel from the list of tunnels a peer participates in.
1508 * @param p Peer to clean.
1509 * @param t Tunnel to remove.
1512 peer_info_remove_tunnel (struct MeshPeerInfo *p, struct MeshTunnel *t)
1516 for (i = 0; i < p->ntunnels; i++)
1518 if (p->tunnels[i] == t)
1520 p->tunnels[i] = p->tunnels[p->ntunnels - 1];
1521 GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
1529 * Function called if the connection to the peer has been stalled for a while,
1530 * possibly due to a missed ACK. Poll the peer about its ACK status.
1532 * @param cls Closure (poll ctx).
1533 * @param tc TaskContext.
1536 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1538 struct MeshFlowControl *fc = cls;
1539 struct GNUNET_MESH_Poll msg;
1540 struct MeshTunnel *t = fc->t;
1541 GNUNET_PEER_Id peer;
1543 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1544 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1549 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
1550 msg.header.size = htons (sizeof (msg));
1551 msg.tid = htonl (t->id.tid);
1552 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1554 if (fc == &t->prev_fc)
1558 else if (fc == &t->next_fc)
1567 send_prebuilt_message (&msg.header, peer, t);
1568 fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
1569 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
1575 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1576 * to obtain a local peer -> destination path and interning the peer ids.
1578 * @return Newly allocated and created path
1580 static struct MeshPeerPath *
1581 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1582 unsigned int get_path_length,
1583 const struct GNUNET_PeerIdentity *put_path,
1584 unsigned int put_path_length)
1586 struct MeshPeerPath *p;
1592 GNUNET_PEER_change_rc (myid, 1);
1593 i = get_path_length;
1594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1595 for (i--; i >= 0; i--)
1597 id = GNUNET_PEER_intern (&get_path[i]);
1598 if (p->length > 0 && id == p->peers[p->length - 1])
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1601 GNUNET_PEER_change_rc (id, -1);
1605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1606 GNUNET_i2s (&get_path[i]));
1608 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1609 p->peers[p->length - 1] = id;
1612 i = put_path_length;
1613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1614 for (i--; i >= 0; i--)
1616 id = GNUNET_PEER_intern (&put_path[i]);
1619 /* PUT path went through us, so discard the path up until now and start
1620 * from here to get a much shorter (and loop-free) path.
1625 if (p->length > 0 && id == p->peers[p->length - 1])
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1628 GNUNET_PEER_change_rc (id, -1);
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1633 GNUNET_i2s (&put_path[i]));
1635 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1636 p->peers[p->length - 1] = id;
1640 if (get_path_length > 0)
1641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1642 GNUNET_i2s (&get_path[0]));
1643 if (put_path_length > 0)
1644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1645 GNUNET_i2s (&put_path[0]));
1646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1648 for (i = 0; i < p->length; i++)
1650 struct GNUNET_PeerIdentity peer_id;
1652 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1654 GNUNET_i2s (&peer_id));
1662 * Adds a path to the peer_infos of all the peers in the path
1664 * @param p Path to process.
1665 * @param confirmed Whether we know if the path works or not.
1668 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1672 /* TODO: invert and add */
1673 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1674 for (i++; i < p->length; i++)
1676 struct MeshPeerInfo *aux;
1677 struct MeshPeerPath *copy;
1679 aux = peer_get_short (p->peers[i]);
1680 copy = path_duplicate (p);
1681 copy->length = i + 1;
1682 peer_info_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
1688 * Send keepalive packets for a peer
1690 * @param cls Closure (tunnel for which to send the keepalive).
1691 * @param tc Notification context.
1694 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1698 * Search for a tunnel among the incoming tunnels
1700 * @param tid the local id of the tunnel
1702 * @return tunnel handler, NULL if doesn't exist
1704 static struct MeshTunnel *
1705 tunnel_get_incoming (MESH_TunnelNumber tid)
1707 struct GNUNET_HashCode hash;
1709 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1710 GMC_hash32 (tid, &hash);
1711 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1716 * Search for a tunnel among the tunnels for a client
1718 * @param c the client whose tunnels to search in
1719 * @param tid the local id of the tunnel
1721 * @return tunnel handler, NULL if doesn't exist
1723 static struct MeshTunnel *
1724 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1726 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1728 return tunnel_get_incoming (tid);
1732 struct GNUNET_HashCode hash;
1734 GMC_hash32 (tid, &hash);
1735 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1741 * Search for a tunnel by global ID using PEER_ID
1743 * @param pi owner of the tunnel
1744 * @param tid global tunnel number
1746 * @return tunnel handler, NULL if doesn't exist
1748 static struct MeshTunnel *
1749 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1751 struct MESH_TunnelID id;
1752 struct GNUNET_HashCode hash;
1757 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1758 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1763 * Search for a tunnel by global ID using full PeerIdentities
1765 * @param oid owner of the tunnel
1766 * @param tid global tunnel number
1768 * @return tunnel handler, NULL if doesn't exist
1770 static struct MeshTunnel *
1771 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1773 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1778 * Add a client to a tunnel, initializing all needed data structures.
1780 * @param t Tunnel to which add the client.
1781 * @param c Client which to add to the tunnel.
1784 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
1786 struct GNUNET_HashCode hash;
1788 if (NULL != t->client)
1793 GMC_hash32 (t->local_tid_dest, &hash);
1795 GNUNET_CONTAINER_multihashmap_put (c->incoming_tunnels, &hash, t,
1796 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1802 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1803 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1813 tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
1815 unsigned int own_pos;
1817 for (own_pos = 0; own_pos < p->length; own_pos++)
1819 if (p->peers[own_pos] == myid)
1822 if (own_pos > p->length - 1)
1828 if (own_pos < p->length - 1)
1829 t->next_hop = p->peers[own_pos + 1];
1831 t->next_hop = p->peers[own_pos];
1832 GNUNET_PEER_change_rc (t->next_hop, 1);
1834 t->prev_hop = p->peers[own_pos - 1];
1836 t->prev_hop = p->peers[0];
1837 GNUNET_PEER_change_rc (t->prev_hop, 1);
1839 if (NULL != t->path)
1840 path_destroy (t->path);
1841 t->path = path_duplicate (p);
1844 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
1845 GNUNET_SCHEDULER_cancel (t->maintenance_task);
1846 t->maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
1853 * Notifies a tunnel that a connection has broken that affects at least
1854 * some of its peers. Sends a notification towards the root of the tree.
1855 * In case the peer is the owner of the tree, notifies the client that owns
1856 * the tunnel and tries to reconnect.
1858 * @param t Tunnel affected.
1859 * @param p1 Peer that got disconnected from p2.
1860 * @param p2 Peer that got disconnected from p1.
1862 * @return Short ID of the peer disconnected (either p1 or p2).
1863 * 0 if the tunnel remained unaffected.
1865 static GNUNET_PEER_Id
1866 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1869 // if (myid != p1 && myid != p2) FIXME
1874 // if (tree_get_predecessor (t->tree) != 0)
1876 // /* We are the peer still connected, notify owner of the disconnection. */
1877 // struct GNUNET_MESH_PathBroken msg;
1878 // struct GNUNET_PeerIdentity neighbor;
1880 // msg.header.size = htons (sizeof (msg));
1881 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1882 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1883 // msg.tid = htonl (t->id.tid);
1884 // msg.peer1 = my_full_id;
1885 // GNUNET_PEER_resolve (pid, &msg.peer2);
1886 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1887 // send_prebuilt_message (&msg.header, &neighbor, t);
1894 * Build a local ACK message and send it to a local client.
1896 * @param t Tunnel on which to send the ACK.
1897 * @param c Client to whom send the ACK.
1898 * @param ack Value of the ACK.
1901 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
1903 struct GNUNET_MESH_LocalAck msg;
1905 msg.header.size = htons (sizeof (msg));
1906 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1907 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
1908 msg.max_pid = htonl (ack);
1909 GNUNET_SERVER_notification_context_unicast(nc,
1916 * Build an ACK message and queue it to send to the given peer.
1918 * @param t Tunnel on which to send the ACK.
1919 * @param peer Peer to whom send the ACK.
1920 * @param ack Value of the ACK.
1923 send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
1925 struct GNUNET_MESH_ACK msg;
1927 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1928 msg.header.size = htons (sizeof (msg));
1929 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
1930 msg.pid = htonl (ack);
1931 msg.tid = htonl (t->id.tid);
1933 send_prebuilt_message (&msg.header, peer, t);
1938 * Send an ACK informing the predecessor about the available buffer space.
1939 * In case there is no predecessor, inform the owning client.
1940 * If buffering is off, send only on behalf of children or self if endpoint.
1941 * If buffering is on, send when sent to children and buffer space is free.
1942 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
1943 * the ACK itself goes "back" (towards root).
1945 * @param t Tunnel on which to send the ACK.
1946 * @param type Type of message that triggered the ACK transmission.
1949 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
1953 /* Is it after unicast retransmission? */
1956 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1958 "ACK due to FWD DATA retransmission\n");
1959 if (GNUNET_YES == t->nobuffer)
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
1965 case GNUNET_MESSAGE_TYPE_MESH_ACK:
1966 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1968 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
1969 case GNUNET_MESSAGE_TYPE_MESH_POLL:
1970 t->force_ack = GNUNET_YES;
1976 /* Check if we need to transmit the ACK */
1977 if (t->queue_max > t->next_fc.queue_n * 4 &&
1978 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) &&
1979 GNUNET_NO == t->force_ack)
1981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
1982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1983 " t->qmax: %u, t->qn: %u\n",
1984 t->queue_max, t->next_fc.queue_n);
1985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1986 " t->pid: %u, t->ack: %u\n",
1987 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent);
1991 /* Ok, ACK might be necessary, what PID to ACK? */
1992 ack = t->prev_fc.last_pid_recv + t->queue_max - t->next_fc.queue_n;
1993 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack)
1995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
1999 t->prev_fc.last_ack_sent = ack;
2000 if (NULL != t->owner)
2001 send_local_ack (t, t->owner, ack);
2002 else if (0 != t->prev_hop)
2003 send_ack (t, t->prev_hop, ack);
2007 t->force_ack = GNUNET_NO;
2012 * Send an ACK informing the children node/client about the available
2014 * If buffering is off, send only on behalf of root (can be self).
2015 * If buffering is on, send when sent to predecessor and buffer space is free.
2016 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2017 * the ACK itself goes "forward" (towards children/clients).
2019 * @param t Tunnel on which to send the ACK.
2020 * @param type Type of message that triggered the ACK transmission.
2023 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2027 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2028 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2029 /* Is it after data to_origin retransmission? */
2032 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2033 if (GNUNET_YES == t->nobuffer)
2035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2036 " Not sending ACK, nobuffer + traffic\n");
2040 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2041 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2043 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2044 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2045 t->force_ack = GNUNET_YES;
2051 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2053 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2055 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2058 " Not sending ACK, not needed\n");
2061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2062 " Sending BCK ACK %u (last sent: %u)\n",
2063 ack, t->next_fc.last_ack_sent);
2064 t->next_fc.last_ack_sent = ack;
2066 if (NULL != t->client)
2067 send_local_ack (t, t->client, ack);
2068 else if (0 != t->next_hop)
2069 send_ack (t, t->next_hop, ack);
2072 t->force_ack = GNUNET_NO;
2077 * @brief Re-initiate traffic to this peer if necessary.
2079 * Check if there is traffic queued towards this peer
2080 * and the core transmit handle is NULL (traffic was stalled).
2081 * If so, call core tmt rdy.
2083 * @param peer_id Short ID of peer to which initiate traffic.
2086 peer_unlock_queue(GNUNET_PEER_Id peer_id)
2088 struct MeshPeerInfo *peer;
2089 struct GNUNET_PeerIdentity id;
2090 struct MeshPeerQueue *q;
2093 peer = peer_get_short (peer_id);
2094 if (NULL != peer->core_transmit)
2097 q = queue_get_next (peer);
2100 /* Might br multicast traffic already sent to this particular peer but
2101 * not to other children in this tunnel.
2102 * This way t->queue_n would be > 0 but the queue of this particular peer
2108 GNUNET_PEER_resolve (peer->id, &id);
2109 peer->core_transmit =
2110 GNUNET_CORE_notify_transmit_ready(core_handle,
2113 GNUNET_TIME_UNIT_FOREVER_REL,
2123 * Send a message to all peers in this tunnel that the tunnel is no longer
2126 * @param t The tunnel whose peers to notify.
2129 tunnel_send_destroy (struct MeshTunnel *t)
2131 struct GNUNET_MESH_TunnelDestroy msg;
2132 struct GNUNET_PeerIdentity id;
2134 msg.header.size = htons (sizeof (msg));
2135 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2136 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2137 msg.tid = htonl (t->id.tid);
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2139 " sending tunnel destroy for tunnel: %s [%X]\n",
2140 GNUNET_i2s (&msg.oid), t->id.tid);
2142 if (NULL == t->client && 0 != t->next_hop)
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop);
2145 GNUNET_PEER_resolve (t->next_hop, &id);
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2147 " sending forward to %s\n",
2149 send_prebuilt_message (&msg.header, t->next_hop, t);
2151 if (NULL == t->owner && 0 != t->prev_hop)
2153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop);
2154 GNUNET_PEER_resolve (t->prev_hop, &id);
2155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2156 " sending back to %s\n",
2158 send_prebuilt_message (&msg.header, t->prev_hop, t);
2164 * Cancel all transmissions towards a neighbor that belongs to a certain tunnel.
2166 * @param t Tunnel which to cancel.
2167 * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
2170 peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
2172 struct MeshPeerInfo *peer_info;
2173 struct MeshPeerQueue *pq;
2174 struct MeshPeerQueue *next;
2176 peer_info = peer_get_short (neighbor);
2177 for (pq = peer_info->queue_head; NULL != pq; pq = next)
2180 if (pq->tunnel == t)
2182 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
2183 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
2185 /* Should have been removed on destroy children */
2188 queue_destroy (pq, GNUNET_YES);
2191 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
2193 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
2194 peer_info->core_transmit = NULL;
2200 * Destroy the tunnel and free any allocated resources linked to it.
2202 * @param t the tunnel to destroy
2204 * @return GNUNET_OK on success
2207 tunnel_destroy (struct MeshTunnel *t)
2209 struct MeshClient *c;
2210 struct GNUNET_HashCode hash;
2220 struct GNUNET_PeerIdentity id;
2222 GNUNET_PEER_resolve (t->id.oid, &id);
2223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2224 GNUNET_i2s (&id), t->id.tid);
2226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2230 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2231 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2239 GMC_hash32 (t->local_tid, &hash);
2241 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2248 if (NULL != t->client)
2251 GMC_hash32 (t->local_tid_dest, &hash);
2253 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2259 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2266 peer_cancel_queues (t->next_hop, t);
2267 peer_cancel_queues (t->prev_hop, t);
2268 GNUNET_PEER_change_rc(t->next_hop, -1);
2269 GNUNET_PEER_change_rc(t->prev_hop, -1);
2271 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2272 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2275 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
2276 path_destroy (t->path);
2282 * Tunnel is empty: destroy it.
2284 * @param t Tunnel to destroy.
2287 tunnel_destroy_empty (struct MeshTunnel *t)
2291 struct GNUNET_PeerIdentity id;
2293 GNUNET_PEER_resolve (t->id.oid, &id);
2294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2295 "executing destruction of empty tunnel %s [%X]\n",
2296 GNUNET_i2s (&id), t->id.tid);
2300 if (GNUNET_NO == t->destroy)
2301 tunnel_send_destroy (t);
2302 if (0 == t->pending_messages)
2305 t->destroy = GNUNET_YES;
2309 * Initialize a Flow Control structure to the initial state.
2311 * @param fc Flow Control structure to initialize.
2314 fc_init (struct MeshFlowControl *fc)
2316 fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
2317 fc->last_pid_recv = (uint32_t) -1;
2318 fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
2319 fc->last_ack_recv = (uint32_t) -1;
2320 fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
2321 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
2326 * Create a new tunnel
2328 * @param owner Who is the owner of the tunnel (short ID).
2329 * @param tid Tunnel Number of the tunnel.
2330 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
2331 * @param local Tunnel Number for the tunnel, for the client point of view.
2333 * @return A new initialized tunnel. NULL on error.
2335 static struct MeshTunnel *
2336 tunnel_new (GNUNET_PEER_Id owner,
2337 MESH_TunnelNumber tid,
2338 struct MeshClient *client,
2339 MESH_TunnelNumber local)
2341 struct MeshTunnel *t;
2342 struct GNUNET_HashCode hash;
2344 if (n_tunnels >= max_tunnels && NULL == client)
2347 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2350 t->queue_max = (max_msgs_queue / max_tunnels) + 1;
2352 fc_init (&t->next_fc);
2353 fc_init (&t->prev_fc);
2354 t->local_tid = local;
2356 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
2358 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2360 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2361 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2368 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2375 GMC_hash32 (t->local_tid, &hash);
2377 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2378 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2382 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2393 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2394 * client when the client disconnects. If the client is not the owner, the
2395 * owner will get notified if no more clients are in the tunnel and the client
2396 * get removed from the tunnel's list.
2398 * @param cls closure (client that is disconnecting)
2399 * @param key the hash of the local tunnel id (used to access the hashmap)
2400 * @param value the value stored at the key (tunnel to destroy)
2402 * @return GNUNET_OK, keep iterating.
2405 tunnel_destroy_iterator (void *cls,
2406 const struct GNUNET_HashCode * key,
2409 struct MeshTunnel *t = value;
2410 struct MeshClient *c = cls;
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2413 " Tunnel %X / %X destroy, due to client %u shutdown.\n",
2414 t->local_tid, t->local_tid_dest, c->id);
2415 if (GNUNET_NO == c->shutting_down)
2416 send_client_tunnel_destroy (t);
2417 client_delete_tunnel (c, t);
2420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id);
2424 else if (c == t->owner)
2426 struct MeshPeerInfo *p;
2427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
2430 p = peer_get_short(t->dest);
2431 peer_info_remove_tunnel (p, t);
2437 tunnel_destroy_empty (t);
2444 * Timeout function, destroys tunnel if called
2446 * @param cls Closure (tunnel to destroy).
2447 * @param tc TaskContext
2450 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2452 struct MeshTunnel *t = cls;
2453 struct GNUNET_PeerIdentity id;
2455 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
2456 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2458 GNUNET_PEER_resolve(t->id.oid, &id);
2459 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2460 "Tunnel %s [%X] timed out. Destroying.\n",
2461 GNUNET_i2s(&id), t->id.tid);
2462 send_client_tunnel_destroy (t);
2468 * Resets the tunnel timeout. Starts it if no timeout was running.
2470 * @param t Tunnel whose timeout to reset.
2472 * TODO use heap to improve efficiency of scheduler.
2475 tunnel_reset_timeout (struct MeshTunnel *t)
2477 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
2479 if (GNUNET_SCHEDULER_NO_TASK != t->maintenance_task)
2480 GNUNET_SCHEDULER_cancel (t->maintenance_task);
2481 t->maintenance_task =
2482 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2483 (refresh_path_time, 4), &tunnel_timeout, t);
2487 /******************************************************************************/
2488 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2489 /******************************************************************************/
2492 * Function to send a create path packet to a peer.
2494 * @param cls closure
2495 * @param size number of bytes available in buf
2496 * @param buf where the callee should write the message
2497 * @return number of bytes written to buf
2500 send_core_path_create (void *cls, size_t size, void *buf)
2502 struct MeshTunnel *t = cls;
2503 struct GNUNET_MESH_CreateTunnel *msg;
2504 struct GNUNET_PeerIdentity *peer_ptr;
2505 struct MeshPeerPath *p = t->path;
2510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2512 sizeof (struct GNUNET_MESH_CreateTunnel) +
2513 p->length * sizeof (struct GNUNET_PeerIdentity);
2515 if (size < size_needed || NULL == buf)
2520 msg = (struct GNUNET_MESH_CreateTunnel *) buf;
2521 msg->header.size = htons (size_needed);
2522 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2523 msg->tid = ntohl (t->id.tid);
2526 if (GNUNET_YES == t->nobuffer)
2527 opt |= MESH_TUNNEL_OPT_NOBUFFER;
2528 msg->opt = htonl (opt);
2529 msg->port = htonl (t->port);
2531 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2532 for (i = 0; i < p->length; i++)
2534 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2538 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2544 * Creates a path ack message in buf and frees all unused resources.
2546 * @param cls closure (MeshTransmissionDescriptor)
2547 * @param size number of bytes available in buf
2548 * @param buf where the callee should write the message
2549 * @return number of bytes written to buf
2552 send_core_path_ack (void *cls, size_t size, void *buf)
2554 struct MeshTunnel *t = cls;
2555 struct GNUNET_MESH_PathACK *msg = buf;
2557 GNUNET_assert (NULL != t);
2558 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2563 t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
2564 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2565 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2566 GNUNET_PEER_resolve (t->id.oid, &msg->oid);
2567 msg->tid = htonl (t->id.tid);
2568 msg->peer_id = my_full_id;
2569 msg->ack = htonl (t->prev_fc.last_ack_sent);
2571 /* TODO add signature */
2573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2574 return sizeof (struct GNUNET_MESH_PathACK);
2579 * Free a transmission that was already queued with all resources
2580 * associated to the request.
2582 * @param queue Queue handler to cancel.
2583 * @param clear_cls Is it necessary to free associated cls?
2586 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2588 struct MeshFlowControl *fc;
2590 if (GNUNET_YES == clear_cls)
2592 switch (queue->type)
2594 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2595 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
2596 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
2598 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2599 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2600 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2601 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2602 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2604 " prebuilt message\n");
2605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2607 GNUNET_MESH_DEBUG_M2S (queue->type));
2609 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2614 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2615 " type %s unknown!\n",
2616 GNUNET_MESH_DEBUG_M2S (queue->type));
2618 GNUNET_free_non_null (queue->cls);
2620 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2621 queue->peer->queue_tail,
2624 /* Delete from appropiate fc in the tunnel */
2625 if (queue->peer->id == queue->tunnel->next_hop)
2626 fc = &queue->tunnel->next_fc;
2627 else if (queue->peer->id == queue->tunnel->next_hop)
2628 fc = &queue->tunnel->next_fc;
2635 GNUNET_free (queue);
2640 * @brief Get the next transmittable message from the queue.
2642 * This will be the head, except in the case of being a data packet
2643 * not allowed by the destination peer.
2645 * @param peer Destination peer.
2647 * @return The next viable MeshPeerQueue element to send to that peer.
2648 * NULL when there are no transmittable messages.
2650 struct MeshPeerQueue *
2651 queue_get_next (const struct MeshPeerInfo *peer)
2653 struct MeshPeerQueue *q;
2655 struct GNUNET_MESH_Unicast *ucast;
2656 struct GNUNET_MESH_ToOrigin *to_orig;
2657 struct MeshTunnel* t;
2661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n");
2662 for (q = peer->queue_head; NULL != q; q = q->next)
2665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2667 GNUNET_MESH_DEBUG_M2S(q->type));
2670 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2671 ucast = (struct GNUNET_MESH_Unicast *) q->cls;
2672 pid = ntohl (ucast->pid);
2673 ack = t->next_fc.last_ack_recv;
2675 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2676 to_orig = (struct GNUNET_MESH_ToOrigin *) q->cls;
2677 pid = ntohl (to_orig->pid);
2678 ack = t->prev_fc.last_ack_recv;
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2686 "* ACK: %u, PID: %u\n",
2688 if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2701 "* nothing found\n");
2707 queue_send (void *cls, size_t size, void *buf)
2709 struct MeshPeerInfo *peer = cls;
2710 struct GNUNET_MessageHeader *msg;
2711 struct MeshPeerQueue *queue;
2712 struct MeshTunnel *t;
2713 struct GNUNET_PeerIdentity dst_id;
2714 struct MeshFlowControl *fc;
2718 peer->core_transmit = NULL;
2720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
2721 queue = queue_get_next (peer);
2723 /* Queue has no internal mesh traffic nor sendable payload */
2726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n");
2727 if (NULL == peer->queue_head)
2728 GNUNET_break (0); /* Core tmt_rdy should've been canceled */
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n");
2733 GNUNET_PEER_resolve (peer->id, &dst_id);
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2736 GNUNET_i2s (&dst_id));
2737 /* Check if buffer size is enough for the message */
2738 if (queue->size > size)
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2741 "* not enough room, reissue\n");
2742 peer->core_transmit =
2743 GNUNET_CORE_notify_transmit_ready (core_handle,
2746 GNUNET_TIME_UNIT_FOREVER_REL,
2753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n");
2756 GNUNET_assert (0 < t->pending_messages);
2757 t->pending_messages--;
2761 switch (queue->type)
2764 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2765 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2766 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
2767 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
2768 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2769 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
2770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2772 GNUNET_MESH_DEBUG_M2S (queue->type));
2774 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2775 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2776 data_size = send_core_data_raw (queue->cls, size, buf);
2777 msg = (struct GNUNET_MessageHeader *) buf;
2778 type = ntohs (msg->type);
2780 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n");
2782 data_size = send_core_path_create (queue->cls, size, buf);
2784 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n");
2786 data_size = send_core_path_ack (queue->cls, size, buf);
2790 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2791 "* type unknown: %u\n",
2796 /* Free queue, but cls was freed by send_core_* */
2797 queue_destroy (queue, GNUNET_NO);
2799 /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
2802 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2803 t->next_fc.last_pid_sent = ((struct GNUNET_MESH_Unicast *) buf)->pid;
2804 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
2806 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2807 t->prev_fc.last_pid_sent = ((struct GNUNET_MESH_ToOrigin *) buf)->pid;
2808 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
2814 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n");
2820 /* If more data in queue, send next */
2821 queue = queue_get_next (peer);
2824 struct GNUNET_PeerIdentity id;
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n");
2827 GNUNET_PEER_resolve (peer->id, &id);
2828 peer->core_transmit =
2829 GNUNET_CORE_notify_transmit_ready(core_handle,
2832 GNUNET_TIME_UNIT_FOREVER_REL,
2838 else if (NULL != peer->queue_head)
2840 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2842 GNUNET_i2s (&my_full_id));
2843 if (peer->id == t->next_hop)
2845 else if (peer->id == t->prev_hop)
2852 if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
2855 fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
2859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size);
2865 * @brief Queue and pass message to core when possible.
2867 * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
2868 * accounts for it. In case the queue is full, the message is dropped and
2871 * Otherwise, message is treated as internal and allowed to go regardless of
2874 * @param cls Closure (@c type dependant). It will be used by queue_send to
2875 * build the message to be sent if not already prebuilt.
2876 * @param type Type of the message, 0 for a raw message.
2877 * @param size Size of the message.
2878 * @param dst Neighbor to send message to.
2879 * @param t Tunnel this message belongs to.
2882 queue_add (void *cls, uint16_t type, size_t size,
2883 struct MeshPeerInfo *dst, struct MeshTunnel *t)
2885 struct MeshPeerQueue *queue;
2886 struct GNUNET_PeerIdentity id;
2890 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2892 n = &t->next_fc.queue_n;
2894 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
2896 n = &t->prev_fc.queue_n;
2900 if (*n >= t->queue_max)
2903 GNUNET_STATISTICS_update(stats,
2904 "# messages dropped (buffer full)",
2906 return; /* Drop message */
2910 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2916 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
2917 if (NULL == dst->core_transmit)
2919 GNUNET_PEER_resolve (dst->id, &id);
2920 dst->core_transmit =
2921 GNUNET_CORE_notify_transmit_ready (core_handle,
2924 GNUNET_TIME_UNIT_FOREVER_REL,
2930 t->pending_messages++;
2934 /******************************************************************************/
2935 /******************** MESH NETWORK HANDLERS **************************/
2936 /******************************************************************************/
2940 * Core handler for path creation
2942 * @param cls closure
2943 * @param message message
2944 * @param peer peer identity this notification is about
2946 * @return GNUNET_OK to keep the connection open,
2947 * GNUNET_SYSERR to close it (signal serious error)
2950 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2951 const struct GNUNET_MessageHeader *message)
2953 unsigned int own_pos;
2956 MESH_TunnelNumber tid;
2957 struct GNUNET_MESH_CreateTunnel *msg;
2958 struct GNUNET_PeerIdentity *pi;
2959 struct MeshPeerPath *path;
2960 struct MeshPeerInfo *dest_peer_info;
2961 struct MeshPeerInfo *orig_peer_info;
2962 struct MeshTunnel *t;
2964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2965 "Received a path create msg [%s]\n",
2966 GNUNET_i2s (&my_full_id));
2967 size = ntohs (message->size);
2968 if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
2970 GNUNET_break_op (0);
2974 size -= sizeof (struct GNUNET_MESH_CreateTunnel);
2975 if (size % sizeof (struct GNUNET_PeerIdentity))
2977 GNUNET_break_op (0);
2980 size /= sizeof (struct GNUNET_PeerIdentity);
2983 GNUNET_break_op (0);
2986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2987 msg = (struct GNUNET_MESH_CreateTunnel *) message;
2989 tid = ntohl (msg->tid);
2990 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2992 " path is for tunnel %s[%X].\n", GNUNET_i2s (pi), tid);
2993 t = tunnel_get (pi, tid);
2994 if (NULL == t) /* might be a local tunnel */
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2999 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
3005 t->port = ntohl (msg->port);
3006 opt = ntohl (msg->opt);
3007 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
3009 t->nobuffer = GNUNET_YES;
3010 t->prev_fc.last_ack_sent = t->prev_fc.last_pid_recv + 1;
3011 t->next_fc.last_ack_sent = t->next_fc.last_pid_recv + 1;
3014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3016 tunnel_reset_timeout (t);
3018 t->state = MESH_TUNNEL_WAITING;
3020 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
3021 if (NULL == dest_peer_info)
3023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3024 " Creating PeerInfo for destination.\n");
3025 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3026 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
3027 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
3029 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3031 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
3032 if (NULL == orig_peer_info)
3034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3035 " Creating PeerInfo for origin.\n");
3036 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
3037 orig_peer_info->id = GNUNET_PEER_intern (pi);
3038 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
3039 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3042 path = path_new (size);
3044 for (i = 0; i < size; i++)
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3047 GNUNET_i2s (&pi[i]));
3048 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3049 if (path->peers[i] == myid)
3052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3053 if (own_pos == 0 && path->peers[own_pos] != myid)
3055 /* create path: self not found in path through self */
3056 GNUNET_break_op (0);
3057 path_destroy (path);
3061 path_add_to_peers (path, GNUNET_NO);
3062 tunnel_use_path (t, path);
3064 if (own_pos == size - 1)
3066 struct MeshClient *c;
3067 struct GNUNET_HashCode hc;
3069 /* Find target client */
3070 GMC_hash32 (t->port, &hc);
3071 c = GNUNET_CONTAINER_multihashmap_get (ports, &hc);
3074 /* TODO send reject */
3078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3079 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
3082 /* Assign local tid */
3083 while (NULL != tunnel_get_incoming (next_local_tid))
3084 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3085 t->local_tid_dest = next_local_tid++;
3086 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
3088 tunnel_add_client (t, c);
3089 send_client_tunnel_create (t);
3094 struct MeshPeerPath *path2;
3096 t->next_hop = path->peers[own_pos + 1];
3097 GNUNET_PEER_change_rc(t->next_hop, 1);
3099 /* It's for somebody else! Retransmit. */
3100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3101 path2 = path_duplicate (path);
3102 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3103 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3104 send_create_path (t);
3112 * Core handler for path ACKs
3114 * @param cls closure
3115 * @param message message
3116 * @param peer peer identity this notification is about
3118 * @return GNUNET_OK to keep the connection open,
3119 * GNUNET_SYSERR to close it (signal serious error)
3122 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3123 const struct GNUNET_MessageHeader *message)
3125 struct GNUNET_MESH_PathACK *msg;
3126 struct MeshPeerInfo *peer_info;
3127 struct MeshPeerPath *p;
3128 struct MeshTunnel *t;
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3131 GNUNET_i2s (&my_full_id));
3132 msg = (struct GNUNET_MESH_PathACK *) message;
3133 t = tunnel_get (&msg->oid, ntohl(msg->tid));
3136 /* TODO notify that we don't know the tunnel */
3137 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
3138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
3139 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
3143 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3145 peer_info = peer_get (&msg->peer_id);
3146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
3147 GNUNET_i2s (&msg->peer_id));
3148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
3151 /* Add path to peers? */
3155 path_add_to_peers (p, GNUNET_YES);
3161 t->state = MESH_TUNNEL_READY;
3162 t->next_fc.last_ack_recv = ntohl (msg->ack);
3163 t->prev_fc.last_ack_sent = ntohl (msg->ack);
3165 /* Message for us? */
3166 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3169 if (NULL == t->owner)
3171 GNUNET_break_op (0);
3174 if (NULL != peer_info->dhtget)
3176 GNUNET_DHT_get_stop (peer_info->dhtget);
3177 peer_info->dhtget = NULL;
3179 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3180 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
3184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3185 " not for us, retransmitting...\n");
3186 peer_info = peer_get (&msg->oid);
3187 send_prebuilt_message (message, t->prev_hop, t);
3193 * Core handler for notifications of broken paths
3195 * @param cls closure
3196 * @param message message
3197 * @param peer peer identity this notification is about
3199 * @return GNUNET_OK to keep the connection open,
3200 * GNUNET_SYSERR to close it (signal serious error)
3203 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3204 const struct GNUNET_MessageHeader *message)
3206 struct GNUNET_MESH_PathBroken *msg;
3207 struct MeshTunnel *t;
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3210 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3211 msg = (struct GNUNET_MESH_PathBroken *) message;
3212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3213 GNUNET_i2s (&msg->peer1));
3214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3215 GNUNET_i2s (&msg->peer2));
3216 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3219 GNUNET_break_op (0);
3222 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3223 GNUNET_PEER_search (&msg->peer2));
3230 * Core handler for tunnel destruction
3232 * @param cls closure
3233 * @param message message
3234 * @param peer peer identity this notification is about
3236 * @return GNUNET_OK to keep the connection open,
3237 * GNUNET_SYSERR to close it (signal serious error)
3240 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3241 const struct GNUNET_MessageHeader *message)
3243 struct GNUNET_MESH_TunnelDestroy *msg;
3244 struct MeshTunnel *t;
3246 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3248 "Got a TUNNEL DESTROY packet from %s\n",
3250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3251 " for tunnel %s [%u]\n",
3252 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3253 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3256 /* Probably already got the message from another path,
3257 * destroyed the tunnel and retransmitted to children.
3260 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
3264 // TODO check owner's signature
3265 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3267 /* Tunnel was incoming, notify clients */
3268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3269 t->local_tid, t->local_tid_dest);
3270 send_client_tunnel_destroy (t);
3272 tunnel_send_destroy (t);
3273 t->destroy = GNUNET_YES;
3274 // TODO: add timeout to destroy the tunnel anyway
3280 * Core handler for mesh network traffic going from the origin to a peer
3282 * @param cls closure
3283 * @param peer peer identity this notification is about
3284 * @param message message
3285 * @return GNUNET_OK to keep the connection open,
3286 * GNUNET_SYSERR to close it (signal serious error)
3289 handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3290 const struct GNUNET_MessageHeader *message)
3292 struct GNUNET_MESH_Unicast *msg;
3293 struct MeshTunnel *t;
3298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3301 size = ntohs (message->size);
3303 sizeof (struct GNUNET_MESH_Unicast) +
3304 sizeof (struct GNUNET_MessageHeader))
3309 msg = (struct GNUNET_MESH_Unicast *) message;
3310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3311 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3313 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3316 /* TODO notify back: we don't know this tunnel */
3317 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3318 GNUNET_break_op (0);
3321 pid = ntohl (msg->pid);
3322 if (t->prev_fc.last_pid_recv == pid)
3324 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
3325 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3326 " Already seen pid %u, DROPPING!\n", pid);
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3332 " pid %u not seen yet, forwarding\n", pid);
3335 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent))
3337 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
3338 GNUNET_break_op (0);
3339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3340 "Received PID %u, ACK %u\n",
3341 pid, t->prev_fc.last_ack_sent);
3342 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3345 t->prev_fc.last_pid_recv = pid;
3347 tunnel_reset_timeout (t);
3348 if (t->dest == myid)
3350 if (NULL == t->client)
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3356 " it's for us! sending to clients...\n");
3357 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
3358 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
3359 message, GNUNET_NO);
3360 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
3363 if (0 == t->next_hop)
3368 ttl = ntohl (msg->ttl);
3369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
3372 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
3373 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3374 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3378 " not for us, retransmitting...\n");
3380 send_prebuilt_message (message, t->next_hop, t);
3381 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
3387 * Core handler for mesh network traffic toward the owner of a tunnel
3389 * @param cls closure
3390 * @param message message
3391 * @param peer peer identity this notification is about
3393 * @return GNUNET_OK to keep the connection open,
3394 * GNUNET_SYSERR to close it (signal serious error)
3397 handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3398 const struct GNUNET_MessageHeader *message)
3400 struct GNUNET_MESH_ToOrigin *msg;
3401 struct MeshTunnel *t;
3405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3407 size = ntohs (message->size);
3408 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3409 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3411 GNUNET_break_op (0);
3414 msg = (struct GNUNET_MESH_ToOrigin *) message;
3415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
3416 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
3417 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3418 pid = ntohl (msg->pid);
3422 /* TODO notify that we dont know this tunnel (whom)? */
3423 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
3424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3425 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
3426 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3430 if (t->next_fc.last_pid_recv == pid)
3432 /* already seen this packet, drop */
3433 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
3434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3435 " Already seen pid %u, DROPPING!\n", pid);
3436 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3440 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
3442 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
3443 GNUNET_break_op (0);
3444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3445 "Received PID %u, ACK %u\n",
3446 pid, t->next_fc.last_ack_sent);
3447 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3450 t->next_fc.last_pid_recv = pid;
3451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3452 " pid %u not seen yet, forwarding\n", pid);
3454 if (NULL != t->owner)
3457 struct GNUNET_MESH_ToOrigin *copy;
3459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3460 " it's for us! sending to clients...\n");
3461 /* TODO signature verification */
3462 memcpy (cbuf, message, size);
3463 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3464 copy->tid = htonl (t->local_tid);
3465 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
3466 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3467 ©->header, GNUNET_NO);
3468 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
3471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3472 " not for us, retransmitting...\n");
3474 if (0 == t->prev_hop) /* No owner AND no prev hop */
3476 if (GNUNET_YES == t->destroy)
3478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3479 "to orig received on a dying tunnel %s [%X]\n",
3480 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3484 "unknown to origin at %s\n",
3485 GNUNET_i2s (&my_full_id));
3486 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3489 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3490 "on tunnel %s [%X]\n",
3491 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
3494 send_prebuilt_message (message, t->prev_hop, t);
3495 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
3502 * Core handler for mesh network traffic point-to-point acks.
3504 * @param cls closure
3505 * @param message message
3506 * @param peer peer identity this notification is about
3508 * @return GNUNET_OK to keep the connection open,
3509 * GNUNET_SYSERR to close it (signal serious error)
3512 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3513 const struct GNUNET_MessageHeader *message)
3515 struct GNUNET_MESH_ACK *msg;
3516 struct MeshTunnel *t;
3520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
3522 msg = (struct GNUNET_MESH_ACK *) message;
3524 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3528 /* TODO notify that we dont know this tunnel (whom)? */
3529 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
3532 ack = ntohl (msg->pid);
3533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
3535 /* Is this a forward or backward ACK? */
3536 id = GNUNET_PEER_search (peer);
3537 if (t->next_hop == id)
3540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
3541 if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task &&
3542 GMC_is_pid_bigger (ack, t->next_fc.last_ack_recv))
3544 GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
3545 t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3546 t->next_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3548 t->next_fc.last_ack_recv = ack;
3549 peer_unlock_queue (t->next_hop);
3550 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3552 else if (t->prev_hop == id)
3555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
3556 if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task &&
3557 GMC_is_pid_bigger (ack, t->prev_fc.last_ack_recv))
3559 GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
3560 t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
3561 t->prev_fc.poll_time = GNUNET_TIME_UNIT_SECONDS;
3563 t->prev_fc.last_ack_recv = ack;
3564 peer_unlock_queue (t->prev_hop);
3565 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
3568 GNUNET_break_op (0);
3574 * Core handler for mesh network traffic point-to-point ack polls.
3576 * @param cls closure
3577 * @param message message
3578 * @param peer peer identity this notification is about
3580 * @return GNUNET_OK to keep the connection open,
3581 * GNUNET_SYSERR to close it (signal serious error)
3584 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
3585 const struct GNUNET_MessageHeader *message)
3587 struct GNUNET_MESH_Poll *msg;
3588 struct MeshTunnel *t;
3591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
3594 msg = (struct GNUNET_MESH_Poll *) message;
3596 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3600 /* TODO notify that we dont know this tunnel (whom)? */
3601 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
3602 GNUNET_break_op (0);
3606 /* Is this a forward or backward ACK? */
3607 id = GNUNET_PEER_search(peer);
3608 if (t->next_hop == id)
3610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
3611 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3613 else if (t->prev_hop == id)
3615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
3616 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
3626 * Core handler for mesh keepalives.
3628 * @param cls closure
3629 * @param message message
3630 * @param peer peer identity this notification is about
3631 * @return GNUNET_OK to keep the connection open,
3632 * GNUNET_SYSERR to close it (signal serious error)
3634 * TODO: Check who we got this from, to validate route.
3637 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
3638 const struct GNUNET_MessageHeader *message)
3640 struct GNUNET_MESH_TunnelKeepAlive *msg;
3641 struct MeshTunnel *t;
3643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
3646 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
3647 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3651 /* TODO notify that we dont know that tunnel */
3652 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
3657 tunnel_reset_timeout (t);
3659 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
3660 send_prebuilt_message (message, t->next_hop, t);
3667 * Functions to handle messages from core
3669 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3670 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3671 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3672 sizeof (struct GNUNET_MESH_PathBroken)},
3673 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
3674 sizeof (struct GNUNET_MESH_TunnelDestroy)},
3675 {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3676 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
3677 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
3678 {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3679 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
3680 sizeof (struct GNUNET_MESH_ACK)},
3681 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
3682 sizeof (struct GNUNET_MESH_Poll)},
3683 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3684 sizeof (struct GNUNET_MESH_PathACK)},
3690 /******************************************************************************/
3691 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3692 /******************************************************************************/
3697 * notify_client_connection_failure: notify a client that the connection to the
3698 * requested remote peer is not possible (for instance, no route found)
3699 * Function called when the socket is ready to queue more data. "buf" will be
3700 * NULL and "size" zero if the socket was closed for writing in the meantime.
3702 * @param cls closure
3703 * @param size number of bytes available in buf
3704 * @param buf where the callee should write the message
3705 * @return number of bytes written to buf
3708 notify_client_connection_failure (void *cls, size_t size, void *buf)
3711 struct MeshPeerInfo *peer_info;
3712 struct GNUNET_MESH_PeerControl *msg;
3713 struct GNUNET_PeerIdentity id;
3715 if (0 == size && NULL == buf)
3717 // TODO retry? cancel?
3721 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3722 peer_info = (struct MeshPeerInfo *) cls;
3723 msg = (struct GNUNET_MESH_PeerControl *) buf;
3724 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3725 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3726 // msg->tunnel_id = htonl(peer_info->t->tid);
3727 GNUNET_PEER_resolve (peer_info->id, &id);
3728 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3736 * Send keepalive packets for a tunnel.
3738 * @param cls Closure (tunnel for which to send the keepalive).
3739 * @param tc Notification context.
3742 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3744 struct MeshTunnel *t = cls;
3745 struct GNUNET_MESH_TunnelKeepAlive *msg;
3746 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
3749 t->maintenance_task = GNUNET_SCHEDULER_NO_TASK;
3750 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
3751 NULL == t->owner || 0 == t->local_tid || 0 != t->prev_hop)
3756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3757 "sending keepalive for tunnel %d\n", t->id.tid);
3759 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
3760 msg->header.size = htons (size);
3761 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3762 msg->oid = my_full_id;
3763 msg->tid = htonl (t->id.tid);
3764 send_prebuilt_message (&msg->header, t->next_hop, t);
3766 t->maintenance_task =
3767 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3772 * Function to process paths received for a new peer addition. The recorded
3773 * paths form the initial tunnel, which can be optimized later.
3774 * Called on each result obtained for the DHT search.
3776 * @param cls closure
3777 * @param exp when will this value expire
3778 * @param key key of the result
3779 * @param get_path path of the get request
3780 * @param get_path_length lenght of get_path
3781 * @param put_path path of the put request
3782 * @param put_path_length length of the put_path
3783 * @param type type of the result
3784 * @param size number of bytes in data
3785 * @param data pointer to the result data
3788 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3789 const struct GNUNET_HashCode * key,
3790 const struct GNUNET_PeerIdentity *get_path,
3791 unsigned int get_path_length,
3792 const struct GNUNET_PeerIdentity *put_path,
3793 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3794 size_t size, const void *data)
3796 struct MeshPeerInfo *peer = cls;
3797 struct MeshPeerPath *p;
3798 struct GNUNET_PeerIdentity pi;
3801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3802 GNUNET_PEER_resolve (peer->id, &pi);
3803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3805 p = path_build_from_dht (get_path, get_path_length,
3806 put_path, put_path_length);
3807 path_add_to_peers (p, GNUNET_NO);
3809 for (i = 0; i < peer->ntunnels; i++)
3811 if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
3812 peer_connect (peer, peer->tunnels[i]);
3819 /******************************************************************************/
3820 /********************* MESH LOCAL HANDLES **************************/
3821 /******************************************************************************/
3825 * Handler for client disconnection
3827 * @param cls closure
3828 * @param client identification of the client; NULL
3829 * for the last call when the server is destroyed
3832 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3834 struct MeshClient *c;
3835 struct MeshClient *next;
3837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3847 if (c->handle != client)
3849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3855 GNUNET_SERVER_client_drop (c->handle);
3856 c->shutting_down = GNUNET_YES;
3857 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3858 &tunnel_destroy_iterator, c);
3859 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3860 &tunnel_destroy_iterator, c);
3861 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3862 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3864 if (NULL != c->ports)
3865 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
3867 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
3868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3870 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
3873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
3879 * Handler for new clients
3881 * @param cls closure
3882 * @param client identification of the client
3883 * @param message the actual message, which includes messages the client wants
3886 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3887 const struct GNUNET_MessageHeader *message)
3889 struct GNUNET_MESH_ClientConnect *cc_msg;
3890 struct MeshClient *c;
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3897 /* Check data sanity */
3898 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3899 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3900 if (0 != (size % sizeof (uint32_t)))
3903 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3906 size /= sizeof (uint32_t);
3908 /* Create new client structure */
3909 c = GNUNET_malloc (sizeof (struct MeshClient));
3910 c->id = next_client_id++; /* overflow not important: just for debug */
3911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
3912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
3914 GNUNET_SERVER_client_keep (client);
3918 struct GNUNET_HashCode hc;
3920 p = (uint32_t *) &cc_msg[1];
3921 c->ports = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
3922 for (i = 0; i < size; i++)
3925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
3926 GMC_hash32 (u32, &hc);
3928 /* store in client's hashmap */
3929 GNUNET_CONTAINER_multihashmap_put (c->ports, &hc, c,
3930 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
3931 /* store in global hashmap */
3932 /* FIXME only allow one client to have the port open,
3933 * have a backup hashmap with waiting clients */
3934 GNUNET_CONTAINER_multihashmap_put (ports, &hc, c,
3935 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3939 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
3940 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
3941 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
3942 GNUNET_SERVER_notification_context_add (nc, client);
3943 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
3945 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3951 * Handler for requests of new tunnels
3953 * @param cls Closure.
3954 * @param client Identification of the client.
3955 * @param message The actual message.
3958 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3959 const struct GNUNET_MessageHeader *message)
3961 struct GNUNET_MESH_TunnelMessage *t_msg;
3962 struct MeshPeerInfo *peer_info;
3963 struct MeshTunnel *t;
3964 struct MeshClient *c;
3965 MESH_TunnelNumber tid;
3967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3969 /* Sanity check for client registration */
3970 if (NULL == (c = client_get (client)))
3973 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3978 /* Message sanity check */
3979 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3982 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3986 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s\n",
3988 GNUNET_i2s (&t_msg->peer));
3989 /* Sanity check for tunnel numbering */
3990 tid = ntohl (t_msg->tunnel_id);
3991 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3997 /* Sanity check for duplicate tunnel IDs */
3998 if (NULL != tunnel_get_by_local_id (c, tid))
4001 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4005 while (NULL != tunnel_get_by_pi (myid, next_tid))
4006 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4007 t = tunnel_new (myid, next_tid, c, tid);
4008 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
4012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4015 t->port = ntohl (t_msg->port);
4016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
4017 GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
4019 peer_info = peer_get (&t_msg->peer);
4020 GNUNET_array_append (peer_info->tunnels, peer_info->ntunnels, t);
4021 peer_connect (peer_info, t);
4022 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4028 * Handler for requests of deleting tunnels
4030 * @param cls closure
4031 * @param client identification of the client
4032 * @param message the actual message
4035 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4036 const struct GNUNET_MessageHeader *message)
4038 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4039 struct MeshClient *c;
4040 struct MeshTunnel *t;
4041 MESH_TunnelNumber tid;
4043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4044 "Got a DESTROY TUNNEL from client!\n");
4046 /* Sanity check for client registration */
4047 if (NULL == (c = client_get (client)))
4050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4055 /* Message sanity check */
4056 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4059 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4063 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4065 /* Retrieve tunnel */
4066 tid = ntohl (tunnel_msg->tunnel_id);
4067 t = tunnel_get_by_local_id(c, tid);
4070 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4072 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4077 tunnel_destroy_empty (t);
4078 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4081 send_client_tunnel_destroy (t);
4082 client_delete_tunnel (c, t);
4084 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4086 tunnel_send_destroy (t);
4087 peer_remove_tunnel (peer_get_short(t->dest), t);
4088 t->destroy = GNUNET_YES;
4089 /* The tunnel will be destroyed when the last message is transmitted. */
4090 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4096 * Handler for requests of seeting tunnel's buffering policy.
4098 * @param cls Closure (unused).
4099 * @param client Identification of the client.
4100 * @param message The actual message.
4103 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
4104 const struct GNUNET_MessageHeader *message)
4106 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4107 struct MeshClient *c;
4108 struct MeshTunnel *t;
4109 MESH_TunnelNumber tid;
4111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4112 "Got a BUFFER request from client!\n");
4114 /* Sanity check for client registration */
4115 if (NULL == (c = client_get (client)))
4118 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4123 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4125 /* Retrieve tunnel */
4126 tid = ntohl (tunnel_msg->tunnel_id);
4127 t = tunnel_get_by_local_id(c, tid);
4130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4132 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4136 switch (ntohs(message->type))
4138 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
4139 t->nobuffer = GNUNET_NO;
4141 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
4142 t->nobuffer = GNUNET_YES;
4148 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4153 * Handler for client traffic directed to one peer
4155 * @param cls closure
4156 * @param client identification of the client
4157 * @param message the actual message
4160 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4161 const struct GNUNET_MessageHeader *message)
4163 struct MeshClient *c;
4164 struct MeshTunnel *t;
4165 struct GNUNET_MESH_Unicast *data_msg;
4166 MESH_TunnelNumber tid;
4169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4170 "Got a unicast request from a client!\n");
4172 /* Sanity check for client registration */
4173 if (NULL == (c = client_get (client)))
4176 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4181 data_msg = (struct GNUNET_MESH_Unicast *) message;
4183 /* Sanity check for message size */
4184 size = ntohs (message->size);
4185 if (sizeof (struct GNUNET_MESH_Unicast) +
4186 sizeof (struct GNUNET_MessageHeader) > size)
4189 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4193 /* Tunnel exists? */
4194 tid = ntohl (data_msg->tid);
4195 t = tunnel_get_by_local_id (c, tid);
4199 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4203 /* Is it a local tunnel? Then, does client own the tunnel? */
4204 if (t->owner->handle != client)
4207 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4211 /* PID should be as expected: client<->service communication */
4212 if (ntohl (data_msg->pid) != t->prev_fc.last_pid_recv + 1)
4215 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4216 "Unicast PID, expected %u, got %u\n",
4217 t->prev_fc.last_pid_recv + 1, ntohl (data_msg->pid));
4218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4222 /* Ok, everything is correct, send the message
4223 * (pretend we got it from a mesh peer)
4226 /* Work around const limitation */
4227 char buf[ntohs (message->size)] GNUNET_ALIGN;
4228 struct GNUNET_MESH_Unicast *copy;
4230 copy = (struct GNUNET_MESH_Unicast *) buf;
4231 memcpy (buf, data_msg, size);
4232 copy->oid = my_full_id;
4233 copy->tid = htonl (t->id.tid);
4234 copy->ttl = htonl (default_ttl);
4235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4236 " calling generic handler...\n");
4237 handle_mesh_unicast (NULL, &my_full_id, ©->header);
4239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
4240 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4247 * Handler for client traffic directed to the origin
4249 * @param cls closure
4250 * @param client identification of the client
4251 * @param message the actual message
4254 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4255 const struct GNUNET_MessageHeader *message)
4257 struct GNUNET_MESH_ToOrigin *data_msg;
4258 struct MeshFlowControl *fc;
4259 struct MeshClient *c;
4260 struct MeshTunnel *t;
4261 MESH_TunnelNumber tid;
4264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4265 "Got a ToOrigin request from a client!\n");
4266 /* Sanity check for client registration */
4267 if (NULL == (c = client_get (client)))
4270 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4275 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4277 /* Sanity check for message size */
4278 size = ntohs (message->size);
4279 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4280 sizeof (struct GNUNET_MessageHeader) > size)
4283 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4287 /* Tunnel exists? */
4288 tid = ntohl (data_msg->tid);
4289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4290 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4293 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4296 t = tunnel_get_by_local_id (c, tid);
4299 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4300 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4302 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4306 /* It should be sent by someone who has this as incoming tunnel. */
4310 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4314 /* PID should be as expected */
4316 if (ntohl (data_msg->pid) != fc->last_pid_recv + 1)
4319 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4320 "To Origin PID, expected %u, got %u\n",
4321 fc->last_pid_recv + 1,
4322 ntohl (data_msg->pid));
4323 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4327 /* Ok, everything is correct, send the message
4328 * (pretend we got it from a mesh peer)
4331 char buf[ntohs (message->size)] GNUNET_ALIGN;
4332 struct GNUNET_MESH_ToOrigin *copy;
4334 /* Work around 'const' limitation */
4335 memcpy (buf, data_msg, size);
4336 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4337 GNUNET_PEER_resolve (t->id.oid, ©->oid);
4338 copy->tid = htonl (t->id.tid);
4339 copy->ttl = htonl (default_ttl);
4341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4342 " calling generic handler...\n");
4343 handle_mesh_to_orig (NULL, &my_full_id, ©->header);
4345 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4352 * Handler for client's ACKs for payload traffic.
4354 * @param cls Closure (unused).
4355 * @param client Identification of the client.
4356 * @param message The actual message.
4359 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
4360 const struct GNUNET_MessageHeader *message)
4362 struct GNUNET_MESH_LocalAck *msg;
4363 struct MeshTunnel *t;
4364 struct MeshClient *c;
4365 MESH_TunnelNumber tid;
4368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
4369 /* Sanity check for client registration */
4370 if (NULL == (c = client_get (client)))
4373 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4378 msg = (struct GNUNET_MESH_LocalAck *) message;
4380 /* Tunnel exists? */
4381 tid = ntohl (msg->tunnel_id);
4382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
4383 t = tunnel_get_by_local_id (c, tid);
4387 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
4388 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
4389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4393 ack = ntohl (msg->max_pid);
4394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4396 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
4399 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
4400 t->prev_fc.last_ack_recv = ack;
4401 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4405 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
4406 t->next_fc.last_ack_recv = ack;
4407 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4410 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4418 * Iterator over all tunnels to send a monitoring client info about each tunnel.
4420 * @param cls Closure (client handle).
4421 * @param key Key (hashed tunnel ID, unused).
4422 * @param value Tunnel info.
4424 * @return GNUNET_YES, to keep iterating.
4427 monitor_all_tunnels_iterator (void *cls,
4428 const struct GNUNET_HashCode * key,
4431 struct GNUNET_SERVER_Client *client = cls;
4432 struct MeshTunnel *t = value;
4433 struct GNUNET_MESH_LocalMonitor *msg;
4435 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
4436 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
4437 msg->tunnel_id = htonl (t->id.tid);
4438 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4439 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
4440 GNUNET_PEER_resolve (t->dest, &msg->destination);
4442 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4443 "* sending info about tunnel %s [%u]\n",
4444 GNUNET_i2s (&msg->owner), t->id.tid);
4446 GNUNET_SERVER_notification_context_unicast (nc, client,
4447 &msg->header, GNUNET_NO);
4453 * Handler for client's MONITOR request.
4455 * @param cls Closure (unused).
4456 * @param client Identification of the client.
4457 * @param message The actual message.
4460 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
4461 const struct GNUNET_MessageHeader *message)
4463 struct MeshClient *c;
4465 /* Sanity check for client registration */
4466 if (NULL == (c = client_get (client)))
4469 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4473 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4474 "Received get tunnels request from client %u\n",
4476 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
4477 monitor_all_tunnels_iterator,
4479 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4480 "Get tunnels request from client %u completed\n",
4482 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4487 * Handler for client's MONITOR_TUNNEL request.
4489 * @param cls Closure (unused).
4490 * @param client Identification of the client.
4491 * @param message The actual message.
4494 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
4495 const struct GNUNET_MessageHeader *message)
4497 const struct GNUNET_MESH_LocalMonitor *msg;
4498 struct GNUNET_MESH_LocalMonitor *resp;
4499 struct MeshClient *c;
4500 struct MeshTunnel *t;
4502 /* Sanity check for client registration */
4503 if (NULL == (c = client_get (client)))
4506 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4510 msg = (struct GNUNET_MESH_LocalMonitor *) message;
4511 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4512 "Received tunnel info request from client %u for tunnel %s[%X]\n",
4515 ntohl (msg->tunnel_id));
4516 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
4519 /* We don't know the tunnel FIXME */
4520 struct GNUNET_MESH_LocalMonitor warn;
4523 GNUNET_SERVER_notification_context_unicast (nc, client,
4526 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4530 /* Initialize context */
4531 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
4533 GNUNET_PEER_resolve (t->dest, &resp->destination);
4534 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
4535 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
4536 &resp->header, GNUNET_NO);
4539 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4540 "Monitor tunnel request from client %u completed\n",
4542 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4547 * Functions to handle messages from clients
4549 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4550 {&handle_local_new_client, NULL,
4551 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4552 {&handle_local_tunnel_create, NULL,
4553 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4554 sizeof (struct GNUNET_MESH_TunnelMessage)},
4555 {&handle_local_tunnel_destroy, NULL,
4556 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4557 sizeof (struct GNUNET_MESH_TunnelMessage)},
4558 {&handle_local_tunnel_buffer, NULL,
4559 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
4560 sizeof (struct GNUNET_MESH_TunnelMessage)},
4561 {&handle_local_tunnel_buffer, NULL,
4562 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
4563 sizeof (struct GNUNET_MESH_TunnelMessage)},
4564 {&handle_local_unicast, NULL,
4565 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4566 {&handle_local_to_origin, NULL,
4567 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4568 {&handle_local_ack, NULL,
4569 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
4570 sizeof (struct GNUNET_MESH_LocalAck)},
4571 {&handle_local_get_tunnels, NULL,
4572 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
4573 sizeof (struct GNUNET_MessageHeader)},
4574 {&handle_local_show_tunnel, NULL,
4575 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
4576 sizeof (struct GNUNET_MESH_LocalMonitor)},
4582 * Method called whenever a given peer connects.
4584 * @param cls closure
4585 * @param peer peer identity this notification is about
4588 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
4590 struct MeshPeerInfo *peer_info;
4591 struct MeshPeerPath *path;
4593 DEBUG_CONN ("Peer connected\n");
4594 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4595 peer_info = peer_get (peer);
4596 if (myid == peer_info->id)
4598 DEBUG_CONN (" (self)\n");
4603 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4605 path = path_new (2);
4606 path->peers[0] = myid;
4607 path->peers[1] = peer_info->id;
4608 GNUNET_PEER_change_rc (myid, 1);
4609 GNUNET_PEER_change_rc (peer_info->id, 1);
4610 peer_info_add_path (peer_info, path, GNUNET_YES);
4611 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
4617 * Method called whenever a peer disconnects.
4619 * @param cls closure
4620 * @param peer peer identity this notification is about
4623 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4625 struct MeshPeerInfo *pi;
4626 struct MeshPeerQueue *q;
4627 struct MeshPeerQueue *n;
4629 DEBUG_CONN ("Peer disconnected\n");
4630 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4640 /* TODO try to reroute this traffic instead */
4641 queue_destroy(q, GNUNET_YES);
4644 if (NULL != pi->core_transmit)
4646 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
4647 pi->core_transmit = NULL;
4649 peer_remove_path (pi, pi->id, myid);
4652 DEBUG_CONN (" (self)\n");
4654 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
4660 * Install server (service) handlers and start listening to clients.
4665 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4666 GNUNET_SERVER_disconnect_notify (server_handle,
4667 &handle_local_client_disconnect, NULL);
4668 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
4670 clients_head = NULL;
4671 clients_tail = NULL;
4673 GNUNET_SERVER_resume (server_handle);
4678 * To be called on core init/fail.
4680 * @param cls Closure (config)
4681 * @param server handle to the server for this service
4682 * @param identity the public identity of this peer
4685 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4686 const struct GNUNET_PeerIdentity *identity)
4688 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4692 GNUNET_break (core_handle == server);
4693 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4696 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4697 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4699 GNUNET_i2s (identity));
4700 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4702 GNUNET_i2s (&my_full_id));
4703 GNUNET_CORE_disconnect (core_handle);
4704 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4705 NULL, /* Closure passed to MESH functions */
4706 &core_init, /* Call core_init once connected */
4707 &core_connect, /* Handle connects */
4708 &core_disconnect, /* remove peers on disconnects */
4709 NULL, /* Don't notify about all incoming messages */
4710 GNUNET_NO, /* For header only in notification */
4711 NULL, /* Don't notify about all outbound messages */
4712 GNUNET_NO, /* For header-only out notification */
4713 core_handlers); /* Register these handlers */
4722 /******************************************************************************/
4723 /************************ MAIN FUNCTIONS ****************************/
4724 /******************************************************************************/
4727 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4729 * @param cls closure
4730 * @param key current key code
4731 * @param value value in the hash map
4732 * @return GNUNET_YES if we should continue to iterate,
4736 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
4738 struct MeshTunnel *t = value;
4745 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4747 * @param cls closure
4748 * @param key current key code
4749 * @param value value in the hash map
4750 * @return GNUNET_YES if we should continue to iterate,
4754 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
4756 struct MeshPeerInfo *p = value;
4757 struct MeshPeerQueue *q;
4758 struct MeshPeerQueue *n;
4766 queue_destroy(q, GNUNET_YES);
4770 peer_info_destroy (p);
4776 * Task run during shutdown.
4782 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4786 if (core_handle != NULL)
4788 GNUNET_CORE_disconnect (core_handle);
4793 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
4796 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4797 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4798 if (dht_handle != NULL)
4800 GNUNET_DHT_disconnect (dht_handle);
4805 GNUNET_SERVER_notification_context_destroy (nc);
4808 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4810 GNUNET_SCHEDULER_cancel (announce_id_task);
4811 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4818 * Callback for hostkey read/generation.
4820 * @param cls Closure (Configuration handle).
4821 * @param pk The ECC private key.
4822 * @param emsg Error message, if any.
4825 key_generation_cb (void *cls,
4826 struct GNUNET_CRYPTO_EccPrivateKey *pk,
4829 const struct GNUNET_CONFIGURATION_Handle *c = cls;
4830 struct MeshPeerInfo *peer;
4831 struct MeshPeerPath *p;
4836 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4837 _("Could not access hostkey: %s. Exiting.\n"),
4839 GNUNET_SCHEDULER_shutdown ();
4842 my_private_key = pk;
4843 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
4844 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4845 &my_full_id.hashPubKey);
4846 myid = GNUNET_PEER_intern (&my_full_id);
4847 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4848 "Mesh for peer [%s] starting\n",
4849 GNUNET_i2s(&my_full_id));
4851 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4852 NULL, /* Closure passed to MESH functions */
4853 &core_init, /* Call core_init once connected */
4854 &core_connect, /* Handle connects */
4855 &core_disconnect, /* remove peers on disconnects */
4856 NULL, /* Don't notify about all incoming messages */
4857 GNUNET_NO, /* For header only in notification */
4858 NULL, /* Don't notify about all outbound messages */
4859 GNUNET_NO, /* For header-only out notification */
4860 core_handlers); /* Register these handlers */
4861 if (core_handle == NULL)
4864 GNUNET_SCHEDULER_shutdown ();
4869 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4871 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4873 /* Create a peer_info for the local peer */
4874 peer = peer_get (&my_full_id);
4877 GNUNET_PEER_change_rc (myid, 1);
4878 peer_info_add_path (peer, p, GNUNET_YES);
4880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
4885 * Process mesh requests.
4887 * @param cls closure
4888 * @param server the initialized server
4889 * @param c configuration to use
4892 run (void *cls, struct GNUNET_SERVER_Handle *server,
4893 const struct GNUNET_CONFIGURATION_Handle *c)
4897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4898 server_handle = server;
4899 GNUNET_SERVER_suspend (server_handle);
4902 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
4905 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4907 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4908 "mesh", "peer/privatekey");
4909 GNUNET_SCHEDULER_shutdown ();
4914 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
4915 &refresh_path_time))
4917 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4919 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4920 "mesh", "refresh path time");
4921 GNUNET_SCHEDULER_shutdown ();
4926 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
4929 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4931 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4932 "mesh", "id announce time");
4933 GNUNET_SCHEDULER_shutdown ();
4938 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
4941 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4943 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4944 "mesh", "connect timeout");
4945 GNUNET_SCHEDULER_shutdown ();
4950 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
4953 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4955 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4956 "mesh", "max msgs queue");
4957 GNUNET_SCHEDULER_shutdown ();
4962 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
4965 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4967 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
4968 "mesh", "max tunnels");
4969 GNUNET_SCHEDULER_shutdown ();
4974 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
4977 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4979 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4980 "mesh", "default ttl", 64);
4985 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
4988 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4989 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
4990 "mesh", "max peers", 1000);
4995 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
4996 &dht_replication_level))
4998 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5000 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
5001 "mesh", "dht replication level", 3);
5002 dht_replication_level = 3;
5005 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5006 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5007 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5008 ports = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5010 dht_handle = GNUNET_DHT_connect (c, 64);
5011 if (NULL == dht_handle)
5015 stats = GNUNET_STATISTICS_create ("mesh", c);
5017 /* Scheduled the task to clean up when shutdown is called */
5018 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
5020 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
5023 GNUNET_free (keyfile);
5028 * The main function for the mesh service.
5030 * @param argc number of arguments from the command line
5031 * @param argv command line arguments
5032 * @return 0 ok, 1 on error
5035 main (int argc, char *const *argv)
5040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
5041 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
5043 ret = (GNUNET_OK == r) ? 0 : 1;
5044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
5048 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5049 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5050 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);