2009-01-03 21:22:43 +00:00
|
|
|
/*
|
2014-09-17 08:56:54 +00:00
|
|
|
* TCP/IP or UDP/IP networking functions
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
2015-01-23 09:45:19 +00:00
|
|
|
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2015-03-06 13:17:10 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2009-01-04 16:27:10 +00:00
|
|
|
*
|
2009-01-03 21:22:43 +00: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.
|
|
|
|
*/
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 10:39:06 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_NET_C)
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/net.h"
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-10-28 17:48:30 +00:00
|
|
|
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2014-03-21 09:31:12 +00:00
|
|
|
#ifdef _WIN32_WINNT
|
|
|
|
#undef _WIN32_WINNT
|
|
|
|
#endif
|
|
|
|
/* Enables getaddrinfo() & Co */
|
2013-12-17 16:38:55 +00:00
|
|
|
#define _WIN32_WINNT 0x0501
|
2013-12-17 15:10:58 +00:00
|
|
|
#include <ws2tcpip.h>
|
|
|
|
|
2013-12-17 16:38:55 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <windows.h>
|
|
|
|
|
2013-12-30 14:42:43 +00:00
|
|
|
#if defined(_MSC_VER)
|
2009-01-03 21:22:43 +00:00
|
|
|
#if defined(_WIN32_WCE)
|
|
|
|
#pragma comment( lib, "ws2.lib" )
|
|
|
|
#else
|
|
|
|
#pragma comment( lib, "ws2_32.lib" )
|
|
|
|
#endif
|
2013-12-30 14:42:43 +00:00
|
|
|
#endif /* _MSC_VER */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2011-04-24 16:08:12 +00: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 21:22:43 +00:00
|
|
|
#define close(fd) closesocket(fd)
|
|
|
|
|
|
|
|
static int wsa_init_done = 0;
|
|
|
|
|
2014-06-17 12:34:44 +00:00
|
|
|
#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2009-01-03 21:22:43 +00:00
|
|
|
#include <sys/time.h>
|
2013-07-03 13:31:03 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <errno.h>
|
2009-07-27 21:09:47 +00:00
|
|
|
|
2014-06-17 12:34:44 +00:00
|
|
|
#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2013-07-03 13:31:03 +00:00
|
|
|
|
2013-12-13 11:54:09 +00:00
|
|
|
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2009-01-03 21:22:43 +00:00
|
|
|
#include <time.h>
|
2013-07-03 13:31:03 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-10-28 17:48:30 +00:00
|
|
|
#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
|
2012-10-01 14:41:15 +00:00
|
|
|
#include <basetsd.h>
|
|
|
|
typedef UINT32 uint32_t;
|
|
|
|
#else
|
|
|
|
#include <inttypes.h>
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2015-01-30 10:58:35 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
#define mbedtls_snprintf snprintf
|
2015-01-30 10:58:35 +00:00
|
|
|
#endif
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
2013-12-13 10:55:32 +00:00
|
|
|
* Prepare for using the sockets interface
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2013-12-17 14:57:05 +00:00
|
|
|
static int net_prepare( void )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-10-28 17:48:30 +00:00
|
|
|
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2009-01-03 21:22:43 +00:00
|
|
|
WSADATA wsaData;
|
|
|
|
|
|
|
|
if( wsa_init_done == 0 )
|
|
|
|
{
|
2014-05-15 00:54:37 +00:00
|
|
|
if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_SOCKET_FAILED );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
wsa_init_done = 1;
|
|
|
|
}
|
|
|
|
#else
|
2013-10-28 17:48:30 +00:00
|
|
|
#if !defined(EFIX64) && !defined(EFI32)
|
2009-01-03 21:22:43 +00:00
|
|
|
signal( SIGPIPE, SIG_IGN );
|
2013-10-28 17:48:30 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
2013-12-17 15:46:19 +00:00
|
|
|
return( 0 );
|
2013-12-13 10:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-03-23 16:38:16 +00:00
|
|
|
* Initiate a TCP connection with host:port and the given protocol
|
2013-12-13 10:55:32 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_net_connect( int *fd, const char *host, int port, int proto )
|
2013-12-13 10:55:32 +00:00
|
|
|
{
|
2013-12-17 14:57:05 +00:00
|
|
|
int ret;
|
2013-12-13 11:54:09 +00:00
|
|
|
struct addrinfo hints, *addr_list, *cur;
|
|
|
|
char port_str[6];
|
|
|
|
|
2013-12-17 14:57:05 +00:00
|
|
|
if( ( ret = net_prepare() ) != 0 )
|
|
|
|
return( ret );
|
2013-12-13 11:54:09 +00:00
|
|
|
|
|
|
|
/* getaddrinfo expects port as a string */
|
|
|
|
memset( port_str, 0, sizeof( port_str ) );
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_snprintf( port_str, sizeof( port_str ), "%d", port );
|
2013-12-13 11:54:09 +00:00
|
|
|
|
2014-03-23 16:38:16 +00:00
|
|
|
/* Do name resolution with both IPv6 and IPv4 */
|
2013-12-13 11:54:09 +00:00
|
|
|
memset( &hints, 0, sizeof( hints ) );
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
2015-04-08 10:49:31 +00:00
|
|
|
hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
|
|
|
|
hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
|
2013-12-13 11:54:09 +00:00
|
|
|
|
|
|
|
if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
|
2013-12-13 11:54:09 +00:00
|
|
|
|
|
|
|
/* Try the sockaddrs until a connection succeeds */
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
|
2013-12-13 11:54:09 +00:00
|
|
|
for( cur = addr_list; cur != NULL; cur = cur->ai_next )
|
|
|
|
{
|
2013-12-31 09:45:16 +00:00
|
|
|
*fd = (int) socket( cur->ai_family, cur->ai_socktype,
|
|
|
|
cur->ai_protocol );
|
2013-12-13 11:54:09 +00:00
|
|
|
if( *fd < 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
|
2013-12-13 11:54:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
|
|
|
|
{
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
close( *fd );
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
|
2013-12-13 11:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo( addr_list );
|
|
|
|
|
|
|
|
return( ret );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a listening socket on bind_ip:port
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_net_bind( int *fd, const char *bind_ip, int port, int proto )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-12-17 14:57:05 +00:00
|
|
|
int n, ret;
|
2013-12-13 13:00:51 +00:00
|
|
|
struct addrinfo hints, *addr_list, *cur;
|
|
|
|
char port_str[6];
|
|
|
|
|
2013-12-17 14:57:05 +00:00
|
|
|
if( ( ret = net_prepare() ) != 0 )
|
|
|
|
return( ret );
|
2013-12-13 13:00:51 +00:00
|
|
|
|
|
|
|
/* getaddrinfo expects port as a string */
|
|
|
|
memset( port_str, 0, sizeof( port_str ) );
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_snprintf( port_str, sizeof( port_str ), "%d", port );
|
2013-12-13 13:00:51 +00:00
|
|
|
|
|
|
|
/* Bind to IPv6 and/or IPv4, but only in TCP */
|
|
|
|
memset( &hints, 0, sizeof( hints ) );
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
2015-04-08 10:49:31 +00:00
|
|
|
hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
|
|
|
|
hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
|
2013-12-13 13:00:51 +00:00
|
|
|
if( bind_ip == NULL )
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
|
|
|
|
if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
|
2013-12-13 13:00:51 +00:00
|
|
|
|
|
|
|
/* Try the sockaddrs until a binding succeeds */
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
|
2013-12-13 13:00:51 +00:00
|
|
|
for( cur = addr_list; cur != NULL; cur = cur->ai_next )
|
|
|
|
{
|
2013-12-31 09:45:16 +00:00
|
|
|
*fd = (int) socket( cur->ai_family, cur->ai_socktype,
|
|
|
|
cur->ai_protocol );
|
2013-12-13 13:00:51 +00:00
|
|
|
if( *fd < 0 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
|
2013-12-13 13:00:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-12-17 12:59:01 +00:00
|
|
|
n = 1;
|
2014-04-17 10:43:05 +00:00
|
|
|
if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(const char *) &n, sizeof( n ) ) != 0 )
|
|
|
|
{
|
|
|
|
close( *fd );
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
|
2014-04-17 10:43:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-12-17 12:59:01 +00:00
|
|
|
|
2013-12-13 13:00:51 +00:00
|
|
|
if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
|
|
|
|
{
|
|
|
|
close( *fd );
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = MBEDTLS_ERR_NET_BIND_FAILED;
|
2013-12-13 13:00:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-23 16:38:16 +00:00
|
|
|
/* Listen only makes sense for TCP */
|
2015-04-08 10:49:31 +00:00
|
|
|
if( proto == MBEDTLS_NET_PROTO_TCP )
|
2013-12-13 13:00:51 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
if( listen( *fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
|
2014-03-23 16:38:16 +00:00
|
|
|
{
|
|
|
|
close( *fd );
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
|
2014-03-23 16:38:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-12-13 13:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* I we ever get there, it's a success */
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo( addr_list );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 19:59:49 +00:00
|
|
|
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
2014-01-23 19:59:49 +00:00
|
|
|
* Check if the requested operation would be blocking on a non-blocking socket
|
|
|
|
* and thus 'failed' with a negative return value.
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2014-01-23 19:59:49 +00:00
|
|
|
static int net_would_block( int fd )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2014-03-21 09:31:12 +00:00
|
|
|
((void) fd);
|
2009-01-03 21:22:43 +00:00
|
|
|
return( WSAGetLastError() == WSAEWOULDBLOCK );
|
2014-01-23 19:59:49 +00:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
#else
|
2014-01-23 19:59:49 +00:00
|
|
|
/*
|
|
|
|
* 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 );
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
switch( errno )
|
|
|
|
{
|
|
|
|
#if defined EAGAIN
|
|
|
|
case EAGAIN:
|
|
|
|
#endif
|
|
|
|
#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
#endif
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
return( 0 );
|
|
|
|
}
|
2014-06-17 12:34:44 +00:00
|
|
|
#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Accept a connection from a remote client
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_net_accept( int bind_fd, int *client_fd, void *client_ip )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2014-03-23 16:38:16 +00:00
|
|
|
int ret;
|
|
|
|
int type;
|
|
|
|
|
2013-12-13 15:21:25 +00:00
|
|
|
struct sockaddr_storage client_addr;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2011-12-20 12:19:03 +00:00
|
|
|
#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
|
|
|
|
defined(_SOCKLEN_T_DECLARED)
|
2009-01-03 21:22:43 +00:00
|
|
|
socklen_t n = (socklen_t) sizeof( client_addr );
|
2014-03-23 16:38:16 +00:00
|
|
|
socklen_t type_len = (socklen_t) sizeof( type );
|
2009-01-03 21:22:43 +00:00
|
|
|
#else
|
|
|
|
int n = (int) sizeof( client_addr );
|
2014-03-23 16:38:16 +00:00
|
|
|
int type_len = (int) sizeof( type );
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
|
|
|
|
2014-03-23 16:38:16 +00:00
|
|
|
/* Is this a TCP or UDP socket? */
|
2015-03-25 15:57:52 +00:00
|
|
|
if( getsockopt( bind_fd, SOL_SOCKET, SO_TYPE, (void *) &type, &type_len ) != 0 ||
|
2014-03-23 16:38:16 +00:00
|
|
|
( type != SOCK_STREAM && type != SOCK_DGRAM ) )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
|
2014-03-23 16:38:16 +00:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2014-03-23 16:38:16 +00:00
|
|
|
if( type == SOCK_STREAM )
|
|
|
|
{
|
|
|
|
/* TCP: actual accept() */
|
|
|
|
ret = *client_fd = (int) accept( bind_fd,
|
|
|
|
(struct sockaddr *) &client_addr, &n );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* UDP: wait for a message, but keep it in the queue */
|
|
|
|
char buf[1] = { 0 };
|
|
|
|
|
2015-01-12 18:02:15 +00:00
|
|
|
ret = recvfrom( bind_fd, buf, sizeof( buf ), MSG_PEEK,
|
2014-03-23 16:38:16 +00:00
|
|
|
(struct sockaddr *) &client_addr, &n );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ret < 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2014-07-21 11:42:54 +00:00
|
|
|
if( net_would_block( bind_fd ) != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_WANT_READ );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
2014-03-23 16:38:16 +00:00
|
|
|
/* UDP: hijack the listening socket for communicating with the client */
|
|
|
|
if( type != SOCK_STREAM )
|
|
|
|
{
|
|
|
|
if( connect( bind_fd, (struct sockaddr *) &client_addr, n ) != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
|
2014-03-23 16:38:16 +00:00
|
|
|
|
|
|
|
*client_fd = bind_fd;
|
|
|
|
}
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
if( client_ip != NULL )
|
2013-12-13 15:21:25 +00:00
|
|
|
{
|
|
|
|
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 ) );
|
|
|
|
}
|
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the socket blocking or non-blocking
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_net_set_block( int fd )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-10-28 17:48:30 +00:00
|
|
|
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2011-04-24 16:08:12 +00:00
|
|
|
u_long n = 0;
|
2009-01-03 21:22:43 +00:00
|
|
|
return( ioctlsocket( fd, FIONBIO, &n ) );
|
|
|
|
#else
|
|
|
|
return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_net_set_nonblock( int fd )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2013-10-28 17:48:30 +00:00
|
|
|
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2011-04-24 16:08:12 +00:00
|
|
|
u_long n = 1;
|
2009-01-03 21:22:43 +00:00
|
|
|
return( ioctlsocket( fd, FIONBIO, &n ) );
|
|
|
|
#else
|
|
|
|
return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Portable usleep helper
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_net_usleep( unsigned long usec )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
2014-11-21 08:52:23 +00:00
|
|
|
tv.tv_sec = usec / 1000000;
|
2014-10-20 11:59:19 +00:00
|
|
|
#if !defined(_WIN32) && ( defined(__unix__) || defined(__unix) || \
|
|
|
|
( defined(__APPLE__) && defined(__MACH__) ) )
|
2014-11-21 08:52:23 +00:00
|
|
|
tv.tv_usec = (suseconds_t) usec % 1000000;
|
2014-08-16 10:45:52 +00:00
|
|
|
#else
|
2014-11-21 08:52:23 +00:00
|
|
|
tv.tv_usec = usec % 1000000;
|
2014-08-16 10:45:52 +00:00
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
select( 0, NULL, NULL, NULL, &tv );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read at most 'len' characters
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
|
2013-10-28 17:48:30 +00:00
|
|
|
{
|
2014-01-23 19:59:49 +00:00
|
|
|
int fd = *((int *) ctx);
|
2014-08-16 10:45:52 +00:00
|
|
|
int ret = (int) read( fd, buf, len );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( ret < 0 )
|
|
|
|
{
|
2014-01-23 19:59:49 +00:00
|
|
|
if( net_would_block( fd ) != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_WANT_READ );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-10-28 17:48:30 +00:00
|
|
|
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2009-01-03 21:22:43 +00:00
|
|
|
if( WSAGetLastError() == WSAECONNRESET )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_CONN_RESET );
|
2009-01-03 21:22:43 +00:00
|
|
|
#else
|
|
|
|
if( errno == EPIPE || errno == ECONNRESET )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_CONN_RESET );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( errno == EINTR )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_WANT_READ );
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_RECV_FAILED );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2014-09-18 09:22:45 +00:00
|
|
|
/*
|
2014-10-01 13:01:39 +00:00
|
|
|
* Read at most 'len' characters, blocking for at most 'timeout' ms
|
2014-09-18 09:22:45 +00:00
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
|
2014-10-01 13:01:39 +00:00
|
|
|
uint32_t timeout )
|
2014-09-18 09:22:45 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set read_fds;
|
|
|
|
int fd = *((int *) ctx);
|
|
|
|
|
|
|
|
FD_ZERO( &read_fds );
|
|
|
|
FD_SET( fd, &read_fds );
|
|
|
|
|
2014-10-01 13:01:39 +00:00
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = ( timeout % 1000 ) * 1000;
|
2014-09-18 09:22:45 +00:00
|
|
|
|
|
|
|
ret = select( fd + 1, &read_fds, NULL, NULL, &tv );
|
|
|
|
|
|
|
|
/* Zero fds ready means we timed out */
|
|
|
|
if( ret == 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_TIMEOUT );
|
2014-09-18 09:22:45 +00:00
|
|
|
|
|
|
|
if( ret < 0 )
|
|
|
|
{
|
|
|
|
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
|
|
|
if( WSAGetLastError() == WSAEINTR )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_WANT_READ );
|
2014-09-18 09:22:45 +00:00
|
|
|
#else
|
|
|
|
if( errno == EINTR )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_WANT_READ );
|
2014-09-18 09:22:45 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_RECV_FAILED );
|
2014-09-18 09:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This call will not block */
|
2015-04-08 10:49:31 +00:00
|
|
|
return( mbedtls_net_recv( ctx, buf, len ) );
|
2014-09-18 09:22:45 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
2014-09-18 09:22:45 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Write at most 'len' characters
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2014-01-23 19:59:49 +00:00
|
|
|
int fd = *((int *) ctx);
|
2014-08-16 10:45:52 +00:00
|
|
|
int ret = (int) write( fd, buf, len );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( ret < 0 )
|
|
|
|
{
|
2014-01-23 19:59:49 +00:00
|
|
|
if( net_would_block( fd ) != 0 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_WANT_WRITE );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-10-28 17:48:30 +00:00
|
|
|
#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2009-01-03 21:22:43 +00:00
|
|
|
if( WSAGetLastError() == WSAECONNRESET )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_CONN_RESET );
|
2009-01-03 21:22:43 +00:00
|
|
|
#else
|
|
|
|
if( errno == EPIPE || errno == ECONNRESET )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_CONN_RESET );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
if( errno == EINTR )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_WANT_WRITE );
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_NET_SEND_FAILED );
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Gracefully close the connection
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_net_close( int fd )
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
shutdown( fd, 2 );
|
|
|
|
close( fd );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_NET_C */
|