Calculate bytes sent/received during handshake and requests

Hi - I am looking for a way to compute the network traffic to/from my embedded device, ie all the data I sent from my server and received. This includes my get, post requests - which are straightforward to compute.
But I am not entirely sure what my connect operation costs in terms of bytes sent and received(all the TLS handshake stuff). How can I approximate this value?

Hi @imagiko
You can modify the bio callback functions you set to Mbed TLS to track the size of the packets being sent on traffic.
Does that make sense?
Mbed TLS Team member

Thanks for the tip, Ron. I am not sure I have access to this callback, as I am using a version that has already been ported. One of the ways I think it could be done is by adding a field for tx_bytes and rx_bytes to the mbedtls_ssl_config struct.

Then, I add a line to mbedtls_ssl_write_record function:

 ssl->tx_bytes += ssl->out_msglen;

and a line in the mbedtls_ssl_read_record function:

ssl->rx_bytes += ssl->in_msglen;

In a client function, where I have a connect() function, if I print the fields tx_bytes and rx_bytes right after a successful connection, that should give me the cumulative bytes transferred during the entire handshake.
My reasoning is that all the steps of the handshake that are handled in the mbedtls_ssl_handshake_client_step function eventually call the write/read record function, and would be the right place to insert the byte counter. Can you tell me if this can give me a correct estimate?

Hi…just wanted to check in again. Do you think this method will give me the traffic bytes correctly?

Hi @imagiko
You have more access to the callbacks than the config structure.
Please check in your code where you call mbedtls_ssl_set_bio , and add your suggestions of accumulating the data sent \ received in the callback you set in that function (either modify the callabcks, or wrap the callabcks with your own custom defined callback that accumulate in the callback context, and then call the original callback. )