mbedtls: mbedlts_net_bind should bind on all interfaces with bind_ip == NULL
As per mbedtls documentation. This is different to lwip_getaddrinfo() which uses loopback for a NULL address. Fixes bug mentioned here https://groups.google.com/forum/#!topic/esp-open-rtos/3KH5TZSTMUw
This commit is contained in:
parent
e2759f9e7d
commit
3dfa2272cc
2 changed files with 34 additions and 10 deletions
|
@ -158,7 +158,7 @@ void tls_server_task(void *pvParameters)
|
||||||
/*
|
/*
|
||||||
* 1. Start the connection
|
* 1. Start the connection
|
||||||
*/
|
*/
|
||||||
ret = mbedtls_net_bind(&server_ctx, "0.0.0.0", PORT, MBEDTLS_NET_PROTO_TCP);
|
ret = mbedtls_net_bind(&server_ctx, NULL, PORT, MBEDTLS_NET_PROTO_TCP);
|
||||||
if(ret != 0)
|
if(ret != 0)
|
||||||
{
|
{
|
||||||
printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
|
printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
|
||||||
|
|
|
@ -129,19 +129,41 @@ int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char
|
||||||
int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
|
int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
|
||||||
{
|
{
|
||||||
int n, ret;
|
int n, ret;
|
||||||
struct addrinfo hints, *addr_list, *cur;
|
struct addrinfo *addr_list, *cur;
|
||||||
|
|
||||||
|
/* Only request desired protocol */
|
||||||
|
const struct addrinfo hints = {
|
||||||
|
.ai_family = AF_UNSPEC,
|
||||||
|
.ai_socktype = (proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM),
|
||||||
|
.ai_protocol = (proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP),
|
||||||
|
};
|
||||||
|
|
||||||
|
struct sockaddr_in sockaddr_ipaddr_any = {
|
||||||
|
.sin_len = sizeof(struct sockaddr_in),
|
||||||
|
.sin_family = AF_INET,
|
||||||
|
.sin_port = htons(atoi(port)),
|
||||||
|
.sin_addr = { IPADDR_ANY },
|
||||||
|
};
|
||||||
|
|
||||||
|
struct addrinfo all_interfaces_addr = {
|
||||||
|
.ai_family = AF_INET,
|
||||||
|
.ai_socktype = hints.ai_socktype,
|
||||||
|
.ai_protocol = hints.ai_protocol,
|
||||||
|
.ai_addrlen = sizeof(struct sockaddr_in),
|
||||||
|
.ai_addr = (struct sockaddr *)&sockaddr_ipaddr_any,
|
||||||
|
};
|
||||||
|
|
||||||
if( ( ret = net_prepare() ) != 0 )
|
if( ( ret = net_prepare() ) != 0 )
|
||||||
return( ret );
|
return( ret );
|
||||||
|
|
||||||
/* Bind to IPv6 and/or IPv4, but only in the desired protocol */
|
if(bind_ip == NULL) {
|
||||||
memset( &hints, 0, sizeof( hints ) );
|
/* mbedTLS docs specify bind_ip == NULL means all interfaces, but lwip getaddrinfo() assumes NULL
|
||||||
hints.ai_family = AF_UNSPEC;
|
means localhost. So we swap in a precreated IPADDR_ANY addrinfo result here. */
|
||||||
hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
|
addr_list = &all_interfaces_addr;
|
||||||
hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
|
}
|
||||||
|
else if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 ) {
|
||||||
if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )
|
|
||||||
return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
|
return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
|
||||||
|
}
|
||||||
|
|
||||||
/* Try the sockaddrs until a binding succeeds */
|
/* Try the sockaddrs until a binding succeeds */
|
||||||
ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
|
ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
|
||||||
|
@ -187,7 +209,9 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(bind_ip != NULL) {
|
||||||
freeaddrinfo( addr_list );
|
freeaddrinfo( addr_list );
|
||||||
|
}
|
||||||
|
|
||||||
return( ret );
|
return( ret );
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue