blob: 8918fb555246e11394226bb6228d8aa262a2d740 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * TCP networking functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker5121ce52009-01-03 21:22:43 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000025#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
27#include POLARSSL_CONFIG_FILE
28#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#if defined(POLARSSL_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakkerfa6a6202013-10-28 18:48:30 +010034#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
35 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010037#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010038#ifdef _WIN32_WINNT
39#undef _WIN32_WINNT
40#endif
41/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010042#define _WIN32_WINNT 0x0501
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010043#include <ws2tcpip.h>
44#endif
45
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010046#include <winsock2.h>
47#include <windows.h>
48
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010049#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000050#if defined(_WIN32_WCE)
51#pragma comment( lib, "ws2.lib" )
52#else
53#pragma comment( lib, "ws2_32.lib" )
54#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010055#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Paul Bakkerf4f69682011-04-24 16:08:12 +000057#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
58#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000059#define close(fd) closesocket(fd)
60
61static int wsa_init_done = 0;
62
Paul Bakkerdb20c102014-06-17 14:34:44 +020063#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000064
65#include <sys/types.h>
66#include <sys/socket.h>
67#include <netinet/in.h>
68#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020069#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000070#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020071#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000072#include <unistd.h>
73#include <signal.h>
74#include <fcntl.h>
75#include <netdb.h>
76#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000077
Paul Bakker6a2f8572012-08-23 07:45:37 +000078#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
Markus Pfeiffera26a0052014-04-22 20:16:15 +000079 defined(__DragonFly__)
Paul Bakker854963c2009-07-19 20:50:11 +000080#include <sys/endian.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010081#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
82 defined(EFIX64) || defined(EFI32)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000083#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000084#elif defined(sun)
85#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020086#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
87#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000088#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000089#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000090#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Paul Bakkerdb20c102014-06-17 14:34:44 +020092#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Paul Bakker5121ce52009-01-03 21:22:43 +000094#include <stdlib.h>
95#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020096
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +010097#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
98 !defined(EFI32)
99#define snprintf _snprintf
100#endif
101
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200102#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000103#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200104#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100106#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000107#include <basetsd.h>
108typedef UINT32 uint32_t;
109#else
110#include <inttypes.h>
111#endif
112
Paul Bakker5121ce52009-01-03 21:22:43 +0000113/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000114 * htons() is not always available.
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200115 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and
116 * __BIG_ENDIAN to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200118#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
119 __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000120#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100121#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000122#else
Paul Bakker37286a52013-03-06 16:55:11 +0100123#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
124 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
125#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
126 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
127 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
128 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000129#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Paul Bakker66d5d072014-06-17 16:39:18 +0200131unsigned short net_htons( unsigned short n );
132unsigned long net_htonl( unsigned long n );
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000133#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100134#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
136/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100137 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100139static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100141#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
142 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 WSADATA wsaData;
144
145 if( wsa_init_done == 0 )
146 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200147 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000148 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
150 wsa_init_done = 1;
151 }
152#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100153#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 signal( SIGPIPE, SIG_IGN );
155#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100156#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100157 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100158}
159
160/*
161 * Initiate a TCP connection with host:port
162 */
163int net_connect( int *fd, const char *host, int port )
164{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100165#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100166 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100167 struct addrinfo hints, *addr_list, *cur;
168 char port_str[6];
169
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100170 if( ( ret = net_prepare() ) != 0 )
171 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100172
173 /* getaddrinfo expects port as a string */
174 memset( port_str, 0, sizeof( port_str ) );
175 snprintf( port_str, sizeof( port_str ), "%d", port );
176
177 /* Do name resolution with both IPv6 and IPv4, but only TCP */
178 memset( &hints, 0, sizeof( hints ) );
179 hints.ai_family = AF_UNSPEC;
180 hints.ai_socktype = SOCK_STREAM;
181 hints.ai_protocol = IPPROTO_TCP;
182
183 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
184 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
185
186 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100187 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100188 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
189 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100190 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
191 cur->ai_protocol );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100192 if( *fd < 0 )
193 {
194 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
195 continue;
196 }
197
198 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
199 {
200 ret = 0;
201 break;
202 }
203
204 close( *fd );
205 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
206 }
207
208 freeaddrinfo( addr_list );
209
210 return( ret );
211
212#else
213 /* Legacy IPv4-only version */
214
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100215 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100216 struct sockaddr_in server_addr;
217 struct hostent *server_host;
218
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100219 if( ( ret = net_prepare() ) != 0 )
220 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000223 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
Paul Bakkerbbc10072013-10-14 16:33:24 +0200225 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000226 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
228 memcpy( (void *) &server_addr.sin_addr,
229 (void *) server_host->h_addr,
230 server_host->h_length );
231
232 server_addr.sin_family = AF_INET;
233 server_addr.sin_port = net_htons( port );
234
235 if( connect( *fd, (struct sockaddr *) &server_addr,
236 sizeof( server_addr ) ) < 0 )
237 {
238 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000239 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 }
241
242 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100243#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000244}
245
246/*
247 * Create a listening socket on bind_ip:port
248 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000249int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000250{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100251#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100252 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100253 struct addrinfo hints, *addr_list, *cur;
254 char port_str[6];
255
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100256 if( ( ret = net_prepare() ) != 0 )
257 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100258
259 /* getaddrinfo expects port as a string */
260 memset( port_str, 0, sizeof( port_str ) );
261 snprintf( port_str, sizeof( port_str ), "%d", port );
262
263 /* Bind to IPv6 and/or IPv4, but only in TCP */
264 memset( &hints, 0, sizeof( hints ) );
265 hints.ai_family = AF_UNSPEC;
266 hints.ai_socktype = SOCK_STREAM;
267 hints.ai_protocol = IPPROTO_TCP;
268 if( bind_ip == NULL )
269 hints.ai_flags = AI_PASSIVE;
270
271 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
272 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
273
274 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100275 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100276 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
277 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100278 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
279 cur->ai_protocol );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100280 if( *fd < 0 )
281 {
282 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
283 continue;
284 }
285
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100286 n = 1;
Paul Bakker874bd642014-04-17 12:43:05 +0200287 if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
288 (const char *) &n, sizeof( n ) ) != 0 )
289 {
290 close( *fd );
291 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
292 continue;
293 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100294
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100295 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
296 {
297 close( *fd );
298 ret = POLARSSL_ERR_NET_BIND_FAILED;
299 continue;
300 }
301
302 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
303 {
304 close( *fd );
305 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
306 continue;
307 }
308
309 /* I we ever get there, it's a success */
310 ret = 0;
311 break;
312 }
313
314 freeaddrinfo( addr_list );
315
316 return( ret );
317
318#else
319 /* Legacy IPv4-only version */
320
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100321 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 struct sockaddr_in server_addr;
323
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100324 if( ( ret = net_prepare() ) != 0 )
325 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
Paul Bakkerbbc10072013-10-14 16:33:24 +0200327 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000328 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
330 n = 1;
331 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
332 (const char *) &n, sizeof( n ) );
333
Paul Bakker37286a52013-03-06 16:55:11 +0100334 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 server_addr.sin_family = AF_INET;
336 server_addr.sin_port = net_htons( port );
337
338 if( bind_ip != NULL )
339 {
340 memset( c, 0, sizeof( c ) );
341 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
342
343 for( n = 0; n < 4; n++ )
344 if( c[n] < 0 || c[n] > 255 )
345 break;
346
347 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100348 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000349 ( (uint32_t) c[0] << 24 ) |
350 ( (uint32_t) c[1] << 16 ) |
351 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100352 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 }
354
355 if( bind( *fd, (struct sockaddr *) &server_addr,
356 sizeof( server_addr ) ) < 0 )
357 {
358 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000359 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 }
361
Paul Bakker192381a2011-05-20 12:31:31 +0000362 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 {
364 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000365 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366 }
367
368 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100369#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000370}
371
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100372#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
373 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100374/*
375 * Check if the requested operation would be blocking on a non-blocking socket
376 * and thus 'failed' with a negative return value.
377 */
378static int net_would_block( int fd )
379{
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +0100380 ((void) fd);
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100382}
Paul Bakker5121ce52009-01-03 21:22:43 +0000383#else
Paul Bakker80025412014-01-23 20:59:49 +0100384/*
385 * Check if the requested operation would be blocking on a non-blocking socket
386 * and thus 'failed' with a negative return value.
387 *
388 * Note: on a blocking socket this function always returns 0!
389 */
390static int net_would_block( int fd )
391{
392 /*
393 * Never return 'WOULD BLOCK' on a non-blocking socket
394 */
395 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
396 return( 0 );
397
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 switch( errno )
399 {
400#if defined EAGAIN
401 case EAGAIN:
402#endif
403#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
404 case EWOULDBLOCK:
405#endif
406 return( 1 );
407 }
408 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000409}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200410#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
412/*
413 * Accept a connection from a remote client
414 */
415int net_accept( int bind_fd, int *client_fd, void *client_ip )
416{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100417#if defined(POLARSSL_HAVE_IPV6)
418 struct sockaddr_storage client_addr;
419#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100421#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
Paul Bakker394c56f2011-12-20 12:19:03 +0000423#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
424 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 socklen_t n = (socklen_t) sizeof( client_addr );
426#else
427 int n = (int) sizeof( client_addr );
428#endif
429
Paul Bakkerbbc10072013-10-14 16:33:24 +0200430 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
431 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000432
433 if( *client_fd < 0 )
434 {
Manuel Pégourié-Gonnard9a6b4422014-07-21 13:42:54 +0200435 if( net_would_block( bind_fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000436 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
Paul Bakker40e46942009-01-03 21:51:57 +0000438 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 }
440
441 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100442 {
443#if defined(POLARSSL_HAVE_IPV6)
444 if( client_addr.ss_family == AF_INET )
445 {
446 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
447 memcpy( client_ip, &addr4->sin_addr.s_addr,
448 sizeof( addr4->sin_addr.s_addr ) );
449 }
450 else
451 {
452 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
453 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
454 sizeof( addr6->sin6_addr.s6_addr ) );
455 }
456#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 memcpy( client_ip, &client_addr.sin_addr.s_addr,
458 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100459#endif /* POLARSSL_HAVE_IPV6 */
460 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000461
462 return( 0 );
463}
464
465/*
466 * Set the socket blocking or non-blocking
467 */
468int net_set_block( int fd )
469{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100470#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
471 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000472 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000473 return( ioctlsocket( fd, FIONBIO, &n ) );
474#else
475 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
476#endif
477}
478
479int net_set_nonblock( int fd )
480{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100481#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
482 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000483 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 return( ioctlsocket( fd, FIONBIO, &n ) );
485#else
486 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
487#endif
488}
489
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200490#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000491/*
492 * Portable usleep helper
493 */
494void net_usleep( unsigned long usec )
495{
496 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100497 tv.tv_sec = usec / 1000000;
Paul Bakker82788fb2014-10-20 13:59:19 +0200498#if !defined(_WIN32) && ( defined(__unix__) || defined(__unix) || \
499 ( defined(__APPLE__) && defined(__MACH__) ) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100500 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200501#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100502 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200503#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 select( 0, NULL, NULL, NULL, &tv );
505}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200506#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
508/*
509 * Read at most 'len' characters
510 */
Paul Bakker23986e52011-04-24 08:57:21 +0000511int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100512{
Paul Bakker80025412014-01-23 20:59:49 +0100513 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200514 int ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 if( ret < 0 )
517 {
Paul Bakker80025412014-01-23 20:59:49 +0100518 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000519 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100521#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
522 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000524 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525#else
526 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000527 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000530 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000531#endif
532
Paul Bakker40e46942009-01-03 21:51:57 +0000533 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 }
535
536 return( ret );
537}
538
539/*
540 * Write at most 'len' characters
541 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000542int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000543{
Paul Bakker80025412014-01-23 20:59:49 +0100544 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200545 int ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547 if( ret < 0 )
548 {
Paul Bakker80025412014-01-23 20:59:49 +0100549 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000550 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000551
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100552#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
553 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000555 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000556#else
557 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000558 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000561 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562#endif
563
Paul Bakker40e46942009-01-03 21:51:57 +0000564 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 }
566
567 return( ret );
568}
569
570/*
571 * Gracefully close the connection
572 */
573void net_close( int fd )
574{
575 shutdown( fd, 2 );
576 close( fd );
577}
578
Paul Bakker9af723c2014-05-01 13:03:14 +0200579#endif /* POLARSSL_NET_C */