cancel
Showing results for 
Search instead for 
Did you mean: 

MBEDTLS: creating a socket returns MBEDTLS_ERR_NET_SOCKET_FAILED

Evgeniy Vasyliev
Associate III
Posted on April 05, 2018 at 19:39

Hi, everyone!

I am quite new to SSL/TLS and I am trying to make SSL server on my own STM32F427 MCU board. I already have the standard HTTP server working correctly using lwIP netconn library and I now am trying to make it be more secured with adding the SSL layer over same lwIP.

So, I am using the code source from CubeMX examples for SSL server, but always have the same problem with mbedtls_net_bind command execution. Seems that my code is exactly the same as in example, but at mine the mbedtls_net_bind function always returns value MBEDTLS_ERR_NET_SOCKET_FAILED.

I am setting MBEDTLS library for default values in STM32CubeMX and here are few things from code how I am doing the job:

1. Start of my SSL server code:

static mbedtls_net_context listen_fd;

mbedtls_net_init(&listen_fd);

// This function always gives out -0x42, which is MBEDTLS_ERR_NET_SOCKET_FAILED

if ((ret = mbedtls_net_bind(&listen_fd, NULL, '4433', MBEDTLS_NET_PROTO_TCP)) != 0)

{

    debug_printf(' failed  ! mbedtls_net_bind returned %d', ret);

    goto exit;

}

2. Function mbedtls_net_init:

void mbedtls_net_init( mbedtls_net_context *ctx )

{

    ctx->fd = -1;

    // Check whether lwIP was already initialized

    if (lwIpInitialized == 1)

        return;

  MX_LWIP_Init();

}

3. Start of function mbedtls_net_bind, which is calling the problem:

int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )

{

      int ret = 0;

      int n;

      struct addrinfo hints, *addr_list, *cur;

      /* Bind to IPv6 and/or IPv4, but only in the desired protocol */

      memset( &hints, 0, sizeof( hints ) );

      hints.ai_family = AF_UNSPEC;

      hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;

      hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;

      if( bind_ip == NULL )

          hints.ai_flags = AI_PASSIVE;

      if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )

          return( MBEDTLS_ERR_NET_UNKNOWN_HOST );

      /* Try the sockaddrs until a binding succeeds */

      ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;

      for( cur = addr_list; cur != NULL; cur = cur->ai_next )

      {

          // Here at socket() function I always have an error, I have the following values at

          // cur->ai_family = AF_UNSPEC;

 

          // cur->ai_socktype = SOCK_STREAM;

          // cur->ai_protocol = IPPROTO_TCP;

          ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );

          if( ctx->fd < 0 )

          {

              ret = MBEDTLS_ERR_NET_SOCKET_FAILED;

              continue;

          }

..........

Can anyone please suggest what I am doing wrong?

Thanks for any useful response...

#socket #mbedtls #mbedtls_err_net_socket_failed
0 REPLIES 0