AnsweredAssumed Answered

MBEDTLS: creating a socket returns MBEDTLS_ERR_NET_SOCKET_FAILED

Question asked by Evgeniy Vasyliev on Apr 5, 2018

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;



// 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)



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 */
      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;


Can anyone please suggest what I am doing wrong?
Thanks for any useful response...