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 MeshTunnel3State 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 * Pending message count.
120 int pending_messages;
123 * Destroy flag: if true, destroy on last message.
128 * Queued messages, to transmit once tunnel gets connected.
130 struct MeshTunnelQueue *tq_head;
131 struct MeshTunnelQueue *tq_tail;
136 * Struct used to queue messages in a tunnel.
138 struct MeshTunnelQueue
143 struct MeshTunnelQueue *next;
144 struct MeshTunnelQueue *prev;
149 struct MeshChannel *ch;
154 /* struct GNUNET_MessageHeader *msg; */
157 /******************************************************************************/
158 /******************************* GLOBALS ***********************************/
159 /******************************************************************************/
162 * Global handle to the statistics service.
164 extern struct GNUNET_STATISTICS_Handle *stats;
167 * Default TTL for payload packets.
169 static unsigned long long default_ttl;
172 * Local peer own ID (memory efficient handle).
174 static GNUNET_PEER_Id my_short_id;
177 * Local peer own ID (full value).
179 const static struct GNUNET_PeerIdentity *my_full_id;
184 const static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
187 /******************************************************************************/
188 /******************************** STATIC ***********************************/
189 /******************************************************************************/
192 * Get string description for tunnel state.
194 * @param s Tunnel state.
196 * @return String representation.
199 GMT_state2s (enum MeshTunnel3State s)
201 static char buf[128];
205 case MESH_TUNNEL3_NEW:
206 return "MESH_TUNNEL3_NEW";
207 case MESH_TUNNEL3_SEARCHING:
208 return "MESH_TUNNEL3_SEARCHING";
209 case MESH_TUNNEL3_WAITING:
210 return "MESH_TUNNEL3_WAITING";
211 case MESH_TUNNEL3_READY:
212 return "MESH_TUNNEL3_READY";
213 case MESH_TUNNEL3_RECONNECTING:
214 return "MESH_TUNNEL3_RECONNECTING";
217 sprintf (buf, "%u (UNKNOWN STATE)", s);
223 * Pick a connection on which send the next data message.
225 * @param t Tunnel on which to send the message.
226 * @param fwd Is this a fwd message?
228 * @return The connection on which to send the next message.
230 static struct MeshConnection *
231 tunnel_get_connection (struct MeshTunnel3 *t, int fwd)
233 struct MeshTConnection *iter;
234 struct MeshConnection *best;
236 unsigned int lowest_q;
238 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GMP_2s (t->peer));
241 for (iter = t->connection_head; NULL != iter; iter = iter->next)
243 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
244 GNUNET_h2s (GMC_get_id (iter->c)), GMC_get_state (iter->c));
245 if (MESH_CONNECTION_READY == GMC_get_state (iter->c))
247 qn = GMC_get_qn (iter->c, fwd);
248 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
261 handle_data (struct MeshTunnel3 *t,
262 const struct GNUNET_MESH_Data *msg,
265 struct MeshChannel *ch;
270 size = ntohs (msg->header.size);
272 sizeof (struct GNUNET_MESH_Data) +
273 sizeof (struct GNUNET_MessageHeader))
278 type = ntohs (msg->header.type);
279 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
280 GNUNET_MESH_DEBUG_M2S (type));
281 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
282 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
285 ch = GMT_get_channel (t, ntohl (msg->chid));
288 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
290 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
295 GMT_change_state (t, MESH_TUNNEL3_READY);
296 GMCH_handle_data (ch, msg, fwd);
300 handle_data_ack (struct MeshTunnel3 *t,
301 const struct GNUNET_MESH_DataACK *msg,
304 struct MeshChannel *ch;
308 size = ntohs (msg->header.size);
309 if (size != sizeof (struct GNUNET_MESH_DataACK))
316 ch = GMT_get_channel (t, ntohl (msg->chid));
319 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
321 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
326 GMCH_handle_data_ack (ch, msg, fwd);
330 handle_ch_create (struct MeshTunnel3 *t,
331 const struct GNUNET_MESH_ChannelCreate *msg,
334 struct MeshTChannel *tch;
335 struct MeshChannel *ch;
339 size = ntohs (msg->header.size);
340 if (size != sizeof (struct GNUNET_MESH_ChannelCreate))
347 ch = GMT_get_channel (t, ntohl (msg->chid));
350 /* Probably a retransmission, safe to ignore */
351 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
355 ch = GMCH_handle_create (msg, fwd);
358 tch = GNUNET_new (struct MeshTChannel);
360 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, tch);
364 handle_ch_ack (struct MeshTunnel3 *t,
365 const struct GNUNET_MESH_ChannelManage *msg,
368 struct MeshChannel *ch;
372 size = ntohs (msg->header.size);
373 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
380 ch = GMT_get_channel (t, ntohl (msg->chid));
383 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
385 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
390 GMCH_handle_ack (ch, msg, fwd);
394 handle_ch_destroy (struct MeshTunnel3 *t,
395 const struct GNUNET_MESH_ChannelManage *msg,
398 struct MeshChannel *ch;
402 size = ntohs (msg->header.size);
403 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
410 ch = GMT_get_channel (t, ntohl (msg->chid));
413 /* Probably a retransmission, safe to ignore */
417 GMCH_handle_destroy (ch, msg, fwd);
420 /******************************************************************************/
421 /******************************** API ***********************************/
422 /******************************************************************************/
425 * Demultiplex by message type and call appropriate handler for a message
426 * towards a channel of a local tunnel.
428 * @param t Tunnel this message came on.
429 * @param msgh Message header.
430 * @param fwd Is this message fwd?
433 GMT_handle_decrypted (struct MeshTunnel3 *t,
434 const struct GNUNET_MessageHeader *msgh,
439 type = ntohs (msgh->type);
440 LOG (GNUNET_ERROR_TYPE_DEBUG,
441 "Got a %s message!\n",
442 GNUNET_MESH_DEBUG_M2S (type));
446 case GNUNET_MESSAGE_TYPE_MESH_DATA:
447 /* Don't send hop ACK, wait for client to ACK */
448 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
451 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
452 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
455 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
457 (struct GNUNET_MESH_ChannelCreate *) msgh,
461 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
463 (struct GNUNET_MESH_ChannelManage *) msgh,
467 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
468 handle_ch_destroy (t,
469 (struct GNUNET_MESH_ChannelManage *) msgh,
475 LOG (GNUNET_ERROR_TYPE_DEBUG,
476 "end-to-end message not known (%u)\n",
483 * Cache a message to be sent once tunnel is online.
485 * @param t Tunnel to hold the message.
486 * @param ch Channel the message is about.
487 * @param msg Message itself (copy will be made).
488 * @param fwd Is this fwd?
491 GMT_queue_data (struct MeshTunnel3 *t,
492 struct MeshChannel *ch,
493 struct GNUNET_MessageHeader *msg,
496 struct MeshTunnelQueue *tq;
497 uint16_t size = ntohs (msg->size);
499 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
502 memcpy (&tq[1], msg, size);
503 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
505 if (MESH_TUNNEL3_READY == t->state)
506 GMT_send_queued_data (t, fwd);
511 * Send all cached messages that we can, tunnel is online.
513 * @param t Tunnel that holds the messages.
514 * @param fwd Is this fwd?
517 GMT_send_queued_data (struct MeshTunnel3 *t, int fwd)
519 struct MeshTunnelQueue *tq;
520 struct MeshTunnelQueue *next;
523 LOG (GNUNET_ERROR_TYPE_DEBUG,
524 "GMT_send_queued_data on tunnel %s\n",
526 room = GMT_get_buffer (t, fwd);
527 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
528 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
532 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
533 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
542 * Initialize the tunnel subsystem.
544 * @param c Configuration handle.
545 * @param id Peer identity.
546 * @param key ECC private key, to derive all other keys and do crypto.
549 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
550 const struct GNUNET_PeerIdentity *id,
551 const struct GNUNET_CRYPTO_EccPrivateKey *key)
554 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
557 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
558 "MESH", "DEFAULT_TTL", "USING DEFAULT");
562 my_private_key = key;
563 my_short_id = GNUNET_PEER_intern (my_full_id);
568 * Shut down the tunnel subsystem.
573 GNUNET_PEER_change_rc (my_short_id, -1);
583 struct MeshTunnel3 *t;
585 t = GNUNET_new (struct MeshTunnel3);
588 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
589 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
592 // tunnel_destroy (t);
596 // char salt[] = "salt";
597 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
598 // salt, sizeof (salt),
599 // &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
600 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
601 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
603 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
604 // salt, sizeof (salt),
605 // &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
606 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
607 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
616 * Change the tunnel state.
618 * @param t Tunnel whose state to change.
619 * @param state New state.
622 GMT_change_state (struct MeshTunnel3* t, enum MeshTunnel3State state)
626 LOG (GNUNET_ERROR_TYPE_DEBUG,
627 "Tunnel %s state was %s\n",
629 GMT_state2s (t->state));
630 LOG (GNUNET_ERROR_TYPE_DEBUG,
631 "Tunnel %s state is now %s\n",
633 GMT_state2s (state));
635 if (MESH_TUNNEL3_READY == state && 3 <= GMT_count_connections (t))
637 GMP_stop_search (t->peer);
643 * Add a connection to a tunnel.
646 * @param c Connection.
649 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
651 struct MeshTConnection *aux;
653 for (aux = t->connection_head; aux != NULL; aux = aux->next)
657 aux = GNUNET_new (struct MeshTConnection);
659 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, aux);
664 * Remove a connection from a tunnel.
667 * @param c Connection.
670 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
672 struct MeshTConnection *aux;
674 for (aux = t->connection_head; aux != NULL; aux = aux->next)
677 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
685 * Add a channel to a tunnel.
691 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
693 struct MeshTChannel *aux;
695 for (aux = t->channel_head; aux != NULL; aux = aux->next)
699 aux = GNUNET_new (struct MeshTChannel);
701 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
706 * Remove a channel from a tunnel.
712 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
714 struct MeshTChannel *aux;
716 for (aux = t->channel_head; aux != NULL; aux = aux->next)
719 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
727 * Search for a channel by global ID.
729 * @param t Tunnel containing the channel.
730 * @param chid Public channel number.
732 * @return channel handler, NULL if doesn't exist
735 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid)
737 struct MeshTChannel *iter;
742 for (iter = t->channel_head; NULL != iter; iter = iter->next)
744 if (GMCH_get_id (iter->ch) == chid)
748 return NULL == iter ? NULL : iter->ch;
753 * Tunnel is empty: destroy it.
755 * Notifies all connections about the destruction.
757 * @param t Tunnel to destroy.
760 GMT_destroy_empty (struct MeshTunnel3 *t)
762 struct MeshTConnection *iter;
764 for (iter = t->connection_head; NULL != iter; iter = iter->next)
766 GMC_send_destroy (iter->c);
769 if (0 == t->pending_messages)
772 t->destroy = GNUNET_YES;
777 * Destroy tunnel if empty (no more channels).
779 * @param t Tunnel to destroy if empty.
782 GMT_destroy_if_empty (struct MeshTunnel3 *t)
784 if (1 < GMT_count_channels (t))
787 GMT_destroy_empty (t);
792 * Destroy the tunnel.
794 * This function does not generate any warning traffic to clients or peers.
797 * Cancel messages belonging to this tunnel queued to neighbors.
798 * Free any allocated resources linked to the tunnel.
800 * @param t The tunnel to destroy.
803 GMT_destroy (struct MeshTunnel3 *t)
805 struct MeshTConnection *iter;
806 struct MeshTConnection *next;
811 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
813 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
816 for (iter = t->connection_head; NULL != iter; iter = next)
819 GMC_destroy (iter->c);
823 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
824 GMP_set_tunnel (t->peer, NULL);
831 * Notifies a tunnel that a connection has broken that affects at least
832 * some of its peers. Sends a notification towards the root of the tree.
833 * In case the peer is the owner of the tree, notifies the client that owns
834 * the tunnel and tries to reconnect.
836 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
838 * @param t Tunnel affected.
839 * @param p1 Peer that got disconnected from p2.
840 * @param p2 Peer that got disconnected from p1.
842 * @return Short ID of the peer disconnected (either p1 or p2).
843 * 0 if the tunnel remained unaffected.
846 GMT_notify_connection_broken (struct MeshTunnel3* t,
847 GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
849 // if (myid != p1 && myid != p2) FIXME
854 // if (tree_get_predecessor (t->tree) != 0)
856 // /* We are the peer still connected, notify owner of the disconnection. */
857 // struct GNUNET_MESH_PathBroken msg;
858 // struct GNUNET_PeerIdentity neighbor;
860 // msg.header.size = htons (sizeof (msg));
861 // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
862 // GNUNET_PEER_resolve (t->id.oid, &msg.oid);
863 // msg.tid = htonl (t->id.tid);
864 // msg.peer1 = my_full_id;
865 // GNUNET_PEER_resolve (pid, &msg.peer2);
866 // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
867 // send_prebuilt_message (&msg.header, &neighbor, t);
873 * @brief Use the given path for the tunnel.
874 * Update the next and prev hops (and RCs).
875 * (Re)start the path refresh in case the tunnel is locally owned.
877 * @param t Tunnel to update.
878 * @param p Path to use.
880 * @return Connection created.
882 struct MeshConnection *
883 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p)
885 struct MeshConnection *c;
886 struct GNUNET_HashCode cid;
887 unsigned int own_pos;
889 if (NULL == t || NULL == p)
895 for (own_pos = 0; own_pos < p->length; own_pos++)
897 if (p->peers[own_pos] == my_short_id)
900 if (own_pos > p->length - 1)
906 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
907 c = GMC_new (&cid, t, p, own_pos);
908 GMT_add_connection (t, c);
914 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
915 * Encrypt data with the tunnel key.
918 * @param t Tunnel whose key to use.
919 * @param dst Destination for the encrypted data.
920 * @param src Source of the plaintext.
921 * @param size Size of the plaintext.
922 * @param iv Initialization Vector to use.
923 * @param fwd Is this a fwd message?
926 GMT_encrypt (struct MeshTunnel3 *t,
927 void *dst, const void *src,
928 size_t size, uint64_t iv, int fwd)
930 memcpy (dst, src, size);
935 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
936 * Decrypt data with the tunnel key.
939 * @param t Tunnel whose key to use.
940 * @param dst Destination for the plaintext.
941 * @param src Source of the encrypted data.
942 * @param size Size of the encrypted data.
943 * @param iv Initialization Vector to use.
944 * @param fwd Is this a fwd message?
947 GMT_decrypt (struct MeshTunnel3 *t,
948 void *dst, const void *src,
949 size_t size, uint64_t iv, int fwd)
951 memcpy (dst, src, size);
956 * Count established (ready) connections of a tunnel.
958 * @param t Tunnel on which to count.
960 * @return Number of connections.
963 GMT_count_connections (struct MeshTunnel3 *t)
965 struct MeshTConnection *iter;
968 for (count = 0, iter = t->connection_head;
970 iter = iter->next, count++);
976 * Count channels of a tunnel.
978 * @param t Tunnel on which to count.
980 * @return Number of channels.
983 GMT_count_channels (struct MeshTunnel3 *t)
985 struct MeshTChannel *iter;
988 for (count = 0, iter = t->channel_head;
990 iter = iter->next, count++);
997 * Get the state of a tunnel.
1001 * @return Tunnel's state.
1003 enum MeshTunnel3State
1004 GMT_get_state (struct MeshTunnel3 *t)
1007 return (enum MeshTunnel3State) -1;
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);
1078 * Get the tunnel's next free global channel ID.
1082 * @return GID of a channel free to use.
1085 GMT_get_next_chid (struct MeshTunnel3 *t)
1087 MESH_ChannelNumber chid;
1089 while (NULL != GMT_get_channel (t, t->next_chid))
1091 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
1092 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1094 chid = t->next_chid;
1095 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1102 * Sends an already built message on a tunnel, encrypting it and
1103 * choosing the best connection.
1105 * @param message Message to send. Function modifies it.
1106 * @param t Tunnel on which this message is transmitted.
1107 * @param ch Channel on which this message is transmitted.
1108 * @param fwd Is this a fwd message?
1111 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1112 struct MeshTunnel3 *t,
1113 struct MeshChannel *ch,
1116 struct MeshConnection *c;
1117 struct GNUNET_MESH_Encrypted *msg;
1118 size_t size = ntohs (message->size);
1119 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1123 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n", GMP_2s (t->peer));
1125 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1126 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1127 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED);
1128 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1129 msg->iv = GNUNET_htonll (iv);
1130 GMT_encrypt (t, &msg[1], message, size, iv, fwd);
1131 c = tunnel_get_connection (t, fwd);
1134 GNUNET_break (GNUNET_YES == t->destroy);
1137 type = ntohs (message->type);
1140 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1141 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1142 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1143 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1144 msg->cid = *GMC_get_id (c);
1145 msg->ttl = htonl (default_ttl);
1148 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1149 GNUNET_MESH_DEBUG_M2S (type));
1154 GMC_send_prebuilt_message (&msg->header, c, ch, fwd);
1158 * Is the tunnel directed towards the local peer?
1162 * @return GNUNET_YES if it is loopback.
1165 GMT_is_loopback (const struct MeshTunnel3 *t)
1167 return (my_short_id == GMP_get_short_id(t->peer));
1172 * Get the static string for the peer this tunnel is directed.
1176 * @return Static string the destination peer's ID.
1179 GMT_2s (const struct MeshTunnel3 *t)
1181 return GMP_2s (t->peer);