From 6e315a900932f50c85b281431a1a8a29c67216f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Fri, 13 Dec 2013 16:21:25 +0100 Subject: [PATCH] Adapt net_accept() to IPv6 --- include/polarssl/net.h | 3 ++- library/net.c | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/polarssl/net.h b/include/polarssl/net.h index 88302ac0a..22698b4ce 100644 --- a/include/polarssl/net.h +++ b/include/polarssl/net.h @@ -82,9 +82,10 @@ int net_bind( int *fd, const char *bind_ip, int port ); * \param bind_fd Relevant socket * \param client_fd Will contain the connected client socket * \param client_ip Will contain the client IP address + * Must be at least 4 bytes, or 16 if IPv6 is supported * * \return 0 if successful, POLARSSL_ERR_NET_ACCEPT_FAILED, or - * POLARSSL_ERR_NET_WOULD_BLOCK is bind_fd was set to + * POLARSSL_ERR_NET_WANT_READ is bind_fd was set to * non-blocking and accept() is blocking. */ int net_accept( int bind_fd, int *client_fd, void *client_ip ); diff --git a/library/net.c b/library/net.c index d02e3f269..f00e8ef81 100644 --- a/library/net.c +++ b/library/net.c @@ -364,7 +364,11 @@ static int net_is_blocking( void ) */ int net_accept( int bind_fd, int *client_fd, void *client_ip ) { +#if defined(POLARSSL_HAVE_IPV6) + struct sockaddr_storage client_addr; +#else struct sockaddr_in client_addr; +#endif #if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \ defined(_SOCKLEN_T_DECLARED) @@ -385,8 +389,25 @@ int net_accept( int bind_fd, int *client_fd, void *client_ip ) } if( client_ip != NULL ) + { +#if defined(POLARSSL_HAVE_IPV6) + if( client_addr.ss_family == AF_INET ) + { + struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr; + memcpy( client_ip, &addr4->sin_addr.s_addr, + sizeof( addr4->sin_addr.s_addr ) ); + } + else + { + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr; + memcpy( client_ip, &addr6->sin6_addr.s6_addr, + sizeof( addr6->sin6_addr.s6_addr ) ); + } +#else memcpy( client_ip, &client_addr.sin_addr.s_addr, sizeof( client_addr.sin_addr.s_addr ) ); +#endif /* POLARSSL_HAVE_IPV6 */ + } return( 0 ); }