2 This file is part of GNUnet.
3 (C) 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 #include "gnunet_util_lib.h"
24 #include "gnunet-service-mesh_tunnel.h"
25 #include "gnunet-service-mesh_connection.h"
26 #include "gnunet-service-mesh_channel.h"
27 #include "mesh_path.h"
31 * All the states a tunnel can be in.
36 * Uninitialized status, should never appear in operation.
41 * Path to the peer not known yet
43 MESH_TUNNEL_SEARCHING,
46 * Request sent, not yet answered.
51 * Peer connected and ready to accept data
56 * Peer connected previosly but not responding
58 MESH_TUNNEL_RECONNECTING
63 /******************************************************************************/
64 /******************************** STRUCTS **********************************/
65 /******************************************************************************/
68 * Struct containing all information regarding a tunnel to a peer.
73 * Endpoint of the tunnel.
75 struct MeshPeer *peer;
78 * State of the tunnel.
80 enum MeshTunnelState state;
83 * Local peer ephemeral private key
85 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
88 * Local peer ephemeral public key
90 struct GNUNET_CRYPTO_EccPublicSignKey *my_eph;
93 * Remote peer's public key.
95 struct GNUNET_CRYPTO_EccPublicSignKey *peers_eph;
98 * Encryption ("our") key.
100 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
103 * Decryption ("their") key.
105 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
108 * Paths that are actively used to reach the destination peer.
110 struct MeshConnection *connection_head;
111 struct MeshConnection *connection_tail;
114 * Next connection number.
119 * Channels inside this tunnel.
121 struct MeshChannel *channel_head;
122 struct MeshChannel *channel_tail;
125 * Channel ID for the next created channel.
127 MESH_ChannelNumber next_chid;
130 * Channel ID for the next incoming channel.
132 MESH_ChannelNumber next_local_chid;
135 * Pending message count.
137 int pending_messages;
140 * Destroy flag: if true, destroy on last message.
145 * Queued messages, to transmit once tunnel gets connected.
147 struct MeshTunnelQueue *tq_head;
148 struct MeshTunnelQueue *tq_tail;
153 * Struct used to queue messages in a tunnel.
155 struct MeshTunnelQueue
160 struct MeshTunnelQueue *next;
161 struct MeshTunnelQueue *prev;
166 struct MeshChannel *ch;
171 /* struct GNUNET_MessageHeader *msg; */
174 /******************************************************************************/
175 /******************************* GLOBALS ***********************************/
176 /******************************************************************************/
179 * Default TTL for payload packets.
181 static unsigned long long default_ttl;
184 * Local peer own ID (memory efficient handle).
186 static GNUNET_PEER_Id my_short_id;
189 * Local peer own ID (full value).
191 const static struct GNUNET_PeerIdentity *my_full_id;
196 const static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
199 /******************************************************************************/
200 /******************************** STATIC ***********************************/
201 /******************************************************************************/
205 * Get string description for tunnel state.
207 * @param s Tunnel state.
209 * @return String representation.
212 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
214 static char buf[128];
218 case MESH_TUNNEL_NEW:
219 return "MESH_TUNNEL_NEW";
220 case MESH_TUNNEL_SEARCHING:
221 return "MESH_TUNNEL_SEARCHING";
222 case MESH_TUNNEL_WAITING:
223 return "MESH_TUNNEL_WAITING";
224 case MESH_TUNNEL_READY:
225 return "MESH_TUNNEL_READY";
226 case MESH_TUNNEL_RECONNECTING:
227 return "MESH_TUNNEL_RECONNECTING";
230 sprintf (buf, "%u (UNKNOWN STATE)", s);
237 * Pick a connection on which send the next data message.
239 * @param t Tunnel on which to send the message.
240 * @param fwd Is this a fwd message?
242 * @return The connection on which to send the next message.
244 static struct MeshConnection *
245 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
247 struct MeshConnection *c;
248 struct MeshConnection *best;
249 struct MeshFlowControl *fc;
250 unsigned int lowest_q;
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n",
256 for (c = t->connection_head; NULL != c; c = c->next)
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
259 GNUNET_h2s (&c->id), c->state);
260 if (MESH_CONNECTION_READY == c->state)
262 fc = fwd ? &c->fwd_fc : &c->bck_fc;
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", fc->queue_n);
269 if (fc->queue_n < lowest_q)
272 lowest_q = fc->queue_n;
281 * Get the total buffer space for a tunnel.
284 * @param fwd Is this for FWD traffic?
286 * @return Buffer space offered by all connections in the tunnel.
289 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
291 struct MeshConnection *c;
292 struct MeshFlowControl *fc;
295 c = t->connection_head;
298 /* If terminal, return biggest channel buffer */
299 if (NULL == c || GMC_is_terminal (c, fwd))
301 struct MeshChannel *ch;
304 if (NULL == t->channel_head)
307 for (ch = t->channel_head; NULL != ch; ch = ch->next)
309 ch_buf = GMCH_get_buffer (ch, fwd);
316 /* If not terminal, return sum of connection buffers */
319 if (c->state != MESH_CONNECTION_READY)
325 fc = fwd ? &c->fwd_fc : &c->bck_fc;
326 buffer += fc->queue_max - fc->queue_n;
335 * Send all cached messages that we can, tunnel is online.
337 * @param t Tunnel that holds the messages.
338 * @param fwd Is this fwd?
341 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
343 struct MeshTunnelQueue *tq;
344 struct MeshTunnelQueue *next;
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "tunnel_send_queued_data on tunnel %s\n",
350 room = tunnel_get_buffer (t, fwd);
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
352 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
356 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
357 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
366 * Cache a message to be sent once tunnel is online.
368 * @param t Tunnel to hold the message.
369 * @param ch Channel the message is about.
370 * @param msg Message itself (copy will be made).
371 * @param fwd Is this fwd?
374 tunnel_queue_data (struct MeshTunnel2 *t,
375 struct MeshChannel *ch,
376 struct GNUNET_MessageHeader *msg,
379 struct MeshTunnelQueue *tq;
380 uint16_t size = ntohs (msg->size);
382 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
385 memcpy (&tq[1], msg, size);
386 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
388 if (MESH_TUNNEL_READY == t->state)
389 tunnel_send_queued_data (t, fwd);
394 /******************************************************************************/
395 /******************************** API ***********************************/
396 /******************************************************************************/
399 * Initialize the tunnel subsystem.
401 * @param c Configuration handle.
402 * @param id Peer identity.
403 * @param key ECC private key, to derive all other keys and do crypto.
406 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
407 const struct GNUNET_PeerIdentity *id,
408 const struct GNUNET_CRYPTO_EccPrivateKey *key)
411 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
414 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
415 "MESH", "DEFAULT_TTL", "USING DEFAULT");
419 my_private_key = key;
420 my_short_id = GNUNET_PEER_intern (my_full_id);
425 * Shut down the tunnel subsystem.
430 GNUNET_PEER_change_rc (my_short_id, -1);
440 struct MeshTunnel2 *t;
442 t = GNUNET_new (struct MeshTunnel2);
444 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
446 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
447 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
450 // tunnel_destroy (t);
454 // char salt[] = "salt";
455 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
456 // salt, sizeof (salt),
457 // &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
458 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
459 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
461 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
462 // salt, sizeof (salt),
463 // &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
464 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
465 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
474 * Change the tunnel state.
476 * @param t Tunnel whose state to change.
477 * @param state New state.
480 tunnel_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 "Tunnel %s state was %s\n",
487 GNUNET_MESH_DEBUG_TS2S (t->state));
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "Tunnel %s state is now %s\n",
491 GNUNET_MESH_DEBUG_TS2S (state));
497 * Add a connection to a tunnel.
500 * @param c Connection.
503 GMT_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
505 struct MeshConnection *aux;
507 for (aux = t->connection_head; aux != NULL; aux = aux->next)
510 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
517 * Tunnel is empty: destroy it.
519 * Notifies all connections about the destruction.
521 * @param t Tunnel to destroy.
524 GMT_destroy_empty (struct MeshTunnel2 *t)
526 struct MeshConnection *c;
528 for (c = t->connection_head; NULL != c; c = c->next)
530 if (GNUNET_NO == c->destroy)
531 GMC_send_destroy (c);
534 if (0 == t->pending_messages)
537 t->destroy = GNUNET_YES;
542 * Destroy tunnel if empty (no more channels).
544 * @param t Tunnel to destroy if empty.
547 GMT_destroy_if_empty (struct MeshTunnel2 *t)
549 if (1 <= GMCH_count (t->channel_head))
552 GMT_destroy_empty (t);
558 * Destroy the tunnel.
560 * This function does not generate any warning traffic to clients or peers.
563 * Cancel messages belonging to this tunnel queued to neighbors.
564 * Free any allocated resources linked to the tunnel.
566 * @param t The tunnel to destroy.
569 GMT_destroy (struct MeshTunnel2 *t)
571 struct MeshConnection *c;
572 struct MeshConnection *next;
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
580 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
583 for (c = t->connection_head; NULL != c; c = next)
589 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
590 t->peer->tunnel = NULL;
596 * Demultiplex by message type and call appropriate handler for a message
597 * towards a channel of a local tunnel.
599 * @param t Tunnel this message came on.
600 * @param msgh Message header.
601 * @param fwd Is this message fwd?
604 GMT_handle_decrypted (struct MeshTunnel2 *t,
605 const struct GNUNET_MessageHeader *msgh,
608 switch (ntohs (msgh->type))
610 case GNUNET_MESSAGE_TYPE_MESH_DATA:
611 /* Don't send hop ACK, wait for client to ACK */
612 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
615 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
616 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
619 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
620 handle_channel_create (t,
621 (struct GNUNET_MESH_ChannelCreate *) msgh,
625 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
626 handle_channel_ack (t,
627 (struct GNUNET_MESH_ChannelManage *) msgh,
631 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
632 handle_channel_destroy (t,
633 (struct GNUNET_MESH_ChannelManage *) msgh,
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639 "end-to-end message not known (%u)\n",
646 * Notifies a tunnel that a connection has broken that affects at least
647 * some of its peers. Sends a notification towards the root of the tree.
648 * In case the peer is the owner of the tree, notifies the client that owns
649 * the tunnel and tries to reconnect.
651 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
653 * @param t Tunnel affected.
654 * @param p1 Peer that got disconnected from p2.
655 * @param p2 Peer that got disconnected from p1.
657 * @return Short ID of the peer disconnected (either p1 or p2).
658 * 0 if the tunnel remained unaffected.
660 static GNUNET_PEER_Id
661 GMT_notify_connection_broken (struct MeshTunnel2* t,
662 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
664 // if (myid != p1 && myid != p2) FIXME
669 // if (tree_get_predecessor (t->tree) != 0)
671 // /* We are the peer still connected, notify owner of the disconnection. */
672 // struct GNUNET_MESH_PathBroken msg;
673 // struct GNUNET_PeerIdentity neighbor;
675 // msg.header.size = htons (sizeof (msg));
676 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
677 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
678 // msg.tid = htonl (t->id.tid);
679 // msg.peer1 = my_full_id;
680 // GNUNET_PEER_resolve (pid, &msg.peer2);
681 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
682 // send_prebuilt_message (&msg.header, &neighbor, t);
688 * @brief Use the given path for the tunnel.
689 * Update the next and prev hops (and RCs).
690 * (Re)start the path refresh in case the tunnel is locally owned.
692 * @param t Tunnel to update.
693 * @param p Path to use.
695 * @return Connection created.
697 struct MeshConnection *
698 GMT_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
700 struct MeshConnection *c;
701 struct GNUNET_HashCode cid;
702 struct MeshPeer *peer;
703 unsigned int own_pos;
705 if (NULL == t || NULL == p)
711 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
715 GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, c);
716 for (own_pos = 0; own_pos < p->length; own_pos++)
718 if (p->peers[own_pos] == myid)
721 if (own_pos > p->length - 1)
724 connection_destroy (c);
727 c->own_pos = own_pos;
732 c->fwd_maintenance_task =
733 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
734 &connection_fwd_keepalive, c);
737 peer = connection_get_next_hop (c);
738 if (NULL == peer->connections)
740 connection_destroy (c);
743 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
744 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
745 peer = connection_get_prev_hop (c);
746 if (NULL == peer->connections)
748 connection_destroy (c);
751 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
752 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
758 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
759 * Encrypt data with the tunnel key.
761 * @param t Tunnel whose key to use.
762 * @param dst Destination for the encrypted data.
763 * @param src Source of the plaintext.
764 * @param size Size of the plaintext.
765 * @param iv Initialization Vector to use.
766 * @param fwd Is this a fwd message?
769 GMT_encrypt (struct MeshTunnel2 *t,
770 void *dst, const void *src,
771 size_t size, uint64_t iv, int fwd)
773 memcpy (dst, src, size);
778 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
779 * Decrypt data with the tunnel key.
781 * @param t Tunnel whose key to use.
782 * @param dst Destination for the plaintext.
783 * @param src Source of the encrypted data.
784 * @param size Size of the encrypted data.
785 * @param iv Initialization Vector to use.
786 * @param fwd Is this a fwd message?
789 GMT_decrypt (struct MeshTunnel2 *t,
790 void *dst, const void *src,
791 size_t size, uint64_t iv, int fwd)
793 memcpy (dst, src, size);
798 * Count established (ready) connections of a tunnel.
800 * @param t Tunnel on which to send the message.
802 * @return Number of connections.
805 GMT_count_connections (struct MeshTunnel2 *t)
807 return GMC_count (t->connection_head);
812 * Sends an already built message on a tunnel, choosing the best connection.
814 * @param message Message to send. Function modifies it.
815 * @param t Tunnel on which this message is transmitted.
816 * @param ch Channel on which this message is transmitted.
817 * @param fwd Is this a fwd message?
820 GMT_send_prebuilt_message (struct GNUNET_MESH_Encrypted *msg,
821 struct MeshTunnel2 *t,
822 struct MeshChannel *ch,
825 struct MeshConnection *c;
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
830 c = tunnel_get_connection (t, fwd);
833 GNUNET_break (GNUNET_YES == t->destroy);
836 type = ntohs (msg->header.type);
839 case GNUNET_MESSAGE_TYPE_MESH_FWD:
840 case GNUNET_MESSAGE_TYPE_MESH_BCK:
841 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
842 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
844 msg->ttl = htonl (default_ttl);
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
848 GNUNET_MESH_DEBUG_M2S (type));
853 send_prebuilt_message_connection (&msg->header, c, ch, fwd);