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 "mesh_protocol_enc.h"
26 #include "gnunet-service-mesh_tunnel.h"
27 #include "gnunet-service-mesh_connection.h"
28 #include "gnunet-service-mesh_channel.h"
29 #include "mesh_path.h"
31 #define LOG(level, ...) GNUNET_log_from(level,"mesh-tun",__VA_ARGS__)
34 * All the states a tunnel can be in.
39 * Uninitialized status, should never appear in operation.
44 * Path to the peer not known yet
46 MESH_TUNNEL_SEARCHING,
49 * Request sent, not yet answered.
54 * Peer connected and ready to accept data
59 * Peer connected previosly but not responding
61 MESH_TUNNEL_RECONNECTING
66 /******************************************************************************/
67 /******************************** STRUCTS **********************************/
68 /******************************************************************************/
71 * Struct containing all information regarding a tunnel to a peer.
76 * Endpoint of the tunnel.
78 struct MeshPeer *peer;
81 * State of the tunnel.
83 enum MeshTunnelState state;
86 * Local peer ephemeral private key
88 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
91 * Local peer ephemeral public key
93 struct GNUNET_CRYPTO_EccPublicSignKey *my_eph;
96 * Remote peer's public key.
98 struct GNUNET_CRYPTO_EccPublicSignKey *peers_eph;
101 * Encryption ("our") key.
103 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
106 * Decryption ("their") key.
108 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
111 * Paths that are actively used to reach the destination peer.
113 struct MeshConnection *connection_head;
114 struct MeshConnection *connection_tail;
117 * Next connection number.
122 * Channels inside this tunnel.
124 struct MeshChannel *channel_head;
125 struct MeshChannel *channel_tail;
128 * Channel ID for the next created channel.
130 MESH_ChannelNumber next_chid;
133 * Channel ID for the next incoming channel.
135 MESH_ChannelNumber next_local_chid;
138 * Pending message count.
140 int pending_messages;
143 * Destroy flag: if true, destroy on last message.
148 * Queued messages, to transmit once tunnel gets connected.
150 struct MeshTunnelQueue *tq_head;
151 struct MeshTunnelQueue *tq_tail;
156 * Struct used to queue messages in a tunnel.
158 struct MeshTunnelQueue
163 struct MeshTunnelQueue *next;
164 struct MeshTunnelQueue *prev;
169 struct MeshChannel *ch;
174 /* struct GNUNET_MessageHeader *msg; */
177 /******************************************************************************/
178 /******************************* GLOBALS ***********************************/
179 /******************************************************************************/
182 * Default TTL for payload packets.
184 static unsigned long long default_ttl;
187 * Local peer own ID (memory efficient handle).
189 static GNUNET_PEER_Id my_short_id;
192 * Local peer own ID (full value).
194 const static struct GNUNET_PeerIdentity *my_full_id;
199 const static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
202 /******************************************************************************/
203 /******************************** STATIC ***********************************/
204 /******************************************************************************/
208 * Get string description for tunnel state.
210 * @param s Tunnel state.
212 * @return String representation.
215 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
217 static char buf[128];
221 case MESH_TUNNEL_NEW:
222 return "MESH_TUNNEL_NEW";
223 case MESH_TUNNEL_SEARCHING:
224 return "MESH_TUNNEL_SEARCHING";
225 case MESH_TUNNEL_WAITING:
226 return "MESH_TUNNEL_WAITING";
227 case MESH_TUNNEL_READY:
228 return "MESH_TUNNEL_READY";
229 case MESH_TUNNEL_RECONNECTING:
230 return "MESH_TUNNEL_RECONNECTING";
233 sprintf (buf, "%u (UNKNOWN STATE)", s);
240 * Pick a connection on which send the next data message.
242 * @param t Tunnel on which to send the message.
243 * @param fwd Is this a fwd message?
245 * @return The connection on which to send the next message.
247 static struct MeshConnection *
248 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
250 struct MeshConnection *c;
251 struct MeshConnection *best;
252 struct MeshFlowControl *fc;
253 unsigned int lowest_q;
255 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n",
259 for (c = t->connection_head; NULL != c; c = c->next)
261 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
262 GNUNET_h2s (&c->id), c->state);
263 if (MESH_CONNECTION_READY == c->state)
265 fc = fwd ? &c->fwd_fc : &c->bck_fc;
271 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", fc->queue_n);
272 if (fc->queue_n < lowest_q)
275 lowest_q = fc->queue_n;
284 * Get the total buffer space for a tunnel.
287 * @param fwd Is this for FWD traffic?
289 * @return Buffer space offered by all connections in the tunnel.
292 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
294 struct MeshConnection *c;
295 struct MeshFlowControl *fc;
298 c = t->connection_head;
301 /* If terminal, return biggest channel buffer */
302 if (NULL == c || GMC_is_terminal (c, fwd))
304 struct MeshChannel *ch;
307 if (NULL == t->channel_head)
310 for (ch = t->channel_head; NULL != ch; ch = ch->next)
312 ch_buf = GMCH_get_buffer (ch, fwd);
319 /* If not terminal, return sum of connection buffers */
322 if (c->state != MESH_CONNECTION_READY)
328 fc = fwd ? &c->fwd_fc : &c->bck_fc;
329 buffer += fc->queue_max - fc->queue_n;
338 * Send all cached messages that we can, tunnel is online.
340 * @param t Tunnel that holds the messages.
341 * @param fwd Is this fwd?
344 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
346 struct MeshTunnelQueue *tq;
347 struct MeshTunnelQueue *next;
350 LOG (GNUNET_ERROR_TYPE_DEBUG,
351 "tunnel_send_queued_data on tunnel %s\n",
353 room = tunnel_get_buffer (t, fwd);
354 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
355 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
359 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
360 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
369 * Cache a message to be sent once tunnel is online.
371 * @param t Tunnel to hold the message.
372 * @param ch Channel the message is about.
373 * @param msg Message itself (copy will be made).
374 * @param fwd Is this fwd?
377 GMT_queue_data (struct MeshTunnel2 *t,
378 struct MeshChannel *ch,
379 struct GNUNET_MessageHeader *msg,
382 struct MeshTunnelQueue *tq;
383 uint16_t size = ntohs (msg->size);
385 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
388 memcpy (&tq[1], msg, size);
389 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
391 if (MESH_TUNNEL_READY == t->state)
392 tunnel_send_queued_data (t, fwd);
397 /******************************************************************************/
398 /******************************** API ***********************************/
399 /******************************************************************************/
402 * Initialize the tunnel subsystem.
404 * @param c Configuration handle.
405 * @param id Peer identity.
406 * @param key ECC private key, to derive all other keys and do crypto.
409 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
410 const struct GNUNET_PeerIdentity *id,
411 const struct GNUNET_CRYPTO_EccPrivateKey *key)
414 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
417 LOG_config_invalid (GNUNET_ERROR_TYPE_WARNING,
418 "MESH", "DEFAULT_TTL", "USING DEFAULT");
422 my_private_key = key;
423 my_short_id = GNUNET_PEER_intern (my_full_id);
428 * Shut down the tunnel subsystem.
433 GNUNET_PEER_change_rc (my_short_id, -1);
443 struct MeshTunnel2 *t;
445 t = GNUNET_new (struct MeshTunnel2);
447 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
449 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
450 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
453 // tunnel_destroy (t);
457 // char salt[] = "salt";
458 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
459 // salt, sizeof (salt),
460 // &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
461 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
462 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
464 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
465 // salt, sizeof (salt),
466 // &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
467 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
468 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
477 * Change the tunnel state.
479 * @param t Tunnel whose state to change.
480 * @param state New state.
483 GMT_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
487 LOG (GNUNET_ERROR_TYPE_DEBUG,
488 "Tunnel %s state was %s\n",
490 GNUNET_MESH_DEBUG_TS2S (t->state));
491 LOG (GNUNET_ERROR_TYPE_DEBUG,
492 "Tunnel %s state is now %s\n",
494 GNUNET_MESH_DEBUG_TS2S (state));
500 * Add a connection to a tunnel.
503 * @param c Connection.
506 GMT_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
508 struct MeshConnection *aux;
510 for (aux = t->connection_head; aux != NULL; aux = aux->next)
513 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
520 * Tunnel is empty: destroy it.
522 * Notifies all connections about the destruction.
524 * @param t Tunnel to destroy.
527 GMT_destroy_empty (struct MeshTunnel2 *t)
529 struct MeshConnection *c;
531 for (c = t->connection_head; NULL != c; c = c->next)
533 if (GNUNET_NO == c->destroy)
534 GMC_send_destroy (c);
537 if (0 == t->pending_messages)
540 t->destroy = GNUNET_YES;
545 * Destroy tunnel if empty (no more channels).
547 * @param t Tunnel to destroy if empty.
550 GMT_destroy_if_empty (struct MeshTunnel2 *t)
552 if (1 <= GMCH_count (t->channel_head))
555 GMT_destroy_empty (t);
561 * Destroy the tunnel.
563 * This function does not generate any warning traffic to clients or peers.
566 * Cancel messages belonging to this tunnel queued to neighbors.
567 * Free any allocated resources linked to the tunnel.
569 * @param t The tunnel to destroy.
572 GMT_destroy (struct MeshTunnel2 *t)
574 struct MeshConnection *c;
575 struct MeshConnection *next;
580 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
583 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
586 for (c = t->connection_head; NULL != c; c = next)
592 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
593 t->peer->tunnel = NULL;
599 * Demultiplex by message type and call appropriate handler for a message
600 * towards a channel of a local tunnel.
602 * @param t Tunnel this message came on.
603 * @param msgh Message header.
604 * @param fwd Is this message fwd?
607 GMT_handle_decrypted (struct MeshTunnel2 *t,
608 const struct GNUNET_MessageHeader *msgh,
611 switch (ntohs (msgh->type))
613 case GNUNET_MESSAGE_TYPE_MESH_DATA:
614 /* Don't send hop ACK, wait for client to ACK */
615 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
618 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
619 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
622 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
623 handle_channel_create (t,
624 (struct GNUNET_MESH_ChannelCreate *) msgh,
628 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
629 handle_channel_ack (t,
630 (struct GNUNET_MESH_ChannelManage *) msgh,
634 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
635 handle_channel_destroy (t,
636 (struct GNUNET_MESH_ChannelManage *) msgh,
641 LOG (GNUNET_ERROR_TYPE_DEBUG,
642 "end-to-end message not known (%u)\n",
649 * Notifies a tunnel that a connection has broken that affects at least
650 * some of its peers. Sends a notification towards the root of the tree.
651 * In case the peer is the owner of the tree, notifies the client that owns
652 * the tunnel and tries to reconnect.
654 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
656 * @param t Tunnel affected.
657 * @param p1 Peer that got disconnected from p2.
658 * @param p2 Peer that got disconnected from p1.
660 * @return Short ID of the peer disconnected (either p1 or p2).
661 * 0 if the tunnel remained unaffected.
664 GMT_notify_connection_broken (struct MeshTunnel2* t,
665 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
667 // if (myid != p1 && myid != p2) FIXME
672 // if (tree_get_predecessor (t->tree) != 0)
674 // /* We are the peer still connected, notify owner of the disconnection. */
675 // struct GNUNET_MESH_PathBroken msg;
676 // struct GNUNET_PeerIdentity neighbor;
678 // msg.header.size = htons (sizeof (msg));
679 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
680 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
681 // msg.tid = htonl (t->id.tid);
682 // msg.peer1 = my_full_id;
683 // GNUNET_PEER_resolve (pid, &msg.peer2);
684 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
685 // send_prebuilt_message (&msg.header, &neighbor, t);
691 * @brief Use the given path for the tunnel.
692 * Update the next and prev hops (and RCs).
693 * (Re)start the path refresh in case the tunnel is locally owned.
695 * @param t Tunnel to update.
696 * @param p Path to use.
698 * @return Connection created.
700 struct MeshConnection *
701 GMT_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
703 struct MeshConnection *c;
704 struct GNUNET_HashCode cid;
705 struct MeshPeer *peer;
706 unsigned int own_pos;
708 if (NULL == t || NULL == p)
714 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
718 GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, c);
719 for (own_pos = 0; own_pos < p->length; own_pos++)
721 if (p->peers[own_pos] == myid)
724 if (own_pos > p->length - 1)
727 connection_destroy (c);
730 c->own_pos = own_pos;
735 c->fwd_maintenance_task =
736 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
737 &connection_fwd_keepalive, c);
740 peer = connection_get_next_hop (c);
741 if (NULL == peer->connections)
743 connection_destroy (c);
746 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
747 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
748 peer = connection_get_prev_hop (c);
749 if (NULL == peer->connections)
751 connection_destroy (c);
754 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
755 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
761 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
762 * Encrypt data with the tunnel key.
764 * @param t Tunnel whose key to use.
765 * @param dst Destination for the encrypted data.
766 * @param src Source of the plaintext.
767 * @param size Size of the plaintext.
768 * @param iv Initialization Vector to use.
769 * @param fwd Is this a fwd message?
772 GMT_encrypt (struct MeshTunnel2 *t,
773 void *dst, const void *src,
774 size_t size, uint64_t iv, int fwd)
776 memcpy (dst, src, size);
781 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
782 * Decrypt data with the tunnel key.
784 * @param t Tunnel whose key to use.
785 * @param dst Destination for the plaintext.
786 * @param src Source of the encrypted data.
787 * @param size Size of the encrypted data.
788 * @param iv Initialization Vector to use.
789 * @param fwd Is this a fwd message?
792 GMT_decrypt (struct MeshTunnel2 *t,
793 void *dst, const void *src,
794 size_t size, uint64_t iv, int fwd)
796 memcpy (dst, src, size);
801 * Count established (ready) connections of a tunnel.
803 * @param t Tunnel on which to send the message.
805 * @return Number of connections.
808 GMT_count_connections (struct MeshTunnel2 *t)
810 return GMC_count (t->connection_head);
815 * Sends an already built message on a tunnel, choosing the best connection.
817 * @param message Message to send. Function modifies it.
818 * @param t Tunnel on which this message is transmitted.
819 * @param ch Channel on which this message is transmitted.
820 * @param fwd Is this a fwd message?
823 GMT_send_prebuilt_message (struct GNUNET_MESH_Encrypted *msg,
824 struct MeshTunnel2 *t,
825 struct MeshChannel *ch,
828 struct MeshConnection *c;
831 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
833 c = tunnel_get_connection (t, fwd);
836 GNUNET_break (GNUNET_YES == t->destroy);
839 type = ntohs (msg->header.type);
842 case GNUNET_MESSAGE_TYPE_MESH_FWD:
843 case GNUNET_MESSAGE_TYPE_MESH_BCK:
844 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
845 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
847 msg->ttl = htonl (default_ttl);
850 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
851 GNUNET_MESH_DEBUG_M2S (type));
856 GMC_send_prebuilt_message (&msg->header, c, ch, fwd);