blob: 3f0e448ba93cbafb31fbdf3907f0426e2b453bb3 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * TCP networking functions
3 *
Paul Bakker9af723c2014-05-01 13:03:14 +02004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (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 Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakkerfa6a6202013-10-28 18:48:30 +010036#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
37 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010039#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010040#ifdef _WIN32_WINNT
41#undef _WIN32_WINNT
42#endif
43/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010044#define _WIN32_WINNT 0x0501
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010045#include <ws2tcpip.h>
46#endif
47
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010048#include <winsock2.h>
49#include <windows.h>
50
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010051#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000052#if defined(_WIN32_WCE)
53#pragma comment( lib, "ws2.lib" )
54#else
55#pragma comment( lib, "ws2_32.lib" )
56#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010057#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakkerf4f69682011-04-24 16:08:12 +000059#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
60#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000061#define close(fd) closesocket(fd)
62
63static int wsa_init_done = 0;
64
Paul Bakkerdb20c102014-06-17 14:34:44 +020065#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000066
67#include <sys/types.h>
68#include <sys/socket.h>
69#include <netinet/in.h>
70#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020071#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000072#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020073#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000074#include <unistd.h>
75#include <signal.h>
76#include <fcntl.h>
77#include <netdb.h>
78#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000079
Paul Bakker6a2f8572012-08-23 07:45:37 +000080#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
Markus Pfeiffera26a0052014-04-22 20:16:15 +000081 defined(__DragonFly__)
Paul Bakker854963c2009-07-19 20:50:11 +000082#include <sys/endian.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010083#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
84 defined(EFIX64) || defined(EFI32)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000085#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000086#elif defined(sun)
87#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020088#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
89#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000090#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000091#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000092#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Paul Bakkerdb20c102014-06-17 14:34:44 +020094#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000095
Paul Bakker5121ce52009-01-03 21:22:43 +000096#include <stdlib.h>
97#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020098
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +010099#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
100 !defined(EFI32)
101#define snprintf _snprintf
102#endif
103
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200104#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000105#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200106#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100108#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000109#include <basetsd.h>
110typedef UINT32 uint32_t;
111#else
112#include <inttypes.h>
113#endif
114
Paul Bakker5121ce52009-01-03 21:22:43 +0000115/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000116 * htons() is not always available.
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200117 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and
118 * __BIG_ENDIAN to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200120#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
121 __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000122#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100123#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000124#else
Paul Bakker37286a52013-03-06 16:55:11 +0100125#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
126 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
127#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
128 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
129 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
130 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000131#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Paul Bakker66d5d072014-06-17 16:39:18 +0200133unsigned short net_htons( unsigned short n );
134unsigned long net_htonl( unsigned long n );
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000135#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100136#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100139 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100141static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000142{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100143#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
144 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 WSADATA wsaData;
146
147 if( wsa_init_done == 0 )
148 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200149 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000150 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 wsa_init_done = 1;
153 }
154#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100155#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 signal( SIGPIPE, SIG_IGN );
157#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100158#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100159 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100160}
161
162/*
163 * Initiate a TCP connection with host:port
164 */
165int net_connect( int *fd, const char *host, int port )
166{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100167#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100168 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100169 struct addrinfo hints, *addr_list, *cur;
170 char port_str[6];
171
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100172 if( ( ret = net_prepare() ) != 0 )
173 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100174
175 /* getaddrinfo expects port as a string */
176 memset( port_str, 0, sizeof( port_str ) );
177 snprintf( port_str, sizeof( port_str ), "%d", port );
178
179 /* Do name resolution with both IPv6 and IPv4, but only TCP */
180 memset( &hints, 0, sizeof( hints ) );
181 hints.ai_family = AF_UNSPEC;
182 hints.ai_socktype = SOCK_STREAM;
183 hints.ai_protocol = IPPROTO_TCP;
184
185 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
186 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
187
188 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100189 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100190 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
191 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100192 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
193 cur->ai_protocol );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100194 if( *fd < 0 )
195 {
196 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
197 continue;
198 }
199
200 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
201 {
202 ret = 0;
203 break;
204 }
205
206 close( *fd );
207 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
208 }
209
210 freeaddrinfo( addr_list );
211
212 return( ret );
213
214#else
215 /* Legacy IPv4-only version */
216
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100217 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100218 struct sockaddr_in server_addr;
219 struct hostent *server_host;
220
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100221 if( ( ret = net_prepare() ) != 0 )
222 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
224 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000225 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
Paul Bakkerbbc10072013-10-14 16:33:24 +0200227 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000228 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
230 memcpy( (void *) &server_addr.sin_addr,
231 (void *) server_host->h_addr,
232 server_host->h_length );
233
234 server_addr.sin_family = AF_INET;
235 server_addr.sin_port = net_htons( port );
236
237 if( connect( *fd, (struct sockaddr *) &server_addr,
238 sizeof( server_addr ) ) < 0 )
239 {
240 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000241 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 }
243
244 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100245#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000246}
247
248/*
249 * Create a listening socket on bind_ip:port
250 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000251int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000252{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100253#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100254 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100255 struct addrinfo hints, *addr_list, *cur;
256 char port_str[6];
257
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100258 if( ( ret = net_prepare() ) != 0 )
259 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100260
261 /* getaddrinfo expects port as a string */
262 memset( port_str, 0, sizeof( port_str ) );
263 snprintf( port_str, sizeof( port_str ), "%d", port );
264
265 /* Bind to IPv6 and/or IPv4, but only in TCP */
266 memset( &hints, 0, sizeof( hints ) );
267 hints.ai_family = AF_UNSPEC;
268 hints.ai_socktype = SOCK_STREAM;
269 hints.ai_protocol = IPPROTO_TCP;
270 if( bind_ip == NULL )
271 hints.ai_flags = AI_PASSIVE;
272
273 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
274 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
275
276 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100277 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100278 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
279 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100280 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
281 cur->ai_protocol );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100282 if( *fd < 0 )
283 {
284 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
285 continue;
286 }
287
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100288 n = 1;
Paul Bakker874bd642014-04-17 12:43:05 +0200289 if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
290 (const char *) &n, sizeof( n ) ) != 0 )
291 {
292 close( *fd );
293 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
294 continue;
295 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100296
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100297 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
298 {
299 close( *fd );
300 ret = POLARSSL_ERR_NET_BIND_FAILED;
301 continue;
302 }
303
304 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
305 {
306 close( *fd );
307 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
308 continue;
309 }
310
311 /* I we ever get there, it's a success */
312 ret = 0;
313 break;
314 }
315
316 freeaddrinfo( addr_list );
317
318 return( ret );
319
320#else
321 /* Legacy IPv4-only version */
322
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100323 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 struct sockaddr_in server_addr;
325
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100326 if( ( ret = net_prepare() ) != 0 )
327 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
Paul Bakkerbbc10072013-10-14 16:33:24 +0200329 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000330 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
332 n = 1;
333 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
334 (const char *) &n, sizeof( n ) );
335
Paul Bakker37286a52013-03-06 16:55:11 +0100336 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 server_addr.sin_family = AF_INET;
338 server_addr.sin_port = net_htons( port );
339
340 if( bind_ip != NULL )
341 {
342 memset( c, 0, sizeof( c ) );
343 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
344
345 for( n = 0; n < 4; n++ )
346 if( c[n] < 0 || c[n] > 255 )
347 break;
348
349 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100350 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000351 ( (uint32_t) c[0] << 24 ) |
352 ( (uint32_t) c[1] << 16 ) |
353 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100354 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 }
356
357 if( bind( *fd, (struct sockaddr *) &server_addr,
358 sizeof( server_addr ) ) < 0 )
359 {
360 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000361 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 }
363
Paul Bakker192381a2011-05-20 12:31:31 +0000364 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 {
366 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000367 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 }
369
370 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100371#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000372}
373
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100374#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
375 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100376/*
377 * Check if the requested operation would be blocking on a non-blocking socket
378 * and thus 'failed' with a negative return value.
379 */
380static int net_would_block( int fd )
381{
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +0100382 ((void) fd);
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100384}
Paul Bakker5121ce52009-01-03 21:22:43 +0000385#else
Paul Bakker80025412014-01-23 20:59:49 +0100386/*
387 * Check if the requested operation would be blocking on a non-blocking socket
388 * and thus 'failed' with a negative return value.
389 *
390 * Note: on a blocking socket this function always returns 0!
391 */
392static int net_would_block( int fd )
393{
394 /*
395 * Never return 'WOULD BLOCK' on a non-blocking socket
396 */
397 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
398 return( 0 );
399
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 switch( errno )
401 {
402#if defined EAGAIN
403 case EAGAIN:
404#endif
405#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
406 case EWOULDBLOCK:
407#endif
408 return( 1 );
409 }
410 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200412#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
414/*
415 * Accept a connection from a remote client
416 */
417int net_accept( int bind_fd, int *client_fd, void *client_ip )
418{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100419#if defined(POLARSSL_HAVE_IPV6)
420 struct sockaddr_storage client_addr;
421#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100423#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
Paul Bakker394c56f2011-12-20 12:19:03 +0000425#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
426 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 socklen_t n = (socklen_t) sizeof( client_addr );
428#else
429 int n = (int) sizeof( client_addr );
430#endif
431
Paul Bakkerbbc10072013-10-14 16:33:24 +0200432 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
433 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
435 if( *client_fd < 0 )
436 {
Manuel Pégourié-Gonnard9a6b4422014-07-21 13:42:54 +0200437 if( net_would_block( bind_fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000438 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
Paul Bakker40e46942009-01-03 21:51:57 +0000440 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 }
442
443 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100444 {
445#if defined(POLARSSL_HAVE_IPV6)
446 if( client_addr.ss_family == AF_INET )
447 {
448 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
449 memcpy( client_ip, &addr4->sin_addr.s_addr,
450 sizeof( addr4->sin_addr.s_addr ) );
451 }
452 else
453 {
454 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
455 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
456 sizeof( addr6->sin6_addr.s6_addr ) );
457 }
458#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 memcpy( client_ip, &client_addr.sin_addr.s_addr,
460 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100461#endif /* POLARSSL_HAVE_IPV6 */
462 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
464 return( 0 );
465}
466
467/*
468 * Set the socket blocking or non-blocking
469 */
470int net_set_block( int fd )
471{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100472#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
473 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000474 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 return( ioctlsocket( fd, FIONBIO, &n ) );
476#else
477 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
478#endif
479}
480
481int net_set_nonblock( int fd )
482{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100483#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
484 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000485 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 return( ioctlsocket( fd, FIONBIO, &n ) );
487#else
488 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
489#endif
490}
491
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200492#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000493/*
494 * Portable usleep helper
495 */
496void net_usleep( unsigned long usec )
497{
498 struct timeval tv;
499 tv.tv_sec = 0;
Paul Bakker82788fb2014-10-20 13:59:19 +0200500#if !defined(_WIN32) && ( defined(__unix__) || defined(__unix) || \
501 ( defined(__APPLE__) && defined(__MACH__) ) )
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200502 tv.tv_usec = (suseconds_t) usec;
503#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 tv.tv_usec = usec;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200505#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 select( 0, NULL, NULL, NULL, &tv );
507}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200508#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510/*
511 * Read at most 'len' characters
512 */
Paul Bakker23986e52011-04-24 08:57:21 +0000513int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100514{
Paul Bakker80025412014-01-23 20:59:49 +0100515 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200516 int ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 if( ret < 0 )
519 {
Paul Bakker80025412014-01-23 20:59:49 +0100520 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000521 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100523#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
524 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000525 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000526 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527#else
528 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000529 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
531 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000532 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000533#endif
534
Paul Bakker40e46942009-01-03 21:51:57 +0000535 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 }
537
538 return( ret );
539}
540
541/*
542 * Write at most 'len' characters
543 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000544int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000545{
Paul Bakker80025412014-01-23 20:59:49 +0100546 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200547 int ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 if( ret < 0 )
550 {
Paul Bakker80025412014-01-23 20:59:49 +0100551 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000552 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100554#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
555 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000557 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558#else
559 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000560 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
562 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000563 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564#endif
565
Paul Bakker40e46942009-01-03 21:51:57 +0000566 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 }
568
569 return( ret );
570}
571
572/*
573 * Gracefully close the connection
574 */
575void net_close( int fd )
576{
577 shutdown( fd, 2 );
578 close( fd );
579}
580
Paul Bakker9af723c2014-05-01 13:03:14 +0200581#endif /* POLARSSL_NET_C */