blob: 5360c07425c06e5bb3353b6b0fa6eac5cf99e230 [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
Andres Amaya Garcia6e5e9aa2017-07-06 14:09:24 +010095/* Some MS functions want int and MSVC warns if we pass size_t,
Andres Amaya Garciaa4d18572017-07-06 16:27:00 +010096 * but the standard functions use socklen_t, so cast only for MSVC */
Andres Amaya Garcia6e5e9aa2017-07-06 14:09:24 +010097#if defined(_MSC_VER)
98#define MSVC_INT_CAST (int)
99#else
100#define MSVC_INT_CAST
101#endif
102
Paul Bakker5121ce52009-01-03 21:22:43 +0000103#include <stdlib.h>
104#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200105
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100106#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
107 !defined(EFI32)
108#define snprintf _snprintf
109#endif
110
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200111#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000112#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200113#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100115#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000116#include <basetsd.h>
117typedef UINT32 uint32_t;
118#else
119#include <inttypes.h>
120#endif
121
Paul Bakker5121ce52009-01-03 21:22:43 +0000122/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000123 * htons() is not always available.
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200124 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and
125 * __BIG_ENDIAN to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200127#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
128 __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000129#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100130#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000131#else
Paul Bakker37286a52013-03-06 16:55:11 +0100132#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
133 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
134#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
135 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
136 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
137 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000138#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
Rich Evansa18b11f2015-01-30 10:58:35 +0000140#if defined(POLARSSL_PLATFORM_C)
141#include "polarssl/platform.h"
142#else
143#define polarssl_snprintf snprintf
144#endif
145
Paul Bakker66d5d072014-06-17 16:39:18 +0200146unsigned short net_htons( unsigned short n );
147unsigned long net_htonl( unsigned long n );
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000148#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100149#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100152 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100154static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000155{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100156#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
157 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 WSADATA wsaData;
159
160 if( wsa_init_done == 0 )
161 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200162 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000163 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
165 wsa_init_done = 1;
166 }
167#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100168#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 signal( SIGPIPE, SIG_IGN );
170#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100171#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100172 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100173}
174
175/*
176 * Initiate a TCP connection with host:port
177 */
178int net_connect( int *fd, const char *host, int port )
179{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100180#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100181 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100182 struct addrinfo hints, *addr_list, *cur;
183 char port_str[6];
184
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100185 if( ( ret = net_prepare() ) != 0 )
186 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100187
188 /* getaddrinfo expects port as a string */
189 memset( port_str, 0, sizeof( port_str ) );
Rich Evansa18b11f2015-01-30 10:58:35 +0000190 polarssl_snprintf( port_str, sizeof( port_str ), "%d", port );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100191
192 /* Do name resolution with both IPv6 and IPv4, but only TCP */
193 memset( &hints, 0, sizeof( hints ) );
194 hints.ai_family = AF_UNSPEC;
195 hints.ai_socktype = SOCK_STREAM;
196 hints.ai_protocol = IPPROTO_TCP;
197
198 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
199 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
200
201 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100202 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100203 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
204 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100205 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
206 cur->ai_protocol );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100207 if( *fd < 0 )
208 {
209 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
210 continue;
211 }
212
Andres Amaya Garcia6e5e9aa2017-07-06 14:09:24 +0100213 if( connect( *fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100214 {
215 ret = 0;
216 break;
217 }
218
219 close( *fd );
220 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
221 }
222
223 freeaddrinfo( addr_list );
224
225 return( ret );
226
227#else
228 /* Legacy IPv4-only version */
229
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100230 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100231 struct sockaddr_in server_addr;
232 struct hostent *server_host;
233
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100234 if( ( ret = net_prepare() ) != 0 )
235 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
237 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000238 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
Paul Bakkerbbc10072013-10-14 16:33:24 +0200240 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000241 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
243 memcpy( (void *) &server_addr.sin_addr,
244 (void *) server_host->h_addr,
245 server_host->h_length );
246
247 server_addr.sin_family = AF_INET;
248 server_addr.sin_port = net_htons( port );
249
250 if( connect( *fd, (struct sockaddr *) &server_addr,
251 sizeof( server_addr ) ) < 0 )
252 {
253 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000254 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 }
256
257 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100258#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000259}
260
261/*
262 * Create a listening socket on bind_ip:port
263 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000264int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000265{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100266#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100267 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100268 struct addrinfo hints, *addr_list, *cur;
269 char port_str[6];
270
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100271 if( ( ret = net_prepare() ) != 0 )
272 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100273
274 /* getaddrinfo expects port as a string */
275 memset( port_str, 0, sizeof( port_str ) );
Rich Evansa18b11f2015-01-30 10:58:35 +0000276 polarssl_snprintf( port_str, sizeof( port_str ), "%d", port );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100277
278 /* Bind to IPv6 and/or IPv4, but only in TCP */
279 memset( &hints, 0, sizeof( hints ) );
280 hints.ai_family = AF_UNSPEC;
281 hints.ai_socktype = SOCK_STREAM;
282 hints.ai_protocol = IPPROTO_TCP;
283 if( bind_ip == NULL )
284 hints.ai_flags = AI_PASSIVE;
285
286 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
287 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
288
289 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100290 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100291 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
292 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100293 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
294 cur->ai_protocol );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100295 if( *fd < 0 )
296 {
297 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
298 continue;
299 }
300
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100301 n = 1;
Paul Bakker874bd642014-04-17 12:43:05 +0200302 if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
303 (const char *) &n, sizeof( n ) ) != 0 )
304 {
305 close( *fd );
306 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
307 continue;
308 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100309
Andres Amaya Garcia6e5e9aa2017-07-06 14:09:24 +0100310 if( bind( *fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100311 {
312 close( *fd );
313 ret = POLARSSL_ERR_NET_BIND_FAILED;
314 continue;
315 }
316
317 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
318 {
319 close( *fd );
320 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
321 continue;
322 }
323
324 /* I we ever get there, it's a success */
325 ret = 0;
326 break;
327 }
328
329 freeaddrinfo( addr_list );
330
331 return( ret );
332
333#else
334 /* Legacy IPv4-only version */
335
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100336 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 struct sockaddr_in server_addr;
338
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100339 if( ( ret = net_prepare() ) != 0 )
340 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
Paul Bakkerbbc10072013-10-14 16:33:24 +0200342 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000343 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
345 n = 1;
346 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
347 (const char *) &n, sizeof( n ) );
348
Paul Bakker37286a52013-03-06 16:55:11 +0100349 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 server_addr.sin_family = AF_INET;
351 server_addr.sin_port = net_htons( port );
352
353 if( bind_ip != NULL )
354 {
355 memset( c, 0, sizeof( c ) );
356 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
357
358 for( n = 0; n < 4; n++ )
359 if( c[n] < 0 || c[n] > 255 )
360 break;
361
362 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100363 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000364 ( (uint32_t) c[0] << 24 ) |
365 ( (uint32_t) c[1] << 16 ) |
366 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100367 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 }
369
370 if( bind( *fd, (struct sockaddr *) &server_addr,
371 sizeof( server_addr ) ) < 0 )
372 {
373 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000374 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 }
376
Paul Bakker192381a2011-05-20 12:31:31 +0000377 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 {
379 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000380 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 }
382
383 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100384#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000385}
386
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100387#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
388 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100389/*
390 * Check if the requested operation would be blocking on a non-blocking socket
391 * and thus 'failed' with a negative return value.
392 */
393static int net_would_block( int fd )
394{
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +0100395 ((void) fd);
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100397}
Paul Bakker5121ce52009-01-03 21:22:43 +0000398#else
Paul Bakker80025412014-01-23 20:59:49 +0100399/*
400 * Check if the requested operation would be blocking on a non-blocking socket
401 * and thus 'failed' with a negative return value.
402 *
403 * Note: on a blocking socket this function always returns 0!
404 */
405static int net_would_block( int fd )
406{
407 /*
408 * Never return 'WOULD BLOCK' on a non-blocking socket
409 */
410 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
411 return( 0 );
412
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 switch( errno )
414 {
415#if defined EAGAIN
416 case EAGAIN:
417#endif
418#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
419 case EWOULDBLOCK:
420#endif
421 return( 1 );
422 }
423 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200425#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427/*
428 * Accept a connection from a remote client
429 */
430int net_accept( int bind_fd, int *client_fd, void *client_ip )
431{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100432#if defined(POLARSSL_HAVE_IPV6)
433 struct sockaddr_storage client_addr;
434#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100436#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
Paul Bakker394c56f2011-12-20 12:19:03 +0000438#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
Manuel Pégourié-Gonnard61462472015-10-05 12:16:06 +0100439 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t)
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 socklen_t n = (socklen_t) sizeof( client_addr );
441#else
442 int n = (int) sizeof( client_addr );
443#endif
444
Paul Bakkerbbc10072013-10-14 16:33:24 +0200445 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
446 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000447
448 if( *client_fd < 0 )
449 {
Manuel Pégourié-Gonnard9a6b4422014-07-21 13:42:54 +0200450 if( net_would_block( bind_fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000451 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
Paul Bakker40e46942009-01-03 21:51:57 +0000453 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 }
455
456 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100457 {
458#if defined(POLARSSL_HAVE_IPV6)
459 if( client_addr.ss_family == AF_INET )
460 {
461 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
462 memcpy( client_ip, &addr4->sin_addr.s_addr,
463 sizeof( addr4->sin_addr.s_addr ) );
464 }
465 else
466 {
467 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
468 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
469 sizeof( addr6->sin6_addr.s6_addr ) );
470 }
471#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 memcpy( client_ip, &client_addr.sin_addr.s_addr,
473 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100474#endif /* POLARSSL_HAVE_IPV6 */
475 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
477 return( 0 );
478}
479
480/*
481 * Set the socket blocking or non-blocking
482 */
483int net_set_block( int fd )
484{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100485#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
486 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000487 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 return( ioctlsocket( fd, FIONBIO, &n ) );
489#else
490 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
491#endif
492}
493
494int net_set_nonblock( int fd )
495{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100496#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
497 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000498 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 return( ioctlsocket( fd, FIONBIO, &n ) );
500#else
501 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
502#endif
503}
504
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200505#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000506/*
507 * Portable usleep helper
508 */
509void net_usleep( unsigned long usec )
510{
Simon Butchera91d85e2016-01-15 14:36:08 +0000511#if defined(_WIN32)
512 Sleep( ( usec + 999 ) / 1000 );
513#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100515 tv.tv_sec = usec / 1000000;
Simon Butchera91d85e2016-01-15 14:36:08 +0000516#if defined(__unix__) || defined(__unix) || \
517 ( defined(__APPLE__) && defined(__MACH__) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100518 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200519#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100520 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200521#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 select( 0, NULL, NULL, NULL, &tv );
Simon Butchera91d85e2016-01-15 14:36:08 +0000523#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000524}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200525#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527/*
528 * Read at most 'len' characters
529 */
Paul Bakker23986e52011-04-24 08:57:21 +0000530int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100531{
Paul Bakker80025412014-01-23 20:59:49 +0100532 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200533 int ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 if( ret < 0 )
536 {
Paul Bakker80025412014-01-23 20:59:49 +0100537 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000538 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100540#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
541 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000543 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544#else
545 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000546 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000549 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550#endif
551
Paul Bakker40e46942009-01-03 21:51:57 +0000552 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 }
554
555 return( ret );
556}
557
558/*
559 * Write at most 'len' characters
560 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000561int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000562{
Paul Bakker80025412014-01-23 20:59:49 +0100563 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200564 int ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
566 if( ret < 0 )
567 {
Paul Bakker80025412014-01-23 20:59:49 +0100568 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000569 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100571#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
572 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000573 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000574 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575#else
576 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000577 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
579 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000580 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581#endif
582
Paul Bakker40e46942009-01-03 21:51:57 +0000583 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 }
585
586 return( ret );
587}
588
589/*
590 * Gracefully close the connection
591 */
592void net_close( int fd )
593{
594 shutdown( fd, 2 );
595 close( fd );
596}
597
Paul Bakker9af723c2014-05-01 13:03:14 +0200598#endif /* POLARSSL_NET_C */