reconnecting a lwip socket / SSL handshake problem

f.h-f.s.
Posts: 215
Joined: Thu Dec 08, 2016 2:53 pm

reconnecting a lwip socket / SSL handshake problem

Postby f.h-f.s. » Fri Jan 27, 2017 9:39 am

Hi all,

I'm sending a message over a ssl connection, after completing and closing the connection I am attempting to create another connection and that's where I'm getting a lwip_bind failed:

Code: Select all

lwip_socket(PF_INET, SOCK_STREAM, 0) = 1
lwip_bind(1, addr=0.0.0.0 port=8883)
lwip_bind(1) failed, err=-8
E (1121912) SSLClient: LWIP Bind Failed: 0xffffffff, socket: 1
lwip_close: (1)
lwip_close: is_tcp=1
Error -8 is defined as: Address already in use. so I googled around and set CONFIG_LWIP_SO_REUSE but it seems that does not help.

If the lwip_bind doesn't return a failure or when I restart the device the ssl connection succeeds or gets stuck in a handshake state TLS_ST_SR_KEY_EXCH 28 or TLS_ST_CW_KEY_EXCH 14.
I'm having a hard time finding out what those states mean, besides the obvious that they flag a failed key exchange of some sort.
The OSSL_HANDSHAKE_STATE doesn't provide me with a hint on what to do next.

After completing the ssl connection on success or failure I shutdown the ssl connection like this:

Code: Select all

SSL_shutdown(_ssl);
SSL_free(_ssl);
_ssl = 0;
close(socket);
socket = -1;
SSL_CTX_free(_ssl_ctx);
_ssl_ctx = 0;

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: reconnecting a lwip socket / SSL handshake problem

Postby kolban » Fri Jan 27, 2017 3:01 pm

Howdy,
Thankfully, the solution is likely to be quite straightforward.

In your project, run "make menuconfig". In the resulting menu, select "Component config" and then "LWIP". From there you can select the option "Enable SO_REUSEADRR option". Rebuild your solution and you might be just fine.

Good luck.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

f.h-f.s.
Posts: 215
Joined: Thu Dec 08, 2016 2:53 pm

Re: reconnecting a lwip socket / SSL handshake problem

Postby f.h-f.s. » Fri Jan 27, 2017 4:43 pm

Yeah I did that, it still happens. The bigger problem is the ssl handshake state. It wont properly establish a connection even after it closes and I try again.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: reconnecting a lwip socket / SSL handshake problem

Postby kolban » Fri Jan 27, 2017 5:45 pm

If no-one responds with assistance from the information you have already provided, I would suggest creating a simple application that connects to some well known HTTPS server such as "https://httpbin.org" and see if that works. If it fails the same, then you can attach the sample application so that others might be able to look at the whole story in context and/or attempt to recreate on their own devices.

I notice you are using the "lwip" APIs ... while I have no reason to not believe that these won't work, I'm curious as to why you chose to use these as opposed to the "sockets" APIs?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

f.h-f.s.
Posts: 215
Joined: Thu Dec 08, 2016 2:53 pm

Re: reconnecting a lwip socket / SSL handshake problem

Postby f.h-f.s. » Mon Jan 30, 2017 8:25 am

I just followed the OpenSSL example project. That is how I establish the connection and how I close the connection.
You do make a good point, I'll try to set up a ssl connection on another server, close it off and try again to see what happens.

f.h-f.s.
Posts: 215
Joined: Thu Dec 08, 2016 2:53 pm

Re: reconnecting a lwip socket / SSL handshake problem

Postby f.h-f.s. » Thu Feb 02, 2017 9:19 am

I think this problem was caused by my bluetooth task.
When I delay my bt scan to when my connection has been established the connection succeeds and the esp32 starts scanning.
I'm guessing the bt task interrupts the ssl connection when it is doing it's handhake and results in a failure.

thanks for the help

Who is online

Users browsing this forum: No registered users and 204 guests