digidotcom / xbee_ansic_library

A collection of portable ANSI C code for communicating with Digi International's XBee wireless radio modules in API mode.
207 stars 118 forks source link

MbedTLS Handshake in Xbee3 with AWS gives Socket closed unexpectedly(0x76) error after parsing server hello #27

Closed Rutvij-dev closed 4 years ago

Rutvij-dev commented 4 years ago

Hi, I am working with MCU based solution where i have added extended socket apis in AWS freeRTOS using https://github.com/digidotcom/xbee_ansic_library. In this implementations i am able to connect to the AWS server using socket create and connect option. I am using firmware version 11415.

I have written xbee_recv call back such as it uses 8K buffer and this will be filled up with Xbee RF hardware event and can be used for mqtt to to take data from this same buffer. To do this i have maintained one buffer with two pointers i.e hardware pointer and software pointer.

Hw pointer deals with incoming data from Xbee RF and SW pointer will be used in taking out the data and pas to upper layer.

Now in TLS handshaking phase i am able to achieve "client state: 0" viz. send client hello and "client state: 2" viz. parse server hello, but when it goes to the 3rd state "client state: 3" it goes to mbedtsl_ssl_flush_output it send 167 bytes for flushing , but at this point i am receiving socket state "0x76 -Socket closed unexpectedly" and connection lost logs.

Before this stage i received response from AWS for 5350 bytes which i stored in 8K buffer and and nb_want variable got satisfied.

below are my mbedtls enabled logs with MBEDTLS_DEBUG_C macro

mbedTLS: |2| 0x200044cc: <= parse server hello MBEDTLS_SSL_SERVER_HELLO 0 mbedTLS: |2| 0x200044cc: client state: 3 mbedTLS: |2| 0x200044cc: => flush output mbedTLS: |2| 0x200044cc: message length: 159, out_left: 159 Here socket gets closed unexpectedly at sending above bytes

Full logs are attached here with mbedtls_logs.txt

Please help me understand what is going on,

Any help would be greatly appreciable.

Thanks

tomlogic commented 4 years ago

I'd recommend working your way up to making the TLS connection to AWS.

Do some initial testing with the library you're developing and perhaps create a simple HTTP client to connect to a web server and retrieve a file. You'll be able to validate that you're receiving large amounts of data correctly. If you have the client POST data to a CGI script on the web server, you can confirm that sending large blocks of data works correctly as well. Doing the initial tests with cleartext will help you identify failures, even if you're just using md5sum to validate file contents, you'll know that you successfully sent or received 32KB (for example).

Then try using the TLS code with a web server that you have control over so you can configure the TLS options, view logs that could indicate the cause of failed connections, and even capture packets on the server (possibly with tcpdump) for analysis in Wireshark to see what happens in the handshake. If you have the private key file for the TLS certificate on the web server, Wireshark will even be able to decode encrypted data from the handshake.

You're making solid progress with your code, and it seems like you aren't far off from having a working handshake.

Rutvij-dev commented 4 years ago

Hi, Thanks for the help it worked i am now able to communicate with AWS :)

While debugging this the issue seemed with send API of xbee3 cellular . I am using latest user guide Revision history—90002258 for firmware 11415, now this guide says it has hardbound data limit of 1500 bytes for transfer for TCP and less for UDP

I have selected XBEE_SOCK_PROTOCOL_TCP while creating socket and able to connect to AWS. But while sending to socket using xbee_sock_send it cannot send 419 bytes of data saying EMSGSIZE(122), so i had to put soft limit with a condition and divide them into chunk of 160 bytes max.

Same thing happened with receive callback which is getting 512 bytes of chunk and not more then that, however guide says it may catch-up till 1500 bytes.

So send/recv both should work but in my case it did not work. So why this limit is mis-macthing ?

Is there anything needs to be configured to extend limit for send/recv ?

Thanks

tomlogic commented 4 years ago

Make sure you have XBEE_CELLULAR_ENABLED defined in your project so it will use a maximum RF payload of 1500. I believe XBEE_MAX_RX_FRAME_LEN and XBEE_MAX_TX_FRAME_LEN are using safe values for the extended sockets.

How large are your serial buffers? -EMSGSIZE usually indicates that code in xbee_device.c can't fit the full frame into an empty serial transmit buffer. You'll get -EBUSY if the buffer is too full to take the frame, but it should eventually drain and you'll be able to queue the outbound frame at that point.

Rutvij-dev commented 4 years ago

Hi, you were right, there were buffer limits in my rx-tx UART buffers -255 bytes , i enlarged this size and now OTA as well as MQTT messages to AWS are working fine.

I made this size 1023 Bytes.

Thanks for the debug pointer, really appreciate.

Thanks