blob: 17210ef482174ff6be86bc5f2330d7cc0a2ebb3d [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
Paul Bakkerfa6a6202013-10-28 18:48:30 +010033#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
34 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010036#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010037#ifdef _WIN32_WINNT
38#undef _WIN32_WINNT
39#endif
40/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010041#define _WIN32_WINNT 0x0501
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010042#include <ws2tcpip.h>
43#endif
44
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010045#include <winsock2.h>
46#include <windows.h>
47
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010048#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000049#if defined(_WIN32_WCE)
50#pragma comment( lib, "ws2.lib" )
51#else
52#pragma comment( lib, "ws2_32.lib" )
53#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010054#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Paul Bakkerf4f69682011-04-24 16:08:12 +000056#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
57#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000058#define close(fd) closesocket(fd)
59
60static int wsa_init_done = 0;
61
Paul Bakkerdb20c102014-06-17 14:34:44 +020062#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64#include <sys/types.h>
65#include <sys/socket.h>
66#include <netinet/in.h>
67#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020068#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000069#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020070#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000071#include <unistd.h>
72#include <signal.h>
73#include <fcntl.h>
74#include <netdb.h>
75#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000076
Paul Bakker6a2f8572012-08-23 07:45:37 +000077#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
Markus Pfeiffera26a0052014-04-22 20:16:15 +000078 defined(__DragonFly__)
Paul Bakker854963c2009-07-19 20:50:11 +000079#include <sys/endian.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010080#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
81 defined(EFIX64) || defined(EFI32)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000082#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000083#elif defined(sun)
84#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020085#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
86#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000087#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000088#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000089#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Paul Bakkerdb20c102014-06-17 14:34:44 +020091#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Paul Bakker5121ce52009-01-03 21:22:43 +000093#include <stdlib.h>
94#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020095
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +010096#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
97 !defined(EFI32)
98#define snprintf _snprintf
99#endif
100
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200101#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000102#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200103#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100105#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000106#include <basetsd.h>
107typedef UINT32 uint32_t;
108#else
109#include <inttypes.h>
110#endif
111
Paul Bakker5121ce52009-01-03 21:22:43 +0000112/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000113 * htons() is not always available.
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200114 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and
115 * __BIG_ENDIAN to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200117#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
118 __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000119#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100120#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000121#else
Paul Bakker37286a52013-03-06 16:55:11 +0100122#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
123 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
124#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
125 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
126 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
127 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000128#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Paul Bakker66d5d072014-06-17 16:39:18 +0200130unsigned short net_htons( unsigned short n );
131unsigned long net_htonl( unsigned long n );
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000132#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100133#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100136 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100138static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000139{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100140#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
141 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 WSADATA wsaData;
143
144 if( wsa_init_done == 0 )
145 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200146 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000147 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
149 wsa_init_done = 1;
150 }
151#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100152#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 signal( SIGPIPE, SIG_IGN );
154#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100155#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100156 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100157}
158
159/*
160 * Initiate a TCP connection with host:port
161 */
162int net_connect( int *fd, const char *host, int port )
163{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100164#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100165 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100166 struct addrinfo hints, *addr_list, *cur;
167 char port_str[6];
168
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100169 if( ( ret = net_prepare() ) != 0 )
170 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100171
172 /* getaddrinfo expects port as a string */
173 memset( port_str, 0, sizeof( port_str ) );
174 snprintf( port_str, sizeof( port_str ), "%d", port );
175
176 /* Do name resolution with both IPv6 and IPv4, but only TCP */
177 memset( &hints, 0, sizeof( hints ) );
178 hints.ai_family = AF_UNSPEC;
179 hints.ai_socktype = SOCK_STREAM;
180 hints.ai_protocol = IPPROTO_TCP;
181
182 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
183 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
184
185 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100186 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100187 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
188 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100189 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
190 cur->ai_protocol );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100191 if( *fd < 0 )
192 {
193 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
194 continue;
195 }
196
197 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
198 {
199 ret = 0;
200 break;
201 }
202
203 close( *fd );
204 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
205 }
206
207 freeaddrinfo( addr_list );
208
209 return( ret );
210
211#else
212 /* Legacy IPv4-only version */
213
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100214 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100215 struct sockaddr_in server_addr;
216 struct hostent *server_host;
217
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100218 if( ( ret = net_prepare() ) != 0 )
219 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000222 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
Paul Bakkerbbc10072013-10-14 16:33:24 +0200224 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000225 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
227 memcpy( (void *) &server_addr.sin_addr,
228 (void *) server_host->h_addr,
229 server_host->h_length );
230
231 server_addr.sin_family = AF_INET;
232 server_addr.sin_port = net_htons( port );
233
234 if( connect( *fd, (struct sockaddr *) &server_addr,
235 sizeof( server_addr ) ) < 0 )
236 {
237 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000238 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 }
240
241 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100242#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000243}
244
245/*
246 * Create a listening socket on bind_ip:port
247 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000248int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000249{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100250#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100251 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100252 struct addrinfo hints, *addr_list, *cur;
253 char port_str[6];
254
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100255 if( ( ret = net_prepare() ) != 0 )
256 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100257
258 /* getaddrinfo expects port as a string */
259 memset( port_str, 0, sizeof( port_str ) );
260 snprintf( port_str, sizeof( port_str ), "%d", port );
261
262 /* Bind to IPv6 and/or IPv4, but only in TCP */
263 memset( &hints, 0, sizeof( hints ) );
264 hints.ai_family = AF_UNSPEC;
265 hints.ai_socktype = SOCK_STREAM;
266 hints.ai_protocol = IPPROTO_TCP;
267 if( bind_ip == NULL )
268 hints.ai_flags = AI_PASSIVE;
269
270 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
271 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
272
273 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100274 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100275 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
276 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100277 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
278 cur->ai_protocol );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100279 if( *fd < 0 )
280 {
281 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
282 continue;
283 }
284
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100285 n = 1;
Paul Bakker874bd642014-04-17 12:43:05 +0200286 if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
287 (const char *) &n, sizeof( n ) ) != 0 )
288 {
289 close( *fd );
290 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
291 continue;
292 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100293
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100294 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
295 {
296 close( *fd );
297 ret = POLARSSL_ERR_NET_BIND_FAILED;
298 continue;
299 }
300
301 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
302 {
303 close( *fd );
304 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
305 continue;
306 }
307
308 /* I we ever get there, it's a success */
309 ret = 0;
310 break;
311 }
312
313 freeaddrinfo( addr_list );
314
315 return( ret );
316
317#else
318 /* Legacy IPv4-only version */
319
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100320 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 struct sockaddr_in server_addr;
322
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100323 if( ( ret = net_prepare() ) != 0 )
324 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
Paul Bakkerbbc10072013-10-14 16:33:24 +0200326 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000327 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329 n = 1;
330 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
331 (const char *) &n, sizeof( n ) );
332
Paul Bakker37286a52013-03-06 16:55:11 +0100333 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 server_addr.sin_family = AF_INET;
335 server_addr.sin_port = net_htons( port );
336
337 if( bind_ip != NULL )
338 {
339 memset( c, 0, sizeof( c ) );
340 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
341
342 for( n = 0; n < 4; n++ )
343 if( c[n] < 0 || c[n] > 255 )
344 break;
345
346 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100347 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000348 ( (uint32_t) c[0] << 24 ) |
349 ( (uint32_t) c[1] << 16 ) |
350 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100351 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 }
353
354 if( bind( *fd, (struct sockaddr *) &server_addr,
355 sizeof( server_addr ) ) < 0 )
356 {
357 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000358 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 }
360
Paul Bakker192381a2011-05-20 12:31:31 +0000361 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 {
363 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000364 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 }
366
367 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100368#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000369}
370
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100371#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
372 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100373/*
374 * Check if the requested operation would be blocking on a non-blocking socket
375 * and thus 'failed' with a negative return value.
376 */
377static int net_would_block( int fd )
378{
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +0100379 ((void) fd);
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100381}
Paul Bakker5121ce52009-01-03 21:22:43 +0000382#else
Paul Bakker80025412014-01-23 20:59:49 +0100383/*
384 * Check if the requested operation would be blocking on a non-blocking socket
385 * and thus 'failed' with a negative return value.
386 *
387 * Note: on a blocking socket this function always returns 0!
388 */
389static int net_would_block( int fd )
390{
391 /*
392 * Never return 'WOULD BLOCK' on a non-blocking socket
393 */
394 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
395 return( 0 );
396
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 switch( errno )
398 {
399#if defined EAGAIN
400 case EAGAIN:
401#endif
402#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
403 case EWOULDBLOCK:
404#endif
405 return( 1 );
406 }
407 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000408}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200409#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
411/*
412 * Accept a connection from a remote client
413 */
414int net_accept( int bind_fd, int *client_fd, void *client_ip )
415{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100416#if defined(POLARSSL_HAVE_IPV6)
417 struct sockaddr_storage client_addr;
418#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100420#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
Paul Bakker394c56f2011-12-20 12:19:03 +0000422#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
423 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 socklen_t n = (socklen_t) sizeof( client_addr );
425#else
426 int n = (int) sizeof( client_addr );
427#endif
428
Paul Bakkerbbc10072013-10-14 16:33:24 +0200429 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
430 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000431
432 if( *client_fd < 0 )
433 {
Manuel Pégourié-Gonnard9a6b4422014-07-21 13:42:54 +0200434 if( net_would_block( bind_fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000435 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
Paul Bakker40e46942009-01-03 21:51:57 +0000437 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 }
439
440 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100441 {
442#if defined(POLARSSL_HAVE_IPV6)
443 if( client_addr.ss_family == AF_INET )
444 {
445 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
446 memcpy( client_ip, &addr4->sin_addr.s_addr,
447 sizeof( addr4->sin_addr.s_addr ) );
448 }
449 else
450 {
451 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
452 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
453 sizeof( addr6->sin6_addr.s6_addr ) );
454 }
455#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 memcpy( client_ip, &client_addr.sin_addr.s_addr,
457 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100458#endif /* POLARSSL_HAVE_IPV6 */
459 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461 return( 0 );
462}
463
464/*
465 * Set the socket blocking or non-blocking
466 */
467int net_set_block( int fd )
468{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100469#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
470 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000471 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 return( ioctlsocket( fd, FIONBIO, &n ) );
473#else
474 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
475#endif
476}
477
478int net_set_nonblock( int fd )
479{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100480#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
481 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000482 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 return( ioctlsocket( fd, FIONBIO, &n ) );
484#else
485 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
486#endif
487}
488
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200489#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000490/*
491 * Portable usleep helper
492 */
493void net_usleep( unsigned long usec )
494{
495 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100496 tv.tv_sec = usec / 1000000;
Paul Bakker82788fb2014-10-20 13:59:19 +0200497#if !defined(_WIN32) && ( defined(__unix__) || defined(__unix) || \
498 ( defined(__APPLE__) && defined(__MACH__) ) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100499 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200500#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100501 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200502#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 select( 0, NULL, NULL, NULL, &tv );
504}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200505#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
507/*
508 * Read at most 'len' characters
509 */
Paul Bakker23986e52011-04-24 08:57:21 +0000510int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100511{
Paul Bakker80025412014-01-23 20:59:49 +0100512 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200513 int ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 if( ret < 0 )
516 {
Paul Bakker80025412014-01-23 20:59:49 +0100517 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000518 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100520#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
521 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000523 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524#else
525 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000526 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000529 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000530#endif
531
Paul Bakker40e46942009-01-03 21:51:57 +0000532 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 }
534
535 return( ret );
536}
537
538/*
539 * Write at most 'len' characters
540 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000541int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000542{
Paul Bakker80025412014-01-23 20:59:49 +0100543 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200544 int ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
546 if( ret < 0 )
547 {
Paul Bakker80025412014-01-23 20:59:49 +0100548 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000549 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100551#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
552 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000554 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555#else
556 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000557 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000560 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000561#endif
562
Paul Bakker40e46942009-01-03 21:51:57 +0000563 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 }
565
566 return( ret );
567}
568
569/*
570 * Gracefully close the connection
571 */
572void net_close( int fd )
573{
574 shutdown( fd, 2 );
575 close( fd );
576}
577
Paul Bakker9af723c2014-05-01 13:03:14 +0200578#endif /* POLARSSL_NET_C */