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 /******************************************************************************/
38 /******************************** STRUCTS **********************************/
39 /******************************************************************************/
43 struct MeshTChannel *next;
44 struct MeshTChannel *prev;
45 struct MeshChannel *ch;
48 struct MeshTConnection
50 struct MeshTConnection *next;
51 struct MeshTConnection *prev;
52 struct MeshConnection *c;
56 * Struct containing all information regarding a tunnel to a peer.
61 * Endpoint of the tunnel.
63 struct MeshPeer *peer;
66 * State of the tunnel.
68 enum MeshTunnelState state;
71 * Local peer ephemeral private key
73 struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
76 * Local peer ephemeral public key
78 struct GNUNET_CRYPTO_EccPublicSignKey *my_eph;
81 * Remote peer's public key.
83 struct GNUNET_CRYPTO_EccPublicSignKey *peers_eph;
86 * Encryption ("our") key.
88 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
91 * Decryption ("their") key.
93 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
96 * Paths that are actively used to reach the destination peer.
98 struct MeshTConnection *connection_head;
99 struct MeshTConnection *connection_tail;
102 * Next connection number.
107 * Channels inside this tunnel.
109 struct MeshTChannel *channel_head;
110 struct MeshTChannel *channel_tail;
113 * Channel ID for the next created channel.
115 MESH_ChannelNumber next_chid;
118 * Channel ID for the next incoming channel.
120 MESH_ChannelNumber next_local_chid;
123 * Pending message count.
125 int pending_messages;
128 * Destroy flag: if true, destroy on last message.
133 * Queued messages, to transmit once tunnel gets connected.
135 struct MeshTunnelQueue *tq_head;
136 struct MeshTunnelQueue *tq_tail;
141 * Struct used to queue messages in a tunnel.
143 struct MeshTunnelQueue
148 struct MeshTunnelQueue *next;
149 struct MeshTunnelQueue *prev;
154 struct MeshChannel *ch;
159 /* struct GNUNET_MessageHeader *msg; */
162 /******************************************************************************/
163 /******************************* GLOBALS ***********************************/
164 /******************************************************************************/
167 * Global handle to the statistics service.
169 extern struct GNUNET_STATISTICS_Handle *stats;
172 * Default TTL for payload packets.
174 static unsigned long long default_ttl;
177 * Local peer own ID (memory efficient handle).
179 static GNUNET_PEER_Id my_short_id;
182 * Local peer own ID (full value).
184 const static struct GNUNET_PeerIdentity *my_full_id;
189 const static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
192 /******************************************************************************/
193 /******************************** STATIC ***********************************/
194 /******************************************************************************/
197 * Get string description for tunnel state.
199 * @param s Tunnel state.
201 * @return String representation.
204 GMT_state2s (enum MeshTunnelState s)
206 static char buf[128];
210 case MESH_TUNNEL_NEW:
211 return "MESH_TUNNEL_NEW";
212 case MESH_TUNNEL_SEARCHING:
213 return "MESH_TUNNEL_SEARCHING";
214 case MESH_TUNNEL_WAITING:
215 return "MESH_TUNNEL_WAITING";
216 case MESH_TUNNEL_READY:
217 return "MESH_TUNNEL_READY";
218 case MESH_TUNNEL_RECONNECTING:
219 return "MESH_TUNNEL_RECONNECTING";
222 sprintf (buf, "%u (UNKNOWN STATE)", s);
229 * Search for a channel by global ID using full PeerIdentities.
231 * @param t Tunnel containing the channel.
232 * @param chid Public channel number.
234 * @return channel handler, NULL if doesn't exist
236 static struct MeshChannel *
237 get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid)
239 struct MeshTChannel *iter;
244 for (iter = t->channel_head; NULL != iter; iter = iter->next)
246 if (GMCH_get_id (iter->ch) == chid)
250 return NULL == iter ? NULL : iter->ch;
255 * Pick a connection on which send the next data message.
257 * @param t Tunnel on which to send the message.
258 * @param fwd Is this a fwd message?
260 * @return The connection on which to send the next message.
262 static struct MeshConnection *
263 tunnel_get_connection (struct MeshTunnel3 *t, int fwd)
265 struct MeshTConnection *iter;
266 struct MeshConnection *best;
268 unsigned int lowest_q;
270 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GMP_2s (t->peer));
273 for (iter = t->connection_head; NULL != iter; iter = iter->next)
275 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
276 GNUNET_h2s (GMC_get_id (iter->c)), GMC_get_state (iter->c));
277 if (MESH_CONNECTION_READY == GMC_get_state (iter->c))
279 qn = GMC_get_qn (iter->c, fwd);
280 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
293 * Get the total buffer space for a tunnel.
296 * @param fwd Is this for FWD traffic?
298 * @return Buffer space offered by all connections in the tunnel.
301 tunnel_get_buffer (struct MeshTunnel3 *t, int fwd)
303 struct MeshTConnection *iter;
306 iter = t->connection_head;
309 /* If terminal, return biggest channel buffer */
310 if (NULL == iter || GMC_is_terminal (iter->c, fwd))
312 struct MeshTChannel *iter_ch;
315 if (NULL == t->channel_head)
318 for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = iter_ch->next)
320 ch_buf = GMCH_get_buffer (iter_ch->ch, fwd);
327 /* If not terminal, return sum of connection buffers */
330 if (GMC_get_state (iter->c) != MESH_CONNECTION_READY)
336 buffer += GMC_get_buffer (iter->c, fwd);
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 MeshTunnel3 *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 handle_data (struct MeshTunnel3 *t,
376 const struct GNUNET_MESH_Data *msg,
379 struct MeshChannel *ch;
384 size = ntohs (msg->header.size);
386 sizeof (struct GNUNET_MESH_Data) +
387 sizeof (struct GNUNET_MessageHeader))
392 type = ntohs (msg->header.type);
393 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
394 GNUNET_MESH_DEBUG_M2S (type));
395 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
396 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
399 ch = get_channel (t, ntohl (msg->chid));
402 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
404 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
409 GMT_change_state (t, MESH_TUNNEL_READY);
410 GMCH_handle_data (ch, msg, fwd);
414 handle_data_ack (struct MeshTunnel3 *t,
415 const struct GNUNET_MESH_DataACK *msg,
418 struct MeshChannel *ch;
422 size = ntohs (msg->header.size);
423 if (size != sizeof (struct GNUNET_MESH_DataACK))
430 ch = get_channel (t, ntohl (msg->chid));
433 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
435 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
440 GMCH_handle_data_ack (ch, msg, fwd);
444 handle_ch_create (struct MeshTunnel3 *t,
445 const struct GNUNET_MESH_ChannelCreate *msg,
448 struct MeshTChannel *tch;
449 struct MeshChannel *ch;
453 size = ntohs (msg->header.size);
454 if (size != sizeof (struct GNUNET_MESH_ChannelCreate))
461 ch = get_channel (t, ntohl (msg->chid));
464 /* Probably a retransmission, safe to ignore */
465 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
469 ch = GMCH_handle_create (msg, fwd);
472 tch = GNUNET_new (struct MeshTChannel);
474 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, tch);
478 handle_ch_ack (struct MeshTunnel3 *t,
479 const struct GNUNET_MESH_ChannelManage *msg,
482 struct MeshChannel *ch;
486 size = ntohs (msg->header.size);
487 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
494 ch = get_channel (t, ntohl (msg->chid));
497 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
499 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
504 GMCH_handle_ack (ch, msg, fwd);
508 handle_ch_destroy (struct MeshTunnel3 *t,
509 const struct GNUNET_MESH_ChannelManage *msg,
512 struct MeshChannel *ch;
516 size = ntohs (msg->header.size);
517 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
524 ch = get_channel (t, ntohl (msg->chid));
527 /* Probably a retransmission, safe to ignore */
531 GMCH_handle_destroy (ch, msg, fwd);
534 /******************************************************************************/
535 /******************************** API ***********************************/
536 /******************************************************************************/
539 * Demultiplex by message type and call appropriate handler for a message
540 * towards a channel of a local tunnel.
542 * @param t Tunnel this message came on.
543 * @param msgh Message header.
544 * @param fwd Is this message fwd?
547 GMT_handle_decrypted (struct MeshTunnel3 *t,
548 const struct GNUNET_MessageHeader *msgh,
553 type = ntohs (msgh->type);
554 LOG (GNUNET_ERROR_TYPE_DEBUG,
555 "Got a %s message!\n",
556 GNUNET_MESH_DEBUG_M2S (type));
560 case GNUNET_MESSAGE_TYPE_MESH_DATA:
561 /* Don't send hop ACK, wait for client to ACK */
562 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
565 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
566 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
569 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
571 (struct GNUNET_MESH_ChannelCreate *) msgh,
575 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
577 (struct GNUNET_MESH_ChannelManage *) msgh,
581 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
582 handle_ch_destroy (t,
583 (struct GNUNET_MESH_ChannelManage *) msgh,
589 LOG (GNUNET_ERROR_TYPE_DEBUG,
590 "end-to-end message not known (%u)\n",
597 * Cache a message to be sent once tunnel is online.
599 * @param t Tunnel to hold the message.
600 * @param ch Channel the message is about.
601 * @param msg Message itself (copy will be made).
602 * @param fwd Is this fwd?
605 GMT_queue_data (struct MeshTunnel3 *t,
606 struct MeshChannel *ch,
607 struct GNUNET_MessageHeader *msg,
610 struct MeshTunnelQueue *tq;
611 uint16_t size = ntohs (msg->size);
613 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
616 memcpy (&tq[1], msg, size);
617 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
619 if (MESH_TUNNEL_READY == t->state)
620 tunnel_send_queued_data (t, fwd);
625 * Initialize the tunnel subsystem.
627 * @param c Configuration handle.
628 * @param id Peer identity.
629 * @param key ECC private key, to derive all other keys and do crypto.
632 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
633 const struct GNUNET_PeerIdentity *id,
634 const struct GNUNET_CRYPTO_EccPrivateKey *key)
637 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
640 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
641 "MESH", "DEFAULT_TTL", "USING DEFAULT");
645 my_private_key = key;
646 my_short_id = GNUNET_PEER_intern (my_full_id);
651 * Shut down the tunnel subsystem.
656 GNUNET_PEER_change_rc (my_short_id, -1);
666 struct MeshTunnel3 *t;
668 t = GNUNET_new (struct MeshTunnel3);
670 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
672 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
673 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
676 // tunnel_destroy (t);
680 // char salt[] = "salt";
681 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
682 // salt, sizeof (salt),
683 // &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
684 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
685 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
687 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
688 // salt, sizeof (salt),
689 // &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
690 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
691 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
700 * Change the tunnel state.
702 * @param t Tunnel whose state to change.
703 * @param state New state.
706 GMT_change_state (struct MeshTunnel3* t, enum MeshTunnelState state)
710 LOG (GNUNET_ERROR_TYPE_DEBUG,
711 "Tunnel %s state was %s\n",
713 GMT_state2s (t->state));
714 LOG (GNUNET_ERROR_TYPE_DEBUG,
715 "Tunnel %s state is now %s\n",
717 GMT_state2s (state));
723 * Add a connection to a tunnel.
726 * @param c Connection.
729 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
731 struct MeshTConnection *aux;
733 for (aux = t->connection_head; aux != NULL; aux = aux->next)
737 aux = GNUNET_new (struct MeshTConnection);
739 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, aux);
744 * Tunnel is empty: destroy it.
746 * Notifies all connections about the destruction.
748 * @param t Tunnel to destroy.
751 GMT_destroy_empty (struct MeshTunnel3 *t)
753 struct MeshTConnection *iter;
755 for (iter = t->connection_head; NULL != iter; iter = iter->next)
757 GMC_send_destroy (iter->c);
760 if (0 == t->pending_messages)
763 t->destroy = GNUNET_YES;
768 * Destroy tunnel if empty (no more channels).
770 * @param t Tunnel to destroy if empty.
773 GMT_destroy_if_empty (struct MeshTunnel3 *t)
775 if (1 < GMT_count_channels (t))
778 GMT_destroy_empty (t);
783 * Destroy the tunnel.
785 * This function does not generate any warning traffic to clients or peers.
788 * Cancel messages belonging to this tunnel queued to neighbors.
789 * Free any allocated resources linked to the tunnel.
791 * @param t The tunnel to destroy.
794 GMT_destroy (struct MeshTunnel3 *t)
796 struct MeshTConnection *iter;
797 struct MeshTConnection *next;
802 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
804 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
807 for (iter = t->connection_head; NULL != iter; iter = next)
810 GMC_destroy (iter->c);
814 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
815 GMP_set_tunnel (t->peer, NULL);
822 * Notifies a tunnel that a connection has broken that affects at least
823 * some of its peers. Sends a notification towards the root of the tree.
824 * In case the peer is the owner of the tree, notifies the client that owns
825 * the tunnel and tries to reconnect.
827 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
829 * @param t Tunnel affected.
830 * @param p1 Peer that got disconnected from p2.
831 * @param p2 Peer that got disconnected from p1.
833 * @return Short ID of the peer disconnected (either p1 or p2).
834 * 0 if the tunnel remained unaffected.
837 GMT_notify_connection_broken (struct MeshTunnel3* t,
838 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
840 // if (myid != p1 && myid != p2) FIXME
845 // if (tree_get_predecessor (t->tree) != 0)
847 // /* We are the peer still connected, notify owner of the disconnection. */
848 // struct GNUNET_MESH_PathBroken msg;
849 // struct GNUNET_PeerIdentity neighbor;
851 // msg.header.size = htons (sizeof (msg));
852 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
853 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
854 // msg.tid = htonl (t->id.tid);
855 // msg.peer1 = my_full_id;
856 // GNUNET_PEER_resolve (pid, &msg.peer2);
857 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
858 // send_prebuilt_message (&msg.header, &neighbor, t);
864 * @brief Use the given path for the tunnel.
865 * Update the next and prev hops (and RCs).
866 * (Re)start the path refresh in case the tunnel is locally owned.
868 * @param t Tunnel to update.
869 * @param p Path to use.
871 * @return Connection created.
873 struct MeshConnection *
874 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p)
876 struct MeshConnection *c;
877 struct GNUNET_HashCode cid;
878 unsigned int own_pos;
880 if (NULL == t || NULL == p)
886 for (own_pos = 0; own_pos < p->length; own_pos++)
888 if (p->peers[own_pos] == my_short_id)
891 if (own_pos > p->length - 1)
897 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
898 c = GMC_new (&cid, t, p, own_pos);
899 GMT_add_connection (t, c);
905 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
906 * Encrypt data with the tunnel key.
908 * @param t Tunnel whose key to use.
909 * @param dst Destination for the encrypted data.
910 * @param src Source of the plaintext.
911 * @param size Size of the plaintext.
912 * @param iv Initialization Vector to use.
913 * @param fwd Is this a fwd message?
916 GMT_encrypt (struct MeshTunnel3 *t,
917 void *dst, const void *src,
918 size_t size, uint64_t iv, int fwd)
920 memcpy (dst, src, size);
925 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
926 * Decrypt data with the tunnel key.
928 * @param t Tunnel whose key to use.
929 * @param dst Destination for the plaintext.
930 * @param src Source of the encrypted data.
931 * @param size Size of the encrypted data.
932 * @param iv Initialization Vector to use.
933 * @param fwd Is this a fwd message?
936 GMT_decrypt (struct MeshTunnel3 *t,
937 void *dst, const void *src,
938 size_t size, uint64_t iv, int fwd)
940 memcpy (dst, src, size);
945 * Count established (ready) connections of a tunnel.
947 * @param t Tunnel on which to count.
949 * @return Number of connections.
952 GMT_count_connections (struct MeshTunnel3 *t)
954 struct MeshTConnection *iter;
957 for (count = 0, iter = t->connection_head;
959 iter = iter->next, count++);
965 * Count channels of a tunnel.
967 * @param t Tunnel on which to count.
969 * @return Number of channels.
972 GMT_count_channels (struct MeshTunnel3 *t)
974 struct MeshTChannel *iter;
977 for (count = 0, iter = t->channel_head;
979 iter = iter->next, count++);
986 * Sends an already built message on a tunnel, choosing the best connection.
988 * @param message Message to send. Function modifies it.
989 * @param t Tunnel on which this message is transmitted.
990 * @param ch Channel on which this message is transmitted.
991 * @param fwd Is this a fwd message?
994 GMT_send_prebuilt_message (struct GNUNET_MESH_Encrypted *msg,
995 struct MeshTunnel3 *t,
996 struct MeshChannel *ch,
999 struct MeshConnection *c;
1002 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n", GMP_2s (t->peer));
1003 c = tunnel_get_connection (t, fwd);
1006 GNUNET_break (GNUNET_YES == t->destroy);
1009 type = ntohs (msg->header.type);
1012 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1013 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1014 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1015 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1016 msg->cid = *GMC_get_id (c);
1017 msg->ttl = htonl (default_ttl);
1020 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1021 GNUNET_MESH_DEBUG_M2S (type));
1026 GMC_send_prebuilt_message (&msg->header, c, ch, fwd);