mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-22 22:00:39 -06:00
PolarSSL: update to current stable version (1.3.4)
I just removed Externals/polarssl/, added the new version, then deleted the following files/directories: DartConfiguration.tcl Makefile doxygen/ library/Makefile programs/ scripts/ tests/ visualc/
This commit is contained in:
275
Externals/polarssl/library/net.c
vendored
275
Externals/polarssl/library/net.c
vendored
@ -1,7 +1,7 @@
|
||||
/*
|
||||
* TCP networking functions
|
||||
*
|
||||
* Copyright (C) 2006-2010, Brainspark B.V.
|
||||
* Copyright (C) 2006-2013, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
@ -29,16 +29,24 @@
|
||||
|
||||
#include "polarssl/net.h"
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN32_WCE)
|
||||
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
|
||||
!defined(EFI32)
|
||||
|
||||
#if defined(POLARSSL_HAVE_IPV6)
|
||||
#define _WIN32_WINNT 0x0501
|
||||
#include <ws2tcpip.h>
|
||||
#endif
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#if defined(_WIN32_WCE)
|
||||
#pragma comment( lib, "ws2.lib" )
|
||||
#else
|
||||
#pragma comment( lib, "ws2_32.lib" )
|
||||
#endif
|
||||
#endif /* _MSC_VER */
|
||||
|
||||
#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
|
||||
#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
|
||||
@ -52,7 +60,9 @@ static int wsa_init_done = 0;
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <fcntl.h>
|
||||
@ -62,10 +72,13 @@ static int wsa_init_done = 0;
|
||||
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
|
||||
defined(__DragonflyBSD__)
|
||||
#include <sys/endian.h>
|
||||
#elif defined(__APPLE__)
|
||||
#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
|
||||
defined(EFIX64) || defined(EFI32)
|
||||
#include <machine/endian.h>
|
||||
#elif defined(sun)
|
||||
#include <sys/isa_defs.h>
|
||||
#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
|
||||
#include <arpa/nameser_compat.h>
|
||||
#else
|
||||
#include <endian.h>
|
||||
#endif
|
||||
@ -74,9 +87,17 @@ static int wsa_init_done = 0;
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
|
||||
!defined(EFI32)
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
|
||||
#include <basetsd.h>
|
||||
typedef UINT32 uint32_t;
|
||||
#else
|
||||
@ -86,7 +107,7 @@ typedef UINT32 uint32_t;
|
||||
/*
|
||||
* htons() is not always available.
|
||||
* By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
|
||||
* to help determine endianess.
|
||||
* to help determine endianness.
|
||||
*/
|
||||
#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
|
||||
#define POLARSSL_HTONS(n) (n)
|
||||
@ -106,14 +127,12 @@ unsigned long net_htonl(unsigned long n);
|
||||
#define net_htonl(n) POLARSSL_HTONL(n)
|
||||
|
||||
/*
|
||||
* Initiate a TCP connection with host:port
|
||||
* Prepare for using the sockets interface
|
||||
*/
|
||||
int net_connect( int *fd, const char *host, int port )
|
||||
static int net_prepare( void )
|
||||
{
|
||||
struct sockaddr_in server_addr;
|
||||
struct hostent *server_host;
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN32_WCE)
|
||||
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
||||
!defined(EFI32)
|
||||
WSADATA wsaData;
|
||||
|
||||
if( wsa_init_done == 0 )
|
||||
@ -124,13 +143,79 @@ int net_connect( int *fd, const char *host, int port )
|
||||
wsa_init_done = 1;
|
||||
}
|
||||
#else
|
||||
#if !defined(EFIX64) && !defined(EFI32)
|
||||
signal( SIGPIPE, SIG_IGN );
|
||||
#endif
|
||||
#endif
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
* Initiate a TCP connection with host:port
|
||||
*/
|
||||
int net_connect( int *fd, const char *host, int port )
|
||||
{
|
||||
#if defined(POLARSSL_HAVE_IPV6)
|
||||
int ret;
|
||||
struct addrinfo hints, *addr_list, *cur;
|
||||
char port_str[6];
|
||||
|
||||
if( ( ret = net_prepare() ) != 0 )
|
||||
return( ret );
|
||||
|
||||
/* getaddrinfo expects port as a string */
|
||||
memset( port_str, 0, sizeof( port_str ) );
|
||||
snprintf( port_str, sizeof( port_str ), "%d", port );
|
||||
|
||||
/* Do name resolution with both IPv6 and IPv4, but only TCP */
|
||||
memset( &hints, 0, sizeof( hints ) );
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_TCP;
|
||||
|
||||
if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
|
||||
return( POLARSSL_ERR_NET_UNKNOWN_HOST );
|
||||
|
||||
/* Try the sockaddrs until a connection succeeds */
|
||||
ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
|
||||
for( cur = addr_list; cur != NULL; cur = cur->ai_next )
|
||||
{
|
||||
*fd = (int) socket( cur->ai_family, cur->ai_socktype,
|
||||
cur->ai_protocol );
|
||||
if( *fd < 0 )
|
||||
{
|
||||
ret = POLARSSL_ERR_NET_SOCKET_FAILED;
|
||||
continue;
|
||||
}
|
||||
|
||||
if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
|
||||
{
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
close( *fd );
|
||||
ret = POLARSSL_ERR_NET_CONNECT_FAILED;
|
||||
}
|
||||
|
||||
freeaddrinfo( addr_list );
|
||||
|
||||
return( ret );
|
||||
|
||||
#else
|
||||
/* Legacy IPv4-only version */
|
||||
|
||||
int ret;
|
||||
struct sockaddr_in server_addr;
|
||||
struct hostent *server_host;
|
||||
|
||||
if( ( ret = net_prepare() ) != 0 )
|
||||
return( ret );
|
||||
|
||||
if( ( server_host = gethostbyname( host ) ) == NULL )
|
||||
return( POLARSSL_ERR_NET_UNKNOWN_HOST );
|
||||
|
||||
if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
|
||||
if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
|
||||
return( POLARSSL_ERR_NET_SOCKET_FAILED );
|
||||
|
||||
memcpy( (void *) &server_addr.sin_addr,
|
||||
@ -148,6 +233,7 @@ int net_connect( int *fd, const char *host, int port )
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
#endif /* POLARSSL_HAVE_IPV6 */
|
||||
}
|
||||
|
||||
/*
|
||||
@ -155,24 +241,78 @@ int net_connect( int *fd, const char *host, int port )
|
||||
*/
|
||||
int net_bind( int *fd, const char *bind_ip, int port )
|
||||
{
|
||||
int n, c[4];
|
||||
#if defined(POLARSSL_HAVE_IPV6)
|
||||
int n, ret;
|
||||
struct addrinfo hints, *addr_list, *cur;
|
||||
char port_str[6];
|
||||
|
||||
if( ( ret = net_prepare() ) != 0 )
|
||||
return( ret );
|
||||
|
||||
/* getaddrinfo expects port as a string */
|
||||
memset( port_str, 0, sizeof( port_str ) );
|
||||
snprintf( port_str, sizeof( port_str ), "%d", port );
|
||||
|
||||
/* Bind to IPv6 and/or IPv4, but only in TCP */
|
||||
memset( &hints, 0, sizeof( hints ) );
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_TCP;
|
||||
if( bind_ip == NULL )
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
|
||||
if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
|
||||
return( POLARSSL_ERR_NET_UNKNOWN_HOST );
|
||||
|
||||
/* Try the sockaddrs until a binding succeeds */
|
||||
ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
|
||||
for( cur = addr_list; cur != NULL; cur = cur->ai_next )
|
||||
{
|
||||
*fd = (int) socket( cur->ai_family, cur->ai_socktype,
|
||||
cur->ai_protocol );
|
||||
if( *fd < 0 )
|
||||
{
|
||||
ret = POLARSSL_ERR_NET_SOCKET_FAILED;
|
||||
continue;
|
||||
}
|
||||
|
||||
n = 1;
|
||||
setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
|
||||
(const char *) &n, sizeof( n ) );
|
||||
|
||||
if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
|
||||
{
|
||||
close( *fd );
|
||||
ret = POLARSSL_ERR_NET_BIND_FAILED;
|
||||
continue;
|
||||
}
|
||||
|
||||
if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
|
||||
{
|
||||
close( *fd );
|
||||
ret = POLARSSL_ERR_NET_LISTEN_FAILED;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* I we ever get there, it's a success */
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
freeaddrinfo( addr_list );
|
||||
|
||||
return( ret );
|
||||
|
||||
#else
|
||||
/* Legacy IPv4-only version */
|
||||
|
||||
int ret, n, c[4];
|
||||
struct sockaddr_in server_addr;
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN32_WCE)
|
||||
WSADATA wsaData;
|
||||
if( ( ret = net_prepare() ) != 0 )
|
||||
return( ret );
|
||||
|
||||
if( wsa_init_done == 0 )
|
||||
{
|
||||
if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
|
||||
return( POLARSSL_ERR_NET_SOCKET_FAILED );
|
||||
|
||||
wsa_init_done = 1;
|
||||
}
|
||||
#else
|
||||
signal( SIGPIPE, SIG_IGN );
|
||||
#endif
|
||||
|
||||
if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
|
||||
if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
|
||||
return( POLARSSL_ERR_NET_SOCKET_FAILED );
|
||||
|
||||
n = 1;
|
||||
@ -214,16 +354,34 @@ int net_bind( int *fd, const char *bind_ip, int port )
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
#endif /* POLARSSL_HAVE_IPV6 */
|
||||
}
|
||||
|
||||
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
||||
!defined(EFI32)
|
||||
/*
|
||||
* Check if the current operation is blocking
|
||||
* Check if the requested operation would be blocking on a non-blocking socket
|
||||
* and thus 'failed' with a negative return value.
|
||||
*/
|
||||
static int net_is_blocking( void )
|
||||
static int net_would_block( int fd )
|
||||
{
|
||||
#if defined(_WIN32) || defined(_WIN32_WCE)
|
||||
return( WSAGetLastError() == WSAEWOULDBLOCK );
|
||||
}
|
||||
#else
|
||||
/*
|
||||
* Check if the requested operation would be blocking on a non-blocking socket
|
||||
* and thus 'failed' with a negative return value.
|
||||
*
|
||||
* Note: on a blocking socket this function always returns 0!
|
||||
*/
|
||||
static int net_would_block( int fd )
|
||||
{
|
||||
/*
|
||||
* Never return 'WOULD BLOCK' on a non-blocking socket
|
||||
*/
|
||||
if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
|
||||
return( 0 );
|
||||
|
||||
switch( errno )
|
||||
{
|
||||
#if defined EAGAIN
|
||||
@ -235,15 +393,19 @@ static int net_is_blocking( void )
|
||||
return( 1 );
|
||||
}
|
||||
return( 0 );
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Accept a connection from a remote client
|
||||
*/
|
||||
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)
|
||||
@ -252,20 +414,37 @@ int net_accept( int bind_fd, int *client_fd, void *client_ip )
|
||||
int n = (int) sizeof( client_addr );
|
||||
#endif
|
||||
|
||||
*client_fd = accept( bind_fd, (struct sockaddr *)
|
||||
&client_addr, &n );
|
||||
*client_fd = (int) accept( bind_fd, (struct sockaddr *)
|
||||
&client_addr, &n );
|
||||
|
||||
if( *client_fd < 0 )
|
||||
{
|
||||
if( net_is_blocking() != 0 )
|
||||
if( net_would_block( *client_fd ) != 0 )
|
||||
return( POLARSSL_ERR_NET_WANT_READ );
|
||||
|
||||
return( POLARSSL_ERR_NET_ACCEPT_FAILED );
|
||||
}
|
||||
|
||||
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 );
|
||||
}
|
||||
@ -275,7 +454,8 @@ int net_accept( int bind_fd, int *client_fd, void *client_ip )
|
||||
*/
|
||||
int net_set_block( int fd )
|
||||
{
|
||||
#if defined(_WIN32) || defined(_WIN32_WCE)
|
||||
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
||||
!defined(EFI32)
|
||||
u_long n = 0;
|
||||
return( ioctlsocket( fd, FIONBIO, &n ) );
|
||||
#else
|
||||
@ -285,7 +465,8 @@ int net_set_block( int fd )
|
||||
|
||||
int net_set_nonblock( int fd )
|
||||
{
|
||||
#if defined(_WIN32) || defined(_WIN32_WCE)
|
||||
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
||||
!defined(EFI32)
|
||||
u_long n = 1;
|
||||
return( ioctlsocket( fd, FIONBIO, &n ) );
|
||||
#else
|
||||
@ -293,6 +474,7 @@ int net_set_nonblock( int fd )
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_HAVE_TIME)
|
||||
/*
|
||||
* Portable usleep helper
|
||||
*/
|
||||
@ -303,20 +485,23 @@ void net_usleep( unsigned long usec )
|
||||
tv.tv_usec = usec;
|
||||
select( 0, NULL, NULL, NULL, &tv );
|
||||
}
|
||||
#endif /* POLARSSL_HAVE_TIME */
|
||||
|
||||
/*
|
||||
* Read at most 'len' characters
|
||||
*/
|
||||
int net_recv( void *ctx, unsigned char *buf, size_t len )
|
||||
{
|
||||
int ret = read( *((int *) ctx), buf, len );
|
||||
{
|
||||
int fd = *((int *) ctx);
|
||||
int ret = read( fd, buf, len );
|
||||
|
||||
if( ret < 0 )
|
||||
{
|
||||
if( net_is_blocking() != 0 )
|
||||
if( net_would_block( fd ) != 0 )
|
||||
return( POLARSSL_ERR_NET_WANT_READ );
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN32_WCE)
|
||||
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
||||
!defined(EFI32)
|
||||
if( WSAGetLastError() == WSAECONNRESET )
|
||||
return( POLARSSL_ERR_NET_CONN_RESET );
|
||||
#else
|
||||
@ -338,14 +523,16 @@ int net_recv( void *ctx, unsigned char *buf, size_t len )
|
||||
*/
|
||||
int net_send( void *ctx, const unsigned char *buf, size_t len )
|
||||
{
|
||||
int ret = write( *((int *) ctx), buf, len );
|
||||
int fd = *((int *) ctx);
|
||||
int ret = write( fd, buf, len );
|
||||
|
||||
if( ret < 0 )
|
||||
{
|
||||
if( net_is_blocking() != 0 )
|
||||
if( net_would_block( fd ) != 0 )
|
||||
return( POLARSSL_ERR_NET_WANT_WRITE );
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN32_WCE)
|
||||
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
||||
!defined(EFI32)
|
||||
if( WSAGetLastError() == WSAECONNRESET )
|
||||
return( POLARSSL_ERR_NET_CONN_RESET );
|
||||
#else
|
||||
|
Reference in New Issue
Block a user