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__)
36 #define START_FUNCTION LOG(GNUNET_ERROR_TYPE_DEBUG, "%s start\n", __FUNCTION__)
37 #define END_FUNCTION LOG(GNUNET_ERROR_TYPE_DEBUG, "%s end\n", __FUNCTION__)
40 /******************************************************************************/
41 /******************************** STRUCTS **********************************/
42 /******************************************************************************/
46 struct MeshTChannel *next;
47 struct MeshTChannel *prev;
48 struct MeshChannel *ch;
51 struct MeshTConnection
53 struct MeshTConnection *next;
54 struct MeshTConnection *prev;
55 struct MeshConnection *c;
59 * Struct containing all information regarding a tunnel to a peer.
64 * Endpoint of the tunnel.
66 struct MeshPeer *peer;
69 * State of the tunnel.
71 enum MeshTunnel3State state;
74 * Local peer ephemeral private key
76 struct GNUNET_CRYPTO_EddsaPrivateKey *my_eph_key;
79 * Local peer ephemeral public key
81 struct GNUNET_CRYPTO_EddsaPublicKey *my_eph;
84 * Remote peer's public key.
86 struct GNUNET_CRYPTO_EddsaPublicKey *peers_eph;
89 * Encryption ("our") key.
91 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
94 * Decryption ("their") key.
96 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
99 * Paths that are actively used to reach the destination peer.
101 struct MeshTConnection *connection_head;
102 struct MeshTConnection *connection_tail;
105 * Next connection number.
110 * Channels inside this tunnel.
112 struct MeshTChannel *channel_head;
113 struct MeshTChannel *channel_tail;
116 * Channel ID for the next created channel.
118 MESH_ChannelNumber next_chid;
121 * Pending message count.
123 int pending_messages;
126 * Destroy flag: if true, destroy on last message.
131 * Queued messages, to transmit once tunnel gets connected.
133 struct MeshTunnelQueue *tq_head;
134 struct MeshTunnelQueue *tq_tail;
139 * Struct used to queue messages in a tunnel.
141 struct MeshTunnelQueue
146 struct MeshTunnelQueue *next;
147 struct MeshTunnelQueue *prev;
152 struct MeshChannel *ch;
157 /* struct GNUNET_MessageHeader *msg; */
160 /******************************************************************************/
161 /******************************* GLOBALS ***********************************/
162 /******************************************************************************/
165 * Global handle to the statistics service.
167 extern struct GNUNET_STATISTICS_Handle *stats;
170 * Local peer own ID (memory efficient handle).
172 extern GNUNET_PEER_Id myid;
175 * Local peer own ID (full value).
177 extern struct GNUNET_PeerIdentity my_full_id;
180 * Default TTL for payload packets.
182 static unsigned long long default_ttl;
187 const static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
190 /******************************************************************************/
191 /******************************** STATIC ***********************************/
192 /******************************************************************************/
195 * Get string description for tunnel state.
197 * @param s Tunnel state.
199 * @return String representation.
202 GMT_state2s (enum MeshTunnel3State s)
204 static char buf[128];
208 case MESH_TUNNEL3_NEW:
209 return "MESH_TUNNEL3_NEW";
210 case MESH_TUNNEL3_SEARCHING:
211 return "MESH_TUNNEL3_SEARCHING";
212 case MESH_TUNNEL3_WAITING:
213 return "MESH_TUNNEL3_WAITING";
214 case MESH_TUNNEL3_READY:
215 return "MESH_TUNNEL3_READY";
216 case MESH_TUNNEL3_RECONNECTING:
217 return "MESH_TUNNEL3_RECONNECTING";
220 sprintf (buf, "%u (UNKNOWN STATE)", s);
226 * Pick a connection on which send the next data message.
228 * @param t Tunnel on which to send the message.
229 * @param fwd Is this a fwd message?
231 * @return The connection on which to send the next message.
233 static struct MeshConnection *
234 tunnel_get_connection (struct MeshTunnel3 *t, int fwd)
236 struct MeshTConnection *iter;
237 struct MeshConnection *best;
239 unsigned int lowest_q;
241 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n", GMP_2s (t->peer));
244 for (iter = t->connection_head; NULL != iter; iter = iter->next)
246 LOG (GNUNET_ERROR_TYPE_DEBUG, " connection %s: %u\n",
247 GNUNET_h2s (GMC_get_id (iter->c)), GMC_get_state (iter->c));
248 if (MESH_CONNECTION_READY == GMC_get_state (iter->c))
250 qn = GMC_get_qn (iter->c, fwd);
251 LOG (GNUNET_ERROR_TYPE_DEBUG, " q_n %u, \n", qn);
264 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
265 * Encrypt data with the tunnel key.
267 * @param t Tunnel whose key to use.
268 * @param dst Destination for the GMT_encrypted data.
269 * @param src Source of the plaintext.
270 * @param size Size of the plaintext.
271 * @param iv Initialization Vector to use.
272 * @param fwd Is this a fwd message?
275 GMT_encrypt (struct MeshTunnel3 *t,
276 void *dst, const void *src,
277 size_t size, uint64_t iv, int fwd)
279 memcpy (dst, src, size);
284 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
285 * Decrypt data with the tunnel key.
287 * @param t Tunnel whose key to use.
288 * @param dst Destination for the plaintext.
289 * @param src Source of the GMT_encrypted data.
290 * @param size Size of the GMT_encrypted data.
291 * @param iv Initialization Vector to use.
292 * @param fwd Is this a fwd message?
295 GMT_decrypt (struct MeshTunnel3 *t,
296 void *dst, const void *src,
297 size_t size, uint64_t iv, int fwd)
299 memcpy (dst, src, size);
304 handle_data (struct MeshTunnel3 *t,
305 const struct GNUNET_MESH_Data *msg,
308 struct MeshChannel *ch;
313 size = ntohs (msg->header.size);
315 sizeof (struct GNUNET_MESH_Data) +
316 sizeof (struct GNUNET_MessageHeader))
321 type = ntohs (msg->header.type);
322 LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message\n",
323 GNUNET_MESH_DEBUG_M2S (type));
324 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
325 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
328 ch = GMT_get_channel (t, ntohl (msg->chid));
331 GNUNET_STATISTICS_update (stats, "# data on unknown channel",
333 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
338 GMT_change_state (t, MESH_TUNNEL3_READY);
339 GMCH_handle_data (ch, msg, fwd);
343 handle_data_ack (struct MeshTunnel3 *t,
344 const struct GNUNET_MESH_DataACK *msg,
347 struct MeshChannel *ch;
351 size = ntohs (msg->header.size);
352 if (size != sizeof (struct GNUNET_MESH_DataACK))
359 ch = GMT_get_channel (t, ntohl (msg->chid));
362 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
364 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
369 GMCH_handle_data_ack (ch, msg, fwd);
373 handle_ch_create (struct MeshTunnel3 *t,
374 const struct GNUNET_MESH_ChannelCreate *msg,
379 struct MeshTChannel *tch;
380 struct MeshChannel *ch;
384 size = ntohs (msg->header.size);
385 if (size != sizeof (struct GNUNET_MESH_ChannelCreate))
392 ch = GMT_get_channel (t, ntohl (msg->chid));
395 /* Probably a retransmission, safe to ignore */
396 LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists...\n");
400 ch = GMCH_handle_create (t, msg, fwd);
403 tch = GNUNET_new (struct MeshTChannel);
405 GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, tch);
411 handle_ch_ack (struct MeshTunnel3 *t,
412 const struct GNUNET_MESH_ChannelManage *msg,
415 struct MeshChannel *ch;
419 size = ntohs (msg->header.size);
420 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
427 ch = GMT_get_channel (t, ntohl (msg->chid));
430 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel",
432 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
437 GMCH_handle_ack (ch, msg, fwd);
441 handle_ch_destroy (struct MeshTunnel3 *t,
442 const struct GNUNET_MESH_ChannelManage *msg,
445 struct MeshChannel *ch;
449 size = ntohs (msg->header.size);
450 if (size != sizeof (struct GNUNET_MESH_ChannelManage))
457 ch = GMT_get_channel (t, ntohl (msg->chid));
460 /* Probably a retransmission, safe to ignore */
464 GMCH_handle_destroy (ch, msg, fwd);
469 * Demultiplex by message type and call appropriate handler for a message
470 * towards a channel of a local tunnel.
472 * @param t Tunnel this message came on.
473 * @param msgh Message header.
474 * @param fwd Is this message fwd?
477 handle_GMT_decrypted (struct MeshTunnel3 *t,
478 const struct GNUNET_MessageHeader *msgh,
483 type = ntohs (msgh->type);
484 LOG (GNUNET_ERROR_TYPE_DEBUG,
485 "Got a %s message!\n",
486 GNUNET_MESH_DEBUG_M2S (type));
490 case GNUNET_MESSAGE_TYPE_MESH_DATA:
491 /* Don't send hop ACK, wait for client to ACK */
492 handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
495 case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
496 handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
499 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
501 (struct GNUNET_MESH_ChannelCreate *) msgh,
505 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
507 (struct GNUNET_MESH_ChannelManage *) msgh,
511 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
512 handle_ch_destroy (t,
513 (struct GNUNET_MESH_ChannelManage *) msgh,
519 LOG (GNUNET_ERROR_TYPE_DEBUG,
520 "end-to-end message not known (%u)\n",
525 /******************************************************************************/
526 /******************************** API ***********************************/
527 /******************************************************************************/
531 * Decrypt and demultiplex by message type. Call appropriate handler
534 * @param t Tunnel this message came on.
535 * @param msgh Encrypted message.
536 * @param fwd Is this message fwd?
539 GMT_handle_encrypted (struct MeshTunnel3 *t,
540 const struct GNUNET_MESH_Encrypted *msg,
543 size_t size = ntohs (msg->header.size);
544 size_t payload_size = size - sizeof (struct GNUNET_MESH_Encrypted);
545 char cbuf[payload_size];
546 struct GNUNET_MessageHeader *msgh;
549 GMT_decrypt (t, cbuf, &msg[1], payload_size, msg->iv, fwd);
551 while (off < payload_size)
553 msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
554 handle_GMT_decrypted (t, msgh, fwd);
555 off += ntohs (msgh->size);
561 * Cache a message to be sent once tunnel is online.
563 * @param t Tunnel to hold the message.
564 * @param ch Channel the message is about.
565 * @param msg Message itself (copy will be made).
566 * @param fwd Is this fwd?
569 GMT_queue_data (struct MeshTunnel3 *t,
570 struct MeshChannel *ch,
571 struct GNUNET_MessageHeader *msg,
574 struct MeshTunnelQueue *tq;
575 uint16_t size = ntohs (msg->size);
577 tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
580 memcpy (&tq[1], msg, size);
581 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
583 if (MESH_TUNNEL3_READY == t->state)
584 GMT_send_queued_data (t, fwd);
589 * Send all cached messages that we can, tunnel is online.
591 * @param t Tunnel that holds the messages.
592 * @param fwd Is this fwd?
595 GMT_send_queued_data (struct MeshTunnel3 *t, int fwd)
597 struct MeshTunnelQueue *tq;
598 struct MeshTunnelQueue *next;
601 LOG (GNUNET_ERROR_TYPE_DEBUG,
602 "GMT_send_queued_data on tunnel %s\n",
604 room = GMT_get_buffer (t, fwd);
605 LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room);
606 for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
608 LOG (GNUNET_ERROR_TYPE_DEBUG, " data on channel %s\n", GMCH_2s (tq->ch));
611 GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
612 GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
617 LOG (GNUNET_ERROR_TYPE_DEBUG,
618 "GMT_send_queued_data end\n",
624 * Initialize the tunnel subsystem.
626 * @param c Configuration handle.
627 * @param id Peer identity.
628 * @param key ECC private key, to derive all other keys and do crypto.
631 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
632 const struct GNUNET_CRYPTO_EddsaPrivateKey *key)
634 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
636 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
639 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
640 "MESH", "DEFAULT_TTL", "USING DEFAULT");
643 my_private_key = key;
648 * Shut down the tunnel subsystem.
659 * @param destination Peer this tunnel is towards.
662 GMT_new (struct MeshPeer *destination)
664 struct MeshTunnel3 *t;
666 t = GNUNET_new (struct MeshTunnel3);
668 t->peer = destination;
670 // GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
671 // GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
674 // tunnel_destroy (t);
678 // char salt[] = "salt";
679 // GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
680 // salt, sizeof (salt),
681 // &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
682 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
683 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
685 // GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
686 // salt, sizeof (salt),
687 // &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
688 // GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
689 // &my_full_id, sizeof (struct GNUNET_PeerIdentity),
697 * Change the tunnel state.
699 * @param t Tunnel whose state to change.
700 * @param state New state.
703 GMT_change_state (struct MeshTunnel3* t, enum MeshTunnel3State state)
707 LOG (GNUNET_ERROR_TYPE_DEBUG,
708 "Tunnel %s state was %s\n",
710 GMT_state2s (t->state));
711 LOG (GNUNET_ERROR_TYPE_DEBUG,
712 "Tunnel %s state is now %s\n",
714 GMT_state2s (state));
716 if (MESH_TUNNEL3_READY == state && 3 <= GMT_count_connections (t))
718 GMP_stop_search (t->peer);
724 * Add a connection to a tunnel.
727 * @param c Connection.
730 GMT_add_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
732 struct MeshTConnection *aux;
734 for (aux = t->connection_head; aux != NULL; aux = aux->next)
738 aux = GNUNET_new (struct MeshTConnection);
740 GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, aux);
745 * Remove a connection from a tunnel.
748 * @param c Connection.
751 GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c)
753 struct MeshTConnection *aux;
755 for (aux = t->connection_head; aux != NULL; aux = aux->next)
758 GNUNET_CONTAINER_DLL_remove (t->connection_head, t->connection_tail, aux);
766 * Add a channel to a tunnel.
772 GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
774 struct MeshTChannel *aux;
776 for (aux = t->channel_head; aux != NULL; aux = aux->next)
780 aux = GNUNET_new (struct MeshTChannel);
782 GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux);
787 * Remove a channel from a tunnel.
793 GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch)
795 struct MeshTChannel *aux;
797 for (aux = t->channel_head; aux != NULL; aux = aux->next)
800 GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux);
808 * Search for a channel by global ID.
810 * @param t Tunnel containing the channel.
811 * @param chid Public channel number.
813 * @return channel handler, NULL if doesn't exist
816 GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid)
818 struct MeshTChannel *iter;
823 for (iter = t->channel_head; NULL != iter; iter = iter->next)
825 if (GMCH_get_id (iter->ch) == chid)
829 return NULL == iter ? NULL : iter->ch;
834 * Tunnel is empty: destroy it.
836 * Notifies all connections about the destruction.
838 * @param t Tunnel to destroy.
841 GMT_destroy_empty (struct MeshTunnel3 *t)
843 struct MeshTConnection *iter;
845 for (iter = t->connection_head; NULL != iter; iter = iter->next)
847 GMC_send_destroy (iter->c);
850 if (0 == t->pending_messages)
853 t->destroy = GNUNET_YES;
858 * Destroy tunnel if empty (no more channels).
860 * @param t Tunnel to destroy if empty.
863 GMT_destroy_if_empty (struct MeshTunnel3 *t)
865 if (1 < GMT_count_channels (t))
868 GMT_destroy_empty (t);
873 * Destroy the tunnel.
875 * This function does not generate any warning traffic to clients or peers.
878 * Cancel messages belonging to this tunnel queued to neighbors.
879 * Free any allocated resources linked to the tunnel.
881 * @param t The tunnel to destroy.
884 GMT_destroy (struct MeshTunnel3 *t)
886 struct MeshTConnection *iter;
887 struct MeshTConnection *next;
892 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n", GMP_2s (t->peer));
894 // if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
897 for (iter = t->connection_head; NULL != iter; iter = next)
900 GMC_destroy (iter->c);
904 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
905 GMP_set_tunnel (t->peer, NULL);
912 * @brief Use the given path for the tunnel.
913 * Update the next and prev hops (and RCs).
914 * (Re)start the path refresh in case the tunnel is locally owned.
916 * @param t Tunnel to update.
917 * @param p Path to use.
919 * @return Connection created.
921 struct MeshConnection *
922 GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p)
924 struct MeshConnection *c;
925 struct GNUNET_HashCode cid;
926 unsigned int own_pos;
928 if (NULL == t || NULL == p)
934 for (own_pos = 0; own_pos < p->length; own_pos++)
936 if (p->peers[own_pos] == myid)
939 if (own_pos > p->length - 1)
945 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
946 c = GMC_new (&cid, t, p, own_pos);
947 GMT_add_connection (t, c);
953 * Count established (ready) connections of a tunnel.
955 * @param t Tunnel on which to count.
957 * @return Number of connections.
960 GMT_count_connections (struct MeshTunnel3 *t)
962 struct MeshTConnection *iter;
965 for (count = 0, iter = t->connection_head;
967 iter = iter->next, count++);
973 * Count channels of a tunnel.
975 * @param t Tunnel on which to count.
977 * @return Number of channels.
980 GMT_count_channels (struct MeshTunnel3 *t)
982 struct MeshTChannel *iter;
985 for (count = 0, iter = t->channel_head;
987 iter = iter->next, count++);
994 * Get the state of a tunnel.
998 * @return Tunnel's state.
1000 enum MeshTunnel3State
1001 GMT_get_state (struct MeshTunnel3 *t)
1004 return (enum MeshTunnel3State) -1;
1009 * Get the total buffer space for a tunnel.
1012 * @param fwd Is this for FWD traffic?
1014 * @return Buffer space offered by all connections in the tunnel.
1017 GMT_get_buffer (struct MeshTunnel3 *t, int fwd)
1019 struct MeshTConnection *iter;
1020 unsigned int buffer;
1022 iter = t->connection_head;
1025 /* If terminal, return biggest channel buffer */
1026 if (NULL == iter || GMC_is_terminal (iter->c, fwd))
1028 struct MeshTChannel *iter_ch;
1029 unsigned int ch_buf;
1031 if (NULL == t->channel_head)
1033 /* Probably getting buffer for a channel create. */
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 * Send ACK on one or more connections due to buffer space to the client.
1104 * Iterates all connections of the tunnel and sends ACKs appropriately.
1106 * @param ch Channel which has some free buffer space.
1107 * @param fwd Is this in for FWD traffic? (ACK goes dest->root)
1110 GMT_send_acks (struct MeshTunnel3 *t, unsigned int buffer, int fwd)
1112 struct MeshTConnection *iter;
1115 uint32_t allow_per_connection;
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1119 "Tunnel send acks on %s:%X\n",
1120 fwd ? "FWD" : "BCK", GMT_2s (t));
1127 if (NULL == t->channel_head ||
1128 GNUNET_NO == GMCH_is_origin (t->channel_head->ch, fwd))
1134 /* Count connections, how many messages are already allowed */
1135 cs = GMT_count_connections (t);
1136 for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next)
1138 allowed += GMC_get_allowed (iter->c, fwd);
1141 /* Make sure there is no overflow */
1142 if (allowed > buffer)
1148 /* Authorize connections to send more data */
1149 to_allow = buffer; /* - allowed; */
1151 for (iter = t->connection_head; NULL != iter && to_allow > 0; iter = iter->next)
1153 allow_per_connection = to_allow/cs;
1154 to_allow -= allow_per_connection;
1156 if (GMC_get_allowed (iter->c, fwd) > 64 / 3)
1160 GMC_allow (iter->c, buffer, fwd);
1163 GNUNET_break (to_allow == 0);
1168 * Sends an already built message on a tunnel, GMT_encrypting it and
1169 * choosing the best connection.
1171 * @param message Message to send. Function modifies it.
1172 * @param t Tunnel on which this message is transmitted.
1173 * @param ch Channel on which this message is transmitted.
1174 * @param fwd Is this a fwd message?
1177 GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1178 struct MeshTunnel3 *t,
1179 struct MeshChannel *ch,
1182 struct MeshConnection *c;
1183 struct GNUNET_MESH_Encrypted *msg;
1184 size_t size = ntohs (message->size);
1185 char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size];
1189 LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GMP_2s (t->peer));
1191 iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
1192 msg = (struct GNUNET_MESH_Encrypted *) cbuf;
1193 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED);
1194 msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size);
1195 msg->iv = GNUNET_htonll (iv);
1196 GMT_encrypt (t, &msg[1], message, size, iv, fwd);
1197 c = tunnel_get_connection (t, fwd);
1200 GNUNET_break (GNUNET_YES == t->destroy);
1203 type = ntohs (message->type);
1206 case GNUNET_MESSAGE_TYPE_MESH_DATA:
1207 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
1208 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
1209 case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
1210 msg->cid = *GMC_get_id (c);
1211 msg->ttl = htonl (default_ttl);
1214 LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
1215 GNUNET_MESH_DEBUG_M2S (type));
1220 t->pending_messages++;
1221 GMC_send_prebuilt_message (&msg->header, c, ch, fwd);
1225 * Is the tunnel directed towards the local peer?
1229 * @return GNUNET_YES if it is loopback.
1232 GMT_is_loopback (const struct MeshTunnel3 *t)
1234 return (myid == GMP_get_short_id(t->peer));
1239 * Is the tunnel using this path already?
1244 * @return GNUNET_YES a connection uses this path.
1247 GMT_is_path_used (const struct MeshTunnel3 *t, const struct MeshPeerPath *p)
1249 struct MeshTConnection *iter;
1251 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1252 if (GMC_get_path (iter->c) == p)
1260 * Get a cost of a path for a tunnel considering existing connections.
1263 * @param path Candidate path.
1265 * @return Cost of the path (path length + number of overlapping nodes)
1268 GMT_get_path_cost (const struct MeshTunnel3 *t,
1269 const struct MeshPeerPath *path)
1271 struct MeshTConnection *iter;
1272 unsigned int overlap;
1280 GNUNET_assert (NULL != t);
1282 for (i = 0; i < path->length; i++)
1284 for (iter = t->connection_head; NULL != iter; iter = iter->next)
1286 for (j = 0; j < GMC_get_path (iter->c)->length; j++)
1288 if (path->peers[i] == GMC_get_path (iter->c)->peers[j])
1296 return (path->length + overlap) * (path->score * -1);
1301 * Get the static string for the peer this tunnel is directed.
1305 * @return Static string the destination peer's ID.
1308 GMT_2s (const struct MeshTunnel3 *t)
1310 return GMP_2s (t->peer);