blob: c7ce2584e34d1d9b06107dd807ae9fbbae5109cc [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakker40e46942009-01-03 21:51:57 +000029#if defined(POLARSSL_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
36 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010038#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010039#ifdef _WIN32_WINNT
40#undef _WIN32_WINNT
41#endif
42/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010043#define _WIN32_WINNT 0x0501
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010044#include <ws2tcpip.h>
45#endif
46
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010047#include <winsock2.h>
48#include <windows.h>
49
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010050#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000051#if defined(_WIN32_WCE)
52#pragma comment( lib, "ws2.lib" )
53#else
54#pragma comment( lib, "ws2_32.lib" )
55#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010056#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Paul Bakkerf4f69682011-04-24 16:08:12 +000058#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
59#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000060#define close(fd) closesocket(fd)
61
62static int wsa_init_done = 0;
63
Paul Bakkerdb20c102014-06-17 14:34:44 +020064#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66#include <sys/types.h>
67#include <sys/socket.h>
68#include <netinet/in.h>
69#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020070#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000071#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020072#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000073#include <unistd.h>
74#include <signal.h>
75#include <fcntl.h>
76#include <netdb.h>
77#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000078
Paul Bakker6a2f8572012-08-23 07:45:37 +000079#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
Markus Pfeiffera26a0052014-04-22 20:16:15 +000080 defined(__DragonFly__)
Paul Bakker854963c2009-07-19 20:50:11 +000081#include <sys/endian.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010082#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
83 defined(EFIX64) || defined(EFI32)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000084#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000085#elif defined(sun)
86#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020087#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
88#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000089#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000090#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000091#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Paul Bakkerdb20c102014-06-17 14:34:44 +020093#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Paul Bakker5121ce52009-01-03 21:22:43 +000095#include <stdlib.h>
96#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020097
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +010098#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
99 !defined(EFI32)
100#define snprintf _snprintf
101#endif
102
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200103#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000104#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200105#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100107#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000108#include <basetsd.h>
109typedef UINT32 uint32_t;
110#else
111#include <inttypes.h>
112#endif
113
Paul Bakker5121ce52009-01-03 21:22:43 +0000114/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000115 * htons() is not always available.
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200116 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and
117 * __BIG_ENDIAN to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200119#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
120 __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000121#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100122#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000123#else
Paul Bakker37286a52013-03-06 16:55:11 +0100124#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
125 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
126#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
127 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
128 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
129 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000130#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Rich Evansa18b11f2015-01-30 10:58:35 +0000132#if defined(POLARSSL_PLATFORM_C)
133#include "polarssl/platform.h"
134#else
135#define polarssl_snprintf snprintf
136#endif
137
Paul Bakker66d5d072014-06-17 16:39:18 +0200138unsigned short net_htons( unsigned short n );
139unsigned long net_htonl( unsigned long n );
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000140#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100141#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100144 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100146static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000147{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100148#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
149 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 WSADATA wsaData;
151
152 if( wsa_init_done == 0 )
153 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200154 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000155 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 wsa_init_done = 1;
158 }
159#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100160#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 signal( SIGPIPE, SIG_IGN );
162#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100163#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100164 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100165}
166
167/*
168 * Initiate a TCP connection with host:port
169 */
170int net_connect( int *fd, const char *host, int port )
171{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100172#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100173 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100174 struct addrinfo hints, *addr_list, *cur;
175 char port_str[6];
176
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100177 if( ( ret = net_prepare() ) != 0 )
178 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100179
180 /* getaddrinfo expects port as a string */
181 memset( port_str, 0, sizeof( port_str ) );
Rich Evansa18b11f2015-01-30 10:58:35 +0000182 polarssl_snprintf( port_str, sizeof( port_str ), "%d", port );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100183
184 /* Do name resolution with both IPv6 and IPv4, but only TCP */
185 memset( &hints, 0, sizeof( hints ) );
186 hints.ai_family = AF_UNSPEC;
187 hints.ai_socktype = SOCK_STREAM;
188 hints.ai_protocol = IPPROTO_TCP;
189
190 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
191 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
192
193 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100194 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100195 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
196 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100197 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
198 cur->ai_protocol );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100199 if( *fd < 0 )
200 {
201 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
202 continue;
203 }
204
205 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
206 {
207 ret = 0;
208 break;
209 }
210
211 close( *fd );
212 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
213 }
214
215 freeaddrinfo( addr_list );
216
217 return( ret );
218
219#else
220 /* Legacy IPv4-only version */
221
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100222 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100223 struct sockaddr_in server_addr;
224 struct hostent *server_host;
225
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100226 if( ( ret = net_prepare() ) != 0 )
227 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
229 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000230 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Paul Bakkerbbc10072013-10-14 16:33:24 +0200232 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000233 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
235 memcpy( (void *) &server_addr.sin_addr,
236 (void *) server_host->h_addr,
237 server_host->h_length );
238
239 server_addr.sin_family = AF_INET;
240 server_addr.sin_port = net_htons( port );
241
242 if( connect( *fd, (struct sockaddr *) &server_addr,
243 sizeof( server_addr ) ) < 0 )
244 {
245 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000246 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 }
248
249 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100250#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000251}
252
253/*
254 * Create a listening socket on bind_ip:port
255 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000256int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000257{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100258#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100259 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100260 struct addrinfo hints, *addr_list, *cur;
261 char port_str[6];
262
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100263 if( ( ret = net_prepare() ) != 0 )
264 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100265
266 /* getaddrinfo expects port as a string */
267 memset( port_str, 0, sizeof( port_str ) );
Rich Evansa18b11f2015-01-30 10:58:35 +0000268 polarssl_snprintf( port_str, sizeof( port_str ), "%d", port );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100269
270 /* Bind to IPv6 and/or IPv4, but only in TCP */
271 memset( &hints, 0, sizeof( hints ) );
272 hints.ai_family = AF_UNSPEC;
273 hints.ai_socktype = SOCK_STREAM;
274 hints.ai_protocol = IPPROTO_TCP;
275 if( bind_ip == NULL )
276 hints.ai_flags = AI_PASSIVE;
277
278 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
279 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
280
281 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100282 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100283 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
284 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100285 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
286 cur->ai_protocol );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100287 if( *fd < 0 )
288 {
289 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
290 continue;
291 }
292
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100293 n = 1;
Paul Bakker874bd642014-04-17 12:43:05 +0200294 if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
295 (const char *) &n, sizeof( n ) ) != 0 )
296 {
297 close( *fd );
298 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
299 continue;
300 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100301
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100302 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
303 {
304 close( *fd );
305 ret = POLARSSL_ERR_NET_BIND_FAILED;
306 continue;
307 }
308
309 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
310 {
311 close( *fd );
312 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
313 continue;
314 }
315
316 /* I we ever get there, it's a success */
317 ret = 0;
318 break;
319 }
320
321 freeaddrinfo( addr_list );
322
323 return( ret );
324
325#else
326 /* Legacy IPv4-only version */
327
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100328 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 struct sockaddr_in server_addr;
330
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100331 if( ( ret = net_prepare() ) != 0 )
332 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000333
Paul Bakkerbbc10072013-10-14 16:33:24 +0200334 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000335 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337 n = 1;
338 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
339 (const char *) &n, sizeof( n ) );
340
Paul Bakker37286a52013-03-06 16:55:11 +0100341 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 server_addr.sin_family = AF_INET;
343 server_addr.sin_port = net_htons( port );
344
345 if( bind_ip != NULL )
346 {
347 memset( c, 0, sizeof( c ) );
348 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
349
350 for( n = 0; n < 4; n++ )
351 if( c[n] < 0 || c[n] > 255 )
352 break;
353
354 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100355 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000356 ( (uint32_t) c[0] << 24 ) |
357 ( (uint32_t) c[1] << 16 ) |
358 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100359 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 }
361
362 if( bind( *fd, (struct sockaddr *) &server_addr,
363 sizeof( server_addr ) ) < 0 )
364 {
365 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000366 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 }
368
Paul Bakker192381a2011-05-20 12:31:31 +0000369 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 {
371 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000372 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 }
374
375 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100376#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000377}
378
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100379#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
380 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100381/*
382 * Check if the requested operation would be blocking on a non-blocking socket
383 * and thus 'failed' with a negative return value.
384 */
385static int net_would_block( int fd )
386{
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +0100387 ((void) fd);
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100389}
Paul Bakker5121ce52009-01-03 21:22:43 +0000390#else
Paul Bakker80025412014-01-23 20:59:49 +0100391/*
392 * Check if the requested operation would be blocking on a non-blocking socket
393 * and thus 'failed' with a negative return value.
394 *
395 * Note: on a blocking socket this function always returns 0!
396 */
397static int net_would_block( int fd )
398{
399 /*
400 * Never return 'WOULD BLOCK' on a non-blocking socket
401 */
402 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
403 return( 0 );
404
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 switch( errno )
406 {
407#if defined EAGAIN
408 case EAGAIN:
409#endif
410#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
411 case EWOULDBLOCK:
412#endif
413 return( 1 );
414 }
415 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200417#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
419/*
420 * Accept a connection from a remote client
421 */
422int net_accept( int bind_fd, int *client_fd, void *client_ip )
423{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100424#if defined(POLARSSL_HAVE_IPV6)
425 struct sockaddr_storage client_addr;
426#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100428#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
Paul Bakker394c56f2011-12-20 12:19:03 +0000430#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
Manuel Pégourié-Gonnard61462472015-10-05 12:16:06 +0100431 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t)
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 socklen_t n = (socklen_t) sizeof( client_addr );
433#else
434 int n = (int) sizeof( client_addr );
435#endif
436
Paul Bakkerbbc10072013-10-14 16:33:24 +0200437 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
438 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440 if( *client_fd < 0 )
441 {
Manuel Pégourié-Gonnard9a6b4422014-07-21 13:42:54 +0200442 if( net_would_block( bind_fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000443 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
Paul Bakker40e46942009-01-03 21:51:57 +0000445 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 }
447
448 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100449 {
450#if defined(POLARSSL_HAVE_IPV6)
451 if( client_addr.ss_family == AF_INET )
452 {
453 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
454 memcpy( client_ip, &addr4->sin_addr.s_addr,
455 sizeof( addr4->sin_addr.s_addr ) );
456 }
457 else
458 {
459 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
460 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
461 sizeof( addr6->sin6_addr.s6_addr ) );
462 }
463#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 memcpy( client_ip, &client_addr.sin_addr.s_addr,
465 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100466#endif /* POLARSSL_HAVE_IPV6 */
467 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
469 return( 0 );
470}
471
472/*
473 * Set the socket blocking or non-blocking
474 */
475int net_set_block( int fd )
476{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100477#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
478 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000479 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 return( ioctlsocket( fd, FIONBIO, &n ) );
481#else
482 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
483#endif
484}
485
486int net_set_nonblock( int fd )
487{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100488#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
489 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000490 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 return( ioctlsocket( fd, FIONBIO, &n ) );
492#else
493 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
494#endif
495}
496
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200497#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000498/*
499 * Portable usleep helper
500 */
501void net_usleep( unsigned long usec )
502{
Simon Butchera91d85e2016-01-15 14:36:08 +0000503#if defined(_WIN32)
504 Sleep( ( usec + 999 ) / 1000 );
505#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100507 tv.tv_sec = usec / 1000000;
Simon Butchera91d85e2016-01-15 14:36:08 +0000508#if defined(__unix__) || defined(__unix) || \
509 ( defined(__APPLE__) && defined(__MACH__) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100510 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200511#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100512 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200513#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 select( 0, NULL, NULL, NULL, &tv );
Simon Butchera91d85e2016-01-15 14:36:08 +0000515#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000516}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200517#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
519/*
520 * Read at most 'len' characters
521 */
Paul Bakker23986e52011-04-24 08:57:21 +0000522int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100523{
Paul Bakker80025412014-01-23 20:59:49 +0100524 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200525 int ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 if( ret < 0 )
528 {
Paul Bakker80025412014-01-23 20:59:49 +0100529 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000530 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100532#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
533 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000535 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000536#else
537 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000538 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
540 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000541 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542#endif
543
Paul Bakker40e46942009-01-03 21:51:57 +0000544 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 }
546
547 return( ret );
548}
549
550/*
551 * Write at most 'len' characters
552 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000553int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000554{
Paul Bakker80025412014-01-23 20:59:49 +0100555 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200556 int ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558 if( ret < 0 )
559 {
Paul Bakker80025412014-01-23 20:59:49 +0100560 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000561 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100563#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
564 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000566 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567#else
568 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000569 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
571 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000572 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573#endif
574
Paul Bakker40e46942009-01-03 21:51:57 +0000575 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 }
577
578 return( ret );
579}
580
581/*
582 * Gracefully close the connection
583 */
584void net_close( int fd )
585{
586 shutdown( fd, 2 );
587 close( fd );
588}
589
Paul Bakker9af723c2014-05-01 13:03:14 +0200590#endif /* POLARSSL_NET_C */