X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fmesh%2Fgnunet-service-mesh_tunnel.c;h=231eeabebb97d1a2941b24bd6946ba7c020184d4;hb=1ef7f26b7579ae2a6256751a3f5ebfbf60afbb2f;hp=634a4d7f1315a14cb58a311dbb5a3f086ebd9463;hpb=9a50e92d4a43f917b0cd1ed34d4932470cb3a3da;p=oweals%2Fgnunet.git diff --git a/src/mesh/gnunet-service-mesh_tunnel.c b/src/mesh/gnunet-service-mesh_tunnel.c index 634a4d7f1..231eeabeb 100644 --- a/src/mesh/gnunet-service-mesh_tunnel.c +++ b/src/mesh/gnunet-service-mesh_tunnel.c @@ -33,6 +33,9 @@ #define LOG(level, ...) GNUNET_log_from(level,"mesh-tun",__VA_ARGS__) +#define START_FUNCTION LOG(GNUNET_ERROR_TYPE_DEBUG, "%s start\n", __FUNCTION__) +#define END_FUNCTION LOG(GNUNET_ERROR_TYPE_DEBUG, "%s end\n", __FUNCTION__) + /******************************************************************************/ /******************************** STRUCTS **********************************/ @@ -257,6 +260,46 @@ tunnel_get_connection (struct MeshTunnel3 *t, int fwd) } +/** + * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME + * Encrypt data with the tunnel key. + * + * @param t Tunnel whose key to use. + * @param dst Destination for the GMT_encrypted data. + * @param src Source of the plaintext. + * @param size Size of the plaintext. + * @param iv Initialization Vector to use. + * @param fwd Is this a fwd message? + */ +static void +GMT_encrypt (struct MeshTunnel3 *t, + void *dst, const void *src, + size_t size, uint64_t iv, int fwd) +{ + memcpy (dst, src, size); +} + + +/** + * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME + * Decrypt data with the tunnel key. + * + * @param t Tunnel whose key to use. + * @param dst Destination for the plaintext. + * @param src Source of the GMT_encrypted data. + * @param size Size of the GMT_encrypted data. + * @param iv Initialization Vector to use. + * @param fwd Is this a fwd message? + */ +static void +GMT_decrypt (struct MeshTunnel3 *t, + void *dst, const void *src, + size_t size, uint64_t iv, int fwd) +{ + memcpy (dst, src, size); +} + + void handle_data (struct MeshTunnel3 *t, const struct GNUNET_MESH_Data *msg, @@ -331,6 +374,8 @@ handle_ch_create (struct MeshTunnel3 *t, const struct GNUNET_MESH_ChannelCreate *msg, int fwd) { + ; + struct MeshTChannel *tch; struct MeshChannel *ch; size_t size; @@ -352,12 +397,14 @@ handle_ch_create (struct MeshTunnel3 *t, } else { - ch = GMCH_handle_create (msg, fwd); + ch = GMCH_handle_create (t, msg, fwd); } tch = GNUNET_new (struct MeshTChannel); tch->ch = ch; GNUNET_CONTAINER_DLL_insert (t->channel_head, t->channel_tail, tch); + + ; } void @@ -417,9 +464,6 @@ handle_ch_destroy (struct MeshTunnel3 *t, GMCH_handle_destroy (ch, msg, fwd); } -/******************************************************************************/ -/******************************** API ***********************************/ -/******************************************************************************/ /** * Demultiplex by message type and call appropriate handler for a message @@ -429,10 +473,10 @@ handle_ch_destroy (struct MeshTunnel3 *t, * @param msgh Message header. * @param fwd Is this message fwd? */ -void -GMT_handle_decrypted (struct MeshTunnel3 *t, - const struct GNUNET_MessageHeader *msgh, - int fwd) +static void +handle_decrypted (struct MeshTunnel3 *t, + const struct GNUNET_MessageHeader *msgh, + int fwd) { uint16_t type; @@ -478,6 +522,40 @@ GMT_handle_decrypted (struct MeshTunnel3 *t, } } +/******************************************************************************/ +/******************************** API ***********************************/ +/******************************************************************************/ + + +/** + * Decrypt and demultiplex by message type. Call appropriate handler + * for every message. + * + * @param t Tunnel this message came on. + * @param msg Encrypted message. + * @param fwd Is this message fwd? + */ +void +GMT_handle_encrypted (struct MeshTunnel3 *t, + const struct GNUNET_MESH_Encrypted *msg, + int fwd) +{ + size_t size = ntohs (msg->header.size); + size_t payload_size = size - sizeof (struct GNUNET_MESH_Encrypted); + char cbuf[payload_size]; + struct GNUNET_MessageHeader *msgh; + unsigned int off; + + GMT_decrypt (t, cbuf, &msg[1], payload_size, msg->iv, fwd); + off = 0; + while (off < payload_size) + { + msgh = (struct GNUNET_MessageHeader *) &cbuf[off]; + handle_decrypted (t, msgh, fwd); + off += ntohs (msgh->size); + } +} + /** * Cache a message to be sent once tunnel is online. @@ -522,11 +600,12 @@ GMT_send_queued_data (struct MeshTunnel3 *t, int fwd) LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_send_queued_data on tunnel %s\n", - GMP_2s (t->peer)); + GMT_2s (t)); room = GMT_get_buffer (t, fwd); LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space: %u\n", room); for (tq = t->tq_head; NULL != tq && room > 0; tq = next) { + LOG (GNUNET_ERROR_TYPE_DEBUG, " data on channel %s\n", GMCH_2s (tq->ch)); next = tq->next; room--; GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq); @@ -535,6 +614,9 @@ GMT_send_queued_data (struct MeshTunnel3 *t, int fwd) GNUNET_free (tq); } + LOG (GNUNET_ERROR_TYPE_DEBUG, + "GMT_send_queued_data end\n", + GMP_2s (t->peer)); } @@ -549,6 +631,7 @@ void GMT_init (const struct GNUNET_CONFIGURATION_Handle *c, const struct GNUNET_CRYPTO_EddsaPrivateKey *key) { + LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n"); if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL", &default_ttl)) @@ -567,7 +650,6 @@ GMT_init (const struct GNUNET_CONFIGURATION_Handle *c, void GMT_shutdown (void) { - GNUNET_PEER_change_rc (myid, -1); } @@ -867,48 +949,6 @@ GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p) } -/** - * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME - * Encrypt data with the tunnel key. - * Make static? - * - * @param t Tunnel whose key to use. - * @param dst Destination for the encrypted data. - * @param src Source of the plaintext. - * @param size Size of the plaintext. - * @param iv Initialization Vector to use. - * @param fwd Is this a fwd message? - */ -void -GMT_encrypt (struct MeshTunnel3 *t, - void *dst, const void *src, - size_t size, uint64_t iv, int fwd) -{ - memcpy (dst, src, size); -} - - -/** - * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME - * Decrypt data with the tunnel key. - * Make static? - * - * @param t Tunnel whose key to use. - * @param dst Destination for the plaintext. - * @param src Source of the encrypted data. - * @param size Size of the encrypted data. - * @param iv Initialization Vector to use. - * @param fwd Is this a fwd message? - */ -void -GMT_decrypt (struct MeshTunnel3 *t, - void *dst, const void *src, - size_t size, uint64_t iv, int fwd) -{ - memcpy (dst, src, size); -} - - /** * Count established (ready) connections of a tunnel. * @@ -944,7 +984,7 @@ GMT_count_channels (struct MeshTunnel3 *t) for (count = 0, iter = t->channel_head; NULL != iter; - iter = iter->next, count++); + iter = iter->next, count++) /* skip */; return count; } @@ -968,10 +1008,13 @@ GMT_get_state (struct MeshTunnel3 *t) /** * Get the total buffer space for a tunnel. * + * If terminal, use the biggest channel buffer (or 64) if no channel exists. + * If not terminal, use the sum of all connection buffers. + * * @param t Tunnel. * @param fwd Is this for FWD traffic? * - * @return Buffer space offered by all connections in the tunnel. + * @return Buffer space offered by all entities (c/ch) in the tunnel. */ unsigned int GMT_get_buffer (struct MeshTunnel3 *t, int fwd) @@ -989,7 +1032,10 @@ GMT_get_buffer (struct MeshTunnel3 *t, int fwd) unsigned int ch_buf; if (NULL == t->channel_head) + { + /* Probably getting buffer for a channel create/handshake. */ return 64; + } for (iter_ch = t->channel_head; NULL != iter_ch; iter_ch = iter_ch->next) { @@ -1056,7 +1102,129 @@ GMT_get_next_chid (struct MeshTunnel3 *t) /** - * Sends an already built message on a tunnel, encrypting it and + * Send ACK on one or more channels due to buffer in connections.. + * + * @param t Channel which has some free buffer space. + * @param fwd Is this for FWD traffic? (ACK goes to root) + */ +void +GMT_unchoke_channels (struct MeshTunnel3 *t, int fwd) +{ + struct MeshTChannel *iter; + unsigned int buffer; + unsigned int channels = GMT_count_channels (t); + unsigned int choked_n; + struct MeshChannel *choked[channels]; + + LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT_unchoke_channels on %s\n", GMT_2s (t)); + + if (NULL == t) + { + GNUNET_break (0); + return; + } + + /* Get buffer space */ + buffer = GMT_get_buffer (t, fwd); + if (0 == buffer) + { + return; + } + + /* Count and remember choked channels */ + choked_n = 0; + for (iter = t->channel_head; NULL != iter; iter = iter->next) + { + if (GNUNET_NO == GMCH_get_allowed (iter->ch, fwd)) + { + choked[choked_n++] = iter->ch; + } + } + + /* Unchoke random channels */ + while (0 < buffer && 0 < choked_n) + { + unsigned int r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, + choked_n); + GMCH_allow_client (choked[r], fwd); + choked_n--; + buffer--; + choked[r] = choked[choked_n]; + } +} + + +/** + * Send ACK on one or more connections due to buffer space to the client. + * + * Iterates all connections of the tunnel and sends ACKs appropriately. + * + * @param t Tunnel. + * @param fwd Is this in for FWD traffic? (ACK goes dest->root) + */ +void +GMT_send_acks (struct MeshTunnel3 *t, int fwd) +{ + struct MeshTConnection *iter; + uint32_t allowed; + uint32_t to_allow; + uint32_t allow_per_connection; + unsigned int cs; + unsigned int buffer; + + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Tunnel send %s ACKs on %s\n", + fwd ? "FWD" : "BCK", GMT_2s (t)); + + if (NULL == t) + { + GNUNET_break (0); + return; + } + if (NULL == t->channel_head || + GNUNET_NO == GMCH_is_origin (t->channel_head->ch, !fwd)) + { + GNUNET_break (0); + return; + } + + buffer = GMT_get_buffer (t, fwd); + + /* Count connections, how many messages are already allowed */ + cs = GMT_count_connections (t); + for (allowed = 0, iter = t->connection_head; NULL != iter; iter = iter->next) + { + allowed += GMC_get_allowed (iter->c, fwd); + } + + /* Make sure there is no overflow */ + if (allowed > buffer) + { + GNUNET_break (0); + return; + } + + /* Authorize connections to send more data */ + to_allow = buffer; /* - allowed; */ + + for (iter = t->connection_head; NULL != iter && to_allow > 0; iter = iter->next) + { + allow_per_connection = to_allow/cs; + to_allow -= allow_per_connection; + cs--; + if (GMC_get_allowed (iter->c, fwd) > 64 / 3) + { + continue; + } + GMC_allow (iter->c, buffer, fwd); + } + + GNUNET_break (to_allow == 0); +} + + +/** + * Sends an already built message on a tunnel, GMT_encrypting it and * choosing the best connection. * * @param message Message to send. Function modifies it. @@ -1077,7 +1245,7 @@ GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message, uint64_t iv; uint16_t type; - LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n", GMP_2s (t->peer)); + LOG (GNUNET_ERROR_TYPE_DEBUG, "GMT Send on Tunnel %s\n", GMP_2s (t->peer)); iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX); msg = (struct GNUNET_MESH_Encrypted *) cbuf; @@ -1094,10 +1262,10 @@ GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message, type = ntohs (message->type); switch (type) { - case GNUNET_MESSAGE_TYPE_MESH_FWD: - case GNUNET_MESSAGE_TYPE_MESH_BCK: + case GNUNET_MESSAGE_TYPE_MESH_DATA: case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE: case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY: + case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK: msg->cid = *GMC_get_id (c); msg->ttl = htonl (default_ttl); break;