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_EddsaPrivateKey *my_eph_key;
76 * Local peer ephemeral public key
78 struct GNUNET_CRYPTO_EddsaPublicKey *my_eph;
81 * Remote peer's public key.
83 struct GNUNET_CRYPTO_EddsaPublicKey *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 * Local peer own ID (memory efficient handle).
169 extern GNUNET_PEER_Id myid;
172 * Local peer own ID (full value).
174 extern struct GNUNET_PeerIdentity my_full_id;
177 * Default TTL for payload packets.
179 static unsigned long long default_ttl;
184 const static struct GNUNET_CRYPTO_EddsaPrivateKey *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 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
262 * Encrypt data with the tunnel key.
264 * @param t Tunnel whose key to use.
265 * @param dst Destination for the GMT_encrypted data.
266 * @param src Source of the plaintext.
267 * @param size Size of the plaintext.
268 * @param iv Initialization Vector to use.
269 * @param fwd Is this a fwd message?
272 GMT_encrypt (struct MeshTunnel3 *t,
273 void *dst, const void *src,
274 size_t size, uint64_t iv, int fwd)
276 memcpy (dst, src, size);
281 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
282 * Decrypt data with the tunnel key.
284 * @param t Tunnel whose key to use.
285 * @param dst Destination for the plaintext.
286 * @param src Source of the GMT_encrypted data.
287 * @param size Size of the GMT_encrypted data.
288 * @param iv Initialization Vector to use.
289 * @param fwd Is this a fwd message?
292 GMT_decrypt (struct MeshTunnel3 *t,
293 void *dst, const void *src,
294 size_t size, uint64_t iv, int fwd)
296 memcpy (dst, src, size);
301 handle_data (struct MeshTunnel3 *t,
302 const struct GNUNET_MESH_Data *msg,
305 struct MeshChannel *ch;
310 size = ntohs (msg->header.size);
312 sizeof (struct GNUNET_MESH_Data) +
313 sizeof (struct GNUNET_MessageHeader))
318 type = ntohs (msg->header.type);
319 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
320 GNUNET_MESH_DEBUG_M2S (type));
321 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
322 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
325 ch = GMT_get_channel (t, ntohl (msg->chid));
328 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
330 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
335 GMT_change_state (t, MESH_TUNNEL3_READY);
336 GMCH_handle_data (ch, msg, fwd);
340 handle_data_ack (struct MeshTunnel3 *t,
341 const struct GNUNET_MESH_DataACK *msg,
344 struct MeshChannel *ch;
348 size = ntohs (msg->header.size);
349 if (size != sizeof (struct GNUNET_MESH_DataACK))
356 ch = GMT_get_channel (t, ntohl (msg->chid));
359 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
361 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
366 GMCH_handle_data_ack (ch, msg, fwd);
370 handle_ch_create (struct MeshTunnel3 *t,
371 const struct GNUNET_MESH_ChannelCreate *msg,
374 struct MeshTChannel *tch;
375 struct MeshChannel *ch;
379 size = ntohs (msg->header.size);
380 if (size != sizeof (struct GNUNET_MESH_ChannelCreate))
387 ch = GMT_get_channel (t, ntohl (msg->chid));
390 /* Probably a retransmission, safe to ignore */
391 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
395 ch = GMCH_handle_create (msg, fwd);
398 tch = GNUNET_new (struct MeshTChannel);
400 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, tch);
404 handle_ch_ack (struct MeshTunnel3 *t,
405 const struct GNUNET_MESH_ChannelManage *msg,
408 struct MeshChannel *ch;
412 size = ntohs (msg->header.size);
413 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
420 ch = GMT_get_channel (t, ntohl (msg->chid));
423 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
425 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
430 GMCH_handle_ack (ch, msg, fwd);
434 handle_ch_destroy (struct MeshTunnel3 *t,
435 const struct GNUNET_MESH_ChannelManage *msg,
438 struct MeshChannel *ch;
442 size = ntohs (msg->header.size);
443 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
450 ch = GMT_get_channel (t, ntohl (msg->chid));
453 /* Probably a retransmission, safe to ignore */
457 GMCH_handle_destroy (ch, msg, fwd);
462 * Demultiplex by message type and call appropriate handler for a message
463 * towards a channel of a local tunnel.
465 * @param t Tunnel this message came on.
466 * @param msgh Message header.
467 * @param fwd Is this message fwd?
470 handle_GMT_decrypted (struct MeshTunnel3 *t,
471 const struct GNUNET_MessageHeader *msgh,
476 type = ntohs (msgh->type);
477 LOG (GNUNET_ERROR_TYPE_DEBUG,
478 "Got a %s message!\n",
479 GNUNET_MESH_DEBUG_M2S (type));
483 case GNUNET_MESSAGE_TYPE_MESH_DATA:
484 /* Don't send hop ACK, wait for client to ACK */
485 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
488 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
489 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
492 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
494 (struct GNUNET_MESH_ChannelCreate *) msgh,
498 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
500 (struct GNUNET_MESH_ChannelManage *) msgh,
504 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
505 handle_ch_destroy (t,
506 (struct GNUNET_MESH_ChannelManage *) msgh,
512 LOG (GNUNET_ERROR_TYPE_DEBUG,
513 "end-to-end message not known (%u)\n",
518 /******************************************************************************/
519 /******************************** API ***********************************/
520 /******************************************************************************/
524 * Decrypt and demultiplex by message type. Call appropriate handler
527 * @param t Tunnel this message came on.
528 * @param msgh Encrypted message.
529 * @param fwd Is this message fwd?
532 GMT_handle_encrypted (struct MeshTunnel3 *t,
533 const struct GNUNET_MESH_Encrypted *msg,
536 size_t size = ntohs (msg->header.size);
537 size_t payload_size = size - sizeof (struct GNUNET_MESH_Encrypted);
538 char cbuf[payload_size];
539 struct GNUNET_MessageHeader *msgh;
542 GMT_decrypt (t, cbuf, &msg[1], payload_size, msg->iv, fwd);
544 while (off < payload_size)
546 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
547 handle_GMT_decrypted (t, msgh, fwd);
548 off += ntohs (msgh->size);
554 * Cache a message to be sent once tunnel is online.
556 * @param t Tunnel to hold the message.
557 * @param ch Channel the message is about.
558 * @param msg Message itself (copy will be made).
559 * @param fwd Is this fwd?
562 GMT_queue_data (struct MeshTunnel3 *t,
563 struct MeshChannel *ch,
564 struct GNUNET_MessageHeader *msg,
567 struct MeshTunnelQueue *tq;
568 uint16_t size = ntohs (msg->size);
570 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
573 memcpy (&tq[1], msg, size);
574 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
576 if (MESH_TUNNEL3_READY == t->state)
577 GMT_send_queued_data (t, fwd);
582 * Send all cached messages that we can, tunnel is online.
584 * @param t Tunnel that holds the messages.
585 * @param fwd Is this fwd?
588 GMT_send_queued_data (struct MeshTunnel3 *t, int fwd)
590 struct MeshTunnelQueue *tq;
591 struct MeshTunnelQueue *next;
594 LOG (GNUNET_ERROR_TYPE_DEBUG,
595 "GMT_send_queued_data on tunnel %s\n",
597 room = GMT_get_buffer (t, fwd);
598 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
599 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
603 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
604 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
613 * Initialize the tunnel subsystem.
615 * @param c Configuration handle.
616 * @param id Peer identity.
617 * @param key ECC private key, to derive all other keys and do crypto.
620 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
621 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
624 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
627 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
628 "MESH", "DEFAULT_TTL", "USING DEFAULT");
631 my_private_key = key;
636 * Shut down the tunnel subsystem.
641 GNUNET_PEER_change_rc (myid, -1);
648 * @param destination Peer this tunnel is towards.
651 GMT_new (struct MeshPeer *destination)
653 struct MeshTunnel3 *t;
655 t = GNUNET_new (struct MeshTunnel3);
657 t->peer = destination;
659 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
660 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
663 // tunnel_destroy (t);
667 // char salt[] = "salt";
668 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
669 // salt, sizeof (salt),
670 // &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
671 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
672 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
674 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
675 // salt, sizeof (salt),
676 // &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
677 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
678 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
686 * Change the tunnel state.
688 * @param t Tunnel whose state to change.
689 * @param state New state.
692 GMT_change_state (struct MeshTunnel3* t, enum MeshTunnel3State state)
696 LOG (GNUNET_ERROR_TYPE_DEBUG,
697 "Tunnel %s state was %s\n",
699 GMT_state2s (t->state));
700 LOG (GNUNET_ERROR_TYPE_DEBUG,
701 "Tunnel %s state is now %s\n",
703 GMT_state2s (state));
705 if (MESH_TUNNEL3_READY == state && 3 <= GMT_count_connections (t))
707 GMP_stop_search (t->peer);
713 * Add a connection to a tunnel.
716 * @param c Connection.
719 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
721 struct MeshTConnection *aux;
723 for (aux = t->connection_head; aux != NULL; aux = aux->next)
727 aux = GNUNET_new (struct MeshTConnection);
729 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, aux);
734 * Remove a connection from a tunnel.
737 * @param c Connection.
740 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
742 struct MeshTConnection *aux;
744 for (aux = t->connection_head; aux != NULL; aux = aux->next)
747 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
755 * Add a channel to a tunnel.
761 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
763 struct MeshTChannel *aux;
765 for (aux = t->channel_head; aux != NULL; aux = aux->next)
769 aux = GNUNET_new (struct MeshTChannel);
771 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
776 * Remove a channel from a tunnel.
782 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
784 struct MeshTChannel *aux;
786 for (aux = t->channel_head; aux != NULL; aux = aux->next)
789 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
797 * Search for a channel by global ID.
799 * @param t Tunnel containing the channel.
800 * @param chid Public channel number.
802 * @return channel handler, NULL if doesn't exist
805 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid)
807 struct MeshTChannel *iter;
812 for (iter = t->channel_head; NULL != iter; iter = iter->next)
814 if (GMCH_get_id (iter->ch) == chid)
818 return NULL == iter ? NULL : iter->ch;
823 * Tunnel is empty: destroy it.
825 * Notifies all connections about the destruction.
827 * @param t Tunnel to destroy.
830 GMT_destroy_empty (struct MeshTunnel3 *t)
832 struct MeshTConnection *iter;
834 for (iter = t->connection_head; NULL != iter; iter = iter->next)
836 GMC_send_destroy (iter->c);
839 if (0 == t->pending_messages)
842 t->destroy = GNUNET_YES;
847 * Destroy tunnel if empty (no more channels).
849 * @param t Tunnel to destroy if empty.
852 GMT_destroy_if_empty (struct MeshTunnel3 *t)
854 if (1 < GMT_count_channels (t))
857 GMT_destroy_empty (t);
862 * Destroy the tunnel.
864 * This function does not generate any warning traffic to clients or peers.
867 * Cancel messages belonging to this tunnel queued to neighbors.
868 * Free any allocated resources linked to the tunnel.
870 * @param t The tunnel to destroy.
873 GMT_destroy (struct MeshTunnel3 *t)
875 struct MeshTConnection *iter;
876 struct MeshTConnection *next;
881 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
883 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
886 for (iter = t->connection_head; NULL != iter; iter = next)
889 GMC_destroy (iter->c);
893 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
894 GMP_set_tunnel (t->peer, NULL);
901 * @brief Use the given path for the tunnel.
902 * Update the next and prev hops (and RCs).
903 * (Re)start the path refresh in case the tunnel is locally owned.
905 * @param t Tunnel to update.
906 * @param p Path to use.
908 * @return Connection created.
910 struct MeshConnection *
911 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p)
913 struct MeshConnection *c;
914 struct GNUNET_HashCode cid;
915 unsigned int own_pos;
917 if (NULL == t || NULL == p)
923 for (own_pos = 0; own_pos < p->length; own_pos++)
925 if (p->peers[own_pos] == myid)
928 if (own_pos > p->length - 1)
934 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
935 c = GMC_new (&cid, t, p, own_pos);
936 GMT_add_connection (t, c);
942 * Count established (ready) connections of a tunnel.
944 * @param t Tunnel on which to count.
946 * @return Number of connections.
949 GMT_count_connections (struct MeshTunnel3 *t)
951 struct MeshTConnection *iter;
954 for (count = 0, iter = t->connection_head;
956 iter = iter->next, count++);
962 * Count channels of a tunnel.
964 * @param t Tunnel on which to count.
966 * @return Number of channels.
969 GMT_count_channels (struct MeshTunnel3 *t)
971 struct MeshTChannel *iter;
974 for (count = 0, iter = t->channel_head;
976 iter = iter->next, count++);
983 * Get the state of a tunnel.
987 * @return Tunnel's state.
989 enum MeshTunnel3State
990 GMT_get_state (struct MeshTunnel3 *t)
993 return (enum MeshTunnel3State) -1;
998 * Get the total buffer space for a tunnel.
1001 * @param fwd Is this for FWD traffic?
1003 * @return Buffer space offered by all connections in the tunnel.
1006 GMT_get_buffer (struct MeshTunnel3 *t, int fwd)
1008 struct MeshTConnection *iter;
1009 unsigned int buffer;
1011 iter = t->connection_head;
1014 /* If terminal, return biggest channel buffer */
1015 if (NULL == iter || GMC_is_terminal (iter->c, fwd))
1017 struct MeshTChannel *iter_ch;
1018 unsigned int ch_buf;
1020 if (NULL == t->channel_head)
1023 for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = iter_ch->next)
1025 ch_buf = GMCH_get_buffer (iter_ch->ch, fwd);
1026 if (ch_buf > buffer)
1032 /* If not terminal, return sum of connection buffers */
1033 while (NULL != iter)
1035 if (GMC_get_state (iter->c) != MESH_CONNECTION_READY)
1041 buffer += GMC_get_buffer (iter->c, fwd);
1050 * Get the tunnel's destination.
1054 * @return ID of the destination peer.
1056 const struct GNUNET_PeerIdentity *
1057 GMT_get_destination (struct MeshTunnel3 *t)
1059 return GMP_get_id (t->peer);
1064 * Get the tunnel's next free global channel ID.
1068 * @return GID of a channel free to use.
1071 GMT_get_next_chid (struct MeshTunnel3 *t)
1073 MESH_ChannelNumber chid;
1075 while (NULL != GMT_get_channel (t, t->next_chid))
1077 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid);
1078 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1080 chid = t->next_chid;
1081 t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
1088 * Sends an already built message on a tunnel, GMT_encrypting it and
1089 * choosing the best connection.
1091 * @param message Message to send. Function modifies it.
1092 * @param t Tunnel on which this message is transmitted.
1093 * @param ch Channel on which this message is transmitted.
1094 * @param fwd Is this a fwd message?
1097 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1098 struct MeshTunnel3 *t,
1099 struct MeshChannel *ch,
1102 struct MeshConnection *c;
1103 struct GNUNET_MESH_Encrypted *msg;
1104 size_t size = ntohs (message->size);
1105 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1109 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n", GMP_2s (t->peer));
1111 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1112 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1113 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED);
1114 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1115 msg->iv = GNUNET_htonll (iv);
1116 GMT_encrypt (t, &msg[1], message, size, iv, fwd);
1117 c = tunnel_get_connection (t, fwd);
1120 GNUNET_break (GNUNET_YES == t->destroy);
1123 type = ntohs (message->type);
1126 case GNUNET_MESSAGE_TYPE_MESH_FWD:
1127 case GNUNET_MESSAGE_TYPE_MESH_BCK:
1128 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1129 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1130 msg->cid = *GMC_get_id (c);
1131 msg->ttl = htonl (default_ttl);
1134 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1135 GNUNET_MESH_DEBUG_M2S (type));
1140 t->pending_messages++;
1141 GMC_send_prebuilt_message (&msg->header, c, ch, fwd);
1145 * Is the tunnel directed towards the local peer?
1149 * @return GNUNET_YES if it is loopback.
1152 GMT_is_loopback (const struct MeshTunnel3 *t)
1154 return (myid == GMP_get_short_id(t->peer));
1159 * Is the tunnel using this path already?
1164 * @return GNUNET_YES a connection uses this path.
1167 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p)
1169 struct MeshTConnection *iter;
1171 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1172 if (GMC_get_path (iter->c) == p)
1180 * Get a cost of a path for a tunnel considering existing connections.
1183 * @param path Candidate path.
1185 * @return Cost of the path (path length + number of overlapping nodes)
1188 GMT_get_path_cost (const struct MeshTunnel3 *t,
1189 const struct MeshPeerPath *path)
1191 struct MeshTConnection *iter;
1192 unsigned int overlap;
1200 GNUNET_assert (NULL != t);
1202 for (i = 0; i < path->length; i++)
1204 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1206 for (j = 0; j < GMC_get_path (iter->c)->length; j++)
1208 if (path->peers[i] == GMC_get_path (iter->c)->peers[j])
1216 return (path->length + overlap) * (path->score * -1);
1221 * Get the static string for the peer this tunnel is directed.
1225 * @return Static string the destination peer's ID.
1228 GMT_2s (const struct MeshTunnel3 *t)
1230 return GMP_2s (t->peer);