Receiving MBEDTLS_ERR_NET_CONN_RESET when downloading file from a remote server

mr1000
Posts: 23
Joined: Fri Jan 12, 2018 9:05 am

Receiving MBEDTLS_ERR_NET_CONN_RESET when downloading file from a remote server

Postby mr1000 » Wed Jul 10, 2019 8:46 pm

Hi there,
  • I'm using esp-idf version 3.0.5
  • I have ESP32 connected to a GSM module via UART.
  • Also I'm using do while loop for read the response of the server (https_request example similar code)
  • GSM Task (uart) and HTTPS Task have the same priority

The problem is when the program is reading the file from server, it returns a -0x0050 (MBEDTLS_ERR_NET_CONN_RESET ) and it stops de download. This happens sometimes at 40 seconds from downloading, sometimes at 100, sometimes at 200... it varies

I've tried the follow command to see if it happens from a PC and it's server problem:
curl -i -v --http1.1 --limit-rate 1000 https://server_domain/file.txt
But it doesn't seem so as the download correctly finishes.
As you see I try to simulate slow connection, because the GSM Uart baudrate is 19200, that would be 2400 bytes/s.

  • I've seen the return error comes from mbedtls_ssl_handshake() call that is inside the the mbedtls_ssl_read() call.

This is how the do while loop for reading looks like, deleted a few comment code

Code: Select all

do
        {
            len = BUFFSIZE;
            ret = mbedtls_ssl_read(&ssl, (unsigned char *)bufferRead, len);

            if(ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE)
            {
                continue;
            }

            if(ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
            {
                ret = 0;
                break;
            }

            if( ret < 0 ) /* receive error */
            {
                break;
            }
            else if( (ret > 0) && (!resp_body_start) )	/* reading header first and a piece of body */
            {
                if( (!http_200_flag) && (strstr(bufferRead, "200") == NULL) )
                {
                    break;
                }
                http_200_flag = true;

            	resp_body_start = https_readPastHttpHeader( bufferRead,	ret, &index );
            	if( (resp_body_start) && (index > 0))
            	{
            		i_write_len = ret - index;

            		if( i_write_len > 0 )
            		{
				//save data to partition
				saveDataToPartition( &bufferRead );		
                        }
            	}
            	else
            	{
            		break;
            	}
            }
            else if( (ret > 0) && (resp_body_start) )	/* already read the header, reading body */
            {
            	//save data to partition
		saveDataToPartition( &bufferRead );	
            }
            else if( ret == 0 )	/* packet over */
            {
            	break;
            }
        }
        while(1);

I read the data from mbedtls_ssl_read in chunks of 2048 bytes (size of bufferRead) and write the data in NVM memory, by printing the timestamp I see the task reads data each 16384 bytes (8 * 2048)

Code: Select all

rTime: 638638 //rTime -> timestamp reported by esp_logtime()
Write OK of 2048 bytes to 0x00000000 //this address is relative, not actually 0x0000 address!
rTime: 638648
Write OK of 2048 bytes to 0x00000800
rTime: 638648
Write OK of 2048 bytes to 0x00001000
rTime: 638648
Write OK of 2048 bytes to 0x00001800
rTime: 638648
Write OK of 2048 bytes to 0x00002000
rTime: 638648
Write OK of 2048 bytes to 0x00002800
rTime: 638658
Write OK of 2048 bytes to 0x00003000
rTime: 638658
Write OK of 2048 bytes to 0x00003800
rTime: 647298
Write OK of 2048 bytes to 0x00004000
rTime: 647308
Write OK of 2048 bytes to 0x00004800
rTime: 647308
Write OK of 2048 bytes to 0x00005000
rTime: 647308
Write OK of 2048 bytes to 0x00005800
rTime: 647308
Write OK of 2048 bytes to 0x00006000
rTime: 647308
Write OK of 2048 bytes to 0x00006800
rTime: 647308
Write OK of 2048 bytes to 0x00007000
rTime: 647308
Write OK of 2048 bytes to 0x00007800
rTime: 656638
Write OK of 2048 bytes to 0x00008000
rTime: 656648
Write OK of 2048 bytes to 0x00008800
rTime: 656648
Write OK of 2048 bytes to 0x00009000
rTime: 656648
Write OK of 2048 bytes to 0x00009800
rTime: 656648
Write OK of 2048 bytes to 0x0000A000
rTime: 656658
Write OK of 2048 bytes to 0x0000A800
rTime: 656658
Write OK of 2048 bytes to 0x0000B000
rTime: 656658
Write OK of 2048 bytes to 0x0000B800
rTime: 665228
Write OK of 2048 bytes to 0x0000C000
rTime: 665238
Write OK of 2048 bytes to 0x0000C800
rTime: 665238
Write OK of 2048 bytes to 0x0000D000
rTime: 665238
Write OK of 2048 bytes to 0x0000D800
rTime: 665238
Write OK of 2048 bytes to 0x0000E000
rTime: 665248
Write OK of 2048 bytes to 0x0000E800
rTime: 665248
Write OK of 2048 bytes to 0x0000F000
rTime: 665248
Write OK of 2048 bytes to 0x0000F800
rTime: 673878
Write OK of 2048 bytes to 0x00010000
rTime: 673888
Write OK of 2048 bytes to 0x00010800
rTime: 673888
Write OK of 2048 bytes to 0x00011000
rTime: 673888
Write OK of 2048 bytes to 0x00011800
rTime: 673888
Write OK of 2048 bytes to 0x00012000
rTime: 673898
Write OK of 2048 bytes to 0x00012800
rTime: 673898
Write OK of 2048 bytes to 0x00013000
rTime: 673898
Write OK of 2048 bytes to 0x00013800
rTime: 683208
Write OK of 2048 bytes to 0x00014000
rTime: 683218
Write OK of 2048 bytes to 0x00014800
rTime: 683218
Write OK of 2048 bytes to 0x00015000
rTime: 683218
Write OK of 2048 bytes to 0x00015800
rTime: 683218
Write OK of 2048 bytes to 0x00016000
rTime: 683228
Write OK of 2048 bytes to 0x00016800
rTime: 683228
Write OK of 2048 bytes to 0x00017000
rTime: 683228
Write OK of 2048 bytes to 0x00017800
rTime: 691798
Write OK of 2048 bytes to 0x00018000
rTime: 691808
Write OK of 2048 bytes to 0x00018800
rTime: 691808
Write OK of 2048 bytes to 0x00019000
rTime: 691808
Write OK of 2048 bytes to 0x00019800
rTime: 691808
Write OK of 2048 bytes to 0x0001A000
rTime: 691818
Write OK of 2048 bytes to 0x0001A800
rTime: 691818
Write OK of 2048 bytes to 0x0001B000
rTime: 691818
Write OK of 2048 bytes to 0x0001B800
rTime: 701998
Write OK of 2048 bytes to 0x0001C000
rTime: 702008
Write OK of 2048 bytes to 0x0001C800
rTime: 702008
Write OK of 2048 bytes to 0x0001D000
rTime: 702008
Write OK of 2048 bytes to 0x0001D800
rTime: 702008
Write OK of 2048 bytes to 0x0001E000
rTime: 702008
Write OK of 2048 bytes to 0x0001E800
rTime: 702008
Write OK of 2048 bytes to 0x0001F000
rTime: 702008
Write OK of 2048 bytes to 0x0001F800
rTime: 711328
Write OK of 2048 bytes to 0x00020000
rTime: 711338
Write OK of 2048 bytes to 0x00020800
rTime: 711338
Write OK of 2048 bytes to 0x00021000
rTime: 711338
Write OK of 2048 bytes to 0x00021800
rTime: 711338
Write OK of 2048 bytes to 0x00022000
rTime: 711348
Write OK of 2048 bytes to 0x00022800
rTime: 711348
Write OK of 2048 bytes to 0x00023000
rTime: 711348
Write OK of 2048 bytes to 0x00023800
rTime: 719918
Write OK of 2048 bytes to 0x00024000
rTime: 719928
Write OK of 2048 bytes to 0x00024800
rTime: 719928
Write OK of 2048 bytes to 0x00025000
rTime: 719928
Write OK of 2048 bytes to 0x00025800
rTime: 719928
Write OK of 2048 bytes to 0x00026000
rTime: 719938
Write OK of 2048 bytes to 0x00026800
rTime: 719938
Write OK of 2048 bytes to 0x00027000
rTime: 719938
Write OK of 2048 bytes to 0x00027800
rTime: 732148
Write OK of 2048 bytes to 0x00028000
rTime: 732158
Write OK of 2048 bytes to 0x00028800
rTime: 732158
Write OK of 2048 bytes to 0x00029000
rTime: 732158
Write OK of 2048 bytes to 0x00029800
rTime: 732158
Write OK of 2048 bytes to 0x0002A000
rTime: 732168
Write OK of 2048 bytes to 0x0002A800
rTime: 732168
Write OK of 2048 bytes to 0x0002B000
rTime: 732168
Write OK of 2048 bytes to 0x0002B800
rTime: 739348
Receive error: -0x50 //received MBEDTLS_ERR_NET_CONN_RESET 
Bytes read: 0x0002C000
elapsed: 110140 //110 seconds



Could be some bug that has been solved?
Been reading releases page and seen a lot of lwip improvements but dunno if it's related
https://github.com/espressif/esp-idf/releases

Also, those are the calls I use before initializing HTTPS connection, there's something missing maybe?

Code: Select all

mbedtls_ssl_init(&ssl);
mbedtls_x509_crt_init(&cacert);
mbedtls_ctr_drbg_init(&ctr_drbg);

mbedtls_ssl_config_init(&conf);
mbedtls_entropy_init(&entropy);
mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0)) != 0)

mbedtls_x509_crt_parse(&cacert, server_root_cert_pem_start, server_root_cert_pem_end-server_root_cert_pem_start);
mbedtls_ssl_set_hostname(&ssl, https_webServer)
mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)

mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
		
mbedtls_ssl_conf_read_timeout(&conf, SSL_READ_TIMEOUT_MS); 
mbedtls_ssl_setup(&ssl, &conf)

mbedtls_net_init(&server_fd)
mbedtls_net_connect(&server_fd, https_webServer, https_webPort, MBEDTLS_NET_PROTO_TCP)
mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, NULL, mbedtls_net_recv_timeout);
mbedtls_ssl_handshake(&ssl)
mbedtls_ssl_get_verify_result(&ssl) //verify returns ok
mbedtls_ssl_write(&ssl, (const unsigned char *)buffer + written_bytes, length_request - written_bytes) 
//this write would be the GET header
And this is the GET header I sent to server, been messing around it a bit changing header parameters but with same result:

Code: Select all

GET https://server_domain/file.bin HTTP/1.1
Host: server_domain
Connection: Keep-Alive
User-Agent: ESP32
Accept: */*


Any advise of how can I find the problem would be very apreciated.
Thanks in advance

Edit: is it possible to redirect debug messages to, lets say, uart ch 2? the default uart ch is connected to other microcontroller which I need not to receive garbage info, so I have the debug esp_log messages disabled...

mr1000
Posts: 23
Joined: Fri Jan 12, 2018 9:05 am

Re: Receiving MBEDTLS_ERR_NET_CONN_RESET when downloading file from a remote server

Postby mr1000 » Thu Jul 11, 2019 5:40 pm

Managed to do a log of mbedtls debug including some of my prints

https://pastebin.com/FeHQChfx

Seems to be a lot of ssl->f_recv(_timeout)() errors:
1330 (-0xffffface)
this one is repeated periodically all over
but can't find what they mean...

Edit: okay, so those aren't errors :lol: , it's just the number of bytes read.
It seems that they're not 'fetched input' till there's 16413 bytes read.
This number of bytes seems realted to CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=16384 I think, gonna try to decrease it and increment it.

I also attach my sdkconfig file, maybe there's some misconfiguration I'm not aware of... thanks
https://pastebin.com/xs9BRDAG

Who is online

Users browsing this forum: Bing [Bot] and 118 guests