2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* TCP networking functions
|
|
|
|
*
|
2013-07-03 15:31:03 +02:00
|
|
|
* Copyright (C) 2006-2013, Brainspark B.V.
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
|
|
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
2010-07-18 12:13:04 +02:00
|
|
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2009-07-28 19:23:11 +02:00
|
|
|
* All rights reserved.
|
2009-01-04 17:27:10 +01:00
|
|
|
*
|
2009-01-03 22:22:43 +01:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2009-01-03 22:51:57 +01:00
|
|
|
#include "polarssl/config.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-01-03 22:51:57 +01:00
|
|
|
#if defined(POLARSSL_NET_C)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-01-03 22:51:57 +01:00
|
|
|
#include "polarssl/net.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-10-28 18:48:30 +01:00
|
|
|
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#if defined(_WIN32_WCE)
|
|
|
|
#pragma comment( lib, "ws2.lib" )
|
|
|
|
#else
|
|
|
|
#pragma comment( lib, "ws2_32.lib" )
|
|
|
|
#endif
|
|
|
|
|
2011-04-24 18:08:12 +02:00
|
|
|
#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
|
|
|
|
#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
|
2009-01-03 22:22:43 +01:00
|
|
|
#define close(fd) closesocket(fd)
|
|
|
|
|
|
|
|
static int wsa_init_done = 0;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2013-07-03 15:31:03 +02:00
|
|
|
#if defined(POLARSSL_HAVE_TIME)
|
2009-01-03 22:22:43 +01:00
|
|
|
#include <sys/time.h>
|
2013-07-03 15:31:03 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <errno.h>
|
2009-07-27 23:09:47 +02:00
|
|
|
|
2012-08-23 09:45:37 +02:00
|
|
|
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
|
|
|
|
defined(__DragonflyBSD__)
|
2009-07-19 22:50:11 +02:00
|
|
|
#include <sys/endian.h>
|
2013-10-28 18:48:30 +01:00
|
|
|
#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
|
|
|
|
defined(EFIX64) || defined(EFI32)
|
2009-07-27 23:09:47 +02:00
|
|
|
#include <machine/endian.h>
|
2012-04-03 09:54:30 +02:00
|
|
|
#elif defined(sun)
|
|
|
|
#include <sys/isa_defs.h>
|
2013-07-26 14:10:22 +02:00
|
|
|
#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
|
|
|
|
#include <arpa/nameser_compat.h>
|
2009-07-19 22:50:11 +02:00
|
|
|
#else
|
2009-04-19 20:55:16 +02:00
|
|
|
#include <endian.h>
|
2009-07-19 22:50:11 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2013-07-03 15:31:03 +02:00
|
|
|
|
2013-12-13 12:54:09 +01:00
|
|
|
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
2013-07-03 15:31:03 +02:00
|
|
|
#if defined(POLARSSL_HAVE_TIME)
|
2009-01-03 22:22:43 +01:00
|
|
|
#include <time.h>
|
2013-07-03 15:31:03 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-10-28 18:48:30 +01:00
|
|
|
#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
|
2012-10-01 16:41:15 +02:00
|
|
|
#include <basetsd.h>
|
|
|
|
typedef UINT32 uint32_t;
|
|
|
|
#else
|
|
|
|
#include <inttypes.h>
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2009-04-19 20:55:16 +02:00
|
|
|
* htons() is not always available.
|
|
|
|
* By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
|
2013-10-29 10:02:51 +01:00
|
|
|
* to help determine endianness.
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2009-04-19 20:55:16 +02:00
|
|
|
#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
|
2009-07-27 23:09:47 +02:00
|
|
|
#define POLARSSL_HTONS(n) (n)
|
2013-03-06 16:55:11 +01:00
|
|
|
#define POLARSSL_HTONL(n) (n)
|
2009-04-19 20:55:16 +02:00
|
|
|
#else
|
2013-03-06 16:55:11 +01:00
|
|
|
#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
|
|
|
|
(((unsigned short)(n) & 0xFF00 ) >> 8 ))
|
|
|
|
#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
|
|
|
|
(((unsigned long )(n) & 0xFF00 ) << 8 ) | \
|
|
|
|
(((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
|
|
|
|
(((unsigned long )(n) & 0xFF000000) >> 24))
|
2009-04-19 20:55:16 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-04-19 20:55:16 +02:00
|
|
|
unsigned short net_htons(unsigned short n);
|
2013-03-06 16:55:11 +01:00
|
|
|
unsigned long net_htonl(unsigned long n);
|
2009-07-27 23:09:47 +02:00
|
|
|
#define net_htons(n) POLARSSL_HTONS(n)
|
2013-03-06 16:55:11 +01:00
|
|
|
#define net_htonl(n) POLARSSL_HTONL(n)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
2013-12-13 11:55:32 +01:00
|
|
|
* Prepare for using the sockets interface
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2013-12-13 11:55:32 +01:00
|
|
|
static void net_prepare( void )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-10-28 18:48:30 +01:00
|
|
|
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2009-01-03 22:22:43 +01:00
|
|
|
WSADATA wsaData;
|
|
|
|
|
|
|
|
if( wsa_init_done == 0 )
|
|
|
|
{
|
|
|
|
if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_NET_SOCKET_FAILED );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
wsa_init_done = 1;
|
|
|
|
}
|
|
|
|
#else
|
2013-10-28 18:48:30 +01:00
|
|
|
#if !defined(EFIX64) && !defined(EFI32)
|
2009-01-03 22:22:43 +01:00
|
|
|
signal( SIGPIPE, SIG_IGN );
|
2013-10-28 18:48:30 +01:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
2013-12-13 11:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initiate a TCP connection with host:port
|
|
|
|
*/
|
|
|
|
int net_connect( int *fd, const char *host, int port )
|
|
|
|
{
|
2013-12-13 12:54:09 +01:00
|
|
|
#if defined(POLARSSL_HAVE_IPV6)
|
|
|
|
int ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
|
|
|
|
struct addrinfo hints, *addr_list, *cur;
|
|
|
|
char port_str[6];
|
|
|
|
|
|
|
|
net_prepare();
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
for( cur = addr_list; cur != NULL; cur = cur->ai_next )
|
|
|
|
{
|
|
|
|
*fd = 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 */
|
|
|
|
|
2013-12-13 11:55:32 +01:00
|
|
|
struct sockaddr_in server_addr;
|
|
|
|
struct hostent *server_host;
|
|
|
|
|
|
|
|
net_prepare();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( ( server_host = gethostbyname( host ) ) == NULL )
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_NET_UNKNOWN_HOST );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-10-14 16:33:24 +02:00
|
|
|
if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_NET_SOCKET_FAILED );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
memcpy( (void *) &server_addr.sin_addr,
|
|
|
|
(void *) server_host->h_addr,
|
|
|
|
server_host->h_length );
|
|
|
|
|
|
|
|
server_addr.sin_family = AF_INET;
|
|
|
|
server_addr.sin_port = net_htons( port );
|
|
|
|
|
|
|
|
if( connect( *fd, (struct sockaddr *) &server_addr,
|
|
|
|
sizeof( server_addr ) ) < 0 )
|
|
|
|
{
|
|
|
|
close( *fd );
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_NET_CONNECT_FAILED );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
2013-12-13 12:54:09 +01:00
|
|
|
#endif /* POLARSSL_HAVE_IPV6 */
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a listening socket on bind_ip:port
|
|
|
|
*/
|
2010-03-16 22:09:09 +01:00
|
|
|
int net_bind( int *fd, const char *bind_ip, int port )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-12-13 14:00:51 +01:00
|
|
|
#if defined(POLARSSL_HAVE_IPV6)
|
|
|
|
int ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
|
|
|
|
struct addrinfo hints, *addr_list, *cur;
|
|
|
|
char port_str[6];
|
|
|
|
|
|
|
|
net_prepare();
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
for( cur = addr_list; cur != NULL; cur = cur->ai_next )
|
|
|
|
{
|
|
|
|
*fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
|
|
|
|
if( *fd < 0 )
|
|
|
|
{
|
|
|
|
ret = POLARSSL_ERR_NET_SOCKET_FAILED;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
int n, c[4];
|
|
|
|
struct sockaddr_in server_addr;
|
|
|
|
|
2013-12-13 11:55:32 +01:00
|
|
|
net_prepare();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-10-14 16:33:24 +02:00
|
|
|
if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_NET_SOCKET_FAILED );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
n = 1;
|
|
|
|
setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(const char *) &n, sizeof( n ) );
|
|
|
|
|
2013-03-06 16:55:11 +01:00
|
|
|
server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
|
2009-01-03 22:22:43 +01:00
|
|
|
server_addr.sin_family = AF_INET;
|
|
|
|
server_addr.sin_port = net_htons( port );
|
|
|
|
|
|
|
|
if( bind_ip != NULL )
|
|
|
|
{
|
|
|
|
memset( c, 0, sizeof( c ) );
|
|
|
|
sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
|
|
|
|
|
|
|
|
for( n = 0; n < 4; n++ )
|
|
|
|
if( c[n] < 0 || c[n] > 255 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if( n == 4 )
|
2013-03-06 16:55:11 +01:00
|
|
|
server_addr.sin_addr.s_addr = net_htonl(
|
2012-10-01 16:41:15 +02:00
|
|
|
( (uint32_t) c[0] << 24 ) |
|
|
|
|
( (uint32_t) c[1] << 16 ) |
|
|
|
|
( (uint32_t) c[2] << 8 ) |
|
2013-03-06 16:55:11 +01:00
|
|
|
( (uint32_t) c[3] ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( bind( *fd, (struct sockaddr *) &server_addr,
|
|
|
|
sizeof( server_addr ) ) < 0 )
|
|
|
|
{
|
|
|
|
close( *fd );
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_NET_BIND_FAILED );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2011-05-20 14:31:31 +02:00
|
|
|
if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
close( *fd );
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_NET_LISTEN_FAILED );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
2013-12-13 14:00:51 +01:00
|
|
|
#endif /* POLARSSL_HAVE_IPV6 */
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the current operation is blocking
|
|
|
|
*/
|
|
|
|
static int net_is_blocking( void )
|
|
|
|
{
|
2013-10-28 18:48:30 +01:00
|
|
|
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2009-01-03 22:22:43 +01:00
|
|
|
return( WSAGetLastError() == WSAEWOULDBLOCK );
|
|
|
|
#else
|
|
|
|
switch( errno )
|
|
|
|
{
|
|
|
|
#if defined EAGAIN
|
|
|
|
case EAGAIN:
|
|
|
|
#endif
|
|
|
|
#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
#endif
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
return( 0 );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Accept a connection from a remote client
|
|
|
|
*/
|
|
|
|
int net_accept( int bind_fd, int *client_fd, void *client_ip )
|
|
|
|
{
|
|
|
|
struct sockaddr_in client_addr;
|
|
|
|
|
2011-12-20 13:19:03 +01:00
|
|
|
#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
|
|
|
|
defined(_SOCKLEN_T_DECLARED)
|
2009-01-03 22:22:43 +01:00
|
|
|
socklen_t n = (socklen_t) sizeof( client_addr );
|
|
|
|
#else
|
|
|
|
int n = (int) sizeof( client_addr );
|
|
|
|
#endif
|
|
|
|
|
2013-10-14 16:33:24 +02:00
|
|
|
*client_fd = (int) accept( bind_fd, (struct sockaddr *)
|
|
|
|
&client_addr, &n );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( *client_fd < 0 )
|
|
|
|
{
|
|
|
|
if( net_is_blocking() != 0 )
|
2011-05-18 15:32:51 +02:00
|
|
|
return( POLARSSL_ERR_NET_WANT_READ );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_NET_ACCEPT_FAILED );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( client_ip != NULL )
|
|
|
|
memcpy( client_ip, &client_addr.sin_addr.s_addr,
|
|
|
|
sizeof( client_addr.sin_addr.s_addr ) );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the socket blocking or non-blocking
|
|
|
|
*/
|
|
|
|
int net_set_block( int fd )
|
|
|
|
{
|
2013-10-28 18:48:30 +01:00
|
|
|
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2011-04-24 18:08:12 +02:00
|
|
|
u_long n = 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ioctlsocket( fd, FIONBIO, &n ) );
|
|
|
|
#else
|
|
|
|
return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int net_set_nonblock( int fd )
|
|
|
|
{
|
2013-10-28 18:48:30 +01:00
|
|
|
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2011-04-24 18:08:12 +02:00
|
|
|
u_long n = 1;
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ioctlsocket( fd, FIONBIO, &n ) );
|
|
|
|
#else
|
|
|
|
return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-07-03 15:31:03 +02:00
|
|
|
#if defined(POLARSSL_HAVE_TIME)
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Portable usleep helper
|
|
|
|
*/
|
|
|
|
void net_usleep( unsigned long usec )
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = usec;
|
|
|
|
select( 0, NULL, NULL, NULL, &tv );
|
|
|
|
}
|
2013-07-03 15:31:03 +02:00
|
|
|
#endif /* POLARSSL_HAVE_TIME */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read at most 'len' characters
|
|
|
|
*/
|
2011-04-24 10:57:21 +02:00
|
|
|
int net_recv( void *ctx, unsigned char *buf, size_t len )
|
2013-10-28 18:48:30 +01:00
|
|
|
{
|
2009-01-03 22:22:43 +01:00
|
|
|
int ret = read( *((int *) ctx), buf, len );
|
|
|
|
|
|
|
|
if( ret < 0 )
|
|
|
|
{
|
|
|
|
if( net_is_blocking() != 0 )
|
2011-05-18 15:32:51 +02:00
|
|
|
return( POLARSSL_ERR_NET_WANT_READ );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-10-28 18:48:30 +01:00
|
|
|
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2009-01-03 22:22:43 +01:00
|
|
|
if( WSAGetLastError() == WSAECONNRESET )
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_NET_CONN_RESET );
|
2009-01-03 22:22:43 +01:00
|
|
|
#else
|
|
|
|
if( errno == EPIPE || errno == ECONNRESET )
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_NET_CONN_RESET );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( errno == EINTR )
|
2011-05-18 15:32:51 +02:00
|
|
|
return( POLARSSL_ERR_NET_WANT_READ );
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_NET_RECV_FAILED );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write at most 'len' characters
|
|
|
|
*/
|
2011-06-21 09:36:43 +02:00
|
|
|
int net_send( void *ctx, const unsigned char *buf, size_t len )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
int ret = write( *((int *) ctx), buf, len );
|
|
|
|
|
|
|
|
if( ret < 0 )
|
|
|
|
{
|
|
|
|
if( net_is_blocking() != 0 )
|
2011-05-18 15:32:51 +02:00
|
|
|
return( POLARSSL_ERR_NET_WANT_WRITE );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-10-28 18:48:30 +01:00
|
|
|
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2009-01-03 22:22:43 +01:00
|
|
|
if( WSAGetLastError() == WSAECONNRESET )
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_NET_CONN_RESET );
|
2009-01-03 22:22:43 +01:00
|
|
|
#else
|
|
|
|
if( errno == EPIPE || errno == ECONNRESET )
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_NET_CONN_RESET );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
if( errno == EINTR )
|
2011-05-18 15:32:51 +02:00
|
|
|
return( POLARSSL_ERR_NET_WANT_WRITE );
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
|
2009-01-03 22:51:57 +01:00
|
|
|
return( POLARSSL_ERR_NET_SEND_FAILED );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Gracefully close the connection
|
|
|
|
*/
|
|
|
|
void net_close( int fd )
|
|
|
|
{
|
|
|
|
shutdown( fd, 2 );
|
|
|
|
close( fd );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|