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_statistics_service.h"
26 #include "mesh_protocol_enc.h"
28 #include "gnunet-service-mesh_tunnel.h"
29 #include "gnunet-service-mesh_connection.h"
30 #include "gnunet-service-mesh_channel.h"
31 #include "gnunet-service-mesh_peer.h"
32 #include "mesh_path.h"
34 #define LOG(level, ...) GNUNET_log_from(level,"mesh-tun",__VA_ARGS__)
37 * All the states a tunnel can be in.
42 * Uninitialized status, should never appear in operation.
47 * Path to the peer not known yet
49 MESH_TUNNEL_SEARCHING,
52 * Request sent, not yet answered.
57 * Peer connected and ready to accept data
62 * Peer connected previosly but not responding
64 MESH_TUNNEL_RECONNECTING
69 /******************************************************************************/
70 /******************************** STRUCTS **********************************/
71 /******************************************************************************/
74 * Struct containing all information regarding a tunnel to a peer.
79 * Endpoint of the tunnel.
81 struct MeshPeer *peer;
84 * State of the tunnel.
86 enum MeshTunnelState state;
89 * Local peer ephemeral private key
91 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
94 * Local peer ephemeral public key
96 struct GNUNET_CRYPTO_EccPublicSignKey *my_eph;
99 * Remote peer's public key.
101 struct GNUNET_CRYPTO_EccPublicSignKey *peers_eph;
104 * Encryption ("our") key.
106 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
109 * Decryption ("their") key.
111 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
114 * Paths that are actively used to reach the destination peer.
116 struct MeshConnection *connection_head;
117 struct MeshConnection *connection_tail;
120 * Next connection number.
125 * Channels inside this tunnel.
127 struct MeshChannel *channel_head;
128 struct MeshChannel *channel_tail;
131 * Channel ID for the next created channel.
133 MESH_ChannelNumber next_chid;
136 * Channel ID for the next incoming channel.
138 MESH_ChannelNumber next_local_chid;
141 * Pending message count.
143 int pending_messages;
146 * Destroy flag: if true, destroy on last message.
151 * Queued messages, to transmit once tunnel gets connected.
153 struct MeshTunnelQueue *tq_head;
154 struct MeshTunnelQueue *tq_tail;
159 * Struct used to queue messages in a tunnel.
161 struct MeshTunnelQueue
166 struct MeshTunnelQueue *next;
167 struct MeshTunnelQueue *prev;
172 struct MeshChannel *ch;
177 /* struct GNUNET_MessageHeader *msg; */
180 /******************************************************************************/
181 /******************************* GLOBALS ***********************************/
182 /******************************************************************************/
185 * Global handle to the statistics service.
187 extern struct GNUNET_STATISTICS_Handle *stats;
190 * Default TTL for payload packets.
192 static unsigned long long default_ttl;
195 * Local peer own ID (memory efficient handle).
197 static GNUNET_PEER_Id my_short_id;
200 * Local peer own ID (full value).
202 const static struct GNUNET_PeerIdentity *my_full_id;
207 const static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
210 /******************************************************************************/
211 /******************************** STATIC ***********************************/
212 /******************************************************************************/
215 * Get string description for tunnel state.
217 * @param s Tunnel state.
219 * @return String representation.
222 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
224 static char buf[128];
228 case MESH_TUNNEL_NEW:
229 return "MESH_TUNNEL_NEW";
230 case MESH_TUNNEL_SEARCHING:
231 return "MESH_TUNNEL_SEARCHING";
232 case MESH_TUNNEL_WAITING:
233 return "MESH_TUNNEL_WAITING";
234 case MESH_TUNNEL_READY:
235 return "MESH_TUNNEL_READY";
236 case MESH_TUNNEL_RECONNECTING:
237 return "MESH_TUNNEL_RECONNECTING";
240 sprintf (buf, "%u (UNKNOWN STATE)", s);
247 * Pick a connection on which send the next data message.
249 * @param t Tunnel on which to send the message.
250 * @param fwd Is this a fwd message?
252 * @return The connection on which to send the next message.
254 static struct MeshConnection *
255 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
257 struct MeshConnection *c;
258 struct MeshConnection *best;
259 struct MeshFlowControl *fc;
260 unsigned int lowest_q;
262 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n",
266 for (c = t->connection_head; NULL != c; c = c->next)
268 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
269 GNUNET_h2s (GMC_get_id (c)), c->state);
270 if (MESH_CONNECTION_READY == c->state)
272 fc = fwd ? &c->fwd_fc : &c->bck_fc;
278 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", fc->queue_n);
279 if (fc->queue_n < lowest_q)
282 lowest_q = fc->queue_n;
291 * Get the total buffer space for a tunnel.
294 * @param fwd Is this for FWD traffic?
296 * @return Buffer space offered by all connections in the tunnel.
299 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
301 struct MeshConnection *c;
302 struct MeshFlowControl *fc;
305 c = t->connection_head;
308 /* If terminal, return biggest channel buffer */
309 if (NULL == c || GMC_is_terminal (c, fwd))
311 struct MeshChannel *ch;
314 if (NULL == t->channel_head)
317 for (ch = t->channel_head; NULL != ch; ch = ch->next)
319 ch_buf = GMCH_get_buffer (ch, fwd);
326 /* If not terminal, return sum of connection buffers */
329 if (c->state != MESH_CONNECTION_READY)
335 fc = fwd ? &c->fwd_fc : &c->bck_fc;
336 buffer += fc->queue_max - fc->queue_n;
345 * Send all cached messages that we can, tunnel is online.
347 * @param t Tunnel that holds the messages.
348 * @param fwd Is this fwd?
351 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
353 struct MeshTunnelQueue *tq;
354 struct MeshTunnelQueue *next;
357 LOG (GNUNET_ERROR_TYPE_DEBUG,
358 "tunnel_send_queued_data on tunnel %s\n",
360 room = tunnel_get_buffer (t, fwd);
361 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
362 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
366 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
367 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
375 /******************************************************************************/
376 /******************************** API ***********************************/
377 /******************************************************************************/
380 * Cache a message to be sent once tunnel is online.
382 * @param t Tunnel to hold the message.
383 * @param ch Channel the message is about.
384 * @param msg Message itself (copy will be made).
385 * @param fwd Is this fwd?
388 GMT_queue_data (struct MeshTunnel2 *t,
389 struct MeshChannel *ch,
390 struct GNUNET_MessageHeader *msg,
393 struct MeshTunnelQueue *tq;
394 uint16_t size = ntohs (msg->size);
396 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
399 memcpy (&tq[1], msg, size);
400 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
402 if (MESH_TUNNEL_READY == t->state)
403 tunnel_send_queued_data (t, fwd);
408 * Initialize the tunnel subsystem.
410 * @param c Configuration handle.
411 * @param id Peer identity.
412 * @param key ECC private key, to derive all other keys and do crypto.
415 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
416 const struct GNUNET_PeerIdentity *id,
417 const struct GNUNET_CRYPTO_EccPrivateKey *key)
420 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
423 LOG_config_invalid (GNUNET_ERROR_TYPE_WARNING,
424 "MESH", "DEFAULT_TTL", "USING DEFAULT");
428 my_private_key = key;
429 my_short_id = GNUNET_PEER_intern (my_full_id);
434 * Shut down the tunnel subsystem.
439 GNUNET_PEER_change_rc (my_short_id, -1);
449 struct MeshTunnel2 *t;
451 t = GNUNET_new (struct MeshTunnel2);
453 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
455 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
456 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
459 // tunnel_destroy (t);
463 // char salt[] = "salt";
464 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
465 // salt, sizeof (salt),
466 // &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
467 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
468 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
470 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
471 // salt, sizeof (salt),
472 // &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
473 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
474 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
483 * Change the tunnel state.
485 * @param t Tunnel whose state to change.
486 * @param state New state.
489 GMT_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
493 LOG (GNUNET_ERROR_TYPE_DEBUG,
494 "Tunnel %s state was %s\n",
496 GNUNET_MESH_DEBUG_TS2S (t->state));
497 LOG (GNUNET_ERROR_TYPE_DEBUG,
498 "Tunnel %s state is now %s\n",
500 GNUNET_MESH_DEBUG_TS2S (state));
506 * Add a connection to a tunnel.
509 * @param c Connection.
512 GMT_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
514 struct MeshConnection *aux;
516 for (aux = t->connection_head; aux != NULL; aux = aux->next)
519 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
526 * Tunnel is empty: destroy it.
528 * Notifies all connections about the destruction.
530 * @param t Tunnel to destroy.
533 GMT_destroy_empty (struct MeshTunnel2 *t)
535 struct MeshConnection *c;
537 for (c = t->connection_head; NULL != c; c = c->next)
539 if (GNUNET_NO == c->destroy)
540 GMC_send_destroy (c);
543 if (0 == t->pending_messages)
546 t->destroy = GNUNET_YES;
551 * Destroy tunnel if empty (no more channels).
553 * @param t Tunnel to destroy if empty.
556 GMT_destroy_if_empty (struct MeshTunnel2 *t)
558 if (1 < GMCH_count (t->channel_head))
561 GMT_destroy_empty (t);
567 * Destroy the tunnel.
569 * This function does not generate any warning traffic to clients or peers.
572 * Cancel messages belonging to this tunnel queued to neighbors.
573 * Free any allocated resources linked to the tunnel.
575 * @param t The tunnel to destroy.
578 GMT_destroy (struct MeshTunnel2 *t)
580 struct MeshConnection *c;
581 struct MeshConnection *next;
586 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
589 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
592 for (c = t->connection_head; NULL != c; c = next)
598 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
599 GMP_set_tunnel (t->peer, NULL);
605 * Demultiplex by message type and call appropriate handler for a message
606 * towards a channel of a local tunnel.
608 * @param t Tunnel this message came on.
609 * @param msgh Message header.
610 * @param fwd Is this message fwd?
613 GMT_handle_decrypted (struct MeshTunnel2 *t,
614 const struct GNUNET_MessageHeader *msgh,
617 switch (ntohs (msgh->type))
619 case GNUNET_MESSAGE_TYPE_MESH_DATA:
620 /* Don't send hop ACK, wait for client to ACK */
621 GMCH_handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
624 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
625 GMCH_handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
628 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
629 GMCH_handle_create (t,
630 (struct GNUNET_MESH_ChannelCreate *) msgh,
634 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
636 (struct GNUNET_MESH_ChannelManage *) msgh,
640 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
641 GMCH_handle_destroy (t,
642 (struct GNUNET_MESH_ChannelManage *) msgh,
648 LOG (GNUNET_ERROR_TYPE_DEBUG,
649 "end-to-end message not known (%u)\n",
656 * Notifies a tunnel that a connection has broken that affects at least
657 * some of its peers. Sends a notification towards the root of the tree.
658 * In case the peer is the owner of the tree, notifies the client that owns
659 * the tunnel and tries to reconnect.
661 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
663 * @param t Tunnel affected.
664 * @param p1 Peer that got disconnected from p2.
665 * @param p2 Peer that got disconnected from p1.
667 * @return Short ID of the peer disconnected (either p1 or p2).
668 * 0 if the tunnel remained unaffected.
671 GMT_notify_connection_broken (struct MeshTunnel2* t,
672 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
674 // if (myid != p1 && myid != p2) FIXME
679 // if (tree_get_predecessor (t->tree) != 0)
681 // /* We are the peer still connected, notify owner of the disconnection. */
682 // struct GNUNET_MESH_PathBroken msg;
683 // struct GNUNET_PeerIdentity neighbor;
685 // msg.header.size = htons (sizeof (msg));
686 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
687 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
688 // msg.tid = htonl (t->id.tid);
689 // msg.peer1 = my_full_id;
690 // GNUNET_PEER_resolve (pid, &msg.peer2);
691 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
692 // send_prebuilt_message (&msg.header, &neighbor, t);
698 * @brief Use the given path for the tunnel.
699 * Update the next and prev hops (and RCs).
700 * (Re)start the path refresh in case the tunnel is locally owned.
702 * @param t Tunnel to update.
703 * @param p Path to use.
705 * @return Connection created.
707 struct MeshConnection *
708 GMT_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
710 struct MeshConnection *c;
711 struct GNUNET_HashCode cid;
712 unsigned int own_pos;
714 if (NULL == t || NULL == p)
720 for (own_pos = 0; own_pos < p->length; own_pos++)
722 if (p->peers[own_pos] == my_short_id)
725 if (own_pos > p->length - 1)
731 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
732 c = GMC_new (&cid, t, p, own_pos);
733 GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, c);
739 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
740 * Encrypt data with the tunnel key.
742 * @param t Tunnel whose key to use.
743 * @param dst Destination for the encrypted data.
744 * @param src Source of the plaintext.
745 * @param size Size of the plaintext.
746 * @param iv Initialization Vector to use.
747 * @param fwd Is this a fwd message?
750 GMT_encrypt (struct MeshTunnel2 *t,
751 void *dst, const void *src,
752 size_t size, uint64_t iv, int fwd)
754 memcpy (dst, src, size);
759 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
760 * Decrypt data with the tunnel key.
762 * @param t Tunnel whose key to use.
763 * @param dst Destination for the plaintext.
764 * @param src Source of the encrypted data.
765 * @param size Size of the encrypted data.
766 * @param iv Initialization Vector to use.
767 * @param fwd Is this a fwd message?
770 GMT_decrypt (struct MeshTunnel2 *t,
771 void *dst, const void *src,
772 size_t size, uint64_t iv, int fwd)
774 memcpy (dst, src, size);
779 * Count established (ready) connections of a tunnel.
781 * @param t Tunnel on which to send the message.
783 * @return Number of connections.
786 GMT_count_connections (struct MeshTunnel2 *t)
788 return GMC_count (t->connection_head);
793 * Sends an already built message on a tunnel, choosing the best connection.
795 * @param message Message to send. Function modifies it.
796 * @param t Tunnel on which this message is transmitted.
797 * @param ch Channel on which this message is transmitted.
798 * @param fwd Is this a fwd message?
801 GMT_send_prebuilt_message (struct GNUNET_MESH_Encrypted *msg,
802 struct MeshTunnel2 *t,
803 struct MeshChannel *ch,
806 struct MeshConnection *c;
809 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
811 c = tunnel_get_connection (t, fwd);
814 GNUNET_break (GNUNET_YES == t->destroy);
817 type = ntohs (msg->header.type);
820 case GNUNET_MESSAGE_TYPE_MESH_FWD:
821 case GNUNET_MESSAGE_TYPE_MESH_BCK:
822 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
823 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
824 msg->cid = *GMC_get_id (c);
825 msg->ttl = htonl (default_ttl);
828 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
829 GNUNET_MESH_DEBUG_M2S (type));
834 GMC_send_prebuilt_message (&msg->header, c, ch, fwd);