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"
33 * All the states a tunnel can be in.
38 * Uninitialized status, should never appear in operation.
43 * Path to the peer not known yet
45 MESH_TUNNEL_SEARCHING,
48 * Request sent, not yet answered.
53 * Peer connected and ready to accept data
58 * Peer connected previosly but not responding
60 MESH_TUNNEL_RECONNECTING
65 /******************************************************************************/
66 /******************************** STRUCTS **********************************/
67 /******************************************************************************/
70 * Struct containing all information regarding a tunnel to a peer.
75 * Endpoint of the tunnel.
77 struct MeshPeer *peer;
80 * State of the tunnel.
82 enum MeshTunnelState state;
85 * Local peer ephemeral private key
87 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
90 * Local peer ephemeral public key
92 struct GNUNET_CRYPTO_EccPublicSignKey *my_eph;
95 * Remote peer's public key.
97 struct GNUNET_CRYPTO_EccPublicSignKey *peers_eph;
100 * Encryption ("our") key.
102 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
105 * Decryption ("their") key.
107 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
110 * Paths that are actively used to reach the destination peer.
112 struct MeshConnection *connection_head;
113 struct MeshConnection *connection_tail;
116 * Next connection number.
121 * Channels inside this tunnel.
123 struct MeshChannel *channel_head;
124 struct MeshChannel *channel_tail;
127 * Channel ID for the next created channel.
129 MESH_ChannelNumber next_chid;
132 * Channel ID for the next incoming channel.
134 MESH_ChannelNumber next_local_chid;
137 * Pending message count.
139 int pending_messages;
142 * Destroy flag: if true, destroy on last message.
147 * Queued messages, to transmit once tunnel gets connected.
149 struct MeshTunnelQueue *tq_head;
150 struct MeshTunnelQueue *tq_tail;
155 * Struct used to queue messages in a tunnel.
157 struct MeshTunnelQueue
162 struct MeshTunnelQueue *next;
163 struct MeshTunnelQueue *prev;
168 struct MeshChannel *ch;
173 /* struct GNUNET_MessageHeader *msg; */
176 /******************************************************************************/
177 /******************************* GLOBALS ***********************************/
178 /******************************************************************************/
181 * Default TTL for payload packets.
183 static unsigned long long default_ttl;
186 * Local peer own ID (memory efficient handle).
188 static GNUNET_PEER_Id my_short_id;
191 * Local peer own ID (full value).
193 const static struct GNUNET_PeerIdentity *my_full_id;
198 const static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
201 /******************************************************************************/
202 /******************************** STATIC ***********************************/
203 /******************************************************************************/
207 * Get string description for tunnel state.
209 * @param s Tunnel state.
211 * @return String representation.
214 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
216 static char buf[128];
220 case MESH_TUNNEL_NEW:
221 return "MESH_TUNNEL_NEW";
222 case MESH_TUNNEL_SEARCHING:
223 return "MESH_TUNNEL_SEARCHING";
224 case MESH_TUNNEL_WAITING:
225 return "MESH_TUNNEL_WAITING";
226 case MESH_TUNNEL_READY:
227 return "MESH_TUNNEL_READY";
228 case MESH_TUNNEL_RECONNECTING:
229 return "MESH_TUNNEL_RECONNECTING";
232 sprintf (buf, "%u (UNKNOWN STATE)", s);
239 * Pick a connection on which send the next data message.
241 * @param t Tunnel on which to send the message.
242 * @param fwd Is this a fwd message?
244 * @return The connection on which to send the next message.
246 static struct MeshConnection *
247 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
249 struct MeshConnection *c;
250 struct MeshConnection *best;
251 struct MeshFlowControl *fc;
252 unsigned int lowest_q;
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n",
258 for (c = t->connection_head; NULL != c; c = c->next)
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
261 GNUNET_h2s (&c->id), c->state);
262 if (MESH_CONNECTION_READY == c->state)
264 fc = fwd ? &c->fwd_fc : &c->bck_fc;
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", fc->queue_n);
271 if (fc->queue_n < lowest_q)
274 lowest_q = fc->queue_n;
283 * Get the total buffer space for a tunnel.
286 * @param fwd Is this for FWD traffic?
288 * @return Buffer space offered by all connections in the tunnel.
291 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
293 struct MeshConnection *c;
294 struct MeshFlowControl *fc;
297 c = t->connection_head;
300 /* If terminal, return biggest channel buffer */
301 if (NULL == c || GMC_is_terminal (c, fwd))
303 struct MeshChannel *ch;
306 if (NULL == t->channel_head)
309 for (ch = t->channel_head; NULL != ch; ch = ch->next)
311 ch_buf = GMCH_get_buffer (ch, fwd);
318 /* If not terminal, return sum of connection buffers */
321 if (c->state != MESH_CONNECTION_READY)
327 fc = fwd ? &c->fwd_fc : &c->bck_fc;
328 buffer += fc->queue_max - fc->queue_n;
337 * Send all cached messages that we can, tunnel is online.
339 * @param t Tunnel that holds the messages.
340 * @param fwd Is this fwd?
343 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
345 struct MeshTunnelQueue *tq;
346 struct MeshTunnelQueue *next;
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "tunnel_send_queued_data on tunnel %s\n",
352 room = tunnel_get_buffer (t, fwd);
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
354 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
358 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
359 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
368 * Cache a message to be sent once tunnel is online.
370 * @param t Tunnel to hold the message.
371 * @param ch Channel the message is about.
372 * @param msg Message itself (copy will be made).
373 * @param fwd Is this fwd?
376 GMT_queue_data (struct MeshTunnel2 *t,
377 struct MeshChannel *ch,
378 struct GNUNET_MessageHeader *msg,
381 struct MeshTunnelQueue *tq;
382 uint16_t size = ntohs (msg->size);
384 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
387 memcpy (&tq[1], msg, size);
388 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
390 if (MESH_TUNNEL_READY == t->state)
391 tunnel_send_queued_data (t, fwd);
396 /******************************************************************************/
397 /******************************** API ***********************************/
398 /******************************************************************************/
401 * Initialize the tunnel subsystem.
403 * @param c Configuration handle.
404 * @param id Peer identity.
405 * @param key ECC private key, to derive all other keys and do crypto.
408 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
409 const struct GNUNET_PeerIdentity *id,
410 const struct GNUNET_CRYPTO_EccPrivateKey *key)
413 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
416 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
417 "MESH", "DEFAULT_TTL", "USING DEFAULT");
421 my_private_key = key;
422 my_short_id = GNUNET_PEER_intern (my_full_id);
427 * Shut down the tunnel subsystem.
432 GNUNET_PEER_change_rc (my_short_id, -1);
442 struct MeshTunnel2 *t;
444 t = GNUNET_new (struct MeshTunnel2);
446 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
448 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
449 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
452 // tunnel_destroy (t);
456 // char salt[] = "salt";
457 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
458 // salt, sizeof (salt),
459 // &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
460 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
461 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
463 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
464 // salt, sizeof (salt),
465 // &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
466 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
467 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
476 * Change the tunnel state.
478 * @param t Tunnel whose state to change.
479 * @param state New state.
482 GMT_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Tunnel %s state was %s\n",
489 GNUNET_MESH_DEBUG_TS2S (t->state));
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "Tunnel %s state is now %s\n",
493 GNUNET_MESH_DEBUG_TS2S (state));
499 * Add a connection to a tunnel.
502 * @param c Connection.
505 GMT_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
507 struct MeshConnection *aux;
509 for (aux = t->connection_head; aux != NULL; aux = aux->next)
512 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
519 * Tunnel is empty: destroy it.
521 * Notifies all connections about the destruction.
523 * @param t Tunnel to destroy.
526 GMT_destroy_empty (struct MeshTunnel2 *t)
528 struct MeshConnection *c;
530 for (c = t->connection_head; NULL != c; c = c->next)
532 if (GNUNET_NO == c->destroy)
533 GMC_send_destroy (c);
536 if (0 == t->pending_messages)
539 t->destroy = GNUNET_YES;
544 * Destroy tunnel if empty (no more channels).
546 * @param t Tunnel to destroy if empty.
549 GMT_destroy_if_empty (struct MeshTunnel2 *t)
551 if (1 <= GMCH_count (t->channel_head))
554 GMT_destroy_empty (t);
560 * Destroy the tunnel.
562 * This function does not generate any warning traffic to clients or peers.
565 * Cancel messages belonging to this tunnel queued to neighbors.
566 * Free any allocated resources linked to the tunnel.
568 * @param t The tunnel to destroy.
571 GMT_destroy (struct MeshTunnel2 *t)
573 struct MeshConnection *c;
574 struct MeshConnection *next;
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
582 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
585 for (c = t->connection_head; NULL != c; c = next)
591 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
592 t->peer->tunnel = NULL;
598 * Demultiplex by message type and call appropriate handler for a message
599 * towards a channel of a local tunnel.
601 * @param t Tunnel this message came on.
602 * @param msgh Message header.
603 * @param fwd Is this message fwd?
606 GMT_handle_decrypted (struct MeshTunnel2 *t,
607 const struct GNUNET_MessageHeader *msgh,
610 switch (ntohs (msgh->type))
612 case GNUNET_MESSAGE_TYPE_MESH_DATA:
613 /* Don't send hop ACK, wait for client to ACK */
614 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
617 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
618 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
621 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
622 handle_channel_create (t,
623 (struct GNUNET_MESH_ChannelCreate *) msgh,
627 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
628 handle_channel_ack (t,
629 (struct GNUNET_MESH_ChannelManage *) msgh,
633 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
634 handle_channel_destroy (t,
635 (struct GNUNET_MESH_ChannelManage *) msgh,
640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
641 "end-to-end message not known (%u)\n",
648 * Notifies a tunnel that a connection has broken that affects at least
649 * some of its peers. Sends a notification towards the root of the tree.
650 * In case the peer is the owner of the tree, notifies the client that owns
651 * the tunnel and tries to reconnect.
653 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
655 * @param t Tunnel affected.
656 * @param p1 Peer that got disconnected from p2.
657 * @param p2 Peer that got disconnected from p1.
659 * @return Short ID of the peer disconnected (either p1 or p2).
660 * 0 if the tunnel remained unaffected.
663 GMT_notify_connection_broken (struct MeshTunnel2* t,
664 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
666 // if (myid != p1 && myid != p2) FIXME
671 // if (tree_get_predecessor (t->tree) != 0)
673 // /* We are the peer still connected, notify owner of the disconnection. */
674 // struct GNUNET_MESH_PathBroken msg;
675 // struct GNUNET_PeerIdentity neighbor;
677 // msg.header.size = htons (sizeof (msg));
678 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
679 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
680 // msg.tid = htonl (t->id.tid);
681 // msg.peer1 = my_full_id;
682 // GNUNET_PEER_resolve (pid, &msg.peer2);
683 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
684 // send_prebuilt_message (&msg.header, &neighbor, t);
690 * @brief Use the given path for the tunnel.
691 * Update the next and prev hops (and RCs).
692 * (Re)start the path refresh in case the tunnel is locally owned.
694 * @param t Tunnel to update.
695 * @param p Path to use.
697 * @return Connection created.
699 struct MeshConnection *
700 GMT_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
702 struct MeshConnection *c;
703 struct GNUNET_HashCode cid;
704 struct MeshPeer *peer;
705 unsigned int own_pos;
707 if (NULL == t || NULL == p)
713 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
717 GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, c);
718 for (own_pos = 0; own_pos < p->length; own_pos++)
720 if (p->peers[own_pos] == myid)
723 if (own_pos > p->length - 1)
726 connection_destroy (c);
729 c->own_pos = own_pos;
734 c->fwd_maintenance_task =
735 GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
736 &connection_fwd_keepalive, c);
739 peer = connection_get_next_hop (c);
740 if (NULL == peer->connections)
742 connection_destroy (c);
745 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
746 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
747 peer = connection_get_prev_hop (c);
748 if (NULL == peer->connections)
750 connection_destroy (c);
753 GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
754 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
760 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
761 * Encrypt data with the tunnel key.
763 * @param t Tunnel whose key to use.
764 * @param dst Destination for the encrypted data.
765 * @param src Source of the plaintext.
766 * @param size Size of the plaintext.
767 * @param iv Initialization Vector to use.
768 * @param fwd Is this a fwd message?
771 GMT_encrypt (struct MeshTunnel2 *t,
772 void *dst, const void *src,
773 size_t size, uint64_t iv, int fwd)
775 memcpy (dst, src, size);
780 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
781 * Decrypt data with the tunnel key.
783 * @param t Tunnel whose key to use.
784 * @param dst Destination for the plaintext.
785 * @param src Source of the encrypted data.
786 * @param size Size of the encrypted data.
787 * @param iv Initialization Vector to use.
788 * @param fwd Is this a fwd message?
791 GMT_decrypt (struct MeshTunnel2 *t,
792 void *dst, const void *src,
793 size_t size, uint64_t iv, int fwd)
795 memcpy (dst, src, size);
800 * Count established (ready) connections of a tunnel.
802 * @param t Tunnel on which to send the message.
804 * @return Number of connections.
807 GMT_count_connections (struct MeshTunnel2 *t)
809 return GMC_count (t->connection_head);
814 * Sends an already built message on a tunnel, choosing the best connection.
816 * @param message Message to send. Function modifies it.
817 * @param t Tunnel on which this message is transmitted.
818 * @param ch Channel on which this message is transmitted.
819 * @param fwd Is this a fwd message?
822 GMT_send_prebuilt_message (struct GNUNET_MESH_Encrypted *msg,
823 struct MeshTunnel2 *t,
824 struct MeshChannel *ch,
827 struct MeshConnection *c;
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
832 c = tunnel_get_connection (t, fwd);
835 GNUNET_break (GNUNET_YES == t->destroy);
838 type = ntohs (msg->header.type);
841 case GNUNET_MESSAGE_TYPE_MESH_FWD:
842 case GNUNET_MESSAGE_TYPE_MESH_BCK:
843 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
844 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
846 msg->ttl = htonl (default_ttl);
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
850 GNUNET_MESH_DEBUG_M2S (type));
855 GMC_send_prebuilt_message (&msg->header, c, ch, fwd);