Facing TLS issue with recv() api on Legato, when using Azure IoT C SDK?

Hi Team,

Couple of days ago, I have ported latest Azure IoT C SDK on to MangoH Yellow Board with the required source and header files. I have written required Cdef and Adef files and for Security I am using OpenSSL standard Lib provided for MangoH Yellow.

When I am trying to run the application, the application is suppose to get authenticated by the Azure IoT HUB through TLS handshake and then send 5 Encrypted MQTT Payloads.

In my case, the application is giving error as shown below

Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ==Receive Socketio_dowork :: Socket Id is : 8== 

Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | =====BERFORE RECEVIED BYTES VALUE :: 0 :: Byte string : === 

Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | =====AFTER RECEVIED BYTES VALUE :: -1 :: Byte string : === 

Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | Error: Time:Mon Oct 11 08:42:01 2021 File:/home/ai/myMangOH/mangOH/apps/openssl_vikas/azureComponents/socketio_berkeley.c Func:socketio_dowork Line:1045 Socketio_Failure: Receiving data from endpoin 

Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ^M 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ==received < 0 && errno is commented 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====tlsio_openssl_dowork :: else part==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====tlsio_openssl_dowork :: If Its in open state==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====write_outgoing_bytes Start here==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====write_outgoing_bytes End here==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====tlsio_openssl_dowork :: xio_dowork is calling==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] |  



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ==Receive Socketio_dowork :: Socket Id is : 8== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | =====BERFORE RECEVIED BYTES VALUE :: 0 :: Byte string : === 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | =====AFTER RECEVIED BYTES VALUE :: -1 :: Byte string : === 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | Error: Time:Mon Oct 11 08:42:01 2021 File:/home/ai/myMangOH/mangOH/apps/openssl_vikas/azureComponents/socketio_berkeley.c Func:socketio_dowork Line:1045 Socketio_Failure: Receiving data from endpoin 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ^M 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ==received < 0 && errno is commented 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====tlsio_openssl_dowork :: else part==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====tlsio_openssl_dowork :: If Its in open state==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====write_outgoing_bytes Start here==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====write_outgoing_bytes End here==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====tlsio_openssl_dowork :: xio_dowork is calling==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] |  



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ==Receive Socketio_dowork :: Socket Id is : 8== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | =====BERFORE RECEVIED BYTES VALUE :: 0 :: Byte string : === 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | =====AFTER RECEVIED BYTES VALUE :: -1 :: Byte string : === 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | Error: Time:Mon Oct 11 08:42:01 2021 File:/home/ai/myMangOH/mangOH/apps/openssl_vikas/azureComponents/socketio_berkeley.c Func:socketio_dowork Line:1045 Socketio_Failure: Receiving data from endpoin 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ^M 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ==received < 0 && errno is commented 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====tlsio_openssl_dowork :: else part==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====tlsio_openssl_dowork :: If Its in open state==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====write_outgoing_bytes Start here==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====write_outgoing_bytes End here==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ====tlsio_openssl_dowork :: xio_dowork is calling==== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] |  



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ==Receive Socketio_dowork :: Socket Id is : 8== 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | =====BERFORE RECEVIED BYTES VALUE :: 0 :: Byte string : === 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | =====AFTER RECEVIED BYTES VALUE :: -1 :: Byte string : === 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | Error: Time:Mon Oct 11 08:42:01 2021 File:/home/ai/myMangOH/mangOH/apps/openssl_vikas/azureComponents/socketio_berkeley.c **Func:socketio_dowork Line:1045 Socketio_Failure: Receiving data from endpoint** 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ^M 



Oct 11 14:12:01 swi-mdm9x28-wp user.info Legato:  INFO | legatoazure[17644] | ==received < 0 && errno is commented

Here after TCP handshake, the client initiates TLS handshake where in after Server Hello message the Client is sending FIN (Connection Finish) message.
This is due to the recv() api present in Azure IoT C SDK, File: c-utility/adapters/socketio_brkeley.c, present in api: socketio_dowork()
Snippet:

do
{
received = recv(socket_io_instance->socket, socket_io_instance->recv_bytes, XIO_RECEIVE_BUFFER_SIZE, MSG_NOSIGNAL);
if (received > 0)
{
if (socket_io_instance->on_bytes_received != NULL)
{
/* Explicitly ignoring here the result of the callback */
(void)socket_io_instance->on_bytes_received(socket_io_instance->on_bytes_received_context, socket_io_instance->recv_bytes, received);
}
}
else if (received == 0)
{
// Do not log error here due to this is probably the socket being closed on the other end
indicate_error(socket_io_instance);
}
else if (received < 0 && errno != EAGAIN)
{
LogError("Socketio_Failure: Receiving data from endpoint: errno=%d.", errno);
indicate_error(socket_io_instance);
}

 } whiledo
{
received = recv(socket_io_instance->socket, socket_io_instance->recv_bytes, XIO_RECEIVE_BUFFER_SIZE, MSG_NOSIGNAL);
if (received > 0)
{
if (socket_io_instance->on_bytes_received != NULL)
{
/* Explicitly ignoring here the result of the callback */
(void)socket_io_instance->on_bytes_received(socket_io_instance->on_bytes_received_context, socket_io_instance->recv_bytes, received);
}
}
else if (received == 0)
{
// Do not log error here due to this is probably the socket being closed on the other end
indicate_error(socket_io_instance);
}
else if (received < 0 && errno != EAGAIN)
{
LogError("Socketio_Failure: Receiving data from endpoint: errno=%d.", errno);
indicate_error(socket_io_instance);
}

 } while (received > 0 && socket_io_instance->io_state == IO_STATE_OPEN); (received > 0 && socket_io_instance->io_state == IO_STATE_OPEN);

Basically, every time even after the device receives Server Hello, the recv() api is returning -1 and 0 Bytes are received.

Below is the Wireshark Capture:

Things Tried:

  1. Increased received time out value, still the issue exists.
  2. Tested the same code base on Linux, the connection and MQTT Publish happens properly without any issues.

I am suspecting that is there any issue with the Legato Platform recv() api.
Now, also I am thinking as I am using socket.h related api’s the issues are prevailing. Should I use any le related api’s like le_send() or le_recv() if exist.

What am I missing here, please help me resolve this issue?

Thanks and Regards,
Pallavi B

the recv() should be working fine.

You can see this application note for socket programming

https://source.sierrawireless.com/resources/airprime/application_notes_and_code_samples/airprime_wp_series_and_fx30_uart_to_tcp_app/