Skip to content

Commit 144994c

Browse files
authored
Merge pull request esp8266#23 from me-no-dev/add-send-calculator
add send packet size calculator
2 parents d26f23a + 23d532a commit 144994c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

ssl/ssl.h

+9
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,15 @@ EXP_FUNC int STDCALL ssl_read(SSL *ssl, uint8_t **in_data);
296296
*/
297297
EXP_FUNC int STDCALL ssl_write(SSL *ssl, const uint8_t *out_data, int out_len);
298298

299+
/**
300+
* @brief Calculate the size of the encrypted data from what you are about to send
301+
* @param ssl [in] An SSL obect reference.
302+
* @param out_len [in] The number of bytes to be written.
303+
* @return The number of bytes that will be sent, or if < 0 if an error.
304+
* @see ssl.h for the error code list.
305+
*/
306+
EXP_FUNC int STDCALL ssl_calculate_write_length(SSL *ssl, int out_len);
307+
299308
/**
300309
* @brief Find an ssl object based on a file descriptor.
301310
*

ssl/tls1.c

+29
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,35 @@ EXP_FUNC int STDCALL ssl_write(SSL *ssl, const uint8_t *out_data, int out_len)
315315
return out_len;
316316
}
317317

318+
EXP_FUNC int STDCALL ssl_calculate_write_length(SSL *ssl, int length)
319+
{
320+
int msg_length = 0;
321+
if (ssl->hs_status == SSL_ERROR_DEAD)
322+
return SSL_ERROR_CONN_LOST;
323+
324+
if (ssl->flag & SSL_SENT_CLOSE_NOTIFY)
325+
return SSL_CLOSE_NOTIFY;
326+
327+
msg_length += length;
328+
329+
if (ssl->flag & SSL_TX_ENCRYPTED)
330+
{
331+
msg_length += ssl->cipher_info->digest_size;
332+
{
333+
int last_blk_size = msg_length%ssl->cipher_info->padding_size;
334+
int pad_bytes = ssl->cipher_info->padding_size - last_blk_size;
335+
if (pad_bytes == 0)
336+
pad_bytes += ssl->cipher_info->padding_size;
337+
msg_length += pad_bytes;
338+
}
339+
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_1)
340+
{
341+
msg_length += ssl->cipher_info->iv_size;
342+
}
343+
}
344+
return SSL_RECORD_SIZE+msg_length;
345+
}
346+
318347
/**
319348
* Add a certificate to the certificate chain.
320349
*/

0 commit comments

Comments
 (0)