blob: a8591ed0d0a6bd38436e11af1ad5666cc57e6e5c [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.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
Paul Bakker66d5d072014-06-17 16:39:18 +0200132unsigned short net_htons( unsigned short n );
133unsigned long net_htonl( unsigned long n );
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000134#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100135#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
137/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100138 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100140static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000141{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100142#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
143 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 WSADATA wsaData;
145
146 if( wsa_init_done == 0 )
147 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200148 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000149 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151 wsa_init_done = 1;
152 }
153#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100154#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 signal( SIGPIPE, SIG_IGN );
156#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100157#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100158 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100159}
160
161/*
162 * Initiate a TCP connection with host:port
163 */
164int net_connect( int *fd, const char *host, int port )
165{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100166#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100167 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100168 struct addrinfo hints, *addr_list, *cur;
169 char port_str[6];
170
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100171 if( ( ret = net_prepare() ) != 0 )
172 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100173
174 /* getaddrinfo expects port as a string */
175 memset( port_str, 0, sizeof( port_str ) );
176 snprintf( port_str, sizeof( port_str ), "%d", port );
177
178 /* Do name resolution with both IPv6 and IPv4, but only TCP */
179 memset( &hints, 0, sizeof( hints ) );
180 hints.ai_family = AF_UNSPEC;
181 hints.ai_socktype = SOCK_STREAM;
182 hints.ai_protocol = IPPROTO_TCP;
183
184 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
185 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
186
187 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100188 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100189 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
190 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100191 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
192 cur->ai_protocol );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100193 if( *fd < 0 )
194 {
195 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
196 continue;
197 }
198
199 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
200 {
201 ret = 0;
202 break;
203 }
204
205 close( *fd );
206 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
207 }
208
209 freeaddrinfo( addr_list );
210
211 return( ret );
212
213#else
214 /* Legacy IPv4-only version */
215
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100216 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100217 struct sockaddr_in server_addr;
218 struct hostent *server_host;
219
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100220 if( ( ret = net_prepare() ) != 0 )
221 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
223 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000224 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
Paul Bakkerbbc10072013-10-14 16:33:24 +0200226 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000227 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
229 memcpy( (void *) &server_addr.sin_addr,
230 (void *) server_host->h_addr,
231 server_host->h_length );
232
233 server_addr.sin_family = AF_INET;
234 server_addr.sin_port = net_htons( port );
235
236 if( connect( *fd, (struct sockaddr *) &server_addr,
237 sizeof( server_addr ) ) < 0 )
238 {
239 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000240 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 }
242
243 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100244#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000245}
246
247/*
248 * Create a listening socket on bind_ip:port
249 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000250int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000251{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100252#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100253 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100254 struct addrinfo hints, *addr_list, *cur;
255 char port_str[6];
256
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100257 if( ( ret = net_prepare() ) != 0 )
258 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100259
260 /* getaddrinfo expects port as a string */
261 memset( port_str, 0, sizeof( port_str ) );
262 snprintf( port_str, sizeof( port_str ), "%d", port );
263
264 /* Bind to IPv6 and/or IPv4, but only in TCP */
265 memset( &hints, 0, sizeof( hints ) );
266 hints.ai_family = AF_UNSPEC;
267 hints.ai_socktype = SOCK_STREAM;
268 hints.ai_protocol = IPPROTO_TCP;
269 if( bind_ip == NULL )
270 hints.ai_flags = AI_PASSIVE;
271
272 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
273 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
274
275 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100276 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100277 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
278 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100279 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
280 cur->ai_protocol );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100281 if( *fd < 0 )
282 {
283 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
284 continue;
285 }
286
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100287 n = 1;
Paul Bakker874bd642014-04-17 12:43:05 +0200288 if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
289 (const char *) &n, sizeof( n ) ) != 0 )
290 {
291 close( *fd );
292 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
293 continue;
294 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100295
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100296 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
297 {
298 close( *fd );
299 ret = POLARSSL_ERR_NET_BIND_FAILED;
300 continue;
301 }
302
303 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
304 {
305 close( *fd );
306 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
307 continue;
308 }
309
310 /* I we ever get there, it's a success */
311 ret = 0;
312 break;
313 }
314
315 freeaddrinfo( addr_list );
316
317 return( ret );
318
319#else
320 /* Legacy IPv4-only version */
321
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100322 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 struct sockaddr_in server_addr;
324
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100325 if( ( ret = net_prepare() ) != 0 )
326 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
Paul Bakkerbbc10072013-10-14 16:33:24 +0200328 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000329 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
331 n = 1;
332 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
333 (const char *) &n, sizeof( n ) );
334
Paul Bakker37286a52013-03-06 16:55:11 +0100335 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 server_addr.sin_family = AF_INET;
337 server_addr.sin_port = net_htons( port );
338
339 if( bind_ip != NULL )
340 {
341 memset( c, 0, sizeof( c ) );
342 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
343
344 for( n = 0; n < 4; n++ )
345 if( c[n] < 0 || c[n] > 255 )
346 break;
347
348 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100349 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000350 ( (uint32_t) c[0] << 24 ) |
351 ( (uint32_t) c[1] << 16 ) |
352 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100353 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 }
355
356 if( bind( *fd, (struct sockaddr *) &server_addr,
357 sizeof( server_addr ) ) < 0 )
358 {
359 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000360 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 }
362
Paul Bakker192381a2011-05-20 12:31:31 +0000363 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 {
365 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000366 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 }
368
369 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100370#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000371}
372
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100373#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
374 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100375/*
376 * Check if the requested operation would be blocking on a non-blocking socket
377 * and thus 'failed' with a negative return value.
378 */
379static int net_would_block( int fd )
380{
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +0100381 ((void) fd);
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100383}
Paul Bakker5121ce52009-01-03 21:22:43 +0000384#else
Paul Bakker80025412014-01-23 20:59:49 +0100385/*
386 * Check if the requested operation would be blocking on a non-blocking socket
387 * and thus 'failed' with a negative return value.
388 *
389 * Note: on a blocking socket this function always returns 0!
390 */
391static int net_would_block( int fd )
392{
393 /*
394 * Never return 'WOULD BLOCK' on a non-blocking socket
395 */
396 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
397 return( 0 );
398
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 switch( errno )
400 {
401#if defined EAGAIN
402 case EAGAIN:
403#endif
404#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
405 case EWOULDBLOCK:
406#endif
407 return( 1 );
408 }
409 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200411#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
413/*
414 * Accept a connection from a remote client
415 */
416int net_accept( int bind_fd, int *client_fd, void *client_ip )
417{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100418#if defined(POLARSSL_HAVE_IPV6)
419 struct sockaddr_storage client_addr;
420#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100422#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
Paul Bakker394c56f2011-12-20 12:19:03 +0000424#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
425 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 socklen_t n = (socklen_t) sizeof( client_addr );
427#else
428 int n = (int) sizeof( client_addr );
429#endif
430
Paul Bakkerbbc10072013-10-14 16:33:24 +0200431 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
432 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
434 if( *client_fd < 0 )
435 {
Manuel Pégourié-Gonnard9a6b4422014-07-21 13:42:54 +0200436 if( net_would_block( bind_fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000437 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
Paul Bakker40e46942009-01-03 21:51:57 +0000439 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 }
441
442 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100443 {
444#if defined(POLARSSL_HAVE_IPV6)
445 if( client_addr.ss_family == AF_INET )
446 {
447 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
448 memcpy( client_ip, &addr4->sin_addr.s_addr,
449 sizeof( addr4->sin_addr.s_addr ) );
450 }
451 else
452 {
453 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
454 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
455 sizeof( addr6->sin6_addr.s6_addr ) );
456 }
457#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 memcpy( client_ip, &client_addr.sin_addr.s_addr,
459 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100460#endif /* POLARSSL_HAVE_IPV6 */
461 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
463 return( 0 );
464}
465
466/*
467 * Set the socket blocking or non-blocking
468 */
469int net_set_block( int fd )
470{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100471#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
472 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000473 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 return( ioctlsocket( fd, FIONBIO, &n ) );
475#else
476 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
477#endif
478}
479
480int net_set_nonblock( int fd )
481{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100482#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
483 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000484 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 return( ioctlsocket( fd, FIONBIO, &n ) );
486#else
487 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
488#endif
489}
490
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200491#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000492/*
493 * Portable usleep helper
494 */
495void net_usleep( unsigned long usec )
496{
497 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100498 tv.tv_sec = usec / 1000000;
Paul Bakker82788fb2014-10-20 13:59:19 +0200499#if !defined(_WIN32) && ( defined(__unix__) || defined(__unix) || \
500 ( defined(__APPLE__) && defined(__MACH__) ) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100501 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200502#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100503 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200504#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 select( 0, NULL, NULL, NULL, &tv );
506}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200507#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509/*
510 * Read at most 'len' characters
511 */
Paul Bakker23986e52011-04-24 08:57:21 +0000512int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100513{
Paul Bakker80025412014-01-23 20:59:49 +0100514 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200515 int ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 if( ret < 0 )
518 {
Paul Bakker80025412014-01-23 20:59:49 +0100519 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000520 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100522#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
523 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000525 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526#else
527 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000528 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
530 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000531 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532#endif
533
Paul Bakker40e46942009-01-03 21:51:57 +0000534 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 }
536
537 return( ret );
538}
539
540/*
541 * Write at most 'len' characters
542 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000543int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000544{
Paul Bakker80025412014-01-23 20:59:49 +0100545 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200546 int ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548 if( ret < 0 )
549 {
Paul Bakker80025412014-01-23 20:59:49 +0100550 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000551 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100553#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
554 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000556 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557#else
558 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000559 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
561 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000562 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563#endif
564
Paul Bakker40e46942009-01-03 21:51:57 +0000565 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 }
567
568 return( ret );
569}
570
571/*
572 * Gracefully close the connection
573 */
574void net_close( int fd )
575{
576 shutdown( fd, 2 );
577 close( fd );
578}
579
Paul Bakker9af723c2014-05-01 13:03:14 +0200580#endif /* POLARSSL_NET_C */