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);
228 * Pick a connection on which send the next data message.
230 * @param t Tunnel on which to send the message.
231 * @param fwd Is this a fwd message?
233 * @return The connection on which to send the next message.
235 static struct MeshConnection *
236 tunnel_get_connection (struct MeshTunnel3 *t, int fwd)
238 struct MeshTConnection *iter;
239 struct MeshConnection *best;
241 unsigned int lowest_q;
243 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GMP_2s (t->peer));
246 for (iter = t->connection_head; NULL != iter; iter = iter->next)
248 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
249 GNUNET_h2s (GMC_get_id (iter->c)), GMC_get_state (iter->c));
250 if (MESH_CONNECTION_READY == GMC_get_state (iter->c))
252 qn = GMC_get_qn (iter->c, fwd);
253 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
266 handle_data (struct MeshTunnel3 *t,
267 const struct GNUNET_MESH_Data *msg,
270 struct MeshChannel *ch;
275 size = ntohs (msg->header.size);
277 sizeof (struct GNUNET_MESH_Data) +
278 sizeof (struct GNUNET_MessageHeader))
283 type = ntohs (msg->header.type);
284 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
285 GNUNET_MESH_DEBUG_M2S (type));
286 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
287 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
290 ch = GMT_get_channel (t, ntohl (msg->chid));
293 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
295 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
300 GMT_change_state (t, MESH_TUNNEL_READY);
301 GMCH_handle_data (ch, msg, fwd);
305 handle_data_ack (struct MeshTunnel3 *t,
306 const struct GNUNET_MESH_DataACK *msg,
309 struct MeshChannel *ch;
313 size = ntohs (msg->header.size);
314 if (size != sizeof (struct GNUNET_MESH_DataACK))
321 ch = GMT_get_channel (t, ntohl (msg->chid));
324 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
326 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
331 GMCH_handle_data_ack (ch, msg, fwd);
335 handle_ch_create (struct MeshTunnel3 *t,
336 const struct GNUNET_MESH_ChannelCreate *msg,
339 struct MeshTChannel *tch;
340 struct MeshChannel *ch;
344 size = ntohs (msg->header.size);
345 if (size != sizeof (struct GNUNET_MESH_ChannelCreate))
352 ch = GMT_get_channel (t, ntohl (msg->chid));
355 /* Probably a retransmission, safe to ignore */
356 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
360 ch = GMCH_handle_create (msg, fwd);
363 tch = GNUNET_new (struct MeshTChannel);
365 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, tch);
369 handle_ch_ack (struct MeshTunnel3 *t,
370 const struct GNUNET_MESH_ChannelManage *msg,
373 struct MeshChannel *ch;
377 size = ntohs (msg->header.size);
378 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
385 ch = GMT_get_channel (t, ntohl (msg->chid));
388 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
390 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
395 GMCH_handle_ack (ch, msg, fwd);
399 handle_ch_destroy (struct MeshTunnel3 *t,
400 const struct GNUNET_MESH_ChannelManage *msg,
403 struct MeshChannel *ch;
407 size = ntohs (msg->header.size);
408 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
415 ch = GMT_get_channel (t, ntohl (msg->chid));
418 /* Probably a retransmission, safe to ignore */
422 GMCH_handle_destroy (ch, msg, fwd);
425 /******************************************************************************/
426 /******************************** API ***********************************/
427 /******************************************************************************/
430 * Demultiplex by message type and call appropriate handler for a message
431 * towards a channel of a local tunnel.
433 * @param t Tunnel this message came on.
434 * @param msgh Message header.
435 * @param fwd Is this message fwd?
438 GMT_handle_decrypted (struct MeshTunnel3 *t,
439 const struct GNUNET_MessageHeader *msgh,
444 type = ntohs (msgh->type);
445 LOG (GNUNET_ERROR_TYPE_DEBUG,
446 "Got a %s message!\n",
447 GNUNET_MESH_DEBUG_M2S (type));
451 case GNUNET_MESSAGE_TYPE_MESH_DATA:
452 /* Don't send hop ACK, wait for client to ACK */
453 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
456 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
457 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
460 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
462 (struct GNUNET_MESH_ChannelCreate *) msgh,
466 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
468 (struct GNUNET_MESH_ChannelManage *) msgh,
472 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
473 handle_ch_destroy (t,
474 (struct GNUNET_MESH_ChannelManage *) msgh,
480 LOG (GNUNET_ERROR_TYPE_DEBUG,
481 "end-to-end message not known (%u)\n",
488 * Cache a message to be sent once tunnel is online.
490 * @param t Tunnel to hold the message.
491 * @param ch Channel the message is about.
492 * @param msg Message itself (copy will be made).
493 * @param fwd Is this fwd?
496 GMT_queue_data (struct MeshTunnel3 *t,
497 struct MeshChannel *ch,
498 struct GNUNET_MessageHeader *msg,
501 struct MeshTunnelQueue *tq;
502 uint16_t size = ntohs (msg->size);
504 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
507 memcpy (&tq[1], msg, size);
508 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
510 if (MESH_TUNNEL_READY == t->state)
511 GMT_send_queued_data (t, fwd);
516 * Send all cached messages that we can, tunnel is online.
518 * @param t Tunnel that holds the messages.
519 * @param fwd Is this fwd?
522 GMT_send_queued_data (struct MeshTunnel3 *t, int fwd)
524 struct MeshTunnelQueue *tq;
525 struct MeshTunnelQueue *next;
528 LOG (GNUNET_ERROR_TYPE_DEBUG,
529 "GMT_send_queued_data on tunnel %s\n",
531 room = GMT_get_buffer (t, fwd);
532 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
533 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
537 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
538 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
547 * Initialize the tunnel subsystem.
549 * @param c Configuration handle.
550 * @param id Peer identity.
551 * @param key ECC private key, to derive all other keys and do crypto.
554 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
555 const struct GNUNET_PeerIdentity *id,
556 const struct GNUNET_CRYPTO_EccPrivateKey *key)
559 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
562 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
563 "MESH", "DEFAULT_TTL", "USING DEFAULT");
567 my_private_key = key;
568 my_short_id = GNUNET_PEER_intern (my_full_id);
573 * Shut down the tunnel subsystem.
578 GNUNET_PEER_change_rc (my_short_id, -1);
588 struct MeshTunnel3 *t;
590 t = GNUNET_new (struct MeshTunnel3);
592 t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
594 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
595 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
598 // tunnel_destroy (t);
602 // char salt[] = "salt";
603 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
604 // salt, sizeof (salt),
605 // &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
606 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
607 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
609 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
610 // salt, sizeof (salt),
611 // &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
612 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
613 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
622 * Change the tunnel state.
624 * @param t Tunnel whose state to change.
625 * @param state New state.
628 GMT_change_state (struct MeshTunnel3* t, enum MeshTunnelState state)
632 LOG (GNUNET_ERROR_TYPE_DEBUG,
633 "Tunnel %s state was %s\n",
635 GMT_state2s (t->state));
636 LOG (GNUNET_ERROR_TYPE_DEBUG,
637 "Tunnel %s state is now %s\n",
639 GMT_state2s (state));
645 * Add a connection to a tunnel.
648 * @param c Connection.
651 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
653 struct MeshTConnection *aux;
655 for (aux = t->connection_head; aux != NULL; aux = aux->next)
659 aux = GNUNET_new (struct MeshTConnection);
661 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, aux);
666 * Remove a connection from a tunnel.
669 * @param c Connection.
672 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
674 struct MeshTConnection *aux;
676 for (aux = t->connection_head; aux != NULL; aux = aux->next)
679 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
687 * Add a channel to a tunnel.
693 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
695 struct MeshTChannel *aux;
697 for (aux = t->channel_head; aux != NULL; aux = aux->next)
701 aux = GNUNET_new (struct MeshTChannel);
703 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
708 * Remove a channel from a tunnel.
714 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
716 struct MeshTChannel *aux;
718 for (aux = t->channel_head; aux != NULL; aux = aux->next)
721 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
729 * Search for a channel by global ID.
731 * @param t Tunnel containing the channel.
732 * @param chid Public channel number.
734 * @return channel handler, NULL if doesn't exist
737 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid)
739 struct MeshTChannel *iter;
744 for (iter = t->channel_head; NULL != iter; iter = iter->next)
746 if (GMCH_get_id (iter->ch) == chid)
750 return NULL == iter ? NULL : iter->ch;
755 * Tunnel is empty: destroy it.
757 * Notifies all connections about the destruction.
759 * @param t Tunnel to destroy.
762 GMT_destroy_empty (struct MeshTunnel3 *t)
764 struct MeshTConnection *iter;
766 for (iter = t->connection_head; NULL != iter; iter = iter->next)
768 GMC_send_destroy (iter->c);
771 if (0 == t->pending_messages)
774 t->destroy = GNUNET_YES;
779 * Destroy tunnel if empty (no more channels).
781 * @param t Tunnel to destroy if empty.
784 GMT_destroy_if_empty (struct MeshTunnel3 *t)
786 if (1 < GMT_count_channels (t))
789 GMT_destroy_empty (t);
794 * Destroy the tunnel.
796 * This function does not generate any warning traffic to clients or peers.
799 * Cancel messages belonging to this tunnel queued to neighbors.
800 * Free any allocated resources linked to the tunnel.
802 * @param t The tunnel to destroy.
805 GMT_destroy (struct MeshTunnel3 *t)
807 struct MeshTConnection *iter;
808 struct MeshTConnection *next;
813 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
815 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
818 for (iter = t->connection_head; NULL != iter; iter = next)
821 GMC_destroy (iter->c);
825 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
826 GMP_set_tunnel (t->peer, NULL);
833 * Notifies a tunnel that a connection has broken that affects at least
834 * some of its peers. Sends a notification towards the root of the tree.
835 * In case the peer is the owner of the tree, notifies the client that owns
836 * the tunnel and tries to reconnect.
838 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
840 * @param t Tunnel affected.
841 * @param p1 Peer that got disconnected from p2.
842 * @param p2 Peer that got disconnected from p1.
844 * @return Short ID of the peer disconnected (either p1 or p2).
845 * 0 if the tunnel remained unaffected.
848 GMT_notify_connection_broken (struct MeshTunnel3* t,
849 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
851 // if (myid != p1 && myid != p2) FIXME
856 // if (tree_get_predecessor (t->tree) != 0)
858 // /* We are the peer still connected, notify owner of the disconnection. */
859 // struct GNUNET_MESH_PathBroken msg;
860 // struct GNUNET_PeerIdentity neighbor;
862 // msg.header.size = htons (sizeof (msg));
863 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
864 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
865 // msg.tid = htonl (t->id.tid);
866 // msg.peer1 = my_full_id;
867 // GNUNET_PEER_resolve (pid, &msg.peer2);
868 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
869 // send_prebuilt_message (&msg.header, &neighbor, t);
875 * @brief Use the given path for the tunnel.
876 * Update the next and prev hops (and RCs).
877 * (Re)start the path refresh in case the tunnel is locally owned.
879 * @param t Tunnel to update.
880 * @param p Path to use.
882 * @return Connection created.
884 struct MeshConnection *
885 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p)
887 struct MeshConnection *c;
888 struct GNUNET_HashCode cid;
889 unsigned int own_pos;
891 if (NULL == t || NULL == p)
897 for (own_pos = 0; own_pos < p->length; own_pos++)
899 if (p->peers[own_pos] == my_short_id)
902 if (own_pos > p->length - 1)
908 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
909 c = GMC_new (&cid, t, p, own_pos);
910 GMT_add_connection (t, c);
916 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
917 * Encrypt data with the tunnel key.
920 * @param t Tunnel whose key to use.
921 * @param dst Destination for the encrypted data.
922 * @param src Source of the plaintext.
923 * @param size Size of the plaintext.
924 * @param iv Initialization Vector to use.
925 * @param fwd Is this a fwd message?
928 GMT_encrypt (struct MeshTunnel3 *t,
929 void *dst, const void *src,
930 size_t size, uint64_t iv, int fwd)
932 memcpy (dst, src, size);
937 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
938 * Decrypt data with the tunnel key.
941 * @param t Tunnel whose key to use.
942 * @param dst Destination for the plaintext.
943 * @param src Source of the encrypted data.
944 * @param size Size of the encrypted data.
945 * @param iv Initialization Vector to use.
946 * @param fwd Is this a fwd message?
949 GMT_decrypt (struct MeshTunnel3 *t,
950 void *dst, const void *src,
951 size_t size, uint64_t iv, int fwd)
953 memcpy (dst, src, size);
958 * Count established (ready) connections of a tunnel.
960 * @param t Tunnel on which to count.
962 * @return Number of connections.
965 GMT_count_connections (struct MeshTunnel3 *t)
967 struct MeshTConnection *iter;
970 for (count = 0, iter = t->connection_head;
972 iter = iter->next, count++);
978 * Count channels of a tunnel.
980 * @param t Tunnel on which to count.
982 * @return Number of channels.
985 GMT_count_channels (struct MeshTunnel3 *t)
987 struct MeshTChannel *iter;
990 for (count = 0, iter = t->channel_head;
992 iter = iter->next, count++);
999 * Get the state of a tunnel.
1003 * @return Tunnel's state.
1005 enum MeshTunnelState
1006 GMT_get_state (struct MeshTunnel3 *t)
1012 * Get the total buffer space for a tunnel.
1015 * @param fwd Is this for FWD traffic?
1017 * @return Buffer space offered by all connections in the tunnel.
1020 GMT_get_buffer (struct MeshTunnel3 *t, int fwd)
1022 struct MeshTConnection *iter;
1023 unsigned int buffer;
1025 iter = t->connection_head;
1028 /* If terminal, return biggest channel buffer */
1029 if (NULL == iter || GMC_is_terminal (iter->c, fwd))
1031 struct MeshTChannel *iter_ch;
1032 unsigned int ch_buf;
1034 if (NULL == t->channel_head)
1037 for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = iter_ch->next)
1039 ch_buf = GMCH_get_buffer (iter_ch->ch, fwd);
1040 if (ch_buf > buffer)
1046 /* If not terminal, return sum of connection buffers */
1047 while (NULL != iter)
1049 if (GMC_get_state (iter->c) != MESH_CONNECTION_READY)
1055 buffer += GMC_get_buffer (iter->c, fwd);
1064 * Get the tunnel's destination.
1068 * @return ID of the destination peer.
1070 const struct GNUNET_PeerIdentity *
1071 GMT_get_destination (struct MeshTunnel3 *t)
1073 return GMP_get_id (t->peer);
1079 * Get the tunnel's next free Channel ID.
1083 * @return ID of a channel free to use.
1086 GMT_get_next_chid (struct MeshTunnel3 *t)
1088 MESH_ChannelNumber chid;
1090 while (NULL != GMT_get_channel (t, t->next_chid))
1092 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
1093 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1095 chid = t->next_chid;
1096 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1103 * Sends an already built message on a tunnel, encrypting it and
1104 * choosing the best connection.
1106 * @param message Message to send. Function modifies it.
1107 * @param t Tunnel on which this message is transmitted.
1108 * @param ch Channel on which this message is transmitted.
1109 * @param fwd Is this a fwd message?
1112 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1113 struct MeshTunnel3 *t,
1114 struct MeshChannel *ch,
1117 struct MeshConnection *c;
1118 struct GNUNET_MESH_Encrypted *msg;
1119 size_t size = ntohs (message->size);
1120 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1124 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n", GMP_2s (t->peer));
1126 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1127 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1128 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED);
1129 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1130 msg->iv = GNUNET_htonll (iv);
1131 GMT_encrypt (t, &msg[1], message, size, iv, fwd);
1132 c = tunnel_get_connection (t, fwd);
1135 GNUNET_break (GNUNET_YES == t->destroy);
1138 type = ntohs (message->type);
1141 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1142 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1143 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1144 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1145 msg->cid = *GMC_get_id (c);
1146 msg->ttl = htonl (default_ttl);
1149 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1150 GNUNET_MESH_DEBUG_M2S (type));
1155 GMC_send_prebuilt_message (&msg->header, c, ch, fwd);
1159 * Is the tunnel directed towards the local peer?
1163 * @return GNUNET_YES if it is loopback.
1166 GMT_is_loopback (const struct MeshTunnel3 *t)
1168 return (my_short_id == GMP_get_short_id(t->peer));
1173 * Get the static string for the peer this tunnel is directed.
1177 * @return Static string the destination peer's ID.
1180 GMT_2s (const struct MeshTunnel3 *t)
1182 return GMP_2s (t->peer);