Adapt net_accept() to IPv6

This commit is contained in:
Manuel Pégourié-Gonnard 2013-12-13 16:21:25 +01:00
parent 389ce63735
commit 6e315a9009
2 changed files with 23 additions and 1 deletions

View File

@ -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 );

View File

@ -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 );
}